Project Files
src / index.ts
// src/index.ts
import { type PluginContext } from "@lmstudio/sdk";
import { toolsProvider } from "./toolsProvider.js";
import { globalConfigSchematics } from "./config.js";
import { getLogsDir } from "./core-bundle.mjs";
import { warmupBackendAtStartup } from "./core/tools.js";
import fs from "fs";
import path from "path";
export async function main(context: PluginContext) {
context
.withGlobalConfigSchematics(globalConfigSchematics)
.withToolsProvider(toolsProvider);
// Startup file log for diagnostics
try {
const cwd = process.cwd();
const logsDir = getLogsDir();
const ts = () => {
try {
return new Date().toLocaleString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
timeZoneName: "short",
});
} catch {
return new Date().toString();
}
};
if (!fs.existsSync(logsDir)) fs.mkdirSync(logsDir, { recursive: true });
const lines = [
`${ts()} - Plugin start`,
` cwd=${cwd}`,
` node=${process.version} platform=${process.platform} arch=${process.arch}`,
];
fs.appendFileSync(
path.join(logsDir, "generate-image-plugin.log"),
lines.join("\n") + "\n"
);
} catch {}
// Load cached connection config from last run (if available).
// This allows warmup to use the user's configured host/port instead of defaults.
try {
const logsDir = getLogsDir();
const cachePath = path.join(logsDir, "last-connection-config.json");
if (fs.existsSync(cachePath)) {
const cached = JSON.parse(fs.readFileSync(cachePath, "utf-8"));
if (cached.DRAW_THINGS_HOST && !process.env.DRAW_THINGS_HOST) {
process.env.DRAW_THINGS_HOST = cached.DRAW_THINGS_HOST;
}
if (cached.DRAW_THINGS_HTTP_PORT && !process.env.DRAW_THINGS_HTTP_PORT) {
process.env.DRAW_THINGS_HTTP_PORT = cached.DRAW_THINGS_HTTP_PORT;
}
if (cached.DRAW_THINGS_GRPC_PORT && !process.env.DRAW_THINGS_GRPC_PORT) {
process.env.DRAW_THINGS_GRPC_PORT = cached.DRAW_THINGS_GRPC_PORT;
}
}
} catch {}
// Eager backend warmup: probe gRPC/HTTP reachability and select transport.
try {
if (process.env.LMS_BACKEND_WARMED_UP !== "1") {
const logsDir = getLogsDir();
const ts = () => {
try {
return new Date().toLocaleString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
timeZoneName: "short",
});
} catch {
return new Date().toString();
}
};
try {
if (!fs.existsSync(logsDir)) fs.mkdirSync(logsDir, { recursive: true });
fs.appendFileSync(
path.join(logsDir, "generate-image-plugin.log"),
`${ts()} - Startup warmup(main): begin\n`
);
} catch {}
await warmupBackendAtStartup();
process.env.LMS_BACKEND_WARMED_UP = "1";
try {
fs.appendFileSync(
path.join(logsDir, "generate-image-plugin.log"),
`${ts()} - Startup warmup(main): done\n`
);
} catch {}
}
} catch (e) {
try {
const logsDir = getLogsDir();
const ts = () => {
try {
return new Date().toLocaleString(undefined, {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
timeZoneName: "short",
});
} catch {
return new Date().toString();
}
};
if (!fs.existsSync(logsDir)) fs.mkdirSync(logsDir, { recursive: true });
fs.appendFileSync(
path.join(logsDir, "generate-image-plugin.log"),
`${ts()} - Startup warmup(main): error: ${String(
(e as any)?.message || e
)}\n`
);
} catch {}
}
}