feat(workflow #874): NodeRegistry 统一工作流节点抽象 + 双 Claw 事件对齐
#874 P3 工作流引擎 NodeRegistry 统一(加速带「工具即节点」思想落地,零源码拷贝重写): - app/core/node_registry.py(新增):把 skill / MCP / Agent / DAG 四套割裂表达收敛为 单一 Node 抽象。BaseNode(Protocol) 定义 node_type/name/input_schema/output_schema/ risk_level + async run(ctx);NodeContext 携带 db/tenant_id/claw_type/trace_id/黑板/emit; NodeResult.to_blackboard() 与既有 orchestrator 黑板结构无缝互转。内建八型节点 llm/reduce/local/external/agent/skill/mcp/loop,均继承 NodeBase 公共生命周期 (发 start 步骤 -> 执行 -> ok/fail 覆盖同 index),异常统一归一为 NodeError(带结构化 diagnosis)。SkillNode 用 asyncio.to_thread 包裹同步执行器避免阻塞事件循环; LoopNode 硬限流 20 次迭代 / 并发上限 5,子节点经 ctx.child() 复用同一 StepCounter 与 emit,保证步骤序号全图唯一。CapabilitySnapshot 统一承载 skills/mcp_servers/agents/ function_definitions/mcp_tool_map,供 _build_system_prompt、run_graph、yaml 校验三处 共用同一份能力真相。normalize_claw() 将 im_wechat/im_platform 归一为 "im",避免 Grafana 维度碎片化。 - app/core/capability_discovery.py:新增 discover_agents(),与 discover_skills 同构的 三级降级策略(显式安装 -> published 兜底 -> rank_by_query 裁剪),补齐 Agent 维度的 统一能力发现,杜绝各处内联第二份发现逻辑(#866 回退事故教训)。 - app/core/orchestrator.py:VALID_NODE_TYPES 与 validate_graph 改为运行期读 registry.node_types()/requires_agent(),注册表成为节点类型的单一真相源;_run_node 改为 拼装 NodeContext 后委托 registry.get(ntype).run(ctx),本体只保留调度与拓扑;run_graph 新增 on_event/claw_type/tenant_id/snapshot 参数,编排全过程以统一 EngineEvent 下发。 - app/core/yaml_workflow.py:valid_node_types() 实时读注册表;validate_yaml 补 skill/mcp/ loop 必填校验;yaml_to_orchestration / orchestration_to_yaml 双向透传节点扩展字段 (label/skill_id/func_name/tool/server_id/args/body/items/split/max_iterations/ concurrency/join),修复配置往返丢字段。 - app/core/engine_event.py:make_step 新增 node 参数,以 step["node"]={id,type,label} 承载节点身份。设计书原文的 EngineEvent(kind="node") 与前端仅识别 thinking/skill/mcp/ agent/kb/media 的现状冲突(未知 kind 会被静默丢帧),故映射为已知 kind + 附加字段, 线格式对旧前端字节兼容。 - app/api/chat_service.py:Web Claw 编排调用点透传 on_event/claw_type/tenant_id, 租户与通道标签复用 #875 的 ContextVar(get_route_ctx),不再新增一层参数透传。 IM Claw 经 EngineCore 自动继承同一事件流(单引擎原则,im_llm.py 零改动)。 - tests/test_node_registry.py(新增):R-NODE-1~11 确定性哨兵,无网络无外部依赖。守卫 内建八型齐全、注册表为单一真相源(orchestrator 与 yaml 校验实时同源)、describe 契约、 双 Claw 事件对齐(SseAdapter 与 BufferAdapter 逐键相等且均带 node 身份)、claw 归一与 tenant 标签、新节点必填校验、YAML 往返不吞字段、Loop 限流聚合、失败结构化诊断两端可见、 禁止第二份能力发现、register 拒绝非法节点。 验证:容器内 py_compile 全通过;pytest tests/ 退出码 0(85 项 = 83 passed + 2 skipped, 0 FAILED/ERROR),含 R-NODE 16 项与 R-DUAL 双 Claw 哨兵。 safe-align: 仅复制 7 个 git-tracked 文件,未触碰 secrets/.venv/node_modules/dist/caches。 本地提交不 push。
This commit is contained in:
1 parent
04a0641202
commit
33d0f9c32e
7 files changed
+1703
-57
No files matched your search
@@ -28,7 +28,9 @@
|
||||
--------------------------------
|
||||
CAP_SKILL_LIMIT 单轮注入技能数上限,默认 12
|
||||
CAP_MCP_LIMIT 单轮注入 MCP 服务数上限,默认 8
|
||||
CAP_AGENT_LIMIT 单轮可见 Agent 数上限,默认 6
|
||||
CAP_SKILL_FALLBACK_PUBLISHED 安装表为空时是否降级为 published,默认 开
|
||||
CAP_AGENT_FALLBACK_PUBLISHED Agent 同上,默认 开
|
||||
CAP_MCP_AUTO 未显式指定时是否自动挂载已启用 MCP,默认 开
|
||||
"""
|
||||
|
||||
@@ -44,6 +46,7 @@ logger = logging.getLogger(__name__)
|
||||
__all__ = [
|
||||
"discover_skills",
|
||||
"discover_mcp_servers",
|
||||
"discover_agents",
|
||||
"build_mcp_tool_defs",
|
||||
"rank_by_query",
|
||||
]
|
||||
@@ -226,6 +229,97 @@ def discover_mcp_servers(
|
||||
return out
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Agent 发现(#874:画布节点候选 / IM `/flow` 可选清单)
|
||||
# --------------------------------------------------------------------------
|
||||
def discover_agents(
|
||||
db,
|
||||
user_id: str | None,
|
||||
query: str = "",
|
||||
explicit_ids: Iterable[str] | None = None,
|
||||
limit: int | None = None,
|
||||
claw: str = "web",
|
||||
) -> list:
|
||||
"""发现当前请求可用的智能体(双 Claw 共用),策略与 discover_skills 同构。
|
||||
|
||||
返回 `_resolve_market_agents` 的结构:
|
||||
[{id, name, role_prompt, instructions, skill_ids, knowledge_base_ids,
|
||||
model_id, tools, description, tool_policy, platform, agent_endpoint,
|
||||
agent_api_key, orchestration}]
|
||||
|
||||
用途定位:给 NodeRegistry 生成 agent 型节点候选、给 IM `/flow` 列可选智能体。
|
||||
若要拿它往 system prompt 里注入,请自行收窄 limit —— 多个 Agent 人格叠加会串味。
|
||||
"""
|
||||
limit = limit if limit is not None else _env_int("CAP_AGENT_LIMIT", 6)
|
||||
if limit <= 0:
|
||||
return []
|
||||
|
||||
from app.api.chat_skill import _resolve_market_agents
|
||||
from app.models import AppInstallation, MarketApp
|
||||
|
||||
# 0) 显式指定(Web Claw 勾选 / 画布已引用)—— 不做相关性裁剪,用户意愿最高
|
||||
ids = [i for i in (explicit_ids or []) if i]
|
||||
if ids:
|
||||
out = _resolve_market_agents(db, ids, user_id or "")
|
||||
logger.info("[cap] agents claw=%s source=explicit picked=%d", claw, len(out))
|
||||
return out
|
||||
|
||||
pool: list = []
|
||||
source = "empty"
|
||||
|
||||
# 1) 用户显式安装
|
||||
if user_id:
|
||||
try:
|
||||
app_ids = [
|
||||
i.app_id
|
||||
for i in db.query(AppInstallation)
|
||||
.filter(AppInstallation.user_id == user_id)
|
||||
.all()
|
||||
]
|
||||
if app_ids:
|
||||
aids = [
|
||||
a.id
|
||||
for a in db.query(MarketApp)
|
||||
.filter(
|
||||
MarketApp.id.in_(app_ids),
|
||||
MarketApp.type == "agent",
|
||||
MarketApp.status == "published",
|
||||
)
|
||||
.all()
|
||||
]
|
||||
if aids:
|
||||
pool = _resolve_market_agents(db, aids, user_id)
|
||||
source = "installed"
|
||||
except Exception as e:
|
||||
logger.warning("[cap] 安装 Agent 发现失败(降级继续): %s", e)
|
||||
|
||||
# 2) 降级:平台已发布全集
|
||||
if not pool and _env_flag("CAP_AGENT_FALLBACK_PUBLISHED", True):
|
||||
try:
|
||||
aids = [
|
||||
a.id
|
||||
for a in db.query(MarketApp)
|
||||
.filter(MarketApp.type == "agent", MarketApp.status == "published")
|
||||
.all()
|
||||
]
|
||||
if aids:
|
||||
pool = _resolve_market_agents(db, aids, user_id or "")
|
||||
source = "published"
|
||||
except Exception as e:
|
||||
logger.warning("[cap] published Agent 发现失败: %s", e)
|
||||
|
||||
# 3) 预算裁剪
|
||||
picked = rank_by_query(pool, query, limit)
|
||||
logger.info(
|
||||
"[cap] agents claw=%s source=%s pool=%d picked=%d",
|
||||
claw,
|
||||
source,
|
||||
len(pool),
|
||||
len(picked),
|
||||
)
|
||||
return picked
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# MCP → function definitions
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
@@ -27,7 +27,8 @@ step 对象契约(AiProcessBlock 消费):
|
||||
skill -> {"type":"skill", "name": str, "args": dict, "result": str}
|
||||
mcp -> {"type":"mcp", "server": str, "name": str, "args": dict, "result": str}
|
||||
|
||||
附加(新增,前端可选消费): "diagnosis": {...}, "phase": "start|ok|fail"
|
||||
附加(新增,前端可选消费): "diagnosis": {...}, "phase": "start|ok|fail",
|
||||
"node": {"id","type","label"} # #874 节点身份
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -233,12 +234,18 @@ def make_step(
|
||||
phase: str = "",
|
||||
index: Optional[int] = None,
|
||||
diagnosis: Any = None,
|
||||
node: Optional[Dict[str, Any]] = None,
|
||||
**kw,
|
||||
) -> EngineEvent:
|
||||
"""过程步骤(思考 / 技能 / MCP / Agent / KB / 媒体)。
|
||||
|
||||
index 用于「原地更新同一步骤」:先发 phase=start 占位,
|
||||
执行完再用同一 index 发 phase=ok|fail 覆盖,前端即得到实时进度。
|
||||
|
||||
node(#874)承载图节点身份 {id, type, label}。设计书原文写的是
|
||||
`EngineEvent(kind="node")`,但前端只认既有 step 类型,发未知 type 会静默丢帧;
|
||||
故 kind 仍取节点映射后的已知类型,节点身份改由本字段附加 —— 非空才输出,
|
||||
老前端忽略,新前端可据此渲染 DAG 节点进度。
|
||||
"""
|
||||
step: Dict[str, Any] = {"type": kind}
|
||||
if kind == STEP_THINKING:
|
||||
@@ -254,6 +261,8 @@ def make_step(
|
||||
step["result"] = result or ""
|
||||
if phase:
|
||||
step["phase"] = phase
|
||||
if isinstance(node, dict) and node:
|
||||
step["node"] = node
|
||||
|
||||
diag = Diagnosis.from_dict(diagnosis) if diagnosis is not None else None
|
||||
if diag is not None:
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -7,6 +7,18 @@
|
||||
- 节点执行复用既有原语(_build_system_prompt / _execute_skill_tool / _call_external_agent /
|
||||
3.1 工具沙箱),完全向后兼容——agent.orchestration 为 null 时 chat_service 走原逻辑。
|
||||
- 并发安全:每个节点用独立 SessionLocal 会话做 DB 读,避免共享请求会话跨协程竞态。
|
||||
|
||||
#874 变更
|
||||
--------
|
||||
节点类型不再硬编码在本模块,改由 `app.core.node_registry` 统一持有:
|
||||
|
||||
* 合法类型 -> registry.node_types()(同时供 yaml_workflow 校验,单一真相源)
|
||||
* 节点执行 -> registry.get(type).run(NodeContext),本模块只负责拓扑与调度
|
||||
* 双 Claw 对齐 -> run_graph(on_event=...) 把节点过程发成统一 EngineEvent,
|
||||
Web(SseAdapter) 与 IM(BufferAdapter) 吃同一份事件流
|
||||
|
||||
原有 llm/local/external/reduce 语义逐字保留(节点实现内部仍调用本模块的
|
||||
_llm_complete / _run_local_agent_turn),新增 skill/mcp/agent/loop 四型。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -17,9 +29,20 @@ from typing import Any, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from app.core.engine_event import STEP_THINKING, make_step
|
||||
from app.core.node_registry import (
|
||||
NodeContext,
|
||||
NodeError,
|
||||
StepCounter,
|
||||
normalize_claw,
|
||||
registry,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("aiflow.orchestrator")
|
||||
|
||||
VALID_NODE_TYPES = ("llm", "local", "external", "reduce")
|
||||
# 兼容导出:历史调用方直接 import 了这个名字。真正的校验一律走 registry.node_types(),
|
||||
# 以便运行期注册的第三方节点立即生效(本常量只是 import 期快照)。
|
||||
VALID_NODE_TYPES = registry.node_types()
|
||||
|
||||
|
||||
class OrchestrationError(ValueError):
|
||||
@@ -50,12 +73,25 @@ def validate_graph(graph: Any) -> None:
|
||||
raise OrchestrationError(f"节点 id 重复: {nid}")
|
||||
ids.add(nid)
|
||||
ntype = n.get("type")
|
||||
if ntype not in VALID_NODE_TYPES:
|
||||
valid = registry.node_types()
|
||||
if ntype not in valid:
|
||||
raise OrchestrationError(
|
||||
f"节点 {nid} 类型非法: {ntype} (允许 {', '.join(VALID_NODE_TYPES)})"
|
||||
f"节点 {nid} 类型非法: {ntype} (允许 {', '.join(valid)})"
|
||||
)
|
||||
if ntype in ("local", "external") and not n.get("agent_id"):
|
||||
if registry.requires_agent(ntype) and not n.get("agent_id"):
|
||||
raise OrchestrationError(f"节点 {nid} 类型 {ntype} 必须指定 agent_id")
|
||||
if ntype == "skill" and not (n.get("skill_id") or n.get("func_name")):
|
||||
raise OrchestrationError(f"节点 {nid} 类型 skill 必须指定 skill_id 或 func_name")
|
||||
if ntype == "mcp" and not n.get("tool"):
|
||||
raise OrchestrationError(f"节点 {nid} 类型 mcp 必须指定 tool")
|
||||
if ntype == "loop":
|
||||
body = n.get("body")
|
||||
if not isinstance(body, dict) or not body.get("type"):
|
||||
raise OrchestrationError(f"节点 {nid} 类型 loop 必须提供含 type 的 body")
|
||||
if body["type"] not in valid:
|
||||
raise OrchestrationError(f"节点 {nid} 的 body 类型非法: {body['type']}")
|
||||
if body["type"] == "loop":
|
||||
raise OrchestrationError(f"节点 {nid} 的 body 不允许再嵌套 loop")
|
||||
|
||||
for i, e in enumerate(edges):
|
||||
if not isinstance(e, dict):
|
||||
@@ -275,12 +311,33 @@ async def _run_local_agent_turn(agent: dict, node_input: str, db, user_id, conv_
|
||||
# ---------------------------------------------------------------------------
|
||||
# 单节点执行(含独立 DB 会话,保证并发安全)
|
||||
# ---------------------------------------------------------------------------
|
||||
async def _run_node(node, graph, blackboard, query, user_id, conv_id, trace_id) -> dict:
|
||||
from app.api.chat_skill import _resolve_market_agents
|
||||
async def _run_node(
|
||||
node,
|
||||
graph,
|
||||
blackboard,
|
||||
query,
|
||||
user_id,
|
||||
conv_id,
|
||||
trace_id,
|
||||
*,
|
||||
on_event=None,
|
||||
claw_type: str = "",
|
||||
tenant_id: str = "",
|
||||
counter: Optional[StepCounter] = None,
|
||||
snapshot=None,
|
||||
) -> dict:
|
||||
"""执行单节点:拼装上游输入 → 交给 registry 里的节点实现 → 回填黑板结构。
|
||||
|
||||
本函数不再包含任何节点类型分支 —— 新增能力只需注册新节点,无需改调度层。
|
||||
"""
|
||||
from app.database import SessionLocal
|
||||
|
||||
nid = node["id"]
|
||||
ntype = node["type"]
|
||||
impl = registry.get(ntype)
|
||||
if impl is None:
|
||||
raise OrchestrationError(f"未知节点类型 {ntype}")
|
||||
|
||||
preds = _predecessors(graph, nid)
|
||||
pred_results = [blackboard[p] for p in preds]
|
||||
is_entry = not preds
|
||||
@@ -298,66 +355,104 @@ async def _run_node(node, graph, blackboard, query, user_id, conv_id, trace_id)
|
||||
upstream = ""
|
||||
|
||||
with SessionLocal() as db:
|
||||
if ntype in ("llm", "reduce"):
|
||||
if ntype == "llm":
|
||||
sys_prompt = node.get("system_prompt") or (
|
||||
"你是一个处理节点,请根据上游输入完成指定的文本变换、摘要或路由判断,"
|
||||
"只输出处理后的结果,不要多余解释。"
|
||||
)
|
||||
else:
|
||||
sys_prompt = node.get("system_prompt") or (
|
||||
"你是一个结果汇总器,请将多个子任务的输出综合成连贯、完整、面向用户的最终回答。"
|
||||
)
|
||||
uc = _assemble_node_input(query, upstream, is_entry)
|
||||
text = await _llm_complete(sys_prompt, uc, node.get("model_id"), db, user_id)
|
||||
return {"ok": True, "node_id": nid, "node_type": ntype, "output": text}
|
||||
|
||||
if ntype == "local":
|
||||
agents = _resolve_market_agents(db, [node["agent_id"]], user_id)
|
||||
if not agents:
|
||||
raise OrchestrationError(f"local 节点 {nid} 引用的 agent_id={node['agent_id']} 不存在")
|
||||
ni = _assemble_node_input(query, upstream, is_entry)
|
||||
res = await _run_local_agent_turn(agents[0], ni, db, user_id, conv_id)
|
||||
return {
|
||||
"ok": True,
|
||||
"node_id": nid,
|
||||
"node_type": "local",
|
||||
"output": res["text"],
|
||||
"used_tools": res.get("used_tools"),
|
||||
}
|
||||
|
||||
if ntype == "external":
|
||||
from app.api.chat_agents import _call_external_agent
|
||||
|
||||
agents = _resolve_market_agents(db, [node["agent_id"]], user_id)
|
||||
if not agents:
|
||||
raise OrchestrationError(f"external 节点 {nid} 引用的 agent_id={node['agent_id']} 不存在")
|
||||
ni = _assemble_node_input(query, upstream, is_entry)
|
||||
answer, _ = await _call_external_agent(
|
||||
db, agents[0], query=ni, user_id=user_id, ext_conversation_id=None, history=[]
|
||||
)
|
||||
return {"ok": True, "node_id": nid, "node_type": "external", "output": answer or ""}
|
||||
|
||||
raise OrchestrationError(f"未知节点类型 {ntype}")
|
||||
ctx = NodeContext(
|
||||
db=db,
|
||||
node=node,
|
||||
graph=graph,
|
||||
blackboard=blackboard,
|
||||
query=query,
|
||||
upstream=upstream,
|
||||
is_entry=is_entry,
|
||||
user_id=user_id or "",
|
||||
conv_id=conv_id,
|
||||
trace_id=str(trace_id or ""),
|
||||
tenant_id=str(tenant_id or ""),
|
||||
claw_type=claw_type or "",
|
||||
snapshot=snapshot,
|
||||
on_event=on_event,
|
||||
counter=counter or StepCounter(),
|
||||
)
|
||||
try:
|
||||
result = await impl.run(ctx)
|
||||
except NodeError as e:
|
||||
# 归一为 OrchestrationError,run_graph 的 on_error=halt|continue 语义不变
|
||||
raise OrchestrationError(str(e)) from e
|
||||
return result.to_blackboard()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 入口
|
||||
# ---------------------------------------------------------------------------
|
||||
async def run_graph(graph, query, db, user_id, conv_id, trace_id=None):
|
||||
async def _emit(on_event, event) -> None:
|
||||
"""图级事件发射(失败安全 —— 观测不得拖垮编排)。"""
|
||||
if on_event is None:
|
||||
return
|
||||
try:
|
||||
res = on_event(event)
|
||||
if asyncio.iscoroutine(res) or isinstance(res, asyncio.Future):
|
||||
await res
|
||||
except Exception as e:
|
||||
logger.debug("[orchestrator] emit 失败(忽略): %s", e)
|
||||
|
||||
|
||||
async def run_graph(
|
||||
graph,
|
||||
query,
|
||||
db,
|
||||
user_id,
|
||||
conv_id,
|
||||
trace_id=None,
|
||||
*,
|
||||
on_event=None,
|
||||
claw_type: str = "",
|
||||
tenant_id: str = "",
|
||||
snapshot=None,
|
||||
):
|
||||
"""执行编排图,返回 (answer: str, trace: dict)。
|
||||
|
||||
- 拓扑分层后逐层 asyncio.gather 并行执行。
|
||||
- 节点异常按 on_error=halt|continue 处理(默认 continue,失败时下游拿到错误作为输入)。
|
||||
- on_event 非空时,图级与节点级过程以统一 EngineEvent 推送:
|
||||
Web Claw 传 SseAdapter(逐帧渲染),IM Claw 传 BufferAdapter(整条装配)。
|
||||
两端共用同一份事件流,IM 侧无需任何编排专属逻辑。
|
||||
"""
|
||||
validate_graph(graph)
|
||||
layers = topological_layers(graph)
|
||||
blackboard: dict = {}
|
||||
trace = {"nodes": [], "trace_id": trace_id, "layers": layers}
|
||||
|
||||
claw = normalize_claw(claw_type)
|
||||
counter = StepCounter()
|
||||
node_count = len(graph.get("nodes") or [])
|
||||
|
||||
await _emit(
|
||||
on_event,
|
||||
make_step(
|
||||
STEP_THINKING,
|
||||
text=f"开始编排执行:{node_count} 个节点 / {len(layers)} 层",
|
||||
trace_id=str(trace_id or ""),
|
||||
tenant_id=str(tenant_id or ""),
|
||||
claw=claw,
|
||||
index=counter.next(),
|
||||
),
|
||||
)
|
||||
|
||||
for layer in layers:
|
||||
tasks = [
|
||||
_run_node(_node_by_id(graph, nid), graph, blackboard, query, user_id, conv_id, trace_id)
|
||||
_run_node(
|
||||
_node_by_id(graph, nid),
|
||||
graph,
|
||||
blackboard,
|
||||
query,
|
||||
user_id,
|
||||
conv_id,
|
||||
trace_id,
|
||||
on_event=on_event,
|
||||
claw_type=claw_type,
|
||||
tenant_id=tenant_id,
|
||||
counter=counter,
|
||||
snapshot=snapshot,
|
||||
)
|
||||
for nid in layer
|
||||
]
|
||||
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||
@@ -392,4 +487,17 @@ async def run_graph(graph, query, db, user_id, conv_id, trace_id=None):
|
||||
else:
|
||||
answer = f"⚠️ 编排输出节点 {output_node} 执行失败: {final.get('error', '未知错误')}"
|
||||
trace["output_node"] = output_node
|
||||
|
||||
ok_cnt = sum(1 for n in trace["nodes"] if n.get("ok"))
|
||||
await _emit(
|
||||
on_event,
|
||||
make_step(
|
||||
STEP_THINKING,
|
||||
text=f"编排执行完成:{ok_cnt}/{len(trace['nodes'])} 个节点成功,输出节点 {output_node}",
|
||||
trace_id=str(trace_id or ""),
|
||||
tenant_id=str(tenant_id or ""),
|
||||
claw=claw,
|
||||
index=counter.next(),
|
||||
),
|
||||
)
|
||||
return answer, trace
|
||||
@@ -72,10 +72,38 @@ _REVERSE_CONFIG = {
|
||||
"agent_endpoint", "api_key", "strategy", "max_tokens",
|
||||
}
|
||||
|
||||
VALID_NODE_TYPES = {"llm", "local", "external", "reduce"}
|
||||
VALID_ON_ERROR = {"halt", "continue"}
|
||||
VALID_REDUCE_STRATEGY = {"concat", "first", "last"}
|
||||
|
||||
# #874 新节点(skill/mcp/loop)的身份与配置字段:必须留在节点顶层双向透传。
|
||||
# 若任其落进 config 子对象,反向转换时 _CONFIG_MAP 取不回来 —— YAML 存一次就丢配置。
|
||||
_NODE_FIELDS = (
|
||||
"label", "skill_id", "func_name", "tool", "server_id", "args",
|
||||
"body", "items", "split", "max_iterations", "concurrency", "join",
|
||||
)
|
||||
|
||||
|
||||
def valid_node_types() -> set:
|
||||
"""合法节点类型 —— 唯一真相源是 NodeRegistry(#874)。
|
||||
|
||||
改造前这里硬编码了一份 {llm, local, external, reduce},与 orchestrator 里的
|
||||
元组各写各的:注册新节点必须改两处,漏一处就出现「画布能跑、YAML 报非法」。
|
||||
现在统一读注册表;注册表不可用时退回内建四型,保证校验器永不因导入失败而瘫痪。
|
||||
"""
|
||||
try:
|
||||
from app.core.node_registry import registry
|
||||
|
||||
types = set(registry.node_types())
|
||||
if types:
|
||||
return types
|
||||
except Exception: # pragma: no cover - 仅在注册表不可用时兜底
|
||||
pass
|
||||
return {"llm", "local", "external", "reduce"}
|
||||
|
||||
|
||||
# 兼容导出:历史调用方 `from ... import VALID_NODE_TYPES` 仍可用(import 期快照)
|
||||
VALID_NODE_TYPES = valid_node_types()
|
||||
|
||||
|
||||
# ── validation ──────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -112,18 +140,29 @@ def validate_yaml(data: Any) -> dict:
|
||||
errors.append(f"节点 id 重复: {nid}")
|
||||
node_ids.add(nid)
|
||||
|
||||
allowed = valid_node_types()
|
||||
ntype = node.get("type")
|
||||
if ntype not in VALID_NODE_TYPES:
|
||||
errors.append(f"节点 {nid} 类型非法: {ntype} (允许 {', '.join(sorted(VALID_NODE_TYPES))})")
|
||||
if ntype not in allowed:
|
||||
errors.append(f"节点 {nid} 类型非法: {ntype} (允许 {', '.join(sorted(allowed))})")
|
||||
|
||||
on_error = node.get("on_error", "continue")
|
||||
if on_error and on_error not in VALID_ON_ERROR:
|
||||
errors.append(f"节点 {nid} on_error 非法: {on_error}")
|
||||
|
||||
# type-specific checks
|
||||
if ntype in ("local", "external"):
|
||||
if ntype in ("local", "external", "agent"):
|
||||
if not node.get("agent_id"):
|
||||
errors.append(f"节点 {nid} 类型 {ntype} 必须指定 agent_id")
|
||||
if ntype == "skill" and not (node.get("skill_id") or node.get("func_name")):
|
||||
errors.append(f"节点 {nid} 类型 skill 必须指定 skill_id 或 func_name")
|
||||
if ntype == "mcp" and not node.get("tool"):
|
||||
errors.append(f"节点 {nid} 类型 mcp 必须指定 tool")
|
||||
if ntype == "loop":
|
||||
body = node.get("body")
|
||||
if not isinstance(body, dict) or not body.get("type"):
|
||||
errors.append(f"节点 {nid} 类型 loop 必须提供含 type 的 body")
|
||||
elif body["type"] not in allowed or body["type"] == "loop":
|
||||
errors.append(f"节点 {nid} 的 body 类型非法: {body.get('type')}")
|
||||
|
||||
# config validation
|
||||
cfg = node.get("config", {})
|
||||
@@ -205,10 +244,15 @@ def yaml_to_orchestration(data: dict) -> dict:
|
||||
"on_error": raw_node.get("on_error", "continue"),
|
||||
}
|
||||
|
||||
# agent_id for local/external
|
||||
# agent_id for local/external/agent
|
||||
if raw_node.get("agent_id"):
|
||||
node["agent_id"] = raw_node["agent_id"]
|
||||
|
||||
# #874 skill/mcp/loop 节点字段原样透传
|
||||
for _f in _NODE_FIELDS:
|
||||
if raw_node.get(_f) is not None:
|
||||
node[_f] = raw_node[_f]
|
||||
|
||||
# flatten config into top-level keys
|
||||
config = raw_node.get("config", {})
|
||||
if isinstance(config, dict):
|
||||
@@ -218,7 +262,8 @@ def yaml_to_orchestration(data: dict) -> dict:
|
||||
|
||||
# also carry over any extra keys from raw_node (preserve flexibility)
|
||||
for k, v in raw_node.items():
|
||||
if k not in ("id", "type", "config", "description", "on_error", "agent_id"):
|
||||
if k not in ("id", "type", "config", "description", "on_error", "agent_id") \
|
||||
and k not in _NODE_FIELDS:
|
||||
if k in ("skill_ids", "knowledge_base_ids", "model_id", "system_prompt",
|
||||
"agent_endpoint", "api_key", "strategy", "max_tokens"):
|
||||
node[k] = v
|
||||
@@ -262,6 +307,9 @@ def orchestration_to_yaml(data: dict) -> dict:
|
||||
node["on_error"] = json_node["on_error"]
|
||||
if json_node.get("agent_id"):
|
||||
node["agent_id"] = json_node["agent_id"]
|
||||
for _f in _NODE_FIELDS:
|
||||
if json_node.get(_f) is not None:
|
||||
node[_f] = json_node[_f]
|
||||
|
||||
# nest known config keys into config sub-object
|
||||
config: dict = {}
|
||||
@@ -272,7 +320,7 @@ def orchestration_to_yaml(data: dict) -> dict:
|
||||
# also preserve any extra keys not in the map
|
||||
for k, v in json_node.items():
|
||||
if k not in ("id", "type", "description", "on_error", "agent_id") \
|
||||
and k not in _REVERSE_CONFIG:
|
||||
and k not in _NODE_FIELDS and k not in _REVERSE_CONFIG:
|
||||
if v is not None:
|
||||
config[k] = v
|
||||
|
||||
|
||||
Reference in new issue
Block a user