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:
dayuan.jiang
2026-10-05 17:33:36 +09:00
parent 4731394f32
commit c75f74a5a0
36 changed files with 1179 additions and 188 deletions
+26 -21
View File
@@ -122,34 +122,33 @@ export function useDiagramToolHandlers({
}
processedToolCallsRef.current.add(toolCall.toolCallId)
// Only these two put their result on the canvas. Other tools
// (get_shape_library, which the server runs, still arrives here)
// leave the stored originals for the preview code to undo.
const drawsDiagram =
toolCall.toolName === "display_diagram" ||
toolCall.toolName === "edit_diagram"
// Stored originals belong to previews not handled yet: this call's,
// and those of earlier calls with invalid input, which never get
// here. The first is the diagram before all of them. This call's
// result replaces those previews, so the preview code must neither
// draw them again nor undo them later.
const [originalXml] = editDiagramOriginalXmlRef.current.values()
if (drawsDiagram) {
for (const id of editDiagramOriginalXmlRef.current.keys()) {
processedToolCallsRef.current.add(id)
}
editDiagramOriginalXmlRef.current.clear()
}
// Only display_diagram, edit_diagram and a completing append_diagram
// put their result on the canvas. Other tools (get_shape_library,
// which the server runs, still arrives here) leave the stored
// originals for the preview code to undo.
if (toolCall.toolName === "display_diagram") {
await handleDisplayDiagram(toolCall, addToolOutput, originalXml)
await handleDisplayDiagram(toolCall, addToolOutput, takeOriginals())
} else if (toolCall.toolName === "edit_diagram") {
await handleEditDiagram(toolCall, addToolOutput, originalXml)
await handleEditDiagram(toolCall, addToolOutput, takeOriginals())
} else if (toolCall.toolName === "append_diagram") {
handleAppendDiagram(toolCall, addToolOutput)
}
}
// Stored originals belong to previews not handled yet: this call's, and
// those of earlier calls with invalid input, which never get to the
// handler. The first is the diagram before all of them. A call that
// draws its result replaces those previews, so the preview code must
// neither draw them again nor undo them later. Returns that first one.
const takeOriginals = (): string | undefined => {
const [originalXml] = editDiagramOriginalXmlRef.current.values()
for (const id of editDiagramOriginalXmlRef.current.keys()) {
processedToolCallsRef.current.add(id)
}
editDiagramOriginalXmlRef.current.clear()
return originalXml
}
// originalXml: the diagram before the streamed previews, if any were drawn
const handleDisplayDiagram = async (
toolCall: ToolCall,
@@ -540,11 +539,17 @@ Start your continuation with the NEXT character after where it stopped.`,
partialXmlRef.current = "" // Reset
const prepared = prepareNewDiagram(finalXml, NEW_PAGE)
// It draws now: it takes the stored originals, as display_diagram
const originalXml = prepared.ok ? takeOriginals() : undefined
const validationError = prepared.ok
? onDisplayChart(prepared.xml, true)
: prepared.error
if (validationError) {
// Loading failed: back to the diagram before the previews
if (prepared.ok && originalXml) {
onDisplayChart(originalXml, true)
}
addToolOutput({
tool: "append_diagram",
toolCallId: toolCall.toolCallId,
+45 -17
View File
@@ -64,6 +64,28 @@ function migrateOldConfig(): MultiModelConfig | null {
return config
}
const isKnownProvider = (p: { provider: string }) =>
Object.hasOwn(PROVIDER_INFO, p.provider)
/**
* The stored config without providers this version does not know (saved
* by another version, or edited by hand): they would break every list of
* models. They stay in storage (saveConfig keeps them). Throws on bad JSON.
*/
function parseStoredConfig(stored: string): MultiModelConfig {
const config = JSON.parse(stored) as MultiModelConfig
const known = config.providers.filter(isKnownProvider)
if (known.length < config.providers.length) {
console.warn(
"Skipped saved providers this version does not know:",
config.providers
.filter((p) => !isKnownProvider(p))
.map((p) => p.provider),
)
}
return { ...config, providers: known }
}
/**
* Load config from localStorage
*/
@@ -74,21 +96,7 @@ function loadConfig(): MultiModelConfig {
const stored = localStorage.getItem(STORAGE_KEYS.modelConfigs)
if (stored) {
try {
const config = JSON.parse(stored) as MultiModelConfig
// A provider this version does not know (saved by another
// version, or edited by hand) would break every list of models
const known = config.providers.filter((p) =>
Object.hasOwn(PROVIDER_INFO, p.provider),
)
if (known.length < config.providers.length) {
console.warn(
"Skipped saved providers this version does not know:",
config.providers
.filter((p) => !known.includes(p))
.map((p) => p.provider),
)
}
return { ...config, providers: known }
return parseStoredConfig(stored)
} catch {
console.error("Failed to parse model config")
}
@@ -113,7 +121,26 @@ function loadConfig(): MultiModelConfig {
*/
function saveConfig(config: MultiModelConfig): void {
if (typeof window === "undefined") return
localStorage.setItem(STORAGE_KEYS.modelConfigs, JSON.stringify(config))
// Providers this version does not know are not in config: keep them,
// with their keys, for the version that saved them
let unknown: MultiModelConfig["providers"] = []
try {
const stored = localStorage.getItem(STORAGE_KEYS.modelConfigs)
if (stored) {
unknown = (JSON.parse(stored) as MultiModelConfig).providers.filter(
(p) => !isKnownProvider(p),
)
}
} catch {
// Unreadable: nothing to keep
}
localStorage.setItem(
STORAGE_KEYS.modelConfigs,
JSON.stringify({
...config,
providers: [...config.providers, ...unknown],
}),
)
}
/**
@@ -488,7 +515,8 @@ export function getSelectedAIConfig(): {
let config: MultiModelConfig
try {
config = JSON.parse(stored)
// Unknown providers would break the model lookup below
config = parseStoredConfig(stored)
} catch {
return { ...empty, accessCode }
}
+5
View File
@@ -114,6 +114,11 @@ export function useSessionManager(
// Load sessions list
const metadata = await getAllSessionMetadata()
setSessions(metadata)
// The desktop app may try its other port next launch, where
// an older version may have saved the chats
window.electronAPI
?.chatsLoaded?.(metadata.length)
.catch(() => {})
// Only load a session if initialSessionId is provided (from URL param)
if (initialSessionId) {