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
- Variables
- func DaemonSocket(codexHome string) string
- type API
- type Client
- func (c *Client) Close() error
- func (c *Client) Initialize(ctx context.Context) error
- func (c *Client) LoadedThreads(ctx context.Context) ([]string, error)
- func (c *Client) ReadThread(ctx context.Context, threadID string) (Thread, error)
- func (c *Client) Resume(ctx context.Context, threadID string) (Resumed, error)
- func (c *Client) Threads() <-chan ThreadEvent
- func (c *Client) Unsubscribe(ctx context.Context, threadID string) error
- func (c *Client) UpdateSettings(ctx context.Context, threadID string, s Settings) error
- func (c *Client) Updates() <-chan SettingsUpdated
- type Conn
- type Dialer
- type RPCError
- type Resumed
- type Settings
- type SettingsUpdated
- type SettingsWriter
- func (w *SettingsWriter) Enqueue(thread string, r config.Route)
- func (w *SettingsWriter) Forget(thread string)
- func (w *SettingsWriter) Reported(thread string, r config.Route)
- func (w *SettingsWriter) Resync(thread string, current config.Route)
- func (w *SettingsWriter) Run(ctx context.Context, api API)
- type Thread
- type ThreadEvent
Constants ¶
const ( MethodInitialize = "initialize" 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 ¶
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
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
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.
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 ¶
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 ¶
Initialize performs the initialize handshake with experimentalApi enabled (thread/settings/update is experimental) and sends initialized.
func (*Client) LoadedThreads ¶ added in v0.3.0
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) ReadThread ¶ added in v0.3.0
ReadThread returns a thread's metadata without its turns.
func (*Client) Resume ¶
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
Unsubscribe ends this client's subscription to a thread. The daemon unloads an idle thread only once no client is subscribed.
func (*Client) UpdateSettings ¶
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 Resumed ¶ added in v0.3.0
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 SettingsUpdated ¶
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.
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.
type ThreadEvent ¶ added in v0.3.0
ThreadEvent is a thread lifecycle or turn notification. Thread is set for thread/started only.