disallow empty text parts
This commit is contained in:
parent
3bd270a41e
commit
7a01cebd1d
1 changed files with 102 additions and 0 deletions
102
core/tools/systemMessageTools/textifySystemTools.ts
Normal file
102
core/tools/systemMessageTools/textifySystemTools.ts
Normal file
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue