// 任务分发模块:分析记录注册表(模块会话空间的 UI 侧过滤索引)。 // 设计合同:docs/specs/workspace-task-dispatch.md §8「模块会话空间」M3 实现口径: // 会话本体仍是唯一 v4 存储;本表只记录「哪次分析用了哪个 sessionId」+ 解析结果, // 会话活性/标题/终态从 sessions-index 读取。localStorage 持久化(renderer 本地)。 const STORAGE_KEY = "zcode.task-dispatch.history.v1"; const MAX_RECORDS = 100; export interface TaskDispatchAnalysisRecord { /** 分析记录 id(创建时生成,非 sessionId)。 */ id: string; sessionId: string; workspaceKey: string; workspaceLabel: string; createdAt: number; status: "analyzing" | "done" | "failed"; /** done:解析出的计划;failed:失败摘要(≤200 字符)。 */ plan?: { tasks: { title: string; prompt: string; dependsOn?: string[]; parallelGroup?: number }[] }; failure?: string; } type Listener = () => void; function isBrowserStorageAvailable(): boolean { try { return typeof window !== "undefined" && window.localStorage !== null; } catch { return false; } } function loadRecords(): TaskDispatchAnalysisRecord[] { if (!isBrowserStorageAvailable()) return []; try { const raw = window.localStorage.getItem(STORAGE_KEY); if (!raw) return []; const parsed: unknown = JSON.parse(raw); if (!Array.isArray(parsed)) return []; // 逐条宽松校验:损坏条目跳过而不是整表报废。 return parsed.filter( (item): item is TaskDispatchAnalysisRecord => typeof item === "object" && item !== null && typeof (item as TaskDispatchAnalysisRecord).id === "string" && typeof (item as TaskDispatchAnalysisRecord).sessionId === "string", ); } catch { return []; } } class TaskDispatchHistoryStore { private records: TaskDispatchAnalysisRecord[] = loadRecords(); private listeners = new Set(); subscribe = (listener: Listener): (() => void) => { this.listeners.add(listener); return () => this.listeners.delete(listener); }; getSnapshot = (): TaskDispatchAnalysisRecord[] => this.records; private emit(): void { for (const listener of this.listeners) listener(); } private persist(): void { if (!isBrowserStorageAvailable()) return; try { window.localStorage.setItem(STORAGE_KEY, JSON.stringify(this.records)); } catch { // 配额满/隐私模式:内存态仍可用,不阻断分析流程。 } } upsert(record: TaskDispatchAnalysisRecord): void { const index = this.records.findIndex((item) => item.id === record.id); if (index >= 0) { this.records[index] = record; } else { // 新记录插头部;超限裁尾(最旧)。 this.records.unshift(record); if (this.records.length > MAX_RECORDS) { this.records = this.records.slice(0, MAX_RECORDS); } } this.persist(); this.emit(); } /** 按注册表 id 集合过滤(模块历史视图:sessionId 在 sessions-index 中已消失的记录仍保留展示)。 */ getByWorkspace(workspaceKey: string): TaskDispatchAnalysisRecord[] { return this.records.filter((record) => record.workspaceKey === workspaceKey); } findById(id: string): TaskDispatchAnalysisRecord | null { return this.records.find((record) => record.id === id) ?? null; } findBySessionId(sessionId: string): TaskDispatchAnalysisRecord | null { return this.records.find((record) => record.sessionId === sessionId) ?? null; } } export const taskDispatchHistoryStore = new TaskDispatchHistoryStore();