Skip to content

Added error throw for when user enters multiple messages. #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/methods/chat-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ describe("ChatSession", () => {
match.any,
);
});
it("sendMessage() should reset messageInProgress flag after resolving promise", async () => {
const mockResponse = getMockResponse(
"unary-success-basic-reply-short.json",
);
stub(request, "makeModelRequest").resolves(mockResponse as Response);
const chatSession = new ChatSession("MY_API_KEY", "a-model");
await chatSession.sendMessage("hello");
expect(chatSession["_messageInProgress"]).to.be.false;
});
});
describe("sendMessageRecitationErrorNotAddingResponseToHistory()", () => {
it("generateContent errors should be catchable", async () => {
Expand Down Expand Up @@ -75,6 +84,22 @@ describe("ChatSession", () => {
expect(consoleStub).to.not.be.called;
clock.restore();
});
it("sendMessageStream() should reset messageInProgress flag after resolving promise", async () => {
const consoleStub = stub(console, "error");
const generateContentStreamStub = stub(
generateContentMethods,
"generateContentStream",
).resolves();
const chatSession = new ChatSession("MY_API_KEY", "a-model");
await expect(chatSession.sendMessageStream("hello")).to.be.fulfilled;
expect(generateContentStreamStub).to.be.calledWith(
"MY_API_KEY",
"a-model",
match.any,
);
expect(consoleStub).to.not.be.called;
expect(chatSession["_messageInProgress"]).to.be.false;
});
it("downstream sendPromise errors should log but not throw", async () => {
const clock = useFakeTimers();
const consoleStub = stub(console, "error");
Expand Down
17 changes: 17 additions & 0 deletions src/methods/chat-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class ChatSession {
private _apiKey: string;
private _history: Content[] = [];
private _sendPromise: Promise<void> = Promise.resolve();
private _messageInProgress: boolean = false;

constructor(
apiKey: string,
Expand Down Expand Up @@ -81,6 +82,12 @@ export class ChatSession {
request: string | Array<string | Part>,
requestOptions: SingleRequestOptions = {},
): Promise<GenerateContentResult> {
if (this._messageInProgress) {
throw new Error(
"sendMessage() was called while another message was in progress, this may lead to unexpected behavior.",
);
}
this._messageInProgress = true;
await this._sendPromise;
const newContent = formatNewContent(request);
const generateContentRequest: GenerateContentRequest = {
Expand Down Expand Up @@ -126,6 +133,9 @@ export class ChatSession {
}
}
finalResult = result;
})
.finally(() => {
this._messageInProgress = false;
});
await this._sendPromise;
return finalResult;
Expand All @@ -144,6 +154,12 @@ export class ChatSession {
request: string | Array<string | Part>,
requestOptions: SingleRequestOptions = {},
): Promise<GenerateContentStreamResult> {
if (this._messageInProgress) {
throw new Error(
"sendMessageStream() was called while another message was in progress, this may lead to unexpected behavior.",
);
}
this._messageInProgress = true;
await this._sendPromise;
const newContent = formatNewContent(request);
const generateContentRequest: GenerateContentRequest = {
Expand Down Expand Up @@ -203,6 +219,7 @@ export class ChatSession {
console.error(e);
}
});
this._messageInProgress = false;
return streamPromise;
}
}