models.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package models
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/charmbracelet/bubbles/v2/help"
  6. "github.com/charmbracelet/bubbles/v2/key"
  7. "github.com/charmbracelet/bubbles/v2/spinner"
  8. tea "github.com/charmbracelet/bubbletea/v2"
  9. "github.com/charmbracelet/crush/internal/config"
  10. "github.com/charmbracelet/crush/internal/fur/provider"
  11. "github.com/charmbracelet/crush/internal/tui/components/completions"
  12. "github.com/charmbracelet/crush/internal/tui/components/core"
  13. "github.com/charmbracelet/crush/internal/tui/components/core/list"
  14. "github.com/charmbracelet/crush/internal/tui/components/dialogs"
  15. "github.com/charmbracelet/crush/internal/tui/styles"
  16. "github.com/charmbracelet/crush/internal/tui/util"
  17. "github.com/charmbracelet/lipgloss/v2"
  18. )
  19. const (
  20. ModelsDialogID dialogs.DialogID = "models"
  21. defaultWidth = 60
  22. )
  23. const (
  24. LargeModelType int = iota
  25. SmallModelType
  26. largeModelInputPlaceholder = "Choose a model for large, complex tasks"
  27. smallModelInputPlaceholder = "Choose a model for small, simple tasks"
  28. )
  29. // ModelSelectedMsg is sent when a model is selected
  30. type ModelSelectedMsg struct {
  31. Model config.SelectedModel
  32. ModelType config.SelectedModelType
  33. }
  34. // CloseModelDialogMsg is sent when a model is selected
  35. type CloseModelDialogMsg struct{}
  36. // ModelDialog interface for the model selection dialog
  37. type ModelDialog interface {
  38. dialogs.DialogModel
  39. }
  40. type ModelOption struct {
  41. Provider provider.Provider
  42. Model provider.Model
  43. }
  44. type modelDialogCmp struct {
  45. width int
  46. wWidth int
  47. wHeight int
  48. modelList *ModelListComponent
  49. keyMap KeyMap
  50. help help.Model
  51. // API key state
  52. needsAPIKey bool
  53. apiKeyInput *APIKeyInput
  54. selectedModel *ModelOption
  55. selectedModelType config.SelectedModelType
  56. isAPIKeyValid bool
  57. apiKeyValue string
  58. }
  59. func NewModelDialogCmp() ModelDialog {
  60. listKeyMap := list.DefaultKeyMap()
  61. keyMap := DefaultKeyMap()
  62. listKeyMap.Down.SetEnabled(false)
  63. listKeyMap.Up.SetEnabled(false)
  64. listKeyMap.HalfPageDown.SetEnabled(false)
  65. listKeyMap.HalfPageUp.SetEnabled(false)
  66. listKeyMap.Home.SetEnabled(false)
  67. listKeyMap.End.SetEnabled(false)
  68. listKeyMap.DownOneItem = keyMap.Next
  69. listKeyMap.UpOneItem = keyMap.Previous
  70. t := styles.CurrentTheme()
  71. inputStyle := t.S().Base.Padding(0, 1, 0, 1)
  72. modelList := NewModelListComponent(listKeyMap, inputStyle, "Choose a model for large, complex tasks")
  73. apiKeyInput := NewAPIKeyInput()
  74. apiKeyInput.SetShowTitle(false)
  75. help := help.New()
  76. help.Styles = t.S().Help
  77. return &modelDialogCmp{
  78. modelList: modelList,
  79. apiKeyInput: apiKeyInput,
  80. width: defaultWidth,
  81. keyMap: DefaultKeyMap(),
  82. help: help,
  83. }
  84. }
  85. func (m *modelDialogCmp) Init() tea.Cmd {
  86. return tea.Batch(m.modelList.Init(), m.apiKeyInput.Init())
  87. }
  88. func (m *modelDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  89. switch msg := msg.(type) {
  90. case tea.WindowSizeMsg:
  91. m.wWidth = msg.Width
  92. m.wHeight = msg.Height
  93. m.apiKeyInput.SetWidth(m.width - 2)
  94. m.help.Width = m.width - 2
  95. return m, m.modelList.SetSize(m.listWidth(), m.listHeight())
  96. case APIKeyStateChangeMsg:
  97. u, cmd := m.apiKeyInput.Update(msg)
  98. m.apiKeyInput = u.(*APIKeyInput)
  99. return m, cmd
  100. case tea.KeyPressMsg:
  101. switch {
  102. case key.Matches(msg, m.keyMap.Select):
  103. if m.isAPIKeyValid {
  104. return m, m.saveAPIKeyAndContinue(m.apiKeyValue)
  105. }
  106. if m.needsAPIKey {
  107. // Handle API key submission
  108. m.apiKeyValue = m.apiKeyInput.Value()
  109. provider, err := m.getProvider(m.selectedModel.Provider.ID)
  110. if err != nil || provider == nil {
  111. return m, util.ReportError(fmt.Errorf("provider %s not found", m.selectedModel.Provider.ID))
  112. }
  113. providerConfig := config.ProviderConfig{
  114. ID: string(m.selectedModel.Provider.ID),
  115. Name: m.selectedModel.Provider.Name,
  116. APIKey: m.apiKeyValue,
  117. Type: provider.Type,
  118. BaseURL: provider.APIEndpoint,
  119. }
  120. return m, tea.Sequence(
  121. util.CmdHandler(APIKeyStateChangeMsg{
  122. State: APIKeyInputStateVerifying,
  123. }),
  124. func() tea.Msg {
  125. start := time.Now()
  126. err := providerConfig.TestConnection(config.Get().Resolver())
  127. // intentionally wait for at least 750ms to make sure the user sees the spinner
  128. elapsed := time.Since(start)
  129. if elapsed < 750*time.Millisecond {
  130. time.Sleep(750*time.Millisecond - elapsed)
  131. }
  132. if err == nil {
  133. m.isAPIKeyValid = true
  134. return APIKeyStateChangeMsg{
  135. State: APIKeyInputStateVerified,
  136. }
  137. }
  138. return APIKeyStateChangeMsg{
  139. State: APIKeyInputStateError,
  140. }
  141. },
  142. )
  143. }
  144. // Normal model selection
  145. selectedItemInx := m.modelList.SelectedIndex()
  146. if selectedItemInx == list.NoSelection {
  147. return m, nil
  148. }
  149. items := m.modelList.Items()
  150. selectedItem := items[selectedItemInx].(completions.CompletionItem).Value().(ModelOption)
  151. var modelType config.SelectedModelType
  152. if m.modelList.GetModelType() == LargeModelType {
  153. modelType = config.SelectedModelTypeLarge
  154. } else {
  155. modelType = config.SelectedModelTypeSmall
  156. }
  157. // Check if provider is configured
  158. if m.isProviderConfigured(string(selectedItem.Provider.ID)) {
  159. return m, tea.Sequence(
  160. util.CmdHandler(dialogs.CloseDialogMsg{}),
  161. util.CmdHandler(ModelSelectedMsg{
  162. Model: config.SelectedModel{
  163. Model: selectedItem.Model.ID,
  164. Provider: string(selectedItem.Provider.ID),
  165. },
  166. ModelType: modelType,
  167. }),
  168. )
  169. } else {
  170. // Provider not configured, show API key input
  171. m.needsAPIKey = true
  172. m.selectedModel = &selectedItem
  173. m.selectedModelType = modelType
  174. m.apiKeyInput.SetProviderName(selectedItem.Provider.Name)
  175. return m, nil
  176. }
  177. case key.Matches(msg, m.keyMap.Tab):
  178. if m.needsAPIKey {
  179. u, cmd := m.apiKeyInput.Update(msg)
  180. m.apiKeyInput = u.(*APIKeyInput)
  181. return m, cmd
  182. }
  183. if m.modelList.GetModelType() == LargeModelType {
  184. m.modelList.SetInputPlaceholder(smallModelInputPlaceholder)
  185. return m, m.modelList.SetModelType(SmallModelType)
  186. } else {
  187. m.modelList.SetInputPlaceholder(largeModelInputPlaceholder)
  188. return m, m.modelList.SetModelType(LargeModelType)
  189. }
  190. case key.Matches(msg, m.keyMap.Close):
  191. if m.needsAPIKey {
  192. if m.isAPIKeyValid {
  193. return m, nil
  194. }
  195. // Go back to model selection
  196. m.needsAPIKey = false
  197. m.selectedModel = nil
  198. m.isAPIKeyValid = false
  199. m.apiKeyValue = ""
  200. m.apiKeyInput.Reset()
  201. return m, nil
  202. }
  203. return m, util.CmdHandler(dialogs.CloseDialogMsg{})
  204. default:
  205. if m.needsAPIKey {
  206. u, cmd := m.apiKeyInput.Update(msg)
  207. m.apiKeyInput = u.(*APIKeyInput)
  208. return m, cmd
  209. } else {
  210. u, cmd := m.modelList.Update(msg)
  211. m.modelList = u
  212. return m, cmd
  213. }
  214. }
  215. case tea.PasteMsg:
  216. if m.needsAPIKey {
  217. u, cmd := m.apiKeyInput.Update(msg)
  218. m.apiKeyInput = u.(*APIKeyInput)
  219. return m, cmd
  220. } else {
  221. var cmd tea.Cmd
  222. m.modelList, cmd = m.modelList.Update(msg)
  223. return m, cmd
  224. }
  225. case spinner.TickMsg:
  226. u, cmd := m.apiKeyInput.Update(msg)
  227. m.apiKeyInput = u.(*APIKeyInput)
  228. return m, cmd
  229. }
  230. return m, nil
  231. }
  232. func (m *modelDialogCmp) View() string {
  233. t := styles.CurrentTheme()
  234. if m.needsAPIKey {
  235. // Show API key input
  236. m.keyMap.isAPIKeyHelp = true
  237. m.keyMap.isAPIKeyValid = m.isAPIKeyValid
  238. apiKeyView := m.apiKeyInput.View()
  239. apiKeyView = t.S().Base.Width(m.width - 3).Height(lipgloss.Height(apiKeyView)).PaddingLeft(1).Render(apiKeyView)
  240. content := lipgloss.JoinVertical(
  241. lipgloss.Left,
  242. t.S().Base.Padding(0, 1, 1, 1).Render(core.Title(m.apiKeyInput.GetTitle(), m.width-4)),
  243. apiKeyView,
  244. "",
  245. t.S().Base.Width(m.width-2).PaddingLeft(1).AlignHorizontal(lipgloss.Left).Render(m.help.View(m.keyMap)),
  246. )
  247. return m.style().Render(content)
  248. }
  249. // Show model selection
  250. listView := m.modelList.View()
  251. radio := m.modelTypeRadio()
  252. content := lipgloss.JoinVertical(
  253. lipgloss.Left,
  254. t.S().Base.Padding(0, 1, 1, 1).Render(core.Title("Switch Model", m.width-lipgloss.Width(radio)-5)+" "+radio),
  255. listView,
  256. "",
  257. t.S().Base.Width(m.width-2).PaddingLeft(1).AlignHorizontal(lipgloss.Left).Render(m.help.View(m.keyMap)),
  258. )
  259. return m.style().Render(content)
  260. }
  261. func (m *modelDialogCmp) Cursor() *tea.Cursor {
  262. if m.needsAPIKey {
  263. cursor := m.apiKeyInput.Cursor()
  264. if cursor != nil {
  265. cursor = m.moveCursor(cursor)
  266. return cursor
  267. }
  268. } else {
  269. cursor := m.modelList.Cursor()
  270. if cursor != nil {
  271. cursor = m.moveCursor(cursor)
  272. return cursor
  273. }
  274. }
  275. return nil
  276. }
  277. func (m *modelDialogCmp) style() lipgloss.Style {
  278. t := styles.CurrentTheme()
  279. return t.S().Base.
  280. Width(m.width).
  281. Border(lipgloss.RoundedBorder()).
  282. BorderForeground(t.BorderFocus)
  283. }
  284. func (m *modelDialogCmp) listWidth() int {
  285. return defaultWidth - 2 // 4 for padding
  286. }
  287. func (m *modelDialogCmp) listHeight() int {
  288. items := m.modelList.Items()
  289. listHeigh := len(items) + 2 + 4
  290. return min(listHeigh, m.wHeight/2)
  291. }
  292. func (m *modelDialogCmp) Position() (int, int) {
  293. row := m.wHeight/4 - 2 // just a bit above the center
  294. col := m.wWidth / 2
  295. col -= m.width / 2
  296. return row, col
  297. }
  298. func (m *modelDialogCmp) moveCursor(cursor *tea.Cursor) *tea.Cursor {
  299. row, col := m.Position()
  300. if m.needsAPIKey {
  301. offset := row + 3 // Border + title + API key input offset
  302. cursor.Y += offset
  303. cursor.X = cursor.X + col + 2
  304. } else {
  305. offset := row + 3 // Border + title
  306. cursor.Y += offset
  307. cursor.X = cursor.X + col + 2
  308. }
  309. return cursor
  310. }
  311. func (m *modelDialogCmp) ID() dialogs.DialogID {
  312. return ModelsDialogID
  313. }
  314. func (m *modelDialogCmp) modelTypeRadio() string {
  315. t := styles.CurrentTheme()
  316. choices := []string{"Large Task", "Small Task"}
  317. iconSelected := "◉"
  318. iconUnselected := "○"
  319. if m.modelList.GetModelType() == LargeModelType {
  320. return t.S().Base.Foreground(t.FgHalfMuted).Render(iconSelected + " " + choices[0] + " " + iconUnselected + " " + choices[1])
  321. }
  322. return t.S().Base.Foreground(t.FgHalfMuted).Render(iconUnselected + " " + choices[0] + " " + iconSelected + " " + choices[1])
  323. }
  324. func (m *modelDialogCmp) isProviderConfigured(providerID string) bool {
  325. cfg := config.Get()
  326. if _, ok := cfg.Providers.Get(providerID); ok {
  327. return true
  328. }
  329. return false
  330. }
  331. func (m *modelDialogCmp) getProvider(providerID provider.InferenceProvider) (*provider.Provider, error) {
  332. providers, err := config.Providers()
  333. if err != nil {
  334. return nil, err
  335. }
  336. for _, p := range providers {
  337. if p.ID == providerID {
  338. return &p, nil
  339. }
  340. }
  341. return nil, nil
  342. }
  343. func (m *modelDialogCmp) saveAPIKeyAndContinue(apiKey string) tea.Cmd {
  344. if m.selectedModel == nil {
  345. return util.ReportError(fmt.Errorf("no model selected"))
  346. }
  347. cfg := config.Get()
  348. err := cfg.SetProviderAPIKey(string(m.selectedModel.Provider.ID), apiKey)
  349. if err != nil {
  350. return util.ReportError(fmt.Errorf("failed to save API key: %w", err))
  351. }
  352. // Reset API key state and continue with model selection
  353. selectedModel := *m.selectedModel
  354. return tea.Sequence(
  355. util.CmdHandler(dialogs.CloseDialogMsg{}),
  356. util.CmdHandler(ModelSelectedMsg{
  357. Model: config.SelectedModel{
  358. Model: selectedModel.Model.ID,
  359. Provider: string(selectedModel.Provider.ID),
  360. },
  361. ModelType: m.selectedModelType,
  362. }),
  363. )
  364. }