Skip to content

Commit 0956911

Browse files
committed
Fix the issue #124
1 parent 927f6f0 commit 0956911

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/methods/chat-session.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import * as chaiAsPromised from "chai-as-promised";
2222
import * as generateContentMethods from "./generate-content";
2323
import { GenerateContentStreamResult } from "../../types";
2424
import { ChatSession } from "./chat-session";
25+
import { getMockResponse } from "../../test-utils/mock-response";
26+
import * as request from "../requests/request";
2527

2628
use(sinonChai);
2729
use(chaiAsPromised);
@@ -45,6 +47,15 @@ describe("ChatSession", () => {
4547
);
4648
});
4749
});
50+
describe("sendMessageRecitationErrorNotAddingResponseToHistory()", () => {
51+
it("generateContent errors should be catchable", async () => {
52+
const mockResponse = getMockResponse("unary-failure-citations.json");
53+
stub(request, "makeModelRequest").resolves(mockResponse as Response);
54+
const chatSession = new ChatSession("MY_API_KEY", "a-model");
55+
await chatSession.sendMessage("hello");
56+
expect((await chatSession.getHistory()).length).equals(0);
57+
});
58+
});
4859
describe("sendMessageStream()", () => {
4960
it("generateContentStream errors should be catchable", async () => {
5061
const clock = useFakeTimers();

src/methods/chat-session.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ export class ChatSession {
110110
.then((result) => {
111111
if (
112112
result.response.candidates &&
113-
result.response.candidates.length > 0
113+
result.response.candidates.length > 0 &&
114+
result.response.candidates[0]?.content !== undefined
114115
) {
115116
this._history.push(newContent);
116117
const responseContent: Content = {
@@ -179,7 +180,11 @@ export class ChatSession {
179180
})
180181
.then((streamResult) => streamResult.response)
181182
.then((response) => {
182-
if (response.candidates && response.candidates.length > 0) {
183+
if (
184+
response.candidates &&
185+
response.candidates.length > 0 &&
186+
response.candidates[0]?.content !== undefined
187+
) {
183188
this._history.push(newContent);
184189
const responseContent = { ...response.candidates[0].content };
185190
// Response seems to come back without a role set.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"candidates": [
3+
{
4+
"finishReason": "RECITATION",
5+
"index": 0
6+
}
7+
],
8+
"usageMetadata": {
9+
"promptTokenCount": 18,
10+
"totalTokenCount": 18
11+
},
12+
"modelVersion": "gemini-1.5-flash-001"
13+
}

0 commit comments

Comments
 (0)