@@ -3,6 +3,7 @@ import { cursor, clear, fg, style, write, screen } from "./terminal"
33import { parseKey , LineEditor } from "./input"
44import { Spinner } from "./spinner"
55import { chat , command } from "./session"
6+ import { createMarkdownRenderer } from "./markdown"
67import { formatTokens , formatDuration } from "../metrics"
78import { Icons } from "../cmd/tui/util/icons"
89import { type Task , type AgentStatus } from "./taskpanel"
@@ -20,6 +21,30 @@ import { Command } from "../../command"
2021import { select } from "./select"
2122import type { ChatChunk } from "./session"
2223
24+ export function summarizeInput ( tool : string , input ?: Record < string , unknown > ) : string {
25+ if ( ! input ) return ""
26+ const str = ( key : string ) => String ( input [ key ] || "" )
27+
28+ if ( tool === "bash" ) return str ( "command" ) . slice ( 0 , 60 )
29+ if ( tool === "read" ) return str ( "filePath" ) . split ( "/" ) . slice ( - 2 ) . join ( "/" )
30+ if ( tool === "write" || tool === "edit" ) return str ( "filePath" ) . split ( "/" ) . slice ( - 2 ) . join ( "/" )
31+ if ( tool === "rg" || tool === "grep" ) {
32+ const pattern = str ( "pattern" ) . slice ( 0 , 30 )
33+ const include = str ( "include" )
34+ return include ? `"${ pattern } " in ${ include } ` : `"${ pattern } "`
35+ }
36+ if ( tool === "glob" ) return str ( "pattern" ) . slice ( 0 , 60 )
37+ if ( tool === "task" ) {
38+ const agent = str ( "subagent_type" )
39+ const desc = str ( "description" ) . slice ( 0 , 40 )
40+ return agent ? `@${ agent } : ${ desc } ` : desc
41+ }
42+ if ( tool === "todowrite" || tool === "todoread" ) return "todo list"
43+
44+ const first = Object . values ( input ) . find ( ( v ) => typeof v === "string" )
45+ return first ? String ( first ) . slice ( 0 , 60 ) : ""
46+ }
47+
2348// UI State
2449let tasksVisible = false
2550let tasks : Task [ ] = [ ]
@@ -357,6 +382,9 @@ async function handleCustomCommand(name: string, args: string) {
357382 let totalTokens = 0
358383 const startTime = Date . now ( )
359384 isOperationInProgress = true
385+ let lastChunkWasToolStart = false
386+ let lastToolSummary = ""
387+ const md = createMarkdownRenderer ( )
360388
361389 try {
362390 const options = {
@@ -378,18 +406,32 @@ async function handleCustomCommand(name: string, args: string) {
378406 }
379407
380408 if ( chunk . type === "text" && chunk . content ) {
381- write ( chunk . content )
409+ lastChunkWasToolStart = false
410+ write ( md . render ( chunk . content ) )
382411 }
383412
384413 if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
385- write ( `${ fg . gray } ${ chunk . tool ?. trim ( ) } ${ style . reset } ` )
414+ const tool = chunk . tool . trim ( )
415+ lastToolSummary = summarizeInput ( tool , chunk . input )
416+ const summary = lastToolSummary ? ` ${ lastToolSummary } ` : ""
417+ write ( `${ fg . gray } ◇ ${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } ${ fg . gray } ${ summary } ${ style . reset } \n` )
418+ lastChunkWasToolStart = true
386419 }
387420
388421 if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
389- write ( `${ fg . gray } ✓${ style . reset } ` )
422+ const tool = chunk . tool . trim ( )
423+ if ( lastChunkWasToolStart && lastToolSummary === summarizeInput ( tool , chunk . input ) ) {
424+ write (
425+ `\x1b[1A\r\x1b[2K${ fg . green } ✓${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } ${ fg . gray } ${ lastToolSummary ? ` ${ lastToolSummary } ` : "" } ${ style . reset } \n` ,
426+ )
427+ } else {
428+ write ( `${ fg . green } ✓${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } \n` )
429+ }
430+ lastChunkWasToolStart = false
390431 }
391432
392433 if ( chunk . type === "error" && chunk . content ) {
434+ lastChunkWasToolStart = false
393435 const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
394436 write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
395437 }
@@ -399,6 +441,7 @@ async function handleCustomCommand(name: string, args: string) {
399441 }
400442 }
401443
444+ write ( md . flush ( ) )
402445 const duration = Date . now ( ) - startTime
403446 write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
404447 } finally {
@@ -424,6 +467,9 @@ async function handleMessage(message: string) {
424467 let totalTokens = 0
425468 const startTime = Date . now ( )
426469 isOperationInProgress = true
470+ let lastChunkWasToolStart = false
471+ let lastToolSummary = ""
472+ const md = createMarkdownRenderer ( )
427473
428474 try {
429475 const options = {
@@ -445,18 +491,32 @@ async function handleMessage(message: string) {
445491 }
446492
447493 if ( chunk . type === "text" && chunk . content ) {
448- write ( chunk . content )
494+ lastChunkWasToolStart = false
495+ write ( md . render ( chunk . content ) )
449496 }
450497
451498 if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
452- write ( `${ fg . gray } ${ chunk . tool ?. trim ( ) } ${ style . reset } ` )
499+ const tool = chunk . tool . trim ( )
500+ lastToolSummary = summarizeInput ( tool , chunk . input )
501+ const summary = lastToolSummary ? ` ${ lastToolSummary } ` : ""
502+ write ( `${ fg . gray } ◇ ${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } ${ fg . gray } ${ summary } ${ style . reset } \n` )
503+ lastChunkWasToolStart = true
453504 }
454505
455506 if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
456- write ( `${ fg . gray } ✓${ style . reset } ` )
507+ const tool = chunk . tool . trim ( )
508+ if ( lastChunkWasToolStart && lastToolSummary === summarizeInput ( tool , chunk . input ) ) {
509+ write (
510+ `\x1b[1A\r\x1b[2K${ fg . green } ✓${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } ${ fg . gray } ${ lastToolSummary ? ` ${ lastToolSummary } ` : "" } ${ style . reset } \n` ,
511+ )
512+ } else {
513+ write ( `${ fg . green } ✓${ style . reset } ${ fg . cyan } ${ tool } ${ style . reset } \n` )
514+ }
515+ lastChunkWasToolStart = false
457516 }
458517
459518 if ( chunk . type === "error" && chunk . content ) {
519+ lastChunkWasToolStart = false
460520 const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
461521 write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
462522 }
@@ -466,6 +526,7 @@ async function handleMessage(message: string) {
466526 }
467527 }
468528
529+ write ( md . flush ( ) )
469530 const duration = Date . now ( ) - startTime
470531 write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
471532 } finally {
0 commit comments