agent.go

  1package backend
  2
  3import (
  4	"context"
  5
  6	"github.com/charmbracelet/crush/internal/proto"
  7)
  8
  9// SendMessage sends a prompt to the agent coordinator for the given
 10// workspace and session.
 11func (b *Backend) SendMessage(ctx context.Context, workspaceID string, msg proto.AgentMessage) error {
 12	ws, err := b.GetWorkspace(workspaceID)
 13	if err != nil {
 14		return err
 15	}
 16
 17	if ws.AgentCoordinator == nil {
 18		return ErrAgentNotInitialized
 19	}
 20
 21	_, err = ws.AgentCoordinator.Run(ctx, msg.SessionID, msg.Prompt)
 22	return err
 23}
 24
 25// GetAgentInfo returns the agent's model and busy status.
 26func (b *Backend) GetAgentInfo(workspaceID string) (proto.AgentInfo, error) {
 27	ws, err := b.GetWorkspace(workspaceID)
 28	if err != nil {
 29		return proto.AgentInfo{}, err
 30	}
 31
 32	var agentInfo proto.AgentInfo
 33	if ws.AgentCoordinator != nil {
 34		m := ws.AgentCoordinator.Model()
 35		agentInfo = proto.AgentInfo{
 36			Model:  m.CatwalkCfg,
 37			IsBusy: ws.AgentCoordinator.IsBusy(),
 38		}
 39	}
 40	return agentInfo, nil
 41}
 42
 43// InitAgent initializes the coder agent for the workspace.
 44func (b *Backend) InitAgent(ctx context.Context, workspaceID string) error {
 45	ws, err := b.GetWorkspace(workspaceID)
 46	if err != nil {
 47		return err
 48	}
 49
 50	return ws.InitCoderAgent(ctx)
 51}
 52
 53// UpdateAgent reloads the agent model configuration.
 54func (b *Backend) UpdateAgent(ctx context.Context, workspaceID string) error {
 55	ws, err := b.GetWorkspace(workspaceID)
 56	if err != nil {
 57		return err
 58	}
 59
 60	return ws.UpdateAgentModel(ctx)
 61}
 62
 63// CancelSession cancels an ongoing agent operation for the given
 64// session.
 65func (b *Backend) CancelSession(workspaceID, sessionID string) error {
 66	ws, err := b.GetWorkspace(workspaceID)
 67	if err != nil {
 68		return err
 69	}
 70
 71	if ws.AgentCoordinator != nil {
 72		ws.AgentCoordinator.Cancel(sessionID)
 73	}
 74	return nil
 75}
 76
 77// SummarizeSession triggers a session summarization.
 78func (b *Backend) SummarizeSession(ctx context.Context, workspaceID, sessionID string) error {
 79	ws, err := b.GetWorkspace(workspaceID)
 80	if err != nil {
 81		return err
 82	}
 83
 84	if ws.AgentCoordinator == nil {
 85		return ErrAgentNotInitialized
 86	}
 87
 88	return ws.AgentCoordinator.Summarize(ctx, sessionID)
 89}
 90
 91// QueuedPrompts returns the number of queued prompts for the session.
 92func (b *Backend) QueuedPrompts(workspaceID, sessionID string) (int, error) {
 93	ws, err := b.GetWorkspace(workspaceID)
 94	if err != nil {
 95		return 0, err
 96	}
 97
 98	if ws.AgentCoordinator == nil {
 99		return 0, nil
100	}
101
102	return ws.AgentCoordinator.QueuedPrompts(sessionID), nil
103}
104
105// ClearQueue clears the prompt queue for the session.
106func (b *Backend) ClearQueue(workspaceID, sessionID string) error {
107	ws, err := b.GetWorkspace(workspaceID)
108	if err != nil {
109		return err
110	}
111
112	if ws.AgentCoordinator != nil {
113		ws.AgentCoordinator.ClearQueue(sessionID)
114	}
115	return nil
116}