@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.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/controller.d.ts +457 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +2164 -0
- package/dist/controller.js.map +1 -0
- package/dist/extension.d.ts +31 -0
- package/dist/extension.d.ts.map +1 -0
- package/dist/extension.js +89 -0
- package/dist/extension.js.map +1 -0
- package/dist/launcher.d.ts +131 -0
- package/dist/launcher.d.ts.map +1 -0
- package/dist/launcher.js +374 -0
- package/dist/launcher.js.map +1 -0
- package/dist/lifecycle.d.ts +103 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +206 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/multi-worker.d.ts +324 -0
- package/dist/multi-worker.d.ts.map +1 -0
- package/dist/multi-worker.js +833 -0
- package/dist/multi-worker.js.map +1 -0
- package/dist/protocol.d.ts +249 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +430 -0
- package/dist/protocol.js.map +1 -0
- package/dist/transport.d.ts +135 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +459 -0
- package/dist/transport.js.map +1 -0
- package/dist/ui.d.ts +63 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +100 -0
- package/dist/ui.js.map +1 -0
- package/dist/worker.d.ts +94 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +842 -0
- package/dist/worker.js.map +1 -0
- package/package.json +72 -0
- package/runtime/bootstrap.mjs +286 -0
- package/src/controller.ts +2715 -0
- package/src/extension.ts +107 -0
- package/src/launcher.ts +510 -0
- package/src/lifecycle.ts +281 -0
- package/src/multi-worker.ts +1096 -0
- package/src/protocol.ts +739 -0
- package/src/transport.ts +561 -0
- package/src/ui.ts +131 -0
- package/src/worker.ts +996 -0
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通讯协议 v1 定义与常量约束
|
|
3
|
+
* 双向命名管道 JSONL 通讯
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export const PROTOCOL_VERSION = 1;
|
|
7
|
+
|
|
8
|
+
// 协议限额与超时常量 (设计方案第 5 节)
|
|
9
|
+
export const MAX_FRAME_SIZE = 1 * 1024 * 1024; // 1 MiB 单帧上限
|
|
10
|
+
export const MAX_TASK_TEXT_SIZE = 256 * 1024; // 256 KiB 任务文本上限
|
|
11
|
+
export const MAX_REPORT_SIZE = 64 * 1024; // 64 KiB 单条报告上限
|
|
12
|
+
export const MAX_WAIT_RESULT_SIZE = 16 * 1024; // 16 KiB worker_wait 单次返回上限
|
|
13
|
+
export const MAX_DEDUP_CACHE_SIZE = 1024; // 去重记录上限
|
|
14
|
+
export const MAX_FOLLOWUP_QUEUE_SIZE = 8; // 排队 followup 上限
|
|
15
|
+
export const MAX_INBOX_EVENTS = 128; // 收件箱事件条数上限
|
|
16
|
+
export const MAX_INBOX_BYTES = 8 * 1024 * 1024;// 收件箱总字节上限 (8 MiB)
|
|
17
|
+
export const MAX_FOLLOWUP_QUEUE_BYTES = 1 * 1024 * 1024; // followup 队列总字节上限 (1 MiB)
|
|
18
|
+
export const MAX_REPORT_CACHE_SIZE = 128; // 关键报告缓存条数上限
|
|
19
|
+
export const MAX_REPORT_CACHE_BYTES = 8 * 1024 * 1024; // 关键报告缓存总字节上限 (8 MiB)
|
|
20
|
+
|
|
21
|
+
// 2A: 协议严格校验字段上限
|
|
22
|
+
export const MAX_ID_LENGTH = 128; // controllerId/workerId/id/taskId/replyTo 长度上限
|
|
23
|
+
export const MAX_TYPE_LENGTH = 64; // 消息 type 长度上限
|
|
24
|
+
export const MAX_ERROR_MESSAGE_SIZE = 4 * 1024; // error.message / ack.error 上限
|
|
25
|
+
export const MAX_FOLLOWUP_MESSAGE_SIZE = 256 * 1024; // followup.message 上限
|
|
26
|
+
export const MAX_REPORT_SUMMARY_SIZE = 8 * 1024; // worker 回执 summary 上限
|
|
27
|
+
export const MAX_PATH_FIELD_SIZE = 4096; // 路径字段上限
|
|
28
|
+
export const MAX_SEND_BUFFER_BYTES = 4 * 1024 * 1024; // 发送队列字节上限 (4 MiB)
|
|
29
|
+
export const MAX_CONTROL_DEDUP_SIZE = 256; // 控制类消息独立去重额度
|
|
30
|
+
|
|
31
|
+
// 2B: 投递结果未知 (ACK 超时) 登记上限
|
|
32
|
+
export const MAX_UNKNOWN_DELIVERIES = 64; // unknownDeliveries 条数上限 (FIFO 淘汰)
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 协议消息类型白名单 (共 23 个)
|
|
36
|
+
*/
|
|
37
|
+
export const MESSAGE_TYPES = [
|
|
38
|
+
"hello",
|
|
39
|
+
"hello_ok",
|
|
40
|
+
"ping",
|
|
41
|
+
"pong",
|
|
42
|
+
"ack",
|
|
43
|
+
"error",
|
|
44
|
+
"launch",
|
|
45
|
+
"terminate",
|
|
46
|
+
"child_spawned",
|
|
47
|
+
"child_exit",
|
|
48
|
+
"launch_failed",
|
|
49
|
+
"worker_ready",
|
|
50
|
+
"model_changed",
|
|
51
|
+
"task",
|
|
52
|
+
"followup",
|
|
53
|
+
"abort",
|
|
54
|
+
"close",
|
|
55
|
+
"activity",
|
|
56
|
+
"report_candidate",
|
|
57
|
+
"report_committed",
|
|
58
|
+
"report_missing",
|
|
59
|
+
"stopped",
|
|
60
|
+
"local_input",
|
|
61
|
+
] as const;
|
|
62
|
+
|
|
63
|
+
export function isKnownMessageType(type: string): boolean {
|
|
64
|
+
return (MESSAGE_TYPES as readonly string[]).includes(type);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const AUTH_TIMEOUT_MS = 5000; // 单连接认证超时 5s
|
|
68
|
+
export const DEFAULT_ACK_TIMEOUT_MS = 5000; // 普通 ACK 超时 5s
|
|
69
|
+
export const LAUNCH_TIMEOUT_MS = 30000; // 启动握手超时 30s
|
|
70
|
+
export const HEARTBEAT_INTERVAL_MS = 5000; // 心跳发送间隔 5s
|
|
71
|
+
export const HEARTBEAT_TIMEOUT_MS = 20000; // 连续 20s 无消息进入 unresponsive
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 协议基础信封
|
|
75
|
+
*/
|
|
76
|
+
export interface Envelope<T = unknown> {
|
|
77
|
+
version: 1;
|
|
78
|
+
controllerId: string;
|
|
79
|
+
workerId: string;
|
|
80
|
+
taskId?: string;
|
|
81
|
+
revision?: number;
|
|
82
|
+
id: string; // 唯一消息 ID
|
|
83
|
+
replyTo?: string; // ACK/响应对应的请求 ID
|
|
84
|
+
seq: number; // 当前连接内递增序号
|
|
85
|
+
type: string; // 消息类型
|
|
86
|
+
payload: T;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 验证测试项
|
|
91
|
+
*/
|
|
92
|
+
export interface WorkerValidationItem {
|
|
93
|
+
command: string;
|
|
94
|
+
outcome: "passed" | "failed" | "not_run";
|
|
95
|
+
note?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Worker 回执载荷
|
|
100
|
+
*/
|
|
101
|
+
export interface WorkerReportPayload {
|
|
102
|
+
kind: "progress" | "question" | "blocked" | "result" | "failed";
|
|
103
|
+
summary: string;
|
|
104
|
+
changedFiles?: string[];
|
|
105
|
+
validation?: WorkerValidationItem[];
|
|
106
|
+
unresolved?: string[];
|
|
107
|
+
question?: string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function jsonByteLength(value: unknown): number {
|
|
111
|
+
return Buffer.byteLength(JSON.stringify(value ?? null), "utf8");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Pi 支持的 thinking 级别 (与 @earendil-works/pi-agent-core ThinkingLevel 一致)
|
|
116
|
+
*/
|
|
117
|
+
export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
|
|
118
|
+
export type ThinkingLevelValue = (typeof THINKING_LEVELS)[number];
|
|
119
|
+
|
|
120
|
+
// 消息载荷定义
|
|
121
|
+
export interface HelloPayload {
|
|
122
|
+
role: "supervisor" | "worker";
|
|
123
|
+
token: string;
|
|
124
|
+
controllerId: string;
|
|
125
|
+
workerId: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface HelloOkPayload {
|
|
129
|
+
sessionGeneration?: number;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface PingPayload {
|
|
133
|
+
timestamp: number;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface PongPayload {
|
|
137
|
+
timestamp: number;
|
|
138
|
+
replyTimestamp: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface AckPayload {
|
|
142
|
+
id: string;
|
|
143
|
+
ok: boolean;
|
|
144
|
+
error?: string;
|
|
145
|
+
code?: string;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ErrorPayload {
|
|
149
|
+
code: string;
|
|
150
|
+
message: string;
|
|
151
|
+
retryable?: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface LaunchPayload {
|
|
155
|
+
cwd: string;
|
|
156
|
+
nodePath: string;
|
|
157
|
+
piCliPath: string;
|
|
158
|
+
extensionPath: string;
|
|
159
|
+
workerToken: string;
|
|
160
|
+
workerPipePath: string;
|
|
161
|
+
/** 可选:限定 --model 查找的 provider;不传则使用 Pi 默认设置 */
|
|
162
|
+
provider?: string;
|
|
163
|
+
/** 可选:模型 ID 或模糊匹配模式 (可含 provider/id 与 :<thinking> 后缀) */
|
|
164
|
+
model?: string;
|
|
165
|
+
/** 可选:思考级别 off|minimal|low|medium|high|xhigh|max */
|
|
166
|
+
thinkingLevel?: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface TerminatePayload {
|
|
170
|
+
force?: boolean;
|
|
171
|
+
reason?: string;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface ChildSpawnedPayload {
|
|
175
|
+
pid: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ChildExitPayload {
|
|
179
|
+
pid: number;
|
|
180
|
+
code: number | null;
|
|
181
|
+
signal: string | null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export interface LaunchFailedPayload {
|
|
185
|
+
error: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface WorkerReadyPayload {
|
|
189
|
+
cwd: string;
|
|
190
|
+
sessionId?: string;
|
|
191
|
+
provider?: string;
|
|
192
|
+
modelId?: string;
|
|
193
|
+
thinkingLevel?: string;
|
|
194
|
+
tools: string[];
|
|
195
|
+
version: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Worker 会话中模型或思考级别变化后的刷新上报
|
|
200
|
+
*/
|
|
201
|
+
export interface ModelChangedPayload {
|
|
202
|
+
provider?: string;
|
|
203
|
+
modelId?: string;
|
|
204
|
+
thinkingLevel?: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface TaskPayload {
|
|
208
|
+
taskId: string;
|
|
209
|
+
runId: number;
|
|
210
|
+
revision: number;
|
|
211
|
+
task: string;
|
|
212
|
+
context?: string;
|
|
213
|
+
allowedPaths?: string[];
|
|
214
|
+
acceptanceCriteria?: string[];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface FollowupPayload {
|
|
218
|
+
taskId: string;
|
|
219
|
+
runId: number;
|
|
220
|
+
revision: number;
|
|
221
|
+
message: string;
|
|
222
|
+
kind: "supplement" | "reply" | "revision";
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface AbortPayload {
|
|
226
|
+
reason?: string;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface ClosePayload {
|
|
230
|
+
disposition: "accepted" | "abandoned";
|
|
231
|
+
force?: boolean;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface ActivityPayload {
|
|
235
|
+
state: "idle" | "busy" | "waiting_ui" | "unknown";
|
|
236
|
+
toolName?: string;
|
|
237
|
+
modelId?: string;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export interface ReportCandidatePayload {
|
|
241
|
+
taskId: string;
|
|
242
|
+
revision: number;
|
|
243
|
+
runId: number;
|
|
244
|
+
report: WorkerReportPayload;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface ReportCommittedPayload {
|
|
248
|
+
taskId: string;
|
|
249
|
+
revision: number;
|
|
250
|
+
runId: number;
|
|
251
|
+
report: WorkerReportPayload;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface StoppedPayload {
|
|
255
|
+
taskId: string;
|
|
256
|
+
revision: number;
|
|
257
|
+
runId: number;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export interface ReportMissingPayload {
|
|
261
|
+
taskId: string;
|
|
262
|
+
revision: number;
|
|
263
|
+
runId: number;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface LocalInputPayload {
|
|
267
|
+
taskId?: string;
|
|
268
|
+
textSummary?: string;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 协议错误码枚举
|
|
273
|
+
*/
|
|
274
|
+
export const ProtocolErrorCode = {
|
|
275
|
+
AUTH_FAILED: "AUTH_FAILED",
|
|
276
|
+
PROTOCOL_ERROR: "PROTOCOL_ERROR",
|
|
277
|
+
PAYLOAD_TOO_LARGE: "PAYLOAD_TOO_LARGE",
|
|
278
|
+
TIMEOUT: "TIMEOUT",
|
|
279
|
+
DUPLICATE_REQUEST_MISMATCH: "DUPLICATE_REQUEST_MISMATCH",
|
|
280
|
+
ALREADY_EXISTS: "ALREADY_EXISTS",
|
|
281
|
+
IDENTITY_MISMATCH: "IDENTITY_MISMATCH",
|
|
282
|
+
NOT_FOUND: "NOT_FOUND",
|
|
283
|
+
INVALID_STATE: "INVALID_STATE",
|
|
284
|
+
DEDUP_FULL: "DEDUP_FULL",
|
|
285
|
+
QUEUE_FULL: "QUEUE_FULL",
|
|
286
|
+
QUEUE_BYTES_EXCEEDED: "QUEUE_BYTES_EXCEEDED",
|
|
287
|
+
INBOX_FULL: "INBOX_FULL",
|
|
288
|
+
REPORT_CACHE_FULL: "REPORT_CACHE_FULL",
|
|
289
|
+
PEER_DISCONNECTED: "PEER_DISCONNECTED",
|
|
290
|
+
PROCESS_EXITED: "PROCESS_EXITED",
|
|
291
|
+
LAUNCH_FAILED: "LAUNCH_FAILED",
|
|
292
|
+
DISPOSED: "DISPOSED",
|
|
293
|
+
DELIVERY_UNKNOWN: "DELIVERY_UNKNOWN",
|
|
294
|
+
} as const;
|
|
295
|
+
|
|
296
|
+
export type ProtocolErrorCode = typeof ProtocolErrorCode[keyof typeof ProtocolErrorCode];
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 协议校验错误:携带错误码 (PROTOCOL_ERROR / PAYLOAD_TOO_LARGE 等)
|
|
300
|
+
*/
|
|
301
|
+
export class ProtocolValidationError extends Error {
|
|
302
|
+
public readonly code: ProtocolErrorCode;
|
|
303
|
+
|
|
304
|
+
constructor(code: ProtocolErrorCode, message: string) {
|
|
305
|
+
super(message);
|
|
306
|
+
this.code = code;
|
|
307
|
+
this.name = "ProtocolValidationError";
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ===== 内部校验工具 (错误 message 均包含字段名与 ProtocolErrorCode) =====
|
|
312
|
+
|
|
313
|
+
function requireString(value: unknown, field: string, maxLen: number, minLen = 1): string {
|
|
314
|
+
if (typeof value !== "string" || value.length < minLen) {
|
|
315
|
+
throw new ProtocolValidationError(
|
|
316
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
317
|
+
`${field} 必须是长度 >=${minLen} 的字符串: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
if (value.length > maxLen) {
|
|
321
|
+
throw new ProtocolValidationError(
|
|
322
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
323
|
+
`${field} 长度 ${value.length} 超过上限 ${maxLen}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
return value;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function optionalString(value: unknown, field: string, maxLen: number): string | undefined {
|
|
330
|
+
if (value === undefined || value === null) {
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
return requireString(value, field, maxLen);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function requireInt(value: unknown, field: string, opts: { min?: number } = {}): number {
|
|
337
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
338
|
+
throw new ProtocolValidationError(
|
|
339
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
340
|
+
`${field} 必须是整数: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
if (opts.min !== undefined && value < opts.min) {
|
|
344
|
+
throw new ProtocolValidationError(
|
|
345
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
346
|
+
`${field} 必须 >= ${opts.min},实际 ${value}: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
return value;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function requireEnum(value: unknown, field: string, allowed: readonly string[]): string {
|
|
353
|
+
if (typeof value !== "string" || !allowed.includes(value)) {
|
|
354
|
+
throw new ProtocolValidationError(
|
|
355
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
356
|
+
`${field} 必须是以下之一: ${allowed.join(" | ")},实际 ${JSON.stringify(value)}: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
return value;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function checkStringArray(value: string[], field: string, opts: { maxItems?: number; maxItemLen?: number }): string[] {
|
|
363
|
+
if (opts.maxItems !== undefined && value.length > opts.maxItems) {
|
|
364
|
+
throw new ProtocolValidationError(
|
|
365
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
366
|
+
`${field} 项数 ${value.length} 超过上限 ${opts.maxItems}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
for (let i = 0; i < value.length; i++) {
|
|
370
|
+
const item = value[i];
|
|
371
|
+
if (typeof item !== "string" || item.length === 0) {
|
|
372
|
+
throw new ProtocolValidationError(
|
|
373
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
374
|
+
`${field}[${i}] 必须是非空字符串: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
if (opts.maxItemLen !== undefined && item.length > opts.maxItemLen) {
|
|
378
|
+
throw new ProtocolValidationError(
|
|
379
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
380
|
+
`${field}[${i}] 长度 ${item.length} 超过上限 ${opts.maxItemLen}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return value;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function optionalStringArray(value: unknown, field: string, opts: { maxItems?: number; maxItemLen?: number }): string[] | undefined {
|
|
388
|
+
if (value === undefined || value === null) {
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
if (!Array.isArray(value)) {
|
|
392
|
+
throw new ProtocolValidationError(
|
|
393
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
394
|
+
`${field} 必须是字符串数组: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
return checkStringArray(value, field, opts);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function requireStringArray(value: unknown, field: string, opts: { maxItems?: number; maxItemLen?: number }): string[] {
|
|
401
|
+
if (!Array.isArray(value)) {
|
|
402
|
+
throw new ProtocolValidationError(
|
|
403
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
404
|
+
`${field} 必须是字符串数组: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
return checkStringArray(value, field, opts);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function requireObject(value: unknown, field: string): Record<string, unknown> {
|
|
411
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
412
|
+
throw new ProtocolValidationError(
|
|
413
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
414
|
+
`${field} 必须是非 null 的非数组对象: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
return value as Record<string, unknown>;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* 严格校验接收到的 Envelope 基础字段
|
|
422
|
+
*/
|
|
423
|
+
export function validateEnvelope(obj: unknown): Envelope {
|
|
424
|
+
if (typeof obj !== "object" || obj === null) {
|
|
425
|
+
throw new Error("Envelope 必须是一个非空对象");
|
|
426
|
+
}
|
|
427
|
+
const e = obj as Record<string, unknown>;
|
|
428
|
+
if (e.version !== 1) {
|
|
429
|
+
throw new Error(`不支持的协议版本: ${String(e.version)}`);
|
|
430
|
+
}
|
|
431
|
+
if (typeof e.controllerId !== "string" || !e.controllerId) {
|
|
432
|
+
throw new Error("Envelope 缺少合法的 controllerId");
|
|
433
|
+
}
|
|
434
|
+
if (typeof e.workerId !== "string" || !e.workerId) {
|
|
435
|
+
throw new Error("Envelope 缺少合法的 workerId");
|
|
436
|
+
}
|
|
437
|
+
if (typeof e.id !== "string" || !e.id) {
|
|
438
|
+
throw new Error("Envelope 缺少合法的消息 id");
|
|
439
|
+
}
|
|
440
|
+
if (typeof e.seq !== "number" || !Number.isInteger(e.seq) || e.seq < 0) {
|
|
441
|
+
throw new Error("Envelope 缺少合法的递增 seq");
|
|
442
|
+
}
|
|
443
|
+
if (typeof e.type !== "string" || !e.type) {
|
|
444
|
+
throw new Error("Envelope 缺少合法的 type");
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 长度上限
|
|
448
|
+
for (const field of ["controllerId", "workerId", "id"] as const) {
|
|
449
|
+
const v = e[field] as string;
|
|
450
|
+
if (v.length > MAX_ID_LENGTH) {
|
|
451
|
+
throw new ProtocolValidationError(
|
|
452
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
453
|
+
`Envelope ${field} 长度 ${v.length} 超过上限 ${MAX_ID_LENGTH}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (e.type.length > MAX_TYPE_LENGTH) {
|
|
458
|
+
throw new ProtocolValidationError(
|
|
459
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
460
|
+
`Envelope type 长度 ${e.type.length} 超过上限 ${MAX_TYPE_LENGTH}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
if (!isKnownMessageType(e.type)) {
|
|
464
|
+
throw new ProtocolValidationError(
|
|
465
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
466
|
+
`Envelope 包含未知消息类型: "${e.type}": ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
if (e.taskId !== undefined) {
|
|
470
|
+
if (typeof e.taskId !== "string" || e.taskId.length === 0) {
|
|
471
|
+
throw new ProtocolValidationError(
|
|
472
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
473
|
+
`Envelope taskId 必须是长度 1..${MAX_ID_LENGTH} 的字符串: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
if (e.taskId.length > MAX_ID_LENGTH) {
|
|
477
|
+
throw new ProtocolValidationError(
|
|
478
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
479
|
+
`Envelope taskId 长度 ${e.taskId.length} 超过上限 ${MAX_ID_LENGTH}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (e.revision !== undefined) {
|
|
484
|
+
if (typeof e.revision !== "number" || !Number.isInteger(e.revision) || e.revision < 1) {
|
|
485
|
+
throw new ProtocolValidationError(
|
|
486
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
487
|
+
`Envelope revision 必须是 >=1 的整数,实际 ${JSON.stringify(e.revision)}: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (e.replyTo !== undefined) {
|
|
492
|
+
if (typeof e.replyTo !== "string" || e.replyTo.length === 0) {
|
|
493
|
+
throw new ProtocolValidationError(
|
|
494
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
495
|
+
`Envelope replyTo 必须是长度 1..${MAX_ID_LENGTH} 的字符串: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
if (e.replyTo.length > MAX_ID_LENGTH) {
|
|
499
|
+
throw new ProtocolValidationError(
|
|
500
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
501
|
+
`Envelope replyTo 长度 ${e.replyTo.length} 超过上限 ${MAX_ID_LENGTH}: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (typeof e.payload !== "object" || e.payload === null || Array.isArray(e.payload)) {
|
|
506
|
+
throw new ProtocolValidationError(
|
|
507
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
508
|
+
`Envelope payload 必须是非 null 的非数组对象: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return e as unknown as Envelope;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* 按消息类型严格校验 payload,成功时返回规范化后的 payload 对象,失败抛 ProtocolValidationError
|
|
517
|
+
*/
|
|
518
|
+
export function validatePayload(type: string, payload: unknown): unknown {
|
|
519
|
+
const p = requireObject(payload, "payload");
|
|
520
|
+
switch (type) {
|
|
521
|
+
case "hello": {
|
|
522
|
+
requireEnum(p.role, "role", ["supervisor", "worker"]);
|
|
523
|
+
requireString(p.token, "token", 256);
|
|
524
|
+
requireString(p.controllerId, "controllerId", MAX_ID_LENGTH);
|
|
525
|
+
requireString(p.workerId, "workerId", MAX_ID_LENGTH);
|
|
526
|
+
return p;
|
|
527
|
+
}
|
|
528
|
+
case "hello_ok": {
|
|
529
|
+
if (p.sessionGeneration !== undefined && typeof p.sessionGeneration !== "number") {
|
|
530
|
+
throw new ProtocolValidationError(
|
|
531
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
532
|
+
`hello_ok.sessionGeneration 必须是数字: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
return p;
|
|
536
|
+
}
|
|
537
|
+
case "ping": {
|
|
538
|
+
requireInt(p.timestamp, "timestamp");
|
|
539
|
+
return p;
|
|
540
|
+
}
|
|
541
|
+
case "pong": {
|
|
542
|
+
requireInt(p.timestamp, "timestamp");
|
|
543
|
+
requireInt(p.replyTimestamp, "replyTimestamp");
|
|
544
|
+
return p;
|
|
545
|
+
}
|
|
546
|
+
case "ack": {
|
|
547
|
+
requireString(p.id, "id", MAX_ID_LENGTH);
|
|
548
|
+
if (typeof p.ok !== "boolean") {
|
|
549
|
+
throw new ProtocolValidationError(
|
|
550
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
551
|
+
`ack.ok 必须是布尔值: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
optionalString(p.error, "error", MAX_ERROR_MESSAGE_SIZE);
|
|
555
|
+
optionalString(p.code, "code", MAX_TYPE_LENGTH);
|
|
556
|
+
return p;
|
|
557
|
+
}
|
|
558
|
+
case "error": {
|
|
559
|
+
requireString(p.code, "code", MAX_TYPE_LENGTH);
|
|
560
|
+
requireString(p.message, "message", MAX_ERROR_MESSAGE_SIZE);
|
|
561
|
+
if (p.retryable !== undefined && typeof p.retryable !== "boolean") {
|
|
562
|
+
throw new ProtocolValidationError(
|
|
563
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
564
|
+
`error.retryable 必须是布尔值: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
return p;
|
|
568
|
+
}
|
|
569
|
+
case "launch": {
|
|
570
|
+
for (const field of ["cwd", "nodePath", "piCliPath", "extensionPath"] as const) {
|
|
571
|
+
requireString(p[field], field, MAX_PATH_FIELD_SIZE);
|
|
572
|
+
}
|
|
573
|
+
requireString(p.workerToken, "workerToken", 256);
|
|
574
|
+
requireString(p.workerPipePath, "workerPipePath", MAX_PATH_FIELD_SIZE);
|
|
575
|
+
optionalString(p.provider, "provider", 256);
|
|
576
|
+
optionalString(p.model, "model", 256);
|
|
577
|
+
const thinkingLevel = optionalString(p.thinkingLevel, "thinkingLevel", 256);
|
|
578
|
+
if (thinkingLevel !== undefined && !(THINKING_LEVELS as readonly string[]).includes(thinkingLevel)) {
|
|
579
|
+
throw new ProtocolValidationError(
|
|
580
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
581
|
+
`launch.thinkingLevel 必须是以下之一: ${THINKING_LEVELS.join(" | ")},实际 "${thinkingLevel}": ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
return p;
|
|
585
|
+
}
|
|
586
|
+
case "terminate": {
|
|
587
|
+
if (p.force !== undefined && typeof p.force !== "boolean") {
|
|
588
|
+
throw new ProtocolValidationError(
|
|
589
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
590
|
+
`terminate.force 必须是布尔值: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
optionalString(p.reason, "reason", MAX_ERROR_MESSAGE_SIZE);
|
|
594
|
+
return p;
|
|
595
|
+
}
|
|
596
|
+
case "child_spawned": {
|
|
597
|
+
requireInt(p.pid, "pid", { min: 1 });
|
|
598
|
+
return p;
|
|
599
|
+
}
|
|
600
|
+
case "child_exit": {
|
|
601
|
+
requireInt(p.pid, "pid", { min: 0 });
|
|
602
|
+
if (p.code !== null && p.code !== undefined) {
|
|
603
|
+
requireInt(p.code, "code");
|
|
604
|
+
}
|
|
605
|
+
if (p.signal !== null && p.signal !== undefined) {
|
|
606
|
+
requireString(p.signal, "signal", 64);
|
|
607
|
+
}
|
|
608
|
+
return p;
|
|
609
|
+
}
|
|
610
|
+
case "launch_failed": {
|
|
611
|
+
requireString(p.error, "error", MAX_ERROR_MESSAGE_SIZE);
|
|
612
|
+
return p;
|
|
613
|
+
}
|
|
614
|
+
case "worker_ready": {
|
|
615
|
+
requireString(p.cwd, "cwd", MAX_PATH_FIELD_SIZE);
|
|
616
|
+
requireStringArray(p.tools, "tools", { maxItems: 256, maxItemLen: 128 });
|
|
617
|
+
requireString(p.version, "version", 64);
|
|
618
|
+
optionalString(p.sessionId, "sessionId", 256);
|
|
619
|
+
optionalString(p.provider, "provider", 256);
|
|
620
|
+
optionalString(p.modelId, "modelId", 256);
|
|
621
|
+
optionalString(p.thinkingLevel, "thinkingLevel", 256);
|
|
622
|
+
return p;
|
|
623
|
+
}
|
|
624
|
+
case "model_changed": {
|
|
625
|
+
optionalString(p.provider, "provider", 256);
|
|
626
|
+
optionalString(p.modelId, "modelId", 256);
|
|
627
|
+
optionalString(p.thinkingLevel, "thinkingLevel", 256);
|
|
628
|
+
return p;
|
|
629
|
+
}
|
|
630
|
+
case "task": {
|
|
631
|
+
requireString(p.taskId, "taskId", MAX_ID_LENGTH);
|
|
632
|
+
requireInt(p.runId, "runId", { min: 1 });
|
|
633
|
+
requireInt(p.revision, "revision", { min: 1 });
|
|
634
|
+
requireString(p.task, "task", MAX_TASK_TEXT_SIZE);
|
|
635
|
+
optionalString(p.context, "context", MAX_TASK_TEXT_SIZE);
|
|
636
|
+
optionalStringArray(p.allowedPaths, "allowedPaths", { maxItems: 128, maxItemLen: 1024 });
|
|
637
|
+
optionalStringArray(p.acceptanceCriteria, "acceptanceCriteria", { maxItems: 128, maxItemLen: 1024 });
|
|
638
|
+
return p;
|
|
639
|
+
}
|
|
640
|
+
case "followup": {
|
|
641
|
+
requireString(p.taskId, "taskId", MAX_ID_LENGTH);
|
|
642
|
+
requireInt(p.runId, "runId", { min: 1 });
|
|
643
|
+
requireInt(p.revision, "revision", { min: 1 });
|
|
644
|
+
requireString(p.message, "message", MAX_FOLLOWUP_MESSAGE_SIZE);
|
|
645
|
+
requireEnum(p.kind, "kind", ["supplement", "reply", "revision"]);
|
|
646
|
+
return p;
|
|
647
|
+
}
|
|
648
|
+
case "abort": {
|
|
649
|
+
optionalString(p.reason, "reason", MAX_ERROR_MESSAGE_SIZE);
|
|
650
|
+
return p;
|
|
651
|
+
}
|
|
652
|
+
case "close": {
|
|
653
|
+
requireEnum(p.disposition, "disposition", ["accepted", "abandoned"]);
|
|
654
|
+
if (p.force !== undefined && typeof p.force !== "boolean") {
|
|
655
|
+
throw new ProtocolValidationError(
|
|
656
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
657
|
+
`close.force 必须是布尔值: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
return p;
|
|
661
|
+
}
|
|
662
|
+
case "activity": {
|
|
663
|
+
requireEnum(p.state, "state", ["idle", "busy", "waiting_ui", "unknown"]);
|
|
664
|
+
optionalString(p.toolName, "toolName", 256);
|
|
665
|
+
optionalString(p.modelId, "modelId", 256);
|
|
666
|
+
return p;
|
|
667
|
+
}
|
|
668
|
+
case "report_candidate":
|
|
669
|
+
case "report_committed": {
|
|
670
|
+
// 真实流程中 currentTaskId 为空时会回退为空串,这里允许 0 长度
|
|
671
|
+
requireString(p.taskId, "taskId", MAX_ID_LENGTH, 0);
|
|
672
|
+
requireInt(p.runId, "runId", { min: 1 });
|
|
673
|
+
requireInt(p.revision, "revision", { min: 1 });
|
|
674
|
+
p.report = validateWorkerReport(p.report);
|
|
675
|
+
return p;
|
|
676
|
+
}
|
|
677
|
+
case "report_missing":
|
|
678
|
+
case "stopped": {
|
|
679
|
+
requireString(p.taskId, "taskId", MAX_ID_LENGTH, 0);
|
|
680
|
+
requireInt(p.runId, "runId", { min: 1 });
|
|
681
|
+
requireInt(p.revision, "revision", { min: 1 });
|
|
682
|
+
return p;
|
|
683
|
+
}
|
|
684
|
+
case "local_input": {
|
|
685
|
+
optionalString(p.taskId, "taskId", MAX_ID_LENGTH);
|
|
686
|
+
optionalString(p.textSummary, "textSummary", 512);
|
|
687
|
+
return p;
|
|
688
|
+
}
|
|
689
|
+
default:
|
|
690
|
+
throw new ProtocolValidationError(
|
|
691
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
692
|
+
`未知消息类型 "${type}" 的 payload 无法校验: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* 严格校验 worker 回执 (worker_report 载荷)
|
|
699
|
+
*/
|
|
700
|
+
export function validateWorkerReport(
|
|
701
|
+
report: unknown,
|
|
702
|
+
opts?: { maxBytes?: number },
|
|
703
|
+
): WorkerReportPayload {
|
|
704
|
+
const maxBytes = opts?.maxBytes ?? MAX_REPORT_SIZE;
|
|
705
|
+
const totalBytes = jsonByteLength(report);
|
|
706
|
+
if (totalBytes > maxBytes) {
|
|
707
|
+
throw new ProtocolValidationError(
|
|
708
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
709
|
+
`回执整体 ${totalBytes} 字节超过上限 ${maxBytes} 字节 (64 KiB): ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
const p = requireObject(report, "report");
|
|
713
|
+
requireEnum(p.kind, "kind", ["progress", "question", "blocked", "result", "failed"]);
|
|
714
|
+
requireString(p.summary, "summary", MAX_REPORT_SUMMARY_SIZE);
|
|
715
|
+
optionalStringArray(p.changedFiles, "changedFiles", { maxItems: 256, maxItemLen: 1024 });
|
|
716
|
+
if (p.validation !== undefined && p.validation !== null) {
|
|
717
|
+
if (!Array.isArray(p.validation)) {
|
|
718
|
+
throw new ProtocolValidationError(
|
|
719
|
+
ProtocolErrorCode.PROTOCOL_ERROR,
|
|
720
|
+
`report.validation 必须是数组: ${ProtocolErrorCode.PROTOCOL_ERROR}`,
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
if (p.validation.length > 64) {
|
|
724
|
+
throw new ProtocolValidationError(
|
|
725
|
+
ProtocolErrorCode.PAYLOAD_TOO_LARGE,
|
|
726
|
+
`report.validation 项数 ${p.validation.length} 超过上限 64: ${ProtocolErrorCode.PAYLOAD_TOO_LARGE}`,
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
for (let i = 0; i < p.validation.length; i++) {
|
|
730
|
+
const item = requireObject(p.validation[i], `report.validation[${i}]`);
|
|
731
|
+
requireString(item.command, `report.validation[${i}].command`, 2048);
|
|
732
|
+
requireEnum(item.outcome, `report.validation[${i}].outcome`, ["passed", "failed", "not_run"]);
|
|
733
|
+
optionalString(item.note, `report.validation[${i}].note`, 1024);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
optionalStringArray(p.unresolved, "unresolved", { maxItems: 64, maxItemLen: 2048 });
|
|
737
|
+
optionalString(p.question, "question", 8192);
|
|
738
|
+
return p as unknown as WorkerReportPayload;
|
|
739
|
+
}
|