Fix tool call reuse between Claude and OAI

This commit is contained in:
Cohee 2024-10-06 22:22:19 +03:00
parent f7ec2e47d6
commit 6b022e783d
2 changed files with 12 additions and 2 deletions

View File

@ -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. * A class that represents a tool definition.
*/ */
@ -571,7 +580,7 @@ export class ToolManager {
id, id,
displayName, displayName,
name, name,
parameters, parameters: stringify(parameters),
result: toolResult, result: toolResult,
}; };
result.invocations.push(invocation); result.invocations.push(invocation);

View File

@ -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) // 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) => { messages.forEach((message) => {
if (message.role === 'assistant' && message.tool_calls) { if (message.role === 'assistant' && message.tool_calls) {
message.content = message.tool_calls.map((tc) => ({ message.content = message.tool_calls.map((tc) => ({
type: 'tool_use', type: 'tool_use',
id: tc.id, id: tc.id,
name: tc.function.name, name: tc.function.name,
input: tc.function.arguments, input: parse(tc.function.arguments),
})); }));
} }