import type { Step, StepRun, StepScope } from './step.js' import { buildStepRun, roundTripStepResult } from './step.js' export type StepExecutor = (run: StepRun, handler: () => T | Promise) => Promise export type InlineStepOptions = { scope?: StepScope execute?: StepExecutor } export function createInlineStep(options: InlineStepOptions = {}): Step { const occurrences = new Map() const execute = options.execute ?? inlineExecute return { async run(id, handler) { const run = buildStepRun(id, options.scope, nextOccurrence(id, options.scope, occurrences)) return execute(run, handler) }, } } async function inlineExecute(run: StepRun, handler: () => T | Promise): Promise { const result = await handler() return roundTripStepResult(run.key, result) } function nextOccurrence( id: string, scope: StepScope | undefined, occurrences: Map, ): number { const run = buildStepRun(id, scope, 0) const occurrence = occurrences.get(run.key) ?? 0 occurrences.set(run.key, occurrence + 1) return occurrence }