Skip to content
Closed
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 src/image-rescale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function rescaleImage(mediaType: string, b64: string, maxDim: number): Pro
const dims = decodeImageDims(b64);
const gated = (dims !== undefined && Math.max(dims.w, dims.h) > maxDim) || origBytes > RESCALE_TRIGGER_BYTES;
if (!gated) return null;
const hash = createHash("sha256").update(b64, "utf8").digest("hex");
const hash = createHash("sha256").update(`${mime}:${maxDim}:${b64}`, "utf8").digest("hex");
let pending = encodeCache.get(hash);
const cacheHit = pending !== undefined;
if (!pending) {
Expand Down
25 changes: 25 additions & 0 deletions tests/issue3400-image-rescale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,31 @@ test("byte-stable: same history → identical outbound bytes across cache states
assert.equal(out2, out1, "two independent encodes of the same history must be byte-identical (upstream prefix-cache protection)");
});

for (const [firstDim, nextDim] of [[1600, 800], [800, 1600]]) {
test(`encode cache respects a changed dimension limit (${firstDim} to ${nextDim})`, async () => {
const { ctx } = ctxFor(`s-cache-dim-${firstDim}`);
const input = [anthropicImageMsg("msg_dim", BIG.b64)];
const first = await applyImageRescalePass(input, { ...ctx, maxDim: firstDim });
const next = await applyImageRescalePass(input, { ...ctx, maxDim: nextDim });
const imageData = (messages: BiliMessage[]) => (messages[0].rawAnthropicBlock as { source: { data: string } }).source.data;
const decoded = await Jimp.read(Buffer.from(imageData(next), "base64"));
assert.equal(decoded.width, nextDim, "cached encodes must use the current policy");
assert.notEqual(imageData(first), imageData(next));
assert.deepEqual(await applyImageRescalePass(input, { ...ctx, maxDim: nextDim }), next, "the same policy still reuses byte-identical output");
assert.equal(logLines.filter((line) => line.includes("[image-rescale]")).length, 2);
});
}

test("a cached low-benefit result does not suppress a later stricter resize", async () => {
const { ctx } = ctxFor("s-cache-null-dim");
const input = [anthropicImageMsg("msg_null_dim", BMP.b64, "image/bmp")];
assert.equal((await applyImageRescalePass(input, { ...ctx, maxDim: 1600 }))[0], input[0]);
const next = await applyImageRescalePass(input, { ...ctx, maxDim: 800 });
const block = next[0].rawAnthropicBlock as { source: { data: string } };
const decoded = await Jimp.read(Buffer.from(block.source.data, "base64"));
assert.equal(decoded.width, 800);
});

test("openai singular image_url converts to plural parts with inline note", async () => {
const { session, ctx } = ctxFor("s-openai");
session.state.messageRefs.byRaw["msg_o"] = "m20001";
Expand Down
Loading