@firebase/ai 2.2.1-canary.55f3f83a7 → 2.2.1-canary.6ab71fa0b
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.
- package/dist/ai-public.d.ts +162 -32
- package/dist/ai.d.ts +168 -32
- package/dist/esm/index.esm.js +115 -22
- package/dist/esm/index.esm.js.map +1 -1
- package/dist/esm/src/requests/hybrid-helpers.d.ts +28 -0
- package/dist/esm/src/types/chrome-adapter.d.ts +2 -2
- package/dist/esm/src/types/content.d.ts +81 -2
- package/dist/esm/src/types/enums.d.ts +53 -4
- package/dist/esm/src/types/language-model.d.ts +10 -20
- package/dist/esm/src/types/requests.d.ts +15 -7
- package/dist/index.cjs.js +116 -21
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +116 -21
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +115 -22
- package/dist/index.node.mjs.map +1 -1
- package/dist/src/requests/hybrid-helpers.d.ts +28 -0
- package/dist/src/types/chrome-adapter.d.ts +2 -2
- package/dist/src/types/content.d.ts +81 -2
- package/dist/src/types/enums.d.ts +53 -4
- package/dist/src/types/language-model.d.ts +10 -20
- package/dist/src/types/requests.d.ts +15 -7
- package/package.json +8 -8
package/dist/ai.d.ts
CHANGED
|
@@ -366,13 +366,13 @@ export declare class ChatSession {
|
|
|
366
366
|
}
|
|
367
367
|
|
|
368
368
|
/**
|
|
369
|
-
*
|
|
369
|
+
* Defines an inference "backend" that uses Chrome's on-device model,
|
|
370
370
|
* and encapsulates logic for detecting when on-device inference is
|
|
371
371
|
* possible.
|
|
372
372
|
*
|
|
373
373
|
* These methods should not be called directly by the user.
|
|
374
374
|
*
|
|
375
|
-
* @
|
|
375
|
+
* @beta
|
|
376
376
|
*/
|
|
377
377
|
export declare interface ChromeAdapter {
|
|
378
378
|
/**
|
|
@@ -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}
|
|
@@ -1335,9 +1425,8 @@ export declare const HarmSeverity: {
|
|
|
1335
1425
|
export declare type HarmSeverity = (typeof HarmSeverity)[keyof typeof HarmSeverity];
|
|
1336
1426
|
|
|
1337
1427
|
/**
|
|
1338
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1339
1428
|
* Configures hybrid inference.
|
|
1340
|
-
* @
|
|
1429
|
+
* @beta
|
|
1341
1430
|
*/
|
|
1342
1431
|
export declare interface HybridParams {
|
|
1343
1432
|
/**
|
|
@@ -1820,20 +1909,37 @@ export declare interface ImagenSafetySettings {
|
|
|
1820
1909
|
}
|
|
1821
1910
|
|
|
1822
1911
|
/**
|
|
1823
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1824
1912
|
* Determines whether inference happens on-device or in-cloud.
|
|
1825
|
-
*
|
|
1913
|
+
*
|
|
1914
|
+
* @remarks
|
|
1915
|
+
* <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
|
|
1916
|
+
* on-device model. If on-device inference is not available, the SDK
|
|
1917
|
+
* will fall back to using a cloud-hosted model.
|
|
1918
|
+
* <br/>
|
|
1919
|
+
* <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
|
|
1920
|
+
* on-device model. The SDK will not fall back to a cloud-hosted model.
|
|
1921
|
+
* If on-device inference is not available, inference methods will throw.
|
|
1922
|
+
* <br/>
|
|
1923
|
+
* <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
|
|
1924
|
+
* cloud-hosted model. The SDK will not fall back to an on-device model.
|
|
1925
|
+
* <br/>
|
|
1926
|
+
* <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
|
|
1927
|
+
* cloud-hosted model. If not available, the SDK will fall back to an
|
|
1928
|
+
* on-device model.
|
|
1929
|
+
*
|
|
1930
|
+
* @beta
|
|
1826
1931
|
*/
|
|
1827
1932
|
export declare const InferenceMode: {
|
|
1828
1933
|
readonly PREFER_ON_DEVICE: "prefer_on_device";
|
|
1829
1934
|
readonly ONLY_ON_DEVICE: "only_on_device";
|
|
1830
1935
|
readonly ONLY_IN_CLOUD: "only_in_cloud";
|
|
1936
|
+
readonly PREFER_IN_CLOUD: "prefer_in_cloud";
|
|
1831
1937
|
};
|
|
1832
1938
|
|
|
1833
1939
|
/**
|
|
1834
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1835
1940
|
* Determines whether inference happens on-device or in-cloud.
|
|
1836
|
-
*
|
|
1941
|
+
*
|
|
1942
|
+
* @beta
|
|
1837
1943
|
*/
|
|
1838
1944
|
export declare type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
|
|
1839
1945
|
|
|
@@ -1855,6 +1961,8 @@ export declare interface InlineDataPart {
|
|
|
1855
1961
|
* @internal
|
|
1856
1962
|
*/
|
|
1857
1963
|
thoughtSignature?: never;
|
|
1964
|
+
executableCode?: never;
|
|
1965
|
+
codeExecutionResult?: never;
|
|
1858
1966
|
}
|
|
1859
1967
|
|
|
1860
1968
|
/**
|
|
@@ -1866,10 +1974,26 @@ export declare class IntegerSchema extends Schema {
|
|
|
1866
1974
|
}
|
|
1867
1975
|
|
|
1868
1976
|
/**
|
|
1869
|
-
*
|
|
1870
|
-
*
|
|
1977
|
+
* The programming language of the code.
|
|
1978
|
+
*
|
|
1979
|
+
* @public
|
|
1980
|
+
*/
|
|
1981
|
+
export declare const Language: {
|
|
1982
|
+
UNSPECIFIED: string;
|
|
1983
|
+
PYTHON: string;
|
|
1984
|
+
};
|
|
1985
|
+
|
|
1986
|
+
/**
|
|
1987
|
+
* The programming language of the code.
|
|
1988
|
+
*
|
|
1871
1989
|
* @public
|
|
1872
1990
|
*/
|
|
1991
|
+
export declare type Language = (typeof Language)[keyof typeof Language];
|
|
1992
|
+
|
|
1993
|
+
/**
|
|
1994
|
+
* Configures the creation of an on-device language model session.
|
|
1995
|
+
* @beta
|
|
1996
|
+
*/
|
|
1873
1997
|
export declare interface LanguageModelCreateCoreOptions {
|
|
1874
1998
|
topK?: number;
|
|
1875
1999
|
temperature?: number;
|
|
@@ -1877,9 +2001,8 @@ export declare interface LanguageModelCreateCoreOptions {
|
|
|
1877
2001
|
}
|
|
1878
2002
|
|
|
1879
2003
|
/**
|
|
1880
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1881
2004
|
* Configures the creation of an on-device language model session.
|
|
1882
|
-
* @
|
|
2005
|
+
* @beta
|
|
1883
2006
|
*/
|
|
1884
2007
|
export declare interface LanguageModelCreateOptions extends LanguageModelCreateCoreOptions {
|
|
1885
2008
|
signal?: AbortSignal;
|
|
@@ -1887,18 +2010,16 @@ export declare interface LanguageModelCreateOptions extends LanguageModelCreateC
|
|
|
1887
2010
|
}
|
|
1888
2011
|
|
|
1889
2012
|
/**
|
|
1890
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1891
2013
|
* Options for the expected inputs for an on-device language model.
|
|
1892
|
-
* @
|
|
2014
|
+
* @beta
|
|
1893
2015
|
*/ export declare interface LanguageModelExpected {
|
|
1894
2016
|
type: LanguageModelMessageType;
|
|
1895
2017
|
languages?: string[];
|
|
1896
2018
|
}
|
|
1897
2019
|
|
|
1898
2020
|
/**
|
|
1899
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1900
2021
|
* An on-device language model message.
|
|
1901
|
-
* @
|
|
2022
|
+
* @beta
|
|
1902
2023
|
*/
|
|
1903
2024
|
export declare interface LanguageModelMessage {
|
|
1904
2025
|
role: LanguageModelMessageRole;
|
|
@@ -1906,9 +2027,8 @@ export declare interface LanguageModelMessage {
|
|
|
1906
2027
|
}
|
|
1907
2028
|
|
|
1908
2029
|
/**
|
|
1909
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1910
2030
|
* An on-device language model content object.
|
|
1911
|
-
* @
|
|
2031
|
+
* @beta
|
|
1912
2032
|
*/
|
|
1913
2033
|
export declare interface LanguageModelMessageContent {
|
|
1914
2034
|
type: LanguageModelMessageType;
|
|
@@ -1916,30 +2036,26 @@ export declare interface LanguageModelMessageContent {
|
|
|
1916
2036
|
}
|
|
1917
2037
|
|
|
1918
2038
|
/**
|
|
1919
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1920
2039
|
* Content formats that can be provided as on-device message content.
|
|
1921
|
-
* @
|
|
2040
|
+
* @beta
|
|
1922
2041
|
*/
|
|
1923
2042
|
export declare type LanguageModelMessageContentValue = ImageBitmapSource | AudioBuffer | BufferSource | string;
|
|
1924
2043
|
|
|
1925
2044
|
/**
|
|
1926
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1927
2045
|
* Allowable roles for on-device language model usage.
|
|
1928
|
-
* @
|
|
2046
|
+
* @beta
|
|
1929
2047
|
*/
|
|
1930
2048
|
export declare type LanguageModelMessageRole = 'system' | 'user' | 'assistant';
|
|
1931
2049
|
|
|
1932
2050
|
/**
|
|
1933
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1934
2051
|
* Allowable types for on-device language model messages.
|
|
1935
|
-
* @
|
|
2052
|
+
* @beta
|
|
1936
2053
|
*/
|
|
1937
2054
|
export declare type LanguageModelMessageType = 'text' | 'image' | 'audio';
|
|
1938
2055
|
|
|
1939
2056
|
/**
|
|
1940
|
-
* <b>(EXPERIMENTAL)</b>
|
|
1941
2057
|
* Options for an on-device language model prompt.
|
|
1942
|
-
* @
|
|
2058
|
+
* @beta
|
|
1943
2059
|
*/
|
|
1944
2060
|
export declare interface LanguageModelPromptOptions {
|
|
1945
2061
|
responseConstraint?: object;
|
|
@@ -2291,22 +2407,40 @@ export declare interface ObjectSchemaRequest extends SchemaRequest {
|
|
|
2291
2407
|
}
|
|
2292
2408
|
|
|
2293
2409
|
/**
|
|
2294
|
-
* <b>(EXPERIMENTAL)</b>
|
|
2295
2410
|
* Encapsulates configuration for on-device inference.
|
|
2296
2411
|
*
|
|
2297
|
-
* @
|
|
2412
|
+
* @beta
|
|
2298
2413
|
*/
|
|
2299
2414
|
export declare interface OnDeviceParams {
|
|
2300
2415
|
createOptions?: LanguageModelCreateOptions;
|
|
2301
2416
|
promptOptions?: LanguageModelPromptOptions;
|
|
2302
2417
|
}
|
|
2303
2418
|
|
|
2419
|
+
/**
|
|
2420
|
+
* Represents the result of the code execution.
|
|
2421
|
+
*
|
|
2422
|
+
* @public
|
|
2423
|
+
*/
|
|
2424
|
+
export declare const Outcome: {
|
|
2425
|
+
UNSPECIFIED: string;
|
|
2426
|
+
OK: string;
|
|
2427
|
+
FAILED: string;
|
|
2428
|
+
DEADLINE_EXCEEDED: string;
|
|
2429
|
+
};
|
|
2430
|
+
|
|
2431
|
+
/**
|
|
2432
|
+
* Represents the result of the code execution.
|
|
2433
|
+
*
|
|
2434
|
+
* @public
|
|
2435
|
+
*/
|
|
2436
|
+
export declare type Outcome = (typeof Outcome)[keyof typeof Outcome];
|
|
2437
|
+
|
|
2304
2438
|
/**
|
|
2305
2439
|
* Content part - includes text, image/video, or function call/response
|
|
2306
2440
|
* part types.
|
|
2307
2441
|
* @public
|
|
2308
2442
|
*/
|
|
2309
|
-
export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart;
|
|
2443
|
+
export declare type Part = TextPart | InlineDataPart | FunctionCallPart | FunctionResponsePart | FileDataPart | ExecutableCodePart | CodeExecutionResultPart;
|
|
2310
2444
|
|
|
2311
2445
|
/**
|
|
2312
2446
|
* Possible roles.
|
|
@@ -2819,6 +2953,8 @@ export declare interface TextPart {
|
|
|
2819
2953
|
* @internal
|
|
2820
2954
|
*/
|
|
2821
2955
|
thoughtSignature?: string;
|
|
2956
|
+
executableCode?: never;
|
|
2957
|
+
codeExecutionResult?: never;
|
|
2822
2958
|
}
|
|
2823
2959
|
|
|
2824
2960
|
/**
|
|
@@ -2859,7 +2995,7 @@ export declare interface ThinkingConfig {
|
|
|
2859
2995
|
* Defines a tool that model can call to access external knowledge.
|
|
2860
2996
|
* @public
|
|
2861
2997
|
*/
|
|
2862
|
-
export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool;
|
|
2998
|
+
export declare type Tool = FunctionDeclarationsTool | GoogleSearchTool | CodeExecutionTool;
|
|
2863
2999
|
|
|
2864
3000
|
/**
|
|
2865
3001
|
* Tool config. This config is shared for all tools provided in the request.
|
package/dist/esm/index.esm.js
CHANGED
|
@@ -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.
|
|
7
|
+
var version = "2.2.1-canary.6ab71fa0b";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @license
|
|
@@ -353,14 +353,51 @@ const ResponseModality = {
|
|
|
353
353
|
AUDIO: 'AUDIO'
|
|
354
354
|
};
|
|
355
355
|
/**
|
|
356
|
-
* <b>(EXPERIMENTAL)</b>
|
|
357
356
|
* Determines whether inference happens on-device or in-cloud.
|
|
358
|
-
*
|
|
357
|
+
*
|
|
358
|
+
* @remarks
|
|
359
|
+
* <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
|
|
360
|
+
* on-device model. If on-device inference is not available, the SDK
|
|
361
|
+
* will fall back to using a cloud-hosted model.
|
|
362
|
+
* <br/>
|
|
363
|
+
* <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
|
|
364
|
+
* on-device model. The SDK will not fall back to a cloud-hosted model.
|
|
365
|
+
* If on-device inference is not available, inference methods will throw.
|
|
366
|
+
* <br/>
|
|
367
|
+
* <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
|
|
368
|
+
* cloud-hosted model. The SDK will not fall back to an on-device model.
|
|
369
|
+
* <br/>
|
|
370
|
+
* <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
|
|
371
|
+
* cloud-hosted model. If not available, the SDK will fall back to an
|
|
372
|
+
* on-device model.
|
|
373
|
+
*
|
|
374
|
+
* @beta
|
|
359
375
|
*/
|
|
360
376
|
const InferenceMode = {
|
|
361
377
|
'PREFER_ON_DEVICE': 'prefer_on_device',
|
|
362
378
|
'ONLY_ON_DEVICE': 'only_on_device',
|
|
363
|
-
'ONLY_IN_CLOUD': 'only_in_cloud'
|
|
379
|
+
'ONLY_IN_CLOUD': 'only_in_cloud',
|
|
380
|
+
'PREFER_IN_CLOUD': 'prefer_in_cloud'
|
|
381
|
+
};
|
|
382
|
+
/**
|
|
383
|
+
* Represents the result of the code execution.
|
|
384
|
+
*
|
|
385
|
+
* @public
|
|
386
|
+
*/
|
|
387
|
+
const Outcome = {
|
|
388
|
+
UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
|
|
389
|
+
OK: 'OUTCOME_OK',
|
|
390
|
+
FAILED: 'OUTCOME_FAILED',
|
|
391
|
+
DEADLINE_EXCEEDED: 'OUTCOME_DEADLINE_EXCEEDED'
|
|
392
|
+
};
|
|
393
|
+
/**
|
|
394
|
+
* The programming language of the code.
|
|
395
|
+
*
|
|
396
|
+
* @public
|
|
397
|
+
*/
|
|
398
|
+
const Language = {
|
|
399
|
+
UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
|
|
400
|
+
PYTHON: 'PYTHON'
|
|
364
401
|
};
|
|
365
402
|
|
|
366
403
|
/**
|
|
@@ -2057,6 +2094,72 @@ function aggregateResponses(responses) {
|
|
|
2057
2094
|
return aggregatedResponse;
|
|
2058
2095
|
}
|
|
2059
2096
|
|
|
2097
|
+
/**
|
|
2098
|
+
* @license
|
|
2099
|
+
* Copyright 2025 Google LLC
|
|
2100
|
+
*
|
|
2101
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2102
|
+
* you may not use this file except in compliance with the License.
|
|
2103
|
+
* You may obtain a copy of the License at
|
|
2104
|
+
*
|
|
2105
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2106
|
+
*
|
|
2107
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2108
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2109
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2110
|
+
* See the License for the specific language governing permissions and
|
|
2111
|
+
* limitations under the License.
|
|
2112
|
+
*/
|
|
2113
|
+
const errorsCausingFallback = [
|
|
2114
|
+
// most network errors
|
|
2115
|
+
AIErrorCode.FETCH_ERROR,
|
|
2116
|
+
// fallback code for all other errors in makeRequest
|
|
2117
|
+
AIErrorCode.ERROR,
|
|
2118
|
+
// error due to API not being enabled in project
|
|
2119
|
+
AIErrorCode.API_NOT_ENABLED
|
|
2120
|
+
];
|
|
2121
|
+
/**
|
|
2122
|
+
* Dispatches a request to the appropriate backend (on-device or in-cloud)
|
|
2123
|
+
* based on the inference mode.
|
|
2124
|
+
*
|
|
2125
|
+
* @param request - The request to be sent.
|
|
2126
|
+
* @param chromeAdapter - The on-device model adapter.
|
|
2127
|
+
* @param onDeviceCall - The function to call for on-device inference.
|
|
2128
|
+
* @param inCloudCall - The function to call for in-cloud inference.
|
|
2129
|
+
* @returns The response from the backend.
|
|
2130
|
+
*/
|
|
2131
|
+
async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
|
|
2132
|
+
if (!chromeAdapter) {
|
|
2133
|
+
return inCloudCall();
|
|
2134
|
+
}
|
|
2135
|
+
switch (chromeAdapter.mode) {
|
|
2136
|
+
case InferenceMode.ONLY_ON_DEVICE:
|
|
2137
|
+
if (await chromeAdapter.isAvailable(request)) {
|
|
2138
|
+
return onDeviceCall();
|
|
2139
|
+
}
|
|
2140
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
|
|
2141
|
+
case InferenceMode.ONLY_IN_CLOUD:
|
|
2142
|
+
return inCloudCall();
|
|
2143
|
+
case InferenceMode.PREFER_IN_CLOUD:
|
|
2144
|
+
try {
|
|
2145
|
+
return await inCloudCall();
|
|
2146
|
+
}
|
|
2147
|
+
catch (e) {
|
|
2148
|
+
if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
|
|
2149
|
+
return onDeviceCall();
|
|
2150
|
+
}
|
|
2151
|
+
throw e;
|
|
2152
|
+
}
|
|
2153
|
+
case InferenceMode.PREFER_ON_DEVICE:
|
|
2154
|
+
if (await chromeAdapter.isAvailable(request)) {
|
|
2155
|
+
return onDeviceCall();
|
|
2156
|
+
}
|
|
2157
|
+
return inCloudCall();
|
|
2158
|
+
default:
|
|
2159
|
+
throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2060
2163
|
/**
|
|
2061
2164
|
* @license
|
|
2062
2165
|
* Copyright 2024 Google LLC
|
|
@@ -2081,13 +2184,7 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
|
|
|
2081
2184
|
/* stream */ true, JSON.stringify(params), requestOptions);
|
|
2082
2185
|
}
|
|
2083
2186
|
async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
2084
|
-
|
|
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
|
-
}
|
|
2187
|
+
const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
|
|
2091
2188
|
return processStream(response, apiSettings); // TODO: Map streaming responses
|
|
2092
2189
|
}
|
|
2093
2190
|
async function generateContentOnCloud(apiSettings, model, params, requestOptions) {
|
|
@@ -2098,13 +2195,7 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
|
|
|
2098
2195
|
/* stream */ false, JSON.stringify(params), requestOptions);
|
|
2099
2196
|
}
|
|
2100
2197
|
async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
2101
|
-
|
|
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
|
-
}
|
|
2198
|
+
const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
|
|
2108
2199
|
const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
|
|
2109
2200
|
const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
|
|
2110
2201
|
return {
|
|
@@ -2314,7 +2405,9 @@ function validateChatHistory(history) {
|
|
|
2314
2405
|
functionCall: 0,
|
|
2315
2406
|
functionResponse: 0,
|
|
2316
2407
|
thought: 0,
|
|
2317
|
-
thoughtSignature: 0
|
|
2408
|
+
thoughtSignature: 0,
|
|
2409
|
+
executableCode: 0,
|
|
2410
|
+
codeExecutionResult: 0
|
|
2318
2411
|
};
|
|
2319
2412
|
for (const part of parts) {
|
|
2320
2413
|
for (const key of VALID_PART_FIELDS) {
|
|
@@ -2515,8 +2608,8 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
|
|
|
2515
2608
|
return response.json();
|
|
2516
2609
|
}
|
|
2517
2610
|
async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
|
|
2518
|
-
if (chromeAdapter
|
|
2519
|
-
|
|
2611
|
+
if (chromeAdapter?.mode === InferenceMode.ONLY_ON_DEVICE) {
|
|
2612
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, 'countTokens() is not supported for on-device models.');
|
|
2520
2613
|
}
|
|
2521
2614
|
return countTokensOnCloud(apiSettings, model, params, requestOptions);
|
|
2522
2615
|
}
|
|
@@ -3942,5 +4035,5 @@ function registerAI() {
|
|
|
3942
4035
|
}
|
|
3943
4036
|
registerAI();
|
|
3944
4037
|
|
|
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 };
|
|
4038
|
+
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
4039
|
//# sourceMappingURL=index.esm.js.map
|