Context Window Warning (#152)

* context window warning & compact command

* auto compact

* fix permissions

* update readme

* fix 3.5 context window

* small update

* remove unused interface

* remove unused msg
This commit is contained in:
Kujtim Hoxha 2025-05-09 19:30:57 +02:00 committed by GitHub
commit 90084ce43d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 537 additions and 98 deletions

View file

@ -21,7 +21,6 @@ import (
type StatusCmp interface {
tea.Model
SetHelpWidgetMsg(string)
}
type statusCmp struct {
@ -74,11 +73,9 @@ func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var helpWidget = ""
// getHelpWidget returns the help widget with current theme colors
func getHelpWidget(helpText string) string {
func getHelpWidget() string {
t := theme.CurrentTheme()
if helpText == "" {
helpText = "ctrl+? help"
}
helpText := "ctrl+? help"
return styles.Padded().
Background(t.TextMuted()).
@ -87,7 +84,7 @@ func getHelpWidget(helpText string) string {
Render(helpText)
}
func formatTokensAndCost(tokens int64, cost float64) string {
func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {
// Format tokens in human-readable format (e.g., 110K, 1.2M)
var formattedTokens string
switch {
@ -110,32 +107,48 @@ func formatTokensAndCost(tokens int64, cost float64) string {
// Format cost with $ symbol and 2 decimal places
formattedCost := fmt.Sprintf("$%.2f", cost)
return fmt.Sprintf("Tokens: %s, Cost: %s", formattedTokens, formattedCost)
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("")
status := getHelpWidget()
tokenInfoWidth := 0
if m.session.ID != "" {
tokens := formatTokensAndCost(m.session.PromptTokens+m.session.CompletionTokens, m.session.Cost)
totalTokens := m.session.PromptTokens + m.session.CompletionTokens
tokens := formatTokensAndCost(totalTokens, model.ContextWindow, m.session.Cost)
tokensStyle := styles.Padded().
Background(t.Text()).
Foreground(t.BackgroundSecondary()).
Render(tokens)
status += tokensStyle
Foreground(t.BackgroundSecondary())
percentage := (float64(totalTokens) / float64(model.ContextWindow)) * 100
if percentage > 80 {
tokensStyle = tokensStyle.Background(t.Warning())
}
tokenInfoWidth = lipgloss.Width(tokens) + 2
status += tokensStyle.Render(tokens)
}
diagnostics := styles.Padded().
Background(t.BackgroundDarker()).
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().
Foreground(t.Background()).
Width(m.availableFooterMsgWidth(diagnostics))
Width(availableWidht)
switch m.info.Type {
case util.InfoTypeInfo:
@ -146,18 +159,18 @@ func (m statusCmp) View() string {
infoStyle = infoStyle.Background(t.Error())
}
infoWidth := availableWidht - 10
// Truncate message if it's longer than available width
msg := m.info.Msg
availWidth := m.availableFooterMsgWidth(diagnostics) - 10
if len(msg) > availWidth && availWidth > 0 {
msg = msg[:availWidth] + "..."
if len(msg) > infoWidth && infoWidth > 0 {
msg = msg[:infoWidth] + "..."
}
status += infoStyle.Render(msg)
} else {
status += styles.Padded().
Foreground(t.Text()).
Background(t.BackgroundSecondary()).
Width(m.availableFooterMsgWidth(diagnostics)).
Width(availableWidht).
Render("")
}
@ -245,12 +258,10 @@ func (m *statusCmp) projectDiagnostics() string {
return strings.Join(diagnostics, " ")
}
func (m statusCmp) availableFooterMsgWidth(diagnostics string) int {
tokens := ""
func (m statusCmp) availableFooterMsgWidth(diagnostics, tokenInfo string) int {
tokensWidth := 0
if m.session.ID != "" {
tokens = formatTokensAndCost(m.session.PromptTokens+m.session.CompletionTokens, m.session.Cost)
tokensWidth = lipgloss.Width(tokens) + 2
tokensWidth = lipgloss.Width(tokenInfo) + 2
}
return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(m.model())-lipgloss.Width(diagnostics)-tokensWidth)
}
@ -272,14 +283,8 @@ func (m statusCmp) model() string {
Render(model.Name)
}
func (m statusCmp) SetHelpWidgetMsg(s string) {
// Update the help widget text using the getHelpWidget function
helpWidget = getHelpWidget(s)
}
func NewStatusCmp(lspClients map[string]*lsp.Client) StatusCmp {
// Initialize the help widget with default text
helpWidget = getHelpWidget("")
helpWidget = getHelpWidget()
return &statusCmp{
messageTTL: 10 * time.Second,

View file

@ -302,11 +302,8 @@ func (f *filepickerCmp) View() string {
}
if file.IsDir() {
filename = filename + "/"
} else if isExtSupported(file.Name()) {
filename = filename
} else {
filename = filename
}
// No need to reassign filename if it's not changing
files = append(files, itemStyle.Padding(0, 1).Render(filename))
}

View file

@ -2,6 +2,8 @@ package dialog
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
@ -13,7 +15,6 @@ import (
"github.com/opencode-ai/opencode/internal/tui/styles"
"github.com/opencode-ai/opencode/internal/tui/theme"
"github.com/opencode-ai/opencode/internal/tui/util"
"strings"
)
type PermissionAction string
@ -150,7 +151,7 @@ func (p *permissionDialogCmp) selectCurrentOption() tea.Cmd {
func (p *permissionDialogCmp) renderButtons() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
allowStyle := baseStyle
allowSessionStyle := baseStyle
denyStyle := baseStyle
@ -196,7 +197,7 @@ func (p *permissionDialogCmp) renderButtons() string {
func (p *permissionDialogCmp) renderHeader() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
toolKey := baseStyle.Foreground(t.TextMuted()).Bold(true).Render("Tool")
toolValue := baseStyle.
Foreground(t.Text()).
@ -229,9 +230,36 @@ func (p *permissionDialogCmp) renderHeader() string {
case tools.BashToolName:
headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Command"))
case tools.EditToolName:
headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Diff"))
params := p.permission.Params.(tools.EditPermissionsParams)
fileKey := baseStyle.Foreground(t.TextMuted()).Bold(true).Render("File")
filePath := baseStyle.
Foreground(t.Text()).
Width(p.width - lipgloss.Width(fileKey)).
Render(fmt.Sprintf(": %s", params.FilePath))
headerParts = append(headerParts,
lipgloss.JoinHorizontal(
lipgloss.Left,
fileKey,
filePath,
),
baseStyle.Render(strings.Repeat(" ", p.width)),
)
case tools.WriteToolName:
headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("Diff"))
params := p.permission.Params.(tools.WritePermissionsParams)
fileKey := baseStyle.Foreground(t.TextMuted()).Bold(true).Render("File")
filePath := baseStyle.
Foreground(t.Text()).
Width(p.width - lipgloss.Width(fileKey)).
Render(fmt.Sprintf(": %s", params.FilePath))
headerParts = append(headerParts,
lipgloss.JoinHorizontal(
lipgloss.Left,
fileKey,
filePath,
),
baseStyle.Render(strings.Repeat(" ", p.width)),
)
case tools.FetchToolName:
headerParts = append(headerParts, baseStyle.Foreground(t.TextMuted()).Width(p.width).Bold(true).Render("URL"))
}
@ -242,13 +270,13 @@ func (p *permissionDialogCmp) renderHeader() string {
func (p *permissionDialogCmp) renderBashContent() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
if pr, ok := p.permission.Params.(tools.BashPermissionsParams); ok {
content := fmt.Sprintf("```bash\n%s\n```", pr.Command)
// Use the cache for markdown rendering
renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) {
r := styles.GetMarkdownRenderer(p.width-10)
r := styles.GetMarkdownRenderer(p.width - 10)
s, err := r.Render(content)
return styles.ForceReplaceBackgroundWithLipgloss(s, t.Background()), err
})
@ -302,13 +330,13 @@ func (p *permissionDialogCmp) renderWriteContent() string {
func (p *permissionDialogCmp) renderFetchContent() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
if pr, ok := p.permission.Params.(tools.FetchPermissionsParams); ok {
content := fmt.Sprintf("```bash\n%s\n```", pr.URL)
// Use the cache for markdown rendering
renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) {
r := styles.GetMarkdownRenderer(p.width-10)
r := styles.GetMarkdownRenderer(p.width - 10)
s, err := r.Render(content)
return styles.ForceReplaceBackgroundWithLipgloss(s, t.Background()), err
})
@ -325,12 +353,12 @@ func (p *permissionDialogCmp) renderFetchContent() string {
func (p *permissionDialogCmp) renderDefaultContent() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
content := p.permission.Description
// Use the cache for markdown rendering
renderedContent := p.GetOrSetMarkdown(p.permission.ID, func() (string, error) {
r := styles.GetMarkdownRenderer(p.width-10)
r := styles.GetMarkdownRenderer(p.width - 10)
s, err := r.Render(content)
return styles.ForceReplaceBackgroundWithLipgloss(s, t.Background()), err
})
@ -358,7 +386,7 @@ func (p *permissionDialogCmp) styleViewport() string {
func (p *permissionDialogCmp) render() string {
t := theme.CurrentTheme()
baseStyle := styles.BaseStyle()
title := baseStyle.
Bold(true).
Width(p.width - 4).

View file

@ -10,14 +10,17 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/opencode-ai/opencode/internal/app"
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/llm/agent"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/opencode-ai/opencode/internal/permission"
"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/components/core"
"github.com/opencode-ai/opencode/internal/tui/components/dialog"
"github.com/opencode-ai/opencode/internal/tui/layout"
"github.com/opencode-ai/opencode/internal/tui/page"
"github.com/opencode-ai/opencode/internal/tui/theme"
"github.com/opencode-ai/opencode/internal/tui/util"
)
@ -32,6 +35,8 @@ type keyMap struct {
SwitchTheme key.Binding
}
type startCompactSessionMsg struct{}
const (
quitKey = "q"
)
@ -91,13 +96,14 @@ var logsKeyReturnKey = key.NewBinding(
)
type appModel struct {
width, height int
currentPage page.PageID
previousPage page.PageID
pages map[page.PageID]tea.Model
loadedPages map[page.PageID]bool
status core.StatusCmp
app *app.App
width, height int
currentPage page.PageID
previousPage page.PageID
pages map[page.PageID]tea.Model
loadedPages map[page.PageID]bool
status core.StatusCmp
app *app.App
selectedSession session.Session
showPermissions bool
permissions dialog.PermissionDialogCmp
@ -126,9 +132,12 @@ type appModel struct {
showThemeDialog bool
themeDialog dialog.ThemeDialog
showArgumentsDialog bool
argumentsDialog dialog.ArgumentsDialogCmp
isCompacting bool
compactingMessage string
}
func (a appModel) Init() tea.Cmd {
@ -151,6 +160,7 @@ func (a appModel) Init() tea.Cmd {
cmd = a.initDialog.Init()
cmds = append(cmds, cmd)
cmd = a.filepicker.Init()
cmds = append(cmds, cmd)
cmd = a.themeDialog.Init()
cmds = append(cmds, cmd)
@ -203,7 +213,7 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, filepickerCmd)
a.initDialog.SetSize(msg.Width, msg.Height)
if a.showArgumentsDialog {
a.argumentsDialog.SetSize(msg.Width, msg.Height)
args, argsCmd := a.argumentsDialog.Update(msg)
@ -293,6 +303,70 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
a.showCommandDialog = false
return a, nil
case startCompactSessionMsg:
// Start compacting the current session
a.isCompacting = true
a.compactingMessage = "Starting summarization..."
if a.selectedSession.ID == "" {
a.isCompacting = false
return a, util.ReportWarn("No active session to summarize")
}
// Start the summarization process
return a, func() tea.Msg {
ctx := context.Background()
a.app.CoderAgent.Summarize(ctx, a.selectedSession.ID)
return nil
}
case pubsub.Event[agent.AgentEvent]:
payload := msg.Payload
if payload.Error != nil {
a.isCompacting = false
return a, util.ReportError(payload.Error)
}
a.compactingMessage = payload.Progress
if payload.Done && payload.Type == agent.AgentEventTypeSummarize {
a.isCompacting = false
if payload.SessionID != "" {
// Switch to the new session
return a, func() tea.Msg {
sessions, err := a.app.Sessions.List(context.Background())
if err != nil {
return util.InfoMsg{
Type: util.InfoTypeError,
Msg: "Failed to list sessions: " + err.Error(),
}
}
for _, s := range sessions {
if s.ID == payload.SessionID {
return dialog.SessionSelectedMsg{Session: s}
}
}
return util.InfoMsg{
Type: util.InfoTypeError,
Msg: "Failed to find new session",
}
}
}
return a, util.ReportInfo("Session summarization complete")
} else if payload.Done && payload.Type == agent.AgentEventTypeResponse && a.selectedSession.ID != "" {
model := a.app.CoderAgent.Model()
contextWindow := model.ContextWindow
tokens := a.selectedSession.CompletionTokens + a.selectedSession.PromptTokens
if (tokens >= int64(float64(contextWindow)*0.95)) && config.Get().AutoCompact {
return a, util.CmdHandler(startCompactSessionMsg{})
}
}
// Continue listening for events
return a, nil
case dialog.CloseThemeDialogMsg:
a.showThemeDialog = false
return a, nil
@ -342,7 +416,13 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, nil
case chat.SessionSelectedMsg:
a.selectedSession = msg
a.sessionDialog.SetSelectedSession(msg.ID)
case pubsub.Event[session.Session]:
if msg.Type == pubsub.UpdatedEvent && msg.Payload.ID == a.selectedSession.ID {
a.selectedSession = msg.Payload
}
case dialog.SessionSelectedMsg:
a.showSessionDialog = false
if a.currentPage == page.ChatPage {
@ -357,22 +437,22 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return a, msg.Command.Handler(msg.Command)
}
return a, util.ReportInfo("Command selected: " + msg.Command.Title)
case dialog.ShowArgumentsDialogMsg:
// Show arguments dialog
a.argumentsDialog = dialog.NewArgumentsDialogCmp(msg.CommandID, msg.Content)
a.showArgumentsDialog = true
return a, a.argumentsDialog.Init()
case dialog.CloseArgumentsDialogMsg:
// Close arguments dialog
a.showArgumentsDialog = false
// If submitted, replace $ARGUMENTS and run the command
if msg.Submit {
// Replace $ARGUMENTS with the provided arguments
content := strings.ReplaceAll(msg.Content, "$ARGUMENTS", msg.Arguments)
// Execute the command with arguments
return a, util.CmdHandler(dialog.CommandRunCustomMsg{
Content: content,
@ -387,7 +467,7 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
a.argumentsDialog = args.(dialog.ArgumentsDialogCmp)
return a, cmd
}
switch {
case key.Matches(msg, keys.Quit):
@ -606,6 +686,15 @@ func (a *appModel) RegisterCommand(cmd dialog.Command) {
a.commands = append(a.commands, cmd)
}
func (a *appModel) findCommand(id string) (dialog.Command, bool) {
for _, cmd := range a.commands {
if cmd.ID == id {
return cmd, true
}
}
return dialog.Command{}, false
}
func (a *appModel) moveToPage(pageID page.PageID) tea.Cmd {
if a.app.CoderAgent.IsBusy() {
// For now we don't move to any page if the agent is busy
@ -668,10 +757,29 @@ func (a appModel) View() string {
}
if !a.app.CoderAgent.IsBusy() {
a.status.SetHelpWidgetMsg("ctrl+? help")
} else {
a.status.SetHelpWidgetMsg("? help")
// Show compacting status overlay
if a.isCompacting {
t := theme.CurrentTheme()
style := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(t.BorderFocused()).
BorderBackground(t.Background()).
Padding(1, 2).
Background(t.Background()).
Foreground(t.Text())
overlay := style.Render("Summarizing\n" + a.compactingMessage)
row := lipgloss.Height(appView) / 2
row -= lipgloss.Height(overlay) / 2
col := lipgloss.Width(appView) / 2
col -= lipgloss.Width(overlay) / 2
appView = layout.PlaceOverlay(
col,
row,
overlay,
appView,
true,
)
}
if a.showHelp {
@ -789,7 +897,7 @@ func (a appModel) View() string {
true,
)
}
if a.showArgumentsDialog {
overlay := a.argumentsDialog.View()
row := lipgloss.Height(appView) / 2
@ -850,7 +958,17 @@ If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (
)
},
})
model.RegisterCommand(dialog.Command{
ID: "compact",
Title: "Compact Session",
Description: "Summarize the current session and create a new one with the summary",
Handler: func(cmd dialog.Command) tea.Cmd {
return func() tea.Msg {
return startCompactSessionMsg{}
}
},
})
// Load custom commands
customCommands, err := dialog.LoadCustomCommands()
if err != nil {
@ -860,6 +978,6 @@ If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (
model.RegisterCommand(cmd)
}
}
return model
}