UNPKG

@mastra/core

Version:
1 lines 13.8 kB
{"version":3,"file":"client-C9xbmacV.cjs","names":[],"sources":["../src/a2a/types.ts","../src/a2a/error.ts"],"sourcesContent":["import type { AgentCard, JSONRPCMessage, Message, Task } from '@a2a-js/sdk';\nimport type { FullOutput, MastraModelOutput } from '../stream/base/output';\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\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\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/** Error code for Content Type Not Supported (-32005). The requested content type is not supported. */\nexport const ErrorCodeContentTypeNotSupported = -32005;\nexport type ErrorCodeContentTypeNotSupported = typeof ErrorCodeContentTypeNotSupported;\n/** Error code for Invalid Agent Response (-32006). The agent returned an invalid response. */\nexport const ErrorCodeInvalidAgentResponse = -32006;\nexport type ErrorCodeInvalidAgentResponse = typeof ErrorCodeInvalidAgentResponse;\n/** Error code for Extended Agent Card Not Configured (-32007). The agent has no extended card configured. */\nexport const ErrorCodeExtendedAgentCardNotConfigured = -32007;\nexport type ErrorCodeExtendedAgentCardNotConfigured = typeof ErrorCodeExtendedAgentCardNotConfigured;\n/** Error code for Extension Support Required (-32008). The request requires extension support. */\nexport const ErrorCodeExtensionSupportRequired = -32008;\nexport type ErrorCodeExtensionSupportRequired = typeof ErrorCodeExtensionSupportRequired;\n/** Error code for Version Not Supported (-32009). The requested protocol version is not supported. */\nexport const ErrorCodeVersionNotSupported = -32009;\nexport type ErrorCodeVersionNotSupported = typeof ErrorCodeVersionNotSupported;\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 | typeof ErrorCodeContentTypeNotSupported\n | typeof ErrorCodeInvalidAgentResponse\n | typeof ErrorCodeExtendedAgentCardNotConfigured\n | typeof ErrorCodeExtensionSupportRequired\n | typeof ErrorCodeVersionNotSupported;\n\nexport type RequestCredentialsMode = 'omit' | 'same-origin' | 'include';\n\nexport interface A2AAgentCardVerificationContext {\n cardUrl: string;\n fetchedAt: Date;\n}\n\nexport interface A2AAgentVerificationOptions {\n verify: (card: AgentCard, context: A2AAgentCardVerificationContext) => Promise<void> | void;\n}\n\nexport interface A2AAgentOptions {\n url: string;\n id?: string;\n name?: string;\n description?: string;\n headers?: Record<string, string>;\n retries?: number;\n backoffMs?: number;\n maxBackoffMs?: number;\n credentials?: RequestCredentialsMode;\n fetch?: typeof fetch;\n abortSignal?: AbortSignal;\n timeoutMs?: number;\n verifyAgentCard?: A2AAgentVerificationOptions;\n}\n\nexport interface A2AAgentRunState {\n runId: string;\n contextId?: string;\n taskId?: string;\n executionUrl: string;\n cardUrl: string;\n streamingSupported: boolean;\n waitingForInput: boolean;\n lastTask?: Task;\n}\n\nexport interface A2AAgentResumePayload {\n taskId?: string;\n contextId?: string;\n executionUrl: string;\n cardUrl: string;\n waitingForInput: boolean;\n task?: Task;\n}\n\nexport type A2AAgentGenerateResult = FullOutput<undefined> & {\n task?: Task;\n message?: Message;\n resumePayload?: A2AAgentResumePayload;\n resumeSchema?: string;\n};\n\nexport type A2AAgentStreamResult = MastraModelOutput<undefined> & {\n task: Promise<Task | undefined>;\n suspendPayload: Promise<A2AAgentResumePayload | undefined>;\n resumeSchema: Promise<string | undefined>;\n getResult(): Promise<A2AAgentGenerateResult>;\n};\n","import {\n ErrorCodeContentTypeNotSupported,\n ErrorCodeExtendedAgentCardNotConfigured,\n ErrorCodeParseError,\n ErrorCodeExtensionSupportRequired,\n ErrorCodeInvalidRequest,\n ErrorCodeInvalidAgentResponse,\n ErrorCodeMethodNotFound,\n ErrorCodePushNotificationNotSupported,\n ErrorCodeTaskNotCancelable,\n ErrorCodeTaskNotFound,\n ErrorCodeUnsupportedOperation,\n ErrorCodeInvalidParams,\n ErrorCodeInternalError,\n ErrorCodeVersionNotSupported,\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 MastraA2AError 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 = 'MastraA2AError';\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): MastraA2AError {\n return new MastraA2AError(ErrorCodeParseError, message, data);\n }\n\n static invalidRequest(message: string, data?: unknown): MastraA2AError {\n return new MastraA2AError(ErrorCodeInvalidRequest, message, data);\n }\n\n static methodNotFound(method: string): MastraA2AError {\n return new MastraA2AError(ErrorCodeMethodNotFound, `Method not found: ${method}`);\n }\n\n static invalidParams(message: string, data?: unknown): MastraA2AError {\n return new MastraA2AError(ErrorCodeInvalidParams, message, data);\n }\n\n static internalError(message: string, data?: unknown): MastraA2AError {\n return new MastraA2AError(ErrorCodeInternalError, message, data);\n }\n\n static taskNotFound(taskId: string): MastraA2AError {\n return new MastraA2AError(ErrorCodeTaskNotFound, `Task not found: ${taskId}`, undefined, taskId);\n }\n\n static taskNotCancelable(taskId: string): MastraA2AError {\n return new MastraA2AError(ErrorCodeTaskNotCancelable, `Task not cancelable: ${taskId}`, undefined, taskId);\n }\n\n static pushNotificationNotSupported(): MastraA2AError {\n return new MastraA2AError(ErrorCodePushNotificationNotSupported, 'Push Notification is not supported');\n }\n\n static unsupportedOperation(operation: string): MastraA2AError {\n return new MastraA2AError(ErrorCodeUnsupportedOperation, `Unsupported operation: ${operation}`);\n }\n\n static contentTypeNotSupported(contentType: string): MastraA2AError {\n return new MastraA2AError(ErrorCodeContentTypeNotSupported, `Content type not supported: ${contentType}`, {\n contentType,\n });\n }\n\n static invalidAgentResponse(message: string, data?: unknown): MastraA2AError {\n return new MastraA2AError(ErrorCodeInvalidAgentResponse, message, data);\n }\n\n static extendedAgentCardNotConfigured(): MastraA2AError {\n return new MastraA2AError(ErrorCodeExtendedAgentCardNotConfigured, 'Extended agent card is not configured');\n }\n\n static extensionSupportRequired(extension?: string): MastraA2AError {\n return new MastraA2AError(\n ErrorCodeExtensionSupportRequired,\n extension ? `Extension support required: ${extension}` : 'Extension support required',\n extension ? { extension } : undefined,\n );\n }\n\n static versionNotSupported(version: string): MastraA2AError {\n return new MastraA2AError(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