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

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.ccbf7ba36";
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
  }
@@ -2523,6 +2527,25 @@ class LiveSession {
2523
2527
  this.webSocketHandler.send(JSON.stringify(message));
2524
2528
  });
2525
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
+ }
2526
2549
  /**
2527
2550
  * Sends a stream of {@link GenerativeContentBlob}.
2528
2551
  *
@@ -3504,9 +3527,9 @@ class AudioConversationRunner {
3504
3527
  }
3505
3528
  else {
3506
3529
  try {
3507
- const resultPart = await this.options.functionCallingHandler(message.functionCalls);
3530
+ const functionResponse = await this.options.functionCallingHandler(message.functionCalls);
3508
3531
  if (!this.isStopped) {
3509
- void this.liveSession.send([resultPart]);
3532
+ void this.liveSession.sendFunctionResponses([functionResponse]);
3510
3533
  }
3511
3534
  }
3512
3535
  catch (e) {