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

Feat/add buffer support #298

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cows-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": minor
---

added support for buffers in uploadFile
4 changes: 3 additions & 1 deletion common/api-review/generative-ai-server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

```ts

/// <reference types="node" />

// @public
export interface CachedContent extends CachedContentBase {
createTime?: string;
Expand Down Expand Up @@ -347,7 +349,7 @@ export class GoogleAIFileManager {
deleteFile(fileId: string): Promise<void>;
getFile(fileId: string, requestOptions?: SingleRequestOptions): Promise<FileMetadataResponse>;
listFiles(listParams?: ListParams, requestOptions?: SingleRequestOptions): Promise<ListFilesResponse>;
uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
uploadFile(fileData: string | Buffer, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
}

// @public
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/server/generative-ai.googleaifilemanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export declare class GoogleAIFileManager
| [deleteFile(fileId)](./generative-ai.googleaifilemanager.deletefile.md) | | Delete file with given ID. |
| [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 precedence 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 precedence over the [RequestOptions](./generative-ai.requestoptions.md) values provided at the time of the [GoogleAIFileManager](./generative-ai.googleaifilemanager.md) initialization.</p> |
| [uploadFile(filePath, fileMetadata)](./generative-ai.googleaifilemanager.uploadfile.md) | | Upload a file. |
| [uploadFile(fileData, fileMetadata)](./generative-ai.googleaifilemanager.uploadfile.md) | | Upload a file. |

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Upload a file.
**Signature:**

```typescript
uploadFile(filePath: string, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
uploadFile(fileData: string | Buffer, fileMetadata: FileMetadata): Promise<UploadFileResponse>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| filePath | string | |
| fileData | string \| Buffer | |
| fileMetadata | [FileMetadata](./generative-ai.filemetadata.md) | |

**Returns:**
Expand Down
88 changes: 88 additions & 0 deletions src/server/file-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as request from "./request";
import { RpcTask } from "./constants";
import { DEFAULT_API_VERSION } from "../requests/request";
import { FileMetadata } from "../../types/server";
import { readFile } from "fs/promises";

use(sinonChai);
use(chaiAsPromised);
Expand Down Expand Up @@ -61,6 +62,28 @@ describe("GoogleAIFileManager", () => {
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("Content-Type: image/png");
});
it("passes uploadFile request info reading from buffer", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
json: fakeUploadJson,
} as Response);
const fileManager = new GoogleAIFileManager("apiKey");
const fileBuffer = await readFile("./test-utils/cat.png");
const result = await fileManager.uploadFile(fileBuffer, {
mimeType: "image/png",
});
expect(result.file.uri).to.equal(FAKE_URI);
expect(makeRequestStub.args[0][0].task).to.equal(RpcTask.UPLOAD);
expect(makeRequestStub.args[0][0].toString()).to.include("/upload/");
expect(makeRequestStub.args[0][1]).to.be.instanceOf(Headers);
expect(makeRequestStub.args[0][1].get("X-Goog-Upload-Protocol")).to.equal(
"multipart",
);
expect(makeRequestStub.args[0][2]).to.be.instanceOf(Blob);
const bodyBlob = makeRequestStub.args[0][2];
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("Content-Type: image/png");
});
it("passes uploadFile request info and metadata", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
Expand All @@ -80,6 +103,26 @@ describe("GoogleAIFileManager", () => {
expect(blobText).to.include("files/customname");
expect(blobText).to.include("mydisplayname");
});
it("passes uploadFile request info and metadata from buffer", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
json: fakeUploadJson,
} as Response);
const fileManager = new GoogleAIFileManager("apiKey");
const fileBuffer = await readFile("./test-utils/cat.png");
const result = await fileManager.uploadFile(fileBuffer, {
mimeType: "image/png",
name: "files/customname",
displayName: "mydisplayname",
});
expect(result.file.uri).to.equal(FAKE_URI);
expect(makeRequestStub.args[0][2]).to.be.instanceOf(Blob);
const bodyBlob = makeRequestStub.args[0][2];
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("Content-Type: image/png");
expect(blobText).to.include("files/customname");
expect(blobText).to.include("mydisplayname");
});
it("passes uploadFile metadata and formats file name", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
Expand All @@ -95,6 +138,22 @@ describe("GoogleAIFileManager", () => {
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("files/customname");
});
it("passes uploadFile metadata and formats file name from buffer", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
json: fakeUploadJson,
} as Response);
const fileManager = new GoogleAIFileManager("apiKey");
const fileBuffer = await readFile("./test-utils/cat.png");
await fileManager.uploadFile(fileBuffer, {
mimeType: "image/png",
name: "customname",
displayName: "mydisplayname",
});
const bodyBlob = makeRequestStub.args[0][2];
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("files/customname");
});
it("passes uploadFile request info (with options)", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
Expand Down Expand Up @@ -123,6 +182,35 @@ describe("GoogleAIFileManager", () => {
/^http:\/\/mysite\.com/,
);
});
it("passes uploadFile request info (with options) from buffer", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
json: fakeUploadJson,
} as Response);
const fileManager = new GoogleAIFileManager("apiKey", {
apiVersion: "v3000",
baseUrl: "http://mysite.com",
});
const fileBuffer = await readFile("./test-utils/cat.png");
const result = await fileManager.uploadFile(fileBuffer, {
mimeType: "image/png",
});
expect(result.file.uri).to.equal(FAKE_URI);
expect(makeRequestStub.args[0][0].task).to.equal(RpcTask.UPLOAD);
expect(makeRequestStub.args[0][0].toString()).to.include("/upload/");
expect(makeRequestStub.args[0][1]).to.be.instanceOf(Headers);
expect(makeRequestStub.args[0][1].get("X-Goog-Upload-Protocol")).to.equal(
"multipart",
);
expect(makeRequestStub.args[0][2]).to.be.instanceOf(Blob);
const bodyBlob = makeRequestStub.args[0][2];
const blobText = await (bodyBlob as Blob).text();
expect(blobText).to.include("Content-Type: image/png");
expect(makeRequestStub.args[0][0].toString()).to.include("v3000/files");
expect(makeRequestStub.args[0][0].toString()).to.match(
/^http:\/\/mysite\.com/,
);
});
it("passes listFiles request info", async () => {
const makeRequestStub = stub(request, "makeServerRequest").resolves({
ok: true,
Expand Down
5 changes: 3 additions & 2 deletions src/server/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export class GoogleAIFileManager {
* Upload a file.
*/
async uploadFile(
filePath: string,
fileData: string | Buffer,
fileMetadata: FileMetadata,
): Promise<UploadFileResponse> {
const file = readFileSync(filePath);
const file = fileData instanceof Buffer ? fileData : readFileSync(fileData);

const url = new FilesRequestUrl(
RpcTask.UPLOAD,
this.apiKey,
Expand Down