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 registerRemoveModuleFromRecordTool(server) { server.tool( "remove_module_from_record", `Removes a builder module from a record's builder array. Identify the module to remove by either: - sectionId (unique, preferred) — get it from the builder JSON of the record - modulePosition (0-based index) — position in the builder array Required: tableName + recordNum + (sectionId OR modulePosition)`, 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 to remove (preferred)"), modulePosition: z.number().optional().describe("Position in builder array (0-based). Use if sectionId not available."), }), { readOnlyHint: false, destructiveHint: true }, withAuth(async ({ tableName, recordNum, sectionId, modulePosition }, extra) => { try { const validationError = validateRequired( { tableName, recordNum }, ['tableName', 'recordNum'], 'remove_module_from_record' ); 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; const response = await AcaiHttpClient.postViewerAction( credentials, "removeModuleFromRecord", payload, credentials.token, credentials.tokenHash, {}, 15000 ); const apiError = handleApiResponse(response.data, 'remove_module_from_record'); if (apiError) return apiError; return { content: [{ type: "text", text: JSON.stringify({ success: true, action: 'remove_module_from_record', tableName, recordNum, ...response.data, }, null, 2) }], }; } catch (error) { return handleToolError(error, 'remove_module_from_record', { tableName, recordNum, sectionId, modulePosition }); } }) ); }