mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
Compare commits
13 Commits
v0.4.1
...
feature/mc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecf716125e | ||
|
|
0a2e31b18c | ||
|
|
78a1f978fc | ||
|
|
5ab751c986 | ||
|
|
dad1480d8c | ||
|
|
0c95e829f0 | ||
|
|
f6eeeb0d5b | ||
|
|
c0952d6170 | ||
|
|
79aa8734f1 | ||
|
|
cd76fa615e | ||
|
|
c527ce1520 | ||
|
|
44840d27b3 | ||
|
|
f175276872 |
21
.github/workflows/docker-build.yml
vendored
21
.github/workflows/docker-build.yml
vendored
@@ -64,3 +64,24 @@ jobs:
|
|||||||
cache-to: type=gha,mode=max
|
cache-to: type=gha,mode=max
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
|
|
||||||
|
# Push to AWS ECR for App Runner auto-deploy
|
||||||
|
- name: Configure AWS credentials
|
||||||
|
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
|
||||||
|
uses: aws-actions/configure-aws-credentials@v4
|
||||||
|
with:
|
||||||
|
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
||||||
|
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
||||||
|
aws-region: ap-northeast-1
|
||||||
|
|
||||||
|
- name: Login to Amazon ECR
|
||||||
|
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
|
||||||
|
id: login-ecr
|
||||||
|
uses: aws-actions/amazon-ecr-login@v2
|
||||||
|
|
||||||
|
- name: Push to ECR (triggers App Runner auto-deploy)
|
||||||
|
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main'
|
||||||
|
run: |
|
||||||
|
docker pull ghcr.io/${{ github.repository }}:latest
|
||||||
|
docker tag ghcr.io/${{ github.repository }}:latest ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-1.amazonaws.com/next-ai-draw-io:latest
|
||||||
|
docker push ${{ secrets.AWS_ACCOUNT_ID }}.dkr.ecr.ap-northeast-1.amazonaws.com/next-ai-draw-io:latest
|
||||||
|
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
/node_modules
|
/node_modules
|
||||||
|
packages/*/node_modules
|
||||||
|
packages/*/dist
|
||||||
/.pnp
|
/.pnp
|
||||||
.pnp.*
|
.pnp.*
|
||||||
.yarn/*
|
.yarn/*
|
||||||
@@ -46,3 +48,5 @@ push-via-ec2.sh
|
|||||||
.dev.vars
|
.dev.vars
|
||||||
.open-next/
|
.open-next/
|
||||||
.wrangler/
|
.wrangler/
|
||||||
|
.env*.local
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,6 @@ EXPOSE 3000
|
|||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
ENV HOSTNAME="0.0.0.0"
|
ENV HOSTNAME="0.0.0.0"
|
||||||
|
|
||||||
# Start the application
|
# Start the application (HOSTNAME override needed for AWS App Runner)
|
||||||
CMD ["node", "server.js"]
|
CMD ["sh", "-c", "HOSTNAME=0.0.0.0 exec node server.js"]
|
||||||
|
|
||||||
|
|||||||
31
README.md
31
README.md
@@ -30,6 +30,7 @@ https://github.com/user-attachments/assets/9d60a3e8-4a1c-4b5e-acbb-26af2d3eabd1
|
|||||||
- [Table of Contents](#table-of-contents)
|
- [Table of Contents](#table-of-contents)
|
||||||
- [Examples](#examples)
|
- [Examples](#examples)
|
||||||
- [Features](#features)
|
- [Features](#features)
|
||||||
|
- [MCP Server (Preview)](#mcp-server-preview)
|
||||||
- [Getting Started](#getting-started)
|
- [Getting Started](#getting-started)
|
||||||
- [Try it Online](#try-it-online)
|
- [Try it Online](#try-it-online)
|
||||||
- [Run with Docker (Recommended)](#run-with-docker-recommended)
|
- [Run with Docker (Recommended)](#run-with-docker-recommended)
|
||||||
@@ -92,6 +93,36 @@ Here are some example prompts and their generated diagrams:
|
|||||||
- **Cloud Architecture Diagram Support**: Specialized support for generating cloud architecture diagrams (AWS, GCP, Azure)
|
- **Cloud Architecture Diagram Support**: Specialized support for generating cloud architecture diagrams (AWS, GCP, Azure)
|
||||||
- **Animated Connectors**: Create dynamic and animated connectors between diagram elements for better visualization
|
- **Animated Connectors**: Create dynamic and animated connectors between diagram elements for better visualization
|
||||||
|
|
||||||
|
## MCP Server (Preview)
|
||||||
|
|
||||||
|
> **Preview Feature**: This feature is experimental and may change.
|
||||||
|
|
||||||
|
Use Next AI Draw.io with AI agents like Claude Desktop, Cursor, and VS Code via MCP (Model Context Protocol).
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Claude Code CLI
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add drawio -- npx @next-ai-drawio/mcp-server@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
Then ask Claude to create diagrams:
|
||||||
|
> "Create a flowchart showing user authentication with login, MFA, and session management"
|
||||||
|
|
||||||
|
The diagram appears in your browser in real-time!
|
||||||
|
|
||||||
|
See the [MCP Server README](./packages/mcp-server/README.md) for VS Code, Cursor, and other client configurations.
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
### Try it Online
|
### Try it Online
|
||||||
|
|||||||
@@ -70,29 +70,41 @@ function isMinimalDiagram(xml: string): boolean {
|
|||||||
|
|
||||||
// Helper function to replace historical tool call XML with placeholders
|
// Helper function to replace historical tool call XML with placeholders
|
||||||
// This reduces token usage and forces LLM to rely on the current diagram XML (source of truth)
|
// This reduces token usage and forces LLM to rely on the current diagram XML (source of truth)
|
||||||
|
// Also fixes invalid/undefined inputs from interrupted streaming
|
||||||
function replaceHistoricalToolInputs(messages: any[]): any[] {
|
function replaceHistoricalToolInputs(messages: any[]): any[] {
|
||||||
return messages.map((msg) => {
|
return messages.map((msg) => {
|
||||||
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
const replacedContent = msg.content.map((part: any) => {
|
const replacedContent = msg.content
|
||||||
if (part.type === "tool-call") {
|
.map((part: any) => {
|
||||||
const toolName = part.toolName
|
if (part.type === "tool-call") {
|
||||||
if (
|
const toolName = part.toolName
|
||||||
toolName === "display_diagram" ||
|
// Fix invalid/undefined inputs from interrupted streaming
|
||||||
toolName === "edit_diagram"
|
if (
|
||||||
) {
|
!part.input ||
|
||||||
return {
|
typeof part.input !== "object" ||
|
||||||
...part,
|
Object.keys(part.input).length === 0
|
||||||
input: {
|
) {
|
||||||
placeholder:
|
// Skip tool calls with invalid inputs entirely
|
||||||
"[XML content replaced - see current diagram XML in system context]",
|
return null
|
||||||
},
|
}
|
||||||
|
if (
|
||||||
|
toolName === "display_diagram" ||
|
||||||
|
toolName === "edit_diagram"
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
...part,
|
||||||
|
input: {
|
||||||
|
placeholder:
|
||||||
|
"[XML content replaced - see current diagram XML in system context]",
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return part
|
||||||
return part
|
})
|
||||||
})
|
.filter(Boolean) // Remove null entries (invalid tool calls)
|
||||||
return { ...msg, content: replacedContent }
|
return { ...msg, content: replacedContent }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -231,6 +243,36 @@ ${userInputText}
|
|||||||
// Convert UIMessages to ModelMessages and add system message
|
// Convert UIMessages to ModelMessages and add system message
|
||||||
const modelMessages = convertToModelMessages(messages)
|
const modelMessages = convertToModelMessages(messages)
|
||||||
|
|
||||||
|
// DEBUG: Log incoming messages structure
|
||||||
|
console.log("[route.ts] Incoming messages count:", messages.length)
|
||||||
|
messages.forEach((msg: any, idx: number) => {
|
||||||
|
console.log(
|
||||||
|
`[route.ts] Message ${idx} role:`,
|
||||||
|
msg.role,
|
||||||
|
"parts count:",
|
||||||
|
msg.parts?.length,
|
||||||
|
)
|
||||||
|
if (msg.parts) {
|
||||||
|
msg.parts.forEach((part: any, partIdx: number) => {
|
||||||
|
if (
|
||||||
|
part.type === "tool-invocation" ||
|
||||||
|
part.type === "tool-result"
|
||||||
|
) {
|
||||||
|
console.log(`[route.ts] Part ${partIdx}:`, {
|
||||||
|
type: part.type,
|
||||||
|
toolName: part.toolName,
|
||||||
|
hasInput: !!part.input,
|
||||||
|
inputType: typeof part.input,
|
||||||
|
inputKeys:
|
||||||
|
part.input && typeof part.input === "object"
|
||||||
|
? Object.keys(part.input)
|
||||||
|
: null,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Replace historical tool call XML with placeholders to reduce tokens
|
// Replace historical tool call XML with placeholders to reduce tokens
|
||||||
// Disabled by default - some models (e.g. minimax) copy placeholders instead of generating XML
|
// Disabled by default - some models (e.g. minimax) copy placeholders instead of generating XML
|
||||||
const enableHistoryReplace =
|
const enableHistoryReplace =
|
||||||
@@ -246,6 +288,63 @@ ${userInputText}
|
|||||||
msg.content && Array.isArray(msg.content) && msg.content.length > 0,
|
msg.content && Array.isArray(msg.content) && msg.content.length > 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Filter out tool-calls with invalid inputs (from failed repair or interrupted streaming)
|
||||||
|
// Bedrock API rejects messages where toolUse.input is not a valid JSON object
|
||||||
|
enhancedMessages = enhancedMessages
|
||||||
|
.map((msg: any) => {
|
||||||
|
if (msg.role !== "assistant" || !Array.isArray(msg.content)) {
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
const filteredContent = msg.content.filter((part: any) => {
|
||||||
|
if (part.type === "tool-call") {
|
||||||
|
// Check if input is a valid object (not null, undefined, or empty)
|
||||||
|
if (
|
||||||
|
!part.input ||
|
||||||
|
typeof part.input !== "object" ||
|
||||||
|
Object.keys(part.input).length === 0
|
||||||
|
) {
|
||||||
|
console.warn(
|
||||||
|
`[route.ts] Filtering out tool-call with invalid input:`,
|
||||||
|
{ toolName: part.toolName, input: part.input },
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return { ...msg, content: filteredContent }
|
||||||
|
})
|
||||||
|
.filter((msg: any) => msg.content && msg.content.length > 0)
|
||||||
|
|
||||||
|
// DEBUG: Log modelMessages structure (what's being sent to AI)
|
||||||
|
console.log("[route.ts] Model messages count:", enhancedMessages.length)
|
||||||
|
enhancedMessages.forEach((msg: any, idx: number) => {
|
||||||
|
console.log(
|
||||||
|
`[route.ts] ModelMsg ${idx} role:`,
|
||||||
|
msg.role,
|
||||||
|
"content count:",
|
||||||
|
msg.content?.length,
|
||||||
|
)
|
||||||
|
if (msg.content) {
|
||||||
|
msg.content.forEach((part: any, partIdx: number) => {
|
||||||
|
if (part.type === "tool-call" || part.type === "tool-result") {
|
||||||
|
console.log(`[route.ts] Content ${partIdx}:`, {
|
||||||
|
type: part.type,
|
||||||
|
toolName: part.toolName,
|
||||||
|
hasInput: !!part.input,
|
||||||
|
inputType: typeof part.input,
|
||||||
|
inputValue:
|
||||||
|
part.input === undefined
|
||||||
|
? "undefined"
|
||||||
|
: part.input === null
|
||||||
|
? "null"
|
||||||
|
: "object",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Update the last message with user input only (XML moved to separate cached system message)
|
// Update the last message with user input only (XML moved to separate cached system message)
|
||||||
if (enhancedMessages.length >= 1) {
|
if (enhancedMessages.length >= 1) {
|
||||||
const lastModelMessage = enhancedMessages[enhancedMessages.length - 1]
|
const lastModelMessage = enhancedMessages[enhancedMessages.length - 1]
|
||||||
@@ -327,14 +426,30 @@ ${userInputText}
|
|||||||
stopWhen: stepCountIs(5),
|
stopWhen: stepCountIs(5),
|
||||||
// Repair truncated tool calls when maxOutputTokens is reached mid-JSON
|
// Repair truncated tool calls when maxOutputTokens is reached mid-JSON
|
||||||
experimental_repairToolCall: async ({ toolCall, error }) => {
|
experimental_repairToolCall: async ({ toolCall, error }) => {
|
||||||
|
// DEBUG: Log what we're trying to repair
|
||||||
|
console.log(`[repairToolCall] Tool: ${toolCall.toolName}`)
|
||||||
|
console.log(
|
||||||
|
`[repairToolCall] Error: ${error.name} - ${error.message}`,
|
||||||
|
)
|
||||||
|
console.log(`[repairToolCall] Input type: ${typeof toolCall.input}`)
|
||||||
|
console.log(`[repairToolCall] Input value:`, toolCall.input)
|
||||||
|
|
||||||
// Only attempt repair for invalid tool input (broken JSON from truncation)
|
// Only attempt repair for invalid tool input (broken JSON from truncation)
|
||||||
if (
|
if (
|
||||||
error instanceof InvalidToolInputError ||
|
error instanceof InvalidToolInputError ||
|
||||||
error.name === "AI_InvalidToolInputError"
|
error.name === "AI_InvalidToolInputError"
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
// Pre-process to fix common LLM JSON errors that jsonrepair can't handle
|
||||||
|
let inputToRepair = toolCall.input
|
||||||
|
if (typeof inputToRepair === "string") {
|
||||||
|
// Fix `:=` instead of `: ` (LLM sometimes generates this)
|
||||||
|
inputToRepair = inputToRepair.replace(/:=/g, ": ")
|
||||||
|
// Fix `= "` instead of `: "`
|
||||||
|
inputToRepair = inputToRepair.replace(/=\s*"/g, ': "')
|
||||||
|
}
|
||||||
// Use jsonrepair to fix truncated JSON
|
// Use jsonrepair to fix truncated JSON
|
||||||
const repairedInput = jsonrepair(toolCall.input)
|
const repairedInput = jsonrepair(inputToRepair)
|
||||||
console.log(
|
console.log(
|
||||||
`[repairToolCall] Repaired truncated JSON for tool: ${toolCall.toolName}`,
|
`[repairToolCall] Repaired truncated JSON for tool: ${toolCall.toolName}`,
|
||||||
)
|
)
|
||||||
@@ -344,6 +459,26 @@ ${userInputText}
|
|||||||
`[repairToolCall] Failed to repair JSON for tool: ${toolCall.toolName}`,
|
`[repairToolCall] Failed to repair JSON for tool: ${toolCall.toolName}`,
|
||||||
repairError,
|
repairError,
|
||||||
)
|
)
|
||||||
|
// Return a placeholder input to avoid API errors in multi-step
|
||||||
|
// The tool will fail gracefully on client side
|
||||||
|
if (toolCall.toolName === "edit_diagram") {
|
||||||
|
return {
|
||||||
|
...toolCall,
|
||||||
|
input: {
|
||||||
|
operations: [],
|
||||||
|
_error: "JSON repair failed - no operations to apply",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (toolCall.toolName === "display_diagram") {
|
||||||
|
return {
|
||||||
|
...toolCall,
|
||||||
|
input: {
|
||||||
|
xml: "",
|
||||||
|
_error: "JSON repair failed - empty diagram",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -408,33 +543,37 @@ Notes:
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
edit_diagram: {
|
edit_diagram: {
|
||||||
description: `Edit specific parts of the current diagram by replacing exact line matches. Use this tool to make targeted fixes without regenerating the entire XML.
|
description: `Edit the current diagram by ID-based operations (update/add/delete cells).
|
||||||
CRITICAL: Copy-paste the EXACT search pattern from the "Current diagram XML" in system context. Do NOT reorder attributes or reformat - the attribute order in draw.io XML varies and you MUST match it exactly.
|
|
||||||
IMPORTANT: Keep edits concise:
|
|
||||||
- COPY the exact mxCell line from the current XML (attribute order matters!)
|
|
||||||
- Only include the lines that are changing, plus 1-2 surrounding lines for context if needed
|
|
||||||
- Break large changes into multiple smaller edits
|
|
||||||
- Each search must contain complete lines (never truncate mid-line)
|
|
||||||
- First match only - be specific enough to target the right element
|
|
||||||
|
|
||||||
⚠️ JSON ESCAPING: Every " inside string values MUST be escaped as \\". Example: x=\\"100\\" y=\\"200\\" - BOTH quotes need backslashes!`,
|
Operations:
|
||||||
|
- update: Replace an existing cell by its id. Provide cell_id and complete new_xml.
|
||||||
|
- add: Add a new cell. Provide cell_id (new unique id) and new_xml.
|
||||||
|
- delete: Remove a cell by its id. Only cell_id is needed.
|
||||||
|
|
||||||
|
For update/add, new_xml must be a complete mxCell element including mxGeometry.
|
||||||
|
|
||||||
|
⚠️ JSON ESCAPING: Every " inside new_xml MUST be escaped as \\". Example: id=\\"5\\" value=\\"Label\\"`,
|
||||||
inputSchema: z.object({
|
inputSchema: z.object({
|
||||||
edits: z
|
operations: z
|
||||||
.array(
|
.array(
|
||||||
z.object({
|
z.object({
|
||||||
search: z
|
type: z
|
||||||
|
.enum(["update", "add", "delete"])
|
||||||
|
.describe("Operation type"),
|
||||||
|
cell_id: z
|
||||||
.string()
|
.string()
|
||||||
.describe(
|
.describe(
|
||||||
"EXACT lines copied from current XML (preserve attribute order!)",
|
"The id of the mxCell. Must match the id attribute in new_xml.",
|
||||||
),
|
),
|
||||||
replace: z
|
new_xml: z
|
||||||
.string()
|
.string()
|
||||||
.describe("Replacement lines"),
|
.optional()
|
||||||
|
.describe(
|
||||||
|
"Complete mxCell XML element (required for update/add)",
|
||||||
|
),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.describe(
|
.describe("Array of operations to apply"),
|
||||||
"Array of search/replace pairs to apply sequentially",
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
append_diagram: {
|
append_diagram: {
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { Cloud, FileText, GitBranch, Palette, Zap } from "lucide-react"
|
import {
|
||||||
|
Cloud,
|
||||||
|
FileText,
|
||||||
|
GitBranch,
|
||||||
|
Palette,
|
||||||
|
Terminal,
|
||||||
|
Zap,
|
||||||
|
} from "lucide-react"
|
||||||
|
|
||||||
interface ExampleCardProps {
|
interface ExampleCardProps {
|
||||||
icon: React.ReactNode
|
icon: React.ReactNode
|
||||||
@@ -108,6 +115,33 @@ export default function ExamplePanel({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="py-6 px-2 animate-fade-in">
|
<div className="py-6 px-2 animate-fade-in">
|
||||||
|
{/* MCP Server Notice */}
|
||||||
|
<a
|
||||||
|
href="https://github.com/DayuanJiang/next-ai-draw-io/tree/main/packages/mcp-server"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="block mb-4 p-3 rounded-xl bg-gradient-to-r from-purple-500/10 to-blue-500/10 border border-purple-500/20 hover:border-purple-500/40 transition-colors group"
|
||||||
|
>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<div className="w-8 h-8 rounded-lg bg-purple-500/20 flex items-center justify-center shrink-0">
|
||||||
|
<Terminal className="w-4 h-4 text-purple-500" />
|
||||||
|
</div>
|
||||||
|
<div className="min-w-0">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<span className="text-sm font-medium text-foreground group-hover:text-purple-500 transition-colors">
|
||||||
|
MCP Server
|
||||||
|
</span>
|
||||||
|
<span className="px-1.5 py-0.5 text-[10px] font-semibold bg-purple-500 text-white rounded">
|
||||||
|
PREVIEW
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
Use in Claude Desktop, VS Code & Cursor
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</a>
|
||||||
|
|
||||||
{/* Welcome section */}
|
{/* Welcome section */}
|
||||||
<div className="text-center mb-6">
|
<div className="text-center mb-6">
|
||||||
<h2 className="text-lg font-semibold text-foreground mb-2">
|
<h2 className="text-lg font-semibold text-foreground mb-2">
|
||||||
|
|||||||
@@ -10,9 +10,7 @@ import {
|
|||||||
Cpu,
|
Cpu,
|
||||||
FileCode,
|
FileCode,
|
||||||
FileText,
|
FileText,
|
||||||
Minus,
|
|
||||||
Pencil,
|
Pencil,
|
||||||
Plus,
|
|
||||||
RotateCcw,
|
RotateCcw,
|
||||||
ThumbsDown,
|
ThumbsDown,
|
||||||
ThumbsUp,
|
ThumbsUp,
|
||||||
@@ -30,6 +28,7 @@ import {
|
|||||||
} from "@/components/ai-elements/reasoning"
|
} from "@/components/ai-elements/reasoning"
|
||||||
import { ScrollArea } from "@/components/ui/scroll-area"
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
||||||
import {
|
import {
|
||||||
|
applyDiagramOperations,
|
||||||
convertToLegalXml,
|
convertToLegalXml,
|
||||||
isMxCellXmlComplete,
|
isMxCellXmlComplete,
|
||||||
replaceNodes,
|
replaceNodes,
|
||||||
@@ -38,9 +37,27 @@ import {
|
|||||||
import ExamplePanel from "./chat-example-panel"
|
import ExamplePanel from "./chat-example-panel"
|
||||||
import { CodeBlock } from "./code-block"
|
import { CodeBlock } from "./code-block"
|
||||||
|
|
||||||
interface EditPair {
|
interface DiagramOperation {
|
||||||
search: string
|
type: "update" | "add" | "delete"
|
||||||
replace: string
|
cell_id: string
|
||||||
|
new_xml?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper to extract complete operations from streaming input
|
||||||
|
function getCompleteOperations(
|
||||||
|
operations: DiagramOperation[] | undefined,
|
||||||
|
): DiagramOperation[] {
|
||||||
|
if (!operations || !Array.isArray(operations)) return []
|
||||||
|
return operations.filter(
|
||||||
|
(op) =>
|
||||||
|
op &&
|
||||||
|
typeof op.type === "string" &&
|
||||||
|
["update", "add", "delete"].includes(op.type) &&
|
||||||
|
typeof op.cell_id === "string" &&
|
||||||
|
op.cell_id.length > 0 &&
|
||||||
|
// delete doesn't need new_xml, update/add do
|
||||||
|
(op.type === "delete" || typeof op.new_xml === "string"),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tool part interface for type safety
|
// Tool part interface for type safety
|
||||||
@@ -48,49 +65,44 @@ interface ToolPartLike {
|
|||||||
type: string
|
type: string
|
||||||
toolCallId: string
|
toolCallId: string
|
||||||
state?: string
|
state?: string
|
||||||
input?: { xml?: string; edits?: EditPair[] } & Record<string, unknown>
|
input?: {
|
||||||
|
xml?: string
|
||||||
|
operations?: DiagramOperation[]
|
||||||
|
} & Record<string, unknown>
|
||||||
output?: string
|
output?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
function EditDiffDisplay({ edits }: { edits: EditPair[] }) {
|
function OperationsDisplay({ operations }: { operations: DiagramOperation[] }) {
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{edits.map((edit, index) => (
|
{operations.map((op, index) => (
|
||||||
<div
|
<div
|
||||||
key={`${(edit.search || "").slice(0, 50)}-${(edit.replace || "").slice(0, 50)}-${index}`}
|
key={`${op.type}-${op.cell_id}-${index}`}
|
||||||
className="rounded-lg border border-border/50 overflow-hidden bg-background/50"
|
className="rounded-lg border border-border/50 overflow-hidden bg-background/50"
|
||||||
>
|
>
|
||||||
<div className="px-3 py-1.5 bg-muted/40 border-b border-border/30 flex items-center gap-2">
|
<div className="px-3 py-1.5 bg-muted/40 border-b border-border/30 flex items-center gap-2">
|
||||||
<span className="text-xs font-medium text-muted-foreground">
|
<span
|
||||||
Change {index + 1}
|
className={`text-[10px] font-medium uppercase tracking-wide ${
|
||||||
|
op.type === "delete"
|
||||||
|
? "text-red-600"
|
||||||
|
: op.type === "add"
|
||||||
|
? "text-green-600"
|
||||||
|
: "text-blue-600"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{op.type}
|
||||||
|
</span>
|
||||||
|
<span className="text-xs text-muted-foreground">
|
||||||
|
cell_id: {op.cell_id}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="divide-y divide-border/30">
|
{op.new_xml && (
|
||||||
{/* Search (old) */}
|
|
||||||
<div className="px-3 py-2">
|
<div className="px-3 py-2">
|
||||||
<div className="flex items-center gap-1.5 mb-1.5">
|
<pre className="text-[11px] font-mono text-foreground/80 bg-muted/30 rounded px-2 py-1.5 overflow-x-auto whitespace-pre-wrap break-all">
|
||||||
<Minus className="w-3 h-3 text-red-500" />
|
{op.new_xml}
|
||||||
<span className="text-[10px] font-medium text-red-600 uppercase tracking-wide">
|
|
||||||
Remove
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<pre className="text-[11px] font-mono text-red-700 bg-red-50 rounded px-2 py-1.5 overflow-x-auto whitespace-pre-wrap break-all">
|
|
||||||
{edit.search}
|
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
{/* Replace (new) */}
|
)}
|
||||||
<div className="px-3 py-2">
|
|
||||||
<div className="flex items-center gap-1.5 mb-1.5">
|
|
||||||
<Plus className="w-3 h-3 text-green-500" />
|
|
||||||
<span className="text-[10px] font-medium text-green-600 uppercase tracking-wide">
|
|
||||||
Add
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<pre className="text-[11px] font-mono text-green-700 bg-green-50 rounded px-2 py-1.5 overflow-x-auto whitespace-pre-wrap break-all">
|
|
||||||
{edit.replace}
|
|
||||||
</pre>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -173,6 +185,7 @@ interface ChatMessageDisplayProps {
|
|||||||
setInput: (input: string) => void
|
setInput: (input: string) => void
|
||||||
setFiles: (files: File[]) => void
|
setFiles: (files: File[]) => void
|
||||||
processedToolCallsRef: MutableRefObject<Set<string>>
|
processedToolCallsRef: MutableRefObject<Set<string>>
|
||||||
|
editDiagramOriginalXmlRef: MutableRefObject<Map<string, string>>
|
||||||
sessionId?: string
|
sessionId?: string
|
||||||
onRegenerate?: (messageIndex: number) => void
|
onRegenerate?: (messageIndex: number) => void
|
||||||
onEditMessage?: (messageIndex: number, newText: string) => void
|
onEditMessage?: (messageIndex: number, newText: string) => void
|
||||||
@@ -184,6 +197,7 @@ export function ChatMessageDisplay({
|
|||||||
setInput,
|
setInput,
|
||||||
setFiles,
|
setFiles,
|
||||||
processedToolCallsRef,
|
processedToolCallsRef,
|
||||||
|
editDiagramOriginalXmlRef,
|
||||||
sessionId,
|
sessionId,
|
||||||
onRegenerate,
|
onRegenerate,
|
||||||
onEditMessage,
|
onEditMessage,
|
||||||
@@ -201,6 +215,14 @@ export function ChatMessageDisplay({
|
|||||||
null,
|
null,
|
||||||
)
|
)
|
||||||
const STREAMING_DEBOUNCE_MS = 150 // Only update diagram every 150ms during streaming
|
const STREAMING_DEBOUNCE_MS = 150 // Only update diagram every 150ms during streaming
|
||||||
|
// Refs for edit_diagram streaming
|
||||||
|
const pendingEditRef = useRef<{
|
||||||
|
operations: DiagramOperation[]
|
||||||
|
toolCallId: string
|
||||||
|
} | null>(null)
|
||||||
|
const editDebounceTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(
|
||||||
|
null,
|
||||||
|
)
|
||||||
const [expandedTools, setExpandedTools] = useState<Record<string, boolean>>(
|
const [expandedTools, setExpandedTools] = useState<Record<string, boolean>>(
|
||||||
{},
|
{},
|
||||||
)
|
)
|
||||||
@@ -292,15 +314,12 @@ export function ChatMessageDisplay({
|
|||||||
|
|
||||||
const handleDisplayChart = useCallback(
|
const handleDisplayChart = useCallback(
|
||||||
(xml: string, showToast = false) => {
|
(xml: string, showToast = false) => {
|
||||||
console.time("perf:handleDisplayChart")
|
|
||||||
const currentXml = xml || ""
|
const currentXml = xml || ""
|
||||||
const convertedXml = convertToLegalXml(currentXml)
|
const convertedXml = convertToLegalXml(currentXml)
|
||||||
if (convertedXml !== previousXML.current) {
|
if (convertedXml !== previousXML.current) {
|
||||||
// Parse and validate XML BEFORE calling replaceNodes
|
// Parse and validate XML BEFORE calling replaceNodes
|
||||||
console.time("perf:DOMParser")
|
|
||||||
const parser = new DOMParser()
|
const parser = new DOMParser()
|
||||||
const testDoc = parser.parseFromString(convertedXml, "text/xml")
|
const testDoc = parser.parseFromString(convertedXml, "text/xml")
|
||||||
console.timeEnd("perf:DOMParser")
|
|
||||||
const parseError = testDoc.querySelector("parsererror")
|
const parseError = testDoc.querySelector("parsererror")
|
||||||
|
|
||||||
if (parseError) {
|
if (parseError) {
|
||||||
@@ -316,7 +335,6 @@ export function ChatMessageDisplay({
|
|||||||
"AI generated invalid diagram XML. Please try regenerating.",
|
"AI generated invalid diagram XML. Please try regenerating.",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
console.timeEnd("perf:handleDisplayChart")
|
|
||||||
return // Skip this update
|
return // Skip this update
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,14 +344,10 @@ export function ChatMessageDisplay({
|
|||||||
const baseXML =
|
const baseXML =
|
||||||
chartXML ||
|
chartXML ||
|
||||||
`<mxfile><diagram name="Page-1" id="page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`
|
`<mxfile><diagram name="Page-1" id="page-1"><mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/></root></mxGraphModel></diagram></mxfile>`
|
||||||
console.time("perf:replaceNodes")
|
|
||||||
const replacedXML = replaceNodes(baseXML, convertedXml)
|
const replacedXML = replaceNodes(baseXML, convertedXml)
|
||||||
console.timeEnd("perf:replaceNodes")
|
|
||||||
|
|
||||||
// Validate and auto-fix the XML
|
// Validate and auto-fix the XML
|
||||||
console.time("perf:validateAndFixXml")
|
|
||||||
const validation = validateAndFixXml(replacedXML)
|
const validation = validateAndFixXml(replacedXML)
|
||||||
console.timeEnd("perf:validateAndFixXml")
|
|
||||||
if (validation.valid) {
|
if (validation.valid) {
|
||||||
previousXML.current = convertedXml
|
previousXML.current = convertedXml
|
||||||
// Use fixed XML if available, otherwise use original
|
// Use fixed XML if available, otherwise use original
|
||||||
@@ -370,9 +384,6 @@ export function ChatMessageDisplay({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.timeEnd("perf:handleDisplayChart")
|
|
||||||
} else {
|
|
||||||
console.timeEnd("perf:handleDisplayChart")
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[chartXML, onDisplayChart],
|
[chartXML, onDisplayChart],
|
||||||
@@ -391,11 +402,6 @@ export function ChatMessageDisplay({
|
|||||||
}, [editingMessageId])
|
}, [editingMessageId])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.time("perf:message-display-useEffect")
|
|
||||||
let processedCount = 0
|
|
||||||
let skippedCount = 0
|
|
||||||
let debouncedCount = 0
|
|
||||||
|
|
||||||
// Only process the last message for streaming performance
|
// Only process the last message for streaming performance
|
||||||
// Previous messages are already processed and won't change
|
// Previous messages are already processed and won't change
|
||||||
const messagesToProcess =
|
const messagesToProcess =
|
||||||
@@ -425,7 +431,6 @@ export function ChatMessageDisplay({
|
|||||||
const lastXml =
|
const lastXml =
|
||||||
lastProcessedXmlRef.current.get(toolCallId)
|
lastProcessedXmlRef.current.get(toolCallId)
|
||||||
if (lastXml === xml) {
|
if (lastXml === xml) {
|
||||||
skippedCount++
|
|
||||||
return // Skip redundant processing
|
return // Skip redundant processing
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,9 +450,6 @@ export function ChatMessageDisplay({
|
|||||||
debounceTimeoutRef.current = null
|
debounceTimeoutRef.current = null
|
||||||
pendingXmlRef.current = null
|
pendingXmlRef.current = null
|
||||||
if (pendingXml) {
|
if (pendingXml) {
|
||||||
console.log(
|
|
||||||
"perf:debounced-handleDisplayChart executing",
|
|
||||||
)
|
|
||||||
handleDisplayChart(
|
handleDisplayChart(
|
||||||
pendingXml,
|
pendingXml,
|
||||||
false,
|
false,
|
||||||
@@ -461,7 +463,6 @@ export function ChatMessageDisplay({
|
|||||||
STREAMING_DEBOUNCE_MS,
|
STREAMING_DEBOUNCE_MS,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
debouncedCount++
|
|
||||||
} else if (
|
} else if (
|
||||||
state === "output-available" &&
|
state === "output-available" &&
|
||||||
!processedToolCalls.current.has(toolCallId)
|
!processedToolCalls.current.has(toolCallId)
|
||||||
@@ -477,17 +478,129 @@ export function ChatMessageDisplay({
|
|||||||
processedToolCalls.current.add(toolCallId)
|
processedToolCalls.current.add(toolCallId)
|
||||||
// Clean up the ref entry - tool is complete, no longer needed
|
// Clean up the ref entry - tool is complete, no longer needed
|
||||||
lastProcessedXmlRef.current.delete(toolCallId)
|
lastProcessedXmlRef.current.delete(toolCallId)
|
||||||
processedCount++
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle edit_diagram streaming - apply operations incrementally for preview
|
||||||
|
// Uses shared editDiagramOriginalXmlRef to coordinate with tool handler
|
||||||
|
if (
|
||||||
|
part.type === "tool-edit_diagram" &&
|
||||||
|
input?.operations
|
||||||
|
) {
|
||||||
|
const completeOps = getCompleteOperations(
|
||||||
|
input.operations as DiagramOperation[],
|
||||||
|
)
|
||||||
|
|
||||||
|
if (completeOps.length === 0) return
|
||||||
|
|
||||||
|
// Capture original XML when streaming starts (store in shared ref)
|
||||||
|
if (
|
||||||
|
!editDiagramOriginalXmlRef.current.has(
|
||||||
|
toolCallId,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
if (!chartXML) {
|
||||||
|
console.warn(
|
||||||
|
"[edit_diagram streaming] No chart XML available",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
editDiagramOriginalXmlRef.current.set(
|
||||||
|
toolCallId,
|
||||||
|
chartXML,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalXml =
|
||||||
|
editDiagramOriginalXmlRef.current.get(
|
||||||
|
toolCallId,
|
||||||
|
)
|
||||||
|
if (!originalXml) return
|
||||||
|
|
||||||
|
// Skip if no change from last processed state
|
||||||
|
const lastCount = lastProcessedXmlRef.current.get(
|
||||||
|
toolCallId + "-opCount",
|
||||||
|
)
|
||||||
|
if (lastCount === String(completeOps.length)) return
|
||||||
|
|
||||||
|
if (
|
||||||
|
state === "input-streaming" ||
|
||||||
|
state === "input-available"
|
||||||
|
) {
|
||||||
|
// Queue the operations for debounced processing
|
||||||
|
pendingEditRef.current = {
|
||||||
|
operations: completeOps,
|
||||||
|
toolCallId,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!editDebounceTimeoutRef.current) {
|
||||||
|
editDebounceTimeoutRef.current = setTimeout(
|
||||||
|
() => {
|
||||||
|
const pending =
|
||||||
|
pendingEditRef.current
|
||||||
|
editDebounceTimeoutRef.current =
|
||||||
|
null
|
||||||
|
pendingEditRef.current = null
|
||||||
|
|
||||||
|
if (pending) {
|
||||||
|
const origXml =
|
||||||
|
editDiagramOriginalXmlRef.current.get(
|
||||||
|
pending.toolCallId,
|
||||||
|
)
|
||||||
|
if (!origXml) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
result: editedXml,
|
||||||
|
} = applyDiagramOperations(
|
||||||
|
origXml,
|
||||||
|
pending.operations,
|
||||||
|
)
|
||||||
|
handleDisplayChart(
|
||||||
|
editedXml,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
lastProcessedXmlRef.current.set(
|
||||||
|
pending.toolCallId +
|
||||||
|
"-opCount",
|
||||||
|
String(
|
||||||
|
pending.operations
|
||||||
|
.length,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(
|
||||||
|
`[edit_diagram streaming] Operation failed:`,
|
||||||
|
e instanceof Error
|
||||||
|
? e.message
|
||||||
|
: e,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
STREAMING_DEBOUNCE_MS,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
state === "output-available" &&
|
||||||
|
!processedToolCalls.current.has(toolCallId)
|
||||||
|
) {
|
||||||
|
// Final state - cleanup streaming refs (tool handler does final application)
|
||||||
|
if (editDebounceTimeoutRef.current) {
|
||||||
|
clearTimeout(editDebounceTimeoutRef.current)
|
||||||
|
editDebounceTimeoutRef.current = null
|
||||||
|
}
|
||||||
|
lastProcessedXmlRef.current.delete(
|
||||||
|
toolCallId + "-opCount",
|
||||||
|
)
|
||||||
|
processedToolCalls.current.add(toolCallId)
|
||||||
|
// Note: Don't delete editDiagramOriginalXmlRef here - tool handler needs it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(
|
|
||||||
`perf:message-display-useEffect processed=${processedCount} skipped=${skippedCount} debounced=${debouncedCount}`,
|
|
||||||
)
|
|
||||||
console.timeEnd("perf:message-display-useEffect")
|
|
||||||
|
|
||||||
// Cleanup: clear any pending debounce timeout on unmount
|
// Cleanup: clear any pending debounce timeout on unmount
|
||||||
return () => {
|
return () => {
|
||||||
@@ -495,8 +608,12 @@ export function ChatMessageDisplay({
|
|||||||
clearTimeout(debounceTimeoutRef.current)
|
clearTimeout(debounceTimeoutRef.current)
|
||||||
debounceTimeoutRef.current = null
|
debounceTimeoutRef.current = null
|
||||||
}
|
}
|
||||||
|
if (editDebounceTimeoutRef.current) {
|
||||||
|
clearTimeout(editDebounceTimeoutRef.current)
|
||||||
|
editDebounceTimeoutRef.current = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [messages, handleDisplayChart])
|
}, [messages, handleDisplayChart, chartXML])
|
||||||
|
|
||||||
const renderToolPart = (part: ToolPartLike) => {
|
const renderToolPart = (part: ToolPartLike) => {
|
||||||
const callId = part.toolCallId
|
const callId = part.toolCallId
|
||||||
@@ -582,9 +699,9 @@ export function ChatMessageDisplay({
|
|||||||
{typeof input === "object" && input.xml ? (
|
{typeof input === "object" && input.xml ? (
|
||||||
<CodeBlock code={input.xml} language="xml" />
|
<CodeBlock code={input.xml} language="xml" />
|
||||||
) : typeof input === "object" &&
|
) : typeof input === "object" &&
|
||||||
input.edits &&
|
input.operations &&
|
||||||
Array.isArray(input.edits) ? (
|
Array.isArray(input.operations) ? (
|
||||||
<EditDiffDisplay edits={input.edits} />
|
<OperationsDisplay operations={input.operations} />
|
||||||
) : typeof input === "object" &&
|
) : typeof input === "object" &&
|
||||||
Object.keys(input).length > 0 ? (
|
Object.keys(input).length > 0 ? (
|
||||||
<CodeBlock
|
<CodeBlock
|
||||||
|
|||||||
@@ -202,6 +202,10 @@ export default function ChatPanel({
|
|||||||
// Persist processed tool call IDs so collapsing the chat doesn't replay old tool outputs
|
// Persist processed tool call IDs so collapsing the chat doesn't replay old tool outputs
|
||||||
const processedToolCallsRef = useRef<Set<string>>(new Set())
|
const processedToolCallsRef = useRef<Set<string>>(new Set())
|
||||||
|
|
||||||
|
// Store original XML for edit_diagram streaming - shared between streaming preview and tool handler
|
||||||
|
// Key: toolCallId, Value: original XML before any operations applied
|
||||||
|
const editDiagramOriginalXmlRef = useRef<Map<string, string>>(new Map())
|
||||||
|
|
||||||
// Debounce timeout for localStorage writes (prevents blocking during streaming)
|
// Debounce timeout for localStorage writes (prevents blocking during streaming)
|
||||||
const localStorageDebounceRef = useRef<ReturnType<
|
const localStorageDebounceRef = useRef<ReturnType<
|
||||||
typeof setTimeout
|
typeof setTimeout
|
||||||
@@ -323,23 +327,68 @@ ${finalXml}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (toolCall.toolName === "edit_diagram") {
|
} else if (toolCall.toolName === "edit_diagram") {
|
||||||
const { edits } = toolCall.input as {
|
const { operations } = toolCall.input as {
|
||||||
edits: Array<{ search: string; replace: string }>
|
operations: Array<{
|
||||||
|
type: "update" | "add" | "delete"
|
||||||
|
cell_id: string
|
||||||
|
new_xml?: string
|
||||||
|
}>
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentXml = ""
|
let currentXml = ""
|
||||||
try {
|
try {
|
||||||
// Use chartXML from ref directly - more reliable than export
|
// Use the original XML captured during streaming (shared with chat-message-display)
|
||||||
const cachedXML = chartXMLRef.current
|
// This ensures we apply operations to the same base XML that streaming used
|
||||||
if (cachedXML) {
|
const originalXml = editDiagramOriginalXmlRef.current.get(
|
||||||
currentXml = cachedXML
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
|
if (originalXml) {
|
||||||
|
currentXml = originalXml
|
||||||
} else {
|
} else {
|
||||||
// Fallback to export only if no cached XML
|
// Fallback: use chartXML from ref if streaming didn't capture original
|
||||||
currentXml = await onFetchChart(false)
|
const cachedXML = chartXMLRef.current
|
||||||
|
if (cachedXML) {
|
||||||
|
currentXml = cachedXML
|
||||||
|
} else {
|
||||||
|
// Last resort: export from iframe
|
||||||
|
currentXml = await onFetchChart(false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { replaceXMLParts } = await import("@/lib/utils")
|
const { applyDiagramOperations } = await import(
|
||||||
const editedXml = replaceXMLParts(currentXml, edits)
|
"@/lib/utils"
|
||||||
|
)
|
||||||
|
const { result: editedXml, errors } =
|
||||||
|
applyDiagramOperations(currentXml, operations)
|
||||||
|
|
||||||
|
// Check for operation errors
|
||||||
|
if (errors.length > 0) {
|
||||||
|
const errorMessages = errors
|
||||||
|
.map(
|
||||||
|
(e) =>
|
||||||
|
`- ${e.type} on cell_id="${e.cellId}": ${e.message}`,
|
||||||
|
)
|
||||||
|
.join("\n")
|
||||||
|
|
||||||
|
addToolOutput({
|
||||||
|
tool: "edit_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `Some operations failed:\n${errorMessages}
|
||||||
|
|
||||||
|
Current diagram XML:
|
||||||
|
\`\`\`xml
|
||||||
|
${currentXml}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Please check the cell IDs and retry.`,
|
||||||
|
})
|
||||||
|
// Clean up the shared original XML ref
|
||||||
|
editDiagramOriginalXmlRef.current.delete(
|
||||||
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// loadDiagram validates and returns error if invalid
|
// loadDiagram validates and returns error if invalid
|
||||||
const validationError = onDisplayChart(editedXml)
|
const validationError = onDisplayChart(editedXml)
|
||||||
@@ -359,23 +408,30 @@ Current diagram XML:
|
|||||||
${currentXml}
|
${currentXml}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
Please fix the edit to avoid structural issues (e.g., duplicate IDs, invalid references).`,
|
Please fix the operations to avoid structural issues.`,
|
||||||
})
|
})
|
||||||
|
// Clean up the shared original XML ref
|
||||||
|
editDiagramOriginalXmlRef.current.delete(
|
||||||
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
onExport()
|
onExport()
|
||||||
addToolOutput({
|
addToolOutput({
|
||||||
tool: "edit_diagram",
|
tool: "edit_diagram",
|
||||||
toolCallId: toolCall.toolCallId,
|
toolCallId: toolCall.toolCallId,
|
||||||
output: `Successfully applied ${edits.length} edit(s) to the diagram.`,
|
output: `Successfully applied ${operations.length} operation(s) to the diagram.`,
|
||||||
})
|
})
|
||||||
|
// Clean up the shared original XML ref
|
||||||
|
editDiagramOriginalXmlRef.current.delete(
|
||||||
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("[edit_diagram] Failed:", error)
|
console.error("[edit_diagram] Failed:", error)
|
||||||
|
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
error instanceof Error ? error.message : String(error)
|
error instanceof Error ? error.message : String(error)
|
||||||
|
|
||||||
// Use addToolOutput with state: 'output-error' for proper error signaling
|
|
||||||
addToolOutput({
|
addToolOutput({
|
||||||
tool: "edit_diagram",
|
tool: "edit_diagram",
|
||||||
toolCallId: toolCall.toolCallId,
|
toolCallId: toolCall.toolCallId,
|
||||||
@@ -387,8 +443,12 @@ Current diagram XML:
|
|||||||
${currentXml || "No XML available"}
|
${currentXml || "No XML available"}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
Please retry with an adjusted search pattern or use display_diagram if retries are exhausted.`,
|
Please check cell IDs and retry, or use display_diagram to regenerate.`,
|
||||||
})
|
})
|
||||||
|
// Clean up the shared original XML ref even on error
|
||||||
|
editDiagramOriginalXmlRef.current.delete(
|
||||||
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} else if (toolCall.toolName === "append_diagram") {
|
} else if (toolCall.toolName === "append_diagram") {
|
||||||
const { xml } = toolCall.input as { xml: string }
|
const { xml } = toolCall.input as { xml: string }
|
||||||
@@ -477,6 +537,32 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
// Silence access code error in console since it's handled by UI
|
// Silence access code error in console since it's handled by UI
|
||||||
if (!error.message.includes("Invalid or missing access code")) {
|
if (!error.message.includes("Invalid or missing access code")) {
|
||||||
console.error("Chat error:", error)
|
console.error("Chat error:", error)
|
||||||
|
// Debug: Log messages structure when error occurs
|
||||||
|
console.log("[onError] messages count:", messages.length)
|
||||||
|
messages.forEach((msg, idx) => {
|
||||||
|
console.log(`[onError] Message ${idx}:`, {
|
||||||
|
role: msg.role,
|
||||||
|
partsCount: msg.parts?.length,
|
||||||
|
})
|
||||||
|
if (msg.parts) {
|
||||||
|
msg.parts.forEach((part: any, partIdx: number) => {
|
||||||
|
console.log(
|
||||||
|
`[onError] Part ${partIdx}:`,
|
||||||
|
JSON.stringify({
|
||||||
|
type: part.type,
|
||||||
|
toolName: part.toolName,
|
||||||
|
hasInput: !!part.input,
|
||||||
|
inputType: typeof part.input,
|
||||||
|
inputKeys:
|
||||||
|
part.input &&
|
||||||
|
typeof part.input === "object"
|
||||||
|
? Object.keys(part.input)
|
||||||
|
: null,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate technical errors into user-friendly messages
|
// Translate technical errors into user-friendly messages
|
||||||
@@ -692,12 +778,10 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
// Debounce: save after 1 second of no changes
|
// Debounce: save after 1 second of no changes
|
||||||
localStorageDebounceRef.current = setTimeout(() => {
|
localStorageDebounceRef.current = setTimeout(() => {
|
||||||
try {
|
try {
|
||||||
console.time("perf:localStorage-messages")
|
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
STORAGE_MESSAGES_KEY,
|
STORAGE_MESSAGES_KEY,
|
||||||
JSON.stringify(messages),
|
JSON.stringify(messages),
|
||||||
)
|
)
|
||||||
console.timeEnd("perf:localStorage-messages")
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to save messages to localStorage:", error)
|
console.error("Failed to save messages to localStorage:", error)
|
||||||
}
|
}
|
||||||
@@ -723,9 +807,7 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
|
|
||||||
// Debounce: save after 1 second of no changes
|
// Debounce: save after 1 second of no changes
|
||||||
xmlStorageDebounceRef.current = setTimeout(() => {
|
xmlStorageDebounceRef.current = setTimeout(() => {
|
||||||
console.time("perf:localStorage-xml")
|
|
||||||
localStorage.setItem(STORAGE_DIAGRAM_XML_KEY, chartXML)
|
localStorage.setItem(STORAGE_DIAGRAM_XML_KEY, chartXML)
|
||||||
console.timeEnd("perf:localStorage-xml")
|
|
||||||
}, LOCAL_STORAGE_DEBOUNCE_MS)
|
}, LOCAL_STORAGE_DEBOUNCE_MS)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -738,13 +820,11 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
// Save XML snapshots to localStorage whenever they change
|
// Save XML snapshots to localStorage whenever they change
|
||||||
const saveXmlSnapshots = useCallback(() => {
|
const saveXmlSnapshots = useCallback(() => {
|
||||||
try {
|
try {
|
||||||
console.time("perf:localStorage-snapshots")
|
|
||||||
const snapshotsArray = Array.from(xmlSnapshotsRef.current.entries())
|
const snapshotsArray = Array.from(xmlSnapshotsRef.current.entries())
|
||||||
localStorage.setItem(
|
localStorage.setItem(
|
||||||
STORAGE_XML_SNAPSHOTS_KEY,
|
STORAGE_XML_SNAPSHOTS_KEY,
|
||||||
JSON.stringify(snapshotsArray),
|
JSON.stringify(snapshotsArray),
|
||||||
)
|
)
|
||||||
console.timeEnd("perf:localStorage-snapshots")
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(
|
console.error(
|
||||||
"Failed to save XML snapshots to localStorage:",
|
"Failed to save XML snapshots to localStorage:",
|
||||||
@@ -1295,6 +1375,7 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
setInput={setInput}
|
setInput={setInput}
|
||||||
setFiles={handleFileChange}
|
setFiles={handleFileChange}
|
||||||
processedToolCallsRef={processedToolCallsRef}
|
processedToolCallsRef={processedToolCallsRef}
|
||||||
|
editDiagramOriginalXmlRef={editDiagramOriginalXmlRef}
|
||||||
sessionId={sessionId}
|
sessionId={sessionId}
|
||||||
onRegenerate={handleRegenerate}
|
onRegenerate={handleRegenerate}
|
||||||
status={status}
|
status={status}
|
||||||
|
|||||||
@@ -86,20 +86,16 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
chart: string,
|
chart: string,
|
||||||
skipValidation?: boolean,
|
skipValidation?: boolean,
|
||||||
): string | null => {
|
): string | null => {
|
||||||
console.time("perf:loadDiagram")
|
|
||||||
let xmlToLoad = chart
|
let xmlToLoad = chart
|
||||||
|
|
||||||
// Validate XML structure before loading (unless skipped for internal use)
|
// Validate XML structure before loading (unless skipped for internal use)
|
||||||
if (!skipValidation) {
|
if (!skipValidation) {
|
||||||
console.time("perf:loadDiagram-validation")
|
|
||||||
const validation = validateAndFixXml(chart)
|
const validation = validateAndFixXml(chart)
|
||||||
console.timeEnd("perf:loadDiagram-validation")
|
|
||||||
if (!validation.valid) {
|
if (!validation.valid) {
|
||||||
console.warn(
|
console.warn(
|
||||||
"[loadDiagram] Validation error:",
|
"[loadDiagram] Validation error:",
|
||||||
validation.error,
|
validation.error,
|
||||||
)
|
)
|
||||||
console.timeEnd("perf:loadDiagram")
|
|
||||||
return validation.error
|
return validation.error
|
||||||
}
|
}
|
||||||
// Use fixed XML if auto-fix was applied
|
// Use fixed XML if auto-fix was applied
|
||||||
@@ -116,14 +112,11 @@ export function DiagramProvider({ children }: { children: React.ReactNode }) {
|
|||||||
setChartXML(xmlToLoad)
|
setChartXML(xmlToLoad)
|
||||||
|
|
||||||
if (drawioRef.current) {
|
if (drawioRef.current) {
|
||||||
console.time("perf:drawio-iframe-load")
|
|
||||||
drawioRef.current.load({
|
drawioRef.current.load({
|
||||||
xml: xmlToLoad,
|
xml: xmlToLoad,
|
||||||
})
|
})
|
||||||
console.timeEnd("perf:drawio-iframe-load")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeEnd("perf:loadDiagram")
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ https://github.com/user-attachments/assets/b2eef5f3-b335-4e71-a755-dc2e80931979
|
|||||||
- [目录](#目录)
|
- [目录](#目录)
|
||||||
- [示例](#示例)
|
- [示例](#示例)
|
||||||
- [功能特性](#功能特性)
|
- [功能特性](#功能特性)
|
||||||
|
- [MCP服务器(预览)](#mcp服务器预览)
|
||||||
- [快速开始](#快速开始)
|
- [快速开始](#快速开始)
|
||||||
- [在线试用](#在线试用)
|
- [在线试用](#在线试用)
|
||||||
- [使用Docker运行(推荐)](#使用docker运行推荐)
|
- [使用Docker运行(推荐)](#使用docker运行推荐)
|
||||||
@@ -88,6 +89,36 @@ https://github.com/user-attachments/assets/b2eef5f3-b335-4e71-a755-dc2e80931979
|
|||||||
- **云架构图支持**:专门支持生成云架构图(AWS、GCP、Azure)
|
- **云架构图支持**:专门支持生成云架构图(AWS、GCP、Azure)
|
||||||
- **动画连接器**:在图表元素之间创建动态动画连接器,实现更好的可视化效果
|
- **动画连接器**:在图表元素之间创建动态动画连接器,实现更好的可视化效果
|
||||||
|
|
||||||
|
## MCP服务器(预览)
|
||||||
|
|
||||||
|
> **预览功能**:此功能为实验性功能,可能会有变化。
|
||||||
|
|
||||||
|
通过MCP(模型上下文协议)在Claude Desktop、Cursor和VS Code等AI代理中使用Next AI Draw.io。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Claude Code CLI
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add drawio -- npx @next-ai-drawio/mcp-server@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
然后让Claude创建图表:
|
||||||
|
> "创建一个展示用户认证流程的流程图,包含登录、MFA和会话管理"
|
||||||
|
|
||||||
|
图表会实时显示在浏览器中!
|
||||||
|
|
||||||
|
详情请参阅[MCP服务器README](../packages/mcp-server/README.md),了解VS Code、Cursor等客户端配置。
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
||||||
### 在线试用
|
### 在线试用
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ https://github.com/user-attachments/assets/b2eef5f3-b335-4e71-a755-dc2e80931979
|
|||||||
- [目次](#目次)
|
- [目次](#目次)
|
||||||
- [例](#例)
|
- [例](#例)
|
||||||
- [機能](#機能)
|
- [機能](#機能)
|
||||||
|
- [MCPサーバー(プレビュー)](#mcpサーバープレビュー)
|
||||||
- [はじめに](#はじめに)
|
- [はじめに](#はじめに)
|
||||||
- [オンラインで試す](#オンラインで試す)
|
- [オンラインで試す](#オンラインで試す)
|
||||||
- [Dockerで実行(推奨)](#dockerで実行推奨)
|
- [Dockerで実行(推奨)](#dockerで実行推奨)
|
||||||
@@ -88,6 +89,36 @@ https://github.com/user-attachments/assets/b2eef5f3-b335-4e71-a755-dc2e80931979
|
|||||||
- **クラウドアーキテクチャダイアグラムサポート**:クラウドアーキテクチャダイアグラムの生成を専門的にサポート(AWS、GCP、Azure)
|
- **クラウドアーキテクチャダイアグラムサポート**:クラウドアーキテクチャダイアグラムの生成を専門的にサポート(AWS、GCP、Azure)
|
||||||
- **アニメーションコネクタ**:より良い可視化のためにダイアグラム要素間に動的でアニメーション化されたコネクタを作成
|
- **アニメーションコネクタ**:より良い可視化のためにダイアグラム要素間に動的でアニメーション化されたコネクタを作成
|
||||||
|
|
||||||
|
## MCPサーバー(プレビュー)
|
||||||
|
|
||||||
|
> **プレビュー機能**:この機能は実験的であり、変更される可能性があります。
|
||||||
|
|
||||||
|
MCP(Model Context Protocol)を介して、Claude Desktop、Cursor、VS CodeなどのAIエージェントでNext AI Draw.ioを使用できます。
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Claude Code CLI
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add drawio -- npx @next-ai-drawio/mcp-server@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
Claudeにダイアグラムの作成を依頼:
|
||||||
|
> 「ログイン、MFA、セッション管理を含むユーザー認証のフローチャートを作成してください」
|
||||||
|
|
||||||
|
ダイアグラムがリアルタイムでブラウザに表示されます!
|
||||||
|
|
||||||
|
詳細は[MCPサーバーREADME](../packages/mcp-server/README.md)をご覧ください(VS Code、Cursorなどのクライアント設定も含む)。
|
||||||
|
|
||||||
## はじめに
|
## はじめに
|
||||||
|
|
||||||
### オンラインで試す
|
### オンラインで試す
|
||||||
|
|||||||
@@ -438,6 +438,16 @@ function validateProviderCredentials(provider: ProviderName): void {
|
|||||||
* - SILICONFLOW_BASE_URL: SiliconFlow endpoint (optional, defaults to https://api.siliconflow.com/v1)
|
* - SILICONFLOW_BASE_URL: SiliconFlow endpoint (optional, defaults to https://api.siliconflow.com/v1)
|
||||||
*/
|
*/
|
||||||
export function getAIModel(overrides?: ClientOverrides): ModelConfig {
|
export function getAIModel(overrides?: ClientOverrides): ModelConfig {
|
||||||
|
// SECURITY: Prevent SSRF attacks (GHSA-9qf7-mprq-9qgm)
|
||||||
|
// If a custom baseUrl is provided, an API key MUST also be provided.
|
||||||
|
// This prevents attackers from redirecting server API keys to malicious endpoints.
|
||||||
|
if (overrides?.baseUrl && !overrides?.apiKey) {
|
||||||
|
throw new Error(
|
||||||
|
`API key is required when using a custom base URL. ` +
|
||||||
|
`Please provide your own API key in Settings.`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Check if client is providing their own provider override
|
// Check if client is providing their own provider override
|
||||||
const isClientOverride = !!(overrides?.provider && overrides?.apiKey)
|
const isClientOverride = !!(overrides?.provider && overrides?.apiKey)
|
||||||
|
|
||||||
|
|||||||
@@ -88,19 +88,15 @@ Note that:
|
|||||||
- NEVER include XML comments (<!-- ... -->) in your generated XML. Draw.io strips comments, which breaks edit_diagram patterns.
|
- NEVER include XML comments (<!-- ... -->) in your generated XML. Draw.io strips comments, which breaks edit_diagram patterns.
|
||||||
|
|
||||||
When using edit_diagram tool:
|
When using edit_diagram tool:
|
||||||
- CRITICAL: Copy search patterns EXACTLY from the "Current diagram XML" in system context - attribute order matters!
|
- Use operations: update (modify cell by id), add (new cell), delete (remove cell by id)
|
||||||
- Always include the element's id attribute for unique targeting: {"search": "<mxCell id=\\"5\\"", ...}
|
- For update/add: provide cell_id and complete new_xml (full mxCell element including mxGeometry)
|
||||||
- Include complete elements (mxCell + mxGeometry) for reliable matching
|
- For delete: only cell_id is needed
|
||||||
- Preserve exact whitespace, indentation, and line breaks
|
- Find the cell_id from "Current diagram XML" in system context
|
||||||
- BAD: {"search": "value=\\"Label\\"", ...} - too vague, matches multiple elements
|
- Example update: {"operations": [{"type": "update", "cell_id": "3", "new_xml": "<mxCell id=\\"3\\" value=\\"New Label\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>"}]}
|
||||||
- GOOD: {"search": "<mxCell id=\\"3\\" value=\\"Old\\" style=\\"...\\">", "replace": "<mxCell id=\\"3\\" value=\\"New\\" style=\\"...\\">"}
|
- Example delete: {"operations": [{"type": "delete", "cell_id": "5"}]}
|
||||||
- For multiple changes, use separate edits in array
|
- Example add: {"operations": [{"type": "add", "cell_id": "new1", "new_xml": "<mxCell id=\\"new1\\" value=\\"New Box\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"400\\" y=\\"200\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>"}]}
|
||||||
- RETRY POLICY: If pattern not found, retry up to 3 times with adjusted patterns. After 3 failures, use display_diagram instead.
|
|
||||||
|
|
||||||
⚠️ CRITICAL JSON ESCAPING: When outputting edit_diagram tool calls, you MUST escape ALL double quotes inside string values:
|
⚠️ JSON ESCAPING: Every " inside new_xml MUST be escaped as \\". Example: id=\\"5\\" value=\\"Label\\"
|
||||||
- CORRECT: "y=\\"119\\"" (both quotes escaped)
|
|
||||||
- WRONG: "y="119\\"" (missing backslash before first quote - causes JSON parse error!)
|
|
||||||
- Every " inside a JSON string value needs \\" - no exceptions!
|
|
||||||
|
|
||||||
## Draw.io XML Structure Reference
|
## Draw.io XML Structure Reference
|
||||||
|
|
||||||
@@ -268,69 +264,43 @@ const EXTENDED_ADDITIONS = `
|
|||||||
|
|
||||||
### edit_diagram Details
|
### edit_diagram Details
|
||||||
|
|
||||||
**CRITICAL RULES:**
|
edit_diagram uses ID-based operations to modify cells directly by their id attribute.
|
||||||
- Copy-paste the EXACT search pattern from the "Current diagram XML" in system context
|
|
||||||
- Do NOT reorder attributes or reformat - the attribute order in draw.io XML varies and you MUST match it exactly
|
**Operations:**
|
||||||
- Only include the lines that are changing, plus 1-2 surrounding lines for context if needed
|
- **update**: Replace an existing cell. Provide cell_id and new_xml.
|
||||||
- Break large changes into multiple smaller edits
|
- **add**: Add a new cell. Provide cell_id (new unique id) and new_xml.
|
||||||
- Each search must contain complete lines (never truncate mid-line)
|
- **delete**: Remove a cell. Only cell_id is needed.
|
||||||
- First match only - be specific enough to target the right element
|
|
||||||
|
|
||||||
**Input Format:**
|
**Input Format:**
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
{
|
{
|
||||||
"edits": [
|
"operations": [
|
||||||
{
|
{"type": "update", "cell_id": "3", "new_xml": "<mxCell ...complete element...>"},
|
||||||
"search": "EXACT lines copied from current XML (preserve attribute order!)",
|
{"type": "add", "cell_id": "new1", "new_xml": "<mxCell ...new element...>"},
|
||||||
"replace": "Replacement lines"
|
{"type": "delete", "cell_id": "5"}
|
||||||
}
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
## edit_diagram Best Practices
|
**Examples:**
|
||||||
|
|
||||||
### Core Principle: Unique & Precise Patterns
|
Change label:
|
||||||
Your search pattern MUST uniquely identify exactly ONE location in the XML. Before writing a search pattern:
|
|
||||||
1. Review the "Current diagram XML" in the system context
|
|
||||||
2. Identify the exact element(s) to modify by their unique id attribute
|
|
||||||
3. Include enough context to ensure uniqueness
|
|
||||||
|
|
||||||
### Pattern Construction Rules
|
|
||||||
|
|
||||||
**Rule 1: Always include the element's id attribute**
|
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
{"search": "<mxCell id=\\"node5\\"", "replace": "<mxCell id=\\"node5\\" value=\\"New Label\\""}
|
{"operations": [{"type": "update", "cell_id": "3", "new_xml": "<mxCell id=\\"3\\" value=\\"New Label\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>"}]}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
**Rule 2: Include complete XML elements when possible**
|
Add new shape:
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
{
|
{"operations": [{"type": "add", "cell_id": "new1", "new_xml": "<mxCell id=\\"new1\\" value=\\"New Box\\" style=\\"rounded=1;fillColor=#dae8fc;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"400\\" y=\\"200\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>"}]}
|
||||||
"search": "<mxCell id=\\"3\\" value=\\"Old\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>",
|
|
||||||
"replace": "<mxCell id=\\"3\\" value=\\"New\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\">\\n <mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/>\\n</mxCell>"
|
|
||||||
}
|
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
**Rule 3: Preserve exact whitespace and formatting**
|
Delete cell:
|
||||||
Copy the search pattern EXACTLY from the current XML, including leading spaces, line breaks (\\n), and attribute order.
|
\`\`\`json
|
||||||
|
{"operations": [{"type": "delete", "cell_id": "5"}]}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
### Good vs Bad Patterns
|
**Error Recovery:**
|
||||||
|
If cell_id not found, check "Current diagram XML" for correct IDs. Use display_diagram if major restructuring is needed
|
||||||
**BAD:** \`{"search": "value=\\"Label\\""}\` - Too vague, matches multiple elements
|
|
||||||
**BAD:** \`{"search": "<mxCell value=\\"X\\" id=\\"5\\""}\` - Reordered attributes won't match
|
|
||||||
**GOOD:** \`{"search": "<mxCell id=\\"5\\" parent=\\"1\\" style=\\"...\\" value=\\"Old\\" vertex=\\"1\\">"}\` - Uses unique id with full context
|
|
||||||
|
|
||||||
### ⚠️ JSON Escaping (CRITICAL)
|
|
||||||
Every double quote inside JSON string values MUST be escaped with backslash:
|
|
||||||
- **CORRECT:** \`"x=\\"100\\" y=\\"200\\""\` - both quotes escaped
|
|
||||||
- **WRONG:** \`"x=\\"100\\" y="200\\""\` - missing backslash causes JSON parse error!
|
|
||||||
|
|
||||||
### Error Recovery
|
|
||||||
If edit_diagram fails with "pattern not found":
|
|
||||||
1. **First retry**: Check attribute order - copy EXACTLY from current XML
|
|
||||||
2. **Second retry**: Expand context - include more surrounding lines
|
|
||||||
3. **Third retry**: Try matching on just \`<mxCell id="X"\` prefix + full replacement
|
|
||||||
4. **After 3 failures**: Fall back to display_diagram to regenerate entire diagram
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
522
lib/utils.ts
522
lib/utils.ts
@@ -377,303 +377,223 @@ export function replaceNodes(currentXML: string, nodes: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// ============================================================================
|
||||||
* Create a character count dictionary from a string
|
// ID-based Diagram Operations
|
||||||
* Used for attribute-order agnostic comparison
|
// ============================================================================
|
||||||
*/
|
|
||||||
function charCountDict(str: string): Map<string, number> {
|
export interface DiagramOperation {
|
||||||
const dict = new Map<string, number>()
|
type: "update" | "add" | "delete"
|
||||||
for (const char of str) {
|
cell_id: string
|
||||||
dict.set(char, (dict.get(char) || 0) + 1)
|
new_xml?: string
|
||||||
}
|
}
|
||||||
return dict
|
|
||||||
|
export interface OperationError {
|
||||||
|
type: "update" | "add" | "delete"
|
||||||
|
cellId: string
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApplyOperationsResult {
|
||||||
|
result: string
|
||||||
|
errors: OperationError[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two strings by character frequency (order-agnostic)
|
* Apply diagram operations (update/add/delete) using ID-based lookup.
|
||||||
|
* This replaces the text-matching approach with direct DOM manipulation.
|
||||||
|
*
|
||||||
|
* @param xmlContent - The full mxfile XML content
|
||||||
|
* @param operations - Array of operations to apply
|
||||||
|
* @returns Object with result XML and any errors
|
||||||
*/
|
*/
|
||||||
function sameCharFrequency(a: string, b: string): boolean {
|
export function applyDiagramOperations(
|
||||||
const trimmedA = a.trim()
|
|
||||||
const trimmedB = b.trim()
|
|
||||||
if (trimmedA.length !== trimmedB.length) return false
|
|
||||||
|
|
||||||
const dictA = charCountDict(trimmedA)
|
|
||||||
const dictB = charCountDict(trimmedB)
|
|
||||||
|
|
||||||
if (dictA.size !== dictB.size) return false
|
|
||||||
|
|
||||||
for (const [char, count] of dictA) {
|
|
||||||
if (dictB.get(char) !== count) return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Replace specific parts of XML content using search and replace pairs
|
|
||||||
* @param xmlContent - The original XML string
|
|
||||||
* @param searchReplacePairs - Array of {search: string, replace: string} objects
|
|
||||||
* @returns The updated XML string with replacements applied
|
|
||||||
*/
|
|
||||||
export function replaceXMLParts(
|
|
||||||
xmlContent: string,
|
xmlContent: string,
|
||||||
searchReplacePairs: Array<{ search: string; replace: string }>,
|
operations: DiagramOperation[],
|
||||||
): string {
|
): ApplyOperationsResult {
|
||||||
// Format the XML first to ensure consistent line breaks
|
const errors: OperationError[] = []
|
||||||
let result = formatXML(xmlContent)
|
|
||||||
|
|
||||||
for (const { search, replace } of searchReplacePairs) {
|
// Parse the XML
|
||||||
// Also format the search content for consistency
|
const parser = new DOMParser()
|
||||||
const formattedSearch = formatXML(search)
|
const doc = parser.parseFromString(xmlContent, "text/xml")
|
||||||
const searchLines = formattedSearch.split("\n")
|
|
||||||
|
|
||||||
// Split into lines for exact line matching
|
// Check for parse errors
|
||||||
const resultLines = result.split("\n")
|
const parseError = doc.querySelector("parsererror")
|
||||||
|
if (parseError) {
|
||||||
// Remove trailing empty line if exists (from the trailing \n in search content)
|
return {
|
||||||
if (searchLines[searchLines.length - 1] === "") {
|
result: xmlContent,
|
||||||
searchLines.pop()
|
errors: [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cellId: "",
|
||||||
|
message: `XML parse error: ${parseError.textContent}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always search from the beginning - pairs may not be in document order
|
|
||||||
const startLineNum = 0
|
|
||||||
|
|
||||||
// Try to find match using multiple strategies
|
|
||||||
let matchFound = false
|
|
||||||
let matchStartLine = -1
|
|
||||||
let matchEndLine = -1
|
|
||||||
|
|
||||||
// First try: exact match
|
|
||||||
for (
|
|
||||||
let i = startLineNum;
|
|
||||||
i <= resultLines.length - searchLines.length;
|
|
||||||
i++
|
|
||||||
) {
|
|
||||||
let matches = true
|
|
||||||
|
|
||||||
for (let j = 0; j < searchLines.length; j++) {
|
|
||||||
if (resultLines[i + j] !== searchLines[j]) {
|
|
||||||
matches = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches) {
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = i + searchLines.length
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Second try: line-trimmed match (fallback)
|
|
||||||
if (!matchFound) {
|
|
||||||
for (
|
|
||||||
let i = startLineNum;
|
|
||||||
i <= resultLines.length - searchLines.length;
|
|
||||||
i++
|
|
||||||
) {
|
|
||||||
let matches = true
|
|
||||||
|
|
||||||
for (let j = 0; j < searchLines.length; j++) {
|
|
||||||
const originalTrimmed = resultLines[i + j].trim()
|
|
||||||
const searchTrimmed = searchLines[j].trim()
|
|
||||||
|
|
||||||
if (originalTrimmed !== searchTrimmed) {
|
|
||||||
matches = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches) {
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = i + searchLines.length
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Third try: substring match as last resort (for single-line XML)
|
|
||||||
if (!matchFound) {
|
|
||||||
// Try to find as a substring in the entire content
|
|
||||||
const searchStr = search.trim()
|
|
||||||
const resultStr = result
|
|
||||||
const index = resultStr.indexOf(searchStr)
|
|
||||||
|
|
||||||
if (index !== -1) {
|
|
||||||
// Found as substring - replace it
|
|
||||||
result =
|
|
||||||
resultStr.substring(0, index) +
|
|
||||||
replace.trim() +
|
|
||||||
resultStr.substring(index + searchStr.length)
|
|
||||||
// Re-format after substring replacement
|
|
||||||
result = formatXML(result)
|
|
||||||
continue // Skip the line-based replacement below
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fourth try: character frequency match (attribute-order agnostic)
|
|
||||||
// This handles cases where the model generates XML with different attribute order
|
|
||||||
if (!matchFound) {
|
|
||||||
for (
|
|
||||||
let i = startLineNum;
|
|
||||||
i <= resultLines.length - searchLines.length;
|
|
||||||
i++
|
|
||||||
) {
|
|
||||||
let matches = true
|
|
||||||
|
|
||||||
for (let j = 0; j < searchLines.length; j++) {
|
|
||||||
if (
|
|
||||||
!sameCharFrequency(resultLines[i + j], searchLines[j])
|
|
||||||
) {
|
|
||||||
matches = false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches) {
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = i + searchLines.length
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fifth try: Match by mxCell id attribute
|
|
||||||
// Extract id from search pattern and find the element with that id
|
|
||||||
if (!matchFound) {
|
|
||||||
const idMatch = search.match(/id="([^"]+)"/)
|
|
||||||
if (idMatch) {
|
|
||||||
const searchId = idMatch[1]
|
|
||||||
// Find lines that contain this id
|
|
||||||
for (let i = startLineNum; i < resultLines.length; i++) {
|
|
||||||
if (resultLines[i].includes(`id="${searchId}"`)) {
|
|
||||||
// Found the element with matching id
|
|
||||||
// Now find the extent of this element (it might span multiple lines)
|
|
||||||
let endLine = i + 1
|
|
||||||
const line = resultLines[i].trim()
|
|
||||||
|
|
||||||
// Check if it's a self-closing tag or has children
|
|
||||||
if (!line.endsWith("/>")) {
|
|
||||||
// Find the closing tag or the end of the mxCell block
|
|
||||||
let depth = 1
|
|
||||||
while (endLine < resultLines.length && depth > 0) {
|
|
||||||
const currentLine = resultLines[endLine].trim()
|
|
||||||
if (
|
|
||||||
currentLine.startsWith("<") &&
|
|
||||||
!currentLine.startsWith("</") &&
|
|
||||||
!currentLine.endsWith("/>")
|
|
||||||
) {
|
|
||||||
depth++
|
|
||||||
} else if (currentLine.startsWith("</")) {
|
|
||||||
depth--
|
|
||||||
}
|
|
||||||
endLine++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = endLine
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sixth try: Match by value attribute (label text)
|
|
||||||
// Extract value from search pattern and find elements with that value
|
|
||||||
if (!matchFound) {
|
|
||||||
const valueMatch = search.match(/value="([^"]*)"/)
|
|
||||||
if (valueMatch) {
|
|
||||||
const searchValue = valueMatch[0] // Use full match like value="text"
|
|
||||||
for (let i = startLineNum; i < resultLines.length; i++) {
|
|
||||||
if (resultLines[i].includes(searchValue)) {
|
|
||||||
// Found element with matching value
|
|
||||||
let endLine = i + 1
|
|
||||||
const line = resultLines[i].trim()
|
|
||||||
|
|
||||||
if (!line.endsWith("/>")) {
|
|
||||||
let depth = 1
|
|
||||||
while (endLine < resultLines.length && depth > 0) {
|
|
||||||
const currentLine = resultLines[endLine].trim()
|
|
||||||
if (
|
|
||||||
currentLine.startsWith("<") &&
|
|
||||||
!currentLine.startsWith("</") &&
|
|
||||||
!currentLine.endsWith("/>")
|
|
||||||
) {
|
|
||||||
depth++
|
|
||||||
} else if (currentLine.startsWith("</")) {
|
|
||||||
depth--
|
|
||||||
}
|
|
||||||
endLine++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = endLine
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seventh try: Normalized whitespace match
|
|
||||||
// Collapse all whitespace and compare
|
|
||||||
if (!matchFound) {
|
|
||||||
const normalizeWs = (s: string) => s.replace(/\s+/g, " ").trim()
|
|
||||||
const normalizedSearch = normalizeWs(search)
|
|
||||||
|
|
||||||
for (
|
|
||||||
let i = startLineNum;
|
|
||||||
i <= resultLines.length - searchLines.length;
|
|
||||||
i++
|
|
||||||
) {
|
|
||||||
// Build a normalized version of the candidate lines
|
|
||||||
const candidateLines = resultLines.slice(
|
|
||||||
i,
|
|
||||||
i + searchLines.length,
|
|
||||||
)
|
|
||||||
const normalizedCandidate = normalizeWs(
|
|
||||||
candidateLines.join(" "),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (normalizedCandidate === normalizedSearch) {
|
|
||||||
matchStartLine = i
|
|
||||||
matchEndLine = i + searchLines.length
|
|
||||||
matchFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!matchFound) {
|
|
||||||
throw new Error(
|
|
||||||
`Search pattern not found in the diagram. The pattern may not exist in the current structure.`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace the matched lines
|
|
||||||
const replaceLines = replace.split("\n")
|
|
||||||
|
|
||||||
// Remove trailing empty line if exists
|
|
||||||
if (replaceLines[replaceLines.length - 1] === "") {
|
|
||||||
replaceLines.pop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perform the replacement
|
|
||||||
const newResultLines = [
|
|
||||||
...resultLines.slice(0, matchStartLine),
|
|
||||||
...replaceLines,
|
|
||||||
...resultLines.slice(matchEndLine),
|
|
||||||
]
|
|
||||||
|
|
||||||
result = newResultLines.join("\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
// Find the root element (inside mxGraphModel)
|
||||||
|
const root = doc.querySelector("root")
|
||||||
|
if (!root) {
|
||||||
|
return {
|
||||||
|
result: xmlContent,
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cellId: "",
|
||||||
|
message: "Could not find <root> element in XML",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build a map of cell IDs to elements
|
||||||
|
const cellMap = new Map<string, Element>()
|
||||||
|
root.querySelectorAll("mxCell").forEach((cell) => {
|
||||||
|
const id = cell.getAttribute("id")
|
||||||
|
if (id) cellMap.set(id, cell)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Process each operation
|
||||||
|
for (const op of operations) {
|
||||||
|
if (op.type === "update") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml is required for update operation",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the new XML
|
||||||
|
const newDoc = parser.parseFromString(
|
||||||
|
`<wrapper>${op.new_xml}</wrapper>`,
|
||||||
|
"text/xml",
|
||||||
|
)
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml must contain an mxCell element",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate ID matches
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import and replace the node
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
existingCell.parentNode?.replaceChild(importedNode, existingCell)
|
||||||
|
|
||||||
|
// Update the map with the new element
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "add") {
|
||||||
|
// Check if ID already exists
|
||||||
|
if (cellMap.has(op.cell_id)) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" already exists`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml is required for add operation",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the new XML
|
||||||
|
const newDoc = parser.parseFromString(
|
||||||
|
`<wrapper>${op.new_xml}</wrapper>`,
|
||||||
|
"text/xml",
|
||||||
|
)
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml must contain an mxCell element",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate ID matches
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import and append the node
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
root.appendChild(importedNode)
|
||||||
|
|
||||||
|
// Add to map
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "delete") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "delete",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for edges referencing this cell (warning only, still delete)
|
||||||
|
const referencingEdges = root.querySelectorAll(
|
||||||
|
`mxCell[source="${op.cell_id}"], mxCell[target="${op.cell_id}"]`,
|
||||||
|
)
|
||||||
|
if (referencingEdges.length > 0) {
|
||||||
|
const edgeIds = Array.from(referencingEdges)
|
||||||
|
.map((e) => e.getAttribute("id"))
|
||||||
|
.join(", ")
|
||||||
|
console.warn(
|
||||||
|
`[applyDiagramOperations] Deleting cell "${op.cell_id}" which is referenced by edges: ${edgeIds}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the node
|
||||||
|
existingCell.parentNode?.removeChild(existingCell)
|
||||||
|
cellMap.delete(op.cell_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize back to string
|
||||||
|
const serializer = new XMLSerializer()
|
||||||
|
const result = serializer.serializeToString(doc)
|
||||||
|
|
||||||
|
return { result, errors }
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -823,8 +743,6 @@ function checkNestedMxCells(xml: string): string | null {
|
|||||||
* @returns null if valid, error message string if invalid
|
* @returns null if valid, error message string if invalid
|
||||||
*/
|
*/
|
||||||
export function validateMxCellStructure(xml: string): string | null {
|
export function validateMxCellStructure(xml: string): string | null {
|
||||||
console.time("perf:validateMxCellStructure")
|
|
||||||
console.log(`perf:validateMxCellStructure XML size: ${xml.length} bytes`)
|
|
||||||
// Size check for performance
|
// Size check for performance
|
||||||
if (xml.length > MAX_XML_SIZE) {
|
if (xml.length > MAX_XML_SIZE) {
|
||||||
console.warn(
|
console.warn(
|
||||||
@@ -834,18 +752,10 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
|
|
||||||
// 0. First use DOM parser to catch syntax errors (most accurate)
|
// 0. First use DOM parser to catch syntax errors (most accurate)
|
||||||
try {
|
try {
|
||||||
console.time("perf:validate-DOMParser")
|
|
||||||
const parser = new DOMParser()
|
const parser = new DOMParser()
|
||||||
const doc = parser.parseFromString(xml, "text/xml")
|
const doc = parser.parseFromString(xml, "text/xml")
|
||||||
console.timeEnd("perf:validate-DOMParser")
|
|
||||||
const parseError = doc.querySelector("parsererror")
|
const parseError = doc.querySelector("parsererror")
|
||||||
if (parseError) {
|
if (parseError) {
|
||||||
const actualError = parseError.textContent || "Unknown parse error"
|
|
||||||
console.log(
|
|
||||||
"[validateMxCellStructure] DOMParser error:",
|
|
||||||
actualError,
|
|
||||||
)
|
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return `Invalid XML: The XML contains syntax errors (likely unescaped special characters like <, >, & in attribute values). Please escape special characters: use < for <, > for >, & for &, " for ". Regenerate the diagram with properly escaped values.`
|
return `Invalid XML: The XML contains syntax errors (likely unescaped special characters like <, >, & in attribute values). Please escape special characters: use < for <, > for >, & for &, " for ". Regenerate the diagram with properly escaped values.`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -854,7 +764,6 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
for (const cell of allCells) {
|
for (const cell of allCells) {
|
||||||
if (cell.parentElement?.tagName === "mxCell") {
|
if (cell.parentElement?.tagName === "mxCell") {
|
||||||
const id = cell.getAttribute("id") || "unknown"
|
const id = cell.getAttribute("id") || "unknown"
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return `Invalid XML: Found nested mxCell (id="${id}"). Cells should be siblings, not nested inside other mxCell elements.`
|
return `Invalid XML: Found nested mxCell (id="${id}"). Cells should be siblings, not nested inside other mxCell elements.`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -868,16 +777,12 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
|
|
||||||
// 1. Check for CDATA wrapper (invalid at document root)
|
// 1. Check for CDATA wrapper (invalid at document root)
|
||||||
if (/^\s*<!\[CDATA\[/.test(xml)) {
|
if (/^\s*<!\[CDATA\[/.test(xml)) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return "Invalid XML: XML is wrapped in CDATA section - remove <![CDATA[ from start and ]]> from end"
|
return "Invalid XML: XML is wrapped in CDATA section - remove <![CDATA[ from start and ]]> from end"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Check for duplicate structural attributes
|
// 2. Check for duplicate structural attributes
|
||||||
console.time("perf:checkDuplicateAttributes")
|
|
||||||
const dupAttrError = checkDuplicateAttributes(xml)
|
const dupAttrError = checkDuplicateAttributes(xml)
|
||||||
console.timeEnd("perf:checkDuplicateAttributes")
|
|
||||||
if (dupAttrError) {
|
if (dupAttrError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return dupAttrError
|
return dupAttrError
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -887,33 +792,25 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
while ((attrValMatch = attrValuePattern.exec(xml)) !== null) {
|
while ((attrValMatch = attrValuePattern.exec(xml)) !== null) {
|
||||||
const value = attrValMatch[1]
|
const value = attrValMatch[1]
|
||||||
if (/</.test(value) && !/</.test(value)) {
|
if (/</.test(value) && !/</.test(value)) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return "Invalid XML: Unescaped < character in attribute values. Replace < with <"
|
return "Invalid XML: Unescaped < character in attribute values. Replace < with <"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Check for duplicate IDs
|
// 4. Check for duplicate IDs
|
||||||
console.time("perf:checkDuplicateIds")
|
|
||||||
const dupIdError = checkDuplicateIds(xml)
|
const dupIdError = checkDuplicateIds(xml)
|
||||||
console.timeEnd("perf:checkDuplicateIds")
|
|
||||||
if (dupIdError) {
|
if (dupIdError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return dupIdError
|
return dupIdError
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Check for tag mismatches
|
// 5. Check for tag mismatches
|
||||||
console.time("perf:checkTagMismatches")
|
|
||||||
const tagMismatchError = checkTagMismatches(xml)
|
const tagMismatchError = checkTagMismatches(xml)
|
||||||
console.timeEnd("perf:checkTagMismatches")
|
|
||||||
if (tagMismatchError) {
|
if (tagMismatchError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return tagMismatchError
|
return tagMismatchError
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. Check invalid character references
|
// 6. Check invalid character references
|
||||||
const charRefError = checkCharacterReferences(xml)
|
const charRefError = checkCharacterReferences(xml)
|
||||||
if (charRefError) {
|
if (charRefError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return charRefError
|
return charRefError
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,7 +819,6 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
let commentMatch
|
let commentMatch
|
||||||
while ((commentMatch = commentPattern.exec(xml)) !== null) {
|
while ((commentMatch = commentPattern.exec(xml)) !== null) {
|
||||||
if (/--/.test(commentMatch[1])) {
|
if (/--/.test(commentMatch[1])) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return "Invalid XML: Comment contains -- (double hyphen) which is not allowed"
|
return "Invalid XML: Comment contains -- (double hyphen) which is not allowed"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -930,24 +826,20 @@ export function validateMxCellStructure(xml: string): string | null {
|
|||||||
// 8. Check for unescaped entity references and invalid entity names
|
// 8. Check for unescaped entity references and invalid entity names
|
||||||
const entityError = checkEntityReferences(xml)
|
const entityError = checkEntityReferences(xml)
|
||||||
if (entityError) {
|
if (entityError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return entityError
|
return entityError
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. Check for empty id attributes on mxCell
|
// 9. Check for empty id attributes on mxCell
|
||||||
if (/<mxCell[^>]*\sid\s*=\s*["']\s*["'][^>]*>/g.test(xml)) {
|
if (/<mxCell[^>]*\sid\s*=\s*["']\s*["'][^>]*>/g.test(xml)) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return "Invalid XML: Found mxCell element(s) with empty id attribute"
|
return "Invalid XML: Found mxCell element(s) with empty id attribute"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 10. Check for nested mxCell tags
|
// 10. Check for nested mxCell tags
|
||||||
const nestedCellError = checkNestedMxCells(xml)
|
const nestedCellError = checkNestedMxCells(xml)
|
||||||
if (nestedCellError) {
|
if (nestedCellError) {
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return nestedCellError
|
return nestedCellError
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeEnd("perf:validateMxCellStructure")
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "next-ai-draw-io",
|
"name": "next-ai-draw-io",
|
||||||
"version": "0.4.1",
|
"version": "0.4.3",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
162
packages/mcp-server/README.md
Normal file
162
packages/mcp-server/README.md
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
# Next AI Draw.io MCP Server
|
||||||
|
|
||||||
|
MCP (Model Context Protocol) server that enables AI agents like Claude Desktop and Cursor to generate and edit draw.io diagrams with **real-time browser preview**.
|
||||||
|
|
||||||
|
**Self-contained** - includes an embedded HTTP server, no external dependencies required.
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Claude Desktop
|
||||||
|
|
||||||
|
Add to your Claude Desktop config (`~/Library/Application Support/Claude/claude_desktop_config.json` on macOS):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### VS Code
|
||||||
|
|
||||||
|
Add to your VS Code settings (`.vscode/mcp.json` in workspace or user settings):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cursor
|
||||||
|
|
||||||
|
Add to Cursor MCP config (`~/.cursor/mcp.json`):
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Claude Code CLI
|
||||||
|
|
||||||
|
```bash
|
||||||
|
claude mcp add drawio -- npx @next-ai-drawio/mcp-server@latest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Other MCP Clients
|
||||||
|
|
||||||
|
Use the standard MCP configuration with:
|
||||||
|
- **Command**: `npx`
|
||||||
|
- **Args**: `["@next-ai-drawio/mcp-server@latest"]`
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
1. Restart your MCP client after updating config
|
||||||
|
2. Ask the AI to create a diagram:
|
||||||
|
> "Create a flowchart showing user authentication with login, MFA, and session management"
|
||||||
|
3. The diagram appears in your browser in real-time!
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Real-time Preview**: Diagrams appear and update in your browser as the AI creates them
|
||||||
|
- **Natural Language**: Describe diagrams in plain text - flowcharts, architecture diagrams, etc.
|
||||||
|
- **Edit Support**: Modify existing diagrams with natural language instructions
|
||||||
|
- **Export**: Save diagrams as `.drawio` files
|
||||||
|
- **Self-contained**: Embedded server, works offline (except draw.io UI which loads from embed.diagrams.net)
|
||||||
|
|
||||||
|
## Available Tools
|
||||||
|
|
||||||
|
| Tool | Description |
|
||||||
|
|------|-------------|
|
||||||
|
| `start_session` | Opens browser with real-time diagram preview |
|
||||||
|
| `display_diagram` | Create a new diagram from XML |
|
||||||
|
| `edit_diagram` | Edit diagram by ID-based operations (update/add/delete cells) |
|
||||||
|
| `get_diagram` | Get the current diagram XML |
|
||||||
|
| `export_diagram` | Save diagram to a `.drawio` file |
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
```
|
||||||
|
┌─────────────────┐ stdio ┌─────────────────┐
|
||||||
|
│ Claude Desktop │ <───────────> │ MCP Server │
|
||||||
|
│ (AI Agent) │ │ (this package) │
|
||||||
|
└─────────────────┘ └────────┬────────┘
|
||||||
|
│
|
||||||
|
┌────────▼────────┐
|
||||||
|
│ Embedded HTTP │
|
||||||
|
│ Server (:6002) │
|
||||||
|
└────────┬────────┘
|
||||||
|
│
|
||||||
|
┌────────▼────────┐
|
||||||
|
│ User's Browser │
|
||||||
|
│ (draw.io embed) │
|
||||||
|
└─────────────────┘
|
||||||
|
```
|
||||||
|
|
||||||
|
1. **MCP Server** receives tool calls from Claude via stdio
|
||||||
|
2. **Embedded HTTP Server** serves the draw.io UI and handles state
|
||||||
|
3. **Browser** shows real-time diagram updates via polling
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
| Variable | Default | Description |
|
||||||
|
|----------|---------|-------------|
|
||||||
|
| `PORT` | `6002` | Port for the embedded HTTP server |
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Port already in use
|
||||||
|
|
||||||
|
If port 6002 is in use, the server will automatically try the next available port (up to 6020).
|
||||||
|
|
||||||
|
Or set a custom port:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"drawio": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["@next-ai-drawio/mcp-server@latest"],
|
||||||
|
"env": { "PORT": "6003" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### "No active session"
|
||||||
|
|
||||||
|
Call `start_session` first to open the browser window.
|
||||||
|
|
||||||
|
### Browser not updating
|
||||||
|
|
||||||
|
Check that the browser URL has the `?mcp=` query parameter. The MCP session ID connects the browser to the server.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Apache-2.0
|
||||||
2044
packages/mcp-server/package-lock.json
generated
Normal file
2044
packages/mcp-server/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
55
packages/mcp-server/package.json
Normal file
55
packages/mcp-server/package.json
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
{
|
||||||
|
"name": "@next-ai-drawio/mcp-server",
|
||||||
|
"version": "0.1.2",
|
||||||
|
"description": "MCP server for Next AI Draw.io - AI-powered diagram generation with real-time browser preview",
|
||||||
|
"type": "module",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"bin": {
|
||||||
|
"next-ai-drawio-mcp": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"dev": "tsx watch src/index.ts",
|
||||||
|
"start": "node dist/index.js",
|
||||||
|
"prepublishOnly": "npm run build"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"mcp",
|
||||||
|
"drawio",
|
||||||
|
"diagram",
|
||||||
|
"ai",
|
||||||
|
"claude",
|
||||||
|
"model-context-protocol"
|
||||||
|
],
|
||||||
|
"author": "Biki-dev",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/Biki-dev/next-ai-draw-io",
|
||||||
|
"directory": "packages/mcp-server"
|
||||||
|
},
|
||||||
|
"homepage": "https://next-ai-drawio.jiang.jp",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/Biki-dev/next-ai-draw-io/issues"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@modelcontextprotocol/sdk": "^1.0.4",
|
||||||
|
"linkedom": "^0.18.0",
|
||||||
|
"open": "^10.1.0",
|
||||||
|
"zod": "^3.24.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^20",
|
||||||
|
"tsx": "^4.19.0",
|
||||||
|
"typescript": "^5"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
]
|
||||||
|
}
|
||||||
219
packages/mcp-server/src/diagram-operations.ts
Normal file
219
packages/mcp-server/src/diagram-operations.ts
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
/**
|
||||||
|
* ID-based diagram operations
|
||||||
|
* Copied from lib/utils.ts to avoid cross-package imports
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface DiagramOperation {
|
||||||
|
type: "update" | "add" | "delete"
|
||||||
|
cell_id: string
|
||||||
|
new_xml?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OperationError {
|
||||||
|
type: "update" | "add" | "delete"
|
||||||
|
cellId: string
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApplyOperationsResult {
|
||||||
|
result: string
|
||||||
|
errors: OperationError[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply diagram operations (update/add/delete) using ID-based lookup.
|
||||||
|
* This replaces the text-matching approach with direct DOM manipulation.
|
||||||
|
*
|
||||||
|
* @param xmlContent - The full mxfile XML content
|
||||||
|
* @param operations - Array of operations to apply
|
||||||
|
* @returns Object with result XML and any errors
|
||||||
|
*/
|
||||||
|
export function applyDiagramOperations(
|
||||||
|
xmlContent: string,
|
||||||
|
operations: DiagramOperation[],
|
||||||
|
): ApplyOperationsResult {
|
||||||
|
const errors: OperationError[] = []
|
||||||
|
|
||||||
|
// Parse the XML
|
||||||
|
const parser = new DOMParser()
|
||||||
|
const doc = parser.parseFromString(xmlContent, "text/xml")
|
||||||
|
|
||||||
|
// Check for parse errors
|
||||||
|
const parseError = doc.querySelector("parsererror")
|
||||||
|
if (parseError) {
|
||||||
|
return {
|
||||||
|
result: xmlContent,
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cellId: "",
|
||||||
|
message: `XML parse error: ${parseError.textContent}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the root element (inside mxGraphModel)
|
||||||
|
const root = doc.querySelector("root")
|
||||||
|
if (!root) {
|
||||||
|
return {
|
||||||
|
result: xmlContent,
|
||||||
|
errors: [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cellId: "",
|
||||||
|
message: "Could not find <root> element in XML",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build a map of cell IDs to elements
|
||||||
|
const cellMap = new Map<string, Element>()
|
||||||
|
root.querySelectorAll("mxCell").forEach((cell) => {
|
||||||
|
const id = cell.getAttribute("id")
|
||||||
|
if (id) cellMap.set(id, cell)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Process each operation
|
||||||
|
for (const op of operations) {
|
||||||
|
if (op.type === "update") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml is required for update operation",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the new XML
|
||||||
|
const newDoc = parser.parseFromString(
|
||||||
|
`<wrapper>${op.new_xml}</wrapper>`,
|
||||||
|
"text/xml",
|
||||||
|
)
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml must contain an mxCell element",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate ID matches
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({
|
||||||
|
type: "update",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import and replace the node
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
existingCell.parentNode?.replaceChild(importedNode, existingCell)
|
||||||
|
|
||||||
|
// Update the map with the new element
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "add") {
|
||||||
|
// Check if ID already exists
|
||||||
|
if (cellMap.has(op.cell_id)) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" already exists`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml is required for add operation",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the new XML
|
||||||
|
const newDoc = parser.parseFromString(
|
||||||
|
`<wrapper>${op.new_xml}</wrapper>`,
|
||||||
|
"text/xml",
|
||||||
|
)
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: "new_xml must contain an mxCell element",
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate ID matches
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({
|
||||||
|
type: "add",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import and append the node
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
root.appendChild(importedNode)
|
||||||
|
|
||||||
|
// Add to map
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "delete") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({
|
||||||
|
type: "delete",
|
||||||
|
cellId: op.cell_id,
|
||||||
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for edges referencing this cell (warning only, still delete)
|
||||||
|
const referencingEdges = root.querySelectorAll(
|
||||||
|
`mxCell[source="${op.cell_id}"], mxCell[target="${op.cell_id}"]`,
|
||||||
|
)
|
||||||
|
if (referencingEdges.length > 0) {
|
||||||
|
const edgeIds = Array.from(referencingEdges)
|
||||||
|
.map((e) => e.getAttribute("id"))
|
||||||
|
.join(", ")
|
||||||
|
console.warn(
|
||||||
|
`[applyDiagramOperations] Deleting cell "${op.cell_id}" which is referenced by edges: ${edgeIds}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the node
|
||||||
|
existingCell.parentNode?.removeChild(existingCell)
|
||||||
|
cellMap.delete(op.cell_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize back to string
|
||||||
|
const serializer = new XMLSerializer()
|
||||||
|
const result = serializer.serializeToString(doc)
|
||||||
|
|
||||||
|
return { result, errors }
|
||||||
|
}
|
||||||
384
packages/mcp-server/src/http-server.ts
Normal file
384
packages/mcp-server/src/http-server.ts
Normal file
@@ -0,0 +1,384 @@
|
|||||||
|
/**
|
||||||
|
* Embedded HTTP Server for MCP
|
||||||
|
*
|
||||||
|
* Serves a static HTML page with draw.io embed and handles state sync.
|
||||||
|
* This eliminates the need for an external Next.js app.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import http from "node:http"
|
||||||
|
import { log } from "./logger.js"
|
||||||
|
|
||||||
|
interface SessionState {
|
||||||
|
xml: string
|
||||||
|
version: number
|
||||||
|
lastUpdated: Date
|
||||||
|
}
|
||||||
|
|
||||||
|
// In-memory state store (shared with MCP server in same process)
|
||||||
|
export const stateStore = new Map<string, SessionState>()
|
||||||
|
|
||||||
|
let server: http.Server | null = null
|
||||||
|
let serverPort: number = 6002
|
||||||
|
const MAX_PORT = 6020 // Don't retry beyond this port
|
||||||
|
const SESSION_TTL = 60 * 60 * 1000 // 1 hour
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get state for a session
|
||||||
|
*/
|
||||||
|
export function getState(sessionId: string): SessionState | undefined {
|
||||||
|
return stateStore.get(sessionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set state for a session
|
||||||
|
*/
|
||||||
|
export function setState(sessionId: string, xml: string): number {
|
||||||
|
const existing = stateStore.get(sessionId)
|
||||||
|
const newVersion = (existing?.version || 0) + 1
|
||||||
|
|
||||||
|
stateStore.set(sessionId, {
|
||||||
|
xml,
|
||||||
|
version: newVersion,
|
||||||
|
lastUpdated: new Date(),
|
||||||
|
})
|
||||||
|
|
||||||
|
log.debug(`State updated: session=${sessionId}, version=${newVersion}`)
|
||||||
|
return newVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the embedded HTTP server
|
||||||
|
*/
|
||||||
|
export function startHttpServer(port: number = 6002): Promise<number> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (server) {
|
||||||
|
resolve(serverPort)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
serverPort = port
|
||||||
|
server = http.createServer(handleRequest)
|
||||||
|
|
||||||
|
server.on("error", (err: NodeJS.ErrnoException) => {
|
||||||
|
if (err.code === "EADDRINUSE") {
|
||||||
|
if (port >= MAX_PORT) {
|
||||||
|
reject(
|
||||||
|
new Error(
|
||||||
|
`No available ports in range 6002-${MAX_PORT}`,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.info(`Port ${port} in use, trying ${port + 1}`)
|
||||||
|
server = null
|
||||||
|
startHttpServer(port + 1)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject)
|
||||||
|
} else {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server.listen(port, () => {
|
||||||
|
serverPort = port
|
||||||
|
log.info(`Embedded HTTP server running on http://localhost:${port}`)
|
||||||
|
resolve(port)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop the HTTP server
|
||||||
|
*/
|
||||||
|
export function stopHttpServer(): void {
|
||||||
|
if (server) {
|
||||||
|
server.close()
|
||||||
|
server = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up expired sessions
|
||||||
|
*/
|
||||||
|
function cleanupExpiredSessions(): void {
|
||||||
|
const now = Date.now()
|
||||||
|
for (const [sessionId, state] of stateStore) {
|
||||||
|
if (now - state.lastUpdated.getTime() > SESSION_TTL) {
|
||||||
|
stateStore.delete(sessionId)
|
||||||
|
log.info(`Cleaned up expired session: ${sessionId}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run cleanup every 5 minutes
|
||||||
|
setInterval(cleanupExpiredSessions, 5 * 60 * 1000)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current server port
|
||||||
|
*/
|
||||||
|
export function getServerPort(): number {
|
||||||
|
return serverPort
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle HTTP requests
|
||||||
|
*/
|
||||||
|
function handleRequest(
|
||||||
|
req: http.IncomingMessage,
|
||||||
|
res: http.ServerResponse,
|
||||||
|
): void {
|
||||||
|
const url = new URL(req.url || "/", `http://localhost:${serverPort}`)
|
||||||
|
|
||||||
|
// CORS headers for local development
|
||||||
|
res.setHeader("Access-Control-Allow-Origin", "*")
|
||||||
|
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
|
||||||
|
if (req.method === "OPTIONS") {
|
||||||
|
res.writeHead(204)
|
||||||
|
res.end()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route handling
|
||||||
|
if (url.pathname === "/" || url.pathname === "/index.html") {
|
||||||
|
serveHtml(req, res, url)
|
||||||
|
} else if (
|
||||||
|
url.pathname === "/api/state" ||
|
||||||
|
url.pathname === "/api/mcp/state"
|
||||||
|
) {
|
||||||
|
handleStateApi(req, res, url)
|
||||||
|
} else if (
|
||||||
|
url.pathname === "/api/health" ||
|
||||||
|
url.pathname === "/api/mcp/health"
|
||||||
|
) {
|
||||||
|
res.writeHead(200, { "Content-Type": "application/json" })
|
||||||
|
res.end(JSON.stringify({ status: "ok", mcp: true }))
|
||||||
|
} else {
|
||||||
|
res.writeHead(404)
|
||||||
|
res.end("Not Found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serve the HTML page with draw.io embed
|
||||||
|
*/
|
||||||
|
function serveHtml(
|
||||||
|
req: http.IncomingMessage,
|
||||||
|
res: http.ServerResponse,
|
||||||
|
url: URL,
|
||||||
|
): void {
|
||||||
|
const sessionId = url.searchParams.get("mcp") || ""
|
||||||
|
|
||||||
|
res.writeHead(200, { "Content-Type": "text/html" })
|
||||||
|
res.end(getHtmlPage(sessionId))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle state API requests
|
||||||
|
*/
|
||||||
|
function handleStateApi(
|
||||||
|
req: http.IncomingMessage,
|
||||||
|
res: http.ServerResponse,
|
||||||
|
url: URL,
|
||||||
|
): void {
|
||||||
|
if (req.method === "GET") {
|
||||||
|
const sessionId = url.searchParams.get("sessionId")
|
||||||
|
if (!sessionId) {
|
||||||
|
res.writeHead(400, { "Content-Type": "application/json" })
|
||||||
|
res.end(JSON.stringify({ error: "sessionId required" }))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = stateStore.get(sessionId)
|
||||||
|
res.writeHead(200, { "Content-Type": "application/json" })
|
||||||
|
res.end(
|
||||||
|
JSON.stringify({
|
||||||
|
xml: state?.xml || null,
|
||||||
|
version: state?.version || 0,
|
||||||
|
lastUpdated: state?.lastUpdated?.toISOString() || null,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else if (req.method === "POST") {
|
||||||
|
let body = ""
|
||||||
|
req.on("data", (chunk) => {
|
||||||
|
body += chunk
|
||||||
|
})
|
||||||
|
req.on("end", () => {
|
||||||
|
try {
|
||||||
|
const { sessionId, xml } = JSON.parse(body)
|
||||||
|
if (!sessionId) {
|
||||||
|
res.writeHead(400, { "Content-Type": "application/json" })
|
||||||
|
res.end(JSON.stringify({ error: "sessionId required" }))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const version = setState(sessionId, xml)
|
||||||
|
res.writeHead(200, { "Content-Type": "application/json" })
|
||||||
|
res.end(JSON.stringify({ success: true, version }))
|
||||||
|
} catch {
|
||||||
|
res.writeHead(400, { "Content-Type": "application/json" })
|
||||||
|
res.end(JSON.stringify({ error: "Invalid JSON" }))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
res.writeHead(405)
|
||||||
|
res.end("Method Not Allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the HTML page with draw.io embed
|
||||||
|
*/
|
||||||
|
function getHtmlPage(sessionId: string): string {
|
||||||
|
return `<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Draw.io MCP - ${sessionId || "No Session"}</title>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||||
|
html, body { width: 100%; height: 100%; overflow: hidden; }
|
||||||
|
#container { width: 100%; height: 100%; display: flex; flex-direction: column; }
|
||||||
|
#header {
|
||||||
|
padding: 8px 16px;
|
||||||
|
background: #1a1a2e;
|
||||||
|
color: #eee;
|
||||||
|
font-family: system-ui, sans-serif;
|
||||||
|
font-size: 14px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
#header .session { color: #888; font-size: 12px; }
|
||||||
|
#header .status { font-size: 12px; }
|
||||||
|
#header .status.connected { color: #4ade80; }
|
||||||
|
#header .status.disconnected { color: #f87171; }
|
||||||
|
#drawio { flex: 1; border: none; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container">
|
||||||
|
<div id="header">
|
||||||
|
<div>
|
||||||
|
<strong>Draw.io MCP</strong>
|
||||||
|
<span class="session">${sessionId ? `Session: ${sessionId}` : "No MCP session"}</span>
|
||||||
|
</div>
|
||||||
|
<div id="status" class="status disconnected">Connecting...</div>
|
||||||
|
</div>
|
||||||
|
<iframe id="drawio" src="https://embed.diagrams.net/?embed=1&proto=json&spin=1&libraries=1"></iframe>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const sessionId = "${sessionId}";
|
||||||
|
const iframe = document.getElementById('drawio');
|
||||||
|
const statusEl = document.getElementById('status');
|
||||||
|
|
||||||
|
let currentVersion = 0;
|
||||||
|
let isDrawioReady = false;
|
||||||
|
let pendingXml = null;
|
||||||
|
let lastLoadedXml = null;
|
||||||
|
|
||||||
|
// Listen for messages from draw.io
|
||||||
|
window.addEventListener('message', (event) => {
|
||||||
|
if (event.origin !== 'https://embed.diagrams.net') return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const msg = JSON.parse(event.data);
|
||||||
|
handleDrawioMessage(msg);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore non-JSON messages
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function handleDrawioMessage(msg) {
|
||||||
|
if (msg.event === 'init') {
|
||||||
|
isDrawioReady = true;
|
||||||
|
statusEl.textContent = 'Ready';
|
||||||
|
statusEl.className = 'status connected';
|
||||||
|
|
||||||
|
// Load pending XML if any
|
||||||
|
if (pendingXml) {
|
||||||
|
loadDiagram(pendingXml);
|
||||||
|
pendingXml = null;
|
||||||
|
}
|
||||||
|
} else if (msg.event === 'save') {
|
||||||
|
// User saved - push to state
|
||||||
|
if (msg.xml && msg.xml !== lastLoadedXml) {
|
||||||
|
pushState(msg.xml);
|
||||||
|
}
|
||||||
|
} else if (msg.event === 'export') {
|
||||||
|
// Export completed
|
||||||
|
if (msg.data) {
|
||||||
|
pushState(msg.data);
|
||||||
|
}
|
||||||
|
} else if (msg.event === 'autosave') {
|
||||||
|
// Autosave - push to state
|
||||||
|
if (msg.xml && msg.xml !== lastLoadedXml) {
|
||||||
|
pushState(msg.xml);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadDiagram(xml) {
|
||||||
|
if (!isDrawioReady) {
|
||||||
|
pendingXml = xml;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastLoadedXml = xml;
|
||||||
|
iframe.contentWindow.postMessage(JSON.stringify({
|
||||||
|
action: 'load',
|
||||||
|
xml: xml,
|
||||||
|
autosave: 1
|
||||||
|
}), '*');
|
||||||
|
}
|
||||||
|
|
||||||
|
async function pushState(xml) {
|
||||||
|
if (!sessionId) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/state', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ sessionId, xml })
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const result = await response.json();
|
||||||
|
currentVersion = result.version;
|
||||||
|
lastLoadedXml = xml;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to push state:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function pollState() {
|
||||||
|
if (!sessionId) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/state?sessionId=' + encodeURIComponent(sessionId));
|
||||||
|
if (!response.ok) return;
|
||||||
|
|
||||||
|
const state = await response.json();
|
||||||
|
|
||||||
|
if (state.version && state.version > currentVersion && state.xml) {
|
||||||
|
currentVersion = state.version;
|
||||||
|
loadDiagram(state.xml);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to poll state:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start polling if we have a session
|
||||||
|
if (sessionId) {
|
||||||
|
pollState();
|
||||||
|
setInterval(pollState, 2000);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
}
|
||||||
476
packages/mcp-server/src/index.ts
Normal file
476
packages/mcp-server/src/index.ts
Normal file
@@ -0,0 +1,476 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
/**
|
||||||
|
* MCP Server for Next AI Draw.io
|
||||||
|
*
|
||||||
|
* Enables AI agents (Claude Desktop, Cursor, etc.) to generate and edit
|
||||||
|
* draw.io diagrams with real-time browser preview.
|
||||||
|
*
|
||||||
|
* Uses an embedded HTTP server - no external dependencies required.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Setup DOM polyfill for Node.js (required for XML operations)
|
||||||
|
import { DOMParser } from "linkedom"
|
||||||
|
;(globalThis as any).DOMParser = DOMParser
|
||||||
|
|
||||||
|
// Create XMLSerializer polyfill using outerHTML
|
||||||
|
class XMLSerializerPolyfill {
|
||||||
|
serializeToString(node: any): string {
|
||||||
|
if (node.outerHTML !== undefined) {
|
||||||
|
return node.outerHTML
|
||||||
|
}
|
||||||
|
if (node.documentElement) {
|
||||||
|
return node.documentElement.outerHTML
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
;(globalThis as any).XMLSerializer = XMLSerializerPolyfill
|
||||||
|
|
||||||
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
||||||
|
import open from "open"
|
||||||
|
import { z } from "zod"
|
||||||
|
import {
|
||||||
|
applyDiagramOperations,
|
||||||
|
type DiagramOperation,
|
||||||
|
} from "./diagram-operations.js"
|
||||||
|
import {
|
||||||
|
getServerPort,
|
||||||
|
getState,
|
||||||
|
setState,
|
||||||
|
startHttpServer,
|
||||||
|
} from "./http-server.js"
|
||||||
|
import { log } from "./logger.js"
|
||||||
|
|
||||||
|
// Server configuration
|
||||||
|
const config = {
|
||||||
|
port: parseInt(process.env.PORT || "6002"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Session state (single session for simplicity)
|
||||||
|
let currentSession: {
|
||||||
|
id: string
|
||||||
|
xml: string
|
||||||
|
version: number
|
||||||
|
} | null = null
|
||||||
|
|
||||||
|
// Create MCP server
|
||||||
|
const server = new McpServer({
|
||||||
|
name: "next-ai-drawio",
|
||||||
|
version: "0.1.2",
|
||||||
|
})
|
||||||
|
|
||||||
|
// Register prompt with workflow guidance
|
||||||
|
server.prompt(
|
||||||
|
"diagram-workflow",
|
||||||
|
"Guidelines for creating and editing draw.io diagrams",
|
||||||
|
() => ({
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: {
|
||||||
|
type: "text",
|
||||||
|
text: `# Draw.io Diagram Workflow Guidelines
|
||||||
|
|
||||||
|
## Creating a New Diagram
|
||||||
|
1. Call start_session to open the browser preview
|
||||||
|
2. Use display_diagram with complete mxGraphModel XML to create a new diagram
|
||||||
|
|
||||||
|
## Adding Elements to Existing Diagram
|
||||||
|
1. Use edit_diagram with "add" operation
|
||||||
|
2. Provide a unique cell_id and complete mxCell XML
|
||||||
|
3. No need to call get_diagram first - the server fetches latest state automatically
|
||||||
|
|
||||||
|
## Modifying or Deleting Existing Elements
|
||||||
|
1. FIRST call get_diagram to see current cell IDs and structure
|
||||||
|
2. THEN call edit_diagram with "update" or "delete" operations
|
||||||
|
3. For update, provide the cell_id and complete new mxCell XML
|
||||||
|
|
||||||
|
## Important Notes
|
||||||
|
- display_diagram REPLACES the entire diagram - only use for new diagrams
|
||||||
|
- edit_diagram PRESERVES user's manual changes (fetches browser state first)
|
||||||
|
- Always use unique cell_ids when adding elements (e.g., "shape-1", "arrow-2")`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool: start_session
|
||||||
|
server.registerTool(
|
||||||
|
"start_session",
|
||||||
|
{
|
||||||
|
description:
|
||||||
|
"Start a new diagram session and open the browser for real-time preview. " +
|
||||||
|
"Starts an embedded server and opens a browser window with draw.io. " +
|
||||||
|
"The browser will show diagram updates as they happen.",
|
||||||
|
inputSchema: {},
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
// Start embedded HTTP server
|
||||||
|
const port = await startHttpServer(config.port)
|
||||||
|
|
||||||
|
// Create session
|
||||||
|
const sessionId = `mcp-${Date.now().toString(36)}-${Math.random().toString(36).substring(2, 8)}`
|
||||||
|
currentSession = {
|
||||||
|
id: sessionId,
|
||||||
|
xml: "",
|
||||||
|
version: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open browser
|
||||||
|
const browserUrl = `http://localhost:${port}?mcp=${sessionId}`
|
||||||
|
await open(browserUrl)
|
||||||
|
|
||||||
|
log.info(`Started session ${sessionId}, browser at ${browserUrl}`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Session started successfully!\n\nSession ID: ${sessionId}\nBrowser URL: ${browserUrl}\n\nThe browser will now show real-time diagram updates.`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
log.error("start_session failed:", message)
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Error: ${message}` }],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool: display_diagram
|
||||||
|
server.registerTool(
|
||||||
|
"display_diagram",
|
||||||
|
{
|
||||||
|
description:
|
||||||
|
"Display a NEW draw.io diagram from XML. REPLACES the entire diagram. " +
|
||||||
|
"Use this for creating new diagrams from scratch. " +
|
||||||
|
"To ADD elements to an existing diagram, use edit_diagram with 'add' operation instead. " +
|
||||||
|
"You should generate valid draw.io/mxGraph XML format.",
|
||||||
|
inputSchema: {
|
||||||
|
xml: z
|
||||||
|
.string()
|
||||||
|
.describe("The draw.io XML to display (mxGraphModel format)"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async ({ xml }) => {
|
||||||
|
try {
|
||||||
|
if (!currentSession) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No active session. Please call start_session first.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info(`Displaying diagram, ${xml.length} chars`)
|
||||||
|
|
||||||
|
// Update session state
|
||||||
|
currentSession.xml = xml
|
||||||
|
currentSession.version++
|
||||||
|
|
||||||
|
// Push to embedded server state
|
||||||
|
setState(currentSession.id, xml)
|
||||||
|
|
||||||
|
log.info(`Diagram displayed successfully`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Diagram displayed successfully!\n\nThe diagram is now visible in your browser.\n\nXML length: ${xml.length} characters`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
log.error("display_diagram failed:", message)
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Error: ${message}` }],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool: edit_diagram
|
||||||
|
server.registerTool(
|
||||||
|
"edit_diagram",
|
||||||
|
{
|
||||||
|
description:
|
||||||
|
"Edit the current diagram by ID-based operations (update/add/delete cells). " +
|
||||||
|
"ALWAYS fetches the latest state from browser first, so user's manual changes are preserved.\n\n" +
|
||||||
|
"IMPORTANT workflow:\n" +
|
||||||
|
"- For ADD operations: Can use directly - just provide new unique cell_id and new_xml.\n" +
|
||||||
|
"- For UPDATE/DELETE: Call get_diagram FIRST to see current cell IDs, then edit.\n\n" +
|
||||||
|
"Operations:\n" +
|
||||||
|
"- add: Add a new cell. Provide cell_id (new unique id) and new_xml.\n" +
|
||||||
|
"- update: Replace an existing cell by its id. Provide cell_id and complete new_xml.\n" +
|
||||||
|
"- delete: Remove a cell by its id. Only cell_id is needed.\n\n" +
|
||||||
|
"For add/update, new_xml must be a complete mxCell element including mxGeometry.",
|
||||||
|
inputSchema: {
|
||||||
|
operations: z
|
||||||
|
.array(
|
||||||
|
z.object({
|
||||||
|
type: z
|
||||||
|
.enum(["update", "add", "delete"])
|
||||||
|
.describe("Operation type"),
|
||||||
|
cell_id: z.string().describe("The id of the mxCell"),
|
||||||
|
new_xml: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.describe(
|
||||||
|
"Complete mxCell XML element (required for update/add)",
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.describe("Array of operations to apply"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async ({ operations }) => {
|
||||||
|
try {
|
||||||
|
if (!currentSession) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No active session. Please call start_session first.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch latest state from browser
|
||||||
|
const browserState = getState(currentSession.id)
|
||||||
|
if (browserState?.xml) {
|
||||||
|
currentSession.xml = browserState.xml
|
||||||
|
log.info("Fetched latest diagram state from browser")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentSession.xml) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No diagram to edit. Please create a diagram first with display_diagram.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info(`Editing diagram with ${operations.length} operation(s)`)
|
||||||
|
|
||||||
|
// Apply operations
|
||||||
|
const { result, errors } = applyDiagramOperations(
|
||||||
|
currentSession.xml,
|
||||||
|
operations as DiagramOperation[],
|
||||||
|
)
|
||||||
|
|
||||||
|
if (errors.length > 0) {
|
||||||
|
const errorMessages = errors
|
||||||
|
.map((e) => `${e.type} ${e.cellId}: ${e.message}`)
|
||||||
|
.join("\n")
|
||||||
|
log.warn(`Edit had ${errors.length} error(s): ${errorMessages}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update state
|
||||||
|
currentSession.xml = result
|
||||||
|
currentSession.version++
|
||||||
|
|
||||||
|
// Push to embedded server
|
||||||
|
setState(currentSession.id, result)
|
||||||
|
|
||||||
|
log.info(`Diagram edited successfully`)
|
||||||
|
|
||||||
|
const successMsg = `Diagram edited successfully!\n\nApplied ${operations.length} operation(s).`
|
||||||
|
const errorMsg =
|
||||||
|
errors.length > 0
|
||||||
|
? `\n\nWarnings:\n${errors.map((e) => `- ${e.type} ${e.cellId}: ${e.message}`).join("\n")}`
|
||||||
|
: ""
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: successMsg + errorMsg,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
log.error("edit_diagram failed:", message)
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Error: ${message}` }],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool: get_diagram
|
||||||
|
server.registerTool(
|
||||||
|
"get_diagram",
|
||||||
|
{
|
||||||
|
description:
|
||||||
|
"Get the current diagram XML (fetches latest from browser, including user's manual edits). " +
|
||||||
|
"Call this BEFORE edit_diagram if you need to update or delete existing elements, " +
|
||||||
|
"so you can see the current cell IDs and structure.",
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
try {
|
||||||
|
if (!currentSession) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No active session. Please call start_session first.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch latest state from browser
|
||||||
|
const browserState = getState(currentSession.id)
|
||||||
|
if (browserState?.xml) {
|
||||||
|
currentSession.xml = browserState.xml
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentSession.xml) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "No diagram exists yet. Use display_diagram to create one.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Current diagram XML:\n\n${currentSession.xml}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
log.error("get_diagram failed:", message)
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Error: ${message}` }],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tool: export_diagram
|
||||||
|
server.registerTool(
|
||||||
|
"export_diagram",
|
||||||
|
{
|
||||||
|
description: "Export the current diagram to a .drawio file.",
|
||||||
|
inputSchema: {
|
||||||
|
path: z
|
||||||
|
.string()
|
||||||
|
.describe(
|
||||||
|
"File path to save the diagram (e.g., ./diagram.drawio)",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async ({ path }) => {
|
||||||
|
try {
|
||||||
|
if (!currentSession) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No active session. Please call start_session first.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch latest state
|
||||||
|
const browserState = getState(currentSession.id)
|
||||||
|
if (browserState?.xml) {
|
||||||
|
currentSession.xml = browserState.xml
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentSession.xml) {
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: "Error: No diagram to export. Please create a diagram first.",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fs = await import("node:fs/promises")
|
||||||
|
const nodePath = await import("node:path")
|
||||||
|
|
||||||
|
let filePath = path
|
||||||
|
if (!filePath.endsWith(".drawio")) {
|
||||||
|
filePath = `${filePath}.drawio`
|
||||||
|
}
|
||||||
|
|
||||||
|
const absolutePath = nodePath.resolve(filePath)
|
||||||
|
await fs.writeFile(absolutePath, currentSession.xml, "utf-8")
|
||||||
|
|
||||||
|
log.info(`Diagram exported to ${absolutePath}`)
|
||||||
|
|
||||||
|
return {
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `Diagram exported successfully!\n\nFile: ${absolutePath}\nSize: ${currentSession.xml.length} characters`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const message =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
log.error("export_diagram failed:", message)
|
||||||
|
return {
|
||||||
|
content: [{ type: "text", text: `Error: ${message}` }],
|
||||||
|
isError: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Start the MCP server
|
||||||
|
async function main() {
|
||||||
|
log.info("Starting MCP server for Next AI Draw.io (embedded mode)...")
|
||||||
|
|
||||||
|
const transport = new StdioServerTransport()
|
||||||
|
await server.connect(transport)
|
||||||
|
|
||||||
|
log.info("MCP server running on stdio")
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((error) => {
|
||||||
|
log.error("Fatal error:", error)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
24
packages/mcp-server/src/logger.ts
Normal file
24
packages/mcp-server/src/logger.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* Logger for MCP server
|
||||||
|
*
|
||||||
|
* CRITICAL: MCP servers communicate via STDIO (stdin/stdout).
|
||||||
|
* Using console.log() will corrupt the JSON-RPC protocol messages.
|
||||||
|
* ALL logging MUST use console.error() which writes to stderr.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const log = {
|
||||||
|
info: (msg: string, ...args: unknown[]) => {
|
||||||
|
console.error(`[MCP-DrawIO] [INFO] ${msg}`, ...args)
|
||||||
|
},
|
||||||
|
error: (msg: string, ...args: unknown[]) => {
|
||||||
|
console.error(`[MCP-DrawIO] [ERROR] ${msg}`, ...args)
|
||||||
|
},
|
||||||
|
debug: (msg: string, ...args: unknown[]) => {
|
||||||
|
if (process.env.DEBUG === "true") {
|
||||||
|
console.error(`[MCP-DrawIO] [DEBUG] ${msg}`, ...args)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
warn: (msg: string, ...args: unknown[]) => {
|
||||||
|
console.error(`[MCP-DrawIO] [WARN] ${msg}`, ...args)
|
||||||
|
},
|
||||||
|
}
|
||||||
19
packages/mcp-server/tsconfig.json
Normal file
19
packages/mcp-server/tsconfig.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "Node16",
|
||||||
|
"moduleResolution": "Node16",
|
||||||
|
"outDir": "./dist",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"resolveJsonModule": true
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"],
|
||||||
|
"exclude": ["node_modules", "dist"]
|
||||||
|
}
|
||||||
251
scripts/test-diagram-operations.mjs
Normal file
251
scripts/test-diagram-operations.mjs
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
/**
|
||||||
|
* Simple test script for applyDiagramOperations function
|
||||||
|
* Run with: node scripts/test-diagram-operations.mjs
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { JSDOM } from "jsdom"
|
||||||
|
|
||||||
|
// Set up DOMParser for Node.js environment
|
||||||
|
const dom = new JSDOM()
|
||||||
|
globalThis.DOMParser = dom.window.DOMParser
|
||||||
|
globalThis.XMLSerializer = dom.window.XMLSerializer
|
||||||
|
|
||||||
|
// Import the function (we'll inline it since it's not ESM exported)
|
||||||
|
function applyDiagramOperations(xmlContent, operations) {
|
||||||
|
const errors = []
|
||||||
|
const parser = new DOMParser()
|
||||||
|
const doc = parser.parseFromString(xmlContent, "text/xml")
|
||||||
|
|
||||||
|
const parseError = doc.querySelector("parsererror")
|
||||||
|
if (parseError) {
|
||||||
|
return {
|
||||||
|
result: xmlContent,
|
||||||
|
errors: [{ type: "update", cellId: "", message: `XML parse error: ${parseError.textContent}` }],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const root = doc.querySelector("root")
|
||||||
|
if (!root) {
|
||||||
|
return {
|
||||||
|
result: xmlContent,
|
||||||
|
errors: [{ type: "update", cellId: "", message: "Could not find <root> element in XML" }],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cellMap = new Map()
|
||||||
|
root.querySelectorAll("mxCell").forEach((cell) => {
|
||||||
|
const id = cell.getAttribute("id")
|
||||||
|
if (id) cellMap.set(id, cell)
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const op of operations) {
|
||||||
|
if (op.type === "update") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({ type: "update", cellId: op.cell_id, message: `Cell with id="${op.cell_id}" not found` })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({ type: "update", cellId: op.cell_id, message: "new_xml is required for update operation" })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const newDoc = parser.parseFromString(`<wrapper>${op.new_xml}</wrapper>`, "text/xml")
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({ type: "update", cellId: op.cell_id, message: "new_xml must contain an mxCell element" })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({ type: "update", cellId: op.cell_id, message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"` })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
existingCell.parentNode?.replaceChild(importedNode, existingCell)
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "add") {
|
||||||
|
if (cellMap.has(op.cell_id)) {
|
||||||
|
errors.push({ type: "add", cellId: op.cell_id, message: `Cell with id="${op.cell_id}" already exists` })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (!op.new_xml) {
|
||||||
|
errors.push({ type: "add", cellId: op.cell_id, message: "new_xml is required for add operation" })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const newDoc = parser.parseFromString(`<wrapper>${op.new_xml}</wrapper>`, "text/xml")
|
||||||
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
|
if (!newCell) {
|
||||||
|
errors.push({ type: "add", cellId: op.cell_id, message: "new_xml must contain an mxCell element" })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const newCellId = newCell.getAttribute("id")
|
||||||
|
if (newCellId !== op.cell_id) {
|
||||||
|
errors.push({ type: "add", cellId: op.cell_id, message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"` })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
const importedNode = doc.importNode(newCell, true)
|
||||||
|
root.appendChild(importedNode)
|
||||||
|
cellMap.set(op.cell_id, importedNode)
|
||||||
|
} else if (op.type === "delete") {
|
||||||
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
|
if (!existingCell) {
|
||||||
|
errors.push({ type: "delete", cellId: op.cell_id, message: `Cell with id="${op.cell_id}" not found` })
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
existingCell.parentNode?.removeChild(existingCell)
|
||||||
|
cellMap.delete(op.cell_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const serializer = new XMLSerializer()
|
||||||
|
const result = serializer.serializeToString(doc)
|
||||||
|
return { result, errors }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test data
|
||||||
|
const sampleXml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<mxfile>
|
||||||
|
<diagram>
|
||||||
|
<mxGraphModel>
|
||||||
|
<root>
|
||||||
|
<mxCell id="0"/>
|
||||||
|
<mxCell id="1" parent="0"/>
|
||||||
|
<mxCell id="2" value="Box A" style="rounded=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="100" y="100" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="Box B" style="rounded=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="300" y="100" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="" style="edgeStyle=orthogonalEdgeStyle;" edge="1" parent="1" source="2" target="3">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
</root>
|
||||||
|
</mxGraphModel>
|
||||||
|
</diagram>
|
||||||
|
</mxfile>`
|
||||||
|
|
||||||
|
let passed = 0
|
||||||
|
let failed = 0
|
||||||
|
|
||||||
|
function test(name, fn) {
|
||||||
|
try {
|
||||||
|
fn()
|
||||||
|
console.log(`✓ ${name}`)
|
||||||
|
passed++
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`✗ ${name}`)
|
||||||
|
console.log(` Error: ${e.message}`)
|
||||||
|
failed++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function assert(condition, message) {
|
||||||
|
if (!condition) throw new Error(message || "Assertion failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests
|
||||||
|
test("Update operation changes cell value", () => {
|
||||||
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cell_id: "2",
|
||||||
|
new_xml: '<mxCell id="2" value="Updated Box A" style="rounded=1;" vertex="1" parent="1"><mxGeometry x="100" y="100" width="120" height="60" as="geometry"/></mxCell>',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
assert(errors.length === 0, `Expected no errors, got: ${JSON.stringify(errors)}`)
|
||||||
|
assert(result.includes('value="Updated Box A"'), "Updated value should be in result")
|
||||||
|
assert(!result.includes('value="Box A"'), "Old value should not be in result")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Update operation fails for non-existent cell", () => {
|
||||||
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{ type: "update", cell_id: "999", new_xml: '<mxCell id="999" value="Test"/>' },
|
||||||
|
])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("not found"), "Error should mention not found")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Update operation fails on ID mismatch", () => {
|
||||||
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{ type: "update", cell_id: "2", new_xml: '<mxCell id="WRONG" value="Test"/>' },
|
||||||
|
])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("ID mismatch"), "Error should mention ID mismatch")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Add operation creates new cell", () => {
|
||||||
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{
|
||||||
|
type: "add",
|
||||||
|
cell_id: "new1",
|
||||||
|
new_xml: '<mxCell id="new1" value="New Box" style="rounded=1;" vertex="1" parent="1"><mxGeometry x="500" y="100" width="120" height="60" as="geometry"/></mxCell>',
|
||||||
|
},
|
||||||
|
])
|
||||||
|
assert(errors.length === 0, `Expected no errors, got: ${JSON.stringify(errors)}`)
|
||||||
|
assert(result.includes('id="new1"'), "New cell should be in result")
|
||||||
|
assert(result.includes('value="New Box"'), "New cell value should be in result")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Add operation fails for duplicate ID", () => {
|
||||||
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{ type: "add", cell_id: "2", new_xml: '<mxCell id="2" value="Duplicate"/>' },
|
||||||
|
])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("already exists"), "Error should mention already exists")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Add operation fails on ID mismatch", () => {
|
||||||
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{ type: "add", cell_id: "new1", new_xml: '<mxCell id="WRONG" value="Test"/>' },
|
||||||
|
])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("ID mismatch"), "Error should mention ID mismatch")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Delete operation removes cell", () => {
|
||||||
|
const { result, errors } = applyDiagramOperations(sampleXml, [{ type: "delete", cell_id: "3" }])
|
||||||
|
assert(errors.length === 0, `Expected no errors, got: ${JSON.stringify(errors)}`)
|
||||||
|
assert(!result.includes('id="3"'), "Deleted cell should not be in result")
|
||||||
|
assert(result.includes('id="2"'), "Other cells should remain")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Delete operation fails for non-existent cell", () => {
|
||||||
|
const { errors } = applyDiagramOperations(sampleXml, [{ type: "delete", cell_id: "999" }])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("not found"), "Error should mention not found")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Multiple operations in sequence", () => {
|
||||||
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
|
{
|
||||||
|
type: "update",
|
||||||
|
cell_id: "2",
|
||||||
|
new_xml: '<mxCell id="2" value="Updated" style="rounded=1;" vertex="1" parent="1"><mxGeometry x="100" y="100" width="120" height="60" as="geometry"/></mxCell>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "add",
|
||||||
|
cell_id: "new1",
|
||||||
|
new_xml: '<mxCell id="new1" value="Added" style="rounded=1;" vertex="1" parent="1"><mxGeometry x="500" y="100" width="120" height="60" as="geometry"/></mxCell>',
|
||||||
|
},
|
||||||
|
{ type: "delete", cell_id: "3" },
|
||||||
|
])
|
||||||
|
assert(errors.length === 0, `Expected no errors, got: ${JSON.stringify(errors)}`)
|
||||||
|
assert(result.includes('value="Updated"'), "Updated value should be present")
|
||||||
|
assert(result.includes('id="new1"'), "Added cell should be present")
|
||||||
|
assert(!result.includes('id="3"'), "Deleted cell should not be present")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Invalid XML returns parse error", () => {
|
||||||
|
const { errors } = applyDiagramOperations("<not valid xml", [{ type: "delete", cell_id: "1" }])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Missing root element returns error", () => {
|
||||||
|
const { errors } = applyDiagramOperations("<mxfile></mxfile>", [{ type: "delete", cell_id: "1" }])
|
||||||
|
assert(errors.length === 1, "Should have one error")
|
||||||
|
assert(errors[0].message.includes("root"), "Error should mention root element")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Summary
|
||||||
|
console.log(`\n${passed} passed, ${failed} failed`)
|
||||||
|
process.exit(failed > 0 ? 1 : 0)
|
||||||
@@ -29,5 +29,5 @@
|
|||||||
".next/types/**/*.ts",
|
".next/types/**/*.ts",
|
||||||
".next/dev/types/**/*.ts"
|
".next/dev/types/**/*.ts"
|
||||||
],
|
],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules", "packages"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user