Transformers — /transformers

A Transformer converts the payload between the sender’s format and the receiver’s: JSON ↔ XML, renaming fields, flattening structures, computing derived values, splitting one message into several.
The same Transformer is reusable in Delivery Definitions, in Collections and in the forwardings of a Collection to several interfaces.
Where the Transformer sits in the flow
- Delivery — between receipt and sending to the destination.
- Collection — between reading the source and forwarding to the target interfaces.
- Forwarding — a Transformer of its own per destination, when the same collection feeds systems that expect different formats.
- Producer Application — on a Delivery that receives from several systems, a Transformer of its own per producer, which converts that producer’s format into the Delivery’s and runs before the Delivery Transformer. See Transformer by producer Application.
Before changing a Transformer, check the consolidated usage shown in the listing. It covers direct use in Definitions and indirect use — via Collection forwardings and by producer Application on a Delivery. Both must be considered, and the second one is easy to forget.
When the Transformer fails
An exception, a timeout or an empty result ({}, [], null, empty text) is a Transformer
error: nothing is sent to the destination, and the message waits to be transformed by the
current version of the script on the next attempt. Once attempts are exhausted, the Interface
blocks until someone reprocesses or cancels the message. Fixing the Transformer and reprocessing is
the way out. See Transformer error.
The listing
The screen opens on active Transformers, grouped like the other registration screens: by connection category (Applications, Industrial Protocols, Databases, SAP…) and, inside it, by Application. Each Application group brings together the locally scoped Transformers that some Delivery, Collection or other link of that Application uses — one used by two Applications shows up in both. At the end, under Shared:
- Global — every Transformer with Global Scope, the library any Definition can use, whether it is in use or not;
- Unused — the locally scoped ones no Definition uses today, cleanup candidates.
Each Application header is the same as on the other screens: the description, the error badge, the group total in purple — the Transformer colour — with the active and inactive ones as subtle text (6 transformer(s) · 5 active · 1 inactive), and, at the right edge, the connection type with its status and the integration map. The error badge is No errors in neutral grey, or N with errors in red — see Messages with errors. The search opens all groups.
| Filter | What it does |
|---|---|
| Status (Active, Inactive, All) | Opens on Active; a deactivated Transformer does not disappear, it just leaves the default view |
| Usage (Any usage, Unused, With errors) | Unused shows the cleanup candidates; With errors, the ones that have a message stuck in Transformer error |
Integrate two Definitions (wand) and Import (file arrow) are icons on the filter bar, with the name in the tooltip. New Transformer remains a button.
| Column | Content |
|---|---|
| Transformer | Name in bold, with the description below |
| Scope | Local (only shown to the Scope’s Definitions) or Global (any Definition can use it). The tooltip explains each one |
| In use | One icon per link type, with the count — see below |
| Runs 24h | How many times the Transformer ran in the last 24 hours on real messages (the screen’s Test does not count), with the failed ones next to it; the tooltip shows the average time. The engine saves the count every ~30 s, and history starts with the version that added the column. In the Definitions using popup, the same column shows the runs of each usage — who actually uses the Transformer and who only has it linked |
| Errors | Messages stuck in Transformer error because of this Transformer; a grey check when there are none. The number opens the errors popup |
| Updated | Current version, date and author of the last change. Who created it and when shows in the tooltip |
| Active | Turns it on and off, with confirmation |
Messages with errors
The group’s N with errors badge, and the number in the Errors column, open a popup with the messages stuck in Transformer error: in an Application group, that Application’s; in Global and Unused, those of the group’s Transformers; in the column, that Transformer’s. Every stuck message counts, from any date — it stays in that status until someone reprocesses or cancels it, and an old one blocks the Interface just as much as one from today.
Each row shows when the message arrived, how many attempts it made, Application · Interface · Definition, the Transformer and the reason for the last failure. The way to fix it is in the popup itself:
Adjust with the message
The wrench opens the Transformer editor in another tab, with the payload of that message already in the Input Sample Payload and the test already run — the error shows in the Result. Fix the script, test and save.
Reprocess
Back in the popup, Reprocess (on the row), Reprocess selected or Reprocess all ask for the reason, as in Delivery Errors, and follow the result. The message runs with the current version of the Transformer.
The eye opens the full message in another tab. Reprocessing requires the permission to reprocess Messages errors; without it, the popup only shows. The popup brings the 200 most recent — the rest are in Messages.
Who uses this Transformer
The In use column summarizes the links by type, each one with its icon and the quantity:
| Icon | Link | Means |
|---|---|---|
| Blue inbox | Delivery | The Delivery itself transforms with this Transformer |
| Teal download | Collection | The Collection itself transforms with this Transformer |
| Amber arrow | Forwarding | Only one destination of that Collection uses it; the Collection may use another, or none |
| Purple send | By producer | Only the messages of one producer Application go through it, before the Delivery Transformer |
| Indigo fork | By router | A Router route transforms before delivering |
| Lightning bolt | Trigger | A Trigger with a UNS Snapshot transforms the JSON before sending |
| Grey link | Reference | The Definition is in this Transformer’s Scope: it may come to use it, but transforms nothing with it today |
Clicking the summary opens the list of uses, and you pick what to open from there. View details and filter leads to the full modal, with a search field of its own — a shared Transformer accumulates dozens of rows.
The distinction matters when changing something: touching a forwarding Transformer affects one branch of the Collection, not the whole Collection. A Definition that is in the Scope and using it counts once, as real usage. See Scope is not usage.
The usage count is not cut by your Interfaces; the identity of who uses it is. A Transformer used only by another area showed “None” and looked free to delete — today it shows the total, and the difference appears as “N in Interfaces you cannot access”. Knowing something is in use does not reveal by whom: no Application code, no Definition code, no id.
Deleting a Transformer in use
Deleting a Transformer that a Delivery, a Collection, a producer Application, a Router route or a Trigger still uses is refused, with a message saying how many uses remain — remove it from there first. Before, the same situation reached the screen as a generic server error.
A forwarding does not block the deletion: the destination that used the deleted Transformer falls back to the destination Delivery’s Transformer, as it already did.
See what the Transformer does
Whoever picks a Transformer in a combo rarely remembers the format it returns. Next to every Transformer combo — on the Delivery, on the Forward Collection rows and on the Forward Response rows — there is an eye icon (See what this Transformer does). With no Transformer chosen, it is dimmed. The popup shows, without leaving the form:
| Part | What it brings |
|---|---|
| Header | The name, the status (active or inactive), the output Content-Type and the Generated by AI tag, when it applies. The Open in editor icon opens the Transformer in a new tab |
| What it does | The description from the record |
| Input model / Output model | The examples stored in the editor, with indented JSON. With no model, it says Not set in the editor. |
| Real example | The Run on the last message button runs the script on the last real message that went through this Transformer and shows side by side the Input (last message) and the Script output, with the code of the Definition the message came from |
| View script | Opens the script, read-only |
Run on the last message uses the same sample as the editor’s Testing with the latest message and saves nothing: not the message, not the model, not the Transformer. It is the quickest way to check, before saving the Collection or the Delivery, whether the output suits the destination. As in the editor, Test Bench messages do not count and only the Interfaces you can see count; with none, the popup says no message has gone through it yet.
The icon only shows for whoever has access to the Transformers screen.
Writing by hand — /transformers/:id
A Monaco editor (the same as VS Code), with an immediate test against a sample payload. Use new
as :id to create one from scratch. To just look up what a Transformer does, without leaving the Collection or
the Delivery, use the eye next to the combo.
At the top, on a single line, sit the name, the description, the version and the scope, and the Back and Save buttons. On a narrow screen, version, scope and buttons move down to the next line together.
The script is JavaScript. Besides the payload, it receives a context:
| Field | Content |
|---|---|
topico | Source topic, in MQTT collections |
uid | Message identifier |
dataRecebimento | Time of receipt |
vars | Available variables: the Global ones plus those of the Application in context |
An Application variable overrides a Global one with the same name. That is what lets you write a
single Transformer using vars.CENTRO and install it across several plants — see
Variables.
To read the variable of a specific Application, regardless of the message’s Application, use
app.CODE.NAME — app.MES.LINHA, for example. The editor’s placeholder picker has one section per
Application. On save, the variable must exist and the Application must be within your reach (see
Variable from another Application).
The Result in full screen
The 3. Result column is narrow for a large result. The full-screen icon, next to the copy one, opens the result in a popup with:
- Original / Formatted — JSON or XML indented for reading. A result that is not valid JSON or XML stays in Original only.
- Copy original or Copy formatted — copies what is on screen, and the label says which.
Formatting is for reading only: Transformation OK and the Expected Output use the original result, exactly as the script produced it.
Testing with the latest message
On a Transformer already saved, Test with the latest message, next to Test, fetches the most recent message that went through it — directly, or through the Deliveries and Collections that use it —, puts the payload in the Input Sample Payload and runs the script with it. Above the result appears where the sample came from (Message #id from date · Definition), with a link to the message.
- It is the payload as the Transformer receives it: on a Delivery, what was received; on a Collection, what was collected; on a forwarding, what the source returned; by producer Application, what the producer sent; on a message stopped in Transformer error, what was waiting for the transformation.
- Test messages from the Test Bench are left out.
- An encrypted payload is not decrypted here — decrypting asks for a password and goes to the audit log. The Result warns about it and the link takes you to the message, where you can decrypt it.
- It only appears for those with access to Messages, and only considers messages from the Interfaces you can see. With none, the Result says no message has gone through it yet.
- The Input Sample Payload keeps the message’s payload; save if you want to keep it as an example.
- From the listing’s errors popup, Adjust with the message opens the editor the same way, but with the message that failed instead of the most recent one — see Messages with errors.
Converting XML
Three functions are available in the script, with no import:
| Function | What it does |
|---|---|
xmlToJson(xml) | Converts XML into an object, turning what looks like a number into a number |
xmlToJsonRaw(xml) | Converts XML into an object with no type coercion: every text stays a string |
jsonToXml(obj) | Builds the XML back from the object |
Use xmlToJsonRaw whenever the XML carries an identifier. xmlToJson eats the leading zero:
the production order <ID>000100234567</ID> reaches the script as 100234567 — a number that does
not exist in the ERP. The same goes for material code, batch, plant and postcode, and the damage is
silent, because the script receives a plausible number.
Both functions drop the namespace prefix and keep attributes. xmlToJson still exists and is still
the default on purpose: every Transformer already written was written against its behaviour, and a
script comparing if (x.Qtd > 10) would start comparing text if the conversion changed underneath —
the break would show up on the first message, in production.
The name opens locked
When editing, the Transformer field comes with a padlock. The name is not just a label: it is the natural key for import and export (packs, export files, MCP tools) and it is how the team recognises the script in the Definitions using it. Renaming makes an older pack create a new Transformer instead of updating this one.
Clicking the padlock brings up the impact warning; only after confirming does the field unlock. There is no padlock when creating — nothing points at the name yet. The change is recorded in the audit log, under its own action Change Transformer Name.
Scope is not usage
The target button, next to the scope tag, opens the list of Application / Interface / Definition this Transformer reaches. That list is a reference, not a link: the actual link is created by the Definition itself, on its own screen.
To leave no doubt, every row in the popup carries an Actual use column:
| Tag | Means |
|---|---|
| In use | The Definition — or one of its forwards, or a producer Application on it — points at this Transformer right now |
| Reference only | Nothing points at it yet; the Definition is listed only as a candidate |
An In use reference cannot be removed from here: undo the link on the Definition screen first — on those rows the bin icon is dimmed, with the reason in the tooltip.
Removing takes two steps, just like adding. The bin marks the reference to leave — the row is highlighted and the icon turns into an undo —, and nothing is removed until you click Save. Closing the window discards the marks, both the removals and any add rows you filled in.
Below the references, the Forwardings section lists the Collections and Deliveries that use this Transformer in the forwarding to another destination — the Forward Collection of a Collection or the Forward Response of a Delivery. Each row shows the Application, the Interface, the origin Collection or Delivery and, after the arrow, the destination; a forwarding that is switched off shows faded. The section is read-only and only shows when there is one: the forwarding link is made in the origin’s Forward, not in the Scope. It exists so the popup answers “who uses this” in full, since a forwarding uses the Transformer without the destination Definition being in the Scope.
If the marks would empty the scope, the screen says so first: with no references left the Transformer becomes Global again — it shows up for everyone and can be picked in any Delivery or Collection Definition. That is the only real effect of removing a reference.
Version history
Every saved change to the script, the payload models or the name creates a version. In the history:
- the newest is the Current one — the snapshot of what is saved today;
- the rest are Obsolete: kept for review and for restoring, but not what runs.
Restore this version asks for confirmation and only changes what is on screen — the script and the models become those of the chosen version, but nothing is written until you click Save. To back out, leave the screen without saving.
An obsolete version can be removed, also with confirmation; removal is permanent and goes to the audit log. The Current version never leaves — without it the history would lose the reference to what is live.
From the payload comparison
In the Collection form, the comparison with the target Delivery offers Create Transformer for this destination when what the Collection delivers does not fit the Delivery. The button opens this screen in a new tab, already in the new Transformer popup, with:
| Field | Comes filled with |
|---|---|
| Name | COLLECTION CODE → DELIVERY CODE. You can change it |
| Scope | The Application, the Interface and the target Delivery |
| Input model | The Collection’s latest real message, when there is one |
| Output model | A JSON with the fields the Delivery expects, without values |
| Description | The mandatory fields the script has to build |
Once the record is confirmed, the editor opens with both models ready for Test: what is left is writing the script and saving. The scope is only a reference: for it to take effect, pick the Transformer in the Transformer column of the Forward Collection row. The combo has Refresh list at the bottom, which brings in the newly created Transformer without closing the Collection form.
Moving a Transformer to another environment
A Transformer written and tested in staging can go to production — or to another customer’s installation — as a file. There is no need to go through Import/Export in the Danger Zone, which moves whole domains at once.
Exporting
In the listing, the Export to file icon on the Transformer’s row downloads a .json with the
record, the script, the input and output payload models and the list of Scope Definitions.
Deliberately left out: the version history (the destination starts counting from scratch) and the authorship from the source (whoever imports becomes the author). If you only see part of the Scope, the file carries only that part.
The Scope is stored by short names — Application · Interface · Definition — never by the record’s internal number. Those numbers are sequential and local: the same id points at something else in the destination, and a file carrying them would tie the script to the wrong Definition without complaining about anything.
Importing
The Import icon (file arrow), on the filter bar, asks for the file and shows what will happen before writing anything.
For each Scope Definition, the CMS looks for the same trio of short names here and reports what it found:
| Situation | What the screen shows |
|---|---|
| The trio exists in this environment | exists here |
| The Application does not exist here | Application does not exist here |
| The Application exists, the Interface does not | Interface does not exist here |
| The Interface exists, the Definition does not | Definition does not exist here |
| The Definition exists, but belongs to an Interface you cannot access | exists, but belongs to an Interface you cannot access |
And for each row you choose:
- Use this one — only shown when the trio matched; reuses the Definition that was found;
- Choose another — opens the Application → Interface → Definition combos of this environment, so you can point that Scope by hand;
- Skip — discards the row.
Skipping every row (or importing a Transformer that was already global) brings the Transformer in as Global: it becomes available to every Definition, and the link can be made later, on each Definition’s screen.
The import never overwrites an existing Transformer. If one with the same name already exists here, the imported one comes in as “name (importado)” — the screen says so before writing. It is up to you to compare the two and delete the old one, if that is the case.
Choose another depends on the /applications and /interfaces Tools: without them the combos
would be empty, and the screen shows the option disabled with the reason. The chosen Definition is
also re-checked on the server against your Interfaces — you cannot tie the script to an area you
cannot reach.
Every import is recorded in the audit log, under the Import Transformer action, with the name that came in the file, the name it was created with and how many Scope links were born.
Generating with AI
Someone who does not write code can generate the script by describing the result. It is one of the ways the CMS uses AI — see Configuration assistants for the full set.
Provide the examples
An input payload (X) and an output one (Y) — as a real example or as a JSON Schema.
Describe the transformation
A free-text prompt explaining the rules (“sum the quantities per batch”, “convert the date to ISO
8601”, “when shift is missing, use 1”).
Review the result
The backend generates the script and runs it in the same sandboxed worker pool used in production, against the sample payload. You see the compared result (X → obtained Y), not the code — unless you open the code icon, which also shows the full history of generations and prompts.
Adjust or approve
If the result is not right, adjust the prompt and generate again — every attempt stays in the history and can be restored. Only when you click Approve does the script become effective for real use.
The provider (Anthropic, OpenAI or one compatible with the OpenAI API, such as Ollama, Groq or OpenRouter) is chosen in Settings → AI. The API key is stored encrypted and never shown again.
The AI call happens only on this screen, by explicit user action. The engine that runs transformers during receipt, delivery and collection does not depend on AI — a provider outage does not affect the delivery of a single message.
Connecting two Definitions that already exist
The Integrate two Definitions icon (wand), on this screen’s filter bar, opens the assistant that generates the Transformer between two ends already configured in the CMS, from what actually flows through each one. See Between Applications.
The execution engine
Implemented in cms-api/src/modules/transformador/.
Isolation
Scripts run in worker_threads, in a pool managed by TransformadorWorkerPool:
- the API’s main thread is never blocked by a slow or hung script;
- each worker has a memory limit via
resourceLimits; - besides the
vmtimeout inside the worker, there is an external timeout: a hung worker is killed and replaced in the pool.
The pool size comes from TRANSFORMADOR_WORKER_POOL_SIZE (default 4).
In practice: a script with an infinite loop takes down the execution for that message only — not the API, not the scheduler, not the other interfaces.
Generation is decoupled from execution
The AI lives in modules/transformador-ia/, separate from the pool. Every generation is recorded in
TransformadorGeracao with the prompt used and can be restored.
Never point the execution engine at the AI service. The separation exists so a provider outage does not affect message delivery.
Versions and scope
TransformadorVersao— script version history.TransformadorEscopo— where the transformer is in use.
The consolidated usage in the listing does not come from TransformadorEscopo: it is counted
across the five real link tables (Delivery, Collection, the two forwarding kinds and
DefinicaoTransformadorProdutor, the Transformer by producer Application). TransformadorEscopo
only holds the reference list behind the scope popup — the difference between the two is exactly the
Actual use column.