Skip to content

Commit 109f9be

Browse files
authored
Merge 9e8ef56 into 052e438
2 parents 052e438 + 9e8ef56 commit 109f9be

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

.changeset/tall-peas-tell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/vertexai': patch
3+
---
4+
5+
Send App Check dummy token in header if there is an App Check getToken error.

packages/vertexai/src/logger.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { Logger } from '@firebase/logger';
19+
20+
export const logger = new Logger('@firebase/vertexai');

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('ChatSession', () => {
8787
match.any
8888
);
8989
await clock.runAllAsync();
90-
expect(consoleStub.args[0][0].toString()).to.include(
90+
expect(consoleStub.args[0][1].toString()).to.include(
9191
// Firefox has different wording when a property is undefined
9292
'undefined'
9393
);

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { formatBlockErrorMessage } from '../requests/response-helpers';
2929
import { validateChatHistory } from './chat-session-helpers';
3030
import { generateContent, generateContentStream } from './generate-content';
3131
import { ApiSettings } from '../types/internal';
32+
import { logger } from '../logger';
3233

3334
/**
3435
* Do not log a message for this error.
@@ -112,7 +113,7 @@ export class ChatSession {
112113
} else {
113114
const blockErrorMessage = formatBlockErrorMessage(result.response);
114115
if (blockErrorMessage) {
115-
console.warn(
116+
logger.warn(
116117
`sendMessage() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`
117118
);
118119
}
@@ -169,7 +170,7 @@ export class ChatSession {
169170
} else {
170171
const blockErrorMessage = formatBlockErrorMessage(response);
171172
if (blockErrorMessage) {
172-
console.warn(
173+
logger.warn(
173174
`sendMessageStream() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`
174175
);
175176
}
@@ -182,7 +183,7 @@ export class ChatSession {
182183
if (e.message !== SILENT_ERROR) {
183184
// Users do not have access to _sendPromise to catch errors
184185
// downstream from streamPromise, so they should not throw.
185-
console.error(e);
186+
logger.error(e);
186187
}
187188
});
188189
return streamPromise;

packages/vertexai/src/requests/request.test.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { expect, use } from 'chai';
19-
import { restore, stub } from 'sinon';
19+
import { match, restore, stub } from 'sinon';
2020
import sinonChai from 'sinon-chai';
2121
import chaiAsPromised from 'chai-as-promised';
2222
import { RequestUrl, Task, getHeaders, makeRequest } from './request';
@@ -169,13 +169,18 @@ describe('request methods', () => {
169169
project: 'myproject',
170170
location: 'moon',
171171
getAppCheckToken: () =>
172-
Promise.resolve({ token: 'token', error: Error('oops') })
172+
Promise.resolve({ token: 'dummytoken', error: Error('oops') })
173173
},
174174
true,
175175
{}
176176
);
177+
const warnStub = stub(console, 'warn');
177178
const headers = await getHeaders(fakeUrl);
178-
expect(headers.has('X-Firebase-AppCheck')).to.be.false;
179+
expect(headers.get('X-Firebase-AppCheck')).to.equal('dummytoken');
180+
expect(warnStub).to.be.calledWith(
181+
match(/vertexai/),
182+
match(/App Check.*oops/)
183+
);
179184
});
180185
it('adds auth token if it exists', async () => {
181186
const headers = await getHeaders(fakeUrl);

packages/vertexai/src/requests/request.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
LANGUAGE_TAG,
2525
PACKAGE_VERSION
2626
} from '../constants';
27+
import { logger } from '../logger';
2728

2829
export enum Task {
2930
GENERATE_CONTENT = 'generateContent',
@@ -83,8 +84,13 @@ export async function getHeaders(url: RequestUrl): Promise<Headers> {
8384
headers.append('x-goog-api-key', url.apiSettings.apiKey);
8485
if (url.apiSettings.getAppCheckToken) {
8586
const appCheckToken = await url.apiSettings.getAppCheckToken();
86-
if (appCheckToken && !appCheckToken.error) {
87+
if (appCheckToken) {
8788
headers.append('X-Firebase-AppCheck', appCheckToken.token);
89+
if (appCheckToken.error) {
90+
logger.warn(
91+
`Unable to obtain a valid App Check token: ${appCheckToken.error.message}`
92+
);
93+
}
8894
}
8995
}
9096

packages/vertexai/src/requests/response-helpers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
VertexAIErrorCode
2525
} from '../types';
2626
import { VertexAIError } from '../errors';
27+
import { logger } from '../logger';
2728

2829
/**
2930
* Creates an EnhancedGenerateContentResponse object that has helper functions and
@@ -56,7 +57,7 @@ export function addHelpers(
5657
(response as EnhancedGenerateContentResponse).text = () => {
5758
if (response.candidates && response.candidates.length > 0) {
5859
if (response.candidates.length > 1) {
59-
console.warn(
60+
logger.warn(
6061
`This response had ${response.candidates.length} ` +
6162
`candidates. Returning text from the first candidate only. ` +
6263
`Access response.candidates directly to use the other candidates.`
@@ -88,7 +89,7 @@ export function addHelpers(
8889
(response as EnhancedGenerateContentResponse).functionCalls = () => {
8990
if (response.candidates && response.candidates.length > 0) {
9091
if (response.candidates.length > 1) {
91-
console.warn(
92+
logger.warn(
9293
`This response had ${response.candidates.length} ` +
9394
`candidates. Returning function calls from the first candidate only. ` +
9495
`Access response.candidates directly to use the other candidates.`

0 commit comments

Comments
 (0)