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 registerReorderModuleTool(server) { server.tool( "reorder_module", `Move a module from one position to another in a record's builder array. Use list_page_modules first to see current positions. 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)"), fromPosition: z.number().describe("Current position of the module (0-based)"), toPosition: z.number().describe("Target position to move the module to (0-based)"), }), { readOnlyHint: false, destructiveHint: false }, withAuth(async ({ tableName, recordNum, fromPosition, toPosition }, extra) => { try { const validationError = validateRequired( { tableName, recordNum }, ['tableName', 'recordNum'], 'reorder_module' ); if (validationError) return validationError; const credentials = await getSessionCredentials(extra.sessionId); const response = await AcaiHttpClient.postViewerAction( credentials, "reorderModule", { tableName, recordNum, fromPosition, toPosition, }, credentials.token, credentials.tokenHash, {}, 15000 ); const apiError = handleApiResponse(response.data, 'reorder_module'); if (apiError) return apiError; return { content: [{ type: "text", text: JSON.stringify({ success: true, action: 'reorder_module', ...response.data, }, null, 2) }], }; } catch (error) { return handleToolError(error, 'reorder_module', { tableName, recordNum, fromPosition, toPosition }); } }) ); }