import { U as User, A as A2ARequestHandler, S as ServerCallContext } from './a2a_request_handler-B3LxMq3P.cjs'; import { ae as AgentCard } from './extensions-DvruCIzw.cjs'; import { L as Logger } from './logger-DM9C11Ou.cjs'; /** * Web-Standard Types for A2A Server * * These types define the web-standard interfaces used by all framework adapters. * They are based on the Web Fetch API which is available in all modern edge runtimes. */ /** * Web-standard Request type. * Available in browsers, Cloudflare Workers, Deno, Bun, and Node.js 18+. */ type WebRequest = Request; /** * Web-standard Response type. */ type WebResponse = Response; /** * Default user builder that returns an unauthenticated user. */ declare const defaultUserBuilder: UserBuilder; /** * Function to build user information from a request. * This allows framework-agnostic authentication. * Named `UserBuilder` for consistency with Express/Hono APIs. * * @example * ```ts * // JWT authentication * const userBuilder: UserBuilder = async (req) => { * const token = req.headers.get('Authorization')?.replace('Bearer ', ''); * if (!token) return new UnauthenticatedUser(); * const payload = await verifyJWT(token); * return { isAuthenticated: true, userName: payload.sub }; * }; * ``` */ type UserBuilder = (request: WebRequest) => Promise; /** * UserBuilder factory with common authentication patterns. * Matches the Express/Hono API for consistency. */ declare const UserBuilder: { /** * Returns an unauthenticated user for all requests. * Use this when no authentication is required. */ noAuthentication: () => UserBuilder; }; /** * Provider for agent card data. * Can be either: * - An object with a `getAgentCard()` method (like A2ARequestHandler) * - A function that returns a Promise * * This matches the Express/Hono API for consistency. * * @example * ```ts * // Using A2ARequestHandler * createAgentCardHandler({ agentCardProvider: requestHandler }); * * // Using a function * createAgentCardHandler({ agentCardProvider: async () => myAgentCard }); * ``` */ type AgentCardProvider = { getAgentCard(): Promise; } | (() => Promise); /** * Resolves an AgentCardProvider to a function. * Handles both object and function forms. */ declare function resolveAgentCardProvider(provider: AgentCardProvider): () => Promise; /** * Base configuration options for all A2A server implementations. * All framework-specific options should extend this interface. * * This ensures a consistent API across Express, Hono, Elysia, itty-router, Fresh, etc. */ interface A2AServerOptions { /** Logger instance for request/error logging */ logger?: Logger; /** Function to build user from web-standard Request */ userBuilder?: UserBuilder; /** Path for agent card endpoint (default: '/.well-known/agent-card.json') */ agentCardPath?: string; /** Enable REST API endpoints (default: false) */ enableRest?: boolean; /** Base path for REST endpoints (default: '/rest') */ restBasePath?: string; } /** * Configuration options for A2A web-standard handlers. */ interface WebHandlerOptions { /** Logger instance for request/error logging */ logger?: Logger; /** Function to build user from request */ userBuilder?: UserBuilder; /** Base path for API routes (e.g., '/api/a2a') */ basePath?: string; } /** * Resolved configuration with defaults applied. */ interface ResolvedWebHandlerOptions { logger: Logger; userBuilder: UserBuilder; basePath: string; } /** * Applies defaults to web handler options. */ declare function resolveOptions(options?: WebHandlerOptions): ResolvedWebHandlerOptions; /** * Route handler function type. * Takes a web-standard Request and returns a Response. */ type RouteHandler = (request: WebRequest) => Promise; /** * Route definition for the web-standard router. */ interface Route { method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; pattern: string; handler: RouteHandler; } /** * SSE (Server-Sent Events) event structure. * Used internally by the web-standard handlers. */ interface SSEEvent { id?: string; event?: string; data: string; retry?: number; } /** * Formats an SSE event to the wire format. * Supports optional id, event type, and retry fields. */ declare function formatSSE(event: SSEEvent): string; /** * Formats a data event for SSE (convenience wrapper). * Compatible with the shared sse_utils.ts format. */ declare function formatSSEData(data: unknown): string; /** * Formats an error event for SSE. * Uses the "error" event type for client-side error handling. * Compatible with the shared sse_utils.ts format. */ declare function formatSSEError(error: unknown): string; /** * Standard SSE response headers. * Re-exported from shared sse_utils for convenience. */ declare const SSE_HEADERS: HeadersInit; /** * Creates an SSE streaming response from an async generator. * * This implementation provides: * - Proper abort signal handling with event listener cleanup * - Graceful stream termination on client disconnect * - Error event emission before stream close * * For frameworks with native backpressure support (like Hono's streamSSE), * use the `processStream` function with `createHonoStreamConsumer` instead. * * @param generator - Async generator yielding events * @param options - Response options (headers, signal) * @returns Web-standard Response with SSE stream */ declare function createSSEResponse(generator: AsyncGenerator, options?: { headers?: HeadersInit; signal?: AbortSignal; }): WebResponse; /** * Creates a JSON response with the given status code. */ declare function jsonResponse(body: unknown, status: number, headers?: HeadersInit): WebResponse; /** * Creates an empty response (204 No Content). */ declare function noContentResponse(headers?: HeadersInit): WebResponse; /** * Parses JSON from a request body. * Returns null if parsing fails. * * Note: This function uses a type assertion because `request.json()` returns `unknown`. * The caller is responsible for ensuring the parsed data matches type T, typically * through validation in the transport handler layer. * * @typeParam T - Expected type of the parsed JSON (default: unknown) * @param request - Web-standard Request object * @returns Parsed JSON as type T, or null if parsing fails */ declare function parseJsonBody(request: WebRequest): Promise; /** * Extracts path parameters from a URL pattern match. * Simple implementation for common patterns like /tasks/:taskId */ declare function extractPathParams(pattern: string, path: string): Record | null; /** * Generates a UUID v4 using the Web Crypto API. * Works in all modern edge runtimes without external dependencies. */ declare function generateId(): string; /** * Web-Standard Base Handlers for A2A Server * * These handlers implement the A2A protocol using only web-standard APIs. * They can be used directly or wrapped by framework-specific adapters. * * This module uses the shared logic from: * - transports/jsonrpc/json_rpc_logic.ts - JSON-RPC processing * - request_handler/agent_card_utils.ts - Agent card handling * - error.ts - Error formatting * - sse_utils.ts - SSE event formatting * * ## Pluggable Streaming * * The handlers support pluggable streaming via the `StreamingStrategy` interface. * This allows frameworks with native backpressure support (like Hono's streamSSE) * to use their optimized streaming while maintaining a common handler interface. */ /** * Strategy for creating SSE streaming responses. * * This interface allows frameworks to provide their own streaming implementation * with native backpressure support while using the common handler logic. * * @example * ```ts * // Default web-standard strategy (no backpressure) * const defaultStrategy: StreamingStrategy = { * createResponse: (generator, options) => createSSEResponse(generator, options), * }; * * // Hono strategy with native backpressure * const honoStrategy: StreamingStrategy = { * createResponse: (generator, options) => { * return streamSSE(c, async (sseStream) => { * const consumer = createHonoStreamConsumer(sseStream, signal); * // ... process generator with consumer * }); * }, * }; * ``` */ interface StreamingStrategy { /** * Creates an SSE streaming response from an async generator. * * @param generator - Async generator yielding SSE events * @param options - Response options (headers, signal) * @returns Web-standard Response with SSE stream */ createResponse(generator: AsyncGenerator, options?: { headers?: HeadersInit; signal?: AbortSignal; }): WebResponse | Promise; } /** * Options for the agent card handler. */ interface AgentCardHandlerOptions extends WebHandlerOptions { /** * Provider for agent card data. * Can be an A2ARequestHandler, an object with getAgentCard(), or a function. */ agentCardProvider?: AgentCardProvider; } /** * Creates a web-standard handler for the agent card endpoint. * * Uses shared fetchAgentCard() logic from request_handler/agent_card_utils.ts. */ declare function createAgentCardHandler(requestHandler: A2ARequestHandler, options?: AgentCardHandlerOptions): (request: WebRequest) => Promise; /** * Options for the JSON-RPC handler. */ interface JsonRpcHandlerOptions extends WebHandlerOptions { /** * Strategy for creating streaming responses. * Defaults to web-standard ReadableStream (no backpressure). * Use a framework-specific strategy for native backpressure support. */ streamingStrategy?: StreamingStrategy; } /** * Creates a web-standard handler for JSON-RPC requests. * * Uses shared processJsonRpc() logic from transports/jsonrpc/json_rpc_logic.ts. * * @param requestHandler - The A2A request handler * @param options - Handler options including optional streaming strategy */ declare function createJsonRpcHandler(requestHandler: A2ARequestHandler, options?: JsonRpcHandlerOptions): (request: WebRequest) => Promise; /** * REST route definition. */ interface RestRoute { method: 'GET' | 'POST' | 'DELETE'; pattern: string; handler: (request: WebRequest, params: Record, context: ServerCallContext, logger: Logger) => Promise; } /** * Options for the REST handlers. */ interface RestHandlerOptions extends WebHandlerOptions { /** * Strategy for creating streaming responses. * Defaults to web-standard ReadableStream (no backpressure). * Use a framework-specific strategy for native backpressure support. */ streamingStrategy?: StreamingStrategy; } /** * Creates web-standard handlers for REST API endpoints. * * Uses shared error formatting from error.ts. * * @param requestHandler - The A2A request handler * @param options - Handler options including optional streaming strategy */ declare function createRestHandlers(requestHandler: A2ARequestHandler, options?: RestHandlerOptions): { routes: RestRoute[]; handleRequest: (request: WebRequest, pathname: string) => Promise; }; /** * Configuration for the combined A2A handler. */ interface A2AHandlerConfig extends WebHandlerOptions { /** Path for agent card endpoint (default: '/.well-known/agent-card.json') */ agentCardPath?: string; /** Path for JSON-RPC endpoint (default: '/') */ jsonRpcPath?: string; /** Path prefix for REST endpoints (default: '') */ restBasePath?: string; /** * Strategy for creating streaming responses. * Defaults to web-standard ReadableStream (no backpressure). * Use a framework-specific strategy for native backpressure support. */ streamingStrategy?: StreamingStrategy; } /** * Creates a combined handler that routes to agent card, JSON-RPC, or REST handlers. * This is the main entry point for framework adapters. * * @param requestHandler - The A2A request handler * @param config - Handler configuration including optional streaming strategy */ declare function createA2AHandler(requestHandler: A2ARequestHandler, config?: A2AHandlerConfig): (request: WebRequest) => Promise; export { type AgentCardHandlerOptions as A, type JsonRpcHandlerOptions as J, type RestHandlerOptions as R, type SSEEvent as S, UserBuilder as U, type WebRequest as W, createAgentCardHandler as a, createRestHandlers as b, createJsonRpcHandler as c, type AgentCardProvider as d, type WebResponse as e, defaultUserBuilder as f, type A2AServerOptions as g, type WebHandlerOptions as h, type ResolvedWebHandlerOptions as i, resolveOptions as j, type RouteHandler as k, type Route as l, formatSSE as m, formatSSEData as n, formatSSEError as o, createSSEResponse as p, jsonResponse as q, resolveAgentCardProvider as r, noContentResponse as s, parseJsonBody as t, extractPathParams as u, generateId as v, createA2AHandler as w, type A2AHandlerConfig as x, SSE_HEADERS as y };