From 7b08c7332aaf4141310ca9ed385e2686aa5f458a Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Thu, 13 Nov 2025 22:27:11 +0900 Subject: [PATCH 1/4] 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.`, }); } } From 8b68e5740a7f35ac4b2d124fa8e776d8fb1cd1c2 Mon Sep 17 00:00:00 2001 From: DayuanJiang Date: Thu, 13 Nov 2025 14:20:08 +0000 Subject: [PATCH 2/4] Auto-commit before push --- .claude/settings.local.json | 3 ++- ec2-push-script.sh | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 ec2-push-script.sh diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 31dc750..2ecf826 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -7,7 +7,8 @@ "Bash(npx tsc:*)", "WebFetch(domain:sdk.vercel.ai)", "Bash(npm run build:*)", - "Bash(npm outdated:*)" + "Bash(npm outdated:*)", + "Bash(./push-via-ec2.sh)" ], "deny": [], "ask": [] diff --git a/ec2-push-script.sh b/ec2-push-script.sh new file mode 100644 index 0000000..09fa027 --- /dev/null +++ b/ec2-push-script.sh @@ -0,0 +1,44 @@ +#!/bin/bash +set -e + +BRANCH_NAME="$1" + +cd /tmp/next-ai-draw-io + +# Configure git +git config user.name "DayuanJiang" +git config user.email "jdy.toh@gmail.com" + +echo "=== Current git status ===" +git status + +echo "=== Checking out branch: ${BRANCH_NAME} ===" +git checkout ${BRANCH_NAME} || git checkout -b ${BRANCH_NAME} + +echo "=== Setting remote URL to use SSH ===" +git remote set-url origin git@github.com:DayuanJiang/next-ai-draw-io.git + +echo "=== Remote URL configured ===" +git remote -v + +# Commit any local changes if needed +git add -A || true +git diff --cached --quiet || git commit -m "Auto-commit before push" || true + +echo "=== Adding GitHub to known hosts ===" +ssh-keyscan -H github.com >> ~/.ssh/known_hosts 2>/dev/null || true + +echo "=== Testing SSH connection to GitHub ===" +ssh -T git@github.com 2>&1 || true + +echo "=== Pushing branch ${BRANCH_NAME} to GitHub ===" +git push -u origin ${BRANCH_NAME} + +echo "=== Push successful! ===" + +# Clean up +echo "=== Cleaning up ===" +cd /tmp +rm -rf next-ai-draw-io + +echo "Done!" From 8c4913d85e2d22c01f871fa9c08cc06af53a6441 Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Thu, 13 Nov 2025 23:24:50 +0900 Subject: [PATCH 3/4] chore: remove ec2-push-script.sh and add to .gitignore --- .gitignore | 1 + ec2-push-script.sh | 44 -------------------------------------------- 2 files changed, 1 insertion(+), 44 deletions(-) delete mode 100644 ec2-push-script.sh diff --git a/.gitignore b/.gitignore index af5de8c..efc3635 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts push-via-ec2.sh +ec2-push-script.sh .claude/settings.local.json diff --git a/ec2-push-script.sh b/ec2-push-script.sh deleted file mode 100644 index 09fa027..0000000 --- a/ec2-push-script.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -set -e - -BRANCH_NAME="$1" - -cd /tmp/next-ai-draw-io - -# Configure git -git config user.name "DayuanJiang" -git config user.email "jdy.toh@gmail.com" - -echo "=== Current git status ===" -git status - -echo "=== Checking out branch: ${BRANCH_NAME} ===" -git checkout ${BRANCH_NAME} || git checkout -b ${BRANCH_NAME} - -echo "=== Setting remote URL to use SSH ===" -git remote set-url origin git@github.com:DayuanJiang/next-ai-draw-io.git - -echo "=== Remote URL configured ===" -git remote -v - -# Commit any local changes if needed -git add -A || true -git diff --cached --quiet || git commit -m "Auto-commit before push" || true - -echo "=== Adding GitHub to known hosts ===" -ssh-keyscan -H github.com >> ~/.ssh/known_hosts 2>/dev/null || true - -echo "=== Testing SSH connection to GitHub ===" -ssh -T git@github.com 2>&1 || true - -echo "=== Pushing branch ${BRANCH_NAME} to GitHub ===" -git push -u origin ${BRANCH_NAME} - -echo "=== Push successful! ===" - -# Clean up -echo "=== Cleaning up ===" -cd /tmp -rm -rf next-ai-draw-io - -echo "Done!" From cb458af3717ff04c28b073e94d4669998658b2ec Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Thu, 13 Nov 2025 23:29:57 +0900 Subject: [PATCH 4/4] chore: remove .claude/settings.local.json from git tracking --- .claude/settings.local.json | 16 ---------------- .gitignore | 1 - 2 files changed, 17 deletions(-) delete mode 100644 .claude/settings.local.json diff --git a/.claude/settings.local.json b/.claude/settings.local.json deleted file mode 100644 index 2ecf826..0000000 --- a/.claude/settings.local.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "permissions": { - "allow": [ - "Bash(npm update:*)", - "WebFetch(domain:ai-sdk.dev)", - "mcp__ide__getDiagnostics", - "Bash(npx tsc:*)", - "WebFetch(domain:sdk.vercel.ai)", - "Bash(npm run build:*)", - "Bash(npm outdated:*)", - "Bash(./push-via-ec2.sh)" - ], - "deny": [], - "ask": [] - } -} diff --git a/.gitignore b/.gitignore index efc3635..af5de8c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,5 +40,4 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts push-via-ec2.sh -ec2-push-script.sh .claude/settings.local.json