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