@firebase/ai 2.5.0 → 2.6.0-20251112180857

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/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.5.0";
11
+ var version = "2.6.0-20251112180857";
12
12
 
13
13
  /**
14
14
  * @license
@@ -770,6 +770,18 @@ class GoogleAIBackend extends Backend {
770
770
  constructor() {
771
771
  super(BackendType.GOOGLE_AI);
772
772
  }
773
+ /**
774
+ * @internal
775
+ */
776
+ _getModelPath(project, model) {
777
+ return `/${DEFAULT_API_VERSION}/projects/${project}/${model}`;
778
+ }
779
+ /**
780
+ * @internal
781
+ */
782
+ _getTemplatePath(project, templateId) {
783
+ return `/${DEFAULT_API_VERSION}/projects/${project}/templates/${templateId}`;
784
+ }
773
785
  }
774
786
  /**
775
787
  * Configuration class for the Vertex AI Gemini API.
@@ -796,6 +808,18 @@ class VertexAIBackend extends Backend {
796
808
  this.location = location;
797
809
  }
798
810
  }
811
+ /**
812
+ * @internal
813
+ */
814
+ _getModelPath(project, model) {
815
+ return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/${model}`;
816
+ }
817
+ /**
818
+ * @internal
819
+ */
820
+ _getTemplatePath(project, templateId) {
821
+ return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/templates/${templateId}`;
822
+ }
799
823
  }
800
824
 
801
825
  /**
@@ -1253,6 +1277,67 @@ function factory(container, { instanceIdentifier }) {
1253
1277
  return new AIService(app, backend, auth, appCheckProvider, chromeAdapterFactory);
1254
1278
  }
1255
1279
 
1280
+ /**
1281
+ * @license
1282
+ * Copyright 2025 Google LLC
1283
+ *
1284
+ * Licensed under the Apache License, Version 2.0 (the "License");
1285
+ * you may not use this file except in compliance with the License.
1286
+ * You may obtain a copy of the License at
1287
+ *
1288
+ * http://www.apache.org/licenses/LICENSE-2.0
1289
+ *
1290
+ * Unless required by applicable law or agreed to in writing, software
1291
+ * distributed under the License is distributed on an "AS IS" BASIS,
1292
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1293
+ * See the License for the specific language governing permissions and
1294
+ * limitations under the License.
1295
+ */
1296
+ /**
1297
+ * Initializes an {@link ApiSettings} object from an {@link AI} instance.
1298
+ *
1299
+ * If this is a Server App, the {@link ApiSettings} object's `getAppCheckToken()` will resolve
1300
+ * with the `FirebaseServerAppSettings.appCheckToken`, instead of requiring that an App Check
1301
+ * instance is initialized.
1302
+ */
1303
+ function initApiSettings(ai) {
1304
+ if (!ai.app?.options?.apiKey) {
1305
+ throw new AIError(AIErrorCode.NO_API_KEY, `The "apiKey" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid API key.`);
1306
+ }
1307
+ else if (!ai.app?.options?.projectId) {
1308
+ throw new AIError(AIErrorCode.NO_PROJECT_ID, `The "projectId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid project ID.`);
1309
+ }
1310
+ else if (!ai.app?.options?.appId) {
1311
+ throw new AIError(AIErrorCode.NO_APP_ID, `The "appId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid app ID.`);
1312
+ }
1313
+ const apiSettings = {
1314
+ apiKey: ai.app.options.apiKey,
1315
+ project: ai.app.options.projectId,
1316
+ appId: ai.app.options.appId,
1317
+ automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
1318
+ location: ai.location,
1319
+ backend: ai.backend
1320
+ };
1321
+ if (app._isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
1322
+ const token = ai.app.settings.appCheckToken;
1323
+ apiSettings.getAppCheckToken = () => {
1324
+ return Promise.resolve({ token });
1325
+ };
1326
+ }
1327
+ else if (ai.appCheck) {
1328
+ if (ai.options?.useLimitedUseAppCheckTokens) {
1329
+ apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
1330
+ }
1331
+ else {
1332
+ apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
1333
+ }
1334
+ }
1335
+ if (ai.auth) {
1336
+ apiSettings.getAuthToken = () => ai.auth.getToken();
1337
+ }
1338
+ return apiSettings;
1339
+ }
1340
+
1256
1341
  /**
1257
1342
  * @license
1258
1343
  * Copyright 2025 Google LLC
@@ -1296,43 +1381,8 @@ class AIModel {
1296
1381
  * @internal
1297
1382
  */
1298
1383
  constructor(ai, modelName) {
1299
- if (!ai.app?.options?.apiKey) {
1300
- throw new AIError(AIErrorCode.NO_API_KEY, `The "apiKey" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid API key.`);
1301
- }
1302
- else if (!ai.app?.options?.projectId) {
1303
- throw new AIError(AIErrorCode.NO_PROJECT_ID, `The "projectId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid project ID.`);
1304
- }
1305
- else if (!ai.app?.options?.appId) {
1306
- throw new AIError(AIErrorCode.NO_APP_ID, `The "appId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid app ID.`);
1307
- }
1308
- else {
1309
- this._apiSettings = {
1310
- apiKey: ai.app.options.apiKey,
1311
- project: ai.app.options.projectId,
1312
- appId: ai.app.options.appId,
1313
- automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
1314
- location: ai.location,
1315
- backend: ai.backend
1316
- };
1317
- if (app._isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
1318
- const token = ai.app.settings.appCheckToken;
1319
- this._apiSettings.getAppCheckToken = () => {
1320
- return Promise.resolve({ token });
1321
- };
1322
- }
1323
- else if (ai.appCheck) {
1324
- if (ai.options?.useLimitedUseAppCheckTokens) {
1325
- this._apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
1326
- }
1327
- else {
1328
- this._apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
1329
- }
1330
- }
1331
- if (ai.auth) {
1332
- this._apiSettings.getAuthToken = () => ai.auth.getToken();
1333
- }
1334
- this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
1335
- }
1384
+ this._apiSettings = initApiSettings(ai);
1385
+ this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
1336
1386
  }
1337
1387
  /**
1338
1388
  * Normalizes the given model name to a fully qualified model resource name.
@@ -1381,7 +1431,7 @@ class AIModel {
1381
1431
 
1382
1432
  /**
1383
1433
  * @license
1384
- * Copyright 2024 Google LLC
1434
+ * Copyright 2025 Google LLC
1385
1435
  *
1386
1436
  * Licensed under the Apache License, Version 2.0 (the "License");
1387
1437
  * you may not use this file except in compliance with the License.
@@ -1395,47 +1445,33 @@ class AIModel {
1395
1445
  * See the License for the specific language governing permissions and
1396
1446
  * limitations under the License.
1397
1447
  */
1398
- var Task;
1399
- (function (Task) {
1400
- Task["GENERATE_CONTENT"] = "generateContent";
1401
- Task["STREAM_GENERATE_CONTENT"] = "streamGenerateContent";
1402
- Task["COUNT_TOKENS"] = "countTokens";
1403
- Task["PREDICT"] = "predict";
1404
- })(Task || (Task = {}));
1405
- class RequestUrl {
1406
- constructor(model, task, apiSettings, stream, requestOptions) {
1407
- this.model = model;
1408
- this.task = task;
1409
- this.apiSettings = apiSettings;
1410
- this.stream = stream;
1411
- this.requestOptions = requestOptions;
1448
+ class RequestURL {
1449
+ constructor(params) {
1450
+ this.params = params;
1412
1451
  }
1413
1452
  toString() {
1414
1453
  const url = new URL(this.baseUrl); // Throws if the URL is invalid
1415
- url.pathname = `/${this.apiVersion}/${this.modelPath}:${this.task}`;
1454
+ url.pathname = this.pathname;
1416
1455
  url.search = this.queryParams.toString();
1417
1456
  return url.toString();
1418
1457
  }
1419
- get baseUrl() {
1420
- return this.requestOptions?.baseUrl || `https://${DEFAULT_DOMAIN}`;
1421
- }
1422
- get apiVersion() {
1423
- return DEFAULT_API_VERSION; // TODO: allow user-set options if that feature becomes available
1424
- }
1425
- get modelPath() {
1426
- if (this.apiSettings.backend instanceof GoogleAIBackend) {
1427
- return `projects/${this.apiSettings.project}/${this.model}`;
1428
- }
1429
- else if (this.apiSettings.backend instanceof VertexAIBackend) {
1430
- return `projects/${this.apiSettings.project}/locations/${this.apiSettings.backend.location}/${this.model}`;
1458
+ get pathname() {
1459
+ // We need to construct a different URL if the request is for server side prompt templates,
1460
+ // since the URL patterns are different. Server side prompt templates expect a templateId
1461
+ // instead of a model name.
1462
+ if (this.params.templateId) {
1463
+ return `${this.params.apiSettings.backend._getTemplatePath(this.params.apiSettings.project, this.params.templateId)}:${this.params.task}`;
1431
1464
  }
1432
1465
  else {
1433
- throw new AIError(AIErrorCode.ERROR, `Invalid backend: ${JSON.stringify(this.apiSettings.backend)}`);
1466
+ return `${this.params.apiSettings.backend._getModelPath(this.params.apiSettings.project, this.params.model)}:${this.params.task}`;
1434
1467
  }
1435
1468
  }
1469
+ get baseUrl() {
1470
+ return this.params.requestOptions?.baseUrl ?? `https://${DEFAULT_DOMAIN}`;
1471
+ }
1436
1472
  get queryParams() {
1437
1473
  const params = new URLSearchParams();
1438
- if (this.stream) {
1474
+ if (this.params.stream) {
1439
1475
  params.set('alt', 'sse');
1440
1476
  }
1441
1477
  return params;
@@ -1475,12 +1511,12 @@ async function getHeaders(url) {
1475
1511
  const headers = new Headers();
1476
1512
  headers.append('Content-Type', 'application/json');
1477
1513
  headers.append('x-goog-api-client', getClientHeaders());
1478
- headers.append('x-goog-api-key', url.apiSettings.apiKey);
1479
- if (url.apiSettings.automaticDataCollectionEnabled) {
1480
- headers.append('X-Firebase-Appid', url.apiSettings.appId);
1514
+ headers.append('x-goog-api-key', url.params.apiSettings.apiKey);
1515
+ if (url.params.apiSettings.automaticDataCollectionEnabled) {
1516
+ headers.append('X-Firebase-Appid', url.params.apiSettings.appId);
1481
1517
  }
1482
- if (url.apiSettings.getAppCheckToken) {
1483
- const appCheckToken = await url.apiSettings.getAppCheckToken();
1518
+ if (url.params.apiSettings.getAppCheckToken) {
1519
+ const appCheckToken = await url.params.apiSettings.getAppCheckToken();
1484
1520
  if (appCheckToken) {
1485
1521
  headers.append('X-Firebase-AppCheck', appCheckToken.token);
1486
1522
  if (appCheckToken.error) {
@@ -1488,39 +1524,33 @@ async function getHeaders(url) {
1488
1524
  }
1489
1525
  }
1490
1526
  }
1491
- if (url.apiSettings.getAuthToken) {
1492
- const authToken = await url.apiSettings.getAuthToken();
1527
+ if (url.params.apiSettings.getAuthToken) {
1528
+ const authToken = await url.params.apiSettings.getAuthToken();
1493
1529
  if (authToken) {
1494
1530
  headers.append('Authorization', `Firebase ${authToken.accessToken}`);
1495
1531
  }
1496
1532
  }
1497
1533
  return headers;
1498
1534
  }
1499
- async function constructRequest(model, task, apiSettings, stream, body, requestOptions) {
1500
- const url = new RequestUrl(model, task, apiSettings, stream, requestOptions);
1501
- return {
1502
- url: url.toString(),
1503
- fetchOptions: {
1504
- method: 'POST',
1505
- headers: await getHeaders(url),
1506
- body
1507
- }
1508
- };
1509
- }
1510
- async function makeRequest(model, task, apiSettings, stream, body, requestOptions) {
1511
- const url = new RequestUrl(model, task, apiSettings, stream, requestOptions);
1535
+ async function makeRequest(requestUrlParams, body) {
1536
+ const url = new RequestURL(requestUrlParams);
1512
1537
  let response;
1513
1538
  let fetchTimeoutId;
1514
1539
  try {
1515
- const request = await constructRequest(model, task, apiSettings, stream, body, requestOptions);
1516
- // Timeout is 180s by default
1517
- const timeoutMillis = requestOptions?.timeout != null && requestOptions.timeout >= 0
1518
- ? requestOptions.timeout
1540
+ const fetchOptions = {
1541
+ method: 'POST',
1542
+ headers: await getHeaders(url),
1543
+ body
1544
+ };
1545
+ // Timeout is 180s by default.
1546
+ const timeoutMillis = requestUrlParams.requestOptions?.timeout != null &&
1547
+ requestUrlParams.requestOptions.timeout >= 0
1548
+ ? requestUrlParams.requestOptions.timeout
1519
1549
  : DEFAULT_FETCH_TIMEOUT_MS;
1520
1550
  const abortController = new AbortController();
1521
1551
  fetchTimeoutId = setTimeout(() => abortController.abort(), timeoutMillis);
1522
- request.fetchOptions.signal = abortController.signal;
1523
- response = await fetch(request.url, request.fetchOptions);
1552
+ fetchOptions.signal = abortController.signal;
1553
+ response = await fetch(url.toString(), fetchOptions);
1524
1554
  if (!response.ok) {
1525
1555
  let message = '';
1526
1556
  let errorDetails;
@@ -1542,7 +1572,7 @@ async function makeRequest(model, task, apiSettings, stream, body, requestOption
1542
1572
  throw new AIError(AIErrorCode.API_NOT_ENABLED, `The Firebase AI SDK requires the Firebase AI ` +
1543
1573
  `API ('firebasevertexai.googleapis.com') to be enabled in your ` +
1544
1574
  `Firebase project. Enable this API by visiting the Firebase Console ` +
1545
- `at https://console.firebase.google.com/project/${url.apiSettings.project}/genai/ ` +
1575
+ `at https://console.firebase.google.com/project/${url.params.apiSettings.project}/genai/ ` +
1546
1576
  `and clicking "Get started". If you enabled this API recently, ` +
1547
1577
  `wait a few minutes for the action to propagate to our systems and ` +
1548
1578
  `then retry.`, {
@@ -2283,8 +2313,13 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
2283
2313
  if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2284
2314
  params = mapGenerateContentRequest(params);
2285
2315
  }
2286
- return makeRequest(model, Task.STREAM_GENERATE_CONTENT, apiSettings,
2287
- /* stream */ true, JSON.stringify(params), requestOptions);
2316
+ return makeRequest({
2317
+ task: "streamGenerateContent" /* Task.STREAM_GENERATE_CONTENT */,
2318
+ model,
2319
+ apiSettings,
2320
+ stream: true,
2321
+ requestOptions
2322
+ }, JSON.stringify(params));
2288
2323
  }
2289
2324
  async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
2290
2325
  const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
@@ -2294,8 +2329,37 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
2294
2329
  if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2295
2330
  params = mapGenerateContentRequest(params);
2296
2331
  }
2297
- return makeRequest(model, Task.GENERATE_CONTENT, apiSettings,
2298
- /* stream */ false, JSON.stringify(params), requestOptions);
2332
+ return makeRequest({
2333
+ model,
2334
+ task: "generateContent" /* Task.GENERATE_CONTENT */,
2335
+ apiSettings,
2336
+ stream: false,
2337
+ requestOptions
2338
+ }, JSON.stringify(params));
2339
+ }
2340
+ async function templateGenerateContent(apiSettings, templateId, templateParams, requestOptions) {
2341
+ const response = await makeRequest({
2342
+ task: "templateGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT */,
2343
+ templateId,
2344
+ apiSettings,
2345
+ stream: false,
2346
+ requestOptions
2347
+ }, JSON.stringify(templateParams));
2348
+ const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
2349
+ const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
2350
+ return {
2351
+ response: enhancedResponse
2352
+ };
2353
+ }
2354
+ async function templateGenerateContentStream(apiSettings, templateId, templateParams, requestOptions) {
2355
+ const response = await makeRequest({
2356
+ task: "templateStreamGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_STREAM_GENERATE_CONTENT */,
2357
+ templateId,
2358
+ apiSettings,
2359
+ stream: true,
2360
+ requestOptions
2361
+ }, JSON.stringify(templateParams));
2362
+ return processStream(response, apiSettings);
2299
2363
  }
2300
2364
  async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
2301
2365
  const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
@@ -2707,7 +2771,13 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
2707
2771
  else {
2708
2772
  body = JSON.stringify(params);
2709
2773
  }
2710
- const response = await makeRequest(model, Task.COUNT_TOKENS, apiSettings, false, body, requestOptions);
2774
+ const response = await makeRequest({
2775
+ model,
2776
+ task: "countTokens" /* Task.COUNT_TOKENS */,
2777
+ apiSettings,
2778
+ stream: false,
2779
+ requestOptions
2780
+ }, body);
2711
2781
  return response.json();
2712
2782
  }
2713
2783
  async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
@@ -3260,8 +3330,13 @@ class ImagenModel extends AIModel {
3260
3330
  ...this.generationConfig,
3261
3331
  ...this.safetySettings
3262
3332
  });
3263
- const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
3264
- /* stream */ false, JSON.stringify(body), this.requestOptions);
3333
+ const response = await makeRequest({
3334
+ task: "predict" /* Task.PREDICT */,
3335
+ model: this.model,
3336
+ apiSettings: this._apiSettings,
3337
+ stream: false,
3338
+ requestOptions: this.requestOptions
3339
+ }, JSON.stringify(body));
3265
3340
  return handlePredictResponse(response);
3266
3341
  }
3267
3342
  /**
@@ -3289,8 +3364,13 @@ class ImagenModel extends AIModel {
3289
3364
  ...this.generationConfig,
3290
3365
  ...this.safetySettings
3291
3366
  });
3292
- const response = await makeRequest(this.model, Task.PREDICT, this._apiSettings,
3293
- /* stream */ false, JSON.stringify(body), this.requestOptions);
3367
+ const response = await makeRequest({
3368
+ task: "predict" /* Task.PREDICT */,
3369
+ model: this.model,
3370
+ apiSettings: this._apiSettings,
3371
+ stream: false,
3372
+ requestOptions: this.requestOptions
3373
+ }, JSON.stringify(body));
3294
3374
  return handlePredictResponse(response);
3295
3375
  }
3296
3376
  }
@@ -3442,6 +3522,121 @@ class WebSocketHandlerImpl {
3442
3522
  }
3443
3523
  }
3444
3524
 
3525
+ /**
3526
+ * @license
3527
+ * Copyright 2025 Google LLC
3528
+ *
3529
+ * Licensed under the Apache License, Version 2.0 (the "License");
3530
+ * you may not use this file except in compliance with the License.
3531
+ * You may obtain a copy of the License at
3532
+ *
3533
+ * http://www.apache.org/licenses/LICENSE-2.0
3534
+ *
3535
+ * Unless required by applicable law or agreed to in writing, software
3536
+ * distributed under the License is distributed on an "AS IS" BASIS,
3537
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3538
+ * See the License for the specific language governing permissions and
3539
+ * limitations under the License.
3540
+ */
3541
+ /**
3542
+ * {@link GenerativeModel} APIs that execute on a server-side template.
3543
+ *
3544
+ * This class should only be instantiated with {@link getTemplateGenerativeModel}.
3545
+ *
3546
+ * @beta
3547
+ */
3548
+ class TemplateGenerativeModel {
3549
+ /**
3550
+ * @hideconstructor
3551
+ */
3552
+ constructor(ai, requestOptions) {
3553
+ this.requestOptions = requestOptions || {};
3554
+ this._apiSettings = initApiSettings(ai);
3555
+ }
3556
+ /**
3557
+ * Makes a single non-streaming call to the model and returns an object
3558
+ * containing a single {@link GenerateContentResponse}.
3559
+ *
3560
+ * @param templateId - The ID of the server-side template to execute.
3561
+ * @param templateVariables - A key-value map of variables to populate the
3562
+ * template with.
3563
+ *
3564
+ * @beta
3565
+ */
3566
+ async generateContent(templateId, templateVariables // anything!
3567
+ ) {
3568
+ return templateGenerateContent(this._apiSettings, templateId, { inputs: templateVariables }, this.requestOptions);
3569
+ }
3570
+ /**
3571
+ * Makes a single streaming call to the model and returns an object
3572
+ * containing an iterable stream that iterates over all chunks in the
3573
+ * streaming response as well as a promise that returns the final aggregated
3574
+ * response.
3575
+ *
3576
+ * @param templateId - The ID of the server-side template to execute.
3577
+ * @param templateVariables - A key-value map of variables to populate the
3578
+ * template with.
3579
+ *
3580
+ * @beta
3581
+ */
3582
+ async generateContentStream(templateId, templateVariables) {
3583
+ return templateGenerateContentStream(this._apiSettings, templateId, { inputs: templateVariables }, this.requestOptions);
3584
+ }
3585
+ }
3586
+
3587
+ /**
3588
+ * @license
3589
+ * Copyright 2025 Google LLC
3590
+ *
3591
+ * Licensed under the Apache License, Version 2.0 (the "License");
3592
+ * you may not use this file except in compliance with the License.
3593
+ * You may obtain a copy of the License at
3594
+ *
3595
+ * http://www.apache.org/licenses/LICENSE-2.0
3596
+ *
3597
+ * Unless required by applicable law or agreed to in writing, software
3598
+ * distributed under the License is distributed on an "AS IS" BASIS,
3599
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3600
+ * See the License for the specific language governing permissions and
3601
+ * limitations under the License.
3602
+ */
3603
+ /**
3604
+ * Class for Imagen model APIs that execute on a server-side template.
3605
+ *
3606
+ * This class should only be instantiated with {@link getTemplateImagenModel}.
3607
+ *
3608
+ * @beta
3609
+ */
3610
+ class TemplateImagenModel {
3611
+ /**
3612
+ * @hideconstructor
3613
+ */
3614
+ constructor(ai, requestOptions) {
3615
+ this.requestOptions = requestOptions || {};
3616
+ this._apiSettings = initApiSettings(ai);
3617
+ }
3618
+ /**
3619
+ * Makes a single call to the model and returns an object containing a single
3620
+ * {@link ImagenGenerationResponse}.
3621
+ *
3622
+ * @param templateId - The ID of the server-side template to execute.
3623
+ * @param templateVariables - A key-value map of variables to populate the
3624
+ * template with.
3625
+ *
3626
+ * @beta
3627
+ */
3628
+ async generateImages(templateId, templateVariables) {
3629
+ const response = await makeRequest({
3630
+ task: "templatePredict" /* ServerPromptTemplateTask.TEMPLATE_PREDICT */,
3631
+ templateId,
3632
+ apiSettings: this._apiSettings,
3633
+ stream: false,
3634
+ requestOptions: this.requestOptions
3635
+ }, JSON.stringify({ inputs: templateVariables }));
3636
+ return handlePredictResponse(response);
3637
+ }
3638
+ }
3639
+
3445
3640
  /**
3446
3641
  * @license
3447
3642
  * Copyright 2024 Google LLC
@@ -4233,6 +4428,30 @@ function getLiveGenerativeModel(ai, modelParams) {
4233
4428
  const webSocketHandler = new WebSocketHandlerImpl();
4234
4429
  return new LiveGenerativeModel(ai, modelParams, webSocketHandler);
4235
4430
  }
4431
+ /**
4432
+ * Returns a {@link TemplateGenerativeModel} class for executing server-side
4433
+ * templates.
4434
+ *
4435
+ * @param ai - An {@link AI} instance.
4436
+ * @param requestOptions - Additional options to use when making requests.
4437
+ *
4438
+ * @beta
4439
+ */
4440
+ function getTemplateGenerativeModel(ai, requestOptions) {
4441
+ return new TemplateGenerativeModel(ai, requestOptions);
4442
+ }
4443
+ /**
4444
+ * Returns a {@link TemplateImagenModel} class for executing server-side
4445
+ * Imagen templates.
4446
+ *
4447
+ * @param ai - An {@link AI} instance.
4448
+ * @param requestOptions - Additional options to use when making requests.
4449
+ *
4450
+ * @beta
4451
+ */
4452
+ function getTemplateImagenModel(ai, requestOptions) {
4453
+ return new TemplateImagenModel(ai, requestOptions);
4454
+ }
4236
4455
 
4237
4456
  /**
4238
4457
  * The Firebase AI Web SDK.
@@ -4287,11 +4506,15 @@ exports.ResponseModality = ResponseModality;
4287
4506
  exports.Schema = Schema;
4288
4507
  exports.SchemaType = SchemaType;
4289
4508
  exports.StringSchema = StringSchema;
4509
+ exports.TemplateGenerativeModel = TemplateGenerativeModel;
4510
+ exports.TemplateImagenModel = TemplateImagenModel;
4290
4511
  exports.URLRetrievalStatus = URLRetrievalStatus;
4291
4512
  exports.VertexAIBackend = VertexAIBackend;
4292
4513
  exports.getAI = getAI;
4293
4514
  exports.getGenerativeModel = getGenerativeModel;
4294
4515
  exports.getImagenModel = getImagenModel;
4295
4516
  exports.getLiveGenerativeModel = getLiveGenerativeModel;
4517
+ exports.getTemplateGenerativeModel = getTemplateGenerativeModel;
4518
+ exports.getTemplateImagenModel = getTemplateImagenModel;
4296
4519
  exports.startAudioConversation = startAudioConversation;
4297
4520
  //# sourceMappingURL=index.cjs.js.map