@xmanrui/dsh-im 4.39.0 → 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 +4 -3
- package/lib/index.js +279 -279
- package/package.json +4 -3
- package/plugin-src/host/modern-harness-api.mjs +10 -4
- package/src/channels/qq/qq-bridge.mjs +147 -1
- package/src/channels/qq/state-store.mjs +89 -0
- package/src/channels/shared/conversation-state-store.mjs +94 -0
- package/src/channels/shared/deferred-delivery-coordinator.mjs +5 -1
- package/src/channels/shared/harness-client.mjs +9 -1
- package/src/channels/shared/i18n-en/shared-b.mjs +2 -0
- package/src/channels/shared/prompt-failure-tracker.mjs +65 -0
- package/src/channels/shared/text-harness-bridge.mjs +156 -1
|
@@ -168,6 +168,13 @@ function controlTextOf(message) {
|
|
|
168
168
|
return cleanText(message?.controlText ?? message?.content);
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
/** Durable queue-entry id; the conversation key keeps ids unique across chats. */
|
|
172
|
+
function pendingMessageId(key, messageId) {
|
|
173
|
+
return `${key}\u0000${messageId}`;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const PENDING_RESTART_NOTICE = '排队中的消息在服务重启时被中断。为避免重复执行,未自动重放;如果该消息尚未处理,请重新发送。';
|
|
177
|
+
|
|
171
178
|
export class TextHarnessBridge {
|
|
172
179
|
#descriptor;
|
|
173
180
|
#bot;
|
|
@@ -183,6 +190,8 @@ export class TextHarnessBridge {
|
|
|
183
190
|
#signal;
|
|
184
191
|
#keepaliveIntervalMs;
|
|
185
192
|
#queues = new Map();
|
|
193
|
+
// Crash-recovery for #queues, started with the bridge; see recoverPendingMessages().
|
|
194
|
+
#pendingRecovery = null;
|
|
186
195
|
#busyMessages = new BusyMessageDispatcher();
|
|
187
196
|
#pendingInteractions = new Map();
|
|
188
197
|
#interactionKeys = new Map();
|
|
@@ -265,6 +274,10 @@ export class TextHarnessBridge {
|
|
|
265
274
|
label: descriptor.key,
|
|
266
275
|
logger,
|
|
267
276
|
});
|
|
277
|
+
// A queued message lives only in #queues. If the process dies before its
|
|
278
|
+
// turn runs the user has already been told it is queued, so the persisted
|
|
279
|
+
// entry must be replayed or reported as soon as the bridge starts.
|
|
280
|
+
this.recoverPendingMessages();
|
|
268
281
|
}
|
|
269
282
|
|
|
270
283
|
get status() {
|
|
@@ -747,9 +760,13 @@ export class TextHarnessBridge {
|
|
|
747
760
|
releaseMessageId = true,
|
|
748
761
|
alreadyRecorded = false,
|
|
749
762
|
} = {}) {
|
|
763
|
+
// Persist before the message joins the in-memory chain: the chain may wait
|
|
764
|
+
// behind a long turn, and a restart in that window must not lose the input.
|
|
765
|
+
const pending = this.#recordPendingMessage(message, messageId, senderId, key);
|
|
750
766
|
const previous = this.#queues.get(key) ?? Promise.resolve();
|
|
751
767
|
const current = previous
|
|
752
768
|
.catch(() => undefined)
|
|
769
|
+
.then(() => pending)
|
|
753
770
|
.then(() => this.#process(
|
|
754
771
|
message,
|
|
755
772
|
messageId,
|
|
@@ -757,14 +774,147 @@ export class TextHarnessBridge {
|
|
|
757
774
|
key,
|
|
758
775
|
{ alreadyRecorded },
|
|
759
776
|
))
|
|
760
|
-
.finally(() => {
|
|
777
|
+
.finally(async () => {
|
|
761
778
|
if (releaseMessageId) this.#acceptedMessageIds.delete(messageId);
|
|
762
779
|
if (this.#queues.get(key) === current) this.#queues.delete(key);
|
|
780
|
+
await this.#removePendingMessage(pendingMessageId(key, messageId));
|
|
763
781
|
});
|
|
764
782
|
this.#queues.set(key, current);
|
|
765
783
|
return current;
|
|
766
784
|
}
|
|
767
785
|
|
|
786
|
+
/**
|
|
787
|
+
* Replay (submitted: false) or report (submitted: true) the messages this
|
|
788
|
+
* bridge persisted before a restart. Idempotent: each entry is removed as it
|
|
789
|
+
* is handled, and the returned promise is shared with waitForIdle().
|
|
790
|
+
*/
|
|
791
|
+
recoverPendingMessages() {
|
|
792
|
+
this.#pendingRecovery ??= this.#recoverPendingMessages().catch((error) => {
|
|
793
|
+
this.#logger.warn?.(
|
|
794
|
+
`[dsh-im:${this.#descriptor.key}] queued-message recovery failed:`,
|
|
795
|
+
error?.message ?? error,
|
|
796
|
+
);
|
|
797
|
+
});
|
|
798
|
+
return this.#pendingRecovery;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
async #recoverPendingMessages() {
|
|
802
|
+
if (typeof this.#state?.pendingMessages !== 'function') return;
|
|
803
|
+
const entries = this.#state.pendingMessages();
|
|
804
|
+
if (!Array.isArray(entries) || entries.length === 0) return;
|
|
805
|
+
for (const entry of entries) {
|
|
806
|
+
if (!entry || this.#signal?.aborted) return;
|
|
807
|
+
try {
|
|
808
|
+
if (entry.submitted === true) {
|
|
809
|
+
await this.#notifyInterruptedPending(entry);
|
|
810
|
+
await this.#removePendingMessage(entry.id);
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
this.#logger.warn?.(
|
|
814
|
+
`[dsh-im:${this.#descriptor.key}] replaying a queued message that a restart interrupted:`,
|
|
815
|
+
entry.messageId,
|
|
816
|
+
);
|
|
817
|
+
// The turn never reached Harness, so any earlier mark-seen is an
|
|
818
|
+
// attempt, not a completed delivery; clear it so the replay can run.
|
|
819
|
+
if (typeof this.#state.unmarkSeen === 'function') {
|
|
820
|
+
await this.#state.unmarkSeen(entry.messageId).catch(() => undefined);
|
|
821
|
+
}
|
|
822
|
+
await this.#replayPendingMessage(entry);
|
|
823
|
+
} catch (error) {
|
|
824
|
+
this.#logger.warn?.(
|
|
825
|
+
`[dsh-im:${this.#descriptor.key}] failed to recover a queued message:`,
|
|
826
|
+
entry?.messageId,
|
|
827
|
+
error?.message ?? error,
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
#recordPendingMessage(message, messageId, senderId, key) {
|
|
834
|
+
if (typeof this.#state?.putPendingMessage !== 'function') return Promise.resolve();
|
|
835
|
+
const text = controlTextOf(message);
|
|
836
|
+
if (!text) return Promise.resolve();
|
|
837
|
+
const entry = {
|
|
838
|
+
id: pendingMessageId(key, messageId),
|
|
839
|
+
key,
|
|
840
|
+
messageId,
|
|
841
|
+
kind: message.kind === 'group' ? 'group' : 'direct',
|
|
842
|
+
text,
|
|
843
|
+
submitted: false,
|
|
844
|
+
createdAt: new Date().toISOString(),
|
|
845
|
+
...(senderId ? { senderId } : {}),
|
|
846
|
+
...(message.replyTarget !== undefined ? { target: message.replyTarget } : {}),
|
|
847
|
+
};
|
|
848
|
+
return this.#state.putPendingMessage(entry).catch((error) => {
|
|
849
|
+
this.#logger.warn?.(
|
|
850
|
+
`[dsh-im:${this.#descriptor.key}] failed to persist a queued message:`,
|
|
851
|
+
error?.message ?? error,
|
|
852
|
+
);
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
async #markPendingSubmitted(key, messageId) {
|
|
857
|
+
if (typeof this.#state?.patchPendingMessage !== 'function') return;
|
|
858
|
+
await this.#state.patchPendingMessage(pendingMessageId(key, messageId), { submitted: true });
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
async #removePendingMessage(id) {
|
|
862
|
+
if (typeof this.#state?.removePendingMessage !== 'function') return;
|
|
863
|
+
try {
|
|
864
|
+
await this.#state.removePendingMessage(id);
|
|
865
|
+
} catch (error) {
|
|
866
|
+
this.#logger.warn?.(
|
|
867
|
+
`[dsh-im:${this.#descriptor.key}] failed to clear a queued-message entry:`,
|
|
868
|
+
error?.message ?? error,
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
#replayPendingMessage(entry) {
|
|
874
|
+
const message = this.#pendingReplayMessage(entry);
|
|
875
|
+
if (!message) return Promise.resolve();
|
|
876
|
+
return this.#enqueueMessage(message, entry.messageId, entry.senderId ?? '', entry.key);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
#pendingReplayMessage(entry) {
|
|
880
|
+
const separator = String(entry.key).indexOf(':');
|
|
881
|
+
if (separator < 0) return null;
|
|
882
|
+
const kind = entry.key.slice(0, separator);
|
|
883
|
+
const conversationId = entry.key.slice(separator + 1);
|
|
884
|
+
if (!conversationId || !entry.messageId) return null;
|
|
885
|
+
// Rebuild exactly the turn fields the queue path reads, so the recovered
|
|
886
|
+
// text is dispatched as the user's own message with no added framing.
|
|
887
|
+
return {
|
|
888
|
+
messageId: entry.messageId,
|
|
889
|
+
senderId: entry.senderId ?? '',
|
|
890
|
+
kind: kind === 'group' ? 'group' : 'direct',
|
|
891
|
+
conversationId,
|
|
892
|
+
content: entry.text,
|
|
893
|
+
controlText: entry.text,
|
|
894
|
+
addressed: true,
|
|
895
|
+
plainText: true,
|
|
896
|
+
...(entry.target !== undefined ? { replyTarget: entry.target } : {}),
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
async #notifyInterruptedPending(entry) {
|
|
901
|
+
if (entry.target === undefined || entry.target === null) {
|
|
902
|
+
this.#logger.warn?.(
|
|
903
|
+
`[dsh-im:${this.#descriptor.key}] a queued message was interrupted by a restart but has no delivery target:`,
|
|
904
|
+
entry.messageId,
|
|
905
|
+
);
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
try {
|
|
909
|
+
await this.#bot.sendText(entry.target, t(PENDING_RESTART_NOTICE));
|
|
910
|
+
} catch (error) {
|
|
911
|
+
this.#logger.warn?.(
|
|
912
|
+
`[dsh-im:${this.#descriptor.key}] failed to report an interrupted queued message:`,
|
|
913
|
+
error?.message ?? error,
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
768
918
|
async #deliverDeferredOutcome(entry, outcome) {
|
|
769
919
|
const text = deferredOutcomeText(outcome);
|
|
770
920
|
return typeof this.#bot.sendDelivery === 'function'
|
|
@@ -773,6 +923,7 @@ export class TextHarnessBridge {
|
|
|
773
923
|
}
|
|
774
924
|
|
|
775
925
|
async waitForIdle() {
|
|
926
|
+
await this.#pendingRecovery;
|
|
776
927
|
await this.#busyMessages.whenIdle();
|
|
777
928
|
await Promise.allSettled([
|
|
778
929
|
...this.#queues.values(),
|
|
@@ -1118,6 +1269,10 @@ export class TextHarnessBridge {
|
|
|
1118
1269
|
// opened: the auto-steer lane keys its authorship decision off this
|
|
1119
1270
|
// record, and the finally below retires it when the turn settles.
|
|
1120
1271
|
this.#turnAuthors.set(conversationKey, { senderId, steered: 0, turnMessageId: messageId });
|
|
1272
|
+
// The prompt is about to enter the Harness inbox, which is itself durable.
|
|
1273
|
+
// Marking the entry submitted first means a crash after this point reports
|
|
1274
|
+
// the message instead of replaying a turn the Harness may already own.
|
|
1275
|
+
await this.#markPendingSubmitted(conversationKey, messageId);
|
|
1121
1276
|
const { sessionId, answer, artifacts = [] } = await askInWorkspaceSession({
|
|
1122
1277
|
deferredDelivery: () => ({ coordinator: this.#deferred, target: this.#descriptor.key === 'whatsapp' ? { jid: target.jid, selfChat: target.selfChat } : target }),
|
|
1123
1278
|
harness: this.#harness,
|