@firebase/ai 2.2.1-canary.55f3f83a7 → 2.2.1-canary.9b8ab02c5

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.
@@ -393,6 +393,53 @@ export declare interface CitationMetadata {
393
393
  citations: Citation[];
394
394
  }
395
395
 
396
+ /**
397
+ * The results of code execution run by the model.
398
+ *
399
+ * @public
400
+ */
401
+ export declare interface CodeExecutionResult {
402
+ /**
403
+ * The result of the code execution.
404
+ */
405
+ outcome?: Outcome;
406
+ /**
407
+ * The output from the code execution, or an error message
408
+ * if it failed.
409
+ */
410
+ output?: string;
411
+ }
412
+
413
+ /**
414
+ * Represents the code execution result from the model.
415
+ *
416
+ * @public
417
+ */
418
+ export declare interface CodeExecutionResultPart {
419
+ text?: never;
420
+ inlineData?: never;
421
+ functionCall?: never;
422
+ functionResponse?: never;
423
+ fileData: never;
424
+ thought?: never;
425
+ /* Excluded from this release type: thoughtSignature */
426
+ executableCode?: never;
427
+ codeExecutionResult?: CodeExecutionResult;
428
+ }
429
+
430
+ /**
431
+ * A tool that enables the model to use code execution.
432
+ *
433
+ * @public
434
+ */
435
+ export declare interface CodeExecutionTool {
436
+ /**
437
+ * Specifies the Google Search configuration.
438
+ * Currently, this is an empty object, but it's reserved for future configuration options.
439
+ */
440
+ codeExecution: {};
441
+ }
442
+
396
443
  /**
397
444
  * Content type for both prompts and response candidates.
398
445
  * @public
@@ -530,6 +577,39 @@ export declare interface ErrorDetails {
530
577
  [key: string]: unknown;
531
578
  }
532
579
 
580
+ /**
581
+ * An interface for executable code returned by the model.
582
+ *
583
+ * @public
584
+ */
585
+ export declare interface ExecutableCode {
586
+ /**
587
+ * The programming language of the code.
588
+ */
589
+ language?: Language;
590
+ /**
591
+ * The source code to be executed.
592
+ */
593
+ code?: string;
594
+ }
595
+
596
+ /**
597
+ * Represents the code that is executed by the model.
598
+ *
599
+ * @public
600
+ */
601
+ export declare interface ExecutableCodePart {
602
+ text?: never;
603
+ inlineData?: never;
604
+ functionCall?: never;
605
+ functionResponse?: never;
606
+ fileData: never;
607
+ thought?: never;
608
+ /* Excluded from this release type: thoughtSignature */
609
+ executableCode?: ExecutableCode;
610
+ codeExecutionResult?: never;
611
+ }
612
+
533
613
  /**
534
614
  * Data pointing to a file uploaded on Google Cloud Storage.
535
615
  * @public
@@ -551,6 +631,8 @@ export declare interface FileDataPart {
551
631
  fileData: FileData;
552
632
  thought?: boolean;
553
633
  /* Excluded from this release type: thoughtSignature */
634
+ executableCode?: never;
635
+ codeExecutionResult?: never;
554
636
  }
555
637
 
556
638
  /**
@@ -669,6 +751,8 @@ export declare interface FunctionCallPart {
669
751
  functionResponse?: never;
670
752
  thought?: boolean;
671
753
  /* Excluded from this release type: thoughtSignature */
754
+ executableCode?: never;
755
+ codeExecutionResult?: never;
672
756
  }
673
757
 
674
758
  /**
@@ -753,6 +837,8 @@ export declare interface FunctionResponsePart {
753
837
  functionResponse: FunctionResponse;
754
838
  thought?: boolean;
755
839
  /* Excluded from this release type: thoughtSignature */
840
+ executableCode?: never;
841
+ codeExecutionResult?: never;
756
842
  }
757
843
 
758
844
  /**
@@ -1024,8 +1110,6 @@ export declare interface GoogleSearchTool {
1024
1110
  /**
1025
1111
  * Specifies the Google Search configuration.
1026
1112
  * Currently, this is an empty object, but it's reserved for future configuration options.
1027
- * Specifies the Google Search configuration. Currently, this is an empty object, but it's
1028
- * reserved for future configuration options.
1029
1113
  *
1030
1114
  * When using this feature, you are required to comply with the "Grounding with Google Search"
1031
1115
  * usage requirements for your chosen API provider: {@link https://ai.google.dev/gemini-api/terms#grounding-with-google-search | Gemini Developer API}
@@ -1717,17 +1801,36 @@ export declare interface ImagenSafetySettings {
1717
1801
  /**
1718
1802
  * <b>(EXPERIMENTAL)</b>
1719
1803
  * Determines whether inference happens on-device or in-cloud.
1804
+ *
1805
+ * @remarks
1806
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
1807
+ * on-device model. If on-device inference is not available, the SDK
1808
+ * will fall back to using a cloud-hosted model.
1809
+ * <br/>
1810
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
1811
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
1812
+ * If on-device inference is not available, inference methods will throw.
1813
+ * <br/>
1814
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
1815
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
1816
+ * <br/>
1817
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
1818
+ * cloud-hosted model. If not available, the SDK will fall back to an
1819
+ * on-device model.
1820
+ *
1720
1821
  * @public
1721
1822
  */
1722
1823
  export declare const InferenceMode: {
1723
1824
  readonly PREFER_ON_DEVICE: "prefer_on_device";
1724
1825
  readonly ONLY_ON_DEVICE: "only_on_device";
1725
1826
  readonly ONLY_IN_CLOUD: "only_in_cloud";
1827
+ readonly PREFER_IN_CLOUD: "prefer_in_cloud";
1726
1828
  };
1727
1829
 
1728
1830
  /**
1729
1831
  * <b>(EXPERIMENTAL)</b>
1730
1832
  * Determines whether inference happens on-device or in-cloud.
1833
+ *
1731
1834
  * @public
1732
1835
  */
1733
1836
  export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
@@ -1747,6 +1850,8 @@ export declare interface InlineDataPart {
1747
1850
  videoMetadata?: VideoMetadata;
1748
1851
  thought?: boolean;
1749
1852
  /* Excluded from this release type: thoughtSignature */
1853
+ executableCode?: never;
1854
+ codeExecutionResult?: never;
1750
1855
  }
1751
1856
 
1752
1857
  /**
@@ -1757,6 +1862,23 @@ export declare class IntegerSchema extends Schema {
1757
1862
  constructor(schemaParams?: SchemaParams);
1758
1863
  }
1759
1864
 
1865
+ /**
1866
+ * The programming language of the code.
1867
+ *
1868
+ * @public
1869
+ */
1870
+ export declare const Language: {
1871
+ UNSPECIFIED: string;
1872
+ PYTHON: string;
1873
+ };
1874
+
1875
+ /**
1876
+ * The programming language of the code.
1877
+ *
1878
+ * @public
1879
+ */
1880
+ export declare type Language = (typeof Language)[keyof typeof Language];
1881
+
1760
1882
  /**
1761
1883
  * <b>(EXPERIMENTAL)</b>
1762
1884
  * Configures the creation of an on-device language model session.
@@ -2177,12 +2299,31 @@ export declare interface OnDeviceParams {
2177
2299
  promptOptions?: LanguageModelPromptOptions;
2178
2300
  }
2179
2301
 
2302
+ /**
2303
+ * Represents the result of the code execution.
2304
+ *
2305
+ * @public
2306
+ */
2307
+ export declare const Outcome: {
2308
+ UNSPECIFIED: string;
2309
+ OK: string;
2310
+ FAILED: string;
2311
+ DEADLINE_EXCEEDED: string;
2312
+ };
2313
+
2314
+ /**
2315
+ * Represents the result of the code execution.
2316
+ *
2317
+ * @public
2318
+ */
2319
+ export declare type Outcome = (typeof Outcome)[keyof typeof Outcome];
2320
+
2180
2321
  /**
2181
2322
  * Content part - includes text, image/video, or function call/response
2182
2323
  * part types.
2183
2324
  * @public
2184
2325
  */
2185
- export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
2326
+ export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart | ExecutableCodePart | CodeExecutionResultPart;
2186
2327
 
2187
2328
  /**
2188
2329
  * Possible roles.
@@ -2684,6 +2825,8 @@ export declare interface TextPart {
2684
2825
  functionResponse?: never;
2685
2826
  thought?: boolean;
2686
2827
  /* Excluded from this release type: thoughtSignature */
2828
+ executableCode?: never;
2829
+ codeExecutionResult?: never;
2687
2830
  }
2688
2831
 
2689
2832
  /**
@@ -2724,7 +2867,7 @@ export declare interface ThinkingConfig {
2724
2867
  * Defines a tool that model can call to access external knowledge.
2725
2868
  * @public
2726
2869
  */
2727
- export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool;
2870
+ export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool | CodeExecutionTool;
2728
2871
 
2729
2872
  /**
2730
2873
  * Tool config. This config is shared for all tools provided in the request.
package/dist/ai.d.ts CHANGED
@@ -436,6 +436,56 @@ export declare interface CitationMetadata {
436
436
  citations: Citation[];
437
437
  }
438
438
 
439
+ /**
440
+ * The results of code execution run by the model.
441
+ *
442
+ * @public
443
+ */
444
+ export declare interface CodeExecutionResult {
445
+ /**
446
+ * The result of the code execution.
447
+ */
448
+ outcome?: Outcome;
449
+ /**
450
+ * The output from the code execution, or an error message
451
+ * if it failed.
452
+ */
453
+ output?: string;
454
+ }
455
+
456
+ /**
457
+ * Represents the code execution result from the model.
458
+ *
459
+ * @public
460
+ */
461
+ export declare interface CodeExecutionResultPart {
462
+ text?: never;
463
+ inlineData?: never;
464
+ functionCall?: never;
465
+ functionResponse?: never;
466
+ fileData: never;
467
+ thought?: never;
468
+ /**
469
+ * @internal
470
+ */
471
+ thoughtSignature?: never;
472
+ executableCode?: never;
473
+ codeExecutionResult?: CodeExecutionResult;
474
+ }
475
+
476
+ /**
477
+ * A tool that enables the model to use code execution.
478
+ *
479
+ * @public
480
+ */
481
+ export declare interface CodeExecutionTool {
482
+ /**
483
+ * Specifies the Google Search configuration.
484
+ * Currently, this is an empty object, but it's reserved for future configuration options.
485
+ */
486
+ codeExecution: {};
487
+ }
488
+
439
489
  /**
440
490
  * Content type for both prompts and response candidates.
441
491
  * @public
@@ -573,6 +623,42 @@ export declare interface ErrorDetails {
573
623
  [key: string]: unknown;
574
624
  }
575
625
 
626
+ /**
627
+ * An interface for executable code returned by the model.
628
+ *
629
+ * @public
630
+ */
631
+ export declare interface ExecutableCode {
632
+ /**
633
+ * The programming language of the code.
634
+ */
635
+ language?: Language;
636
+ /**
637
+ * The source code to be executed.
638
+ */
639
+ code?: string;
640
+ }
641
+
642
+ /**
643
+ * Represents the code that is executed by the model.
644
+ *
645
+ * @public
646
+ */
647
+ export declare interface ExecutableCodePart {
648
+ text?: never;
649
+ inlineData?: never;
650
+ functionCall?: never;
651
+ functionResponse?: never;
652
+ fileData: never;
653
+ thought?: never;
654
+ /**
655
+ * @internal
656
+ */
657
+ thoughtSignature?: never;
658
+ executableCode?: ExecutableCode;
659
+ codeExecutionResult?: never;
660
+ }
661
+
576
662
  /**
577
663
  * Data pointing to a file uploaded on Google Cloud Storage.
578
664
  * @public
@@ -597,6 +683,8 @@ export declare interface FileDataPart {
597
683
  * @internal
598
684
  */
599
685
  thoughtSignature?: never;
686
+ executableCode?: never;
687
+ codeExecutionResult?: never;
600
688
  }
601
689
 
602
690
  /**
@@ -718,6 +806,8 @@ export declare interface FunctionCallPart {
718
806
  * @internal
719
807
  */
720
808
  thoughtSignature?: never;
809
+ executableCode?: never;
810
+ codeExecutionResult?: never;
721
811
  }
722
812
 
723
813
  /**
@@ -805,6 +895,8 @@ export declare interface FunctionResponsePart {
805
895
  * @internal
806
896
  */
807
897
  thoughtSignature?: never;
898
+ executableCode?: never;
899
+ codeExecutionResult?: never;
808
900
  }
809
901
 
810
902
  /**
@@ -1110,8 +1202,6 @@ export declare interface GoogleSearchTool {
1110
1202
  /**
1111
1203
  * Specifies the Google Search configuration.
1112
1204
  * Currently, this is an empty object, but it's reserved for future configuration options.
1113
- * Specifies the Google Search configuration. Currently, this is an empty object, but it's
1114
- * reserved for future configuration options.
1115
1205
  *
1116
1206
  * When using this feature, you are required to comply with the "Grounding with Google Search"
1117
1207
  * usage requirements for your chosen API provider: {@link https://ai.google.dev/gemini-api/terms#grounding-with-google-search | Gemini Developer API}
@@ -1822,17 +1912,36 @@ export declare interface ImagenSafetySettings {
1822
1912
  /**
1823
1913
  * <b>(EXPERIMENTAL)</b>
1824
1914
  * Determines whether inference happens on-device or in-cloud.
1915
+ *
1916
+ * @remarks
1917
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
1918
+ * on-device model. If on-device inference is not available, the SDK
1919
+ * will fall back to using a cloud-hosted model.
1920
+ * <br/>
1921
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
1922
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
1923
+ * If on-device inference is not available, inference methods will throw.
1924
+ * <br/>
1925
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
1926
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
1927
+ * <br/>
1928
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
1929
+ * cloud-hosted model. If not available, the SDK will fall back to an
1930
+ * on-device model.
1931
+ *
1825
1932
  * @public
1826
1933
  */
1827
1934
  export declare const InferenceMode: {
1828
1935
  readonly PREFER_ON_DEVICE: "prefer_on_device";
1829
1936
  readonly ONLY_ON_DEVICE: "only_on_device";
1830
1937
  readonly ONLY_IN_CLOUD: "only_in_cloud";
1938
+ readonly PREFER_IN_CLOUD: "prefer_in_cloud";
1831
1939
  };
1832
1940
 
1833
1941
  /**
1834
1942
  * <b>(EXPERIMENTAL)</b>
1835
1943
  * Determines whether inference happens on-device or in-cloud.
1944
+ *
1836
1945
  * @public
1837
1946
  */
1838
1947
  export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
@@ -1855,6 +1964,8 @@ export declare interface InlineDataPart {
1855
1964
  * @internal
1856
1965
  */
1857
1966
  thoughtSignature?: never;
1967
+ executableCode?: never;
1968
+ codeExecutionResult?: never;
1858
1969
  }
1859
1970
 
1860
1971
  /**
@@ -1865,6 +1976,23 @@ export declare class IntegerSchema extends Schema {
1865
1976
  constructor(schemaParams?: SchemaParams);
1866
1977
  }
1867
1978
 
1979
+ /**
1980
+ * The programming language of the code.
1981
+ *
1982
+ * @public
1983
+ */
1984
+ export declare const Language: {
1985
+ UNSPECIFIED: string;
1986
+ PYTHON: string;
1987
+ };
1988
+
1989
+ /**
1990
+ * The programming language of the code.
1991
+ *
1992
+ * @public
1993
+ */
1994
+ export declare type Language = (typeof Language)[keyof typeof Language];
1995
+
1868
1996
  /**
1869
1997
  * <b>(EXPERIMENTAL)</b>
1870
1998
  * Configures the creation of an on-device language model session.
@@ -2301,12 +2429,31 @@ export declare interface OnDeviceParams {
2301
2429
  promptOptions?: LanguageModelPromptOptions;
2302
2430
  }
2303
2431
 
2432
+ /**
2433
+ * Represents the result of the code execution.
2434
+ *
2435
+ * @public
2436
+ */
2437
+ export declare const Outcome: {
2438
+ UNSPECIFIED: string;
2439
+ OK: string;
2440
+ FAILED: string;
2441
+ DEADLINE_EXCEEDED: string;
2442
+ };
2443
+
2444
+ /**
2445
+ * Represents the result of the code execution.
2446
+ *
2447
+ * @public
2448
+ */
2449
+ export declare type Outcome = (typeof Outcome)[keyof typeof Outcome];
2450
+
2304
2451
  /**
2305
2452
  * Content part - includes text, image/video, or function call/response
2306
2453
  * part types.
2307
2454
  * @public
2308
2455
  */
2309
- export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
2456
+ export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart | ExecutableCodePart | CodeExecutionResultPart;
2310
2457
 
2311
2458
  /**
2312
2459
  * Possible roles.
@@ -2819,6 +2966,8 @@ export declare interface TextPart {
2819
2966
  * @internal
2820
2967
  */
2821
2968
  thoughtSignature?: string;
2969
+ executableCode?: never;
2970
+ codeExecutionResult?: never;
2822
2971
  }
2823
2972
 
2824
2973
  /**
@@ -2859,7 +3008,7 @@ export declare interface ThinkingConfig {
2859
3008
  * Defines a tool that model can call to access external knowledge.
2860
3009
  * @public
2861
3010
  */
2862
- export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool;
3011
+ export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool | CodeExecutionTool;
2863
3012
 
2864
3013
  /**
2865
3014
  * Tool config. This config is shared for all tools provided in the request.
@@ -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.2.1-canary.55f3f83a7";
7
+ var version = "2.2.1-canary.9b8ab02c5";
8
8
 
9
9
  /**
10
10
  * @license
@@ -355,12 +355,50 @@ const ResponseModality = {
355
355
  /**
356
356
  * <b>(EXPERIMENTAL)</b>
357
357
  * Determines whether inference happens on-device or in-cloud.
358
+ *
359
+ * @remarks
360
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
361
+ * on-device model. If on-device inference is not available, the SDK
362
+ * will fall back to using a cloud-hosted model.
363
+ * <br/>
364
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
365
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
366
+ * If on-device inference is not available, inference methods will throw.
367
+ * <br/>
368
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
369
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
370
+ * <br/>
371
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
372
+ * cloud-hosted model. If not available, the SDK will fall back to an
373
+ * on-device model.
374
+ *
358
375
  * @public
359
376
  */
360
377
  const InferenceMode = {
361
378
  'PREFER_ON_DEVICE': 'prefer_on_device',
362
379
  'ONLY_ON_DEVICE': 'only_on_device',
363
- 'ONLY_IN_CLOUD': 'only_in_cloud'
380
+ 'ONLY_IN_CLOUD': 'only_in_cloud',
381
+ 'PREFER_IN_CLOUD': 'prefer_in_cloud'
382
+ };
383
+ /**
384
+ * Represents the result of the code execution.
385
+ *
386
+ * @public
387
+ */
388
+ const Outcome = {
389
+ UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
390
+ OK: 'OUTCOME_OK',
391
+ FAILED: 'OUTCOME_FAILED',
392
+ DEADLINE_EXCEEDED: 'OUTCOME_DEADLINE_EXCEEDED'
393
+ };
394
+ /**
395
+ * The programming language of the code.
396
+ *
397
+ * @public
398
+ */
399
+ const Language = {
400
+ UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
401
+ PYTHON: 'PYTHON'
364
402
  };
365
403
 
366
404
  /**
@@ -2057,6 +2095,72 @@ function aggregateResponses(responses) {
2057
2095
  return aggregatedResponse;
2058
2096
  }
2059
2097
 
2098
+ /**
2099
+ * @license
2100
+ * Copyright 2025 Google LLC
2101
+ *
2102
+ * Licensed under the Apache License, Version 2.0 (the "License");
2103
+ * you may not use this file except in compliance with the License.
2104
+ * You may obtain a copy of the License at
2105
+ *
2106
+ * http://www.apache.org/licenses/LICENSE-2.0
2107
+ *
2108
+ * Unless required by applicable law or agreed to in writing, software
2109
+ * distributed under the License is distributed on an "AS IS" BASIS,
2110
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2111
+ * See the License for the specific language governing permissions and
2112
+ * limitations under the License.
2113
+ */
2114
+ const errorsCausingFallback = [
2115
+ // most network errors
2116
+ AIErrorCode.FETCH_ERROR,
2117
+ // fallback code for all other errors in makeRequest
2118
+ AIErrorCode.ERROR,
2119
+ // error due to API not being enabled in project
2120
+ AIErrorCode.API_NOT_ENABLED
2121
+ ];
2122
+ /**
2123
+ * Dispatches a request to the appropriate backend (on-device or in-cloud)
2124
+ * based on the inference mode.
2125
+ *
2126
+ * @param request - The request to be sent.
2127
+ * @param chromeAdapter - The on-device model adapter.
2128
+ * @param onDeviceCall - The function to call for on-device inference.
2129
+ * @param inCloudCall - The function to call for in-cloud inference.
2130
+ * @returns The response from the backend.
2131
+ */
2132
+ async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
2133
+ if (!chromeAdapter) {
2134
+ return inCloudCall();
2135
+ }
2136
+ switch (chromeAdapter.mode) {
2137
+ case InferenceMode.ONLY_ON_DEVICE:
2138
+ if (await chromeAdapter.isAvailable(request)) {
2139
+ return onDeviceCall();
2140
+ }
2141
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
2142
+ case InferenceMode.ONLY_IN_CLOUD:
2143
+ return inCloudCall();
2144
+ case InferenceMode.PREFER_IN_CLOUD:
2145
+ try {
2146
+ return await inCloudCall();
2147
+ }
2148
+ catch (e) {
2149
+ if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
2150
+ return onDeviceCall();
2151
+ }
2152
+ throw e;
2153
+ }
2154
+ case InferenceMode.PREFER_ON_DEVICE:
2155
+ if (await chromeAdapter.isAvailable(request)) {
2156
+ return onDeviceCall();
2157
+ }
2158
+ return inCloudCall();
2159
+ default:
2160
+ throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
2161
+ }
2162
+ }
2163
+
2060
2164
  /**
2061
2165
  * @license
2062
2166
  * Copyright 2024 Google LLC
@@ -2081,13 +2185,7 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
2081
2185
  /* stream */ true, JSON.stringify(params), requestOptions);
2082
2186
  }
2083
2187
  async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
2084
- let response;
2085
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
2086
- response = await chromeAdapter.generateContentStream(params);
2087
- }
2088
- else {
2089
- response = await generateContentStreamOnCloud(apiSettings, model, params, requestOptions);
2090
- }
2188
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
2091
2189
  return processStream(response, apiSettings); // TODO: Map streaming responses
2092
2190
  }
2093
2191
  async function generateContentOnCloud(apiSettings, model, params, requestOptions) {
@@ -2098,13 +2196,7 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
2098
2196
  /* stream */ false, JSON.stringify(params), requestOptions);
2099
2197
  }
2100
2198
  async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
2101
- let response;
2102
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
2103
- response = await chromeAdapter.generateContent(params);
2104
- }
2105
- else {
2106
- response = await generateContentOnCloud(apiSettings, model, params, requestOptions);
2107
- }
2199
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
2108
2200
  const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
2109
2201
  const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
2110
2202
  return {
@@ -2314,7 +2406,9 @@ function validateChatHistory(history) {
2314
2406
  functionCall: 0,
2315
2407
  functionResponse: 0,
2316
2408
  thought: 0,
2317
- thoughtSignature: 0
2409
+ thoughtSignature: 0,
2410
+ executableCode: 0,
2411
+ codeExecutionResult: 0
2318
2412
  };
2319
2413
  for (const part of parts) {
2320
2414
  for (const key of VALID_PART_FIELDS) {
@@ -2515,8 +2609,8 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
2515
2609
  return response.json();
2516
2610
  }
2517
2611
  async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
2518
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
2519
- return (await chromeAdapter.countTokens(params)).json();
2612
+ if (chromeAdapter?.mode === InferenceMode.ONLY_ON_DEVICE) {
2613
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'countTokens() is not supported for on-device models.');
2520
2614
  }
2521
2615
  return countTokensOnCloud(apiSettings, model, params, requestOptions);
2522
2616
  }
@@ -3942,5 +4036,5 @@ function registerAI() {
3942
4036
  }
3943
4037
  registerAI();
3944
4038
 
3945
- 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, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
4039
+ 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, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
3946
4040
  //# sourceMappingURL=index.esm.js.map