mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-08 18:57:47 +08:00
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.
276 lines
8.2 KiB
TypeScript
276 lines
8.2 KiB
TypeScript
import { app, BrowserWindow, type IpcMainInvokeEvent, ipcMain } from "electron"
|
|
import { rebuildAppMenu, switchPreset } from "./app-menu"
|
|
import {
|
|
type ConfigPreset,
|
|
createPreset,
|
|
deletePreset,
|
|
getAllPresets,
|
|
getCurrentPreset,
|
|
getCurrentPresetId,
|
|
getUserLocale,
|
|
setCurrentPreset,
|
|
setUserLocale,
|
|
updatePreset,
|
|
} from "./config-manager"
|
|
import { restartNextServer } from "./next-server"
|
|
import { noteNoChats, rememberChatPort } from "./port-manager"
|
|
import {
|
|
applyProxyToEnv,
|
|
getProxyConfig,
|
|
type ProxyConfig,
|
|
saveProxyConfig,
|
|
} from "./proxy-manager"
|
|
import { isAppUrl } from "./window-manager"
|
|
|
|
/**
|
|
* Allowed configuration keys for presets
|
|
* This whitelist prevents arbitrary environment variable injection
|
|
*/
|
|
const ALLOWED_CONFIG_KEYS = new Set([
|
|
"AI_PROVIDER",
|
|
"AI_MODEL",
|
|
"AI_API_KEY",
|
|
"AI_BASE_URL",
|
|
"TEMPERATURE",
|
|
])
|
|
|
|
/**
|
|
* Sanitize preset config to only include allowed keys
|
|
*/
|
|
function sanitizePresetConfig(
|
|
config: Record<string, string | undefined>,
|
|
): Record<string, string | undefined> {
|
|
const sanitized: Record<string, string | undefined> = {}
|
|
for (const key of ALLOWED_CONFIG_KEYS) {
|
|
if (key in config && typeof config[key] === "string") {
|
|
sanitized[key] = config[key]
|
|
}
|
|
}
|
|
return sanitized
|
|
}
|
|
|
|
/**
|
|
* Register an IPC handler that only answers the app's own pages
|
|
* (the main window on the app server, or the local settings page).
|
|
* A main window that somehow ends up on an external site still gets the
|
|
* preload API, so its calls must be rejected here.
|
|
*/
|
|
function handle<Args extends unknown[]>(
|
|
channel: string,
|
|
listener: (event: IpcMainInvokeEvent, ...args: Args) => unknown,
|
|
): void {
|
|
ipcMain.handle(channel, (event, ...args) => {
|
|
const url = event.senderFrame?.url
|
|
if (!isAppUrl(url) && !url?.startsWith("file://")) {
|
|
throw new Error(`Blocked "${channel}" from untrusted page: ${url}`)
|
|
}
|
|
return listener(event, ...(args as Args))
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Register all IPC handlers
|
|
*/
|
|
export function registerIpcHandlers(): void {
|
|
// ==================== App Info ====================
|
|
|
|
handle("get-version", () => {
|
|
return app.getVersion()
|
|
})
|
|
|
|
// ==================== Where the chats are ====================
|
|
|
|
// The page saved a chat, or loaded without any: decides which port
|
|
// (and so which origin's chats) the next launch opens
|
|
handle("chat-saved", () => rememberChatPort())
|
|
handle("chats-loaded", (_event, count: unknown) => {
|
|
if (count === 0) noteNoChats()
|
|
})
|
|
|
|
// ==================== Window Controls ====================
|
|
|
|
ipcMain.on("window-minimize", (event) => {
|
|
const win = BrowserWindow.fromWebContents(event.sender)
|
|
win?.minimize()
|
|
})
|
|
|
|
ipcMain.on("window-maximize", (event) => {
|
|
const win = BrowserWindow.fromWebContents(event.sender)
|
|
if (win?.isMaximized()) {
|
|
win.unmaximize()
|
|
} else {
|
|
win?.maximize()
|
|
}
|
|
})
|
|
|
|
ipcMain.on("window-close", (event) => {
|
|
const win = BrowserWindow.fromWebContents(event.sender)
|
|
win?.close()
|
|
})
|
|
|
|
// ==================== Config Presets ====================
|
|
|
|
handle("config-presets:get-all", () => {
|
|
return getAllPresets()
|
|
})
|
|
|
|
handle("config-presets:get-current", () => {
|
|
return getCurrentPreset()
|
|
})
|
|
|
|
handle("config-presets:get-current-id", () => {
|
|
return getCurrentPresetId()
|
|
})
|
|
|
|
handle(
|
|
"config-presets:save",
|
|
async (
|
|
_event,
|
|
preset: Omit<ConfigPreset, "id" | "createdAt" | "updatedAt"> & {
|
|
id?: string
|
|
},
|
|
) => {
|
|
// Validate preset name
|
|
if (typeof preset?.name !== "string" || !preset.name.trim()) {
|
|
throw new Error("Invalid preset name")
|
|
}
|
|
|
|
// Sanitize config to only allow whitelisted keys
|
|
const sanitizedConfig = sanitizePresetConfig(preset.config ?? {})
|
|
|
|
if (preset.id) {
|
|
// Update existing preset
|
|
const updated = updatePreset(preset.id, {
|
|
name: preset.name.trim(),
|
|
config: sanitizedConfig,
|
|
})
|
|
// Re-apply the active preset so the edit takes effect
|
|
if (updated && updated.id === getCurrentPresetId()) {
|
|
await switchPreset(updated.id)
|
|
} else {
|
|
rebuildAppMenu()
|
|
}
|
|
return updated
|
|
}
|
|
// Create new preset
|
|
const created = createPreset({
|
|
name: preset.name.trim(),
|
|
config: sanitizedConfig,
|
|
})
|
|
rebuildAppMenu()
|
|
return created
|
|
},
|
|
)
|
|
|
|
handle("config-presets:delete", async (_event, id: string) => {
|
|
const wasCurrent = id === getCurrentPresetId()
|
|
// Deleting the active preset also clears its env vars
|
|
const deleted = deletePreset(id)
|
|
rebuildAppMenu()
|
|
|
|
// Restart so the server stops using the deleted preset
|
|
if (deleted && wasCurrent && app.isPackaged) {
|
|
await restartNextServer()
|
|
}
|
|
return deleted
|
|
})
|
|
|
|
handle("config-presets:apply", async (_event, id: string) => {
|
|
try {
|
|
const env = await switchPreset(id)
|
|
// In development mode, electron-dev.mjs restarts Next.js
|
|
return app.isPackaged
|
|
? { success: true, env }
|
|
: { success: true, env, devMode: true }
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to restart server",
|
|
}
|
|
}
|
|
})
|
|
|
|
handle("config-presets:set-current", (_event, id: string | null) => {
|
|
return setCurrentPreset(id)
|
|
})
|
|
|
|
// ==================== Proxy Settings ====================
|
|
|
|
handle("get-proxy", () => {
|
|
return getProxyConfig()
|
|
})
|
|
|
|
handle("set-proxy", async (_event, config: ProxyConfig) => {
|
|
const isOptionalString = (value: unknown) =>
|
|
value === undefined || typeof value === "string"
|
|
if (
|
|
typeof config !== "object" ||
|
|
config === null ||
|
|
!isOptionalString(config.httpProxy) ||
|
|
!isOptionalString(config.httpsProxy)
|
|
) {
|
|
return { success: false, error: "Invalid proxy settings" }
|
|
}
|
|
|
|
try {
|
|
// Save config to file
|
|
saveProxyConfig({
|
|
httpProxy: config.httpProxy,
|
|
httpsProxy: config.httpsProxy,
|
|
})
|
|
|
|
// Apply to current process environment
|
|
applyProxyToEnv()
|
|
|
|
if (!app.isPackaged) {
|
|
// In development, env vars are already applied
|
|
// Next.js dev server may need manual restart
|
|
return { success: true, devMode: true }
|
|
}
|
|
|
|
// Production: restart Next.js server to pick up new env vars
|
|
await restartNextServer()
|
|
return { success: true }
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to apply proxy settings",
|
|
}
|
|
}
|
|
})
|
|
|
|
// ==================== User Locale ====================
|
|
|
|
handle("get-user-locale", () => {
|
|
return getUserLocale()
|
|
})
|
|
|
|
handle("set-user-locale", (_event, locale: string) => {
|
|
// Validate locale is one of the supported values
|
|
if (!["en", "zh", "ja", "zh-Hant"].includes(locale)) {
|
|
return { success: false, error: "Invalid locale" }
|
|
}
|
|
|
|
try {
|
|
setUserLocale(locale as "en" | "zh" | "ja" | "zh-Hant")
|
|
// Rebuild the menu to reflect the new locale
|
|
rebuildAppMenu()
|
|
return { success: true }
|
|
} catch (error) {
|
|
return {
|
|
success: false,
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to set locale",
|
|
}
|
|
}
|
|
})
|
|
}
|