Skip to content

Commit 7b3b299

Browse files
refactor: remove vertexai code and wrap around firebase ai package
1 parent d3d06ab commit 7b3b299

27 files changed

+107
-2719
lines changed

packages/ai/lib/index.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +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 } from './public-types';
22+
import { AI, AIOptions, VertexAI, VertexAIOptions } from './public-types';
2323
import { AIError } from './errors';
2424
import { GenerativeModel } from './models/generative-model';
2525
export { ChatSession } from './methods/chat-session';
26+
2627
export * from './requests/schema-builder';
28+
export * from './types';
29+
export * from './backend';
2730

28-
export {
29-
GenerativeModel,
30-
AIError,
31-
AIErrorCode,
32-
ModelParams,
33-
RequestOptions,
34-
GoogleAIBackend,
35-
VertexAIBackend,
36-
};
31+
export { GenerativeModel, AIError, VertexAI, VertexAIOptions };
3732

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

packages/ai/lib/public-types.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,27 @@ export interface AI {
155155
*/
156156
location: string;
157157
}
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,75 @@ export const enum AIErrorCode {
102102
/** An error occurred due an attempt to use an unsupported feature. */
103103
UNSUPPORTED = 'unsupported',
104104
}
105+
106+
/**
107+
* Standardized error codes that <code>{@link VertexAIError}</code> can have.
108+
*
109+
* @public
110+
*/
111+
export const enum VertexAIErrorCode {
112+
/** A generic error occurred. */
113+
ERROR = 'error',
114+
115+
/** An error occurred in a request. */
116+
REQUEST_ERROR = 'request-error',
117+
118+
/** An error occurred in a response. */
119+
RESPONSE_ERROR = 'response-error',
120+
121+
/** An error occurred while performing a fetch. */
122+
FETCH_ERROR = 'fetch-error',
123+
124+
/** An error associated with a Content object. */
125+
INVALID_CONTENT = 'invalid-content',
126+
127+
/** An error due to the Firebase API not being enabled in the Console. */
128+
API_NOT_ENABLED = 'api-not-enabled',
129+
130+
/** An error due to invalid Schema input. */
131+
INVALID_SCHEMA = 'invalid-schema',
132+
133+
/** An error occurred due to a missing Firebase API key. */
134+
NO_API_KEY = 'no-api-key',
135+
136+
/** An error occurred due to a model name not being specified during initialization. */
137+
NO_MODEL = 'no-model',
138+
139+
/** An error occurred due to a missing project ID. */
140+
NO_PROJECT_ID = 'no-project-id',
141+
142+
/** An error occurred while parsing. */
143+
PARSE_FAILED = 'parse-failed',
144+
}
145+
146+
/**
147+
* Error class for the Vertex AI in Firebase SDK.
148+
*
149+
* @public
150+
*/
151+
export class VertexAIError extends FirebaseError {
152+
/**
153+
* Constructs a new instance of the `VertexAIError` class.
154+
*
155+
* @param code - The error code from <code>{@link VertexAIErrorCode}</code>.
156+
* @param message - A human-readable message describing the error.
157+
* @param customErrorData - Optional error data.
158+
*/
159+
constructor(
160+
readonly code: VertexAIErrorCode,
161+
message: string,
162+
readonly customErrorData?: CustomErrorData,
163+
) {
164+
// Match error format used by FirebaseError from ErrorFactory
165+
const service = VERTEX_TYPE;
166+
const serviceName = 'VertexAI';
167+
const fullCode = `${service}/${code}`;
168+
const fullMessage = `${serviceName}: ${message} (${fullCode})`;
169+
super(code, fullMessage);
170+
171+
Object.setPrototypeOf(this, VertexAIError.prototype);
172+
173+
// Since Error is an interface, we don't inherit toString and so we define it ourselves.
174+
this.toString = () => fullMessage;
175+
}
176+
}

packages/vertexai/lib/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import { version } from './version';
1919

20-
export const VERTEX_TYPE = 'vertexAI';
21-
2220
export const DEFAULT_LOCATION = 'us-central1';
2321

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

packages/vertexai/lib/errors.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/vertexai/lib/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ import {
2424
GenerativeModel,
2525
RequestOptions,
2626
ModelParams,
27+
VertexAIErrorCode,
28+
VertexAIError,
29+
VertexAI,
30+
VertexAIOptions,
2731
} from '@react-native-firebase/ai';
28-
import { VertexAIErrorCode } from './types';
2932
import { DEFAULT_LOCATION } from './constants';
30-
import { VertexAI, VertexAIOptions } from './public-types';
31-
import { VertexAIError } from './errors';
3233
import { VertexAIService } from './service';
33-
export { ChatSession } from './methods/chat-session';
34-
export * from './requests/schema-builder';
3534

36-
export { VertexAIError };
35+
export * from '@react-native-firebase/ai';
3736

3837
/**
3938
* Returns a <code>{@link VertexAI}</code> instance for the given app.

packages/vertexai/lib/methods/chat-session-helpers.ts

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)