Skip to content

Commit d53e7c7

Browse files
refactor: make ai dependency on vertexai
1 parent cde4fae commit d53e7c7

File tree

12 files changed

+178
-213
lines changed

12 files changed

+178
-213
lines changed

packages/ai/lib/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { version } from './version';
1919

2020
export const AI_TYPE = 'AI';
2121

22-
export const VERTEX_TYPE = 'vertexAI';
23-
2422
export const DEFAULT_LOCATION = 'us-central1';
2523

2624
export const DEFAULT_BASE_URL = 'https://firebasevertexai.googleapis.com';

packages/ai/lib/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { AI_TYPE } from './constants';
2626
*/
2727
export class AIError extends FirebaseError {
2828
/**
29-
* Constructs a new instance of the `VertexAIError` class.
29+
* Constructs a new instance of the `AIError` class.
3030
*
31-
* @param code - The error code from <code>{@link VertexAIErrorCode}</code>.
31+
* @param code - The error code from <code>{@link AIErrorCode}</code>.
3232
* @param message - A human-readable message describing the error.
3333
* @param customErrorData - Optional error data.
3434
*/

packages/ai/lib/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ import './polyfills';
1919
import { getApp, ReactNativeFirebase } from '@react-native-firebase/app';
2020
import { GoogleAIBackend, VertexAIBackend } from './backend';
2121
import { AIErrorCode, ModelParams, RequestOptions } from './types';
22-
import { AI, AIOptions, VertexAI, VertexAIOptions } from './public-types';
22+
import { AI, AIOptions } from './public-types';
2323
import { AIError } from './errors';
2424
import { GenerativeModel } from './models/generative-model';
25-
export { ChatSession } from './methods/chat-session';
25+
import { AIModel } from './models/ai-model';
2626

27+
export * from './public-types';
28+
export { ChatSession } from './methods/chat-session';
2729
export * from './requests/schema-builder';
28-
export * from './types';
29-
export * from './backend';
30-
31-
export { GenerativeModel, AIError, VertexAI, VertexAIOptions };
30+
export { GoogleAIBackend, VertexAIBackend } from './backend';
31+
export { GenerativeModel, AIError, AIModel };
3232

3333
/**
3434
* Returns the default {@link AI} instance that is associated with the provided

packages/ai/lib/public-types.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,6 @@ import { FirebaseAppCheckTypes } from '@react-native-firebase/app-check';
2121

2222
export * from './types';
2323

24-
/**
25-
* An instance of the Vertex AI in Firebase SDK.
26-
* @public
27-
*/
28-
export interface VertexAI {
29-
/**
30-
* The {@link @firebase/app#FirebaseApp} this <code>{@link VertexAI}</code> instance is associated with.
31-
*/
32-
app: ReactNativeFirebase.FirebaseApp;
33-
location: string;
34-
appCheck?: FirebaseAppCheckTypes.Module | null;
35-
auth?: FirebaseAuthTypes.Module | null;
36-
}
37-
38-
/**
39-
* Options when initializing the Vertex AI in Firebase SDK.
40-
* @public
41-
*/
42-
export interface VertexAIOptions {
43-
location?: string;
44-
appCheck?: FirebaseAppCheckTypes.Module | null;
45-
auth?: FirebaseAuthTypes.Module | null;
46-
}
47-
4824
/**
4925
* Options for initializing the AI service using {@link getAI | getAI()}.
5026
* This allows specifying which backend to use (Vertex AI Gemini API or Gemini Developer API)
@@ -142,6 +118,8 @@ export interface AI {
142118
* The {@link @firebase/app#FirebaseApp} this {@link AI} instance is associated with.
143119
*/
144120
app: ReactNativeFirebase.FirebaseApp;
121+
appCheck?: FirebaseAppCheckTypes.Module | null;
122+
auth?: FirebaseAuthTypes.Module | null;
145123
/**
146124
* A {@link Backend} instance that specifies the configuration for the target backend,
147125
* either the Gemini Developer API (using {@link GoogleAIBackend}) or the
@@ -155,27 +133,3 @@ export interface AI {
155133
*/
156134
location: string;
157135
}
158-
159-
/**
160-
* An instance of the Vertex AI in Firebase SDK.
161-
* @public
162-
*/
163-
export interface VertexAI {
164-
/**
165-
* The {@link @firebase/app#FirebaseApp} this <code>{@link VertexAI}</code> instance is associated with.
166-
*/
167-
app: ReactNativeFirebase.FirebaseApp;
168-
location: string;
169-
appCheck?: FirebaseAppCheckTypes.Module | null;
170-
auth?: FirebaseAuthTypes.Module | null;
171-
}
172-
173-
/**
174-
* Options when initializing the Vertex AI in Firebase SDK.
175-
* @public
176-
*/
177-
export interface VertexAIOptions {
178-
location?: string;
179-
appCheck?: FirebaseAppCheckTypes.Module | null;
180-
auth?: FirebaseAuthTypes.Module | null;
181-
}

packages/ai/lib/types/error.ts

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError } from '@firebase/util';
1918
import { GenerateContentResponse } from './responses';
20-
import { VERTEX_TYPE } from '../constants';
2119

2220
/**
2321
* Details object that may be included in an error response.
@@ -104,75 +102,3 @@ export const enum AIErrorCode {
104102
/** An error occurred due an attempt to use an unsupported feature. */
105103
UNSUPPORTED = 'unsupported',
106104
}
107-
108-
/**
109-
* Standardized error codes that <code>{@link VertexAIError}</code> can have.
110-
*
111-
* @public
112-
*/
113-
export const enum VertexAIErrorCode {
114-
/** A generic error occurred. */
115-
ERROR = 'error',
116-
117-
/** An error occurred in a request. */
118-
REQUEST_ERROR = 'request-error',
119-
120-
/** An error occurred in a response. */
121-
RESPONSE_ERROR = 'response-error',
122-
123-
/** An error occurred while performing a fetch. */
124-
FETCH_ERROR = 'fetch-error',
125-
126-
/** An error associated with a Content object. */
127-
INVALID_CONTENT = 'invalid-content',
128-
129-
/** An error due to the Firebase API not being enabled in the Console. */
130-
API_NOT_ENABLED = 'api-not-enabled',
131-
132-
/** An error due to invalid Schema input. */
133-
INVALID_SCHEMA = 'invalid-schema',
134-
135-
/** An error occurred due to a missing Firebase API key. */
136-
NO_API_KEY = 'no-api-key',
137-
138-
/** An error occurred due to a model name not being specified during initialization. */
139-
NO_MODEL = 'no-model',
140-
141-
/** An error occurred due to a missing project ID. */
142-
NO_PROJECT_ID = 'no-project-id',
143-
144-
/** An error occurred while parsing. */
145-
PARSE_FAILED = 'parse-failed',
146-
}
147-
148-
/**
149-
* Error class for the Vertex AI in Firebase SDK.
150-
*
151-
* @public
152-
*/
153-
export class VertexAIError extends FirebaseError {
154-
/**
155-
* Constructs a new instance of the `VertexAIError` class.
156-
*
157-
* @param code - The error code from <code>{@link VertexAIErrorCode}</code>.
158-
* @param message - A human-readable message describing the error.
159-
* @param customErrorData - Optional error data.
160-
*/
161-
constructor(
162-
readonly code: VertexAIErrorCode,
163-
message: string,
164-
readonly customErrorData?: CustomErrorData,
165-
) {
166-
// Match error format used by FirebaseError from ErrorFactory
167-
const service = VERTEX_TYPE;
168-
const serviceName = 'VertexAI';
169-
const fullCode = `${service}/${code}`;
170-
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
171-
super(code, fullMessage);
172-
173-
Object.setPrototypeOf(this, VertexAIError.prototype);
174-
175-
// Since Error is an interface, we don't inherit toString and so we define it ourselves.
176-
this.toString = () => fullMessage;
177-
}
178-
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @license
3+
* Copyright 2025 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+
import { describe, expect, it } from '@jest/globals';
18+
import {
19+
AIError,
20+
AIModel,
21+
GenerativeModel,
22+
VertexAIError,
23+
VertexAIErrorCode,
24+
VertexAIModel,
25+
VertexAI,
26+
getGenerativeModel,
27+
} from '../lib/index';
28+
import { AI, AIErrorCode } from '@react-native-firebase/ai';
29+
import { VertexAIBackend } from '@react-native-firebase/ai';
30+
import { ReactNativeFirebase } from '@react-native-firebase/app';
31+
32+
function assertAssignable<T, _U extends T>(): void {}
33+
34+
const fakeAI: AI = {
35+
app: {
36+
name: 'DEFAULT',
37+
automaticDataCollectionEnabled: true,
38+
options: {
39+
apiKey: 'key',
40+
projectId: 'my-project',
41+
appId: 'app-id',
42+
},
43+
} as ReactNativeFirebase.FirebaseApp,
44+
backend: new VertexAIBackend('us-central1'),
45+
location: 'us-central1',
46+
};
47+
48+
const fakeVertexAI: VertexAI = fakeAI;
49+
50+
describe('backwards-compatible types', function () {
51+
it('AI is backwards compatible with VertexAI', function () {
52+
assertAssignable<VertexAI, AI>();
53+
});
54+
55+
it('AIError is backwards compatible with VertexAIError', function () {
56+
assertAssignable<typeof VertexAIError, typeof AIError>();
57+
const err = new VertexAIError(VertexAIErrorCode.ERROR, '');
58+
expect(err).toBeInstanceOf(AIError);
59+
expect(err).toBeInstanceOf(VertexAIError);
60+
});
61+
62+
it('AIErrorCode is backwards compatible with VertexAIErrorCode', () => {
63+
assertAssignable<VertexAIErrorCode, AIErrorCode>();
64+
const errCode = AIErrorCode.ERROR;
65+
expect(errCode).toBe(VertexAIErrorCode.ERROR);
66+
});
67+
68+
it('AIModel is backwards compatible with VertexAIModel', () => {
69+
assertAssignable<typeof VertexAIModel, typeof AIModel>();
70+
71+
const model = new GenerativeModel(fakeAI, { model: 'model-name' });
72+
expect(model).toBeInstanceOf(AIModel);
73+
expect(model).toBeInstanceOf(VertexAIModel);
74+
});
75+
76+
describe('backward-compatible functions', () => {
77+
it('getGenerativeModel', () => {
78+
const model = getGenerativeModel(fakeVertexAI, { model: 'model-name' });
79+
expect(model).toBeInstanceOf(AIModel);
80+
expect(model).toBeInstanceOf(VertexAIModel);
81+
});
82+
});
83+
});

packages/vertexai/lib/ai-symlink

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vertexai/lib/index.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
* limitations under the License.
1616
*/
1717

18-
import './polyfills';
1918
import { getApp, ReactNativeFirebase } from '@react-native-firebase/app';
20-
import {
21-
getGenerativeModel as getGenerativeModelFromAI,
22-
getAI,
23-
VertexAIBackend,
24-
GenerativeModel,
25-
RequestOptions,
26-
ModelParams,
27-
VertexAIErrorCode,
28-
VertexAIError,
29-
VertexAI,
30-
VertexAIOptions,
31-
} from './ai-symlink/index';
32-
import { DEFAULT_LOCATION } from './ai-symlink/constants';
33-
import { VertexAIService } from './service';
19+
import { VertexAIBackend, AIModel, AIError, AIErrorCode } from '@react-native-firebase/ai';
20+
import { VertexAIOptions, VertexAI } from './public-types';
21+
export * from './public-types';
22+
export * from '@react-native-firebase/ai';
23+
24+
const DEFAULT_LOCATION = 'us-central1';
3425

3526
/**
27+
* @deprecated Use the new {@link getAI | getAI()} instead. The Vertex AI in Firebase SDK has been
28+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
29+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
30+
*
31+
* Returns a {@link VertexAI} instance for the given app, configured to use the
32+
* Vertex AI Gemini API. This instance will be
33+
* configured to use the Vertex AI Gemini API.
34+
*
3635
* Returns a <code>{@link VertexAI}</code> instance for the given app.
3736
*
3837
* @public
@@ -51,30 +50,31 @@ export function getVertexAI(
5150
location: options?.location || DEFAULT_LOCATION,
5251
appCheck: options?.appCheck || null,
5352
auth: options?.auth || null,
54-
} as VertexAIService;
53+
backend: new VertexAIBackend(options?.location || DEFAULT_LOCATION),
54+
};
5555
}
5656

5757
/**
58-
* Returns a <code>{@link GenerativeModel}</code> class with methods for inference
59-
* and other functionality.
58+
* @deprecated Use the new {@link AIModel} instead. The Vertex AI in Firebase SDK has been
59+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
60+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
61+
*
62+
* Base class for Firebase AI model APIs.
6063
*
6164
* @public
6265
*/
63-
export function getGenerativeModel(
64-
vertexAI: VertexAI,
65-
modelParams: ModelParams,
66-
requestOptions?: RequestOptions,
67-
): GenerativeModel {
68-
if (!modelParams.model) {
69-
throw new VertexAIError(
70-
VertexAIErrorCode.NO_MODEL,
71-
`Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`,
72-
);
73-
}
66+
export const VertexAIModel = AIModel;
7467

75-
const ai = getAI(vertexAI.app, {
76-
backend: new VertexAIBackend(vertexAI.location),
77-
});
68+
/**
69+
* @deprecated Use the new {@link AIError} instead. The Vertex AI in Firebase SDK has been
70+
* replaced with the Firebase AI SDK to accommodate the evolving set of supported features and
71+
* services. For migration details, see the {@link https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk | migration guide}.
72+
*
73+
* Error class for the Firebase AI SDK.
74+
*
75+
* @public
76+
*/
77+
export const VertexAIError = AIError;
7878

79-
return getGenerativeModelFromAI(ai, modelParams, requestOptions);
80-
}
79+
export { AIErrorCode as VertexAIErrorCode };
80+
export { VertexAIBackend, AIModel, AIError };

0 commit comments

Comments
 (0)