import { z } from "zod";
import { createToolFactory, PermissionLevel, ToolCategory, ToolExecutionError, } from "../types.js";
/**
 * Read tool - Reads files from the filesystem with optional offset/limit.
 */
export const readToolFactory = createToolFactory("read", (services) => ({
    description: `
Reads a file from the local filesystem. You can access any file directly by using this tool. Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.
`,
    category: ToolCategory.FILE,
    defaultPermission: PermissionLevel.ALLOW,
    inputSchema: z.object({
        file_path: z.string().describe("Relative path to file"),
        offset: z
            .number()
            .optional()
            .describe("Line number to start reading from"),
        limit: z.number().optional().describe("Number of lines to read"),
    }),
    execute: async ({ file_path, offset, limit }) => {
        try {
            const content = await services.appShell.readFile(file_path, {
                offset,
                limit,
            });
            // Format with line numbers (cat -n style)
            const lines = content.split("\n");
            const lineNumberStart = offset ?? 1;
            const formattedLines = lines.map((line, index) => {
                const lineNumber = lineNumberStart + index;
                return `${lineNumber.toString().padStart(6)}\t${line}`;
            });
            return formattedLines.join("\n");
        }
        catch (error) {
            if (!(error instanceof Error)) {
                throw new Error("Failed to read file: Unknown error");
            }
            throw new ToolExecutionError(`Failed to read file: ${error.message}`, {
                toolName: "",
                input: {},
                context: {},
                severity: "warn",
            });
        }
    },
    getSummary: (input, output) => {
        // Full file read
        if (input.offset === undefined && input.limit === undefined) {
            return { output: "full read" };
        }
        const lineCount = typeof output === "string" ? output.split("\n").length : 0;
        const startLine = input.offset ?? 1;
        const endLine = startLine + lineCount - 1;
        return {
            output: `lines ${startLine}-${endLine}`,
        };
    },
}));
//# sourceMappingURL=read.js.map