会议模式:自定义节点做完(用户的 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:
l
2026-10-12 11:14:49 +08:00
parent 001d16029b
commit e8749008ac
7 changed files with 504 additions and 71 deletions

View File

@@ -166,6 +166,30 @@ def _trace_tts(text: str, outcome: str, total_bytes: int, chunks: int, seconds:
pass
def _custom_defs():
"""
加载用户的自定义节点。
★ 用 try 包住:加载器自己出问题(比如数据目录不可写)也不能让 meta 挂掉 ——
meta 挂了整个会议界面就是白屏,用户连"哪里出错了"都看不到。
坏掉的节点以 error 字段返回,界面上照样列出来、显示原因。
"""
try:
import custom_nodes
return custom_nodes.list_defs()
except Exception as exc: # noqa: BLE001
return [{'key': '', 'label': '(自定义节点加载失败)',
'error': f'{type(exc).__name__}: {exc}'}]
def _custom_dir():
try:
import custom_nodes
return custom_nodes.nodes_dir()
except Exception: # noqa: BLE001
return ''
class SseChannel:
"""
一条 SSE 连接。
@@ -683,6 +707,24 @@ class KernelHandler(BaseHTTPRequestHandler):
if pathname == '/api/meeting/meta' and method == 'GET':
# 编辑器要用:节点类型、判断条件、可用智能体
self.send_json(200, {
# ★ 用户用 Python 拓展出来的节点也在这里列出来。
# 界面拿到 inputs / outputs 就能**自动画出引脚** ——
# 不用在 Kotlin 里再写一份,也就不会再出现
# "引擎有了、界面没有"那种事。
'customNodes': [
{
'key': d.get('key'),
'label': d.get('label'),
'desc': d.get('desc') or '',
'file': os.path.basename(d.get('file') or ''),
'inputs': d.get('inputs') or [],
'outputs': d.get('outputs') or [],
'hasExecIn': bool(d.get('hasExecIn')),
'error': d.get('error') or '',
}
for d in _custom_defs()
],
'customDir': _custom_dir(),
'nodeTypes': [
{'key': 'start', 'label': '开始事件', 'desc': '整张图从这里开始(exec 起点)'},
{'key': 'agent', 'label': '智能体', 'desc': '一个智能体处理一轮'},