Skip to content

Commit 28df5cc

Browse files
authored
Merge d024da2 into bc5a7c4
2 parents bc5a7c4 + d024da2 commit 28df5cc

File tree

3 files changed

+16
-49
lines changed

3 files changed

+16
-49
lines changed

.changeset/dull-rabbits-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/ai': patch
3+
---
4+
5+
Removed error logging in `ChatSession.sendMessageStream()`, since these errors are catchable.

packages/ai/src/methods/chat-session.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { match, restore, stub, useFakeTimers } from 'sinon';
2020
import sinonChai from 'sinon-chai';
2121
import chaiAsPromised from 'chai-as-promised';
2222
import * as generateContentMethods from './generate-content';
23-
import { Content, GenerateContentStreamResult, InferenceMode } from '../types';
23+
import { Content, InferenceMode } from '../types';
2424
import { ChatSession } from './chat-session';
2525
import { ApiSettings } from '../types/internal';
2626
import { VertexAIBackend } from '../backend';
@@ -136,31 +136,5 @@ describe('ChatSession', () => {
136136
expect(consoleStub).to.not.be.called;
137137
clock.restore();
138138
});
139-
it('downstream sendPromise errors should log but not throw', async () => {
140-
const clock = useFakeTimers();
141-
const consoleStub = stub(console, 'error');
142-
// make response undefined so that response.candidates errors
143-
const generateContentStreamStub = stub(
144-
generateContentMethods,
145-
'generateContentStream'
146-
).resolves({} as unknown as GenerateContentStreamResult);
147-
const chatSession = new ChatSession(
148-
fakeApiSettings,
149-
'a-model',
150-
fakeChromeAdapter
151-
);
152-
await chatSession.sendMessageStream('hello');
153-
expect(generateContentStreamStub).to.be.calledWith(
154-
fakeApiSettings,
155-
'a-model',
156-
match.any
157-
);
158-
await clock.runAllAsync();
159-
expect(consoleStub.args[0][1].toString()).to.include(
160-
// Firefox has different wording when a property is undefined
161-
'undefined'
162-
);
163-
clock.restore();
164-
});
165139
});
166140
});

packages/ai/src/methods/chat-session.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ import { ApiSettings } from '../types/internal';
3232
import { logger } from '../logger';
3333
import { ChromeAdapter } from '../types/chrome-adapter';
3434

35-
/**
36-
* Do not log a message for this error.
37-
*/
38-
const SILENT_ERROR = 'SILENT_ERROR';
39-
4035
/**
4136
* ChatSession class that enables sending chat messages and stores
4237
* history of sent and received messages so far.
@@ -153,14 +148,9 @@ export class ChatSession {
153148
this.requestOptions
154149
);
155150

156-
// Add onto the chain.
157-
this._sendPromise = this._sendPromise
158-
.then(() => streamPromise)
159-
// This must be handled to avoid unhandled rejection, but jump
160-
// to the final catch block with a label to not log this error.
161-
.catch(_ignored => {
162-
throw new Error(SILENT_ERROR);
163-
})
151+
// This promise is not returned to the user, but is used to chain promise sequences that
152+
// update the history and handle errors.
153+
const historyUpdatePromise = streamPromise
164154
.then(streamResult => streamResult.response)
165155
.then(response => {
166156
if (response.candidates && response.candidates.length > 0) {
@@ -180,16 +170,14 @@ export class ChatSession {
180170
}
181171
}
182172
})
183-
.catch(e => {
184-
// Errors in streamPromise are already catchable by the user as
185-
// streamPromise is returned.
186-
// Avoid duplicating the error message in logs.
187-
if (e.message !== SILENT_ERROR) {
188-
// Users do not have access to _sendPromise to catch errors
189-
// downstream from streamPromise, so they should not throw.
190-
logger.error(e);
191-
}
173+
.catch(() => {
174+
// Suppress unhandled rejection error. The user is responsible for handling the error
175+
// on the returned streamPromise.
192176
});
177+
178+
// Chain the history update to the serialization promise.
179+
this._sendPromise = historyUpdatePromise;
180+
193181
return streamPromise;
194182
}
195183
}

0 commit comments

Comments
 (0)