Forked from brdcastro/maestro
"use strict";
/**
* @file Image metadata tools — returns dimensions/size without embedding base64 in context.
*
* IMPORTANT: These tools NEVER return image data (base64) because that would flood the
* conversation history — every subsequent tool call re-sends all prior context, so a single
* 500KB base64 gets re-paid dozens of times. If visual analysis is needed, the user should
* attach the image directly to the chat so the vision model can see it through the proper
* image input channel.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createImageAnalysisTool = createImageAnalysisTool;
exports.createBatchImageAnalysisTool = createBatchImageAnalysisTool;
exports.createCatalogImagesTool = createCatalogImagesTool;
const sdk_1 = require("@lmstudio/sdk");
const zod_1 = require("zod");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const child_process_1 = require("child_process");
const util_1 = require("util");
const ffmpegPath_1 = require("./ffmpegPath");
const shared_1 = require("./shared");
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
const SUPPORTED_EXTENSIONS = new Set([
".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp", ".tiff", ".tif",
]);
/** Get image dimensions via ffprobe — fast, no resize, no base64. */
async function probeDimensions(resolvedPath) {
const ffprobe = await (0, ffmpegPath_1.getFfprobePath)();
try {
const { stdout } = await execFileAsync(ffprobe, [
"-v", "quiet", "-print_format", "json",
"-show_streams", "-select_streams", "v:0", resolvedPath,
]);
const stream = JSON.parse(stdout)?.streams?.[0];
return {
width: stream?.width ?? 0,
height: stream?.height ?? 0,
};
}
catch {
return { width: 0, height: 0 };
}
}
function createImageAnalysisTool(_configMaxDim = 384) {
return (0, sdk_1.tool)({
name: "analyze_image",
description: "Get metadata (dimensions, size) for a single image file. " +
"Does NOT return image data — for visual analysis, attach the image to the chat directly. " +
"Supports JPEG, PNG, WebP, GIF, BMP, TIFF.",
parameters: {
file_path: zod_1.z.string().describe("Absolute path to the image file."),
},
implementation: async ({ file_path }, { status }) => {
const resolvedPath = (0, path_1.isAbsolute)(file_path) ? file_path : (0, path_1.resolve)(file_path);
if (!(0, shared_1.isSafePath)(resolvedPath)) {
return { error: `Access denied: '${resolvedPath}' is in a protected system directory.` };
}
const ext = (0, path_1.extname)(resolvedPath).toLowerCase();
if (!SUPPORTED_EXTENSIONS.has(ext)) {
return { error: `Unsupported image format '${ext}'. Supported: ${[...SUPPORTED_EXTENSIONS].join(", ")}` };
}
let fileSize = 0;
try {
const fileStat = await (0, promises_1.stat)(resolvedPath);
if (!fileStat.isFile())
return { error: `Not a file: ${resolvedPath}` };
fileSize = fileStat.size;
}
catch {
return { error: `File not found: ${resolvedPath}` };
}
status("Probing image...");
const { width, height } = await probeDimensions(resolvedPath);
return {
file_path: resolvedPath,
width: width || undefined,
height: height || undefined,
size_kb: Math.round(fileSize / 1024),
};
},
});
}
/**
* Batch image metadata — returns dimensions/size for all images in a directory.
* Identical to catalog_images; kept for backward compatibility.
*/
function createBatchImageAnalysisTool(_configMaxDim = 512) {
return (0, sdk_1.tool)({
name: "analyze_images",
description: "Get metadata (dimensions, size) for all images in a directory. " +
"Does NOT return image data — for visual analysis, attach images to the chat directly. " +
"Functionally equivalent to catalog_images.",
parameters: {
directory: zod_1.z.string().describe("Path to directory containing images."),
limit: zod_1.z.number().int().min(1).max(200).optional()
.describe("Max number of images to inspect. Default: 50."),
},
implementation: async ({ directory, limit: maxImages }, { status }) => {
const imageLimit = maxImages ?? 50;
const dirPath = (0, path_1.isAbsolute)(directory) ? directory : (0, path_1.resolve)(directory);
if (!(0, shared_1.isSafePath)(dirPath)) {
return { error: `Access denied: '${dirPath}' is in a protected system directory.` };
}
let entries;
try {
const { readdir } = await Promise.resolve().then(() => __importStar(require("fs/promises")));
entries = await readdir(dirPath);
}
catch {
return { error: `Cannot read directory: ${dirPath}` };
}
const allImageFiles = entries
.filter(name => SUPPORTED_EXTENSIONS.has((0, path_1.extname)(name).toLowerCase()))
.sort();
const imageFiles = allImageFiles.slice(0, imageLimit);
if (imageFiles.length === 0) {
return { error: `No supported images found in: ${dirPath}` };
}
status(`Inspecting ${imageFiles.length} images...`);
const results = await Promise.all(imageFiles.map(async (name) => {
const fullPath = (0, path_1.join)(dirPath, name);
try {
const fileStat = await (0, promises_1.stat)(fullPath);
const { width, height } = await probeDimensions(fullPath);
return {
name,
width: width || undefined,
height: height || undefined,
size_kb: Math.round(fileStat.size / 1024),
};
}
catch (err) {
return { name, error: err?.message || String(err) };
}
}));
return {
directory: dirPath,
total_found: allImageFiles.length,
processed: results.length,
images: results,
};
},
});
}
/**
* Catalog images — returns metadata + short filenames for all images in a directory.
* Lightweight alternative to analyze_images when you just need the inventory.
*/
function createCatalogImagesTool() {
return (0, sdk_1.tool)({
name: "catalog_images",
description: "List all images in a directory with metadata (name, dimensions, size). " +
"Use this FIRST to understand available assets before analyzing specific ones. " +
"No image data is returned — just the inventory.",
parameters: {
directory: zod_1.z.string().describe("Path to directory containing images."),
},
implementation: async ({ directory }, { status }) => {
const dirPath = (0, path_1.isAbsolute)(directory) ? directory : (0, path_1.resolve)(directory);
if (!(0, shared_1.isSafePath)(dirPath)) {
return { error: `Access denied: '${dirPath}'` };
}
let entries;
try {
const { readdir } = await Promise.resolve().then(() => __importStar(require("fs/promises")));
entries = await readdir(dirPath);
}
catch {
return { error: `Cannot read directory: ${dirPath}` };
}
const imageFiles = entries
.filter(name => SUPPORTED_EXTENSIONS.has((0, path_1.extname)(name).toLowerCase()))
.sort();
if (imageFiles.length === 0) {
return { directory: dirPath, count: 0, images: [] };
}
status(`Cataloging ${imageFiles.length} images...`);
const ffprobe = await (0, ffmpegPath_1.getFfprobePath)();
const catalog = await Promise.all(imageFiles.map(async (name) => {
const fullPath = (0, path_1.join)(dirPath, name);
try {
const fileStat = await (0, promises_1.stat)(fullPath);
let width = 0, height = 0;
try {
const { stdout } = await execFileAsync(ffprobe, [
"-v", "quiet", "-print_format", "json",
"-show_streams", "-select_streams", "v:0", fullPath,
]);
const stream = JSON.parse(stdout)?.streams?.[0];
width = stream?.width ?? 0;
height = stream?.height ?? 0;
}
catch { }
return {
name,
width, height,
size_kb: Math.round(fileStat.size / 1024),
ext: (0, path_1.extname)(name).toLowerCase(),
};
}
catch {
return { name, error: "unreadable" };
}
}));
return { directory: dirPath, count: catalog.length, images: catalog };
},
});
}
//# sourceMappingURL=imageAnalysis.js.map