From ae2024a053927cb5a6386c22633af98af94718a6 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 10 Oct 2026 21:38:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E8=AE=AE=E6=A8=A1=E5=BC=8F=EF=BC=9A?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=8C=96=E5=BC=95=E8=84=9A=EF=BC=88exec=20?= =?UTF-8?q?=E4=B8=8D=E4=BC=A0=E6=95=B0=E6=8D=AE=EF=BC=89+=20=E5=80=BC?= =?UTF-8?q?=E8=8A=82=E7=82=B9=20+=20=E8=BF=9E=E7=BA=BF=E5=8F=AF=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户四条:端点分 bool/str/num/exec、exec 不传递数据、值可以传 str/bool/num、 连线要能删除。 【1. 类型化引脚】 引脚种类从"exec / data"细化成 **exec / str / bool / num**,和蓝图一样强类型: 连错类型直接接不上。 节点 数据入 数据出 start —— str value —— 节点上选(str / bool / num) agent str str judge str bool(判断结果可以直接喂给 while) var str ——(存进去的值不从它往下传) readvar —— str while bool —— end str —— 保存时按引脚类型校验,不匹配的边**直接丢掉**。 【2. exec 执行链不传递数据】 这一条把我之前的一个偷懒设计去掉了:原来留了个"没有数据入边时就用 exec 上游的输出"的回退 —— 那等于**让 exec 偷偷传数据**,于是"接一根 exec 线" 和"接一根数据线"效果一样,类型也没法校验。 现在数据**只**沿数据线走。代价是老会议里"一根线连到底"的图不再自动传值, 得补一根数据线 —— 但这是对的:顺序和取值本来就不该是同一件事。 【3. 值节点】 新增 `值`(纯节点,只有一个数据输出),类型可选 str / bool / num。 实测:开始事件(str) + 值节点(str) → 智能体,两个数据源都进了智能体; 而 num 类型的值节点接到 str 输入上**被正确拒绝**。 取值统一走 _pure_value():纯节点(读变量 / 值)**现取现值**, 而不是去 outputs 里找 —— 它们可能压根没被执行链走到过。 【4. 连线可删除】 蓝图里是 Alt+点引脚 断开;手机上没键盘,改成**点线选中、再点删除**: - 点中连线 → 变蓝加粗带光晕,底部出现「已选中一条连线(str 数据线)· 删除 / 取消」 - 点删除 → 连线消失并落盘 连线是曲线,不能像节点那样用矩形判,所以沿贝塞尔采样 24 段找最近的 (命中半径 14dp,手指没那么准)。 **实测:7 条连线 → 删除 → 5 条**,界面上那条线确实没了。 【验证】 - 引擎回归 40/40,Node 与 Python **16/16 逐用例一致** - 会议 HTTP 端到端 13/13 ×2 - 类型校验实测:num → str 的连接被丢掉 - 删除连线实测:7 → 5 条(模拟器截图确认) ★ 测试用例里加了 withDataEdges():老用例的边都是"一根线既排顺序又传值"的 老写法,现在要补一条并行的 str 数据线。这正是用户在界面上迁移时要做的事, 所以放在测试宿主里统一处理,比改 29 处数据干净。 两边变量用例还各补了一条之前缺的数据线(写变量要拿到上游的值必须有数据线)。 【还没做的】 - **Node 侧引擎的 while 节点**:Python 有了,Node 还没有(要把它那边的 runMeeting 也改成可递归的 walk)。两边 exec/data 和类型化都同步了,对拍一致。 - while 的配置面板说明文字、值节点的验收用例。 --- .../com/doubao/lynxclone/ui/MeetingsScreen.kt | 151 +++++++++++++++++- android/kernel/meeting.py | 147 +++++++++++++---- scripts/verify-meeting-http.mjs | 17 +- scripts/verify-meeting.mjs | 28 +++- server/src/meeting.mjs | 51 ++++-- 5 files changed, 344 insertions(+), 50 deletions(-) diff --git a/android/app/src/main/java/com/doubao/lynxclone/ui/MeetingsScreen.kt b/android/app/src/main/java/com/doubao/lynxclone/ui/MeetingsScreen.kt index 622b88d..5568e36 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/ui/MeetingsScreen.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/ui/MeetingsScreen.kt @@ -263,6 +263,8 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - // 正在连线:记"从哪条引脚拉出来的"(节点 + 引脚种类 + 分支)。 // 判断节点有真/假两条 exec 出线,只记节点 id 分不清是从哪个口拉出来的。 var linkingFrom by remember { mutableStateOf(null) } + // 点中的连线(要能删)。存的是 edges 里的下标。 + var selectedEdge by remember { mutableStateOf(null) } var running by remember { mutableStateOf(false) } var runInput by remember { mutableStateOf("") } var runError by remember { mutableStateOf("") } @@ -436,6 +438,33 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - return null } + /** 命中哪条连线。返回 edges 里的下标;离得太远就返回 null。 */ + fun edgeAt(c: Offset): Int? { + val ns = nodesSnapshot.value + val wireHit = 14.dp.toPx() // 手指没那么准,给宽一点 + var best: Int? = null + var bestD = Float.MAX_VALUE + meeting.edges.forEachIndexed { index, edge -> + val a = ns.firstOrNull { it.id == edge.from } ?: return@forEachIndexed + val b = ns.firstOrNull { it.id == edge.to } ?: return@forEachIndexed + val start = Offset( + portOut(a, edge.kind, edge.branch).x.dp.toPx(), + portOut(a, edge.kind, edge.branch).y.dp.toPx(), + ) + val end = Offset( + portIn(b, edge.kind).x.dp.toPx(), + portIn(b, edge.kind).y.dp.toPx(), + ) + val dx = maxOf(36.dp.toPx(), kotlin.math.abs(end.x - start.x) * 0.5f) + val d = distanceToWire(start, end, dx, c) + if (d < bestD) { + bestD = d + best = index + } + } + return if (bestD <= wireHit) best else null + } + awaitEachGesture { val down = awaitFirstDown(requireUnconsumed = false) var last = down.position @@ -582,9 +611,19 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - } linkingFrom = null } - // 点空白:取消连线;点节点:开配置 - portHitAtDown == null && hit != null -> configTarget = hit - portHitAtDown == null && linking != null -> linkingFrom = null + // 点空白:取消连线 / 取消选中;点节点:开配置 + portHitAtDown == null && hit != null -> { + configTarget = hit + selectedEdge = null + } + portHitAtDown == null && linking != null -> { + linkingFrom = null + selectedEdge = null + } + // ★ 什么都没点到 → 再看看是不是点在**连线上**。 + // 连线是曲线,不能像节点那样用矩形判, + // 所以沿曲线采样找最近的(见 distanceToWire)。 + else -> selectedEdge = edgeAt(c0) } } else if (hit != null) { persist() @@ -640,11 +679,20 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - moveTo(start.x, start.y) cubicTo(start.x + dx, start.y, end.x - dx, end.y, end.x, end.y) } + // 点中的那条画粗 + 套一层光晕,一眼看出"选的是这条" + val isSel = selectedEdge == meeting.edges.indexOf(edge) + if (isSel) { + drawPath( + path = path, + color = Doubao.Blue.copy(alpha = 0.30f), + style = Stroke(width = 9.dp.toPx(), cap = androidx.compose.ui.graphics.StrokeCap.Round), + ) + } drawPath( path = path, - color = color, + color = if (isSel) Doubao.Blue else color, style = Stroke( - width = 2.5.dp.toPx(), + width = if (isSel) 3.5.dp.toPx() else 2.5.dp.toPx(), cap = androidx.compose.ui.graphics.StrokeCap.Round, pathEffect = if (edge.branch == "false") { PathEffect.dashPathEffect(floatArrayOf(9f, 9f)) @@ -801,6 +849,57 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - ) } } + // ★ 点中一条连线后,这里出现「删除」。 + // 蓝图里是 Alt+点引脚 断开;手机上没键盘,改成"点线选中、再点删除"—— + // 比长按好发现,也不会误删。 + selectedEdge?.let { idx -> + val edge = meeting.edges.getOrNull(idx) + if (edge != null) { + Row( + Modifier + .fillMaxWidth() + .padding(horizontal = 12.dp) + .clip(RoundedCornerShape(10.dp)) + .background(Doubao.Blue.copy(alpha = 0.08f)) + .padding(horizontal = 10.dp, vertical = 6.dp), + verticalAlignment = Alignment.CenterVertically, + ) { + Text( + "已选中一条连线(${kindLabel(edge.kind)})", + fontSize = 12.sp, + color = doubaoTextSecondary(), + modifier = Modifier.weight(1f), + ) + Text( + "删除", + color = MaterialTheme.colorScheme.error, + fontSize = 13.sp, + fontWeight = FontWeight.Medium, + modifier = Modifier + .clip(RoundedCornerShape(8.dp)) + .clickable { + meeting = meeting.copy( + edges = meeting.edges.filterIndexed { i, _ -> i != idx }, + ) + selectedEdge = null + dirty = true + persist() + } + .padding(horizontal = 10.dp, vertical = 4.dp), + ) + Spacer(Modifier.width(4.dp)) + Text( + "取消", + color = doubaoTextSecondary(), + fontSize = 13.sp, + modifier = Modifier + .clip(RoundedCornerShape(8.dp)) + .clickable { selectedEdge = null } + .padding(horizontal = 8.dp, vertical = 4.dp), + ) + } + } + } } // 结果单开一屏(不是弹窗):一次运行可能几十轮、每轮都有大段输出, @@ -879,6 +978,40 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () - } } +/** + * 三次贝塞尔上的一点。 + * + * 连线命中判断要用:连线是曲线,不能像节点那样用矩形判。 + * 沿着曲线采一串点、找最近的,够简单也够准。 + */ +private fun bezierAt(p0: Offset, p1: Offset, p2: Offset, p3: Offset, t: Float): Offset { + val mt = 1f - t + val a = mt * mt * mt + val b = 3f * mt * mt * t + val c = 3f * mt * t * t + val d = t * t * t + return Offset( + a * p0.x + b * p1.x + c * p2.x + d * p3.x, + a * p0.y + b * p1.y + c * p2.y + d * p3.y, + ) +} + +/** 连线离某个点最近有多远(像素)。用来判断"这一下点的是不是这条线"。 */ +private fun distanceToWire( + start: Offset, end: Offset, dx: Float, target: Offset, +): Float { + val c1 = Offset(start.x + dx, start.y) + val c2 = Offset(end.x - dx, end.y) + var best = Float.MAX_VALUE + // 24 段够细了:连线上最细的弯也就几个像素 + for (i in 0..24) { + val p = bezierAt(start, c1, c2, end, i / 24f) + val d = (p - target).getDistance() + if (d < best) best = d + } + return best +} + /** * 一个引脚。照 UE 蓝图分两类: * @@ -1010,6 +1143,14 @@ private fun portOut(node: MeetingNode, kind: String, branch: String): Offset = private data class LinkFrom(val nodeId: String, val kind: String, val branch: String) /** 执行引脚画成白色三角、数据引脚画成彩色圆点 —— 蓝图就靠这个一眼分辨 */ +/** 连线种类的中文名(选中提示里用) */ +private fun kindLabel(kind: String): String = when (kind) { + "exec" -> "执行线" + "bool" -> "bool 数据线" + "num" -> "num 数据线" + else -> "str 数据线" +} + private val EXEC_COLOR = Color(0xFFE8EAED) private val EXEC_BORDER = Color(0xFF5F6368) diff --git a/android/kernel/meeting.py b/android/kernel/meeting.py index 6647fe6..962d038 100644 --- a/android/kernel/meeting.py +++ b/android/kernel/meeting.py @@ -287,6 +287,7 @@ def save_meeting(meeting_id: str, payload: dict): continue merged = {**item, **payload, 'id': meeting_id, 'updatedAt': now_iso()} merged['nodes'] = _sanitize_nodes(merged.get('nodes') or []) + _set_type_context(merged['nodes']) merged['edges'] = _sanitize_edges(merged.get('edges') or [], merged['nodes']) items[i] = merged _write(items) @@ -397,6 +398,64 @@ def _sanitize_nodes(nodes: list) -> list: return out +# --------------------------------------------------------------------------- # +# 引脚类型表(引擎侧) +# --------------------------------------------------------------------------- # +# +# 界面上的引脚定义要和这里一致,否则会出现"界面上能连、保存时被丢掉" +# 这种说不清的问题。两边都从同一个规则来:按节点类型查数据引脚的类型。 +# +# 节点 数据入 数据出 +# start —— str(用户发的那句 / 写死的文本) +# value —— 节点上选(str / bool / num) +# agent str str +# judge str bool(判断结果,可以直接喂给 while) +# var str ——(存进去的值不从它往下传) +# readvar —— str(变量的当前值) +# while bool —— +# end str —— + +NODE_DATA_IN = { + 'agent': 'str', + 'judge': 'str', + 'var': 'str', + 'while': 'bool', + 'end': 'str', +} + +NODE_DATA_OUT = { + 'start': 'str', + 'agent': 'str', + 'judge': 'bool', + 'readvar': 'str', +} + +# _sanitize_edges 只收 nodes 列表,而类型查询要按 id 查节点。 +# 用一个模块级的临时表把它传进去 —— 比给一串辅助函数都加一个参数干净。 +_LAST_BY_ID = {} + + +def _set_type_context(nodes: list) -> None: + global _LAST_BY_ID + _LAST_BY_ID = {n['id']: n for n in nodes} + + +def _data_in_type(node_id: str): + node = _LAST_BY_ID.get(node_id) + return NODE_DATA_IN.get(node['type']) if node else None + + +def _data_out_type(node_id: str): + node = _LAST_BY_ID.get(node_id) + if not node: + return None + if node['type'] == 'value': + # 值节点的类型由它自己选(str / bool / num) + raw = str(node.get('valueType') or 'str') + return raw if raw in PIN_TYPES else 'str' + return NODE_DATA_OUT.get(node['type']) + + def _sanitize_edges(edges: list, nodes: list) -> list: """ 丢掉指向不存在节点的连线,以及**接到没有那个端口的节点上的连线**。 @@ -424,15 +483,19 @@ def _sanitize_edges(edges: list, nodes: list) -> list: continue kind = _kind(e) - # ★ 从"读变量"出来的线**一律当数据线**:它是纯节点(蓝图里的 Get), - # 输出天然是数据,不该拿它当执行链的一环。 - # 让保存时自动推断,用户就不用去理解两种线的区别也能连对。 - if by_id[src]['type'] == 'readvar': - kind = DATA - # 进"写变量"的线也是数据线(它要的是值,不是执行信号 —— 虽然它也有 exec 入, - # 但用户往它身上连多半是想存这个东西) - if by_id[dst]['type'] == 'var' and str(e.get('kind') or '') == '': - kind = DATA + # 老边(没有 kind)当 exec。exec 现在只排顺序、不传数据, + # 所以老图想传值得补一根数据线 —— 这是类型化之后必然的取舍。 + + # ★ 端口类型校验:exec 只能接 exec,数据只能接**同类型**的数据。 + # 蓝图里连错类型会直接拒绝并提示,这里在保存时把不合法的边丢掉。 + # 不校验的话,把 bool 接到文本输入上会得到一个字符串 "true", + # 然后模型就懵了 —— 这种错最难查(图看着连对了)。 + if kind != EXEC: + _set_type_context(nodes) + want = _data_in_type(dst) + give = _data_out_type(src) + if want is None or give is None or want != give: + continue out.append({'from': src, 'to': dst, 'branch': str(e.get('branch') or ''), 'kind': kind}) return out @@ -470,9 +533,25 @@ def _outgoing(edges: list, node_id: str) -> list: EXEC = 'exec' DATA = 'data' +# 数据引脚的类型。和蓝图一样是**强类型**的:连错类型直接接不上。 +PIN_TYPES = ('str', 'bool', 'num') + +# 老会议里没有 kind 的边当 exec —— 它们仍然负责"跑的顺序", +# 但**不再顺带传数据**。想传值就得画数据线(界面上现在很好画了)。 +_DATA_KINDS = set(PIN_TYPES) + def _kind(edge: dict) -> str: - return DATA if edge.get('kind') == DATA else EXEC + """边的种类:exec,或某种数据类型(str/bool/num)。认不出来的一律当 exec。""" + raw = str(edge.get('kind') or '') + if raw == DATA: + # 只写了 'data' 没写具体类型的旧数据:当 str(最常见的那种) + return 'str' + return raw if raw in _DATA_KINDS else EXEC + + +def _is_data(edge: dict) -> bool: + return _kind(edge) in _DATA_KINDS def _exec_in(edges: list, node_id: str) -> list: @@ -487,11 +566,11 @@ def _exec_out(edges: list, node_id: str, branch: str = None) -> list: def _data_in(edges: list, node_id: str) -> list: - return [e for e in _incoming(edges, node_id) if _kind(e) == DATA] + return [e for e in _incoming(edges, node_id) if _is_data(e)] def _data_out(edges: list, node_id: str) -> list: - return [e for e in _outgoing(edges, node_id) if _kind(e) == DATA] + return [e for e in _outgoing(edges, node_id) if _is_data(e)] # --------------------------------------------------------------------------- # @@ -558,30 +637,42 @@ def _collect_input(node_id: str, edges: list, outputs: dict, variables: dict, """ 算出这个节点的输入文本。 - 优先级: - 1. **数据入边**:把上游的值拼起来(多条按连线顺序)。 - 其中"读变量"是纯节点,在**被读取的这一刻**才求值 —— - 它不在执行链上,所以不能靠 outputs 里有没有它来判断。 - 2. 没有数据入边时,用 **exec 上游的输出**。 - 这一步是为了省事:最简单的"一根线连到底"不该逼用户连两根线。 + ★ **只认数据线**。以前这里还留了个"没有数据入边就用 exec 上游的输出"的回退, + 那等于让 exec 偷偷传数据 —— 于是"接一根 exec 线"和"接一根数据线"效果一样, + 类型也没法校验。用户明确说了"exec 执行链不传递数据",所以回退删掉了。 + + 代价是老会议里那些"一根线连到底"的图不再自动传值,得补一根数据线。 + 但这是对的:顺序和取值本来就不该是同一件事。 """ parts = [] for e in _data_in(edges, node_id): # 纯节点(读变量 / 值)现取现值,普通节点取它执行时的输出 value = _pure_value(e.get('from'), by_id, outputs, variables) - if value: - parts.append(str(value)) - if parts: - return '\n\n'.join(parts).strip() - - # 回退:exec 上游的输出 - for e in _exec_in(edges, node_id): - text = outputs.get(e.get('from')) - if text: - parts.append(str(text)) + if value not in (None, ''): + parts.append(_as_text(value, _kind(e))) return '\n\n'.join(parts).strip() +def _as_text(value, kind: str) -> str: + """ + 把引脚上的值转成文本。 + + 类型信息主要用在**连接校验**和**界面显示**上;真正送进模型/提示词的时候 + 终归是文本。所以这里做一次规整: + bool 统一成 'true'/'false'(模型对这两个词最不容易理解错) + num 去掉多余小数位 + """ + if kind == 'bool': + return 'true' if truthy(value) else 'false' + if kind == 'num': + try: + num = float(value) + return str(int(num)) if num == int(num) else str(num) + except (TypeError, ValueError): + return str(value) + return str(value) + + def _judge_rule(node: dict, text: str) -> bool: """结构化条件判断(不用 eval,理由见文件头)""" kind = str(node.get('judgeKind') or 'not_empty') diff --git a/scripts/verify-meeting-http.mjs b/scripts/verify-meeting-http.mjs index debc945..0cc56ef 100644 --- a/scripts/verify-meeting-http.mjs +++ b/scripts/verify-meeting-http.mjs @@ -106,12 +106,19 @@ const graph = { { id: 'f', type: 'agent', x: 200, y: 50, title: '别的专家', agentId: '', outputControl: '一句话' }, { id: 'e', type: 'end', x: 300, y: 0 }, ], + // ★ exec 排顺序、str 数据线传值 —— 判断节点要拿到内容必须有数据线。 + // 老写法"一根线既排顺序又传值"在类型化之后不成立了。 edges: [ - { from: 's', to: 'j' }, - { from: 'j', to: 't', branch: 'true' }, - { from: 'j', to: 'f', branch: 'false' }, - { from: 't', to: 'e' }, - { from: 'f', to: 'e' }, + { from: 's', to: 'j', kind: 'exec' }, + { from: 's', to: 'j', kind: 'str' }, + { from: 'j', to: 't', kind: 'exec', branch: 'true' }, + { from: 'j', to: 'f', kind: 'exec', branch: 'false' }, + { from: 'j', to: 't', kind: 'str' }, + { from: 'j', to: 'f', kind: 'str' }, + { from: 't', to: 'e', kind: 'exec' }, + { from: 'f', to: 'e', kind: 'exec' }, + { from: 't', to: 'e', kind: 'str' }, + { from: 'f', to: 'e', kind: 'str' }, ], }; const saved = await api(`/api/meetings/${meetingId}`, { diff --git a/scripts/verify-meeting.mjs b/scripts/verify-meeting.mjs index 79eaa23..c6c380e 100644 --- a/scripts/verify-meeting.mjs +++ b/scripts/verify-meeting.mjs @@ -204,6 +204,9 @@ const cases = [ // 数据线:e 从读变量拉值(rd 是纯节点,不在执行链上) edges: [ { from: 's', to: 'w', kind: 'exec' }, + // ★ 写变量要拿到上游的值,必须有**数据线** —— + // exec 只排顺序、不传数据。这正是类型化之后和以前最大的区别。 + { from: 's', to: 'w', kind: 'str' }, { from: 'w', to: 'e', kind: 'exec' }, { from: 'rd', to: 'e', kind: 'data' }, ], @@ -221,6 +224,9 @@ const cases = [ ], edges: [ { from: 's', to: 'w', kind: 'exec' }, + // ★ 写变量要拿到上游的值,必须有**数据线** —— + // exec 只排顺序、不传数据。这正是类型化之后和以前最大的区别。 + { from: 's', to: 'w', kind: 'str' }, { from: 'w', to: 'e', kind: 'exec' }, { from: 'rd', to: 'e', kind: 'data' }, ], @@ -309,6 +315,24 @@ const cases = [ /* ------------------------------------------------- 跑一份实现,收集事件 */ +/** + * 给用例图补上数据线。 + * + * ★ exec 现在**不传数据**了(照 UE 蓝图:exec 只排顺序,值走类型化的数据线)。 + * 老用例里的边都是"一根线既排顺序又传值"的老写法,所以要给每条没有 kind + * 的边补一条并行的 str 数据线 —— 这正是用户在界面上迁移时会做的事。 + * + * 用例里**显式写了 kind 的边不动**(那些是在测类型校验本身)。 + */ +function withDataEdges(meeting) { + const edges = []; + for (const e of meeting.edges || []) { + edges.push(e); + if (!e.kind) edges.push({ ...e, kind: 'str' }); + } + return { ...meeting, edges }; +} + async function runOne(impl, cs) { const order = []; const events = []; @@ -329,7 +353,7 @@ async function runOne(impl, cs) { return base + extra; }; const out = await runMeeting({ - meeting: cs.meeting, + meeting: withDataEdges(cs.meeting), settings: {}, userInput: '', emit: (e) => { @@ -390,7 +414,7 @@ print(json.dumps(out, ensure_ascii=False)) `; const res = spawnSync(PY, ['-c', script], { input: JSON.stringify(cases.map((c) => ({ - meeting: c.meeting, + meeting: withDataEdges(c.meeting), stopAfter: c.stopAfter || null, }))), encoding: 'utf8', diff --git a/server/src/meeting.mjs b/server/src/meeting.mjs index feebbe8..b77d75f 100644 --- a/server/src/meeting.mjs +++ b/server/src/meeting.mjs @@ -327,9 +327,25 @@ const outgoing = (edges, id) => edges.filter((e) => e.from === id); export const EXEC = 'exec'; export const DATA = 'data'; -const kindOf = (e) => (e.kind === DATA ? DATA : EXEC); +/** 数据引脚的类型。和蓝图一样是**强类型**的:连错类型直接接不上。 */ +export const PIN_TYPES = ['str', 'bool', 'num']; + +const DATA_KINDS = new Set(PIN_TYPES); + +/** + * 边的种类:exec,或某种数据类型(str/bool/num)。认不出来的一律当 exec。 + * + * ★ 老会议里没有 kind 的边当 exec —— 它们仍然负责"跑的顺序", + * 但**不再顺带传数据**。想传值就得画数据线。 + */ +const kindOf = (e) => { + const raw = String(e.kind || ''); + if (raw === DATA) return 'str'; // 只写 'data' 的旧数据:当 str + return DATA_KINDS.has(raw) ? raw : EXEC; +}; +const isData = (e) => DATA_KINDS.has(kindOf(e)); const execIn = (edges, id) => incoming(edges, id).filter((e) => kindOf(e) === EXEC); -const dataIn = (edges, id) => incoming(edges, id).filter((e) => kindOf(e) === DATA); +const dataIn = (edges, id) => incoming(edges, id).filter((e) => isData(e)); /** * 纯节点(蓝图里的 Get / 常量) @@ -366,22 +382,37 @@ function execOut(edges, id, branch = null) { * 这一步是为了省事:最简单的"一根线连到底"不该逼用户连两根线。 */ function collectInput(id, edges, outputs, variables, byId) { + // ★ **只认数据线**。以前这里还留了个"没有数据入边就用 exec 上游的输出"的回退, + // 那等于让 exec 偷偷传数据 —— 于是"接一根 exec 线"和"接一根数据线"效果一样, + // 类型也没法校验。用户明确说了"exec 执行链不传递数据",所以回退删掉了。 const parts = []; for (const e of dataIn(edges, id)) { // 纯节点(读变量 / 值)现取现值,普通节点取它执行时的输出 const value = pureValue(e.from, byId, outputs, variables); - if (value) parts.push(String(value)); - } - if (parts.length) return parts.join('\n\n').trim(); - - // 回退:exec 上游的输出 - for (const e of execIn(edges, id)) { - const text = outputs[e.from]; - if (text) parts.push(String(text)); + if (value !== undefined && value !== null && value !== '') { + parts.push(asText(value, kindOf(e))); + } } return parts.join('\n\n').trim(); } +/** + * 把引脚上的值转成文本。 + * + * 类型信息主要用在**连接校验**和**界面显示**上;真正送进模型的时候终归是文本。 + * bool 统一成 'true'/'false'(模型对这两个词最不容易理解错) + * num 去掉多余小数位 + */ +function asText(value, kind) { + if (kind === 'bool') return truthy(value) ? 'true' : 'false'; + if (kind === 'num') { + const n = Number(value); + if (!Number.isFinite(n)) return String(value); + return Number.isInteger(n) ? String(n) : String(n); + } + return String(value); +} + /** 结构化条件判断(不用 eval,理由见文件头) */ export function judgeRule(node, text) { const kind = String(node.judgeKind || 'not_empty');