title: Server. Task Manager

A2A Node SDK - v0.1.0 / Modules / Server / TaskManager

Class: TaskManager

Server.TaskManager

Manages task lifecycle and state

The TaskManager is responsible for creating, updating, and managing tasks throughout their lifecycle. It provides methods for task creation, status updates, cancellation, and artifact management.

Example

// Create a task manager with in-memory storage
const taskStore = new InMemoryTaskStore();
const taskManager = new TaskManager(taskStore);
 
// Create a new task
const task = await taskManager.createTask({
  name: 'ProcessData',
  agentId: 'data-processor',
  parts: [{ type: 'text', content: 'Process this data', format: 'plain' }],
  expectedParts: 1,
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString()
});
 
console.log('Created task:', task.id);

Table of contents

Constructors

Methods

Constructors

constructor

new TaskManager(taskStore, pushService?): TaskManager

Creates a new TaskManager

Parameters

NameTypeDescription
taskStoreTaskStoreStorage backend for tasks
pushService?PushNotificationServiceOptional service for push notifications

Returns

TaskManager

Methods

addArtifact

addArtifact(taskId, artifact): Promise<void>

Adds an artifact to a task

Artifacts represent files, data, or other content associated with a task. This method adds an artifact to a specific task.

Parameters

NameTypeDescription
taskIdstringID of the task
artifactObjectArtifact to add
artifact.contentRecord<string, any>The actual content of the artifact
artifact.createdAtstringISO timestamp when the artifact was created
artifact.idstringUnique identifier for the artifact
artifact.type"text" | "file" | "data"The type of content this artifact contains
artifact.updatedAtstringISO timestamp when the artifact was last updated

Returns

Promise<void>

Promise resolving when the artifact is added

Throws

If the task is not found

Example

await taskManager.addArtifact('task-123', {
  id: 'artifact-456',
  type: 'file',
  content: 'base64-encoded-file-data',
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString(),
  metadata: {
    filename: 'report.pdf',
    mimeType: 'application/pdf'
  }
});

cancelTask

cancelTask(taskId): Promise<void>

Cancels a task

Cancels a task that is not already in a terminal state (completed, failed, or canceled). If a push service is configured, it will send a push notification about the cancellation.

Parameters

NameTypeDescription
taskIdstringID of the task to cancel

Returns

Promise<void>

Promise resolving when the task is canceled

Throws

If the task is not found

Throws

If the task is already in a terminal state

Example

try {
  await taskManager.cancelTask('task-123');
  console.log('Task canceled successfully');
} catch (error) {
  if (error instanceof InvalidTaskStateError) {
    console.error('Cannot cancel task in terminal state');
  } else {
    console.error('Error canceling task:', error);
  }
}

createTask

createTask(task): Promise<Task>

Creates a new task

Creates a task with the provided parameters and initializes a result aggregator if expectedParts is specified.

Parameters

NameTypeDescription
taskOmit<Task, "status" | "id">Task parameters (without id and status)

Returns

Promise<Task>

Promise resolving to the created task

Example

const task = await taskManager.createTask({
  name: 'ProcessImage',
  agentId: 'image-processor',
  parts: [{
    type: 'file',
    content: 'base64-encoded-image-data',
    mimeType: 'image/jpeg',
    name: 'image.jpg'
  }],
  expectedParts: 2,
  createdAt: new Date().toISOString(),
  updatedAt: new Date().toISOString()
});

getAggregator

getAggregator(taskId): undefined | ResultAggregator

Gets the result aggregator for a task

Result aggregators collect and process task results, including handling streaming responses and combining multiple parts.

Parameters

NameTypeDescription
taskIdstringID of the task

Returns

undefined | ResultAggregator

The result aggregator for the task, or undefined if none exists


getArtifact

getArtifact(taskId, artifactId): Promise<null | { content: Record<string, any> ; createdAt: string ; id: string ; type: "text" | "file" | "data" ; updatedAt: string }>

Gets a specific artifact for a task

Retrieves a specific artifact by ID from a specific task.

Parameters

NameTypeDescription
taskIdstringID of the task
artifactIdstringID of the artifact

Returns

Promise<null | { content: Record<string, any> ; createdAt: string ; id: string ; type: "text" | "file" | "data" ; updatedAt: string }>

Promise resolving to the artifact, or null if not found

Throws

If the task is not found

Example

const artifact = await taskManager.getArtifact('task-123', 'artifact-456');
if (artifact) {
  console.log('Found artifact:', artifact.id);
  console.log('Type:', artifact.type);
  console.log('Created at:', artifact.createdAt);
} else {
  console.log('Artifact not found');
}

getArtifacts

getArtifacts(taskId): Promise<{ content: Record<string, any> ; createdAt: string ; id: string ; type: "text" | "file" | "data" ; updatedAt: string }[]>

Gets all artifacts for a task

Retrieves all artifacts associated with a specific task.

Parameters

NameTypeDescription
taskIdstringID of the task

Returns

Promise<{ content: Record<string, any> ; createdAt: string ; id: string ; type: "text" | "file" | "data" ; updatedAt: string }[]>

Promise resolving to an array of artifacts

Throws

If the task is not found

Example

const artifacts = await taskManager.getArtifacts('task-123');
console.log(`Task has ${artifacts.length} artifacts`);
 
// Process each artifact
for (const artifact of artifacts) {
  console.log(`Artifact ${artifact.id} of type ${artifact.type}`);
}

getTask

getTask(taskId): Promise<Task>

Gets a task by ID

Retrieves a task from the task store by its ID.

Parameters

NameTypeDescription
taskIdstringID of the task to retrieve

Returns

Promise<Task>

Promise resolving to the task

Throws

If the task is not found

Example

try {
  const task = await taskManager.getTask('task-123');
  console.log('Task status:', task.status);
} catch (error) {
  if (error instanceof TaskNotFoundError) {
    console.error('Task not found');
  } else {
    console.error('Error retrieving task:', error);
  }
}

listActiveTasks

listActiveTasks(): Promise<Task[]>

Lists all active tasks

Retrieves all tasks that are not in a terminal state (completed, failed, or canceled). Active tasks include those with status ‘submitted’, ‘working’, or ‘input_required’.

Returns

Promise<Task[]>

Promise resolving to an array of active tasks

Example

const activeTasks = await taskManager.listActiveTasks();
console.log(`Found ${activeTasks.length} active tasks`);
 
// Process each active task
for (const task of activeTasks) {
  console.log(`Task ${task.id} is ${task.status}`);
}

updateTask

updateTask(taskId, updates): Promise<Task>

Updates a task with new data

Updates a task with the provided partial task data and sets the updatedAt timestamp to the current time.

Parameters

NameTypeDescription
taskIdstringID of the task to update
updatesPartial<Omit<Task, "id">>Partial task data to update

Returns

Promise<Task>

Promise resolving to the updated task

Throws

If the task is not found

Example

const updatedTask = await taskManager.updateTask('task-123', {
  name: 'Updated Task Name',
  metadata: { priority: 'high' }
});

updateTaskStatus

updateTaskStatus(taskId, status): Promise<Task>

Updates a task’s status

Updates the status of a task and sets the updatedAt timestamp to the current time.

Parameters

NameTypeDescription
taskIdstringID of the task to update
status"submitted" | "working" | "input_required" | "completed" | "failed" | "canceled"New status for the task

Returns

Promise<Task>

Promise resolving to the updated task

Throws

If the task is not found

Example

// Update task status to 'completed'
const task = await taskManager.updateTaskStatus('task-123', 'completed');
console.log('Task status updated:', task.status);