@barnum/barnum 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/linux-arm64/barnum +0 -0
- package/artifacts/linux-x64/barnum +0 -0
- package/artifacts/macos-arm64/barnum +0 -0
- package/artifacts/macos-x64/barnum +0 -0
- package/artifacts/win-x64/barnum.exe +0 -0
- package/cli.cjs +33 -0
- package/dist/all.d.ts +12 -0
- package/dist/all.js +8 -0
- package/dist/ast.d.ts +375 -0
- package/dist/ast.js +381 -0
- package/dist/bind.d.ts +62 -0
- package/dist/bind.js +106 -0
- package/dist/builtins.d.ts +257 -0
- package/dist/builtins.js +600 -0
- package/dist/chain.d.ts +2 -0
- package/dist/chain.js +8 -0
- package/dist/effect-id.d.ts +14 -0
- package/dist/effect-id.js +16 -0
- package/dist/handler.d.ts +50 -0
- package/dist/handler.js +146 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +5 -0
- package/dist/pipe.d.ts +11 -0
- package/dist/pipe.js +11 -0
- package/dist/race.d.ts +53 -0
- package/dist/race.js +141 -0
- package/dist/recursive.d.ts +34 -0
- package/dist/recursive.js +53 -0
- package/dist/run.d.ts +7 -0
- package/dist/run.js +143 -0
- package/dist/schema.d.ts +8 -0
- package/dist/schema.js +95 -0
- package/dist/try-catch.d.ts +23 -0
- package/dist/try-catch.js +36 -0
- package/dist/worker.d.ts +11 -0
- package/dist/worker.js +46 -0
- package/package.json +40 -16
- package/src/all.ts +89 -0
- package/src/ast.ts +878 -0
- package/src/bind.ts +192 -0
- package/src/builtins.ts +804 -0
- package/src/chain.ts +17 -0
- package/src/effect-id.ts +30 -0
- package/src/handler.ts +279 -0
- package/src/index.ts +30 -0
- package/src/pipe.ts +93 -0
- package/src/race.ts +183 -0
- package/src/recursive.ts +112 -0
- package/src/run.ts +181 -0
- package/src/schema.ts +118 -0
- package/src/try-catch.ts +53 -0
- package/src/worker.ts +56 -0
- package/README.md +0 -19
- package/barnum-config-schema.json +0 -408
- package/cli.js +0 -20
- package/index.js +0 -23
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/cli.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const { execFileSync } = require("child_process");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const os = require("os");
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
|
|
9
|
+
const platform = os.platform();
|
|
10
|
+
const arch = os.arch();
|
|
11
|
+
|
|
12
|
+
let binaryName = "barnum";
|
|
13
|
+
let artifactDir;
|
|
14
|
+
|
|
15
|
+
if (platform === "darwin" && arch === "arm64") artifactDir = "macos-arm64";
|
|
16
|
+
else if (platform === "darwin") artifactDir = "macos-x64";
|
|
17
|
+
else if (platform === "linux" && arch === "arm64") artifactDir = "linux-arm64";
|
|
18
|
+
else if (platform === "linux") artifactDir = "linux-x64";
|
|
19
|
+
else if (platform === "win32") { artifactDir = "win-x64"; binaryName = "barnum.exe"; }
|
|
20
|
+
else { console.error(`Unsupported platform: ${platform}-${arch}`); process.exit(1); }
|
|
21
|
+
|
|
22
|
+
const binaryPath = path.join(__dirname, "artifacts", artifactDir, binaryName);
|
|
23
|
+
|
|
24
|
+
if (!fs.existsSync(binaryPath)) {
|
|
25
|
+
console.error(`Binary not found: ${binaryPath}`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
execFileSync(binaryPath, process.argv.slice(2), { stdio: "inherit" });
|
|
31
|
+
} catch (e) {
|
|
32
|
+
process.exit(e.status || 1);
|
|
33
|
+
}
|
package/dist/all.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Pipeable, type TypedAction } from "./ast.js";
|
|
2
|
+
export declare function all(): TypedAction<any, []>;
|
|
3
|
+
export declare function all<In, O1>(a1: Pipeable<In, O1>): TypedAction<In, [O1]>;
|
|
4
|
+
export declare function all<In, O1, O2>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>): TypedAction<In, [O1, O2]>;
|
|
5
|
+
export declare function all<In, O1, O2, O3>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>): TypedAction<In, [O1, O2, O3]>;
|
|
6
|
+
export declare function all<In, O1, O2, O3, O4>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>): TypedAction<In, [O1, O2, O3, O4]>;
|
|
7
|
+
export declare function all<In, O1, O2, O3, O4, O5>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>): TypedAction<In, [O1, O2, O3, O4, O5]>;
|
|
8
|
+
export declare function all<In, O1, O2, O3, O4, O5, O6>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>, a6: Pipeable<In, O6>): TypedAction<In, [O1, O2, O3, O4, O5, O6]>;
|
|
9
|
+
export declare function all<In, O1, O2, O3, O4, O5, O6, O7>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>, a6: Pipeable<In, O6>, a7: Pipeable<In, O7>): TypedAction<In, [O1, O2, O3, O4, O5, O6, O7]>;
|
|
10
|
+
export declare function all<In, O1, O2, O3, O4, O5, O6, O7, O8>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>, a6: Pipeable<In, O6>, a7: Pipeable<In, O7>, a8: Pipeable<In, O8>): TypedAction<In, [O1, O2, O3, O4, O5, O6, O7, O8]>;
|
|
11
|
+
export declare function all<In, O1, O2, O3, O4, O5, O6, O7, O8, O9>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>, a6: Pipeable<In, O6>, a7: Pipeable<In, O7>, a8: Pipeable<In, O8>, a9: Pipeable<In, O9>): TypedAction<In, [O1, O2, O3, O4, O5, O6, O7, O8, O9]>;
|
|
12
|
+
export declare function all<In, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10>(a1: Pipeable<In, O1>, a2: Pipeable<In, O2>, a3: Pipeable<In, O3>, a4: Pipeable<In, O4>, a5: Pipeable<In, O5>, a6: Pipeable<In, O6>, a7: Pipeable<In, O7>, a8: Pipeable<In, O8>, a9: Pipeable<In, O9>, a10: Pipeable<In, O10>): TypedAction<In, [O1, O2, O3, O4, O5, O6, O7, O8, O9, O10]>;
|
package/dist/all.js
ADDED
package/dist/ast.d.ts
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import type { JSONSchema7 } from "json-schema";
|
|
2
|
+
export type Action = InvokeAction | ChainAction | ForEachAction | AllAction | BranchAction | ResumeHandleAction | ResumePerformAction | RestartHandleAction | RestartPerformAction;
|
|
3
|
+
export interface InvokeAction {
|
|
4
|
+
kind: "Invoke";
|
|
5
|
+
handler: HandlerKind;
|
|
6
|
+
}
|
|
7
|
+
export interface ChainAction {
|
|
8
|
+
kind: "Chain";
|
|
9
|
+
first: Action;
|
|
10
|
+
rest: Action;
|
|
11
|
+
}
|
|
12
|
+
export interface ForEachAction {
|
|
13
|
+
kind: "ForEach";
|
|
14
|
+
action: Action;
|
|
15
|
+
}
|
|
16
|
+
export interface AllAction {
|
|
17
|
+
kind: "All";
|
|
18
|
+
actions: Action[];
|
|
19
|
+
}
|
|
20
|
+
export interface BranchAction {
|
|
21
|
+
kind: "Branch";
|
|
22
|
+
cases: Record<string, Action>;
|
|
23
|
+
}
|
|
24
|
+
export interface ResumeHandleAction {
|
|
25
|
+
kind: "ResumeHandle";
|
|
26
|
+
resume_handler_id: ResumeHandlerId;
|
|
27
|
+
body: Action;
|
|
28
|
+
handler: Action;
|
|
29
|
+
}
|
|
30
|
+
export interface ResumePerformAction {
|
|
31
|
+
kind: "ResumePerform";
|
|
32
|
+
resume_handler_id: ResumeHandlerId;
|
|
33
|
+
}
|
|
34
|
+
export interface RestartHandleAction {
|
|
35
|
+
kind: "RestartHandle";
|
|
36
|
+
restart_handler_id: RestartHandlerId;
|
|
37
|
+
body: Action;
|
|
38
|
+
handler: Action;
|
|
39
|
+
}
|
|
40
|
+
export interface RestartPerformAction {
|
|
41
|
+
kind: "RestartPerform";
|
|
42
|
+
restart_handler_id: RestartHandlerId;
|
|
43
|
+
}
|
|
44
|
+
export type HandlerKind = TypeScriptHandler | BuiltinHandler;
|
|
45
|
+
export interface TypeScriptHandler {
|
|
46
|
+
kind: "TypeScript";
|
|
47
|
+
module: string;
|
|
48
|
+
func: string;
|
|
49
|
+
input_schema?: JSONSchema7;
|
|
50
|
+
output_schema?: JSONSchema7;
|
|
51
|
+
}
|
|
52
|
+
export interface BuiltinHandler {
|
|
53
|
+
kind: "Builtin";
|
|
54
|
+
builtin: BuiltinKind;
|
|
55
|
+
}
|
|
56
|
+
export type BuiltinKind = {
|
|
57
|
+
kind: "Constant";
|
|
58
|
+
value: unknown;
|
|
59
|
+
} | {
|
|
60
|
+
kind: "Identity";
|
|
61
|
+
} | {
|
|
62
|
+
kind: "Drop";
|
|
63
|
+
} | {
|
|
64
|
+
kind: "Tag";
|
|
65
|
+
value: string;
|
|
66
|
+
} | {
|
|
67
|
+
kind: "Merge";
|
|
68
|
+
} | {
|
|
69
|
+
kind: "Flatten";
|
|
70
|
+
} | {
|
|
71
|
+
kind: "ExtractField";
|
|
72
|
+
value: string;
|
|
73
|
+
} | {
|
|
74
|
+
kind: "ExtractIndex";
|
|
75
|
+
value: number;
|
|
76
|
+
} | {
|
|
77
|
+
kind: "Pick";
|
|
78
|
+
value: string[];
|
|
79
|
+
} | {
|
|
80
|
+
kind: "CollectSome";
|
|
81
|
+
} | {
|
|
82
|
+
kind: "Sleep";
|
|
83
|
+
value: number;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* When TIn is `never` (handler ignores input), produce `any` so the
|
|
87
|
+
* combinator/pipe can sit in any pipeline position.
|
|
88
|
+
*/
|
|
89
|
+
export type PipeIn<T> = [T] extends [never] ? any : T;
|
|
90
|
+
export interface Config {
|
|
91
|
+
workflow: Action;
|
|
92
|
+
}
|
|
93
|
+
type UnionToIntersection<TUnion> = (TUnion extends any ? (x: TUnion) => void : never) extends (x: infer TIntersection) => void ? TIntersection : never;
|
|
94
|
+
/** Merge a tuple of objects into a single intersection type. */
|
|
95
|
+
export type MergeTuple<TTuple> = TTuple extends unknown[] ? UnionToIntersection<TTuple[number]> : never;
|
|
96
|
+
/**
|
|
97
|
+
* An action with tracked input/output types. Phantom fields enforce invariance
|
|
98
|
+
* and are never set at runtime — they exist only for the TypeScript compiler.
|
|
99
|
+
*
|
|
100
|
+
* Each type variable gets a contravariant + covariant field pair:
|
|
101
|
+
* In: __in (contravariant) + __in_co (covariant) → invariant
|
|
102
|
+
* Out: __out (covariant) + __out_contra (contravariant) → invariant
|
|
103
|
+
*
|
|
104
|
+
* This ensures exact type matching at every pipeline connection point.
|
|
105
|
+
* Data crosses serialization boundaries to handlers in arbitrary languages
|
|
106
|
+
* (Rust, Python, etc.), so extra/missing fields are runtime errors.
|
|
107
|
+
*/
|
|
108
|
+
export type TypedAction<In = unknown, Out = unknown, Refs extends string = never> = Action & {
|
|
109
|
+
__in?: (input: In) => void;
|
|
110
|
+
__in_co?: In;
|
|
111
|
+
__out?: () => Out;
|
|
112
|
+
__out_contra?: (output: Out) => void;
|
|
113
|
+
__refs?: {
|
|
114
|
+
_brand: Refs;
|
|
115
|
+
};
|
|
116
|
+
/** Chain this action with another. `a.then(b)` ≡ `chain(a, b)`. */
|
|
117
|
+
then<TNext>(next: Pipeable<Out, TNext>): TypedAction<In, TNext, Refs>;
|
|
118
|
+
/** Apply an action to each element of an array output. `a.forEach(b)` ≡ `a.then(forEach(b))`. */
|
|
119
|
+
forEach<TIn, TElement, TNext, TRefs extends string>(this: TypedAction<TIn, TElement[], TRefs>, action: Pipeable<TElement, TNext>): TypedAction<TIn, TNext[], TRefs>;
|
|
120
|
+
/** Dispatch on a tagged union output. Auto-unwraps `value` before each case handler. */
|
|
121
|
+
branch<TCases extends {
|
|
122
|
+
[K in BranchKeys<Out>]: CaseHandler<BranchPayload<Out, K>, unknown>;
|
|
123
|
+
}>(cases: [BranchKeys<Out>] extends [never] ? never : TCases): TypedAction<In, ExtractOutput<TCases[keyof TCases & string]>, Refs>;
|
|
124
|
+
/** Flatten a nested array output. `a.flatten()` ≡ `pipe(a, flatten())`. */
|
|
125
|
+
flatten(): TypedAction<In, Out extends (infer TElement)[][] ? TElement[] : Out, Refs>;
|
|
126
|
+
/** Discard output. `a.drop()` ≡ `pipe(a, drop)`. */
|
|
127
|
+
drop(): TypedAction<In, never, Refs>;
|
|
128
|
+
/** Wrap output as a tagged union member. Requires full variant map TDef so __def is carried. */
|
|
129
|
+
tag<TDef extends Record<string, unknown>, TKind extends keyof TDef & string>(kind: TKind): TypedAction<In, TaggedUnion<TDef>, Refs>;
|
|
130
|
+
/** Extract a field from the output object. `a.get("name")` ≡ `pipe(a, extractField("name"))`. */
|
|
131
|
+
get<TField extends keyof Out & string>(field: TField): TypedAction<In, Out[TField], Refs>;
|
|
132
|
+
/**
|
|
133
|
+
* Run this sub-pipeline, then merge its output back into the original input.
|
|
134
|
+
* `pipe(extractField("x"), transform).augment()` takes `In`, runs the
|
|
135
|
+
* sub-pipeline to get `Out`, and returns `In & Out`.
|
|
136
|
+
*
|
|
137
|
+
* Unlike the standalone `augment()` function, the postfix form has access
|
|
138
|
+
* to `In` so the intersection types correctly.
|
|
139
|
+
*/
|
|
140
|
+
augment(): TypedAction<In, In & Out, Refs>;
|
|
141
|
+
/** Merge a tuple of objects into a single object. `a.merge()` ≡ `pipe(a, merge())`. */
|
|
142
|
+
merge(): TypedAction<In, MergeTuple<Out>, Refs>;
|
|
143
|
+
/** Select fields from the output. `a.pick("x", "y")` ≡ `pipe(a, pick("x", "y"))`. */
|
|
144
|
+
pick<TKeys extends (keyof Out & string)[]>(...keys: TKeys): TypedAction<In, Pick<Out, TKeys[number]>, Refs>;
|
|
145
|
+
/**
|
|
146
|
+
* Transform the Some value inside an Option output. Only callable when
|
|
147
|
+
* Out is Option<T>. Uses `this` parameter constraint to gate availability.
|
|
148
|
+
*/
|
|
149
|
+
mapOption<TIn, T, U, TRefs extends string>(this: TypedAction<TIn, Option<T>, TRefs>, action: Pipeable<T, U>): TypedAction<TIn, Option<U>, TRefs>;
|
|
150
|
+
/**
|
|
151
|
+
* Transform the Err value of a Result output.
|
|
152
|
+
* `Result<TValue, TError> → Result<TValue, TErrorOut>`
|
|
153
|
+
*
|
|
154
|
+
* Only callable when Out is Result<TValue, TError>.
|
|
155
|
+
*/
|
|
156
|
+
mapErr<TIn, TValue, TError, TErrorOut>(this: TypedAction<TIn, Result<TValue, TError>, any>, action: Pipeable<TError, TErrorOut>): TypedAction<TIn, Result<TValue, TErrorOut>, Refs>;
|
|
157
|
+
/**
|
|
158
|
+
* Unwrap a Result output. If Ok, pass through the value. If Err, apply
|
|
159
|
+
* the default action. Only callable when Out is Result<TValue, TError>.
|
|
160
|
+
*
|
|
161
|
+
* The `this` constraint provides TValue from context, so throw tokens
|
|
162
|
+
* (Out=never) work without explicit type parameters:
|
|
163
|
+
* `handler.unwrapOr(throwError)`
|
|
164
|
+
*
|
|
165
|
+
* Uses CaseHandler for defaultAction (covariant output only) so that
|
|
166
|
+
* `TypedAction<TError, never>` is assignable to `CaseHandler<TError, TValue>`.
|
|
167
|
+
*
|
|
168
|
+
* Refs position uses `any` in the `this` constraint to avoid TS
|
|
169
|
+
* falling back to the constraint bound `string` when Refs = never.
|
|
170
|
+
* The return type uses the enclosing TypedAction's `Refs` directly.
|
|
171
|
+
*/
|
|
172
|
+
unwrapOr<TIn, TValue, TError>(this: TypedAction<TIn, Result<TValue, TError>, any>, defaultAction: CaseHandler<TError, TValue>): TypedAction<TIn, TValue, Refs>;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Parameter type for pipe and combinators. Contains the same phantom fields
|
|
176
|
+
* as TypedAction but without methods.
|
|
177
|
+
*
|
|
178
|
+
* Invariance: Both In and Out are invariant, matching TypedAction:
|
|
179
|
+
* In: __in (contravariant) + __in_co (covariant) → invariant
|
|
180
|
+
* Out: __out (covariant) + __out_contra (contravariant) → invariant
|
|
181
|
+
*
|
|
182
|
+
* Why no methods: TypedAction's methods (then, branch, etc.) participate in
|
|
183
|
+
* TS assignability checks in complex, recursive ways that interfere with
|
|
184
|
+
* generic inference in pipe overloads. Pipeable strips methods so that only
|
|
185
|
+
* phantom fields drive inference — predictable covariant/contravariant
|
|
186
|
+
* resolution, with invariance enforced when TS checks candidates from
|
|
187
|
+
* both sides of a connection.
|
|
188
|
+
*
|
|
189
|
+
* TypedAction (with methods) is assignable to Pipeable because Pipeable
|
|
190
|
+
* only requires a subset of properties.
|
|
191
|
+
*/
|
|
192
|
+
export type Pipeable<In = unknown, Out = unknown> = Action & {
|
|
193
|
+
__in?: (input: In) => void;
|
|
194
|
+
__in_co?: In;
|
|
195
|
+
__out?: () => Out;
|
|
196
|
+
__out_contra?: (output: Out) => void;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Contravariant-only input checking for branch case handler positions.
|
|
200
|
+
*
|
|
201
|
+
* Omits __in_co (covariant input) and __out_contra (contravariant output)
|
|
202
|
+
* compared to TypedAction/Pipeable. This gives:
|
|
203
|
+
* In: contravariant only (via __in)
|
|
204
|
+
* Out: covariant only (via __out)
|
|
205
|
+
*
|
|
206
|
+
* Why contravariant input: a handler that accepts `unknown` (like drop)
|
|
207
|
+
* can handle any variant. (input: unknown) => void is assignable to
|
|
208
|
+
* (input: HasErrors) => void because HasErrors extends unknown.
|
|
209
|
+
*
|
|
210
|
+
* Why covariant output: the constraint doesn't restrict output types —
|
|
211
|
+
* they're inferred from the actual case handlers via ExtractOutput.
|
|
212
|
+
* TypedAction's invariant __out_contra with Out=unknown would
|
|
213
|
+
* reject any handler with a specific output type, so we omit it.
|
|
214
|
+
*
|
|
215
|
+
* TypedAction is assignable to CaseHandler because CaseHandler only
|
|
216
|
+
* requires a subset of TypedAction's phantom fields.
|
|
217
|
+
*/
|
|
218
|
+
type CaseHandler<TIn = unknown, TOut = unknown> = Action & {
|
|
219
|
+
__in?: (input: TIn) => void;
|
|
220
|
+
__out?: () => TOut;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Standard tagged union type. Each variant is `{ kind: K; value: TDef[K] }`
|
|
224
|
+
* with a phantom `__def` field carrying the full variant map. The __def
|
|
225
|
+
* field enables `.branch()` to decompose the union via simple indexing
|
|
226
|
+
* (`keyof ExtractDef<Out>` and `ExtractDef<Out>[K]`) instead of
|
|
227
|
+
* conditional types (`KindOf<Out>` and `Extract<Out, { kind: K }>`).
|
|
228
|
+
*/
|
|
229
|
+
type VoidToNull<T> = 0 extends 1 & T ? T : [T] extends [never] ? never : [T] extends [void] ? null : T;
|
|
230
|
+
export type TaggedUnion<TDef extends Record<string, unknown>> = {
|
|
231
|
+
[K in keyof TDef & string]: {
|
|
232
|
+
kind: K;
|
|
233
|
+
value: VoidToNull<TDef[K]>;
|
|
234
|
+
__def?: TDef;
|
|
235
|
+
};
|
|
236
|
+
}[keyof TDef & string];
|
|
237
|
+
/** Extract the variant map definition from a tagged union's phantom __def. */
|
|
238
|
+
export type ExtractDef<T> = T extends {
|
|
239
|
+
__def?: infer D;
|
|
240
|
+
} ? D : never;
|
|
241
|
+
export type OptionDef<T> = {
|
|
242
|
+
Some: T;
|
|
243
|
+
None: void;
|
|
244
|
+
};
|
|
245
|
+
export type Option<T> = TaggedUnion<OptionDef<T>>;
|
|
246
|
+
export type ResultDef<TValue, TError> = {
|
|
247
|
+
Ok: TValue;
|
|
248
|
+
Err: TError;
|
|
249
|
+
};
|
|
250
|
+
export type Result<TValue, TError> = TaggedUnion<ResultDef<TValue, TError>>;
|
|
251
|
+
/** Extract all `kind` string literals from a discriminated union. */
|
|
252
|
+
type KindOf<T> = T extends {
|
|
253
|
+
kind: infer K extends string;
|
|
254
|
+
} ? K : never;
|
|
255
|
+
/** Extract the `value` field from a `{ kind, value }` variant. Falls back to T if no `value` field. */
|
|
256
|
+
type UnwrapVariant<T> = T extends {
|
|
257
|
+
value: infer V;
|
|
258
|
+
} ? V : T;
|
|
259
|
+
/**
|
|
260
|
+
* Branch case keys: prefer ExtractDef (simple keyof indexing) when the
|
|
261
|
+
* output carries __def. Falls back to KindOf (conditional type) for
|
|
262
|
+
* outputs without __def.
|
|
263
|
+
*/
|
|
264
|
+
type BranchKeys<Out> = [ExtractDef<Out>] extends [never] ? KindOf<Out> : keyof ExtractDef<Out> & string;
|
|
265
|
+
/**
|
|
266
|
+
* Branch case payload: prefer ExtractDef[K] (simple indexing) when available.
|
|
267
|
+
* Falls back to UnwrapVariant<Extract<Out, { kind: K }>> for outputs without __def.
|
|
268
|
+
*/
|
|
269
|
+
type BranchPayload<Out, K extends string> = [ExtractDef<Out>] extends [never] ? UnwrapVariant<Extract<Out, {
|
|
270
|
+
kind: K;
|
|
271
|
+
}>> : K extends keyof ExtractDef<Out> ? VoidToNull<ExtractDef<Out>[K]> : never;
|
|
272
|
+
/**
|
|
273
|
+
* Attach `.then()` and `.forEach()` methods to a plain Action object.
|
|
274
|
+
* Methods are non-enumerable: invisible to JSON.stringify and toEqual.
|
|
275
|
+
*/
|
|
276
|
+
export declare function typedAction<In = unknown, Out = unknown, Refs extends string = never>(action: Action): TypedAction<In, Out, Refs>;
|
|
277
|
+
/**
|
|
278
|
+
* Extract the input type from a TypedAction.
|
|
279
|
+
*
|
|
280
|
+
* Uses direct phantom field extraction (not full TypedAction matching) to
|
|
281
|
+
* avoid the `TypedAction<any, any, any>` constraint which fails for In=never
|
|
282
|
+
* due to __in contravariance.
|
|
283
|
+
*/
|
|
284
|
+
export type ExtractInput<T> = T extends {
|
|
285
|
+
__in?: (input: infer In) => void;
|
|
286
|
+
} ? In : never;
|
|
287
|
+
/**
|
|
288
|
+
* Extract the output type from a TypedAction.
|
|
289
|
+
*
|
|
290
|
+
* Uses direct phantom field extraction to avoid constraint issues.
|
|
291
|
+
*/
|
|
292
|
+
export type ExtractOutput<T> = T extends {
|
|
293
|
+
__out?: () => infer Out;
|
|
294
|
+
} ? Out : never;
|
|
295
|
+
export { pipe } from "./pipe.js";
|
|
296
|
+
export { chain } from "./chain.js";
|
|
297
|
+
export { all } from "./all.js";
|
|
298
|
+
export { bind, bindInput, type VarRef, type InferVarRefs } from "./bind.js";
|
|
299
|
+
export { defineRecursiveFunctions } from "./recursive.js";
|
|
300
|
+
export { resetEffectIdCounter } from "./effect-id.js";
|
|
301
|
+
import { type RestartHandlerId, type ResumeHandlerId } from "./effect-id.js";
|
|
302
|
+
export { tryCatch } from "./try-catch.js";
|
|
303
|
+
export { race, sleep, withTimeout } from "./race.js";
|
|
304
|
+
export declare function forEach<In, Out>(action: Pipeable<In, Out>): TypedAction<In[], Out[]>;
|
|
305
|
+
/**
|
|
306
|
+
* Compute the branch input type from its cases. For each case key K,
|
|
307
|
+
* wraps the case handler's input type in `{ kind: K; value: T }`.
|
|
308
|
+
* This ensures the branch input is a proper tagged union matching the
|
|
309
|
+
* `{ kind, value }` convention.
|
|
310
|
+
*
|
|
311
|
+
* Example: `BranchInput<{ Yes: TypedAction<number, ...>, No: TypedAction<string, ...> }>`
|
|
312
|
+
* = `{ kind: "Yes"; value: number } | { kind: "No"; value: string }`
|
|
313
|
+
*
|
|
314
|
+
* When a case handler uses `any` as input, the wrapping produces
|
|
315
|
+
* `{ kind: K; value: any }`, which is the correct escape hatch.
|
|
316
|
+
*/
|
|
317
|
+
export type BranchInput<TCases> = {
|
|
318
|
+
[K in keyof TCases & string]: {
|
|
319
|
+
kind: K;
|
|
320
|
+
value: ExtractInput<TCases[K]>;
|
|
321
|
+
};
|
|
322
|
+
}[keyof TCases & string];
|
|
323
|
+
export declare function branch<TCases extends Record<string, Action>>(cases: TCases): TypedAction<BranchInput<TCases>, ExtractOutput<TCases[keyof TCases & string]>>;
|
|
324
|
+
type LoopResultDef<TContinue, TBreak> = {
|
|
325
|
+
Continue: TContinue;
|
|
326
|
+
Break: TBreak;
|
|
327
|
+
};
|
|
328
|
+
export type LoopResult<TContinue, TBreak> = TaggedUnion<LoopResultDef<TContinue, TBreak>>;
|
|
329
|
+
export declare const TAG_BREAK: Action;
|
|
330
|
+
export declare const IDENTITY: Action;
|
|
331
|
+
/**
|
|
332
|
+
* Restartable scope. The body callback receives `restart`, a TypedAction that
|
|
333
|
+
* re-executes the body from the beginning with a new input value.
|
|
334
|
+
*
|
|
335
|
+
* If the body completes normally → output is TOut.
|
|
336
|
+
* If restart fires → body re-executes with the restarted value.
|
|
337
|
+
*
|
|
338
|
+
* Compiled form: `RestartHandle(id, ExtractIndex(0), body)`
|
|
339
|
+
*/
|
|
340
|
+
export declare function recur<TIn = never, TOut = any>(bodyFn: (restart: TypedAction<TIn, never>) => Pipeable<TIn, TOut>): TypedAction<PipeIn<TIn>, TOut>;
|
|
341
|
+
/**
|
|
342
|
+
* Early return scope. The body callback receives `earlyReturn`, a TypedAction
|
|
343
|
+
* that exits the scope immediately with the returned value.
|
|
344
|
+
*
|
|
345
|
+
* If the body completes normally → output is TOut.
|
|
346
|
+
* If earlyReturn fires → output is TEarlyReturn.
|
|
347
|
+
* Combined output: TEarlyReturn | TOut.
|
|
348
|
+
*
|
|
349
|
+
* Built on the restart mechanism: input is tagged Continue, body runs inside
|
|
350
|
+
* a Branch. earlyReturn tags with Break and performs — the handler restarts
|
|
351
|
+
* the body, Branch takes the Break path, and the value exits.
|
|
352
|
+
*/
|
|
353
|
+
export declare function earlyReturn<TEarlyReturn = never, TIn = any, TOut = any>(bodyFn: (earlyReturn: TypedAction<TEarlyReturn, never>) => Pipeable<TIn, TOut>): TypedAction<TIn, TEarlyReturn | TOut>;
|
|
354
|
+
/**
|
|
355
|
+
* Build the restart+branch compiled form:
|
|
356
|
+
* `Chain(Tag("Continue"), RestartHandle(id, ExtractIndex(0), Branch({ Continue: continueArm, Break: breakArm })))`
|
|
357
|
+
*
|
|
358
|
+
* Input is tagged Continue so the Branch enters the continueArm on first execution.
|
|
359
|
+
* Continue tag → restart → re-enters continueArm. Break tag → restart → runs breakArm, exits `RestartHandle`.
|
|
360
|
+
*
|
|
361
|
+
* Used by earlyReturn, loop, tryCatch, and race.
|
|
362
|
+
*/
|
|
363
|
+
export declare function buildRestartBranchAction(restartHandlerId: RestartHandlerId, continueArm: Action, breakArm: Action): Action;
|
|
364
|
+
/**
|
|
365
|
+
* Iterative loop. The body callback receives `recur` and `done`:
|
|
366
|
+
* - `recur`: restart the loop with a new input
|
|
367
|
+
* - `done`: exit the loop with the break value
|
|
368
|
+
*
|
|
369
|
+
* Both are TypedAction values (not functions), consistent with throwError in tryCatch.
|
|
370
|
+
*
|
|
371
|
+
* Compiles to `RestartHandle`/`RestartPerform`/Branch — same effect substrate as tryCatch and earlyReturn.
|
|
372
|
+
*/
|
|
373
|
+
export declare function loop<TBreak = never, TIn = never>(bodyFn: (recur: TypedAction<TIn, never>, done: TypedAction<VoidToNull<TBreak>, never>) => Pipeable<TIn, never>): TypedAction<PipeIn<TIn>, VoidToNull<TBreak>>;
|
|
374
|
+
/** Simple config factory. */
|
|
375
|
+
export declare function config(workflow: Action): Config;
|