import fs from 'node:fs/promises';
import path from 'node:path';
import { z } from 'zod';
import { resolveInCwd } from './types.js';
const schema = z.object({
    path: z.string().min(1),
    contents: z.string(),
});
export const writeTool = {
    name: 'write_file',
    description: 'Write contents to a file, creating it (and parent directories) if needed, ' +
        'or overwriting it entirely if it exists. For partial changes to an existing file, prefer edit_file.',
    schema,
    jsonSchema: {
        type: 'object',
        properties: {
            path: { type: 'string', description: 'File path (absolute or relative to cwd)' },
            contents: { type: 'string', description: 'Full file contents to write' },
        },
        required: ['path', 'contents'],
    },
    readOnly: false,
    summarize: (i) => `${i.path} (${i.contents.length} chars)`,
    run: async (input, ctx) => {
        const file = resolveInCwd(ctx.cwd, input.path);
        await fs.mkdir(path.dirname(file), { recursive: true });
        await fs.writeFile(file, input.contents, 'utf8');
        const lineCount = input.contents.split('\n').length;
        return `Wrote ${lineCount} lines to ${input.path}`;
    },
};
