Agentic.CLI.StreamSession (agentic v0.4.0)

Copy Markdown

Spawn-and-stream session runner for CLI agent protocols.

The Protocol modules are turn-synchronous by design: send/3 blocks until the final result envelope and the caller never sees intermediate chunks. Harnesses that watch an agent work (control planes, desktops, transcripts) need the opposite: spawn once, stream every chunk as it arrives, and optionally inject extra stdin turns (answers/steer) while the CLI is running.

This module owns that lifecycle:

  • the session's port is opened inside a dedicated owner task, so the capture loop always owns the port and its lifetime
  • a capture loop streams chunks into on_chunk, wraps up with on_exit, and pings on_heartbeat at a fixed interval while the process lives (session sweeps can key off that)
  • send_stdin/3 lets the harness inject further user envelopes into a still-open stdin (claude stream-json); opencode turns are per-process one-shots by protocol design, so only fresh sessions accept prompts there
  • all protocol knowledge (CLI args, model flags, gateway env, the stream-json envelope format, the bubblewrap sandbox) stays inside the protocol modules — StreamSession is transport only.

Per-protocol spawn shapes come from agentic itself:

  • :claude_code — Agentic.Protocol.ClaudeCode.start/2 opens a long-lived port running -p --input-format stream-json
  • :opencode — Agentic.Protocol.OpenCode.build_config/1 plus turn_spawn_args/2: sh -c 'exec opencode … "$AGENTIC_PROMPT" </dev/null>' (the CLI reads stdin to EOF, so the prompt must be an env var and stdin must terminate)

Example

{:ok, session_id} =
  Agentic.CLI.StreamSession.start(:claude_code, %{model: model},
    %{metadata: %{workspace: workspace}},
    initial_prompt: "fix the failing test",
    on_chunk: fn chunk -> Log.append(chunk) end,
    on_exit: fn status -> Log.finish(status) end,
    on_heartbeat: fn -> Registry.ping() end
  )

extra =
  Agentic.Protocol.ClaudeCode.format_messages(
    [%{"role" => "user", "content" => "use port 4000"}],
    %{}
  )

Agentic.CLI.StreamSession.send_stdin(session_id, extra, "

")

Summary

Functions

Is the session's owning process alive?

Delegate to the protocol's own model catalogue (list_models/0).

Protocol.OpenCode.list_models_grouped/0 — models grouped by provider (authenticated subscriptions first) for optgroup-style pickers.

Push extra content into a session's open stdin (claude stream-json).

Spawn a session and start streaming. Returns {:ok, session_id}.

Tear down a session — the owner closes the port on its way out.

Functions

alive?(session_id)

@spec alive?(String.t()) :: boolean()

Is the session's owning process alive?

list_models(atom)

@spec list_models(:claude_code | :opencode) :: [String.t()]

Delegate to the protocol's own model catalogue (list_models/0).

list_models_grouped(atom)

@spec list_models_grouped(:opencode) :: [{String.t(), [String.t()]}]

Protocol.OpenCode.list_models_grouped/0 — models grouped by provider (authenticated subscriptions first) for optgroup-style pickers.

send_stdin(session_id, iodata, suffix \\ [])

@spec send_stdin(String.t(), iodata(), iodata()) ::
  :ok | {:error, :session_not_active}

Push extra content into a session's open stdin (claude stream-json).

start(kind, backend_config, ctx, opts \\ [])

@spec start(:claude_code | :opencode, map(), map(), keyword()) ::
  {:ok, String.t()} | {:error, term()}

Spawn a session and start streaming. Returns {:ok, session_id}.

Options

  • :initial_prompt — first prompt; wrapped per protocol (claude: a stream-json user envelope over stdin; opencode: $AGENTIC_PROMPT)
  • :on_chunk — (binary() -> any()), every stdout/stderr chunk
  • :on_exit — (status :: integer() | :closed -> any())

  • :on_heartbeat — (-> any()), called every :heartbeat_ms while alive
  • :heartbeat_ms — heartbeat cadence (default 30_000)
  • :stderr_to_stdout — merge stderr into the chunk stream (default true)

stop(session_id)

@spec stop(String.t()) :: :ok | {:error, :session_not_active}

Tear down a session — the owner closes the port on its way out.