@rethinkingstudio/clawpilot 1.0.17 → 1.0.19

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.
@@ -1,6 +1,17 @@
1
1
  import { WebSocket } from "ws";
2
2
  import { OpenClawGatewayClient } from "./gateway-client.js";
3
3
  import { handleLocalCommand } from "../commands/local-handlers.js";
4
+ import { handleProviderCommand } from "../commands/provider-handlers.js";
5
+ import { homedir } from "os";
6
+ import { join } from "path";
7
+ import { mkdir, writeFile } from "fs/promises";
8
+ import { randomUUID } from "crypto";
9
+
10
+ // ---------------------------------------------------------------------------
11
+ // Constants
12
+ // ---------------------------------------------------------------------------
13
+
14
+ const OUTBOUND_DIR = join(homedir(), ".openclaw", "media", "outbound");
4
15
 
5
16
  // ---------------------------------------------------------------------------
6
17
  // Messages: relay client ↔ relay server
@@ -98,7 +109,7 @@ export async function runRelayManager(opts: RelayManagerOptions): Promise<boolea
98
109
  gatewayClient.start();
99
110
  });
100
111
 
101
- relayWs.on("message", (raw) => {
112
+ relayWs.on("message", async (raw) => {
102
113
  let msg: FromServer;
103
114
  try {
104
115
  msg = JSON.parse(raw.toString()) as FromServer;
@@ -111,6 +122,23 @@ export async function runRelayManager(opts: RelayManagerOptions): Promise<boolea
111
122
  const requestId = msg.id;
112
123
  console.log(`[relay] cmd received method=${msg.method} id=${requestId ?? "(no-id)"}`);
113
124
 
125
+ // Handle clawpilot.provider.* commands locally (async)
126
+ const providerPromise = handleProviderCommand(msg.method, msg.params);
127
+ if (providerPromise !== null) {
128
+ const result = await providerPromise;
129
+ if (requestId) {
130
+ send({
131
+ type: "res",
132
+ id: requestId,
133
+ ok: result.ok,
134
+ ...(result.ok
135
+ ? { payload: result.payload }
136
+ : { error: { message: result.error } }),
137
+ });
138
+ }
139
+ return;
140
+ }
141
+
114
142
  // Handle clawpilot.* commands locally without forwarding to the gateway
115
143
  const localResult = handleLocalCommand(msg.method);
116
144
  if (localResult !== null) {
@@ -124,12 +152,43 @@ export async function runRelayManager(opts: RelayManagerOptions): Promise<boolea
124
152
  return;
125
153
  }
126
154
 
127
- // Debug log for chat.send with attachments
155
+ // Handle chat.send with attachments - save to disk and add path reference
128
156
  if (msg.method === "chat.send") {
129
157
  const params = msg.params as any;
130
158
  if (params.attachments && params.attachments.length > 0) {
131
- console.log(`[relay->gateway] chat.send with ${params.attachments.length} attachment(s), keys: ${Object.keys(params).join(', ')}`);
132
- console.log(`[relay->gateway] first attachment: mimeType=${params.attachments[0].mimeType}, fileName=${params.attachments[0].fileName}, contentLength=${params.attachments[0].content?.length}`);
159
+ const fileReferences: string[] = [];
160
+
161
+ // Ensure outbound directory exists
162
+ await mkdir(OUTBOUND_DIR, { recursive: true });
163
+
164
+ // Save each attachment to disk and create path reference
165
+ for (const att of params.attachments) {
166
+ try {
167
+ // Decode base64 to buffer
168
+ const buffer = Buffer.from(att.content, "base64");
169
+ const ext = att.mimeType === "image/png" ? ".png" : ".jpg";
170
+ const stagedFileName = `${randomUUID()}${ext}`;
171
+ const stagedPath = join(OUTBOUND_DIR, stagedFileName);
172
+
173
+ // Write to disk
174
+ await writeFile(stagedPath, buffer);
175
+ console.log(`[relay] Saved attachment to: ${stagedPath}`);
176
+
177
+ // Create path reference (same format as ClawX)
178
+ fileReferences.push(
179
+ `[media attached: ${stagedPath} (${att.mimeType}) | ${stagedPath}]`
180
+ );
181
+ } catch (err) {
182
+ console.error(`[relay] Failed to save attachment: ${err}`);
183
+ }
184
+ }
185
+
186
+ // Append file references to message
187
+ if (fileReferences.length > 0) {
188
+ const refs = fileReferences.join("\n");
189
+ params.message = params.message ? `${params.message}\n\n${refs}` : refs;
190
+ console.log(`[relay] Added file references to message`);
191
+ }
133
192
  }
134
193
  }
135
194