@privateclaw/privateclaw 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +100 -0
  2. package/dist/bridges/echo-bridge.d.ts +10 -0
  3. package/dist/bridges/echo-bridge.js +16 -0
  4. package/dist/bridges/echo-bridge.js.map +1 -0
  5. package/dist/bridges/openai-compatible-bridge.d.ts +15 -0
  6. package/dist/bridges/openai-compatible-bridge.js +61 -0
  7. package/dist/bridges/openai-compatible-bridge.js.map +1 -0
  8. package/dist/bridges/openclaw-agent-bridge.d.ts +35 -0
  9. package/dist/bridges/openclaw-agent-bridge.js +363 -0
  10. package/dist/bridges/openclaw-agent-bridge.js.map +1 -0
  11. package/dist/bridges/webhook-bridge.d.ts +21 -0
  12. package/dist/bridges/webhook-bridge.js +28 -0
  13. package/dist/bridges/webhook-bridge.js.map +1 -0
  14. package/dist/cli.d.ts +2 -0
  15. package/dist/cli.js +96 -0
  16. package/dist/cli.js.map +1 -0
  17. package/dist/compat/openclaw.d.ts +134 -0
  18. package/dist/compat/openclaw.js +67 -0
  19. package/dist/compat/openclaw.js.map +1 -0
  20. package/dist/index.d.ts +11 -0
  21. package/dist/index.js +12 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/openclaw-command-discovery.d.ts +2 -0
  24. package/dist/openclaw-command-discovery.js +215 -0
  25. package/dist/openclaw-command-discovery.js.map +1 -0
  26. package/dist/openclaw-plugin.d.ts +30 -0
  27. package/dist/openclaw-plugin.js +294 -0
  28. package/dist/openclaw-plugin.js.map +1 -0
  29. package/dist/provider.d.ts +31 -0
  30. package/dist/provider.js +314 -0
  31. package/dist/provider.js.map +1 -0
  32. package/dist/relay-endpoints.d.ts +6 -0
  33. package/dist/relay-endpoints.js +20 -0
  34. package/dist/relay-endpoints.js.map +1 -0
  35. package/dist/relay-provider-client.d.ts +41 -0
  36. package/dist/relay-provider-client.js +272 -0
  37. package/dist/relay-provider-client.js.map +1 -0
  38. package/dist/types.d.ts +49 -0
  39. package/dist/types.js +2 -0
  40. package/dist/types.js.map +1 -0
  41. package/index.ts +2 -0
  42. package/openclaw.plugin.json +72 -0
  43. package/package.json +66 -0
  44. package/src/bridges/echo-bridge.ts +22 -0
  45. package/src/bridges/openai-compatible-bridge.ts +105 -0
  46. package/src/bridges/openclaw-agent-bridge.test.ts +254 -0
  47. package/src/bridges/openclaw-agent-bridge.ts +490 -0
  48. package/src/bridges/webhook-bridge.ts +45 -0
  49. package/src/cli.ts +115 -0
  50. package/src/compat/openclaw.ts +139 -0
  51. package/src/index.ts +11 -0
  52. package/src/openclaw-command-discovery.ts +276 -0
  53. package/src/openclaw-plugin.test.ts +114 -0
  54. package/src/openclaw-plugin.ts +439 -0
  55. package/src/provider.test.ts +311 -0
  56. package/src/provider.ts +444 -0
  57. package/src/relay-endpoints.test.ts +17 -0
  58. package/src/relay-endpoints.ts +25 -0
  59. package/src/relay-provider-client.ts +341 -0
  60. package/src/types.ts +60 -0
  61. package/tsconfig.json +9 -0
@@ -0,0 +1,105 @@
1
+ import type { BridgeResponse, PrivateClawAgentBridge, PrivateClawConversationTurn } from "../types.js";
2
+
3
+ interface OpenAICompatibleBridgeOptions {
4
+ endpoint: string;
5
+ model: string;
6
+ apiKey?: string;
7
+ systemPrompt?: string;
8
+ }
9
+
10
+ interface OpenAICompatibleResponse {
11
+ choices?: Array<{
12
+ message?: {
13
+ content?:
14
+ | string
15
+ | Array<{
16
+ type?: string;
17
+ text?: string;
18
+ }>;
19
+ };
20
+ }>;
21
+ error?: {
22
+ message?: string;
23
+ };
24
+ }
25
+
26
+ type OpenAICompatibleRole = "system" | "user" | "assistant";
27
+
28
+ function normalizeRole(role: PrivateClawConversationTurn["role"]): OpenAICompatibleRole {
29
+ switch (role) {
30
+ case "assistant":
31
+ return "assistant";
32
+ case "system":
33
+ return "system";
34
+ case "user":
35
+ return "user";
36
+ }
37
+ }
38
+
39
+ function normalizeContent(
40
+ content:
41
+ | string
42
+ | Array<{
43
+ type?: string;
44
+ text?: string;
45
+ }>
46
+ | undefined,
47
+ ): string {
48
+ if (typeof content === "string") {
49
+ return content.trim();
50
+ }
51
+
52
+ if (!Array.isArray(content)) {
53
+ return "";
54
+ }
55
+
56
+ return content
57
+ .map((part) => (part.type === "text" && typeof part.text === "string" ? part.text : ""))
58
+ .join("")
59
+ .trim();
60
+ }
61
+
62
+ export class OpenAICompatibleBridge implements PrivateClawAgentBridge {
63
+ constructor(private readonly options: OpenAICompatibleBridgeOptions) {}
64
+
65
+ async handleUserMessage(params: {
66
+ history: ReadonlyArray<PrivateClawConversationTurn>;
67
+ }): Promise<BridgeResponse> {
68
+ const messages = [
69
+ ...(this.options.systemPrompt
70
+ ? [{ role: "system" as const, content: this.options.systemPrompt }]
71
+ : []),
72
+ ...params.history.map((turn) => ({
73
+ role: normalizeRole(turn.role),
74
+ content: turn.text,
75
+ })),
76
+ ];
77
+
78
+ const response = await fetch(this.options.endpoint, {
79
+ method: "POST",
80
+ headers: {
81
+ "content-type": "application/json",
82
+ ...(this.options.apiKey ? { authorization: `Bearer ${this.options.apiKey}` } : {}),
83
+ },
84
+ body: JSON.stringify({
85
+ model: this.options.model,
86
+ messages,
87
+ }),
88
+ });
89
+
90
+ const payload = (await response.json()) as OpenAICompatibleResponse;
91
+ if (!response.ok) {
92
+ throw new Error(
93
+ payload.error?.message ??
94
+ `OpenAI-compatible bridge responded with ${response.status} ${response.statusText}`,
95
+ );
96
+ }
97
+
98
+ const text = normalizeContent(payload.choices?.[0]?.message?.content);
99
+ if (text !== "") {
100
+ return text;
101
+ }
102
+
103
+ throw new Error("OpenAI-compatible bridge did not return a text response.");
104
+ }
105
+ }
@@ -0,0 +1,254 @@
1
+ import assert from "node:assert/strict";
2
+ import fs from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import test from "node:test";
6
+ import { zipSync } from "fflate";
7
+ import {
8
+ OpenClawAgentBridge,
9
+ parseOpenClawAgentOutput,
10
+ } from "./openclaw-agent-bridge.js";
11
+
12
+ test("parseOpenClawAgentOutput returns a string for a single payload", () => {
13
+ const result = parseOpenClawAgentOutput(
14
+ JSON.stringify({
15
+ status: "ok",
16
+ result: {
17
+ payloads: [{ text: "pong" }],
18
+ },
19
+ }),
20
+ );
21
+
22
+ assert.equal(result, "pong");
23
+ });
24
+
25
+ test("parseOpenClawAgentOutput returns multi-message response for multiple payloads", () => {
26
+ const result = parseOpenClawAgentOutput(
27
+ JSON.stringify({
28
+ status: "ok",
29
+ result: {
30
+ payloads: [{ text: "first" }, { text: "second" }],
31
+ },
32
+ }),
33
+ );
34
+
35
+ assert.deepEqual(result, { messages: [{ text: "first" }, { text: "second" }] });
36
+ });
37
+
38
+ test("OpenClawAgentBridge invokes openclaw agent with session-aware arguments", async () => {
39
+ let invokedFile = "";
40
+ let invokedArgs: string[] = [];
41
+
42
+ const bridge = new OpenClawAgentBridge({
43
+ executable: "openclaw-custom",
44
+ agentId: "ops",
45
+ channel: "discord",
46
+ thinking: "low",
47
+ timeoutSeconds: 30,
48
+ execFileImpl: (file, args, _options, callback) => {
49
+ invokedFile = file;
50
+ invokedArgs = args;
51
+ callback(
52
+ null,
53
+ JSON.stringify({
54
+ status: "ok",
55
+ result: {
56
+ payloads: [{ text: "bridge-ok" }],
57
+ },
58
+ }),
59
+ "",
60
+ );
61
+ },
62
+ });
63
+
64
+ const result = await bridge.handleUserMessage({
65
+ sessionId: "privateclaw-session",
66
+ message: "hello",
67
+ history: [],
68
+ invite: {
69
+ version: 1,
70
+ sessionId: "privateclaw-session",
71
+ sessionKey: "test",
72
+ appWsUrl: "ws://127.0.0.1/app?sessionId=privateclaw-session",
73
+ expiresAt: new Date().toISOString(),
74
+ },
75
+ });
76
+
77
+ assert.equal(invokedFile, "openclaw-custom");
78
+ assert.deepEqual(invokedArgs, [
79
+ "agent",
80
+ "--session-id",
81
+ "privateclaw-session",
82
+ "--message",
83
+ "hello",
84
+ "--json",
85
+ "--agent",
86
+ "ops",
87
+ "--channel",
88
+ "discord",
89
+ "--thinking",
90
+ "low",
91
+ "--timeout",
92
+ "30",
93
+ ]);
94
+ assert.equal(result, "bridge-ok");
95
+ });
96
+
97
+ test("OpenClawAgentBridge stages image and PDF attachments with tool hints", async (t) => {
98
+ const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-bridge-"));
99
+ t.after(async () => {
100
+ await fs.rm(workspaceDir, { recursive: true, force: true });
101
+ });
102
+
103
+ let invokedArgs: string[] = [];
104
+ const bridge = new OpenClawAgentBridge({
105
+ workspaceDir,
106
+ execFileImpl: (_file, args, _options, callback) => {
107
+ invokedArgs = args;
108
+ callback(
109
+ null,
110
+ JSON.stringify({
111
+ status: "ok",
112
+ result: {
113
+ payloads: [{ text: "attachment-ok" }],
114
+ },
115
+ }),
116
+ "",
117
+ );
118
+ },
119
+ });
120
+
121
+ const result = await bridge.handleUserMessage({
122
+ sessionId: "privateclaw-session",
123
+ message: "Please inspect these files.",
124
+ attachments: [
125
+ {
126
+ id: "image-1",
127
+ name: "photo.png",
128
+ mimeType: "image/png",
129
+ sizeBytes: 16,
130
+ dataBase64: Buffer.from("image-bytes").toString("base64"),
131
+ },
132
+ {
133
+ id: "pdf-1",
134
+ name: "report.pdf",
135
+ mimeType: "application/pdf",
136
+ sizeBytes: 16,
137
+ dataBase64: Buffer.from("%PDF-1.7").toString("base64"),
138
+ },
139
+ ],
140
+ history: [],
141
+ invite: {
142
+ version: 1,
143
+ sessionId: "privateclaw-session",
144
+ sessionKey: "test",
145
+ appWsUrl: "ws://127.0.0.1/app?sessionId=privateclaw-session",
146
+ expiresAt: new Date().toISOString(),
147
+ },
148
+ });
149
+
150
+ const prompt = invokedArgs[4];
151
+ assert.equal(invokedArgs[0], "agent");
152
+ assert.equal(invokedArgs[1], "--session-id");
153
+ assert.equal(invokedArgs[2], "privateclaw-session");
154
+ assert.equal(invokedArgs[3], "--message");
155
+ assert.match(prompt ?? "", /PrivateClaw staged the attachments into the OpenClaw workspace\./u);
156
+ assert.match(prompt ?? "", /Use the image tool with "privateclaw\/privateclaw-session\//u);
157
+ assert.match(prompt ?? "", /Use the pdf tool with "privateclaw\/privateclaw-session\//u);
158
+ assert.equal(result, "attachment-ok");
159
+
160
+ const stagedDir = path.join(workspaceDir, "privateclaw", "privateclaw-session");
161
+ const stagedNames = await fs.readdir(stagedDir);
162
+ assert.equal(stagedNames.length, 2);
163
+ assert(stagedNames.some((name) => name.endsWith("-photo.png")));
164
+ assert(stagedNames.some((name) => name.endsWith("-report.pdf")));
165
+ });
166
+
167
+ test("OpenClawAgentBridge extracts DOCX text and points the agent at the staged text file", async (t) => {
168
+ const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "privateclaw-bridge-"));
169
+ t.after(async () => {
170
+ await fs.rm(workspaceDir, { recursive: true, force: true });
171
+ });
172
+
173
+ let invokedArgs: string[] = [];
174
+ const bridge = new OpenClawAgentBridge({
175
+ workspaceDir,
176
+ execFileImpl: (_file, args, _options, callback) => {
177
+ invokedArgs = args;
178
+ callback(
179
+ null,
180
+ JSON.stringify({
181
+ status: "ok",
182
+ result: {
183
+ payloads: [{ text: "docx-ok" }],
184
+ },
185
+ }),
186
+ "",
187
+ );
188
+ },
189
+ });
190
+
191
+ const docxBytes = buildMinimalDocx("Hello from DOCX");
192
+ const result = await bridge.handleUserMessage({
193
+ sessionId: "privateclaw-session",
194
+ message: "",
195
+ attachments: [
196
+ {
197
+ id: "docx-1",
198
+ name: "spec.docx",
199
+ mimeType: "application/octet-stream",
200
+ sizeBytes: docxBytes.length,
201
+ dataBase64: docxBytes.toString("base64"),
202
+ },
203
+ ],
204
+ history: [],
205
+ invite: {
206
+ version: 1,
207
+ sessionId: "privateclaw-session",
208
+ sessionKey: "test",
209
+ appWsUrl: "ws://127.0.0.1/app?sessionId=privateclaw-session",
210
+ expiresAt: new Date().toISOString(),
211
+ },
212
+ });
213
+
214
+ const prompt = invokedArgs[4] ?? "";
215
+ assert.match(
216
+ prompt,
217
+ /The user sent one or more file attachments without additional text\./u,
218
+ );
219
+ assert.match(prompt, /extractedTextPath: privateclaw\/privateclaw-session\//u);
220
+ assert.match(prompt, /Read "privateclaw\/privateclaw-session\/.*\.extracted\.txt" first/u);
221
+
222
+ const stagedDir = path.join(workspaceDir, "privateclaw", "privateclaw-session");
223
+ const stagedNames = await fs.readdir(stagedDir);
224
+ const extractedTextName = stagedNames.find((name) => name.endsWith(".extracted.txt"));
225
+ assert.ok(extractedTextName);
226
+ const extractedText = await fs.readFile(path.join(stagedDir, extractedTextName), "utf8");
227
+ assert.match(extractedText, /Hello from DOCX/u);
228
+ assert.equal(result, "docx-ok");
229
+ });
230
+
231
+ function buildMinimalDocx(text: string): Buffer {
232
+ const archive = zipSync({
233
+ "word/document.xml": strToBytes(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
234
+ <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
235
+ <w:body>
236
+ <w:p><w:r><w:t>${escapeXml(text)}</w:t></w:r></w:p>
237
+ </w:body>
238
+ </w:document>`),
239
+ });
240
+ return Buffer.from(archive);
241
+ }
242
+
243
+ function strToBytes(value: string): Uint8Array {
244
+ return new TextEncoder().encode(value);
245
+ }
246
+
247
+ function escapeXml(value: string): string {
248
+ return value
249
+ .replaceAll("&", "&amp;")
250
+ .replaceAll("<", "&lt;")
251
+ .replaceAll(">", "&gt;")
252
+ .replaceAll('"', "&quot;")
253
+ .replaceAll("'", "&apos;");
254
+ }