appserver

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 16 Imported by: 0 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.

Documentation

Overview

Package appserver is a second client of a Codex app-server: the session's private one in stage 1, or a home's shared daemon in stage 2. It speaks JSON-RPC 2.0 over a Dialer (WebSocket over the unix socket per research §3.6), initializes with experimentalApi, resumes each thread (thread/resume with excludeTurns) so it receives that thread's notifications, writes thread/settings/update so the Codex footer shows the routed model, and surfaces thread/settings/updated notifications so the router can tell its own echo from a manual /model pin. For the daemon it also lists and reads loaded threads, unsubscribes, and surfaces thread and turn notifications.

Index

Constants

View Source
const (
	MethodInitialize      = "initialize"
	MethodConfigRead      = "config/read"
	MethodInitialized     = "initialized"
	MethodResume          = "thread/resume"
	MethodSettingsUpdate  = "thread/settings/update"
	MethodSettingsUpdated = "thread/settings/updated"
	MethodLoadedList      = "thread/loaded/list"
	MethodRead            = "thread/read"
	MethodUnsubscribe     = "thread/unsubscribe"
	MethodThreadStarted   = "thread/started"
	MethodThreadClosed    = "thread/closed"
	MethodTurnStarted     = "turn/started"
	MethodTurnCompleted   = "turn/completed"
)

JSON-RPC method names used.

Variables

View Source
var ErrClosed = errors.New("appserver: connection closed")

ErrClosed is returned by calls made on, or pending at, a closed connection.

Functions

func DaemonSocket added in v0.3.0

func DaemonSocket(codexHome string) string

DaemonSocket is the control socket of the shared daemon of the Codex home at codexHome. Codex makes it a symlink into a short per-user tmp dir.

Types

type API

type API interface {
	Initialize(ctx context.Context) error
	Provider(ctx context.Context) (string, error)
	Resume(ctx context.Context, threadID string) (Resumed, error)
	UpdateSettings(ctx context.Context, threadID string, s Settings) error
	Updates() <-chan SettingsUpdated
	Close() error
}

API is what the launcher uses; Client implements it.

func Dial

func Dial(ctx context.Context, socketPath string, d Dialer, log *slog.Logger) (API, error)

Dial connects to the app-server socket through d and starts reading. The client logs to log; nil means slog.Default(). Call Initialize before any other request.

type Client

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

Client is a JSON-RPC client over one Conn. A caller's ctx that ends while its call waits for the write slot or the reply abandons only that call; it does not interrupt a write in progress. A write that does not finish within writeTimeout closes the connection, because a partly written message leaves the stream unusable.

func DialDaemon added in v0.3.0

func DialDaemon(ctx context.Context, socketPath string, d Dialer, log *slog.Logger) (*Client, error)

DialDaemon is Dial for a home's shared daemon: the client also yields thread lifecycle and turn notifications on Threads.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection, fails pending calls with ErrClosed and waits for the client's goroutines to stop. It returns the first error from closing the connection.

func (*Client) Initialize

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

Initialize performs the initialize handshake with experimentalApi enabled (thread/settings/update is experimental) and sends initialized.

func (*Client) LoadedThreads added in v0.3.0

func (c *Client) LoadedThreads(ctx context.Context) ([]string, error)

LoadedThreads returns the ids of the threads the app-server holds in memory, following thread/loaded/list's cursor to the last page.

func (*Client) Provider added in v0.10.1

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

Provider reads the effective provider without requesting configuration layers. Startup flags can be overridden by managed configuration.

func (*Client) ReadThread added in v0.3.0

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

ReadThread returns a thread's metadata without its turns.

func (*Client) Resume

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

Resume subscribes this client to a thread's turn and settings notifications with thread/resume {threadId, excludeTurns: true}; the app-server sends them only to clients that resumed the thread. A thread that cannot be resumed, such as one without a rollout (-32600 "no rollout found"), comes back as an *RPCError the caller may ignore: settings writes to it still work.

func (*Client) Threads added in v0.3.0

func (c *Client) Threads() <-chan ThreadEvent

Threads yields thread/started, thread/closed, turn/started and turn/completed in arrival order, and is closed when the connection ends. It is nil unless the client came from DialDaemon. Like Updates it must be drained independently of the consumer's own RPC calls.

func (*Client) Unsubscribe added in v0.3.0

func (c *Client) Unsubscribe(ctx context.Context, threadID string) error

Unsubscribe ends this client's subscription to a thread. The daemon unloads an idle thread only once no client is subscribed.

func (*Client) UpdateSettings

func (c *Client) UpdateSettings(ctx context.Context, threadID string, s Settings) error

UpdateSettings sets the thread's model and effort for subsequent turns. An empty field is omitted, which leaves that setting unchanged.

func (*Client) Updates

func (c *Client) Updates() <-chan SettingsUpdated

Updates yields thread/settings/updated notifications in arrival order. It is closed when the connection ends.

type Conn

type Conn interface {
	Read(ctx context.Context) ([]byte, error)
	Write(ctx context.Context, msg []byte) error
	Close() error
}

Conn is one message-oriented connection to the app-server. Cancelling the ctx of a Read or Write may close the connection.

type Dialer

type Dialer interface {
	Dial(ctx context.Context, socketPath string) (Conn, error)
}

Dialer opens a Conn to the app-server socket.

func WebSocket

func WebSocket() Dialer

WebSocket returns the Dialer for Codex's app-server unix socket transport: a WebSocket handshake over the socket, one JSON-RPC message per text frame.

type RPCError

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

RPCError is a JSON-RPC error response from the app-server.

func (*RPCError) Error

func (e *RPCError) Error() string

type Resumed added in v0.3.0

type Resumed struct {
	Settings Settings
	Cwd      string
}

Resumed is what thread/resume returns: the thread's current settings and cwd. An empty Effort is the model's default effort, not a missing route.

type Settings

type Settings struct {
	Model  string        `json:"model"`
	Effort config.Effort `json:"effort"`
}

Settings are the thread settings the router writes and reads.

type SettingsUpdated

type SettingsUpdated struct {
	ThreadID string
	Settings Settings
}

SettingsUpdated is a thread/settings/updated notification.

type SettingsWriter added in v0.3.1

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

SettingsWriter mirrors routes into the Codex footer. Per thread it keeps only the newest wanted route (the engine accepts an echo of any later write as covering earlier ones), and at write time skips a route equal to the thread's current one: the last route written or reported by the app-server. Counting reports matters: after a manual /model pick and a resume, writing the router's old route again must not be skipped.

func NewSettingsWriter added in v0.3.1

func NewSettingsWriter(log *slog.Logger, resume bool) *SettingsWriter

NewSettingsWriter returns a writer. With resume it resumes each thread once, before its first write, so this client gets the thread's notifications; a caller that subscribes threads itself passes false.

func (*SettingsWriter) Enqueue added in v0.3.1

func (w *SettingsWriter) Enqueue(thread string, r config.Route)

Enqueue asks for r in thread's footer. It never blocks.

func (*SettingsWriter) Forget added in v0.3.1

func (w *SettingsWriter) Forget(thread string)

Forget drops everything known about thread, queued write included.

func (*SettingsWriter) Reported added in v0.3.1

func (w *SettingsWriter) Reported(thread string, r config.Route)

Reported records a thread/settings/updated value.

func (*SettingsWriter) Resync added in v0.3.1

func (w *SettingsWriter) Resync(thread string, current config.Route)

Resync takes the route an app-server reports for thread after a (re)subscribe and queues the last wanted route again, so a restarted app-server's footer catches up. Nothing is written when they match.

func (*SettingsWriter) Run added in v0.3.1

func (w *SettingsWriter) Run(ctx context.Context, api API)

Run writes queued routes through api until ctx ends. A caller that reconnects runs it again with the new api; the queue carries over.

type Thread added in v0.3.0

type Thread struct {
	ID           string          `json:"id"`
	Cwd          string          `json:"cwd"`
	Ephemeral    bool            `json:"ephemeral"`
	ParentID     *string         `json:"parentThreadId"`
	Source       json.RawMessage `json:"source"`
	ThreadSource *string         `json:"threadSource"`
}

Thread is the part of a Codex thread the brain reads.

func (Thread) Main added in v0.3.0

func (t Thread) Main() bool

Main reports whether t is a thread the user started: not ephemeral, not a subagent, and of the user's thread source or none. Side threads such as thread_title are ephemeral and of another source.

type ThreadEvent added in v0.3.0

type ThreadEvent struct {
	Method   string
	ThreadID string
	Thread   *Thread
}

ThreadEvent is a thread lifecycle or turn notification. Thread is set for thread/started only.

Jump to

Keyboard shortcuts

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