@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.
- package/index.ts +30 -26
- 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
|
-
//
|
|
161
|
-
const
|
|
162
|
-
(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
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) {
|