mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-21 08:11:58 +00:00
4c7c0cf283
服务端给被驳回的那一步的 node.failed 带上了 reason=rejected(上一个提交)。界面上读 node.failed 的几处都认它 (05-已知遗留 (a)、总方案 X9:驳回是人的决定,不是故障),没有 reason 的老运行照旧画失败: - run/decode:问数据过程、记录页、画布右栏的时间线里,这一步状态 rejected(StepStatus 加一种),副标题写谁驳回、 备注(REJECTED_TEXT.step),原话放展开里;不是 error 级别,不当成出错 - run/trace、run/derive:NodeState 加 rejected;那一段收成 rejected,不记成「失败的节点」,下游是「未到达」不是 「被阻断」;回放投影同样是 rejected - 画布节点卡:rejected 一种读数(耗时 + 原话第一句,整句悬停),中性色、停止剪影,不挂失败红框、不抖动;小地图、 运行胶囊的计数、时间轴泳道认得它;记录页航迹的节点卡写「被驳回:原话」 - check-decode、check-run-states 各加一段:新旧两种形状
1146 lines
53 KiB
TypeScript
1146 lines
53 KiB
TypeScript
import './surface.css'
|
||
import {
|
||
createContext, memo, useCallback, useContext, useEffect, useId, useMemo, useRef, useState, type CSSProperties,
|
||
} from 'react'
|
||
import {
|
||
Background, BackgroundVariant, ControlButton, Controls, MiniMap, ReactFlow, ReactFlowProvider,
|
||
useReactFlow, useStoreApi, ViewportPortal, type AriaLabelConfig, type Edge, type MiniMapNodeProps, type Viewport,
|
||
} from '@xyflow/react'
|
||
import { Crosshair, Lock, PanelRightOpen, Route as RouteIcon, Search } from 'lucide-react'
|
||
import clsx from 'clsx'
|
||
import { FlowEdge, MomentContext, RoutingContext, type EdgeRunState, type EdgeView } from './FlowEdge'
|
||
import { openInspector, stepsFirst, useSheet } from './InspectorSheet'
|
||
import { NodeCard } from './NodeCard'
|
||
import { NODE_CATEGORIES, NODE_DEFS, type NodeDef } from './nodeDefs'
|
||
import { loopFrames } from './loops'
|
||
import { buildRoutes, NODE_WIDTH } from './routing'
|
||
import { RunTimeline, useDock } from './RunTimeline'
|
||
import { isComposing, toast } from '../components/ui'
|
||
import { runClassLabel } from '../lib/terms'
|
||
import { shortId } from '../lib/format'
|
||
import {
|
||
activeEdgesOf, edgeIdOf, edgeKey, heldEdgesOf, topology, walkedEdges, type GraphLike,
|
||
} from '../run/derive'
|
||
import {
|
||
isActivePhase, isTerminal, type NodeState, type NodeTrace, type RunPhase, type Trace,
|
||
} from '../run/trace'
|
||
import { replayTraceCached } from '../run/useNodeView'
|
||
import { EDIT_LOCK_TEXT, useStudio, type FlowNode } from '../store/studio'
|
||
import type { NodeType } from '../types'
|
||
|
||
const nodeTypes = { card: NodeCard }
|
||
const edgeTypes = { flow: FlowEdge }
|
||
|
||
/** 读得清卡片字的最低缩放。低于它,12px 的标题落到 7px 以下,光弧也细到看不见 */
|
||
const READABLE_ZOOM = 0.6
|
||
/**
|
||
* 精简档里标题换成放大的字(见 NodeCard 的 .nc-lod),0.4 倍时屏幕上仍有 11px 以上。
|
||
* 整张图在可读缩放下放不下、在这里放得下时,打开就看全图的形状,而不是只看前两列
|
||
*/
|
||
const COMPACT_ZOOM = 0.4
|
||
/** 语义缩放三档的门槛,上下各留 0.02 的回滞,免得停在门槛上来回跳 */
|
||
const LOD_FULL = 0.6
|
||
const LOD_SIGNAL = 0.35
|
||
const HYSTERESIS = 0.02
|
||
/** 手动平移、缩放之后,跟随执行让出这么久 */
|
||
const FOLLOW_PAUSE_MS = 10_000
|
||
/** 一次性时刻挂在根元素上的时长:够卡片按层级错开播完,之后撤掉,免得后来的变化再播一遍 */
|
||
const MOMENT_MS = 2600
|
||
|
||
type Lod = 'full' | 'compact' | 'signal'
|
||
|
||
/**
|
||
* cur 为 null 表示视口已经停稳:按落点直接分档,不带回滞。回滞只防"停在门槛上
|
||
* 来回跳",停稳了还带着它,从 0.5 取景回 0.6 就会一直卡在 compact(要 0.62 才回 full),
|
||
* 同样的 60% 打开时是完整卡片、定位过去却只剩标题
|
||
*/
|
||
function lodFor(zoom: number, cur: Lod | null): Lod {
|
||
const full = cur == null ? LOD_FULL - 1e-3 : cur === 'full' ? LOD_FULL - HYSTERESIS : LOD_FULL + HYSTERESIS
|
||
const signal = cur == null ? LOD_SIGNAL : cur === 'signal' ? LOD_SIGNAL + HYSTERESIS : LOD_SIGNAL - HYSTERESIS
|
||
return zoom >= full ? 'full' : zoom < signal ? 'signal' : 'compact'
|
||
}
|
||
|
||
const reducedMotion = (): boolean =>
|
||
typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches
|
||
|
||
/** 系统「减少动效」的开关,跟着系统设置实时变 */
|
||
function useReducedMotion(): boolean {
|
||
const [reduced, setReduced] = useState(reducedMotion)
|
||
useEffect(() => {
|
||
if (typeof matchMedia !== 'function') return
|
||
const mq = matchMedia('(prefers-reduced-motion: reduce)')
|
||
const sync = () => setReduced(mq.matches)
|
||
mq.addEventListener('change', sync)
|
||
return () => mq.removeEventListener('change', sync)
|
||
}, [])
|
||
return reduced
|
||
}
|
||
|
||
/**
|
||
* 程序取景一律线性插值。d3 默认的 interpolateZoom 走"先拉远再推近"的弧线,
|
||
* 半路会缩到 0.43–0.5,把画布切进 compact 档:定位到失败节点,落地时错误正文被藏了
|
||
*/
|
||
const LINEAR = 'linear' as const
|
||
|
||
// 「只看执行路径」看的是这一次运行的路径:清掉结果、开始下一次运行(含接着跑)时复位。
|
||
// 放在模块级而不是画布组件里:画布不在屏上时(去了别的页)清掉的结果也要复位
|
||
useStudio.subscribe((s, prev) => {
|
||
if (s.runPhase === prev.runPhase || !useDock.getState().pathOnly) return
|
||
if (s.runPhase === 'idle' || (isActivePhase(s.runPhase) && !isActivePhase(prev.runPhase))) {
|
||
useDock.getState().set({ pathOnly: false })
|
||
}
|
||
})
|
||
|
||
/** React Flow 自带控件和读屏描述默认是英文 */
|
||
const ARIA: Partial<AriaLabelConfig> = {
|
||
'node.a11yDescription.default': '按回车或空格选中节点,按 Delete 键删除,Esc 取消。',
|
||
'node.a11yDescription.keyboardDisabled': '按回车或空格选中节点,再用方向键移动,按 Delete 键删除,Esc 取消。',
|
||
'node.a11yDescription.ariaLiveMessage': ({ x, y }: { direction: string; x: number; y: number }) =>
|
||
`已移动选中的节点,新位置 x ${x},y ${y}`,
|
||
'edge.a11yDescription.default': '按回车或空格选中连线,再按 Delete 键删除,Esc 取消。',
|
||
'controls.ariaLabel': '画布控件',
|
||
'controls.zoomIn.ariaLabel': '放大',
|
||
'controls.zoomOut.ariaLabel': '缩小',
|
||
'controls.fitView.ariaLabel': '适应画布',
|
||
'minimap.ariaLabel': '小地图',
|
||
'handle.ariaLabel': '连接点',
|
||
}
|
||
|
||
/** 已经响应过的取景请求 */
|
||
let lastFocus: object | null = null
|
||
|
||
/** StudioState 的 getViewportCenter 由 W-studio 声明;这里只负责把实现挂上去 */
|
||
const setStudio = useStudio.setState as (patch: Record<string, unknown>) => void
|
||
|
||
// -------------------------------------------------------------------------
|
||
// 回放:航迹投影到游标那一刻(run/useNodeView 的 replayTraceCached,卡片读同一份),
|
||
// 边、小地图、回边的圈数都按那一刻画
|
||
// -------------------------------------------------------------------------
|
||
|
||
/**
|
||
* 回边兜回去了几次:目标这一次进入以来开跑的次数减一。回放截到游标。
|
||
* 走过 done 出口的那一段是上一次进入的收尾——嵌套在外层循环里时,内层每次进来
|
||
* 重新数,和循环卡片上「第几轮」同一个口径。审批恢复的重放不算又跑了一次
|
||
*/
|
||
function loopsOf(n: NodeTrace | undefined, at: number | null): number {
|
||
if (!n) return 0
|
||
let runs = 0
|
||
for (let i = n.segments.length - 1; i >= 0; i -= 1) {
|
||
const s = n.segments[i]
|
||
if (s.kind !== 'run' || s.resumed || (at != null && s.start > at)) continue
|
||
if (s.handle === 'done' && runs > 0) break
|
||
runs += 1
|
||
}
|
||
return Math.max(0, runs - 1)
|
||
}
|
||
|
||
/**
|
||
* 运行中点了一个跑过的节点:右栏滚到它的步骤。右栏归助手面板,它接这个事件
|
||
* (在对话层时先切到运行层),接了就 preventDefault;没人接时自己找步骤行描一圈
|
||
*/
|
||
function revealStep(nodeId: string): void {
|
||
const runId = useStudio.getState().run?.id
|
||
const handled = !window.dispatchEvent(new CustomEvent('agentlab:reveal-step', {
|
||
detail: { runId, nodeId }, cancelable: true,
|
||
}))
|
||
if (handled) return
|
||
const rows = document.querySelectorAll<HTMLElement>(`[data-step-status][data-node-id="${CSS.escape(nodeId)}"]`)
|
||
const row = rows[rows.length - 1]
|
||
if (!row) return
|
||
row.scrollIntoView({ block: 'center', behavior: reducedMotion() ? 'auto' : 'smooth' })
|
||
row.dataset.flash = 'focus'
|
||
setTimeout(() => { if (row.dataset.flash === 'focus') delete row.dataset.flash }, 1800)
|
||
}
|
||
|
||
/** 每条边此刻的运行态 */
|
||
function classifyEdges(
|
||
trace: Trace, phase: RunPhase, graph: GraphLike, flowing: string[],
|
||
): Map<string, EdgeRunState> {
|
||
const out = new Map<string, EdgeRunState>()
|
||
if (phase === 'idle' && !Object.keys(trace.nodes).length) return out
|
||
const flow = new Set(flowing)
|
||
const held = new Set(heldEdgesOf(trace, graph))
|
||
const walked = walkedEdges(trace, graph)
|
||
const settled = isTerminal(phase)
|
||
const stateOf = (id: string): NodeState => trace.nodes[id]?.state ?? 'idle'
|
||
for (const e of graph.edges) {
|
||
const id = edgeIdOf(e)
|
||
const target = stateOf(e.target)
|
||
const source = stateOf(e.source)
|
||
let s: EdgeRunState = 'idle'
|
||
if (flow.has(id)) s = 'flow'
|
||
else if (held.has(id)) s = 'held'
|
||
else if (target === 'blocked' && (source === 'failed' || source === 'blocked')) s = 'cut'
|
||
else if (walked.has(id)) s = 'walked'
|
||
else if (settled) s = 'unwalked'
|
||
out.set(id, s)
|
||
}
|
||
return out
|
||
}
|
||
|
||
// -------------------------------------------------------------------------
|
||
// 小地图:有运行时按状态着色,失败和等人放大;没有运行时按类型色
|
||
// -------------------------------------------------------------------------
|
||
|
||
/**
|
||
* states:每个节点此刻的状态(没有运行时为 null,按类型色画)。
|
||
* unit:小地图上 1px 合多少画布单位。失败、等人的那一圈要按屏幕像素定大小——
|
||
* 按节点尺寸定的话,70 个节点的长图里它会缩成不到 1px,正好在最需要的时候看不见
|
||
*/
|
||
const MiniStates = createContext<{ states: Record<string, NodeState>; unit: number } | null>(null)
|
||
|
||
const MINI_FILL: Partial<Record<NodeState, string>> = {
|
||
running: 'var(--st-running)',
|
||
waiting: 'var(--st-waiting)',
|
||
failed: 'var(--st-failed)',
|
||
done: 'color-mix(in srgb, var(--st-done) 70%, transparent)',
|
||
skipped: 'var(--st-skipped)',
|
||
cancelled: 'var(--st-cancelled)',
|
||
// 被驳回:人的决定,和取消同一种中性色(不画失败色)
|
||
rejected: 'var(--st-cancelled)',
|
||
suspended: 'var(--st-suspended)',
|
||
blocked: 'var(--st-blocked)',
|
||
unreached: 'color-mix(in srgb, var(--st-unreached) 45%, transparent)',
|
||
queued: 'var(--st-queued)',
|
||
}
|
||
|
||
const MiniNode = memo(function MiniNode({ id, x, y, width, height, color, borderRadius, shapeRendering, className }: MiniMapNodeProps) {
|
||
const mini = useContext(MiniStates)
|
||
const state = mini?.states[id]
|
||
const alert = state === 'failed' || state === 'waiting'
|
||
const fill = mini ? MINI_FILL[state ?? 'idle'] ?? 'var(--border-strong)' : color
|
||
// 圈:比节点每边大 3px,至少 12px 见方,线宽 2px(都是小地图上的屏幕像素)
|
||
const u = mini?.unit ?? 1
|
||
const rw = Math.max(width + 6 * u, 12 * u)
|
||
const rh = Math.max(height + 6 * u, 12 * u)
|
||
return (
|
||
<>
|
||
<rect className={clsx('react-flow__minimap-node', className, state && `sf-mm-${state}`)}
|
||
x={x} y={y} rx={borderRadius} ry={borderRadius} width={width} height={height}
|
||
style={{ fill }} shapeRendering={shapeRendering} />
|
||
{/* 失败、等人在小地图上放大一圈:大图里它们是唯一需要人去处理的地方 */}
|
||
{alert && (
|
||
<rect className={clsx('sf-mm-ring', `sf-mm-${state}`)}
|
||
x={x + width / 2 - rw / 2} y={y + height / 2 - rh / 2} width={rw} height={rh}
|
||
rx={3 * u} ry={3 * u} style={{ strokeWidth: 2 * u }} />
|
||
)}
|
||
</>
|
||
)
|
||
})
|
||
|
||
const typeColor = (n: { data?: unknown }): string => {
|
||
const type = (n.data as { nodeType?: NodeType } | undefined)?.nodeType
|
||
return type ? `var(--nt-${type})` : 'var(--text-faint)'
|
||
}
|
||
|
||
// -------------------------------------------------------------------------
|
||
// Copilot 这一轮删掉的节点:原地留一道虚线框,停一下再淡出
|
||
// -------------------------------------------------------------------------
|
||
|
||
/** 虚线框停留的总时长,和 surface.css 里 sf-ghost-out 的时长一致 */
|
||
const GHOST_MS = 2400
|
||
|
||
interface Ghost {
|
||
id: string
|
||
x: number
|
||
y: number
|
||
w: number
|
||
h: number
|
||
label: string
|
||
type: NodeType
|
||
}
|
||
|
||
const ghostOf = (n: FlowNode): Ghost => ({
|
||
id: n.id, x: n.position.x, y: n.position.y,
|
||
w: n.measured?.width ?? NODE_WIDTH, h: n.measured?.height ?? 80,
|
||
label: n.data.label || NODE_DEFS[n.data.nodeType]?.label || n.id, type: n.data.nodeType,
|
||
})
|
||
|
||
/**
|
||
* 助手改完图,新加的节点有入场、改过的有标记,删掉的却是一声不响就没了——回执写着
|
||
* 「删除 1」,画布上看不出删的是哪一个、原来在哪。在它原来的位置画一道虚线框、
|
||
* 写上名字,停两秒淡出。只由这一轮落定触发、只播一次;关了动效就不画(回执里照样写着)
|
||
*/
|
||
function useRemovedGhosts(): { seq: number; items: Ghost[] } | null {
|
||
const [ghosts, setGhosts] = useState<{ seq: number; items: Ghost[] } | null>(null)
|
||
useEffect(() => {
|
||
// 这一轮开始前的样子:删掉的节点可能在流式途中就没了,落定时 store 里已经找不到
|
||
let before = new Map<string, Ghost>()
|
||
let timer = 0
|
||
const unsub = useStudio.subscribe((s, prev) => {
|
||
if (s.copilot.active && !prev.copilot.active) before = new Map(prev.nodes.map((n) => [n.id, ghostOf(n)]))
|
||
if (s.copilotTurns === prev.copilotTurns) return
|
||
const turn = s.copilotTurns[s.copilotTurns.length - 1]
|
||
if (!turn || turn.phase !== 'done' || turn.outcome !== 'applied') return
|
||
if (prev.copilotTurns.find((t) => t.id === turn.id)?.phase === 'done') return
|
||
const items = (turn.diff?.removed ?? []).flatMap((id) => before.get(id) ?? [])
|
||
before = new Map()
|
||
if (!items.length || reducedMotion()) return
|
||
clearTimeout(timer)
|
||
setGhosts((g) => ({ seq: (g?.seq ?? 0) + 1, items }))
|
||
timer = window.setTimeout(() => setGhosts(null), GHOST_MS)
|
||
})
|
||
return () => {
|
||
unsub()
|
||
clearTimeout(timer)
|
||
}
|
||
}, [])
|
||
return ghosts
|
||
}
|
||
|
||
// -------------------------------------------------------------------------
|
||
// 循环体底框:垫在边和卡片下面,只画不接指针
|
||
// -------------------------------------------------------------------------
|
||
|
||
/**
|
||
* 每个循环一块淡色区域,左上角写「循环体 · 最多 N 轮」;在跑的循环再补一句「第 k 轮」,
|
||
* 回放时是游标那一刻的轮次。几何只跟着节点、连线重算,事件进来只换标签上的轮次
|
||
*/
|
||
const LoopFrames = memo(function LoopFrames({ nodes, edges, trace }: {
|
||
nodes: FlowNode[]
|
||
edges: Edge[]
|
||
trace: Trace
|
||
}) {
|
||
const frames = useMemo(() => loopFrames(nodes, edges), [nodes, edges])
|
||
if (!frames.length) return null
|
||
return (
|
||
<ViewportPortal>
|
||
<div className="sf-loops" aria-hidden>
|
||
<svg className="sf-loops-svg">
|
||
{frames.map((f) => <path key={f.id} className="sf-loop-frame" data-loop={f.id} d={f.path} />)}
|
||
</svg>
|
||
{frames.map((f) => {
|
||
const n = trace.nodes[f.id]
|
||
const round = n?.state === 'running' && n.iteration ? n.iteration : null
|
||
return (
|
||
<div key={f.id} className={clsx('sf-loop-tag', round && 'is-running')} data-loop={f.id}
|
||
style={{ transform: `translate(${f.labelX}px, ${f.labelY}px) translateY(-50%)` }}>
|
||
<span className="sf-loop-glyph">↻</span>
|
||
<span>循环体 · 最多 {f.max} 轮</span>
|
||
{round && <b className="tnum">第 {round} 轮</b>}
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
</ViewportPortal>
|
||
)
|
||
})
|
||
|
||
// -------------------------------------------------------------------------
|
||
// 双击空白处快速添加:在光标处搜类型、回车放下。和节点库读同一份节点定义
|
||
// -------------------------------------------------------------------------
|
||
|
||
const QUICK_W = 248
|
||
const QUICK_H = 312
|
||
|
||
function QuickAdd({ x, y, bounds, onPick, onClose }: {
|
||
/** 双击处,相对画布容器的像素 */
|
||
x: number
|
||
y: number
|
||
bounds: { w: number; h: number }
|
||
onPick: (type: NodeType) => void
|
||
onClose: () => void
|
||
}) {
|
||
const [query, setQuery] = useState('')
|
||
const [cur, setCur] = useState(0)
|
||
const box = useRef<HTMLDivElement>(null)
|
||
const list = useRef<HTMLDivElement>(null)
|
||
const defs = useMemo(() => {
|
||
const q = query.trim().toLowerCase()
|
||
// 按节点库的分组顺序排;搜的时候名字里有的排在只是说明里提到的前面——搜「分」
|
||
// 要的是「条件分支」,不是说明里写着「分派」的协作节点
|
||
const all = NODE_CATEGORIES.flatMap((c) => Object.values(NODE_DEFS).filter((d) => d.category === c))
|
||
if (!q) return all
|
||
const named = (d: NodeDef) => d.label.toLowerCase().includes(q) || d.type.includes(q)
|
||
return [...all.filter(named), ...all.filter((d) => !named(d) && d.description.toLowerCase().includes(q))]
|
||
}, [query])
|
||
const at = Math.min(cur, Math.max(0, defs.length - 1))
|
||
const listId = useId()
|
||
|
||
// 点到外面就收起;它是个一次性的小面板,不该留在那儿挡画布
|
||
useEffect(() => {
|
||
const onDown = (e: PointerEvent) => { if (!box.current?.contains(e.target as Node)) onClose() }
|
||
document.addEventListener('pointerdown', onDown, true)
|
||
return () => document.removeEventListener('pointerdown', onDown, true)
|
||
}, [onClose])
|
||
useEffect(() => {
|
||
list.current?.querySelector<HTMLElement>(`[data-i="${at}"]`)?.scrollIntoView({ block: 'nearest' })
|
||
}, [at])
|
||
|
||
const onKeyDown = (e: React.KeyboardEvent) => {
|
||
if (isComposing(e)) return
|
||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||
e.preventDefault()
|
||
setCur(Math.max(0, Math.min(defs.length - 1, at + (e.key === 'ArrowDown' ? 1 : -1))))
|
||
} else if (e.key === 'Enter') {
|
||
e.preventDefault()
|
||
if (defs[at]) onPick(defs[at].type)
|
||
} else if (e.key === 'Escape') {
|
||
// 用掉这一下:别再让它去关属性面板、清运行结果
|
||
e.preventDefault()
|
||
e.stopPropagation()
|
||
onClose()
|
||
}
|
||
}
|
||
// 贴着光标放,放不下就往回收,不伸出画布
|
||
const left = Math.max(8, Math.min(x, bounds.w - QUICK_W - 8))
|
||
const top = Math.max(8, Math.min(y, bounds.h - QUICK_H - 8))
|
||
const optId = (i: number) => `${listId}-${i}`
|
||
return (
|
||
<div ref={box} className="sf-quick sheet-in nodrag nopan nowheel" style={{ left, top, width: QUICK_W }}
|
||
role="dialog" aria-label="快速添加节点" data-esc-layer onKeyDown={onKeyDown}>
|
||
<label className="sf-quick-search">
|
||
<Search size={12} aria-hidden />
|
||
<input
|
||
autoFocus
|
||
value={query}
|
||
placeholder="添加节点:搜索名称或用途"
|
||
role="combobox"
|
||
aria-expanded
|
||
aria-controls={listId}
|
||
aria-activedescendant={defs[at] ? optId(at) : undefined}
|
||
aria-autocomplete="list"
|
||
onChange={(e) => { setQuery(e.target.value); setCur(0) }}
|
||
/>
|
||
</label>
|
||
<div className="sf-quick-list" role="listbox" id={listId} ref={list} aria-label="节点类型">
|
||
{defs.map((d, i) => {
|
||
const Icon = d.icon
|
||
return (
|
||
<div key={d.type} id={optId(i)} data-i={i} role="option" aria-selected={i === at}
|
||
className={clsx('sf-quick-item', `nt-${d.type}`, i === at && 'is-active')}
|
||
title={d.description}
|
||
onPointerEnter={() => setCur(i)}
|
||
onClick={() => onPick(d.type)}>
|
||
<span className="sf-quick-icon"><Icon size={12} aria-hidden /></span>
|
||
<span className="min-w-0 flex-1 truncate">{d.label}</span>
|
||
<span className="sf-dim">{d.category}</span>
|
||
</div>
|
||
)
|
||
})}
|
||
{!defs.length && <div className="sf-quick-empty">没有与「{query.trim()}」匹配的节点</div>}
|
||
</div>
|
||
<div className="sf-quick-foot">↑↓ 选择 · ↵ 添加到此处 · Esc 关闭</div>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
// -------------------------------------------------------------------------
|
||
|
||
function CanvasInner() {
|
||
const rf = useRef<HTMLDivElement>(null)
|
||
const { screenToFlowPosition, fitView, setViewport, setCenter, getViewport, getNodes, getNodesBounds,
|
||
getInternalNode, zoomTo } = useReactFlow()
|
||
const rfStore = useStoreApi()
|
||
const nodes = useStudio((s) => s.nodes)
|
||
const edges = useStudio((s) => s.edges)
|
||
const activeEdges = useStudio((s) => s.activeEdges)
|
||
const fitRequest = useStudio((s) => s.fitRequest)
|
||
const trace = useStudio((s) => s.trace)
|
||
const runPhase = useStudio((s) => s.runPhase)
|
||
const runClass = useStudio((s) => s.trace.runClass ?? s.run?.run_class)
|
||
const runVersion = useStudio((s) => s.run?.version ?? null)
|
||
const replayAt = useStudio((s) => s.replayAt)
|
||
const focusRequest = useStudio((s) => s.focusRequest)
|
||
const selectedId = useStudio((s) => s.selectedId)
|
||
const follow = useStudio((s) => s.follow)
|
||
const pathOnly = useDock((s) => s.pathOnly)
|
||
const sheetOpen = useSheet((s) => s.open)
|
||
// 动作从 getState 取:整个 useStudio() 解构会订阅全部字段,每个 token 都让画布重渲染一次
|
||
const actions = useStudio.getState()
|
||
|
||
const graph = useMemo<GraphLike>(() => ({ nodes, edges }), [nodes, edges])
|
||
const topo = useMemo(() => topology(graph), [graph])
|
||
const hasRun = runPhase !== 'idle' || Object.keys(trace.nodes).length > 0
|
||
const active = isActivePhase(runPhase)
|
||
const reduced = useReducedMotion()
|
||
// 正式运行期间画布只读:它跑的是已发布的不可变版本,此刻改图既改不了它,
|
||
// 还会让事件对到已经被删掉的节点上。探索运行只关删除和连线,拖动照常
|
||
const formalLock = active && runClass === 'formal'
|
||
|
||
// 走线方案整张图算一次。节点尺寸要等 React Flow 量完才有,量到之后
|
||
// nodes 会变,这里跟着重算,端口和车道就落到实测尺寸上。
|
||
const routes = useMemo(() => buildRoutes(nodes, edges), [nodes, edges])
|
||
// 走廊挪不出地方的出口标签:节点 → 出口 → 最多多宽。卡片按它截短,不伸进拐弯
|
||
const exitRooms = useMemo(() => {
|
||
const out = new Map<string, Record<string, number>>()
|
||
for (const e of edges) {
|
||
const exit = routes.get(e.id)?.exit
|
||
if (!exit) continue
|
||
const rooms = out.get(e.source) ?? {}
|
||
rooms[exit.handle] = Math.min(rooms[exit.handle] ?? Infinity, exit.room)
|
||
out.set(e.source, rooms)
|
||
}
|
||
return out
|
||
}, [routes, edges])
|
||
|
||
// ---- 此刻(实时或回放游标那一刻)的航迹 ----
|
||
// graph 直接用 store 里的 nodes / edges 数组:卡片那边按同样的引用取,缓存才对得上
|
||
const view = useMemo(() => {
|
||
if (replayAt == null) return { trace, phase: runPhase, flowing: activeEdges }
|
||
const t = replayTraceCached(trace, replayAt, graph)
|
||
return { trace: t, phase: t.phase, flowing: activeEdgesOf(t, graph) }
|
||
}, [trace, runPhase, activeEdges, replayAt, graph])
|
||
|
||
const edgeStates = useMemo(
|
||
() => classifyEdges(view.trace, view.phase, graph, view.flowing),
|
||
[view, graph],
|
||
)
|
||
// 「只看执行路径」只在运行真正结束、看的也是结束那一刻时压暗:运行中、等人、挂起时
|
||
// 下游还没走到不等于"不在路径上",压暗会把正要执行的那一截藏起来
|
||
const pathView = pathOnly && isTerminal(runPhase) && isTerminal(view.phase)
|
||
const visited = useMemo(() => {
|
||
const out = new Set<string>()
|
||
for (const [id, n] of Object.entries(view.trace.nodes)) {
|
||
if (n.count > 0 || n.state === 'running' || n.state === 'waiting' || n.state === 'skipped') out.add(id)
|
||
}
|
||
return out
|
||
}, [view.trace])
|
||
|
||
// ---- 一次性时刻:只由实时事件触发,回放和补发的历史直接落终态 ----
|
||
const [moment, setMoment] = useState<{ kind: 'start' | 'success' | 'failed'; seq: number; origin: number } | null>(null)
|
||
useEffect(() => {
|
||
let last = 0
|
||
let seq = 0
|
||
let timer: ReturnType<typeof setTimeout> | null = null
|
||
const unsub = useStudio.subscribe((s, prev) => {
|
||
// trace 可能被脚本、热更新短暂置空:订阅回调在 setState 里抛,崩的是调用 setState 的那一方
|
||
if (s.runPhase === prev.runPhase || s.trace?.lastReplay || s.replayAt != null) return
|
||
let kind: 'start' | 'success' | 'failed' | null = null
|
||
if ((prev.runPhase === 'idle' || prev.runPhase === 'queued') && s.runPhase === 'running') {
|
||
// 大图的开场点亮会变成一场烟花,跳过
|
||
kind = s.nodes.length <= 60 ? 'start' : null
|
||
} else if (isActivePhase(prev.runPhase) && s.runPhase === 'succeeded') kind = 'success'
|
||
else if (isActivePhase(prev.runPhase) && s.runPhase === 'failed') kind = 'failed'
|
||
const now = Date.now()
|
||
// 同一秒最多一个时刻:接连来的两个只留第一个
|
||
if (!kind || now - last < 1000) return
|
||
last = now
|
||
seq += 1
|
||
const failedId = s.trace?.failedNodeId
|
||
const origin = kind === 'failed' && failedId ? topology({ nodes: s.nodes, edges: s.edges }).rank[failedId] ?? 0 : 0
|
||
setMoment({ kind, seq, origin })
|
||
if (timer) clearTimeout(timer)
|
||
timer = setTimeout(() => setMoment(null), MOMENT_MS)
|
||
})
|
||
return () => {
|
||
unsub()
|
||
if (timer) clearTimeout(timer)
|
||
}
|
||
}, [])
|
||
const momentCtx = useMemo(() => (moment ? { kind: moment.kind, seq: moment.seq } : null), [moment])
|
||
|
||
// ---- 边:运行态 + 回边 + 选中时的入边 / 出边 ----
|
||
const decorated = useMemo<Edge[]>(() => {
|
||
const editing = !hasRun && selectedId != null
|
||
return edges.map((e) => {
|
||
const state = edgeStates.get(e.id) ?? 'idle'
|
||
const back = topo.back.has(edgeKey(e))
|
||
const sf: EdgeView = {
|
||
state, back, rank: topo.rank[e.target] ?? 0,
|
||
loops: back ? loopsOf(view.trace.nodes[e.target], replayAt) : 0,
|
||
}
|
||
const className = clsx(
|
||
`sf-e-${state}`,
|
||
back && 'sf-e-back',
|
||
editing && (e.source === selectedId ? 'sf-e-out' : e.target === selectedId ? 'sf-e-in' : 'sf-e-dim'),
|
||
pathView && (state === 'flow' || state === 'walked' || state === 'held' ? 'sf-e-onpath' : 'sf-e-offpath'),
|
||
)
|
||
// animated 一律关掉:React Flow 的 animated 是一条永远在走的虚线,方向和"是否真在流"都说不清
|
||
return { ...e, type: 'flow', className, animated: false, data: { ...e.data, sf } }
|
||
})
|
||
}, [edges, edgeStates, topo, view.trace, replayAt, hasRun, selectedId, pathView])
|
||
|
||
// ---- 节点:注入拓扑层级(一次性时刻按它错开)和出口标签的宽度上限,只看执行路径时压暗没走的 ----
|
||
// 按原节点对象缓存:拖一个节点只换那一个的副本,其余卡片的 data 引用不变,memo 生效
|
||
const nodeCache = useRef(new WeakMap<FlowNode, { key: string; out: FlowNode }>())
|
||
const flowNodes = useMemo(() => nodes.map((n) => {
|
||
const rank = topo.rank[n.id] ?? 0
|
||
const off = pathView && !visited.has(n.id)
|
||
const exitRoom = exitRooms.get(n.id)
|
||
const key = `${rank}|${off ? 1 : 0}|${exitRoom ? JSON.stringify(exitRoom) : ''}`
|
||
const hit = nodeCache.current.get(n)
|
||
if (hit && hit.key === key) return hit.out
|
||
const out: FlowNode = {
|
||
...n,
|
||
className: clsx(n.className, off && 'sf-offpath') || undefined,
|
||
style: { ...n.style, '--rank': rank } as CSSProperties,
|
||
data: { ...n.data, rank, ...(exitRoom ? { exitRoom } : {}) } as FlowNode['data'],
|
||
}
|
||
nodeCache.current.set(n, { key, out })
|
||
return out
|
||
}), [nodes, topo, pathView, visited, exitRooms])
|
||
|
||
// ---- 小地图的状态表 ----
|
||
// 小地图按节点和视口的并集缩进 150×100。这里只按节点算(视口一动就重算不值得),
|
||
// 得到的比例偏小一点,圈在屏幕上也就小一点,不影响看见
|
||
const miniUnit = useMemo(() => {
|
||
if (!hasRun || !nodes.length) return 1
|
||
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity
|
||
for (const n of nodes) {
|
||
x0 = Math.min(x0, n.position.x)
|
||
y0 = Math.min(y0, n.position.y)
|
||
x1 = Math.max(x1, n.position.x + (n.measured?.width ?? NODE_WIDTH))
|
||
y1 = Math.max(y1, n.position.y + (n.measured?.height ?? 80))
|
||
}
|
||
return Math.max(1, (x1 - x0) / 150, (y1 - y0) / 100)
|
||
}, [hasRun, nodes])
|
||
const miniStates = useMemo(() => {
|
||
if (!hasRun) return null
|
||
const states: Record<string, NodeState> = {}
|
||
for (const n of nodes) states[n.id] = view.trace.nodes[n.id]?.state ?? 'idle'
|
||
return { states, unit: miniUnit }
|
||
}, [hasRun, nodes, view.trace, miniUnit])
|
||
|
||
// ---- 语义缩放:只在跨档时写一次属性,--zoom 和缩放读数直接写到 DOM,不经过 React ----
|
||
// 移动途中带回滞;停稳后(onMoveEnd)按落点重新分档
|
||
const zoomText = useRef<HTMLSpanElement>(null)
|
||
const lod = useRef<Lod>('full')
|
||
const applyZoom = useCallback((zoom: number, settled = false) => {
|
||
const el = rf.current
|
||
if (!el) return
|
||
el.style.setProperty('--zoom', String(Math.round(zoom * 1000) / 1000))
|
||
if (zoomText.current) zoomText.current.textContent = `${Math.round(zoom * 100)}%`
|
||
const next = lodFor(zoom, settled ? null : lod.current)
|
||
if (next !== lod.current || !el.dataset.lod) {
|
||
lod.current = next
|
||
el.dataset.lod = next
|
||
}
|
||
}, [])
|
||
useEffect(() => {
|
||
applyZoom(rfStore.getState().transform[2], true)
|
||
return rfStore.subscribe((s, prev) => {
|
||
if (s.transform[2] !== prev.transform[2]) applyZoom(s.transform[2])
|
||
})
|
||
}, [rfStore, applyZoom])
|
||
|
||
// ---- 取景 ----
|
||
/**
|
||
* 正在飞的那一次程序取景(跟随、点名定位、Copilot 跟镜头)。画布尺寸恰好在
|
||
* 半路变了(运行一开始航迹坞就升起来)时,下面的保持中心会打断它;这里记着
|
||
* 目标,打断之后重新飞一次,不然镜头停在半路、要看的节点还在视口外
|
||
*/
|
||
const flight = useRef<{ ids: string[]; zoomAtLeast?: number; until: number } | null>(null)
|
||
const retarget = useRef<(() => void) | null>(null)
|
||
|
||
// 画布尺寸变了(航迹坞出现、变量抽屉挤进来)时保持视口中心不动。React Flow
|
||
// 默认钉住左上角,坞一升起来,画面下半截的节点就被它盖住了
|
||
const box = useRef<HTMLDivElement>(null)
|
||
useEffect(() => {
|
||
const el = box.current
|
||
if (!el) return
|
||
let last = { w: el.clientWidth, h: el.clientHeight }
|
||
const ro = new ResizeObserver(() => {
|
||
const w = el.clientWidth
|
||
const h = el.clientHeight
|
||
if (last.w && last.h && (w !== last.w || h !== last.h)) {
|
||
const { x, y, zoom } = getViewport()
|
||
void setViewport({ x: x + (w - last.w) / 2, y: y + (h - last.h) / 2, zoom })
|
||
if (flight.current && Date.now() < flight.current.until) requestAnimationFrame(() => retarget.current?.())
|
||
}
|
||
last = { w, h }
|
||
})
|
||
ro.observe(el)
|
||
return () => ro.disconnect()
|
||
}, [getViewport, setViewport])
|
||
|
||
const pausedUntil = useRef(0)
|
||
/**
|
||
* 人手动平移、缩放的那一下:开始时记下视口,结束时真的动过才让跟随让路。
|
||
* 只是按一下(点空白处取消选择、正式运行时点节点)d3 也报开始和结束,那不算
|
||
*/
|
||
const userMove = useRef<Viewport | null>(null)
|
||
const [followPaused, setFollowPaused] = useState(false)
|
||
const pauseFollow = useCallback(() => {
|
||
pausedUntil.current = Date.now() + FOLLOW_PAUSE_MS
|
||
setFollowPaused(true)
|
||
}, [])
|
||
useEffect(() => {
|
||
if (!followPaused) return
|
||
const t = setTimeout(() => setFollowPaused(false), FOLLOW_PAUSE_MS)
|
||
return () => clearTimeout(t)
|
||
}, [followPaused])
|
||
|
||
/**
|
||
* 打开一张图时的取景。整图放得下、缩放不低于可读下限就整图居中;放不下就
|
||
* 不再硬塞——以入口为左锚、按可读缩放只取前几列,其余交给小地图。以前一律
|
||
* 整图塞进来,12 节点的图缩到 0.2,标题只剩 3px。窄画布另有一档,见下面 narrow。
|
||
*/
|
||
const frame = useCallback((duration: number) => {
|
||
const el = rf.current
|
||
const all = getNodes()
|
||
if (!el || !all.length) return
|
||
const bounds = getNodesBounds(all)
|
||
const W = el.clientWidth
|
||
const H = el.clientHeight
|
||
const PAD = 40
|
||
if (!W || !H || !bounds.width) return
|
||
const fit = Math.min((W - 2 * PAD) / bounds.width, (H - 2 * PAD) / bounds.height)
|
||
if (fit >= READABLE_ZOOM) {
|
||
void fitView({ padding: 0.12, maxZoom: 1.1, minZoom: READABLE_ZOOM, duration, interpolate: LINEAR })
|
||
return
|
||
}
|
||
// 按列聚类
|
||
const xs = [...new Set(all.map((n) => Math.round(n.position.x)))].sort((a, b) => a - b)
|
||
const cols: number[] = []
|
||
for (const x of xs) if (!cols.length || x - cols[cols.length - 1] > 60) cols.push(x)
|
||
const spanOf = (k: number) => (cols[Math.min(k, cols.length) - 1] ?? bounds.x) + NODE_WIDTH - bounds.x
|
||
// 窄画布(1024 宽、两边栏都开着时只剩五百来 px):可读缩放下连前三列都放不下,
|
||
// 打开只看得见两张半卡片。这时退到精简档:整张图放得下就看全图,放不下就多看几列。
|
||
// 宽画布照旧按可读缩放——精简卡没有摘要和遥测,能看清就不该让人先放大一次
|
||
const narrow = cols.length >= 3 && spanOf(3) * READABLE_ZOOM > W - 2 * PAD
|
||
if (narrow && fit >= COMPACT_ZOOM) {
|
||
void fitView({ padding: 0.12, maxZoom: READABLE_ZOOM, minZoom: COMPACT_ZOOM, duration, interpolate: LINEAR })
|
||
return
|
||
}
|
||
// 取前四列的跨度定缩放
|
||
let zoom = narrow
|
||
? Math.max(COMPACT_ZOOM, Math.min(READABLE_ZOOM, (W - 2 * PAD) / spanOf(4)))
|
||
: Math.min(1, Math.max(READABLE_ZOOM, (W - 2 * PAD) / Math.max(spanOf(4), NODE_WIDTH)))
|
||
// 竖直方向放得下就别再放大:宁可多看几列
|
||
const vfit = (H - 2 * PAD) / bounds.height
|
||
if (vfit >= (narrow ? COMPACT_ZOOM : READABLE_ZOOM)) zoom = Math.min(zoom, vfit)
|
||
const entries = all.filter((n) => (n.data as FlowNode['data']).nodeType === 'input')
|
||
const anchor = entries[0] ?? all.reduce((a, b) => (a.position.x <= b.position.x ? a : b))
|
||
const anchorH = anchor.measured?.height ?? 80
|
||
const x = PAD - bounds.x * zoom
|
||
// 竖直方向按"这一屏看得见的那几列"居中,不按整张图:斜着往下长的图按整体居中,
|
||
// 入口那几列会贴在顶上,下面空一大块
|
||
const reach = bounds.x + (W - PAD) / zoom
|
||
const seen = all.filter((n) => n.position.x <= reach)
|
||
const top = Math.min(...seen.map((n) => n.position.y))
|
||
const bottom = Math.max(...seen.map((n) => n.position.y + (n.measured?.height ?? 80)))
|
||
const y = (bottom - top) * zoom <= H - 2 * PAD
|
||
? (H - (bottom - top) * zoom) / 2 - top * zoom
|
||
: H / 2 - (anchor.position.y + anchorH / 2) * zoom
|
||
void setViewport({ x, y, zoom }, { duration, interpolate: LINEAR })
|
||
}, [fitView, getNodes, getNodesBounds, setViewport])
|
||
|
||
// 打开、换图、自动排版、Copilot 生成完之后取景。位置全变了却不重新取景,
|
||
// 人会盯着一片空白以为图没了。要等 React Flow 量完节点尺寸才算得准
|
||
const framed = useRef(false)
|
||
useEffect(() => {
|
||
let raf = 0
|
||
let tries = 0
|
||
const duration = framed.current && !reducedMotion() ? 280 : 0
|
||
const attempt = () => {
|
||
const all = getNodes()
|
||
if (all.length && !all.every((n) => n.measured?.width) && tries++ < 40) {
|
||
raf = requestAnimationFrame(attempt)
|
||
return
|
||
}
|
||
framed.current = true
|
||
frame(duration)
|
||
}
|
||
raf = requestAnimationFrame(attempt)
|
||
return () => cancelAnimationFrame(raf)
|
||
}, [fitRequest, frame, getNodes])
|
||
|
||
/** 把一组节点摆进视口。都已经在视口里就不动:画面不该无缘无故地跳 */
|
||
const bringIntoView = useCallback((ids: string[], opts: { force?: boolean; zoomAtLeast?: number } = {}) => {
|
||
const el = rf.current
|
||
if (!el) return false
|
||
const rects = ids.map((id) => getInternalNode(id)).filter(Boolean).map((n) => ({
|
||
x: n!.internals.positionAbsolute.x, y: n!.internals.positionAbsolute.y,
|
||
w: n!.measured.width ?? NODE_WIDTH, h: n!.measured.height ?? 80,
|
||
}))
|
||
if (!rects.length) return false
|
||
const { x: vx, y: vy, zoom } = getViewport()
|
||
const M = 32
|
||
const inside = rects.every((r) => r.x * zoom + vx >= M && r.y * zoom + vy >= M
|
||
&& (r.x + r.w) * zoom + vx <= el.clientWidth - M && (r.y + r.h) * zoom + vy <= el.clientHeight - M)
|
||
if (inside && !opts.force) return false
|
||
const minX = Math.min(...rects.map((r) => r.x))
|
||
const minY = Math.min(...rects.map((r) => r.y))
|
||
const maxX = Math.max(...rects.map((r) => r.x + r.w))
|
||
const maxY = Math.max(...rects.map((r) => r.y + r.h))
|
||
const z = Math.max(zoom, opts.zoomAtLeast ?? 0)
|
||
// 几个节点同时开跑、包围盒放不下时,只追第一个,免得视口来回跳
|
||
const fits = (maxX - minX) * z <= el.clientWidth - 2 * M && (maxY - minY) * z <= el.clientHeight - 2 * M
|
||
const target = fits ? { cx: (minX + maxX) / 2, cy: (minY + maxY) / 2 }
|
||
: { cx: rects[0].x + rects[0].w / 2, cy: rects[0].y + rects[0].h / 2 }
|
||
const duration = reducedMotion() ? 0 : 320
|
||
flight.current = { ids, zoomAtLeast: opts.zoomAtLeast, until: Date.now() + duration + 80 }
|
||
void setCenter(target.cx, target.cy, { zoom: z, duration, interpolate: LINEAR })
|
||
return true
|
||
}, [getInternalNode, getViewport, setCenter])
|
||
retarget.current = () => {
|
||
const f = flight.current
|
||
if (f) bringIntoView(f.ids, { force: true, zoomAtLeast: f.zoomAtLeast })
|
||
}
|
||
|
||
// 跟随执行:有节点实时开跑、而它在视口外时平移过去。只由 node.started 驱动,
|
||
// 不新增任何动画循环;同一帧里一起开跑的并成一次
|
||
useEffect(() => {
|
||
let raf = 0
|
||
let pending: string[] = []
|
||
const unsub = useStudio.subscribe((s, prev) => {
|
||
if (s.trace === prev.trace || !s.trace || !s.follow || s.replayAt != null || s.trace.lastReplay) return
|
||
for (const [id, n] of Object.entries(s.trace.nodes)) {
|
||
if (n.state === 'running' && !n.looping && n.count > (prev.trace?.nodes[id]?.count ?? 0)) pending.push(id)
|
||
}
|
||
if (!pending.length || raf) return
|
||
raf = requestAnimationFrame(() => {
|
||
raf = 0
|
||
const ids = pending
|
||
pending = []
|
||
// 人正拖着画布时也不抢:松手之后再按"动过没有"决定暂不暂停
|
||
if (Date.now() < pausedUntil.current || userMove.current) return
|
||
bringIntoView(ids, { zoomAtLeast: 0.5 })
|
||
})
|
||
})
|
||
return () => {
|
||
unsub()
|
||
cancelAnimationFrame(raf)
|
||
}
|
||
}, [bringIntoView])
|
||
|
||
// Copilot 搭图时镜头跟着它刚动过的节点:落在视口外就平移过去,不改缩放(新节点
|
||
// 本来就落在上游旁边或视口中心附近的空地上)。人一旦手动平移、缩放,这一轮就不再跟;
|
||
// 节点还在滑向落点时每帧都会变,飞行途中不重新起飞,免得镜头追着它抖
|
||
const copilotOff = useRef(false)
|
||
useEffect(() => {
|
||
let raf = 0
|
||
const unsub = useStudio.subscribe((s, prev) => {
|
||
if (s.copilot.active && !prev.copilot.active) copilotOff.current = false
|
||
if (!s.copilot.active || !s.copilotCursor || copilotOff.current) return
|
||
if (s.copilotCursor === prev.copilotCursor && s.nodes === prev.nodes) return
|
||
if (raf) return
|
||
raf = requestAnimationFrame(() => {
|
||
raf = 0
|
||
const st = useStudio.getState()
|
||
if (!st.copilot.active || !st.copilotCursor || copilotOff.current || userMove.current) return
|
||
if (flight.current && Date.now() < flight.current.until) return
|
||
bringIntoView([st.copilotCursor])
|
||
})
|
||
})
|
||
return () => {
|
||
unsub()
|
||
cancelAnimationFrame(raf)
|
||
}
|
||
}, [bringIntoView])
|
||
|
||
// 右栏、时间轴、胶囊请求取景:点名要看它,就算已在视口里也居中一下。
|
||
// focusRequest 每次请求都是新对象(seq +1),同一个节点连点两次也会再取景
|
||
useEffect(() => {
|
||
// 画布重新挂载(从别的页回来)时 store 里还留着上一次的请求,不该再取景一次
|
||
if (!focusRequest || focusRequest === lastFocus) return
|
||
lastFocus = focusRequest
|
||
let raf = 0
|
||
let tries = 0
|
||
const go = () => {
|
||
if (!getInternalNode(focusRequest.id)?.measured?.width && tries++ < 20) {
|
||
raf = requestAnimationFrame(go)
|
||
return
|
||
}
|
||
if (bringIntoView([focusRequest.id], { force: true, zoomAtLeast: READABLE_ZOOM })) pauseFollow()
|
||
}
|
||
go()
|
||
return () => cancelAnimationFrame(raf)
|
||
}, [focusRequest, bringIntoView, getInternalNode, pauseFollow])
|
||
|
||
// 调色板点击添加节点时落在视口中心。ReactFlowProvider 在这里面,外面拿不到 useReactFlow
|
||
useEffect(() => {
|
||
const getViewportCenter = () => {
|
||
const r = rf.current?.getBoundingClientRect()
|
||
if (!r) return { x: 0, y: 0 }
|
||
return screenToFlowPosition({ x: r.left + r.width / 2, y: r.top + r.height / 2 })
|
||
}
|
||
setStudio({ getViewportCenter })
|
||
return () => setStudio({ getViewportCenter: null })
|
||
}, [screenToFlowPosition])
|
||
|
||
const onDrop = useCallback(
|
||
(event: React.DragEvent) => {
|
||
event.preventDefault()
|
||
const type = event.dataTransfer.getData('application/agentlab-node') as NodeType
|
||
if (!type) return
|
||
if (formalLock) {
|
||
toast.warn(EDIT_LOCK_TEXT.formal)
|
||
return
|
||
}
|
||
actions.addNode(type, screenToFlowPosition({ x: event.clientX, y: event.clientY }))
|
||
},
|
||
[actions, screenToFlowPosition, formalLock],
|
||
)
|
||
|
||
const ghosts = useRemovedGhosts()
|
||
|
||
// ---- 双击空白处快速添加 ----
|
||
const [quick, setQuick] = useState<{ x: number; y: number; flow: { x: number; y: number } } | null>(null)
|
||
const closeQuick = useCallback(() => setQuick(null), [])
|
||
const onPaneDoubleClick = (e: React.MouseEvent) => {
|
||
if (!(e.target as Element).classList?.contains('react-flow__pane')) return
|
||
if (formalLock) {
|
||
toast.warn(EDIT_LOCK_TEXT.formal)
|
||
return
|
||
}
|
||
if (useStudio.getState().copilot.active) {
|
||
toast.info(EDIT_LOCK_TEXT.copilot)
|
||
return
|
||
}
|
||
const r = box.current?.getBoundingClientRect()
|
||
if (!r) return
|
||
setQuick({ x: e.clientX - r.left, y: e.clientY - r.top, flow: screenToFlowPosition({ x: e.clientX, y: e.clientY }) })
|
||
}
|
||
const pickQuick = (type: NodeType) => {
|
||
const at = quick?.flow
|
||
setQuick(null)
|
||
// 双击处是卡片的中心,不是左上角
|
||
if (at) actions.addNode(type, { x: at.x - NODE_WIDTH / 2, y: at.y - 40 })
|
||
}
|
||
|
||
// ---- 运行中点节点:先看它的步骤,属性面板是次级入口 ----
|
||
// 看运行(进行中或回放)时点了跑过的节点,属性面板不自动盖上右栏(见 InspectorSheet);
|
||
// 这里给一个明确的入口,双击节点也能开
|
||
// 选中了却没开面板,只会是「先看步骤」那条路。运行结束后它照样留着:不然节点选中着、
|
||
// 面板没开、入口也没了
|
||
const peekId = selectedId && sheetOpen !== selectedId ? selectedId : null
|
||
const peekNode = peekId ? nodes.find((n) => n.id === peekId) : undefined
|
||
const peekLabel = peekNode ? peekNode.data.label || NODE_DEFS[peekNode.data.nodeType]?.label || peekNode.id : ''
|
||
|
||
const rootStyle = useMemo(
|
||
() => (moment?.kind === 'failed' ? { '--moment-origin': moment.origin } as CSSProperties : undefined),
|
||
[moment],
|
||
)
|
||
// 「适配全图」是人主动要看全:不设可读下限。打开时的取景策略在 frame() 里
|
||
const fitAll = useMemo(
|
||
() => ({ padding: 0.12, maxZoom: 1.1, duration: reduced ? 0 : 240, interpolate: LINEAR }),
|
||
[reduced],
|
||
)
|
||
const showFollow = active && replayAt == null && follow
|
||
|
||
return (
|
||
<div className="relative min-h-0 flex-1" ref={box}>
|
||
<RoutingContext.Provider value={routes}>
|
||
<MomentContext.Provider value={momentCtx}>
|
||
<MiniStates.Provider value={miniStates}>
|
||
<ReactFlow
|
||
ref={rf}
|
||
className="sf-canvas"
|
||
style={rootStyle}
|
||
data-run-phase={view.phase}
|
||
data-run-class={hasRun ? runClass ?? 'exploratory' : undefined}
|
||
data-replay={replayAt != null ? '1' : undefined}
|
||
data-path-only={pathView ? '1' : undefined}
|
||
data-readonly={formalLock ? '1' : undefined}
|
||
data-moment={moment?.kind}
|
||
data-moment-seq={moment?.seq}
|
||
nodes={flowNodes}
|
||
edges={decorated}
|
||
nodeTypes={nodeTypes}
|
||
edgeTypes={edgeTypes}
|
||
onNodesChange={actions.onNodesChange}
|
||
onEdgesChange={actions.onEdgesChange}
|
||
onConnect={actions.onConnect}
|
||
onNodeClick={(_, node) => {
|
||
if (!stepsFirst(node.id)) {
|
||
// 点的是已经选中、面板却没开的节点(先看过步骤、运行随后结束了):
|
||
// 选中不变就不会触发面板的订阅,这里直接点名打开
|
||
openInspector(node.id)
|
||
return
|
||
}
|
||
// 先请右栏滚过去、再选中:右栏接手时会先收起盖在它上面的属性面板(select(null)),
|
||
// 顺序反过来的话刚点的这个节点就被取消选中,「…的配置」入口也跟着没了
|
||
revealStep(node.id)
|
||
actions.select(node.id)
|
||
}}
|
||
onNodeDoubleClick={(_, node) => openInspector(node.id)}
|
||
onPaneClick={() => actions.select(null)}
|
||
onDoubleClick={onPaneDoubleClick}
|
||
zoomOnDoubleClick={false}
|
||
onNodeMouseEnter={(_, node) => { if (hasRun) useDock.getState().set({ canvasHover: node.id }) }}
|
||
onNodeMouseLeave={() => { if (hasRun) useDock.getState().set({ canvasHover: null }) }}
|
||
onMoveStart={(event, vp) => {
|
||
// 程序取景(跟随、聚焦)没有 event;有 event 的是人在拖、在滚轮
|
||
if (event) userMove.current = vp
|
||
}}
|
||
onMoveEnd={(event, vp) => {
|
||
applyZoom(vp.zoom, true)
|
||
// 只认人手的那一下收尾。程序取景飞到一半被人抓住时,d3 先收掉那段动画,
|
||
// React Flow 却把它的收尾推迟到下一拍才报——那时人的拖动已经开始,这条
|
||
// 不带 event 的收尾要是也来结账,就把人的起点吃掉了,拖完跟随照样不暂停
|
||
if (!event) return
|
||
const from = userMove.current
|
||
if (!from) return
|
||
userMove.current = null
|
||
const moved = Math.abs(vp.x - from.x) > 2 || Math.abs(vp.y - from.y) > 2
|
||
|| Math.abs(vp.zoom - from.zoom) > 1e-3
|
||
if (!moved) return
|
||
// 人接过了镜头:程序取景不再续飞,Copilot 这一轮也不再跟
|
||
flight.current = null
|
||
if (useStudio.getState().copilot.active) copilotOff.current = true
|
||
if (active) pauseFollow()
|
||
}}
|
||
onDrop={onDrop}
|
||
onDragOver={(e) => {
|
||
e.preventDefault()
|
||
e.dataTransfer.dropEffect = formalLock ? 'none' : 'move'
|
||
}}
|
||
nodesDraggable={!formalLock}
|
||
nodesConnectable={!active}
|
||
edgesReconnectable={!active}
|
||
deleteKeyCode={active ? null : ['Backspace', 'Delete']}
|
||
minZoom={0.2}
|
||
maxZoom={2}
|
||
defaultEdgeOptions={{ type: 'flow' }}
|
||
ariaLabelConfig={ARIA}
|
||
proOptions={{ hideAttribution: true }}
|
||
>
|
||
{/* 两级网格:细点每格一个,粗点每五格一个,像工程图纸的坐标纸 */}
|
||
<Background id="minor" variant={BackgroundVariant.Dots} gap={18} size={1} color="var(--grid-dot)" />
|
||
<Background id="major" variant={BackgroundVariant.Dots} gap={90} size={1.6}
|
||
color="var(--grid-dot-major)" bgColor="transparent" />
|
||
<Controls showInteractive={false} fitViewOptions={fitAll}>
|
||
<ControlButton className="sf-zoom-btn"
|
||
onClick={() => void zoomTo(1, { duration: reduced ? 0 : 240, interpolate: LINEAR })}
|
||
title="当前缩放比例 · 点击恢复为 100%" aria-label="恢复为 100% 缩放">
|
||
<span ref={zoomText} className="tnum" />
|
||
</ControlButton>
|
||
</Controls>
|
||
<MiniMap
|
||
pannable
|
||
zoomable
|
||
// 遮罩压得太狠会把视口外的失败、等人一起压暗——那恰好是最需要看见的地方
|
||
maskColor="color-mix(in srgb, var(--bg) 58%, transparent)"
|
||
maskStrokeColor="var(--text-faint)"
|
||
maskStrokeWidth={1}
|
||
nodeColor={typeColor}
|
||
nodeComponent={MiniNode}
|
||
style={{ width: 150, height: 100 }}
|
||
ariaLabel={hasRun ? '小地图(按运行状态着色)' : '小地图'}
|
||
/>
|
||
<LoopFrames nodes={nodes} edges={edges} trace={view.trace} />
|
||
{ghosts && (
|
||
<ViewportPortal>
|
||
{ghosts.items.map((g) => (
|
||
<div key={`${ghosts.seq}:${g.id}`} className={clsx('sf-ghost', `nt-${g.type}`)} data-ghost={g.id}
|
||
style={{ transform: `translate(${g.x}px, ${g.y}px)`, width: g.w, height: g.h }} aria-hidden>
|
||
<span className="sf-ghost-tag">已删除 · {g.label}</span>
|
||
</div>
|
||
))}
|
||
</ViewportPortal>
|
||
)}
|
||
</ReactFlow>
|
||
</MiniStates.Provider>
|
||
</MomentContext.Provider>
|
||
</RoutingContext.Provider>
|
||
|
||
{/* 画布顶上的浮条排成一行:跟随在左,只读横幅居中;放不下时横幅折到下一行、
|
||
再不够就截掉说明文字。各自绝对定位的话,笔记本宽度下横幅会盖住「恢复」 */}
|
||
{(formalLock || showFollow || peekId) && (
|
||
<div className="sf-float">
|
||
{showFollow && (
|
||
<div className={clsx('sf-follow', followPaused && 'is-paused')}>
|
||
<Crosshair size={11} />
|
||
{followPaused ? (
|
||
<>
|
||
<span>已暂停跟随</span>
|
||
<button type="button" onClick={() => { pausedUntil.current = 0; setFollowPaused(false) }}>
|
||
恢复
|
||
</button>
|
||
</>
|
||
) : (
|
||
<span>跟随执行</span>
|
||
)}
|
||
</div>
|
||
)}
|
||
{peekId && (
|
||
<button type="button" className="sf-peek" onClick={() => openInspector(peekId)}
|
||
title="属性面板会覆盖右栏的运行视图,点击底部横条即可返回;也可双击节点打开">
|
||
<PanelRightOpen size={11} aria-hidden />
|
||
<span className="sf-peek-name">{peekLabel}</span>
|
||
<span>的配置</span>
|
||
</button>
|
||
)}
|
||
{formalLock && (
|
||
<div className="sf-banner" role="status">
|
||
<Lock size={12} />
|
||
<span className="sf-banner-main">{runClassLabel('formal', runVersion)} 进行中 · 画布只读</span>
|
||
<span className="sf-banner-dim">当前运行的是已发布的不可变版本,运行结束后可继续编辑</span>
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
|
||
{quick && box.current && (
|
||
<QuickAdd x={quick.x} y={quick.y} bounds={{ w: box.current.clientWidth, h: box.current.clientHeight }}
|
||
onPick={pickQuick} onClose={closeQuick} />
|
||
)}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** 画布下方的航迹坞:有运行时才出现,没有运行不占地方 */
|
||
function RunDock() {
|
||
const trace = useStudio((s) => s.trace)
|
||
const runPhase = useStudio((s) => s.runPhase)
|
||
const nodes = useStudio((s) => s.nodes)
|
||
const edges = useStudio((s) => s.edges)
|
||
const replayAt = useStudio((s) => s.replayAt)
|
||
const hovered = useStudio((s) => s.hoveredNodeId)
|
||
const follow = useStudio((s) => s.follow)
|
||
const run = useStudio((s) => s.run)
|
||
const canvasHover = useDock((s) => s.canvasHover)
|
||
const pathOnly = useDock((s) => s.pathOnly)
|
||
const open = useDock((s) => s.open)
|
||
const height = useDock((s) => s.height)
|
||
const playing = useDock((s) => s.playing)
|
||
const speed = useDock((s) => s.speed)
|
||
const compress = useDock((s) => s.compress)
|
||
const setDock = useDock((s) => s.set)
|
||
const { setReplayAt, setHoveredNode, focusNode, setFollow } = useStudio.getState()
|
||
|
||
const graph = useMemo(() => ({ nodes, edges }), [nodes, edges])
|
||
const ui = useMemo(() => ({ open, height, playing, speed, compress }), [open, height, playing, speed, compress])
|
||
if (runPhase === 'idle' && trace.startedAt == null && !Object.keys(trace.nodes).length) return null
|
||
|
||
const title = run
|
||
? `${runClassLabel(run.run_class ?? trace.runClass ?? 'exploratory', run.version)} ${shortId(run.id)}`
|
||
: runClassLabel(trace.runClass ?? 'exploratory')
|
||
return (
|
||
<RunTimeline
|
||
trace={trace}
|
||
graph={graph}
|
||
title={title}
|
||
replayAt={replayAt}
|
||
onReplayAt={setReplayAt}
|
||
hoveredNodeId={hovered ?? canvasHover}
|
||
onHoverNode={setHoveredNode}
|
||
onFocusNode={focusNode}
|
||
ui={ui}
|
||
onUi={setDock}
|
||
extra={(
|
||
<>
|
||
{/* 跟随只对还在跑的运行有意义;执行路径要等运行结束才定下来 */}
|
||
{isActivePhase(runPhase) && (
|
||
<label className="tl-toggle" title="跟着正在执行的节点平移画布;手动平移后暂停 10 秒">
|
||
<input type="checkbox" checked={follow} aria-label="跟随执行"
|
||
onChange={(e) => setFollow(e.target.checked)} />
|
||
<Crosshair size={11} /> <span className="tl-tt">跟随执行</span>
|
||
</label>
|
||
)}
|
||
{isTerminal(runPhase) && (
|
||
<label className="tl-toggle" title="调暗本次未执行的节点和连线,只突出执行路径">
|
||
<input type="checkbox" checked={pathOnly} aria-label="只看执行路径"
|
||
onChange={(e) => setDock({ pathOnly: e.target.checked })} />
|
||
<RouteIcon size={11} /> <span className="tl-tt">只看执行路径</span>
|
||
</label>
|
||
)}
|
||
</>
|
||
)}
|
||
/>
|
||
)
|
||
}
|
||
|
||
export function FlowCanvas() {
|
||
return (
|
||
<ReactFlowProvider>
|
||
<div className="flex h-full w-full flex-col">
|
||
<CanvasInner />
|
||
<RunDock />
|
||
</div>
|
||
</ReactFlowProvider>
|
||
)
|
||
}
|