Skip to content
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

Add X-Firebase-AppId header to VertexAI requests #8809

Merged
merged 7 commits into from
Mar 25, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only send if data collection is on
  • Loading branch information
dlarocque committed Feb 25, 2025
commit 767cd7717b8487852a0aaf8dac1daca13e64c501
2 changes: 2 additions & 0 deletions packages/vertexai/src/models/vertexai-model.ts
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ export abstract class VertexAIModel {
apiKey: vertexAI.app.options.apiKey,
project: vertexAI.app.options.projectId,
appId: vertexAI.app.options.appId,
automaticDataCollectionEnabled:
vertexAI.app.automaticDataCollectionEnabled,
location: vertexAI.location
};

44 changes: 44 additions & 0 deletions packages/vertexai/src/requests/request.test.ts
Original file line number Diff line number Diff line change
@@ -126,6 +126,50 @@ describe('request methods', () => {
const headers = await getHeaders(fakeUrl);
expect(headers.get('x-goog-api-key')).to.equal('key');
});
it('adds app id if automatedDataCollectionEnabled is undefined', async () => {
const headers = await getHeaders(fakeUrl);
expect(headers.get('X-Firebase-AppId')).to.equal('my-appid');
});
it('adds app id if automatedDataCollectionEnabled is true', async () => {
const fakeApiSettings: ApiSettings = {
apiKey: 'key',
project: 'myproject',
appId: 'my-appid',
location: 'moon',
automaticDataCollectionEnabled: true,
getAuthToken: () => Promise.resolve({ accessToken: 'authtoken' }),
getAppCheckToken: () => Promise.resolve({ token: 'appchecktoken' })
};
const fakeUrl = new RequestUrl(
'models/model-name',
Task.GENERATE_CONTENT,
fakeApiSettings,
true,
{}
);
const headers = await getHeaders(fakeUrl);
expect(headers.get('X-Firebase-AppId')).to.equal('my-appid');
});
it('does not add app id if automatedDataCollectionEnabled is false', async () => {
const fakeApiSettings: ApiSettings = {
apiKey: 'key',
project: 'myproject',
appId: 'my-appid',
location: 'moon',
automaticDataCollectionEnabled: false,
getAuthToken: () => Promise.resolve({ accessToken: 'authtoken' }),
getAppCheckToken: () => Promise.resolve({ token: 'appchecktoken' })
};
const fakeUrl = new RequestUrl(
'models/model-name',
Task.GENERATE_CONTENT,
fakeApiSettings,
true,
{}
);
const headers = await getHeaders(fakeUrl);
expect(headers.get('X-Firebase-AppId')).to.be.null;
});
it('adds app check token if it exists', async () => {
const headers = await getHeaders(fakeUrl);
expect(headers.get('X-Firebase-AppCheck')).to.equal('appchecktoken');
4 changes: 3 additions & 1 deletion packages/vertexai/src/requests/request.ts
Original file line number Diff line number Diff line change
@@ -84,7 +84,9 @@ export async function getHeaders(url: RequestUrl): Promise<Headers> {
headers.append('Content-Type', 'application/json');
headers.append('x-goog-api-client', getClientHeaders());
headers.append('x-goog-api-key', url.apiSettings.apiKey);
headers.append('X-Firebase-AppId', url.apiSettings.appId); // Will be converted to 'X-Firebase-Appid' before it's sent in the browser.
if (url.apiSettings.automaticDataCollectionEnabled !== false) {
headers.append('X-Firebase-AppId', url.apiSettings.appId);
}
if (url.apiSettings.getAppCheckToken) {
const appCheckToken = await url.apiSettings.getAppCheckToken();
if (appCheckToken) {
1 change: 1 addition & 0 deletions packages/vertexai/src/types/internal.ts
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ export interface ApiSettings {
project: string;
appId: string;
location: string;
automaticDataCollectionEnabled?: boolean;
getAuthToken?: () => Promise<FirebaseAuthTokenData | null>;
getAppCheckToken?: () => Promise<AppCheckTokenResult>;
}
Loading