taskDispatchHistoryStore.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // 任务分发模块:分析记录注册表(模块会话空间的 UI 侧过滤索引)。
  2. // 设计合同:docs/specs/workspace-task-dispatch.md §8「模块会话空间」M3 实现口径:
  3. // 会话本体仍是唯一 v4 存储;本表只记录「哪次分析用了哪个 sessionId」+ 解析结果,
  4. // 会话活性/标题/终态从 sessions-index 读取。localStorage 持久化(renderer 本地)。
  5. const STORAGE_KEY = "zcode.task-dispatch.history.v1";
  6. const MAX_RECORDS = 100;
  7. export interface TaskDispatchAnalysisRecord {
  8. /** 分析记录 id(创建时生成,非 sessionId)。 */
  9. id: string;
  10. sessionId: string;
  11. workspaceKey: string;
  12. workspaceLabel: string;
  13. createdAt: number;
  14. status: "analyzing" | "done" | "failed";
  15. /** done:解析出的计划;failed:失败摘要(≤200 字符)。 */
  16. plan?: { tasks: { title: string; prompt: string; dependsOn?: string[]; parallelGroup?: number }[] };
  17. failure?: string;
  18. }
  19. type Listener = () => void;
  20. function isBrowserStorageAvailable(): boolean {
  21. try {
  22. return typeof window !== "undefined" && window.localStorage !== null;
  23. } catch {
  24. return false;
  25. }
  26. }
  27. function loadRecords(): TaskDispatchAnalysisRecord[] {
  28. if (!isBrowserStorageAvailable()) return [];
  29. try {
  30. const raw = window.localStorage.getItem(STORAGE_KEY);
  31. if (!raw) return [];
  32. const parsed: unknown = JSON.parse(raw);
  33. if (!Array.isArray(parsed)) return [];
  34. // 逐条宽松校验:损坏条目跳过而不是整表报废。
  35. return parsed.filter(
  36. (item): item is TaskDispatchAnalysisRecord =>
  37. typeof item === "object" &&
  38. item !== null &&
  39. typeof (item as TaskDispatchAnalysisRecord).id === "string" &&
  40. typeof (item as TaskDispatchAnalysisRecord).sessionId === "string",
  41. );
  42. } catch {
  43. return [];
  44. }
  45. }
  46. class TaskDispatchHistoryStore {
  47. private records: TaskDispatchAnalysisRecord[] = loadRecords();
  48. private listeners = new Set<Listener>();
  49. subscribe = (listener: Listener): (() => void) => {
  50. this.listeners.add(listener);
  51. return () => this.listeners.delete(listener);
  52. };
  53. getSnapshot = (): TaskDispatchAnalysisRecord[] => this.records;
  54. private emit(): void {
  55. for (const listener of this.listeners) listener();
  56. }
  57. private persist(): void {
  58. if (!isBrowserStorageAvailable()) return;
  59. try {
  60. window.localStorage.setItem(STORAGE_KEY, JSON.stringify(this.records));
  61. } catch {
  62. // 配额满/隐私模式:内存态仍可用,不阻断分析流程。
  63. }
  64. }
  65. upsert(record: TaskDispatchAnalysisRecord): void {
  66. const index = this.records.findIndex((item) => item.id === record.id);
  67. if (index >= 0) {
  68. this.records[index] = record;
  69. } else {
  70. // 新记录插头部;超限裁尾(最旧)。
  71. this.records.unshift(record);
  72. if (this.records.length > MAX_RECORDS) {
  73. this.records = this.records.slice(0, MAX_RECORDS);
  74. }
  75. }
  76. this.persist();
  77. this.emit();
  78. }
  79. /** 按注册表 id 集合过滤(模块历史视图:sessionId 在 sessions-index 中已消失的记录仍保留展示)。 */
  80. getByWorkspace(workspaceKey: string): TaskDispatchAnalysisRecord[] {
  81. return this.records.filter((record) => record.workspaceKey === workspaceKey);
  82. }
  83. findById(id: string): TaskDispatchAnalysisRecord | null {
  84. return this.records.find((record) => record.id === id) ?? null;
  85. }
  86. findBySessionId(sessionId: string): TaskDispatchAnalysisRecord | null {
  87. return this.records.find((record) => record.sessionId === sessionId) ?? null;
  88. }
  89. }
  90. export const taskDispatchHistoryStore = new TaskDispatchHistoryStore();