-
Notifications
You must be signed in to change notification settings - Fork 215
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
Remove coupling to 'fs' for GoogleAIFileManager.uploadFile #327
Comments
Agreed, we should change it to |
Hi @evanlesmez Thanks for bringing this issue to our attention. Just to clarify: the code in src/server is currently designed for Node environment only, which is why it uses the fs module (not compatible with web browsers). Are you requesting that we add support for the Web File API to enable this functionality in web environments? |
I am not requesting integration of the Web File API. My example project used Vite for bundling.
My solution was to override the // vite.config.ts
import { defineConfig } from 'vite'
import path from 'path'
// https://vite.dev/config/
export default defineConfig({
plugins: [preact()],
resolve: {
alias: {
fs: path.resolve(__dirname, 'src/mocks/fs.js')
}
}
}) // src/mocks/fs.js
export const readFileSync = () => ""; I also adapted the example from the rest API in Gemini vision documentation into Javascript passing an array buffer from the file reader browser API. async function uploadGoogleAIFileManager(buffer, file, displayName) {
const mimeType = file.type;
const numBytes = file.size;
// Start resumable upload
const startRes = await fetch(
`${BASE_URL}/upload/v1beta/files?key=${apiKey}`,
{
method: "POST",
headers: {
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
"X-Goog-Upload-Header-Content-Length": numBytes,
"X-Goog-Upload-Header-Content-Type": mimeType,
"Content-Type": "application/json",
},
body: JSON.stringify({ file: { display_name: displayName } }),
}
);
const uploadUrl = startRes.headers.get("X-Goog-Upload-URL");
if (!uploadUrl) throw new Error("Failed to get upload URL");
// Upload the actual file
const uploadRes = await fetch(uploadUrl, {
method: "POST",
headers: {
"Content-Length": numBytes,
"X-Goog-Upload-Offset": "0",
"X-Goog-Upload-Command": "upload, finalize",
},
body: buffer,
});
const fileInfo = await uploadRes.json();
return fileInfo;
} IMO it is reasonable that people may want to interact with large files that need to be uploaded before inference in a browser targeted project so it would be nice to accommodate them instead of having them each re-write the same wrappers and config overwrites. |
@evanlesmez got it, the change to support upload with buffer is in PR #365, thanks |
Description of the feature request:
The following line breaks the file manager client's upload file in browser environment where 'fs' is not present.
generative-ai-js/src/server/file-manager.ts
Line 57 in 2df2af0
Seems limiting to force access to actual file system.
What problem are you trying to solve with this feature?
No response
Any other information you'd like to share?
No response
The text was updated successfully, but these errors were encountered: