mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 02:01:29 +00:00
fix: include empty tools param when conversation has tool history (#150)
Anthropic (via LiteLLM/proxy) requires the `tools` parameter to be present when messages include tool_calls or tool role messages, even if no tools are currently being provided. This adds a `hasToolHistory()` helper to detect if the conversation contains tool calls or tool results, and ensures `tools: []` is included in the request params when needed. Fixes #149 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Ubuntu <ubuntu@ip-172-31-50-87.us-west-2.compute.internal> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
00370cab39
commit
8f67e00160
1 changed files with 23 additions and 0 deletions
|
|
@ -26,6 +26,26 @@ import { parseStreamingJson } from "../utils/json-parse.js";
|
|||
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
||||
|
||||
import { transformMessages } from "./transorm-messages.js";
|
||||
import type { Message } from "../types.js";
|
||||
|
||||
/**
|
||||
* Check if conversation messages contain tool calls or tool results.
|
||||
* This is needed because Anthropic (via proxy) requires the tools param
|
||||
* to be present when messages include tool_calls or tool role messages.
|
||||
*/
|
||||
function hasToolHistory(messages: Message[]): boolean {
|
||||
for (const msg of messages) {
|
||||
if (msg.role === "toolResult") {
|
||||
return true;
|
||||
}
|
||||
if (msg.role === "assistant") {
|
||||
if (msg.content.some((block) => block.type === "toolCall")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export interface OpenAICompletionsOptions extends StreamOptions {
|
||||
toolChoice?: "auto" | "none" | "required" | { type: "function"; function: { name: string } };
|
||||
|
|
@ -296,6 +316,9 @@ function buildParams(model: Model<"openai-completions">, context: Context, optio
|
|||
|
||||
if (context.tools) {
|
||||
params.tools = convertTools(context.tools);
|
||||
} else if (hasToolHistory(context.messages)) {
|
||||
// Anthropic (via LiteLLM/proxy) requires tools param when conversation has tool_calls/tool_results
|
||||
params.tools = [];
|
||||
}
|
||||
|
||||
if (options?.toolChoice) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue