@@ -2372,3 +2372,86 @@ export function shouldCollapseUserMessage(text: string): boolean {
23722372 visible . split ( "\n" ) . length > MAX_COLLAPSED_USER_MESSAGE_LINES
23732373 ) ;
23742374}
2375+
2376+ const isWorkLogTimelineRow = ( row : MessagesTimelineRow ) =>
2377+ row . kind === "work" ||
2378+ row . kind === "work-live" ||
2379+ row . kind === "work-toggle" ||
2380+ row . kind === "thinking" ;
2381+
2382+ /**
2383+ * Display-only pass over derived rows for the `hideReasoning` and
2384+ * `compactWorkLog` client settings. Runs after derivation so the incremental
2385+ * projection state stays untouched.
2386+ */
2387+ export function applyTimelineDensity (
2388+ rows : MessagesTimelineRow [ ] ,
2389+ options : { compactWorkLog : boolean ; hideReasoning : boolean } ,
2390+ ) : MessagesTimelineRow [ ] {
2391+ if ( ! options . compactWorkLog && ! options . hideReasoning ) return rows ;
2392+ let result = rows ;
2393+
2394+ if ( options . hideReasoning ) {
2395+ // Commentary = an assistant message without meta that is followed, within
2396+ // the same turn, by more work or another assistant message. The trailing
2397+ // message (final answer, or the commentary still streaming) stays visible.
2398+ const hiddenCommentaryIds = new Set < string > ( ) ;
2399+ let laterActivityInTurn = false ;
2400+ for ( let index = result . length - 1 ; index >= 0 ; index -= 1 ) {
2401+ const row = result [ index ] ! ;
2402+ if ( row . kind === "message" && row . message . role === "user" ) {
2403+ laterActivityInTurn = false ;
2404+ continue ;
2405+ }
2406+ if ( row . kind === "message" && row . message . role === "assistant" ) {
2407+ if ( ! row . showAssistantMeta && laterActivityInTurn ) hiddenCommentaryIds . add ( row . id ) ;
2408+ laterActivityInTurn = true ;
2409+ } else if ( isWorkLogTimelineRow ( row ) ) {
2410+ laterActivityInTurn = true ;
2411+ }
2412+ }
2413+ result = result . flatMap ( ( row ) : MessagesTimelineRow [ ] => {
2414+ if ( hiddenCommentaryIds . has ( row . id ) ) return [ ] ;
2415+ if ( row . kind === "work" ) {
2416+ const groupedEntries = row . groupedEntries . filter ( ( entry ) => entry . itemType !== "reasoning" ) ;
2417+ if ( groupedEntries . length === 0 ) return [ ] ;
2418+ return groupedEntries . length === row . groupedEntries . length
2419+ ? [ row ]
2420+ : [ { ...row , groupedEntries } ] ;
2421+ }
2422+ if ( row . kind === "work-live" ) {
2423+ if ( row . entry . itemType === "reasoning" ) {
2424+ return row . active ? [ { kind : "thinking" , id : row . id , createdAt : row . createdAt } ] : [ ] ;
2425+ }
2426+ if ( row . thought === undefined ) return [ row ] ;
2427+ const { thought : _thought , ...rest } = row ;
2428+ return [ rest ] ;
2429+ }
2430+ return [ row ] ;
2431+ } ) ;
2432+ }
2433+
2434+ if ( options . compactWorkLog ) {
2435+ // Each run of consecutive work-log rows collapses to its last row, and a
2436+ // work group to its last entry, so the line updates instead of growing.
2437+ result = result . flatMap ( ( row , index ) : MessagesTimelineRow [ ] => {
2438+ if ( ! isWorkLogTimelineRow ( row ) ) return [ row ] ;
2439+ const next = result [ index + 1 ] ;
2440+ if ( next !== undefined && isWorkLogTimelineRow ( next ) ) return [ ] ;
2441+ const compactRow : MessagesTimelineRow = { ...row , continuesWorkLog : false } ;
2442+ if ( compactRow . kind === "work" ) {
2443+ return [
2444+ {
2445+ ...compactRow ,
2446+ groupedEntries : compactRow . groupedEntries . slice ( - 1 ) ,
2447+ isExpandedToolGroup : false ,
2448+ } ,
2449+ ] ;
2450+ }
2451+ if ( compactRow . kind === "work-live" ) return [ { ...compactRow , expanded : false } ] ;
2452+ return [ compactRow ] ;
2453+ } ) ;
2454+ }
2455+
2456+ return result ;
2457+ }
0 commit comments