/** * Request kernel — the one place in the SDK that touches `fetch`. * * Isomorphic: uses only platform globals, no Node-only APIs. The kernel is * safe to execute inside a V8 isolate where `fs`, `child_process`, and * `process` are absent. * * Failure translation: maps HTTP status codes and network errors to the * appropriate {@link Run402Error} subclass. Callers never see `undefined` * or a response-shaped error value — they either get the parsed body as T * or an exception. */ import type { AuthRequestMeta, CredentialsProvider, ProjectKeys } from "./credentials.js"; export interface KernelConfig { apiBase: string; fetch: typeof globalThis.fetch; credentials: CredentialsProvider; clientMetadata?: Run402ClientMetadata | false; /** * Per-client observability accumulator, mutated by every request made * through this kernel config. Set by {@link buildClient} — a fresh * {@link ClientStats} object per `Run402` instance, monotonic (no reset) * for the instance's lifetime. Exposed to callers via `Client.stats()` / * `Run402.stats()`. Not meant to be set directly by SDK consumers. * @internal */ stats?: ClientStats; } export interface Run402ClientMetadata { surface?: string; version?: string; sdkVersion?: string; } export interface RequestOptions { method?: string; headers?: Record; body?: unknown; /** Send body as a raw string (e.g. `text/plain` SQL) or bytes, skipping JSON.stringify. */ rawBody?: string | Uint8Array; /** Include credential headers from `credentials.getAuth(path)`. Default: true. */ withAuth?: boolean; /** Optional write capability + target, passed to `getAuth` for operator-approval matching. */ authMeta?: AuthRequestMeta; /** Short verb phrase attached to thrown errors (e.g. "provisioning project"). */ context: string; /** * Logical retry attempt number, surfaced verbatim in the `RUN402_TRACE` * line's `attempt=` field. Default 1 — the kernel itself never retries * a request; a caller that re-issues the same logical request after a * higher-level retry (e.g. a safe release-race replan) may pass an * incrementing value so the trace reads as one logical operation's history * rather than unrelated single-attempt calls. */ attempt?: number; } /** Cumulative request-kernel observability for one SDK instance (`Run402.stats()`). Monotonic — never resets. */ export interface ClientStats { /** Number of fetch calls made (successful or not). */ round_trips: number; /** Summed wall-clock time spent in fetch + body read, in milliseconds. */ wire_ms: number; /** Summed request body bytes sent, where knowable. */ bytes_up: number; /** Summed response body bytes received (Content-Length when present, measured otherwise). */ bytes_down: number; } /** * What the seller's `PAYMENT-RESPONSE` receipt says actually settled. * * OBSERVED, never inferred. `network` is the chain the payment landed on * according to the settlement receipt — not a guess from local wallet config. * That distinction is the point: a caller must be able to tell a real payment * from a testnet one, and a buyer holding mainnet funds makes any config-derived * guess wrong. */ export interface PaymentSettlement { success: boolean; network: string; transaction: string; payer: string | null; } export interface ResponseEnvelope { status: number; body: T; /** * Present only when the response carried an x402 settlement receipt, i.e. * this request actually moved money. Absent/`null` means no payment was made * on this request — NOT that a payment failed. */ settlement?: PaymentSettlement | null; } /** * Decode the `PAYMENT-RESPONSE` receipt if the response carries one. * * Deliberately total: a malformed or unexpected receipt yields `null` rather * than throwing. A caller asking "what did I just pay?" must never have its * SUCCESSFUL response turned into an error by a reporting concern. * * Strict receipt VALIDATION — matching the receipt against the challenge the * buyer accepted — belongs to the paid-fetch buyer path, which does it. This is * disclosure of what the seller reported, and is labelled as such. */ export declare function decodeSettlementReceipt(res: { headers: { get(name: string): string | null; }; }): PaymentSettlement | null; /** Internal client surface passed to each namespace. */ export interface Client { /** API base URL, e.g. `https://api.run402.com`. Exposed for namespaces that need to compute derived URLs (e.g. REST endpoints). */ readonly apiBase: string; request(path: string, opts: RequestOptions): Promise; requestWithResponse(path: string, opts: RequestOptions): Promise>; getProjectCredentials(id: string): Promise; /** @deprecated Use getProjectCredentials. */ getProject(id: string): Promise; /** The underlying credentials provider. Namespaces use this to access optional methods (saveProject, setActiveProject, ...). */ readonly credentials: CredentialsProvider; /** * The injected fetch (or default `globalThis.fetch`). Namespaces use this * when they need to hit a non-gateway URL — e.g. an S3 presigned URL from * a multipart upload, where auth + apiBase injection would be wrong. */ readonly fetch: typeof globalThis.fetch; /** Cumulative observability for this client instance. See {@link ClientStats}. */ stats(): ClientStats; } export declare function request(kernel: KernelConfig, path: string, opts: RequestOptions): Promise; export declare function requestWithResponse(kernel: KernelConfig, path: string, opts: RequestOptions): Promise>; export declare function buildClient(kernel: KernelConfig): Client; export declare function clientMetadataHeaders(metadata: KernelConfig["clientMetadata"]): Record; //# sourceMappingURL=kernel.d.ts.map