@firebase/ai 2.6.0 → 2.6.1-canary.5579b387a

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 (37) hide show
  1. package/dist/ai-public.d.ts +104 -11
  2. package/dist/ai.d.ts +105 -12
  3. package/dist/esm/index.esm.js +159 -79
  4. package/dist/esm/index.esm.js.map +1 -1
  5. package/dist/esm/src/constants.d.ts +1 -1
  6. package/dist/esm/src/methods/chat-session.d.ts +7 -3
  7. package/dist/esm/src/methods/count-tokens.d.ts +2 -2
  8. package/dist/esm/src/methods/generate-content.d.ts +5 -5
  9. package/dist/esm/src/models/generative-model.d.ts +4 -4
  10. package/dist/esm/src/models/imagen-model.d.ts +3 -3
  11. package/dist/esm/src/models/template-generative-model.d.ts +3 -3
  12. package/dist/esm/src/models/template-imagen-model.d.ts +2 -2
  13. package/dist/esm/src/requests/request.d.ts +4 -2
  14. package/dist/esm/src/requests/stream-reader.d.ts +1 -3
  15. package/dist/esm/src/types/enums.d.ts +21 -0
  16. package/dist/esm/src/types/imagen/internal.d.ts +1 -1
  17. package/dist/esm/src/types/requests.d.ts +68 -3
  18. package/dist/index.cjs.js +159 -78
  19. package/dist/index.cjs.js.map +1 -1
  20. package/dist/index.node.cjs.js +159 -78
  21. package/dist/index.node.cjs.js.map +1 -1
  22. package/dist/index.node.mjs +159 -79
  23. package/dist/index.node.mjs.map +1 -1
  24. package/dist/src/constants.d.ts +1 -1
  25. package/dist/src/methods/chat-session.d.ts +7 -3
  26. package/dist/src/methods/count-tokens.d.ts +2 -2
  27. package/dist/src/methods/generate-content.d.ts +5 -5
  28. package/dist/src/models/generative-model.d.ts +4 -4
  29. package/dist/src/models/imagen-model.d.ts +3 -3
  30. package/dist/src/models/template-generative-model.d.ts +3 -3
  31. package/dist/src/models/template-imagen-model.d.ts +2 -2
  32. package/dist/src/requests/request.d.ts +4 -2
  33. package/dist/src/requests/stream-reader.d.ts +1 -3
  34. package/dist/src/types/enums.d.ts +21 -0
  35. package/dist/src/types/imagen/internal.d.ts +1 -1
  36. package/dist/src/types/requests.d.ts +68 -3
  37. package/package.json +8 -8
@@ -25,4 +25,4 @@ export declare const DEFAULT_FETCH_TIMEOUT_MS: number;
25
25
  /**
26
26
  * Defines the name of the default in-cloud model to use for hybrid inference.
27
27
  */
28
- export declare const DEFAULT_HYBRID_IN_CLOUD_MODEL = "gemini-2.0-flash-lite";
28
+ export declare const DEFAULT_HYBRID_IN_CLOUD_MODEL = "gemini-2.5-flash-lite";
@@ -14,7 +14,7 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { Content, GenerateContentResult, GenerateContentStreamResult, Part, RequestOptions, StartChatParams } from '../types';
17
+ import { Content, GenerateContentResult, GenerateContentStreamResult, Part, RequestOptions, SingleRequestOptions, StartChatParams } from '../types';
18
18
  import { ApiSettings } from '../types/internal';
19
19
  import { ChromeAdapter } from '../types/chrome-adapter';
20
20
  /**
@@ -30,6 +30,10 @@ export declare class ChatSession {
30
30
  requestOptions?: RequestOptions | undefined;
31
31
  private _apiSettings;
32
32
  private _history;
33
+ /**
34
+ * Ensures sequential execution of chat messages to maintain history order.
35
+ * Each call waits for the previous one to settle before proceeding.
36
+ */
33
37
  private _sendPromise;
34
38
  constructor(apiSettings: ApiSettings, model: string, chromeAdapter?: ChromeAdapter | undefined, params?: StartChatParams | undefined, requestOptions?: RequestOptions | undefined);
35
39
  /**
@@ -42,11 +46,11 @@ export declare class ChatSession {
42
46
  * Sends a chat message and receives a non-streaming
43
47
  * {@link GenerateContentResult}
44
48
  */
45
- sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
49
+ sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
46
50
  /**
47
51
  * Sends a chat message and receives the response as a
48
52
  * {@link GenerateContentStreamResult} containing an iterable stream
49
53
  * and a response promise.
50
54
  */
51
- sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
55
+ sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
52
56
  }
@@ -14,8 +14,8 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { CountTokensRequest, CountTokensResponse, RequestOptions } from '../types';
17
+ import { CountTokensRequest, CountTokensResponse, SingleRequestOptions, RequestOptions } from '../types';
18
18
  import { ApiSettings } from '../types/internal';
19
19
  import { ChromeAdapter } from '../types/chrome-adapter';
20
- export declare function countTokensOnCloud(apiSettings: ApiSettings, model: string, params: CountTokensRequest, requestOptions?: RequestOptions): Promise<CountTokensResponse>;
20
+ export declare function countTokensOnCloud(apiSettings: ApiSettings, model: string, params: CountTokensRequest, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
21
21
  export declare function countTokens(apiSettings: ApiSettings, model: string, params: CountTokensRequest, chromeAdapter?: ChromeAdapter, requestOptions?: RequestOptions): Promise<CountTokensResponse>;
@@ -14,10 +14,10 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, RequestOptions } from '../types';
17
+ import { GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, SingleRequestOptions } from '../types';
18
18
  import { ApiSettings } from '../types/internal';
19
19
  import { ChromeAdapter } from '../types/chrome-adapter';
20
- export declare function generateContentStream(apiSettings: ApiSettings, model: string, params: GenerateContentRequest, chromeAdapter?: ChromeAdapter, requestOptions?: RequestOptions): Promise<GenerateContentStreamResult>;
21
- export declare function templateGenerateContent(apiSettings: ApiSettings, templateId: string, templateParams: object, requestOptions?: RequestOptions): Promise<GenerateContentResult>;
22
- export declare function templateGenerateContentStream(apiSettings: ApiSettings, templateId: string, templateParams: object, requestOptions?: RequestOptions): Promise<GenerateContentStreamResult>;
23
- export declare function generateContent(apiSettings: ApiSettings, model: string, params: GenerateContentRequest, chromeAdapter?: ChromeAdapter, requestOptions?: RequestOptions): Promise<GenerateContentResult>;
20
+ export declare function generateContentStream(apiSettings: ApiSettings, model: string, params: GenerateContentRequest, chromeAdapter?: ChromeAdapter, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
21
+ export declare function templateGenerateContent(apiSettings: ApiSettings, templateId: string, templateParams: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
22
+ export declare function templateGenerateContentStream(apiSettings: ApiSettings, templateId: string, templateParams: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
23
+ export declare function generateContent(apiSettings: ApiSettings, model: string, params: GenerateContentRequest, chromeAdapter?: ChromeAdapter, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
@@ -14,7 +14,7 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { Content, CountTokensRequest, CountTokensResponse, GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, GenerationConfig, ModelParams, Part, RequestOptions, SafetySetting, StartChatParams, Tool, ToolConfig } from '../types';
17
+ import { Content, CountTokensRequest, CountTokensResponse, GenerateContentRequest, GenerateContentResult, GenerateContentStreamResult, GenerationConfig, ModelParams, Part, SafetySetting, RequestOptions, StartChatParams, Tool, ToolConfig, SingleRequestOptions } from '../types';
18
18
  import { ChatSession } from '../methods/chat-session';
19
19
  import { AI } from '../public-types';
20
20
  import { AIModel } from './ai-model';
@@ -36,14 +36,14 @@ export declare class GenerativeModel extends AIModel {
36
36
  * Makes a single non-streaming call to the model
37
37
  * and returns an object containing a single {@link GenerateContentResponse}.
38
38
  */
39
- generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
39
+ generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
40
40
  /**
41
41
  * Makes a single streaming call to the model
42
42
  * and returns an object containing an iterable stream that iterates
43
43
  * over all chunks in the streaming response as well as
44
44
  * a promise that returns the final aggregated response.
45
45
  */
46
- generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
46
+ generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
47
47
  /**
48
48
  * Gets a new {@link ChatSession} instance which can be used for
49
49
  * multi-turn chats.
@@ -52,5 +52,5 @@ export declare class GenerativeModel extends AIModel {
52
52
  /**
53
53
  * Counts the tokens in the provided request.
54
54
  */
55
- countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
55
+ countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
56
56
  }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { AI } from '../public-types';
18
- import { ImagenGCSImage, ImagenGenerationConfig, ImagenInlineImage, RequestOptions, ImagenModelParams, ImagenGenerationResponse, ImagenSafetySettings } from '../types';
18
+ import { ImagenGCSImage, ImagenGenerationConfig, ImagenInlineImage, RequestOptions, ImagenModelParams, ImagenGenerationResponse, ImagenSafetySettings, SingleRequestOptions } from '../types';
19
19
  import { AIModel } from './ai-model';
20
20
  /**
21
21
  * Class for Imagen model APIs.
@@ -78,7 +78,7 @@ export declare class ImagenModel extends AIModel {
78
78
  *
79
79
  * @public
80
80
  */
81
- generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
81
+ generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
82
82
  /**
83
83
  * Generates images to Cloud Storage for Firebase using the Imagen model.
84
84
  *
@@ -98,5 +98,5 @@ export declare class ImagenModel extends AIModel {
98
98
  * returned object will have a `filteredReason` property.
99
99
  * If all images are filtered, the `images` array will be empty.
100
100
  */
101
- generateImagesGCS(prompt: string, gcsURI: string): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
101
+ generateImagesGCS(prompt: string, gcsURI: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
102
102
  }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { GenerateContentResult, RequestOptions } from '../types';
18
- import { AI, GenerateContentStreamResult } from '../public-types';
18
+ import { AI, GenerateContentStreamResult, SingleRequestOptions } from '../public-types';
19
19
  import { ApiSettings } from '../types/internal';
20
20
  /**
21
21
  * {@link GenerativeModel} APIs that execute on a server-side template.
@@ -47,7 +47,7 @@ export declare class TemplateGenerativeModel {
47
47
  *
48
48
  * @beta
49
49
  */
50
- generateContent(templateId: string, templateVariables: object): Promise<GenerateContentResult>;
50
+ generateContent(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
51
51
  /**
52
52
  * Makes a single streaming call to the model and returns an object
53
53
  * containing an iterable stream that iterates over all chunks in the
@@ -60,5 +60,5 @@ export declare class TemplateGenerativeModel {
60
60
  *
61
61
  * @beta
62
62
  */
63
- generateContentStream(templateId: string, templateVariables: object): Promise<GenerateContentStreamResult>;
63
+ generateContentStream(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
64
64
  }
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  import { RequestOptions } from '../types';
18
- import { AI, ImagenGenerationResponse, ImagenInlineImage } from '../public-types';
18
+ import { AI, ImagenGenerationResponse, ImagenInlineImage, SingleRequestOptions } from '../public-types';
19
19
  import { ApiSettings } from '../types/internal';
20
20
  /**
21
21
  * Class for Imagen model APIs that execute on a server-side template.
@@ -47,5 +47,5 @@ export declare class TemplateImagenModel {
47
47
  *
48
48
  * @beta
49
49
  */
50
- generateImages(templateId: string, templateVariables: object): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
50
+ generateImages(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
51
51
  }
@@ -14,8 +14,10 @@
14
14
  * See the License for the specific language governing permissions and
15
15
  * limitations under the License.
16
16
  */
17
- import { RequestOptions } from '../types';
17
+ import { SingleRequestOptions } from '../types';
18
18
  import { ApiSettings } from '../types/internal';
19
+ export declare const TIMEOUT_EXPIRED_MESSAGE = "Timeout has expired.";
20
+ export declare const ABORT_ERROR_NAME = "AbortError";
19
21
  export declare const enum Task {
20
22
  GENERATE_CONTENT = "generateContent",
21
23
  STREAM_GENERATE_CONTENT = "streamGenerateContent",
@@ -30,7 +32,7 @@ export declare const enum ServerPromptTemplateTask {
30
32
  interface BaseRequestURLParams {
31
33
  apiSettings: ApiSettings;
32
34
  stream: boolean;
33
- requestOptions?: RequestOptions;
35
+ singleRequestOptions?: SingleRequestOptions;
34
36
  }
35
37
  /**
36
38
  * Parameters used to construct the URL of a request to use a model.
@@ -27,9 +27,7 @@ import { InferenceSource } from '../public-types';
27
27
  */
28
28
  export declare function processStream(response: Response, apiSettings: ApiSettings, inferenceSource?: InferenceSource): GenerateContentStreamResult;
29
29
  /**
30
- * Reads a raw stream from the fetch response and join incomplete
31
- * chunks, returning a new stream that provides a single complete
32
- * GenerateContentResponse in each iteration.
30
+ * Reads a raw string stream, buffers incomplete chunks, and yields parsed JSON objects.
33
31
  */
34
32
  export declare function getResponseStream<T>(inputStream: ReadableStream<string>): ReadableStream<T>;
35
33
  /**
@@ -396,3 +396,24 @@ export declare const Language: {
396
396
  * @beta
397
397
  */
398
398
  export type Language = (typeof Language)[keyof typeof Language];
399
+ /**
400
+ * A preset that controls the model's "thinking" process. Use
401
+ * `ThinkingLevel.LOW` for faster responses on less complex tasks, and
402
+ * `ThinkingLevel.HIGH` for better reasoning on more complex tasks.
403
+ *
404
+ * @public
405
+ */
406
+ export declare const ThinkingLevel: {
407
+ MINIMAL: string;
408
+ LOW: string;
409
+ MEDIUM: string;
410
+ HIGH: string;
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
+ export type ThinkingLevel = (typeof ThinkingLevel)[keyof typeof ThinkingLevel];
@@ -73,7 +73,7 @@ export interface ImagenResponseInternal {
73
73
  * The parameters to be sent in the request body of the HTTP call
74
74
  * to the Vertex AI backend.
75
75
  *
76
- * We need a seperate internal-only interface for this because the REST
76
+ * We need a separate internal-only interface for this because the REST
77
77
  * API expects different parameter names than what we show to our users.
78
78
  *
79
79
  * Sample request body JSON:
@@ -17,7 +17,7 @@
17
17
  import { ObjectSchema, TypedSchema } from '../requests/schema-builder';
18
18
  import { Content, Part } from './content';
19
19
  import { LanguageModelCreateOptions, LanguageModelPromptOptions } from './language-model';
20
- import { FunctionCallingMode, HarmBlockMethod, HarmBlockThreshold, HarmCategory, InferenceMode, ResponseModality } from './enums';
20
+ import { FunctionCallingMode, HarmBlockMethod, HarmBlockThreshold, HarmCategory, InferenceMode, ResponseModality, ThinkingLevel } from './enums';
21
21
  import { ObjectSchemaRequest, SchemaRequest } from './schema';
22
22
  /**
23
23
  * Base parameters for a number of methods.
@@ -232,6 +232,46 @@ export interface RequestOptions {
232
232
  */
233
233
  baseUrl?: string;
234
234
  }
235
+ /**
236
+ * Options that can be provided per-request.
237
+ * Extends the base {@link RequestOptions} (like `timeout` and `baseUrl`)
238
+ * with request-specific controls like cancellation via `AbortSignal`.
239
+ *
240
+ * Options specified here will override any default {@link RequestOptions}
241
+ * configured on a model (for example, {@link GenerativeModel}).
242
+ *
243
+ * @public
244
+ */
245
+ export interface SingleRequestOptions extends RequestOptions {
246
+ /**
247
+ * An `AbortSignal` instance that allows cancelling ongoing requests (like `generateContent` or
248
+ * `generateImages`).
249
+ *
250
+ * If provided, calling `abort()` on the corresponding `AbortController`
251
+ * will attempt to cancel the underlying HTTP request. An `AbortError` will be thrown
252
+ * if cancellation is successful.
253
+ *
254
+ * Note that this will not cancel the request in the backend, so any applicable billing charges
255
+ * will still be applied despite cancellation.
256
+ *
257
+ * @example
258
+ * ```javascript
259
+ * const controller = new AbortController();
260
+ * const model = getGenerativeModel({
261
+ * // ...
262
+ * });
263
+ * model.generateContent(
264
+ * "Write a story about a magic backpack.",
265
+ * { signal: controller.signal }
266
+ * );
267
+ *
268
+ * // To cancel request:
269
+ * controller.abort();
270
+ * ```
271
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
272
+ */
273
+ signal?: AbortSignal;
274
+ }
235
275
  /**
236
276
  * Defines a tool that model can call to access external knowledge.
237
277
  * @public
@@ -401,17 +441,42 @@ export interface ThinkingConfig {
401
441
  /**
402
442
  * The thinking budget, in tokens.
403
443
  *
444
+ * @remarks
404
445
  * This parameter sets an upper limit on the number of tokens the model can use for its internal
405
446
  * "thinking" process. A higher budget may result in higher quality responses for complex tasks
406
447
  * but can also increase latency and cost.
407
448
  *
408
- * If you don't specify a budget, the model will determine the appropriate amount
409
- * of thinking based on the complexity of the prompt.
449
+ * The range of supported thinking budget values depends on the model.
450
+ *
451
+ * <ul>
452
+ * <li>To use the default thinking budget for a model, leave
453
+ * this value undefined.</li>
454
+ *
455
+ * <li>To disable thinking, when supported by the model, set this value
456
+ * to `0`.</li>
457
+ *
458
+ * <li>To use dynamic thinking, which allows the model to decide on the thinking
459
+ * budget based on the task, set this value to `-1`.</li>
460
+ * </ul>
410
461
  *
411
462
  * An error will be thrown if you set a thinking budget for a model that does not support this
412
463
  * feature or if the specified budget is not within the model's supported range.
464
+ *
465
+ * The model will also error if `thinkingLevel` and `thinkingBudget` are
466
+ * both set.
413
467
  */
414
468
  thinkingBudget?: number;
469
+ /**
470
+ * If not specified, Gemini will use the model's default dynamic thinking level.
471
+ *
472
+ * @remarks
473
+ * Note: The model will error if `thinkingLevel` and `thinkingBudget` are
474
+ * both set.
475
+ *
476
+ * Important: Gemini 2.5 series models do not support thinking levels; use
477
+ * `thinkingBudget` to set a thinking budget instead.
478
+ */
479
+ thinkingLevel?: ThinkingLevel;
415
480
  /**
416
481
  * Whether to include "thought summaries" in the model's response.
417
482
  *