会议模式:自定义节点做完(用户的 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:
@@ -34,6 +34,7 @@ import androidx.compose.material.icons.filled.PlayArrow
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Surface
|
||||
@@ -622,12 +623,12 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () -
|
||||
/** 命中哪个引脚。返回 (节点, 引脚, 是输出端) */
|
||||
fun portAt(c: Offset): Triple<MeetingNode, PinSpec, Boolean>? {
|
||||
for (n in nodesSnapshot.value.asReversed()) {
|
||||
outputPins(n).forEachIndexed { i, pin ->
|
||||
outputPins(n, vm.meetingMeta.customNodes).forEachIndexed { i, pin ->
|
||||
val p = pinPos(n, i, isOutput = true)
|
||||
val pp = Offset(p.x.dp.toPx(), p.y.dp.toPx())
|
||||
if ((pp - c).getDistance() <= portHit) return Triple(n, pin, true)
|
||||
}
|
||||
inputPins(n).forEachIndexed { i, pin ->
|
||||
inputPins(n, vm.meetingMeta.customNodes).forEachIndexed { i, pin ->
|
||||
val p = pinPos(n, i, isOutput = false)
|
||||
val pp = Offset(p.x.dp.toPx(), p.y.dp.toPx())
|
||||
if ((pp - c).getDistance() <= portHit) return Triple(n, pin, false)
|
||||
@@ -1066,6 +1067,7 @@ if (edge.kind == "exec") {
|
||||
inDegree = meeting.edges.count { it.to == node.id },
|
||||
outDegree = meeting.edges.count { it.from == node.id },
|
||||
linkingKind = linkingFrom?.kind,
|
||||
customDefs = vm.meetingMeta.customNodes,
|
||||
modifier = Modifier.offset(node.x.dp, node.y.dp),
|
||||
)
|
||||
}
|
||||
@@ -1308,6 +1310,62 @@ if (edge.kind == "exec") {
|
||||
.heightIn(max = 440.dp)
|
||||
.verticalScroll(rememberScrollState()),
|
||||
) {
|
||||
// ★ **用户用 Python 拓展出来的节点**,列在内置节点前面。
|
||||
// 引脚是从内核读的,用户往 nodes/ 丢一个 .py,这里就多一项。
|
||||
vm.meetingMeta.customNodes.forEach { cd ->
|
||||
val bad = cd.error.isNotBlank()
|
||||
Column(
|
||||
Modifier.fillMaxWidth()
|
||||
.clip(RoundedCornerShape(10.dp))
|
||||
.clickable(enabled = !bad) {
|
||||
val n = MeetingNode(
|
||||
id = "n_${System.currentTimeMillis().toString(36)}",
|
||||
type = "custom",
|
||||
customKey = cd.key,
|
||||
x = 120f + meeting.nodes.size * 26f,
|
||||
y = 120f + meeting.nodes.size * 26f,
|
||||
title = cd.label,
|
||||
)
|
||||
meeting = meeting.copy(nodes = meeting.nodes + n)
|
||||
dirty = true
|
||||
persist()
|
||||
adding = false
|
||||
}
|
||||
.padding(horizontal = 10.dp, vertical = 8.dp),
|
||||
) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Text(
|
||||
"🐍 " + cd.label,
|
||||
fontSize = 14.sp,
|
||||
color = if (bad) MaterialTheme.colorScheme.error else doubaoText(),
|
||||
)
|
||||
if (cd.file.isNotBlank()) {
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
cd.file,
|
||||
fontSize = 10.sp,
|
||||
color = doubaoTextSecondary(),
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
if (bad) {
|
||||
cd.error
|
||||
} else {
|
||||
cd.desc.ifBlank {
|
||||
"入 ${cd.dataInputs.size} · 出 ${cd.dataOutputs.size} · exec 分支 ${cd.execOutputs.size} 路"
|
||||
}
|
||||
},
|
||||
fontSize = 11.sp,
|
||||
color = if (bad) MaterialTheme.colorScheme.error else doubaoTextSecondary(),
|
||||
lineHeight = 15.sp,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (vm.meetingMeta.customNodes.isNotEmpty()) {
|
||||
HorizontalDivider(Modifier.padding(vertical = 6.dp), thickness = 0.5.dp)
|
||||
}
|
||||
|
||||
vm.meetingMeta.nodeTypes.forEach { (key, label, desc) ->
|
||||
Column(
|
||||
Modifier.fillMaxWidth()
|
||||
@@ -1392,6 +1450,12 @@ private data class PinSpec(
|
||||
val key: String,
|
||||
val label: String,
|
||||
val accepts: List<String> = emptyList(),
|
||||
/**
|
||||
* exec 引脚的分支名。自定义节点可以有**多个 exec 出口**(= N 路分支),
|
||||
* 连线时要把它写进 edge.branch,引擎才知道该走哪条。
|
||||
* 内置节点(判断/while)的 true/false/done/body 是同一套机制。
|
||||
*/
|
||||
val branch: String = "",
|
||||
) {
|
||||
/** 能不能接下 fromKind 这条线 */
|
||||
fun accepts_(fromKind: String): Boolean =
|
||||
@@ -1437,7 +1501,32 @@ private fun opSpec(key: String): Triple<String, String, Int> = when (key) {
|
||||
private fun opLabel(key: String): String =
|
||||
OPS_UI.firstOrNull { it.first == key }?.second ?: key
|
||||
|
||||
private fun inputPins(node: MeetingNode): List<PinSpec> = when (node.type) {
|
||||
/**
|
||||
* 节点的输入引脚。
|
||||
*
|
||||
* ★ `custom` 的引脚来自**内核**(用户在 nodes/ 里写的声明),不在这里硬编码 ——
|
||||
* 这样"加一种节点"就真的只是丢一个 .py,不用改 Kotlin。
|
||||
* meta 没加载到时给一个宽松的默认(不收 exec、一个文本入口),
|
||||
* 免得卡片画不出引脚、用户以为节点坏了。
|
||||
*/
|
||||
private fun inputPins(
|
||||
node: MeetingNode,
|
||||
custom: List<com.doubao.lynxclone.data.CustomNodeDef> = emptyList(),
|
||||
): List<PinSpec> = when (node.type) {
|
||||
"custom" -> {
|
||||
val d = custom.firstOrNull { it.key == node.customKey }
|
||||
if (d == null) {
|
||||
listOf(PinSpec("exec", "", "▶"), PinSpec("str", "", "输入", accepts = ANY_DATA))
|
||||
} else {
|
||||
buildList {
|
||||
// 自己声明的 exec 入口(没有就不画 —— 纯数据节点)
|
||||
d.inputs.filter { it.type == "exec" }.forEach { add(PinSpec("exec", "", "▶")) }
|
||||
d.dataInputs.forEach { p ->
|
||||
add(PinSpec(p.type, p.name, p.name, accepts = listOf(p.type)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"start", "readvar", "value" -> emptyList()
|
||||
// 转换:什么都收(出口类型在配置面板里选)
|
||||
// Python 节点:执行节点 —— 上游什么都要能收(它就是拿来处理任意内容的)
|
||||
@@ -1489,7 +1578,31 @@ private fun inputPins(node: MeetingNode): List<PinSpec> = when (node.type) {
|
||||
* ★ 写变量**没有数据出线**(存进去的值不从它往下传),但有 exec 出线。
|
||||
* ★ 纯节点(读变量 / 值)**只有数据出线**,没有任何 exec 引脚。
|
||||
*/
|
||||
private fun outputPins(node: MeetingNode): List<PinSpec> = when (node.type) {
|
||||
/** 节点的输出引脚(custom 同样来自内核的定义) */
|
||||
private fun outputPins(
|
||||
node: MeetingNode,
|
||||
custom: List<com.doubao.lynxclone.data.CustomNodeDef> = emptyList(),
|
||||
): List<PinSpec> = when (node.type) {
|
||||
"custom" -> {
|
||||
val d = custom.firstOrNull { it.key == node.customKey }
|
||||
if (d == null) {
|
||||
listOf(PinSpec("exec", "", "▶"), PinSpec("str", "", "输出"))
|
||||
} else {
|
||||
buildList {
|
||||
// 多个 exec 出口 = N 路分支,每条一个引脚(名字就是分支名)
|
||||
d.execOutputs.forEach { add(PinSpec("exec", it.name, it.name, branch = it.name)) }
|
||||
d.dataOutputs.forEachIndexed { i, p ->
|
||||
add(
|
||||
PinSpec(
|
||||
p.type,
|
||||
p.name,
|
||||
p.name + if (i > 0) "(接不了线)" else "",
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"end" -> emptyList()
|
||||
"readvar" -> listOf(PinSpec("str", "", "值"))
|
||||
// 转换:出口类型就是它要转成的那个类型
|
||||
@@ -1536,8 +1649,11 @@ private const val PORT_R = 5.5f
|
||||
private const val PORT_HIT = 19f
|
||||
|
||||
/** 节点高矮跟着引脚数走:判断节点两行出线、智能体进出各有两种,就高一些 */
|
||||
private fun nodeHeight(node: MeetingNode): Float {
|
||||
val rows = maxOf(inputPins(node).size, outputPins(node).size, 1)
|
||||
private fun nodeHeight(
|
||||
node: MeetingNode,
|
||||
customDefs: List<com.doubao.lynxclone.data.CustomNodeDef> = emptyList(),
|
||||
) : Float {
|
||||
val rows = maxOf(inputPins(node, customDefs).size, outputPins(node, customDefs).size, 1)
|
||||
return HEADER_H + rows * PIN_ROW_H + BODY_H
|
||||
}
|
||||
|
||||
@@ -2962,6 +3078,8 @@ private fun NodeCard(
|
||||
linkingKind: String?,
|
||||
/** 这个节点的告警(比如"输入是空的,漏了数据线") */
|
||||
warn: String = "",
|
||||
/** 自定义节点的定义(引脚从内核来,卡片照着画) */
|
||||
customDefs: List<com.doubao.lynxclone.data.CustomNodeDef> = emptyList(),
|
||||
modifier: Modifier,
|
||||
) {
|
||||
val accent = nodeColor(node.type)
|
||||
|
||||
Reference in New Issue
Block a user