Documentation
¶
Overview ¶
Package codex is an unofficial Go SDK for the OpenAI Codex agent.
It mirrors the official TypeScript SDK (@openai/codex-sdk): it spawns the `codex` CLI with `exec --experimental-json`, writes the prompt to stdin and streams structured JSONL events back over stdout.
Basic usage:
client, err := codex.New(nil)
if err != nil {
log.Fatal(err)
}
thread := client.StartThread(nil)
turn, err := thread.Run(ctx, "Diagnose the test failure and propose a fix", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(turn.FinalResponse)
Call Run repeatedly on the same Thread to continue the conversation, or use RunStreamed to react to events as they are produced.
Index ¶
- Variables
- type Codex
- type StreamedTurn
- type Thread
- func (t *Thread) ID() string
- func (t *Thread) Run(ctx context.Context, prompt string, turnOptions *types.TurnOptions) (*Turn, error)
- func (t *Thread) RunInputs(ctx context.Context, inputs []types.UserInput, turnOptions *types.TurnOptions) (*Turn, error)
- func (t *Thread) RunStreamed(ctx context.Context, prompt string, turnOptions *types.TurnOptions) (*StreamedTurn, error)
- func (t *Thread) RunStreamedInputs(ctx context.Context, inputs []types.UserInput, turnOptions *types.TurnOptions) (*StreamedTurn, error)
- type Turn
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("codex: streamed turn closed")
ErrClosed is returned by StreamedTurn.Err and StreamedTurn.Close when Close stopped a turn that was still in progress. It is distinct from the context errors so callers can tell a deliberate close from an upstream cancellation.
var ErrIncompleteTurn = errors.New("codex: stream ended without a terminal turn event")
ErrIncompleteTurn means the process exited successfully without reporting turn.completed or turn.failed.
var Version = strings.TrimSpace(versionFile)
Version is the SDK version, read from the VERSION file at build time.
Functions ¶
This section is empty.
Types ¶
type Codex ¶
type Codex struct {
// contains filtered or unexported fields
}
Codex is the entry point for interacting with the Codex agent. Create one with New, then use StartThread or ResumeThread.
func New ¶
func New(options *types.CodexOptions) (*Codex, error)
New creates a Codex client. Passing nil uses default options. The codex executable is located at construction time; a *types.CLINotFoundError is returned if it cannot be found.
func (*Codex) ExecutablePath ¶
ExecutablePath returns the resolved path of the codex binary.
func (*Codex) ResumeThread ¶
func (c *Codex) ResumeThread(id string, options *types.ThreadOptions) *Thread
ResumeThread resumes a previously started thread by id. Threads are persisted by codex in ~/.codex/sessions.
func (*Codex) StartThread ¶
func (c *Codex) StartThread(options *types.ThreadOptions) *Thread
StartThread starts a new conversation with the agent. Passing nil uses default thread options. The thread id is populated once the first turn starts.
type StreamedTurn ¶
type StreamedTurn struct {
// contains filtered or unexported fields
}
StreamedTurn is the result of Thread.RunStreamed. Read events from Events until it is closed, then call Err to learn how the turn ended.
func (*StreamedTurn) Close ¶ added in v0.1.1
func (s *StreamedTurn) Close() error
Close releases the turn and waits for cleanup. If the turn is still in progress the codex process (and, on Unix, its whole process group) is killed and Close returns ErrClosed. If turn.completed or turn.failed has already been delivered, codex is left to exit on its own (so the session is persisted) and Close returns the turn's terminal error, exactly like Err. Close is safe to call more than once and from a goroutine other than the one reading Events.
func (*StreamedTurn) Err ¶
func (s *StreamedTurn) Err() error
Err blocks until the stream has finished and returns the terminal error: nil on success, *types.ExecError if codex exited non-zero, *types.ParseError on malformed output, ErrIncompleteTurn on missing terminal events, the context error on cancellation, or ErrClosed if Close stopped the turn.
func (*StreamedTurn) Events ¶
func (s *StreamedTurn) Events() <-chan types.ThreadEvent
Events yields thread events as they are produced. It is closed when the turn ends, the process exits, the context is cancelled, or Close is called.
type Thread ¶
type Thread struct {
// contains filtered or unexported fields
}
Thread represents a conversation with the agent. One thread can have multiple consecutive turns. A Thread is safe to reuse sequentially; do not run turns on the same Thread concurrently.
func (*Thread) Run ¶
func (t *Thread) Run(ctx context.Context, prompt string, turnOptions *types.TurnOptions) (*Turn, error)
Run sends a text prompt to the agent and returns the completed turn.
func (*Thread) RunInputs ¶
func (t *Thread) RunInputs(ctx context.Context, inputs []types.UserInput, turnOptions *types.TurnOptions) (*Turn, error)
RunInputs sends structured input (text and local images) to the agent and returns the completed turn.
func (*Thread) RunStreamed ¶
func (t *Thread) RunStreamed(ctx context.Context, prompt string, turnOptions *types.TurnOptions) (*StreamedTurn, error)
RunStreamed sends a text prompt to the agent and streams events as they are produced.
func (*Thread) RunStreamedInputs ¶
func (t *Thread) RunStreamedInputs(ctx context.Context, inputs []types.UserInput, turnOptions *types.TurnOptions) (*StreamedTurn, error)
RunStreamedInputs sends structured input to the agent and streams events as they are produced.
type Turn ¶
type Turn struct {
// Items are all items that completed during the turn, in order.
Items []types.ThreadItem
// FinalResponse is the text of the last agent_message item (JSON when
// structured output was requested).
FinalResponse string
// Usage is the token usage reported for the turn; nil if not reported.
Usage *types.Usage
}
Turn is a completed turn of a thread.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
resume_thread
command
Example: multi-turn conversation and resuming a persisted thread by id.
|
Example: multi-turn conversation and resuming a persisted thread by id. |
|
simple_run
command
Example: one-shot turn with the Codex agent.
|
Example: one-shot turn with the Codex agent. |
|
streaming
command
Example: stream events as the agent works.
|
Example: stream events as the agent works. |
|
structured_output
command
Example: request JSON output that conforms to a schema.
|
Example: request JSON output that conforms to a schema. |
|
with_images
command
Example: send text together with local images.
|
Example: send text together with local images. |
|
internal
|
|
|
config
Package config serializes structured Codex config overrides into the `--config key=value` TOML-literal form the codex CLI expects.
|
Package config serializes structured Codex config overrides into the `--config key=value` TOML-literal form the codex CLI expects. |
|
log
Package log provides a minimal verbosity-gated logger for the SDK.
|
Package log provides a minimal verbosity-gated logger for the SDK. |
|
transport
Package transport spawns the codex CLI and streams its JSONL output.
|
Package transport spawns the codex CLI and streams its JSONL output. |
|
Package types contains the public type definitions for the Codex SDK: thread events, thread items, configuration options and error types.
|
Package types contains the public type definitions for the Codex SDK: thread events, thread items, configuration options and error types. |