protocol.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. import type { RemoteAssetInstallMode } from "./remoteAssetInstallMode.js";
  2. import type { RemoteResourcePackageSelection } from "./remoteResourcePackages.js";
  3. import type { ProviderFamilyDomain } from "./model-provider-family.js";
  4. import type { ProviderFamilyConnectionSelectionSettings } from "./provider-family-connection-selection.js";
  5. import type { ZCodeProvider } from "./zcode-task-types-core.js";
  6. import type { WorkspacePurpose } from "./workspacePurpose.js";
  7. import type { DynamicWorkflowMode } from "./dynamic-workflow-feature.js";
  8. import type { EmbeddedBrowserViewportPreference } from "./browser-use/command-metadata.js";
  9. import type { AppToolSearchMode } from "./app-runtime-preferences.js";
  10. // ── Domain types ──
  11. export interface FileEntry {
  12. name: string;
  13. path: string;
  14. type: "file" | "directory";
  15. /** 旧远端服务端可能不返回;调用方应按 false 处理。 */
  16. isSymbolicLink?: boolean;
  17. }
  18. export interface FileStat {
  19. path: string;
  20. type: "file" | "directory";
  21. /** 文件字节数;旧远端服务端可能不返回,调用方需按 undefined 处理。 */
  22. size?: number;
  23. /** 文件最后修改时间;分享分块读取用它检测读取期间的并发改写。 */
  24. mtimeMs?: number;
  25. }
  26. /** 文件系统变更事件(目录级粒度) */
  27. export interface FileWatchEvent {
  28. /** 发生变更的目录路径 */
  29. dirPath: string;
  30. /**
  31. * 能由底层 watcher 确认时返回发生变化的完整路径。
  32. * 旧 Host、平台未返回 filename 或同一防抖窗口包含多个路径时省略,调用方应保守刷新。
  33. */
  34. changedPath?: string;
  35. }
  36. export interface FileTextSlice {
  37. path: string;
  38. content: string;
  39. offset: number;
  40. bytesRead: number;
  41. totalBytes: number;
  42. truncated: boolean;
  43. isBinary: boolean;
  44. }
  45. export interface FileMediaPreview {
  46. path: string;
  47. mediaType: string;
  48. dataBase64: string;
  49. totalBytes: number;
  50. }
  51. export interface FileBinaryPreview {
  52. path: string;
  53. dataBase64: string;
  54. totalBytes: number;
  55. }
  56. export interface WorkspaceFileEntry {
  57. name: string;
  58. path: string;
  59. relativePath: string;
  60. type: "file" | "directory";
  61. }
  62. export interface SystemInfo {
  63. homedir: string;
  64. platform: string;
  65. }
  66. /** 支持的语言 */
  67. export type Locale = "zh-CN" | "en-US";
  68. /** 界面语言偏好;system 表示跟随当前运行端系统语言。 */
  69. export type LocalePreference = "system" | Locale;
  70. /** ZCode 运行中继续输入时的交互行为 */
  71. export type ZCodeInteractionBehavior = "queue" | "guide";
  72. /** 桌面端 Electron 自动更新发布通道。 */
  73. export type ElectronReleaseChannel = "stable" | "preview";
  74. /** Windows Bash 工具可使用的集成终端 shell 方言。 */
  75. export type IntegratedTerminalShellDialect = "cmd" | "git-bash";
  76. /** 设置页中 Windows Bash shell 的用户选择。 */
  77. export type IntegratedTerminalShellSelection =
  78. | { mode: "auto" }
  79. | {
  80. mode: "shell";
  81. dialect: IntegratedTerminalShellDialect;
  82. id: string;
  83. label: string;
  84. path: string;
  85. };
  86. /** 当前 host 可识别的 Windows shell 选项。 */
  87. export interface IntegratedTerminalShellOption {
  88. dialect: IntegratedTerminalShellDialect;
  89. id: string;
  90. label: string;
  91. path: string;
  92. source: "system" | "path";
  93. }
  94. /** 默认语言 */
  95. export const DEFAULT_LOCALE: Locale = "zh-CN";
  96. // ── Workspace / Tab ──
  97. /** 标签页唯一标识 */
  98. export type TabId = string;
  99. /** 单个标签页的状态 */
  100. export interface TabState {
  101. id: TabId;
  102. /** workspace 绝对路径 */
  103. workspacePath: string;
  104. /** 显示名称,通常为路径最后一段 */
  105. label: string;
  106. }
  107. export interface SSHRemoteTargetSnapshot {
  108. kind: "ssh";
  109. host: string;
  110. port?: number;
  111. username: string;
  112. /** 用户建立连接时选择的 SSH config Host alias,仅用于 UI 展示。 */
  113. sshConfigAlias?: string;
  114. privateKeyPath?: string;
  115. assetInstallMode?: RemoteAssetInstallMode;
  116. resourcePackages?: RemoteResourcePackageSelection;
  117. /**
  118. * SSH 密码不会写入 setting.json。
  119. * 这里只保存 credentialService 的键名,恢复时再去安全存储读取真实密码。
  120. */
  121. passwordCredentialKey?: string;
  122. /**
  123. * 私钥口令不会写入 setting.json。
  124. * 这里只保存 credentialService 的键名,恢复时再去安全存储读取真实口令。
  125. */
  126. privateKeyPassphraseCredentialKey?: string;
  127. }
  128. export interface WSLRemoteTargetSnapshot {
  129. kind: "wsl";
  130. distro?: string;
  131. user?: string;
  132. }
  133. export interface DockerRemoteTargetSnapshot {
  134. kind: "docker";
  135. container: string;
  136. }
  137. export interface ServerRemoteTargetSnapshot {
  138. kind: "server";
  139. url: string;
  140. name?: string;
  141. workspacePath?: string;
  142. serverId?: string;
  143. /**
  144. * Server token 不写入 setting.json。
  145. * 这里只保存 credentialService 的键名,恢复时再读取真实 token。
  146. */
  147. tokenCredentialKey?: string;
  148. }
  149. export type RemoteTargetSnapshot =
  150. | SSHRemoteTargetSnapshot
  151. | WSLRemoteTargetSnapshot
  152. | DockerRemoteTargetSnapshot
  153. | ServerRemoteTargetSnapshot;
  154. export interface RemoteWorkspaceSessionSnapshot {
  155. /** 远程 workspace 的真实绝对路径 */
  156. workspacePath: string;
  157. /** 用户可见的 workspace 分类;旧远端快照缺失时按 project 兼容读取。 */
  158. workspacePurpose?: WorkspacePurpose;
  159. /** 发起远程连接时的本机 workspace 路径,仅用于 MCP filesystem 路径改写。 */
  160. localWorkspacePath?: string;
  161. /** 远程 workspace 的稳定身份键(authority + canonicalPath)。 */
  162. workspaceIdentity?: string;
  163. /** 远程连接目标的可恢复快照 */
  164. target: RemoteTargetSnapshot;
  165. /** 最近一次成功打开该 workspace 的时间戳 */
  166. lastOpenedAt: number;
  167. /** 最近一次恢复/重连结果 */
  168. lastConnectionStatus: "connected" | "failed";
  169. /** 最近一次失败原因;成功后清空 */
  170. lastConnectionError?: string;
  171. /** 最近一次失败的错误码,与 lastConnectionError 同写同清;等待点也写成失败,只看文案分不清两类 */
  172. lastConnectionErrorCode?: string;
  173. }
  174. export interface LocalWorkspaceSessionEntry {
  175. kind: "local";
  176. workspacePath: string;
  177. /** 项目展示分类;旧数据缺省为 project,conversation 仍使用真实 workspacePath 作为 cwd/key。 */
  178. workspacePurpose?: WorkspacePurpose;
  179. }
  180. export interface RemoteWorkspaceSessionEntry extends RemoteWorkspaceSessionSnapshot {
  181. kind: "remote";
  182. }
  183. export type PersistedWorkspaceSessionEntry =
  184. | LocalWorkspaceSessionEntry
  185. | RemoteWorkspaceSessionEntry;
  186. // ── Process Monitor ──
  187. /** 资源管理器分类:基础服务 / 内置插件 / 社区插件 */
  188. export type ResourceUsageCategory = "base" | "builtin-plugin" | "community-plugin";
  189. /** 基础服务分组键;插件分组键为插件名或 MCP server name */
  190. export type ResourceUsageBaseGroupKey = "main" | "gpu" | "renderer" | "host" | "cli" | "utility";
  191. /** 资源管理器中的一个进程行(CPU 为整机归一化百分比,内存为字节) */
  192. export interface ResourceUsageProcess {
  193. pid: number;
  194. /** 进程显示名,如 zcode-main / zcode-agent-zcode-demo / node_repl */
  195. name: string;
  196. category: ResourceUsageCategory;
  197. groupKey: string;
  198. groupLabel: string;
  199. cpuPercent: number;
  200. memoryBytes: number;
  201. /** false 表示只知道拓扑,指标尚未采到(显示为 —) */
  202. sampled: boolean;
  203. }
  204. /** Host 侧采样得到的外部进程行(Agent / MCP / 终端等),由 main 合并进快照 */
  205. export type HostResourceUsageProcess = Omit<ResourceUsageProcess, "sampled">;
  206. /** 资源管理器一次完整快照,由 main 进程合并 Electron 指标、系统总量与 Host 采样后返回 */
  207. export interface ResourceUsageSnapshot {
  208. sampledAt: number;
  209. logicalCpuCount: number;
  210. system: {
  211. cpuPercent: number;
  212. memoryTotalBytes: number;
  213. memoryUsedBytes: number;
  214. };
  215. app: {
  216. cpuPercent: number;
  217. memoryBytes: number;
  218. };
  219. processes: ResourceUsageProcess[];
  220. }
  221. export interface AppSettings {
  222. /** 当前 App/Host 不再显示提交前体验套餐推荐;不改变任何入口的模型选择。 */
  223. startPlanRecommendationDismissed?: boolean;
  224. recentProjects: string[]; // 最近项目列表,最多保留 10 个
  225. locale: Locale; // 界面语言
  226. /**
  227. * 用户覆盖的快捷键绑定(命令 ID → 绑定串数组,格式见 shortcutCommands.ts)。
  228. * 只存用户覆盖:未覆盖的命令不落盘,读取时与 SHORTCUT_COMMANDS 默认绑定合并;
  229. * 覆盖语义为整组替换(如 openCommandCenter 的双默认绑定被覆盖时同时失效)。
  230. */
  231. shortcutBindings?: Record<string, string[]>;
  232. /** 界面语言偏好;locale 保存偏好解析后的实际语言,供 main/menu/远控等非浏览器上下文使用。 */
  233. localePreference?: LocalePreference;
  234. /** 是否尽量继承系统终端 profile、shell 环境和字体 */
  235. terminalInheritSystemProfile?: boolean;
  236. /** 用户显式覆盖的终端字体;为空时从系统终端配置自动探测 */
  237. terminalFontFamily?: string;
  238. /** Windows 下 Bash 工具使用的本机 shell;未配置时自动选择。 */
  239. integratedTerminalShell?: IntegratedTerminalShellSelection;
  240. /** HTTP/HTTPS 出口代理,例如 http://127.0.0.1:7890;为空时直连。下次启动 app/agent 生效。 */
  241. httpProxy?: string;
  242. /** 代理绕过规则,例如 localhost,127.0.0.1,.example.com;只在 httpProxy 存在时影响 renderer,agent/tool 仍按显式环境使用。 */
  243. httpProxyNoProxy?: string;
  244. /** 自定义 PEM 根证书路径;下次启动 app/agent 时用于 renderer 校验与 agent NODE_EXTRA_CA_CERTS。 */
  245. httpProxyCaCertPath?: string;
  246. /**
  247. * 内置浏览器忽略 HTTPS 证书校验错误(自签名、过期、域名不匹配等),用于访问内网测试站点。
  248. * 只影响内置浏览器出口,不影响 ZCode 自身对后端与模型 API 的请求。默认关闭,重启后生效。
  249. */
  250. embeddedBrowserAllowInsecureCertificates?: boolean;
  251. /** 人类用户主动打开 Browser tab 时的一次性显示偏好;Agent Browser Use 不读写。 */
  252. embeddedBrowserViewportPreference?: EmbeddedBrowserViewportPreference;
  253. /**
  254. * 用户已在设置页关闭输入框的「电脑操作」按钮(内部 hidden 态)。
  255. * 取 hidden 语义而非 visible:undefined 即默认显示,老用户无需数据迁移。
  256. * 关闭后按钮不再渲染,且不因重启或版本更新自愈,仅能在设置页重新开启。
  257. */
  258. computerUseComposerEntryHidden?: boolean;
  259. /** 自动归档旧会话的总开关 */
  260. taskAutoArchiveEnabled?: boolean;
  261. /** 自动归档修复后的一次性关闭标记;只用于设置迁移,不参与归档判断。 */
  262. taskAutoArchiveResetVersion?: 1;
  263. /** 自动归档阈值;当任务最后更新时间早于该天数时允许被归档 */
  264. taskAutoArchiveOlderThanDays?: number;
  265. /** 桌面端关闭窗口时隐藏到托盘驻留(Windows/Linux;spec:docs/desktop/linux-close-to-tray.md) */
  266. closeToTrayOnWindows?: boolean;
  267. /** 存在执行中的闲时任务时阻止系统闲置休眠(手动开关,防不了合盖)。 */
  268. keepAwakeWhileRunning?: boolean;
  269. /** Windows 关闭到托盘默认值是否已执行过一次性迁移;只用于设置迁移,不参与业务判断。 */
  270. closeToTrayOnWindowsMigrationInitialized?: boolean;
  271. /**
  272. * Linux 关闭到托盘默认值(关)是否已执行过一次性归位迁移。
  273. * shared 迁移无平台概念、曾把存量 Linux 用户归为 true,而 Linux 此前无设置入口,
  274. * main 进程 bootstrap 借此标记把首次启动的 Linux 用户一次性归位为默认关。
  275. */
  276. closeToTrayLinuxMigrationInitialized?: boolean;
  277. /** 桌面端全局页面缩放档位;用于重启后恢复界面缩放,Web/手机端忽略。 */
  278. desktopZoomLevel?: number;
  279. /** 桌面主窗口最近一次非最大化宽高及最大化状态;Web/手机端忽略。 */
  280. desktopWindowSize?: {
  281. width: number;
  282. height: number;
  283. maximized: boolean;
  284. };
  285. /** 桌面端 Chromium 硬件加速开关;只在下次启动 main 进程早期生效,Web/手机端忽略。 */
  286. desktopChromiumHardwareAccelerationEnabled?: boolean;
  287. /** 是否在消息流中展示模型思考过程 */
  288. messageStreamShowReasoning?: boolean;
  289. // TODO(settings-schema-version): 能证明所有受支持升级路径都已执行本次迁移后,改用统一 settings schema version,并一起删除此 marker、迁移函数和持久化判断。
  290. /** 显示模型思考过程的默认值是否已执行过一次性迁移;只用于设置迁移,不参与消息渲染判断。 */
  291. messageStreamShowReasoningMigrationInitialized?: boolean;
  292. /** 是否在消息流中展示 todo 工具渲染;不影响摘要面板的 todo */
  293. messageStreamShowTodos?: boolean;
  294. /** 是否把连续的只读工具调用聚合成 Explore。 */
  295. toolGroupingExploreEnabled?: boolean;
  296. /** 是否把连续的非只读 Shell 工具调用聚合成 Terminal。 */
  297. toolGroupingTerminalEnabled?: boolean;
  298. /** 是否把连续的 Write/Edit/ApplyPatch 工具调用聚合成 Changes。 */
  299. toolGroupingChangesEnabled?: boolean;
  300. /** ZCode 运行中继续输入时,是排队到下一轮,还是引导到下一次工具调用后运行 */
  301. zcodeInteractionBehavior?: ZCodeInteractionBehavior;
  302. /** Agent 提问五分钟无人回答时是否允许自动继续;缺失按开启兼容旧配置。 */
  303. askUserQuestionAutoResolutionEnabled?: boolean;
  304. /** 是否完整保留 Model I/O;开启后不轮转、不限额重置、不压缩或裁剪,鉴权信息仍会脱敏。 */
  305. modelIoFullRetentionEnabled?: boolean;
  306. /**
  307. * 用户选的动态工作流模式(docs/dynamic-workflow/launch.md「The user's choice」)。缺席 = 跟随服务端
  308. * 提供的模式;只在功能已提供时生效。选回提供的模式时删除该字段,未改动的安装继续跟随服务端翻转。
  309. */
  310. dynamicWorkflowMode?: DynamicWorkflowMode;
  311. /** 已启用的内置 Agent CLI;UI 对外仍展示为“Install”,底层语义保持为启用内置能力。 */
  312. enabledBuiltinAgentCliProviders?: ZCodeProvider[];
  313. /** 设置页中每个 Provider Family 当前唯一的结构化连接选择。 */
  314. providerFamilyConnectionSelections?: ProviderFamilyConnectionSelectionSettings;
  315. /** 用户通过 WelcomeScreen 成功连接后确认的 ZAI / BigModel provider family 运行域。 */
  316. providerFamilyDomain?: ProviderFamilyDomain;
  317. /** 最近一次设置或清空 providerFamilyDomain 的时间。 */
  318. providerFamilyDomainUpdatedAt?: number;
  319. /** 旧 oauth/provider 状态是否已经尝试迁移到 providerFamilyDomain。 */
  320. providerFamilyDomainMigrated?: boolean;
  321. /** 新建或冷恢复 Session 是否为 Bash 注入 bfs/ugrep 增强;默认启用。 */
  322. nativeSearchEnhancementsEnabled?: boolean;
  323. /** 新建或冷恢复时读取;已有 Runtime 保持原策略。 */
  324. toolSearchMode?: AppToolSearchMode;
  325. /** 新建或冷恢复 Session 是否启用 Memory;默认关闭。 */
  326. memoryEnabled?: boolean;
  327. onboardingOccupation?:
  328. | "office"
  329. | "developer"
  330. | "independent"
  331. | "infrastructure"
  332. | "product"
  333. | "design"
  334. | "student"
  335. | "creator"
  336. | "operations"
  337. | "marketing"
  338. | "finance"
  339. | "accounting"
  340. | "legal"
  341. | "other"
  342. | null;
  343. proactiveSuggestionsEnabled?: boolean;
  344. /** 新版只保存活动执行目录;不表达项目/连接成员,null 表示明确清空。 */
  345. lastActiveWorkspace?: PersistedWorkspaceSessionEntry | null;
  346. /** Host 从项目/本机入口派生,供旧版降级读取;新版不作为入口真相。 */
  347. lastWorkspaceSession?: PersistedWorkspaceSessionEntry[];
  348. /** 每个 Source Server 已解析的 canonical conversation backing workspace。 */
  349. remoteConversationWorkspaceBySourceServerId?: Record<
  350. string,
  351. {
  352. workspacePath: string;
  353. lastResolvedAt: number;
  354. }
  355. >;
  356. /** R3 控制端已识别的 Source Server;运行态仍由远端 Server 权威提供。 */
  357. sourceServers?: import("./source-server/contracts.js").SourceServerRecord[];
  358. /** R3 到达 Source Server 的线路;只保存 credential key,不保存 secret。 */
  359. connectionProfiles?: import("./source-server/contracts.js").ConnectionProfile[];
  360. /** R3 workspace 打开历史;连接线路与 workspace 实体分离。 */
  361. sourceServerWorkspaceHistory?: import("./source-server/contracts.js").SourceServerWorkspaceHistoryEntry[];
  362. /** 桌面端启动时是否执行 Source Server 静默重连;缺失按 true 兼容旧设置。 */
  363. remoteStartupAutoReconnectEnabled?: boolean;
  364. sourceServerDirectoryMigrationVersion?: 1;
  365. /** 旧客户端只接受 2;保留回滚兼容,新介绍不使用该字段判断已读。 */
  366. remoteBoundaryOnboardingVersion?: 2;
  367. /** 仅表示用户明确确认持续运行介绍。 */
  368. remoteBoundaryIntroAcknowledgedVersion?: 1;
  369. /** 上次关闭时激活的 tab 索引 */
  370. lastActiveTabIndex?: number;
  371. /** 每个 workspace 的最后活跃 taskId,下次打开自动恢复 */
  372. lastActiveTaskByWorkspace?: Record<string, string>;
  373. /** 数据目录的根路径(替代 homedir),默认为 os.homedir();.zcode/v2 后缀不变 */
  374. dataBaseDir?: string;
  375. /** 自动更新安装完成后,等待首次启动展示的版本说明 */
  376. pendingPostUpdateReleaseNotes?: {
  377. version: string;
  378. title: string;
  379. markdown: string;
  380. releaseDate?: string;
  381. releaseNotesByLocale?: Partial<Record<Locale, { title: string; markdown: string }>>;
  382. };
  383. /** 设置页“接收 preview 自动更新”偏好;仅桌面端自动更新读取。 */
  384. receivePreviewUpdates?: boolean;
  385. /** 设置页/更新弹窗“以后自动下载并安装更新”偏好;仅桌面端自动更新读取。 */
  386. autoDownloadAndInstallUpdates?: boolean;
  387. /** 用户跳过的 Electron 自动更新版本;按通道隔离,避免 stable / preview 互相遮挡。 */
  388. skippedElectronUpdateVersions?: Partial<Record<ElectronReleaseChannel, string>>;
  389. /** 首次启动设置同步提示是否已消费;只表示弹窗不再出现,不代表导入成功。 */
  390. settingsSyncFirstRunPromptHandled?: boolean;
  391. /** 外部 Web 远控 relay 的非敏感设备身份;passHash 必须存 credentialService。 */
  392. /** V2 非敏感注册身份;JWT 和一次性 token 不持久化于 AppSettings。 */
  393. webRemoteControlRelayV2Device?: {
  394. deviceSid: string;
  395. userId: string;
  396. endpointOrigin: string;
  397. };
  398. webRemoteControlExternalRelayDevice?: {
  399. deviceSid: string;
  400. };
  401. /** 上次成功开启 Web 远控时的目标;缺失表示用户已明确关闭,不自动恢复。 */
  402. webRemoteControlLastEnabledContext?: {
  403. workspacePath: string;
  404. workspaceIdentity?: string;
  405. initialTaskId?: string;
  406. };
  407. /** 设置页里的临时 endpoint override;正式/测试默认 base url 由 ZCODE_BASE_URL env 管理。 */
  408. zcodeEndpointOrigin?: string;
  409. }