|
|
|
@ -441,48 +441,59 @@ export default defineEventHandler(async (event) => { |
|
|
|
|
|
|
|
const parts: Array<Record<string, unknown>> = []; |
|
|
|
for (const step of steps) { |
|
|
|
for (const r of step.reasoning) { |
|
|
|
if (r.type === "reasoning" && r.text) { |
|
|
|
parts.push({ id: `p_${parts.length}`, type: "reasoning", text: r.text }); |
|
|
|
} |
|
|
|
} |
|
|
|
const toolResultMap = new Map(step.toolResults.map((tr) => [tr.toolCallId, tr.output])); |
|
|
|
const approvalRequestMap = new Map<string, string>(); |
|
|
|
for (const cp of step.content as Array<Record<string, unknown>>) { |
|
|
|
if (cp.type === "tool-approval-request" && cp.approvalId && cp.toolCall) { |
|
|
|
const tc = cp.toolCall as Record<string, unknown>; |
|
|
|
if (tc.toolCallId) { |
|
|
|
approvalRequestMap.set(tc.toolCallId as string, cp.approvalId as string); |
|
|
|
} |
|
|
|
const contentArr = step.content as Array<Record<string, unknown>>; |
|
|
|
const approvalToolCallIds = new Set<string>(); |
|
|
|
for (const cp of contentArr) { |
|
|
|
if (cp.type === "tool-approval-request" && (cp as any).approvalId && (cp as any).toolCall) { |
|
|
|
approvalToolCallIds.add((cp as any).toolCall.toolCallId); |
|
|
|
} |
|
|
|
} |
|
|
|
for (const tc of step.toolCalls) { |
|
|
|
const approvalId = approvalRequestMap.get(tc.toolCallId); |
|
|
|
if (approvalId) { |
|
|
|
parts.push({ |
|
|
|
id: `p_${parts.length}`, |
|
|
|
type: "tool-call", |
|
|
|
toolCallId: tc.toolCallId, |
|
|
|
toolName: tc.toolName, |
|
|
|
args: tc.input, |
|
|
|
state: "approval-requested", |
|
|
|
approvalId, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
for (const cp of contentArr) { |
|
|
|
if (cp.type === "reasoning" && (cp as any).text) { |
|
|
|
parts.push({ id: `p_${parts.length}`, type: "reasoning", text: (cp as any).text }); |
|
|
|
} else if (cp.type === "text" && (cp as any).text) { |
|
|
|
parts.push({ id: `p_${parts.length}`, type: "text", text: (cp as any).text }); |
|
|
|
} else if (cp.type === "tool-approval-request" && (cp as any).approvalId && (cp as any).toolCall) { |
|
|
|
const tc = (cp as any).toolCall; |
|
|
|
const toolResult = toolResultMap.get(tc.toolCallId); |
|
|
|
if (toolResult === undefined) { |
|
|
|
parts.push({ |
|
|
|
id: `p_${parts.length}`, |
|
|
|
type: "tool-call", |
|
|
|
toolCallId: tc.toolCallId, |
|
|
|
toolName: tc.toolName, |
|
|
|
args: tc.input, |
|
|
|
state: "approval-requested", |
|
|
|
approvalId: (cp as any).approvalId, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
parts.push({ |
|
|
|
id: `p_${parts.length}`, |
|
|
|
type: "tool-call", |
|
|
|
toolCallId: tc.toolCallId, |
|
|
|
toolName: tc.toolName, |
|
|
|
args: tc.input, |
|
|
|
result: toolResult, |
|
|
|
state: "result", |
|
|
|
approvalId: (cp as any).approvalId, |
|
|
|
}); |
|
|
|
} |
|
|
|
} else if (cp.type === "tool-call") { |
|
|
|
const tc = cp as any; |
|
|
|
if (approvalToolCallIds.has(tc.toolCallId)) continue; |
|
|
|
const toolResult = toolResultMap.get(tc.toolCallId); |
|
|
|
parts.push({ |
|
|
|
id: `p_${parts.length}`, |
|
|
|
type: "tool-call", |
|
|
|
toolCallId: tc.toolCallId, |
|
|
|
toolName: tc.toolName, |
|
|
|
args: tc.input, |
|
|
|
result: toolResultMap.get(tc.toolCallId), |
|
|
|
result: toolResult, |
|
|
|
state: "result", |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
if (step.text) { |
|
|
|
parts.push({ id: `p_${parts.length}`, type: "text", text: step.text }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (isApprovalContinue) { |
|
|
|
|