Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { assertContiguousSteps, assertNoOverflowStep,} from './run-steps.ts';
import type { RunArgs, RunCallableArgs,} from './types.ts';
/** Applies synchronous pipeline steps with wide implementation types. Dispatches on the first absent `fnN` through an explicit if-chain of nested calls; no array indexing, no recursion. Argument validation runs before step execution so a gap or overflow logs under `invalid pipe arguments`, distinct from the `step failed` log reserved for a throwing step. @param args - value, contiguous step functions, and optional logger @returns final pipeline output @throws {@link PipeStepGapError} when step keys are not contiguous @throws {@link PipeStepOverflowError} when `fn10` is present @throws whatever pipeline step throws; the throw propagates unchanged @example ```ts const value = runPipe({ value: 1, fn1: (input) => input }); ``` */export function runPipe(args: RunArgs,): unknown { /** Logger tagged at the synchronous core boundary. */ const l = tagged(args.l === undefined ? { tag: runPipe.name, } : { tag: runPipe.name, l: args.l, },); l.debug('entry',);
/* oxlint-disable typescript/no-unsafe-type-assertion -- widen each step's `never` input to `unknown` for application; RunCallableArgs preserves `this: void`, so this is parameter-variance widening only and the typed overloads remain the type-safe public surface */ /** Callable arguments with step inputs widened for internal invocation. */ const callableArgs = args as RunCallableArgs; /* oxlint-enable typescript/no-unsafe-type-assertion */ /** Callable value and steps destructured so step functions are invoked without method-style `this`. */ const { value, fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, } = callableArgs; /** Ordered step slots used only for runtime contiguity validation. */ const steps = [ fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, ] as const;
try { assertNoOverflowStep(fn10,); assertContiguousSteps(steps,); } catch (error) { l.error(`invalid pipe arguments: ${String(error,)}`,); throw error; }
try { if (fn2 === undefined) { l.debug('1 step',); return fn1(value,); } if (fn3 === undefined) { l.debug('2 steps',); return fn2(fn1(value,),); } if (fn4 === undefined) { l.debug('3 steps',); return fn3( fn2(fn1(value,),), ); } if (fn5 === undefined) { l.debug('4 steps',); return fn4( fn3( fn2(fn1(value,),), ), ); } if (fn6 === undefined) { l.debug('5 steps',); return fn5( fn4( fn3( fn2(fn1(value,),), ), ), ); } if (fn7 === undefined) { l.debug('6 steps',); return fn6( fn5( fn4( fn3( fn2(fn1(value,),), ), ), ), ); } if (fn8 === undefined) { l.debug('7 steps',); return fn7( fn6( fn5( fn4( fn3( fn2(fn1(value,),), ), ), ), ), ); } if (fn9 === undefined) { l.debug('8 steps',); return fn8( fn7( fn6( fn5( fn4( fn3( fn2(fn1(value,),), ), ), ), ), ), ); }
l.debug('9 steps',); return fn9( fn8( fn7( fn6( fn5( fn4( fn3( fn2(fn1(value,),), ), ), ), ), ), ), ); } catch (error) { l.error(`step failed: ${String(error,)}`,); throw error; }}
/** Applies asynchronous pipeline steps with wide implementation types. Awaits the initial value first (so a `T | Promise<T>` is accepted), awaits every intermediate before the next step so steps run strictly sequentially, and awaits the final application so a last-step rejection stays inside the execution `try` and is logged before it propagates. Argument validation runs before execution under the `invalid pipe arguments` log. @param args - possibly promised value, contiguous step functions, and optional logger @returns promise resolving to final pipeline output @throws {@link PipeStepGapError} when step keys are not contiguous @throws {@link PipeStepOverflowError} when `fn10` is present @throws whatever pipeline step throws or rejects with; the failure propagates unchanged @example ```ts const value = await runPipeAsync({ value: Promise.resolve(1), fn1: (input) => input }); ``` */export async function runPipeAsync(args: RunArgs,): Promise<unknown> { /** Logger tagged at the asynchronous core boundary. */ const l = tagged(args.l === undefined ? { tag: runPipeAsync.name, } : { tag: runPipeAsync.name, l: args.l, },); l.debug('entry',);
/* oxlint-disable typescript/no-unsafe-type-assertion -- widen each step's `never` input to `unknown` for application; RunCallableArgs preserves `this: void`, so this is parameter-variance widening only and the typed overloads remain the type-safe public surface */ /** Callable arguments with step inputs widened for internal invocation. */ const callableArgs = args as RunCallableArgs; /* oxlint-enable typescript/no-unsafe-type-assertion */ /** Callable value and steps destructured so step functions are invoked without method-style `this`. */ const { value, fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, fn10, } = callableArgs; /** Ordered step slots used only for runtime contiguity validation. */ const steps = [ fn1, fn2, fn3, fn4, fn5, fn6, fn7, fn8, fn9, ] as const;
try { assertNoOverflowStep(fn10,); assertContiguousSteps(steps,); } catch (error) { l.error(`invalid pipe arguments: ${String(error,)}`,); throw error; }
try { if (fn2 === undefined) { l.debug('1 step',); return await fn1(await value,); } if (fn3 === undefined) { l.debug('2 steps',); return await fn2(await fn1(await value,),); } if (fn4 === undefined) { l.debug('3 steps',); return await fn3( await fn2(await fn1(await value,),), ); } if (fn5 === undefined) { l.debug('4 steps',); return await fn4( await fn3( await fn2(await fn1(await value,),), ), ); } if (fn6 === undefined) { l.debug('5 steps',); return await fn5( await fn4( await fn3( await fn2(await fn1(await value,),), ), ), ); } if (fn7 === undefined) { l.debug('6 steps',); return await fn6( await fn5( await fn4( await fn3( await fn2(await fn1(await value,),), ), ), ), ); } if (fn8 === undefined) { l.debug('7 steps',); return await fn7( await fn6( await fn5( await fn4( await fn3( await fn2(await fn1(await value,),), ), ), ), ), ); } if (fn9 === undefined) { l.debug('8 steps',); return await fn8( await fn7( await fn6( await fn5( await fn4( await fn3( await fn2(await fn1(await value,),), ), ), ), ), ), ); }
l.debug('9 steps',); return await fn9( await fn8( await fn7( await fn6( await fn5( await fn4( await fn3( await fn2(await fn1(await value,),), ), ), ), ), ), ), ); } catch (error) { l.error(`step failed: ${String(error,)}`,); throw error; }}