1
1
import { CHATS_SAVE_DIRECTORY } from '../../file-utils.js' ;
2
- import { getVerbose , output , outputVerbose , outputWarning , setVerbose } from '../../output.js' ;
2
+ import { formatCost , formatSpeed , formatTokenCount } from '../../format.js' ;
3
+ import { getVerbose , outputSystem , outputWarning , setVerbose } from '../../output.js' ;
3
4
import { getProvider , getProviderConfig } from './providers.js' ;
4
- import { messages } from './state.js' ;
5
- import { exit , saveConversation } from './utils.js' ;
5
+ import { messages , totalUsage } from './state.js' ;
6
+ import { calculateUsageCost } from './usage.js' ;
7
+ import { exit , filterOutApiKey , saveConversation } from './utils.js' ;
6
8
7
9
export function processChatCommand ( input : string ) {
8
10
if ( ! input . startsWith ( '/' ) ) {
@@ -24,21 +26,27 @@ export function processChatCommand(input: string) {
24
26
return true ;
25
27
}
26
28
29
+ if ( command === '/debug' ) {
30
+ outputDebugInfo ( ) ;
31
+ return true ;
32
+ }
33
+
27
34
if ( command === '/forget' ) {
28
35
// Clear all messages
29
36
messages . length = 0 ;
37
+ outputSystem ( 'Forgot all past messages from the current session.\n' ) ;
30
38
return true ;
31
39
}
32
40
33
41
if ( command === '/verbose' ) {
34
42
setVerbose ( ! getVerbose ( ) ) ;
35
- output ( `Verbose mode: ${ getVerbose ( ) ? 'on' : 'off' } ` ) ;
43
+ outputSystem ( `Verbose mode: ${ getVerbose ( ) ? 'on' : 'off' } \n ` ) ;
36
44
return true ;
37
45
}
38
46
39
47
if ( input === '/save' ) {
40
48
const saveConversationMessage = saveConversation ( messages ) ;
41
- output ( saveConversationMessage ) ;
49
+ outputSystem ( saveConversationMessage ) ;
42
50
return true ;
43
51
}
44
52
@@ -48,37 +56,41 @@ export function processChatCommand(input: string) {
48
56
49
57
export function outputHelp ( ) {
50
58
const lines = [
51
- '' ,
52
59
'Available commands:' ,
53
- ' - /exit: Exit the CLI' ,
54
- ' - /info: Show current provider, model, and system prompt' ,
55
- ' - /forget: AI will forget previous messages' ,
56
- ` - /save: Save in a text file in ${ CHATS_SAVE_DIRECTORY } ` ,
57
- ' - /verbose: Toggle verbose output' ,
60
+ '- /exit: Exit the CLI' ,
61
+ '- /info: Show current provider, model, and system prompt' ,
62
+ '- /forget: AI will forget previous messages' ,
63
+ `- /save: Save in a text file in ${ CHATS_SAVE_DIRECTORY } ` ,
64
+ '- /verbose: Toggle verbose output' ,
58
65
'' ,
59
66
] ;
60
-
61
- output ( lines . join ( '\n' ) ) ;
67
+ outputSystem ( lines . join ( '\n' ) ) ;
62
68
}
63
69
64
70
export function outputInfo ( ) {
65
71
const provider = getProvider ( ) ;
66
72
const providerConfig = getProviderConfig ( ) ;
67
73
68
74
const lines = [
69
- '' ,
70
- 'Info:' ,
71
- ` - Provider: ${ provider . label } ` ,
72
- ` - Model: ${ providerConfig . model } ` ,
73
- ` - System prompt: ${ providerConfig . systemPrompt } ` ,
75
+ 'Session info:' ,
76
+ `- Provider: ${ provider . label } ` ,
77
+ `- Model: ${ providerConfig . model } ` ,
78
+ `- Cost: ${ formatCost ( calculateUsageCost ( totalUsage , { provider, providerConfig } ) ) } ` ,
79
+ `- Usage: ${ formatTokenCount ( totalUsage . inputTokens ) } input token(s), ${ formatTokenCount ( totalUsage . outputTokens ) } output token(s), ${ totalUsage . requests } request(s)usag` ,
80
+ `- Avg Speed: ${ formatSpeed ( totalUsage . outputTokens , totalUsage . responseTime ) } ` ,
81
+ `- System prompt: ${ providerConfig . systemPrompt } ` ,
74
82
'' ,
75
83
] ;
76
- output ( lines . join ( '\n' ) ) ;
84
+ outputSystem ( lines . join ( '\n' ) ) ;
85
+ }
86
+
87
+ export function outputDebugInfo ( ) {
88
+ outputSystem ( `Provider: ${ toJson ( getProvider ( ) . label ) } \n` ) ;
89
+ outputSystem ( `Provider Config: ${ toJson ( getProviderConfig ( ) ) } \n` ) ;
90
+ outputSystem ( `Messages: ${ toJson ( messages ) } \n` ) ;
91
+ outputSystem ( `Usage: ${ toJson ( totalUsage ) } \n` ) ;
92
+ }
77
93
78
- const rawMessages = JSON . stringify (
79
- messages . map ( ( m ) => `${ m . role } : ${ m . content } ` ) ,
80
- null ,
81
- 2 ,
82
- ) ;
83
- outputVerbose ( `Messages: ${ rawMessages } \n` ) ;
94
+ function toJson ( value : any ) {
95
+ return JSON . stringify ( value , filterOutApiKey , 2 ) ;
84
96
}
0 commit comments