Skip to content

YAML API

从 Workflow YAML 构建 Engine 与配置的工具函数,位于 uni-flowyaml 模块。

createEngineFromYaml

typescript
async function createEngineFromYaml(
  source: string,
  options?: CreateEngineFromYamlOptions,
): Promise<WorkflowEngine>
参数类型说明
sourcestring文件路径(可读)或内联 YAML 字符串
optionsCreateEngineFromYamlOptions见下表

CreateEngineFromYamlOptions

字段类型说明
registryUnitPluginRegistry自定义 uses → 插件映射
bindingsUsesBindingsHTTP 远程 Unit;与 registry 合并,registry 优先
engineOptionsWorkflowEngineOptions转发给 createWorkflowEngine
typescript
const engine = await createEngineFromYaml('workflow.yaml', {
  registry: { 'my.agent': () => createMockAdapter() },
  bindings: { 'remote.agent': { type: 'http', endpoint: 'http://...' } },
});
const result = await engine.run({ task: 'demo' });

validateWorkflowYamlSource

typescript
function validateWorkflowYamlSource(source: string): WorkflowYamlDocument
参数类型说明
sourcestringYAML 文本(非文件路径)

对照 Schema:schemas/uniflow.workflow.schema.json。失败抛出 YamlValidationError

CLI uniflow validate 内部使用此函数(先读文件再校验)。

符号级中文注解见 生成附录说明附录正文(查找 createEngineFromYaml / validateWorkflowYamlSource / runWorkflowAsUnit)。

相关函数

函数说明
createEngineFromDocument(doc, options?)已解析文档 → Engine
createWorkflowConfigFromYaml(source, options?){ workflowId, config, options? }
createWorkflowConfigFromDocument(doc, options?)文档 → 配置
validateWorkflowDocument(doc)校验已解析对象
loadWorkflowSchema()加载 JSON Schema
resolveSchemaPath()解析 Schema 文件路径
registryFromBindings(bindings?)bindings → 插件注册表
mergePluginRegistries(...parts)合并多个注册表
runWorkflowAsUnit(source, input, options?)Workflow-as-Unit:YAML → AgentOutput
createWorkflowAsUnitHttpHandler(source, options?)Node POST /execute 监听器工厂

错误类型

何时抛出
YamlValidationErrorSchema 不通过
YamlLoadError插件解析 / bindings / flow 映射失败

CLI validate

bash
npx uniflow validate path/to/workflow.yaml

仅 Schema 校验,不 resolve 插件、不执行工作流。

示例

typescript
import { createEngineFromYaml, validateWorkflowYamlSource } from 'uni-flow';
import { readFileSync } from 'node:fs';

const yaml = readFileSync('examples/yaml-sequential.yaml', 'utf8');
const doc = validateWorkflowYamlSource(yaml);
console.log('id:', doc.metadata.id);

const engine = await createEngineFromYaml(yaml);
await engine.run({ task: 'test' });

相关

MIT Licensed