@xmanrui/dsh-im 4.21.0 → 4.21.2
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.
- package/README.en.md +10 -0
- package/README.md +10 -0
- package/lib/client.js +46 -9
- package/lib/index.js +281 -280
- package/package.json +1 -1
- package/plugin-src/client/channels/feishu/index.js +22 -7
- package/plugin-src/client/credential-binding.js +2 -0
- package/plugin-src/client/i18n.js +6 -0
- package/plugin-src/host/channels/feishu/rpc.mjs +2 -1
- package/plugin-src/host/index.mjs +7 -0
- package/plugin-src/host/injected-context.mjs +104 -0
- package/scripts/verify-lan-management.mjs +53 -8
- package/src/channels/dingtalk/dingtalk-bridge.mjs +104 -27
- package/src/channels/feishu/bridge.mjs +35 -2
- package/src/channels/qq/qq-bridge.mjs +27 -2
- package/src/channels/shared/context-enhancement.mjs +40 -3
- package/src/channels/shared/control-command.mjs +8 -1
- package/src/channels/shared/harness-client.mjs +7 -0
- package/src/channels/shared/i18n-en/dingtalk.mjs +1 -0
- package/src/channels/shared/i18n-en/weixin.mjs +2 -0
- package/src/channels/shared/im-source-guidance.mjs +65 -0
- package/src/channels/shared/injected-context.mjs +362 -0
- package/src/channels/shared/semantic/artifact.mjs +3 -3
- package/src/channels/shared/semantic/reply-reference.mjs +2 -1
- package/src/channels/shared/text-harness-bridge.mjs +23 -2
- package/src/channels/shared/workspace-session.mjs +9 -0
- package/src/channels/wecom/wecom-bridge.mjs +11 -1
- package/src/channels/wecom-app/wecom-app-bridge.mjs +11 -1
- package/src/channels/weixin/weixin-api.mjs +52 -13
- package/src/channels/weixin/weixin-bridge.mjs +13 -1
|
@@ -60,7 +60,11 @@ import {
|
|
|
60
60
|
} from '../shared/workspace-command.mjs';
|
|
61
61
|
import { askInWorkspaceSession } from '../shared/workspace-session.mjs';
|
|
62
62
|
import { createDeferredDeliveryCoordinator, deferredOutcomeText } from '../shared/deferred-delivery-coordinator.mjs';
|
|
63
|
-
import {
|
|
63
|
+
import {
|
|
64
|
+
captureContextEnhancement,
|
|
65
|
+
captureContextEnhancementSource,
|
|
66
|
+
enhanceContextContent,
|
|
67
|
+
} from '../shared/context-enhancement.mjs';
|
|
64
68
|
import { deliverOutboundArtifacts } from '../shared/semantic/artifact-delivery.mjs';
|
|
65
69
|
import {
|
|
66
70
|
createDeliveryReceipt,
|
|
@@ -1416,6 +1420,16 @@ export class FeishuHarnessBridge {
|
|
|
1416
1420
|
hasFiles: hasInboundFiles(message),
|
|
1417
1421
|
pendingInteraction: this.#hasPendingInteraction(key),
|
|
1418
1422
|
control: { owner: this, key },
|
|
1423
|
+
enhancement: captureContextEnhancementSource(
|
|
1424
|
+
this.#contextEnhancement,
|
|
1425
|
+
event.message.chat_type === 'p2p' ? 'direct' : 'group',
|
|
1426
|
+
() => ({
|
|
1427
|
+
channel: 'feishu',
|
|
1428
|
+
senderId: senderOpenId(event),
|
|
1429
|
+
chatId: event.message.chat_id,
|
|
1430
|
+
threadId: event.message.thread_id,
|
|
1431
|
+
}),
|
|
1432
|
+
),
|
|
1419
1433
|
},
|
|
1420
1434
|
);
|
|
1421
1435
|
if (result?.stopped) {
|
|
@@ -2475,7 +2489,7 @@ export class FeishuHarnessBridge {
|
|
|
2475
2489
|
await this.#sendCard(chatId, customSteerCard(), { key, updateMessageId: messageId, replyTo: messageId });
|
|
2476
2490
|
return;
|
|
2477
2491
|
}
|
|
2478
|
-
await this.#sendSteer({ key, chatId, messageId }, raw);
|
|
2492
|
+
await this.#sendSteer({ key, chatId, messageId, actor }, raw);
|
|
2479
2493
|
return;
|
|
2480
2494
|
}
|
|
2481
2495
|
if (action === 'presets') {
|
|
@@ -3201,11 +3215,26 @@ export class FeishuHarnessBridge {
|
|
|
3201
3215
|
*/
|
|
3202
3216
|
async #sendSteer(entry, text) {
|
|
3203
3217
|
const { key, chatId } = entry;
|
|
3218
|
+
// Card routes carry the conversation key, not the raw event, so the topic
|
|
3219
|
+
// id is recovered from the key the channel itself minted.
|
|
3220
|
+
const threadId = typeof key === 'string'
|
|
3221
|
+
? /(?:^|:)thread:(.+)$/u.exec(key)?.[1]
|
|
3222
|
+
: undefined;
|
|
3204
3223
|
const result = await runControlCommand(
|
|
3205
3224
|
`/steer ${text}`, this.#harness, this.#state, key, {
|
|
3206
3225
|
signal: this.#signal,
|
|
3207
3226
|
pendingInteraction: this.#hasPendingInteraction(key),
|
|
3208
3227
|
control: { owner: this, key },
|
|
3228
|
+
enhancement: captureContextEnhancementSource(
|
|
3229
|
+
this.#contextEnhancement,
|
|
3230
|
+
typeof key === 'string' && key.startsWith('p2p:') ? 'direct' : 'group',
|
|
3231
|
+
() => ({
|
|
3232
|
+
channel: 'feishu',
|
|
3233
|
+
senderId: entry.actor ?? entry.operatorOpenId,
|
|
3234
|
+
chatId,
|
|
3235
|
+
threadId,
|
|
3236
|
+
}),
|
|
3237
|
+
),
|
|
3209
3238
|
},
|
|
3210
3239
|
);
|
|
3211
3240
|
await this.#send(chatId, result?.message || t('已提交补充指令。'), { replyTo: entry.messageId ?? null });
|
|
@@ -4964,6 +4993,7 @@ export class FeishuHarnessBridge {
|
|
|
4964
4993
|
text,
|
|
4965
4994
|
content,
|
|
4966
4995
|
titleText: event.batchSubmission?.title,
|
|
4996
|
+
sourceGuidance: snapshot?.config?.guidance,
|
|
4967
4997
|
contextEnhanced,
|
|
4968
4998
|
createOptions: { signal: this.#signal },
|
|
4969
4999
|
existsOptions: { signal: this.#signal },
|
|
@@ -5207,6 +5237,7 @@ export class FeishuHarnessBridge {
|
|
|
5207
5237
|
text,
|
|
5208
5238
|
content,
|
|
5209
5239
|
titleText: event.batchSubmission?.title,
|
|
5240
|
+
sourceGuidance: snapshot?.config?.guidance,
|
|
5210
5241
|
contextEnhanced,
|
|
5211
5242
|
createOptions: { signal: this.#signal },
|
|
5212
5243
|
existsOptions: { signal: this.#signal },
|
|
@@ -5287,6 +5318,7 @@ export class FeishuHarnessBridge {
|
|
|
5287
5318
|
text,
|
|
5288
5319
|
content,
|
|
5289
5320
|
titleText: event.batchSubmission?.title,
|
|
5321
|
+
sourceGuidance: snapshot?.config?.guidance,
|
|
5290
5322
|
contextEnhanced,
|
|
5291
5323
|
createOptions: { signal: this.#signal },
|
|
5292
5324
|
existsOptions: { signal: this.#signal },
|
|
@@ -5358,6 +5390,7 @@ export class FeishuHarnessBridge {
|
|
|
5358
5390
|
text,
|
|
5359
5391
|
content,
|
|
5360
5392
|
titleText: event.batchSubmission?.title,
|
|
5393
|
+
sourceGuidance: snapshot?.config?.guidance,
|
|
5361
5394
|
contextEnhanced,
|
|
5362
5395
|
createOptions: { signal: this.#signal },
|
|
5363
5396
|
existsOptions: { signal: this.#signal },
|
|
@@ -22,7 +22,11 @@ import {
|
|
|
22
22
|
runPresetCommand,
|
|
23
23
|
} from '../shared/preset-command.mjs';
|
|
24
24
|
import { askInWorkspaceSession } from '../shared/workspace-session.mjs';
|
|
25
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
captureContextEnhancement,
|
|
27
|
+
captureContextEnhancementSource,
|
|
28
|
+
enhanceContextContent,
|
|
29
|
+
} from '../shared/context-enhancement.mjs';
|
|
26
30
|
import {
|
|
27
31
|
BatchInputManager,
|
|
28
32
|
batchInputBusyMessage,
|
|
@@ -759,7 +763,17 @@ export class QqHarnessBridge {
|
|
|
759
763
|
return { message: t('当前任务仍在运行,请先停止任务或等待任务完成后再执行此操作。') };
|
|
760
764
|
}
|
|
761
765
|
const options = { signal: this.#signal, isDirect: message.kind === 'c2c', pendingInteraction,
|
|
762
|
-
control: { owner: this, key }, deferredDelivery: this.#deferred
|
|
766
|
+
control: { owner: this, key }, deferredDelivery: this.#deferred,
|
|
767
|
+
enhancement: captureContextEnhancementSource(
|
|
768
|
+
this.#contextEnhancement,
|
|
769
|
+
message.kind === 'c2c' ? 'direct' : 'group',
|
|
770
|
+
() => ({
|
|
771
|
+
channel: 'qq',
|
|
772
|
+
senderId: nonEmptyString(message.senderId),
|
|
773
|
+
senderName: message.kind === 'group' ? message.senderName : undefined,
|
|
774
|
+
chatId: message.kind === 'group' ? message.groupOpenid : message.senderId,
|
|
775
|
+
}),
|
|
776
|
+
) };
|
|
763
777
|
// Existing runners own all Host mutations and control authorization.
|
|
764
778
|
const execute = async () => {
|
|
765
779
|
this.#signal?.throwIfAborted();
|
|
@@ -824,6 +838,16 @@ export class QqHarnessBridge {
|
|
|
824
838
|
|| this.#approvals.hasPending(key),
|
|
825
839
|
control: { owner: this, key },
|
|
826
840
|
deferredDelivery: this.#deferred,
|
|
841
|
+
enhancement: captureContextEnhancementSource(
|
|
842
|
+
this.#contextEnhancement,
|
|
843
|
+
message.kind === 'c2c' ? 'direct' : 'group',
|
|
844
|
+
() => ({
|
|
845
|
+
channel: 'qq',
|
|
846
|
+
senderId: nonEmptyString(message.senderId),
|
|
847
|
+
senderName: message.kind === 'group' ? message.senderName : undefined,
|
|
848
|
+
chatId: message.kind === 'group' ? message.groupOpenid : message.senderId,
|
|
849
|
+
}),
|
|
850
|
+
),
|
|
827
851
|
});
|
|
828
852
|
if (result?.stopped) {
|
|
829
853
|
await Promise.allSettled([
|
|
@@ -1046,6 +1070,7 @@ export class QqHarnessBridge {
|
|
|
1046
1070
|
text,
|
|
1047
1071
|
content,
|
|
1048
1072
|
titleText: batchSubmission?.title,
|
|
1073
|
+
sourceGuidance: snapshot?.config?.guidance,
|
|
1049
1074
|
contextEnhanced,
|
|
1050
1075
|
createOptions: { signal: this.#signal },
|
|
1051
1076
|
existsOptions: { signal: this.#signal },
|
|
@@ -4,6 +4,23 @@ export const CONTEXT_ENHANCEMENT_FIELDS = Object.freeze([
|
|
|
4
4
|
'chatId', 'threadId', 'botId',
|
|
5
5
|
]);
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Tag grammar of the injected context prefix. The producer here and the
|
|
9
|
+
* Host-side splitter in `injected-context.mjs` share these literals, so the
|
|
10
|
+
* prefix a channel writes can never drift from the parser that pairs it.
|
|
11
|
+
*/
|
|
12
|
+
export const INJECTED_CONTEXT_TAGS = Object.freeze({
|
|
13
|
+
sourceOpen: '<dsh_im_source>',
|
|
14
|
+
sourceClose: '</dsh_im_source>',
|
|
15
|
+
guidanceOpen: '<dsh_im_source_guidance>',
|
|
16
|
+
guidanceClose: '</dsh_im_source_guidance>',
|
|
17
|
+
replyOpen: '<dsh_im_reply_to>',
|
|
18
|
+
replyClose: '</dsh_im_reply_to>',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/** Separator the producer joins prefix blocks with, and the splitter consumes. */
|
|
22
|
+
export const INJECTED_CONTEXT_SEPARATOR = '\n\n';
|
|
23
|
+
|
|
7
24
|
export const CONTEXT_ENHANCEMENT_GUIDANCE_MAX_LENGTH = 8_000;
|
|
8
25
|
export const CONTEXT_GROUP_GUIDANCE_EXAMPLE = `仅依据当前消息的 <dsh_im_source> 中实际提供的字段理解来源;没有提供的字段不要猜测或补全。
|
|
9
26
|
当前消息来自群聊,请使用严肃、克制、简洁的表达方式。`;
|
|
@@ -138,6 +155,26 @@ export function captureContextEnhancement(provider, conversationType) {
|
|
|
138
155
|
}
|
|
139
156
|
}
|
|
140
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Capture the enhancement one prompt replays, together with the source factory
|
|
160
|
+
* that fills its selected fields.
|
|
161
|
+
*
|
|
162
|
+
* Ordinary messages snapshot this when they are accepted, so a queued message
|
|
163
|
+
* keeps the settings it arrived under. A control command is never queued, so it
|
|
164
|
+
* captures at the moment it runs -- and it must, because the source fields of a
|
|
165
|
+
* steering instruction belong to whoever issued it, not to the message that
|
|
166
|
+
* opened the turn.
|
|
167
|
+
*
|
|
168
|
+
* @param provider - the bot's enhancement provider.
|
|
169
|
+
* @param conversationType - the inbound message's scope.
|
|
170
|
+
* @param source - factory for the currently selected source fields.
|
|
171
|
+
* @returns the enhancement to apply, or null when the scope is off.
|
|
172
|
+
*/
|
|
173
|
+
export function captureContextEnhancementSource(provider, conversationType, source) {
|
|
174
|
+
const snapshot = captureContextEnhancement(provider, conversationType);
|
|
175
|
+
return snapshot === null ? null : Object.freeze({ snapshot, source });
|
|
176
|
+
}
|
|
177
|
+
|
|
141
178
|
function sourceString(value, field) {
|
|
142
179
|
if (field === 'senderId' && (typeof value === 'bigint' || Number.isFinite(value))) {
|
|
143
180
|
value = String(value);
|
|
@@ -165,7 +202,7 @@ function sourceBlock(snapshot, sourceFactory) {
|
|
|
165
202
|
const json = JSON.stringify(projected).replace(/[<>&]/g, (character) => ({
|
|
166
203
|
'<': '\\u003c', '>': '\\u003e', '&': '\\u0026',
|
|
167
204
|
})[character]);
|
|
168
|
-
return
|
|
205
|
+
return `${INJECTED_CONTEXT_TAGS.sourceOpen}${json}${INJECTED_CONTEXT_TAGS.sourceClose}`;
|
|
169
206
|
}
|
|
170
207
|
|
|
171
208
|
function guidanceBlock(guidance) {
|
|
@@ -173,7 +210,7 @@ function guidanceBlock(guidance) {
|
|
|
173
210
|
const body = guidance.replace(/<\/?dsh_im_source_guidance\b[^>]*(?:>|$)/gi, (tag) => (
|
|
174
211
|
tag.replace(/</g, '<').replace(/>/g, '>')
|
|
175
212
|
));
|
|
176
|
-
return
|
|
213
|
+
return `${INJECTED_CONTEXT_TAGS.guidanceOpen}\n${body}\n${INJECTED_CONTEXT_TAGS.guidanceClose}`;
|
|
177
214
|
}
|
|
178
215
|
|
|
179
216
|
/** Add one text prefix; never inspect sources, format or copy content when off. */
|
|
@@ -183,7 +220,7 @@ export function enhanceContextContent(content, snapshot, sourceFactory) {
|
|
|
183
220
|
const blocks = [sourceBlock(snapshot, sourceFactory), guidanceBlock(snapshot.config.guidance)]
|
|
184
221
|
.filter(Boolean);
|
|
185
222
|
if (blocks.length === 0) return content;
|
|
186
|
-
const prefix = blocks.join(
|
|
223
|
+
const prefix = blocks.join(INJECTED_CONTEXT_SEPARATOR);
|
|
187
224
|
if (typeof content === 'string') return `${prefix}\n\n${content}`;
|
|
188
225
|
if (Array.isArray(content)) return [{ type: 'text', text: prefix }, ...content];
|
|
189
226
|
return content;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { enhanceContextContent } from './context-enhancement.mjs';
|
|
1
2
|
import { t } from './i18n.mjs';
|
|
2
3
|
import manifest from '../../../package.json' with { type: 'json' };
|
|
3
4
|
|
|
@@ -41,6 +42,7 @@ export async function runControlCommand(text, harness, state, key, {
|
|
|
41
42
|
pendingInteraction = false,
|
|
42
43
|
control,
|
|
43
44
|
deferredDelivery,
|
|
45
|
+
enhancement,
|
|
44
46
|
} = {}) {
|
|
45
47
|
if (!isControlCommand(text)) return null;
|
|
46
48
|
const command = text.trim();
|
|
@@ -93,8 +95,13 @@ export async function runControlCommand(text, harness, state, key, {
|
|
|
93
95
|
if (typeof session.steerActiveTurn !== 'function') {
|
|
94
96
|
throw new TypeError('Harness session does not support steering active turns');
|
|
95
97
|
}
|
|
98
|
+
// A mid-turn correction carries the same provenance as the message that
|
|
99
|
+
// opened the turn, so a group member who steers is identified too.
|
|
100
|
+
const steering = enhancement
|
|
101
|
+
? enhanceContextContent(instruction, enhancement.snapshot, enhancement.source)
|
|
102
|
+
: instruction;
|
|
96
103
|
const steered = await session.steerActiveTurn(
|
|
97
|
-
|
|
104
|
+
steering,
|
|
98
105
|
control,
|
|
99
106
|
requestOptions(signal),
|
|
100
107
|
);
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
imageFileSourcesFromContent,
|
|
14
14
|
isModelImageRejection,
|
|
15
15
|
} from './image-prompt.mjs';
|
|
16
|
+
import { imSourceGuidance } from './im-source-guidance.mjs';
|
|
16
17
|
import { outboundArtifactRegistry } from './semantic/artifact.mjs';
|
|
17
18
|
import { t } from './i18n.mjs';
|
|
18
19
|
import { watchHarnessMux } from './harness-mux.mjs';
|
|
@@ -1508,6 +1509,12 @@ export class HarnessClient {
|
|
|
1508
1509
|
if (!Array.isArray(content) || content.length === 0) {
|
|
1509
1510
|
throw new TypeError('Harness prompt content is required');
|
|
1510
1511
|
}
|
|
1512
|
+
// Publish the guidance the channel's own captured settings produced, so
|
|
1513
|
+
// the Host materializes it once per Session as prompt context instead of
|
|
1514
|
+
// per user message. It is passed in and never parsed back out of
|
|
1515
|
+
// `content`: the prompt also carries whatever the user typed, and a
|
|
1516
|
+
// message that merely looks like a guidance block is not configuration.
|
|
1517
|
+
imSourceGuidance.publish(sessionId, options.sourceGuidance);
|
|
1511
1518
|
const clientTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
1512
1519
|
const sendPrompt = (promptContent) => this.rpc('session.prompt', {
|
|
1513
1520
|
sessionId,
|
|
@@ -16,6 +16,7 @@ export default {
|
|
|
16
16
|
|
|
17
17
|
'钉钉未能换取图片下载地址,请重新发送;若持续失败,请检查机器人的“企业内机器人发送消息权限”。': 'DingTalk could not provide the image download address. Please resend; if it keeps failing, check the bot\'s "Send messages as an internal robot" permission.',
|
|
18
18
|
'钉钉没有返回图片下载地址,请重新发送。': 'DingTalk did not return an image download address. Please resend.',
|
|
19
|
+
'钉钉未提供引用附件的下载信息,无法读取原附件。请直接重新发送附件后再提问。': 'DingTalk did not provide download information for the quoted attachment, so it cannot be read. Please resend the attachment directly with your question.',
|
|
19
20
|
'钉钉返回的图片临时地址无法读取,请重新发送。': 'The temporary image address returned by DingTalk could not be read. Please resend.',
|
|
20
21
|
'结果文件「{name}」发送结果未能确认,请先检查聊天内是否已收到,不要立即重试。': 'Delivery of the result file "{name}" could not be confirmed. Please check whether it already arrived in the chat before retrying.',
|
|
21
22
|
'结果文件「{name}」已生成,但钉钉应用或机器人缺少文件消息权限。请开通应用 qyapi_base 权限,并确认机器人具备文件消息发送能力。': 'Result file "{name}" was generated, but the DingTalk app or bot lacks file message permission. Enable the app\'s qyapi_base permission and make sure the bot can send file messages.',
|
|
@@ -6,6 +6,7 @@ export default {
|
|
|
6
6
|
'微信已连接 DeepSeek Harness。': 'WeChat is connected to DeepSeek Harness.',
|
|
7
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.',
|
|
8
8
|
'结果文件「{name}」超过当前微信会话可发送的文件大小,未发送。': 'The result file "{name}" exceeds the file size limit of the current WeChat conversation and was not sent.',
|
|
9
|
+
'结果文件「{name}」上传微信时长时间没有进展,已超时,文件尚未发送。请检查网络后重试,或压缩、拆分文件后发送。': 'The upload of "{name}" to WeChat stalled and timed out. The file has not been sent. Check your network and retry, or compress or split the file before sending.',
|
|
9
10
|
'结果文件「{name}」暂时被微信限流,未能发送,请稍后重试。': 'The result file "{name}" was temporarily rate-limited by WeChat and was not sent. Please try again later.',
|
|
10
11
|
'结果文件「{name}」已生成,但微信拒绝了该文件消息。': 'The result file "{name}" was generated, but WeChat rejected the file message.',
|
|
11
12
|
'结果文件「{name}」已生成,但暂时未能通过微信发送,请稍后重试。': 'The result file "{name}" was generated but could not be sent via WeChat right now. Please try again later.',
|
|
@@ -72,6 +73,7 @@ export default {
|
|
|
72
73
|
'微信文件上传失败(HTTP {status})。': 'The WeChat file upload failed (HTTP {status}).',
|
|
73
74
|
'微信文件上传响应缺少下载参数。': 'The WeChat file upload response is missing the download parameter.',
|
|
74
75
|
'微信文件上传失败。': 'The WeChat file upload failed.',
|
|
76
|
+
'微信文件上传长时间没有进展,已超时。': 'The WeChat file upload stalled and timed out.',
|
|
75
77
|
'拒绝访问不受信任的微信服务地址。': 'Refusing to access an untrusted WeChat service URL.',
|
|
76
78
|
'微信服务请求失败(HTTP {status})。': 'The WeChat service request failed (HTTP {status}).',
|
|
77
79
|
'微信服务返回了无法解析的响应。': 'The WeChat service returned an unparseable response.',
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Session-keyed source guidance for the current conversation.
|
|
2
|
+
//
|
|
3
|
+
// The prompt RPC carries no message source, so a channel publishes the guidance
|
|
4
|
+
// it would otherwise repeat inside every user message when it dispatches the
|
|
5
|
+
// prompt. The Host materializes it once per session as dynamic prompt context
|
|
6
|
+
// (`systemPrompt.context`), which appends a durable snapshot only when the
|
|
7
|
+
// rendered text changes. The registry is module-level because one process owns
|
|
8
|
+
// exactly one, the same shape `outboundArtifactRegistry` uses.
|
|
9
|
+
|
|
10
|
+
/** Prompt-context name the Host materializes this guidance under. */
|
|
11
|
+
export const IM_SOURCE_GUIDANCE_CONTEXT = 'dsh-im:source-guidance';
|
|
12
|
+
|
|
13
|
+
/** Contexts are joined in ascending order; this one follows the policy facts. */
|
|
14
|
+
export const IM_SOURCE_GUIDANCE_ORDER = 125;
|
|
15
|
+
|
|
16
|
+
/** Bound for one session's guidance; the settings cap is the same size. */
|
|
17
|
+
const GUIDANCE_MAX_LENGTH = 8_000;
|
|
18
|
+
|
|
19
|
+
/** Bound on retained sessions, so an abandoned conversation cannot grow it. */
|
|
20
|
+
const MAX_SESSIONS = 1_024;
|
|
21
|
+
|
|
22
|
+
class ImSourceGuidanceRegistry {
|
|
23
|
+
#bySession = new Map();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Publish the guidance in force for one session. Empty guidance clears it, so
|
|
27
|
+
* a conversation that turns enhancement off stops contributing a snapshot.
|
|
28
|
+
* @param sessionId - the Session the prompt was dispatched to.
|
|
29
|
+
* @param guidance - the scope's guidance text, or empty when none applies.
|
|
30
|
+
*/
|
|
31
|
+
publish(sessionId, guidance) {
|
|
32
|
+
if (typeof sessionId !== 'string' || !sessionId) return;
|
|
33
|
+
const text = typeof guidance === 'string' ? guidance.slice(0, GUIDANCE_MAX_LENGTH) : '';
|
|
34
|
+
// Delete first so the re-inserted key is the most recently used one.
|
|
35
|
+
this.#bySession.delete(sessionId);
|
|
36
|
+
if (!text.trim()) return;
|
|
37
|
+
this.#bySession.set(sessionId, text);
|
|
38
|
+
while (this.#bySession.size > MAX_SESSIONS) {
|
|
39
|
+
this.#bySession.delete(this.#bySession.keys().next().value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param sessionId - a Session id, or anything else.
|
|
45
|
+
* @returns the guidance in force for that session, or undefined.
|
|
46
|
+
*/
|
|
47
|
+
get(sessionId) {
|
|
48
|
+
return typeof sessionId === 'string' ? this.#bySession.get(sessionId) : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Drop one session's guidance.
|
|
53
|
+
* @param sessionId - the Session leaving the registry.
|
|
54
|
+
*/
|
|
55
|
+
forget(sessionId) {
|
|
56
|
+
if (typeof sessionId === 'string') this.#bySession.delete(sessionId);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @returns how many sessions currently carry guidance. */
|
|
60
|
+
get size() {
|
|
61
|
+
return this.#bySession.size;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const imSourceGuidance = new ImSourceGuidanceRegistry();
|