mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-08 18:57:47 +08:00
fix: what the third round broke, and the first batch's review
MCP preview after the server lost a session (it expired, or the MCP process restarted): - Every server state has an id, made when the state is created. The tab notices a new id even when the version numbers happen to match, and every push names the state it was based on, so one based on a lost state is refused, also when it comes before the tab's first poll (the server recovers the saved file first). - The tab keeps the newest canvas XML, saved or not. When the server knows nothing (no file) or exactly what the tab last saved, the canvas wins and is saved, so edits made while the server was down are kept. Otherwise the server's diagram (an AI write the tab missed, a cleared document that was saved) is shown and the tab's copy goes to History. - Late answers to an old state's push or poll are dropped; a failed push says the server is unreachable; Download as .drawio saves the canvas. Settings and server: - Saved providers this version does not know stay in storage with their keys, and sending no longer trips over them. - The desktop "Ollama (Local)" preset with a key goes to local Ollama again; a server model's Ollama URL variable is read; the admin panel writes Ollama Cloud's URL for a key without one. - Provider error texts show again in the desktop app and for EdgeOne. - .env: a quoted value followed by a comment ending in a quote is read as dotenv reads it; unquoted values are unchanged. - Desktop app: the next launch opens the port where a chat was last saved; a launch elsewhere that saves nothing does not move it, and a page with no chats lets the next launch try the other port once. - The Test button no longer stays busy after another tab changed the key. - A completed append_diagram is no longer undone by an earlier failed edit's preview; a file read once in vain is saved again once it is read or gone. From the first batch's review: - The admin panel's Test of an entry without a URL now tests the server's <P>_BASE_URL, where chat sends the entry's key; chat is unchanged (the first fix rerouted working setups). - The model list ends downloads that are too large, accepts answers without a body, and keeps the "redirects are not allowed" explanation. - A test covers the preview's History rendering.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
* Serves draw.io embed with state sync and history UI
|
||||
*/
|
||||
|
||||
import { randomUUID } from "node:crypto"
|
||||
import { readFileSync } from "node:fs"
|
||||
import http from "node:http"
|
||||
import { dirname, join } from "node:path"
|
||||
@@ -105,11 +106,22 @@ function ensureSessionStateInitialized(sessionId: string): void {
|
||||
// A blank diagram keeps the draw.io spinner (spin=1) from waiting
|
||||
// forever when no load(xml) is ever sent
|
||||
setState(sessionId, BLANK_MXFILE, undefined, false, false)
|
||||
// Nothing is known about this session: a tab that still shows it keeps
|
||||
// its diagram
|
||||
const state = stateStore.get(sessionId)
|
||||
if (state) state.blank = true
|
||||
}
|
||||
|
||||
interface SessionState {
|
||||
xml: string
|
||||
version: number
|
||||
// Made when the state is created (first use, or again after it expired
|
||||
// or the MCP process restarted) and kept by every write. A tab tells by
|
||||
// it that the server lost what it knew, and every push names the state
|
||||
// it was based on, so one based on a lost state is refused.
|
||||
stateId: string
|
||||
// Created blank because nothing was saved; cleared by the first write
|
||||
blank?: boolean
|
||||
// Version of the last write the browser did not make itself (AI edit,
|
||||
// restore). A browser push based on an older version is rejected.
|
||||
serverVersion?: number
|
||||
@@ -178,6 +190,7 @@ export function setState(
|
||||
stateStore.set(sessionId, {
|
||||
xml,
|
||||
version: newVersion,
|
||||
stateId: existing?.stateId ?? randomUUID(),
|
||||
serverVersion: fromBrowser ? existing?.serverVersion : newVersion,
|
||||
lastUpdated: new Date(),
|
||||
lastPolled: existing?.lastPolled,
|
||||
@@ -444,6 +457,8 @@ function handleStateApi(
|
||||
JSON.stringify({
|
||||
xml: state?.xml || null,
|
||||
version: state?.version || 0,
|
||||
stateId: state?.stateId ?? null,
|
||||
blank: !!state?.blank,
|
||||
syncRequested: !!state?.syncRequested,
|
||||
exportFormat: state?.exportFormat || null,
|
||||
exportXml: state?.exportXml || null,
|
||||
@@ -486,12 +501,58 @@ function handleStateApi(
|
||||
return
|
||||
}
|
||||
|
||||
// A push can come before the tab's first poll after a
|
||||
// restart: recover the saved file first, so it is compared
|
||||
// with that and never overwrites it unseen
|
||||
ensureSessionStateInitialized(sessionId)
|
||||
const current = stateStore.get(sessionId)
|
||||
|
||||
// A tab of this version names the state its push is based
|
||||
// on. Another state (the server lost the one it knew, or
|
||||
// the tab has not polled yet): refused, and the tab's next
|
||||
// poll decides whose diagram wins.
|
||||
if (current && "stateId" in data) {
|
||||
if (data.stateId !== current.stateId) {
|
||||
res.writeHead(409, {
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
error: "Session was recreated",
|
||||
stateChanged: true,
|
||||
version: current.version,
|
||||
}),
|
||||
)
|
||||
return
|
||||
}
|
||||
// What a recovering tab showed: kept in history only
|
||||
if (data.source === "recover") {
|
||||
const saved =
|
||||
typeof data.xml === "string" &&
|
||||
!!data.xml &&
|
||||
data.xml !== current.xml
|
||||
if (saved) {
|
||||
addHistory(sessionId, data.xml, data.svg || "")
|
||||
}
|
||||
res.writeHead(409, {
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
error: "Diagram changed on the server",
|
||||
version: current.version,
|
||||
savedToHistory: saved,
|
||||
}),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// The browser edited a version older than the latest AI write
|
||||
// (it has not loaded that write yet). Keep the AI write; the
|
||||
// browser loads it on its next poll. A sync reply is also
|
||||
// stale after a newer write of the browser's own (a user
|
||||
// edit saved while the export ran).
|
||||
const current = stateStore.get(sessionId)
|
||||
if (
|
||||
typeof data.baseVersion === "number" &&
|
||||
(data.baseVersion < (current?.serverVersion ?? 0) ||
|
||||
|
||||
Reference in New Issue
Block a user