Files
agenticSystem/mcp-server/tools/languages/setRecordTranslations.js
Jordan Diaz 9c3d9fb999 fix(mcp): las traducciones de vars de modulo van por NOMBRE de var, no columna fisica
Una sesion real del agente guardo title3/title6/title2 (columnas de
builder_custom segun varsMeta.fieldName, como decian las docs) y el front
no pintaba nada: el runtime traduce vars con t($record, $var) por nombre
de var. Corregidas descripciones de set/get_record_translations, docs
03/09/11b y ACAI_ENDPOINTS; ademas el puente PHP ahora rechaza titleN/
textN sobre builder_custom con error explicativo (guardrail).
2026-07-17 08:18:18 +00:00

80 lines
4.9 KiB
JavaScript

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 and use its recordNum, BUT the field key MUST be the VAR NAME from builder.json (e.g. 'titulo', 'subtitulo', 'enlace_anchor') — NEVER the physical column (title3, text1...): the engine translates module vars by var name and physical columns are rejected. Example: set tableName='builder_custom', recordNum=<varsMeta recordNum>, fields={ titulo: 'Translated title' }.
Rules:
- An EMPTY STRING ('') as a value DELETES that translation (falls back to base language at runtime).
- 'enlace' IS translatable: absolute path with the language prefix (e.g. '/en/contact/'). Note: when the base 'enlace' changes, CocoEnlace regenerates per-language rows and may overwrite 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' must be an absolute path (e.g. '/en/contact/')."),
}),
{ 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 });
}
})
);
}