@@ -22,6 +22,8 @@ export type ChatProps = {
2222 disabledReason : string | null ;
2323 error : string | null ;
2424 sessions : SessionsListResult | null ;
25+ isToolOutputExpanded : ( id : string ) => boolean ;
26+ onToolOutputToggle : ( id : string , expanded : boolean ) => void ;
2527 onRefresh : ( ) => void ;
2628 onDraftChange : ( next : string ) => void ;
2729 onSend : ( ) => void ;
@@ -88,10 +90,11 @@ export function renderChat(props: ChatProps) {
8890 content : [ { type : "text" , text : item . text } ] ,
8991 timestamp : item . startedAt ,
9092 } ,
93+ props ,
9194 { streaming : true } ,
9295 ) ;
9396 }
94- return renderMessage ( item . message ) ;
97+ return renderMessage ( item . message , props ) ;
9598 } ) }
9699 </ div >
97100
@@ -131,12 +134,30 @@ type ChatItem =
131134 | { kind : "stream" ; key : string ; text : string ; startedAt : number }
132135 | { kind : "reading-indicator" ; key : string } ;
133136
137+ const CHAT_HISTORY_RENDER_LIMIT = 200 ;
138+
134139function buildChatItems ( props : ChatProps ) : ChatItem [ ] {
135140 const items : ChatItem [ ] = [ ] ;
136141 const history = Array . isArray ( props . messages ) ? props . messages : [ ] ;
137142 const tools = Array . isArray ( props . toolMessages ) ? props . toolMessages : [ ] ;
138- for ( let i = 0 ; i < history . length ; i ++ ) {
139- items . push ( { kind : "message" , key : messageKey ( history [ i ] , i ) , message : history [ i ] } ) ;
143+ const historyStart = Math . max ( 0 , history . length - CHAT_HISTORY_RENDER_LIMIT ) ;
144+ if ( historyStart > 0 ) {
145+ items . push ( {
146+ kind : "message" ,
147+ key : "chat:history:notice" ,
148+ message : {
149+ role : "system" ,
150+ content : `Showing last ${ CHAT_HISTORY_RENDER_LIMIT } messages (${ historyStart } hidden).` ,
151+ timestamp : Date . now ( ) ,
152+ } ,
153+ } ) ;
154+ }
155+ for ( let i = historyStart ; i < history . length ; i ++ ) {
156+ items . push ( {
157+ kind : "message" ,
158+ key : messageKey ( history [ i ] , i ) ,
159+ message : history [ i ] ,
160+ } ) ;
140161 }
141162 for ( let i = 0 ; i < tools . length ; i ++ ) {
142163 items . push ( {
@@ -260,7 +281,11 @@ function renderReadingIndicator() {
260281 ` ;
261282}
262283
263- function renderMessage ( message : unknown , opts ?: { streaming ?: boolean } ) {
284+ function renderMessage (
285+ message : unknown ,
286+ props ?: Pick < ChatProps , "isToolOutputExpanded" | "onToolOutputToggle" > ,
287+ opts ?: { streaming ?: boolean } ,
288+ ) {
264289 const m = message as Record < string , unknown > ;
265290 const role = typeof m . role === "string" ? m . role : "unknown" ;
266291 const toolCards = extractToolCards ( message ) ;
@@ -287,14 +312,28 @@ function renderMessage(message: unknown, opts?: { streaming?: boolean }) {
287312 typeof m . timestamp === "number" ? new Date ( m . timestamp ) . toLocaleTimeString ( ) : "" ;
288313 const klass = role === "assistant" ? "assistant" : role === "user" ? "user" : "other" ;
289314 const who = role === "assistant" ? "Assistant" : role === "user" ? "You" : role ;
315+ const toolCallId = typeof m . toolCallId === "string" ? m . toolCallId : "" ;
316+ const toolCardBase =
317+ toolCallId ||
318+ ( typeof m . id === "string" ? m . id : "" ) ||
319+ ( typeof m . messageId === "string" ? m . messageId : "" ) ||
320+ ( typeof m . timestamp === "number" ? String ( m . timestamp ) : "tool-card" ) ;
290321 return html `
291322 < div class ="chat-line ${ klass } ">
292323 < div class ="chat-msg ">
293324 < div class ="chat-bubble ${ opts ?. streaming ? "streaming" : "" } ">
294325 ${ markdown
295326 ? html `< div class ="chat-text "> ${ unsafeHTML ( toSanitizedMarkdownHtml ( markdown ) ) } </ div > `
296327 : nothing }
297- ${ toolCards . map ( ( card ) => renderToolCard ( card ) ) }
328+ ${ toolCards . map ( ( card , index ) =>
329+ renderToolCard ( card , {
330+ id : `${ toolCardBase } :${ index } ` ,
331+ expanded : props ?. isToolOutputExpanded
332+ ? props . isToolOutputExpanded ( `${ toolCardBase } :${ index } ` )
333+ : false ,
334+ onToggle : props ?. onToolOutputToggle ,
335+ } ) ,
336+ ) }
298337 </ div >
299338 < div class ="chat-stamp mono ">
300339 ${ who } ${ timestamp ? html ` · ${ timestamp } ` : nothing }
@@ -368,19 +407,49 @@ function extractToolCards(message: unknown): ToolCard[] {
368407 return cards ;
369408}
370409
371- function renderToolCard ( card : ToolCard ) {
410+ function renderToolCard (
411+ card : ToolCard ,
412+ opts ?: {
413+ id : string ;
414+ expanded : boolean ;
415+ onToggle ?: ( id : string , expanded : boolean ) => void ;
416+ } ,
417+ ) {
372418 const display = resolveToolDisplay ( { name : card . name , args : card . args } ) ;
373419 const detail = formatToolDetail ( display ) ;
420+ const hasOutput = typeof card . text === "string" && card . text . length > 0 ;
421+ const expanded = opts ?. expanded ?? false ;
422+ const id = opts ?. id ?? `${ card . name } -${ Math . random ( ) } ` ;
374423 return html `
375424 < div class ="chat-tool-card ">
376425 < div class ="chat-tool-card__title "> ${ display . emoji } ${ display . label } </ div >
377426 ${ detail
378427 ? html `< div class ="chat-tool-card__detail "> ${ detail } </ div > `
379428 : nothing }
380- ${ card . text
381- ? html `< div class ="chat-tool-card__output chat-text ">
382- ${ unsafeHTML ( toSanitizedMarkdownHtml ( card . text ) ) }
383- </ div > `
429+ ${ hasOutput
430+ ? html `
431+ < details
432+ class ="chat-tool-card__details "
433+ ?open =${ expanded }
434+ @toggle =${ ( e : Event ) => {
435+ if ( ! opts ?. onToggle ) return ;
436+ const target = e . currentTarget as HTMLDetailsElement ;
437+ opts . onToggle ( id , target . open ) ;
438+ } }
439+ >
440+ < summary class ="chat-tool-card__summary ">
441+ ${ expanded ? "Hide output" : "Show output" }
442+ < span class ="chat-tool-card__summary-meta ">
443+ (${ card . text ?. length ?? 0 } chars)
444+ </ span >
445+ </ summary >
446+ ${ expanded
447+ ? html `< div class ="chat-tool-card__output chat-text ">
448+ ${ unsafeHTML ( toSanitizedMarkdownHtml ( card . text ?? "" ) ) }
449+ </ div > `
450+ : nothing }
451+ </ details >
452+ `
384453 : nothing }
385454 </ div >
386455 ` ;
0 commit comments