@xmanrui/dsh-im 5.6.0 → 5.8.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 (41) hide show
  1. package/README.en.md +22 -6
  2. package/README.md +22 -6
  3. package/assets/sponsors/88api.jpg +0 -0
  4. package/lib/client.js +361 -159
  5. package/lib/index.js +323 -321
  6. package/package.json +1 -1
  7. package/plugin-src/client/channels/feishu/api.js +5 -0
  8. package/plugin-src/client/channels/feishu/conversation-directory-settings.js +73 -0
  9. package/plugin-src/client/channels/feishu/index.js +1 -0
  10. package/plugin-src/client/delivery-settings.js +13 -0
  11. package/plugin-src/client/i18n-ru.js +18 -0
  12. package/plugin-src/client/i18n.js +18 -0
  13. package/plugin-src/client/session-channel-logos.js +8 -0
  14. package/plugin-src/client/styles.js +6 -0
  15. package/plugin-src/client/workspace-editor.js +6 -5
  16. package/plugin-src/host/channels/feishu/rpc.mjs +25 -0
  17. package/plugin-src/host/channels/shared/workspace-rpc.mjs +2 -0
  18. package/plugin-src/host/delivery-suggestions.mjs +2 -0
  19. package/plugin-src/host/update-service.mjs +44 -13
  20. package/scripts/verify-package.mjs +17 -6
  21. package/src/channels/feishu/bridge.mjs +29 -12
  22. package/src/channels/feishu/feishu-runtime.mjs +13 -1
  23. package/src/channels/feishu/message-utils.mjs +1 -0
  24. package/src/channels/qq/markdown-reply.mjs +5 -2
  25. package/src/channels/qq/qq-bridge.mjs +41 -2
  26. package/src/channels/shared/bot-workspace-store.mjs +248 -0
  27. package/src/channels/shared/conversation-directory-ensure.mjs +46 -0
  28. package/src/channels/shared/conversation-directory.mjs +29 -0
  29. package/src/channels/shared/harness-client.mjs +72 -2
  30. package/src/channels/shared/i18n-en/diagnostics.mjs +2 -0
  31. package/src/channels/shared/i18n-en/shared-a.mjs +5 -0
  32. package/src/channels/shared/i18n-ru/diagnostics.mjs +2 -0
  33. package/src/channels/shared/i18n-ru/shared-a.mjs +5 -0
  34. package/src/channels/shared/message-failure.mjs +8 -0
  35. package/src/channels/shared/new-command.mjs +7 -0
  36. package/src/channels/shared/semantic/reply-reference.mjs +25 -5
  37. package/src/channels/shared/text-harness-bridge.mjs +3 -2
  38. package/src/channels/shared/workspace-command.mjs +22 -9
  39. package/src/channels/slack/slack-api.mjs +21 -5
  40. package/src/channels/slack/slack-runtime.mjs +69 -20
  41. package/src/channels/wecom/wecom-controller.mjs +89 -1
@@ -12,6 +12,27 @@ import { publicMessageFailure } from '../shared/message-failure.mjs';
12
12
  const ACTIVE_ATTEMPT_STATES = new Set(['pending', 'connecting']);
13
13
  const TERMINAL_ATTEMPT_STATES = new Set(['connected', 'failed', 'cancelled', 'expired']);
14
14
 
15
+ // A runtime may legitimately sit in 'connecting' while the SDK retries with
16
+ // backoff: every retry emits 'reconnecting', which refreshes lastCheckedAt at
17
+ // least once per reconnect cycle. After a server-side disconnect, however, the
18
+ // SDK marks the close manual and stops for good without emitting anything
19
+ // else, so 'connecting' goes stale forever and initialize() below would keep
20
+ // skipping the bot on every supervisor pass. A 'connecting' status whose last
21
+ // transition is older than this grace is treated as stalled and restarted.
22
+ const STALLED_CONNECTING_GRACE_MS = 120_000;
23
+ // Two deployments holding the same credential keep kicking each other, and
24
+ // every restart authenticates successfully (so failure-based backoff never
25
+ // triggers) while only surviving one reconnect cycle. A connection that
26
+ // dropped again within this window of being established counts as short-lived
27
+ // and its restarts are paced; a connection that stays healthy this long
28
+ // proves the bot is not in a kick loop and clears the pacing.
29
+ const STABLE_CONNECTION_MS = 300_000;
30
+ // Automatic restarts are paced per bot: when the same credential is held by
31
+ // another connection, each side kicks the other, and restarting at supervisor
32
+ // cadence would storm the provider and fight the legitimate holder.
33
+ const RESTART_BACKOFF_INITIAL_MS = 60_000;
34
+ const RESTART_BACKOFF_MAX_MS = 600_000;
35
+
15
36
  function cleanString(value) {
16
37
  return typeof value === 'string' && value.trim() ? value.trim() : null;
17
38
  }
@@ -49,6 +70,8 @@ export class WecomController {
49
70
  #transitions = new Map();
50
71
  #revision = 0;
51
72
  #closed = false;
73
+ #now;
74
+ #restartBackoff = new Map();
52
75
 
53
76
  constructor({
54
77
  qrAuth,
@@ -57,6 +80,7 @@ export class WecomController {
57
80
  createRuntime,
58
81
  deleteState = async () => {},
59
82
  logger = console,
83
+ nowImpl = Date.now,
60
84
  }) {
61
85
  if (!qrAuth || typeof qrAuth.start !== 'function' || typeof qrAuth.poll !== 'function') {
62
86
  throw new TypeError('Enterprise WeChat QR auth is required');
@@ -70,12 +94,14 @@ export class WecomController {
70
94
  throw new TypeError('WecomController requires a config store');
71
95
  }
72
96
  if (typeof createRuntime !== 'function') throw new TypeError('createRuntime is required');
97
+ if (typeof nowImpl !== 'function') throw new TypeError('nowImpl must be a function');
73
98
  this.#qrAuth = qrAuth;
74
99
  this.#credentials = credentials;
75
100
  this.#configStore = configStore;
76
101
  this.#createRuntime = createRuntime;
77
102
  this.#deleteState = deleteState;
78
103
  this.#logger = logger;
104
+ this.#now = nowImpl;
79
105
  this.#diagnostics = createConnectionDiagnostics({ channel: 'wecom', logger });
80
106
  }
81
107
 
@@ -91,17 +117,47 @@ export class WecomController {
91
117
  for (const config of this.#configStore.list()) {
92
118
  await this.#withBotTransition(config.botId, async () => {
93
119
  const existing = this.#runtimes.get(config.botId)?.status;
94
- if (this.#closed || existing?.ready || existing?.wecomConnectionState === 'connecting') return;
120
+ if (this.#closed || existing?.ready) {
121
+ // A single successful authenticate does not prove the bot is back
122
+ // for good: in a kick loop every restart authenticates and is
123
+ // dropped again one cycle later. Only a connection that has now
124
+ // held healthy past the stability window clears the pacing.
125
+ if (existing?.ready && this.#connectionStable(existing)) this.#restartBackoff.delete(config.botId);
126
+ return;
127
+ }
128
+ if (existing?.wecomConnectionState === 'connecting' && !this.#stalledConnecting(existing)) return;
129
+ const stalled = this.#stalledConnecting(existing);
130
+ const shortLived = stalled && this.#shortLivedConnecting(existing);
131
+ const backoff = this.#restartBackoff.get(config.botId);
132
+ if (backoff && backoff.nextAttemptAt > this.#now()) return;
133
+ if (shortLived && !backoff) {
134
+ // First short-lived drop: schedule one pacing window before
135
+ // contending for the credential again, instead of grabbing it back
136
+ // at supervisor cadence and immediately kicking the other side.
137
+ this.#scheduleRestartBackoff(config.botId);
138
+ this.#logger.warn?.(`[dsh-im:wecom] bot ${config.botId} connection was short-lived; deferring reconnect`);
139
+ return;
140
+ }
95
141
  try {
96
142
  const secret = await this.#resolveSecret(config.secretRef);
97
143
  if (!secret) {
98
144
  this.#errors.set(config.botId, safeError('missing-secret', t('企业微信机器人凭据缺失,请移除后重新扫码。')));
99
145
  return;
100
146
  }
147
+ if (stalled) {
148
+ if (shortLived) {
149
+ // The pacing window has elapsed; contend again. The schedule
150
+ // above doubles the next window first, so a renewed kick loop
151
+ // slows down instead of cycling at supervisor cadence.
152
+ this.#scheduleRestartBackoff(config.botId);
153
+ }
154
+ this.#logger.warn?.(`[dsh-im:wecom] bot ${config.botId} connection stalled in 'connecting'; restarting`);
155
+ }
101
156
  await this.#startRuntime(config, secret);
102
157
  this.#errors.delete(config.botId);
103
158
  } catch (error) {
104
159
  this.#errors.set(config.botId, this.#failure(error, 'connection-failed', t('企业微信连接未就绪,插件会自动重试。')));
160
+ this.#scheduleRestartBackoff(config.botId);
105
161
  this.#logger.warn?.(`[dsh-im:wecom] bot ${config.botId} failed to initialize`);
106
162
  } finally {
107
163
  this.#touch();
@@ -111,6 +167,38 @@ export class WecomController {
111
167
  return this.status();
112
168
  }
113
169
 
170
+ #stalledConnecting(status) {
171
+ if (status?.wecomConnectionState !== 'connecting') return false;
172
+ const { lastCheckedAt } = status;
173
+ return typeof lastCheckedAt === 'number' && this.#now() - lastCheckedAt > STALLED_CONNECTING_GRACE_MS;
174
+ }
175
+
176
+ // Whether the connection that just went silent had been established for
177
+ // less than the stability window: the signature of a credential contested
178
+ // by another deployment, as opposed to an ordinary healthy connection that
179
+ // dropped. Based on the timestamps of the drop (lastCheckedAt) and of the
180
+ // last successful authenticate (lastConnectedAt), so it does not change as
181
+ // time passes without a new connection.
182
+ #shortLivedConnecting(status) {
183
+ const { lastCheckedAt, lastConnectedAt } = status ?? {};
184
+ return typeof lastCheckedAt === 'number' && typeof lastConnectedAt === 'number'
185
+ && lastCheckedAt > lastConnectedAt
186
+ && lastCheckedAt - lastConnectedAt < STABLE_CONNECTION_MS;
187
+ }
188
+
189
+ #connectionStable(status) {
190
+ const { lastConnectedAt } = status ?? {};
191
+ return typeof lastConnectedAt === 'number' && this.#now() - lastConnectedAt >= STABLE_CONNECTION_MS;
192
+ }
193
+
194
+ #scheduleRestartBackoff(botId) {
195
+ const entry = this.#restartBackoff.get(botId) ?? { consecutive: 0, nextAttemptAt: 0 };
196
+ entry.consecutive += 1;
197
+ entry.nextAttemptAt = this.#now()
198
+ + Math.min(RESTART_BACKOFF_INITIAL_MS * 2 ** (entry.consecutive - 1), RESTART_BACKOFF_MAX_MS);
199
+ this.#restartBackoff.set(botId, entry);
200
+ }
201
+
114
202
  async startProvisioning() {
115
203
  if (this.#closed) throw new Error('Enterprise WeChat controller is closed');
116
204
  if (this.#activeAttemptId) await this.cancelProvisioning(this.#activeAttemptId);