// HTTP layer shared by the Cloudflare Worker and the local Node server.
import { screen, verify, VERSION } from './core.js';
import { verifyChain } from './receipt.js';
const J = (status, obj) => new Response(JSON.stringify(obj, null, 2), { status, headers: { 'content-type': 'application/json; charset=utf-8', 'access-control-allow-origin': '*', 'cache-control': 'no-store' } });
export async function handle(request, deps) {
const url = new URL(request.url);
const p = url.pathname.replace(/\/+$/, '') || '/';
if (request.method === 'OPTIONS') return new Response(null, { status: 204, headers: { 'access-control-allow-origin': '*', 'access-control-allow-methods': 'GET,POST,OPTIONS', 'access-control-allow-headers': 'content-type' } });
try {
if (p === '/' || p === '/v1') return J(200, {
service: 'AgentComply', version: VERSION,
what: 'Pre-payment compliance screening for AI agents paying over x402: one call returns ok / caution / block plus a signed, hash-chained compliance receipt.',
endpoints: { 'POST /v1/screen': 'screen a payment (JSON body)', 'GET /v1/screen?payTo=&chain=&intent=&amount_usd=': 'same, query form', 'POST /v1/verify': 'verify a receipt (body: receipt JSON)', 'GET /v1/pubkey': 'issuer public key (JWK)', 'GET /v1/policy': 'active policy', 'GET /v1/ledger/verify': 'verify the whole receipt chain' },
mode: deps.mode, disclaimer: 'Informational only; not legal advice.',
});
if (p === '/v1/pubkey') return J(200, deps.signer.publicJwk);
if (p === '/v1/policy') return J(200, deps.policy);
if (p === '/v1/screen') {
let raw;
if (request.method === 'POST') { try { raw = await request.json(); } catch { return J(400, { error: 'invalid_input', errors: ['body must be JSON'] }); } }
else if (request.method === 'GET') { raw = Object.fromEntries(url.searchParams); }
else return J(405, { error: 'method_not_allowed' });
const r = await screen(raw, deps);
return J(r.status, r.body);
}
if (p === '/v1/verify' && request.method === 'POST') {
let body; try { body = await request.json(); } catch { return J(400, { error: 'invalid_input', errors: ['body must be a receipt JSON'] }); }
const receipt = body.receipt || body;
return J(200, await verify(receipt, deps.signer.publicJwk));
}
if (p === '/v1/ledger/verify') return J(200, await verifyChain(await deps.store.list(), deps.signer.publicJwk));
return J(404, { error: 'not_found' });
} catch (e) {
return J(500, { error: 'internal_error', message: 'unexpected error; no receipt issued' });
}
}
agentgate-source: code mirror on Cloudflare Pages. All repos