fix: use cached chartXML for edit_diagram to avoid Vercel timeout

DrawIO iframe export was unreliable on Vercel due to network latency,
causing edit_diagram tool to hang. Now uses chartXML from context directly,
falling back to export only when no cached XML exists.
This commit is contained in:
dayuan.jiang
2025-12-05 00:43:21 +09:00
parent 46cbc3354c
commit 7b8bd8c621

View File

@@ -99,8 +99,18 @@ export default function ChatPanel({
let currentXml = ""; let currentXml = "";
try { try {
// Fetch without saving to history - edit_diagram shouldn't create history entry console.log("[edit_diagram] Starting...");
currentXml = await onFetchChart(false); // Use chartXML from context directly - more reliable than export
// especially on Vercel where DrawIO iframe may have latency issues
if (chartXML) {
currentXml = chartXML;
console.log("[edit_diagram] Using cached chartXML, length:", currentXml.length);
} else {
// Fallback to export only if no cached XML
console.log("[edit_diagram] No cached XML, fetching from DrawIO...");
currentXml = await onFetchChart(false);
console.log("[edit_diagram] Got XML from export, length:", currentXml.length);
}
const { replaceXMLParts } = await import("@/lib/utils"); const { replaceXMLParts } = await import("@/lib/utils");
const editedXml = replaceXMLParts(currentXml, edits); const editedXml = replaceXMLParts(currentXml, edits);
@@ -112,8 +122,9 @@ export default function ChatPanel({
toolCallId: toolCall.toolCallId, toolCallId: toolCall.toolCallId,
output: `Successfully applied ${edits.length} edit(s) to the diagram.`, output: `Successfully applied ${edits.length} edit(s) to the diagram.`,
}); });
console.log("[edit_diagram] Success");
} catch (error) { } catch (error) {
console.error("Edit diagram failed:", error); console.error("[edit_diagram] Failed:", error);
const errorMessage = const errorMessage =
error instanceof Error error instanceof Error
@@ -127,7 +138,7 @@ export default function ChatPanel({
Current diagram XML: Current diagram XML:
\`\`\`xml \`\`\`xml
${currentXml} ${currentXml || "No XML available"}
\`\`\` \`\`\`
Please retry with an adjusted search pattern or use display_diagram if retries are exhausted.`, Please retry with an adjusted search pattern or use display_diagram if retries are exhausted.`,