src / toolsProvider.ts
import { tool, type Tool, type ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { configSchematics, WORKING_DIRECTORY_KEY } from "./config";
import { resolveRoot } from "./root";
import { runEdit } from "./tools/edit";
import { runRead } from "./tools/read";
import { runWrite } from "./tools/write";
export function buildTools(root: string): Tool[] {
const read = tool({
name: "read",
description:
"Read a text file from the working directory as numbered lines. offset=start line (1-based), " +
"limit=line count (default 100). The footer gives the next offset.",
parameters: {
path: z.string(),
offset: z.number().int().optional(),
limit: z.number().int().optional(),
},
implementation: args => runRead(root, args),
});
const write = tool({
name: "write",
description:
"Create or overwrite a file in the working directory with the given content. Creates parent directories.",
parameters: { path: z.string(), content: z.string() },
implementation: args => runWrite(root, args),
});
const edit = tool({
name: "edit",
description:
"Replace exact text in a file. old_string must match exactly once, or set replace_all=true.",
parameters: {
path: z.string(),
old_string: z.string(),
new_string: z.string(),
replace_all: z.boolean().optional(),
},
implementation: args => runEdit(root, args),
});
return [read, write, edit];
}
export async function toolsProvider(ctl: ToolsProviderController): Promise<Tool[]> {
const configured = ctl.getGlobalPluginConfig(configSchematics).get(WORKING_DIRECTORY_KEY);
const root = await resolveRoot(configured, () => ctl.getWorkingDirectory());
return buildTools(root);
}
src / toolsProvider.ts
import { tool, type Tool, type ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import { configSchematics, WORKING_DIRECTORY_KEY } from "./config";
import { resolveRoot } from "./root";
import { runEdit } from "./tools/edit";
import { runRead } from "./tools/read";
import { runWrite } from "./tools/write";
export function buildTools(root: string): Tool[] {
const read = tool({
name: "read",
description:
"Read a text file from the working directory as numbered lines. offset=start line (1-based), " +
"limit=line count (default 100). The footer gives the next offset.",
parameters: {
path: z.string(),
offset: z.number().int().optional(),
limit: z.number().int().optional(),
},
implementation: args => runRead(root, args),
});
const write = tool({
name: "write",
description:
"Create or overwrite a file in the working directory with the given content. Creates parent directories.",
parameters: { path: z.string(), content: z.string() },
implementation: args => runWrite(root, args),
});
const edit = tool({
name: "edit",
description:
"Replace exact text in a file. old_string must match exactly once, or set replace_all=true.",
parameters: {
path: z.string(),
old_string: z.string(),
new_string: z.string(),
replace_all: z.boolean().optional(),
},
implementation: args => runEdit(root, args),
});
return [read, write, edit];
}
export async function toolsProvider(ctl: ToolsProviderController): Promise<Tool[]> {
const configured = ctl.getGlobalPluginConfig(configSchematics).get(WORKING_DIRECTORY_KEY);
const root = await resolveRoot(configured, () => ctl.getWorkingDirectory());
return buildTools(root);
}