跳到主要内容

Workflow.tool()

.tool() 方法将 tool 添加为声明式步骤。该 tool 自身的输入和输出 schema 会生效,因此前一个步骤的输出必须满足 tool 的输入 schema。如果数据不匹配,请使用 .map() 转换数据。

与使用 createStep() 包装 tool 不同,.tool() 会在 workflow 图中记录一个声明式条目。这使 workflow 可移植:同一个图可以序列化并持久化为动态 workflow

使用示例
使用示例的直接链接

workflow.tool(testTool).commit();

参数
参数的直接链接

toolOrId:

Tool | string
Tool 实例,或在 Mastra 实例上注册的 tool ID。传入 ID 时,tool 会在执行时从注册表中解析。

options?:

{ retries?: number, scorers?: DynamicArgument<MastraScorers>, metadata?: StepMetadata }
tool 步骤的步骤级别重试次数、scorers 和 metadata。

stepOptions?:

{ id?: string }
步骤在 workflow 中调用位置的 ID。默认使用 tool 的 ID。同一个 tool 在一个 workflow 中出现多次时,请设置此项。

返回值
返回值的直接链接

workflow:

Workflow
用于方法链式调用的 workflow 实例

通过 ID 引用 tool
通过 ID 引用 tool的直接链接

传入字符串可引用已注册的 tool,而无需导入它。workflow 运行时,该 tool 必须已在 Mastra 实例上注册:

workflow.tool("lookup-customer", { retries: 2 }).commit();

持久化 tool 步骤
持久化 tool 步骤的直接链接

使用 .tool() 构建的 workflow 会序列化为动态 workflow使用的同一种声明式条目。只有 retriesmetadata 可以通过存储往返保留。值为函数的 scorers 选项会在存储 workflow 时抛出错误。