ACPex.Schema.Types.SessionUpdate (ACPex v0.1.1)

Copy Markdown View Source

Session update union type.

A SessionUpdate can be one of several variants, discriminated by the type field:

  • "user_message_chunk" - Streaming user message content
  • "agent_message_chunk" - Streaming agent response content
  • "agent_thought_chunk" - Streaming agent reasoning content
  • "tool_call" - Notification of a new tool call
  • "tool_call_update" - Status update for an existing tool call
  • "plan" - Agent's execution plan for complex tasks
  • "available_commands_update" - Changes in available commands
  • "current_mode_update" - Session mode changes

Usage

You can use any of the variant structs directly:

# User message chunk
%ACPex.Schema.Types.SessionUpdate.UserMessageChunk{
  session_update: "update-123",
  content: %{"type" => "text", "text" => "Hello"}
}

# Tool call
%ACPex.Schema.Types.SessionUpdate.ToolCall{
  session_update: "update-456",
  tool_call_id: "tool-789",
  title: "Read File",
  status: "running"
}

Or use maps (which will be validated at runtime):

%{
  "type" => "user_message_chunk",
  "sessionUpdate" => "update-123",
  "content" => %{"type" => "text", "text" => "Hello"}
}

Decoding from JSON

Use the decode/1 function to decode JSON into the appropriate variant struct:

json = ~s({"type":"user_message_chunk","sessionUpdate":"u1","content":{"type":"text","text":"Hi"}})
{:ok, %ACPex.Schema.Types.SessionUpdate.UserMessageChunk{}} = SessionUpdate.decode(json)

Encoding to JSON

All variant structs implement Jason.Encoder:

update = %ACPex.Schema.Types.SessionUpdate.UserMessageChunk{
  session_update: "u1",
  content: %{"type" => "text", "text" => "Hi"}
}
Jason.encode!(update)

Summary

Functions

Decodes a JSON string or map into the appropriate SessionUpdate variant struct.

Same as decode/1 but raises on error.

Types

Functions

decode(json)

@spec decode(String.t() | map()) :: {:ok, variant()} | {:error, String.t()}

Decodes a JSON string or map into the appropriate SessionUpdate variant struct.

The type field is used as a discriminator to determine which variant to decode to.

Examples

iex> SessionUpdate.decode(~s({"type":"user_message_chunk","sessionUpdate":"u1","content":{"type":"text","text":"Hi"}}))
{:ok, %SessionUpdate.UserMessageChunk{session_update: "u1", content: %{"type" => "text", "text" => "Hi"}}}

iex> SessionUpdate.decode(%{"type" => "tool_call", "sessionUpdate" => "u2", "toolCallId" => "t1", "title" => "Read"})
{:ok, %SessionUpdate.ToolCall{session_update: "u2", tool_call_id: "t1", title: "Read"}}

iex> SessionUpdate.decode(%{"type" => "unknown"})
{:error, "Unknown session update type: unknown"}

decode!(json)

@spec decode!(String.t() | map()) :: variant()

Same as decode/1 but raises on error.