@firebase/ai 2.4.0 → 2.5.0-canary.0800a8bed

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 +134 -9
  2. package/dist/ai.d.ts +137 -9
  3. package/dist/esm/index.esm.js +202 -68
  4. package/dist/esm/index.esm.js.map +1 -1
  5. package/dist/esm/src/factory-node.d.ts +19 -0
  6. package/dist/esm/src/methods/chrome-adapter.d.ts +1 -1
  7. package/dist/esm/src/methods/live-session.d.ts +64 -9
  8. package/dist/esm/src/requests/hybrid-helpers.d.ts +7 -2
  9. package/dist/esm/src/requests/response-helpers.d.ts +2 -2
  10. package/dist/esm/src/requests/stream-reader.d.ts +2 -1
  11. package/dist/esm/src/service.d.ts +3 -4
  12. package/dist/esm/src/types/chrome-adapter.d.ts +5 -0
  13. package/dist/esm/src/types/enums.d.ts +15 -0
  14. package/dist/esm/src/types/live-responses.d.ts +21 -3
  15. package/dist/esm/src/types/requests.d.ts +23 -0
  16. package/dist/esm/src/types/responses.d.ts +28 -1
  17. package/dist/index.cjs.js +202 -67
  18. package/dist/index.cjs.js.map +1 -1
  19. package/dist/index.node.cjs.js +306 -166
  20. package/dist/index.node.cjs.js.map +1 -1
  21. package/dist/index.node.mjs +306 -167
  22. package/dist/index.node.mjs.map +1 -1
  23. package/dist/src/factory-node.d.ts +19 -0
  24. package/dist/src/methods/chrome-adapter.d.ts +1 -1
  25. package/dist/src/methods/live-session.d.ts +64 -9
  26. package/dist/src/requests/hybrid-helpers.d.ts +7 -2
  27. package/dist/src/requests/response-helpers.d.ts +2 -2
  28. package/dist/src/requests/stream-reader.d.ts +2 -1
  29. package/dist/src/service.d.ts +3 -4
  30. package/dist/src/types/chrome-adapter.d.ts +5 -0
  31. package/dist/src/types/enums.d.ts +15 -0
  32. package/dist/src/types/live-responses.d.ts +21 -3
  33. package/dist/src/types/requests.d.ts +23 -0
  34. package/dist/src/types/responses.d.ts +28 -1
  35. package/package.json +8 -8
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ import { ComponentContainer, InstanceFactoryOptions } from '@firebase/component';
18
+ import { AIService } from './service';
19
+ export declare function factory(container: ComponentContainer, { instanceIdentifier }: InstanceFactoryOptions): AIService;
@@ -25,11 +25,11 @@ import { LanguageModel } from '../types/language-model';
25
25
  export declare class ChromeAdapterImpl implements ChromeAdapter {
26
26
  languageModelProvider: LanguageModel;
27
27
  mode: InferenceMode;
28
- onDeviceParams: OnDeviceParams;
29
28
  static SUPPORTED_MIME_TYPES: string[];
30
29
  private isDownloading;
31
30
  private downloadPromise;
32
31
  private oldSession;
32
+ onDeviceParams: OnDeviceParams;
33
33
  constructor(languageModelProvider: LanguageModel, mode: InferenceMode, onDeviceParams?: OnDeviceParams);
34
34
  /**
35
35
  * Checks if a given request can be made on-device.
@@ -53,32 +53,65 @@ export declare class LiveSession {
53
53
  */
54
54
  send(request: string | Array<string | Part>, turnComplete?: boolean): Promise<void>;
55
55
  /**
56
- * Sends realtime input to the server.
56
+ * Sends text to the server in realtime.
57
57
  *
58
- * @param mediaChunks - The media chunks to send.
58
+ * @example
59
+ * ```javascript
60
+ * liveSession.sendTextRealtime("Hello, how are you?");
61
+ * ```
62
+ *
63
+ * @param text - The text data to send.
59
64
  * @throws If this session has been closed.
60
65
  *
61
66
  * @beta
62
67
  */
63
- sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
68
+ sendTextRealtime(text: string): Promise<void>;
64
69
  /**
65
- * Sends function responses to the server.
70
+ * Sends audio data to the server in realtime.
66
71
  *
67
- * @param functionResponses - The function responses to send.
72
+ * @remarks The server requires that the audio data is base64-encoded 16-bit PCM at 16kHz
73
+ * little-endian.
74
+ *
75
+ * @example
76
+ * ```javascript
77
+ * // const pcmData = ... base64-encoded 16-bit PCM at 16kHz little-endian.
78
+ * const blob = { mimeType: "audio/pcm", data: pcmData };
79
+ * liveSession.sendAudioRealtime(blob);
80
+ * ```
81
+ *
82
+ * @param blob - The base64-encoded PCM data to send to the server in realtime.
68
83
  * @throws If this session has been closed.
69
84
  *
70
85
  * @beta
71
86
  */
72
- sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
87
+ sendAudioRealtime(blob: GenerativeContentBlob): Promise<void>;
73
88
  /**
74
- * Sends a stream of {@link GenerativeContentBlob}.
89
+ * Sends video data to the server in realtime.
75
90
  *
76
- * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
91
+ * @remarks The server requires that the video is sent as individual video frames at 1 FPS. It
92
+ * is recommended to set `mimeType` to `image/jpeg`.
93
+ *
94
+ * @example
95
+ * ```javascript
96
+ * // const videoFrame = ... base64-encoded JPEG data
97
+ * const blob = { mimeType: "image/jpeg", data: videoFrame };
98
+ * liveSession.sendVideoRealtime(blob);
99
+ * ```
100
+ * @param blob - The base64-encoded video data to send to the server in realtime.
77
101
  * @throws If this session has been closed.
78
102
  *
79
103
  * @beta
80
104
  */
81
- sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
105
+ sendVideoRealtime(blob: GenerativeContentBlob): Promise<void>;
106
+ /**
107
+ * Sends function responses to the server.
108
+ *
109
+ * @param functionResponses - The function responses to send.
110
+ * @throws If this session has been closed.
111
+ *
112
+ * @beta
113
+ */
114
+ sendFunctionResponses(functionResponses: FunctionResponse[]): Promise<void>;
82
115
  /**
83
116
  * Yields messages received from the server.
84
117
  * This can only be used by one consumer at a time.
@@ -96,4 +129,26 @@ export declare class LiveSession {
96
129
  * @beta
97
130
  */
98
131
  close(): Promise<void>;
132
+ /**
133
+ * Sends realtime input to the server.
134
+ *
135
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
136
+ *
137
+ * @param mediaChunks - The media chunks to send.
138
+ * @throws If this session has been closed.
139
+ *
140
+ * @beta
141
+ */
142
+ sendMediaChunks(mediaChunks: GenerativeContentBlob[]): Promise<void>;
143
+ /**
144
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
145
+ *
146
+ * Sends a stream of {@link GenerativeContentBlob}.
147
+ *
148
+ * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
149
+ * @throws If this session has been closed.
150
+ *
151
+ * @beta
152
+ */
153
+ sendMediaStream(mediaChunkStream: ReadableStream<GenerativeContentBlob>): Promise<void>;
99
154
  }
@@ -14,7 +14,11 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { GenerateContentRequest, ChromeAdapter } from '../types';
17
+ import { GenerateContentRequest, ChromeAdapter, InferenceSource } from '../types';
18
+ interface CallResult<Response> {
19
+ response: Response;
20
+ inferenceSource: InferenceSource;
21
+ }
18
22
  /**
19
23
  * Dispatches a request to the appropriate backend (on-device or in-cloud)
20
24
  * based on the inference mode.
@@ -25,4 +29,5 @@ import { GenerateContentRequest, ChromeAdapter } from '../types';
25
29
  * @param inCloudCall - The function to call for in-cloud inference.
26
30
  * @returns The response from the backend.
27
31
  */
28
- export declare function callCloudOrDevice<Response>(request: GenerateContentRequest, chromeAdapter: ChromeAdapter | undefined, onDeviceCall: () => Promise<Response>, inCloudCall: () => Promise<Response>): Promise<Response>;
32
+ export declare function callCloudOrDevice<Response>(request: GenerateContentRequest, chromeAdapter: ChromeAdapter | undefined, onDeviceCall: () => Promise<Response>, inCloudCall: () => Promise<Response>): Promise<CallResult<Response>>;
33
+ export {};
@@ -14,12 +14,12 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { EnhancedGenerateContentResponse, FunctionCall, GenerateContentResponse, ImagenGCSImage, ImagenInlineImage, InlineDataPart, Part } from '../types';
17
+ import { EnhancedGenerateContentResponse, FunctionCall, GenerateContentResponse, ImagenGCSImage, ImagenInlineImage, InlineDataPart, Part, InferenceSource } from '../types';
18
18
  /**
19
19
  * Creates an EnhancedGenerateContentResponse object that has helper functions and
20
20
  * other modifications that improve usability.
21
21
  */
22
- export declare function createEnhancedContentResponse(response: GenerateContentResponse): EnhancedGenerateContentResponse;
22
+ export declare function createEnhancedContentResponse(response: GenerateContentResponse, inferenceSource?: InferenceSource): EnhancedGenerateContentResponse;
23
23
  /**
24
24
  * Adds convenience helper methods to a response object, including stream
25
25
  * chunks (as long as each chunk is a complete GenerateContentResponse JSON).
@@ -16,6 +16,7 @@
16
16
  */
17
17
  import { GenerateContentResponse, GenerateContentStreamResult } from '../types';
18
18
  import { ApiSettings } from '../types/internal';
19
+ import { InferenceSource } from '../public-types';
19
20
  /**
20
21
  * Process a response.body stream from the backend and return an
21
22
  * iterator that provides one complete GenerateContentResponse at a time
@@ -24,7 +25,7 @@ import { ApiSettings } from '../types/internal';
24
25
  *
25
26
  * @param response - Response from a fetch call
26
27
  */
27
- export declare function processStream(response: Response, apiSettings: ApiSettings): GenerateContentStreamResult;
28
+ export declare function processStream(response: Response, apiSettings: ApiSettings, inferenceSource?: InferenceSource): GenerateContentStreamResult;
28
29
  /**
29
30
  * Reads a raw stream from the fetch response and join incomplete
30
31
  * chunks, returning a new stream that provides a single complete
@@ -15,21 +15,20 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { FirebaseApp, _FirebaseService } from '@firebase/app';
18
- import { AI, AIOptions, InferenceMode, OnDeviceParams } from './public-types';
18
+ import { AI, AIOptions, ChromeAdapter, InferenceMode, OnDeviceParams } from './public-types';
19
19
  import { AppCheckInternalComponentName, FirebaseAppCheckInternal } from '@firebase/app-check-interop-types';
20
20
  import { Provider } from '@firebase/component';
21
21
  import { FirebaseAuthInternal, FirebaseAuthInternalName } from '@firebase/auth-interop-types';
22
22
  import { Backend } from './backend';
23
- import { ChromeAdapterImpl } from './methods/chrome-adapter';
24
23
  export declare class AIService implements AI, _FirebaseService {
25
24
  app: FirebaseApp;
26
25
  backend: Backend;
27
- chromeAdapterFactory?: ((mode: InferenceMode, window?: Window, params?: OnDeviceParams) => ChromeAdapterImpl | undefined) | undefined;
26
+ chromeAdapterFactory?: ((mode: InferenceMode, window?: Window, params?: OnDeviceParams) => ChromeAdapter | undefined) | undefined;
28
27
  auth: FirebaseAuthInternal | null;
29
28
  appCheck: FirebaseAppCheckInternal | null;
30
29
  _options?: Omit<AIOptions, 'backend'>;
31
30
  location: string;
32
- constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>, chromeAdapterFactory?: ((mode: InferenceMode, window?: Window, params?: OnDeviceParams) => ChromeAdapterImpl | undefined) | undefined);
31
+ constructor(app: FirebaseApp, backend: Backend, authProvider?: Provider<FirebaseAuthInternalName>, appCheckProvider?: Provider<AppCheckInternalComponentName>, chromeAdapterFactory?: ((mode: InferenceMode, window?: Window, params?: OnDeviceParams) => ChromeAdapter | undefined) | undefined);
33
32
  _delete(): Promise<void>;
34
33
  set options(optionsToSet: AIOptions);
35
34
  get options(): AIOptions | undefined;
@@ -14,6 +14,7 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
+ import { InferenceMode } from './enums';
17
18
  import { CountTokensRequest, GenerateContentRequest } from './requests';
18
19
  /**
19
20
  * Defines an inference "backend" that uses Chrome's on-device model,
@@ -25,6 +26,10 @@ import { CountTokensRequest, GenerateContentRequest } from './requests';
25
26
  * @beta
26
27
  */
27
28
  export interface ChromeAdapter {
29
+ /**
30
+ * @internal
31
+ */
32
+ mode: InferenceMode;
28
33
  /**
29
34
  * Checks if the on-device model is capable of handling a given
30
35
  * request.
@@ -349,6 +349,21 @@ export declare const InferenceMode: {
349
349
  * @beta
350
350
  */
351
351
  export type InferenceMode = (typeof InferenceMode)[keyof typeof InferenceMode];
352
+ /**
353
+ * Indicates whether inference happened on-device or in-cloud.
354
+ *
355
+ * @beta
356
+ */
357
+ export declare const InferenceSource: {
358
+ readonly ON_DEVICE: "on_device";
359
+ readonly IN_CLOUD: "in_cloud";
360
+ };
361
+ /**
362
+ * Indicates whether inference happened on-device or in-cloud.
363
+ *
364
+ * @beta
365
+ */
366
+ export type InferenceSource = (typeof InferenceSource)[keyof typeof InferenceSource];
352
367
  /**
353
368
  * Represents the result of the code execution.
354
369
  *
@@ -15,7 +15,8 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { Content, FunctionResponse, GenerativeContentBlob, Part } from './content';
18
- import { LiveGenerationConfig, Tool, ToolConfig } from './requests';
18
+ import { AudioTranscriptionConfig, LiveGenerationConfig, Tool, ToolConfig } from './requests';
19
+ import { Transcription } from './responses';
19
20
  /**
20
21
  * User input that is sent to the model.
21
22
  *
@@ -25,6 +26,8 @@ export interface _LiveClientContent {
25
26
  clientContent: {
26
27
  turns: [Content];
27
28
  turnComplete: boolean;
29
+ inputTranscription?: Transcription;
30
+ outputTranscription?: Transcription;
28
31
  };
29
32
  }
30
33
  /**
@@ -34,7 +37,13 @@ export interface _LiveClientContent {
34
37
  */
35
38
  export interface _LiveClientRealtimeInput {
36
39
  realtimeInput: {
37
- mediaChunks: GenerativeContentBlob[];
40
+ text?: string;
41
+ audio?: GenerativeContentBlob;
42
+ video?: GenerativeContentBlob;
43
+ /**
44
+ * @deprecated Use `text`, `audio`, and `video` instead.
45
+ */
46
+ mediaChunks?: GenerativeContentBlob[];
38
47
  };
39
48
  }
40
49
  /**
@@ -53,9 +62,18 @@ export interface _LiveClientToolResponse {
53
62
  export interface _LiveClientSetup {
54
63
  setup: {
55
64
  model: string;
56
- generationConfig?: LiveGenerationConfig;
65
+ generationConfig?: _LiveGenerationConfig;
57
66
  tools?: Tool[];
58
67
  toolConfig?: ToolConfig;
59
68
  systemInstruction?: string | Part | Content;
69
+ inputAudioTranscription?: AudioTranscriptionConfig;
70
+ outputAudioTranscription?: AudioTranscriptionConfig;
60
71
  };
61
72
  }
73
+ /**
74
+ * The Live Generation Config.
75
+ *
76
+ * The public API ({@link LiveGenerationConfig}) has `inputAudioTranscription` and `outputAudioTranscription`,
77
+ * but the server expects these fields to be in the top-level `setup` message. This was a conscious API decision.
78
+ */
79
+ export type _LiveGenerationConfig = Omit<LiveGenerationConfig, 'inputAudioTranscription' | 'outputAudioTranscription'>;
@@ -167,6 +167,24 @@ export interface LiveGenerationConfig {
167
167
  * The modalities of the response.
168
168
  */
169
169
  responseModalities?: ResponseModality[];
170
+ /**
171
+ * Enables transcription of audio input.
172
+ *
173
+ * When enabled, the model will respond with transcriptions of your audio input in the `inputTranscriptions` property
174
+ * in {@link LiveServerContent} messages. Note that the transcriptions are broken up across
175
+ * messages, so you may only receive small amounts of text per message. For example, if you ask the model
176
+ * "How are you today?", the model may transcribe that input across three messages, broken up as "How a", "re yo", "u today?".
177
+ */
178
+ inputAudioTranscription?: AudioTranscriptionConfig;
179
+ /**
180
+ * Enables transcription of audio input.
181
+ *
182
+ * When enabled, the model will respond with transcriptions of its audio output in the `outputTranscription` property
183
+ * in {@link LiveServerContent} messages. Note that the transcriptions are broken up across
184
+ * messages, so you may only receive small amounts of text per message. For example, if the model says
185
+ * "How are you today?", the model may transcribe that output across three messages, broken up as "How a", "re yo", "u today?".
186
+ */
187
+ outputAudioTranscription?: AudioTranscriptionConfig;
170
188
  }
171
189
  /**
172
190
  * Params for {@link GenerativeModel.startChat}.
@@ -439,3 +457,8 @@ export interface SpeechConfig {
439
457
  */
440
458
  voiceConfig?: VoiceConfig;
441
459
  }
460
+ /**
461
+ * The audio transcription configuration.
462
+ */
463
+ export interface AudioTranscriptionConfig {
464
+ }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { Content, FunctionCall, InlineDataPart } from './content';
18
- import { BlockReason, FinishReason, HarmCategory, HarmProbability, HarmSeverity, Modality } from './enums';
18
+ import { BlockReason, FinishReason, HarmCategory, HarmProbability, HarmSeverity, InferenceSource, Modality } from './enums';
19
19
  /**
20
20
  * Result object returned from {@link GenerativeModel.generateContent} call.
21
21
  *
@@ -76,6 +76,12 @@ export interface EnhancedGenerateContentResponse extends GenerateContentResponse
76
76
  * set to `true`.
77
77
  */
78
78
  thoughtSummary: () => string | undefined;
79
+ /**
80
+ * Indicates whether inference happened on-device or in-cloud.
81
+ *
82
+ * @beta
83
+ */
84
+ inferenceSource?: InferenceSource;
79
85
  }
80
86
  /**
81
87
  * Individual response from {@link GenerativeModel.generateContent} and
@@ -510,6 +516,27 @@ export interface LiveServerContent {
510
516
  * model was not interrupted.
511
517
  */
512
518
  interrupted?: boolean;
519
+ /**
520
+ * Transcription of the audio that was input to the model.
521
+ */
522
+ inputTranscription?: Transcription;
523
+ /**
524
+ * Transcription of the audio output from the model.
525
+ */
526
+ outputTranscription?: Transcription;
527
+ }
528
+ /**
529
+ * Transcription of audio. This can be returned from a {@link LiveGenerativeModel} if transcription
530
+ * is enabled with the `inputAudioTranscription` or `outputAudioTranscription` properties on
531
+ * the {@link LiveGenerationConfig}.
532
+ *
533
+ * @beta
534
+ */
535
+ export interface Transcription {
536
+ /**
537
+ * The text transcription of the audio.
538
+ */
539
+ text?: string;
513
540
  }
514
541
  /**
515
542
  * A request from the model for the client to execute one or more functions.