@ax-llm/ax 11.0.29 → 11.0.30

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
@@ -752,9 +752,12 @@ var ColorLog = class {
752
752
 
753
753
  // ai/debug.ts
754
754
  var colorLog = new ColorLog();
755
- var formatChatMessage = (msg, hideContent) => {
755
+ var formatChatMessage = (msg, hideContent, hideSystemPrompt) => {
756
756
  switch (msg.role) {
757
757
  case "system":
758
+ if (hideSystemPrompt) {
759
+ return "";
760
+ }
758
761
  return `
759
762
  ${colorLog.blueBright("System:")}
760
763
  ${colorLog.whiteBright(msg.content)}`;
@@ -800,13 +803,15 @@ ${hideContent ? "" : colorLog.whiteBright(msg.content ?? "<empty>")}`;
800
803
  throw new Error("Invalid role");
801
804
  }
802
805
  };
803
- var logChatRequestMessage = (msg) => {
804
- process.stdout.write(`${formatChatMessage(msg)}
806
+ var logChatRequestMessage = (msg, hideSystemPrompt) => {
807
+ process.stdout.write(`${formatChatMessage(msg, hideSystemPrompt)}
805
808
  `);
806
809
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
807
810
  };
808
- var logChatRequest = (chatPrompt) => {
809
- const items = chatPrompt?.map((msg) => formatChatMessage(msg));
811
+ var logChatRequest = (chatPrompt, hideSystemPrompt) => {
812
+ const items = chatPrompt?.map(
813
+ (msg) => formatChatMessage(msg, hideSystemPrompt)
814
+ );
810
815
  if (items) {
811
816
  process.stdout.write(items.join("\n"));
812
817
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
@@ -1126,7 +1131,7 @@ var AxBaseAI = class {
1126
1131
  return res2;
1127
1132
  };
1128
1133
  if (options?.debug ?? this.debug) {
1129
- logChatRequest(req.chatPrompt);
1134
+ logChatRequest(req.chatPrompt, options?.debugHideSystemPrompt);
1130
1135
  }
1131
1136
  const rt = options?.rateLimiter ?? this.rt;
1132
1137
  const rv = rt ? await rt(fn, { modelUsage: this.modelUsage }) : await fn();
@@ -3908,9 +3913,9 @@ function mergeFunctionCalls(functionCalls, functionCallDeltas) {
3908
3913
  // mem/memory.ts
3909
3914
  var defaultLimit = 1e4;
3910
3915
  var MemoryImpl = class {
3911
- constructor(limit = defaultLimit, debug = false) {
3916
+ constructor(limit = defaultLimit, options) {
3912
3917
  this.limit = limit;
3913
- this.debug = debug;
3918
+ this.options = options;
3914
3919
  if (limit <= 0) {
3915
3920
  throw Error("argument 'limit' must be greater than 0");
3916
3921
  }
@@ -3931,8 +3936,8 @@ var MemoryImpl = class {
3931
3936
  }
3932
3937
  add(value) {
3933
3938
  this.addMemory(value);
3934
- if (this.debug) {
3935
- debugRequest(value);
3939
+ if (this.options?.debug) {
3940
+ debugRequest(value, this.options?.debugHideSystemPrompt);
3936
3941
  }
3937
3942
  }
3938
3943
  addResultMessage({
@@ -3951,7 +3956,7 @@ var MemoryImpl = class {
3951
3956
  functionCalls
3952
3957
  }) {
3953
3958
  this.addResultMessage({ content, name, functionCalls });
3954
- if (this.debug) {
3959
+ if (this.options?.debug) {
3955
3960
  debugResponse({ content, name, functionCalls });
3956
3961
  }
3957
3962
  }
@@ -3975,7 +3980,7 @@ var MemoryImpl = class {
3975
3980
  lastItem.chat.functionCalls = functionCalls;
3976
3981
  }
3977
3982
  }
3978
- if (this.debug) {
3983
+ if (this.options?.debug) {
3979
3984
  if (delta && typeof delta === "string") {
3980
3985
  debugResponseDelta(delta);
3981
3986
  } else if (lastItem) {
@@ -4031,10 +4036,10 @@ var MemoryImpl = class {
4031
4036
  }
4032
4037
  };
4033
4038
  var AxMemory = class {
4034
- constructor(limit = defaultLimit, debug = false) {
4039
+ constructor(limit = defaultLimit, options) {
4035
4040
  this.limit = limit;
4036
- this.debug = debug;
4037
- this.defaultMemory = new MemoryImpl(limit, debug);
4041
+ this.options = options;
4042
+ this.defaultMemory = new MemoryImpl(limit, options);
4038
4043
  }
4039
4044
  memories = /* @__PURE__ */ new Map();
4040
4045
  defaultMemory;
@@ -4043,7 +4048,7 @@ var AxMemory = class {
4043
4048
  return this.defaultMemory;
4044
4049
  }
4045
4050
  if (!this.memories.has(sessionId)) {
4046
- this.memories.set(sessionId, new MemoryImpl(this.limit));
4051
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
4047
4052
  }
4048
4053
  return this.memories.get(sessionId);
4049
4054
  }
@@ -4072,15 +4077,15 @@ var AxMemory = class {
4072
4077
  if (!sessionId) {
4073
4078
  this.defaultMemory.reset();
4074
4079
  } else {
4075
- this.memories.set(sessionId, new MemoryImpl(this.limit));
4080
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
4076
4081
  }
4077
4082
  }
4078
4083
  };
4079
- function debugRequest(value) {
4084
+ function debugRequest(value, hideSystemPrompt) {
4080
4085
  if (Array.isArray(value)) {
4081
- logChatRequest(value);
4086
+ logChatRequest(value, hideSystemPrompt);
4082
4087
  } else {
4083
- logChatRequestMessage(value);
4088
+ logChatRequestMessage(value, hideSystemPrompt);
4084
4089
  }
4085
4090
  }
4086
4091
  function debugResponse(value) {
@@ -5565,10 +5570,13 @@ ${errors}
5565
5570
  }
5566
5571
 
5567
5572
  // dsp/datetime.ts
5568
- function parseLLMFriendlyDate(field, dateStr) {
5573
+ function parseLLMFriendlyDate(field, dateStr, required = false) {
5569
5574
  try {
5570
5575
  return _parseLLMFriendlyDate(dateStr);
5571
5576
  } catch (err) {
5577
+ if (field.isOptional && !required) {
5578
+ return;
5579
+ }
5572
5580
  const message = err.message;
5573
5581
  throw new ValidationError({ fields: [field], message, value: dateStr });
5574
5582
  }
@@ -5582,10 +5590,13 @@ function _parseLLMFriendlyDate(dateStr) {
5582
5590
  const date = import_moment_timezone.default.utc(dateStr, "YYYY-MM-DD").startOf("day");
5583
5591
  return date.toDate();
5584
5592
  }
5585
- function parseLLMFriendlyDateTime(field, dateStr) {
5593
+ function parseLLMFriendlyDateTime(field, dateStr, required = false) {
5586
5594
  try {
5587
5595
  return _parseLLMFriendlyDateTime(dateStr);
5588
5596
  } catch (err) {
5597
+ if (field.isOptional && !required) {
5598
+ return;
5599
+ }
5589
5600
  const message = err.message;
5590
5601
  throw new ValidationError({ fields: [field], message, value: dateStr });
5591
5602
  }
@@ -5654,8 +5665,9 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5654
5665
  if (field.name in values) {
5655
5666
  continue;
5656
5667
  }
5657
- const prefix = field.title + ":";
5658
- let e = matchesContent(content, prefix, xstate.s + 1);
5668
+ const isFirst = xstate.extractedFields.length === 0;
5669
+ const prefix = (isFirst ? "" : "\n") + field.title + ":";
5670
+ let e = matchesContent(content, prefix, xstate.s === 0 ? 0 : xstate.s + 1);
5659
5671
  switch (e) {
5660
5672
  case -1:
5661
5673
  if (streamingValidation && values.length == 0 && !field.isOptional) {
@@ -5683,7 +5695,11 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5683
5695
  if (parsedValue !== void 0) {
5684
5696
  values[xstate.currField.name] = parsedValue;
5685
5697
  }
5686
- xstate.prevField = { field: xstate.currField, s: xstate.s, e };
5698
+ if (xstate.prevFields) {
5699
+ xstate.prevFields?.push({ field: xstate.currField, s: xstate.s, e });
5700
+ } else {
5701
+ xstate.prevFields = [{ field: xstate.currField, s: xstate.s, e }];
5702
+ }
5687
5703
  }
5688
5704
  checkMissingRequiredFields(xstate, values, index);
5689
5705
  xstate.s = e + prefixLen;
@@ -5708,7 +5724,7 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
5708
5724
  const sigFields = sig.getOutputFields();
5709
5725
  checkMissingRequiredFields(xstate, values, sigFields.length);
5710
5726
  };
5711
- var convertValueToType = (field, val) => {
5727
+ var convertValueToType = (field, val, required = false) => {
5712
5728
  switch (field.type?.name) {
5713
5729
  case "code":
5714
5730
  return extractBlock(val);
@@ -5717,6 +5733,9 @@ var convertValueToType = (field, val) => {
5717
5733
  case "number": {
5718
5734
  const v = Number(val);
5719
5735
  if (Number.isNaN(v)) {
5736
+ if (field.isOptional && !required) {
5737
+ return;
5738
+ }
5720
5739
  throw new Error("Invalid number");
5721
5740
  }
5722
5741
  return v;
@@ -5731,16 +5750,22 @@ var convertValueToType = (field, val) => {
5731
5750
  } else if (v === "false") {
5732
5751
  return false;
5733
5752
  } else {
5753
+ if (field.isOptional && !required) {
5754
+ return;
5755
+ }
5734
5756
  throw new Error("Invalid boolean");
5735
5757
  }
5736
5758
  }
5737
5759
  case "date":
5738
- return parseLLMFriendlyDate(field, val);
5760
+ return parseLLMFriendlyDate(field, val, required);
5739
5761
  case "datetime":
5740
- return parseLLMFriendlyDateTime(field, val);
5762
+ return parseLLMFriendlyDateTime(field, val, required);
5741
5763
  case "class":
5742
5764
  const className = val;
5743
5765
  if (field.type.classes && !field.type.classes.includes(className)) {
5766
+ if (field.isOptional) {
5767
+ return;
5768
+ }
5744
5769
  throw new Error(
5745
5770
  `Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
5746
5771
  );
@@ -5776,11 +5801,11 @@ function* yieldDelta(content, field, s, e, xstate) {
5776
5801
  }
5777
5802
  }
5778
5803
  function* streamValues(sig, content, values, xstate) {
5779
- if (xstate.prevField && !xstate.prevField.field.isInternal) {
5780
- const { field, s, e } = xstate.prevField;
5804
+ for (const prevField of xstate.prevFields ?? []) {
5805
+ const { field, s, e } = prevField;
5781
5806
  yield* yieldDelta(content, field, s, e, xstate);
5782
- xstate.prevField = void 0;
5783
5807
  }
5808
+ xstate.prevFields = void 0;
5784
5809
  if (!xstate.currField || xstate.currField.isInternal) {
5785
5810
  return;
5786
5811
  }
@@ -5861,7 +5886,7 @@ function validateAndParseFieldValue(field, fieldValue) {
5861
5886
  for (const [index, item] of value.entries()) {
5862
5887
  if (item !== void 0) {
5863
5888
  const v = typeof item === "string" ? item.trim() : item;
5864
- value[index] = convertValueToType(field, v);
5889
+ value[index] = convertValueToType(field, v, true);
5865
5890
  }
5866
5891
  }
5867
5892
  } else {
@@ -6486,7 +6511,11 @@ var AxGen = class extends AxProgramWithSignature {
6486
6511
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
6487
6512
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
6488
6513
  const debug = options.debug ?? ai.getOptions().debug;
6489
- const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, debug);
6514
+ const memOptions = {
6515
+ debug: options.debug,
6516
+ debugHideSystemPrompt: options.debugHideSystemPrompt
6517
+ };
6518
+ const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
6490
6519
  let err;
6491
6520
  if (options?.functions && options.functions.length > 0) {
6492
6521
  const promptTemplate = this.options?.promptTemplate ?? AxPromptTemplate;