import {
buildAgentPaymentRequest,
validateAgentScope,
type AgentScope,
} from "@conduitexchange/connect";
const SCOPE: AgentScope = {
agentId: "ag_inference_42",
budget: "10", // $10 lifetime cap
perCallCap: "0.25", // never exceed $0.25 per call
expiresAt: new Date(Date.now() + 3600_000).toISOString(),
allowedMerchants: [process.env.MERCHANT!],
};
export async function chargeAgent(callId: string, amount: string) {
const charge = {
agentId: SCOPE.agentId,
callId,
purpose: "inference" as const,
amount,
currency: "USDC" as const,
merchantAddress: process.env.MERCHANT!,
};
// Pure check — no network call, no side effects.
const v = validateAgentScope(SCOPE, charge, alreadySpent());
if (!v.ok) throw new Error("scope rejected: " + v.reason);
// Shape the on-wire request. Server-side, your handler verifies
// an HMAC signature derived from the agent's secret before settling.
const req = buildAgentPaymentRequest(charge);
await fetch("/api/connect/agent/charge", {
method: "POST",
headers: { "x-conduit-signature": signHmac(req) },
body: JSON.stringify(req),
});
}