| 1 | import { |
| 2 | AssistantChatMessage, |
| 3 | MessagePart, |
| 4 | ToolCallState, |
| 5 | UserChatMessage, |
| 6 | } from "../.."; |
| 7 | import { |
| 8 | normalizeToMessageParts, |
| 9 | renderContextItems, |
| 10 | } from "../../util/messageContent"; |
| 11 | import { |
| 12 | CANCELLED_TOOL_CALL_MESSAGE, |
| 13 | NO_TOOL_CALL_OUTPUT_MESSAGE, |
| 14 | } from "../constants"; |
| 15 | function toolCallStateToSystemToolCall(state: ToolCallState): string { |
| 16 | let parts = ["```tool"]; |
| 17 | parts.push(`TOOL_NAME: ${state.toolCall.function.name}`); |
| 18 | try { |
| 19 | for (const arg in state.parsedArgs) { |
| 20 | parts.push(`BEGIN_ARG: ${arg}`); |
| 21 | parts.push(JSON.stringify(state.parsedArgs[arg])); |
| 22 | parts.push(`END_ARG`); |
| 23 | } |
| 24 | } catch (e) { |
| 25 | console.log("Failed to stringify json args", state.parsedArgs); |
| 26 | } |
| 27 | // TODO - include tool call id for parallel. Confuses dumb models |
| 28 | parts.push("```"); |
| 29 | return parts.join("\n"); |
| 30 | } |
| 31 | |
| 32 | function toolCallStateToSystemToolOutput(state: ToolCallState): string { |
| 33 | let output = `Tool output for ${state.toolCall.function.name} tool call:\n\n`; |
| 34 | // TODO - include tool call id for parallel. Confuses dumb models |
| 35 | // let output = `Tool output for tool call ${state.toolCallId} (${state.toolCall.function.name}):\n\n`; |
| 36 | if (state.status === "canceled") { |
| 37 | output += CANCELLED_TOOL_CALL_MESSAGE; |
| 38 | } else if (state.output?.length) { |
| 39 | output += renderContextItems(state.output); |
| 40 | } else { |
| 41 | output += NO_TOOL_CALL_OUTPUT_MESSAGE; |
| 42 | } |
| 43 | return output; |
| 44 | } |
| 45 | |
| 46 | export function convertToolCallStatesToSystemCallsAndOutput( |
| 47 | originalAssistantMessage: AssistantChatMessage, |
| 48 | toolCallStates: ToolCallState[], |
| 49 | ): { |
| 50 | assistantMessage: AssistantChatMessage; |
| 51 | userMessage: UserChatMessage; |
| 52 | } { |
| 53 | const assistantParts = normalizeToMessageParts(originalAssistantMessage); |
| 54 | if (originalAssistantMessage.toolCalls) { |
| 55 | assistantParts.push( |
| 56 | ...toolCallStates.map((state) => ({ |
| 57 | type: "text" as const, |
| 58 | text: toolCallStateToSystemToolCall(state), |
| 59 | })), |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | const userParts: MessagePart[] = []; |
| 64 | if (toolCallStates) { |
| 65 | for (const state of toolCallStates) { |
| 66 | userParts.push({ |
| 67 | type: "text", |
| 68 | text: toolCallStateToSystemToolOutput(state), |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | if (userParts.length === 0) { |
| 73 | userParts.push({ |
| 74 | type: "text", |
| 75 | text: "No tool output for tool calls", |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | const nonEmptyAssistantParts = assistantParts.filter( |
| 80 | (p) => p.type !== "text" || !!p.text, |
| 81 | ); |
| 82 | const nonEmptyUserParts = userParts.filter( |
| 83 | (p) => p.type !== "text" || !!p.text, |
| 84 | ); |
| 85 | |
| 86 | // new message with tool calls moved to content |
| 87 | const assistantMessage: AssistantChatMessage = { |
| 88 | ...originalAssistantMessage, |
| 89 | content: nonEmptyAssistantParts, |
| 90 | toolCalls: undefined, // remove tool calls from the assistant message |
| 91 | }; |
| 92 | |
| 93 | const userMessage: UserChatMessage = { |
| 94 | role: "user", |
| 95 | content: nonEmptyUserParts, |
| 96 | }; |
| 97 | |
| 98 | return { |
| 99 | assistantMessage, |
| 100 | userMessage, |
| 101 | }; |
| 102 | } |