From 1e07384364d6ec42239f8fdebcffc66f340c3761 Mon Sep 17 00:00:00 2001 From: Timo Clasen Date: Tue, 8 Jul 2025 22:37:25 +0200 Subject: fix: make compact command interruptible (#691) Co-authored-by: GitHub Action --- packages/tui/internal/app/app.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'packages/tui') diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.go index b7911b56e..83934343e 100644 --- a/packages/tui/internal/app/app.go +++ b/packages/tui/internal/app/app.go @@ -35,6 +35,7 @@ type App struct { Commands commands.CommandRegistry InitialModel *string InitialPrompt *string + compactCancel context.CancelFunc } type SessionSelectedMsg = *opencode.Session @@ -306,13 +307,26 @@ func (a *App) InitializeProject(ctx context.Context) tea.Cmd { } func (a *App) CompactSession(ctx context.Context) tea.Cmd { + if a.compactCancel != nil { + a.compactCancel() + } + + compactCtx, cancel := context.WithCancel(ctx) + a.compactCancel = cancel + go func() { - _, err := a.Client.Session.Summarize(ctx, a.Session.ID, opencode.SessionSummarizeParams{ + defer func() { + a.compactCancel = nil + }() + + _, err := a.Client.Session.Summarize(compactCtx, a.Session.ID, opencode.SessionSummarizeParams{ ProviderID: opencode.F(a.Provider.ID), ModelID: opencode.F(a.Model.ID), }) if err != nil { - slog.Error("Failed to compact session", "error", err) + if compactCtx.Err() != context.Canceled { + slog.Error("Failed to compact session", "error", err) + } } }() return nil @@ -415,6 +429,12 @@ func (a *App) SendChatMessage( } func (a *App) Cancel(ctx context.Context, sessionID string) error { + // Cancel any running compact operation + if a.compactCancel != nil { + a.compactCancel() + a.compactCancel = nil + } + _, err := a.Client.Session.Abort(ctx, sessionID) if err != nil { slog.Error("Failed to cancel session", "error", err) -- cgit v1.2.3