import { z } from "zod"; import { withAuth, getSessionCredentials } from "../../auth/index.js"; import { handleToolError, validateRequired, handleApiResponse } from "../helpers/errorHandler.js"; import { AcaiHttpClient } from "../helpers/acaiHttpClient.js"; import { withAuthParams } from "../helpers/authSchema.js"; export function registerSetRecordTranslationsTool(server) { server.tool( "set_record_translations", `Write translations for a single record + language into the central cms_traducciones table. Values are stored Base64 automatically (transparent — pass plain text). Only translatable field types can be translated: textfield, textbox, wysiwyg, codigo and multitext. Four usage scenarios: 1. Normal records — translate the visible text fields of any content table. tableName without 'cms_', recordNum = the record 'num', fields = { fieldName: translatedText }. 2. configuracion / configuracion_tienda — the global settings tables are ordinary records; translate their text fields the same way (locate the row with list_table_records). 3. textos_generales — the template literals used by the Twig '| translate' filter are normal records: translate their 'texto' field. Find the right num by 'identificador' with list_table_records first (tableName='textos_generales', fields={ texto: '...' }). 4. Module vars — the textual values of a module live in the 'builder_custom' table. Call get_module_config_vars to get varsMeta ({ fieldName, recordNum } per var, and per item for multi vars), then set tableName='builder_custom', recordNum + fields={ : translatedText } from varsMeta. Rules: - An EMPTY STRING ('') as a value DELETES that translation (falls back to base language at runtime). - The 'enlace' field is NEVER translated by hand — the CocoEnlace engine maintains it; the PHP rejects it. - multitext fields are translated as the full serialized JSON string in a single row (translate the whole JSON value, not individual items). - prefix is the language code from list_web_languages (e.g. 'en'); do NOT write to prefix 'www' (base language is edited via normal record fields, not translations). Params: - tableName (string) without 'cms_' prefix. - recordNum (number) the record 'num' primary key. - prefix (string) target language prefix (e.g. 'en'). - fields (object) { fieldName: translatedValue } map. '' deletes.`, withAuthParams({ tableName: z.string().describe("Table name without 'cms_' prefix (e.g. 'apartados', 'builder_custom', 'textos_generales')"), recordNum: z.number().describe("Record 'num' primary key to translate"), prefix: z.string().describe("Target language prefix from list_web_languages (e.g. 'en'). Never 'www' (base language)."), fields: z.record(z.string()).describe("Map { fieldName: translatedValue }. Empty string '' deletes the translation. 'enlace' is forbidden."), }), { readOnlyHint: false, destructiveHint: false }, withAuth(async ({ tableName, recordNum, prefix, fields }, extra) => { try { const validationError = validateRequired( { tableName, recordNum, prefix, fields }, ["tableName", "recordNum", "prefix", "fields"], "set_record_translations" ); if (validationError) return validationError; const credentials = await getSessionCredentials(extra.sessionId); const response = await AcaiHttpClient.postViewerAction( credentials, "setTranslations", { tableName, recordNum, prefix, fields }, credentials.token, credentials.tokenHash, {}, 15000 ); const apiError = handleApiResponse(response.data, "set_record_translations"); if (apiError) return apiError; return { content: [{ type: "text", text: JSON.stringify({ success: true, action: "set_record_translations", tableName, recordNum, prefix, updated: response.data?.updated, deleted: response.data?.deleted, }, null, 2) }], }; } catch (error) { return handleToolError(error, "set_record_translations", { tableName, recordNum, prefix }); } }) ); }