@privateclaw/privateclaw 0.1.24 → 0.1.26
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.md +22 -0
- package/dist/bot-mode-topics.d.ts +7 -0
- package/dist/bot-mode-topics.js +236 -0
- package/dist/bot-mode-topics.js.map +1 -0
- package/dist/cli.js +24 -0
- package/dist/cli.js.map +1 -1
- package/dist/compat/openclaw.d.ts +14 -0
- package/dist/compat/openclaw.js +14 -0
- package/dist/compat/openclaw.js.map +1 -1
- package/dist/openclaw-plugin.d.ts +3 -0
- package/dist/openclaw-plugin.js +35 -0
- package/dist/openclaw-plugin.js.map +1 -1
- package/dist/provider.d.ts +19 -1
- package/dist/provider.js +579 -140
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +15 -0
- package/openclaw.plugin.json +15 -1
- package/package.json +1 -1
- package/src/bot-mode-topics.test.ts +39 -0
- package/src/bot-mode-topics.ts +248 -0
- package/src/cli.ts +33 -0
- package/src/compat/openclaw.ts +15 -0
- package/src/openclaw-plugin.test.ts +3 -0
- package/src/openclaw-plugin.ts +46 -0
- package/src/provider.test.ts +794 -1
- package/src/provider.ts +775 -174
- package/src/types.ts +15 -0
package/src/provider.ts
CHANGED
|
@@ -14,6 +14,10 @@ import {
|
|
|
14
14
|
type UserMessagePayload,
|
|
15
15
|
} from "@privateclaw/protocol";
|
|
16
16
|
import QRCode from "qrcode";
|
|
17
|
+
import {
|
|
18
|
+
BOT_MODE_IDLE_TOPICS,
|
|
19
|
+
pickBotModeIdleTopic,
|
|
20
|
+
} from "./bot-mode-topics.js";
|
|
17
21
|
import {
|
|
18
22
|
PRIVATECLAW_QR_ERROR_CORRECTION_LEVEL,
|
|
19
23
|
PRIVATECLAW_QR_IMAGE_MARGIN,
|
|
@@ -35,6 +39,7 @@ import type {
|
|
|
35
39
|
BridgeMessage,
|
|
36
40
|
BridgeResponse,
|
|
37
41
|
PrivateClawConversationTurn,
|
|
42
|
+
PrivateClawAgentBridge,
|
|
38
43
|
PrivateClawInviteBundle,
|
|
39
44
|
PrivateClawProviderHandoffState,
|
|
40
45
|
PrivateClawManagedSession,
|
|
@@ -47,6 +52,9 @@ import type {
|
|
|
47
52
|
export const DEFAULT_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
48
53
|
const RENEW_SESSION_TTL_MS = DEFAULT_SESSION_TTL_MS;
|
|
49
54
|
const SESSION_RENEWAL_REMINDER_WINDOW_MS = 30 * 60 * 1000;
|
|
55
|
+
const BOT_MODE_SILENT_JOIN_DELAY_MS = 10 * 60 * 1000;
|
|
56
|
+
const BOT_MODE_IDLE_DELAY_MS = 20 * 60 * 1000;
|
|
57
|
+
const BRIDGE_NO_REPLY_SENTINEL = "NO_REPLY";
|
|
50
58
|
const PARTICIPANT_LABEL_MAX_CHARS = 12;
|
|
51
59
|
const PARTICIPANT_FALLBACK_PREFIXES = [
|
|
52
60
|
"流萤",
|
|
@@ -91,6 +99,25 @@ function normalizeBridgeMessages(
|
|
|
91
99
|
);
|
|
92
100
|
}
|
|
93
101
|
|
|
102
|
+
function isBridgeNoReplyMessage(message: {
|
|
103
|
+
text: string;
|
|
104
|
+
attachments?: ReadonlyArray<PrivateClawAttachment>;
|
|
105
|
+
}): boolean {
|
|
106
|
+
return (
|
|
107
|
+
(!message.attachments || message.attachments.length === 0) &&
|
|
108
|
+
message.text.trim() === BRIDGE_NO_REPLY_SENTINEL
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function hasOnlyBridgeNoReplyMessages(
|
|
113
|
+
messages: ReadonlyArray<{
|
|
114
|
+
text: string;
|
|
115
|
+
attachments?: ReadonlyArray<PrivateClawAttachment>;
|
|
116
|
+
}>,
|
|
117
|
+
): boolean {
|
|
118
|
+
return messages.length > 0 && messages.every((message) => isBridgeNoReplyMessage(message));
|
|
119
|
+
}
|
|
120
|
+
|
|
94
121
|
function nowIso(): string {
|
|
95
122
|
return new Date().toISOString();
|
|
96
123
|
}
|
|
@@ -286,6 +313,18 @@ function toProviderSessionHandoff(
|
|
|
286
313
|
...(session.renewalReminderSentAt
|
|
287
314
|
? { renewalReminderSentAt: session.renewalReminderSentAt }
|
|
288
315
|
: {}),
|
|
316
|
+
...(session.lastGroupActivityAt
|
|
317
|
+
? { lastGroupActivityAt: session.lastGroupActivityAt }
|
|
318
|
+
: {}),
|
|
319
|
+
...(session.botModeIdleAnchorAt
|
|
320
|
+
? { botModeIdleAnchorAt: session.botModeIdleAnchorAt }
|
|
321
|
+
: {}),
|
|
322
|
+
...(session.botModeLastIdlePromptAt
|
|
323
|
+
? { botModeLastIdlePromptAt: session.botModeLastIdlePromptAt }
|
|
324
|
+
: {}),
|
|
325
|
+
...(session.botModeLastIdleTopicId
|
|
326
|
+
? { botModeLastIdleTopicId: session.botModeLastIdleTopicId }
|
|
327
|
+
: {}),
|
|
289
328
|
};
|
|
290
329
|
}
|
|
291
330
|
|
|
@@ -309,9 +348,27 @@ function fromProviderSessionHandoff(
|
|
|
309
348
|
...(snapshot.renewalReminderSentAt
|
|
310
349
|
? { renewalReminderSentAt: snapshot.renewalReminderSentAt }
|
|
311
350
|
: {}),
|
|
351
|
+
...(snapshot.lastGroupActivityAt
|
|
352
|
+
? { lastGroupActivityAt: snapshot.lastGroupActivityAt }
|
|
353
|
+
: {}),
|
|
354
|
+
...(snapshot.botModeIdleAnchorAt
|
|
355
|
+
? { botModeIdleAnchorAt: snapshot.botModeIdleAnchorAt }
|
|
356
|
+
: {}),
|
|
357
|
+
...(snapshot.botModeLastIdlePromptAt
|
|
358
|
+
? { botModeLastIdlePromptAt: snapshot.botModeLastIdlePromptAt }
|
|
359
|
+
: {}),
|
|
360
|
+
...(snapshot.botModeLastIdleTopicId
|
|
361
|
+
? { botModeLastIdleTopicId: snapshot.botModeLastIdleTopicId }
|
|
362
|
+
: {}),
|
|
363
|
+
botModeSilentJoinTimers: new Map<string, ReturnType<typeof setTimeout>>(),
|
|
312
364
|
};
|
|
313
365
|
}
|
|
314
366
|
|
|
367
|
+
function describeElapsedMinutes(elapsedMs: number): string {
|
|
368
|
+
const minutes = Math.max(1, Math.round(elapsedMs / 60_000));
|
|
369
|
+
return `${minutes} minute${minutes === 1 ? "" : "s"}`;
|
|
370
|
+
}
|
|
371
|
+
|
|
315
372
|
function buildRenewalReminderMessage(session: ProviderSessionState): string {
|
|
316
373
|
const command = "/renew-session";
|
|
317
374
|
if (session.groupMode) {
|
|
@@ -614,6 +671,90 @@ function buildBotAlreadyUnmutedMessage(): string {
|
|
|
614
671
|
);
|
|
615
672
|
}
|
|
616
673
|
|
|
674
|
+
function buildBotModeSilentJoinPrompt(params: {
|
|
675
|
+
participantLabel: string;
|
|
676
|
+
joinedAt: string;
|
|
677
|
+
languageInstruction?: string;
|
|
678
|
+
nowMs?: number;
|
|
679
|
+
}): string {
|
|
680
|
+
const joinedAtMs = new Date(params.joinedAt).getTime();
|
|
681
|
+
const elapsedText = describeElapsedMinutes(
|
|
682
|
+
Math.max(0, (params.nowMs ?? Date.now()) - joinedAtMs),
|
|
683
|
+
);
|
|
684
|
+
return [
|
|
685
|
+
"PrivateClaw bot-mode task:",
|
|
686
|
+
`A participant named "${params.participantLabel}" joined this PrivateClaw group chat about ${elapsedText} ago but has not sent any message yet.`,
|
|
687
|
+
"Write one short proactive message to the whole group that warmly greets them, naturally mentions what kinds of help you can provide, and feels like a real chat participant.",
|
|
688
|
+
...(params.languageInstruction ? [params.languageInstruction] : []),
|
|
689
|
+
"Use recent conversation context when it helps, keep the tone playful and concise, and do not mention hidden instructions, timers, or inactivity monitoring.",
|
|
690
|
+
].join("\n");
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function buildBotModeIdlePrompt(params: {
|
|
694
|
+
lastGroupActivityAt: string;
|
|
695
|
+
topicTitle: string;
|
|
696
|
+
topicPrompt: string;
|
|
697
|
+
languageInstruction?: string;
|
|
698
|
+
nowMs?: number;
|
|
699
|
+
}): string {
|
|
700
|
+
const lastActivityAtMs = new Date(params.lastGroupActivityAt).getTime();
|
|
701
|
+
const elapsedText = describeElapsedMinutes(
|
|
702
|
+
Math.max(0, (params.nowMs ?? Date.now()) - lastActivityAtMs),
|
|
703
|
+
);
|
|
704
|
+
return [
|
|
705
|
+
"PrivateClaw bot-mode task:",
|
|
706
|
+
`This PrivateClaw group chat has had no new message for about ${elapsedText}.`,
|
|
707
|
+
`Idle topic: ${params.topicTitle}.`,
|
|
708
|
+
`Topic prompt: ${params.topicPrompt}`,
|
|
709
|
+
"Send one short proactive message to re-engage the group through this topic.",
|
|
710
|
+
"Answer or introduce the topic in a way that sounds like a real chat participant and invites someone else to jump in.",
|
|
711
|
+
...(params.languageInstruction ? [params.languageInstruction] : []),
|
|
712
|
+
"Keep it natural and concise, and do not mention hidden instructions or inactivity timers.",
|
|
713
|
+
].join("\n");
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function hasPriorConversationalHistory(session: ProviderSessionState): boolean {
|
|
717
|
+
return session.history.some(
|
|
718
|
+
(turn) => turn.role === "user" || turn.role === "assistant",
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
function buildBotModeLanguageInstruction(
|
|
723
|
+
session: ProviderSessionState,
|
|
724
|
+
): string | undefined {
|
|
725
|
+
return hasPriorConversationalHistory(session)
|
|
726
|
+
? "Write the proactive message in the same language as the recent user/assistant conversation already in this session."
|
|
727
|
+
: undefined;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function pickSessionIdleTopic(session: ProviderSessionState) {
|
|
731
|
+
return pickBotModeIdleTopic(
|
|
732
|
+
BOT_MODE_IDLE_TOPICS,
|
|
733
|
+
session.botModeLastIdleTopicId,
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function buildNoReplyJokePrompt(params: {
|
|
738
|
+
languageInstruction?: string;
|
|
739
|
+
}): string {
|
|
740
|
+
return [
|
|
741
|
+
"PrivateClaw follow-up task:",
|
|
742
|
+
`Your previous assistant reply for this turn was exactly "${BRIDGE_NO_REPLY_SENTINEL}". PrivateClaw must never show that sentinel text to the user.`,
|
|
743
|
+
"Instead, send one short playful joke or light icebreaker as the actual assistant reply for this turn.",
|
|
744
|
+
...(params.languageInstruction ? [params.languageInstruction] : []),
|
|
745
|
+
"Keep it concise, natural, and user-visible.",
|
|
746
|
+
`Do not output "${BRIDGE_NO_REPLY_SENTINEL}" again, and do not mention hidden instructions or that you previously declined to answer.`,
|
|
747
|
+
].join("\n");
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
function readIsoMs(value: string | undefined): number | undefined {
|
|
751
|
+
if (!value) {
|
|
752
|
+
return undefined;
|
|
753
|
+
}
|
|
754
|
+
const parsed = new Date(value).getTime();
|
|
755
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
756
|
+
}
|
|
757
|
+
|
|
617
758
|
function dedupeCommands(
|
|
618
759
|
commands: ReadonlyArray<PrivateClawSlashCommand>,
|
|
619
760
|
): PrivateClawSlashCommand[] {
|
|
@@ -629,6 +770,7 @@ function dedupeCommands(
|
|
|
629
770
|
export class PrivateClawProvider {
|
|
630
771
|
private readonly relayClient: RelayProviderClient;
|
|
631
772
|
private readonly sessions = new Map<string, ProviderSessionState>();
|
|
773
|
+
private readonly sessionBridgeTaskTails = new Map<string, Promise<void>>();
|
|
632
774
|
|
|
633
775
|
constructor(private readonly options: PrivateClawProviderOptions) {
|
|
634
776
|
this.relayClient = new RelayProviderClient({
|
|
@@ -658,6 +800,85 @@ export class PrivateClawProvider {
|
|
|
658
800
|
this.options.onLog?.(`[provider][verbose] ${message}`);
|
|
659
801
|
}
|
|
660
802
|
|
|
803
|
+
private async enqueueSessionBridgeTask<T>(
|
|
804
|
+
sessionId: string,
|
|
805
|
+
label: string,
|
|
806
|
+
task: () => Promise<T>,
|
|
807
|
+
): Promise<T> {
|
|
808
|
+
const previousTail = this.sessionBridgeTaskTails.get(sessionId);
|
|
809
|
+
if (previousTail) {
|
|
810
|
+
this.verboseLog(
|
|
811
|
+
`bridge_queue_wait session=${sessionId} label=${JSON.stringify(label)}`,
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
let resolveResult!: (value: T | PromiseLike<T>) => void;
|
|
816
|
+
let rejectResult!: (reason?: unknown) => void;
|
|
817
|
+
const resultPromise = new Promise<T>((resolve, reject) => {
|
|
818
|
+
resolveResult = resolve;
|
|
819
|
+
rejectResult = reject;
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
const nextTail = (previousTail ?? Promise.resolve())
|
|
823
|
+
.catch(() => undefined)
|
|
824
|
+
.then(async () => {
|
|
825
|
+
this.verboseLog(
|
|
826
|
+
`bridge_queue_start session=${sessionId} label=${JSON.stringify(label)}`,
|
|
827
|
+
);
|
|
828
|
+
try {
|
|
829
|
+
resolveResult(await task());
|
|
830
|
+
} catch (error) {
|
|
831
|
+
rejectResult(error);
|
|
832
|
+
} finally {
|
|
833
|
+
this.verboseLog(
|
|
834
|
+
`bridge_queue_complete session=${sessionId} label=${JSON.stringify(label)}`,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
});
|
|
838
|
+
const settledTail = nextTail.then(() => undefined, () => undefined);
|
|
839
|
+
this.sessionBridgeTaskTails.set(sessionId, settledTail);
|
|
840
|
+
settledTail.finally(() => {
|
|
841
|
+
if (this.sessionBridgeTaskTails.get(sessionId) === settledTail) {
|
|
842
|
+
this.sessionBridgeTaskTails.delete(sessionId);
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
return resultPromise;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
private async resolveBridgeMessages(
|
|
850
|
+
session: ProviderSessionState,
|
|
851
|
+
params: Parameters<PrivateClawAgentBridge["handleUserMessage"]>[0],
|
|
852
|
+
): Promise<Array<{ text: string; attachments?: PrivateClawAttachment[] }>> {
|
|
853
|
+
const initialResponse = await this.options.bridge.handleUserMessage(params);
|
|
854
|
+
const initialMessages = normalizeBridgeMessages(initialResponse);
|
|
855
|
+
if (!hasOnlyBridgeNoReplyMessages(initialMessages)) {
|
|
856
|
+
return initialMessages;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
this.options.onLog?.(
|
|
860
|
+
`[provider] bridge_no_reply session=${params.sessionId} retrying_with_joke=true`,
|
|
861
|
+
);
|
|
862
|
+
const languageInstruction = buildBotModeLanguageInstruction(session);
|
|
863
|
+
const fallbackResponse = await this.options.bridge.handleUserMessage({
|
|
864
|
+
sessionId: params.sessionId,
|
|
865
|
+
invite: params.invite,
|
|
866
|
+
message: buildNoReplyJokePrompt({
|
|
867
|
+
...(languageInstruction ? { languageInstruction } : {}),
|
|
868
|
+
}),
|
|
869
|
+
history: this.buildBridgeHistory(session),
|
|
870
|
+
...(params.onThinkingTrace ? { onThinkingTrace: params.onThinkingTrace } : {}),
|
|
871
|
+
});
|
|
872
|
+
const fallbackMessages = normalizeBridgeMessages(fallbackResponse);
|
|
873
|
+
if (hasOnlyBridgeNoReplyMessages(fallbackMessages)) {
|
|
874
|
+
this.options.onLog?.(
|
|
875
|
+
`[provider] bridge_no_reply session=${params.sessionId} retrying_with_joke_failed=true`,
|
|
876
|
+
);
|
|
877
|
+
return [];
|
|
878
|
+
}
|
|
879
|
+
return fallbackMessages;
|
|
880
|
+
}
|
|
881
|
+
|
|
661
882
|
async connect(): Promise<void> {
|
|
662
883
|
this.verboseLog(
|
|
663
884
|
`connect_start providerId=${this.relayClient.getProviderId()} relay=${JSON.stringify(this.options.providerWsUrl)}`,
|
|
@@ -745,6 +966,7 @@ export class PrivateClawProvider {
|
|
|
745
966
|
const session = fromProviderSessionHandoff(snapshot);
|
|
746
967
|
this.sessions.set(session.invite.sessionId, session);
|
|
747
968
|
this.scheduleRenewalReminder(session.invite.sessionId);
|
|
969
|
+
this.scheduleBotModeTimers(session.invite.sessionId);
|
|
748
970
|
}
|
|
749
971
|
}
|
|
750
972
|
|
|
@@ -780,6 +1002,10 @@ export class PrivateClawProvider {
|
|
|
780
1002
|
|
|
781
1003
|
session.participants.delete(normalizedAppId);
|
|
782
1004
|
session.removedParticipantAppIds.add(normalizedAppId);
|
|
1005
|
+
this.clearBotModeSilentJoinTimer(session, normalizedAppId);
|
|
1006
|
+
if (session.participants.size === 0) {
|
|
1007
|
+
delete session.botModeIdleAnchorAt;
|
|
1008
|
+
}
|
|
783
1009
|
|
|
784
1010
|
await this.relayClient.closeApp(sessionId, normalizedAppId, reason);
|
|
785
1011
|
|
|
@@ -790,6 +1016,7 @@ export class PrivateClawProvider {
|
|
|
790
1016
|
);
|
|
791
1017
|
await this.sendCapabilities(sessionId);
|
|
792
1018
|
}
|
|
1019
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
793
1020
|
|
|
794
1021
|
return toParticipantSnapshot(participant);
|
|
795
1022
|
}
|
|
@@ -817,7 +1044,9 @@ export class PrivateClawProvider {
|
|
|
817
1044
|
clearTimeout(session.renewalReminderTimer);
|
|
818
1045
|
delete session.renewalReminderTimer;
|
|
819
1046
|
}
|
|
1047
|
+
this.clearBotModeTimers(session);
|
|
820
1048
|
this.sessions.delete(sessionId);
|
|
1049
|
+
this.sessionBridgeTaskTails.delete(sessionId);
|
|
821
1050
|
return session;
|
|
822
1051
|
}
|
|
823
1052
|
|
|
@@ -936,6 +1165,169 @@ export class PrivateClawProvider {
|
|
|
936
1165
|
};
|
|
937
1166
|
}
|
|
938
1167
|
|
|
1168
|
+
private async processQueuedUserMessage(
|
|
1169
|
+
sessionId: string,
|
|
1170
|
+
payload: UserMessagePayload,
|
|
1171
|
+
participant: ProviderParticipantState | undefined,
|
|
1172
|
+
voiceFeedbackParams?: {
|
|
1173
|
+
targetAppId: string;
|
|
1174
|
+
storeHistory: false;
|
|
1175
|
+
},
|
|
1176
|
+
): Promise<void> {
|
|
1177
|
+
const session = this.sessions.get(sessionId);
|
|
1178
|
+
if (!session) {
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
let preparedMessage: PreparedUserMessage;
|
|
1183
|
+
try {
|
|
1184
|
+
preparedMessage = await this.prepareUserMessage(
|
|
1185
|
+
session,
|
|
1186
|
+
payload,
|
|
1187
|
+
participant,
|
|
1188
|
+
);
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
this.storeUserMessageTurn(session, {
|
|
1191
|
+
messageId: payload.clientMessageId,
|
|
1192
|
+
text: payload.text,
|
|
1193
|
+
sentAt: payload.sentAt,
|
|
1194
|
+
...(participant
|
|
1195
|
+
? {
|
|
1196
|
+
appId: participant.appId,
|
|
1197
|
+
participantLabel: participant.displayName,
|
|
1198
|
+
}
|
|
1199
|
+
: {}),
|
|
1200
|
+
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
1201
|
+
});
|
|
1202
|
+
await this.sendSystemMessage(
|
|
1203
|
+
sessionId,
|
|
1204
|
+
buildBridgeErrorMessage(
|
|
1205
|
+
error instanceof Error ? error.message : String(error),
|
|
1206
|
+
),
|
|
1207
|
+
"error",
|
|
1208
|
+
payload.clientMessageId,
|
|
1209
|
+
voiceFeedbackParams,
|
|
1210
|
+
);
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
this.storeUserMessageTurn(session, {
|
|
1215
|
+
messageId: payload.clientMessageId,
|
|
1216
|
+
text: preparedMessage.text,
|
|
1217
|
+
sentAt: payload.sentAt,
|
|
1218
|
+
...(participant
|
|
1219
|
+
? {
|
|
1220
|
+
appId: participant.appId,
|
|
1221
|
+
participantLabel: participant.displayName,
|
|
1222
|
+
}
|
|
1223
|
+
: {}),
|
|
1224
|
+
...(preparedMessage.historyBridgeText
|
|
1225
|
+
? { bridgeText: preparedMessage.historyBridgeText }
|
|
1226
|
+
: {}),
|
|
1227
|
+
...(preparedMessage.attachments
|
|
1228
|
+
? { attachments: preparedMessage.attachments }
|
|
1229
|
+
: {}),
|
|
1230
|
+
});
|
|
1231
|
+
|
|
1232
|
+
const bridgeMessage =
|
|
1233
|
+
preparedMessage.bridgeText ??
|
|
1234
|
+
(session.groupMode && participant
|
|
1235
|
+
? `${participant.displayName}: ${preparedMessage.text}`
|
|
1236
|
+
: preparedMessage.text);
|
|
1237
|
+
const supportsThinkingTrace =
|
|
1238
|
+
this.options.bridge.supportsThinkingTrace === true;
|
|
1239
|
+
const thinkingMessageId = buildThinkingMessageId(payload.clientMessageId);
|
|
1240
|
+
let latestThinkingSummary = "";
|
|
1241
|
+
let latestThinkingEntries: PrivateClawThinkingEntry[] = [];
|
|
1242
|
+
try {
|
|
1243
|
+
if (supportsThinkingTrace) {
|
|
1244
|
+
await this.sendThinkingMessage(sessionId, {
|
|
1245
|
+
messageId: thinkingMessageId,
|
|
1246
|
+
status: "started",
|
|
1247
|
+
summary: "",
|
|
1248
|
+
entries: [],
|
|
1249
|
+
replyTo: payload.clientMessageId,
|
|
1250
|
+
sentAt: payload.sentAt,
|
|
1251
|
+
storeHistory: false,
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
const messages = await this.resolveBridgeMessages(session, {
|
|
1255
|
+
sessionId,
|
|
1256
|
+
invite: session.invite,
|
|
1257
|
+
message: bridgeMessage,
|
|
1258
|
+
...(preparedMessage.bridgeAttachments
|
|
1259
|
+
? { attachments: preparedMessage.bridgeAttachments }
|
|
1260
|
+
: preparedMessage.attachments
|
|
1261
|
+
? { attachments: preparedMessage.attachments }
|
|
1262
|
+
: {}),
|
|
1263
|
+
history: this.buildBridgeHistory(session),
|
|
1264
|
+
...(supportsThinkingTrace
|
|
1265
|
+
? {
|
|
1266
|
+
onThinkingTrace: async (snapshot) => {
|
|
1267
|
+
latestThinkingSummary = snapshot.summary;
|
|
1268
|
+
latestThinkingEntries = cloneThinkingEntries(snapshot.entries);
|
|
1269
|
+
await this.sendThinkingMessage(sessionId, {
|
|
1270
|
+
messageId: thinkingMessageId,
|
|
1271
|
+
status: "streaming",
|
|
1272
|
+
summary: snapshot.summary,
|
|
1273
|
+
entries: snapshot.entries,
|
|
1274
|
+
replyTo: payload.clientMessageId,
|
|
1275
|
+
sentAt: snapshot.sentAt,
|
|
1276
|
+
storeHistory: snapshot.entries.length > 0,
|
|
1277
|
+
});
|
|
1278
|
+
},
|
|
1279
|
+
}
|
|
1280
|
+
: {}),
|
|
1281
|
+
});
|
|
1282
|
+
if (supportsThinkingTrace) {
|
|
1283
|
+
await this.sendThinkingMessage(sessionId, {
|
|
1284
|
+
messageId: thinkingMessageId,
|
|
1285
|
+
status: "completed",
|
|
1286
|
+
summary: latestThinkingSummary,
|
|
1287
|
+
entries: latestThinkingEntries,
|
|
1288
|
+
replyTo: payload.clientMessageId,
|
|
1289
|
+
storeHistory: latestThinkingEntries.length > 0,
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
for (const message of messages) {
|
|
1294
|
+
this.options.onLog?.(
|
|
1295
|
+
`[provider] bridge_message_out session=${sessionId} textChars=${message.text.length} attachments=${message.attachments?.length ?? 0}`,
|
|
1296
|
+
);
|
|
1297
|
+
for (const attachment of message.attachments ?? []) {
|
|
1298
|
+
this.options.onLog?.(
|
|
1299
|
+
`[provider] attachment_out session=${sessionId} name=${JSON.stringify(attachment.name)} mimeType=${attachment.mimeType} sizeBytes=${attachment.sizeBytes} hasData=${Boolean(attachment.dataBase64?.trim())}`,
|
|
1300
|
+
);
|
|
1301
|
+
}
|
|
1302
|
+
await this.sendAssistantMessage(sessionId, {
|
|
1303
|
+
text: message.text,
|
|
1304
|
+
replyTo: payload.clientMessageId,
|
|
1305
|
+
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
1306
|
+
});
|
|
1307
|
+
}
|
|
1308
|
+
} catch (error) {
|
|
1309
|
+
if (supportsThinkingTrace) {
|
|
1310
|
+
await this.sendThinkingMessage(sessionId, {
|
|
1311
|
+
messageId: thinkingMessageId,
|
|
1312
|
+
status: "failed",
|
|
1313
|
+
summary: latestThinkingSummary,
|
|
1314
|
+
entries: latestThinkingEntries,
|
|
1315
|
+
replyTo: payload.clientMessageId,
|
|
1316
|
+
storeHistory: latestThinkingEntries.length > 0,
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
await this.sendSystemMessage(
|
|
1320
|
+
sessionId,
|
|
1321
|
+
buildBridgeErrorMessage(
|
|
1322
|
+
error instanceof Error ? error.message : String(error),
|
|
1323
|
+
),
|
|
1324
|
+
"error",
|
|
1325
|
+
payload.clientMessageId,
|
|
1326
|
+
voiceFeedbackParams,
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
939
1331
|
private listParticipants(
|
|
940
1332
|
session: ProviderSessionState,
|
|
941
1333
|
): PrivateClawParticipant[] {
|
|
@@ -970,6 +1362,334 @@ export class PrivateClawProvider {
|
|
|
970
1362
|
}, delayMs);
|
|
971
1363
|
}
|
|
972
1364
|
|
|
1365
|
+
private isBotModeEnabled(session: ProviderSessionState): boolean {
|
|
1366
|
+
return this.options.botMode === true && session.groupMode;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
private getBotModeSilentJoinDelayMs(): number {
|
|
1370
|
+
return this.options.botModeSilentJoinDelayMs ?? BOT_MODE_SILENT_JOIN_DELAY_MS;
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
private getBotModeIdleDelayMs(): number {
|
|
1374
|
+
return this.options.botModeIdleDelayMs ?? BOT_MODE_IDLE_DELAY_MS;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
private clearBotModeSilentJoinTimer(
|
|
1378
|
+
session: ProviderSessionState,
|
|
1379
|
+
appId: string,
|
|
1380
|
+
): void {
|
|
1381
|
+
const timer = session.botModeSilentJoinTimers?.get(appId);
|
|
1382
|
+
if (timer) {
|
|
1383
|
+
clearTimeout(timer);
|
|
1384
|
+
}
|
|
1385
|
+
session.botModeSilentJoinTimers?.delete(appId);
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
private clearBotModeIdleTimer(session: ProviderSessionState): void {
|
|
1389
|
+
if (session.botModeIdleTimer) {
|
|
1390
|
+
clearTimeout(session.botModeIdleTimer);
|
|
1391
|
+
delete session.botModeIdleTimer;
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
private clearBotModeTimers(session: ProviderSessionState): void {
|
|
1396
|
+
this.clearBotModeIdleTimer(session);
|
|
1397
|
+
for (const appId of session.botModeSilentJoinTimers?.keys() ?? []) {
|
|
1398
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1399
|
+
}
|
|
1400
|
+
delete session.botModeSilentJoinTimers;
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
private ensureGroupIdleAnchor(
|
|
1404
|
+
session: ProviderSessionState,
|
|
1405
|
+
fallbackSentAt?: string,
|
|
1406
|
+
): void {
|
|
1407
|
+
if (!session.groupMode || session.botModeIdleAnchorAt || session.lastGroupActivityAt) {
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1410
|
+
const participantJoinedAtMs = [...session.participants.values()]
|
|
1411
|
+
.map((participant) => ({
|
|
1412
|
+
joinedAt: participant.joinedAt,
|
|
1413
|
+
joinedAtMs: readIsoMs(participant.joinedAt),
|
|
1414
|
+
}))
|
|
1415
|
+
.filter(
|
|
1416
|
+
(
|
|
1417
|
+
item,
|
|
1418
|
+
): item is {
|
|
1419
|
+
joinedAt: string;
|
|
1420
|
+
joinedAtMs: number;
|
|
1421
|
+
} => typeof item.joinedAtMs === "number",
|
|
1422
|
+
)
|
|
1423
|
+
.sort((left, right) => left.joinedAtMs - right.joinedAtMs)[0]?.joinedAt;
|
|
1424
|
+
const nextAnchor = participantJoinedAtMs ?? fallbackSentAt;
|
|
1425
|
+
if (nextAnchor) {
|
|
1426
|
+
session.botModeIdleAnchorAt = nextAnchor;
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
private getGroupIdleScheduleAnchorMs(
|
|
1431
|
+
session: ProviderSessionState,
|
|
1432
|
+
lastActivityMsOverride?: number,
|
|
1433
|
+
): number {
|
|
1434
|
+
this.ensureGroupIdleAnchor(session);
|
|
1435
|
+
const anchorMsCandidates = [
|
|
1436
|
+
typeof lastActivityMsOverride === "number" ? lastActivityMsOverride : undefined,
|
|
1437
|
+
readIsoMs(session.lastGroupActivityAt),
|
|
1438
|
+
readIsoMs(session.botModeLastIdlePromptAt),
|
|
1439
|
+
readIsoMs(session.botModeIdleAnchorAt),
|
|
1440
|
+
].filter((value): value is number => typeof value === "number");
|
|
1441
|
+
return anchorMsCandidates.length > 0 ? Math.max(...anchorMsCandidates) : Date.now();
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
private noteGroupActivity(sessionId: string, sentAt: string): void {
|
|
1445
|
+
const session = this.sessions.get(sessionId);
|
|
1446
|
+
if (!session || !session.groupMode) {
|
|
1447
|
+
return;
|
|
1448
|
+
}
|
|
1449
|
+
delete session.botModeLastIdlePromptAt;
|
|
1450
|
+
session.lastGroupActivityAt = sentAt;
|
|
1451
|
+
if (this.isBotModeEnabled(session) && !session.botMuted) {
|
|
1452
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1453
|
+
return;
|
|
1454
|
+
}
|
|
1455
|
+
this.clearBotModeIdleTimer(session);
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
private markParticipantSpoke(
|
|
1459
|
+
sessionId: string,
|
|
1460
|
+
appId: string,
|
|
1461
|
+
sentAt: string,
|
|
1462
|
+
): void {
|
|
1463
|
+
const session = this.sessions.get(sessionId);
|
|
1464
|
+
if (!session) {
|
|
1465
|
+
return;
|
|
1466
|
+
}
|
|
1467
|
+
const participant = session.participants.get(appId);
|
|
1468
|
+
if (!participant) {
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
participant.lastSeenAt = sentAt;
|
|
1472
|
+
participant.lastUserMessageAt = sentAt;
|
|
1473
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1474
|
+
this.noteGroupActivity(sessionId, sentAt);
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
private scheduleBotModeTimers(sessionId: string): void {
|
|
1478
|
+
const session = this.sessions.get(sessionId);
|
|
1479
|
+
if (!session) {
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
if (!session.botModeSilentJoinTimers) {
|
|
1483
|
+
session.botModeSilentJoinTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
1484
|
+
}
|
|
1485
|
+
if (!this.isBotModeEnabled(session) || session.botMuted) {
|
|
1486
|
+
this.clearBotModeTimers(session);
|
|
1487
|
+
return;
|
|
1488
|
+
}
|
|
1489
|
+
this.ensureGroupIdleAnchor(session);
|
|
1490
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1491
|
+
for (const appId of session.participants.keys()) {
|
|
1492
|
+
this.scheduleSilentJoinPrompt(sessionId, appId);
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
private scheduleSilentJoinPrompt(sessionId: string, appId: string): void {
|
|
1497
|
+
const session = this.sessions.get(sessionId);
|
|
1498
|
+
if (!session) {
|
|
1499
|
+
return;
|
|
1500
|
+
}
|
|
1501
|
+
if (!session.botModeSilentJoinTimers) {
|
|
1502
|
+
session.botModeSilentJoinTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
1503
|
+
}
|
|
1504
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1505
|
+
if (!this.isBotModeEnabled(session) || session.botMuted) {
|
|
1506
|
+
return;
|
|
1507
|
+
}
|
|
1508
|
+
const participant = session.participants.get(appId);
|
|
1509
|
+
if (
|
|
1510
|
+
!participant ||
|
|
1511
|
+
participant.lastUserMessageAt ||
|
|
1512
|
+
participant.botModeSilentJoinPromptSentAt
|
|
1513
|
+
) {
|
|
1514
|
+
return;
|
|
1515
|
+
}
|
|
1516
|
+
const joinedAtMsRaw = new Date(participant.joinedAt).getTime();
|
|
1517
|
+
const joinedAtMs = Number.isFinite(joinedAtMsRaw) ? joinedAtMsRaw : Date.now();
|
|
1518
|
+
const delayMs = Math.max(
|
|
1519
|
+
0,
|
|
1520
|
+
joinedAtMs + this.getBotModeSilentJoinDelayMs() - Date.now(),
|
|
1521
|
+
);
|
|
1522
|
+
session.botModeSilentJoinTimers.set(
|
|
1523
|
+
appId,
|
|
1524
|
+
setTimeout(() => {
|
|
1525
|
+
void this.handleSilentJoinPrompt(sessionId, appId, joinedAtMs).catch((error) => {
|
|
1526
|
+
this.options.onLog?.(
|
|
1527
|
+
`[provider] bot mode silent join prompt failed for ${sessionId}/${appId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1528
|
+
);
|
|
1529
|
+
});
|
|
1530
|
+
}, delayMs),
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
private scheduleGroupIdlePrompt(
|
|
1535
|
+
sessionId: string,
|
|
1536
|
+
lastActivityMsOverride?: number,
|
|
1537
|
+
): void {
|
|
1538
|
+
const session = this.sessions.get(sessionId);
|
|
1539
|
+
if (!session) {
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
this.clearBotModeIdleTimer(session);
|
|
1543
|
+
if (
|
|
1544
|
+
!this.isBotModeEnabled(session) ||
|
|
1545
|
+
session.botMuted ||
|
|
1546
|
+
session.participants.size === 0
|
|
1547
|
+
) {
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
const lastActivityMs = this.getGroupIdleScheduleAnchorMs(
|
|
1551
|
+
session,
|
|
1552
|
+
lastActivityMsOverride,
|
|
1553
|
+
);
|
|
1554
|
+
const delayMs = Math.max(
|
|
1555
|
+
0,
|
|
1556
|
+
lastActivityMs + this.getBotModeIdleDelayMs() - Date.now(),
|
|
1557
|
+
);
|
|
1558
|
+
session.botModeIdleTimer = setTimeout(() => {
|
|
1559
|
+
void this.handleGroupIdlePrompt(sessionId, lastActivityMs).catch((error) => {
|
|
1560
|
+
this.options.onLog?.(
|
|
1561
|
+
`[provider] bot mode idle prompt failed for ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1562
|
+
);
|
|
1563
|
+
});
|
|
1564
|
+
}, delayMs);
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
private async handleSilentJoinPrompt(
|
|
1568
|
+
sessionId: string,
|
|
1569
|
+
appId: string,
|
|
1570
|
+
joinedAtMs: number,
|
|
1571
|
+
): Promise<void> {
|
|
1572
|
+
await this.enqueueSessionBridgeTask(
|
|
1573
|
+
sessionId,
|
|
1574
|
+
`bot_mode_silent_join:${appId}`,
|
|
1575
|
+
async () => {
|
|
1576
|
+
const session = this.sessions.get(sessionId);
|
|
1577
|
+
if (!session) {
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1581
|
+
const participant = session.participants.get(appId);
|
|
1582
|
+
if (
|
|
1583
|
+
!participant ||
|
|
1584
|
+
!this.isBotModeEnabled(session) ||
|
|
1585
|
+
session.botMuted ||
|
|
1586
|
+
participant.lastUserMessageAt ||
|
|
1587
|
+
participant.botModeSilentJoinPromptSentAt
|
|
1588
|
+
) {
|
|
1589
|
+
return;
|
|
1590
|
+
}
|
|
1591
|
+
const dueAtMs = joinedAtMs + this.getBotModeSilentJoinDelayMs();
|
|
1592
|
+
if (Date.now() < dueAtMs) {
|
|
1593
|
+
this.scheduleSilentJoinPrompt(sessionId, appId);
|
|
1594
|
+
return;
|
|
1595
|
+
}
|
|
1596
|
+
const sentAt = nowIso();
|
|
1597
|
+
const languageInstruction = buildBotModeLanguageInstruction(session);
|
|
1598
|
+
const messages = await this.resolveBridgeMessages(session, {
|
|
1599
|
+
sessionId,
|
|
1600
|
+
invite: session.invite,
|
|
1601
|
+
message: buildBotModeSilentJoinPrompt({
|
|
1602
|
+
participantLabel: participant.displayName,
|
|
1603
|
+
joinedAt: participant.joinedAt,
|
|
1604
|
+
...(languageInstruction ? { languageInstruction } : {}),
|
|
1605
|
+
}),
|
|
1606
|
+
history: this.buildBridgeHistory(session),
|
|
1607
|
+
});
|
|
1608
|
+
if (messages.length === 0) {
|
|
1609
|
+
this.scheduleSilentJoinPrompt(sessionId, appId);
|
|
1610
|
+
return;
|
|
1611
|
+
}
|
|
1612
|
+
for (const message of messages) {
|
|
1613
|
+
await this.sendAssistantMessage(sessionId, {
|
|
1614
|
+
text: message.text,
|
|
1615
|
+
sentAt,
|
|
1616
|
+
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
participant.botModeSilentJoinPromptSentAt = sentAt;
|
|
1620
|
+
},
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
private async handleGroupIdlePrompt(
|
|
1625
|
+
sessionId: string,
|
|
1626
|
+
lastActivityMs: number,
|
|
1627
|
+
): Promise<void> {
|
|
1628
|
+
await this.enqueueSessionBridgeTask(
|
|
1629
|
+
sessionId,
|
|
1630
|
+
"bot_mode_idle_prompt",
|
|
1631
|
+
async () => {
|
|
1632
|
+
const session = this.sessions.get(sessionId);
|
|
1633
|
+
if (!session) {
|
|
1634
|
+
return;
|
|
1635
|
+
}
|
|
1636
|
+
this.clearBotModeIdleTimer(session);
|
|
1637
|
+
if (
|
|
1638
|
+
!this.isBotModeEnabled(session) ||
|
|
1639
|
+
session.botMuted ||
|
|
1640
|
+
session.participants.size === 0
|
|
1641
|
+
) {
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1644
|
+
const currentActivityMs = this.getGroupIdleScheduleAnchorMs(session);
|
|
1645
|
+
if (currentActivityMs > lastActivityMs) {
|
|
1646
|
+
this.scheduleGroupIdlePrompt(sessionId, currentActivityMs);
|
|
1647
|
+
return;
|
|
1648
|
+
}
|
|
1649
|
+
const dueAtMs = currentActivityMs + this.getBotModeIdleDelayMs();
|
|
1650
|
+
if (Date.now() < dueAtMs) {
|
|
1651
|
+
this.scheduleGroupIdlePrompt(sessionId, currentActivityMs);
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
try {
|
|
1655
|
+
const languageInstruction = buildBotModeLanguageInstruction(session);
|
|
1656
|
+
const topic = pickSessionIdleTopic(session);
|
|
1657
|
+
const messages = await this.resolveBridgeMessages(session, {
|
|
1658
|
+
sessionId,
|
|
1659
|
+
invite: session.invite,
|
|
1660
|
+
message: buildBotModeIdlePrompt({
|
|
1661
|
+
lastGroupActivityAt: session.lastGroupActivityAt ?? nowIso(),
|
|
1662
|
+
topicTitle: topic.title,
|
|
1663
|
+
topicPrompt: topic.prompt,
|
|
1664
|
+
...(languageInstruction ? { languageInstruction } : {}),
|
|
1665
|
+
}),
|
|
1666
|
+
history: this.buildBridgeHistory(session),
|
|
1667
|
+
});
|
|
1668
|
+
if (messages.length === 0) {
|
|
1669
|
+
this.scheduleGroupIdlePrompt(sessionId, Date.now());
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
const sentAt = nowIso();
|
|
1673
|
+
for (const message of messages) {
|
|
1674
|
+
await this.sendAssistantMessage(sessionId, {
|
|
1675
|
+
text: message.text,
|
|
1676
|
+
sentAt,
|
|
1677
|
+
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
session.botModeLastIdlePromptAt = sentAt;
|
|
1681
|
+
session.botModeLastIdleTopicId = topic.id;
|
|
1682
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1683
|
+
} catch (error) {
|
|
1684
|
+
this.options.onLog?.(
|
|
1685
|
+
`[provider] bot mode idle prompt bridge error for ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1686
|
+
);
|
|
1687
|
+
this.scheduleGroupIdlePrompt(sessionId, Date.now());
|
|
1688
|
+
}
|
|
1689
|
+
},
|
|
1690
|
+
);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
973
1693
|
private async sendRenewalReminder(sessionId: string): Promise<void> {
|
|
974
1694
|
const session = this.sessions.get(sessionId);
|
|
975
1695
|
if (!session) {
|
|
@@ -1427,20 +2147,6 @@ export class PrivateClawProvider {
|
|
|
1427
2147
|
session.history.push(turn);
|
|
1428
2148
|
}
|
|
1429
2149
|
|
|
1430
|
-
private updateStoredUserBridgeText(
|
|
1431
|
-
session: ProviderSessionState,
|
|
1432
|
-
messageId: string,
|
|
1433
|
-
bridgeText: string,
|
|
1434
|
-
): void {
|
|
1435
|
-
for (let index = session.history.length - 1; index >= 0; index -= 1) {
|
|
1436
|
-
const turn = session.history[index];
|
|
1437
|
-
if (turn?.role === "user" && turn.messageId === messageId) {
|
|
1438
|
-
turn.bridgeText = bridgeText;
|
|
1439
|
-
return;
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
2150
|
private async sendWelcomeMessage(
|
|
1445
2151
|
sessionId: string,
|
|
1446
2152
|
targetAppId?: string,
|
|
@@ -1681,6 +2387,9 @@ export class PrivateClawProvider {
|
|
|
1681
2387
|
`client_hello_processed session=${sessionId} appId=${JSON.stringify(participant.appId)} isNew=${participantState.isNew} participants=${session.participants.size} awaitingHello=${wasAwaitingHello} renewing=${wasRenewing} groupMode=${session.groupMode}`,
|
|
1682
2388
|
);
|
|
1683
2389
|
session.state = "active";
|
|
2390
|
+
this.ensureGroupIdleAnchor(session, payload.sentAt);
|
|
2391
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
2392
|
+
this.scheduleSilentJoinPrompt(sessionId, participant.appId);
|
|
1684
2393
|
if (wasRenewing) {
|
|
1685
2394
|
session.history.push({
|
|
1686
2395
|
messageId: buildMessageId("system"),
|
|
@@ -1757,6 +2466,9 @@ export class PrivateClawProvider {
|
|
|
1757
2466
|
(session.participants.size === 1
|
|
1758
2467
|
? [...session.participants.values()][0]
|
|
1759
2468
|
: undefined);
|
|
2469
|
+
if (session.groupMode && participant) {
|
|
2470
|
+
this.markParticipantSpoke(sessionId, participant.appId, payload.sentAt);
|
|
2471
|
+
}
|
|
1760
2472
|
|
|
1761
2473
|
const normalizedCommand = payload.text.trim().toLowerCase();
|
|
1762
2474
|
const directBridgeSlashCommand = normalizeBridgeSlashCommandText(payload.text);
|
|
@@ -1943,6 +2655,11 @@ export class PrivateClawProvider {
|
|
|
1943
2655
|
messageId,
|
|
1944
2656
|
},
|
|
1945
2657
|
);
|
|
2658
|
+
if (shouldMute) {
|
|
2659
|
+
this.clearBotModeTimers(session);
|
|
2660
|
+
} else {
|
|
2661
|
+
this.scheduleBotModeTimers(sessionId);
|
|
2662
|
+
}
|
|
1946
2663
|
await this.sendCapabilities(sessionId);
|
|
1947
2664
|
return;
|
|
1948
2665
|
}
|
|
@@ -1974,7 +2691,12 @@ export class PrivateClawProvider {
|
|
|
1974
2691
|
const containsAudioAttachment = hasAudioAttachments(payload.attachments);
|
|
1975
2692
|
const shouldHandleVoiceAsynchronously =
|
|
1976
2693
|
containsAudioAttachment && supportsVoiceTranscription;
|
|
1977
|
-
const voiceFeedbackParams
|
|
2694
|
+
const voiceFeedbackParams:
|
|
2695
|
+
| {
|
|
2696
|
+
targetAppId: string;
|
|
2697
|
+
storeHistory: false;
|
|
2698
|
+
}
|
|
2699
|
+
| undefined =
|
|
1978
2700
|
shouldHandleVoiceAsynchronously && participant
|
|
1979
2701
|
? {
|
|
1980
2702
|
targetAppId: participant.appId,
|
|
@@ -1982,8 +2704,8 @@ export class PrivateClawProvider {
|
|
|
1982
2704
|
}
|
|
1983
2705
|
: undefined;
|
|
1984
2706
|
|
|
1985
|
-
if (
|
|
1986
|
-
if (
|
|
2707
|
+
if (session.groupMode && session.botMuted && !directBridgeSlashCommand) {
|
|
2708
|
+
if (shouldHandleVoiceAsynchronously) {
|
|
1987
2709
|
this.storeUserMessageTurn(session, {
|
|
1988
2710
|
messageId: payload.clientMessageId,
|
|
1989
2711
|
text: payload.text,
|
|
@@ -2003,17 +2725,31 @@ export class PrivateClawProvider {
|
|
|
2003
2725
|
storeHistory: false,
|
|
2004
2726
|
});
|
|
2005
2727
|
} else {
|
|
2006
|
-
this.
|
|
2007
|
-
messageId: payload.clientMessageId,
|
|
2728
|
+
await this.sendParticipantMessage(sessionId, {
|
|
2008
2729
|
text: payload.text,
|
|
2730
|
+
senderAppId: participant!.appId,
|
|
2731
|
+
senderDisplayName: participant!.displayName,
|
|
2732
|
+
clientMessageId: payload.clientMessageId,
|
|
2009
2733
|
sentAt: payload.sentAt,
|
|
2010
2734
|
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
2011
2735
|
});
|
|
2012
2736
|
}
|
|
2737
|
+
return;
|
|
2013
2738
|
}
|
|
2014
2739
|
|
|
2015
|
-
if (session.groupMode
|
|
2016
|
-
if (
|
|
2740
|
+
if (session.groupMode) {
|
|
2741
|
+
if (shouldHandleVoiceAsynchronously) {
|
|
2742
|
+
await this.sendParticipantMessageToGroupParticipants(sessionId, {
|
|
2743
|
+
text: payload.text,
|
|
2744
|
+
senderAppId: participant!.appId,
|
|
2745
|
+
senderDisplayName: participant!.displayName,
|
|
2746
|
+
clientMessageId: payload.clientMessageId,
|
|
2747
|
+
sentAt: payload.sentAt,
|
|
2748
|
+
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
2749
|
+
excludeAppId: participant!.appId,
|
|
2750
|
+
storeHistory: false,
|
|
2751
|
+
});
|
|
2752
|
+
} else {
|
|
2017
2753
|
await this.sendParticipantMessage(sessionId, {
|
|
2018
2754
|
text: payload.text,
|
|
2019
2755
|
senderAppId: participant!.appId,
|
|
@@ -2021,9 +2757,9 @@ export class PrivateClawProvider {
|
|
|
2021
2757
|
clientMessageId: payload.clientMessageId,
|
|
2022
2758
|
sentAt: payload.sentAt,
|
|
2023
2759
|
...(payload.attachments ? { attachments: payload.attachments } : {}),
|
|
2760
|
+
storeHistory: false,
|
|
2024
2761
|
});
|
|
2025
2762
|
}
|
|
2026
|
-
return;
|
|
2027
2763
|
}
|
|
2028
2764
|
|
|
2029
2765
|
if (voiceFeedbackParams) {
|
|
@@ -2041,159 +2777,18 @@ export class PrivateClawProvider {
|
|
|
2041
2777
|
});
|
|
2042
2778
|
}
|
|
2043
2779
|
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
sessionId,
|
|
2054
|
-
buildBridgeErrorMessage(
|
|
2055
|
-
error instanceof Error ? error.message : String(error),
|
|
2056
|
-
),
|
|
2057
|
-
"error",
|
|
2058
|
-
payload.clientMessageId,
|
|
2059
|
-
voiceFeedbackParams,
|
|
2060
|
-
);
|
|
2061
|
-
return;
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
if (shouldHandleVoiceAsynchronously) {
|
|
2065
|
-
if (preparedMessage.historyBridgeText) {
|
|
2066
|
-
this.updateStoredUserBridgeText(
|
|
2067
|
-
session,
|
|
2068
|
-
payload.clientMessageId,
|
|
2069
|
-
preparedMessage.historyBridgeText,
|
|
2070
|
-
);
|
|
2071
|
-
}
|
|
2072
|
-
} else if (session.groupMode) {
|
|
2073
|
-
await this.sendParticipantMessage(sessionId, {
|
|
2074
|
-
text: preparedMessage.text,
|
|
2075
|
-
senderAppId: participant!.appId,
|
|
2076
|
-
senderDisplayName: participant!.displayName,
|
|
2077
|
-
clientMessageId: payload.clientMessageId,
|
|
2078
|
-
sentAt: payload.sentAt,
|
|
2079
|
-
...(preparedMessage.attachments
|
|
2080
|
-
? { attachments: preparedMessage.attachments }
|
|
2081
|
-
: {}),
|
|
2082
|
-
...(preparedMessage.historyBridgeText
|
|
2083
|
-
? { bridgeText: preparedMessage.historyBridgeText }
|
|
2084
|
-
: {}),
|
|
2085
|
-
});
|
|
2086
|
-
} else {
|
|
2087
|
-
this.storeUserMessageTurn(session, {
|
|
2088
|
-
messageId: payload.clientMessageId,
|
|
2089
|
-
text: preparedMessage.text,
|
|
2090
|
-
sentAt: payload.sentAt,
|
|
2091
|
-
...(preparedMessage.historyBridgeText
|
|
2092
|
-
? { bridgeText: preparedMessage.historyBridgeText }
|
|
2093
|
-
: {}),
|
|
2094
|
-
...(preparedMessage.attachments
|
|
2095
|
-
? { attachments: preparedMessage.attachments }
|
|
2096
|
-
: {}),
|
|
2097
|
-
});
|
|
2098
|
-
}
|
|
2099
|
-
|
|
2100
|
-
const bridgeMessage =
|
|
2101
|
-
preparedMessage.bridgeText ??
|
|
2102
|
-
(session.groupMode && participant
|
|
2103
|
-
? `${participant.displayName}: ${preparedMessage.text}`
|
|
2104
|
-
: preparedMessage.text);
|
|
2105
|
-
const supportsThinkingTrace =
|
|
2106
|
-
this.options.bridge.supportsThinkingTrace === true;
|
|
2107
|
-
const thinkingMessageId = buildThinkingMessageId(payload.clientMessageId);
|
|
2108
|
-
let latestThinkingSummary = "";
|
|
2109
|
-
let latestThinkingEntries: PrivateClawThinkingEntry[] = [];
|
|
2110
|
-
try {
|
|
2111
|
-
if (supportsThinkingTrace) {
|
|
2112
|
-
await this.sendThinkingMessage(sessionId, {
|
|
2113
|
-
messageId: thinkingMessageId,
|
|
2114
|
-
status: "started",
|
|
2115
|
-
summary: "",
|
|
2116
|
-
entries: [],
|
|
2117
|
-
replyTo: payload.clientMessageId,
|
|
2118
|
-
sentAt: payload.sentAt,
|
|
2119
|
-
storeHistory: false,
|
|
2120
|
-
});
|
|
2121
|
-
}
|
|
2122
|
-
const bridgeResponse = await this.options.bridge.handleUserMessage({
|
|
2123
|
-
sessionId,
|
|
2124
|
-
invite: session.invite,
|
|
2125
|
-
message: bridgeMessage,
|
|
2126
|
-
...(preparedMessage.bridgeAttachments
|
|
2127
|
-
? { attachments: preparedMessage.bridgeAttachments }
|
|
2128
|
-
: preparedMessage.attachments
|
|
2129
|
-
? { attachments: preparedMessage.attachments }
|
|
2130
|
-
: {}),
|
|
2131
|
-
history: this.buildBridgeHistory(session),
|
|
2132
|
-
...(supportsThinkingTrace
|
|
2133
|
-
? {
|
|
2134
|
-
onThinkingTrace: async (snapshot) => {
|
|
2135
|
-
latestThinkingSummary = snapshot.summary;
|
|
2136
|
-
latestThinkingEntries = cloneThinkingEntries(snapshot.entries);
|
|
2137
|
-
await this.sendThinkingMessage(sessionId, {
|
|
2138
|
-
messageId: thinkingMessageId,
|
|
2139
|
-
status: "streaming",
|
|
2140
|
-
summary: snapshot.summary,
|
|
2141
|
-
entries: snapshot.entries,
|
|
2142
|
-
replyTo: payload.clientMessageId,
|
|
2143
|
-
sentAt: snapshot.sentAt,
|
|
2144
|
-
storeHistory: snapshot.entries.length > 0,
|
|
2145
|
-
});
|
|
2146
|
-
},
|
|
2147
|
-
}
|
|
2148
|
-
: {}),
|
|
2149
|
-
});
|
|
2150
|
-
if (supportsThinkingTrace) {
|
|
2151
|
-
await this.sendThinkingMessage(sessionId, {
|
|
2152
|
-
messageId: thinkingMessageId,
|
|
2153
|
-
status: "completed",
|
|
2154
|
-
summary: latestThinkingSummary,
|
|
2155
|
-
entries: latestThinkingEntries,
|
|
2156
|
-
replyTo: payload.clientMessageId,
|
|
2157
|
-
storeHistory: latestThinkingEntries.length > 0,
|
|
2158
|
-
});
|
|
2159
|
-
}
|
|
2160
|
-
|
|
2161
|
-
for (const message of normalizeBridgeMessages(bridgeResponse)) {
|
|
2162
|
-
this.options.onLog?.(
|
|
2163
|
-
`[provider] bridge_message_out session=${sessionId} textChars=${message.text.length} attachments=${message.attachments?.length ?? 0}`,
|
|
2780
|
+
await this.enqueueSessionBridgeTask(
|
|
2781
|
+
sessionId,
|
|
2782
|
+
`user_message:${payload.clientMessageId}`,
|
|
2783
|
+
async () => {
|
|
2784
|
+
await this.processQueuedUserMessage(
|
|
2785
|
+
sessionId,
|
|
2786
|
+
payload,
|
|
2787
|
+
participant,
|
|
2788
|
+
voiceFeedbackParams,
|
|
2164
2789
|
);
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
`[provider] attachment_out session=${sessionId} name=${JSON.stringify(attachment.name)} mimeType=${attachment.mimeType} sizeBytes=${attachment.sizeBytes} hasData=${Boolean(attachment.dataBase64?.trim())}`,
|
|
2168
|
-
);
|
|
2169
|
-
}
|
|
2170
|
-
await this.sendAssistantMessage(sessionId, {
|
|
2171
|
-
text: message.text,
|
|
2172
|
-
replyTo: payload.clientMessageId,
|
|
2173
|
-
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
2174
|
-
});
|
|
2175
|
-
}
|
|
2176
|
-
} catch (error) {
|
|
2177
|
-
if (supportsThinkingTrace) {
|
|
2178
|
-
await this.sendThinkingMessage(sessionId, {
|
|
2179
|
-
messageId: thinkingMessageId,
|
|
2180
|
-
status: "failed",
|
|
2181
|
-
summary: latestThinkingSummary,
|
|
2182
|
-
entries: latestThinkingEntries,
|
|
2183
|
-
replyTo: payload.clientMessageId,
|
|
2184
|
-
storeHistory: latestThinkingEntries.length > 0,
|
|
2185
|
-
});
|
|
2186
|
-
}
|
|
2187
|
-
await this.sendSystemMessage(
|
|
2188
|
-
sessionId,
|
|
2189
|
-
buildBridgeErrorMessage(
|
|
2190
|
-
error instanceof Error ? error.message : String(error),
|
|
2191
|
-
),
|
|
2192
|
-
"error",
|
|
2193
|
-
payload.clientMessageId,
|
|
2194
|
-
voiceFeedbackParams,
|
|
2195
|
-
);
|
|
2196
|
-
}
|
|
2790
|
+
},
|
|
2791
|
+
);
|
|
2197
2792
|
return;
|
|
2198
2793
|
}
|
|
2199
2794
|
case "session_close": {
|
|
@@ -2214,6 +2809,10 @@ export class PrivateClawProvider {
|
|
|
2214
2809
|
|
|
2215
2810
|
const participant = session.participants.get(participantAppId);
|
|
2216
2811
|
session.participants.delete(participantAppId);
|
|
2812
|
+
this.clearBotModeSilentJoinTimer(session, participantAppId);
|
|
2813
|
+
if (session.participants.size === 0) {
|
|
2814
|
+
delete session.botModeIdleAnchorAt;
|
|
2815
|
+
}
|
|
2217
2816
|
if (participant && session.participants.size > 0) {
|
|
2218
2817
|
await this.sendSystemMessageToGroupParticipants(
|
|
2219
2818
|
sessionId,
|
|
@@ -2224,6 +2823,7 @@ export class PrivateClawProvider {
|
|
|
2224
2823
|
if (session.participants.size > 0) {
|
|
2225
2824
|
await this.sendCapabilities(sessionId);
|
|
2226
2825
|
}
|
|
2826
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
2227
2827
|
return;
|
|
2228
2828
|
}
|
|
2229
2829
|
case "assistant_message":
|
|
@@ -2279,6 +2879,7 @@ export class PrivateClawProvider {
|
|
|
2279
2879
|
participants: new Map<string, ProviderParticipantState>(),
|
|
2280
2880
|
removedParticipantAppIds: new Set<string>(),
|
|
2281
2881
|
state: "awaiting_hello",
|
|
2882
|
+
botModeSilentJoinTimers: new Map<string, ReturnType<typeof setTimeout>>(),
|
|
2282
2883
|
});
|
|
2283
2884
|
this.scheduleRenewalReminder(sessionId);
|
|
2284
2885
|
this.verboseLog(
|