1package backend
2
3import (
4 "context"
5
6 tea "charm.land/bubbletea/v2"
7
8 mcptools "github.com/charmbracelet/crush/internal/agent/tools/mcp"
9 "github.com/charmbracelet/crush/internal/app"
10 "github.com/charmbracelet/crush/internal/config"
11)
12
13// SubscribeEvents returns the event channel for a workspace's app.
14func (b *Backend) SubscribeEvents(workspaceID string) (<-chan tea.Msg, error) {
15 ws, err := b.GetWorkspace(workspaceID)
16 if err != nil {
17 return nil, err
18 }
19
20 return ws.Events(), nil
21}
22
23// GetLSPStates returns the state of all LSP clients.
24func (b *Backend) GetLSPStates(workspaceID string) (map[string]app.LSPClientInfo, error) {
25 _, err := b.GetWorkspace(workspaceID)
26 if err != nil {
27 return nil, err
28 }
29
30 return app.GetLSPStates(), nil
31}
32
33// GetLSPDiagnostics returns diagnostics for a specific LSP client in
34// the workspace.
35func (b *Backend) GetLSPDiagnostics(workspaceID, lspName string) (any, error) {
36 ws, err := b.GetWorkspace(workspaceID)
37 if err != nil {
38 return nil, err
39 }
40
41 for name, client := range ws.LSPManager.Clients().Seq2() {
42 if name == lspName {
43 return client.GetDiagnostics(), nil
44 }
45 }
46
47 return nil, ErrLSPClientNotFound
48}
49
50// GetWorkspaceConfig returns the workspace-level configuration.
51func (b *Backend) GetWorkspaceConfig(workspaceID string) (*config.Config, error) {
52 ws, err := b.GetWorkspace(workspaceID)
53 if err != nil {
54 return nil, err
55 }
56
57 return ws.Cfg.Config(), nil
58}
59
60// GetWorkspaceProviders returns the configured providers for a
61// workspace.
62func (b *Backend) GetWorkspaceProviders(workspaceID string) (any, error) {
63 ws, err := b.GetWorkspace(workspaceID)
64 if err != nil {
65 return nil, err
66 }
67
68 providers, _ := config.Providers(ws.Cfg.Config())
69 return providers, nil
70}
71
72// LSPStart starts an LSP server for the given path.
73func (b *Backend) LSPStart(ctx context.Context, workspaceID, path string) error {
74 ws, err := b.GetWorkspace(workspaceID)
75 if err != nil {
76 return err
77 }
78
79 ws.LSPManager.Start(ctx, path)
80 return nil
81}
82
83// LSPStopAll stops all LSP servers for a workspace.
84func (b *Backend) LSPStopAll(ctx context.Context, workspaceID string) error {
85 ws, err := b.GetWorkspace(workspaceID)
86 if err != nil {
87 return err
88 }
89
90 ws.LSPManager.StopAll(ctx)
91 return nil
92}
93
94// MCPGetStates returns the current state of all MCP clients.
95func (b *Backend) MCPGetStates(_ string) map[string]mcptools.ClientInfo {
96 return mcptools.GetStates()
97}
98
99// MCPRefreshPrompts refreshes prompts for a named MCP client.
100func (b *Backend) MCPRefreshPrompts(ctx context.Context, _ string, name string) {
101 mcptools.RefreshPrompts(ctx, name)
102}
103
104// MCPRefreshResources refreshes resources for a named MCP client.
105func (b *Backend) MCPRefreshResources(ctx context.Context, _ string, name string) {
106 mcptools.RefreshResources(ctx, name)
107}