| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package backend
- import (
- "context"
- "github.com/charmbracelet/crush/internal/config"
- "github.com/charmbracelet/crush/internal/proto"
- )
- // SendMessage sends a prompt to the agent coordinator for the given
- // workspace and session.
- func (b *Backend) SendMessage(ctx context.Context, workspaceID string, msg proto.AgentMessage) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- if ws.AgentCoordinator == nil {
- return ErrAgentNotInitialized
- }
- _, err = ws.AgentCoordinator.Run(ctx, msg.SessionID, msg.Prompt)
- return err
- }
- // GetAgentInfo returns the agent's model and busy status.
- func (b *Backend) GetAgentInfo(workspaceID string) (proto.AgentInfo, error) {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return proto.AgentInfo{}, err
- }
- var agentInfo proto.AgentInfo
- if ws.AgentCoordinator != nil {
- m := ws.AgentCoordinator.Model()
- agentInfo = proto.AgentInfo{
- Model: m.CatwalkCfg,
- ModelCfg: m.ModelCfg,
- IsBusy: ws.AgentCoordinator.IsBusy(),
- IsReady: true,
- }
- }
- return agentInfo, nil
- }
- // InitAgent initializes the coder agent for the workspace.
- func (b *Backend) InitAgent(ctx context.Context, workspaceID string) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- return ws.InitCoderAgent(ctx)
- }
- // UpdateAgent reloads the agent model configuration.
- func (b *Backend) UpdateAgent(ctx context.Context, workspaceID string) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- return ws.UpdateAgentModel(ctx)
- }
- // CancelSession cancels an ongoing agent operation for the given
- // session.
- func (b *Backend) CancelSession(workspaceID, sessionID string) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- if ws.AgentCoordinator != nil {
- ws.AgentCoordinator.Cancel(sessionID)
- }
- return nil
- }
- // SummarizeSession triggers a session summarization.
- func (b *Backend) SummarizeSession(ctx context.Context, workspaceID, sessionID string) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- if ws.AgentCoordinator == nil {
- return ErrAgentNotInitialized
- }
- return ws.AgentCoordinator.Summarize(ctx, sessionID)
- }
- // QueuedPrompts returns the number of queued prompts for the session.
- func (b *Backend) QueuedPrompts(workspaceID, sessionID string) (int, error) {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return 0, err
- }
- if ws.AgentCoordinator == nil {
- return 0, nil
- }
- return ws.AgentCoordinator.QueuedPrompts(sessionID), nil
- }
- // ClearQueue clears the prompt queue for the session.
- func (b *Backend) ClearQueue(workspaceID, sessionID string) error {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return err
- }
- if ws.AgentCoordinator != nil {
- ws.AgentCoordinator.ClearQueue(sessionID)
- }
- return nil
- }
- // QueuedPromptsList returns the list of queued prompt strings for a
- // session.
- func (b *Backend) QueuedPromptsList(workspaceID, sessionID string) ([]string, error) {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return nil, err
- }
- if ws.AgentCoordinator == nil {
- return nil, nil
- }
- return ws.AgentCoordinator.QueuedPromptsList(sessionID), nil
- }
- // GetDefaultSmallModel returns the default small model for a provider.
- func (b *Backend) GetDefaultSmallModel(workspaceID, providerID string) (config.SelectedModel, error) {
- ws, err := b.GetWorkspace(workspaceID)
- if err != nil {
- return config.SelectedModel{}, err
- }
- return ws.GetDefaultSmallModel(providerID), nil
- }
|