Skip to content

Commit fecd9ee

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 a1ae6ce commit fecd9ee

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎pkg/app/app.go‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,25 @@ 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+
sess := a.session
722+
if sess == nil {
723+
return
724+
}
725+
726+
go func() {
722727
events := make(chan runtime.Event, 100)
723-
a.runtime.Summarize(context.Background(), a.session, additionalPrompt, events)
724-
close(events)
728+
go func() {
729+
defer close(events)
730+
a.runtime.Summarize(ctx, sess, additionalPrompt, events)
731+
}()
725732
for event := range events {
726-
a.events <- event
733+
if ctx.Err() != nil {
734+
return
735+
}
736+
a.sendEvent(ctx, event)
727737
}
728-
}
738+
}()
729739
}
730740

731741
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)