@johngalt5/bsv-overlay 0.2.10 → 0.2.12

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 (2) hide show
  1. package/index.ts +30 -26
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -157,28 +157,19 @@ function startBackgroundService(env, cliPath, logger) {
157
157
  if (event.action === 'queued-for-agent' && event.serviceId) {
158
158
  logger?.info?.(`[bsv-overlay] ⚡ Incoming ${event.serviceId} request from ${event.from?.slice(0, 12)}... — waking agent`);
159
159
 
160
- // Wake the agent via gateway cron.wake RPC (fire-and-forget)
161
- const wakeText = `⚡ Incoming overlay service request: ${event.serviceId} from ${event.from?.slice(0, 16)}... (${event.satoshisReceived || '?'} sats paid). Check overlay({ action: "pending-requests" }) for details, fulfill it using your capabilities, then call overlay({ action: "fulfill", requestId: "...", recipientKey: "...", serviceId: "...", result: {...} }).`;
162
- (async () => {
163
- try {
164
- const gatewayUrl = 'http://127.0.0.1:18789';
165
- const wakeResp = await fetch(gatewayUrl, {
166
- method: 'POST',
167
- headers: { 'Content-Type': 'application/json' },
168
- body: JSON.stringify({
169
- jsonrpc: '2.0',
170
- id: `overlay-wake-${Date.now()}`,
171
- method: 'cron.wake',
172
- params: { mode: 'now', text: wakeText },
173
- }),
174
- });
175
- if (wakeResp.ok) {
176
- logger?.info?.('[bsv-overlay] Agent woken for service fulfillment');
177
- }
178
- } catch (wakeErr: any) {
179
- logger?.warn?.('[bsv-overlay] Failed to wake agent:', wakeErr.message);
180
- }
181
- })();
160
+ // Write alert file for the cron-based fulfillment checker to pick up
161
+ const alertDir = path.join(process.env.HOME || '', '.clawdbot', 'bsv-overlay');
162
+ const alertPath = path.join(alertDir, 'pending-alert.jsonl');
163
+ try {
164
+ fs.mkdirSync(alertDir, { recursive: true });
165
+ fs.appendFileSync(alertPath, JSON.stringify({
166
+ requestId: event.id,
167
+ serviceId: event.serviceId,
168
+ from: event.from,
169
+ satoshis: event.satoshisReceived,
170
+ ts: Date.now(),
171
+ }) + '\n');
172
+ } catch {}
182
173
  }
183
174
  } catch {}
184
175
  }
@@ -686,8 +677,11 @@ async function handleServiceRequest(params, env, cliPath, config, api) {
686
677
  throw new Error(`Service request failed: ${requestOutput.error}`);
687
678
  }
688
679
 
689
- // 7. Poll for response
690
- const maxPollAttempts = 12; // ~60 seconds with 5 second intervals
680
+ // 7. Poll for response (up to 120s, with early return)
681
+ // The WebSocket background service will also receive the response
682
+ // asynchronously, so we return a "pending" status if we time out
683
+ // rather than throwing an error.
684
+ const maxPollAttempts = 24; // ~120 seconds with 5 second intervals
691
685
  let attempts = 0;
692
686
 
693
687
  while (attempts < maxPollAttempts) {
@@ -699,7 +693,7 @@ async function handleServiceRequest(params, env, cliPath, config, api) {
699
693
  const pollOutput = parseCliOutput(pollResult.stdout);
700
694
 
701
695
  if (pollOutput.success && pollOutput.data) {
702
- // FIX: Check pollOutput.data.messages array for service-response
696
+ // Check pollOutput.data.messages array for service-response
703
697
  const messages = pollOutput.data.messages || [];
704
698
  for (const msg of messages) {
705
699
  if (msg.type === 'service-response' && msg.from === bestProvider.identityKey) {
@@ -720,7 +714,17 @@ async function handleServiceRequest(params, env, cliPath, config, api) {
720
714
  }
721
715
  }
722
716
 
723
- throw new Error(`Service request timed out after ${maxPollAttempts * 5} seconds`);
717
+ // Don't throw the response may still arrive via WebSocket
718
+ // Record the spend since payment was already sent
719
+ recordSpend(walletDir, price, service, bestProvider.agentName);
720
+ return {
721
+ provider: bestProvider.agentName,
722
+ cost: price,
723
+ status: "pending",
724
+ message: `Request sent and paid (${price} sats). The provider hasn't responded within 120s, but the response may still arrive via the background WebSocket service. Check notifications later.`,
725
+ requestId: requestOutput.data?.messageId,
726
+ providerKey: bestProvider.identityKey,
727
+ };
724
728
  }
725
729
 
726
730
  async function handleDiscover(params, env, cliPath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johngalt5/bsv-overlay",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Clawdbot BSV Overlay — agent discovery, service marketplace, and micropayments on the BSV blockchain",
5
5
  "type": "module",
6
6
  "files": [