@@ -38,6 +38,7 @@ type Chat struct {
3838 Error * errbook.AiError
3939 Output string
4040 GlamOutput string
41+ TokenUsage llms.Usage
4142
4243 state state
4344 opts * Options
@@ -82,7 +83,7 @@ func NewChat(cfg *options.Config, opts ...Option) *Chat {
8283 }
8384}
8485
85- // Run starts the chat .
86+ // Run starts the c .
8687func (c * Chat ) Run () error {
8788 if _ , err := tea .NewProgram (c ).Run (); err != nil {
8889 return errbook .Wrap ("Couldn't start Bubble Tea program." , err )
@@ -110,7 +111,7 @@ func (c *Chat) Run() error {
110111 }
111112
112113 if c .config .CacheWriteToID != "" {
113- return saveConversation (c )
114+ return c . saveConversation ()
114115 }
115116
116117 return nil
@@ -158,6 +159,7 @@ func (c *Chat) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
158159 }
159160 if msg .IsLast () {
160161 c .state = doneState
162+ c .TokenUsage = msg .GetUsage ()
161163 return c , c .quit
162164 }
163165 cmds = append (cmds , c .awaitChatCompletedCmd ())
@@ -330,9 +332,20 @@ func (c *Chat) readStdinCmd() tea.Msg {
330332 }
331333}
332334
333- func saveConversation (chat * Chat ) error {
334- if chat .config .NoCache {
335- if ! chat .config .Quiet {
335+ func (c * Chat ) renderMarkdown (raw string ) string {
336+ if c .config .Raw {
337+ return raw
338+ }
339+ rendered , err := c .glam .Render (raw )
340+ if err != nil {
341+ return raw
342+ }
343+ return rendered
344+ }
345+
346+ func (c * Chat ) saveConversation () error {
347+ if c .config .NoCache {
348+ if ! c .config .Quiet {
336349 fmt .Fprintf (
337350 os .Stderr ,
338351 "\n Conversation was not saved because %s or %s is set.\n " ,
@@ -344,9 +357,9 @@ func saveConversation(chat *Chat) error {
344357 }
345358
346359 ctx := context .Background ()
347- convoStore := chat .engine .GetConvoStore ()
348- writeToID := chat .config .CacheWriteToID
349- writeToTitle := strings .TrimSpace (chat .config .CacheWriteToTitle )
360+ convoStore := c .engine .GetConvoStore ()
361+ writeToID := c .config .CacheWriteToID
362+ writeToTitle := strings .TrimSpace (c .config .CacheWriteToTitle )
350363
351364 if convo .MatchSha1 (writeToTitle ) || writeToTitle == "" {
352365 messages , err := convoStore .Messages (ctx , writeToID )
@@ -363,28 +376,32 @@ func saveConversation(chat *Chat) error {
363376 if err := convoStore .PersistentMessages (ctx , writeToID ); err != nil {
364377 return errbook .Wrap (fmt .Sprintf (
365378 "There was a problem writing %s to the cache. Use %s / %s to disable it." ,
366- chat .config .CacheWriteToID ,
379+ c .config .CacheWriteToID ,
367380 console .StderrStyles ().InlineCode .Render ("--no-cache" ),
368381 console .StderrStyles ().InlineCode .Render ("NO_CACHE" ),
369382 ), err )
370383 }
371384
372- if err := convoStore .SaveConversation (ctx , writeToID , writeToTitle , chat .config .Model ); err != nil {
385+ if err := convoStore .SaveConversation (ctx , writeToID , writeToTitle , c .config .Model ); err != nil {
373386 return errbook .Wrap (fmt .Sprintf (
374387 "There was a problem writing %s to the cache. Use %s / %s to disable it." ,
375- chat .config .CacheWriteToID ,
388+ c .config .CacheWriteToID ,
376389 console .StderrStyles ().InlineCode .Render ("--no-cache" ),
377390 console .StderrStyles ().InlineCode .Render ("NO_CACHE" ),
378391 ), err )
379392 }
380393
381- if ! chat .config .Quiet {
382- fmt .Fprintln (
383- os .Stderr ,
384- "\n Conversation saved:" ,
385- console .StderrStyles ().InlineCode .Render (chat .config .CacheWriteToID [:convo .Sha1short ]),
386- console .StderrStyles ().Comment .Render (writeToTitle ),
387- )
394+ if ! c .config .Quiet {
395+ content := fmt .Sprintf ("\n **Conversation successfully saved:** `%s` `%s`\n " , c .config .CacheWriteToID [:convo .Sha1short ], writeToTitle )
396+ if c .config .ShowTokenUsages {
397+ content += fmt .Sprintf ("\n First Token: `%.3fs` | Avg: `%.3f/s` | Total: `%.3fs` | Tokens: `%d`" ,
398+ c .TokenUsage .FirstTokenTime .Seconds (),
399+ c .TokenUsage .AverageTokensPerSecond ,
400+ c .TokenUsage .TotalTime .Seconds (),
401+ c .TokenUsage .TotalTokens ,
402+ )
403+ }
404+ _ , _ = fmt .Fprint (os .Stderr , c .renderMarkdown (content ))
388405 }
389406
390407 return nil
0 commit comments