agents.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.App.Agents(
  26. context.Background(),
  27. )
  28. if err != nil {
  29. slog.Error("Failed to get agent list", "error", err)
  30. return items, err
  31. }
  32. if agents == nil {
  33. return items, nil
  34. }
  35. for _, agent := range *agents {
  36. if query != "" && !strings.Contains(strings.ToLower(agent.Name), strings.ToLower(query)) {
  37. continue
  38. }
  39. if agent.Mode == opencode.AgentModePrimary {
  40. continue
  41. }
  42. displayFunc := func(s styles.Style) string {
  43. t := theme.CurrentTheme()
  44. muted := s.Foreground(t.TextMuted()).Render
  45. return s.Render(agent.Name) + muted(" (agent)")
  46. }
  47. item := CompletionSuggestion{
  48. Display: displayFunc,
  49. Value: agent.Name,
  50. ProviderID: cg.GetId(),
  51. RawData: agent,
  52. }
  53. items = append(items, item)
  54. }
  55. return items, nil
  56. }
  57. func NewAgentsContextGroup(app *app.App) CompletionProvider {
  58. return &agentsContextGroup{
  59. app: app,
  60. }
  61. }