Skip to content

Commit b3885d1

Browse files
authored
feat(tui): retain cache when cycling between subagent/parent sessions for perf (sst#1981)
1 parent ca3769b commit b3885d1

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/tui/internal/components/chat/messages.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
171171
m.showThinkingBlocks = !m.showThinkingBlocks
172172
m.app.State.ShowThinkingBlocks = &m.showThinkingBlocks
173173
return m, tea.Batch(m.renderView(), m.app.SaveState())
174-
case app.SessionLoadedMsg, app.SessionClearedMsg:
174+
case app.SessionLoadedMsg:
175+
m.tail = true
176+
m.loading = true
177+
return m, m.renderView()
178+
case app.SessionClearedMsg:
175179
m.cache.Clear()
176180
m.tail = true
177181
m.loading = true
@@ -183,6 +187,21 @@ func (m *messagesComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
183187
return m, m.renderView()
184188
}
185189
case app.SessionSelectedMsg:
190+
currentParent := m.app.Session.ParentID
191+
if currentParent == "" {
192+
currentParent = m.app.Session.ID
193+
}
194+
195+
targetParent := msg.ParentID
196+
if targetParent == "" {
197+
targetParent = msg.ID
198+
}
199+
200+
// Clear cache only if switching between different session families
201+
if currentParent != targetParent {
202+
m.cache.Clear()
203+
}
204+
186205
m.viewport.GotoBottom()
187206
case app.MessageRevertedMsg:
188207
if msg.Session.ID == m.app.Session.ID {

packages/tui/internal/tui/tui.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,19 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
621621
},
622622
}
623623
case app.SessionSelectedMsg:
624+
updated, cmd := a.messages.Update(msg)
625+
a.messages = updated.(chat.MessagesComponent)
626+
cmds = append(cmds, cmd)
627+
624628
messages, err := a.app.ListMessages(context.Background(), msg.ID)
625629
if err != nil {
626630
slog.Error("Failed to list messages", "error", err.Error())
627631
return a, toast.NewErrorToast("Failed to open session")
628632
}
629633
a.app.Session = msg
630634
a.app.Messages = messages
631-
return a, util.CmdHandler(app.SessionLoadedMsg{})
635+
cmds = append(cmds, util.CmdHandler(app.SessionLoadedMsg{}))
636+
return a, tea.Batch(cmds...)
632637
case app.SessionCreatedMsg:
633638
a.app.Session = msg.Session
634639
case dialog.ScrollToMessageMsg:

0 commit comments

Comments
 (0)