Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbortSignal support #144

Merged
merged 31 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4e033fd
Add RequestOptions to FileManager
DellaBitta May 7, 2024
e706597
Merge branch 'main' into ddb-request-abort-signal
DellaBitta May 15, 2024
a9440ae
Added RequestOptions to FileManager operations.
DellaBitta May 15, 2024
58bdc31
format
DellaBitta May 15, 2024
291f5bc
remove debug output
DellaBitta May 20, 2024
2d19db5
Added FileManager abort integration tests.
DellaBitta May 20, 2024
2f67539
docs
DellaBitta May 20, 2024
c1f6ddf
Update count-tokens.test.ts
DellaBitta May 20, 2024
c5e8b37
RequestOptions parameter changes. Removal of a cat.
DellaBitta May 20, 2024
95083e1
Created SingleRequestOptions sub interface
DellaBitta May 20, 2024
f4f168c
asynchronous typo.
DellaBitta May 20, 2024
2b30a9e
Rename abortSignal to signal.
DellaBitta May 21, 2024
b125380
docs
DellaBitta May 21, 2024
f7b7b6d
predendence -> precedence typo fix
DellaBitta May 22, 2024
fbc68fd
tests timeout config in conjunction with signal
DellaBitta May 22, 2024
1ec0275
merge main
DellaBitta Jun 5, 2024
1e440f4
docs gen
DellaBitta Jun 5, 2024
7ebd83b
Merge branch 'main' into ddb-request-abort-signal
DellaBitta Jul 16, 2024
b73cb1a
merge main
DellaBitta Jul 16, 2024
20b16fb
ChatSession and GenerativeModel implementation
DellaBitta Jul 17, 2024
c4c8426
remove SRO from some FileManager functions
DellaBitta Jul 17, 2024
c61466a
docs
DellaBitta Jul 17, 2024
6e26b0c
format
DellaBitta Jul 17, 2024
bec6f01
GoogleAIFileManager & GenerativeModel tests.
DellaBitta Jul 17, 2024
5bbbcf5
ChatSessionManager tests
DellaBitta Jul 17, 2024
6abb6d4
extra GoogleAIFileManager variables
DellaBitta Jul 17, 2024
4d7121f
Fix utest failure due to signal now appearing in requests
DellaBitta Jul 18, 2024
5e52c28
yarn docs
DellaBitta Jul 18, 2024
0edc918
AbortSignal in buildFetchOptions only if needed.
DellaBitta Jul 19, 2024
1444eb6
docs
DellaBitta Jul 19, 2024
ac4b721
changeset
DellaBitta Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

## GoogleAIFileManager.deleteFile() method

Delete file with given ID
Delete file with given ID.

Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.
DellaBitta marked this conversation as resolved.
Show resolved Hide resolved

**Signature:**

```typescript
deleteFile(fileId: string): Promise<void>;
deleteFile(fileId: string, requestOptions?: SingleRequestOptions): Promise<void>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fileId | string | |
| requestOptions | [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | _(Optional)_ |

**Returns:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

## GoogleAIFileManager.getFile() method

Get metadata for file with given ID
Get metadata for file with given ID.

Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.

**Signature:**

```typescript
getFile(fileId: string): Promise<FileMetadataResponse>;
getFile(fileId: string, requestOptions?: SingleRequestOptions): Promise<FileMetadataResponse>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| fileId | string | |
| requestOptions | [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | _(Optional)_ |

**Returns:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

## GoogleAIFileManager.listFiles() method

List all uploaded files
List all uploaded files.

Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.

**Signature:**

```typescript
listFiles(listParams?: ListParams): Promise<ListFilesResponse>;
listFiles(listParams?: ListParams, requestOptions?: SingleRequestOptions): Promise<ListFilesResponse>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| listParams | [ListParams](./generative-ai.listparams.md) | _(Optional)_ |
| requestOptions | [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | _(Optional)_ |

**Returns:**

Expand Down
8 changes: 4 additions & 4 deletions docs/reference/files/generative-ai.googleaifilemanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export declare class GoogleAIFileManager

| Method | Modifiers | Description |
| --- | --- | --- |
| [deleteFile(fileId)](./generative-ai.googleaifilemanager.deletefile.md) | | Delete file with given ID |
| [getFile(fileId)](./generative-ai.googleaifilemanager.getfile.md) | | Get metadata for file with given ID |
| [listFiles(listParams)](./generative-ai.googleaifilemanager.listfiles.md) | | List all uploaded files |
| [uploadFile(filePath, fileMetadata)](./generative-ai.googleaifilemanager.uploadfile.md) | | Upload a file |
| [deleteFile(fileId, requestOptions)](./generative-ai.googleaifilemanager.deletefile.md) | | <p>Delete file with given ID.</p><p>Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.</p> |
| [getFile(fileId, requestOptions)](./generative-ai.googleaifilemanager.getfile.md) | | <p>Get metadata for file with given ID.</p><p>Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.</p> |
| [listFiles(listParams, requestOptions)](./generative-ai.googleaifilemanager.listfiles.md) | | <p>List all uploaded files.</p><p>Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.</p> |
| [uploadFile(filePath, fileMetadata, requestOptions)](./generative-ai.googleaifilemanager.uploadfile.md) | | <p>Upload a file.</p><p>Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.</p> |

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

## GoogleAIFileManager.uploadFile() method

Upload a file
Upload a file.

Any fields set in the optional [SingleRequestOptions](./generative-ai.singlerequestoptions.md) parameter will take predendence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.

**Signature:**

```typescript
uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
uploadFile(filePath: string, fileMetadata: FileMetadata, requestOptions?: SingleRequestOptions): Promise<UploadFileResponse>;
```

## Parameters
Expand All @@ -18,6 +20,7 @@ uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResp
| --- | --- | --- |
| filePath | string | |
| fileMetadata | [FileMetadata](./generative-ai.filemetadata.md) | |
| requestOptions | [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | _(Optional)_ |

**Returns:**

Expand Down
1 change: 1 addition & 0 deletions docs/reference/files/generative-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
| [ListFilesResponse](./generative-ai.listfilesresponse.md) | Response from calling [GoogleAIFileManager.listFiles()](./generative-ai.googleaifilemanager.listfiles.md) |
| [ListParams](./generative-ai.listparams.md) | Params to pass to [GoogleAIFileManager.listFiles()](./generative-ai.googleaifilemanager.listfiles.md) |
| [RequestOptions](./generative-ai.requestoptions.md) | Params passed to getGenerativeModel() or GoogleAIFileManager(). |
| [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | Params passed to atomic asynchronous operations. |
| [UploadFileResponse](./generative-ai.uploadfileresponse.md) | Response from calling [GoogleAIFileManager.uploadFile()](./generative-ai.googleaifilemanager.uploadfile.md) |

21 changes: 21 additions & 0 deletions docs/reference/files/generative-ai.singlerequestoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [SingleRequestOptions](./generative-ai.singlerequestoptions.md)

## SingleRequestOptions interface

Params passed to atomic asynchronous operations.

**Signature:**

```typescript
export interface SingleRequestOptions extends RequestOptions
```
**Extends:** [RequestOptions](./generative-ai.requestoptions.md)

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [signal?](./generative-ai.singlerequestoptions.signal.md) | | AbortSignal | _(Optional)_ An object that may be used to abort asynchronous requests. The request may also be aborted due to the expiration of the timeout value, if provided, and if the timeout occurs first. |

13 changes: 13 additions & 0 deletions docs/reference/files/generative-ai.singlerequestoptions.signal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [SingleRequestOptions](./generative-ai.singlerequestoptions.md) &gt; [signal](./generative-ai.singlerequestoptions.signal.md)

## SingleRequestOptions.signal property

An object that may be used to abort asynchronous requests. The request may also be aborted due to the expiration of the timeout value, if provided, and if the timeout occurs first.

**Signature:**

```typescript
signal?: AbortSignal;
```
1 change: 1 addition & 0 deletions docs/reference/main/generative-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
| [RequestOptions](./generative-ai.requestoptions.md) | Params passed to getGenerativeModel() or GoogleAIFileManager(). |
| [SafetyRating](./generative-ai.safetyrating.md) | A safety rating associated with a [GenerateContentCandidate](./generative-ai.generatecontentcandidate.md) |
| [SafetySetting](./generative-ai.safetysetting.md) | Safety setting that can be sent as part of request parameters. |
| [SingleRequestOptions](./generative-ai.singlerequestoptions.md) | Params passed to atomic asynchronous operations. |
| [StartChatParams](./generative-ai.startchatparams.md) | Params for [GenerativeModel.startChat()](./generative-ai.generativemodel.startchat.md)<!-- -->. |
| [TextPart](./generative-ai.textpart.md) | Content part interface if the part represents a text string. |
| [ToolConfig](./generative-ai.toolconfig.md) | Tool config. This config is shared for all tools provided in the request. |
Expand Down
21 changes: 21 additions & 0 deletions docs/reference/main/generative-ai.singlerequestoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [SingleRequestOptions](./generative-ai.singlerequestoptions.md)

## SingleRequestOptions interface

Params passed to atomic asynchronous operations.

**Signature:**

```typescript
export interface SingleRequestOptions extends RequestOptions
```
**Extends:** [RequestOptions](./generative-ai.requestoptions.md)

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [signal?](./generative-ai.singlerequestoptions.signal.md) | | AbortSignal | _(Optional)_ An object that may be used to abort asynchronous requests. The request may also be aborted due to the expiration of the timeout value, if provided, and if the timeout occurs first. |

13 changes: 13 additions & 0 deletions docs/reference/main/generative-ai.singlerequestoptions.signal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [SingleRequestOptions](./generative-ai.singlerequestoptions.md) &gt; [signal](./generative-ai.singlerequestoptions.signal.md)

## SingleRequestOptions.signal property

An object that may be used to abort asynchronous requests. The request may also be aborted due to the expiration of the timeout value, if provided, and if the timeout occurs first.

**Signature:**

```typescript
signal?: AbortSignal;
```
69 changes: 56 additions & 13 deletions packages/main/src/files/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { RequestOptions } from "../../types";
import { RequestOptions, SingleRequestOptions } from "../../types";
import { readFileSync } from "fs";
import { FilesRequestUrl, getHeaders, makeFilesRequest } from "./request";
import {
Expand Down Expand Up @@ -44,21 +44,31 @@ export interface UploadMetadata {
export class GoogleAIFileManager {
constructor(
public apiKey: string,
private _requestOptions?: RequestOptions,
private _requestOptions: RequestOptions = {},
) {}

/**
* Upload a file
* Upload a file.
*
* Any fields set in the optional {@link SingleRequestOptions} parameter will take
* predendence over the {@link RequestOptions} values provided at the time of the
* {@link GoogleAIFileManager} initialization.
*/
async uploadFile(
filePath: string,
fileMetadata: FileMetadata,
requestOptions: SingleRequestOptions = {},
): Promise<UploadFileResponse> {
const file = readFileSync(filePath);
const filesRequestOptions: SingleRequestOptions = {
...this._requestOptions,
...requestOptions,
};

const url = new FilesRequestUrl(
FilesTask.UPLOAD,
this.apiKey,
this._requestOptions,
filesRequestOptions,
);

const uploadHeaders = getHeaders(url);
Expand Down Expand Up @@ -93,13 +103,24 @@ export class GoogleAIFileManager {
}

/**
* List all uploaded files
* List all uploaded files.
*
* Any fields set in the optional {@link SingleRequestOptions} parameter will take
* predendence over the {@link RequestOptions} values provided at the time of the
* {@link GoogleAIFileManager} initialization.
*/
async listFiles(listParams?: ListParams): Promise<ListFilesResponse> {
async listFiles(
listParams?: ListParams,
requestOptions: SingleRequestOptions = {},
): Promise<ListFilesResponse> {
const filesRequestOptions: SingleRequestOptions = {
...this._requestOptions,
...requestOptions,
};
const url = new FilesRequestUrl(
FilesTask.LIST,
this.apiKey,
this._requestOptions,
filesRequestOptions,
);
if (listParams?.pageSize) {
url.appendParam("pageSize", listParams.pageSize.toString());
Expand All @@ -113,13 +134,24 @@ export class GoogleAIFileManager {
}

/**
* Get metadata for file with given ID
* Get metadata for file with given ID.
*
* Any fields set in the optional {@link SingleRequestOptions} parameter will take
* predendence over the {@link RequestOptions} values provided at the time of the
* {@link GoogleAIFileManager} initialization.
*/
async getFile(fileId: string): Promise<FileMetadataResponse> {
async getFile(
fileId: string,
requestOptions: SingleRequestOptions = {},
): Promise<FileMetadataResponse> {
const filesRequestOptions: SingleRequestOptions = {
...this._requestOptions,
...requestOptions,
};
const url = new FilesRequestUrl(
FilesTask.GET,
this.apiKey,
this._requestOptions,
filesRequestOptions,
);
url.appendPath(parseFileId(fileId));
const uploadHeaders = getHeaders(url);
Expand All @@ -128,13 +160,24 @@ export class GoogleAIFileManager {
}

/**
* Delete file with given ID
* Delete file with given ID.
*
* Any fields set in the optional {@link SingleRequestOptions} parameter will take
* predendence over the {@link RequestOptions} values provided at the time of the
* {@link GoogleAIFileManager} initialization.
*/
async deleteFile(fileId: string): Promise<void> {
async deleteFile(
fileId: string,
requestOptions: SingleRequestOptions = {},
): Promise<void> {
const filesRequestOptions: SingleRequestOptions = {
...this._requestOptions,
...requestOptions,
};
const url = new FilesRequestUrl(
FilesTask.DELETE,
this.apiKey,
this._requestOptions,
filesRequestOptions,
);
url.appendPath(parseFileId(fileId));
const uploadHeaders = getHeaders(url);
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

export { GoogleAIFileManager } from "./file-manager";
export * from "./types";
export { RequestOptions } from "../../types";
export { RequestOptions, SingleRequestOptions } from "../../types";
25 changes: 16 additions & 9 deletions packages/main/src/files/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
DEFAULT_BASE_URL,
getClientHeaders,
} from "../requests/request";
import { RequestOptions } from "../../types";
import { SingleRequestOptions } from "../../types";
import { FilesTask } from "./constants";

const taskToMethod = {
Expand All @@ -40,7 +40,7 @@ export class FilesRequestUrl {
constructor(
public task: FilesTask,
public apiKey: string,
public requestOptions?: RequestOptions,
public requestOptions?: SingleRequestOptions,
) {
const apiVersion = this.requestOptions?.apiVersion || DEFAULT_API_VERSION;
const baseUrl = this.requestOptions?.baseUrl || DEFAULT_BASE_URL;
Expand Down Expand Up @@ -131,13 +131,20 @@ export async function makeFilesRequest(
}

/**
* Get AbortSignal if timeout is specified
* Create an AbortSignal based on the timeout and signal in the
* RequestOptions.
*/
function getSignal(requestOptions?: RequestOptions): AbortSignal | null {
if (requestOptions?.timeout >= 0) {
const abortController = new AbortController();
const signal = abortController.signal;
setTimeout(() => abortController.abort(), requestOptions.timeout);
return signal;
function getSignal(requestOptions?: SingleRequestOptions): AbortSignal | null {
if (requestOptions?.signal !== undefined || requestOptions?.timeout >= 0) {
const controller = new AbortController();
if (requestOptions?.timeout >= 0) {
setTimeout(() => controller.abort(), requestOptions.timeout);
}
if (requestOptions.signal) {
requestOptions.signal.addEventListener("abort", () => {
controller.abort();
});
}
return controller.signal;
}
}
Loading
Loading