Skip to content

Commit 3a1f9e6

Browse files
committed
Show message count in session browser dialog
Add NumMessages field to session.Summary and populate it from both SQLite (via LEFT JOIN on session_items) and InMemory stores. Display the count in the session browser as '(N)' next to each session title. Assisted-By: cagent
1 parent 2cceb97 commit 3a1f9e6

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

pkg/session/session.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ func (s *Session) IsSubSession() bool {
418418
return s.ParentID != ""
419419
}
420420

421+
// MessageCount returns the number of items that contain a message.
422+
func (s *Session) MessageCount() int {
423+
n := 0
424+
for _, item := range s.Messages {
425+
if item.IsMessage() {
426+
n++
427+
}
428+
}
429+
return n
430+
}
421431
// New creates a new agent session
422432
func New(opts ...Opt) *Session {
423433
sessionID := uuid.New().String()

pkg/session/store.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type Summary struct {
7171
CreatedAt time.Time
7272
Starred bool
7373
BranchParentSessionID string
74+
NumMessages int
7475
}
7576

7677
// Store defines the interface for session storage
@@ -164,6 +165,7 @@ func (s *InMemorySessionStore) GetSessionSummaries(_ context.Context) ([]Summary
164165
CreatedAt: value.CreatedAt,
165166
Starred: value.Starred,
166167
BranchParentSessionID: value.BranchParentSessionID,
168+
NumMessages: value.MessageCount(),
167169
})
168170
return true
169171
})
@@ -878,7 +880,11 @@ func (s *SQLiteSessionStore) GetSessions(ctx context.Context) ([]*Session, error
878880
// This is much faster than GetSessions as it doesn't load message content.
879881
func (s *SQLiteSessionStore) GetSessionSummaries(ctx context.Context) ([]Summary, error) {
880882
rows, err := s.db.QueryContext(ctx,
881-
"SELECT id, title, created_at, starred, branch_parent_session_id FROM sessions WHERE parent_id IS NULL OR parent_id = '' ORDER BY created_at DESC")
883+
`SELECT s.id, s.title, s.created_at, s.starred, s.branch_parent_session_id,
884+
(SELECT COUNT(*) FROM session_items si WHERE si.session_id = s.id AND si.item_type = 'message')
885+
FROM sessions s
886+
WHERE s.parent_id IS NULL OR s.parent_id = ''
887+
ORDER BY s.created_at DESC`)
882888
if err != nil {
883889
return nil, err
884890
}
@@ -888,7 +894,8 @@ func (s *SQLiteSessionStore) GetSessionSummaries(ctx context.Context) ([]Summary
888894
for rows.Next() {
889895
var id, title, createdAtStr, starredStr string
890896
var branchParentID sql.NullString
891-
if err := rows.Scan(&id, &title, &createdAtStr, &starredStr, &branchParentID); err != nil {
897+
var numMessages int
898+
if err := rows.Scan(&id, &title, &createdAtStr, &starredStr, &branchParentID, &numMessages); err != nil {
892899
return nil, err
893900
}
894901
createdAt, err := time.Parse(time.RFC3339, createdAtStr)
@@ -905,6 +912,7 @@ func (s *SQLiteSessionStore) GetSessionSummaries(ctx context.Context) ([]Summary
905912
CreatedAt: createdAt,
906913
Starred: starred,
907914
BranchParentSessionID: branchParentID.String,
915+
NumMessages: numMessages,
908916
})
909917
}
910918

pkg/session/store_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,12 @@ func TestGetSessionSummaries(t *testing.T) {
215215
assert.Equal(t, "session-2", summaries[0].ID)
216216
assert.Equal(t, "Second Session", summaries[0].Title)
217217
assert.Equal(t, session2Time, summaries[0].CreatedAt)
218+
assert.Equal(t, 1, summaries[0].NumMessages)
218219

219220
assert.Equal(t, "session-1", summaries[1].ID)
220221
assert.Equal(t, "First Session", summaries[1].Title)
221222
assert.Equal(t, session1Time, summaries[1].CreatedAt)
223+
assert.Equal(t, 1, summaries[1].NumMessages)
222224
}
223225

224226
func TestBranchSessionCopiesPrefix(t *testing.T) {

pkg/tui/dialog/session_browser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,15 @@ func (d *sessionBrowserDialog) renderSession(sess session.Summary, selected bool
317317
title = "Untitled"
318318
}
319319

320+
suffix := fmt.Sprintf(" (%d) • %s", sess.NumMessages, d.timeAgo(sess.CreatedAt))
321+
320322
starWidth := 3
321-
maxTitleLen := maxWidth - 25 - starWidth
323+
maxTitleLen := maxWidth - len(suffix) - starWidth
322324
if len(title) > maxTitleLen {
323325
title = title[:maxTitleLen-1] + "…"
324326
}
325327

326-
return styles.StarIndicator(sess.Starred) + titleStyle.Render(title) + timeStyle.Render(" • "+d.timeAgo(sess.CreatedAt))
328+
return styles.StarIndicator(sess.Starred) + titleStyle.Render(title) + timeStyle.Render(suffix)
327329
}
328330

329331
func (d *sessionBrowserDialog) timeAgo(t time.Time) string {

0 commit comments

Comments
 (0)