-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelay.cjs
More file actions
321 lines (282 loc) · 9.89 KB
/
relay.cjs
File metadata and controls
321 lines (282 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env node
/**
* AskClaw IM Relay — bridges NATS ↔ local gateway WebSocket
*
* Runs on each agent's VPS. Subscribes to askclaw.chat.{agent}.request,
* connects to the local gateway via WebSocket, streams responses back
* via NATS to the reply subject.
*
* Usage:
* AGENT=ash GATEWAY_URL=ws://127.0.0.1:18789/ GATEWAY_TOKEN=xxx \
* GATEWAY_ORIGIN=https://your-openclaw.example.com NATS_URL=tls://127.0.0.1:4222 \
* NATS_USER=myuser NATS_PASS=xxx node relay.js
*/
'use strict';
const WebSocket = require('ws');
const { randomUUID } = require('crypto');
const { connect, StringCodec } = require('nats');
const fs = require('fs');
const path = require('path');
const AGENT = process.env.AGENT || 'ash';
const GATEWAY_URL = process.env.GATEWAY_URL || 'ws://127.0.0.1:18789/';
const GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || '';
const GATEWAY_ORIGIN = process.env.GATEWAY_ORIGIN || 'http://127.0.0.1:18789';
const NATS_URL = process.env.NATS_URL || 'tls://127.0.0.1:4222';
const NATS_USER = process.env.NATS_USER || '';
const NATS_PASS = process.env.NATS_PASS || '';
const NATS_CA = process.env.NATS_CA || '/etc/nats/certs/ca.pem';
const SUBJECT = `askclaw.chat.${AGENT}.request`;
const sc = StringCodec();
let nc = null;
function log(...args) {
console.log(new Date().toISOString(), `[relay:${AGENT}]`, ...args);
}
function uuid() {
return randomUUID();
}
/* ── Gateway WebSocket session ── */
class GatewaySession {
constructor() {
this.ws = null;
this.connected = false;
this.connecting = false;
this.pendingRequests = new Map(); // id → {resolve, reject}
this.activeStream = null; // {replySubject, runId}
this.reconnectDelay = 1000;
}
connect() {
if (this.connecting || this.connected) return;
this.connecting = true;
log(`Connecting to gateway ${GATEWAY_URL}`);
const ws = new WebSocket(GATEWAY_URL, {
headers: { Origin: GATEWAY_ORIGIN }
});
ws.on('open', () => {
log('WS open, sending connect frame');
const connectId = uuid();
ws.send(JSON.stringify({
type: 'req', id: connectId, method: 'connect',
params: {
minProtocol: 3, maxProtocol: 3,
auth: { token: GATEWAY_TOKEN },
role: 'operator',
scopes: ['operator.write', 'operator.read'],
client: { id: 'webchat', displayName: `Relay:${AGENT}`, mode: 'webchat', version: '2.0', platform: 'node' }
}
}));
this.pendingRequests.set(connectId, {
resolve: () => {
this.connected = true;
this.connecting = false;
this.reconnectDelay = 1000;
log('Gateway connected');
},
reject: (err) => {
log('Connect rejected:', err.message);
ws.close();
}
});
});
ws.on('message', (data) => {
let msg;
try { msg = JSON.parse(data.toString()); } catch { return; }
if (msg.type === 'res') {
const pending = this.pendingRequests.get(msg.id);
if (pending) {
this.pendingRequests.delete(msg.id);
if (msg.ok) {
// Store runId for chat.send responses
if (msg.payload && msg.payload.runId && this.activeStream) {
this.activeStream.runId = msg.payload.runId;
}
pending.resolve(msg.payload);
} else {
pending.reject(new Error(msg.error?.message || 'gateway error'));
}
}
} else if (msg.type === 'event' && msg.event === 'chat') {
this._handleChatEvent(msg.payload);
}
// Ignore connect.challenge, health, etc.
});
ws.on('close', (code) => {
log(`WS closed (${code}), reconnecting in ${this.reconnectDelay}ms`);
this.ws = null;
this.connected = false;
this.connecting = false;
// Reject pending requests
for (const [, p] of this.pendingRequests) p.reject(new Error('ws closed'));
this.pendingRequests.clear();
// Fail active stream
if (this.activeStream) {
this._publishReply(this.activeStream.replySubject, { error: 'gateway disconnected' });
this._publishReply(this.activeStream.replySubject, '[DONE]');
this.activeStream = null;
}
setTimeout(() => this.connect(), this.reconnectDelay);
this.reconnectDelay = Math.min(this.reconnectDelay * 1.5, 30000);
});
ws.on('error', (err) => {
log('WS error:', err.message);
});
this.ws = ws;
}
_handleChatEvent(payload) {
if (!payload || !this.activeStream) return;
const state = payload.state;
const text = extractText(payload.message);
const stream = this.activeStream;
if (stream.runId && payload.runId && stream.runId !== payload.runId) return;
if (state === 'delta') {
stream.accumulated = text;
this._publishReply(stream.replySubject, { delta: text });
} else if (state === 'final') {
if (text && stream.accumulated === '') {
this._publishReply(stream.replySubject, { delta: text });
}
this._publishReply(stream.replySubject, '[DONE]');
this.activeStream = null;
} else if (state === 'aborted' || state === 'error') {
this._publishReply(stream.replySubject, { error: payload.errorMessage || 'aborted' });
this._publishReply(stream.replySubject, '[DONE]');
this.activeStream = null;
}
}
_publishReply(subject, data) {
if (!nc) return;
const payload = typeof data === 'string' ? data : JSON.stringify(data);
nc.publish(subject, sc.encode(payload));
}
async sendMessage(text, replySubject, files) {
if (!this.connected || !this.ws) {
this._publishReply(replySubject, { error: 'gateway not connected' });
this._publishReply(replySubject, '[DONE]');
return;
}
// Only one stream at a time
if (this.activeStream) {
this._publishReply(replySubject, { error: 'busy — another request is in progress' });
this._publishReply(replySubject, '[DONE]');
return;
}
this.activeStream = { replySubject, runId: null, accumulated: '' };
const reqId = uuid();
const sendPromise = new Promise((resolve, reject) => {
this.pendingRequests.set(reqId, { resolve, reject });
});
// Build chat.send params — include attachments if files are present
const params = {
sessionKey: 'main',
message: text,
idempotencyKey: uuid()
};
if (Array.isArray(files) && files.length > 0) {
// Convert frontend file format → gateway attachment format
// Frontend sends: { data: base64, name: string, type: mimeType }
// Gateway expects: { content: base64, fileName: string, mimeType: string }
params.attachments = files.map(f => ({
content: f.data,
fileName: f.name,
mimeType: f.type
}));
log(`Sending ${files.length} attachment(s) to gateway`);
}
this.ws.send(JSON.stringify({
type: 'req', id: reqId, method: 'chat.send',
params
}));
try {
await sendPromise;
} catch (err) {
this._publishReply(replySubject, { error: err.message });
this._publishReply(replySubject, '[DONE]');
this.activeStream = null;
}
}
async getHistory(replySubject) {
if (!this.connected || !this.ws) {
this._publishReply(replySubject, JSON.stringify({ error: 'gateway not connected' }));
return;
}
const reqId = uuid();
const promise = new Promise((resolve, reject) => {
this.pendingRequests.set(reqId, { resolve, reject });
});
this.ws.send(JSON.stringify({
type: 'req', id: reqId, method: 'chat.history', params: {}
}));
try {
const result = await promise;
this._publishReply(replySubject, JSON.stringify({ ok: true, payload: result }));
} catch (err) {
this._publishReply(replySubject, JSON.stringify({ error: err.message }));
}
}
resetSession() {
// Close and reconnect to get a fresh webchat session
if (this.ws) {
this.ws.close();
}
}
}
function extractText(message) {
if (!message) return '';
const content = message.content;
if (!Array.isArray(content)) return '';
return content
.filter(c => (c.type === 'text' || c.type === 'output_text') && typeof c.text === 'string')
.map(c => c.text)
.join('');
}
/* ── Main ── */
async function main() {
log(`Starting relay for agent "${AGENT}"`);
log(`NATS: ${NATS_URL}, Subject: ${SUBJECT}`);
log(`Gateway: ${GATEWAY_URL}`);
// Connect to NATS
const tlsOpts = {};
if (NATS_CA && fs.existsSync(NATS_CA)) {
tlsOpts.ca = fs.readFileSync(NATS_CA);
}
nc = await connect({
servers: NATS_URL,
user: NATS_USER,
pass: NATS_PASS,
tls: Object.keys(tlsOpts).length > 0 ? tlsOpts : undefined
});
log('NATS connected');
// Connect to gateway
const gw = new GatewaySession();
gw.connect();
// Subscribe to chat requests
const sub = nc.subscribe(SUBJECT);
log(`Subscribed to ${SUBJECT}`);
for await (const msg of sub) {
const raw = sc.decode(msg.data);
let req;
try { req = JSON.parse(raw); } catch { continue; }
const replySubject = msg.reply || req.replySubject;
if (!replySubject) {
log('No reply subject, ignoring');
continue;
}
if (req.type === 'send') {
const fileCount = Array.isArray(req.files) ? req.files.length : 0;
log(`Chat request: "${(req.text || '').substring(0, 50)}..."${fileCount ? ` [${fileCount} file(s)]` : ''} → reply ${replySubject}`);
gw.sendMessage(req.text, replySubject, req.files);
} else if (req.type === 'history') {
log('History request');
gw.getHistory(replySubject);
} else if (req.type === 'new') {
log('New session request');
gw.resetSession();
nc.publish(replySubject, sc.encode(JSON.stringify({ ok: true })));
} else {
log('Unknown request type:', req.type);
}
}
}
main().catch(err => {
console.error('Fatal:', err);
process.exit(1);
});