refactor(ai): extract synthetic tool result helper
This commit is contained in:
@@ -100,28 +100,31 @@ export function transformMessages<TApi extends Api>(
|
|||||||
const result: Message[] = [];
|
const result: Message[] = [];
|
||||||
let pendingToolCalls: ToolCall[] = [];
|
let pendingToolCalls: ToolCall[] = [];
|
||||||
let existingToolResultIds = new Set<string>();
|
let existingToolResultIds = new Set<string>();
|
||||||
|
const insertSyntheticToolResults = () => {
|
||||||
|
if (pendingToolCalls.length > 0) {
|
||||||
|
for (const tc of pendingToolCalls) {
|
||||||
|
if (!existingToolResultIds.has(tc.id)) {
|
||||||
|
result.push({
|
||||||
|
role: "toolResult",
|
||||||
|
toolCallId: tc.id,
|
||||||
|
toolName: tc.name,
|
||||||
|
content: [{ type: "text", text: "No result provided" }],
|
||||||
|
isError: true,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} as ToolResultMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pendingToolCalls = [];
|
||||||
|
existingToolResultIds = new Set();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (let i = 0; i < transformed.length; i++) {
|
for (let i = 0; i < transformed.length; i++) {
|
||||||
const msg = transformed[i];
|
const msg = transformed[i];
|
||||||
|
|
||||||
if (msg.role === "assistant") {
|
if (msg.role === "assistant") {
|
||||||
// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now
|
// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now
|
||||||
if (pendingToolCalls.length > 0) {
|
insertSyntheticToolResults();
|
||||||
for (const tc of pendingToolCalls) {
|
|
||||||
if (!existingToolResultIds.has(tc.id)) {
|
|
||||||
result.push({
|
|
||||||
role: "toolResult",
|
|
||||||
toolCallId: tc.id,
|
|
||||||
toolName: tc.name,
|
|
||||||
content: [{ type: "text", text: "No result provided" }],
|
|
||||||
isError: true,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
} as ToolResultMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pendingToolCalls = [];
|
|
||||||
existingToolResultIds = new Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip errored/aborted assistant messages entirely.
|
// Skip errored/aborted assistant messages entirely.
|
||||||
// These are incomplete turns that shouldn't be replayed:
|
// These are incomplete turns that shouldn't be replayed:
|
||||||
@@ -146,22 +149,7 @@ export function transformMessages<TApi extends Api>(
|
|||||||
result.push(msg);
|
result.push(msg);
|
||||||
} else if (msg.role === "user") {
|
} else if (msg.role === "user") {
|
||||||
// User message interrupts tool flow - insert synthetic results for orphaned calls
|
// User message interrupts tool flow - insert synthetic results for orphaned calls
|
||||||
if (pendingToolCalls.length > 0) {
|
insertSyntheticToolResults();
|
||||||
for (const tc of pendingToolCalls) {
|
|
||||||
if (!existingToolResultIds.has(tc.id)) {
|
|
||||||
result.push({
|
|
||||||
role: "toolResult",
|
|
||||||
toolCallId: tc.id,
|
|
||||||
toolName: tc.name,
|
|
||||||
content: [{ type: "text", text: "No result provided" }],
|
|
||||||
isError: true,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
} as ToolResultMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pendingToolCalls = [];
|
|
||||||
existingToolResultIds = new Set();
|
|
||||||
}
|
|
||||||
result.push(msg);
|
result.push(msg);
|
||||||
} else {
|
} else {
|
||||||
result.push(msg);
|
result.push(msg);
|
||||||
|
|||||||
Reference in New Issue
Block a user