ExAgent.Chat (ExAgent v3.0.0)

View Source

Pure view state for one authorized ExAgent.Server conversation.

Stability — Stable.

Start with new/2 and apply a Server.history_page/2 with projection: :chat. Request include_draft: true to restore eligible drafts. Reduce ordered replay batches from the page's cursor. On an expired cursor or a root terminal, apply a fresh page to refresh the accepted transcript. Read turns, pending reviews and deferrals from the same owner and pass them to put_controls/4. Deferrals expose identifiers, kind, the timer wake time and the candidate Frame3 delegation path when present. Read the corresponding item with ExAgent.Server.pending/1 after authorizing the viewer to present a form or supply a result.

For example, after resolving an authorized owner:

chat = ExAgent.Chat.new(namespace, conversation_id)
{:ok, page} = ExAgent.Server.history_page(owner, projection: :chat, include_draft: true)
{chat, {:reset, rows}} = ExAgent.Chat.apply_page(chat, page)
{:ok, batch} = ExAgent.Server.events_since(owner, chat.cursor)
chat = ExAgent.Chat.reduce(chat, batch)
answer = ExAgent.Chat.draft(chat)

Row updates can be applied directly to a UI stream. They carry stable history offsets within the current revision. The candidate :activities field holds up to 256 status records from the current owner for the most recent root attempt, with identifiers for delegated runs and background jobs. Entries are newest first by activity event. An open child without a known outcome becomes :unknown when the root attempt ends. Activity resets on a new attempt and is included in chat history pages. The caller owns authorization, subscriptions and batch acknowledgements. Read provisional text with draft/2. Its accumulators are internal.

When a root turn fails, failure holds its run ID and public diagnostic. An idle owner with a failed last turn projects as :failed. An owner that needs recovery still projects as :blocked. The next turn clears the failure, and history pages restore it for the lifetime of the same owner.

Transcript rows come from history pages. A newly admitted prompt enters the transcript when the owner commits its turn history. To show it immediately, keep the submitted prompt in the client under its admitted run ID and reconcile it with the terminal history update. The turn controls contain IDs and status.

Summary

Functions

Reflect a Server admission refusal that requires checkpoint confirmation or recovery.

Apply an authorized chat history page. Latest pages return a reset or append. With projection: :chat, a latest page restores the transcript, cursors and active root run ID so later events for that run can update the view. With include_draft: false (the default), both drafts start empty and accumulate eligible deltas replayed after the page's cursor. With include_draft: true, the page also restores any eligible answer and commentary drafts up to that cursor and reports whether a checkpoint is pending.

Read a provisional channel as text, or :omitted on overflow.

Initialize an empty view for the Server's trusted namespace and agent ID.

Update turns, approvals, deferral summaries and the current blockage from owner reads.

Reduce an ordered Server replay batch. Events from the active root's child runs can update the candidate activity projection. Covered events and events from other emitters or scopes are ignored. A batch cursor from another emitter requires a fresh page.

Types

activity()

(candidate API)
@type activity() ::
  %{
    kind: :child_run,
    id: String.t(),
    parent_run_id: String.t(),
    status: :running | :succeeded | :failed | :cancelled | :paused | :unknown
  }
  | %{
      kind: :job,
      id: String.t(),
      tool: String.t() | nil,
      status:
        :running
        | :succeeded
        | :failed
        | :cancelled
        | :timeout
        | :expired
        | :denied
    }

blockage()

@type blockage() :: :lease | :checkpoint | :recovery | nil

cancellation()

@type cancellation() ::
  :cancelled_before_start | :cancelled | :uncertain | :unconfirmed

deferral()

@type deferral() :: %{
  :id => String.t(),
  :call_ref => String.t() | nil,
  :tool => String.t(),
  :kind => :external | :input | :timer,
  optional(:delegation_path) => ExAgent.Continuation.delegation_path(),
  optional(:wake_at) => non_neg_integer()
}

failure()

@type failure() :: %{run_id: String.t(), error: map()}

row()

@type row() :: %{id: non_neg_integer(), kind: :message | :activity, text: String.t()}

row_update()

@type row_update() :: {:reset | :append | :prepend, [row()]}

t()

@type t() :: %ExAgent.Chat{
  active_run_id: String.t() | nil,
  activities: [activity()],
  agent_id: String.t(),
  answer_draft: struct() | :omitted,
  approvals: [ExAgent.Continuation.ApprovalRequest.t()],
  before: {reference(), non_neg_integer()} | nil,
  blocked: blockage(),
  cancellation: %{run_id: String.t(), status: cancellation()} | nil,
  commentary_draft: struct() | :omitted,
  cursor: ExAgent.Event.cursor() | nil,
  deferrals: [deferral()],
  emitter_id: String.t() | nil,
  failure: failure() | nil,
  guarded?: boolean(),
  head: {reference(), non_neg_integer()} | nil,
  namespace: String.t() | nil,
  status:
    :idle
    | :running
    | :paused
    | :failed
    | :blocked
    | :reset_required
    | :superseded
    | :unavailable,
  turns: [ExAgent.Server.turn()]
}

Functions

admission_refused(chat, atom)

@spec admission_refused(t(), :checkpoint_pending | :continuation_pending) :: t()

Reflect a Server admission refusal that requires checkpoint confirmation or recovery.

apply_page(chat, page, mode \\ :latest)

@spec apply_page(t(), map(), :latest | :older) :: {t(), row_update()}

Apply an authorized chat history page. Latest pages return a reset or append. With projection: :chat, a latest page restores the transcript, cursors and active root run ID so later events for that run can update the view. With include_draft: false (the default), both drafts start empty and accumulate eligible deltas replayed after the page's cursor. With include_draft: true, the page also restores any eligible answer and commentary drafts up to that cursor and reports whether a checkpoint is pending.

Older pages return a prepend in chronological order and leave the active view and event cursor at the latest position.

draft(chat, channel \\ :answer)

@spec draft(t(), :answer | :commentary) :: String.t() | :omitted

Read a provisional channel as text, or :omitted on overflow.

new(namespace, agent_id)

@spec new(String.t() | nil, String.t()) :: t()

Initialize an empty view for the Server's trusted namespace and agent ID.

put_controls(chat, turns, pending, blocked)

@spec put_controls(t(), [ExAgent.Server.turn()], [term()], blockage()) :: t()

Update turns, approvals, deferral summaries and the current blockage from owner reads.

reduce(chat, map)

@spec reduce(t(), %{items: [ExAgent.Event.t()], cursor: ExAgent.Event.cursor()}) ::
  t()

Reduce an ordered Server replay batch. Events from the active root's child runs can update the candidate activity projection. Covered events and events from other emitters or scopes are ignored. A batch cursor from another emitter requires a fresh page.