Run.resume()
.resume() 方法使用新数据恢复已暂停的 workflow run,让你可以从特定步骤继续执行。
使用示例使用示例的直接链接
const run = await workflow.createRun()
const result = await run.start({ inputData: { value: 'initial data' } })
if (result.status === 'suspended') {
const resumedResults = await run.resume({
resumeData: { value: 'resume data' },
})
}
参数参数的直接链接
resumeData?:
z.infer<TResumeSchema>
用于恢复已暂停步骤的数据
step?:
Step<string, any, any, TResumeSchema, any, TEngineType> | [...Step<string, any, any, any, any, TEngineType>[], Step<string, any, any, TResumeSchema, any, TEngineType>] | string | string[]
作为恢复执行起点的步骤。可以是 Step 实例、Step 数组、步骤 ID 字符串或步骤 ID 字符串数组
requestContext?:
RequestContext
恢复执行时使用的 Request Context 数据
retryCount?:
number
嵌套 workflow 执行的可选重试次数
forEachIndex?:
number
指定已暂停
.foreach() 步骤中的某次迭代。传入要恢复的迭代从零开始的索引;其他迭代保持暂停。省略此字段可使用同一 resumeData 恢复该步骤所有已暂停的迭代。tracingContext?:
TracingContext
用于创建子 span 和添加 metadata 的 Tracing 上下文。使用 Mastra 的 tracing 系统时会自动注入。
currentSpan?:
Span
用于创建子 span 和添加 metadata 的当前 span。可在执行期间用它创建自定义子 span 或更新 span 属性。
tracingOptions?:
TracingOptions
Tracing 配置选项。
metadata?:
Record<string, any>
要添加到根 trace span 的 metadata。可用于添加用户 ID、会话 ID 或 feature flag 等自定义属性。
requestContextKeys?:
string[]
要提取为此 trace 的 metadata 的其他 RequestContext 键。支持使用点表示法访问嵌套值(例如 'user.id')。
traceId?:
string
此次执行使用的 trace ID(1-32 个十六进制字符)。提供后,此 trace 将归入指定的 trace。
parentSpanId?:
string
此次执行使用的父 span ID(1-16 个十六进制字符)。提供后,根 span 将创建为该 span 的子 span。
tags?:
string[]
应用于此 trace 的标签,即用于对 trace 进行分类和筛选的字符串标签。
outputOptions?:
OutputOptions
输出配置选项。
includeState?:
boolean
是否在结果中包含 workflow run 状态。
返回值返回值的直接链接
result:
Promise<WorkflowResult<TState, TOutput, TSteps>>
解析为 workflow 执行结果的 promise,其中包含步骤输出和状态
traceId?:
string
启用 Tracing 时与此次执行关联的 trace ID。可用它关联日志并调试执行流程。
spanId?:
string
启用 Tracing 时与此次执行关联的根 span ID。可用于 span 级别的查找和关联。
扩展使用示例扩展使用示例的直接链接
if (result.status === 'suspended') {
const resumedResults = await run.resume({
step: result.suspended[0],
resumeData: { value: 'resume data' },
})
}
备注
仅有一个步骤暂停时,可以省略 step 参数,workflow 会自动恢复该步骤。对于有多个已暂停步骤的 workflow,必须明确指定要恢复的步骤。
恢复单次 .foreach() 迭代resuming-a-single-foreach-iteration的直接链接
当 .foreach() 步骤在多次迭代中暂停时,使用 forEachIndex(从零开始)逐次恢复迭代,并为每次迭代提供不同的 resumeData。未被 forEachIndex 指定的迭代会保持暂停,直至恢复。
// Resume only the second iteration (index 1) with its own data
await run.resume({
step: 'approve',
resumeData: { ok: true },
forEachIndex: 1,
})
// Later, resume the first iteration with different data
await run.resume({
step: 'approve',
resumeData: { ok: false },
forEachIndex: 0,
})
如果省略 forEachIndex,该步骤每个已暂停的迭代都会使用同一个 resumeData 恢复。