server

package
v1.0.29 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 59 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 server implements the Aviary HTTPS server.

Index

Constants

View Source
const UsageSourceTaskDelivery = "task_delivery"

UsageSourceTaskDelivery marks a usage record that logs where a task's or workflow's output was delivered rather than tokens spent.

Variables

View Source
var ErrReexecRequired = errors.New("server re-exec required")

ErrReexecRequired is returned by ListenAndServe after a safe restart has checkpointed every running job. The caller starts the binary at ReexecPath in place of this process.

View Source
var ErrRestartRequired = errors.New("server restart required")

ErrRestartRequired is returned by ListenAndServe when an explicit process restart was requested (for example via the daemons API).

Functions

func BearerMiddleware

func BearerMiddleware(token string, authDisabled func() bool, next http.Handler) http.Handler

BearerMiddleware enforces token authentication.

Accepts:

  • Authorization: Bearer <token>
  • aviary_session cookie
  • ?token=<token> query param (for APIs like EventSource that cannot set custom headers)

When authDisabled reports true (server.disable_token_auth), every request is let through without a token.

func GenerateToken

func GenerateToken() (string, error)

GenerateToken creates a new random token and saves it.

func InstallService

func InstallService(opts ServiceOptions) error

InstallService installs and starts a simple service for common platforms.

func IsRunning

func IsRunning() (bool, int, error)

IsRunning returns true if a process with the stored PID is alive.

func LoadOrGenerateTLS

func LoadOrGenerateTLS(t config.TLSConfig) (tls.Certificate, error)

LoadOrGenerateTLS loads the configured TLS cert+key, or the self-signed one, generating a new self-signed cert when none exists or the existing one is expired or does not cover every name in SelfSignedSANs.

func LoadOrGenerateToken

func LoadOrGenerateToken() (string, bool, error)

LoadOrGenerateToken loads the stored token or generates a new one. Returns the token and a boolean indicating if it was newly generated.

func LoadToken

func LoadToken() (string, error)

LoadToken returns the stored token, or an error if none is saved.

func LoginHandler

func LoginHandler(token string, authDisabled func() bool) http.HandlerFunc

LoginHandler handles POST /api/login — validates token, sets session cookie. When token authentication is disabled, any login succeeds without setting a session cookie.

func MinimalMode added in v0.7.8

func MinimalMode() bool

MinimalMode reports whether the minimal Code Loop-only mode is on.

func PIDPath

func PIDPath() string

PIDPath returns the path to the PID file.

func ProcessAlive added in v0.7.8

func ProcessAlive(pid int) bool

ProcessAlive reports whether the process with the given PID is running.

func ReadPID

func ReadPID() (int, error)

ReadPID reads the PID from the PID file. Returns 0 and no error if the file does not exist.

func RemovePID

func RemovePID() error

RemovePID deletes the PID file.

func SelfSignedSANs added in v0.7.3

func SelfSignedSANs(t config.TLSConfig) []string

SelfSignedSANs returns the names a generated cert must cover: localhost, the loopback addresses, the hostname and the host's current non-loopback IPv4 addresses unless t.NoHostSANs is set, and t.SANs.

func SetMinimalMode added in v0.7.8

func SetMinimalMode(on bool)

SetMinimalMode turns the minimal Code Loop-only mode on or off for the servers created after the call.

func SetWebUIEnabled added in v0.7.8

func SetWebUIEnabled(on bool)

SetWebUIEnabled controls whether the servers created after the call serve the web UI. The MCP endpoint is served either way.

func StartService

func StartService(name string) error

StartService starts the named service using the platform's service manager.

func StopService

func StopService(name string) error

StopService stops the named service using the platform's service manager.

func TLSFiles added in v0.7.3

func TLSFiles(t config.TLSConfig, regenerate bool) (certFile, keyFile string, err error)

TLSFiles returns the cert and key files the server serves with: the configured ones when set, otherwise the self-signed pair, which is generated or regenerated first when it does not cover the host's names.

func UninstallService

func UninstallService(name string) error

UninstallService removes the service registration.

func WebAssetsProblem added in v0.7.12

func WebAssetsProblem() string

WebAssetsProblem describes what is wrong with the embedded web UI, or returns "" when nothing is: no web build is embedded, or, for a backend run from a source checkout, the build is older than the web sources.

func WritePID

func WritePID() error

WritePID writes the current process PID to the PID file.

Types

type DaemonStatus

type DaemonStatus struct {
	Name       string  `json:"name"`          // display name, e.g. "aviary", "myagent/signal/0"
	Type       string  `json:"type"`          // "server", "signal", "slack", "discord"
	PID        int     `json:"pid,omitempty"` // process ID (0 = no subprocess)
	Addr       string  `json:"addr,omitempty"`
	Started    string  `json:"started"`
	Uptime     string  `json:"uptime"`
	CPUPercent float64 `json:"cpu_percent"` // -1 = unavailable
	RSSBytes   uint64  `json:"rss_bytes"`
	Status     string  `json:"status"`          // "running", "sleeping", "gone", "error", etc.
	Error      string  `json:"error,omitempty"` // last error message, set when status == "error"
	Managed    bool    `json:"managed"`         // true = aviary owns and monitors the process
}

DaemonStatus describes a single running daemon or integration service.

type ProcSampler

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

ProcSampler periodically measures CPU% and RSS for tracked PIDs. It must be refreshed via Sample on a regular interval; callers retrieve cached values via Get without blocking.

func NewProcSampler

func NewProcSampler() *ProcSampler

NewProcSampler creates an empty ProcSampler.

func (*ProcSampler) Forget

func (s *ProcSampler) Forget(pid int)

Forget removes cached state for a PID that is no longer needed.

func (*ProcSampler) Get

func (s *ProcSampler) Get(pid int) (ProcStats, bool)

Get returns the most recently cached stats for pid, or (zero, false) if not tracked.

func (*ProcSampler) Sample

func (s *ProcSampler) Sample(pids []int)

Sample reads /proc/<pid>/stat and /proc/<pid>/statm for each PID, computes CPU% as a delta from the previous call, and caches the results.

type ProcStats

type ProcStats struct {
	CPUPercent float64 `json:"cpu_percent"` // CPU usage 0-100; -1 if unavailable
	RSSBytes   uint64  `json:"rss_bytes"`   // resident set size in bytes; 0 if unavailable
	Status     string  `json:"status"`      // "running", "sleeping", "zombie", etc.
}

ProcStats holds a snapshot of process resource usage.

type Server

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

Server wraps an HTTPS server with token auth, MCP routing, and agent management.

func New

func New(cfg *config.Config, token string) *Server

New creates a new Server with the given config and auth token.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server address string.

func (*Server) Agents

func (s *Server) Agents() *agent.Manager

Agents returns the agent manager.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context) error

ListenAndServe starts the server on the configured port. It returns only when the context is cancelled, an error occurs, or an explicit process restart is requested.

func (*Server) Notifier added in v0.7.42

func (s *Server) Notifier() *notify.Service

Notifier returns the server's notification service.

func (*Server) Projects added in v0.7.0

func (s *Server) Projects() *projects.Manager

Projects returns the server's Code Loop manager.

func (*Server) ReexecPath added in v0.7.0

func (s *Server) ReexecPath() string

ReexecPath is the binary to start after ListenAndServe returns ErrReexecRequired. It is resolved before an upgrade replaces the binary, because the running executable's own path can point at the moved-aside copy.

func (*Server) RestartStatus added in v0.7.0

func (s *Server) RestartStatus() saferestart.Status

RestartStatus reports the restart phase and the jobs a restart would stop.

type ServiceOptions

type ServiceOptions struct {
	Name        string
	DisplayName string
	Description string
	Exec        string   // path to executable
	Args        []string // arguments
	WorkingDir  string
	User        string
}

ServiceOptions describes a service to install.

Jump to

Keyboard shortcuts

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