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,427 @@
|
|
|
1
|
+
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import { EventEmitter, once } from "node:events";
|
|
3
|
+
import type { PiCliInvocation } from "./pi-cli.ts";
|
|
4
|
+
|
|
5
|
+
export interface PiRpcRecord {
|
|
6
|
+
type: string;
|
|
7
|
+
[key: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface PiRpcResponse extends PiRpcRecord {
|
|
11
|
+
type: "response";
|
|
12
|
+
id?: string;
|
|
13
|
+
command: string;
|
|
14
|
+
success: boolean;
|
|
15
|
+
data?: unknown;
|
|
16
|
+
error?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PiRpcClientOptions {
|
|
20
|
+
invocation: PiCliInvocation;
|
|
21
|
+
args?: string[];
|
|
22
|
+
cwd: string;
|
|
23
|
+
env?: NodeJS.ProcessEnv;
|
|
24
|
+
requestTimeoutMs?: number;
|
|
25
|
+
maxRecordBytes?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PendingRequest {
|
|
29
|
+
command: string;
|
|
30
|
+
resolve: (response: PiRpcResponse) => void;
|
|
31
|
+
reject: (error: Error) => void;
|
|
32
|
+
timeout: NodeJS.Timeout;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 表示 Pi RPC 命令被拒绝或后台进程退出。
|
|
37
|
+
*/
|
|
38
|
+
export class PiRpcError extends Error {
|
|
39
|
+
/**
|
|
40
|
+
* 创建 Pi RPC 错误。
|
|
41
|
+
*
|
|
42
|
+
* @param message 面向用户的中文错误信息。
|
|
43
|
+
* @param command 被拒绝的 RPC 命令。
|
|
44
|
+
*/
|
|
45
|
+
constructor(
|
|
46
|
+
message: string,
|
|
47
|
+
readonly command?: string,
|
|
48
|
+
) {
|
|
49
|
+
super(message);
|
|
50
|
+
this.name = "PiRpcError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 通过 LF 分帧的 JSONL stdin/stdout 协议控制 Pi RPC 子进程。
|
|
56
|
+
*/
|
|
57
|
+
export class PiRpcClient extends EventEmitter {
|
|
58
|
+
readonly pid: number | undefined;
|
|
59
|
+
private readonly child: ChildProcessWithoutNullStreams;
|
|
60
|
+
private readonly requestTimeoutMs: number;
|
|
61
|
+
private readonly maxRecordBytes: number;
|
|
62
|
+
private readonly pending = new Map<string, PendingRequest>();
|
|
63
|
+
private readonly closed: Promise<{ code: number | null; signal: NodeJS.Signals | null }>;
|
|
64
|
+
private resolveClosed!: (result: { code: number | null; signal: NodeJS.Signals | null }) => void;
|
|
65
|
+
private stdoutBuffer = "";
|
|
66
|
+
private requestSequence = 0;
|
|
67
|
+
private closedResult: { code: number | null; signal: NodeJS.Signals | null } | undefined;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 创建 RPC 客户端并接管子进程输出流。
|
|
71
|
+
*
|
|
72
|
+
* @param child 已启动的 Pi 子进程。
|
|
73
|
+
* @param requestTimeoutMs 单条命令响应超时。
|
|
74
|
+
* @param maxRecordBytes 单条 JSONL 最大字节数。
|
|
75
|
+
*/
|
|
76
|
+
private constructor(
|
|
77
|
+
child: ChildProcessWithoutNullStreams,
|
|
78
|
+
requestTimeoutMs: number,
|
|
79
|
+
maxRecordBytes: number,
|
|
80
|
+
) {
|
|
81
|
+
super();
|
|
82
|
+
this.child = child;
|
|
83
|
+
this.pid = child.pid;
|
|
84
|
+
this.requestTimeoutMs = requestTimeoutMs;
|
|
85
|
+
this.maxRecordBytes = maxRecordBytes;
|
|
86
|
+
this.closed = new Promise((resolveClosed) => {
|
|
87
|
+
this.resolveClosed = resolveClosed;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
child.stdout.setEncoding("utf8");
|
|
91
|
+
child.stderr.setEncoding("utf8");
|
|
92
|
+
child.stdout.on("data", (chunk: string) => this.consumeStdout(chunk));
|
|
93
|
+
child.stderr.on("data", () => undefined);
|
|
94
|
+
child.on("error", (error) => this.rejectPending(new PiRpcError(`Pi RPC 子进程错误: ${error.message}`)));
|
|
95
|
+
child.on("close", (code, signal) => {
|
|
96
|
+
this.closedResult = { code, signal };
|
|
97
|
+
this.resolveClosed(this.closedResult);
|
|
98
|
+
this.rejectPending(
|
|
99
|
+
new PiRpcError(`Pi RPC 子进程已退出, code=${code ?? "null"}, signal=${signal ?? "null"}`),
|
|
100
|
+
);
|
|
101
|
+
this.emit("process_exit", this.closedResult);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 启动 Pi RPC 子进程。
|
|
107
|
+
*
|
|
108
|
+
* @param options 入口、工作目录和环境变量。
|
|
109
|
+
* @returns 已完成 spawn 的 RPC 客户端。
|
|
110
|
+
*/
|
|
111
|
+
static async start(options: PiRpcClientOptions): Promise<PiRpcClient> {
|
|
112
|
+
const child = spawn(
|
|
113
|
+
options.invocation.command,
|
|
114
|
+
[...options.invocation.args, "--mode", "rpc", ...(options.args ?? [])],
|
|
115
|
+
{
|
|
116
|
+
cwd: options.cwd,
|
|
117
|
+
env: options.env ?? process.env,
|
|
118
|
+
shell: false,
|
|
119
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
120
|
+
windowsHide: true,
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
const client = new PiRpcClient(
|
|
124
|
+
child,
|
|
125
|
+
options.requestTimeoutMs ?? 10000,
|
|
126
|
+
options.maxRecordBytes ?? 16 * 1024 * 1024,
|
|
127
|
+
);
|
|
128
|
+
await new Promise<void>((resolveSpawn, rejectSpawn) => {
|
|
129
|
+
child.once("spawn", () => resolveSpawn());
|
|
130
|
+
child.once("error", (error) => rejectSpawn(new PiRpcError(`无法启动 Pi RPC 子进程: ${error.message}`)));
|
|
131
|
+
});
|
|
132
|
+
return client;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 获取 RPC 子进程是否已退出。
|
|
137
|
+
*
|
|
138
|
+
* @returns 已收到 close 事件时返回 true。
|
|
139
|
+
*/
|
|
140
|
+
get isClosed(): boolean {
|
|
141
|
+
return this.closedResult !== undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 发送 RPC 命令并等待对应 response。
|
|
146
|
+
*
|
|
147
|
+
* @param command 不含协议 ID 的命令对象。
|
|
148
|
+
* @param timeoutMs 可选的单次超时覆盖。
|
|
149
|
+
* @returns RPC response。
|
|
150
|
+
* @throws {PiRpcError} 命令失败、超时或进程已退出时抛出。
|
|
151
|
+
*/
|
|
152
|
+
async request(
|
|
153
|
+
command: Record<string, unknown> & { type: string },
|
|
154
|
+
timeoutMs = this.requestTimeoutMs,
|
|
155
|
+
): Promise<PiRpcResponse> {
|
|
156
|
+
if (this.closedResult) {
|
|
157
|
+
throw new PiRpcError("Pi RPC 子进程已退出, 无法发送命令。");
|
|
158
|
+
}
|
|
159
|
+
const id = typeof command.id === "string"
|
|
160
|
+
? command.id
|
|
161
|
+
: `pi-rpc-${++this.requestSequence}`;
|
|
162
|
+
const outgoing = { ...command, id };
|
|
163
|
+
if (this.pending.has(id)) {
|
|
164
|
+
throw new PiRpcError(`Pi RPC 请求 ID 已在使用: ${id}`, command.type);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const responsePromise = new Promise<PiRpcResponse>((resolveResponse, rejectResponse) => {
|
|
168
|
+
const timeout = setTimeout(() => {
|
|
169
|
+
this.pending.delete(id);
|
|
170
|
+
rejectResponse(new PiRpcError(`Pi RPC 命令响应超时: ${command.type}`, command.type));
|
|
171
|
+
}, timeoutMs);
|
|
172
|
+
timeout.unref();
|
|
173
|
+
this.pending.set(id, {
|
|
174
|
+
command: command.type,
|
|
175
|
+
resolve: resolveResponse,
|
|
176
|
+
reject: rejectResponse,
|
|
177
|
+
timeout,
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
await this.writeRecord(outgoing);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
const pending = this.pending.get(id);
|
|
185
|
+
if (pending) {
|
|
186
|
+
clearTimeout(pending.timeout);
|
|
187
|
+
this.pending.delete(id);
|
|
188
|
+
pending.reject(error instanceof Error ? error : new PiRpcError("Pi RPC 写入失败。"));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return responsePromise;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 订阅下一条指定类型的 Pi RPC 事件。
|
|
196
|
+
*
|
|
197
|
+
* @param eventType 事件 type。
|
|
198
|
+
* @param timeoutMs 等待超时。
|
|
199
|
+
* @param signal 用于取消等待的 AbortSignal。
|
|
200
|
+
* @returns 匹配事件记录。
|
|
201
|
+
*/
|
|
202
|
+
waitForEvent<T extends PiRpcRecord>(
|
|
203
|
+
eventType: string,
|
|
204
|
+
timeoutMs: number,
|
|
205
|
+
signal?: AbortSignal,
|
|
206
|
+
): Promise<T> {
|
|
207
|
+
return new Promise<T>((resolveEvent, rejectEvent) => {
|
|
208
|
+
let settled = false;
|
|
209
|
+
const cleanup = (): void => {
|
|
210
|
+
clearTimeout(timeout);
|
|
211
|
+
this.off(eventType, onEvent);
|
|
212
|
+
signal?.removeEventListener("abort", onAbort);
|
|
213
|
+
};
|
|
214
|
+
const finish = (action: () => void): void => {
|
|
215
|
+
if (settled) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
settled = true;
|
|
219
|
+
cleanup();
|
|
220
|
+
action();
|
|
221
|
+
};
|
|
222
|
+
const onEvent = (event: T): void => finish(() => resolveEvent(event));
|
|
223
|
+
const onAbort = (): void => finish(() => rejectEvent(new PiRpcError("等待 Pi RPC 事件已取消。")));
|
|
224
|
+
const timeout = setTimeout(
|
|
225
|
+
() => finish(() => rejectEvent(new PiRpcError(`等待 Pi RPC 事件超时: ${eventType}`))),
|
|
226
|
+
timeoutMs,
|
|
227
|
+
);
|
|
228
|
+
timeout.unref();
|
|
229
|
+
if (signal?.aborted) {
|
|
230
|
+
onAbort();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.on(eventType, onEvent);
|
|
234
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 请求 Pi 关闭并等待子进程退出, 超时不会自动强杀。
|
|
240
|
+
*
|
|
241
|
+
* @param timeoutMs 优雅退出等待时长。
|
|
242
|
+
* @returns 无返回值。
|
|
243
|
+
* @throws {PiRpcError} 子进程未在期限内退出时抛出。
|
|
244
|
+
*/
|
|
245
|
+
async shutdown(timeoutMs = 5000): Promise<void> {
|
|
246
|
+
if (this.closedResult) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this.child.stdin.end();
|
|
250
|
+
let timeout: NodeJS.Timeout | undefined;
|
|
251
|
+
try {
|
|
252
|
+
await Promise.race([
|
|
253
|
+
this.closed,
|
|
254
|
+
new Promise<never>((_resolveTimeout, rejectTimeout) => {
|
|
255
|
+
timeout = setTimeout(
|
|
256
|
+
() => rejectTimeout(new PiRpcError("Pi RPC 子进程未能优雅退出; 未执行强制终止。")),
|
|
257
|
+
timeoutMs,
|
|
258
|
+
);
|
|
259
|
+
timeout.unref();
|
|
260
|
+
}),
|
|
261
|
+
]);
|
|
262
|
+
} finally {
|
|
263
|
+
if (timeout) {
|
|
264
|
+
clearTimeout(timeout);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 写入一条 JSONL 命令并遵守 stdin backpressure。
|
|
271
|
+
*
|
|
272
|
+
* @param record 要发送的 JSON 对象。
|
|
273
|
+
* @returns 无返回值。
|
|
274
|
+
*/
|
|
275
|
+
private async writeRecord(record: Record<string, unknown>): Promise<void> {
|
|
276
|
+
if (!this.child.stdin.writable || this.closedResult) {
|
|
277
|
+
throw new PiRpcError("Pi RPC stdin 已关闭。");
|
|
278
|
+
}
|
|
279
|
+
const line = `${JSON.stringify(record)}\n`;
|
|
280
|
+
if (Buffer.byteLength(line, "utf8") > this.maxRecordBytes) {
|
|
281
|
+
throw new PiRpcError("Pi RPC 命令超过允许的记录大小。");
|
|
282
|
+
}
|
|
283
|
+
if (!this.child.stdin.write(line, "utf8")) {
|
|
284
|
+
await Promise.race([
|
|
285
|
+
once(this.child.stdin, "drain").then(() => undefined),
|
|
286
|
+
this.closed.then(() => {
|
|
287
|
+
throw new PiRpcError("Pi RPC 子进程在接收命令期间退出。");
|
|
288
|
+
}),
|
|
289
|
+
]);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 按 LF 拆分 stdout JSONL, 不把 Unicode 行分隔符当成记录边界。
|
|
295
|
+
*
|
|
296
|
+
* @param chunk stdout 新到达的 UTF-8 文本。
|
|
297
|
+
* @returns 无返回值。
|
|
298
|
+
*/
|
|
299
|
+
private consumeStdout(chunk: string): void {
|
|
300
|
+
this.stdoutBuffer += chunk;
|
|
301
|
+
if (Buffer.byteLength(this.stdoutBuffer, "utf8") > this.maxRecordBytes && !this.stdoutBuffer.includes("\n")) {
|
|
302
|
+
this.rejectPending(new PiRpcError("Pi RPC stdout 单条记录超过允许大小。"));
|
|
303
|
+
this.child.kill();
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let newlineIndex = this.stdoutBuffer.indexOf("\n");
|
|
308
|
+
while (newlineIndex >= 0) {
|
|
309
|
+
let line = this.stdoutBuffer.slice(0, newlineIndex);
|
|
310
|
+
this.stdoutBuffer = this.stdoutBuffer.slice(newlineIndex + 1);
|
|
311
|
+
if (line.endsWith("\r")) {
|
|
312
|
+
line = line.slice(0, -1);
|
|
313
|
+
}
|
|
314
|
+
if (Buffer.byteLength(line, "utf8") > this.maxRecordBytes) {
|
|
315
|
+
this.rejectPending(new PiRpcError("Pi RPC stdout 单条记录超过允许大小。"));
|
|
316
|
+
this.child.kill();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (line) {
|
|
320
|
+
this.consumeLine(line);
|
|
321
|
+
}
|
|
322
|
+
if (Buffer.byteLength(this.stdoutBuffer, "utf8") > this.maxRecordBytes) {
|
|
323
|
+
this.rejectPending(new PiRpcError("Pi RPC stdout 单条记录超过允许大小。"));
|
|
324
|
+
this.child.kill();
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
newlineIndex = this.stdoutBuffer.indexOf("\n");
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 解析并分派一条完整 JSONL 记录。
|
|
333
|
+
*
|
|
334
|
+
* @param line JSONL 文本记录。
|
|
335
|
+
* @returns 无返回值。
|
|
336
|
+
*/
|
|
337
|
+
private consumeLine(line: string): void {
|
|
338
|
+
let record: PiRpcRecord;
|
|
339
|
+
try {
|
|
340
|
+
const parsed: unknown = JSON.parse(line);
|
|
341
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
342
|
+
throw new Error("not_object");
|
|
343
|
+
}
|
|
344
|
+
record = parsed as PiRpcRecord;
|
|
345
|
+
if (typeof record.type !== "string") {
|
|
346
|
+
throw new Error("missing_type");
|
|
347
|
+
}
|
|
348
|
+
} catch {
|
|
349
|
+
this.rejectPending(new PiRpcError("Pi RPC stdout 包含无效 JSONL 记录。"));
|
|
350
|
+
this.child.kill();
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (record.type === "response" && typeof record.id === "string") {
|
|
355
|
+
this.consumeResponse(record as PiRpcResponse);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (record.type === "extension_ui_request") {
|
|
359
|
+
this.handleExtensionUiRequest(record);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
this.emit(record.type, record);
|
|
363
|
+
this.emit("rpc_record", record);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 完成与 response ID 匹配的待处理命令。
|
|
368
|
+
*
|
|
369
|
+
* @param response Pi RPC response。
|
|
370
|
+
* @returns 无返回值。
|
|
371
|
+
*/
|
|
372
|
+
private consumeResponse(response: PiRpcResponse): void {
|
|
373
|
+
const pending = this.pending.get(response.id ?? "");
|
|
374
|
+
if (!pending) {
|
|
375
|
+
this.emit("orphan_response", response);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
clearTimeout(pending.timeout);
|
|
379
|
+
this.pending.delete(response.id ?? "");
|
|
380
|
+
if (!response.success) {
|
|
381
|
+
pending.reject(new PiRpcError(response.error ?? `Pi RPC 命令失败: ${pending.command}`, pending.command));
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
pending.resolve(response);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 对无交互界面的后台扩展请求 fail-closed, 取消所有 dialog。
|
|
389
|
+
*
|
|
390
|
+
* @param request 扩展 UI 请求记录。
|
|
391
|
+
* @returns 无返回值。
|
|
392
|
+
*/
|
|
393
|
+
private handleExtensionUiRequest(request: PiRpcRecord): void {
|
|
394
|
+
const method = request.method;
|
|
395
|
+
const id = request.id;
|
|
396
|
+
if (typeof method !== "string" || typeof id !== "string") {
|
|
397
|
+
this.emit("extension_ui_notice", request);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (!["select", "confirm", "input", "editor"].includes(method)) {
|
|
401
|
+
this.emit("extension_ui_notice", request);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const response = method === "confirm"
|
|
406
|
+
? { type: "extension_ui_response", id, confirmed: false }
|
|
407
|
+
: { type: "extension_ui_response", id, cancelled: true };
|
|
408
|
+
void this.writeRecord(response).catch((error: unknown) => {
|
|
409
|
+
this.rejectPending(error instanceof Error ? error : new PiRpcError("无法取消扩展 UI 请求。"));
|
|
410
|
+
});
|
|
411
|
+
this.emit("extension_ui_cancelled", { method, id });
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* 在进程退出或协议错误时拒绝全部待处理命令。
|
|
416
|
+
*
|
|
417
|
+
* @param error 终止原因。
|
|
418
|
+
* @returns 无返回值。
|
|
419
|
+
*/
|
|
420
|
+
private rejectPending(error: Error): void {
|
|
421
|
+
for (const [id, pending] of this.pending) {
|
|
422
|
+
clearTimeout(pending.timeout);
|
|
423
|
+
pending.reject(error);
|
|
424
|
+
this.pending.delete(id);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|