@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
@@ -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.2.1";
7
+ var version = "2.3.0-caching-fdc.9f17eac6e";
8
8
 
9
9
  /**
10
10
  * @license
@@ -297,14 +297,51 @@ const ResponseModality = {
297
297
  AUDIO: 'AUDIO'
298
298
  };
299
299
  /**
300
- * <b>(EXPERIMENTAL)</b>
301
300
  * Determines whether inference happens on-device or in-cloud.
302
- * @public
301
+ *
302
+ * @remarks
303
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
304
+ * on-device model. If on-device inference is not available, the SDK
305
+ * will fall back to using a cloud-hosted model.
306
+ * <br/>
307
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
308
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
309
+ * If on-device inference is not available, inference methods will throw.
310
+ * <br/>
311
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
312
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
313
+ * <br/>
314
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
315
+ * cloud-hosted model. If not available, the SDK will fall back to an
316
+ * on-device model.
317
+ *
318
+ * @beta
303
319
  */
304
320
  const InferenceMode = {
305
321
  'PREFER_ON_DEVICE': 'prefer_on_device',
306
322
  'ONLY_ON_DEVICE': 'only_on_device',
307
- 'ONLY_IN_CLOUD': 'only_in_cloud'
323
+ 'ONLY_IN_CLOUD': 'only_in_cloud',
324
+ 'PREFER_IN_CLOUD': 'prefer_in_cloud'
325
+ };
326
+ /**
327
+ * Represents the result of the code execution.
328
+ *
329
+ * @beta
330
+ */
331
+ const Outcome = {
332
+ UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
333
+ OK: 'OUTCOME_OK',
334
+ FAILED: 'OUTCOME_FAILED',
335
+ DEADLINE_EXCEEDED: 'OUTCOME_DEADLINE_EXCEEDED'
336
+ };
337
+ /**
338
+ * The programming language of the code.
339
+ *
340
+ * @beta
341
+ */
342
+ const Language = {
343
+ UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
344
+ PYTHON: 'PYTHON'
308
345
  };
309
346
 
310
347
  /**
@@ -323,6 +360,45 @@ const InferenceMode = {
323
360
  * See the License for the specific language governing permissions and
324
361
  * limitations under the License.
325
362
  */
363
+ /**
364
+ * The status of a URL retrieval.
365
+ *
366
+ * @remarks
367
+ * <b>URL_RETRIEVAL_STATUS_UNSPECIFIED:</b> Unspecified retrieval status.
368
+ * <br/>
369
+ * <b>URL_RETRIEVAL_STATUS_SUCCESS:</b> The URL retrieval was successful.
370
+ * <br/>
371
+ * <b>URL_RETRIEVAL_STATUS_ERROR:</b> The URL retrieval failed.
372
+ * <br/>
373
+ * <b>URL_RETRIEVAL_STATUS_PAYWALL:</b> The URL retrieval failed because the content is behind a paywall.
374
+ * <br/>
375
+ * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
376
+ * <br/>
377
+ *
378
+ * @beta
379
+ */
380
+ const URLRetrievalStatus = {
381
+ /**
382
+ * Unspecified retrieval status.
383
+ */
384
+ URL_RETRIEVAL_STATUS_UNSPECIFIED: 'URL_RETRIEVAL_STATUS_UNSPECIFIED',
385
+ /**
386
+ * The URL retrieval was successful.
387
+ */
388
+ URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS',
389
+ /**
390
+ * The URL retrieval failed.
391
+ */
392
+ URL_RETRIEVAL_STATUS_ERROR: 'URL_RETRIEVAL_STATUS_ERROR',
393
+ /**
394
+ * The URL retrieval failed because the content is behind a paywall.
395
+ */
396
+ URL_RETRIEVAL_STATUS_PAYWALL: 'URL_RETRIEVAL_STATUS_PAYWALL',
397
+ /**
398
+ * The URL retrieval failed because the content is unsafe.
399
+ */
400
+ URL_RETRIEVAL_STATUS_UNSAFE: 'URL_RETRIEVAL_STATUS_UNSAFE'
401
+ };
326
402
  /**
327
403
  * The types of responses that can be returned by {@link LiveSession.receive}.
328
404
  *
@@ -1516,7 +1592,7 @@ function mapGenerateContentCandidates(candidates) {
1516
1592
  // videoMetadata is not supported.
1517
1593
  // Throw early since developers may send a long video as input and only expect to pay
1518
1594
  // for inference on a small portion of the video.
1519
- if (candidate.content?.parts.some(part => part?.videoMetadata)) {
1595
+ if (candidate.content?.parts?.some(part => part?.videoMetadata)) {
1520
1596
  throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
1521
1597
  }
1522
1598
  const mappedCandidate = {
@@ -1526,7 +1602,8 @@ function mapGenerateContentCandidates(candidates) {
1526
1602
  finishMessage: candidate.finishMessage,
1527
1603
  safetyRatings: mappedSafetyRatings,
1528
1604
  citationMetadata,
1529
- groundingMetadata: candidate.groundingMetadata
1605
+ groundingMetadata: candidate.groundingMetadata,
1606
+ urlContextMetadata: candidate.urlContextMetadata
1530
1607
  };
1531
1608
  mappedCandidates.push(mappedCandidate);
1532
1609
  });
@@ -1617,6 +1694,14 @@ async function* generateResponseSequence(stream, apiSettings) {
1617
1694
  else {
1618
1695
  enhancedResponse = createEnhancedContentResponse(value);
1619
1696
  }
1697
+ const firstCandidate = enhancedResponse.candidates?.[0];
1698
+ // Don't yield a response with no useful data for the developer.
1699
+ if (!firstCandidate?.content?.parts &&
1700
+ !firstCandidate?.finishReason &&
1701
+ !firstCandidate?.citationMetadata &&
1702
+ !firstCandidate?.urlContextMetadata) {
1703
+ continue;
1704
+ }
1620
1705
  yield enhancedResponse;
1621
1706
  }
1622
1707
  }
@@ -1696,36 +1781,43 @@ function aggregateResponses(responses) {
1696
1781
  candidate.safetyRatings;
1697
1782
  aggregatedResponse.candidates[i].groundingMetadata =
1698
1783
  candidate.groundingMetadata;
1784
+ // The urlContextMetadata object is defined in the first chunk of the response stream.
1785
+ // In all subsequent chunks, the urlContextMetadata object will be undefined. We need to
1786
+ // make sure that we don't overwrite the first value urlContextMetadata object with undefined.
1787
+ // FIXME: What happens if we receive a second, valid urlContextMetadata object?
1788
+ const urlContextMetadata = candidate.urlContextMetadata;
1789
+ if (typeof urlContextMetadata === 'object' &&
1790
+ urlContextMetadata !== null &&
1791
+ Object.keys(urlContextMetadata).length > 0) {
1792
+ aggregatedResponse.candidates[i].urlContextMetadata =
1793
+ urlContextMetadata;
1794
+ }
1699
1795
  /**
1700
1796
  * Candidates should always have content and parts, but this handles
1701
1797
  * possible malformed responses.
1702
1798
  */
1703
- if (candidate.content && candidate.content.parts) {
1799
+ if (candidate.content) {
1800
+ // Skip a candidate without parts.
1801
+ if (!candidate.content.parts) {
1802
+ continue;
1803
+ }
1704
1804
  if (!aggregatedResponse.candidates[i].content) {
1705
1805
  aggregatedResponse.candidates[i].content = {
1706
1806
  role: candidate.content.role || 'user',
1707
1807
  parts: []
1708
1808
  };
1709
1809
  }
1710
- const newPart = {};
1711
1810
  for (const part of candidate.content.parts) {
1712
- if (part.text !== undefined) {
1713
- // The backend can send empty text parts. If these are sent back
1714
- // (e.g. in chat history), the backend will respond with an error.
1715
- // To prevent this, ignore empty text parts.
1716
- if (part.text === '') {
1717
- continue;
1718
- }
1719
- newPart.text = part.text;
1811
+ const newPart = { ...part };
1812
+ // The backend can send empty text parts. If these are sent back
1813
+ // (e.g. in chat history), the backend will respond with an error.
1814
+ // To prevent this, ignore empty text parts.
1815
+ if (part.text === '') {
1816
+ continue;
1720
1817
  }
1721
- if (part.functionCall) {
1722
- newPart.functionCall = part.functionCall;
1818
+ if (Object.keys(newPart).length > 0) {
1819
+ aggregatedResponse.candidates[i].content.parts.push(newPart);
1723
1820
  }
1724
- if (Object.keys(newPart).length === 0) {
1725
- throw new AIError(AIErrorCode.INVALID_CONTENT, 'Part should have at least one property, but there are none. This is likely caused ' +
1726
- 'by a malformed response from the backend.');
1727
- }
1728
- aggregatedResponse.candidates[i].content.parts.push(newPart);
1729
1821
  }
1730
1822
  }
1731
1823
  }
@@ -1734,6 +1826,72 @@ function aggregateResponses(responses) {
1734
1826
  return aggregatedResponse;
1735
1827
  }
1736
1828
 
1829
+ /**
1830
+ * @license
1831
+ * Copyright 2025 Google LLC
1832
+ *
1833
+ * Licensed under the Apache License, Version 2.0 (the "License");
1834
+ * you may not use this file except in compliance with the License.
1835
+ * You may obtain a copy of the License at
1836
+ *
1837
+ * http://www.apache.org/licenses/LICENSE-2.0
1838
+ *
1839
+ * Unless required by applicable law or agreed to in writing, software
1840
+ * distributed under the License is distributed on an "AS IS" BASIS,
1841
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1842
+ * See the License for the specific language governing permissions and
1843
+ * limitations under the License.
1844
+ */
1845
+ const errorsCausingFallback = [
1846
+ // most network errors
1847
+ AIErrorCode.FETCH_ERROR,
1848
+ // fallback code for all other errors in makeRequest
1849
+ AIErrorCode.ERROR,
1850
+ // error due to API not being enabled in project
1851
+ AIErrorCode.API_NOT_ENABLED
1852
+ ];
1853
+ /**
1854
+ * Dispatches a request to the appropriate backend (on-device or in-cloud)
1855
+ * based on the inference mode.
1856
+ *
1857
+ * @param request - The request to be sent.
1858
+ * @param chromeAdapter - The on-device model adapter.
1859
+ * @param onDeviceCall - The function to call for on-device inference.
1860
+ * @param inCloudCall - The function to call for in-cloud inference.
1861
+ * @returns The response from the backend.
1862
+ */
1863
+ async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
1864
+ if (!chromeAdapter) {
1865
+ return inCloudCall();
1866
+ }
1867
+ switch (chromeAdapter.mode) {
1868
+ case InferenceMode.ONLY_ON_DEVICE:
1869
+ if (await chromeAdapter.isAvailable(request)) {
1870
+ return onDeviceCall();
1871
+ }
1872
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
1873
+ case InferenceMode.ONLY_IN_CLOUD:
1874
+ return inCloudCall();
1875
+ case InferenceMode.PREFER_IN_CLOUD:
1876
+ try {
1877
+ return await inCloudCall();
1878
+ }
1879
+ catch (e) {
1880
+ if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
1881
+ return onDeviceCall();
1882
+ }
1883
+ throw e;
1884
+ }
1885
+ case InferenceMode.PREFER_ON_DEVICE:
1886
+ if (await chromeAdapter.isAvailable(request)) {
1887
+ return onDeviceCall();
1888
+ }
1889
+ return inCloudCall();
1890
+ default:
1891
+ throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
1892
+ }
1893
+ }
1894
+
1737
1895
  /**
1738
1896
  * @license
1739
1897
  * Copyright 2024 Google LLC
@@ -1758,13 +1916,7 @@ async function generateContentStreamOnCloud(apiSettings, model, params, requestO
1758
1916
  /* stream */ true, JSON.stringify(params), requestOptions);
1759
1917
  }
1760
1918
  async function generateContentStream(apiSettings, model, params, chromeAdapter, requestOptions) {
1761
- let response;
1762
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
1763
- response = await chromeAdapter.generateContentStream(params);
1764
- }
1765
- else {
1766
- response = await generateContentStreamOnCloud(apiSettings, model, params, requestOptions);
1767
- }
1919
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, requestOptions));
1768
1920
  return processStream(response, apiSettings); // TODO: Map streaming responses
1769
1921
  }
1770
1922
  async function generateContentOnCloud(apiSettings, model, params, requestOptions) {
@@ -1775,13 +1927,7 @@ async function generateContentOnCloud(apiSettings, model, params, requestOptions
1775
1927
  /* stream */ false, JSON.stringify(params), requestOptions);
1776
1928
  }
1777
1929
  async function generateContent(apiSettings, model, params, chromeAdapter, requestOptions) {
1778
- let response;
1779
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
1780
- response = await chromeAdapter.generateContent(params);
1781
- }
1782
- else {
1783
- response = await generateContentOnCloud(apiSettings, model, params, requestOptions);
1784
- }
1930
+ const response = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, requestOptions));
1785
1931
  const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
1786
1932
  const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
1787
1933
  return {
@@ -1991,7 +2137,9 @@ function validateChatHistory(history) {
1991
2137
  functionCall: 0,
1992
2138
  functionResponse: 0,
1993
2139
  thought: 0,
1994
- thoughtSignature: 0
2140
+ thoughtSignature: 0,
2141
+ executableCode: 0,
2142
+ codeExecutionResult: 0
1995
2143
  };
1996
2144
  for (const part of parts) {
1997
2145
  for (const key of VALID_PART_FIELDS) {
@@ -2192,8 +2340,8 @@ async function countTokensOnCloud(apiSettings, model, params, requestOptions) {
2192
2340
  return response.json();
2193
2341
  }
2194
2342
  async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
2195
- if (chromeAdapter && (await chromeAdapter.isAvailable(params))) {
2196
- return (await chromeAdapter.countTokens(params)).json();
2343
+ if (chromeAdapter?.mode === InferenceMode.ONLY_ON_DEVICE) {
2344
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'countTokens() is not supported for on-device models.');
2197
2345
  }
2198
2346
  return countTokensOnCloud(apiSettings, model, params, requestOptions);
2199
2347
  }
@@ -2375,6 +2523,25 @@ class LiveSession {
2375
2523
  this.webSocketHandler.send(JSON.stringify(message));
2376
2524
  });
2377
2525
  }
2526
+ /**
2527
+ * Sends function responses to the server.
2528
+ *
2529
+ * @param functionResponses - The function responses to send.
2530
+ * @throws If this session has been closed.
2531
+ *
2532
+ * @beta
2533
+ */
2534
+ async sendFunctionResponses(functionResponses) {
2535
+ if (this.isClosed) {
2536
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2537
+ }
2538
+ const message = {
2539
+ toolResponse: {
2540
+ functionResponses
2541
+ }
2542
+ };
2543
+ this.webSocketHandler.send(JSON.stringify(message));
2544
+ }
2378
2545
  /**
2379
2546
  * Sends a stream of {@link GenerativeContentBlob}.
2380
2547
  *
@@ -3356,9 +3523,9 @@ class AudioConversationRunner {
3356
3523
  }
3357
3524
  else {
3358
3525
  try {
3359
- const resultPart = await this.options.functionCallingHandler(message.functionCalls);
3526
+ const functionResponse = await this.options.functionCallingHandler(message.functionCalls);
3360
3527
  if (!this.isStopped) {
3361
- void this.liveSession.send([resultPart]);
3528
+ void this.liveSession.sendFunctionResponses([functionResponse]);
3362
3529
  }
3363
3530
  }
3364
3531
  catch (e) {
@@ -3629,5 +3796,5 @@ function registerAI() {
3629
3796
  }
3630
3797
  registerAI();
3631
3798
 
3632
- 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, IntegerSchema, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
3799
+ 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, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, startAudioConversation };
3633
3800
  //# sourceMappingURL=index.node.mjs.map