import fs from 'node:fs'; import path from 'node:path'; import { createHash } from 'node:crypto'; import { safePath } from '../../server/files.mjs'; import { assertLocalTree, samePath } from './file-state.mjs'; import { desktopInput, assertDesktopFrame, desktopError } from '../../shared/contracts/desktop-bridge.mjs'; import { ceiling } from '../../shared/contracts/execution-policy.mjs'; export class DesktopBridge { constructor(controller, { dialog, shell, getWindow, onStopped = () => {} }) { Object.assign(this, { controller, dialog, shell, getWindow, onStopped }); this.pending = new Set(); } async project(id) { const project = (await this.controller.api('/bootstrap')).projects.find(value => value.id === id); if (!project) throw desktopError('项目未在当前宿主登记。'); assertLocalTree(project.root); if (!samePath(project.root, fs.realpathSync.native(project.root))) throw desktopError('原项目目录已变化。'); return project; } async task(input) { const session = await this.controller.api('/sessions/' + input.sessionId); if (session.projectId !== input.projectId || session.execution?.task?.id !== input.taskId) throw desktopError('该入口不属于当前项目与Task,请刷新后核对。'); return session; } async execute(event, action, input) { const window = this.getWindow(); assertDesktopFrame(event, window, this.controller.url); const value = desktopInput(action, input); if (this.pending.has(action)) throw desktopError('同一项本机操作正在处理,请等待原操作。'); this.pending.add(action); try { if (!await this.controller.connect()) throw desktopError('原执行宿主已结束,请重新打开工作台。'); assertDesktopFrame(event, this.getWindow(), this.controller.url); if (action === 'status') return this.controller.api('/host/status'); if (action === 'pickProject') { const selected = await this.dialog.showOpenDialog(window, { title: '选择工作台项目', properties: ['openDirectory'], buttonLabel: '打开项目' }); assertDesktopFrame(event, this.getWindow(), this.controller.url); if (selected.canceled) return null; if (selected.filePaths.length !== 1 || !path.isAbsolute(selected.filePaths[0])) throw desktopError('请选择一个实际项目文件夹。'); assertLocalTree(selected.filePaths[0]); return this.controller.api('/projects', { method: 'POST', input: { root: selected.filePaths[0] } }); } if (action === 'projectFolder') { const project = await this.project(value.projectId); const error = await this.shell.openPath(project.root); if (error) throw desktopError('系统无法打开所属项目目录。'); return { opened: true, projectId: project.id, root: project.root }; } if (action === 'openFilePreview') { const access = await this.controller.api('/local-file-preview-access?url=' + encodeURIComponent(value.url)); if (!access.allowed || access.projectId !== value.projectId) throw desktopError('此预览已关闭或不属于当前项目。'); assertDesktopFrame(event, this.getWindow(), this.controller.url); await this.shell.openExternal(value.url); return { opened: true }; } if (action === 'reviewTask') { const session = await this.task(value); await this.project(value.projectId); return { projectId: session.projectId, sessionId: session.id, taskId: value.taskId, eventSeq: session.eventSeq }; } if (action === 'revealFile') { await this.task(value); const project = await this.project(value.projectId), target = safePath(project, value.path); assertLocalTree(target); const stat = fs.lstatSync(target); const { config } = await this.controller.api('/bootstrap'); if (!stat.isFile() || stat.nlink !== 1 || stat.size > ceiling(config, 1024 * 1024)) throw desktopError('原文件入口只接受当前工程的普通文本文件。'); const bytes = fs.readFileSync(target); if (bytes.includes(0) || createHash('sha256').update(bytes).digest('hex') !== value.sha256) throw desktopError('文件内容已变化或不是文本,请重新读取后定位。'); try { new TextDecoder('utf8', { fatal: true }).decode(bytes); } catch { throw desktopError('当前文件不是UTF-8文本,请先核对编码。'); } await this.task(value); assertLocalTree(target); if (createHash('sha256').update(fs.readFileSync(target)).digest('hex') !== value.sha256) throw desktopError('核对期间原文件已变化,请重新读取后定位。'); this.shell.showItemInFolder(target); return { revealed: true, projectId: project.id, sessionId: value.sessionId, taskId: value.taskId, path: value.path, sha256: value.sha256 }; } if (action === 'stopHost') { const status = await this.controller.status(); const choice = await this.dialog.showMessageBox(window, { type: 'warning', title: '退出执行宿主', message: '停止当前执行宿主?', detail: '所属任务、模型请求和工具进程会先结束。关闭普通窗口会保留后台任务。', buttons: ['保留后台任务', '停止并退出'], defaultId: 0, cancelId: 0, noLink: true }); assertDesktopFrame(event, this.getWindow(), this.controller.url); if (choice.response !== 1) return { cancelled: true }; const current = await this.controller.connect(); if (!current || current.identity.instanceId !== status.identity.instanceId || current.identity.runtimeIdentity !== status.identity.runtimeIdentity) throw desktopError('停止确认期间原宿主已变化,请重新核对。'); const result = await this.controller.stop(); this.onStopped(); return result; } } finally { this.pending.delete(action); } } }