mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
refactor(core): skip the text MIME scan when checking for media (#63823)
This commit is contained in:
committed by
GitHub
Unverified
parent
48de2e315c
commit
0b7a1a04e6
@@ -1,6 +1,10 @@
|
||||
export * as Mime from "./mime.js"
|
||||
|
||||
export function detect(bytes: Uint8Array) {
|
||||
return detectMedia(bytes) ?? (isText(bytes) ? "text/plain" : "application/octet-stream")
|
||||
}
|
||||
|
||||
export function detectMedia(bytes: Uint8Array) {
|
||||
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png"
|
||||
if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg"
|
||||
const signature = ascii(bytes, 0, 6)
|
||||
@@ -29,7 +33,7 @@ export function detect(bytes: Uint8Array) {
|
||||
if (brand === "M4A ") return "audio/mp4"
|
||||
return "video/mp4"
|
||||
}
|
||||
return isText(bytes) ? "text/plain" : "application/octet-stream"
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const isTextual = (mime: string) =>
|
||||
|
||||
@@ -198,8 +198,8 @@ export const Plugin = {
|
||||
return yield* new ToolFailure({ message: `${source} is larger than 20 MiB.` })
|
||||
return (yield* environment.files.read(target.absolute)).bytes
|
||||
})
|
||||
const mime = Mime.detect(bytes)
|
||||
if (!INPUT_TYPES.includes(mime))
|
||||
const mime = Mime.detectMedia(bytes)
|
||||
if (!mime || !INPUT_TYPES.includes(mime))
|
||||
return yield* new ToolFailure({ message: `${source} is not a PNG, JPEG, WebP or GIF image.` })
|
||||
return { data: Buffer.from(bytes).toString("base64"), mime, name: path.basename(source) }
|
||||
})
|
||||
@@ -314,7 +314,8 @@ export const Plugin = {
|
||||
const image = result.images[0]
|
||||
if (!image) return yield* Effect.fail(new Error(`${ref} returned no image.`))
|
||||
const bytes = Buffer.from(image.data, "base64")
|
||||
const file = stem + (EXTENSIONS[Mime.detect(bytes)] ?? ".png")
|
||||
const mime = Mime.detectMedia(bytes)
|
||||
const file = stem + (EXTENSIONS[mime ?? "image/png"] ?? ".png")
|
||||
// The edit prompt covered the requested path. Replacing a different existing file needs its own
|
||||
// approval; a new file only has to pass the deny rules.
|
||||
if (target && file !== target.absolute) {
|
||||
|
||||
@@ -138,8 +138,8 @@ export const read = Effect.fn("ReadTool.read")(function* (
|
||||
)
|
||||
if (first instanceof ListPage) return first
|
||||
|
||||
const media = Mime.detect(first.bytes)
|
||||
if (MEDIA_MIMES.has(media)) {
|
||||
const media = Mime.detectMedia(first.bytes)
|
||||
if (media && MEDIA_MIMES.has(media)) {
|
||||
if (first.info.size > MAX_MEDIA_INGEST_BYTES)
|
||||
return yield* new MediaIngestLimitError({ resource, maximumBytes: MAX_MEDIA_INGEST_BYTES })
|
||||
const whole = yield* readFile(files, input, resource)
|
||||
|
||||
@@ -4,6 +4,15 @@ import { Mime } from "@opencode/core/mime"
|
||||
const ftyp = (hex: string) => Uint8Array.from(Buffer.from(hex, "hex"))
|
||||
|
||||
describe("Mime.detect", () => {
|
||||
test.each([
|
||||
[Uint8Array.of(65), "text/plain"],
|
||||
[Uint8Array.of(0xff), "application/octet-stream"],
|
||||
[Uint8Array.of(0xe2, 0x82), "text/plain"], // A sample may end inside a UTF-8 character.
|
||||
] as const)("leaves samples without media headers to the text fallback: %j", (bytes, mime) => {
|
||||
expect(Mime.detectMedia(bytes)).toBeUndefined()
|
||||
expect(Mime.detect(bytes)).toBe(mime)
|
||||
})
|
||||
|
||||
test("tells ISO-BMFF brands apart", () => {
|
||||
expect(Mime.detect(ftyp("000000206674797061766966000000004d695072617669666d6961666d696631"))).toBe("image/avif")
|
||||
expect(Mime.detect(ftyp("000000186674797061766973000000006d73663161766973"))).toBe("image/avif")
|
||||
|
||||
Reference in New Issue
Block a user