mirror of
https://github.com/charmbracelet/crush.git
synced 2026-05-30 18:47:33 +00:00
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
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
|
|
}
|