UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

132 lines 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.x402DiscoverResourcesTool = exports.getX402PaymentInfoTool = exports.x402FetchTool = void 0; const zod_1 = require("zod"); const client_js_1 = require("../client.js"); const fetch_1 = require("@x402/fetch"); const evm_1 = require("@x402/evm"); const constants_js_1 = require("./constants.js"); const fetch_js_1 = require("../utils/fetch.js"); exports.x402FetchTool = (0, client_js_1.createTool)({ name: "x402Fetch", description: "Fetch an HTTP resource, automatically paying with USDC via x402 if the server requires payment. Supports any x402-enabled API endpoint.", parameters: zod_1.z.object({ url: zod_1.z.string().describe("The URL to fetch"), method: zod_1.z.string().optional().describe("HTTP method (default GET)"), headers: zod_1.z .record(zod_1.z.string()) .optional() .describe("Additional request headers"), body: zod_1.z.string().optional().describe("Request body for POST/PUT"), maxPaymentUsd: zod_1.z .number() .optional() .describe("Maximum payment in USD to allow (default 1.00)"), }), execute: async (client, args) => { const { url, method = "GET", headers = {}, body, maxPaymentUsd = 1.0 } = args; const walletClient = client.getWalletClient(); if (!walletClient?.account) { throw new Error("No wallet account available for x402 payments"); } const account = walletClient.account; if (!account.signTypedData) { throw new Error("Wallet account must support signTypedData for x402 payments"); } const publicClient = client.getPublicClient(); const signer = (0, evm_1.toClientEvmSigner)({ address: account.address, signTypedData: (msg) => account.signTypedData(msg), }, publicClient); const maxAmountBaseUnits = BigInt(Math.floor(maxPaymentUsd * 10 ** constants_js_1.USDC_DECIMALS)); const paymentClient = new fetch_1.x402Client(); paymentClient.register("eip155:*", new evm_1.ExactEvmScheme(signer)); paymentClient.registerPolicy((_version, requirements) => requirements.filter((r) => BigInt(r.amount) <= maxAmountBaseUnits)); let paymentMade = false; paymentClient.onAfterPaymentCreation(async () => { paymentMade = true; }); const fetchWithPayment = (0, fetch_1.wrapFetchWithPayment)(fetch, paymentClient); const response = await fetchWithPayment(url, { method, headers, ...(body ? { body } : {}), }); const responseBody = await response.text(); const responseHeaders = {}; response.headers.forEach((value, key) => { responseHeaders[key] = value; }); return { status: response.status, headers: responseHeaders, body: responseBody, paymentMade, }; }, }); exports.getX402PaymentInfoTool = (0, client_js_1.createTool)({ name: "getX402PaymentInfo", description: "Check the x402 payment requirements for a URL without making a payment. Returns pricing, accepted networks, and payment details.", parameters: zod_1.z.object({ url: zod_1.z.string().describe("The URL to check payment requirements for"), }), execute: async (_client, args) => { const { url } = args; const response = await fetch(url); if (response.status !== 402) { return { paymentRequired: false, status: response.status, }; } // Try v2: parse payment-required header (base64 JSON) const paymentHeader = response.headers.get("payment-required"); if (paymentHeader) { try { const decoded = JSON.parse(Buffer.from(paymentHeader, "base64").toString("utf-8")); return { paymentRequired: true, x402Version: 2, ...decoded, }; } catch { // Fall through to v1 body parsing } } // Try v1: parse response body as JSON try { const body = await response.json(); return { paymentRequired: true, x402Version: 1, ...body, }; } catch { return { paymentRequired: true, error: "Could not parse payment requirements", }; } }, }); exports.x402DiscoverResourcesTool = (0, client_js_1.createTool)({ name: "x402DiscoverResources", description: "Discover available x402-enabled paid services and APIs using the Bazaar discovery API. Search by keyword or category.", parameters: zod_1.z.object({ query: zod_1.z.string().optional().describe("Search query to filter resources"), }), execute: async (_client, args) => { const { query } = args; const url = new URL(constants_js_1.X402_DISCOVERY_URL); if (query) { url.searchParams.set("q", query); } const response = await fetch(url.toString()); await (0, fetch_js_1.assertOkResponse)(response, "Failed to fetch x402 resources"); return await response.json(); }, }); //# sourceMappingURL=tools.js.map