-
Notifications
You must be signed in to change notification settings - Fork 20.3k
feat: better image handling (auto resize & max size constraints) #26401
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6918d29
wip
rekram1-node 890fec3
Merge branch 'dev' into image-handling
rekram1-node c455e9f
add photon image resizing
rekram1-node 6785a45
resize stuff
rekram1-node a7060c6
type errs
rekram1-node 4850562
load wasm
rekram1-node 8e8fef4
Merge branch 'dev' into image-handling
rekram1-node 99e3393
Merge branch 'dev' into image-handling
rekram1-node dc02e5f
Merge branch 'dev' into image-handling
rekram1-node 628044e
address pr feedback
rekram1-node 065ecfd
Merge branch 'dev' into image-handling
rekram1-node 859697c
Merge branch 'dev' into image-handling
rekram1-node 88af039
feedback
rekram1-node 443b9d5
Merge branch 'dev' into image-handling
rekram1-node 9eb3442
fix attachment config imports
rekram1-node 3ed0ba9
regen sdk
rekram1-node 31e3fe8
feedback
rekram1-node 0f1a4f6
fix
rekram1-node 9b10fe2
fixes
rekram1-node 4436a7b
fix
rekram1-node File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| export * as ConfigAttachment from "./attachment" | ||
|
|
||
| import { Schema } from "effect" | ||
| import { zod } from "@opencode-ai/core/effect-zod" | ||
| import { PositiveInt, withStatics } from "@opencode-ai/core/schema" | ||
|
|
||
| export const Image = Schema.Struct({ | ||
| auto_resize: Schema.optional(Schema.Boolean).annotate({ | ||
| description: "Resize images before sending them to the model when they exceed configured limits (default: true)", | ||
| }), | ||
| max_width: Schema.optional(PositiveInt).annotate({ | ||
| description: "Maximum image width before resizing or rejecting the attachment (default: 2000)", | ||
| }), | ||
| max_height: Schema.optional(PositiveInt).annotate({ | ||
| description: "Maximum image height before resizing or rejecting the attachment (default: 2000)", | ||
| }), | ||
| max_base64_bytes: Schema.optional(PositiveInt).annotate({ | ||
| description: "Maximum base64 payload bytes for an image attachment (default: 4718592)", | ||
| }), | ||
| }) | ||
| .annotate({ identifier: "ImageAttachmentConfig" }) | ||
| .pipe(withStatics((s) => ({ zod: zod(s) }))) | ||
| export type Image = Schema.Schema.Type<typeof Image> | ||
|
|
||
| export const Info = Schema.Struct({ | ||
| image: Schema.optional(Image).annotate({ description: "Image attachment configuration" }), | ||
| }) | ||
| .annotate({ identifier: "AttachmentConfig" }) | ||
| .pipe(withStatics((s) => ({ zod: zod(s) }))) | ||
| export type Info = Schema.Schema.Type<typeof Info> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import { Config } from "@/config/config" | ||
| import type { MessageV2 } from "@/session/message-v2" | ||
| import * as Log from "@opencode-ai/core/util/log" | ||
| import { Context, Effect, Layer, Schema } from "effect" | ||
|
|
||
| const MAX_BASE64_BYTES = 4.5 * 1024 * 1024 | ||
| const MAX_WIDTH = 2000 | ||
| const MAX_HEIGHT = 2000 | ||
| const AUTO_RESIZE = true | ||
| const JPEG_QUALITIES = [80, 85, 70, 55, 40] | ||
| const log = Log.create({ service: "image" }) | ||
|
|
||
| export class PhotonUnavailableError extends Schema.TaggedErrorClass<PhotonUnavailableError>()( | ||
| "ImagePhotonUnavailableError", | ||
| {}, | ||
| ) { | ||
| override get message() { | ||
| return "Photon image processor is unavailable" | ||
| } | ||
| } | ||
|
|
||
| export class InvalidDataUrlError extends Schema.TaggedErrorClass<InvalidDataUrlError>()("ImageInvalidDataUrlError", { | ||
| url: Schema.String, | ||
| }) { | ||
| override get message() { | ||
| return "Image URL must be a base64 data URL" | ||
| } | ||
| } | ||
|
|
||
| export class DecodeError extends Schema.TaggedErrorClass<DecodeError>()("ImageDecodeError", {}) { | ||
| override get message() { | ||
| return "Image could not be decoded" | ||
| } | ||
| } | ||
|
|
||
| export class SizeError extends Schema.TaggedErrorClass<SizeError>()("ImageSizeError", { | ||
| bytes: Schema.Number, | ||
| max: Schema.Number, | ||
| width: Schema.Number, | ||
| height: Schema.Number, | ||
| max_width: Schema.Number, | ||
| max_height: Schema.Number, | ||
| }) { | ||
| override get message() { | ||
| return `Image ${this.width}x${this.height} with base64 size ${this.bytes} exceeds configured limits and could not be resized below ${this.max_width}x${this.max_height}/${this.max} bytes` | ||
| } | ||
| } | ||
|
|
||
| export type Error = PhotonUnavailableError | InvalidDataUrlError | DecodeError | SizeError | ||
|
|
||
| export interface Interface { | ||
| readonly normalize: (input: MessageV2.FilePart) => Effect.Effect<MessageV2.FilePart, Error> | ||
| } | ||
|
|
||
| export class Service extends Context.Service<Service, Interface>()("@opencode/Image") {} | ||
|
|
||
| export const layer = Layer.effect( | ||
| Service, | ||
| Effect.gen(function* () { | ||
| const config = yield* Config.Service | ||
| const loadPhoton = yield* Effect.cached( | ||
| Effect.promise(async () => { | ||
| try { | ||
| const photonWasm = (await import("@silvia-odwyer/photon-node/photon_rs_bg.wasm", { with: { type: "file" } })) | ||
| .default | ||
| // Patched photon-node reads this during module init so Bun compiled binaries use the embedded wasm path. | ||
| ;(globalThis as typeof globalThis & { __OPENCODE_PHOTON_WASM_PATH?: string }).__OPENCODE_PHOTON_WASM_PATH = | ||
| photonWasm | ||
| return await import("@silvia-odwyer/photon-node") | ||
| } catch { | ||
| return null | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| const normalize = Effect.fn("Image.normalize")(function* (input: MessageV2.FilePart) { | ||
| const image = (yield* config.get()).attachment?.image | ||
| const info = { | ||
| autoResize: image?.auto_resize ?? AUTO_RESIZE, | ||
| maxWidth: image?.max_width ?? MAX_WIDTH, | ||
| maxHeight: image?.max_height ?? MAX_HEIGHT, | ||
| maxBase64Bytes: image?.max_base64_bytes ?? MAX_BASE64_BYTES, | ||
| } | ||
| if (!input.url.startsWith("data:") || !input.url.includes(";base64,")) | ||
| return yield* new InvalidDataUrlError({ url: input.url }) | ||
|
|
||
| const base64 = input.url.slice(input.url.indexOf(";base64,") + ";base64,".length) | ||
| const photon = yield* loadPhoton | ||
| if (!photon) return yield* new PhotonUnavailableError() | ||
|
|
||
| const decoded = yield* Effect.sync(() => { | ||
| try { | ||
| return photon.PhotonImage.new_from_byteslice(Buffer.from(base64, "base64")) | ||
| } catch { | ||
| return undefined | ||
| } | ||
| }) | ||
| if (!decoded) return yield* new DecodeError() | ||
|
|
||
| try { | ||
| const originalWidth = decoded.get_width() | ||
| const originalHeight = decoded.get_height() | ||
| if ( | ||
| originalWidth <= info.maxWidth && | ||
| originalHeight <= info.maxHeight && | ||
| Buffer.byteLength(base64, "utf8") <= info.maxBase64Bytes | ||
| ) | ||
| return input | ||
| if (!info.autoResize) | ||
| return yield* new SizeError({ | ||
| bytes: Buffer.byteLength(base64, "utf8"), | ||
| max: info.maxBase64Bytes, | ||
| width: originalWidth, | ||
| height: originalHeight, | ||
| max_width: info.maxWidth, | ||
| max_height: info.maxHeight, | ||
| }) | ||
|
|
||
| const scale = Math.min(1, info.maxWidth / originalWidth, info.maxHeight / originalHeight) | ||
| for (const size of Array.from({ length: 32 }).reduce<Array<{ width: number; height: number }>>((acc) => { | ||
| const previous = acc.at(-1) ?? { | ||
| width: Math.max(1, Math.round(originalWidth * scale)), | ||
| height: Math.max(1, Math.round(originalHeight * scale)), | ||
| } | ||
| const next = | ||
| acc.length === 0 | ||
| ? previous | ||
| : { | ||
| width: previous.width === 1 ? 1 : Math.max(1, Math.floor(previous.width * 0.75)), | ||
| height: previous.height === 1 ? 1 : Math.max(1, Math.floor(previous.height * 0.75)), | ||
| } | ||
| return acc.some((item) => item.width === next.width && item.height === next.height) ? acc : [...acc, next] | ||
| }, [])) { | ||
| const resized = photon.resize(decoded, size.width, size.height, photon.SamplingFilter.Lanczos3) | ||
| const candidate = [ | ||
| { data: Buffer.from(resized.get_bytes()).toString("base64"), mime: "image/png" }, | ||
| ...JPEG_QUALITIES.map((quality) => ({ | ||
| data: Buffer.from(resized.get_bytes_jpeg(quality)).toString("base64"), | ||
| mime: "image/jpeg", | ||
| })), | ||
| ] | ||
| .map((item) => ({ ...item, bytes: Buffer.byteLength(item.data, "utf8") })) | ||
| .find((item) => item.bytes <= info.maxBase64Bytes) | ||
| resized.free() | ||
|
|
||
| if (candidate) { | ||
| log.info("using resized image", { | ||
| from_mime: input.mime, | ||
| to_mime: candidate.mime, | ||
| from: `${originalWidth}x${originalHeight}`, | ||
| to: `${size.width}x${size.height}`, | ||
| }) | ||
| return { | ||
| ...input, | ||
| mime: candidate.mime, | ||
| url: `data:${candidate.mime};base64,${candidate.data}`, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return yield* new SizeError({ | ||
| bytes: Buffer.byteLength(base64, "utf8"), | ||
| max: info.maxBase64Bytes, | ||
| width: originalWidth, | ||
| height: originalHeight, | ||
| max_width: info.maxWidth, | ||
| max_height: info.maxHeight, | ||
| }) | ||
| } finally { | ||
| decoded.free() | ||
| } | ||
| }) | ||
|
|
||
| return Service.of({ normalize }) | ||
| }), | ||
| ) | ||
|
|
||
| export const defaultLayer = layer.pipe(Layer.provide(Config.defaultLayer)) | ||
|
|
||
| export * as Image from "./image" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.