@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/index.cjs.js
CHANGED
|
@@ -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.
|
|
11
|
+
var version = "2.11.0-20260408221811";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* @license
|
|
@@ -609,6 +609,11 @@ const SchemaType = {
|
|
|
609
609
|
* and the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#safety-filters | Responsible AI and usage guidelines}
|
|
610
610
|
* for more details.
|
|
611
611
|
*
|
|
612
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
613
|
+
* early as June 2026. As a replacement, you can
|
|
614
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
615
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
616
|
+
*
|
|
612
617
|
* @public
|
|
613
618
|
*/
|
|
614
619
|
const ImagenSafetyFilterLevel = {
|
|
@@ -638,6 +643,11 @@ const ImagenSafetyFilterLevel = {
|
|
|
638
643
|
* See the <a href="http://firebase.google.com/docs/vertex-ai/generate-images">personGeneration</a>
|
|
639
644
|
* documentation for more details.
|
|
640
645
|
*
|
|
646
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
647
|
+
* early as June 2026. As a replacement, you can
|
|
648
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
649
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
650
|
+
*
|
|
641
651
|
* @public
|
|
642
652
|
*/
|
|
643
653
|
const ImagenPersonFilterLevel = {
|
|
@@ -671,6 +681,11 @@ const ImagenPersonFilterLevel = {
|
|
|
671
681
|
* See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
|
|
672
682
|
* for more details and examples of the supported aspect ratios.
|
|
673
683
|
*
|
|
684
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
685
|
+
* early as June 2026. As a replacement, you can
|
|
686
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
687
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
688
|
+
*
|
|
674
689
|
* @public
|
|
675
690
|
*/
|
|
676
691
|
const ImagenAspectRatio = {
|
|
@@ -2579,95 +2594,7 @@ function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, n
|
|
|
2579
2594
|
|
|
2580
2595
|
/**
|
|
2581
2596
|
* @license
|
|
2582
|
-
* Copyright
|
|
2583
|
-
*
|
|
2584
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2585
|
-
* you may not use this file except in compliance with the License.
|
|
2586
|
-
* You may obtain a copy of the License at
|
|
2587
|
-
*
|
|
2588
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2589
|
-
*
|
|
2590
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2591
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2592
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2593
|
-
* See the License for the specific language governing permissions and
|
|
2594
|
-
* limitations under the License.
|
|
2595
|
-
*/
|
|
2596
|
-
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2597
|
-
const VALID_PART_FIELDS = [
|
|
2598
|
-
'text',
|
|
2599
|
-
'inlineData',
|
|
2600
|
-
'functionCall',
|
|
2601
|
-
'functionResponse',
|
|
2602
|
-
'thought',
|
|
2603
|
-
'thoughtSignature'
|
|
2604
|
-
];
|
|
2605
|
-
const VALID_PARTS_PER_ROLE = {
|
|
2606
|
-
user: ['text', 'inlineData'],
|
|
2607
|
-
function: ['functionResponse'],
|
|
2608
|
-
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2609
|
-
// System instructions shouldn't be in history anyway.
|
|
2610
|
-
system: ['text']
|
|
2611
|
-
};
|
|
2612
|
-
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2613
|
-
user: ['model'],
|
|
2614
|
-
function: ['model'],
|
|
2615
|
-
model: ['user', 'function'],
|
|
2616
|
-
// System instructions shouldn't be in history.
|
|
2617
|
-
system: []
|
|
2618
|
-
};
|
|
2619
|
-
function validateChatHistory(history) {
|
|
2620
|
-
let prevContent = null;
|
|
2621
|
-
for (const currContent of history) {
|
|
2622
|
-
const { role, parts } = currContent;
|
|
2623
|
-
if (!prevContent && role !== 'user') {
|
|
2624
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2625
|
-
}
|
|
2626
|
-
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2627
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2628
|
-
}
|
|
2629
|
-
if (!Array.isArray(parts)) {
|
|
2630
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2631
|
-
}
|
|
2632
|
-
if (parts.length === 0) {
|
|
2633
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2634
|
-
}
|
|
2635
|
-
const countFields = {
|
|
2636
|
-
text: 0,
|
|
2637
|
-
inlineData: 0,
|
|
2638
|
-
functionCall: 0,
|
|
2639
|
-
functionResponse: 0,
|
|
2640
|
-
thought: 0,
|
|
2641
|
-
thoughtSignature: 0,
|
|
2642
|
-
executableCode: 0,
|
|
2643
|
-
codeExecutionResult: 0
|
|
2644
|
-
};
|
|
2645
|
-
for (const part of parts) {
|
|
2646
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2647
|
-
if (key in part) {
|
|
2648
|
-
countFields[key] += 1;
|
|
2649
|
-
}
|
|
2650
|
-
}
|
|
2651
|
-
}
|
|
2652
|
-
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2653
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2654
|
-
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2655
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2656
|
-
}
|
|
2657
|
-
}
|
|
2658
|
-
if (prevContent) {
|
|
2659
|
-
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2660
|
-
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2661
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2662
|
-
}
|
|
2663
|
-
}
|
|
2664
|
-
prevContent = currContent;
|
|
2665
|
-
}
|
|
2666
|
-
}
|
|
2667
|
-
|
|
2668
|
-
/**
|
|
2669
|
-
* @license
|
|
2670
|
-
* Copyright 2024 Google LLC
|
|
2597
|
+
* Copyright 2026 Google LLC
|
|
2671
2598
|
*
|
|
2672
2599
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2673
2600
|
* you may not use this file except in compliance with the License.
|
|
@@ -2692,28 +2619,23 @@ const SILENT_ERROR = 'SILENT_ERROR';
|
|
|
2692
2619
|
*/
|
|
2693
2620
|
const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10;
|
|
2694
2621
|
/**
|
|
2695
|
-
*
|
|
2696
|
-
* history of sent and received messages so far.
|
|
2622
|
+
* Base class for various `ChatSession` classes that enables sending chat
|
|
2623
|
+
* messages and stores history of sent and received messages so far.
|
|
2697
2624
|
*
|
|
2698
2625
|
* @public
|
|
2699
2626
|
*/
|
|
2700
|
-
class
|
|
2701
|
-
constructor(apiSettings,
|
|
2702
|
-
this.model = model;
|
|
2703
|
-
this.chromeAdapter = chromeAdapter;
|
|
2627
|
+
class ChatSessionBase {
|
|
2628
|
+
constructor(apiSettings, params, requestOptions) {
|
|
2704
2629
|
this.params = params;
|
|
2705
2630
|
this.requestOptions = requestOptions;
|
|
2706
2631
|
this._history = [];
|
|
2707
2632
|
/**
|
|
2708
2633
|
* Ensures sequential execution of chat messages to maintain history order.
|
|
2709
2634
|
* Each call waits for the previous one to settle before proceeding.
|
|
2635
|
+
* @internal
|
|
2710
2636
|
*/
|
|
2711
2637
|
this._sendPromise = Promise.resolve();
|
|
2712
2638
|
this._apiSettings = apiSettings;
|
|
2713
|
-
if (params?.history) {
|
|
2714
|
-
validateChatHistory(params.history);
|
|
2715
|
-
this._history = params.history;
|
|
2716
|
-
}
|
|
2717
2639
|
}
|
|
2718
2640
|
/**
|
|
2719
2641
|
* Gets the chat history so far. Blocked prompts are not added to history.
|
|
@@ -2724,26 +2646,12 @@ class ChatSession {
|
|
|
2724
2646
|
await this._sendPromise;
|
|
2725
2647
|
return this._history;
|
|
2726
2648
|
}
|
|
2727
|
-
/**
|
|
2728
|
-
* Format Content into a request for generateContent or
|
|
2729
|
-
* generateContentStream.
|
|
2730
|
-
* @internal
|
|
2731
|
-
*/
|
|
2732
|
-
_formatRequest(incomingContent, tempHistory) {
|
|
2733
|
-
return {
|
|
2734
|
-
safetySettings: this.params?.safetySettings,
|
|
2735
|
-
generationConfig: this.params?.generationConfig,
|
|
2736
|
-
tools: this.params?.tools,
|
|
2737
|
-
toolConfig: this.params?.toolConfig,
|
|
2738
|
-
systemInstruction: this.params?.systemInstruction,
|
|
2739
|
-
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2740
|
-
};
|
|
2741
|
-
}
|
|
2742
2649
|
/**
|
|
2743
2650
|
* Sends a chat message and receives a non-streaming
|
|
2744
2651
|
* {@link GenerateContentResult}
|
|
2652
|
+
* @internal
|
|
2745
2653
|
*/
|
|
2746
|
-
async
|
|
2654
|
+
async _sendMessage(request, singleRequestOptions) {
|
|
2747
2655
|
let finalResult = {};
|
|
2748
2656
|
await this._sendPromise;
|
|
2749
2657
|
/**
|
|
@@ -2755,7 +2663,7 @@ class ChatSession {
|
|
|
2755
2663
|
this._sendPromise = this._sendPromise.then(async () => {
|
|
2756
2664
|
let functionCalls;
|
|
2757
2665
|
let functionCallTurnCount = 0;
|
|
2758
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2666
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2759
2667
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2760
2668
|
// Repeats until model returns a response with no function calls
|
|
2761
2669
|
// or until `functionCallMaxTurns` is met or exceeded.
|
|
@@ -2771,10 +2679,7 @@ class ChatSession {
|
|
|
2771
2679
|
}
|
|
2772
2680
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2773
2681
|
tempHistory.push(formattedContent);
|
|
2774
|
-
const result = await
|
|
2775
|
-
...this.requestOptions,
|
|
2776
|
-
...singleRequestOptions
|
|
2777
|
-
});
|
|
2682
|
+
const result = await this._callGenerateContent(formattedRequest, singleRequestOptions);
|
|
2778
2683
|
if (result) {
|
|
2779
2684
|
finalResult = result;
|
|
2780
2685
|
functionCalls = this._getCallableFunctionCalls(result.response);
|
|
@@ -2814,8 +2719,9 @@ class ChatSession {
|
|
|
2814
2719
|
* Sends a chat message and receives the response as a
|
|
2815
2720
|
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2816
2721
|
* and a response promise.
|
|
2722
|
+
* @internal
|
|
2817
2723
|
*/
|
|
2818
|
-
async
|
|
2724
|
+
async _sendMessageStream(request, singleRequestOptions) {
|
|
2819
2725
|
await this._sendPromise;
|
|
2820
2726
|
/**
|
|
2821
2727
|
* Temporarily store multiple turns for cases like automatic function
|
|
@@ -2826,7 +2732,7 @@ class ChatSession {
|
|
|
2826
2732
|
const callGenerateContentStream = async () => {
|
|
2827
2733
|
let functionCalls;
|
|
2828
2734
|
let functionCallTurnCount = 0;
|
|
2829
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2735
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2830
2736
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2831
2737
|
let result;
|
|
2832
2738
|
// Repeats until model returns a response with no function calls
|
|
@@ -2843,10 +2749,7 @@ class ChatSession {
|
|
|
2843
2749
|
}
|
|
2844
2750
|
tempHistory.push(formattedContent);
|
|
2845
2751
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2846
|
-
result = await
|
|
2847
|
-
...this.requestOptions,
|
|
2848
|
-
...singleRequestOptions
|
|
2849
|
-
});
|
|
2752
|
+
result = await this._callGenerateContentStream(formattedRequest, singleRequestOptions);
|
|
2850
2753
|
functionCalls = this._getCallableFunctionCalls(result.firstValue);
|
|
2851
2754
|
if (functionCalls &&
|
|
2852
2755
|
result.firstValue &&
|
|
@@ -2940,7 +2843,7 @@ class ChatSession {
|
|
|
2940
2843
|
* @internal
|
|
2941
2844
|
*/
|
|
2942
2845
|
async _callFunctionsAsNeeded(functionCalls) {
|
|
2943
|
-
const activeCallList =
|
|
2846
|
+
const activeCallList = [];
|
|
2944
2847
|
const promiseList = [];
|
|
2945
2848
|
const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
|
|
2946
2849
|
if (functionDeclarationsTool &&
|
|
@@ -2953,7 +2856,8 @@ class ChatSession {
|
|
|
2953
2856
|
wrappedError.stack = e.stack;
|
|
2954
2857
|
throw wrappedError;
|
|
2955
2858
|
});
|
|
2956
|
-
activeCallList.
|
|
2859
|
+
activeCallList.push({
|
|
2860
|
+
name: functionCall.name,
|
|
2957
2861
|
id: functionCall.id,
|
|
2958
2862
|
results
|
|
2959
2863
|
});
|
|
@@ -2963,12 +2867,16 @@ class ChatSession {
|
|
|
2963
2867
|
// Wait for promises to finish.
|
|
2964
2868
|
await Promise.all(promiseList);
|
|
2965
2869
|
const functionResponseParts = [];
|
|
2966
|
-
for (const
|
|
2870
|
+
for (const { name, id, results } of activeCallList) {
|
|
2871
|
+
const functionResponse = {
|
|
2872
|
+
name,
|
|
2873
|
+
response: await results
|
|
2874
|
+
};
|
|
2875
|
+
if (id) {
|
|
2876
|
+
functionResponse.id = id;
|
|
2877
|
+
}
|
|
2967
2878
|
functionResponseParts.push({
|
|
2968
|
-
functionResponse
|
|
2969
|
-
name,
|
|
2970
|
-
response: await callData.results
|
|
2971
|
-
}
|
|
2879
|
+
functionResponse
|
|
2972
2880
|
});
|
|
2973
2881
|
}
|
|
2974
2882
|
return functionResponseParts;
|
|
@@ -2979,6 +2887,182 @@ class ChatSession {
|
|
|
2979
2887
|
}
|
|
2980
2888
|
}
|
|
2981
2889
|
|
|
2890
|
+
/**
|
|
2891
|
+
* @license
|
|
2892
|
+
* Copyright 2024 Google LLC
|
|
2893
|
+
*
|
|
2894
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2895
|
+
* you may not use this file except in compliance with the License.
|
|
2896
|
+
* You may obtain a copy of the License at
|
|
2897
|
+
*
|
|
2898
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2899
|
+
*
|
|
2900
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2901
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2902
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2903
|
+
* See the License for the specific language governing permissions and
|
|
2904
|
+
* limitations under the License.
|
|
2905
|
+
*/
|
|
2906
|
+
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2907
|
+
const VALID_PART_FIELDS = [
|
|
2908
|
+
'text',
|
|
2909
|
+
'inlineData',
|
|
2910
|
+
'functionCall',
|
|
2911
|
+
'functionResponse',
|
|
2912
|
+
'thought',
|
|
2913
|
+
'thoughtSignature'
|
|
2914
|
+
];
|
|
2915
|
+
const VALID_PARTS_PER_ROLE = {
|
|
2916
|
+
user: ['text', 'inlineData'],
|
|
2917
|
+
function: ['functionResponse'],
|
|
2918
|
+
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2919
|
+
// System instructions shouldn't be in history anyway.
|
|
2920
|
+
system: ['text']
|
|
2921
|
+
};
|
|
2922
|
+
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2923
|
+
user: ['model'],
|
|
2924
|
+
function: ['model'],
|
|
2925
|
+
model: ['user', 'function'],
|
|
2926
|
+
// System instructions shouldn't be in history.
|
|
2927
|
+
system: []
|
|
2928
|
+
};
|
|
2929
|
+
function validateChatHistory(history) {
|
|
2930
|
+
let prevContent = null;
|
|
2931
|
+
for (const currContent of history) {
|
|
2932
|
+
const { role, parts } = currContent;
|
|
2933
|
+
if (!prevContent && role !== 'user') {
|
|
2934
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2935
|
+
}
|
|
2936
|
+
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2937
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2938
|
+
}
|
|
2939
|
+
if (!Array.isArray(parts)) {
|
|
2940
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2941
|
+
}
|
|
2942
|
+
if (parts.length === 0) {
|
|
2943
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2944
|
+
}
|
|
2945
|
+
const countFields = {
|
|
2946
|
+
text: 0,
|
|
2947
|
+
inlineData: 0,
|
|
2948
|
+
functionCall: 0,
|
|
2949
|
+
functionResponse: 0,
|
|
2950
|
+
thought: 0,
|
|
2951
|
+
thoughtSignature: 0,
|
|
2952
|
+
executableCode: 0,
|
|
2953
|
+
codeExecutionResult: 0
|
|
2954
|
+
};
|
|
2955
|
+
for (const part of parts) {
|
|
2956
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2957
|
+
if (key in part) {
|
|
2958
|
+
countFields[key] += 1;
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2963
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2964
|
+
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2965
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
if (prevContent) {
|
|
2969
|
+
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2970
|
+
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2971
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2972
|
+
}
|
|
2973
|
+
}
|
|
2974
|
+
prevContent = currContent;
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
|
|
2978
|
+
/**
|
|
2979
|
+
* @license
|
|
2980
|
+
* Copyright 2024 Google LLC
|
|
2981
|
+
*
|
|
2982
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2983
|
+
* you may not use this file except in compliance with the License.
|
|
2984
|
+
* You may obtain a copy of the License at
|
|
2985
|
+
*
|
|
2986
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2987
|
+
*
|
|
2988
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2989
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2990
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2991
|
+
* See the License for the specific language governing permissions and
|
|
2992
|
+
* limitations under the License.
|
|
2993
|
+
*/
|
|
2994
|
+
/**
|
|
2995
|
+
* ChatSession class that enables sending chat messages and stores
|
|
2996
|
+
* history of sent and received messages so far.
|
|
2997
|
+
*
|
|
2998
|
+
* @public
|
|
2999
|
+
*/
|
|
3000
|
+
class ChatSession extends ChatSessionBase {
|
|
3001
|
+
constructor(apiSettings, model, chromeAdapter, params, requestOptions) {
|
|
3002
|
+
super(apiSettings, params, requestOptions);
|
|
3003
|
+
this.model = model;
|
|
3004
|
+
this.chromeAdapter = chromeAdapter;
|
|
3005
|
+
this.params = params;
|
|
3006
|
+
this.requestOptions = requestOptions;
|
|
3007
|
+
if (params?.history) {
|
|
3008
|
+
validateChatHistory(params.history);
|
|
3009
|
+
this._history = params.history;
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
/**
|
|
3013
|
+
* Format Content into a request for generateContent or
|
|
3014
|
+
* generateContentStream.
|
|
3015
|
+
* @internal
|
|
3016
|
+
*/
|
|
3017
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3018
|
+
return {
|
|
3019
|
+
safetySettings: this.params?.safetySettings,
|
|
3020
|
+
generationConfig: this.params?.generationConfig,
|
|
3021
|
+
tools: this.params?.tools,
|
|
3022
|
+
toolConfig: this.params?.toolConfig,
|
|
3023
|
+
systemInstruction: this.params?.systemInstruction,
|
|
3024
|
+
contents: [...this._history, ...tempHistory, incomingContent]
|
|
3025
|
+
};
|
|
3026
|
+
}
|
|
3027
|
+
/**
|
|
3028
|
+
* Calls default generateContent() (versus a specialized one like
|
|
3029
|
+
* templateGenerateContent).
|
|
3030
|
+
* @internal
|
|
3031
|
+
*/
|
|
3032
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3033
|
+
return generateContent(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
3034
|
+
...this.requestOptions,
|
|
3035
|
+
...singleRequestOptions
|
|
3036
|
+
});
|
|
3037
|
+
}
|
|
3038
|
+
/**
|
|
3039
|
+
* Calls default generateContentStream() (versus a specialized one like
|
|
3040
|
+
* templateGenerateContentStream).
|
|
3041
|
+
* @internal
|
|
3042
|
+
*/
|
|
3043
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3044
|
+
return generateContentStream(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
3045
|
+
...this.requestOptions,
|
|
3046
|
+
...singleRequestOptions
|
|
3047
|
+
});
|
|
3048
|
+
}
|
|
3049
|
+
/**
|
|
3050
|
+
* Sends a chat message and receives a non-streaming
|
|
3051
|
+
* {@link GenerateContentResult}
|
|
3052
|
+
*/
|
|
3053
|
+
async sendMessage(request, singleRequestOptions) {
|
|
3054
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
3055
|
+
}
|
|
3056
|
+
/**
|
|
3057
|
+
* Sends a chat message and receives the response as a
|
|
3058
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
3059
|
+
* and a response promise.
|
|
3060
|
+
*/
|
|
3061
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
3062
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
3063
|
+
}
|
|
3064
|
+
}
|
|
3065
|
+
|
|
2982
3066
|
/**
|
|
2983
3067
|
* @license
|
|
2984
3068
|
* Copyright 2024 Google LLC
|
|
@@ -3146,8 +3230,9 @@ function validateGenerationConfig(generationConfig) {
|
|
|
3146
3230
|
}
|
|
3147
3231
|
if ((generationConfig.responseSchema != null ||
|
|
3148
3232
|
generationConfig.responseJsonSchema != null) &&
|
|
3149
|
-
generationConfig.responseMimeType) {
|
|
3150
|
-
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set
|
|
3233
|
+
generationConfig.responseMimeType !== 'application/json') {
|
|
3234
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set to "application/json" if` +
|
|
3235
|
+
` responseSchema or responseJsonSchema are set.`);
|
|
3151
3236
|
}
|
|
3152
3237
|
}
|
|
3153
3238
|
|
|
@@ -3579,6 +3664,11 @@ class LiveGenerativeModel extends AIModel {
|
|
|
3579
3664
|
* }
|
|
3580
3665
|
* ```
|
|
3581
3666
|
*
|
|
3667
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3668
|
+
* early as June 2026. As a replacement, you can
|
|
3669
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3670
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3671
|
+
*
|
|
3582
3672
|
* @public
|
|
3583
3673
|
*/
|
|
3584
3674
|
class ImagenModel extends AIModel {
|
|
@@ -3822,6 +3912,112 @@ class WebSocketHandlerImpl {
|
|
|
3822
3912
|
}
|
|
3823
3913
|
}
|
|
3824
3914
|
|
|
3915
|
+
/**
|
|
3916
|
+
* @license
|
|
3917
|
+
* Copyright 2026 Google LLC
|
|
3918
|
+
*
|
|
3919
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3920
|
+
* you may not use this file except in compliance with the License.
|
|
3921
|
+
* You may obtain a copy of the License at
|
|
3922
|
+
*
|
|
3923
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3924
|
+
*
|
|
3925
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3926
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3927
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3928
|
+
* See the License for the specific language governing permissions and
|
|
3929
|
+
* limitations under the License.
|
|
3930
|
+
*/
|
|
3931
|
+
/**
|
|
3932
|
+
* `ChatSession` class for use with server prompt templates that
|
|
3933
|
+
* enables sending chat messages and stores history of sent and
|
|
3934
|
+
* received messages so far.
|
|
3935
|
+
*
|
|
3936
|
+
* @beta
|
|
3937
|
+
*/
|
|
3938
|
+
class TemplateChatSession extends ChatSessionBase {
|
|
3939
|
+
constructor(apiSettings, params, requestOptions) {
|
|
3940
|
+
super(apiSettings, params, requestOptions);
|
|
3941
|
+
this.params = params;
|
|
3942
|
+
this.requestOptions = requestOptions;
|
|
3943
|
+
if (params.history) {
|
|
3944
|
+
validateChatHistory(params.history);
|
|
3945
|
+
this._history = params.history;
|
|
3946
|
+
}
|
|
3947
|
+
}
|
|
3948
|
+
/**
|
|
3949
|
+
* Format the internal state to the body payload for `templateGenerateContent`.
|
|
3950
|
+
* @internal
|
|
3951
|
+
*/
|
|
3952
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3953
|
+
const request = {
|
|
3954
|
+
history: [...this._history, ...tempHistory, incomingContent]
|
|
3955
|
+
};
|
|
3956
|
+
if (this.params.templateVariables) {
|
|
3957
|
+
request.inputs = this.params.templateVariables;
|
|
3958
|
+
}
|
|
3959
|
+
if (this.params.tools) {
|
|
3960
|
+
request.tools = this.params.tools?.map(tool => {
|
|
3961
|
+
if (tool.functionDeclarations) {
|
|
3962
|
+
return {
|
|
3963
|
+
templateFunctions: tool.functionDeclarations.map(declaration => {
|
|
3964
|
+
if (declaration.parameters) {
|
|
3965
|
+
const newDeclaration = { ...declaration };
|
|
3966
|
+
delete newDeclaration.parameters;
|
|
3967
|
+
newDeclaration.inputSchema = declaration.parameters;
|
|
3968
|
+
return newDeclaration;
|
|
3969
|
+
}
|
|
3970
|
+
return declaration;
|
|
3971
|
+
})
|
|
3972
|
+
};
|
|
3973
|
+
}
|
|
3974
|
+
return tool;
|
|
3975
|
+
});
|
|
3976
|
+
}
|
|
3977
|
+
if (this.params.toolConfig) {
|
|
3978
|
+
request.toolConfig = this.params.toolConfig;
|
|
3979
|
+
}
|
|
3980
|
+
return request;
|
|
3981
|
+
}
|
|
3982
|
+
/**
|
|
3983
|
+
* Calls the specific templateGenerateContent() function needed for
|
|
3984
|
+
* this specialized TemplateChatSession.
|
|
3985
|
+
* @internal
|
|
3986
|
+
*/
|
|
3987
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3988
|
+
return templateGenerateContent(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3989
|
+
...this.requestOptions,
|
|
3990
|
+
...singleRequestOptions
|
|
3991
|
+
});
|
|
3992
|
+
}
|
|
3993
|
+
/**
|
|
3994
|
+
* Calls the specific templateGenerateContentStream() function needed for
|
|
3995
|
+
* this specialized TemplateChatSession.
|
|
3996
|
+
* @internal
|
|
3997
|
+
*/
|
|
3998
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3999
|
+
return templateGenerateContentStream(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
4000
|
+
...this.requestOptions,
|
|
4001
|
+
...singleRequestOptions
|
|
4002
|
+
});
|
|
4003
|
+
}
|
|
4004
|
+
/**
|
|
4005
|
+
* Sends a chat message and receives a non-streaming
|
|
4006
|
+
* {@link GenerateContentResult}
|
|
4007
|
+
*/
|
|
4008
|
+
async sendMessage(request, singleRequestOptions) {
|
|
4009
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
4010
|
+
}
|
|
4011
|
+
/**
|
|
4012
|
+
* Sends a chat message and receives the response as a
|
|
4013
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
4014
|
+
* and a response promise.
|
|
4015
|
+
*/
|
|
4016
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
4017
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
4018
|
+
}
|
|
4019
|
+
}
|
|
4020
|
+
|
|
3825
4021
|
/**
|
|
3826
4022
|
* @license
|
|
3827
4023
|
* Copyright 2025 Google LLC
|
|
@@ -3887,6 +4083,18 @@ class TemplateGenerativeModel {
|
|
|
3887
4083
|
...singleRequestOptions
|
|
3888
4084
|
});
|
|
3889
4085
|
}
|
|
4086
|
+
/**
|
|
4087
|
+
* Starts a {@link TemplateChatSession} that will use this template to
|
|
4088
|
+
* respond to messages.
|
|
4089
|
+
*
|
|
4090
|
+
* @param params - Configurations for the chat, including the template
|
|
4091
|
+
* ID and input variables.
|
|
4092
|
+
*
|
|
4093
|
+
* @beta
|
|
4094
|
+
*/
|
|
4095
|
+
startChat(params) {
|
|
4096
|
+
return new TemplateChatSession(this._apiSettings, params, this.requestOptions);
|
|
4097
|
+
}
|
|
3890
4098
|
}
|
|
3891
4099
|
|
|
3892
4100
|
/**
|
|
@@ -3910,7 +4118,10 @@ class TemplateGenerativeModel {
|
|
|
3910
4118
|
*
|
|
3911
4119
|
* This class should only be instantiated with {@link getTemplateImagenModel}.
|
|
3912
4120
|
*
|
|
3913
|
-
* @
|
|
4121
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4122
|
+
* early as June 2026. As a replacement, you can
|
|
4123
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4124
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3914
4125
|
*/
|
|
3915
4126
|
class TemplateImagenModel {
|
|
3916
4127
|
/**
|
|
@@ -4219,6 +4430,11 @@ class AnyOfSchema extends Schema {
|
|
|
4219
4430
|
* }
|
|
4220
4431
|
* ```
|
|
4221
4432
|
*
|
|
4433
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4434
|
+
* early as June 2026. As a replacement, you can
|
|
4435
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4436
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4437
|
+
*
|
|
4222
4438
|
* @public
|
|
4223
4439
|
*/
|
|
4224
4440
|
class ImagenImageFormat {
|
|
@@ -4719,6 +4935,11 @@ function getGenerativeModel(ai, modelParams, requestOptions) {
|
|
|
4719
4935
|
*
|
|
4720
4936
|
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
|
|
4721
4937
|
*
|
|
4938
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4939
|
+
* early as June 2026. As a replacement, you can
|
|
4940
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4941
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4942
|
+
*
|
|
4722
4943
|
* @param ai - An {@link AI} instance.
|
|
4723
4944
|
* @param modelParams - Parameters to use when making Imagen requests.
|
|
4724
4945
|
* @param requestOptions - Additional options to use when making requests.
|
|
@@ -4769,10 +4990,13 @@ function getTemplateGenerativeModel(ai, requestOptions) {
|
|
|
4769
4990
|
* Returns a {@link TemplateImagenModel} class for executing server-side
|
|
4770
4991
|
* Imagen templates.
|
|
4771
4992
|
*
|
|
4993
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4994
|
+
* early as June 2026. As a replacement, you can
|
|
4995
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4996
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4997
|
+
*
|
|
4772
4998
|
* @param ai - An {@link AI} instance.
|
|
4773
4999
|
* @param requestOptions - Additional options to use when making requests.
|
|
4774
|
-
*
|
|
4775
|
-
* @beta
|
|
4776
5000
|
*/
|
|
4777
5001
|
function getTemplateImagenModel(ai, requestOptions) {
|
|
4778
5002
|
return new TemplateImagenModel(ai, requestOptions);
|
|
@@ -4801,6 +5025,7 @@ exports.BackendType = BackendType;
|
|
|
4801
5025
|
exports.BlockReason = BlockReason;
|
|
4802
5026
|
exports.BooleanSchema = BooleanSchema;
|
|
4803
5027
|
exports.ChatSession = ChatSession;
|
|
5028
|
+
exports.ChatSessionBase = ChatSessionBase;
|
|
4804
5029
|
exports.FinishReason = FinishReason;
|
|
4805
5030
|
exports.FunctionCallingMode = FunctionCallingMode;
|
|
4806
5031
|
exports.GenerativeModel = GenerativeModel;
|
|
@@ -4831,6 +5056,7 @@ exports.ResponseModality = ResponseModality;
|
|
|
4831
5056
|
exports.Schema = Schema;
|
|
4832
5057
|
exports.SchemaType = SchemaType;
|
|
4833
5058
|
exports.StringSchema = StringSchema;
|
|
5059
|
+
exports.TemplateChatSession = TemplateChatSession;
|
|
4834
5060
|
exports.TemplateGenerativeModel = TemplateGenerativeModel;
|
|
4835
5061
|
exports.TemplateImagenModel = TemplateImagenModel;
|
|
4836
5062
|
exports.ThinkingLevel = ThinkingLevel;
|