运行.timeTravel()
🌐 Run.timeTravel()
.timeTravel() 方法可以从任意特定步骤重新执行工作流,使用存储的快照数据或你提供的自定义上下文。这对于调试失败的工作流、使用不同输入测试单个步骤或在不重新运行整个工作流的情况下从错误中恢复非常有用。
🌐 The .timeTravel() method re-executes a workflow starting from any specific step, using either stored snapshot data or custom context you provide. This is useful for debugging failed workflows, testing individual steps with different inputs, or recovering from errors without re-running the entire workflow.
使用示例Direct link to 使用示例
🌐 Usage example
const run = await workflow.createRun();
const result = await run.timeTravel({
step: "step2",
inputData: { value: 10 },
});
参数Direct link to 参数
🌐 Parameters
step:
Step<string, any, TInputSchema, any, any, any, TEngineType> | [...Step<string, any, any, any, any, any, TEngineType>[], Step<string, any, TInputSchema, any, any, any, TEngineType>] | string | string[]
The target step to start execution from. It can be a Step instance, array of Steps (for nested workflows), step ID string, or array of step ID strings. Use dot notation or arrays for nested workflow steps (e.g., 'nestedWorkflow.step3' or ['nestedWorkflow', 'step3'])
inputData?:
z.infer<TInputSchema>
Input data for the target step. Must match the step's input schema. If not provided, uses data from the workflow snapshot
resumeData?:
any
Resume data to provide if the workflow was previously suspended
initialState?:
z.infer<TState>
Initial state to set for the workflow run. Used to set workflow-level state before execution
context?:
TimeTravelContext<any, any, any, any>
Execution context containing step results for steps before the target step. Each key is a step ID with a StepResult object containing status, payload, output, startedAt, endedAt, suspendPayload, and resumePayload
nestedStepsContext?:
Record<string, TimeTravelContext<any, any, any, any>>
Context for nested workflow steps. Keyed by nested workflow ID, each containing step results for that nested workflow
requestContext?:
RequestContext
Request Context data to use during time travel execution
outputWriter?:
(chunk: TOutput) => Promise<void>
Optional asynchronous function to handle output chunks as they are produced
tracingContext?:
TracingContext
Tracing context for creating child spans and adding metadata. Automatically injected when using Mastra's tracing system.
currentSpan?:
Span
Current span for creating child spans and adding metadata. Use this to create custom child spans or update span attributes during execution.
tracingOptions?:
TracingOptions
Options for Tracing configuration.
metadata?:
Record<string, any>
Metadata to add to the root trace span. Useful for adding custom attributes like user IDs, session IDs, or feature flags.
requestContextKeys?:
string[]
Additional RequestContext keys to extract as metadata for this trace. Supports dot notation for nested values (e.g., 'user.id').
traceId?:
string
Trace ID to use for this execution (1-32 hexadecimal characters). If provided, this trace will be part of the specified trace.
parentSpanId?:
string
Parent span ID to use for this execution (1-16 hexadecimal characters). If provided, the root span will be created as a child of this span.
tags?:
string[]
Tags to apply to this trace. String labels for categorizing and filtering traces.
outputOptions?:
OutputOptions
Options for output configuration.
includeState?:
boolean
Whether to include the workflow run state in the result.
includeResumeLabels?:
boolean
Whether to include resume labels in the result.
返回Direct link to 返回
🌐 Returns
result:
Promise<WorkflowResult<TState, TInput, TOutput, TSteps>>
A promise that resolves to the workflow execution result containing step outputs and status
traceId?:
string
The trace ID associated with this execution when Tracing is enabled. Use this to correlate logs and debug execution flow.
扩展使用示例Direct link to 扩展使用示例
🌐 Extended usage examples
带有自定义上下文的时间旅行Direct link to 带有自定义上下文的时间旅行
🌐 Time travel with custom context
const result = await run.timeTravel({
step: "step2",
context: {
step1: {
status: "success",
payload: { value: 0 },
output: { step1Result: 2 },
startedAt: Date.now(),
endedAt: Date.now(),
},
},
});
时间旅行到嵌套工作流步骤Direct link to 时间旅行到嵌套工作流步骤
🌐 Time travel to nested workflow step
// Using dot notation
const result = await run.timeTravel({
step: "nestedWorkflow.step3",
inputData: { value: 10 },
});
// Using array of step IDs
const result = await run.timeTravel({
step: ["nestedWorkflow", "step3"],
inputData: { value: 10 },
});
具有初始状态的时间旅行Direct link to 具有初始状态的时间旅行
🌐 Time travel with initial state
const result = await run.timeTravel({
step: "step2",
inputData: { value: 10 },
initialState: {
counter: 5,
metadata: { source: "time-travel" },
},
});
带有嵌套工作流上下文的时间旅行Direct link to 带有嵌套工作流上下文的时间旅行
🌐 Time travel with nested workflows context
const result = await run.timeTravel({
step: "nestedWorkflow.step3",
context: {
step1: {
status: "success",
payload: { value: 0 },
output: { step1Result: 2 },
startedAt: Date.now(),
endedAt: Date.now(),
},
nestedWorkflow: {
status: "running",
payload: { step1Result: 2 },
startedAt: Date.now(),
},
},
nestedStepsContext: {
nestedWorkflow: {
step2: {
status: "success",
payload: { step1Result: 2 },
output: { step2Result: 3 },
startedAt: Date.now(),
endedAt: Date.now(),
},
},
},
});
注意Direct link to 注意
🌐 Notes
- 时间旅行需要配置存储,因为它依赖于持久化的工作流快照
- 在重新执行工作流时,工作流会从存储中加载现有的快照(如果有的话)
- 目标步骤之前的步骤结果是从快照或提供的上下文中重建的
- 执行从指定步骤开始,使用提供的或重建的输入数据
- 从那时起,工作流程将继续直到完成
- 时间旅行可以用于尚未运行的工作流,通过提供自定义上下文或输入数据来让步骤从特定点开始。
相关Direct link to 相关
🌐 Related