Fix tool call reuse between Claude and OAI
| @@ -89,6 +89,15 @@ function tryParse(str) { | |||
| 89 | } | 89 | } |
| 90 | 90 | ||
| 91 | /** | 91 | /** |
| 92 | * Stringifies an object if it is not already a string. | ||
| 93 | * @param {any} obj The object to stringify | ||
| 94 | * @returns {string} A JSON string representation of the object. | ||
| 95 | */ | ||
| 96 | function stringify(obj) { | ||
| 97 | return typeof obj === 'string' ? obj : JSON.stringify(obj); | ||
| 98 | } | ||
| 99 | |||
| 100 | /** | ||
| 92 | * A class that represents a tool definition. | 101 | * A class that represents a tool definition. |
| 93 | */ | 102 | */ |
| 94 | class ToolDefinition { | 103 | class ToolDefinition { |
| @@ -571,7 +580,7 @@ export class ToolManager { | |||
| 571 | id, | 580 | id, |
| 572 | displayName, | 581 | displayName, |
| 573 | name, | 582 | name, |
| 574 | parameters, | 583 | parameters: stringify(parameters), |
| 575 | result: toolResult, | 584 | result: toolResult, |
| 576 | }; | 585 | }; |
| 577 | result.invocations.push(invocation); | 586 | result.invocations.push(invocation); |
| @@ -131,13 +131,14 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi | |||
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | // Now replace all further messages that have the role 'system' with the role 'user'. (or all if we're not using one) | 133 | // Now replace all further messages that have the role 'system' with the role 'user'. (or all if we're not using one) |
| 134 | const parse = (str) => typeof str === 'string' ? JSON.parse(str) : str; | ||
| 134 | messages.forEach((message) => { | 135 | messages.forEach((message) => { |
| 135 | if (message.role === 'assistant' && message.tool_calls) { | 136 | if (message.role === 'assistant' && message.tool_calls) { |
| 136 | message.content = message.tool_calls.map((tc) => ({ | 137 | message.content = message.tool_calls.map((tc) => ({ |
| 137 | type: 'tool_use', | 138 | type: 'tool_use', |
| 138 | id: tc.id, | 139 | id: tc.id, |
| 139 | name: tc.function.name, | 140 | name: tc.function.name, |
| 140 | input: tc.function.arguments, | 141 | input: parse(tc.function.arguments), |
| 141 | })); | 142 | })); |
| 142 | } | 143 | } |
| 143 | 144 | ||