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 withon_exit, and pingson_heartbeatat a fixed interval while the process lives (session sweeps can key off that) send_stdin/3lets 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/2opens a long-lived port running-p --input-format stream-json:opencode—Agentic.Protocol.OpenCode.build_config/1plusturn_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
Is the session's owning process alive?
@spec list_models(:claude_code | :opencode) :: [String.t()]
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).
@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_mswhile alive:heartbeat_ms— heartbeat cadence (default 30_000):stderr_to_stdout— merge stderr into the chunk stream (default true)
@spec stop(String.t()) :: :ok | {:error, :session_not_active}
Tear down a session — the owner closes the port on its way out.