@xmanrui/dsh-im 4.19.2 → 4.20.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 (40) hide show
  1. package/README.en.md +78 -0
  2. package/README.md +78 -0
  3. package/lib/client.js +594 -217
  4. package/lib/index.js +279 -276
  5. package/package.json +9 -1
  6. package/plugin-src/client/channels/weixin/api.js +35 -17
  7. package/plugin-src/client/channels/weixin/connection-error.js +68 -0
  8. package/plugin-src/client/channels/weixin/index.js +36 -12
  9. package/plugin-src/client/i18n.js +2 -0
  10. package/plugin-src/host/channels/shared/startup.mjs +7 -4
  11. package/plugin-src/host/channels/weixin/connection-supervisor.mjs +13 -1
  12. package/plugin-src/host/channels/weixin/index.mjs +12 -3
  13. package/plugin-src/host/channels/weixin/production.mjs +53 -3
  14. package/plugin-src/host/channels/weixin/rpc.mjs +22 -17
  15. package/src/channels/dingtalk/dingtalk-bridge.mjs +4 -1
  16. package/src/channels/dingtalk/dingtalk-menu.mjs +8 -4
  17. package/src/channels/feishu/bridge.mjs +44 -13
  18. package/src/channels/qq/qq-bridge.mjs +12 -4
  19. package/src/channels/qq/qq-menu.mjs +11 -8
  20. package/src/channels/shared/bot-workspace-store.mjs +532 -40
  21. package/src/channels/shared/command-catalog.mjs +5 -0
  22. package/src/channels/shared/compact-command.mjs +14 -4
  23. package/src/channels/shared/control-command.mjs +1 -1
  24. package/src/channels/shared/deferred-delivery-coordinator.mjs +1 -1
  25. package/src/channels/shared/history-command.mjs +1 -1
  26. package/src/channels/shared/i18n-en/shared-a.mjs +41 -0
  27. package/src/channels/shared/i18n-en/weixin.mjs +2 -0
  28. package/src/channels/shared/model-command.mjs +5 -3
  29. package/src/channels/shared/workspace-command.mjs +114 -9
  30. package/src/channels/shared/workspace-session.mjs +55 -5
  31. package/src/channels/telegram/telegram-runtime.mjs +1 -1
  32. package/src/channels/wecom/wecom-bridge.mjs +2 -2
  33. package/src/channels/weixin/connection-error.en.mjs +116 -0
  34. package/src/channels/weixin/connection-error.mjs +204 -0
  35. package/src/channels/weixin/diagnostic-details.mjs +40 -0
  36. package/src/channels/weixin/state-store.mjs +4 -3
  37. package/src/channels/weixin/weixin-api.mjs +20 -8
  38. package/src/channels/weixin/weixin-bridge.mjs +3 -2
  39. package/src/channels/weixin/weixin-controller.mjs +133 -104
  40. package/src/channels/weixin/weixin-runtime.mjs +35 -24
@@ -35,6 +35,11 @@ export const SHARED_COMMAND_CATALOG = Object.freeze([
35
35
  defineCatalogCommand('workspace', '切换工作区', [
36
36
  '/workspace 工作区序号或绝对路径 切换工作区',
37
37
  ], { aliases: ['ws'] }),
38
+ defineCatalogCommand('conv', '设置当前对话专属工作区', [
39
+ '/conv 或 /conversation 查看当前对话工作区',
40
+ '/conv 工作区绝对路径或序号 设置当前对话专属工作区',
41
+ '/conv clear 清除专属工作区,回到 bot 默认工作区',
42
+ ], { aliases: ['conversation', 'thread'] }),
38
43
  defineCatalogCommand('workspacelist', '列出工作区绝对路径', [
39
44
  '/workspacelist 列出工作区绝对路径',
40
45
  '/ws、/wsl、/workspaces 工作区命令别名',
@@ -80,11 +80,21 @@ export async function runCompactCommand(text, harness, state, conversationKey, o
80
80
  if (typeof sessionId !== 'string' || !sessionId) {
81
81
  return commandResult(t('当前聊天还没有可压缩的会话,请先发送一条消息。'));
82
82
  }
83
- if (typeof harness?.executeCommand !== 'function') {
84
- return commandResult(t('当前机器人暂不支持上下文压缩。'));
85
- }
86
83
  try {
87
- const execution = await harness.executeCommand(sessionId, '/compact', options);
84
+ let execution;
85
+ if (typeof harness?.workspaceSession === 'function') {
86
+ const session = harness.workspaceSession(sessionId, conversationKey);
87
+ if (typeof session?.executeCommand !== 'function') {
88
+ return commandResult(t('当前机器人暂不支持上下文压缩。'));
89
+ }
90
+ execution = await session.executeCommand('/compact', options);
91
+ } else {
92
+ // Legacy Harnesses have no scoped Session handle to carry the route fence.
93
+ if (typeof harness?.executeCommand !== 'function') {
94
+ return commandResult(t('当前机器人暂不支持上下文压缩。'));
95
+ }
96
+ execution = await harness.executeCommand(sessionId, '/compact', options);
97
+ }
88
98
  if (execution === undefined) {
89
99
  return commandResult(t('当前 Harness 未注册 /compact 命令,请确认上下文压缩组件已启用。'));
90
100
  }
@@ -24,7 +24,7 @@ function boundSession(harness, state, key) {
24
24
  if (typeof harness?.workspaceSession !== 'function') {
25
25
  throw new TypeError('Harness does not support workspace sessions');
26
26
  }
27
- const session = harness.workspaceSession(sessionId);
27
+ const session = harness.workspaceSession(sessionId, key);
28
28
  if (!session || typeof session !== 'object') {
29
29
  throw new TypeError('Harness returned an invalid workspace session');
30
30
  }
@@ -128,7 +128,7 @@ export function createDeferredDeliveryCoordinator({
128
128
  entry = { ...entry, turn: outcome.turn };
129
129
  }
130
130
  if (stopping) {
131
- const session = harness.workspaceSession?.(entry.sessionId);
131
+ const session = harness.workspaceSession?.(entry.sessionId, entry.key);
132
132
  const stopped = typeof session?.stopDeferredTurn === 'function'
133
133
  ? await session.stopDeferredTurn({ turn: entry.turn, promptRpcId: entry.promptRpcId }, {
134
134
  signal: activeSignal, isCurrent: () => bound(entry),
@@ -124,7 +124,7 @@ export async function runHistoryCommand(text, harness, state, key, {
124
124
  }
125
125
 
126
126
  try {
127
- const session = harness?.workspaceSession?.(sessionId);
127
+ const session = harness?.workspaceSession?.(sessionId, key);
128
128
  if (typeof session?.readHistory !== 'function') {
129
129
  return commandResult(t('当前 Harness 暂不支持读取会话历史。'));
130
130
  }
@@ -125,6 +125,47 @@ export default {
125
125
  '/compact 压缩当前会话的较早上下文': '/compact Compact the earlier context of the current session',
126
126
  '/workspace 工作区序号或绝对路径 切换工作区':
127
127
  '/workspace <workspace index or absolute path> Switch workspace',
128
+ '设置当前对话专属工作区': 'Set a workspace dedicated to this conversation',
129
+ '/conv 或 /conversation 查看当前对话工作区':
130
+ '/conv or /conversation Show the workspace of this conversation',
131
+ '/conv 工作区绝对路径或序号 设置当前对话专属工作区':
132
+ '/conv <workspace absolute path or index> Set a workspace dedicated to this conversation',
133
+ '/conv clear 清除专属工作区,回到 bot 默认工作区':
134
+ '/conv clear Clear the dedicated workspace and fall back to the bot default',
135
+ '对话专属:/conv 工作区序号或绝对路径(仅影响当前对话)':
136
+ 'Dedicated to this conversation: /conv <workspace index or absolute path> (affects this conversation only)',
137
+ '当前对话工作区:{workspace}': 'This conversation uses the workspace: {workspace}',
138
+ '状态:已为该对话显式绑定,之后修改 bot 默认工作区不会影响本对话。':
139
+ 'Status: explicitly bound for this conversation; changing the bot default workspace later will not affect it.',
140
+ '状态:未显式绑定,当前跟随 bot 默认工作区。':
141
+ 'Status: not explicitly bound; this conversation currently follows the bot default workspace.',
142
+ '当前对话工作区已切换为:{workspace}': 'This conversation now uses the workspace: {workspace}',
143
+ '已清除对话专属工作区,当前使用 bot 默认工作区:{workspace}(之后默认工作区的变化会同步到本对话)':
144
+ 'Cleared the dedicated workspace. This conversation now uses the bot default: {workspace} (later changes to that default follow here as well)',
145
+ '可切换的工作区({count}):': 'Available workspaces ({count}):',
146
+ '用法:/conv 工作区序号或绝对路径': 'Usage: /conv <workspace index or absolute path>',
147
+ '清除:/conv clear': 'Clear: /conv clear',
148
+ '{message}\n用法:/conv 工作区绝对路径': '{message}\nUsage: /conv <workspace absolute path>',
149
+ '当前机器人暂不支持按对话设置专属工作区。':
150
+ 'This bot does not support per-conversation workspaces yet.',
151
+ '当前机器人暂不支持设置对话工作区。':
152
+ 'This bot does not support setting a conversation workspace yet.',
153
+ '当前消息缺少可设置的对话上下文。':
154
+ 'This message has no conversation context to bind a workspace to.',
155
+ '暂时无法读取当前对话工作区,请稍后重试。':
156
+ 'This conversation workspace is temporarily unavailable. Please try again later.',
157
+ '暂时无法清除对话工作区,请稍后重试。':
158
+ 'The conversation workspace could not be cleared right now. Please try again later.',
159
+ '机器人正在移除或已重新接入,无法读取对话工作区。':
160
+ 'The bot is being removed or was reconnected, so the conversation workspace cannot be read.',
161
+ '机器人正在移除或已重新接入,无法清除对话工作区。':
162
+ 'The bot is being removed or was reconnected, so the conversation workspace cannot be cleared.',
163
+ '机器人正在移除或已重新接入,无法切换对话工作区。':
164
+ 'The bot is being removed or was reconnected, so the conversation workspace cannot be switched.',
165
+ '不带工作区参数时,/sessionlist 默认列出当前对话的有效工作区。':
166
+ 'Without a workspace argument, /sessionlist lists the workspace this conversation effectively uses.',
167
+ '不带工作区参数时,默认列出当前对话的有效工作区(未设置对话专属工作区时即 bot 默认工作区)。':
168
+ 'Without a workspace argument it lists the workspace this conversation effectively uses (the bot default when no conversation workspace is set).',
128
169
  '/workspacelist 列出工作区绝对路径': '/workspacelist List absolute workspace paths',
129
170
  '/ws、/wsl、/workspaces 工作区命令别名': '/ws, /wsl, /workspaces Workspace command aliases',
130
171
  '/sessionlist [工作区序号或绝对路径] 列出会话 ID 和标题':
@@ -1,5 +1,7 @@
1
+ import diagnostics from '../../weixin/connection-error.en.mjs';
1
2
  // English translations (weixin area). Keys are exact Chinese literals passed to t().
2
3
  export default {
4
+ ...diagnostics,
3
5
  // weixin-bridge.mjs
4
6
  '微信已连接 DeepSeek Harness。': 'WeChat is connected to DeepSeek Harness.',
5
7
  '结果文件「{name}」已生成,但微信机器人当前没有文件消息发送权限,请检查机器人文件消息能力。': 'The result file "{name}" was generated, but the WeChat bot currently has no permission to send file messages. Please check the bot\'s file messaging capability.',
@@ -443,7 +443,7 @@ async function boundSession(harness, state, key, options) {
443
443
  if (typeof harness?.workspaceSession !== 'function') {
444
444
  throw new TypeError('Harness does not support workspace sessions');
445
445
  }
446
- const session = harness.workspaceSession(sessionId);
446
+ const session = harness.workspaceSession(sessionId, key);
447
447
  if (!session || typeof session.sessionExists !== 'function') {
448
448
  throw new TypeError('Harness returned an invalid workspace session');
449
449
  }
@@ -705,11 +705,13 @@ export async function runModelCommand(text, harness, state, key, options = {}) {
705
705
  throw new TypeError('Harness cannot create a conversation session');
706
706
  }
707
707
  // An explicit choice must remain usable even when the saved bot model expires.
708
- const sessionId = await harness.createSession({ ...requestOptions, inheritBotModel: false });
708
+ const sessionId = await harness.createSession({
709
+ ...requestOptions, inheritBotModel: false, conversationKey: key,
710
+ });
709
711
  if (typeof sessionId !== 'string' || !sessionId) {
710
712
  throw new TypeError('Harness returned an invalid session id');
711
713
  }
712
- const session = harness.workspaceSession(sessionId);
714
+ const session = harness.workspaceSession(sessionId, key);
713
715
  applied = await selectAndVerifyModel(session, selection, requestOptions);
714
716
  const currentSessionId = state.sessionFor(key);
715
717
  if (typeof currentSessionId === 'string' && currentSessionId) {
@@ -6,6 +6,7 @@ import { WORKSPACE_SESSION_STALE } from './workspace-session.mjs';
6
6
 
7
7
  const WORKSPACE_COMMAND = /^\/(?:workspace|ws)(?:\s+([\s\S]+))?$/i;
8
8
  const WORKSPACE_LIST_COMMAND = /^\/(?:workspacelist|workspaces|wsl)(?:\s+([\s\S]+))?$/i;
9
+ const THREAD_WORKSPACE_COMMAND = /^\/(?:conv|conversation|thread)(?:\s+([\s\S]+))?$/i;
9
10
  const SESSION_LIST_COMMAND = /^\/(?:sessionlist|sessions)(?:\s+([\s\S]+))?$/i;
10
11
  const SESSION_BIND_PREFIX = /^\/session(?=$|\s)/i;
11
12
  const SESSION_BIND_COMMAND = /^\/session[ \t]+([^\s]+)$/i;
@@ -21,6 +22,7 @@ const SESSION_LIST_USAGE = [
21
22
  '/sessionlist --limit N 列出当前工作区前 N 个会话(N 为正整数)',
22
23
  '/sessionlist 工作区序号 按 /workspacelist 序号列出会话',
23
24
  '/sessionlist 工作区绝对路径 列出指定工作区会话',
25
+ '不带工作区参数时,默认列出当前对话的有效工作区(未设置对话专属工作区时即 bot 默认工作区)。',
24
26
  ].join('\n');
25
27
 
26
28
  function commandResult(message, messages = [message]) {
@@ -158,7 +160,9 @@ async function runWorkspaceListCommand(match, harness) {
158
160
  )),
159
161
  '',
160
162
  t('切换用法:/workspace 工作区序号或绝对路径'),
163
+ t('对话专属:/conv 工作区序号或绝对路径(仅影响当前对话)'),
161
164
  t('查看会话:/sessionlist 工作区序号或绝对路径'),
165
+ t('不带工作区参数时,/sessionlist 默认列出当前对话的有效工作区。'),
162
166
  ];
163
167
  const message = lines.join('\n');
164
168
  return commandResult(message, splitWorkspaceCommandMessage(message));
@@ -170,8 +174,22 @@ async function runWorkspaceListCommand(match, harness) {
170
174
  }
171
175
  }
172
176
 
177
+ async function conversationEffectiveWorkspace(harness, conversationKey) {
178
+ if (typeof harness?.currentConversationWorkspace !== 'function'
179
+ || typeof conversationKey !== 'string' || !conversationKey) return null;
180
+ const [current] = await existingWorkspacePaths([harness.currentConversationWorkspace(conversationKey)]);
181
+ harness.assertWorkspaceScope?.();
182
+ return current ?? null;
183
+ }
184
+
173
185
  export async function resolveSessionListWorkspace(selector, harness, options = {}) {
174
186
  if (!selector) {
187
+ // With no explicit selector the listing follows the conversation's effective
188
+ // workspace, so /conv B makes /sessionlist list B instead of the bot default.
189
+ if (options.conversationKey) {
190
+ const effective = await conversationEffectiveWorkspace(harness, options.conversationKey);
191
+ if (effective) return { workspace: effective };
192
+ }
175
193
  if (typeof harness?.currentWorkspace !== 'function') {
176
194
  return { error: t('当前机器人没有可用的工作区。') };
177
195
  }
@@ -242,21 +260,23 @@ function sessionListMessage(workspace, sessions, { currentWorkspace = false } =
242
260
  ].join('\n');
243
261
  }
244
262
 
245
- async function currentSessionListWorkspace(harness) {
263
+ async function currentSessionListWorkspace(harness, conversationKey) {
264
+ const effective = await conversationEffectiveWorkspace(harness, conversationKey);
265
+ if (effective) return effective;
246
266
  if (typeof harness?.currentWorkspace !== 'function') return null;
247
267
  const [current] = await existingWorkspacePaths([harness.currentWorkspace()]);
248
268
  harness.assertWorkspaceScope?.();
249
269
  return current ?? null;
250
270
  }
251
271
 
252
- async function runSessionListCommand(match, harness) {
272
+ async function runSessionListCommand(match, harness, conversationKey) {
253
273
  const request = parseSessionListArgument(match[1]);
254
274
  if (request.error) return commandResult(request.error);
255
275
  if (typeof harness?.listWorkspaceSessions !== 'function') {
256
276
  return commandResult(t('当前机器人暂不支持列出工作区会话。'));
257
277
  }
258
278
  try {
259
- const resolved = await resolveSessionListWorkspace(request.selector, harness);
279
+ const resolved = await resolveSessionListWorkspace(request.selector, harness, { conversationKey });
260
280
  if (resolved.error) return commandResult(resolved.error);
261
281
  const listed = await harness.listWorkspaceSessions(resolved.workspace);
262
282
  if (!listed || !Array.isArray(listed.sessions)) {
@@ -264,7 +284,7 @@ async function runSessionListCommand(match, harness) {
264
284
  }
265
285
  harness.assertWorkspaceScope?.();
266
286
  const workspace = normalizedWorkspacePath(listed.workspace) ?? resolved.workspace;
267
- const currentWorkspace = await currentSessionListWorkspace(harness);
287
+ const currentWorkspace = await currentSessionListWorkspace(harness, conversationKey);
268
288
  const sessions = request.limit === null
269
289
  ? listed.sessions
270
290
  : listed.sessions.slice(0, request.limit);
@@ -311,13 +331,15 @@ async function runSessionBindCommand(command, harness, conversationKey) {
311
331
  const match = SESSION_BIND_COMMAND.exec(command);
312
332
  let sessionId = match?.[1];
313
333
  if (typeof sessionId === 'string' && /^\d+$/u.test(sessionId)) {
314
- // 序号模式:把 /session N 解析成当前工作区会话列表中的第 N 个会话
334
+ // Use the same effective-workspace resolver as /sessionlist, so its indexes
335
+ // cannot select a session from the bot default after /conv sets an override.
315
336
  if (typeof harness?.listWorkspaceSessions !== 'function'
316
- || typeof harness?.currentWorkspace !== 'function') {
337
+ || (typeof harness?.currentWorkspace !== 'function'
338
+ && typeof harness?.currentConversationWorkspace !== 'function')) {
317
339
  return commandResult(t('当前机器人暂不支持按序号绑定,请使用 /session Session ID。'));
318
340
  }
319
341
  try {
320
- const selected = await selectedWorkspacePath(harness.currentWorkspace());
342
+ const selected = await resolveSessionListWorkspace('', harness, { conversationKey });
321
343
  if (selected.error) return commandResult(selected.error);
322
344
  const listed = await harness.listWorkspaceSessions(selected.workspace);
323
345
  if (!listed || !Array.isArray(listed.sessions)) {
@@ -371,14 +393,96 @@ async function runSessionBindCommand(command, harness, conversationKey) {
371
393
  }
372
394
  }
373
395
 
396
+ async function runConversationWorkspaceCommand(command, harness, conversationKey) {
397
+ if (typeof harness?.currentConversationWorkspace !== 'function') {
398
+ return commandResult(t('当前机器人暂不支持按对话设置专属工作区。'));
399
+ }
400
+ if (typeof conversationKey !== 'string' || !conversationKey) {
401
+ return commandResult(t('当前消息缺少可设置的对话上下文。'));
402
+ }
403
+ const argument = THREAD_WORKSPACE_COMMAND.exec(command)?.[1]?.trim() ?? '';
404
+ if (!argument) {
405
+ try {
406
+ const current = harness.currentConversationWorkspace(conversationKey);
407
+ harness.assertWorkspaceScope?.();
408
+ const lines = [t('当前对话工作区:{workspace}', { workspace: current })];
409
+ // 明确区分「显式绑定」与「回落 bot 默认」,否则用户无法判断之后改默认值会不会影响本对话。
410
+ const bound = typeof harness?.hasConversationWorkspaceOverride === 'function'
411
+ && harness.hasConversationWorkspaceOverride(conversationKey) === true;
412
+ lines.push(bound
413
+ ? t('状态:已为该对话显式绑定,之后修改 bot 默认工作区不会影响本对话。')
414
+ : t('状态:未显式绑定,当前跟随 bot 默认工作区。'));
415
+ // 顺带列出可切换的工作区,省得先跑一次 /workspacelist 再回来切。
416
+ try {
417
+ const { paths } = await workspacePathSnapshot(harness);
418
+ if (paths.length > 0) {
419
+ lines.push('', t('可切换的工作区({count}):', { count: paths.length }));
420
+ for (const [index, workspace] of paths.entries()) {
421
+ lines.push(`${index + 1}. ${workspace}${workspace === current ? t('(当前)') : ''}`);
422
+ }
423
+ }
424
+ } catch { /* 列表拿不到不影响主信息 */ }
425
+ lines.push('', t('用法:/conv 工作区序号或绝对路径'), t('清除:/conv clear'));
426
+ const message = lines.join('\n');
427
+ return commandResult(message, splitWorkspaceCommandMessage(message));
428
+ } catch (error) {
429
+ if (error?.code === 'workspace-bot-not-found') {
430
+ return commandResult(t('机器人正在移除或已重新接入,无法读取对话工作区。'));
431
+ }
432
+ return commandResult(t('暂时无法读取当前对话工作区,请稍后重试。'));
433
+ }
434
+ }
435
+ if (/^(?:--default|clear)$/iu.test(argument)) {
436
+ try {
437
+ const current = await harness.clearConversationWorkspace(conversationKey);
438
+ harness.assertWorkspaceScope?.();
439
+ return commandResult(t('已清除对话专属工作区,当前使用 bot 默认工作区:{workspace}(之后默认工作区的变化会同步到本对话)', { workspace: current }));
440
+ } catch (error) {
441
+ if (error?.code === 'workspace-bot-not-found') {
442
+ return commandResult(t('机器人正在移除或已重新接入,无法清除对话工作区。'));
443
+ }
444
+ return commandResult(t('暂时无法清除对话工作区,请稍后重试。'));
445
+ }
446
+ }
447
+ if (typeof harness?.switchConversationWorkspace !== 'function') {
448
+ return commandResult(t('当前机器人暂不支持设置对话工作区。'));
449
+ }
450
+ try {
451
+ let selected = argument;
452
+ if (/^\d+$/u.test(argument)) {
453
+ const { paths } = await workspacePathSnapshot(harness);
454
+ const position = Number(argument);
455
+ if (!Number.isSafeInteger(position) || position < 1 || position > paths.length) {
456
+ return commandResult(t('工作区序号不存在,请先执行 /workspacelist。'));
457
+ }
458
+ selected = paths[position - 1];
459
+ }
460
+ const current = await harness.switchConversationWorkspace(conversationKey, selected);
461
+ return commandResult(t('当前对话工作区已切换为:{workspace}', { workspace: current }));
462
+ } catch (error) {
463
+ if (['workspace-not-absolute', 'workspace-not-found', 'workspace-not-directory'].includes(error?.code)) {
464
+ return commandResult(t(`{message}
465
+ 用法:/conv 工作区绝对路径`, { message: error.message }));
466
+ }
467
+ if (error?.code === 'workspace-bot-not-found') {
468
+ return commandResult(t('机器人正在移除或已重新接入,无法切换对话工作区。'));
469
+ }
470
+ throw error;
471
+ }
472
+ }
473
+
374
474
  export async function runWorkspaceCommand(text, harness, conversationKey) {
375
475
  if (!isWorkspaceCommand(text)) return null;
376
476
  const command = text.trim();
477
+ const threadMatch = THREAD_WORKSPACE_COMMAND.exec(command);
478
+ if (threadMatch) {
479
+ return runConversationWorkspaceCommand(command, harness, conversationKey);
480
+ }
377
481
  if (SESSION_BIND_PREFIX.test(command)) {
378
482
  return runSessionBindCommand(command, harness, conversationKey);
379
483
  }
380
484
  const sessionListMatch = SESSION_LIST_COMMAND.exec(command);
381
- if (sessionListMatch) return runSessionListCommand(sessionListMatch, harness);
485
+ if (sessionListMatch) return runSessionListCommand(sessionListMatch, harness, conversationKey);
382
486
  const listMatch = WORKSPACE_LIST_COMMAND.exec(command);
383
487
  if (listMatch) return runWorkspaceListCommand(listMatch, harness);
384
488
  const match = WORKSPACE_COMMAND.exec(command);
@@ -423,5 +527,6 @@ export function isWorkspaceCommand(text) {
423
527
  return SESSION_BIND_PREFIX.test(command)
424
528
  || SESSION_LIST_COMMAND.test(command)
425
529
  || WORKSPACE_LIST_COMMAND.test(command)
426
- || WORKSPACE_COMMAND.test(command);
530
+ || WORKSPACE_COMMAND.test(command)
531
+ || THREAD_WORKSPACE_COMMAND.test(command);
427
532
  }
@@ -3,9 +3,45 @@ import { initialSessionTitle } from './session-title.mjs';
3
3
 
4
4
  export const WORKSPACE_SESSION_STALE = 'workspace-session-stale';
5
5
 
6
- function workspaceSession(harness, sessionId) {
6
+ function workspaceSessionStaleError() {
7
+ const error = new Error('The conversation workspace changed before the prompt was sent.');
8
+ error.code = WORKSPACE_SESSION_STALE;
9
+ return error;
10
+ }
11
+
12
+ /**
13
+ * Read the conversation's effective-workspace generation. A bot-scoped Harness
14
+ * exposes it; anything else (plain fixtures, older Harnesses) yields null and
15
+ * disables the conversation-level fence without changing existing behavior.
16
+ */
17
+ function readConversationGeneration(harness, conversationKey) {
18
+ return typeof harness?.conversationWorkspaceGeneration === 'function'
19
+ ? harness.conversationWorkspaceGeneration(conversationKey) ?? null
20
+ : null;
21
+ }
22
+
23
+ function conversationGenerationMoved(harness, conversationKey, generation) {
24
+ if (generation === null) return false;
25
+ return readConversationGeneration(harness, conversationKey) !== generation;
26
+ }
27
+
28
+ /**
29
+ * Wait for a conversation-level workspace switch that is still committing.
30
+ * An explicit /conv publishes its fence before it persists, so a message that
31
+ * is already in flight must settle on the new workspace instead of resolving a
32
+ * session in the one being left behind.
33
+ */
34
+ async function awaitPendingConversationSwitch(harness, conversationKey) {
35
+ if (typeof harness?.pendingConversationWorkspaceSwitch !== 'function') return;
36
+ const pending = harness.pendingConversationWorkspaceSwitch(conversationKey);
37
+ if (pending && typeof pending.then === 'function') await pending.catch(() => undefined);
38
+ }
39
+
40
+ function workspaceSession(harness, sessionId, conversationKey) {
7
41
  if (typeof harness.workspaceSession === 'function') {
8
- return harness.workspaceSession(sessionId);
42
+ return conversationKey
43
+ ? harness.workspaceSession(sessionId, conversationKey)
44
+ : harness.workspaceSession(sessionId);
9
45
  }
10
46
  const session = {
11
47
  sessionId,
@@ -67,12 +103,20 @@ export async function askInWorkspaceSession({
67
103
  while (true) {
68
104
  try {
69
105
  const binding = await withSessionBindingLock(state, key, async () => {
106
+ await awaitPendingConversationSwitch(harness, key);
70
107
  let sessionId = state.sessionFor(key);
71
- let session = sessionId ? workspaceSession(harness, sessionId) : null;
108
+ let session = sessionId ? workspaceSession(harness, sessionId, key) : null;
72
109
  if (!session || !(await sessionExists(session, existsOptions))) {
73
- sessionId = await createSession(harness, createOptions);
110
+ sessionId = await createSession(harness, {
111
+ conversationKey: key,
112
+ ...(createOptions ?? {}),
113
+ });
74
114
  if (await state.setSession(key, sessionId) === false) return null;
75
- session = workspaceSession(harness, sessionId);
115
+ // Binding committed: capture the conversation's effective-workspace
116
+ // generation together with the session, so the prompt below is fenced
117
+ // against a conversation switch that only commits after the bind.
118
+ const conversationGeneration = readConversationGeneration(harness, key);
119
+ session = workspaceSession(harness, sessionId, key);
76
120
  if (initialTitle && typeof session.renameTitle === 'function') {
77
121
  try {
78
122
  await session.renameTitle(initialTitle, renameOptions);
@@ -83,6 +127,12 @@ export async function askInWorkspaceSession({
83
127
  console.warn('[dsh-im] unable to set the initial Session title:', error?.message ?? error);
84
128
  }
85
129
  }
130
+ // A switch that committed while the title was being set has already
131
+ // cleared the mapping and must not receive this prompt.
132
+ if (conversationGenerationMoved(harness, key, conversationGeneration)) {
133
+ throw workspaceSessionStaleError();
134
+ }
135
+ return { sessionId, session };
86
136
  }
87
137
  return { sessionId, session };
88
138
  });
@@ -1062,7 +1062,7 @@ export class TelegramRuntime {
1062
1062
  }
1063
1063
  const sessionId = this.#state.sessionFor(key);
1064
1064
  const session = typeof sessionId === 'string' && sessionId
1065
- ? this.#harness.workspaceSession?.(sessionId)
1065
+ ? this.#harness.workspaceSession?.(sessionId, key)
1066
1066
  : null;
1067
1067
  const text = await recoverAssistantTextByTimestamp({ session, quotedAt, signal });
1068
1068
  return text ? { content: text } : { unavailableReason: 'not-delivered' };
@@ -628,7 +628,7 @@ export class WecomHarnessBridge {
628
628
  workspacePathSnapshot(this.#harness, options),
629
629
  this.#harness.listWorkspaceSessions?.(workspace, options),
630
630
  (async () => {
631
- const session = sessionId ? this.#harness.workspaceSession?.(sessionId) : null;
631
+ const session = sessionId ? this.#harness.workspaceSession?.(sessionId, key) : null;
632
632
  return typeof session?.models === 'function'
633
633
  ? session.models(options) : this.#harness.listModels?.(options);
634
634
  })(),
@@ -852,7 +852,7 @@ export class WecomHarnessBridge {
852
852
  } else if (menu.section === 'models') {
853
853
  title = t('🧠 切换模型');
854
854
  const sessionId = this.#state.sessionFor(key);
855
- const session = sessionId ? this.#harness.workspaceSession?.(sessionId) : null;
855
+ const session = sessionId ? this.#harness.workspaceSession?.(sessionId, key) : null;
856
856
  const catalog = typeof session?.models === 'function'
857
857
  ? await session.models(options) : await this.#harness.listModels(options);
858
858
  entries = catalog.groups.flatMap((group) => group.models.map((model) => [
@@ -0,0 +1,116 @@
1
+ // Shared diagnostic copy for the WeChat Host and settings client.
2
+ export default {
3
+ "插件版本": "Plugin version",
4
+ "DSH 微信管理接口返回了无法识别的响应,请重新读取状态。": "The DSH WeChat management endpoint returned an invalid response. Refresh the status.",
5
+ "无法读取现有登录凭据。请检查 DSH 凭据存储。": "Could not read the saved login credential. Check the DSH credential store.",
6
+ "登录凭据无法写入 DSH 凭据存储。请检查凭据存储是否可写。": "Could not save the login credential to DSH. Check whether the credential store is writable.",
7
+ "账号配置无法写入本机。请检查 DSH_HOME 目录权限。": "Could not save the account configuration. Check permissions on DSH_HOME.",
8
+ "无法初始化账号状态或工作区。请检查 DSH_HOME 和工作区目录。": "Could not prepare the account state or workspace. Check DSH_HOME and the workspace directory.",
9
+ "插件无法连接当前 Harness,请检查宿主是否已就绪。": "The plugin could not connect to this Harness. Check whether the host is ready.",
10
+ "Harness 健康检查超时。请确认 dsh web 未阻塞。": "The Harness health check timed out. Check whether the host is responsive.",
11
+ "Harness 健康检查需要身份认证。请检查代理、网关或自定义鉴权配置。": "The Harness health check requires authentication. Check proxy, gateway or custom authentication settings.",
12
+ "本机 Harness 请求被代理要求认证。请让回环地址绕过代理,并检查 NO_PROXY 配置。": "The proxy requires authentication for the local Harness request. Bypass the proxy for loopback addresses and check NO_PROXY.",
13
+ "Harness 异常拒绝了回环地址的健康检查。请检查 HTTP 代理、Harness 源码版本和构建产物。": "Harness rejected a loopback health check. Check the HTTP proxy and the running Harness build.",
14
+ "Harness 的 Host 信任检查拒绝了非回环地址请求。请检查 harnessBaseUrl 与 trustedHosts 配置。": "Harness rejected the non-loopback Host. Check harnessBaseUrl and trustedHosts.",
15
+ "健康检查收到了非 Harness 标准的 403 拒绝响应。请检查代理或网关配置。": "The health check received a non-standard HTTP 403 response. Check proxy or gateway settings.",
16
+ "找不到 Harness 健康检查接口。请确认 Harness 与插件版本兼容。": "The Harness health check endpoint was not found. Check Harness and plugin compatibility.",
17
+ "Harness 健康检查返回服务错误。请查看 DSH 日志。": "The Harness health check returned a service error. Check the DSH logs.",
18
+ "Harness 返回了无法识别的响应。请确认 Harness 与插件版本兼容。": "Harness returned an unrecognized response. Check Harness and plugin compatibility.",
19
+ "Harness 拒绝了健康检查请求。请查看 DSH 日志。": "Harness rejected the health check. Check the DSH logs.",
20
+ "Harness 健康检查发生未知错误。请查看 DSH 日志。": "The Harness health check failed with an unknown error. Check the DSH logs.",
21
+ "消息连接初始化失败。请查看 DSH 日志后重试。": "The message connection could not start. Check the DSH logs and retry.",
22
+ "暂时无法访问微信服务。": "The WeChat service is temporarily unreachable.",
23
+ "微信服务请求超时。": "The WeChat service request timed out.",
24
+ "微信服务请求失败(HTTP {status})。": "The WeChat service request failed (HTTP {status}).",
25
+ "微信服务返回了无法解析的响应。": "The WeChat service returned an unparseable response.",
26
+ "微信服务没有返回有效二维码。": "The WeChat service did not return a valid QR code.",
27
+ "微信服务返回了不受信任的扫码地址。": "The WeChat service returned an untrusted QR-code URL.",
28
+ "微信服务返回了无效的连接地址。": "The WeChat service returned an invalid connection URL.",
29
+ "微信服务返回了不受信任的连接地址。": "The WeChat service returned an untrusted connection URL.",
30
+ "拒绝访问不受信任的微信服务地址。": "Refusing to access an untrusted WeChat service URL.",
31
+ "微信服务返回了无法识别的扫码状态。": "The WeChat service returned an unrecognized QR-code status.",
32
+ "微信授权成功,但返回的账号凭据不完整。": "WeChat authorized the account but returned incomplete credentials.",
33
+ "微信服务拒绝了二维码申请。": "The WeChat service rejected the QR-code request.",
34
+ "已取得扫码地址,但本机二维码图片生成失败。": "The login URL was obtained, but the QR-code image could not be generated locally.",
35
+ "无法生成微信二维码。": "Could not generate a WeChat QR code.",
36
+ "这次微信绑定任务已不存在,请重新读取状态或生成二维码。": "This WeChat setup attempt no longer exists. Refresh the status or generate a new QR code.",
37
+ "这次微信绑定任务当前不能提交配对码,请重新读取状态。": "This setup attempt is not waiting for a pairing code. Refresh the status.",
38
+ "登录凭据缺失,请移除账号后重新扫码。": "The login credential is missing. Remove the account and scan a new QR code.",
39
+ "微信登录凭据已失效,请移除账号后重新扫码。": "The WeChat login credential has expired. Remove the account and scan a new QR code.",
40
+ "无法从 DSH 凭据存储移除微信登录凭据。": "Could not remove the WeChat login credential from the DSH credential store.",
41
+ "微信账号配置移除失败。": "Could not remove the WeChat account configuration.",
42
+ "无法读取微信账号状态文件。": "Could not read the WeChat account state file.",
43
+ "无法保存微信账号状态文件。": "Could not save the WeChat account state file.",
44
+ "微信账号已移除,但本机状态文件清理失败。": "The WeChat account was removed, but its local state file could not be cleaned up.",
45
+ "无法保存微信工作区设置。": "Could not save the WeChat workspace settings.",
46
+ "微信账号已移除,但工作区设置清理失败。": "The WeChat account was removed, but its workspace settings could not be cleaned up.",
47
+ "微信账号连接启动失败。": "The WeChat account connection could not start.",
48
+ "微信消息同步请求被拒绝。": "WeChat rejected the message sync request.",
49
+ "微信服务未确认停止通知。": "WeChat did not confirm the stop notification.",
50
+ "微信消息连接停止时发生错误。": "An error occurred while stopping the WeChat connection.",
51
+ "无法读取微信连接状态,请重新读取;之前的操作可能已完成。": "Could not read the WeChat connection status. Refresh it; the previous operation may have completed.",
52
+ "微信激活过程中发生未知错误。": "An unknown error occurred while activating the WeChat account.",
53
+ "微信操作发生未知错误。": "The WeChat operation failed with an unknown error.",
54
+ "微信配置格式错误,请检查 config.json 和 workspaces.json 后重启 DSH。": "The WeChat configuration is invalid. Check config.json and workspaces.json, then restart DSH.",
55
+ "微信配置访问权限不足,请检查数据目录权限后重启 DSH。": "The WeChat configuration could not be accessed. Check the data directory permissions and restart DSH.",
56
+ "微信初始化失败,请查看 DSH 启动日志。": "WeChat initialization failed. Check the DSH startup logs.",
57
+ "微信连接未就绪。": "The WeChat connection is not ready.",
58
+ "微信操作失败后,原账号状态恢复失败。": "The previous account state could not be restored after the failed WeChat operation.",
59
+ "原账号状态尚未确认,请重新读取状态,并反馈诊断信息。": "The previous account state has not been confirmed. Refresh the status and share the diagnostic information.",
60
+ "操作未完成,已恢复之前的本机状态。请处理上述问题后重试。": "The operation did not complete. The previous local state was restored. Resolve the problem above before retrying.",
61
+ "凭据由只读来源提供,请在启动 DSH 的配置中检查该来源。": "The credential comes from a read-only source. Check that source in the configuration used to launch DSH.",
62
+ "微信服务域名解析失败,请检查运行 DSH 的机器的网络和 DNS 设置后重试。": "The WeChat service domain could not be resolved. Check the network and DNS settings on the machine running DSH and retry.",
63
+ "请检查 DSH 数据目录和对应文件的读写权限。": "Check read and write permissions on the DSH data directory and the affected file.",
64
+ "磁盘空间不足,请释放空间后重试。": "The disk is full. Free up space and retry.",
65
+ "本机文件格式无效,请检查对应配置或状态文件;不要清空登录凭据。": "The local file format is invalid. Check the affected configuration or state file; do not clear login credentials.",
66
+ "请检查运行 DSH 的机器的系统时间、证书和网络设置。": "Check the system time, certificates and network settings on the machine running DSH.",
67
+ "请移除失效接入并重新扫码绑定。": "Remove the invalid connection and scan a new QR code to connect.",
68
+ "请确认当前 DSH 宿主已就绪,并检查 Harness 与插件实际运行版本及宿主日志。": "Check that this DSH host is ready, and check the running Harness and plugin versions and the host logs.",
69
+ "微信服务限流,请稍后重试。": "The WeChat service is rate-limiting requests. Try again later.",
70
+ "访问被拒绝,请检查服务访问限制;HTTP 状态本身不能确认登录凭据已失效。": "Access was denied. Check service access restrictions; the HTTP status alone does not establish that the login credential expired.",
71
+ "请检查运行 DSH 的机器能否访问微信服务,然后重试。": "Check whether the machine running DSH can access the WeChat service, then retry.",
72
+ "请按参考号查看 DSH 日志,并复制诊断信息反馈。": "Find this reference in the DSH logs and copy the diagnostic information when reporting the issue.",
73
+ "无法完成微信管理请求,请检查 DSH 连接后重新读取状态。": "The WeChat management request could not be completed. Check the DSH connection and refresh the status.",
74
+ "查询微信扫码状态失败": "Could not query WeChat QR-code status",
75
+ "无法完成微信管理请求": "Could not complete the WeChat management request",
76
+ "无法生成微信二维码": "Could not generate a WeChat QR code",
77
+ "状态读取失败,以下是上次读取的状态。": "Status refresh failed. The last known status is shown below.",
78
+ "诊断详情": "Diagnostic details",
79
+ "诊断信息": "Diagnostic information",
80
+ "复制诊断信息": "Copy diagnostic information",
81
+ "诊断信息已复制": "Diagnostic information copied",
82
+ "未取得 Host 诊断参考号。": "No diagnostic reference was received from the host.",
83
+ "无法访问剪贴板,请选择并复制以下诊断信息。": "Clipboard access is unavailable. Select and copy the diagnostic information below.",
84
+ "错误码": "Error code",
85
+ "失败阶段": "Failed step",
86
+ "底层原因": "Underlying cause",
87
+ "HTTP 状态": "HTTP status",
88
+ "微信返回码": "WeChat return code",
89
+ "参考号": "Reference",
90
+ "发生时间": "Time",
91
+ "加载微信配置": "Load WeChat configuration",
92
+ "申请二维码": "Request QR code",
93
+ "生成二维码图片": "Generate QR-code image",
94
+ "查询扫码状态": "Query QR-code status",
95
+ "提交配对码": "Submit pairing code",
96
+ "取消绑定": "Cancel setup",
97
+ "读取登录凭据": "Read login credential",
98
+ "保存登录凭据": "Save login credential",
99
+ "移除登录凭据": "Remove login credential",
100
+ "保存账号配置": "Save account configuration",
101
+ "移除账号配置": "Remove account configuration",
102
+ "读取账号状态": "Read account state",
103
+ "保存账号状态": "Save account state",
104
+ "清理账号状态": "Clean up account state",
105
+ "保存工作区设置": "Save workspace settings",
106
+ "清理工作区设置": "Clean up workspace settings",
107
+ "准备消息连接": "Prepare message connection",
108
+ "激活微信账号": "Activate WeChat account",
109
+ "检查 DSH 宿主": "Check DSH host",
110
+ "启动微信连接": "Start WeChat connection",
111
+ "同步微信消息": "Sync WeChat messages",
112
+ "停止微信连接": "Stop WeChat connection",
113
+ "读取连接状态": "Read connection status",
114
+ "恢复原账号状态": "Restore previous account state",
115
+ "访问 DSH 管理接口": "Access DSH management API"
116
+ };