Fix tool call reuse between Claude and OAI

6b022e783da7e53b928060e889c6975c564e2d87

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

2 files changed, +12 -2Showing whitespace changes
public/scripts/tool-calling.js+10 -1
@@ -89,6 +89,15 @@ function tryParse(str) {
8989}
9090
9191/**
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+/**
92101 * A class that represents a tool definition.
93102 */
94103class ToolDefinition {
@@ -571,7 +580,7 @@ export class ToolManager {
571580 id,
572581 displayName,
573582 name,
574583 parameters: stringify(parameters),
575584 result: toolResult,
576585 };
577586 result.invocations.push(invocation);
src/prompt-converters.js+2 -1
@@ -131,13 +131,14 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi
131131 }
132132
133133 // 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;
134135 messages.forEach((message) => {
135136 if (message.role === 'assistant' && message.tool_calls) {
136137 message.content = message.tool_calls.map((tc) => ({
137138 type: 'tool_use',
138139 id: tc.id,
139140 name: tc.function.name,
140141 input: parse(tc.function.arguments),
141142 }));
142143 }
143144