@mozhi0012/pi-terminal-worker 0.3.0 → 0.5.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/README.md +238 -212
- package/dist/controller.d.ts +6 -0
- package/dist/controller.d.ts.map +1 -1
- package/dist/controller.js +118 -56
- package/dist/controller.js.map +1 -1
- package/dist/json-schema.d.ts +28 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +54 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/launcher.d.ts +9 -1
- package/dist/launcher.d.ts.map +1 -1
- package/dist/launcher.js +95 -22
- package/dist/launcher.js.map +1 -1
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +4 -0
- package/dist/lifecycle.js.map +1 -1
- package/dist/mcp-server.d.ts +40 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +364 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/multi-worker.d.ts.map +1 -1
- package/dist/multi-worker.js +1 -1
- package/dist/multi-worker.js.map +1 -1
- package/dist/schema-validate.d.ts +17 -0
- package/dist/schema-validate.d.ts.map +1 -0
- package/dist/schema-validate.js +100 -0
- package/dist/schema-validate.js.map +1 -0
- package/dist/terminal-multiplexer.d.ts +25 -0
- package/dist/terminal-multiplexer.d.ts.map +1 -0
- package/dist/terminal-multiplexer.js +102 -0
- package/dist/terminal-multiplexer.js.map +1 -0
- package/package.json +81 -75
- package/runtime/bootstrap-support.mjs +68 -67
- package/runtime/bootstrap.mjs +328 -312
- package/runtime/terminal-support.mjs +63 -0
- package/src/controller.ts +101 -58
- package/src/json-schema.ts +61 -0
- package/src/launcher.ts +592 -517
- package/src/lifecycle.ts +289 -284
- package/src/mcp-server.ts +409 -0
- package/src/multi-worker.ts +1 -1
- package/src/schema-validate.ts +100 -0
- package/src/terminal-multiplexer.ts +108 -0
package/src/launcher.ts
CHANGED
|
@@ -1,517 +1,592 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 启动器:终端后端探测 (WT → PowerShell → CMD 回退)、命令行构建与描述符编码
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as fs from "node:fs";
|
|
6
|
-
import * as path from "node:path";
|
|
7
|
-
import { spawn, spawnSync, type ChildProcess, type SpawnOptions } from "node:child_process";
|
|
8
|
-
import { fileURLToPath } from "node:url";
|
|
9
|
-
import type { WorkerRole } from "./protocol.js";
|
|
10
|
-
|
|
11
|
-
export interface WorkerDescriptor {
|
|
12
|
-
version: 1;
|
|
13
|
-
controllerId: string;
|
|
14
|
-
workerId: string;
|
|
15
|
-
pipePath: string;
|
|
16
|
-
bootstrapToken: string;
|
|
17
|
-
/** 缺省仅用于兼容旧描述符;新启动始终写入明确角色。 */
|
|
18
|
-
role?: WorkerRole;
|
|
19
|
-
/** 仅 lead 派发的 L2 设置,用于父进程异常退出后的级联清理。 */
|
|
20
|
-
ownerPid?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 可用终端后端:Windows Terminal 优先,缺失时依次回退 PowerShell 与 CMD
|
|
25
|
-
*/
|
|
26
|
-
export type TerminalBackend = "wt" | "powershell" | "cmd";
|
|
27
|
-
|
|
28
|
-
export interface TerminalBackendInfo {
|
|
29
|
-
backend: TerminalBackend;
|
|
30
|
-
command: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface LaunchEnvironment {
|
|
34
|
-
terminal: TerminalBackendInfo;
|
|
35
|
-
nodePath: string;
|
|
36
|
-
piCliPath: string;
|
|
37
|
-
extensionPath: string;
|
|
38
|
-
bootstrapPath: string;
|
|
39
|
-
cwd: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 可注入的探测函数集合,便于单元测试不依赖真实环境
|
|
44
|
-
*/
|
|
45
|
-
export interface TerminalProbes {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
if (fs.existsSync(
|
|
255
|
-
return
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
const
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*/
|
|
430
|
-
export function
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 启动器:终端后端探测 (WT → PowerShell → CMD 回退)、命令行构建与描述符编码
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as fs from "node:fs";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { spawn, spawnSync, type ChildProcess, type SpawnOptions } from "node:child_process";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import type { WorkerRole } from "./protocol.js";
|
|
10
|
+
|
|
11
|
+
export interface WorkerDescriptor {
|
|
12
|
+
version: 1;
|
|
13
|
+
controllerId: string;
|
|
14
|
+
workerId: string;
|
|
15
|
+
pipePath: string;
|
|
16
|
+
bootstrapToken: string;
|
|
17
|
+
/** 缺省仅用于兼容旧描述符;新启动始终写入明确角色。 */
|
|
18
|
+
role?: WorkerRole;
|
|
19
|
+
/** 仅 lead 派发的 L2 设置,用于父进程异常退出后的级联清理。 */
|
|
20
|
+
ownerPid?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 可用终端后端:Windows Terminal 优先,缺失时依次回退 PowerShell 与 CMD
|
|
25
|
+
*/
|
|
26
|
+
export type TerminalBackend = "herdr" | "tmux" | "wt" | "powershell" | "cmd";
|
|
27
|
+
|
|
28
|
+
export interface TerminalBackendInfo {
|
|
29
|
+
backend: TerminalBackend;
|
|
30
|
+
command: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface LaunchEnvironment {
|
|
34
|
+
terminal: TerminalBackendInfo;
|
|
35
|
+
nodePath: string;
|
|
36
|
+
piCliPath: string;
|
|
37
|
+
extensionPath: string;
|
|
38
|
+
bootstrapPath: string;
|
|
39
|
+
cwd: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 可注入的探测函数集合,便于单元测试不依赖真实环境
|
|
44
|
+
*/
|
|
45
|
+
export interface TerminalProbes {
|
|
46
|
+
platform?: NodeJS.Platform;
|
|
47
|
+
env?: NodeJS.ProcessEnv;
|
|
48
|
+
findHerdr?: () => string | null;
|
|
49
|
+
findTmux?: () => string | null;
|
|
50
|
+
findWt: () => string | null;
|
|
51
|
+
findPowershell: () => string | null;
|
|
52
|
+
findCmd: () => string | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 描述符 Base64Url 编码 (不包含任务文本或凭据)
|
|
57
|
+
*/
|
|
58
|
+
export function encodeDescriptor(desc: WorkerDescriptor): string {
|
|
59
|
+
const json = JSON.stringify(desc);
|
|
60
|
+
return Buffer.from(json, "utf8").toString("base64url");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 描述符 Base64Url 解码
|
|
65
|
+
*/
|
|
66
|
+
export function decodeDescriptor(raw: string): WorkerDescriptor {
|
|
67
|
+
try {
|
|
68
|
+
const json = Buffer.from(raw, "base64url").toString("utf8");
|
|
69
|
+
const parsed = JSON.parse(json);
|
|
70
|
+
if (
|
|
71
|
+
parsed.version !== 1 ||
|
|
72
|
+
typeof parsed.controllerId !== "string" ||
|
|
73
|
+
typeof parsed.workerId !== "string" ||
|
|
74
|
+
typeof parsed.pipePath !== "string" ||
|
|
75
|
+
typeof parsed.bootstrapToken !== "string" ||
|
|
76
|
+
(parsed.role !== undefined && parsed.role !== "worker" && parsed.role !== "lead") ||
|
|
77
|
+
(parsed.ownerPid !== undefined && (!Number.isSafeInteger(parsed.ownerPid) || parsed.ownerPid <= 0))
|
|
78
|
+
) {
|
|
79
|
+
throw new Error("描述符格式不符合规范");
|
|
80
|
+
}
|
|
81
|
+
return parsed as WorkerDescriptor;
|
|
82
|
+
} catch (err: unknown) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`解码 Worker 描述符失败: ${err instanceof Error ? err.message : String(err)}`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 寻找 Windows PowerShell (powershell.exe)
|
|
91
|
+
*/
|
|
92
|
+
export function findPowershell(): string | null {
|
|
93
|
+
const override = process.env.PI_TERMINAL_WORKER_POWERSHELL_PATH;
|
|
94
|
+
if (override && fs.existsSync(override)) {
|
|
95
|
+
return override;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const res = spawnSync("where.exe", ["powershell.exe"], {
|
|
100
|
+
shell: false,
|
|
101
|
+
windowsHide: true,
|
|
102
|
+
encoding: "utf8",
|
|
103
|
+
});
|
|
104
|
+
if (!res.error && res.status === 0 && res.stdout) {
|
|
105
|
+
const located = res.stdout.split(/\r?\n/).map((line) => line.trim()).find(Boolean);
|
|
106
|
+
if (located) return located;
|
|
107
|
+
}
|
|
108
|
+
} catch {}
|
|
109
|
+
|
|
110
|
+
const windir = process.env.WINDIR || "C:\\Windows";
|
|
111
|
+
const candidate = path.join(windir, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
112
|
+
if (fs.existsSync(candidate)) {
|
|
113
|
+
return candidate;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 寻找 CMD (comspec / System32\cmd.exe)
|
|
121
|
+
*/
|
|
122
|
+
export function findCmdShell(): string | null {
|
|
123
|
+
const comspec = process.env.Comspec || process.env.COMSPEC;
|
|
124
|
+
if (comspec && fs.existsSync(comspec)) {
|
|
125
|
+
return comspec;
|
|
126
|
+
}
|
|
127
|
+
const windir = process.env.WINDIR || "C:\\Windows";
|
|
128
|
+
const candidate = path.join(windir, "System32", "cmd.exe");
|
|
129
|
+
if (fs.existsSync(candidate)) {
|
|
130
|
+
return candidate;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 按 WT → PowerShell → CMD 顺序探测终端后端;全部缺失时返回 null (由调用方报明确错误)
|
|
137
|
+
*/
|
|
138
|
+
export function detectTerminalBackends(probes: Partial<TerminalProbes> = {}): TerminalBackendInfo[] {
|
|
139
|
+
const platform = probes.platform ?? (process.env.PI_TEST_MOCK_PLATFORM as NodeJS.Platform | undefined) ?? process.platform;
|
|
140
|
+
const env = probes.env ?? process.env;
|
|
141
|
+
// Injected probes must never pick up a developer's real multiplexer session.
|
|
142
|
+
const injected = Object.keys(probes).length > 0;
|
|
143
|
+
const inHerdr = env.HERDR_ENV === "1" && !!env.HERDR_PANE_ID;
|
|
144
|
+
const defaults: TerminalBackend[] = [
|
|
145
|
+
...(inHerdr ? ["herdr" as const] : []),
|
|
146
|
+
...(platform === "win32" ? ["wt", "powershell", "cmd"] as const : ["tmux"] as const),
|
|
147
|
+
];
|
|
148
|
+
const setting = env.PI_TERMINAL_WORKER_BACKEND;
|
|
149
|
+
let order = defaults;
|
|
150
|
+
if (setting !== undefined) {
|
|
151
|
+
const entries = setting.split(",").map((s) => s.trim());
|
|
152
|
+
const names = new Set(["herdr", "tmux", "wt", "powershell", "cmd"]);
|
|
153
|
+
if (entries.some((s) => !names.has(s.replace(/^!/, "")))) {
|
|
154
|
+
throw new Error(`无效的 PI_TERMINAL_WORKER_BACKEND: ${setting}`);
|
|
155
|
+
}
|
|
156
|
+
const excluded = new Set(entries.filter((s) => s.startsWith("!")).map((s) => s.slice(1)));
|
|
157
|
+
const selected = entries.filter((s) => !s.startsWith("!")) as TerminalBackend[];
|
|
158
|
+
order = [...new Set(selected.length ? selected : defaults)].filter((s) => !excluded.has(s));
|
|
159
|
+
}
|
|
160
|
+
const finders: Record<TerminalBackend, () => string | null> = {
|
|
161
|
+
herdr: probes.findHerdr ?? (injected ? () => null : () => findHerdr(env, platform)),
|
|
162
|
+
tmux: probes.findTmux ?? (injected ? () => null : () => findExecutable("tmux", env.PI_TERMINAL_WORKER_TMUX_PATH, platform)),
|
|
163
|
+
wt: probes.findWt ?? findWindowsTerminal,
|
|
164
|
+
powershell: probes.findPowershell ?? findPowershell,
|
|
165
|
+
cmd: probes.findCmd ?? findCmdShell,
|
|
166
|
+
};
|
|
167
|
+
const result: TerminalBackendInfo[] = [];
|
|
168
|
+
for (const backend of order) {
|
|
169
|
+
if ((backend === "wt" || backend === "powershell" || backend === "cmd") && platform !== "win32") continue;
|
|
170
|
+
if (backend === "herdr" && !inHerdr) continue;
|
|
171
|
+
const command = finders[backend]();
|
|
172
|
+
if (command) result.push({ backend, command });
|
|
173
|
+
}
|
|
174
|
+
if (setting && !result.length && setting.split(",").some((s) => !s.trim().startsWith("!"))) {
|
|
175
|
+
throw new Error(`指定的终端后端不可用: ${setting};herdr 需要 HERDR_ENV=1/HERDR_PANE_ID,` +
|
|
176
|
+
"请检查 PI_TERMINAL_WORKER_HERDR_PATH / PI_TERMINAL_WORKER_TMUX_PATH 和平台限制");
|
|
177
|
+
}
|
|
178
|
+
return result;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function findExecutable(name: string, override: string | undefined, platform = process.platform): string | null {
|
|
182
|
+
if (override) return fs.existsSync(override) ? override : null;
|
|
183
|
+
const found = spawnSync(platform === "win32" ? "where.exe" : "which", [name], {
|
|
184
|
+
encoding: "utf8", windowsHide: true, timeout: 5000,
|
|
185
|
+
});
|
|
186
|
+
return !found.error && found.status === 0 ? found.stdout.trim().split(/\r?\n/)[0] || null : null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function findHerdr(env = process.env, platform = process.platform): string | null {
|
|
190
|
+
const found = findExecutable(platform === "win32" ? "herdr.exe" : "herdr", env.PI_TERMINAL_WORKER_HERDR_PATH, platform);
|
|
191
|
+
if (found || env.PI_TERMINAL_WORKER_HERDR_PATH) return found;
|
|
192
|
+
const candidate = platform === "win32" && env.LOCALAPPDATA
|
|
193
|
+
? path.join(env.LOCALAPPDATA, "Programs", "Herdr", "bin", "herdr.exe") : null;
|
|
194
|
+
return candidate && fs.existsSync(candidate) ? candidate : null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function detectTerminalBackend(probes: Partial<TerminalProbes> = {}): TerminalBackendInfo | null {
|
|
198
|
+
return detectTerminalBackends(probes)[0] ?? null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 寻找 Windows Terminal (wt.exe)
|
|
203
|
+
*/
|
|
204
|
+
export function findWindowsTerminal(): string | null {
|
|
205
|
+
if (process.env.PI_TERMINAL_WORKER_WT_PATH && fs.existsSync(process.env.PI_TERMINAL_WORKER_WT_PATH)) {
|
|
206
|
+
return process.env.PI_TERMINAL_WORKER_WT_PATH;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
// Do not invoke wt.exe with an unsupported probe such as --version: older
|
|
211
|
+
// Windows Terminal builds open their Help dialog for unknown arguments.
|
|
212
|
+
const res = spawnSync("where.exe", ["wt.exe"], {
|
|
213
|
+
shell: false,
|
|
214
|
+
windowsHide: true,
|
|
215
|
+
encoding: "utf8",
|
|
216
|
+
});
|
|
217
|
+
if (!res.error && res.status === 0 && res.stdout) {
|
|
218
|
+
const located = res.stdout.split(/\r?\n/).map((line) => line.trim()).find(Boolean);
|
|
219
|
+
if (located) return located;
|
|
220
|
+
}
|
|
221
|
+
} catch {}
|
|
222
|
+
|
|
223
|
+
const localAppData = process.env.LOCALAPPDATA;
|
|
224
|
+
if (localAppData) {
|
|
225
|
+
const defaultWt = path.join(localAppData, "Microsoft", "WindowsApps", "wt.exe");
|
|
226
|
+
if (fs.existsSync(defaultWt)) {
|
|
227
|
+
return defaultWt;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 尝试从常见的系统用户目录探测
|
|
232
|
+
const userProfile = process.env.USERPROFILE;
|
|
233
|
+
if (userProfile) {
|
|
234
|
+
const userWt = path.join(
|
|
235
|
+
userProfile,
|
|
236
|
+
"AppData",
|
|
237
|
+
"Local",
|
|
238
|
+
"Microsoft",
|
|
239
|
+
"WindowsApps",
|
|
240
|
+
"wt.exe",
|
|
241
|
+
);
|
|
242
|
+
if (fs.existsSync(userWt)) {
|
|
243
|
+
return userWt;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 寻找 Pi CLI 入口文件
|
|
252
|
+
*/
|
|
253
|
+
export function findPiCliPath(): string | null {
|
|
254
|
+
if (process.env.PI_TERMINAL_WORKER_PI_CLI_PATH && fs.existsSync(process.env.PI_TERMINAL_WORKER_PI_CLI_PATH)) {
|
|
255
|
+
return process.env.PI_TERMINAL_WORKER_PI_CLI_PATH;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const entry = process.argv[1];
|
|
259
|
+
if (entry && fs.existsSync(entry)) {
|
|
260
|
+
const real = fs.realpathSync(entry);
|
|
261
|
+
if (/[/\\\\]@earendil-works[/\\\\]pi-coding-agent[/\\\\].*[/\\\\]cli\.js$/.test(real)) return real;
|
|
262
|
+
}
|
|
263
|
+
// Invoke npm's JS entry with this Node when available (also works with portable Node).
|
|
264
|
+
const npmCli = path.join(path.dirname(process.execPath), process.platform === "win32"
|
|
265
|
+
? "node_modules/npm/bin/npm-cli.js" : "../lib/node_modules/npm/bin/npm-cli.js");
|
|
266
|
+
const root = fs.existsSync(npmCli)
|
|
267
|
+
? spawnSync(process.execPath, [npmCli, "root", "-g"], { encoding: "utf8", windowsHide: true, timeout: 5000 })
|
|
268
|
+
: spawnSync("npm", ["root", "-g"], { encoding: "utf8", windowsHide: true, timeout: 5000 });
|
|
269
|
+
if (!root.error && root.status === 0) {
|
|
270
|
+
const candidate = path.join(root.stdout.trim(), "@earendil-works/pi-coding-agent/dist/bundle/cli.js");
|
|
271
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const appData = process.env.APPDATA;
|
|
275
|
+
if (appData) {
|
|
276
|
+
const candidate = path.join(
|
|
277
|
+
appData,
|
|
278
|
+
"npm",
|
|
279
|
+
"node_modules",
|
|
280
|
+
"@earendil-works",
|
|
281
|
+
"pi-coding-agent",
|
|
282
|
+
"dist",
|
|
283
|
+
"bundle",
|
|
284
|
+
"cli.js",
|
|
285
|
+
);
|
|
286
|
+
if (fs.existsSync(candidate)) {
|
|
287
|
+
return candidate;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// 尝试探测全局 npm 根目录
|
|
292
|
+
const prefix = process.env.PREFIX || (process.platform === "win32" ? path.join(process.env.APPDATA || "", "npm") : "/usr/local");
|
|
293
|
+
const fallback = path.join(
|
|
294
|
+
prefix,
|
|
295
|
+
"node_modules",
|
|
296
|
+
"@earendil-works",
|
|
297
|
+
"pi-coding-agent",
|
|
298
|
+
"dist",
|
|
299
|
+
"bundle",
|
|
300
|
+
"cli.js",
|
|
301
|
+
);
|
|
302
|
+
if (fs.existsSync(fallback)) {
|
|
303
|
+
return fallback;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* 解析包内 bootstrap.mjs 脚本绝对路径
|
|
311
|
+
*/
|
|
312
|
+
export function getBootstrapPath(): string {
|
|
313
|
+
const currentFile = fileURLToPath(import.meta.url);
|
|
314
|
+
const pkgRoot = path.resolve(path.dirname(currentFile), "..");
|
|
315
|
+
const candidate = path.join(pkgRoot, "runtime", "bootstrap.mjs");
|
|
316
|
+
if (fs.existsSync(candidate)) {
|
|
317
|
+
return candidate;
|
|
318
|
+
}
|
|
319
|
+
throw new Error(`找不到 bootstrap.mjs 脚本: ${candidate}`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* 返回与当前 Controller 相同的扩展包路径,供 Worker 显式加载。
|
|
324
|
+
*/
|
|
325
|
+
export function getExtensionPath(): string {
|
|
326
|
+
const currentFile = fileURLToPath(import.meta.url);
|
|
327
|
+
const pkgRoot = path.resolve(path.dirname(currentFile), "..");
|
|
328
|
+
const distEntry = path.join(pkgRoot, "dist", "extension.js");
|
|
329
|
+
if (fs.existsSync(distEntry)) {
|
|
330
|
+
return pkgRoot;
|
|
331
|
+
}
|
|
332
|
+
const sourceEntry = path.join(pkgRoot, "src", "extension.ts");
|
|
333
|
+
if (fs.existsSync(sourceEntry)) {
|
|
334
|
+
return pkgRoot;
|
|
335
|
+
}
|
|
336
|
+
throw new Error(`找不到扩展入口: ${pkgRoot}`);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* 校验并准备启动环境
|
|
341
|
+
*/
|
|
342
|
+
export function validateAndPrepareLaunch(
|
|
343
|
+
targetCwd: string,
|
|
344
|
+
probes: Partial<TerminalProbes> = {},
|
|
345
|
+
): LaunchEnvironment {
|
|
346
|
+
// 2. 校验 CWD
|
|
347
|
+
if (!path.isAbsolute(targetCwd)) {
|
|
348
|
+
throw new Error(`cwd 必须是绝对路径: "${targetCwd}"`);
|
|
349
|
+
}
|
|
350
|
+
if (!fs.existsSync(targetCwd)) {
|
|
351
|
+
throw new Error(`指定的 cwd 不存在: "${targetCwd}"`);
|
|
352
|
+
}
|
|
353
|
+
const stat = fs.statSync(targetCwd);
|
|
354
|
+
if (!stat.isDirectory()) {
|
|
355
|
+
throw new Error(`指定的 cwd 不是一个目录: "${targetCwd}"`);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// 3. 探测终端后端:WT → PowerShell → CMD,全部缺失时明确失败 (不盲目继续)
|
|
359
|
+
const backends = detectTerminalBackends(probes);
|
|
360
|
+
const terminal = backends[0] ?? null;
|
|
361
|
+
if (!terminal) {
|
|
362
|
+
if (process.env.PI_TEST_MOCK_WT) {
|
|
363
|
+
// 测试环境允许模拟 WT
|
|
364
|
+
} else {
|
|
365
|
+
throw new Error(
|
|
366
|
+
`未找到可用的终端后端 (Windows: herdr/wt/powershell/cmd;Linux: herdr/tmux)。` +
|
|
367
|
+
`请安装平台适用的终端,检查 HERDR_ENV/HERDR_PANE_ID,或设置 PI_TERMINAL_WORKER_BACKEND、` +
|
|
368
|
+
`PI_TERMINAL_WORKER_HERDR_PATH / PI_TERMINAL_WORKER_TMUX_PATH / PI_TERMINAL_WORKER_WT_PATH。`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// 4. 校验 Node
|
|
374
|
+
const nodePath = process.execPath;
|
|
375
|
+
if (!fs.existsSync(nodePath)) {
|
|
376
|
+
throw new Error(`未找到 Node 可执行文件: ${nodePath}`);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// 5. 校验 Pi CLI
|
|
380
|
+
const piCliPath = findPiCliPath();
|
|
381
|
+
if (!piCliPath && !process.env.PI_TEST_MOCK_CLI) {
|
|
382
|
+
throw new Error(
|
|
383
|
+
`未找到 Pi CLI 入口文件 (@earendil-works/pi-coding-agent)。请确保全局已正确安装。`,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// 6. 获取 bootstrap.mjs 与当前扩展包路径
|
|
388
|
+
const bootstrapPath = getBootstrapPath();
|
|
389
|
+
const extensionPath = getExtensionPath();
|
|
390
|
+
|
|
391
|
+
return {
|
|
392
|
+
terminal: terminal ?? { backend: "wt", command: "wt.exe" },
|
|
393
|
+
nodePath,
|
|
394
|
+
piCliPath: piCliPath || "cli.js",
|
|
395
|
+
extensionPath,
|
|
396
|
+
bootstrapPath,
|
|
397
|
+
cwd: targetCwd,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 构造 Windows Terminal 启动参数
|
|
403
|
+
*/
|
|
404
|
+
export function buildWtArgs(
|
|
405
|
+
title: string,
|
|
406
|
+
cwd: string,
|
|
407
|
+
nodePath: string,
|
|
408
|
+
bootstrapPath: string,
|
|
409
|
+
descriptorBase64Url: string,
|
|
410
|
+
): string[] {
|
|
411
|
+
return [
|
|
412
|
+
"-w",
|
|
413
|
+
"new",
|
|
414
|
+
"new-tab",
|
|
415
|
+
"--title",
|
|
416
|
+
title,
|
|
417
|
+
"-d",
|
|
418
|
+
cwd,
|
|
419
|
+
nodePath,
|
|
420
|
+
bootstrapPath,
|
|
421
|
+
"--descriptor",
|
|
422
|
+
descriptorBase64Url,
|
|
423
|
+
];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* PowerShell 单引号字符串:仅需转义内部单引号 (成对翻倍),
|
|
428
|
+
* 天然保护空格、中文、% ! & " 换行等字符。
|
|
429
|
+
*/
|
|
430
|
+
export function psQuote(value: string): string {
|
|
431
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Windows 原生引号处理 (供 PowerShell ArgumentList / CreateProcess 命令行解析):
|
|
436
|
+
* 双引号包裹 + 内部 " 转义为 \"。
|
|
437
|
+
*/
|
|
438
|
+
export function windowsQuoted(value: string): string {
|
|
439
|
+
return `"${value.replace(/"/g, '\\"')}"`;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* CMD 后端安全约束:值中不得出现双引号 / CR / LF / NUL。
|
|
444
|
+
* 这些字符会破坏 start "..." 的引号结构或切断命令行,且 /c 的百分号规则与批处理不同,
|
|
445
|
+
* 不尝试用 %% 或 ^ 转义任意内插字符串;原生路径中的 & % ! 在环境变量单次展开及引号内保留。
|
|
446
|
+
*/
|
|
447
|
+
export function assertCmdSafeValues(pairs: Array<[string, string]>): void {
|
|
448
|
+
for (const [name, value] of pairs) {
|
|
449
|
+
if (/["\r\n\0]/.test(value)) {
|
|
450
|
+
throw new Error(
|
|
451
|
+
`CMD 后端要求 ${name} 不含双引号/CR/LF/NUL: ${JSON.stringify(value)}`,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* 统一的终端启动描述:由 buildTerminalLaunch 产出,由 launchWorkerWindow 执行
|
|
459
|
+
*/
|
|
460
|
+
export interface TerminalLaunchSpec {
|
|
461
|
+
backend: TerminalBackend;
|
|
462
|
+
command: string;
|
|
463
|
+
args: string[];
|
|
464
|
+
cwd: string;
|
|
465
|
+
title: string;
|
|
466
|
+
/** CMD 后端将各值放入受控 env 字段,命令串只含 %ENV_*% 占位 */
|
|
467
|
+
extraEnv?: Record<string, string>;
|
|
468
|
+
/** PS 后端:短命隐藏宿主 (真正的新控制台由 Start-Process 创建) */
|
|
469
|
+
windowsHide?: boolean;
|
|
470
|
+
/** CMD 后端:args 即完整命令行,Node 不做任何二次引号处理 */
|
|
471
|
+
windowsVerbatimArguments?: boolean;
|
|
472
|
+
/** Multiplexers start bootstrap in a real pane, then retain an explicit cleanup target. */
|
|
473
|
+
workerCommand?: string[];
|
|
474
|
+
tmuxSession?: string;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* 构造终端启动描述:
|
|
479
|
+
* - wt:沿用 buildWtArgs (shell:false 直接传参,无注入面);
|
|
480
|
+
* - powershell:短命隐藏 PowerShell 的 EncodedCommand 执行
|
|
481
|
+
* Start-Process -FilePath node -WorkingDirectory cwd -WindowStyle Normal -ErrorAction Stop,
|
|
482
|
+
* 由新控制台直接跑 bootstrap,保证 Node stdin/stdout 为真实 TTY;
|
|
483
|
+
* ArgumentList 对路径做 Windows 引号处理 (PS 的 ArgumentList 会拼成字符串);
|
|
484
|
+
* - cmd:固定命令串 start "%ENV_TITLE%" /D "%ENV_CWD%" "%ENV_NODE%" "%ENV_BOOT%" --descriptor %ENV_DESC%,
|
|
485
|
+
* 值全部放受控 env 字段,/d /v:off /s /c + windowsVerbatimArguments。
|
|
486
|
+
* 三种后端均为独立可交互控制台,bootstrap/子进程退出或关窗后自动关闭。
|
|
487
|
+
*/
|
|
488
|
+
export function buildTerminalLaunch(
|
|
489
|
+
env: LaunchEnvironment,
|
|
490
|
+
title: string,
|
|
491
|
+
descriptorBase64Url: string,
|
|
492
|
+
): TerminalLaunchSpec {
|
|
493
|
+
const t = env.terminal;
|
|
494
|
+
|
|
495
|
+
if (t.backend === "herdr") {
|
|
496
|
+
return { backend: "herdr", command: t.command, args: [], cwd: env.cwd, title,
|
|
497
|
+
workerCommand: [env.nodePath, env.bootstrapPath, "--descriptor", descriptorBase64Url] };
|
|
498
|
+
}
|
|
499
|
+
if (t.backend === "tmux") {
|
|
500
|
+
const session = `pitw-${decodeDescriptor(descriptorBase64Url).workerId}`;
|
|
501
|
+
if (!/^pitw-[a-zA-Z0-9_-]+$/.test(session)) throw new Error("非法 tmux Worker ID");
|
|
502
|
+
const inside = !!process.env.TMUX;
|
|
503
|
+
return {
|
|
504
|
+
backend: "tmux", command: t.command, cwd: env.cwd, title,
|
|
505
|
+
args: [inside ? "new-window" : "new-session", "-d", "-P", "-F", "#{window_id}",
|
|
506
|
+
...(inside ? [] : ["-s", session]), "-n", title, "-c", env.cwd,
|
|
507
|
+
env.nodePath, env.bootstrapPath, "--descriptor", descriptorBase64Url],
|
|
508
|
+
...(inside ? {} : { tmuxSession: session }),
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
if (t.backend === "wt") {
|
|
513
|
+
return {
|
|
514
|
+
backend: "wt",
|
|
515
|
+
command: t.command,
|
|
516
|
+
args: buildWtArgs(title, env.cwd, env.nodePath, env.bootstrapPath, descriptorBase64Url),
|
|
517
|
+
cwd: env.cwd,
|
|
518
|
+
title,
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (t.backend === "powershell") {
|
|
523
|
+
const safeTitle = title.replace(/[\r\n]+/g, " ");
|
|
524
|
+
// Start-Process 的 ArgumentList 会被拼成字符串交给子进程,必须做 Windows 引号处理
|
|
525
|
+
const argLine =
|
|
526
|
+
`${windowsQuoted(env.bootstrapPath)} --descriptor ${descriptorBase64Url}` +
|
|
527
|
+
(safeTitle ? ` --title ${windowsQuoted(safeTitle)}` : "");
|
|
528
|
+
const script =
|
|
529
|
+
`Start-Process -FilePath ${psQuote(env.nodePath)} ` +
|
|
530
|
+
`-WorkingDirectory ${psQuote(env.cwd)} ` +
|
|
531
|
+
`-WindowStyle Normal -ErrorAction Stop ` +
|
|
532
|
+
`-ArgumentList ${psQuote(argLine)}`;
|
|
533
|
+
return {
|
|
534
|
+
backend: "powershell",
|
|
535
|
+
command: t.command,
|
|
536
|
+
args: [
|
|
537
|
+
"-NoProfile",
|
|
538
|
+
"-NonInteractive",
|
|
539
|
+
"-ExecutionPolicy",
|
|
540
|
+
"Bypass",
|
|
541
|
+
"-EncodedCommand",
|
|
542
|
+
Buffer.from(script, "utf16le").toString("base64"),
|
|
543
|
+
],
|
|
544
|
+
cwd: env.cwd,
|
|
545
|
+
title,
|
|
546
|
+
windowsHide: true,
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// cmd 后端:固定命令串 + 受控环境变量;拒绝双引号/CR/LF/NUL,不做 %% / ^ 转义
|
|
551
|
+
assertCmdSafeValues([
|
|
552
|
+
["title", title],
|
|
553
|
+
["cwd", env.cwd],
|
|
554
|
+
["nodePath", env.nodePath],
|
|
555
|
+
["bootstrapPath", env.bootstrapPath],
|
|
556
|
+
]);
|
|
557
|
+
const command =
|
|
558
|
+
`start "%ENV_TITLE%" /D "%ENV_CWD%" "%ENV_NODE%" "%ENV_BOOT%" --descriptor %ENV_DESC%`;
|
|
559
|
+
return {
|
|
560
|
+
backend: "cmd",
|
|
561
|
+
command: t.command,
|
|
562
|
+
args: ["/d", "/v:off", "/s", "/c", command],
|
|
563
|
+
cwd: env.cwd,
|
|
564
|
+
title,
|
|
565
|
+
extraEnv: {
|
|
566
|
+
ENV_TITLE: title,
|
|
567
|
+
ENV_CWD: env.cwd,
|
|
568
|
+
ENV_NODE: env.nodePath,
|
|
569
|
+
ENV_BOOT: env.bootstrapPath,
|
|
570
|
+
ENV_DESC: descriptorBase64Url,
|
|
571
|
+
},
|
|
572
|
+
windowsVerbatimArguments: true,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 启动终端窗口。spawnFn 可注入,单元测试不实际开窗。
|
|
578
|
+
*/
|
|
579
|
+
export function launchWorkerWindow(
|
|
580
|
+
spec: TerminalLaunchSpec,
|
|
581
|
+
options: { spawnFn?: (command: string, args: string[], options: SpawnOptions) => ChildProcess } = {},
|
|
582
|
+
): ChildProcess {
|
|
583
|
+
const spawnFn = options.spawnFn ?? spawn;
|
|
584
|
+
return spawnFn(spec.command, spec.args, {
|
|
585
|
+
shell: false,
|
|
586
|
+
windowsHide: spec.windowsHide ?? false,
|
|
587
|
+
windowsVerbatimArguments: spec.windowsVerbatimArguments ?? false,
|
|
588
|
+
stdio: "ignore",
|
|
589
|
+
cwd: spec.cwd,
|
|
590
|
+
env: spec.extraEnv ? { ...process.env, ...spec.extraEnv } : undefined,
|
|
591
|
+
});
|
|
592
|
+
}
|