A2A Conversations

See A2AMock for agent registration and server setup.

Task-bound Conversations

Register an ordered conversation with onConversation(agentName, turns). Each turn has a string or RegExp pattern and a response with state, optional parts, and optional artifacts. Omitted output arrays are empty. The exported A2AConversationTurn type is available from both the package root and the A2A subpath.

Conversation typescript
a2a.onConversation("assistant", [
  {
    pattern: "book a trip",
    response: {
      state: "TASK_STATE_INPUT_REQUIRED",
      parts: [{ text: "Where would you like to go?" }],
    },
  },
  {
    pattern: "Paris",
    response: {
      state: "TASK_STATE_COMPLETED",
      parts: [{ text: "Your destination is Paris." }],
      artifacts: [{ name: "destination", parts: [{ data: { city: "Paris" } }] }],
    },
  },
]);

Conversations must be nonempty, with input-required intermediate turns and a completed or failed final turn. Each part has exactly one of text, data, or url, and data is not null. Invalid definitions throw without being registered. Sends that match a conversation with malformed parts are rejected. A send without message.taskId selects a first turn in the existing registration order. SendMessage considers every fixture kind, so an earlier message or task fixture that matches the same text is used instead. SendStreamingMessage considers only streaming tasks and conversations. Subsequent sends include that task ID and must match its next turn. A supplied message.contextId must agree with the task. An omitted, null, or empty context is taken from the task on continuation, or generated for a new task. New sends without a task ID create independent tasks even in a shared context. A null or empty task ID or message ID counts as omitted, and an omitted message ID is generated.

Continue with either SendMessage or SendStreamingMessage; the task ID selects the conversation. A conversation stream sends one event that contains the accumulated result.task snapshot. A rejected streaming send returns a plain JSON-RPC error body, not an event stream. Each accepted turn records the user message and agent reply in order, preserving user message IDs, stamping both messages with the task and context IDs, and appending artifacts. While the task awaits input, status.message holds the agent reply. Rejected sends leave task state unchanged. GetTask and ListTasks expose current state. GetTask and CancelTask return the task itself as result, as in { "result": { "id": "task-...", "status": {...}, "history": [...] } }, which A2A SDK clients decode directly. CancelTask preserves accepted output but prevents continuation. reset() removes all registrations and tasks.

In JSON configuration, add conversations: [{ turns: [...] }] to an agent. Configured patterns are strings, and conversations are registered after that agent's messages, tasks, and streaming tasks. Existing one-shot fixtures remain supported.

Recovering accepted responses

Pass { replayLimit: 2 } as the third argument to onConversation to retain two accepted message IDs per task. Configuration conversations accept the same replayLimit alongside turns. The limit must be a nonnegative safe integer. Its default, zero, disables retention.

Retry with the task ID, the original nonempty message ID, and structurally equal JSON parts to recover the original task snapshot. Object key order does not matter. Either send method can replay a response, including after completion or cancellation, without changing the current task. A conflicting context or different parts is an error. A request without a task ID always starts a fresh task.

New keyed acceptances evict the oldest retained ID when the limit is exceeded. Replays do not refresh retention order. Errors and sends with omitted, null, or empty IDs do not consume retention. An evicted ID is subject to normal continuation rules. When retention is enabled, concurrent identical keyed retries accept just one turn. Reset clears retained responses.