diff --git a/public/scripts/tool-calling.js b/public/scripts/tool-calling.js index 26ae26091..677abc16e 100644 --- a/public/scripts/tool-calling.js +++ b/public/scripts/tool-calling.js @@ -88,6 +88,15 @@ function tryParse(str) { } } +/** + * Stringifies an object if it is not already a string. + * @param {any} obj The object to stringify + * @returns {string} A JSON string representation of the object. + */ +function stringify(obj) { + return typeof obj === 'string' ? obj : JSON.stringify(obj); +} + /** * A class that represents a tool definition. */ @@ -571,7 +580,7 @@ export class ToolManager { id, displayName, name, - parameters, + parameters: stringify(parameters), result: toolResult, }; result.invocations.push(invocation); diff --git a/src/prompt-converters.js b/src/prompt-converters.js index 0d1412301..19afabbaf 100644 --- a/src/prompt-converters.js +++ b/src/prompt-converters.js @@ -131,13 +131,14 @@ function convertClaudeMessages(messages, prefillString, useSysPrompt, humanMsgFi } // Now replace all further messages that have the role 'system' with the role 'user'. (or all if we're not using one) + const parse = (str) => typeof str === 'string' ? JSON.parse(str) : str; messages.forEach((message) => { if (message.role === 'assistant' && message.tool_calls) { message.content = message.tool_calls.map((tc) => ({ type: 'tool_use', id: tc.id, name: tc.function.name, - input: tc.function.arguments, + input: parse(tc.function.arguments), })); }