codexsock

package module
v0.0.0-...-7447fcd Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Codex socket transport

Go client and codex-send CLI for discovering and messaging sessions on a running local Codex App Server. Use a shared App Server that can see the threads you want to address.

Requires Go 1.25 or newer. Tested on Linux with Codex CLI 0.172.0. Queue submission uses the experimental App Server API. Default socket resolution follows the corresponding Codex source.

go install github.com/PeterSR/codex-socket-transport/cmd/codex-send@latest
go get github.com/PeterSR/codex-socket-transport   # the Go library
codex-send list --loaded
codex-send send --thread 'Build monitor' --message 'The build finished.' --json
codex-send rename --thread 'Build monitor' 'Release monitor'

All commands accept --remote unix:///path/to/server.sock and --timeout DURATION, which limits the operation after standard input has been read, including connecting (default 10s). codex-send --version and codex-send help print the version and usage. The default unix:// resolves to app-server-control/app-server-control.sock under CODEX_HOME or ~/.codex. For the Go API, use codexsock.Dial and close the client when finished. It supports concurrent calls and leaves reconnects to the caller.

  • list uses the server's normal saved-thread listing. list --loaded uses the loaded-thread registry. read --thread-id UUID prints one thread as JSON. probe checks that the server responds.
  • send --thread ID_OR_NAME resolves an exact name and reports ambiguity. Scripts can pin a UUID with --thread-id and use --stdin to keep the message out of the process arguments. --client-id sets the correlation ID.
  • send --json reports queued, failed, or unknown. queued includes both IDs and means the server accepted the submission. An idle thread may consume it immediately. --resume requests a separate wake-up after queue acceptance and reports a failed wake-up in resumeError. An unknown outcome exits nonzero. Check the thread before sending again.
  • steer --thread-id UUID --turn TURN_ID --message TEXT targets a known active turn. Ordinary sends use the queue.
  • The App Server has no per-message delivery receipts in 0.172.0.

In the Go API, Config.Timeout limits each call, not a whole operation. Bound an operation with the deadline of the context you pass in. Name lookup and listing support up to 10,000 threads; use a thread ID beyond that. See DESIGN.md for the protocol and error model.

Run go test -race ./... for the socket tests. Set CODEX_SOCKET_TEST_BIN to an absolute Codex executable path to include the isolated App Server test. Set CODEX_SEND_TEST_BIN to a built codex-send to exercise its JSON and stdin interface against the same server.

License

MIT, see LICENSE.

Documentation

Overview

Package codexsock is a client for a local Codex App Server. It connects over the server's Unix domain socket, speaks JSON-RPC over WebSocket, and offers thread discovery, naming, queued messages and mid-turn steering.

Use Dial to open a Client, then call its methods. A Client supports concurrent calls and never reconnects or replays a request: after the connection drops, Dial again. Queued sends report one of three outcomes (see QueueStatus), and a caller must treat an unknown outcome as possibly delivered. The App Server API is experimental and can change between Codex releases.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned (wrapped) by FindThread when no thread matches.
	ErrNotFound = errors.New("thread not found")
	// ErrAmbiguous is returned (wrapped) by FindThread when more than one
	// thread has the requested exact name.
	ErrAmbiguous = errors.New("ambiguous thread name")
)
View Source
var ErrClosed = errors.New("codexsock: client closed")

ErrClosed is returned by calls made after Close, and by calls still pending when Close runs.

Functions

func ValidThreadID

func ValidThreadID(id string) bool

ValidThreadID reports whether id has the canonical 8-4-4-4-12 hexadecimal UUID form used for Codex thread IDs.

func Version

func Version() string

Version returns the version of this module as recorded in the running binary's build information. A build reports a VCS-derived version: a tag, or a pseudo-version that may end in +dirty. It returns "dev" when no version is recorded, for example under go test.

Types

type Client

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

Client is an initialized connection to an App Server. Create it with Dial. Calls can run concurrently. After the connection closes, use Dial again; the client never replays requests.

All calls share one connection. If the context of a call expires while its request is being written, the WebSocket library closes the whole connection, which fails every concurrent call. A call whose context expires while it waits for the write lock is reported as sent (so Queue reports an unknown outcome), which is pessimistic but safe.

func Dial

func Dial(ctx context.Context, cfg Config) (*Client, error)

Dial connects to the App Server named by cfg, performs the initialize handshake and returns a ready Client. The caller must Close it. Dial fails if the socket is unreachable, the WebSocket upgrade is refused or the server rejects initialization.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection, fails pending calls with ErrClosed and waits for the reader goroutine to stop. It is safe to call more than once.

func (*Client) FindThread

func (c *Client) FindThread(ctx context.Context, idOrName string) (Thread, error)

FindThread resolves a thread by ID or exact name. An ID match wins over a name match. A name lookup lists saved and loaded threads, which takes several calls, so bound the whole operation with ctx. It supports at most 10,000 threads per listing and fails with an error beyond that; use the thread ID instead. It returns ErrNotFound when nothing matches and ErrAmbiguous when several threads share the name.

func (*Client) ListThreads

func (c *Client) ListThreads(ctx context.Context, opt ListOptions) ([]Thread, error)

ListThreads returns saved threads, or loaded threads when opt.LoadedOnly is set. It pages through the server's results with one call per page, so bound the whole operation with ctx. A listing of more than 10,000 threads fails with an error rather than returning a partial result.

func (*Client) Probe

func (c *Client) Probe(ctx context.Context) error

Probe makes one inexpensive read-only call to check that the server responds.

func (*Client) Queue

func (c *Client) Queue(ctx context.Context, id string, msg Message, opt QueueOptions) (QueueResult, error)

Queue adds one text message to the queue of the thread with the given ID. The result is always populated, including on error. An unknown status means the message may have been accepted, so a caller should keep the correlation ID and avoid blind retries.

func (*Client) ReadThread

func (c *Client) ReadThread(ctx context.Context, id string) (Thread, error)

ReadThread returns the metadata of the thread with the given ID, without its turns. It rejects an invalid thread ID before contacting the server.

func (*Client) Rename

func (c *Client) Rename(ctx context.Context, id, name string) error

Rename sets the name of the thread with the given ID. It rejects an invalid thread ID and an empty name before contacting the server.

func (*Client) Resume

func (c *Client) Resume(ctx context.Context, id string) error

Resume asks the server to resume the thread with the given ID without loading its turns. It rejects an invalid thread ID before contacting the server.

func (*Client) Steer

func (c *Client) Steer(ctx context.Context, id, turnID, text string) (string, error)

Steer adds text to the active turn turnID of the thread with the given ID. The server rejects it when that turn is not the active one. It returns the turn ID the server confirmed.

type Config

type Config struct {
	// Address is the App Server endpoint. It must be "unix://PATH" for an
	// explicit socket. An empty string or "unix://" selects the default
	// socket under $CODEX_HOME, or ~/.codex when CODEX_HOME is unset. Other
	// schemes are rejected.
	Address string
	// Timeout is a per-call limit, not an operation limit. It bounds the
	// connection handshake and each individual RPC call, so an operation that
	// makes several calls (such as FindThread) can take longer in total. Bound
	// a whole operation with the deadline of the context passed to it. A zero
	// or negative value means 5 seconds.
	Timeout time.Duration
}

Config configures Dial.

type ListOptions

type ListOptions struct {
	// LoadedOnly lists only threads currently loaded in the server's memory
	// instead of the saved-thread listing.
	LoadedOnly bool
}

ListOptions selects which threads ListThreads returns.

type Message

type Message struct {
	// Text is the message body. It must contain a non-blank character.
	Text string
	// ClientMessageID is an optional caller-chosen correlation ID. Queue
	// generates one when it is empty.
	ClientMessageID string
}

Message is the text to deliver with Queue.

type QueueOptions

type QueueOptions struct {
	// Resume sends a separate thread resume request after the server has
	// accepted the message. A failed resume does not change the queue status;
	// it is reported in QueueResult.ResumeError.
	Resume bool
}

QueueOptions adjusts Queue.

type QueueResult

type QueueResult struct {
	// Status is the outcome. See the QueueStatus constants.
	Status QueueStatus `json:"status"`
	// ClientMessageID is the correlation ID sent with the message.
	ClientMessageID string `json:"clientMessageId,omitempty"`
	// SubmissionID is the server's ID for the queued submission when Status
	// is QueueAccepted.
	SubmissionID string `json:"submissionId,omitempty"`
	// ResumeError describes a failed resume request after an accepted queue.
	ResumeError string `json:"resumeError,omitempty"`
}

QueueResult is the structured outcome of Queue.

type QueueStatus

type QueueStatus string

QueueStatus is the outcome of Queue.

const (
	// QueueAccepted ("queued") means the server accepted the submission and
	// returned a matching submission ID. It does not mean the thread has
	// started or finished processing the message.
	QueueAccepted QueueStatus = "queued"
	// QueueUnknown ("unknown") means the request may have reached the server,
	// for example after a timeout, a disconnect or an unusable reply, so the
	// message may or may not be queued. Check the thread before sending
	// again; do not retry blindly.
	QueueUnknown QueueStatus = "unknown"
	// QueueFailed ("failed") means the message was definitely not queued:
	// invalid input, a cancelled context before anything was written, or a
	// request the server rejected as invalid.
	QueueFailed QueueStatus = "failed"
)

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

RPCError is a JSON-RPC error object returned by the App Server. Use errors.As to inspect Code.

func (*RPCError) Error

func (e *RPCError) Error() string

type Thread

type Thread struct {
	ID        string       `json:"id"`
	Name      string       `json:"name,omitempty"`
	CWD       string       `json:"cwd,omitempty"`
	CreatedAt int64        `json:"createdAt,omitempty"`
	UpdatedAt int64        `json:"updatedAt,omitempty"`
	Status    ThreadStatus `json:"status"`
}

Thread describes one Codex thread as reported by the App Server.

type ThreadStatus

type ThreadStatus struct {
	Type        string   `json:"type,omitempty"`
	ActiveFlags []string `json:"activeFlags,omitempty"`
}

ThreadStatus is the runtime status of a thread. Type is a server-defined value such as "notLoaded", "idle" or "active"; ActiveFlags qualify an active thread.

Directories

Path Synopsis
cmd
codex-send command
internal
fakeserver
Package fakeserver is a test helper that serves a minimal Codex App Server over a Unix socket.
Package fakeserver is a test helper that serves a minimal Codex App Server over a Unix socket.

Jump to

Keyboard shortcuts

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