From 4ec3e5bfe77e08bbf5a25db7b3a711979bc3b67c Mon Sep 17 00:00:00 2001 From: Charles Yao Date: Sat, 29 Aug 2026 11:30:08 -0700 Subject: [PATCH] feat(dify-builder): translate generator graph into build intents Co-Authored-By: Claude Opus 4.8 --- .../dify_builder/agent/graph_translate.py | 52 +++++++++++++++++++ .../dify_builder/test_graph_translate.py | 24 +++++++++ 2 files changed, 76 insertions(+) create mode 100644 api/services/dify_builder/agent/graph_translate.py create mode 100644 api/tests/unit_tests/services/dify_builder/test_graph_translate.py diff --git a/api/services/dify_builder/agent/graph_translate.py b/api/services/dify_builder/agent/graph_translate.py new file mode 100644 index 00000000000..d40e883932a --- /dev/null +++ b/api/services/dify_builder/agent/graph_translate.py @@ -0,0 +1,52 @@ +"""Translate a WorkflowGenerator graph into Dify Builder MutationIntents. + +The generator emits ReactFlow nodes ({id, type:"custom", data:{type:,...}}) +and edges. We emit one create_node per node (config = node.data minus the +redundant "type" key -- apply_create_node re-sets it) followed by one connect +per edge, so every connect endpoint already exists at apply time. The start +node is renamed to the conventional id "start" so it reconciles with a draft +that already has one (the handler's present-filter drops the duplicate).""" + +from typing import Any + +from core.dify_builder.models import MutationIntent + +_START_ID = "start" + + +def to_intents(graph: dict[str, Any]) -> list[MutationIntent]: + nodes = graph.get("nodes") or [] + edges = graph.get("edges") or [] + + # rename map: the generator start id -> canonical "start" + remap: dict[str, str] = {} + for node in nodes: + data = node.get("data") or {} + if data.get("type") == "start" and node.get("id"): + remap[str(node["id"])] = _START_ID + + creates: list[MutationIntent] = [] + for node in nodes: + data = node.get("data") or {} + node_type = data.get("type") + node_id = node.get("id") + if not node_type or not node_id: + continue + config = {k: v for k, v in data.items() if k != "type"} + creates.append(MutationIntent(op="create_node", args={ + "node_type": str(node_type), + "config": config, + "node_id": remap.get(str(node_id), str(node_id)), + })) + + connects: list[MutationIntent] = [] + for edge in edges: + src, tgt = edge.get("source"), edge.get("target") + if not src or not tgt: + continue + connects.append(MutationIntent(op="connect", args={ + "from_node": remap.get(str(src), str(src)), + "to_node": remap.get(str(tgt), str(tgt)), + })) + + return creates + connects diff --git a/api/tests/unit_tests/services/dify_builder/test_graph_translate.py b/api/tests/unit_tests/services/dify_builder/test_graph_translate.py new file mode 100644 index 00000000000..fddeb0a7324 --- /dev/null +++ b/api/tests/unit_tests/services/dify_builder/test_graph_translate.py @@ -0,0 +1,24 @@ +def test_to_intents_translates_nodes_then_edges_and_normalizes_start(): + from services.dify_builder.agent.graph_translate import to_intents + + graph = { + "nodes": [ + {"id": "n_start", "type": "custom", "position": {"x": 0, "y": 0}, + "data": {"type": "start", "title": "Start", "variables": []}}, + {"id": "n_llm", "type": "custom", "position": {"x": 1, "y": 0}, + "data": {"type": "llm", "title": "LLM", "model": {"provider": "", "name": ""}}}, + ], + "edges": [{"id": "e1", "source": "n_start", "target": "n_llm", "type": "custom"}], + } + intents = to_intents(graph) + ops = [(i.op, i.args.get("node_id") or (i.args.get("from_node"), i.args.get("to_node"))) for i in intents] + assert ops == [("create_node", "start"), ("create_node", "n_llm"), ("connect", ("start", "n_llm"))] + # config carries data minus the type key; create_node args expose node_type + start = intents[0] + assert start.args["node_type"] == "start" and "type" not in start.args["config"] + assert start.args["config"]["title"] == "Start" + +def test_to_intents_skips_malformed_nodes(): + from services.dify_builder.agent.graph_translate import to_intents + graph = {"nodes": [{"id": "x", "data": {}}, {"id": "", "data": {"type": "llm"}}], "edges": []} + assert to_intents(graph) == [] # no type / no id -> dropped