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

105 lines 4.78 kB
"use strict"; /** * @module AgentExecutor * @description Interfaces and implementations for executing agent tasks * * This module provides the interfaces and implementations for executing tasks * on behalf of agents in the A2A protocol. It handles task lifecycle management, * status updates, and event publishing. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultAgentExecutor = void 0; const a2a_core_1 = require("@dexwox-labs/a2a-core"); /** * Default implementation of the AgentExecutor interface * * This class provides a basic implementation of the AgentExecutor interface * that manages task lifecycle, publishes events, and handles errors. It uses * a TaskManager for task persistence and a TaskEventManager for event publishing. * * @example * ```typescript * // Create dependencies * const taskStore = new InMemoryTaskStore(); * const taskManager = new TaskManager(taskStore); * const taskEventManager = new TaskEventManager(); * * // Create the executor * const executor = new DefaultAgentExecutor(taskManager, taskEventManager); * * // Execute a task * await executor.execute( * createRequestContext(task, 'agent-123'), * new EventQueue() * ); * ``` */ class DefaultAgentExecutor { /** * Creates a new DefaultAgentExecutor * * @param taskManager - The task manager for task persistence * @param taskEventManager - The task event manager for event publishing */ constructor(taskManager, taskEventManager) { this.taskManager = taskManager; this.taskEventManager = taskEventManager; } /** * Executes a task on behalf of an agent * * This method handles the execution of a task, including updating its status, * publishing events, and handling errors. It transitions the task from its * current state to 'working' and then to 'completed' if successful. * * @param context - The request context containing task and agent information * @param eventQueue - The event queue for publishing events * @returns Promise that resolves when execution is complete * @throws Various task-related errors if execution fails */ execute(context, eventQueue) { return __awaiter(this, void 0, void 0, function* () { const task = yield this.taskManager.getTask(context.task.id); // Update task status to working const workingTask = yield this.taskManager.updateTaskStatus(task.id, 'working'); // Publish task update event this.taskEventManager.taskUpdated(workingTask, task.status); try { // TODO: Implement actual agent execution logic // For now we'll simulate successful completion yield new Promise(resolve => setTimeout(resolve, 1000)); const completedTask = yield this.taskManager.updateTaskStatus(task.id, 'completed'); this.taskEventManager.taskCompleted(completedTask); } catch (error) { const failedTask = yield this.taskManager.updateTaskStatus(task.id, 'failed'); const normalizedError = error instanceof Error ? error : new Error(error instanceof Object ? JSON.stringify(error) : String(error)); this.taskEventManager.taskFailed(failedTask, normalizedError); throw normalizedError; } }); } cancel(context, eventQueue) { return __awaiter(this, void 0, void 0, function* () { const task = yield this.taskManager.getTask(context.task.id); if (['completed', 'failed', 'canceled'].includes(task.status)) { throw new a2a_core_1.InvalidTaskStateError(`Task ${task.id} is already in terminal state: ${task.status}`); } yield this.taskManager.cancelTask(task.id); this.taskEventManager.taskCanceled(task); }); } } exports.DefaultAgentExecutor = DefaultAgentExecutor; //# sourceMappingURL=agent-executor.js.map