61 lines
3.1 KiB
JavaScript
61 lines
3.1 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth } from "../../auth/index.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
import { handleToolError } from "../helpers/errorHandler.js";
|
|
import { pythonPost } from "../helpers/pythonServerClient.js";
|
|
import { getCurrentProjectInfo } from "../files/helpers.js";
|
|
|
|
// Tool: set_layout_field
|
|
// Replaces the content of a global layout field (style or javascript) in the
|
|
// project's layout.json. Destructive — overwrites the existing content. The
|
|
// backend enforces a 500KB size limit and returns 400 if exceeded; that error
|
|
// is propagated via handleToolError.
|
|
|
|
export function registerSetLayoutFieldTool(server) {
|
|
server.tool(
|
|
"set_layout_field",
|
|
`Replace the content of a global layout field: 'style' (CSS), 'javascript' (JS), 'header' (Twig source of the site header), 'footer' (Twig source). CRITICAL: for header/footer, ALWAYS use this tool instead of editing template/estandar/modulos/custom-header-twig/index-base.tpl or custom-footer-twig/index-base.tpl directly with acai-line-replace or acai-write. Editing those .tpl files directly leaves layout.json.{header,footer} out of sync and the visual builder will overwrite your changes on its next save. This tool writes the source, syncs layout.json, regenerates the compiled module files, and runs the TWIG compilation in one atomic pipeline. Destructive: overwrites the full content. Pair with get_layout_field first to read the current source.`,
|
|
withAuthParams({
|
|
field: z.enum(["style", "javascript", "header", "footer"]).describe("Which layout field: 'style', 'javascript', 'header' or 'footer'"),
|
|
content: z.string().describe("Full replacement content. Max 500KB."),
|
|
}),
|
|
{ readOnlyHint: false, destructiveHint: true },
|
|
withAuth(async ({ field, content }, _extra) => {
|
|
try {
|
|
const { projectSlug } = getCurrentProjectInfo();
|
|
const result = await pythonPost("/api/project/layout-field/save", {
|
|
project: projectSlug,
|
|
field,
|
|
content,
|
|
});
|
|
if (!result?.success) {
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: false,
|
|
error: result?.error || "Could not save layout field",
|
|
}),
|
|
}],
|
|
isError: true,
|
|
};
|
|
}
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: JSON.stringify({
|
|
success: true,
|
|
field: result.field || field,
|
|
}, null, 2),
|
|
}],
|
|
};
|
|
} catch (error) {
|
|
return handleToolError(error, "set_layout_field", {
|
|
field,
|
|
contentLength: typeof content === "string" ? content.length : 0,
|
|
});
|
|
}
|
|
})
|
|
);
|
|
}
|