@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.
@@ -2148,6 +2148,15 @@ export declare class LiveSession {
2148
2148
  * @beta
2149
2149
  */
2150
2150
  sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2151
+ /**
2152
+ * Sends function responses to the server.
2153
+ *
2154
+ * @param functionResponses - The function responses to send.
2155
+ * @throws If this session has been closed.
2156
+ *
2157
+ * @beta
2158
+ */
2159
+ sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2151
2160
  /**
2152
2161
  * Sends a stream of {@link GenerativeContentBlob}.
2153
2162
  *
@@ -2777,7 +2786,7 @@ export declare interface StartAudioConversationOptions {
2777
2786
  * The handler should perform the function call and return the result as a `Part`,
2778
2787
  * which will then be sent back to the model.
2779
2788
  */
2780
- functionCallingHandler?: (functionCalls: LiveServerToolCall['functionCalls']) => Promise<Part>;
2789
+ functionCallingHandler?: (functionCalls: FunctionCall[]) => Promise<FunctionResponse>;
2781
2790
  }
2782
2791
 
2783
2792
  /**
package/dist/ai.d.ts CHANGED
@@ -2276,6 +2276,15 @@ export declare class LiveSession {
2276
2276
  * @beta
2277
2277
  */
2278
2278
  sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2279
+ /**
2280
+ * Sends function responses to the server.
2281
+ *
2282
+ * @param functionResponses - The function responses to send.
2283
+ * @throws If this session has been closed.
2284
+ *
2285
+ * @beta
2286
+ */
2287
+ sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2279
2288
  /**
2280
2289
  * Sends a stream of {@link GenerativeContentBlob}.
2281
2290
  *
@@ -2913,7 +2922,7 @@ export declare interface StartAudioConversationOptions {
2913
2922
  * The handler should perform the function call and return the result as a `Part`,
2914
2923
  * which will then be sent back to the model.
2915
2924
  */
2916
- functionCallingHandler?: (functionCalls: LiveServerToolCall['functionCalls']) => Promise<Part>;
2925
+ functionCallingHandler?: (functionCalls: FunctionCall[]) => Promise<FunctionResponse>;
2917
2926
  }
2918
2927
 
2919
2928
  /**
@@ -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.3.0-canary.7a7634f79";
7
+ var version = "2.3.0-canary.ccbf7ba36";
8
8
 
9
9
  /**
10
10
  * @license
@@ -1915,7 +1915,7 @@ function mapGenerateContentCandidates(candidates) {
1915
1915
  // videoMetadata is not supported.
1916
1916
  // Throw early since developers may send a long video as input and only expect to pay
1917
1917
  // for inference on a small portion of the video.
1918
- if (candidate.content?.parts.some(part => part?.videoMetadata)) {
1918
+ if (candidate.content?.parts?.some(part => part?.videoMetadata)) {
1919
1919
  throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
1920
1920
  }
1921
1921
  const mappedCandidate = {
@@ -2017,6 +2017,14 @@ async function* generateResponseSequence(stream, apiSettings) {
2017
2017
  else {
2018
2018
  enhancedResponse = createEnhancedContentResponse(value);
2019
2019
  }
2020
+ const firstCandidate = enhancedResponse.candidates?.[0];
2021
+ // Don't yield a response with no useful data for the developer.
2022
+ if (!firstCandidate?.content?.parts &&
2023
+ !firstCandidate?.finishReason &&
2024
+ !firstCandidate?.citationMetadata &&
2025
+ !firstCandidate?.urlContextMetadata) {
2026
+ continue;
2027
+ }
2020
2028
  yield enhancedResponse;
2021
2029
  }
2022
2030
  }
@@ -2111,32 +2119,28 @@ function aggregateResponses(responses) {
2111
2119
  * Candidates should always have content and parts, but this handles
2112
2120
  * possible malformed responses.
2113
2121
  */
2114
- if (candidate.content && candidate.content.parts) {
2122
+ if (candidate.content) {
2123
+ // Skip a candidate without parts.
2124
+ if (!candidate.content.parts) {
2125
+ continue;
2126
+ }
2115
2127
  if (!aggregatedResponse.candidates[i].content) {
2116
2128
  aggregatedResponse.candidates[i].content = {
2117
2129
  role: candidate.content.role || 'user',
2118
2130
  parts: []
2119
2131
  };
2120
2132
  }
2121
- const newPart = {};
2122
2133
  for (const part of candidate.content.parts) {
2123
- if (part.text !== undefined) {
2124
- // The backend can send empty text parts. If these are sent back
2125
- // (e.g. in chat history), the backend will respond with an error.
2126
- // To prevent this, ignore empty text parts.
2127
- if (part.text === '') {
2128
- continue;
2129
- }
2130
- newPart.text = part.text;
2134
+ const newPart = { ...part };
2135
+ // The backend can send empty text parts. If these are sent back
2136
+ // (e.g. in chat history), the backend will respond with an error.
2137
+ // To prevent this, ignore empty text parts.
2138
+ if (part.text === '') {
2139
+ continue;
2131
2140
  }
2132
- if (part.functionCall) {
2133
- newPart.functionCall = part.functionCall;
2141
+ if (Object.keys(newPart).length > 0) {
2142
+ aggregatedResponse.candidates[i].content.parts.push(newPart);
2134
2143
  }
2135
- if (Object.keys(newPart).length === 0) {
2136
- throw new AIError(AIErrorCode.INVALID_CONTENT, 'Part should have at least one property, but there are none. This is likely caused ' +
2137
- 'by a malformed response from the backend.');
2138
- }
2139
- aggregatedResponse.candidates[i].content.parts.push(newPart);
2140
2144
  }
2141
2145
  }
2142
2146
  }
@@ -2842,6 +2846,25 @@ class LiveSession {
2842
2846
  this.webSocketHandler.send(JSON.stringify(message));
2843
2847
  });
2844
2848
  }
2849
+ /**
2850
+ * Sends function responses to the server.
2851
+ *
2852
+ * @param functionResponses - The function responses to send.
2853
+ * @throws If this session has been closed.
2854
+ *
2855
+ * @beta
2856
+ */
2857
+ async sendFunctionResponses(functionResponses) {
2858
+ if (this.isClosed) {
2859
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2860
+ }
2861
+ const message = {
2862
+ toolResponse: {
2863
+ functionResponses
2864
+ }
2865
+ };
2866
+ this.webSocketHandler.send(JSON.stringify(message));
2867
+ }
2845
2868
  /**
2846
2869
  * Sends a stream of {@link GenerativeContentBlob}.
2847
2870
  *
@@ -3823,9 +3846,9 @@ class AudioConversationRunner {
3823
3846
  }
3824
3847
  else {
3825
3848
  try {
3826
- const resultPart = await this.options.functionCallingHandler(message.functionCalls);
3849
+ const functionResponse = await this.options.functionCallingHandler(message.functionCalls);
3827
3850
  if (!this.isStopped) {
3828
- void this.liveSession.send([resultPart]);
3851
+ void this.liveSession.sendFunctionResponses([functionResponse]);
3829
3852
  }
3830
3853
  }
3831
3854
  catch (e) {