src / tools / peekFileTool.ts

import { tool, Tool, ToolsProviderController } from "@lmstudio/sdk";
import { z } from "zod";
import * as fs from 'fs/promises';

export const getPeekFileTool = (ctl: ToolsProviderController) => {
  return tool({
    name: "peekFile",
    description: "Read a portion of a file (specify line range).",
    parameters: {
      filepath: z.string(),
      startLine: z.number().min(1).default(1),
      endLine: z.number().min(1).default(10)
    },
    implementation: async ({ filepath, startLine, endLine }) => {
      try {
        const data = await fs.readFile(filepath, 'utf-8');
        const lines = data.split('\n');
        
        // Handle edge cases
        const actualEnd = Math.min(endLine, lines.length);
        const actualStart = Math.min(startLine - 1, lines.length);
        
        const content = lines.slice(actualStart, actualEnd).join('\n');
        
        return content;
      } catch (err) {
        return `Error peeking file: ${err.message}`;
      }
    },
  });
};