@privateclaw/privateclaw 0.1.24 → 0.1.25
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 +13 -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 +15 -0
- package/dist/provider.js +327 -0
- package/dist/provider.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/openclaw.plugin.json +15 -1
- package/package.json +1 -1
- 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 +427 -1
- package/src/provider.ts +419 -0
- package/src/types.ts +13 -0
package/src/provider.ts
CHANGED
|
@@ -47,6 +47,8 @@ import type {
|
|
|
47
47
|
export const DEFAULT_SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
|
48
48
|
const RENEW_SESSION_TTL_MS = DEFAULT_SESSION_TTL_MS;
|
|
49
49
|
const SESSION_RENEWAL_REMINDER_WINDOW_MS = 30 * 60 * 1000;
|
|
50
|
+
const BOT_MODE_SILENT_JOIN_DELAY_MS = 10 * 60 * 1000;
|
|
51
|
+
const BOT_MODE_IDLE_DELAY_MS = 20 * 60 * 1000;
|
|
50
52
|
const PARTICIPANT_LABEL_MAX_CHARS = 12;
|
|
51
53
|
const PARTICIPANT_FALLBACK_PREFIXES = [
|
|
52
54
|
"流萤",
|
|
@@ -286,6 +288,15 @@ function toProviderSessionHandoff(
|
|
|
286
288
|
...(session.renewalReminderSentAt
|
|
287
289
|
? { renewalReminderSentAt: session.renewalReminderSentAt }
|
|
288
290
|
: {}),
|
|
291
|
+
...(session.lastGroupActivityAt
|
|
292
|
+
? { lastGroupActivityAt: session.lastGroupActivityAt }
|
|
293
|
+
: {}),
|
|
294
|
+
...(session.botModeIdleAnchorAt
|
|
295
|
+
? { botModeIdleAnchorAt: session.botModeIdleAnchorAt }
|
|
296
|
+
: {}),
|
|
297
|
+
...(session.botModeLastIdlePromptAt
|
|
298
|
+
? { botModeLastIdlePromptAt: session.botModeLastIdlePromptAt }
|
|
299
|
+
: {}),
|
|
289
300
|
};
|
|
290
301
|
}
|
|
291
302
|
|
|
@@ -309,9 +320,24 @@ function fromProviderSessionHandoff(
|
|
|
309
320
|
...(snapshot.renewalReminderSentAt
|
|
310
321
|
? { renewalReminderSentAt: snapshot.renewalReminderSentAt }
|
|
311
322
|
: {}),
|
|
323
|
+
...(snapshot.lastGroupActivityAt
|
|
324
|
+
? { lastGroupActivityAt: snapshot.lastGroupActivityAt }
|
|
325
|
+
: {}),
|
|
326
|
+
...(snapshot.botModeIdleAnchorAt
|
|
327
|
+
? { botModeIdleAnchorAt: snapshot.botModeIdleAnchorAt }
|
|
328
|
+
: {}),
|
|
329
|
+
...(snapshot.botModeLastIdlePromptAt
|
|
330
|
+
? { botModeLastIdlePromptAt: snapshot.botModeLastIdlePromptAt }
|
|
331
|
+
: {}),
|
|
332
|
+
botModeSilentJoinTimers: new Map<string, ReturnType<typeof setTimeout>>(),
|
|
312
333
|
};
|
|
313
334
|
}
|
|
314
335
|
|
|
336
|
+
function describeElapsedMinutes(elapsedMs: number): string {
|
|
337
|
+
const minutes = Math.max(1, Math.round(elapsedMs / 60_000));
|
|
338
|
+
return `${minutes} minute${minutes === 1 ? "" : "s"}`;
|
|
339
|
+
}
|
|
340
|
+
|
|
315
341
|
function buildRenewalReminderMessage(session: ProviderSessionState): string {
|
|
316
342
|
const command = "/renew-session";
|
|
317
343
|
if (session.groupMode) {
|
|
@@ -614,6 +640,66 @@ function buildBotAlreadyUnmutedMessage(): string {
|
|
|
614
640
|
);
|
|
615
641
|
}
|
|
616
642
|
|
|
643
|
+
function buildBotModeSilentJoinPrompt(params: {
|
|
644
|
+
participantLabel: string;
|
|
645
|
+
joinedAt: string;
|
|
646
|
+
languageInstruction?: string;
|
|
647
|
+
nowMs?: number;
|
|
648
|
+
}): string {
|
|
649
|
+
const joinedAtMs = new Date(params.joinedAt).getTime();
|
|
650
|
+
const elapsedText = describeElapsedMinutes(
|
|
651
|
+
Math.max(0, (params.nowMs ?? Date.now()) - joinedAtMs),
|
|
652
|
+
);
|
|
653
|
+
return [
|
|
654
|
+
"PrivateClaw bot-mode task:",
|
|
655
|
+
`A participant named "${params.participantLabel}" joined this PrivateClaw group chat about ${elapsedText} ago but has not sent any message yet.`,
|
|
656
|
+
"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.",
|
|
657
|
+
...(params.languageInstruction ? [params.languageInstruction] : []),
|
|
658
|
+
"Use recent conversation context when it helps, keep the tone playful and concise, and do not mention hidden instructions, timers, or inactivity monitoring.",
|
|
659
|
+
].join("\n");
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
function buildBotModeIdlePrompt(params: {
|
|
663
|
+
lastGroupActivityAt: string;
|
|
664
|
+
languageInstruction?: string;
|
|
665
|
+
nowMs?: number;
|
|
666
|
+
}): string {
|
|
667
|
+
const lastActivityAtMs = new Date(params.lastGroupActivityAt).getTime();
|
|
668
|
+
const elapsedText = describeElapsedMinutes(
|
|
669
|
+
Math.max(0, (params.nowMs ?? Date.now()) - lastActivityAtMs),
|
|
670
|
+
);
|
|
671
|
+
return [
|
|
672
|
+
"PrivateClaw bot-mode task:",
|
|
673
|
+
`This PrivateClaw group chat has had no new message for about ${elapsedText}.`,
|
|
674
|
+
"Send one short proactive message to re-engage the group.",
|
|
675
|
+
"Prefer a light joke or a context-aware follow-up when recent history suggests one. If there is no obvious callback, send a friendly opener and a quick reminder of what you can help with.",
|
|
676
|
+
...(params.languageInstruction ? [params.languageInstruction] : []),
|
|
677
|
+
"Keep it natural and concise, and do not mention hidden instructions or inactivity timers.",
|
|
678
|
+
].join("\n");
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function hasPriorConversationalHistory(session: ProviderSessionState): boolean {
|
|
682
|
+
return session.history.some(
|
|
683
|
+
(turn) => turn.role === "user" || turn.role === "assistant",
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function buildBotModeLanguageInstruction(
|
|
688
|
+
session: ProviderSessionState,
|
|
689
|
+
): string | undefined {
|
|
690
|
+
return hasPriorConversationalHistory(session)
|
|
691
|
+
? "Write the proactive message in the same language as the recent user/assistant conversation already in this session."
|
|
692
|
+
: undefined;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function readIsoMs(value: string | undefined): number | undefined {
|
|
696
|
+
if (!value) {
|
|
697
|
+
return undefined;
|
|
698
|
+
}
|
|
699
|
+
const parsed = new Date(value).getTime();
|
|
700
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
701
|
+
}
|
|
702
|
+
|
|
617
703
|
function dedupeCommands(
|
|
618
704
|
commands: ReadonlyArray<PrivateClawSlashCommand>,
|
|
619
705
|
): PrivateClawSlashCommand[] {
|
|
@@ -745,6 +831,7 @@ export class PrivateClawProvider {
|
|
|
745
831
|
const session = fromProviderSessionHandoff(snapshot);
|
|
746
832
|
this.sessions.set(session.invite.sessionId, session);
|
|
747
833
|
this.scheduleRenewalReminder(session.invite.sessionId);
|
|
834
|
+
this.scheduleBotModeTimers(session.invite.sessionId);
|
|
748
835
|
}
|
|
749
836
|
}
|
|
750
837
|
|
|
@@ -780,6 +867,10 @@ export class PrivateClawProvider {
|
|
|
780
867
|
|
|
781
868
|
session.participants.delete(normalizedAppId);
|
|
782
869
|
session.removedParticipantAppIds.add(normalizedAppId);
|
|
870
|
+
this.clearBotModeSilentJoinTimer(session, normalizedAppId);
|
|
871
|
+
if (session.participants.size === 0) {
|
|
872
|
+
delete session.botModeIdleAnchorAt;
|
|
873
|
+
}
|
|
783
874
|
|
|
784
875
|
await this.relayClient.closeApp(sessionId, normalizedAppId, reason);
|
|
785
876
|
|
|
@@ -790,6 +881,7 @@ export class PrivateClawProvider {
|
|
|
790
881
|
);
|
|
791
882
|
await this.sendCapabilities(sessionId);
|
|
792
883
|
}
|
|
884
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
793
885
|
|
|
794
886
|
return toParticipantSnapshot(participant);
|
|
795
887
|
}
|
|
@@ -817,6 +909,7 @@ export class PrivateClawProvider {
|
|
|
817
909
|
clearTimeout(session.renewalReminderTimer);
|
|
818
910
|
delete session.renewalReminderTimer;
|
|
819
911
|
}
|
|
912
|
+
this.clearBotModeTimers(session);
|
|
820
913
|
this.sessions.delete(sessionId);
|
|
821
914
|
return session;
|
|
822
915
|
}
|
|
@@ -970,6 +1063,315 @@ export class PrivateClawProvider {
|
|
|
970
1063
|
}, delayMs);
|
|
971
1064
|
}
|
|
972
1065
|
|
|
1066
|
+
private isBotModeEnabled(session: ProviderSessionState): boolean {
|
|
1067
|
+
return this.options.botMode === true && session.groupMode;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
private getBotModeSilentJoinDelayMs(): number {
|
|
1071
|
+
return this.options.botModeSilentJoinDelayMs ?? BOT_MODE_SILENT_JOIN_DELAY_MS;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
private getBotModeIdleDelayMs(): number {
|
|
1075
|
+
return this.options.botModeIdleDelayMs ?? BOT_MODE_IDLE_DELAY_MS;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
private clearBotModeSilentJoinTimer(
|
|
1079
|
+
session: ProviderSessionState,
|
|
1080
|
+
appId: string,
|
|
1081
|
+
): void {
|
|
1082
|
+
const timer = session.botModeSilentJoinTimers?.get(appId);
|
|
1083
|
+
if (timer) {
|
|
1084
|
+
clearTimeout(timer);
|
|
1085
|
+
}
|
|
1086
|
+
session.botModeSilentJoinTimers?.delete(appId);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
private clearBotModeIdleTimer(session: ProviderSessionState): void {
|
|
1090
|
+
if (session.botModeIdleTimer) {
|
|
1091
|
+
clearTimeout(session.botModeIdleTimer);
|
|
1092
|
+
delete session.botModeIdleTimer;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
private clearBotModeTimers(session: ProviderSessionState): void {
|
|
1097
|
+
this.clearBotModeIdleTimer(session);
|
|
1098
|
+
for (const appId of session.botModeSilentJoinTimers?.keys() ?? []) {
|
|
1099
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1100
|
+
}
|
|
1101
|
+
delete session.botModeSilentJoinTimers;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
private ensureGroupIdleAnchor(
|
|
1105
|
+
session: ProviderSessionState,
|
|
1106
|
+
fallbackSentAt?: string,
|
|
1107
|
+
): void {
|
|
1108
|
+
if (!session.groupMode || session.botModeIdleAnchorAt || session.lastGroupActivityAt) {
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
const participantJoinedAtMs = [...session.participants.values()]
|
|
1112
|
+
.map((participant) => ({
|
|
1113
|
+
joinedAt: participant.joinedAt,
|
|
1114
|
+
joinedAtMs: readIsoMs(participant.joinedAt),
|
|
1115
|
+
}))
|
|
1116
|
+
.filter(
|
|
1117
|
+
(
|
|
1118
|
+
item,
|
|
1119
|
+
): item is {
|
|
1120
|
+
joinedAt: string;
|
|
1121
|
+
joinedAtMs: number;
|
|
1122
|
+
} => typeof item.joinedAtMs === "number",
|
|
1123
|
+
)
|
|
1124
|
+
.sort((left, right) => left.joinedAtMs - right.joinedAtMs)[0]?.joinedAt;
|
|
1125
|
+
const nextAnchor = participantJoinedAtMs ?? fallbackSentAt;
|
|
1126
|
+
if (nextAnchor) {
|
|
1127
|
+
session.botModeIdleAnchorAt = nextAnchor;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
private getGroupIdleScheduleAnchorMs(
|
|
1132
|
+
session: ProviderSessionState,
|
|
1133
|
+
lastActivityMsOverride?: number,
|
|
1134
|
+
): number {
|
|
1135
|
+
this.ensureGroupIdleAnchor(session);
|
|
1136
|
+
const anchorMsCandidates = [
|
|
1137
|
+
typeof lastActivityMsOverride === "number" ? lastActivityMsOverride : undefined,
|
|
1138
|
+
readIsoMs(session.lastGroupActivityAt),
|
|
1139
|
+
readIsoMs(session.botModeLastIdlePromptAt),
|
|
1140
|
+
readIsoMs(session.botModeIdleAnchorAt),
|
|
1141
|
+
].filter((value): value is number => typeof value === "number");
|
|
1142
|
+
return anchorMsCandidates.length > 0 ? Math.max(...anchorMsCandidates) : Date.now();
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
private noteGroupActivity(sessionId: string, sentAt: string): void {
|
|
1146
|
+
const session = this.sessions.get(sessionId);
|
|
1147
|
+
if (!session || !session.groupMode) {
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
delete session.botModeLastIdlePromptAt;
|
|
1151
|
+
session.lastGroupActivityAt = sentAt;
|
|
1152
|
+
if (this.isBotModeEnabled(session) && !session.botMuted) {
|
|
1153
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
this.clearBotModeIdleTimer(session);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
private markParticipantSpoke(
|
|
1160
|
+
sessionId: string,
|
|
1161
|
+
appId: string,
|
|
1162
|
+
sentAt: string,
|
|
1163
|
+
): void {
|
|
1164
|
+
const session = this.sessions.get(sessionId);
|
|
1165
|
+
if (!session) {
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
const participant = session.participants.get(appId);
|
|
1169
|
+
if (!participant) {
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
participant.lastSeenAt = sentAt;
|
|
1173
|
+
participant.lastUserMessageAt = sentAt;
|
|
1174
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1175
|
+
this.noteGroupActivity(sessionId, sentAt);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
private scheduleBotModeTimers(sessionId: string): void {
|
|
1179
|
+
const session = this.sessions.get(sessionId);
|
|
1180
|
+
if (!session) {
|
|
1181
|
+
return;
|
|
1182
|
+
}
|
|
1183
|
+
if (!session.botModeSilentJoinTimers) {
|
|
1184
|
+
session.botModeSilentJoinTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
1185
|
+
}
|
|
1186
|
+
if (!this.isBotModeEnabled(session) || session.botMuted) {
|
|
1187
|
+
this.clearBotModeTimers(session);
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
this.ensureGroupIdleAnchor(session);
|
|
1191
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1192
|
+
for (const appId of session.participants.keys()) {
|
|
1193
|
+
this.scheduleSilentJoinPrompt(sessionId, appId);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
private scheduleSilentJoinPrompt(sessionId: string, appId: string): void {
|
|
1198
|
+
const session = this.sessions.get(sessionId);
|
|
1199
|
+
if (!session) {
|
|
1200
|
+
return;
|
|
1201
|
+
}
|
|
1202
|
+
if (!session.botModeSilentJoinTimers) {
|
|
1203
|
+
session.botModeSilentJoinTimers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
1204
|
+
}
|
|
1205
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1206
|
+
if (!this.isBotModeEnabled(session) || session.botMuted) {
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
const participant = session.participants.get(appId);
|
|
1210
|
+
if (
|
|
1211
|
+
!participant ||
|
|
1212
|
+
participant.lastUserMessageAt ||
|
|
1213
|
+
participant.botModeSilentJoinPromptSentAt
|
|
1214
|
+
) {
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
const joinedAtMsRaw = new Date(participant.joinedAt).getTime();
|
|
1218
|
+
const joinedAtMs = Number.isFinite(joinedAtMsRaw) ? joinedAtMsRaw : Date.now();
|
|
1219
|
+
const delayMs = Math.max(
|
|
1220
|
+
0,
|
|
1221
|
+
joinedAtMs + this.getBotModeSilentJoinDelayMs() - Date.now(),
|
|
1222
|
+
);
|
|
1223
|
+
session.botModeSilentJoinTimers.set(
|
|
1224
|
+
appId,
|
|
1225
|
+
setTimeout(() => {
|
|
1226
|
+
void this.handleSilentJoinPrompt(sessionId, appId, joinedAtMs).catch((error) => {
|
|
1227
|
+
this.options.onLog?.(
|
|
1228
|
+
`[provider] bot mode silent join prompt failed for ${sessionId}/${appId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1229
|
+
);
|
|
1230
|
+
});
|
|
1231
|
+
}, delayMs),
|
|
1232
|
+
);
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
private scheduleGroupIdlePrompt(
|
|
1236
|
+
sessionId: string,
|
|
1237
|
+
lastActivityMsOverride?: number,
|
|
1238
|
+
): void {
|
|
1239
|
+
const session = this.sessions.get(sessionId);
|
|
1240
|
+
if (!session) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
this.clearBotModeIdleTimer(session);
|
|
1244
|
+
if (
|
|
1245
|
+
!this.isBotModeEnabled(session) ||
|
|
1246
|
+
session.botMuted ||
|
|
1247
|
+
session.participants.size === 0
|
|
1248
|
+
) {
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
const lastActivityMs = this.getGroupIdleScheduleAnchorMs(
|
|
1252
|
+
session,
|
|
1253
|
+
lastActivityMsOverride,
|
|
1254
|
+
);
|
|
1255
|
+
const delayMs = Math.max(
|
|
1256
|
+
0,
|
|
1257
|
+
lastActivityMs + this.getBotModeIdleDelayMs() - Date.now(),
|
|
1258
|
+
);
|
|
1259
|
+
session.botModeIdleTimer = setTimeout(() => {
|
|
1260
|
+
void this.handleGroupIdlePrompt(sessionId, lastActivityMs).catch((error) => {
|
|
1261
|
+
this.options.onLog?.(
|
|
1262
|
+
`[provider] bot mode idle prompt failed for ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1263
|
+
);
|
|
1264
|
+
});
|
|
1265
|
+
}, delayMs);
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
private async handleSilentJoinPrompt(
|
|
1269
|
+
sessionId: string,
|
|
1270
|
+
appId: string,
|
|
1271
|
+
joinedAtMs: number,
|
|
1272
|
+
): Promise<void> {
|
|
1273
|
+
const session = this.sessions.get(sessionId);
|
|
1274
|
+
if (!session) {
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
this.clearBotModeSilentJoinTimer(session, appId);
|
|
1278
|
+
const participant = session.participants.get(appId);
|
|
1279
|
+
if (
|
|
1280
|
+
!participant ||
|
|
1281
|
+
!this.isBotModeEnabled(session) ||
|
|
1282
|
+
session.botMuted ||
|
|
1283
|
+
participant.lastUserMessageAt ||
|
|
1284
|
+
participant.botModeSilentJoinPromptSentAt
|
|
1285
|
+
) {
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
const dueAtMs = joinedAtMs + this.getBotModeSilentJoinDelayMs();
|
|
1289
|
+
if (Date.now() < dueAtMs) {
|
|
1290
|
+
this.scheduleSilentJoinPrompt(sessionId, appId);
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
const sentAt = nowIso();
|
|
1294
|
+
const languageInstruction = buildBotModeLanguageInstruction(session);
|
|
1295
|
+
const bridgeResponse = await this.options.bridge.handleUserMessage({
|
|
1296
|
+
sessionId,
|
|
1297
|
+
invite: session.invite,
|
|
1298
|
+
message: buildBotModeSilentJoinPrompt({
|
|
1299
|
+
participantLabel: participant.displayName,
|
|
1300
|
+
joinedAt: participant.joinedAt,
|
|
1301
|
+
...(languageInstruction ? { languageInstruction } : {}),
|
|
1302
|
+
}),
|
|
1303
|
+
history: this.buildBridgeHistory(session),
|
|
1304
|
+
});
|
|
1305
|
+
for (const message of normalizeBridgeMessages(bridgeResponse)) {
|
|
1306
|
+
await this.sendAssistantMessage(sessionId, {
|
|
1307
|
+
text: message.text,
|
|
1308
|
+
sentAt,
|
|
1309
|
+
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
participant.botModeSilentJoinPromptSentAt = sentAt;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
private async handleGroupIdlePrompt(
|
|
1316
|
+
sessionId: string,
|
|
1317
|
+
lastActivityMs: number,
|
|
1318
|
+
): Promise<void> {
|
|
1319
|
+
const session = this.sessions.get(sessionId);
|
|
1320
|
+
if (!session) {
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
this.clearBotModeIdleTimer(session);
|
|
1324
|
+
if (
|
|
1325
|
+
!this.isBotModeEnabled(session) ||
|
|
1326
|
+
session.botMuted ||
|
|
1327
|
+
session.participants.size === 0
|
|
1328
|
+
) {
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const currentActivityMs = this.getGroupIdleScheduleAnchorMs(session);
|
|
1332
|
+
if (currentActivityMs > lastActivityMs) {
|
|
1333
|
+
this.scheduleGroupIdlePrompt(sessionId, currentActivityMs);
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
const dueAtMs = currentActivityMs + this.getBotModeIdleDelayMs();
|
|
1337
|
+
if (Date.now() < dueAtMs) {
|
|
1338
|
+
this.scheduleGroupIdlePrompt(sessionId, currentActivityMs);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
try {
|
|
1342
|
+
const languageInstruction = buildBotModeLanguageInstruction(session);
|
|
1343
|
+
const bridgeResponse = await this.options.bridge.handleUserMessage({
|
|
1344
|
+
sessionId,
|
|
1345
|
+
invite: session.invite,
|
|
1346
|
+
message: buildBotModeIdlePrompt({
|
|
1347
|
+
lastGroupActivityAt: session.lastGroupActivityAt ?? nowIso(),
|
|
1348
|
+
...(languageInstruction ? { languageInstruction } : {}),
|
|
1349
|
+
}),
|
|
1350
|
+
history: this.buildBridgeHistory(session),
|
|
1351
|
+
});
|
|
1352
|
+
const messages = normalizeBridgeMessages(bridgeResponse);
|
|
1353
|
+
if (messages.length === 0) {
|
|
1354
|
+
this.scheduleGroupIdlePrompt(sessionId, Date.now());
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1357
|
+
const sentAt = nowIso();
|
|
1358
|
+
for (const message of messages) {
|
|
1359
|
+
await this.sendAssistantMessage(sessionId, {
|
|
1360
|
+
text: message.text,
|
|
1361
|
+
sentAt,
|
|
1362
|
+
...(message.attachments ? { attachments: message.attachments } : {}),
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
session.botModeLastIdlePromptAt = sentAt;
|
|
1366
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
1367
|
+
} catch (error) {
|
|
1368
|
+
this.options.onLog?.(
|
|
1369
|
+
`[provider] bot mode idle prompt bridge error for ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1370
|
+
);
|
|
1371
|
+
this.scheduleGroupIdlePrompt(sessionId, Date.now());
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
|
|
973
1375
|
private async sendRenewalReminder(sessionId: string): Promise<void> {
|
|
974
1376
|
const session = this.sessions.get(sessionId);
|
|
975
1377
|
if (!session) {
|
|
@@ -1681,6 +2083,9 @@ export class PrivateClawProvider {
|
|
|
1681
2083
|
`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
2084
|
);
|
|
1683
2085
|
session.state = "active";
|
|
2086
|
+
this.ensureGroupIdleAnchor(session, payload.sentAt);
|
|
2087
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
2088
|
+
this.scheduleSilentJoinPrompt(sessionId, participant.appId);
|
|
1684
2089
|
if (wasRenewing) {
|
|
1685
2090
|
session.history.push({
|
|
1686
2091
|
messageId: buildMessageId("system"),
|
|
@@ -1757,6 +2162,9 @@ export class PrivateClawProvider {
|
|
|
1757
2162
|
(session.participants.size === 1
|
|
1758
2163
|
? [...session.participants.values()][0]
|
|
1759
2164
|
: undefined);
|
|
2165
|
+
if (session.groupMode && participant) {
|
|
2166
|
+
this.markParticipantSpoke(sessionId, participant.appId, payload.sentAt);
|
|
2167
|
+
}
|
|
1760
2168
|
|
|
1761
2169
|
const normalizedCommand = payload.text.trim().toLowerCase();
|
|
1762
2170
|
const directBridgeSlashCommand = normalizeBridgeSlashCommandText(payload.text);
|
|
@@ -1943,6 +2351,11 @@ export class PrivateClawProvider {
|
|
|
1943
2351
|
messageId,
|
|
1944
2352
|
},
|
|
1945
2353
|
);
|
|
2354
|
+
if (shouldMute) {
|
|
2355
|
+
this.clearBotModeTimers(session);
|
|
2356
|
+
} else {
|
|
2357
|
+
this.scheduleBotModeTimers(sessionId);
|
|
2358
|
+
}
|
|
1946
2359
|
await this.sendCapabilities(sessionId);
|
|
1947
2360
|
return;
|
|
1948
2361
|
}
|
|
@@ -2214,6 +2627,10 @@ export class PrivateClawProvider {
|
|
|
2214
2627
|
|
|
2215
2628
|
const participant = session.participants.get(participantAppId);
|
|
2216
2629
|
session.participants.delete(participantAppId);
|
|
2630
|
+
this.clearBotModeSilentJoinTimer(session, participantAppId);
|
|
2631
|
+
if (session.participants.size === 0) {
|
|
2632
|
+
delete session.botModeIdleAnchorAt;
|
|
2633
|
+
}
|
|
2217
2634
|
if (participant && session.participants.size > 0) {
|
|
2218
2635
|
await this.sendSystemMessageToGroupParticipants(
|
|
2219
2636
|
sessionId,
|
|
@@ -2224,6 +2641,7 @@ export class PrivateClawProvider {
|
|
|
2224
2641
|
if (session.participants.size > 0) {
|
|
2225
2642
|
await this.sendCapabilities(sessionId);
|
|
2226
2643
|
}
|
|
2644
|
+
this.scheduleGroupIdlePrompt(sessionId);
|
|
2227
2645
|
return;
|
|
2228
2646
|
}
|
|
2229
2647
|
case "assistant_message":
|
|
@@ -2279,6 +2697,7 @@ export class PrivateClawProvider {
|
|
|
2279
2697
|
participants: new Map<string, ProviderParticipantState>(),
|
|
2280
2698
|
removedParticipantAppIds: new Set<string>(),
|
|
2281
2699
|
state: "awaiting_hello",
|
|
2700
|
+
botModeSilentJoinTimers: new Map<string, ReturnType<typeof setTimeout>>(),
|
|
2282
2701
|
});
|
|
2283
2702
|
this.scheduleRenewalReminder(sessionId);
|
|
2284
2703
|
this.verboseLog(
|
package/src/types.ts
CHANGED
|
@@ -85,6 +85,9 @@ export interface PrivateClawProviderOptions {
|
|
|
85
85
|
defaultTtlMs?: number;
|
|
86
86
|
providerLabel?: string;
|
|
87
87
|
welcomeMessage?: string;
|
|
88
|
+
botMode?: boolean;
|
|
89
|
+
botModeSilentJoinDelayMs?: number;
|
|
90
|
+
botModeIdleDelayMs?: number;
|
|
88
91
|
commandsProvider?: () => Promise<PrivateClawSlashCommand[]>;
|
|
89
92
|
verboseController?: PrivateClawVerboseController;
|
|
90
93
|
onLog?: (message: string) => void;
|
|
@@ -92,6 +95,8 @@ export interface PrivateClawProviderOptions {
|
|
|
92
95
|
|
|
93
96
|
export interface ProviderParticipantState extends PrivateClawParticipant {
|
|
94
97
|
lastSeenAt: string;
|
|
98
|
+
lastUserMessageAt?: string;
|
|
99
|
+
botModeSilentJoinPromptSentAt?: string;
|
|
95
100
|
supportsThinkingTrace?: boolean;
|
|
96
101
|
}
|
|
97
102
|
|
|
@@ -124,12 +129,17 @@ export interface ProviderSessionState {
|
|
|
124
129
|
participants: Map<string, ProviderParticipantState>;
|
|
125
130
|
removedParticipantAppIds: Set<string>;
|
|
126
131
|
state: "awaiting_hello" | "active";
|
|
132
|
+
lastGroupActivityAt?: string;
|
|
133
|
+
botModeIdleAnchorAt?: string;
|
|
134
|
+
botModeLastIdlePromptAt?: string;
|
|
127
135
|
pendingRenewal?: {
|
|
128
136
|
expiresAt: string;
|
|
129
137
|
sentAt: string;
|
|
130
138
|
};
|
|
131
139
|
renewalReminderSentAt?: string;
|
|
132
140
|
renewalReminderTimer?: ReturnType<typeof setTimeout>;
|
|
141
|
+
botModeIdleTimer?: ReturnType<typeof setTimeout>;
|
|
142
|
+
botModeSilentJoinTimers?: Map<string, ReturnType<typeof setTimeout>>;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
export interface PrivateClawProviderSessionHandoff {
|
|
@@ -146,6 +156,9 @@ export interface PrivateClawProviderSessionHandoff {
|
|
|
146
156
|
sentAt: string;
|
|
147
157
|
};
|
|
148
158
|
renewalReminderSentAt?: string;
|
|
159
|
+
lastGroupActivityAt?: string;
|
|
160
|
+
botModeIdleAnchorAt?: string;
|
|
161
|
+
botModeLastIdlePromptAt?: string;
|
|
149
162
|
}
|
|
150
163
|
|
|
151
164
|
export interface PrivateClawProviderHandoffState {
|