agents.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package dialog
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/charmbracelet/bubbles/v2/key"
  6. tea "github.com/charmbracelet/bubbletea/v2"
  7. "github.com/lithammer/fuzzysearch/fuzzy"
  8. "github.com/sst/opencode-sdk-go"
  9. "github.com/sst/opencode/internal/app"
  10. "github.com/sst/opencode/internal/components/list"
  11. "github.com/sst/opencode/internal/components/modal"
  12. "github.com/sst/opencode/internal/layout"
  13. "github.com/sst/opencode/internal/styles"
  14. "github.com/sst/opencode/internal/theme"
  15. "github.com/sst/opencode/internal/util"
  16. )
  17. const (
  18. numVisibleAgents = 10
  19. minAgentDialogWidth = 40
  20. maxAgentDialogWidth = 60
  21. maxDescriptionLength = 60
  22. maxRecentAgents = 5
  23. )
  24. // AgentDialog interface for the agent selection dialog
  25. type AgentDialog interface {
  26. layout.Modal
  27. }
  28. type agentDialog struct {
  29. app *app.App
  30. allAgents []agentSelectItem
  31. width int
  32. height int
  33. modal *modal.Modal
  34. searchDialog *SearchDialog
  35. dialogWidth int
  36. }
  37. // agentSelectItem combines the visual improvements with code patterns
  38. type agentSelectItem struct {
  39. name string
  40. displayName string
  41. description string
  42. mode string // "primary", "subagent", "all"
  43. isCurrent bool
  44. agentIndex int
  45. agent opencode.Agent // Keep original agent for compatibility
  46. }
  47. func (a agentSelectItem) Render(
  48. selected bool,
  49. width int,
  50. baseStyle styles.Style,
  51. ) string {
  52. t := theme.CurrentTheme()
  53. itemStyle := baseStyle.
  54. Background(t.BackgroundPanel()).
  55. Foreground(t.Text())
  56. if selected {
  57. // Use agent color for highlighting when selected (visual improvement)
  58. agentColor := util.GetAgentColor(a.agentIndex)
  59. itemStyle = itemStyle.Foreground(agentColor)
  60. }
  61. descStyle := baseStyle.
  62. Foreground(t.TextMuted()).
  63. Background(t.BackgroundPanel())
  64. // Calculate available width (accounting for padding and margins)
  65. availableWidth := width - 2 // Account for left padding
  66. agentName := a.displayName
  67. // Determine if agent is built-in or custom using the agent's builtIn field
  68. var displayText string
  69. if a.agent.BuiltIn {
  70. displayText = "(built-in)"
  71. } else {
  72. if a.description != "" {
  73. displayText = a.description
  74. } else {
  75. displayText = "(user)"
  76. }
  77. }
  78. separator := " - "
  79. // Calculate how much space we have for the description (visual improvement)
  80. nameAndSeparatorLength := len(agentName) + len(separator)
  81. descriptionMaxLength := availableWidth - nameAndSeparatorLength
  82. // Cap description length to the maximum allowed
  83. if descriptionMaxLength > maxDescriptionLength {
  84. descriptionMaxLength = maxDescriptionLength
  85. }
  86. // Truncate description if it's too long (visual improvement)
  87. if len(displayText) > descriptionMaxLength && descriptionMaxLength > 3 {
  88. displayText = displayText[:descriptionMaxLength-3] + "..."
  89. }
  90. namePart := itemStyle.Render(agentName)
  91. descPart := descStyle.Render(separator + displayText)
  92. combinedText := namePart + descPart
  93. return baseStyle.
  94. Background(t.BackgroundPanel()).
  95. PaddingLeft(1).
  96. Width(width).
  97. Render(combinedText)
  98. }
  99. func (a agentSelectItem) Selectable() bool {
  100. return true
  101. }
  102. type agentKeyMap struct {
  103. Enter key.Binding
  104. Escape key.Binding
  105. }
  106. var agentKeys = agentKeyMap{
  107. Enter: key.NewBinding(
  108. key.WithKeys("enter"),
  109. key.WithHelp("enter", "select agent"),
  110. ),
  111. Escape: key.NewBinding(
  112. key.WithKeys("esc"),
  113. key.WithHelp("esc", "close"),
  114. ),
  115. }
  116. func (a *agentDialog) Init() tea.Cmd {
  117. a.setupAllAgents()
  118. return a.searchDialog.Init()
  119. }
  120. func (a *agentDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  121. switch msg := msg.(type) {
  122. case tea.WindowSizeMsg:
  123. a.width = msg.Width
  124. a.height = msg.Height
  125. a.searchDialog.SetWidth(a.dialogWidth)
  126. a.searchDialog.SetHeight(msg.Height)
  127. case SearchSelectionMsg:
  128. // Handle selection from search dialog
  129. if item, ok := msg.Item.(agentSelectItem); ok {
  130. if !item.isCurrent {
  131. // Switch to selected agent (using their better pattern)
  132. return a, tea.Sequence(
  133. util.CmdHandler(modal.CloseModalMsg{}),
  134. util.CmdHandler(app.AgentSelectedMsg{AgentName: item.name}),
  135. )
  136. }
  137. }
  138. return a, util.CmdHandler(modal.CloseModalMsg{})
  139. case SearchCancelledMsg:
  140. return a, util.CmdHandler(modal.CloseModalMsg{})
  141. case SearchRemoveItemMsg:
  142. if item, ok := msg.Item.(agentSelectItem); ok {
  143. if a.isAgentInRecentSection(item, msg.Index) {
  144. a.app.State.RemoveAgentFromRecentlyUsed(item.name)
  145. items := a.buildDisplayList(a.searchDialog.GetQuery())
  146. a.searchDialog.SetItems(items)
  147. return a, a.app.SaveState()
  148. }
  149. }
  150. return a, nil
  151. case SearchQueryChangedMsg:
  152. // Update the list based on search query
  153. items := a.buildDisplayList(msg.Query)
  154. a.searchDialog.SetItems(items)
  155. return a, nil
  156. }
  157. updatedDialog, cmd := a.searchDialog.Update(msg)
  158. a.searchDialog = updatedDialog.(*SearchDialog)
  159. return a, cmd
  160. }
  161. func (a *agentDialog) SetSize(width, height int) {
  162. a.width = width
  163. a.height = height
  164. }
  165. func (a *agentDialog) View() string {
  166. return a.searchDialog.View()
  167. }
  168. func (a *agentDialog) calculateOptimalWidth(agents []agentSelectItem) int {
  169. maxWidth := minAgentDialogWidth
  170. for _, agent := range agents {
  171. // Calculate the width needed for this item: "AgentName - Description" (visual improvement)
  172. itemWidth := len(agent.displayName)
  173. if agent.agent.BuiltIn {
  174. itemWidth += len("(built-in)") + 3 // " - "
  175. } else {
  176. if agent.description != "" {
  177. descLength := len(agent.description)
  178. if descLength > maxDescriptionLength {
  179. descLength = maxDescriptionLength
  180. }
  181. itemWidth += descLength + 3 // " - "
  182. } else {
  183. itemWidth += len("(user)") + 3 // " - "
  184. }
  185. }
  186. if itemWidth > maxWidth {
  187. maxWidth = itemWidth
  188. }
  189. }
  190. maxWidth = min(maxWidth, maxAgentDialogWidth)
  191. return maxWidth
  192. }
  193. func (a *agentDialog) setupAllAgents() {
  194. currentAgentName := a.app.Agent().Name
  195. // Build agent items from app.Agents (no API call needed) - their pattern
  196. a.allAgents = make([]agentSelectItem, 0, len(a.app.Agents))
  197. for i, agent := range a.app.Agents {
  198. if agent.Mode == "subagent" {
  199. continue // Skip subagents entirely
  200. }
  201. isCurrent := agent.Name == currentAgentName
  202. // Create display name (capitalize first letter)
  203. displayName := strings.Title(agent.Name)
  204. a.allAgents = append(a.allAgents, agentSelectItem{
  205. name: agent.Name,
  206. displayName: displayName,
  207. description: agent.Description, // Keep for search but don't use in display
  208. mode: string(agent.Mode),
  209. isCurrent: isCurrent,
  210. agentIndex: i,
  211. agent: agent, // Keep original for compatibility
  212. })
  213. }
  214. a.sortAgents()
  215. // Calculate optimal width based on all agents (visual improvement)
  216. a.dialogWidth = a.calculateOptimalWidth(a.allAgents)
  217. // Ensure minimum width to prevent textinput issues
  218. a.dialogWidth = max(a.dialogWidth, minAgentDialogWidth)
  219. a.searchDialog = NewSearchDialog("Search agents...", numVisibleAgents)
  220. a.searchDialog.SetWidth(a.dialogWidth)
  221. // Build initial display list (empty query shows grouped view)
  222. items := a.buildDisplayList("")
  223. a.searchDialog.SetItems(items)
  224. }
  225. func (a *agentDialog) sortAgents() {
  226. sort.Slice(a.allAgents, func(i, j int) bool {
  227. agentA := a.allAgents[i]
  228. agentB := a.allAgents[j]
  229. // Current agent goes first (your preference)
  230. if agentA.name == a.app.Agent().Name {
  231. return true
  232. }
  233. if agentB.name == a.app.Agent().Name {
  234. return false
  235. }
  236. // Alphabetical order for all other agents
  237. return agentA.name < agentB.name
  238. })
  239. }
  240. // buildDisplayList creates the list items based on search query
  241. func (a *agentDialog) buildDisplayList(query string) []list.Item {
  242. if query != "" {
  243. // Search mode: use fuzzy matching
  244. return a.buildSearchResults(query)
  245. } else {
  246. // Grouped mode: show Recent agents section and alphabetical list (their pattern)
  247. return a.buildGroupedResults()
  248. }
  249. }
  250. // buildSearchResults creates a flat list of search results using fuzzy matching
  251. func (a *agentDialog) buildSearchResults(query string) []list.Item {
  252. agentNames := []string{}
  253. agentMap := make(map[string]agentSelectItem)
  254. for _, agent := range a.allAgents {
  255. // Only include non-subagents in search
  256. if agent.mode == "subagent" {
  257. continue
  258. }
  259. searchStr := agent.name
  260. agentNames = append(agentNames, searchStr)
  261. agentMap[searchStr] = agent
  262. }
  263. matches := fuzzy.RankFindFold(query, agentNames)
  264. sort.Sort(matches)
  265. items := []list.Item{}
  266. seenAgents := make(map[string]bool)
  267. for _, match := range matches {
  268. agent := agentMap[match.Target]
  269. // Create a unique key to avoid duplicates
  270. key := agent.name
  271. if seenAgents[key] {
  272. continue
  273. }
  274. seenAgents[key] = true
  275. items = append(items, agent)
  276. }
  277. return items
  278. }
  279. // buildGroupedResults creates a grouped list with Recent agents section and categorized agents
  280. func (a *agentDialog) buildGroupedResults() []list.Item {
  281. var items []list.Item
  282. // Add Recent section (their pattern)
  283. recentAgents := a.getRecentAgents(maxRecentAgents)
  284. if len(recentAgents) > 0 {
  285. items = append(items, list.HeaderItem("Recent"))
  286. for _, agent := range recentAgents {
  287. items = append(items, agent)
  288. }
  289. }
  290. // Create map of recent agent names for filtering
  291. recentAgentNames := make(map[string]bool)
  292. for _, recent := range recentAgents {
  293. recentAgentNames[recent.name] = true
  294. }
  295. // Only show non-subagents (primary/user) in the main section
  296. mainAgents := make([]agentSelectItem, 0)
  297. for _, agent := range a.allAgents {
  298. if !recentAgentNames[agent.name] {
  299. mainAgents = append(mainAgents, agent)
  300. }
  301. }
  302. // Sort main agents alphabetically
  303. sort.Slice(mainAgents, func(i, j int) bool {
  304. return mainAgents[i].name < mainAgents[j].name
  305. })
  306. // Add main agents section
  307. if len(mainAgents) > 0 {
  308. items = append(items, list.HeaderItem("Agents"))
  309. for _, agent := range mainAgents {
  310. items = append(items, agent)
  311. }
  312. }
  313. return items
  314. }
  315. func (a *agentDialog) Render(background string) string {
  316. return a.modal.Render(a.View(), background)
  317. }
  318. func (a *agentDialog) Close() tea.Cmd {
  319. return nil
  320. }
  321. // getRecentAgents returns the most recently used agents (their pattern)
  322. func (a *agentDialog) getRecentAgents(limit int) []agentSelectItem {
  323. var recentAgents []agentSelectItem
  324. // Get recent agents from app state
  325. for _, usage := range a.app.State.RecentlyUsedAgents {
  326. if len(recentAgents) >= limit {
  327. break
  328. }
  329. // Find the corresponding agent
  330. for _, agent := range a.allAgents {
  331. if agent.name == usage.AgentName {
  332. recentAgents = append(recentAgents, agent)
  333. break
  334. }
  335. }
  336. }
  337. // If no recent agents, use the current agent
  338. if len(recentAgents) == 0 {
  339. currentAgentName := a.app.Agent().Name
  340. for _, agent := range a.allAgents {
  341. if agent.name == currentAgentName {
  342. recentAgents = append(recentAgents, agent)
  343. break
  344. }
  345. }
  346. }
  347. return recentAgents
  348. }
  349. func (a *agentDialog) isAgentInRecentSection(agent agentSelectItem, index int) bool {
  350. // Only check if we're in grouped mode (no search query)
  351. if a.searchDialog.GetQuery() != "" {
  352. return false
  353. }
  354. recentAgents := a.getRecentAgents(maxRecentAgents)
  355. if len(recentAgents) == 0 {
  356. return false
  357. }
  358. // Index 0 is the "Recent" header, so recent agents are at indices 1 to len(recentAgents)
  359. if index >= 1 && index <= len(recentAgents) {
  360. if index-1 < len(recentAgents) {
  361. recentAgent := recentAgents[index-1]
  362. return recentAgent.name == agent.name
  363. }
  364. }
  365. return false
  366. }
  367. func NewAgentDialog(app *app.App) AgentDialog {
  368. dialog := &agentDialog{
  369. app: app,
  370. }
  371. dialog.setupAllAgents()
  372. dialog.modal = modal.New(
  373. modal.WithTitle("Select Agent"),
  374. modal.WithMaxWidth(dialog.dialogWidth+4),
  375. )
  376. return dialog
  377. }