@wu529778790/open-im 1.10.9-beta.26 → 1.10.9-beta.28

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.
@@ -11,6 +11,7 @@ import { randomBytes } from 'node:crypto';
11
11
  import { createLogger } from '../logger.js';
12
12
  import { jitteredDelay, isFatalReconnectError, SLOW_PROBE_MS } from '../shared/reconnect.js';
13
13
  import { cacheContextToken } from './message-sender.js';
14
+ import { setClawbotContextToken, clearClawbotContextToken } from '../shared/active-chats.js';
14
15
  import { CLAWBOT_POLL_INTERVAL_MS } from '../constants.js';
15
16
  const log = createLogger('ClawBot');
16
17
  const RECONNECT_DELAYS_MS = [3000, 5000, 10000, 20000, 30000];
@@ -97,6 +98,7 @@ function startPolling() {
97
98
  log.warn(`ClawBot fatal error (errcode=${res.errcode}), entering slow-probe mode`);
98
99
  fatal = true;
99
100
  getUpdatesBuf = ''; // session expired, cursor is stale
101
+ clearClawbotContextToken(); // session expired, token is stale
100
102
  updateState('error');
101
103
  scheduleReconnect();
102
104
  return;
@@ -132,9 +134,10 @@ function startPolling() {
132
134
  log.warn('ClawBot message missing from_user_id, skipping');
133
135
  continue;
134
136
  }
135
- // Cache context_token for reply capability
137
+ // Cache context_token for reply capability (in-memory + persisted)
136
138
  if (msg.context_token) {
137
139
  cacheContextToken(chatId, msg.context_token);
140
+ setClawbotContextToken(msg.context_token);
138
141
  }
139
142
  log.info(`ClawBot message: chatId=${chatId}, msgId=${msgId}, content="${content.substring(0, 100)}"`);
140
143
  if (messageHandler) {
@@ -294,6 +297,7 @@ export function stopClawbot() {
294
297
  reconnectTimer = null;
295
298
  }
296
299
  messageHandler = null;
300
+ // Don't clear context_token here — it's persisted for startup notifications
297
301
  updateState('disconnected');
298
302
  log.info('ClawBot client stopped');
299
303
  }
@@ -8,6 +8,7 @@ import { createLogger } from '../logger.js';
8
8
  import { splitLongContent, toReplyPlainText } from '../shared/utils.js';
9
9
  import { MAX_CLAWBOT_MESSAGE_LENGTH } from '../constants.js';
10
10
  import { getChannelState } from './client.js';
11
+ import { getActiveChatId, getClawbotContextToken } from '../shared/active-chats.js';
11
12
  const log = createLogger('ClawBotSender');
12
13
  let apiUrl = 'https://ilinkai.weixin.qq.com';
13
14
  let apiToken = '';
@@ -16,6 +17,13 @@ const contextTokenCache = new Map();
16
17
  export function initClawBotSender(url, token) {
17
18
  apiUrl = url;
18
19
  apiToken = token;
20
+ // Restore persisted context_token for the active chat (survives restarts)
21
+ const activeChatId = getActiveChatId('clawbot');
22
+ const savedToken = getClawbotContextToken();
23
+ if (activeChatId && savedToken) {
24
+ contextTokenCache.set(activeChatId, savedToken);
25
+ log.info(`Restored context_token for chatId=${activeChatId}`);
26
+ }
19
27
  }
20
28
  /** Cache a context_token for a chatId (called when receiving messages) */
21
29
  export function cacheContextToken(chatId, token) {
@@ -45,8 +53,9 @@ async function postMessage(chatId, text, contextToken) {
45
53
  }
46
54
  const token = contextToken ?? getCachedContextToken(chatId);
47
55
  if (!token) {
48
- log.warn(`ClawBot no context_token for chatId=${chatId}, cannot send reply`);
49
- return false;
56
+ const errMsg = `ClawBot no context_token for chatId=${chatId}, cannot send reply`;
57
+ log.warn(errMsg);
58
+ throw new Error(errMsg);
50
59
  }
51
60
  try {
52
61
  const url = `${apiUrl}/ilink/bot/sendmessage`;
@@ -10,4 +10,10 @@ export declare function getActiveChatId(platform: 'dingtalk' | 'feishu' | 'qq' |
10
10
  export declare function setActiveChatId(platform: 'dingtalk' | 'feishu' | 'qq' | 'telegram' | 'wework' | 'workbuddy' | 'clawbot', chatId: string): void;
11
11
  export declare function getDingTalkActiveTarget(): DingTalkActiveTarget | undefined;
12
12
  export declare function setDingTalkActiveTarget(target: Omit<DingTalkActiveTarget, 'updatedAt'>): void;
13
+ /** Get persisted ClawBot context_token (survives restarts) */
14
+ export declare function getClawbotContextToken(): string | undefined;
15
+ /** Persist ClawBot context_token (called when receiving messages with context_token) */
16
+ export declare function setClawbotContextToken(token: string): void;
17
+ /** Clear persisted ClawBot context_token (e.g. on session expiry) */
18
+ export declare function clearClawbotContextToken(): void;
13
19
  export declare function flushActiveChats(): void;
@@ -76,6 +76,24 @@ export function setDingTalkActiveTarget(target) {
76
76
  }
77
77
  scheduleSave();
78
78
  }
79
+ /** Get persisted ClawBot context_token (survives restarts) */
80
+ export function getClawbotContextToken() {
81
+ return data.clawbotContextToken;
82
+ }
83
+ /** Persist ClawBot context_token (called when receiving messages with context_token) */
84
+ export function setClawbotContextToken(token) {
85
+ if (data.clawbotContextToken === token)
86
+ return;
87
+ data.clawbotContextToken = token;
88
+ scheduleSave();
89
+ }
90
+ /** Clear persisted ClawBot context_token (e.g. on session expiry) */
91
+ export function clearClawbotContextToken() {
92
+ if (!data.clawbotContextToken)
93
+ return;
94
+ data.clawbotContextToken = undefined;
95
+ scheduleSave();
96
+ }
79
97
  export function flushActiveChats() {
80
98
  if (saveTimer) {
81
99
  clearTimeout(saveTimer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wu529778790/open-im",
3
- "version": "1.10.9-beta.26",
3
+ "version": "1.10.9-beta.28",
4
4
  "description": "Multi-platform IM bridge for AI CLI tools (Claude, Codex, CodeBuddy)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",