Skip to content

Commit bc4dc9e

Browse files
committed
refactor(core): drop image dimensions from image_generate
1 parent 2c1e120 commit bc4dc9e

2 files changed

Lines changed: 4 additions & 40 deletions

File tree

‎packages/core/src/tool/plugin/image-generate.ts‎

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ export const Input = Schema.Struct({
5757
export const Output = Schema.Struct({
5858
path: Schema.String,
5959
model: Schema.String.annotate({ description: "providerID/modelID" }),
60-
width: Schema.optionalKey(Schema.Int),
61-
height: Schema.optionalKey(Schema.Int),
6260
})
6361

6462
export const Plugin = {
@@ -252,14 +250,13 @@ export const Plugin = {
252250
if (yield* exists(file))
253251
return yield* new ToolFailure({ message: `${file} already exists. Choose a new path.` })
254252
yield* environment.files.write(file, bytes)
255-
const size = dimensions(bytes, mime)
256253
const note = mime === "image/png" ? "" : ` The model returned ${mime}, so the file is in that format.`
257254
return {
258-
output: { path: file, model: ref, ...size },
255+
output: { path: file, model: ref },
259256
content: [
260257
{
261258
type: "text" as const,
262-
text: `Saved ${size ? `${size.width}x${size.height} ` : ""}image to ${file} (${ref}).${note}`,
259+
text: `Saved image to ${file} (${ref}).${note}`,
263260
},
264261
{ type: "file" as const, uri: `data:${mime};base64,${image.data}`, mime, name: file },
265262
],
@@ -290,34 +287,3 @@ export function defaultModel(
290287
function acceptsImages(model: Model.Info) {
291288
return model.capabilities.input.some((item) => item.startsWith("image"))
292289
}
293-
294-
/** Reads pixel dimensions from the image header. */
295-
export function dimensions(bytes: Uint8Array, mime: string) {
296-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
297-
if (mime === "image/png" && bytes.length >= 24) return { width: view.getUint32(16), height: view.getUint32(20) }
298-
if (mime === "image/gif" && bytes.length >= 10)
299-
return { width: view.getUint16(6, true), height: view.getUint16(8, true) }
300-
if (mime === "image/webp" && bytes.length >= 31) {
301-
const chunk = String.fromCharCode(...bytes.subarray(12, 16))
302-
if (chunk === "VP8 ") return { width: view.getUint16(26, true) & 0x3fff, height: view.getUint16(28, true) & 0x3fff }
303-
if (chunk === "VP8L") {
304-
const bits = view.getUint32(21, true)
305-
return { width: (bits & 0x3fff) + 1, height: ((bits >> 14) & 0x3fff) + 1 }
306-
}
307-
if (chunk === "VP8X")
308-
return {
309-
width: (view.getUint32(24, true) & 0xffffff) + 1,
310-
height: (view.getUint32(27, true) & 0xffffff) + 1,
311-
}
312-
}
313-
if (mime === "image/jpeg") {
314-
let offset = 2
315-
while (offset + 9 < bytes.length && bytes[offset] === 0xff) {
316-
const marker = bytes[offset + 1]!
317-
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc)
318-
return { width: view.getUint16(offset + 7), height: view.getUint16(offset + 5) }
319-
offset += 2 + view.getUint16(offset + 2)
320-
}
321-
}
322-
return undefined
323-
}

‎packages/core/test/tool-image-generate.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { executeTool, toolDefinitions, toolIdentity } from "./lib/tool"
2121
import { makePluginTestLayer } from "./plugin/fixture"
2222

2323
const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
24-
// 5x3, with EXIF and Photoshop segments ahead of the frame header.
2524
const JPEG =
2625
"/9j/4AAQSkZJRgABAQAASABIAAD/4QBMRXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAABaADAAQAAAABAAAAAwAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAAwAFAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMAAgICAgICAwICAwUDAwMFBgUFBQUGCAYGBgYGCAoICAgICAgKCgoKCgoKCgwMDAwMDA4ODg4ODw8PDw8PDw8PD//bAEMBAgICBAQEBwQEBxALCQsQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEP/dAAQAAf/aAAwDAQACEQMRAD8A7iiiiv8AQA+XP//Z"
2726

@@ -121,10 +120,10 @@ describe("image_generate", () => {
121120

122121
const result = yield* run({ prompt: "make it blue", images: ["in.png"], path: "out.png" })
123122
const file = path.join(directory, "out.png")
124-
expect(result.output).toEqual({ path: file, model: "test/painter", width: 1, height: 1 })
123+
expect(result.output).toEqual({ path: file, model: "test/painter" })
125124
expect(result.content?.[0]).toEqual({
126125
type: "text",
127-
text: `Saved 1x1 image to ${file} (test/painter).`,
126+
text: `Saved image to ${file} (test/painter).`,
128127
})
129128
expect(result.content?.[1]).toMatchObject({ type: "file", mime: "image/png", name: file })
130129
expect((yield* Effect.promise(() => fs.readFile(file))).toString("base64")).toBe(PNG)
@@ -162,7 +161,6 @@ describe("image_generate", () => {
162161
const result = yield* run({ prompt: "a cat" })
163162
expect(path.dirname(result.output.path)).toBe(path.join(global.tmp, "images", session.id))
164163
expect(path.extname(result.output.path)).toBe(".jpg")
165-
expect(result.output).toMatchObject({ width: 5, height: 3 })
166164
expect(text(result)).toContain("The model returned image/jpeg, so the file is in that format.")
167165
expect(yield* Effect.promise(() => Bun.file(result.output.path).exists())).toBe(true)
168166

0 commit comments

Comments
 (0)