backend.go

  1// Package backend provides transport-agnostic operations for managing
  2// workspaces, sessions, agents, permissions, and events. It is consumed
  3// by protocol-specific layers such as HTTP (server) and ACP.
  4package backend
  5
  6import (
  7	"context"
  8	"errors"
  9	"fmt"
 10	"log/slog"
 11	"runtime"
 12
 13	"github.com/charmbracelet/crush/internal/app"
 14	"github.com/charmbracelet/crush/internal/config"
 15	"github.com/charmbracelet/crush/internal/csync"
 16	"github.com/charmbracelet/crush/internal/db"
 17	"github.com/charmbracelet/crush/internal/proto"
 18	"github.com/charmbracelet/crush/internal/ui/util"
 19	"github.com/charmbracelet/crush/internal/version"
 20	"github.com/google/uuid"
 21)
 22
 23// Common errors returned by backend operations.
 24var (
 25	ErrWorkspaceNotFound       = errors.New("workspace not found")
 26	ErrLSPClientNotFound       = errors.New("LSP client not found")
 27	ErrAgentNotInitialized     = errors.New("agent coordinator not initialized")
 28	ErrPathRequired            = errors.New("path is required")
 29	ErrInvalidPermissionAction = errors.New("invalid permission action")
 30	ErrUnknownCommand          = errors.New("unknown command")
 31)
 32
 33// ShutdownFunc is called when the backend needs to trigger a server
 34// shutdown (e.g. when the last workspace is removed).
 35type ShutdownFunc func()
 36
 37// Backend provides transport-agnostic business logic for the Crush
 38// server. It manages workspaces and delegates to [app.App] services.
 39type Backend struct {
 40	workspaces *csync.Map[string, *Workspace]
 41	cfg        *config.ConfigStore
 42	ctx        context.Context
 43	shutdownFn ShutdownFunc
 44}
 45
 46// Workspace represents a running [app.App] workspace with its
 47// associated resources and state.
 48type Workspace struct {
 49	*app.App
 50	ID   string
 51	Path string
 52	Cfg  *config.ConfigStore
 53	Env  []string
 54}
 55
 56// New creates a new [Backend].
 57func New(ctx context.Context, cfg *config.ConfigStore, shutdownFn ShutdownFunc) *Backend {
 58	return &Backend{
 59		workspaces: csync.NewMap[string, *Workspace](),
 60		cfg:        cfg,
 61		ctx:        ctx,
 62		shutdownFn: shutdownFn,
 63	}
 64}
 65
 66// GetWorkspace retrieves a workspace by ID.
 67func (b *Backend) GetWorkspace(id string) (*Workspace, error) {
 68	ws, ok := b.workspaces.Get(id)
 69	if !ok {
 70		return nil, ErrWorkspaceNotFound
 71	}
 72	return ws, nil
 73}
 74
 75// ListWorkspaces returns all running workspaces.
 76func (b *Backend) ListWorkspaces() []proto.Workspace {
 77	workspaces := []proto.Workspace{}
 78	for _, ws := range b.workspaces.Seq2() {
 79		workspaces = append(workspaces, workspaceToProto(ws))
 80	}
 81	return workspaces
 82}
 83
 84// CreateWorkspace initializes a new workspace from the given
 85// parameters. It creates the config, database connection, and
 86// [app.App] instance.
 87func (b *Backend) CreateWorkspace(args proto.Workspace) (*Workspace, proto.Workspace, error) {
 88	if args.Path == "" {
 89		return nil, proto.Workspace{}, ErrPathRequired
 90	}
 91
 92	id := uuid.New().String()
 93	cfg, err := config.Init(args.Path, args.DataDir, args.Debug)
 94	if err != nil {
 95		return nil, proto.Workspace{}, fmt.Errorf("failed to initialize config: %w", err)
 96	}
 97
 98	cfg.Overrides().SkipPermissionRequests = args.YOLO
 99
100	if err := createDotCrushDir(cfg.Config().Options.DataDirectory); err != nil {
101		return nil, proto.Workspace{}, fmt.Errorf("failed to create data directory: %w", err)
102	}
103
104	conn, err := db.Connect(b.ctx, cfg.Config().Options.DataDirectory)
105	if err != nil {
106		return nil, proto.Workspace{}, fmt.Errorf("failed to connect to database: %w", err)
107	}
108
109	appWorkspace, err := app.New(b.ctx, conn, cfg)
110	if err != nil {
111		return nil, proto.Workspace{}, fmt.Errorf("failed to create app workspace: %w", err)
112	}
113
114	ws := &Workspace{
115		App:  appWorkspace,
116		ID:   id,
117		Path: args.Path,
118		Cfg:  cfg,
119		Env:  args.Env,
120	}
121
122	b.workspaces.Set(id, ws)
123
124	if args.Version != "" && args.Version != version.Version {
125		slog.Warn(
126			"Client/server version mismatch",
127			"client", args.Version,
128			"server", version.Version,
129		)
130		appWorkspace.SendEvent(util.NewWarnMsg(fmt.Sprintf(
131			"Server version %q differs from client version %q. Consider restarting the server.",
132			version.Version, args.Version,
133		)))
134	}
135
136	result := proto.Workspace{
137		ID:      id,
138		Path:    args.Path,
139		DataDir: cfg.Config().Options.DataDirectory,
140		Debug:   cfg.Config().Options.Debug,
141		YOLO:    cfg.Overrides().SkipPermissionRequests,
142		Config:  cfg.Config(),
143		Env:     args.Env,
144	}
145
146	return ws, result, nil
147}
148
149// DeleteWorkspace shuts down and removes a workspace. If it was the
150// last workspace, the shutdown callback is invoked.
151func (b *Backend) DeleteWorkspace(id string) {
152	ws, ok := b.workspaces.Get(id)
153	if ok {
154		ws.Shutdown()
155	}
156	b.workspaces.Del(id)
157
158	if b.workspaces.Len() == 0 && b.shutdownFn != nil {
159		slog.Info("Last workspace removed, shutting down server...")
160		b.shutdownFn()
161	}
162}
163
164// GetWorkspaceProto returns the proto representation of a workspace.
165func (b *Backend) GetWorkspaceProto(id string) (proto.Workspace, error) {
166	ws, err := b.GetWorkspace(id)
167	if err != nil {
168		return proto.Workspace{}, err
169	}
170	return workspaceToProto(ws), nil
171}
172
173// VersionInfo returns server version information.
174func (b *Backend) VersionInfo() proto.VersionInfo {
175	return proto.VersionInfo{
176		Version:   version.Version,
177		Commit:    version.Commit,
178		BuildID:   version.BuildID,
179		GoVersion: runtime.Version(),
180		Platform:  fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
181	}
182}
183
184// Config returns the server-level configuration.
185func (b *Backend) Config() *config.ConfigStore {
186	return b.cfg
187}
188
189// Shutdown initiates a graceful server shutdown.
190func (b *Backend) Shutdown() {
191	if b.shutdownFn != nil {
192		b.shutdownFn()
193	}
194}
195
196func workspaceToProto(ws *Workspace) proto.Workspace {
197	cfg := ws.Cfg.Config()
198	return proto.Workspace{
199		ID:      ws.ID,
200		Path:    ws.Path,
201		YOLO:    ws.Cfg.Overrides().SkipPermissionRequests,
202		DataDir: cfg.Options.DataDirectory,
203		Debug:   cfg.Options.Debug,
204		Config:  cfg,
205	}
206}