@xmanrui/dsh-im 4.2.1 → 4.4.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 (74) hide show
  1. package/README.en.md +4 -2
  2. package/README.md +4 -2
  3. package/lib/client.js +1267 -787
  4. package/lib/index.js +237 -235
  5. package/package.json +1 -1
  6. package/plugin-src/client/access-policy-settings.js +351 -0
  7. package/plugin-src/client/channel-card-meta.js +2 -1
  8. package/plugin-src/client/channels/dingtalk/api.js +5 -0
  9. package/plugin-src/client/channels/dingtalk/index.js +1 -0
  10. package/plugin-src/client/channels/feishu/api.js +5 -0
  11. package/plugin-src/client/channels/feishu/index.js +1 -0
  12. package/plugin-src/client/channels/qq/api.js +5 -0
  13. package/plugin-src/client/channels/qq/index.js +1 -0
  14. package/plugin-src/client/channels/shared/token-api.js +5 -0
  15. package/plugin-src/client/channels/shared/token-channel.js +1 -0
  16. package/plugin-src/client/channels/telegram/api.js +2 -17
  17. package/plugin-src/client/channels/telegram/index.js +0 -112
  18. package/plugin-src/client/channels/telegram/styles.js +0 -28
  19. package/plugin-src/client/channels/wecom/api.js +5 -0
  20. package/plugin-src/client/channels/wecom/index.js +1 -0
  21. package/plugin-src/client/channels/weixin/api.js +5 -0
  22. package/plugin-src/client/channels/weixin/index.js +1 -0
  23. package/plugin-src/client/channels/whatsapp/api.js +4 -10
  24. package/plugin-src/client/channels/whatsapp/index.js +1 -125
  25. package/plugin-src/client/channels/whatsapp/styles.js +0 -25
  26. package/plugin-src/client/context-enhancement.js +207 -102
  27. package/plugin-src/client/delivery-settings.js +61 -9
  28. package/plugin-src/client/i18n.js +71 -3
  29. package/plugin-src/client/index.js +1 -0
  30. package/plugin-src/client/styles.js +59 -8
  31. package/plugin-src/host/channels/dingtalk/production.mjs +12 -1
  32. package/plugin-src/host/channels/dingtalk/rpc.mjs +11 -0
  33. package/plugin-src/host/channels/feishu/production.mjs +39 -1
  34. package/plugin-src/host/channels/feishu/rpc.mjs +19 -2
  35. package/plugin-src/host/channels/qq/production.mjs +12 -1
  36. package/plugin-src/host/channels/qq/rpc.mjs +11 -0
  37. package/plugin-src/host/channels/shared/access-policy-production.mjs +137 -0
  38. package/plugin-src/host/channels/shared/access-policy-rpc.mjs +17 -0
  39. package/plugin-src/host/channels/shared/production.mjs +15 -1
  40. package/plugin-src/host/channels/shared/rpc.mjs +9 -0
  41. package/plugin-src/host/channels/slack/production.mjs +12 -1
  42. package/plugin-src/host/channels/slack/rpc.mjs +10 -0
  43. package/plugin-src/host/channels/telegram/rpc.mjs +2 -49
  44. package/plugin-src/host/channels/wecom/production.mjs +12 -1
  45. package/plugin-src/host/channels/wecom/rpc.mjs +11 -0
  46. package/plugin-src/host/channels/weixin/production.mjs +12 -1
  47. package/plugin-src/host/channels/weixin/rpc.mjs +11 -0
  48. package/plugin-src/host/channels/whatsapp/production.mjs +16 -2
  49. package/plugin-src/host/channels/whatsapp/rpc.mjs +10 -15
  50. package/scripts/verify-package.mjs +4 -3
  51. package/src/channels/dingtalk/dingtalk-bridge.mjs +61 -9
  52. package/src/channels/dingtalk/dingtalk-runtime.mjs +4 -0
  53. package/src/channels/discord/discord-runtime.mjs +29 -1
  54. package/src/channels/feishu/bridge.mjs +286 -120
  55. package/src/channels/feishu/feishu-runtime.mjs +4 -0
  56. package/src/channels/feishu/message-utils.mjs +5 -0
  57. package/src/channels/qq/qq-bridge.mjs +59 -17
  58. package/src/channels/qq/qq-runtime.mjs +25 -3
  59. package/src/channels/shared/access-policy.mjs +210 -0
  60. package/src/channels/shared/bot-workspace-store.mjs +176 -33
  61. package/src/channels/shared/command-permission.mjs +32 -0
  62. package/src/channels/shared/compact-command.mjs +5 -2
  63. package/src/channels/shared/context-enhancement.mjs +73 -20
  64. package/src/channels/shared/i18n-en/shared-c.mjs +2 -0
  65. package/src/channels/shared/inbound-access.mjs +43 -0
  66. package/src/channels/shared/text-harness-bridge.mjs +47 -4
  67. package/src/channels/shared/workspace-command.mjs +10 -1
  68. package/src/channels/slack/slack-runtime.mjs +4 -0
  69. package/src/channels/telegram/telegram-runtime.mjs +5 -13
  70. package/src/channels/wecom/wecom-bridge.mjs +51 -4
  71. package/src/channels/wecom/wecom-runtime.mjs +4 -0
  72. package/src/channels/weixin/weixin-bridge.mjs +59 -16
  73. package/src/channels/weixin/weixin-runtime.mjs +4 -0
  74. package/src/channels/whatsapp/whatsapp-runtime.mjs +42 -20
@@ -13,6 +13,10 @@ import {
13
13
  normalizeAgentPresetCatalog,
14
14
  validateAgentPresetId,
15
15
  } from './agent-preset.mjs';
16
+ import {
17
+ normalizeAccessPolicy,
18
+ validateAccessPolicy,
19
+ } from './access-policy.mjs';
16
20
  import { CONNECTION_TEST_STATE_IDENTITY } from './connection-test.mjs';
17
21
  import {
18
22
  DEFAULT_CONTEXT_ENHANCEMENT_CONFIG,
@@ -131,6 +135,26 @@ function normalizeDeliveryTargets(value) {
131
135
  return deliveryTargets;
132
136
  }
133
137
 
138
+ function normalizeAccessPolicies(value, workspaces) {
139
+ const accessPolicies = Object.create(null);
140
+ if (value === undefined) return accessPolicies;
141
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
142
+ // Preserve the distinction between a missing policy (eligible for startup
143
+ // initialization) and damaged persisted data (fail closed).
144
+ for (const botId of Object.keys(workspaces)) accessPolicies[botId] = null;
145
+ return accessPolicies;
146
+ }
147
+ for (const [botId, policy] of Object.entries(value)) {
148
+ try {
149
+ botIdOf(botId);
150
+ accessPolicies[botId] = normalizeAccessPolicy(policy);
151
+ } catch {
152
+ // An invalid key cannot identify a bot, so it is isolated and ignored.
153
+ }
154
+ }
155
+ return accessPolicies;
156
+ }
157
+
134
158
  function normalizeDocument(value) {
135
159
  if (!value || ![1, 2].includes(value.version) || !value.workspaces
136
160
  || typeof value.workspaces !== 'object' || Array.isArray(value.workspaces)) return null;
@@ -168,15 +192,52 @@ function normalizeDocument(value) {
168
192
  if (value.version === 1 && value.deliveryTargets !== undefined) return null;
169
193
  const deliveryTargets = normalizeDeliveryTargets(value.deliveryTargets);
170
194
  if (!deliveryTargets) return null;
195
+ const accessPolicies = normalizeAccessPolicies(value.accessPolicies, workspaces);
196
+ const version = value.accessPolicies === undefined ? value.version : 2;
171
197
  return {
172
- version: value.version,
198
+ // A v1 file cannot be emitted with this optional v2 section. If one is
199
+ // recovered from an interrupted/manual edit, retain it on the next write.
200
+ version,
173
201
  workspaces,
174
202
  agentPresets,
175
203
  contextEnhancement,
176
204
  deliveryTargets,
205
+ accessPolicies,
177
206
  };
178
207
  }
179
208
 
209
+ function storedDocument({
210
+ version,
211
+ workspaces,
212
+ agentPresets,
213
+ contextEnhancement,
214
+ deliveryTargets,
215
+ accessPolicies,
216
+ }) {
217
+ const document = { version, workspaces };
218
+ if (Object.keys(agentPresets).length > 0) document.agentPresets = agentPresets;
219
+ if (Object.keys(contextEnhancement).length > 0) {
220
+ document.contextEnhancement = contextEnhancement;
221
+ }
222
+ if (version >= 2 && Object.keys(deliveryTargets).length > 0) {
223
+ document.deliveryTargets = deliveryTargets;
224
+ }
225
+ if (version >= 2 && Object.keys(accessPolicies).length > 0) {
226
+ document.accessPolicies = accessPolicies;
227
+ }
228
+ return document;
229
+ }
230
+
231
+ async function writeStoredDocument(path, document) {
232
+ await mkdir(dirname(path), { recursive: true, mode: 0o700 });
233
+ const temporary = `${path}.tmp`;
234
+ await writeFile(temporary, `${JSON.stringify(document, null, 2)}\n`, {
235
+ encoding: 'utf8',
236
+ mode: 0o600,
237
+ });
238
+ await rename(temporary, path);
239
+ }
240
+
180
241
  export async function validateWorkspacePath(value) {
181
242
  if (typeof value !== 'string' || !value.trim() || !isAbsolute(value.trim())) {
182
243
  const error = new Error('工作区必须是绝对路径。');
@@ -208,6 +269,7 @@ export class BotWorkspaceStore {
208
269
  #agentPresets = {};
209
270
  #contextEnhancement = {};
210
271
  #deliveryTargets = Object.create(null);
272
+ #accessPolicies = Object.create(null);
211
273
  #generations = new Map();
212
274
  #nextGeneration = 1;
213
275
  #incarnations = new Map();
@@ -233,6 +295,7 @@ export class BotWorkspaceStore {
233
295
  this.#agentPresets = normalized.agentPresets;
234
296
  this.#contextEnhancement = normalized.contextEnhancement;
235
297
  this.#deliveryTargets = normalized.deliveryTargets;
298
+ this.#accessPolicies = normalized.accessPolicies;
236
299
  } catch (error) {
237
300
  if (error?.code !== 'ENOENT') throw error;
238
301
  this.#version = 1;
@@ -240,6 +303,7 @@ export class BotWorkspaceStore {
240
303
  this.#agentPresets = {};
241
304
  this.#contextEnhancement = {};
242
305
  this.#deliveryTargets = Object.create(null);
306
+ this.#accessPolicies = Object.create(null);
243
307
  }
244
308
  this.#generations.clear();
245
309
  this.#nextGeneration = 1;
@@ -278,6 +342,13 @@ export class BotWorkspaceStore {
278
342
  : DEFAULT_CONTEXT_ENHANCEMENT_CONFIG;
279
343
  }
280
344
 
345
+ accessPolicyFor(botId) {
346
+ const id = botIdOf(botId);
347
+ return this.has(id) && Object.hasOwn(this.#accessPolicies, id)
348
+ ? this.#accessPolicies[id]
349
+ : null;
350
+ }
351
+
281
352
  listDeliveryTargets(botId) {
282
353
  const id = botIdOf(botId);
283
354
  if (!this.has(id)) throw deliveryTargetError('unknown-bot', 'Unknown bot');
@@ -373,28 +444,53 @@ export class BotWorkspaceStore {
373
444
  }
374
445
  }
375
446
 
376
- async ensure(botId, { workspace = this.#defaultWorkspace, defaultAgentPreset } = {}) {
447
+ async ensure(botId, {
448
+ workspace = this.#defaultWorkspace,
449
+ defaultAgentPreset,
450
+ initialAccessPolicy,
451
+ } = {}) {
377
452
  const id = botIdOf(botId);
378
453
  const initialWorkspace = resolve(workspace);
379
454
  return this.#enqueue(id, async () => {
380
- if (!this.#workspaces[id]) {
381
- const agentPreset = validateAgentPresetId(defaultAgentPreset);
455
+ const createsBot = !this.#workspaces[id];
456
+ const initializesAccessPolicy = initialAccessPolicy !== undefined
457
+ && !Object.hasOwn(this.#accessPolicies, id);
458
+ if (createsBot || initializesAccessPolicy) {
459
+ const accessPolicy = initializesAccessPolicy
460
+ ? validateAccessPolicy(initialAccessPolicy)
461
+ : undefined;
462
+ const agentPreset = createsBot ? validateAgentPresetId(defaultAgentPreset) : null;
382
463
  const hadAgentPreset = Object.hasOwn(this.#agentPresets, id);
383
464
  const previousAgentPreset = this.#agentPresets[id];
384
- this.#workspaces[id] = initialWorkspace;
385
- if (agentPreset) this.#agentPresets[id] = agentPreset;
386
- this.#generations.set(id, this.#freshGeneration());
387
- this.#incarnations.set(id, this.#freshIncarnation());
465
+ const nextAccessPolicies = initializesAccessPolicy
466
+ ? { ...this.#accessPolicies, [id]: accessPolicy }
467
+ : this.#accessPolicies;
468
+ if (createsBot) {
469
+ this.#workspaces[id] = initialWorkspace;
470
+ if (agentPreset) this.#agentPresets[id] = agentPreset;
471
+ this.#generations.set(id, this.#freshGeneration());
472
+ this.#incarnations.set(id, this.#freshIncarnation());
473
+ }
474
+ const nextVersion = initializesAccessPolicy ? 2 : this.#version;
388
475
  try {
389
- await this.#persist();
476
+ await this.#persist(
477
+ this.#contextEnhancement,
478
+ this.#deliveryTargets,
479
+ nextVersion,
480
+ nextAccessPolicies,
481
+ );
390
482
  } catch (error) {
391
- delete this.#workspaces[id];
392
- if (hadAgentPreset) this.#agentPresets[id] = previousAgentPreset;
393
- else delete this.#agentPresets[id];
394
- this.#generations.delete(id);
395
- this.#incarnations.delete(id);
483
+ if (createsBot) {
484
+ delete this.#workspaces[id];
485
+ if (hadAgentPreset) this.#agentPresets[id] = previousAgentPreset;
486
+ else delete this.#agentPresets[id];
487
+ this.#generations.delete(id);
488
+ this.#incarnations.delete(id);
489
+ }
396
490
  throw error;
397
491
  }
492
+ this.#accessPolicies = nextAccessPolicies;
493
+ this.#version = nextVersion;
398
494
  } else if (!this.#generations.has(id)) {
399
495
  this.#generations.set(id, this.#freshGeneration());
400
496
  }
@@ -487,6 +583,30 @@ export class BotWorkspaceStore {
487
583
  });
488
584
  }
489
585
 
586
+ async setAccessPolicy(botId, value, { incarnation } = {}) {
587
+ const id = botIdOf(botId);
588
+ const expectedIncarnation = incarnation === undefined ? this.incarnationFor(id) : incarnation;
589
+ const policy = validateAccessPolicy(value);
590
+ return this.#enqueue(id, async () => {
591
+ if (!this.has(id) || expectedIncarnation !== this.incarnationFor(id)) {
592
+ const error = new Error('找不到要修改的机器人。');
593
+ error.code = 'workspace-bot-not-found';
594
+ throw error;
595
+ }
596
+ const next = { ...this.#accessPolicies, [id]: policy };
597
+ // Inbound messages keep the previous committed snapshot until rename succeeds.
598
+ await this.#persist(
599
+ this.#contextEnhancement,
600
+ this.#deliveryTargets,
601
+ 2,
602
+ next,
603
+ );
604
+ this.#accessPolicies = next;
605
+ this.#version = 2;
606
+ return policy;
607
+ });
608
+ }
609
+
490
610
  async bindWorkspaceSession(botId, value, {
491
611
  conversationKey,
492
612
  sessionId,
@@ -646,6 +766,7 @@ export class BotWorkspaceStore {
646
766
  ...Object.keys(this.#agentPresets),
647
767
  ...Object.keys(this.#contextEnhancement),
648
768
  ...Object.keys(this.#deliveryTargets),
769
+ ...Object.keys(this.#accessPolicies),
649
770
  ...this.#dirtyRemovals,
650
771
  ]);
651
772
  for (const botId of candidates) {
@@ -663,6 +784,7 @@ export class BotWorkspaceStore {
663
784
  workspace: this.workspaceFor(bot.botId),
664
785
  agentPreset: this.agentPresetFor(bot.botId),
665
786
  contextEnhancement: this.contextEnhancementFor(bot.botId),
787
+ accessPolicy: this.accessPolicyFor(bot.botId),
666
788
  }
667
789
  : bot),
668
790
  };
@@ -694,12 +816,14 @@ export class BotWorkspaceStore {
694
816
  const hadPreset = Object.hasOwn(this.#agentPresets, id);
695
817
  const hadContextEnhancement = Object.hasOwn(this.#contextEnhancement, id);
696
818
  const hadDeliveryTargets = Object.hasOwn(this.#deliveryTargets, id);
819
+ const hadAccessPolicy = Object.hasOwn(this.#accessPolicies, id);
697
820
  const needsCleanup = hadWorkspace || hadPreset || hadContextEnhancement
698
- || hadDeliveryTargets || this.#dirtyRemovals.has(id);
821
+ || hadDeliveryTargets || hadAccessPolicy || this.#dirtyRemovals.has(id);
699
822
  delete this.#workspaces[id];
700
823
  delete this.#agentPresets[id];
701
824
  delete this.#contextEnhancement[id];
702
825
  delete this.#deliveryTargets[id];
826
+ delete this.#accessPolicies[id];
703
827
  this.#generations.delete(id);
704
828
  this.#incarnations.delete(id);
705
829
  if (!needsCleanup) return {
@@ -733,24 +857,16 @@ export class BotWorkspaceStore {
733
857
  contextEnhancement = this.#contextEnhancement,
734
858
  deliveryTargets = this.#deliveryTargets,
735
859
  version = this.#version,
860
+ accessPolicies = this.#accessPolicies,
736
861
  ) {
737
- const document = { version, workspaces: this.#workspaces };
738
- if (Object.keys(this.#agentPresets).length > 0) {
739
- document.agentPresets = this.#agentPresets;
740
- }
741
- if (Object.keys(contextEnhancement).length > 0) {
742
- document.contextEnhancement = contextEnhancement;
743
- }
744
- if (version >= 2 && Object.keys(deliveryTargets).length > 0) {
745
- document.deliveryTargets = deliveryTargets;
746
- }
747
- await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
748
- const temporary = `${this.#path}.tmp`;
749
- await writeFile(temporary, `${JSON.stringify(document, null, 2)}\n`, {
750
- encoding: 'utf8',
751
- mode: 0o600,
752
- });
753
- await rename(temporary, this.#path);
862
+ await writeStoredDocument(this.#path, storedDocument({
863
+ version,
864
+ workspaces: this.#workspaces,
865
+ agentPresets: this.#agentPresets,
866
+ contextEnhancement,
867
+ deliveryTargets,
868
+ accessPolicies,
869
+ }));
754
870
  this.#dirtyRemovals.clear();
755
871
  }
756
872
 
@@ -758,7 +874,8 @@ export class BotWorkspaceStore {
758
874
  if (Object.keys(this.#workspaces).length > 0
759
875
  || Object.keys(this.#agentPresets).length > 0
760
876
  || Object.keys(this.#contextEnhancement).length > 0
761
- || Object.keys(this.#deliveryTargets).length > 0) {
877
+ || Object.keys(this.#deliveryTargets).length > 0
878
+ || Object.keys(this.#accessPolicies).length > 0) {
762
879
  await this.#persist();
763
880
  return;
764
881
  }
@@ -1280,6 +1397,31 @@ export function createWorkspaceAwareController(controller, { workspaces, stateFo
1280
1397
  return result;
1281
1398
  });
1282
1399
  };
1400
+ const updateAccessPolicy = (botId, value, projectStatus) => {
1401
+ const incarnation = workspaces.incarnationFor(botId);
1402
+ const policy = validateAccessPolicy(value);
1403
+ return withBotTransition(botId, async () => {
1404
+ const snapshot = await controller.status();
1405
+ if (!snapshot?.bots?.some((bot) => bot?.botId === botId)) {
1406
+ const error = new Error('找不到要修改的机器人。');
1407
+ error.code = 'workspace-bot-not-found';
1408
+ throw error;
1409
+ }
1410
+ const catalog = await resolveAgentPresetCatalog(agentPresetCatalog);
1411
+ const decorated = workspaces.decorateStatus(snapshot);
1412
+ const updated = {
1413
+ ...decorated,
1414
+ bots: decorated.bots.map((bot) => bot?.botId === botId
1415
+ ? { ...bot, accessPolicy: policy } : bot),
1416
+ ...(catalog ? { agentPresetCatalog: catalog } : {}),
1417
+ };
1418
+ // Prepare the complete channel-specific response before commit. Failed
1419
+ // projections and disk writes must leave the live policy unchanged.
1420
+ const result = projectStatus ? await projectStatus(updated) : updated;
1421
+ await workspaces.setAccessPolicy(botId, policy, { incarnation });
1422
+ return result;
1423
+ });
1424
+ };
1283
1425
  const deleteWithWorkspace = (botId, invokeDelete) => withBotTransition(botId, async () => {
1284
1426
  // Fence the old runtime without changing the durable mapping. A crash
1285
1427
  // before the controller removes its config therefore keeps the bot's
@@ -1320,6 +1462,7 @@ export function createWorkspaceAwareController(controller, { workspaces, stateFo
1320
1462
  if (property === 'updateWorkspace') return updateWorkspace;
1321
1463
  if (property === 'updateAgentPreset') return updateAgentPreset;
1322
1464
  if (property === 'updateContextEnhancement') return updateContextEnhancement;
1465
+ if (property === 'updateAccessPolicy') return updateAccessPolicy;
1323
1466
  const value = Reflect.get(target, property, target);
1324
1467
  if (typeof value !== 'function') return value;
1325
1468
  if (property === 'deleteBot') {
@@ -0,0 +1,32 @@
1
+ import { isBatchInputCommand } from './batch-input.mjs';
2
+ import { isCompactCommand } from './compact-command.mjs';
3
+ import { isControlCommand } from './control-command.mjs';
4
+ import { isHistoryCommand } from './history-command.mjs';
5
+ import { isModelCommand } from './model-command.mjs';
6
+ import { isPresetCommand } from './preset-command.mjs';
7
+ import { isWorkspaceCommand } from './workspace-command.mjs';
8
+
9
+ const SIMPLE_TEXT_COMMANDS = new Set(['/help', '/status', '/new']);
10
+
11
+ /**
12
+ * Match only commands that the shared bridges already execute locally.
13
+ * Unknown slash-prefixed text remains an ordinary prompt.
14
+ */
15
+ export function isSharedLocalCommand(text, {
16
+ hasImages = false,
17
+ hasFiles = false,
18
+ } = {}) {
19
+ if (typeof text !== 'string') return false;
20
+ const command = text.trim();
21
+ if (!command) return false;
22
+ if (isBatchInputCommand(command) || isHistoryCommand(command)) return true;
23
+ if (!hasFiles && (
24
+ isControlCommand(command)
25
+ || isModelCommand(command)
26
+ || isPresetCommand(command)
27
+ )) return true;
28
+ if (hasImages || hasFiles) return false;
29
+ return SIMPLE_TEXT_COMMANDS.has(command.toLowerCase())
30
+ || isWorkspaceCommand(command)
31
+ || isCompactCommand(command);
32
+ }
@@ -70,9 +70,8 @@ function compactErrorMessage(error) {
70
70
  * Unknown input returns null so the caller may continue ordinary message routing.
71
71
  */
72
72
  export async function runCompactCommand(text, harness, state, conversationKey, options = {}) {
73
- if (typeof text !== 'string') return null;
73
+ if (!isCompactCommand(text)) return null;
74
74
  const match = COMPACT_COMMAND.exec(text.trim());
75
- if (!match) return null;
76
75
  if (match[1].trim()) return commandResult(t(COMPACT_USAGE));
77
76
  if (typeof state?.sessionFor !== 'function') {
78
77
  return commandResult(t('当前机器人没有可用的会话状态。'));
@@ -94,3 +93,7 @@ export async function runCompactCommand(text, harness, state, conversationKey, o
94
93
  return commandResult(compactErrorMessage(error));
95
94
  }
96
95
  }
96
+
97
+ export function isCompactCommand(text) {
98
+ return typeof text === 'string' && COMPACT_COMMAND.test(text.trim());
99
+ }
@@ -4,6 +4,12 @@ export const CONTEXT_ENHANCEMENT_FIELDS = Object.freeze([
4
4
  ]);
5
5
 
6
6
  export const CONTEXT_ENHANCEMENT_GUIDANCE_MAX_LENGTH = 8_000;
7
+ export const CONTEXT_GROUP_GUIDANCE_EXAMPLE = `仅依据当前消息的 <dsh_im_source> 中实际提供的字段理解来源;没有提供的字段不要猜测或补全。
8
+ 当前消息来自群聊,请使用严肃、克制、简洁的表达方式。`;
9
+ export const CONTEXT_DIRECT_GUIDANCE_EXAMPLE = `仅依据当前消息的 <dsh_im_source> 中实际提供的字段理解来源;没有提供的字段不要猜测或补全。
10
+ 当前消息来自私聊,可以使用更轻松、幽默、详细的表达方式。`;
11
+
12
+ // Kept for integrations that imported the original combined example.
7
13
  export const CONTEXT_GUIDANCE_EXAMPLE = `仅依据当前消息的 <dsh_im_source> 中实际提供的字段理解来源;没有提供的字段不要猜测或补全。
8
14
  conversationType是群聊时回复严肃一点,conversationType是私聊时回复一定要幽默搞笑,像周星驰的电影一样搞笑`;
9
15
 
@@ -11,13 +17,21 @@ conversationType是群聊时回复严肃一点,conversationType是私聊时回
11
17
  export const DEFAULT_CONTEXT_GUIDANCE = CONTEXT_GUIDANCE_EXAMPLE;
12
18
 
13
19
  export const DEFAULT_CONTEXT_ENHANCEMENT_CONFIG = Object.freeze({
14
- groupEnabled: false,
15
- directEnabled: false,
16
- fields: Object.freeze(['senderId']),
17
- guidance: '',
20
+ group: Object.freeze({
21
+ enabled: false,
22
+ fields: Object.freeze(['senderId']),
23
+ guidance: '',
24
+ }),
25
+ direct: Object.freeze({
26
+ enabled: false,
27
+ fields: Object.freeze(['senderId']),
28
+ guidance: '',
29
+ }),
18
30
  });
19
31
 
20
- const CONFIG_KEYS = ['groupEnabled', 'directEnabled', 'fields', 'guidance'];
32
+ const CONFIG_KEYS = ['group', 'direct'];
33
+ const SCOPE_KEYS = ['enabled', 'fields', 'guidance'];
34
+ const LEGACY_CONFIG_KEYS = ['groupEnabled', 'directEnabled', 'fields', 'guidance'];
21
35
  const CHANNELS = new Set([
22
36
  'wecom', 'weixin', 'feishu', 'dingtalk', 'qq',
23
37
  'slack', 'telegram', 'discord', 'whatsapp',
@@ -31,16 +45,19 @@ function invalidConfig(message) {
31
45
  return error;
32
46
  }
33
47
 
34
- /** Validate the complete atomic save, preserving explicit empty selections/text. */
35
- export function validateContextEnhancementConfig(input) {
36
- if (!input || typeof input !== 'object' || Array.isArray(input)
37
- || ![Object.prototype, null].includes(Object.getPrototypeOf(input))
38
- || Reflect.ownKeys(input).length !== CONFIG_KEYS.length
39
- || !CONFIG_KEYS.every((key) => Object.hasOwn(input, key))) {
48
+ function hasExactKeys(input, keys) {
49
+ return input && typeof input === 'object' && !Array.isArray(input)
50
+ && [Object.prototype, null].includes(Object.getPrototypeOf(input))
51
+ && Reflect.ownKeys(input).length === keys.length
52
+ && keys.every((key) => Object.hasOwn(input, key));
53
+ }
54
+
55
+ function validateContextEnhancementScope(input) {
56
+ if (!hasExactKeys(input, SCOPE_KEYS)) {
40
57
  throw invalidConfig('请提交完整的上下文增强设置。');
41
58
  }
42
- const { groupEnabled, directEnabled, fields, guidance } = input;
43
- if (typeof groupEnabled !== 'boolean' || typeof directEnabled !== 'boolean') {
59
+ const { enabled, fields, guidance } = input;
60
+ if (typeof enabled !== 'boolean') {
44
61
  throw invalidConfig('群聊和私聊开关必须是布尔值。');
45
62
  }
46
63
  if (!Array.isArray(fields) || ![...fields].every((field) => CONTEXT_ENHANCEMENT_FIELDS.includes(field))) {
@@ -50,19 +67,51 @@ export function validateContextEnhancementConfig(input) {
50
67
  throw invalidConfig(`增强提示词不得超过 ${CONTEXT_ENHANCEMENT_GUIDANCE_MAX_LENGTH} 个字符。`);
51
68
  }
52
69
  return Object.freeze({
53
- groupEnabled,
54
- directEnabled,
70
+ enabled,
55
71
  fields: Object.freeze(CONTEXT_ENHANCEMENT_FIELDS.filter((field) => fields.includes(field))),
56
72
  guidance: guidance.trim() ? guidance : '',
57
73
  });
58
74
  }
59
75
 
76
+ /** Validate the complete atomic save, preserving explicit empty selections/text. */
77
+ export function validateContextEnhancementConfig(input) {
78
+ if (!hasExactKeys(input, CONFIG_KEYS)) {
79
+ throw invalidConfig('请提交完整的上下文增强设置。');
80
+ }
81
+ return Object.freeze({
82
+ group: validateContextEnhancementScope(input.group),
83
+ direct: validateContextEnhancementScope(input.direct),
84
+ });
85
+ }
86
+
87
+ function migrateLegacyContextEnhancementConfig(input) {
88
+ if (!hasExactKeys(input, LEGACY_CONFIG_KEYS)) {
89
+ throw invalidConfig('请提交完整的上下文增强设置。');
90
+ }
91
+ return validateContextEnhancementConfig({
92
+ group: {
93
+ enabled: input.groupEnabled,
94
+ fields: input.fields,
95
+ guidance: input.guidance,
96
+ },
97
+ direct: {
98
+ enabled: input.directEnabled,
99
+ fields: input.fields,
100
+ guidance: input.guidance,
101
+ },
102
+ });
103
+ }
104
+
60
105
  /** Missing or damaged enhancement settings must never break an existing bot. */
61
106
  export function normalizeContextEnhancementConfig(input) {
62
107
  try {
63
108
  return validateContextEnhancementConfig(input);
64
109
  } catch {
65
- return DEFAULT_CONTEXT_ENHANCEMENT_CONFIG;
110
+ try {
111
+ return migrateLegacyContextEnhancementConfig(input);
112
+ } catch {
113
+ return DEFAULT_CONTEXT_ENHANCEMENT_CONFIG;
114
+ }
66
115
  }
67
116
  }
68
117
 
@@ -71,11 +120,15 @@ export function captureContextEnhancement(provider, conversationType) {
71
120
  if (conversationType !== 'group' && conversationType !== 'direct') return null;
72
121
  try {
73
122
  const settings = provider?.getSettings?.();
74
- const enabledKey = conversationType === 'group' ? 'groupEnabled' : 'directEnabled';
75
- if (settings?.[enabledKey] !== true) return null;
123
+ const legacyEnabledKey = conversationType === 'group' ? 'groupEnabled' : 'directEnabled';
124
+ const enabled = Object.hasOwn(settings ?? {}, conversationType)
125
+ ? settings?.[conversationType]?.enabled
126
+ : settings?.[legacyEnabledKey];
127
+ if (enabled !== true) return null;
76
128
  const config = normalizeContextEnhancementConfig(settings);
77
- if (config[enabledKey] !== true) return null;
78
- return Object.freeze({ config, botId: provider.botId, conversationType });
129
+ const scope = config[conversationType];
130
+ if (scope.enabled !== true) return null;
131
+ return Object.freeze({ config: scope, botId: provider.botId, conversationType });
79
132
  } catch {
80
133
  return null;
81
134
  }
@@ -185,4 +185,6 @@ export default {
185
185
  'Collected {count}/{limit} messages. The batch is full; send /send or /cancel.',
186
186
  '批量内容提交失败,已保留 {count} 条消息。\n请再次发送 /send 重试或 /cancel 取消。':
187
187
  'Batch submission failed; {count} messages were retained.\nSend /send to retry or /cancel to cancel.',
188
+ '你可以发送普通消息,但没有执行命令的权限。':
189
+ 'You can send regular messages, but you do not have permission to run commands.',
188
190
  };
@@ -0,0 +1,43 @@
1
+ import { evaluateAccessPolicy } from './access-policy.mjs';
2
+ import { isSharedLocalCommand } from './command-permission.mjs';
3
+
4
+ export const COMMAND_PERMISSION_DENIED_MESSAGE = '你可以发送普通消息,但没有执行命令的权限。';
5
+
6
+ const POLICY_NOT_CONFIGURED = Object.freeze({ allowed: true, reason: 'policy-not-configured' });
7
+ const PRIVILEGED_SENDER = Object.freeze({ allowed: true, reason: 'privileged-sender' });
8
+
9
+ /**
10
+ * Read one committed policy snapshot and decide a single inbound event.
11
+ * A missing provider is kept backward-compatible for direct bridge fixtures;
12
+ * production always injects a provider, whose missing/damaged value fails closed.
13
+ */
14
+ export function evaluateInboundAccess(accessPolicy, {
15
+ conversationType,
16
+ senderIds,
17
+ text = '',
18
+ hasImages = false,
19
+ hasFiles = false,
20
+ isCommand = isSharedLocalCommand(text, { hasImages, hasFiles }),
21
+ } = {}) {
22
+ if (!accessPolicy) return POLICY_NOT_CONFIGURED;
23
+ try {
24
+ if (typeof accessPolicy.isPrivileged === 'function'
25
+ && accessPolicy.isPrivileged(senderIds, conversationType) === true) {
26
+ return PRIVILEGED_SENDER;
27
+ }
28
+ } catch {
29
+ // A broken privilege lookup must not bypass the persisted policy.
30
+ }
31
+ let policy = null;
32
+ try {
33
+ policy = accessPolicy.getSettings();
34
+ } catch {
35
+ // Provider failures are equivalent to an unavailable persisted policy.
36
+ }
37
+ return evaluateAccessPolicy(policy, {
38
+ conversationType,
39
+ senderIds,
40
+ isCommand,
41
+ ...(typeof accessPolicy.equals === 'function' ? { equals: accessPolicy.equals } : {}),
42
+ });
43
+ }