workspace-config.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // workspace-config topic(v4 additive 新增):workspace 级配置目录的活性数据源。
  2. // 背景:旧协议里 provider registry / 模型目录热更新经 per-session `state.updated`(settings patch)
  3. // 下发,host 侧 zcodeTaskIndexSyncer 转成 workspace_config_options_update 广播给 UI;
  4. // 旧 session/subscribe + state.updated 词表不承载配置目录,统一改走本 topic。
  5. // 语义:conflated 最新态(同 sessions-index),载荷是 workspace 级配置目录 + slash 命令目录。
  6. // 注意这是 workspace 级不是 session 级——session 级 current 选择在 conversation topic 的
  7. // `config`(sessionConfigStateSchema)里;本 topic 的 currentValue 表示 workspace 缺省。
  8. // 纪律:additive 演进——新增 topic / 新增可选字段合法,改已有字段形状不合法。
  9. import { z } from "zod";
  10. // 与 host 侧 ZCodeConfigSelectValue(zcode-task-types-core)结构对齐:
  11. // syncer 转发 workspace_config_options_update 时零映射直通,下游 useZCodeConfig 消费面不改。
  12. export const workspaceConfigSelectValueSchema = z.object({
  13. value: z.string(),
  14. name: z.string(),
  15. description: z.string().optional(),
  16. // 值来源:原生模型列表或会话侧注入项(UI 去重与展示控制)。
  17. origin: z.enum(["native", "injected"]).optional(),
  18. // 模型选项所属供应商/分组 id(provider → model 分组选择)。
  19. modelProviderId: z.string().optional(),
  20. modelProviderName: z.string().optional(),
  21. // 缺失表示旧 payload/能力未知;空数组表示 catalog 已知没有可选 reasoning 档位。
  22. modelThoughtLevels: z.array(z.string()).optional(),
  23. modelDefaultThoughtLevel: z.string().optional(),
  24. });
  25. export type WorkspaceConfigSelectValue = z.infer<typeof workspaceConfigSelectValueSchema>;
  26. // 与 host 侧 ZCodeConfigOption 结构对齐(id: model / mode / thought_level / 自定义)。
  27. export const workspaceConfigOptionSchema = z.object({
  28. id: z.string(),
  29. name: z.string(),
  30. description: z.string().optional(),
  31. category: z.string().optional(),
  32. type: z.enum(["select", "boolean"]),
  33. currentValue: z.union([z.string(), z.boolean()]),
  34. options: z.array(workspaceConfigSelectValueSchema).optional(),
  35. });
  36. export type WorkspaceConfigOption = z.infer<typeof workspaceConfigOptionSchema>;
  37. // slash 命令目录(workspace 级;与 host 侧 ZCodeSlashCommand 对齐)。
  38. export const workspaceSlashCommandSchema = z.object({
  39. name: z.string(),
  40. description: z.string(),
  41. inputHint: z.string().optional(),
  42. source: z.enum(["builtin", "custom"]).optional(),
  43. });
  44. export type WorkspaceSlashCommand = z.infer<typeof workspaceSlashCommandSchema>;
  45. // topic 载荷本体:整体替换语义(conflated 最新态,绝不深合并——同一纪律)。
  46. export const workspaceConfigStateSchema = z.object({
  47. configOptions: z.array(workspaceConfigOptionSchema),
  48. slashCommands: z.array(workspaceSlashCommandSchema),
  49. });
  50. export type WorkspaceConfigState = z.infer<typeof workspaceConfigStateSchema>;
  51. export const workspaceConfigSnapshotSchema = z.object({
  52. protocolVersion: z.literal(1),
  53. workspaceId: z.string(),
  54. // host 级配置日志代际(与 sessions-index 的 logEpoch 同构、彼此独立)。
  55. logEpoch: z.string(),
  56. config: workspaceConfigStateSchema,
  57. });
  58. export type WorkspaceConfigSnapshot = z.infer<typeof workspaceConfigSnapshotSchema>;
  59. // delta 集合刻意只有一个 op:配置目录是小体量整体替换态,不做字段级增量。
  60. export const workspaceConfigDeltaSchema = z.discriminatedUnion("op", [
  61. z.object({ op: z.literal("config.updated"), config: workspaceConfigStateSchema }),
  62. ]);
  63. export type WorkspaceConfigDelta = z.infer<typeof workspaceConfigDeltaSchema>;
  64. /** workspace-config topic key 构造(与 parseWorkspaceConfigTopic 对偶)。 */
  65. export function workspaceConfigTopic(workspaceId: string): string {
  66. return `workspace-config/${workspaceId}`;
  67. }
  68. /** workspace-config topic key 解析("workspace-config/<workspaceId>")。 */
  69. export function parseWorkspaceConfigTopic(topic: string): string | null {
  70. if (!topic.startsWith("workspace-config/")) return null;
  71. const workspaceId = topic.slice("workspace-config/".length);
  72. return workspaceId.length > 0 ? workspaceId : null;
  73. }