@ax-llm/ax 10.0.49 → 10.0.50

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.js CHANGED
@@ -5133,7 +5133,7 @@ var checkMissingRequiredFields = (xstate, values, currentIndex) => {
5133
5133
  });
5134
5134
  }
5135
5135
  };
5136
- var streamingExtractValues = (sig, values, xstate, content) => {
5136
+ var streamingExtractValues = (sig, values, xstate, content, streamingValidation = false) => {
5137
5137
  const fields = sig.getOutputFields();
5138
5138
  for (const [index, field] of fields.entries()) {
5139
5139
  if (field.name in values) {
@@ -5143,6 +5143,12 @@ var streamingExtractValues = (sig, values, xstate, content) => {
5143
5143
  let e = matchesContent(content, prefix, xstate.s + 1);
5144
5144
  switch (e) {
5145
5145
  case -1:
5146
+ if (streamingValidation && xstate.s == -1 && !field.isOptional) {
5147
+ throw new ValidationError({
5148
+ message: "Required field not found",
5149
+ fields: [field]
5150
+ });
5151
+ }
5146
5152
  continue;
5147
5153
  // Field is not found, continue to the next field
5148
5154
  case -2:
@@ -5159,6 +5165,7 @@ var streamingExtractValues = (sig, values, xstate, content) => {
5159
5165
  checkMissingRequiredFields(xstate, values, index);
5160
5166
  xstate.s = e + prefixLen;
5161
5167
  xstate.currField = field;
5168
+ xstate.currFieldIndex = index;
5162
5169
  if (!xstate.extractedFields.includes(field)) {
5163
5170
  xstate.extractedFields.push(field);
5164
5171
  }
@@ -5172,8 +5179,8 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
5172
5179
  values[xstate.currField.name] = parsedValue;
5173
5180
  }
5174
5181
  }
5175
- const fields = sig.getOutputFields();
5176
- checkMissingRequiredFields(xstate, values, fields.length - 1);
5182
+ const sigFields = sig.getOutputFields();
5183
+ checkMissingRequiredFields(xstate, values, sigFields.length);
5177
5184
  };
5178
5185
  var convertValueToType = (field, val) => {
5179
5186
  switch (field.type?.name) {
@@ -5658,6 +5665,7 @@ var AxGen = class extends AxProgramWithSignature {
5658
5665
  traceId,
5659
5666
  functions
5660
5667
  }) {
5668
+ const streamingValidation = !functions || functions.length == 0;
5661
5669
  const functionCalls = [];
5662
5670
  const values = {};
5663
5671
  const xstate = {
@@ -5673,14 +5681,21 @@ var AxGen = class extends AxProgramWithSignature {
5673
5681
  if (v.modelUsage) {
5674
5682
  this.usage.push({ ...usageInfo, ...v.modelUsage });
5675
5683
  }
5676
- if (result.content) {
5684
+ if (result.functionCalls) {
5685
+ mergeFunctionCalls(functionCalls, result.functionCalls);
5686
+ mem.updateResult(
5687
+ { name: result.name, content, functionCalls },
5688
+ sessionId
5689
+ );
5690
+ } else if (result.content) {
5677
5691
  content += result.content;
5678
5692
  mem.updateResult({ name: result.name, content }, sessionId);
5679
5693
  const skip = streamingExtractValues(
5680
5694
  this.signature,
5681
5695
  values,
5682
5696
  xstate,
5683
- content
5697
+ content,
5698
+ streamingValidation
5684
5699
  );
5685
5700
  if (skip) {
5686
5701
  continue;
@@ -5695,13 +5710,6 @@ var AxGen = class extends AxProgramWithSignature {
5695
5710
  assertAssertions(this.asserts, values);
5696
5711
  yield* streamValues(this.signature, values, xstate, content);
5697
5712
  }
5698
- if (result.functionCalls) {
5699
- mergeFunctionCalls(functionCalls, result.functionCalls);
5700
- mem.updateResult(
5701
- { name: result.name, content, functionCalls },
5702
- sessionId
5703
- );
5704
- }
5705
5713
  if (result.finishReason === "length") {
5706
5714
  throw new Error("Max tokens reached before completion");
5707
5715
  }
@@ -5720,17 +5728,18 @@ var AxGen = class extends AxProgramWithSignature {
5720
5728
  traceId
5721
5729
  );
5722
5730
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5731
+ } else {
5732
+ streamingExtractFinalValue(this.signature, values, xstate, content);
5733
+ assertStreamingAssertions(
5734
+ this.streamingAsserts,
5735
+ values,
5736
+ xstate,
5737
+ content,
5738
+ true
5739
+ );
5740
+ assertAssertions(this.asserts, values);
5741
+ yield* streamValues(this.signature, values, xstate, content, true);
5723
5742
  }
5724
- streamingExtractFinalValue(this.signature, values, xstate, content);
5725
- assertStreamingAssertions(
5726
- this.streamingAsserts,
5727
- values,
5728
- xstate,
5729
- content,
5730
- true
5731
- );
5732
- assertAssertions(this.asserts, values);
5733
- yield* streamValues(this.signature, values, xstate, content, true);
5734
5743
  }
5735
5744
  async processResponse({
5736
5745
  ai,
@@ -5742,15 +5751,15 @@ var AxGen = class extends AxProgramWithSignature {
5742
5751
  functions
5743
5752
  }) {
5744
5753
  const values = {};
5745
- for (const result of res.results ?? []) {
5754
+ let results = res.results ?? [];
5755
+ if (res.results.length > 1) {
5756
+ results = res.results.filter((r) => r.functionCalls);
5757
+ }
5758
+ for (const result of results) {
5746
5759
  if (res.modelUsage) {
5747
5760
  this.usage.push({ ...usageInfo, ...res.modelUsage });
5748
5761
  }
5749
5762
  mem.addResult(result, sessionId);
5750
- if (result.content) {
5751
- extractValues(this.signature, values, result.content);
5752
- assertAssertions(this.asserts, values);
5753
- }
5754
5763
  if (result.functionCalls) {
5755
5764
  const funcs = parseFunctionCalls(ai, result.functionCalls, values);
5756
5765
  if (funcs) {
@@ -5767,6 +5776,9 @@ var AxGen = class extends AxProgramWithSignature {
5767
5776
  );
5768
5777
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5769
5778
  }
5779
+ } else if (result.content) {
5780
+ extractValues(this.signature, values, result.content);
5781
+ assertAssertions(this.asserts, values);
5770
5782
  }
5771
5783
  if (result.finishReason === "length") {
5772
5784
  throw new Error("Max tokens reached before completion");