Skip to content
Merged
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
2 changes: 1 addition & 1 deletion FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ agy 的 ACP 不收视频(image 块报 `unsupported mimeType`,resource blob
补丁只在这之前做转换:

1. 客户端把视频当作图片发过来。OpenAI 协议写 `image_url`,值可以是 `data:video/mp4;base64,…`,也可以是 http(s) 链接。Magpie 会原样转成 `image` 块交给插件。
2. base64 的直接改成 `video` 块。链接先下载(只认识别得出的视频,或 `Content-Type: video/*`),缓存在 `~/.local/state/magpie-agy-bridge/media/<sha256(链接)>`,同一个链接在后面的历史里再出现时读缓存,一小时没用的缓存在下次下载时清掉。
2. base64 的直接改成 `video` 块。是不是视频看文件头,不看声明的类型:有的客户端把所有附件都写成 `image_url`,视频也标成 `image/png`、`image/jpeg`。链接先下载(只认识别得出的视频,或 `Content-Type: video/*`),缓存在 `~/.local/state/magpie-agy-bridge/media/<sha256(链接)>`,同一个链接在后面的历史里再出现时读缓存,一小时没用的缓存在下次下载时清掉。
3. 之后由上游写文件、开 `view_file`、拒绝非 Gemini 模型。

## 用法
Expand Down
5 changes: 4 additions & 1 deletion fork/patch-e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ test("a video sent as an image block, inline or by link, goes to agy as a file f
assert.equal((await readSse(await ask({ type: "image", source: { type: "base64", media_type: "video/mp4", data: mp4.toString("base64") } }, "inline"))).error, null)
assert.equal((await readSse(await ask({ type: "image", source: { type: "url", url } }, "link"))).error, null)
assert.equal((await readSse(await ask({ type: "video", source: { type: "url", url } }, "link-video"))).error, null)
assert.equal(files.length, 3, "each prompt names a staged file")
// labelled an image, the bytes a video (a client that sends every attachment as image_url)
assert.equal((await readSse(await ask({ type: "image", source: { type: "base64", media_type: "image/png", data: mp4.toString("base64") } }, "mislabelled"))).error, null)
assert.equal((await readSse(await ask({ type: "image_url", image_url: { url: `data:image/jpeg;base64,${mp4.toString("base64")}` } }, "mislabelled-url"))).error, null)
assert.equal(files.length, 5, "each prompt names a staged file")
for (const file of files) {
assert.equal(path.basename(path.dirname(file)), "attachments")
assert.deepEqual(fs.readFileSync(file), mp4)
Expand Down
12 changes: 9 additions & 3 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,23 @@ function blockUrl(block) {
return null
}

// The video type base64 data's first bytes say, or null. Clients that send every
// attachment as image_url label a video image/png or image/jpeg.
const sniffVideoData = (data) => sniffVideo(Buffer.from(data.slice(0, 16), "base64"))

// A block with a video, a data: URL or an http(s) link, as upstream's base64
// image or video block; null for anything else (left for upstream to judge).
async function videoBlock(block) {
const source = block?.source
if (block?.type === "image" && source?.type === "base64" && typeof source.data === "string" && /^video\//i.test(source.media_type || "")) {
return { type: "video", source }
if (block?.type === "image" && source?.type === "base64" && typeof source.data === "string") {
const sniffed = sniffVideoData(source.data)
if (sniffed) return { type: "video", source: { ...source, media_type: sniffed } }
if (/^video\//i.test(source.media_type || "")) return { type: "video", source }
}
const url = blockUrl(block)
if (typeof url !== "string") return null
const data = /^data:([\w.+-]+\/[\w.+-]+)(?:;[^,]*)?;base64,(.*)$/is.exec(url)
if (data) return asBlock(data[1].toLowerCase(), data[2])
if (data) return asBlock(sniffVideoData(data[2]) || data[1].toLowerCase(), data[2])
if (!/^https?:/i.test(url)) return null
const got = await urlVideo(url)
return got && asBlock(got.type, got.bytes.toString("base64"))
Expand Down
Loading