P0 会话可靠性收口(根治"模型思考着会话就停止"): - P0-1 finish_reason 全链路贯通:DONE 事件与 IterationStep 新增 finishReason,OpenAI 共享 SSE / Anthropic message_delta.stop_reason / Ollama done_reason 三路采集,TRACE 层弃用硬编码 'stop' 记录真值 - P0-2 空响应守卫 + 降级重试:零产出流→可重试错误走退避;思考耗尽输出预算(reasoning-only + length)→自动关闭思考降级重试一次;仍失败→OUTPUT_LENGTH_EXCEEDED 结构化错误 + 故障转移;附带根治 abort 恰逢零工具调用轮被 COMPLETED 抢占的真实缺陷 - P0-3 思考×能力×预算三对齐:DeepSeek/MiMo/Agnes/Ollama 四家 supportsThinking=false 强制不发思考参数;小输出预算告警;设置页联动提示 - P0-4 渲染层可见性:截断/空完成/友好错误三类提示,i18n 全部出层 - P0-5 回归四件套:reasoning-only 终止判定、集成级空闲超时、504 引擎重试归类、思考中 abort→USER_INTERRUPT、P4-2 强制收尾路径 FEAT-1:LLM 设置新增「最大输出上限」——Provider 支持矩阵显隐 + 模型上限钳制提示 + 超限保存警告 + llm.maxTokens 热生效 P1 修复面收口: - 渲染层三缺陷根治:后台会话回放缓冲(2000 条/4MB 有界 + agent:getReplayState + 事件总线)+ abort 双层自愈 + sendMessage 收尾兜底 + 中断卡片清扫 - 工具 abort 信号全覆盖:web_search/web_fetch/http_request/code_search/git 系列/delegate_task 全部接入引擎中断;web_search 时间预算收敛(720s→≤240s);移除伪造 ToolExecutionContext 与死代码 - 安全:本地 Pinned CONNECT 代理根治浏览器通道 DNS rebinding(校验期 IP pinning,可注入 resolver 表测);配置 URL 域名解析深校验(DeepCheckSoftFailure 软失败);SSE 空 error 帧防御修复;Ollama generate/embed AbortSignal.any 合并 - 缺陷清单:UTF-16 BOM 读取、tmp 同毫秒碰撞(nanoid 后缀)、code_search JS 回退参数对称(case_sensitive/前后文独立)、list_directory include_node_modules、崩溃自愈退避(60s 窗 ≥3 次停 reload)、MemoryViewer/Sidebar i18n 收口 P2 能力演进: - 会话回收站:SCHEMA_VERSION 3 + 迁移 10(deleted_at,存在性守卫),软删除/恢复/彻底删除/30 天自动清理(启动+24h),searchMessages 聚合剔除,Sidebar 回收站面板 - 会话回放播放器:sessions:listRecordings/readRecording(白名单+目录边界+20MB 上限),SessionReplayPlayer 时间轴/步进/变速,Trace 面板入口 - electron-updater 自动更新:双轨(手动 feed 比对保留),生产环境启动静默检查 + update:status 广播 + app:updateInstall + LogsSettings UpdatePanel + builder publish 配置 - @ 文件提及:workspace.listFiles/readFileClip(边界/512KB/NUL 拒绝/MEMORY.md 保护),ChatInput Fuse 联想+键盘导航+附件管线注入 - MCP Resources/Prompts 发现:可选能力 try/catch 降级,mcp:listServerContents,MCPSettings 展开视图 - 文档对齐:内部 API 标准 HTML(Adapter 清单补 MiMo/已实现注记/STREAM_RESET/DONE.finishReason/ repetition_truncation 映射);README v0.8.0 亮点表 P3 测试基建: - 新增 4 个测试文件:engine-stream-contract(6)、engine-stream-reliability(4:集成空闲超时/504 重试/思考中 abort/P4-2 强制收尾)、thinking-capability-gate(7)、pinned-proxy(9,含深校验 5)、session-trash(5,DB 域)、use-agent-stream hook 级(5)、agent.test 回放缓冲(2) - 契约更新:orchestrator 被中断 SubAgent success=false(abort 优先级修复语义)、SSE 空 error 帧、UTF-16 正常读取、DeepSeek 未配置思考显式 disabled、迁移矩阵 v2→3 - 弱断言根治:registry WEBP 单向断言、hooks-contracts 自比恒真、memory 空 token 补强 全量验证:typecheck 0 错误 / lint 0 问题 / 系统 Node 2144 通过(301 DB 用例按 ABI 跳过)/ Electron ABI 2445/2445 全量通过 0 跳过
315 lines
10 KiB
TypeScript
315 lines
10 KiB
TypeScript
/**
|
||
* Window Manager — 多窗口管理
|
||
*
|
||
* 职责:
|
||
* 1. 创建和管理多个窗口(每个工作空间可独立开窗口)
|
||
* 2. 窗口状态持久化(位置、大小)
|
||
* 3. 全局快捷键注册(Cmd+Shift+M 切换到 Metona)
|
||
* 4. 关闭到托盘行为
|
||
*
|
||
* @see docs/MetonaAI-Desktop UI UX 设计集成方案.html — 窗口管理
|
||
*/
|
||
|
||
import { BrowserWindow, globalShortcut, shell } from 'electron';
|
||
import { join } from 'path';
|
||
import { existsSync } from 'fs';
|
||
import { is } from '@electron-toolkit/utils';
|
||
import log from 'electron-log';
|
||
|
||
export interface WindowState {
|
||
x?: number;
|
||
y?: number;
|
||
width: number;
|
||
height: number;
|
||
isMaximized?: boolean;
|
||
}
|
||
|
||
export class WindowManager {
|
||
private windows = new Map<string, BrowserWindow>();
|
||
private activeWindowId: string | null = null;
|
||
/**
|
||
* v0.8.0 P1-3.7: 渲染进程崩溃自愈退避 —— 按 window id 记录 { count, windowStart }。
|
||
* 60s 窗口内连续崩溃 ≥3 次不再自动 reload(避免 crash→reload→crash 死循环),
|
||
* 改弹系统错误对话框;自上次 reload 起 5 分钟无崩溃则计数复位。
|
||
*/
|
||
private crashReloadAttempts = new Map<
|
||
number,
|
||
{ count: number; windowStart: number; lastAt: number }
|
||
>();
|
||
private static readonly CRASH_RELOAD_MAX = 3;
|
||
private static readonly CRASH_RELOAD_WINDOW_MS = 60_000;
|
||
private static readonly CRASH_RELOAD_RESET_MS = 5 * 60_000;
|
||
|
||
/**
|
||
* 创建新窗口
|
||
*/
|
||
createWindow(options: {
|
||
id?: string;
|
||
workspacePath?: string;
|
||
title?: string;
|
||
state?: WindowState;
|
||
beforeLoad?: (win: BrowserWindow) => void;
|
||
}): BrowserWindow {
|
||
const id = options.id ?? `window_${Date.now()}`;
|
||
const state = options.state ?? { width: 1440, height: 900 };
|
||
|
||
const win = new BrowserWindow({
|
||
width: state.width,
|
||
height: state.height,
|
||
x: state.x,
|
||
y: state.y,
|
||
minWidth: 960,
|
||
minHeight: 600,
|
||
icon: this.getIconPath(),
|
||
show: false,
|
||
titleBarStyle: 'hiddenInset',
|
||
title: options.title ?? 'MetonaAI Desktop',
|
||
webPreferences: {
|
||
// v0.6.4 安全加固: preload 已迁移为 CJS 产物(preload.cjs,见 electron.vite.config.ts
|
||
// 的 rollupOptions.output.format:'cjs'),原 ESM .mjs 与 sandbox 不兼容的限制解除。
|
||
// sandbox:true 后渲染进程即使被 XSS 也无法触碰 Node/加载任意模块 —— 这是
|
||
// Electron 官方推荐的最高优先级防线(Electron 安全清单第 1 条)。
|
||
preload: join(__dirname, '../preload/preload.cjs'),
|
||
sandbox: true,
|
||
contextIsolation: true,
|
||
nodeIntegration: false,
|
||
},
|
||
});
|
||
|
||
// P2-11 修复: 在 loadURL 之前执行回调,确保 IPC handler 在渲染进程加载前注册
|
||
if (options.beforeLoad) options.beforeLoad(win);
|
||
|
||
// 加载页面
|
||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
|
||
win.loadURL(process.env['ELECTRON_RENDERER_URL']);
|
||
} else {
|
||
win.loadFile(join(__dirname, '../../dist/index.html'));
|
||
}
|
||
|
||
// 窗口事件
|
||
win.on('ready-to-show', () => {
|
||
if (state.isMaximized) {
|
||
win.maximize();
|
||
}
|
||
win.show();
|
||
});
|
||
|
||
win.on('closed', () => {
|
||
this.windows.delete(id);
|
||
if (this.activeWindowId === id) {
|
||
this.activeWindowId =
|
||
this.windows.size > 0 ? (this.windows.keys().next().value ?? null) : null;
|
||
}
|
||
});
|
||
|
||
// 崩溃可观测性 + 自愈: 渲染进程崩溃(OOM/原生崩溃)时记录归因日志并自动重载,
|
||
// 替代静默白屏(用户此前感知为"应用崩溃"且无从恢复)
|
||
// v0.8.0 P1-3.7: 增加退避 —— 60s 内连续 ≥3 次崩溃停止自动 reload 并弹窗
|
||
//(根因持久存在时旧实现会 crash→reload→crash 死循环);稳定 5 分钟后计数复位。
|
||
win.webContents.on('render-process-gone', (_event, details) => {
|
||
log.error(
|
||
`[WindowManager] render-process-gone (id=${win.id}): reason=${details.reason} exitCode=${details.exitCode}`,
|
||
);
|
||
if (!win.isDestroyed() && details.reason !== 'clean-exit') {
|
||
const now = Date.now();
|
||
const record = this.crashReloadAttempts.get(win.id) ?? {
|
||
count: 0,
|
||
windowStart: now,
|
||
lastAt: 0,
|
||
};
|
||
// 稳定 5 分钟 → 计数复位
|
||
if (record.lastAt > 0 && now - record.lastAt > WindowManager.CRASH_RELOAD_RESET_MS) {
|
||
record.count = 0;
|
||
record.windowStart = now;
|
||
}
|
||
// 60s 滑动窗口外 → 重新起算
|
||
if (now - record.windowStart > WindowManager.CRASH_RELOAD_WINDOW_MS) {
|
||
record.count = 0;
|
||
record.windowStart = now;
|
||
}
|
||
record.count += 1;
|
||
record.lastAt = now;
|
||
this.crashReloadAttempts.set(win.id, record);
|
||
|
||
if (record.count > WindowManager.CRASH_RELOAD_MAX) {
|
||
log.error(
|
||
`[WindowManager] Crash reload threshold exceeded (${record.count} in ${WindowManager.CRASH_RELOAD_WINDOW_MS / 1000}s) — NOT reloading automatically (crash loop protection)`,
|
||
);
|
||
try {
|
||
// showErrorBox 为同步 API(void 返回),崩溃循环保护路径直接弹窗
|
||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||
const { dialog } = require('electron') as typeof import('electron');
|
||
dialog.showErrorBox(
|
||
'MetonaAI Desktop 反复崩溃',
|
||
`渲染进程在 ${WindowManager.CRASH_RELOAD_WINDOW_MS / 1000} 秒内连续崩溃 ${record.count} 次,已停止自动恢复。\n\n请重启应用;若反复出现,请携带日志(设置 → 日志与数据 → 打开日志目录)反馈。`,
|
||
);
|
||
} catch {
|
||
/* dialog 不可用时仅保留日志 */
|
||
}
|
||
return;
|
||
}
|
||
|
||
try {
|
||
win.webContents.reload();
|
||
log.info(`[WindowManager] Window ${id} reloaded after renderer crash`);
|
||
} catch (err) {
|
||
log.error(`[WindowManager] Reload after crash failed:`, err);
|
||
}
|
||
}
|
||
});
|
||
|
||
win.webContents.setWindowOpenHandler(({ url }) => {
|
||
// C-8 修复: 校验 URL 协议,只允许 http/https,防止 javascript:/file: 等协议执行代码
|
||
try {
|
||
const parsed = new URL(url);
|
||
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
|
||
shell.openExternal(url);
|
||
} else {
|
||
log.warn(`[WindowManager] Blocked window.open with unsafe protocol: ${parsed.protocol}`);
|
||
}
|
||
} catch {
|
||
log.warn(`[WindowManager] Blocked window.open with invalid URL: ${url.slice(0, 100)}`);
|
||
}
|
||
return { action: 'deny' };
|
||
});
|
||
|
||
// v0.6.4 安全加固: will-navigate 拦截 —— SPA 应用主框架不应发生顶层导航;
|
||
// 除开发服务器热更新入口外的一切导航一律取消,http(s) 外链转系统浏览器。
|
||
// (此前渲染进程若被注入 webContents.location=... 可静默替换页面。)
|
||
win.webContents.on('will-navigate', (event, url) => {
|
||
const isDevServer =
|
||
process.env['ELECTRON_RENDERER_URL'] !== undefined &&
|
||
(() => {
|
||
try {
|
||
return new URL(url).origin === new URL(process.env['ELECTRON_RENDERER_URL']!).origin;
|
||
} catch {
|
||
return false;
|
||
}
|
||
})();
|
||
const isAppFile = url.startsWith('file://');
|
||
if (!isDevServer && !isAppFile) {
|
||
event.preventDefault();
|
||
log.warn(`[WindowManager] Blocked top-level navigation to ${url.slice(0, 120)}`);
|
||
try {
|
||
const parsed = new URL(url);
|
||
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
|
||
void import('electron').then(({ shell }) => shell.openExternal(url));
|
||
}
|
||
} catch {
|
||
/* 非 URL 一律丢弃 */
|
||
}
|
||
}
|
||
});
|
||
|
||
this.windows.set(id, win);
|
||
this.activeWindowId = id;
|
||
|
||
log.info(`Window created: ${id} (${options.title ?? 'MetonaAI Desktop'})`);
|
||
|
||
return win;
|
||
}
|
||
|
||
/**
|
||
* 获取窗口
|
||
*/
|
||
getWindow(id: string): BrowserWindow | undefined {
|
||
return this.windows.get(id);
|
||
}
|
||
|
||
/**
|
||
* 获取活动窗口
|
||
*/
|
||
getActiveWindow(): BrowserWindow | null {
|
||
if (this.activeWindowId) {
|
||
return this.windows.get(this.activeWindowId) ?? null;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 获取所有窗口
|
||
*/
|
||
getAllWindows(): BrowserWindow[] {
|
||
return Array.from(this.windows.values());
|
||
}
|
||
|
||
/**
|
||
* 聚焦到窗口(从任意应用切换)
|
||
*/
|
||
focusWindow(): void {
|
||
const win = this.getActiveWindow();
|
||
if (win) {
|
||
if (win.isMinimized()) {
|
||
win.restore();
|
||
}
|
||
win.show();
|
||
win.focus();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 注册全局快捷键
|
||
*/
|
||
registerGlobalShortcuts(): void {
|
||
// Cmd/Ctrl+Shift+M — 从任意应用切换到 Metona
|
||
const registered = globalShortcut.register('CommandOrControl+Shift+M', () => {
|
||
this.focusWindow();
|
||
log.info('Global shortcut: Switch to Metona');
|
||
});
|
||
|
||
if (!registered) {
|
||
log.warn('Failed to register global shortcut: Cmd+Shift+M');
|
||
} else {
|
||
log.info('Global shortcut registered: Cmd+Shift+M');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 注销全局快捷键
|
||
*/
|
||
unregisterGlobalShortcuts(): void {
|
||
globalShortcut.unregisterAll();
|
||
log.info('Global shortcuts unregistered');
|
||
}
|
||
|
||
/**
|
||
* 关闭所有窗口
|
||
*/
|
||
closeAll(): void {
|
||
for (const [id, win] of this.windows) {
|
||
try {
|
||
win.destroy();
|
||
} catch {
|
||
log.warn(`Failed to close window: ${id}`);
|
||
}
|
||
}
|
||
this.windows.clear();
|
||
this.activeWindowId = null;
|
||
}
|
||
|
||
/**
|
||
* 获取窗口数量
|
||
*/
|
||
get count(): number {
|
||
return this.windows.size;
|
||
}
|
||
|
||
/**
|
||
* 获取应用图标路径
|
||
*/
|
||
private getIconPath(): string | undefined {
|
||
const candidates = [
|
||
join(__dirname, '../../assets/logo.ico'),
|
||
join(__dirname, '../../assets/logo.png'),
|
||
join(process.resourcesPath || '', 'app.asar.unpacked', 'assets/logo.ico'),
|
||
join(process.resourcesPath || '', 'app.asar.unpacked', 'assets/logo.png'),
|
||
join(process.resourcesPath || '', 'assets/logo.ico'),
|
||
join(process.resourcesPath || '', 'assets/logo.png'),
|
||
];
|
||
for (const p of candidates) {
|
||
if (existsSync(p)) return p;
|
||
}
|
||
return undefined;
|
||
}
|
||
}
|