Skip to content

Commit ee67d48

Browse files
committed
fix(flows-create): auto-generate node IDs and remap wires
AI agents supply logical node IDs (like 'inject1') for wiring references, but those IDs can collide with existing Node-RED node IDs, causing 'duplicate id' errors. Now the tool always generates fresh UUID-style IDs, builds an old→new remap, and rewires all connections. Fixes the dogfooding gap where agents had to create a flow empty then update it — now a single create call works.
1 parent d9d7992 commit ee67d48

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

src/tools/flows/create.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ export function registerCreateFlowTool(server: McpServer, client: NodeRedClient)
2323
},
2424
async (args) => {
2525
const flowId = generateId();
26+
const remap = new Map<string, string>();
27+
for (const node of args.nodes) {
28+
const oldId = (node.id as string) || generateId();
29+
remap.set(oldId, generateId());
30+
}
31+
const nodes = args.nodes.map((n: Record<string, unknown>) => {
32+
const oldId = (n.id as string) || "";
33+
const next: Record<string, unknown> = { ...n, id: remap.get(oldId) ?? generateId(), z: flowId };
34+
if (Array.isArray(next.wires)) {
35+
next.wires = next.wires.map((ports: unknown) =>
36+
Array.isArray(ports) ? ports.map((tid: string) => remap.get(tid) ?? tid) : ports
37+
);
38+
}
39+
return next;
40+
});
2641
const doc = {
2742
id: flowId,
2843
label: args.label,
29-
nodes: args.nodes.map((n) => ({ ...n, id: (n.id as string) || generateId(), z: flowId })),
44+
nodes,
3045
configs: args.configs,
3146
subflows: args.subflows,
3247
} as unknown as FlowDocument;

0 commit comments

Comments
 (0)