@johngalt5/bsv-overlay 0.2.13 → 0.2.14
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 +29 -10
- 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,31 @@ function stopAutoImport() {
|
|
|
140
141
|
}
|
|
141
142
|
}
|
|
142
143
|
|
|
144
|
+
// Wake the agent via gateway WebSocket JSON-RPC (event-driven, zero polling)
|
|
145
|
+
function wakeAgent(text: string, logger?: any) {
|
|
146
|
+
try {
|
|
147
|
+
const ws = new WebSocket('ws://127.0.0.1:18789');
|
|
148
|
+
const timeout = setTimeout(() => { try { ws.close(); } catch {} }, 5000);
|
|
149
|
+
ws.on('open', () => {
|
|
150
|
+
ws.send(JSON.stringify({
|
|
151
|
+
jsonrpc: '2.0',
|
|
152
|
+
id: `overlay-wake-${Date.now()}`,
|
|
153
|
+
method: 'cron.wake',
|
|
154
|
+
params: { mode: 'now', text },
|
|
155
|
+
}));
|
|
156
|
+
clearTimeout(timeout);
|
|
157
|
+
setTimeout(() => { try { ws.close(); } catch {} }, 500);
|
|
158
|
+
logger?.info?.('[bsv-overlay] Agent woken via WebSocket');
|
|
159
|
+
});
|
|
160
|
+
ws.on('error', (err) => {
|
|
161
|
+
clearTimeout(timeout);
|
|
162
|
+
logger?.warn?.('[bsv-overlay] WebSocket wake failed:', err.message);
|
|
163
|
+
});
|
|
164
|
+
} catch (err: any) {
|
|
165
|
+
logger?.warn?.('[bsv-overlay] Wake failed:', err.message);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
143
169
|
// Categorize WebSocket events into notification types
|
|
144
170
|
function categorizeEvent(event) {
|
|
145
171
|
const base = { ts: Date.now(), from: event.from?.slice(0, 16), fullFrom: event.from };
|
|
@@ -191,18 +217,11 @@ function startBackgroundService(env, cliPath, logger) {
|
|
|
191
217
|
const alertDir = path.join(process.env.HOME || '', '.clawdbot', 'bsv-overlay');
|
|
192
218
|
fs.mkdirSync(alertDir, { recursive: true });
|
|
193
219
|
|
|
194
|
-
// Detect queued-for-agent events —
|
|
220
|
+
// Detect queued-for-agent events — wake agent via WebSocket
|
|
195
221
|
if (event.action === 'queued-for-agent' && event.serviceId) {
|
|
196
222
|
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 {}
|
|
223
|
+
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: { ... } })`;
|
|
224
|
+
wakeAgent(wakeText, logger);
|
|
206
225
|
}
|
|
207
226
|
|
|
208
227
|
// Write payment/activity notifications for ALL significant events
|