opencode-copilot-account-switcher 0.13.6 → 0.14.1

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 (58) hide show
  1. package/dist/common-settings-actions.d.ts +1 -1
  2. package/dist/common-settings-actions.js +54 -0
  3. package/dist/common-settings-store.d.ts +25 -0
  4. package/dist/common-settings-store.js +81 -0
  5. package/dist/menu-runtime.js +12 -1
  6. package/dist/plugin-hooks.d.ts +8 -0
  7. package/dist/plugin-hooks.js +96 -0
  8. package/dist/providers/codex-menu-adapter.js +14 -1
  9. package/dist/providers/copilot-menu-adapter.js +13 -0
  10. package/dist/store-paths.d.ts +1 -0
  11. package/dist/store-paths.js +3 -0
  12. package/dist/ui/menu.d.ts +73 -34
  13. package/dist/ui/menu.js +195 -0
  14. package/dist/wechat/bind-flow.d.ts +25 -0
  15. package/dist/wechat/bind-flow.js +101 -0
  16. package/dist/wechat/bridge.d.ts +69 -0
  17. package/dist/wechat/bridge.js +180 -0
  18. package/dist/wechat/broker-client.d.ts +33 -0
  19. package/dist/wechat/broker-client.js +257 -0
  20. package/dist/wechat/broker-entry.d.ts +17 -0
  21. package/dist/wechat/broker-entry.js +182 -0
  22. package/dist/wechat/broker-launcher.d.ts +27 -0
  23. package/dist/wechat/broker-launcher.js +191 -0
  24. package/dist/wechat/broker-server.d.ts +25 -0
  25. package/dist/wechat/broker-server.js +540 -0
  26. package/dist/wechat/command-parser.d.ts +7 -0
  27. package/dist/wechat/command-parser.js +16 -0
  28. package/dist/wechat/compat/openclaw-guided-smoke.d.ts +178 -0
  29. package/dist/wechat/compat/openclaw-guided-smoke.js +1133 -0
  30. package/dist/wechat/compat/openclaw-public-helpers.d.ts +111 -0
  31. package/dist/wechat/compat/openclaw-public-helpers.js +262 -0
  32. package/dist/wechat/compat/openclaw-smoke.d.ts +48 -0
  33. package/dist/wechat/compat/openclaw-smoke.js +100 -0
  34. package/dist/wechat/compat/slash-guard.d.ts +11 -0
  35. package/dist/wechat/compat/slash-guard.js +24 -0
  36. package/dist/wechat/handle.d.ts +8 -0
  37. package/dist/wechat/handle.js +46 -0
  38. package/dist/wechat/ipc-auth.d.ts +6 -0
  39. package/dist/wechat/ipc-auth.js +39 -0
  40. package/dist/wechat/openclaw-account-adapter.d.ts +30 -0
  41. package/dist/wechat/openclaw-account-adapter.js +70 -0
  42. package/dist/wechat/operator-store.d.ts +9 -0
  43. package/dist/wechat/operator-store.js +69 -0
  44. package/dist/wechat/protocol.d.ts +29 -0
  45. package/dist/wechat/protocol.js +75 -0
  46. package/dist/wechat/request-store.d.ts +41 -0
  47. package/dist/wechat/request-store.js +215 -0
  48. package/dist/wechat/session-digest.d.ts +41 -0
  49. package/dist/wechat/session-digest.js +134 -0
  50. package/dist/wechat/state-paths.d.ts +14 -0
  51. package/dist/wechat/state-paths.js +45 -0
  52. package/dist/wechat/status-format.d.ts +14 -0
  53. package/dist/wechat/status-format.js +174 -0
  54. package/dist/wechat/token-store.d.ts +18 -0
  55. package/dist/wechat/token-store.js +100 -0
  56. package/dist/wechat/wechat-status-runtime.d.ts +24 -0
  57. package/dist/wechat/wechat-status-runtime.js +238 -0
  58. package/package.json +8 -3
@@ -0,0 +1,238 @@
1
+ import { loadOpenClawWeixinPublicHelpers, } from "./compat/openclaw-public-helpers.js";
2
+ import { parseWechatSlashCommand } from "./command-parser.js";
3
+ const DEFAULT_RETRY_DELAY_MS = 1_000;
4
+ const DEFAULT_LONG_POLL_TIMEOUT_MS = 25_000;
5
+ export const DEFAULT_NON_SLASH_REPLY_TEXT = "请使用 slash 命令(/status、/reply、/allow)";
6
+ export const DEFAULT_SLASH_HANDLER_ERROR_REPLY_TEXT = "命令处理失败,请稍后重试。";
7
+ function createAbortError() {
8
+ const error = new Error("wechat status runtime stopped");
9
+ error.name = "AbortError";
10
+ return error;
11
+ }
12
+ function isAbortError(error) {
13
+ return error instanceof Error && error.name === "AbortError";
14
+ }
15
+ function withAbort(promise, signal) {
16
+ if (signal.aborted) {
17
+ return Promise.reject(createAbortError());
18
+ }
19
+ return new Promise((resolve, reject) => {
20
+ let settled = false;
21
+ const cleanup = () => {
22
+ signal.removeEventListener("abort", onAbort);
23
+ };
24
+ const onAbort = () => {
25
+ if (settled) {
26
+ return;
27
+ }
28
+ settled = true;
29
+ cleanup();
30
+ reject(createAbortError());
31
+ };
32
+ signal.addEventListener("abort", onAbort, { once: true });
33
+ promise.then((value) => {
34
+ if (settled) {
35
+ return;
36
+ }
37
+ settled = true;
38
+ cleanup();
39
+ resolve(value);
40
+ }, (error) => {
41
+ if (settled) {
42
+ return;
43
+ }
44
+ settled = true;
45
+ cleanup();
46
+ reject(error);
47
+ });
48
+ });
49
+ }
50
+ function sleep(ms, signal) {
51
+ if (signal.aborted) {
52
+ return Promise.reject(createAbortError());
53
+ }
54
+ return new Promise((resolve, reject) => {
55
+ const timer = setTimeout(() => {
56
+ signal.removeEventListener("abort", onAbort);
57
+ resolve();
58
+ }, ms);
59
+ const onAbort = () => {
60
+ clearTimeout(timer);
61
+ signal.removeEventListener("abort", onAbort);
62
+ reject(createAbortError());
63
+ };
64
+ signal.addEventListener("abort", onAbort, { once: true });
65
+ });
66
+ }
67
+ function normalizePositiveInteger(value, fallback) {
68
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
69
+ return fallback;
70
+ }
71
+ return Math.floor(value);
72
+ }
73
+ function extractMessageText(message) {
74
+ for (const item of message.item_list ?? []) {
75
+ if (item?.type !== 1) {
76
+ continue;
77
+ }
78
+ if (typeof item.text_item?.text === "string" && item.text_item.text.trim().length > 0) {
79
+ return item.text_item.text;
80
+ }
81
+ }
82
+ return "";
83
+ }
84
+ function toNonEmptyString(value) {
85
+ if (typeof value !== "string") {
86
+ return null;
87
+ }
88
+ const trimmed = value.trim();
89
+ return trimmed.length > 0 ? trimmed : null;
90
+ }
91
+ export function createWechatStatusRuntime(input = {}) {
92
+ const loadPublicHelpers = input.loadPublicHelpers ?? loadOpenClawWeixinPublicHelpers;
93
+ const onSlashCommand = input.onSlashCommand ??
94
+ (async () => {
95
+ return "/status 处理中";
96
+ });
97
+ const onRuntimeError = input.onRuntimeError ?? (() => { });
98
+ const retryDelayMs = normalizePositiveInteger(input.retryDelayMs, DEFAULT_RETRY_DELAY_MS);
99
+ const longPollTimeoutMs = normalizePositiveInteger(input.longPollTimeoutMs, DEFAULT_LONG_POLL_TIMEOUT_MS);
100
+ let started = false;
101
+ let closed = false;
102
+ let stopController = null;
103
+ let pollingTask = null;
104
+ const poll = async (signal) => {
105
+ let initialized = null;
106
+ while (!signal.aborted) {
107
+ try {
108
+ if (!initialized) {
109
+ const helpers = await withAbort(loadPublicHelpers(input.publicHelpersOptions), signal);
110
+ const latestAccountState = helpers.latestAccountState;
111
+ if (!latestAccountState) {
112
+ throw new Error("missing wechat account state");
113
+ }
114
+ initialized = {
115
+ helpers,
116
+ accountId: latestAccountState.accountId,
117
+ baseUrl: latestAccountState.baseUrl,
118
+ token: latestAccountState.token,
119
+ getUpdatesBuf: typeof latestAccountState.getUpdatesBuf === "string" ? latestAccountState.getUpdatesBuf : "",
120
+ };
121
+ }
122
+ const response = await withAbort(initialized.helpers.getUpdates({
123
+ baseUrl: initialized.baseUrl,
124
+ token: initialized.token,
125
+ get_updates_buf: initialized.getUpdatesBuf,
126
+ timeoutMs: longPollTimeoutMs,
127
+ }), signal);
128
+ // 语义锁定:一旦服务端返回新的 get_updates_buf,立即推进游标;
129
+ // 后续轮询即便失败,也不会回滚到旧 buf。
130
+ if (typeof response.get_updates_buf === "string") {
131
+ initialized.getUpdatesBuf = response.get_updates_buf;
132
+ if (typeof initialized.helpers.persistGetUpdatesBuf === "function") {
133
+ try {
134
+ await withAbort(initialized.helpers.persistGetUpdatesBuf({
135
+ accountId: initialized.accountId,
136
+ getUpdatesBuf: response.get_updates_buf,
137
+ }), signal);
138
+ }
139
+ catch (error) {
140
+ if (isAbortError(error)) {
141
+ return;
142
+ }
143
+ onRuntimeError(error);
144
+ }
145
+ }
146
+ }
147
+ const messages = Array.isArray(response.msgs) ? response.msgs : [];
148
+ for (const message of messages) {
149
+ if (signal.aborted) {
150
+ return;
151
+ }
152
+ const to = toNonEmptyString(message.from_user_id);
153
+ const text = extractMessageText(message);
154
+ if (!to || text.trim().length === 0) {
155
+ continue;
156
+ }
157
+ const parsedCommand = parseWechatSlashCommand(text);
158
+ let replyText = DEFAULT_NON_SLASH_REPLY_TEXT;
159
+ if (parsedCommand) {
160
+ try {
161
+ replyText = await onSlashCommand({
162
+ command: parsedCommand,
163
+ text,
164
+ message,
165
+ });
166
+ }
167
+ catch (error) {
168
+ onRuntimeError(error);
169
+ replyText = DEFAULT_SLASH_HANDLER_ERROR_REPLY_TEXT;
170
+ }
171
+ }
172
+ try {
173
+ await withAbort(initialized.helpers.sendMessageWeixin({
174
+ to,
175
+ text: replyText,
176
+ opts: {
177
+ baseUrl: initialized.baseUrl,
178
+ token: initialized.token,
179
+ contextToken: toNonEmptyString(message.context_token) ?? undefined,
180
+ },
181
+ }), signal);
182
+ }
183
+ catch (error) {
184
+ if (isAbortError(error)) {
185
+ return;
186
+ }
187
+ onRuntimeError(error);
188
+ }
189
+ }
190
+ }
191
+ catch (error) {
192
+ if (isAbortError(error)) {
193
+ return;
194
+ }
195
+ onRuntimeError(error);
196
+ if (signal.aborted || closed) {
197
+ return;
198
+ }
199
+ try {
200
+ await sleep(retryDelayMs, signal);
201
+ }
202
+ catch (sleepError) {
203
+ if (isAbortError(sleepError)) {
204
+ return;
205
+ }
206
+ onRuntimeError(sleepError);
207
+ }
208
+ }
209
+ }
210
+ };
211
+ return {
212
+ start: async () => {
213
+ if (started) {
214
+ return;
215
+ }
216
+ started = true;
217
+ closed = false;
218
+ const controller = new AbortController();
219
+ stopController = controller;
220
+ pollingTask = poll(controller.signal);
221
+ },
222
+ close: async () => {
223
+ if (!started) {
224
+ return;
225
+ }
226
+ closed = true;
227
+ started = false;
228
+ const controller = stopController;
229
+ stopController = null;
230
+ controller?.abort();
231
+ const task = pollingTask;
232
+ pollingTask = null;
233
+ if (task) {
234
+ await task.catch(() => { });
235
+ }
236
+ },
237
+ };
238
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-copilot-account-switcher",
3
- "version": "0.13.6",
3
+ "version": "0.14.1",
4
4
  "description": "GitHub Copilot account switcher plugin for OpenCode",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -33,7 +33,7 @@
33
33
  "auth"
34
34
  ],
35
35
  "engines": {
36
- "node": ">=20.0.0"
36
+ "node": ">=24.0.0"
37
37
  },
38
38
  "files": [
39
39
  "dist/",
@@ -46,6 +46,9 @@
46
46
  "check:copilot-sync": "node scripts/sync-copilot-upstream.mjs --output src/upstream/copilot-plugin.snapshot.ts --check",
47
47
  "sync:codex-snapshot": "node scripts/sync-codex-upstream.mjs --output src/upstream/codex-plugin.snapshot.ts",
48
48
  "check:codex-sync": "node scripts/sync-codex-upstream.mjs --output src/upstream/codex-plugin.snapshot.ts --check",
49
+ "wechat:smoke:self-test": "npm run build && node --input-type=module -e \"import('./dist/wechat/compat/openclaw-smoke.js').then(async (m) => { const results = await m.runOpenClawSmoke('self-test'); console.log(JSON.stringify(results, null, 2)); })\"",
50
+ "wechat:smoke:real-account": "npm run build && node --input-type=module -e \"import('./dist/wechat/compat/openclaw-smoke.js').then(async (m) => { const dryRun = process.argv.includes('--dry-run'); const results = await m.runOpenClawSmoke('real-account', { dryRun }); console.log(JSON.stringify(results, null, 2)); })\" --",
51
+ "wechat:smoke:guided": "npm run build && node dist/wechat/compat/openclaw-guided-smoke.js",
49
52
  "test": "npm run build && node --test",
50
53
  "typecheck": "tsc --noEmit",
51
54
  "prepublishOnly": "npm run build"
@@ -58,8 +61,10 @@
58
61
  "typescript": "^5.0.0"
59
62
  },
60
63
  "dependencies": {
61
- "@opencode-ai/sdk": "^1.2.26",
62
64
  "@opencode-ai/plugin": "^1.2.26",
65
+ "@opencode-ai/sdk": "^1.2.26",
66
+ "@tencent-weixin/openclaw-weixin": "^1.0.3",
67
+ "openclaw": "2026.3.13",
63
68
  "xdg-basedir": "^5.1.0"
64
69
  }
65
70
  }