Files
AgentLab/frontend/src/lib/leave.ts
T
ThearchyHelios 3e311ec551 第三波收尾:跨模块整合、全站扫尾、检查脚本整合;网页工具认得本机代理的 fake-ip
整合(3c-integrate)
- 公共层:跨天时长(formatSpan / formatOffset / formatLapse)、本机署名(lib/actor)、检索分类色令牌、
  数据源目录(useDatasources)收进公共层;失败说明能一次列出多个写坏的自定义工具,查询超时指到数据源的查询时限
- 管理页收尾:设置页接入统一的离开前确认;检查脚本不再把沙箱里的真实值打进输出
- 画布 ↔ 右栏:点出具印章、点节点能跳到右栏对应位置;「定位」直接聚焦到出问题的字段
- 回答流 / 问数据 / 记录页:失败信息带「去哪修」且不再被翻译两遍;协作团队的结局按执行截取;
  等审批时断流重连、而运行已在空档里跑完的,补全事件

扫尾(3c-sweep)
- 术语统一:开始运行 / 继续运行、工作流最大步数;未保存的运行统一叫「未保存的工作流」(后端新运行同步)
- 沙箱报错不再带 Python 异常类名;新增 check-tokens,未定义的 CSS 变量为 0,两个旧别名迁移后删除

检查脚本(3c-checks)
- check-ui 跟上新界面(运行列表按 data 属性找行),加上令牌与选中态的回归断言,输出里的名字打码
- 新增 check-all:按顺序跑全部检查、汇总退出码,--repeat 列出偶发项
- 单节出错只记这一节失败、不再带崩整个脚本;外壳遥测浮层「重新检测」时焦点不再掉到 body

网页工具与 fake-ip
- Clash / mihomo 一类代理开 TUN + fake-ip 时,查什么都先拿到 198.18 段的假地址,被当成内网拦截。
  新增 AGENTLAB_HTTP_TOOL_FAKE_IP_RANGES(默认空):落在段里的解析结果改向公共 DNS(DoH)核实真实地址,
  公网放行、内网照拦、核实不了按拦截处理。DoH 默认先问境外的,境内公共 DNS 会对被封锁的域名返回污染结果
- 列表型环境变量接受逗号分隔(原来注释说逗号分隔、实际只认 JSON 数组);域名解析改为异步,不再阻塞事件循环

验证(沙箱):tsc 干净,pytest 866 passed(两遍),检查脚本全绿。
2026-09-27 23:37:32 +08:00

176 lines
7.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 离开前确认:页面有没保存的改动时登记一道守卫,站内的每一次换页都先经过它——
* 左侧导航、页面里的链接、⌘K、⌥ 数字、浏览器前进后退、代码里的 navigate()——
* 关页和刷新也会让浏览器问一句。
*
* 以前设置页自己在捕获阶段拦 <a> 的点击:⌘K 和快捷键直接调 navigate(),浏览器
* 后退也不走 <a>,改到一半的表单照样丢。路由换成 data router 之后,react-router 的
* useBlocker 拦得住所有换页;但一个 router 同时只认一个 blocker,所以由外壳挂唯一
* 那一个(useLeaveBlocker),页面只往这里登记,别自己调 useBlocker。
*
* useLeaveGuard(dirty > 0, () => confirmDialog({ title: `有 ${dirty} 项设置还没保存`, … }))
*
* 接入时的几条约定:
* - 页面原来自己拦 <a>、自己挂 beforeunload 的那套要删掉:两套并存,点一下导航问两遍;
* - 页面里已有的「先问再跳」(切标签前自己 confirm 一次、再 navigate)也要么删掉、交给
* 这里在 pathname 变化时问,要么跳转时带 leavePass()。否则先问一遍,跳的时候守卫还在,
* 又问一遍。useTabRoute 的 navigate 不收参数、带不了通行证,所以只能删掉预问;
* - 不要自己调 react-router 的 useBlocker。
*/
import { useEffect, useRef } from 'react'
import { NavigationType, parsePath, useBlocker } from 'react-router-dom'
import type { BlockerFunction, NavigateOptions, Path } from 'react-router-dom'
export interface LeaveGuard {
/**
* 这一次换页要不要拦。next 为 null 表示去向还没定(canLeave() 不带目标时)。
* 默认:路径变了就拦;只改 ?query 或 #hash 不算,同一页里换个筛选不会丢表单
*/
blocks?: (next: Path | null, current: Path) => boolean
/** 拦下之后怎么问:true 放行,false 留下。抛错按留下算 */
confirm: (next: Path | null) => Promise<boolean>
/** 关页、刷新时要不要让浏览器问一句,默认要。浏览器只给通用文案,写不了自己的话 */
unload?: boolean
}
const guards = new Set<LeaveGuard>()
const pathChanged = (next: Path | null, current: Path) => next == null || next.pathname !== current.pathname
function blocking(next: Path | null, current: Path): LeaveGuard[] {
return [...guards].filter((g) => {
try {
return (g.blocks ?? pathChanged)(next, current)
} catch {
return true
}
})
}
const onBeforeUnload = (e: BeforeUnloadEvent) => {
e.preventDefault()
e.returnValue = ''
}
let unloadOn = false
/** 只在有守卫时挂 beforeunload:常驻的监听会让浏览器的往返缓存失效 */
function syncUnload() {
const want = [...guards].some((g) => g.unload !== false)
if (want === unloadOn || typeof window === 'undefined') return
unloadOn = want
if (want) window.addEventListener('beforeunload', onBeforeUnload)
else window.removeEventListener('beforeunload', onBeforeUnload)
}
/** 登记一道守卫,返回撤销函数。组件里用 useLeaveGuard,它管登记和撤销的时机 */
export function registerLeaveGuard(guard: LeaveGuard): () => void {
guards.add(guard)
syncUnload()
return () => {
guards.delete(guard)
syncUnload()
}
}
/** 一道一道地问:同时弹两个确认框,人不知道先答哪个。有一道说留下就留下 */
async function ask(list: LeaveGuard[], next: Path | null): Promise<boolean> {
for (const g of list) {
let ok = false
try {
ok = await g.confirm(next)
} catch {
ok = false
}
if (!ok) return false
}
return true
}
const here = (): Path => ({ pathname: location.pathname, search: location.search, hash: location.hash })
/**
* 有没保存的改动时拦住离开。active 为 false 时什么都不登记。
* confirm、blocks 每次渲染取最新的,不必包 useCallback。
*/
export function useLeaveGuard(
active: boolean,
confirm: (next: Path | null) => Promise<boolean>,
opts?: Pick<LeaveGuard, 'blocks' | 'unload'>,
): void {
const latest = useRef({ confirm, blocks: opts?.blocks })
latest.current = { confirm, blocks: opts?.blocks }
const unload = opts?.unload
useEffect(() => {
if (!active) return
return registerLeaveGuard({
confirm: (next) => latest.current.confirm(next),
blocks: (next, current) => (latest.current.blocks ?? pathChanged)(next, current),
unload,
})
}, [active, unload])
}
/**
* 先问、再做。「新对话」「新建工作流」要先建出东西、建完才跳:等到跳的时候才问,
* 人点了「留下」,东西已经建出来了。所以先问一遍;问过之后的那次跳转带上
* leavePass(),不再问第二遍。没有拦着的守卫就直接是 true。
* to 省略表示去向还没定:只要有守卫在就问。
*/
export function canLeave(to?: string): Promise<boolean> {
const next = to == null ? null : { pathname: '/', search: '', hash: '', ...parsePath(to) }
const list = blocking(next, here())
return list.length ? ask(list, next) : Promise.resolve(true)
}
const PASS = '__leaveConfirmed'
/** 给 canLeave() 问过的那一次跳转放行:navigate(to, leavePass(opts)) */
export function leavePass<T extends NavigateOptions>(opts?: T): T {
const state = opts?.state && typeof opts.state === 'object' ? opts.state : {}
return { ...opts, state: { ...state, [PASS]: true } } as T
}
/** 拦下时那几道守卫:问的时候就问它们,不再重新挑(后退时地址已经变过一次了) */
let held: LeaveGuard[] = []
const shouldBlock: BlockerFunction = ({ currentLocation, nextLocation, historyAction }) => {
// 后退前进不认通行证:它存在历史记录的 state 里,退回那一条时还在
if (historyAction !== NavigationType.Pop && (nextLocation.state as Record<string, unknown> | null)?.[PASS]) return false
held = blocking(nextLocation, currentLocation)
return held.length > 0
}
/**
* 外壳挂一次(App 里)。一个 router 同时只认一个 blocker:页面别自己调 useBlocker,
* 用 useLeaveGuard 登记。
*/
export function useLeaveBlocker(): void {
const blocker = useBlocker(shouldBlock)
const latest = useRef(blocker)
latest.current = blocker
// 正在问的是哪一次:StrictMode 下 effect 会跑两遍,同一次别问两回
const asking = useRef<string | null>(null)
useEffect(() => {
if (blocker.state !== 'blocked') {
asking.current = null
return
}
const key = blocker.location.key
if (asking.current === key) return
asking.current = key
void ask(held, blocker.location).then((ok) => {
const b = latest.current
// 问的时候人又点了别处(比如又按了后退),这一问就作废,归新的那次
if (b.state !== 'blocked' || b.location.key !== key) return
if (ok) b.proceed()
else b.reset()
})
}, [blocker])
}
// 开发期挂到 window 上,好让 check-shell 登记一道测试守卫,看各条换页的路都拦得住。
// 生产构建里去掉
if (import.meta.env.DEV && typeof window !== 'undefined') {
;(window as any).__leave = { register: registerLeaveGuard, count: () => guards.size }
}