@eduardo191labs/opencode-free-pi 0.3.19 → 0.3.21
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/README.md +4 -3
- package/dist/tools/image.d.ts +11 -1
- package/dist/tools/image.js +137 -45
- package/dist/tools/vision.js +12 -0
- package/package.json +1 -1
- package/src/tools/image.ts +143 -44
- package/src/tools/vision.ts +12 -0
package/README.md
CHANGED
|
@@ -129,9 +129,10 @@ herramienta `look_at_image` con una ruta y una pregunta concreta. El plugin:
|
|
|
129
129
|
- Solo lee archivos dentro de la carpeta del proyecto (incluidos enlaces
|
|
130
130
|
simbólicos resueltos).
|
|
131
131
|
- Acepta PNG, JPEG y WebP.
|
|
132
|
-
-
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
- Lee las dimensiones de la cabecera y envía el archivo intacto mientras no
|
|
133
|
+
supere los 5 MB, conservando la resolución y la calidad originales. Solo las
|
|
134
|
+
imágenes mayores se recodifican con Photon, manteniendo el mayor tamaño y la
|
|
135
|
+
mejor calidad JPEG que quepan en el límite.
|
|
135
136
|
- Envía la imagen a `/tools/vision` y devuelve la respuesta del modelo de
|
|
136
137
|
visión como texto.
|
|
137
138
|
|
package/dist/tools/image.d.ts
CHANGED
|
@@ -41,8 +41,18 @@ export declare function detectMime(bytes: Uint8Array): VisionMime | undefined;
|
|
|
41
41
|
* so a symlink cannot escape the project folder.
|
|
42
42
|
*/
|
|
43
43
|
export declare function loadImage(cwd: string, path: string): LoadedImage;
|
|
44
|
+
export type ImageSize = {
|
|
45
|
+
readonly width: number;
|
|
46
|
+
readonly height: number;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Reads dimensions from the file header. This avoids decoding typical images
|
|
50
|
+
* with WASM just to decide whether they need to be resized.
|
|
51
|
+
*/
|
|
52
|
+
export declare function imageSize(bytes: Buffer, mime: VisionMime): ImageSize | undefined;
|
|
44
53
|
/**
|
|
45
|
-
*
|
|
54
|
+
* Sends the file untouched while it fits the provider's 5 MB limit. Only larger
|
|
55
|
+
* files are decoded, and then kept at the highest resolution/quality that fits.
|
|
46
56
|
*
|
|
47
57
|
* Photon is optional: if its WASM module cannot load, images that already fit
|
|
48
58
|
* are still sent unchanged, while oversized ones fail with a clear message.
|
package/dist/tools/image.js
CHANGED
|
@@ -12,9 +12,14 @@ import { isAbsolute, relative, resolve, sep } from "node:path";
|
|
|
12
12
|
export const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
|
|
13
13
|
/** Refuse to read absurd files before decoding them in memory. */
|
|
14
14
|
const MAX_SOURCE_BYTES = 80 * 1024 * 1024;
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Only images above the provider's 5 MB limit are re-encoded. The CLI sends
|
|
17
|
+
* full-resolution files, and more pixels help OCR; the first JPEG quality that
|
|
18
|
+
* fits is kept instead of forcing an aggressive downscale.
|
|
19
|
+
*/
|
|
20
|
+
const JPEG_QUALITIES = [92, 85, 75, 62, 50];
|
|
21
|
+
/** Refuse to decode absurd canvases that could exhaust memory. */
|
|
22
|
+
const MAX_DECODE_DIMENSION = 8_000;
|
|
18
23
|
const OUTSIDE = "look_at_image only reads images inside the project folder.";
|
|
19
24
|
let photonPromise;
|
|
20
25
|
/** Loads Photon lazily so the plugin startup does not pay for the WASM module. */
|
|
@@ -104,6 +109,92 @@ export function loadImage(cwd, path) {
|
|
|
104
109
|
closeSync(fd);
|
|
105
110
|
}
|
|
106
111
|
}
|
|
112
|
+
function pngSize(bytes) {
|
|
113
|
+
if (bytes.length < 24 || bytes.toString("latin1", 12, 16) !== "IHDR") {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
const width = bytes.readUInt32BE(16);
|
|
117
|
+
const height = bytes.readUInt32BE(20);
|
|
118
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
119
|
+
}
|
|
120
|
+
/** Walks the JPEG markers up to the first Start Of Frame segment. */
|
|
121
|
+
function jpegSize(bytes) {
|
|
122
|
+
let offset = 2;
|
|
123
|
+
while (offset + 9 < bytes.length) {
|
|
124
|
+
if (bytes[offset] !== 0xff) {
|
|
125
|
+
offset += 1;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const marker = bytes[offset + 1];
|
|
129
|
+
if (marker === 0xff) {
|
|
130
|
+
offset += 1;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (marker === 0xd8 ||
|
|
134
|
+
marker === 0xd9 ||
|
|
135
|
+
(marker >= 0xd0 && marker <= 0xd7)) {
|
|
136
|
+
offset += 2;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const length = bytes.readUInt16BE(offset + 2);
|
|
140
|
+
if (length < 2)
|
|
141
|
+
return undefined;
|
|
142
|
+
const sof = (marker >= 0xc0 && marker <= 0xc3) ||
|
|
143
|
+
(marker >= 0xc5 && marker <= 0xc7) ||
|
|
144
|
+
(marker >= 0xc9 && marker <= 0xcb) ||
|
|
145
|
+
(marker >= 0xcd && marker <= 0xcf);
|
|
146
|
+
if (sof) {
|
|
147
|
+
if (offset + 9 > bytes.length)
|
|
148
|
+
return undefined;
|
|
149
|
+
const height = bytes.readUInt16BE(offset + 5);
|
|
150
|
+
const width = bytes.readUInt16BE(offset + 7);
|
|
151
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
152
|
+
}
|
|
153
|
+
offset += 2 + length;
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
function webpSize(bytes) {
|
|
158
|
+
if (bytes.length < 30)
|
|
159
|
+
return undefined;
|
|
160
|
+
const chunk = bytes.toString("latin1", 12, 16);
|
|
161
|
+
if (chunk === "VP8X") {
|
|
162
|
+
const width = 1 + (bytes[24] | (bytes[25] << 8) | (bytes[26] << 16));
|
|
163
|
+
const height = 1 + (bytes[27] | (bytes[28] << 8) | (bytes[29] << 16));
|
|
164
|
+
return { width, height };
|
|
165
|
+
}
|
|
166
|
+
if (chunk === "VP8 ") {
|
|
167
|
+
if (bytes[23] !== 0x9d ||
|
|
168
|
+
bytes[24] !== 0x01 ||
|
|
169
|
+
bytes[25] !== 0x2a) {
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
const width = bytes.readUInt16LE(26) & 0x3fff;
|
|
173
|
+
const height = bytes.readUInt16LE(28) & 0x3fff;
|
|
174
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
175
|
+
}
|
|
176
|
+
if (chunk === "VP8L") {
|
|
177
|
+
if (bytes[20] !== 0x2f)
|
|
178
|
+
return undefined;
|
|
179
|
+
const bits = bytes.readUInt32LE(21);
|
|
180
|
+
return {
|
|
181
|
+
width: (bits & 0x3fff) + 1,
|
|
182
|
+
height: ((bits >>> 14) & 0x3fff) + 1,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Reads dimensions from the file header. This avoids decoding typical images
|
|
189
|
+
* with WASM just to decide whether they need to be resized.
|
|
190
|
+
*/
|
|
191
|
+
export function imageSize(bytes, mime) {
|
|
192
|
+
if (mime === "image/png")
|
|
193
|
+
return pngSize(bytes);
|
|
194
|
+
if (mime === "image/jpeg")
|
|
195
|
+
return jpegSize(bytes);
|
|
196
|
+
return webpSize(bytes);
|
|
197
|
+
}
|
|
107
198
|
function unchanged(bytes, mime, width, height) {
|
|
108
199
|
return {
|
|
109
200
|
ok: true,
|
|
@@ -118,44 +209,61 @@ function unchanged(bytes, mime, width, height) {
|
|
|
118
209
|
resized: false,
|
|
119
210
|
};
|
|
120
211
|
}
|
|
212
|
+
function candidate(bytes, mime) {
|
|
213
|
+
return { data: bytes.toString("base64"), mime, bytes: bytes.length };
|
|
214
|
+
}
|
|
121
215
|
/**
|
|
122
|
-
* Re-encodes the image at `width` x `height
|
|
123
|
-
*
|
|
216
|
+
* Re-encodes the image at `width` x `height`. It keeps the source format when
|
|
217
|
+
* possible and stops at the first JPEG quality that fits, so one call usually
|
|
218
|
+
* costs a single encode instead of five.
|
|
124
219
|
*/
|
|
125
|
-
function encodeCandidate(photon, image, width, height) {
|
|
220
|
+
function encodeCandidate(photon, image, width, height, preferred) {
|
|
126
221
|
const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
|
|
127
222
|
try {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
223
|
+
// Keep PNG screenshots crisp: JPEG artifacts blur UI text and transparency,
|
|
224
|
+
// and the vision model charges by pixels, not by encoded bytes.
|
|
225
|
+
if (preferred === "image/png") {
|
|
226
|
+
const png = Buffer.from(resized.get_bytes());
|
|
227
|
+
if (png.length <= MAX_IMAGE_BYTES)
|
|
228
|
+
return candidate(png, "image/png");
|
|
229
|
+
}
|
|
230
|
+
if (preferred === "image/webp") {
|
|
231
|
+
try {
|
|
232
|
+
const webp = Buffer.from(resized.get_bytes_webp());
|
|
233
|
+
if (webp.length <= MAX_IMAGE_BYTES)
|
|
234
|
+
return candidate(webp, "image/webp");
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// Older Photon builds may not expose WebP encoding.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
132
240
|
for (const quality of JPEG_QUALITIES) {
|
|
133
241
|
const jpeg = Buffer.from(resized.get_bytes_jpeg(quality));
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
mime: "image/jpeg",
|
|
137
|
-
bytes: jpeg.length,
|
|
138
|
-
});
|
|
242
|
+
if (jpeg.length <= MAX_IMAGE_BYTES)
|
|
243
|
+
return candidate(jpeg, "image/jpeg");
|
|
139
244
|
}
|
|
140
|
-
return
|
|
141
|
-
.filter((candidate) => candidate.bytes <= MAX_IMAGE_BYTES)
|
|
142
|
-
.sort((a, b) => a.bytes - b.bytes)[0];
|
|
245
|
+
return undefined;
|
|
143
246
|
}
|
|
144
247
|
finally {
|
|
145
248
|
resized.free();
|
|
146
249
|
}
|
|
147
250
|
}
|
|
148
251
|
/**
|
|
149
|
-
*
|
|
252
|
+
* Sends the file untouched while it fits the provider's 5 MB limit. Only larger
|
|
253
|
+
* files are decoded, and then kept at the highest resolution/quality that fits.
|
|
150
254
|
*
|
|
151
255
|
* Photon is optional: if its WASM module cannot load, images that already fit
|
|
152
256
|
* are still sent unchanged, while oversized ones fail with a clear message.
|
|
153
257
|
*/
|
|
154
258
|
export async function prepareImage(bytes, mime, name) {
|
|
259
|
+
// The normal path never touches WASM, so screenshots and phone photos under
|
|
260
|
+
// 5 MB keep their original resolution and add no local processing latency.
|
|
261
|
+
const size = imageSize(bytes, mime);
|
|
262
|
+
if (bytes.length <= MAX_IMAGE_BYTES) {
|
|
263
|
+
return unchanged(bytes, mime, size?.width ?? 0, size?.height ?? 0);
|
|
264
|
+
}
|
|
155
265
|
const photo = await loadPhoton();
|
|
156
266
|
if (!photo) {
|
|
157
|
-
if (bytes.length <= MAX_IMAGE_BYTES)
|
|
158
|
-
return unchanged(bytes, mime, 0, 0);
|
|
159
267
|
return {
|
|
160
268
|
ok: false,
|
|
161
269
|
text: `${name} is larger than 5 MB and image resizing is unavailable in this OpenCode runtime. Resize it first.`,
|
|
@@ -166,8 +274,6 @@ export async function prepareImage(bytes, mime, name) {
|
|
|
166
274
|
image = photo.PhotonImage.new_from_byteslice(new Uint8Array(bytes));
|
|
167
275
|
}
|
|
168
276
|
catch {
|
|
169
|
-
if (bytes.length <= MAX_IMAGE_BYTES)
|
|
170
|
-
return unchanged(bytes, mime, 0, 0);
|
|
171
277
|
return {
|
|
172
278
|
ok: false,
|
|
173
279
|
text: `${name} could not be decoded, so it cannot be reduced below 5 MB.`,
|
|
@@ -176,23 +282,14 @@ export async function prepareImage(bytes, mime, name) {
|
|
|
176
282
|
try {
|
|
177
283
|
const originalWidth = image.get_width();
|
|
178
284
|
const originalHeight = image.get_height();
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
let width = originalWidth;
|
|
185
|
-
let height = originalHeight;
|
|
186
|
-
if (width > MAX_DIMENSION) {
|
|
187
|
-
height = Math.max(1, Math.round((height * MAX_DIMENSION) / width));
|
|
188
|
-
width = MAX_DIMENSION;
|
|
189
|
-
}
|
|
190
|
-
if (height > MAX_DIMENSION) {
|
|
191
|
-
width = Math.max(1, Math.round((width * MAX_DIMENSION) / height));
|
|
192
|
-
height = MAX_DIMENSION;
|
|
193
|
-
}
|
|
285
|
+
const longest = Math.max(originalWidth, originalHeight);
|
|
286
|
+
let scale = longest > MAX_DECODE_DIMENSION
|
|
287
|
+
? MAX_DECODE_DIMENSION / longest
|
|
288
|
+
: 1;
|
|
194
289
|
while (true) {
|
|
195
|
-
const
|
|
290
|
+
const width = Math.max(1, Math.round(originalWidth * scale));
|
|
291
|
+
const height = Math.max(1, Math.round(originalHeight * scale));
|
|
292
|
+
const best = encodeCandidate(photo, image, width, height, mime);
|
|
196
293
|
if (best) {
|
|
197
294
|
return {
|
|
198
295
|
ok: true,
|
|
@@ -209,12 +306,7 @@ export async function prepareImage(bytes, mime, name) {
|
|
|
209
306
|
}
|
|
210
307
|
if (width === 1 && height === 1)
|
|
211
308
|
break;
|
|
212
|
-
|
|
213
|
-
const nextHeight = height === 1 ? 1 : Math.max(1, Math.floor(height * 0.75));
|
|
214
|
-
if (nextWidth === width && nextHeight === height)
|
|
215
|
-
break;
|
|
216
|
-
width = nextWidth;
|
|
217
|
-
height = nextHeight;
|
|
309
|
+
scale *= 0.85;
|
|
218
310
|
}
|
|
219
311
|
return {
|
|
220
312
|
ok: false,
|
package/dist/tools/vision.js
CHANGED
|
@@ -65,6 +65,7 @@ async function executeVision(ctx, deps, raw, context) {
|
|
|
65
65
|
if (!isFreePiSession(deps, context.sessionID)) {
|
|
66
66
|
return fail("look_at_image is only available in free-pi sessions.");
|
|
67
67
|
}
|
|
68
|
+
const startedAt = Date.now();
|
|
68
69
|
const image = loadImage(ctx.location.directory, path);
|
|
69
70
|
if (!image.ok)
|
|
70
71
|
return fail(image.text);
|
|
@@ -113,10 +114,21 @@ async function executeVision(ctx, deps, raw, context) {
|
|
|
113
114
|
if (typeof data?.text !== "string" || data.text === "") {
|
|
114
115
|
return fail("The vision model returned no answer.");
|
|
115
116
|
}
|
|
117
|
+
const elapsed = Date.now() - startedAt;
|
|
118
|
+
deps.log.info("look_at_image answered", {
|
|
119
|
+
path,
|
|
120
|
+
ms: elapsed,
|
|
121
|
+
resized: prepared.resized,
|
|
122
|
+
bytes: prepared.bytes,
|
|
123
|
+
size: `${prepared.width}x${prepared.height}`,
|
|
124
|
+
mime: prepared.mime,
|
|
125
|
+
model: data.model,
|
|
126
|
+
});
|
|
116
127
|
return done(questionText(path, data.text), {
|
|
117
128
|
ok: true,
|
|
118
129
|
...(typeof data.cost_usd === "number" ? { cost_usd: data.cost_usd } : {}),
|
|
119
130
|
...(typeof data.model === "string" ? { model: data.model } : {}),
|
|
131
|
+
elapsed_ms: elapsed,
|
|
120
132
|
});
|
|
121
133
|
}
|
|
122
134
|
catch (error) {
|
package/package.json
CHANGED
package/src/tools/image.ts
CHANGED
|
@@ -41,9 +41,14 @@ export type PreparedImage =
|
|
|
41
41
|
export const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
|
|
42
42
|
/** Refuse to read absurd files before decoding them in memory. */
|
|
43
43
|
const MAX_SOURCE_BYTES = 80 * 1024 * 1024;
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Only images above the provider's 5 MB limit are re-encoded. The CLI sends
|
|
46
|
+
* full-resolution files, and more pixels help OCR; the first JPEG quality that
|
|
47
|
+
* fits is kept instead of forcing an aggressive downscale.
|
|
48
|
+
*/
|
|
49
|
+
const JPEG_QUALITIES = [92, 85, 75, 62, 50] as const;
|
|
50
|
+
/** Refuse to decode absurd canvases that could exhaust memory. */
|
|
51
|
+
const MAX_DECODE_DIMENSION = 8_000;
|
|
47
52
|
|
|
48
53
|
const OUTSIDE = "look_at_image only reads images inside the project folder.";
|
|
49
54
|
|
|
@@ -143,6 +148,100 @@ export function loadImage(cwd: string, path: string): LoadedImage {
|
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
|
|
151
|
+
export type ImageSize = { readonly width: number; readonly height: number };
|
|
152
|
+
|
|
153
|
+
function pngSize(bytes: Buffer): ImageSize | undefined {
|
|
154
|
+
if (bytes.length < 24 || bytes.toString("latin1", 12, 16) !== "IHDR") {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
const width = bytes.readUInt32BE(16);
|
|
158
|
+
const height = bytes.readUInt32BE(20);
|
|
159
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Walks the JPEG markers up to the first Start Of Frame segment. */
|
|
163
|
+
function jpegSize(bytes: Buffer): ImageSize | undefined {
|
|
164
|
+
let offset = 2;
|
|
165
|
+
while (offset + 9 < bytes.length) {
|
|
166
|
+
if (bytes[offset] !== 0xff) {
|
|
167
|
+
offset += 1;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const marker = bytes[offset + 1]!;
|
|
171
|
+
if (marker === 0xff) {
|
|
172
|
+
offset += 1;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (
|
|
176
|
+
marker === 0xd8 ||
|
|
177
|
+
marker === 0xd9 ||
|
|
178
|
+
(marker >= 0xd0 && marker <= 0xd7)
|
|
179
|
+
) {
|
|
180
|
+
offset += 2;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const length = bytes.readUInt16BE(offset + 2);
|
|
184
|
+
if (length < 2) return undefined;
|
|
185
|
+
const sof =
|
|
186
|
+
(marker >= 0xc0 && marker <= 0xc3) ||
|
|
187
|
+
(marker >= 0xc5 && marker <= 0xc7) ||
|
|
188
|
+
(marker >= 0xc9 && marker <= 0xcb) ||
|
|
189
|
+
(marker >= 0xcd && marker <= 0xcf);
|
|
190
|
+
if (sof) {
|
|
191
|
+
if (offset + 9 > bytes.length) return undefined;
|
|
192
|
+
const height = bytes.readUInt16BE(offset + 5);
|
|
193
|
+
const width = bytes.readUInt16BE(offset + 7);
|
|
194
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
195
|
+
}
|
|
196
|
+
offset += 2 + length;
|
|
197
|
+
}
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function webpSize(bytes: Buffer): ImageSize | undefined {
|
|
202
|
+
if (bytes.length < 30) return undefined;
|
|
203
|
+
const chunk = bytes.toString("latin1", 12, 16);
|
|
204
|
+
if (chunk === "VP8X") {
|
|
205
|
+
const width = 1 + (bytes[24]! | (bytes[25]! << 8) | (bytes[26]! << 16));
|
|
206
|
+
const height = 1 + (bytes[27]! | (bytes[28]! << 8) | (bytes[29]! << 16));
|
|
207
|
+
return { width, height };
|
|
208
|
+
}
|
|
209
|
+
if (chunk === "VP8 ") {
|
|
210
|
+
if (
|
|
211
|
+
bytes[23] !== 0x9d ||
|
|
212
|
+
bytes[24] !== 0x01 ||
|
|
213
|
+
bytes[25] !== 0x2a
|
|
214
|
+
) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
const width = bytes.readUInt16LE(26) & 0x3fff;
|
|
218
|
+
const height = bytes.readUInt16LE(28) & 0x3fff;
|
|
219
|
+
return width > 0 && height > 0 ? { width, height } : undefined;
|
|
220
|
+
}
|
|
221
|
+
if (chunk === "VP8L") {
|
|
222
|
+
if (bytes[20] !== 0x2f) return undefined;
|
|
223
|
+
const bits = bytes.readUInt32LE(21);
|
|
224
|
+
return {
|
|
225
|
+
width: (bits & 0x3fff) + 1,
|
|
226
|
+
height: ((bits >>> 14) & 0x3fff) + 1,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Reads dimensions from the file header. This avoids decoding typical images
|
|
234
|
+
* with WASM just to decide whether they need to be resized.
|
|
235
|
+
*/
|
|
236
|
+
export function imageSize(
|
|
237
|
+
bytes: Buffer,
|
|
238
|
+
mime: VisionMime,
|
|
239
|
+
): ImageSize | undefined {
|
|
240
|
+
if (mime === "image/png") return pngSize(bytes);
|
|
241
|
+
if (mime === "image/jpeg") return jpegSize(bytes);
|
|
242
|
+
return webpSize(bytes);
|
|
243
|
+
}
|
|
244
|
+
|
|
146
245
|
function unchanged(
|
|
147
246
|
bytes: Buffer,
|
|
148
247
|
mime: VisionMime,
|
|
@@ -169,40 +268,51 @@ type Candidate = {
|
|
|
169
268
|
readonly bytes: number;
|
|
170
269
|
};
|
|
171
270
|
|
|
271
|
+
function candidate(bytes: Buffer, mime: VisionMime): Candidate {
|
|
272
|
+
return { data: bytes.toString("base64"), mime, bytes: bytes.length };
|
|
273
|
+
}
|
|
274
|
+
|
|
172
275
|
/**
|
|
173
|
-
* Re-encodes the image at `width` x `height
|
|
174
|
-
*
|
|
276
|
+
* Re-encodes the image at `width` x `height`. It keeps the source format when
|
|
277
|
+
* possible and stops at the first JPEG quality that fits, so one call usually
|
|
278
|
+
* costs a single encode instead of five.
|
|
175
279
|
*/
|
|
176
280
|
function encodeCandidate(
|
|
177
281
|
photon: PhotonModule,
|
|
178
282
|
image: InstanceType<PhotonModule["PhotonImage"]>,
|
|
179
283
|
width: number,
|
|
180
284
|
height: number,
|
|
285
|
+
preferred: VisionMime,
|
|
181
286
|
): Candidate | undefined {
|
|
182
287
|
const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
|
|
183
288
|
try {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
289
|
+
// Keep PNG screenshots crisp: JPEG artifacts blur UI text and transparency,
|
|
290
|
+
// and the vision model charges by pixels, not by encoded bytes.
|
|
291
|
+
if (preferred === "image/png") {
|
|
292
|
+
const png = Buffer.from(resized.get_bytes());
|
|
293
|
+
if (png.length <= MAX_IMAGE_BYTES) return candidate(png, "image/png");
|
|
294
|
+
}
|
|
295
|
+
if (preferred === "image/webp") {
|
|
296
|
+
try {
|
|
297
|
+
const webp = Buffer.from(resized.get_bytes_webp());
|
|
298
|
+
if (webp.length <= MAX_IMAGE_BYTES) return candidate(webp, "image/webp");
|
|
299
|
+
} catch {
|
|
300
|
+
// Older Photon builds may not expose WebP encoding.
|
|
301
|
+
}
|
|
302
|
+
}
|
|
188
303
|
for (const quality of JPEG_QUALITIES) {
|
|
189
304
|
const jpeg = Buffer.from(resized.get_bytes_jpeg(quality));
|
|
190
|
-
|
|
191
|
-
data: jpeg.toString("base64"),
|
|
192
|
-
mime: "image/jpeg",
|
|
193
|
-
bytes: jpeg.length,
|
|
194
|
-
});
|
|
305
|
+
if (jpeg.length <= MAX_IMAGE_BYTES) return candidate(jpeg, "image/jpeg");
|
|
195
306
|
}
|
|
196
|
-
return
|
|
197
|
-
.filter((candidate) => candidate.bytes <= MAX_IMAGE_BYTES)
|
|
198
|
-
.sort((a, b) => a.bytes - b.bytes)[0];
|
|
307
|
+
return undefined;
|
|
199
308
|
} finally {
|
|
200
309
|
resized.free();
|
|
201
310
|
}
|
|
202
311
|
}
|
|
203
312
|
|
|
204
313
|
/**
|
|
205
|
-
*
|
|
314
|
+
* Sends the file untouched while it fits the provider's 5 MB limit. Only larger
|
|
315
|
+
* files are decoded, and then kept at the highest resolution/quality that fits.
|
|
206
316
|
*
|
|
207
317
|
* Photon is optional: if its WASM module cannot load, images that already fit
|
|
208
318
|
* are still sent unchanged, while oversized ones fail with a clear message.
|
|
@@ -212,9 +322,15 @@ export async function prepareImage(
|
|
|
212
322
|
mime: VisionMime,
|
|
213
323
|
name: string,
|
|
214
324
|
): Promise<PreparedImage> {
|
|
325
|
+
// The normal path never touches WASM, so screenshots and phone photos under
|
|
326
|
+
// 5 MB keep their original resolution and add no local processing latency.
|
|
327
|
+
const size = imageSize(bytes, mime);
|
|
328
|
+
if (bytes.length <= MAX_IMAGE_BYTES) {
|
|
329
|
+
return unchanged(bytes, mime, size?.width ?? 0, size?.height ?? 0);
|
|
330
|
+
}
|
|
331
|
+
|
|
215
332
|
const photo = await loadPhoton();
|
|
216
333
|
if (!photo) {
|
|
217
|
-
if (bytes.length <= MAX_IMAGE_BYTES) return unchanged(bytes, mime, 0, 0);
|
|
218
334
|
return {
|
|
219
335
|
ok: false,
|
|
220
336
|
text: `${name} is larger than 5 MB and image resizing is unavailable in this OpenCode runtime. Resize it first.`,
|
|
@@ -225,7 +341,6 @@ export async function prepareImage(
|
|
|
225
341
|
try {
|
|
226
342
|
image = photo.PhotonImage.new_from_byteslice(new Uint8Array(bytes));
|
|
227
343
|
} catch {
|
|
228
|
-
if (bytes.length <= MAX_IMAGE_BYTES) return unchanged(bytes, mime, 0, 0);
|
|
229
344
|
return {
|
|
230
345
|
ok: false,
|
|
231
346
|
text: `${name} could not be decoded, so it cannot be reduced below 5 MB.`,
|
|
@@ -235,27 +350,15 @@ export async function prepareImage(
|
|
|
235
350
|
try {
|
|
236
351
|
const originalWidth = image.get_width();
|
|
237
352
|
const originalHeight = image.get_height();
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
) {
|
|
243
|
-
return unchanged(bytes, mime, originalWidth, originalHeight);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
let width = originalWidth;
|
|
247
|
-
let height = originalHeight;
|
|
248
|
-
if (width > MAX_DIMENSION) {
|
|
249
|
-
height = Math.max(1, Math.round((height * MAX_DIMENSION) / width));
|
|
250
|
-
width = MAX_DIMENSION;
|
|
251
|
-
}
|
|
252
|
-
if (height > MAX_DIMENSION) {
|
|
253
|
-
width = Math.max(1, Math.round((width * MAX_DIMENSION) / height));
|
|
254
|
-
height = MAX_DIMENSION;
|
|
255
|
-
}
|
|
353
|
+
const longest = Math.max(originalWidth, originalHeight);
|
|
354
|
+
let scale = longest > MAX_DECODE_DIMENSION
|
|
355
|
+
? MAX_DECODE_DIMENSION / longest
|
|
356
|
+
: 1;
|
|
256
357
|
|
|
257
358
|
while (true) {
|
|
258
|
-
const
|
|
359
|
+
const width = Math.max(1, Math.round(originalWidth * scale));
|
|
360
|
+
const height = Math.max(1, Math.round(originalHeight * scale));
|
|
361
|
+
const best = encodeCandidate(photo, image, width, height, mime);
|
|
259
362
|
if (best) {
|
|
260
363
|
return {
|
|
261
364
|
ok: true,
|
|
@@ -271,11 +374,7 @@ export async function prepareImage(
|
|
|
271
374
|
};
|
|
272
375
|
}
|
|
273
376
|
if (width === 1 && height === 1) break;
|
|
274
|
-
|
|
275
|
-
const nextHeight = height === 1 ? 1 : Math.max(1, Math.floor(height * 0.75));
|
|
276
|
-
if (nextWidth === width && nextHeight === height) break;
|
|
277
|
-
width = nextWidth;
|
|
278
|
-
height = nextHeight;
|
|
377
|
+
scale *= 0.85;
|
|
279
378
|
}
|
|
280
379
|
|
|
281
380
|
return {
|
package/src/tools/vision.ts
CHANGED
|
@@ -96,6 +96,7 @@ async function executeVision(
|
|
|
96
96
|
return fail("look_at_image is only available in free-pi sessions.");
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
const startedAt = Date.now();
|
|
99
100
|
const image = loadImage(ctx.location.directory, path);
|
|
100
101
|
if (!image.ok) return fail(image.text);
|
|
101
102
|
|
|
@@ -150,10 +151,21 @@ async function executeVision(
|
|
|
150
151
|
if (typeof data?.text !== "string" || data.text === "") {
|
|
151
152
|
return fail("The vision model returned no answer.");
|
|
152
153
|
}
|
|
154
|
+
const elapsed = Date.now() - startedAt;
|
|
155
|
+
deps.log.info("look_at_image answered", {
|
|
156
|
+
path,
|
|
157
|
+
ms: elapsed,
|
|
158
|
+
resized: prepared.resized,
|
|
159
|
+
bytes: prepared.bytes,
|
|
160
|
+
size: `${prepared.width}x${prepared.height}`,
|
|
161
|
+
mime: prepared.mime,
|
|
162
|
+
model: data.model,
|
|
163
|
+
});
|
|
153
164
|
return done(questionText(path, data.text), {
|
|
154
165
|
ok: true,
|
|
155
166
|
...(typeof data.cost_usd === "number" ? { cost_usd: data.cost_usd } : {}),
|
|
156
167
|
...(typeof data.model === "string" ? { model: data.model } : {}),
|
|
168
|
+
elapsed_ms: elapsed,
|
|
157
169
|
});
|
|
158
170
|
} catch (error) {
|
|
159
171
|
if (error instanceof Error && error.name === "TimeoutError") {
|