fix(tool-pair-validator): emit schema-compatible synthetic tool results
This commit is contained in:
@@ -82,8 +82,10 @@ describe("recoverToolResultMissing", () => {
|
||||
body: {
|
||||
parts: [{
|
||||
type: "tool_result",
|
||||
toolUseId: "call_recovered",
|
||||
tool_use_id: "call_recovered",
|
||||
content: "Operation cancelled by user (ESC pressed)",
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Operation cancelled by user (ESC pressed)" }],
|
||||
}],
|
||||
},
|
||||
})
|
||||
@@ -123,8 +125,10 @@ describe("recoverToolResultMissing", () => {
|
||||
body: {
|
||||
parts: [{
|
||||
type: "tool_result",
|
||||
toolUseId: "toolu_recovered",
|
||||
tool_use_id: "toolu_recovered",
|
||||
content: "Operation cancelled by user (ESC pressed)",
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Operation cancelled by user (ESC pressed)" }],
|
||||
}],
|
||||
},
|
||||
})
|
||||
|
||||
@@ -6,6 +6,14 @@ import { normalizeSDKResponse } from "../../shared"
|
||||
import { promptAsyncAfterSessionIdle } from "../shared/prompt-async-gate"
|
||||
|
||||
type Client = ReturnType<typeof createOpencodeClient>
|
||||
type ToolResultContent = { type: "text"; text: string }
|
||||
type ToolResultPart = {
|
||||
type: "tool_result"
|
||||
toolUseId: string
|
||||
tool_use_id?: string
|
||||
isError?: boolean
|
||||
content: ToolResultContent[]
|
||||
}
|
||||
type ClientWithPromptAsync = {
|
||||
session: {
|
||||
promptAsync: (opts: { path: { id: string }; body: Record<string, unknown> }) => Promise<unknown>
|
||||
@@ -96,8 +104,10 @@ export async function recoverToolResultMissing(
|
||||
|
||||
const toolResultParts = toolUseIds.map((id) => ({
|
||||
type: "tool_result" as const,
|
||||
toolUseId: id,
|
||||
tool_use_id: id,
|
||||
content: "Operation cancelled by user (ESC pressed)",
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: "Operation cancelled by user (ESC pressed)" }],
|
||||
}))
|
||||
|
||||
const launchAgent = resumeConfig?.agent
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"
|
||||
|
||||
import type { MessageData } from "./types"
|
||||
|
||||
let sqliteBackend = false
|
||||
let storedParts: Array<{ type: string; id?: string; callID?: string; name?: string; tool?: string; [key: string]: unknown }> = []
|
||||
|
||||
mock.module("../../shared/opencode-storage-detection", () => ({
|
||||
isSqliteBackend: () => sqliteBackend,
|
||||
}))
|
||||
|
||||
mock.module("./storage", () => ({
|
||||
readParts: () => storedParts,
|
||||
}))
|
||||
|
||||
const { recoverUnavailableTool } = await import("./recover-unavailable-tool")
|
||||
|
||||
const failedAssistantMsg: MessageData = {
|
||||
info: { id: "msg_failed", role: "assistant", error: 'No such tool: bash' },
|
||||
parts: [],
|
||||
}
|
||||
|
||||
function createMockClient(messages: MessageData[] = []) {
|
||||
const promptAsync = mock(() => Promise.resolve({}))
|
||||
|
||||
return {
|
||||
client: {
|
||||
session: {
|
||||
messages: mock(() => Promise.resolve({ data: messages })),
|
||||
promptAsync,
|
||||
},
|
||||
} as never,
|
||||
promptAsync,
|
||||
}
|
||||
}
|
||||
|
||||
describe("recoverUnavailableTool", () => {
|
||||
beforeEach(() => {
|
||||
sqliteBackend = false
|
||||
storedParts = []
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
it("sends a schema-compatible recovered tool result for sqlite fallback", async () => {
|
||||
//#given
|
||||
sqliteBackend = true
|
||||
const { client, promptAsync } = createMockClient([
|
||||
{
|
||||
info: { id: "msg_failed", role: "assistant" },
|
||||
parts: [{ type: "tool", id: "prt_valid_call", callID: "call_recovered", name: "bash", input: {} }],
|
||||
},
|
||||
])
|
||||
|
||||
//#when
|
||||
const result = await recoverUnavailableTool(client, "ses_1", failedAssistantMsg)
|
||||
|
||||
//#then
|
||||
expect(result).toBe(true)
|
||||
expect(promptAsync).toHaveBeenCalledWith({
|
||||
path: { id: "ses_1" },
|
||||
body: {
|
||||
parts: [{
|
||||
type: "tool_result",
|
||||
toolUseId: "call_recovered",
|
||||
tool_use_id: "call_recovered",
|
||||
isError: true,
|
||||
content: [{ type: "text", text: '{"status":"error","error":"Tool not available. Please continue without this tool."}' }],
|
||||
}],
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
it("sends a schema-compatible recovered tool result for stored parts fallback", async () => {
|
||||
//#given
|
||||
storedParts = [{
|
||||
type: "tool",
|
||||
id: "prt_stored_valid_call",
|
||||
callID: "toolu_recovered",
|
||||
tool: "bash",
|
||||
state: { input: {} },
|
||||
}]
|
||||
const { client, promptAsync } = createMockClient()
|
||||
|
||||
//#when
|
||||
const result = await recoverUnavailableTool(client, "ses_2", failedAssistantMsg)
|
||||
|
||||
//#then
|
||||
expect(result).toBe(true)
|
||||
expect(promptAsync).toHaveBeenCalledWith({
|
||||
path: { id: "ses_2" },
|
||||
body: {
|
||||
parts: [{
|
||||
type: "tool_result",
|
||||
toolUseId: "toolu_recovered",
|
||||
tool_use_id: "toolu_recovered",
|
||||
isError: true,
|
||||
content: [{ type: "text", text: '{"status":"error","error":"Tool not available. Please continue without this tool."}' }],
|
||||
}],
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -10,8 +10,10 @@ type Client = ReturnType<typeof createOpencodeClient>
|
||||
|
||||
interface ToolResultPart {
|
||||
type: "tool_result"
|
||||
tool_use_id: string
|
||||
content: string
|
||||
toolUseId: string
|
||||
tool_use_id?: string
|
||||
isError?: boolean
|
||||
content: Array<{ type: "text"; text: string }>
|
||||
}
|
||||
|
||||
interface PromptWithToolResultInput {
|
||||
@@ -91,8 +93,10 @@ export async function recoverUnavailableTool(
|
||||
|
||||
const toolResultParts = targetToolUses.map((part) => ({
|
||||
type: "tool_result" as const,
|
||||
toolUseId: part.id,
|
||||
tool_use_id: part.id,
|
||||
content: '{"status":"error","error":"Tool not available. Please continue without this tool."}',
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: '{"status":"error","error":"Tool not available. Please continue without this tool."}' }],
|
||||
}))
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user