2026-01-13 10:02:21 +02:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
|
|
|
|
|
2026-02-17 15:46:21 +09:00
|
|
|
export const JSON_ERROR_TOOL_EXCLUDE_LIST = [
|
|
|
|
|
"bash",
|
|
|
|
|
"read",
|
|
|
|
|
"glob",
|
|
|
|
|
"grep",
|
|
|
|
|
"webfetch",
|
|
|
|
|
"look_at",
|
|
|
|
|
"grep_app_searchgithub",
|
|
|
|
|
"websearch_web_search_exa",
|
|
|
|
|
] as const
|
|
|
|
|
|
2026-01-13 10:02:21 +02:00
|
|
|
export const JSON_ERROR_PATTERNS = [
|
2026-02-17 15:46:21 +09:00
|
|
|
/json parse error/i,
|
|
|
|
|
/failed to parse json/i,
|
|
|
|
|
/invalid json/i,
|
|
|
|
|
/malformed json/i,
|
|
|
|
|
/unexpected end of json input/i,
|
|
|
|
|
/syntaxerror:\s*unexpected token.*json/i,
|
|
|
|
|
/json[^\n]*expected '\}'/i,
|
|
|
|
|
/json[^\n]*unexpected eof/i,
|
2026-01-13 10:02:21 +02:00
|
|
|
] as const
|
|
|
|
|
|
2026-02-17 15:46:21 +09:00
|
|
|
const JSON_ERROR_REMINDER_MARKER = "[JSON PARSE ERROR - IMMEDIATE ACTION REQUIRED]"
|
|
|
|
|
const JSON_ERROR_EXCLUDED_TOOLS = new Set<string>(JSON_ERROR_TOOL_EXCLUDE_LIST)
|
|
|
|
|
|
2026-01-13 10:02:21 +02:00
|
|
|
export const JSON_ERROR_REMINDER = `
|
|
|
|
|
[JSON PARSE ERROR - IMMEDIATE ACTION REQUIRED]
|
|
|
|
|
|
|
|
|
|
You sent invalid JSON arguments. The system could not parse your tool call.
|
|
|
|
|
STOP and do this NOW:
|
|
|
|
|
|
|
|
|
|
1. LOOK at the error message above to see what was expected vs what you sent.
|
|
|
|
|
2. CORRECT your JSON syntax (missing braces, unescaped quotes, trailing commas, etc).
|
|
|
|
|
3. RETRY the tool call with valid JSON.
|
|
|
|
|
|
|
|
|
|
DO NOT repeat the exact same invalid call.
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
export function createJsonErrorRecoveryHook(_ctx: PluginInput) {
|
|
|
|
|
return {
|
|
|
|
|
"tool.execute.after": async (
|
2026-02-17 15:46:21 +09:00
|
|
|
input: { tool: string; sessionID: string; callID: string },
|
2026-01-13 10:02:21 +02:00
|
|
|
output: { title: string; output: string; metadata: unknown }
|
|
|
|
|
) => {
|
2026-02-17 15:46:21 +09:00
|
|
|
if (JSON_ERROR_EXCLUDED_TOOLS.has(input.tool.toLowerCase())) return
|
2026-01-13 10:02:21 +02:00
|
|
|
if (typeof output.output !== "string") return
|
2026-02-17 15:46:21 +09:00
|
|
|
if (output.output.includes(JSON_ERROR_REMINDER_MARKER)) return
|
2026-01-13 10:02:21 +02:00
|
|
|
|
2026-02-17 15:46:21 +09:00
|
|
|
const hasJsonError = JSON_ERROR_PATTERNS.some((pattern) => pattern.test(output.output))
|
2026-01-13 10:02:21 +02:00
|
|
|
|
|
|
|
|
if (hasJsonError) {
|
|
|
|
|
output.output += `\n${JSON_ERROR_REMINDER}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|