Skip to content

Commit 7b06106

Browse files
committed
Fix all tests
1 parent 87a1b72 commit 7b06106

33 files changed

+213
-244
lines changed

common/api-review/vertexai.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const BackendType: {
2828
};
2929

3030
// @public (undocumented)
31-
export type BackendType = typeof BackendType[keyof typeof BackendType];
31+
export type BackendType = (typeof BackendType)[keyof typeof BackendType];
3232

3333
// @public
3434
export interface BaseParams {
@@ -901,4 +901,6 @@ export interface WebAttribution {
901901
}
902902

903903

904+
// (No @packageDocumentation comment for this package)
905+
904906
```

packages/vertexai/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = {
3030
{
3131
'packageDir': [path.resolve(__dirname, '../../'), __dirname]
3232
}
33-
]
33+
],
34+
'@typescript-eslint/consistent-type-definitions': 0
3435
}
3536
};

packages/vertexai/src/api.test.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
*/
1717
import { ImagenModelParams, ModelParams, GenAIErrorCode } from './types';
1818
import { GenAIError } from './errors';
19-
import { ImagenModel, getGenerativeModel, getImagenModel } from './api';
19+
import { ImagenModel, getGenerativeModel, getImagenModel, vertexAIBackend } from './api';
2020
import { expect } from 'chai';
21-
import { VertexAI } from './public-types';
21+
import { GenAI } from './public-types';
2222
import { GenerativeModel } from './models/generative-model';
2323

24-
const fakeVertexAI: VertexAI = {
24+
const fakeGenAI: GenAI = {
2525
app: {
2626
name: 'DEFAULT',
2727
automaticDataCollectionEnabled: true,
@@ -31,13 +31,14 @@ const fakeVertexAI: VertexAI = {
3131
appId: 'my-appid'
3232
}
3333
},
34+
backend: vertexAIBackend('us-central1'),
3435
location: 'us-central1'
3536
};
3637

3738
describe('Top level API', () => {
3839
it('getGenerativeModel throws if no model is provided', () => {
3940
try {
40-
getGenerativeModel(fakeVertexAI, {} as ModelParams);
41+
getGenerativeModel(fakeGenAI, {} as ModelParams);
4142
} catch (e) {
4243
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_MODEL);
4344
expect((e as GenAIError).message).includes(
@@ -48,9 +49,9 @@ describe('Top level API', () => {
4849
});
4950
it('getGenerativeModel throws if no apiKey is provided', () => {
5051
const fakeVertexNoApiKey = {
51-
...fakeVertexAI,
52+
...fakeGenAI,
5253
app: { options: { projectId: 'my-project', appId: 'my-appid' } }
53-
} as VertexAI;
54+
} as GenAI;
5455
try {
5556
getGenerativeModel(fakeVertexNoApiKey, { model: 'my-model' });
5657
} catch (e) {
@@ -64,15 +65,13 @@ describe('Top level API', () => {
6465
});
6566
it('getGenerativeModel throws if no projectId is provided', () => {
6667
const fakeVertexNoProject = {
67-
...fakeVertexAI,
68+
...fakeGenAI,
6869
app: { options: { apiKey: 'my-key', appId: 'my-appid' } }
69-
} as VertexAI;
70+
} as GenAI;
7071
try {
7172
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' });
7273
} catch (e) {
73-
expect((e as GenAIError).code).includes(
74-
GenAIErrorCode.NO_PROJECT_ID
75-
);
74+
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_PROJECT_ID);
7675
expect((e as GenAIError).message).equals(
7776
`VertexAI: The "projectId" field is empty in the local` +
7877
` Firebase config. Firebase VertexAI requires this field ` +
@@ -82,28 +81,28 @@ describe('Top level API', () => {
8281
});
8382
it('getGenerativeModel throws if no appId is provided', () => {
8483
const fakeVertexNoProject = {
85-
...fakeVertexAI,
84+
...fakeGenAI,
8685
app: { options: { apiKey: 'my-key', projectId: 'my-projectid' } }
87-
} as VertexAI;
86+
} as GenAI;
8887
try {
8988
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' });
9089
} catch (e) {
91-
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_APP_ID);
92-
expect((e as VertexAIError).message).equals(
90+
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_APP_ID);
91+
expect((e as GenAIError).message).equals(
9392
`VertexAI: The "appId" field is empty in the local` +
9493
` Firebase config. Firebase VertexAI requires this field ` +
95-
`to contain a valid app ID. (vertexAI/${VertexAIErrorCode.NO_APP_ID})`
94+
`to contain a valid app ID. (vertexAI/${GenAIErrorCode.NO_APP_ID})`
9695
);
9796
}
9897
});
9998
it('getGenerativeModel gets a GenerativeModel', () => {
100-
const genModel = getGenerativeModel(fakeVertexAI, { model: 'my-model' });
99+
const genModel = getGenerativeModel(fakeGenAI, { model: 'my-model' });
101100
expect(genModel).to.be.an.instanceOf(GenerativeModel);
102101
expect(genModel.model).to.equal('publishers/google/models/my-model');
103102
});
104103
it('getImagenModel throws if no model is provided', () => {
105104
try {
106-
getImagenModel(fakeVertexAI, {} as ImagenModelParams);
105+
getImagenModel(fakeGenAI, {} as ImagenModelParams);
107106
} catch (e) {
108107
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_MODEL);
109108
expect((e as GenAIError).message).includes(
@@ -114,9 +113,9 @@ describe('Top level API', () => {
114113
});
115114
it('getImagenModel throws if no apiKey is provided', () => {
116115
const fakeVertexNoApiKey = {
117-
...fakeVertexAI,
116+
...fakeGenAI,
118117
app: { options: { projectId: 'my-project', appId: 'my-appid' } }
119-
} as VertexAI;
118+
} as GenAI;
120119
try {
121120
getImagenModel(fakeVertexNoApiKey, { model: 'my-model' });
122121
} catch (e) {
@@ -130,15 +129,13 @@ describe('Top level API', () => {
130129
});
131130
it('getImagenModel throws if no projectId is provided', () => {
132131
const fakeVertexNoProject = {
133-
...fakeVertexAI,
132+
...fakeGenAI,
134133
app: { options: { apiKey: 'my-key', appId: 'my-appid' } }
135-
} as VertexAI;
134+
} as GenAI;
136135
try {
137136
getImagenModel(fakeVertexNoProject, { model: 'my-model' });
138137
} catch (e) {
139-
expect((e as GenAIError).code).includes(
140-
GenAIErrorCode.NO_PROJECT_ID
141-
);
138+
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_PROJECT_ID);
142139
expect((e as GenAIError).message).equals(
143140
`VertexAI: The "projectId" field is empty in the local` +
144141
` Firebase config. Firebase VertexAI requires this field ` +
@@ -148,22 +145,22 @@ describe('Top level API', () => {
148145
});
149146
it('getImagenModel throws if no appId is provided', () => {
150147
const fakeVertexNoProject = {
151-
...fakeVertexAI,
148+
...fakeGenAI,
152149
app: { options: { apiKey: 'my-key', projectId: 'my-project' } }
153-
} as VertexAI;
150+
} as GenAI;
154151
try {
155152
getImagenModel(fakeVertexNoProject, { model: 'my-model' });
156153
} catch (e) {
157-
expect((e as VertexAIError).code).includes(VertexAIErrorCode.NO_APP_ID);
158-
expect((e as VertexAIError).message).equals(
154+
expect((e as GenAIError).code).includes(GenAIErrorCode.NO_APP_ID);
155+
expect((e as GenAIError).message).equals(
159156
`VertexAI: The "appId" field is empty in the local` +
160157
` Firebase config. Firebase VertexAI requires this field ` +
161-
`to contain a valid app ID. (vertexAI/${VertexAIErrorCode.NO_APP_ID})`
158+
`to contain a valid app ID. (vertexAI/${GenAIErrorCode.NO_APP_ID})`
162159
);
163160
}
164161
});
165162
it('getImagenModel gets an ImagenModel', () => {
166-
const genModel = getImagenModel(fakeVertexAI, { model: 'my-model' });
163+
const genModel = getImagenModel(fakeGenAI, { model: 'my-model' });
167164
expect(genModel).to.be.an.instanceOf(ImagenModel);
168165
expect(genModel.model).to.equal('publishers/google/models/my-model');
169166
});

packages/vertexai/src/api.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ import { Provider } from '@firebase/component';
2020
import { getModularInstance } from '@firebase/util';
2121
import { DEFAULT_LOCATION, GENAI_TYPE } from './constants';
2222
import { GenAIService } from './service';
23-
import { BackendType, GenAI, GenAIOptions, GoogleAIBackend, VertexAI, VertexAIBackend, VertexAIOptions } from './public-types';
23+
import {
24+
BackendType,
25+
GenAI,
26+
GenAIOptions,
27+
GoogleAIBackend,
28+
VertexAI,
29+
VertexAIBackend,
30+
VertexAIOptions
31+
} from './public-types';
2432
import {
2533
ImagenModelParams,
2634
ModelParams,
@@ -38,7 +46,7 @@ export { GenAIModel, GenerativeModel, ImagenModel, GenAIError };
3846

3947
// Temporary aliases from new 'GenAI' names to the original 'VertexAI' names.
4048
// These will be removed in v12 of the SDK.
41-
export {
49+
export {
4250
GenAIModel as VertexAIModel,
4351
GenAIError as VertexAIError,
4452
GenAIErrorCode as VertexAIErrorCode
@@ -54,7 +62,7 @@ declare module '@firebase/component' {
5462
* Returns a <code>{@link VertexAI}</code> instance for the given app.
5563
*
5664
* @public
57-
*
65+
*
5866
* @param app - The {@link @firebase/app#FirebaseApp} to use.
5967
*/
6068
export function getVertexAI(
@@ -67,15 +75,15 @@ export function getVertexAI(
6775

6876
const identifier = encodeInstanceIdentifier({
6977
backendType: BackendType.VERTEX_AI,
70-
location: options?.location ?? ""
78+
location: options?.location ?? ''
7179
});
7280
return genAIProvider.getImmediate({
7381
identifier
7482
});
7583
}
7684

7785
/**
78-
* Returns a <code>{@link VertexAI}</code> instance for the given app that has the Developer
86+
* Returns a <code>{@link GenAI}</code> instance for the given app that has the Developer
7987
* API enabled.
8088
*
8189
* @public
@@ -90,9 +98,7 @@ export function getGenAI(
9098
// Dependencies
9199
const genAIProvider: Provider<'genAI'> = _getProvider(app, GENAI_TYPE);
92100

93-
const identifier = encodeInstanceIdentifier(
94-
options.backend
95-
);
101+
const identifier = encodeInstanceIdentifier(options.backend);
96102
return genAIProvider.getImmediate({
97103
identifier
98104
});
@@ -111,7 +117,7 @@ export function vertexAIBackend(location?: string): VertexAIBackend {
111117
backendType: BackendType.VERTEX_AI,
112118
location: location ?? DEFAULT_LOCATION
113119
};
114-
120+
115121
return backend;
116122
}
117123

packages/vertexai/src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export const VERTEX_TYPE = 'vertexAI';
2323
export const GENAI_TYPE = 'genAI';
2424

2525
export const DEFAULT_INSTANCE_IDENTIFER: InstanceIdentifier = {
26-
backendType: BackendType.GOOGLE_AI,
27-
}
26+
backendType: BackendType.GOOGLE_AI
27+
};
2828

2929
export const DEFAULT_LOCATION = 'us-central1';
3030

packages/vertexai/src/developerAPI.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export function mapGenerateContentResponse(
110110
*/
111111
export function mapCountTokensRequest(
112112
countTokensRequest: CountTokensRequest,
113-
model: string
114113
): DeveloperAPICountTokensRequest {
115114
const mappedCountTokensRequest: DeveloperAPICountTokensRequest = {
116115
generateContentRequest: {

packages/vertexai/src/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { VERTEX_TYPE } from './constants';
2121

2222
/**
2323
* Error class for the Vertex AI in Firebase SDK.
24-
*
24+
*
2525
* @public
2626
*/
2727
export class GenAIError extends FirebaseError {
@@ -63,4 +63,4 @@ export class GenAIError extends FirebaseError {
6363
// Since Error is an interface, we don't inherit toString and so we define it ourselves.
6464
this.toString = () => fullMessage;
6565
}
66-
}
66+
}

packages/vertexai/src/helpers.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,27 @@ import { GenAIErrorCode } from './types';
2424
* @internal
2525
*/
2626
export function encodeInstanceIdentifier(
27-
instanceIdentifier: InstanceIdentifier,
27+
instanceIdentifier: InstanceIdentifier
2828
): string {
29-
switch(instanceIdentifier.backendType) {
29+
switch (instanceIdentifier.backendType) {
3030
case BackendType.VERTEX_AI:
3131
return `genai/vertexai/${location || DEFAULT_LOCATION}`;
3232
case BackendType.GOOGLE_AI:
33-
return 'genai/googleai'
33+
return 'genai/googleai';
3434
default:
35-
throw new GenAIError(GenAIErrorCode.ERROR, `An internal error occured: Unknown Backend ${instanceIdentifier}. Please submit an issue at https://github.com/firebase/firebase-js-sdk.`)
35+
throw new GenAIError(
36+
GenAIErrorCode.ERROR,
37+
`An internal error occured: Unknown Backend ${instanceIdentifier}. Please submit an issue at https://github.com/firebase/firebase-js-sdk.`
38+
);
3639
}
3740
}
3841

3942
/**
4043
* @internal
4144
*/
42-
export function decodeInstanceIdentifier(instanceIdentifier: string): InstanceIdentifier {
45+
export function decodeInstanceIdentifier(
46+
instanceIdentifier: string
47+
): InstanceIdentifier {
4348
const identifierParts = instanceIdentifier.split('/');
4449
const backend = identifierParts[1];
4550
switch (backend) {
@@ -51,7 +56,7 @@ export function decodeInstanceIdentifier(instanceIdentifier: string): InstanceId
5156
};
5257
case 'googleai':
5358
return {
54-
backendType: BackendType.GOOGLE_AI,
59+
backendType: BackendType.GOOGLE_AI
5560
};
5661
default:
5762
throw new GenAIError(

packages/vertexai/src/index.node.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,16 @@ function registerGenAI(): void {
4141

4242
let instanceIdentifier: InstanceIdentifier;
4343
if (options.instanceIdentifier) {
44-
instanceIdentifier = decodeInstanceIdentifier(options.instanceIdentifier);
44+
instanceIdentifier = decodeInstanceIdentifier(
45+
options.instanceIdentifier
46+
);
4547
} else {
4648
instanceIdentifier = DEFAULT_INSTANCE_IDENTIFER;
4749
}
4850

4951
const backend = instanceIdentifier;
5052

51-
return new GenAIService(
52-
app,
53-
backend,
54-
auth,
55-
appCheckProvider,
56-
);
53+
return new GenAIService(app, backend, auth, appCheckProvider);
5754
},
5855
ComponentType.PUBLIC
5956
).setMultipleInstances(true)

0 commit comments

Comments
 (0)