97 lines
4.8 KiB
JavaScript
97 lines
4.8 KiB
JavaScript
import { z } from "zod";
|
|
import { withAuth } from "../../auth/index.js";
|
|
import { withAuthParams } from "../helpers/authSchema.js";
|
|
import { handleToolError } from "../helpers/errorHandler.js";
|
|
import { callSchemaEndpoint } from "./_schemaEndpoint.js";
|
|
|
|
// Tool: create_field
|
|
// Crea un nuevo campo en una tabla existente. Backend aplica los defaults
|
|
// segun `type` y permite overrides via `initialProps`.
|
|
|
|
const FIELD_TYPES = [
|
|
"textfield", "textbox", "wysiwyg", "date", "list",
|
|
"checkbox", "upload", "multitext", "codigo", "separator",
|
|
];
|
|
|
|
export function registerCreateFieldTool(server) {
|
|
server.tool(
|
|
"create_field",
|
|
`Add a new field to an existing table.
|
|
|
|
Field types:
|
|
- textfield: single-line text
|
|
- textbox: multi-line plain text
|
|
- wysiwyg: rich text editor
|
|
- codigo: code editor (HTML/JS/CSS snippet)
|
|
- date: date/datetime picker
|
|
- list: select/radio/checkboxes (needs listType + optionsType in initialProps)
|
|
- checkbox: boolean
|
|
- upload: file upload (images/docs)
|
|
- multitext: repeater of text entries
|
|
- separator: visual separator in the form (no data column)
|
|
|
|
'initialProps' is optional; use it to override defaults (e.g. {isRequired:1, maxLength:100}).
|
|
Table names WITHOUT 'cms_' prefix. Primary key is always 'num'.
|
|
|
|
MULTITEXT FIELDS (type='multitext') — initialProps shape:
|
|
A multitext field is a repeater of N sub-fields per row. Each row in the
|
|
admin form gives the user a set of sub-fields to fill (e.g. a list of FAQ
|
|
items where each one has 'question' + 'answer' + 'category').
|
|
- tablaAuxiliar (string, opcional): nombre de tabla auxiliar para el repeater.
|
|
- descriptionjson (REQUIRED): JSON STRING (not object) with the array of
|
|
sub-fields. Each sub-field is an object with:
|
|
- id_campo (string): clave tecnica, slug. Estable: NO se cambia despues.
|
|
- nombre_campo (string): etiqueta visible en la UI.
|
|
- tipo (string '0'..'5'): '0'=texto, '1'=tabla CMS, '3'=fecha, '4'=color, '5'=icono.
|
|
- When tipo='1': also pass tabla (target table without 'cms_'),
|
|
campo_valor (default 'num'), campo_muestra (label field).
|
|
Example (correct):
|
|
"descriptionjson": "[{\\"id_campo\\":\\"pregunta\\",\\"nombre_campo\\":\\"Pregunta\\",\\"tipo\\":\\"0\\"},{\\"id_campo\\":\\"respuesta\\",\\"nombre_campo\\":\\"Respuesta\\",\\"tipo\\":\\"0\\"}]"
|
|
⚠ It MUST be a JSON-encoded string. If you pass an object the backend
|
|
rejects it. Strings, single-quoted JSON, or other formats break the editor.
|
|
|
|
LIST FIELDS (type='list') — initialProps shape:
|
|
- listType (REQUIRED): one of 'pulldown' | 'radios' | 'pulldownMulti' | 'checkboxes'.
|
|
NOT 'select' nor 'dropdown' — use 'pulldown'.
|
|
- optionsType (REQUIRED): one of 'text' | 'table' | 'query'.
|
|
|
|
- When optionsType='text': pass 'optionsText' as a SINGLE string with one
|
|
option per line, separated by REAL NEWLINE CHARACTERS ('\\n' in JSON).
|
|
Each line is either 'value|Label' (preferred) or just 'label' (value=label).
|
|
⚠ Do NOT separate options with commas. Commas inside an option are valid
|
|
data — Acai uses '\\n' as the option delimiter, period.
|
|
Example (correct):
|
|
"optionsText": "indefinido|Indefinido\\ntemporal|Temporal\\nfreelance|Freelance"
|
|
Example (WRONG, will store all 4 as a single option):
|
|
"optionsText": "Indefinido,Temporal,Prácticas,Freelance"
|
|
|
|
- When optionsType='table': pass 'optionsTablename' (target table without cms_),
|
|
'optionsValueField' (default 'num'), 'optionsLabelField'. Optional 'filterField'
|
|
is a SQL WHERE clause without the WHERE keyword (e.g. "visible=1").
|
|
|
|
- When optionsType='query': pass 'optionsQuery' as raw SQL. Acai uses POSITIONAL
|
|
columns: column 0 is the value, column 1 is the label. So write
|
|
"SELECT num, titulo FROM cms_xxx WHERE active=1" — the 'AS value/label'
|
|
aliases have NO effect.`,
|
|
withAuthParams({
|
|
tableName: z.string().describe("Table name without 'cms_' prefix"),
|
|
fieldName: z.string().describe("New field name (SQL-safe identifier)"),
|
|
label: z.string().describe("Human-readable label shown in the admin form"),
|
|
type: z.enum(FIELD_TYPES).describe("Field type"),
|
|
initialProps: z.object({}).passthrough().optional().describe("Optional overrides for the default field config"),
|
|
}),
|
|
{ readOnlyHint: false, destructiveHint: false },
|
|
withAuth(async ({ tableName, fieldName, label, type, initialProps }, _extra) => {
|
|
try {
|
|
const body = { tableName, fieldName, label, type };
|
|
if (initialProps && typeof initialProps === "object") body.initialProps = initialProps;
|
|
|
|
const { mcp } = await callSchemaEndpoint("/api/schema/create-field", body);
|
|
return mcp;
|
|
} catch (error) {
|
|
return handleToolError(error, "create_field", { tableName, fieldName, type });
|
|
}
|
|
})
|
|
);
|
|
}
|