@privateclaw/privateclaw 0.1.32 → 0.1.34

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.
Files changed (40) hide show
  1. package/README.md +16 -9
  2. package/dist/bridges/openclaw-agent-bridge.d.ts +7 -0
  3. package/dist/bridges/openclaw-agent-bridge.js +71 -25
  4. package/dist/bridges/openclaw-agent-bridge.js.map +1 -1
  5. package/dist/channel.d.ts +14 -0
  6. package/dist/channel.js +103 -0
  7. package/dist/channel.js.map +1 -0
  8. package/dist/cli.js +16 -2
  9. package/dist/cli.js.map +1 -1
  10. package/dist/compat/openclaw.d.ts +231 -2
  11. package/dist/compat/openclaw.js +1 -0
  12. package/dist/compat/openclaw.js.map +1 -1
  13. package/dist/openclaw-plugin.js +199 -4
  14. package/dist/openclaw-plugin.js.map +1 -1
  15. package/dist/pair-session.d.ts +32 -0
  16. package/dist/pair-session.js +115 -41
  17. package/dist/pair-session.js.map +1 -1
  18. package/dist/provider.d.ts +4 -1
  19. package/dist/provider.js +109 -4
  20. package/dist/provider.js.map +1 -1
  21. package/dist/session-control.d.ts +15 -1
  22. package/dist/session-control.js +184 -5
  23. package/dist/session-control.js.map +1 -1
  24. package/dist/types.d.ts +20 -1
  25. package/openclaw.plugin.json +3 -2
  26. package/package.json +2 -2
  27. package/src/bridges/openclaw-agent-bridge.test.ts +154 -0
  28. package/src/bridges/openclaw-agent-bridge.ts +97 -27
  29. package/src/channel.ts +130 -0
  30. package/src/cli.ts +17 -1
  31. package/src/compat/openclaw.ts +282 -2
  32. package/src/openclaw-plugin.test.ts +392 -3
  33. package/src/openclaw-plugin.ts +262 -3
  34. package/src/pair-session.test.ts +119 -0
  35. package/src/pair-session.ts +144 -47
  36. package/src/provider.test.ts +266 -0
  37. package/src/provider.ts +133 -4
  38. package/src/session-control.test.ts +87 -0
  39. package/src/session-control.ts +252 -5
  40. package/src/types.ts +23 -0
@@ -288,11 +288,16 @@ function extractDisplayResult(
288
288
 
289
289
  const messages =
290
290
  payloads.flatMap((payload) =>
291
- [payload.text, payload.message, payload.summary].flatMap((candidate) =>
292
- typeof candidate === "string" && stripStructuredResponseBlock(candidate) !== ""
293
- ? [{ text: stripStructuredResponseBlock(candidate) }]
294
- : [],
295
- ),
291
+ [payload.text, payload.message, payload.summary].flatMap((candidate) => {
292
+ if (typeof candidate !== "string") {
293
+ return [];
294
+ }
295
+ const normalized = normalizePlainBridgeTextMessage(
296
+ candidate,
297
+ options?.workspaceDir ? { workspaceDir: options.workspaceDir } : undefined,
298
+ );
299
+ return normalized ? [normalized] : [];
300
+ }),
296
301
  ) ?? [];
297
302
 
298
303
  if (messages.length === 0) {
@@ -1314,9 +1319,12 @@ function extractAssistantDisplayResult(
1314
1319
  return { result: structuredResult, isStructured: true };
1315
1320
  }
1316
1321
 
1317
- const fallbackText = stripStructuredResponseBlock(rawText);
1318
- return fallbackText !== ""
1319
- ? { result: { messages: [{ text: fallbackText }] }, isStructured: false }
1322
+ const fallbackMessage = normalizePlainBridgeTextMessage(
1323
+ rawText,
1324
+ options?.workspaceDir ? { workspaceDir: options.workspaceDir } : undefined,
1325
+ );
1326
+ return fallbackMessage
1327
+ ? { result: { messages: [fallbackMessage] }, isStructured: false }
1320
1328
  : { result: { messages: [] }, isStructured: false };
1321
1329
  }
1322
1330
 
@@ -1357,7 +1365,7 @@ function buildStructuredResponseContractPrompt(): string {
1357
1365
  "- To send images/files, add an attachments array to the message: {\"text\":\"...\",\"attachments\":[{\"name\":\"file.png\",\"mimeType\":\"image/png\",\"filePath\":\"relative/path/in/current/workspace/or/absolute/local/path\"}]}",
1358
1366
  "- Or use dataBase64 for inline binary: {\"name\":\"file.png\",\"mimeType\":\"image/png\",\"dataBase64\":\"...\"}",
1359
1367
  "- Never put attachments at the top level of the response object. Files must live inside messages[].attachments.",
1360
- "- Never put channel-specific markup such as <qqimg>...</qqimg>, markdown image syntax, or raw local file paths inside messages[].text. Put files only in messages[].attachments.",
1368
+ "- Never put channel-specific markup such as <qqimg>...</qqimg>, <qqvoice>...</qqvoice>, <qqvideo>...</qqvideo>, or <qqfile>...</qqfile>, markdown image syntax, or raw local file paths inside messages[].text. Put files only in messages[].attachments.",
1361
1369
  "- Use data for optional machine-readable extraction results that PrivateClaw may consume in future file-processing flows.",
1362
1370
  "- Do not wrap the JSON in markdown fences.",
1363
1371
  ].join("\n");
@@ -1437,14 +1445,14 @@ function parseStructuredResponseResult(
1437
1445
  const messages: NormalizedBridgeMessage[] = [];
1438
1446
  for (const message of structured.messages ?? []) {
1439
1447
  const text = typeof message.text === "string" ? message.text.trim() : "";
1440
- const qqimgResult = extractInlineQqimgAttachments(text, options);
1448
+ const qqMediaResult = extractInlineQqMediaAttachments(text, options);
1441
1449
  const attachments = [
1442
1450
  ...resolveStructuredAttachments(message.attachments, options),
1443
- ...qqimgResult.attachments,
1451
+ ...qqMediaResult.attachments,
1444
1452
  ];
1445
- if (qqimgResult.text !== "" || attachments.length > 0) {
1453
+ if (qqMediaResult.text !== "" || attachments.length > 0) {
1446
1454
  messages.push({
1447
- text: qqimgResult.text,
1455
+ text: qqMediaResult.text,
1448
1456
  ...(attachments.length > 0 ? { attachments } : {}),
1449
1457
  });
1450
1458
  }
@@ -1516,7 +1524,31 @@ function extractStructuredResponsePayload(raw: string): string | undefined {
1516
1524
  return undefined;
1517
1525
  }
1518
1526
 
1519
- function extractInlineQqimgAttachments(
1527
+ function normalizePlainBridgeTextMessage(
1528
+ rawText: string,
1529
+ options?: {
1530
+ workspaceDir?: string;
1531
+ },
1532
+ ): NormalizedBridgeMessage | undefined {
1533
+ const strippedText = stripStructuredResponseBlock(rawText);
1534
+ if (strippedText === "") {
1535
+ return undefined;
1536
+ }
1537
+
1538
+ const mediaResult = extractInlineQqMediaAttachments(strippedText, options);
1539
+ if (mediaResult.text === "" && mediaResult.attachments.length === 0) {
1540
+ return undefined;
1541
+ }
1542
+
1543
+ return {
1544
+ text: mediaResult.text,
1545
+ ...(mediaResult.attachments.length > 0
1546
+ ? { attachments: mediaResult.attachments }
1547
+ : {}),
1548
+ };
1549
+ }
1550
+
1551
+ export function extractInlineQqMediaAttachments(
1520
1552
  text: string,
1521
1553
  options?: {
1522
1554
  workspaceDir?: string;
@@ -1525,17 +1557,28 @@ function extractInlineQqimgAttachments(
1525
1557
  text: string;
1526
1558
  attachments: PrivateClawAttachment[];
1527
1559
  } {
1528
- if (!text.includes("<qqimg>")) {
1560
+ if (!text.includes("<qq")) {
1529
1561
  return { text, attachments: [] };
1530
1562
  }
1531
1563
 
1532
1564
  const attachments: PrivateClawAttachment[] = [];
1533
- for (const match of text.matchAll(/<qqimg>([\s\S]*?)<\/qqimg>/giu)) {
1534
- const rawValue = match[1]?.trim();
1535
- if (!rawValue) {
1565
+ const mediaTagPattern = /<(qqimg|qqvoice|qqvideo|qqfile)>([\s\S]*?)<\/(?:qqimg|qqvoice|qqvideo|qqfile|img)>/giu;
1566
+ for (const match of text.matchAll(mediaTagPattern)) {
1567
+ const rawTagName = match[1]?.trim().toLowerCase();
1568
+ const rawValue = match[2]?.trim();
1569
+ if (
1570
+ !rawTagName ||
1571
+ !isInlineQqMediaTag(rawTagName) ||
1572
+ !rawValue ||
1573
+ rawValue === ""
1574
+ ) {
1536
1575
  continue;
1537
1576
  }
1538
- const attachment = resolveInlineQqimgAttachment(rawValue, options);
1577
+ const attachment = resolveInlineQqMediaAttachment(
1578
+ rawValue,
1579
+ rawTagName,
1580
+ options,
1581
+ );
1539
1582
  if (attachment) {
1540
1583
  attachments.push(attachment);
1541
1584
  }
@@ -1543,15 +1586,40 @@ function extractInlineQqimgAttachments(
1543
1586
 
1544
1587
  return {
1545
1588
  text: text
1546
- .replace(/<qqimg>[\s\S]*?<\/qqimg>/giu, "")
1589
+ .replace(mediaTagPattern, "")
1547
1590
  .replace(/\n{3,}/gu, "\n\n")
1548
1591
  .trim(),
1549
1592
  attachments,
1550
1593
  };
1551
1594
  }
1552
1595
 
1553
- function resolveInlineQqimgAttachment(
1596
+ function isInlineQqMediaTag(value: string): value is "qqimg" | "qqvoice" | "qqvideo" | "qqfile" {
1597
+ return value === "qqimg" || value === "qqvoice" || value === "qqvideo" || value === "qqfile";
1598
+ }
1599
+
1600
+ function inferInlineQqMediaMimeType(
1601
+ fileName: string,
1602
+ tagName: "qqimg" | "qqvoice" | "qqvideo" | "qqfile",
1603
+ ): string {
1604
+ const inferred = inferMimeTypeFromFileName(fileName);
1605
+ if (inferred) {
1606
+ return inferred;
1607
+ }
1608
+ switch (tagName) {
1609
+ case "qqimg":
1610
+ return "image/*";
1611
+ case "qqvoice":
1612
+ return "audio/*";
1613
+ case "qqvideo":
1614
+ return "video/*";
1615
+ case "qqfile":
1616
+ return GENERIC_BINARY_MIME;
1617
+ }
1618
+ }
1619
+
1620
+ function resolveInlineQqMediaAttachment(
1554
1621
  rawValue: string,
1622
+ tagName: "qqimg" | "qqvoice" | "qqvideo" | "qqfile",
1555
1623
  options?: {
1556
1624
  workspaceDir?: string;
1557
1625
  },
@@ -1564,10 +1632,11 @@ function resolveInlineQqimgAttachment(
1564
1632
  if (parsedUrl.protocol === "file:") {
1565
1633
  const filePath = fileURLToPath(parsedUrl);
1566
1634
  const fileBytes = readFileSync(filePath);
1635
+ const fileName = path.basename(filePath) || "attachment";
1567
1636
  return {
1568
1637
  id: `structured-attachment-${randomUUID()}`,
1569
- name: path.basename(filePath) || "attachment",
1570
- mimeType: inferMimeTypeFromFileName(filePath) ?? GENERIC_BINARY_MIME,
1638
+ name: fileName,
1639
+ mimeType: inferInlineQqMediaMimeType(fileName, tagName),
1571
1640
  sizeBytes: fileBytes.byteLength,
1572
1641
  dataBase64: fileBytes.toString("base64"),
1573
1642
  };
@@ -1576,7 +1645,7 @@ function resolveInlineQqimgAttachment(
1576
1645
  return {
1577
1646
  id: `structured-attachment-${randomUUID()}`,
1578
1647
  name: fileName,
1579
- mimeType: inferMimeTypeFromFileName(fileName) ?? GENERIC_BINARY_MIME,
1648
+ mimeType: inferInlineQqMediaMimeType(fileName, tagName),
1580
1649
  sizeBytes: 0,
1581
1650
  uri: parsedUrl.toString(),
1582
1651
  };
@@ -1591,10 +1660,11 @@ function resolveInlineQqimgAttachment(
1591
1660
  return undefined;
1592
1661
  }
1593
1662
  const fileBytes = readFileSync(filePath);
1663
+ const fileName = path.basename(filePath) || "attachment";
1594
1664
  return {
1595
1665
  id: `structured-attachment-${randomUUID()}`,
1596
- name: path.basename(filePath) || "attachment",
1597
- mimeType: inferMimeTypeFromFileName(filePath) ?? GENERIC_BINARY_MIME,
1666
+ name: fileName,
1667
+ mimeType: inferInlineQqMediaMimeType(fileName, tagName),
1598
1668
  sizeBytes: fileBytes.byteLength,
1599
1669
  dataBase64: fileBytes.toString("base64"),
1600
1670
  };
@@ -1865,7 +1935,7 @@ function resolveAttachmentMimeType(attachment: PrivateClawAttachment): string {
1865
1935
  return inferMimeTypeFromFileName(attachment.name) ?? (explicitMimeType || GENERIC_BINARY_MIME);
1866
1936
  }
1867
1937
 
1868
- function inferMimeTypeFromFileName(fileName: string): string | undefined {
1938
+ export function inferMimeTypeFromFileName(fileName: string): string | undefined {
1869
1939
  const extension = getFileExtension(fileName);
1870
1940
  switch (extension) {
1871
1941
  case "png":
package/src/channel.ts ADDED
@@ -0,0 +1,130 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import type {
3
+ OpenClawChannelPluginCompat,
4
+ OpenClawOutboundDeliveryResultCompat,
5
+ ReplyPayloadCompat,
6
+ } from "./compat/openclaw.js";
7
+ import type { PrivateClawOpenClawOutboundMessage } from "./types.js";
8
+
9
+ export const PRIVATECLAW_OPENCLAW_CHANNEL_ID = "privateclaw";
10
+ export const PRIVATECLAW_OPENCLAW_ACCOUNT_ID = "default";
11
+
12
+ interface PrivateClawVirtualAccount {
13
+ accountId: string;
14
+ name: string;
15
+ enabled: true;
16
+ configured: true;
17
+ }
18
+
19
+ function resolvePrivateClawAccountId(accountId?: string | null): string {
20
+ const normalized = accountId?.trim();
21
+ return normalized ? normalized : PRIVATECLAW_OPENCLAW_ACCOUNT_ID;
22
+ }
23
+
24
+ function toOutboundMessage(
25
+ payload: ReplyPayloadCompat,
26
+ ): PrivateClawOpenClawOutboundMessage {
27
+ return {
28
+ ...(payload.text?.trim() ? { text: payload.text.trim() } : {}),
29
+ ...(payload.mediaUrl?.trim() ? { mediaUrl: payload.mediaUrl.trim() } : {}),
30
+ ...(payload.mediaUrls?.length ? { mediaUrls: payload.mediaUrls } : {}),
31
+ ...(typeof payload.replyToId === "string" || payload.replyToId === null
32
+ ? { replyToId: payload.replyToId }
33
+ : {}),
34
+ };
35
+ }
36
+
37
+ function buildDeliveryResult(
38
+ sessionId: string,
39
+ ): OpenClawOutboundDeliveryResultCompat {
40
+ return {
41
+ channel: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
42
+ messageId: `privateclaw-outbound-${randomUUID()}`,
43
+ chatId: sessionId,
44
+ timestamp: Date.now(),
45
+ };
46
+ }
47
+
48
+ export function createPrivateClawChannelPlugin(params: {
49
+ deliverOutboundMessage(
50
+ sessionId: string,
51
+ payload: PrivateClawOpenClawOutboundMessage,
52
+ ): Promise<void>;
53
+ }): OpenClawChannelPluginCompat<PrivateClawVirtualAccount> {
54
+ return {
55
+ id: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
56
+ meta: {
57
+ id: PRIVATECLAW_OPENCLAW_CHANNEL_ID,
58
+ label: "PrivateClaw",
59
+ selectionLabel: "PrivateClaw",
60
+ docsPath: "https://privateclaw.us",
61
+ docsLabel: "PrivateClaw docs",
62
+ blurb:
63
+ "Routes paired PrivateClaw app conversations back through the encrypted relay session.",
64
+ showConfigured: false,
65
+ quickstartAllowFrom: false,
66
+ preferSessionLookupForAnnounceTarget: true,
67
+ },
68
+ capabilities: {
69
+ chatTypes: ["direct", "group"],
70
+ reply: true,
71
+ threads: false,
72
+ media: true,
73
+ nativeCommands: false,
74
+ },
75
+ config: {
76
+ listAccountIds: () => [PRIVATECLAW_OPENCLAW_ACCOUNT_ID],
77
+ resolveAccount: (_cfg, accountId) => ({
78
+ accountId: resolvePrivateClawAccountId(accountId),
79
+ name: "PrivateClaw virtual account",
80
+ enabled: true,
81
+ configured: true,
82
+ }),
83
+ defaultAccountId: () => PRIVATECLAW_OPENCLAW_ACCOUNT_ID,
84
+ isEnabled: () => true,
85
+ isConfigured: () => true,
86
+ describeAccount: (account) => ({
87
+ accountId: account.accountId,
88
+ name: account.name,
89
+ enabled: true,
90
+ configured: true,
91
+ linked: true,
92
+ running: true,
93
+ connected: true,
94
+ }),
95
+ },
96
+ commands: {
97
+ skipWhenConfigEmpty: false,
98
+ },
99
+ threading: {
100
+ resolveReplyToMode: () => "all",
101
+ },
102
+ outbound: {
103
+ deliveryMode: "direct",
104
+ sendPayload: async (ctx) => {
105
+ await params.deliverOutboundMessage(ctx.to, toOutboundMessage(ctx.payload));
106
+ return buildDeliveryResult(ctx.to);
107
+ },
108
+ sendText: async (ctx) => {
109
+ await params.deliverOutboundMessage(ctx.to, {
110
+ ...(ctx.text.trim() ? { text: ctx.text } : {}),
111
+ ...(ctx.mediaUrl?.trim() ? { mediaUrl: ctx.mediaUrl.trim() } : {}),
112
+ ...(typeof ctx.replyToId === "string" || ctx.replyToId === null
113
+ ? { replyToId: ctx.replyToId }
114
+ : {}),
115
+ });
116
+ return buildDeliveryResult(ctx.to);
117
+ },
118
+ sendMedia: async (ctx) => {
119
+ await params.deliverOutboundMessage(ctx.to, {
120
+ ...(ctx.text.trim() ? { text: ctx.text } : {}),
121
+ ...(ctx.mediaUrl?.trim() ? { mediaUrl: ctx.mediaUrl.trim() } : {}),
122
+ ...(typeof ctx.replyToId === "string" || ctx.replyToId === null
123
+ ? { replyToId: ctx.replyToId }
124
+ : {}),
125
+ });
126
+ return buildDeliveryResult(ctx.to);
127
+ },
128
+ },
129
+ };
130
+ }
package/src/cli.ts CHANGED
@@ -29,6 +29,7 @@ import { DEFAULT_RELAY_BASE_URL } from "./relay-defaults.js";
29
29
  import { resolveRelayEndpoints } from "./relay-endpoints.js";
30
30
  import {
31
31
  buildManagedSessionQrLegacyLines,
32
+ dispatchRoutedAppMessageToPluginServiceFromStateDir,
32
33
  followManagedSessionLogFromStateDir,
33
34
  buildManagedSessionsReportLines,
34
35
  closeManagedSessionsFromStateDir,
@@ -151,6 +152,7 @@ function createProvider(params?: {
151
152
  provider: PrivateClawProvider;
152
153
  stateDir: string;
153
154
  } {
155
+ const stateDir = resolvePrivateClawStateDir();
154
156
  const providerWsUrl = process.env.PRIVATECLAW_PROVIDER_WS_URL?.trim();
155
157
  const appWsUrl = process.env.PRIVATECLAW_APP_WS_URL?.trim();
156
158
  const relayBaseUrl =
@@ -279,12 +281,26 @@ function createProvider(params?: {
279
281
  return [];
280
282
  }
281
283
  },
284
+ appMessageRouter: async (message) => {
285
+ try {
286
+ await dispatchRoutedAppMessageToPluginServiceFromStateDir({
287
+ stateDir,
288
+ message,
289
+ });
290
+ return true;
291
+ } catch (error) {
292
+ console.log(
293
+ `[privateclaw-provider] [privateclaw] runtime routing unavailable, falling back to bridge for ${message.sessionId}: ${error instanceof Error ? error.message : String(error)}`,
294
+ );
295
+ return false;
296
+ }
297
+ },
282
298
  ...(welcomeMessage ? { welcomeMessage } : {}),
283
299
  onLog: (message) => {
284
300
  console.log(`[privateclaw-provider] ${message}`);
285
301
  },
286
302
  }),
287
- stateDir: resolvePrivateClawStateDir(),
303
+ stateDir,
288
304
  };
289
305
  }
290
306