src / tools / createFile.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 const getCreateFileTool = (ctl: ToolsProviderController) => {
  return tool({
    name: "createFile",
    description: "Create a file with the given name and content.",
    parameters: { name: z.string(), content: z.string() },
    implementation: async ({ name, content }) => {
      const config = ctl.getPluginConfig(configSchematics);
      if (existsSync(name)) {
        return "Error: File already exists.";
      }
      await writeFile(name, content, "utf-8");
      return "File created.";
    },
  });
};