pi-wechat-gateway 0.3.0 → 0.3.2
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 +14 -0
- package/package.json +4 -3
- package/src/config.ts +26 -1
- package/src/gateway.ts +13 -1
- package/src/main.ts +6 -0
- package/src/vendor-probe.ts +126 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.2 — 2026-12-06
|
|
4
|
+
|
|
5
|
+
- **发送返回体形状校验**:`realDeps.sendText` 调完第三方 `sendMessageWeixin` 后要求返回
|
|
6
|
+
`{ messageId: string }`(上游契约),结构变了当 `send-failed` 抛出,而不是把 `undefined` 当成功。
|
|
7
|
+
校验抽成纯函数 `assertSendResult`,新增 5 条断言。
|
|
8
|
+
- 新增 `.github/workflows/ci.yml`(install + `npm run validate`);CI 固定 `TZ=Asia/Shanghai`(用例断言本地时间格式,UTC runner 上 5 条红)。
|
|
9
|
+
|
|
10
|
+
## 0.3.1 — 2026-12-05
|
|
11
|
+
|
|
12
|
+
- **响亮失败**:`run` / `drain-once` 在第三方微信 SDK 的接口与锁定版本不匹配时**拒绝启动**——
|
|
13
|
+
stderr 列出缺的符号(`getUpdates` / `sendMessageWeixin` / `notifyStart` / `notifyStop`)与回锁定版本的命令,
|
|
14
|
+
退出码 **4**(此前是一边 `poll-failed` 退避一边静默收不到消息)。依赖由 `^2.4.10` 收紧为精确 `2.4.10`,
|
|
15
|
+
`npm run validate` 加一条 import 冒烟。
|
|
16
|
+
|
|
3
17
|
## 0.3.0 — 2026-12-05
|
|
4
18
|
|
|
5
19
|
- **slug 口径与 pi-auto 统一**:`src/slug.ts` 从 `path.basename()` 改为「相对 $HOME 的路径,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-wechat-gateway",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "One WeChat channel, many Pi sessions: an out-of-process iLink gateway that fans notifications in, routes replies back by quote/prefix, and keeps every session free of the single-bridge lock.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi",
|
|
@@ -28,10 +28,11 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"test": "node --test 'test/*.test.mjs'",
|
|
30
30
|
"check": "node --check src/main.ts 2>/dev/null || node --input-type=module -e \"await import('./src/router.ts'); await import('./src/queue.ts'); console.log('ok')\"",
|
|
31
|
-
"validate": "npm run test && npm run check"
|
|
31
|
+
"validate": "npm run test && npm run check && npm run smoke",
|
|
32
|
+
"smoke": "node --input-type=module -e \"const p = await import('./src/vendor-probe.ts'); if (typeof p.probeVendor !== 'function') throw new Error('vendor-probe: 未导出 probeVendor'); await import('./src/gateway.ts'); await import('./src/main.ts'); console.log('import smoke ok')\""
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@tencent-weixin/openclaw-weixin": "
|
|
35
|
+
"@tencent-weixin/openclaw-weixin": "2.4.10",
|
|
35
36
|
"qrcode-terminal": "0.12.0"
|
|
36
37
|
},
|
|
37
38
|
"repository": {
|
package/src/config.ts
CHANGED
|
@@ -35,6 +35,31 @@ const envInt = (name: string, fallback: number): number => {
|
|
|
35
35
|
};
|
|
36
36
|
const envStr = (name: string, fallback: string): string => process.env[name]?.trim() || fallback;
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* `~` / `~/x` 展开成 `<home>/x`(与 `bin/mailbox-send` 的 `os.path.expanduser` 同式;`~user` 不支持,原样返回)。
|
|
40
|
+
* 不展开的话 `PI_MAILBOX_DIR=~/mbox` 在本仓是字面量目录 `<cwd>/~/mbox`,与读方算的不是一个地方。
|
|
41
|
+
*/
|
|
42
|
+
function expandHomeTilde(p: string): string {
|
|
43
|
+
if (p === "~") return os.homedir();
|
|
44
|
+
if (p.startsWith("~/")) return path.join(os.homedir(), p.slice(2));
|
|
45
|
+
return p;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 信箱根(契约 D1,与 pi-mailbox 的 `mailboxRoot()` 同式):
|
|
50
|
+
* `PI_MAILBOX_DIR` > 旧名 `PI_AUTO_MAILBOX_DIR` > `PI_WECHAT_MAILBOX` > `~/.pi/agent/mailbox`。
|
|
51
|
+
* `PI_WECHAT_MAILBOX` 降级为只读别名 —— 它曾经是本仓独有的名字,读方不认,
|
|
52
|
+
* 设了就会「入站落进没人轮询的目录」(2026-12-05 审计 §1.1)。
|
|
53
|
+
*/
|
|
54
|
+
export function mailboxRoot(env: NodeJS.ProcessEnv | Record<string, string | undefined> = process.env): string {
|
|
55
|
+
return expandHomeTilde(
|
|
56
|
+
env.PI_MAILBOX_DIR?.trim() ||
|
|
57
|
+
env.PI_AUTO_MAILBOX_DIR?.trim() ||
|
|
58
|
+
env.PI_WECHAT_MAILBOX?.trim() ||
|
|
59
|
+
path.join(home(), ".pi", "agent", "mailbox"),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
38
63
|
/** "HH:MM" → 分钟;解析失败回退默认。 */
|
|
39
64
|
export function parseClock(value: string, fallbackMin: number): number {
|
|
40
65
|
const m = /^(\d{1,2}):(\d{2})$/.exec(String(value ?? "").trim());
|
|
@@ -70,7 +95,7 @@ export function loadConfig(): GatewayConfig {
|
|
|
70
95
|
return { token: "", userId: "" };
|
|
71
96
|
}
|
|
72
97
|
})();
|
|
73
|
-
const mailboxDir =
|
|
98
|
+
const mailboxDir = mailboxRoot();
|
|
74
99
|
return {
|
|
75
100
|
baseUrl: envStr("PI_WECHAT_BASE_URL", creds.baseUrl || "https://ilinkai.weixin.qq.com"),
|
|
76
101
|
token: creds.token,
|
package/src/gateway.ts
CHANGED
|
@@ -294,6 +294,16 @@ async function sendOne(cfg: GatewayConfig, state: GatewayState, deps: GatewayDep
|
|
|
294
294
|
|
|
295
295
|
// ---------------------------------------------------------------- 主循环
|
|
296
296
|
|
|
297
|
+
/**
|
|
298
|
+
* 上游 `sendMessageWeixin` 的成功契约:`{ messageId: string, serverMessageId?: string }`,失败直接 throw。
|
|
299
|
+
* 我们深引的是 `dist/**`(无 exports 保证),返回体一旦改名不能当成功——把它变成响亮的 send-failed。
|
|
300
|
+
*/
|
|
301
|
+
export function assertSendResult(result: unknown): void {
|
|
302
|
+
if (!result || typeof result !== "object" || typeof (result as { messageId?: unknown }).messageId !== "string") {
|
|
303
|
+
throw new Error(`sendMessageWeixin 返回体不符合预期(缺 messageId):${JSON.stringify(result)?.slice(0, 200)}`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
297
307
|
export function realDeps(logFile: string, cfg?: GatewayConfig): GatewayDeps {
|
|
298
308
|
const cfgBaseUrl = () => cfg?.baseUrl ?? "";
|
|
299
309
|
const cfgToken = () => cfg?.token ?? "";
|
|
@@ -320,7 +330,9 @@ export function realDeps(logFile: string, cfg?: GatewayConfig): GatewayDeps {
|
|
|
320
330
|
},
|
|
321
331
|
async sendText(params) {
|
|
322
332
|
const mod = await import("@tencent-weixin/openclaw-weixin/dist/src/messaging/send.js");
|
|
323
|
-
|
|
333
|
+
const result: unknown = await mod.sendMessageWeixin(params as never);
|
|
334
|
+
assertSendResult(result);
|
|
335
|
+
return result;
|
|
324
336
|
},
|
|
325
337
|
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
326
338
|
now: () => new Date(),
|
package/src/main.ts
CHANGED
|
@@ -27,6 +27,7 @@ import { readLogin, sendVerifyCode, startLogin } from "./login.ts";
|
|
|
27
27
|
import { drainOutbox, realDeps, runGateway, type GatewayDeps } from "./gateway.ts";
|
|
28
28
|
import { priorityOf } from "./router.ts";
|
|
29
29
|
import { renderQr } from "./qr.ts";
|
|
30
|
+
import { reportVendorProbe } from "./vendor-probe.ts";
|
|
30
31
|
import {
|
|
31
32
|
UNIT,
|
|
32
33
|
clearCredentials,
|
|
@@ -361,6 +362,8 @@ async function main(): Promise<number> {
|
|
|
361
362
|
}
|
|
362
363
|
|
|
363
364
|
if (cmd === "drain-once") {
|
|
365
|
+
// 深引第三方 dist/:发不出去就是静默失败,先确认 4 个符号还在(缺即非 0 退出)。
|
|
366
|
+
if (!(await reportVendorProbe())) return 4;
|
|
364
367
|
ensureDirs({ outbox: p.outbox, working: p.working, sent: p.sent, failed: p.failed, pending: p.pending });
|
|
365
368
|
const deps: GatewayDeps = realDeps(p.log, cfg);
|
|
366
369
|
const state = { syncBuf: "", contextTokens: (readJson(p.context) as Record<string, string>) ?? {} };
|
|
@@ -405,6 +408,9 @@ async function main(): Promise<number> {
|
|
|
405
408
|
console.error(`没有可用凭据(${cfg.credentialsFile})。先跑:pi-wechat-gateway login`);
|
|
406
409
|
return 3;
|
|
407
410
|
}
|
|
411
|
+
// 启动前置:第三方 SDK 的 4 个符号(getUpdates / sendMessageWeixin / notifyStart / notifyStop)
|
|
412
|
+
// 缺一个就不启动 —— 否则失败姿势是「日志里 poll-failed + 退避 + 收不到消息」。
|
|
413
|
+
if (!(await reportVendorProbe())) return 4;
|
|
408
414
|
ensureDirs({ outbox: p.outbox, working: p.working, sent: p.sent, failed: p.failed, pending: p.pending });
|
|
409
415
|
const controller = new AbortController();
|
|
410
416
|
const stop = () => controller.abort();
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 启动探针:第三方微信 SDK 是不是还是本版本假设的那套接口。
|
|
3
|
+
*
|
|
4
|
+
* `@tencent-weixin/openclaw-weixin` 没有 `exports` 字段,`gateway.ts` 是**按路径**深引
|
|
5
|
+
* `dist/src/api/api.js` 与 `dist/src/messaging/send.js` 的。上游加 `exports`、改目录结构或改符号名,
|
|
6
|
+
* 深引当场解析失败/拿到 undefined;而网关的失败姿势是 `poll-failed` 进日志 + 指数退避 ⇒
|
|
7
|
+
* 「静默降级为收不到消息」。所以启动时先对一遍 4 个符号,缺一个就带着清单非 0 退出。
|
|
8
|
+
*
|
|
9
|
+
* 版本精确锁定(见 package.json):这里引的是**路径**,不是契约,minor 也可能动。
|
|
10
|
+
* 版本号与接口是两件事,所以分两级:
|
|
11
|
+
* - 符号缺失/解析失败 ⇒ 阻断启动(exit 4,hint 才出现);
|
|
12
|
+
* - 版本号与锁定值不同但符号齐全 ⇒ 只在 stderr 给一条 warning,**照常启动**(接口在就还能用,
|
|
13
|
+
* 拿版本号挡启动只会让「升级后其实没问题」也起不来)。
|
|
14
|
+
*/
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { createRequire } from "node:module";
|
|
17
|
+
|
|
18
|
+
export const VENDOR_PACKAGE = "@tencent-weixin/openclaw-weixin";
|
|
19
|
+
|
|
20
|
+
/** `package.json` 里锁定的版本;探针文案与自检都以它为准。 */
|
|
21
|
+
export const PINNED_VENDOR_VERSION = "2.4.10";
|
|
22
|
+
|
|
23
|
+
/** 网关真正调用的 4 个符号,以及它们所在的深引路径。 */
|
|
24
|
+
export const VENDOR_SURFACE: readonly { specifier: string; symbols: readonly string[] }[] = [
|
|
25
|
+
{
|
|
26
|
+
specifier: `${VENDOR_PACKAGE}/dist/src/api/api.js`,
|
|
27
|
+
symbols: ["getUpdates", "notifyStart", "notifyStop"],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
specifier: `${VENDOR_PACKAGE}/dist/src/messaging/send.js`,
|
|
31
|
+
symbols: ["sendMessageWeixin"],
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
export type ProbeImporter = (specifier: string) => Promise<Record<string, unknown>>;
|
|
36
|
+
|
|
37
|
+
export type VendorProbeResult =
|
|
38
|
+
| { ok: true; version: string; warnings: string[] }
|
|
39
|
+
| { ok: false; problems: string[]; hint: string };
|
|
40
|
+
|
|
41
|
+
const defaultImporter: ProbeImporter = (specifier) => import(specifier);
|
|
42
|
+
|
|
43
|
+
/** 已安装版本号;读不到就写 "unknown"(读不到版本本身不算探针失败)。 */
|
|
44
|
+
export function installedVendorVersion(): string {
|
|
45
|
+
try {
|
|
46
|
+
const require = createRequire(import.meta.url);
|
|
47
|
+
const pkgPath = require.resolve(`${VENDOR_PACKAGE}/package.json`);
|
|
48
|
+
const raw = JSON.parse(readFileSync(pkgPath, "utf8")) as { version?: unknown };
|
|
49
|
+
return typeof raw.version === "string" ? raw.version : "unknown";
|
|
50
|
+
} catch {
|
|
51
|
+
return "unknown";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 缺符号时的可操作建议。版本号只在与锁定值**不同**时出现:符号全在时读到的版本经常就是锁定值,
|
|
57
|
+
* 写成「当前安装 X,本版本锁的是 X」是自相矛盾的无用信息。
|
|
58
|
+
*/
|
|
59
|
+
export function vendorHint(installed: string): string {
|
|
60
|
+
const expected = VENDOR_SURFACE.flatMap((entry) => entry.symbols).length;
|
|
61
|
+
const lines = [`${VENDOR_PACKAGE} 的目录结构或导出变了:启动需要的 ${expected} 个符号见上面 problems。`];
|
|
62
|
+
if (installed !== "unknown" && installed !== PINNED_VENDOR_VERSION) {
|
|
63
|
+
lines.push(`当前安装 ${installed},与锁定值 ${PINNED_VENDOR_VERSION} 不同 —— 这很可能就是布局变了的原因。`);
|
|
64
|
+
}
|
|
65
|
+
lines.push(
|
|
66
|
+
`先回锁定版本:npm install ${VENDOR_PACKAGE}@${PINNED_VENDOR_VERSION}`,
|
|
67
|
+
"要跟进新版:核对 src/vendor-probe.ts 的 VENDOR_SURFACE 与 src/gateway.ts 的深引路径,改完再启网关(符号缺失时网关不启动,避免静默收不到消息)。",
|
|
68
|
+
);
|
|
69
|
+
return lines.join("\n");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 版本号与锁定值不同但接口还在时的唯一提示(不阻断启动)。 */
|
|
73
|
+
export function versionWarning(installed: string): string | undefined {
|
|
74
|
+
if (installed === "unknown" || installed === PINNED_VENDOR_VERSION) return undefined;
|
|
75
|
+
return (
|
|
76
|
+
`${VENDOR_PACKAGE} 装的是 ${installed},锁定值是 ${PINNED_VENDOR_VERSION}:4 个符号都在,照常启动;` +
|
|
77
|
+
`若后续出现解析/调用错误,先回锁定版本(npm install ${VENDOR_PACKAGE}@${PINNED_VENDOR_VERSION})。`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 逐个模块对符号:缺的进 problems(带符号名与路径),一个都不缺才 ok。
|
|
83
|
+
* `importer` 可注入,单测不必真装第三方包。
|
|
84
|
+
*/
|
|
85
|
+
export async function probeVendor(
|
|
86
|
+
importer: ProbeImporter = defaultImporter,
|
|
87
|
+
versionOf: () => string = installedVendorVersion,
|
|
88
|
+
): Promise<VendorProbeResult> {
|
|
89
|
+
const problems: string[] = [];
|
|
90
|
+
for (const { specifier, symbols } of VENDOR_SURFACE) {
|
|
91
|
+
let mod: Record<string, unknown>;
|
|
92
|
+
try {
|
|
93
|
+
mod = await importer(specifier);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
problems.push(`${specifier} 无法 import:${String((error as Error)?.message ?? error)}`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const missing = symbols.filter((name) => typeof mod?.[name] !== "function");
|
|
99
|
+
if (missing.length > 0) {
|
|
100
|
+
problems.push(`${specifier} 缺符号:${missing.join(", ")}(typeof !== "function")`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const installed = versionOf();
|
|
104
|
+
if (problems.length === 0) {
|
|
105
|
+
const warning = versionWarning(installed);
|
|
106
|
+
return { ok: true, version: installed, warnings: warning ? [warning] : [] };
|
|
107
|
+
}
|
|
108
|
+
return { ok: false, problems, hint: vendorHint(installed) };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 供 CLI 用:失败时把清单与建议一次打到 stderr;成功时只在版本漂移时给一条 warning。 */
|
|
112
|
+
export async function reportVendorProbe(
|
|
113
|
+
log: (line: string) => void = (line) => console.error(line),
|
|
114
|
+
importer: ProbeImporter = defaultImporter,
|
|
115
|
+
versionOf: () => string = installedVendorVersion,
|
|
116
|
+
): Promise<boolean> {
|
|
117
|
+
const probe = await probeVendor(importer, versionOf);
|
|
118
|
+
if (probe.ok) {
|
|
119
|
+
for (const warning of probe.warnings) log(warning);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
log(`pi-wechat-gateway 启动失败:${VENDOR_PACKAGE} 的接口和本版本不匹配(不启动,避免静默收不到消息)。`);
|
|
123
|
+
for (const problem of probe.problems) log(` - ${problem}`);
|
|
124
|
+
log(probe.hint);
|
|
125
|
+
return false;
|
|
126
|
+
}
|