@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
160 lines (159 loc) • 7.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DirectX402RequestSchema = exports.RetryWithX402Schema = exports.HttpRequestSchema = exports.ListX402ServicesSchema = void 0;
exports.resolveFacilitatorUrl = resolveFacilitatorUrl;
const zod_1 = require("zod");
const constants_1 = require("./constants");
/**
* Resolves a facilitator name or URL to the actual URL.
*
* @param facilitator - Either a known facilitator name ('cdp', 'payai') or a custom URL
* @returns The facilitator URL
*/
function resolveFacilitatorUrl(facilitator) {
if (facilitator in constants_1.KNOWN_FACILITATORS) {
return constants_1.KNOWN_FACILITATORS[facilitator];
}
return facilitator;
}
// Schema for listing x402 services
exports.ListX402ServicesSchema = zod_1.z
.object({
facilitator: zod_1.z
.union([zod_1.z.enum(["cdp", "payai"]), zod_1.z.string().url()])
.default(constants_1.DEFAULT_FACILITATOR)
.describe("Facilitator to query: 'cdp' (Coinbase CDP), 'payai' (PayAI Network), or a custom facilitator URL."),
maxUsdcPrice: zod_1.z
.number()
.positive()
.finite()
.optional()
.describe("Optional maximum price in USDC whole units (e.g., 0.1 for 0.10 USDC). Only USDC payment options will be considered when this filter is applied."),
x402Versions: zod_1.z
.array(zod_1.z.union([zod_1.z.literal(1), zod_1.z.literal(2)]))
.default([1, 2])
.describe("Filter by x402 protocol version (1 or 2). Defaults to accepting both versions."),
keyword: zod_1.z
.string()
.optional()
.describe("Optional keyword to filter services by description (case-insensitive). Example: 'weather' to find weather-related services."),
})
.strip()
.describe("Parameters for listing x402 services with optional filtering");
// Schema for initial HTTP request
exports.HttpRequestSchema = zod_1.z
.object({
url: zod_1.z
.string()
.url()
.describe("The URL of the API endpoint (can be localhost for development)"),
method: zod_1.z
.enum(["GET", "POST", "PUT", "DELETE", "PATCH"])
.nullable()
.default("GET")
.describe("The HTTP method to use for the request"),
headers: zod_1.z
.record(zod_1.z.string())
.optional()
.nullable()
.describe("Optional headers to include in the request"),
queryParams: zod_1.z
.record(zod_1.z.string())
.optional()
.nullable()
.describe("Query parameters to append to the URL as key-value string pairs. " +
"Use ONLY for GET/DELETE requests. " +
"For POST/PUT/PATCH, you must use the 'body' parameter instead. " +
"Example: {location: 'NYC', units: 'metric'} becomes ?location=NYC&units=metric"),
body: zod_1.z
.any()
.optional()
.nullable()
.describe("Request body - REQUIRED for POST/PUT/PATCH requests when sending data. " +
"Always prefer 'body' over 'queryParams' for POST/PUT/PATCH. " +
"Do NOT use for GET or DELETE, use queryParams instead."),
})
.strip()
.describe("Instructions for making a basic HTTP request");
// Payment option schema that supports both v1 and v2 formats
const PaymentOptionSchema = zod_1.z
.object({
scheme: zod_1.z.string().describe("Payment scheme (e.g., 'exact')"),
network: zod_1.z
.string()
.describe("Network identifier (v1: 'base-sepolia' or v2 CAIP-2: 'eip155:84532')"),
asset: zod_1.z.string().describe("Asset address or identifier"),
// v1 format
maxAmountRequired: zod_1.z.string().optional().describe("Maximum amount required (v1 format)"),
// v2 format
amount: zod_1.z.string().optional().describe("Amount required (v2 format)"),
price: zod_1.z.string().optional().describe("Price (v2 format, e.g., '$0.01')"),
payTo: zod_1.z.string().optional().describe("Payment recipient address (v2 format)"),
})
.describe("Payment option supporting both v1 and v2 x402 formats");
// Schema for retrying a failed request with x402 payment
exports.RetryWithX402Schema = zod_1.z
.object({
url: zod_1.z
.string()
.url()
.describe("The URL of the API endpoint (can be localhost for development)"),
method: zod_1.z
.enum(["GET", "POST", "PUT", "DELETE", "PATCH"])
.nullable()
.default("GET")
.describe("The HTTP method to use for the request"),
headers: zod_1.z.record(zod_1.z.string()).optional().describe("Optional headers to include in the request"),
queryParams: zod_1.z
.record(zod_1.z.string())
.optional()
.nullable()
.describe("Query parameters to append to the URL as key-value string pairs. " +
"Use ONLY for GET/DELETE requests. " +
"For POST/PUT/PATCH, you must use the 'body' parameter instead. " +
"Example: {location: 'NYC', units: 'metric'} becomes ?location=NYC&units=metric"),
body: zod_1.z
.any()
.optional()
.describe("Request body - REQUIRED for POST/PUT/PATCH requests when sending data. " +
"Always prefer 'body' over 'queryParams' for POST/PUT/PATCH. " +
"Do NOT use for GET or DELETE, use queryParams instead."),
selectedPaymentOption: PaymentOptionSchema.describe("The payment option to use for this request"),
})
.strip()
.describe("Instructions for retrying a request with x402 payment after receiving a 402 response");
// Schema for direct x402 payment request (with warning)
exports.DirectX402RequestSchema = zod_1.z
.object({
url: zod_1.z
.string()
.url()
.describe("The URL of the API endpoint (can be localhost for development)"),
method: zod_1.z
.enum(["GET", "POST", "PUT", "DELETE", "PATCH"])
.nullable()
.default("GET")
.describe("The HTTP method to use for the request"),
headers: zod_1.z
.record(zod_1.z.string())
.optional()
.nullable()
.describe("Optional headers to include in the request"),
queryParams: zod_1.z
.record(zod_1.z.string())
.optional()
.nullable()
.describe("Query parameters to append to the URL as key-value string pairs. " +
"Use ONLY for GET/DELETE requests. " +
"For POST/PUT/PATCH, use the 'body' parameter instead. " +
"Example: {location: 'NYC', units: 'metric'} becomes ?location=NYC&units=metric"),
body: zod_1.z
.any()
.optional()
.nullable()
.describe("Request body - REQUIRED for POST/PUT/PATCH requests when sending data. " +
"Always prefer 'body' over 'queryParams' for POST/PUT/PATCH. " +
"Do NOT use for GET or DELETE, use queryParams instead."),
})
.strip()
.describe("Instructions for making an HTTP request with automatic x402 payment handling. WARNING: This bypasses user confirmation - only use when explicitly told to skip confirmation!");