status.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package status
  2. import (
  3. "fmt"
  4. "strings"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/lipgloss/v2"
  7. "github.com/sst/opencode/internal/app"
  8. "github.com/sst/opencode/internal/styles"
  9. "github.com/sst/opencode/internal/theme"
  10. )
  11. type StatusComponent interface {
  12. tea.Model
  13. tea.ViewModel
  14. }
  15. type statusComponent struct {
  16. app *app.App
  17. width int
  18. }
  19. func (m statusComponent) Init() tea.Cmd {
  20. return nil
  21. }
  22. func (m statusComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  23. switch msg := msg.(type) {
  24. case tea.WindowSizeMsg:
  25. m.width = msg.Width
  26. return m, nil
  27. }
  28. return m, nil
  29. }
  30. func (m statusComponent) logo() string {
  31. t := theme.CurrentTheme()
  32. base := styles.NewStyle().Foreground(t.TextMuted()).Background(t.BackgroundElement()).Render
  33. emphasis := styles.NewStyle().
  34. Foreground(t.Text()).
  35. Background(t.BackgroundElement()).
  36. Bold(true).
  37. Render
  38. open := base("open")
  39. code := emphasis("code ")
  40. version := base(m.app.Version)
  41. return styles.NewStyle().
  42. Background(t.BackgroundElement()).
  43. Padding(0, 1).
  44. Render(open + code + version)
  45. }
  46. func formatTokensAndCost(tokens float64, contextWindow float64, cost float64) string {
  47. // Format tokens in human-readable format (e.g., 110K, 1.2M)
  48. var formattedTokens string
  49. switch {
  50. case tokens >= 1_000_000:
  51. formattedTokens = fmt.Sprintf("%.1fM", float64(tokens)/1_000_000)
  52. case tokens >= 1_000:
  53. formattedTokens = fmt.Sprintf("%.1fK", float64(tokens)/1_000)
  54. default:
  55. formattedTokens = fmt.Sprintf("%d", int(tokens))
  56. }
  57. // Remove .0 suffix if present
  58. if strings.HasSuffix(formattedTokens, ".0K") {
  59. formattedTokens = strings.Replace(formattedTokens, ".0K", "K", 1)
  60. }
  61. if strings.HasSuffix(formattedTokens, ".0M") {
  62. formattedTokens = strings.Replace(formattedTokens, ".0M", "M", 1)
  63. }
  64. // Format cost with $ symbol and 2 decimal places
  65. formattedCost := fmt.Sprintf("$%.2f", cost)
  66. percentage := (float64(tokens) / float64(contextWindow)) * 100
  67. return fmt.Sprintf(
  68. "Context: %s (%d%%), Cost: %s",
  69. formattedTokens,
  70. int(percentage),
  71. formattedCost,
  72. )
  73. }
  74. func (m statusComponent) View() string {
  75. t := theme.CurrentTheme()
  76. logo := m.logo()
  77. cwd := styles.NewStyle().
  78. Foreground(t.TextMuted()).
  79. Background(t.BackgroundPanel()).
  80. Padding(0, 1).
  81. Render(m.app.Info.Path.Cwd)
  82. sessionInfo := ""
  83. if m.app.Session.ID != "" {
  84. tokens := float64(0)
  85. cost := float64(0)
  86. contextWindow := m.app.Model.Limit.Context
  87. for _, message := range m.app.Messages {
  88. cost += message.Metadata.Assistant.Cost
  89. usage := message.Metadata.Assistant.Tokens
  90. if usage.Output > 0 {
  91. if message.Metadata.Assistant.Summary {
  92. tokens = usage.Output
  93. continue
  94. }
  95. tokens = (usage.Input +
  96. usage.Cache.Write +
  97. usage.Cache.Read +
  98. usage.Output +
  99. usage.Reasoning)
  100. }
  101. }
  102. sessionInfo = styles.NewStyle().
  103. Foreground(t.TextMuted()).
  104. Background(t.BackgroundElement()).
  105. Padding(0, 1).
  106. Render(formatTokensAndCost(tokens, contextWindow, cost))
  107. }
  108. // diagnostics := styles.Padded().Background(t.BackgroundElement()).Render(m.projectDiagnostics())
  109. space := max(
  110. 0,
  111. m.width-lipgloss.Width(logo)-lipgloss.Width(cwd)-lipgloss.Width(sessionInfo),
  112. )
  113. spacer := styles.NewStyle().Background(t.BackgroundPanel()).Width(space).Render("")
  114. status := logo + cwd + spacer + sessionInfo
  115. blank := styles.NewStyle().Background(t.Background()).Width(m.width).Render("")
  116. return blank + "\n" + status
  117. }
  118. func NewStatusCmp(app *app.App) StatusComponent {
  119. statusComponent := &statusComponent{
  120. app: app,
  121. }
  122. return statusComponent
  123. }