Skip to content

Commit ad1e17e

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
fix: fix stream send message content
PiperOrigin-RevId: 590343492
1 parent 886a3cd commit ad1e17e

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

.release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.1.0"
2+
".": "0.0.0"
33
}

CHANGELOG.md

-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +0,0 @@
1-
# Changelog
2-
3-
## 0.1.0 (2023-12-12)
4-
5-
6-
### Features
7-
8-
* add streamGenerateContent method ([6263800](https://github.com/googleapis/nodejs-vertexai/commit/626380039d7bb2fb9af9219f70ad549950b5f490))
9-
* add streamSendMessage method ([598b1dd](https://github.com/googleapis/nodejs-vertexai/commit/598b1dd7ca8d84c9b32e633a65634abea232f7de))
10-
* add generateContent method ([d7f1f0f](https://github.com/googleapis/nodejs-vertexai/commit/d7f1f0f66b7bf22c2cb59a8ef698b426cf7e3b8b))
11-
* add countTokens method ([e0265a3](https://github.com/googleapis/nodejs-vertexai/commit/e0265a36d73b460c66062a0b520b5556d0aa894b))

src/index.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export declare interface StartChatSessionRequest extends StartChatParams {
145145
export class ChatSession {
146146
private project: string;
147147
private location: string;
148+
private _send_stream_promise: Promise<void> = Promise.resolve();
148149

149150
private historyInternal: Content[];
150151
private _vertex_instance: VertexAI_Internal;
@@ -195,21 +196,12 @@ export class ChatSession {
195196

196197
return Promise.resolve({response: generateContentResponse});
197198
}
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;
213205
const streamGenerateContentResponse =
214206
await streamGenerateContentResult.response;
215207
// Only push the latest message to history if the response returned a result
@@ -225,7 +217,22 @@ export class ChatSession {
225217
// TODO: handle promptFeedback in the response
226218
throw new Error('Did not get a candidate from the model');
227219
}
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);
228234

235+
this._send_stream_promise = this.appendHistory(streamGenerateContentResultPromise, newContent);
229236
return streamGenerateContentResultPromise;
230237
}
231238
}

0 commit comments

Comments
 (0)