This repository was archived by the owner on Sep 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathstatus.go
More file actions
351 lines (300 loc) · 9.94 KB
/
status.go
File metadata and controls
351 lines (300 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package core
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/llm/models"
"github.com/opencode-ai/opencode/internal/lsp"
"github.com/opencode-ai/opencode/internal/lsp/protocol"
"github.com/opencode-ai/opencode/internal/pubsub"
"github.com/opencode-ai/opencode/internal/session"
"github.com/opencode-ai/opencode/internal/tui/components/chat"
"github.com/opencode-ai/opencode/internal/tui/styles"
"github.com/opencode-ai/opencode/internal/tui/theme"
"github.com/opencode-ai/opencode/internal/tui/util"
)
type StatusCmp interface {
tea.Model
}
type statusCmp struct {
info util.InfoMsg
width int
messageTTL time.Duration
lspClients map[string]*lsp.Client
session session.Session
}
// clearMessageCmd is a command that clears status messages after a timeout
func (m statusCmp) clearMessageCmd(ttl time.Duration) tea.Cmd {
return tea.Tick(ttl, func(time.Time) tea.Msg {
return util.ClearStatusMsg{}
})
}
func (m statusCmp) Init() tea.Cmd {
return nil
}
func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
return m, nil
case chat.SessionSelectedMsg:
m.session = msg
case chat.SessionClearedMsg:
m.session = session.Session{}
case pubsub.Event[session.Session]:
if msg.Type == pubsub.UpdatedEvent {
if m.session.ID == msg.Payload.ID {
m.session = msg.Payload
}
}
case util.InfoMsg:
m.info = msg
ttl := msg.TTL
if ttl == 0 {
ttl = m.messageTTL
}
return m, m.clearMessageCmd(ttl)
case util.ClearStatusMsg:
m.info = util.InfoMsg{}
}
return m, nil
}
var helpWidget = ""
// getHelpWidget returns the help widget with current theme colors
func getHelpWidget() string {
t := theme.CurrentTheme()
helpText := "ctrl+? help"
helpStyle := styles.Padded().Bold(true)
// In transparency mode, remove background
if theme.IsTransparentBackground() {
helpStyle = helpStyle.Foreground(t.TextMuted())
} else {
helpStyle = helpStyle.
Background(t.TextMuted()).
Foreground(t.BackgroundDarker())
}
return helpStyle.Render(helpText)
}
func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {
// Format tokens in human-readable format (e.g., 110K, 1.2M)
var formattedTokens string
switch {
case tokens >= 1_000_000:
formattedTokens = fmt.Sprintf("%.1fM", float64(tokens)/1_000_000)
case tokens >= 1_000:
formattedTokens = fmt.Sprintf("%.1fK", float64(tokens)/1_000)
default:
formattedTokens = fmt.Sprintf("%d", tokens)
}
// Remove .0 suffix if present
if strings.HasSuffix(formattedTokens, ".0K") {
formattedTokens = strings.Replace(formattedTokens, ".0K", "K", 1)
}
if strings.HasSuffix(formattedTokens, ".0M") {
formattedTokens = strings.Replace(formattedTokens, ".0M", "M", 1)
}
// Format cost with $ symbol and 2 decimal places
formattedCost := fmt.Sprintf("$%.2f", cost)
percentage := (float64(tokens) / float64(contextWindow)) * 100
if percentage > 80 {
// add the warning icon and percentage
formattedTokens = fmt.Sprintf("%s(%d%%)", styles.WarningIcon, int(percentage))
}
return fmt.Sprintf("Context: %s, Cost: %s", formattedTokens, formattedCost)
}
func (m statusCmp) View() string {
t := theme.CurrentTheme()
modelID := config.Get().Agents[config.AgentCoder].Model
model := models.SupportedModels[modelID]
// Initialize the help widget
status := getHelpWidget()
tokenInfoWidth := 0
if m.session.ID != "" {
totalTokens := m.session.PromptTokens + m.session.CompletionTokens
tokens := formatTokensAndCost(totalTokens, model.ContextWindow, m.session.Cost)
tokensStyle := styles.Padded()
// In transparency mode, remove background
if theme.IsTransparentBackground() {
tokensStyle = tokensStyle.Foreground(t.Text())
} else {
tokensStyle = tokensStyle.
Background(t.Text()).
Foreground(t.BackgroundSecondary())
}
percentage := (float64(totalTokens) / float64(model.ContextWindow)) * 100
if percentage > 80 {
// Even in transparency mode, show warning background for high token usage
tokensStyle = tokensStyle.Background(t.Warning())
}
tokenInfoWidth = lipgloss.Width(tokens) + 2
status += tokensStyle.Render(tokens)
}
diagnosticsStyle := styles.Padded()
// In transparency mode, remove background
if theme.IsTransparentBackground() {
diagnosticsStyle = diagnosticsStyle.Foreground(t.Text())
} else {
diagnosticsStyle = diagnosticsStyle.Background(t.BackgroundDarker())
}
diagnostics := diagnosticsStyle.Render(m.projectDiagnostics())
availableWidht := max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(m.model())-lipgloss.Width(diagnostics)-tokenInfoWidth)
if m.info.Msg != "" {
infoStyle := styles.Padded().Width(availableWidht)
if theme.IsTransparentBackground() {
// In transparent mode, use colored foreground on transparent background
switch m.info.Type {
case util.InfoTypeInfo:
infoStyle = infoStyle.Foreground(t.Info())
case util.InfoTypeWarn:
infoStyle = infoStyle.Foreground(t.Warning())
case util.InfoTypeError:
infoStyle = infoStyle.Foreground(t.Error())
}
} else {
// In non-transparent mode, use colored background with contrasting text
infoStyle = infoStyle.Foreground(t.Background())
switch m.info.Type {
case util.InfoTypeInfo:
infoStyle = infoStyle.Background(t.Info())
case util.InfoTypeWarn:
infoStyle = infoStyle.Background(t.Warning())
case util.InfoTypeError:
infoStyle = infoStyle.Background(t.Error())
}
}
infoWidth := availableWidht - 10
// Truncate message if it's longer than available width
msg := m.info.Msg
if len(msg) > infoWidth && infoWidth > 0 {
msg = msg[:infoWidth] + "..."
}
status += infoStyle.Render(msg)
} else {
emptyStyle := styles.Padded().Width(availableWidht)
// In transparency mode, remove background
if theme.IsTransparentBackground() {
emptyStyle = emptyStyle.Foreground(t.Text())
} else {
emptyStyle = emptyStyle.
Foreground(t.Text()).
Background(t.BackgroundSecondary())
}
status += emptyStyle.Render("")
}
status += diagnostics
status += m.model()
return status
}
func (m *statusCmp) projectDiagnostics() string {
t := theme.CurrentTheme()
// Check if any LSP server is still initializing
initializing := false
for _, client := range m.lspClients {
if client.GetServerState() == lsp.StateStarting {
initializing = true
break
}
}
// If any server is initializing, show that status
if initializing {
initStyle := lipgloss.NewStyle().Foreground(t.Warning())
// In transparency mode, remove background
if !theme.IsTransparentBackground() {
initStyle = initStyle.Background(t.BackgroundDarker())
}
return initStyle.Render(fmt.Sprintf("%s Initializing LSP...", styles.SpinnerIcon))
}
errorDiagnostics := []protocol.Diagnostic{}
warnDiagnostics := []protocol.Diagnostic{}
hintDiagnostics := []protocol.Diagnostic{}
infoDiagnostics := []protocol.Diagnostic{}
for _, client := range m.lspClients {
for _, d := range client.GetDiagnostics() {
for _, diag := range d {
switch diag.Severity {
case protocol.SeverityError:
errorDiagnostics = append(errorDiagnostics, diag)
case protocol.SeverityWarning:
warnDiagnostics = append(warnDiagnostics, diag)
case protocol.SeverityHint:
hintDiagnostics = append(hintDiagnostics, diag)
case protocol.SeverityInformation:
infoDiagnostics = append(infoDiagnostics, diag)
}
}
}
}
if len(errorDiagnostics) == 0 && len(warnDiagnostics) == 0 && len(hintDiagnostics) == 0 && len(infoDiagnostics) == 0 {
return "No diagnostics"
}
diagnostics := []string{}
if len(errorDiagnostics) > 0 {
errStyle := lipgloss.NewStyle().Foreground(t.Error())
if !theme.IsTransparentBackground() {
errStyle = errStyle.Background(t.BackgroundDarker())
}
errStr := errStyle.Render(fmt.Sprintf("%s %d", styles.ErrorIcon, len(errorDiagnostics)))
diagnostics = append(diagnostics, errStr)
}
if len(warnDiagnostics) > 0 {
warnStyle := lipgloss.NewStyle().Foreground(t.Warning())
if !theme.IsTransparentBackground() {
warnStyle = warnStyle.Background(t.BackgroundDarker())
}
warnStr := warnStyle.Render(fmt.Sprintf("%s %d", styles.WarningIcon, len(warnDiagnostics)))
diagnostics = append(diagnostics, warnStr)
}
if len(hintDiagnostics) > 0 {
hintStyle := lipgloss.NewStyle().Foreground(t.Text())
if !theme.IsTransparentBackground() {
hintStyle = hintStyle.Background(t.BackgroundDarker())
}
hintStr := hintStyle.Render(fmt.Sprintf("%s %d", styles.HintIcon, len(hintDiagnostics)))
diagnostics = append(diagnostics, hintStr)
}
if len(infoDiagnostics) > 0 {
infoStyle := lipgloss.NewStyle().Foreground(t.Info())
if !theme.IsTransparentBackground() {
infoStyle = infoStyle.Background(t.BackgroundDarker())
}
infoStr := infoStyle.Render(fmt.Sprintf("%s %d", styles.InfoIcon, len(infoDiagnostics)))
diagnostics = append(diagnostics, infoStr)
}
return strings.Join(diagnostics, " ")
}
func (m statusCmp) availableFooterMsgWidth(diagnostics, tokenInfo string) int {
tokensWidth := 0
if m.session.ID != "" {
tokensWidth = lipgloss.Width(tokenInfo) + 2
}
return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(m.model())-lipgloss.Width(diagnostics)-tokensWidth)
}
func (m statusCmp) model() string {
t := theme.CurrentTheme()
cfg := config.Get()
coder, ok := cfg.Agents[config.AgentCoder]
if !ok {
return "Unknown"
}
model := models.SupportedModels[coder.Model]
modelStyle := styles.Padded()
// In transparency mode, remove background
if theme.IsTransparentBackground() {
modelStyle = modelStyle.Foreground(t.Secondary())
} else {
modelStyle = modelStyle.
Background(t.Secondary()).
Foreground(t.Background())
}
return modelStyle.Render(model.Name)
}
func NewStatusCmp(lspClients map[string]*lsp.Client) StatusCmp {
helpWidget = getHelpWidget()
return &statusCmp{
messageTTL: 10 * time.Second,
lspClients: lspClients,
}
}