From 7b08c7332aaf4141310ca9ed385e2686aa5f458a Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Thu, 13 Nov 2025 22:27:11 +0900 Subject: [PATCH] feat: add automatic fallback from edit_diagram to display_diagram with 3-retry policy - Updated system prompt to allow up to 3 retry attempts with adjusted search patterns - Simplified error response to provide current diagram XML and reference retry policy - AI model self-manages retries based on system instructions --- app/api/chat/route.ts | 5 ++++- components/chat-panel.tsx | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index d074d80..5183e9b 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -72,7 +72,10 @@ When using edit_diagram tool: - Example GOOD edit: {"search": " ", "replace": " "} - Example BAD edit: Including 10+ unchanged lines just to change one attribute - For multiple changes, use separate edits: [{"search": "line1", "replace": "new1"}, {"search": "line2", "replace": "new2"}] -- CRITICAL: If edit_diagram fails because the search pattern cannot be found, fall back to using display_diagram to regenerate the entire diagram with your changes. Do NOT keep trying edit_diagram with different search patterns. +- RETRY POLICY: If edit_diagram fails because the search pattern cannot be found: + * You may retry edit_diagram up to 3 times with adjusted search patterns + * After 3 failed attempts, you MUST fall back to using display_diagram to regenerate the entire diagram + * The error message will indicate how many retries remain `; const lastMessage = messages[messages.length - 1]; diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx index aa8693f..78f60da 100644 --- a/components/chat-panel.tsx +++ b/components/chat-panel.tsx @@ -98,10 +98,18 @@ export default function ChatPanel() { const errorMessage = error instanceof Error ? error.message : String(error); + // Provide detailed error with current diagram XML addToolResult({ tool: "edit_diagram", toolCallId: toolCall.toolCallId, - output: `Failed to edit diagram: ${errorMessage}`, + output: `Edit failed: ${errorMessage} + +Current diagram XML: +\`\`\`xml +${currentXml} +\`\`\` + +Please retry with an adjusted search pattern or use display_diagram if retries are exhausted.`, }); } }