Add Gemini API Endpoint for Files API #27
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Files API 支持实现说明
我已在
AIStudioToAPI中实现了对 Google Files API 的支持。这涉及对服务端请求处理器和客户端浏览器脚本的修改,以处理二进制文件上传和多步 API 流程。已作出的变更
1. 服务端:src/core/RequestHandler.js
express.json中间件跳过请求(即非 JSON 内容类型,如二进制上传)时,手动读取 POST/PUT/PATCH 请求流的逻辑。body_b64) 并传递给浏览器代理,以防止 JSON 序列化导致的数据损坏。2. 客户端:scripts/client/build.js
b64toBlob辅助函数,用于将接收到的 Base64 数据转换回 Blob 对象,供浏览器的fetchAPI 使用。__proxy_host__查询参数并据此动态切换目标域名的逻辑(例如,从generativelanguage切换到storage.googleapis.com)。Host标头的逻辑,以识别代理自身的地址。_transmitHeaders以拦截location和x-goog-upload-url标头。它将__proxy_host__参数注入到这些 URL 中,确保后续请求能正确路由回代理,随后再转发至正确的 Google 域名。验证清单
用户可以通过尝试使用调用 Google Files API 的工具或脚本(例如,为 Gemini 1.5 Pro 上传图像或文件)来验证此实现。
手动验证步骤
npm run start或node main.js。google-generativeai库或curl)。[Files API Support] Switching target host to: storage.googleapis.com(或类似内容)。[Files API] Converted Base64 body to Blob.[Files API] Rewrote x-goog-upload-url: ...代码差异
// RequestHandler.js - 二进制读取
if ((req.method === "POST" /.../) && Object.keys(req.body || {}).length === 0) {
}
// build.js - 标头重写
if ((lowerKey === 'location' || lowerKey === 'x-goog-upload-url') && v.includes('googleapis.com')) {
}