Skip to content

Commit 37bdd41

Browse files
authored
Merge 931972d into 40be2db
2 parents 40be2db + 931972d commit 37bdd41

20 files changed

+1068
-255
lines changed

.changeset/long-keys-watch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/ai': minor
4+
---
5+
6+
Add support for `AbortSignal`, allowing requests to be aborted.

common/api-review/ai.api.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export class ChatSession {
120120
params?: StartChatParams | undefined;
121121
// (undocumented)
122122
requestOptions?: RequestOptions | undefined;
123-
sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
124-
sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
123+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
124+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
125125
}
126126

127127
// @public
@@ -396,9 +396,9 @@ export interface GenerativeContentBlob {
396396
// @public
397397
export class GenerativeModel extends AIModel {
398398
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
399-
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
400-
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
401-
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
399+
countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
400+
generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
401+
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
402402
// (undocumented)
403403
generationConfig: GenerationConfig;
404404
// (undocumented)
@@ -598,9 +598,9 @@ export interface ImagenInlineImage {
598598
// @beta
599599
export class ImagenModel extends AIModel {
600600
constructor(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions | undefined);
601-
generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
601+
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
602602
// @internal
603-
generateImagesGCS(prompt: string, gcsURI: string): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
603+
generateImagesGCS(prompt: string, gcsURI: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
604604
generationConfig?: ImagenGenerationConfig;
605605
// (undocumented)
606606
requestOptions?: RequestOptions | undefined;
@@ -862,6 +862,11 @@ export interface Segment {
862862
startIndex: number;
863863
}
864864

865+
// @public
866+
export interface SingleRequestOptions extends RequestOptions {
867+
signal?: AbortSignal;
868+
}
869+
865870
// @public
866871
export interface StartChatParams extends BaseParams {
867872
// (undocumented)

common/api-review/vertexai.api.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export class ChatSession {
120120
params?: StartChatParams | undefined;
121121
// (undocumented)
122122
requestOptions?: RequestOptions | undefined;
123-
sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
124-
sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
123+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
124+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
125125
}
126126

127127
// @public
@@ -394,11 +394,19 @@ export interface GenerativeContentBlob {
394394
}
395395

396396
// @public
397+
<<<<<<< HEAD
398+
export class GenerativeModel extends VertexAIModel {
399+
constructor(vertexAI: VertexAI, modelParams: ModelParams, requestOptions?: RequestOptions);
400+
countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
401+
generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
402+
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
403+
=======
397404
export class GenerativeModel extends AIModel {
398405
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions);
399406
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
400407
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
401408
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
409+
>>>>>>> main
402410
// (undocumented)
403411
generationConfig: GenerationConfig;
404412
// (undocumented)
@@ -595,11 +603,17 @@ export interface ImagenInlineImage {
595603
}
596604

597605
// @beta
606+
<<<<<<< HEAD
607+
export class ImagenModel extends VertexAIModel {
608+
constructor(vertexAI: VertexAI, modelParams: ImagenModelParams, requestOptions?: RequestOptions | undefined);
609+
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
610+
=======
598611
export class ImagenModel extends AIModel {
599612
constructor(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions | undefined);
600613
generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
614+
>>>>>>> main
601615
// @internal
602-
generateImagesGCS(prompt: string, gcsURI: string): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
616+
generateImagesGCS(prompt: string, gcsURI: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
603617
generationConfig?: ImagenGenerationConfig;
604618
// (undocumented)
605619
requestOptions?: RequestOptions | undefined;
@@ -857,6 +871,11 @@ export interface Segment {
857871
startIndex: number;
858872
}
859873

874+
// @public
875+
export interface SingleRequestOptions extends RequestOptions {
876+
signal?: AbortSignal;
877+
}
878+
860879
// @public
861880
export interface StartChatParams extends BaseParams {
862881
// (undocumented)

docs-devsite/_toc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ toc:
132132
path: /docs/reference/js/ai.schemashared.md
133133
- title: Segment
134134
path: /docs/reference/js/ai.segment.md
135+
- title: SingleRequestOptions
136+
path: /docs/reference/js/ai.singlerequestoptions.md
135137
- title: StartChatParams
136138
path: /docs/reference/js/ai.startchatparams.md
137139
- title: StringSchema

docs-devsite/ai.chatsession.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export declare class ChatSession
3737
| Method | Modifiers | Description |
3838
| --- | --- | --- |
3939
| [getHistory()](./ai.chatsession.md#chatsessiongethistory) | | Gets the chat history so far. Blocked prompts are not added to history. Neither blocked candidates nor the prompts that generated them are added to history. |
40-
| [sendMessage(request)](./ai.chatsession.md#chatsessionsendmessage) | | Sends a chat message and receives a non-streaming [GenerateContentResult](./ai.generatecontentresult.md#generatecontentresult_interface) |
41-
| [sendMessageStream(request)](./ai.chatsession.md#chatsessionsendmessagestream) | | Sends a chat message and receives the response as a [GenerateContentStreamResult](./ai.generatecontentstreamresult.md#generatecontentstreamresult_interface) containing an iterable stream and a response promise. |
40+
| [sendMessage(request, singleRequestOptions)](./ai.chatsession.md#chatsessionsendmessage) | | Sends a chat message and receives a non-streaming [GenerateContentResult](./ai.generatecontentresult.md#generatecontentresult_interface) |
41+
| [sendMessageStream(request, singleRequestOptions)](./ai.chatsession.md#chatsessionsendmessagestream) | | Sends a chat message and receives the response as a [GenerateContentStreamResult](./ai.generatecontentstreamresult.md#generatecontentstreamresult_interface) containing an iterable stream and a response promise. |
4242

4343
## ChatSession.(constructor)
4444

@@ -103,14 +103,15 @@ Sends a chat message and receives a non-streaming [GenerateContentResult](./ai.g
103103
<b>Signature:</b>
104104

105105
```typescript
106-
sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
106+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
107107
```
108108

109109
#### Parameters
110110

111111
| Parameter | Type | Description |
112112
| --- | --- | --- |
113113
| request | string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
114+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
114115

115116
<b>Returns:</b>
116117

@@ -123,14 +124,15 @@ Sends a chat message and receives the response as a [GenerateContentStreamResult
123124
<b>Signature:</b>
124125

125126
```typescript
126-
sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
127+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
127128
```
128129

129130
#### Parameters
130131

131132
| Parameter | Type | Description |
132133
| --- | --- | --- |
133134
| request | string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
135+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
134136

135137
<b>Returns:</b>
136138

docs-devsite/ai.generativemodel.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export declare class GenerativeModel extends AIModel
4040
4141
| Method | Modifiers | Description |
4242
| --- | --- | --- |
43-
| [countTokens(request)](./ai.generativemodel.md#generativemodelcounttokens) | | Counts the tokens in the provided request. |
44-
| [generateContent(request)](./ai.generativemodel.md#generativemodelgeneratecontent) | | Makes a single non-streaming call to the model and returns an object containing a single [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface)<!-- -->. |
45-
| [generateContentStream(request)](./ai.generativemodel.md#generativemodelgeneratecontentstream) | | Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response. |
43+
| [countTokens(request, singleRequestOptions)](./ai.generativemodel.md#generativemodelcounttokens) | | Counts the tokens in the provided request. |
44+
| [generateContent(request, singleRequestOptions)](./ai.generativemodel.md#generativemodelgeneratecontent) | | Makes a single non-streaming call to the model and returns an object containing a single [GenerateContentResponse](./ai.generatecontentresponse.md#generatecontentresponse_interface)<!-- -->. |
45+
| [generateContentStream(request, singleRequestOptions)](./ai.generativemodel.md#generativemodelgeneratecontentstream) | | Makes a single streaming call to the model and returns an object containing an iterable stream that iterates over all chunks in the streaming response as well as a promise that returns the final aggregated response. |
4646
| [startChat(startChatParams)](./ai.generativemodel.md#generativemodelstartchat) | | Gets a new [ChatSession](./ai.chatsession.md#chatsession_class) instance which can be used for multi-turn chats. |
4747
4848
## GenerativeModel.(constructor)
@@ -118,14 +118,15 @@ Counts the tokens in the provided request.
118118
<b>Signature:</b>
119119
120120
```typescript
121-
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
121+
countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
122122
```
123123
124124
#### Parameters
125125
126126
| Parameter | Type | Description |
127127
| --- | --- | --- |
128128
| request | [CountTokensRequest](./ai.counttokensrequest.md#counttokensrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
129+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
129130
130131
<b>Returns:</b>
131132
@@ -138,14 +139,15 @@ Makes a single non-streaming call to the model and returns an object containing
138139
<b>Signature:</b>
139140
140141
```typescript
141-
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
142+
generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
142143
```
143144
144145
#### Parameters
145146
146147
| Parameter | Type | Description |
147148
| --- | --- | --- |
148149
| request | [GenerateContentRequest](./ai.generatecontentrequest.md#generatecontentrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
150+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
149151
150152
<b>Returns:</b>
151153
@@ -158,14 +160,15 @@ Makes a single streaming call to the model and returns an object containing an i
158160
<b>Signature:</b>
159161
160162
```typescript
161-
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
163+
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
162164
```
163165
164166
#### Parameters
165167
166168
| Parameter | Type | Description |
167169
| --- | --- | --- |
168170
| request | [GenerateContentRequest](./ai.generatecontentrequest.md#generatecontentrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
171+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
169172
170173
<b>Returns:</b>
171174

docs-devsite/ai.imagenmodel.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export declare class ImagenModel extends AIModel
4242
4343
| Method | Modifiers | Description |
4444
| --- | --- | --- |
45-
| [generateImages(prompt)](./ai.imagenmodel.md#imagenmodelgenerateimages) | | <b><i>(Public Preview)</i></b> Generates images using the Imagen model and returns them as base64-encoded strings. |
45+
| [generateImages(prompt, singleRequestOptions)](./ai.imagenmodel.md#imagenmodelgenerateimages) | | <b><i>(Public Preview)</i></b> Generates images using the Imagen model and returns them as base64-encoded strings. |
4646
4747
## ImagenModel.(constructor)
4848
@@ -118,14 +118,15 @@ If the prompt was not blocked, but one or more of the generated images were filt
118118
<b>Signature:</b>
119119
120120
```typescript
121-
generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
121+
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
122122
```
123123
124124
#### Parameters
125125
126126
| Parameter | Type | Description |
127127
| --- | --- | --- |
128128
| prompt | string | A text prompt describing the image(s) to generate. |
129+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
129130
130131
<b>Returns:</b>
131132

docs-devsite/ai.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ The Firebase AI Web SDK.
117117
| [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) | Final format for [Schema](./ai.schema.md#schema_class) params passed to backend requests. |
118118
| [SchemaShared](./ai.schemashared.md#schemashared_interface) | Basic [Schema](./ai.schema.md#schema_class) properties shared across several Schema-related types. |
119119
| [Segment](./ai.segment.md#segment_interface) | |
120+
| [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | Options that can be provided per-request. Extends the base [RequestOptions](./ai.requestoptions.md#requestoptions_interface) (like <code>timeout</code> and <code>baseUrl</code>) with request-specific controls like cancellation via <code>AbortSignal</code>.<!-- -->Options specified here will override any default [RequestOptions](./ai.requestoptions.md#requestoptions_interface) configured on a model (for example, [GenerativeModel](./ai.generativemodel.md#generativemodel_class)<!-- -->). |
120121
| [StartChatParams](./ai.startchatparams.md#startchatparams_interface) | Params for [GenerativeModel.startChat()](./ai.generativemodel.md#generativemodelstartchat)<!-- -->. |
121122
| [TextPart](./ai.textpart.md#textpart_interface) | Content part interface if the part represents a text string. |
122123
| [ToolConfig](./ai.toolconfig.md#toolconfig_interface) | Tool config. This config is shared for all tools provided in the request. |
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# SingleRequestOptions interface
13+
Options that can be provided per-request. Extends the base [RequestOptions](./ai.requestoptions.md#requestoptions_interface) (like `timeout` and `baseUrl`<!-- -->) with request-specific controls like cancellation via `AbortSignal`<!-- -->.
14+
15+
Options specified here will override any default [RequestOptions](./ai.requestoptions.md#requestoptions_interface) configured on a model (for example, [GenerativeModel](./ai.generativemodel.md#generativemodel_class)<!-- -->).
16+
17+
<b>Signature:</b>
18+
19+
```typescript
20+
export interface SingleRequestOptions extends RequestOptions
21+
```
22+
<b>Extends:</b> [RequestOptions](./ai.requestoptions.md#requestoptions_interface)
23+
24+
## Properties
25+
26+
| Property | Type | Description |
27+
| --- | --- | --- |
28+
| [signal](./ai.singlerequestoptions.md#singlerequestoptionssignal) | AbortSignal | An <code>AbortSignal</code> instance that allows cancelling ongoing requests (like <code>generateContent</code> or <code>generateImages</code>).<!-- -->If provided, calling <code>abort()</code> on the corresponding <code>AbortController</code> will attempt to cancel the underlying HTTP request. An <code>AbortError</code> will be thrown if cancellation is successful.<!-- -->Note that this will not cancel the request in the backend, so any applicable billing charges will still be applied despite cancellation. |
29+
30+
## SingleRequestOptions.signal
31+
32+
An `AbortSignal` instance that allows cancelling ongoing requests (like `generateContent` or `generateImages`<!-- -->).
33+
34+
If provided, calling `abort()` on the corresponding `AbortController` will attempt to cancel the underlying HTTP request. An `AbortError` will be thrown if cancellation is successful.
35+
36+
Note that this will not cancel the request in the backend, so any applicable billing charges will still be applied despite cancellation.
37+
38+
<b>Signature:</b>
39+
40+
```typescript
41+
signal?: AbortSignal;
42+
```
43+
44+
### Example
45+
46+
47+
```javascript
48+
const controller = new AbortController();
49+
const model = getGenerativeModel({
50+
// ...
51+
});
52+
model.generateContent(
53+
"Write a story about a magic backpack.",
54+
{ signal: controller.signal }
55+
);
56+
57+
// To cancel request:
58+
controller.abort();
59+
60+
```
61+

0 commit comments

Comments
 (0)