src / toolsProvider.ts

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

// See details about defining tools in the documentation:
// https://lmstudio.ai/docs/typescript/agent/tools

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

  const tools: Tool[] = [];

  const createFileTool = tool({
    // Name of the tool, this will be passed to the model. Aim for concise, descriptive names
    name: "createFile",
    // Your description here, more details will help the model to understand when to use the tool
    description: "Create a file with the given name and content.",
    parameters: { file_name: z.string(), content: z.string().describe("Content of the file") },
    implementation: async ({ file_name, content }) => {
      const filePath = join(ctl.getWorkingDirectory(), file_name);
      if (existsSync(filePath)) {
        return "Error: File already exists.";
      }
      await writeFile(filePath, content, "utf-8");
      return "File created.";
    },
  });
  tools.push(createFileTool);
  
  return tools;
}