diff --git a/api/services/dify_builder/agent/build.py b/api/services/dify_builder/agent/build.py index 5f623e7b27a..5432736b092 100644 --- a/api/services/dify_builder/agent/build.py +++ b/api/services/dify_builder/agent/build.py @@ -7,6 +7,7 @@ rather than crashing the advance. build_nodes lives in the same module import json import logging +import re from collections.abc import Callable, Sequence from datetime import UTC, datetime from typing import Any @@ -510,6 +511,8 @@ def build_nodes( # runtime model follows the user's choice. grounding_mc = _selected_workflow_model(tenant_id, resource_ids) or mc _ground(intents, grounding_mc, tenant_id, plan_items) + for node_id in _ground_placeholder_endpoints(intents): + logger.info("Dify Builder: http-request %s had a placeholder URL; now read from a start variable", node_id) applicable, rejected = graph_ops.filter_applicable({"nodes": [], "edges": []}, intents, _ALLOWED_NODE_TYPES) if not applicable: reason = rejected[0][1] if rejected else "no applicable node intents" @@ -569,6 +572,73 @@ def build_nodes( ) +# Hosts / tokens an LLM writes when it does not know the real endpoint. A Dify +# template (``{{#node.var#}}``) is a real reference and is NOT a placeholder; +# a bare ``{tenant}`` or ```` is. +_PLACEHOLDER_URL_RE = re.compile( + r"(^|[./-])example\.(com|org|net)\b" # api.example.com, example.org + r"|your[-_]?(api|domain|server|host|company)" # your-api.com + r"|placeholder|localhost" + r"|<[^>]+>" # + r"|(? bool: + """True for a URL the model invented rather than one the user supplied.""" + text = (url or "").strip() + return not text or _PLACEHOLDER_URL_RE.search(text) is not None + + +def _ground_placeholder_endpoints(intents: list[MutationIntent]) -> list[str]: + """Replace every http-request placeholder URL with a REQUIRED start-node + variable and a template reference to it. + + ESQ1-302 called ``https://api.example.com/ppt/generate``: the model had no + endpoint, so it invented one, and every test run failed against a host that + does not exist. Turning the URL into an input makes the test-data gate ask + the user for the real endpoint -- or, when they have none, tells them what + the step needs. Builder-only; the shared generator's own http-request + example is ``https://example.com``, so cmd+K keeps its placeholders. + Returns the ids of the http-request nodes it re-pointed. + """ + start = next((i for i in intents if i.op == "create_node" and i.args.get("node_type") == "start"), None) + if start is None: + return [] + start_id = str(start.args.get("node_id") or "") + if not start_id: + return [] + config = dict(start.args.get("config") or {}) + variables = list(config.get("variables") or []) + grounded: list[str] = [] + for intent in intents: + if intent.op != "create_node" or intent.args.get("node_type") != "http-request": + continue + node_config = dict(intent.args.get("config") or {}) + if not _is_placeholder_endpoint(str(node_config.get("url") or "")): + continue + node_id = str(intent.args.get("node_id") or "") + var_name = f"{node_id}_url" + if not any(v.get("variable") == var_name for v in variables if isinstance(v, dict)): + variables.append( + { + "variable": var_name, + "label": f"{node_config.get('title') or node_id} URL", + "type": "text-input", + "required": True, + "max_length": 2048, + } + ) + node_config["url"] = f"{{{{#{start_id}.{var_name}#}}}}" + intent.args["config"] = node_config + grounded.append(node_id) + if grounded: + config["variables"] = variables + start.args["config"] = config + return grounded + + def _ground(intents: list[MutationIntent], mc: ModelConfig, tenant_id: str, plan_items: list[str]) -> None: mode = mc.mode.value if hasattr(mc.mode, "value") else str(mc.mode) datasets = resources.list_tenant_resources(tenant_id).datasets diff --git a/api/tests/unit_tests/services/dify_builder/test_build_nodes.py b/api/tests/unit_tests/services/dify_builder/test_build_nodes.py index 54f10c6616c..88edc9aa4c2 100644 --- a/api/tests/unit_tests/services/dify_builder/test_build_nodes.py +++ b/api/tests/unit_tests/services/dify_builder/test_build_nodes.py @@ -516,3 +516,88 @@ def test_build_nodes_keeps_the_applicable_intents_and_records_a_partial_reject(c assert "connect" in warnings[0] assert "'maybe'" in warnings[0] assert "has no handle" in warnings[0] + + +# ---- ESQ1-302: placeholder endpoints ----------------------------------------- + +_HTTP_BODY = {"type": "json", "data": [{"type": "text", "key": "", "value": "{}"}]} + + +def _gen_graph_with_http(url: str) -> dict: + return { + "graph": { + "nodes": [ + {"id": "s", "type": "custom", "data": {"type": "start", "title": "Start", "variables": []}}, + { + "id": "h", + "type": "custom", + "data": { + "type": "http-request", + "title": "Call PPT API", + "method": "post", + "url": url, + "authorization": {"type": "no-auth", "config": None}, + "headers": "", + "params": "", + "body": _HTTP_BODY, + }, + }, + {"id": "e", "type": "custom", "data": {"type": "end", "title": "End", "outputs": []}}, + ], + "edges": [{"id": "e1", "source": "s", "target": "h"}, {"id": "e2", "source": "h", "target": "e"}], + }, + "error": "", + "errors": [], + } + + +def _build_with(gen_graph: dict): + with ( + patch.object(build, "_generator_model_config", return_value=_fake_mc("anthropic", "x")), + patch( + "services.dify_builder.agent.build.WorkflowGeneratorService.generate_workflow_graph", + return_value=gen_graph, + ), + patch.object( + build.resources, + "list_tenant_resources", + return_value=resources.TenantResources(models=[], datasets=[], tools=[]), + ), + ): + return build.build_nodes("t1", {}, ["Call the PPT API"]).intents + + +def test_build_nodes_grounds_a_placeholder_endpoint_into_a_required_start_variable(): + """The ESQ1-302 draft called ``https://api.example.com/ppt/generate`` -- an + endpoint the model invented. A placeholder URL now becomes a required start + variable, so the test-data gate asks the user for the real endpoint instead + of the run failing against a host that does not exist.""" + intents = _build_with(_gen_graph_with_http("https://api.example.com/ppt/generate")) + + http = next(i for i in intents if i.args.get("node_type") == "http-request") + start = next(i for i in intents if i.args.get("node_type") == "start") + assert http.args["config"]["url"] == "{{#s.h_url#}}" + var = next(v for v in start.args["config"]["variables"] if v["variable"] == "h_url") + assert var["type"] == "text-input" + assert var["required"] is True + assert "Call PPT API" in var["label"] + + +def test_build_nodes_leaves_a_real_endpoint_and_a_templated_url_alone(): + for url in ("https://api.openai.com/v1/chat", "{{#s.endpoint#}}/generate"): + intents = _build_with(_gen_graph_with_http(url)) + http = next(i for i in intents if i.args.get("node_type") == "http-request") + start = next(i for i in intents if i.args.get("node_type") == "start") + assert http.args["config"]["url"] == url + assert start.args["config"]["variables"] == [] + + +def test_is_placeholder_endpoint_recognises_the_usual_inventions(): + assert build._is_placeholder_endpoint("https://api.example.com/ppt/generate") + assert build._is_placeholder_endpoint("http://example.org/x") + assert build._is_placeholder_endpoint("https://your-api.com/v1") + assert build._is_placeholder_endpoint("https:///generate") + assert build._is_placeholder_endpoint("https://api.acme.com/{tenant}/x") + assert build._is_placeholder_endpoint("") + assert not build._is_placeholder_endpoint("https://api.openai.com/v1") + assert not build._is_placeholder_endpoint("{{#s.endpoint#}}/x") # a Dify template is a real reference