youraitools-lax-agent / tree / scripts/record-example.mjs ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
// Records one example by running the PRODUCTION app code of a tool locally: the real handler answers a real request,
// paid with a throwaway test payer through a fake facilitator (nothing settles on chain, nothing is spent).
// Usage (run by scripts/record-examples.sh, which copies this file into the tool repo so imports resolve there):
//   node .record-example.mjs <capability> <METHOD> <path?query> [jsonBody]  > out.json
import { createApp } from './src/app.js';
import { Stats } from './src/stats.js';
import { wrapFetchWithPaymentFromConfig } from '@x402/fetch';
import { ExactEvmScheme } from '@x402/evm/exact/client';
import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts';

const [capability, method, pathq, body] = process.argv.slice(2);
const PAY_TO = '0x3c1A1E01e790Cd326495631B3b38Bd7068F08d6F', NETWORK = 'eip155:8453';
const fac = {
  async getSupported() { return { kinds: [{ x402Version: 2, scheme: 'exact', network: NETWORK }], extensions: ['bazaar'], signers: {} }; },
  async verify(p, req) { const a = p?.payload?.authorization; return a && a.to.toLowerCase() === req.payTo.toLowerCase() && String(a.value) === String(req.amount) ? { isValid: true, payer: a.from } : { isValid: false, invalidReason: 'fake_invalid' }; },
  async settle(p, req) { return { success: true, transaction: 'local-recording-not-settled', network: req.network, payer: p.payload.authorization.from }; },
};
const kv = (() => { const m = new Map(); return { async get(k) { return m.has(k) ? JSON.parse(m.get(k)) : null; }, async put(k, v) { m.set(k, v); } }; })();
const prefix = process.env.PREFIX; // e.g. /webread
const app = createApp({ facilitatorClient: fac, payTo: PAY_TO, network: NETWORK, publicUrl: 'https://api.youraitools.co' + prefix, stats: new Stats(kv, { flushMs: 0 }) });
const local = 'https://local.test';
const fetchApp = (input, init) => app.fetch(input instanceof Request ? input : new Request(input, init));
const pay = wrapFetchWithPaymentFromConfig(fetchApp, { schemes: [{ network: NETWORK, client: new ExactEvmScheme(privateKeyToAccount(generatePrivateKey())) }] });
const init = { method, headers: body ? { 'content-type': 'application/json' } : {}, body: body || undefined };
const unpaid = await fetchApp(local + pathq, init);
const r = await pay(local + pathq, init);
const response = await r.json();
const [path, qs] = pathq.split('?');
const request = { method, url: 'https://api.youraitools.co' + prefix + path };
if (qs) request.query = Object.fromEntries(new URLSearchParams(qs));
if (body) request.body = JSON.parse(body);
process.stdout.write(JSON.stringify({ capability, request, response, recorded: { at: new Date().toISOString(), unpaid_status: unpaid.status, paid_status: r.status, how: 'production handler run locally, paid by a throwaway test payer through a fake facilitator (no on-chain settlement)' } }, null, 1) + '\n');

agentgate-source: code mirror on Cloudflare Pages. All repos