summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/completions/agents.go
blob: c39fe30365177dbc650cd4764a1276136c4b19d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package completions

import (
	"context"
	"log/slog"
	"strings"

	"github.com/sst/opencode-sdk-go"
	"github.com/sst/opencode/internal/app"
	"github.com/sst/opencode/internal/styles"
	"github.com/sst/opencode/internal/theme"
)

type agentsContextGroup struct {
	app *app.App
}

func (cg *agentsContextGroup) GetId() string {
	return "agents"
}

func (cg *agentsContextGroup) GetEmptyMessage() string {
	return "no matching agents"
}

func (cg *agentsContextGroup) GetChildEntries(
	query string,
) ([]CompletionSuggestion, error) {
	items := make([]CompletionSuggestion, 0)

	query = strings.TrimSpace(query)

	agents, err := cg.app.Client.App.Agents(
		context.Background(),
	)
	if err != nil {
		slog.Error("Failed to get agent list", "error", err)
		return items, err
	}
	if agents == nil {
		return items, nil
	}

	for _, agent := range *agents {
		if query != "" && !strings.Contains(strings.ToLower(agent.Name), strings.ToLower(query)) {
			continue
		}
		if agent.Mode == opencode.AgentModePrimary {
			continue
		}

		displayFunc := func(s styles.Style) string {
			t := theme.CurrentTheme()
			muted := s.Foreground(t.TextMuted()).Render
			return s.Render(agent.Name) + muted(" (agent)")
		}

		item := CompletionSuggestion{
			Display:    displayFunc,
			Value:      agent.Name,
			ProviderID: cg.GetId(),
			RawData:    agent,
		}
		items = append(items, item)
	}

	return items, nil
}

func NewAgentsContextGroup(app *app.App) CompletionProvider {
	return &agentsContextGroup{
		app: app,
	}
}