UNPKG

@a2alite/sdk

Version:

A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management

102 lines (101 loc) 3.22 kB
import { v4 as uuidv4 } from "uuid"; import { TaskSchema, } from "../types/types.js"; class TaskHandler { constructor(baseTask) { this.task = { id: baseTask?.id || uuidv4(), kind: "task", contextId: baseTask?.contextId || undefined, status: baseTask?.status ? { ...baseTask.status } : { state: "submitted", timestamp: new Date().toISOString(), }, artifacts: baseTask?.artifacts ? [...baseTask.artifacts] : [], history: baseTask?.history ? [...baseTask.history] : [], metadata: baseTask?.metadata ? { ...baseTask.metadata } : {}, }; } withId(id) { this.task.id = id; return this; } withContextId(contextId) { this.task.contextId = contextId; return this; } withStatus(status) { this.task.status = { ...status, timestamp: status.timestamp || new Date().toISOString(), }; // update message taskId and contextId if message is set if (status.message) { this.task.status.message = { ...status.message, taskId: this.task.id, contextId: this.task.contextId, }; } return this; } handleStatusUpdate(event) { if (event.taskId !== this.task.id) { throw new Error(`Task ID mismatch for status update: expected ${this.task.id}, got ${event.taskId}`); } return this.withStatus({ ...event.status, }); } withArtifacts(artifacts) { this.task.artifacts = [...artifacts]; return this; } upsertArtifact(artifact, append = false) { if (!this.task.artifacts) { this.task.artifacts = []; } const existingIndex = this.task.artifacts.findIndex((a) => a.artifactId === artifact.artifactId); // ToDo: find out how the artifacts parts are aggregated if an artifacts has multimodal parts with chunks. if (existingIndex !== -1) { if (append) { this.task.artifacts[existingIndex].parts.push(...artifact.parts); } else { this.task.artifacts[existingIndex] = artifact; } } else { this.task.artifacts.push(artifact); } return this; } handleArtifactUpdate(event) { if (event.taskId !== this.task.id) return this; return this.upsertArtifact(event.artifact, event.append); } withMetadata(metadata) { this.task.metadata = metadata; return this; } withHistory(messages) { this.task.history = [...messages]; return this; } addMessageToHistory(message) { if (!this.task.history) { this.task.history = []; } this.task.history.push(message); return this; } getTask() { if (!this.task.contextId) { throw new Error("Context ID is required for task"); } return TaskSchema.parse(this.task); } } export { TaskHandler };