@@ -145,6 +145,7 @@ export declare interface StartChatSessionRequest extends StartChatParams {
145
145
export class ChatSession {
146
146
private project : string ;
147
147
private location : string ;
148
+ private _send_stream_promise : Promise < void > = Promise . resolve ( ) ;
148
149
149
150
private historyInternal : Content [ ] ;
150
151
private _vertex_instance : VertexAI_Internal ;
@@ -195,21 +196,12 @@ export class ChatSession {
195
196
196
197
return Promise . resolve ( { response : generateContentResponse } ) ;
197
198
}
198
-
199
- async sendMessageStream ( request : string | Array < string | Part > ) :
200
- Promise < StreamGenerateContentResult > {
201
- const newContent : Content = formulateNewContent ( request ) ;
202
- const generateContentrequest : GenerateContentRequest = {
203
- contents : this . historyInternal . concat ( [ newContent ] ) ,
204
- safety_settings : this . safety_settings ,
205
- generation_config : this . generation_config ,
206
- } ;
207
-
208
- const streamGenerateContentResultPromise =
209
- this . _model_instance . generateContentStream (
210
- generateContentrequest ) ;
211
- const streamGenerateContentResult =
212
- await streamGenerateContentResultPromise ;
199
+
200
+ async appendHistory (
201
+ streamGenerateContentResultPromise : Promise < StreamGenerateContentResult > ,
202
+ newContent : Content ,
203
+ ) : Promise < void > {
204
+ const streamGenerateContentResult = await streamGenerateContentResultPromise ;
213
205
const streamGenerateContentResponse =
214
206
await streamGenerateContentResult . response ;
215
207
// Only push the latest message to history if the response returned a result
@@ -225,7 +217,22 @@ export class ChatSession {
225
217
// TODO: handle promptFeedback in the response
226
218
throw new Error ( 'Did not get a candidate from the model' ) ;
227
219
}
220
+ }
221
+
222
+ async sendMessageStream ( request : string | Array < string | Part > ) :
223
+ Promise < StreamGenerateContentResult > {
224
+ const newContent : Content = formulateNewContent ( request ) ;
225
+ const generateContentrequest : GenerateContentRequest = {
226
+ contents : this . historyInternal . concat ( [ newContent ] ) ,
227
+ safety_settings : this . safety_settings ,
228
+ generation_config : this . generation_config ,
229
+ } ;
230
+
231
+ const streamGenerateContentResultPromise =
232
+ this . _model_instance . generateContentStream (
233
+ generateContentrequest ) ;
228
234
235
+ this . _send_stream_promise = this . appendHistory ( streamGenerateContentResultPromise , newContent ) ;
229
236
return streamGenerateContentResultPromise ;
230
237
}
231
238
}
0 commit comments