serve

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package serve exposes a control.Controller over HTTP: the typed event stream as Server-Sent Events, and the commands as small JSON POST endpoints. It is a second frontend alongside the chat TUI — proof that the controller is transport-agnostic, and the basis for a browser/desktop client. One server drives one session; multiple browser tabs share it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broadcaster

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

Broadcaster is the event.Sink the controller emits to in server mode. It marshals each event once and fans it out to every connected SSE subscriber. A slow subscriber's buffer is allowed to drop rather than back-pressure the agent goroutine — a browser that can't keep up loses intermediate frames, not the whole session (it can refetch /history).

func NewBroadcaster

func NewBroadcaster() *Broadcaster

NewBroadcaster returns an empty Broadcaster ready to accept subscribers.

func (*Broadcaster) Emit

func (b *Broadcaster) Emit(e event.Event)

Emit marshals the event to JSON and delivers it to every subscriber. Drops to a subscriber whose buffer is full rather than blocking. A marshal failure is dropped silently — one bad event shouldn't stall the stream.

func (*Broadcaster) Subscribe

func (b *Broadcaster) Subscribe() (<-chan []byte, func())

Subscribe registers a new SSE client and returns its channel plus an unsubscribe func the handler must call (defer) when the client disconnects.

func (*Broadcaster) Subscribers

func (b *Broadcaster) Subscribers() int

Subscribers reports the current connection count (for diagnostics/tests).

type Server

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

Server wires a controller to its HTTP surface. The Broadcaster must be the same sink the controller was constructed with, so events reach SSE clients.

func New

func New(ctrl *control.Controller, bc *Broadcaster) *Server

New builds a Server. bc must be the controller's event sink.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the HTTP routes: GET / (a minimal browser client), GET /events (SSE), GET /history, GET /context, and POST command endpoints. CORS is NOT applied by default — same-origin policy protects the unauthenticated agent endpoints. Call HandlerWithCORS to opt in for local development.

func (*Server) HandlerWithCORS

func (s *Server) HandlerWithCORS(origin string) http.Handler

HandlerWithCORS returns the same routes as Handler but adds permissive CORS headers so a dev frontend on a different origin (e.g. Vite on :5173) can reach the server. Do NOT use in production — the server has no auth.

func (*Server) Run

func (s *Server) Run(addr string) error

Run serves until the process is killed. Interactive approval is enabled so "ask" decisions surface as approval_request events answered via POST /approve.

func (*Server) RunGraceful

func (s *Server) RunGraceful(ctx context.Context, addr string) error

RunGraceful serves with graceful shutdown. It listens for SIGINT/SIGTERM on the provided context and drains active connections for up to 10 seconds before returning.

Jump to

Keyboard shortcuts

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