Skip to content

Commit e700ba0

Browse files
authored
Merge 247c666 into 5940794
2 parents 5940794 + 247c666 commit e700ba0

34 files changed

+1604
-215
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.

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"src/index.node.ts",
3838
"--timeout",
3939
"5000",
40-
"integration/**/*.test.ts"
40+
"integration/**/prompt-templates.test.ts"
4141
],
4242
"env": {
4343
"TS_NODE_COMPILER_OPTIONS": "{\"module\":\"commonjs\"}"

common/api-review/ai.api.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ export class ChatSession {
150150
params?: StartChatParams | undefined;
151151
// (undocumented)
152152
requestOptions?: RequestOptions | undefined;
153-
sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
154-
sendMessageStream(request: string | Array<string | Part>): Promise<GenerateContentStreamResult>;
153+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
154+
sendMessageStream(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
155155
}
156156

157157
// @beta
@@ -539,9 +539,9 @@ export interface GenerativeContentBlob {
539539
export class GenerativeModel extends AIModel {
540540
// Warning: (ae-incompatible-release-tags) The symbol "__constructor" is marked as @public, but its signature references "ChromeAdapter" which is marked as @beta
541541
constructor(ai: AI, modelParams: ModelParams, requestOptions?: RequestOptions, chromeAdapter?: ChromeAdapter | undefined);
542-
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
543-
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
544-
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
542+
countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
543+
generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
544+
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
545545
// (undocumented)
546546
generationConfig: GenerationConfig;
547547
// (undocumented)
@@ -784,9 +784,9 @@ export interface ImagenInlineImage {
784784
// @public
785785
export class ImagenModel extends AIModel {
786786
constructor(ai: AI, modelParams: ImagenModelParams, requestOptions?: RequestOptions | undefined);
787-
generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
787+
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
788788
// @internal
789-
generateImagesGCS(prompt: string, gcsURI: string): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
789+
generateImagesGCS(prompt: string, gcsURI: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenGCSImage>>;
790790
generationConfig?: ImagenGenerationConfig;
791791
// (undocumented)
792792
requestOptions?: RequestOptions | undefined;
@@ -1294,6 +1294,11 @@ export interface Segment {
12941294
text: string;
12951295
}
12961296

1297+
// @public
1298+
export interface SingleRequestOptions extends RequestOptions {
1299+
signal?: AbortSignal;
1300+
}
1301+
12971302
// @beta
12981303
export interface SpeechConfig {
12991304
voiceConfig?: VoiceConfig;
@@ -1333,8 +1338,9 @@ export class TemplateGenerativeModel {
13331338
constructor(ai: AI, requestOptions?: RequestOptions);
13341339
// @internal (undocumented)
13351340
_apiSettings: ApiSettings;
1336-
generateContent(templateId: string, templateVariables: object): Promise<GenerateContentResult>;
1337-
generateContentStream(templateId: string, templateVariables: object): Promise<GenerateContentStreamResult>;
1341+
generateContent(templateId: string, templateVariables: object, // anything!
1342+
singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
1343+
generateContentStream(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
13381344
requestOptions?: RequestOptions;
13391345
}
13401346

@@ -1343,7 +1349,7 @@ export class TemplateImagenModel {
13431349
constructor(ai: AI, requestOptions?: RequestOptions);
13441350
// @internal (undocumented)
13451351
_apiSettings: ApiSettings;
1346-
generateImages(templateId: string, templateVariables: object): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
1352+
generateImages(templateId: string, templateVariables: object, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
13471353
requestOptions?: RequestOptions;
13481354
}
13491355

docs-devsite/_toc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ toc:
190190
path: /docs/reference/js/ai.searchentrypoint.md
191191
- title: Segment
192192
path: /docs/reference/js/ai.segment.md
193+
- title: SingleRequestOptions
194+
path: /docs/reference/js/ai.singlerequestoptions.md
193195
- title: SpeechConfig
194196
path: /docs/reference/js/ai.speechconfig.md
195197
- title: StartAudioConversationOptions

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

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

106106
```typescript
107-
sendMessage(request: string | Array<string | Part>): Promise<GenerateContentResult>;
107+
sendMessage(request: string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
108108
```
109109

110110
#### Parameters
111111

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

116117
<b>Returns:</b>
117118

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

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

130131
#### Parameters
131132

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

136138
<b>Returns:</b>
137139

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)
@@ -119,14 +119,15 @@ Counts the tokens in the provided request.
119119
<b>Signature:</b>
120120
121121
```typescript
122-
countTokens(request: CountTokensRequest | string | Array<string | Part>): Promise<CountTokensResponse>;
122+
countTokens(request: CountTokensRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<CountTokensResponse>;
123123
```
124124
125125
#### Parameters
126126
127127
| Parameter | Type | Description |
128128
| --- | --- | --- |
129129
| request | [CountTokensRequest](./ai.counttokensrequest.md#counttokensrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
130+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
130131
131132
<b>Returns:</b>
132133
@@ -139,14 +140,15 @@ Makes a single non-streaming call to the model and returns an object containing
139140
<b>Signature:</b>
140141
141142
```typescript
142-
generateContent(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentResult>;
143+
generateContent(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentResult>;
143144
```
144145
145146
#### Parameters
146147
147148
| Parameter | Type | Description |
148149
| --- | --- | --- |
149150
| request | [GenerateContentRequest](./ai.generatecontentrequest.md#generatecontentrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
151+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
150152
151153
<b>Returns:</b>
152154
@@ -159,14 +161,15 @@ Makes a single streaming call to the model and returns an object containing an i
159161
<b>Signature:</b>
160162
161163
```typescript
162-
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>): Promise<GenerateContentStreamResult>;
164+
generateContentStream(request: GenerateContentRequest | string | Array<string | Part>, singleRequestOptions?: SingleRequestOptions): Promise<GenerateContentStreamResult>;
163165
```
164166
165167
#### Parameters
166168
167169
| Parameter | Type | Description |
168170
| --- | --- | --- |
169171
| request | [GenerateContentRequest](./ai.generatecontentrequest.md#generatecontentrequest_interface) \| string \| Array&lt;string \| [Part](./ai.md#part)<!-- -->&gt; | |
172+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
170173
171174
<b>Returns:</b>
172175

docs-devsite/ai.imagenmodel.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export declare class ImagenModel extends AIModel
3939
4040
| Method | Modifiers | Description |
4141
| --- | --- | --- |
42-
| [generateImages(prompt)](./ai.imagenmodel.md#imagenmodelgenerateimages) | | Generates images using the Imagen model and returns them as base64-encoded strings. |
42+
| [generateImages(prompt, singleRequestOptions)](./ai.imagenmodel.md#imagenmodelgenerateimages) | | Generates images using the Imagen model and returns them as base64-encoded strings. |
4343
4444
## ImagenModel.(constructor)
4545
@@ -100,14 +100,15 @@ If the prompt was not blocked, but one or more of the generated images were filt
100100
<b>Signature:</b>
101101
102102
```typescript
103-
generateImages(prompt: string): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
103+
generateImages(prompt: string, singleRequestOptions?: SingleRequestOptions): Promise<ImagenGenerationResponse<ImagenInlineImage>>;
104104
```
105105
106106
#### Parameters
107107
108108
| Parameter | Type | Description |
109109
| --- | --- | --- |
110110
| prompt | string | A text prompt describing the image(s) to generate. |
111+
| singleRequestOptions | [SingleRequestOptions](./ai.singlerequestoptions.md#singlerequestoptions_interface) | |
111112
112113
<b>Returns:</b>
113114

docs-devsite/ai.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The Firebase AI Web SDK.
133133
| [SchemaShared](./ai.schemashared.md#schemashared_interface) | Basic [Schema](./ai.schema.md#schema_class) properties shared across several Schema-related types. |
134134
| [SearchEntrypoint](./ai.searchentrypoint.md#searchentrypoint_interface) | Google search entry point. |
135135
| [Segment](./ai.segment.md#segment_interface) | Represents a specific segment within a [Content](./ai.content.md#content_interface) object, often used to pinpoint the exact location of text or data that grounding information refers to. |
136+
| [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)<!-- -->). |
136137
| [SpeechConfig](./ai.speechconfig.md#speechconfig_interface) | <b><i>(Public Preview)</i></b> Configures speech synthesis. |
137138
| [StartAudioConversationOptions](./ai.startaudioconversationoptions.md#startaudioconversationoptions_interface) | <b><i>(Public Preview)</i></b> Options for [startAudioConversation()](./ai.md#startaudioconversation_01c8e7f)<!-- -->. |
138139
| [StartChatParams](./ai.startchatparams.md#startchatparams_interface) | Params for [GenerativeModel.startChat()](./ai.generativemodel.md#generativemodelstartchat)<!-- -->. |
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)