pi-wechat-session-hub 0.1.4 → 0.1.5
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/CHANGELOG.md +43 -1
- package/README.md +56 -15
- package/docs/decisions/0002-notification-ingress-context-token.md +69 -0
- package/docs/handoff-next-session.md +6 -4
- package/docs/implementation-spec.md +42 -26
- package/extensions/wechat-session-hub.ts +3 -0
- package/package.json +7 -6
- package/scripts/wx-notify.mjs +266 -0
- package/src/commands/wechat-notify-tool.ts +188 -0
- package/src/config/defaults.ts +13 -1
- package/src/config/types.ts +17 -0
- package/src/config/validation.ts +29 -0
- package/src/gateway/command-parser.ts +5 -0
- package/src/gateway/command-router.ts +94 -9
- package/src/gateway/gateway-runtime.ts +41 -16
- package/src/gateway/notification/context-token-store.ts +186 -0
- package/src/gateway/notification/notification-ingress-server.ts +494 -0
- package/src/gateway/notification/notification-service.ts +526 -0
- package/src/gateway/ui-worker-extension-bridge.ts +976 -1092
- package/src/gateway/ui-worker-ipc.ts +1406 -1517
- package/src/runtime/background-worker-manager.ts +628 -594
- package/src/runtime/gateway-worker-manager.ts +51 -47
- package/src/runtime/ui-worker-manager.ts +1157 -1216
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Pi 微信会话枢纽通知入口 CLI。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node wx-notify.mjs "标题" "正文" [--source 标识] [--manager-dir <目录>] [--timeout <毫秒>]
|
|
7
|
+
*
|
|
8
|
+
* 通过本机 named pipe (Windows) 或 unix socket (POSIX) 向 Hub daemon
|
|
9
|
+
* 投递一条通知, 由 daemon 复用最新 context_token 直发微信。
|
|
10
|
+
* 零依赖, 可被任意程序 spawn。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createHmac, randomUUID } from "node:crypto";
|
|
14
|
+
import { createConnection } from "node:net";
|
|
15
|
+
import { readFile } from "node:fs/promises";
|
|
16
|
+
import { homedir, tmpdir } from "node:os";
|
|
17
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
18
|
+
import { createHash } from "node:crypto";
|
|
19
|
+
|
|
20
|
+
const DEFAULT_TIMEOUT_MS = 10000;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 解析命令行参数。
|
|
24
|
+
*
|
|
25
|
+
* @param argv 原始参数数组。
|
|
26
|
+
* @returns 解析结果, 非法时抛错。
|
|
27
|
+
*/
|
|
28
|
+
function parseArguments(argv) {
|
|
29
|
+
const positional = [];
|
|
30
|
+
let source;
|
|
31
|
+
let managerDirectory;
|
|
32
|
+
let timeoutMs = DEFAULT_TIMEOUT_MS;
|
|
33
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
34
|
+
const argument = argv[index];
|
|
35
|
+
if (argument === "--source") {
|
|
36
|
+
source = argv[index + 1];
|
|
37
|
+
if (!source) {
|
|
38
|
+
throw new Error("--source 需要一个值。");
|
|
39
|
+
}
|
|
40
|
+
index += 1;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (argument === "--manager-dir") {
|
|
44
|
+
managerDirectory = argv[index + 1];
|
|
45
|
+
if (!managerDirectory) {
|
|
46
|
+
throw new Error("--manager-dir 需要一个值。");
|
|
47
|
+
}
|
|
48
|
+
index += 1;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (argument === "--timeout") {
|
|
52
|
+
timeoutMs = Number(argv[index + 1]);
|
|
53
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0 || timeoutMs > 60000) {
|
|
54
|
+
throw new Error("--timeout 必须是 1-60000 之间的毫秒数。");
|
|
55
|
+
}
|
|
56
|
+
index += 1;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (argument === "--help" || argument === "-h") {
|
|
60
|
+
printUsageAndExit(0);
|
|
61
|
+
}
|
|
62
|
+
positional.push(argument);
|
|
63
|
+
}
|
|
64
|
+
if (positional.length < 1 || positional.length > 2) {
|
|
65
|
+
printUsageAndExit(1);
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
title: positional.length === 2 ? positional[0] : undefined,
|
|
69
|
+
text: positional[positional.length - 1],
|
|
70
|
+
source,
|
|
71
|
+
managerDirectory,
|
|
72
|
+
timeoutMs,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 打印用法并退出。
|
|
78
|
+
*
|
|
79
|
+
* @param code 退出码。
|
|
80
|
+
* @returns 无返回值。
|
|
81
|
+
*/
|
|
82
|
+
function printUsageAndExit(code) {
|
|
83
|
+
process.stdout.write([
|
|
84
|
+
"用法: node wx-notify.mjs [标题] <正文> [--source 标识] [--manager-dir <目录>] [--timeout <毫秒>]",
|
|
85
|
+
"",
|
|
86
|
+
"示例:",
|
|
87
|
+
' node wx-notify.mjs "构建完成" "beacon 项目编译通过, 耗时 2 分钟"',
|
|
88
|
+
' node wx-notify.mjs "CI 失败" "main 分支测试挂了" --source github-actions',
|
|
89
|
+
"",
|
|
90
|
+
"退出码: 0 成功; 非 0 失败 (原因见 stderr)。",
|
|
91
|
+
].join("\n"));
|
|
92
|
+
process.stdout.write("\n");
|
|
93
|
+
process.exit(code);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 推导 Hub manager 数据目录。
|
|
98
|
+
*
|
|
99
|
+
* @param override 显式指定值。
|
|
100
|
+
* @returns 绝对路径。
|
|
101
|
+
*/
|
|
102
|
+
function resolveManagerDirectory(override) {
|
|
103
|
+
if (override) {
|
|
104
|
+
return resolve(override);
|
|
105
|
+
}
|
|
106
|
+
const agentDirectory = process.env.PI_CODING_AGENT_DIR;
|
|
107
|
+
const base = agentDirectory?.trim()
|
|
108
|
+
? (isAbsolute(agentDirectory) ? agentDirectory : join(homedir(), agentDirectory))
|
|
109
|
+
: join(homedir(), ".pi", "agent");
|
|
110
|
+
return join(base, "wechat-session-manager");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 计算 named pipe / unix socket endpoint, 与服务端同一逻辑。
|
|
115
|
+
*
|
|
116
|
+
* @param managerDirectory manager 数据目录。
|
|
117
|
+
* @returns endpoint 文本。
|
|
118
|
+
*/
|
|
119
|
+
function createEndpoint(managerDirectory) {
|
|
120
|
+
const resolved = resolve(managerDirectory);
|
|
121
|
+
const normalized = process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
122
|
+
const scope = createHash("sha256").update(normalized).digest("hex").slice(0, 24);
|
|
123
|
+
if (process.platform === "win32") {
|
|
124
|
+
return `\\\\.\\pipe\\pi-wechat-session-hub-${scope}-notify`;
|
|
125
|
+
}
|
|
126
|
+
return join(tmpdir(), `pi-wechat-session-hub-${scope}-notify.sock`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 计算与服务端一致的 HMAC proof。
|
|
131
|
+
*
|
|
132
|
+
* @param key hex 共享密钥。
|
|
133
|
+
* @param requestId 请求 ID。
|
|
134
|
+
* @param timestamp 毫秒时间戳。
|
|
135
|
+
* @param payload 规范化 JSON 字符串。
|
|
136
|
+
* @returns base64url proof。
|
|
137
|
+
*/
|
|
138
|
+
function computeProof(key, requestId, timestamp, payload) {
|
|
139
|
+
return createHmac("sha256", Buffer.from(key, "hex"))
|
|
140
|
+
.update(`${requestId}\n${timestamp}\n`, "utf8")
|
|
141
|
+
.update(payload, "utf8")
|
|
142
|
+
.digest("base64url");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 连接 endpoint 并完成一次请求-响应。
|
|
147
|
+
*
|
|
148
|
+
* @param endpoint IPC endpoint。
|
|
149
|
+
* @param frame JSON 帧。
|
|
150
|
+
* @param timeoutMs 超时。
|
|
151
|
+
* @returns 服务端响应对象。
|
|
152
|
+
*/
|
|
153
|
+
function requestOnce(endpoint, frame, timeoutMs) {
|
|
154
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
155
|
+
const socket = createConnection(endpoint);
|
|
156
|
+
let buffer = "";
|
|
157
|
+
let settled = false;
|
|
158
|
+
const finish = (error, response) => {
|
|
159
|
+
if (settled) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
settled = true;
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
socket.destroy();
|
|
165
|
+
if (error) {
|
|
166
|
+
rejectPromise(error);
|
|
167
|
+
} else {
|
|
168
|
+
resolvePromise(response);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const timer = setTimeout(() => finish(new Error("连接 Hub 通知入口超时。")), timeoutMs);
|
|
172
|
+
timer.unref?.();
|
|
173
|
+
socket.on("connect", () => {
|
|
174
|
+
socket.write(`${JSON.stringify(frame)}\n`, "utf8");
|
|
175
|
+
});
|
|
176
|
+
socket.on("data", (chunk) => {
|
|
177
|
+
buffer += chunk;
|
|
178
|
+
const newline = buffer.indexOf("\n");
|
|
179
|
+
if (newline < 0) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const line = buffer.slice(0, newline);
|
|
183
|
+
try {
|
|
184
|
+
finish(undefined, JSON.parse(line));
|
|
185
|
+
} catch {
|
|
186
|
+
finish(new Error("Hub 返回了无效响应。"));
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
socket.on("error", (error) => {
|
|
190
|
+
finish(new Error(`无法连接 Hub 通知入口 (${error.code ?? error.message})。Hub 可能未启动。`));
|
|
191
|
+
});
|
|
192
|
+
socket.on("close", () => finish(new Error("连接在收到响应前关闭。")));
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 主流程。
|
|
198
|
+
*
|
|
199
|
+
* @returns 无返回值。
|
|
200
|
+
*/
|
|
201
|
+
async function main() {
|
|
202
|
+
const options = parseArguments(process.argv.slice(2));
|
|
203
|
+
const managerDirectory = resolveManagerDirectory(options.managerDirectory);
|
|
204
|
+
const keyPath = join(managerDirectory, "notify-shared.key");
|
|
205
|
+
let key;
|
|
206
|
+
try {
|
|
207
|
+
key = (await readFile(keyPath, "utf8")).trim();
|
|
208
|
+
} catch {
|
|
209
|
+
process.stderr.write([
|
|
210
|
+
`未找到通知入口 key: ${keyPath}`,
|
|
211
|
+
"请先在 Pi TUI 启动 Hub (/wx start), 首次启动会自动生成 key。",
|
|
212
|
+
].join("\n"));
|
|
213
|
+
process.stderr.write("\n");
|
|
214
|
+
process.exit(2);
|
|
215
|
+
}
|
|
216
|
+
if (!/^[a-f0-9]{64}$/i.test(key)) {
|
|
217
|
+
process.stderr.write("通知入口 key 内容无效, 请重新启动 Hub 生成。");
|
|
218
|
+
process.stderr.write("\n");
|
|
219
|
+
process.exit(2);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const requestId = randomUUID();
|
|
223
|
+
const timestamp = Date.now();
|
|
224
|
+
const payload = JSON.stringify({
|
|
225
|
+
...(options.title ? { title: options.title } : {}),
|
|
226
|
+
text: options.text,
|
|
227
|
+
...(options.source ? { source: options.source } : {}),
|
|
228
|
+
});
|
|
229
|
+
const frame = {
|
|
230
|
+
requestId,
|
|
231
|
+
timestamp,
|
|
232
|
+
payload,
|
|
233
|
+
proof: computeProof(key, requestId, timestamp, payload),
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const endpoint = createEndpoint(managerDirectory);
|
|
237
|
+
let response;
|
|
238
|
+
try {
|
|
239
|
+
response = await requestOnce(endpoint, frame, options.timeoutMs);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
process.stderr.write(error instanceof Error ? error.message : String(error));
|
|
242
|
+
process.stderr.write("\n");
|
|
243
|
+
process.exit(3);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (response.ok) {
|
|
247
|
+
const detail = response.code === "queued"
|
|
248
|
+
? `通知已积压 (第 ${response.position} 条), 你续命后自动补发。`
|
|
249
|
+
: "通知已发送到微信。";
|
|
250
|
+
process.stdout.write(detail);
|
|
251
|
+
process.stdout.write("\n");
|
|
252
|
+
process.exit(0);
|
|
253
|
+
}
|
|
254
|
+
process.stderr.write(`发送失败 (${response.code}): ${response.message ?? "未知原因"}`);
|
|
255
|
+
if (response.retryAfterMs) {
|
|
256
|
+
process.stderr.write(`; 建议 ${Math.ceil(response.retryAfterMs / 1000)} 秒后重试。`);
|
|
257
|
+
}
|
|
258
|
+
process.stderr.write("\n");
|
|
259
|
+
process.exit(4);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
main().catch((error) => {
|
|
263
|
+
process.stderr.write(error instanceof Error ? error.message : String(error));
|
|
264
|
+
process.stderr.write("\n");
|
|
265
|
+
process.exit(1);
|
|
266
|
+
});
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { createHmac, randomUUID } from "node:crypto";
|
|
2
|
+
import { createConnection } from "node:net";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { Type } from "typebox";
|
|
8
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { getManagerDataDirectory } from "../config/paths.ts";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 单次通知投递的连接超时。
|
|
13
|
+
*/
|
|
14
|
+
const NOTIFY_TIMEOUT_MS = 10000;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 通知入口的 pipe endpoint, 与服务端同一推导逻辑。
|
|
18
|
+
*
|
|
19
|
+
* @param managerDirectory manager 数据目录。
|
|
20
|
+
* @returns Windows named pipe 或 POSIX socket 路径。
|
|
21
|
+
*/
|
|
22
|
+
function createEndpoint(managerDirectory: string): string {
|
|
23
|
+
const resolved = resolve(managerDirectory);
|
|
24
|
+
const normalized = process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
25
|
+
const scope = createHash("sha256").update(normalized).digest("hex").slice(0, 24);
|
|
26
|
+
return process.platform === "win32"
|
|
27
|
+
? `\\\\.\\pipe\\pi-wechat-session-hub-${scope}-notify`
|
|
28
|
+
: join(tmpdir(), `pi-wechat-session-hub-${scope}-notify.sock`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 计算与服务端一致的 HMAC proof。
|
|
33
|
+
*
|
|
34
|
+
* @param key hex 共享密钥。
|
|
35
|
+
* @param requestId 请求 ID。
|
|
36
|
+
* @param timestamp 毫秒时间戳。
|
|
37
|
+
* @param payload 规范化 JSON 字符串。
|
|
38
|
+
* @returns base64url proof。
|
|
39
|
+
*/
|
|
40
|
+
function computeProof(key: string, requestId: string, timestamp: number, payload: string): string {
|
|
41
|
+
return createHmac("sha256", Buffer.from(key, "hex"))
|
|
42
|
+
.update(`${requestId}\n${timestamp}\n`, "utf8")
|
|
43
|
+
.update(payload, "utf8")
|
|
44
|
+
.digest("base64url");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 通过本机通知入口发送一条微信通知; 由 wx_notify 工具与测试共用。
|
|
49
|
+
*
|
|
50
|
+
* @param managerDirectory manager 数据目录。
|
|
51
|
+
* @param options 标题与正文。
|
|
52
|
+
* @returns 用户可读的结果说明。
|
|
53
|
+
*/
|
|
54
|
+
export async function sendNotificationViaIngress(
|
|
55
|
+
managerDirectory: string,
|
|
56
|
+
options: { title?: string; text: string; source?: string },
|
|
57
|
+
): Promise<string> {
|
|
58
|
+
const keyPath = join(managerDirectory, "notify-shared.key");
|
|
59
|
+
let key: string;
|
|
60
|
+
try {
|
|
61
|
+
key = (await readFile(keyPath, "utf8")).trim();
|
|
62
|
+
} catch {
|
|
63
|
+
return "微信通知发送失败: Hub 尚未初始化通知入口。请让用户在 Pi TUI 执行 /wx start 后重试。";
|
|
64
|
+
}
|
|
65
|
+
if (!/^[a-f0-9]{64}$/i.test(key)) {
|
|
66
|
+
return "微信通知发送失败: 通知入口 key 无效。请让用户执行 /wx restart 重新生成。";
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const requestId = randomUUID();
|
|
70
|
+
const timestamp = Date.now();
|
|
71
|
+
const payload = JSON.stringify({
|
|
72
|
+
...(options.title ? { title: options.title } : {}),
|
|
73
|
+
text: options.text,
|
|
74
|
+
...(options.source ? { source: options.source } : {}),
|
|
75
|
+
});
|
|
76
|
+
const frame = {
|
|
77
|
+
requestId,
|
|
78
|
+
timestamp,
|
|
79
|
+
payload,
|
|
80
|
+
proof: computeProof(key, requestId, timestamp, payload),
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const endpoint = createEndpoint(managerDirectory);
|
|
84
|
+
return await new Promise<string>((resolvePromise) => {
|
|
85
|
+
const socket = createConnection(endpoint);
|
|
86
|
+
let buffer = "";
|
|
87
|
+
let settled = false;
|
|
88
|
+
const finish = (message: string): void => {
|
|
89
|
+
if (settled) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
settled = true;
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
socket.destroy();
|
|
95
|
+
resolvePromise(message);
|
|
96
|
+
};
|
|
97
|
+
const timer = setTimeout(() => finish("微信通知发送失败: 连接 Hub 通知入口超时。Hub 可能未启动, 请让用户执行 /wx start。"), NOTIFY_TIMEOUT_MS);
|
|
98
|
+
timer.unref();
|
|
99
|
+
socket.on("connect", () => {
|
|
100
|
+
socket.write(`${JSON.stringify(frame)}\n`, "utf8");
|
|
101
|
+
});
|
|
102
|
+
socket.on("data", (chunk: Buffer) => {
|
|
103
|
+
buffer += chunk.toString("utf8");
|
|
104
|
+
const newline = buffer.indexOf("\n");
|
|
105
|
+
if (newline < 0) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let response: Record<string, unknown>;
|
|
109
|
+
try {
|
|
110
|
+
response = JSON.parse(buffer.slice(0, newline)) as Record<string, unknown>;
|
|
111
|
+
} catch {
|
|
112
|
+
finish("微信通知发送失败: Hub 返回了无效响应。");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (response.ok === true) {
|
|
116
|
+
finish(response.code === "queued"
|
|
117
|
+
? `通知已进入积压队列 (第 ${String(response.position)} 条); 用户续命后自动补发到微信。`
|
|
118
|
+
: "通知已发送到用户微信。");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const code = typeof response.code === "string" ? response.code : "unknown";
|
|
122
|
+
const message = typeof response.message === "string" ? response.message : "未知原因";
|
|
123
|
+
finish(`微信通知未能送达 (${code}): ${message}`);
|
|
124
|
+
});
|
|
125
|
+
socket.on("error", () => {
|
|
126
|
+
finish("微信通知发送失败: 无法连接 Hub 通知入口。Hub 可能未启动, 请让用户执行 /wx start 后重试。");
|
|
127
|
+
});
|
|
128
|
+
socket.on("close", () => finish("微信通知发送失败: 连接在收到响应前关闭。"));
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 注册 wx_notify 工具: 当前 Pi 会话内的 agent 可直接发微信通知。
|
|
134
|
+
*
|
|
135
|
+
* 工具随包自动注册, 无需用户配置; 失败信息引导用户操作而不是静默重试。
|
|
136
|
+
*
|
|
137
|
+
* @param pi Pi 扩展 API。
|
|
138
|
+
* @returns 无返回值。
|
|
139
|
+
*/
|
|
140
|
+
export function registerWechatNotifyTool(pi: ExtensionAPI): void {
|
|
141
|
+
// 旧版本 Pi 或测试 fake 可能不提供 registerTool; 工具是增强能力, 缺失时静默跳过。
|
|
142
|
+
if (typeof pi.registerTool !== "function") {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
pi.registerTool({
|
|
146
|
+
name: "wx_notify",
|
|
147
|
+
label: "微信通知",
|
|
148
|
+
description: [
|
|
149
|
+
"把一条通知直接发送到用户的微信 (与当前会话无关, 不进入对话上下文)。",
|
|
150
|
+
"适用: 长任务完成、构建/测试结果、后台任务需要用户决策。",
|
|
151
|
+
"不适用: 普通对话回复 (直接在会话里说即可)。正文请简洁, 一两句话。",
|
|
152
|
+
].join(" "),
|
|
153
|
+
promptSnippet: "Send a WeChat notification to the user (wx_notify)",
|
|
154
|
+
promptGuidelines: [
|
|
155
|
+
"Use wx_notify when the user asks to be notified via 微信/WeChat (例如: 构建完微信通知我、发微信告诉我、结果发我微信), or when a long-running task finishes while the user is likely away.",
|
|
156
|
+
],
|
|
157
|
+
parameters: Type.Object({
|
|
158
|
+
title: Type.String({ description: "通知标题, 例如: 构建完成" }),
|
|
159
|
+
text: Type.String({ description: "通知正文, 一两句话说明结果" }),
|
|
160
|
+
source: Type.Optional(Type.String({ description: "调用方标识, 例如: ci、build-script" })),
|
|
161
|
+
}),
|
|
162
|
+
async execute(_toolCallId, params) {
|
|
163
|
+
const managerDirectory = getManagerDataDirectory();
|
|
164
|
+
const message = await sendNotificationViaIngress(managerDirectory, {
|
|
165
|
+
title: params.title,
|
|
166
|
+
text: params.text,
|
|
167
|
+
...(params.source ? { source: params.source } : {}),
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
content: [{ type: "text", text: message }],
|
|
171
|
+
details: {},
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 供测试使用的路径推导, 与 getManagerDataDirectory 一致。
|
|
179
|
+
*
|
|
180
|
+
* @param agentDirectory Pi agent 目录覆盖值。
|
|
181
|
+
* @returns manager 数据目录。
|
|
182
|
+
*/
|
|
183
|
+
export function resolveNotifyManagerDirectory(agentDirectory?: string): string {
|
|
184
|
+
const base = agentDirectory?.trim()
|
|
185
|
+
? (isAbsolute(agentDirectory) ? agentDirectory : join(homedir(), agentDirectory))
|
|
186
|
+
: join(homedir(), ".pi", "agent");
|
|
187
|
+
return join(base, "wechat-session-manager");
|
|
188
|
+
}
|
package/src/config/defaults.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import type { ManagerConfiguration } from "./types.ts";
|
|
1
|
+
import type { ManagerConfiguration, NotificationConfiguration } from "./types.ts";
|
|
2
2
|
import { getCredentialFilePath } from "./paths.ts";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* 创建默认通知配置。
|
|
6
|
+
*
|
|
7
|
+
* @returns 默认使用积压补发策略的通知配置。
|
|
8
|
+
*/
|
|
9
|
+
export function createDefaultNotificationConfiguration(): NotificationConfiguration {
|
|
10
|
+
return {
|
|
11
|
+
overflowPolicy: "queue",
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
4
15
|
/**
|
|
5
16
|
* 创建默认运行时配置。
|
|
6
17
|
*
|
|
@@ -26,5 +37,6 @@ export function createDefaultConfiguration(
|
|
|
26
37
|
maxBackgroundWorkers: 4,
|
|
27
38
|
maxQueuePerSession: 20,
|
|
28
39
|
},
|
|
40
|
+
notification: createDefaultNotificationConfiguration(),
|
|
29
41
|
};
|
|
30
42
|
}
|
package/src/config/types.ts
CHANGED
|
@@ -20,6 +20,22 @@ export interface RuntimeConfiguration {
|
|
|
20
20
|
[key: string]: unknown;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* 通知入口的过期处理策略。
|
|
25
|
+
*/
|
|
26
|
+
export type NotificationOverflowPolicy = "queue" | "drop";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 通知入口配置。
|
|
30
|
+
*/
|
|
31
|
+
export interface NotificationConfiguration {
|
|
32
|
+
/**
|
|
33
|
+
* 过期通知的处理策略: queue 为积压补发, drop 为直接拒绝。
|
|
34
|
+
*/
|
|
35
|
+
overflowPolicy: NotificationOverflowPolicy;
|
|
36
|
+
[key: string]: unknown;
|
|
37
|
+
}
|
|
38
|
+
|
|
23
39
|
export interface ManagerConfiguration {
|
|
24
40
|
schemaVersion: 1;
|
|
25
41
|
autoStartOnWindowsLogon: boolean;
|
|
@@ -29,5 +45,6 @@ export interface ManagerConfiguration {
|
|
|
29
45
|
projects: Record<string, ProjectDefinition>;
|
|
30
46
|
wechat: WechatConfiguration;
|
|
31
47
|
runtime: RuntimeConfiguration;
|
|
48
|
+
notification: NotificationConfiguration;
|
|
32
49
|
[key: string]: unknown;
|
|
33
50
|
}
|
package/src/config/validation.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { createDefaultConfiguration } from "./defaults.ts";
|
|
|
3
3
|
import { getCredentialFilePath } from "./paths.ts";
|
|
4
4
|
import type {
|
|
5
5
|
ManagerConfiguration,
|
|
6
|
+
NotificationConfiguration,
|
|
6
7
|
ProjectDefinition,
|
|
7
8
|
RuntimeConfiguration,
|
|
8
9
|
RuntimeMode,
|
|
@@ -46,6 +47,9 @@ export function validateConfiguration(
|
|
|
46
47
|
const inputRuntime = input.runtime === undefined
|
|
47
48
|
? {}
|
|
48
49
|
: asRecord(input.runtime, "runtime");
|
|
50
|
+
const inputNotification = input.notification === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: asRecord(input.notification, "notification");
|
|
49
53
|
|
|
50
54
|
const candidate: Record<string, unknown> = {
|
|
51
55
|
...defaults,
|
|
@@ -58,6 +62,10 @@ export function validateConfiguration(
|
|
|
58
62
|
...defaults.runtime,
|
|
59
63
|
...inputRuntime,
|
|
60
64
|
},
|
|
65
|
+
notification: {
|
|
66
|
+
...defaults.notification,
|
|
67
|
+
...inputNotification,
|
|
68
|
+
},
|
|
61
69
|
allowedProjectRoots: input.allowedProjectRoots ?? defaults.allowedProjectRoots,
|
|
62
70
|
projects: input.projects ?? defaults.projects,
|
|
63
71
|
};
|
|
@@ -127,6 +135,7 @@ export function validateConfiguration(
|
|
|
127
135
|
|
|
128
136
|
const wechat = validateWechat(candidate.wechat);
|
|
129
137
|
const runtime = validateRuntime(candidate.runtime);
|
|
138
|
+
const notification = validateNotification(candidate.notification);
|
|
130
139
|
|
|
131
140
|
return {
|
|
132
141
|
...candidate,
|
|
@@ -137,6 +146,7 @@ export function validateConfiguration(
|
|
|
137
146
|
projects,
|
|
138
147
|
wechat,
|
|
139
148
|
runtime,
|
|
149
|
+
notification,
|
|
140
150
|
} as ManagerConfiguration;
|
|
141
151
|
}
|
|
142
152
|
|
|
@@ -190,6 +200,25 @@ function validateWechat(value: unknown): WechatConfiguration {
|
|
|
190
200
|
return wechat as WechatConfiguration;
|
|
191
201
|
}
|
|
192
202
|
|
|
203
|
+
/**
|
|
204
|
+
* 校验通知入口配置并保留扩展字段。
|
|
205
|
+
*
|
|
206
|
+
* @param value 待校验值。
|
|
207
|
+
* @returns 通知配置对象。
|
|
208
|
+
*/
|
|
209
|
+
function validateNotification(value: unknown): NotificationConfiguration {
|
|
210
|
+
const notification = asRecord(value, "notification");
|
|
211
|
+
if (
|
|
212
|
+
notification.overflowPolicy !== "queue" &&
|
|
213
|
+
notification.overflowPolicy !== "drop"
|
|
214
|
+
) {
|
|
215
|
+
throw new ConfigurationValidationError(
|
|
216
|
+
"notification.overflowPolicy 只能是 queue 或 drop。",
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return notification as NotificationConfiguration;
|
|
220
|
+
}
|
|
221
|
+
|
|
193
222
|
/**
|
|
194
223
|
* 校验运行时参数并保留扩展字段。
|
|
195
224
|
*
|
|
@@ -4,6 +4,7 @@ import type { RuntimeMode } from "../config/types.ts";
|
|
|
4
4
|
export type GatewayCommand =
|
|
5
5
|
| { kind: "help" }
|
|
6
6
|
| { kind: "status" }
|
|
7
|
+
| { kind: "renew" }
|
|
7
8
|
| { kind: "projects"; arguments: string }
|
|
8
9
|
| { kind: "new"; mode?: RuntimeMode; projectAlias?: string; name?: string }
|
|
9
10
|
| { kind: "sessions"; projectAlias?: string; state: "default" | "active" | "saved" | "pending" | "all" }
|
|
@@ -65,6 +66,10 @@ export function parseGatewayCommand(text: string): GatewayCommand {
|
|
|
65
66
|
ensureNoArguments(arguments_);
|
|
66
67
|
return { kind: "status" };
|
|
67
68
|
}
|
|
69
|
+
if (commandName === "renew") {
|
|
70
|
+
ensureNoArguments(arguments_);
|
|
71
|
+
return { kind: "renew" };
|
|
72
|
+
}
|
|
68
73
|
if (commandName === "projects") {
|
|
69
74
|
return {
|
|
70
75
|
kind: "projects",
|