79 lines
3.9 KiB
JavaScript
79 lines
3.9 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth, getSessionCredentials } from "../../auth/index.js";
|
|
import { handleToolError, handleApiResponse, validateRequired } from "../helpers/errorHandler.js";
|
|
import { AcaiHttpClient } from "../helpers/acaiHttpClient.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
|
|
export function registerCheckModuleTool(server) {
|
|
server.tool(
|
|
"check_module",
|
|
"Preview how a module renders with sample data. Returns a preview (first 50 lines + summary) by default — use fullRender=true for complete output. Always shows errors in full.",
|
|
withAuthParams({
|
|
moduleName: z.string().describe("Module ID/name to check"),
|
|
vars: z.record(z.string(), z.any()).describe("Object with builder variable values. Keys should match the variable names from data-field-label (without spaces/special chars)"),
|
|
fullRender: z.boolean().optional().describe("If true, returns complete rendered HTML. Default: false (preview — first 50 lines + summary, saves tokens)."),
|
|
}),
|
|
{ readOnlyHint: true, destructiveHint: false },
|
|
withAuth(async ({ moduleName, vars, fullRender }, extra) => {
|
|
const startTime = Date.now();
|
|
console.error(`[Tool] check_module - START: moduleName=${moduleName}, varsCount=${Object.keys(vars || {}).length}, sessionId=${extra.sessionId}`);
|
|
|
|
try {
|
|
// Validate required parameters
|
|
const validationError = validateRequired(
|
|
{ moduleName, vars },
|
|
['moduleName', 'vars'],
|
|
'check_module'
|
|
);
|
|
if (validationError) {
|
|
console.error(`[Tool] check_module - VALIDATION ERROR: ${validationError.content[0].text}`);
|
|
return validationError;
|
|
}
|
|
|
|
const credentials = await getSessionCredentials(extra.sessionId);
|
|
|
|
const payload = {
|
|
moduleName: moduleName,
|
|
vars: vars
|
|
};
|
|
|
|
console.error(`[Tool] check_module - Calling AcaiHttpClient.checkModuleCode...`);
|
|
const response = await AcaiHttpClient.checkModuleCode(credentials, credentials.token, payload);
|
|
|
|
// Check for API errors in response
|
|
/*const apiError = handleApiResponse(response.data, 'check_module');
|
|
if (apiError) {
|
|
console.error(`[Tool] check_module - API ERROR: ${apiError.content[0].text}`);
|
|
return apiError;
|
|
}*/
|
|
|
|
const elapsedTime = Date.now() - startTime;
|
|
console.error(`[Tool] check_module - SUCCESS: completed in ${elapsedTime}ms`);
|
|
|
|
let outputText = `Module Preview for "${moduleName}":\n\n${JSON.stringify(response.data, null, 2)}`;
|
|
|
|
// Preview mode (default): truncate to first 50 lines + summary
|
|
if (!fullRender) {
|
|
const lines = outputText.split('\n');
|
|
const PREVIEW_LINES = 50;
|
|
if (lines.length > PREVIEW_LINES) {
|
|
const preview = lines.slice(0, PREVIEW_LINES).join('\n');
|
|
outputText = `${preview}\n\n--- PREVIEW: showing ${PREVIEW_LINES} of ${lines.length} lines. Use fullRender=true for complete output. ---`;
|
|
}
|
|
}
|
|
|
|
return {
|
|
content: [{
|
|
type: "text",
|
|
text: outputText
|
|
}],
|
|
};
|
|
} catch (error) {
|
|
const elapsedTime = Date.now() - startTime;
|
|
console.error(`[Tool] check_module - ERROR after ${elapsedTime}ms: ${error.message}`);
|
|
return handleToolError(error, 'check_module', { moduleName, varsCount: Object.keys(vars || {}).length });
|
|
}
|
|
})
|
|
);
|
|
}
|