UNPKG

@dexwox-labs/a2a-server

Version:

TypeScript server implementation for Google's Agent-to-Agent (A2A) protocol - includes Express/WebSocket handlers, request validation and queue management

206 lines 6.56 kB
/** * @module TaskEventManager * @description Manages task lifecycle events in the A2A protocol * * This module provides a manager for publishing task-related events to an event queue. * It handles events for task creation, updates, completion, failure, cancellation, * and artifact additions. */ import { EventQueue } from './event-queue'; import { Task, A2AError, Artifact } from '@dexwox-labs/a2a-core'; /** * Manages task lifecycle events * * The TaskEventManager is responsible for publishing task-related events to an * event queue. It provides methods for different types of task events, such as * creation, updates, completion, failure, and cancellation. * * @example * ```typescript * // Create an event queue * const eventQueue = new EventQueue(); * * // Create a task event manager * const taskEventManager = new TaskEventManager(eventQueue); * * // Subscribe to task events * eventQueue.subscribe(event => { * console.log(`Task event: ${event.type} for task ${event.task.id}`); * }); * * // Publish a task created event * taskEventManager.taskCreated({ * id: 'task-123', * name: 'Process Data', * status: 'submitted', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }); * ``` */ export declare class TaskEventManager { /** The event queue to publish events to */ private eventQueue; /** * Creates a new TaskEventManager * * @param eventQueue - The event queue to publish events to */ constructor(eventQueue: EventQueue); /** * Publishes a task created event * * This method publishes an event indicating that a new task has been created. * It validates that the task has an ID before publishing the event. * * @param task - The task that was created * @throws InvalidTaskStateError if the task doesn't have an ID * * @example * ```typescript * taskEventManager.taskCreated({ * id: 'task-123', * name: 'Process Data', * status: 'submitted', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }); * ``` */ taskCreated(task: Task): void; /** * Publishes a task updated event * * This method publishes an event indicating that a task has been updated. * It validates that the task has an ID before publishing the event and * optionally includes the previous state of the task. * * @param task - The updated task * @param previousState - Optional previous state of the task * @throws InvalidTaskStateError if the task doesn't have an ID * * @example * ```typescript * taskEventManager.taskUpdated( * { * id: 'task-123', * name: 'Process Data', * status: 'working', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }, * 'submitted' // previous state * ); * ``` */ taskUpdated(task: Task, previousState?: string): void; /** * Publishes a task completed event * * This method publishes an event indicating that a task has been completed. * It validates that the task has an ID before publishing the event. * * @param task - The completed task * @throws InvalidTaskStateError if the task doesn't have an ID * * @example * ```typescript * taskEventManager.taskCompleted({ * id: 'task-123', * name: 'Process Data', * status: 'completed', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }); * ``` */ taskCompleted(task: Task): void; /** * Publishes a task failed event * * This method publishes an event indicating that a task has failed. * It validates that the task has an ID before publishing the event and * normalizes the error to ensure it conforms to the A2AError format. * * @param task - The failed task * @param error - The error that caused the task to fail * @throws InvalidTaskStateError if the task doesn't have an ID * * @example * ```typescript * try { * // Some operation that might fail * throw new Error('Processing failed'); * } catch (error) { * taskEventManager.taskFailed( * { * id: 'task-123', * name: 'Process Data', * status: 'failed', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }, * error * ); * } * ``` */ taskFailed(task: Task, error: Error | A2AError): void; /** * Publishes a task canceled event * * This method publishes an event indicating that a task has been canceled. * It validates that the task has an ID before publishing the event. * * @param task - The canceled task * @throws InvalidTaskStateError if the task doesn't have an ID * * @example * ```typescript * taskEventManager.taskCanceled({ * id: 'task-123', * name: 'Process Data', * status: 'canceled', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }); * ``` */ taskCanceled(task: Task): void; /** * Publishes an artifact added event * * This method publishes an event indicating that an artifact has been added to a task. * It validates that both the task and artifact have IDs before publishing the event. * * @param task - The task that the artifact was added to * @param artifact - The artifact that was added * @throws InvalidTaskStateError if the task or artifact doesn't have an ID * * @example * ```typescript * taskEventManager.artifactAdded( * { * id: 'task-123', * name: 'Process Data', * status: 'working', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString() * }, * { * id: 'artifact-456', * type: 'file', * content: 'base64-encoded-content', * createdAt: new Date().toISOString(), * updatedAt: new Date().toISOString(), * metadata: { * filename: 'result.pdf', * mimeType: 'application/pdf' * } * } * ); * ``` */ artifactAdded(task: Task, artifact: Artifact): void; } //# sourceMappingURL=task-event-manager.d.ts.map