agents.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package completions
  2. import (
  3. "context"
  4. "log/slog"
  5. "strings"
  6. "github.com/sst/opencode-sdk-go"
  7. "github.com/sst/opencode/internal/app"
  8. "github.com/sst/opencode/internal/styles"
  9. "github.com/sst/opencode/internal/theme"
  10. )
  11. type agentsContextGroup struct {
  12. app *app.App
  13. }
  14. func (cg *agentsContextGroup) GetId() string {
  15. return "agents"
  16. }
  17. func (cg *agentsContextGroup) GetEmptyMessage() string {
  18. return "no matching agents"
  19. }
  20. func (cg *agentsContextGroup) GetChildEntries(
  21. query string,
  22. ) ([]CompletionSuggestion, error) {
  23. items := make([]CompletionSuggestion, 0)
  24. query = strings.TrimSpace(query)
  25. agents, err := cg.app.Client.Agent.List(
  26. context.Background(),
  27. opencode.AgentListParams{},
  28. )
  29. if err != nil {
  30. slog.Error("Failed to get agent list", "error", err)
  31. return items, err
  32. }
  33. if agents == nil {
  34. return items, nil
  35. }
  36. for _, agent := range *agents {
  37. if query != "" && !strings.Contains(strings.ToLower(agent.Name), strings.ToLower(query)) {
  38. continue
  39. }
  40. if agent.Mode == opencode.AgentModePrimary {
  41. continue
  42. }
  43. displayFunc := func(s styles.Style) string {
  44. t := theme.CurrentTheme()
  45. muted := s.Foreground(t.TextMuted()).Render
  46. return s.Render(agent.Name) + muted(" (agent)")
  47. }
  48. item := CompletionSuggestion{
  49. Display: displayFunc,
  50. Value: agent.Name,
  51. ProviderID: cg.GetId(),
  52. RawData: agent,
  53. }
  54. items = append(items, item)
  55. }
  56. return items, nil
  57. }
  58. func NewAgentsContextGroup(app *app.App) CompletionProvider {
  59. return &agentsContextGroup{
  60. app: app,
  61. }
  62. }