fix(tool-pair-validator): emit schema-compatible synthetic tool results

This commit is contained in:
rshks
2026-05-08 14:09:42 +05:30
committed by YeonGyu-Kim
parent 24f7e560ca
commit b504fb1deb
7 changed files with 218 additions and 20 deletions
+17 -5
View File
@@ -13,8 +13,10 @@ type ToolUsePart = {
type ToolResultPart = {
type: "tool_result"
tool_use_id: string
content: string
toolUseId: string
tool_use_id?: string
isError?: boolean
content: Array<{ type: "text"; text: string }>
[key: string]: unknown
}
@@ -52,9 +54,17 @@ function getToolUseID(part: TransformPart): string | null {
}
function getToolResultID(part: TransformPart): string | null {
const candidate = part as { type?: unknown; tool_use_id?: unknown }
const candidate = part as { type?: unknown; toolUseId?: unknown; tool_use_id?: unknown }
if (candidate.type === "tool_result" && typeof candidate.tool_use_id === "string" && candidate.tool_use_id.length > 0) {
if (candidate.type !== "tool_result") {
return null
}
if (typeof candidate.toolUseId === "string" && candidate.toolUseId.length > 0) {
return candidate.toolUseId
}
if (typeof candidate.tool_use_id === "string" && candidate.tool_use_id.length > 0) {
return candidate.tool_use_id
}
@@ -94,8 +104,10 @@ function extractToolResultIDs(parts: TransformPart[]): Set<string> {
function createToolResultPart(toolUseID: string): ToolResultPart {
return {
type: "tool_result",
toolUseId: toolUseID,
tool_use_id: toolUseID,
content: TOOL_RESULT_PLACEHOLDER,
isError: true,
content: [{ type: "text", text: TOOL_RESULT_PLACEHOLDER }],
}
}