1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
package dialog
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/lipgloss/v2"
"github.com/muesli/reflow/truncate"
"github.com/sst/opencode-sdk-go"
"github.com/sst/opencode/internal/app"
"github.com/sst/opencode/internal/components/list"
"github.com/sst/opencode/internal/components/modal"
"github.com/sst/opencode/internal/layout"
"github.com/sst/opencode/internal/styles"
"github.com/sst/opencode/internal/theme"
"github.com/sst/opencode/internal/util"
)
// TimelineDialog interface for the session timeline dialog
type TimelineDialog interface {
layout.Modal
}
// ScrollToMessageMsg is sent when a message should be scrolled to
type ScrollToMessageMsg struct {
MessageID string
}
// RestoreToMessageMsg is sent when conversation should be restored to a specific message
type RestoreToMessageMsg struct {
MessageID string
Index int
}
// timelineItem represents a user message in the timeline list
type timelineItem struct {
messageID string
content string
timestamp time.Time
index int // Index in the full message list
toolCount int // Number of tools used in this message
}
func (n timelineItem) Render(
selected bool,
width int,
isFirstInViewport bool,
baseStyle styles.Style,
isCurrent bool,
) string {
t := theme.CurrentTheme()
infoStyle := baseStyle.Background(t.BackgroundPanel()).Foreground(t.Info()).Render
textStyle := baseStyle.Background(t.BackgroundPanel()).Foreground(t.Text()).Render
// Add dot after timestamp if this is the current message - only apply color when not selected
var dot string
var dotVisualLen int
if isCurrent {
if selected {
dot = "● "
} else {
dot = lipgloss.NewStyle().Foreground(t.Success()).Render("● ")
}
dotVisualLen = 2 // "● " is 2 characters wide
}
// Format timestamp - only apply color when not selected
var timeStr string
var timeVisualLen int
if selected {
timeStr = n.timestamp.Format("15:04") + " " + dot
timeVisualLen = lipgloss.Width(n.timestamp.Format("15:04")+" ") + dotVisualLen
} else {
timeStr = infoStyle(n.timestamp.Format("15:04")+" ") + dot
timeVisualLen = lipgloss.Width(n.timestamp.Format("15:04")+" ") + dotVisualLen
}
// Tool count display (fixed width for alignment) - only apply color when not selected
toolInfo := ""
toolInfoVisualLen := 0
if n.toolCount > 0 {
toolInfoText := fmt.Sprintf("(%d tools)", n.toolCount)
if selected {
toolInfo = toolInfoText
} else {
toolInfo = infoStyle(toolInfoText)
}
toolInfoVisualLen = lipgloss.Width(toolInfo)
}
// Calculate available space for content
// Reserve space for: timestamp + dot + space + toolInfo + padding + some buffer
reservedSpace := timeVisualLen + 1 + toolInfoVisualLen + 4
contentWidth := max(width-reservedSpace, 8)
truncatedContent := truncate.StringWithTail(
strings.Split(n.content, "\n")[0],
uint(contentWidth),
"...",
)
// Apply normal text color to content for non-selected items
var styledContent string
if selected {
styledContent = truncatedContent
} else {
styledContent = textStyle(truncatedContent)
}
// Create the line with proper spacing - content left-aligned, tools right-aligned
var text string
text = timeStr + styledContent
if toolInfo != "" {
bgColor := t.BackgroundPanel()
if selected {
bgColor = t.Primary()
}
text = layout.Render(
layout.FlexOptions{
Background: &bgColor,
Direction: layout.Row,
Justify: layout.JustifySpaceBetween,
Align: layout.AlignStretch,
Width: width - 2,
},
layout.FlexItem{
View: text,
},
layout.FlexItem{
View: toolInfo,
},
)
}
var itemStyle styles.Style
if selected {
itemStyle = baseStyle.
Background(t.Primary()).
Foreground(t.BackgroundElement()).
Width(width).
PaddingLeft(1)
} else {
itemStyle = baseStyle.PaddingLeft(1)
}
return itemStyle.Render(text)
}
func (n timelineItem) Selectable() bool {
return true
}
type timelineDialog struct {
width int
height int
modal *modal.Modal
list list.List[timelineItem]
app *app.App
}
func (n *timelineDialog) Init() tea.Cmd {
return nil
}
func (n *timelineDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
n.width = msg.Width
n.height = msg.Height
n.list.SetMaxWidth(layout.Current.Container.Width - 12)
case tea.KeyPressMsg:
switch msg.String() {
case "up", "down":
// Handle navigation and immediately scroll to selected message
var cmd tea.Cmd
listModel, cmd := n.list.Update(msg)
n.list = listModel.(list.List[timelineItem])
// Get the newly selected item and scroll to it immediately
if item, idx := n.list.GetSelectedItem(); idx >= 0 {
return n, tea.Sequence(
cmd,
util.CmdHandler(ScrollToMessageMsg{MessageID: item.messageID}),
)
}
return n, cmd
case "r":
// Restore conversation to selected message
if item, idx := n.list.GetSelectedItem(); idx >= 0 {
return n, tea.Sequence(
util.CmdHandler(RestoreToMessageMsg{MessageID: item.messageID, Index: item.index}),
util.CmdHandler(modal.CloseModalMsg{}),
)
}
case "enter":
// Keep Enter functionality for closing the modal
if _, idx := n.list.GetSelectedItem(); idx >= 0 {
return n, util.CmdHandler(modal.CloseModalMsg{})
}
}
}
var cmd tea.Cmd
listModel, cmd := n.list.Update(msg)
n.list = listModel.(list.List[timelineItem])
return n, cmd
}
func (n *timelineDialog) Render(background string) string {
listView := n.list.View()
t := theme.CurrentTheme()
keyStyle := styles.NewStyle().
Foreground(t.Text()).
Background(t.BackgroundPanel()).
Bold(true).
Render
mutedStyle := styles.NewStyle().Foreground(t.TextMuted()).Background(t.BackgroundPanel()).Render
helpText := keyStyle(
"↑/↓",
) + mutedStyle(
" jump ",
) + keyStyle(
"r",
) + mutedStyle(
" restore",
)
bgColor := t.BackgroundPanel()
helpView := styles.NewStyle().
Background(bgColor).
Width(layout.Current.Container.Width - 14).
PaddingLeft(1).
PaddingTop(1).
Render(helpText)
content := strings.Join([]string{listView, helpView}, "\n")
return n.modal.Render(content, background)
}
func (n *timelineDialog) Close() tea.Cmd {
return nil
}
// extractMessagePreview extracts a preview from message parts
func extractMessagePreview(parts []opencode.PartUnion) string {
for _, part := range parts {
switch casted := part.(type) {
case opencode.TextPart:
text := strings.TrimSpace(casted.Text)
if text != "" {
return text
}
}
}
return "No text content"
}
// countToolsInResponse counts tools in the assistant's response to a user message
func countToolsInResponse(messages []app.Message, userMessageIndex int) int {
count := 0
// Look at subsequent messages to find the assistant's response
for i := userMessageIndex + 1; i < len(messages); i++ {
message := messages[i]
// If we hit another user message, stop looking
if _, isUser := message.Info.(opencode.UserMessage); isUser {
break
}
// Count tools in this assistant message
for _, part := range message.Parts {
switch part.(type) {
case opencode.ToolPart:
count++
}
}
}
return count
}
// NewTimelineDialog creates a new session timeline dialog
func NewTimelineDialog(app *app.App) TimelineDialog { // renamed from NewNavigationDialog
var items []timelineItem
// Filter to only user messages and extract relevant info
for i, message := range app.Messages {
if userMsg, ok := message.Info.(opencode.UserMessage); ok {
preview := extractMessagePreview(message.Parts)
toolCount := countToolsInResponse(app.Messages, i)
items = append(items, timelineItem{
messageID: userMsg.ID,
content: preview,
timestamp: time.UnixMilli(int64(userMsg.Time.Created)),
index: i,
toolCount: toolCount,
})
}
}
listComponent := list.NewListComponent(
list.WithItems(items),
list.WithMaxVisibleHeight[timelineItem](12),
list.WithFallbackMessage[timelineItem]("No user messages in this session"),
list.WithAlphaNumericKeys[timelineItem](true),
list.WithRenderFunc(
func(item timelineItem, selected bool, width int, baseStyle styles.Style) string {
// Determine if this item is the current message for the session
isCurrent := false
if app.Session.Revert.MessageID != "" {
// When reverted, Session.Revert.MessageID contains the NEXT user message ID
// So we need to find the previous user message to highlight the correct one
for i, navItem := range items {
if navItem.messageID == app.Session.Revert.MessageID && i > 0 {
// Found the next message, so the previous one is current
isCurrent = item.messageID == items[i-1].messageID
break
}
}
} else if len(app.Messages) > 0 {
// If not reverted, highlight the last user message
lastUserMsgID := ""
for i := len(app.Messages) - 1; i >= 0; i-- {
if userMsg, ok := app.Messages[i].Info.(opencode.UserMessage); ok {
lastUserMsgID = userMsg.ID
break
}
}
isCurrent = item.messageID == lastUserMsgID
}
// Only show the dot if undo/redo/restore is available
showDot := app.Session.Revert.MessageID != ""
return item.Render(selected, width, false, baseStyle, isCurrent && showDot)
},
),
list.WithSelectableFunc(func(item timelineItem) bool {
return true
}),
)
listComponent.SetMaxWidth(layout.Current.Container.Width - 12)
return &timelineDialog{
list: listComponent,
app: app,
modal: modal.New(
modal.WithTitle("Session Timeline"),
modal.WithMaxWidth(layout.Current.Container.Width-8),
),
}
}
|