Skip to main content
Message and Event are the two fundamental data structures in AgentScope.
  • Message — the unit of inter-agent communication and persistence. Each Msg represents a complete conversation turn that is stored in context and exchanged between agents.
  • Event — the unit of frontend interaction and streaming. Events carry incremental progress updates (text tokens, tool call fragments, permission requests) and drive real-time UIs and human-in-the-loop workflows.
A sequence of events produced during a single reply call accumulates into exactly one assistant Msg. This guarantees that the complete message state is always recoverable from its event stream.

Message

An instance of the Msg class in AgentScope contains a complete conversation turn — a user input, or a complete assistant response, organized through different types of content blocks (Blocks).
  1. Running an agent’s reply_stream once produces a complete Msg instance, containing all information such as multiple rounds of reasoning, tool calls, and execution results.
  2. During frontend rendering, a Msg instance corresponds to a single complete message bubble.

Structure

The Msg class has the following core fields:

Content Blocks

Message content is composed of typed blocks. Each block represents a distinct piece of information:
Role constraints are enforced at construction:
  • msg.role=="user" messages can only contain TextBlock and DataBlock;
  • msg.role=="system" messages can only contain TextBlock;
  • msg.role=="assistant" messages can contain all block types.
These content blocks carry different data information, and their detailed fields are described below:
This content block allows passing through custom metadata defined by the model provider (such as the signature in Anthropic models, etc.).
Data Source Configuration:
  • Base64Source:
    • type: Fixed as "base64".
    • data: Base64-encoded binary data.
    • media_type: Media type (e.g., "image/png", "audio/mpeg", "video/mp4", etc.).
  • URLSource:
    • type: Fixed as "url".
    • url: A valid URI/URL string satisfying the RFC 3986 standard.
    • media_type: Media type (e.g., "image/png", "audio/wav", etc.).
When finally passed to the LLM API, HintBlock is also converted to a standard user message (User message).To avoid confusion with user inputs, it is recommended to use XML tags (e.g., <system-reminder>...</system-reminder>) to wrap the hint content.
The id of a ToolResultBlock must match the id of the launching ToolCallBlock. Supports multimodal data.

Create Messages

AgentScope provides three shortcut methods to construct Msg objects to avoid repeatedly setting the role parameter, and supports building TextBlocks from strings: When the content parameter is a string, it is automatically wrapped into a TextBlock.

Access Content

Msg provides helper methods to extract specific block types:

Event

Events are the streaming counterpart of messages. While the agent executes, it yields a sequence of AgentEvent objects that represent incremental progress — text tokens arriving, tool calls being constructed, results streaming back. Each event is lightweight and self-contained.

Event Lifecycle

Every event carries a reply_id that links it to the message being constructed. Within a reply, block_id or tool_call_id identifies which content block an event belongs to. Events follow a start → delta → end pattern for each content block: All events within the same reply share the same reply_id. Within a reply, use block_id to correlate text/thinking/data block events, and tool_call_id to correlate tool call and tool result events.

Event Types

All events inherit from EventBase which provides common fields: Events are grouped by category below. Every event also carries a reply_id field (except where noted) that links it to the message being constructed.
ReplyStartEvent — Agent begins a new reply.ReplyEndEvent — Agent finishes the reply.ExceedMaxItersEvent — Agent reached the maximum reasoning-acting iterations.
TextBlockStartEvent — A new text block begins.TextBlockDeltaEvent — Incremental text content arrives.TextBlockEndEvent — The text block is complete.
ThinkingBlockStartEvent — A new thinking block begins.ThinkingBlockDeltaEvent — Incremental thinking content arrives.ThinkingBlockEndEvent — The thinking block is complete.
DataBlockStartEvent — A new data block begins (image, audio, etc.).DataBlockDeltaEvent — Incremental binary data arrives.DataBlockEndEvent — The data block is complete.
ToolCallStartEvent — The agent begins a tool call.ToolCallDeltaEvent — Incremental tool call input arrives.ToolCallEndEvent — The tool call input is complete.
ToolResultStartEvent — Tool execution begins.ToolResultTextDeltaEvent — Incremental text output from the tool.ToolResultDataDeltaEvent — Binary data output from the tool.ToolResultEndEvent — Tool execution is complete.
ModelCallStartEvent — A model API call begins.ModelCallEndEvent — A model API call completes.
RequireUserConfirmEvent — Agent pauses for user confirmation.RequireExternalExecutionEvent — Agent pauses for external execution.UserConfirmResultEvent — User provides confirmation results (input event).ExternalExecutionResultEvent — External system provides execution results (input event).
Unlike text / thinking / data / tool blocks, these events do not follow the start → delta → end pattern. The full payload arrives in a single event because it is known up-front rather than streamed.HintBlockEvent — A HintBlock is injected into the agent’s context (e.g. a scheduled-task trigger, a team message, a result returned by an offloaded background tool).CustomEvent — Generic extensible event used by service-layer middleware to notify subscribers about state changes (task progress, team membership, permission updates, …) without polluting the core agent event enum.

Reconstruct Messages from Events

Events and messages are not independent — they are two views of the same data. Every event produced by reply_stream can be applied to a Msg via append_event(), reconstructing the complete message incrementally. This guarantees that the final message state is fully recoverable from the event stream alone.
The append_event method handles all event types:
This design makes deployment more flexible: the backend can stream events via SSE to the frontend, which reconstructs and renders the message in real time. Even if the connection is interrupted, replaying the event sequence from any checkpoint can restore the exact message state.

TypeScript Support

AgentScope provides TypeScript versions of messages and event primitives, so frontends can use the exact same appendEvent API to reconstruct messages from the event stream. Install the TypeScript version of AgentScope:
Example of receiving and reconstructing messages on the frontend:

Example: Streaming UI

A typical pattern for building a streaming interface:

Further Reading

Agent

How the agent produces events and messages in the ReAct loop

Context

How messages are stored, compressed, and offloaded