@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.
@@ -3,10 +3,13 @@
3
3
  *
4
4
  * @packageDocumentation
5
5
  */
6
+ import { AIService } from './service';
7
+ import { ComponentContainer, InstanceFactoryOptions } from '@firebase/component';
6
8
  declare global {
7
9
  interface Window {
8
10
  [key: string]: unknown;
9
11
  }
10
12
  }
13
+ export declare function factory(container: ComponentContainer, { instanceIdentifier }: InstanceFactoryOptions): AIService;
11
14
  export * from './api';
12
15
  export * from './public-types';
@@ -34,24 +34,25 @@ export declare class ChromeAdapterImpl implements ChromeAdapter {
34
34
  /**
35
35
  * Checks if a given request can be made on-device.
36
36
  *
37
- * <ol>Encapsulates a few concerns:
38
- * <li>the mode</li>
39
- * <li>API existence</li>
40
- * <li>prompt formatting</li>
41
- * <li>model availability, including triggering download if necessary</li>
42
- * </ol>
37
+ * Encapsulates a few concerns:
38
+ * the mode
39
+ * API existence
40
+ * prompt formatting
41
+ * model availability, including triggering download if necessary
43
42
  *
44
- * <p>Pros: callers needn't be concerned with details of on-device availability.</p>
45
- * <p>Cons: this method spans a few concerns and splits request validation from usage.
43
+ *
44
+ * Pros: callers needn't be concerned with details of on-device availability.</p>
45
+ * Cons: this method spans a few concerns and splits request validation from usage.
46
46
  * If instance variables weren't already part of the API, we could consider a better
47
- * separation of concerns.</p>
47
+ * separation of concerns.
48
48
  */
49
49
  isAvailable(request: GenerateContentRequest): Promise<boolean>;
50
50
  /**
51
51
  * Generates content on device.
52
52
  *
53
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating content in
54
- * Cloud.</p>
53
+ * @remarks
54
+ * This is comparable to {@link GenerativeModel.generateContent} for generating content in
55
+ * Cloud.
55
56
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
56
57
  * @returns {@link Response}, so we can reuse common response formatting.
57
58
  */
@@ -59,8 +60,9 @@ export declare class ChromeAdapterImpl implements ChromeAdapter {
59
60
  /**
60
61
  * Generates content stream on device.
61
62
  *
62
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
63
- * Cloud.</p>
63
+ * @remarks
64
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
65
+ * Cloud.
64
66
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
65
67
  * @returns {@link Response}, so we can reuse common response formatting.
66
68
  */
@@ -77,11 +79,11 @@ export declare class ChromeAdapterImpl implements ChromeAdapter {
77
79
  /**
78
80
  * Triggers out-of-band download of an on-device model.
79
81
  *
80
- * <p>Chrome only downloads models as needed. Chrome knows a model is needed when code calls
81
- * LanguageModel.create.</p>
82
+ * Chrome only downloads models as needed. Chrome knows a model is needed when code calls
83
+ * LanguageModel.create.
82
84
  *
83
- * <p>Since Chrome manages the download, the SDK can only avoid redundant download requests by
84
- * tracking if a download has previously been requested.</p>
85
+ * Since Chrome manages the download, the SDK can only avoid redundant download requests by
86
+ * tracking if a download has previously been requested.
85
87
  */
86
88
  private download;
87
89
  /**
@@ -99,12 +101,12 @@ export declare class ChromeAdapterImpl implements ChromeAdapter {
99
101
  /**
100
102
  * Abstracts Chrome session creation.
101
103
  *
102
- * <p>Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
104
+ * Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
103
105
  * inference. To map the Firebase AI API to Chrome's API, the SDK creates a new session for all
104
- * inference.</p>
106
+ * inference.
105
107
  *
106
- * <p>Chrome will remove a model from memory if it's no longer in use, so this method ensures a
107
- * new session is created before an old session is destroyed.</p>
108
+ * Chrome will remove a model from memory if it's no longer in use, so this method ensures a
109
+ * new session is created before an old session is destroyed.
108
110
  */
109
111
  private createSession;
110
112
  /**
@@ -33,7 +33,7 @@ export declare abstract class AIModel {
33
33
  /**
34
34
  * @internal
35
35
  */
36
- protected _apiSettings: ApiSettings;
36
+ _apiSettings: ApiSettings;
37
37
  /**
38
38
  * Constructs a new instance of the {@link AIModel} class.
39
39
  *
@@ -35,6 +35,10 @@ export interface AI {
35
35
  * Vertex AI Gemini API (using {@link VertexAIBackend}).
36
36
  */
37
37
  backend: Backend;
38
+ /**
39
+ * Options applied to this {@link AI} instance.
40
+ */
41
+ options?: AIOptions;
38
42
  /**
39
43
  * @deprecated use `AI.backend.location` instead.
40
44
  *
@@ -83,6 +87,11 @@ export type BackendType = (typeof BackendType)[keyof typeof BackendType];
83
87
  export interface AIOptions {
84
88
  /**
85
89
  * The backend configuration to use for the AI service instance.
90
+ * Defaults to the Gemini Developer API backend ({@link GoogleAIBackend}).
86
91
  */
87
- backend: Backend;
92
+ backend?: Backend;
93
+ /**
94
+ * Whether to use App Check limited use tokens. Defaults to false.
95
+ */
96
+ useLimitedUseAppCheckTokens?: boolean;
88
97
  }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { FirebaseApp, _FirebaseService } from '@firebase/app';
18
- import { AI } from './public-types';
18
+ import { AI, AIOptions } from './public-types';
19
19
  import { AppCheckInternalComponentName, FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
20
20
  import { Provider } from '@firebase/component';
21
21
  import { FirebaseAuthInternal, FirebaseAuthInternalName } from '@firebase/auth-interop-types';
@@ -25,7 +25,10 @@ export declare class AIService implements AI, _FirebaseService {
25
25
  backend: Backend;
26
26
  auth: FirebaseAuthInternal | null;
27
27
  appCheck: FirebaseAppCheckInternal | null;
28
+ _options?: Omit<AIOptions, 'backend'>;
28
29
  location: string;
29
30
  constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>);
30
31
  _delete(): Promise<void>;
32
+ set options(optionsToSet: AIOptions);
33
+ get options(): AIOptions | undefined;
31
34
  }
@@ -34,16 +34,18 @@ export interface ChromeAdapter {
34
34
  /**
35
35
  * Generates content using on-device inference.
36
36
  *
37
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating
38
- * content using in-cloud inference.</p>
37
+ * @remarks
38
+ * This is comparable to {@link GenerativeModel.generateContent} for generating
39
+ * content using in-cloud inference.
39
40
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
40
41
  */
41
42
  generateContent(request: GenerateContentRequest): Promise<Response>;
42
43
  /**
43
44
  * Generates a content stream using on-device inference.
44
45
  *
45
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating
46
- * a content stream using in-cloud inference.</p>
46
+ * @remarks
47
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating
48
+ * a content stream using in-cloud inference.
47
49
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
48
50
  */
49
51
  generateContentStream(request: GenerateContentRequest): Promise<Response>;
@@ -146,7 +146,10 @@ export interface RequestOptions {
146
146
  */
147
147
  timeout?: number;
148
148
  /**
149
- * Base url for endpoint. Defaults to https://firebasevertexai.googleapis.com
149
+ * Base url for endpoint. Defaults to
150
+ * https://firebasevertexai.googleapis.com, which is the
151
+ * {@link https://console.cloud.google.com/apis/library/firebasevertexai.googleapis.com?project=_ | Firebase AI Logic API}
152
+ * (used regardless of your chosen Gemini API provider).
150
153
  */
151
154
  baseUrl?: string;
152
155
  }
package/dist/index.cjs.js CHANGED
@@ -8,7 +8,7 @@ var util = require('@firebase/util');
8
8
  var logger$1 = require('@firebase/logger');
9
9
 
10
10
  var name = "@firebase/ai";
11
- var version = "2.1.0-20250806231852";
11
+ var version = "2.1.0-canary.02280d747";
12
12
 
13
13
  /**
14
14
  * @license
@@ -657,6 +657,12 @@ class AIService {
657
657
  _delete() {
658
658
  return Promise.resolve();
659
659
  }
660
+ set options(optionsToSet) {
661
+ this._options = optionsToSet;
662
+ }
663
+ get options() {
664
+ return this._options;
665
+ }
660
666
  }
661
667
 
662
668
  /**
@@ -841,7 +847,12 @@ class AIModel {
841
847
  };
842
848
  }
843
849
  else if (ai.appCheck) {
844
- this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
850
+ if (ai.options?.useLimitedUseAppCheckTokens) {
851
+ this._apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
852
+ }
853
+ else {
854
+ this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
855
+ }
845
856
  }
846
857
  if (ai.auth) {
847
858
  this._apiSettings.getAuthToken = () => ai.auth.getToken();
@@ -2373,17 +2384,17 @@ class ChromeAdapterImpl {
2373
2384
  /**
2374
2385
  * Checks if a given request can be made on-device.
2375
2386
  *
2376
- * <ol>Encapsulates a few concerns:
2377
- * <li>the mode</li>
2378
- * <li>API existence</li>
2379
- * <li>prompt formatting</li>
2380
- * <li>model availability, including triggering download if necessary</li>
2381
- * </ol>
2387
+ * Encapsulates a few concerns:
2388
+ * the mode
2389
+ * API existence
2390
+ * prompt formatting
2391
+ * model availability, including triggering download if necessary
2392
+ *
2382
2393
  *
2383
- * <p>Pros: callers needn't be concerned with details of on-device availability.</p>
2384
- * <p>Cons: this method spans a few concerns and splits request validation from usage.
2394
+ * Pros: callers needn't be concerned with details of on-device availability.</p>
2395
+ * Cons: this method spans a few concerns and splits request validation from usage.
2385
2396
  * If instance variables weren't already part of the API, we could consider a better
2386
- * separation of concerns.</p>
2397
+ * separation of concerns.
2387
2398
  */
2388
2399
  async isAvailable(request) {
2389
2400
  if (!this.mode) {
@@ -2424,8 +2435,9 @@ class ChromeAdapterImpl {
2424
2435
  /**
2425
2436
  * Generates content on device.
2426
2437
  *
2427
- * <p>This is comparable to {@link GenerativeModel.generateContent} for generating content in
2428
- * Cloud.</p>
2438
+ * @remarks
2439
+ * This is comparable to {@link GenerativeModel.generateContent} for generating content in
2440
+ * Cloud.
2429
2441
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
2430
2442
  * @returns {@link Response}, so we can reuse common response formatting.
2431
2443
  */
@@ -2438,8 +2450,9 @@ class ChromeAdapterImpl {
2438
2450
  /**
2439
2451
  * Generates content stream on device.
2440
2452
  *
2441
- * <p>This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
2442
- * Cloud.</p>
2453
+ * @remarks
2454
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
2455
+ * Cloud.
2443
2456
  * @param request - a standard Firebase AI {@link GenerateContentRequest}
2444
2457
  * @returns {@link Response}, so we can reuse common response formatting.
2445
2458
  */
@@ -2490,11 +2503,11 @@ class ChromeAdapterImpl {
2490
2503
  /**
2491
2504
  * Triggers out-of-band download of an on-device model.
2492
2505
  *
2493
- * <p>Chrome only downloads models as needed. Chrome knows a model is needed when code calls
2494
- * LanguageModel.create.</p>
2506
+ * Chrome only downloads models as needed. Chrome knows a model is needed when code calls
2507
+ * LanguageModel.create.
2495
2508
  *
2496
- * <p>Since Chrome manages the download, the SDK can only avoid redundant download requests by
2497
- * tracking if a download has previously been requested.</p>
2509
+ * Since Chrome manages the download, the SDK can only avoid redundant download requests by
2510
+ * tracking if a download has previously been requested.
2498
2511
  */
2499
2512
  download() {
2500
2513
  if (this.isDownloading) {
@@ -2548,12 +2561,12 @@ class ChromeAdapterImpl {
2548
2561
  /**
2549
2562
  * Abstracts Chrome session creation.
2550
2563
  *
2551
- * <p>Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
2564
+ * Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
2552
2565
  * inference. To map the Firebase AI API to Chrome's API, the SDK creates a new session for all
2553
- * inference.</p>
2566
+ * inference.
2554
2567
  *
2555
- * <p>Chrome will remove a model from memory if it's no longer in use, so this method ensures a
2556
- * new session is created before an old session is destroyed.</p>
2568
+ * Chrome will remove a model from memory if it's no longer in use, so this method ensures a
2569
+ * new session is created before an old session is destroyed.
2557
2570
  */
2558
2571
  async createSession() {
2559
2572
  if (!this.languageModelProvider) {
@@ -2961,14 +2974,20 @@ class ImagenImageFormat {
2961
2974
  *
2962
2975
  * @public
2963
2976
  */
2964
- function getAI(app$1 = app.getApp(), options = { backend: new GoogleAIBackend() }) {
2977
+ function getAI(app$1 = app.getApp(), options) {
2965
2978
  app$1 = util.getModularInstance(app$1);
2966
2979
  // Dependencies
2967
2980
  const AIProvider = app._getProvider(app$1, AI_TYPE);
2968
- const identifier = encodeInstanceIdentifier(options.backend);
2969
- return AIProvider.getImmediate({
2981
+ const backend = options?.backend ?? new GoogleAIBackend();
2982
+ const finalOptions = {
2983
+ useLimitedUseAppCheckTokens: options?.useLimitedUseAppCheckTokens ?? false
2984
+ };
2985
+ const identifier = encodeInstanceIdentifier(backend);
2986
+ const aiInstance = AIProvider.getImmediate({
2970
2987
  identifier
2971
2988
  });
2989
+ aiInstance.options = finalOptions;
2990
+ return aiInstance;
2972
2991
  }
2973
2992
  /**
2974
2993
  * Returns a {@link GenerativeModel} class with methods for inference
@@ -3024,18 +3043,19 @@ function getImagenModel(ai, modelParams, requestOptions) {
3024
3043
  *
3025
3044
  * @packageDocumentation
3026
3045
  */
3046
+ function factory(container, { instanceIdentifier }) {
3047
+ if (!instanceIdentifier) {
3048
+ throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
3049
+ }
3050
+ const backend = decodeInstanceIdentifier(instanceIdentifier);
3051
+ // getImmediate for FirebaseApp will always succeed
3052
+ const app = container.getProvider('app').getImmediate();
3053
+ const auth = container.getProvider('auth-internal');
3054
+ const appCheckProvider = container.getProvider('app-check-internal');
3055
+ return new AIService(app, backend, auth, appCheckProvider);
3056
+ }
3027
3057
  function registerAI() {
3028
- app._registerComponent(new component.Component(AI_TYPE, (container, { instanceIdentifier }) => {
3029
- if (!instanceIdentifier) {
3030
- throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
3031
- }
3032
- const backend = decodeInstanceIdentifier(instanceIdentifier);
3033
- // getImmediate for FirebaseApp will always succeed
3034
- const app = container.getProvider('app').getImmediate();
3035
- const auth = container.getProvider('auth-internal');
3036
- const appCheckProvider = container.getProvider('app-check-internal');
3037
- return new AIService(app, backend, auth, appCheckProvider);
3038
- }, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3058
+ app._registerComponent(new component.Component(AI_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
3039
3059
  app.registerVersion(name, version);
3040
3060
  // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
3041
3061
  app.registerVersion(name, version, 'cjs2020');
@@ -3077,6 +3097,7 @@ exports.Schema = Schema;
3077
3097
  exports.SchemaType = SchemaType;
3078
3098
  exports.StringSchema = StringSchema;
3079
3099
  exports.VertexAIBackend = VertexAIBackend;
3100
+ exports.factory = factory;
3080
3101
  exports.getAI = getAI;
3081
3102
  exports.getGenerativeModel = getGenerativeModel;
3082
3103
  exports.getImagenModel = getImagenModel;