@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.cjs CHANGED
@@ -13188,34 +13188,35 @@ var AxFlow = class extends AxProgramWithSignature {
13188
13188
  constructor(signature = "userInput:string -> flowOutput:string") {
13189
13189
  super(signature);
13190
13190
  }
13191
- /**
13192
- * Declares a reusable computational node and its input/output signature.
13193
- * Returns a new AxFlow type that tracks this node in the TNodes registry.
13194
- *
13195
- * @param name - The name of the node
13196
- * @param signature - Signature string in the same format as AxSignature
13197
- * @param options - Optional program forward options (same as AxGen)
13198
- * @returns New AxFlow instance with updated TNodes type
13199
- *
13200
- * @example
13201
- * ```typescript
13202
- * flow.node('summarizer', 'text:string -> summary:string')
13203
- * flow.node('analyzer', 'text:string -> analysis:string, confidence:number', { debug: true })
13204
- * ```
13205
- */
13206
- node(name, signature, options) {
13207
- if (!signature) {
13208
- throw new Error(
13209
- `Invalid signature for node '${name}': signature cannot be empty`
13191
+ // Implementation
13192
+ node(name, signatureOrAxGen, options) {
13193
+ if (signatureOrAxGen instanceof AxGen) {
13194
+ this.nodes.set(name, {
13195
+ inputs: {},
13196
+ outputs: {}
13197
+ });
13198
+ this.nodeGenerators.set(
13199
+ name,
13200
+ signatureOrAxGen
13210
13201
  );
13202
+ } else {
13203
+ const signature = signatureOrAxGen;
13204
+ if (!signature) {
13205
+ throw new Error(
13206
+ `Invalid signature for node '${name}': signature cannot be empty`
13207
+ );
13208
+ }
13209
+ this.nodes.set(name, {
13210
+ inputs: {},
13211
+ outputs: {}
13212
+ });
13213
+ this.nodeGenerators.set(name, new AxGen(signature, options));
13211
13214
  }
13212
- this.nodes.set(name, {
13213
- inputs: {},
13214
- outputs: {}
13215
- });
13216
- this.nodeGenerators.set(name, new AxGen(signature, options));
13217
13215
  return this;
13218
13216
  }
13217
+ n(name, signatureOrAxGen, options) {
13218
+ return this.node(name, signatureOrAxGen, options);
13219
+ }
13219
13220
  /**
13220
13221
  * Applies a synchronous transformation to the state object.
13221
13222
  * Returns a new AxFlow type with the evolved state.
@@ -13246,6 +13247,12 @@ var AxFlow = class extends AxProgramWithSignature {
13246
13247
  }
13247
13248
  return this;
13248
13249
  }
13250
+ /**
13251
+ * Short alias for map()
13252
+ */
13253
+ m(transform) {
13254
+ return this.map(transform);
13255
+ }
13249
13256
  /**
13250
13257
  * Labels a step for later reference (useful for feedback loops).
13251
13258
  *
@@ -13265,6 +13272,12 @@ var AxFlow = class extends AxProgramWithSignature {
13265
13272
  this.stepLabels.set(label, this.flowDefinition.length);
13266
13273
  return this;
13267
13274
  }
13275
+ /**
13276
+ * Short alias for label()
13277
+ */
13278
+ l(label) {
13279
+ return this.label(label);
13280
+ }
13268
13281
  /**
13269
13282
  * Executes a previously defined node with full type safety.
13270
13283
  * The node name must exist in TNodes, and the mapping function is typed based on the node's signature.
@@ -13313,6 +13326,12 @@ var AxFlow = class extends AxProgramWithSignature {
13313
13326
  }
13314
13327
  return this;
13315
13328
  }
13329
+ /**
13330
+ * Short alias for execute()
13331
+ */
13332
+ e(nodeName, mapping, dynamicContext) {
13333
+ return this.execute(nodeName, mapping, dynamicContext);
13334
+ }
13316
13335
  /**
13317
13336
  * Starts a conditional branch based on a predicate function.
13318
13337
  *
@@ -13340,6 +13359,12 @@ var AxFlow = class extends AxProgramWithSignature {
13340
13359
  };
13341
13360
  return this;
13342
13361
  }
13362
+ /**
13363
+ * Short alias for branch()
13364
+ */
13365
+ b(predicate) {
13366
+ return this.branch(predicate);
13367
+ }
13343
13368
  /**
13344
13369
  * Defines a branch case for the current branch context.
13345
13370
  *
@@ -13354,10 +13379,33 @@ var AxFlow = class extends AxProgramWithSignature {
13354
13379
  this.branchContext.branches.set(value, []);
13355
13380
  return this;
13356
13381
  }
13382
+ /**
13383
+ * Short alias for when()
13384
+ */
13385
+ w(value) {
13386
+ return this.when(value);
13387
+ }
13357
13388
  /**
13358
13389
  * Ends the current branch and merges all branch paths back into the main flow.
13390
+ * Optionally specify the explicit merged state type for better type safety.
13359
13391
  *
13360
- * @returns this (for chaining)
13392
+ * @param explicitMergedType - Optional type hint for the merged state (defaults to current TState)
13393
+ * @returns AxFlow instance with the merged state type
13394
+ *
13395
+ * @example
13396
+ * ```typescript
13397
+ * // Default behavior - preserves current TState
13398
+ * flow.branch(state => state.type)
13399
+ * .when('simple').execute('simpleProcessor', ...)
13400
+ * .when('complex').execute('complexProcessor', ...)
13401
+ * .merge()
13402
+ *
13403
+ * // Explicit type - specify exact merged state shape
13404
+ * flow.branch(state => state.type)
13405
+ * .when('simple').map(state => ({ result: state.simpleResult, method: 'simple' }))
13406
+ * .when('complex').map(state => ({ result: state.complexResult, method: 'complex' }))
13407
+ * .merge<{ result: string; method: string }>()
13408
+ * ```
13361
13409
  */
13362
13410
  merge() {
13363
13411
  if (!this.branchContext) {
@@ -13379,6 +13427,12 @@ var AxFlow = class extends AxProgramWithSignature {
13379
13427
  });
13380
13428
  return this;
13381
13429
  }
13430
+ /**
13431
+ * Short alias for merge()
13432
+ */
13433
+ mg() {
13434
+ return this.merge();
13435
+ }
13382
13436
  /**
13383
13437
  * Executes multiple operations in parallel and merges their results.
13384
13438
  * Both typed and legacy untyped branches are supported.
@@ -13428,6 +13482,12 @@ var AxFlow = class extends AxProgramWithSignature {
13428
13482
  }
13429
13483
  };
13430
13484
  }
13485
+ /**
13486
+ * Short alias for parallel()
13487
+ */
13488
+ p(branches) {
13489
+ return this.parallel(branches);
13490
+ }
13431
13491
  /**
13432
13492
  * Creates a feedback loop that jumps back to a labeled step if a condition is met.
13433
13493
  *
@@ -13473,32 +13533,46 @@ var AxFlow = class extends AxProgramWithSignature {
13473
13533
  });
13474
13534
  return this;
13475
13535
  }
13536
+ /**
13537
+ * Short alias for feedback()
13538
+ */
13539
+ fb(condition, targetLabel, maxIterations = 10) {
13540
+ return this.feedback(condition, targetLabel, maxIterations);
13541
+ }
13476
13542
  /**
13477
13543
  * Marks the beginning of a loop block.
13478
13544
  *
13479
13545
  * @param condition - Function that takes the current state and returns a boolean
13546
+ * @param maxIterations - Maximum number of iterations to prevent infinite loops (default: 100)
13480
13547
  * @returns this (for chaining)
13481
13548
  *
13482
13549
  * @example
13483
13550
  * ```typescript
13484
- * flow.while(state => state.iterations < 3)
13551
+ * flow.while(state => state.iterations < 3, 10)
13485
13552
  * .map(state => ({ ...state, iterations: (state.iterations || 0) + 1 }))
13486
13553
  * .endWhile()
13487
13554
  * ```
13488
13555
  */
13489
- while(condition) {
13556
+ while(condition, maxIterations = 100) {
13490
13557
  const loopStartIndex = this.flowDefinition.length;
13491
13558
  this.loopStack.push(loopStartIndex);
13492
13559
  const placeholderStep = Object.assign(
13493
13560
  (state) => state,
13494
13561
  {
13495
13562
  _condition: condition,
13563
+ _maxIterations: maxIterations,
13496
13564
  _isLoopStart: true
13497
13565
  }
13498
13566
  );
13499
13567
  this.flowDefinition.push(placeholderStep);
13500
13568
  return this;
13501
13569
  }
13570
+ /**
13571
+ * Short alias for while()
13572
+ */
13573
+ wh(condition, maxIterations = 100) {
13574
+ return this.while(condition, maxIterations);
13575
+ }
13502
13576
  /**
13503
13577
  * Marks the end of a loop block.
13504
13578
  *
@@ -13514,18 +13588,32 @@ var AxFlow = class extends AxProgramWithSignature {
13514
13588
  throw new Error("Loop start step not found or invalid");
13515
13589
  }
13516
13590
  const condition = placeholderStep._condition;
13591
+ const maxIterations = placeholderStep._maxIterations;
13517
13592
  const loopBodySteps = this.flowDefinition.splice(loopStartIndex + 1);
13518
13593
  this.flowDefinition[loopStartIndex] = async (state, context3) => {
13519
13594
  let currentState = state;
13520
- while (condition(currentState)) {
13595
+ let iterations = 0;
13596
+ while (condition(currentState) && iterations < maxIterations) {
13597
+ iterations++;
13521
13598
  for (const step of loopBodySteps) {
13522
13599
  currentState = await step(currentState, context3);
13523
13600
  }
13524
13601
  }
13602
+ if (iterations >= maxIterations && condition(currentState)) {
13603
+ throw new Error(
13604
+ `While loop exceeded maximum iterations (${maxIterations}). Consider increasing maxIterations or ensuring the loop condition eventually becomes false.`
13605
+ );
13606
+ }
13525
13607
  return currentState;
13526
13608
  };
13527
13609
  return this;
13528
13610
  }
13611
+ /**
13612
+ * Short alias for endWhile()
13613
+ */
13614
+ end() {
13615
+ return this.endWhile();
13616
+ }
13529
13617
  /**
13530
13618
  * Executes the flow with the given AI service and input values.
13531
13619
  *