From 7a01cebd1d0dce2d7372381510656fe8a905f0f5 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 22 Jul 2025 08:20:10 +0200 Subject: [PATCH] disallow empty text parts --- .../systemMessageTools/textifySystemTools.ts | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 core/tools/systemMessageTools/textifySystemTools.ts diff --git a/core/tools/systemMessageTools/textifySystemTools.ts b/core/tools/systemMessageTools/textifySystemTools.ts new file mode 100644 index 000000000..f654037df --- /dev/null +++ b/core/tools/systemMessageTools/textifySystemTools.ts @@ -0,0 +1,102 @@ +import { + AssistantChatMessage, + MessagePart, + ToolCallState, + UserChatMessage, +} from "../.."; +import { + normalizeToMessageParts, + renderContextItems, +} from "../../util/messageContent"; +import { + CANCELLED_TOOL_CALL_MESSAGE, + NO_TOOL_CALL_OUTPUT_MESSAGE, +} from "../constants"; +function toolCallStateToSystemToolCall(state: ToolCallState): string { + let parts = ["```tool"]; + parts.push(`TOOL_NAME: ${state.toolCall.function.name}`); + try { + for (const arg in state.parsedArgs) { + parts.push(`BEGIN_ARG: ${arg}`); + parts.push(JSON.stringify(state.parsedArgs[arg])); + parts.push(`END_ARG`); + } + } catch (e) { + console.log("Failed to stringify json args", state.parsedArgs); + } + // TODO - include tool call id for parallel. Confuses dumb models + parts.push("```"); + return parts.join("\n"); +} + +function toolCallStateToSystemToolOutput(state: ToolCallState): string { + let output = `Tool output for ${state.toolCall.function.name} tool call:\n\n`; + // TODO - include tool call id for parallel. Confuses dumb models + // let output = `Tool output for tool call ${state.toolCallId} (${state.toolCall.function.name}):\n\n`; + if (state.status === "canceled") { + output += CANCELLED_TOOL_CALL_MESSAGE; + } else if (state.output?.length) { + output += renderContextItems(state.output); + } else { + output += NO_TOOL_CALL_OUTPUT_MESSAGE; + } + return output; +} + +export function convertToolCallStatesToSystemCallsAndOutput( + originalAssistantMessage: AssistantChatMessage, + toolCallStates: ToolCallState[], +): { + assistantMessage: AssistantChatMessage; + userMessage: UserChatMessage; +} { + const assistantParts = normalizeToMessageParts(originalAssistantMessage); + if (originalAssistantMessage.toolCalls) { + assistantParts.push( + ...toolCallStates.map((state) => ({ + type: "text" as const, + text: toolCallStateToSystemToolCall(state), + })), + ); + } + + const userParts: MessagePart[] = []; + if (toolCallStates) { + for (const state of toolCallStates) { + userParts.push({ + type: "text", + text: toolCallStateToSystemToolOutput(state), + }); + } + } + if (userParts.length === 0) { + userParts.push({ + type: "text", + text: "No tool output for tool calls", + }); + } + + const nonEmptyAssistantParts = assistantParts.filter( + (p) => p.type !== "text" || !!p.text, + ); + const nonEmptyUserParts = userParts.filter( + (p) => p.type !== "text" || !!p.text, + ); + + // new message with tool calls moved to content + const assistantMessage: AssistantChatMessage = { + ...originalAssistantMessage, + content: nonEmptyAssistantParts, + toolCalls: undefined, // remove tool calls from the assistant message + }; + + const userMessage: UserChatMessage = { + role: "user", + content: nonEmptyUserParts, + }; + + return { + assistantMessage, + userMessage, + }; +}