@mizchi/vlmkit-ai 0.22.0 → 0.23.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/image-gen-client.d.mts +41 -3
- package/dist/image-gen-client.mjs +63 -15
- package/dist/index.d.mts +6 -2
- package/dist/index.mjs +6 -2
- package/dist/llm-client.d.mts +4 -4
- package/dist/llm-client.mjs +4 -4
- package/dist/zoom-drivers.d.mts +33 -0
- package/dist/zoom-drivers.mjs +316 -0
- package/dist/zoom-geometry.d.mts +85 -0
- package/dist/zoom-geometry.mjs +103 -0
- package/dist/zoom-image.d.mts +42 -0
- package/dist/zoom-image.mjs +147 -0
- package/dist/zoom-loop.d.mts +129 -0
- package/dist/zoom-loop.mjs +314 -0
- package/dist/zoom.d.mts +29 -0
- package/dist/zoom.mjs +61 -0
- package/package.json +3 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//#region src/zoom-geometry.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The arithmetic of letting a vision model zoom: which size it sees, which pixels a box it
|
|
4
|
+
* names refers to, and how large the magnified crop comes back. Pure — no image bytes here.
|
|
5
|
+
*
|
|
6
|
+
* The technique is the one Anthropic's multimodal cookbook measured (a chart question went from
|
|
7
|
+
* 36/40 wrong to 40/40 right once the model could crop and magnify), taken without anything
|
|
8
|
+
* provider-specific. Its one load-bearing idea: **the caller, not the provider, decides the size
|
|
9
|
+
* the model sees.** Every provider downscales a large image before the model reads it, by rules
|
|
10
|
+
* that differ between providers and change between releases; a box the model names is in *that*
|
|
11
|
+
* image's pixels, and mapping it back needs to know the size exactly. So the image is resized
|
|
12
|
+
* here to fit a budget below any provider's own limit (`fitToBudget`), the model is told its
|
|
13
|
+
* size in pixels, and a box it names is mapped onto the full-resolution original
|
|
14
|
+
* (`viewBoxToOriginal`) — the crop always comes from the original, never from the view.
|
|
15
|
+
*/
|
|
16
|
+
/** How large an image one message may carry. Providers differ; stay under all of them. */
|
|
17
|
+
interface ImageBudget {
|
|
18
|
+
/** Longest edge, px. */
|
|
19
|
+
maxEdge: number;
|
|
20
|
+
/** Width × height, px². Tile-counting providers bill by area; this bounds the bill too. */
|
|
21
|
+
maxPixels: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 1568px on the long edge and about 1.15 megapixels: the size below which Anthropic documents
|
|
25
|
+
* that it does not resize. Other providers resize by their own rules (OpenAI's high-detail path
|
|
26
|
+
* rescales the short side to 768px; Gemini tiles), and a model reading a resized copy may name
|
|
27
|
+
* boxes in that copy's pixels — which is why the loop also offers normalized coordinates
|
|
28
|
+
* (`coordinates: "normalized"`, 0–1000 on each axis), invariant to any resize. Pass a budget
|
|
29
|
+
* that matches the model you use; nothing here assumes this one.
|
|
30
|
+
*/
|
|
31
|
+
declare const DEFAULT_IMAGE_BUDGET: ImageBudget;
|
|
32
|
+
/**
|
|
33
|
+
* How the model states a box. `pixels`: absolute pixels of the view it was shown (the cookbook's
|
|
34
|
+
* form; exact when the provider does not resize the view again). `normalized`: 0–1000 of the
|
|
35
|
+
* view's width and height, the form several vision models are trained to emit for boxes, and
|
|
36
|
+
* correct whatever size the provider actually showed the model.
|
|
37
|
+
*/
|
|
38
|
+
type ZoomCoordinates = "pixels" | "normalized";
|
|
39
|
+
/** A box as the model stated it, in the view's pixel space. */
|
|
40
|
+
declare function toViewBox(box: Box, view: Size, coordinates: ZoomCoordinates): Box;
|
|
41
|
+
interface Size {
|
|
42
|
+
width: number;
|
|
43
|
+
height: number;
|
|
44
|
+
}
|
|
45
|
+
/** A box in some image's pixel space, x2 > x1 and y2 > y1 (half-open: x1 <= x < x2). */
|
|
46
|
+
interface Box {
|
|
47
|
+
x1: number;
|
|
48
|
+
y1: number;
|
|
49
|
+
x2: number;
|
|
50
|
+
y2: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The largest aspect-preserving size within the budget, never larger than the input. An image
|
|
54
|
+
* that already fits is returned unchanged, so a small screenshot is shown at 1:1.
|
|
55
|
+
*/
|
|
56
|
+
declare function fitToBudget(size: Size, budget?: ImageBudget): Size;
|
|
57
|
+
/**
|
|
58
|
+
* The size a crop is magnified to: the largest aspect-preserving size within the budget, even
|
|
59
|
+
* when that means enlarging it — a 60×20 region comes back filling the budget, which is the
|
|
60
|
+
* point. (Magnifying past the original's own pixels adds no detail, but it does make each
|
|
61
|
+
* original pixel several of the model's, which is what lets it read an 8px gap.)
|
|
62
|
+
*/
|
|
63
|
+
declare function zoomSize(crop: Size, budget?: ImageBudget): Size;
|
|
64
|
+
type BoxProblem = {
|
|
65
|
+
ok: false;
|
|
66
|
+
reason: string;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Clamp a box the model named to the view it saw, and reject one with no area left. Models
|
|
70
|
+
* name boxes past the edge routinely (x2 = 1600 on a 1568px view); clamping is the honest
|
|
71
|
+
* reading. A box that is empty after clamping is an error the model is told about, so it can
|
|
72
|
+
* try again, rather than a crash.
|
|
73
|
+
*/
|
|
74
|
+
declare function clampBox(box: Box, view: Size): {
|
|
75
|
+
ok: true;
|
|
76
|
+
box: Box;
|
|
77
|
+
} | BoxProblem;
|
|
78
|
+
/**
|
|
79
|
+
* The original's pixels a view box covers. Scales on each axis separately (the view's integer
|
|
80
|
+
* size is not an exact multiple of the original's), rounds outward so a 1px feature on the
|
|
81
|
+
* box's edge is kept, and never returns an empty box.
|
|
82
|
+
*/
|
|
83
|
+
declare function viewBoxToOriginal(box: Box, view: Size, original: Size): Box;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { Box, BoxProblem, DEFAULT_IMAGE_BUDGET, ImageBudget, Size, ZoomCoordinates, clampBox, fitToBudget, toViewBox, viewBoxToOriginal, zoomSize };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
//#region src/zoom-geometry.ts
|
|
2
|
+
/**
|
|
3
|
+
* 1568px on the long edge and about 1.15 megapixels: the size below which Anthropic documents
|
|
4
|
+
* that it does not resize. Other providers resize by their own rules (OpenAI's high-detail path
|
|
5
|
+
* rescales the short side to 768px; Gemini tiles), and a model reading a resized copy may name
|
|
6
|
+
* boxes in that copy's pixels — which is why the loop also offers normalized coordinates
|
|
7
|
+
* (`coordinates: "normalized"`, 0–1000 on each axis), invariant to any resize. Pass a budget
|
|
8
|
+
* that matches the model you use; nothing here assumes this one.
|
|
9
|
+
*/
|
|
10
|
+
const DEFAULT_IMAGE_BUDGET = {
|
|
11
|
+
maxEdge: 1568,
|
|
12
|
+
maxPixels: 115e4
|
|
13
|
+
};
|
|
14
|
+
/** A box as the model stated it, in the view's pixel space. */
|
|
15
|
+
function toViewBox(box, view, coordinates) {
|
|
16
|
+
if (coordinates === "pixels") return box;
|
|
17
|
+
return {
|
|
18
|
+
x1: box.x1 / 1e3 * view.width,
|
|
19
|
+
y1: box.y1 / 1e3 * view.height,
|
|
20
|
+
x2: box.x2 / 1e3 * view.width,
|
|
21
|
+
y2: box.y2 / 1e3 * view.height
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The largest aspect-preserving size within the budget, never larger than the input. An image
|
|
26
|
+
* that already fits is returned unchanged, so a small screenshot is shown at 1:1.
|
|
27
|
+
*/
|
|
28
|
+
function fitToBudget(size, budget = DEFAULT_IMAGE_BUDGET) {
|
|
29
|
+
const { width, height } = size;
|
|
30
|
+
if (width <= 0 || height <= 0) throw new RangeError(`not an image size: ${width}x${height}`);
|
|
31
|
+
const scale = Math.min(1, budget.maxEdge / Math.max(width, height), Math.sqrt(budget.maxPixels / (width * height)));
|
|
32
|
+
if (scale >= 1) return {
|
|
33
|
+
width,
|
|
34
|
+
height
|
|
35
|
+
};
|
|
36
|
+
return scaled(size, scale);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The size a crop is magnified to: the largest aspect-preserving size within the budget, even
|
|
40
|
+
* when that means enlarging it — a 60×20 region comes back filling the budget, which is the
|
|
41
|
+
* point. (Magnifying past the original's own pixels adds no detail, but it does make each
|
|
42
|
+
* original pixel several of the model's, which is what lets it read an 8px gap.)
|
|
43
|
+
*/
|
|
44
|
+
function zoomSize(crop, budget = DEFAULT_IMAGE_BUDGET) {
|
|
45
|
+
return scaled(crop, Math.min(budget.maxEdge / Math.max(crop.width, crop.height), Math.sqrt(budget.maxPixels / (crop.width * crop.height))));
|
|
46
|
+
}
|
|
47
|
+
/** Floor with a hair of tolerance: 3000 × (1568 / 3000) is 1567.9999… in floating point, and it means 1568. */
|
|
48
|
+
function scaled(size, scale) {
|
|
49
|
+
return {
|
|
50
|
+
width: Math.max(1, Math.floor(size.width * scale + 1e-6)),
|
|
51
|
+
height: Math.max(1, Math.floor(size.height * scale + 1e-6))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Clamp a box the model named to the view it saw, and reject one with no area left. Models
|
|
56
|
+
* name boxes past the edge routinely (x2 = 1600 on a 1568px view); clamping is the honest
|
|
57
|
+
* reading. A box that is empty after clamping is an error the model is told about, so it can
|
|
58
|
+
* try again, rather than a crash.
|
|
59
|
+
*/
|
|
60
|
+
function clampBox(box, view) {
|
|
61
|
+
if (![
|
|
62
|
+
box.x1,
|
|
63
|
+
box.y1,
|
|
64
|
+
box.x2,
|
|
65
|
+
box.y2
|
|
66
|
+
].every((n) => Number.isFinite(n))) return {
|
|
67
|
+
ok: false,
|
|
68
|
+
reason: "coordinates must be numbers"
|
|
69
|
+
};
|
|
70
|
+
const clamped = {
|
|
71
|
+
x1: Math.max(0, Math.min(Math.round(box.x1), view.width)),
|
|
72
|
+
y1: Math.max(0, Math.min(Math.round(box.y1), view.height)),
|
|
73
|
+
x2: Math.max(0, Math.min(Math.round(box.x2), view.width)),
|
|
74
|
+
y2: Math.max(0, Math.min(Math.round(box.y2), view.height))
|
|
75
|
+
};
|
|
76
|
+
if (clamped.x1 >= clamped.x2 || clamped.y1 >= clamped.y2) return {
|
|
77
|
+
ok: false,
|
|
78
|
+
reason: `invalid region (need x1 < x2 and y1 < y2 inside the ${view.width}x${view.height} image)`
|
|
79
|
+
};
|
|
80
|
+
return {
|
|
81
|
+
ok: true,
|
|
82
|
+
box: clamped
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The original's pixels a view box covers. Scales on each axis separately (the view's integer
|
|
87
|
+
* size is not an exact multiple of the original's), rounds outward so a 1px feature on the
|
|
88
|
+
* box's edge is kept, and never returns an empty box.
|
|
89
|
+
*/
|
|
90
|
+
function viewBoxToOriginal(box, view, original) {
|
|
91
|
+
const sx = original.width / view.width;
|
|
92
|
+
const sy = original.height / view.height;
|
|
93
|
+
const x1 = Math.max(0, Math.floor(box.x1 * sx));
|
|
94
|
+
const y1 = Math.max(0, Math.floor(box.y1 * sy));
|
|
95
|
+
return {
|
|
96
|
+
x1,
|
|
97
|
+
y1,
|
|
98
|
+
x2: Math.min(original.width, Math.max(x1 + 1, Math.ceil(box.x2 * sx))),
|
|
99
|
+
y2: Math.min(original.height, Math.max(y1 + 1, Math.ceil(box.y2 * sy)))
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
export { DEFAULT_IMAGE_BUDGET, clampBox, fitToBudget, toViewBox, viewBoxToOriginal, zoomSize };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Box, ImageBudget, Size } from "./zoom-geometry.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/zoom-image.d.ts
|
|
4
|
+
interface RgbaImage {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
/** RGBA, row-major, 4 bytes per pixel, alpha always 255 after decode. */
|
|
8
|
+
data: Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
/** Decode a PNG and flatten it onto white. Anything else is refused with the reason. */
|
|
11
|
+
declare function decodeImage(buf: Buffer): RgbaImage;
|
|
12
|
+
declare function encodePngImage(img: RgbaImage): Buffer;
|
|
13
|
+
declare function cropImage(img: RgbaImage, box: Box): RgbaImage;
|
|
14
|
+
/** Resize to exactly `size`: area-average on each axis that shrinks, bilinear on one that grows. */
|
|
15
|
+
declare function resample(img: RgbaImage, size: Size): RgbaImage;
|
|
16
|
+
/** One image as the zoom loop holds it: the full-resolution original and the view the model sees. */
|
|
17
|
+
interface ZoomSource {
|
|
18
|
+
original: RgbaImage;
|
|
19
|
+
view: RgbaImage;
|
|
20
|
+
/** The view, encoded — what is actually sent. */
|
|
21
|
+
viewPng: Buffer;
|
|
22
|
+
}
|
|
23
|
+
declare function prepareZoomSource(png: Buffer, budget?: ImageBudget): ZoomSource;
|
|
24
|
+
type ZoomOutcome = {
|
|
25
|
+
ok: true; /** The box in view pixels after clamping. */
|
|
26
|
+
viewBox: Box; /** The original's pixels that were cropped. */
|
|
27
|
+
originalBox: Box; /** The magnified crop, encoded. */
|
|
28
|
+
png: Buffer;
|
|
29
|
+
size: Size; /** What to tell the model alongside the image. */
|
|
30
|
+
text: string;
|
|
31
|
+
} | {
|
|
32
|
+
ok: false;
|
|
33
|
+
text: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Crop the original under a view box and magnify it to the budget. The text states both
|
|
37
|
+
* spaces, so the model can place what it sees in the zoom back onto the view it is reasoning
|
|
38
|
+
* about ("…a 180x60px region of the original, returned at 1568x522px").
|
|
39
|
+
*/
|
|
40
|
+
declare function zoomInto(source: ZoomSource, box: Box, budget?: ImageBudget): ZoomOutcome;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { RgbaImage, ZoomOutcome, ZoomSource, cropImage, decodeImage, encodePngImage, prepareZoomSource, resample, zoomInto };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { DEFAULT_IMAGE_BUDGET, clampBox, fitToBudget, viewBoxToOriginal, zoomSize } from "./zoom-geometry.mjs";
|
|
2
|
+
import { PNG } from "pngjs";
|
|
3
|
+
//#region src/zoom-image.ts
|
|
4
|
+
/**
|
|
5
|
+
* The image half of zooming: make the view the model sees, and cut and magnify a region of
|
|
6
|
+
* the original when it asks. PNG in, PNG out, through `pngjs` only, so no native image library
|
|
7
|
+
* is needed — screenshots are what vlmkit feeds a vision model, and they are PNGs.
|
|
8
|
+
*
|
|
9
|
+
* Resampling is a box (area-average) filter when shrinking, which keeps 1px rules visible in
|
|
10
|
+
* the view instead of dropping them between samples, and bilinear when magnifying. Transparent
|
|
11
|
+
* pixels are composited onto white first: a transparent region encoded as black is the classic
|
|
12
|
+
* way a crop "turns dark" in front of a model.
|
|
13
|
+
*/
|
|
14
|
+
const PNG_SIGNATURE = Buffer.from([
|
|
15
|
+
137,
|
|
16
|
+
80,
|
|
17
|
+
78,
|
|
18
|
+
71,
|
|
19
|
+
13,
|
|
20
|
+
10,
|
|
21
|
+
26,
|
|
22
|
+
10
|
|
23
|
+
]);
|
|
24
|
+
/** Decode a PNG and flatten it onto white. Anything else is refused with the reason. */
|
|
25
|
+
function decodeImage(buf) {
|
|
26
|
+
if (buf.length < 8 || !buf.subarray(0, 8).equals(PNG_SIGNATURE)) throw new Error("zoom reads PNG images only (screenshots); convert JPEG/WebP to PNG first");
|
|
27
|
+
const png = PNG.sync.read(buf);
|
|
28
|
+
const data = new Uint8Array(png.width * png.height * 4);
|
|
29
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
30
|
+
const a = png.data[i + 3] / 255;
|
|
31
|
+
data[i] = Math.round(png.data[i] * a + 255 * (1 - a));
|
|
32
|
+
data[i + 1] = Math.round(png.data[i + 1] * a + 255 * (1 - a));
|
|
33
|
+
data[i + 2] = Math.round(png.data[i + 2] * a + 255 * (1 - a));
|
|
34
|
+
data[i + 3] = 255;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
width: png.width,
|
|
38
|
+
height: png.height,
|
|
39
|
+
data
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function encodePngImage(img) {
|
|
43
|
+
const png = new PNG({
|
|
44
|
+
width: img.width,
|
|
45
|
+
height: img.height
|
|
46
|
+
});
|
|
47
|
+
png.data = Buffer.from(img.data.buffer, img.data.byteOffset, img.data.byteLength);
|
|
48
|
+
return PNG.sync.write(png);
|
|
49
|
+
}
|
|
50
|
+
function cropImage(img, box) {
|
|
51
|
+
const width = box.x2 - box.x1;
|
|
52
|
+
const height = box.y2 - box.y1;
|
|
53
|
+
const data = new Uint8Array(width * height * 4);
|
|
54
|
+
for (let y = 0; y < height; y++) {
|
|
55
|
+
const from = ((box.y1 + y) * img.width + box.x1) * 4;
|
|
56
|
+
data.set(img.data.subarray(from, from + width * 4), y * width * 4);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
width,
|
|
60
|
+
height,
|
|
61
|
+
data
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Resize to exactly `size`: area-average on each axis that shrinks, bilinear on one that grows. */
|
|
65
|
+
function resample(img, size) {
|
|
66
|
+
if (size.width === img.width && size.height === img.height) return img;
|
|
67
|
+
const out = new Uint8Array(size.width * size.height * 4);
|
|
68
|
+
const sx = img.width / size.width;
|
|
69
|
+
const sy = img.height / size.height;
|
|
70
|
+
const px = (x, y, c) => img.data[(y * img.width + x) * 4 + c];
|
|
71
|
+
for (let oy = 0; oy < size.height; oy++) for (let ox = 0; ox < size.width; ox++) {
|
|
72
|
+
const o = (oy * size.width + ox) * 4;
|
|
73
|
+
if (sx > 1 || sy > 1) {
|
|
74
|
+
const x0 = ox * sx, x1 = Math.min(img.width, (ox + 1) * sx);
|
|
75
|
+
const y0 = oy * sy, y1 = Math.min(img.height, (oy + 1) * sy);
|
|
76
|
+
const acc = [
|
|
77
|
+
0,
|
|
78
|
+
0,
|
|
79
|
+
0
|
|
80
|
+
];
|
|
81
|
+
let total = 0;
|
|
82
|
+
for (let y = Math.floor(y0); y < Math.ceil(y1); y++) {
|
|
83
|
+
const wy = Math.min(y + 1, y1) - Math.max(y, y0);
|
|
84
|
+
for (let x = Math.floor(x0); x < Math.ceil(x1); x++) {
|
|
85
|
+
const w = wy * (Math.min(x + 1, x1) - Math.max(x, x0));
|
|
86
|
+
if (w <= 0) continue;
|
|
87
|
+
for (let c = 0; c < 3; c++) acc[c] += px(x, y, c) * w;
|
|
88
|
+
total += w;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (let c = 0; c < 3; c++) out[o + c] = Math.round(acc[c] / total);
|
|
92
|
+
} else {
|
|
93
|
+
const fx = Math.max(0, Math.min(img.width - 1, (ox + .5) * sx - .5));
|
|
94
|
+
const fy = Math.max(0, Math.min(img.height - 1, (oy + .5) * sy - .5));
|
|
95
|
+
const x0 = Math.floor(fx), y0 = Math.floor(fy);
|
|
96
|
+
const x1 = Math.min(img.width - 1, x0 + 1), y1 = Math.min(img.height - 1, y0 + 1);
|
|
97
|
+
const tx = fx - x0, ty = fy - y0;
|
|
98
|
+
for (let c = 0; c < 3; c++) {
|
|
99
|
+
const top = px(x0, y0, c) * (1 - tx) + px(x1, y0, c) * tx;
|
|
100
|
+
const bottom = px(x0, y1, c) * (1 - tx) + px(x1, y1, c) * tx;
|
|
101
|
+
out[o + c] = Math.round(top * (1 - ty) + bottom * ty);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
out[o + 3] = 255;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
width: size.width,
|
|
108
|
+
height: size.height,
|
|
109
|
+
data: out
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function prepareZoomSource(png, budget = DEFAULT_IMAGE_BUDGET) {
|
|
113
|
+
const original = decodeImage(png);
|
|
114
|
+
const view = resample(original, fitToBudget(original, budget));
|
|
115
|
+
return {
|
|
116
|
+
original,
|
|
117
|
+
view,
|
|
118
|
+
viewPng: encodePngImage(view)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Crop the original under a view box and magnify it to the budget. The text states both
|
|
123
|
+
* spaces, so the model can place what it sees in the zoom back onto the view it is reasoning
|
|
124
|
+
* about ("…a 180x60px region of the original, returned at 1568x522px").
|
|
125
|
+
*/
|
|
126
|
+
function zoomInto(source, box, budget = DEFAULT_IMAGE_BUDGET) {
|
|
127
|
+
const clamped = clampBox(box, source.view);
|
|
128
|
+
if (!clamped.ok) return {
|
|
129
|
+
ok: false,
|
|
130
|
+
text: `Error: ${clamped.reason}`
|
|
131
|
+
};
|
|
132
|
+
const originalBox = viewBoxToOriginal(clamped.box, source.view, source.original);
|
|
133
|
+
const crop = cropImage(source.original, originalBox);
|
|
134
|
+
const size = zoomSize(crop, budget);
|
|
135
|
+
const zoomed = resample(crop, size);
|
|
136
|
+
const b = clamped.box;
|
|
137
|
+
return {
|
|
138
|
+
ok: true,
|
|
139
|
+
viewBox: b,
|
|
140
|
+
originalBox,
|
|
141
|
+
png: encodePngImage(zoomed),
|
|
142
|
+
size,
|
|
143
|
+
text: `Zoomed into (${b.x1},${b.y1})-(${b.x2},${b.y2}) of the image you see at ${source.view.width}x${source.view.height}px: a ${crop.width}x${crop.height}px region of the original, returned magnified to ${size.width}x${size.height}px.`
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
export { cropImage, decodeImage, encodePngImage, prepareZoomSource, resample, zoomInto };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Box, ImageBudget, ZoomCoordinates } from "./zoom-geometry.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/zoom-loop.d.ts
|
|
4
|
+
type ZoomPart = {
|
|
5
|
+
type: "text";
|
|
6
|
+
text: string;
|
|
7
|
+
} | {
|
|
8
|
+
type: "image";
|
|
9
|
+
png: Buffer;
|
|
10
|
+
};
|
|
11
|
+
interface ZoomCall {
|
|
12
|
+
/** Provider call id (native), or a synthetic one (text protocol). */
|
|
13
|
+
id: string;
|
|
14
|
+
imageIndex: number;
|
|
15
|
+
box: Box;
|
|
16
|
+
/** Set when the provider's arguments did not parse; the loop answers with it. */
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
interface ZoomResult {
|
|
20
|
+
callId: string;
|
|
21
|
+
/** Text first, then the magnified crop when the zoom succeeded. */
|
|
22
|
+
parts: ZoomPart[];
|
|
23
|
+
isError: boolean;
|
|
24
|
+
}
|
|
25
|
+
type ZoomTurn = {
|
|
26
|
+
role: "user";
|
|
27
|
+
parts: ZoomPart[];
|
|
28
|
+
} | {
|
|
29
|
+
role: "assistant";
|
|
30
|
+
text: string;
|
|
31
|
+
calls: ZoomCall[]; /** The provider's own assistant message, so a driver can replay it verbatim. */
|
|
32
|
+
raw?: unknown;
|
|
33
|
+
} | {
|
|
34
|
+
role: "tool"; /** True when the results answer native function calls; false for the text protocol. */
|
|
35
|
+
native: boolean;
|
|
36
|
+
results: ZoomResult[];
|
|
37
|
+
};
|
|
38
|
+
interface ZoomUsage {
|
|
39
|
+
promptTokens: number;
|
|
40
|
+
completionTokens: number;
|
|
41
|
+
}
|
|
42
|
+
interface DriverTurn {
|
|
43
|
+
text: string;
|
|
44
|
+
calls: ZoomCall[];
|
|
45
|
+
raw?: unknown;
|
|
46
|
+
usage?: ZoomUsage;
|
|
47
|
+
/** The provider stopped for its own reason (length, refusal) — not a finished answer. */
|
|
48
|
+
stop?: string;
|
|
49
|
+
}
|
|
50
|
+
interface ZoomToolSpec {
|
|
51
|
+
name: "zoom";
|
|
52
|
+
description: string;
|
|
53
|
+
/** JSON Schema of the arguments. */
|
|
54
|
+
parameters: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
interface VisionChatDriver {
|
|
57
|
+
model: string;
|
|
58
|
+
/** True when the driver maps `ZoomToolSpec` onto the provider's function calling. */
|
|
59
|
+
nativeTools: boolean;
|
|
60
|
+
/** `tool` is omitted on the wrap-up turn, when the model must answer from what it has seen. */
|
|
61
|
+
turn(transcript: readonly ZoomTurn[], options: {
|
|
62
|
+
tool?: ZoomToolSpec;
|
|
63
|
+
maxTokens: number;
|
|
64
|
+
}): Promise<DriverTurn>;
|
|
65
|
+
}
|
|
66
|
+
interface ZoomImageInput {
|
|
67
|
+
png: Buffer;
|
|
68
|
+
/** Shown before the image, e.g. "Baseline screenshot". */
|
|
69
|
+
label?: string;
|
|
70
|
+
}
|
|
71
|
+
interface ZoomLoopOptions {
|
|
72
|
+
/** Zoom calls allowed before the model must answer (default 6). */
|
|
73
|
+
maxZooms?: number;
|
|
74
|
+
maxTokens?: number;
|
|
75
|
+
budget?: ImageBudget;
|
|
76
|
+
coordinates?: ZoomCoordinates;
|
|
77
|
+
/** Force the text protocol even when the driver has native tools. */
|
|
78
|
+
protocol?: "native" | "text";
|
|
79
|
+
}
|
|
80
|
+
interface ZoomLoopResult {
|
|
81
|
+
answer: string;
|
|
82
|
+
zooms: {
|
|
83
|
+
imageIndex: number;
|
|
84
|
+
viewBox: Box;
|
|
85
|
+
originalBox: Box;
|
|
86
|
+
}[];
|
|
87
|
+
/** Calls the loop refused (bad box, unknown image) — told to the model, not thrown. */
|
|
88
|
+
rejected: {
|
|
89
|
+
call: ZoomCall;
|
|
90
|
+
reason: string;
|
|
91
|
+
}[];
|
|
92
|
+
turns: number;
|
|
93
|
+
/** True when the zoom budget ran out and the model was asked to answer. */
|
|
94
|
+
wrappedUp: boolean;
|
|
95
|
+
protocol: "native" | "text";
|
|
96
|
+
usage: ZoomUsage;
|
|
97
|
+
/** Set when the provider stopped for its own reason on the last turn. */
|
|
98
|
+
stop?: string;
|
|
99
|
+
}
|
|
100
|
+
declare function zoomToolSpec(imageCount: number, coordinates: ZoomCoordinates): ZoomToolSpec;
|
|
101
|
+
/** Arguments a native call carried, whatever shape the provider gave them in. */
|
|
102
|
+
declare function zoomCallFromArgs(id: string, args: unknown): ZoomCall | {
|
|
103
|
+
id: string;
|
|
104
|
+
error: string;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Zoom requests written in a reply, one `ZOOM <image> <x1> <y1> <x2> <y2>` per line. The
|
|
108
|
+
* image index is always written, even with one image, so the grammar has one shape.
|
|
109
|
+
*/
|
|
110
|
+
declare function parseZoomRequests(text: string, turn: number): ZoomCall[];
|
|
111
|
+
interface SingleLookResult {
|
|
112
|
+
answer: string;
|
|
113
|
+
usage: ZoomUsage;
|
|
114
|
+
stop?: string;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The control arm of a zoom measurement: the same driver, the same images resized to the same
|
|
118
|
+
* budget, the same labels and question — and one turn with no tool. Comparing this against
|
|
119
|
+
* `runZoomLoop` isolates what zooming adds; comparing against a different client would also
|
|
120
|
+
* measure the difference between two request builders.
|
|
121
|
+
*/
|
|
122
|
+
declare function runSingleLook(driver: VisionChatDriver, images: readonly ZoomImageInput[], question: string, options?: Pick<ZoomLoopOptions, "maxTokens" | "budget" | "coordinates">): Promise<SingleLookResult>;
|
|
123
|
+
/**
|
|
124
|
+
* Run the loop. `images` are PNGs at any size; each is shown at `budget` and labelled with its
|
|
125
|
+
* index and size. The answer is the model's last reply with no zoom request.
|
|
126
|
+
*/
|
|
127
|
+
declare function runZoomLoop(driver: VisionChatDriver, images: readonly ZoomImageInput[], question: string, options?: ZoomLoopOptions): Promise<ZoomLoopResult>;
|
|
128
|
+
//#endregion
|
|
129
|
+
export { DriverTurn, SingleLookResult, VisionChatDriver, ZoomCall, ZoomImageInput, ZoomLoopOptions, ZoomLoopResult, ZoomPart, ZoomResult, ZoomToolSpec, ZoomTurn, ZoomUsage, parseZoomRequests, runSingleLook, runZoomLoop, zoomCallFromArgs, zoomToolSpec };
|