mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-22 00:00:27 +00:00
fix(ai): Sanitize tool call IDs for Anthropic API compatibility
- Anthropic API requires tool call IDs to match pattern ^[a-zA-Z0-9_-]+$ - OpenAI Responses API generates IDs with pipe character (|) which breaks Anthropic - Added sanitizeToolCallId() to replace invalid characters with underscores - Fixes cross-provider handoffs from OpenAI Responses to Anthropic - Added test to verify the fix works
This commit is contained in:
parent
080bf67306
commit
6679a83b32
3 changed files with 180 additions and 61 deletions
|
|
@ -282,6 +282,12 @@ function buildParams(
|
|||
return params;
|
||||
}
|
||||
|
||||
// Sanitize tool call IDs to match Anthropic's required pattern: ^[a-zA-Z0-9_-]+$
|
||||
function sanitizeToolCallId(id: string): string {
|
||||
// Replace any character that isn't alphanumeric, underscore, or hyphen with underscore
|
||||
return id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
}
|
||||
|
||||
function convertMessages(messages: Message[], model: Model<"anthropic-messages">): MessageParam[] {
|
||||
const params: MessageParam[] = [];
|
||||
|
||||
|
|
@ -348,7 +354,7 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
|
|||
} else if (block.type === "toolCall") {
|
||||
blocks.push({
|
||||
type: "tool_use",
|
||||
id: block.id,
|
||||
id: sanitizeToolCallId(block.id),
|
||||
name: block.name,
|
||||
input: block.arguments,
|
||||
});
|
||||
|
|
@ -365,7 +371,7 @@ function convertMessages(messages: Message[], model: Model<"anthropic-messages">
|
|||
content: [
|
||||
{
|
||||
type: "tool_result",
|
||||
tool_use_id: msg.toolCallId,
|
||||
tool_use_id: sanitizeToolCallId(msg.toolCallId),
|
||||
content: msg.content,
|
||||
is_error: msg.isError,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue