Documentation
¶
Overview ¶
Package godex provides an idiomatic Go SDK for orchestrating Codex agents through the Codex CLI.
Index ¶
- Variables
- func RunJSON[T any](ctx context.Context, thread *Thread, input string, options *RunJSONOptions[T]) (T, error)
- type AgentMessageItem
- type ApprovalMode
- type Codex
- type CodexOptions
- type CommandExecutionItem
- type CommandExecutionStatus
- type ErrorItem
- type FileChangeItem
- type FileUpdateChange
- type InputSegment
- type ItemCompletedEvent
- type ItemStartedEvent
- type ItemUpdatedEvent
- type McpToolCallItem
- type McpToolCallStatus
- type PatchApplyStatus
- type PatchChangeKind
- type ReasoningItem
- type RunJSONOptions
- type RunResult
- type RunStreamedJSONResult
- type RunStreamedJSONUpdate
- type RunStreamedResult
- type SandboxMode
- type SchemaViolationError
- type Stream
- type StreamCallbacks
- type StreamCommandEvent
- type StreamErrorItemEvent
- type StreamFileChangeEvent
- type StreamItemStage
- type StreamMessageEvent
- type StreamPatchEvent
- type StreamReasoningEvent
- type StreamTodoListEvent
- type StreamToolCallEvent
- type StreamWebSearchEvent
- type Thread
- func (t *Thread) ID() string
- func (t *Thread) Run(ctx context.Context, input string, turnOptions *TurnOptions) (RunResult, error)
- func (t *Thread) RunInputs(ctx context.Context, segments []InputSegment, turnOptions *TurnOptions) (RunResult, error)
- func (t *Thread) RunStreamed(ctx context.Context, input string, turnOptions *TurnOptions) (RunStreamedResult, error)
- func (t *Thread) RunStreamedInputs(ctx context.Context, segments []InputSegment, turnOptions *TurnOptions) (RunStreamedResult, error)
- type ThreadError
- type ThreadErrorEvent
- type ThreadEvent
- type ThreadEventType
- type ThreadItem
- type ThreadItemType
- type ThreadOptions
- type ThreadStartedEvent
- type ThreadStreamError
- type TodoItem
- type TodoListItem
- type Turn
- type TurnCompletedEvent
- type TurnFailedEvent
- type TurnOptions
- type TurnStartedEvent
- type Usage
- type WebSearchItem
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoStructuredOutput indicates that the turn completed without returning a structured // response that could be decoded into the requested type. ErrNoStructuredOutput = errors.New("structured output not returned") )
Functions ¶
Types ¶
type AgentMessageItem ¶
type AgentMessageItem struct {
ID string `json:"id"`
Type string `json:"type"`
Text string `json:"text"`
}
AgentMessageItem contains the model's response payload (natural language or structured JSON).
type ApprovalMode ¶
type ApprovalMode string
ApprovalMode describes how the Codex CLI should request approval for actions that might require user consent. The Codex CLI itself interprets these values, the SDK merely forwards them when provided.
const ( ApprovalModeNever ApprovalMode = "never" ApprovalModeOnRequest ApprovalMode = "on-request" ApprovalModeOnFailure ApprovalMode = "on-failure" ApprovalModeUntrusted ApprovalMode = "untrusted" )
type Codex ¶
type Codex struct {
// contains filtered or unexported fields
}
Codex is the entrypoint for interacting with the Codex agent via the CLI.
func New ¶
func New(options CodexOptions) (*Codex, error)
New constructs a Codex SDK instance. The Codex binary is discovered automatically unless CodexOptions.CodexPathOverride is provided.
func (*Codex) ResumeThread ¶
func (c *Codex) ResumeThread(id string, options ThreadOptions) *Thread
ResumeThread recreates a thread using a previously obtained thread identifier.
func (*Codex) StartThread ¶
func (c *Codex) StartThread(options ThreadOptions) *Thread
StartThread opens a new thread with the agent.
type CodexOptions ¶
type CodexOptions struct {
// CodexPathOverride allows specifying the path to a Codex binary instead of the bundled one.
CodexPathOverride string
// BaseURL overrides the service endpoint used by the Codex CLI.
BaseURL string
// APIKey optionally overrides authentication for the Codex CLI. When empty, the CLI
// falls back to its own configured credentials (e.g. environment variables or auth login).
APIKey string
// ConfigOverrides forwards CLI configuration overrides as `-c key=value` pairs. When
// the `profile` key is present it is emitted as `--profile <value>` instead.
ConfigOverrides map[string]any
}
CodexOptions configure the SDK itself rather than an individual thread.
type CommandExecutionItem ¶
type CommandExecutionItem struct {
ID string `json:"id"`
Type string `json:"type"`
Command string `json:"command"`
AggregatedOutput string `json:"aggregated_output"`
ExitCode *int `json:"exit_code,omitempty"`
Status CommandExecutionStatus `json:"status"`
}
CommandExecutionItem captures a command execution requested by the agent.
type CommandExecutionStatus ¶
type CommandExecutionStatus string
CommandExecutionStatus represents the lifecycle stage of a command started by the agent.
const ( CommandExecutionStatusInProgress CommandExecutionStatus = "in_progress" CommandExecutionStatusCompleted CommandExecutionStatus = "completed" CommandExecutionStatusFailed CommandExecutionStatus = "failed" )
type ErrorItem ¶
type ErrorItem struct {
ID string `json:"id"`
Type string `json:"type"`
Message string `json:"message"`
}
ErrorItem captures non-fatal errors emitted by the agent.
type FileChangeItem ¶
type FileChangeItem struct {
ID string `json:"id"`
Type string `json:"type"`
Changes []FileUpdateChange `json:"changes"`
Status PatchApplyStatus `json:"status"`
}
FileChangeItem aggregates the set of file updates that comprise a patch.
type FileUpdateChange ¶
type FileUpdateChange struct {
Path string `json:"path"`
Kind PatchChangeKind `json:"kind"`
}
FileUpdateChange represents a single file edit made by the agent.
type InputSegment ¶ added in v0.0.2
type InputSegment struct {
// Text holds a natural-language prompt fragment. Leave empty to indicate the
// segment references an image instead.
Text string
// LocalImagePath contains a filesystem path to an image that should be
// forwarded to the CLI via --image. Leave empty for text segments.
LocalImagePath string
// contains filtered or unexported fields
}
InputSegment represents a piece of user-provided input sent to the Codex CLI. Exactly one of Text or LocalImagePath must be populated.
func BytesImageSegment ¶ added in v0.0.6
func BytesImageSegment(name string, data []byte) (InputSegment, error)
BytesImageSegment writes the provided image bytes to a temporary file and returns a segment that references it. The file is cleaned up automatically when the run finishes.
func LocalImageSegment ¶ added in v0.0.2
func LocalImageSegment(path string) InputSegment
LocalImageSegment creates an input segment pointing at a local image file. The path is forwarded to the Codex CLI using repeated --image flags.
func TextSegment ¶ added in v0.0.2
func TextSegment(text string) InputSegment
TextSegment creates a textual input segment. Multiple text segments are concatenated with blank lines between them, matching the TypeScript SDK's behaviour.
func URLImageSegment ¶ added in v0.0.6
func URLImageSegment(ctx context.Context, rawURL string) (InputSegment, error)
URLImageSegment downloads an image from the provided URL into a temporary file and returns an input segment that references it. The file is cleaned up automatically when the run finishes.
type ItemCompletedEvent ¶
type ItemCompletedEvent struct {
Type ThreadEventType `json:"type"`
Item ThreadItem `json:"item"`
}
ItemCompletedEvent signals an item reaching a terminal state.
func (ItemCompletedEvent) EventType ¶
func (e ItemCompletedEvent) EventType() ThreadEventType
type ItemStartedEvent ¶
type ItemStartedEvent struct {
Type ThreadEventType `json:"type"`
Item ThreadItem `json:"item"`
}
ItemStartedEvent emits when a thread item is created.
func (ItemStartedEvent) EventType ¶
func (e ItemStartedEvent) EventType() ThreadEventType
type ItemUpdatedEvent ¶
type ItemUpdatedEvent struct {
Type ThreadEventType `json:"type"`
Item ThreadItem `json:"item"`
}
ItemUpdatedEvent emits as an item transitions between intermediate states.
func (ItemUpdatedEvent) EventType ¶
func (e ItemUpdatedEvent) EventType() ThreadEventType
type McpToolCallItem ¶
type McpToolCallItem struct {
ID string `json:"id"`
Type string `json:"type"`
Server string `json:"server"`
Tool string `json:"tool"`
Status McpToolCallStatus `json:"status"`
}
McpToolCallItem represents activity relating to a single MCP tool call.
type McpToolCallStatus ¶
type McpToolCallStatus string
McpToolCallStatus describes the status of an MCP tool invocation.
const ( McpToolCallStatusInProgress McpToolCallStatus = "in_progress" McpToolCallStatusCompleted McpToolCallStatus = "completed" McpToolCallStatusFailed McpToolCallStatus = "failed" )
type PatchApplyStatus ¶
type PatchApplyStatus string
PatchApplyStatus indicates whether the patch was applied successfully.
const ( PatchApplyStatusCompleted PatchApplyStatus = "completed" PatchApplyStatusFailed PatchApplyStatus = "failed" )
type PatchChangeKind ¶
type PatchChangeKind string
PatchChangeKind indicates how a file changed.
const ( PatchChangeKindAdd PatchChangeKind = "add" PatchChangeKindDelete PatchChangeKind = "delete" PatchChangeKindUpdate PatchChangeKind = "update" )
type ReasoningItem ¶
type ReasoningItem struct {
ID string `json:"id"`
Type string `json:"type"`
Text string `json:"text"`
}
ReasoningItem provides insight into the agent's intermediate reasoning.
type RunJSONOptions ¶ added in v0.0.6
type RunJSONOptions[T any] struct { // TurnOptions forwards additional options for the turn. When nil a zero TurnOptions // value is used. TurnOptions *TurnOptions // Schema provides an explicit JSON schema for the structured output. When nil the // helper attempts schema inference unless DisableSchemaInference is true. Schema any // DisableSchemaInference prevents automatic schema inference from T when Schema is nil. DisableSchemaInference bool }
RunJSONOptions configure a typed JSON turn.
type RunResult ¶
type RunResult = Turn
RunResult is an alias for Turn to mirror the TypeScript SDK naming.
type RunStreamedJSONResult ¶ added in v0.0.6
type RunStreamedJSONResult[T any] struct { // contains filtered or unexported fields }
RunStreamedJSONResult exposes the streaming lifecycle for a typed structured output turn.
func RunStreamedJSON ¶ added in v0.0.6
func RunStreamedJSON[T any](ctx context.Context, thread *Thread, input string, options *RunJSONOptions[T]) (RunStreamedJSONResult[T], error)
RunStreamedJSON executes a turn expecting structured JSON output and streams raw events alongside typed snapshots decoded into T.
func (RunStreamedJSONResult[T]) Close ¶ added in v0.0.6
func (r RunStreamedJSONResult[T]) Close() error
Close cancels the turn and waits for shutdown.
func (RunStreamedJSONResult[T]) Events ¶ added in v0.0.6
func (r RunStreamedJSONResult[T]) Events() <-chan ThreadEvent
Events returns the stream of raw thread events produced by the turn.
func (RunStreamedJSONResult[T]) Updates ¶ added in v0.0.6
func (r RunStreamedJSONResult[T]) Updates() <-chan RunStreamedJSONUpdate[T]
Updates yields typed structured output snapshots. The channel closes once the turn finishes.
func (RunStreamedJSONResult[T]) Wait ¶ added in v0.0.6
func (r RunStreamedJSONResult[T]) Wait() error
Wait blocks until the turn finishes and returns the terminal error, if any.
type RunStreamedJSONUpdate ¶ added in v0.0.6
RunStreamedJSONUpdate captures a typed snapshot of the structured output as the turn progresses.
type RunStreamedResult ¶
type RunStreamedResult struct {
// contains filtered or unexported fields
}
RunStreamedResult is returned by Thread.RunStreamed and exposes the event stream.
func (RunStreamedResult) Close ¶
func (r RunStreamedResult) Close() error
Close cancels the stream context and waits for shutdown.
func (RunStreamedResult) Events ¶
func (r RunStreamedResult) Events() <-chan ThreadEvent
Events returns the channel that yields events sequentially as they arrive.
func (RunStreamedResult) Wait ¶
func (r RunStreamedResult) Wait() error
Wait blocks until the stream finishes and returns the terminal error, if any.
type SandboxMode ¶
type SandboxMode string
SandboxMode mirrors the CLI sandbox configuration that controls which filesystem operations the agent may perform.
const ( SandboxModeReadOnly SandboxMode = "read-only" SandboxModeWorkspaceWrite SandboxMode = "workspace-write" SandboxModeDangerFullAccess SandboxMode = "danger-full-access" )
type SchemaViolationError ¶ added in v0.0.6
type SchemaViolationError struct {
Message string
}
SchemaViolationError indicates that the structured output failed schema validation.
func (*SchemaViolationError) Error ¶ added in v0.0.6
func (e *SchemaViolationError) Error() string
Error implements the error interface.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream is an internal helper that coordinates the lifecycle of a streaming turn.
func (*Stream) Events ¶
func (s *Stream) Events() <-chan ThreadEvent
type StreamCallbacks ¶ added in v0.0.7
type StreamCallbacks struct {
// OnEvent fires for every event before any type-specific callback.
OnEvent func(ThreadEvent)
OnThreadStarted func(ThreadStartedEvent)
OnTurnStarted func(TurnStartedEvent)
OnTurnCompleted func(TurnCompletedEvent)
OnTurnFailed func(TurnFailedEvent)
OnThreadError func(ThreadErrorEvent)
OnMessage func(StreamMessageEvent)
OnReasoning func(StreamReasoningEvent)
OnCommand func(StreamCommandEvent)
OnPatch func(StreamPatchEvent)
OnFileChange func(StreamFileChangeEvent)
OnWebSearch func(StreamWebSearchEvent)
OnToolCall func(StreamToolCallEvent)
OnTodoList func(StreamTodoListEvent)
OnErrorItem func(StreamErrorItemEvent)
}
StreamCallbacks enumerates optional hooks invoked when streaming events are delivered.
type StreamCommandEvent ¶ added in v0.0.7
type StreamCommandEvent struct {
Stage StreamItemStage
Command CommandExecutionItem
}
StreamCommandEvent describes a callback payload for command execution items.
type StreamErrorItemEvent ¶ added in v0.0.7
type StreamErrorItemEvent struct {
Stage StreamItemStage
Error ErrorItem
}
StreamErrorItemEvent describes a callback payload for non-fatal error items.
type StreamFileChangeEvent ¶ added in v0.0.7
type StreamFileChangeEvent struct {
Stage StreamItemStage
Patch FileChangeItem
Change FileUpdateChange
}
StreamFileChangeEvent describes a callback payload for each file updated within a patch.
type StreamItemStage ¶ added in v0.0.7
type StreamItemStage string
StreamItemStage indicates which phase of the lifecycle produced a callback.
const ( StreamItemStageStarted StreamItemStage = "started" StreamItemStageUpdated StreamItemStage = "updated" StreamItemStageCompleted StreamItemStage = "completed" )
type StreamMessageEvent ¶ added in v0.0.7
type StreamMessageEvent struct {
Stage StreamItemStage
Message AgentMessageItem
}
StreamMessageEvent describes a callback payload for agent message items.
type StreamPatchEvent ¶ added in v0.0.7
type StreamPatchEvent struct {
Stage StreamItemStage
Patch FileChangeItem
}
StreamPatchEvent describes a callback payload for patch/file change items.
type StreamReasoningEvent ¶ added in v0.0.7
type StreamReasoningEvent struct {
Stage StreamItemStage
Reasoning ReasoningItem
}
StreamReasoningEvent describes a callback payload for reasoning items.
type StreamTodoListEvent ¶ added in v0.0.7
type StreamTodoListEvent struct {
Stage StreamItemStage
List TodoListItem
}
StreamTodoListEvent describes a callback payload for todo list items.
type StreamToolCallEvent ¶ added in v0.0.7
type StreamToolCallEvent struct {
Stage StreamItemStage
ToolCall McpToolCallItem
}
StreamToolCallEvent describes a callback payload for MCP tool call items.
type StreamWebSearchEvent ¶ added in v0.0.7
type StreamWebSearchEvent struct {
Stage StreamItemStage
Search WebSearchItem
}
StreamWebSearchEvent describes a callback payload for web search items.
type Thread ¶
type Thread struct {
// contains filtered or unexported fields
}
Thread encapsulates a conversation with the Codex agent. It is safe to reuse a Thread across sequential turns, but concurrent Run/RunStreamed calls on the same Thread are not supported.
func (*Thread) ID ¶
ID returns the identifier of the thread. For new threads this becomes available after the first `thread.started` event is received.
func (*Thread) Run ¶
func (t *Thread) Run(ctx context.Context, input string, turnOptions *TurnOptions) (RunResult, error)
Run submits the input to the agent and waits for the turn to finish, returning the final response.
func (*Thread) RunInputs ¶ added in v0.0.2
func (t *Thread) RunInputs(ctx context.Context, segments []InputSegment, turnOptions *TurnOptions) (RunResult, error)
RunInputs mirrors Run but accepts structured input segments.
func (*Thread) RunStreamed ¶
func (t *Thread) RunStreamed(ctx context.Context, input string, turnOptions *TurnOptions) (RunStreamedResult, error)
RunStreamed submits the provided input to the agent and streams events as they occur.
func (*Thread) RunStreamedInputs ¶ added in v0.0.2
func (t *Thread) RunStreamedInputs(ctx context.Context, segments []InputSegment, turnOptions *TurnOptions) (RunStreamedResult, error)
RunStreamedInputs behaves like RunStreamed but accepts structured input segments, allowing callers to mix multiple text fragments and local image paths.
type ThreadError ¶
type ThreadError struct {
Message string `json:"message"`
}
ThreadError represents a fatal error emitted for the turn.
type ThreadErrorEvent ¶
type ThreadErrorEvent struct {
Type ThreadEventType `json:"type"`
Message string `json:"message"`
}
ThreadErrorEvent is emitted when the stream itself experiences an unrecoverable error.
func (ThreadErrorEvent) EventType ¶
func (e ThreadErrorEvent) EventType() ThreadEventType
type ThreadEvent ¶
type ThreadEvent interface {
EventType() ThreadEventType
// contains filtered or unexported methods
}
ThreadEvent is the interface implemented by all event variants returned by the CLI.
type ThreadEventType ¶
type ThreadEventType string
ThreadEventType enumerates the JSON event types streamed by the Codex CLI.
const ( ThreadEventTypeThreadStarted ThreadEventType = "thread.started" ThreadEventTypeTurnStarted ThreadEventType = "turn.started" ThreadEventTypeTurnCompleted ThreadEventType = "turn.completed" ThreadEventTypeTurnFailed ThreadEventType = "turn.failed" ThreadEventTypeItemStarted ThreadEventType = "item.started" ThreadEventTypeItemUpdated ThreadEventType = "item.updated" ThreadEventTypeItemCompleted ThreadEventType = "item.completed" ThreadEventTypeError ThreadEventType = "error" )
type ThreadItem ¶
type ThreadItem interface {
// contains filtered or unexported methods
}
ThreadItem is the polymorphic representation of all items that can appear on a thread.
type ThreadItemType ¶
type ThreadItemType string
ThreadItemType enumerates all valid thread item type strings.
const ( ThreadItemTypeAgentMessage ThreadItemType = "agent_message" ThreadItemTypeReasoning ThreadItemType = "reasoning" ThreadItemTypeCommandExecution ThreadItemType = "command_execution" ThreadItemTypeFileChange ThreadItemType = "file_change" ThreadItemTypeMcpToolCall ThreadItemType = "mcp_tool_call" ThreadItemTypeWebSearch ThreadItemType = "web_search" ThreadItemTypeTodoList ThreadItemType = "todo_list" ThreadItemTypeError ThreadItemType = "error" )
ThreadItemType constants.
type ThreadOptions ¶
type ThreadOptions struct {
// Model specifies the model identifier to use for the thread.
Model string
// SandboxMode controls the CLI sandbox setting (equivalent to `--sandbox` flag).
SandboxMode SandboxMode
// WorkingDirectory sets the working directory for the agent (`--cd` flag).
WorkingDirectory string
// SkipGitRepoCheck mirrors the CLI flag `--skip-git-repo-check`.
SkipGitRepoCheck bool
}
ThreadOptions configure how the CLI executes a particular thread.
type ThreadStartedEvent ¶
type ThreadStartedEvent struct {
Type ThreadEventType `json:"type"`
ThreadID string `json:"thread_id"`
}
ThreadStartedEvent is emitted when a new thread is created.
func (ThreadStartedEvent) EventType ¶
func (e ThreadStartedEvent) EventType() ThreadEventType
type ThreadStreamError ¶ added in v0.0.3
type ThreadStreamError struct {
ThreadError
}
ThreadStreamError wraps a thread-level error emitted by the Codex CLI. It is returned when Run/RunInputs encounter a `thread.error` event or when RunStreamedResult.Wait is invoked after such an event.
func (*ThreadStreamError) Error ¶ added in v0.0.3
func (e *ThreadStreamError) Error() string
Error implements the error interface.
type TodoListItem ¶
type TodoListItem struct {
ID string `json:"id"`
Type string `json:"type"`
Items []TodoItem `json:"items"`
}
TodoListItem tracks the set of tasks managed by the agent during a turn.
type Turn ¶
type Turn struct {
Items []ThreadItem
FinalResponse string
Usage *Usage
}
Turn represents a fully completed turn from the Codex agent.
type TurnCompletedEvent ¶
type TurnCompletedEvent struct {
Type ThreadEventType `json:"type"`
Usage Usage `json:"usage"`
}
TurnCompletedEvent indicates a successful completion of a turn.
func (TurnCompletedEvent) EventType ¶
func (e TurnCompletedEvent) EventType() ThreadEventType
type TurnFailedEvent ¶
type TurnFailedEvent struct {
Type ThreadEventType `json:"type"`
Error ThreadError `json:"error"`
}
TurnFailedEvent signals that a turn ended due to a fatal error.
func (TurnFailedEvent) EventType ¶
func (e TurnFailedEvent) EventType() ThreadEventType
type TurnOptions ¶
type TurnOptions struct {
// OutputSchema is an optional JSON schema describing the structured response to
// collect from the agent. Must serialize to a JSON object (not an array or primitive).
OutputSchema any
// Callbacks attaches optional streaming callbacks invoked as events arrive.
Callbacks *StreamCallbacks
}
TurnOptions configure a single turn executed within a thread.
type TurnStartedEvent ¶
type TurnStartedEvent struct {
Type ThreadEventType `json:"type"`
}
TurnStartedEvent marks the beginning of a new turn.
func (TurnStartedEvent) EventType ¶
func (e TurnStartedEvent) EventType() ThreadEventType
type Usage ¶
type Usage struct {
InputTokens int `json:"input_tokens"`
CachedInputTokens int `json:"cached_input_tokens"`
OutputTokens int `json:"output_tokens"`
}
Usage captures token consumption metrics for a completed turn.
type WebSearchItem ¶
type WebSearchItem struct {
ID string `json:"id"`
Type string `json:"type"`
Query string `json:"query"`
}
WebSearchItem denotes a web search performed by the agent.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
images
command
|
|
|
schema
command
|
|
|
streaming
command
|
|
|
streaming_callbacks
command
|
|
|
structured_output
command
|
|
|
internal
|
|