status.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package core
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/charmbracelet/lipgloss"
  8. "github.com/opencode-ai/opencode/internal/config"
  9. "github.com/opencode-ai/opencode/internal/llm/models"
  10. "github.com/opencode-ai/opencode/internal/lsp"
  11. "github.com/opencode-ai/opencode/internal/lsp/protocol"
  12. "github.com/opencode-ai/opencode/internal/pubsub"
  13. "github.com/opencode-ai/opencode/internal/session"
  14. "github.com/opencode-ai/opencode/internal/tui/components/chat"
  15. "github.com/opencode-ai/opencode/internal/tui/styles"
  16. "github.com/opencode-ai/opencode/internal/tui/util"
  17. )
  18. type StatusCmp interface {
  19. tea.Model
  20. SetHelpMsg(string)
  21. }
  22. type statusCmp struct {
  23. info util.InfoMsg
  24. width int
  25. messageTTL time.Duration
  26. lspClients map[string]*lsp.Client
  27. session session.Session
  28. }
  29. // clearMessageCmd is a command that clears status messages after a timeout
  30. func (m statusCmp) clearMessageCmd(ttl time.Duration) tea.Cmd {
  31. return tea.Tick(ttl, func(time.Time) tea.Msg {
  32. return util.ClearStatusMsg{}
  33. })
  34. }
  35. func (m statusCmp) Init() tea.Cmd {
  36. return nil
  37. }
  38. func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  39. switch msg := msg.(type) {
  40. case tea.WindowSizeMsg:
  41. m.width = msg.Width
  42. return m, nil
  43. case chat.SessionSelectedMsg:
  44. m.session = msg
  45. case chat.SessionClearedMsg:
  46. m.session = session.Session{}
  47. case pubsub.Event[session.Session]:
  48. if msg.Type == pubsub.UpdatedEvent {
  49. if m.session.ID == msg.Payload.ID {
  50. m.session = msg.Payload
  51. }
  52. }
  53. case util.InfoMsg:
  54. m.info = msg
  55. ttl := msg.TTL
  56. if ttl == 0 {
  57. ttl = m.messageTTL
  58. }
  59. return m, m.clearMessageCmd(ttl)
  60. case util.ClearStatusMsg:
  61. m.info = util.InfoMsg{}
  62. }
  63. return m, nil
  64. }
  65. var helpWidget = styles.Padded.Background(styles.ForgroundMid).Foreground(styles.BackgroundDarker).Bold(true).Render("ctrl+? help")
  66. func formatTokensAndCost(tokens int64, cost float64) string {
  67. // Format tokens in human-readable format (e.g., 110K, 1.2M)
  68. var formattedTokens string
  69. switch {
  70. case tokens >= 1_000_000:
  71. formattedTokens = fmt.Sprintf("%.1fM", float64(tokens)/1_000_000)
  72. case tokens >= 1_000:
  73. formattedTokens = fmt.Sprintf("%.1fK", float64(tokens)/1_000)
  74. default:
  75. formattedTokens = fmt.Sprintf("%d", tokens)
  76. }
  77. // Remove .0 suffix if present
  78. if strings.HasSuffix(formattedTokens, ".0K") {
  79. formattedTokens = strings.Replace(formattedTokens, ".0K", "K", 1)
  80. }
  81. if strings.HasSuffix(formattedTokens, ".0M") {
  82. formattedTokens = strings.Replace(formattedTokens, ".0M", "M", 1)
  83. }
  84. // Format cost with $ symbol and 2 decimal places
  85. formattedCost := fmt.Sprintf("$%.2f", cost)
  86. return fmt.Sprintf("Tokens: %s, Cost: %s", formattedTokens, formattedCost)
  87. }
  88. func (m statusCmp) View() string {
  89. status := helpWidget
  90. if m.session.ID != "" {
  91. tokens := formatTokensAndCost(m.session.PromptTokens+m.session.CompletionTokens, m.session.Cost)
  92. tokensStyle := styles.Padded.
  93. Background(styles.Forground).
  94. Foreground(styles.BackgroundDim).
  95. Render(tokens)
  96. status += tokensStyle
  97. }
  98. diagnostics := styles.Padded.Background(styles.BackgroundDarker).Render(m.projectDiagnostics())
  99. if m.info.Msg != "" {
  100. infoStyle := styles.Padded.
  101. Foreground(styles.Base).
  102. Width(m.availableFooterMsgWidth(diagnostics))
  103. switch m.info.Type {
  104. case util.InfoTypeInfo:
  105. infoStyle = infoStyle.Background(styles.BorderColor)
  106. case util.InfoTypeWarn:
  107. infoStyle = infoStyle.Background(styles.Peach)
  108. case util.InfoTypeError:
  109. infoStyle = infoStyle.Background(styles.Red)
  110. }
  111. // Truncate message if it's longer than available width
  112. msg := m.info.Msg
  113. availWidth := m.availableFooterMsgWidth(diagnostics) - 10
  114. if len(msg) > availWidth && availWidth > 0 {
  115. msg = msg[:availWidth] + "..."
  116. }
  117. status += infoStyle.Render(msg)
  118. } else {
  119. status += styles.Padded.
  120. Foreground(styles.Base).
  121. Background(styles.BackgroundDim).
  122. Width(m.availableFooterMsgWidth(diagnostics)).
  123. Render("")
  124. }
  125. status += diagnostics
  126. status += m.model()
  127. return status
  128. }
  129. func (m *statusCmp) projectDiagnostics() string {
  130. // Check if any LSP server is still initializing
  131. initializing := false
  132. for _, client := range m.lspClients {
  133. if client.GetServerState() == lsp.StateStarting {
  134. initializing = true
  135. break
  136. }
  137. }
  138. // If any server is initializing, show that status
  139. if initializing {
  140. return lipgloss.NewStyle().
  141. Background(styles.BackgroundDarker).
  142. Foreground(styles.Peach).
  143. Render(fmt.Sprintf("%s Initializing LSP...", styles.SpinnerIcon))
  144. }
  145. errorDiagnostics := []protocol.Diagnostic{}
  146. warnDiagnostics := []protocol.Diagnostic{}
  147. hintDiagnostics := []protocol.Diagnostic{}
  148. infoDiagnostics := []protocol.Diagnostic{}
  149. for _, client := range m.lspClients {
  150. for _, d := range client.GetDiagnostics() {
  151. for _, diag := range d {
  152. switch diag.Severity {
  153. case protocol.SeverityError:
  154. errorDiagnostics = append(errorDiagnostics, diag)
  155. case protocol.SeverityWarning:
  156. warnDiagnostics = append(warnDiagnostics, diag)
  157. case protocol.SeverityHint:
  158. hintDiagnostics = append(hintDiagnostics, diag)
  159. case protocol.SeverityInformation:
  160. infoDiagnostics = append(infoDiagnostics, diag)
  161. }
  162. }
  163. }
  164. }
  165. if len(errorDiagnostics) == 0 && len(warnDiagnostics) == 0 && len(hintDiagnostics) == 0 && len(infoDiagnostics) == 0 {
  166. return "No diagnostics"
  167. }
  168. diagnostics := []string{}
  169. if len(errorDiagnostics) > 0 {
  170. errStr := lipgloss.NewStyle().
  171. Background(styles.BackgroundDarker).
  172. Foreground(styles.Error).
  173. Render(fmt.Sprintf("%s %d", styles.ErrorIcon, len(errorDiagnostics)))
  174. diagnostics = append(diagnostics, errStr)
  175. }
  176. if len(warnDiagnostics) > 0 {
  177. warnStr := lipgloss.NewStyle().
  178. Background(styles.BackgroundDarker).
  179. Foreground(styles.Warning).
  180. Render(fmt.Sprintf("%s %d", styles.WarningIcon, len(warnDiagnostics)))
  181. diagnostics = append(diagnostics, warnStr)
  182. }
  183. if len(hintDiagnostics) > 0 {
  184. hintStr := lipgloss.NewStyle().
  185. Background(styles.BackgroundDarker).
  186. Foreground(styles.Text).
  187. Render(fmt.Sprintf("%s %d", styles.HintIcon, len(hintDiagnostics)))
  188. diagnostics = append(diagnostics, hintStr)
  189. }
  190. if len(infoDiagnostics) > 0 {
  191. infoStr := lipgloss.NewStyle().
  192. Background(styles.BackgroundDarker).
  193. Foreground(styles.Peach).
  194. Render(fmt.Sprintf("%s %d", styles.InfoIcon, len(infoDiagnostics)))
  195. diagnostics = append(diagnostics, infoStr)
  196. }
  197. return strings.Join(diagnostics, " ")
  198. }
  199. func (m statusCmp) availableFooterMsgWidth(diagnostics string) int {
  200. tokens := ""
  201. tokensWidth := 0
  202. if m.session.ID != "" {
  203. tokens = formatTokensAndCost(m.session.PromptTokens+m.session.CompletionTokens, m.session.Cost)
  204. tokensWidth = lipgloss.Width(tokens) + 2
  205. }
  206. return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(m.model())-lipgloss.Width(diagnostics)-tokensWidth)
  207. }
  208. func (m statusCmp) model() string {
  209. cfg := config.Get()
  210. coder, ok := cfg.Agents[config.AgentCoder]
  211. if !ok {
  212. return "Unknown"
  213. }
  214. model := models.SupportedModels[coder.Model]
  215. return styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render(model.Name)
  216. }
  217. func (m statusCmp) SetHelpMsg(s string) {
  218. helpWidget = styles.Padded.Background(styles.Forground).Foreground(styles.BackgroundDarker).Bold(true).Render(s)
  219. }
  220. func NewStatusCmp(lspClients map[string]*lsp.Client) StatusCmp {
  221. return &statusCmp{
  222. messageTTL: 10 * time.Second,
  223. lspClients: lspClients,
  224. }
  225. }