@firebase/ai 2.1.0-20250806231852 → 2.1.0-canary.02280d747

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,17 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
 
7
+ import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
7
8
  import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
9
+ import { ComponentContainer } from '@firebase/component';
8
10
  import { FirebaseApp } from '@firebase/app';
11
+ import { FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
12
+ import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
13
+ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
9
14
  import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
10
15
  import { FirebaseError } from '@firebase/util';
16
+ import { InstanceFactoryOptions } from '@firebase/component';
17
+ import { Provider } from '@firebase/component';
11
18
 
12
19
  /**
13
20
  * An instance of the Firebase AI SDK.
@@ -27,6 +34,10 @@ export declare interface AI {
27
34
  * Vertex AI Gemini API (using {@link VertexAIBackend}).
28
35
  */
29
36
  backend: Backend;
37
+ /**
38
+ * Options applied to this {@link AI} instance.
39
+ */
40
+ options?: AIOptions;
30
41
  /**
31
42
  * @deprecated use `AI.backend.location` instead.
32
43
  *
@@ -125,8 +136,26 @@ export declare abstract class AIModel {
125
136
  export declare interface AIOptions {
126
137
  /**
127
138
  * The backend configuration to use for the AI service instance.
139
+ * Defaults to the Gemini Developer API backend ({@link GoogleAIBackend}).
140
+ */
141
+ backend?: Backend;
142
+ /**
143
+ * Whether to use App Check limited use tokens. Defaults to false.
128
144
  */
145
+ useLimitedUseAppCheckTokens?: boolean;
146
+ }
147
+
148
+ declare class AIService implements AI, _FirebaseService {
149
+ app: FirebaseApp;
129
150
  backend: Backend;
151
+ auth: FirebaseAuthInternal | null;
152
+ appCheck: FirebaseAppCheckInternal | null;
153
+ _options?: Omit<AIOptions, 'backend'>;
154
+ location: string;
155
+ constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>);
156
+ _delete(): Promise<void>;
157
+ set options(optionsToSet: AIOptions);
158
+ get options(): AIOptions | undefined;
130
159
  }
131
160
 
132
161
  /**
@@ -320,16 +349,18 @@ export declare interface ChromeAdapter {
320
349
  /**
321
350
  * Generates content using on-device inference.
322
351
  *
323
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating
324
- * content using in-cloud inference.</p>
352
+ * @remarks
353
+ * This is comparable to {@link GenerativeModel.generateContent} for generating
354
+ * content using in-cloud inference.
325
355
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
326
356
  */
327
357
  generateContent(request: GenerateContentRequest): Promise<Response>;
328
358
  /**
329
359
  * Generates a content stream using on-device inference.
330
360
  *
331
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating
332
- * a content stream using in-cloud inference.</p>
361
+ * @remarks
362
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating
363
+ * a content stream using in-cloud inference.
333
364
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
334
365
  */
335
366
  generateContentStream(request: GenerateContentRequest): Promise<Response>;
@@ -485,6 +516,8 @@ export declare interface ErrorDetails {
485
516
  [key: string]: unknown;
486
517
  }
487
518
 
519
+ export declare function factory(container: ComponentContainer, { instanceIdentifier }: InstanceFactoryOptions): AIService;
520
+
488
521
  /**
489
522
  * Data pointing to a file uploaded on Google Cloud Storage.
490
523
  * @public
@@ -555,6 +588,8 @@ export declare const FinishReason: {
555
588
  */
556
589
  export declare type FinishReason = (typeof FinishReason)[keyof typeof FinishReason];
557
590
 
591
+ /* Excluded from this release type: _FirebaseService */
592
+
558
593
  /**
559
594
  * A predicted {@link FunctionCall} returned from the model
560
595
  * that contains a string representing the {@link FunctionDeclaration.name}
@@ -1904,7 +1939,10 @@ export declare interface RequestOptions {
1904
1939
  */
1905
1940
  timeout?: number;
1906
1941
  /**
1907
- * Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com
1942
+ * Base url for endpoint. Defaults to
1943
+ * https://firebasevertexai.googleapis.com, which is the
1944
+ * {@link https://console.cloud.google.com/apis/library/firebasevertexai.googleapis.com?project=_ | Firebase AI Logic API}
1945
+ * (used regardless of your chosen Gemini API provider).
1908
1946
  */
1909
1947
  baseUrl?: string;
1910
1948
  }
package/dist/ai.d.ts CHANGED
@@ -4,10 +4,18 @@
4
4
  * @packageDocumentation
5
5
  */
6
6
 
7
+ import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
7
8
  import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
9
+ import { ComponentContainer } from '@firebase/component';
8
10
  import { FirebaseApp } from '@firebase/app';
11
+ import { FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
12
+ import { FirebaseAuthInternal } from '@firebase/auth-interop-types';
13
+ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
9
14
  import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
10
15
  import { FirebaseError } from '@firebase/util';
16
+ import { _FirebaseService } from '@firebase/app';
17
+ import { InstanceFactoryOptions } from '@firebase/component';
18
+ import { Provider } from '@firebase/component';
11
19
 
12
20
  /**
13
21
  * An instance of the Firebase AI SDK.
@@ -27,6 +35,10 @@ export declare interface AI {
27
35
  * Vertex AI Gemini API (using {@link VertexAIBackend}).
28
36
  */
29
37
  backend: Backend;
38
+ /**
39
+ * Options applied to this {@link AI} instance.
40
+ */
41
+ options?: AIOptions;
30
42
  /**
31
43
  * @deprecated use `AI.backend.location` instead.
32
44
  *
@@ -111,7 +123,7 @@ export declare abstract class AIModel {
111
123
  /**
112
124
  * @internal
113
125
  */
114
- protected _apiSettings: ApiSettings;
126
+ _apiSettings: ApiSettings;
115
127
  /**
116
128
  * Constructs a new instance of the {@link AIModel} class.
117
129
  *
@@ -159,8 +171,26 @@ export declare abstract class AIModel {
159
171
  export declare interface AIOptions {
160
172
  /**
161
173
  * The backend configuration to use for the AI service instance.
174
+ * Defaults to the Gemini Developer API backend ({@link GoogleAIBackend}).
175
+ */
176
+ backend?: Backend;
177
+ /**
178
+ * Whether to use App Check limited use tokens. Defaults to false.
162
179
  */
180
+ useLimitedUseAppCheckTokens?: boolean;
181
+ }
182
+
183
+ declare class AIService implements AI, _FirebaseService {
184
+ app: FirebaseApp;
163
185
  backend: Backend;
186
+ auth: FirebaseAuthInternal | null;
187
+ appCheck: FirebaseAppCheckInternal | null;
188
+ _options?: Omit<AIOptions, 'backend'>;
189
+ location: string;
190
+ constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>);
191
+ _delete(): Promise<void>;
192
+ set options(optionsToSet: AIOptions);
193
+ get options(): AIOptions | undefined;
164
194
  }
165
195
 
166
196
  /**
@@ -360,16 +390,18 @@ export declare interface ChromeAdapter {
360
390
  /**
361
391
  * Generates content using on-device inference.
362
392
  *
363
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating
364
- * content using in-cloud inference.</p>
393
+ * @remarks
394
+ * This is comparable to {@link GenerativeModel.generateContent} for generating
395
+ * content using in-cloud inference.
365
396
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
366
397
  */
367
398
  generateContent(request: GenerateContentRequest): Promise<Response>;
368
399
  /**
369
400
  * Generates a content stream using on-device inference.
370
401
  *
371
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating
372
- * a content stream using in-cloud inference.</p>
402
+ * @remarks
403
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating
404
+ * a content stream using in-cloud inference.
373
405
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
374
406
  */
375
407
  generateContentStream(request: GenerateContentRequest): Promise<Response>;
@@ -528,6 +560,8 @@ export declare interface ErrorDetails {
528
560
  [key: string]: unknown;
529
561
  }
530
562
 
563
+ export declare function factory(container: ComponentContainer, { instanceIdentifier }: InstanceFactoryOptions): AIService;
564
+
531
565
  /**
532
566
  * Data pointing to a file uploaded on Google Cloud Storage.
533
567
  * @public
@@ -2003,7 +2037,10 @@ export declare interface RequestOptions {
2003
2037
  */
2004
2038
  timeout?: number;
2005
2039
  /**
2006
- * Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com
2040
+ * Base url for endpoint. Defaults to
2041
+ * https://firebasevertexai.googleapis.com, which is the
2042
+ * {@link https://console.cloud.google.com/apis/library/firebasevertexai.googleapis.com?project=_ | Firebase AI Logic API}
2043
+ * (used regardless of your chosen Gemini API provider).
2007
2044
  */
2008
2045
  baseUrl?: string;
2009
2046
  }
@@ -4,7 +4,7 @@ import { FirebaseError, getModularInstance } from '@firebase/util';
4
4
  import { Logger } from '@firebase/logger';
5
5
 
6
6
  var name = "@firebase/ai";
7
- var version = "2.1.0-20250806231852";
7
+ var version = "2.1.0-canary.02280d747";
8
8
 
9
9
  /**
10
10
  * @license
@@ -653,6 +653,12 @@ class AIService {
653
653
  _delete() {
654
654
  return Promise.resolve();
655
655
  }
656
+ set options(optionsToSet) {
657
+ this._options = optionsToSet;
658
+ }
659
+ get options() {
660
+ return this._options;
661
+ }
656
662
  }
657
663
 
658
664
  /**
@@ -837,7 +843,12 @@ class AIModel {
837
843
  };
838
844
  }
839
845
  else if (ai.appCheck) {
840
- this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
846
+ if (ai.options?.useLimitedUseAppCheckTokens) {
847
+ this._apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
848
+ }
849
+ else {
850
+ this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
851
+ }
841
852
  }
842
853
  if (ai.auth) {
843
854
  this._apiSettings.getAuthToken = () => ai.auth.getToken();
@@ -2369,17 +2380,17 @@ class ChromeAdapterImpl {
2369
2380
  /**
2370
2381
  * Checks if a given request can be made on-device.
2371
2382
  *
2372
- * <ol>Encapsulates a few concerns:
2373
- * <li>the mode</li>
2374
- * <li>API existence</li>
2375
- * <li>prompt formatting</li>
2376
- * <li>model availability, including triggering download if necessary</li>
2377
- * </ol>
2383
+ * Encapsulates a few concerns:
2384
+ * the mode
2385
+ * API existence
2386
+ * prompt formatting
2387
+ * model availability, including triggering download if necessary
2388
+ *
2378
2389
  *
2379
- * <p>Pros: callers needn't be concerned with details of on-device availability.</p>
2380
- * <p>Cons: this method spans a few concerns and splits request validation from usage.
2390
+ * Pros: callers needn't be concerned with details of on-device availability.</p>
2391
+ * Cons: this method spans a few concerns and splits request validation from usage.
2381
2392
  * If instance variables weren't already part of the API, we could consider a better
2382
- * separation of concerns.</p>
2393
+ * separation of concerns.
2383
2394
  */
2384
2395
  async isAvailable(request) {
2385
2396
  if (!this.mode) {
@@ -2420,8 +2431,9 @@ class ChromeAdapterImpl {
2420
2431
  /**
2421
2432
  * Generates content on device.
2422
2433
  *
2423
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating content in
2424
- * Cloud.</p>
2434
+ * @remarks
2435
+ * This is comparable to {@link GenerativeModel.generateContent} for generating content in
2436
+ * Cloud.
2425
2437
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
2426
2438
  * @returns {@link Response}, so we can reuse common response formatting.
2427
2439
  */
@@ -2434,8 +2446,9 @@ class ChromeAdapterImpl {
2434
2446
  /**
2435
2447
  * Generates content stream on device.
2436
2448
  *
2437
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
2438
- * Cloud.</p>
2449
+ * @remarks
2450
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
2451
+ * Cloud.
2439
2452
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
2440
2453
  * @returns {@link Response}, so we can reuse common response formatting.
2441
2454
  */
@@ -2486,11 +2499,11 @@ class ChromeAdapterImpl {
2486
2499
  /**
2487
2500
  * Triggers out-of-band download of an on-device model.
2488
2501
  *
2489
- * <p>Chrome only downloads models as needed. Chrome knows a model is needed when code calls
2490
- * LanguageModel.create.</p>
2502
+ * Chrome only downloads models as needed. Chrome knows a model is needed when code calls
2503
+ * LanguageModel.create.
2491
2504
  *
2492
- * <p>Since Chrome manages the download, the SDK can only avoid redundant download requests by
2493
- * tracking if a download has previously been requested.</p>
2505
+ * Since Chrome manages the download, the SDK can only avoid redundant download requests by
2506
+ * tracking if a download has previously been requested.
2494
2507
  */
2495
2508
  download() {
2496
2509
  if (this.isDownloading) {
@@ -2544,12 +2557,12 @@ class ChromeAdapterImpl {
2544
2557
  /**
2545
2558
  * Abstracts Chrome session creation.
2546
2559
  *
2547
- * <p>Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
2560
+ * Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
2548
2561
  * inference. To map the Firebase AI API to Chrome's API, the SDK creates a new session for all
2549
- * inference.</p>
2562
+ * inference.
2550
2563
  *
2551
- * <p>Chrome will remove a model from memory if it's no longer in use, so this method ensures a
2552
- * new session is created before an old session is destroyed.</p>
2564
+ * Chrome will remove a model from memory if it's no longer in use, so this method ensures a
2565
+ * new session is created before an old session is destroyed.
2553
2566
  */
2554
2567
  async createSession() {
2555
2568
  if (!this.languageModelProvider) {
@@ -2957,14 +2970,20 @@ class ImagenImageFormat {
2957
2970
  *
2958
2971
  * @public
2959
2972
  */
2960
- function getAI(app = getApp(), options = { backend: new GoogleAIBackend() }) {
2973
+ function getAI(app = getApp(), options) {
2961
2974
  app = getModularInstance(app);
2962
2975
  // Dependencies
2963
2976
  const AIProvider = _getProvider(app, AI_TYPE);
2964
- const identifier = encodeInstanceIdentifier(options.backend);
2965
- return AIProvider.getImmediate({
2977
+ const backend = options?.backend ?? new GoogleAIBackend();
2978
+ const finalOptions = {
2979
+ useLimitedUseAppCheckTokens: options?.useLimitedUseAppCheckTokens ?? false
2980
+ };
2981
+ const identifier = encodeInstanceIdentifier(backend);
2982
+ const aiInstance = AIProvider.getImmediate({
2966
2983
  identifier
2967
2984
  });
2985
+ aiInstance.options = finalOptions;
2986
+ return aiInstance;
2968
2987
  }
2969
2988
  /**
2970
2989
  * Returns a {@link GenerativeModel} class with methods for inference
@@ -3020,23 +3039,24 @@ function getImagenModel(ai, modelParams, requestOptions) {
3020
3039
  *
3021
3040
  * @packageDocumentation
3022
3041
  */
3042
+ function factory(container, { instanceIdentifier }) {
3043
+ if (!instanceIdentifier) {
3044
+ throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
3045
+ }
3046
+ const backend = decodeInstanceIdentifier(instanceIdentifier);
3047
+ // getImmediate for FirebaseApp will always succeed
3048
+ const app = container.getProvider('app').getImmediate();
3049
+ const auth = container.getProvider('auth-internal');
3050
+ const appCheckProvider = container.getProvider('app-check-internal');
3051
+ return new AIService(app, backend, auth, appCheckProvider);
3052
+ }
3023
3053
  function registerAI() {
3024
- _registerComponent(new Component(AI_TYPE, (container, { instanceIdentifier }) => {
3025
- if (!instanceIdentifier) {
3026
- throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
3027
- }
3028
- const backend = decodeInstanceIdentifier(instanceIdentifier);
3029
- // getImmediate for FirebaseApp will always succeed
3030
- const app = container.getProvider('app').getImmediate();
3031
- const auth = container.getProvider('auth-internal');
3032
- const appCheckProvider = container.getProvider('app-check-internal');
3033
- return new AIService(app, backend, auth, appCheckProvider);
3034
- }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3054
+ _registerComponent(new Component(AI_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3035
3055
  registerVersion(name, version);
3036
3056
  // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
3037
3057
  registerVersion(name, version, 'esm2020');
3038
3058
  }
3039
3059
  registerAI();
3040
3060
 
3041
- export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, IntegerSchema, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel };
3061
+ export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, IntegerSchema, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, factory, getAI, getGenerativeModel, getImagenModel };
3042
3062
  //# sourceMappingURL=index.esm.js.map