@firebase/ai 2.7.0 → 2.8.0-20260203132428

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.
@@ -21,6 +21,7 @@ export declare const STAGING_URL = "https://staging-firebasevertexai.sandbox.goo
21
21
  export declare const DEFAULT_API_VERSION = "v1beta";
22
22
  export declare const PACKAGE_VERSION: string;
23
23
  export declare const LANGUAGE_TAG = "gl-js";
24
+ export declare const HYBRID_TAG = "hybrid";
24
25
  export declare const DEFAULT_FETCH_TIMEOUT_MS: number;
25
26
  /**
26
27
  * Defines the name of the default in-cloud model to use for hybrid inference.
@@ -14,7 +14,7 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { FunctionResponse, GenerativeContentBlob, LiveServerContent, LiveServerToolCall, LiveServerToolCallCancellation, Part } from '../public-types';
17
+ import { FunctionResponse, GenerativeContentBlob, LiveServerContent, LiveServerGoingAwayNotice, LiveServerToolCall, LiveServerToolCallCancellation, Part } from '../public-types';
18
18
  import { WebSocketHandler } from '../websocket';
19
19
  /**
20
20
  * Represents an active, real-time, bidirectional conversation with the model.
@@ -121,7 +121,7 @@ export declare class LiveSession {
121
121
  *
122
122
  * @beta
123
123
  */
124
- receive(): AsyncGenerator<LiveServerContent | LiveServerToolCall | LiveServerToolCallCancellation>;
124
+ receive(): AsyncGenerator<LiveServerContent | LiveServerToolCall | LiveServerToolCallCancellation | LiveServerGoingAwayNotice>;
125
125
  /**
126
126
  * Closes this session.
127
127
  * All methods on this session will throw an error once this resolves.
@@ -138,7 +138,7 @@ export interface FileDataPart {
138
138
  /**
139
139
  * Represents the code that is executed by the model.
140
140
  *
141
- * @beta
141
+ * @public
142
142
  */
143
143
  export interface ExecutableCodePart {
144
144
  text?: never;
@@ -157,7 +157,7 @@ export interface ExecutableCodePart {
157
157
  /**
158
158
  * Represents the code execution result from the model.
159
159
  *
160
- * @beta
160
+ * @public
161
161
  */
162
162
  export interface CodeExecutionResultPart {
163
163
  text?: never;
@@ -176,7 +176,7 @@ export interface CodeExecutionResultPart {
176
176
  /**
177
177
  * An interface for executable code returned by the model.
178
178
  *
179
- * @beta
179
+ * @public
180
180
  */
181
181
  export interface ExecutableCode {
182
182
  /**
@@ -191,7 +191,7 @@ export interface ExecutableCode {
191
191
  /**
192
192
  * The results of code execution run by the model.
193
193
  *
194
- * @beta
194
+ * @public
195
195
  */
196
196
  export interface CodeExecutionResult {
197
197
  /**
@@ -367,7 +367,7 @@ export type InferenceSource = (typeof InferenceSource)[keyof typeof InferenceSou
367
367
  /**
368
368
  * Represents the result of the code execution.
369
369
  *
370
- * @beta
370
+ * @public
371
371
  */
372
372
  export declare const Outcome: {
373
373
  UNSPECIFIED: string;
@@ -378,13 +378,13 @@ export declare const Outcome: {
378
378
  /**
379
379
  * Represents the result of the code execution.
380
380
  *
381
- * @beta
381
+ * @public
382
382
  */
383
383
  export type Outcome = (typeof Outcome)[keyof typeof Outcome];
384
384
  /**
385
385
  * The programming language of the code.
386
386
  *
387
- * @beta
387
+ * @public
388
388
  */
389
389
  export declare const Language: {
390
390
  UNSPECIFIED: string;
@@ -393,7 +393,7 @@ export declare const Language: {
393
393
  /**
394
394
  * The programming language of the code.
395
395
  *
396
- * @beta
396
+ * @public
397
397
  */
398
398
  export type Language = (typeof Language)[keyof typeof Language];
399
399
  /**
@@ -17,6 +17,7 @@
17
17
  import { AppCheckTokenResult } from '@firebase/app-check-interop-types';
18
18
  import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
19
19
  import { Backend } from '../backend';
20
+ import { InferenceMode } from './enums';
20
21
  export * from './imagen/internal';
21
22
  export interface ApiSettings {
22
23
  apiKey: string;
@@ -30,4 +31,5 @@ export interface ApiSettings {
30
31
  backend: Backend;
31
32
  getAuthToken?: () => Promise<FirebaseAuthTokenData | null>;
32
33
  getAppCheckToken?: () => Promise<AppCheckTokenResult>;
34
+ inferenceMode?: InferenceMode;
33
35
  }
@@ -118,6 +118,18 @@ export interface UsageMetadata {
118
118
  * A list of tokens used by tools, broken down by modality.
119
119
  */
120
120
  toolUsePromptTokensDetails?: ModalityTokenCount[];
121
+ /**
122
+ * The number of tokens in the prompt that were served from the cache.
123
+ * If implicit caching is not active or no content was cached,
124
+ * this will be 0.
125
+ */
126
+ cachedContentTokenCount?: number;
127
+ /**
128
+ * Detailed breakdown of the cached tokens by modality (for example, text or
129
+ * image). This list provides granular insight into which parts of
130
+ * the content were cached.
131
+ */
132
+ cacheTokensDetails?: ModalityTokenCount[];
121
133
  }
122
134
  /**
123
135
  * Represents token counting info for a single modality.
@@ -341,7 +353,7 @@ export interface Segment {
341
353
  /**
342
354
  * Metadata related to {@link URLContextTool}.
343
355
  *
344
- * @beta
356
+ * @public
345
357
  */
346
358
  export interface URLContextMetadata {
347
359
  /**
@@ -352,7 +364,7 @@ export interface URLContextMetadata {
352
364
  /**
353
365
  * Metadata for a single URL retrieved by the {@link URLContextTool} tool.
354
366
  *
355
- * @beta
367
+ * @public
356
368
  */
357
369
  export interface URLMetadata {
358
370
  /**
@@ -379,7 +391,7 @@ export interface URLMetadata {
379
391
  * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
380
392
  * <br/>
381
393
  *
382
- * @beta
394
+ * @public
383
395
  */
384
396
  export declare const URLRetrievalStatus: {
385
397
  /**
@@ -418,7 +430,7 @@ export declare const URLRetrievalStatus: {
418
430
  * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
419
431
  * <br/>
420
432
  *
421
- * @beta
433
+ * @public
422
434
  */
423
435
  export type URLRetrievalStatus = (typeof URLRetrievalStatus)[keyof typeof URLRetrievalStatus];
424
436
  /**
@@ -562,6 +574,18 @@ export interface LiveServerToolCallCancellation {
562
574
  */
563
575
  functionIds: string[];
564
576
  }
577
+ /**
578
+ * Notification that the server will not be able to service the client soon.
579
+ *
580
+ * @beta
581
+ */
582
+ export interface LiveServerGoingAwayNotice {
583
+ type: 'goingAwayNotice';
584
+ /**
585
+ * The remaining time (in seconds) before the connection will be terminated.
586
+ */
587
+ timeLeft: number;
588
+ }
565
589
  /**
566
590
  * The types of responses that can be returned by {@link LiveSession.receive}.
567
591
  *
@@ -571,6 +595,7 @@ export declare const LiveResponseType: {
571
595
  SERVER_CONTENT: string;
572
596
  TOOL_CALL: string;
573
597
  TOOL_CALL_CANCELLATION: string;
598
+ GOING_AWAY_NOTICE: string;
574
599
  };
575
600
  /**
576
601
  * The types of responses that can be returned by {@link LiveSession.receive}.
package/dist/index.cjs.js CHANGED
@@ -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.7.0";
11
+ var version = "2.8.0-20260203132428";
12
12
 
13
13
  /**
14
14
  * @license
@@ -32,6 +32,7 @@ const DEFAULT_DOMAIN = 'firebasevertexai.googleapis.com';
32
32
  const DEFAULT_API_VERSION = 'v1beta';
33
33
  const PACKAGE_VERSION = version;
34
34
  const LANGUAGE_TAG = 'gl-js';
35
+ const HYBRID_TAG = 'hybrid';
35
36
  const DEFAULT_FETCH_TIMEOUT_MS = 180 * 1000;
36
37
  /**
37
38
  * Defines the name of the default in-cloud model to use for hybrid inference.
@@ -395,7 +396,7 @@ const InferenceSource = {
395
396
  /**
396
397
  * Represents the result of the code execution.
397
398
  *
398
- * @beta
399
+ * @public
399
400
  */
400
401
  const Outcome = {
401
402
  UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
@@ -406,7 +407,7 @@ const Outcome = {
406
407
  /**
407
408
  * The programming language of the code.
408
409
  *
409
- * @beta
410
+ * @public
410
411
  */
411
412
  const Language = {
412
413
  UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
@@ -457,7 +458,7 @@ const ThinkingLevel = {
457
458
  * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
458
459
  * <br/>
459
460
  *
460
- * @beta
461
+ * @public
461
462
  */
462
463
  const URLRetrievalStatus = {
463
464
  /**
@@ -489,7 +490,8 @@ const URLRetrievalStatus = {
489
490
  const LiveResponseType = {
490
491
  SERVER_CONTENT: 'serverContent',
491
492
  TOOL_CALL: 'toolCall',
492
- TOOL_CALL_CANCELLATION: 'toolCallCancellation'
493
+ TOOL_CALL_CANCELLATION: 'toolCallCancellation',
494
+ GOING_AWAY_NOTICE: 'goingAwayNotice'
493
495
  };
494
496
 
495
497
  /**
@@ -1516,16 +1518,25 @@ class WebSocketUrl {
1516
1518
  /**
1517
1519
  * Log language and "fire/version" to x-goog-api-client
1518
1520
  */
1519
- function getClientHeaders() {
1521
+ function getClientHeaders(url) {
1520
1522
  const loggingTags = [];
1521
1523
  loggingTags.push(`${LANGUAGE_TAG}/${PACKAGE_VERSION}`);
1522
1524
  loggingTags.push(`fire/${PACKAGE_VERSION}`);
1525
+ /**
1526
+ * No call would be made if ONLY_ON_DEVICE.
1527
+ * ONLY_IN_CLOUD does not indicate an intention to use hybrid.
1528
+ */
1529
+ if (url.params.apiSettings.inferenceMode === InferenceMode.PREFER_ON_DEVICE ||
1530
+ url.params.apiSettings.inferenceMode === InferenceMode.PREFER_IN_CLOUD) {
1531
+ // No version
1532
+ loggingTags.push(HYBRID_TAG);
1533
+ }
1523
1534
  return loggingTags.join(' ');
1524
1535
  }
1525
1536
  async function getHeaders(url) {
1526
1537
  const headers = new Headers();
1527
1538
  headers.append('Content-Type', 'application/json');
1528
- headers.append('x-goog-api-client', getClientHeaders());
1539
+ headers.append('x-goog-api-client', getClientHeaders(url));
1529
1540
  headers.append('x-goog-api-key', url.params.apiSettings.apiKey);
1530
1541
  if (url.params.apiSettings.automaticDataCollectionEnabled) {
1531
1542
  headers.append('X-Firebase-Appid', url.params.apiSettings.appId);
@@ -3142,6 +3153,13 @@ class LiveSession {
3142
3153
  ...message.toolCallCancellation
3143
3154
  };
3144
3155
  }
3156
+ else if ('goAway' in message) {
3157
+ const notice = message.goAway;
3158
+ yield {
3159
+ type: LiveResponseType.GOING_AWAY_NOTICE,
3160
+ timeLeft: parseDuration(notice.timeLeft)
3161
+ };
3162
+ }
3145
3163
  else {
3146
3164
  logger.warn(`Received an unknown message type from the server: ${JSON.stringify(message)}`);
3147
3165
  }
@@ -3220,6 +3238,18 @@ class LiveSession {
3220
3238
  }
3221
3239
  }
3222
3240
  }
3241
+ /**
3242
+ * Parses a duration string (e.g. "3.000000001s") into a number of seconds.
3243
+ *
3244
+ * @param duration - The duration string to parse.
3245
+ * @returns The duration in seconds.
3246
+ */
3247
+ function parseDuration(duration) {
3248
+ if (!duration || !duration.endsWith('s')) {
3249
+ return 0;
3250
+ }
3251
+ return Number(duration.slice(0, -1)); // slice removes the trailing 's'.
3252
+ }
3223
3253
 
3224
3254
  /**
3225
3255
  * @license
@@ -4467,7 +4497,9 @@ function getGenerativeModel(ai, modelParams, requestOptions) {
4467
4497
  * chromeAdapterFactory() method.
4468
4498
  */
4469
4499
  const chromeAdapter = ai.chromeAdapterFactory?.(hybridParams.mode, typeof window === 'undefined' ? undefined : window, hybridParams.onDeviceParams);
4470
- return new GenerativeModel(ai, inCloudParams, requestOptions, chromeAdapter);
4500
+ const generativeModel = new GenerativeModel(ai, inCloudParams, requestOptions, chromeAdapter);
4501
+ generativeModel._apiSettings.inferenceMode = hybridParams.mode;
4502
+ return generativeModel;
4471
4503
  }
4472
4504
  /**
4473
4505
  * Returns an {@link ImagenModel} class with methods for using Imagen.