UNPKG

@a2alite/sdk

Version:

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

130 lines (129 loc) 5.18 kB
import type { AgentArtifactParams, AgentStreamQueue, AgentTaskParams } from "./types.ts"; import { AgentExecutionContext } from "./context.ts"; import type { AgentStreamEvent } from "./types.ts"; import { Task, TaskState } from "../../types/types.ts"; /** * Checks if a task state is final (terminal) * * Final states indicate that the task has completed processing and will not * transition to any other state. These include successful completion, failures, * cancellation, and rejection. * * @param state - The task state to check * @returns true if the state is final, false otherwise */ declare const isFinalTaskState: (state: TaskState) => boolean; /** * Checks if a task state is pending (requires external input) * * Pending states indicate that the task is waiting for external input * or authorization before it can continue processing. * * @param state - The task state to check * @returns true if the state is pending, false otherwise */ declare const isPendingTaskState: (state: TaskState) => boolean; /** * Checks if a stream event indicates the end of the stream * * @param event - The agent stream event to check * @returns true if this is an end-of-stream event, false otherwise */ declare function isEndOfStream(event: AgentStreamEvent): boolean; /** * Manages streaming updates for an agent task * * AgentTaskStream provides a way to send incremental updates about a task's progress * to clients through a queue-based system. It supports artifact streaming, status updates, * and automatic stream termination when tasks reach final or pending states. */ declare class AgentTaskStream { /** Whether the stream has been closed */ closed: boolean; /** The execution context associated with this stream */ cx: AgentExecutionContext; /** The queue for streaming events */ streamQueue: AgentStreamQueue; /** * Creates a new AgentTaskStream * @param cx - The execution context that must have a current task * @throws Error if the execution context has no current task */ constructor(cx: AgentExecutionContext); /** * Terminates the stream if the task is in a pending or final state * Sends an end-of-stream event when terminating * @private */ private _terminateIfPendingOrFinalState; /** * Ensures the stream is still open for writing * @throws Error if the stream has been closed * @private */ private _ensureOpen; /** * Sends a task status update event to the stream * @private */ _sendTaskStatusUpdate(): Promise<void>; /** * Gets the current task associated with this stream * @returns The current task * @throws Error if no task is found */ getTask(): Task; /** * Writes an artifact update to the stream * * This method sends artifact data to connected clients and automatically * sets the task state to 'working' if not already set. It supports both * complete artifacts and chunked streaming. * * @param params - Artifact parameters * @param params.artifact - The artifact to stream * @param params.append - Whether to append to existing artifact (default: false) * @param params.lastChunk - Whether this is the final chunk (default: false) * @param sendTaskStatusUpdate - Whether to send status update (default: true) */ writeArtifact({ artifact, append, lastChunk }: AgentArtifactParams, sendTaskStatusUpdate?: boolean): Promise<void>; /** * Sets the task state and sends status update if changed * @param state - The new task state * @throws Error if no task is found * @private * @todo Use this method to set the task state consistently */ private _setTaskState; /** * Starts the task with the provided parameters * Sets the task state to 'working' and sends status update * @param taskParams - Task parameters to update */ start(taskParams: AgentTaskParams): Promise<void>; /** * Rejects the task with the provided parameters * Sets the task state to 'rejected' and terminates the stream * @param taskParams - Task parameters including rejection reason */ reject(taskParams: AgentTaskParams): Promise<void>; /** * Marks the task as requiring authentication * Sets the task state to 'auth-required' and terminates the stream * @param taskParams - Task parameters including auth requirements */ authRequired(taskParams: AgentTaskParams): Promise<void>; /** * Marks the task as requiring additional input * Sets the task state to 'input-required' and terminates the stream * @param taskParams - Task parameters including input requirements */ inputRequired(taskParams: AgentTaskParams): Promise<void>; /** * Completes the task with the provided parameters * Sets the task state to 'completed' and terminates the stream * @param taskParams - Task parameters including completion details */ complete(taskParams: AgentTaskParams): Promise<void>; } export { isFinalTaskState, isPendingTaskState, isEndOfStream, AgentTaskStream };