hotswap

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package hotswap provides workspace change detection and live reload hooks.

It wires file/polling watchers with adaptors that refresh runtime finders and registries when workspace resources change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action describes what happened to a workspace resource.

const (
	// ActionAddOrUpdate indicates a resource was created or modified.
	ActionAddOrUpdate Action = iota
	// ActionDelete indicates a resource was removed.
	ActionDelete
)

type Adaptor

type Adaptor[T any] struct {
	// contains filtered or unexported fields
}

Adaptor bridges a workspace loader and a finder using the Reloadable interface.

func NewAdaptor

func NewAdaptor[T any](load LoaderFunc[T], set SetFunc[T], remove RemoveFunc) *Adaptor[T]

NewAdaptor creates a generic adaptor.

func (*Adaptor[T]) OnChange

func (a *Adaptor[T]) OnChange(ctx context.Context, ch Change) error

OnChange handles a workspace resource change.

type Change

type Change struct {
	Kind   string // workspace kind (e.g. "agents", "models", "embedders")
	Name   string // bare resource name without extension
	Action Action
}

Change represents a single workspace resource mutation.

type FSWatcher

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

FSWatcher implements Watcher using fsnotify for filesystem-based workspaces.

func NewFSWatcher

func NewFSWatcher(root string, opts ...WatcherOption) *FSWatcher

NewFSWatcher creates an FS-based watcher rooted at the given workspace directory.

func (*FSWatcher) Close

func (w *FSWatcher) Close() error

Close releases the underlying fsnotify watcher.

func (*FSWatcher) Watch

func (w *FSWatcher) Watch(ctx context.Context, kinds []string, onChange func(Change)) error

Watch begins observing the specified workspace kinds (subdirectories) and calls onChange for each detected YAML file mutation. It blocks until ctx is cancelled.

type LoaderFunc

type LoaderFunc[T any] func(ctx context.Context, name string) (T, error)

LoaderFunc loads a resource by name.

type Manager

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

Manager coordinates watchers and dispatches changes to registered Reloadables.

func New

func New(watcher Watcher, opts ...Option) *Manager

New creates a Manager that uses the given Watcher.

func (*Manager) Register

func (m *Manager) Register(kind string, r Reloadable)

Register adds a Reloadable handler for the specified workspace kind.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) error

Start begins watching and dispatching. It spawns a goroutine and returns immediately. Call Stop to shut down.

func (*Manager) Stop

func (m *Manager) Stop()

Stop cancels the watcher and waits for the dispatch loop to exit.

type Option

type Option func(*Manager)

Option configures Manager.

type PollingOption

type PollingOption func(*PollingWatcher)

PollingOption configures a PollingWatcher.

func WithPollInterval

func WithPollInterval(d time.Duration) PollingOption

WithPollInterval sets the polling interval.

type PollingWatcher

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

PollingWatcher implements Watcher for non-FS stores by periodically polling Store.Entries and comparing snapshots to detect changes.

func NewPollingWatcher

func NewPollingWatcher(store workspace.Store, opts ...PollingOption) *PollingWatcher

NewPollingWatcher creates a watcher that polls the given store.

func (*PollingWatcher) Close

func (w *PollingWatcher) Close() error

Close is a no-op for the polling watcher.

func (*PollingWatcher) Watch

func (w *PollingWatcher) Watch(ctx context.Context, kinds []string, onChange func(Change)) error

Watch polls the store at the configured interval and calls onChange when resources are added, updated, or deleted. It blocks until ctx is cancelled.

type Reloadable

type Reloadable interface {
	OnChange(ctx context.Context, change Change) error
}

Reloadable handles workspace resource changes for a specific kind.

func NewAgentAdaptor

func NewAgentAdaptor(loader agent.Loader, finder *agentfinder.Finder) Reloadable

NewAgentAdaptor creates a Reloadable that reloads agents on workspace changes.

func NewEmbedderAdaptor

func NewEmbedderAdaptor(loader *embedderloader.Service, finder *embedderfinder.Finder) Reloadable

NewEmbedderAdaptor creates a Reloadable that reloads embedder configs on workspace changes.

func NewModelAdaptor

func NewModelAdaptor(loader *modelloader.Service, finder *modelfinder.Finder) Reloadable

NewModelAdaptor creates a Reloadable that reloads model configs on workspace changes.

type RemoveFunc

type RemoveFunc func(name string)

RemoveFunc removes a resource from the finder.

type SetFunc

type SetFunc[T any] func(name string, v T)

SetFunc injects a loaded resource into the finder.

type Watcher

type Watcher interface {
	// Watch begins observing the given workspace kinds and delivers changes to
	// the provided callback. It blocks until ctx is cancelled or an
	// unrecoverable error occurs.
	Watch(ctx context.Context, kinds []string, onChange func(Change)) error

	// Close releases watcher resources.
	Close() error
}

Watcher is an abstraction over the mechanism that detects workspace changes. An FS implementation uses fsnotify; a DB implementation might poll or use change streams.

type WatcherOption

type WatcherOption func(*FSWatcher)

WatcherOption configures FSWatcher.

func WithDebounce

func WithDebounce(d time.Duration) WatcherOption

WithDebounce sets the debounce duration for the FS watcher.

Jump to

Keyboard shortcuts

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