@@ -8,6 +8,7 @@ import type { Message, Part, Session, SessionStatus } from "@opencode-ai/sdk/v2"
88import { oc } from "./api"
99import { createServerEvents , type ServerEvents } from "./event-source"
1010
11+ export const TAIL = 80
1112const SKIP_PARTS = new Set ( [ "patch" , "step-start" , "step-finish" ] )
1213// Keep diff statistics without retaining patch bodies unused by this transcript.
1314const visibleMessage = ( info : Message ) : Message => {
@@ -81,6 +82,9 @@ export function createLiveSession(
8182 const [ clearedThrough , setClearedThrough ] = createSignal ( "" )
8283 const [ connection , setConnection ] = createStore ( { stream : "" , snapshot : "" , event : "" } )
8384 const [ loading , setLoading ] = createSignal ( false )
85+ const [ loadingOlder , setLoadingOlder ] = createSignal ( false )
86+ const [ olderCursor , setOlderCursor ] = createSignal < string | undefined > ( )
87+ const hasOlder = ( ) => olderCursor ( ) !== undefined
8488 const latest = ( ) => data . message [ sessionID ] ?. at ( - 1 )
8589 const error = ( ) => {
8690 const message = latest ( )
@@ -136,28 +140,43 @@ export function createLiveSession(
136140 }
137141 if ( disposed ) return
138142 snapshotStarted = true
139- const [ session , messages , status ] = await Promise . all ( [
143+ const [ session , page , status ] = await Promise . all ( [
140144 oc . session ( sessionID , directory , controller . signal ) ,
141- oc . messages ( sessionID , directory , controller . signal ) ,
145+ oc . messagesPage ( sessionID , directory , TAIL , undefined , controller . signal ) ,
142146 oc . status ( directory , controller . signal ) ,
143147 ] )
144148 if ( disposed ) return
145149 const events = duringLoad !
146150 duringLoad = undefined
147- messages . sort ( ( a , b ) => a . info . id . localeCompare ( b . info . id ) )
151+ const messages = page . items . sort ( ( a , b ) => a . info . id . localeCompare ( b . info . id ) )
148152 batch ( ( ) => {
149153 const previous = latest ( )
150154 if ( previous && ( messages . findLast ( ( m ) => m . info . role === "user" ) ?. info . id ?? "" ) > previous . id ) clearFailure ( )
155+ const existing = data . message [ sessionID ] ?? [ ]
156+ const first = messages [ 0 ] ?. info . id
157+ // A fresh tail supersedes its overlapping window; already-loaded older
158+ // messages remain available across reconnects and explicit refreshes.
159+ const retained = olderCursor ( ) || page . cursor
160+ ? existing . filter ( ( message ) => ! first || message . id < first )
161+ : [ ]
162+ const merged = [ ...retained , ...messages . map ( ( message ) => visibleMessage ( message . info ) ) ]
163+ const cursor = ! page . cursor && first && ( ! existing . length || first <= existing [ 0 ] ! . id )
164+ ? undefined
165+ : olderCursor ( ) ?? ( existing . length ? undefined : page . cursor )
151166 // Reconnect snapshots must not remount the unchanged transcript.
152167 setData ( "session" , reconcile ( [ session ] ) )
153168 setData ( "session_status" , reconcile ( { ...status , [ sessionID ] : status [ sessionID ] ?? { type : "idle" } } ) )
154- setData ( "message" , sessionID , reconcile ( messages . map ( ( m ) => visibleMessage ( m . info ) ) ) )
169+ setData ( "message" , sessionID , reconcile ( merged ) )
155170 setData (
156171 "part" ,
157172 reconcile (
158- Object . fromEntries ( messages . map ( ( m ) => [ m . info . id , m . parts . filter ( ( p ) => ! SKIP_PARTS . has ( p . type ) ) ] ) ) ,
173+ Object . fromEntries ( [
174+ ...retained . map ( ( message ) => [ message . id , data . part [ message . id ] ?? [ ] ] as const ) ,
175+ ...messages . map ( ( message ) => [ message . info . id , message . parts . filter ( ( part ) => ! SKIP_PARTS . has ( part . type ) ) ] as const ) ,
176+ ] ) ,
159177 ) ,
160178 )
179+ setOlderCursor ( cursor )
161180 setData ( "part_text_accum_delta" , reconcile ( { } ) )
162181 const bases = new Set < string > ( )
163182 uncertainParts . clear ( )
@@ -214,6 +233,30 @@ export function createLiveSession(
214233 return activeLoad
215234 }
216235
236+ const older = async ( ) : Promise < void > => {
237+ if ( disposed || loadingOlder ( ) || ! hasOlder ( ) ) return
238+ setLoadingOlder ( true )
239+ try {
240+ if ( activeLoad ) await activeLoad
241+ const before = olderCursor ( )
242+ if ( disposed || ! before ) return
243+ const page = await oc . messagesPage ( sessionID , directory , TAIL , before , controller . signal )
244+ if ( disposed ) return
245+ batch ( ( ) => {
246+ const existing = data . message [ sessionID ] ?? [ ]
247+ const byID = new Map ( existing . map ( ( message ) => [ message . id , message ] ) )
248+ page . items . forEach ( ( message ) => byID . set ( message . info . id , visibleMessage ( message . info ) ) )
249+ setData ( "message" , sessionID , reconcile ( [ ...byID . values ( ) ] . sort ( ( a , b ) => a . id . localeCompare ( b . id ) ) ) )
250+ page . items . forEach ( ( message ) =>
251+ setData ( "part" , message . info . id , reconcile ( message . parts . filter ( ( part ) => ! SKIP_PARTS . has ( part . type ) ) ) ) ,
252+ )
253+ setOlderCursor ( page . cursor )
254+ } )
255+ } finally {
256+ setLoadingOlder ( false )
257+ }
258+ }
259+
217260 const refresh = ( ) => {
218261 if ( activeLoad || duringLoad ) {
219262 refreshAfterLoad = true
@@ -241,6 +284,7 @@ export function createLiveSession(
241284 case "message.updated" : {
242285 const info : Message = props . info
243286 if ( info . sessionID !== sessionID ) break
287+ if ( hasOlder ( ) && data . message [ sessionID ] ?. [ 0 ] && info . id < data . message [ sessionID ] ! [ 0 ] ! . id ) break
244288 setData (
245289 produce ( ( draft ) => {
246290 draft . message [ sessionID ] ??= [ ]
@@ -265,7 +309,7 @@ export function createLiveSession(
265309 }
266310 case "message.part.updated" : {
267311 const part : Part = props . part
268- if ( part . sessionID !== sessionID || SKIP_PARTS . has ( part . type ) ) break
312+ if ( part . sessionID !== sessionID || SKIP_PARTS . has ( part . type ) || ! data . message [ sessionID ] ?. some ( ( message ) => message . id === part . messageID ) ) break
269313 setData (
270314 produce ( ( draft ) => {
271315 delete draft . part_text_accum_delta [ part . id ]
@@ -383,5 +427,5 @@ export function createLiveSession(
383427 source ?. close ( )
384428 } )
385429
386- return { data, load, apply, error, connectionError, loading, retry }
430+ return { data, load, older , hasOlder , loadingOlder , apply, error, connectionError, loading, retry }
387431}
0 commit comments