Fix tool call reuse between Claude and OAI

6b022e783da7e53b928060e889c6975c564e2d87

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +12 -2Ignore whitespace
public/scripts/tool-calling.js+10 -1
@@ -89,6 +89,15 @@ function tryParse(str) {
89}89}
9090
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 */
96function 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 */
94class ToolDefinition {103class 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);
src/prompt-converters.js+2 -1
@@ -131,13 +131,14 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi
131 }131 }
132132
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 }
143144