src / toolsProvider.ts

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { configSchematics } from "./configSchematics";
import { existsSync } from "fs";
import { writeFile } from "fs/promises";

export async function toolsProvider(ctl: ToolsProviderController) {
  const config = ctl.getPluginConfig(configSchematics);

  const tools: Tool[] = [];

  const createFileTool = tool({
    name: "createFile",
    description: "Create a file with the given name and content.",
    parameters: { name: z.string(), content: z.string() },
    implementation: async ({ name, content }) => {
      if (existsSync(name)) {
        return "Error: File already exists.";
      }
      await writeFile(name, content, "utf-8");
      return "File created.";
    },
  });

  const pwdTool = tool({
    name: "pwd",
    description: "Get the current working directory.",
    parameters: {},
    implementation: async () => {
      return process.cwd();
    },
  });

  tools.push(createFileTool);
  tools.push(pwdTool);

  const printAllToolsTool = tool({
    name: "printAllTools",
    description: "Print all tools.",
    parameters: {},
    implementation: async () => {
      return tools.map((tool) => tool.name).join(", ");
    },
  });

  tools.push(printAllToolsTool);

  return tools;
}