@firebase/ai 2.12.0 → 2.13.0-20260526192810

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,7 +4,7 @@ import { FirebaseError, Deferred, getModularInstance } from '@firebase/util';
4
4
  import { Logger } from '@firebase/logger';
5
5
 
6
6
  var name = "@firebase/ai";
7
- var version = "2.12.0";
7
+ var version = "2.13.0-20260526192810";
8
8
 
9
9
  /**
10
10
  * @license
@@ -443,7 +443,7 @@ const ResponseModality = {
443
443
  * cloud-hosted model. If not available, the SDK will fall back to an
444
444
  * on-device model.
445
445
  *
446
- * @beta
446
+ * @public
447
447
  */
448
448
  const InferenceMode = {
449
449
  'PREFER_ON_DEVICE': 'prefer_on_device',
@@ -454,7 +454,7 @@ const InferenceMode = {
454
454
  /**
455
455
  * Indicates whether inference happened on-device or in-cloud.
456
456
  *
457
- * @beta
457
+ * @public
458
458
  */
459
459
  const InferenceSource = {
460
460
  'ON_DEVICE': 'on_device',
@@ -2127,7 +2127,9 @@ async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCa
2127
2127
  };
2128
2128
  }
2129
2129
  catch (e) {
2130
- if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
2130
+ if (e instanceof AIError &&
2131
+ errorsCausingFallback.includes(e.code) &&
2132
+ (await chromeAdapter.isAvailable(request))) {
2131
2133
  return {
2132
2134
  response: await onDeviceCall(),
2133
2135
  inferenceSource: InferenceSource.ON_DEVICE
@@ -2454,7 +2456,9 @@ class ChatSessionBase {
2454
2456
  else {
2455
2457
  formattedContent = formatNewContent(request);
2456
2458
  }
2457
- const formattedRequest = this._formatRequest(formattedContent, tempHistory);
2459
+ const formattedRequest = this._formatRequest(formattedContent, [
2460
+ ...tempHistory
2461
+ ]);
2458
2462
  tempHistory.push(formattedContent);
2459
2463
  const result = await this._callGenerateContent(formattedRequest, singleRequestOptions);
2460
2464
  if (result) {
@@ -2524,8 +2528,10 @@ class ChatSessionBase {
2524
2528
  else {
2525
2529
  formattedContent = formatNewContent(request);
2526
2530
  }
2531
+ const formattedRequest = this._formatRequest(formattedContent, [
2532
+ ...tempHistory
2533
+ ]);
2527
2534
  tempHistory.push(formattedContent);
2528
- const formattedRequest = this._formatRequest(formattedContent, tempHistory);
2529
2535
  result = await this._callGenerateContentStream(formattedRequest, singleRequestOptions);
2530
2536
  functionCalls = this._getCallableFunctionCalls(result.firstValue);
2531
2537
  if (functionCalls &&
@@ -2881,6 +2887,17 @@ async function countTokens(apiSettings, model, params, chromeAdapter, requestOpt
2881
2887
  return countTokensOnCloud(apiSettings, model, params, requestOptions);
2882
2888
  }
2883
2889
 
2890
+ /**
2891
+ * @internal
2892
+ */
2893
+ var Availability;
2894
+ (function (Availability) {
2895
+ Availability["UNAVAILABLE"] = "unavailable";
2896
+ Availability["DOWNLOADABLE"] = "downloadable";
2897
+ Availability["DOWNLOADING"] = "downloading";
2898
+ Availability["AVAILABLE"] = "available";
2899
+ })(Availability || (Availability = {}));
2900
+
2884
2901
  /**
2885
2902
  * @license
2886
2903
  * Copyright 2024 Google LLC
@@ -2913,6 +2930,52 @@ class GenerativeModel extends AIModel {
2913
2930
  this.systemInstruction = formatSystemInstruction(modelParams.systemInstruction);
2914
2931
  this.requestOptions = requestOptions || {};
2915
2932
  }
2933
+ /**
2934
+ * Initializes on-device models.
2935
+ *
2936
+ * @remarks
2937
+ * This may trigger a download on first
2938
+ * use. Wait for this promise to complete before calling inference
2939
+ * methods if you want to ensure the device models are ready before
2940
+ * any calls. Calling inference methods before the device is ready
2941
+ * will result in a cloud fallback if `inferenceMode` is set to
2942
+ * `PREFER_ON_DEVICE`, and an error if set to `ONLY_ON_DEVICE`.
2943
+ *
2944
+ * IMPORTANT: This call must be made on or after a user has interacted
2945
+ * with the page (for example, through a button click or key press).
2946
+ * If it is called without a user interaction, and it requires a download,
2947
+ * this will cause an error.
2948
+ *
2949
+ * See the
2950
+ * {@link https://developer.chrome.com/docs/ai/prompt-api#use_the_prompt_api | Prompt API docs }
2951
+ * for more details on this requirement.
2952
+ *
2953
+ * @param onDownloadProgress A callback called repeatedly as the
2954
+ * download progresses that provides a `progressValue` between 0
2955
+ * and 1 representing how much of the download is complete. This
2956
+ * will be ignored if `monitor` was populated in
2957
+ * {@link LanguageModelCreateOptions}.
2958
+ *
2959
+ * @public
2960
+ */
2961
+ async initializeDeviceModel(onDownloadProgress) {
2962
+ if (!this.chromeAdapter ||
2963
+ this.chromeAdapter.mode === InferenceMode.ONLY_IN_CLOUD) {
2964
+ return;
2965
+ }
2966
+ const availability = await this.chromeAdapter.downloadIfAvailable(onDownloadProgress);
2967
+ if (availability === Availability.UNAVAILABLE) {
2968
+ const notEnabledError = new AIError(AIErrorCode.API_NOT_ENABLED, 'Local LanguageModel API not available in this environment.');
2969
+ if (this.chromeAdapter.mode === InferenceMode.ONLY_ON_DEVICE) {
2970
+ throw notEnabledError;
2971
+ }
2972
+ else {
2973
+ // No reason to throw if not in ONLY_ON_DEVICE mode.
2974
+ logger.debug(notEnabledError.message);
2975
+ }
2976
+ }
2977
+ await this.chromeAdapter.downloadPromise;
2978
+ }
2916
2979
  /**
2917
2980
  * Makes a single non-streaming call to the model
2918
2981
  * and returns an object containing a single {@link GenerateContentResponse}.