@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.node.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 = {
|
|
@@ -2271,95 +2286,7 @@ function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, n
|
|
|
2271
2286
|
|
|
2272
2287
|
/**
|
|
2273
2288
|
* @license
|
|
2274
|
-
* Copyright
|
|
2275
|
-
*
|
|
2276
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2277
|
-
* you may not use this file except in compliance with the License.
|
|
2278
|
-
* You may obtain a copy of the License at
|
|
2279
|
-
*
|
|
2280
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2281
|
-
*
|
|
2282
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2283
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2284
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2285
|
-
* See the License for the specific language governing permissions and
|
|
2286
|
-
* limitations under the License.
|
|
2287
|
-
*/
|
|
2288
|
-
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2289
|
-
const VALID_PART_FIELDS = [
|
|
2290
|
-
'text',
|
|
2291
|
-
'inlineData',
|
|
2292
|
-
'functionCall',
|
|
2293
|
-
'functionResponse',
|
|
2294
|
-
'thought',
|
|
2295
|
-
'thoughtSignature'
|
|
2296
|
-
];
|
|
2297
|
-
const VALID_PARTS_PER_ROLE = {
|
|
2298
|
-
user: ['text', 'inlineData'],
|
|
2299
|
-
function: ['functionResponse'],
|
|
2300
|
-
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2301
|
-
// System instructions shouldn't be in history anyway.
|
|
2302
|
-
system: ['text']
|
|
2303
|
-
};
|
|
2304
|
-
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2305
|
-
user: ['model'],
|
|
2306
|
-
function: ['model'],
|
|
2307
|
-
model: ['user', 'function'],
|
|
2308
|
-
// System instructions shouldn't be in history.
|
|
2309
|
-
system: []
|
|
2310
|
-
};
|
|
2311
|
-
function validateChatHistory(history) {
|
|
2312
|
-
let prevContent = null;
|
|
2313
|
-
for (const currContent of history) {
|
|
2314
|
-
const { role, parts } = currContent;
|
|
2315
|
-
if (!prevContent && role !== 'user') {
|
|
2316
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2317
|
-
}
|
|
2318
|
-
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2319
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2320
|
-
}
|
|
2321
|
-
if (!Array.isArray(parts)) {
|
|
2322
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2323
|
-
}
|
|
2324
|
-
if (parts.length === 0) {
|
|
2325
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2326
|
-
}
|
|
2327
|
-
const countFields = {
|
|
2328
|
-
text: 0,
|
|
2329
|
-
inlineData: 0,
|
|
2330
|
-
functionCall: 0,
|
|
2331
|
-
functionResponse: 0,
|
|
2332
|
-
thought: 0,
|
|
2333
|
-
thoughtSignature: 0,
|
|
2334
|
-
executableCode: 0,
|
|
2335
|
-
codeExecutionResult: 0
|
|
2336
|
-
};
|
|
2337
|
-
for (const part of parts) {
|
|
2338
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2339
|
-
if (key in part) {
|
|
2340
|
-
countFields[key] += 1;
|
|
2341
|
-
}
|
|
2342
|
-
}
|
|
2343
|
-
}
|
|
2344
|
-
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2345
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2346
|
-
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2347
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2348
|
-
}
|
|
2349
|
-
}
|
|
2350
|
-
if (prevContent) {
|
|
2351
|
-
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2352
|
-
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2353
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2354
|
-
}
|
|
2355
|
-
}
|
|
2356
|
-
prevContent = currContent;
|
|
2357
|
-
}
|
|
2358
|
-
}
|
|
2359
|
-
|
|
2360
|
-
/**
|
|
2361
|
-
* @license
|
|
2362
|
-
* Copyright 2024 Google LLC
|
|
2289
|
+
* Copyright 2026 Google LLC
|
|
2363
2290
|
*
|
|
2364
2291
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2365
2292
|
* you may not use this file except in compliance with the License.
|
|
@@ -2384,28 +2311,23 @@ const SILENT_ERROR = 'SILENT_ERROR';
|
|
|
2384
2311
|
*/
|
|
2385
2312
|
const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10;
|
|
2386
2313
|
/**
|
|
2387
|
-
*
|
|
2388
|
-
* history of sent and received messages so far.
|
|
2314
|
+
* Base class for various `ChatSession` classes that enables sending chat
|
|
2315
|
+
* messages and stores history of sent and received messages so far.
|
|
2389
2316
|
*
|
|
2390
2317
|
* @public
|
|
2391
2318
|
*/
|
|
2392
|
-
class
|
|
2393
|
-
constructor(apiSettings,
|
|
2394
|
-
this.model = model;
|
|
2395
|
-
this.chromeAdapter = chromeAdapter;
|
|
2319
|
+
class ChatSessionBase {
|
|
2320
|
+
constructor(apiSettings, params, requestOptions) {
|
|
2396
2321
|
this.params = params;
|
|
2397
2322
|
this.requestOptions = requestOptions;
|
|
2398
2323
|
this._history = [];
|
|
2399
2324
|
/**
|
|
2400
2325
|
* Ensures sequential execution of chat messages to maintain history order.
|
|
2401
2326
|
* Each call waits for the previous one to settle before proceeding.
|
|
2327
|
+
* @internal
|
|
2402
2328
|
*/
|
|
2403
2329
|
this._sendPromise = Promise.resolve();
|
|
2404
2330
|
this._apiSettings = apiSettings;
|
|
2405
|
-
if (params?.history) {
|
|
2406
|
-
validateChatHistory(params.history);
|
|
2407
|
-
this._history = params.history;
|
|
2408
|
-
}
|
|
2409
2331
|
}
|
|
2410
2332
|
/**
|
|
2411
2333
|
* Gets the chat history so far. Blocked prompts are not added to history.
|
|
@@ -2416,26 +2338,12 @@ class ChatSession {
|
|
|
2416
2338
|
await this._sendPromise;
|
|
2417
2339
|
return this._history;
|
|
2418
2340
|
}
|
|
2419
|
-
/**
|
|
2420
|
-
* Format Content into a request for generateContent or
|
|
2421
|
-
* generateContentStream.
|
|
2422
|
-
* @internal
|
|
2423
|
-
*/
|
|
2424
|
-
_formatRequest(incomingContent, tempHistory) {
|
|
2425
|
-
return {
|
|
2426
|
-
safetySettings: this.params?.safetySettings,
|
|
2427
|
-
generationConfig: this.params?.generationConfig,
|
|
2428
|
-
tools: this.params?.tools,
|
|
2429
|
-
toolConfig: this.params?.toolConfig,
|
|
2430
|
-
systemInstruction: this.params?.systemInstruction,
|
|
2431
|
-
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2432
|
-
};
|
|
2433
|
-
}
|
|
2434
2341
|
/**
|
|
2435
2342
|
* Sends a chat message and receives a non-streaming
|
|
2436
2343
|
* {@link GenerateContentResult}
|
|
2344
|
+
* @internal
|
|
2437
2345
|
*/
|
|
2438
|
-
async
|
|
2346
|
+
async _sendMessage(request, singleRequestOptions) {
|
|
2439
2347
|
let finalResult = {};
|
|
2440
2348
|
await this._sendPromise;
|
|
2441
2349
|
/**
|
|
@@ -2447,7 +2355,7 @@ class ChatSession {
|
|
|
2447
2355
|
this._sendPromise = this._sendPromise.then(async () => {
|
|
2448
2356
|
let functionCalls;
|
|
2449
2357
|
let functionCallTurnCount = 0;
|
|
2450
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2358
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2451
2359
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2452
2360
|
// Repeats until model returns a response with no function calls
|
|
2453
2361
|
// or until `functionCallMaxTurns` is met or exceeded.
|
|
@@ -2463,10 +2371,7 @@ class ChatSession {
|
|
|
2463
2371
|
}
|
|
2464
2372
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2465
2373
|
tempHistory.push(formattedContent);
|
|
2466
|
-
const result = await
|
|
2467
|
-
...this.requestOptions,
|
|
2468
|
-
...singleRequestOptions
|
|
2469
|
-
});
|
|
2374
|
+
const result = await this._callGenerateContent(formattedRequest, singleRequestOptions);
|
|
2470
2375
|
if (result) {
|
|
2471
2376
|
finalResult = result;
|
|
2472
2377
|
functionCalls = this._getCallableFunctionCalls(result.response);
|
|
@@ -2506,8 +2411,9 @@ class ChatSession {
|
|
|
2506
2411
|
* Sends a chat message and receives the response as a
|
|
2507
2412
|
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2508
2413
|
* and a response promise.
|
|
2414
|
+
* @internal
|
|
2509
2415
|
*/
|
|
2510
|
-
async
|
|
2416
|
+
async _sendMessageStream(request, singleRequestOptions) {
|
|
2511
2417
|
await this._sendPromise;
|
|
2512
2418
|
/**
|
|
2513
2419
|
* Temporarily store multiple turns for cases like automatic function
|
|
@@ -2518,7 +2424,7 @@ class ChatSession {
|
|
|
2518
2424
|
const callGenerateContentStream = async () => {
|
|
2519
2425
|
let functionCalls;
|
|
2520
2426
|
let functionCallTurnCount = 0;
|
|
2521
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2427
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2522
2428
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2523
2429
|
let result;
|
|
2524
2430
|
// Repeats until model returns a response with no function calls
|
|
@@ -2535,10 +2441,7 @@ class ChatSession {
|
|
|
2535
2441
|
}
|
|
2536
2442
|
tempHistory.push(formattedContent);
|
|
2537
2443
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2538
|
-
result = await
|
|
2539
|
-
...this.requestOptions,
|
|
2540
|
-
...singleRequestOptions
|
|
2541
|
-
});
|
|
2444
|
+
result = await this._callGenerateContentStream(formattedRequest, singleRequestOptions);
|
|
2542
2445
|
functionCalls = this._getCallableFunctionCalls(result.firstValue);
|
|
2543
2446
|
if (functionCalls &&
|
|
2544
2447
|
result.firstValue &&
|
|
@@ -2632,7 +2535,7 @@ class ChatSession {
|
|
|
2632
2535
|
* @internal
|
|
2633
2536
|
*/
|
|
2634
2537
|
async _callFunctionsAsNeeded(functionCalls) {
|
|
2635
|
-
const activeCallList =
|
|
2538
|
+
const activeCallList = [];
|
|
2636
2539
|
const promiseList = [];
|
|
2637
2540
|
const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
|
|
2638
2541
|
if (functionDeclarationsTool &&
|
|
@@ -2645,7 +2548,8 @@ class ChatSession {
|
|
|
2645
2548
|
wrappedError.stack = e.stack;
|
|
2646
2549
|
throw wrappedError;
|
|
2647
2550
|
});
|
|
2648
|
-
activeCallList.
|
|
2551
|
+
activeCallList.push({
|
|
2552
|
+
name: functionCall.name,
|
|
2649
2553
|
id: functionCall.id,
|
|
2650
2554
|
results
|
|
2651
2555
|
});
|
|
@@ -2655,12 +2559,16 @@ class ChatSession {
|
|
|
2655
2559
|
// Wait for promises to finish.
|
|
2656
2560
|
await Promise.all(promiseList);
|
|
2657
2561
|
const functionResponseParts = [];
|
|
2658
|
-
for (const
|
|
2562
|
+
for (const { name, id, results } of activeCallList) {
|
|
2563
|
+
const functionResponse = {
|
|
2564
|
+
name,
|
|
2565
|
+
response: await results
|
|
2566
|
+
};
|
|
2567
|
+
if (id) {
|
|
2568
|
+
functionResponse.id = id;
|
|
2569
|
+
}
|
|
2659
2570
|
functionResponseParts.push({
|
|
2660
|
-
functionResponse
|
|
2661
|
-
name,
|
|
2662
|
-
response: await callData.results
|
|
2663
|
-
}
|
|
2571
|
+
functionResponse
|
|
2664
2572
|
});
|
|
2665
2573
|
}
|
|
2666
2574
|
return functionResponseParts;
|
|
@@ -2671,6 +2579,182 @@ class ChatSession {
|
|
|
2671
2579
|
}
|
|
2672
2580
|
}
|
|
2673
2581
|
|
|
2582
|
+
/**
|
|
2583
|
+
* @license
|
|
2584
|
+
* Copyright 2024 Google LLC
|
|
2585
|
+
*
|
|
2586
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2587
|
+
* you may not use this file except in compliance with the License.
|
|
2588
|
+
* You may obtain a copy of the License at
|
|
2589
|
+
*
|
|
2590
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2591
|
+
*
|
|
2592
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2593
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2594
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2595
|
+
* See the License for the specific language governing permissions and
|
|
2596
|
+
* limitations under the License.
|
|
2597
|
+
*/
|
|
2598
|
+
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2599
|
+
const VALID_PART_FIELDS = [
|
|
2600
|
+
'text',
|
|
2601
|
+
'inlineData',
|
|
2602
|
+
'functionCall',
|
|
2603
|
+
'functionResponse',
|
|
2604
|
+
'thought',
|
|
2605
|
+
'thoughtSignature'
|
|
2606
|
+
];
|
|
2607
|
+
const VALID_PARTS_PER_ROLE = {
|
|
2608
|
+
user: ['text', 'inlineData'],
|
|
2609
|
+
function: ['functionResponse'],
|
|
2610
|
+
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2611
|
+
// System instructions shouldn't be in history anyway.
|
|
2612
|
+
system: ['text']
|
|
2613
|
+
};
|
|
2614
|
+
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2615
|
+
user: ['model'],
|
|
2616
|
+
function: ['model'],
|
|
2617
|
+
model: ['user', 'function'],
|
|
2618
|
+
// System instructions shouldn't be in history.
|
|
2619
|
+
system: []
|
|
2620
|
+
};
|
|
2621
|
+
function validateChatHistory(history) {
|
|
2622
|
+
let prevContent = null;
|
|
2623
|
+
for (const currContent of history) {
|
|
2624
|
+
const { role, parts } = currContent;
|
|
2625
|
+
if (!prevContent && role !== 'user') {
|
|
2626
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2627
|
+
}
|
|
2628
|
+
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2629
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2630
|
+
}
|
|
2631
|
+
if (!Array.isArray(parts)) {
|
|
2632
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2633
|
+
}
|
|
2634
|
+
if (parts.length === 0) {
|
|
2635
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2636
|
+
}
|
|
2637
|
+
const countFields = {
|
|
2638
|
+
text: 0,
|
|
2639
|
+
inlineData: 0,
|
|
2640
|
+
functionCall: 0,
|
|
2641
|
+
functionResponse: 0,
|
|
2642
|
+
thought: 0,
|
|
2643
|
+
thoughtSignature: 0,
|
|
2644
|
+
executableCode: 0,
|
|
2645
|
+
codeExecutionResult: 0
|
|
2646
|
+
};
|
|
2647
|
+
for (const part of parts) {
|
|
2648
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2649
|
+
if (key in part) {
|
|
2650
|
+
countFields[key] += 1;
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2655
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2656
|
+
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2657
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2660
|
+
if (prevContent) {
|
|
2661
|
+
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2662
|
+
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2663
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
prevContent = currContent;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
|
|
2670
|
+
/**
|
|
2671
|
+
* @license
|
|
2672
|
+
* Copyright 2024 Google LLC
|
|
2673
|
+
*
|
|
2674
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2675
|
+
* you may not use this file except in compliance with the License.
|
|
2676
|
+
* You may obtain a copy of the License at
|
|
2677
|
+
*
|
|
2678
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2679
|
+
*
|
|
2680
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2681
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2682
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2683
|
+
* See the License for the specific language governing permissions and
|
|
2684
|
+
* limitations under the License.
|
|
2685
|
+
*/
|
|
2686
|
+
/**
|
|
2687
|
+
* ChatSession class that enables sending chat messages and stores
|
|
2688
|
+
* history of sent and received messages so far.
|
|
2689
|
+
*
|
|
2690
|
+
* @public
|
|
2691
|
+
*/
|
|
2692
|
+
class ChatSession extends ChatSessionBase {
|
|
2693
|
+
constructor(apiSettings, model, chromeAdapter, params, requestOptions) {
|
|
2694
|
+
super(apiSettings, params, requestOptions);
|
|
2695
|
+
this.model = model;
|
|
2696
|
+
this.chromeAdapter = chromeAdapter;
|
|
2697
|
+
this.params = params;
|
|
2698
|
+
this.requestOptions = requestOptions;
|
|
2699
|
+
if (params?.history) {
|
|
2700
|
+
validateChatHistory(params.history);
|
|
2701
|
+
this._history = params.history;
|
|
2702
|
+
}
|
|
2703
|
+
}
|
|
2704
|
+
/**
|
|
2705
|
+
* Format Content into a request for generateContent or
|
|
2706
|
+
* generateContentStream.
|
|
2707
|
+
* @internal
|
|
2708
|
+
*/
|
|
2709
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
2710
|
+
return {
|
|
2711
|
+
safetySettings: this.params?.safetySettings,
|
|
2712
|
+
generationConfig: this.params?.generationConfig,
|
|
2713
|
+
tools: this.params?.tools,
|
|
2714
|
+
toolConfig: this.params?.toolConfig,
|
|
2715
|
+
systemInstruction: this.params?.systemInstruction,
|
|
2716
|
+
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2717
|
+
};
|
|
2718
|
+
}
|
|
2719
|
+
/**
|
|
2720
|
+
* Calls default generateContent() (versus a specialized one like
|
|
2721
|
+
* templateGenerateContent).
|
|
2722
|
+
* @internal
|
|
2723
|
+
*/
|
|
2724
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
2725
|
+
return generateContent(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
2726
|
+
...this.requestOptions,
|
|
2727
|
+
...singleRequestOptions
|
|
2728
|
+
});
|
|
2729
|
+
}
|
|
2730
|
+
/**
|
|
2731
|
+
* Calls default generateContentStream() (versus a specialized one like
|
|
2732
|
+
* templateGenerateContentStream).
|
|
2733
|
+
* @internal
|
|
2734
|
+
*/
|
|
2735
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
2736
|
+
return generateContentStream(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
2737
|
+
...this.requestOptions,
|
|
2738
|
+
...singleRequestOptions
|
|
2739
|
+
});
|
|
2740
|
+
}
|
|
2741
|
+
/**
|
|
2742
|
+
* Sends a chat message and receives a non-streaming
|
|
2743
|
+
* {@link GenerateContentResult}
|
|
2744
|
+
*/
|
|
2745
|
+
async sendMessage(request, singleRequestOptions) {
|
|
2746
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
2747
|
+
}
|
|
2748
|
+
/**
|
|
2749
|
+
* Sends a chat message and receives the response as a
|
|
2750
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2751
|
+
* and a response promise.
|
|
2752
|
+
*/
|
|
2753
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
2754
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
|
|
2674
2758
|
/**
|
|
2675
2759
|
* @license
|
|
2676
2760
|
* Copyright 2024 Google LLC
|
|
@@ -2838,8 +2922,9 @@ function validateGenerationConfig(generationConfig) {
|
|
|
2838
2922
|
}
|
|
2839
2923
|
if ((generationConfig.responseSchema != null ||
|
|
2840
2924
|
generationConfig.responseJsonSchema != null) &&
|
|
2841
|
-
generationConfig.responseMimeType) {
|
|
2842
|
-
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set
|
|
2925
|
+
generationConfig.responseMimeType !== 'application/json') {
|
|
2926
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set to "application/json" if` +
|
|
2927
|
+
` responseSchema or responseJsonSchema are set.`);
|
|
2843
2928
|
}
|
|
2844
2929
|
}
|
|
2845
2930
|
|
|
@@ -3271,6 +3356,11 @@ class LiveGenerativeModel extends AIModel {
|
|
|
3271
3356
|
* }
|
|
3272
3357
|
* ```
|
|
3273
3358
|
*
|
|
3359
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3360
|
+
* early as June 2026. As a replacement, you can
|
|
3361
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3362
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3363
|
+
*
|
|
3274
3364
|
* @public
|
|
3275
3365
|
*/
|
|
3276
3366
|
class ImagenModel extends AIModel {
|
|
@@ -3514,6 +3604,112 @@ class WebSocketHandlerImpl {
|
|
|
3514
3604
|
}
|
|
3515
3605
|
}
|
|
3516
3606
|
|
|
3607
|
+
/**
|
|
3608
|
+
* @license
|
|
3609
|
+
* Copyright 2026 Google LLC
|
|
3610
|
+
*
|
|
3611
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3612
|
+
* you may not use this file except in compliance with the License.
|
|
3613
|
+
* You may obtain a copy of the License at
|
|
3614
|
+
*
|
|
3615
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3616
|
+
*
|
|
3617
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3618
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3619
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3620
|
+
* See the License for the specific language governing permissions and
|
|
3621
|
+
* limitations under the License.
|
|
3622
|
+
*/
|
|
3623
|
+
/**
|
|
3624
|
+
* `ChatSession` class for use with server prompt templates that
|
|
3625
|
+
* enables sending chat messages and stores history of sent and
|
|
3626
|
+
* received messages so far.
|
|
3627
|
+
*
|
|
3628
|
+
* @beta
|
|
3629
|
+
*/
|
|
3630
|
+
class TemplateChatSession extends ChatSessionBase {
|
|
3631
|
+
constructor(apiSettings, params, requestOptions) {
|
|
3632
|
+
super(apiSettings, params, requestOptions);
|
|
3633
|
+
this.params = params;
|
|
3634
|
+
this.requestOptions = requestOptions;
|
|
3635
|
+
if (params.history) {
|
|
3636
|
+
validateChatHistory(params.history);
|
|
3637
|
+
this._history = params.history;
|
|
3638
|
+
}
|
|
3639
|
+
}
|
|
3640
|
+
/**
|
|
3641
|
+
* Format the internal state to the body payload for `templateGenerateContent`.
|
|
3642
|
+
* @internal
|
|
3643
|
+
*/
|
|
3644
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3645
|
+
const request = {
|
|
3646
|
+
history: [...this._history, ...tempHistory, incomingContent]
|
|
3647
|
+
};
|
|
3648
|
+
if (this.params.templateVariables) {
|
|
3649
|
+
request.inputs = this.params.templateVariables;
|
|
3650
|
+
}
|
|
3651
|
+
if (this.params.tools) {
|
|
3652
|
+
request.tools = this.params.tools?.map(tool => {
|
|
3653
|
+
if (tool.functionDeclarations) {
|
|
3654
|
+
return {
|
|
3655
|
+
templateFunctions: tool.functionDeclarations.map(declaration => {
|
|
3656
|
+
if (declaration.parameters) {
|
|
3657
|
+
const newDeclaration = { ...declaration };
|
|
3658
|
+
delete newDeclaration.parameters;
|
|
3659
|
+
newDeclaration.inputSchema = declaration.parameters;
|
|
3660
|
+
return newDeclaration;
|
|
3661
|
+
}
|
|
3662
|
+
return declaration;
|
|
3663
|
+
})
|
|
3664
|
+
};
|
|
3665
|
+
}
|
|
3666
|
+
return tool;
|
|
3667
|
+
});
|
|
3668
|
+
}
|
|
3669
|
+
if (this.params.toolConfig) {
|
|
3670
|
+
request.toolConfig = this.params.toolConfig;
|
|
3671
|
+
}
|
|
3672
|
+
return request;
|
|
3673
|
+
}
|
|
3674
|
+
/**
|
|
3675
|
+
* Calls the specific templateGenerateContent() function needed for
|
|
3676
|
+
* this specialized TemplateChatSession.
|
|
3677
|
+
* @internal
|
|
3678
|
+
*/
|
|
3679
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3680
|
+
return templateGenerateContent(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3681
|
+
...this.requestOptions,
|
|
3682
|
+
...singleRequestOptions
|
|
3683
|
+
});
|
|
3684
|
+
}
|
|
3685
|
+
/**
|
|
3686
|
+
* Calls the specific templateGenerateContentStream() function needed for
|
|
3687
|
+
* this specialized TemplateChatSession.
|
|
3688
|
+
* @internal
|
|
3689
|
+
*/
|
|
3690
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3691
|
+
return templateGenerateContentStream(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3692
|
+
...this.requestOptions,
|
|
3693
|
+
...singleRequestOptions
|
|
3694
|
+
});
|
|
3695
|
+
}
|
|
3696
|
+
/**
|
|
3697
|
+
* Sends a chat message and receives a non-streaming
|
|
3698
|
+
* {@link GenerateContentResult}
|
|
3699
|
+
*/
|
|
3700
|
+
async sendMessage(request, singleRequestOptions) {
|
|
3701
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
3702
|
+
}
|
|
3703
|
+
/**
|
|
3704
|
+
* Sends a chat message and receives the response as a
|
|
3705
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
3706
|
+
* and a response promise.
|
|
3707
|
+
*/
|
|
3708
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
3709
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
|
|
3517
3713
|
/**
|
|
3518
3714
|
* @license
|
|
3519
3715
|
* Copyright 2025 Google LLC
|
|
@@ -3579,6 +3775,18 @@ class TemplateGenerativeModel {
|
|
|
3579
3775
|
...singleRequestOptions
|
|
3580
3776
|
});
|
|
3581
3777
|
}
|
|
3778
|
+
/**
|
|
3779
|
+
* Starts a {@link TemplateChatSession} that will use this template to
|
|
3780
|
+
* respond to messages.
|
|
3781
|
+
*
|
|
3782
|
+
* @param params - Configurations for the chat, including the template
|
|
3783
|
+
* ID and input variables.
|
|
3784
|
+
*
|
|
3785
|
+
* @beta
|
|
3786
|
+
*/
|
|
3787
|
+
startChat(params) {
|
|
3788
|
+
return new TemplateChatSession(this._apiSettings, params, this.requestOptions);
|
|
3789
|
+
}
|
|
3582
3790
|
}
|
|
3583
3791
|
|
|
3584
3792
|
/**
|
|
@@ -3602,7 +3810,10 @@ class TemplateGenerativeModel {
|
|
|
3602
3810
|
*
|
|
3603
3811
|
* This class should only be instantiated with {@link getTemplateImagenModel}.
|
|
3604
3812
|
*
|
|
3605
|
-
* @
|
|
3813
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3814
|
+
* early as June 2026. As a replacement, you can
|
|
3815
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3816
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3606
3817
|
*/
|
|
3607
3818
|
class TemplateImagenModel {
|
|
3608
3819
|
/**
|
|
@@ -3911,6 +4122,11 @@ class AnyOfSchema extends Schema {
|
|
|
3911
4122
|
* }
|
|
3912
4123
|
* ```
|
|
3913
4124
|
*
|
|
4125
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4126
|
+
* early as June 2026. As a replacement, you can
|
|
4127
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4128
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4129
|
+
*
|
|
3914
4130
|
* @public
|
|
3915
4131
|
*/
|
|
3916
4132
|
class ImagenImageFormat {
|
|
@@ -4411,6 +4627,11 @@ function getGenerativeModel(ai, modelParams, requestOptions) {
|
|
|
4411
4627
|
*
|
|
4412
4628
|
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
|
|
4413
4629
|
*
|
|
4630
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4631
|
+
* early as June 2026. As a replacement, you can
|
|
4632
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4633
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4634
|
+
*
|
|
4414
4635
|
* @param ai - An {@link AI} instance.
|
|
4415
4636
|
* @param modelParams - Parameters to use when making Imagen requests.
|
|
4416
4637
|
* @param requestOptions - Additional options to use when making requests.
|
|
@@ -4461,10 +4682,13 @@ function getTemplateGenerativeModel(ai, requestOptions) {
|
|
|
4461
4682
|
* Returns a {@link TemplateImagenModel} class for executing server-side
|
|
4462
4683
|
* Imagen templates.
|
|
4463
4684
|
*
|
|
4685
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4686
|
+
* early as June 2026. As a replacement, you can
|
|
4687
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4688
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4689
|
+
*
|
|
4464
4690
|
* @param ai - An {@link AI} instance.
|
|
4465
4691
|
* @param requestOptions - Additional options to use when making requests.
|
|
4466
|
-
*
|
|
4467
|
-
* @beta
|
|
4468
4692
|
*/
|
|
4469
4693
|
function getTemplateImagenModel(ai, requestOptions) {
|
|
4470
4694
|
return new TemplateImagenModel(ai, requestOptions);
|
|
@@ -4493,6 +4717,7 @@ exports.BackendType = BackendType;
|
|
|
4493
4717
|
exports.BlockReason = BlockReason;
|
|
4494
4718
|
exports.BooleanSchema = BooleanSchema;
|
|
4495
4719
|
exports.ChatSession = ChatSession;
|
|
4720
|
+
exports.ChatSessionBase = ChatSessionBase;
|
|
4496
4721
|
exports.FinishReason = FinishReason;
|
|
4497
4722
|
exports.FunctionCallingMode = FunctionCallingMode;
|
|
4498
4723
|
exports.GenerativeModel = GenerativeModel;
|
|
@@ -4523,6 +4748,7 @@ exports.ResponseModality = ResponseModality;
|
|
|
4523
4748
|
exports.Schema = Schema;
|
|
4524
4749
|
exports.SchemaType = SchemaType;
|
|
4525
4750
|
exports.StringSchema = StringSchema;
|
|
4751
|
+
exports.TemplateChatSession = TemplateChatSession;
|
|
4526
4752
|
exports.TemplateGenerativeModel = TemplateGenerativeModel;
|
|
4527
4753
|
exports.TemplateImagenModel = TemplateImagenModel;
|
|
4528
4754
|
exports.ThinkingLevel = ThinkingLevel;
|