@ax-llm/ax 12.0.18 → 12.0.19

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/index.d.cts CHANGED
@@ -3602,7 +3602,7 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3602
3602
  private branchContext;
3603
3603
  constructor(signature?: NonNullable<ConstructorParameters<typeof AxSignature>[0]>);
3604
3604
  /**
3605
- * Declares a reusable computational node and its input/output signature.
3605
+ * Declares a reusable computational node using a signature string.
3606
3606
  * Returns a new AxFlow type that tracks this node in the TNodes registry.
3607
3607
  *
3608
3608
  * @param name - The name of the node
@@ -3620,6 +3620,33 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3620
3620
  [K in TName]: InferAxGen<TSig>;
3621
3621
  }, // Add new node to registry
3622
3622
  TState>;
3623
+ /**
3624
+ * Declares a reusable computational node using an existing AxGen instance.
3625
+ * This allows reusing pre-configured generators in the flow.
3626
+ *
3627
+ * @param name - The name of the node
3628
+ * @param axgenInstance - Existing AxGen instance to use for this node
3629
+ * @returns New AxFlow instance with updated TNodes type
3630
+ *
3631
+ * @example
3632
+ * ```typescript
3633
+ * const summarizer = new AxGen('text:string -> summary:string', { temperature: 0.1 })
3634
+ * flow.node('summarizer', summarizer)
3635
+ * ```
3636
+ */
3637
+ node<TName extends string, TGen extends AxGen<any, any>>(name: TName, axgenInstance: TGen): AxFlow<IN, OUT, TNodes & {
3638
+ [K in TName]: TGen;
3639
+ }, // Add new node to registry with exact type
3640
+ TState>;
3641
+ /**
3642
+ * Short alias for node() - supports both signature strings and AxGen instances
3643
+ */
3644
+ n<TName extends string, TSig extends string>(name: TName, signature: TSig, options?: Readonly<AxProgramForwardOptions>): AxFlow<IN, OUT, TNodes & {
3645
+ [K in TName]: InferAxGen<TSig>;
3646
+ }, TState>;
3647
+ n<TName extends string, TGen extends AxGen<any, any>>(name: TName, axgenInstance: TGen): AxFlow<IN, OUT, TNodes & {
3648
+ [K in TName]: TGen;
3649
+ }, TState>;
3623
3650
  /**
3624
3651
  * Applies a synchronous transformation to the state object.
3625
3652
  * Returns a new AxFlow type with the evolved state.
@@ -3633,6 +3660,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3633
3660
  * ```
3634
3661
  */
3635
3662
  map<TNewState extends AxFlowState>(transform: (state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;
3663
+ /**
3664
+ * Short alias for map()
3665
+ */
3666
+ m<TNewState extends AxFlowState>(transform: (state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;
3636
3667
  /**
3637
3668
  * Labels a step for later reference (useful for feedback loops).
3638
3669
  *
@@ -3646,6 +3677,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3646
3677
  * ```
3647
3678
  */
3648
3679
  label(label: string): this;
3680
+ /**
3681
+ * Short alias for label()
3682
+ */
3683
+ l(label: string): this;
3649
3684
  /**
3650
3685
  * Executes a previously defined node with full type safety.
3651
3686
  * The node name must exist in TNodes, and the mapping function is typed based on the node's signature.
@@ -3661,6 +3696,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3661
3696
  * ```
3662
3697
  */
3663
3698
  execute<TNodeName extends keyof TNodes & string>(nodeName: TNodeName, mapping: (state: TState) => GetGenIn<TNodes[TNodeName]>, dynamicContext?: AxFlowDynamicContext): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;
3699
+ /**
3700
+ * Short alias for execute()
3701
+ */
3702
+ e<TNodeName extends keyof TNodes & string>(nodeName: TNodeName, mapping: (state: TState) => GetGenIn<TNodes[TNodeName]>, dynamicContext?: AxFlowDynamicContext): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;
3664
3703
  /**
3665
3704
  * Starts a conditional branch based on a predicate function.
3666
3705
  *
@@ -3678,6 +3717,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3678
3717
  * ```
3679
3718
  */
3680
3719
  branch(predicate: (state: TState) => unknown): this;
3720
+ /**
3721
+ * Short alias for branch()
3722
+ */
3723
+ b(predicate: (state: TState) => unknown): this;
3681
3724
  /**
3682
3725
  * Defines a branch case for the current branch context.
3683
3726
  *
@@ -3685,12 +3728,37 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3685
3728
  * @returns this (for chaining)
3686
3729
  */
3687
3730
  when(value: unknown): this;
3731
+ /**
3732
+ * Short alias for when()
3733
+ */
3734
+ w(value: unknown): this;
3688
3735
  /**
3689
3736
  * Ends the current branch and merges all branch paths back into the main flow.
3737
+ * Optionally specify the explicit merged state type for better type safety.
3690
3738
  *
3691
- * @returns this (for chaining)
3739
+ * @param explicitMergedType - Optional type hint for the merged state (defaults to current TState)
3740
+ * @returns AxFlow instance with the merged state type
3741
+ *
3742
+ * @example
3743
+ * ```typescript
3744
+ * // Default behavior - preserves current TState
3745
+ * flow.branch(state => state.type)
3746
+ * .when('simple').execute('simpleProcessor', ...)
3747
+ * .when('complex').execute('complexProcessor', ...)
3748
+ * .merge()
3749
+ *
3750
+ * // Explicit type - specify exact merged state shape
3751
+ * flow.branch(state => state.type)
3752
+ * .when('simple').map(state => ({ result: state.simpleResult, method: 'simple' }))
3753
+ * .when('complex').map(state => ({ result: state.complexResult, method: 'complex' }))
3754
+ * .merge<{ result: string; method: string }>()
3755
+ * ```
3692
3756
  */
3693
- merge(): this;
3757
+ merge<TMergedState extends AxFlowState = TState>(): AxFlow<IN, OUT, TNodes, TMergedState>;
3758
+ /**
3759
+ * Short alias for merge()
3760
+ */
3761
+ mg<TMergedState extends AxFlowState = TState>(): AxFlow<IN, OUT, TNodes, TMergedState>;
3694
3762
  /**
3695
3763
  * Executes multiple operations in parallel and merges their results.
3696
3764
  * Both typed and legacy untyped branches are supported.
@@ -3712,6 +3780,14 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3712
3780
  [K in TResultKey]: T;
3713
3781
  }>;
3714
3782
  };
3783
+ /**
3784
+ * Short alias for parallel()
3785
+ */
3786
+ p(branches: (AxFlowParallelBranch | AxFlowTypedParallelBranch<TNodes, TState>)[]): {
3787
+ merge<T, TResultKey extends string>(resultKey: TResultKey, mergeFunction: (...results: unknown[]) => T): AxFlow<IN, OUT, TNodes, TState & {
3788
+ [K in TResultKey]: T;
3789
+ }>;
3790
+ };
3715
3791
  /**
3716
3792
  * Creates a feedback loop that jumps back to a labeled step if a condition is met.
3717
3793
  *
@@ -3729,26 +3805,39 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3729
3805
  * ```
3730
3806
  */
3731
3807
  feedback(condition: (state: TState) => boolean, targetLabel: string, maxIterations?: number): this;
3808
+ /**
3809
+ * Short alias for feedback()
3810
+ */
3811
+ fb(condition: (state: TState) => boolean, targetLabel: string, maxIterations?: number): this;
3732
3812
  /**
3733
3813
  * Marks the beginning of a loop block.
3734
3814
  *
3735
3815
  * @param condition - Function that takes the current state and returns a boolean
3816
+ * @param maxIterations - Maximum number of iterations to prevent infinite loops (default: 100)
3736
3817
  * @returns this (for chaining)
3737
3818
  *
3738
3819
  * @example
3739
3820
  * ```typescript
3740
- * flow.while(state => state.iterations < 3)
3821
+ * flow.while(state => state.iterations < 3, 10)
3741
3822
  * .map(state => ({ ...state, iterations: (state.iterations || 0) + 1 }))
3742
3823
  * .endWhile()
3743
3824
  * ```
3744
3825
  */
3745
- while(condition: (state: TState) => boolean): this;
3826
+ while(condition: (state: TState) => boolean, maxIterations?: number): this;
3827
+ /**
3828
+ * Short alias for while()
3829
+ */
3830
+ wh(condition: (state: TState) => boolean, maxIterations?: number): this;
3746
3831
  /**
3747
3832
  * Marks the end of a loop block.
3748
3833
  *
3749
3834
  * @returns this (for chaining)
3750
3835
  */
3751
3836
  endWhile(): this;
3837
+ /**
3838
+ * Short alias for endWhile()
3839
+ */
3840
+ end(): this;
3752
3841
  /**
3753
3842
  * Executes the flow with the given AI service and input values.
3754
3843
  *
package/index.d.ts CHANGED
@@ -3602,7 +3602,7 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3602
3602
  private branchContext;
3603
3603
  constructor(signature?: NonNullable<ConstructorParameters<typeof AxSignature>[0]>);
3604
3604
  /**
3605
- * Declares a reusable computational node and its input/output signature.
3605
+ * Declares a reusable computational node using a signature string.
3606
3606
  * Returns a new AxFlow type that tracks this node in the TNodes registry.
3607
3607
  *
3608
3608
  * @param name - The name of the node
@@ -3620,6 +3620,33 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3620
3620
  [K in TName]: InferAxGen<TSig>;
3621
3621
  }, // Add new node to registry
3622
3622
  TState>;
3623
+ /**
3624
+ * Declares a reusable computational node using an existing AxGen instance.
3625
+ * This allows reusing pre-configured generators in the flow.
3626
+ *
3627
+ * @param name - The name of the node
3628
+ * @param axgenInstance - Existing AxGen instance to use for this node
3629
+ * @returns New AxFlow instance with updated TNodes type
3630
+ *
3631
+ * @example
3632
+ * ```typescript
3633
+ * const summarizer = new AxGen('text:string -> summary:string', { temperature: 0.1 })
3634
+ * flow.node('summarizer', summarizer)
3635
+ * ```
3636
+ */
3637
+ node<TName extends string, TGen extends AxGen<any, any>>(name: TName, axgenInstance: TGen): AxFlow<IN, OUT, TNodes & {
3638
+ [K in TName]: TGen;
3639
+ }, // Add new node to registry with exact type
3640
+ TState>;
3641
+ /**
3642
+ * Short alias for node() - supports both signature strings and AxGen instances
3643
+ */
3644
+ n<TName extends string, TSig extends string>(name: TName, signature: TSig, options?: Readonly<AxProgramForwardOptions>): AxFlow<IN, OUT, TNodes & {
3645
+ [K in TName]: InferAxGen<TSig>;
3646
+ }, TState>;
3647
+ n<TName extends string, TGen extends AxGen<any, any>>(name: TName, axgenInstance: TGen): AxFlow<IN, OUT, TNodes & {
3648
+ [K in TName]: TGen;
3649
+ }, TState>;
3623
3650
  /**
3624
3651
  * Applies a synchronous transformation to the state object.
3625
3652
  * Returns a new AxFlow type with the evolved state.
@@ -3633,6 +3660,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3633
3660
  * ```
3634
3661
  */
3635
3662
  map<TNewState extends AxFlowState>(transform: (state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;
3663
+ /**
3664
+ * Short alias for map()
3665
+ */
3666
+ m<TNewState extends AxFlowState>(transform: (state: TState) => TNewState): AxFlow<IN, OUT, TNodes, TNewState>;
3636
3667
  /**
3637
3668
  * Labels a step for later reference (useful for feedback loops).
3638
3669
  *
@@ -3646,6 +3677,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3646
3677
  * ```
3647
3678
  */
3648
3679
  label(label: string): this;
3680
+ /**
3681
+ * Short alias for label()
3682
+ */
3683
+ l(label: string): this;
3649
3684
  /**
3650
3685
  * Executes a previously defined node with full type safety.
3651
3686
  * The node name must exist in TNodes, and the mapping function is typed based on the node's signature.
@@ -3661,6 +3696,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3661
3696
  * ```
3662
3697
  */
3663
3698
  execute<TNodeName extends keyof TNodes & string>(nodeName: TNodeName, mapping: (state: TState) => GetGenIn<TNodes[TNodeName]>, dynamicContext?: AxFlowDynamicContext): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;
3699
+ /**
3700
+ * Short alias for execute()
3701
+ */
3702
+ e<TNodeName extends keyof TNodes & string>(nodeName: TNodeName, mapping: (state: TState) => GetGenIn<TNodes[TNodeName]>, dynamicContext?: AxFlowDynamicContext): AxFlow<IN, OUT, TNodes, AddNodeResult<TState, TNodeName, GetGenOut<TNodes[TNodeName]>>>;
3664
3703
  /**
3665
3704
  * Starts a conditional branch based on a predicate function.
3666
3705
  *
@@ -3678,6 +3717,10 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3678
3717
  * ```
3679
3718
  */
3680
3719
  branch(predicate: (state: TState) => unknown): this;
3720
+ /**
3721
+ * Short alias for branch()
3722
+ */
3723
+ b(predicate: (state: TState) => unknown): this;
3681
3724
  /**
3682
3725
  * Defines a branch case for the current branch context.
3683
3726
  *
@@ -3685,12 +3728,37 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3685
3728
  * @returns this (for chaining)
3686
3729
  */
3687
3730
  when(value: unknown): this;
3731
+ /**
3732
+ * Short alias for when()
3733
+ */
3734
+ w(value: unknown): this;
3688
3735
  /**
3689
3736
  * Ends the current branch and merges all branch paths back into the main flow.
3737
+ * Optionally specify the explicit merged state type for better type safety.
3690
3738
  *
3691
- * @returns this (for chaining)
3739
+ * @param explicitMergedType - Optional type hint for the merged state (defaults to current TState)
3740
+ * @returns AxFlow instance with the merged state type
3741
+ *
3742
+ * @example
3743
+ * ```typescript
3744
+ * // Default behavior - preserves current TState
3745
+ * flow.branch(state => state.type)
3746
+ * .when('simple').execute('simpleProcessor', ...)
3747
+ * .when('complex').execute('complexProcessor', ...)
3748
+ * .merge()
3749
+ *
3750
+ * // Explicit type - specify exact merged state shape
3751
+ * flow.branch(state => state.type)
3752
+ * .when('simple').map(state => ({ result: state.simpleResult, method: 'simple' }))
3753
+ * .when('complex').map(state => ({ result: state.complexResult, method: 'complex' }))
3754
+ * .merge<{ result: string; method: string }>()
3755
+ * ```
3692
3756
  */
3693
- merge(): this;
3757
+ merge<TMergedState extends AxFlowState = TState>(): AxFlow<IN, OUT, TNodes, TMergedState>;
3758
+ /**
3759
+ * Short alias for merge()
3760
+ */
3761
+ mg<TMergedState extends AxFlowState = TState>(): AxFlow<IN, OUT, TNodes, TMergedState>;
3694
3762
  /**
3695
3763
  * Executes multiple operations in parallel and merges their results.
3696
3764
  * Both typed and legacy untyped branches are supported.
@@ -3712,6 +3780,14 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3712
3780
  [K in TResultKey]: T;
3713
3781
  }>;
3714
3782
  };
3783
+ /**
3784
+ * Short alias for parallel()
3785
+ */
3786
+ p(branches: (AxFlowParallelBranch | AxFlowTypedParallelBranch<TNodes, TState>)[]): {
3787
+ merge<T, TResultKey extends string>(resultKey: TResultKey, mergeFunction: (...results: unknown[]) => T): AxFlow<IN, OUT, TNodes, TState & {
3788
+ [K in TResultKey]: T;
3789
+ }>;
3790
+ };
3715
3791
  /**
3716
3792
  * Creates a feedback loop that jumps back to a labeled step if a condition is met.
3717
3793
  *
@@ -3729,26 +3805,39 @@ TState extends AxFlowState = IN> extends AxProgramWithSignature<IN, OUT> {
3729
3805
  * ```
3730
3806
  */
3731
3807
  feedback(condition: (state: TState) => boolean, targetLabel: string, maxIterations?: number): this;
3808
+ /**
3809
+ * Short alias for feedback()
3810
+ */
3811
+ fb(condition: (state: TState) => boolean, targetLabel: string, maxIterations?: number): this;
3732
3812
  /**
3733
3813
  * Marks the beginning of a loop block.
3734
3814
  *
3735
3815
  * @param condition - Function that takes the current state and returns a boolean
3816
+ * @param maxIterations - Maximum number of iterations to prevent infinite loops (default: 100)
3736
3817
  * @returns this (for chaining)
3737
3818
  *
3738
3819
  * @example
3739
3820
  * ```typescript
3740
- * flow.while(state => state.iterations < 3)
3821
+ * flow.while(state => state.iterations < 3, 10)
3741
3822
  * .map(state => ({ ...state, iterations: (state.iterations || 0) + 1 }))
3742
3823
  * .endWhile()
3743
3824
  * ```
3744
3825
  */
3745
- while(condition: (state: TState) => boolean): this;
3826
+ while(condition: (state: TState) => boolean, maxIterations?: number): this;
3827
+ /**
3828
+ * Short alias for while()
3829
+ */
3830
+ wh(condition: (state: TState) => boolean, maxIterations?: number): this;
3746
3831
  /**
3747
3832
  * Marks the end of a loop block.
3748
3833
  *
3749
3834
  * @returns this (for chaining)
3750
3835
  */
3751
3836
  endWhile(): this;
3837
+ /**
3838
+ * Short alias for endWhile()
3839
+ */
3840
+ end(): this;
3752
3841
  /**
3753
3842
  * Executes the flow with the given AI service and input values.
3754
3843
  *
package/index.js CHANGED
@@ -13010,34 +13010,35 @@ var AxFlow = class extends AxProgramWithSignature {
13010
13010
  constructor(signature = "userInput:string -> flowOutput:string") {
13011
13011
  super(signature);
13012
13012
  }
13013
- /**
13014
- * Declares a reusable computational node and its input/output signature.
13015
- * Returns a new AxFlow type that tracks this node in the TNodes registry.
13016
- *
13017
- * @param name - The name of the node
13018
- * @param signature - Signature string in the same format as AxSignature
13019
- * @param options - Optional program forward options (same as AxGen)
13020
- * @returns New AxFlow instance with updated TNodes type
13021
- *
13022
- * @example
13023
- * ```typescript
13024
- * flow.node('summarizer', 'text:string -> summary:string')
13025
- * flow.node('analyzer', 'text:string -> analysis:string, confidence:number', { debug: true })
13026
- * ```
13027
- */
13028
- node(name, signature, options) {
13029
- if (!signature) {
13030
- throw new Error(
13031
- `Invalid signature for node '${name}': signature cannot be empty`
13013
+ // Implementation
13014
+ node(name, signatureOrAxGen, options) {
13015
+ if (signatureOrAxGen instanceof AxGen) {
13016
+ this.nodes.set(name, {
13017
+ inputs: {},
13018
+ outputs: {}
13019
+ });
13020
+ this.nodeGenerators.set(
13021
+ name,
13022
+ signatureOrAxGen
13032
13023
  );
13024
+ } else {
13025
+ const signature = signatureOrAxGen;
13026
+ if (!signature) {
13027
+ throw new Error(
13028
+ `Invalid signature for node '${name}': signature cannot be empty`
13029
+ );
13030
+ }
13031
+ this.nodes.set(name, {
13032
+ inputs: {},
13033
+ outputs: {}
13034
+ });
13035
+ this.nodeGenerators.set(name, new AxGen(signature, options));
13033
13036
  }
13034
- this.nodes.set(name, {
13035
- inputs: {},
13036
- outputs: {}
13037
- });
13038
- this.nodeGenerators.set(name, new AxGen(signature, options));
13039
13037
  return this;
13040
13038
  }
13039
+ n(name, signatureOrAxGen, options) {
13040
+ return this.node(name, signatureOrAxGen, options);
13041
+ }
13041
13042
  /**
13042
13043
  * Applies a synchronous transformation to the state object.
13043
13044
  * Returns a new AxFlow type with the evolved state.
@@ -13068,6 +13069,12 @@ var AxFlow = class extends AxProgramWithSignature {
13068
13069
  }
13069
13070
  return this;
13070
13071
  }
13072
+ /**
13073
+ * Short alias for map()
13074
+ */
13075
+ m(transform) {
13076
+ return this.map(transform);
13077
+ }
13071
13078
  /**
13072
13079
  * Labels a step for later reference (useful for feedback loops).
13073
13080
  *
@@ -13087,6 +13094,12 @@ var AxFlow = class extends AxProgramWithSignature {
13087
13094
  this.stepLabels.set(label, this.flowDefinition.length);
13088
13095
  return this;
13089
13096
  }
13097
+ /**
13098
+ * Short alias for label()
13099
+ */
13100
+ l(label) {
13101
+ return this.label(label);
13102
+ }
13090
13103
  /**
13091
13104
  * Executes a previously defined node with full type safety.
13092
13105
  * The node name must exist in TNodes, and the mapping function is typed based on the node's signature.
@@ -13135,6 +13148,12 @@ var AxFlow = class extends AxProgramWithSignature {
13135
13148
  }
13136
13149
  return this;
13137
13150
  }
13151
+ /**
13152
+ * Short alias for execute()
13153
+ */
13154
+ e(nodeName, mapping, dynamicContext) {
13155
+ return this.execute(nodeName, mapping, dynamicContext);
13156
+ }
13138
13157
  /**
13139
13158
  * Starts a conditional branch based on a predicate function.
13140
13159
  *
@@ -13162,6 +13181,12 @@ var AxFlow = class extends AxProgramWithSignature {
13162
13181
  };
13163
13182
  return this;
13164
13183
  }
13184
+ /**
13185
+ * Short alias for branch()
13186
+ */
13187
+ b(predicate) {
13188
+ return this.branch(predicate);
13189
+ }
13165
13190
  /**
13166
13191
  * Defines a branch case for the current branch context.
13167
13192
  *
@@ -13176,10 +13201,33 @@ var AxFlow = class extends AxProgramWithSignature {
13176
13201
  this.branchContext.branches.set(value, []);
13177
13202
  return this;
13178
13203
  }
13204
+ /**
13205
+ * Short alias for when()
13206
+ */
13207
+ w(value) {
13208
+ return this.when(value);
13209
+ }
13179
13210
  /**
13180
13211
  * Ends the current branch and merges all branch paths back into the main flow.
13212
+ * Optionally specify the explicit merged state type for better type safety.
13181
13213
  *
13182
- * @returns this (for chaining)
13214
+ * @param explicitMergedType - Optional type hint for the merged state (defaults to current TState)
13215
+ * @returns AxFlow instance with the merged state type
13216
+ *
13217
+ * @example
13218
+ * ```typescript
13219
+ * // Default behavior - preserves current TState
13220
+ * flow.branch(state => state.type)
13221
+ * .when('simple').execute('simpleProcessor', ...)
13222
+ * .when('complex').execute('complexProcessor', ...)
13223
+ * .merge()
13224
+ *
13225
+ * // Explicit type - specify exact merged state shape
13226
+ * flow.branch(state => state.type)
13227
+ * .when('simple').map(state => ({ result: state.simpleResult, method: 'simple' }))
13228
+ * .when('complex').map(state => ({ result: state.complexResult, method: 'complex' }))
13229
+ * .merge<{ result: string; method: string }>()
13230
+ * ```
13183
13231
  */
13184
13232
  merge() {
13185
13233
  if (!this.branchContext) {
@@ -13201,6 +13249,12 @@ var AxFlow = class extends AxProgramWithSignature {
13201
13249
  });
13202
13250
  return this;
13203
13251
  }
13252
+ /**
13253
+ * Short alias for merge()
13254
+ */
13255
+ mg() {
13256
+ return this.merge();
13257
+ }
13204
13258
  /**
13205
13259
  * Executes multiple operations in parallel and merges their results.
13206
13260
  * Both typed and legacy untyped branches are supported.
@@ -13250,6 +13304,12 @@ var AxFlow = class extends AxProgramWithSignature {
13250
13304
  }
13251
13305
  };
13252
13306
  }
13307
+ /**
13308
+ * Short alias for parallel()
13309
+ */
13310
+ p(branches) {
13311
+ return this.parallel(branches);
13312
+ }
13253
13313
  /**
13254
13314
  * Creates a feedback loop that jumps back to a labeled step if a condition is met.
13255
13315
  *
@@ -13295,32 +13355,46 @@ var AxFlow = class extends AxProgramWithSignature {
13295
13355
  });
13296
13356
  return this;
13297
13357
  }
13358
+ /**
13359
+ * Short alias for feedback()
13360
+ */
13361
+ fb(condition, targetLabel, maxIterations = 10) {
13362
+ return this.feedback(condition, targetLabel, maxIterations);
13363
+ }
13298
13364
  /**
13299
13365
  * Marks the beginning of a loop block.
13300
13366
  *
13301
13367
  * @param condition - Function that takes the current state and returns a boolean
13368
+ * @param maxIterations - Maximum number of iterations to prevent infinite loops (default: 100)
13302
13369
  * @returns this (for chaining)
13303
13370
  *
13304
13371
  * @example
13305
13372
  * ```typescript
13306
- * flow.while(state => state.iterations < 3)
13373
+ * flow.while(state => state.iterations < 3, 10)
13307
13374
  * .map(state => ({ ...state, iterations: (state.iterations || 0) + 1 }))
13308
13375
  * .endWhile()
13309
13376
  * ```
13310
13377
  */
13311
- while(condition) {
13378
+ while(condition, maxIterations = 100) {
13312
13379
  const loopStartIndex = this.flowDefinition.length;
13313
13380
  this.loopStack.push(loopStartIndex);
13314
13381
  const placeholderStep = Object.assign(
13315
13382
  (state) => state,
13316
13383
  {
13317
13384
  _condition: condition,
13385
+ _maxIterations: maxIterations,
13318
13386
  _isLoopStart: true
13319
13387
  }
13320
13388
  );
13321
13389
  this.flowDefinition.push(placeholderStep);
13322
13390
  return this;
13323
13391
  }
13392
+ /**
13393
+ * Short alias for while()
13394
+ */
13395
+ wh(condition, maxIterations = 100) {
13396
+ return this.while(condition, maxIterations);
13397
+ }
13324
13398
  /**
13325
13399
  * Marks the end of a loop block.
13326
13400
  *
@@ -13336,18 +13410,32 @@ var AxFlow = class extends AxProgramWithSignature {
13336
13410
  throw new Error("Loop start step not found or invalid");
13337
13411
  }
13338
13412
  const condition = placeholderStep._condition;
13413
+ const maxIterations = placeholderStep._maxIterations;
13339
13414
  const loopBodySteps = this.flowDefinition.splice(loopStartIndex + 1);
13340
13415
  this.flowDefinition[loopStartIndex] = async (state, context3) => {
13341
13416
  let currentState = state;
13342
- while (condition(currentState)) {
13417
+ let iterations = 0;
13418
+ while (condition(currentState) && iterations < maxIterations) {
13419
+ iterations++;
13343
13420
  for (const step of loopBodySteps) {
13344
13421
  currentState = await step(currentState, context3);
13345
13422
  }
13346
13423
  }
13424
+ if (iterations >= maxIterations && condition(currentState)) {
13425
+ throw new Error(
13426
+ `While loop exceeded maximum iterations (${maxIterations}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`
13427
+ );
13428
+ }
13347
13429
  return currentState;
13348
13430
  };
13349
13431
  return this;
13350
13432
  }
13433
+ /**
13434
+ * Short alias for endWhile()
13435
+ */
13436
+ end() {
13437
+ return this.endWhile();
13438
+ }
13351
13439
  /**
13352
13440
  * Executes the flow with the given AI service and input values.
13353
13441
  *