Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/api/providers/base-openai-compatible-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>

let lastUsage: OpenAI.CompletionUsage | undefined
const activeToolCallIds = new Set<string>()
let hasEmittedToolCallEnd = false

for await (const chunk of stream) {
// Check for provider-specific error responses (e.g., MiniMax base_resp)
Expand Down Expand Up @@ -177,12 +178,14 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
}
}

// Emit tool_call_end events when finish_reason is "tool_calls"
// This ensures tool calls are finalized even if the stream doesn't properly close
if (finishReason === "tool_calls" && activeToolCallIds.size > 0) {
// Emit tool_call_end events when ANY finish_reason is present (not just "tool_calls")
// This ensures compatibility with various OpenAI-compatible servers including ik_llama.cpp
// that may not set finish_reason to "tool_calls" specifically
if (finishReason && activeToolCallIds.size > 0 && !hasEmittedToolCallEnd) {
for (const id of activeToolCallIds) {
yield { type: "tool_call_end", id }
}
hasEmittedToolCallEnd = true
activeToolCallIds.clear()
}

Expand All @@ -191,6 +194,15 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
}
}

// Fallback: If stream ends with active tool calls that weren't finalized
// This is crucial for servers like ik_llama.cpp that may not send proper finish_reason
if (activeToolCallIds.size > 0 && !hasEmittedToolCallEnd) {
for (const id of activeToolCallIds) {
yield { type: "tool_call_end", id }
}
activeToolCallIds.clear()
}

if (lastUsage) {
yield this.processUsageMetrics(lastUsage, this.getModel().info)
}
Expand Down
Loading