import { z } from "zod"; import { withAuth, getSessionCredentials, getApiClient } 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 registerCheckModuleUsageTool(server) { server.tool( "check_module_usage", "Check which pages/URLs use a module. Call BEFORE delete_module to verify it's safe to remove.", withAuthParams({ id: z.string().describe("Module ID to check usage for"), }), { readOnlyHint: true, destructiveHint: false }, withAuth(async ({ id }, extra) => { try { // Validate required parameters const validationError = validateRequired({ id }, ['id'], 'check_module_usage'); if (validationError) return validationError; const credentials = await getSessionCredentials(extra.sessionId); // Build the request payload const payload = { action_ws: "checkModuleInWeb", module: id, token: credentials.token, tokenHash: credentials.tokenHash }; // Make the request to the client's website const response = await AcaiHttpClient.postViewerFunctions( await getApiClient(extra.sessionId), payload ); // Check for API errors in response const apiError = handleApiResponse(response.data, 'check_module_usage'); if (apiError) return apiError; // Extract usage information const usageData = response.data.data || response.data; return { content: [{ type: "text", text: JSON.stringify({ success: true, moduleId: id, usage: usageData, canDelete: !usageData || Object.keys(usageData).length === 0, message: Object.keys(usageData || {}).length === 0 ? "Module is not used anywhere - safe to delete" : `Module is used in ${Object.keys(usageData || {}).length} location(s)` }, null, 2) }], }; } catch (error) { return handleToolError(error, 'check_module_usage', { id }); } }) ); }