Skip to content

Commit 01a112b

Browse files
authored
Add appcheck header (#294)
1 parent 6561882 commit 01a112b

File tree

11 files changed

+269
-85
lines changed

11 files changed

+269
-85
lines changed

packages/vertexai/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { FirebaseApp, getApp, _getProvider } from '@firebase/app';
1919
import { Provider } from '@firebase/component';
2020
import { getModularInstance } from '@firebase/util';
2121
import { DEFAULT_LOCATION, VERTEX_TYPE } from './constants';
22-
import { VertexService } from './factory';
22+
import { VertexService } from './service';
2323
import { Vertex, VertexOptions } from './public-types';
2424
import { ERROR_FACTORY, VertexError } from './errors';
2525
import { ModelParams, RequestOptions } from './types';

packages/vertexai/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
import { registerVersion, _registerComponent } from '@firebase/app';
25-
import { factory } from './factory';
25+
import { VertexService } from './service';
2626
import { VERTEX_TYPE } from './constants';
2727
import { Component, ComponentType } from '@firebase/component';
2828
import { name, version } from '../package.json';
@@ -41,7 +41,7 @@ function registerVertex(): void {
4141
// getImmediate for FirebaseApp will always succeed
4242
const app = container.getProvider('app').getImmediate();
4343
const appCheckProvider = container.getProvider('app-check-internal');
44-
return factory(app, appCheckProvider, { location });
44+
return new VertexService(app, appCheckProvider, { location });
4545
},
4646
ComponentType.PUBLIC
4747
).setMultipleInstances(true)

packages/vertexai/src/methods/count-tokens.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
CountTokensResponse,
2121
RequestOptions
2222
} from '../types';
23-
import { RequestUrl, Task, makeRequest } from '../requests/request';
23+
import { Task, makeRequest } from '../requests/request';
2424
import { ApiSettings } from '../types/internal';
2525

2626
export async function countTokens(
@@ -29,15 +29,11 @@ export async function countTokens(
2929
params: CountTokensRequest,
3030
requestOptions?: RequestOptions
3131
): Promise<CountTokensResponse> {
32-
const url = new RequestUrl(
32+
const response = await makeRequest(
3333
model,
3434
Task.COUNT_TOKENS,
3535
apiSettings,
3636
false,
37-
requestOptions
38-
);
39-
const response = await makeRequest(
40-
url,
4137
JSON.stringify(params),
4238
requestOptions
4339
);

packages/vertexai/src/methods/generate-content.test.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
HarmCategory
2929
} from '../types';
3030
import { ApiSettings } from '../types/internal';
31+
import { Task } from '../requests/request';
3132

3233
use(sinonChai);
3334
use(chaiAsPromised);
@@ -69,10 +70,14 @@ describe('generateContent()', () => {
6970
);
7071
expect(result.response.text()).to.include('Helena');
7172
expect(makeRequestStub).to.be.calledWith(
72-
match.instanceOf(request.RequestUrl),
73+
'model',
74+
Task.GENERATE_CONTENT,
75+
fakeApiSettings,
76+
false,
7377
match((value: string) => {
7478
return value.includes('contents');
75-
})
79+
}),
80+
undefined
7681
);
7782
});
7883
it('long response', async () => {
@@ -88,7 +93,10 @@ describe('generateContent()', () => {
8893
expect(result.response.text()).to.include('Use Freshly Ground Coffee');
8994
expect(result.response.text()).to.include('30 minutes of brewing');
9095
expect(makeRequestStub).to.be.calledWith(
91-
match.instanceOf(request.RequestUrl),
96+
'model',
97+
Task.GENERATE_CONTENT,
98+
fakeApiSettings,
99+
false,
92100
match.any
93101
);
94102
});
@@ -107,7 +115,10 @@ describe('generateContent()', () => {
107115
result.response.candidates?.[0].citationMetadata?.citations.length
108116
).to.equal(1);
109117
expect(makeRequestStub).to.be.calledWith(
110-
match.instanceOf(request.RequestUrl),
118+
'model',
119+
Task.GENERATE_CONTENT,
120+
fakeApiSettings,
121+
false,
111122
match.any
112123
);
113124
});
@@ -125,7 +136,10 @@ describe('generateContent()', () => {
125136
);
126137
expect(result.response.text).to.throw('SAFETY');
127138
expect(makeRequestStub).to.be.calledWith(
128-
match.instanceOf(request.RequestUrl),
139+
'model',
140+
Task.GENERATE_CONTENT,
141+
fakeApiSettings,
142+
false,
129143
match.any
130144
);
131145
});
@@ -143,7 +157,10 @@ describe('generateContent()', () => {
143157
);
144158
expect(result.response.text).to.throw('SAFETY');
145159
expect(makeRequestStub).to.be.calledWith(
146-
match.instanceOf(request.RequestUrl),
160+
'model',
161+
Task.GENERATE_CONTENT,
162+
fakeApiSettings,
163+
false,
147164
match.any
148165
);
149166
});
@@ -159,7 +176,10 @@ describe('generateContent()', () => {
159176
);
160177
expect(result.response.text()).to.equal('');
161178
expect(makeRequestStub).to.be.calledWith(
162-
match.instanceOf(request.RequestUrl),
179+
'model',
180+
Task.GENERATE_CONTENT,
181+
fakeApiSettings,
182+
false,
163183
match.any
164184
);
165185
});
@@ -175,7 +195,10 @@ describe('generateContent()', () => {
175195
);
176196
expect(result.response.text()).to.include('30 minutes of brewing');
177197
expect(makeRequestStub).to.be.calledWith(
178-
match.instanceOf(request.RequestUrl),
198+
'model',
199+
Task.GENERATE_CONTENT,
200+
fakeApiSettings,
201+
false,
179202
match.any
180203
);
181204
});

packages/vertexai/src/methods/generate-content.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
GenerateContentStreamResult,
2323
RequestOptions
2424
} from '../types';
25-
import { RequestUrl, Task, makeRequest } from '../requests/request';
25+
import { Task, makeRequest } from '../requests/request';
2626
import { addHelpers } from '../requests/response-helpers';
2727
import { processStream } from '../requests/stream-reader';
2828
import { ApiSettings } from '../types/internal';
@@ -33,15 +33,11 @@ export async function generateContentStream(
3333
params: GenerateContentRequest,
3434
requestOptions?: RequestOptions
3535
): Promise<GenerateContentStreamResult> {
36-
const url = new RequestUrl(
36+
const response = await makeRequest(
3737
model,
3838
Task.STREAM_GENERATE_CONTENT,
3939
apiSettings,
4040
/* stream */ true,
41-
requestOptions
42-
);
43-
const response = await makeRequest(
44-
url,
4541
JSON.stringify(params),
4642
requestOptions
4743
);
@@ -54,15 +50,11 @@ export async function generateContent(
5450
params: GenerateContentRequest,
5551
requestOptions?: RequestOptions
5652
): Promise<GenerateContentResult> {
57-
const url = new RequestUrl(
53+
const response = await makeRequest(
5854
model,
5955
Task.GENERATE_CONTENT,
6056
apiSettings,
6157
/* stream */ false,
62-
requestOptions
63-
);
64-
const response = await makeRequest(
65-
url,
6658
JSON.stringify(params),
6759
requestOptions
6860
);

packages/vertexai/src/models/generative-model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { formatGenerateContentInput } from '../requests/request-helpers';
3939
import { Vertex } from '../public-types';
4040
import { ERROR_FACTORY, VertexError } from '../errors';
4141
import { ApiSettings } from '../types/internal';
42+
import { VertexService } from '../service';
4243

4344
/**
4445
* Class for generative model APIs.
@@ -67,6 +68,10 @@ export class GenerativeModel {
6768
project: vertex.app.options.projectId,
6869
location: vertex.location
6970
};
71+
if ((vertex as VertexService).appCheck) {
72+
this._apiSettings.getAppCheckToken = () =>
73+
(vertex as VertexService).appCheck!.getToken();
74+
}
7075
}
7176
if (modelParams.model.includes('/')) {
7277
if (modelParams.model.startsWith('models/')) {

0 commit comments

Comments
 (0)