codex

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2026 License: AGPL-3.0 Imports: 40 Imported by: 0

Documentation

Overview

Package codex implements the direct codex app-server runtime driver: it speaks the v2 protocol (internal/agent/runtime/codex/protocol) to a pinned codex CLI running inside the bot workspace, with no ACP adapter in between.

Session state is owned by codex itself under CODEX_HOME on the bot's persistent data volume; Memoh keeps only the thread id in session runtime metadata and projects the turn transcript into its own history.

Bridges codex MCP server elicitation to Memoh's ask_user decision flow. Form-mode schemas reuse the shared elicitation core (the same mapping the ACP runtime uses); url mode renders as a single confirm card. The protocol carries no thread id, so the app-server routes an elicitation to the bot's sole active turn and declines when ownership is ambiguous.

Memoh tool-gateway injection for codex threads. Each thread gets its own reverse-HTTP route into the workspace tools proxy, configured through the thread's `mcp_servers` config override at start/resume/fork. The route outlives individual turns (thread config is start-time), so the tool session identity resolves live from the thread's active turn.

Bridges codex `tool/requestUserInput` to Memoh's ask_user decision flow: codex questions render as ask_user cards, the answers map back to the question ids codex asked with, and a canceled or timed-out card degrades to the empty answer set codex treats as "proceed with defaults".

Index

Constants

View Source
const (
	AuthAPIKey  = codexcfg.AuthAPIKey
	AuthChatGPT = codexcfg.AuthChatGPT
)
View Source
const MemohToolTimeoutSec = 900

MemohToolTimeoutSec caps one Memoh gateway tool call on the codex side. Memoh tools can legitimately block on a human decision (approval cards, ask_user) for up to their wait windows; the toolmount timeout-ladder guard test pins this above them.

View Source
const (
	// RuntimeType is the thread runtime type this driver serves.
	RuntimeType = string(runtimekind.Codex)
)

Variables

View Source
var (
	ErrNotConfigured = codexcfg.ErrNotConfigured
	ParseAgentConfig = codexcfg.ParseAgentConfig
)
View Source
var ErrAuthRequired = external.ErrAuthRequired

ErrAuthRequired identifies a configured Codex runtime that still needs the user to complete account authorization in the Bot's workspace.

View Source
var ErrConnClosed = errors.New("codex app-server connection closed")

ErrConnClosed reports that the app-server connection is gone.

Functions

This section is empty.

Types

type ApprovalService

type ApprovalService interface {
	approval.FlowService
	RegisterWaiter(approvalID string) func()
}

ApprovalService is the decision flow the driver routes approvals through.

type AuthMode

type AuthMode = codexcfg.AuthMode

Configuration lives in the codexcfg leaf package so validators can import it without the driver's dependency tree; these aliases keep driver-side call sites short.

type BridgeSource

type BridgeSource interface {
	MCPClient(ctx context.Context, botID string) (*bridge.Client, error)
	WorkspaceInfo(ctx context.Context, botID string) (bridge.WorkspaceInfo, error)
}

BridgeSource resolves the workspace bridge client for a bot; the workspace manager implements it.

type Config

type Config = codexcfg.Config

Configuration lives in the codexcfg leaf package so validators can import it without the driver's dependency tree; these aliases keep driver-side call sites short.

type DeviceLoginStart

type DeviceLoginStart struct {
	LoginID         string
	UserCode        string
	VerificationURL string
}

DeviceLoginStart describes a pending ChatGPT device-code login.

type DeviceLoginStatus

type DeviceLoginStatus struct {
	// Status is pending, success, error, or unknown (login not tracked, e.g.
	// the app-server restarted).
	Status string
	Error  string
}

DeviceLoginStatus is the poll answer for a pending device-code login.

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver runs codex turns over the app-server protocol.

func NewDriver

func NewDriver(
	bridges BridgeSource,
	agents *botagents.Service,
	credentials *agentcredential.Service,
	approvalSvc ApprovalService,
	userInput UserInputService,
	toolGateway toolmount.Gateway,
	logger *slog.Logger,
) *Driver

NewDriver constructs the codex runtime driver.

func (*Driver) CancelDeviceLogin

func (d *Driver) CancelDeviceLogin(ctx context.Context, botID, botAgentID, loginID string) error

CancelDeviceLogin aborts a pending device-code login.

func (*Driver) CloseAll

func (d *Driver) CloseAll()

CloseAll tears down every app-server (server shutdown).

func (*Driver) CloseBot

func (d *Driver) CloseBot(botID string)

CloseBot recycles the bot's app-server: new work builds a fresh server on current configuration, while in-flight turns finish on the displaced one, which dies at their last release.

func (*Driver) Commands

func (*Driver) Compact

func (*Driver) CompleteChatGPTDeviceLogin

func (d *Driver) CompleteChatGPTDeviceLogin(ctx context.Context, ownerUserID, botID, botAgentID, loginID string) error

func (*Driver) ControlGoal

func (d *Driver) ControlGoal(ctx context.Context, input external.PromptInput, action string) error

func (*Driver) ForkThread

func (d *Driver) ForkThread(ctx context.Context, botID, botAgentID string, runtimeMetadata map[string]any, lastTurnID string) (map[string]any, error)

ForkThread implements external.ThreadForker: it derives a new codex thread sharing history up to lastTurnID (inclusive; empty forks at the head) and returns the runtime-metadata delta naming the forked thread. The session's runtime metadata supplies the source thread id and working directory.

func (*Driver) Goal

func (d *Driver) Goal(ctx context.Context, input external.PromptInput) (*external.Goal, error)

func (*Driver) ModelCatalog

func (d *Driver) ModelCatalog(ctx context.Context, request external.ModelCatalogRequest) (external.ModelCatalog, error)

ModelCatalog returns the models available to this Agent. API-key Agents with an explicit Base URL use that provider's OpenAI-compatible /models endpoint; ChatGPT and default-OpenAI Agents retain Codex's richer native catalog.

func (*Driver) Modes

func (*Driver) PlanMode

func (*Driver) PollDeviceLogin

func (d *Driver) PollDeviceLogin(botID, botAgentID, loginID string) DeviceLoginStatus

PollDeviceLogin reports a tracked login's state.

func (*Driver) Prompt

Prompt implements external.Driver: it runs one turn on the bot's app-server, streaming events through the sink.

func (*Driver) PurgeBotAgentAuth

func (d *Driver) PurgeBotAgentAuth(ctx context.Context, botID, botAgentID string) error

func (*Driver) ReadCommand

func (d *Driver) ReadCommand(ctx context.Context, input external.PromptInput) (external.CommandResult, error)

func (*Driver) RequiredDependency

func (*Driver) RequiredDependency() string

RequiredDependency implements external.DependencyRequirer: the codex CLI is a managed workspace dependency. No version is declared; the handshake in startAppServerSession warns when the running CLI drifts from the protocol snapshot.

func (*Driver) ResetBot

func (d *Driver) ResetBot(botID string)

func (*Driver) ResetBotAgent

func (d *Driver) ResetBotAgent(botID, botAgentID string)

func (*Driver) RuntimeType

func (*Driver) RuntimeType() string

RuntimeType implements external.Driver.

func (*Driver) SetLauncherResolver

func (d *Driver) SetLauncherResolver(r external.LauncherResolver)

SetLauncherResolver installs the workspace dependency resolver. Setter injection keeps the driver constructible without one (tests, the toolkit fallback); assembly wires it right after NewDriver.

func (*Driver) SetMode

The application persists the choice under the thread admission guard. Each new turn supplies it explicitly, including when the Codex thread is warm.

func (*Driver) SetPlanMode

func (*Driver) SetPlanMode(_ context.Context, _ external.PromptInput, mode string) (external.ModeState, error)

func (*Driver) StartChatGPTDeviceLogin

func (d *Driver) StartChatGPTDeviceLogin(ctx context.Context, botID, botAgentID string) (DeviceLoginStart, error)

StartChatGPTDeviceLogin begins a device-code login on the bot's codex, returning the code the user must enter. Completion lands in CODEX_HOME and is observable via PollDeviceLogin.

type UserInputService

type UserInputService interface {
	userinput.FlowService
}

UserInputService is the ask_user decision flow the driver routes tool/requestUserInput through.

Directories

Path Synopsis
Package codexcfg holds the Bot Agent configuration contract for the direct codex runtime.
Package codexcfg holds the Bot Agent configuration contract for the direct codex runtime.
Package protocol implements the wire contract Memoh speaks with the codex app-server: JSON-RPC 2.0 semantics over newline-delimited JSON on stdio, with the `jsonrpc` version field omitted, as the app-server does.
Package protocol implements the wire contract Memoh speaks with the codex app-server: JSON-RPC 2.0 semantics over newline-delimited JSON on stdio, with the `jsonrpc` version field omitted, as the app-server does.
Package protocolgen generates the Go types for the codex app-server v2 protocol from the vendored JSON Schema snapshot under schema/.
Package protocolgen generates the Go types for the codex app-server v2 protocol from the vendored JSON Schema snapshot under schema/.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL