@mozhi0012/pi-terminal-worker 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -0
  3. package/dist/controller.d.ts +457 -0
  4. package/dist/controller.d.ts.map +1 -0
  5. package/dist/controller.js +2164 -0
  6. package/dist/controller.js.map +1 -0
  7. package/dist/extension.d.ts +31 -0
  8. package/dist/extension.d.ts.map +1 -0
  9. package/dist/extension.js +89 -0
  10. package/dist/extension.js.map +1 -0
  11. package/dist/launcher.d.ts +131 -0
  12. package/dist/launcher.d.ts.map +1 -0
  13. package/dist/launcher.js +374 -0
  14. package/dist/launcher.js.map +1 -0
  15. package/dist/lifecycle.d.ts +103 -0
  16. package/dist/lifecycle.d.ts.map +1 -0
  17. package/dist/lifecycle.js +206 -0
  18. package/dist/lifecycle.js.map +1 -0
  19. package/dist/multi-worker.d.ts +324 -0
  20. package/dist/multi-worker.d.ts.map +1 -0
  21. package/dist/multi-worker.js +833 -0
  22. package/dist/multi-worker.js.map +1 -0
  23. package/dist/protocol.d.ts +249 -0
  24. package/dist/protocol.d.ts.map +1 -0
  25. package/dist/protocol.js +430 -0
  26. package/dist/protocol.js.map +1 -0
  27. package/dist/transport.d.ts +135 -0
  28. package/dist/transport.d.ts.map +1 -0
  29. package/dist/transport.js +459 -0
  30. package/dist/transport.js.map +1 -0
  31. package/dist/ui.d.ts +63 -0
  32. package/dist/ui.d.ts.map +1 -0
  33. package/dist/ui.js +100 -0
  34. package/dist/ui.js.map +1 -0
  35. package/dist/worker.d.ts +94 -0
  36. package/dist/worker.d.ts.map +1 -0
  37. package/dist/worker.js +842 -0
  38. package/dist/worker.js.map +1 -0
  39. package/package.json +72 -0
  40. package/runtime/bootstrap.mjs +286 -0
  41. package/src/controller.ts +2715 -0
  42. package/src/extension.ts +107 -0
  43. package/src/launcher.ts +510 -0
  44. package/src/lifecycle.ts +281 -0
  45. package/src/multi-worker.ts +1096 -0
  46. package/src/protocol.ts +739 -0
  47. package/src/transport.ts +561 -0
  48. package/src/ui.ts +131 -0
  49. package/src/worker.ts +996 -0
@@ -0,0 +1,281 @@
1
+ /**
2
+ * 生命周期与代际管理、单实例互斥与资源幂等清理
3
+ */
4
+
5
+ import { ProtocolErrorCode } from "./protocol.js";
6
+
7
+ export type WorkerLifecycleState =
8
+ | "none"
9
+ | "launching"
10
+ | "connected"
11
+ | "closing"
12
+ | "closed"
13
+ | "disconnected"
14
+ | "unresponsive"
15
+ | "detached"
16
+ | "launch_unknown";
17
+
18
+ export type WorkerTaskState =
19
+ | "created"
20
+ | "queued"
21
+ | "running"
22
+ | "waiting_reply"
23
+ | "blocked"
24
+ | "ready_for_review"
25
+ | "failed"
26
+ | "idle_unreported"
27
+ | "stopping"
28
+ | "stopped"
29
+ | "accepted"
30
+ | "abandoned";
31
+
32
+ export type WorkerActivityState = "idle" | "busy" | "waiting_ui" | "unknown";
33
+
34
+ export interface WorkerInstanceMetadata {
35
+ controllerId: string;
36
+ workerId: string;
37
+ taskId: string;
38
+ cwd: string;
39
+ title: string;
40
+ childPid?: number;
41
+ provider?: string;
42
+ modelId?: string;
43
+ thinkingLevel?: string;
44
+ tools?: string[];
45
+ revision: number;
46
+ runId: number;
47
+ lifecycleState: WorkerLifecycleState;
48
+ taskState: WorkerTaskState;
49
+ activityState: WorkerActivityState;
50
+ createdAt: number;
51
+ updatedAt: number;
52
+ }
53
+
54
+ /**
55
+ * 会话代际追踪器,防止跨 reload 或 session switch 的异步回调干扰新会话
56
+ */
57
+ export class SessionGenerationManager {
58
+ private currentGeneration = 0;
59
+
60
+ public get generation(): number {
61
+ return this.currentGeneration;
62
+ }
63
+
64
+ public nextGeneration(): number {
65
+ return ++this.currentGeneration;
66
+ }
67
+
68
+ public isValid(gen: number): boolean {
69
+ return gen === this.currentGeneration;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * 单实例互斥状态机
75
+ */
76
+ export class SingleWorkerManager {
77
+ private currentInstance: WorkerInstanceMetadata | null = null;
78
+ private mutexLocked = false;
79
+
80
+ public hasActiveInstance(): boolean {
81
+ if (!this.currentInstance) return false;
82
+ return (
83
+ this.currentInstance.lifecycleState !== "none" &&
84
+ this.currentInstance.lifecycleState !== "closed"
85
+ );
86
+ }
87
+
88
+ public getInstance(): WorkerInstanceMetadata | null {
89
+ return this.currentInstance ? { ...this.currentInstance } : null;
90
+ }
91
+
92
+ /**
93
+ * 原子请求启动名额
94
+ */
95
+ public acquireLaunchSlot(
96
+ controllerId: string,
97
+ workerId: string,
98
+ taskId: string,
99
+ cwd: string,
100
+ title: string,
101
+ ): WorkerInstanceMetadata {
102
+ if (this.mutexLocked) {
103
+ throw new Error(`当前正在处理其他状态转换,请稍候重试`);
104
+ }
105
+
106
+ this.mutexLocked = true;
107
+ try {
108
+ if (this.hasActiveInstance()) {
109
+ const cur = this.currentInstance!;
110
+ throw new Error(
111
+ `[${ProtocolErrorCode.ALREADY_EXISTS}] 当前已存在运行中或未确认退出的 Worker (ID: ${cur.workerId}, 状态: ${cur.lifecycleState})。单主控同一时间仅允许一个执行端。`,
112
+ );
113
+ }
114
+
115
+ const now = Date.now();
116
+ const instance: WorkerInstanceMetadata = {
117
+ controllerId,
118
+ workerId,
119
+ taskId,
120
+ cwd,
121
+ title,
122
+ revision: 1,
123
+ runId: 1,
124
+ lifecycleState: "launching",
125
+ taskState: "created",
126
+ activityState: "unknown",
127
+ createdAt: now,
128
+ updatedAt: now,
129
+ };
130
+
131
+ this.currentInstance = instance;
132
+ return { ...this.currentInstance };
133
+ } finally {
134
+ this.mutexLocked = false;
135
+ }
136
+ }
137
+
138
+ /**
139
+ * 更新当前实例生命周期状态
140
+ */
141
+ public updateLifecycleState(state: WorkerLifecycleState): void {
142
+ if (!this.currentInstance) return;
143
+ this.currentInstance.lifecycleState = state;
144
+ this.currentInstance.updatedAt = Date.now();
145
+ }
146
+
147
+ /**
148
+ * 更新当前任务状态
149
+ */
150
+ public updateTaskState(state: WorkerTaskState): void {
151
+ if (!this.currentInstance) return;
152
+ this.currentInstance.taskState = state;
153
+ this.currentInstance.updatedAt = Date.now();
154
+ }
155
+
156
+ /**
157
+ * 更新 Pi 活动状态
158
+ */
159
+ public updateActivityState(state: WorkerActivityState): void {
160
+ if (!this.currentInstance) return;
161
+ this.currentInstance.activityState = state;
162
+ this.currentInstance.updatedAt = Date.now();
163
+ }
164
+
165
+ /**
166
+ * 记录子进程 PID
167
+ */
168
+ public setChildPid(pid: number): void {
169
+ if (!this.currentInstance) return;
170
+ this.currentInstance.childPid = pid;
171
+ this.currentInstance.updatedAt = Date.now();
172
+ }
173
+
174
+ /**
175
+ * 更新模型信息 (worker_ready 初始上报或 model_changed 刷新)
176
+ */
177
+ public setModelInfo(
178
+ provider?: string,
179
+ modelId?: string,
180
+ thinkingLevel?: string,
181
+ tools?: string[],
182
+ ): void {
183
+ if (!this.currentInstance) return;
184
+ if (provider !== undefined) this.currentInstance.provider = provider;
185
+ if (modelId !== undefined) this.currentInstance.modelId = modelId;
186
+ if (thinkingLevel !== undefined) this.currentInstance.thinkingLevel = thinkingLevel;
187
+ if (tools) this.currentInstance.tools = [...tools];
188
+ this.currentInstance.updatedAt = Date.now();
189
+ }
190
+
191
+ /**
192
+ * 递增 Revision (返修)
193
+ */
194
+ public bumpRevision(): { revision: number; runId: number } {
195
+ if (!this.currentInstance) {
196
+ throw new Error("当前无存活的 Worker 实例");
197
+ }
198
+ this.currentInstance.revision += 1;
199
+ this.currentInstance.runId += 1;
200
+ this.currentInstance.taskState = "queued";
201
+ this.currentInstance.updatedAt = Date.now();
202
+ return {
203
+ revision: this.currentInstance.revision,
204
+ runId: this.currentInstance.runId,
205
+ };
206
+ }
207
+
208
+ /**
209
+ * 递增 RunId (同 Revision 下的重试或新一轮交互)
210
+ */
211
+ public bumpRunId(): number {
212
+ if (!this.currentInstance) {
213
+ throw new Error("当前无存活的 Worker 实例");
214
+ }
215
+ this.currentInstance.runId += 1;
216
+ this.currentInstance.updatedAt = Date.now();
217
+ return this.currentInstance.runId;
218
+ }
219
+
220
+ /**
221
+ * 释放实例名额 (仅当状态为 closed 或人工 forget 时允许)
222
+ */
223
+ public releaseSlot(force: boolean = false): boolean {
224
+ if (!this.currentInstance) return true;
225
+ if (
226
+ force ||
227
+ this.currentInstance.lifecycleState === "closed" ||
228
+ this.currentInstance.lifecycleState === "none"
229
+ ) {
230
+ this.currentInstance = null;
231
+ return true;
232
+ }
233
+ return false;
234
+ }
235
+ }
236
+
237
+ /**
238
+ * 资源清理注册器,保证 session_shutdown 或退出时幂等清理
239
+ */
240
+ export class CleanupRegistry {
241
+ private disposers = new Set<() => Promise<void> | void>();
242
+ private isDisposed = false;
243
+
244
+ /** 当前待执行的 disposer 数量 (诊断/测试用) */
245
+ public get size(): number {
246
+ return this.disposers.size;
247
+ }
248
+
249
+ public register(disposer: () => Promise<void> | void): () => void {
250
+ this.disposers.add(disposer);
251
+ return () => {
252
+ this.disposers.delete(disposer);
253
+ };
254
+ }
255
+
256
+ /**
257
+ * 执行并清空全部 disposer,同一轮只执行一次。
258
+ * 重复调用直接返回,既不重复执行 disposer 也不抛错。
259
+ */
260
+ public async disposeAll(): Promise<void> {
261
+ if (this.isDisposed) return;
262
+ this.isDisposed = true;
263
+
264
+ for (const disposer of Array.from(this.disposers)) {
265
+ try {
266
+ await disposer();
267
+ } catch {
268
+ // 忽略清理阶段的错误
269
+ }
270
+ }
271
+ this.disposers.clear();
272
+ }
273
+
274
+ /**
275
+ * 复位 dispose 标记,使同一个 registry 实例可被下一次会话复用。
276
+ * disposers 已在 disposeAll 中清空,因此 reset 后可重新 register 并再次 disposeAll。
277
+ */
278
+ public reset(): void {
279
+ this.isDisposed = false;
280
+ }
281
+ }