pi-wechat-session-hub 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.
- package/LICENSE +661 -0
- package/README.md +365 -0
- package/docs/decisions/0001-ilink-context-token-queue.md +41 -0
- package/docs/handoff-next-session.md +134 -0
- package/docs/implementation-spec.md +147 -0
- package/extensions/wechat-session-hub.ts +589 -0
- package/package.json +67 -0
- package/scripts/pi-rpc-smoke.mjs +150 -0
- package/scripts/probe-wt-capability.mjs +27 -0
- package/scripts/start-ui-worker.ps1 +37 -0
- package/src/commands/ilink-qr-login-component.ts +148 -0
- package/src/commands/project-command-parser.ts +108 -0
- package/src/commands/tokenizer.ts +71 -0
- package/src/commands/wechat-manager-command-parser.ts +143 -0
- package/src/config/defaults.ts +30 -0
- package/src/config/paths.ts +56 -0
- package/src/config/store.ts +311 -0
- package/src/config/types.ts +33 -0
- package/src/config/validation.ts +221 -0
- package/src/gateway/channel-types.ts +47 -0
- package/src/gateway/command-parser.ts +291 -0
- package/src/gateway/command-router.ts +1449 -0
- package/src/gateway/credential-store.ts +611 -0
- package/src/gateway/durable-message-queue.ts +1043 -0
- package/src/gateway/gateway-control-ipc.ts +687 -0
- package/src/gateway/gateway-runtime.ts +726 -0
- package/src/gateway/ui-worker-extension-bridge.ts +1070 -0
- package/src/gateway/ui-worker-ipc.ts +1483 -0
- package/src/gateway/ui-worker-types.ts +50 -0
- package/src/gateway/wechat-ilink/api-client.ts +390 -0
- package/src/gateway/wechat-ilink/channel-adapter.ts +320 -0
- package/src/gateway/wechat-ilink/cursor-store.ts +134 -0
- package/src/gateway/wechat-ilink/qr-login.ts +612 -0
- package/src/gateway/wechat-ilink/typing-indicator.ts +133 -0
- package/src/gateway/windows-terminal-launcher.ts +247 -0
- package/src/projects/project-service.ts +483 -0
- package/src/registry/gateway-lease.ts +348 -0
- package/src/registry/manager-registry.ts +466 -0
- package/src/registry/types.ts +61 -0
- package/src/runtime/background-pi-worker.ts +309 -0
- package/src/runtime/background-worker-manager.ts +594 -0
- package/src/runtime/gateway-daemon.ts +281 -0
- package/src/runtime/gateway-lifecycle-operation.ts +308 -0
- package/src/runtime/gateway-service-controller.ts +339 -0
- package/src/runtime/gateway-worker-manager.ts +181 -0
- package/src/runtime/pi-cli.ts +56 -0
- package/src/runtime/pi-rpc-client.ts +427 -0
- package/src/runtime/ui-worker-manager.ts +1186 -0
- package/src/runtime/windows-logon-task-manager.ts +212 -0
- package/src/system/process-liveness.ts +88 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, open, readFile, rename, unlink } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { GatewayLease } from "./gateway-lease.ts";
|
|
5
|
+
import type {
|
|
6
|
+
GatewayStatusRecord,
|
|
7
|
+
ManagerEventRecord,
|
|
8
|
+
RegistrySnapshot,
|
|
9
|
+
RouteRecord,
|
|
10
|
+
SessionIndexRecord,
|
|
11
|
+
WorkerInstanceRecord,
|
|
12
|
+
} from "./types.ts";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 表示 manager registry 文件损坏或版本不兼容。
|
|
16
|
+
*/
|
|
17
|
+
export class RegistryCorruptionError extends Error {
|
|
18
|
+
/**
|
|
19
|
+
* 创建 registry 读取错误。
|
|
20
|
+
*
|
|
21
|
+
* @param filePath 出错的 registry 文件。
|
|
22
|
+
* @param message 不包含文件内容的中文说明。
|
|
23
|
+
*/
|
|
24
|
+
constructor(filePath: string, message: string) {
|
|
25
|
+
super(`${message} (${filePath})`);
|
|
26
|
+
this.name = "RegistryCorruptionError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 管理单写入 Gateway 的 JSON 快照与 append-only JSONL 事件日志。
|
|
32
|
+
*/
|
|
33
|
+
export class ManagerRegistry {
|
|
34
|
+
readonly directory: string;
|
|
35
|
+
private writeQueue: Promise<void> = Promise.resolve();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 创建 manager registry。
|
|
39
|
+
*
|
|
40
|
+
* @param directory manager 数据目录。
|
|
41
|
+
* @param lease 当前 Gateway owner 租约。
|
|
42
|
+
*/
|
|
43
|
+
constructor(
|
|
44
|
+
directory: string,
|
|
45
|
+
private readonly lease: GatewayLease,
|
|
46
|
+
) {
|
|
47
|
+
this.directory = resolve(directory);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 读取 gateway、instances、routes 和 session-index 四份快照。
|
|
52
|
+
*
|
|
53
|
+
* @returns registry 当前可读状态。
|
|
54
|
+
*/
|
|
55
|
+
async readSnapshot(): Promise<RegistrySnapshot> {
|
|
56
|
+
return this.withWriteQueue(async () => {
|
|
57
|
+
const [gateway, instances, routes, storedSessions] = await Promise.all([
|
|
58
|
+
this.readJson<GatewayStatusRecord | null>("gateway.json", null),
|
|
59
|
+
this.readJson<Record<string, WorkerInstanceRecord>>("instances.json", {}),
|
|
60
|
+
this.readJson<Record<string, RouteRecord>>("routes.json", {}),
|
|
61
|
+
this.readJson<Record<string, SessionIndexRecord>>("session-index.json", {}),
|
|
62
|
+
]);
|
|
63
|
+
const sessions = await this.ensurePublicSessionIndexes(storedSessions);
|
|
64
|
+
return { gateway, instances, routes, sessions };
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 保存 Gateway 状态快照。
|
|
70
|
+
*
|
|
71
|
+
* @param status Gateway 当前状态。
|
|
72
|
+
* @returns 无返回值。
|
|
73
|
+
*/
|
|
74
|
+
async setGateway(status: GatewayStatusRecord | null): Promise<void> {
|
|
75
|
+
if (status !== null) {
|
|
76
|
+
await this.writeJson("gateway.json", status);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
await this.withWriteQueue(async () => {
|
|
80
|
+
await this.lease.assertOwned();
|
|
81
|
+
try {
|
|
82
|
+
await unlink(join(this.directory, "gateway.json"));
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (!isNodeError(error) || error.code !== "ENOENT") {
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 新增或更新在线 worker 实例。
|
|
93
|
+
*
|
|
94
|
+
* @param instance worker 状态记录。
|
|
95
|
+
* @returns 无返回值。
|
|
96
|
+
*/
|
|
97
|
+
async upsertInstance(instance: WorkerInstanceRecord): Promise<void> {
|
|
98
|
+
await this.updateRecord<WorkerInstanceRecord>(
|
|
99
|
+
"instances.json",
|
|
100
|
+
instance.instanceId,
|
|
101
|
+
instance,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 移除已停止 worker 实例记录。
|
|
107
|
+
*
|
|
108
|
+
* @param instanceId worker 实例 ID。
|
|
109
|
+
* @returns 无返回值。
|
|
110
|
+
*/
|
|
111
|
+
async removeInstance(instanceId: string): Promise<void> {
|
|
112
|
+
await this.deleteRecord("instances.json", instanceId);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 新增或更新 Pi session 索引记录。
|
|
117
|
+
*
|
|
118
|
+
* @param session session 元数据。
|
|
119
|
+
* @returns 无返回值。
|
|
120
|
+
*/
|
|
121
|
+
async upsertSession(session: SessionIndexRecord): Promise<SessionIndexRecord> {
|
|
122
|
+
return this.withWriteQueue(async () => {
|
|
123
|
+
await this.lease.assertOwned();
|
|
124
|
+
const records = await this.readJson<Record<string, SessionIndexRecord>>("session-index.json", {});
|
|
125
|
+
const indexedRecords = await this.ensurePublicSessionIndexes(records);
|
|
126
|
+
const existing = indexedRecords[session.sessionId];
|
|
127
|
+
const nextIndex = existing?.publicIndex ?? nextPublicIndex(indexedRecords);
|
|
128
|
+
const savedSession = { ...session, publicIndex: nextIndex };
|
|
129
|
+
await this.writeJsonUnlocked("session-index.json", {
|
|
130
|
+
...indexedRecords,
|
|
131
|
+
[session.sessionId]: savedSession,
|
|
132
|
+
});
|
|
133
|
+
return savedSession;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 新增、更新或移除微信 route 绑定。
|
|
139
|
+
*
|
|
140
|
+
* @param routeId 路由稳定 ID。
|
|
141
|
+
* @param route 新绑定, 传 null 表示移除。
|
|
142
|
+
* @returns 无返回值。
|
|
143
|
+
*/
|
|
144
|
+
async setRoute(routeId: string, route: RouteRecord | null): Promise<void> {
|
|
145
|
+
if (route === null) {
|
|
146
|
+
await this.deleteRecord("routes.json", routeId);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
await this.updateRecord<RouteRecord>("routes.json", routeId, route);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 追加不包含凭证或消息正文的 registry 审计事件。
|
|
154
|
+
*
|
|
155
|
+
* @param type 事件类型。
|
|
156
|
+
* @param data 经过筛选的事件元数据。
|
|
157
|
+
* @returns 已追加的事件记录。
|
|
158
|
+
*/
|
|
159
|
+
async appendEvent(
|
|
160
|
+
type: string,
|
|
161
|
+
data: Record<string, unknown> = {},
|
|
162
|
+
): Promise<ManagerEventRecord> {
|
|
163
|
+
assertEventDataIsSafe(data);
|
|
164
|
+
return this.withWriteQueue(async () => {
|
|
165
|
+
await this.lease.assertOwned();
|
|
166
|
+
await mkdir(this.directory, { recursive: true });
|
|
167
|
+
const event: ManagerEventRecord = {
|
|
168
|
+
id: randomUUID(),
|
|
169
|
+
timestamp: new Date().toISOString(),
|
|
170
|
+
type,
|
|
171
|
+
data,
|
|
172
|
+
};
|
|
173
|
+
const filePath = join(this.directory, "events.jsonl");
|
|
174
|
+
const handle = await open(filePath, "a", 0o600);
|
|
175
|
+
try {
|
|
176
|
+
await handle.writeFile(`${JSON.stringify(event)}\n`, "utf8");
|
|
177
|
+
await handle.sync();
|
|
178
|
+
} finally {
|
|
179
|
+
await handle.close();
|
|
180
|
+
}
|
|
181
|
+
return event;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 为历史 registry 分配稳定的用户可见会话索引。
|
|
187
|
+
*
|
|
188
|
+
* @param records 当前 session registry 记录。
|
|
189
|
+
* @returns 包含稳定索引的记录集合。
|
|
190
|
+
* @throws {RegistryCorruptionError} 已有索引重复或租约失效时抛出。
|
|
191
|
+
*/
|
|
192
|
+
private async ensurePublicSessionIndexes(
|
|
193
|
+
records: Record<string, SessionIndexRecord>,
|
|
194
|
+
): Promise<Record<string, SessionIndexRecord>> {
|
|
195
|
+
const usedIndexes = new Set<number>();
|
|
196
|
+
for (const [sessionId, value] of Object.entries(records)) {
|
|
197
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
198
|
+
throw new RegistryCorruptionError(
|
|
199
|
+
join(this.directory, "session-index.json"),
|
|
200
|
+
`session ${sessionId} 记录格式无效, 已拒绝分配公开索引`,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
const session = value as SessionIndexRecord;
|
|
204
|
+
if (!isValidPublicIndex(session.publicIndex)) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (usedIndexes.has(session.publicIndex)) {
|
|
208
|
+
throw new RegistryCorruptionError(
|
|
209
|
+
join(this.directory, "session-index.json"),
|
|
210
|
+
"session publicIndex 重复, 已拒绝继续写入",
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
usedIndexes.add(session.publicIndex);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const missing = Object.entries(records)
|
|
217
|
+
.filter(([, session]) => !isValidPublicIndex(session.publicIndex))
|
|
218
|
+
.sort(([leftId, left], [rightId, right]) => {
|
|
219
|
+
const leftActivity = typeof left.lastActivityAt === "string" ? left.lastActivityAt : "";
|
|
220
|
+
const rightActivity = typeof right.lastActivityAt === "string" ? right.lastActivityAt : "";
|
|
221
|
+
return leftActivity.localeCompare(rightActivity) || leftId.localeCompare(rightId);
|
|
222
|
+
});
|
|
223
|
+
if (missing.length === 0) {
|
|
224
|
+
return records;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
await this.lease.assertOwned();
|
|
228
|
+
let nextIndex = nextPublicIndex(records);
|
|
229
|
+
const indexedRecords = { ...records };
|
|
230
|
+
for (const [sessionId, session] of missing) {
|
|
231
|
+
while (usedIndexes.has(nextIndex)) {
|
|
232
|
+
nextIndex += 1;
|
|
233
|
+
}
|
|
234
|
+
indexedRecords[sessionId] = { ...session, publicIndex: nextIndex };
|
|
235
|
+
usedIndexes.add(nextIndex);
|
|
236
|
+
nextIndex += 1;
|
|
237
|
+
}
|
|
238
|
+
await this.writeJsonUnlocked("session-index.json", indexedRecords);
|
|
239
|
+
return indexedRecords;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 按 key 更新 JSON 快照并原子替换文件。
|
|
244
|
+
*
|
|
245
|
+
* @param fileName registry 文件名。
|
|
246
|
+
* @param key 快照中的记录 ID。
|
|
247
|
+
* @param value 要保存的记录。
|
|
248
|
+
* @returns 无返回值。
|
|
249
|
+
*/
|
|
250
|
+
private async updateRecord<T>(
|
|
251
|
+
fileName: string,
|
|
252
|
+
key: string,
|
|
253
|
+
value: T,
|
|
254
|
+
): Promise<void> {
|
|
255
|
+
await this.withWriteQueue(async () => {
|
|
256
|
+
await this.lease.assertOwned();
|
|
257
|
+
const records = await this.readJson<Record<string, T>>(fileName, {});
|
|
258
|
+
const updated = {
|
|
259
|
+
...records,
|
|
260
|
+
[key]: value,
|
|
261
|
+
};
|
|
262
|
+
await this.writeJsonUnlocked(fileName, updated);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 从 JSON 快照删除指定记录。
|
|
268
|
+
*
|
|
269
|
+
* @param fileName registry 文件名。
|
|
270
|
+
* @param key 快照中的记录 ID。
|
|
271
|
+
* @returns 无返回值。
|
|
272
|
+
*/
|
|
273
|
+
private async deleteRecord(fileName: string, key: string): Promise<void> {
|
|
274
|
+
await this.withWriteQueue(async () => {
|
|
275
|
+
await this.lease.assertOwned();
|
|
276
|
+
const records = await this.readJson<Record<string, unknown>>(fileName, {});
|
|
277
|
+
const updated = { ...records };
|
|
278
|
+
delete updated[key];
|
|
279
|
+
await this.writeJsonUnlocked(fileName, updated);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 在 Gateway owner 租约下写入 JSON 快照。
|
|
285
|
+
*
|
|
286
|
+
* @param fileName registry 文件名。
|
|
287
|
+
* @param value 要序列化的状态。
|
|
288
|
+
* @returns 无返回值。
|
|
289
|
+
*/
|
|
290
|
+
private async writeJson(fileName: string, value: unknown): Promise<void> {
|
|
291
|
+
await this.withWriteQueue(async () => {
|
|
292
|
+
await this.lease.assertOwned();
|
|
293
|
+
await this.writeJsonUnlocked(fileName, value);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 读取 JSON 快照; 文件不存在时返回传入默认值。
|
|
299
|
+
*
|
|
300
|
+
* @param fileName registry 文件名。
|
|
301
|
+
* @param fallback 文件不存在时的默认值。
|
|
302
|
+
* @returns 解码后的 JSON 状态。
|
|
303
|
+
* @throws {RegistryCorruptionError} 文件 JSON 结构不可信时抛出。
|
|
304
|
+
*/
|
|
305
|
+
private async readJson<T>(fileName: string, fallback: T): Promise<T> {
|
|
306
|
+
const filePath = join(this.directory, fileName);
|
|
307
|
+
try {
|
|
308
|
+
const contents = await readFile(filePath, "utf8");
|
|
309
|
+
let parsed: unknown;
|
|
310
|
+
try {
|
|
311
|
+
parsed = JSON.parse(contents) as unknown;
|
|
312
|
+
} catch {
|
|
313
|
+
throw new RegistryCorruptionError(filePath, "registry JSON 格式无效, 已拒绝继续写入");
|
|
314
|
+
}
|
|
315
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
316
|
+
throw new RegistryCorruptionError(filePath, "registry JSON 根节点必须是对象");
|
|
317
|
+
}
|
|
318
|
+
return parsed as T;
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (isNodeError(error) && error.code === "ENOENT") {
|
|
321
|
+
return fallback;
|
|
322
|
+
}
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 原子写入同目录临时文件并替换快照。
|
|
329
|
+
*
|
|
330
|
+
* @param fileName registry 文件名。
|
|
331
|
+
* @param value 要序列化的状态。
|
|
332
|
+
* @returns 无返回值。
|
|
333
|
+
*/
|
|
334
|
+
private async writeJsonUnlocked(fileName: string, value: unknown): Promise<void> {
|
|
335
|
+
await mkdir(this.directory, { recursive: true });
|
|
336
|
+
const filePath = join(this.directory, fileName);
|
|
337
|
+
const temporaryPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`;
|
|
338
|
+
const handle = await open(temporaryPath, "wx", 0o600);
|
|
339
|
+
try {
|
|
340
|
+
await handle.writeFile(`${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
341
|
+
await handle.sync();
|
|
342
|
+
} catch (error) {
|
|
343
|
+
await handle.close();
|
|
344
|
+
try {
|
|
345
|
+
await unlink(temporaryPath);
|
|
346
|
+
} catch {
|
|
347
|
+
throw new Error("registry 快照写入失败, 且无法清理临时文件。");
|
|
348
|
+
}
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
await handle.close();
|
|
352
|
+
try {
|
|
353
|
+
await rename(temporaryPath, filePath);
|
|
354
|
+
} catch (error) {
|
|
355
|
+
try {
|
|
356
|
+
await unlink(temporaryPath);
|
|
357
|
+
} catch (cleanupError) {
|
|
358
|
+
if (!isNodeError(cleanupError) || cleanupError.code !== "ENOENT") {
|
|
359
|
+
throw cleanupError;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 串行执行本 registry 实例的写操作。
|
|
368
|
+
*
|
|
369
|
+
* @param operation 写操作。
|
|
370
|
+
* @returns 写操作结果。
|
|
371
|
+
*/
|
|
372
|
+
private async withWriteQueue<T>(operation: () => Promise<T>): Promise<T> {
|
|
373
|
+
const previous = this.writeQueue;
|
|
374
|
+
let release = (): void => undefined;
|
|
375
|
+
this.writeQueue = new Promise<void>((resolveQueue) => {
|
|
376
|
+
release = resolveQueue;
|
|
377
|
+
});
|
|
378
|
+
await previous;
|
|
379
|
+
try {
|
|
380
|
+
return await operation();
|
|
381
|
+
} finally {
|
|
382
|
+
release();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const SENSITIVE_EVENT_FIELDS = new Set([
|
|
388
|
+
"message",
|
|
389
|
+
"prompt",
|
|
390
|
+
"content",
|
|
391
|
+
"token",
|
|
392
|
+
"credential",
|
|
393
|
+
"credentialfile",
|
|
394
|
+
"apikey",
|
|
395
|
+
"password",
|
|
396
|
+
"secret",
|
|
397
|
+
"output",
|
|
398
|
+
"command",
|
|
399
|
+
]);
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 拒绝把消息正文、凭证或命令输出写入普通审计事件。
|
|
403
|
+
*
|
|
404
|
+
* @param value 待记录事件数据。
|
|
405
|
+
* @param ancestors 当前递归路径上的对象集合。
|
|
406
|
+
* @returns 无返回值。
|
|
407
|
+
* @throws {Error} 发现敏感字段或循环对象时抛出。
|
|
408
|
+
*/
|
|
409
|
+
function assertEventDataIsSafe(
|
|
410
|
+
value: unknown,
|
|
411
|
+
ancestors = new Set<object>(),
|
|
412
|
+
): void {
|
|
413
|
+
if (typeof value !== "object" || value === null) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (ancestors.has(value)) {
|
|
417
|
+
throw new Error("registry 审计事件不能包含循环引用。");
|
|
418
|
+
}
|
|
419
|
+
ancestors.add(value);
|
|
420
|
+
if (Array.isArray(value)) {
|
|
421
|
+
for (const item of value) {
|
|
422
|
+
assertEventDataIsSafe(item, ancestors);
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
for (const [key, nestedValue] of Object.entries(value)) {
|
|
426
|
+
if (SENSITIVE_EVENT_FIELDS.has(key.toLowerCase())) {
|
|
427
|
+
throw new Error("registry 审计事件不能包含消息正文、凭证、命令或输出字段。");
|
|
428
|
+
}
|
|
429
|
+
assertEventDataIsSafe(nestedValue, ancestors);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
ancestors.delete(value);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 判断会话索引是否为正安全整数。
|
|
437
|
+
*
|
|
438
|
+
* @param value 待校验索引。
|
|
439
|
+
* @returns 合法时返回 true。
|
|
440
|
+
*/
|
|
441
|
+
function isValidPublicIndex(value: unknown): value is number {
|
|
442
|
+
return Number.isSafeInteger(value) && typeof value === "number" && value > 0;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* 计算当前 registry 中尚未分配的最小递增索引。
|
|
447
|
+
*
|
|
448
|
+
* @param records 当前 session registry 记录。
|
|
449
|
+
* @returns 大于现有最大索引的下一个索引。
|
|
450
|
+
*/
|
|
451
|
+
function nextPublicIndex(records: Record<string, SessionIndexRecord>): number {
|
|
452
|
+
const maxIndex = Object.values(records).reduce((maximum, session) =>
|
|
453
|
+
isValidPublicIndex(session.publicIndex) ? Math.max(maximum, session.publicIndex) : maximum,
|
|
454
|
+
0);
|
|
455
|
+
return maxIndex + 1;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 判断值是否为 Node.js 文件系统错误。
|
|
460
|
+
*
|
|
461
|
+
* @param error 待检查值。
|
|
462
|
+
* @returns 值带有 Node.js 错误代码时返回 true。
|
|
463
|
+
*/
|
|
464
|
+
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
465
|
+
return error instanceof Error && "code" in error;
|
|
466
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type WorkerMode = "ui" | "background";
|
|
2
|
+
export type WorkerState = "starting" | "idle" | "running" | "stopping" | "stale";
|
|
3
|
+
export type SessionState = "active" | "saved" | "pending";
|
|
4
|
+
|
|
5
|
+
export interface GatewayStatusRecord {
|
|
6
|
+
ownerId: string;
|
|
7
|
+
pid: number;
|
|
8
|
+
processBirthId: string;
|
|
9
|
+
startedAt: string;
|
|
10
|
+
heartbeatAt: string;
|
|
11
|
+
state: "starting" | "online" | "auth_required" | "stopping";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface WorkerInstanceRecord {
|
|
15
|
+
instanceId: string;
|
|
16
|
+
pid: number;
|
|
17
|
+
processBirthId: string;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
sessionFile: string;
|
|
20
|
+
cwd: string;
|
|
21
|
+
projectAlias: string;
|
|
22
|
+
mode: WorkerMode;
|
|
23
|
+
state: WorkerState;
|
|
24
|
+
heartbeatAt: string;
|
|
25
|
+
launchId?: string;
|
|
26
|
+
ipcAuthHash?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SessionIndexRecord {
|
|
30
|
+
sessionId: string;
|
|
31
|
+
publicIndex?: number;
|
|
32
|
+
sessionFile: string;
|
|
33
|
+
cwd: string;
|
|
34
|
+
projectAlias: string;
|
|
35
|
+
name?: string;
|
|
36
|
+
mode: WorkerMode;
|
|
37
|
+
state: SessionState;
|
|
38
|
+
lastActivityAt: string;
|
|
39
|
+
lastEntryId?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RouteRecord {
|
|
43
|
+
sessionId: string;
|
|
44
|
+
workerInstanceId: string;
|
|
45
|
+
generation: number;
|
|
46
|
+
updatedAt: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RegistrySnapshot {
|
|
50
|
+
gateway: GatewayStatusRecord | null;
|
|
51
|
+
instances: Record<string, WorkerInstanceRecord>;
|
|
52
|
+
routes: Record<string, RouteRecord>;
|
|
53
|
+
sessions: Record<string, SessionIndexRecord>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ManagerEventRecord {
|
|
57
|
+
id: string;
|
|
58
|
+
timestamp: string;
|
|
59
|
+
type: string;
|
|
60
|
+
data: Record<string, unknown>;
|
|
61
|
+
}
|