toolsProvider.js
"use strict";
/**
* Context Plugin — toolsProvider
*
* Tools:
* context(action) — clipboard | active_window | running_apps | system
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.toolsProvider = void 0;
const sdk_1 = require("@lmstudio/sdk");
const zod_1 = require("zod");
const child_process_1 = require("child_process");
const os_1 = require("os");
const config_1 = require("./config");
function json(obj) {
return JSON.stringify(obj, null, 2);
}
function safe_impl(name, fn) {
return async (params, ctx) => {
if (ctx.signal.aborted)
return JSON.stringify({ tool_error: true, tool: name, error: "cancelled" });
try {
return await fn(params, ctx);
}
catch (err) {
const msg = err instanceof Error ? err.message : String(err);
return JSON.stringify({ tool_error: true, tool: name, error: msg }, null, 2);
}
};
}
function requireMac() {
if (process.platform !== "darwin") {
throw new Error("This action is macOS only.");
}
}
function runCmd(cmd, timeoutMs = 5000) {
return (0, child_process_1.execSync)(cmd, { timeout: timeoutMs, encoding: "utf-8" }).trim();
}
function getClipboard(maxChars) {
requireMac();
const content = runCmd("pbpaste");
if (content.length > maxChars) {
return content.slice(0, maxChars) + `\n[truncated — ${content.length} total chars]`;
}
return content;
}
function getActiveWindow() {
requireMac();
const script = `
tell application "System Events"
set frontApp to first application process whose frontmost is true
set appName to name of frontApp
set windowTitle to ""
try
set windowTitle to name of front window of frontApp
end try
return appName & "|||" & windowTitle
end tell
`.trim();
const raw = runCmd(`osascript -e '${script.replace(/'/g, "'\\''")}'`);
const [app, title] = raw.split("|||");
return { active_app: app?.trim() ?? "", window_title: title?.trim() ?? "" };
}
function getRunningApps() {
requireMac();
const script = `tell application "System Events" to return name of every application process whose background only is false`;
const raw = runCmd(`osascript -e '${script}'`);
return raw.split(", ").map(s => s.trim()).filter(Boolean).sort();
}
const toolsProvider = async (ctl) => {
const cfg = ctl.getPluginConfig(config_1.pluginConfigSchematics);
return [
(0, sdk_1.tool)({
name: "context",
description: (0, sdk_1.text) `
Read the user's current system context.
action: "clipboard" — Get current clipboard text content
action: "active_window" — Get the name of the active app and focused window title
action: "running_apps" — List all visible running applications
action: "system" — OS info: platform, CPU, RAM, hostname, username
macOS only for clipboard/active_window/running_apps.
system info works on all platforms.
`,
parameters: {
action: zod_1.z.enum(["clipboard", "active_window", "running_apps", "system"]),
},
implementation: safe_impl("context", async ({ action }, ctx) => {
const maxClipboardChars = cfg.get("maxClipboardChars");
if (action === "clipboard") {
ctx.status("Reading clipboard");
const content = getClipboard(maxClipboardChars);
return json({ chars: content.length, content });
}
if (action === "active_window") {
ctx.status("Getting active window");
return json(getActiveWindow());
}
if (action === "running_apps") {
ctx.status("Listing running apps");
const apps = getRunningApps();
return json({ count: apps.length, apps });
}
// system
ctx.status("Collecting system info");
const totalRam = (0, os_1.totalmem)();
const freeRam = (0, os_1.freemem)();
const cpuModel = (0, os_1.cpus)()[0]?.model ?? "unknown";
const cpuCount = (0, os_1.cpus)().length;
return json({
platform: (0, os_1.platform)(),
os_release: (0, os_1.release)(),
arch: (0, os_1.arch)(),
hostname: (0, os_1.hostname)(),
username: (0, os_1.userInfo)().username,
cpu: `${cpuCount}× ${cpuModel}`,
ram_total_gb: (totalRam / 1024 ** 3).toFixed(1),
ram_free_gb: (freeRam / 1024 ** 3).toFixed(1),
ram_used_pct: Math.round((1 - freeRam / totalRam) * 100),
});
}),
}),
];
};
exports.toolsProvider = toolsProvider;