Agentic. Protocol. OpenCode
(agentic v0.4.0)
Copy Markdown
OpenCode CLI protocol implementation.
Drives the real opencode CLI one-shot per turn: the turn prompt is
passed as the final positional argument and the CLI's JSON event stream
(opencode run --format json) is parsed for the assistant text. Session
continuity uses opencode's own session ids (-s <sessionID>) captured
from the event stream — each turn spawns a fresh process, because the
CLI reads stdin to EOF and a port-held stdin pipe can never deliver it.
Usage
# Register the protocol
Agentic.Protocol.Registry.register(:opencode, __MODULE__)
# Use in a session
{:ok, session_id} = Agentic.Protocol.OpenCode.start(config, context)
{:ok, response} = Agentic.Protocol.OpenCode.resume(session_id, messages, context)
Summary
Functions
Provider ids the local opencode CLI is authenticated with, from its
auth.json (checked in the usual locations). These are the user's actual
subscriptions; list_models/0 uses them to order the catalog.
Callback implementation for Agentic.AgentProtocol.estimate_cost/1.
Flatten the turn's messages into the plain-text prompt the CLI expects:
an optional system prompt, then the latest user message. Prior turns are
omitted — opencode's own session history carries them (via -s).
Callback implementation for Agentic.AgentProtocol.get_usage/1.
list_models/0 grouped by provider, preserving first-appearance order
(authenticated providers first, see authenticated_providers/0).
Statelessly map one complete JSON event line (compatibility helper — the turn collector does its own line-buffered accumulation).
Callback implementation for Agentic.AgentProtocol.stream_message/3.
Spawn shape for of a one-shot OpenCode turn, mirroring this module's own
turn runner: {/bin/sh, ["-c", script], env_list}.
Functions
Provider ids the local opencode CLI is authenticated with, from its
auth.json (checked in the usual locations). These are the user's actual
subscriptions; list_models/0 uses them to order the catalog.
Callback implementation for Agentic.AgentProtocol.estimate_cost/1.
Flatten the turn's messages into the plain-text prompt the CLI expects:
an optional system prompt, then the latest user message. Prior turns are
omitted — opencode's own session history carries them (via -s).
Callback implementation for Agentic.AgentProtocol.get_usage/1.
list_models/0 grouped by provider, preserving first-appearance order
(authenticated providers first, see authenticated_providers/0).
Returns [{provider, [model_id, ...]}, ...] — the shape UI pickers render
as optgroups so every configured subscription is discoverable, not just
whichever provider sorts first.
Statelessly map one complete JSON event line (compatibility helper — the turn collector does its own line-buffered accumulation).
Callback implementation for Agentic.AgentProtocol.stream_message/3.
Spawn shape for of a one-shot OpenCode turn, mirroring this module's own
turn runner: {/bin/sh, ["-c", script], env_list}.
OpenCode reads stdin to EOF, so a port-held stdin pipe can never deliver
it — the prompt must travel via the AGENTIC_PROMPT env var and the shell
closes stdin with </dev/null:
exec <exe> <args> $AGENTIC_PROMPT </dev/null
The port opts the caller should use: [:stream, :binary, :exit_status, :stderr_to_stdout, {:args, args}, {:cd, workspace}, {:env, env_list}].