mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-21 20:22:01 +00:00
d071bf119f
一、表结构、实体与引文
- 数据源工具查库时顺带存一份表结构快照(schema_snapshot),和查询快照一起进台账、进封存;
台账里的查询记下 SQL 用到的表
- 报告里的表名、字段名可以点:反引号里的名字一律核对,正文里的裸名字只在明显是标识符时才自动链接,
常见英文单词不链接;点开看到它出现在哪次查询、类型、所属的表,快照被截断时写明
- 找不到的名字标成可疑实体:在快照、SQL、结果列里都没有的记「可能是编造的名字」并给出最接近的 3 个;
快照被截断、核对不了的记「核对不了」,不冤枉成编造
- 检索引文 [[q:K1|原文]]:原文在检索快照的命中片段里逐字出现(空白归一化后)才算有出处,点开高亮原文位置
- 自动链接的名字不算句子的引用,只有写作者写的引用才算
二、结论句与出具
- 报告节点新增 claims:off(默认,保持原样)或 require_citation(没有引用的结论句计入缺口)
- 受管级别的正式运行里,可疑实体和没有引用的结论句按「有缺口」降档,不拦截;探索运行和已发布级别只标注
- 契约的 claims 和报告节点的设置取更严的那个;受管正式运行里写 ignore 也按降档处理,并给出警告
- 出具横幅除了数字,也写出可疑实体和没有引用的结论句
三、受管门禁 G1–G5(只看图和配置)
- G1 带契约的出口,成果字段的文字只能来自报告撰写节点
- G2 模型写的文字不绕过报告撰写节点直接流进出口
- G3 报告撰写节点写明 numbers: strict、on_violation: fail、claims: require_citation
- G4 口径卡的输入不来自计算角色的沙箱代码
- G5 给口径卡供数的 agent 配 output_schema 并开 cite_fields
- 受管级别是错误、挡住发布;已发布级别只给警告;已经发布的版本照常运行,下次发布时按新规则检查
- 每条规则都接进发布前自动修复(一键修复 / 给选项 / 交给 Copilot);禁止的改动补上关 cite_fields、
删 output_schema、放宽契约 claims;Copilot 兜底不许自己把代码节点标成 source、不许放宽报告节点
- 发布弹窗里选「交给 Copilot」时明确写「正在交给 Copilot」
四、旧运行与审计
- 没有契约的旧答案:数字和已封存的查询单元格、口径卡值按数值比对,每个数最多 3 个候选,
默认折叠,写明「猜测的来源,不能当证据」
- 有契约的旧运行:候选只从已封存的口径卡里找,人为插进工件表的行不当证据
- 证据审计:GET /api/runs/{id}/evidence/audit 按「无证据 / 可疑实体 / 有出处 / 旧运行猜测」分组,
可导出 JSON 和 CSV(带封存状态);记录页和画布上的报告节点显示「有出处 N · 无证据 M」
五、SQL 守卫修正(安全)
- 去注释改成单遍扫描,认得字符串和各方言的引号(单引号转义、MySQL 反斜杠与 #、PostgreSQL 的 $$ 与嵌套注释、
Oracle 的 q'[…]'、SQLite 的方括号);以前字符串里的 -- 会被当成注释,SELECT '--'; DROP … 这类写法能骗过多语句拦截
- 看不到服务器设置时每种读法都扫一遍,读法之间切分不一致就拒绝,任何一种读法是写操作就按写操作处理
- 执行查询和工具危险判定都按数据源的方言检查
六、其他
- 修掉检查脚本里画布检查的一处竞态,并行估时按实测更新
681 lines
33 KiB
TypeScript
681 lines
33 KiB
TypeScript
import {
|
||
Bot, Braces, Brain, CheckCircle2, Code2, Database, FileInput, FileOutput, FileText,
|
||
Gauge, GitBranch, Hand, Repeat, Search, Shuffle, Users, Wrench,
|
||
} from 'lucide-react'
|
||
import { APPROVAL_POLICY_LABEL, NODE_TYPE_LABEL, UPGRADE_POLICY_LABEL } from '../lib/terms'
|
||
import type { NodeType } from '../types'
|
||
|
||
export type FieldType =
|
||
| 'text' | 'textarea' | 'prompt' | 'code' | 'number' | 'select' | 'switch'
|
||
| 'json' | 'model' | 'tools' | 'skills' | 'collection'
|
||
| 'ioFields' | 'cases' | 'agents' | 'metricsList' | 'nodeRefs' | 'caliberFrom'
|
||
|
||
/**
|
||
* 这个字段里写的是什么语法。
|
||
*
|
||
* 模板和表达式长得像、错法相反:模板里写 `{{ vars.x }}`,取不到值渲染成空字符串、
|
||
* 不报错;表达式里写裸的 `vars.x == 1`,套上 `{{ }}` 就是另一种错。以前两种输入框
|
||
* 外观一模一样,只能靠 placeholder 暗示,后端专门加了「{{ }} 是多余的」这条告警,
|
||
* 说明这类错误很常见。检查器按它挂徽标、补全和高亮。
|
||
*
|
||
* 不写时按类型推:prompt / textarea / code 是模板,其余是普通文本。
|
||
* 显式写 'plain' 的是长得像模板、后端却不渲染的字段(成员的 system)。
|
||
*/
|
||
export type FieldSyntax = 'template' | 'expression' | 'plain'
|
||
|
||
export interface FieldDef {
|
||
key: string
|
||
label: string
|
||
type: FieldType
|
||
syntax?: FieldSyntax
|
||
placeholder?: string
|
||
help?: string
|
||
options?: { value: string; label: string }[]
|
||
min?: number
|
||
max?: number
|
||
step?: number
|
||
/** 只在满足条件时显示,避免面板一次糊一屏用不上的选项 */
|
||
when?: (config: Record<string, any>) => boolean
|
||
/**
|
||
* 看得见但暂时用不了:返回一句为什么(「先写 Schema 才能开」),返回 null 就能用。
|
||
* 和 when 的区别:藏起来的人不知道有这个选项,禁用的知道、也知道差什么
|
||
*/
|
||
disabled?: (config: Record<string, any>) => string | null
|
||
/** nodeRefs:只能选这一类的上游节点(报告的 metrics_from 只收口径卡) */
|
||
refType?: NodeType
|
||
/**
|
||
* select:config 里写着一个不在选项里的值(手写的、Copilot 写的、后续版本才支持的)时,
|
||
* 下拉里照实多列一项、写明为什么不认。不给的话浏览器会把它显示成第一个选项——看着像默认值
|
||
*/
|
||
unknownLabel?: (value: string) => string
|
||
advanced?: boolean
|
||
}
|
||
|
||
/**
|
||
* 写没写结构化输出 Schema:对象形状(type: object 或带 properties),画布里存成 JSON 文本的也认,
|
||
* 和后端 llm._output_schema 的认法一致
|
||
*/
|
||
export function hasOutputSchema(schema: unknown): boolean {
|
||
let value = schema
|
||
if (typeof value === 'string') {
|
||
if (!value.trim()) return false
|
||
try { value = JSON.parse(value) } catch { return false }
|
||
}
|
||
return !!value && typeof value === 'object' && !Array.isArray(value)
|
||
&& ((value as any).type === 'object' || 'properties' in (value as any))
|
||
}
|
||
|
||
/** 字段实际的语法:显式声明优先,否则按类型推 */
|
||
export function syntaxOf(field: Pick<FieldDef, 'type' | 'syntax'>): FieldSyntax {
|
||
if (field.syntax) return field.syntax
|
||
return field.type === 'prompt' || field.type === 'textarea' || field.type === 'code'
|
||
? 'template' : 'plain'
|
||
}
|
||
|
||
export interface HandleDef {
|
||
id: string
|
||
label: string
|
||
color?: string
|
||
}
|
||
|
||
export interface NodeDef {
|
||
type: NodeType
|
||
label: string
|
||
category: string
|
||
description: string
|
||
icon: typeof Bot
|
||
hasTarget: boolean
|
||
/** 静态出口;branch/loop 这类由配置动态生成的返回 null */
|
||
sources: HandleDef[] | null
|
||
fields: FieldDef[]
|
||
defaults: Record<string, any>
|
||
}
|
||
|
||
const MODEL_FIELDS: FieldDef[] = [
|
||
{ key: 'model', label: '模型', type: 'model', help: '留空则用默认 provider' },
|
||
{
|
||
key: 'thinking', label: '思考模式', type: 'select', advanced: true,
|
||
options: [
|
||
{ value: '', label: '跟随模型默认' },
|
||
{ value: 'summarized', label: '开启并显示摘要' },
|
||
{ value: 'adaptive', label: '开启但不显示' },
|
||
{ value: 'off', label: '关闭' },
|
||
],
|
||
help: 'Claude 4.6+ 默认会先思考再回答,思考也消耗 token',
|
||
},
|
||
{
|
||
key: 'effort', label: '投入程度', type: 'select', advanced: true,
|
||
options: [
|
||
{ value: '', label: '默认' }, { value: 'low', label: 'low' },
|
||
{ value: 'medium', label: 'medium' }, { value: 'high', label: 'high' },
|
||
{ value: 'xhigh', label: 'xhigh' }, { value: 'max', label: 'max' },
|
||
],
|
||
},
|
||
{
|
||
key: 'temperature', label: '温度', type: 'number', min: 0, max: 2, step: 0.1, advanced: true,
|
||
help: 'Claude 4.6 之后的模型不支持,填了也会被自动忽略',
|
||
},
|
||
{ key: 'max_tokens', label: '最大输出 token', type: 'number', min: 1, advanced: true },
|
||
]
|
||
|
||
/**
|
||
* 审批策略。留空 = 跟随设置里的「危险工具默认需要人工确认」。
|
||
*
|
||
* 以前界面新建的节点在 defaults 里写死了 approval:'dangerous',于是每个节点都带着
|
||
* 显式配置,全局设置对它们永远不起作用;后端把空值当成「跟随全局」
|
||
* (NodeContext.approval_mode),这里的默认就该是空。
|
||
*/
|
||
const APPROVAL_FOLLOW = { value: '', label: '跟随全局设置' }
|
||
const APPROVAL_OPTIONS = [
|
||
APPROVAL_FOLLOW,
|
||
{ value: 'dangerous', label: APPROVAL_POLICY_LABEL.dangerous },
|
||
{ value: 'always', label: APPROVAL_POLICY_LABEL.always },
|
||
{ value: 'never', label: APPROVAL_POLICY_LABEL.never },
|
||
]
|
||
|
||
const COMMON_TAIL: FieldDef[] = [
|
||
{
|
||
key: 'assign_to', label: '结果存为变量', type: 'text', placeholder: '例如 result',
|
||
help: '下游用 {{ vars.变量名 }} 引用',
|
||
},
|
||
{
|
||
key: 'skip_if', label: '跳过条件', type: 'text', syntax: 'expression', advanced: true,
|
||
placeholder: 'vars.count == 0',
|
||
},
|
||
{ key: 'retries', label: '失败重试次数', type: 'number', min: 0, max: 5, advanced: true },
|
||
{
|
||
key: 'on_error', label: '出错时', type: 'select', advanced: true,
|
||
options: [{ value: '', label: '中断整个运行' }, { value: 'continue', label: '记录错误并继续' }],
|
||
},
|
||
]
|
||
|
||
export const NODE_DEFS: Record<NodeType, NodeDef> = {
|
||
input: {
|
||
type: 'input', label: NODE_TYPE_LABEL.input, category: '起止', icon: FileInput,
|
||
description: '工作流入口,声明需要哪些输入',
|
||
hasTarget: false, sources: [{ id: 'out', label: '' }],
|
||
fields: [{ key: 'fields', label: '输入字段', type: 'ioFields' }],
|
||
defaults: { fields: [{ name: 'question', required: true }] },
|
||
},
|
||
output: {
|
||
type: 'output', label: NODE_TYPE_LABEL.output, category: '起止', icon: FileOutput,
|
||
description: '收集结构化成果;配出具契约后升级为三档出具(完整 / 降档 / 不予出具)',
|
||
hasTarget: true, sources: [],
|
||
fields: [
|
||
{ key: 'fields', label: '成果字段', type: 'ioFields' },
|
||
{
|
||
key: 'contract', label: '出具契约', type: 'json', syntax: 'template', advanced: true,
|
||
help: '{"metrics_from":["口径卡节点id"],"narrative":"{{ vars.report }}",'
|
||
+ '"required":[…],"expected":[…],"allow_numbers":[…],"strict":false}。'
|
||
+ '配了之后叙述里的每个数字必须能回指指标集,否则降档或不予出具',
|
||
},
|
||
],
|
||
defaults: { fields: [{ name: '结果', value: '{{ last_message }}' }] },
|
||
},
|
||
llm: {
|
||
type: 'llm', label: NODE_TYPE_LABEL.llm, category: '模型', icon: Brain,
|
||
description: '单次 LLM 调用,可要求结构化输出',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'system', label: '系统提示', type: 'textarea', placeholder: '你是…' },
|
||
{ key: 'prompt', label: '用户提示', type: 'prompt', placeholder: '{{ input.question }}' },
|
||
{ key: 'skills', label: '挂载 Skill', type: 'skills' },
|
||
...MODEL_FIELDS,
|
||
{
|
||
key: 'output_schema', label: '结构化输出 Schema', type: 'json', advanced: true,
|
||
help: '填了就强制模型按此 JSON Schema 返回',
|
||
},
|
||
{ key: 'use_history', label: '带上对话历史', type: 'switch', advanced: true },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { system: '', prompt: '{{ input.question }}' },
|
||
},
|
||
agent: {
|
||
type: 'agent', label: NODE_TYPE_LABEL.agent, category: '模型', icon: Bot,
|
||
description: '带工具循环,自己决定调什么工具',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'system', label: '角色设定', type: 'textarea' },
|
||
{ key: 'prompt', label: '任务', type: 'prompt' },
|
||
{ key: 'tools', label: '可用工具', type: 'tools' },
|
||
{ key: 'skills', label: '挂载 Skill', type: 'skills' },
|
||
{
|
||
key: 'max_steps', label: '最大步数', type: 'number', min: 1, max: 100, placeholder: '跟随设置',
|
||
help: '只是兜底。重复调用、连续几步没拿到新信息、预算用完、上下文快满时,它会先按查到的部分收尾。'
|
||
+ '留空跟随「设置 → 运行默认值」;以前默认写进来的 12 也按留空处理',
|
||
},
|
||
{
|
||
key: 'budget_tokens', label: '令牌预算', type: 'number', min: 1000, step: 10000, advanced: true,
|
||
placeholder: '跟随设置',
|
||
help: '这个节点最多用多少令牌(输入加输出),用完就按查到的部分收尾。留空跟随设置,设置里可以改成不限',
|
||
},
|
||
{
|
||
key: 'budget_usd', label: '金额预算(美元)', type: 'number', min: 0.01, step: 0.1, advanced: true,
|
||
placeholder: '跟随设置',
|
||
help: '按模型目录里的价格估算。目录里没有价格的模型估不出金额,只能靠令牌预算',
|
||
},
|
||
{
|
||
key: 'approval', label: '审批策略', type: 'select', options: APPROVAL_OPTIONS,
|
||
help: '需要审批时运行会暂停,等你在运行面板或记录页的审批卡上处理',
|
||
},
|
||
{
|
||
key: 'parallel_tools', label: '并行执行工具', type: 'switch', advanced: true,
|
||
help: '默认关闭:一轮只调一个工具,看到结果再想下一步。开启后一轮可发多个,更快,'
|
||
+ '但这一批中间没有新的思考',
|
||
},
|
||
{
|
||
key: 'output_schema', label: '结构化输出 Schema', type: 'json', advanced: true,
|
||
help: '要把查到的数交给口径卡时写。写了还要开下面的「按出处核对字段」才生效;'
|
||
+ '只写 Schema 不开,产出和以前一样只有文字',
|
||
},
|
||
{
|
||
key: 'cite_fields', label: '按出处核对字段', type: 'switch', advanced: true,
|
||
// 已经开着的不锁:开了之后又清掉 Schema,得还能把它关上
|
||
disabled: (c) => (c.cite_fields || hasOutputSchema(c.output_schema) ? null : '先在上面写「结构化输出 Schema」才能开'),
|
||
help: '结束后多一次抽取调用:模型为 Schema 里每个字段写出它来自哪次查询的哪一格,系统按查询快照取值、'
|
||
+ '核对,对不上的以快照为准;查不到的记为空,不兜底成 0。下游口径卡用 vars.变量名.字段 读',
|
||
},
|
||
...MODEL_FIELDS,
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { tools: [], parallel_tools: false },
|
||
},
|
||
supervisor: {
|
||
type: 'supervisor', label: NODE_TYPE_LABEL.supervisor, category: '模型', icon: Users,
|
||
description: '调度者按进展把任务分派给多个专家',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'goal', label: '团队目标', type: 'prompt' },
|
||
{ key: 'agents', label: '团队成员', type: 'agents' },
|
||
// 叫「最多轮数」:团队用完轮数的报错和右栏都说「调大『最多轮数』」,字段名得对得上
|
||
{ key: 'max_rounds', label: '最多轮数', type: 'number', min: 1, max: 25 },
|
||
{
|
||
// 以前轮数用完就把成员最后的原话当成功结果交出去:成员一直没查到数、只会输出
|
||
// 工具调用的原始标记,运行照样显示「已完成」。默认改成判失败,降档要自己选
|
||
key: 'on_exhausted', label: '用完轮数时', type: 'select',
|
||
// 没写和写 fail 是一回事(后端只认 degrade)。选项用 fail:没写时下拉框落在第一项上
|
||
options: [
|
||
{ value: 'fail', label: '判为失败(默认)' },
|
||
{ value: 'degrade', label: '降档交付' },
|
||
],
|
||
help: '最后一轮调度者仍没判定完成时怎么收场。判为失败:节点报错、运行停下,并点出一次都没'
|
||
+ '被派到的成员;降档交付:把成员最后的原话交给下游,标上「未完成」,出具和复核随之降档。'
|
||
+ '要出数、要出报告的,留在判为失败',
|
||
},
|
||
{
|
||
key: 'max_parallel', label: '每轮最多同时派几人', type: 'number', min: 1, max: 6,
|
||
help: '互不依赖的任务调度者可以放在同一轮同时执行,总耗时按最慢的那个算。'
|
||
+ '设为 1 即退回严格串行——并发的成员看到的是同一份进展快照,'
|
||
+ '任务其实有依赖却被同时派出去,两个人会基于一样的旧信息重复劳动',
|
||
},
|
||
{
|
||
key: 'approval', label: '审批策略', type: 'select', advanced: true,
|
||
options: [
|
||
APPROVAL_FOLLOW,
|
||
{ value: 'dangerous', label: '需要审批的调用直接拦下' },
|
||
{ value: 'never', label: APPROVAL_POLICY_LABEL.never },
|
||
],
|
||
help: '成员们并行跑在一个节点里,停不下来等人审批。默认把危险工具和可写库上的写操作'
|
||
+ '挡下来,并告诉成员换一种做法;需要逐次审批的事交给团队外的 agent 节点',
|
||
},
|
||
...MODEL_FIELDS,
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { max_rounds: 6, max_parallel: 3, agents: [] },
|
||
},
|
||
tool: {
|
||
type: 'tool', label: NODE_TYPE_LABEL.tool, category: '执行', icon: Wrench,
|
||
description: '直接调用一个指定工具',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'tool', label: '工具', type: 'tools', help: '只能选一个' },
|
||
{ key: 'args', label: '参数', type: 'json', syntax: 'template', help: '值里可以用 {{ }} 引用上游' },
|
||
{ key: 'approval', label: '审批策略', type: 'select', options: APPROVAL_OPTIONS },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { args: {} },
|
||
},
|
||
code: {
|
||
type: 'code', label: NODE_TYPE_LABEL.code, category: '执行', icon: Code2,
|
||
description: '在隔离容器里执行代码',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{
|
||
key: 'language', label: '语言', type: 'select',
|
||
options: [
|
||
{ value: 'python', label: 'Python' }, { value: 'bash', label: 'Bash' },
|
||
{ value: 'node', label: 'Node.js' },
|
||
],
|
||
},
|
||
{ key: 'code', label: '代码', type: 'code', help: '代码里可以用 {{ }} 插值' },
|
||
{ key: 'timeout', label: '超时(秒)', type: 'number', min: 1, max: 300 },
|
||
{ key: 'memory_mb', label: '内存上限 MB', type: 'number', min: 64, max: 4096, advanced: true },
|
||
{ key: 'network', label: '允许联网', type: 'switch', help: '默认断网' },
|
||
{
|
||
key: 'isolation', label: '隔离档位', type: 'select',
|
||
options: [
|
||
{ value: '', label: '跟随整机默认' },
|
||
{ value: 'strict', label: 'strict:microVM(独立内核,内存真限得住)' },
|
||
{ value: 'fast', label: 'fast:系统沙箱(低延迟,内存限不住)' },
|
||
],
|
||
help: '不可信代码用 strict;自己写的、已审核的用 fast。两档都拦不住 DNS 出网,'
|
||
+ '真要防数据外泄得靠网络层。要的档位不可用时会退回默认并在时间线上告警',
|
||
},
|
||
{
|
||
key: 'approval', label: '审批策略', type: 'select', advanced: true,
|
||
// 代码节点不跟全局设置走(后端缺省就是 never),所以没有「跟随全局」
|
||
options: [
|
||
{ value: 'never', label: APPROVAL_POLICY_LABEL.never },
|
||
{ value: 'always', label: `${APPROVAL_POLICY_LABEL.always}(审批时可改代码)` },
|
||
],
|
||
},
|
||
{ key: 'fail_fast', label: '执行失败即中断', type: 'switch', advanced: true },
|
||
{
|
||
key: 'evidence_role', label: '证据角色', type: 'select', advanced: true,
|
||
options: [{ value: '', label: '计算(默认)' }, { value: 'source', label: '取数' }],
|
||
help: '报告不能直接引用代码节点的产出,沙箱算出来的数要进口径卡。只是把外部数据原样取回来'
|
||
+ '(调接口、读文件)的标「取数」;做了业务计算的留在「计算」,口径卡用它的数时会提醒',
|
||
},
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { language: 'python', code: 'print("hello")', timeout: 30, network: false, fail_fast: true },
|
||
},
|
||
branch: {
|
||
type: 'branch', label: NODE_TYPE_LABEL.branch, category: '控制', icon: GitBranch,
|
||
description: '按条件或语义分类走不同的路',
|
||
hasTarget: true, sources: null,
|
||
fields: [
|
||
{
|
||
key: 'mode', label: '判断方式', type: 'select',
|
||
options: [
|
||
{ value: 'expression', label: '表达式判断' },
|
||
{ value: 'llm', label: '让模型分类' },
|
||
],
|
||
},
|
||
{ key: 'cases', label: '分支', type: 'cases' },
|
||
{
|
||
key: 'input', label: '待分类内容', type: 'prompt',
|
||
when: (c) => c.mode === 'llm', placeholder: '{{ last_message }}',
|
||
},
|
||
{ key: 'instruction', label: '分类说明', type: 'textarea', when: (c) => c.mode === 'llm' },
|
||
...MODEL_FIELDS.filter((f) => f.key === 'model').map((f) => ({
|
||
...f, when: (c: any) => c.mode === 'llm',
|
||
})),
|
||
...COMMON_TAIL.filter((f) => f.key !== 'assign_to'),
|
||
],
|
||
defaults: { mode: 'expression', cases: [{ key: 'yes', condition: '', label: '' }] },
|
||
},
|
||
loop: {
|
||
type: 'loop', label: NODE_TYPE_LABEL.loop, category: '控制', icon: Repeat,
|
||
description: '遍历列表或按条件重复执行',
|
||
hasTarget: true, sources: null,
|
||
fields: [
|
||
{
|
||
key: 'mode', label: '循环方式', type: 'select',
|
||
options: [
|
||
{ value: 'foreach', label: '遍历列表' },
|
||
{ value: 'while', label: '条件成立时重复' },
|
||
],
|
||
},
|
||
{
|
||
// 后端按模板渲染(control.py 的 ctx.render),以前定义成普通文本,没有补全
|
||
key: 'items', label: '列表来源', type: 'text', syntax: 'template',
|
||
when: (c) => c.mode !== 'while', placeholder: '{{ input.items }}',
|
||
},
|
||
{ key: 'item_var', label: '当前项变量名', type: 'text', when: (c) => c.mode !== 'while' },
|
||
{
|
||
key: 'condition', label: '继续条件', type: 'text', syntax: 'expression',
|
||
when: (c) => c.mode === 'while', placeholder: 'vars.done != true',
|
||
},
|
||
{ key: 'max_iterations', label: '最大迭代次数', type: 'number', min: 1, max: 100 },
|
||
],
|
||
defaults: { mode: 'foreach', item_var: 'item', max_iterations: 10 },
|
||
},
|
||
subgraph: {
|
||
type: 'subgraph', label: NODE_TYPE_LABEL.subgraph, category: '控制', icon: Braces,
|
||
description: '把另一张工作流当成一个节点嵌进来',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'workflow_id', label: '工作流', type: 'select', options: [] },
|
||
{ key: 'input', label: '传入参数', type: 'json', syntax: 'template' },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { input: {} },
|
||
},
|
||
memory: {
|
||
type: 'memory', label: NODE_TYPE_LABEL.memory, category: '上下文', icon: Database,
|
||
description: '读取或写入跨运行的长期记忆',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{
|
||
key: 'action', label: '操作', type: 'select',
|
||
options: [
|
||
{ value: 'recall', label: '回忆(检索)' },
|
||
{ value: 'write', label: '记住(写入)' },
|
||
{ value: 'clear', label: '清空该作用域' },
|
||
],
|
||
},
|
||
{
|
||
key: 'scope', label: '作用域', type: 'text', syntax: 'template',
|
||
help: '留空 = 跟随这次运行的默认作用域(设置 · 运行默认值)',
|
||
},
|
||
{ key: 'query', label: '回忆什么', type: 'prompt', when: (c) => c.action !== 'write' && c.action !== 'clear' },
|
||
{ key: 'limit', label: '返回条数', type: 'number', min: 1, max: 20, when: (c) => c.action !== 'write' },
|
||
{ key: 'content', label: '记住的内容', type: 'prompt', when: (c) => c.action === 'write' },
|
||
{
|
||
key: 'kind', label: '记忆类型', type: 'select', when: (c) => c.action === 'write',
|
||
options: [
|
||
{ value: 'fact', label: '事实' }, { value: 'preference', label: '偏好' },
|
||
{ value: 'episode', label: '事件' },
|
||
],
|
||
},
|
||
{ key: 'importance', label: '重要程度', type: 'number', min: 0, max: 1, step: 0.1, when: (c) => c.action === 'write' },
|
||
...COMMON_TAIL,
|
||
],
|
||
// scope 不写死:留空才会跟着这次运行的默认作用域走(后端 ctx.run.memory_scope),
|
||
// 写死 default 等于让设置里的「默认记忆作用域」对界面新建的节点永远不起作用
|
||
defaults: { action: 'recall', limit: 5, query: '{{ last_message }}' },
|
||
},
|
||
retrieve: {
|
||
type: 'retrieve', label: NODE_TYPE_LABEL.retrieve, category: '上下文', icon: Search,
|
||
description: '从知识库里混合检索相关片段',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'query', label: '检索问题', type: 'prompt' },
|
||
{
|
||
key: 'collection', label: '知识库', type: 'collection',
|
||
help: '留空 = 跟随这次运行的默认知识库(设置 · 运行默认值)',
|
||
},
|
||
{ key: 'limit', label: '返回片段数', type: 'number', min: 1, max: 20 },
|
||
{
|
||
key: 'rerank', label: '重排', type: 'select', advanced: true,
|
||
options: [
|
||
{ value: 'off', label: '不重排' },
|
||
{ value: 'model', label: '让模型重排(更准,多一次调用)' },
|
||
],
|
||
help: '开了会先多捞一些候选,再让模型按"对回答这个问题有多大帮助"重新排序',
|
||
},
|
||
{
|
||
key: 'alpha', label: '向量 vs 关键词', type: 'number', min: 0, max: 1, step: 0.1,
|
||
help: '1 = 纯语义相似,0 = 纯关键词匹配。留空则跟着向量模型的能力走'
|
||
+ '——本地哈希向量没有语义泛化,给它权重反而会让命中率下降',
|
||
},
|
||
{ key: 'min_score', label: '最低分数', type: 'number', min: 0, max: 1, step: 0.05, advanced: true },
|
||
...COMMON_TAIL,
|
||
],
|
||
// alpha 不写死:写死 0.5 等于给一个没有语义能力的信号一半权重,
|
||
// 实测会把 hit@1 从 88% 拉到 81%(backend/tests/test_retrieval_quality.py)
|
||
// collection 同理:留空跟着运行默认值走,正式运行会把实际用的那个写进封存
|
||
defaults: { query: '{{ last_message }}', limit: 5, rerank: 'off' },
|
||
},
|
||
transform: {
|
||
type: 'transform', label: NODE_TYPE_LABEL.transform, category: '上下文', icon: Shuffle,
|
||
description: '不调模型,直接把数据揉成下游要的形状',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{
|
||
key: 'mode', label: '方式', type: 'select',
|
||
options: [
|
||
{ value: 'template', label: '文本模板' },
|
||
{ value: 'expression', label: '表达式' },
|
||
{ value: 'json', label: 'JSON 模板' },
|
||
],
|
||
},
|
||
{ key: 'template', label: '模板', type: 'textarea', when: (c) => c.mode !== 'expression' },
|
||
{
|
||
key: 'expression', label: '表达式', type: 'text', syntax: 'expression',
|
||
when: (c) => c.mode === 'expression', placeholder: 'len(vars.items)',
|
||
},
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { mode: 'template', template: '{{ last_message }}' },
|
||
},
|
||
human: {
|
||
type: 'human', label: NODE_TYPE_LABEL.human, category: '把关', icon: Hand,
|
||
description: '暂停运行,等人审批、补充信息或改草稿',
|
||
hasTarget: true, sources: null,
|
||
fields: [
|
||
{
|
||
key: 'mode', label: '审批方式', type: 'select',
|
||
options: [
|
||
{ value: 'approve', label: '批准 / 驳回' },
|
||
{ value: 'input', label: '补充输入' },
|
||
{ value: 'edit', label: '编辑草稿' },
|
||
],
|
||
},
|
||
{ key: 'title', label: '标题', type: 'text', syntax: 'template' },
|
||
{ key: 'message', label: '给人看的内容', type: 'prompt' },
|
||
{ key: 'draft', label: '草稿内容', type: 'prompt', when: (c) => c.mode === 'edit' },
|
||
{ key: 'stop_on_reject', label: '驳回即终止运行', type: 'switch', when: (c) => c.mode === 'approve' },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { mode: 'approve', title: '需要你审批', message: '{{ last_message }}' },
|
||
},
|
||
validate: {
|
||
type: 'validate', label: NODE_TYPE_LABEL.validate, category: '把关', icon: CheckCircle2,
|
||
description: '按 JSON Schema 校验,不合格可让模型自动返工',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'source', label: '待校验内容', type: 'prompt', placeholder: '{{ last_message }}' },
|
||
{ key: 'schema', label: 'JSON Schema', type: 'json' },
|
||
{ key: 'max_retries', label: '最多返工次数', type: 'number', min: 0, max: 5 },
|
||
{ key: 'repair_with_llm', label: '让模型修复', type: 'switch' },
|
||
{ key: 'fail_fast', label: '校验失败即中断', type: 'switch' },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: {
|
||
source: '{{ last_message }}', max_retries: 2, repair_with_llm: true, fail_fast: true,
|
||
schema: { type: 'object', properties: {}, required: [] },
|
||
},
|
||
},
|
||
report: {
|
||
type: 'report', label: NODE_TYPE_LABEL.report, category: '模型', icon: FileText,
|
||
description: '带引用的报告:模型只写引用标记,数字由系统从口径卡取出来渲染,每个数字都能点开看出处',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{
|
||
key: 'instructions', label: '写作要求', type: 'prompt', placeholder: '为 {{ input.week }} 写周报,先总后分',
|
||
help: '写什么、怎么写。上游口径卡的指标和运行输入会自动整理成证据目录交给模型,不用在这里手抄',
|
||
},
|
||
{
|
||
key: 'metrics_from', label: '指标来自', type: 'nodeRefs', refType: 'metrics',
|
||
help: '留空 = 用上游全部口径卡。报告里的数字只能引用这些卡里的指标',
|
||
},
|
||
{
|
||
// 后端只认 strict / off(report.py 的 NUMBERS)
|
||
key: 'numbers', label: '没写引用的数字', type: 'select',
|
||
options: [
|
||
{ value: 'strict', label: '算违规,要求重写(默认)' },
|
||
{ value: 'off', label: '只标出来,不要求重写' },
|
||
],
|
||
help: '报告里每个数字都该写成引用标记,由系统取值渲染。模型自己写的数字核对不到出处',
|
||
},
|
||
{
|
||
// 留空不是「不处理」:后端按运行类别取默认,探索运行照常产出、正式运行判失败
|
||
key: 'on_violation', label: '重写后仍有违规时', type: 'select',
|
||
options: [
|
||
{ value: '', label: '按运行类别:探索运行照常产出,正式运行判失败' },
|
||
{ value: 'flag', label: '照常产出,把违规的地方标出来' },
|
||
{ value: 'fail', label: '判为失败,报告不往下交' },
|
||
],
|
||
},
|
||
{
|
||
key: 'max_repairs', label: '最多重写几次', type: 'number', min: 0, max: 3, placeholder: '1',
|
||
help: '有违规时把清单交回去让模型重写。每次都是一整篇的调用,写不对的模型多给几次也大多写不对',
|
||
},
|
||
{
|
||
// 后端只认 off / require_citation(schema.REPORT_CLAIMS);judge 是四期的模型裁判,写了会被校验拦下。
|
||
// 没写等于 off:select 显示第一项,正好是默认
|
||
key: 'claims', label: '没挂依据的结论句', type: 'select',
|
||
options: [
|
||
{ value: 'off', label: '不管(默认):不参与出具判档' },
|
||
{ value: 'require_citation', label: '计入缺口:出具按档位降档' },
|
||
],
|
||
help: '结论句是带数字、方向词或因果词的句子。选「计入缺口」时,写作提示要求每句结论挂上 [[see:…]] 依据,'
|
||
+ '没挂的记为缺口、出具降档(系统自动链接的表名字段名不算依据)。受管级别发布必须显式选它。'
|
||
+ '结论句裁判在后续版本支持',
|
||
unknownLabel: (v) => (v === 'judge' ? 'judge:结论句裁判在后续版本支持,现在不能用' : `${v}:不认识的值,只能选上面两项`),
|
||
},
|
||
{
|
||
// 后端只认 link / off(report.py 的 ENTITIES)
|
||
key: 'entities', label: '表名、字段名', type: 'select', advanced: true,
|
||
options: [
|
||
{ value: 'link', label: '核对,对得上的可以点开(默认)' },
|
||
{ value: 'off', label: '不核对' },
|
||
],
|
||
help: '报告里写到的表名、字段名按本次运行的表结构快照和查询核对:对得上的能点开看出处,'
|
||
+ '反引号里写了哪里都没有的名字标成「可能是编造的名字」',
|
||
unknownLabel: (v) => `${v}:不认识的值,只能选 link 或 off`,
|
||
},
|
||
...MODEL_FIELDS,
|
||
{ key: 'system', label: '角色设定', type: 'textarea', advanced: true, placeholder: '你是…' },
|
||
...COMMON_TAIL,
|
||
],
|
||
defaults: { instructions: '', numbers: 'strict', max_repairs: 1 },
|
||
},
|
||
metrics: {
|
||
type: 'metrics', label: NODE_TYPE_LABEL.metrics, category: '把关', icon: Gauge,
|
||
description: '受控指标集:所有算术在这里发生,叙述层只能引用',
|
||
hasTarget: true, sources: [{ id: 'out', label: '' }],
|
||
fields: [
|
||
{ key: 'caliber_from', label: '口径卡从哪来', type: 'caliberFrom' },
|
||
{
|
||
key: 'caliber', label: '口径名称', type: 'text', syntax: 'template', placeholder: '周报口径',
|
||
when: (c) => !c.caliber_from,
|
||
},
|
||
{ key: 'caliber_version', label: '口径版本', type: 'text', placeholder: 'v1', when: (c) => !c.caliber_from },
|
||
{ key: 'metrics', label: '指标定义', type: 'metricsList', when: (c) => !c.caliber_from },
|
||
{
|
||
key: 'upgrade_policy', label: '上游发了新版本时', type: 'select', when: (c) => !!c.caliber_from,
|
||
options: [
|
||
{ value: '', label: '没声明:上游有新版本时挡住正式运行' },
|
||
{ value: 'recompute', label: `${UPGRADE_POLICY_LABEL.recompute}(recompute)` },
|
||
{ value: 'dual', label: `${UPGRADE_POLICY_LABEL.dual}(dual)` },
|
||
{ value: 'incomparable', label: `${UPGRADE_POLICY_LABEL.incomparable}(incomparable)` },
|
||
],
|
||
help: '钉住的那一版之后上游又发了新版本,正式运行前必须声明怎么处置,和子工作流的升版处置是同一套规则;'
|
||
+ '声明了就照它执行,并记进运行记录',
|
||
},
|
||
{
|
||
key: 'on_missing', label: '缺输入时', type: 'select', advanced: true,
|
||
options: [{ value: '', label: '整个节点失败' }, { value: 'null', label: '记为空值,交给出具契约判档' }],
|
||
help: '上游没给出某个指标要的数时怎么办。记为空值的指标在报告里显示「—」,出具契约按必需 / 期望项降档',
|
||
},
|
||
{
|
||
key: 'assign_to', label: '结果存为变量', type: 'text',
|
||
help: '叙述节点用 {{ nodes.节点id.text }} 引用指标清单',
|
||
},
|
||
],
|
||
defaults: {
|
||
caliber: '', caliber_version: 'v1',
|
||
metrics: [{ id: 'total', name: '', unit: '', expression: '' }],
|
||
},
|
||
},
|
||
}
|
||
|
||
export const NODE_CATEGORIES = ['起止', '模型', '执行', '控制', '上下文', '把关']
|
||
|
||
/** branch / loop / human 的出口由配置决定,这里统一算出来。 */
|
||
export function sourceHandles(type: NodeType, config: Record<string, any>): HandleDef[] {
|
||
const def = NODE_DEFS[type]
|
||
// 不认识的类型给一个普通出口,连线还能接上。以前这里直接读 def.sources,
|
||
// 一个未知类型就让画布连带整站白屏
|
||
if (!def) return [{ id: 'out', label: '' }]
|
||
if (def.sources) return def.sources
|
||
if (type === 'branch') {
|
||
const cases = (config.cases ?? []) as { key?: string; label?: string }[]
|
||
// 出口颜色是中性的:ok 色只用来说「完成了」,哪条被走过由卡片按 taken 再点亮。
|
||
// 同名出口只留第一个:key 重复时 React Flow 会出两个同 id 的 handle,跑完两条
|
||
// 一起亮;重名本身由检查器和校验报错。key 用了保留名 default 的,和兜底出口
|
||
// 合并成一个——运行时它们本来就是同一个出口(edge.taken 的 branch 都是 default)
|
||
const seen = new Set<string>()
|
||
const handles: HandleDef[] = []
|
||
for (const c of cases) {
|
||
const key = (c.key ?? '').trim()
|
||
if (!key || seen.has(key)) continue
|
||
seen.add(key)
|
||
handles.push(key === 'default'
|
||
? { id: 'default', label: `${c.label || '其他'}(兜底)`, color: 'var(--text-dim)' }
|
||
: { id: key, label: c.label || key, color: 'var(--text-dim)' })
|
||
}
|
||
if (!seen.has('default')) handles.push({ id: 'default', label: '其他', color: 'var(--text-faint)' })
|
||
return handles
|
||
}
|
||
if (type === 'loop') {
|
||
return [
|
||
{ id: 'body', label: '循环体', color: 'var(--nt)' },
|
||
{ id: 'done', label: '结束', color: 'var(--text-faint)' },
|
||
]
|
||
}
|
||
if (type === 'human') {
|
||
if (config.mode === 'approve') {
|
||
return [
|
||
{ id: 'approved', label: '通过', color: 'var(--ok)' },
|
||
{ id: 'rejected', label: '驳回', color: 'var(--err)' },
|
||
]
|
||
}
|
||
return [{ id: 'out', label: '' }]
|
||
}
|
||
return [{ id: 'out', label: '' }]
|
||
}
|