@privateclaw/privateclaw 0.1.17 → 0.1.23

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/src/provider.ts CHANGED
@@ -9,6 +9,8 @@ import {
9
9
  type PrivateClawInvite,
10
10
  type PrivateClawParticipant,
11
11
  type PrivateClawSlashCommand,
12
+ type PrivateClawThinkingEntry,
13
+ type PrivateClawThinkingStatus,
12
14
  type UserMessagePayload,
13
15
  } from "@privateclaw/protocol";
14
16
  import QRCode from "qrcode";
@@ -109,6 +111,18 @@ function buildPendingAssistantMessageId(replyTo: string): string {
109
111
  return `pending-${replyTo}`;
110
112
  }
111
113
 
114
+ function buildThinkingMessageId(replyTo: string): string {
115
+ return `thinking-${replyTo}`;
116
+ }
117
+
118
+ function cloneThinkingEntry(
119
+ entry: PrivateClawThinkingEntry,
120
+ ): PrivateClawThinkingEntry {
121
+ return {
122
+ ...entry,
123
+ };
124
+ }
125
+
112
126
  function sanitizeParticipantLabel(raw: string | undefined): string | undefined {
113
127
  if (!raw) {
114
128
  return undefined;
@@ -198,6 +212,11 @@ function cloneConversationTurn(
198
212
  })),
199
213
  }
200
214
  : {}),
215
+ ...(turn.thinkingEntries
216
+ ? {
217
+ thinkingEntries: turn.thinkingEntries.map(cloneThinkingEntry),
218
+ }
219
+ : {}),
201
220
  };
202
221
  }
203
222
 
@@ -347,6 +366,17 @@ function formatBridgeHistoryTurn(
347
366
  };
348
367
  }
349
368
 
369
+ function normalizeBridgeSlashCommandText(text: string): string | undefined {
370
+ const trimmed = text.trim();
371
+ return trimmed.startsWith("/") ? trimmed : undefined;
372
+ }
373
+
374
+ function cloneThinkingEntries(
375
+ entries: ReadonlyArray<PrivateClawThinkingEntry>,
376
+ ): PrivateClawThinkingEntry[] {
377
+ return entries.map(cloneThinkingEntry);
378
+ }
379
+
350
380
  function isAudioAttachment(attachment: PrivateClawAttachment): boolean {
351
381
  const normalizedMimeType = attachment.mimeType.trim().toLowerCase();
352
382
  if (normalizedMimeType.startsWith("audio/")) {
@@ -794,7 +824,9 @@ export class PrivateClawProvider {
794
824
  private buildBridgeHistory(
795
825
  session: ProviderSessionState,
796
826
  ): PrivateClawConversationTurn[] {
797
- return session.history.map(formatBridgeHistoryTurn);
827
+ return session.history
828
+ .filter((turn) => turn.role !== "thinking")
829
+ .map(formatBridgeHistoryTurn);
798
830
  }
799
831
 
800
832
  private async prepareUserMessage(
@@ -803,6 +835,7 @@ export class PrivateClawProvider {
803
835
  participant: ProviderParticipantState | undefined,
804
836
  ): Promise<PreparedUserMessage> {
805
837
  const allAttachments = payload.attachments ?? [];
838
+ const directBridgeSlashCommand = normalizeBridgeSlashCommandText(payload.text);
806
839
  const audioAttachments = allAttachments.filter(isAudioAttachment);
807
840
  const providerAudioTranscriber = this.options.audioTranscriber;
808
841
  const bridgeAudioTranscriber = this.options.bridge.transcribeAudioAttachments
@@ -829,6 +862,12 @@ export class PrivateClawProvider {
829
862
  );
830
863
  return {
831
864
  text: payload.text,
865
+ ...(directBridgeSlashCommand
866
+ ? {
867
+ bridgeText: directBridgeSlashCommand,
868
+ historyBridgeText: directBridgeSlashCommand,
869
+ }
870
+ : {}),
832
871
  ...(allAttachments.length > 0 ? { attachments: allAttachments } : {}),
833
872
  ...(allAttachments.length > 0 ? { bridgeAttachments: allAttachments } : {}),
834
873
  };
@@ -966,6 +1005,7 @@ export class PrivateClawProvider {
966
1005
  appId?: string;
967
1006
  deviceLabel?: string;
968
1007
  displayName?: string;
1008
+ supportsThinkingTrace?: boolean;
969
1009
  sentAt: string;
970
1010
  },
971
1011
  ): Promise<{ participant: ProviderParticipantState; isNew: boolean }> {
@@ -982,6 +1022,9 @@ export class PrivateClawProvider {
982
1022
  ...existing,
983
1023
  ...(params.deviceLabel ? { deviceLabel: params.deviceLabel } : {}),
984
1024
  ...(requestedDisplayName ? { displayName: requestedDisplayName } : {}),
1025
+ ...(typeof params.supportsThinkingTrace === "boolean"
1026
+ ? { supportsThinkingTrace: params.supportsThinkingTrace }
1027
+ : {}),
985
1028
  lastSeenAt: params.sentAt,
986
1029
  };
987
1030
  session.participants.set(normalizedAppId, participant);
@@ -1001,6 +1044,9 @@ export class PrivateClawProvider {
1001
1044
  usedLabels,
1002
1045
  ),
1003
1046
  ...(params.deviceLabel ? { deviceLabel: params.deviceLabel } : {}),
1047
+ ...(typeof params.supportsThinkingTrace === "boolean"
1048
+ ? { supportsThinkingTrace: params.supportsThinkingTrace }
1049
+ : {}),
1004
1050
  joinedAt: params.sentAt,
1005
1051
  lastSeenAt: params.sentAt,
1006
1052
  };
@@ -1082,6 +1128,57 @@ export class PrivateClawProvider {
1082
1128
  }, params.targetAppId);
1083
1129
  }
1084
1130
 
1131
+ async sendThinkingMessage(
1132
+ sessionId: string,
1133
+ params: {
1134
+ messageId: string;
1135
+ status: PrivateClawThinkingStatus;
1136
+ summary: string;
1137
+ entries: ReadonlyArray<PrivateClawThinkingEntry>;
1138
+ replyTo?: string;
1139
+ sentAt?: string;
1140
+ targetAppId?: string;
1141
+ storeHistory?: boolean;
1142
+ },
1143
+ ): Promise<void> {
1144
+ const sentAt = params.sentAt ?? nowIso();
1145
+ const session = this.requireSession(sessionId);
1146
+ if (params.storeHistory !== false) {
1147
+ this.upsertThinkingHistoryTurn(session, {
1148
+ messageId: params.messageId,
1149
+ sentAt,
1150
+ status: params.status,
1151
+ summary: params.summary,
1152
+ entries: params.entries,
1153
+ ...(params.replyTo ? { replyTo: params.replyTo } : {}),
1154
+ });
1155
+ }
1156
+ const targetAppIds = params.targetAppId
1157
+ ? (session.participants.get(params.targetAppId)?.supportsThinkingTrace === true
1158
+ ? [params.targetAppId]
1159
+ : [])
1160
+ : [...session.participants.values()]
1161
+ .filter((participant) => participant.supportsThinkingTrace === true)
1162
+ .map((participant) => participant.appId);
1163
+ if (targetAppIds.length === 0) {
1164
+ return;
1165
+ }
1166
+ const payload = {
1167
+ kind: "thinking_message",
1168
+ messageId: params.messageId,
1169
+ status: params.status,
1170
+ summary: params.summary,
1171
+ entries: cloneThinkingEntries(params.entries),
1172
+ sentAt,
1173
+ ...(params.replyTo ? { replyTo: params.replyTo } : {}),
1174
+ } as const;
1175
+ await Promise.all(
1176
+ targetAppIds.map((targetAppId) =>
1177
+ this.sendPayload(sessionId, payload, targetAppId),
1178
+ ),
1179
+ );
1180
+ }
1181
+
1085
1182
  private async sendPendingAssistantStatus(
1086
1183
  sessionId: string,
1087
1184
  params: {
@@ -1299,6 +1396,37 @@ export class PrivateClawProvider {
1299
1396
  });
1300
1397
  }
1301
1398
 
1399
+ private upsertThinkingHistoryTurn(
1400
+ session: ProviderSessionState,
1401
+ params: {
1402
+ messageId: string;
1403
+ replyTo?: string;
1404
+ sentAt: string;
1405
+ status: PrivateClawThinkingStatus;
1406
+ summary: string;
1407
+ entries: ReadonlyArray<PrivateClawThinkingEntry>;
1408
+ },
1409
+ ): void {
1410
+ const turn: PrivateClawConversationTurn = {
1411
+ messageId: params.messageId,
1412
+ role: "thinking",
1413
+ text: params.summary,
1414
+ sentAt: params.sentAt,
1415
+ thinkingStatus: params.status,
1416
+ thinkingSummary: params.summary,
1417
+ thinkingEntries: cloneThinkingEntries(params.entries),
1418
+ ...(params.replyTo ? { replyTo: params.replyTo } : {}),
1419
+ };
1420
+ const existingIndex = session.history.findIndex(
1421
+ (item) => item.messageId === params.messageId,
1422
+ );
1423
+ if (existingIndex >= 0) {
1424
+ session.history[existingIndex] = turn;
1425
+ return;
1426
+ }
1427
+ session.history.push(turn);
1428
+ }
1429
+
1302
1430
  private updateStoredUserBridgeText(
1303
1431
  session: ProviderSessionState,
1304
1432
  messageId: string,
@@ -1432,6 +1560,18 @@ export class PrivateClawProvider {
1432
1560
  },
1433
1561
  );
1434
1562
  break;
1563
+ case "thinking":
1564
+ await this.sendThinkingMessage(sessionId, {
1565
+ messageId: turn.messageId,
1566
+ status: turn.thinkingStatus ?? "completed",
1567
+ summary: turn.thinkingSummary ?? turn.text,
1568
+ entries: turn.thinkingEntries ?? [],
1569
+ sentAt: turn.sentAt,
1570
+ targetAppId,
1571
+ storeHistory: false,
1572
+ ...(turn.replyTo ? { replyTo: turn.replyTo } : {}),
1573
+ });
1574
+ break;
1435
1575
  case "user":
1436
1576
  if (!session.groupMode || !turn.appId || !turn.participantLabel) {
1437
1577
  break;
@@ -1525,6 +1665,7 @@ export class PrivateClawProvider {
1525
1665
  }
1526
1666
  const participantState = await this.upsertParticipant(sessionId, {
1527
1667
  sentAt: payload.sentAt,
1668
+ supportsThinkingTrace: payload.supportsThinkingTrace === true,
1528
1669
  ...(payload.appId ? { appId: normalizedAppId } : {}),
1529
1670
  ...(payload.deviceLabel
1530
1671
  ? { deviceLabel: payload.deviceLabel }
@@ -1618,6 +1759,7 @@ export class PrivateClawProvider {
1618
1759
  : undefined);
1619
1760
 
1620
1761
  const normalizedCommand = payload.text.trim().toLowerCase();
1762
+ const directBridgeSlashCommand = normalizeBridgeSlashCommandText(payload.text);
1621
1763
  const isRenewCommand =
1622
1764
  normalizedCommand === "/renew-session" ||
1623
1765
  normalizedCommand === "/session_renew";
@@ -1870,7 +2012,7 @@ export class PrivateClawProvider {
1870
2012
  }
1871
2013
  }
1872
2014
 
1873
- if (session.groupMode && session.botMuted) {
2015
+ if (session.groupMode && session.botMuted && !directBridgeSlashCommand) {
1874
2016
  if (!shouldHandleVoiceAsynchronously) {
1875
2017
  await this.sendParticipantMessage(sessionId, {
1876
2018
  text: payload.text,
@@ -1955,12 +2097,28 @@ export class PrivateClawProvider {
1955
2097
  });
1956
2098
  }
1957
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[] = [];
1958
2110
  try {
1959
- const bridgeMessage =
1960
- preparedMessage.bridgeText ??
1961
- (session.groupMode && participant
1962
- ? `${participant.displayName}: ${preparedMessage.text}`
1963
- : preparedMessage.text);
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
+ }
1964
2122
  const bridgeResponse = await this.options.bridge.handleUserMessage({
1965
2123
  sessionId,
1966
2124
  invite: session.invite,
@@ -1969,9 +2127,36 @@ export class PrivateClawProvider {
1969
2127
  ? { attachments: preparedMessage.bridgeAttachments }
1970
2128
  : preparedMessage.attachments
1971
2129
  ? { attachments: preparedMessage.attachments }
1972
- : {}),
2130
+ : {}),
1973
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
+ : {}),
1974
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
+ }
1975
2160
 
1976
2161
  for (const message of normalizeBridgeMessages(bridgeResponse)) {
1977
2162
  this.options.onLog?.(
@@ -1989,6 +2174,16 @@ export class PrivateClawProvider {
1989
2174
  });
1990
2175
  }
1991
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
+ }
1992
2187
  await this.sendSystemMessage(
1993
2188
  sessionId,
1994
2189
  buildBridgeErrorMessage(
package/src/types.ts CHANGED
@@ -3,11 +3,13 @@ import type {
3
3
  PrivateClawInvite,
4
4
  PrivateClawParticipant,
5
5
  PrivateClawSlashCommand,
6
+ PrivateClawThinkingEntry,
7
+ PrivateClawThinkingStatus,
6
8
  } from "@privateclaw/protocol";
7
9
 
8
10
  export interface PrivateClawConversationTurn {
9
11
  messageId: string;
10
- role: "user" | "assistant" | "system";
12
+ role: "user" | "assistant" | "system" | "thinking";
11
13
  text: string;
12
14
  sentAt: string;
13
15
  bridgeText?: string;
@@ -16,6 +18,9 @@ export interface PrivateClawConversationTurn {
16
18
  replyTo?: string;
17
19
  severity?: "info" | "error";
18
20
  attachments?: PrivateClawAttachment[];
21
+ thinkingStatus?: PrivateClawThinkingStatus;
22
+ thinkingSummary?: string;
23
+ thinkingEntries?: PrivateClawThinkingEntry[];
19
24
  }
20
25
 
21
26
  export type BridgeMessage =
@@ -48,13 +53,23 @@ export interface PrivateClawVerboseController {
48
53
  enabled: boolean;
49
54
  }
50
55
 
56
+ export interface PrivateClawThinkingTraceSnapshot {
57
+ entries: ReadonlyArray<PrivateClawThinkingEntry>;
58
+ sentAt: string;
59
+ summary: string;
60
+ }
61
+
51
62
  export interface PrivateClawAgentBridge {
63
+ supportsThinkingTrace?: boolean;
52
64
  handleUserMessage(params: {
53
65
  sessionId: string;
54
66
  invite: PrivateClawInvite;
55
67
  message: string;
56
68
  attachments?: ReadonlyArray<PrivateClawAttachment>;
57
69
  history: ReadonlyArray<PrivateClawConversationTurn>;
70
+ onThinkingTrace?: (
71
+ snapshot: PrivateClawThinkingTraceSnapshot,
72
+ ) => void | Promise<void>;
58
73
  }): Promise<BridgeResponse>;
59
74
  transcribeAudioAttachments?(
60
75
  params: PrivateClawAudioTranscriptionRequest,
@@ -77,6 +92,7 @@ export interface PrivateClawProviderOptions {
77
92
 
78
93
  export interface ProviderParticipantState extends PrivateClawParticipant {
79
94
  lastSeenAt: string;
95
+ supportsThinkingTrace?: boolean;
80
96
  }
81
97
 
82
98
  export interface PrivateClawManagedSession {