Skip to content

Engine API

进程内工作流执行核心:createWorkflowEngineWorkflowEngine

createWorkflowEngine

typescript
function createWorkflowEngine(
  config: WorkflowConfig,
  options?: WorkflowEngineOptions,
): WorkflowEngine

WorkflowConfig

字段类型必填说明
workflowIdstring工作流标识
unitsMap<UnitId, WorkflowUnit>Unit 注册表
controlFlowControlFlow控制流实例
sharedStateSharedState默认内存 SharedState
messageBusMessageBus默认内存 MessageBus

WorkflowEngineOptions

字段类型说明
contextManagerContextManager上下文 / 记忆组装
checkpointStoreCheckpointStore检查点持久化
observabilityObservability追踪与指标
policyEnginePolicyEngine超时 / 预算 / 熔断
securitySecurityGovernance权限与 HITL
policyConfigPartial<PolicyConfig>策略配置覆盖
caller{ id: string; roles: string[] }调用方身份

run

typescript
async run(input?: Record<string, unknown>): Promise<WorkflowResult>
参数类型说明
inputobject键值写入 SharedState 后启动执行循环

WorkflowResult

字段类型说明
runIdstring运行 ID
completedUnitsstring[]已完成 Unit
stateRecord<string, unknown>SharedState 快照
messagesWorkflowMessage[]含 HITL 等消息
durationnumber毫秒
tokenUsagenumber累计 token
costnumber累计成本

resume

typescript
async resume(runId: string, snapshotId?: string): Promise<WorkflowResult>
参数类型说明
runIdstring要恢复的运行 ID
snapshotIdstring可选;指定检查点

从 CheckpointStore 加载快照,恢复 ControlFlow 游标与 SharedState,继续执行。

无检查点时抛出:No checkpoint found for run: <runId>

其他 WorkflowEngine 方法

方法说明
steer(targetUnitId, content)向运行中 Unit 注入 steering
followUp(targetUnitId, content)注入 follow-up
respondToHITL(approved, responder)响应 HITL 门控
getRunId()当前 runId
getSharedState()访问 SharedState

示例

typescript
import { createWorkflowEngine, createSharedState, SequentialFlow } from 'uni-flow';

const engine = createWorkflowEngine({
  workflowId: 'demo',
  units,
  controlFlow: new SequentialFlow([unitA, unitB]),
  sharedState: createSharedState(),
});

const result = await engine.run({ task: 'hello' });
console.log(result.completedUnits, result.state);

相关

MIT Licensed