Skip to content

Commit 3fb89d8

Browse files
committed
Make /compact non-blocking with spinner feedback
Run session compaction asynchronously in a background goroutine instead of blocking the TUI render thread. Show the working spinner and pending response indicator while the LLM generates the summary, and clear them when the compaction completes. The compaction context is cancellable via Esc, matching the behavior of regular agent streams. Fixes #1678 Assisted-By: cagent
1 parent 1a8e453 commit 3fb89d8

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎pkg/app/app.go‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,21 @@ func (a *App) ShouldExitAfterFirstResponse() bool {
717717
return a.exitAfterFirstResponse
718718
}
719719

720-
func (a *App) CompactSession(additionalPrompt string) {
721-
if a.session != nil {
720+
func (a *App) CompactSession(ctx context.Context, additionalPrompt string) {
721+
if a.session == nil {
722+
return
723+
}
724+
725+
go func() {
722726
events := make(chan runtime.Event, 100)
723-
a.runtime.Summarize(context.Background(), a.session, additionalPrompt, events)
724-
close(events)
727+
go func() {
728+
defer close(events)
729+
a.runtime.Summarize(ctx, a.session, additionalPrompt, events)
730+
}()
725731
for event := range events {
726-
a.events <- event
732+
a.sendEvent(ctx, event)
727733
}
728-
}
734+
}()
729735
}
730736

731737
func (a *App) PlainTextTranscript() string {

‎pkg/tui/page/chat/chat.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,11 +1155,18 @@ func (p *chatPage) processMessage(msg msgtypes.SendMsg) tea.Cmd {
11551155
// CompactSession generates a summary and compacts the session history
11561156
func (p *chatPage) CompactSession(additionalPrompt string) tea.Cmd {
11571157
// Cancel any active stream without showing cancellation message
1158-
p.cancelStream(false)
1158+
cancelCmd := p.cancelStream(false)
11591159

1160-
p.app.CompactSession(additionalPrompt)
1160+
var ctx context.Context
1161+
ctx, p.msgCancel = context.WithCancel(context.Background())
1162+
p.app.CompactSession(ctx, additionalPrompt)
11611163

1162-
return p.messages.ScrollToBottom()
1164+
return tea.Batch(
1165+
cancelCmd,
1166+
p.setWorking(true),
1167+
p.setPendingResponse(true),
1168+
p.messages.ScrollToBottom(),
1169+
)
11631170
}
11641171

11651172
func (p *chatPage) Cleanup() {

‎pkg/tui/page/chat/runtime_events.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ func (p *chatPage) handleRuntimeEvent(msg tea.Msg) (bool, tea.Cmd) {
124124

125125
case *runtime.SessionCompactionEvent:
126126
if msg.Status == "completed" {
127-
return true, notification.SuccessCmd("Session compacted successfully.")
127+
return true, tea.Batch(
128+
p.setWorking(false),
129+
p.setPendingResponse(false),
130+
notification.SuccessCmd("Session compacted successfully."),
131+
p.messages.ScrollToBottom(),
132+
)
128133
}
129134
return true, nil
130135

0 commit comments

Comments
 (0)