返回 DeepSeek-Reasonix
projectTreeRuntime.ts
根目录 / desktop / frontend / src / lib / projectTreeRuntime.ts
1 import { desktopHost } from "./desktopHost";
2 import { asArray } from "./array";
3 import { runtimeStateStore } from "./runtimeStateStore";
4 import { projectSessionIdentity, projectSessionExcluded, projectSessionResolver } from "./projectSessionIdentity";
5 import type { ProjectNode, ProjectRuntimeTopic, ProjectTreeRuntimeSnapshot } from "./types";
6
7 const noExcludedTopicIds: ReadonlySet<string> = new Set();
8
9 function withoutRuntimeState(node: ProjectNode): ProjectNode {
10 return { ...node, open: undefined, running: undefined, status: undefined,
11 children: asArray(node.children).map(withoutRuntimeState) };
12 }
13
14 function runtimeChildren(runtime: ProjectNode, catalog?: ProjectNode): ProjectNode[] {
15 const overlays = new Map(asArray(runtime.children).map(child => [projectSessionIdentity(child), child]));
16 const children = asArray(catalog?.children).map(child => {
17 const overlay = overlays.get(projectSessionIdentity(child));
18 if (overlay) overlays.delete(projectSessionIdentity(child));
19 return overlay ? { ...withoutRuntimeState(child), open: overlay.open, running: overlay.running,
20 status: overlay.status, children: runtimeChildren(overlay, child) } : withoutRuntimeState(child);
21 });
22 return [...children, ...overlays.values()];
23 }
24
25 function runtimeTopicKey(scope: string, workspaceRoot: string, node: ProjectNode): string {
26 return `${scope}\u0000${workspaceRoot}\u0000${projectSessionIdentity(node)}`;
27 }
28
29 function sameOwnFields(current: ProjectNode, next: ProjectNode): boolean {
30 const keys = new Set([...Object.keys(current), ...Object.keys(next)]);
31 keys.delete("children");
32 for (const key of keys) {
33 if (current[key as keyof ProjectNode] !== next[key as keyof ProjectNode]) return false;
34 }
35 return true;
36 }
37
38 function reconcileNode(current: ProjectNode | undefined, next: ProjectNode): ProjectNode {
39 const currentChildren = asArray(current?.children);
40 const currentByKey = new Map(currentChildren.map((child) => [child.key, child]));
41 const nextChildren = asArray(next.children).map((child) => reconcileNode(currentByKey.get(child.key), child));
42 if (current
43 && sameOwnFields(current, next)
44 && currentChildren.length === nextChildren.length
45 && currentChildren.every((child, index) => child === nextChildren[index])) {
46 return current;
47 }
48 return { ...next, children: nextChildren };
49 }
50
51 function rememberResidentTopics(
52 tree: ProjectNode[],
53 residentTopics: Map<string, ProjectNode>,
54 excludedTopicIds: ReadonlySet<string>,
55 ): Set<string> {
56 const catalogKeys = new Set<string>();
57 for (const project of tree) {
58 if (project.kind !== "project" && project.kind !== "global_folder") continue;
59 const scope = project.kind === "project" ? "project" : "global";
60 const root = scope === "project" ? project.root ?? "" : "";
61 for (const topic of asArray(project.children)) {
62 if (topic.runtimeOnly || projectSessionExcluded(topic, excludedTopicIds)) continue;
63 const key = runtimeTopicKey(scope, root, topic);
64 catalogKeys.add(key);
65 residentTopics.set(key, { ...withoutRuntimeState(topic), children: asArray(topic.children) });
66 }
67 }
68 return catalogKeys;
69 }
70
71 function activeRuntimeTopicKeys(
72 topics: ProjectRuntimeTopic[],
73 excludedTopicIds: ReadonlySet<string>,
74 ): Set<string> {
75 return new Set(topics.flatMap((topic) => {
76 if (projectSessionExcluded(topic.node, excludedTopicIds)) return [];
77 return [runtimeTopicKey(topic.scope, topic.scope === "project" ? topic.workspaceRoot ?? "" : "", topic.node)];
78 }));
79 }
80
81 function pruneResidentTopics(
82 residentTopics: Map<string, ProjectNode>,
83 catalogKeys: ReadonlySet<string>,
84 runtimeKeys: ReadonlySet<string>,
85 ) {
86 for (const key of residentTopics.keys()) {
87 if (!catalogKeys.has(key) && !runtimeKeys.has(key)) residentTopics.delete(key);
88 }
89 }
90
91 // This structural-sharing overlay is loaded after the project tree mounts so
92 // runtime reconciliation does not enlarge the first-paint bundle. Subscription
93 // still precedes the initial snapshot read, so loading the module cannot lose
94 // an ownership transition.
95 export function projectTreeApplyRuntimeTopics(
96 tree: ProjectNode[],
97 topics: ProjectRuntimeTopic[],
98 excludedTopicIds: ReadonlySet<string> = noExcludedTopicIds,
99 residentTopics?: ReadonlyMap<string, ProjectNode>,
100 ): ProjectNode[] {
101 const nextTree = tree.map((project) => {
102 if (project.kind !== "project" && project.kind !== "global_folder") return project;
103 const scope = project.kind === "project" ? "project" : "global";
104 const root = scope === "project" ? project.root ?? "" : "";
105 const available = topics
106 .filter((topic) => !projectSessionExcluded(topic.node, excludedTopicIds)
107 && topic.scope === scope
108 && (scope !== "project" || topic.workspaceRoot === root));
109 const identityOf = projectSessionResolver([...asArray(project.children), ...available.map(topic => topic.node)]);
110 const runtimeByTopic = new Map(available.map(topic => [identityOf(topic.node), topic]));
111 const currentChildren = asArray(project.children);
112 const base: ProjectNode[] = [];
113 for (const node of currentChildren) {
114 if (node.runtimeOnly || projectSessionExcluded(node, excludedTopicIds)) continue;
115 const identity = identityOf(node);
116 if (base.some(row => identityOf(row) === identity)) continue;
117 const candidate = runtimeByTopic.get(identity);
118 if (candidate) runtimeByTopic.delete(identity);
119 const runtime = candidate && (candidate.node.lifecycleGeneration ?? 0) >= (node.lifecycleGeneration ?? 0) ? candidate : undefined;
120 const next = runtime ? {
121 ...withoutRuntimeState(node),
122 session: node.session ?? runtime.node.session,
123 identityAliases: node.identityAliases ?? runtime.node.identityAliases,
124 open: runtime.node.open,
125 running: runtime.node.running,
126 status: runtime.node.status,
127 children: runtimeChildren(runtime.node, node),
128 } : withoutRuntimeState(node);
129 base.push(reconcileNode(node, next));
130 }
131 const runtimeOnly: ProjectNode[] = [];
132 for (const topic of runtimeByTopic.values()) {
133 const topicId = topic.node.topicId!;
134 const identity = projectSessionIdentity(topic.node);
135 const current = currentChildren.find((node) => node.runtimeOnly && projectSessionIdentity(node) === identity);
136 const resident = residentTopics?.get(runtimeTopicKey(scope, root, topic.node));
137 const stable = withoutRuntimeState(resident ?? current ?? topic.node);
138 runtimeOnly.push(reconcileNode(current, {
139 ...stable,
140 key: topic.node.key,
141 kind: topic.node.kind,
142 label: resident?.label ?? topic.node.label,
143 root: topic.node.root,
144 topicId,
145 sessionPath: stable.sessionPath || topic.node.sessionPath,
146 open: topic.node.open,
147 running: topic.node.running,
148 status: topic.node.status,
149 runtimeOnly: true,
150 children: runtimeChildren(topic.node, resident ?? current),
151 }));
152 }
153 return reconcileNode(project, { ...project, children: [...runtimeOnly, ...base] });
154 });
155 return nextTree.every((project, index) => project === tree[index]) ? tree : nextTree;
156 }
157
158 export function createProjectTreeRuntimeProjection() {
159 const residentTopics = new Map<string, ProjectNode>();
160 return {
161 apply(
162 tree: ProjectNode[],
163 topics: ProjectRuntimeTopic[],
164 excludedTopicIds: ReadonlySet<string> = noExcludedTopicIds,
165 ): ProjectNode[] {
166 const catalogKeys = rememberResidentTopics(tree, residentTopics, excludedTopicIds);
167 pruneResidentTopics(residentTopics, catalogKeys, activeRuntimeTopicKeys(topics, excludedTopicIds));
168 return projectTreeApplyRuntimeTopics(tree, topics, excludedTopicIds, residentTopics);
169 },
170 };
171 }
172
173 export function normalizeProjectTreeRuntimeSnapshot(payload: unknown): ProjectTreeRuntimeSnapshot {
174 const value = (payload ?? {}) as Partial<ProjectTreeRuntimeSnapshot>;
175 return { revision: value.revision ?? 0, topics: asArray(value.topics) };
176 }
177
178 export function onProjectTreeRuntimeChanged(cb: (event: ProjectTreeRuntimeSnapshot) => void): () => void {
179 const host = desktopHost();
180 if (host.kind === "none") return () => {};
181 return host.events.on("project-tree:runtime-changed", (payload?: unknown) => cb(normalizeProjectTreeRuntimeSnapshot(payload)));
182 }
183
184 export function bindProjectTreeRuntime(
185 setTree: (update: (tree: ProjectNode[]) => ProjectNode[]) => void,
186 getSnapshot: () => Promise<ProjectTreeRuntimeSnapshot> | undefined,
187 excludedTopicIds: () => ReadonlySet<string>,
188 ) {
189 let active = true;
190 let snapshot: ProjectTreeRuntimeSnapshot | null = null;
191 const projection = createProjectTreeRuntimeProjection();
192 const apply = (tree: ProjectNode[]) => snapshot ? projection.apply(tree, snapshot.topics, excludedTopicIds()) : tree;
193 const accept = (next: ProjectTreeRuntimeSnapshot) => {
194 if (!active || (snapshot && next.revision < snapshot.revision)) return;
195 snapshot = next;
196 setTree(apply);
197 };
198 const unified = () => {
199 const current = runtimeStateStore.getSnapshot();
200 if (current) {
201 const topics = runtimeStateStore.getFailed() ? current.topics.map(topic => ({ ...topic, node: { ...topic.node, running: false, status: "unknown" as const } })) : current.topics;
202 snapshot = null;
203 accept({ revision: current.revision, topics });
204 }
205 };
206 const stopUnified = runtimeStateStore.subscribe(unified);
207 const stop = onProjectTreeRuntimeChanged(next => { if (!runtimeStateStore.getSnapshot()) accept(next); });
208 unified();
209 if (!runtimeStateStore.getSnapshot()) void getSnapshot()?.then(next => { if (!runtimeStateStore.getSnapshot()) accept(next); }).catch(() => {});
210 return {
211 apply,
212 dispose() {
213 active = false;
214 stop();
215 stopUnified();
216 },
217 };
218 }
219
219 lines TYPESCRIPT