会议模式:端口化连线、真假分支、变量节点、结果历史独立界面
用户反馈的四条。
【1. 连线改成端口】
以前是"节点上一个「连线」按钮,真假由程序猜"(第一条连 true、第二条连 false)——
用户根本不知道自己在连哪条,也没法只连一条。
现在:
- 每个节点左侧一个**输入端口**、右侧一个**输出端口**
- **判断节点右侧两个输出端口:上真(绿实线)下假(红虚线)**
- 点输出端口 → 点输入端口,所见即所得
- 输入端口的连线数写在卡片上("← 2 路")—— 智能体的输入本来就能接多路,
多路按顺序拼成一段文本,但以前界面上看不出来接了几路
- 真/假除了颜色还写了字:只靠红绿对色弱用户不友好
★ 所有点击(端口连线、开配置)都收进画布的**同一个**手势处理器统一命中判断,
节点上一个 clickable 都不挂 —— 多个手势识别器叠一起必然互相抢,
这个坑前面已经踩过两次("节点拖不动"就是其中一次)。
【2. 结果单开一屏 + 历史】
原来是弹窗:一次运行可能几十轮、每轮都有大段输出,弹窗那点地方根本放不下。
现在:
- 独立一屏,可以滚动着对照着看
- **历史**:内核在每次跑完时自动落一条记录(含每个节点的产出和轮次),
最多留 20 条,最新的在前。展开能看**哪个节点、第几轮**输出了什么
- 最终输出放最上面(那才是用户要的),其余按**节点在图上从左到右的顺序**排
- 每条都能单独复制
- 删会议时连带清掉它的历史(不然就是永远读不到的垃圾)
【3. 变量节点】
新增两种节点:
- **写变量**:把上游内容(或写死的值)存进一个变量
- **读变量**:把变量的当前值取出来往下传
变量是**整场会议共享的**:任何节点都能读,不用连线传。
需要它是因为会议是多轮多节点的,只靠连线传的话,到分叉再汇合就乱了
(一个节点只能拿到直接上游)。
【4. 智能体可以读/写变量】
智能体节点上勾"可读变量"和"可写变量":
- 可读的:当前值拼进它的提示词
- 可写的:告诉它用 `[[SET 名字=值]]` 写回来;引擎收到后**剥掉那一行**、
写进变量表,并发一个 node_vars 事件让界面显示"它改了哪些"
- **没授权的写不动**:那一行会原样留在正文里 ——
让用户看见"它想写但没权限",比默默失败好查得多
权限要显式勾选,不然模型会去猜"我能不能改这个",行为不可预测。
【一个真正的设计问题,被测试逼出来的】
`[[SET]]` 的解析我一开始放在 HTTP 层(app.py / index.mjs 的 runAgentFn 里),
结果**引擎测试全绿、一上 HTTP 就漏出 [[SET]] 那行** —— 因为引擎测试用的是
自己提供的假 runAgentFn,压根走不到那段代码。
这是节点语义("这个智能体能写哪些变量"是节点配置),应该归引擎。
挪进 run_meeting 之后,引擎测试立刻覆盖到了,而且换个调用方也不会忘。
【验证】
- verify-meeting.mjs:新增 5 个变量用例(写→读、留空用上游、读没设过的变量、
[[SET]] 被收走且不漏进正文、没授权的写不动),
**Node 与 Python 16/16 逐用例一致**,总 40/40
- verify-meeting-http.mjs:13/13 ×2(新增"节点类型里有写变量/读变量"断言)
- 模拟器实测:端口、判断节点的真假两色连线、输入端口的"← N 路"、×2 轮次标记、
写/读变量节点,全部正常显示
- 全部回归:Markdown 19/19、语音 41/41 ×2、会议引擎 40/40、会议 HTTP 13/13 ×2
This commit is contained in:
@@ -32,6 +32,7 @@ end 最终输出:汇总上游内容作为结果。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
@@ -41,7 +42,7 @@ import paths
|
||||
|
||||
_lock = threading.Lock()
|
||||
|
||||
NODE_TYPES = ('start', 'agent', 'judge', 'end')
|
||||
NODE_TYPES = ('start', 'agent', 'judge', 'end', 'var', 'readvar')
|
||||
|
||||
# 判断节点支持的条件。**结构化而不是表达式**,理由见文件头。
|
||||
JUDGE_KINDS = (
|
||||
@@ -54,6 +55,73 @@ JUDGE_KINDS = (
|
||||
('ai', '让模型判断'),
|
||||
)
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# 变量
|
||||
# --------------------------------------------------------------------------- #
|
||||
#
|
||||
# 为什么需要:会议是"多轮、多节点"的,前面的结论要带到后面去。
|
||||
# 只靠连线传的话,到分叉再汇合就乱了(一个节点只能拿到直接上游)。
|
||||
# 变量是**全局的**,任何节点都能读,所以要"记住一件事供后面用"时用它。
|
||||
#
|
||||
# 用 [[SET 名字=值]] 这种显式标记让智能体写变量,而不是让它自由发挥:
|
||||
# 自由发挥没法可靠解析,最后就是"有时能改有时改不了"这种最难查的问题。
|
||||
|
||||
# 整行匹配。^...$ 保证不会误伤正文里出现的方括号。
|
||||
SET_RE = re.compile(r'^[ \t]*\[\[SET[ \t]+([^\]=\r\n]+?)[ \t]*=[ \t]*(.*?)\]\][ \t]*$', re.MULTILINE)
|
||||
|
||||
|
||||
def apply_sets(text: str, variables: dict, allowed) -> tuple:
|
||||
"""
|
||||
从模型输出里抽出 [[SET 名字=值]],写进变量表,并把这些行从正文里去掉。
|
||||
|
||||
返回 (剥干净的正文, {改动的变量})。
|
||||
|
||||
allowed 是"这个节点有权限写哪些变量"。没授权的**原样留在正文里**,
|
||||
而不是悄悄丢掉 —— 用户能看见"它想写但没权限",比默默失败好查得多。
|
||||
"""
|
||||
changed = {}
|
||||
allow = set(allowed or [])
|
||||
|
||||
def repl(match):
|
||||
name = match.group(1).strip()
|
||||
value = match.group(2).strip()
|
||||
if allow and name not in allow:
|
||||
return match.group(0)
|
||||
variables[name] = value
|
||||
changed[name] = value
|
||||
return ''
|
||||
|
||||
cleaned = SET_RE.sub(repl, text or '')
|
||||
# 去掉被 SET 行留出来的多余空行
|
||||
cleaned = re.sub(r'\n{3,}', '\n\n', cleaned).strip()
|
||||
return cleaned, changed
|
||||
|
||||
|
||||
def variables_prompt(variables: dict, read_names, write_names) -> str:
|
||||
"""
|
||||
给智能体拼一段"你能看到哪些变量、能改哪些"的说明。
|
||||
|
||||
只读的不列写法说明,只写的不列当前值 —— 权限边界写清楚,
|
||||
模型才不会去猜"我能不能改这个"。
|
||||
"""
|
||||
reads = [n for n in (read_names or []) if n]
|
||||
writes = [n for n in (write_names or []) if n]
|
||||
if not reads and not writes:
|
||||
return ''
|
||||
parts = []
|
||||
if reads:
|
||||
lines = [f'- {n} = {variables.get(n, "(还没设过)")}' for n in reads]
|
||||
parts.append('【你可以看到的变量】\n' + '\n'.join(lines))
|
||||
if writes:
|
||||
parts.append(
|
||||
'【你可以修改的变量】' + '、'.join(writes) + '\n'
|
||||
'要修改变量时,在回答的**最后**单独起一行写:\n'
|
||||
'[[SET 变量名=新值]]\n'
|
||||
'这一行不会显示给用户,会被系统取走并写入变量。'
|
||||
'不需要修改就不要写这一行。'
|
||||
)
|
||||
return '\n\n'.join(parts)
|
||||
|
||||
|
||||
def _ts36() -> str:
|
||||
num = int(datetime.now(timezone.utc).timestamp() * 1000)
|
||||
@@ -189,6 +257,69 @@ def delete_meeting(meeting_id: str) -> bool:
|
||||
if len(left) == len(items):
|
||||
return False
|
||||
_write(left)
|
||||
# 会议删了,它的运行历史也一并清掉 —— 留着就是永远读不到的垃圾
|
||||
runs = _read_runs()
|
||||
runs.pop(meeting_id, None)
|
||||
_write_runs(runs)
|
||||
return True
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# 运行历史
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
# 每场会议最多留多少条历史。会议可能是无限循环、一次跑几十轮,
|
||||
# 不设上限的话这个文件会一直涨。
|
||||
RUN_HISTORY_LIMIT = 20
|
||||
|
||||
|
||||
def _runs_file() -> str:
|
||||
return os.path.join(paths.get().data_dir, 'meeting-runs.json')
|
||||
|
||||
|
||||
def _read_runs() -> dict:
|
||||
file_path = _runs_file()
|
||||
if not os.path.isfile(file_path):
|
||||
return {}
|
||||
try:
|
||||
with open(file_path, encoding='utf-8') as fh:
|
||||
data = json.load(fh)
|
||||
except (OSError, ValueError) as exc:
|
||||
print(f'[meeting] meeting-runs.json 读不了:{exc}', file=sys.stderr)
|
||||
return {}
|
||||
items = data.get('runs') if isinstance(data, dict) else None
|
||||
return items if isinstance(items, dict) else {}
|
||||
|
||||
|
||||
def _write_runs(runs: dict) -> None:
|
||||
os.makedirs(paths.get().data_dir, exist_ok=True)
|
||||
with open(_runs_file(), 'w', encoding='utf-8') as fh:
|
||||
json.dump({'runs': runs}, fh, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def list_runs(meeting_id: str) -> list:
|
||||
"""某场会议的历史运行记录,**最新的在前**"""
|
||||
return list(reversed(_read_runs().get(meeting_id) or []))
|
||||
|
||||
|
||||
def append_run(meeting_id: str, record: dict) -> dict:
|
||||
with _lock:
|
||||
runs = _read_runs()
|
||||
items = runs.get(meeting_id) or []
|
||||
items.append(record)
|
||||
# 只留最近 RUN_HISTORY_LIMIT 条
|
||||
runs[meeting_id] = items[-RUN_HISTORY_LIMIT:]
|
||||
_write_runs(runs)
|
||||
return record
|
||||
|
||||
|
||||
def clear_runs(meeting_id: str) -> bool:
|
||||
with _lock:
|
||||
runs = _read_runs()
|
||||
if meeting_id not in runs:
|
||||
return False
|
||||
runs.pop(meeting_id, None)
|
||||
_write_runs(runs)
|
||||
return True
|
||||
|
||||
|
||||
@@ -336,6 +467,9 @@ def run_meeting(*, meeting: dict, settings: dict, user_input: str,
|
||||
runs = {}
|
||||
steps = 0
|
||||
|
||||
# 变量表:整场会议共享。var / readvar 节点和授权过的智能体都能读写它。
|
||||
variables = {}
|
||||
|
||||
def limit_of(node: dict) -> int:
|
||||
"""
|
||||
节点的执行次数上限。**0 表示无限**。
|
||||
@@ -388,8 +522,9 @@ def run_meeting(*, meeting: dict, settings: dict, user_input: str,
|
||||
run_index = runs.get(node_id, 0) + 1
|
||||
runs[node_id] = run_index
|
||||
node_type = node['type']
|
||||
title = node.get('title') or {'start': '初始输入', 'agent': '智能体',
|
||||
'judge': '判断', 'end': '最终输出'}.get(node_type, node_type)
|
||||
title = node.get('title') or {'start': '初始输入', 'agent': '智能体', 'judge': '判断',
|
||||
'end': '最终输出', 'var': '写变量',
|
||||
'readvar': '读变量'}.get(node_type, node_type)
|
||||
emit({'type': 'node_start', 'nodeId': node_id, 'nodeType': node_type,
|
||||
'title': title, 'run': run_index, 'maxRuns': limit_of(node)})
|
||||
|
||||
@@ -406,15 +541,49 @@ def run_meeting(*, meeting: dict, settings: dict, user_input: str,
|
||||
next_ids = [e['to'] for e in _outgoing(edges, node_id)]
|
||||
|
||||
elif node_type == 'agent':
|
||||
out = run_agent_fn(node, incoming, settings, emit, node_id)
|
||||
outputs[node_id] = out
|
||||
emit({'type': 'node_done', 'nodeId': node_id, 'output': out, 'run': run_index})
|
||||
raw = run_agent_fn(node, incoming, settings, emit, node_id, variables)
|
||||
# ★ 把 [[SET 名字=值]] 抽出来写进变量,正文里不留那些行。
|
||||
#
|
||||
# 这件事放在**引擎**里而不是"调用引擎的那一层":
|
||||
# 它是节点语义的一部分("这个智能体能写哪些变量"是节点配置),
|
||||
# 放在外面的话 —— 换个调用方就忘了做,而且引擎自己的测试
|
||||
# 压根覆盖不到(真发生过:引擎测试全绿,一上 HTTP 就漏出 [[SET]])。
|
||||
cleaned, changed = apply_sets(raw, variables, node.get('writeVars'))
|
||||
outputs[node_id] = cleaned
|
||||
emit({'type': 'node_done', 'nodeId': node_id, 'output': cleaned, 'run': run_index})
|
||||
if changed:
|
||||
# 改了哪些变量要让用户看见,不然就是"背地里改了我的东西"
|
||||
emit({'type': 'node_vars', 'nodeId': node_id, 'vars': changed,
|
||||
'note': '、'.join(f'{k} = {str(v)[:30]}' for k, v in changed.items())})
|
||||
next_ids = [e['to'] for e in _outgoing(edges, node_id)]
|
||||
|
||||
elif node_type == 'var':
|
||||
# 写变量节点:有上游就用上游内容当值,否则用节点里写死的
|
||||
name = str(node.get('varName') or '').strip()
|
||||
if not name:
|
||||
raise ValueError('变量节点没填变量名')
|
||||
value = (node.get('varValue') or '').strip() or incoming
|
||||
variables[name] = value
|
||||
outputs[node_id] = value
|
||||
emit({'type': 'node_done', 'nodeId': node_id, 'output': value, 'run': run_index,
|
||||
'note': f'变量 {name} = {value[:40]}'})
|
||||
next_ids = [e['to'] for e in _outgoing(edges, node_id)]
|
||||
|
||||
elif node_type == 'readvar':
|
||||
# 读变量节点:把变量的当前值当成输出往下传
|
||||
name = str(node.get('varName') or '').strip()
|
||||
if not name:
|
||||
raise ValueError('读变量节点没填变量名')
|
||||
value = variables.get(name, '')
|
||||
outputs[node_id] = value
|
||||
emit({'type': 'node_done', 'nodeId': node_id, 'output': value, 'run': run_index,
|
||||
'note': f'读到 {name}' + ('' if value else '(还没设过,是空的)')})
|
||||
next_ids = [e['to'] for e in _outgoing(edges, node_id)]
|
||||
|
||||
elif node_type == 'judge':
|
||||
kind = str(node.get('judgeKind') or 'not_empty')
|
||||
if kind == 'ai':
|
||||
verdict = run_agent_fn({**node, '_judgeOnly': True}, incoming, settings, emit, node_id)
|
||||
verdict = run_agent_fn({**node, '_judgeOnly': True}, incoming, settings, emit, node_id, variables)
|
||||
passed = 'YES' in verdict.upper()[:40] or '是' in verdict[:20]
|
||||
else:
|
||||
passed = _judge_rule(node, incoming)
|
||||
|
||||
Reference in New Issue
Block a user