mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-16 19:41:59 +00:00
模板字段加 {{ }} 补全:从源头消灭那类静默失败
模板取不到值时渲染成空字符串、不报错,所以拼错一个变量名在运行时完全静默。
上一个 commit 是事后校验(标红+建议),这个是事前:打 {{ 就弹出这张图里
实际有的变量,每条带说明和产出它的节点。
**只挂在真正过模板渲染的字段上。** 这一条是整件事最容易做错的地方:
这套里有五个字段长得像但走的是表达式求值——skip_if、loop.condition、
cases[].condition、transform.expression、metrics[].expression——那里要写
`vars.count == 0` 这样的裸路径,不带大括号。在那些字段上挂 {{ 触发器等于
手把手教用户写错。同理 supervisor 的 agents[].system 长得和 llm.system
一模一样,但后端是 cfg.get("system") 直接取值、根本不渲染,那里弹补全会
是个谎;它走的是 AgentList 里的裸 textarea,天然不受影响。
几个不写就会坏的细节:
- 这是完全受控组件。插入后 onChange 会一路走到 store 再把 value 灌回来,
DOM 光标会跳到末尾——必须等那一帧过去再自己 setSelectionRange 放回去。
测试里专门验了"插入后继续打字,字落在后面而不是中间"。
- 候选框用文档流里的块,不是绝对定位浮层:属性面板是 overflow-y-auto、
外层 InspectorSheet 是 overflow-hidden,浮层到边缘直接被切掉。
- 用 onMouseDown 不是 onClick——onClick 在 blur 之后才触发,那时候弹层
已经关了。
- Escape 要 stopPropagation:外面 InspectorSheet 也在听 Escape,不拦的话
按一下会连整个属性面板一起关掉。
- 光标前已经打了 | 或 [ 就不再弹——那时候是在写过滤器或下标,不是在选变量。
check-ui 补一组:变量表列得出来、抽屉挤压后画布没被压没(React Flow 容器
高度归零会直接报 error 004)、打 {{ 弹候选、插入成完整写法、光标位置正确。
This commit is contained in:
@@ -5,6 +5,7 @@ import { NODE_DEFS, type FieldDef } from './nodeDefs'
|
||||
import { useStudio } from '../store/studio'
|
||||
import { useCatalog, modelOptions } from '../store/catalog'
|
||||
import { JsonInput } from '../components/ui'
|
||||
import { TemplateText } from './TemplateText'
|
||||
|
||||
/** 属性面板。所有字段都由 nodeDefs 的声明驱动渲染,加节点类型不用改这里。 */
|
||||
export function Inspector() {
|
||||
@@ -165,23 +166,27 @@ function FieldInput({ field, value, onChange }: {
|
||||
/>
|
||||
)
|
||||
|
||||
// 这三种默认都过模板渲染,所以带 {{ }} 补全。
|
||||
// 注意 field.template === false 是显式关闭——supervisor 的 agents[].system
|
||||
// 长得和 llm.system 一模一样,但后端是 cfg.get("system") 直接取值,不渲染,
|
||||
// 在那儿弹补全会是个谎
|
||||
case 'textarea':
|
||||
case 'prompt':
|
||||
return (
|
||||
<textarea
|
||||
className={clsx('field', field.type === 'prompt' && 'mono text-[11.5px]')}
|
||||
<TemplateText
|
||||
className={clsx(field.type === 'prompt' && 'mono text-[11.5px]')}
|
||||
rows={field.type === 'prompt' ? 4 : 3}
|
||||
value={value ?? ''} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
onChange={onChange}
|
||||
/>
|
||||
)
|
||||
|
||||
case 'code':
|
||||
return (
|
||||
<textarea
|
||||
className="field mono text-[11px]" rows={10} spellCheck={false}
|
||||
<TemplateText
|
||||
className="mono text-[11px]" rows={10} spellCheck={false}
|
||||
value={value ?? ''} placeholder={field.placeholder}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
onChange={onChange}
|
||||
/>
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import clsx from 'clsx'
|
||||
import { useStudio } from '../store/studio'
|
||||
import type { Variable } from '../types'
|
||||
|
||||
/**
|
||||
* 会写 `{{ }}` 的输入框,打 `{{` 弹出可用变量。
|
||||
*
|
||||
* 为什么值得做:模板取不到值时渲染成空字符串、不报错,所以拼错一个变量名
|
||||
* 在运行时是完全静默的。补全是从源头上消灭这一整类错误——比事后校验更管用。
|
||||
*
|
||||
* **只能挂在真正过模板渲染的字段上。** 这套里有五个字段长得像但走的是
|
||||
* 表达式求值(skip_if、loop.condition、cases[].condition、transform.expression、
|
||||
* metrics[].expression),那里要写 `vars.count == 0` 这样的裸路径,不带大
|
||||
* 括号。在那些字段上挂 `{{` 触发器等于手把手教用户写错。挂哪些由 nodeDefs
|
||||
* 的 template 标记决定,不在这里猜。
|
||||
*/
|
||||
export function TemplateText({
|
||||
value, onChange, rows, className, placeholder, multiline = true, spellCheck,
|
||||
}: {
|
||||
value: string
|
||||
onChange: (v: string) => void
|
||||
rows?: number
|
||||
className?: string
|
||||
placeholder?: string
|
||||
multiline?: boolean
|
||||
spellCheck?: boolean
|
||||
}) {
|
||||
const variables = useStudio((s) => s.variables)
|
||||
const ref = useRef<HTMLTextAreaElement & HTMLInputElement>(null)
|
||||
const [query, setQuery] = useState<{ start: number; text: string } | null>(null)
|
||||
const [active, setActive] = useState(0)
|
||||
|
||||
const matches = query ? filterVars(variables, query.text).slice(0, 8) : []
|
||||
const open = !!query && matches.length > 0
|
||||
|
||||
useEffect(() => { setActive(0) }, [query?.text])
|
||||
|
||||
/** 光标前刚打完 `{{` 或正在写路径时,把待补全的片段找出来 */
|
||||
const detect = (text: string, caret: number) => {
|
||||
const before = text.slice(0, caret)
|
||||
const at = before.lastIndexOf('{{')
|
||||
if (at < 0) return null
|
||||
const frag = before.slice(at + 2)
|
||||
// 已经收口了,或者已经在写过滤器/下标了,就别再弹——那不是在选变量
|
||||
if (frag.includes('}}') || frag.includes('|') || frag.includes('[')) return null
|
||||
if (frag.length > 40 || /[\n]/.test(frag)) return null
|
||||
return { start: at, text: frag.trim() }
|
||||
}
|
||||
|
||||
const insert = (path: string) => {
|
||||
if (!query) return
|
||||
const el = ref.current
|
||||
if (!el) return
|
||||
const caret = el.selectionStart ?? value.length
|
||||
const after = value.slice(caret)
|
||||
// 全站的写法都是两边带空格:{{ vars.x }}。后端 strip 后匹配,怎么写都行,
|
||||
// 但和既有内容保持一致,不然同一个文件里两种风格
|
||||
const closed = after.startsWith('}}') ? '' : ' }}'
|
||||
const next = `${value.slice(0, query.start)}{{ ${path}${closed}${after}`
|
||||
const pos = query.start + 3 + path.length + closed.length
|
||||
onChange(next)
|
||||
setQuery(null)
|
||||
// 这是完全受控组件:onChange 会一路走到 store 再把 value 重新灌回来,
|
||||
// DOM 的光标会跳到末尾。必须等那一帧过去再自己放回去
|
||||
requestAnimationFrame(() => {
|
||||
el.focus()
|
||||
el.setSelectionRange(pos, pos)
|
||||
})
|
||||
}
|
||||
|
||||
const onKeyDown = (e: React.KeyboardEvent) => {
|
||||
if (!open) return
|
||||
// 弹层开着的时候这几个键有原生行为(换行、移光标、跳焦点),得拦下来
|
||||
if (e.key === 'ArrowDown') { e.preventDefault(); setActive((i) => (i + 1) % matches.length) }
|
||||
else if (e.key === 'ArrowUp') { e.preventDefault(); setActive((i) => (i - 1 + matches.length) % matches.length) }
|
||||
else if (e.key === 'Enter' || e.key === 'Tab') { e.preventDefault(); insert(matches[active].path) }
|
||||
else if (e.key === 'Escape') {
|
||||
// 只关弹层,别让它冒泡上去——外面 InspectorSheet 也在听 Escape
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
setQuery(null)
|
||||
}
|
||||
}
|
||||
|
||||
const common = {
|
||||
ref: ref as any,
|
||||
className: clsx('field', className),
|
||||
value: value ?? '',
|
||||
placeholder,
|
||||
spellCheck,
|
||||
onKeyDown,
|
||||
onBlur: () => setTimeout(() => setQuery(null), 120), // 等点击先落地
|
||||
onChange: (e: any) => {
|
||||
onChange(e.target.value)
|
||||
setQuery(detect(e.target.value, e.target.selectionStart ?? 0))
|
||||
},
|
||||
onClick: (e: any) => setQuery(detect(e.target.value, e.target.selectionStart ?? 0)),
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
{multiline
|
||||
? <textarea {...common} rows={rows} />
|
||||
: <input {...common} />}
|
||||
|
||||
{open && (
|
||||
// 普通文档流里的块,不是绝对定位的浮层:属性面板是 overflow-y-auto、
|
||||
// 外面 InspectorSheet 是 overflow-hidden,浮层到边缘就被切掉。
|
||||
// 这也和 MultiPick 的既有做法一致
|
||||
<div className="fade-up mt-1 overflow-hidden rounded-lg border bg-bg">
|
||||
{matches.map((v, i) => (
|
||||
<button
|
||||
key={v.path}
|
||||
className={clsx('flex w-full items-center gap-2 px-2 py-1.5 text-left',
|
||||
i === active ? 'bg-hover' : 'hover:bg-hover')}
|
||||
// onMouseDown 而不是 onClick:onClick 在 blur 之后才触发,
|
||||
// 那时候弹层已经关了
|
||||
onMouseDown={(e) => { e.preventDefault(); insert(v.path) }}
|
||||
onMouseEnter={() => setActive(i)}
|
||||
>
|
||||
<span className="mono shrink-0 text-[11px]" style={{ color: 'var(--accent)' }}>
|
||||
{v.path}
|
||||
</span>
|
||||
<span className="min-w-0 flex-1 truncate text-[10px] text-faint">
|
||||
{v.description || v.label}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
<div className="border-t px-2 py-1 text-[9.5px] text-faint">
|
||||
↑↓ 选择 · ⏎ 插入 · esc 关掉
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 排序:前缀命中优先,然后按 input → vars → nodes → 内置 的既有顺序 */
|
||||
function filterVars(variables: Variable[], q: string): Variable[] {
|
||||
const query = q.toLowerCase()
|
||||
if (!query) return variables
|
||||
const hit = variables.filter((v) => v.path.toLowerCase().includes(query))
|
||||
return hit.sort((a, b) => {
|
||||
const ap = a.path.toLowerCase().startsWith(query) ? 0 : 1
|
||||
const bp = b.path.toLowerCase().startsWith(query) ? 0 : 1
|
||||
return ap - bp
|
||||
})
|
||||
}
|
||||
@@ -84,6 +84,51 @@ console.log('=== 画布助手栏 ===')
|
||||
await page.close()
|
||||
}
|
||||
|
||||
console.log('\n=== 变量表与模板补全 ===')
|
||||
{
|
||||
const { page, errors } = await visit('/studio')
|
||||
check('没有运行时报错', errors.length === 0, errors.join(' | '))
|
||||
|
||||
await page.getByRole('button', { name: /^变量/ }).click()
|
||||
await page.waitForTimeout(700)
|
||||
const rows = await page.locator('table tbody tr').count()
|
||||
check('变量表列出了变量', rows > 0, `${rows} 行`)
|
||||
const body = await page.locator('body').innerText()
|
||||
check('列出的是可直接粘的写法', body.includes('{{ input.'), '')
|
||||
|
||||
// 抽屉是挤压式的:画布不能被压没,React Flow 容器高度归零会直接报错
|
||||
const canvasH = await page.locator('.react-flow').first()
|
||||
.evaluate((el) => el.getBoundingClientRect().height)
|
||||
check('画布没有被压没', canvasH > 120, `${Math.round(canvasH)}px`)
|
||||
await page.getByRole('button', { name: /^变量/ }).click()
|
||||
await page.waitForTimeout(400)
|
||||
|
||||
// 补全:模板取不到值会静默渲染成空字符串,补全是从源头消灭这类错误。
|
||||
// 默认那张图只有 输入→成果,没有带模板的字段,先从面板拖一个模型调用出来
|
||||
await page.locator('text=模型调用').first().click()
|
||||
await page.waitForTimeout(700)
|
||||
// 必须限定在属性面板里:底下助手栏的 Copilot 输入框也是 aside textarea,
|
||||
// 而它被这一层盖着,点它会一直超时
|
||||
const field = page.locator('.sheet-in textarea').first()
|
||||
await field.click()
|
||||
await page.keyboard.type('{{')
|
||||
await page.waitForTimeout(400)
|
||||
const hasPanel = (await page.locator('body').innerText()).includes('⏎ 插入')
|
||||
check('打 {{ 弹出候选', hasPanel)
|
||||
if (hasPanel) {
|
||||
await page.keyboard.press('Enter')
|
||||
await page.waitForTimeout(350)
|
||||
const v = await field.inputValue()
|
||||
check('选中后插入成完整写法', /\{\{ \S+ \}\}/.test(v), v.slice(0, 40))
|
||||
// 完全受控组件,插入后光标会被重置到末尾,必须自己放回去
|
||||
await page.keyboard.type('X')
|
||||
check('光标停在插入内容之后', (await field.inputValue()).endsWith('X'),
|
||||
(await field.inputValue()).slice(-20))
|
||||
}
|
||||
await shot(page, 'variables')
|
||||
await page.close()
|
||||
}
|
||||
|
||||
console.log('\n=== 动效对前庭敏感者可关 ===')
|
||||
{
|
||||
// 这套界面里动的东西不少(边在流动、节点在脉冲、面板在滑)。
|
||||
|
||||
Reference in New Issue
Block a user