Documentation
¶
Overview ¶
Package codexsup drives `codex app-server` (target: Codex 0.156.1) over its stdio JSON-RPC connection: it starts the process, performs the handshake, and starts, resumes and interrupts turns on one Codex thread. Server notifications reach the caller raw; server requests (approval prompts and the rest) reach a caller handler, or are declined by default — no default path ever accepts.
The wire contract is codex_app_server_protocol.schemas.json (see SCHEMA.md). Framing is internal/acp's Transport, reused unchanged.
Params are never logged: they carry prompt text, commands and file contents.
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) Done() <-chan struct{}
- func (c *Client) Err() error
- func (c *Client) Interrupt(ctx context.Context, turnID string) error
- func (c *Client) LatestModels(ctx context.Context) ([]turnevent.ModelOption, error)
- func (c *Client) ResumeThread(ctx context.Context, threadID string) error
- func (c *Client) SignedIn(ctx context.Context) (bool, error)
- func (c *Client) StartThread(ctx context.Context) (string, error)
- func (c *Client) StartTurn(ctx context.Context, in TurnInput) (string, error)
- func (c *Client) Stop(ctx context.Context) error
- func (c *Client) ThreadID() string
- func (c *Client) UserAgent() string
- func (c *Client) Version() string
- type Config
- type ServerRequest
- type Translator
- type TurnInput
Constants ¶
const ( ApprovalGranular = "granular" ApprovalOnRequest = "on-request" ApprovalNever = "never" )
TurnInput.ApprovalPolicy values. Granular is sent as the object that turns every sandbox block, rule prompt and MCP elicitation into an approval request; the others as their string.
const ( SandboxReadOnly = "readOnly" SandboxWorkspaceWrite = "workspaceWrite" SandboxDangerFullAccess = "dangerFullAccess" )
TurnInput.Sandbox values: the type tag of turn/start's sandboxPolicy.
const ( ReviewerUser = "user" ReviewerAutoReview = "auto_review" )
TurnInput.ApprovalsReviewer values.
Variables ¶
var ErrExited = errors.New("codexsup: app-server exited")
ErrExited is wrapped by every call that fails because the app-server process is gone, whether the caller stopped it or it died on its own.
var ErrNoThread = errors.New("codexsup: no thread")
ErrNoThread is returned by StartTurn and Interrupt before a thread was started or resumed.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a connection to one running app-server process.
func Start ¶
Start launches `<Binary> app-server` and performs the handshake: initialize, then the initialized notification. ctx bounds the handshake only; the process runs until Stop or its own exit.
func (*Client) Done ¶
func (c *Client) Done() <-chan struct{}
Done is closed once the process has exited and its output has been read.
func (*Client) Err ¶
Err is the process's exit error once Done is closed (nil on a clean exit), and nil before.
func (*Client) Interrupt ¶
Interrupt asks Codex to stop the running turn turnID on the client's thread. The turn ends with a turn/completed notification.
func (*Client) LatestModels ¶ added in v0.27.0
LatestModels reads every page of model/list and returns the newest version of each model family, in order of the family's first appearance: Value is the family, ResolvedModel the newest version's id and EffortLevels that version's advertised levels in order. An entry whose id is not shaped gpt-<version>-<family> is left out. Hidden models are not asked for.
Each page is folded into a table of at most maxModelFamilies before the next is read. A read that fails part-way returns an error and no list, so a family seen only on a later page is never dropped silently.
func (*Client) ResumeThread ¶
ResumeThread resumes the thread with id, without its turn history, and makes it the client's thread.
func (*Client) SignedIn ¶ added in v0.27.0
SignedIn reports whether the Codex home can run a turn, from account/read. It is false only when Codex answers no account and requiresOpenaiAuth true; a provider needing no OpenAI sign-in is signed in without an account. The account's fields (an email address among them) are never decoded.
func (*Client) StartThread ¶
StartThread starts a thread in the configured directory and returns the id Codex minted, which becomes the client's thread.
func (*Client) StartTurn ¶
StartTurn starts a turn on the client's thread and returns its id. The turn's progress arrives through OnNotification.
func (*Client) Stop ¶
Stop closes the process's stdin, on which app-server exits, and waits for the exit. If ctx ends first the process is sent SIGTERM, then SIGKILL after killGrace. It returns Err.
func (*Client) ThreadID ¶
ThreadID is the client's current thread, empty before one was started or resumed.
type Config ¶
type Config struct {
// Binary is the codex executable; Dir its working directory; CodexHome
// its CODEX_HOME. All three are required.
Binary string
Dir string
CodexHome string
// ClientVersion is sent as clientInfo.version; empty sends "dev".
ClientVersion string
// OnNotification receives every server notification, raw and in arrival
// order. It runs on the connection's read loop: it must not block, and
// must not call Client methods (their responses are read by that loop).
// params is untrusted input.
OnNotification func(method string, params json.RawMessage)
// OnServerRequest receives every server request under the same read-loop
// rules. It must answer each request exactly once, now or later from any
// goroutine. Nil declines every request (see ServerRequest.Decline).
OnServerRequest func(req *ServerRequest)
// Stderr receives the process's stderr; nil discards it.
Stderr io.Writer
// Log receives diagnostics; nil uses slog.Default().
Log *slog.Logger
}
Config configures one app-server process.
type ServerRequest ¶
type ServerRequest struct {
Method string
// ID is the request's JSON-RPC id, which serverRequest/resolved names as
// requestId when the app-server stops waiting for the answer.
ID json.RawMessage
// Params is the request's params as the app-server sent them. It is
// untrusted input and may carry commands, paths and file contents.
Params json.RawMessage
// contains filtered or unexported fields
}
ServerRequest is one request the app-server sent to the client (a frame carrying both id and method), such as an approval prompt. It must be answered exactly once, by Respond or Decline, from any goroutine; until it is, the Codex turn that raised it waits.
func (*ServerRequest) Decline ¶
func (r *ServerRequest) Decline() error
Decline answers the request with the package's default: a decline for the approval methods, a JSON-RPC error for everything else. It never accepts.
func (*ServerRequest) Respond ¶
func (r *ServerRequest) Respond(result any) error
Respond answers the request with result. A second answer writes nothing and returns acp.ErrAlreadyResolved.
type Translator ¶ added in v0.27.0
type Translator struct {
// contains filtered or unexported fields
}
Translator maps Codex server notifications onto turn events: the Codex twin of streamsup's parser. It holds per-turn state (usage, reroute target) and is not safe for concurrent use; call it from OnNotification, which runs on one goroutine.
Params are untrusted. Text is carried, never interpreted; every string the translator builds from them other than text deltas is bounded here. A tool call's input, output, paths and diffs are carried as streamsup carries tool input and tool_result text, bounded by acp's line cap and capped by turnbridge on the way to the wire.
func NewTranslator ¶ added in v0.27.0
func NewTranslator(model string) *Translator
NewTranslator returns a translator for turns running on model, the model the caller sets on each turn (TurnInput.Model).
func (*Translator) SetModel ¶ added in v0.27.0
func (t *Translator) SetModel(model string)
SetModel changes the model later turns run on.
func (*Translator) Translate ¶ added in v0.27.0
func (t *Translator) Translate(method string, params json.RawMessage) []turnevent.Event
Translate maps one server notification to the events it means, in order. An ignored method yields none; a method or item type with no mapping yields one Unrecognized.
type TurnInput ¶
type TurnInput struct {
Text string
Model string
Effort string
ApprovalPolicy string
Sandbox string
ApprovalsReviewer string
}
TurnInput is one turn's text input and its per-turn overrides. Codex keeps each override for the thread's later turns, so an empty field leaves the thread's setting unchanged rather than resetting it.