@firebase/ai 2.3.0-canary.7a7634f79 → 2.3.0-canary.ea8512812

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.
@@ -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.3.0-canary.7a7634f79";
11
+ var version = "2.3.0-canary.ea8512812";
12
12
 
13
13
  /**
14
14
  * @license
@@ -1596,7 +1596,7 @@ function mapGenerateContentCandidates(candidates) {
1596
1596
  // videoMetadata is not supported.
1597
1597
  // Throw early since developers may send a long video as input and only expect to pay
1598
1598
  // for inference on a small portion of the video.
1599
- if (candidate.content?.parts.some(part => part?.videoMetadata)) {
1599
+ if (candidate.content?.parts?.some(part => part?.videoMetadata)) {
1600
1600
  throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
1601
1601
  }
1602
1602
  const mappedCandidate = {
@@ -1698,6 +1698,14 @@ async function* generateResponseSequence(stream, apiSettings) {
1698
1698
  else {
1699
1699
  enhancedResponse = createEnhancedContentResponse(value);
1700
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
+ }
1701
1709
  yield enhancedResponse;
1702
1710
  }
1703
1711
  }
@@ -1792,32 +1800,28 @@ function aggregateResponses(responses) {
1792
1800
  * Candidates should always have content and parts, but this handles
1793
1801
  * possible malformed responses.
1794
1802
  */
1795
- if (candidate.content && candidate.content.parts) {
1803
+ if (candidate.content) {
1804
+ // Skip a candidate without parts.
1805
+ if (!candidate.content.parts) {
1806
+ continue;
1807
+ }
1796
1808
  if (!aggregatedResponse.candidates[i].content) {
1797
1809
  aggregatedResponse.candidates[i].content = {
1798
1810
  role: candidate.content.role || 'user',
1799
1811
  parts: []
1800
1812
  };
1801
1813
  }
1802
- const newPart = {};
1803
1814
  for (const part of candidate.content.parts) {
1804
- if (part.text !== undefined) {
1805
- // The backend can send empty text parts. If these are sent back
1806
- // (e.g. in chat history), the backend will respond with an error.
1807
- // To prevent this, ignore empty text parts.
1808
- if (part.text === '') {
1809
- continue;
1810
- }
1811
- newPart.text = part.text;
1812
- }
1813
- if (part.functionCall) {
1814
- newPart.functionCall = part.functionCall;
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;
1815
1821
  }
1816
- if (Object.keys(newPart).length === 0) {
1817
- throw new AIError(AIErrorCode.INVALID_CONTENT, 'Part should have at least one property, but there are none. This is likely caused ' +
1818
- 'by a malformed response from the backend.');
1822
+ if (Object.keys(newPart).length > 0) {
1823
+ aggregatedResponse.candidates[i].content.parts.push(newPart);
1819
1824
  }
1820
- aggregatedResponse.candidates[i].content.parts.push(newPart);
1821
1825
  }
1822
1826
  }
1823
1827
  }