feat: extract model runtime

Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
-LAN-
2026-03-15 15:34:47 +08:00
parent 3d5a29462e
commit fbb74a4af9
178 changed files with 4343 additions and 2134 deletions

View File

@@ -0,0 +1,39 @@
from __future__ import annotations
from collections.abc import Mapping
from typing import Any, Protocol
from dify_graph.nodes.code.code_node import WorkflowCodeExecutor
from dify_graph.nodes.code.entities import CodeLanguage
class TemplateRenderError(ValueError):
"""Raised when rendering a template fails."""
class Jinja2TemplateRenderer(Protocol):
"""Shared contract for rendering Jinja2 templates in graph nodes."""
def render_template(self, template: str, variables: Mapping[str, Any]) -> str: ...
class CodeExecutorJinja2TemplateRenderer(Jinja2TemplateRenderer):
"""Adapter that renders Jinja2 templates via the workflow code executor."""
_code_executor: WorkflowCodeExecutor
def __init__(self, code_executor: WorkflowCodeExecutor) -> None:
self._code_executor = code_executor
def render_template(self, template: str, variables: Mapping[str, Any]) -> str:
try:
result = self._code_executor.execute(language=CodeLanguage.JINJA2, code=template, inputs=variables)
except Exception as exc:
if self._code_executor.is_execution_error(exc):
raise TemplateRenderError(str(exc)) from exc
raise
rendered = result.get("result")
if not isinstance(rendered, str):
raise TemplateRenderError("Template render result must be a string.")
return rendered