@@ -3,8 +3,30 @@ import { cursor, clear, fg, style, write, screen } from "./terminal"
33import { parseKey , LineEditor } from "./input"
44import { Spinner } from "./spinner"
55import { chat } from "./session"
6-
7- const PROMPT = `${ fg . cyan } >${ style . reset } `
6+ import { formatTokens , formatDuration } from "../metrics"
7+ import { Icons } from "../cmd/tui/util/icons"
8+ import { renderTaskPanel , type Task , type AgentStatus } from "./taskpanel"
9+ import { renderStatusLine , type StatusLineState } from "./statusline"
10+ import { renderBottomBar , renderPrompt , type BottomBarState } from "./bottombar"
11+
12+ // UI State
13+ let tasksVisible = false
14+ let tasks : Task [ ] = [ ]
15+ let agents : AgentStatus [ ] = [ ]
16+ let statusLine : StatusLineState = {
17+ activity : "Idle" ,
18+ duration : 0 ,
19+ tokens : 0 ,
20+ tasksVisible : false ,
21+ }
22+ let bottomBar : BottomBarState = {
23+ permissionMode : "ask" ,
24+ fileChanges : {
25+ total : 0 ,
26+ added : 0 ,
27+ removed : 0 ,
28+ } ,
29+ }
830
931async function main ( ) {
1032 // Check TTY
@@ -28,7 +50,7 @@ async function main() {
2850 process . stdin . resume ( )
2951
3052 // Render prompt
31- editor . render ( PROMPT )
53+ editor . render ( renderPrompt ( ) )
3254
3355 // Handle input
3456 process . stdin . on ( "data" , async ( data : Buffer ) => {
@@ -40,6 +62,13 @@ async function main() {
4062 process . exit ( 0 )
4163 }
4264
65+ // Ctrl+T to toggle task panel
66+ if ( key . ctrl && key . name === "t" ) {
67+ tasksVisible = ! tasksVisible
68+ statusLine . tasksVisible = tasksVisible
69+ editor . render ( renderPrompt ( ) )
70+ }
71+
4372 const result = editor . handle ( key )
4473
4574 if ( result !== null ) {
@@ -51,9 +80,9 @@ async function main() {
5180 await handleMessage ( result )
5281 }
5382
54- editor . render ( PROMPT )
83+ editor . render ( renderPrompt ( ) )
5584 } else {
56- editor . render ( PROMPT )
85+ editor . render ( renderPrompt ( ) )
5786 }
5887 } )
5988
@@ -102,6 +131,9 @@ async function handleMessage(message: string) {
102131 spinner . start ( )
103132
104133 let first = true
134+ let totalTokens = 0
135+ const startTime = Date . now ( )
136+
105137 try {
106138 for await ( const chunk of chat ( message ) ) {
107139 if ( first ) {
@@ -113,18 +145,26 @@ async function handleMessage(message: string) {
113145 write ( chunk . content )
114146 }
115147
116- if ( chunk . type === "tool_start" && chunk . tool ) {
117- write ( `\n${ fg . yellow } ▶ ${ chunk . tool } ${ style . reset } \n` )
148+ if ( chunk . type === "tool_start" && chunk . tool ?. trim ( ) ) {
149+ write ( `\n${ Icons . taskIcon ( "progress" ) } ${ chunk . tool } \n` )
118150 }
119151
120- if ( chunk . type === "tool_end" && chunk . tool ) {
121- write ( `${ fg . green } ✓ ${ chunk . tool } ${ style . reset } \n` )
152+ if ( chunk . type === "tool_end" && chunk . tool ?. trim ( ) ) {
153+ write ( `${ Icons . taskIcon ( "completed" ) } ${ chunk . tool } \n` )
122154 }
123155
124156 if ( chunk . type === "error" && chunk . content ) {
125- write ( `\n${ fg . red } Error: ${ chunk . content } ${ style . reset } \n` )
157+ const safeContent = chunk . content . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, "" ) . replace ( / [ \x00 - \x1f \x7f ] / g, "" )
158+ write ( `\n${ fg . red } Error: ${ safeContent } ${ style . reset } \n` )
159+ }
160+
161+ if ( chunk . tokens !== undefined ) {
162+ totalTokens += chunk . tokens
126163 }
127164 }
165+
166+ const duration = Date . now ( ) - startTime
167+ write ( `\n${ fg . gray } ${ formatDuration ( duration ) } · ${ formatTokens ( totalTokens ) } ${ style . reset } \n` )
128168 } catch ( err ) {
129169 if ( first ) spinner . stop ( false )
130170 const msg = err instanceof Error ? err . message : String ( err )
0 commit comments