sidebar.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package model
  2. import (
  3. "cmp"
  4. "fmt"
  5. "charm.land/lipgloss/v2"
  6. "github.com/charmbracelet/crush/internal/ui/common"
  7. "github.com/charmbracelet/crush/internal/ui/logo"
  8. uv "github.com/charmbracelet/ultraviolet"
  9. "github.com/charmbracelet/ultraviolet/layout"
  10. )
  11. // modelInfo renders the current model information including reasoning
  12. // settings and context usage/cost for the sidebar.
  13. func (m *UI) modelInfo(width int) string {
  14. model := m.selectedLargeModel()
  15. reasoningInfo := ""
  16. providerName := ""
  17. if model != nil {
  18. // Get provider name first
  19. providerConfig, ok := m.com.Config().Providers.Get(model.ModelCfg.Provider)
  20. if ok {
  21. providerName = providerConfig.Name
  22. // Only check reasoning if model can reason
  23. if model.CatwalkCfg.CanReason {
  24. if len(model.CatwalkCfg.ReasoningLevels) == 0 {
  25. if model.ModelCfg.Think {
  26. reasoningInfo = "Thinking On"
  27. } else {
  28. reasoningInfo = "Thinking Off"
  29. }
  30. } else {
  31. reasoningEffort := cmp.Or(model.ModelCfg.ReasoningEffort, model.CatwalkCfg.DefaultReasoningEffort)
  32. reasoningInfo = fmt.Sprintf("Reasoning %s", common.FormatReasoningEffort(reasoningEffort))
  33. }
  34. }
  35. }
  36. }
  37. var modelContext *common.ModelContextInfo
  38. if model != nil && m.session != nil {
  39. modelContext = &common.ModelContextInfo{
  40. ContextUsed: m.session.CompletionTokens + m.session.PromptTokens,
  41. Cost: m.session.Cost,
  42. ModelContext: model.CatwalkCfg.ContextWindow,
  43. }
  44. }
  45. return common.ModelInfo(m.com.Styles, model.CatwalkCfg.Name, providerName, reasoningInfo, modelContext, width)
  46. }
  47. // getDynamicHeightLimits will give us the num of items to show in each section based on the hight
  48. // some items are more important than others.
  49. func getDynamicHeightLimits(availableHeight int) (maxFiles, maxLSPs, maxMCPs int) {
  50. const (
  51. minItemsPerSection = 2
  52. defaultMaxFilesShown = 10
  53. defaultMaxLSPsShown = 8
  54. defaultMaxMCPsShown = 8
  55. minAvailableHeightLimit = 10
  56. )
  57. // If we have very little space, use minimum values
  58. if availableHeight < minAvailableHeightLimit {
  59. return minItemsPerSection, minItemsPerSection, minItemsPerSection
  60. }
  61. // Distribute available height among the three sections
  62. // Give priority to files, then LSPs, then MCPs
  63. totalSections := 3
  64. heightPerSection := availableHeight / totalSections
  65. // Calculate limits for each section, ensuring minimums
  66. maxFiles = max(minItemsPerSection, min(defaultMaxFilesShown, heightPerSection))
  67. maxLSPs = max(minItemsPerSection, min(defaultMaxLSPsShown, heightPerSection))
  68. maxMCPs = max(minItemsPerSection, min(defaultMaxMCPsShown, heightPerSection))
  69. // If we have extra space, give it to files first
  70. remainingHeight := availableHeight - (maxFiles + maxLSPs + maxMCPs)
  71. if remainingHeight > 0 {
  72. extraForFiles := min(remainingHeight, defaultMaxFilesShown-maxFiles)
  73. maxFiles += extraForFiles
  74. remainingHeight -= extraForFiles
  75. if remainingHeight > 0 {
  76. extraForLSPs := min(remainingHeight, defaultMaxLSPsShown-maxLSPs)
  77. maxLSPs += extraForLSPs
  78. remainingHeight -= extraForLSPs
  79. if remainingHeight > 0 {
  80. maxMCPs += min(remainingHeight, defaultMaxMCPsShown-maxMCPs)
  81. }
  82. }
  83. }
  84. return maxFiles, maxLSPs, maxMCPs
  85. }
  86. // sidebar renders the chat sidebar containing session title, working
  87. // directory, model info, file list, LSP status, and MCP status.
  88. func (m *UI) drawSidebar(scr uv.Screen, area uv.Rectangle) {
  89. if m.session == nil {
  90. return
  91. }
  92. const logoHeightBreakpoint = 30
  93. t := m.com.Styles
  94. width := area.Dx()
  95. height := area.Dy()
  96. title := t.Muted.Width(width).MaxHeight(2).Render(m.session.Title)
  97. cwd := common.PrettyPath(t, m.com.Store().WorkingDir(), width)
  98. sidebarLogo := m.sidebarLogo
  99. if height < logoHeightBreakpoint {
  100. sidebarLogo = logo.SmallRender(m.com.Styles, width)
  101. }
  102. blocks := []string{
  103. sidebarLogo,
  104. title,
  105. "",
  106. cwd,
  107. "",
  108. m.modelInfo(width),
  109. "",
  110. }
  111. sidebarHeader := lipgloss.JoinVertical(
  112. lipgloss.Left,
  113. blocks...,
  114. )
  115. _, remainingHeightArea := layout.SplitVertical(m.layout.sidebar, layout.Fixed(lipgloss.Height(sidebarHeader)))
  116. remainingHeight := remainingHeightArea.Dy() - 10
  117. maxFiles, maxLSPs, maxMCPs := getDynamicHeightLimits(remainingHeight)
  118. lspSection := m.lspInfo(width, maxLSPs, true)
  119. mcpSection := m.mcpInfo(width, maxMCPs, true)
  120. filesSection := m.filesInfo(m.com.Store().WorkingDir(), width, maxFiles, true)
  121. uv.NewStyledString(
  122. lipgloss.NewStyle().
  123. MaxWidth(width).
  124. MaxHeight(height).
  125. Render(
  126. lipgloss.JoinVertical(
  127. lipgloss.Left,
  128. sidebarHeader,
  129. filesSection,
  130. "",
  131. lspSection,
  132. "",
  133. mcpSection,
  134. ),
  135. ),
  136. ).Draw(scr, area)
  137. }