import { z } from "zod"; import { withAuth, getSessionCredentials } from "../../auth/index.js"; import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js"; import { withAuthParams } from "../helpers/authSchema.js"; import { AcaiHttpClient } from "../helpers/acaiHttpClient.js"; export function registerToggleModuleVisibilityTool(server) { server.tool( "toggle_module_visibility", `Show or hide a module on a page without removing it. Identify the module by sectionId (preferred) or modulePosition. Optionally set visible=true/false explicitly, or omit to toggle. Table names WITHOUT 'cms_' prefix. The recordNum is the 'num' primary key.`, withAuthParams({ tableName: z.string().describe("Table name without cms_ prefix (e.g. 'apartados')"), recordNum: z.union([z.string(), z.number()]).describe("Record num (primary key)"), sectionId: z.string().optional().describe("section_id of the module (preferred)"), modulePosition: z.number().optional().describe("Position in builder array (0-based)"), visible: z.boolean().optional().describe("Set explicitly: true=show, false=hide. Omit to toggle."), }), { readOnlyHint: false, destructiveHint: false }, withAuth(async ({ tableName, recordNum, sectionId, modulePosition, visible }, extra) => { try { const validationError = validateRequired( { tableName, recordNum }, ['tableName', 'recordNum'], 'toggle_module_visibility' ); if (validationError) return validationError; if (!sectionId && modulePosition === undefined) { return { content: [{ type: "text", text: "Error: sectionId or modulePosition is required" }], isError: true, }; } const credentials = await getSessionCredentials(extra.sessionId); const payload = { tableName, recordNum, }; if (sectionId) payload.sectionId = sectionId; if (modulePosition !== undefined) payload.modulePosition = modulePosition; if (visible !== undefined) payload.visible = visible; const response = await AcaiHttpClient.postViewerAction( credentials, "toggleModuleVisibility", payload, credentials.token, credentials.tokenHash, {}, 15000 ); const apiError = handleApiResponse(response.data, 'toggle_module_visibility'); if (apiError) return apiError; return { content: [{ type: "text", text: JSON.stringify({ success: true, action: 'toggle_module_visibility', ...response.data, }, null, 2) }], }; } catch (error) { return handleToolError(error, 'toggle_module_visibility', { tableName, recordNum, sectionId, modulePosition }); } }) ); }