2
0

models.go 10 KB

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