ExAgent.Chat.SSE (ExAgent v3.0.0)

View Source

Optional Plug for an authorized Server chat. Mount at a conversation endpoint with the host's authentication and CSRF policy. The required :authorize function receives the connection on every request and returns {:ok, owner_pid} for the conversation or {:error, :unauthorized | :forbidden | :not_found | :unavailable}.

Stability — Candidate.

  • GET opens a conversation event stream. An absent or expired Last-Event-ID starts with a bounded snapshot.
  • POST admits a turn from a JSON object with a string "prompt". Pass the original Idempotency-Key header on each retry. Mint it with ExAgent.Server.new_admission_key/0 and ExAgent.Server.encode_admission_key/1 in the host before submission. The optional :run_options callback receives the authorized connection after key and prompt validation. Return server-side :deps, :permissions and optionally :child_events for this turn.
  • GET with Idempotency-Key reads that turn's admission receipt as JSON.
  • DELETE with a run_id query parameter asks the owner to cancel that turn. The JSON response reports whether the call was acknowledged. Read the matching event or receipt for the turn outcome.

The stream sends snapshot on initial connection, replay gaps and changes to transcript or controls. A snapshot replaces the view, including C7 approval and deferral summaries, observed activities and eligible drafts. A retained cursor behind the head replays subsequent events before a replacement snapshot. A cursor at the head starts with that snapshot. Root Server events carry a restricted payload. An activity event upserts a child or job by kind and ID, and a root run_started resets the list and can carry the initial job activity. The host authorizes and submits C7 decisions or deferred results through its application endpoints. A cursor event can advance the resume position without changing the view. The plug ends the connection on owner loss, write failure or :stream_timeout (default five minutes). Clients can reconnect to the current owner. Disconnecting leaves the turn running.

:heartbeat_ms defaults to 15 seconds and :max_body_bytes to 1 MiB. A host that parses JSON upstream must also bound its parser; this Plug checks the prompt size in parsed bodies. Uses Plug when present.