@@ -42,8 +42,6 @@ class ChatCommand extends BaseCommand {
42
42
43
43
static flags = { } ;
44
44
45
- private lastMessage = 'How can I help you today?' ;
46
-
47
45
private messages = new ChatMessageHistory ( ) ;
48
46
49
47
async run ( ) : Promise < void > {
@@ -54,16 +52,25 @@ class ChatCommand extends BaseCommand {
54
52
apiKey : config . apiKey ,
55
53
platform : config . platform as unknown as Platform
56
54
} ) ;
57
- const userDisplay = this . getDisplayContent ( PromptRole . USER ) ;
55
+ const aiDisplay = this . getDisplayContent ( PromptRole . AI ) ;
56
+ let lastMessage = 'How can I help you today?' ;
57
+
58
+ process . stdout . write ( aiDisplay + lastMessage + '\n' ) ;
58
59
59
60
// eslint-disable-next-line no-constant-condition
60
61
while ( true ) {
61
- const aiMessage = await this . addMessage ( PromptRole . AI , this . lastMessage ) ;
62
- const userMessage = await this . getUserMessage ( aiMessage + `\n${ userDisplay } ` ) ;
63
- const answer = await detector . chat ( userMessage , await this . messages . getMessages ( ) ) ;
62
+ const userMessage = await this . getUserMessage ( ) ;
63
+ const stream = detector . chat ( userMessage , await this . messages . getMessages ( ) ) ;
64
+
65
+ process . stdout . write ( aiDisplay ) ;
66
+ stream . pipe ( process . stdout ) ;
67
+
68
+ lastMessage = await stream . getData ( ) ;
69
+
70
+ process . stdout . write ( '\n' ) ;
64
71
65
72
await this . addMessage ( PromptRole . USER , userMessage ) ;
66
- this . lastMessage = answer ;
73
+ await this . addMessage ( PromptRole . AI , lastMessage ) ;
67
74
}
68
75
} else {
69
76
this . showHelp ( ) ;
@@ -84,9 +91,11 @@ class ChatCommand extends BaseCommand {
84
91
return chalk [ roleDisplay . color ] ( `[${ roleDisplay . name } ] ` ) ;
85
92
}
86
93
87
- private getUserMessage ( aiMessage : string ) : Promise < string > {
94
+ private getUserMessage ( ) : Promise < string > {
95
+ const userDisplay = this . getDisplayContent ( PromptRole . USER ) ;
96
+
88
97
return new Promise < string > ( ( resolve ) => {
89
- reader . question ( aiMessage , resolve ) ;
98
+ reader . question ( userDisplay , resolve ) ;
90
99
} ) ;
91
100
}
92
101
}
0 commit comments