@johngalt5/bsv-overlay 0.2.13 → 0.2.15
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/index.ts +37 -23
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { promisify } from 'util';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
5
|
import fs from 'fs';
|
|
6
|
+
import WebSocket from 'ws';
|
|
6
7
|
|
|
7
8
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
9
|
const __dirname = path.dirname(__filename);
|
|
@@ -140,6 +141,37 @@ function stopAutoImport() {
|
|
|
140
141
|
}
|
|
141
142
|
}
|
|
142
143
|
|
|
144
|
+
// Discover the gateway WebSocket port from environment
|
|
145
|
+
function getGatewayWsUrl(): string {
|
|
146
|
+
const port = process.env.CLAWDBOT_GATEWAY_PORT || process.env.OPENCLAW_GATEWAY_PORT || '18789';
|
|
147
|
+
return `ws://127.0.0.1:${port}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Wake the agent via gateway WebSocket JSON-RPC (event-driven, zero polling)
|
|
151
|
+
function wakeAgent(text: string, logger?: any) {
|
|
152
|
+
try {
|
|
153
|
+
const ws = new WebSocket(getGatewayWsUrl());
|
|
154
|
+
const timeout = setTimeout(() => { try { ws.close(); } catch {} }, 5000);
|
|
155
|
+
ws.on('open', () => {
|
|
156
|
+
ws.send(JSON.stringify({
|
|
157
|
+
jsonrpc: '2.0',
|
|
158
|
+
id: `overlay-wake-${Date.now()}`,
|
|
159
|
+
method: 'cron.wake',
|
|
160
|
+
params: { mode: 'now', text },
|
|
161
|
+
}));
|
|
162
|
+
clearTimeout(timeout);
|
|
163
|
+
setTimeout(() => { try { ws.close(); } catch {} }, 500);
|
|
164
|
+
logger?.info?.('[bsv-overlay] Agent woken via WebSocket');
|
|
165
|
+
});
|
|
166
|
+
ws.on('error', (err) => {
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
logger?.warn?.('[bsv-overlay] WebSocket wake failed:', err.message);
|
|
169
|
+
});
|
|
170
|
+
} catch (err: any) {
|
|
171
|
+
logger?.warn?.('[bsv-overlay] Wake failed:', err.message);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
143
175
|
// Categorize WebSocket events into notification types
|
|
144
176
|
function categorizeEvent(event) {
|
|
145
177
|
const base = { ts: Date.now(), from: event.from?.slice(0, 16), fullFrom: event.from };
|
|
@@ -191,18 +223,11 @@ function startBackgroundService(env, cliPath, logger) {
|
|
|
191
223
|
const alertDir = path.join(process.env.HOME || '', '.clawdbot', 'bsv-overlay');
|
|
192
224
|
fs.mkdirSync(alertDir, { recursive: true });
|
|
193
225
|
|
|
194
|
-
// Detect queued-for-agent events —
|
|
226
|
+
// Detect queued-for-agent events — wake agent via WebSocket
|
|
195
227
|
if (event.action === 'queued-for-agent' && event.serviceId) {
|
|
196
228
|
logger?.info?.(`[bsv-overlay] ⚡ Incoming ${event.serviceId} request from ${event.from?.slice(0, 12)}...`);
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
requestId: event.id,
|
|
200
|
-
serviceId: event.serviceId,
|
|
201
|
-
from: event.from,
|
|
202
|
-
satoshis: event.satoshisReceived,
|
|
203
|
-
ts: Date.now(),
|
|
204
|
-
}) + '\n');
|
|
205
|
-
} catch {}
|
|
229
|
+
const wakeText = `⚡ Incoming overlay service request!\n\nService: ${event.serviceId}\nFrom: ${event.from}\nPaid: ${event.satoshisReceived || '?'} sats\n\nFulfill it now:\n1. overlay({ action: "pending-requests" })\n2. Process the ${event.serviceId} request using your capabilities\n3. overlay({ action: "fulfill", requestId: "${event.id}", recipientKey: "${event.from}", serviceId: "${event.serviceId}", result: { ... } })`;
|
|
230
|
+
wakeAgent(wakeText, logger);
|
|
206
231
|
}
|
|
207
232
|
|
|
208
233
|
// Write payment/activity notifications for ALL significant events
|
|
@@ -560,19 +585,8 @@ export default function register(api) {
|
|
|
560
585
|
if (regOutput.success) {
|
|
561
586
|
api.log?.info?.('[bsv-overlay] Auto-registered on overlay network! Waking agent for service selection...');
|
|
562
587
|
// Wake the agent to present service selection to the user
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
await fetch('http://127.0.0.1:18789', {
|
|
566
|
-
method: 'POST',
|
|
567
|
-
headers: { 'Content-Type': 'application/json' },
|
|
568
|
-
body: JSON.stringify({
|
|
569
|
-
jsonrpc: '2.0',
|
|
570
|
-
id: `overlay-service-select-${Date.now()}`,
|
|
571
|
-
method: 'cron.wake',
|
|
572
|
-
params: { mode: 'now', text: wakeText },
|
|
573
|
-
}),
|
|
574
|
-
});
|
|
575
|
-
} catch {}
|
|
588
|
+
const wakeText = `🎉 BSV Overlay: Auto-registered on the network! Two things to set up:\n\n1. **Agent name**: Ask the user what name they want for their agent on the network. The current default is "${env.AGENT_NAME}". If they want a different name, they need to set it in the plugin config (agentName) and re-register.\n\n2. **Services**: Ask the user which services they want to advertise. Call overlay({ action: "register" }) to get the full list of available services with descriptions and suggested prices, then present them to the user. Let them choose which ones to enable. For each selected service, call overlay({ action: "advertise", serviceId: "...", name: "...", description: "...", priceSats: N }).`;
|
|
589
|
+
wakeAgent(wakeText, api.log);
|
|
576
590
|
}
|
|
577
591
|
}
|
|
578
592
|
}
|