mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 14:25:51 +00:00
feat(workflow): add workflow engine, /workflows panel, /ultracode skill
将 feat/sdk-backend 分支中 workflow 相关的 20 个 commit 压缩为单 commit: - 工作流引擎核心:phase / agent / parallel / pipeline 编排原语(packages/workflow-engine/) - /workflows 面板:三区焦点布局(顶部 run tabs + 左侧 phase 侧栏 + 右侧 agent 列表) - /ultracode skill:多 agent workflow 编排入口 - 进度存储 / journal / notification 系统 - WorkflowService 生命周期管理 + SentryErrorBoundary - 脚本沙箱:禁用 dynamic import()、JSON args 防御性归一化 - journal 与 named-workflow 路径统一在 projectRoot - 错误处理:parallel/pipeline hooks 错误日志、failure routing、semaphore abort - workflow 工具升级为 core 工具 + PascalCase 命名 Co-Authored-By: glm-5.1 <zai-org@claude-code-best.win>
This commit is contained in:
124
packages/workflow-engine/examples/registry-demo.ts
Normal file
124
packages/workflow-engine/examples/registry-demo.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* registry 多后端路由演示(mock adapter,无需 API key)。
|
||||
*
|
||||
* 两个 adapter:strong(被 researcher 路由命中)+ fast(默认)。
|
||||
* 脚本里 agent({agentType:'researcher'}) → strong,其余 → fast。
|
||||
* 证明 agent 后端可通过 AgentAdapterRegistry 插拔 + 路由,引擎不关心实现。
|
||||
*
|
||||
* 用法:bun run packages/workflow-engine/examples/registry-demo.ts
|
||||
*/
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
AgentAdapterRegistry,
|
||||
createFileJournalStore,
|
||||
createHostHandle,
|
||||
runWorkflow,
|
||||
type AgentAdapter,
|
||||
type AgentRunParams,
|
||||
type AgentRunResult,
|
||||
type WorkflowPorts,
|
||||
} from '@claude-code-best/workflow-engine'
|
||||
|
||||
const strongAdapter: AgentAdapter = {
|
||||
id: 'strong',
|
||||
capabilities: { structuredOutput: true, tools: true },
|
||||
async run(p: AgentRunParams): Promise<AgentRunResult> {
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: `[strong] ← ${p.prompt}`,
|
||||
usage: { outputTokens: 1 },
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const fastAdapter: AgentAdapter = {
|
||||
id: 'fast',
|
||||
capabilities: { structuredOutput: false },
|
||||
async run(p: AgentRunParams): Promise<AgentRunResult> {
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: `[fast] ← ${p.prompt}`,
|
||||
usage: { outputTokens: 1 },
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const registry = new AgentAdapterRegistry()
|
||||
.register(strongAdapter)
|
||||
.register(fastAdapter)
|
||||
.route({ kind: 'agentType', agentType: 'researcher', adapter: 'strong' })
|
||||
.default('fast')
|
||||
|
||||
const SCRIPT = `
|
||||
export const meta = { name: 'registry-demo', description: 'multi-adapter routing' }
|
||||
phase('Route')
|
||||
const research = await agent('深度调研任务', { agentType: 'researcher', label: 'research' })
|
||||
const quick = await agent('快速小任务', { label: 'quick' })
|
||||
return { research, quick }
|
||||
`
|
||||
|
||||
function makePorts(runsDir: string): WorkflowPorts {
|
||||
return {
|
||||
// registry 优先,agentRunner 仅作形状占位(不会被调到)
|
||||
agentRunner: { runAgentToResult: async () => ({ kind: 'dead' }) },
|
||||
agentAdapterRegistry: registry,
|
||||
progressEmitter: {
|
||||
emit: e => {
|
||||
if (e.type === 'phase_started') console.log(`\n━ phase: ${e.phase}`)
|
||||
else if (e.type === 'agent_done') {
|
||||
const out =
|
||||
e.result.kind === 'ok'
|
||||
? String(e.result.output)
|
||||
: `[${e.result.kind}]`
|
||||
console.log(` ✓ ${e.label} → ${out}`)
|
||||
}
|
||||
},
|
||||
},
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'demo',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete() {},
|
||||
fail() {},
|
||||
kill() {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await registry.initializeAll()
|
||||
try {
|
||||
const result = await runWorkflow({
|
||||
script: SCRIPT,
|
||||
runId: `demo-${Date.now()}`,
|
||||
ports: makePorts(join(tmpdir(), 'wf-registry-demo')),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
})
|
||||
console.log(`\n■ ${result.status}`)
|
||||
if (result.status === 'completed') {
|
||||
const ret = result.returnValue as { research: string; quick: string }
|
||||
console.log(
|
||||
`research(agentType:researcher) → ${ret.research.startsWith('[strong]') ? 'strong adapter ✓' : '??'}`,
|
||||
)
|
||||
console.log(
|
||||
`quick(默认) → ${ret.quick.startsWith('[fast]') ? 'fast adapter ✓' : '??'}`,
|
||||
)
|
||||
}
|
||||
} finally {
|
||||
await registry.disposeAll()
|
||||
}
|
||||
}
|
||||
74
packages/workflow-engine/examples/research-report/README.md
Normal file
74
packages/workflow-engine/examples/research-report/README.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# research-report —— 库优先运行示例
|
||||
|
||||
用 `@claude-code-best/workflow-engine` **直接**运行一个 workflow,绕开 Workflow 工具与核心 `runAgent`。
|
||||
|
||||
## 状态
|
||||
|
||||
- **引擎层**:完整且测试覆盖 **99.65% 行 / 99.20% 函数**(workflow-engine 包 112 个 mock 测试全绿)。
|
||||
- **本 example**:编排逻辑(`parallel` / `pipeline` / `schema` / `args`)经 mock 端到端验证;**真实 LLM 已跑通**(直连 Anthropic SDK)。
|
||||
- **定位**:库 API 与引擎逻辑的**参考实现 + 冒烟示范**,不是生产服务——见下方「生产就绪」。
|
||||
|
||||
## 它演示了什么
|
||||
|
||||
- **库可独立使用**:`run.ts` 只 `import { runWorkflow, ... } from '@claude-code-best/workflow-engine'`,自己组装 7 个端口,不依赖 `src/` 任何核心模块。
|
||||
- **agent 后端直连 Anthropic SDK**:`agentRunner` 调 `client.messages.create`,子 agent = 一次模型调用(不经核心 `runAgent`、不经 Workflow 工具)。
|
||||
- **真实 LLM + 结构化输出**:`agent(schema)` → prompt 追加 JSON 指令 → 提取 JSON → `validateAgainstSchema`(Ajv)校验,失败回退 `dead`。
|
||||
- **引擎能力全覆盖**:`parallel`(屏障,多角度 fan-out)→ `pipeline`(无屏障,逐条深挖)→ `phase` / `log` / `args`。
|
||||
|
||||
## 运行
|
||||
|
||||
```bash
|
||||
ANTHROPIC_API_KEY=sk-... \
|
||||
bun run packages/workflow-engine/examples/research-report/run.ts "Edge Computing"
|
||||
```
|
||||
|
||||
环境变量:
|
||||
|
||||
- `ANTHROPIC_API_KEY`(必填)
|
||||
- `ANTHROPIC_MODEL`:默认 `claude-sonnet-4-5`
|
||||
- `WORKFLOW_API_CONCURRENCY`:API 并发上限,默认 `3`(见下)。低 tier 可设 `1` 串行
|
||||
- `RESEARCH_RUNS_DIR`:journal 目录,默认 `~/.claude/workflow-runs`(resume 时复用)
|
||||
|
||||
## 健壮性与排错
|
||||
|
||||
runner 内置了几项让真实 API 跑得稳的处理:
|
||||
|
||||
- **API 并发限制**:`llmAgent` 经独立信号量限并发(默认 3),**独立于引擎的 CPU 级 semaphore**——LLM API 对并发远比 CPU 敏感,按 cores(可能 14)放并发会触发 429。用 `WORKFLOW_API_CONCURRENCY` 调。
|
||||
- **429/5xx 重试**:指数退避(500ms → 1s → 2s → 4s,最多 4 次);连接/超时错误也重试。
|
||||
- **SDK 日志关闭**:`new Anthropic({ logLevel: 'off' })`(options 优先级最高,压过 `ANTHROPIC_LOG` env)。否则 SDK 会打 `[log_xxxxx] sending request {…}` 这种完整请求 JSON。
|
||||
- **错误摘要精简**:失败只打 `HTTP 429 rate_limit_error` 这种短行,不打印含 request body 的整段 message。
|
||||
- **synthesize 防 JSON**:prompt 明确禁止把输入的 `deepFindings` JSON 原样粘进报告。
|
||||
|
||||
排错速查:
|
||||
|
||||
| 现象 | 原因 / 处理 |
|
||||
|------|------|
|
||||
| `HTTP 429 ...` 频繁 | 降 `WORKFLOW_API_CONCURRENCY=1`(或 2) |
|
||||
| agent `✗ [dead]` 多 | 模型未按 schema 返回 JSON;换更强模型或放宽 schema |
|
||||
| `[log_xxx] sending request` 刷屏 | 不应再出现(已 `logLevel:'off'`);若仍出现检查 env 是否覆盖 |
|
||||
| 报告被截断 | synthesize 已 `maxTokens:8192`;仍不够可改 workflow 脚本 |
|
||||
|
||||
## 文件
|
||||
|
||||
| 文件 | 作用 |
|
||||
|------|------|
|
||||
| `research-report.workflow.mjs` | workflow 脚本(编排逻辑,纯 JS,引擎沙箱执行) |
|
||||
| `run.ts` | runner:组装端口 + 直连 SDK + 运行 + 终端进度 |
|
||||
| (同级 `../smoke.ts`) | 最小冒烟入口(3 次调用,秒级验证通路) |
|
||||
|
||||
## 扩展点
|
||||
|
||||
- **联网调研**:给 `llmAgent` 的 `messages.create` 加 `tools: [{ type: 'web_search_20250305' }]`(Anthropic server-side web search),research 角度即可联网。
|
||||
- **命名命令复用**:把 `research-report.workflow.mjs` 复制到项目 `.claude/workflows/research-report.mjs`,即可通过 `/research-report` 或 Workflow 工具运行(同一脚本,两种入口)。
|
||||
- **token 预算**:`runWorkflow({ budgetTotal: 200000 })` 设上限;脚本内用 `budget.remaining()` 自适应规模。
|
||||
- **resume**:同 `runId` + `resume: true` 重放 journal,已完成的 agent 不重跑。
|
||||
|
||||
## 生产就绪(诚实)
|
||||
|
||||
本 example 验证的是**库的 API 与引擎编排逻辑**,不是生产服务。要上生产还差:
|
||||
|
||||
- **真实 LLM 压测**:长 workflow、大量并发、中断/resume 的真实场景验证(mock 覆盖不到模型行为)。
|
||||
- **核心 adapter 的 v1 延期项**:`budgetTotal` 注入、skip/retry UI、worktree 隔离、StructuredOutput 完整接入(本 example 用 prompt+JSON 解析,比核心真实路径弱)。
|
||||
- **错误恢复**:journal resume 只在 mock 验证过;真实中途崩溃的重放正确性未压测。
|
||||
|
||||
引擎核心逻辑(并发 / 预算 / journal / schema)有 99.65% 覆盖的 mock 测试兜底,可作为基础继续建。
|
||||
@@ -0,0 +1,124 @@
|
||||
// research-report.workflow.mjs
|
||||
// 技术研究报告 workflow。
|
||||
// 由 run.ts 通过 @claude-code-best/workflow-engine 的 runWorkflow() 直接执行——
|
||||
// 不经 Workflow 工具、不经核心 runAgent。脚本内的 agent / parallel / pipeline /
|
||||
// phase / log / args 均为引擎运行时注入的全局(见 src/engine/script.ts 的沙箱)。
|
||||
//
|
||||
// 编排:多角度并行调研(parallel 屏障)→ 逐条深挖(pipeline 无屏障)→ 综合成报告。
|
||||
|
||||
export const meta = {
|
||||
name: 'research-report',
|
||||
description:
|
||||
'Multi-angle tech research → deep-read → synthesize into a Markdown report',
|
||||
whenToUse: '调研一个技术主题:从多个角度并行研究、逐条深挖、综合成结构化报告',
|
||||
phases: [
|
||||
{ title: 'Research', detail: '多角度并行调研(parallel 屏障)' },
|
||||
{ title: 'DeepRead', detail: '逐条深挖(pipeline 无屏障)' },
|
||||
{ title: 'Synthesize', detail: '综合成 Markdown 报告' },
|
||||
],
|
||||
}
|
||||
|
||||
// agent(schema) 让子 agent 返回「校验对象」而非纯文本。
|
||||
const ANGLE_SCHEMA = {
|
||||
type: 'object',
|
||||
required: ['angle', 'findings'],
|
||||
properties: {
|
||||
angle: { type: 'string', description: '本次调研的角度名' },
|
||||
findings: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['claim', 'evidence'],
|
||||
properties: {
|
||||
claim: { type: 'string', description: '一句话结论' },
|
||||
evidence: { type: 'string', description: '依据/来源/理由' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const DEEP_SCHEMA = {
|
||||
type: 'object',
|
||||
required: ['claim', 'analysis', 'confidence'],
|
||||
properties: {
|
||||
claim: { type: 'string' },
|
||||
analysis: { type: 'string', description: '机理/前提/边界/反例' },
|
||||
confidence: { type: 'string', enum: ['high', 'medium', 'low'] },
|
||||
},
|
||||
}
|
||||
|
||||
// ---- 输入(由 run.ts 通过 args 透传)----
|
||||
const topic = args.topic
|
||||
if (typeof topic !== 'string' || topic.length === 0) {
|
||||
throw new Error('research-report 需要 args.topic(研究主题字符串)')
|
||||
}
|
||||
const angles =
|
||||
Array.isArray(args.angles) && args.angles.length > 0
|
||||
? args.angles
|
||||
: ['核心概念与原理', '主流方案与对比', '工程实践与权衡', '生态与趋势']
|
||||
|
||||
// ---- Phase 1:多角度并行调研。parallel = 屏障,等所有角度完成后才继续。----
|
||||
phase('Research')
|
||||
log(`主题「${topic}」:${angles.length} 个角度并行调研中`)
|
||||
const researched = await parallel(
|
||||
angles.map(
|
||||
a => () =>
|
||||
agent(
|
||||
`你是资深技术研究分析师。针对技术主题「${topic}」,从「${a}」角度调研,给出该角度下 2-4 条最关键的技术发现,每条须附依据。`,
|
||||
{ label: `research:${a}`, phase: 'Research', schema: ANGLE_SCHEMA },
|
||||
),
|
||||
),
|
||||
)
|
||||
// parallel 返回 (object|null)[]:skipped/dead 的角度为 null,过滤后展平
|
||||
const allFindings = researched
|
||||
.filter(Boolean)
|
||||
.flatMap(r => r.findings.map(f => ({ ...f, angle: r.angle })))
|
||||
log(`收集到 ${allFindings.length} 条发现,进入深挖`)
|
||||
|
||||
if (allFindings.length === 0) {
|
||||
return {
|
||||
topic,
|
||||
report: '(所有角度调研均失败,无可用发现)',
|
||||
anglesCovered: 0,
|
||||
findingsDeepened: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Phase 2:逐条深挖。pipeline = 无屏障,每条发现独立跑完所有 stage,互不等待。----
|
||||
phase('DeepRead')
|
||||
const deepened = await pipeline(
|
||||
allFindings,
|
||||
f =>
|
||||
agent(
|
||||
`针对以下技术发现,深入分析其机理、成立前提、适用边界与可能的反例:\n结论:${f.claim}\n依据:${f.evidence}\n角度:${f.angle}`,
|
||||
{ label: `deep:${f.angle}`, phase: 'DeepRead', schema: DEEP_SCHEMA },
|
||||
),
|
||||
// 第二个 stage:按置信度标注交叉价值(演示多 stage pipeline 链式传递)。
|
||||
// stage-1 若 dead 返回 null,这里显式守卫——避免对 null 取属性(否则被 pipeline
|
||||
// 的 per-item catch 吞掉、整条静默丢失)。
|
||||
d =>
|
||||
d
|
||||
? {
|
||||
...d,
|
||||
crossCutting:
|
||||
d.confidence === 'high' ? '可作为报告主干' : '需谨慎引用或佐证',
|
||||
}
|
||||
: null,
|
||||
)
|
||||
const deepFindings = deepened.filter(Boolean)
|
||||
log(`深挖完成 ${deepFindings.length}/${allFindings.length} 条`)
|
||||
|
||||
// ---- Phase 3:综合成 Markdown 报告(无 schema → 返回纯文本)----
|
||||
phase('Synthesize')
|
||||
const report = await agent(
|
||||
`你是首席技术分析师。基于以下经深挖的技术发现,综合一份结构化研究报告(纯 Markdown 叙述)。\n要求:含摘要、分角度分析、关键结论、落地建议与风险;用自然语言陈述每条发现并标注 confidence。\n禁止:在报告中粘贴 JSON 代码块或原样引用下方输入数据。\n\n主题:${topic}\n\n深度发现(JSON,仅供你理解,不要原样输出):\n${JSON.stringify(deepFindings)}`,
|
||||
{ label: 'synthesize', phase: 'Synthesize', maxTokens: 8192 },
|
||||
)
|
||||
|
||||
return {
|
||||
topic,
|
||||
report,
|
||||
anglesCovered: angles.length,
|
||||
findingsDeepened: deepFindings.length,
|
||||
}
|
||||
313
packages/workflow-engine/examples/research-report/run.ts
Normal file
313
packages/workflow-engine/examples/research-report/run.ts
Normal file
@@ -0,0 +1,313 @@
|
||||
/**
|
||||
* research-report runner —— 直接用 @claude-code-best/workflow-engine 运行 workflow,
|
||||
* 完全绕开 Workflow 工具与核心 runAgent。agent() 后端直连 Anthropic SDK
|
||||
* (@anthropic-ai/sdk):子 agent = 一次 messages.create。
|
||||
*
|
||||
* 用法:
|
||||
* ANTHROPIC_API_KEY=sk-... \
|
||||
* bun run packages/workflow-engine/examples/research-report/run.ts "Edge Computing"
|
||||
*
|
||||
* 可选环境变量:
|
||||
* ANTHROPIC_MODEL 模型名,默认 claude-sonnet-4-5
|
||||
* RESEARCH_RUNS_DIR journal 目录,默认 ~/.claude/workflow-runs(resume 复用)
|
||||
*/
|
||||
import Anthropic from '@anthropic-ai/sdk'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { homedir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
createFileJournalStore,
|
||||
createHostHandle,
|
||||
runWorkflow,
|
||||
Semaphore,
|
||||
validateAgainstSchema,
|
||||
type AgentRunParams,
|
||||
type AgentRunResult,
|
||||
type ProgressEvent,
|
||||
type WorkflowPorts,
|
||||
} from '@claude-code-best/workflow-engine'
|
||||
|
||||
const SCRIPT_FILE = `${import.meta.dir}/research-report.workflow.mjs`
|
||||
const DEFAULT_MODEL = process.env.ANTHROPIC_MODEL ?? 'claude-sonnet-4-5'
|
||||
const MAX_TOKENS = 4096
|
||||
|
||||
// 终端着色(无第三方依赖)
|
||||
const paint = {
|
||||
dim: (s: string) => `\x1b[2m${s}\x1b[0m`,
|
||||
cyan: (s: string) => `\x1b[36m${s}\x1b[0m`,
|
||||
green: (s: string) => `\x1b[32m${s}\x1b[0m`,
|
||||
yellow: (s: string) => `\x1b[33m${s}\x1b[0m`,
|
||||
red: (s: string) => `\x1b[31m${s}\x1b[0m`,
|
||||
bold: (s: string) => `\x1b[1m${s}\x1b[0m`,
|
||||
}
|
||||
|
||||
// client 由 main() 构造,llmAgent 闭包引用。null 守卫使 import 时不触发真实调用。
|
||||
const clientRef: { client: Anthropic | null } = { client: null }
|
||||
|
||||
// API 并发上限(独立于引擎的 CPU semaphore——LLM API 对并发远比 CPU 敏感,默认 3)。
|
||||
// 用 WORKFLOW_API_CONCURRENCY 调整。
|
||||
const apiSem = new Semaphore(
|
||||
Math.max(1, Number(process.env.WORKFLOW_API_CONCURRENCY) || 3),
|
||||
)
|
||||
|
||||
/** 429/5xx/连接错误指数退避重试(500ms → 1s → 2s → 4s),最多 4 次。 */
|
||||
async function withRetry<T>(fn: () => Promise<T>, retries = 4): Promise<T> {
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (e) {
|
||||
if (!isRetryable(e) || attempt >= retries) throw e
|
||||
const wait = Math.min(500 * 2 ** attempt, 8000)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, wait)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isRetryable(e: unknown): boolean {
|
||||
const err = e as { status?: number; name?: string }
|
||||
if (err.status === 429) return true
|
||||
if (typeof err.status === 'number' && err.status >= 500) return true
|
||||
if (typeof err.name === 'string' && /Connection|Timeout/i.test(err.name)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/** 精简错误摘要(避免打印整个含 request body 的 message)。 */
|
||||
function errSummary(e: unknown): string {
|
||||
const err = e as {
|
||||
status?: number
|
||||
error?: { type?: string }
|
||||
message?: string
|
||||
}
|
||||
if (err.status) return `HTTP ${err.status} ${err.error?.type ?? ''}`.trim()
|
||||
return (err.message ?? 'unknown').slice(0, 120)
|
||||
}
|
||||
|
||||
/**
|
||||
* 真实 LLM agentRunner:一次 messages.create(经 API 并发信号量 + 重试)。
|
||||
* schema 模式:prompt 追加 JSON 指令 → 取文本 → 提取 JSON → Ajv 校验 → 失败返回 dead。
|
||||
* 非 schema:返回纯文本。
|
||||
*/
|
||||
async function llmAgent(params: AgentRunParams): Promise<AgentRunResult> {
|
||||
const client = clientRef.client
|
||||
if (client === null) return { kind: 'dead' }
|
||||
|
||||
const schemaInstruction = params.schema
|
||||
? '\n\n你必须以一个【单独的 JSON 对象】作为整段回答(不要 Markdown 代码围栏、不要任何解释),该对象须匹配如下 JSON Schema:\n' +
|
||||
JSON.stringify(params.schema)
|
||||
: ''
|
||||
|
||||
const release = await apiSem.acquire()
|
||||
try {
|
||||
const resp = await withRetry(() =>
|
||||
client.messages.create({
|
||||
model: params.model ?? DEFAULT_MODEL,
|
||||
max_tokens: params.maxTokens ?? MAX_TOKENS,
|
||||
messages: [
|
||||
{ role: 'user', content: params.prompt + schemaInstruction },
|
||||
],
|
||||
}),
|
||||
)
|
||||
const outputTokens = resp.usage.output_tokens
|
||||
const truncated = resp.stop_reason === 'max_tokens'
|
||||
|
||||
if (params.schema) {
|
||||
// 截断的 JSON 几乎必然不完整 → 直接判 dead(而非让解析模糊失败)
|
||||
if (truncated) return { kind: 'dead' }
|
||||
const text = resp.content
|
||||
.map(block => (block.type === 'text' ? block.text : ''))
|
||||
.join('')
|
||||
.trim()
|
||||
const parsed = extractJsonObject(text)
|
||||
if (parsed === null) return { kind: 'dead' }
|
||||
const { valid } = validateAgainstSchema(parsed, params.schema)
|
||||
if (!valid) return { kind: 'dead' }
|
||||
return { kind: 'ok', output: parsed as object, usage: { outputTokens } }
|
||||
}
|
||||
const text = resp.content
|
||||
.map(block => (block.type === 'text' ? block.text : ''))
|
||||
.join('')
|
||||
.trim()
|
||||
if (truncated) {
|
||||
console.error(
|
||||
paint.yellow(` ⚠ 输出被 max_tokens 截断(${outputTokens} tokens)`),
|
||||
)
|
||||
}
|
||||
return { kind: 'ok', output: text, usage: { outputTokens } }
|
||||
} catch (e) {
|
||||
console.error(paint.red(` ✗ ${errSummary(e)}`))
|
||||
return { kind: 'dead' }
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 容错 JSON 提取:去代码围栏 → 从首个 { 起做括号深度匹配(跳过字符串字面量与
|
||||
* 转义,仿 src/engine/script.ts 的 extractMeta),取配对的 {…} → JSON.parse。
|
||||
* 比 lastIndexOf('}') 稳健:正确处理 JSON 后散文里含 }、第二个对象、字符串内 }。
|
||||
*/
|
||||
function extractJsonObject(text: string): unknown | null {
|
||||
const stripped = text.replace(/```(?:json)?/gi, '').trim()
|
||||
const start = stripped.indexOf('{')
|
||||
if (start < 0) {
|
||||
try {
|
||||
return JSON.parse(stripped)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
let depth = 0
|
||||
let inStr: string | null = null
|
||||
for (let i = start; i < stripped.length; i++) {
|
||||
const ch = stripped[i]
|
||||
if (inStr) {
|
||||
if (ch === '\\') i++
|
||||
else if (ch === inStr) inStr = null
|
||||
continue
|
||||
}
|
||||
if (ch === '"' || ch === "'" || ch === '`') inStr = ch
|
||||
else if (ch === '{') depth++
|
||||
else if (ch === '}') {
|
||||
depth--
|
||||
if (depth === 0) {
|
||||
try {
|
||||
return JSON.parse(stripped.slice(start, i + 1))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** 内存版 taskRegistrar:不经核心 LocalWorkflowTask,仅维护 runId → AbortController。 */
|
||||
function makeTaskRegistrar(): WorkflowPorts['taskRegistrar'] {
|
||||
const controllers = new Map<string, AbortController>()
|
||||
return {
|
||||
register(opts) {
|
||||
const ac = new AbortController()
|
||||
const runId = opts.runId ?? `research-${controllers.size + 1}`
|
||||
controllers.set(runId, ac)
|
||||
return { runId, signal: ac.signal }
|
||||
},
|
||||
complete() {},
|
||||
fail() {},
|
||||
kill(runId) {
|
||||
controllers.get(runId)?.abort()
|
||||
},
|
||||
pendingAction() {
|
||||
return null
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** 进度事件 → 终端实时打印。 */
|
||||
function printProgress(e: ProgressEvent): void {
|
||||
switch (e.type) {
|
||||
case 'run_started':
|
||||
console.log(paint.bold(paint.cyan(`\n▶ ${e.workflowName}`)))
|
||||
break
|
||||
case 'phase_started':
|
||||
console.log(paint.cyan(`\n━ phase: ${e.phase}`))
|
||||
break
|
||||
case 'phase_done':
|
||||
break
|
||||
case 'agent_started':
|
||||
console.log(` ${paint.dim('→')} ${e.label ?? 'agent'}`)
|
||||
break
|
||||
case 'agent_done': {
|
||||
const tag =
|
||||
e.result.kind === 'ok'
|
||||
? paint.green('✓')
|
||||
: e.result.kind === 'skipped'
|
||||
? paint.yellow('⊘')
|
||||
: paint.red('✗')
|
||||
console.log(
|
||||
` ${tag} ${e.label ?? 'agent'} ${paint.dim(`[${e.result.kind}]`)}`,
|
||||
)
|
||||
break
|
||||
}
|
||||
case 'log':
|
||||
console.log(` ${paint.dim('·')} ${e.message}`)
|
||||
break
|
||||
case 'run_done':
|
||||
console.log(paint.bold(`\n■ ${e.status}`))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/** 组装端口:agent 后端直连 SDK,其余为自包含实现,不触达核心层。 */
|
||||
function makePorts(runsDir: string): WorkflowPorts {
|
||||
return {
|
||||
agentRunner: { runAgentToResult: llmAgent },
|
||||
progressEmitter: { emit: printProgress },
|
||||
taskRegistrar: makeTaskRegistrar(),
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const topic = process.argv[2]
|
||||
if (!topic) {
|
||||
console.error(paint.red('✗ 用法:run.ts <研究主题>'))
|
||||
console.error(paint.dim(' 例:bun run run.ts "Edge Computing"'))
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
clientRef.client = new Anthropic({ logLevel: 'off' })
|
||||
const runsDir =
|
||||
process.env.RESEARCH_RUNS_DIR ?? join(homedir(), '.claude', 'workflow-runs')
|
||||
const script = await readFile(SCRIPT_FILE, 'utf-8')
|
||||
|
||||
const result = await runWorkflow({
|
||||
script,
|
||||
args: { topic },
|
||||
runId: `research-${Date.now()}`,
|
||||
ports: makePorts(runsDir),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
})
|
||||
|
||||
if (result.status !== 'completed') {
|
||||
console.error(
|
||||
paint.red(`✗ workflow ${result.status}:${result.error ?? ''}`),
|
||||
)
|
||||
process.exit(1)
|
||||
}
|
||||
const ret = result.returnValue as {
|
||||
report?: string
|
||||
topic?: string
|
||||
anglesCovered?: number
|
||||
findingsDeepened?: number
|
||||
}
|
||||
console.log(
|
||||
paint.bold(
|
||||
paint.green(`\n════════ 技术研究报告:${ret.topic ?? topic} ════════`),
|
||||
),
|
||||
)
|
||||
console.log(
|
||||
paint.dim(
|
||||
`角度数=${ret.anglesCovered ?? '?'} 深挖=${ret.findingsDeepened ?? '?'}`,
|
||||
),
|
||||
)
|
||||
console.log(ret.report ?? '(无报告输出)')
|
||||
}
|
||||
|
||||
// 仅作为脚本直接运行时启动(import 不触发,便于冒烟/复用端口工厂)
|
||||
if (import.meta.main) {
|
||||
await main()
|
||||
}
|
||||
251
packages/workflow-engine/examples/smoke.ts
Normal file
251
packages/workflow-engine/examples/smoke.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
/**
|
||||
* 冒烟端到端入口 —— 真实 SDK + 引擎,最小验证端到端通路。
|
||||
* 3 次模型调用(2 角度并行 schema + 1 综合),秒级完成、低成本。
|
||||
* 覆盖:runWorkflow、parallel(屏障)、agent(schema) 结构化、agent 文本、进度事件。
|
||||
*
|
||||
* 用法:
|
||||
* ANTHROPIC_API_KEY=sk-... \
|
||||
* bun run packages/workflow-engine/examples/smoke.ts
|
||||
*
|
||||
* 可选:ANTHROPIC_MODEL(默认 claude-sonnet-4-5)
|
||||
*/
|
||||
import Anthropic from '@anthropic-ai/sdk'
|
||||
import { homedir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
createFileJournalStore,
|
||||
createHostHandle,
|
||||
runWorkflow,
|
||||
Semaphore,
|
||||
validateAgainstSchema,
|
||||
type AgentRunParams,
|
||||
type AgentRunResult,
|
||||
type ProgressEvent,
|
||||
type WorkflowPorts,
|
||||
} from '@claude-code-best/workflow-engine'
|
||||
|
||||
const DEFAULT_MODEL = process.env.ANTHROPIC_MODEL ?? 'claude-sonnet-4-5'
|
||||
const clientRef: { client: Anthropic | null } = { client: null }
|
||||
|
||||
const POINT_SCHEMA = {
|
||||
type: 'object',
|
||||
required: ['point'],
|
||||
properties: { point: { type: 'string' } },
|
||||
}
|
||||
|
||||
// 最小 workflow:2 角度并行(schema 结构化)→ 综合(文本)。脚本内用 + 拼接避免 ${}。
|
||||
const SMOKE_SCRIPT =
|
||||
`
|
||||
export const meta = { name: 'smoke', description: 'minimal end-to-end smoke' }
|
||||
phase('Smoke')
|
||||
const angles = ['一句话定义', '一个最核心价值']
|
||||
const points = await parallel(
|
||||
angles.map(a => () =>
|
||||
agent('用简短一句话(30 字内)说明 workflow 编排的「' + a + '」。', {
|
||||
label: 'p:' + a,
|
||||
schema: ` +
|
||||
JSON.stringify(POINT_SCHEMA) +
|
||||
`,
|
||||
}),
|
||||
),
|
||||
)
|
||||
const clean = points.filter(Boolean)
|
||||
const joined = clean.map(p => p.point).join(';')
|
||||
const summary = await agent('把以下要点综合成一句中文结论。要点:' + joined, {
|
||||
label: 'summary',
|
||||
})
|
||||
return { points: clean, summary }
|
||||
`
|
||||
|
||||
// API 并发上限(独立于引擎的 CPU semaphore——LLM API 对并发远比 CPU 敏感,默认 3)。
|
||||
const apiSem = new Semaphore(
|
||||
Math.max(1, Number(process.env.WORKFLOW_API_CONCURRENCY) || 3),
|
||||
)
|
||||
|
||||
/** 429/5xx/连接错误指数退避重试,最多 4 次。 */
|
||||
async function withRetry<T>(fn: () => Promise<T>, retries = 4): Promise<T> {
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (e) {
|
||||
if (!isRetryable(e) || attempt >= retries) throw e
|
||||
const wait = Math.min(500 * 2 ** attempt, 8000)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, wait)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isRetryable(e: unknown): boolean {
|
||||
const err = e as { status?: number; name?: string }
|
||||
if (err.status === 429) return true
|
||||
if (typeof err.status === 'number' && err.status >= 500) return true
|
||||
if (typeof err.name === 'string' && /Connection|Timeout/i.test(err.name)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function errSummary(e: unknown): string {
|
||||
const err = e as {
|
||||
status?: number
|
||||
error?: { type?: string }
|
||||
message?: string
|
||||
}
|
||||
if (err.status) return `HTTP ${err.status} ${err.error?.type ?? ''}`.trim()
|
||||
return (err.message ?? 'unknown').slice(0, 120)
|
||||
}
|
||||
|
||||
async function llmAgent(params: AgentRunParams): Promise<AgentRunResult> {
|
||||
const client = clientRef.client
|
||||
if (client === null) return { kind: 'dead' }
|
||||
const schemaInstruction = params.schema
|
||||
? '\n\n以单独 JSON 对象回答(无围栏无解释),匹配 schema:\n' +
|
||||
JSON.stringify(params.schema)
|
||||
: ''
|
||||
const release = await apiSem.acquire()
|
||||
try {
|
||||
const resp = await withRetry(() =>
|
||||
client.messages.create({
|
||||
model: params.model ?? DEFAULT_MODEL,
|
||||
max_tokens: params.maxTokens ?? 1024,
|
||||
messages: [
|
||||
{ role: 'user', content: params.prompt + schemaInstruction },
|
||||
],
|
||||
}),
|
||||
)
|
||||
const outputTokens = resp.usage.output_tokens
|
||||
if (resp.stop_reason === 'max_tokens') return { kind: 'dead' }
|
||||
const text = resp.content
|
||||
.map(block => (block.type === 'text' ? block.text : ''))
|
||||
.join('')
|
||||
.trim()
|
||||
if (params.schema) {
|
||||
const parsed = extractJsonObject(text)
|
||||
if (parsed === null) return { kind: 'dead' }
|
||||
if (!validateAgainstSchema(parsed, params.schema).valid) {
|
||||
return { kind: 'dead' }
|
||||
}
|
||||
return { kind: 'ok', output: parsed as object, usage: { outputTokens } }
|
||||
}
|
||||
return { kind: 'ok', output: text, usage: { outputTokens } }
|
||||
} catch (e) {
|
||||
console.error(` ✗ ${errSummary(e)}`)
|
||||
return { kind: 'dead' }
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
}
|
||||
|
||||
function extractJsonObject(text: string): unknown | null {
|
||||
const stripped = text.replace(/```(?:json)?/gi, '').trim()
|
||||
const start = stripped.indexOf('{')
|
||||
if (start < 0) {
|
||||
try {
|
||||
return JSON.parse(stripped)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
let depth = 0
|
||||
let inStr: string | null = null
|
||||
for (let i = start; i < stripped.length; i++) {
|
||||
const ch = stripped[i]
|
||||
if (inStr) {
|
||||
if (ch === '\\') i++
|
||||
else if (ch === inStr) inStr = null
|
||||
continue
|
||||
}
|
||||
if (ch === '"' || ch === "'" || ch === '`') inStr = ch
|
||||
else if (ch === '{') depth++
|
||||
else if (ch === '}') {
|
||||
depth--
|
||||
if (depth === 0) {
|
||||
try {
|
||||
return JSON.parse(stripped.slice(start, i + 1))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function makePorts(runsDir: string): WorkflowPorts {
|
||||
return {
|
||||
agentRunner: { runAgentToResult: llmAgent },
|
||||
progressEmitter: {
|
||||
emit: (e: ProgressEvent) => {
|
||||
if (e.type === 'phase_started') console.log(`\n━ phase: ${e.phase}`)
|
||||
else if (e.type === 'agent_started')
|
||||
console.log(` → ${e.label ?? 'agent'}`)
|
||||
else if (e.type === 'agent_done')
|
||||
console.log(
|
||||
` ${e.result.kind === 'ok' ? '✓' : '✗'} ${e.label ?? ''} [${e.result.kind}]`,
|
||||
)
|
||||
else if (e.type === 'log') console.log(` · ${e.message}`)
|
||||
},
|
||||
},
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'smoke',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete() {},
|
||||
fail() {},
|
||||
kill() {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const apiKey = process.env.ANTHROPIC_API_KEY
|
||||
if (!apiKey) {
|
||||
console.error('✗ 缺少 ANTHROPIC_API_KEY 环境变量')
|
||||
process.exit(1)
|
||||
}
|
||||
clientRef.client = new Anthropic({ apiKey, logLevel: 'off' })
|
||||
const runsDir =
|
||||
process.env.RESEARCH_RUNS_DIR ?? join(homedir(), '.claude', 'workflow-runs')
|
||||
|
||||
const result = await runWorkflow({
|
||||
script: SMOKE_SCRIPT,
|
||||
args: {},
|
||||
runId: `smoke-${Date.now()}`,
|
||||
ports: makePorts(runsDir),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: process.cwd(),
|
||||
budgetTotal: null,
|
||||
})
|
||||
|
||||
if (result.status !== 'completed') {
|
||||
console.error(`\n✗ FAIL:${result.status} ${result.error ?? ''}`)
|
||||
process.exit(1)
|
||||
}
|
||||
const ret = result.returnValue as {
|
||||
points: Array<{ point: string }>
|
||||
summary: string
|
||||
}
|
||||
console.log('\n━━━━━━━━ 冒烟结果 ━━━━━━━━')
|
||||
for (const p of ret.points) console.log(`• ${p.point}`)
|
||||
console.log(`\n综合:${ret.summary}`)
|
||||
console.log(
|
||||
`\n✓ PASS:端到端通路正常(${ret.points.length} 要点 + 综合,3 次模型调用)`,
|
||||
)
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await main()
|
||||
}
|
||||
19
packages/workflow-engine/package.json
Normal file
19
packages/workflow-engine/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@claude-code-best/workflow-engine",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"ajv": "^8.18.0",
|
||||
"zod": "^4.3.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@anthropic-ai/sdk": "^0.81.0"
|
||||
}
|
||||
}
|
||||
490
packages/workflow-engine/src/__tests__/WorkflowTool.test.ts
Normal file
490
packages/workflow-engine/src/__tests__/WorkflowTool.test.ts
Normal file
@@ -0,0 +1,490 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { createWorkflowTool } from '../tool/WorkflowTool.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
import type { AgentRunParams, AgentRunResult, ProgressEvent } from '../types.js'
|
||||
|
||||
function mockPorts(
|
||||
runsDir: string,
|
||||
results: Map<string, AgentRunResult>,
|
||||
): {
|
||||
ports: WorkflowPorts
|
||||
events: ProgressEvent[]
|
||||
runStatus: Map<string, string>
|
||||
} {
|
||||
const events: ProgressEvent[] = []
|
||||
const runStatus = new Map<string, string>()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) =>
|
||||
results.get(p.prompt) ?? { kind: 'dead' },
|
||||
},
|
||||
progressEmitter: { emit: e => void events.push(e) },
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'run-x',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete: id => void runStatus.set(id, 'completed'),
|
||||
fail: id => void runStatus.set(id, 'failed'),
|
||||
kill: id => void runStatus.set(id, 'killed'),
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: runsDir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
return { ports, events, runStatus }
|
||||
}
|
||||
|
||||
test('call 返回 launch 消息并在后台完成', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const { ports, runStatus } = mockPorts(
|
||||
dir,
|
||||
new Map([
|
||||
['compute', { kind: 'ok', output: '42', usage: { outputTokens: 1 } }],
|
||||
]),
|
||||
)
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ script: `return agent('compute')` },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toContain('run_id: run-x')
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('completed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('缺少 script/name/scriptPath → 返回错误(不进后台)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const { ports, runStatus } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call({}, undefined, undefined, undefined)
|
||||
expect(res.data.output).toMatch(/^Error:/)
|
||||
expect(runStatus.size).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('脚本语法错 → 返回校验错误(不进后台)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const { ports, runStatus } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ script: `return ((` },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toMatch(/校验失败|Error/)
|
||||
expect(runStatus.size).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('name 解析到 .claude/workflows/<name>.ts', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
await mkdir(join(dir, '.claude', 'workflows'), { recursive: true })
|
||||
await writeFile(
|
||||
join(dir, '.claude', 'workflows', 'release.ts'),
|
||||
`return agent('compute')`,
|
||||
)
|
||||
const { ports, runStatus } = mockPorts(
|
||||
dir,
|
||||
new Map([
|
||||
['compute', { kind: 'ok', output: 'done', usage: { outputTokens: 1 } }],
|
||||
]),
|
||||
)
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ name: 'release' },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toContain('run_id')
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('completed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('renderToolUseMessage / mapToolResultToToolResultBlockParam', () => {
|
||||
const dir = '/tmp'
|
||||
const { ports } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
expect(tool.renderToolUseMessage({ name: 'release' })).toBe(
|
||||
'Workflow: release',
|
||||
)
|
||||
const block = tool.mapToolResultToToolResultBlockParam(
|
||||
{ output: 'hi' },
|
||||
'tu-1',
|
||||
)
|
||||
expect(block.tool_use_id).toBe('tu-1')
|
||||
expect(block.type).toBe('tool_result')
|
||||
expect(block.content[0]!.text).toBe('hi')
|
||||
})
|
||||
|
||||
test('scriptPath 解析到文件内容并后台执行', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const scriptFile = join(dir, 'external.ts')
|
||||
await writeFile(scriptFile, `return agent('compute')`)
|
||||
const { ports, runStatus } = mockPorts(
|
||||
dir,
|
||||
new Map([
|
||||
['compute', { kind: 'ok', output: 'done', usage: { outputTokens: 1 } }],
|
||||
]),
|
||||
)
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ scriptPath: scriptFile },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toContain('run_id')
|
||||
expect(res.data.output).toContain('external.ts')
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('completed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('脚本运行时失败 → onFinish 路由到 fail', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const { ports, runStatus } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
await tool.call(
|
||||
{ script: `throw new Error('boom')` },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('failed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('元数据方法:description/prompt/renderToolUseMessage', async () => {
|
||||
const { ports } = mockPorts('/tmp', new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
expect(tool.isEnabled()).toBe(true)
|
||||
expect(tool.isReadOnly({})).toBe(false)
|
||||
expect(await tool.description()).toBeTruthy()
|
||||
expect(await tool.prompt()).toContain('Workflow')
|
||||
expect(tool.renderToolUseMessage({})).toBe('Workflow: unknown')
|
||||
expect(tool.renderToolUseMessage({ resumeFromRunId: 'r1' })).toBe(
|
||||
'Workflow resume: r1',
|
||||
)
|
||||
})
|
||||
|
||||
test('name 不存在 → 返回错误(不进后台)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
await mkdir(join(dir, '.claude', 'workflows'), { recursive: true })
|
||||
const { ports, runStatus } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ name: 'nope' },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toMatch(/^Error:/)
|
||||
expect(runStatus.size).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('workflow 被 abort → onFinish 路由 kill', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const runStatus = new Map<string, string>()
|
||||
const ac = new AbortController()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async () => ({
|
||||
kind: 'ok',
|
||||
output: 'x',
|
||||
usage: { outputTokens: 1 },
|
||||
}),
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'run-x', signal: ac.signal }),
|
||||
complete: id => void runStatus.set(id, 'completed'),
|
||||
fail: id => void runStatus.set(id, 'failed'),
|
||||
kill: id => void runStatus.set(id, 'killed'),
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
ac.abort()
|
||||
const tool = createWorkflowTool(ports)
|
||||
await tool.call(
|
||||
{ script: `return agent('x')` },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('killed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('args 为 JSON 字符串化的对象时防御性 parse(向后兼容旧 z.string() 契约)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const capturedPrompts: unknown[] = []
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) => {
|
||||
capturedPrompts.push(p.prompt)
|
||||
return { kind: 'ok', output: 'done', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'run-x',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const tool = createWorkflowTool(ports)
|
||||
await tool.call(
|
||||
{
|
||||
script: `return agent(args.commit)`,
|
||||
// 模拟旧契约下模型发送的字符串化 JSON
|
||||
args: '{"commit":"abc123"}',
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
// 若 args 未归一化:args.commit === undefined(string 上无 commit 属性)
|
||||
// 若 args 归一化:args.commit === 'abc123'
|
||||
expect(capturedPrompts).toContain('abc123')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('args 为非合法 JSON 字符串时保持原值不抛', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const capturedPrompts: unknown[] = []
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) => {
|
||||
capturedPrompts.push(p.prompt)
|
||||
return { kind: 'ok', output: 'ok', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'run-x',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const tool = createWorkflowTool(ports)
|
||||
await tool.call(
|
||||
{
|
||||
// 脚本把 args 当字符串用:agent(args) → agent('hello')
|
||||
script: `return agent(args)`,
|
||||
args: 'hello',
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
// 'hello' 不是合法 JSON,应保持为字符串
|
||||
expect(capturedPrompts).toContain('hello')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('scriptPath 越界(resolve 后在 cwd 之外)→ 拒绝并报错(防任意文件读)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const subDir = join(dir, 'sub')
|
||||
await mkdir(subDir, { recursive: true })
|
||||
// 在 subDir 之外(dir 内)放置一个脚本
|
||||
const outsideScript = join(dir, 'outside.ts')
|
||||
await writeFile(outsideScript, `return agent('x')`)
|
||||
// host.cwd = subDir,scriptPath 是 subDir 外的绝对路径
|
||||
const { ports, runStatus } = mockPorts(subDir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
const res = await tool.call(
|
||||
{ scriptPath: outsideScript },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toMatch(/^Error:/)
|
||||
expect(res.data.output).toMatch(/越界|外|outside|contain/i)
|
||||
expect(runStatus.size).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('name 含 ".." 路径段 → 拒绝(防路径遍历逃出 workflowDir)', async () => {
|
||||
const outer = await mkdtemp(join(tmpdir(), 'wf-outer-'))
|
||||
try {
|
||||
// 在 outer 根下放置 evil.ts(在 .claude/workflows 之外)
|
||||
await writeFile(join(outer, 'evil.ts'), `return agent('x')`)
|
||||
await mkdir(join(outer, '.claude', 'workflows'), { recursive: true })
|
||||
const { ports, runStatus } = mockPorts(outer, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
// name = '../../evil' → join 后逃离 workflows 目录到 outer/evil.ts
|
||||
const res = await tool.call(
|
||||
{ name: '../../evil' },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toMatch(/^Error:/)
|
||||
expect(runStatus.size).toBe(0)
|
||||
} finally {
|
||||
await rm(outer, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('name 含路径分隔符或为绝对路径 → 拒绝', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
await mkdir(join(dir, '.claude', 'workflows'), { recursive: true })
|
||||
const { ports } = mockPorts(dir, new Map())
|
||||
const tool = createWorkflowTool(ports)
|
||||
for (const badName of ['foo/bar', '/etc/passwd', '..', '.']) {
|
||||
const res = await tool.call(
|
||||
{ name: badName },
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
expect(res.data.output).toMatch(/^Error:/)
|
||||
}
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('returnValue 为对象 → complete(formatValue 走 JSON 分支)', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-tool-'))
|
||||
try {
|
||||
const { ports, runStatus } = mockPorts(
|
||||
dir,
|
||||
new Map([['x', { kind: 'ok', output: 'x', usage: { outputTokens: 1 } }]]),
|
||||
)
|
||||
const tool = createWorkflowTool(ports)
|
||||
await tool.call(
|
||||
{
|
||||
script: `await agent('x')\nreturn { ok: true, n: 1 }`,
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 50)
|
||||
})
|
||||
expect(runStatus.get('run-x')).toBe('completed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
155
packages/workflow-engine/src/__tests__/agentAdapter.test.ts
Normal file
155
packages/workflow-engine/src/__tests__/agentAdapter.test.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import {
|
||||
AgentAdapterRegistry,
|
||||
AdapterNotFoundError,
|
||||
type AgentAdapter,
|
||||
} from '../agentAdapter.js'
|
||||
import { createHostHandle } from '../ports.js'
|
||||
import type { AgentRunParams, AgentRunResult } from '../types.js'
|
||||
|
||||
function makeAdapter(
|
||||
id: string,
|
||||
result: AgentRunResult = {
|
||||
kind: 'ok',
|
||||
output: `out-${id}`,
|
||||
usage: { outputTokens: 1 },
|
||||
},
|
||||
): AgentAdapter {
|
||||
return {
|
||||
id,
|
||||
capabilities: { structuredOutput: true },
|
||||
async run() {
|
||||
return result
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const P = (over: Partial<AgentRunParams> = {}): AgentRunParams => ({
|
||||
prompt: 'p',
|
||||
...over,
|
||||
})
|
||||
|
||||
const CTX = {
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
runId: 'r',
|
||||
}
|
||||
|
||||
test('resolve 默认走 default adapter,run 返回结果', async () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('a'))
|
||||
.register(makeAdapter('b'))
|
||||
.default('a')
|
||||
expect(reg.resolve(P()).id).toBe('a')
|
||||
const r = await reg.resolve(P()).run(P(), CTX)
|
||||
expect(r.kind).toBe('ok')
|
||||
})
|
||||
|
||||
test('route agentType 命中优先于 default', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('default'))
|
||||
.register(makeAdapter('research'))
|
||||
.route({ kind: 'agentType', agentType: 'researcher', adapter: 'research' })
|
||||
.default('default')
|
||||
expect(reg.resolve(P({ agentType: 'researcher' })).id).toBe('research')
|
||||
expect(reg.resolve(P({ agentType: 'other' })).id).toBe('default')
|
||||
})
|
||||
|
||||
test('route model 前缀匹配', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('cheap'))
|
||||
.register(makeAdapter('strong'))
|
||||
.route({ kind: 'model', pattern: 'claude-opus', adapter: 'strong' })
|
||||
.default('cheap')
|
||||
expect(reg.resolve(P({ model: 'claude-opus-4' })).id).toBe('strong')
|
||||
expect(reg.resolve(P({ model: 'claude-sonnet-4' })).id).toBe('cheap')
|
||||
expect(reg.resolve(P()).id).toBe('cheap') // 无 model → default
|
||||
})
|
||||
|
||||
test('route custom 谓词', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('main'))
|
||||
.register(makeAdapter('special'))
|
||||
.route({
|
||||
kind: 'custom',
|
||||
match: p => p.prompt.includes('VIP'),
|
||||
adapter: 'special',
|
||||
})
|
||||
.default('main')
|
||||
expect(reg.resolve(P({ prompt: 'handle VIP case' })).id).toBe('special')
|
||||
expect(reg.resolve(P({ prompt: 'normal' })).id).toBe('main')
|
||||
})
|
||||
|
||||
test('规则按顺序匹配(先命中先用)', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('a'))
|
||||
.register(makeAdapter('b'))
|
||||
.route({ kind: 'agentType', agentType: 'x', adapter: 'a' })
|
||||
.route({ kind: 'agentType', agentType: 'x', adapter: 'b' })
|
||||
expect(reg.resolve(P({ agentType: 'x' })).id).toBe('a')
|
||||
})
|
||||
|
||||
test('规则命中的 adapter 未注册 → 跳过该规则继续匹配', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('real'))
|
||||
.route({ kind: 'agentType', agentType: 'x', adapter: 'ghost' })
|
||||
.route({ kind: 'agentType', agentType: 'x', adapter: 'real' })
|
||||
expect(reg.resolve(P({ agentType: 'x' })).id).toBe('real')
|
||||
})
|
||||
|
||||
test('无匹配且无 default → AdapterNotFoundError', () => {
|
||||
const reg = new AgentAdapterRegistry().register(makeAdapter('a'))
|
||||
expect(() => reg.resolve(P())).toThrow(AdapterNotFoundError)
|
||||
})
|
||||
|
||||
test('default 指向未注册的 adapter → 仍抛(不静默回退)', () => {
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(makeAdapter('a'))
|
||||
.default('missing')
|
||||
expect(() => reg.resolve(P())).toThrow(AdapterNotFoundError)
|
||||
})
|
||||
|
||||
test('has / get', () => {
|
||||
const reg = new AgentAdapterRegistry().register(makeAdapter('a'))
|
||||
expect(reg.has('a')).toBe(true)
|
||||
expect(reg.has('b')).toBe(false)
|
||||
expect(reg.get('a')?.id).toBe('a')
|
||||
expect(reg.get('b')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('initializeAll / disposeAll 触发 lifecycle(跳过未实现)', async () => {
|
||||
const events: string[] = []
|
||||
const withLifecycle: AgentAdapter = {
|
||||
id: 'a',
|
||||
capabilities: { structuredOutput: false },
|
||||
async run() {
|
||||
return { kind: 'ok', output: 'x', usage: { outputTokens: 1 } }
|
||||
},
|
||||
async initialize() {
|
||||
events.push('init-a')
|
||||
},
|
||||
async dispose() {
|
||||
events.push('dispose-a')
|
||||
},
|
||||
}
|
||||
const noLifecycle = makeAdapter('b') // 无 initialize/dispose
|
||||
const reg = new AgentAdapterRegistry()
|
||||
.register(withLifecycle)
|
||||
.register(noLifecycle)
|
||||
await reg.initializeAll()
|
||||
await reg.disposeAll()
|
||||
expect(events).toEqual(['init-a', 'dispose-a'])
|
||||
})
|
||||
|
||||
test('capabilities 声明可读', () => {
|
||||
const adapter: AgentAdapter = {
|
||||
id: 'a',
|
||||
capabilities: { structuredOutput: true, tools: true, stream: false },
|
||||
async run() {
|
||||
return { kind: 'ok', output: 'x', usage: { outputTokens: 1 } }
|
||||
},
|
||||
}
|
||||
expect(adapter.capabilities.structuredOutput).toBe(true)
|
||||
expect(adapter.capabilities.tools).toBe(true)
|
||||
expect(adapter.capabilities.stream).toBe(false)
|
||||
})
|
||||
94
packages/workflow-engine/src/__tests__/agentId.test.ts
Normal file
94
packages/workflow-engine/src/__tests__/agentId.test.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { createEngineContext } from '../engine/context.js'
|
||||
import { makeHooks } from '../engine/hooks.js'
|
||||
import { createBufferingEmitter } from '../progress/events.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
import type { AgentRunParams, AgentRunResult } from '../types.js'
|
||||
|
||||
function build(results: Map<string, AgentRunResult>) {
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) =>
|
||||
results.get(p.prompt) ?? { kind: 'dead' },
|
||||
},
|
||||
progressEmitter: emitter,
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const ctx = createEngineContext({
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
runId: 'r',
|
||||
workflowName: 'w',
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
})
|
||||
return { ctx, events, hooks: makeHooks(ctx, async () => null) }
|
||||
}
|
||||
|
||||
test('并发 agent 各自拿到唯一 agentId,started/done 配对', async () => {
|
||||
const ok = (out: string): AgentRunResult => ({
|
||||
kind: 'ok',
|
||||
output: out,
|
||||
usage: { outputTokens: 1 },
|
||||
})
|
||||
const { ctx, events, hooks } = build(
|
||||
new Map([
|
||||
['a', ok('1')],
|
||||
['b', ok('2')],
|
||||
]),
|
||||
)
|
||||
await hooks.parallel([() => hooks.agent('a'), () => hooks.agent('b')])
|
||||
const started = events.filter(e => e.type === 'agent_started')
|
||||
const done = events.filter(e => e.type === 'agent_done')
|
||||
expect(started).toHaveLength(2)
|
||||
expect(done).toHaveLength(2)
|
||||
const ids = started.map(e => (e as { agentId: number }).agentId)
|
||||
expect(new Set(ids).size).toBe(2)
|
||||
for (const d of done as Array<{ agentId: number }>) {
|
||||
expect(ids).toContain(d.agentId)
|
||||
}
|
||||
expect(ctx.resources.agentIdSeq.value).toBe(2)
|
||||
})
|
||||
|
||||
test('agentId 单调递增', async () => {
|
||||
const ok = (out: string): AgentRunResult => ({
|
||||
kind: 'ok',
|
||||
output: out,
|
||||
usage: { outputTokens: 1 },
|
||||
})
|
||||
const { events, hooks } = build(
|
||||
new Map([
|
||||
['a', ok('1')],
|
||||
['b', ok('2')],
|
||||
['c', ok('3')],
|
||||
]),
|
||||
)
|
||||
await hooks.agent('a')
|
||||
await hooks.agent('b')
|
||||
await hooks.agent('c')
|
||||
const ids = events
|
||||
.filter(e => e.type === 'agent_started')
|
||||
.map(e => (e as { agentId: number }).agentId)
|
||||
expect(ids).toEqual([0, 1, 2])
|
||||
})
|
||||
29
packages/workflow-engine/src/__tests__/budget.test.ts
Normal file
29
packages/workflow-engine/src/__tests__/budget.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { Budget, BudgetExhaustedError } from '../engine/budget.js'
|
||||
|
||||
test('total=null 时无限制', () => {
|
||||
const b = new Budget(null)
|
||||
expect(b.total).toBeNull()
|
||||
expect(b.remaining()).toBe(Infinity)
|
||||
b.addOutputTokens(999999)
|
||||
expect(b.spent()).toBe(999999)
|
||||
expect(() => b.assertCanSpend()).not.toThrow()
|
||||
})
|
||||
|
||||
test('累加并触顶抛错', () => {
|
||||
const b = new Budget(100)
|
||||
expect(b.remaining()).toBe(100)
|
||||
b.addOutputTokens(40)
|
||||
expect(b.spent()).toBe(40)
|
||||
expect(b.remaining()).toBe(60)
|
||||
expect(() => b.assertCanSpend()).not.toThrow()
|
||||
b.addOutputTokens(60)
|
||||
expect(b.spent()).toBe(100)
|
||||
expect(() => b.assertCanSpend()).toThrow(BudgetExhaustedError)
|
||||
})
|
||||
|
||||
test('addOutputTokens 负值忽略', () => {
|
||||
const b = new Budget(100)
|
||||
b.addOutputTokens(-50)
|
||||
expect(b.spent()).toBe(0)
|
||||
})
|
||||
100
packages/workflow-engine/src/__tests__/concurrency.test.ts
Normal file
100
packages/workflow-engine/src/__tests__/concurrency.test.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { Semaphore, maxConcurrency } from '../engine/concurrency.js'
|
||||
|
||||
test('Semaphore 限制并发,permit 转移不泄漏', async () => {
|
||||
const sem = new Semaphore(2)
|
||||
let active = 0
|
||||
let peak = 0
|
||||
const task = async (): Promise<void> => {
|
||||
const release = await sem.acquire()
|
||||
active++
|
||||
peak = Math.max(peak, active)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 10)
|
||||
})
|
||||
active--
|
||||
release()
|
||||
}
|
||||
await Promise.all(Array.from({ length: 6 }, () => task()))
|
||||
expect(peak).toBe(2) // 永不超过 permits
|
||||
})
|
||||
|
||||
test('maxConcurrency 落在 [1, 16]', () => {
|
||||
const n = maxConcurrency()
|
||||
expect(n).toBeGreaterThanOrEqual(1)
|
||||
expect(n).toBeLessThanOrEqual(16)
|
||||
})
|
||||
|
||||
test('Semaphore(0) 至少 1 permit,acquire 不阻塞', async () => {
|
||||
const sem = new Semaphore(0)
|
||||
const release = await sem.acquire()
|
||||
expect(release).toBeTypeOf('function')
|
||||
release()
|
||||
})
|
||||
|
||||
test('Semaphore 唤醒按 FIFO 顺序', async () => {
|
||||
const sem = new Semaphore(1)
|
||||
const order: string[] = []
|
||||
const first = await sem.acquire()
|
||||
const p1 = sem.acquire().then(r => {
|
||||
order.push('p1')
|
||||
return r
|
||||
})
|
||||
const p2 = sem.acquire().then(r => {
|
||||
order.push('p2')
|
||||
return r
|
||||
})
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
expect(order).toEqual([])
|
||||
first()
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
expect(order).toEqual(['p1'])
|
||||
;(await p1)()
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
expect(order).toEqual(['p1', 'p2'])
|
||||
;(await p2)()
|
||||
})
|
||||
|
||||
test('Semaphore.acquire 传 aborted signal → 立即 reject,不消耗 permit', async () => {
|
||||
// 修复 L:queued waiter 在 abort 时必须立即 reject 而非等 permit。
|
||||
// 否则一个被取消的 agent 阻塞在 acquire(),permit 被消耗(transfer 给已死的 waiter),
|
||||
// 实际并发能力降低;最坏情况下所有 waiter 都被取消,semaphore 还在排队等死掉的 waiter。
|
||||
const sem = new Semaphore(1)
|
||||
const ac = new AbortController()
|
||||
|
||||
// 占用唯一 permit
|
||||
const first = await sem.acquire()
|
||||
|
||||
// 排队的 waiter
|
||||
const queued = sem.acquire(ac.signal)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
|
||||
// abort → waiter 应立即 reject
|
||||
ac.abort()
|
||||
await expect(queued).rejects.toThrow()
|
||||
|
||||
// permit 无泄漏:释放 first 后,新 acquire 应能立即拿到(无 stale waiter 抢占)
|
||||
first()
|
||||
const third = await sem.acquire()
|
||||
expect(third).toBeTypeOf('function')
|
||||
third()
|
||||
})
|
||||
|
||||
test('Semaphore.acquire 传已 aborted 的 signal → 同步 reject', async () => {
|
||||
const sem = new Semaphore(1)
|
||||
const ac = new AbortController()
|
||||
ac.abort()
|
||||
// 信号已 aborted,即使有 permit 也不应 acquire(语义:调用者已取消)
|
||||
// 注意:当前实现先看 available,可能直接返回。本测试 lock "先 check abort"。
|
||||
// 若实现选择"permit 可用时优先发放",则此测试改为:acquire 成功,调用者后续检查 abort。
|
||||
// 当前实现选择前者:aborted signal 立即抛错,避免已死 agent 拿 permit。
|
||||
await expect(sem.acquire(ac.signal)).rejects.toThrow()
|
||||
})
|
||||
76
packages/workflow-engine/src/__tests__/context.test.ts
Normal file
76
packages/workflow-engine/src/__tests__/context.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { createBufferingEmitter } from '../progress/events.js'
|
||||
import {
|
||||
createEngineContext,
|
||||
createSharedResources,
|
||||
} from '../engine/context.js'
|
||||
import { WorkflowError } from '../engine/errors.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
|
||||
function mockPorts(): WorkflowPorts {
|
||||
return {
|
||||
agentRunner: { runAgentToResult: async () => ({ kind: 'dead' }) },
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
test('createSharedResources 初始化预算与计数', () => {
|
||||
const r = createSharedResources(100)
|
||||
expect(r.budget.total).toBe(100)
|
||||
expect(r.agentCountBox.value).toBe(0)
|
||||
expect(r.depth).toBe(0)
|
||||
})
|
||||
|
||||
test('createEngineContext 复制 journal 并重置游标', () => {
|
||||
const journal = [
|
||||
{
|
||||
key: 'k',
|
||||
seq: 0,
|
||||
result: { kind: 'ok' as const, output: 'x', usage: { outputTokens: 1 } },
|
||||
},
|
||||
]
|
||||
const ctx = createEngineContext({
|
||||
ports: mockPorts(),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
runId: 'r1',
|
||||
workflowName: 'w',
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
journal,
|
||||
})
|
||||
expect(ctx.journal).toHaveLength(1)
|
||||
expect(ctx.journalIndex).toBe(0)
|
||||
expect(ctx.journalInvalidated).toBe(false)
|
||||
})
|
||||
|
||||
test('createBufferingEmitter 收集事件', () => {
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
emitter.emit({ type: 'log', runId: 'r', message: 'hi' })
|
||||
expect(events).toHaveLength(1)
|
||||
})
|
||||
|
||||
test('WorkflowError 可识别', () => {
|
||||
const e = new WorkflowError('boom')
|
||||
expect(e).toBeInstanceOf(Error)
|
||||
expect(e.message).toBe('boom')
|
||||
})
|
||||
39
packages/workflow-engine/src/__tests__/errors.test.ts
Normal file
39
packages/workflow-engine/src/__tests__/errors.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { WorkflowError, WorkflowAbortedError } from '../engine/errors.js'
|
||||
|
||||
test('WorkflowError 携带消息与 name', () => {
|
||||
const e = new WorkflowError('脚本错误')
|
||||
expect(e).toBeInstanceOf(Error)
|
||||
expect(e.message).toBe('脚本错误')
|
||||
expect(e.name).toBe('WorkflowError')
|
||||
})
|
||||
|
||||
test('WorkflowAbortedError 是可识别的取消错误', () => {
|
||||
const e = new WorkflowAbortedError()
|
||||
expect(e).toBeInstanceOf(Error)
|
||||
expect(e.name).toBe('WorkflowAbortedError')
|
||||
expect(e.message).toBeTruthy()
|
||||
})
|
||||
|
||||
test('两类错误可被 instanceof 区分(互不混淆)', () => {
|
||||
const a = new WorkflowError('x')
|
||||
const b = new WorkflowAbortedError()
|
||||
expect(a).toBeInstanceOf(WorkflowError)
|
||||
expect(a).not.toBeInstanceOf(WorkflowAbortedError)
|
||||
expect(b).toBeInstanceOf(WorkflowAbortedError)
|
||||
expect(b).not.toBeInstanceOf(WorkflowError)
|
||||
})
|
||||
|
||||
test('可作为普通 Error 在 catch 中捕获', () => {
|
||||
const throwIt = (): never => {
|
||||
throw new WorkflowAbortedError()
|
||||
}
|
||||
let caught: unknown = null
|
||||
try {
|
||||
throwIt()
|
||||
} catch (e) {
|
||||
caught = e
|
||||
}
|
||||
expect(caught).toBeInstanceOf(Error)
|
||||
expect(caught).toBeInstanceOf(WorkflowAbortedError)
|
||||
})
|
||||
51
packages/workflow-engine/src/__tests__/events.test.ts
Normal file
51
packages/workflow-engine/src/__tests__/events.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import {
|
||||
createBufferingEmitter,
|
||||
createProgressEmitter,
|
||||
} from '../progress/events.js'
|
||||
import type { ProgressEvent } from '../types.js'
|
||||
|
||||
const log = (message: string): ProgressEvent =>
|
||||
({ type: 'log', runId: 'r', message }) as ProgressEvent
|
||||
const phase = (p: string): ProgressEvent =>
|
||||
({ type: 'phase_started', runId: 'r', phase: p }) as ProgressEvent
|
||||
|
||||
test('createBufferingEmitter 按序收集所有事件', () => {
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
emitter.emit(log('a'))
|
||||
emitter.emit(phase('P'))
|
||||
expect(events).toHaveLength(2)
|
||||
expect(events[0]).toEqual(log('a'))
|
||||
expect(events[1]).toEqual(phase('P'))
|
||||
})
|
||||
|
||||
test('createBufferingEmitter emit 返回 void(无返回值)', () => {
|
||||
const { emitter } = createBufferingEmitter()
|
||||
expect(emitter.emit(log('x'))).toBeUndefined()
|
||||
})
|
||||
|
||||
test('createBufferingEmitter 各自独立(不共享缓冲)', () => {
|
||||
const a = createBufferingEmitter()
|
||||
const b = createBufferingEmitter()
|
||||
a.emitter.emit(log('1'))
|
||||
expect(a.events).toHaveLength(1)
|
||||
expect(b.events).toHaveLength(0)
|
||||
})
|
||||
|
||||
test('createProgressEmitter 转发事件到回调(按序、不缓冲)', () => {
|
||||
const received: ProgressEvent[] = []
|
||||
const emitter = createProgressEmitter(e => void received.push(e))
|
||||
emitter.emit(log('a'))
|
||||
emitter.emit(log('b'))
|
||||
expect(received).toEqual([log('a'), log('b')])
|
||||
})
|
||||
|
||||
test('createProgressEmitter 回调同步触发', () => {
|
||||
let seen = ''
|
||||
const emitter = createProgressEmitter(e => {
|
||||
seen = (e as { message: string }).message
|
||||
})
|
||||
emitter.emit(log('sync'))
|
||||
// emit 返回前回调已执行
|
||||
expect(seen).toBe('sync')
|
||||
})
|
||||
426
packages/workflow-engine/src/__tests__/hooks.test.ts
Normal file
426
packages/workflow-engine/src/__tests__/hooks.test.ts
Normal file
@@ -0,0 +1,426 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { AgentAdapterRegistry } from '../agentAdapter.js'
|
||||
import { createEngineContext } from '../engine/context.js'
|
||||
import { maxConcurrency, Semaphore } from '../engine/concurrency.js'
|
||||
import { agentCallKey } from '../engine/journal.js'
|
||||
import { makeHooks, type SubWorkflowRunner } from '../engine/hooks.js'
|
||||
import { WorkflowError, WorkflowAbortedError } from '../engine/errors.js'
|
||||
import { createBufferingEmitter } from '../progress/events.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
import type {
|
||||
AgentRunParams,
|
||||
AgentRunResult,
|
||||
JournalEntry,
|
||||
ProgressEvent,
|
||||
} from '../types.js'
|
||||
|
||||
type CtxOverrides = Partial<{
|
||||
agentResults: Map<string, AgentRunResult>
|
||||
runner: (params: AgentRunParams) => Promise<AgentRunResult>
|
||||
pending: { kind: 'skip' | 'retry' } | null
|
||||
journal: JournalEntry[]
|
||||
budgetTotal: number | null
|
||||
signal: AbortSignal
|
||||
truncated: string[]
|
||||
agentAdapterRegistry: AgentAdapterRegistry
|
||||
loggerWarn: (msg: string) => void
|
||||
}>
|
||||
|
||||
function buildCtx(overrides: CtxOverrides = {}): {
|
||||
ctx: ReturnType<typeof createEngineContext>
|
||||
events: ProgressEvent[]
|
||||
hooks: ReturnType<typeof makeHooks>
|
||||
} {
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
const results = overrides.agentResults ?? new Map<string, AgentRunResult>()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: overrides.runner
|
||||
? overrides.runner
|
||||
: async (params: AgentRunParams) =>
|
||||
results.get(params.prompt) ?? { kind: 'dead' },
|
||||
},
|
||||
...(overrides.agentAdapterRegistry
|
||||
? { agentAdapterRegistry: overrides.agentAdapterRegistry }
|
||||
: {}),
|
||||
progressEmitter: emitter,
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => overrides.pending ?? null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async (id: string) => {
|
||||
overrides.truncated?.push(id)
|
||||
},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: {
|
||||
debug: () => {},
|
||||
event: () => {},
|
||||
...(overrides.loggerWarn ? { warn: overrides.loggerWarn } : {}),
|
||||
},
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const ctx = createEngineContext({
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: overrides.signal ?? new AbortController().signal,
|
||||
runId: 'r1',
|
||||
workflowName: 'w',
|
||||
cwd: '/tmp',
|
||||
budgetTotal: overrides.budgetTotal ?? null,
|
||||
journal: overrides.journal,
|
||||
})
|
||||
const noopSub: SubWorkflowRunner = async () => null
|
||||
return { ctx, events, hooks: makeHooks(ctx, noopSub) }
|
||||
}
|
||||
|
||||
test('agent 返回文本结果并计数', async () => {
|
||||
const { ctx, hooks } = buildCtx({
|
||||
agentResults: new Map([
|
||||
['hi', { kind: 'ok', output: 'hello', usage: { outputTokens: 5 } }],
|
||||
]),
|
||||
})
|
||||
const out = await hooks.agent('hi')
|
||||
expect(out).toBe('hello')
|
||||
expect(ctx.resources.agentCountBox.value).toBe(1)
|
||||
})
|
||||
|
||||
test('agent skipped → null 且不计数', async () => {
|
||||
const { hooks } = buildCtx({
|
||||
agentResults: new Map([['hi', { kind: 'skipped' }]]),
|
||||
})
|
||||
expect(await hooks.agent('hi')).toBeNull()
|
||||
})
|
||||
|
||||
test('agent dead → null', async () => {
|
||||
const { hooks } = buildCtx({
|
||||
agentResults: new Map([['hi', { kind: 'dead' }]]),
|
||||
})
|
||||
expect(await hooks.agent('hi')).toBeNull()
|
||||
})
|
||||
|
||||
test('agent journal 命中时不调用 runner', async () => {
|
||||
let called = 0
|
||||
const { emitter } = createBufferingEmitter()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async () => {
|
||||
called++
|
||||
return { kind: 'ok', output: 'live', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: emitter,
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const key = agentCallKey('hi', { prompt: 'hi' })
|
||||
const ctx = createEngineContext({
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
runId: 'r1',
|
||||
workflowName: 'w',
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
journal: [
|
||||
{
|
||||
key,
|
||||
seq: 0,
|
||||
result: { kind: 'ok', output: 'cached', usage: { outputTokens: 1 } },
|
||||
},
|
||||
],
|
||||
})
|
||||
const hooks = makeHooks(ctx, async () => null)
|
||||
expect(await hooks.agent('hi')).toBe('cached')
|
||||
expect(called).toBe(0)
|
||||
})
|
||||
|
||||
test('agent 超过总数上限抛错', async () => {
|
||||
const { hooks, ctx } = buildCtx()
|
||||
ctx.resources.agentCountBox.value = 1000
|
||||
await expect(hooks.agent('hi')).rejects.toThrow(WorkflowError)
|
||||
})
|
||||
|
||||
test('parallel 单项抛错 → null,其余保留', async () => {
|
||||
const { hooks } = buildCtx()
|
||||
const out = await hooks.parallel([
|
||||
async () => 'a',
|
||||
async () => {
|
||||
throw new Error('x')
|
||||
},
|
||||
async () => 'c',
|
||||
])
|
||||
expect(out).toEqual(['a', null, 'c'])
|
||||
})
|
||||
|
||||
test('parallel 单项抛错 → logger.warn 记录失败原因', async () => {
|
||||
const warns: string[] = []
|
||||
const { hooks } = buildCtx({ loggerWarn: msg => warns.push(msg) })
|
||||
await hooks.parallel([
|
||||
async () => 'a',
|
||||
async () => {
|
||||
throw new Error('boom-x')
|
||||
},
|
||||
async () => 'c',
|
||||
])
|
||||
expect(warns.length).toBe(1)
|
||||
expect(warns[0]).toMatch(/boom-x/)
|
||||
})
|
||||
|
||||
test('pipeline 逐 stage 链式,stage 抛错 → null', async () => {
|
||||
const { hooks } = buildCtx()
|
||||
const out = await hooks.pipeline(
|
||||
[1, 2],
|
||||
n => Promise.resolve((n as number) + 1),
|
||||
m => Promise.resolve((m as number) * 10),
|
||||
)
|
||||
expect(out).toEqual([20, 30])
|
||||
const out2 = await hooks.pipeline(
|
||||
[1],
|
||||
() => Promise.reject(new Error('boom')),
|
||||
m => Promise.resolve(m),
|
||||
)
|
||||
expect(out2).toEqual([null])
|
||||
})
|
||||
|
||||
test('pipeline stage 抛错 → logger.warn 记录失败原因', async () => {
|
||||
const warns: string[] = []
|
||||
const { hooks } = buildCtx({ loggerWarn: msg => warns.push(msg) })
|
||||
await hooks.pipeline(
|
||||
[1],
|
||||
() => Promise.reject(new Error('stage-boom')),
|
||||
m => Promise.resolve(m),
|
||||
)
|
||||
expect(warns.length).toBe(1)
|
||||
expect(warns[0]).toMatch(/stage-boom/)
|
||||
})
|
||||
|
||||
test('pipeline 超 4096 抛错', async () => {
|
||||
const { hooks } = buildCtx()
|
||||
await expect(
|
||||
hooks.pipeline(Array(4097), () => Promise.resolve(1)),
|
||||
).rejects.toThrow(WorkflowError)
|
||||
})
|
||||
|
||||
test('phase 切换发射 phase_started/done;log 发射 log', () => {
|
||||
const { hooks, events } = buildCtx()
|
||||
hooks.phase('A')
|
||||
hooks.log('hello')
|
||||
hooks.phase('B')
|
||||
expect(events.some(e => e.type === 'phase_started' && e.phase === 'A')).toBe(
|
||||
true,
|
||||
)
|
||||
expect(events.some(e => e.type === 'phase_done' && e.phase === 'A')).toBe(
|
||||
true,
|
||||
)
|
||||
expect(events.some(e => e.type === 'log' && e.message === 'hello')).toBe(true)
|
||||
expect(events.some(e => e.type === 'phase_started' && e.phase === 'B')).toBe(
|
||||
true,
|
||||
)
|
||||
})
|
||||
|
||||
// ---- 边界与错误路径 ----
|
||||
|
||||
test('agent dead 也计入 agentCountBox', async () => {
|
||||
const { hooks, ctx } = buildCtx({
|
||||
agentResults: new Map([['x', { kind: 'dead' }]]),
|
||||
})
|
||||
await hooks.agent('x')
|
||||
expect(ctx.resources.agentCountBox.value).toBe(1)
|
||||
})
|
||||
|
||||
test('agent pendingAction=skip → null、不调 runner、不计数', async () => {
|
||||
let called = 0
|
||||
const { hooks, ctx } = buildCtx({
|
||||
pending: { kind: 'skip' },
|
||||
runner: async () => {
|
||||
called++
|
||||
return { kind: 'ok', output: 'x', usage: { outputTokens: 1 } }
|
||||
},
|
||||
})
|
||||
expect(await hooks.agent('x')).toBeNull()
|
||||
expect(called).toBe(0)
|
||||
expect(ctx.resources.agentCountBox.value).toBe(0)
|
||||
})
|
||||
|
||||
test('agent journal key 发散 → invalidate 并 truncate', async () => {
|
||||
const truncated: string[] = []
|
||||
const { hooks, ctx } = buildCtx({
|
||||
runner: async () => ({
|
||||
kind: 'ok',
|
||||
output: 'live',
|
||||
usage: { outputTokens: 1 },
|
||||
}),
|
||||
journal: [
|
||||
{
|
||||
key: 'stale-key',
|
||||
seq: 0,
|
||||
result: { kind: 'ok', output: 'old', usage: { outputTokens: 1 } },
|
||||
},
|
||||
],
|
||||
truncated,
|
||||
})
|
||||
const out = await hooks.agent('different-prompt')
|
||||
expect(out).toBe('live')
|
||||
expect(truncated).toContain('r1')
|
||||
expect(ctx.journalInvalidated).toBe(true)
|
||||
})
|
||||
|
||||
test('agent 预算耗尽时抛错', async () => {
|
||||
const { hooks, ctx } = buildCtx({
|
||||
budgetTotal: 10,
|
||||
runner: async () => ({
|
||||
kind: 'ok',
|
||||
output: 'x',
|
||||
usage: { outputTokens: 1 },
|
||||
}),
|
||||
})
|
||||
ctx.resources.budget.addOutputTokens(10)
|
||||
await expect(hooks.agent('x')).rejects.toThrow()
|
||||
})
|
||||
|
||||
test('agent 预算检查在 semaphore 临界区内(queued waiter 看到最新 spent)', async () => {
|
||||
// 当 semaphore capacity < parallel agent 数时,部分 agent 会排队。
|
||||
// 旧 bug:assertCanSpend 在 acquire 之前,所有 waiter 入队时 spent=0 都过检;
|
||||
// 后续 permit 释放后 waiter 直接跑 runner、扣预算,不再 re-check → 全部超支。
|
||||
// 修复:assertCanSpend 移入临界区,waiter 被唤醒后先看 spent 再决定是否跑。
|
||||
// 强制 capacity=1(serializing semaphore)确保 N>1 个 agent 必须排队。
|
||||
const { hooks, ctx } = buildCtx({
|
||||
budgetTotal: 10,
|
||||
runner: async () => {
|
||||
// 让 runner 慢一点,确保 waiter 真的排队
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: 'x',
|
||||
usage: { outputTokens: 6 }, // 每次 6 token,2 次即超 10
|
||||
}
|
||||
},
|
||||
})
|
||||
// 用单 permit semaphore 替换默认的,强制序列化
|
||||
ctx.resources.semaphore = new Semaphore(1)
|
||||
const results = await hooks.parallel([
|
||||
() => hooks.agent('a'),
|
||||
() => hooks.agent('b'),
|
||||
() => hooks.agent('c'),
|
||||
() => hooks.agent('d'),
|
||||
])
|
||||
// 至少 1 个 agent 被 parallel catch 成 null(assertCanSpend 抛错)
|
||||
expect(results.some(r => r === null)).toBe(true)
|
||||
// 不应 4 个全跑扣 24;上限是 at-most-one-over(前两个扣 12,后两个被拦)
|
||||
expect(ctx.resources.budget.spent()).toBeLessThanOrEqual(12)
|
||||
})
|
||||
|
||||
test('agent signal aborted → WorkflowAbortedError', async () => {
|
||||
const ac = new AbortController()
|
||||
ac.abort()
|
||||
const { hooks } = buildCtx({
|
||||
signal: ac.signal,
|
||||
runner: async () => ({
|
||||
kind: 'ok',
|
||||
output: 'x',
|
||||
usage: { outputTokens: 1 },
|
||||
}),
|
||||
})
|
||||
await expect(hooks.agent('x')).rejects.toThrow(WorkflowAbortedError)
|
||||
})
|
||||
|
||||
test('parallel 超过 4096 项抛错', async () => {
|
||||
const { hooks } = buildCtx()
|
||||
await expect(
|
||||
hooks.parallel(Array.from({ length: 4097 }, () => async () => 1)),
|
||||
).rejects.toThrow(WorkflowError)
|
||||
})
|
||||
|
||||
test('workflow() 嵌套超过一层抛错', async () => {
|
||||
const { hooks, ctx } = buildCtx()
|
||||
ctx.resources.depth = 1
|
||||
await expect(hooks.workflow('child')).rejects.toThrow(WorkflowError)
|
||||
})
|
||||
|
||||
test('agent 并发受 semaphore 限制(不超 maxConcurrency)', async () => {
|
||||
let active = 0
|
||||
let peak = 0
|
||||
const { hooks } = buildCtx({
|
||||
runner: async () => {
|
||||
active++
|
||||
peak = Math.max(peak, active)
|
||||
await new Promise(r => {
|
||||
setTimeout(r, 5)
|
||||
})
|
||||
active--
|
||||
return { kind: 'ok', output: 'x', usage: { outputTokens: 1 } }
|
||||
},
|
||||
})
|
||||
await hooks.parallel(Array.from({ length: 32 }, () => () => hooks.agent('p')))
|
||||
expect(peak).toBeLessThanOrEqual(maxConcurrency())
|
||||
})
|
||||
|
||||
test('agentAdapterRegistry 优先于 agentRunner(按路由分发到 adapter)', async () => {
|
||||
const called: string[] = []
|
||||
const registry = new AgentAdapterRegistry()
|
||||
.register({
|
||||
id: 'ad',
|
||||
capabilities: { structuredOutput: true },
|
||||
async run() {
|
||||
called.push('adapter')
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: 'from-adapter',
|
||||
usage: { outputTokens: 1 },
|
||||
}
|
||||
},
|
||||
})
|
||||
.default('ad')
|
||||
const { hooks } = buildCtx({
|
||||
agentAdapterRegistry: registry,
|
||||
runner: async () => {
|
||||
called.push('runner')
|
||||
return { kind: 'ok', output: 'from-runner', usage: { outputTokens: 1 } }
|
||||
},
|
||||
})
|
||||
expect(await hooks.agent('x')).toBe('from-adapter')
|
||||
expect(called).toEqual(['adapter'])
|
||||
})
|
||||
|
||||
test('agentAdapterRegistry resolve 抛错 → agent 上抛(workflow failed)', async () => {
|
||||
const registry = new AgentAdapterRegistry().default('missing') // 未注册
|
||||
const { hooks } = buildCtx({
|
||||
agentAdapterRegistry: registry,
|
||||
runner: async () => ({
|
||||
kind: 'ok',
|
||||
output: 'x',
|
||||
usage: { outputTokens: 1 },
|
||||
}),
|
||||
})
|
||||
await expect(hooks.agent('x')).rejects.toThrow()
|
||||
})
|
||||
88
packages/workflow-engine/src/__tests__/index.test.ts
Normal file
88
packages/workflow-engine/src/__tests__/index.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import * as wf from '../index.js'
|
||||
|
||||
test('引擎核心 API 完整导出', () => {
|
||||
expect(typeof wf.runWorkflow).toBe('function')
|
||||
expect(typeof wf.parseScript).toBe('function')
|
||||
expect(typeof wf.extractMeta).toBe('function')
|
||||
expect(typeof wf.makeHooks).toBe('function')
|
||||
expect(typeof wf.createEngineContext).toBe('function')
|
||||
expect(typeof wf.createSharedResources).toBe('function')
|
||||
})
|
||||
|
||||
test('端口 / host API 完整导出', () => {
|
||||
expect(typeof wf.createHostHandle).toBe('function')
|
||||
expect(typeof wf.isHostHandle).toBe('function')
|
||||
expect(typeof wf.unwrapHostHandle).toBe('function')
|
||||
})
|
||||
|
||||
test('持久化 / 结构化 / 命名 workflow / 进度 API 完整导出', () => {
|
||||
expect(typeof wf.createFileJournalStore).toBe('function')
|
||||
expect(typeof wf.agentCallKey).toBe('function')
|
||||
expect(typeof wf.validateAgainstSchema).toBe('function')
|
||||
expect(typeof wf.resolveNamedWorkflow).toBe('function')
|
||||
expect(typeof wf.listNamedWorkflows).toBe('function')
|
||||
expect(typeof wf.createBufferingEmitter).toBe('function')
|
||||
expect(typeof wf.createProgressEmitter).toBe('function')
|
||||
})
|
||||
|
||||
test('并发 / 预算 / 错误类完整导出', () => {
|
||||
expect(typeof wf.Semaphore).toBe('function')
|
||||
expect(typeof wf.maxConcurrency).toBe('function')
|
||||
expect(typeof wf.Budget).toBe('function')
|
||||
expect(typeof wf.BudgetExhaustedError).toBe('function')
|
||||
expect(typeof wf.WorkflowError).toBe('function')
|
||||
expect(typeof wf.WorkflowAbortedError).toBe('function')
|
||||
expect(typeof wf.ScriptError).toBe('function')
|
||||
})
|
||||
|
||||
test('工具描述符与输入 schema 导出', () => {
|
||||
expect(typeof wf.createWorkflowTool).toBe('function')
|
||||
expect(typeof wf.workflowInputSchema).toBe('object')
|
||||
expect(wf.WORKFLOW_TOOL_NAME).toBe('Workflow')
|
||||
})
|
||||
|
||||
test('引擎常量值稳定', () => {
|
||||
expect(wf.WORKFLOW_DIR_NAME).toBe('.claude/workflows')
|
||||
expect(wf.WORKFLOW_RUNS_DIR).toBe('.claude/workflow-runs')
|
||||
expect(wf.WORKFLOW_TOOL_NAME).toBe('Workflow')
|
||||
expect(wf.MAX_TOTAL_AGENTS).toBe(1000)
|
||||
expect(wf.MAX_ITEMS_PER_CALL).toBe(4096)
|
||||
expect(wf.MAX_CONCURRENCY_CAP).toBe(16)
|
||||
expect(wf.MAX_CONCURRENCY_OFFSET).toBe(2)
|
||||
expect(wf.WORKFLOW_SCRIPT_EXTENSIONS).toEqual(['.ts', '.js', '.mjs'])
|
||||
})
|
||||
|
||||
test('createWorkflowTool 返回完整描述符形状', () => {
|
||||
const tool = wf.createWorkflowTool({
|
||||
agentRunner: { runAgentToResult: async () => ({ kind: 'dead' }) },
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete() {},
|
||||
fail() {},
|
||||
kill() {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: wf.createHostHandle(null),
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
}),
|
||||
})
|
||||
expect(tool.name).toBe('Workflow')
|
||||
expect(tool.isEnabled()).toBe(true)
|
||||
expect(tool.isReadOnly({})).toBe(false)
|
||||
expect(typeof tool.call).toBe('function')
|
||||
expect(typeof tool.description).toBe('function')
|
||||
expect(typeof tool.prompt).toBe('function')
|
||||
expect(typeof tool.renderToolUseMessage).toBe('function')
|
||||
expect(typeof tool.mapToolResultToToolResultBlockParam).toBe('function')
|
||||
})
|
||||
282
packages/workflow-engine/src/__tests__/integration.test.ts
Normal file
282
packages/workflow-engine/src/__tests__/integration.test.ts
Normal file
@@ -0,0 +1,282 @@
|
||||
/**
|
||||
* 集成测试:用忠实 mock adapter 跑「规范 workflow 脚本」(来自 Workflow 工具定义的
|
||||
* canonical 模式:pipeline 无屏障 + parallel 屏障 + agent(schema) + phase)。
|
||||
* 验证引擎与真实 workflow 脚本语义兼容。
|
||||
*/
|
||||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { runWorkflow } from '../engine/runWorkflow.js'
|
||||
import { createFileJournalStore } from '../engine/journal.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
import { createBufferingEmitter } from '../progress/events.js'
|
||||
import type { AgentRunParams, AgentRunResult, ProgressEvent } from '../types.js'
|
||||
|
||||
function canonicalPorts(runsDir: string): {
|
||||
ports: WorkflowPorts
|
||||
events: ProgressEvent[]
|
||||
agentCalls: AgentRunParams[]
|
||||
} {
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
const agentCalls: AgentRunParams[] = []
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (
|
||||
params: AgentRunParams,
|
||||
): Promise<AgentRunResult> => {
|
||||
agentCalls.push(params)
|
||||
const p = params.prompt
|
||||
if (p.startsWith('review-')) {
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: { findings: [{ title: `${p}-finding`, file: 'a.ts' }] },
|
||||
usage: { outputTokens: 5 },
|
||||
}
|
||||
}
|
||||
if (p.startsWith('verify')) {
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: { isReal: true },
|
||||
usage: { outputTokens: 2 },
|
||||
}
|
||||
}
|
||||
return { kind: 'dead' }
|
||||
},
|
||||
},
|
||||
progressEmitter: emitter,
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: runsDir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
return { ports, events, agentCalls }
|
||||
}
|
||||
|
||||
// 规范 review 模式(pipeline→parallel→verify→synthesize),逐字采用 Workflow 工具定义的写法。
|
||||
const CANONICAL_REVIEW_SCRIPT = `
|
||||
export const meta = {
|
||||
name: 'review-changes',
|
||||
description: 'Review changed files across dimensions, verify each finding',
|
||||
phases: [{ title: 'Review' }, { title: 'Verify' }],
|
||||
}
|
||||
const DIMENSIONS = [
|
||||
{ key: 'bugs', prompt: 'review-bugs' },
|
||||
{ key: 'perf', prompt: 'review-perf' },
|
||||
]
|
||||
const FINDINGS_SCHEMA = { type: 'object' }
|
||||
const VERDICT_SCHEMA = { type: 'object' }
|
||||
|
||||
phase('Review')
|
||||
const results = await pipeline(
|
||||
DIMENSIONS,
|
||||
d => agent(d.prompt, { label: 'review:' + d.key, phase: 'Review', schema: FINDINGS_SCHEMA }),
|
||||
review => parallel(
|
||||
review.findings.map(f => () =>
|
||||
agent('verify: ' + f.title, { label: 'verify:' + f.file, phase: 'Verify', schema: VERDICT_SCHEMA })
|
||||
.then(v => ({ ...f, verdict: v }))
|
||||
)
|
||||
)
|
||||
)
|
||||
const all = results.flat().filter(Boolean)
|
||||
const confirmed = all.filter(f => f.verdict && f.verdict.isReal)
|
||||
return { confirmed, total: all.length }
|
||||
`
|
||||
|
||||
test('canonical review 脚本端到端兼容', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-int-'))
|
||||
try {
|
||||
const { ports, events, agentCalls } = canonicalPorts(dir)
|
||||
const result = await runWorkflow({
|
||||
script: CANONICAL_REVIEW_SCRIPT,
|
||||
runId: 'int-1',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
|
||||
expect(result.status).toBe('completed')
|
||||
const ret = result.returnValue as { confirmed: unknown[]; total: number }
|
||||
// 2 维度 × 1 finding,全部 isReal=true → confirmed=2, total=2
|
||||
expect(ret.total).toBe(2)
|
||||
expect(ret.confirmed).toHaveLength(2)
|
||||
// 2 个 review agent + 2 个 verify agent = 4
|
||||
expect(agentCalls).toHaveLength(4)
|
||||
expect(agentCalls.filter(c => c.prompt.startsWith('review-'))).toHaveLength(
|
||||
2,
|
||||
)
|
||||
expect(agentCalls.filter(c => c.prompt.startsWith('verify'))).toHaveLength(
|
||||
2,
|
||||
)
|
||||
// 进度事件:run_started/done + phase Review/Verify + agent started/done
|
||||
expect(
|
||||
events.some(
|
||||
e => e.type === 'run_started' && e.workflowName === 'review-changes',
|
||||
),
|
||||
).toBe(true)
|
||||
expect(
|
||||
events.some(e => e.type === 'run_done' && e.status === 'completed'),
|
||||
).toBe(true)
|
||||
// 脚本显式调用一次 phase('Review');verify agent 的 phase:'Verify' 是展示标签,不发 phase_started
|
||||
expect(
|
||||
events.filter(e => e.type === 'phase_started' && e.phase === 'Review'),
|
||||
).toHaveLength(1)
|
||||
expect(events.filter(e => e.type === 'agent_started')).toHaveLength(4)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('loop-until-dry 模式:连续两轮无新发现即收敛', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-int-'))
|
||||
try {
|
||||
let round = 0
|
||||
const { emitter, events } = createBufferingEmitter()
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (
|
||||
p: AgentRunParams,
|
||||
): Promise<AgentRunResult> => {
|
||||
round++
|
||||
// 第 1-2 轮返回发现,第 3 轮起返回空 → 收敛
|
||||
const found = round <= 2 ? [{ b: round }] : []
|
||||
return {
|
||||
kind: 'ok',
|
||||
output: { bugs: found },
|
||||
usage: { outputTokens: 1 },
|
||||
}
|
||||
},
|
||||
},
|
||||
progressEmitter: emitter,
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(dir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const script = `
|
||||
const seen = []
|
||||
const confirmed = []
|
||||
let dry = 0
|
||||
while (dry < 2) {
|
||||
const found = (await agent('find bugs')).bugs
|
||||
const fresh = found.filter(b => !seen.includes(b.b))
|
||||
if (fresh.length === 0) { dry++; continue }
|
||||
dry = 0
|
||||
for (const b of fresh) seen.push(b.b)
|
||||
confirmed.push(...fresh)
|
||||
}
|
||||
return { confirmed }
|
||||
`
|
||||
const result = await runWorkflow({
|
||||
script,
|
||||
runId: 'int-2',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('completed')
|
||||
const ret = result.returnValue as { confirmed: { b: number }[] }
|
||||
// 第1轮发现{b:1},第2轮发现{b:2}(fresh,因 seen=[1]),第3轮 found{b:3}?
|
||||
// mock 按 round 计数:round1→{b:1}, round2→{b:2}, round3→[](found空)
|
||||
// 但 round2 found=[{b:2}], seen=[1], fresh=[{b:2}] → confirmed=[{b:1},{b:2}], dry=0
|
||||
// round3 found=[] → fresh=[] → dry=1; round4 found=[] → dry=2 → 退出
|
||||
expect(ret.confirmed).toHaveLength(2)
|
||||
expect(
|
||||
events.some(e => e.type === 'run_done' && e.status === 'completed'),
|
||||
).toBe(true)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('resume 兼容:二次运行 journal 命中,agent 不重跑', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-int-'))
|
||||
try {
|
||||
let calls = 0
|
||||
const makePorts = (): WorkflowPorts => ({
|
||||
agentRunner: {
|
||||
runAgentToResult: async () => {
|
||||
calls++
|
||||
return { kind: 'ok', output: 'live', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(dir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
})
|
||||
const script = `
|
||||
phase('A')
|
||||
const a = await agent('do-a')
|
||||
const b = await agent('do-b')
|
||||
return { a, b }
|
||||
`
|
||||
// 第一次运行:2 个 agent 现场跑
|
||||
const first = await runWorkflow({
|
||||
script,
|
||||
runId: 'int-3',
|
||||
ports: makePorts(),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(first.status).toBe('completed')
|
||||
expect(calls).toBe(2)
|
||||
|
||||
// resume 同 runId:journal 命中,不重跑
|
||||
calls = 0
|
||||
const resumed = await runWorkflow({
|
||||
script,
|
||||
runId: 'int-3',
|
||||
ports: makePorts(),
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
resume: true,
|
||||
})
|
||||
expect(resumed.status).toBe('completed')
|
||||
expect(calls).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
113
packages/workflow-engine/src/__tests__/journal.test.ts
Normal file
113
packages/workflow-engine/src/__tests__/journal.test.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { agentCallKey, createFileJournalStore } from '../engine/journal.js'
|
||||
import type { AgentRunParams } from '../types.js'
|
||||
|
||||
const base: AgentRunParams = { prompt: 'do something' }
|
||||
|
||||
test('agentCallKey 对相同 prompt+params 稳定', () => {
|
||||
expect(agentCallKey('p', base)).toBe(agentCallKey('p', base))
|
||||
})
|
||||
|
||||
test('agentCallKey 随 prompt 变化', () => {
|
||||
expect(agentCallKey('p1', base)).not.toBe(agentCallKey('p2', base))
|
||||
})
|
||||
|
||||
test('agentCallKey 忽略纯展示字段 label/phase', () => {
|
||||
const a = agentCallKey('p', { ...base, label: 'A', phase: 'ph1' })
|
||||
const b = agentCallKey('p', { ...base, label: 'B', phase: 'ph2' })
|
||||
expect(a).toBe(b)
|
||||
})
|
||||
|
||||
test('FileJournalStore append → read 保序,truncate 清空', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-journal-'))
|
||||
try {
|
||||
const store = createFileJournalStore(dir)
|
||||
const e1 = {
|
||||
key: 'k1',
|
||||
seq: 0,
|
||||
result: { kind: 'ok' as const, output: 'x', usage: { outputTokens: 1 } },
|
||||
}
|
||||
const e2 = { key: 'k2', seq: 1, result: { kind: 'dead' as const } }
|
||||
await store.append('run-1', e1)
|
||||
await store.append('run-1', e2)
|
||||
const got = await store.read('run-1')
|
||||
expect(got).toHaveLength(2)
|
||||
expect(got[0]!.key).toBe('k1')
|
||||
expect(got[1]!.result.kind).toBe('dead')
|
||||
await store.truncate('run-1')
|
||||
expect(await store.read('run-1')).toEqual([])
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('FileJournalStore read 按 seq 排序——parallel 完成顺序≠调用顺序时 resume 稳定', async () => {
|
||||
// 并发完成顺序不确定:append 落盘 = completion 顺序;resume 时按调用顺序
|
||||
// 匹配 key。无 seq 排序 → 不同 run 的 key 顺序不同 → 几乎所有 key mismatch →
|
||||
// 全重跑,journal 失效。修复:read() 按 seq 升序整理后再返回。
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-journal-sort-'))
|
||||
try {
|
||||
const store = createFileJournalStore(dir)
|
||||
await store.append('r1', {
|
||||
key: 'late',
|
||||
seq: 2,
|
||||
result: { kind: 'ok', output: 'late', usage: { outputTokens: 1 } },
|
||||
})
|
||||
await store.append('r1', {
|
||||
key: 'first',
|
||||
seq: 0,
|
||||
result: { kind: 'ok', output: 'first', usage: { outputTokens: 1 } },
|
||||
})
|
||||
await store.append('r1', {
|
||||
key: 'mid',
|
||||
seq: 1,
|
||||
result: { kind: 'ok', output: 'mid', usage: { outputTokens: 1 } },
|
||||
})
|
||||
const got = await store.read('r1')
|
||||
expect(got.map(e => e.key)).toEqual(['first', 'mid', 'late'])
|
||||
expect(got.map(e => e.seq)).toEqual([0, 1, 2])
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('agentCallKey 随 schema 变化', () => {
|
||||
const k0 = agentCallKey('p', { prompt: 'p' })
|
||||
const k1 = agentCallKey('p', { prompt: 'p', schema: { type: 'object' } })
|
||||
const k2 = agentCallKey('p', { prompt: 'p', schema: { type: 'array' } })
|
||||
expect(k1).not.toBe(k0)
|
||||
expect(k1).not.toBe(k2)
|
||||
})
|
||||
|
||||
test('agentCallKey 随 model 变化', () => {
|
||||
expect(agentCallKey('p', { prompt: 'p', model: 'sonnet' })).not.toBe(
|
||||
agentCallKey('p', { prompt: 'p', model: 'opus' }),
|
||||
)
|
||||
})
|
||||
|
||||
test('agentCallKey 对 params 字段顺序稳定(canonical 排序)', () => {
|
||||
const a = agentCallKey('p', {
|
||||
prompt: 'p',
|
||||
model: 'm',
|
||||
schema: { type: 'object' },
|
||||
})
|
||||
const b = agentCallKey('p', {
|
||||
schema: { type: 'object' },
|
||||
prompt: 'p',
|
||||
model: 'm',
|
||||
})
|
||||
expect(a).toBe(b)
|
||||
})
|
||||
|
||||
test('FileJournalStore read 不存在的 run → []', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-journal-'))
|
||||
try {
|
||||
const store = createFileJournalStore(dir)
|
||||
expect(await store.read('never-existed')).toEqual([])
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,68 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import {
|
||||
listNamedWorkflows,
|
||||
resolveNamedWorkflow,
|
||||
} from '../engine/namedWorkflows.js'
|
||||
|
||||
test('按扩展名优先级解析命名 workflow', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-named-'))
|
||||
try {
|
||||
await writeFile(
|
||||
join(dir, 'a.ts'),
|
||||
'export const meta = { name: "a", description: "d" }\nreturn 1',
|
||||
)
|
||||
await writeFile(join(dir, 'b.js'), 'return 2')
|
||||
await writeFile(join(dir, 'c.mjs'), 'return 3')
|
||||
await writeFile(join(dir, 'ignore.md'), '# not a workflow')
|
||||
|
||||
const a = await resolveNamedWorkflow(dir, 'a')
|
||||
expect(a?.path.endsWith('a.ts')).toBe(true)
|
||||
expect(a?.content).toContain('meta')
|
||||
|
||||
expect(await resolveNamedWorkflow(dir, 'missing')).toBeNull()
|
||||
|
||||
const names = await listNamedWorkflows(dir)
|
||||
expect(names).toEqual(['a', 'b', 'c']) // 不含 .md
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('listNamedWorkflows 不存在目录返回空数组', async () => {
|
||||
expect(
|
||||
await listNamedWorkflows(join(tmpdir(), 'wf-nope-' + Date.now())),
|
||||
).toEqual([])
|
||||
})
|
||||
|
||||
test('resolveNamedWorkflow 在 .ts 缺失时降级到 .js/.mjs', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-named-'))
|
||||
try {
|
||||
await writeFile(join(dir, 'onlyjs.js'), 'return 1')
|
||||
await writeFile(join(dir, 'onlymjs.mjs'), 'return 2')
|
||||
expect(
|
||||
(await resolveNamedWorkflow(dir, 'onlyjs'))?.path.endsWith('onlyjs.js'),
|
||||
).toBe(true)
|
||||
expect(
|
||||
(await resolveNamedWorkflow(dir, 'onlymjs'))?.path.endsWith(
|
||||
'onlymjs.mjs',
|
||||
),
|
||||
).toBe(true)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('listNamedWorkflows 返回排序后的名字', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-named-'))
|
||||
try {
|
||||
await writeFile(join(dir, 'zeta.ts'), 'return 1')
|
||||
await writeFile(join(dir, 'alpha.js'), 'return 2')
|
||||
await writeFile(join(dir, 'mid.mjs'), 'return 3')
|
||||
expect(await listNamedWorkflows(dir)).toEqual(['alpha', 'mid', 'zeta'])
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
56
packages/workflow-engine/src/__tests__/paths.test.ts
Normal file
56
packages/workflow-engine/src/__tests__/paths.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { containsPath, sanitizeWorkflowName } from '../engine/paths.js'
|
||||
|
||||
test('containsPath: target 等于 base → true', () => {
|
||||
const base = join(tmpdir(), 'a')
|
||||
expect(containsPath(base, base)).toBe(true)
|
||||
})
|
||||
|
||||
test('containsPath: target 在 base 内 → true', () => {
|
||||
const base = join(tmpdir(), 'a')
|
||||
const target = join(base, 'b', 'c.ts')
|
||||
expect(containsPath(base, target)).toBe(true)
|
||||
})
|
||||
|
||||
test('containsPath: target 在 base 之外(前缀假阳)→ false', () => {
|
||||
// /tmp/foobar 不应被认为是 /tmp/foo 的子路径
|
||||
const base = join(tmpdir(), 'foo')
|
||||
const target = join(tmpdir(), 'foobar', 'x.ts')
|
||||
expect(containsPath(base, target)).toBe(false)
|
||||
})
|
||||
|
||||
test('containsPath: target 用 .. 越界 → false', () => {
|
||||
const base = join(tmpdir(), 'a', 'b')
|
||||
const target = join(base, '..', 'outside.ts')
|
||||
expect(containsPath(base, target)).toBe(false)
|
||||
})
|
||||
|
||||
test('containsPath: 相对 target 相对 base 解析', () => {
|
||||
const base = join(tmpdir(), 'a')
|
||||
expect(containsPath(base, 'sub/file.ts')).toBe(true)
|
||||
expect(containsPath(base, '../b/file.ts')).toBe(false)
|
||||
})
|
||||
|
||||
test('sanitizeWorkflowName: 合法标识符 → 原值', () => {
|
||||
expect(sanitizeWorkflowName('release')).toBe('release')
|
||||
expect(sanitizeWorkflowName('my-workflow')).toBe('my-workflow')
|
||||
expect(sanitizeWorkflowName('my_workflow_2')).toBe('my_workflow_2')
|
||||
})
|
||||
|
||||
test('sanitizeWorkflowName: 含路径分隔符 → null', () => {
|
||||
expect(sanitizeWorkflowName('foo/bar')).toBeNull()
|
||||
expect(sanitizeWorkflowName('foo\\bar')).toBeNull()
|
||||
expect(sanitizeWorkflowName('/abs/path')).toBeNull()
|
||||
})
|
||||
|
||||
test('sanitizeWorkflowName: . / .. / 空 → null', () => {
|
||||
expect(sanitizeWorkflowName('.')).toBeNull()
|
||||
expect(sanitizeWorkflowName('..')).toBeNull()
|
||||
expect(sanitizeWorkflowName('')).toBeNull()
|
||||
})
|
||||
|
||||
test('sanitizeWorkflowName: 含 null 字节 → null', () => {
|
||||
expect(sanitizeWorkflowName('evil\0.ts')).toBeNull()
|
||||
})
|
||||
61
packages/workflow-engine/src/__tests__/ports.test.ts
Normal file
61
packages/workflow-engine/src/__tests__/ports.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { createHostHandle, isHostHandle, unwrapHostHandle } from '../ports.js'
|
||||
|
||||
test('createHostHandle 包装任意 bundle 且对外不透明', () => {
|
||||
const bundle = { secret: 'ctx', nested: { a: 1 } }
|
||||
const handle = createHostHandle(bundle)
|
||||
expect(isHostHandle(handle)).toBe(true)
|
||||
// 包内不暴露 bundle —— handle 只有符号标记
|
||||
expect(Object.keys(handle)).toHaveLength(0)
|
||||
})
|
||||
|
||||
test('普通对象不是 HostHandle', () => {
|
||||
expect(isHostHandle({} as unknown)).toBe(false)
|
||||
expect(isHostHandle(null)).toBe(false)
|
||||
})
|
||||
|
||||
test('端口对象满足最小形状', () => {
|
||||
// 编译期形状校验:以下赋值通过即说明端口契约自洽
|
||||
const noop = (): void => {}
|
||||
const ports = {
|
||||
agentRunner: { runAgentToResult: noop },
|
||||
progressEmitter: { emit: noop },
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'run-1',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete: noop,
|
||||
fail: noop,
|
||||
kill: noop,
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: {
|
||||
read: async () => [],
|
||||
append: async () => {},
|
||||
truncate: async () => {},
|
||||
},
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: noop, event: noop },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: '/tmp',
|
||||
budgetTotal: null,
|
||||
toolUseId: 'tu-1',
|
||||
}),
|
||||
}
|
||||
expect(ports.taskRegistrar.register().runId).toBe('run-1')
|
||||
expect(ports.hostFactory().toolUseId).toBe('tu-1')
|
||||
})
|
||||
|
||||
test('unwrapHostHandle 取回原始 bundle(同引用)', () => {
|
||||
const bundle = { secret: 'ctx', nested: { a: 1 } }
|
||||
const handle = createHostHandle(bundle)
|
||||
expect(unwrapHostHandle(handle)).toBe(bundle)
|
||||
})
|
||||
|
||||
test('createHostHandle(null) 不透明且解包为 null', () => {
|
||||
const handle = createHostHandle(null)
|
||||
expect(isHostHandle(handle)).toBe(true)
|
||||
expect(unwrapHostHandle(handle)).toBeNull()
|
||||
})
|
||||
423
packages/workflow-engine/src/__tests__/runWorkflow.test.ts
Normal file
423
packages/workflow-engine/src/__tests__/runWorkflow.test.ts
Normal file
@@ -0,0 +1,423 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { runWorkflow } from '../engine/runWorkflow.js'
|
||||
import { agentCallKey, createFileJournalStore } from '../engine/journal.js'
|
||||
import { createHostHandle, type WorkflowPorts } from '../ports.js'
|
||||
import type { AgentRunParams, AgentRunResult, ProgressEvent } from '../types.js'
|
||||
|
||||
function portsWith(
|
||||
runsDir: string,
|
||||
results: Map<string, AgentRunResult>,
|
||||
): WorkflowPorts {
|
||||
return {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) =>
|
||||
results.get(p.prompt) ?? { kind: 'dead' },
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: runsDir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
function portsWithEvents(
|
||||
runsDir: string,
|
||||
results: Map<string, AgentRunResult>,
|
||||
): { ports: WorkflowPorts; events: ProgressEvent[] } {
|
||||
const events: ProgressEvent[] = []
|
||||
return {
|
||||
events,
|
||||
ports: {
|
||||
agentRunner: {
|
||||
runAgentToResult: async (p: AgentRunParams) =>
|
||||
results.get(p.prompt) ?? { kind: 'dead' },
|
||||
},
|
||||
progressEmitter: { emit: e => void events.push(e) },
|
||||
taskRegistrar: {
|
||||
register: () => ({
|
||||
runId: 'r',
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(runsDir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: runsDir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test('端到端:脚本返回 agent 结果,状态 completed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(
|
||||
dir,
|
||||
new Map([
|
||||
['compute', { kind: 'ok', output: '42', usage: { outputTokens: 3 } }],
|
||||
]),
|
||||
)
|
||||
const result = await runWorkflow({
|
||||
script: `export const meta = { name: 't', description: 'd' }\nreturn agent('compute')`,
|
||||
runId: 'run-1',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('completed')
|
||||
expect(result.returnValue).toBe('42')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('脚本语法错误 → failed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(dir, new Map())
|
||||
const result = await runWorkflow({
|
||||
script: `export const meta = { name: 't', description: 'd' }\nreturn ((`,
|
||||
runId: 'run-2',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('failed')
|
||||
expect(result.error).toBeTruthy()
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('resume:journal 命中则不调用 runner', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
let called = 0
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async () => {
|
||||
called++
|
||||
return { kind: 'ok', output: 'live', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(dir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const key = agentCallKey('compute', { prompt: 'compute' })
|
||||
await ports.journalStore.append('run-3', {
|
||||
key,
|
||||
seq: 0,
|
||||
result: { kind: 'ok', output: 'cached', usage: { outputTokens: 1 } },
|
||||
})
|
||||
|
||||
const result = await runWorkflow({
|
||||
script: `return agent('compute')`,
|
||||
runId: 'run-3',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
resume: true,
|
||||
})
|
||||
expect(result.status).toBe('completed')
|
||||
expect(result.returnValue).toBe('cached')
|
||||
expect(called).toBe(0)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('abort → killed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(
|
||||
dir,
|
||||
new Map([['x', { kind: 'ok', output: '1', usage: { outputTokens: 1 } }]]),
|
||||
)
|
||||
const ac = new AbortController()
|
||||
ac.abort()
|
||||
const result = await runWorkflow({
|
||||
script: `return agent('x')`,
|
||||
runId: 'run-4',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: ac.signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('killed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('workflow() 嵌套(一层)共享计数', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
await mkdir(join(dir, '.claude', 'workflows'), { recursive: true })
|
||||
await writeFile(
|
||||
join(dir, '.claude', 'workflows', 'child.ts'),
|
||||
`return agent('child')\n// child workflow`,
|
||||
)
|
||||
const ports = portsWith(
|
||||
dir,
|
||||
new Map([
|
||||
[
|
||||
'child',
|
||||
{ kind: 'ok', output: 'child-out', usage: { outputTokens: 1 } },
|
||||
],
|
||||
]),
|
||||
)
|
||||
const result = await runWorkflow({
|
||||
script: `return workflow('child')`,
|
||||
runId: 'run-5',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('completed')
|
||||
expect(result.returnValue).toBe('child-out')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
// ---- 边界与事件 ----
|
||||
|
||||
test('scriptChanged=true → truncate journal 并全量现场跑', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
let called = 0
|
||||
const ports: WorkflowPorts = {
|
||||
agentRunner: {
|
||||
runAgentToResult: async () => {
|
||||
called++
|
||||
return { kind: 'ok', output: 'live', usage: { outputTokens: 1 } }
|
||||
},
|
||||
},
|
||||
progressEmitter: { emit: () => {} },
|
||||
taskRegistrar: {
|
||||
register: () => ({ runId: 'r', signal: new AbortController().signal }),
|
||||
complete: () => {},
|
||||
fail: () => {},
|
||||
kill: () => {},
|
||||
pendingAction: () => null,
|
||||
},
|
||||
journalStore: createFileJournalStore(dir),
|
||||
permissionGate: { isAborted: () => false },
|
||||
logger: { debug: () => {}, event: () => {} },
|
||||
hostFactory: () => ({
|
||||
handle: createHostHandle(null),
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
}),
|
||||
}
|
||||
const key = agentCallKey('compute', { prompt: 'compute' })
|
||||
await ports.journalStore.append('run-chg', {
|
||||
key,
|
||||
seq: 0,
|
||||
result: { kind: 'ok', output: 'cached', usage: { outputTokens: 1 } },
|
||||
})
|
||||
const result = await runWorkflow({
|
||||
script: `return agent('compute')`,
|
||||
runId: 'run-chg',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
resume: true,
|
||||
scriptChanged: true,
|
||||
})
|
||||
expect(result.status).toBe('completed')
|
||||
expect(result.returnValue).toBe('live')
|
||||
expect(called).toBe(1)
|
||||
// truncate 清空了旧 cached journal,现场 agent append 新 entry(live)
|
||||
const final = await ports.journalStore.read('run-chg')
|
||||
expect(final).toHaveLength(1)
|
||||
expect((final[0]!.result as { output: string }).output).toBe('live')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('脚本运行时抛错(非语法错)→ failed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(dir, new Map())
|
||||
const result = await runWorkflow({
|
||||
script: `throw new Error('boom at runtime')`,
|
||||
runId: 'run-throw',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('failed')
|
||||
expect(result.error).toMatch(/boom/)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('发射 run_started(含 workflowName)与 run_done 事件', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const { ports, events } = portsWithEvents(
|
||||
dir,
|
||||
new Map([['x', { kind: 'ok', output: '1', usage: { outputTokens: 1 } }]]),
|
||||
)
|
||||
await runWorkflow({
|
||||
script: `return agent('x')`,
|
||||
runId: 'run-ev',
|
||||
workflowName: 'my-wf',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(
|
||||
events.some(e => e.type === 'run_started' && e.workflowName === 'my-wf'),
|
||||
).toBe(true)
|
||||
expect(
|
||||
events.some(e => e.type === 'run_done' && e.status === 'completed'),
|
||||
).toBe(true)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('未传 workflowName 时从 meta.name 推导', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const { ports, events } = portsWithEvents(dir, new Map())
|
||||
await runWorkflow({
|
||||
script: `export const meta = { name: 'from-meta', description: 'd' }\nreturn 1`,
|
||||
runId: 'run-meta',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(
|
||||
events.some(
|
||||
e => e.type === 'run_started' && e.workflowName === 'from-meta',
|
||||
),
|
||||
).toBe(true)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('budgetTotal 耗尽 → failed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(
|
||||
dir,
|
||||
new Map([
|
||||
['a', { kind: 'ok', output: '1', usage: { outputTokens: 5 } }],
|
||||
['b', { kind: 'ok', output: '2', usage: { outputTokens: 5 } }],
|
||||
]),
|
||||
)
|
||||
const result = await runWorkflow({
|
||||
script: `await agent('a')\nreturn agent('b')`,
|
||||
runId: 'run-budget',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: 5,
|
||||
})
|
||||
expect(result.status).toBe('failed')
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('workflow() 引用语法错的子脚本 → failed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
await mkdir(join(dir, '.claude', 'workflows'), { recursive: true })
|
||||
await writeFile(join(dir, '.claude', 'workflows', 'broken.ts'), `return ((`)
|
||||
const ports = portsWith(dir, new Map())
|
||||
const result = await runWorkflow({
|
||||
script: `return workflow('broken')`,
|
||||
runId: 'run-sub-err',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('failed')
|
||||
expect(result.error).toMatch(/子 workflow|脚本错误/)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test('workflow() 引用不存在的 name → failed', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'wf-run-'))
|
||||
try {
|
||||
const ports = portsWith(dir, new Map())
|
||||
const result = await runWorkflow({
|
||||
script: `return workflow('ghost')`,
|
||||
runId: 'run-sub-missing',
|
||||
ports,
|
||||
host: createHostHandle(null),
|
||||
signal: new AbortController().signal,
|
||||
cwd: dir,
|
||||
budgetTotal: null,
|
||||
})
|
||||
expect(result.status).toBe('failed')
|
||||
expect(result.error).toMatch(/子 workflow|未找到/)
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
44
packages/workflow-engine/src/__tests__/schema.test.ts
Normal file
44
packages/workflow-engine/src/__tests__/schema.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { workflowInputSchema } from '../tool/schema.js'
|
||||
|
||||
test('空对象通过(所有字段 optional)', () => {
|
||||
expect(workflowInputSchema.safeParse({}).success).toBe(true)
|
||||
})
|
||||
|
||||
test('全部已知字段可填', () => {
|
||||
const r = workflowInputSchema.safeParse({
|
||||
script: 'return 1',
|
||||
name: 'release',
|
||||
scriptPath: '/abs/x.ts',
|
||||
args: { n: 1 },
|
||||
resumeFromRunId: 'run-1',
|
||||
description: 'do thing',
|
||||
title: 'T',
|
||||
})
|
||||
expect(r.success).toBe(true)
|
||||
})
|
||||
|
||||
test('args 接受任意 JSON 值(对象/数组/字符串/数字/布尔/null)', () => {
|
||||
for (const args of [{ a: 1 }, [1, 2], 's', 42, true, null]) {
|
||||
expect(workflowInputSchema.safeParse({ args }).success).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
test('类型错误被拒(script/name/scriptPath 非字符串)', () => {
|
||||
expect(workflowInputSchema.safeParse({ script: 123 }).success).toBe(false)
|
||||
expect(workflowInputSchema.safeParse({ name: 42 }).success).toBe(false)
|
||||
expect(workflowInputSchema.safeParse({ scriptPath: {} }).success).toBe(false)
|
||||
})
|
||||
|
||||
test('resumeFromRunId/description/title 必须为字符串', () => {
|
||||
expect(workflowInputSchema.safeParse({ resumeFromRunId: 1 }).success).toBe(
|
||||
false,
|
||||
)
|
||||
expect(workflowInputSchema.safeParse({ description: 1 }).success).toBe(false)
|
||||
expect(workflowInputSchema.safeParse({ title: 1 }).success).toBe(false)
|
||||
})
|
||||
|
||||
test('未知字段被 strip(zod 默认非 strict,safeParse 成功)', () => {
|
||||
const r = workflowInputSchema.safeParse({ script: 'x', extra: 1 })
|
||||
expect(r.success).toBe(true)
|
||||
})
|
||||
168
packages/workflow-engine/src/__tests__/script.test.ts
Normal file
168
packages/workflow-engine/src/__tests__/script.test.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import {
|
||||
ScriptError,
|
||||
extractMeta,
|
||||
parseScript,
|
||||
type WorkflowHooks,
|
||||
} from '../engine/script.js'
|
||||
|
||||
const stubHooks: WorkflowHooks = {
|
||||
agent: async () => 'agent-result',
|
||||
parallel: async thunks =>
|
||||
Promise.all(
|
||||
thunks.map(async t => {
|
||||
try {
|
||||
return await t()
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
),
|
||||
pipeline: async () => [],
|
||||
phase: () => {},
|
||||
log: () => {},
|
||||
workflow: async () => null,
|
||||
}
|
||||
|
||||
test('extractMeta 提取纯字面量并剥离语句', () => {
|
||||
const src = `export const meta = { name: 'x', description: 'y' }\nreturn 1`
|
||||
const { meta, body } = extractMeta(src)
|
||||
expect(meta?.name).toBe('x')
|
||||
expect(meta?.description).toBe('y')
|
||||
expect(body).not.toContain('export const meta')
|
||||
expect(body).toContain('return 1')
|
||||
})
|
||||
|
||||
test('extractMeta 无 meta 返回 null 且 body 不变', () => {
|
||||
const src = `return 42`
|
||||
const { meta, body } = extractMeta(src)
|
||||
expect(meta).toBeNull()
|
||||
expect(body).toBe(src)
|
||||
})
|
||||
|
||||
test('extractMeta 拒绝非纯字面量(引用变量)', () => {
|
||||
const src = `const x = 1\nexport const meta = { name: 'x', description: y }\nreturn 1`
|
||||
expect(() => extractMeta(src)).toThrow(ScriptError)
|
||||
})
|
||||
|
||||
test('parseScript 执行 body 顶层 return', async () => {
|
||||
const { execute } = parseScript(`return args.n + 1`)
|
||||
const out = await execute(stubHooks, { n: 41 }, { total: null })
|
||||
expect(out).toBe(42)
|
||||
})
|
||||
|
||||
test('脚本中 Date.now() 抛非确定性错误', async () => {
|
||||
const { execute } = parseScript(`return Date.now()`)
|
||||
await expect(execute(stubHooks, {}, { total: null })).rejects.toThrow(
|
||||
/Date\.now/,
|
||||
)
|
||||
})
|
||||
|
||||
test('脚本中 Math.random() 抛非确定性错误', async () => {
|
||||
const { execute } = parseScript(`return Math.random()`)
|
||||
await expect(execute(stubHooks, {}, { total: null })).rejects.toThrow(
|
||||
/Math\.random/,
|
||||
)
|
||||
})
|
||||
|
||||
test('无参 new Date() 抛,有参 new Date() 可用', async () => {
|
||||
const bad = parseScript(`return new Date()`)
|
||||
await expect(bad.execute(stubHooks, {}, { total: null })).rejects.toThrow(
|
||||
/new Date/,
|
||||
)
|
||||
const good = parseScript(
|
||||
`return new Date('2020-06-12T00:00:00Z').getUTCFullYear()`,
|
||||
)
|
||||
await expect(good.execute(stubHooks, {}, { total: null })).resolves.toBe(2020)
|
||||
})
|
||||
|
||||
// ---- meta 校验错误分支与嵌套 ----
|
||||
|
||||
test('extractMeta meta 为数组 → ScriptError', () => {
|
||||
expect(() => extractMeta('export const meta = [1, 2]\nreturn 1')).toThrow(
|
||||
ScriptError,
|
||||
)
|
||||
})
|
||||
|
||||
test('extractMeta meta 缺 name → ScriptError', () => {
|
||||
expect(() =>
|
||||
extractMeta('export const meta = { description: "d" }\nreturn 1'),
|
||||
).toThrow(ScriptError)
|
||||
})
|
||||
|
||||
test('extractMeta meta 缺 description → ScriptError', () => {
|
||||
expect(() =>
|
||||
extractMeta('export const meta = { name: "n" }\nreturn 1'),
|
||||
).toThrow(ScriptError)
|
||||
})
|
||||
|
||||
test('extractMeta meta 大括号未闭合 → ScriptError', () => {
|
||||
expect(() =>
|
||||
extractMeta('export const meta = { name: "n", description: "d"\nreturn 1'),
|
||||
).toThrow(ScriptError)
|
||||
})
|
||||
|
||||
test('extractMeta 支持嵌套对象(phases 数组)', () => {
|
||||
const src = `export const meta = { name: 'x', description: 'y', phases: [{ title: 'A' }, { title: 'B' }] }\nreturn 1`
|
||||
const { meta } = extractMeta(src)
|
||||
expect(meta?.name).toBe('x')
|
||||
expect(meta?.phases).toHaveLength(2)
|
||||
expect(meta?.phases?.[0]?.title).toBe('A')
|
||||
expect(meta?.phases?.[1]?.title).toBe('B')
|
||||
})
|
||||
|
||||
test('parseScript 语法错 → ScriptError', () => {
|
||||
expect(() => parseScript('return ((')).toThrow(ScriptError)
|
||||
})
|
||||
|
||||
test('parseScript 检测 import → 带指引的 ScriptError(不落泛化语法错)', () => {
|
||||
expect(() =>
|
||||
parseScript(
|
||||
`import { foo } from 'bar'\nexport const meta = { name: 'n', description: 'd' }\nreturn foo()`,
|
||||
),
|
||||
).toThrow(ScriptError)
|
||||
expect(() =>
|
||||
parseScript(
|
||||
`import { foo } from 'bar'\nexport const meta = { name: 'n', description: 'd' }\nreturn foo()`,
|
||||
),
|
||||
).toThrow(/不支持 import/)
|
||||
})
|
||||
|
||||
test('parseScript 检测 meta 之外的多余 export → 带指引的 ScriptError', () => {
|
||||
expect(() =>
|
||||
parseScript(
|
||||
`export const meta = { name: 'n', description: 'd' }\nexport const X = 1\nreturn X`,
|
||||
),
|
||||
).toThrow(ScriptError)
|
||||
expect(() =>
|
||||
parseScript(
|
||||
`export const meta = { name: 'n', description: 'd' }\nexport const X = 1\nreturn X`,
|
||||
),
|
||||
).toThrow(/只允许一处 export const meta/)
|
||||
})
|
||||
|
||||
test('parseScript 正常纯 JS 脚本(无 import/无多余 export)不被误拦', () => {
|
||||
const { execute } = parseScript(
|
||||
`export const meta = { name: 'n', description: 'd' }\nconst r = await agent('hi')\nreturn r`,
|
||||
)
|
||||
expect(typeof execute).toBe('function')
|
||||
})
|
||||
|
||||
test('parseScript 检测动态 import(...) → 带指引的 ScriptError(沙箱防逃逸)', () => {
|
||||
expect(() =>
|
||||
parseScript(
|
||||
`const cp = await import('node:child_process')\nreturn cp.execSync('id').toString()`,
|
||||
),
|
||||
).toThrow(ScriptError)
|
||||
expect(() =>
|
||||
parseScript(`const cp = await import('node:child_process')\nreturn cp`),
|
||||
).toThrow(/import/)
|
||||
})
|
||||
|
||||
test('parseScript 检测行中含 import 字符串字面量时不误拦(如 prompt 里出现 "import")', () => {
|
||||
// 字符串里的 import 不应被静态 regex 拦——允许 prompt 包含 "import" 词
|
||||
const { execute } = parseScript(
|
||||
`export const meta = { name: 'n', description: 'd' }\nconst r = await agent('please import this module')\nreturn r`,
|
||||
)
|
||||
expect(typeof execute).toBe('function')
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
import { validateAgainstSchema } from '../engine/structuredOutput.js'
|
||||
|
||||
const schema = {
|
||||
type: 'object',
|
||||
required: ['name', 'count'],
|
||||
properties: {
|
||||
name: { type: 'string' },
|
||||
count: { type: 'number' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
}
|
||||
|
||||
test('合法对象通过', () => {
|
||||
const { valid, errors } = validateAgainstSchema(
|
||||
{ name: 'a', count: 1 },
|
||||
schema,
|
||||
)
|
||||
expect(valid).toBe(true)
|
||||
expect(errors).toEqual([])
|
||||
})
|
||||
|
||||
test('缺字段失败', () => {
|
||||
const { valid, errors } = validateAgainstSchema({ name: 'a' }, schema)
|
||||
expect(valid).toBe(false)
|
||||
expect(errors.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
test('类型错误失败', () => {
|
||||
const { valid } = validateAgainstSchema({ name: 'a', count: 'x' }, schema)
|
||||
expect(valid).toBe(false)
|
||||
})
|
||||
|
||||
test('同一 schema 复用缓存', () => {
|
||||
validateAgainstSchema({ name: 'a', count: 1 }, schema)
|
||||
// 第二次用同一 schema 对象应命中缓存(不抛错即可)
|
||||
expect(validateAgainstSchema({ name: 'b', count: 2 }, schema).valid).toBe(
|
||||
true,
|
||||
)
|
||||
})
|
||||
30
packages/workflow-engine/src/__tests__/types.test.ts
Normal file
30
packages/workflow-engine/src/__tests__/types.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { expect, test } from 'bun:test'
|
||||
|
||||
// 直接构造类型形状,验证 JSON 往返(resume 持久化的核心要求)。
|
||||
test('AgentRunResult ok 分支可 JSON 往返', () => {
|
||||
const result = {
|
||||
kind: 'ok' as const,
|
||||
output: { confirmed: true },
|
||||
usage: { outputTokens: 42 },
|
||||
}
|
||||
const round = JSON.parse(JSON.stringify(result))
|
||||
expect(round).toEqual(result)
|
||||
expect(round.kind).toBe('ok')
|
||||
})
|
||||
|
||||
test('AgentRunResult skipped/dead 分支可 JSON 往返', () => {
|
||||
for (const kind of ['skipped', 'dead'] as const) {
|
||||
const round = JSON.parse(JSON.stringify({ kind }))
|
||||
expect(round.kind).toBe(kind)
|
||||
}
|
||||
})
|
||||
|
||||
test('JournalEntry 形状稳定', () => {
|
||||
const entry = {
|
||||
key: 'abc123',
|
||||
result: { kind: 'ok', output: 'text', usage: { outputTokens: 1 } },
|
||||
}
|
||||
const round = JSON.parse(JSON.stringify(entry))
|
||||
expect(round.key).toBe('abc123')
|
||||
expect(round.result.kind).toBe('ok')
|
||||
})
|
||||
138
packages/workflow-engine/src/agentAdapter.ts
Normal file
138
packages/workflow-engine/src/agentAdapter.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
// Agent 后端适配器抽象。引擎通过 registry 取 adapter 再调 run,不关心具体实现
|
||||
// (Anthropic SDK / 核心 runAgent / OpenAI / 本地模型 / mock 均为 adapter 的实现)。
|
||||
import type { AgentRunParams, AgentRunResult } from './types.js'
|
||||
import type { HostHandle } from './ports.js'
|
||||
|
||||
/** adapter 能力声明。引擎/脚本据此降级(如后端不支持 schema 则改文本 + 解析)。 */
|
||||
export type AgentAdapterCapabilities = {
|
||||
/** 支持 schema 结构化输出(agent(schema) 直接返回对象)。 */
|
||||
structuredOutput: boolean
|
||||
/** 支持工具调用(仅核心 agent 后端有)。 */
|
||||
tools?: boolean
|
||||
/** 支持流式(v1 引擎不消费,预留)。 */
|
||||
stream?: boolean
|
||||
}
|
||||
|
||||
/** adapter.run 的上下文。 */
|
||||
export type AgentAdapterContext = {
|
||||
/** 透传的不透明 host 句柄(核心 adapter 用;独立后端忽略)。 */
|
||||
host: HostHandle
|
||||
/** 取消信号(与 workflow signal 一致)。 */
|
||||
signal: AbortSignal
|
||||
/** 当前 workflow runId(日志/追踪用)。 */
|
||||
runId: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Agent 后端适配器。引擎只依赖此接口;具体后端实现它并注册到 registry。
|
||||
* initialize/dispose 为可选生命周期(连接池/资源管理),由调用方通过
|
||||
* registry.initializeAll/disposeAll 触发。
|
||||
*/
|
||||
export interface AgentAdapter {
|
||||
/** 唯一标识(registry 路由 / 日志)。 */
|
||||
readonly id: string
|
||||
/** 能力声明。 */
|
||||
readonly capabilities: AgentAdapterCapabilities
|
||||
/** 执行一次 agent 调用。 */
|
||||
run(params: AgentRunParams, ctx: AgentAdapterContext): Promise<AgentRunResult>
|
||||
/** 初始化(由 registry.initializeAll 触发)。 */
|
||||
initialize?(): Promise<void>
|
||||
/** 销毁(由 registry.disposeAll 触发)。 */
|
||||
dispose?(): Promise<void>
|
||||
}
|
||||
|
||||
/** 路由规则:决定哪些 params 走哪个 adapter。按添加顺序匹配,先命中先用。 */
|
||||
export type AdapterRouteRule =
|
||||
| { kind: 'agentType'; agentType: string; adapter: string }
|
||||
| { kind: 'model'; pattern: string; adapter: string }
|
||||
| {
|
||||
kind: 'custom'
|
||||
match: (params: AgentRunParams) => boolean
|
||||
adapter: string
|
||||
}
|
||||
|
||||
/** registry 找不到匹配 adapter 时抛出。 */
|
||||
export class AdapterNotFoundError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = 'AdapterNotFoundError'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 多后端 registry。register 注册 adapter,route/default 配路由,resolve 按
|
||||
* 规则顺序匹配选 adapter。adapter 的 lifecycle(initialize/dispose)通过
|
||||
* initializeAll/disposeAll 统一触发(由调用方在运行前后调)。
|
||||
*/
|
||||
export class AgentAdapterRegistry {
|
||||
private readonly adapters = new Map<string, AgentAdapter>()
|
||||
private readonly rules: AdapterRouteRule[] = []
|
||||
private defaultId: string | null = null
|
||||
|
||||
/** 注册一个 adapter(id 重复则覆盖)。链式。 */
|
||||
register(adapter: AgentAdapter): this {
|
||||
this.adapters.set(adapter.id, adapter)
|
||||
return this
|
||||
}
|
||||
|
||||
/** 设默认 adapter(无规则命中时用)。链式。 */
|
||||
default(adapterId: string): this {
|
||||
this.defaultId = adapterId
|
||||
return this
|
||||
}
|
||||
|
||||
/** 加一条路由规则(按添加顺序匹配)。链式。 */
|
||||
route(rule: AdapterRouteRule): this {
|
||||
this.rules.push(rule)
|
||||
return this
|
||||
}
|
||||
|
||||
has(id: string): boolean {
|
||||
return this.adapters.has(id)
|
||||
}
|
||||
|
||||
get(id: string): AgentAdapter | undefined {
|
||||
return this.adapters.get(id)
|
||||
}
|
||||
|
||||
/** 按规则匹配;第一个命中返回;无命中走 default;都没有抛 AdapterNotFoundError。 */
|
||||
resolve(params: AgentRunParams): AgentAdapter {
|
||||
for (const rule of this.rules) {
|
||||
if (matchRule(rule, params)) {
|
||||
const hit = this.adapters.get(rule.adapter)
|
||||
if (hit) return hit
|
||||
}
|
||||
}
|
||||
if (this.defaultId) {
|
||||
const fallback = this.adapters.get(this.defaultId)
|
||||
if (fallback) return fallback
|
||||
}
|
||||
throw new AdapterNotFoundError(
|
||||
`无 adapter 匹配(rules=${this.rules.length}, default=${this.defaultId ?? '无'})`,
|
||||
)
|
||||
}
|
||||
|
||||
/** 触发所有 adapter 的 initialize(跳过未实现的)。 */
|
||||
async initializeAll(): Promise<void> {
|
||||
for (const a of this.adapters.values()) {
|
||||
await a.initialize?.()
|
||||
}
|
||||
}
|
||||
|
||||
/** 触发所有 adapter 的 dispose(跳过未实现的)。 */
|
||||
async disposeAll(): Promise<void> {
|
||||
for (const a of this.adapters.values()) {
|
||||
await a.dispose?.()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function matchRule(rule: AdapterRouteRule, params: AgentRunParams): boolean {
|
||||
if (rule.kind === 'agentType') return params.agentType === rule.agentType
|
||||
if (rule.kind === 'model') {
|
||||
return (
|
||||
typeof params.model === 'string' && params.model.startsWith(rule.pattern)
|
||||
)
|
||||
}
|
||||
return rule.match(params) // custom
|
||||
}
|
||||
26
packages/workflow-engine/src/constants.ts
Normal file
26
packages/workflow-engine/src/constants.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// 引擎级常量。无运行时依赖。
|
||||
|
||||
/**
|
||||
* Workflow 工具名。PascalCase 与系统其他工具(Agent/Bash/CronCreate…)一致,
|
||||
* 否则大小写敏感的 toolMatchesName 会让模型自然的 select:Workflow 匹配失败。
|
||||
*/
|
||||
export const WORKFLOW_TOOL_NAME = 'Workflow'
|
||||
|
||||
/** 用户命名 workflow 文件目录(相对项目根)。 */
|
||||
export const WORKFLOW_DIR_NAME = '.claude/workflows'
|
||||
|
||||
/** workflow run 持久化目录(journal + run 记录)。 */
|
||||
export const WORKFLOW_RUNS_DIR = '.claude/workflow-runs'
|
||||
|
||||
/** 命名 workflow 支持的脚本扩展名(按优先级)。 */
|
||||
export const WORKFLOW_SCRIPT_EXTENSIONS = ['.ts', '.js', '.mjs'] as const
|
||||
|
||||
/** 并发:信号量许可 = min(MAX_CONCURRENCY_CAP, cpuCores - MAX_CONCURRENCY_OFFSET)。 */
|
||||
export const MAX_CONCURRENCY_OFFSET = 2
|
||||
export const MAX_CONCURRENCY_CAP = 16
|
||||
|
||||
/** 单个 workflow 生命周期内 agent() 总数上限。 */
|
||||
export const MAX_TOTAL_AGENTS = 1000
|
||||
|
||||
/** 单次 parallel()/pipeline() 调用的 items 上限。 */
|
||||
export const MAX_ITEMS_PER_CALL = 4096
|
||||
36
packages/workflow-engine/src/engine/budget.ts
Normal file
36
packages/workflow-engine/src/engine/budget.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export class BudgetExhaustedError extends Error {
|
||||
constructor() {
|
||||
super('workflow token budget 已耗尽(budget.total 达到上限)')
|
||||
this.name = 'BudgetExhaustedError'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Token 预算累加器。脚本通过 `budget.total / budget.spent() / budget.remaining()`
|
||||
* 读取;agent() 调用前 assertCanSpend() 强制硬上限。
|
||||
*/
|
||||
export class Budget {
|
||||
private spentTokens = 0
|
||||
|
||||
constructor(readonly total: number | null) {}
|
||||
|
||||
spent(): number {
|
||||
return this.spentTokens
|
||||
}
|
||||
|
||||
remaining(): number {
|
||||
return this.total == null
|
||||
? Infinity
|
||||
: Math.max(0, this.total - this.spentTokens)
|
||||
}
|
||||
|
||||
addOutputTokens(n: number): void {
|
||||
if (n > 0) this.spentTokens += n
|
||||
}
|
||||
|
||||
assertCanSpend(): void {
|
||||
if (this.total != null && this.spentTokens >= this.total) {
|
||||
throw new BudgetExhaustedError()
|
||||
}
|
||||
}
|
||||
}
|
||||
77
packages/workflow-engine/src/engine/concurrency.ts
Normal file
77
packages/workflow-engine/src/engine/concurrency.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import * as os from 'node:os'
|
||||
import { MAX_CONCURRENCY_CAP, MAX_CONCURRENCY_OFFSET } from '../constants.js'
|
||||
|
||||
/**
|
||||
* 异步信号量。acquire() 返回一个 release 函数;permit 在 release 时直接
|
||||
* 转移给下一个等待者(available 不变),无等待者时才归还。permit 总数守恒。
|
||||
*
|
||||
* acquire(signal?) 支持取消:signal 已 aborted 或在等待期间 abort 时立即 reject,
|
||||
* waiter 从队列移除、不消耗 permit(避免被取消的 agent 占用并发槽)。
|
||||
*/
|
||||
export class Semaphore {
|
||||
private available: number
|
||||
private readonly waiters: Array<{
|
||||
wake: () => void
|
||||
cleanup: () => void
|
||||
}> = []
|
||||
|
||||
constructor(permits: number) {
|
||||
this.available = Math.max(1, Math.floor(permits))
|
||||
}
|
||||
|
||||
async acquire(signal?: AbortSignal): Promise<() => void> {
|
||||
if (signal?.aborted) {
|
||||
throw new Error('Semaphore.acquire aborted (signal already aborted)')
|
||||
}
|
||||
if (this.available > 0) {
|
||||
this.available -= 1
|
||||
return () => this.release()
|
||||
}
|
||||
return new Promise<() => void>((resolve, reject) => {
|
||||
const onAbort = () => {
|
||||
const idx = this.waiters.indexOf(entry)
|
||||
if (idx >= 0) this.waiters.splice(idx, 1)
|
||||
reject(new Error('Semaphore.acquire aborted'))
|
||||
}
|
||||
const wake = () => {
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
resolve(() => this.release())
|
||||
}
|
||||
const entry = {
|
||||
wake,
|
||||
cleanup: () => signal?.removeEventListener('abort', onAbort),
|
||||
}
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
this.waiters.push(entry)
|
||||
})
|
||||
}
|
||||
|
||||
private release(): void {
|
||||
const next = this.waiters.shift()
|
||||
if (next) {
|
||||
next.wake() // 直接转移 permit
|
||||
} else {
|
||||
this.available += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cpuCores(): number {
|
||||
const a = (os as { availableParallelism?: () => number }).availableParallelism
|
||||
if (typeof a === 'function') {
|
||||
try {
|
||||
return a()
|
||||
} catch {
|
||||
// fallthrough
|
||||
}
|
||||
}
|
||||
return os.cpus()?.length ?? 4
|
||||
}
|
||||
|
||||
/** min(MAX_CONCURRENCY_CAP, cpuCores - MAX_CONCURRENCY_OFFSET),至少 1。 */
|
||||
export function maxConcurrency(): number {
|
||||
return Math.max(
|
||||
1,
|
||||
Math.min(MAX_CONCURRENCY_CAP, cpuCores() - MAX_CONCURRENCY_OFFSET),
|
||||
)
|
||||
}
|
||||
70
packages/workflow-engine/src/engine/context.ts
Normal file
70
packages/workflow-engine/src/engine/context.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import type { HostHandle, WorkflowPorts } from '../ports.js'
|
||||
import type { JournalEntry } from '../types.js'
|
||||
import { Budget } from './budget.js'
|
||||
import { Semaphore, maxConcurrency } from './concurrency.js'
|
||||
|
||||
/**
|
||||
* 可被子 workflow 共享的资源。嵌套时 semaphore/budget/agentCountBox 按引用共享,
|
||||
* depth 在执行子 workflow 时临时 +1。
|
||||
*/
|
||||
export type SharedResources = {
|
||||
semaphore: Semaphore
|
||||
budget: Budget
|
||||
agentCountBox: { value: number }
|
||||
/** agent() 调用的递增序号,盖戳 agent_started/agent_done 供进度精确关联。子 workflow 共享。 */
|
||||
agentIdSeq: { value: number }
|
||||
depth: number
|
||||
}
|
||||
|
||||
/** 单次 workflow 运行的执行上下文。 */
|
||||
export type EngineContext = {
|
||||
ports: WorkflowPorts
|
||||
host: HostHandle
|
||||
signal: AbortSignal
|
||||
runId: string
|
||||
workflowName: string
|
||||
cwd: string
|
||||
resources: SharedResources
|
||||
journal: JournalEntry[]
|
||||
journalIndex: number
|
||||
journalInvalidated: boolean
|
||||
currentPhase: string | null
|
||||
}
|
||||
|
||||
export function createSharedResources(
|
||||
budgetTotal: number | null,
|
||||
): SharedResources {
|
||||
return {
|
||||
semaphore: new Semaphore(maxConcurrency()),
|
||||
budget: new Budget(budgetTotal),
|
||||
agentCountBox: { value: 0 },
|
||||
agentIdSeq: { value: 0 },
|
||||
depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
export function createEngineContext(opts: {
|
||||
ports: WorkflowPorts
|
||||
host: HostHandle
|
||||
signal: AbortSignal
|
||||
runId: string
|
||||
workflowName: string
|
||||
cwd: string
|
||||
budgetTotal: number | null
|
||||
journal?: JournalEntry[]
|
||||
}): EngineContext {
|
||||
const resources = createSharedResources(opts.budgetTotal)
|
||||
return {
|
||||
ports: opts.ports,
|
||||
host: opts.host,
|
||||
signal: opts.signal,
|
||||
runId: opts.runId,
|
||||
workflowName: opts.workflowName,
|
||||
cwd: opts.cwd,
|
||||
resources,
|
||||
journal: opts.journal ? [...opts.journal] : [],
|
||||
journalIndex: 0,
|
||||
journalInvalidated: false,
|
||||
currentPhase: null,
|
||||
}
|
||||
}
|
||||
15
packages/workflow-engine/src/engine/errors.ts
Normal file
15
packages/workflow-engine/src/engine/errors.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/** 引擎级可预期错误(脚本错、上限、嵌套)。 */
|
||||
export class WorkflowError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = 'WorkflowError'
|
||||
}
|
||||
}
|
||||
|
||||
/** workflow 被 abort(kill)。 */
|
||||
export class WorkflowAbortedError extends Error {
|
||||
constructor() {
|
||||
super('workflow 已被取消(abort)')
|
||||
this.name = 'WorkflowAbortedError'
|
||||
}
|
||||
}
|
||||
209
packages/workflow-engine/src/engine/hooks.ts
Normal file
209
packages/workflow-engine/src/engine/hooks.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
import { MAX_ITEMS_PER_CALL, MAX_TOTAL_AGENTS } from '../constants.js'
|
||||
import type {
|
||||
AgentRunParams,
|
||||
AgentRunResult,
|
||||
JournalEntry,
|
||||
ProgressEvent,
|
||||
} from '../types.js'
|
||||
import type { EngineContext } from './context.js'
|
||||
import { WorkflowAbortedError, WorkflowError } from './errors.js'
|
||||
import { agentCallKey } from './journal.js'
|
||||
import type { WorkflowHooks } from './script.js'
|
||||
|
||||
/** workflow() 钩子的子 workflow 执行器(由 runWorkflow 注入,避免循环依赖)。 */
|
||||
export type SubWorkflowRunner = (opts: {
|
||||
name?: string
|
||||
scriptPath?: string
|
||||
script?: string
|
||||
args?: unknown
|
||||
}) => Promise<unknown>
|
||||
|
||||
type HookProgressInit =
|
||||
| { type: 'phase_started'; phase: string }
|
||||
| { type: 'phase_done'; phase: string }
|
||||
| { type: 'agent_started'; agentId: number; label?: string; phase?: string }
|
||||
| {
|
||||
type: 'agent_done'
|
||||
agentId: number
|
||||
label?: string
|
||||
phase?: string
|
||||
result: AgentRunResult
|
||||
}
|
||||
| { type: 'log'; message: string }
|
||||
|
||||
export function makeHooks(
|
||||
ctx: EngineContext,
|
||||
runSubWorkflow: SubWorkflowRunner,
|
||||
): WorkflowHooks {
|
||||
// 所有进度事件自动注入 runId,供 adapter 路由到对应 task(多并发 workflow)
|
||||
const emit = (init: HookProgressInit): void => {
|
||||
ctx.ports.progressEmitter.emit({
|
||||
runId: ctx.runId,
|
||||
...init,
|
||||
} as ProgressEvent)
|
||||
}
|
||||
|
||||
const agent: WorkflowHooks['agent'] = async (prompt, opts = {}) => {
|
||||
const r = ctx.resources
|
||||
if (r.agentCountBox.value >= MAX_TOTAL_AGENTS) {
|
||||
throw new WorkflowError(
|
||||
`workflow 超过 agent 总数上限 (${MAX_TOTAL_AGENTS})`,
|
||||
)
|
||||
}
|
||||
|
||||
// 每次 agent() 调用分配唯一 id(含 journal 命中),盖戳 started/done 供 reducer 精确关联
|
||||
const agentId = r.agentIdSeq.value++
|
||||
|
||||
const params: AgentRunParams = { prompt, ...opts }
|
||||
const key = agentCallKey(prompt, params)
|
||||
const label = opts.label as string | undefined
|
||||
const phase =
|
||||
(opts.phase as string | undefined) ?? ctx.currentPhase ?? undefined
|
||||
|
||||
// journal 命中 → 直接返回缓存
|
||||
if (!ctx.journalInvalidated && ctx.journalIndex < ctx.journal.length) {
|
||||
const entry = ctx.journal[ctx.journalIndex]!
|
||||
if (entry.key === key) {
|
||||
ctx.journalIndex++
|
||||
emit({
|
||||
type: 'agent_done',
|
||||
agentId,
|
||||
label,
|
||||
phase,
|
||||
result: entry.result,
|
||||
})
|
||||
return resultToOutput(entry.result)
|
||||
}
|
||||
// 发散:丢弃后续 journal,后续全部现场跑
|
||||
ctx.journalInvalidated = true
|
||||
ctx.journal = ctx.journal.slice(0, ctx.journalIndex)
|
||||
await ctx.ports.journalStore.truncate(ctx.runId)
|
||||
}
|
||||
|
||||
let release: () => void
|
||||
try {
|
||||
release = await ctx.resources.semaphore.acquire(ctx.signal)
|
||||
} catch {
|
||||
// abort 期间在队列中等待:semaphore 已把 waiter 移除、未消耗 permit
|
||||
throw new WorkflowAbortedError()
|
||||
}
|
||||
try {
|
||||
if (ctx.signal.aborted) throw new WorkflowAbortedError()
|
||||
// 预算检查在 semaphore 临界区内:queued waiter 被唤醒后看到最新 spent,
|
||||
// 否则 N 个 waiter 入队时 spent=0 全过检,唤醒后无 re-check 全部超支。
|
||||
// journal 命中路径不扣预算,无需检查。
|
||||
r.budget.assertCanSpend()
|
||||
|
||||
const pending = ctx.ports.taskRegistrar.pendingAction(ctx.runId)
|
||||
if (pending?.kind === 'skip') {
|
||||
const result: AgentRunResult = { kind: 'skipped' }
|
||||
emit({ type: 'agent_done', agentId, label, phase, result })
|
||||
return null
|
||||
}
|
||||
|
||||
ctx.resources.agentCountBox.value++
|
||||
emit({ type: 'agent_started', agentId, label, phase })
|
||||
const registry = ctx.ports.agentAdapterRegistry
|
||||
const result = registry
|
||||
? await registry.resolve(params).run(params, {
|
||||
host: ctx.host,
|
||||
signal: ctx.signal,
|
||||
runId: ctx.runId,
|
||||
})
|
||||
: await ctx.ports.agentRunner.runAgentToResult(params, ctx.host)
|
||||
if (result.kind === 'ok') {
|
||||
ctx.resources.budget.addOutputTokens(result.usage.outputTokens)
|
||||
}
|
||||
emit({ type: 'agent_done', agentId, label, phase, result })
|
||||
|
||||
const entry: JournalEntry = { key, seq: agentId, result }
|
||||
// 关键:push 顺序 = 完成顺序(非调用顺序);read() 已按 seq 重排,
|
||||
// 因此 resume 时调用顺序与 journal 顺序对齐,key 索引稳定。
|
||||
ctx.journal.push(entry)
|
||||
ctx.journalIndex++
|
||||
await ctx.ports.journalStore.append(ctx.runId, entry)
|
||||
return resultToOutput(result)
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
}
|
||||
|
||||
const parallel: WorkflowHooks['parallel'] = async thunks => {
|
||||
if (thunks.length > MAX_ITEMS_PER_CALL) {
|
||||
throw new WorkflowError(
|
||||
`parallel 超过单次调用 items 上限 (${MAX_ITEMS_PER_CALL})`,
|
||||
)
|
||||
}
|
||||
return Promise.all(
|
||||
thunks.map(async (t, i) => {
|
||||
try {
|
||||
return await t()
|
||||
} catch (e) {
|
||||
// "null on error"契约不变,但应 log——否则 workflow 作者无法定位为何 agent 失败
|
||||
ctx.ports.logger.warn?.(
|
||||
`parallel thunk #${i} failed: ${(e as Error).message}`,
|
||||
)
|
||||
return null
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const pipeline: WorkflowHooks['pipeline'] = async <T, R>(
|
||||
items: readonly T[],
|
||||
...stages: Array<
|
||||
(prev: unknown, item: T, index: number) => Promise<unknown>
|
||||
>
|
||||
): Promise<Array<R | null>> => {
|
||||
if (items.length > MAX_ITEMS_PER_CALL) {
|
||||
throw new WorkflowError(
|
||||
`pipeline 超过单次调用 items 上限 (${MAX_ITEMS_PER_CALL})`,
|
||||
)
|
||||
}
|
||||
return Promise.all(
|
||||
items.map(async (item, index): Promise<R | null> => {
|
||||
try {
|
||||
let prev: unknown = item
|
||||
for (const stage of stages) {
|
||||
prev = await stage(prev, item, index)
|
||||
}
|
||||
return prev as R
|
||||
} catch (e) {
|
||||
ctx.ports.logger.warn?.(
|
||||
`pipeline item #${index} failed: ${(e as Error).message}`,
|
||||
)
|
||||
return null
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
const phase: WorkflowHooks['phase'] = title => {
|
||||
if (ctx.currentPhase) {
|
||||
emit({ type: 'phase_done', phase: ctx.currentPhase })
|
||||
}
|
||||
ctx.currentPhase = title
|
||||
emit({ type: 'phase_started', phase: title })
|
||||
}
|
||||
|
||||
const log: WorkflowHooks['log'] = message => {
|
||||
emit({ type: 'log', message })
|
||||
}
|
||||
|
||||
const workflow: WorkflowHooks['workflow'] = async (nameOrRef, args) => {
|
||||
if (ctx.resources.depth >= 1) {
|
||||
throw new WorkflowError('workflow() 嵌套仅允许一层')
|
||||
}
|
||||
const sub: Parameters<SubWorkflowRunner>[0] =
|
||||
typeof nameOrRef === 'string'
|
||||
? { name: nameOrRef }
|
||||
: { scriptPath: nameOrRef.scriptPath }
|
||||
return runSubWorkflow({ ...sub, args })
|
||||
}
|
||||
|
||||
return { agent, parallel, pipeline, phase, log, workflow }
|
||||
}
|
||||
|
||||
function resultToOutput(result: AgentRunResult): unknown {
|
||||
return result.kind === 'ok' ? result.output : null
|
||||
}
|
||||
50
packages/workflow-engine/src/engine/journal.ts
Normal file
50
packages/workflow-engine/src/engine/journal.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { appendFile, mkdir, readFile, rm } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import type { JournalStore } from '../ports.js'
|
||||
import type { AgentRunParams, JournalEntry } from '../types.js'
|
||||
|
||||
/** 去掉纯展示字段后的规范化参数字符串。 */
|
||||
function canonicalParams(params: AgentRunParams): string {
|
||||
const { label: _label, phase: _phase, ...rest } = params
|
||||
const keys = Object.keys(rest).sort()
|
||||
const sorted: Record<string, unknown> = {}
|
||||
for (const k of keys) sorted[k] = rest[k as keyof typeof rest]
|
||||
return JSON.stringify(sorted)
|
||||
}
|
||||
|
||||
/** agent() 调用的确定性 key(prompt + 规范化 params 的 sha256)。 */
|
||||
export function agentCallKey(prompt: string, params: AgentRunParams): string {
|
||||
return createHash('sha256')
|
||||
.update(prompt + '\n' + canonicalParams(params))
|
||||
.digest('hex')
|
||||
}
|
||||
|
||||
/** 文件式 JournalStore(jsonl,每个 run 一个目录)。纯 fs,无核心依赖。 */
|
||||
export function createFileJournalStore(runsDir: string): JournalStore {
|
||||
const pathOf = (runId: string) => join(runsDir, runId, 'journal.jsonl')
|
||||
|
||||
return {
|
||||
async read(runId): Promise<JournalEntry[]> {
|
||||
try {
|
||||
const raw = await readFile(pathOf(runId), 'utf-8')
|
||||
const entries = raw
|
||||
.split('\n')
|
||||
.filter(line => line.trim().length > 0)
|
||||
.map(line => JSON.parse(line) as JournalEntry)
|
||||
// parallel 完成顺序 ≠ 调用顺序;按 seq 重排,使 resume 期间 key 索引稳定。
|
||||
// 缺 seq 的旧 entry 视为 0(保持向前兼容,最坏情况下退化为文件顺序)。
|
||||
return entries.sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0))
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
},
|
||||
async append(runId, entry) {
|
||||
await mkdir(join(runsDir, runId), { recursive: true })
|
||||
await appendFile(pathOf(runId), JSON.stringify(entry) + '\n', 'utf-8')
|
||||
},
|
||||
async truncate(runId) {
|
||||
await rm(join(runsDir, runId), { recursive: true, force: true })
|
||||
},
|
||||
}
|
||||
}
|
||||
46
packages/workflow-engine/src/engine/namedWorkflows.ts
Normal file
46
packages/workflow-engine/src/engine/namedWorkflows.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { readFile, readdir } from 'node:fs/promises'
|
||||
import { join, parse, resolve } from 'node:path'
|
||||
import { WORKFLOW_SCRIPT_EXTENSIONS } from '../constants.js'
|
||||
import { containsPath } from './paths.js'
|
||||
|
||||
type Ext = (typeof WORKFLOW_SCRIPT_EXTENSIONS)[number]
|
||||
|
||||
function isScriptExt(ext: string): ext is Ext {
|
||||
return (WORKFLOW_SCRIPT_EXTENSIONS as readonly string[]).includes(
|
||||
ext.toLowerCase(),
|
||||
)
|
||||
}
|
||||
|
||||
/** 按 .ts → .js → .mjs 优先级解析命名 workflow 文件。 */
|
||||
export async function resolveNamedWorkflow(
|
||||
workflowDir: string,
|
||||
name: string,
|
||||
): Promise<{ path: string; content: string } | null> {
|
||||
for (const ext of WORKFLOW_SCRIPT_EXTENSIONS) {
|
||||
const p = resolve(workflowDir, name + ext)
|
||||
// 双保险:防止上层 sanitize 漏掉的边界 case 把路径遍历到 workflowDir 之外
|
||||
if (!containsPath(workflowDir, p)) return null
|
||||
try {
|
||||
return { path: p, content: await readFile(p, 'utf-8') }
|
||||
} catch {
|
||||
// 试下一个扩展名
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** 列出目录下所有命名 workflow(不含非脚本文件)。 */
|
||||
export async function listNamedWorkflows(
|
||||
workflowDir: string,
|
||||
): Promise<string[]> {
|
||||
let files: string[]
|
||||
try {
|
||||
files = await readdir(workflowDir)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
return files
|
||||
.filter(f => isScriptExt(parse(f).ext))
|
||||
.map(f => parse(f).name)
|
||||
.sort()
|
||||
}
|
||||
26
packages/workflow-engine/src/engine/paths.ts
Normal file
26
packages/workflow-engine/src/engine/paths.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { resolve, sep } from 'node:path'
|
||||
|
||||
/**
|
||||
* 判断 target 解析后是否位于 base 之内(含等于 base)。
|
||||
* 相对 target 会相对 base 解析(不依赖 process.cwd)。
|
||||
* 用 `sep` 边界避免前缀假阳(如 `/foo` 不是 `/foobar` 的父目录)。
|
||||
*/
|
||||
export function containsPath(base: string, target: string): boolean {
|
||||
const resolvedBase = resolve(base)
|
||||
const resolvedTarget = resolve(resolvedBase, target)
|
||||
if (resolvedTarget === resolvedBase) return true
|
||||
return resolvedTarget.startsWith(resolvedBase + sep)
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验命名 workflow 的 name 是否为合法标识符(拒绝路径遍历)。
|
||||
* 拒绝:含路径分隔符、null 字节、`.` / `..`。
|
||||
* 返回清洗后的 name,或 null 表示非法。
|
||||
*/
|
||||
export function sanitizeWorkflowName(name: string): string | null {
|
||||
if (typeof name !== 'string' || name.length === 0) return null
|
||||
if (name.includes('/') || name.includes('\\')) return null
|
||||
if (name.includes('\0')) return null
|
||||
if (name === '.' || name === '..') return null
|
||||
return name
|
||||
}
|
||||
148
packages/workflow-engine/src/engine/runWorkflow.ts
Normal file
148
packages/workflow-engine/src/engine/runWorkflow.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { WORKFLOW_DIR_NAME } from '../constants.js'
|
||||
import type { HostHandle, WorkflowPorts } from '../ports.js'
|
||||
import type { JournalEntry, WorkflowRunResult } from '../types.js'
|
||||
import { createEngineContext } from './context.js'
|
||||
import { WorkflowAbortedError, WorkflowError } from './errors.js'
|
||||
import { makeHooks, type SubWorkflowRunner } from './hooks.js'
|
||||
import { resolveNamedWorkflow } from './namedWorkflows.js'
|
||||
import { parseScript, type ParsedScript } from './script.js'
|
||||
|
||||
export type RunWorkflowOptions = {
|
||||
/** 已解析好的脚本源码。 */
|
||||
script: string
|
||||
args?: unknown
|
||||
runId: string
|
||||
workflowName?: string
|
||||
ports: WorkflowPorts
|
||||
host: HostHandle
|
||||
signal: AbortSignal
|
||||
cwd: string
|
||||
budgetTotal: number | null
|
||||
/** resume:true 时载入既有 journal 重放。 */
|
||||
resume?: boolean
|
||||
/** resume 时脚本源码 hash 是否变化。true 则忽略 journal 全重跑。 */
|
||||
scriptChanged?: boolean
|
||||
}
|
||||
|
||||
export async function runWorkflow(
|
||||
opts: RunWorkflowOptions,
|
||||
): Promise<WorkflowRunResult> {
|
||||
const { ports } = opts
|
||||
|
||||
let parsed: ParsedScript
|
||||
try {
|
||||
parsed = parseScript(opts.script)
|
||||
} catch (e) {
|
||||
const error = (e as Error).message
|
||||
ports.progressEmitter.emit({
|
||||
type: 'run_done',
|
||||
runId: opts.runId,
|
||||
status: 'failed',
|
||||
error,
|
||||
})
|
||||
return { status: 'failed', error }
|
||||
}
|
||||
|
||||
const workflowName = opts.workflowName ?? parsed.meta?.name ?? 'workflow'
|
||||
|
||||
// 载入 journal(仅 resume 且脚本未变)
|
||||
let journal: JournalEntry[] = []
|
||||
let journalInvalidated = false
|
||||
if (opts.resume && !opts.scriptChanged) {
|
||||
journal = await ports.journalStore.read(opts.runId)
|
||||
} else if (opts.scriptChanged) {
|
||||
await ports.journalStore.truncate(opts.runId)
|
||||
journalInvalidated = true
|
||||
}
|
||||
|
||||
const ctx = createEngineContext({
|
||||
ports,
|
||||
host: opts.host,
|
||||
signal: opts.signal,
|
||||
runId: opts.runId,
|
||||
workflowName,
|
||||
cwd: opts.cwd,
|
||||
budgetTotal: opts.budgetTotal,
|
||||
journal,
|
||||
})
|
||||
if (journalInvalidated) ctx.journalInvalidated = true
|
||||
|
||||
ports.progressEmitter.emit({
|
||||
type: 'run_started',
|
||||
runId: opts.runId,
|
||||
workflowName,
|
||||
meta: parsed.meta,
|
||||
})
|
||||
|
||||
// 子 workflow 执行器:复用同一 ctx(共享 journal/并发/预算/计数),临时 +1 depth
|
||||
const runSubWorkflow: SubWorkflowRunner = async sub => {
|
||||
const script = await resolveSubScript(sub, opts.cwd)
|
||||
let subParsed: ParsedScript
|
||||
try {
|
||||
subParsed = parseScript(script)
|
||||
} catch (e) {
|
||||
throw new WorkflowError(`子 workflow 脚本错误:${(e as Error).message}`)
|
||||
}
|
||||
const prevDepth = ctx.resources.depth
|
||||
ctx.resources.depth += 1
|
||||
try {
|
||||
const subHooks = makeHooks(ctx, runSubWorkflow)
|
||||
return await subParsed.execute(subHooks, sub.args, ctx.resources.budget)
|
||||
} finally {
|
||||
ctx.resources.depth = prevDepth
|
||||
}
|
||||
}
|
||||
|
||||
const hooks = makeHooks(ctx, runSubWorkflow)
|
||||
|
||||
try {
|
||||
const returnValue = await parsed.execute(
|
||||
hooks,
|
||||
opts.args,
|
||||
ctx.resources.budget,
|
||||
)
|
||||
ports.progressEmitter.emit({
|
||||
type: 'run_done',
|
||||
runId: opts.runId,
|
||||
status: 'completed',
|
||||
returnValue,
|
||||
})
|
||||
return { status: 'completed', returnValue }
|
||||
} catch (e) {
|
||||
if (e instanceof WorkflowAbortedError) {
|
||||
ports.progressEmitter.emit({
|
||||
type: 'run_done',
|
||||
runId: opts.runId,
|
||||
status: 'killed',
|
||||
})
|
||||
return { status: 'killed' }
|
||||
}
|
||||
const error = (e as Error).message
|
||||
ports.progressEmitter.emit({
|
||||
type: 'run_done',
|
||||
runId: opts.runId,
|
||||
status: 'failed',
|
||||
error,
|
||||
})
|
||||
return { status: 'failed', error }
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveSubScript(
|
||||
sub: { name?: string; scriptPath?: string; script?: string },
|
||||
cwd: string,
|
||||
): Promise<string> {
|
||||
if (sub.script) return sub.script
|
||||
if (sub.scriptPath) return await readFile(sub.scriptPath, 'utf-8')
|
||||
if (sub.name) {
|
||||
const found = await resolveNamedWorkflow(
|
||||
join(cwd, WORKFLOW_DIR_NAME),
|
||||
sub.name,
|
||||
)
|
||||
if (!found) throw new WorkflowError(`子 workflow "${sub.name}" 未找到`)
|
||||
return found.content
|
||||
}
|
||||
throw new WorkflowError('workflow() 需要 name 或 scriptPath')
|
||||
}
|
||||
230
packages/workflow-engine/src/engine/script.ts
Normal file
230
packages/workflow-engine/src/engine/script.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
import type { WorkflowMeta } from '../types.js'
|
||||
|
||||
export class ScriptError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message)
|
||||
this.name = 'ScriptError'
|
||||
}
|
||||
}
|
||||
|
||||
/** 引擎注入脚本的钩子函数形状。 */
|
||||
export type WorkflowHooks = {
|
||||
agent: (prompt: string, opts?: Record<string, unknown>) => Promise<unknown>
|
||||
parallel: <T>(thunks: Array<() => Promise<T>>) => Promise<Array<T | null>>
|
||||
pipeline: <T, R>(
|
||||
items: readonly T[],
|
||||
...stages: Array<
|
||||
(prev: unknown, item: T, index: number) => Promise<unknown>
|
||||
>
|
||||
) => Promise<Array<R | null>>
|
||||
phase: (title: string) => void
|
||||
log: (message: string) => void
|
||||
workflow: (
|
||||
nameOrRef: string | { scriptPath: string },
|
||||
args?: unknown,
|
||||
) => Promise<unknown>
|
||||
}
|
||||
|
||||
const META_RE = /export\s+const\s+meta\s*=\s*/
|
||||
|
||||
/**
|
||||
* 提取 `export const meta = { ... }` 纯字面量。返回 meta 对象与剥离后的 body。
|
||||
* 字面量用无参 Function 求值——任何标识符引用都会抛 ReferenceError → 报「非纯字面量」。
|
||||
*/
|
||||
export function extractMeta(source: string): {
|
||||
meta: WorkflowMeta | null
|
||||
body: string
|
||||
} {
|
||||
const match = META_RE.exec(source)
|
||||
if (!match) return { meta: null, body: source }
|
||||
|
||||
let i = match.index + match[0].length
|
||||
while (i < source.length && /\s/.test(source[i]!)) i++
|
||||
if (source[i] !== '{') {
|
||||
throw new ScriptError('meta 必须是对象字面量 `{ ... }`')
|
||||
}
|
||||
|
||||
// 大括号匹配(处理字符串/转义/嵌套)
|
||||
let depth = 0
|
||||
const start = i
|
||||
let inStr: string | null = null
|
||||
for (; i < source.length; i++) {
|
||||
const ch = source[i]!
|
||||
if (inStr) {
|
||||
if (ch === '\\') {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (ch === inStr) inStr = null
|
||||
continue
|
||||
}
|
||||
if (ch === '"' || ch === "'" || ch === '`') {
|
||||
inStr = ch
|
||||
continue
|
||||
}
|
||||
if (ch === '{') depth++
|
||||
else if (ch === '}') {
|
||||
depth--
|
||||
if (depth === 0) {
|
||||
i++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (depth !== 0) throw new ScriptError('meta 字面量大括号未闭合')
|
||||
|
||||
const literal = source.slice(start, i)
|
||||
let metaObj: unknown
|
||||
try {
|
||||
// 无参 Function:纯字面量可求值;引用任何标识符 → ReferenceError
|
||||
metaObj = new Function(`return (${literal})`)()
|
||||
} catch (e) {
|
||||
throw new ScriptError(
|
||||
`meta 必须是纯字面量(无变量/函数调用/插值):${(e as Error).message}`,
|
||||
)
|
||||
}
|
||||
const meta = validateMeta(metaObj)
|
||||
|
||||
// 剥离 meta 语句(含尾随分号与多余空行)
|
||||
const body = (source.slice(0, match.index) + source.slice(i)).replace(
|
||||
/[ \t]*;[ \t]*\n/,
|
||||
'\n',
|
||||
)
|
||||
return { meta, body }
|
||||
}
|
||||
|
||||
function validateMeta(v: unknown): WorkflowMeta {
|
||||
if (typeof v !== 'object' || v === null || Array.isArray(v)) {
|
||||
throw new ScriptError('meta 必须是对象')
|
||||
}
|
||||
const o = v as Record<string, unknown>
|
||||
if (typeof o.name !== 'string' || typeof o.description !== 'string') {
|
||||
throw new ScriptError('meta 必须含字符串 name 与 description')
|
||||
}
|
||||
return o as unknown as WorkflowMeta
|
||||
}
|
||||
|
||||
// ---- 非确定性沙箱 shim ----
|
||||
class NonDeterministicError extends Error {
|
||||
constructor(fn: string) {
|
||||
super(
|
||||
`${fn} 在 workflow 脚本中不可用(会破坏 resume 的确定性)。请通过 args 传入时间戳/随机种子。`,
|
||||
)
|
||||
this.name = 'NonDeterministicError'
|
||||
}
|
||||
}
|
||||
|
||||
function sandboxDate(): DateConstructor {
|
||||
const fn = function (...args: unknown[]): Date {
|
||||
if (args.length === 0)
|
||||
throw new NonDeterministicError('Date.now()/new Date()')
|
||||
return new (Date as unknown as DateConstructor)(
|
||||
...(args as [string | number | Date]),
|
||||
)
|
||||
} as unknown as DateConstructor
|
||||
fn.now = () => {
|
||||
throw new NonDeterministicError('Date.now()')
|
||||
}
|
||||
fn.parse = Date.parse
|
||||
fn.UTC = Date.UTC
|
||||
return fn
|
||||
}
|
||||
|
||||
function sandboxMath(): Math {
|
||||
return new Proxy(Math, {
|
||||
get(target, prop, receiver) {
|
||||
if (prop === 'random') {
|
||||
return () => {
|
||||
throw new NonDeterministicError('Math.random()')
|
||||
}
|
||||
}
|
||||
return Reflect.get(target, prop, receiver)
|
||||
},
|
||||
}) as Math
|
||||
}
|
||||
|
||||
const AsyncFunction = Object.getPrototypeOf(async function () {})
|
||||
.constructor as {
|
||||
new (...args: string[]): (...args: unknown[]) => Promise<unknown>
|
||||
}
|
||||
|
||||
export type ParsedScript = {
|
||||
meta: WorkflowMeta | null
|
||||
execute: (
|
||||
hooks: WorkflowHooks,
|
||||
args: unknown,
|
||||
budget: unknown,
|
||||
) => Promise<unknown>
|
||||
}
|
||||
|
||||
/** 校验 + 包装脚本为可执行 async 函数(Date/Math 被 shim 覆盖)。 */
|
||||
/**
|
||||
* 检测脚本 body 的常见违例(import / 多余 export),给出带指引的精准错误。
|
||||
* 否则会落到 AsyncFunction 的泛化「语法错误」,模型/用户难定位根因
|
||||
* (脚本是非 ESM 函数体、钩子已注入、引擎不转译 TS)。
|
||||
*/
|
||||
function assertScriptBody(body: string): void {
|
||||
if (/^\s*import\b/m.test(body)) {
|
||||
throw new ScriptError(
|
||||
'workflow 脚本是 new AsyncFunction 的函数体(非 ESM 模块),不支持 import。' +
|
||||
'agent / parallel / pipeline / phase / log / workflow / args / budget 已作为形参注入,直接使用。',
|
||||
)
|
||||
}
|
||||
// 动态 import(...) 调用:沙箱仅保 resume 确定性不保安全,但应阻止明显的逃逸尝试。
|
||||
// 不锚定行首以捕获 `await import(...)`、`return import(...)` 等位置;要求 `import` 后紧跟 `(` 才拦截,
|
||||
// 避免误伤字符串字面量里出现 "import" 词(如 agent('please import this module'))。
|
||||
if (/\bimport\s*\(/m.test(body)) {
|
||||
throw new ScriptError(
|
||||
'workflow 脚本中禁止动态 import(...):会绕过 Date/Math 沙箱,破坏 resume 确定性。' +
|
||||
'沙箱不保安全(与 LLM 同级信任),但禁止显式逃逸。需要外部依赖时通过 args 注入。',
|
||||
)
|
||||
}
|
||||
if (/^\s*export\b/m.test(body)) {
|
||||
throw new ScriptError(
|
||||
'workflow 脚本只允许一处 export const meta = {...}(已被引擎提取)。' +
|
||||
'请删除其余 export / export default;用顶层 return 返回结果。',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export function parseScript(source: string): ParsedScript {
|
||||
const { meta, body } = extractMeta(source)
|
||||
assertScriptBody(body)
|
||||
let fn: (...args: unknown[]) => Promise<unknown>
|
||||
try {
|
||||
fn = new AsyncFunction(
|
||||
'agent',
|
||||
'parallel',
|
||||
'pipeline',
|
||||
'phase',
|
||||
'log',
|
||||
'workflow',
|
||||
'args',
|
||||
'budget',
|
||||
'Date',
|
||||
'Math',
|
||||
body,
|
||||
)
|
||||
} catch (e) {
|
||||
throw new ScriptError(`脚本语法错误:${(e as Error).message}`)
|
||||
}
|
||||
const sandboxedDate = sandboxDate()
|
||||
const sandboxedMath = sandboxMath()
|
||||
return {
|
||||
meta,
|
||||
async execute(hooks, args, budget) {
|
||||
return fn(
|
||||
hooks.agent,
|
||||
hooks.parallel,
|
||||
hooks.pipeline,
|
||||
hooks.phase,
|
||||
hooks.log,
|
||||
hooks.workflow,
|
||||
args,
|
||||
budget,
|
||||
sandboxedDate,
|
||||
sandboxedMath,
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
26
packages/workflow-engine/src/engine/structuredOutput.ts
Normal file
26
packages/workflow-engine/src/engine/structuredOutput.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Ajv, type ValidateFunction } from 'ajv'
|
||||
|
||||
const cache = new WeakMap<object, ValidateFunction>()
|
||||
|
||||
/**
|
||||
* 用 JSON Schema 校验 agent 输出(Ajv,编译结果按 schema 对象缓存)。
|
||||
* 引擎对 adapter 返回的 schema 结果做二次校验,并用于测试。
|
||||
*/
|
||||
export function validateAgainstSchema(
|
||||
value: unknown,
|
||||
schema: object,
|
||||
): { valid: boolean; errors: string[] } {
|
||||
let validate = cache.get(schema)
|
||||
if (!validate) {
|
||||
const ajv = new Ajv({ allErrors: true, strict: false })
|
||||
validate = ajv.compile(schema) as ValidateFunction
|
||||
cache.set(schema, validate)
|
||||
}
|
||||
const valid = validate(value) as boolean
|
||||
return {
|
||||
valid,
|
||||
errors: valid
|
||||
? []
|
||||
: (validate.errors ?? []).map(e => e.message ?? 'validation error'),
|
||||
}
|
||||
}
|
||||
24
packages/workflow-engine/src/index.ts
Normal file
24
packages/workflow-engine/src/index.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
// @claude-code-best/workflow-engine
|
||||
// 确定性 JS 脚本编排引擎。零核心层运行时依赖,通过端口适配与世界对话。
|
||||
|
||||
export * from './types.js'
|
||||
export * from './constants.js'
|
||||
export * from './ports.js'
|
||||
export * from './agentAdapter.js'
|
||||
export * from './engine/concurrency.js'
|
||||
export * from './engine/script.js'
|
||||
export * from './engine/journal.js'
|
||||
export * from './engine/budget.js'
|
||||
export * from './engine/structuredOutput.js'
|
||||
export * from './engine/namedWorkflows.js'
|
||||
export * from './engine/errors.js'
|
||||
export * from './engine/context.js'
|
||||
export * from './engine/hooks.js'
|
||||
export * from './engine/runWorkflow.js'
|
||||
export * from './progress/events.js'
|
||||
export {
|
||||
createWorkflowTool,
|
||||
type WorkflowToolDescriptor,
|
||||
} from './tool/WorkflowTool.js'
|
||||
export { workflowInputSchema, type WorkflowInput } from './tool/schema.js'
|
||||
export { WORKFLOW_TOOL_NAME } from './tool/constants.js'
|
||||
134
packages/workflow-engine/src/ports.ts
Normal file
134
packages/workflow-engine/src/ports.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import type { AgentAdapterRegistry } from './agentAdapter.js'
|
||||
import type {
|
||||
AgentRunParams,
|
||||
AgentRunResult,
|
||||
JournalEntry,
|
||||
ProgressEvent,
|
||||
} from './types.js'
|
||||
|
||||
/**
|
||||
* 不透明 host 句柄。核心侧每次工具调用构造一个,内含 toolUseContext/
|
||||
* canUseTool/parentMessage 等。包内绝不检视其内部,只透传给 AgentRunner。
|
||||
* 这是包与核心层之间唯一的耦合缝隙,且是不透明的。
|
||||
*/
|
||||
const HOST_HANDLE = Symbol('workflow.hostHandle')
|
||||
|
||||
export type HostBundle = unknown
|
||||
|
||||
export type HostHandle = { readonly [HOST_HANDLE]: HostBundle }
|
||||
|
||||
/** 核心 side hostFactory 用:把任意 bundle 包成不透明句柄。 */
|
||||
export function createHostHandle(bundle: HostBundle): HostHandle {
|
||||
return { [HOST_HANDLE]: bundle } as HostHandle
|
||||
}
|
||||
|
||||
/** 类型守卫。 */
|
||||
export function isHostHandle(value: unknown): value is HostHandle {
|
||||
return (
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
HOST_HANDLE in (value as object)
|
||||
)
|
||||
}
|
||||
|
||||
/** 核心 side adapter 用:解包(仅 adapter 应调用)。 */
|
||||
export function unwrapHostHandle(handle: HostHandle): HostBundle {
|
||||
return (handle as { [k: symbol]: HostBundle })[HOST_HANDLE]
|
||||
}
|
||||
|
||||
/** agent() 钩子的后端。 */
|
||||
export type AgentRunner = {
|
||||
runAgentToResult(
|
||||
params: AgentRunParams,
|
||||
host: HostHandle,
|
||||
): Promise<AgentRunResult>
|
||||
}
|
||||
|
||||
/** 进度事件发射。 */
|
||||
export type ProgressEmitter = {
|
||||
emit(event: ProgressEvent): void
|
||||
}
|
||||
|
||||
/** 后台任务生命周期。 */
|
||||
export type TaskRegistrar = {
|
||||
/**
|
||||
* 注册后台任务。adapter 创建 AbortController 并存入 task 状态,
|
||||
* 返回 runId 与 signal(供引擎 detached 执行 + kill 中止用)。
|
||||
*/
|
||||
register(
|
||||
opts: {
|
||||
workflowName: string
|
||||
workflowFile?: string
|
||||
summary?: string
|
||||
toolUseId?: string
|
||||
/** resume 时复用既有 runId(读其 journal)。省略则生成新 id。 */
|
||||
runId?: string
|
||||
},
|
||||
host: HostHandle,
|
||||
): { runId: string; signal: AbortSignal }
|
||||
complete(runId: string, summary?: string): void
|
||||
fail(runId: string, error: string): void
|
||||
kill(runId: string): void
|
||||
/** 返回当前待处理的 skip/retry 动作,或 null。 */
|
||||
pendingAction(runId: string): { kind: 'skip' | 'retry' } | null
|
||||
}
|
||||
|
||||
/** journal 持久化。 */
|
||||
export type JournalStore = {
|
||||
read(runId: string): Promise<JournalEntry[]>
|
||||
append(runId: string, entry: JournalEntry): Promise<void>
|
||||
truncate(runId: string): Promise<void>
|
||||
}
|
||||
|
||||
/** 取消/权限门。 */
|
||||
export type PermissionGate = {
|
||||
isAborted(host: HostHandle): boolean
|
||||
}
|
||||
|
||||
/** 日志 + 遥测。 */
|
||||
export type Logger = {
|
||||
debug(msg: string): void
|
||||
event(name: string, metadata?: Record<string, unknown>): void
|
||||
/**
|
||||
* 警告级日志(如 parallel/pipeline 单项失败被吞掉的错误)。
|
||||
* Optional:旧 ports 实现可省略;hooks 用 `?.()` 容错。
|
||||
*/
|
||||
warn?(msg: string): void
|
||||
}
|
||||
|
||||
/** 引擎从 host 提取的可直接使用上下文(句柄 + 基本字段)。 */
|
||||
export type WorkflowHostContext = {
|
||||
/** 透传给 AgentRunner 的不透明句柄(内含 toolUseContext/canUseTool/parentMessage)。 */
|
||||
handle: HostHandle
|
||||
cwd: string
|
||||
/** token 预算上限,null 表示无限制。 */
|
||||
budgetTotal: number | null
|
||||
/** 核心 side 的工具调用 ID(透传给 task 注册)。 */
|
||||
toolUseId?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 核心 side 提供:从工具调用的核心上下文构造 WorkflowHostContext。
|
||||
* 参数对包是不透明的(unknown);核心侧 hostFactory 知道真实类型。
|
||||
*/
|
||||
export type HostFactory = (args: {
|
||||
context: unknown
|
||||
canUseTool: unknown
|
||||
parentMessage: unknown
|
||||
}) => WorkflowHostContext
|
||||
|
||||
/** 所有端口的聚合。createWorkflowTool(ports) 注入。 */
|
||||
export type WorkflowPorts = {
|
||||
agentRunner: AgentRunner
|
||||
/**
|
||||
* 多后端 adapter registry。提供时优先于 agentRunner——hooks.agent 按 registry
|
||||
* 路由到 adapter.run;省略则回退 agentRunner(兼容旧用法)。
|
||||
*/
|
||||
agentAdapterRegistry?: AgentAdapterRegistry
|
||||
progressEmitter: ProgressEmitter
|
||||
taskRegistrar: TaskRegistrar
|
||||
journalStore: JournalStore
|
||||
permissionGate: PermissionGate
|
||||
logger: Logger
|
||||
hostFactory: HostFactory
|
||||
}
|
||||
20
packages/workflow-engine/src/progress/events.ts
Normal file
20
packages/workflow-engine/src/progress/events.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { ProgressEmitter } from '../ports.js'
|
||||
import type { ProgressEvent } from '../types.js'
|
||||
|
||||
export type { ProgressEvent }
|
||||
|
||||
/** 从单个回调构造 ProgressEmitter。 */
|
||||
export function createProgressEmitter(
|
||||
onEvent: (e: ProgressEvent) => void,
|
||||
): ProgressEmitter {
|
||||
return { emit: onEvent }
|
||||
}
|
||||
|
||||
/** 收集所有事件到数组(测试用)。 */
|
||||
export function createBufferingEmitter(): {
|
||||
emitter: ProgressEmitter
|
||||
events: ProgressEvent[]
|
||||
} {
|
||||
const events: ProgressEvent[] = []
|
||||
return { emitter: { emit: e => void events.push(e) }, events }
|
||||
}
|
||||
232
packages/workflow-engine/src/tool/WorkflowTool.ts
Normal file
232
packages/workflow-engine/src/tool/WorkflowTool.ts
Normal file
@@ -0,0 +1,232 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { join, resolve } from 'node:path'
|
||||
import { z } from 'zod/v4'
|
||||
import { WORKFLOW_DIR_NAME, WORKFLOW_TOOL_NAME } from '../constants.js'
|
||||
import { resolveNamedWorkflow } from '../engine/namedWorkflows.js'
|
||||
import { runWorkflow } from '../engine/runWorkflow.js'
|
||||
import { parseScript } from '../engine/script.js'
|
||||
import { containsPath, sanitizeWorkflowName } from '../engine/paths.js'
|
||||
import type { WorkflowPorts } from '../ports.js'
|
||||
import type { WorkflowRunResult } from '../types.js'
|
||||
import { workflowInputSchema, type WorkflowInput } from './schema.js'
|
||||
|
||||
/** 自包含工具描述符(核心 wiring 用 buildTool 包装它)。零核心层依赖。 */
|
||||
export type WorkflowToolDescriptor = {
|
||||
name: string
|
||||
inputSchema: z.ZodType<WorkflowInput>
|
||||
isEnabled: () => boolean
|
||||
isReadOnly: (input: WorkflowInput) => boolean
|
||||
description: () => Promise<string>
|
||||
prompt: () => Promise<string>
|
||||
renderToolUseMessage: (input: Partial<WorkflowInput>) => string
|
||||
call: (
|
||||
input: WorkflowInput,
|
||||
context: unknown,
|
||||
canUseTool: unknown,
|
||||
parentMessage: unknown,
|
||||
onProgress?: unknown,
|
||||
) => Promise<{ data: { output: string } }>
|
||||
mapToolResultToToolResultBlockParam: (
|
||||
data: { output: string },
|
||||
toolUseId: string,
|
||||
) => {
|
||||
tool_use_id: string
|
||||
type: 'tool_result'
|
||||
content: Array<{ type: 'text'; text: string }>
|
||||
}
|
||||
}
|
||||
|
||||
const WORKFLOW_TOOL_PROMPT = `Use the Workflow tool to execute a workflow script that orchestrates multiple subagents deterministically. The script runs in the background; you receive a run_id immediately and are notified on completion.
|
||||
|
||||
Provide the script inline via "script", or reference a named workflow via "name" (resolved from .claude/workflows/), or an existing file via "scriptPath". Pass "args" as a real JSON value (object/array/string), not a stringified string.
|
||||
|
||||
Use "resumeFromRunId" to resume a prior run — completed agent() calls replay from the journal instantly.
|
||||
|
||||
Script execution model (common pitfalls — getting these wrong is the #1 cause of script errors): the script is the body of \`new AsyncFunction\` — NOT an ESM module, and TypeScript is NOT transpiled. Therefore:
|
||||
- Do NOT use \`import\` — \`agent\`, \`parallel\`, \`pipeline\`, \`phase\`, \`log\`, \`workflow\`, \`args\`, and \`budget\` are injected as parameters; reference them directly.
|
||||
- Do NOT use TS type annotations, \`interface\`, \`enum\`, \`as\`, or generics — the engine does not transpile, so even a .ts file with type syntax fails to parse.
|
||||
- Keep EXACTLY ONE \`export const meta = {...}\` (plain literal) and remove every other \`export\` / \`export default\`.
|
||||
- Return the result with a top-level \`return\`.
|
||||
Prefer .js / .mjs. See /ultracode for the full playbook and quality patterns.`
|
||||
|
||||
export function createWorkflowTool(
|
||||
ports: WorkflowPorts,
|
||||
): WorkflowToolDescriptor {
|
||||
return {
|
||||
name: WORKFLOW_TOOL_NAME,
|
||||
inputSchema: workflowInputSchema,
|
||||
isEnabled: () => true,
|
||||
isReadOnly: () => false,
|
||||
|
||||
async description() {
|
||||
return '执行一个 workflow 脚本,编排多个子 agent 完成任务'
|
||||
},
|
||||
|
||||
async prompt() {
|
||||
return WORKFLOW_TOOL_PROMPT
|
||||
},
|
||||
|
||||
renderToolUseMessage(input) {
|
||||
if (input.resumeFromRunId)
|
||||
return `Workflow resume: ${input.resumeFromRunId}`
|
||||
const id =
|
||||
input.name ?? input.scriptPath ?? (input.script ? 'inline' : 'unknown')
|
||||
return `Workflow: ${id}`
|
||||
},
|
||||
|
||||
async call(input, context, canUseTool, parentMessage) {
|
||||
const host = ports.hostFactory({ context, canUseTool, parentMessage })
|
||||
|
||||
// 解析脚本源
|
||||
let script: string
|
||||
let workflowFile: string | undefined
|
||||
try {
|
||||
const resolved = await resolveScriptSource(input, host.cwd)
|
||||
script = resolved.script
|
||||
workflowFile = resolved.workflowFile
|
||||
} catch (e) {
|
||||
return { data: { output: `Error: ${(e as Error).message}` } }
|
||||
}
|
||||
|
||||
// 快速校验(meta + 语法),失败直接返错给模型,不进后台
|
||||
try {
|
||||
parseScript(script)
|
||||
} catch (e) {
|
||||
return {
|
||||
data: { output: `Error: 脚本校验失败:${(e as Error).message}` },
|
||||
}
|
||||
}
|
||||
|
||||
const workflowName = input.name ?? input.title ?? 'workflow'
|
||||
const { runId, signal } = ports.taskRegistrar.register(
|
||||
{
|
||||
workflowName,
|
||||
...(workflowFile ? { workflowFile } : {}),
|
||||
...(input.description ? { summary: input.description } : {}),
|
||||
...(host.toolUseId ? { toolUseId: host.toolUseId } : {}),
|
||||
...(input.resumeFromRunId ? { runId: input.resumeFromRunId } : {}),
|
||||
},
|
||||
host.handle,
|
||||
)
|
||||
|
||||
// detached 执行
|
||||
void runWorkflow({
|
||||
script,
|
||||
...(input.args !== undefined
|
||||
? { args: normalizeArgs(input.args) }
|
||||
: {}),
|
||||
runId,
|
||||
workflowName,
|
||||
ports,
|
||||
host: host.handle,
|
||||
signal,
|
||||
cwd: host.cwd,
|
||||
budgetTotal: host.budgetTotal,
|
||||
...(input.resumeFromRunId ? { resume: true } : {}),
|
||||
})
|
||||
.then(result => onFinish(ports, result, runId))
|
||||
.catch(e => ports.taskRegistrar.fail(runId, (e as Error).message))
|
||||
|
||||
const scriptPath = workflowFile ?? `<inline run ${runId}>`
|
||||
return {
|
||||
data: {
|
||||
output: [
|
||||
'Workflow 已启动(后台执行)。',
|
||||
`run_id: ${runId}`,
|
||||
`workflow: ${workflowName}`,
|
||||
`script: ${scriptPath}`,
|
||||
'',
|
||||
'完成时会自动通知。用 /workflows 查看实时进度。',
|
||||
].join('\n'),
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
mapToolResultToToolResultBlockParam(data, toolUseId) {
|
||||
return {
|
||||
tool_use_id: toolUseId,
|
||||
type: 'tool_result',
|
||||
content: [{ type: 'text', text: data.output }],
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function onFinish(
|
||||
ports: WorkflowPorts,
|
||||
result: WorkflowRunResult,
|
||||
runId: string,
|
||||
): void {
|
||||
if (result.status === 'completed') {
|
||||
const summary =
|
||||
result.returnValue == null
|
||||
? '(no return value)'
|
||||
: formatValue(result.returnValue)
|
||||
ports.taskRegistrar.complete(runId, summary)
|
||||
} else if (result.status === 'failed') {
|
||||
ports.taskRegistrar.fail(runId, result.error ?? 'workflow failed')
|
||||
} else {
|
||||
ports.taskRegistrar.kill(runId)
|
||||
}
|
||||
}
|
||||
|
||||
function formatValue(v: unknown): string {
|
||||
if (typeof v === 'string') return v.slice(0, 500)
|
||||
try {
|
||||
return JSON.stringify(v).slice(0, 500)
|
||||
} catch {
|
||||
return String(v)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 防御性归一化 args:旧 `z.string()` 契约下模型可能发送字符串化的 JSON 对象。
|
||||
* 仅当字符串能 JSON.parse 出对象/数组时归一化;纯字符串、数字等保留原值。
|
||||
*/
|
||||
function normalizeArgs(raw: unknown): unknown {
|
||||
if (typeof raw !== 'string') return raw
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw)
|
||||
if (typeof parsed === 'object' && parsed !== null) return parsed
|
||||
return raw
|
||||
} catch {
|
||||
return raw
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveScriptSource(
|
||||
input: WorkflowInput,
|
||||
cwd: string,
|
||||
): Promise<{ script: string; workflowFile?: string }> {
|
||||
if (input.script) return { script: input.script }
|
||||
if (input.scriptPath) {
|
||||
const resolved = resolve(cwd, input.scriptPath)
|
||||
if (!containsPath(cwd, resolved)) {
|
||||
throw new Error(
|
||||
`scriptPath "${input.scriptPath}" 越界(resolve 后 ${resolved} 不在 cwd ${cwd} 之内)`,
|
||||
)
|
||||
}
|
||||
return {
|
||||
script: await readFile(resolved, 'utf-8'),
|
||||
workflowFile: resolved,
|
||||
}
|
||||
}
|
||||
if (input.name) {
|
||||
if (sanitizeWorkflowName(input.name) === null) {
|
||||
throw new Error(
|
||||
`命名 workflow 名字 "${input.name}" 非法(含路径分隔符或为 . / ..)`,
|
||||
)
|
||||
}
|
||||
const found = await resolveNamedWorkflow(
|
||||
join(cwd, WORKFLOW_DIR_NAME),
|
||||
input.name,
|
||||
)
|
||||
if (!found) {
|
||||
throw new Error(
|
||||
`命名 workflow "${input.name}" 未找到(查找目录 ${WORKFLOW_DIR_NAME}/)`,
|
||||
)
|
||||
}
|
||||
return { script: found.content, workflowFile: found.path }
|
||||
}
|
||||
throw new Error('必须提供 script、name 或 scriptPath 之一')
|
||||
}
|
||||
1
packages/workflow-engine/src/tool/constants.ts
Normal file
1
packages/workflow-engine/src/tool/constants.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { WORKFLOW_TOOL_NAME } from '../constants.js'
|
||||
37
packages/workflow-engine/src/tool/schema.ts
Normal file
37
packages/workflow-engine/src/tool/schema.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { z } from 'zod/v4'
|
||||
|
||||
/** Workflow 工具输入 schema。args 为任意 JSON 值(对象/数组/字符串等)。 */
|
||||
export const workflowInputSchema = z.object({
|
||||
script: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('自包含的 workflow 脚本源码(inline)'),
|
||||
name: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('命名 workflow,解析到 .claude/workflows/<name>.ts|js|mjs'),
|
||||
scriptPath: z.string().optional().describe('已有脚本文件的绝对路径'),
|
||||
args: z
|
||||
.unknown()
|
||||
.optional()
|
||||
.describe(
|
||||
'透传给脚本的 args 全局变量。传真实 JSON 值(对象/数组/字符串),不要传 JSON 字符串。',
|
||||
),
|
||||
resumeFromRunId: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('resume 指定 run,重放 journal'),
|
||||
description: z.string().optional().describe('本次调用的简短描述(3-5 词)'),
|
||||
title: z.string().optional().describe('进度查看器标题'),
|
||||
})
|
||||
|
||||
/**
|
||||
* Workflow 工具输入类型——从 schema 派生,避免手工 type 与 schema 漂移。
|
||||
* 旧实现里 {@link WorkflowInput} 在 types.ts 手写、schema 在 schema.ts,
|
||||
* 中间靠 `as unknown as z.ZodType<WorkflowInput>` 双重断言连接——schema 改字段
|
||||
* 但 type 没动时 TS 不会报错。z.infer 后 schema/type 永远同步。
|
||||
*/
|
||||
export type WorkflowInput = z.infer<typeof workflowInputSchema>
|
||||
|
||||
/** schema 的 typeof 类型(用于"以 schema 为准"的精确签名)。 */
|
||||
export type WorkflowInputSchema = typeof workflowInputSchema
|
||||
83
packages/workflow-engine/src/types.ts
Normal file
83
packages/workflow-engine/src/types.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
// 纯类型定义。无运行时依赖。
|
||||
// WorkflowInput 已迁移到 tool/schema.ts,用 z.infer 派生避免与 schema 漂移。
|
||||
|
||||
/** 脚本 `export const meta = {...}` 的形状(必须是纯字面量)。 */
|
||||
export type WorkflowMeta = {
|
||||
name: string
|
||||
description: string
|
||||
whenToUse?: string
|
||||
phases?: Array<{ title: string; detail?: string }>
|
||||
}
|
||||
|
||||
/** agent() 传给 AgentRunner 的参数。 */
|
||||
export type AgentRunParams = {
|
||||
prompt: string
|
||||
/** JSON Schema;提供时 agent 返回校验对象而非文本。 */
|
||||
schema?: object
|
||||
model?: string
|
||||
/** 输出 token 上限(透传给 agent 后端,如 LLM 的 max_tokens)。 */
|
||||
maxTokens?: number
|
||||
/** 自定义子 agent 类型(从 registry 解析)。 */
|
||||
agentType?: string
|
||||
isolation?: 'worktree'
|
||||
allowedTools?: string[]
|
||||
/** 仅展示用,不计入 journal key。 */
|
||||
label?: string
|
||||
/** 仅展示用,不计入 journal key。 */
|
||||
phase?: string
|
||||
}
|
||||
|
||||
/** AgentRunner 返回。 */
|
||||
export type AgentRunResult =
|
||||
| { kind: 'ok'; output: string | object; usage: { outputTokens: number } }
|
||||
| { kind: 'skipped' }
|
||||
| { kind: 'dead' }
|
||||
|
||||
/** journal 中单条记录。seq = agent() 调用序号,read() 据此重排以稳定 resume。 */
|
||||
export type JournalEntry = {
|
||||
key: string
|
||||
/** agent() 调用顺序(来自 agentIdSeq,跨 sub-workflow 单调递增)。 */
|
||||
seq: number
|
||||
result: AgentRunResult
|
||||
}
|
||||
|
||||
/** 进度事件。所有变体携带 runId,供 adapter 路由到对应 task(多并发 workflow)。 */
|
||||
export type ProgressEvent =
|
||||
| {
|
||||
type: 'run_started'
|
||||
runId: string
|
||||
workflowName: string
|
||||
meta: WorkflowMeta | null
|
||||
}
|
||||
| { type: 'phase_started'; runId: string; phase: string }
|
||||
| { type: 'phase_done'; runId: string; phase: string }
|
||||
| {
|
||||
type: 'agent_started'
|
||||
runId: string
|
||||
agentId: number
|
||||
label?: string
|
||||
phase?: string
|
||||
}
|
||||
| {
|
||||
type: 'agent_done'
|
||||
runId: string
|
||||
agentId: number
|
||||
label?: string
|
||||
phase?: string
|
||||
result: AgentRunResult
|
||||
}
|
||||
| { type: 'log'; runId: string; message: string }
|
||||
| {
|
||||
type: 'run_done'
|
||||
runId: string
|
||||
status: 'completed' | 'failed' | 'killed'
|
||||
returnValue?: unknown
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** 引擎运行结果。 */
|
||||
export type WorkflowRunResult = {
|
||||
status: 'completed' | 'failed' | 'killed'
|
||||
returnValue?: unknown
|
||||
error?: string
|
||||
}
|
||||
17
packages/workflow-engine/tsconfig.json
Normal file
17
packages/workflow-engine/tsconfig.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"types": ["bun"],
|
||||
"lib": ["ESNext"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user