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 | 101 | * A class that represents a tool definition. |
| 93 | 102 | */ |
| 94 | 103 | class ToolDefinition { |
| @@ -571,7 +580,7 @@ export class ToolManager { | ||
| 571 | 580 | id, |
| 572 | 581 | displayName, |
| 573 | 582 | name, |
| 574 | 583 | parameters: stringify(parameters), |
| 575 | 584 | result: toolResult, |
| 576 | 585 | }; |
| 577 | 586 | result.invocations.push(invocation); |
| @@ -131,13 +131,14 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi | ||
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 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 | 135 | messages.forEach((message) => { |
| 135 | 136 | if (message.role === 'assistant' && message.tool_calls) { |
| 136 | 137 | message.content = message.tool_calls.map((tc) => ({ |
| 137 | 138 | type: 'tool_use', |
| 138 | 139 | id: tc.id, |
| 139 | 140 | name: tc.function.name, |
| 140 | 141 | input: parse(tc.function.arguments), |
| 141 | 142 | })); |
| 142 | 143 | } |
| 143 | 144 | |