@mastra/core
Version:
The core foundation of the Mastra framework, providing essential components and interfaces for building AI-powered applications.
1 lines • 29.6 kB
Source Map (JSON)
{"version":3,"sources":["../../src/a2a/types.ts","../../src/a2a/error.ts"],"names":[],"mappings":";;;AA2iBO,IAAM,mBAAA,GAAsB;AAG5B,IAAM,uBAAA,GAA0B;AAGhC,IAAM,uBAAA,GAA0B;AAGhC,IAAM,sBAAA,GAAyB;AAG/B,IAAM,sBAAA,GAAyB;AAI/B,IAAM,qBAAA,GAAwB;AAG9B,IAAM,0BAAA,GAA6B;AAGnC,IAAM,qCAAA,GAAwC;AAG9C,IAAM,6BAAA,GAAgC;;;ACpjBtC,IAAM,QAAA,GAAN,MAAM,SAAA,SAAiB,KAAA,CAAM;AAAA,EAC3B,IAAA;AAAA,EACA,IAAA;AAAA,EACA,MAAA;AAAA;AAAA,EAEP,WAAA,CAAY,IAAA,EAA+B,OAAA,EAAiB,IAAA,EAAgB,MAAA,EAAiB;AAC3F,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA,EAKA,cAAA,GAAwC;AACtC,IAAA,MAAM,WAAA,GAAqC;AAAA,MACzC,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,SAAS,IAAA,CAAK;AAAA,KAChB;AACA,IAAA,IAAI,IAAA,CAAK,SAAS,MAAA,EAAW;AAC3B,MAAA,WAAA,CAAY,OAAO,IAAA,CAAK,IAAA;AAAA,IAC1B;AACA,IAAA,OAAO,WAAA;AAAA,EACT;AAAA;AAAA,EAIA,OAAO,UAAA,CAAW,OAAA,EAAiB,IAAA,EAA0B;AAC3D,IAAA,OAAO,IAAI,SAAA,CAAS,mBAAA,EAAqB,OAAA,EAAS,IAAI,CAAA;AAAA,EACxD;AAAA,EAEA,OAAO,cAAA,CAAe,OAAA,EAAiB,IAAA,EAA0B;AAC/D,IAAA,OAAO,IAAI,SAAA,CAAS,uBAAA,EAAyB,OAAA,EAAS,IAAI,CAAA;AAAA,EAC5D;AAAA,EAEA,OAAO,eAAe,MAAA,EAA0B;AAC9C,IAAA,OAAO,IAAI,SAAA,CAAS,uBAAA,EAAyB,CAAA,kBAAA,EAAqB,MAAM,CAAA,CAAE,CAAA;AAAA,EAC5E;AAAA,EAEA,OAAO,aAAA,CAAc,OAAA,EAAiB,IAAA,EAA0B;AAC9D,IAAA,OAAO,IAAI,SAAA,CAAS,sBAAA,EAAwB,OAAA,EAAS,IAAI,CAAA;AAAA,EAC3D;AAAA,EAEA,OAAO,aAAA,CAAc,OAAA,EAAiB,IAAA,EAA0B;AAC9D,IAAA,OAAO,IAAI,SAAA,CAAS,sBAAA,EAAwB,OAAA,EAAS,IAAI,CAAA;AAAA,EAC3D;AAAA,EAEA,OAAO,aAAa,MAAA,EAA0B;AAC5C,IAAA,OAAO,IAAI,SAAA,CAAS,qBAAA,EAAuB,mBAAmB,MAAM,CAAA,CAAA,EAAI,QAAW,MAAM,CAAA;AAAA,EAC3F;AAAA,EAEA,OAAO,kBAAkB,MAAA,EAA0B;AACjD,IAAA,OAAO,IAAI,SAAA,CAAS,0BAAA,EAA4B,wBAAwB,MAAM,CAAA,CAAA,EAAI,QAAW,MAAM,CAAA;AAAA,EACrG;AAAA,EAEA,OAAO,4BAAA,GAAyC;AAC9C,IAAA,OAAO,IAAI,SAAA,CAAS,qCAAA,EAAuC,oCAAoC,CAAA;AAAA,EACjG;AAAA,EAEA,OAAO,qBAAqB,SAAA,EAA6B;AACvD,IAAA,OAAO,IAAI,SAAA,CAAS,6BAAA,EAA+B,CAAA,uBAAA,EAA0B,SAAS,CAAA,CAAE,CAAA;AAAA,EAC1F;AACF","file":"index.cjs","sourcesContent":["// === JSON-RPC Base Structures\n\n/**\n * Base interface for identifying JSON-RPC messages.\n */\nexport interface JSONRPCMessageIdentifier {\n /**\n * Request identifier. Can be a string, number, or null.\n * Responses must have the same ID as the request they relate to.\n * Notifications (requests without an expected response) should omit the ID or use null.\n */\n id?: number | string | null;\n}\n\n/**\n * Base interface for all JSON-RPC messages (Requests and Responses).\n */\nexport interface JSONRPCMessage extends JSONRPCMessageIdentifier {\n /**\n * Specifies the JSON-RPC version. Must be \"2.0\".\n * @default \"2.0\"\n * @const \"2.0\"\n */\n jsonrpc?: '2.0';\n}\n\n/**\n * Represents a JSON-RPC request object base structure.\n * Specific request types should extend this.\n */\nexport interface JSONRPCRequest extends JSONRPCMessage {\n /**\n * The name of the method to be invoked.\n */\n method: string;\n\n /**\n * Parameters for the method. Can be a structured object, an array, or null/omitted.\n * Specific request interfaces will define the exact type.\n * @default null\n */\n params?: unknown; // Base type; specific requests will override\n}\n\n/**\n * Represents a JSON-RPC error object.\n */\nexport interface JSONRPCError<Data = unknown | null, Code = number> {\n /**\n * A number indicating the error type that occurred.\n */\n code: Code;\n\n /**\n * A string providing a short description of the error.\n */\n message: string;\n\n /**\n * Optional additional data about the error.\n * @default null\n */\n data?: Data;\n}\n\n/**\n * Represents a JSON-RPC response object.\n */\nexport interface JSONRPCResponse<R = unknown | null, E = unknown | null> extends JSONRPCMessage {\n /**\n * The result of the method invocation. Required on success.\n * Should be null or omitted if an error occurred.\n * @default null\n */\n result?: R;\n\n /**\n * An error object if an error occurred during the request. Required on failure.\n * Should be null or omitted if the request was successful.\n * @default null\n */\n error?: JSONRPCError<E> | null;\n}\n\n// === Core A2A Data Structures\n\n/**\n * Represents the state of a task within the A2A protocol.\n * @description An enumeration.\n */\nexport type TaskState = 'submitted' | 'working' | 'input-required' | 'completed' | 'canceled' | 'failed' | 'unknown';\n\n/**\n * Defines the authentication schemes and credentials for an agent.\n */\nexport interface AgentAuthentication {\n /**\n * List of supported authentication schemes.\n */\n schemes: string[];\n\n /**\n * Credentials for authentication. Can be a string (e.g., token) or null if not required initially.\n * @default null\n */\n credentials?: string | null;\n}\n\n/**\n * Describes the capabilities of an agent.\n */\nexport interface AgentCapabilities {\n /**\n * Indicates if the agent supports streaming responses.\n * @default false\n */\n streaming?: boolean;\n\n /**\n * Indicates if the agent supports push notification mechanisms.\n * @default false\n */\n pushNotifications?: boolean;\n\n /**\n * Indicates if the agent supports providing state transition history.\n * @default false\n */\n stateTransitionHistory?: boolean;\n}\n\n/**\n * Represents the provider or organization behind an agent.\n */\nexport interface AgentProvider {\n /**\n * The name of the organization providing the agent.\n */\n organization: string;\n\n /**\n * URL associated with the agent provider.\n * @default null\n */\n url?: string | null;\n}\n\n/**\n * Defines a specific skill or capability offered by an agent.\n */\nexport interface AgentSkill {\n /**\n * Unique identifier for the skill.\n */\n id: string;\n\n /**\n * Human-readable name of the skill.\n */\n name: string;\n\n /**\n * Optional description of the skill.\n * @default null\n */\n description?: string | null;\n\n /**\n * Optional list of tags associated with the skill for categorization.\n * @default null\n */\n tags?: string[] | null;\n\n /**\n * Optional list of example inputs or use cases for the skill.\n * @default null\n */\n examples?: string[] | null;\n\n /**\n * Optional list of input modes supported by this skill, overriding agent defaults.\n * @default null\n */\n inputModes?: string[] | null;\n\n /**\n * Optional list of output modes supported by this skill, overriding agent defaults.\n * @default null\n */\n outputModes?: string[] | null;\n}\n\n/**\n * Represents the metadata card for an agent, describing its properties and capabilities.\n */\nexport interface AgentCard {\n /**\n * The name of the agent.\n */\n name: string;\n\n /**\n * An optional description of the agent.\n * @default null\n */\n description?: string | null;\n\n /**\n * The base URL endpoint for interacting with the agent.\n */\n url: string;\n\n /**\n * Information about the provider of the agent.\n * @default null\n */\n provider?: AgentProvider | null;\n\n /**\n * The version identifier for the agent or its API.\n */\n version: string;\n\n /**\n * An optional URL pointing to the agent's documentation.\n * @default null\n */\n documentationUrl?: string | null;\n\n /**\n * The capabilities supported by the agent.\n */\n capabilities: AgentCapabilities;\n\n /**\n * Authentication details required to interact with the agent.\n * @default null\n */\n authentication?: AgentAuthentication | null;\n\n /**\n * Default input modes supported by the agent (e.g., 'text', 'file', 'json').\n * @default [\"text\"]\n */\n defaultInputModes?: string[];\n\n /**\n * Default output modes supported by the agent (e.g., 'text', 'file', 'json').\n * @default [\"text\"]\n */\n defaultOutputModes?: string[];\n\n /**\n * List of specific skills offered by the agent.\n */\n skills: AgentSkill[];\n}\n\nexport interface FileContentBase {\n /**\n * Optional name of the file.\n * @default null\n */\n name?: string | null;\n\n /**\n * Optional MIME type of the file content.\n * @default null\n */\n mimeType?: string | null;\n\n /**\n * File content encoded as a Base64 string. Use this OR `uri`.\n */\n bytes?: string | null;\n\n /**\n * URI pointing to the file content. Use this OR `bytes`.\n */\n uri?: string | null;\n}\n\nexport type FileContentBytes = FileContentBase & {\n /* File content encoded as a Base64 string. Use this OR `uri`. */\n bytes: string;\n uri?: never;\n};\n\nexport type FileContentUri = FileContentBase & {\n /** URI pointing to the file content. */\n uri: string;\n bytes?: never;\n};\n\n/**\n * Represents the content of a file, either as base64 encoded bytes or a URI.\n * @description Ensures that either 'bytes' or 'uri' is provided, but not both. (Note: This constraint is informational in TypeScript types).\n */\nexport type FileContent = FileContentBytes | FileContentUri;\n\n/**\n * Represents a part of a message containing text content.\n */\nexport interface TextPart {\n type: 'text';\n\n /**\n * The text content.\n */\n text: string;\n\n /**\n * Optional metadata associated with this text part.\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents a part of a message containing file content.\n */\nexport interface FilePart {\n /**\n * Type identifier for this part.\n */\n type: 'file';\n\n /**\n * The file content, provided either inline or via URI.\n */\n file: FileContent;\n\n /**\n * Optional metadata associated with this file part.\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents a part of a message containing structured data (JSON).\n */\nexport interface DataPart {\n /**\n * Type identifier for this part.\n */\n type: 'data';\n\n /**\n * The structured data content as a JSON object.\n */\n data: Record<string, unknown>;\n\n /**\n * Optional metadata associated with this data part.\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents a single part of a multi-part message. Can be text, file, or data.\n */\nexport type Part = TextPart | FilePart | DataPart;\n\n/**\n * Represents an artifact generated or used by a task, potentially composed of multiple parts.\n */\nexport interface Artifact {\n /**\n * Optional name for the artifact.\n * @default null\n */\n name?: string | null;\n\n /**\n * Optional description of the artifact.\n * @default null\n */\n description?: string | null;\n\n /**\n * The constituent parts of the artifact.\n */\n parts: Part[];\n\n /**\n * Optional index for ordering artifacts, especially relevant in streaming or updates.\n * @default 0\n */\n index?: number;\n\n /**\n * Optional flag indicating if this artifact content should append to previous content (for streaming).\n * @default null\n */\n append?: boolean | null;\n\n /**\n * Optional metadata associated with the artifact.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n\n /**\n * Optional flag indicating if this is the last chunk of data for this artifact (for streaming).\n * @default null\n */\n lastChunk?: boolean | null;\n}\n\n/**\n * Represents a message exchanged between a user and an agent.\n */\nexport interface Message {\n /**\n * The role of the sender (user or agent).\n */\n role: 'user' | 'agent';\n\n /**\n * The content of the message, composed of one or more parts.\n */\n parts: Part[];\n\n /**\n * Optional metadata associated with the message.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents the status of a task at a specific point in time.\n */\nexport interface TaskStatus {\n /**\n * The current state of the task.\n */\n state: TaskState;\n\n /**\n * An optional message associated with the current status (e.g., progress update, final response).\n * @default null\n */\n message?: Message | null;\n\n /**\n * The timestamp when this status was recorded (ISO 8601 format).\n * @format date-time\n */\n timestamp?: string;\n}\n\n/**\n * Represents a task being processed by an agent.\n */\nexport interface Task {\n /**\n * Unique identifier for the task.\n */\n id: string;\n\n /**\n * Optional identifier for the session this task belongs to.\n * @default null\n */\n sessionId?: string | null;\n\n /**\n * The current status of the task.\n */\n status: TaskStatus;\n\n /**\n * Optional list of artifacts associated with the task (e.g., outputs, intermediate files).\n * @default null\n */\n artifacts?: Artifact[] | null;\n\n /**\n * Optional metadata associated with the task.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents the history of messages exchanged within a task's session.\n */\nexport interface TaskHistory {\n /**\n * List of messages in chronological order.\n * @default []\n */\n messageHistory?: Message[];\n}\n\n/**\n * Represents a status update event for a task, typically used in streaming scenarios.\n */\nexport interface TaskStatusUpdateEvent {\n /**\n * The ID of the task being updated.\n */\n id: string;\n\n /**\n * The new status of the task.\n */\n status: TaskStatus;\n\n /**\n * Flag indicating if this is the final update for the task.\n * @default false\n */\n final?: boolean;\n\n /**\n * Optional metadata associated with this update event.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Represents an artifact update event for a task, typically used in streaming scenarios.\n */\nexport interface TaskArtifactUpdateEvent {\n /**\n * The ID of the task being updated.\n */\n id: string;\n\n /**\n * The new or updated artifact for the task.\n */\n artifact: Artifact;\n\n /**\n * Flag indicating if this is the final update for the task.\n * @default false\n */\n final?: boolean;\n\n /**\n * Optional metadata associated with this update event.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n// Alias for backward compatibility\nexport type TaskUpdateEvent = TaskStatusUpdateEvent;\n\n// === Error Types (Standard and A2A)\n\n/** Error code for JSON Parse Error (-32700). Invalid JSON was received by the server. */\nexport const ErrorCodeParseError = -32700;\nexport type ErrorCodeParseError = typeof ErrorCodeParseError;\n/** Error code for Invalid Request (-32600). The JSON sent is not a valid Request object. */\nexport const ErrorCodeInvalidRequest = -32600;\nexport type ErrorCodeInvalidRequest = typeof ErrorCodeInvalidRequest;\n/** Error code for Method Not Found (-32601). The method does not exist / is not available. */\nexport const ErrorCodeMethodNotFound = -32601;\nexport type ErrorCodeMethodNotFound = typeof ErrorCodeMethodNotFound;\n/** Error code for Invalid Params (-32602). Invalid method parameter(s). */\nexport const ErrorCodeInvalidParams = -32602;\nexport type ErrorCodeInvalidParams = typeof ErrorCodeInvalidParams;\n/** Error code for Internal Error (-32603). Internal JSON-RPC error. */\nexport const ErrorCodeInternalError = -32603;\nexport type ErrorCodeInternalError = typeof ErrorCodeInternalError;\n\n/** Error code for Task Not Found (-32001). The specified task was not found. */\nexport const ErrorCodeTaskNotFound = -32001;\nexport type ErrorCodeTaskNotFound = typeof ErrorCodeTaskNotFound;\n/** Error code for Task Not Cancelable (-32002). The specified task cannot be canceled. */\nexport const ErrorCodeTaskNotCancelable = -32002;\nexport type ErrorCodeTaskNotCancelable = typeof ErrorCodeTaskNotCancelable;\n/** Error code for Push Notification Not Supported (-32003). Push Notifications are not supported for this operation or agent. */\nexport const ErrorCodePushNotificationNotSupported = -32003;\nexport type ErrorCodePushNotificationNotSupported = typeof ErrorCodePushNotificationNotSupported;\n/** Error code for Unsupported Operation (-32004). The requested operation is not supported by the agent. */\nexport const ErrorCodeUnsupportedOperation = -32004;\nexport type ErrorCodeUnsupportedOperation = typeof ErrorCodeUnsupportedOperation;\n\n/**\n * Union of all well-known A2A and standard JSON-RPC error codes defined in this schema.\n * Use this type for checking against specific error codes. A server might theoretically\n * use other codes within the valid JSON-RPC ranges.\n */\nexport type KnownErrorCode =\n | typeof ErrorCodeParseError\n | typeof ErrorCodeInvalidRequest\n | typeof ErrorCodeMethodNotFound\n | typeof ErrorCodeInvalidParams\n | typeof ErrorCodeInternalError\n | typeof ErrorCodeTaskNotFound\n | typeof ErrorCodeTaskNotCancelable\n | typeof ErrorCodePushNotificationNotSupported\n | typeof ErrorCodeUnsupportedOperation;\n\ntype A2AError = JSONRPCError<unknown | null, KnownErrorCode | number>;\n\n// === Push Notifications and Authentication Info\n\n/**\n * Authentication information, potentially including additional properties beyond the standard ones.\n * (Note: Schema allows additional properties).\n */\nexport interface AuthenticationInfo extends AgentAuthentication {\n /** Allow any other properties */\n [key: string]: any;\n}\n\n/**\n * Information required for setting up push notifications.\n */\nexport interface PushNotificationConfig {\n /**\n * The URL endpoint where the agent should send notifications.\n */\n url: string;\n\n /**\n * A token to be included in push notification requests for verification/authentication.\n */\n token?: string;\n\n /**\n * Optional authentication details needed by the agent to call the notification URL.\n * @default null\n */\n authentication?: AuthenticationInfo | null;\n}\n\n/**\n * Represents the push notification information associated with a specific task ID.\n * Used as parameters for `tasks/pushNotification/set` and as a result type.\n */\nexport interface TaskPushNotificationConfig {\n /**\n * The ID of the task the notification config is associated with.\n */\n id: string;\n /**\n * The push notification configuration details.\n */\n pushNotificationConfig: PushNotificationConfig;\n}\n\n// ================================================================= A2A Request Parameter Types\n// =================================================================\n\n/**\n * Parameters for the `tasks/send` method.\n */\nexport interface TaskSendParams {\n /**\n * Unique identifier for the task being initiated or continued.\n */\n id: string;\n\n /**\n * Optional identifier for the session this task belongs to. If not provided, a new session might be implicitly created depending on the agent.\n */\n sessionId?: string;\n\n /**\n * The message content to send to the agent for processing.\n */\n message: Message;\n\n /**\n * Optional pushNotification information for receiving notifications about this task. Requires agent capability.\n * @default null\n */\n pushNotification?: PushNotificationConfig | null;\n\n /**\n * Optional parameter to specify how much message history to include in the response.\n * @default null\n */\n historyLength?: number | null;\n\n /**\n * Optional metadata associated with sending this message.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Basic parameters used for task ID operations.\n * Used by: `tasks/cancel`, `tasks/pushNotification/get`.\n */\nexport interface TaskIdParams {\n /**\n * The unique identifier of the task.\n */\n id: string;\n\n /**\n * Optional metadata to include with the operation.\n * @default null\n */\n metadata?: Record<string, unknown> | null;\n}\n\n/**\n * Parameters used for querying task-related information by ID.\n * Used by: `tasks/get`, `tasks/getHistory`, `tasks/subscribe`, `tasks/resubscribe`.\n */\nexport interface TaskQueryParams extends TaskIdParams {\n /**\n * Optional history length to retrieve for the task.\n * @default null\n */\n historyLength?: number | null;\n}\n\n// === A2A Request Interfaces\n\n/**\n * Request to send a message/initiate a task.\n */\nexport interface SendTaskRequest extends JSONRPCRequest {\n /**\n * Method name for sending a task message.\n */\n method: 'tasks/send';\n /**\n * Parameters for the send task method.\n */\n params: TaskSendParams;\n}\n\n/**\n * Request to retrieve the current state of a task.\n */\nexport interface GetTaskRequest extends JSONRPCRequest {\n /**\n * Method name for getting task status.\n */\n method: 'tasks/get';\n /**\n * Parameters for the get task method.\n */\n params: TaskQueryParams;\n}\n\n/**\n * Request to cancel a currently running task.\n */\nexport interface CancelTaskRequest extends JSONRPCRequest {\n /**\n * Method name for canceling a task.\n */\n method: 'tasks/cancel';\n /**\n * Parameters for the cancel task method.\n */\n params: TaskIdParams;\n}\n\n/**\n * Request to set or update the push notification config for a task.\n */\nexport interface SetTaskPushNotificationRequest extends JSONRPCRequest {\n /**\n * Method name for setting a task notifications.\n */\n method: 'tasks/pushNotification/set';\n /**\n * Parameters for the set task push notification method.\n */\n params: TaskPushNotificationConfig; // Uses TaskPushNotificationConfig directly as params\n}\n\n/**\n * Request to retrieve the currently configured push notification configuration for a task.\n */\nexport interface GetTaskPushNotificationRequest extends JSONRPCRequest {\n /**\n * Method name for getting task notification configuration.\n */\n method: 'tasks/pushNotification/get';\n /**\n * Parameters for the get task push notification config method.\n */\n params: TaskIdParams;\n}\n\n/**\n * Request to resubscribe to updates for a task after a connection interruption.\n */\nexport interface TaskResubscriptionRequest extends JSONRPCRequest {\n /**\n * Method name for resubscribing to task updates.\n */\n method: 'tasks/resubscribe';\n /**\n * Parameters for the task resubscription method.\n */\n params: TaskQueryParams;\n}\n\n/**\n * Request to send a message/initiate a task and subscribe to streaming updates.\n */\nexport interface SendTaskStreamingRequest extends JSONRPCRequest {\n /**\n * Method name for sending a task message and subscribing to updates.\n */\n method: 'tasks/sendSubscribe';\n /**\n * Parameters for the streaming task send method.\n */\n params: TaskSendParams;\n}\n\n// === A2A Response Interfaces\n\n/**\n * Response to a `tasks/send` request.\n * Contains the Task object or an error.\n */\nexport type SendTaskResponse = JSONRPCResponse<Task | null, A2AError>;\n\n/**\n * Response to a streaming task operation, either through `tasks/sendSubscribe` or a subscription.\n * Contains a TaskStatusUpdateEvent, TaskArtifactUpdateEvent, or an error.\n */\nexport type SendTaskStreamingResponse = JSONRPCResponse<\n TaskStatusUpdateEvent | TaskArtifactUpdateEvent | null,\n A2AError\n>;\n\n/**\n * Response to a `tasks/get` request. Contains the Task object or an error.\n */\nexport type GetTaskResponse = JSONRPCResponse<Task | null, A2AError>;\n\n/**\n * Response to a `tasks/cancel` request. Contains the updated Task object (usually with 'canceled' state) or an error.\n */\nexport type CancelTaskResponse = JSONRPCResponse<Task | null, A2AError>;\n\n/**\n * Response to a `tasks/getHistory` request. Contains the TaskHistory object or an error.\n */\nexport type GetTaskHistoryResponse = JSONRPCResponse<TaskHistory | null, A2AError>;\n\n/**\n * Response to a `tasks/pushNotification/set` request. Contains the confirmed TaskPushNotificationConfig or an error.\n */\nexport type SetTaskPushNotificationResponse = JSONRPCResponse<TaskPushNotificationConfig | null, A2AError>;\n\n/**\n * Response to a `tasks/pushNotification/get` request. Contains the TaskPushNotificationConfig or an error.\n */\nexport type GetTaskPushNotificationResponse = JSONRPCResponse<TaskPushNotificationConfig | null, A2AError>;\n\n// Note: The response to TaskSubscriptionRequest is typically handled by the underlying protocol\n// (like WebSocket messages containing TaskUpdateEvent) rather than a single JSON-RPC response object.\n// The schema doesn't define a specific JSON-RPC response type for `tasks/subscribe`.\n\n// === Union Types for A2A Requests/Responses\n\n/**\n * Represents any valid request defined in the A2A protocol.\n */\nexport type A2ARequest =\n | SendTaskRequest\n | GetTaskRequest\n | CancelTaskRequest\n // | GetTaskHistoryRequest // Removed as it's not in the latest spec\n | SetTaskPushNotificationRequest\n | GetTaskPushNotificationRequest\n // | TaskSubscriptionRequest // Removed as it's replaced by sendSubscribe or resubscribe\n | TaskResubscriptionRequest\n | SendTaskStreamingRequest;\n\n/**\n * Represents any valid JSON-RPC response defined in the A2A protocol.\n * (This is a helper type, not explicitly defined with `oneOf` in the schema like A2ARequest, but useful).\n */\nexport type A2AResponse =\n | SendTaskResponse\n | GetTaskResponse\n | CancelTaskResponse\n | GetTaskHistoryResponse\n | SetTaskPushNotificationResponse\n | GetTaskPushNotificationResponse;\n// Subscription responses are typically event streams (TaskUpdateEvent) sent over the transport,\n// not direct JSON-RPC responses to the subscribe request itself.\n\n// Helper type for the simplified store\nexport interface TaskAndHistory {\n task: Task;\n history: Message[];\n}\n\nexport interface TaskContext {\n /**\n * The current state of the task when the handler is invoked or resumed.\n * Note: This is a snapshot. For the absolute latest state during async operations,\n * the handler might need to reload the task via the store.\n */\n task: Task;\n\n /**\n * The specific user message that triggered this handler invocation or resumption.\n */\n userMessage: Message;\n\n /**\n * Function to check if cancellation has been requested for this task.\n * Handlers should ideally check this periodically during long-running operations.\n * @returns {boolean} True if cancellation has been requested, false otherwise.\n */\n isCancelled(): boolean;\n\n /**\n * The message history associated with the task up to the point the handler is invoked.\n * Optional, as history might not always be available or relevant.\n */\n history?: Message[];\n\n // taskStore is removed as the server now handles loading/saving directly.\n // If a handler specifically needs history, it would need to be passed differently\n // or the handler pattern might need adjustment based on use case.\n\n // Potential future additions:\n // - logger instance\n // - AbortSignal linked to cancellation\n}\n","import {\n ErrorCodeParseError,\n ErrorCodeInvalidRequest,\n ErrorCodeMethodNotFound,\n ErrorCodePushNotificationNotSupported,\n ErrorCodeTaskNotCancelable,\n ErrorCodeTaskNotFound,\n ErrorCodeUnsupportedOperation,\n ErrorCodeInvalidParams,\n ErrorCodeInternalError,\n} from './types';\nimport type { JSONRPCError, KnownErrorCode } from './types';\n\n/**\n * Custom error class for A2A server operations, incorporating JSON-RPC error codes.\n */\nexport class A2AError extends Error {\n public code: KnownErrorCode | number;\n public data?: unknown;\n public taskId?: string; // Optional task ID context\n\n constructor(code: KnownErrorCode | number, message: string, data?: unknown, taskId?: string) {\n super(message);\n this.name = 'A2AError';\n this.code = code;\n this.data = data;\n this.taskId = taskId; // Store associated task ID if provided\n }\n\n /**\n * Formats the error into a standard JSON-RPC error object structure.\n */\n toJSONRPCError(): JSONRPCError<unknown> {\n const errorObject: JSONRPCError<unknown> = {\n code: this.code,\n message: this.message,\n };\n if (this.data !== undefined) {\n errorObject.data = this.data;\n }\n return errorObject;\n }\n\n // Static factory methods for common errors\n\n static parseError(message: string, data?: unknown): A2AError {\n return new A2AError(ErrorCodeParseError, message, data);\n }\n\n static invalidRequest(message: string, data?: unknown): A2AError {\n return new A2AError(ErrorCodeInvalidRequest, message, data);\n }\n\n static methodNotFound(method: string): A2AError {\n return new A2AError(ErrorCodeMethodNotFound, `Method not found: ${method}`);\n }\n\n static invalidParams(message: string, data?: unknown): A2AError {\n return new A2AError(ErrorCodeInvalidParams, message, data);\n }\n\n static internalError(message: string, data?: unknown): A2AError {\n return new A2AError(ErrorCodeInternalError, message, data);\n }\n\n static taskNotFound(taskId: string): A2AError {\n return new A2AError(ErrorCodeTaskNotFound, `Task not found: ${taskId}`, undefined, taskId);\n }\n\n static taskNotCancelable(taskId: string): A2AError {\n return new A2AError(ErrorCodeTaskNotCancelable, `Task not cancelable: ${taskId}`, undefined, taskId);\n }\n\n static pushNotificationNotSupported(): A2AError {\n return new A2AError(ErrorCodePushNotificationNotSupported, 'Push Notification is not supported');\n }\n\n static unsupportedOperation(operation: string): A2AError {\n return new A2AError(ErrorCodeUnsupportedOperation, `Unsupported operation: ${operation}`);\n }\n}\n"]}