import { z } from "zod"; import { handleToolError, validateRequired } from "../helpers/errorHandler.js"; import { getCurrentProjectInfo, callLocalFileEndpoint, buildLocalFileErrorResponse } from "./helpers.js"; export function registerAcaiWriteTool(server) { server.tool( "acai-write", `Write a full file inside the project. Use for new files or full rewrites. Prefer acai-line-replace for targeted edits. Before writing, check the matching documentation for the file type: - If the file is an index-base template (\`index-base.tpl\` or \`index-base.html\`), make sure you have read \`docs/module-creation-guide.md\` and \`docs/builder-fields.md\` - If the file is a \`.js\` or \`.css\`, make sure you have read \`docs/css-js-conventions.md\` - If the file is a module or global hook PHP file, make sure you have read \`docs/hooks-and-api.md\``, { file_path: z.string().describe("Path relative to the project root"), content: z.string().describe("Full file content to write"), mode: z.enum(["create_or_overwrite", "create_only"]).optional().default("create_or_overwrite").describe("Write mode"), expected_sha256: z.string().optional().describe("Optional optimistic concurrency check from a prior acai-view"), }, { readOnlyHint: false, destructiveHint: false }, async ({ file_path, content, mode = "create_or_overwrite", expected_sha256 }) => { try { const validationError = validateRequired({ file_path }, ["file_path"], "acai-write"); if (validationError) return validationError; const { projectSlug, projectDir } = getCurrentProjectInfo(); const result = await callLocalFileEndpoint("POST", "/api/files/write", { project: projectSlug, projectDir: projectDir, relativePath: file_path, content, mode, expectedSha256: expected_sha256 || "", }); if (!result.data?.success) { return buildLocalFileErrorResponse("acai-write", result, { file_path }); } const data = result.data; return { content: [{ type: "text", text: JSON.stringify({ success: true, file_path: data.filePath, created: data.created, overwritten: data.overwritten, sha256: data.sha256, compiled: data.compiled || false, compile_result: data.compileResult || null, }, null, 2), }], }; } catch (error) { return handleToolError(error, "acai-write", { file_path }); } } ); }