import type { AgentCard, Message, MessageSendParams, Task, TaskArtifactUpdateEvent, TaskIdParams, TaskStatusUpdateEvent } from '@a2a-js/sdk'; import type { ExecutionEventBusManager, TaskStore } from '@a2a-js/sdk/server'; import { AgentExecutor, DefaultRequestHandler, ExecutionEventQueue, ResultManager } from '@a2a-js/sdk/server'; import { StartAgentRequest } from '../common/types.js'; import type { A2AStreamEvent, HttpRequestContext } from './types.js'; /** * Options for configuring the PaymentsRequestHandler */ export interface PaymentsRequestHandlerOptions { /** Whether to execute tasks asynchronously */ asyncExecution?: boolean; /** Default batch mode for all requests (can be overridden per-request) */ defaultBatch?: boolean; /** Default margin percentage for all requests (can be overridden per-request) */ defaultMarginPercent?: number; } /** * PaymentsRequestHandler extends DefaultRequestHandler to add payments validation and burning. * It validates credits before executing a task and burns credits after successful execution. * It also sends push notifications when a task reaches a terminal state. * @param options - Handler options, including asyncExecution, defaultBatch, and defaultMarginPercent */ export declare class PaymentsRequestHandler extends DefaultRequestHandler { private paymentsService; private httpContextByTaskId; private httpContextByMessageId; private asyncExecution; private defaultBatch; private defaultMarginPercent?; /** * Store HTTP context temporarily by messageId (used in middleware when taskId is not yet available). * @param messageId - The messageId from the incoming message * @param ctx - The HTTP context (bearerToken, url, method) */ setHttpRequestContextForMessage(messageId: string, ctx: HttpRequestContext): void; /** * Store HTTP context by taskId (used in middleware when taskId is available). * @param taskId - The taskId of the task * @param ctx - The HTTP context (bearerToken, url, method) */ setHttpRequestContextForTask(taskId: string, ctx: HttpRequestContext): void; /** * @param agentCard - The agent card * @param taskStore - The task store * @param agentExecutor - The business logic executor * @param paymentsService - The payments service for validation and burning * @param eventBusManager - The event bus manager (optional) * @param options - Handler options (asyncExecution, defaultBatch, defaultMarginPercent) */ constructor(agentCard: AgentCard, taskStore: TaskStore, agentExecutor: AgentExecutor, paymentsService: any, eventBusManager?: ExecutionEventBusManager, options?: PaymentsRequestHandlerOptions); /** * Retrieve the HTTP context for a given taskId. * @param taskId - The taskId of the task * @returns The HTTP context (bearerToken, url, method) or undefined */ private getHttpRequestContextForTask; /** * Retrieve the HTTP context for a given messageId. * @param messageId - The messageId of the message * @returns The HTTP context (bearerToken, url, method) or undefined */ private getHttpRequestContextForMessage; /** * Deletes the HTTP context associated with a taskId. * @param taskId - The taskId to delete context for */ deleteHttpRequestContextForTask(taskId: string): void; /** * Get the handler options (defaultBatch, defaultMarginPercent). * Used by middleware to determine default redemption behavior. * @returns The handler options */ getHandlerOptions(): PaymentsRequestHandlerOptions; /** * Validates a request using the x402 payments service. * This method is used by the middleware to validate credits before processing requests. * * @param bearerToken - The bearer token for authentication * @param endpoint - Optional endpoint URL being requested * @param httpVerb - Optional HTTP method being used * @returns Promise resolving to the validation result */ validateRequest(bearerToken: string, endpoint?: string, httpVerb?: string): Promise; /** * Gets redemption configuration for a task based on AgentCard and handler defaults. * The configuration is determined by the server, not by client metadata. * * @returns The redemption configuration */ private getRedemptionConfig; /** * Determines the appropriate redemption method based on server configuration. * * @param bearerToken - The bearer token for authentication * @param creditsUsed - The number of credits to burn * @param httpContext - Optional HTTP context with endpoint and method information * @returns Promise resolving to the redemption result */ private executeRedemption; /** * Creates PaymentsRequestContext from message parameters. * This method handles HTTP context retrieval, validation, and context creation. * @param params - Message send parameters * @param isStreaming - Whether this is for streaming (affects createRequestContext call) * @returns Object containing PaymentsRequestContext and related data */ private createPaymentsRequestContext; /** * x402 v2 A2A transport: if the request is payment-gated and arrived with no * token (the middleware stored a `paymentRequired` on the HTTP context), build * and return the spec-shaped `input-required` task carrying the * X402PaymentRequired object under `x402.payment.required` — WITHOUT executing * the agent. Returns `undefined` when a token was present (normal flow). * * @param params - The incoming message send parameters * @returns The `input-required` Task to return to the client, or `undefined` */ private buildPaymentRequiredTaskIfNeeded; /** * Stamp x402 settlement state onto the final task's metadata, in band, per the * x402 v2 A2A transport. Only applied when the token arrived in band (so the * legacy `payment-signature` header path is unchanged). On success the task * carries `x402.payment.status: payment-completed` + `x402.payment.receipts`; * on failure `payment-failed` + `x402.payment.error` + receipts. * * @param task - The current task (mutated in place) * @param httpContext - The request's HTTP context * @param settlement - The settlement result, or undefined when none ran */ private recordInBandSettlement; /** * Processes streaming events with finalization (credits burning and push notifications). * Similar to processEventsWithFinalization but yields events for streaming. * @param taskId - The task ID * @param resultManager - The result manager * @param eventQueue - The event queue * @param bearerToken - The bearer token * @param validation - The validation result * @returns Async generator yielding processed events */ protected processStreamingEventsWithFinalization(taskId: string, resultManager: ResultManager, eventQueue: ExecutionEventQueue, bearerToken: string, requestHttpContext?: HttpRequestContext): AsyncGenerator; /** * Processes all events, calling handleTaskFinalization when a terminal status-update event is received. * In async mode, it can be launched in background. */ protected processEventsWithFinalization(taskId: string, resultManager: ResultManager, eventQueue: ExecutionEventQueue, bearerToken: string, validation: StartAgentRequest, options?: { firstResultResolver?: (event: any) => void; firstResultRejector?: (err: any) => void; }, requestHttpContext?: HttpRequestContext): Promise; /** * Sends a message, validating credits before execution and burning credits after. * Also sends a push notification if the task reaches a terminal state. * This method overrides the parent implementation to allow eventBus subscription before agent execution. * @param params - Message send parameters * @returns The resulting message or task */ sendMessage(params: MessageSendParams): Promise; /** * Handles credits burning and push notification when a task reaches a terminal state. * This is called asynchronously from the eventBus listener. * Supports batch and margin-based redemptions based on server configuration. * @param resultManager - The result manager * @param event - The status-update event with final state * @param bearerToken - The bearer token for payment validation * @param validation - The validation result from the request */ private handleTaskFinalization; /** * Streams messages and events for a task, with payments validation. * Also sends a push notification if a terminal status-update event is emitted. * @param params - Message send parameters * @returns Async generator of events */ sendMessageStream(params: MessageSendParams): AsyncGenerator; /** * Sends a push notification when a task reaches a terminal state. * @param taskId - The task ID * @param state - The terminal state * @param pushNotificationConfig - The push notification configuration * @param payload - Additional payload to include in the notification */ private sendPushNotification; /** * Migrates the HTTP context from a messageId to a taskId and deletes the temporary messageId context. * @param messageId - The messageId to migrate from * @param taskId - The taskId to migrate to */ migrateHttpRequestContextFromMessageToTask(messageId: string, taskId: string): void; /** * Deletes the HTTP context associated with a messageId. * @param messageId - The messageId to delete context for */ deleteHttpRequestContextForMessage(messageId: string): void; /** * Protected getter to access the private taskStore property from the parent class. * This is a workaround due to SDK limitations. */ protected getTaskStore(): TaskStore; /** * Protected getter to access the private eventBusManager property from the parent class. * This is a workaround due to SDK limitations. */ protected getEventBusManager(): ExecutionEventBusManager; /** * Protected getter to access the private _createRequestContext method from the parent class. * This is a workaround due to SDK limitations. */ protected callCreateRequestContext(incomingMessage: any, taskId: string, isStream: boolean): Promise; /** * Protected getter to access the private _processEvents method from the parent class. * This is a workaround due to SDK limitations. */ protected callProcessEvents(...args: any[]): any; /** * Resubscribes to a task's event stream, ensuring the task has updated metadata before yielding. * This method overrides the parent implementation to ensure metadata is updated before yielding. * @param params - Parameters containing the taskId * @returns Async generator of events */ resubscribe(params: TaskIdParams): AsyncGenerator; } //# sourceMappingURL=paymentsRequestHandler.d.ts.map