@xmanrui/dsh-im 4.38.2 → 5.0.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.
- package/README.en.md +10 -2
- package/README.md +10 -2
- package/lib/client.js +7 -4
- package/lib/index.js +311 -306
- package/package.json +4 -3
- package/plugin-src/host/injected-context.mjs +13 -1
- package/plugin-src/host/modern-harness-api.mjs +10 -4
- package/src/channels/matrix/matrix-api.mjs +34 -0
- package/src/channels/matrix/matrix-config-store.mjs +50 -2
- package/src/channels/matrix/matrix-normalize.mjs +106 -14
- package/src/channels/matrix/matrix-room-history.mjs +240 -8
- package/src/channels/matrix/matrix-runtime.mjs +563 -3
- package/src/channels/qq/qq-bridge.mjs +147 -1
- package/src/channels/qq/state-store.mjs +89 -0
- package/src/channels/shared/context-enhancement.mjs +2 -0
- package/src/channels/shared/conversation/ambient-provider.mjs +115 -0
- package/src/channels/shared/conversation/context-builder.mjs +148 -0
- package/src/channels/shared/conversation/reply-context.mjs +95 -0
- package/src/channels/shared/conversation/root-context.mjs +101 -0
- package/src/channels/shared/conversation/route.mjs +71 -0
- package/src/channels/shared/conversation/semantic-message.mjs +75 -0
- package/src/channels/shared/conversation/trigger-policy.mjs +80 -0
- package/src/channels/shared/conversation-state-store.mjs +95 -1
- package/src/channels/shared/deferred-delivery-coordinator.mjs +5 -1
- package/src/channels/shared/harness-approval.mjs +19 -0
- package/src/channels/shared/harness-client.mjs +9 -1
- package/src/channels/shared/i18n-en/shared-a.mjs +2 -0
- package/src/channels/shared/i18n-en/shared-b.mjs +2 -0
- package/src/channels/shared/injected-context.mjs +64 -2
- package/src/channels/shared/prompt-failure-tracker.mjs +65 -0
- package/src/channels/shared/speaker-label.mjs +46 -0
- package/src/channels/shared/text-harness-bridge.mjs +504 -33
- package/src/channels/wecom/wecom-cards.mjs +1 -1
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** Correlate failures before user/message via the durable inbox's exact claim.
|
|
2
|
+
* This grants no live turn/interaction ownership and never guesses from the
|
|
3
|
+
* next turn/start: other callers may already be queued in the same Session.
|
|
4
|
+
*/
|
|
5
|
+
export class PromptFailureTracker {
|
|
6
|
+
#rpcId;
|
|
7
|
+
#seq;
|
|
8
|
+
#pending = new Map();
|
|
9
|
+
#openTurn = null;
|
|
10
|
+
#claimedTurn = null;
|
|
11
|
+
#failedTurn = null;
|
|
12
|
+
|
|
13
|
+
constructor(rpcId, afterSeq = -1) {
|
|
14
|
+
this.#rpcId = rpcId;
|
|
15
|
+
this.#seq = afterSeq;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
consume(event) {
|
|
19
|
+
const { seq, type, data = {} } = event;
|
|
20
|
+
if (!Number.isSafeInteger(seq) || seq <= this.#seq || this.#failedTurn !== null) {
|
|
21
|
+
return this.#failedTurn;
|
|
22
|
+
}
|
|
23
|
+
// Missing splices could have changed queue positions or canceled the input.
|
|
24
|
+
// Prefer no attribution over attributing another caller's failure.
|
|
25
|
+
if (seq !== this.#seq + 1) {
|
|
26
|
+
this.#pending.clear();
|
|
27
|
+
this.#openTurn = null;
|
|
28
|
+
this.#claimedTurn = null;
|
|
29
|
+
}
|
|
30
|
+
this.#seq = seq;
|
|
31
|
+
if (type === 'turn/start') this.#openTurn = data.turn ?? null;
|
|
32
|
+
if (type === 'agent/inbox/spliced') {
|
|
33
|
+
const { target, start, inserted, outcome } = data;
|
|
34
|
+
const count = data.removedCount ?? 0;
|
|
35
|
+
if (!['next-turn', 'next-step'].includes(target) || !Number.isSafeInteger(start) || start < 0
|
|
36
|
+
|| !Number.isSafeInteger(count) || count < 0 || !Array.isArray(inserted)) {
|
|
37
|
+
this.#pending.clear();
|
|
38
|
+
this.#claimedTurn = null;
|
|
39
|
+
return this.#failedTurn;
|
|
40
|
+
}
|
|
41
|
+
const positions = this.#pending.get(target) ?? [];
|
|
42
|
+
const next = [];
|
|
43
|
+
for (const index of positions) {
|
|
44
|
+
if (index < start) next.push(index);
|
|
45
|
+
else if (index >= start + count) next.push(index + inserted.length - count);
|
|
46
|
+
else if (outcome === undefined && inserted.length === 0 && Number.isSafeInteger(this.#openTurn)) {
|
|
47
|
+
this.#claimedTurn = this.#openTurn;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
inserted.forEach((message, index) => {
|
|
51
|
+
if (message?.source?.rpcId === this.#rpcId) next.push(start + index);
|
|
52
|
+
});
|
|
53
|
+
this.#pending.set(target, next);
|
|
54
|
+
}
|
|
55
|
+
if (type === 'turn/end') {
|
|
56
|
+
const kind = typeof data.reason === 'string' ? data.reason : data.reason?.kind;
|
|
57
|
+
if (Number.isSafeInteger(this.#claimedTurn) && data.turn === this.#claimedTurn && kind && kind !== 'completed') {
|
|
58
|
+
this.#failedTurn = data.turn;
|
|
59
|
+
}
|
|
60
|
+
if (data.turn === this.#claimedTurn) this.#claimedTurn = null;
|
|
61
|
+
this.#openTurn = null;
|
|
62
|
+
}
|
|
63
|
+
return this.#failedTurn;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Speaker labels for room-shared Sessions.
|
|
3
|
+
*
|
|
4
|
+
* A Matrix room routes every addressed message of the room into one shared
|
|
5
|
+
* conversation (see `conversation/route.mjs`: group keys carry no sender), so
|
|
6
|
+
* when several members question the same bot the persisted turn text alone
|
|
7
|
+
* cannot say whose question each turn carries. The bridge therefore labels the
|
|
8
|
+
* model-facing prompt of a group message with the sender's display name; the
|
|
9
|
+
* parsed control text, commands, and ledger keys never pass through here and
|
|
10
|
+
* stay unlabelled.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { INJECTED_CONTEXT_TAGS } from './context-enhancement.mjs';
|
|
14
|
+
|
|
15
|
+
/** Tags whose JSON blocks own their provenance and must never absorb a label. */
|
|
16
|
+
const BLOCK_OPENS = Object.freeze([
|
|
17
|
+
INJECTED_CONTEXT_TAGS.replyOpen,
|
|
18
|
+
INJECTED_CONTEXT_TAGS.sourceOpen,
|
|
19
|
+
INJECTED_CONTEXT_TAGS.contextOpen,
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
function isInjectedBlockPart(part) {
|
|
23
|
+
return part?.type === 'text'
|
|
24
|
+
&& typeof part.text === 'string'
|
|
25
|
+
&& BLOCK_OPENS.some((open) => part.text.startsWith(open));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Prefix the message body of one model-facing prompt with `speakerName: `.
|
|
30
|
+
* A string prompt is labelled inline; a structured content array is labelled
|
|
31
|
+
* on its first non-block text part so injected reply, source, and context
|
|
32
|
+
* blocks keep their own provenance. With no body text to carry the label — an
|
|
33
|
+
* image-only or reply-only turn — one label part is placed ahead of the media.
|
|
34
|
+
* @param {string|Array<Object>|undefined} content - the assembled prompt content.
|
|
35
|
+
* @param {?string} speakerName - the sender's display name; empty labels are dropped.
|
|
36
|
+
* @returns {string|Array<Object>|undefined} the labelled content, or the input unchanged when no label applies.
|
|
37
|
+
*/
|
|
38
|
+
export function prependSpeakerLabel(content, speakerName) {
|
|
39
|
+
const name = typeof speakerName === 'string' ? speakerName.trim() : '';
|
|
40
|
+
if (!name) return content;
|
|
41
|
+
if (typeof content === 'string') return content ? `${name}: ${content}` : content;
|
|
42
|
+
if (!Array.isArray(content)) return content;
|
|
43
|
+
const index = content.findIndex((part) => part?.type === 'text' && !isInjectedBlockPart(part));
|
|
44
|
+
if (index === -1) return [{ type: 'text', text: `${name}:` }, ...content];
|
|
45
|
+
return content.with(index, { ...content[index], text: `${name}: ${content[index].text ?? ''}` });
|
|
46
|
+
}
|