@firebase/ai 2.2.1 → 2.3.0-caching-fdc.9f17eac6e

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.
Files changed (35) hide show
  1. package/dist/ai-public.d.ts +290 -33
  2. package/dist/ai.d.ts +297 -33
  3. package/dist/esm/index.esm.js +2801 -2617
  4. package/dist/esm/index.esm.js.map +1 -1
  5. package/dist/esm/src/factory-browser.d.ts +19 -0
  6. package/dist/esm/src/methods/live-session-helpers.d.ts +2 -2
  7. package/dist/esm/src/methods/live-session.d.ts +10 -1
  8. package/dist/esm/src/requests/hybrid-helpers.d.ts +28 -0
  9. package/dist/esm/src/types/chrome-adapter.d.ts +2 -2
  10. package/dist/esm/src/types/content.d.ts +81 -2
  11. package/dist/esm/src/types/enums.d.ts +53 -4
  12. package/dist/esm/src/types/googleai.d.ts +2 -1
  13. package/dist/esm/src/types/language-model.d.ts +10 -20
  14. package/dist/esm/src/types/live-responses.d.ts +9 -1
  15. package/dist/esm/src/types/requests.d.ts +35 -7
  16. package/dist/esm/src/types/responses.d.ts +92 -0
  17. package/dist/index.cjs.js +2803 -2616
  18. package/dist/index.cjs.js.map +1 -1
  19. package/dist/index.node.cjs.js +212 -42
  20. package/dist/index.node.cjs.js.map +1 -1
  21. package/dist/index.node.mjs +210 -43
  22. package/dist/index.node.mjs.map +1 -1
  23. package/dist/src/factory-browser.d.ts +19 -0
  24. package/dist/src/methods/live-session-helpers.d.ts +2 -2
  25. package/dist/src/methods/live-session.d.ts +10 -1
  26. package/dist/src/requests/hybrid-helpers.d.ts +28 -0
  27. package/dist/src/types/chrome-adapter.d.ts +2 -2
  28. package/dist/src/types/content.d.ts +81 -2
  29. package/dist/src/types/enums.d.ts +53 -4
  30. package/dist/src/types/googleai.d.ts +2 -1
  31. package/dist/src/types/language-model.d.ts +10 -20
  32. package/dist/src/types/live-responses.d.ts +9 -1
  33. package/dist/src/types/requests.d.ts +35 -7
  34. package/dist/src/types/responses.d.ts +92 -0
  35. package/package.json +10 -9
@@ -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.2.1";
11
+ var version = "2.3.0-caching-fdc.9f17eac6e";
12
12
 
13
13
  /**
14
14
  * @license
@@ -301,14 +301,51 @@ const ResponseModality = {
301
301
  AUDIO: 'AUDIO'
302
302
  };
303
303
  /**
304
- * <b>(EXPERIMENTAL)</b>
305
304
  * Determines whether inference happens on-device or in-cloud.
306
- * @public
305
+ *
306
+ * @remarks
307
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
308
+ * on-device model. If on-device inference is not available, the SDK
309
+ * will fall back to using a cloud-hosted model.
310
+ * <br/>
311
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
312
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
313
+ * If on-device inference is not available, inference methods will throw.
314
+ * <br/>
315
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
316
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
317
+ * <br/>
318
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
319
+ * cloud-hosted model. If not available, the SDK will fall back to an
320
+ * on-device model.
321
+ *
322
+ * @beta
307
323
  */
308
324
  const InferenceMode = {
309
325
  'PREFER_ON_DEVICE': 'prefer_on_device',
310
326
  'ONLY_ON_DEVICE': 'only_on_device',
311
- 'ONLY_IN_CLOUD': 'only_in_cloud'
327
+ 'ONLY_IN_CLOUD': 'only_in_cloud',
328
+ 'PREFER_IN_CLOUD': 'prefer_in_cloud'
329
+ };
330
+ /**
331
+ * Represents the result of the code execution.
332
+ *
333
+ * @beta
334
+ */
335
+ const Outcome = {
336
+ UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
337
+ OK: 'OUTCOME_OK',
338
+ FAILED: 'OUTCOME_FAILED',
339
+ DEADLINE_EXCEEDED: 'OUTCOME_DEADLINE_EXCEEDED'
340
+ };
341
+ /**
342
+ * The programming language of the code.
343
+ *
344
+ * @beta
345
+ */
346
+ const Language = {
347
+ UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
348
+ PYTHON: 'PYTHON'
312
349
  };
313
350
 
314
351
  /**
@@ -327,6 +364,45 @@ const InferenceMode = {
327
364
  * See the License for the specific language governing permissions and
328
365
  * limitations under the License.
329
366
  */
367
+ /**
368
+ * The status of a URL retrieval.
369
+ *
370
+ * @remarks
371
+ * <b>URL_RETRIEVAL_STATUS_UNSPECIFIED:</b> Unspecified retrieval status.
372
+ * <br/>
373
+ * <b>URL_RETRIEVAL_STATUS_SUCCESS:</b> The URL retrieval was successful.
374
+ * <br/>
375
+ * <b>URL_RETRIEVAL_STATUS_ERROR:</b> The URL retrieval failed.
376
+ * <br/>
377
+ * <b>URL_RETRIEVAL_STATUS_PAYWALL:</b> The URL retrieval failed because the content is behind a paywall.
378
+ * <br/>
379
+ * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
380
+ * <br/>
381
+ *
382
+ * @beta
383
+ */
384
+ const URLRetrievalStatus = {
385
+ /**
386
+ * Unspecified retrieval status.
387
+ */
388
+ URL_RETRIEVAL_STATUS_UNSPECIFIED: 'URL_RETRIEVAL_STATUS_UNSPECIFIED',
389
+ /**
390
+ * The URL retrieval was successful.
391
+ */
392
+ URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS',
393
+ /**
394
+ * The URL retrieval failed.
395
+ */
396
+ URL_RETRIEVAL_STATUS_ERROR: 'URL_RETRIEVAL_STATUS_ERROR',
397
+ /**
398
+ * The URL retrieval failed because the content is behind a paywall.
399
+ */
400
+ URL_RETRIEVAL_STATUS_PAYWALL: 'URL_RETRIEVAL_STATUS_PAYWALL',
401
+ /**
402
+ * The URL retrieval failed because the content is unsafe.
403
+ */
404
+ URL_RETRIEVAL_STATUS_UNSAFE: 'URL_RETRIEVAL_STATUS_UNSAFE'
405
+ };
330
406
  /**
331
407
  * The types of responses that can be returned by {@link LiveSession.receive}.
332
408
  *
@@ -1520,7 +1596,7 @@ function mapGenerateContentCandidates(candidates) {
1520
1596
  // videoMetadata is not supported.
1521
1597
  // Throw early since developers may send a long video as input and only expect to pay
1522
1598
  // for inference on a small portion of the video.
1523
- if (candidate.content?.parts.some(part => part?.videoMetadata)) {
1599
+ if (candidate.content?.parts?.some(part => part?.videoMetadata)) {
1524
1600
  throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
1525
1601
  }
1526
1602
  const mappedCandidate = {
@@ -1530,7 +1606,8 @@ function mapGenerateContentCandidates(candidates) {
1530
1606
  finishMessage: candidate.finishMessage,
1531
1607
  safetyRatings: mappedSafetyRatings,
1532
1608
  citationMetadata,
1533
- groundingMetadata: candidate.groundingMetadata
1609
+ groundingMetadata: candidate.groundingMetadata,
1610
+ urlContextMetadata: candidate.urlContextMetadata
1534
1611
  };
1535
1612
  mappedCandidates.push(mappedCandidate);
1536
1613
  });
@@ -1621,6 +1698,14 @@ async function* generateResponseSequence(stream, apiSettings) {
1621
1698
  else {
1622
1699
  enhancedResponse = createEnhancedContentResponse(value);
1623
1700
  }
1701
+ const firstCandidate = enhancedResponse.candidates?.[0];
1702
+ // Don't yield a response with no useful data for the developer.
1703
+ if (!firstCandidate?.content?.parts &&
1704
+ !firstCandidate?.finishReason &&
1705
+ !firstCandidate?.citationMetadata &&
1706
+ !firstCandidate?.urlContextMetadata) {
1707
+ continue;
1708
+ }
1624
1709
  yield enhancedResponse;
1625
1710
  }
1626
1711
  }
@@ -1700,36 +1785,43 @@ function aggregateResponses(responses) {
1700
1785
  candidate.safetyRatings;
1701
1786
  aggregatedResponse.candidates[i].groundingMetadata =
1702
1787
  candidate.groundingMetadata;
1788
+ // The urlContextMetadata object is defined in the first chunk of the response stream.
1789
+ // In all subsequent chunks, the urlContextMetadata object will be undefined. We need to
1790
+ // make sure that we don't overwrite the first value urlContextMetadata object with undefined.
1791
+ // FIXME: What happens if we receive a second, valid urlContextMetadata object?
1792
+ const urlContextMetadata = candidate.urlContextMetadata;
1793
+ if (typeof urlContextMetadata === 'object' &&
1794
+ urlContextMetadata !== null &&
1795
+ Object.keys(urlContextMetadata).length > 0) {
1796
+ aggregatedResponse.candidates[i].urlContextMetadata =
1797
+ urlContextMetadata;
1798
+ }
1703
1799
  /**
1704
1800
  * Candidates should always have content and parts, but this handles
1705
1801
  * possible malformed responses.
1706
1802
  */
1707
- if (candidate.content && candidate.content.parts) {
1803
+ if (candidate.content) {
1804
+ // Skip a candidate without parts.
1805
+ if (!candidate.content.parts) {
1806
+ continue;
1807
+ }
1708
1808
  if (!aggregatedResponse.candidates[i].content) {
1709
1809
  aggregatedResponse.candidates[i].content = {
1710
1810
  role: candidate.content.role || 'user',
1711
1811
  parts: []
1712
1812
  };
1713
1813
  }
1714
- const newPart = {};
1715
1814
  for (const part of candidate.content.parts) {
1716
- if (part.text !== undefined) {
1717
- // The backend can send empty text parts. If these are sent back
1718
- // (e.g. in chat history), the backend will respond with an error.
1719
- // To prevent this, ignore empty text parts.
1720
- if (part.text === '') {
1721
- continue;
1722
- }
1723
- newPart.text = part.text;
1815
+ const newPart = { ...part };
1816
+ // The backend can send empty text parts. If these are sent back
1817
+ // (e.g. in chat history), the backend will respond with an error.
1818
+ // To prevent this, ignore empty text parts.
1819
+ if (part.text === '') {
1820
+ continue;
1724
1821
  }
1725
- if (part.functionCall) {
1726
- newPart.functionCall = part.functionCall;
1822
+ if (Object.keys(newPart).length > 0) {
1823
+ aggregatedResponse.candidates[i].content.parts.push(newPart);
1727
1824
  }
1728
- if (Object.keys(newPart).length === 0) {
1729
- throw new AIError(AIErrorCode.INVALID_CONTENT, 'Part should have at least one property, but there are none. This is likely caused ' +
1730
- 'by a malformed response from the backend.');
1731
- }
1732
- aggregatedResponse.candidates[i].content.parts.push(newPart);
1733
1825
  }
1734
1826
  }
1735
1827
  }
@@ -1738,6 +1830,72 @@ function aggregateResponses(responses) {
1738
1830
  return aggregatedResponse;
1739
1831
  }
1740
1832
 
1833
+ /**
1834
+ * @license
1835
+ * Copyright 2025 Google LLC
1836
+ *
1837
+ * Licensed under the Apache License, Version 2.0 (the "License");
1838
+ * you may not use this file except in compliance with the License.
1839
+ * You may obtain a copy of the License at
1840
+ *
1841
+ * http://www.apache.org/licenses/LICENSE-2.0
1842
+ *
1843
+ * Unless required by applicable law or agreed to in writing, software
1844
+ * distributed under the License is distributed on an "AS IS" BASIS,
1845
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1846
+ * See the License for the specific language governing permissions and
1847
+ * limitations under the License.
1848
+ */
1849
+ const errorsCausingFallback = [
1850
+ // most network errors
1851
+ AIErrorCode.FETCH_ERROR,
1852
+ // fallback code for all other errors in makeRequest
1853
+ AIErrorCode.ERROR,
1854
+ // error due to API not being enabled in project
1855
+ AIErrorCode.API_NOT_ENABLED
1856
+ ];
1857
+ /**
1858
+ * Dispatches a request to the appropriate backend (on-device or in-cloud)
1859
+ * based on the inference mode.
1860
+ *
1861
+ * @param request - The request to be sent.
1862
+ * @param chromeAdapter - The on-device model adapter.
1863
+ * @param onDeviceCall - The function to call for on-device inference.
1864
+ * @param inCloudCall - The function to call for in-cloud inference.
1865
+ * @returns The response from the backend.
1866
+ */
1867
+ async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
1868
+ if (!chromeAdapter) {
1869
+ return inCloudCall();
1870
+ }
1871
+ switch (chromeAdapter.mode) {
1872
+ case InferenceMode.ONLY_ON_DEVICE:
1873
+ if (await chromeAdapter.isAvailable(request)) {
1874
+ return onDeviceCall();
1875
+ }
1876
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
1877
+ case InferenceMode.ONLY_IN_CLOUD:
1878
+ return inCloudCall();
1879
+ case InferenceMode.PREFER_IN_CLOUD:
1880
+ try {
1881
+ return await inCloudCall();
1882
+ }
1883
+ catch (e) {
1884
+ if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
1885
+ return onDeviceCall();
1886
+ }
1887
+ throw e;
1888
+ }
1889
+ case InferenceMode.PREFER_ON_DEVICE:
1890
+ if (await chromeAdapter.isAvailable(request)) {
1891
+ return onDeviceCall();
1892
+ }
1893
+ return inCloudCall();
1894
+ default:
1895
+ throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
1896
+ }
1897
+ }
1898
+
1741
1899
  /**
1742
1900
  * @license
1743
1901
  * Copyright 2024 Google LLC
@@ -1762,13 +1920,7 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
1762
1920
  /* stream */ true, JSON.stringify(params), requestOptions);
1763
1921
  }
1764
1922
  async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
1765
- let response;
1766
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
1767
- response = await chromeAdapter.generateContentStream(params);
1768
- }
1769
- else {
1770
- response = await generateContentStreamOnCloud(apiSettings, model, params, requestOptions);
1771
- }
1923
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
1772
1924
  return processStream(response, apiSettings); // TODO: Map streaming responses
1773
1925
  }
1774
1926
  async function generateContentOnCloud(apiSettings, model, params, requestOptions) {
@@ -1779,13 +1931,7 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
1779
1931
  /* stream */ false, JSON.stringify(params), requestOptions);
1780
1932
  }
1781
1933
  async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
1782
- let response;
1783
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
1784
- response = await chromeAdapter.generateContent(params);
1785
- }
1786
- else {
1787
- response = await generateContentOnCloud(apiSettings, model, params, requestOptions);
1788
- }
1934
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
1789
1935
  const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
1790
1936
  const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
1791
1937
  return {
@@ -1995,7 +2141,9 @@ function validateChatHistory(history) {
1995
2141
  functionCall: 0,
1996
2142
  functionResponse: 0,
1997
2143
  thought: 0,
1998
- thoughtSignature: 0
2144
+ thoughtSignature: 0,
2145
+ executableCode: 0,
2146
+ codeExecutionResult: 0
1999
2147
  };
2000
2148
  for (const part of parts) {
2001
2149
  for (const key of VALID_PART_FIELDS) {
@@ -2196,8 +2344,8 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
2196
2344
  return response.json();
2197
2345
  }
2198
2346
  async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
2199
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
2200
- return (await chromeAdapter.countTokens(params)).json();
2347
+ if (chromeAdapter?.mode === InferenceMode.ONLY_ON_DEVICE) {
2348
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'countTokens() is not supported for on-device models.');
2201
2349
  }
2202
2350
  return countTokensOnCloud(apiSettings, model, params, requestOptions);
2203
2351
  }
@@ -2379,6 +2527,25 @@ class LiveSession {
2379
2527
  this.webSocketHandler.send(JSON.stringify(message));
2380
2528
  });
2381
2529
  }
2530
+ /**
2531
+ * Sends function responses to the server.
2532
+ *
2533
+ * @param functionResponses - The function responses to send.
2534
+ * @throws If this session has been closed.
2535
+ *
2536
+ * @beta
2537
+ */
2538
+ async sendFunctionResponses(functionResponses) {
2539
+ if (this.isClosed) {
2540
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2541
+ }
2542
+ const message = {
2543
+ toolResponse: {
2544
+ functionResponses
2545
+ }
2546
+ };
2547
+ this.webSocketHandler.send(JSON.stringify(message));
2548
+ }
2382
2549
  /**
2383
2550
  * Sends a stream of {@link GenerativeContentBlob}.
2384
2551
  *
@@ -3360,9 +3527,9 @@ class AudioConversationRunner {
3360
3527
  }
3361
3528
  else {
3362
3529
  try {
3363
- const resultPart = await this.options.functionCallingHandler(message.functionCalls);
3530
+ const functionResponse = await this.options.functionCallingHandler(message.functionCalls);
3364
3531
  if (!this.isStopped) {
3365
- void this.liveSession.send([resultPart]);
3532
+ void this.liveSession.sendFunctionResponses([functionResponse]);
3366
3533
  }
3367
3534
  }
3368
3535
  catch (e) {
@@ -3659,17 +3826,20 @@ exports.ImagenPersonFilterLevel = ImagenPersonFilterLevel;
3659
3826
  exports.ImagenSafetyFilterLevel = ImagenSafetyFilterLevel;
3660
3827
  exports.InferenceMode = InferenceMode;
3661
3828
  exports.IntegerSchema = IntegerSchema;
3829
+ exports.Language = Language;
3662
3830
  exports.LiveGenerativeModel = LiveGenerativeModel;
3663
3831
  exports.LiveResponseType = LiveResponseType;
3664
3832
  exports.LiveSession = LiveSession;
3665
3833
  exports.Modality = Modality;
3666
3834
  exports.NumberSchema = NumberSchema;
3667
3835
  exports.ObjectSchema = ObjectSchema;
3836
+ exports.Outcome = Outcome;
3668
3837
  exports.POSSIBLE_ROLES = POSSIBLE_ROLES;
3669
3838
  exports.ResponseModality = ResponseModality;
3670
3839
  exports.Schema = Schema;
3671
3840
  exports.SchemaType = SchemaType;
3672
3841
  exports.StringSchema = StringSchema;
3842
+ exports.URLRetrievalStatus = URLRetrievalStatus;
3673
3843
  exports.VertexAIBackend = VertexAIBackend;
3674
3844
  exports.getAI = getAI;
3675
3845
  exports.getGenerativeModel = getGenerativeModel;