@firebase/ai 2.4.0-canary.44d9891f9 → 2.4.0-canary.8209266c6

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.
@@ -2163,32 +2163,65 @@ export declare class LiveSession {
2163
2163
  */
2164
2164
  send(request: string | Array<string | Part>, turnComplete?: boolean): Promise<void>;
2165
2165
  /**
2166
- * Sends realtime input to the server.
2166
+ * Sends text to the server in realtime.
2167
2167
  *
2168
- * @param mediaChunks - The media chunks to send.
2168
+ * @example
2169
+ * ```javascript
2170
+ * liveSession.sendTextRealtime("Hello, how are you?");
2171
+ * ```
2172
+ *
2173
+ * @param text - The text data to send.
2169
2174
  * @throws If this session has been closed.
2170
2175
  *
2171
2176
  * @beta
2172
2177
  */
2173
- sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2178
+ sendTextRealtime(text: string): Promise<void>;
2174
2179
  /**
2175
- * Sends function responses to the server.
2180
+ * Sends audio data to the server in realtime.
2176
2181
  *
2177
- * @param functionResponses - The function responses to send.
2182
+ * @remarks The server requires that the audio data is base64-encoded 16-bit PCM at 16kHz
2183
+ * little-endian.
2184
+ *
2185
+ * @example
2186
+ * ```javascript
2187
+ * // const pcmData = ... base64-encoded 16-bit PCM at 16kHz little-endian.
2188
+ * const blob = { mimeType: "audio/pcm", data: pcmData };
2189
+ * liveSession.sendAudioRealtime(blob);
2190
+ * ```
2191
+ *
2192
+ * @param blob - The base64-encoded PCM data to send to the server in realtime.
2178
2193
  * @throws If this session has been closed.
2179
2194
  *
2180
2195
  * @beta
2181
2196
  */
2182
- sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2197
+ sendAudioRealtime(blob: GenerativeContentBlob): Promise<void>;
2183
2198
  /**
2184
- * Sends a stream of {@link GenerativeContentBlob}.
2199
+ * Sends video data to the server in realtime.
2185
2200
  *
2186
- * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
2201
+ * @remarks The server requires that the video is sent as individual video frames at 1 FPS. It
2202
+ * is recommended to set `mimeType` to `image/jpeg`.
2203
+ *
2204
+ * @example
2205
+ * ```javascript
2206
+ * // const videoFrame = ... base64-encoded JPEG data
2207
+ * const blob = { mimeType: "image/jpeg", data: videoFrame };
2208
+ * liveSession.sendVideoRealtime(blob);
2209
+ * ```
2210
+ * @param blob - The base64-encoded video data to send to the server in realtime.
2187
2211
  * @throws If this session has been closed.
2188
2212
  *
2189
2213
  * @beta
2190
2214
  */
2191
- sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
2215
+ sendVideoRealtime(blob: GenerativeContentBlob): Promise<void>;
2216
+ /**
2217
+ * Sends function responses to the server.
2218
+ *
2219
+ * @param functionResponses - The function responses to send.
2220
+ * @throws If this session has been closed.
2221
+ *
2222
+ * @beta
2223
+ */
2224
+ sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2192
2225
  /**
2193
2226
  * Yields messages received from the server.
2194
2227
  * This can only be used by one consumer at a time.
@@ -2206,6 +2239,28 @@ export declare class LiveSession {
2206
2239
  * @beta
2207
2240
  */
2208
2241
  close(): Promise<void>;
2242
+ /**
2243
+ * Sends realtime input to the server.
2244
+ *
2245
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
2246
+ *
2247
+ * @param mediaChunks - The media chunks to send.
2248
+ * @throws If this session has been closed.
2249
+ *
2250
+ * @beta
2251
+ */
2252
+ sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2253
+ /**
2254
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
2255
+ *
2256
+ * Sends a stream of {@link GenerativeContentBlob}.
2257
+ *
2258
+ * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
2259
+ * @throws If this session has been closed.
2260
+ *
2261
+ * @beta
2262
+ */
2263
+ sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
2209
2264
  }
2210
2265
 
2211
2266
  /**
package/dist/ai.d.ts CHANGED
@@ -2291,32 +2291,65 @@ export declare class LiveSession {
2291
2291
  */
2292
2292
  send(request: string | Array<string | Part>, turnComplete?: boolean): Promise<void>;
2293
2293
  /**
2294
- * Sends realtime input to the server.
2294
+ * Sends text to the server in realtime.
2295
2295
  *
2296
- * @param mediaChunks - The media chunks to send.
2296
+ * @example
2297
+ * ```javascript
2298
+ * liveSession.sendTextRealtime("Hello, how are you?");
2299
+ * ```
2300
+ *
2301
+ * @param text - The text data to send.
2297
2302
  * @throws If this session has been closed.
2298
2303
  *
2299
2304
  * @beta
2300
2305
  */
2301
- sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2306
+ sendTextRealtime(text: string): Promise<void>;
2302
2307
  /**
2303
- * Sends function responses to the server.
2308
+ * Sends audio data to the server in realtime.
2304
2309
  *
2305
- * @param functionResponses - The function responses to send.
2310
+ * @remarks The server requires that the audio data is base64-encoded 16-bit PCM at 16kHz
2311
+ * little-endian.
2312
+ *
2313
+ * @example
2314
+ * ```javascript
2315
+ * // const pcmData = ... base64-encoded 16-bit PCM at 16kHz little-endian.
2316
+ * const blob = { mimeType: "audio/pcm", data: pcmData };
2317
+ * liveSession.sendAudioRealtime(blob);
2318
+ * ```
2319
+ *
2320
+ * @param blob - The base64-encoded PCM data to send to the server in realtime.
2306
2321
  * @throws If this session has been closed.
2307
2322
  *
2308
2323
  * @beta
2309
2324
  */
2310
- sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2325
+ sendAudioRealtime(blob: GenerativeContentBlob): Promise<void>;
2311
2326
  /**
2312
- * Sends a stream of {@link GenerativeContentBlob}.
2327
+ * Sends video data to the server in realtime.
2313
2328
  *
2314
- * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
2329
+ * @remarks The server requires that the video is sent as individual video frames at 1 FPS. It
2330
+ * is recommended to set `mimeType` to `image/jpeg`.
2331
+ *
2332
+ * @example
2333
+ * ```javascript
2334
+ * // const videoFrame = ... base64-encoded JPEG data
2335
+ * const blob = { mimeType: "image/jpeg", data: videoFrame };
2336
+ * liveSession.sendVideoRealtime(blob);
2337
+ * ```
2338
+ * @param blob - The base64-encoded video data to send to the server in realtime.
2315
2339
  * @throws If this session has been closed.
2316
2340
  *
2317
2341
  * @beta
2318
2342
  */
2319
- sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
2343
+ sendVideoRealtime(blob: GenerativeContentBlob): Promise<void>;
2344
+ /**
2345
+ * Sends function responses to the server.
2346
+ *
2347
+ * @param functionResponses - The function responses to send.
2348
+ * @throws If this session has been closed.
2349
+ *
2350
+ * @beta
2351
+ */
2352
+ sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
2320
2353
  /**
2321
2354
  * Yields messages received from the server.
2322
2355
  * This can only be used by one consumer at a time.
@@ -2334,6 +2367,28 @@ export declare class LiveSession {
2334
2367
  * @beta
2335
2368
  */
2336
2369
  close(): Promise<void>;
2370
+ /**
2371
+ * Sends realtime input to the server.
2372
+ *
2373
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
2374
+ *
2375
+ * @param mediaChunks - The media chunks to send.
2376
+ * @throws If this session has been closed.
2377
+ *
2378
+ * @beta
2379
+ */
2380
+ sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
2381
+ /**
2382
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
2383
+ *
2384
+ * Sends a stream of {@link GenerativeContentBlob}.
2385
+ *
2386
+ * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
2387
+ * @throws If this session has been closed.
2388
+ *
2389
+ * @beta
2390
+ */
2391
+ sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
2337
2392
  }
2338
2393
 
2339
2394
  /**
@@ -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.4.0-canary.44d9891f9";
7
+ var version = "2.4.0-canary.8209266c6";
8
8
 
9
9
  /**
10
10
  * @license
@@ -2870,75 +2870,104 @@ class LiveSession {
2870
2870
  this.webSocketHandler.send(JSON.stringify(message));
2871
2871
  }
2872
2872
  /**
2873
- * Sends realtime input to the server.
2873
+ * Sends text to the server in realtime.
2874
2874
  *
2875
- * @param mediaChunks - The media chunks to send.
2875
+ * @example
2876
+ * ```javascript
2877
+ * liveSession.sendTextRealtime("Hello, how are you?");
2878
+ * ```
2879
+ *
2880
+ * @param text - The text data to send.
2876
2881
  * @throws If this session has been closed.
2877
2882
  *
2878
2883
  * @beta
2879
2884
  */
2880
- async sendMediaChunks(mediaChunks) {
2885
+ async sendTextRealtime(text) {
2881
2886
  if (this.isClosed) {
2882
2887
  throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2883
2888
  }
2884
- // The backend does not support sending more than one mediaChunk in one message.
2885
- // Work around this limitation by sending mediaChunks in separate messages.
2886
- mediaChunks.forEach(mediaChunk => {
2887
- const message = {
2888
- realtimeInput: { mediaChunks: [mediaChunk] }
2889
- };
2890
- this.webSocketHandler.send(JSON.stringify(message));
2891
- });
2889
+ const message = {
2890
+ realtimeInput: {
2891
+ text
2892
+ }
2893
+ };
2894
+ this.webSocketHandler.send(JSON.stringify(message));
2892
2895
  }
2893
2896
  /**
2894
- * Sends function responses to the server.
2897
+ * Sends audio data to the server in realtime.
2895
2898
  *
2896
- * @param functionResponses - The function responses to send.
2899
+ * @remarks The server requires that the audio data is base64-encoded 16-bit PCM at 16kHz
2900
+ * little-endian.
2901
+ *
2902
+ * @example
2903
+ * ```javascript
2904
+ * // const pcmData = ... base64-encoded 16-bit PCM at 16kHz little-endian.
2905
+ * const blob = { mimeType: "audio/pcm", data: pcmData };
2906
+ * liveSession.sendAudioRealtime(blob);
2907
+ * ```
2908
+ *
2909
+ * @param blob - The base64-encoded PCM data to send to the server in realtime.
2897
2910
  * @throws If this session has been closed.
2898
2911
  *
2899
2912
  * @beta
2900
2913
  */
2901
- async sendFunctionResponses(functionResponses) {
2914
+ async sendAudioRealtime(blob) {
2902
2915
  if (this.isClosed) {
2903
2916
  throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2904
2917
  }
2905
2918
  const message = {
2906
- toolResponse: {
2907
- functionResponses
2919
+ realtimeInput: {
2920
+ audio: blob
2908
2921
  }
2909
2922
  };
2910
2923
  this.webSocketHandler.send(JSON.stringify(message));
2911
2924
  }
2912
2925
  /**
2913
- * Sends a stream of {@link GenerativeContentBlob}.
2926
+ * Sends video data to the server in realtime.
2914
2927
  *
2915
- * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
2928
+ * @remarks The server requires that the video is sent as individual video frames at 1 FPS. It
2929
+ * is recommended to set `mimeType` to `image/jpeg`.
2930
+ *
2931
+ * @example
2932
+ * ```javascript
2933
+ * // const videoFrame = ... base64-encoded JPEG data
2934
+ * const blob = { mimeType: "image/jpeg", data: videoFrame };
2935
+ * liveSession.sendVideoRealtime(blob);
2936
+ * ```
2937
+ * @param blob - The base64-encoded video data to send to the server in realtime.
2916
2938
  * @throws If this session has been closed.
2917
2939
  *
2918
2940
  * @beta
2919
2941
  */
2920
- async sendMediaStream(mediaChunkStream) {
2942
+ async sendVideoRealtime(blob) {
2921
2943
  if (this.isClosed) {
2922
2944
  throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2923
2945
  }
2924
- const reader = mediaChunkStream.getReader();
2925
- while (true) {
2926
- try {
2927
- const { done, value } = await reader.read();
2928
- if (done) {
2929
- break;
2930
- }
2931
- else if (!value) {
2932
- throw new Error('Missing chunk in reader, but reader is not done.');
2933
- }
2934
- await this.sendMediaChunks([value]);
2935
- }
2936
- catch (e) {
2937
- // Re-throw any errors that occur during stream consumption or sending.
2938
- const message = e instanceof Error ? e.message : 'Error processing media stream.';
2939
- throw new AIError(AIErrorCode.REQUEST_ERROR, message);
2946
+ const message = {
2947
+ realtimeInput: {
2948
+ video: blob
2940
2949
  }
2950
+ };
2951
+ this.webSocketHandler.send(JSON.stringify(message));
2952
+ }
2953
+ /**
2954
+ * Sends function responses to the server.
2955
+ *
2956
+ * @param functionResponses - The function responses to send.
2957
+ * @throws If this session has been closed.
2958
+ *
2959
+ * @beta
2960
+ */
2961
+ async sendFunctionResponses(functionResponses) {
2962
+ if (this.isClosed) {
2963
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
2941
2964
  }
2965
+ const message = {
2966
+ toolResponse: {
2967
+ functionResponses
2968
+ }
2969
+ };
2970
+ this.webSocketHandler.send(JSON.stringify(message));
2942
2971
  }
2943
2972
  /**
2944
2973
  * Yields messages received from the server.
@@ -2996,6 +3025,62 @@ class LiveSession {
2996
3025
  await this.webSocketHandler.close(1000, 'Client closed session.');
2997
3026
  }
2998
3027
  }
3028
+ /**
3029
+ * Sends realtime input to the server.
3030
+ *
3031
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
3032
+ *
3033
+ * @param mediaChunks - The media chunks to send.
3034
+ * @throws If this session has been closed.
3035
+ *
3036
+ * @beta
3037
+ */
3038
+ async sendMediaChunks(mediaChunks) {
3039
+ if (this.isClosed) {
3040
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3041
+ }
3042
+ // The backend does not support sending more than one mediaChunk in one message.
3043
+ // Work around this limitation by sending mediaChunks in separate messages.
3044
+ mediaChunks.forEach(mediaChunk => {
3045
+ const message = {
3046
+ realtimeInput: { mediaChunks: [mediaChunk] }
3047
+ };
3048
+ this.webSocketHandler.send(JSON.stringify(message));
3049
+ });
3050
+ }
3051
+ /**
3052
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
3053
+ *
3054
+ * Sends a stream of {@link GenerativeContentBlob}.
3055
+ *
3056
+ * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
3057
+ * @throws If this session has been closed.
3058
+ *
3059
+ * @beta
3060
+ */
3061
+ async sendMediaStream(mediaChunkStream) {
3062
+ if (this.isClosed) {
3063
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3064
+ }
3065
+ const reader = mediaChunkStream.getReader();
3066
+ while (true) {
3067
+ try {
3068
+ const { done, value } = await reader.read();
3069
+ if (done) {
3070
+ break;
3071
+ }
3072
+ else if (!value) {
3073
+ throw new Error('Missing chunk in reader, but reader is not done.');
3074
+ }
3075
+ await this.sendMediaChunks([value]);
3076
+ }
3077
+ catch (e) {
3078
+ // Re-throw any errors that occur during stream consumption or sending.
3079
+ const message = e instanceof Error ? e.message : 'Error processing media stream.';
3080
+ throw new AIError(AIErrorCode.REQUEST_ERROR, message);
3081
+ }
3082
+ }
3083
+ }
2999
3084
  }
3000
3085
 
3001
3086
  /**
@@ -3768,7 +3853,7 @@ class AudioConversationRunner {
3768
3853
  mimeType: 'audio/pcm',
3769
3854
  data: base64
3770
3855
  };
3771
- void this.liveSession.sendMediaChunks([chunk]);
3856
+ void this.liveSession.sendAudioRealtime(chunk);
3772
3857
  };
3773
3858
  }
3774
3859
  /**