Files
dify/api/services/dify_builder/agent/llm_agent.py
T
Charles YaoandClaude Opus 4.8 8683e6517c feat(dify-builder): wire real Build cognition + neutral empty test-repair
LlmBuilderAgent's 6 Build methods (analyze_goal, propose_plan_v1,
discover_resources, bind_resources, build_nodes, learn_from_build) now
delegate to services.dify_builder.agent.build instead of the canned
PlaceholderAgent. propose_build_repair returns [] -- there's no real
failure signal to diagnose until the live test step is de-canned.

handle_test_and_repair now conditions the error card + apply_repair on
a non-empty propose_build_repair result: placeholder mode keeps today's
"found and fixed" arc, while real cognition (which never reports a
failure yet) emits a neutral "no issues found" review summary instead
of a fake error/fix on a hardcoded node id that real build_nodes
wouldn't have generated anyway.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-29 12:25:14 -07:00

91 lines
4.2 KiB
Python

"""The real Dify Builder agent shell.
Owns the resolved model. Of the Protocol's 14 methods (Fix 4 + Build 7 + Edit 3),
the 3 Fix methods that reason about a failed run -- ``diagnose``,
``diagnose_checklist``, ``propose_repair`` -- and 6 of the 7 Build methods --
``analyze_goal``, ``propose_plan_v1``, ``discover_resources``, ``bind_resources``,
``build_nodes``, ``learn_from_build`` -- now use real LLM cognition via
``services.dify_builder.agent.fix`` / ``services.dify_builder.agent.build``,
resolving the model through ``_model_or_none`` (which degrades to ``None`` on
resolution failure rather than crashing the advance; ``fix.*``/``build.*``
handle that). ``propose_build_repair`` returns ``[]`` -- there is no real
failure signal to diagnose until the live test step is de-canned (deferred).
``generate_mock_inputs`` and every Edit method still delegate to the canned
``PlaceholderAgent`` unchanged. Later slices replace those remaining method
bodies one at a time with real LLM calls, no structural change.
"""
from typing import Any
from core.dify_builder.placeholder_agent import PlaceholderAgent
from core.model_manager import ModelInstance
from services.dify_builder.agent import build, fix # `edit` import added in Task B3
from services.dify_builder.agent.model_resolver import resolve_model_instance
class LlmBuilderAgent:
def __init__(self, tenant_id: str, model_config: dict[str, Any] | None = None) -> None:
self._tenant_id = tenant_id
self._model_config = model_config or {}
self._canned = PlaceholderAgent()
self._model_instance: ModelInstance | None = None
def _model(self) -> ModelInstance:
"""Lazily resolve + memoize the chosen model. Used by cognition (next slice)."""
if self._model_instance is None:
self._model_instance = resolve_model_instance(self._tenant_id, self._model_config)
return self._model_instance
def _model_or_none(self) -> ModelInstance | None:
"""The resolved model, or None if it can't resolve — fix.* degrades on None
rather than crashing the advance."""
try:
return self._model()
except Exception: # any resolution failure -> degrade path (fix.* handles None)
return None
# -- Fix cognition (delegated) --
def diagnose(self, failed_run, graph, node_outputs):
return fix.diagnose(self._model_or_none(), failed_run, graph, node_outputs)
def diagnose_checklist(self, errors, graph):
return fix.diagnose_checklist(self._model_or_none(), errors, graph)
def propose_repair(self, diagnosis, graph):
return fix.propose_repair(self._model_or_none(), diagnosis, graph)
def generate_mock_inputs(self, schema, prior_failed):
return self._canned.generate_mock_inputs(schema, prior_failed)
# -- Build cognition (real) --
def analyze_goal(self, goal_text):
return build.analyze_goal(self._model_or_none(), goal_text)
def propose_plan_v1(self, requirements):
return build.propose_plan_v1(self._model_or_none(), requirements)
def discover_resources(self, plan_items):
return build.discover_resources(self._model_or_none(), self._tenant_id, plan_items)
def bind_resources(self, plan_items, resource_ids, conflict_policy):
return build.bind_resources(self._model_or_none(), self._tenant_id, plan_items, resource_ids, conflict_policy)
def build_nodes(self, plan_items):
return build.build_nodes(self._tenant_id, self._model_config, plan_items)
def propose_build_repair(self, built_node_ids):
return [] # no real failure signal until the live test step is de-canned (deferred)
def learn_from_build(self, goal_text, requirements, plan_items, built_node_ids):
return build.learn_from_build(self._model_or_none(), goal_text, requirements, plan_items, built_node_ids)
# -- Edit cognition (delegated) --
def analyze_impact(self, goal_text, graph):
return self._canned.analyze_impact(goal_text, graph)
def propose_edit_plan(self, edit_rules, graph):
return self._canned.propose_edit_plan(edit_rules, graph)
def build_edit_intents(self, edit_rules, graph):
return self._canned.build_edit_intents(edit_rules, graph)