@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.mjs
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 = {
|
|
@@ -2267,95 +2282,7 @@ function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, n
|
|
|
2267
2282
|
|
|
2268
2283
|
/**
|
|
2269
2284
|
* @license
|
|
2270
|
-
* Copyright
|
|
2271
|
-
*
|
|
2272
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2273
|
-
* you may not use this file except in compliance with the License.
|
|
2274
|
-
* You may obtain a copy of the License at
|
|
2275
|
-
*
|
|
2276
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2277
|
-
*
|
|
2278
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2279
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2280
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2281
|
-
* See the License for the specific language governing permissions and
|
|
2282
|
-
* limitations under the License.
|
|
2283
|
-
*/
|
|
2284
|
-
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2285
|
-
const VALID_PART_FIELDS = [
|
|
2286
|
-
'text',
|
|
2287
|
-
'inlineData',
|
|
2288
|
-
'functionCall',
|
|
2289
|
-
'functionResponse',
|
|
2290
|
-
'thought',
|
|
2291
|
-
'thoughtSignature'
|
|
2292
|
-
];
|
|
2293
|
-
const VALID_PARTS_PER_ROLE = {
|
|
2294
|
-
user: ['text', 'inlineData'],
|
|
2295
|
-
function: ['functionResponse'],
|
|
2296
|
-
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2297
|
-
// System instructions shouldn't be in history anyway.
|
|
2298
|
-
system: ['text']
|
|
2299
|
-
};
|
|
2300
|
-
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2301
|
-
user: ['model'],
|
|
2302
|
-
function: ['model'],
|
|
2303
|
-
model: ['user', 'function'],
|
|
2304
|
-
// System instructions shouldn't be in history.
|
|
2305
|
-
system: []
|
|
2306
|
-
};
|
|
2307
|
-
function validateChatHistory(history) {
|
|
2308
|
-
let prevContent = null;
|
|
2309
|
-
for (const currContent of history) {
|
|
2310
|
-
const { role, parts } = currContent;
|
|
2311
|
-
if (!prevContent && role !== 'user') {
|
|
2312
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2313
|
-
}
|
|
2314
|
-
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2315
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2316
|
-
}
|
|
2317
|
-
if (!Array.isArray(parts)) {
|
|
2318
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2319
|
-
}
|
|
2320
|
-
if (parts.length === 0) {
|
|
2321
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2322
|
-
}
|
|
2323
|
-
const countFields = {
|
|
2324
|
-
text: 0,
|
|
2325
|
-
inlineData: 0,
|
|
2326
|
-
functionCall: 0,
|
|
2327
|
-
functionResponse: 0,
|
|
2328
|
-
thought: 0,
|
|
2329
|
-
thoughtSignature: 0,
|
|
2330
|
-
executableCode: 0,
|
|
2331
|
-
codeExecutionResult: 0
|
|
2332
|
-
};
|
|
2333
|
-
for (const part of parts) {
|
|
2334
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2335
|
-
if (key in part) {
|
|
2336
|
-
countFields[key] += 1;
|
|
2337
|
-
}
|
|
2338
|
-
}
|
|
2339
|
-
}
|
|
2340
|
-
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2341
|
-
for (const key of VALID_PART_FIELDS) {
|
|
2342
|
-
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2343
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
if (prevContent) {
|
|
2347
|
-
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2348
|
-
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2349
|
-
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2350
|
-
}
|
|
2351
|
-
}
|
|
2352
|
-
prevContent = currContent;
|
|
2353
|
-
}
|
|
2354
|
-
}
|
|
2355
|
-
|
|
2356
|
-
/**
|
|
2357
|
-
* @license
|
|
2358
|
-
* Copyright 2024 Google LLC
|
|
2285
|
+
* Copyright 2026 Google LLC
|
|
2359
2286
|
*
|
|
2360
2287
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2361
2288
|
* you may not use this file except in compliance with the License.
|
|
@@ -2380,28 +2307,23 @@ const SILENT_ERROR = 'SILENT_ERROR';
|
|
|
2380
2307
|
*/
|
|
2381
2308
|
const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10;
|
|
2382
2309
|
/**
|
|
2383
|
-
*
|
|
2384
|
-
* history of sent and received messages so far.
|
|
2310
|
+
* Base class for various `ChatSession` classes that enables sending chat
|
|
2311
|
+
* messages and stores history of sent and received messages so far.
|
|
2385
2312
|
*
|
|
2386
2313
|
* @public
|
|
2387
2314
|
*/
|
|
2388
|
-
class
|
|
2389
|
-
constructor(apiSettings,
|
|
2390
|
-
this.model = model;
|
|
2391
|
-
this.chromeAdapter = chromeAdapter;
|
|
2315
|
+
class ChatSessionBase {
|
|
2316
|
+
constructor(apiSettings, params, requestOptions) {
|
|
2392
2317
|
this.params = params;
|
|
2393
2318
|
this.requestOptions = requestOptions;
|
|
2394
2319
|
this._history = [];
|
|
2395
2320
|
/**
|
|
2396
2321
|
* Ensures sequential execution of chat messages to maintain history order.
|
|
2397
2322
|
* Each call waits for the previous one to settle before proceeding.
|
|
2323
|
+
* @internal
|
|
2398
2324
|
*/
|
|
2399
2325
|
this._sendPromise = Promise.resolve();
|
|
2400
2326
|
this._apiSettings = apiSettings;
|
|
2401
|
-
if (params?.history) {
|
|
2402
|
-
validateChatHistory(params.history);
|
|
2403
|
-
this._history = params.history;
|
|
2404
|
-
}
|
|
2405
2327
|
}
|
|
2406
2328
|
/**
|
|
2407
2329
|
* Gets the chat history so far. Blocked prompts are not added to history.
|
|
@@ -2412,26 +2334,12 @@ class ChatSession {
|
|
|
2412
2334
|
await this._sendPromise;
|
|
2413
2335
|
return this._history;
|
|
2414
2336
|
}
|
|
2415
|
-
/**
|
|
2416
|
-
* Format Content into a request for generateContent or
|
|
2417
|
-
* generateContentStream.
|
|
2418
|
-
* @internal
|
|
2419
|
-
*/
|
|
2420
|
-
_formatRequest(incomingContent, tempHistory) {
|
|
2421
|
-
return {
|
|
2422
|
-
safetySettings: this.params?.safetySettings,
|
|
2423
|
-
generationConfig: this.params?.generationConfig,
|
|
2424
|
-
tools: this.params?.tools,
|
|
2425
|
-
toolConfig: this.params?.toolConfig,
|
|
2426
|
-
systemInstruction: this.params?.systemInstruction,
|
|
2427
|
-
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2428
|
-
};
|
|
2429
|
-
}
|
|
2430
2337
|
/**
|
|
2431
2338
|
* Sends a chat message and receives a non-streaming
|
|
2432
2339
|
* {@link GenerateContentResult}
|
|
2340
|
+
* @internal
|
|
2433
2341
|
*/
|
|
2434
|
-
async
|
|
2342
|
+
async _sendMessage(request, singleRequestOptions) {
|
|
2435
2343
|
let finalResult = {};
|
|
2436
2344
|
await this._sendPromise;
|
|
2437
2345
|
/**
|
|
@@ -2443,7 +2351,7 @@ class ChatSession {
|
|
|
2443
2351
|
this._sendPromise = this._sendPromise.then(async () => {
|
|
2444
2352
|
let functionCalls;
|
|
2445
2353
|
let functionCallTurnCount = 0;
|
|
2446
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2354
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2447
2355
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2448
2356
|
// Repeats until model returns a response with no function calls
|
|
2449
2357
|
// or until `functionCallMaxTurns` is met or exceeded.
|
|
@@ -2459,10 +2367,7 @@ class ChatSession {
|
|
|
2459
2367
|
}
|
|
2460
2368
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2461
2369
|
tempHistory.push(formattedContent);
|
|
2462
|
-
const result = await
|
|
2463
|
-
...this.requestOptions,
|
|
2464
|
-
...singleRequestOptions
|
|
2465
|
-
});
|
|
2370
|
+
const result = await this._callGenerateContent(formattedRequest, singleRequestOptions);
|
|
2466
2371
|
if (result) {
|
|
2467
2372
|
finalResult = result;
|
|
2468
2373
|
functionCalls = this._getCallableFunctionCalls(result.response);
|
|
@@ -2502,8 +2407,9 @@ class ChatSession {
|
|
|
2502
2407
|
* Sends a chat message and receives the response as a
|
|
2503
2408
|
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2504
2409
|
* and a response promise.
|
|
2410
|
+
* @internal
|
|
2505
2411
|
*/
|
|
2506
|
-
async
|
|
2412
|
+
async _sendMessageStream(request, singleRequestOptions) {
|
|
2507
2413
|
await this._sendPromise;
|
|
2508
2414
|
/**
|
|
2509
2415
|
* Temporarily store multiple turns for cases like automatic function
|
|
@@ -2514,7 +2420,7 @@ class ChatSession {
|
|
|
2514
2420
|
const callGenerateContentStream = async () => {
|
|
2515
2421
|
let functionCalls;
|
|
2516
2422
|
let functionCallTurnCount = 0;
|
|
2517
|
-
const functionCallMaxTurns = this.requestOptions?.
|
|
2423
|
+
const functionCallMaxTurns = this.requestOptions?.maxSequentialFunctionCalls ??
|
|
2518
2424
|
DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
|
|
2519
2425
|
let result;
|
|
2520
2426
|
// Repeats until model returns a response with no function calls
|
|
@@ -2531,10 +2437,7 @@ class ChatSession {
|
|
|
2531
2437
|
}
|
|
2532
2438
|
tempHistory.push(formattedContent);
|
|
2533
2439
|
const formattedRequest = this._formatRequest(formattedContent, tempHistory);
|
|
2534
|
-
result = await
|
|
2535
|
-
...this.requestOptions,
|
|
2536
|
-
...singleRequestOptions
|
|
2537
|
-
});
|
|
2440
|
+
result = await this._callGenerateContentStream(formattedRequest, singleRequestOptions);
|
|
2538
2441
|
functionCalls = this._getCallableFunctionCalls(result.firstValue);
|
|
2539
2442
|
if (functionCalls &&
|
|
2540
2443
|
result.firstValue &&
|
|
@@ -2628,7 +2531,7 @@ class ChatSession {
|
|
|
2628
2531
|
* @internal
|
|
2629
2532
|
*/
|
|
2630
2533
|
async _callFunctionsAsNeeded(functionCalls) {
|
|
2631
|
-
const activeCallList =
|
|
2534
|
+
const activeCallList = [];
|
|
2632
2535
|
const promiseList = [];
|
|
2633
2536
|
const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
|
|
2634
2537
|
if (functionDeclarationsTool &&
|
|
@@ -2641,7 +2544,8 @@ class ChatSession {
|
|
|
2641
2544
|
wrappedError.stack = e.stack;
|
|
2642
2545
|
throw wrappedError;
|
|
2643
2546
|
});
|
|
2644
|
-
activeCallList.
|
|
2547
|
+
activeCallList.push({
|
|
2548
|
+
name: functionCall.name,
|
|
2645
2549
|
id: functionCall.id,
|
|
2646
2550
|
results
|
|
2647
2551
|
});
|
|
@@ -2651,12 +2555,16 @@ class ChatSession {
|
|
|
2651
2555
|
// Wait for promises to finish.
|
|
2652
2556
|
await Promise.all(promiseList);
|
|
2653
2557
|
const functionResponseParts = [];
|
|
2654
|
-
for (const
|
|
2558
|
+
for (const { name, id, results } of activeCallList) {
|
|
2559
|
+
const functionResponse = {
|
|
2560
|
+
name,
|
|
2561
|
+
response: await results
|
|
2562
|
+
};
|
|
2563
|
+
if (id) {
|
|
2564
|
+
functionResponse.id = id;
|
|
2565
|
+
}
|
|
2655
2566
|
functionResponseParts.push({
|
|
2656
|
-
functionResponse
|
|
2657
|
-
name,
|
|
2658
|
-
response: await callData.results
|
|
2659
|
-
}
|
|
2567
|
+
functionResponse
|
|
2660
2568
|
});
|
|
2661
2569
|
}
|
|
2662
2570
|
return functionResponseParts;
|
|
@@ -2667,6 +2575,182 @@ class ChatSession {
|
|
|
2667
2575
|
}
|
|
2668
2576
|
}
|
|
2669
2577
|
|
|
2578
|
+
/**
|
|
2579
|
+
* @license
|
|
2580
|
+
* Copyright 2024 Google LLC
|
|
2581
|
+
*
|
|
2582
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2583
|
+
* you may not use this file except in compliance with the License.
|
|
2584
|
+
* You may obtain a copy of the License at
|
|
2585
|
+
*
|
|
2586
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2587
|
+
*
|
|
2588
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2589
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2590
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2591
|
+
* See the License for the specific language governing permissions and
|
|
2592
|
+
* limitations under the License.
|
|
2593
|
+
*/
|
|
2594
|
+
// https://ai.google.dev/api/rest/v1beta/Content#part
|
|
2595
|
+
const VALID_PART_FIELDS = [
|
|
2596
|
+
'text',
|
|
2597
|
+
'inlineData',
|
|
2598
|
+
'functionCall',
|
|
2599
|
+
'functionResponse',
|
|
2600
|
+
'thought',
|
|
2601
|
+
'thoughtSignature'
|
|
2602
|
+
];
|
|
2603
|
+
const VALID_PARTS_PER_ROLE = {
|
|
2604
|
+
user: ['text', 'inlineData'],
|
|
2605
|
+
function: ['functionResponse'],
|
|
2606
|
+
model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
|
|
2607
|
+
// System instructions shouldn't be in history anyway.
|
|
2608
|
+
system: ['text']
|
|
2609
|
+
};
|
|
2610
|
+
const VALID_PREVIOUS_CONTENT_ROLES = {
|
|
2611
|
+
user: ['model'],
|
|
2612
|
+
function: ['model'],
|
|
2613
|
+
model: ['user', 'function'],
|
|
2614
|
+
// System instructions shouldn't be in history.
|
|
2615
|
+
system: []
|
|
2616
|
+
};
|
|
2617
|
+
function validateChatHistory(history) {
|
|
2618
|
+
let prevContent = null;
|
|
2619
|
+
for (const currContent of history) {
|
|
2620
|
+
const { role, parts } = currContent;
|
|
2621
|
+
if (!prevContent && role !== 'user') {
|
|
2622
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
|
|
2623
|
+
}
|
|
2624
|
+
if (!POSSIBLE_ROLES.includes(role)) {
|
|
2625
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
|
|
2626
|
+
}
|
|
2627
|
+
if (!Array.isArray(parts)) {
|
|
2628
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
|
|
2629
|
+
}
|
|
2630
|
+
if (parts.length === 0) {
|
|
2631
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
|
|
2632
|
+
}
|
|
2633
|
+
const countFields = {
|
|
2634
|
+
text: 0,
|
|
2635
|
+
inlineData: 0,
|
|
2636
|
+
functionCall: 0,
|
|
2637
|
+
functionResponse: 0,
|
|
2638
|
+
thought: 0,
|
|
2639
|
+
thoughtSignature: 0,
|
|
2640
|
+
executableCode: 0,
|
|
2641
|
+
codeExecutionResult: 0
|
|
2642
|
+
};
|
|
2643
|
+
for (const part of parts) {
|
|
2644
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2645
|
+
if (key in part) {
|
|
2646
|
+
countFields[key] += 1;
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
const validParts = VALID_PARTS_PER_ROLE[role];
|
|
2651
|
+
for (const key of VALID_PART_FIELDS) {
|
|
2652
|
+
if (!validParts.includes(key) && countFields[key] > 0) {
|
|
2653
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
|
|
2654
|
+
}
|
|
2655
|
+
}
|
|
2656
|
+
if (prevContent) {
|
|
2657
|
+
const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
|
|
2658
|
+
if (!validPreviousContentRoles.includes(prevContent.role)) {
|
|
2659
|
+
throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
prevContent = currContent;
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
/**
|
|
2667
|
+
* @license
|
|
2668
|
+
* Copyright 2024 Google LLC
|
|
2669
|
+
*
|
|
2670
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2671
|
+
* you may not use this file except in compliance with the License.
|
|
2672
|
+
* You may obtain a copy of the License at
|
|
2673
|
+
*
|
|
2674
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2675
|
+
*
|
|
2676
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
2677
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2678
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2679
|
+
* See the License for the specific language governing permissions and
|
|
2680
|
+
* limitations under the License.
|
|
2681
|
+
*/
|
|
2682
|
+
/**
|
|
2683
|
+
* ChatSession class that enables sending chat messages and stores
|
|
2684
|
+
* history of sent and received messages so far.
|
|
2685
|
+
*
|
|
2686
|
+
* @public
|
|
2687
|
+
*/
|
|
2688
|
+
class ChatSession extends ChatSessionBase {
|
|
2689
|
+
constructor(apiSettings, model, chromeAdapter, params, requestOptions) {
|
|
2690
|
+
super(apiSettings, params, requestOptions);
|
|
2691
|
+
this.model = model;
|
|
2692
|
+
this.chromeAdapter = chromeAdapter;
|
|
2693
|
+
this.params = params;
|
|
2694
|
+
this.requestOptions = requestOptions;
|
|
2695
|
+
if (params?.history) {
|
|
2696
|
+
validateChatHistory(params.history);
|
|
2697
|
+
this._history = params.history;
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
/**
|
|
2701
|
+
* Format Content into a request for generateContent or
|
|
2702
|
+
* generateContentStream.
|
|
2703
|
+
* @internal
|
|
2704
|
+
*/
|
|
2705
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
2706
|
+
return {
|
|
2707
|
+
safetySettings: this.params?.safetySettings,
|
|
2708
|
+
generationConfig: this.params?.generationConfig,
|
|
2709
|
+
tools: this.params?.tools,
|
|
2710
|
+
toolConfig: this.params?.toolConfig,
|
|
2711
|
+
systemInstruction: this.params?.systemInstruction,
|
|
2712
|
+
contents: [...this._history, ...tempHistory, incomingContent]
|
|
2713
|
+
};
|
|
2714
|
+
}
|
|
2715
|
+
/**
|
|
2716
|
+
* Calls default generateContent() (versus a specialized one like
|
|
2717
|
+
* templateGenerateContent).
|
|
2718
|
+
* @internal
|
|
2719
|
+
*/
|
|
2720
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
2721
|
+
return generateContent(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
2722
|
+
...this.requestOptions,
|
|
2723
|
+
...singleRequestOptions
|
|
2724
|
+
});
|
|
2725
|
+
}
|
|
2726
|
+
/**
|
|
2727
|
+
* Calls default generateContentStream() (versus a specialized one like
|
|
2728
|
+
* templateGenerateContentStream).
|
|
2729
|
+
* @internal
|
|
2730
|
+
*/
|
|
2731
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
2732
|
+
return generateContentStream(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
|
|
2733
|
+
...this.requestOptions,
|
|
2734
|
+
...singleRequestOptions
|
|
2735
|
+
});
|
|
2736
|
+
}
|
|
2737
|
+
/**
|
|
2738
|
+
* Sends a chat message and receives a non-streaming
|
|
2739
|
+
* {@link GenerateContentResult}
|
|
2740
|
+
*/
|
|
2741
|
+
async sendMessage(request, singleRequestOptions) {
|
|
2742
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
2743
|
+
}
|
|
2744
|
+
/**
|
|
2745
|
+
* Sends a chat message and receives the response as a
|
|
2746
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
2747
|
+
* and a response promise.
|
|
2748
|
+
*/
|
|
2749
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
2750
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
|
|
2670
2754
|
/**
|
|
2671
2755
|
* @license
|
|
2672
2756
|
* Copyright 2024 Google LLC
|
|
@@ -2834,8 +2918,9 @@ function validateGenerationConfig(generationConfig) {
|
|
|
2834
2918
|
}
|
|
2835
2919
|
if ((generationConfig.responseSchema != null ||
|
|
2836
2920
|
generationConfig.responseJsonSchema != null) &&
|
|
2837
|
-
generationConfig.responseMimeType) {
|
|
2838
|
-
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set
|
|
2921
|
+
generationConfig.responseMimeType !== 'application/json') {
|
|
2922
|
+
throw new AIError(AIErrorCode.UNSUPPORTED, `responseMimeType must be set to "application/json" if` +
|
|
2923
|
+
` responseSchema or responseJsonSchema are set.`);
|
|
2839
2924
|
}
|
|
2840
2925
|
}
|
|
2841
2926
|
|
|
@@ -3267,6 +3352,11 @@ class LiveGenerativeModel extends AIModel {
|
|
|
3267
3352
|
* }
|
|
3268
3353
|
* ```
|
|
3269
3354
|
*
|
|
3355
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3356
|
+
* early as June 2026. As a replacement, you can
|
|
3357
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3358
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3359
|
+
*
|
|
3270
3360
|
* @public
|
|
3271
3361
|
*/
|
|
3272
3362
|
class ImagenModel extends AIModel {
|
|
@@ -3510,6 +3600,112 @@ class WebSocketHandlerImpl {
|
|
|
3510
3600
|
}
|
|
3511
3601
|
}
|
|
3512
3602
|
|
|
3603
|
+
/**
|
|
3604
|
+
* @license
|
|
3605
|
+
* Copyright 2026 Google LLC
|
|
3606
|
+
*
|
|
3607
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
3608
|
+
* you may not use this file except in compliance with the License.
|
|
3609
|
+
* You may obtain a copy of the License at
|
|
3610
|
+
*
|
|
3611
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
3612
|
+
*
|
|
3613
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
3614
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
3615
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
3616
|
+
* See the License for the specific language governing permissions and
|
|
3617
|
+
* limitations under the License.
|
|
3618
|
+
*/
|
|
3619
|
+
/**
|
|
3620
|
+
* `ChatSession` class for use with server prompt templates that
|
|
3621
|
+
* enables sending chat messages and stores history of sent and
|
|
3622
|
+
* received messages so far.
|
|
3623
|
+
*
|
|
3624
|
+
* @beta
|
|
3625
|
+
*/
|
|
3626
|
+
class TemplateChatSession extends ChatSessionBase {
|
|
3627
|
+
constructor(apiSettings, params, requestOptions) {
|
|
3628
|
+
super(apiSettings, params, requestOptions);
|
|
3629
|
+
this.params = params;
|
|
3630
|
+
this.requestOptions = requestOptions;
|
|
3631
|
+
if (params.history) {
|
|
3632
|
+
validateChatHistory(params.history);
|
|
3633
|
+
this._history = params.history;
|
|
3634
|
+
}
|
|
3635
|
+
}
|
|
3636
|
+
/**
|
|
3637
|
+
* Format the internal state to the body payload for `templateGenerateContent`.
|
|
3638
|
+
* @internal
|
|
3639
|
+
*/
|
|
3640
|
+
_formatRequest(incomingContent, tempHistory) {
|
|
3641
|
+
const request = {
|
|
3642
|
+
history: [...this._history, ...tempHistory, incomingContent]
|
|
3643
|
+
};
|
|
3644
|
+
if (this.params.templateVariables) {
|
|
3645
|
+
request.inputs = this.params.templateVariables;
|
|
3646
|
+
}
|
|
3647
|
+
if (this.params.tools) {
|
|
3648
|
+
request.tools = this.params.tools?.map(tool => {
|
|
3649
|
+
if (tool.functionDeclarations) {
|
|
3650
|
+
return {
|
|
3651
|
+
templateFunctions: tool.functionDeclarations.map(declaration => {
|
|
3652
|
+
if (declaration.parameters) {
|
|
3653
|
+
const newDeclaration = { ...declaration };
|
|
3654
|
+
delete newDeclaration.parameters;
|
|
3655
|
+
newDeclaration.inputSchema = declaration.parameters;
|
|
3656
|
+
return newDeclaration;
|
|
3657
|
+
}
|
|
3658
|
+
return declaration;
|
|
3659
|
+
})
|
|
3660
|
+
};
|
|
3661
|
+
}
|
|
3662
|
+
return tool;
|
|
3663
|
+
});
|
|
3664
|
+
}
|
|
3665
|
+
if (this.params.toolConfig) {
|
|
3666
|
+
request.toolConfig = this.params.toolConfig;
|
|
3667
|
+
}
|
|
3668
|
+
return request;
|
|
3669
|
+
}
|
|
3670
|
+
/**
|
|
3671
|
+
* Calls the specific templateGenerateContent() function needed for
|
|
3672
|
+
* this specialized TemplateChatSession.
|
|
3673
|
+
* @internal
|
|
3674
|
+
*/
|
|
3675
|
+
_callGenerateContent(formattedRequest, singleRequestOptions) {
|
|
3676
|
+
return templateGenerateContent(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3677
|
+
...this.requestOptions,
|
|
3678
|
+
...singleRequestOptions
|
|
3679
|
+
});
|
|
3680
|
+
}
|
|
3681
|
+
/**
|
|
3682
|
+
* Calls the specific templateGenerateContentStream() function needed for
|
|
3683
|
+
* this specialized TemplateChatSession.
|
|
3684
|
+
* @internal
|
|
3685
|
+
*/
|
|
3686
|
+
_callGenerateContentStream(formattedRequest, singleRequestOptions) {
|
|
3687
|
+
return templateGenerateContentStream(this._apiSettings, this.params.templateId, formattedRequest, {
|
|
3688
|
+
...this.requestOptions,
|
|
3689
|
+
...singleRequestOptions
|
|
3690
|
+
});
|
|
3691
|
+
}
|
|
3692
|
+
/**
|
|
3693
|
+
* Sends a chat message and receives a non-streaming
|
|
3694
|
+
* {@link GenerateContentResult}
|
|
3695
|
+
*/
|
|
3696
|
+
async sendMessage(request, singleRequestOptions) {
|
|
3697
|
+
return this._sendMessage(request, singleRequestOptions);
|
|
3698
|
+
}
|
|
3699
|
+
/**
|
|
3700
|
+
* Sends a chat message and receives the response as a
|
|
3701
|
+
* {@link GenerateContentStreamResult} containing an iterable stream
|
|
3702
|
+
* and a response promise.
|
|
3703
|
+
*/
|
|
3704
|
+
async sendMessageStream(request, singleRequestOptions) {
|
|
3705
|
+
return this._sendMessageStream(request, singleRequestOptions);
|
|
3706
|
+
}
|
|
3707
|
+
}
|
|
3708
|
+
|
|
3513
3709
|
/**
|
|
3514
3710
|
* @license
|
|
3515
3711
|
* Copyright 2025 Google LLC
|
|
@@ -3575,6 +3771,18 @@ class TemplateGenerativeModel {
|
|
|
3575
3771
|
...singleRequestOptions
|
|
3576
3772
|
});
|
|
3577
3773
|
}
|
|
3774
|
+
/**
|
|
3775
|
+
* Starts a {@link TemplateChatSession} that will use this template to
|
|
3776
|
+
* respond to messages.
|
|
3777
|
+
*
|
|
3778
|
+
* @param params - Configurations for the chat, including the template
|
|
3779
|
+
* ID and input variables.
|
|
3780
|
+
*
|
|
3781
|
+
* @beta
|
|
3782
|
+
*/
|
|
3783
|
+
startChat(params) {
|
|
3784
|
+
return new TemplateChatSession(this._apiSettings, params, this.requestOptions);
|
|
3785
|
+
}
|
|
3578
3786
|
}
|
|
3579
3787
|
|
|
3580
3788
|
/**
|
|
@@ -3598,7 +3806,10 @@ class TemplateGenerativeModel {
|
|
|
3598
3806
|
*
|
|
3599
3807
|
* This class should only be instantiated with {@link getTemplateImagenModel}.
|
|
3600
3808
|
*
|
|
3601
|
-
* @
|
|
3809
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
3810
|
+
* early as June 2026. As a replacement, you can
|
|
3811
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
3812
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
3602
3813
|
*/
|
|
3603
3814
|
class TemplateImagenModel {
|
|
3604
3815
|
/**
|
|
@@ -3907,6 +4118,11 @@ class AnyOfSchema extends Schema {
|
|
|
3907
4118
|
* }
|
|
3908
4119
|
* ```
|
|
3909
4120
|
*
|
|
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)}.
|
|
4125
|
+
*
|
|
3910
4126
|
* @public
|
|
3911
4127
|
*/
|
|
3912
4128
|
class ImagenImageFormat {
|
|
@@ -4407,6 +4623,11 @@ function getGenerativeModel(ai, modelParams, requestOptions) {
|
|
|
4407
4623
|
*
|
|
4408
4624
|
* Only Imagen 3 models (named `imagen-3.0-*`) are supported.
|
|
4409
4625
|
*
|
|
4626
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4627
|
+
* early as June 2026. As a replacement, you can
|
|
4628
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4629
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4630
|
+
*
|
|
4410
4631
|
* @param ai - An {@link AI} instance.
|
|
4411
4632
|
* @param modelParams - Parameters to use when making Imagen requests.
|
|
4412
4633
|
* @param requestOptions - Additional options to use when making requests.
|
|
@@ -4457,10 +4678,13 @@ function getTemplateGenerativeModel(ai, requestOptions) {
|
|
|
4457
4678
|
* Returns a {@link TemplateImagenModel} class for executing server-side
|
|
4458
4679
|
* Imagen templates.
|
|
4459
4680
|
*
|
|
4681
|
+
* @deprecated All Imagen models are deprecated and will shut down as
|
|
4682
|
+
* early as June 2026. As a replacement, you can
|
|
4683
|
+
* {@link https://firebase.google.com/docs/ai-logic/imagen-models-migration |
|
|
4684
|
+
* migrate your apps to use Gemini Image models (the "Nano Banana" models)}.
|
|
4685
|
+
*
|
|
4460
4686
|
* @param ai - An {@link AI} instance.
|
|
4461
4687
|
* @param requestOptions - Additional options to use when making requests.
|
|
4462
|
-
*
|
|
4463
|
-
* @beta
|
|
4464
4688
|
*/
|
|
4465
4689
|
function getTemplateImagenModel(ai, requestOptions) {
|
|
4466
4690
|
return new TemplateImagenModel(ai, requestOptions);
|
|
@@ -4479,5 +4703,5 @@ function registerAI() {
|
|
|
4479
4703
|
}
|
|
4480
4704
|
registerAI();
|
|
4481
4705
|
|
|
4482
|
-
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 };
|
|
4706
|
+
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 };
|
|
4483
4707
|
//# sourceMappingURL=index.node.mjs.map
|