@depup/firebase__ai 2.9.0-depup.0

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 (119) hide show
  1. package/README.md +31 -0
  2. package/changes.json +10 -0
  3. package/dist/ai-public.d.ts +3472 -0
  4. package/dist/ai.d.ts +3712 -0
  5. package/dist/esm/index.esm.js +4765 -0
  6. package/dist/esm/index.esm.js.map +1 -0
  7. package/dist/esm/package.json +1 -0
  8. package/dist/esm/src/api.d.ts +121 -0
  9. package/dist/esm/src/backend.d.ts +98 -0
  10. package/dist/esm/src/constants.d.ts +29 -0
  11. package/dist/esm/src/errors.d.ts +35 -0
  12. package/dist/esm/src/factory-browser.d.ts +19 -0
  13. package/dist/esm/src/factory-node.d.ts +19 -0
  14. package/dist/esm/src/googleai-mappers.d.ts +73 -0
  15. package/dist/esm/src/helpers.d.ts +30 -0
  16. package/dist/esm/src/index.d.ts +13 -0
  17. package/dist/esm/src/index.node.d.ts +7 -0
  18. package/dist/esm/src/logger.d.ts +18 -0
  19. package/dist/esm/src/methods/chat-session-helpers.d.ts +18 -0
  20. package/dist/esm/src/methods/chat-session.d.ts +77 -0
  21. package/dist/esm/src/methods/chrome-adapter.d.ts +124 -0
  22. package/dist/esm/src/methods/count-tokens.d.ts +21 -0
  23. package/dist/esm/src/methods/generate-content.d.ts +25 -0
  24. package/dist/esm/src/methods/live-session-helpers.d.ts +154 -0
  25. package/dist/esm/src/methods/live-session.d.ts +154 -0
  26. package/dist/esm/src/models/ai-model.d.ts +72 -0
  27. package/dist/esm/src/models/generative-model.d.ts +56 -0
  28. package/dist/esm/src/models/imagen-model.d.ts +102 -0
  29. package/dist/esm/src/models/index.d.ts +20 -0
  30. package/dist/esm/src/models/live-generative-model.d.ts +55 -0
  31. package/dist/esm/src/models/template-generative-model.d.ts +64 -0
  32. package/dist/esm/src/models/template-imagen-model.d.ts +51 -0
  33. package/dist/esm/src/models/utils.d.ts +26 -0
  34. package/dist/esm/src/public-types.d.ts +97 -0
  35. package/dist/esm/src/requests/hybrid-helpers.d.ts +33 -0
  36. package/dist/esm/src/requests/imagen-image-format.d.ts +61 -0
  37. package/dist/esm/src/requests/request-helpers.d.ts +28 -0
  38. package/dist/esm/src/requests/request.d.ts +69 -0
  39. package/dist/esm/src/requests/response-helpers.d.ts +57 -0
  40. package/dist/esm/src/requests/schema-builder.d.ts +170 -0
  41. package/dist/esm/src/requests/stream-reader.d.ts +39 -0
  42. package/dist/esm/src/service.d.ts +35 -0
  43. package/dist/esm/src/types/chrome-adapter.d.ts +61 -0
  44. package/dist/esm/src/types/content.d.ts +266 -0
  45. package/dist/esm/src/types/enums.d.ts +419 -0
  46. package/dist/esm/src/types/error.d.ts +89 -0
  47. package/dist/esm/src/types/googleai.d.ts +57 -0
  48. package/dist/esm/src/types/imagen/index.d.ts +18 -0
  49. package/dist/esm/src/types/imagen/internal.d.ts +134 -0
  50. package/dist/esm/src/types/imagen/requests.d.ts +245 -0
  51. package/dist/esm/src/types/imagen/responses.d.ts +79 -0
  52. package/dist/esm/src/types/index.d.ts +26 -0
  53. package/dist/esm/src/types/internal.d.ts +35 -0
  54. package/dist/esm/src/types/language-model.d.ts +107 -0
  55. package/dist/esm/src/types/live-responses.d.ts +79 -0
  56. package/dist/esm/src/types/requests.d.ts +543 -0
  57. package/dist/esm/src/types/responses.d.ts +607 -0
  58. package/dist/esm/src/types/schema.d.ts +139 -0
  59. package/dist/esm/src/websocket.d.ts +67 -0
  60. package/dist/index.cjs.js +4820 -0
  61. package/dist/index.cjs.js.map +1 -0
  62. package/dist/index.node.cjs.js +4512 -0
  63. package/dist/index.node.cjs.js.map +1 -0
  64. package/dist/index.node.mjs +4457 -0
  65. package/dist/index.node.mjs.map +1 -0
  66. package/dist/src/api.d.ts +121 -0
  67. package/dist/src/backend.d.ts +98 -0
  68. package/dist/src/constants.d.ts +29 -0
  69. package/dist/src/errors.d.ts +35 -0
  70. package/dist/src/factory-browser.d.ts +19 -0
  71. package/dist/src/factory-node.d.ts +19 -0
  72. package/dist/src/googleai-mappers.d.ts +73 -0
  73. package/dist/src/helpers.d.ts +30 -0
  74. package/dist/src/index.d.ts +13 -0
  75. package/dist/src/index.node.d.ts +7 -0
  76. package/dist/src/logger.d.ts +18 -0
  77. package/dist/src/methods/chat-session-helpers.d.ts +18 -0
  78. package/dist/src/methods/chat-session.d.ts +77 -0
  79. package/dist/src/methods/chrome-adapter.d.ts +124 -0
  80. package/dist/src/methods/count-tokens.d.ts +21 -0
  81. package/dist/src/methods/generate-content.d.ts +25 -0
  82. package/dist/src/methods/live-session-helpers.d.ts +154 -0
  83. package/dist/src/methods/live-session.d.ts +154 -0
  84. package/dist/src/models/ai-model.d.ts +72 -0
  85. package/dist/src/models/generative-model.d.ts +56 -0
  86. package/dist/src/models/imagen-model.d.ts +102 -0
  87. package/dist/src/models/index.d.ts +20 -0
  88. package/dist/src/models/live-generative-model.d.ts +55 -0
  89. package/dist/src/models/template-generative-model.d.ts +64 -0
  90. package/dist/src/models/template-imagen-model.d.ts +51 -0
  91. package/dist/src/models/utils.d.ts +26 -0
  92. package/dist/src/public-types.d.ts +97 -0
  93. package/dist/src/requests/hybrid-helpers.d.ts +33 -0
  94. package/dist/src/requests/imagen-image-format.d.ts +61 -0
  95. package/dist/src/requests/request-helpers.d.ts +28 -0
  96. package/dist/src/requests/request.d.ts +69 -0
  97. package/dist/src/requests/response-helpers.d.ts +57 -0
  98. package/dist/src/requests/schema-builder.d.ts +170 -0
  99. package/dist/src/requests/stream-reader.d.ts +39 -0
  100. package/dist/src/service.d.ts +35 -0
  101. package/dist/src/tsdoc-metadata.json +11 -0
  102. package/dist/src/types/chrome-adapter.d.ts +61 -0
  103. package/dist/src/types/content.d.ts +266 -0
  104. package/dist/src/types/enums.d.ts +419 -0
  105. package/dist/src/types/error.d.ts +89 -0
  106. package/dist/src/types/googleai.d.ts +57 -0
  107. package/dist/src/types/imagen/index.d.ts +18 -0
  108. package/dist/src/types/imagen/internal.d.ts +134 -0
  109. package/dist/src/types/imagen/requests.d.ts +245 -0
  110. package/dist/src/types/imagen/responses.d.ts +79 -0
  111. package/dist/src/types/index.d.ts +26 -0
  112. package/dist/src/types/internal.d.ts +35 -0
  113. package/dist/src/types/language-model.d.ts +107 -0
  114. package/dist/src/types/live-responses.d.ts +79 -0
  115. package/dist/src/types/requests.d.ts +543 -0
  116. package/dist/src/types/responses.d.ts +607 -0
  117. package/dist/src/types/schema.d.ts +139 -0
  118. package/dist/src/websocket.d.ts +67 -0
  119. package/package.json +106 -0
@@ -0,0 +1,4765 @@
1
+ import { _isFirebaseServerApp, _getProvider, getApp, _registerComponent, registerVersion } from '@firebase/app';
2
+ import { Component } from '@firebase/component';
3
+ import { FirebaseError, Deferred, getModularInstance } from '@firebase/util';
4
+ import { Logger } from '@firebase/logger';
5
+
6
+ var name = "@firebase/ai";
7
+ var version = "2.9.0";
8
+
9
+ /**
10
+ * @license
11
+ * Copyright 2024 Google LLC
12
+ *
13
+ * Licensed under the Apache License, Version 2.0 (the "License");
14
+ * you may not use this file except in compliance with the License.
15
+ * You may obtain a copy of the License at
16
+ *
17
+ * http://www.apache.org/licenses/LICENSE-2.0
18
+ *
19
+ * Unless required by applicable law or agreed to in writing, software
20
+ * distributed under the License is distributed on an "AS IS" BASIS,
21
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ * See the License for the specific language governing permissions and
23
+ * limitations under the License.
24
+ */
25
+ const AI_TYPE = 'AI';
26
+ const DEFAULT_LOCATION = 'us-central1';
27
+ const DEFAULT_DOMAIN = 'firebasevertexai.googleapis.com';
28
+ const DEFAULT_API_VERSION = 'v1beta';
29
+ const PACKAGE_VERSION = version;
30
+ const LANGUAGE_TAG = 'gl-js';
31
+ const HYBRID_TAG = 'hybrid';
32
+ const DEFAULT_FETCH_TIMEOUT_MS = 180 * 1000;
33
+ /**
34
+ * Defines the name of the default in-cloud model to use for hybrid inference.
35
+ */
36
+ const DEFAULT_HYBRID_IN_CLOUD_MODEL = 'gemini-2.5-flash-lite';
37
+
38
+ /**
39
+ * @license
40
+ * Copyright 2024 Google LLC
41
+ *
42
+ * Licensed under the Apache License, Version 2.0 (the "License");
43
+ * you may not use this file except in compliance with the License.
44
+ * You may obtain a copy of the License at
45
+ *
46
+ * http://www.apache.org/licenses/LICENSE-2.0
47
+ *
48
+ * Unless required by applicable law or agreed to in writing, software
49
+ * distributed under the License is distributed on an "AS IS" BASIS,
50
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51
+ * See the License for the specific language governing permissions and
52
+ * limitations under the License.
53
+ */
54
+ /**
55
+ * Error class for the Firebase AI SDK.
56
+ *
57
+ * @public
58
+ */
59
+ class AIError extends FirebaseError {
60
+ /**
61
+ * Constructs a new instance of the `AIError` class.
62
+ *
63
+ * @param code - The error code from {@link (AIErrorCode:type)}.
64
+ * @param message - A human-readable message describing the error.
65
+ * @param customErrorData - Optional error data.
66
+ */
67
+ constructor(code, message, customErrorData) {
68
+ // Match error format used by FirebaseError from ErrorFactory
69
+ const service = AI_TYPE;
70
+ const fullCode = `${service}/${code}`;
71
+ const fullMessage = `${service}: ${message} (${fullCode})`;
72
+ super(code, fullMessage);
73
+ this.code = code;
74
+ this.customErrorData = customErrorData;
75
+ // FirebaseError initializes a stack trace, but it assumes the error is created from the error
76
+ // factory. Since we break this assumption, we set the stack trace to be originating from this
77
+ // constructor.
78
+ // This is only supported in V8.
79
+ if (Error.captureStackTrace) {
80
+ // Allows us to initialize the stack trace without including the constructor itself at the
81
+ // top level of the stack trace.
82
+ Error.captureStackTrace(this, AIError);
83
+ }
84
+ // Allows instanceof AIError in ES5/ES6
85
+ // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
86
+ // TODO(dlarocque): Replace this with `new.target`: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
87
+ // which we can now use since we no longer target ES5.
88
+ Object.setPrototypeOf(this, AIError.prototype);
89
+ // Since Error is an interface, we don't inherit toString and so we define it ourselves.
90
+ this.toString = () => fullMessage;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * @license
96
+ * Copyright 2024 Google LLC
97
+ *
98
+ * Licensed under the Apache License, Version 2.0 (the "License");
99
+ * you may not use this file except in compliance with the License.
100
+ * You may obtain a copy of the License at
101
+ *
102
+ * http://www.apache.org/licenses/LICENSE-2.0
103
+ *
104
+ * Unless required by applicable law or agreed to in writing, software
105
+ * distributed under the License is distributed on an "AS IS" BASIS,
106
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
107
+ * See the License for the specific language governing permissions and
108
+ * limitations under the License.
109
+ */
110
+ /**
111
+ * Possible roles.
112
+ * @public
113
+ */
114
+ const POSSIBLE_ROLES = ['user', 'model', 'function', 'system'];
115
+ /**
116
+ * Harm categories that would cause prompts or candidates to be blocked.
117
+ * @public
118
+ */
119
+ const HarmCategory = {
120
+ HARM_CATEGORY_HATE_SPEECH: 'HARM_CATEGORY_HATE_SPEECH',
121
+ HARM_CATEGORY_SEXUALLY_EXPLICIT: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
122
+ HARM_CATEGORY_HARASSMENT: 'HARM_CATEGORY_HARASSMENT',
123
+ HARM_CATEGORY_DANGEROUS_CONTENT: 'HARM_CATEGORY_DANGEROUS_CONTENT'
124
+ };
125
+ /**
126
+ * Threshold above which a prompt or candidate will be blocked.
127
+ * @public
128
+ */
129
+ const HarmBlockThreshold = {
130
+ /**
131
+ * Content with `NEGLIGIBLE` will be allowed.
132
+ */
133
+ BLOCK_LOW_AND_ABOVE: 'BLOCK_LOW_AND_ABOVE',
134
+ /**
135
+ * Content with `NEGLIGIBLE` and `LOW` will be allowed.
136
+ */
137
+ BLOCK_MEDIUM_AND_ABOVE: 'BLOCK_MEDIUM_AND_ABOVE',
138
+ /**
139
+ * Content with `NEGLIGIBLE`, `LOW`, and `MEDIUM` will be allowed.
140
+ */
141
+ BLOCK_ONLY_HIGH: 'BLOCK_ONLY_HIGH',
142
+ /**
143
+ * All content will be allowed.
144
+ */
145
+ BLOCK_NONE: 'BLOCK_NONE',
146
+ /**
147
+ * All content will be allowed. This is the same as `BLOCK_NONE`, but the metadata corresponding
148
+ * to the {@link (HarmCategory:type)} will not be present in the response.
149
+ */
150
+ OFF: 'OFF'
151
+ };
152
+ /**
153
+ * This property is not supported in the Gemini Developer API ({@link GoogleAIBackend}).
154
+ *
155
+ * @public
156
+ */
157
+ const HarmBlockMethod = {
158
+ /**
159
+ * The harm block method uses both probability and severity scores.
160
+ */
161
+ SEVERITY: 'SEVERITY',
162
+ /**
163
+ * The harm block method uses the probability score.
164
+ */
165
+ PROBABILITY: 'PROBABILITY'
166
+ };
167
+ /**
168
+ * Probability that a prompt or candidate matches a harm category.
169
+ * @public
170
+ */
171
+ const HarmProbability = {
172
+ /**
173
+ * Content has a negligible chance of being unsafe.
174
+ */
175
+ NEGLIGIBLE: 'NEGLIGIBLE',
176
+ /**
177
+ * Content has a low chance of being unsafe.
178
+ */
179
+ LOW: 'LOW',
180
+ /**
181
+ * Content has a medium chance of being unsafe.
182
+ */
183
+ MEDIUM: 'MEDIUM',
184
+ /**
185
+ * Content has a high chance of being unsafe.
186
+ */
187
+ HIGH: 'HIGH'
188
+ };
189
+ /**
190
+ * Harm severity levels.
191
+ * @public
192
+ */
193
+ const HarmSeverity = {
194
+ /**
195
+ * Negligible level of harm severity.
196
+ */
197
+ HARM_SEVERITY_NEGLIGIBLE: 'HARM_SEVERITY_NEGLIGIBLE',
198
+ /**
199
+ * Low level of harm severity.
200
+ */
201
+ HARM_SEVERITY_LOW: 'HARM_SEVERITY_LOW',
202
+ /**
203
+ * Medium level of harm severity.
204
+ */
205
+ HARM_SEVERITY_MEDIUM: 'HARM_SEVERITY_MEDIUM',
206
+ /**
207
+ * High level of harm severity.
208
+ */
209
+ HARM_SEVERITY_HIGH: 'HARM_SEVERITY_HIGH',
210
+ /**
211
+ * Harm severity is not supported.
212
+ *
213
+ * @remarks
214
+ * The GoogleAI backend does not support `HarmSeverity`, so this value is used as a fallback.
215
+ */
216
+ HARM_SEVERITY_UNSUPPORTED: 'HARM_SEVERITY_UNSUPPORTED'
217
+ };
218
+ /**
219
+ * Reason that a prompt was blocked.
220
+ * @public
221
+ */
222
+ const BlockReason = {
223
+ /**
224
+ * Content was blocked by safety settings.
225
+ */
226
+ SAFETY: 'SAFETY',
227
+ /**
228
+ * Content was blocked, but the reason is uncategorized.
229
+ */
230
+ OTHER: 'OTHER',
231
+ /**
232
+ * Content was blocked because it contained terms from the terminology blocklist.
233
+ */
234
+ BLOCKLIST: 'BLOCKLIST',
235
+ /**
236
+ * Content was blocked due to prohibited content.
237
+ */
238
+ PROHIBITED_CONTENT: 'PROHIBITED_CONTENT'
239
+ };
240
+ /**
241
+ * Reason that a candidate finished.
242
+ * @public
243
+ */
244
+ const FinishReason = {
245
+ /**
246
+ * Natural stop point of the model or provided stop sequence.
247
+ */
248
+ STOP: 'STOP',
249
+ /**
250
+ * The maximum number of tokens as specified in the request was reached.
251
+ */
252
+ MAX_TOKENS: 'MAX_TOKENS',
253
+ /**
254
+ * The candidate content was flagged for safety reasons.
255
+ */
256
+ SAFETY: 'SAFETY',
257
+ /**
258
+ * The candidate content was flagged for recitation reasons.
259
+ */
260
+ RECITATION: 'RECITATION',
261
+ /**
262
+ * Unknown reason.
263
+ */
264
+ OTHER: 'OTHER',
265
+ /**
266
+ * The candidate content contained forbidden terms.
267
+ */
268
+ BLOCKLIST: 'BLOCKLIST',
269
+ /**
270
+ * The candidate content potentially contained prohibited content.
271
+ */
272
+ PROHIBITED_CONTENT: 'PROHIBITED_CONTENT',
273
+ /**
274
+ * The candidate content potentially contained Sensitive Personally Identifiable Information (SPII).
275
+ */
276
+ SPII: 'SPII',
277
+ /**
278
+ * The function call generated by the model was invalid.
279
+ */
280
+ MALFORMED_FUNCTION_CALL: 'MALFORMED_FUNCTION_CALL'
281
+ };
282
+ /**
283
+ * @public
284
+ */
285
+ const FunctionCallingMode = {
286
+ /**
287
+ * Default model behavior; model decides to predict either a function call
288
+ * or a natural language response.
289
+ */
290
+ AUTO: 'AUTO',
291
+ /**
292
+ * Model is constrained to always predicting a function call only.
293
+ * If `allowed_function_names` is set, the predicted function call will be
294
+ * limited to any one of `allowed_function_names`, else the predicted
295
+ * function call will be any one of the provided `function_declarations`.
296
+ */
297
+ ANY: 'ANY',
298
+ /**
299
+ * Model will not predict any function call. Model behavior is same as when
300
+ * not passing any function declarations.
301
+ */
302
+ NONE: 'NONE'
303
+ };
304
+ /**
305
+ * Content part modality.
306
+ * @public
307
+ */
308
+ const Modality = {
309
+ /**
310
+ * Unspecified modality.
311
+ */
312
+ MODALITY_UNSPECIFIED: 'MODALITY_UNSPECIFIED',
313
+ /**
314
+ * Plain text.
315
+ */
316
+ TEXT: 'TEXT',
317
+ /**
318
+ * Image.
319
+ */
320
+ IMAGE: 'IMAGE',
321
+ /**
322
+ * Video.
323
+ */
324
+ VIDEO: 'VIDEO',
325
+ /**
326
+ * Audio.
327
+ */
328
+ AUDIO: 'AUDIO',
329
+ /**
330
+ * Document (for example, PDF).
331
+ */
332
+ DOCUMENT: 'DOCUMENT'
333
+ };
334
+ /**
335
+ * Generation modalities to be returned in generation responses.
336
+ *
337
+ * @beta
338
+ */
339
+ const ResponseModality = {
340
+ /**
341
+ * Text.
342
+ * @beta
343
+ */
344
+ TEXT: 'TEXT',
345
+ /**
346
+ * Image.
347
+ * @beta
348
+ */
349
+ IMAGE: 'IMAGE',
350
+ /**
351
+ * Audio.
352
+ * @beta
353
+ */
354
+ AUDIO: 'AUDIO'
355
+ };
356
+ /**
357
+ * Determines whether inference happens on-device or in-cloud.
358
+ *
359
+ * @remarks
360
+ * <b>PREFER_ON_DEVICE:</b> Attempt to make inference calls using an
361
+ * on-device model. If on-device inference is not available, the SDK
362
+ * will fall back to using a cloud-hosted model.
363
+ * <br/>
364
+ * <b>ONLY_ON_DEVICE:</b> Only attempt to make inference calls using an
365
+ * on-device model. The SDK will not fall back to a cloud-hosted model.
366
+ * If on-device inference is not available, inference methods will throw.
367
+ * <br/>
368
+ * <b>ONLY_IN_CLOUD:</b> Only attempt to make inference calls using a
369
+ * cloud-hosted model. The SDK will not fall back to an on-device model.
370
+ * <br/>
371
+ * <b>PREFER_IN_CLOUD:</b> Attempt to make inference calls to a
372
+ * cloud-hosted model. If not available, the SDK will fall back to an
373
+ * on-device model.
374
+ *
375
+ * @beta
376
+ */
377
+ const InferenceMode = {
378
+ 'PREFER_ON_DEVICE': 'prefer_on_device',
379
+ 'ONLY_ON_DEVICE': 'only_on_device',
380
+ 'ONLY_IN_CLOUD': 'only_in_cloud',
381
+ 'PREFER_IN_CLOUD': 'prefer_in_cloud'
382
+ };
383
+ /**
384
+ * Indicates whether inference happened on-device or in-cloud.
385
+ *
386
+ * @beta
387
+ */
388
+ const InferenceSource = {
389
+ 'ON_DEVICE': 'on_device',
390
+ 'IN_CLOUD': 'in_cloud'
391
+ };
392
+ /**
393
+ * Represents the result of the code execution.
394
+ *
395
+ * @public
396
+ */
397
+ const Outcome = {
398
+ UNSPECIFIED: 'OUTCOME_UNSPECIFIED',
399
+ OK: 'OUTCOME_OK',
400
+ FAILED: 'OUTCOME_FAILED',
401
+ DEADLINE_EXCEEDED: 'OUTCOME_DEADLINE_EXCEEDED'
402
+ };
403
+ /**
404
+ * The programming language of the code.
405
+ *
406
+ * @public
407
+ */
408
+ const Language = {
409
+ UNSPECIFIED: 'LANGUAGE_UNSPECIFIED',
410
+ PYTHON: 'PYTHON'
411
+ };
412
+ /**
413
+ * A preset that controls the model's "thinking" process. Use
414
+ * `ThinkingLevel.LOW` for faster responses on less complex tasks, and
415
+ * `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
416
+ *
417
+ * @public
418
+ */
419
+ const ThinkingLevel = {
420
+ MINIMAL: 'MINIMAL',
421
+ LOW: 'LOW',
422
+ MEDIUM: 'MEDIUM',
423
+ HIGH: 'HIGH'
424
+ };
425
+
426
+ /**
427
+ * @license
428
+ * Copyright 2024 Google LLC
429
+ *
430
+ * Licensed under the Apache License, Version 2.0 (the "License");
431
+ * you may not use this file except in compliance with the License.
432
+ * You may obtain a copy of the License at
433
+ *
434
+ * http://www.apache.org/licenses/LICENSE-2.0
435
+ *
436
+ * Unless required by applicable law or agreed to in writing, software
437
+ * distributed under the License is distributed on an "AS IS" BASIS,
438
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
439
+ * See the License for the specific language governing permissions and
440
+ * limitations under the License.
441
+ */
442
+ /**
443
+ * The status of a URL retrieval.
444
+ *
445
+ * @remarks
446
+ * <b>URL_RETRIEVAL_STATUS_UNSPECIFIED:</b> Unspecified retrieval status.
447
+ * <br/>
448
+ * <b>URL_RETRIEVAL_STATUS_SUCCESS:</b> The URL retrieval was successful.
449
+ * <br/>
450
+ * <b>URL_RETRIEVAL_STATUS_ERROR:</b> The URL retrieval failed.
451
+ * <br/>
452
+ * <b>URL_RETRIEVAL_STATUS_PAYWALL:</b> The URL retrieval failed because the content is behind a paywall.
453
+ * <br/>
454
+ * <b>URL_RETRIEVAL_STATUS_UNSAFE:</b> The URL retrieval failed because the content is unsafe.
455
+ * <br/>
456
+ *
457
+ * @public
458
+ */
459
+ const URLRetrievalStatus = {
460
+ /**
461
+ * Unspecified retrieval status.
462
+ */
463
+ URL_RETRIEVAL_STATUS_UNSPECIFIED: 'URL_RETRIEVAL_STATUS_UNSPECIFIED',
464
+ /**
465
+ * The URL retrieval was successful.
466
+ */
467
+ URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS',
468
+ /**
469
+ * The URL retrieval failed.
470
+ */
471
+ URL_RETRIEVAL_STATUS_ERROR: 'URL_RETRIEVAL_STATUS_ERROR',
472
+ /**
473
+ * The URL retrieval failed because the content is behind a paywall.
474
+ */
475
+ URL_RETRIEVAL_STATUS_PAYWALL: 'URL_RETRIEVAL_STATUS_PAYWALL',
476
+ /**
477
+ * The URL retrieval failed because the content is unsafe.
478
+ */
479
+ URL_RETRIEVAL_STATUS_UNSAFE: 'URL_RETRIEVAL_STATUS_UNSAFE'
480
+ };
481
+ /**
482
+ * The types of responses that can be returned by {@link LiveSession.receive}.
483
+ *
484
+ * @beta
485
+ */
486
+ const LiveResponseType = {
487
+ SERVER_CONTENT: 'serverContent',
488
+ TOOL_CALL: 'toolCall',
489
+ TOOL_CALL_CANCELLATION: 'toolCallCancellation',
490
+ GOING_AWAY_NOTICE: 'goingAwayNotice'
491
+ };
492
+
493
+ /**
494
+ * @license
495
+ * Copyright 2024 Google LLC
496
+ *
497
+ * Licensed under the Apache License, Version 2.0 (the "License");
498
+ * you may not use this file except in compliance with the License.
499
+ * You may obtain a copy of the License at
500
+ *
501
+ * http://www.apache.org/licenses/LICENSE-2.0
502
+ *
503
+ * Unless required by applicable law or agreed to in writing, software
504
+ * distributed under the License is distributed on an "AS IS" BASIS,
505
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
506
+ * See the License for the specific language governing permissions and
507
+ * limitations under the License.
508
+ */
509
+ /**
510
+ * Standardized error codes that {@link AIError} can have.
511
+ *
512
+ * @public
513
+ */
514
+ const AIErrorCode = {
515
+ /** A generic error occurred. */
516
+ ERROR: 'error',
517
+ /** An error occurred in a request. */
518
+ REQUEST_ERROR: 'request-error',
519
+ /** An error occurred in a response. */
520
+ RESPONSE_ERROR: 'response-error',
521
+ /** An error occurred while performing a fetch. */
522
+ FETCH_ERROR: 'fetch-error',
523
+ /** An error occurred because an operation was attempted on a closed session. */
524
+ SESSION_CLOSED: 'session-closed',
525
+ /** An error associated with a Content object. */
526
+ INVALID_CONTENT: 'invalid-content',
527
+ /** An error due to the Firebase API not being enabled in the Console. */
528
+ API_NOT_ENABLED: 'api-not-enabled',
529
+ /** An error due to invalid Schema input. */
530
+ INVALID_SCHEMA: 'invalid-schema',
531
+ /** An error occurred due to a missing Firebase API key. */
532
+ NO_API_KEY: 'no-api-key',
533
+ /** An error occurred due to a missing Firebase app ID. */
534
+ NO_APP_ID: 'no-app-id',
535
+ /** An error occurred due to a model name not being specified during initialization. */
536
+ NO_MODEL: 'no-model',
537
+ /** An error occurred due to a missing project ID. */
538
+ NO_PROJECT_ID: 'no-project-id',
539
+ /** An error occurred while parsing. */
540
+ PARSE_FAILED: 'parse-failed',
541
+ /** An error occurred due an attempt to use an unsupported feature. */
542
+ UNSUPPORTED: 'unsupported'
543
+ };
544
+
545
+ /**
546
+ * @license
547
+ * Copyright 2024 Google LLC
548
+ *
549
+ * Licensed under the Apache License, Version 2.0 (the "License");
550
+ * you may not use this file except in compliance with the License.
551
+ * You may obtain a copy of the License at
552
+ *
553
+ * http://www.apache.org/licenses/LICENSE-2.0
554
+ *
555
+ * Unless required by applicable law or agreed to in writing, software
556
+ * distributed under the License is distributed on an "AS IS" BASIS,
557
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
558
+ * See the License for the specific language governing permissions and
559
+ * limitations under the License.
560
+ */
561
+ /**
562
+ * Contains the list of OpenAPI data types
563
+ * as defined by the
564
+ * {@link https://swagger.io/docs/specification/data-models/data-types/ | OpenAPI specification}
565
+ * @public
566
+ */
567
+ const SchemaType = {
568
+ /** String type. */
569
+ STRING: 'string',
570
+ /** Number type. */
571
+ NUMBER: 'number',
572
+ /** Integer type. */
573
+ INTEGER: 'integer',
574
+ /** Boolean type. */
575
+ BOOLEAN: 'boolean',
576
+ /** Array type. */
577
+ ARRAY: 'array',
578
+ /** Object type. */
579
+ OBJECT: 'object'
580
+ };
581
+
582
+ /**
583
+ * @license
584
+ * Copyright 2025 Google LLC
585
+ *
586
+ * Licensed under the Apache License, Version 2.0 (the "License");
587
+ * you may not use this file except in compliance with the License.
588
+ * You may obtain a copy of the License at
589
+ *
590
+ * http://www.apache.org/licenses/LICENSE-2.0
591
+ *
592
+ * Unless required by applicable law or agreed to in writing, software
593
+ * distributed under the License is distributed on an "AS IS" BASIS,
594
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
595
+ * See the License for the specific language governing permissions and
596
+ * limitations under the License.
597
+ */
598
+ /**
599
+ * A filter level controlling how aggressively to filter sensitive content.
600
+ *
601
+ * Text prompts provided as inputs and images (generated or uploaded) through Imagen on Vertex AI
602
+ * are assessed against a list of safety filters, which include 'harmful categories' (for example,
603
+ * `violence`, `sexual`, `derogatory`, and `toxic`). This filter level controls how aggressively to
604
+ * filter out potentially harmful content from responses. See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
605
+ * and the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#safety-filters | Responsible AI and usage guidelines}
606
+ * for more details.
607
+ *
608
+ * @public
609
+ */
610
+ const ImagenSafetyFilterLevel = {
611
+ /**
612
+ * The most aggressive filtering level; most strict blocking.
613
+ */
614
+ BLOCK_LOW_AND_ABOVE: 'block_low_and_above',
615
+ /**
616
+ * Blocks some sensitive prompts and responses.
617
+ */
618
+ BLOCK_MEDIUM_AND_ABOVE: 'block_medium_and_above',
619
+ /**
620
+ * Blocks few sensitive prompts and responses.
621
+ */
622
+ BLOCK_ONLY_HIGH: 'block_only_high',
623
+ /**
624
+ * The least aggressive filtering level; blocks very few sensitive prompts and responses.
625
+ *
626
+ * Access to this feature is restricted and may require your case to be reviewed and approved by
627
+ * Cloud support.
628
+ */
629
+ BLOCK_NONE: 'block_none'
630
+ };
631
+ /**
632
+ * A filter level controlling whether generation of images containing people or faces is allowed.
633
+ *
634
+ * See the <a href="http://firebase.google.com/docs/vertex-ai/generate-images">personGeneration</a>
635
+ * documentation for more details.
636
+ *
637
+ * @public
638
+ */
639
+ const ImagenPersonFilterLevel = {
640
+ /**
641
+ * Disallow generation of images containing people or faces; images of people are filtered out.
642
+ */
643
+ BLOCK_ALL: 'dont_allow',
644
+ /**
645
+ * Allow generation of images containing adults only; images of children are filtered out.
646
+ *
647
+ * Generation of images containing people or faces may require your use case to be
648
+ * reviewed and approved by Cloud support; see the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen | Responsible AI and usage guidelines}
649
+ * for more details.
650
+ */
651
+ ALLOW_ADULT: 'allow_adult',
652
+ /**
653
+ * Allow generation of images containing adults only; images of children are filtered out.
654
+ *
655
+ * Generation of images containing people or faces may require your use case to be
656
+ * reviewed and approved by Cloud support; see the {@link https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen | Responsible AI and usage guidelines}
657
+ * for more details.
658
+ */
659
+ ALLOW_ALL: 'allow_all'
660
+ };
661
+ /**
662
+ * Aspect ratios for Imagen images.
663
+ *
664
+ * To specify an aspect ratio for generated images, set the `aspectRatio` property in your
665
+ * {@link ImagenGenerationConfig}.
666
+ *
667
+ * See the {@link http://firebase.google.com/docs/vertex-ai/generate-images | documentation }
668
+ * for more details and examples of the supported aspect ratios.
669
+ *
670
+ * @public
671
+ */
672
+ const ImagenAspectRatio = {
673
+ /**
674
+ * Square (1:1) aspect ratio.
675
+ */
676
+ 'SQUARE': '1:1',
677
+ /**
678
+ * Landscape (3:4) aspect ratio.
679
+ */
680
+ 'LANDSCAPE_3x4': '3:4',
681
+ /**
682
+ * Portrait (4:3) aspect ratio.
683
+ */
684
+ 'PORTRAIT_4x3': '4:3',
685
+ /**
686
+ * Landscape (16:9) aspect ratio.
687
+ */
688
+ 'LANDSCAPE_16x9': '16:9',
689
+ /**
690
+ * Portrait (9:16) aspect ratio.
691
+ */
692
+ 'PORTRAIT_9x16': '9:16'
693
+ };
694
+
695
+ /**
696
+ * @license
697
+ * Copyright 2024 Google LLC
698
+ *
699
+ * Licensed under the Apache License, Version 2.0 (the "License");
700
+ * you may not use this file except in compliance with the License.
701
+ * You may obtain a copy of the License at
702
+ *
703
+ * http://www.apache.org/licenses/LICENSE-2.0
704
+ *
705
+ * Unless required by applicable law or agreed to in writing, software
706
+ * distributed under the License is distributed on an "AS IS" BASIS,
707
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
708
+ * See the License for the specific language governing permissions and
709
+ * limitations under the License.
710
+ */
711
+ /**
712
+ * An enum-like object containing constants that represent the supported backends
713
+ * for the Firebase AI SDK.
714
+ * This determines which backend service (Vertex AI Gemini API or Gemini Developer API)
715
+ * the SDK will communicate with.
716
+ *
717
+ * These values are assigned to the `backendType` property within the specific backend
718
+ * configuration objects ({@link GoogleAIBackend} or {@link VertexAIBackend}) to identify
719
+ * which service to target.
720
+ *
721
+ * @public
722
+ */
723
+ const BackendType = {
724
+ /**
725
+ * Identifies the backend service for the Vertex AI Gemini API provided through Google Cloud.
726
+ * Use this constant when creating a {@link VertexAIBackend} configuration.
727
+ */
728
+ VERTEX_AI: 'VERTEX_AI',
729
+ /**
730
+ * Identifies the backend service for the Gemini Developer API ({@link https://ai.google/ | Google AI}).
731
+ * Use this constant when creating a {@link GoogleAIBackend} configuration.
732
+ */
733
+ GOOGLE_AI: 'GOOGLE_AI'
734
+ }; // Using 'as const' makes the string values literal types
735
+
736
+ /**
737
+ * @license
738
+ * Copyright 2025 Google LLC
739
+ *
740
+ * Licensed under the Apache License, Version 2.0 (the "License");
741
+ * you may not use this file except in compliance with the License.
742
+ * You may obtain a copy of the License at
743
+ *
744
+ * http://www.apache.org/licenses/LICENSE-2.0
745
+ *
746
+ * Unless required by applicable law or agreed to in writing, software
747
+ * distributed under the License is distributed on an "AS IS" BASIS,
748
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
749
+ * See the License for the specific language governing permissions and
750
+ * limitations under the License.
751
+ */
752
+ /**
753
+ * Abstract base class representing the configuration for an AI service backend.
754
+ * This class should not be instantiated directly. Use its subclasses; {@link GoogleAIBackend} for
755
+ * the Gemini Developer API (via {@link https://ai.google/ | Google AI}), and
756
+ * {@link VertexAIBackend} for the Vertex AI Gemini API.
757
+ *
758
+ * @public
759
+ */
760
+ class Backend {
761
+ /**
762
+ * Protected constructor for use by subclasses.
763
+ * @param type - The backend type.
764
+ */
765
+ constructor(type) {
766
+ this.backendType = type;
767
+ }
768
+ }
769
+ /**
770
+ * Configuration class for the Gemini Developer API.
771
+ *
772
+ * Use this with {@link AIOptions} when initializing the AI service via
773
+ * {@link getAI | getAI()} to specify the Gemini Developer API as the backend.
774
+ *
775
+ * @public
776
+ */
777
+ class GoogleAIBackend extends Backend {
778
+ /**
779
+ * Creates a configuration object for the Gemini Developer API backend.
780
+ */
781
+ constructor() {
782
+ super(BackendType.GOOGLE_AI);
783
+ }
784
+ /**
785
+ * @internal
786
+ */
787
+ _getModelPath(project, model) {
788
+ return `/${DEFAULT_API_VERSION}/projects/${project}/${model}`;
789
+ }
790
+ /**
791
+ * @internal
792
+ */
793
+ _getTemplatePath(project, templateId) {
794
+ return `/${DEFAULT_API_VERSION}/projects/${project}/templates/${templateId}`;
795
+ }
796
+ }
797
+ /**
798
+ * Configuration class for the Vertex AI Gemini API.
799
+ *
800
+ * Use this with {@link AIOptions} when initializing the AI service via
801
+ * {@link getAI | getAI()} to specify the Vertex AI Gemini API as the backend.
802
+ *
803
+ * @public
804
+ */
805
+ class VertexAIBackend extends Backend {
806
+ /**
807
+ * Creates a configuration object for the Vertex AI backend.
808
+ *
809
+ * @param location - The region identifier, defaulting to `us-central1`;
810
+ * see {@link https://firebase.google.com/docs/vertex-ai/locations#available-locations | Vertex AI locations}
811
+ * for a list of supported locations.
812
+ */
813
+ constructor(location = DEFAULT_LOCATION) {
814
+ super(BackendType.VERTEX_AI);
815
+ if (!location) {
816
+ this.location = DEFAULT_LOCATION;
817
+ }
818
+ else {
819
+ this.location = location;
820
+ }
821
+ }
822
+ /**
823
+ * @internal
824
+ */
825
+ _getModelPath(project, model) {
826
+ return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/${model}`;
827
+ }
828
+ /**
829
+ * @internal
830
+ */
831
+ _getTemplatePath(project, templateId) {
832
+ return `/${DEFAULT_API_VERSION}/projects/${project}/locations/${this.location}/templates/${templateId}`;
833
+ }
834
+ }
835
+
836
+ /**
837
+ * @license
838
+ * Copyright 2025 Google LLC
839
+ *
840
+ * Licensed under the Apache License, Version 2.0 (the "License");
841
+ * you may not use this file except in compliance with the License.
842
+ * You may obtain a copy of the License at
843
+ *
844
+ * http://www.apache.org/licenses/LICENSE-2.0
845
+ *
846
+ * Unless required by applicable law or agreed to in writing, software
847
+ * distributed under the License is distributed on an "AS IS" BASIS,
848
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
849
+ * See the License for the specific language governing permissions and
850
+ * limitations under the License.
851
+ */
852
+ /**
853
+ * Encodes a {@link Backend} into a string that will be used to uniquely identify {@link AI}
854
+ * instances by backend type.
855
+ *
856
+ * @internal
857
+ */
858
+ function encodeInstanceIdentifier(backend) {
859
+ if (backend instanceof GoogleAIBackend) {
860
+ return `${AI_TYPE}/googleai`;
861
+ }
862
+ else if (backend instanceof VertexAIBackend) {
863
+ return `${AI_TYPE}/vertexai/${backend.location}`;
864
+ }
865
+ else {
866
+ throw new AIError(AIErrorCode.ERROR, `Invalid backend: ${JSON.stringify(backend.backendType)}`);
867
+ }
868
+ }
869
+ /**
870
+ * Decodes an instance identifier string into a {@link Backend}.
871
+ *
872
+ * @internal
873
+ */
874
+ function decodeInstanceIdentifier(instanceIdentifier) {
875
+ const identifierParts = instanceIdentifier.split('/');
876
+ if (identifierParts[0] !== AI_TYPE) {
877
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown prefix '${identifierParts[0]}'`);
878
+ }
879
+ const backendType = identifierParts[1];
880
+ switch (backendType) {
881
+ case 'vertexai':
882
+ const location = identifierParts[2];
883
+ if (!location) {
884
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier, unknown location '${instanceIdentifier}'`);
885
+ }
886
+ return new VertexAIBackend(location);
887
+ case 'googleai':
888
+ return new GoogleAIBackend();
889
+ default:
890
+ throw new AIError(AIErrorCode.ERROR, `Invalid instance identifier string: '${instanceIdentifier}'`);
891
+ }
892
+ }
893
+
894
+ /**
895
+ * @license
896
+ * Copyright 2024 Google LLC
897
+ *
898
+ * Licensed under the Apache License, Version 2.0 (the "License");
899
+ * you may not use this file except in compliance with the License.
900
+ * You may obtain a copy of the License at
901
+ *
902
+ * http://www.apache.org/licenses/LICENSE-2.0
903
+ *
904
+ * Unless required by applicable law or agreed to in writing, software
905
+ * distributed under the License is distributed on an "AS IS" BASIS,
906
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
907
+ * See the License for the specific language governing permissions and
908
+ * limitations under the License.
909
+ */
910
+ const logger = new Logger('@firebase/vertexai');
911
+
912
+ /**
913
+ * @internal
914
+ */
915
+ var Availability;
916
+ (function (Availability) {
917
+ Availability["UNAVAILABLE"] = "unavailable";
918
+ Availability["DOWNLOADABLE"] = "downloadable";
919
+ Availability["DOWNLOADING"] = "downloading";
920
+ Availability["AVAILABLE"] = "available";
921
+ })(Availability || (Availability = {}));
922
+
923
+ /**
924
+ * @license
925
+ * Copyright 2025 Google LLC
926
+ *
927
+ * Licensed under the Apache License, Version 2.0 (the "License");
928
+ * you may not use this file except in compliance with the License.
929
+ * You may obtain a copy of the License at
930
+ *
931
+ * http://www.apache.org/licenses/LICENSE-2.0
932
+ *
933
+ * Unless required by applicable law or agreed to in writing, software
934
+ * distributed under the License is distributed on an "AS IS" BASIS,
935
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
936
+ * See the License for the specific language governing permissions and
937
+ * limitations under the License.
938
+ */
939
+ // Defaults to support image inputs for convenience.
940
+ const defaultExpectedInputs = [{ type: 'image' }];
941
+ /**
942
+ * Defines an inference "backend" that uses Chrome's on-device model,
943
+ * and encapsulates logic for detecting when on-device inference is
944
+ * possible.
945
+ */
946
+ class ChromeAdapterImpl {
947
+ constructor(languageModelProvider, mode, onDeviceParams) {
948
+ this.languageModelProvider = languageModelProvider;
949
+ this.mode = mode;
950
+ this.isDownloading = false;
951
+ this.onDeviceParams = {
952
+ createOptions: {
953
+ expectedInputs: defaultExpectedInputs
954
+ }
955
+ };
956
+ if (onDeviceParams) {
957
+ this.onDeviceParams = onDeviceParams;
958
+ if (!this.onDeviceParams.createOptions) {
959
+ this.onDeviceParams.createOptions = {
960
+ expectedInputs: defaultExpectedInputs
961
+ };
962
+ }
963
+ else if (!this.onDeviceParams.createOptions.expectedInputs) {
964
+ this.onDeviceParams.createOptions.expectedInputs =
965
+ defaultExpectedInputs;
966
+ }
967
+ }
968
+ }
969
+ /**
970
+ * Checks if a given request can be made on-device.
971
+ *
972
+ * Encapsulates a few concerns:
973
+ * the mode
974
+ * API existence
975
+ * prompt formatting
976
+ * model availability, including triggering download if necessary
977
+ *
978
+ *
979
+ * Pros: callers needn't be concerned with details of on-device availability.</p>
980
+ * Cons: this method spans a few concerns and splits request validation from usage.
981
+ * If instance variables weren't already part of the API, we could consider a better
982
+ * separation of concerns.
983
+ */
984
+ async isAvailable(request) {
985
+ if (!this.mode) {
986
+ logger.debug(`On-device inference unavailable because mode is undefined.`);
987
+ return false;
988
+ }
989
+ if (this.mode === InferenceMode.ONLY_IN_CLOUD) {
990
+ logger.debug(`On-device inference unavailable because mode is "only_in_cloud".`);
991
+ return false;
992
+ }
993
+ // Triggers out-of-band download so model will eventually become available.
994
+ const availability = await this.downloadIfAvailable();
995
+ if (this.mode === InferenceMode.ONLY_ON_DEVICE) {
996
+ // If it will never be available due to API inavailability, throw.
997
+ if (availability === Availability.UNAVAILABLE) {
998
+ throw new AIError(AIErrorCode.API_NOT_ENABLED, 'Local LanguageModel API not available in this environment.');
999
+ }
1000
+ else if (availability === Availability.DOWNLOADABLE ||
1001
+ availability === Availability.DOWNLOADING) {
1002
+ // TODO(chholland): Better user experience during download - progress?
1003
+ logger.debug(`Waiting for download of LanguageModel to complete.`);
1004
+ await this.downloadPromise;
1005
+ return true;
1006
+ }
1007
+ return true;
1008
+ }
1009
+ // Applies prefer_on_device logic.
1010
+ if (availability !== Availability.AVAILABLE) {
1011
+ logger.debug(`On-device inference unavailable because availability is "${availability}".`);
1012
+ return false;
1013
+ }
1014
+ if (!ChromeAdapterImpl.isOnDeviceRequest(request)) {
1015
+ logger.debug(`On-device inference unavailable because request is incompatible.`);
1016
+ return false;
1017
+ }
1018
+ return true;
1019
+ }
1020
+ /**
1021
+ * Generates content on device.
1022
+ *
1023
+ * @remarks
1024
+ * This is comparable to {@link GenerativeModel.generateContent} for generating content in
1025
+ * Cloud.
1026
+ * @param request - a standard Firebase AI {@link GenerateContentRequest}
1027
+ * @returns {@link Response}, so we can reuse common response formatting.
1028
+ */
1029
+ async generateContent(request) {
1030
+ const session = await this.createSession();
1031
+ const contents = await Promise.all(request.contents.map(ChromeAdapterImpl.toLanguageModelMessage));
1032
+ const text = await session.prompt(contents, this.onDeviceParams.promptOptions);
1033
+ return ChromeAdapterImpl.toResponse(text);
1034
+ }
1035
+ /**
1036
+ * Generates content stream on device.
1037
+ *
1038
+ * @remarks
1039
+ * This is comparable to {@link GenerativeModel.generateContentStream} for generating content in
1040
+ * Cloud.
1041
+ * @param request - a standard Firebase AI {@link GenerateContentRequest}
1042
+ * @returns {@link Response}, so we can reuse common response formatting.
1043
+ */
1044
+ async generateContentStream(request) {
1045
+ const session = await this.createSession();
1046
+ const contents = await Promise.all(request.contents.map(ChromeAdapterImpl.toLanguageModelMessage));
1047
+ const stream = session.promptStreaming(contents, this.onDeviceParams.promptOptions);
1048
+ return ChromeAdapterImpl.toStreamResponse(stream);
1049
+ }
1050
+ async countTokens(_request) {
1051
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'Count Tokens is not yet available for on-device model.');
1052
+ }
1053
+ /**
1054
+ * Asserts inference for the given request can be performed by an on-device model.
1055
+ */
1056
+ static isOnDeviceRequest(request) {
1057
+ // Returns false if the prompt is empty.
1058
+ if (request.contents.length === 0) {
1059
+ logger.debug('Empty prompt rejected for on-device inference.');
1060
+ return false;
1061
+ }
1062
+ for (const content of request.contents) {
1063
+ if (content.role === 'function') {
1064
+ logger.debug(`"Function" role rejected for on-device inference.`);
1065
+ return false;
1066
+ }
1067
+ // Returns false if request contains an image with an unsupported mime type.
1068
+ for (const part of content.parts) {
1069
+ if (part.inlineData &&
1070
+ ChromeAdapterImpl.SUPPORTED_MIME_TYPES.indexOf(part.inlineData.mimeType) === -1) {
1071
+ logger.debug(`Unsupported mime type "${part.inlineData.mimeType}" rejected for on-device inference.`);
1072
+ return false;
1073
+ }
1074
+ }
1075
+ }
1076
+ return true;
1077
+ }
1078
+ /**
1079
+ * Encapsulates logic to get availability and download a model if one is downloadable.
1080
+ */
1081
+ async downloadIfAvailable() {
1082
+ const availability = await this.languageModelProvider?.availability(this.onDeviceParams.createOptions);
1083
+ if (availability === Availability.DOWNLOADABLE) {
1084
+ this.download();
1085
+ }
1086
+ return availability;
1087
+ }
1088
+ /**
1089
+ * Triggers out-of-band download of an on-device model.
1090
+ *
1091
+ * Chrome only downloads models as needed. Chrome knows a model is needed when code calls
1092
+ * LanguageModel.create.
1093
+ *
1094
+ * Since Chrome manages the download, the SDK can only avoid redundant download requests by
1095
+ * tracking if a download has previously been requested.
1096
+ */
1097
+ download() {
1098
+ if (this.isDownloading) {
1099
+ return;
1100
+ }
1101
+ this.isDownloading = true;
1102
+ this.downloadPromise = this.languageModelProvider
1103
+ ?.create(this.onDeviceParams.createOptions)
1104
+ .finally(() => {
1105
+ this.isDownloading = false;
1106
+ });
1107
+ }
1108
+ /**
1109
+ * Converts Firebase AI {@link Content} object to a Chrome {@link LanguageModelMessage} object.
1110
+ */
1111
+ static async toLanguageModelMessage(content) {
1112
+ const languageModelMessageContents = await Promise.all(content.parts.map(ChromeAdapterImpl.toLanguageModelMessageContent));
1113
+ return {
1114
+ role: ChromeAdapterImpl.toLanguageModelMessageRole(content.role),
1115
+ content: languageModelMessageContents
1116
+ };
1117
+ }
1118
+ /**
1119
+ * Converts a Firebase AI Part object to a Chrome LanguageModelMessageContent object.
1120
+ */
1121
+ static async toLanguageModelMessageContent(part) {
1122
+ if (part.text) {
1123
+ return {
1124
+ type: 'text',
1125
+ value: part.text
1126
+ };
1127
+ }
1128
+ else if (part.inlineData) {
1129
+ const formattedImageContent = await fetch(`data:${part.inlineData.mimeType};base64,${part.inlineData.data}`);
1130
+ const imageBlob = await formattedImageContent.blob();
1131
+ const imageBitmap = await createImageBitmap(imageBlob);
1132
+ return {
1133
+ type: 'image',
1134
+ value: imageBitmap
1135
+ };
1136
+ }
1137
+ throw new AIError(AIErrorCode.REQUEST_ERROR, `Processing of this Part type is not currently supported.`);
1138
+ }
1139
+ /**
1140
+ * Converts a Firebase AI {@link Role} string to a {@link LanguageModelMessageRole} string.
1141
+ */
1142
+ static toLanguageModelMessageRole(role) {
1143
+ // Assumes 'function' rule has been filtered by isOnDeviceRequest
1144
+ return role === 'model' ? 'assistant' : 'user';
1145
+ }
1146
+ /**
1147
+ * Abstracts Chrome session creation.
1148
+ *
1149
+ * Chrome uses a multi-turn session for all inference. Firebase AI uses single-turn for all
1150
+ * inference. To map the Firebase AI API to Chrome's API, the SDK creates a new session for all
1151
+ * inference.
1152
+ *
1153
+ * Chrome will remove a model from memory if it's no longer in use, so this method ensures a
1154
+ * new session is created before an old session is destroyed.
1155
+ */
1156
+ async createSession() {
1157
+ if (!this.languageModelProvider) {
1158
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Chrome AI requested for unsupported browser version.');
1159
+ }
1160
+ const newSession = await this.languageModelProvider.create(this.onDeviceParams.createOptions);
1161
+ if (this.oldSession) {
1162
+ this.oldSession.destroy();
1163
+ }
1164
+ // Holds session reference, so model isn't unloaded from memory.
1165
+ this.oldSession = newSession;
1166
+ return newSession;
1167
+ }
1168
+ /**
1169
+ * Formats string returned by Chrome as a {@link Response} returned by Firebase AI.
1170
+ */
1171
+ static toResponse(text) {
1172
+ return {
1173
+ json: async () => ({
1174
+ candidates: [
1175
+ {
1176
+ content: {
1177
+ parts: [{ text }]
1178
+ }
1179
+ }
1180
+ ]
1181
+ })
1182
+ };
1183
+ }
1184
+ /**
1185
+ * Formats string stream returned by Chrome as SSE returned by Firebase AI.
1186
+ */
1187
+ static toStreamResponse(stream) {
1188
+ const encoder = new TextEncoder();
1189
+ return {
1190
+ body: stream.pipeThrough(new TransformStream({
1191
+ transform(chunk, controller) {
1192
+ const json = JSON.stringify({
1193
+ candidates: [
1194
+ {
1195
+ content: {
1196
+ role: 'model',
1197
+ parts: [{ text: chunk }]
1198
+ }
1199
+ }
1200
+ ]
1201
+ });
1202
+ controller.enqueue(encoder.encode(`data: ${json}\n\n`));
1203
+ }
1204
+ }))
1205
+ };
1206
+ }
1207
+ }
1208
+ // Visible for testing
1209
+ ChromeAdapterImpl.SUPPORTED_MIME_TYPES = ['image/jpeg', 'image/png'];
1210
+ /**
1211
+ * Creates a ChromeAdapterImpl on demand.
1212
+ */
1213
+ function chromeAdapterFactory(mode, window, params) {
1214
+ // Do not initialize a ChromeAdapter if we are not in hybrid mode.
1215
+ if (typeof window !== 'undefined' && mode) {
1216
+ return new ChromeAdapterImpl(window.LanguageModel, mode, params);
1217
+ }
1218
+ }
1219
+
1220
+ /**
1221
+ * @license
1222
+ * Copyright 2024 Google LLC
1223
+ *
1224
+ * Licensed under the Apache License, Version 2.0 (the "License");
1225
+ * you may not use this file except in compliance with the License.
1226
+ * You may obtain a copy of the License at
1227
+ *
1228
+ * http://www.apache.org/licenses/LICENSE-2.0
1229
+ *
1230
+ * Unless required by applicable law or agreed to in writing, software
1231
+ * distributed under the License is distributed on an "AS IS" BASIS,
1232
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233
+ * See the License for the specific language governing permissions and
1234
+ * limitations under the License.
1235
+ */
1236
+ class AIService {
1237
+ constructor(app, backend, authProvider, appCheckProvider, chromeAdapterFactory) {
1238
+ this.app = app;
1239
+ this.backend = backend;
1240
+ this.chromeAdapterFactory = chromeAdapterFactory;
1241
+ const appCheck = appCheckProvider?.getImmediate({ optional: true });
1242
+ const auth = authProvider?.getImmediate({ optional: true });
1243
+ this.auth = auth || null;
1244
+ this.appCheck = appCheck || null;
1245
+ if (backend instanceof VertexAIBackend) {
1246
+ this.location = backend.location;
1247
+ }
1248
+ else {
1249
+ this.location = '';
1250
+ }
1251
+ }
1252
+ _delete() {
1253
+ return Promise.resolve();
1254
+ }
1255
+ set options(optionsToSet) {
1256
+ this._options = optionsToSet;
1257
+ }
1258
+ get options() {
1259
+ return this._options;
1260
+ }
1261
+ }
1262
+
1263
+ /**
1264
+ * @license
1265
+ * Copyright 2025 Google LLC
1266
+ *
1267
+ * Licensed under the Apache License, Version 2.0 (the "License");
1268
+ * you may not use this file except in compliance with the License.
1269
+ * You may obtain a copy of the License at
1270
+ *
1271
+ * http://www.apache.org/licenses/LICENSE-2.0
1272
+ *
1273
+ * Unless required by applicable law or agreed to in writing, software
1274
+ * distributed under the License is distributed on an "AS IS" BASIS,
1275
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1276
+ * See the License for the specific language governing permissions and
1277
+ * limitations under the License.
1278
+ */
1279
+ function factory(container, { instanceIdentifier }) {
1280
+ if (!instanceIdentifier) {
1281
+ throw new AIError(AIErrorCode.ERROR, 'AIService instance identifier is undefined.');
1282
+ }
1283
+ const backend = decodeInstanceIdentifier(instanceIdentifier);
1284
+ // getImmediate for FirebaseApp will always succeed
1285
+ const app = container.getProvider('app').getImmediate();
1286
+ const auth = container.getProvider('auth-internal');
1287
+ const appCheckProvider = container.getProvider('app-check-internal');
1288
+ return new AIService(app, backend, auth, appCheckProvider, chromeAdapterFactory);
1289
+ }
1290
+
1291
+ /**
1292
+ * @license
1293
+ * Copyright 2025 Google LLC
1294
+ *
1295
+ * Licensed under the Apache License, Version 2.0 (the "License");
1296
+ * you may not use this file except in compliance with the License.
1297
+ * You may obtain a copy of the License at
1298
+ *
1299
+ * http://www.apache.org/licenses/LICENSE-2.0
1300
+ *
1301
+ * Unless required by applicable law or agreed to in writing, software
1302
+ * distributed under the License is distributed on an "AS IS" BASIS,
1303
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1304
+ * See the License for the specific language governing permissions and
1305
+ * limitations under the License.
1306
+ */
1307
+ /**
1308
+ * Initializes an {@link ApiSettings} object from an {@link AI} instance.
1309
+ *
1310
+ * If this is a Server App, the {@link ApiSettings} object's `getAppCheckToken()` will resolve
1311
+ * with the `FirebaseServerAppSettings.appCheckToken`, instead of requiring that an App Check
1312
+ * instance is initialized.
1313
+ */
1314
+ function initApiSettings(ai) {
1315
+ if (!ai.app?.options?.apiKey) {
1316
+ throw new AIError(AIErrorCode.NO_API_KEY, `The "apiKey" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid API key.`);
1317
+ }
1318
+ else if (!ai.app?.options?.projectId) {
1319
+ throw new AIError(AIErrorCode.NO_PROJECT_ID, `The "projectId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid project ID.`);
1320
+ }
1321
+ else if (!ai.app?.options?.appId) {
1322
+ throw new AIError(AIErrorCode.NO_APP_ID, `The "appId" field is empty in the local Firebase config. Firebase AI requires this field to contain a valid app ID.`);
1323
+ }
1324
+ const apiSettings = {
1325
+ apiKey: ai.app.options.apiKey,
1326
+ project: ai.app.options.projectId,
1327
+ appId: ai.app.options.appId,
1328
+ automaticDataCollectionEnabled: ai.app.automaticDataCollectionEnabled,
1329
+ location: ai.location,
1330
+ backend: ai.backend
1331
+ };
1332
+ if (_isFirebaseServerApp(ai.app) && ai.app.settings.appCheckToken) {
1333
+ const token = ai.app.settings.appCheckToken;
1334
+ apiSettings.getAppCheckToken = () => {
1335
+ return Promise.resolve({ token });
1336
+ };
1337
+ }
1338
+ else if (ai.appCheck) {
1339
+ if (ai.options?.useLimitedUseAppCheckTokens) {
1340
+ apiSettings.getAppCheckToken = () => ai.appCheck.getLimitedUseToken();
1341
+ }
1342
+ else {
1343
+ apiSettings.getAppCheckToken = () => ai.appCheck.getToken();
1344
+ }
1345
+ }
1346
+ if (ai.auth) {
1347
+ apiSettings.getAuthToken = () => ai.auth.getToken();
1348
+ }
1349
+ return apiSettings;
1350
+ }
1351
+
1352
+ /**
1353
+ * @license
1354
+ * Copyright 2025 Google LLC
1355
+ *
1356
+ * Licensed under the Apache License, Version 2.0 (the "License");
1357
+ * you may not use this file except in compliance with the License.
1358
+ * You may obtain a copy of the License at
1359
+ *
1360
+ * http://www.apache.org/licenses/LICENSE-2.0
1361
+ *
1362
+ * Unless required by applicable law or agreed to in writing, software
1363
+ * distributed under the License is distributed on an "AS IS" BASIS,
1364
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365
+ * See the License for the specific language governing permissions and
1366
+ * limitations under the License.
1367
+ */
1368
+ /**
1369
+ * Base class for Firebase AI model APIs.
1370
+ *
1371
+ * Instances of this class are associated with a specific Firebase AI {@link Backend}
1372
+ * and provide methods for interacting with the configured generative model.
1373
+ *
1374
+ * @public
1375
+ */
1376
+ class AIModel {
1377
+ /**
1378
+ * Constructs a new instance of the {@link AIModel} class.
1379
+ *
1380
+ * This constructor should only be called from subclasses that provide
1381
+ * a model API.
1382
+ *
1383
+ * @param ai - an {@link AI} instance.
1384
+ * @param modelName - The name of the model being used. It can be in one of the following formats:
1385
+ * - `my-model` (short name, will resolve to `publishers/google/models/my-model`)
1386
+ * - `models/my-model` (will resolve to `publishers/google/models/my-model`)
1387
+ * - `publishers/my-publisher/models/my-model` (fully qualified model name)
1388
+ *
1389
+ * @throws If the `apiKey` or `projectId` fields are missing in your
1390
+ * Firebase config.
1391
+ *
1392
+ * @internal
1393
+ */
1394
+ constructor(ai, modelName) {
1395
+ this._apiSettings = initApiSettings(ai);
1396
+ this.model = AIModel.normalizeModelName(modelName, this._apiSettings.backend.backendType);
1397
+ }
1398
+ /**
1399
+ * Normalizes the given model name to a fully qualified model resource name.
1400
+ *
1401
+ * @param modelName - The model name to normalize.
1402
+ * @returns The fully qualified model resource name.
1403
+ *
1404
+ * @internal
1405
+ */
1406
+ static normalizeModelName(modelName, backendType) {
1407
+ if (backendType === BackendType.GOOGLE_AI) {
1408
+ return AIModel.normalizeGoogleAIModelName(modelName);
1409
+ }
1410
+ else {
1411
+ return AIModel.normalizeVertexAIModelName(modelName);
1412
+ }
1413
+ }
1414
+ /**
1415
+ * @internal
1416
+ */
1417
+ static normalizeGoogleAIModelName(modelName) {
1418
+ return `models/${modelName}`;
1419
+ }
1420
+ /**
1421
+ * @internal
1422
+ */
1423
+ static normalizeVertexAIModelName(modelName) {
1424
+ let model;
1425
+ if (modelName.includes('/')) {
1426
+ if (modelName.startsWith('models/')) {
1427
+ // Add 'publishers/google' if the user is only passing in 'models/model-name'.
1428
+ model = `publishers/google/${modelName}`;
1429
+ }
1430
+ else {
1431
+ // Any other custom format (e.g. tuned models) must be passed in correctly.
1432
+ model = modelName;
1433
+ }
1434
+ }
1435
+ else {
1436
+ // If path is not included, assume it's a non-tuned model.
1437
+ model = `publishers/google/models/${modelName}`;
1438
+ }
1439
+ return model;
1440
+ }
1441
+ }
1442
+
1443
+ /**
1444
+ * @license
1445
+ * Copyright 2025 Google LLC
1446
+ *
1447
+ * Licensed under the Apache License, Version 2.0 (the "License");
1448
+ * you may not use this file except in compliance with the License.
1449
+ * You may obtain a copy of the License at
1450
+ *
1451
+ * http://www.apache.org/licenses/LICENSE-2.0
1452
+ *
1453
+ * Unless required by applicable law or agreed to in writing, software
1454
+ * distributed under the License is distributed on an "AS IS" BASIS,
1455
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1456
+ * See the License for the specific language governing permissions and
1457
+ * limitations under the License.
1458
+ */
1459
+ const TIMEOUT_EXPIRED_MESSAGE = 'Timeout has expired.';
1460
+ const ABORT_ERROR_NAME = 'AbortError';
1461
+ class RequestURL {
1462
+ constructor(params) {
1463
+ this.params = params;
1464
+ }
1465
+ toString() {
1466
+ const url = new URL(this.baseUrl); // Throws if the URL is invalid
1467
+ url.pathname = this.pathname;
1468
+ url.search = this.queryParams.toString();
1469
+ return url.toString();
1470
+ }
1471
+ get pathname() {
1472
+ // We need to construct a different URL if the request is for server side prompt templates,
1473
+ // since the URL patterns are different. Server side prompt templates expect a templateId
1474
+ // instead of a model name.
1475
+ if (this.params.templateId) {
1476
+ return `${this.params.apiSettings.backend._getTemplatePath(this.params.apiSettings.project, this.params.templateId)}:${this.params.task}`;
1477
+ }
1478
+ else {
1479
+ return `${this.params.apiSettings.backend._getModelPath(this.params.apiSettings.project, this.params.model)}:${this.params.task}`;
1480
+ }
1481
+ }
1482
+ get baseUrl() {
1483
+ return (this.params.singleRequestOptions?.baseUrl ?? `https://${DEFAULT_DOMAIN}`);
1484
+ }
1485
+ get queryParams() {
1486
+ const params = new URLSearchParams();
1487
+ if (this.params.stream) {
1488
+ params.set('alt', 'sse');
1489
+ }
1490
+ return params;
1491
+ }
1492
+ }
1493
+ class WebSocketUrl {
1494
+ constructor(apiSettings) {
1495
+ this.apiSettings = apiSettings;
1496
+ }
1497
+ toString() {
1498
+ const url = new URL(`wss://${DEFAULT_DOMAIN}`);
1499
+ url.pathname = this.pathname;
1500
+ const queryParams = new URLSearchParams();
1501
+ queryParams.set('key', this.apiSettings.apiKey);
1502
+ url.search = queryParams.toString();
1503
+ return url.toString();
1504
+ }
1505
+ get pathname() {
1506
+ if (this.apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
1507
+ return 'ws/google.firebase.vertexai.v1beta.GenerativeService/BidiGenerateContent';
1508
+ }
1509
+ else {
1510
+ return `ws/google.firebase.vertexai.v1beta.LlmBidiService/BidiGenerateContent/locations/${this.apiSettings.location}`;
1511
+ }
1512
+ }
1513
+ }
1514
+ /**
1515
+ * Log language and "fire/version" to x-goog-api-client
1516
+ */
1517
+ function getClientHeaders(url) {
1518
+ const loggingTags = [];
1519
+ loggingTags.push(`${LANGUAGE_TAG}/${PACKAGE_VERSION}`);
1520
+ loggingTags.push(`fire/${PACKAGE_VERSION}`);
1521
+ /**
1522
+ * No call would be made if ONLY_ON_DEVICE.
1523
+ * ONLY_IN_CLOUD does not indicate an intention to use hybrid.
1524
+ */
1525
+ if (url.params.apiSettings.inferenceMode === InferenceMode.PREFER_ON_DEVICE ||
1526
+ url.params.apiSettings.inferenceMode === InferenceMode.PREFER_IN_CLOUD) {
1527
+ // No version
1528
+ loggingTags.push(HYBRID_TAG);
1529
+ }
1530
+ return loggingTags.join(' ');
1531
+ }
1532
+ async function getHeaders(url) {
1533
+ const headers = new Headers();
1534
+ headers.append('Content-Type', 'application/json');
1535
+ headers.append('x-goog-api-client', getClientHeaders(url));
1536
+ headers.append('x-goog-api-key', url.params.apiSettings.apiKey);
1537
+ if (url.params.apiSettings.automaticDataCollectionEnabled) {
1538
+ headers.append('X-Firebase-Appid', url.params.apiSettings.appId);
1539
+ }
1540
+ if (url.params.apiSettings.getAppCheckToken) {
1541
+ const appCheckToken = await url.params.apiSettings.getAppCheckToken();
1542
+ if (appCheckToken) {
1543
+ headers.append('X-Firebase-AppCheck', appCheckToken.token);
1544
+ if (appCheckToken.error) {
1545
+ logger.warn(`Unable to obtain a valid App Check token: ${appCheckToken.error.message}`);
1546
+ }
1547
+ }
1548
+ }
1549
+ if (url.params.apiSettings.getAuthToken) {
1550
+ const authToken = await url.params.apiSettings.getAuthToken();
1551
+ if (authToken) {
1552
+ headers.append('Authorization', `Firebase ${authToken.accessToken}`);
1553
+ }
1554
+ }
1555
+ return headers;
1556
+ }
1557
+ async function makeRequest(requestUrlParams, body) {
1558
+ const url = new RequestURL(requestUrlParams);
1559
+ let response;
1560
+ const externalSignal = requestUrlParams.singleRequestOptions?.signal;
1561
+ const timeoutMillis = requestUrlParams.singleRequestOptions?.timeout != null &&
1562
+ requestUrlParams.singleRequestOptions.timeout >= 0
1563
+ ? requestUrlParams.singleRequestOptions.timeout
1564
+ : DEFAULT_FETCH_TIMEOUT_MS;
1565
+ const internalAbortController = new AbortController();
1566
+ const fetchTimeoutId = setTimeout(() => {
1567
+ internalAbortController.abort(new DOMException(TIMEOUT_EXPIRED_MESSAGE, ABORT_ERROR_NAME));
1568
+ logger.debug(`Aborting request to ${url} due to timeout (${timeoutMillis}ms)`);
1569
+ }, timeoutMillis);
1570
+ // Used to abort the fetch if either the user-defined `externalSignal` is aborted, or if the
1571
+ // internal signal (triggered by timeouts) is aborted.
1572
+ const combinedSignal = AbortSignal.any(externalSignal
1573
+ ? [externalSignal, internalAbortController.signal]
1574
+ : [internalAbortController.signal]);
1575
+ if (externalSignal && externalSignal.aborted) {
1576
+ clearTimeout(fetchTimeoutId);
1577
+ throw new DOMException(externalSignal.reason ?? 'Aborted externally before fetch', ABORT_ERROR_NAME);
1578
+ }
1579
+ try {
1580
+ const fetchOptions = {
1581
+ method: 'POST',
1582
+ headers: await getHeaders(url),
1583
+ signal: combinedSignal,
1584
+ body
1585
+ };
1586
+ response = await fetch(url.toString(), fetchOptions);
1587
+ if (!response.ok) {
1588
+ let message = '';
1589
+ let errorDetails;
1590
+ try {
1591
+ const json = await response.json();
1592
+ message = json.error.message;
1593
+ if (json.error.details) {
1594
+ message += ` ${JSON.stringify(json.error.details)}`;
1595
+ errorDetails = json.error.details;
1596
+ }
1597
+ }
1598
+ catch (e) {
1599
+ // ignored
1600
+ }
1601
+ if (response.status === 403 &&
1602
+ errorDetails &&
1603
+ errorDetails.some((detail) => detail.reason === 'SERVICE_DISABLED') &&
1604
+ errorDetails.some((detail) => detail.links?.[0]?.description.includes('Google developers console API activation'))) {
1605
+ throw new AIError(AIErrorCode.API_NOT_ENABLED, `The Firebase AI SDK requires the Firebase AI ` +
1606
+ `API ('firebasevertexai.googleapis.com') to be enabled in your ` +
1607
+ `Firebase project. Enable this API by visiting the Firebase Console ` +
1608
+ `at https://console.firebase.google.com/project/${url.params.apiSettings.project}/ailogic/ ` +
1609
+ `and clicking "Get started". If you enabled this API recently, ` +
1610
+ `wait a few minutes for the action to propagate to our systems and ` +
1611
+ `then retry.`, {
1612
+ status: response.status,
1613
+ statusText: response.statusText,
1614
+ errorDetails
1615
+ });
1616
+ }
1617
+ throw new AIError(AIErrorCode.FETCH_ERROR, `Error fetching from ${url}: [${response.status} ${response.statusText}] ${message}`, {
1618
+ status: response.status,
1619
+ statusText: response.statusText,
1620
+ errorDetails
1621
+ });
1622
+ }
1623
+ }
1624
+ catch (e) {
1625
+ let err = e;
1626
+ if (e.code !== AIErrorCode.FETCH_ERROR &&
1627
+ e.code !== AIErrorCode.API_NOT_ENABLED &&
1628
+ e instanceof Error &&
1629
+ e.name !== ABORT_ERROR_NAME) {
1630
+ err = new AIError(AIErrorCode.ERROR, `Error fetching from ${url.toString()}: ${e.message}`);
1631
+ err.stack = e.stack;
1632
+ }
1633
+ throw err;
1634
+ }
1635
+ finally {
1636
+ // When doing streaming requests, this will clear the timeout once the stream begins.
1637
+ // If a timeout it 3000ms, and the stream starts after 300ms and ends after 5000ms, the
1638
+ // timeout will be cleared after 300ms, so it won't abort the request.
1639
+ clearTimeout(fetchTimeoutId);
1640
+ }
1641
+ return response;
1642
+ }
1643
+
1644
+ /**
1645
+ * @license
1646
+ * Copyright 2024 Google LLC
1647
+ *
1648
+ * Licensed under the Apache License, Version 2.0 (the "License");
1649
+ * you may not use this file except in compliance with the License.
1650
+ * You may obtain a copy of the License at
1651
+ *
1652
+ * http://www.apache.org/licenses/LICENSE-2.0
1653
+ *
1654
+ * Unless required by applicable law or agreed to in writing, software
1655
+ * distributed under the License is distributed on an "AS IS" BASIS,
1656
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1657
+ * See the License for the specific language governing permissions and
1658
+ * limitations under the License.
1659
+ */
1660
+ /**
1661
+ * Check that at least one candidate exists and does not have a bad
1662
+ * finish reason. Warns if multiple candidates exist.
1663
+ */
1664
+ function hasValidCandidates(response) {
1665
+ if (response.candidates && response.candidates.length > 0) {
1666
+ if (response.candidates.length > 1) {
1667
+ logger.warn(`This response had ${response.candidates.length} ` +
1668
+ `candidates. Returning text from the first candidate only. ` +
1669
+ `Access response.candidates directly to use the other candidates.`);
1670
+ }
1671
+ if (hadBadFinishReason(response.candidates[0])) {
1672
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Response error: ${formatBlockErrorMessage(response)}. Response body stored in error.response`, {
1673
+ response
1674
+ });
1675
+ }
1676
+ return true;
1677
+ }
1678
+ else {
1679
+ return false;
1680
+ }
1681
+ }
1682
+ /**
1683
+ * Creates an EnhancedGenerateContentResponse object that has helper functions and
1684
+ * other modifications that improve usability.
1685
+ */
1686
+ function createEnhancedContentResponse(response, inferenceSource = InferenceSource.IN_CLOUD) {
1687
+ /**
1688
+ * The Vertex AI backend omits default values.
1689
+ * This causes the `index` property to be omitted from the first candidate in the
1690
+ * response, since it has index 0, and 0 is a default value.
1691
+ * See: https://github.com/firebase/firebase-js-sdk/issues/8566
1692
+ */
1693
+ if (response.candidates && !response.candidates[0].hasOwnProperty('index')) {
1694
+ response.candidates[0].index = 0;
1695
+ }
1696
+ const responseWithHelpers = addHelpers(response);
1697
+ responseWithHelpers.inferenceSource = inferenceSource;
1698
+ return responseWithHelpers;
1699
+ }
1700
+ /**
1701
+ * Adds convenience helper methods to a response object, including stream
1702
+ * chunks (as long as each chunk is a complete GenerateContentResponse JSON).
1703
+ */
1704
+ function addHelpers(response) {
1705
+ response.text = () => {
1706
+ if (hasValidCandidates(response)) {
1707
+ return getText(response, part => !part.thought);
1708
+ }
1709
+ else if (response.promptFeedback) {
1710
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Text not available. ${formatBlockErrorMessage(response)}`, {
1711
+ response
1712
+ });
1713
+ }
1714
+ return '';
1715
+ };
1716
+ response.thoughtSummary = () => {
1717
+ if (hasValidCandidates(response)) {
1718
+ const result = getText(response, part => !!part.thought);
1719
+ return result === '' ? undefined : result;
1720
+ }
1721
+ else if (response.promptFeedback) {
1722
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Thought summary not available. ${formatBlockErrorMessage(response)}`, {
1723
+ response
1724
+ });
1725
+ }
1726
+ return undefined;
1727
+ };
1728
+ response.inlineDataParts = () => {
1729
+ if (hasValidCandidates(response)) {
1730
+ return getInlineDataParts(response);
1731
+ }
1732
+ else if (response.promptFeedback) {
1733
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Data not available. ${formatBlockErrorMessage(response)}`, {
1734
+ response
1735
+ });
1736
+ }
1737
+ return undefined;
1738
+ };
1739
+ response.functionCalls = () => {
1740
+ if (hasValidCandidates(response)) {
1741
+ return getFunctionCalls(response);
1742
+ }
1743
+ else if (response.promptFeedback) {
1744
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Function call not available. ${formatBlockErrorMessage(response)}`, {
1745
+ response
1746
+ });
1747
+ }
1748
+ return undefined;
1749
+ };
1750
+ return response;
1751
+ }
1752
+ /**
1753
+ * Returns all text from the first candidate's parts, filtering by whether
1754
+ * `partFilter()` returns true.
1755
+ *
1756
+ * @param response - The `GenerateContentResponse` from which to extract text.
1757
+ * @param partFilter - Only return `Part`s for which this returns true
1758
+ */
1759
+ function getText(response, partFilter) {
1760
+ const textStrings = [];
1761
+ if (response.candidates?.[0].content?.parts) {
1762
+ for (const part of response.candidates?.[0].content?.parts) {
1763
+ if (part.text && partFilter(part)) {
1764
+ textStrings.push(part.text);
1765
+ }
1766
+ }
1767
+ }
1768
+ if (textStrings.length > 0) {
1769
+ return textStrings.join('');
1770
+ }
1771
+ else {
1772
+ return '';
1773
+ }
1774
+ }
1775
+ /**
1776
+ * Returns every {@link FunctionCall} associated with first candidate.
1777
+ */
1778
+ function getFunctionCalls(response) {
1779
+ if (!response) {
1780
+ return undefined;
1781
+ }
1782
+ const functionCalls = [];
1783
+ if (response.candidates?.[0].content?.parts) {
1784
+ for (const part of response.candidates?.[0].content?.parts) {
1785
+ if (part.functionCall) {
1786
+ functionCalls.push(part.functionCall);
1787
+ }
1788
+ }
1789
+ }
1790
+ if (functionCalls.length > 0) {
1791
+ return functionCalls;
1792
+ }
1793
+ else {
1794
+ return undefined;
1795
+ }
1796
+ }
1797
+ /**
1798
+ * Returns every {@link InlineDataPart} in the first candidate if present.
1799
+ *
1800
+ * @internal
1801
+ */
1802
+ function getInlineDataParts(response) {
1803
+ const data = [];
1804
+ if (response.candidates?.[0].content?.parts) {
1805
+ for (const part of response.candidates?.[0].content?.parts) {
1806
+ if (part.inlineData) {
1807
+ data.push(part);
1808
+ }
1809
+ }
1810
+ }
1811
+ if (data.length > 0) {
1812
+ return data;
1813
+ }
1814
+ else {
1815
+ return undefined;
1816
+ }
1817
+ }
1818
+ const badFinishReasons = [FinishReason.RECITATION, FinishReason.SAFETY];
1819
+ function hadBadFinishReason(candidate) {
1820
+ return (!!candidate.finishReason &&
1821
+ badFinishReasons.some(reason => reason === candidate.finishReason));
1822
+ }
1823
+ function formatBlockErrorMessage(response) {
1824
+ let message = '';
1825
+ if ((!response.candidates || response.candidates.length === 0) &&
1826
+ response.promptFeedback) {
1827
+ message += 'Response was blocked';
1828
+ if (response.promptFeedback?.blockReason) {
1829
+ message += ` due to ${response.promptFeedback.blockReason}`;
1830
+ }
1831
+ if (response.promptFeedback?.blockReasonMessage) {
1832
+ message += `: ${response.promptFeedback.blockReasonMessage}`;
1833
+ }
1834
+ }
1835
+ else if (response.candidates?.[0]) {
1836
+ const firstCandidate = response.candidates[0];
1837
+ if (hadBadFinishReason(firstCandidate)) {
1838
+ message += `Candidate was blocked due to ${firstCandidate.finishReason}`;
1839
+ if (firstCandidate.finishMessage) {
1840
+ message += `: ${firstCandidate.finishMessage}`;
1841
+ }
1842
+ }
1843
+ }
1844
+ return message;
1845
+ }
1846
+ /**
1847
+ * Convert a generic successful fetch response body to an Imagen response object
1848
+ * that can be returned to the user. This converts the REST APIs response format to our
1849
+ * APIs representation of a response.
1850
+ *
1851
+ * @internal
1852
+ */
1853
+ async function handlePredictResponse(response) {
1854
+ const responseJson = await response.json();
1855
+ const images = [];
1856
+ let filteredReason = undefined;
1857
+ // The backend should always send a non-empty array of predictions if the response was successful.
1858
+ if (!responseJson.predictions || responseJson.predictions?.length === 0) {
1859
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, 'No predictions or filtered reason received from Vertex AI. Please report this issue with the full error details at https://github.com/firebase/firebase-js-sdk/issues.');
1860
+ }
1861
+ for (const prediction of responseJson.predictions) {
1862
+ if (prediction.raiFilteredReason) {
1863
+ filteredReason = prediction.raiFilteredReason;
1864
+ }
1865
+ else if (prediction.mimeType && prediction.bytesBase64Encoded) {
1866
+ images.push({
1867
+ mimeType: prediction.mimeType,
1868
+ bytesBase64Encoded: prediction.bytesBase64Encoded
1869
+ });
1870
+ }
1871
+ else if (prediction.mimeType && prediction.gcsUri) {
1872
+ images.push({
1873
+ mimeType: prediction.mimeType,
1874
+ gcsURI: prediction.gcsUri
1875
+ });
1876
+ }
1877
+ else if (prediction.safetyAttributes) ;
1878
+ else {
1879
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, `Unexpected element in 'predictions' array in response: '${JSON.stringify(prediction)}'`);
1880
+ }
1881
+ }
1882
+ return { images, filteredReason };
1883
+ }
1884
+
1885
+ /**
1886
+ * @license
1887
+ * Copyright 2025 Google LLC
1888
+ *
1889
+ * Licensed under the Apache License, Version 2.0 (the "License");
1890
+ * you may not use this file except in compliance with the License.
1891
+ * You may obtain a copy of the License at
1892
+ *
1893
+ * http://www.apache.org/licenses/LICENSE-2.0
1894
+ *
1895
+ * Unless required by applicable law or agreed to in writing, software
1896
+ * distributed under the License is distributed on an "AS IS" BASIS,
1897
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1898
+ * See the License for the specific language governing permissions and
1899
+ * limitations under the License.
1900
+ */
1901
+ /**
1902
+ * This SDK supports both the Vertex AI Gemini API and the Gemini Developer API (using Google AI).
1903
+ * The public API prioritizes the format used by the Vertex AI Gemini API.
1904
+ * We avoid having two sets of types by translating requests and responses between the two API formats.
1905
+ * This translation allows developers to switch between the Vertex AI Gemini API and the Gemini Developer API
1906
+ * with minimal code changes.
1907
+ *
1908
+ * In here are functions that map requests and responses between the two API formats.
1909
+ * Requests in the Vertex AI format are mapped to the Google AI format before being sent.
1910
+ * Responses from the Google AI backend are mapped back to the Vertex AI format before being returned to the user.
1911
+ */
1912
+ /**
1913
+ * Maps a Vertex AI {@link GenerateContentRequest} to a format that can be sent to Google AI.
1914
+ *
1915
+ * @param generateContentRequest The {@link GenerateContentRequest} to map.
1916
+ * @returns A {@link GenerateContentResponse} that conforms to the Google AI format.
1917
+ *
1918
+ * @throws If the request contains properties that are unsupported by Google AI.
1919
+ *
1920
+ * @internal
1921
+ */
1922
+ function mapGenerateContentRequest(generateContentRequest) {
1923
+ generateContentRequest.safetySettings?.forEach(safetySetting => {
1924
+ if (safetySetting.method) {
1925
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'SafetySetting.method is not supported in the the Gemini Developer API. Please remove this property.');
1926
+ }
1927
+ });
1928
+ if (generateContentRequest.generationConfig?.topK) {
1929
+ const roundedTopK = Math.round(generateContentRequest.generationConfig.topK);
1930
+ if (roundedTopK !== generateContentRequest.generationConfig.topK) {
1931
+ logger.warn('topK in GenerationConfig has been rounded to the nearest integer to match the format for requests to the Gemini Developer API.');
1932
+ generateContentRequest.generationConfig.topK = roundedTopK;
1933
+ }
1934
+ }
1935
+ return generateContentRequest;
1936
+ }
1937
+ /**
1938
+ * Maps a {@link GenerateContentResponse} from Google AI to the format of the
1939
+ * {@link GenerateContentResponse} that we get from VertexAI that is exposed in the public API.
1940
+ *
1941
+ * @param googleAIResponse The {@link GenerateContentResponse} from Google AI.
1942
+ * @returns A {@link GenerateContentResponse} that conforms to the public API's format.
1943
+ *
1944
+ * @internal
1945
+ */
1946
+ function mapGenerateContentResponse(googleAIResponse) {
1947
+ const generateContentResponse = {
1948
+ candidates: googleAIResponse.candidates
1949
+ ? mapGenerateContentCandidates(googleAIResponse.candidates)
1950
+ : undefined,
1951
+ prompt: googleAIResponse.promptFeedback
1952
+ ? mapPromptFeedback(googleAIResponse.promptFeedback)
1953
+ : undefined,
1954
+ usageMetadata: googleAIResponse.usageMetadata
1955
+ };
1956
+ return generateContentResponse;
1957
+ }
1958
+ /**
1959
+ * Maps a Vertex AI {@link CountTokensRequest} to a format that can be sent to Google AI.
1960
+ *
1961
+ * @param countTokensRequest The {@link CountTokensRequest} to map.
1962
+ * @param model The model to count tokens with.
1963
+ * @returns A {@link CountTokensRequest} that conforms to the Google AI format.
1964
+ *
1965
+ * @internal
1966
+ */
1967
+ function mapCountTokensRequest(countTokensRequest, model) {
1968
+ const mappedCountTokensRequest = {
1969
+ generateContentRequest: {
1970
+ model,
1971
+ ...countTokensRequest
1972
+ }
1973
+ };
1974
+ return mappedCountTokensRequest;
1975
+ }
1976
+ /**
1977
+ * Maps a Google AI {@link GoogleAIGenerateContentCandidate} to a format that conforms
1978
+ * to the Vertex AI API format.
1979
+ *
1980
+ * @param candidates The {@link GoogleAIGenerateContentCandidate} to map.
1981
+ * @returns A {@link GenerateContentCandidate} that conforms to the Vertex AI format.
1982
+ *
1983
+ * @throws If any {@link Part} in the candidates has a `videoMetadata` property.
1984
+ *
1985
+ * @internal
1986
+ */
1987
+ function mapGenerateContentCandidates(candidates) {
1988
+ const mappedCandidates = [];
1989
+ let mappedSafetyRatings;
1990
+ if (mappedCandidates) {
1991
+ candidates.forEach(candidate => {
1992
+ // Map citationSources to citations.
1993
+ let citationMetadata;
1994
+ if (candidate.citationMetadata) {
1995
+ citationMetadata = {
1996
+ citations: candidate.citationMetadata.citationSources
1997
+ };
1998
+ }
1999
+ // Assign missing candidate SafetyRatings properties to their defaults if undefined.
2000
+ if (candidate.safetyRatings) {
2001
+ mappedSafetyRatings = candidate.safetyRatings.map(safetyRating => {
2002
+ return {
2003
+ ...safetyRating,
2004
+ severity: safetyRating.severity ?? HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
2005
+ probabilityScore: safetyRating.probabilityScore ?? 0,
2006
+ severityScore: safetyRating.severityScore ?? 0
2007
+ };
2008
+ });
2009
+ }
2010
+ // videoMetadata is not supported.
2011
+ // Throw early since developers may send a long video as input and only expect to pay
2012
+ // for inference on a small portion of the video.
2013
+ if (candidate.content?.parts?.some(part => part?.videoMetadata)) {
2014
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Part.videoMetadata is not supported in the Gemini Developer API. Please remove this property.');
2015
+ }
2016
+ const mappedCandidate = {
2017
+ index: candidate.index,
2018
+ content: candidate.content,
2019
+ finishReason: candidate.finishReason,
2020
+ finishMessage: candidate.finishMessage,
2021
+ safetyRatings: mappedSafetyRatings,
2022
+ citationMetadata,
2023
+ groundingMetadata: candidate.groundingMetadata,
2024
+ urlContextMetadata: candidate.urlContextMetadata
2025
+ };
2026
+ mappedCandidates.push(mappedCandidate);
2027
+ });
2028
+ }
2029
+ return mappedCandidates;
2030
+ }
2031
+ function mapPromptFeedback(promptFeedback) {
2032
+ // Assign missing SafetyRating properties to their defaults if undefined.
2033
+ const mappedSafetyRatings = [];
2034
+ promptFeedback.safetyRatings.forEach(safetyRating => {
2035
+ mappedSafetyRatings.push({
2036
+ category: safetyRating.category,
2037
+ probability: safetyRating.probability,
2038
+ severity: safetyRating.severity ?? HarmSeverity.HARM_SEVERITY_UNSUPPORTED,
2039
+ probabilityScore: safetyRating.probabilityScore ?? 0,
2040
+ severityScore: safetyRating.severityScore ?? 0,
2041
+ blocked: safetyRating.blocked
2042
+ });
2043
+ });
2044
+ const mappedPromptFeedback = {
2045
+ blockReason: promptFeedback.blockReason,
2046
+ safetyRatings: mappedSafetyRatings,
2047
+ blockReasonMessage: promptFeedback.blockReasonMessage
2048
+ };
2049
+ return mappedPromptFeedback;
2050
+ }
2051
+
2052
+ /**
2053
+ * @license
2054
+ * Copyright 2024 Google LLC
2055
+ *
2056
+ * Licensed under the Apache License, Version 2.0 (the "License");
2057
+ * you may not use this file except in compliance with the License.
2058
+ * You may obtain a copy of the License at
2059
+ *
2060
+ * http://www.apache.org/licenses/LICENSE-2.0
2061
+ *
2062
+ * Unless required by applicable law or agreed to in writing, software
2063
+ * distributed under the License is distributed on an "AS IS" BASIS,
2064
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2065
+ * See the License for the specific language governing permissions and
2066
+ * limitations under the License.
2067
+ */
2068
+ const responseLineRE = /^data\: (.*)(?:\n\n|\r\r|\r\n\r\n)/;
2069
+ /**
2070
+ * Process a response.body stream from the backend and return an
2071
+ * iterator that provides one complete GenerateContentResponse at a time
2072
+ * and a promise that resolves with a single aggregated
2073
+ * GenerateContentResponse.
2074
+ *
2075
+ * @param response - Response from a fetch call
2076
+ */
2077
+ async function processStream(response, apiSettings, inferenceSource) {
2078
+ const inputStream = response.body.pipeThrough(new TextDecoderStream('utf8', { fatal: true }));
2079
+ const responseStream = getResponseStream(inputStream);
2080
+ // We split the stream so the user can iterate over partial results (stream1)
2081
+ // while we aggregate the full result for history/final response (stream2).
2082
+ const [stream1, stream2] = responseStream.tee();
2083
+ const { response: internalResponse, firstValue } = await processStreamInternal(stream2, apiSettings, inferenceSource);
2084
+ return {
2085
+ stream: generateResponseSequence(stream1, apiSettings, inferenceSource),
2086
+ response: internalResponse,
2087
+ firstValue
2088
+ };
2089
+ }
2090
+ /**
2091
+ * Consumes streams teed from the input stream for internal needs.
2092
+ * The streams need to be teed because each stream can only be consumed
2093
+ * by one reader.
2094
+ *
2095
+ * "streamForPeek"
2096
+ * This tee is used to peek at the first value for relevant information
2097
+ * that we need to evaluate before returning the stream handle to the
2098
+ * client. For example, we need to check if the response is a function
2099
+ * call that may need to be handled by automatic function calling before
2100
+ * returning a response to the client.
2101
+ *
2102
+ * "streamForAggregation"
2103
+ * We iterate through this tee independently from the user and aggregate
2104
+ * it into a single response when the stream is complete. We need this
2105
+ * aggregate object to add to chat history when using ChatSession. It's
2106
+ * also provided to the user if they want it.
2107
+ */
2108
+ async function processStreamInternal(stream, apiSettings, inferenceSource) {
2109
+ const [streamForPeek, streamForAggregation] = stream.tee();
2110
+ const reader = streamForPeek.getReader();
2111
+ const { value } = await reader.read();
2112
+ return {
2113
+ firstValue: value,
2114
+ response: getResponsePromise(streamForAggregation, apiSettings, inferenceSource)
2115
+ };
2116
+ }
2117
+ async function getResponsePromise(stream, apiSettings, inferenceSource) {
2118
+ const allResponses = [];
2119
+ const reader = stream.getReader();
2120
+ while (true) {
2121
+ const { done, value } = await reader.read();
2122
+ if (done) {
2123
+ let generateContentResponse = aggregateResponses(allResponses);
2124
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2125
+ generateContentResponse = mapGenerateContentResponse(generateContentResponse);
2126
+ }
2127
+ return createEnhancedContentResponse(generateContentResponse, inferenceSource);
2128
+ }
2129
+ allResponses.push(value);
2130
+ }
2131
+ }
2132
+ async function* generateResponseSequence(stream, apiSettings, inferenceSource) {
2133
+ const reader = stream.getReader();
2134
+ while (true) {
2135
+ const { value, done } = await reader.read();
2136
+ if (done) {
2137
+ break;
2138
+ }
2139
+ let enhancedResponse;
2140
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2141
+ enhancedResponse = createEnhancedContentResponse(mapGenerateContentResponse(value), inferenceSource);
2142
+ }
2143
+ else {
2144
+ enhancedResponse = createEnhancedContentResponse(value, inferenceSource);
2145
+ }
2146
+ const firstCandidate = enhancedResponse.candidates?.[0];
2147
+ if (!firstCandidate?.content?.parts &&
2148
+ !firstCandidate?.finishReason &&
2149
+ !firstCandidate?.citationMetadata &&
2150
+ !firstCandidate?.urlContextMetadata) {
2151
+ continue;
2152
+ }
2153
+ yield enhancedResponse;
2154
+ }
2155
+ }
2156
+ /**
2157
+ * Reads a raw string stream, buffers incomplete chunks, and yields parsed JSON objects.
2158
+ */
2159
+ function getResponseStream(inputStream) {
2160
+ const reader = inputStream.getReader();
2161
+ const stream = new ReadableStream({
2162
+ start(controller) {
2163
+ let currentText = '';
2164
+ return pump();
2165
+ function pump() {
2166
+ return reader.read().then(({ value, done }) => {
2167
+ if (done) {
2168
+ if (currentText.trim()) {
2169
+ controller.error(new AIError(AIErrorCode.PARSE_FAILED, 'Failed to parse stream'));
2170
+ return;
2171
+ }
2172
+ controller.close();
2173
+ return;
2174
+ }
2175
+ currentText += value;
2176
+ // SSE events may span chunk boundaries, so we buffer until we match
2177
+ // the full "data: {json}\n\n" pattern.
2178
+ let match = currentText.match(responseLineRE);
2179
+ let parsedResponse;
2180
+ while (match) {
2181
+ try {
2182
+ parsedResponse = JSON.parse(match[1]);
2183
+ }
2184
+ catch (e) {
2185
+ controller.error(new AIError(AIErrorCode.PARSE_FAILED, `Error parsing JSON response: "${match[1]}`));
2186
+ return;
2187
+ }
2188
+ controller.enqueue(parsedResponse);
2189
+ currentText = currentText.substring(match[0].length);
2190
+ match = currentText.match(responseLineRE);
2191
+ }
2192
+ return pump();
2193
+ });
2194
+ }
2195
+ }
2196
+ });
2197
+ return stream;
2198
+ }
2199
+ /**
2200
+ * Aggregates an array of `GenerateContentResponse`s into a single
2201
+ * GenerateContentResponse.
2202
+ */
2203
+ function aggregateResponses(responses) {
2204
+ const lastResponse = responses[responses.length - 1];
2205
+ const aggregatedResponse = {
2206
+ promptFeedback: lastResponse?.promptFeedback
2207
+ };
2208
+ for (const response of responses) {
2209
+ if (response.candidates) {
2210
+ for (const candidate of response.candidates) {
2211
+ // Use 0 if index is undefined (protobuf default value omission).
2212
+ const i = candidate.index || 0;
2213
+ if (!aggregatedResponse.candidates) {
2214
+ aggregatedResponse.candidates = [];
2215
+ }
2216
+ if (!aggregatedResponse.candidates[i]) {
2217
+ aggregatedResponse.candidates[i] = {
2218
+ index: candidate.index
2219
+ };
2220
+ }
2221
+ // Overwrite with the latest metadata
2222
+ aggregatedResponse.candidates[i].citationMetadata =
2223
+ candidate.citationMetadata;
2224
+ aggregatedResponse.candidates[i].finishReason = candidate.finishReason;
2225
+ aggregatedResponse.candidates[i].finishMessage =
2226
+ candidate.finishMessage;
2227
+ aggregatedResponse.candidates[i].safetyRatings =
2228
+ candidate.safetyRatings;
2229
+ aggregatedResponse.candidates[i].groundingMetadata =
2230
+ candidate.groundingMetadata;
2231
+ // The urlContextMetadata object is defined in the first chunk of the response stream.
2232
+ // In all subsequent chunks, the urlContextMetadata object will be undefined. We need to
2233
+ // make sure that we don't overwrite the first value urlContextMetadata object with undefined.
2234
+ // FIXME: What happens if we receive a second, valid urlContextMetadata object?
2235
+ const urlContextMetadata = candidate.urlContextMetadata;
2236
+ if (typeof urlContextMetadata === 'object' &&
2237
+ urlContextMetadata !== null &&
2238
+ Object.keys(urlContextMetadata).length > 0) {
2239
+ aggregatedResponse.candidates[i].urlContextMetadata =
2240
+ urlContextMetadata;
2241
+ }
2242
+ if (candidate.content) {
2243
+ if (!candidate.content.parts) {
2244
+ continue;
2245
+ }
2246
+ if (!aggregatedResponse.candidates[i].content) {
2247
+ aggregatedResponse.candidates[i].content = {
2248
+ role: candidate.content.role || 'user',
2249
+ parts: []
2250
+ };
2251
+ }
2252
+ for (const part of candidate.content.parts) {
2253
+ const newPart = { ...part };
2254
+ // The backend can send empty text parts. If these are sent back
2255
+ // (e.g. in chat history), the backend will respond with an error.
2256
+ // To prevent this, ignore empty text parts.
2257
+ if (part.text === '') {
2258
+ continue;
2259
+ }
2260
+ if (Object.keys(newPart).length > 0) {
2261
+ aggregatedResponse.candidates[i].content.parts.push(newPart);
2262
+ }
2263
+ }
2264
+ }
2265
+ }
2266
+ }
2267
+ }
2268
+ return aggregatedResponse;
2269
+ }
2270
+
2271
+ /**
2272
+ * @license
2273
+ * Copyright 2025 Google LLC
2274
+ *
2275
+ * Licensed under the Apache License, Version 2.0 (the "License");
2276
+ * you may not use this file except in compliance with the License.
2277
+ * You may obtain a copy of the License at
2278
+ *
2279
+ * http://www.apache.org/licenses/LICENSE-2.0
2280
+ *
2281
+ * Unless required by applicable law or agreed to in writing, software
2282
+ * distributed under the License is distributed on an "AS IS" BASIS,
2283
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2284
+ * See the License for the specific language governing permissions and
2285
+ * limitations under the License.
2286
+ */
2287
+ const errorsCausingFallback = [
2288
+ // most network errors
2289
+ AIErrorCode.FETCH_ERROR,
2290
+ // fallback code for all other errors in makeRequest
2291
+ AIErrorCode.ERROR,
2292
+ // error due to API not being enabled in project
2293
+ AIErrorCode.API_NOT_ENABLED
2294
+ ];
2295
+ /**
2296
+ * Dispatches a request to the appropriate backend (on-device or in-cloud)
2297
+ * based on the inference mode.
2298
+ *
2299
+ * @param request - The request to be sent.
2300
+ * @param chromeAdapter - The on-device model adapter.
2301
+ * @param onDeviceCall - The function to call for on-device inference.
2302
+ * @param inCloudCall - The function to call for in-cloud inference.
2303
+ * @returns The response from the backend.
2304
+ */
2305
+ async function callCloudOrDevice(request, chromeAdapter, onDeviceCall, inCloudCall) {
2306
+ if (!chromeAdapter) {
2307
+ return {
2308
+ response: await inCloudCall(),
2309
+ inferenceSource: InferenceSource.IN_CLOUD
2310
+ };
2311
+ }
2312
+ switch (chromeAdapter.mode) {
2313
+ case InferenceMode.ONLY_ON_DEVICE:
2314
+ if (await chromeAdapter.isAvailable(request)) {
2315
+ return {
2316
+ response: await onDeviceCall(),
2317
+ inferenceSource: InferenceSource.ON_DEVICE
2318
+ };
2319
+ }
2320
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Inference mode is ONLY_ON_DEVICE, but an on-device model is not available.');
2321
+ case InferenceMode.ONLY_IN_CLOUD:
2322
+ return {
2323
+ response: await inCloudCall(),
2324
+ inferenceSource: InferenceSource.IN_CLOUD
2325
+ };
2326
+ case InferenceMode.PREFER_IN_CLOUD:
2327
+ try {
2328
+ return {
2329
+ response: await inCloudCall(),
2330
+ inferenceSource: InferenceSource.IN_CLOUD
2331
+ };
2332
+ }
2333
+ catch (e) {
2334
+ if (e instanceof AIError && errorsCausingFallback.includes(e.code)) {
2335
+ return {
2336
+ response: await onDeviceCall(),
2337
+ inferenceSource: InferenceSource.ON_DEVICE
2338
+ };
2339
+ }
2340
+ throw e;
2341
+ }
2342
+ case InferenceMode.PREFER_ON_DEVICE:
2343
+ if (await chromeAdapter.isAvailable(request)) {
2344
+ return {
2345
+ response: await onDeviceCall(),
2346
+ inferenceSource: InferenceSource.ON_DEVICE
2347
+ };
2348
+ }
2349
+ return {
2350
+ response: await inCloudCall(),
2351
+ inferenceSource: InferenceSource.IN_CLOUD
2352
+ };
2353
+ default:
2354
+ throw new AIError(AIErrorCode.ERROR, `Unexpected infererence mode: ${chromeAdapter.mode}`);
2355
+ }
2356
+ }
2357
+
2358
+ /**
2359
+ * @license
2360
+ * Copyright 2024 Google LLC
2361
+ *
2362
+ * Licensed under the Apache License, Version 2.0 (the "License");
2363
+ * you may not use this file except in compliance with the License.
2364
+ * You may obtain a copy of the License at
2365
+ *
2366
+ * http://www.apache.org/licenses/LICENSE-2.0
2367
+ *
2368
+ * Unless required by applicable law or agreed to in writing, software
2369
+ * distributed under the License is distributed on an "AS IS" BASIS,
2370
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2371
+ * See the License for the specific language governing permissions and
2372
+ * limitations under the License.
2373
+ */
2374
+ async function generateContentStreamOnCloud(apiSettings, model, params, singleRequestOptions) {
2375
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2376
+ params = mapGenerateContentRequest(params);
2377
+ }
2378
+ return makeRequest({
2379
+ task: "streamGenerateContent" /* Task.STREAM_GENERATE_CONTENT */,
2380
+ model,
2381
+ apiSettings,
2382
+ stream: true,
2383
+ singleRequestOptions
2384
+ }, JSON.stringify(params));
2385
+ }
2386
+ async function generateContentStream(apiSettings, model, params, chromeAdapter, singleRequestOptions) {
2387
+ const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContentStream(params), () => generateContentStreamOnCloud(apiSettings, model, params, singleRequestOptions));
2388
+ return processStream(callResult.response, apiSettings, callResult.inferenceSource);
2389
+ }
2390
+ async function generateContentOnCloud(apiSettings, model, params, singleRequestOptions) {
2391
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2392
+ params = mapGenerateContentRequest(params);
2393
+ }
2394
+ return makeRequest({
2395
+ model,
2396
+ task: "generateContent" /* Task.GENERATE_CONTENT */,
2397
+ apiSettings,
2398
+ stream: false,
2399
+ singleRequestOptions
2400
+ }, JSON.stringify(params));
2401
+ }
2402
+ async function templateGenerateContent(apiSettings, templateId, templateParams, singleRequestOptions) {
2403
+ const response = await makeRequest({
2404
+ task: "templateGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_GENERATE_CONTENT */,
2405
+ templateId,
2406
+ apiSettings,
2407
+ stream: false,
2408
+ singleRequestOptions
2409
+ }, JSON.stringify(templateParams));
2410
+ const generateContentResponse = await processGenerateContentResponse(response, apiSettings);
2411
+ const enhancedResponse = createEnhancedContentResponse(generateContentResponse);
2412
+ return {
2413
+ response: enhancedResponse
2414
+ };
2415
+ }
2416
+ async function templateGenerateContentStream(apiSettings, templateId, templateParams, singleRequestOptions) {
2417
+ const response = await makeRequest({
2418
+ task: "templateStreamGenerateContent" /* ServerPromptTemplateTask.TEMPLATE_STREAM_GENERATE_CONTENT */,
2419
+ templateId,
2420
+ apiSettings,
2421
+ stream: true,
2422
+ singleRequestOptions
2423
+ }, JSON.stringify(templateParams));
2424
+ return processStream(response, apiSettings);
2425
+ }
2426
+ async function generateContent(apiSettings, model, params, chromeAdapter, singleRequestOptions) {
2427
+ const callResult = await callCloudOrDevice(params, chromeAdapter, () => chromeAdapter.generateContent(params), () => generateContentOnCloud(apiSettings, model, params, singleRequestOptions));
2428
+ const generateContentResponse = await processGenerateContentResponse(callResult.response, apiSettings);
2429
+ const enhancedResponse = createEnhancedContentResponse(generateContentResponse, callResult.inferenceSource);
2430
+ return {
2431
+ response: enhancedResponse
2432
+ };
2433
+ }
2434
+ async function processGenerateContentResponse(response, apiSettings) {
2435
+ const responseJson = await response.json();
2436
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2437
+ return mapGenerateContentResponse(responseJson);
2438
+ }
2439
+ else {
2440
+ return responseJson;
2441
+ }
2442
+ }
2443
+
2444
+ /**
2445
+ * @license
2446
+ * Copyright 2024 Google LLC
2447
+ *
2448
+ * Licensed under the Apache License, Version 2.0 (the "License");
2449
+ * you may not use this file except in compliance with the License.
2450
+ * You may obtain a copy of the License at
2451
+ *
2452
+ * http://www.apache.org/licenses/LICENSE-2.0
2453
+ *
2454
+ * Unless required by applicable law or agreed to in writing, software
2455
+ * distributed under the License is distributed on an "AS IS" BASIS,
2456
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2457
+ * See the License for the specific language governing permissions and
2458
+ * limitations under the License.
2459
+ */
2460
+ function formatSystemInstruction(input) {
2461
+ // null or undefined
2462
+ if (input == null) {
2463
+ return undefined;
2464
+ }
2465
+ else if (typeof input === 'string') {
2466
+ return { role: 'system', parts: [{ text: input }] };
2467
+ }
2468
+ else if (input.text) {
2469
+ return { role: 'system', parts: [input] };
2470
+ }
2471
+ else if (input.parts) {
2472
+ if (!input.role) {
2473
+ return { role: 'system', parts: input.parts };
2474
+ }
2475
+ else {
2476
+ return input;
2477
+ }
2478
+ }
2479
+ }
2480
+ function formatNewContent(request) {
2481
+ let newParts = [];
2482
+ if (typeof request === 'string') {
2483
+ newParts = [{ text: request }];
2484
+ }
2485
+ else {
2486
+ for (const partOrString of request) {
2487
+ if (typeof partOrString === 'string') {
2488
+ newParts.push({ text: partOrString });
2489
+ }
2490
+ else {
2491
+ newParts.push(partOrString);
2492
+ }
2493
+ }
2494
+ }
2495
+ return assignRoleToPartsAndValidateSendMessageRequest(newParts);
2496
+ }
2497
+ /**
2498
+ * When multiple Part types (i.e. FunctionResponsePart and TextPart) are
2499
+ * passed in a single Part array, we may need to assign different roles to each
2500
+ * part. Currently only FunctionResponsePart requires a role other than 'user'.
2501
+ * @private
2502
+ * @param parts Array of parts to pass to the model
2503
+ * @returns Array of content items
2504
+ */
2505
+ function assignRoleToPartsAndValidateSendMessageRequest(parts) {
2506
+ const userContent = { role: 'user', parts: [] };
2507
+ const functionContent = { role: 'function', parts: [] };
2508
+ let hasUserContent = false;
2509
+ let hasFunctionContent = false;
2510
+ for (const part of parts) {
2511
+ if ('functionResponse' in part) {
2512
+ functionContent.parts.push(part);
2513
+ hasFunctionContent = true;
2514
+ }
2515
+ else {
2516
+ userContent.parts.push(part);
2517
+ hasUserContent = true;
2518
+ }
2519
+ }
2520
+ if (hasUserContent && hasFunctionContent) {
2521
+ throw new AIError(AIErrorCode.INVALID_CONTENT, 'Within a single message, FunctionResponse cannot be mixed with other type of Part in the request for sending chat message.');
2522
+ }
2523
+ if (!hasUserContent && !hasFunctionContent) {
2524
+ throw new AIError(AIErrorCode.INVALID_CONTENT, 'No Content is provided for sending chat message.');
2525
+ }
2526
+ if (hasUserContent) {
2527
+ return userContent;
2528
+ }
2529
+ return functionContent;
2530
+ }
2531
+ function formatGenerateContentInput(params) {
2532
+ let formattedRequest;
2533
+ if (params.contents) {
2534
+ formattedRequest = params;
2535
+ }
2536
+ else {
2537
+ // Array or string
2538
+ const content = formatNewContent(params);
2539
+ formattedRequest = { contents: [content] };
2540
+ }
2541
+ if (params.systemInstruction) {
2542
+ formattedRequest.systemInstruction = formatSystemInstruction(params.systemInstruction);
2543
+ }
2544
+ return formattedRequest;
2545
+ }
2546
+ /**
2547
+ * Convert the user-defined parameters in {@link ImagenGenerationParams} to the format
2548
+ * that is expected from the REST API.
2549
+ *
2550
+ * @internal
2551
+ */
2552
+ function createPredictRequestBody(prompt, { gcsURI, imageFormat, addWatermark, numberOfImages = 1, negativePrompt, aspectRatio, safetyFilterLevel, personFilterLevel }) {
2553
+ // Properties that are undefined will be omitted from the JSON string that is sent in the request.
2554
+ const body = {
2555
+ instances: [
2556
+ {
2557
+ prompt
2558
+ }
2559
+ ],
2560
+ parameters: {
2561
+ storageUri: gcsURI,
2562
+ negativePrompt,
2563
+ sampleCount: numberOfImages,
2564
+ aspectRatio,
2565
+ outputOptions: imageFormat,
2566
+ addWatermark,
2567
+ safetyFilterLevel,
2568
+ personGeneration: personFilterLevel,
2569
+ includeRaiReason: true,
2570
+ includeSafetyAttributes: true
2571
+ }
2572
+ };
2573
+ return body;
2574
+ }
2575
+
2576
+ /**
2577
+ * @license
2578
+ * Copyright 2024 Google LLC
2579
+ *
2580
+ * Licensed under the Apache License, Version 2.0 (the "License");
2581
+ * you may not use this file except in compliance with the License.
2582
+ * You may obtain a copy of the License at
2583
+ *
2584
+ * http://www.apache.org/licenses/LICENSE-2.0
2585
+ *
2586
+ * Unless required by applicable law or agreed to in writing, software
2587
+ * distributed under the License is distributed on an "AS IS" BASIS,
2588
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2589
+ * See the License for the specific language governing permissions and
2590
+ * limitations under the License.
2591
+ */
2592
+ // https://ai.google.dev/api/rest/v1beta/Content#part
2593
+ const VALID_PART_FIELDS = [
2594
+ 'text',
2595
+ 'inlineData',
2596
+ 'functionCall',
2597
+ 'functionResponse',
2598
+ 'thought',
2599
+ 'thoughtSignature'
2600
+ ];
2601
+ const VALID_PARTS_PER_ROLE = {
2602
+ user: ['text', 'inlineData'],
2603
+ function: ['functionResponse'],
2604
+ model: ['text', 'functionCall', 'thought', 'thoughtSignature'],
2605
+ // System instructions shouldn't be in history anyway.
2606
+ system: ['text']
2607
+ };
2608
+ const VALID_PREVIOUS_CONTENT_ROLES = {
2609
+ user: ['model'],
2610
+ function: ['model'],
2611
+ model: ['user', 'function'],
2612
+ // System instructions shouldn't be in history.
2613
+ system: []
2614
+ };
2615
+ function validateChatHistory(history) {
2616
+ let prevContent = null;
2617
+ for (const currContent of history) {
2618
+ const { role, parts } = currContent;
2619
+ if (!prevContent && role !== 'user') {
2620
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `First Content should be with role 'user', got ${role}`);
2621
+ }
2622
+ if (!POSSIBLE_ROLES.includes(role)) {
2623
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `Each item should include role field. Got ${role} but valid roles are: ${JSON.stringify(POSSIBLE_ROLES)}`);
2624
+ }
2625
+ if (!Array.isArray(parts)) {
2626
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `Content should have 'parts' property with an array of Parts`);
2627
+ }
2628
+ if (parts.length === 0) {
2629
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `Each Content should have at least one part`);
2630
+ }
2631
+ const countFields = {
2632
+ text: 0,
2633
+ inlineData: 0,
2634
+ functionCall: 0,
2635
+ functionResponse: 0,
2636
+ thought: 0,
2637
+ thoughtSignature: 0,
2638
+ executableCode: 0,
2639
+ codeExecutionResult: 0
2640
+ };
2641
+ for (const part of parts) {
2642
+ for (const key of VALID_PART_FIELDS) {
2643
+ if (key in part) {
2644
+ countFields[key] += 1;
2645
+ }
2646
+ }
2647
+ }
2648
+ const validParts = VALID_PARTS_PER_ROLE[role];
2649
+ for (const key of VALID_PART_FIELDS) {
2650
+ if (!validParts.includes(key) && countFields[key] > 0) {
2651
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't contain '${key}' part`);
2652
+ }
2653
+ }
2654
+ if (prevContent) {
2655
+ const validPreviousContentRoles = VALID_PREVIOUS_CONTENT_ROLES[role];
2656
+ if (!validPreviousContentRoles.includes(prevContent.role)) {
2657
+ throw new AIError(AIErrorCode.INVALID_CONTENT, `Content with role '${role}' can't follow '${prevContent.role}'. Valid previous roles: ${JSON.stringify(VALID_PREVIOUS_CONTENT_ROLES)}`);
2658
+ }
2659
+ }
2660
+ prevContent = currContent;
2661
+ }
2662
+ }
2663
+
2664
+ /**
2665
+ * @license
2666
+ * Copyright 2024 Google LLC
2667
+ *
2668
+ * Licensed under the Apache License, Version 2.0 (the "License");
2669
+ * you may not use this file except in compliance with the License.
2670
+ * You may obtain a copy of the License at
2671
+ *
2672
+ * http://www.apache.org/licenses/LICENSE-2.0
2673
+ *
2674
+ * Unless required by applicable law or agreed to in writing, software
2675
+ * distributed under the License is distributed on an "AS IS" BASIS,
2676
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2677
+ * See the License for the specific language governing permissions and
2678
+ * limitations under the License.
2679
+ */
2680
+ /**
2681
+ * Used to break the internal promise chain when an error is already handled
2682
+ * by the user, preventing duplicate console logs.
2683
+ */
2684
+ const SILENT_ERROR = 'SILENT_ERROR';
2685
+ /**
2686
+ * Prevent infinite loop if the model continues to request sequential
2687
+ * function calls during automatic function calling.
2688
+ */
2689
+ const DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS = 10;
2690
+ /**
2691
+ * ChatSession class that enables sending chat messages and stores
2692
+ * history of sent and received messages so far.
2693
+ *
2694
+ * @public
2695
+ */
2696
+ class ChatSession {
2697
+ constructor(apiSettings, model, chromeAdapter, params, requestOptions) {
2698
+ this.model = model;
2699
+ this.chromeAdapter = chromeAdapter;
2700
+ this.params = params;
2701
+ this.requestOptions = requestOptions;
2702
+ this._history = [];
2703
+ /**
2704
+ * Ensures sequential execution of chat messages to maintain history order.
2705
+ * Each call waits for the previous one to settle before proceeding.
2706
+ */
2707
+ this._sendPromise = Promise.resolve();
2708
+ this._apiSettings = apiSettings;
2709
+ if (params?.history) {
2710
+ validateChatHistory(params.history);
2711
+ this._history = params.history;
2712
+ }
2713
+ }
2714
+ /**
2715
+ * Gets the chat history so far. Blocked prompts are not added to history.
2716
+ * Neither blocked candidates nor the prompts that generated them are added
2717
+ * to history.
2718
+ */
2719
+ async getHistory() {
2720
+ await this._sendPromise;
2721
+ return this._history;
2722
+ }
2723
+ /**
2724
+ * Format Content into a request for generateContent or
2725
+ * generateContentStream.
2726
+ * @internal
2727
+ */
2728
+ _formatRequest(incomingContent, tempHistory) {
2729
+ return {
2730
+ safetySettings: this.params?.safetySettings,
2731
+ generationConfig: this.params?.generationConfig,
2732
+ tools: this.params?.tools,
2733
+ toolConfig: this.params?.toolConfig,
2734
+ systemInstruction: this.params?.systemInstruction,
2735
+ contents: [...this._history, ...tempHistory, incomingContent]
2736
+ };
2737
+ }
2738
+ /**
2739
+ * Sends a chat message and receives a non-streaming
2740
+ * {@link GenerateContentResult}
2741
+ */
2742
+ async sendMessage(request, singleRequestOptions) {
2743
+ let finalResult = {};
2744
+ await this._sendPromise;
2745
+ /**
2746
+ * Temporarily store multiple turns for cases like automatic function
2747
+ * calling, only writing them to official history when the entire
2748
+ * sequence has completed successfully.
2749
+ */
2750
+ const tempHistory = [];
2751
+ this._sendPromise = this._sendPromise.then(async () => {
2752
+ let functionCalls;
2753
+ let functionCallTurnCount = 0;
2754
+ const functionCallMaxTurns = this.requestOptions?.maxSequentalFunctionCalls ??
2755
+ DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
2756
+ // Repeats until model returns a response with no function calls
2757
+ // or until `functionCallMaxTurns` is met or exceeded.
2758
+ do {
2759
+ let formattedContent;
2760
+ if (functionCalls) {
2761
+ functionCallTurnCount++;
2762
+ const functionResponseParts = await this._callFunctionsAsNeeded(functionCalls);
2763
+ formattedContent = formatNewContent(functionResponseParts);
2764
+ }
2765
+ else {
2766
+ formattedContent = formatNewContent(request);
2767
+ }
2768
+ const formattedRequest = this._formatRequest(formattedContent, tempHistory);
2769
+ tempHistory.push(formattedContent);
2770
+ const result = await generateContent(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
2771
+ ...this.requestOptions,
2772
+ ...singleRequestOptions
2773
+ });
2774
+ if (result) {
2775
+ finalResult = result;
2776
+ functionCalls = this._getCallableFunctionCalls(result.response);
2777
+ if (result.response.candidates &&
2778
+ result.response.candidates.length > 0) {
2779
+ // TODO: Make this update atomic. If creating `responseContent` throws,
2780
+ // history will contain the user message but not the response, causing
2781
+ // validation errors on the next request.
2782
+ const responseContent = {
2783
+ parts: result.response.candidates?.[0].content.parts || [],
2784
+ // Response seems to come back without a role set.
2785
+ role: result.response.candidates?.[0].content.role || 'model'
2786
+ };
2787
+ tempHistory.push(responseContent);
2788
+ }
2789
+ else {
2790
+ const blockErrorMessage = formatBlockErrorMessage(result.response);
2791
+ if (blockErrorMessage) {
2792
+ logger.warn(`sendMessage() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
2793
+ }
2794
+ }
2795
+ }
2796
+ else {
2797
+ functionCalls = undefined;
2798
+ }
2799
+ } while (functionCalls && functionCallTurnCount < functionCallMaxTurns);
2800
+ if (functionCalls && functionCallTurnCount >= functionCallMaxTurns) {
2801
+ logger.warn(`Automatic function calling exceeded the limit of` +
2802
+ ` ${functionCallMaxTurns} function calls. Returning last model response.`);
2803
+ }
2804
+ });
2805
+ await this._sendPromise;
2806
+ this._history = this._history.concat(tempHistory);
2807
+ return finalResult;
2808
+ }
2809
+ /**
2810
+ * Sends a chat message and receives the response as a
2811
+ * {@link GenerateContentStreamResult} containing an iterable stream
2812
+ * and a response promise.
2813
+ */
2814
+ async sendMessageStream(request, singleRequestOptions) {
2815
+ await this._sendPromise;
2816
+ /**
2817
+ * Temporarily store multiple turns for cases like automatic function
2818
+ * calling, only writing them to official history when the entire
2819
+ * sequence has completed successfully.
2820
+ */
2821
+ const tempHistory = [];
2822
+ const callGenerateContentStream = async () => {
2823
+ let functionCalls;
2824
+ let functionCallTurnCount = 0;
2825
+ const functionCallMaxTurns = this.requestOptions?.maxSequentalFunctionCalls ??
2826
+ DEFAULT_MAX_SEQUENTIAL_FUNCTION_CALLS;
2827
+ let result;
2828
+ // Repeats until model returns a response with no function calls
2829
+ // or until `functionCallMaxTurns` is met or exceeded.
2830
+ do {
2831
+ let formattedContent;
2832
+ if (functionCalls) {
2833
+ functionCallTurnCount++;
2834
+ const functionResponseParts = await this._callFunctionsAsNeeded(functionCalls);
2835
+ formattedContent = formatNewContent(functionResponseParts);
2836
+ }
2837
+ else {
2838
+ formattedContent = formatNewContent(request);
2839
+ }
2840
+ tempHistory.push(formattedContent);
2841
+ const formattedRequest = this._formatRequest(formattedContent, tempHistory);
2842
+ result = await generateContentStream(this._apiSettings, this.model, formattedRequest, this.chromeAdapter, {
2843
+ ...this.requestOptions,
2844
+ ...singleRequestOptions
2845
+ });
2846
+ functionCalls = this._getCallableFunctionCalls(result.firstValue);
2847
+ if (functionCalls &&
2848
+ result.firstValue &&
2849
+ result.firstValue.candidates &&
2850
+ result.firstValue.candidates.length > 0) {
2851
+ const responseContent = {
2852
+ ...result.firstValue.candidates[0].content
2853
+ };
2854
+ if (!responseContent.role) {
2855
+ responseContent.role = 'model';
2856
+ }
2857
+ tempHistory.push(responseContent);
2858
+ }
2859
+ } while (functionCalls && functionCallTurnCount < functionCallMaxTurns);
2860
+ if (functionCalls && functionCallTurnCount >= functionCallMaxTurns) {
2861
+ logger.warn(`Automatic function calling exceeded the limit of` +
2862
+ ` ${functionCallMaxTurns} function calls. Returning last model response.`);
2863
+ }
2864
+ return { stream: result.stream, response: result.response };
2865
+ };
2866
+ const streamPromise = callGenerateContentStream();
2867
+ // Add onto the chain.
2868
+ this._sendPromise = this._sendPromise
2869
+ .then(async () => streamPromise)
2870
+ // This must be handled to avoid unhandled rejection, but jump
2871
+ // to the final catch block with a label to not log this error.
2872
+ .catch(_ignored => {
2873
+ // If the initial fetch fails, the user's `streamPromise` rejects.
2874
+ // We swallow the error here to prevent double logging in the final catch.
2875
+ throw new Error(SILENT_ERROR);
2876
+ })
2877
+ .then(streamResult => streamResult.response)
2878
+ .then(response => {
2879
+ // This runs after the stream completes. Runtime errors here cannot be
2880
+ // caught by the user because their promise has likely already resolved.
2881
+ // TODO: Move response validation logic upstream to `stream-reader` so
2882
+ // errors propagate to the user's `result.response` promise.
2883
+ if (response.candidates && response.candidates.length > 0) {
2884
+ this._history = this._history.concat(tempHistory);
2885
+ // TODO: Validate that `response.candidates[0].content` is not null.
2886
+ const responseContent = { ...response.candidates[0].content };
2887
+ if (!responseContent.role) {
2888
+ responseContent.role = 'model';
2889
+ }
2890
+ this._history.push(responseContent);
2891
+ }
2892
+ else {
2893
+ const blockErrorMessage = formatBlockErrorMessage(response);
2894
+ if (blockErrorMessage) {
2895
+ logger.warn(`sendMessageStream() was unsuccessful. ${blockErrorMessage}. Inspect response object for details.`);
2896
+ }
2897
+ }
2898
+ })
2899
+ .catch(e => {
2900
+ // Filter out errors already handled by the user or initiated by them.
2901
+ if (e.message !== SILENT_ERROR && e.name !== 'AbortError') {
2902
+ logger.error(e);
2903
+ }
2904
+ });
2905
+ return streamPromise;
2906
+ }
2907
+ /**
2908
+ * Get function calls that the SDK has references to actually call.
2909
+ * This is all-or-nothing. If the model is requesting multiple
2910
+ * function calls, all of them must have references in order for
2911
+ * automatic function calling to work.
2912
+ *
2913
+ * @internal
2914
+ */
2915
+ _getCallableFunctionCalls(response) {
2916
+ const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
2917
+ if (!functionDeclarationsTool?.functionDeclarations) {
2918
+ return;
2919
+ }
2920
+ const functionCalls = getFunctionCalls(response);
2921
+ if (!functionCalls) {
2922
+ return;
2923
+ }
2924
+ for (const functionCall of functionCalls) {
2925
+ const hasFunctionReference = functionDeclarationsTool.functionDeclarations?.some(declaration => declaration.name === functionCall.name &&
2926
+ typeof declaration.functionReference === 'function');
2927
+ if (!hasFunctionReference) {
2928
+ return;
2929
+ }
2930
+ }
2931
+ return functionCalls;
2932
+ }
2933
+ /**
2934
+ * Call user-defined functions if requested by the model, and return
2935
+ * the response that should be sent to the model.
2936
+ * @internal
2937
+ */
2938
+ async _callFunctionsAsNeeded(functionCalls) {
2939
+ const activeCallList = new Map();
2940
+ const promiseList = [];
2941
+ const functionDeclarationsTool = this.params?.tools?.find(tool => tool.functionDeclarations);
2942
+ if (functionDeclarationsTool &&
2943
+ functionDeclarationsTool.functionDeclarations) {
2944
+ for (const functionCall of functionCalls) {
2945
+ const functionDeclaration = functionDeclarationsTool.functionDeclarations.find(declaration => declaration.name === functionCall.name);
2946
+ if (functionDeclaration?.functionReference) {
2947
+ const results = Promise.resolve(functionDeclaration.functionReference(functionCall.args)).catch(e => {
2948
+ const wrappedError = new AIError(AIErrorCode.ERROR, `Error in user-defined function "${functionDeclaration.name}": ${e.message}`);
2949
+ wrappedError.stack = e.stack;
2950
+ throw wrappedError;
2951
+ });
2952
+ activeCallList.set(functionCall.name, {
2953
+ id: functionCall.id,
2954
+ results
2955
+ });
2956
+ promiseList.push(results);
2957
+ }
2958
+ }
2959
+ // Wait for promises to finish.
2960
+ await Promise.all(promiseList);
2961
+ const functionResponseParts = [];
2962
+ for (const [name, callData] of activeCallList) {
2963
+ functionResponseParts.push({
2964
+ functionResponse: {
2965
+ name,
2966
+ response: await callData.results
2967
+ }
2968
+ });
2969
+ }
2970
+ return functionResponseParts;
2971
+ }
2972
+ else {
2973
+ throw new AIError(AIErrorCode.REQUEST_ERROR, `No function declarations were provided in "tools".`);
2974
+ }
2975
+ }
2976
+ }
2977
+
2978
+ /**
2979
+ * @license
2980
+ * Copyright 2024 Google LLC
2981
+ *
2982
+ * Licensed under the Apache License, Version 2.0 (the "License");
2983
+ * you may not use this file except in compliance with the License.
2984
+ * You may obtain a copy of the License at
2985
+ *
2986
+ * http://www.apache.org/licenses/LICENSE-2.0
2987
+ *
2988
+ * Unless required by applicable law or agreed to in writing, software
2989
+ * distributed under the License is distributed on an "AS IS" BASIS,
2990
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2991
+ * See the License for the specific language governing permissions and
2992
+ * limitations under the License.
2993
+ */
2994
+ async function countTokensOnCloud(apiSettings, model, params, singleRequestOptions) {
2995
+ let body = '';
2996
+ if (apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
2997
+ const mappedParams = mapCountTokensRequest(params, model);
2998
+ body = JSON.stringify(mappedParams);
2999
+ }
3000
+ else {
3001
+ body = JSON.stringify(params);
3002
+ }
3003
+ const response = await makeRequest({
3004
+ model,
3005
+ task: "countTokens" /* Task.COUNT_TOKENS */,
3006
+ apiSettings,
3007
+ stream: false,
3008
+ singleRequestOptions
3009
+ }, body);
3010
+ return response.json();
3011
+ }
3012
+ async function countTokens(apiSettings, model, params, chromeAdapter, requestOptions) {
3013
+ if (chromeAdapter?.mode === InferenceMode.ONLY_ON_DEVICE) {
3014
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'countTokens() is not supported for on-device models.');
3015
+ }
3016
+ return countTokensOnCloud(apiSettings, model, params, requestOptions);
3017
+ }
3018
+
3019
+ /**
3020
+ * @license
3021
+ * Copyright 2024 Google LLC
3022
+ *
3023
+ * Licensed under the Apache License, Version 2.0 (the "License");
3024
+ * you may not use this file except in compliance with the License.
3025
+ * You may obtain a copy of the License at
3026
+ *
3027
+ * http://www.apache.org/licenses/LICENSE-2.0
3028
+ *
3029
+ * Unless required by applicable law or agreed to in writing, software
3030
+ * distributed under the License is distributed on an "AS IS" BASIS,
3031
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3032
+ * See the License for the specific language governing permissions and
3033
+ * limitations under the License.
3034
+ */
3035
+ /**
3036
+ * Class for generative model APIs.
3037
+ * @public
3038
+ */
3039
+ class GenerativeModel extends AIModel {
3040
+ constructor(ai, modelParams, requestOptions, chromeAdapter) {
3041
+ super(ai, modelParams.model);
3042
+ this.chromeAdapter = chromeAdapter;
3043
+ this.generationConfig = modelParams.generationConfig || {};
3044
+ validateGenerationConfig(this.generationConfig);
3045
+ this.safetySettings = modelParams.safetySettings || [];
3046
+ this.tools = modelParams.tools;
3047
+ this.toolConfig = modelParams.toolConfig;
3048
+ this.systemInstruction = formatSystemInstruction(modelParams.systemInstruction);
3049
+ this.requestOptions = requestOptions || {};
3050
+ }
3051
+ /**
3052
+ * Makes a single non-streaming call to the model
3053
+ * and returns an object containing a single {@link GenerateContentResponse}.
3054
+ */
3055
+ async generateContent(request, singleRequestOptions) {
3056
+ const formattedParams = formatGenerateContentInput(request);
3057
+ return generateContent(this._apiSettings, this.model, {
3058
+ generationConfig: this.generationConfig,
3059
+ safetySettings: this.safetySettings,
3060
+ tools: this.tools,
3061
+ toolConfig: this.toolConfig,
3062
+ systemInstruction: this.systemInstruction,
3063
+ ...formattedParams
3064
+ }, this.chromeAdapter,
3065
+ // Merge request options
3066
+ {
3067
+ ...this.requestOptions,
3068
+ ...singleRequestOptions
3069
+ });
3070
+ }
3071
+ /**
3072
+ * Makes a single streaming call to the model
3073
+ * and returns an object containing an iterable stream that iterates
3074
+ * over all chunks in the streaming response as well as
3075
+ * a promise that returns the final aggregated response.
3076
+ */
3077
+ async generateContentStream(request, singleRequestOptions) {
3078
+ const formattedParams = formatGenerateContentInput(request);
3079
+ const { stream, response } = await generateContentStream(this._apiSettings, this.model, {
3080
+ generationConfig: this.generationConfig,
3081
+ safetySettings: this.safetySettings,
3082
+ tools: this.tools,
3083
+ toolConfig: this.toolConfig,
3084
+ systemInstruction: this.systemInstruction,
3085
+ ...formattedParams
3086
+ }, this.chromeAdapter,
3087
+ // Merge request options
3088
+ {
3089
+ ...this.requestOptions,
3090
+ ...singleRequestOptions
3091
+ });
3092
+ return { stream, response };
3093
+ }
3094
+ /**
3095
+ * Gets a new {@link ChatSession} instance which can be used for
3096
+ * multi-turn chats.
3097
+ */
3098
+ startChat(startChatParams) {
3099
+ return new ChatSession(this._apiSettings, this.model, this.chromeAdapter, {
3100
+ tools: this.tools,
3101
+ toolConfig: this.toolConfig,
3102
+ systemInstruction: this.systemInstruction,
3103
+ generationConfig: this.generationConfig,
3104
+ safetySettings: this.safetySettings,
3105
+ /**
3106
+ * Overrides params inherited from GenerativeModel with those explicitly set in the
3107
+ * StartChatParams. For example, if startChatParams.generationConfig is set, it'll override
3108
+ * this.generationConfig.
3109
+ */
3110
+ ...startChatParams
3111
+ }, this.requestOptions);
3112
+ }
3113
+ /**
3114
+ * Counts the tokens in the provided request.
3115
+ */
3116
+ async countTokens(request, singleRequestOptions) {
3117
+ const formattedParams = formatGenerateContentInput(request);
3118
+ return countTokens(this._apiSettings, this.model, formattedParams, this.chromeAdapter,
3119
+ // Merge request options
3120
+ {
3121
+ ...this.requestOptions,
3122
+ ...singleRequestOptions
3123
+ });
3124
+ }
3125
+ }
3126
+ /**
3127
+ * Client-side validation of some common `GenerationConfig` pitfalls, in order
3128
+ * to save the developer a wasted request.
3129
+ */
3130
+ function validateGenerationConfig(generationConfig) {
3131
+ if (
3132
+ // != allows for null and undefined. 0 is considered "set" by the model
3133
+ generationConfig.thinkingConfig?.thinkingBudget != null &&
3134
+ generationConfig.thinkingConfig?.thinkingLevel) {
3135
+ throw new AIError(AIErrorCode.UNSUPPORTED, `Cannot set both thinkingBudget and thinkingLevel in a config.`);
3136
+ }
3137
+ }
3138
+
3139
+ /**
3140
+ * @license
3141
+ * Copyright 2025 Google LLC
3142
+ *
3143
+ * Licensed under the Apache License, Version 2.0 (the "License");
3144
+ * you may not use this file except in compliance with the License.
3145
+ * You may obtain a copy of the License at
3146
+ *
3147
+ * http://www.apache.org/licenses/LICENSE-2.0
3148
+ *
3149
+ * Unless required by applicable law or agreed to in writing, software
3150
+ * distributed under the License is distributed on an "AS IS" BASIS,
3151
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3152
+ * See the License for the specific language governing permissions and
3153
+ * limitations under the License.
3154
+ */
3155
+ /**
3156
+ * Represents an active, real-time, bidirectional conversation with the model.
3157
+ *
3158
+ * This class should only be instantiated by calling {@link LiveGenerativeModel.connect}.
3159
+ *
3160
+ * @beta
3161
+ */
3162
+ class LiveSession {
3163
+ /**
3164
+ * @internal
3165
+ */
3166
+ constructor(webSocketHandler, serverMessages) {
3167
+ this.webSocketHandler = webSocketHandler;
3168
+ this.serverMessages = serverMessages;
3169
+ /**
3170
+ * Indicates whether this Live session is closed.
3171
+ *
3172
+ * @beta
3173
+ */
3174
+ this.isClosed = false;
3175
+ /**
3176
+ * Indicates whether this Live session is being controlled by an `AudioConversationController`.
3177
+ *
3178
+ * @beta
3179
+ */
3180
+ this.inConversation = false;
3181
+ }
3182
+ /**
3183
+ * Sends content to the server.
3184
+ *
3185
+ * @param request - The message to send to the model.
3186
+ * @param turnComplete - Indicates if the turn is complete. Defaults to false.
3187
+ * @throws If this session has been closed.
3188
+ *
3189
+ * @beta
3190
+ */
3191
+ async send(request, turnComplete = true) {
3192
+ if (this.isClosed) {
3193
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3194
+ }
3195
+ const newContent = formatNewContent(request);
3196
+ const message = {
3197
+ clientContent: {
3198
+ turns: [newContent],
3199
+ turnComplete
3200
+ }
3201
+ };
3202
+ this.webSocketHandler.send(JSON.stringify(message));
3203
+ }
3204
+ /**
3205
+ * Sends text to the server in realtime.
3206
+ *
3207
+ * @example
3208
+ * ```javascript
3209
+ * liveSession.sendTextRealtime("Hello, how are you?");
3210
+ * ```
3211
+ *
3212
+ * @param text - The text data to send.
3213
+ * @throws If this session has been closed.
3214
+ *
3215
+ * @beta
3216
+ */
3217
+ async sendTextRealtime(text) {
3218
+ if (this.isClosed) {
3219
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3220
+ }
3221
+ const message = {
3222
+ realtimeInput: {
3223
+ text
3224
+ }
3225
+ };
3226
+ this.webSocketHandler.send(JSON.stringify(message));
3227
+ }
3228
+ /**
3229
+ * Sends audio data to the server in realtime.
3230
+ *
3231
+ * @remarks The server requires that the audio data is base64-encoded 16-bit PCM at 16kHz
3232
+ * little-endian.
3233
+ *
3234
+ * @example
3235
+ * ```javascript
3236
+ * // const pcmData = ... base64-encoded 16-bit PCM at 16kHz little-endian.
3237
+ * const blob = { mimeType: "audio/pcm", data: pcmData };
3238
+ * liveSession.sendAudioRealtime(blob);
3239
+ * ```
3240
+ *
3241
+ * @param blob - The base64-encoded PCM data to send to the server in realtime.
3242
+ * @throws If this session has been closed.
3243
+ *
3244
+ * @beta
3245
+ */
3246
+ async sendAudioRealtime(blob) {
3247
+ if (this.isClosed) {
3248
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3249
+ }
3250
+ const message = {
3251
+ realtimeInput: {
3252
+ audio: blob
3253
+ }
3254
+ };
3255
+ this.webSocketHandler.send(JSON.stringify(message));
3256
+ }
3257
+ /**
3258
+ * Sends video data to the server in realtime.
3259
+ *
3260
+ * @remarks The server requires that the video is sent as individual video frames at 1 FPS. It
3261
+ * is recommended to set `mimeType` to `image/jpeg`.
3262
+ *
3263
+ * @example
3264
+ * ```javascript
3265
+ * // const videoFrame = ... base64-encoded JPEG data
3266
+ * const blob = { mimeType: "image/jpeg", data: videoFrame };
3267
+ * liveSession.sendVideoRealtime(blob);
3268
+ * ```
3269
+ * @param blob - The base64-encoded video data to send to the server in realtime.
3270
+ * @throws If this session has been closed.
3271
+ *
3272
+ * @beta
3273
+ */
3274
+ async sendVideoRealtime(blob) {
3275
+ if (this.isClosed) {
3276
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3277
+ }
3278
+ const message = {
3279
+ realtimeInput: {
3280
+ video: blob
3281
+ }
3282
+ };
3283
+ this.webSocketHandler.send(JSON.stringify(message));
3284
+ }
3285
+ /**
3286
+ * Sends function responses to the server.
3287
+ *
3288
+ * @param functionResponses - The function responses to send.
3289
+ * @throws If this session has been closed.
3290
+ *
3291
+ * @beta
3292
+ */
3293
+ async sendFunctionResponses(functionResponses) {
3294
+ if (this.isClosed) {
3295
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3296
+ }
3297
+ const message = {
3298
+ toolResponse: {
3299
+ functionResponses
3300
+ }
3301
+ };
3302
+ this.webSocketHandler.send(JSON.stringify(message));
3303
+ }
3304
+ /**
3305
+ * Yields messages received from the server.
3306
+ * This can only be used by one consumer at a time.
3307
+ *
3308
+ * @returns An `AsyncGenerator` that yields server messages as they arrive.
3309
+ * @throws If the session is already closed, or if we receive a response that we don't support.
3310
+ *
3311
+ * @beta
3312
+ */
3313
+ async *receive() {
3314
+ if (this.isClosed) {
3315
+ throw new AIError(AIErrorCode.SESSION_CLOSED, 'Cannot read from a Live session that is closed. Try starting a new Live session.');
3316
+ }
3317
+ for await (const message of this.serverMessages) {
3318
+ if (message && typeof message === 'object') {
3319
+ if (LiveResponseType.SERVER_CONTENT in message) {
3320
+ yield {
3321
+ type: 'serverContent',
3322
+ ...message
3323
+ .serverContent
3324
+ };
3325
+ }
3326
+ else if (LiveResponseType.TOOL_CALL in message) {
3327
+ yield {
3328
+ type: 'toolCall',
3329
+ ...message
3330
+ .toolCall
3331
+ };
3332
+ }
3333
+ else if (LiveResponseType.TOOL_CALL_CANCELLATION in message) {
3334
+ yield {
3335
+ type: 'toolCallCancellation',
3336
+ ...message.toolCallCancellation
3337
+ };
3338
+ }
3339
+ else if ('goAway' in message) {
3340
+ const notice = message.goAway;
3341
+ yield {
3342
+ type: LiveResponseType.GOING_AWAY_NOTICE,
3343
+ timeLeft: parseDuration(notice.timeLeft)
3344
+ };
3345
+ }
3346
+ else {
3347
+ logger.warn(`Received an unknown message type from the server: ${JSON.stringify(message)}`);
3348
+ }
3349
+ }
3350
+ else {
3351
+ logger.warn(`Received an invalid message from the server: ${JSON.stringify(message)}`);
3352
+ }
3353
+ }
3354
+ }
3355
+ /**
3356
+ * Closes this session.
3357
+ * All methods on this session will throw an error once this resolves.
3358
+ *
3359
+ * @beta
3360
+ */
3361
+ async close() {
3362
+ if (!this.isClosed) {
3363
+ this.isClosed = true;
3364
+ await this.webSocketHandler.close(1000, 'Client closed session.');
3365
+ }
3366
+ }
3367
+ /**
3368
+ * Sends realtime input to the server.
3369
+ *
3370
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
3371
+ *
3372
+ * @param mediaChunks - The media chunks to send.
3373
+ * @throws If this session has been closed.
3374
+ *
3375
+ * @beta
3376
+ */
3377
+ async sendMediaChunks(mediaChunks) {
3378
+ if (this.isClosed) {
3379
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3380
+ }
3381
+ // The backend does not support sending more than one mediaChunk in one message.
3382
+ // Work around this limitation by sending mediaChunks in separate messages.
3383
+ mediaChunks.forEach(mediaChunk => {
3384
+ const message = {
3385
+ realtimeInput: { mediaChunks: [mediaChunk] }
3386
+ };
3387
+ this.webSocketHandler.send(JSON.stringify(message));
3388
+ });
3389
+ }
3390
+ /**
3391
+ * @deprecated Use `sendTextRealtime()`, `sendAudioRealtime()`, and `sendVideoRealtime()` instead.
3392
+ *
3393
+ * Sends a stream of {@link GenerativeContentBlob}.
3394
+ *
3395
+ * @param mediaChunkStream - The stream of {@link GenerativeContentBlob} to send.
3396
+ * @throws If this session has been closed.
3397
+ *
3398
+ * @beta
3399
+ */
3400
+ async sendMediaStream(mediaChunkStream) {
3401
+ if (this.isClosed) {
3402
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'This LiveSession has been closed and cannot be used.');
3403
+ }
3404
+ const reader = mediaChunkStream.getReader();
3405
+ while (true) {
3406
+ try {
3407
+ const { done, value } = await reader.read();
3408
+ if (done) {
3409
+ break;
3410
+ }
3411
+ else if (!value) {
3412
+ throw new Error('Missing chunk in reader, but reader is not done.');
3413
+ }
3414
+ await this.sendMediaChunks([value]);
3415
+ }
3416
+ catch (e) {
3417
+ // Re-throw any errors that occur during stream consumption or sending.
3418
+ const message = e instanceof Error ? e.message : 'Error processing media stream.';
3419
+ throw new AIError(AIErrorCode.REQUEST_ERROR, message);
3420
+ }
3421
+ }
3422
+ }
3423
+ }
3424
+ /**
3425
+ * Parses a duration string (e.g. "3.000000001s") into a number of seconds.
3426
+ *
3427
+ * @param duration - The duration string to parse.
3428
+ * @returns The duration in seconds.
3429
+ */
3430
+ function parseDuration(duration) {
3431
+ if (!duration || !duration.endsWith('s')) {
3432
+ return 0;
3433
+ }
3434
+ return Number(duration.slice(0, -1)); // slice removes the trailing 's'.
3435
+ }
3436
+
3437
+ /**
3438
+ * @license
3439
+ * Copyright 2025 Google LLC
3440
+ *
3441
+ * Licensed under the Apache License, Version 2.0 (the "License");
3442
+ * you may not use this file except in compliance with the License.
3443
+ * You may obtain a copy of the License at
3444
+ *
3445
+ * http://www.apache.org/licenses/LICENSE-2.0
3446
+ *
3447
+ * Unless required by applicable law or agreed to in writing, software
3448
+ * distributed under the License is distributed on an "AS IS" BASIS,
3449
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3450
+ * See the License for the specific language governing permissions and
3451
+ * limitations under the License.
3452
+ */
3453
+ /**
3454
+ * Class for Live generative model APIs. The Live API enables low-latency, two-way multimodal
3455
+ * interactions with Gemini.
3456
+ *
3457
+ * This class should only be instantiated with {@link getLiveGenerativeModel}.
3458
+ *
3459
+ * @beta
3460
+ */
3461
+ class LiveGenerativeModel extends AIModel {
3462
+ /**
3463
+ * @internal
3464
+ */
3465
+ constructor(ai, modelParams,
3466
+ /**
3467
+ * @internal
3468
+ */
3469
+ _webSocketHandler) {
3470
+ super(ai, modelParams.model);
3471
+ this._webSocketHandler = _webSocketHandler;
3472
+ this.generationConfig = modelParams.generationConfig || {};
3473
+ this.tools = modelParams.tools;
3474
+ this.toolConfig = modelParams.toolConfig;
3475
+ this.systemInstruction = formatSystemInstruction(modelParams.systemInstruction);
3476
+ }
3477
+ /**
3478
+ * Starts a {@link LiveSession}.
3479
+ *
3480
+ * @returns A {@link LiveSession}.
3481
+ * @throws If the connection failed to be established with the server.
3482
+ *
3483
+ * @beta
3484
+ */
3485
+ async connect() {
3486
+ const url = new WebSocketUrl(this._apiSettings);
3487
+ await this._webSocketHandler.connect(url.toString());
3488
+ let fullModelPath;
3489
+ if (this._apiSettings.backend.backendType === BackendType.GOOGLE_AI) {
3490
+ fullModelPath = `projects/${this._apiSettings.project}/${this.model}`;
3491
+ }
3492
+ else {
3493
+ fullModelPath = `projects/${this._apiSettings.project}/locations/${this._apiSettings.location}/${this.model}`;
3494
+ }
3495
+ // inputAudioTranscription and outputAudioTranscription are on the generation config in the public API,
3496
+ // but the backend expects them to be in the `setup` message.
3497
+ const { inputAudioTranscription, outputAudioTranscription, ...generationConfig } = this.generationConfig;
3498
+ const setupMessage = {
3499
+ setup: {
3500
+ model: fullModelPath,
3501
+ generationConfig,
3502
+ tools: this.tools,
3503
+ toolConfig: this.toolConfig,
3504
+ systemInstruction: this.systemInstruction,
3505
+ inputAudioTranscription,
3506
+ outputAudioTranscription
3507
+ }
3508
+ };
3509
+ try {
3510
+ // Begin listening for server messages, and begin the handshake by sending the 'setupMessage'
3511
+ const serverMessages = this._webSocketHandler.listen();
3512
+ this._webSocketHandler.send(JSON.stringify(setupMessage));
3513
+ // Verify we received the handshake response 'setupComplete'
3514
+ const firstMessage = (await serverMessages.next()).value;
3515
+ if (!firstMessage ||
3516
+ !(typeof firstMessage === 'object') ||
3517
+ !('setupComplete' in firstMessage)) {
3518
+ await this._webSocketHandler.close(1011, 'Handshake failure');
3519
+ throw new AIError(AIErrorCode.RESPONSE_ERROR, 'Server connection handshake failed. The server did not respond with a setupComplete message.');
3520
+ }
3521
+ return new LiveSession(this._webSocketHandler, serverMessages);
3522
+ }
3523
+ catch (e) {
3524
+ // Ensure connection is closed on any setup error
3525
+ await this._webSocketHandler.close();
3526
+ throw e;
3527
+ }
3528
+ }
3529
+ }
3530
+
3531
+ /**
3532
+ * @license
3533
+ * Copyright 2025 Google LLC
3534
+ *
3535
+ * Licensed under the Apache License, Version 2.0 (the "License");
3536
+ * you may not use this file except in compliance with the License.
3537
+ * You may obtain a copy of the License at
3538
+ *
3539
+ * http://www.apache.org/licenses/LICENSE-2.0
3540
+ *
3541
+ * Unless required by applicable law or agreed to in writing, software
3542
+ * distributed under the License is distributed on an "AS IS" BASIS,
3543
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3544
+ * See the License for the specific language governing permissions and
3545
+ * limitations under the License.
3546
+ */
3547
+ /**
3548
+ * Class for Imagen model APIs.
3549
+ *
3550
+ * This class provides methods for generating images using the Imagen model.
3551
+ *
3552
+ * @example
3553
+ * ```javascript
3554
+ * const imagen = new ImagenModel(
3555
+ * ai,
3556
+ * {
3557
+ * model: 'imagen-3.0-generate-002'
3558
+ * }
3559
+ * );
3560
+ *
3561
+ * const response = await imagen.generateImages('A photo of a cat');
3562
+ * if (response.images.length > 0) {
3563
+ * console.log(response.images[0].bytesBase64Encoded);
3564
+ * }
3565
+ * ```
3566
+ *
3567
+ * @public
3568
+ */
3569
+ class ImagenModel extends AIModel {
3570
+ /**
3571
+ * Constructs a new instance of the {@link ImagenModel} class.
3572
+ *
3573
+ * @param ai - an {@link AI} instance.
3574
+ * @param modelParams - Parameters to use when making requests to Imagen.
3575
+ * @param requestOptions - Additional options to use when making requests.
3576
+ *
3577
+ * @throws If the `apiKey` or `projectId` fields are missing in your
3578
+ * Firebase config.
3579
+ */
3580
+ constructor(ai, modelParams, requestOptions) {
3581
+ const { model, generationConfig, safetySettings } = modelParams;
3582
+ super(ai, model);
3583
+ this.requestOptions = requestOptions;
3584
+ this.generationConfig = generationConfig;
3585
+ this.safetySettings = safetySettings;
3586
+ }
3587
+ /**
3588
+ * Generates images using the Imagen model and returns them as
3589
+ * base64-encoded strings.
3590
+ *
3591
+ * @param prompt - A text prompt describing the image(s) to generate.
3592
+ * @returns A promise that resolves to an {@link ImagenGenerationResponse}
3593
+ * object containing the generated images.
3594
+ *
3595
+ * @throws If the request to generate images fails. This happens if the
3596
+ * prompt is blocked.
3597
+ *
3598
+ * @remarks
3599
+ * If the prompt was not blocked, but one or more of the generated images were filtered, the
3600
+ * returned object will have a `filteredReason` property.
3601
+ * If all images are filtered, the `images` array will be empty.
3602
+ *
3603
+ * @public
3604
+ */
3605
+ async generateImages(prompt, singleRequestOptions) {
3606
+ const body = createPredictRequestBody(prompt, {
3607
+ ...this.generationConfig,
3608
+ ...this.safetySettings
3609
+ });
3610
+ const response = await makeRequest({
3611
+ task: "predict" /* Task.PREDICT */,
3612
+ model: this.model,
3613
+ apiSettings: this._apiSettings,
3614
+ stream: false,
3615
+ // Merge request options. Single request options overwrite the model's request options.
3616
+ singleRequestOptions: {
3617
+ ...this.requestOptions,
3618
+ ...singleRequestOptions
3619
+ }
3620
+ }, JSON.stringify(body));
3621
+ return handlePredictResponse(response);
3622
+ }
3623
+ /**
3624
+ * Generates images to Cloud Storage for Firebase using the Imagen model.
3625
+ *
3626
+ * @internal This method is temporarily internal.
3627
+ *
3628
+ * @param prompt - A text prompt describing the image(s) to generate.
3629
+ * @param gcsURI - The URI of file stored in a Cloud Storage for Firebase bucket.
3630
+ * This should be a directory. For example, `gs://my-bucket/my-directory/`.
3631
+ * @returns A promise that resolves to an {@link ImagenGenerationResponse}
3632
+ * object containing the URLs of the generated images.
3633
+ *
3634
+ * @throws If the request fails to generate images fails. This happens if
3635
+ * the prompt is blocked.
3636
+ *
3637
+ * @remarks
3638
+ * If the prompt was not blocked, but one or more of the generated images were filtered, the
3639
+ * returned object will have a `filteredReason` property.
3640
+ * If all images are filtered, the `images` array will be empty.
3641
+ */
3642
+ async generateImagesGCS(prompt, gcsURI, singleRequestOptions) {
3643
+ const body = createPredictRequestBody(prompt, {
3644
+ gcsURI,
3645
+ ...this.generationConfig,
3646
+ ...this.safetySettings
3647
+ });
3648
+ const response = await makeRequest({
3649
+ task: "predict" /* Task.PREDICT */,
3650
+ model: this.model,
3651
+ apiSettings: this._apiSettings,
3652
+ stream: false,
3653
+ // Merge request options. Single request options overwrite the model's request options.
3654
+ singleRequestOptions: {
3655
+ ...this.requestOptions,
3656
+ ...singleRequestOptions
3657
+ }
3658
+ }, JSON.stringify(body));
3659
+ return handlePredictResponse(response);
3660
+ }
3661
+ }
3662
+
3663
+ /**
3664
+ * @license
3665
+ * Copyright 2025 Google LLC
3666
+ *
3667
+ * Licensed under the Apache License, Version 2.0 (the "License");
3668
+ * you may not use this file except in compliance with the License.
3669
+ * You may obtain a copy of the License at
3670
+ *
3671
+ * http://www.apache.org/licenses/LICENSE-2.0
3672
+ *
3673
+ * Unless required by applicable law or agreed to in writing, software
3674
+ * distributed under the License is distributed on an "AS IS" BASIS,
3675
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3676
+ * See the License for the specific language governing permissions and
3677
+ * limitations under the License.
3678
+ */
3679
+ /**
3680
+ * A wrapper for the native `WebSocket` available in both Browsers and Node >= 22.
3681
+ *
3682
+ * @internal
3683
+ */
3684
+ class WebSocketHandlerImpl {
3685
+ constructor() {
3686
+ if (typeof WebSocket === 'undefined') {
3687
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'The WebSocket API is not available in this environment. ' +
3688
+ 'The "Live" feature is not supported here. It is supported in ' +
3689
+ 'modern browser windows, Web Workers with WebSocket support, and Node >= 22.');
3690
+ }
3691
+ }
3692
+ connect(url) {
3693
+ return new Promise((resolve, reject) => {
3694
+ this.ws = new WebSocket(url);
3695
+ this.ws.binaryType = 'blob'; // Only important to set in Node
3696
+ this.ws.addEventListener('open', () => resolve(), { once: true });
3697
+ this.ws.addEventListener('error', () => reject(new AIError(AIErrorCode.FETCH_ERROR, `Error event raised on WebSocket`)), { once: true });
3698
+ this.ws.addEventListener('close', (closeEvent) => {
3699
+ if (closeEvent.reason) {
3700
+ logger.warn(`WebSocket connection closed by server. Reason: '${closeEvent.reason}'`);
3701
+ }
3702
+ });
3703
+ });
3704
+ }
3705
+ send(data) {
3706
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
3707
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'WebSocket is not open.');
3708
+ }
3709
+ this.ws.send(data);
3710
+ }
3711
+ async *listen() {
3712
+ if (!this.ws) {
3713
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'WebSocket is not connected.');
3714
+ }
3715
+ const messageQueue = [];
3716
+ const errorQueue = [];
3717
+ let resolvePromise = null;
3718
+ let isClosed = false;
3719
+ const messageListener = async (event) => {
3720
+ let data;
3721
+ if (event.data instanceof Blob) {
3722
+ data = await event.data.text();
3723
+ }
3724
+ else if (typeof event.data === 'string') {
3725
+ data = event.data;
3726
+ }
3727
+ else {
3728
+ errorQueue.push(new AIError(AIErrorCode.PARSE_FAILED, `Failed to parse WebSocket response. Expected data to be a Blob or string, but was ${typeof event.data}.`));
3729
+ if (resolvePromise) {
3730
+ resolvePromise();
3731
+ resolvePromise = null;
3732
+ }
3733
+ return;
3734
+ }
3735
+ try {
3736
+ const obj = JSON.parse(data);
3737
+ messageQueue.push(obj);
3738
+ }
3739
+ catch (e) {
3740
+ const err = e;
3741
+ errorQueue.push(new AIError(AIErrorCode.PARSE_FAILED, `Error parsing WebSocket message to JSON: ${err.message}`));
3742
+ }
3743
+ if (resolvePromise) {
3744
+ resolvePromise();
3745
+ resolvePromise = null;
3746
+ }
3747
+ };
3748
+ const errorListener = () => {
3749
+ errorQueue.push(new AIError(AIErrorCode.FETCH_ERROR, 'WebSocket connection error.'));
3750
+ if (resolvePromise) {
3751
+ resolvePromise();
3752
+ resolvePromise = null;
3753
+ }
3754
+ };
3755
+ const closeListener = (event) => {
3756
+ if (event.reason) {
3757
+ logger.warn(`WebSocket connection closed by the server with reason: ${event.reason}`);
3758
+ }
3759
+ isClosed = true;
3760
+ if (resolvePromise) {
3761
+ resolvePromise();
3762
+ resolvePromise = null;
3763
+ }
3764
+ // Clean up listeners to prevent memory leaks
3765
+ this.ws?.removeEventListener('message', messageListener);
3766
+ this.ws?.removeEventListener('close', closeListener);
3767
+ this.ws?.removeEventListener('error', errorListener);
3768
+ };
3769
+ this.ws.addEventListener('message', messageListener);
3770
+ this.ws.addEventListener('close', closeListener);
3771
+ this.ws.addEventListener('error', errorListener);
3772
+ while (!isClosed) {
3773
+ if (errorQueue.length > 0) {
3774
+ const error = errorQueue.shift();
3775
+ throw error;
3776
+ }
3777
+ if (messageQueue.length > 0) {
3778
+ yield messageQueue.shift();
3779
+ }
3780
+ else {
3781
+ await new Promise(resolve => {
3782
+ resolvePromise = resolve;
3783
+ });
3784
+ }
3785
+ }
3786
+ // If the loop terminated because isClosed is true, check for any final errors
3787
+ if (errorQueue.length > 0) {
3788
+ const error = errorQueue.shift();
3789
+ throw error;
3790
+ }
3791
+ }
3792
+ close(code, reason) {
3793
+ return new Promise(resolve => {
3794
+ if (!this.ws) {
3795
+ return resolve();
3796
+ }
3797
+ this.ws.addEventListener('close', () => resolve(), { once: true });
3798
+ // Calling 'close' during these states results in an error.
3799
+ if (this.ws.readyState === WebSocket.CLOSED ||
3800
+ this.ws.readyState === WebSocket.CONNECTING) {
3801
+ return resolve();
3802
+ }
3803
+ if (this.ws.readyState !== WebSocket.CLOSING) {
3804
+ this.ws.close(code, reason);
3805
+ }
3806
+ });
3807
+ }
3808
+ }
3809
+
3810
+ /**
3811
+ * @license
3812
+ * Copyright 2025 Google LLC
3813
+ *
3814
+ * Licensed under the Apache License, Version 2.0 (the "License");
3815
+ * you may not use this file except in compliance with the License.
3816
+ * You may obtain a copy of the License at
3817
+ *
3818
+ * http://www.apache.org/licenses/LICENSE-2.0
3819
+ *
3820
+ * Unless required by applicable law or agreed to in writing, software
3821
+ * distributed under the License is distributed on an "AS IS" BASIS,
3822
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3823
+ * See the License for the specific language governing permissions and
3824
+ * limitations under the License.
3825
+ */
3826
+ /**
3827
+ * {@link GenerativeModel} APIs that execute on a server-side template.
3828
+ *
3829
+ * This class should only be instantiated with {@link getTemplateGenerativeModel}.
3830
+ *
3831
+ * @beta
3832
+ */
3833
+ class TemplateGenerativeModel {
3834
+ /**
3835
+ * @hideconstructor
3836
+ */
3837
+ constructor(ai, requestOptions) {
3838
+ this.requestOptions = requestOptions || {};
3839
+ this._apiSettings = initApiSettings(ai);
3840
+ }
3841
+ /**
3842
+ * Makes a single non-streaming call to the model and returns an object
3843
+ * containing a single {@link GenerateContentResponse}.
3844
+ *
3845
+ * @param templateId - The ID of the server-side template to execute.
3846
+ * @param templateVariables - A key-value map of variables to populate the
3847
+ * template with.
3848
+ *
3849
+ * @beta
3850
+ */
3851
+ async generateContent(templateId, templateVariables, singleRequestOptions) {
3852
+ return templateGenerateContent(this._apiSettings, templateId, { inputs: templateVariables }, {
3853
+ ...this.requestOptions,
3854
+ ...singleRequestOptions
3855
+ });
3856
+ }
3857
+ /**
3858
+ * Makes a single streaming call to the model and returns an object
3859
+ * containing an iterable stream that iterates over all chunks in the
3860
+ * streaming response as well as a promise that returns the final aggregated
3861
+ * response.
3862
+ *
3863
+ * @param templateId - The ID of the server-side template to execute.
3864
+ * @param templateVariables - A key-value map of variables to populate the
3865
+ * template with.
3866
+ *
3867
+ * @beta
3868
+ */
3869
+ async generateContentStream(templateId, templateVariables, singleRequestOptions) {
3870
+ return templateGenerateContentStream(this._apiSettings, templateId, { inputs: templateVariables }, {
3871
+ ...this.requestOptions,
3872
+ ...singleRequestOptions
3873
+ });
3874
+ }
3875
+ }
3876
+
3877
+ /**
3878
+ * @license
3879
+ * Copyright 2025 Google LLC
3880
+ *
3881
+ * Licensed under the Apache License, Version 2.0 (the "License");
3882
+ * you may not use this file except in compliance with the License.
3883
+ * You may obtain a copy of the License at
3884
+ *
3885
+ * http://www.apache.org/licenses/LICENSE-2.0
3886
+ *
3887
+ * Unless required by applicable law or agreed to in writing, software
3888
+ * distributed under the License is distributed on an "AS IS" BASIS,
3889
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3890
+ * See the License for the specific language governing permissions and
3891
+ * limitations under the License.
3892
+ */
3893
+ /**
3894
+ * Class for Imagen model APIs that execute on a server-side template.
3895
+ *
3896
+ * This class should only be instantiated with {@link getTemplateImagenModel}.
3897
+ *
3898
+ * @beta
3899
+ */
3900
+ class TemplateImagenModel {
3901
+ /**
3902
+ * @hideconstructor
3903
+ */
3904
+ constructor(ai, requestOptions) {
3905
+ this.requestOptions = requestOptions || {};
3906
+ this._apiSettings = initApiSettings(ai);
3907
+ }
3908
+ /**
3909
+ * Makes a single call to the model and returns an object containing a single
3910
+ * {@link ImagenGenerationResponse}.
3911
+ *
3912
+ * @param templateId - The ID of the server-side template to execute.
3913
+ * @param templateVariables - A key-value map of variables to populate the
3914
+ * template with.
3915
+ *
3916
+ * @beta
3917
+ */
3918
+ async generateImages(templateId, templateVariables, singleRequestOptions) {
3919
+ const response = await makeRequest({
3920
+ task: "templatePredict" /* ServerPromptTemplateTask.TEMPLATE_PREDICT */,
3921
+ templateId,
3922
+ apiSettings: this._apiSettings,
3923
+ stream: false,
3924
+ singleRequestOptions: {
3925
+ ...this.requestOptions,
3926
+ ...singleRequestOptions
3927
+ }
3928
+ }, JSON.stringify({ inputs: templateVariables }));
3929
+ return handlePredictResponse(response);
3930
+ }
3931
+ }
3932
+
3933
+ /**
3934
+ * @license
3935
+ * Copyright 2024 Google LLC
3936
+ *
3937
+ * Licensed under the Apache License, Version 2.0 (the "License");
3938
+ * you may not use this file except in compliance with the License.
3939
+ * You may obtain a copy of the License at
3940
+ *
3941
+ * http://www.apache.org/licenses/LICENSE-2.0
3942
+ *
3943
+ * Unless required by applicable law or agreed to in writing, software
3944
+ * distributed under the License is distributed on an "AS IS" BASIS,
3945
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3946
+ * See the License for the specific language governing permissions and
3947
+ * limitations under the License.
3948
+ */
3949
+ /**
3950
+ * Parent class encompassing all Schema types, with static methods that
3951
+ * allow building specific Schema types. This class can be converted with
3952
+ * `JSON.stringify()` into a JSON string accepted by Vertex AI REST endpoints.
3953
+ * (This string conversion is automatically done when calling SDK methods.)
3954
+ * @public
3955
+ */
3956
+ class Schema {
3957
+ constructor(schemaParams) {
3958
+ // TODO(dlarocque): Enforce this with union types
3959
+ if (!schemaParams.type && !schemaParams.anyOf) {
3960
+ throw new AIError(AIErrorCode.INVALID_SCHEMA, "A schema must have either a 'type' or an 'anyOf' array of sub-schemas.");
3961
+ }
3962
+ // eslint-disable-next-line guard-for-in
3963
+ for (const paramKey in schemaParams) {
3964
+ this[paramKey] = schemaParams[paramKey];
3965
+ }
3966
+ // Ensure these are explicitly set to avoid TS errors.
3967
+ this.type = schemaParams.type;
3968
+ this.format = schemaParams.hasOwnProperty('format')
3969
+ ? schemaParams.format
3970
+ : undefined;
3971
+ this.nullable = schemaParams.hasOwnProperty('nullable')
3972
+ ? !!schemaParams.nullable
3973
+ : false;
3974
+ }
3975
+ /**
3976
+ * Defines how this Schema should be serialized as JSON.
3977
+ * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior
3978
+ * @internal
3979
+ */
3980
+ toJSON() {
3981
+ const obj = {
3982
+ type: this.type
3983
+ };
3984
+ for (const prop in this) {
3985
+ if (this.hasOwnProperty(prop) && this[prop] !== undefined) {
3986
+ if (prop !== 'required' || this.type === SchemaType.OBJECT) {
3987
+ obj[prop] = this[prop];
3988
+ }
3989
+ }
3990
+ }
3991
+ return obj;
3992
+ }
3993
+ static array(arrayParams) {
3994
+ return new ArraySchema(arrayParams, arrayParams.items);
3995
+ }
3996
+ static object(objectParams) {
3997
+ return new ObjectSchema(objectParams, objectParams.properties, objectParams.optionalProperties);
3998
+ }
3999
+ // eslint-disable-next-line id-blacklist
4000
+ static string(stringParams) {
4001
+ return new StringSchema(stringParams);
4002
+ }
4003
+ static enumString(stringParams) {
4004
+ return new StringSchema(stringParams, stringParams.enum);
4005
+ }
4006
+ static integer(integerParams) {
4007
+ return new IntegerSchema(integerParams);
4008
+ }
4009
+ // eslint-disable-next-line id-blacklist
4010
+ static number(numberParams) {
4011
+ return new NumberSchema(numberParams);
4012
+ }
4013
+ // eslint-disable-next-line id-blacklist
4014
+ static boolean(booleanParams) {
4015
+ return new BooleanSchema(booleanParams);
4016
+ }
4017
+ static anyOf(anyOfParams) {
4018
+ return new AnyOfSchema(anyOfParams);
4019
+ }
4020
+ }
4021
+ /**
4022
+ * Schema class for "integer" types.
4023
+ * @public
4024
+ */
4025
+ class IntegerSchema extends Schema {
4026
+ constructor(schemaParams) {
4027
+ super({
4028
+ type: SchemaType.INTEGER,
4029
+ ...schemaParams
4030
+ });
4031
+ }
4032
+ }
4033
+ /**
4034
+ * Schema class for "number" types.
4035
+ * @public
4036
+ */
4037
+ class NumberSchema extends Schema {
4038
+ constructor(schemaParams) {
4039
+ super({
4040
+ type: SchemaType.NUMBER,
4041
+ ...schemaParams
4042
+ });
4043
+ }
4044
+ }
4045
+ /**
4046
+ * Schema class for "boolean" types.
4047
+ * @public
4048
+ */
4049
+ class BooleanSchema extends Schema {
4050
+ constructor(schemaParams) {
4051
+ super({
4052
+ type: SchemaType.BOOLEAN,
4053
+ ...schemaParams
4054
+ });
4055
+ }
4056
+ }
4057
+ /**
4058
+ * Schema class for "string" types. Can be used with or without
4059
+ * enum values.
4060
+ * @public
4061
+ */
4062
+ class StringSchema extends Schema {
4063
+ constructor(schemaParams, enumValues) {
4064
+ super({
4065
+ type: SchemaType.STRING,
4066
+ ...schemaParams
4067
+ });
4068
+ this.enum = enumValues;
4069
+ }
4070
+ /**
4071
+ * @internal
4072
+ */
4073
+ toJSON() {
4074
+ const obj = super.toJSON();
4075
+ if (this.enum) {
4076
+ obj['enum'] = this.enum;
4077
+ }
4078
+ return obj;
4079
+ }
4080
+ }
4081
+ /**
4082
+ * Schema class for "array" types.
4083
+ * The `items` param should refer to the type of item that can be a member
4084
+ * of the array.
4085
+ * @public
4086
+ */
4087
+ class ArraySchema extends Schema {
4088
+ constructor(schemaParams, items) {
4089
+ super({
4090
+ type: SchemaType.ARRAY,
4091
+ ...schemaParams
4092
+ });
4093
+ this.items = items;
4094
+ }
4095
+ /**
4096
+ * @internal
4097
+ */
4098
+ toJSON() {
4099
+ const obj = super.toJSON();
4100
+ obj.items = this.items.toJSON();
4101
+ return obj;
4102
+ }
4103
+ }
4104
+ /**
4105
+ * Schema class for "object" types.
4106
+ * The `properties` param must be a map of `Schema` objects.
4107
+ * @public
4108
+ */
4109
+ class ObjectSchema extends Schema {
4110
+ constructor(schemaParams, properties, optionalProperties = []) {
4111
+ super({
4112
+ type: SchemaType.OBJECT,
4113
+ ...schemaParams
4114
+ });
4115
+ this.properties = properties;
4116
+ this.optionalProperties = optionalProperties;
4117
+ }
4118
+ /**
4119
+ * @internal
4120
+ */
4121
+ toJSON() {
4122
+ const obj = super.toJSON();
4123
+ obj.properties = { ...this.properties };
4124
+ const required = [];
4125
+ if (this.optionalProperties) {
4126
+ for (const propertyKey of this.optionalProperties) {
4127
+ if (!this.properties.hasOwnProperty(propertyKey)) {
4128
+ throw new AIError(AIErrorCode.INVALID_SCHEMA, `Property "${propertyKey}" specified in "optionalProperties" does not exist.`);
4129
+ }
4130
+ }
4131
+ }
4132
+ for (const propertyKey in this.properties) {
4133
+ if (this.properties.hasOwnProperty(propertyKey)) {
4134
+ obj.properties[propertyKey] = this.properties[propertyKey].toJSON();
4135
+ if (!this.optionalProperties.includes(propertyKey)) {
4136
+ required.push(propertyKey);
4137
+ }
4138
+ }
4139
+ }
4140
+ if (required.length > 0) {
4141
+ obj.required = required;
4142
+ }
4143
+ delete obj.optionalProperties;
4144
+ return obj;
4145
+ }
4146
+ }
4147
+ /**
4148
+ * Schema class representing a value that can conform to any of the provided sub-schemas. This is
4149
+ * useful when a field can accept multiple distinct types or structures.
4150
+ * @public
4151
+ */
4152
+ class AnyOfSchema extends Schema {
4153
+ constructor(schemaParams) {
4154
+ if (schemaParams.anyOf.length === 0) {
4155
+ throw new AIError(AIErrorCode.INVALID_SCHEMA, "The 'anyOf' array must not be empty.");
4156
+ }
4157
+ super({
4158
+ ...schemaParams,
4159
+ type: undefined // anyOf schemas do not have an explicit type
4160
+ });
4161
+ this.anyOf = schemaParams.anyOf;
4162
+ }
4163
+ /**
4164
+ * @internal
4165
+ */
4166
+ toJSON() {
4167
+ const obj = super.toJSON();
4168
+ // Ensure the 'anyOf' property contains serialized SchemaRequest objects.
4169
+ if (this.anyOf && Array.isArray(this.anyOf)) {
4170
+ obj.anyOf = this.anyOf.map(s => s.toJSON());
4171
+ }
4172
+ return obj;
4173
+ }
4174
+ }
4175
+
4176
+ /**
4177
+ * @license
4178
+ * Copyright 2025 Google LLC
4179
+ *
4180
+ * Licensed under the Apache License, Version 2.0 (the "License");
4181
+ * you may not use this file except in compliance with the License.
4182
+ * You may obtain a copy of the License at
4183
+ *
4184
+ * http://www.apache.org/licenses/LICENSE-2.0
4185
+ *
4186
+ * Unless required by applicable law or agreed to in writing, software
4187
+ * distributed under the License is distributed on an "AS IS" BASIS,
4188
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4189
+ * See the License for the specific language governing permissions and
4190
+ * limitations under the License.
4191
+ */
4192
+ /**
4193
+ * Defines the image format for images generated by Imagen.
4194
+ *
4195
+ * Use this class to specify the desired format (JPEG or PNG) and compression quality
4196
+ * for images generated by Imagen. This is typically included as part of
4197
+ * {@link ImagenModelParams}.
4198
+ *
4199
+ * @example
4200
+ * ```javascript
4201
+ * const imagenModelParams = {
4202
+ * // ... other ImagenModelParams
4203
+ * imageFormat: ImagenImageFormat.jpeg(75) // JPEG with a compression level of 75.
4204
+ * }
4205
+ * ```
4206
+ *
4207
+ * @public
4208
+ */
4209
+ class ImagenImageFormat {
4210
+ constructor() {
4211
+ this.mimeType = 'image/png';
4212
+ }
4213
+ /**
4214
+ * Creates an {@link ImagenImageFormat} for a JPEG image.
4215
+ *
4216
+ * @param compressionQuality - The level of compression (a number between 0 and 100).
4217
+ * @returns An {@link ImagenImageFormat} object for a JPEG image.
4218
+ *
4219
+ * @public
4220
+ */
4221
+ static jpeg(compressionQuality) {
4222
+ if (compressionQuality &&
4223
+ (compressionQuality < 0 || compressionQuality > 100)) {
4224
+ logger.warn(`Invalid JPEG compression quality of ${compressionQuality} specified; the supported range is [0, 100].`);
4225
+ }
4226
+ return { mimeType: 'image/jpeg', compressionQuality };
4227
+ }
4228
+ /**
4229
+ * Creates an {@link ImagenImageFormat} for a PNG image.
4230
+ *
4231
+ * @returns An {@link ImagenImageFormat} object for a PNG image.
4232
+ *
4233
+ * @public
4234
+ */
4235
+ static png() {
4236
+ return { mimeType: 'image/png' };
4237
+ }
4238
+ }
4239
+
4240
+ /**
4241
+ * @license
4242
+ * Copyright 2025 Google LLC
4243
+ *
4244
+ * Licensed under the Apache License, Version 2.0 (the "License");
4245
+ * you may not use this file except in compliance with the License.
4246
+ * You may obtain a copy of the License at
4247
+ *
4248
+ * http://www.apache.org/licenses/LICENSE-2.0
4249
+ *
4250
+ * Unless required by applicable law or agreed to in writing, software
4251
+ * distributed under the License is distributed on an "AS IS" BASIS,
4252
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4253
+ * See the License for the specific language governing permissions and
4254
+ * limitations under the License.
4255
+ */
4256
+ const SERVER_INPUT_SAMPLE_RATE = 16000;
4257
+ const SERVER_OUTPUT_SAMPLE_RATE = 24000;
4258
+ const AUDIO_PROCESSOR_NAME = 'audio-processor';
4259
+ /**
4260
+ * The JS for an `AudioWorkletProcessor`.
4261
+ * This processor is responsible for taking raw audio from the microphone,
4262
+ * converting it to the required 16-bit 16kHz PCM, and posting it back to the main thread.
4263
+ *
4264
+ * See: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor
4265
+ *
4266
+ * It is defined as a string here so that it can be converted into a `Blob`
4267
+ * and loaded at runtime.
4268
+ */
4269
+ const audioProcessorWorkletString = `
4270
+ class AudioProcessor extends AudioWorkletProcessor {
4271
+ constructor(options) {
4272
+ super();
4273
+ this.targetSampleRate = options.processorOptions.targetSampleRate;
4274
+ // 'sampleRate' is a global variable available inside the AudioWorkletGlobalScope,
4275
+ // representing the native sample rate of the AudioContext.
4276
+ this.inputSampleRate = sampleRate;
4277
+ }
4278
+
4279
+ /**
4280
+ * This method is called by the browser's audio engine for each block of audio data.
4281
+ * Input is a single input, with a single channel (input[0][0]).
4282
+ */
4283
+ process(inputs) {
4284
+ const input = inputs[0];
4285
+ if (input && input.length > 0 && input[0].length > 0) {
4286
+ const pcmData = input[0]; // Float32Array of raw audio samples.
4287
+
4288
+ // Simple linear interpolation for resampling.
4289
+ const resampled = new Float32Array(Math.round(pcmData.length * this.targetSampleRate / this.inputSampleRate));
4290
+ const ratio = pcmData.length / resampled.length;
4291
+ for (let i = 0; i < resampled.length; i++) {
4292
+ resampled[i] = pcmData[Math.floor(i * ratio)];
4293
+ }
4294
+
4295
+ // Convert Float32 (-1, 1) samples to Int16 (-32768, 32767)
4296
+ const resampledInt16 = new Int16Array(resampled.length);
4297
+ for (let i = 0; i < resampled.length; i++) {
4298
+ const sample = Math.max(-1, Math.min(1, resampled[i]));
4299
+ if (sample < 0) {
4300
+ resampledInt16[i] = sample * 32768;
4301
+ } else {
4302
+ resampledInt16[i] = sample * 32767;
4303
+ }
4304
+ }
4305
+
4306
+ this.port.postMessage(resampledInt16);
4307
+ }
4308
+ // Return true to keep the processor alive and processing the next audio block.
4309
+ return true;
4310
+ }
4311
+ }
4312
+
4313
+ // Register the processor with a name that can be used to instantiate it from the main thread.
4314
+ registerProcessor('${AUDIO_PROCESSOR_NAME}', AudioProcessor);
4315
+ `;
4316
+ /**
4317
+ * Encapsulates the core logic of an audio conversation.
4318
+ *
4319
+ * @internal
4320
+ */
4321
+ class AudioConversationRunner {
4322
+ constructor(liveSession, options, deps) {
4323
+ this.liveSession = liveSession;
4324
+ this.options = options;
4325
+ this.deps = deps;
4326
+ /** A flag to indicate if the conversation has been stopped. */
4327
+ this.isStopped = false;
4328
+ /** A deferred that contains a promise that is resolved when stop() is called, to unblock the receive loop. */
4329
+ this.stopDeferred = new Deferred();
4330
+ /** A FIFO queue of 24kHz, 16-bit PCM audio chunks received from the server. */
4331
+ this.playbackQueue = [];
4332
+ /** Tracks scheduled audio sources. Used to cancel scheduled audio when the model is interrupted. */
4333
+ this.scheduledSources = [];
4334
+ /** A high-precision timeline pointer for scheduling gapless audio playback. */
4335
+ this.nextStartTime = 0;
4336
+ /** A mutex to prevent the playback processing loop from running multiple times concurrently. */
4337
+ this.isPlaybackLoopRunning = false;
4338
+ this.liveSession.inConversation = true;
4339
+ // Start listening for messages from the server.
4340
+ this.receiveLoopPromise = this.runReceiveLoop().finally(() => this.cleanup());
4341
+ // Set up the handler for receiving processed audio data from the worklet.
4342
+ // Message data has been resampled to 16kHz 16-bit PCM.
4343
+ this.deps.workletNode.port.onmessage = event => {
4344
+ if (this.isStopped) {
4345
+ return;
4346
+ }
4347
+ const pcm16 = event.data;
4348
+ const base64 = btoa(String.fromCharCode.apply(null, Array.from(new Uint8Array(pcm16.buffer))));
4349
+ const chunk = {
4350
+ mimeType: 'audio/pcm',
4351
+ data: base64
4352
+ };
4353
+ void this.liveSession.sendAudioRealtime(chunk);
4354
+ };
4355
+ }
4356
+ /**
4357
+ * Stops the conversation and unblocks the main receive loop.
4358
+ */
4359
+ async stop() {
4360
+ if (this.isStopped) {
4361
+ return;
4362
+ }
4363
+ this.isStopped = true;
4364
+ this.stopDeferred.resolve(); // Unblock the receive loop
4365
+ await this.receiveLoopPromise; // Wait for the loop and cleanup to finish
4366
+ }
4367
+ /**
4368
+ * Cleans up all audio resources (nodes, stream tracks, context) and marks the
4369
+ * session as no longer in a conversation.
4370
+ */
4371
+ cleanup() {
4372
+ this.interruptPlayback(); // Ensure all audio is stopped on final cleanup.
4373
+ this.deps.workletNode.port.onmessage = null;
4374
+ this.deps.workletNode.disconnect();
4375
+ this.deps.sourceNode.disconnect();
4376
+ this.deps.mediaStream.getTracks().forEach(track => track.stop());
4377
+ if (this.deps.audioContext.state !== 'closed') {
4378
+ void this.deps.audioContext.close();
4379
+ }
4380
+ this.liveSession.inConversation = false;
4381
+ }
4382
+ /**
4383
+ * Adds audio data to the queue and ensures the playback loop is running.
4384
+ */
4385
+ enqueueAndPlay(audioData) {
4386
+ this.playbackQueue.push(audioData);
4387
+ // Will no-op if it's already running.
4388
+ void this.processPlaybackQueue();
4389
+ }
4390
+ /**
4391
+ * Stops all current and pending audio playback and clears the queue. This is
4392
+ * called when the server indicates the model's speech was interrupted with
4393
+ * `LiveServerContent.modelTurn.interrupted`.
4394
+ */
4395
+ interruptPlayback() {
4396
+ // Stop all sources that have been scheduled. The onended event will fire for each,
4397
+ // which will clean up the scheduledSources array.
4398
+ [...this.scheduledSources].forEach(source => source.stop(0));
4399
+ // Clear the internal buffer of unprocessed audio chunks.
4400
+ this.playbackQueue.length = 0;
4401
+ // Reset the playback clock to start fresh.
4402
+ this.nextStartTime = this.deps.audioContext.currentTime;
4403
+ }
4404
+ /**
4405
+ * Processes the playback queue in a loop, scheduling each chunk in a gapless sequence.
4406
+ */
4407
+ async processPlaybackQueue() {
4408
+ if (this.isPlaybackLoopRunning) {
4409
+ return;
4410
+ }
4411
+ this.isPlaybackLoopRunning = true;
4412
+ while (this.playbackQueue.length > 0 && !this.isStopped) {
4413
+ const pcmRawBuffer = this.playbackQueue.shift();
4414
+ try {
4415
+ const pcm16 = new Int16Array(pcmRawBuffer);
4416
+ const frameCount = pcm16.length;
4417
+ const audioBuffer = this.deps.audioContext.createBuffer(1, frameCount, SERVER_OUTPUT_SAMPLE_RATE);
4418
+ // Convert 16-bit PCM to 32-bit PCM, required by the Web Audio API.
4419
+ const channelData = audioBuffer.getChannelData(0);
4420
+ for (let i = 0; i < frameCount; i++) {
4421
+ channelData[i] = pcm16[i] / 32768; // Normalize to Float32 range [-1.0, 1.0]
4422
+ }
4423
+ const source = this.deps.audioContext.createBufferSource();
4424
+ source.buffer = audioBuffer;
4425
+ source.connect(this.deps.audioContext.destination);
4426
+ // Track the source and set up a handler to remove it from tracking when it finishes.
4427
+ this.scheduledSources.push(source);
4428
+ source.onended = () => {
4429
+ this.scheduledSources = this.scheduledSources.filter(s => s !== source);
4430
+ };
4431
+ // To prevent gaps, schedule the next chunk to start either now (if we're catching up)
4432
+ // or exactly when the previous chunk is scheduled to end.
4433
+ this.nextStartTime = Math.max(this.deps.audioContext.currentTime, this.nextStartTime);
4434
+ source.start(this.nextStartTime);
4435
+ // Update the schedule for the *next* chunk.
4436
+ this.nextStartTime += audioBuffer.duration;
4437
+ }
4438
+ catch (e) {
4439
+ logger.error('Error playing audio:', e);
4440
+ }
4441
+ }
4442
+ this.isPlaybackLoopRunning = false;
4443
+ }
4444
+ /**
4445
+ * The main loop that listens for and processes messages from the server.
4446
+ */
4447
+ async runReceiveLoop() {
4448
+ const messageGenerator = this.liveSession.receive();
4449
+ while (!this.isStopped) {
4450
+ const result = await Promise.race([
4451
+ messageGenerator.next(),
4452
+ this.stopDeferred.promise
4453
+ ]);
4454
+ if (this.isStopped || !result || result.done) {
4455
+ break;
4456
+ }
4457
+ const message = result.value;
4458
+ if (message.type === 'serverContent') {
4459
+ const serverContent = message;
4460
+ if (serverContent.interrupted) {
4461
+ this.interruptPlayback();
4462
+ }
4463
+ const audioPart = serverContent.modelTurn?.parts.find(part => part.inlineData?.mimeType.startsWith('audio/'));
4464
+ if (audioPart?.inlineData) {
4465
+ const audioData = Uint8Array.from(atob(audioPart.inlineData.data), c => c.charCodeAt(0)).buffer;
4466
+ this.enqueueAndPlay(audioData);
4467
+ }
4468
+ }
4469
+ else if (message.type === 'toolCall') {
4470
+ if (!this.options.functionCallingHandler) {
4471
+ logger.warn('Received tool call message, but StartAudioConversationOptions.functionCallingHandler is undefined. Ignoring tool call.');
4472
+ }
4473
+ else {
4474
+ try {
4475
+ const functionResponse = await this.options.functionCallingHandler(message.functionCalls);
4476
+ if (!this.isStopped) {
4477
+ void this.liveSession.sendFunctionResponses([functionResponse]);
4478
+ }
4479
+ }
4480
+ catch (e) {
4481
+ throw new AIError(AIErrorCode.ERROR, `Function calling handler failed: ${e.message}`);
4482
+ }
4483
+ }
4484
+ }
4485
+ }
4486
+ }
4487
+ }
4488
+ /**
4489
+ * Starts a real-time, bidirectional audio conversation with the model. This helper function manages
4490
+ * the complexities of microphone access, audio recording, playback, and interruptions.
4491
+ *
4492
+ * @remarks Important: This function must be called in response to a user gesture
4493
+ * (for example, a button click) to comply with {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Best_practices#autoplay_policy | browser autoplay policies}.
4494
+ *
4495
+ * @example
4496
+ * ```javascript
4497
+ * const liveSession = await model.connect();
4498
+ * let conversationController;
4499
+ *
4500
+ * // This function must be called from within a click handler.
4501
+ * async function startConversation() {
4502
+ * try {
4503
+ * conversationController = await startAudioConversation(liveSession);
4504
+ * } catch (e) {
4505
+ * // Handle AI-specific errors
4506
+ * if (e instanceof AIError) {
4507
+ * console.error("AI Error:", e.message);
4508
+ * }
4509
+ * // Handle microphone permission and hardware errors
4510
+ * else if (e instanceof DOMException) {
4511
+ * console.error("Microphone Error:", e.message);
4512
+ * }
4513
+ * // Handle other unexpected errors
4514
+ * else {
4515
+ * console.error("An unexpected error occurred:", e);
4516
+ * }
4517
+ * }
4518
+ * }
4519
+ *
4520
+ * // Later, to stop the conversation:
4521
+ * // if (conversationController) {
4522
+ * // await conversationController.stop();
4523
+ * // }
4524
+ * ```
4525
+ *
4526
+ * @param liveSession - An active {@link LiveSession} instance.
4527
+ * @param options - Configuration options for the audio conversation.
4528
+ * @returns A `Promise` that resolves with an {@link AudioConversationController}.
4529
+ * @throws `AIError` if the environment does not support required Web APIs (`UNSUPPORTED`), if a conversation is already active (`REQUEST_ERROR`), the session is closed (`SESSION_CLOSED`), or if an unexpected initialization error occurs (`ERROR`).
4530
+ * @throws `DOMException` Thrown by `navigator.mediaDevices.getUserMedia()` if issues occur with microphone access, such as permissions being denied (`NotAllowedError`) or no compatible hardware being found (`NotFoundError`). See the {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#exceptions | MDN documentation} for a full list of exceptions.
4531
+ *
4532
+ * @beta
4533
+ */
4534
+ async function startAudioConversation(liveSession, options = {}) {
4535
+ if (liveSession.isClosed) {
4536
+ throw new AIError(AIErrorCode.SESSION_CLOSED, 'Cannot start audio conversation on a closed LiveSession.');
4537
+ }
4538
+ if (liveSession.inConversation) {
4539
+ throw new AIError(AIErrorCode.REQUEST_ERROR, 'An audio conversation is already in progress for this session.');
4540
+ }
4541
+ // Check for necessary Web API support.
4542
+ if (typeof AudioWorkletNode === 'undefined' ||
4543
+ typeof AudioContext === 'undefined' ||
4544
+ typeof navigator === 'undefined' ||
4545
+ !navigator.mediaDevices) {
4546
+ throw new AIError(AIErrorCode.UNSUPPORTED, 'Audio conversation is not supported in this environment. It requires the Web Audio API and AudioWorklet support.');
4547
+ }
4548
+ let audioContext;
4549
+ try {
4550
+ // 1. Set up the audio context. This must be in response to a user gesture.
4551
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Best_practices#autoplay_policy
4552
+ audioContext = new AudioContext();
4553
+ if (audioContext.state === 'suspended') {
4554
+ await audioContext.resume();
4555
+ }
4556
+ // 2. Prompt for microphone access and get the media stream.
4557
+ // This can throw a variety of permission or hardware-related errors.
4558
+ const mediaStream = await navigator.mediaDevices.getUserMedia({
4559
+ audio: true
4560
+ });
4561
+ // 3. Load the AudioWorklet processor.
4562
+ // See: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorklet
4563
+ const workletBlob = new Blob([audioProcessorWorkletString], {
4564
+ type: 'application/javascript'
4565
+ });
4566
+ const workletURL = URL.createObjectURL(workletBlob);
4567
+ await audioContext.audioWorklet.addModule(workletURL);
4568
+ // 4. Create the audio graph: Microphone -> Source Node -> Worklet Node
4569
+ const sourceNode = audioContext.createMediaStreamSource(mediaStream);
4570
+ const workletNode = new AudioWorkletNode(audioContext, AUDIO_PROCESSOR_NAME, {
4571
+ processorOptions: { targetSampleRate: SERVER_INPUT_SAMPLE_RATE }
4572
+ });
4573
+ sourceNode.connect(workletNode);
4574
+ // 5. Instantiate and return the runner which manages the conversation.
4575
+ const runner = new AudioConversationRunner(liveSession, options, {
4576
+ audioContext,
4577
+ mediaStream,
4578
+ sourceNode,
4579
+ workletNode
4580
+ });
4581
+ return { stop: () => runner.stop() };
4582
+ }
4583
+ catch (e) {
4584
+ // Ensure the audio context is closed on any setup error.
4585
+ if (audioContext && audioContext.state !== 'closed') {
4586
+ void audioContext.close();
4587
+ }
4588
+ // Re-throw specific, known error types directly. The user may want to handle `DOMException`
4589
+ // errors differently (for example, if permission to access audio device was denied).
4590
+ if (e instanceof AIError || e instanceof DOMException) {
4591
+ throw e;
4592
+ }
4593
+ // Wrap any other unexpected errors in a standard AIError.
4594
+ throw new AIError(AIErrorCode.ERROR, `Failed to initialize audio recording: ${e.message}`);
4595
+ }
4596
+ }
4597
+
4598
+ /**
4599
+ * @license
4600
+ * Copyright 2024 Google LLC
4601
+ *
4602
+ * Licensed under the Apache License, Version 2.0 (the "License");
4603
+ * you may not use this file except in compliance with the License.
4604
+ * You may obtain a copy of the License at
4605
+ *
4606
+ * http://www.apache.org/licenses/LICENSE-2.0
4607
+ *
4608
+ * Unless required by applicable law or agreed to in writing, software
4609
+ * distributed under the License is distributed on an "AS IS" BASIS,
4610
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4611
+ * See the License for the specific language governing permissions and
4612
+ * limitations under the License.
4613
+ */
4614
+ /**
4615
+ * Returns the default {@link AI} instance that is associated with the provided
4616
+ * {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new instance with the
4617
+ * default settings.
4618
+ *
4619
+ * @example
4620
+ * ```javascript
4621
+ * const ai = getAI(app);
4622
+ * ```
4623
+ *
4624
+ * @example
4625
+ * ```javascript
4626
+ * // Get an AI instance configured to use the Gemini Developer API (via Google AI).
4627
+ * const ai = getAI(app, { backend: new GoogleAIBackend() });
4628
+ * ```
4629
+ *
4630
+ * @example
4631
+ * ```javascript
4632
+ * // Get an AI instance configured to use the Vertex AI Gemini API.
4633
+ * const ai = getAI(app, { backend: new VertexAIBackend() });
4634
+ * ```
4635
+ *
4636
+ * @param app - The {@link @firebase/app#FirebaseApp} to use.
4637
+ * @param options - {@link AIOptions} that configure the AI instance.
4638
+ * @returns The default {@link AI} instance for the given {@link @firebase/app#FirebaseApp}.
4639
+ *
4640
+ * @public
4641
+ */
4642
+ function getAI(app = getApp(), options) {
4643
+ app = getModularInstance(app);
4644
+ // Dependencies
4645
+ const AIProvider = _getProvider(app, AI_TYPE);
4646
+ const backend = options?.backend ?? new GoogleAIBackend();
4647
+ const finalOptions = {
4648
+ useLimitedUseAppCheckTokens: options?.useLimitedUseAppCheckTokens ?? false
4649
+ };
4650
+ const identifier = encodeInstanceIdentifier(backend);
4651
+ const aiInstance = AIProvider.getImmediate({
4652
+ identifier
4653
+ });
4654
+ aiInstance.options = finalOptions;
4655
+ return aiInstance;
4656
+ }
4657
+ /**
4658
+ * Returns a {@link GenerativeModel} class with methods for inference
4659
+ * and other functionality.
4660
+ *
4661
+ * @public
4662
+ */
4663
+ function getGenerativeModel(ai, modelParams, requestOptions) {
4664
+ // Uses the existence of HybridParams.mode to clarify the type of the modelParams input.
4665
+ const hybridParams = modelParams;
4666
+ let inCloudParams;
4667
+ if (hybridParams.mode) {
4668
+ inCloudParams = hybridParams.inCloudParams || {
4669
+ model: DEFAULT_HYBRID_IN_CLOUD_MODEL
4670
+ };
4671
+ }
4672
+ else {
4673
+ inCloudParams = modelParams;
4674
+ }
4675
+ if (!inCloudParams.model) {
4676
+ throw new AIError(AIErrorCode.NO_MODEL, `Must provide a model name. Example: getGenerativeModel({ model: 'my-model-name' })`);
4677
+ }
4678
+ /**
4679
+ * An AIService registered by index.node.ts will not have a
4680
+ * chromeAdapterFactory() method.
4681
+ */
4682
+ const chromeAdapter = ai.chromeAdapterFactory?.(hybridParams.mode, typeof window === 'undefined' ? undefined : window, hybridParams.onDeviceParams);
4683
+ const generativeModel = new GenerativeModel(ai, inCloudParams, requestOptions, chromeAdapter);
4684
+ generativeModel._apiSettings.inferenceMode = hybridParams.mode;
4685
+ return generativeModel;
4686
+ }
4687
+ /**
4688
+ * Returns an {@link ImagenModel} class with methods for using Imagen.
4689
+ *
4690
+ * Only Imagen 3 models (named `imagen-3.0-*`) are supported.
4691
+ *
4692
+ * @param ai - An {@link AI} instance.
4693
+ * @param modelParams - Parameters to use when making Imagen requests.
4694
+ * @param requestOptions - Additional options to use when making requests.
4695
+ *
4696
+ * @throws If the `apiKey` or `projectId` fields are missing in your
4697
+ * Firebase config.
4698
+ *
4699
+ * @public
4700
+ */
4701
+ function getImagenModel(ai, modelParams, requestOptions) {
4702
+ if (!modelParams.model) {
4703
+ throw new AIError(AIErrorCode.NO_MODEL, `Must provide a model name. Example: getImagenModel({ model: 'my-model-name' })`);
4704
+ }
4705
+ return new ImagenModel(ai, modelParams, requestOptions);
4706
+ }
4707
+ /**
4708
+ * Returns a {@link LiveGenerativeModel} class for real-time, bidirectional communication.
4709
+ *
4710
+ * The Live API is only supported in modern browser windows and Node >= 22.
4711
+ *
4712
+ * @param ai - An {@link AI} instance.
4713
+ * @param modelParams - Parameters to use when setting up a {@link LiveSession}.
4714
+ * @throws If the `apiKey` or `projectId` fields are missing in your
4715
+ * Firebase config.
4716
+ *
4717
+ * @beta
4718
+ */
4719
+ function getLiveGenerativeModel(ai, modelParams) {
4720
+ if (!modelParams.model) {
4721
+ throw new AIError(AIErrorCode.NO_MODEL, `Must provide a model name for getLiveGenerativeModel. Example: getLiveGenerativeModel(ai, { model: 'my-model-name' })`);
4722
+ }
4723
+ const webSocketHandler = new WebSocketHandlerImpl();
4724
+ return new LiveGenerativeModel(ai, modelParams, webSocketHandler);
4725
+ }
4726
+ /**
4727
+ * Returns a {@link TemplateGenerativeModel} class for executing server-side
4728
+ * templates.
4729
+ *
4730
+ * @param ai - An {@link AI} instance.
4731
+ * @param requestOptions - Additional options to use when making requests.
4732
+ *
4733
+ * @beta
4734
+ */
4735
+ function getTemplateGenerativeModel(ai, requestOptions) {
4736
+ return new TemplateGenerativeModel(ai, requestOptions);
4737
+ }
4738
+ /**
4739
+ * Returns a {@link TemplateImagenModel} class for executing server-side
4740
+ * Imagen templates.
4741
+ *
4742
+ * @param ai - An {@link AI} instance.
4743
+ * @param requestOptions - Additional options to use when making requests.
4744
+ *
4745
+ * @beta
4746
+ */
4747
+ function getTemplateImagenModel(ai, requestOptions) {
4748
+ return new TemplateImagenModel(ai, requestOptions);
4749
+ }
4750
+
4751
+ /**
4752
+ * The Firebase AI Web SDK.
4753
+ *
4754
+ * @packageDocumentation
4755
+ */
4756
+ function registerAI() {
4757
+ _registerComponent(new Component(AI_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
4758
+ registerVersion(name, version);
4759
+ // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation
4760
+ registerVersion(name, version, 'esm2020');
4761
+ }
4762
+ registerAI();
4763
+
4764
+ export { AIError, AIErrorCode, AIModel, AnyOfSchema, ArraySchema, Backend, BackendType, BlockReason, BooleanSchema, ChatSession, FinishReason, FunctionCallingMode, GenerativeModel, GoogleAIBackend, HarmBlockMethod, HarmBlockThreshold, HarmCategory, HarmProbability, HarmSeverity, ImagenAspectRatio, ImagenImageFormat, ImagenModel, ImagenPersonFilterLevel, ImagenSafetyFilterLevel, InferenceMode, InferenceSource, IntegerSchema, Language, LiveGenerativeModel, LiveResponseType, LiveSession, Modality, NumberSchema, ObjectSchema, Outcome, POSSIBLE_ROLES, ResponseModality, Schema, SchemaType, StringSchema, TemplateGenerativeModel, TemplateImagenModel, ThinkingLevel, URLRetrievalStatus, VertexAIBackend, getAI, getGenerativeModel, getImagenModel, getLiveGenerativeModel, getTemplateGenerativeModel, getTemplateImagenModel, startAudioConversation };
4765
+ //# sourceMappingURL=index.esm.js.map