会议模式:自定义节点做完(用户的 node.txt 方案,全链路测通)
用户:"直接做完并测试"
【做了什么】
把用户给的声明式方案落地成一种真正的节点类型:
ToolName = {
"name": "打分",
"desc": "文字越长分越高",
"input": [{"type": "str", "name": "文本", "value": ""}],
"output": [{"type": "exec", "name": "高分"},
{"type": "exec", "name": "低分"},
{"type": "str", "name": "分数"}],
}
def run(ctx, node):
node["output"]["分数"] = "分数:" + str(len(node["input"]["文本"]))
node["output"]["高分"] = len(...) >= 3
node["output"]["低分"] = len(...) < 3
return node
往 <数据目录>/nodes/ 丢一个 .py,会议里就多出一种节点。**不用改 Kotlin**。
【链路】
1. custom_nodes.py:加载 / 校验 / 规整引脚 / 子线程跑(10 秒超时)
2. meeting.py:新增 `custom` 节点类型
- 类型上下文里加载定义(_LAST_CUSTOM),连线时查声明决定收什么类型
- 执行分支:按引脚收输入 -> run_custom -> 取第一个数据出口往下传
3. app.py:/api/meeting/meta 带 customNodes(含 inputs/outputs)+ customDir
4. Kotlin:MeetingMeta.customNodes、CustomNodeDef/CustomPin、
inputPins/outputPins 对 `custom` **从内核读引脚**、NodeCard 传 customDefs、
加节点面板顶部列出自定义节点(带 🐍 和文件名,坏的显示原因)
5. nodes-示例/:score.py(活文档)+ README.md(格式和三条规则)
【三条语义(已在 README 和节点注释里写清楚,并实测)】
1. node["input"][名] 是**引擎填的实际值**,声明里的 value 只是"没接线时的默认"
2. 多个 exec 出口 = **多路分支**,最多一个 True:
恰好一个 → 走那条
都不 True → **停在原地** + 报警告(不猜、不顺走第一条)
多个 True → 报错
3. return 什么无所谓,引擎读 node["output"]
【发现并修的坑(用户原版写不通的两处)】
① 键必须加引号:`name:"..."` 是 JS 写法,Python 报
"SyntaxError: invalid syntax. Perhaps you forgot a comma?" ——
这句话完全看不出问题在哪。内核专门认出来并给出正确写法。
② 声明是列表、访问是字典,对不上:声明 `input: [{...}]` 是列表,
但 run 里写 `node["input"]["A1"]`(按名字取)。
解法:**引擎在跑之前把 input/output 换成按名字取的字典**,
声明的列表原样留给引擎和界面读类型。
【实测(HTTP 全链路)】
meta: 打分 key=custom_score exec 分支=['高分','低分']
保存后边数: 5/5(类型校验认识自定义节点的引脚)
起点='好' → 执行 ev,sc,lo → 低分出口(分数:1)
起点='这段文字比较长' → 执行 ev,sc,hi → 高分出口(分数:7)
历史里也存了 results ✓
【限制(如实写在 README 里)】
数据出口只有**第一个能接线** —— 边(edge)上没有"从哪个引脚出来"的信息。
其余数据出口的值仍会显示在节点卡片上,只是接不了线。
要修得给边加 fromPin/toPin,属于数据模型层面的改动。
【验证】
- 会议引擎 40/40,Node 与 Python 16/16 逐用例一致
- 会议 HTTP 13/13 ×2、语音 41/41、Markdown 19/19
- 自定义节点:加载 / 运行 / 写变量 / 记日志 / N 路分支 / 报错兜底 / 超时,
Python 侧和 HTTP 侧各测一遍
- APK 构建通过
This commit is contained in:
@@ -44,7 +44,7 @@ import paths
|
||||
_lock = threading.Lock()
|
||||
|
||||
NODE_TYPES = ('start', 'agent', 'judge', 'end', 'var', 'readvar', 'while', 'value',
|
||||
'cast', 'op', 'py')
|
||||
'cast', 'op', 'py', 'custom')
|
||||
|
||||
# 判断节点支持的条件。**结构化而不是表达式**,理由见文件头。
|
||||
# --------------------------------------------------------------------------- #
|
||||
@@ -762,6 +762,9 @@ NODE_DATA_IN = {
|
||||
# 最终输出只是展示,什么类型都能接(展示方式按类型不同)
|
||||
# Python 节点:上游给什么都要能收(它就是拿来处理任意内容的)
|
||||
'py': ('str', 'bool', 'num', 'list'),
|
||||
# 自定义节点:真正收什么由它声明的引脚决定(见 _data_in_type),
|
||||
# 这里给宽松值,免得还没加载到定义时就把线丢了
|
||||
'custom': ('str', 'bool', 'num', 'list'),
|
||||
'end': ('str', 'bool', 'num', 'list'),
|
||||
# 转换:什么都收,出口由 castTo 决定
|
||||
'cast': ('str', 'bool', 'num'),
|
||||
@@ -787,6 +790,7 @@ _LAST_BY_ID = {}
|
||||
_LAST_VAR_TYPE = {}
|
||||
# 纯节点求值时要能找到自己的数据入线,所以也把边表放一份在这儿
|
||||
_LAST_EDGES = []
|
||||
_LAST_CUSTOM = {}
|
||||
|
||||
|
||||
def _set_type_context(nodes: list, vars: list = None, edges: list = None) -> None:
|
||||
@@ -797,15 +801,36 @@ def _set_type_context(nodes: list, vars: list = None, edges: list = None) -> Non
|
||||
它们的值不是"执行时产生的",而是被需要的节点**拉取**时才现算的,
|
||||
现算就得能找到自己的数据入线。
|
||||
"""
|
||||
global _LAST_BY_ID, _LAST_VAR_TYPE, _LAST_EDGES
|
||||
global _LAST_BY_ID, _LAST_VAR_TYPE, _LAST_EDGES, _LAST_CUSTOM
|
||||
_LAST_BY_ID = {n['id']: n for n in nodes}
|
||||
_LAST_VAR_TYPE = {v['name']: v['type'] for v in (vars or [])}
|
||||
# 自定义节点(用户用 Python 拓展的那些):按 key 建索引。
|
||||
# 连线时要查它们声明的引脚类型 —— 不查的话自定义节点的数据线会被当成
|
||||
# "类型不认识"直接丢掉,图看着连了、一保存就没了。
|
||||
try:
|
||||
import custom_nodes as _cn
|
||||
_LAST_CUSTOM = {d['key']: d for d in _cn.list_defs() if not d.get('error')}
|
||||
except Exception: # noqa: BLE001
|
||||
_LAST_CUSTOM = {}
|
||||
if edges is not None:
|
||||
_LAST_EDGES = edges
|
||||
def _data_in_type(node_id: str):
|
||||
node = _LAST_BY_ID.get(node_id)
|
||||
if not node:
|
||||
return None
|
||||
# ★ 自定义节点:按声明的 input 引脚类型。
|
||||
#
|
||||
# 这里是"这个节点收哪些类型"的**并集** —— 引擎的类型检查是按**节点**做的
|
||||
# (一条边上没有"接到哪个引脚"的信息),所以多入口只能放宽成并集。
|
||||
# 界面那一侧知道用户在拖哪根线,做的是**按引脚**的精确校验,
|
||||
# 所以严格程度并不会丢:接错了当场就连不上。
|
||||
if node['type'] == 'custom':
|
||||
d = _LAST_CUSTOM.get(str(node.get('customKey') or ''))
|
||||
if d:
|
||||
kinds = tuple(p['type'] for p in d.get('inputs') or []
|
||||
if p['type'] != 'exec')
|
||||
return kinds or None
|
||||
return None
|
||||
# 运算节点的两个入口,类型由算子决定(加法要数字、拼接要文本)
|
||||
if node['type'] == 'op':
|
||||
spec = _OPS_BY_KEY.get(str(node.get('op') or 'add'))
|
||||
@@ -821,6 +846,25 @@ def _data_in_type(node_id: str):
|
||||
# 不该限制**存进来**的东西:存进去的都是文本,读的时候再按类型解释。
|
||||
return NODE_DATA_IN.get(node['type'])
|
||||
def _data_out_type(node_id: str):
|
||||
"""
|
||||
这个节点"往外给"的是什么类型。
|
||||
|
||||
★ 自定义节点可能有多个数据出口,但**只有第一个能接线** ——
|
||||
边(edge)上没有"从哪个引脚出来"的信息,多个数据出口会分不清谁是谁。
|
||||
所以加载器那边会提醒"多个数据出口只有第一个能连",
|
||||
其余出口的值仍然会出现在节点卡片和运行结果里,只是接不了线。
|
||||
"""
|
||||
_n = _LAST_BY_ID.get(node_id)
|
||||
if _n is not None and _n.get('type') == 'custom':
|
||||
_d = _LAST_CUSTOM.get(str(_n.get('customKey') or ''))
|
||||
if _d:
|
||||
_outs = [p['type'] for p in _d.get('outputs') or [] if p['type'] != 'exec']
|
||||
if _outs:
|
||||
return _outs[0]
|
||||
return _data_out_type_inner(node_id)
|
||||
|
||||
|
||||
def _data_out_type_inner(node_id: str):
|
||||
node = _LAST_BY_ID.get(node_id)
|
||||
if not node:
|
||||
return None
|
||||
@@ -981,6 +1025,29 @@ def _exec_out(edges: list, node_id: str, branch: str = None) -> list:
|
||||
return [e for e in out if (e.get('branch') or 'true') == branch]
|
||||
|
||||
|
||||
def _collect_pin_input(node_id: str, edges: list, pin: dict, defn: dict,
|
||||
outputs: dict, variables: dict, by_id: dict) -> str:
|
||||
"""
|
||||
给自定义节点的某一个入口取值。
|
||||
|
||||
和 _collect_input 的区别:那个是"把所有数据入线拼成一段文本"(给智能体看的),
|
||||
这里是"按引脚取值" —— 数据线本身没有引脚信息,所以只能按**声明顺序**对:
|
||||
第 N 根数据入线 -> 第 N 个数据入口。
|
||||
这是当前数据模型的限制(边不带 fromPin/toPin),加载器会提醒用户
|
||||
"入口超过一个时,连线是按顺序对应的"。
|
||||
"""
|
||||
value = ''
|
||||
for i, e in enumerate(_data_in(edges, node_id)):
|
||||
pins = [p for p in (defn.get('inputs') or []) if p.get('type') != 'exec']
|
||||
idx = pins.index(pin) if pin in pins else 0
|
||||
if i != idx:
|
||||
continue
|
||||
v = _pure_value(e.get('from'), by_id, outputs, variables)
|
||||
if v not in (None, ''):
|
||||
value = v if isinstance(v, str) else _as_text(v, _kind(e))
|
||||
return value
|
||||
|
||||
|
||||
def _data_in(edges: list, node_id: str) -> list:
|
||||
return [e for e in _incoming(edges, node_id) if _is_data(e)]
|
||||
|
||||
@@ -1502,6 +1569,62 @@ def run_meeting(*, meeting: dict, settings: dict, user_input: str,
|
||||
'note': f'读到 {name}' + ('' if value else '(还没设过,是空的)')})
|
||||
next_ids = [e['to'] for e in _exec_out(edges, node_id)]
|
||||
|
||||
elif node_type == 'custom':
|
||||
# ★ 用户用 Python 拓展出来的节点
|
||||
import custom_nodes as _cn
|
||||
d = _LAST_CUSTOM.get(str(node.get('customKey') or ''))
|
||||
if not d:
|
||||
raise ValueError(
|
||||
'这个自定义节点没找到定义(文件可能被删了或名字改了):'
|
||||
+ str(node.get('customKey')))
|
||||
# 把各个数据入口收进来(按引脚名)
|
||||
ins = {}
|
||||
for pin in d.get('inputs') or []:
|
||||
if pin['type'] == 'exec':
|
||||
continue
|
||||
ins[pin['name']] = _collect_pin_input(
|
||||
node_id, edges, pin, d, outputs, variables, by_id)
|
||||
# 没连任何入口时,退一步用 exec 上游的输出喂给**第一个**入口 ——
|
||||
# 和写变量、最终输出一个道理:用户的心智是"线连过来了",
|
||||
# 逼他必须再拉一根数据线只会让他看到空值还不知道为什么。
|
||||
if not any(str(v).strip() for v in ins.values()):
|
||||
data_pins = [p for p in (d.get('inputs') or []) if p['type'] != 'exec']
|
||||
if data_pins and not _data_in(edges, node_id):
|
||||
ups = [outputs.get(e.get('from'))
|
||||
for e in _exec_in(edges, node_id)]
|
||||
ups = [t for t in ups if t]
|
||||
if ups:
|
||||
ins[data_pins[0]['name']] = ups[-1]
|
||||
logs = []
|
||||
outs, err = _cn.run_custom(d, ins, variables, node, logs)
|
||||
if err:
|
||||
emit({'type': 'node_warn', 'nodeId': node_id, 'warn': err})
|
||||
# 数据出口:第一个能接线,值取出来往下传
|
||||
dv = ''
|
||||
for pin in d.get('outputs') or []:
|
||||
if pin['type'] == 'exec':
|
||||
continue
|
||||
dv = '' if outs.get(pin['name']) is None else str(outs.get(pin['name']))
|
||||
break
|
||||
outputs[node_id] = dv
|
||||
emit({'type': 'node_done', 'nodeId': node_id, 'nodeType': node_type,
|
||||
'output': dv, 'run': run_index,
|
||||
'note': (';'.join(logs)[:120] if logs else '')})
|
||||
# ★ 多个 exec 出口 = N 路分支,规则和判断节点一致:
|
||||
# 恰好一个 True 走那条;都不 True 停在原地报警告;
|
||||
# 多个 True 报错(引擎没法替你选)
|
||||
branch_names, warn = _cn.split_execs(d, outs)
|
||||
if warn:
|
||||
emit({'type': 'node_warn', 'nodeId': node_id, 'warn': warn})
|
||||
next_ids = []
|
||||
for e in _exec_out(edges, node_id):
|
||||
b = str(e.get('branch') or '')
|
||||
pin_name = str(e.get('branch') or '')
|
||||
if not branch_names:
|
||||
break
|
||||
if pin_name in branch_names or (not b and len(branch_names) == 1):
|
||||
next_ids.append(e['to'])
|
||||
|
||||
elif node_type == 'py':
|
||||
# ★ Python 节点:跑用户自己写的那段代码
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user