mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
Compare commits
18 Commits
d3be96de79
...
fix/auto-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34913ce477 | ||
|
|
d1d0de3dea | ||
|
|
8c736cee0d | ||
|
|
c5a04c9e50 | ||
|
|
44c453403f | ||
|
|
9727aa5b39 | ||
|
|
51858dbf5d | ||
|
|
3047d19238 | ||
|
|
ed069afdea | ||
|
|
d2e5afb298 | ||
|
|
d3fb2314ee | ||
|
|
447bb30745 | ||
|
|
63398d9f34 | ||
|
|
82f4deb23a | ||
|
|
1fab261cd0 | ||
|
|
7a4a04c263 | ||
|
|
0d2e7a7ad6 | ||
|
|
3218ccc909 |
16
.github/workflows/auto-format.yml
vendored
16
.github/workflows/auto-format.yml
vendored
@@ -14,7 +14,7 @@ jobs:
|
|||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.head_ref }}
|
ref: ${{ github.event.pull_request.head.sha }}
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
@@ -37,11 +37,21 @@ jobs:
|
|||||||
echo "has_changes=true" >> $GITHUB_OUTPUT
|
echo "has_changes=true" >> $GITHUB_OUTPUT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# For fork PRs, just fail if formatting is needed (can't push to forks)
|
||||||
|
- name: Fail if fork PR needs formatting
|
||||||
|
if: steps.changes.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name != github.repository
|
||||||
|
run: |
|
||||||
|
echo "::error::This PR has formatting issues. Please run 'npx @biomejs/biome check --write .' locally and push the changes."
|
||||||
|
git diff --stat
|
||||||
|
exit 1
|
||||||
|
|
||||||
|
# For same-repo PRs, commit and push the changes
|
||||||
- name: Commit changes
|
- name: Commit changes
|
||||||
if: steps.changes.outputs.has_changes == 'true'
|
if: steps.changes.outputs.has_changes == 'true' && github.event.pull_request.head.repo.full_name == github.repository
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name "github-actions[bot]"
|
git config --global user.name "github-actions[bot]"
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
||||||
|
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
|
||||||
git add .
|
git add .
|
||||||
git commit -m "style: auto-format with Biome"
|
git commit -m "style: auto-format with Biome"
|
||||||
git push
|
git push origin HEAD:${{ github.head_ref }}
|
||||||
|
|||||||
44
.github/workflows/ci.yml
vendored
Normal file
44
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
ci:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '20'
|
||||||
|
cache: 'npm'
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Type check
|
||||||
|
run: npx tsc --noEmit
|
||||||
|
|
||||||
|
- name: Lint check
|
||||||
|
run: npm run check
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
|
|
||||||
|
- name: Security audit
|
||||||
|
run: npm audit --audit-level=high --omit=dev
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
import { usePathname, useRouter } from "next/navigation"
|
||||||
import { useCallback, useEffect, useRef, useState } from "react"
|
import { useCallback, useEffect, useRef, useState } from "react"
|
||||||
import { DrawIoEmbed } from "react-drawio"
|
import { DrawIoEmbed } from "react-drawio"
|
||||||
import type { ImperativePanelHandle } from "react-resizable-panels"
|
import type { ImperativePanelHandle } from "react-resizable-panels"
|
||||||
@@ -10,6 +11,7 @@ import {
|
|||||||
ResizablePanelGroup,
|
ResizablePanelGroup,
|
||||||
} from "@/components/ui/resizable"
|
} from "@/components/ui/resizable"
|
||||||
import { useDiagram } from "@/contexts/diagram-context"
|
import { useDiagram } from "@/contexts/diagram-context"
|
||||||
|
import { i18n, type Locale } from "@/lib/i18n/config"
|
||||||
|
|
||||||
const drawioBaseUrl =
|
const drawioBaseUrl =
|
||||||
process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || "https://embed.diagrams.net"
|
process.env.NEXT_PUBLIC_DRAWIO_BASE_URL || "https://embed.diagrams.net"
|
||||||
@@ -24,6 +26,8 @@ export default function Home() {
|
|||||||
showSaveDialog,
|
showSaveDialog,
|
||||||
setShowSaveDialog,
|
setShowSaveDialog,
|
||||||
} = useDiagram()
|
} = useDiagram()
|
||||||
|
const router = useRouter()
|
||||||
|
const pathname = usePathname()
|
||||||
const [isMobile, setIsMobile] = useState(false)
|
const [isMobile, setIsMobile] = useState(false)
|
||||||
const [isChatVisible, setIsChatVisible] = useState(true)
|
const [isChatVisible, setIsChatVisible] = useState(true)
|
||||||
const [drawioUi, setDrawioUi] = useState<"min" | "sketch">("min")
|
const [drawioUi, setDrawioUi] = useState<"min" | "sketch">("min")
|
||||||
@@ -58,6 +62,18 @@ export default function Home() {
|
|||||||
|
|
||||||
// Load preferences from localStorage after mount
|
// Load preferences from localStorage after mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Restore saved locale and redirect if needed
|
||||||
|
const savedLocale = localStorage.getItem("next-ai-draw-io-locale")
|
||||||
|
if (savedLocale && i18n.locales.includes(savedLocale as Locale)) {
|
||||||
|
const pathParts = pathname.split("/").filter(Boolean)
|
||||||
|
const currentLocale = pathParts[0]
|
||||||
|
if (currentLocale !== savedLocale) {
|
||||||
|
pathParts[0] = savedLocale
|
||||||
|
router.replace(`/${pathParts.join("/")}`)
|
||||||
|
return // Wait for redirect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const savedUi = localStorage.getItem("drawio-theme")
|
const savedUi = localStorage.getItem("drawio-theme")
|
||||||
if (savedUi === "min" || savedUi === "sketch") {
|
if (savedUi === "min" || savedUi === "sketch") {
|
||||||
setDrawioUi(savedUi)
|
setDrawioUi(savedUi)
|
||||||
@@ -84,7 +100,7 @@ export default function Home() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setIsLoaded(true)
|
setIsLoaded(true)
|
||||||
}, [])
|
}, [pathname, router])
|
||||||
|
|
||||||
const handleDarkModeChange = async () => {
|
const handleDarkModeChange = async () => {
|
||||||
await saveDiagramToStorage()
|
await saveDiagramToStorage()
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import {
|
|||||||
wrapWithObserve,
|
wrapWithObserve,
|
||||||
} from "@/lib/langfuse"
|
} from "@/lib/langfuse"
|
||||||
import { getSystemPrompt } from "@/lib/system-prompts"
|
import { getSystemPrompt } from "@/lib/system-prompts"
|
||||||
|
import { getUserIdFromRequest } from "@/lib/user-id"
|
||||||
|
|
||||||
export const maxDuration = 120
|
export const maxDuration = 120
|
||||||
|
|
||||||
@@ -167,13 +168,8 @@ async function handleChatRequest(req: Request): Promise<Response> {
|
|||||||
|
|
||||||
const { messages, xml, previousXml, sessionId } = await req.json()
|
const { messages, xml, previousXml, sessionId } = await req.json()
|
||||||
|
|
||||||
// Get user IP for Langfuse tracking (hashed for privacy)
|
// Get user ID for Langfuse tracking and quota
|
||||||
const forwardedFor = req.headers.get("x-forwarded-for")
|
const userId = getUserIdFromRequest(req)
|
||||||
const rawIp = forwardedFor?.split(",")[0]?.trim() || "anonymous"
|
|
||||||
const userId =
|
|
||||||
rawIp === "anonymous"
|
|
||||||
? rawIp
|
|
||||||
: `user-${Buffer.from(rawIp).toString("base64url").slice(0, 8)}`
|
|
||||||
|
|
||||||
// Validate sessionId for Langfuse (must be string, max 200 chars)
|
// Validate sessionId for Langfuse (must be string, max 200 chars)
|
||||||
const validSessionId =
|
const validSessionId =
|
||||||
@@ -613,14 +609,22 @@ Operations:
|
|||||||
|
|
||||||
For update/add, new_xml must be a complete mxCell element including mxGeometry.
|
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\\"`,
|
⚠️ JSON ESCAPING: Every " inside new_xml MUST be escaped as \\". Example: id=\\"5\\" value=\\"Label\\"
|
||||||
|
|
||||||
|
Example - Add a rectangle:
|
||||||
|
{"operations": [{"operation": "add", "cell_id": "rect-1", "new_xml": "<mxCell id=\\"rect-1\\" value=\\"Hello\\" style=\\"rounded=0;\\" vertex=\\"1\\" parent=\\"1\\"><mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/></mxCell>"}]}
|
||||||
|
|
||||||
|
Example - Delete a cell:
|
||||||
|
{"operations": [{"operation": "delete", "cell_id": "rect-1"}]}`,
|
||||||
inputSchema: z.object({
|
inputSchema: z.object({
|
||||||
operations: z
|
operations: z
|
||||||
.array(
|
.array(
|
||||||
z.object({
|
z.object({
|
||||||
type: z
|
operation: z
|
||||||
.enum(["update", "add", "delete"])
|
.enum(["update", "add", "delete"])
|
||||||
.describe("Operation type"),
|
.describe(
|
||||||
|
"Operation to perform: add, update, or delete",
|
||||||
|
),
|
||||||
cell_id: z
|
cell_id: z
|
||||||
.string()
|
.string()
|
||||||
.describe(
|
.describe(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { randomUUID } from "crypto"
|
import { randomUUID } from "crypto"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { getLangfuseClient } from "@/lib/langfuse"
|
import { getLangfuseClient } from "@/lib/langfuse"
|
||||||
|
import { getUserIdFromRequest } from "@/lib/user-id"
|
||||||
|
|
||||||
const feedbackSchema = z.object({
|
const feedbackSchema = z.object({
|
||||||
messageId: z.string().min(1).max(200),
|
messageId: z.string().min(1).max(200),
|
||||||
@@ -32,13 +33,8 @@ export async function POST(req: Request) {
|
|||||||
return Response.json({ success: true, logged: false })
|
return Response.json({ success: true, logged: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user IP for tracking (hashed for privacy)
|
// Get user ID for tracking
|
||||||
const forwardedFor = req.headers.get("x-forwarded-for")
|
const userId = getUserIdFromRequest(req)
|
||||||
const rawIp = forwardedFor?.split(",")[0]?.trim() || "anonymous"
|
|
||||||
const userId =
|
|
||||||
rawIp === "anonymous"
|
|
||||||
? rawIp
|
|
||||||
: `user-${Buffer.from(rawIp).toString("base64url").slice(0, 8)}`
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Find the most recent chat trace for this session to attach the score to
|
// Find the most recent chat trace for this session to attach the score to
|
||||||
|
|||||||
@@ -66,8 +66,22 @@ export const ModelSelectorInput = ({
|
|||||||
|
|
||||||
export type ModelSelectorListProps = ComponentProps<typeof CommandList>
|
export type ModelSelectorListProps = ComponentProps<typeof CommandList>
|
||||||
|
|
||||||
export const ModelSelectorList = (props: ModelSelectorListProps) => (
|
export const ModelSelectorList = ({
|
||||||
<CommandList {...props} />
|
className,
|
||||||
|
...props
|
||||||
|
}: ModelSelectorListProps) => (
|
||||||
|
<div className="relative">
|
||||||
|
<CommandList
|
||||||
|
className={cn(
|
||||||
|
// Hide scrollbar on all platforms
|
||||||
|
"[&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]",
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{/* Bottom shadow indicator for scrollable content */}
|
||||||
|
<div className="pointer-events-none absolute bottom-0 left-0 right-0 h-12 bg-gradient-to-t from-muted/80 via-muted/40 to-transparent" />
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ModelSelectorEmptyProps = ComponentProps<typeof CommandEmpty>
|
export type ModelSelectorEmptyProps = ComponentProps<typeof CommandEmpty>
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ import ExamplePanel from "./chat-example-panel"
|
|||||||
import { CodeBlock } from "./code-block"
|
import { CodeBlock } from "./code-block"
|
||||||
|
|
||||||
interface DiagramOperation {
|
interface DiagramOperation {
|
||||||
type: "update" | "add" | "delete"
|
operation: "update" | "add" | "delete"
|
||||||
cell_id: string
|
cell_id: string
|
||||||
new_xml?: string
|
new_xml?: string
|
||||||
}
|
}
|
||||||
@@ -53,12 +53,12 @@ function getCompleteOperations(
|
|||||||
return operations.filter(
|
return operations.filter(
|
||||||
(op) =>
|
(op) =>
|
||||||
op &&
|
op &&
|
||||||
typeof op.type === "string" &&
|
typeof op.operation === "string" &&
|
||||||
["update", "add", "delete"].includes(op.type) &&
|
["update", "add", "delete"].includes(op.operation) &&
|
||||||
typeof op.cell_id === "string" &&
|
typeof op.cell_id === "string" &&
|
||||||
op.cell_id.length > 0 &&
|
op.cell_id.length > 0 &&
|
||||||
// delete doesn't need new_xml, update/add do
|
// delete doesn't need new_xml, update/add do
|
||||||
(op.type === "delete" || typeof op.new_xml === "string"),
|
(op.operation === "delete" || typeof op.new_xml === "string"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,20 +79,20 @@ function OperationsDisplay({ operations }: { operations: DiagramOperation[] }) {
|
|||||||
<div className="space-y-3">
|
<div className="space-y-3">
|
||||||
{operations.map((op, index) => (
|
{operations.map((op, index) => (
|
||||||
<div
|
<div
|
||||||
key={`${op.type}-${op.cell_id}-${index}`}
|
key={`${op.operation}-${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
|
<span
|
||||||
className={`text-[10px] font-medium uppercase tracking-wide ${
|
className={`text-[10px] font-medium uppercase tracking-wide ${
|
||||||
op.type === "delete"
|
op.operation === "delete"
|
||||||
? "text-red-600"
|
? "text-red-600"
|
||||||
: op.type === "add"
|
: op.operation === "add"
|
||||||
? "text-green-600"
|
? "text-green-600"
|
||||||
: "text-blue-600"
|
: "text-blue-600"
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
{op.type}
|
{op.operation}
|
||||||
</span>
|
</span>
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-xs text-muted-foreground">
|
||||||
cell_id: {op.cell_id}
|
cell_id: {op.cell_id}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import {
|
|||||||
TooltipTrigger,
|
TooltipTrigger,
|
||||||
} from "@/components/ui/tooltip"
|
} from "@/components/ui/tooltip"
|
||||||
import { useDiagram } from "@/contexts/diagram-context"
|
import { useDiagram } from "@/contexts/diagram-context"
|
||||||
|
import { useDiagramToolHandlers } from "@/hooks/use-diagram-tool-handlers"
|
||||||
import { useDictionary } from "@/hooks/use-dictionary"
|
import { useDictionary } from "@/hooks/use-dictionary"
|
||||||
import { getSelectedAIConfig, useModelConfig } from "@/hooks/use-model-config"
|
import { getSelectedAIConfig, useModelConfig } from "@/hooks/use-model-config"
|
||||||
import { getApiEndpoint } from "@/lib/base-path"
|
import { getApiEndpoint } from "@/lib/base-path"
|
||||||
@@ -34,8 +35,9 @@ import { findCachedResponse } from "@/lib/cached-responses"
|
|||||||
import { isPdfFile, isTextFile } from "@/lib/pdf-utils"
|
import { isPdfFile, isTextFile } from "@/lib/pdf-utils"
|
||||||
import { type FileData, useFileProcessor } from "@/lib/use-file-processor"
|
import { type FileData, useFileProcessor } from "@/lib/use-file-processor"
|
||||||
import { useQuotaManager } from "@/lib/use-quota-manager"
|
import { useQuotaManager } from "@/lib/use-quota-manager"
|
||||||
import { formatXML, isMxCellXmlComplete, wrapWithMxFile } from "@/lib/utils"
|
import { formatXML } from "@/lib/utils"
|
||||||
import { ChatMessageDisplay } from "./chat-message-display"
|
import { ChatMessageDisplay } from "./chat-message-display"
|
||||||
|
import { DevXmlSimulator } from "./dev-xml-simulator"
|
||||||
|
|
||||||
// localStorage keys for persistence
|
// localStorage keys for persistence
|
||||||
const STORAGE_MESSAGES_KEY = "next-ai-draw-io-messages"
|
const STORAGE_MESSAGES_KEY = "next-ai-draw-io-messages"
|
||||||
@@ -76,6 +78,7 @@ interface ChatPanelProps {
|
|||||||
const TOOL_ERROR_STATE = "output-error" as const
|
const TOOL_ERROR_STATE = "output-error" as const
|
||||||
const DEBUG = process.env.NODE_ENV === "development"
|
const DEBUG = process.env.NODE_ENV === "development"
|
||||||
const MAX_AUTO_RETRY_COUNT = 1
|
const MAX_AUTO_RETRY_COUNT = 1
|
||||||
|
|
||||||
const MAX_CONTINUATION_RETRY_COUNT = 2 // Limit for truncation continuation retries
|
const MAX_CONTINUATION_RETRY_COUNT = 2 // Limit for truncation continuation retries
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,9 +215,6 @@ export default function ChatPanel({
|
|||||||
chartXMLRef.current = chartXML
|
chartXMLRef.current = chartXML
|
||||||
}, [chartXML])
|
}, [chartXML])
|
||||||
|
|
||||||
// Ref to hold stop function for use in onToolCall (avoids stale closure)
|
|
||||||
const stopRef = useRef<(() => void) | null>(null)
|
|
||||||
|
|
||||||
// Ref to track consecutive auto-retry count (reset on user action)
|
// Ref to track consecutive auto-retry count (reset on user action)
|
||||||
const autoRetryCountRef = useRef(0)
|
const autoRetryCountRef = useRef(0)
|
||||||
// Ref to track continuation retry count (for truncation handling)
|
// Ref to track continuation retry count (for truncation handling)
|
||||||
@@ -237,6 +237,16 @@ export default function ChatPanel({
|
|||||||
> | null>(null)
|
> | null>(null)
|
||||||
const LOCAL_STORAGE_DEBOUNCE_MS = 1000 // Save at most once per second
|
const LOCAL_STORAGE_DEBOUNCE_MS = 1000 // Save at most once per second
|
||||||
|
|
||||||
|
// Diagram tool handlers (display_diagram, edit_diagram, append_diagram)
|
||||||
|
const { handleToolCall } = useDiagramToolHandlers({
|
||||||
|
partialXmlRef,
|
||||||
|
editDiagramOriginalXmlRef,
|
||||||
|
chartXMLRef,
|
||||||
|
onDisplayChart,
|
||||||
|
onFetchChart,
|
||||||
|
onExport,
|
||||||
|
})
|
||||||
|
|
||||||
const {
|
const {
|
||||||
messages,
|
messages,
|
||||||
sendMessage,
|
sendMessage,
|
||||||
@@ -249,311 +259,8 @@ export default function ChatPanel({
|
|||||||
transport: new DefaultChatTransport({
|
transport: new DefaultChatTransport({
|
||||||
api: getApiEndpoint("/api/chat"),
|
api: getApiEndpoint("/api/chat"),
|
||||||
}),
|
}),
|
||||||
async onToolCall({ toolCall }) {
|
onToolCall: async ({ toolCall }) => {
|
||||||
if (DEBUG) {
|
await handleToolCall({ toolCall }, addToolOutput)
|
||||||
console.log(
|
|
||||||
`[onToolCall] Tool: ${toolCall.toolName}, CallId: ${toolCall.toolCallId}`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.toolName === "display_diagram") {
|
|
||||||
const { xml } = toolCall.input as { xml: string }
|
|
||||||
|
|
||||||
// DEBUG: Log raw input to diagnose false truncation detection
|
|
||||||
console.log(
|
|
||||||
"[display_diagram] XML ending (last 100 chars):",
|
|
||||||
xml.slice(-100),
|
|
||||||
)
|
|
||||||
console.log("[display_diagram] XML length:", xml.length)
|
|
||||||
|
|
||||||
// Check if XML is truncated (incomplete mxCell indicates truncated output)
|
|
||||||
const isTruncated = !isMxCellXmlComplete(xml)
|
|
||||||
console.log("[display_diagram] isTruncated:", isTruncated)
|
|
||||||
|
|
||||||
if (isTruncated) {
|
|
||||||
// Store the partial XML for continuation via append_diagram
|
|
||||||
partialXmlRef.current = xml
|
|
||||||
|
|
||||||
// Tell LLM to use append_diagram to continue
|
|
||||||
const partialEnding = partialXmlRef.current.slice(-500)
|
|
||||||
addToolOutput({
|
|
||||||
tool: "display_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `Output was truncated due to length limits. Use the append_diagram tool to continue.
|
|
||||||
|
|
||||||
Your output ended with:
|
|
||||||
\`\`\`
|
|
||||||
${partialEnding}
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
NEXT STEP: Call append_diagram with the continuation XML.
|
|
||||||
- Do NOT include wrapper tags or root cells (id="0", id="1")
|
|
||||||
- Start from EXACTLY where you stopped
|
|
||||||
- Complete all remaining mxCell elements`,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Complete XML received - use it directly
|
|
||||||
// (continuation is now handled via append_diagram tool)
|
|
||||||
const finalXml = xml
|
|
||||||
partialXmlRef.current = "" // Reset any partial from previous truncation
|
|
||||||
|
|
||||||
// Wrap raw XML with full mxfile structure for draw.io
|
|
||||||
const fullXml = wrapWithMxFile(finalXml)
|
|
||||||
|
|
||||||
// loadDiagram validates and returns error if invalid
|
|
||||||
const validationError = onDisplayChart(fullXml)
|
|
||||||
|
|
||||||
if (validationError) {
|
|
||||||
console.warn(
|
|
||||||
"[display_diagram] Validation error:",
|
|
||||||
validationError,
|
|
||||||
)
|
|
||||||
// Return error to model - sendAutomaticallyWhen will trigger retry
|
|
||||||
if (DEBUG) {
|
|
||||||
console.log(
|
|
||||||
"[display_diagram] Adding tool output with state: output-error",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
addToolOutput({
|
|
||||||
tool: "display_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `${validationError}
|
|
||||||
|
|
||||||
Please fix the XML issues and call display_diagram again with corrected XML.
|
|
||||||
|
|
||||||
Your failed XML:
|
|
||||||
\`\`\`xml
|
|
||||||
${finalXml}
|
|
||||||
\`\`\``,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// Success - diagram will be rendered by chat-message-display
|
|
||||||
if (DEBUG) {
|
|
||||||
console.log(
|
|
||||||
"[display_diagram] Success! Adding tool output with state: output-available",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
addToolOutput({
|
|
||||||
tool: "display_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
output: "Successfully displayed the diagram.",
|
|
||||||
})
|
|
||||||
if (DEBUG) {
|
|
||||||
console.log(
|
|
||||||
"[display_diagram] Tool output added. Diagram should be visible now.",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (toolCall.toolName === "edit_diagram") {
|
|
||||||
const { operations } = toolCall.input as {
|
|
||||||
operations: Array<{
|
|
||||||
type: "update" | "add" | "delete"
|
|
||||||
cell_id: string
|
|
||||||
new_xml?: string
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
|
|
||||||
let currentXml = ""
|
|
||||||
try {
|
|
||||||
// Use the original XML captured during streaming (shared with chat-message-display)
|
|
||||||
// This ensures we apply operations to the same base XML that streaming used
|
|
||||||
const originalXml = editDiagramOriginalXmlRef.current.get(
|
|
||||||
toolCall.toolCallId,
|
|
||||||
)
|
|
||||||
if (originalXml) {
|
|
||||||
currentXml = originalXml
|
|
||||||
} else {
|
|
||||||
// Fallback: use chartXML from ref if streaming didn't capture original
|
|
||||||
const cachedXML = chartXMLRef.current
|
|
||||||
if (cachedXML) {
|
|
||||||
currentXml = cachedXML
|
|
||||||
} else {
|
|
||||||
// Last resort: export from iframe
|
|
||||||
currentXml = await onFetchChart(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const { applyDiagramOperations } = await import(
|
|
||||||
"@/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
|
|
||||||
const validationError = onDisplayChart(editedXml)
|
|
||||||
if (validationError) {
|
|
||||||
console.warn(
|
|
||||||
"[edit_diagram] Validation error:",
|
|
||||||
validationError,
|
|
||||||
)
|
|
||||||
addToolOutput({
|
|
||||||
tool: "edit_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `Edit produced invalid XML: ${validationError}
|
|
||||||
|
|
||||||
Current diagram XML:
|
|
||||||
\`\`\`xml
|
|
||||||
${currentXml}
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
Please fix the operations to avoid structural issues.`,
|
|
||||||
})
|
|
||||||
// Clean up the shared original XML ref
|
|
||||||
editDiagramOriginalXmlRef.current.delete(
|
|
||||||
toolCall.toolCallId,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
onExport()
|
|
||||||
addToolOutput({
|
|
||||||
tool: "edit_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
output: `Successfully applied ${operations.length} operation(s) to the diagram.`,
|
|
||||||
})
|
|
||||||
// Clean up the shared original XML ref
|
|
||||||
editDiagramOriginalXmlRef.current.delete(
|
|
||||||
toolCall.toolCallId,
|
|
||||||
)
|
|
||||||
} catch (error) {
|
|
||||||
console.error("[edit_diagram] Failed:", error)
|
|
||||||
|
|
||||||
const errorMessage =
|
|
||||||
error instanceof Error ? error.message : String(error)
|
|
||||||
|
|
||||||
addToolOutput({
|
|
||||||
tool: "edit_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `Edit failed: ${errorMessage}
|
|
||||||
|
|
||||||
Current diagram XML:
|
|
||||||
\`\`\`xml
|
|
||||||
${currentXml || "No XML available"}
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
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") {
|
|
||||||
const { xml } = toolCall.input as { xml: string }
|
|
||||||
|
|
||||||
// Detect if LLM incorrectly started fresh instead of continuing
|
|
||||||
// LLM should only output bare mxCells now, so wrapper tags indicate error
|
|
||||||
const trimmed = xml.trim()
|
|
||||||
const isFreshStart =
|
|
||||||
trimmed.startsWith("<mxGraphModel") ||
|
|
||||||
trimmed.startsWith("<root") ||
|
|
||||||
trimmed.startsWith("<mxfile") ||
|
|
||||||
trimmed.startsWith('<mxCell id="0"') ||
|
|
||||||
trimmed.startsWith('<mxCell id="1"')
|
|
||||||
|
|
||||||
if (isFreshStart) {
|
|
||||||
addToolOutput({
|
|
||||||
tool: "append_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `ERROR: You started fresh with wrapper tags. Do NOT include wrapper tags or root cells (id="0", id="1").
|
|
||||||
|
|
||||||
Continue from EXACTLY where the partial ended:
|
|
||||||
\`\`\`
|
|
||||||
${partialXmlRef.current.slice(-500)}
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
Start your continuation with the NEXT character after where it stopped.`,
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append to accumulated XML
|
|
||||||
partialXmlRef.current += xml
|
|
||||||
|
|
||||||
// Check if XML is now complete (last mxCell is complete)
|
|
||||||
const isComplete = isMxCellXmlComplete(partialXmlRef.current)
|
|
||||||
|
|
||||||
if (isComplete) {
|
|
||||||
// Wrap and display the complete diagram
|
|
||||||
const finalXml = partialXmlRef.current
|
|
||||||
partialXmlRef.current = "" // Reset
|
|
||||||
|
|
||||||
const fullXml = wrapWithMxFile(finalXml)
|
|
||||||
const validationError = onDisplayChart(fullXml)
|
|
||||||
|
|
||||||
if (validationError) {
|
|
||||||
addToolOutput({
|
|
||||||
tool: "append_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `Validation error after assembly: ${validationError}
|
|
||||||
|
|
||||||
Assembled XML:
|
|
||||||
\`\`\`xml
|
|
||||||
${finalXml.substring(0, 2000)}...
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
Please use display_diagram with corrected XML.`,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
addToolOutput({
|
|
||||||
tool: "append_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
output: "Diagram assembly complete and displayed successfully.",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Still incomplete - signal to continue
|
|
||||||
addToolOutput({
|
|
||||||
tool: "append_diagram",
|
|
||||||
toolCallId: toolCall.toolCallId,
|
|
||||||
state: "output-error",
|
|
||||||
errorText: `XML still incomplete (mxCell not closed). Call append_diagram again to continue.
|
|
||||||
|
|
||||||
Current ending:
|
|
||||||
\`\`\`
|
|
||||||
${partialXmlRef.current.slice(-500)}
|
|
||||||
\`\`\`
|
|
||||||
|
|
||||||
Continue from EXACTLY where you stopped.`,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
// Handle server-side quota limit (429 response)
|
// Handle server-side quota limit (429 response)
|
||||||
@@ -717,9 +424,6 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update stopRef so onToolCall can access it
|
|
||||||
stopRef.current = stop
|
|
||||||
|
|
||||||
// Ref to track latest messages for unload persistence
|
// Ref to track latest messages for unload persistence
|
||||||
const messagesRef = useRef(messages)
|
const messagesRef = useRef(messages)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -1337,6 +1041,14 @@ Continue from EXACTLY where you stopped.`,
|
|||||||
/>
|
/>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
{/* Dev XML Streaming Simulator - only in development */}
|
||||||
|
{DEBUG && (
|
||||||
|
<DevXmlSimulator
|
||||||
|
setMessages={setMessages}
|
||||||
|
onDisplayChart={onDisplayChart}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Input */}
|
{/* Input */}
|
||||||
<footer
|
<footer
|
||||||
className={`${isMobile ? "p-2" : "p-4"} border-t border-border/50 bg-card/50`}
|
className={`${isMobile ? "p-2" : "p-4"} border-t border-border/50 bg-card/50`}
|
||||||
|
|||||||
350
components/dev-xml-simulator.tsx
Normal file
350
components/dev-xml-simulator.tsx
Normal file
@@ -0,0 +1,350 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { useEffect, useRef, useState } from "react"
|
||||||
|
import { wrapWithMxFile } from "@/lib/utils"
|
||||||
|
|
||||||
|
// Dev XML presets for streaming simulator
|
||||||
|
const DEV_XML_PRESETS: Record<string, string> = {
|
||||||
|
"Simple Box": `<mxCell id="2" value="Hello World" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="120" y="100" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>`,
|
||||||
|
"Two Boxes with Arrow": `<mxCell id="2" value="Start" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="100" y="100" width="100" height="50" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="End" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="300" y="100" width="100" height="50" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="" style="endArrow=classic;html=1;" edge="1" parent="1" source="2" target="3">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>`,
|
||||||
|
Flowchart: `<mxCell id="2" value="Start" style="ellipse;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="160" y="40" width="80" height="40" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="Process A" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="140" y="120" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="Decision" style="rhombus;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="150" y="220" width="100" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="5" value="Process B" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="300" y="230" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="6" value="End" style="ellipse;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="160" y="340" width="80" height="40" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="7" style="endArrow=classic;html=1;" edge="1" parent="1" source="2" target="3">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="8" style="endArrow=classic;html=1;" edge="1" parent="1" source="3" target="4">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="9" value="Yes" style="endArrow=classic;html=1;" edge="1" parent="1" source="4" target="6">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="10" value="No" style="endArrow=classic;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;" edge="1" parent="1" source="4" target="5">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>`,
|
||||||
|
"Truncated (Error Test)": `<mxCell id="2" value="This cell is truncated" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="120" y="100" width="120" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="Incomplete" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor`,
|
||||||
|
"HTML Escape + Cell Truncate": `<mxCell id="2" value="<b>Chain-of-Thought Prompting</b><br/><font size='12'>Eliciting Reasoning in Large Language Models</font>" style="rounded=0;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;fontSize=16;fontStyle=1;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="40" width="720" height="60" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="3" value="<b>Problem: LLM Reasoning Limitations</b><br/>• Scaling parameters alone insufficient for logical tasks<br/>• Arithmetic, commonsense, symbolic reasoning challenges<br/>• Standard prompting fails on multi-step problems" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="120" width="340" height="120" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="4" value="<b>Traditional Approaches</b><br/>1. <b>Finetuning:</b> Expensive, task-specific<br/>2. <b>Standard Few-Shot:</b> Input→Output pairs<br/> (No explanation of reasoning)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="420" y="120" width="340" height="120" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="5" value="<b>CoT Methodology</b><br/>• Add reasoning steps to few-shot examples<br/>• Natural language intermediate steps<br/>• No parameter updates needed<br/>• Model learns to generate own thought process" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="260" width="340" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="6" value="<b>Example Comparison</b><br/><b>Standard:</b><br/>Q: Roger has 5 balls. He buys 2 cans of 3 balls. How many?<br/>A: 11.<br/><br/><b>CoT:</b><br/>Q: Roger has 5 balls. He buys 2 cans of 3 balls. How many?<br/>A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11." style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="420" y="260" width="340" height="140" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="7" value="<b>Experimental Models</b><br/>• GPT-3 (175B)<br/>• LaMDA (137B)<br/>• PaLM (540B)<br/>• UL2 (20B)<br/>• Codex" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="380" width="340" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="8" value="<b>Reasoning Domains Tested</b><br/>1. <b>Arithmetic:</b> GSM8K, SVAMP, ASDiv, AQuA, MAWPS<br/>2. <b>Commonsense:</b> CSQA, StrategyQA, Date Understanding, Sports Understanding<br/>3. <b>Symbolic:</b> Last Letter Concatenation, Coin Flip" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f5f5f5;strokeColor=#666666;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="420" y="420" width="340" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="9" value="<b>Key Results: Arithmetic</b><br/>• PaLM 540B + CoT: <b>56.9%</b> on GSM8K<br/> (vs 17.9% standard)<br/>• Surpassed finetuned GPT-3 (55%)<br/>• With calculator: <b>58.6%</b>" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="500" width="220" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="10" value="<b>Key Results: Commonsense</b><br/>• StrategyQA: <b>75.6%</b><br/> (vs 69.4% SOTA)<br/>• Sports Understanding: <b>95.4%</b><br/> (vs 84% human)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="280" y="500" width="220" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="11" value="<b>Key Results: Symbolic</b><br/>• OOD Generalization<br/>• Coin Flip: Trained on 2 flips<br/> Works on 3-4 flips with CoT<br/>• Standard prompting fails" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="540" y="500" width="220" height="100" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="12" value="<b>Emergent Ability of Scale</b><br/>• Small models (<10B): No benefit, often harmful<br/>• Large models (100B+): Reasoning emerges<br/>• CoT gains increase dramatically with scale" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="620" width="340" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="13" value="<b>Ablation Studies</b><br/>1. Equation only: Worse than CoT<br/>2. Variable compute (...): No improvement<br/>3. Answer first, then reasoning: Same as baseline<br/>→ Content matters, not just extra tokens" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="420" y="620" width="340" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="14" value="<b>Error Analysis</b><br/>• Semantic understanding errors<br/>• One-step missing errors<br/>• Calculation errors<br/>• Larger models reduce semantic/missing-step errors" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="40" y="720" width="340" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="15" value="<b>Conclusion</b><br/>• CoT unlocks reasoning potential<br/>• Simple paradigm: "show your work"<br/>• Emergent capability of large models<br/>• No specialized architecture needed" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
|
||||||
|
<mxGeometry x="420" y="720" width="340" height="80" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="16" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="3" target="5">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="17" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="4" target="6">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="18" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="5" target="7">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="19" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="6" target="8">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="20" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.25;entryY=0;" edge="1" parent="1" source="7" target="9">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="21" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="7" target="10">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="22" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.75;entryY=0;" edge="1" parent="1" source="7" target="11">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="23" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="9" target="12">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="24" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="10" target="13">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="25" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="11" target="14">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="26" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="12" target="15">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="27" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="13" target="15">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>
|
||||||
|
<mxCell id="28" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;entryX=0.5;entryY=0;" edge="1" parent="1" source="14" target="15">
|
||||||
|
<mxGeometry relative="1" as="geometry"/>
|
||||||
|
</mxCell>`,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DevXmlSimulatorProps {
|
||||||
|
setMessages: React.Dispatch<React.SetStateAction<any[]>>
|
||||||
|
onDisplayChart: (xml: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DevXmlSimulator({
|
||||||
|
setMessages,
|
||||||
|
onDisplayChart,
|
||||||
|
}: DevXmlSimulatorProps) {
|
||||||
|
const [devXml, setDevXml] = useState("")
|
||||||
|
const [isSimulating, setIsSimulating] = useState(false)
|
||||||
|
const [devIntervalMs, setDevIntervalMs] = useState(1)
|
||||||
|
const [devChunkSize, setDevChunkSize] = useState(10)
|
||||||
|
const devStopRef = useRef(false)
|
||||||
|
const devXmlInitializedRef = useRef(false)
|
||||||
|
|
||||||
|
// Restore dev XML from localStorage on mount (after hydration)
|
||||||
|
useEffect(() => {
|
||||||
|
const saved = localStorage.getItem("dev-xml-simulator")
|
||||||
|
if (saved) setDevXml(saved)
|
||||||
|
devXmlInitializedRef.current = true
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Save dev XML to localStorage (only after initial load)
|
||||||
|
useEffect(() => {
|
||||||
|
if (devXmlInitializedRef.current) {
|
||||||
|
localStorage.setItem("dev-xml-simulator", devXml)
|
||||||
|
}
|
||||||
|
}, [devXml])
|
||||||
|
|
||||||
|
const handleDevSimulate = async () => {
|
||||||
|
if (!devXml.trim() || isSimulating) return
|
||||||
|
|
||||||
|
setIsSimulating(true)
|
||||||
|
devStopRef.current = false
|
||||||
|
const toolCallId = `dev-sim-${Date.now()}`
|
||||||
|
const xml = devXml.trim()
|
||||||
|
|
||||||
|
// Add user message and initial assistant message with empty XML
|
||||||
|
const userMsg = {
|
||||||
|
id: `user-${Date.now()}`,
|
||||||
|
role: "user" as const,
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
type: "text" as const,
|
||||||
|
text: "[Dev] Simulating XML streaming",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
const assistantMsg = {
|
||||||
|
id: `assistant-${Date.now()}`,
|
||||||
|
role: "assistant" as const,
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
type: "tool-display_diagram" as const,
|
||||||
|
toolCallId,
|
||||||
|
state: "input-streaming" as const,
|
||||||
|
input: { xml: "" },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
setMessages((prev) => [...prev, userMsg, assistantMsg] as any)
|
||||||
|
|
||||||
|
// Stream characters progressively
|
||||||
|
for (let i = 0; i < xml.length; i += devChunkSize) {
|
||||||
|
if (devStopRef.current) {
|
||||||
|
setIsSimulating(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunk = xml.slice(0, i + devChunkSize)
|
||||||
|
|
||||||
|
setMessages((prev) => {
|
||||||
|
const updated = [...prev]
|
||||||
|
const lastMsg = updated[updated.length - 1] as any
|
||||||
|
if (lastMsg?.role === "assistant" && lastMsg.parts?.[0]) {
|
||||||
|
lastMsg.parts[0].input = { xml: chunk }
|
||||||
|
}
|
||||||
|
return updated
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, devIntervalMs))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devStopRef.current) {
|
||||||
|
setIsSimulating(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finalize: set state to output-available
|
||||||
|
setMessages((prev) => {
|
||||||
|
const updated = [...prev]
|
||||||
|
const lastMsg = updated[updated.length - 1] as any
|
||||||
|
if (lastMsg?.role === "assistant" && lastMsg.parts?.[0]) {
|
||||||
|
lastMsg.parts[0].state = "output-available"
|
||||||
|
lastMsg.parts[0].output = "Successfully displayed the diagram."
|
||||||
|
lastMsg.parts[0].input = { xml }
|
||||||
|
}
|
||||||
|
return updated
|
||||||
|
})
|
||||||
|
|
||||||
|
// Display the final diagram
|
||||||
|
const fullXml = wrapWithMxFile(xml)
|
||||||
|
onDisplayChart(fullXml)
|
||||||
|
|
||||||
|
setIsSimulating(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="border-t border-dashed border-orange-500/50 px-4 py-2 bg-orange-50/50 dark:bg-orange-950/30">
|
||||||
|
<details>
|
||||||
|
<summary className="text-xs text-orange-600 dark:text-orange-400 cursor-pointer font-medium">
|
||||||
|
Dev: XML Streaming Simulator
|
||||||
|
</summary>
|
||||||
|
<div className="mt-2 space-y-2">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<label className="text-xs text-muted-foreground whitespace-nowrap">
|
||||||
|
Preset:
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
onChange={(e) => {
|
||||||
|
if (e.target.value) {
|
||||||
|
setDevXml(DEV_XML_PRESETS[e.target.value])
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
className="flex-1 text-xs p-1 border rounded bg-background"
|
||||||
|
defaultValue=""
|
||||||
|
>
|
||||||
|
<option value="" disabled>
|
||||||
|
Select a preset...
|
||||||
|
</option>
|
||||||
|
{Object.keys(DEV_XML_PRESETS).map((name) => (
|
||||||
|
<option key={name} value={name}>
|
||||||
|
{name}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setDevXml("")}
|
||||||
|
className="px-2 py-1 text-xs text-muted-foreground hover:text-foreground border rounded"
|
||||||
|
>
|
||||||
|
Clear
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<textarea
|
||||||
|
value={devXml}
|
||||||
|
onChange={(e) => setDevXml(e.target.value)}
|
||||||
|
placeholder="Paste mxCell XML here or select a preset..."
|
||||||
|
className="w-full h-24 text-xs font-mono p-2 border rounded bg-background"
|
||||||
|
/>
|
||||||
|
<div className="flex items-center gap-4">
|
||||||
|
<div className="flex items-center gap-2 flex-1">
|
||||||
|
<label className="text-xs text-muted-foreground whitespace-nowrap">
|
||||||
|
Interval:
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min="1"
|
||||||
|
max="200"
|
||||||
|
step="1"
|
||||||
|
value={devIntervalMs}
|
||||||
|
onChange={(e) =>
|
||||||
|
setDevIntervalMs(Number(e.target.value))
|
||||||
|
}
|
||||||
|
className="flex-1 h-1 accent-orange-500"
|
||||||
|
/>
|
||||||
|
<span className="text-xs text-muted-foreground w-12">
|
||||||
|
{devIntervalMs}ms
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<label className="text-xs text-muted-foreground whitespace-nowrap">
|
||||||
|
Chars:
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
min="1"
|
||||||
|
max="100"
|
||||||
|
value={devChunkSize}
|
||||||
|
onChange={(e) =>
|
||||||
|
setDevChunkSize(
|
||||||
|
Math.max(1, Number(e.target.value)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
className="w-14 text-xs p-1 border rounded bg-background"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={handleDevSimulate}
|
||||||
|
disabled={isSimulating || !devXml.trim()}
|
||||||
|
className="px-3 py-1 text-xs bg-orange-500 text-white rounded hover:bg-orange-600 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
{isSimulating
|
||||||
|
? "Streaming..."
|
||||||
|
: `Simulate (${devChunkSize} chars/${devIntervalMs}ms)`}
|
||||||
|
</button>
|
||||||
|
{isSimulating && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
devStopRef.current = true
|
||||||
|
}}
|
||||||
|
className="px-3 py-1 text-xs bg-red-500 text-white rounded hover:bg-red-600"
|
||||||
|
>
|
||||||
|
Stop
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</details>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -412,11 +412,7 @@ export function ModelConfigDialog({
|
|||||||
setSelectedProviderId(
|
setSelectedProviderId(
|
||||||
provider.id,
|
provider.id,
|
||||||
)
|
)
|
||||||
setValidationStatus(
|
setValidationStatus("idle")
|
||||||
provider.validated
|
|
||||||
? "success"
|
|
||||||
: "idle",
|
|
||||||
)
|
|
||||||
setShowApiKey(false)
|
setShowApiKey(false)
|
||||||
}}
|
}}
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -504,7 +500,7 @@ export function ModelConfigDialog({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Provider Details (Right Panel) */}
|
{/* Provider Details (Right Panel) */}
|
||||||
<div className="flex-1 min-w-0 flex flex-col overflow-hidden">
|
<div className="flex-1 min-w-0 flex flex-col overflow-auto [&::-webkit-scrollbar]:hidden ">
|
||||||
{selectedProvider ? (
|
{selectedProvider ? (
|
||||||
<>
|
<>
|
||||||
<ScrollArea className="flex-1" ref={scrollRef}>
|
<ScrollArea className="flex-1" ref={scrollRef}>
|
||||||
@@ -555,6 +551,20 @@ export function ModelConfigDialog({
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() =>
|
||||||
|
setDeleteConfirmOpen(true)
|
||||||
|
}
|
||||||
|
className="text-destructive hover:text-destructive hover:bg-destructive/10"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4 mr-1.5" />
|
||||||
|
{
|
||||||
|
dict.modelConfig
|
||||||
|
.deleteProvider
|
||||||
|
}
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Configuration Section */}
|
{/* Configuration Section */}
|
||||||
@@ -1416,24 +1426,6 @@ export function ModelConfigDialog({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</ConfigSection>
|
</ConfigSection>
|
||||||
|
|
||||||
{/* Danger Zone */}
|
|
||||||
<div className="pt-4">
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="sm"
|
|
||||||
onClick={() =>
|
|
||||||
setDeleteConfirmOpen(true)
|
|
||||||
}
|
|
||||||
className="text-muted-foreground hover:text-destructive hover:bg-destructive/5 rounded-xl"
|
|
||||||
>
|
|
||||||
<Trash2 className="h-4 w-4 mr-2" />
|
|
||||||
{
|
|
||||||
dict.modelConfig
|
|
||||||
.deleteProvider
|
|
||||||
}
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ export function ModelSelector({
|
|||||||
<ModelSelectorInput
|
<ModelSelectorInput
|
||||||
placeholder={dict.modelConfig.searchModels}
|
placeholder={dict.modelConfig.searchModels}
|
||||||
/>
|
/>
|
||||||
<ModelSelectorList>
|
<ModelSelectorList className="[&::-webkit-scrollbar]:hidden [-ms-overflow-style:none] [scrollbar-width:none]">
|
||||||
<ModelSelectorEmpty>
|
<ModelSelectorEmpty>
|
||||||
{validatedModels.length === 0 && models.length > 0
|
{validatedModels.length === 0 && models.length > 0
|
||||||
? dict.modelConfig.noVerifiedModels
|
? dict.modelConfig.noVerifiedModels
|
||||||
|
|||||||
@@ -151,6 +151,9 @@ function SettingsContent({
|
|||||||
}, [open])
|
}, [open])
|
||||||
|
|
||||||
const changeLanguage = (lang: string) => {
|
const changeLanguage = (lang: string) => {
|
||||||
|
// Save locale to localStorage for persistence across restarts
|
||||||
|
localStorage.setItem("next-ai-draw-io-locale", lang)
|
||||||
|
|
||||||
const parts = pathname.split("/")
|
const parts = pathname.split("/")
|
||||||
if (parts.length > 1 && i18n.locales.includes(parts[1] as Locale)) {
|
if (parts.length > 1 && i18n.locales.includes(parts[1] as Locale)) {
|
||||||
parts[1] = lang
|
parts[1] = lang
|
||||||
|
|||||||
@@ -3,16 +3,16 @@ import { app } from "electron"
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Port configuration
|
* Port configuration
|
||||||
|
* Using fixed ports to preserve localStorage across restarts
|
||||||
|
* (localStorage is origin-specific, so changing ports loses all saved data)
|
||||||
*/
|
*/
|
||||||
const PORT_CONFIG = {
|
const PORT_CONFIG = {
|
||||||
// Development mode uses fixed port for hot reload compatibility
|
// Development mode uses fixed port for hot reload compatibility
|
||||||
development: 6002,
|
development: 6002,
|
||||||
// Production mode port range (will find first available)
|
// Production mode uses fixed port (61337) to preserve localStorage
|
||||||
production: {
|
// Falls back to sequential ports if unavailable
|
||||||
min: 10000,
|
production: 61337,
|
||||||
max: 65535,
|
// Maximum attempts to find an available port (fallback)
|
||||||
},
|
|
||||||
// Maximum attempts to find an available port
|
|
||||||
maxAttempts: 100,
|
maxAttempts: 100,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +36,11 @@ export function isPortAvailable(port: number): Promise<boolean> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a random port within the production range
|
|
||||||
*/
|
|
||||||
function getRandomPort(): number {
|
|
||||||
const { min, max } = PORT_CONFIG.production
|
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find an available port
|
* Find an available port
|
||||||
* - In development: uses fixed port (6002)
|
* - In development: uses fixed port (6002)
|
||||||
* - In production: finds a random available port
|
* - In production: uses fixed port (61337) to preserve localStorage
|
||||||
* - If a port was previously allocated, verifies it's still available
|
* - Falls back to sequential ports if preferred port is unavailable
|
||||||
*
|
*
|
||||||
* @param reuseExisting If true, try to reuse the previously allocated port
|
* @param reuseExisting If true, try to reuse the previously allocated port
|
||||||
* @returns Promise<number> The available port
|
* @returns Promise<number> The available port
|
||||||
@@ -56,6 +48,9 @@ function getRandomPort(): number {
|
|||||||
*/
|
*/
|
||||||
export async function findAvailablePort(reuseExisting = true): Promise<number> {
|
export async function findAvailablePort(reuseExisting = true): Promise<number> {
|
||||||
const isDev = !app.isPackaged
|
const isDev = !app.isPackaged
|
||||||
|
const preferredPort = isDev
|
||||||
|
? PORT_CONFIG.development
|
||||||
|
: PORT_CONFIG.production
|
||||||
|
|
||||||
// Try to reuse cached port if requested and available
|
// Try to reuse cached port if requested and available
|
||||||
if (reuseExisting && allocatedPort !== null) {
|
if (reuseExisting && allocatedPort !== null) {
|
||||||
@@ -69,29 +64,22 @@ export async function findAvailablePort(reuseExisting = true): Promise<number> {
|
|||||||
allocatedPort = null
|
allocatedPort = null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isDev) {
|
// Try preferred port first
|
||||||
// Development mode: use fixed port
|
if (await isPortAvailable(preferredPort)) {
|
||||||
const port = PORT_CONFIG.development
|
allocatedPort = preferredPort
|
||||||
const available = await isPortAvailable(port)
|
return preferredPort
|
||||||
if (available) {
|
|
||||||
allocatedPort = port
|
|
||||||
return port
|
|
||||||
}
|
|
||||||
console.warn(
|
|
||||||
`Development port ${port} is in use, finding alternative...`,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Production mode or dev port unavailable: find random available port
|
console.warn(
|
||||||
for (let attempt = 0; attempt < PORT_CONFIG.maxAttempts; attempt++) {
|
`Preferred port ${preferredPort} is in use, finding alternative...`,
|
||||||
const port = isDev
|
)
|
||||||
? PORT_CONFIG.development + attempt + 1
|
|
||||||
: getRandomPort()
|
|
||||||
|
|
||||||
const available = await isPortAvailable(port)
|
// Fallback: try sequential ports starting from preferred + 1
|
||||||
if (available) {
|
for (let attempt = 1; attempt <= PORT_CONFIG.maxAttempts; attempt++) {
|
||||||
|
const port = preferredPort + attempt
|
||||||
|
if (await isPortAvailable(port)) {
|
||||||
allocatedPort = port
|
allocatedPort = port
|
||||||
console.log(`Allocated port: ${port}`)
|
console.log(`Allocated fallback port: ${port}`)
|
||||||
return port
|
return port
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,12 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
<div class="deprecation-notice">
|
||||||
|
<strong>⚠️ Deprecation Notice</strong>
|
||||||
|
<p>This settings panel will be removed in a future update.</p>
|
||||||
|
<p>Please use the <strong>AI Model Configuration</strong> button (left of the Send button in the chat panel) to configure your AI providers. Your settings there will persist across updates.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
<h1>Configuration Presets</h1>
|
<h1>Configuration Presets</h1>
|
||||||
|
|
||||||
<div class="section">
|
<div class="section">
|
||||||
|
|||||||
@@ -24,6 +24,39 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.deprecation-notice {
|
||||||
|
background-color: #fff3cd;
|
||||||
|
border: 1px solid #ffc107;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 16px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deprecation-notice strong {
|
||||||
|
color: #856404;
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deprecation-notice p {
|
||||||
|
color: #856404;
|
||||||
|
font-size: 13px;
|
||||||
|
margin: 4px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
.deprecation-notice {
|
||||||
|
background-color: #332701;
|
||||||
|
border-color: #665200;
|
||||||
|
}
|
||||||
|
|
||||||
|
.deprecation-notice strong,
|
||||||
|
.deprecation-notice p {
|
||||||
|
color: #ffc107;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
* {
|
* {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
|||||||
383
hooks/use-diagram-tool-handlers.ts
Normal file
383
hooks/use-diagram-tool-handlers.ts
Normal file
@@ -0,0 +1,383 @@
|
|||||||
|
import type { MutableRefObject } from "react"
|
||||||
|
import { isMxCellXmlComplete, wrapWithMxFile } from "@/lib/utils"
|
||||||
|
|
||||||
|
const DEBUG = process.env.NODE_ENV === "development"
|
||||||
|
|
||||||
|
interface ToolCall {
|
||||||
|
toolCallId: string
|
||||||
|
toolName: string
|
||||||
|
input: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
type AddToolOutputSuccess = {
|
||||||
|
tool: string
|
||||||
|
toolCallId: string
|
||||||
|
state?: "output-available"
|
||||||
|
output: string
|
||||||
|
errorText?: undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
type AddToolOutputError = {
|
||||||
|
tool: string
|
||||||
|
toolCallId: string
|
||||||
|
state: "output-error"
|
||||||
|
output?: undefined
|
||||||
|
errorText: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AddToolOutputParams = AddToolOutputSuccess | AddToolOutputError
|
||||||
|
|
||||||
|
type AddToolOutputFn = (params: AddToolOutputParams) => void
|
||||||
|
|
||||||
|
interface DiagramOperation {
|
||||||
|
operation: "update" | "add" | "delete"
|
||||||
|
cell_id: string
|
||||||
|
new_xml?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UseDiagramToolHandlersParams {
|
||||||
|
partialXmlRef: MutableRefObject<string>
|
||||||
|
editDiagramOriginalXmlRef: MutableRefObject<Map<string, string>>
|
||||||
|
chartXMLRef: MutableRefObject<string>
|
||||||
|
onDisplayChart: (xml: string, skipValidation?: boolean) => string | null
|
||||||
|
onFetchChart: (saveToHistory?: boolean) => Promise<string>
|
||||||
|
onExport: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook that creates the onToolCall handler for diagram-related tools.
|
||||||
|
* Handles display_diagram, edit_diagram, and append_diagram tools.
|
||||||
|
*
|
||||||
|
* Note: addToolOutput is passed at call time (not hook init) because
|
||||||
|
* it comes from useChat which creates a circular dependency.
|
||||||
|
*/
|
||||||
|
export function useDiagramToolHandlers({
|
||||||
|
partialXmlRef,
|
||||||
|
editDiagramOriginalXmlRef,
|
||||||
|
chartXMLRef,
|
||||||
|
onDisplayChart,
|
||||||
|
onFetchChart,
|
||||||
|
onExport,
|
||||||
|
}: UseDiagramToolHandlersParams) {
|
||||||
|
const handleToolCall = async (
|
||||||
|
{ toolCall }: { toolCall: ToolCall },
|
||||||
|
addToolOutput: AddToolOutputFn,
|
||||||
|
) => {
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
`[onToolCall] Tool: ${toolCall.toolName}, CallId: ${toolCall.toolCallId}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toolCall.toolName === "display_diagram") {
|
||||||
|
await handleDisplayDiagram(toolCall, addToolOutput)
|
||||||
|
} else if (toolCall.toolName === "edit_diagram") {
|
||||||
|
await handleEditDiagram(toolCall, addToolOutput)
|
||||||
|
} else if (toolCall.toolName === "append_diagram") {
|
||||||
|
handleAppendDiagram(toolCall, addToolOutput)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDisplayDiagram = async (
|
||||||
|
toolCall: ToolCall,
|
||||||
|
addToolOutput: AddToolOutputFn,
|
||||||
|
) => {
|
||||||
|
const { xml } = toolCall.input as { xml: string }
|
||||||
|
|
||||||
|
// DEBUG: Log raw input to diagnose false truncation detection
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"[display_diagram] XML ending (last 100 chars):",
|
||||||
|
xml.slice(-100),
|
||||||
|
)
|
||||||
|
console.log("[display_diagram] XML length:", xml.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if XML is truncated (incomplete mxCell indicates truncated output)
|
||||||
|
const isTruncated = !isMxCellXmlComplete(xml)
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("[display_diagram] isTruncated:", isTruncated)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTruncated) {
|
||||||
|
// Store the partial XML for continuation via append_diagram
|
||||||
|
partialXmlRef.current = xml
|
||||||
|
|
||||||
|
// Tell LLM to use append_diagram to continue
|
||||||
|
const partialEnding = partialXmlRef.current.slice(-500)
|
||||||
|
addToolOutput({
|
||||||
|
tool: "display_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `Output was truncated due to length limits. Use the append_diagram tool to continue.
|
||||||
|
|
||||||
|
Your output ended with:
|
||||||
|
\`\`\`
|
||||||
|
${partialEnding}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
NEXT STEP: Call append_diagram with the continuation XML.
|
||||||
|
- Do NOT include wrapper tags or root cells (id="0", id="1")
|
||||||
|
- Start from EXACTLY where you stopped
|
||||||
|
- Complete all remaining mxCell elements`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Complete XML received - use it directly
|
||||||
|
// (continuation is now handled via append_diagram tool)
|
||||||
|
const finalXml = xml
|
||||||
|
partialXmlRef.current = "" // Reset any partial from previous truncation
|
||||||
|
|
||||||
|
// Wrap raw XML with full mxfile structure for draw.io
|
||||||
|
const fullXml = wrapWithMxFile(finalXml)
|
||||||
|
|
||||||
|
// loadDiagram validates and returns error if invalid
|
||||||
|
const validationError = onDisplayChart(fullXml)
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
console.warn("[display_diagram] Validation error:", validationError)
|
||||||
|
// Return error to model - sendAutomaticallyWhen will trigger retry
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"[display_diagram] Adding tool output with state: output-error",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
addToolOutput({
|
||||||
|
tool: "display_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `${validationError}
|
||||||
|
|
||||||
|
Please fix the XML issues and call display_diagram again with corrected XML.
|
||||||
|
|
||||||
|
Your failed XML:
|
||||||
|
\`\`\`xml
|
||||||
|
${finalXml}
|
||||||
|
\`\`\``,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Success - diagram will be rendered by chat-message-display
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"[display_diagram] Success! Adding tool output with state: output-available",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
addToolOutput({
|
||||||
|
tool: "display_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
output: "Successfully displayed the diagram.",
|
||||||
|
})
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"[display_diagram] Tool output added. Diagram should be visible now.",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleEditDiagram = async (
|
||||||
|
toolCall: ToolCall,
|
||||||
|
addToolOutput: AddToolOutputFn,
|
||||||
|
) => {
|
||||||
|
const { operations } = toolCall.input as {
|
||||||
|
operations: DiagramOperation[]
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentXml = ""
|
||||||
|
try {
|
||||||
|
// Use the original XML captured during streaming (shared with chat-message-display)
|
||||||
|
// This ensures we apply operations to the same base XML that streaming used
|
||||||
|
const originalXml = editDiagramOriginalXmlRef.current.get(
|
||||||
|
toolCall.toolCallId,
|
||||||
|
)
|
||||||
|
if (originalXml) {
|
||||||
|
currentXml = originalXml
|
||||||
|
} else {
|
||||||
|
// Fallback: use chartXML from ref if streaming didn't capture original
|
||||||
|
const cachedXML = chartXMLRef.current
|
||||||
|
if (cachedXML) {
|
||||||
|
currentXml = cachedXML
|
||||||
|
} else {
|
||||||
|
// Last resort: export from iframe
|
||||||
|
currentXml = await onFetchChart(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { applyDiagramOperations } = await import("@/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
|
||||||
|
const validationError = onDisplayChart(editedXml)
|
||||||
|
if (validationError) {
|
||||||
|
console.warn(
|
||||||
|
"[edit_diagram] Validation error:",
|
||||||
|
validationError,
|
||||||
|
)
|
||||||
|
addToolOutput({
|
||||||
|
tool: "edit_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `Edit produced invalid XML: ${validationError}
|
||||||
|
|
||||||
|
Current diagram XML:
|
||||||
|
\`\`\`xml
|
||||||
|
${currentXml}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Please fix the operations to avoid structural issues.`,
|
||||||
|
})
|
||||||
|
// Clean up the shared original XML ref
|
||||||
|
editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
onExport()
|
||||||
|
addToolOutput({
|
||||||
|
tool: "edit_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
output: `Successfully applied ${operations.length} operation(s) to the diagram.`,
|
||||||
|
})
|
||||||
|
// Clean up the shared original XML ref
|
||||||
|
editDiagramOriginalXmlRef.current.delete(toolCall.toolCallId)
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[edit_diagram] Failed:", error)
|
||||||
|
|
||||||
|
const errorMessage =
|
||||||
|
error instanceof Error ? error.message : String(error)
|
||||||
|
|
||||||
|
addToolOutput({
|
||||||
|
tool: "edit_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `Edit failed: ${errorMessage}
|
||||||
|
|
||||||
|
Current diagram XML:
|
||||||
|
\`\`\`xml
|
||||||
|
${currentXml || "No XML available"}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleAppendDiagram = (
|
||||||
|
toolCall: ToolCall,
|
||||||
|
addToolOutput: AddToolOutputFn,
|
||||||
|
) => {
|
||||||
|
const { xml } = toolCall.input as { xml: string }
|
||||||
|
|
||||||
|
// Detect if LLM incorrectly started fresh instead of continuing
|
||||||
|
// LLM should only output bare mxCells now, so wrapper tags indicate error
|
||||||
|
const trimmed = xml.trim()
|
||||||
|
const isFreshStart =
|
||||||
|
trimmed.startsWith("<mxGraphModel") ||
|
||||||
|
trimmed.startsWith("<root") ||
|
||||||
|
trimmed.startsWith("<mxfile") ||
|
||||||
|
trimmed.startsWith('<mxCell id="0"') ||
|
||||||
|
trimmed.startsWith('<mxCell id="1"')
|
||||||
|
|
||||||
|
if (isFreshStart) {
|
||||||
|
addToolOutput({
|
||||||
|
tool: "append_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `ERROR: You started fresh with wrapper tags. Do NOT include wrapper tags or root cells (id="0", id="1").
|
||||||
|
|
||||||
|
Continue from EXACTLY where the partial ended:
|
||||||
|
\`\`\`
|
||||||
|
${partialXmlRef.current.slice(-500)}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Start your continuation with the NEXT character after where it stopped.`,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append to accumulated XML
|
||||||
|
partialXmlRef.current += xml
|
||||||
|
|
||||||
|
// Check if XML is now complete (last mxCell is complete)
|
||||||
|
const isComplete = isMxCellXmlComplete(partialXmlRef.current)
|
||||||
|
|
||||||
|
if (isComplete) {
|
||||||
|
// Wrap and display the complete diagram
|
||||||
|
const finalXml = partialXmlRef.current
|
||||||
|
partialXmlRef.current = "" // Reset
|
||||||
|
|
||||||
|
const fullXml = wrapWithMxFile(finalXml)
|
||||||
|
const validationError = onDisplayChart(fullXml)
|
||||||
|
|
||||||
|
if (validationError) {
|
||||||
|
addToolOutput({
|
||||||
|
tool: "append_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `Validation error after assembly: ${validationError}
|
||||||
|
|
||||||
|
Assembled XML:
|
||||||
|
\`\`\`xml
|
||||||
|
${finalXml.substring(0, 2000)}...
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Please use display_diagram with corrected XML.`,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
addToolOutput({
|
||||||
|
tool: "append_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
output: "Diagram assembly complete and displayed successfully.",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Still incomplete - signal to continue
|
||||||
|
addToolOutput({
|
||||||
|
tool: "append_diagram",
|
||||||
|
toolCallId: toolCall.toolCallId,
|
||||||
|
state: "output-error",
|
||||||
|
errorText: `XML still incomplete (mxCell not closed). Call append_diagram again to continue.
|
||||||
|
|
||||||
|
Current ending:
|
||||||
|
\`\`\`
|
||||||
|
${partialXmlRef.current.slice(-500)}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
Continue from EXACTLY where you stopped.`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { handleToolCall }
|
||||||
|
}
|
||||||
@@ -14,22 +14,14 @@ export function register() {
|
|||||||
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
||||||
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
||||||
baseUrl: process.env.LANGFUSE_BASEURL,
|
baseUrl: process.env.LANGFUSE_BASEURL,
|
||||||
// Filter out Next.js HTTP request spans so AI SDK spans become root traces
|
// Whitelist approach: only export AI-related spans
|
||||||
shouldExportSpan: ({ otelSpan }) => {
|
shouldExportSpan: ({ otelSpan }) => {
|
||||||
const spanName = otelSpan.name
|
const spanName = otelSpan.name
|
||||||
// Skip Next.js HTTP infrastructure spans
|
// Only export AI SDK spans (ai.*) and our explicit "chat" wrapper
|
||||||
if (
|
if (spanName === "chat" || spanName.startsWith("ai.")) {
|
||||||
spanName.startsWith("POST") ||
|
return true
|
||||||
spanName.startsWith("GET") ||
|
|
||||||
spanName.startsWith("RSC") ||
|
|
||||||
spanName.includes("BaseServer") ||
|
|
||||||
spanName.includes("handleRequest") ||
|
|
||||||
spanName.includes("resolve page") ||
|
|
||||||
spanName.includes("start response")
|
|
||||||
) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
return true
|
return false
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,30 @@ import {
|
|||||||
// OSS users who don't need quota tracking can simply not set this env var
|
// OSS users who don't need quota tracking can simply not set this env var
|
||||||
const TABLE = process.env.DYNAMODB_QUOTA_TABLE
|
const TABLE = process.env.DYNAMODB_QUOTA_TABLE
|
||||||
const DYNAMODB_REGION = process.env.DYNAMODB_REGION || "ap-northeast-1"
|
const DYNAMODB_REGION = process.env.DYNAMODB_REGION || "ap-northeast-1"
|
||||||
|
// Timezone for daily quota reset (e.g., "Asia/Tokyo" for JST midnight reset)
|
||||||
|
// Defaults to UTC if not set
|
||||||
|
let QUOTA_TIMEZONE = process.env.QUOTA_TIMEZONE || "UTC"
|
||||||
|
|
||||||
|
// Validate timezone at module load
|
||||||
|
try {
|
||||||
|
new Intl.DateTimeFormat("en-CA", { timeZone: QUOTA_TIMEZONE }).format(
|
||||||
|
new Date(),
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
console.warn(
|
||||||
|
`[quota] Invalid QUOTA_TIMEZONE "${QUOTA_TIMEZONE}", using UTC`,
|
||||||
|
)
|
||||||
|
QUOTA_TIMEZONE = "UTC"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get today's date string in the configured timezone (YYYY-MM-DD format)
|
||||||
|
*/
|
||||||
|
function getTodayInTimezone(): string {
|
||||||
|
return new Intl.DateTimeFormat("en-CA", {
|
||||||
|
timeZone: QUOTA_TIMEZONE,
|
||||||
|
}).format(new Date())
|
||||||
|
}
|
||||||
|
|
||||||
// Only create client if quota is enabled
|
// Only create client if quota is enabled
|
||||||
const client = TABLE ? new DynamoDBClient({ region: DYNAMODB_REGION }) : null
|
const client = TABLE ? new DynamoDBClient({ region: DYNAMODB_REGION }) : null
|
||||||
@@ -49,32 +73,67 @@ export async function checkAndIncrementRequest(
|
|||||||
return { allowed: true }
|
return { allowed: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
const today = new Date().toISOString().split("T")[0]
|
const today = getTodayInTimezone()
|
||||||
const currentMinute = Math.floor(Date.now() / 60000).toString()
|
const currentMinute = Math.floor(Date.now() / 60000).toString()
|
||||||
const ttl = Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60
|
const ttl = Math.floor(Date.now() / 1000) + 7 * 24 * 60 * 60
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Atomic check-and-increment with ConditionExpression
|
// First, try to reset counts if it's a new day (atomic day reset)
|
||||||
// This prevents race conditions by failing if limits are exceeded
|
// This will succeed only if lastResetDate < today or doesn't exist
|
||||||
|
try {
|
||||||
|
await client.send(
|
||||||
|
new UpdateItemCommand({
|
||||||
|
TableName: TABLE,
|
||||||
|
Key: { PK: { S: `IP#${ip}` } },
|
||||||
|
// Reset all counts to 1/0 for the new day
|
||||||
|
UpdateExpression: `
|
||||||
|
SET lastResetDate = :today,
|
||||||
|
dailyReqCount = :one,
|
||||||
|
dailyTokenCount = :zero,
|
||||||
|
lastMinute = :minute,
|
||||||
|
tpmCount = :zero,
|
||||||
|
#ttl = :ttl
|
||||||
|
`,
|
||||||
|
// Only succeed if it's a new day (or new item)
|
||||||
|
ConditionExpression: `
|
||||||
|
attribute_not_exists(lastResetDate) OR lastResetDate < :today
|
||||||
|
`,
|
||||||
|
ExpressionAttributeNames: { "#ttl": "ttl" },
|
||||||
|
ExpressionAttributeValues: {
|
||||||
|
":today": { S: today },
|
||||||
|
":zero": { N: "0" },
|
||||||
|
":one": { N: "1" },
|
||||||
|
":minute": { S: currentMinute },
|
||||||
|
":ttl": { N: String(ttl) },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
// New day reset successful
|
||||||
|
return { allowed: true }
|
||||||
|
} catch (resetError: any) {
|
||||||
|
// If condition failed, it's the same day - continue to increment logic
|
||||||
|
if (!(resetError instanceof ConditionalCheckFailedException)) {
|
||||||
|
throw resetError // Re-throw unexpected errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Same day - increment request count with limit checks
|
||||||
await client.send(
|
await client.send(
|
||||||
new UpdateItemCommand({
|
new UpdateItemCommand({
|
||||||
TableName: TABLE,
|
TableName: TABLE,
|
||||||
Key: { PK: { S: `IP#${ip}` } },
|
Key: { PK: { S: `IP#${ip}` } },
|
||||||
// Reset counts if new day/minute, then increment request count
|
// Increment request count, handle minute boundary for TPM
|
||||||
UpdateExpression: `
|
UpdateExpression: `
|
||||||
SET lastResetDate = :today,
|
SET lastMinute = :minute,
|
||||||
dailyReqCount = if_not_exists(dailyReqCount, :zero) + :one,
|
|
||||||
dailyTokenCount = if_not_exists(dailyTokenCount, :zero),
|
|
||||||
lastMinute = :minute,
|
|
||||||
tpmCount = if_not_exists(tpmCount, :zero),
|
tpmCount = if_not_exists(tpmCount, :zero),
|
||||||
#ttl = :ttl
|
#ttl = :ttl
|
||||||
|
ADD dailyReqCount :one
|
||||||
`,
|
`,
|
||||||
// Atomic condition: only succeed if ALL limits pass
|
// Check all limits before allowing increment
|
||||||
// Uses attribute_not_exists for new items, then checks limits for existing items
|
|
||||||
ConditionExpression: `
|
ConditionExpression: `
|
||||||
(attribute_not_exists(lastResetDate) OR lastResetDate < :today OR
|
lastResetDate = :today AND
|
||||||
((attribute_not_exists(dailyReqCount) OR dailyReqCount < :reqLimit) AND
|
(attribute_not_exists(dailyReqCount) OR dailyReqCount < :reqLimit) AND
|
||||||
(attribute_not_exists(dailyTokenCount) OR dailyTokenCount < :tokenLimit))) AND
|
(attribute_not_exists(dailyTokenCount) OR dailyTokenCount < :tokenLimit) AND
|
||||||
(attribute_not_exists(lastMinute) OR lastMinute <> :minute OR
|
(attribute_not_exists(lastMinute) OR lastMinute <> :minute OR
|
||||||
attribute_not_exists(tpmCount) OR tpmCount < :tpmLimit)
|
attribute_not_exists(tpmCount) OR tpmCount < :tpmLimit)
|
||||||
`,
|
`,
|
||||||
|
|||||||
@@ -99,9 +99,9 @@ When using edit_diagram tool:
|
|||||||
- For update/add: provide cell_id and complete new_xml (full mxCell element including mxGeometry)
|
- For update/add: provide cell_id and complete new_xml (full mxCell element including mxGeometry)
|
||||||
- For delete: only cell_id is needed
|
- For delete: only cell_id is needed
|
||||||
- Find the cell_id from "Current diagram XML" in system context
|
- Find the cell_id from "Current diagram XML" in system context
|
||||||
- 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>"}]}
|
- Example update: {"operations": [{"operation": "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>"}]}
|
||||||
- Example delete: {"operations": [{"type": "delete", "cell_id": "5"}]}
|
- Example delete: {"operations": [{"operation": "delete", "cell_id": "5"}]}
|
||||||
- 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>"}]}
|
- Example add: {"operations": [{"operation": "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>"}]}
|
||||||
|
|
||||||
⚠️ JSON ESCAPING: Every " inside new_xml MUST be escaped as \\". Example: id=\\"5\\" value=\\"Label\\"
|
⚠️ JSON ESCAPING: Every " inside new_xml MUST be escaped as \\". Example: id=\\"5\\" value=\\"Label\\"
|
||||||
|
|
||||||
@@ -282,9 +282,9 @@ edit_diagram uses ID-based operations to modify cells directly by their id attri
|
|||||||
\`\`\`json
|
\`\`\`json
|
||||||
{
|
{
|
||||||
"operations": [
|
"operations": [
|
||||||
{"type": "update", "cell_id": "3", "new_xml": "<mxCell ...complete element...>"},
|
{"operation": "update", "cell_id": "3", "new_xml": "<mxCell ...complete element...>"},
|
||||||
{"type": "add", "cell_id": "new1", "new_xml": "<mxCell ...new element...>"},
|
{"operation": "add", "cell_id": "new1", "new_xml": "<mxCell ...new element...>"},
|
||||||
{"type": "delete", "cell_id": "5"}
|
{"operation": "delete", "cell_id": "5"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
@@ -293,17 +293,17 @@ edit_diagram uses ID-based operations to modify cells directly by their id attri
|
|||||||
|
|
||||||
Change label:
|
Change label:
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
{"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>"}]}
|
{"operations": [{"operation": "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>"}]}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
Add new shape:
|
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>"}]}
|
{"operations": [{"operation": "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>"}]}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
Delete cell:
|
Delete cell:
|
||||||
\`\`\`json
|
\`\`\`json
|
||||||
{"operations": [{"type": "delete", "cell_id": "5"}]}
|
{"operations": [{"operation": "delete", "cell_id": "5"}]}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
**Error Recovery:**
|
**Error Recovery:**
|
||||||
|
|||||||
@@ -83,22 +83,24 @@ export const PROVIDER_INFO: Record<
|
|||||||
// Suggested models per provider for quick add
|
// Suggested models per provider for quick add
|
||||||
export const SUGGESTED_MODELS: Record<ProviderName, string[]> = {
|
export const SUGGESTED_MODELS: Record<ProviderName, string[]> = {
|
||||||
openai: [
|
openai: [
|
||||||
// GPT-4o series (latest)
|
"gpt-5.2-pro",
|
||||||
|
"gpt-5.2-chat-latest",
|
||||||
|
"gpt-5.2",
|
||||||
|
"gpt-5.1-codex-mini",
|
||||||
|
"gpt-5.1-codex",
|
||||||
|
"gpt-5.1-chat-latest",
|
||||||
|
"gpt-5.1",
|
||||||
|
"gpt-5-pro",
|
||||||
|
"gpt-5",
|
||||||
|
"gpt-5-mini",
|
||||||
|
"gpt-5-nano",
|
||||||
|
"gpt-5-codex",
|
||||||
|
"gpt-5-chat-latest",
|
||||||
|
"gpt-4.1",
|
||||||
|
"gpt-4.1-mini",
|
||||||
|
"gpt-4.1-nano",
|
||||||
"gpt-4o",
|
"gpt-4o",
|
||||||
"gpt-4o-mini",
|
"gpt-4o-mini",
|
||||||
"gpt-4o-2024-11-20",
|
|
||||||
// GPT-4 Turbo
|
|
||||||
"gpt-4-turbo",
|
|
||||||
"gpt-4-turbo-preview",
|
|
||||||
// o1/o3 reasoning models
|
|
||||||
"o1",
|
|
||||||
"o1-mini",
|
|
||||||
"o1-preview",
|
|
||||||
"o3-mini",
|
|
||||||
// GPT-4
|
|
||||||
"gpt-4",
|
|
||||||
// GPT-3.5
|
|
||||||
"gpt-3.5-turbo",
|
|
||||||
],
|
],
|
||||||
anthropic: [
|
anthropic: [
|
||||||
// Claude 4.5 series (latest)
|
// Claude 4.5 series (latest)
|
||||||
|
|||||||
12
lib/user-id.ts
Normal file
12
lib/user-id.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* Generate a userId from request for tracking purposes.
|
||||||
|
* Uses base64url encoding of IP for URL-safe identifier.
|
||||||
|
* Note: base64 is reversible - this is NOT privacy protection.
|
||||||
|
*/
|
||||||
|
export function getUserIdFromRequest(req: Request): string {
|
||||||
|
const forwardedFor = req.headers.get("x-forwarded-for")
|
||||||
|
const rawIp = forwardedFor?.split(",")[0]?.trim() || "anonymous"
|
||||||
|
return rawIp === "anonymous"
|
||||||
|
? rawIp
|
||||||
|
: `user-${Buffer.from(rawIp).toString("base64url")}`
|
||||||
|
}
|
||||||
167
lib/utils.ts
167
lib/utils.ts
@@ -36,29 +36,32 @@ const VALID_ENTITIES = new Set(["lt", "gt", "amp", "quot", "apos"])
|
|||||||
/**
|
/**
|
||||||
* Check if mxCell XML output is complete (not truncated).
|
* Check if mxCell XML output is complete (not truncated).
|
||||||
* Complete XML ends with a self-closing tag (/>) or closing mxCell tag.
|
* Complete XML ends with a self-closing tag (/>) or closing mxCell tag.
|
||||||
* Also handles function-calling wrapper tags that may be incorrectly included.
|
* Uses a robust approach that handles any LLM provider's wrapper tags
|
||||||
|
* by finding the last valid mxCell ending and checking if suffix is just closing tags.
|
||||||
* @param xml - The XML string to check (can be undefined/null)
|
* @param xml - The XML string to check (can be undefined/null)
|
||||||
* @returns true if XML appears complete, false if truncated or empty
|
* @returns true if XML appears complete, false if truncated or empty
|
||||||
*/
|
*/
|
||||||
export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
|
export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
|
||||||
let trimmed = xml?.trim() || ""
|
const trimmed = xml?.trim() || ""
|
||||||
if (!trimmed) return false
|
if (!trimmed) return false
|
||||||
|
|
||||||
// Strip Anthropic function-calling wrapper tags if present
|
// Find position of last complete mxCell ending (either /> or </mxCell>)
|
||||||
// These can leak into tool input due to AI SDK parsing issues
|
const lastSelfClose = trimmed.lastIndexOf("/>")
|
||||||
// Use loop because tags are nested: </mxCell></mxParameter></invoke>
|
const lastMxCellClose = trimmed.lastIndexOf("</mxCell>")
|
||||||
let prev = ""
|
|
||||||
while (prev !== trimmed) {
|
|
||||||
prev = trimmed
|
|
||||||
trimmed = trimmed
|
|
||||||
.replace(/<\/mxParameter>\s*$/i, "")
|
|
||||||
.replace(/<\/invoke>\s*$/i, "")
|
|
||||||
.replace(/<\/antml:parameter>\s*$/i, "")
|
|
||||||
.replace(/<\/antml:invoke>\s*$/i, "")
|
|
||||||
.trim()
|
|
||||||
}
|
|
||||||
|
|
||||||
return trimmed.endsWith("/>") || trimmed.endsWith("</mxCell>")
|
const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
|
||||||
|
|
||||||
|
// No valid ending found at all
|
||||||
|
if (lastValidEnd === -1) return false
|
||||||
|
|
||||||
|
// Check what comes after the last valid ending
|
||||||
|
// For />: add 2 chars, for </mxCell>: add 9 chars
|
||||||
|
const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
|
||||||
|
const suffix = trimmed.slice(lastValidEnd + endOffset)
|
||||||
|
|
||||||
|
// If suffix is empty or only contains closing tags (any provider's wrapper) or whitespace, it's complete
|
||||||
|
// This regex matches any sequence of closing XML tags like </foo>, </bar>, </|DSML|xyz>
|
||||||
|
return /^(\s*<\/[^>]+>)*\s*$/.test(suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,6 +265,21 @@ export function convertToLegalXml(xmlString: string): string {
|
|||||||
"&",
|
"&",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Fix unescaped < and > in attribute values for XML parsing
|
||||||
|
// HTML content in value attributes (e.g., <b>Title</b>) needs to be escaped
|
||||||
|
// This is critical because DOMParser will fail on unescaped < > in attributes
|
||||||
|
if (/=\s*"[^"]*<[^"]*"/.test(cellContent)) {
|
||||||
|
cellContent = cellContent.replace(
|
||||||
|
/=\s*"([^"]*)"/g,
|
||||||
|
(_match, value) => {
|
||||||
|
const escaped = value
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
return `="${escaped}"`
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// Indent each line of the matched block for readability.
|
// Indent each line of the matched block for readability.
|
||||||
const formatted = cellContent
|
const formatted = cellContent
|
||||||
.split("\n")
|
.split("\n")
|
||||||
@@ -306,6 +324,20 @@ export function wrapWithMxFile(xml: string): string {
|
|||||||
content = xml.replace(/<\/?root>/g, "").trim()
|
content = xml.replace(/<\/?root>/g, "").trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Strip trailing LLM wrapper tags (from any provider: Anthropic, DeepSeek, etc.)
|
||||||
|
// Find the last valid mxCell ending and remove everything after it
|
||||||
|
const lastSelfClose = content.lastIndexOf("/>")
|
||||||
|
const lastMxCellClose = content.lastIndexOf("</mxCell>")
|
||||||
|
const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
|
||||||
|
if (lastValidEnd !== -1) {
|
||||||
|
const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
|
||||||
|
const suffix = content.slice(lastValidEnd + endOffset)
|
||||||
|
// If suffix is only closing tags (wrapper tags), strip it
|
||||||
|
if (/^(\s*<\/[^>]+>)*\s*$/.test(suffix)) {
|
||||||
|
content = content.slice(0, lastValidEnd + endOffset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove any existing root cells from content (LLM shouldn't include them, but handle it gracefully)
|
// Remove any existing root cells from content (LLM shouldn't include them, but handle it gracefully)
|
||||||
// Use flexible patterns that match both self-closing (/>) and non-self-closing (></mxCell>) formats
|
// Use flexible patterns that match both self-closing (/>) and non-self-closing (></mxCell>) formats
|
||||||
content = content
|
content = content
|
||||||
@@ -423,7 +455,7 @@ export function replaceNodes(currentXML: string, nodes: string): string {
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
export interface DiagramOperation {
|
export interface DiagramOperation {
|
||||||
type: "update" | "add" | "delete"
|
operation: "update" | "add" | "delete"
|
||||||
cell_id: string
|
cell_id: string
|
||||||
new_xml?: string
|
new_xml?: string
|
||||||
}
|
}
|
||||||
@@ -496,7 +528,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Process each operation
|
// Process each operation
|
||||||
for (const op of operations) {
|
for (const op of operations) {
|
||||||
if (op.type === "update") {
|
if (op.operation === "update") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
@@ -548,7 +580,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Update the map with the new element
|
// Update the map with the new element
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "add") {
|
} else if (op.operation === "add") {
|
||||||
// Check if ID already exists
|
// Check if ID already exists
|
||||||
if (cellMap.has(op.cell_id)) {
|
if (cellMap.has(op.cell_id)) {
|
||||||
errors.push({
|
errors.push({
|
||||||
@@ -600,7 +632,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Add to map
|
// Add to map
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "delete") {
|
} else if (op.operation === "delete") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
@@ -910,6 +942,21 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
fixes.push("Removed CDATA wrapper")
|
fixes.push("Removed CDATA wrapper")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1b. Strip trailing LLM wrapper tags (DeepSeek, Anthropic, etc.)
|
||||||
|
// These are closing tags after the last valid mxCell that break XML parsing
|
||||||
|
const lastSelfClose = fixed.lastIndexOf("/>")
|
||||||
|
const lastMxCellClose = fixed.lastIndexOf("</mxCell>")
|
||||||
|
const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
|
||||||
|
if (lastValidEnd !== -1) {
|
||||||
|
const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
|
||||||
|
const suffix = fixed.slice(lastValidEnd + endOffset)
|
||||||
|
// If suffix contains only closing tags (wrapper tags) or whitespace, strip it
|
||||||
|
if (/^(\s*<\/[^>]+>)+\s*$/.test(suffix)) {
|
||||||
|
fixed = fixed.slice(0, lastValidEnd + endOffset)
|
||||||
|
fixes.push("Stripped trailing LLM wrapper tags")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Remove text before XML declaration or root element (only if it's garbage text, not valid XML)
|
// 2. Remove text before XML declaration or root element (only if it's garbage text, not valid XML)
|
||||||
const xmlStart = fixed.search(/<(\?xml|mxGraphModel|mxfile)/i)
|
const xmlStart = fixed.search(/<(\?xml|mxGraphModel|mxfile)/i)
|
||||||
if (xmlStart > 0 && !/^<[a-zA-Z]/.test(fixed.trim())) {
|
if (xmlStart > 0 && !/^<[a-zA-Z]/.test(fixed.trim())) {
|
||||||
@@ -1015,8 +1062,8 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
fixes.push("Removed quotes around color values in style")
|
fixes.push("Removed quotes around color values in style")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Fix unescaped < in attribute values
|
// 4. Fix unescaped < and > in attribute values
|
||||||
// This is tricky - we need to find < inside quoted attribute values
|
// < is required to be escaped, > is not strictly required but we escape for consistency
|
||||||
const attrPattern = /(=\s*")([^"]*?)(<)([^"]*?)(")/g
|
const attrPattern = /(=\s*")([^"]*?)(<)([^"]*?)(")/g
|
||||||
let attrMatch
|
let attrMatch
|
||||||
let hasUnescapedLt = false
|
let hasUnescapedLt = false
|
||||||
@@ -1027,12 +1074,12 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasUnescapedLt) {
|
if (hasUnescapedLt) {
|
||||||
// Replace < with < inside attribute values
|
// Replace < and > with < and > inside attribute values
|
||||||
fixed = fixed.replace(/=\s*"([^"]*)"/g, (_match, value) => {
|
fixed = fixed.replace(/=\s*"([^"]*)"/g, (_match, value) => {
|
||||||
const escaped = value.replace(/</g, "<")
|
const escaped = value.replace(/</g, "<").replace(/>/g, ">")
|
||||||
return `="${escaped}"`
|
return `="${escaped}"`
|
||||||
})
|
})
|
||||||
fixes.push("Escaped < characters in attribute values")
|
fixes.push("Escaped <> characters in attribute values")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Fix invalid character references (remove malformed ones)
|
// 5. Fix invalid character references (remove malformed ones)
|
||||||
@@ -1120,7 +1167,8 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 8c. Remove non-draw.io tags (after typo fixes so lowercase variants are fixed first)
|
// 8c. Remove non-draw.io tags (after typo fixes so lowercase variants are fixed first)
|
||||||
// Valid draw.io tags: mxfile, diagram, mxGraphModel, root, mxCell, mxGeometry, mxPoint, Array, Object
|
// IMPORTANT: Only remove tags at the element level, NOT inside quoted attribute values
|
||||||
|
// Tags like <b>, <br> inside value="<b>text</b>" should be preserved (they're HTML content)
|
||||||
const validDrawioTags = new Set([
|
const validDrawioTags = new Set([
|
||||||
"mxfile",
|
"mxfile",
|
||||||
"diagram",
|
"diagram",
|
||||||
@@ -1133,25 +1181,59 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
"Object",
|
"Object",
|
||||||
"mxRectangle",
|
"mxRectangle",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
// Helper: Check if a position is inside a quoted attribute value
|
||||||
|
// by counting unescaped quotes before that position
|
||||||
|
const isInsideQuotes = (str: string, pos: number): boolean => {
|
||||||
|
let inQuote = false
|
||||||
|
let quoteChar = ""
|
||||||
|
for (let i = 0; i < pos && i < str.length; i++) {
|
||||||
|
const c = str[i]
|
||||||
|
if (inQuote) {
|
||||||
|
if (c === quoteChar) inQuote = false
|
||||||
|
} else if (c === '"' || c === "'") {
|
||||||
|
// Check if this quote is part of an attribute (preceded by =)
|
||||||
|
// Look back for = sign
|
||||||
|
let j = i - 1
|
||||||
|
while (j >= 0 && /\s/.test(str[j])) j--
|
||||||
|
if (j >= 0 && str[j] === "=") {
|
||||||
|
inQuote = true
|
||||||
|
quoteChar = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return inQuote
|
||||||
|
}
|
||||||
|
|
||||||
const foreignTagPattern = /<\/?([a-zA-Z][a-zA-Z0-9_]*)[^>]*>/g
|
const foreignTagPattern = /<\/?([a-zA-Z][a-zA-Z0-9_]*)[^>]*>/g
|
||||||
let foreignMatch
|
let foreignMatch
|
||||||
const foreignTags = new Set<string>()
|
const foreignTags = new Set<string>()
|
||||||
|
const foreignTagPositions: Array<{
|
||||||
|
tag: string
|
||||||
|
start: number
|
||||||
|
end: number
|
||||||
|
}> = []
|
||||||
|
|
||||||
while ((foreignMatch = foreignTagPattern.exec(fixed)) !== null) {
|
while ((foreignMatch = foreignTagPattern.exec(fixed)) !== null) {
|
||||||
const tagName = foreignMatch[1]
|
const tagName = foreignMatch[1]
|
||||||
if (!validDrawioTags.has(tagName)) {
|
// Skip if this is a valid draw.io tag
|
||||||
foreignTags.add(tagName)
|
if (validDrawioTags.has(tagName)) continue
|
||||||
}
|
// Skip if this tag is inside a quoted attribute value
|
||||||
|
if (isInsideQuotes(fixed, foreignMatch.index)) continue
|
||||||
|
|
||||||
|
foreignTags.add(tagName)
|
||||||
|
foreignTagPositions.push({
|
||||||
|
tag: tagName,
|
||||||
|
start: foreignMatch.index,
|
||||||
|
end: foreignMatch.index + foreignMatch[0].length,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if (foreignTags.size > 0) {
|
|
||||||
console.log(
|
if (foreignTagPositions.length > 0) {
|
||||||
"[autoFixXml] Step 8c: Found foreign tags:",
|
// Remove tags from end to start to preserve indices
|
||||||
Array.from(foreignTags),
|
foreignTagPositions.sort((a, b) => b.start - a.start)
|
||||||
)
|
for (const { start, end } of foreignTagPositions) {
|
||||||
for (const tag of foreignTags) {
|
fixed = fixed.slice(0, start) + fixed.slice(end)
|
||||||
// Remove opening tags (with or without attributes)
|
|
||||||
fixed = fixed.replace(new RegExp(`<${tag}[^>]*>`, "gi"), "")
|
|
||||||
// Remove closing tags
|
|
||||||
fixed = fixed.replace(new RegExp(`</${tag}>`, "gi"), "")
|
|
||||||
}
|
}
|
||||||
fixes.push(
|
fixes.push(
|
||||||
`Removed foreign tags: ${Array.from(foreignTags).join(", ")}`,
|
`Removed foreign tags: ${Array.from(foreignTags).join(", ")}`,
|
||||||
@@ -1202,6 +1284,7 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
|
|
||||||
// 10b. Remove extra closing tags (more closes than opens)
|
// 10b. Remove extra closing tags (more closes than opens)
|
||||||
// Need to properly count self-closing tags (they don't need closing tags)
|
// Need to properly count self-closing tags (they don't need closing tags)
|
||||||
|
// IMPORTANT: Only count tags at element level, NOT inside quoted attribute values
|
||||||
const tagCounts = new Map<
|
const tagCounts = new Map<
|
||||||
string,
|
string,
|
||||||
{ opens: number; closes: number; selfClosing: number }
|
{ opens: number; closes: number; selfClosing: number }
|
||||||
@@ -1210,12 +1293,18 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
const fullTagPattern = /<(\/?[a-zA-Z][a-zA-Z0-9]*)[^>]*>/g
|
const fullTagPattern = /<(\/?[a-zA-Z][a-zA-Z0-9]*)[^>]*>/g
|
||||||
let tagCountMatch
|
let tagCountMatch
|
||||||
while ((tagCountMatch = fullTagPattern.exec(fixed)) !== null) {
|
while ((tagCountMatch = fullTagPattern.exec(fixed)) !== null) {
|
||||||
|
// Skip tags inside quoted attribute values (e.g., value="<b>Title</b>")
|
||||||
|
if (isInsideQuotes(fixed, tagCountMatch.index)) continue
|
||||||
|
|
||||||
const fullMatch = tagCountMatch[0] // e.g., "<mxCell .../>" or "</mxCell>"
|
const fullMatch = tagCountMatch[0] // e.g., "<mxCell .../>" or "</mxCell>"
|
||||||
const tagPart = tagCountMatch[1] // e.g., "mxCell" or "/mxCell"
|
const tagPart = tagCountMatch[1] // e.g., "mxCell" or "/mxCell"
|
||||||
const isClosing = tagPart.startsWith("/")
|
const isClosing = tagPart.startsWith("/")
|
||||||
const isSelfClosing = fullMatch.endsWith("/>")
|
const isSelfClosing = fullMatch.endsWith("/>")
|
||||||
const tagName = isClosing ? tagPart.slice(1) : tagPart
|
const tagName = isClosing ? tagPart.slice(1) : tagPart
|
||||||
|
|
||||||
|
// Only count valid draw.io tags - skip partial/invalid tags like "mx" from streaming
|
||||||
|
if (!validDrawioTags.has(tagName)) continue
|
||||||
|
|
||||||
let counts = tagCounts.get(tagName)
|
let counts = tagCounts.get(tagName)
|
||||||
if (!counts) {
|
if (!counts) {
|
||||||
counts = { opens: 0, closes: 0, selfClosing: 0 }
|
counts = { opens: 0, closes: 0, selfClosing: 0 }
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "next-ai-draw-io",
|
"name": "next-ai-draw-io",
|
||||||
"version": "0.4.6",
|
"version": "0.4.7",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "next-ai-draw-io",
|
"name": "next-ai-draw-io",
|
||||||
"version": "0.4.6",
|
"version": "0.4.7",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ai-sdk/amazon-bedrock": "^4.0.1",
|
"@ai-sdk/amazon-bedrock": "^4.0.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "next-ai-draw-io",
|
"name": "next-ai-draw-io",
|
||||||
"version": "0.4.6",
|
"version": "0.4.7",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "dist-electron/main/index.js",
|
"main": "dist-electron/main/index.js",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@next-ai-drawio/mcp-server",
|
"name": "@next-ai-drawio/mcp-server",
|
||||||
"version": "0.1.5",
|
"version": "0.1.6",
|
||||||
"description": "MCP server for Next AI Draw.io - AI-powered diagram generation with real-time browser preview",
|
"description": "MCP server for Next AI Draw.io - AI-powered diagram generation with real-time browser preview",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export interface DiagramOperation {
|
export interface DiagramOperation {
|
||||||
type: "update" | "add" | "delete"
|
operation: "update" | "add" | "delete"
|
||||||
cell_id: string
|
cell_id: string
|
||||||
new_xml?: string
|
new_xml?: string
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Process each operation
|
// Process each operation
|
||||||
for (const op of operations) {
|
for (const op of operations) {
|
||||||
if (op.type === "update") {
|
if (op.operation === "update") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
@@ -129,7 +129,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Update the map with the new element
|
// Update the map with the new element
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "add") {
|
} else if (op.operation === "add") {
|
||||||
// Check if ID already exists
|
// Check if ID already exists
|
||||||
if (cellMap.has(op.cell_id)) {
|
if (cellMap.has(op.cell_id)) {
|
||||||
errors.push({
|
errors.push({
|
||||||
@@ -181,7 +181,7 @@ export function applyDiagramOperations(
|
|||||||
|
|
||||||
// Add to map
|
// Add to map
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "delete") {
|
} else if (op.operation === "delete") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
|
|||||||
@@ -265,14 +265,22 @@ server.registerTool(
|
|||||||
"- add: Add a new cell. Provide cell_id (new unique id) and new_xml.\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" +
|
"- 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" +
|
"- 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.",
|
"For add/update, new_xml must be a complete mxCell element including mxGeometry.\n\n" +
|
||||||
|
"Example - Add a rectangle:\n" +
|
||||||
|
'{"operations": [{"operation": "add", "cell_id": "rect-1", "new_xml": "<mxCell id=\\"rect-1\\" value=\\"Hello\\" style=\\"rounded=0;\\" vertex=\\"1\\" parent=\\"1\\"><mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/></mxCell>"}]}\n\n' +
|
||||||
|
"Example - Update a cell:\n" +
|
||||||
|
'{"operations": [{"operation": "update", "cell_id": "3", "new_xml": "<mxCell id=\\"3\\" value=\\"New Label\\" style=\\"rounded=1;\\" vertex=\\"1\\" parent=\\"1\\"><mxGeometry x=\\"100\\" y=\\"100\\" width=\\"120\\" height=\\"60\\" as=\\"geometry\\"/></mxCell>"}]}\n\n' +
|
||||||
|
"Example - Delete a cell:\n" +
|
||||||
|
'{"operations": [{"operation": "delete", "cell_id": "rect-1"}]}',
|
||||||
inputSchema: {
|
inputSchema: {
|
||||||
operations: z
|
operations: z
|
||||||
.array(
|
.array(
|
||||||
z.object({
|
z.object({
|
||||||
type: z
|
operation: z
|
||||||
.enum(["update", "add", "delete"])
|
.enum(["update", "add", "delete"])
|
||||||
.describe("Operation type"),
|
.describe(
|
||||||
|
"Operation to perform: add, update, or delete",
|
||||||
|
),
|
||||||
cell_id: z.string().describe("The id of the mxCell"),
|
cell_id: z.string().describe("The id of the mxCell"),
|
||||||
new_xml: z
|
new_xml: z
|
||||||
.string()
|
.string()
|
||||||
@@ -356,13 +364,13 @@ server.registerTool(
|
|||||||
)
|
)
|
||||||
if (fixed) {
|
if (fixed) {
|
||||||
log.info(
|
log.info(
|
||||||
`Operation ${op.type} ${op.cell_id}: XML auto-fixed: ${fixes.join(", ")}`,
|
`Operation ${op.operation} ${op.cell_id}: XML auto-fixed: ${fixes.join(", ")}`,
|
||||||
)
|
)
|
||||||
return { ...op, new_xml: fixed }
|
return { ...op, new_xml: fixed }
|
||||||
}
|
}
|
||||||
if (!valid && error) {
|
if (!valid && error) {
|
||||||
log.warn(
|
log.warn(
|
||||||
`Operation ${op.type} ${op.cell_id}: XML validation failed: ${error}`,
|
`Operation ${op.operation} ${op.cell_id}: XML validation failed: ${error}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -459,7 +459,8 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
fixes.push("Removed quotes around color values in style")
|
fixes.push("Removed quotes around color values in style")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 10. Fix unescaped < in attribute values
|
// 10. Fix unescaped < and > in attribute values
|
||||||
|
// < is required to be escaped, > is not strictly required but we escape for consistency
|
||||||
const attrPattern = /(=\s*")([^"]*?)(<)([^"]*?)(")/g
|
const attrPattern = /(=\s*")([^"]*?)(<)([^"]*?)(")/g
|
||||||
let attrMatch
|
let attrMatch
|
||||||
let hasUnescapedLt = false
|
let hasUnescapedLt = false
|
||||||
@@ -471,10 +472,10 @@ export function autoFixXml(xml: string): { fixed: string; fixes: string[] } {
|
|||||||
}
|
}
|
||||||
if (hasUnescapedLt) {
|
if (hasUnescapedLt) {
|
||||||
fixed = fixed.replace(/=\s*"([^"]*)"/g, (_match, value) => {
|
fixed = fixed.replace(/=\s*"([^"]*)"/g, (_match, value) => {
|
||||||
const escaped = value.replace(/</g, "<")
|
const escaped = value.replace(/</g, "<").replace(/>/g, ">")
|
||||||
return `="${escaped}"`
|
return `="${escaped}"`
|
||||||
})
|
})
|
||||||
fixes.push("Escaped < characters in attribute values")
|
fixes.push("Escaped <> characters in attribute values")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 11. Fix invalid hex character references
|
// 11. Fix invalid hex character references
|
||||||
@@ -903,24 +904,30 @@ export function validateAndFixXml(xml: string): {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if mxCell XML output is complete (not truncated).
|
* Check if mxCell XML output is complete (not truncated).
|
||||||
|
* Uses a robust approach that handles any LLM provider's wrapper tags
|
||||||
|
* by finding the last valid mxCell ending and checking if suffix is just closing tags.
|
||||||
* @param xml - The XML string to check (can be undefined/null)
|
* @param xml - The XML string to check (can be undefined/null)
|
||||||
* @returns true if XML appears complete, false if truncated or empty
|
* @returns true if XML appears complete, false if truncated or empty
|
||||||
*/
|
*/
|
||||||
export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
|
export function isMxCellXmlComplete(xml: string | undefined | null): boolean {
|
||||||
let trimmed = xml?.trim() || ""
|
const trimmed = xml?.trim() || ""
|
||||||
if (!trimmed) return false
|
if (!trimmed) return false
|
||||||
|
|
||||||
// Strip wrapper tags if present
|
// Find position of last complete mxCell ending (either /> or </mxCell>)
|
||||||
let prev = ""
|
const lastSelfClose = trimmed.lastIndexOf("/>")
|
||||||
while (prev !== trimmed) {
|
const lastMxCellClose = trimmed.lastIndexOf("</mxCell>")
|
||||||
prev = trimmed
|
|
||||||
trimmed = trimmed
|
|
||||||
.replace(/<\/mxParameter>\s*$/i, "")
|
|
||||||
.replace(/<\/invoke>\s*$/i, "")
|
|
||||||
.replace(/<\/antml:parameter>\s*$/i, "")
|
|
||||||
.replace(/<\/antml:invoke>\s*$/i, "")
|
|
||||||
.trim()
|
|
||||||
}
|
|
||||||
|
|
||||||
return trimmed.endsWith("/>") || trimmed.endsWith("</mxCell>")
|
const lastValidEnd = Math.max(lastSelfClose, lastMxCellClose)
|
||||||
|
|
||||||
|
// No valid ending found at all
|
||||||
|
if (lastValidEnd === -1) return false
|
||||||
|
|
||||||
|
// Check what comes after the last valid ending
|
||||||
|
// For />: add 2 chars, for </mxCell>: add 9 chars
|
||||||
|
const endOffset = lastMxCellClose > lastSelfClose ? 9 : 2
|
||||||
|
const suffix = trimmed.slice(lastValidEnd + endOffset)
|
||||||
|
|
||||||
|
// If suffix is empty or only contains closing tags (any provider's wrapper) or whitespace, it's complete
|
||||||
|
// This regex matches any sequence of closing XML tags like </foo>, </bar>, </|DSML|xyz>
|
||||||
|
return /^(\s*<\/[^>]+>)*\s*$/.test(suffix)
|
||||||
}
|
}
|
||||||
|
|||||||
40
renovate.json
Normal file
40
renovate.json
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": ["config:recommended"],
|
||||||
|
"schedule": ["after 10am on saturday"],
|
||||||
|
"timezone": "Asia/Tokyo",
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": ["minor", "patch"],
|
||||||
|
"matchPackagePatterns": ["*"],
|
||||||
|
"groupName": "minor and patch dependencies",
|
||||||
|
"automerge": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": ["major"],
|
||||||
|
"matchPackagePatterns": ["*"],
|
||||||
|
"automerge": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"matchPackagePatterns": ["@ai-sdk/*"],
|
||||||
|
"groupName": "AI SDK packages"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"matchPackagePatterns": ["@radix-ui/*"],
|
||||||
|
"groupName": "Radix UI packages"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"matchPackagePatterns": ["electron", "electron-builder"],
|
||||||
|
"groupName": "Electron packages",
|
||||||
|
"automerge": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"matchPackagePatterns": ["@ai-sdk/*", "ai", "next"],
|
||||||
|
"groupName": "Core framework packages",
|
||||||
|
"automerge": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"vulnerabilityAlerts": {
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,7 +22,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
result: xmlContent,
|
result: xmlContent,
|
||||||
errors: [
|
errors: [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: "",
|
cellId: "",
|
||||||
message: `XML parse error: ${parseError.textContent}`,
|
message: `XML parse error: ${parseError.textContent}`,
|
||||||
},
|
},
|
||||||
@@ -36,7 +36,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
result: xmlContent,
|
result: xmlContent,
|
||||||
errors: [
|
errors: [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: "",
|
cellId: "",
|
||||||
message: "Could not find <root> element in XML",
|
message: "Could not find <root> element in XML",
|
||||||
},
|
},
|
||||||
@@ -51,11 +51,11 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for (const op of operations) {
|
for (const op of operations) {
|
||||||
if (op.type === "update") {
|
if (op.operation === "update") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: `Cell with id="${op.cell_id}" not found`,
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
})
|
})
|
||||||
@@ -63,7 +63,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
}
|
}
|
||||||
if (!op.new_xml) {
|
if (!op.new_xml) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: "new_xml is required for update operation",
|
message: "new_xml is required for update operation",
|
||||||
})
|
})
|
||||||
@@ -76,7 +76,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const newCell = newDoc.querySelector("mxCell")
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
if (!newCell) {
|
if (!newCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: "new_xml must contain an mxCell element",
|
message: "new_xml must contain an mxCell element",
|
||||||
})
|
})
|
||||||
@@ -85,7 +85,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const newCellId = newCell.getAttribute("id")
|
const newCellId = newCell.getAttribute("id")
|
||||||
if (newCellId !== op.cell_id) {
|
if (newCellId !== op.cell_id) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "update",
|
operation: "update",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
})
|
})
|
||||||
@@ -94,10 +94,10 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const importedNode = doc.importNode(newCell, true)
|
const importedNode = doc.importNode(newCell, true)
|
||||||
existingCell.parentNode?.replaceChild(importedNode, existingCell)
|
existingCell.parentNode?.replaceChild(importedNode, existingCell)
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "add") {
|
} else if (op.operation === "add") {
|
||||||
if (cellMap.has(op.cell_id)) {
|
if (cellMap.has(op.cell_id)) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "add",
|
operation: "add",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: `Cell with id="${op.cell_id}" already exists`,
|
message: `Cell with id="${op.cell_id}" already exists`,
|
||||||
})
|
})
|
||||||
@@ -105,7 +105,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
}
|
}
|
||||||
if (!op.new_xml) {
|
if (!op.new_xml) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "add",
|
operation: "add",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: "new_xml is required for add operation",
|
message: "new_xml is required for add operation",
|
||||||
})
|
})
|
||||||
@@ -118,7 +118,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const newCell = newDoc.querySelector("mxCell")
|
const newCell = newDoc.querySelector("mxCell")
|
||||||
if (!newCell) {
|
if (!newCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "add",
|
operation: "add",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: "new_xml must contain an mxCell element",
|
message: "new_xml must contain an mxCell element",
|
||||||
})
|
})
|
||||||
@@ -127,7 +127,7 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const newCellId = newCell.getAttribute("id")
|
const newCellId = newCell.getAttribute("id")
|
||||||
if (newCellId !== op.cell_id) {
|
if (newCellId !== op.cell_id) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "add",
|
operation: "add",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
message: `ID mismatch: cell_id is "${op.cell_id}" but new_xml has id="${newCellId}"`,
|
||||||
})
|
})
|
||||||
@@ -136,11 +136,11 @@ function applyDiagramOperations(xmlContent, operations) {
|
|||||||
const importedNode = doc.importNode(newCell, true)
|
const importedNode = doc.importNode(newCell, true)
|
||||||
root.appendChild(importedNode)
|
root.appendChild(importedNode)
|
||||||
cellMap.set(op.cell_id, importedNode)
|
cellMap.set(op.cell_id, importedNode)
|
||||||
} else if (op.type === "delete") {
|
} else if (op.operation === "delete") {
|
||||||
const existingCell = cellMap.get(op.cell_id)
|
const existingCell = cellMap.get(op.cell_id)
|
||||||
if (!existingCell) {
|
if (!existingCell) {
|
||||||
errors.push({
|
errors.push({
|
||||||
type: "delete",
|
operation: "delete",
|
||||||
cellId: op.cell_id,
|
cellId: op.cell_id,
|
||||||
message: `Cell with id="${op.cell_id}" not found`,
|
message: `Cell with id="${op.cell_id}" not found`,
|
||||||
})
|
})
|
||||||
@@ -201,7 +201,7 @@ function assert(condition, message) {
|
|||||||
test("Update operation changes cell value", () => {
|
test("Update operation changes cell value", () => {
|
||||||
const { result, errors } = applyDiagramOperations(sampleXml, [
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cell_id: "2",
|
cell_id: "2",
|
||||||
new_xml:
|
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>',
|
'<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>',
|
||||||
@@ -224,7 +224,7 @@ test("Update operation changes cell value", () => {
|
|||||||
test("Update operation fails for non-existent cell", () => {
|
test("Update operation fails for non-existent cell", () => {
|
||||||
const { errors } = applyDiagramOperations(sampleXml, [
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cell_id: "999",
|
cell_id: "999",
|
||||||
new_xml: '<mxCell id="999" value="Test"/>',
|
new_xml: '<mxCell id="999" value="Test"/>',
|
||||||
},
|
},
|
||||||
@@ -239,7 +239,7 @@ test("Update operation fails for non-existent cell", () => {
|
|||||||
test("Update operation fails on ID mismatch", () => {
|
test("Update operation fails on ID mismatch", () => {
|
||||||
const { errors } = applyDiagramOperations(sampleXml, [
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cell_id: "2",
|
cell_id: "2",
|
||||||
new_xml: '<mxCell id="WRONG" value="Test"/>',
|
new_xml: '<mxCell id="WRONG" value="Test"/>',
|
||||||
},
|
},
|
||||||
@@ -254,7 +254,7 @@ test("Update operation fails on ID mismatch", () => {
|
|||||||
test("Add operation creates new cell", () => {
|
test("Add operation creates new cell", () => {
|
||||||
const { result, errors } = applyDiagramOperations(sampleXml, [
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "add",
|
operation: "add",
|
||||||
cell_id: "new1",
|
cell_id: "new1",
|
||||||
new_xml:
|
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>',
|
'<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>',
|
||||||
@@ -274,7 +274,7 @@ test("Add operation creates new cell", () => {
|
|||||||
test("Add operation fails for duplicate ID", () => {
|
test("Add operation fails for duplicate ID", () => {
|
||||||
const { errors } = applyDiagramOperations(sampleXml, [
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "add",
|
operation: "add",
|
||||||
cell_id: "2",
|
cell_id: "2",
|
||||||
new_xml: '<mxCell id="2" value="Duplicate"/>',
|
new_xml: '<mxCell id="2" value="Duplicate"/>',
|
||||||
},
|
},
|
||||||
@@ -289,7 +289,7 @@ test("Add operation fails for duplicate ID", () => {
|
|||||||
test("Add operation fails on ID mismatch", () => {
|
test("Add operation fails on ID mismatch", () => {
|
||||||
const { errors } = applyDiagramOperations(sampleXml, [
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "add",
|
operation: "add",
|
||||||
cell_id: "new1",
|
cell_id: "new1",
|
||||||
new_xml: '<mxCell id="WRONG" value="Test"/>',
|
new_xml: '<mxCell id="WRONG" value="Test"/>',
|
||||||
},
|
},
|
||||||
@@ -303,7 +303,7 @@ test("Add operation fails on ID mismatch", () => {
|
|||||||
|
|
||||||
test("Delete operation removes cell", () => {
|
test("Delete operation removes cell", () => {
|
||||||
const { result, errors } = applyDiagramOperations(sampleXml, [
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
{ type: "delete", cell_id: "3" },
|
{ operation: "delete", cell_id: "3" },
|
||||||
])
|
])
|
||||||
assert(
|
assert(
|
||||||
errors.length === 0,
|
errors.length === 0,
|
||||||
@@ -315,7 +315,7 @@ test("Delete operation removes cell", () => {
|
|||||||
|
|
||||||
test("Delete operation fails for non-existent cell", () => {
|
test("Delete operation fails for non-existent cell", () => {
|
||||||
const { errors } = applyDiagramOperations(sampleXml, [
|
const { errors } = applyDiagramOperations(sampleXml, [
|
||||||
{ type: "delete", cell_id: "999" },
|
{ operation: "delete", cell_id: "999" },
|
||||||
])
|
])
|
||||||
assert(errors.length === 1, "Should have one error")
|
assert(errors.length === 1, "Should have one error")
|
||||||
assert(
|
assert(
|
||||||
@@ -327,18 +327,18 @@ test("Delete operation fails for non-existent cell", () => {
|
|||||||
test("Multiple operations in sequence", () => {
|
test("Multiple operations in sequence", () => {
|
||||||
const { result, errors } = applyDiagramOperations(sampleXml, [
|
const { result, errors } = applyDiagramOperations(sampleXml, [
|
||||||
{
|
{
|
||||||
type: "update",
|
operation: "update",
|
||||||
cell_id: "2",
|
cell_id: "2",
|
||||||
new_xml:
|
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>',
|
'<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",
|
operation: "add",
|
||||||
cell_id: "new1",
|
cell_id: "new1",
|
||||||
new_xml:
|
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>',
|
'<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" },
|
{ operation: "delete", cell_id: "3" },
|
||||||
])
|
])
|
||||||
assert(
|
assert(
|
||||||
errors.length === 0,
|
errors.length === 0,
|
||||||
@@ -354,14 +354,14 @@ test("Multiple operations in sequence", () => {
|
|||||||
|
|
||||||
test("Invalid XML returns parse error", () => {
|
test("Invalid XML returns parse error", () => {
|
||||||
const { errors } = applyDiagramOperations("<not valid xml", [
|
const { errors } = applyDiagramOperations("<not valid xml", [
|
||||||
{ type: "delete", cell_id: "1" },
|
{ operation: "delete", cell_id: "1" },
|
||||||
])
|
])
|
||||||
assert(errors.length === 1, "Should have one error")
|
assert(errors.length === 1, "Should have one error")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Missing root element returns error", () => {
|
test("Missing root element returns error", () => {
|
||||||
const { errors } = applyDiagramOperations("<mxfile></mxfile>", [
|
const { errors } = applyDiagramOperations("<mxfile></mxfile>", [
|
||||||
{ type: "delete", cell_id: "1" },
|
{ operation: "delete", cell_id: "1" },
|
||||||
])
|
])
|
||||||
assert(errors.length === 1, "Should have one error")
|
assert(errors.length === 1, "Should have one error")
|
||||||
assert(
|
assert(
|
||||||
|
|||||||
Reference in New Issue
Block a user