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.
Files changed (51) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +365 -0
  3. package/docs/decisions/0001-ilink-context-token-queue.md +41 -0
  4. package/docs/handoff-next-session.md +134 -0
  5. package/docs/implementation-spec.md +147 -0
  6. package/extensions/wechat-session-hub.ts +589 -0
  7. package/package.json +67 -0
  8. package/scripts/pi-rpc-smoke.mjs +150 -0
  9. package/scripts/probe-wt-capability.mjs +27 -0
  10. package/scripts/start-ui-worker.ps1 +37 -0
  11. package/src/commands/ilink-qr-login-component.ts +148 -0
  12. package/src/commands/project-command-parser.ts +108 -0
  13. package/src/commands/tokenizer.ts +71 -0
  14. package/src/commands/wechat-manager-command-parser.ts +143 -0
  15. package/src/config/defaults.ts +30 -0
  16. package/src/config/paths.ts +56 -0
  17. package/src/config/store.ts +311 -0
  18. package/src/config/types.ts +33 -0
  19. package/src/config/validation.ts +221 -0
  20. package/src/gateway/channel-types.ts +47 -0
  21. package/src/gateway/command-parser.ts +291 -0
  22. package/src/gateway/command-router.ts +1449 -0
  23. package/src/gateway/credential-store.ts +611 -0
  24. package/src/gateway/durable-message-queue.ts +1043 -0
  25. package/src/gateway/gateway-control-ipc.ts +687 -0
  26. package/src/gateway/gateway-runtime.ts +726 -0
  27. package/src/gateway/ui-worker-extension-bridge.ts +1070 -0
  28. package/src/gateway/ui-worker-ipc.ts +1483 -0
  29. package/src/gateway/ui-worker-types.ts +50 -0
  30. package/src/gateway/wechat-ilink/api-client.ts +390 -0
  31. package/src/gateway/wechat-ilink/channel-adapter.ts +320 -0
  32. package/src/gateway/wechat-ilink/cursor-store.ts +134 -0
  33. package/src/gateway/wechat-ilink/qr-login.ts +612 -0
  34. package/src/gateway/wechat-ilink/typing-indicator.ts +133 -0
  35. package/src/gateway/windows-terminal-launcher.ts +247 -0
  36. package/src/projects/project-service.ts +483 -0
  37. package/src/registry/gateway-lease.ts +348 -0
  38. package/src/registry/manager-registry.ts +466 -0
  39. package/src/registry/types.ts +61 -0
  40. package/src/runtime/background-pi-worker.ts +309 -0
  41. package/src/runtime/background-worker-manager.ts +594 -0
  42. package/src/runtime/gateway-daemon.ts +281 -0
  43. package/src/runtime/gateway-lifecycle-operation.ts +308 -0
  44. package/src/runtime/gateway-service-controller.ts +339 -0
  45. package/src/runtime/gateway-worker-manager.ts +181 -0
  46. package/src/runtime/pi-cli.ts +56 -0
  47. package/src/runtime/pi-rpc-client.ts +427 -0
  48. package/src/runtime/ui-worker-manager.ts +1186 -0
  49. package/src/runtime/windows-logon-task-manager.ts +212 -0
  50. package/src/system/process-liveness.ts +88 -0
  51. package/tsconfig.json +19 -0
@@ -0,0 +1,291 @@
1
+ import { tokenizeCommandArguments, CommandTokenizeError } from "../commands/tokenizer.ts";
2
+ import type { RuntimeMode } from "../config/types.ts";
3
+
4
+ export type GatewayCommand =
5
+ | { kind: "help" }
6
+ | { kind: "status" }
7
+ | { kind: "projects"; arguments: string }
8
+ | { kind: "new"; mode?: RuntimeMode; projectAlias?: string; name?: string }
9
+ | { kind: "sessions"; projectAlias?: string; state: "default" | "active" | "saved" | "pending" | "all"; page: number }
10
+ | { kind: "resume"; selector: string }
11
+ | { kind: "last"; selector?: string }
12
+ | { kind: "stop"; selector?: string }
13
+ | { kind: "retry"; messageId: string; confirmed: boolean }
14
+ | { kind: "prompt"; text: string };
15
+
16
+ /**
17
+ * 表示 Gateway slash command 参数非法。
18
+ */
19
+ export class GatewayCommandParseError extends Error {
20
+ /**
21
+ * 创建 command parse 错误。
22
+ *
23
+ * @param message 面向用户的中文说明。
24
+ */
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = "GatewayCommandParseError";
28
+ }
29
+ }
30
+
31
+ /**
32
+ * 将入站文本解析为控制命令或普通 prompt。
33
+ *
34
+ * @param text 微信消息正文。
35
+ * @returns 规范化命令对象。
36
+ * @throws {GatewayCommandParseError} 已知命令的参数不合法时抛出。
37
+ */
38
+ export function parseGatewayCommand(text: string): GatewayCommand {
39
+ const trimmed = text.trim();
40
+ if (!trimmed.startsWith("/")) {
41
+ return { kind: "prompt", text };
42
+ }
43
+
44
+ let tokens: string[];
45
+ try {
46
+ tokens = tokenizeCommandArguments(trimmed);
47
+ } catch (error) {
48
+ if (error instanceof CommandTokenizeError) {
49
+ throw new GatewayCommandParseError(error.message);
50
+ }
51
+ throw error;
52
+ }
53
+ if (tokens.length === 0) {
54
+ throw usageError();
55
+ }
56
+
57
+ const commandName = tokens[0].slice(1).toLowerCase();
58
+ const arguments_ = tokens.slice(1);
59
+ if (commandName === "help") {
60
+ ensureNoArguments(arguments_);
61
+ return { kind: "help" };
62
+ }
63
+ if (commandName === "status") {
64
+ ensureNoArguments(arguments_);
65
+ return { kind: "status" };
66
+ }
67
+ if (commandName === "projects") {
68
+ return {
69
+ kind: "projects",
70
+ arguments: extractRawArguments(trimmed),
71
+ };
72
+ }
73
+ if (commandName === "new") {
74
+ return parseNewCommand(arguments_);
75
+ }
76
+ if (commandName === "sessions") {
77
+ return parseSessionsCommand(arguments_);
78
+ }
79
+ if (commandName === "resume") {
80
+ return parseResumeCommand(arguments_);
81
+ }
82
+ if (commandName === "last") {
83
+ return parseLastCommand(arguments_);
84
+ }
85
+ if (commandName === "stop") {
86
+ if (arguments_.length > 1 || (arguments_.length === 1 && !arguments_[0].trim())) {
87
+ throw usageError();
88
+ }
89
+ return {
90
+ kind: "stop",
91
+ ...(arguments_[0] ? { selector: arguments_[0] } : {}),
92
+ };
93
+ }
94
+ if (commandName === "retry") {
95
+ if (
96
+ arguments_.length < 1 ||
97
+ arguments_.length > 2 ||
98
+ !arguments_[0].trim() ||
99
+ (arguments_[1] !== undefined && arguments_[1] !== "--confirm")
100
+ ) {
101
+ throw usageError();
102
+ }
103
+ return {
104
+ kind: "retry",
105
+ messageId: arguments_[0],
106
+ confirmed: arguments_[1] === "--confirm",
107
+ };
108
+ }
109
+
110
+ throw new GatewayCommandParseError(
111
+ `未知命令: /${commandName}。发送 /help 查看支持的命令。`,
112
+ );
113
+ }
114
+
115
+ /**
116
+ * 解析 /new 参数。
117
+ *
118
+ * @param arguments_ 去掉命令名后的 token 列表。
119
+ * @returns 新建 session 参数。
120
+ */
121
+ function parseNewCommand(arguments_: string[]): GatewayCommand {
122
+ const options = readOptions(arguments_, ["mode", "project", "name"]);
123
+ const mode = parseMode(options.get("mode"));
124
+ const projectAlias = options.get("project");
125
+ const name = options.get("name");
126
+ if (projectAlias !== undefined && !projectAlias.trim()) {
127
+ throw usageError();
128
+ }
129
+ if (name !== undefined && (!name.trim() || name.length > 100)) {
130
+ throw new GatewayCommandParseError("session name 需为 1-100 个字符。");
131
+ }
132
+ return {
133
+ kind: "new",
134
+ ...(mode ? { mode } : {}),
135
+ ...(projectAlias ? { projectAlias } : {}),
136
+ ...(name ? { name } : {}),
137
+ };
138
+ }
139
+
140
+ /**
141
+ * 解析 /sessions 参数。
142
+ *
143
+ * @param arguments_ 去掉命令名后的 token 列表。
144
+ * @returns session 列表过滤参数。
145
+ */
146
+ function parseSessionsCommand(arguments_: string[]): GatewayCommand {
147
+ const options = readOptions(arguments_, ["project", "state", "page"]);
148
+ const projectAlias = options.get("project");
149
+ const state = options.get("state") ?? "active";
150
+ const pageText = options.get("page") ?? "1";
151
+ const page = Number(pageText);
152
+ if (!Number.isInteger(page) || page < 1) {
153
+ throw new GatewayCommandParseError("page 必须是大于 0 的整数。");
154
+ }
155
+ if (
156
+ state !== "default" &&
157
+ state !== "active" &&
158
+ state !== "saved" &&
159
+ state !== "pending" &&
160
+ state !== "all"
161
+ ) {
162
+ throw new GatewayCommandParseError("state 只能是 default、active、saved、pending 或 all。");
163
+ }
164
+ return {
165
+ kind: "sessions",
166
+ ...(projectAlias ? { projectAlias } : {}),
167
+ state,
168
+ page,
169
+ };
170
+ }
171
+
172
+ /**
173
+ * 解析 /resume 参数。
174
+ *
175
+ * @param arguments_ 去掉命令名后的 token 列表。
176
+ * @returns 恢复 session 参数。
177
+ */
178
+ function parseResumeCommand(arguments_: string[]): GatewayCommand {
179
+ if (arguments_.length !== 1 || !arguments_[0].trim()) {
180
+ throw usageError();
181
+ }
182
+ return {
183
+ kind: "resume",
184
+ selector: arguments_[0],
185
+ };
186
+ }
187
+
188
+ /**
189
+ * 解析 /last 参数。
190
+ *
191
+ * @param arguments_ 去掉命令名后的 token 列表。
192
+ * @returns 只读查询最近一轮对话的参数。
193
+ */
194
+ function parseLastCommand(arguments_: string[]): GatewayCommand {
195
+ if (arguments_.length > 1 || (arguments_.length === 1 && !arguments_[0].trim())) {
196
+ throw usageError();
197
+ }
198
+ return {
199
+ kind: "last",
200
+ ...(arguments_[0] ? { selector: arguments_[0] } : {}),
201
+ };
202
+ }
203
+
204
+ /**
205
+ * 解析新会话运行模式。
206
+ *
207
+ * @param value 模式参数。
208
+ * @returns 已指定的运行模式, 未指定时返回 undefined。
209
+ * @throws {GatewayCommandParseError} 模式不是 background 或 ui 时抛出。
210
+ */
211
+ function parseMode(value: string | undefined): RuntimeMode | undefined {
212
+ if (value === undefined) {
213
+ return undefined;
214
+ }
215
+ if (value !== "background" && value !== "ui") {
216
+ throw new GatewayCommandParseError("mode 只能是 background 或 ui。");
217
+ }
218
+ return value;
219
+ }
220
+
221
+ /**
222
+ * 解析指定白名单内的 long options。
223
+ *
224
+ * @param tokens 参数 token 列表。
225
+ * @param allowed 允许选项名。
226
+ * @returns 选项和值映射。
227
+ * @throws {GatewayCommandParseError} 参数格式非法时抛出。
228
+ */
229
+ function readOptions(tokens: string[], allowed: string[]): Map<string, string> {
230
+ const options = new Map<string, string>();
231
+ for (let index = 0; index < tokens.length; index += 1) {
232
+ const token = tokens[index];
233
+ if (!token.startsWith("--")) {
234
+ throw usageError();
235
+ }
236
+ const equalsIndex = token.indexOf("=");
237
+ const key = equalsIndex >= 0 ? token.slice(2, equalsIndex) : token.slice(2);
238
+ const inlineValue = equalsIndex >= 0 ? token.slice(equalsIndex + 1) : undefined;
239
+ if (!allowed.includes(key)) {
240
+ throw new GatewayCommandParseError(`未知参数: --${key}。\n${GATEWAY_COMMAND_USAGE}`);
241
+ }
242
+ if (options.has(key)) {
243
+ throw new GatewayCommandParseError(`参数 --${key} 不能重复。\n${GATEWAY_COMMAND_USAGE}`);
244
+ }
245
+ const value = inlineValue ?? tokens[index + 1];
246
+ if (value === undefined || (inlineValue === undefined && value.startsWith("--"))) {
247
+ throw usageError();
248
+ }
249
+ options.set(key, value);
250
+ if (inlineValue === undefined) {
251
+ index += 1;
252
+ }
253
+ }
254
+ return options;
255
+ }
256
+
257
+ /**
258
+ * 检查命令不包含额外参数。
259
+ *
260
+ * @param arguments_ 去掉命令名后的 token 列表。
261
+ * @returns 无返回值。
262
+ * @throws {GatewayCommandParseError} 存在额外参数时抛出。
263
+ */
264
+ function ensureNoArguments(arguments_: string[]): void {
265
+ if (arguments_.length > 0) {
266
+ throw usageError();
267
+ }
268
+ }
269
+
270
+ /**
271
+ * 获取 /projects 后的原始文本, 保留路径参数的原始引号。
272
+ *
273
+ * @param input 完整命令文本。
274
+ * @returns /projects 后的原始参数。
275
+ */
276
+ function extractRawArguments(input: string): string {
277
+ const match = input.match(/^\s*\/projects(?:\s+([\s\S]*))?$/i);
278
+ return match?.[1]?.trim() ?? "";
279
+ }
280
+
281
+ const GATEWAY_COMMAND_USAGE =
282
+ "用法: /new [--mode background|ui] [--project <alias>] [--name <名称>] | /sessions [--project <alias>] [--state active|saved|pending|all] [--page <n>] | /resume <会话索引|名称> | /last [会话索引|名称] | /stop [会话索引|名称] | /retry <message-id> --confirm | /status | /projects | /help";
283
+
284
+ /**
285
+ * 创建统一格式的 command usage 错误。
286
+ *
287
+ * @returns Gateway command parse 错误。
288
+ */
289
+ function usageError(): GatewayCommandParseError {
290
+ return new GatewayCommandParseError(GATEWAY_COMMAND_USAGE);
291
+ }