@firebase/ai 2.10.0 → 2.11.0-20260408221811
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 +246 -20
- package/dist/ai.d.ts +318 -16
- package/dist/esm/index.esm.js +364 -140
- package/dist/esm/index.esm.js.map +1 -1
- package/dist/esm/src/api.d.ts +12 -2
- package/dist/esm/src/methods/chat-session-base.d.ts +89 -0
- package/dist/esm/src/methods/chat-session.d.ts +15 -30
- package/dist/esm/src/methods/generate-content.d.ts +3 -2
- package/dist/esm/src/methods/template-chat-session.d.ts +59 -0
- package/dist/esm/src/models/generative-model.d.ts +5 -0
- package/dist/esm/src/models/imagen-model.d.ts +5 -0
- package/dist/esm/src/models/template-generative-model.d.ts +14 -3
- package/dist/esm/src/models/template-imagen-model.d.ts +4 -1
- package/dist/esm/src/requests/imagen-image-format.d.ts +5 -0
- package/dist/esm/src/types/imagen/requests.d.ts +45 -0
- package/dist/esm/src/types/imagen/responses.d.ts +16 -0
- package/dist/esm/src/types/language-model.d.ts +6 -0
- package/dist/esm/src/types/requests.d.ts +100 -1
- package/dist/index.cjs.js +365 -139
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.node.cjs.js +365 -139
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/index.node.mjs +364 -140
- package/dist/index.node.mjs.map +1 -1
- package/dist/src/api.d.ts +12 -2
- package/dist/src/methods/chat-session-base.d.ts +89 -0
- package/dist/src/methods/chat-session.d.ts +15 -30
- package/dist/src/methods/generate-content.d.ts +3 -2
- package/dist/src/methods/template-chat-session.d.ts +59 -0
- package/dist/src/models/generative-model.d.ts +5 -0
- package/dist/src/models/imagen-model.d.ts +5 -0
- package/dist/src/models/template-generative-model.d.ts +14 -3
- package/dist/src/models/template-imagen-model.d.ts +4 -1
- package/dist/src/requests/imagen-image-format.d.ts +5 -0
- package/dist/src/types/imagen/requests.d.ts +45 -0
- package/dist/src/types/imagen/responses.d.ts +16 -0
- package/dist/src/types/language-model.d.ts +6 -0
- package/dist/src/types/requests.d.ts +100 -1
- package/package.json +2 -2
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.
|
|
7
|
+
var version = "2.11.0-20260408221811";
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @license
|
|
@@ -605,6 +605,11 @@ const SchemaType = {
|
|
|
605
605
|
* and the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#safety-filters | Responsible AI and usage guidelines}
|
|
606
606
|
* for more details.
|
|
607
607
|
*
|
|
608
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
609
|
+
* early as June 2026. As a replacement, you can
|
|
610
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
611
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
612
|
+
*
|
|
608
613
|
* @public
|
|
609
614
|
*/
|
|
610
615
|
const ImagenSafetyFilterLevel = {
|
|
@@ -634,6 +639,11 @@ const ImagenSafetyFilterLevel = {
|
|
|
634
639
|
* See the <a href="http://firebase.google.com/docs/vertex-ai/generate-images">personGeneration</a>
|
|
635
640
|
* documentation for more details.
|
|
636
641
|
*
|
|
642
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
643
|
+
* early as June 2026. As a replacement, you can
|
|
644
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
645
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
646
|
+
*
|
|
637
647
|
* @public
|
|
638
648
|
*/
|
|
639
649
|
const ImagenPersonFilterLevel = {
|
|
@@ -667,6 +677,11 @@ const ImagenPersonFilterLevel = {
|
|
|
667
677
|
* See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
|
|
668
678
|
* for more details and examples of the supported aspect ratios.
|
|
669
679
|
*
|
|
680
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
681
|
+
* early as June 2026. As a replacement, you can
|
|
682
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
683
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
684
|
+
*
|
|
670
685
|
* @public
|
|
671
686
|
*/
|
|
672
687
|
const ImagenAspectRatio = {
|
|
@@ -2575,95 +2590,7 @@ function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, n
|
|
|
2575
2590
|
|
|
2576
2591
|
/**
|
|
2577
2592
|
* @license
|
|
2578
|
-
* Copyright
|
|
2579
|
-
*
|
|
2580
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2581
|
-
* you may not use this file except in compliance with the License.
|
|
2582
|
-
* You may obtain a copy of the License at
|
|
2583
|
-
*
|
|
2584
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2585
|
-
*
|
|
2586
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2587
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2588
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2589
|
-
* See the License for the specific language governing permissions and
|
|
2590
|
-
* limitations under the License.
|
|
2591
|
-
*/
|
|
2592
|
-
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2593
|
-
const VALID_PART_FIELDS = [
|
|
2594
|
-
'text',
|
|
2595
|
-
'inlineData',
|
|
2596
|
-
'functionCall',
|
|
2597
|
-
'functionResponse',
|
|
2598
|
-
'thought',
|
|
2599
|
-
'thoughtSignature'
|
|
2600
|
-
];
|
|
2601
|
-
const VALID_PARTS_PER_ROLE = {
|
|
2602
|
-
user: ['text', 'inlineData'],
|
|
2603
|
-
function: ['functionResponse'],
|
|
2604
|
-
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2605
|
-
// System instructions shouldn't be in history anyway.
|
|
2606
|
-
system: ['text']
|
|
2607
|
-
};
|
|
2608
|
-
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2609
|
-
user: ['model'],
|
|
2610
|
-
function: ['model'],
|
|
2611
|
-
model: ['user', 'function'],
|
|
2612
|
-
// System instructions shouldn't be in history.
|
|
2613
|
-
system: []
|
|
2614
|
-
};
|
|
2615
|
-
function validateChatHistory(history) {
|
|
2616
|
-
let prevContent = null;
|
|
2617
|
-
for (const currContent of history) {
|
|
2618
|
-
const { role, parts } = currContent;
|
|
2619
|
-
if (!prevContent && role !== 'user') {
|
|
2620
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2621
|
-
}
|
|
2622
|
-
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2623
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2624
|
-
}
|
|
2625
|
-
if (!Array.isArray(parts)) {
|
|
2626
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2627
|
-
}
|
|
2628
|
-
if (parts.length === 0) {
|
|
2629
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2630
|
-
}
|
|
2631
|
-
const countFields = {
|
|
2632
|
-
text: 0,
|
|
2633
|
-
inlineData: 0,
|
|
2634
|
-
functionCall: 0,
|
|
2635
|
-
functionResponse: 0,
|
|
2636
|
-
thought: 0,
|
|
2637
|
-
thoughtSignature: 0,
|
|
2638
|
-
executableCode: 0,
|
|
2639
|
-
codeExecutionResult: 0
|
|
2640
|
-
};
|
|
2641
|
-
for (const part of parts) {
|
|
2642
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2643
|
-
if (key in part) {
|
|
2644
|
-
countFields[key] += 1;
|
|
2645
|
-
}
|
|
2646
|
-
}
|
|
2647
|
-
}
|
|
2648
|
-
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2649
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2650
|
-
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2651
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2652
|
-
}
|
|
2653
|
-
}
|
|
2654
|
-
if (prevContent) {
|
|
2655
|
-
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2656
|
-
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2657
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2658
|
-
}
|
|
2659
|
-
}
|
|
2660
|
-
prevContent = currContent;
|
|
2661
|
-
}
|
|
2662
|
-
}
|
|
2663
|
-
|
|
2664
|
-
/**
|
|
2665
|
-
* @license
|
|
2666
|
-
* Copyright 2024 Google LLC
|
|
2593
|
+
* Copyright 2026 Google LLC
|
|
2667
2594
|
*
|
|
2668
2595
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2669
2596
|
* you may not use this file except in compliance with the License.
|
|
@@ -2688,28 +2615,23 @@ const SILENT_ERROR = 'SILENT_ERROR';
|
|
|
2688
2615
|
*/
|
|
2689
2616
|
const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10;
|
|
2690
2617
|
/**
|
|
2691
|
-
*
|
|
2692
|
-
* history of sent and received messages so far.
|
|
2618
|
+
* Base class for various `ChatSession` classes that enables sending chat
|
|
2619
|
+
* messages and stores history of sent and received messages so far.
|
|
2693
2620
|
*
|
|
2694
2621
|
* @public
|
|
2695
2622
|
*/
|
|
2696
|
-
class
|
|
2697
|
-
constructor(apiSettings,
|
|
2698
|
-
this.model = model;
|
|
2699
|
-
this.chromeAdapter = chromeAdapter;
|
|
2623
|
+
class ChatSessionBase {
|
|
2624
|
+
constructor(apiSettings, params, requestOptions) {
|
|
2700
2625
|
this.params = params;
|
|
2701
2626
|
this.requestOptions = requestOptions;
|
|
2702
2627
|
this._history = [];
|
|
2703
2628
|
/**
|
|
2704
2629
|
* Ensures sequential execution of chat messages to maintain history order.
|
|
2705
2630
|
* Each call waits for the previous one to settle before proceeding.
|
|
2631
|
+
* @internal
|
|
2706
2632
|
*/
|
|
2707
2633
|
this._sendPromise = Promise.resolve();
|
|
2708
2634
|
this._apiSettings = apiSettings;
|
|
2709
|
-
if (params?.history) {
|
|
2710
|
-
validateChatHistory(params.history);
|
|
2711
|
-
this._history = params.history;
|
|
2712
|
-
}
|
|
2713
2635
|
}
|
|
2714
2636
|
/**
|
|
2715
2637
|
* Gets the chat history so far. Blocked prompts are not added to history.
|
|
@@ -2720,26 +2642,12 @@ class ChatSession {
|
|
|
2720
2642
|
await this._sendPromise;
|
|
2721
2643
|
return this._history;
|
|
2722
2644
|
}
|
|
2723
|
-
/**
|
|
2724
|
-
* Format Content into a request for generateContent or
|
|
2725
|
-
* generateContentStream.
|
|
2726
|
-
* @internal
|
|
2727
|
-
*/
|
|
2728
|
-
_formatRequest(incomingContent, tempHistory) {
|
|
2729
|
-
return {
|
|
2730
|
-
safetySettings: this.params?.safetySettings,
|
|
2731
|
-
generationConfig: this.params?.generationConfig,
|
|
2732
|
-
tools: this.params?.tools,
|
|
2733
|
-
toolConfig: this.params?.toolConfig,
|
|
2734
|
-
systemInstruction: this.params?.systemInstruction,
|
|
2735
|
-
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2736
|
-
};
|
|
2737
|
-
}
|
|
2738
2645
|
/**
|
|
2739
2646
|
* Sends a chat message and receives a non-streaming
|
|
2740
2647
|
* {@link GenerateContentResult}
|
|
2648
|
+
* @internal
|
|
2741
2649
|
*/
|
|
2742
|
-
async
|
|
2650
|
+
async _sendMessage(request, singleRequestOptions) {
|
|
2743
2651
|
let finalResult = {};
|
|
2744
2652
|
await this._sendPromise;
|
|
2745
2653
|
/**
|
|
@@ -2751,7 +2659,7 @@ class ChatSession {
|
|
|
2751
2659
|
this._sendPromise = this._sendPromise.then(async () => {
|
|
2752
2660
|
let functionCalls;
|
|
2753
2661
|
let functionCallTurnCount = 0;
|
|
2754
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2662
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2755
2663
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2756
2664
|
// Repeats until model returns a response with no function calls
|
|
2757
2665
|
// or until `functionCallMaxTurns` is met or exceeded.
|
|
@@ -2767,10 +2675,7 @@ class ChatSession {
|
|
|
2767
2675
|
}
|
|
2768
2676
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2769
2677
|
tempHistory.push(formattedContent);
|
|
2770
|
-
const result = await
|
|
2771
|
-
...this.requestOptions,
|
|
2772
|
-
...singleRequestOptions
|
|
2773
|
-
});
|
|
2678
|
+
const result = await this._callGenerateContent(formattedRequest, singleRequestOptions);
|
|
2774
2679
|
if (result) {
|
|
2775
2680
|
finalResult = result;
|
|
2776
2681
|
functionCalls = this._getCallableFunctionCalls(result.response);
|
|
@@ -2810,8 +2715,9 @@ class ChatSession {
|
|
|
2810
2715
|
* Sends a chat message and receives the response as a
|
|
2811
2716
|
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2812
2717
|
* and a response promise.
|
|
2718
|
+
* @internal
|
|
2813
2719
|
*/
|
|
2814
|
-
async
|
|
2720
|
+
async _sendMessageStream(request, singleRequestOptions) {
|
|
2815
2721
|
await this._sendPromise;
|
|
2816
2722
|
/**
|
|
2817
2723
|
* Temporarily store multiple turns for cases like automatic function
|
|
@@ -2822,7 +2728,7 @@ class ChatSession {
|
|
|
2822
2728
|
const callGenerateContentStream = async () => {
|
|
2823
2729
|
let functionCalls;
|
|
2824
2730
|
let functionCallTurnCount = 0;
|
|
2825
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2731
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2826
2732
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2827
2733
|
let result;
|
|
2828
2734
|
// Repeats until model returns a response with no function calls
|
|
@@ -2839,10 +2745,7 @@ class ChatSession {
|
|
|
2839
2745
|
}
|
|
2840
2746
|
tempHistory.push(formattedContent);
|
|
2841
2747
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2842
|
-
result = await
|
|
2843
|
-
...this.requestOptions,
|
|
2844
|
-
...singleRequestOptions
|
|
2845
|
-
});
|
|
2748
|
+
result = await this._callGenerateContentStream(formattedRequest, singleRequestOptions);
|
|
2846
2749
|
functionCalls = this._getCallableFunctionCalls(result.firstValue);
|
|
2847
2750
|
if (functionCalls &&
|
|
2848
2751
|
result.firstValue &&
|
|
@@ -2936,7 +2839,7 @@ class ChatSession {
|
|
|
2936
2839
|
* @internal
|
|
2937
2840
|
*/
|
|
2938
2841
|
async _callFunctionsAsNeeded(functionCalls) {
|
|
2939
|
-
const activeCallList =
|
|
2842
|
+
const activeCallList = [];
|
|
2940
2843
|
const promiseList = [];
|
|
2941
2844
|
const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
|
|
2942
2845
|
if (functionDeclarationsTool &&
|
|
@@ -2949,7 +2852,8 @@ class ChatSession {
|
|
|
2949
2852
|
wrappedError.stack = e.stack;
|
|
2950
2853
|
throw wrappedError;
|
|
2951
2854
|
});
|
|
2952
|
-
activeCallList.
|
|
2855
|
+
activeCallList.push({
|
|
2856
|
+
name: functionCall.name,
|
|
2953
2857
|
id: functionCall.id,
|
|
2954
2858
|
results
|
|
2955
2859
|
});
|
|
@@ -2959,12 +2863,16 @@ class ChatSession {
|
|
|
2959
2863
|
// Wait for promises to finish.
|
|
2960
2864
|
await Promise.all(promiseList);
|
|
2961
2865
|
const functionResponseParts = [];
|
|
2962
|
-
for (const
|
|
2866
|
+
for (const { name, id, results } of activeCallList) {
|
|
2867
|
+
const functionResponse = {
|
|
2868
|
+
name,
|
|
2869
|
+
response: await results
|
|
2870
|
+
};
|
|
2871
|
+
if (id) {
|
|
2872
|
+
functionResponse.id = id;
|
|
2873
|
+
}
|
|
2963
2874
|
functionResponseParts.push({
|
|
2964
|
-
functionResponse
|
|
2965
|
-
name,
|
|
2966
|
-
response: await callData.results
|
|
2967
|
-
}
|
|
2875
|
+
functionResponse
|
|
2968
2876
|
});
|
|
2969
2877
|
}
|
|
2970
2878
|
return functionResponseParts;
|
|
@@ -2975,6 +2883,182 @@ class ChatSession {
|
|
|
2975
2883
|
}
|
|
2976
2884
|
}
|
|
2977
2885
|
|
|
2886
|
+
/**
|
|
2887
|
+
* @license
|
|
2888
|
+
* Copyright 2024 Google LLC
|
|
2889
|
+
*
|
|
2890
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2891
|
+
* you may not use this file except in compliance with the License.
|
|
2892
|
+
* You may obtain a copy of the License at
|
|
2893
|
+
*
|
|
2894
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2895
|
+
*
|
|
2896
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2897
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2898
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2899
|
+
* See the License for the specific language governing permissions and
|
|
2900
|
+
* limitations under the License.
|
|
2901
|
+
*/
|
|
2902
|
+
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2903
|
+
const VALID_PART_FIELDS = [
|
|
2904
|
+
'text',
|
|
2905
|
+
'inlineData',
|
|
2906
|
+
'functionCall',
|
|
2907
|
+
'functionResponse',
|
|
2908
|
+
'thought',
|
|
2909
|
+
'thoughtSignature'
|
|
2910
|
+
];
|
|
2911
|
+
const VALID_PARTS_PER_ROLE = {
|
|
2912
|
+
user: ['text', 'inlineData'],
|
|
2913
|
+
function: ['functionResponse'],
|
|
2914
|
+
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2915
|
+
// System instructions shouldn't be in history anyway.
|
|
2916
|
+
system: ['text']
|
|
2917
|
+
};
|
|
2918
|
+
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2919
|
+
user: ['model'],
|
|
2920
|
+
function: ['model'],
|
|
2921
|
+
model: ['user', 'function'],
|
|
2922
|
+
// System instructions shouldn't be in history.
|
|
2923
|
+
system: []
|
|
2924
|
+
};
|
|
2925
|
+
function validateChatHistory(history) {
|
|
2926
|
+
let prevContent = null;
|
|
2927
|
+
for (const currContent of history) {
|
|
2928
|
+
const { role, parts } = currContent;
|
|
2929
|
+
if (!prevContent && role !== 'user') {
|
|
2930
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2931
|
+
}
|
|
2932
|
+
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2933
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2934
|
+
}
|
|
2935
|
+
if (!Array.isArray(parts)) {
|
|
2936
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2937
|
+
}
|
|
2938
|
+
if (parts.length === 0) {
|
|
2939
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2940
|
+
}
|
|
2941
|
+
const countFields = {
|
|
2942
|
+
text: 0,
|
|
2943
|
+
inlineData: 0,
|
|
2944
|
+
functionCall: 0,
|
|
2945
|
+
functionResponse: 0,
|
|
2946
|
+
thought: 0,
|
|
2947
|
+
thoughtSignature: 0,
|
|
2948
|
+
executableCode: 0,
|
|
2949
|
+
codeExecutionResult: 0
|
|
2950
|
+
};
|
|
2951
|
+
for (const part of parts) {
|
|
2952
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2953
|
+
if (key in part) {
|
|
2954
|
+
countFields[key] += 1;
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
}
|
|
2958
|
+
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2959
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2960
|
+
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2961
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
if (prevContent) {
|
|
2965
|
+
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2966
|
+
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2967
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
prevContent = currContent;
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
/**
|
|
2975
|
+
* @license
|
|
2976
|
+
* Copyright 2024 Google LLC
|
|
2977
|
+
*
|
|
2978
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2979
|
+
* you may not use this file except in compliance with the License.
|
|
2980
|
+
* You may obtain a copy of the License at
|
|
2981
|
+
*
|
|
2982
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2983
|
+
*
|
|
2984
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2985
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2986
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2987
|
+
* See the License for the specific language governing permissions and
|
|
2988
|
+
* limitations under the License.
|
|
2989
|
+
*/
|
|
2990
|
+
/**
|
|
2991
|
+
* ChatSession class that enables sending chat messages and stores
|
|
2992
|
+
* history of sent and received messages so far.
|
|
2993
|
+
*
|
|
2994
|
+
* @public
|
|
2995
|
+
*/
|
|
2996
|
+
class ChatSession extends ChatSessionBase {
|
|
2997
|
+
constructor(apiSettings, model, chromeAdapter, params, requestOptions) {
|
|
2998
|
+
super(apiSettings, params, requestOptions);
|
|
2999
|
+
this.model = model;
|
|
3000
|
+
this.chromeAdapter = chromeAdapter;
|
|
3001
|
+
this.params = params;
|
|
3002
|
+
this.requestOptions = requestOptions;
|
|
3003
|
+
if (params?.history) {
|
|
3004
|
+
validateChatHistory(params.history);
|
|
3005
|
+
this._history = params.history;
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
/**
|
|
3009
|
+
* Format Content into a request for generateContent or
|
|
3010
|
+
* generateContentStream.
|
|
3011
|
+
* @internal
|
|
3012
|
+
*/
|
|
3013
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3014
|
+
return {
|
|
3015
|
+
safetySettings: this.params?.safetySettings,
|
|
3016
|
+
generationConfig: this.params?.generationConfig,
|
|
3017
|
+
tools: this.params?.tools,
|
|
3018
|
+
toolConfig: this.params?.toolConfig,
|
|
3019
|
+
systemInstruction: this.params?.systemInstruction,
|
|
3020
|
+
contents: [...this._history, ...tempHistory, incomingContent]
|
|
3021
|
+
};
|
|
3022
|
+
}
|
|
3023
|
+
/**
|
|
3024
|
+
* Calls default generateContent() (versus a specialized one like
|
|
3025
|
+
* templateGenerateContent).
|
|
3026
|
+
* @internal
|
|
3027
|
+
*/
|
|
3028
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3029
|
+
return generateContent(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
3030
|
+
...this.requestOptions,
|
|
3031
|
+
...singleRequestOptions
|
|
3032
|
+
});
|
|
3033
|
+
}
|
|
3034
|
+
/**
|
|
3035
|
+
* Calls default generateContentStream() (versus a specialized one like
|
|
3036
|
+
* templateGenerateContentStream).
|
|
3037
|
+
* @internal
|
|
3038
|
+
*/
|
|
3039
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3040
|
+
return generateContentStream(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
3041
|
+
...this.requestOptions,
|
|
3042
|
+
...singleRequestOptions
|
|
3043
|
+
});
|
|
3044
|
+
}
|
|
3045
|
+
/**
|
|
3046
|
+
* Sends a chat message and receives a non-streaming
|
|
3047
|
+
* {@link GenerateContentResult}
|
|
3048
|
+
*/
|
|
3049
|
+
async sendMessage(request, singleRequestOptions) {
|
|
3050
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
3051
|
+
}
|
|
3052
|
+
/**
|
|
3053
|
+
* Sends a chat message and receives the response as a
|
|
3054
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
3055
|
+
* and a response promise.
|
|
3056
|
+
*/
|
|
3057
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
3058
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
|
|
2978
3062
|
/**
|
|
2979
3063
|
* @license
|
|
2980
3064
|
* Copyright 2024 Google LLC
|
|
@@ -3142,8 +3226,9 @@ function validateGenerationConfig(generationConfig) {
|
|
|
3142
3226
|
}
|
|
3143
3227
|
if ((generationConfig.responseSchema != null ||
|
|
3144
3228
|
generationConfig.responseJsonSchema != null) &&
|
|
3145
|
-
generationConfig.responseMimeType) {
|
|
3146
|
-
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set
|
|
3229
|
+
generationConfig.responseMimeType !== 'application/json') {
|
|
3230
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set to "application/json" if` +
|
|
3231
|
+
` responseSchema or responseJsonSchema are set.`);
|
|
3147
3232
|
}
|
|
3148
3233
|
}
|
|
3149
3234
|
|
|
@@ -3575,6 +3660,11 @@ class LiveGenerativeModel extends AIModel {
|
|
|
3575
3660
|
* }
|
|
3576
3661
|
* ```
|
|
3577
3662
|
*
|
|
3663
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3664
|
+
* early as June 2026. As a replacement, you can
|
|
3665
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3666
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3667
|
+
*
|
|
3578
3668
|
* @public
|
|
3579
3669
|
*/
|
|
3580
3670
|
class ImagenModel extends AIModel {
|
|
@@ -3818,6 +3908,112 @@ class WebSocketHandlerImpl {
|
|
|
3818
3908
|
}
|
|
3819
3909
|
}
|
|
3820
3910
|
|
|
3911
|
+
/**
|
|
3912
|
+
* @license
|
|
3913
|
+
* Copyright 2026 Google LLC
|
|
3914
|
+
*
|
|
3915
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3916
|
+
* you may not use this file except in compliance with the License.
|
|
3917
|
+
* You may obtain a copy of the License at
|
|
3918
|
+
*
|
|
3919
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3920
|
+
*
|
|
3921
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3922
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3923
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3924
|
+
* See the License for the specific language governing permissions and
|
|
3925
|
+
* limitations under the License.
|
|
3926
|
+
*/
|
|
3927
|
+
/**
|
|
3928
|
+
* `ChatSession` class for use with server prompt templates that
|
|
3929
|
+
* enables sending chat messages and stores history of sent and
|
|
3930
|
+
* received messages so far.
|
|
3931
|
+
*
|
|
3932
|
+
* @beta
|
|
3933
|
+
*/
|
|
3934
|
+
class TemplateChatSession extends ChatSessionBase {
|
|
3935
|
+
constructor(apiSettings, params, requestOptions) {
|
|
3936
|
+
super(apiSettings, params, requestOptions);
|
|
3937
|
+
this.params = params;
|
|
3938
|
+
this.requestOptions = requestOptions;
|
|
3939
|
+
if (params.history) {
|
|
3940
|
+
validateChatHistory(params.history);
|
|
3941
|
+
this._history = params.history;
|
|
3942
|
+
}
|
|
3943
|
+
}
|
|
3944
|
+
/**
|
|
3945
|
+
* Format the internal state to the body payload for `templateGenerateContent`.
|
|
3946
|
+
* @internal
|
|
3947
|
+
*/
|
|
3948
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3949
|
+
const request = {
|
|
3950
|
+
history: [...this._history, ...tempHistory, incomingContent]
|
|
3951
|
+
};
|
|
3952
|
+
if (this.params.templateVariables) {
|
|
3953
|
+
request.inputs = this.params.templateVariables;
|
|
3954
|
+
}
|
|
3955
|
+
if (this.params.tools) {
|
|
3956
|
+
request.tools = this.params.tools?.map(tool => {
|
|
3957
|
+
if (tool.functionDeclarations) {
|
|
3958
|
+
return {
|
|
3959
|
+
templateFunctions: tool.functionDeclarations.map(declaration => {
|
|
3960
|
+
if (declaration.parameters) {
|
|
3961
|
+
const newDeclaration = { ...declaration };
|
|
3962
|
+
delete newDeclaration.parameters;
|
|
3963
|
+
newDeclaration.inputSchema = declaration.parameters;
|
|
3964
|
+
return newDeclaration;
|
|
3965
|
+
}
|
|
3966
|
+
return declaration;
|
|
3967
|
+
})
|
|
3968
|
+
};
|
|
3969
|
+
}
|
|
3970
|
+
return tool;
|
|
3971
|
+
});
|
|
3972
|
+
}
|
|
3973
|
+
if (this.params.toolConfig) {
|
|
3974
|
+
request.toolConfig = this.params.toolConfig;
|
|
3975
|
+
}
|
|
3976
|
+
return request;
|
|
3977
|
+
}
|
|
3978
|
+
/**
|
|
3979
|
+
* Calls the specific templateGenerateContent() function needed for
|
|
3980
|
+
* this specialized TemplateChatSession.
|
|
3981
|
+
* @internal
|
|
3982
|
+
*/
|
|
3983
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3984
|
+
return templateGenerateContent(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3985
|
+
...this.requestOptions,
|
|
3986
|
+
...singleRequestOptions
|
|
3987
|
+
});
|
|
3988
|
+
}
|
|
3989
|
+
/**
|
|
3990
|
+
* Calls the specific templateGenerateContentStream() function needed for
|
|
3991
|
+
* this specialized TemplateChatSession.
|
|
3992
|
+
* @internal
|
|
3993
|
+
*/
|
|
3994
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3995
|
+
return templateGenerateContentStream(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3996
|
+
...this.requestOptions,
|
|
3997
|
+
...singleRequestOptions
|
|
3998
|
+
});
|
|
3999
|
+
}
|
|
4000
|
+
/**
|
|
4001
|
+
* Sends a chat message and receives a non-streaming
|
|
4002
|
+
* {@link GenerateContentResult}
|
|
4003
|
+
*/
|
|
4004
|
+
async sendMessage(request, singleRequestOptions) {
|
|
4005
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
4006
|
+
}
|
|
4007
|
+
/**
|
|
4008
|
+
* Sends a chat message and receives the response as a
|
|
4009
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
4010
|
+
* and a response promise.
|
|
4011
|
+
*/
|
|
4012
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
4013
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
4014
|
+
}
|
|
4015
|
+
}
|
|
4016
|
+
|
|
3821
4017
|
/**
|
|
3822
4018
|
* @license
|
|
3823
4019
|
* Copyright 2025 Google LLC
|
|
@@ -3883,6 +4079,18 @@ class TemplateGenerativeModel {
|
|
|
3883
4079
|
...singleRequestOptions
|
|
3884
4080
|
});
|
|
3885
4081
|
}
|
|
4082
|
+
/**
|
|
4083
|
+
* Starts a {@link TemplateChatSession} that will use this template to
|
|
4084
|
+
* respond to messages.
|
|
4085
|
+
*
|
|
4086
|
+
* @param params - Configurations for the chat, including the template
|
|
4087
|
+
* ID and input variables.
|
|
4088
|
+
*
|
|
4089
|
+
* @beta
|
|
4090
|
+
*/
|
|
4091
|
+
startChat(params) {
|
|
4092
|
+
return new TemplateChatSession(this._apiSettings, params, this.requestOptions);
|
|
4093
|
+
}
|
|
3886
4094
|
}
|
|
3887
4095
|
|
|
3888
4096
|
/**
|
|
@@ -3906,7 +4114,10 @@ class TemplateGenerativeModel {
|
|
|
3906
4114
|
*
|
|
3907
4115
|
* This class should only be instantiated with {@link getTemplateImagenModel}.
|
|
3908
4116
|
*
|
|
3909
|
-
* @
|
|
4117
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4118
|
+
* early as June 2026. As a replacement, you can
|
|
4119
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4120
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3910
4121
|
*/
|
|
3911
4122
|
class TemplateImagenModel {
|
|
3912
4123
|
/**
|
|
@@ -4215,6 +4426,11 @@ class AnyOfSchema extends Schema {
|
|
|
4215
4426
|
* }
|
|
4216
4427
|
* ```
|
|
4217
4428
|
*
|
|
4429
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4430
|
+
* early as June 2026. As a replacement, you can
|
|
4431
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4432
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4433
|
+
*
|
|
4218
4434
|
* @public
|
|
4219
4435
|
*/
|
|
4220
4436
|
class ImagenImageFormat {
|
|
@@ -4715,6 +4931,11 @@ function getGenerativeModel(ai, modelParams, requestOptions) {
|
|
|
4715
4931
|
*
|
|
4716
4932
|
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
|
|
4717
4933
|
*
|
|
4934
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4935
|
+
* early as June 2026. As a replacement, you can
|
|
4936
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4937
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4938
|
+
*
|
|
4718
4939
|
* @param ai - An {@link AI} instance.
|
|
4719
4940
|
* @param modelParams - Parameters to use when making Imagen requests.
|
|
4720
4941
|
* @param requestOptions - Additional options to use when making requests.
|
|
@@ -4765,10 +4986,13 @@ function getTemplateGenerativeModel(ai, requestOptions) {
|
|
|
4765
4986
|
* Returns a {@link TemplateImagenModel} class for executing server-side
|
|
4766
4987
|
* Imagen templates.
|
|
4767
4988
|
*
|
|
4989
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4990
|
+
* early as June 2026. As a replacement, you can
|
|
4991
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4992
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4993
|
+
*
|
|
4768
4994
|
* @param ai - An {@link AI} instance.
|
|
4769
4995
|
* @param requestOptions - Additional options to use when making requests.
|
|
4770
|
-
*
|
|
4771
|
-
* @beta
|
|
4772
4996
|
*/
|
|
4773
4997
|
function getTemplateImagenModel(ai, requestOptions) {
|
|
4774
4998
|
return new TemplateImagenModel(ai, requestOptions);
|
|
@@ -4787,5 +5011,5 @@ function registerAI() {
|
|
|
4787
5011
|
}
|
|
4788
5012
|
registerAI();
|
|
4789
5013
|
|
|
4790
|
-
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, InferenceSource, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, TemplateGenerativeModel, TemplateImagenModel, ThinkingLevel, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, getTemplateGenerativeModel, getTemplateImagenModel, startAudioConversation };
|
|
5014
|
+
export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, ChatSessionBase, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, InferenceSource, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, TemplateChatSession, TemplateGenerativeModel, TemplateImagenModel, ThinkingLevel, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, getTemplateGenerativeModel, getTemplateImagenModel, startAudioConversation };
|
|
4791
5015
|
//# sourceMappingURL=index.esm.js.map
|