Project Files
src / tools / upscale.ts
import { tool, type Tool, type ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { UpscaleToolParamsShape, formatToolMetaBlock, setActiveChatContext, syncAttachmentsToState } from "../core-bundle.mjs";
import path from "path";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { quality: _hiddenQuality, prompt: _hiddenPrompt, model: _hiddenModel, ...UpscaleToolParamsShapeAgent } = UpscaleToolParamsShape;
export function createUpscaleTool(ctl: ToolsProviderController): Tool {
return tool({
name: "upscale",
description: `Re-render the canvas at a higher resolution via Draw Things image2image. No crop — uses the full canvas as-is.
Use scaleFactor to multiply the canvas dimensions (e.g. scaleFactor=2 doubles width and height).
Alternatively, provide explicit width and/or height. Maximum output: 2048×2048.
Parameters:
- canvas: Source image. Notation: 'a1', 'v2', 'p1', 'i3'. Shorthand: 'a'→'a1', 'i'→'i1', etc.
- scaleFactor: Multiply canvas dimensions by this factor. Mutually exclusive with width/height.
- width / height: Explicit output dimensions (max 2048). Mutually exclusive with scaleFactor.
- imageFormat: Target aspect ratio preset (e.g. 'square', 'landscape', 'portrait', '16:9'). Combinable with scaleFactor.
Returns: Inline preview, JSON with file URLs, model info, and clickable links.
${formatToolMetaBlock()}`,
parameters: UpscaleToolParamsShapeAgent as Record<string, z.ZodTypeAny>,
implementation: async (args: any, ctx: any) => {
const onProgress = (step: number, totalSteps: number | undefined, message?: string) => {
try {
if (step === -1 && message) { ctx.status(message); return; }
if (totalSteps && totalSteps > 0) {
ctx.status(`Step ${step}/${totalSteps} (${Math.round((step / (totalSteps + 1)) * 100)}%)`);
} else {
ctx.status(`Step ${step}...`);
}
} catch {}
};
try {
const workingDir = ctl.getWorkingDirectory();
if (typeof workingDir === "string" && workingDir.trim().length > 0) {
const chatId = path.basename(workingDir);
if (/^\d+$/.test(chatId)) {
setActiveChatContext({ chatId, workingDir, requestId: `tool-${Date.now()}` });
}
}
} catch {}
try {
const workingDir = ctl.getWorkingDirectory();
if (typeof workingDir === "string" && workingDir.trim().length > 0) {
await syncAttachmentsToState(workingDir, false, Number.MAX_SAFE_INTEGER);
}
} catch {}
const mod = await import("../core/tools.js");
const resp = await mod.handleUpscale(args, onProgress);
if (resp && Array.isArray(resp.content)) return resp.content;
return typeof resp === "string" ? resp : JSON.stringify(resp);
},
});
}