@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.d.cts CHANGED
@@ -257,6 +257,7 @@ type AxAIServiceActionOptions<TModel = unknown, TEmbedModel = unknown> = {
257
257
  traceId?: string;
258
258
  rateLimiter?: AxRateLimiterFunction;
259
259
  debug?: boolean;
260
+ debugHideSystemPrompt?: boolean;
260
261
  };
261
262
  interface AxAIService<TModel = unknown, TEmbedModel = unknown> {
262
263
  getId(): string;
@@ -1353,10 +1354,13 @@ interface AxAIMemory {
1353
1354
 
1354
1355
  declare class AxMemory implements AxAIMemory {
1355
1356
  private limit;
1356
- private debug;
1357
+ private options?;
1357
1358
  private memories;
1358
1359
  private defaultMemory;
1359
- constructor(limit?: number, debug?: boolean);
1360
+ constructor(limit?: number, options?: {
1361
+ debug?: boolean;
1362
+ debugHideSystemPrompt?: boolean;
1363
+ } | undefined);
1360
1364
  private getMemory;
1361
1365
  add(value: AxChatRequest['chatPrompt'][number] | AxChatRequest['chatPrompt'], sessionId?: string): void;
1362
1366
  addResult(result: Readonly<AxChatResponseResult>, sessionId?: string): void;
@@ -1563,6 +1567,7 @@ type AxProgramForwardOptions = {
1563
1567
  stopFunction?: string;
1564
1568
  fastFail?: boolean;
1565
1569
  debug?: boolean;
1570
+ debugHideSystemPrompt?: boolean;
1566
1571
  };
1567
1572
  type AxProgramStreamingForwardOptions = Omit<AxProgramForwardOptions, 'stream'>;
1568
1573
  type AxGenDeltaOut<OUT> = {
package/index.d.ts CHANGED
@@ -257,6 +257,7 @@ type AxAIServiceActionOptions<TModel = unknown, TEmbedModel = unknown> = {
257
257
  traceId?: string;
258
258
  rateLimiter?: AxRateLimiterFunction;
259
259
  debug?: boolean;
260
+ debugHideSystemPrompt?: boolean;
260
261
  };
261
262
  interface AxAIService<TModel = unknown, TEmbedModel = unknown> {
262
263
  getId(): string;
@@ -1353,10 +1354,13 @@ interface AxAIMemory {
1353
1354
 
1354
1355
  declare class AxMemory implements AxAIMemory {
1355
1356
  private limit;
1356
- private debug;
1357
+ private options?;
1357
1358
  private memories;
1358
1359
  private defaultMemory;
1359
- constructor(limit?: number, debug?: boolean);
1360
+ constructor(limit?: number, options?: {
1361
+ debug?: boolean;
1362
+ debugHideSystemPrompt?: boolean;
1363
+ } | undefined);
1360
1364
  private getMemory;
1361
1365
  add(value: AxChatRequest['chatPrompt'][number] | AxChatRequest['chatPrompt'], sessionId?: string): void;
1362
1366
  addResult(result: Readonly<AxChatResponseResult>, sessionId?: string): void;
@@ -1563,6 +1567,7 @@ type AxProgramForwardOptions = {
1563
1567
  stopFunction?: string;
1564
1568
  fastFail?: boolean;
1565
1569
  debug?: boolean;
1570
+ debugHideSystemPrompt?: boolean;
1566
1571
  };
1567
1572
  type AxProgramStreamingForwardOptions = Omit<AxProgramForwardOptions, 'stream'>;
1568
1573
  type AxGenDeltaOut<OUT> = {
package/index.js CHANGED
@@ -603,9 +603,12 @@ var ColorLog = class {
603
603
 
604
604
  // ai/debug.ts
605
605
  var colorLog = new ColorLog();
606
- var formatChatMessage = (msg, hideContent) => {
606
+ var formatChatMessage = (msg, hideContent, hideSystemPrompt) => {
607
607
  switch (msg.role) {
608
608
  case "system":
609
+ if (hideSystemPrompt) {
610
+ return "";
611
+ }
609
612
  return `
610
613
  ${colorLog.blueBright("System:")}
611
614
  ${colorLog.whiteBright(msg.content)}`;
@@ -651,13 +654,15 @@ ${hideContent ? "" : colorLog.whiteBright(msg.content ?? "<empty>")}`;
651
654
  throw new Error("Invalid role");
652
655
  }
653
656
  };
654
- var logChatRequestMessage = (msg) => {
655
- process.stdout.write(`${formatChatMessage(msg)}
657
+ var logChatRequestMessage = (msg, hideSystemPrompt) => {
658
+ process.stdout.write(`${formatChatMessage(msg, hideSystemPrompt)}
656
659
  `);
657
660
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
658
661
  };
659
- var logChatRequest = (chatPrompt) => {
660
- const items = chatPrompt?.map((msg) => formatChatMessage(msg));
662
+ var logChatRequest = (chatPrompt, hideSystemPrompt) => {
663
+ const items = chatPrompt?.map(
664
+ (msg) => formatChatMessage(msg, hideSystemPrompt)
665
+ );
661
666
  if (items) {
662
667
  process.stdout.write(items.join("\n"));
663
668
  process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
@@ -977,7 +982,7 @@ var AxBaseAI = class {
977
982
  return res2;
978
983
  };
979
984
  if (options?.debug ?? this.debug) {
980
- logChatRequest(req.chatPrompt);
985
+ logChatRequest(req.chatPrompt, options?.debugHideSystemPrompt);
981
986
  }
982
987
  const rt = options?.rateLimiter ?? this.rt;
983
988
  const rv = rt ? await rt(fn, { modelUsage: this.modelUsage }) : await fn();
@@ -3759,9 +3764,9 @@ function mergeFunctionCalls(functionCalls, functionCallDeltas) {
3759
3764
  // mem/memory.ts
3760
3765
  var defaultLimit = 1e4;
3761
3766
  var MemoryImpl = class {
3762
- constructor(limit = defaultLimit, debug = false) {
3767
+ constructor(limit = defaultLimit, options) {
3763
3768
  this.limit = limit;
3764
- this.debug = debug;
3769
+ this.options = options;
3765
3770
  if (limit <= 0) {
3766
3771
  throw Error("argument 'limit' must be greater than 0");
3767
3772
  }
@@ -3782,8 +3787,8 @@ var MemoryImpl = class {
3782
3787
  }
3783
3788
  add(value) {
3784
3789
  this.addMemory(value);
3785
- if (this.debug) {
3786
- debugRequest(value);
3790
+ if (this.options?.debug) {
3791
+ debugRequest(value, this.options?.debugHideSystemPrompt);
3787
3792
  }
3788
3793
  }
3789
3794
  addResultMessage({
@@ -3802,7 +3807,7 @@ var MemoryImpl = class {
3802
3807
  functionCalls
3803
3808
  }) {
3804
3809
  this.addResultMessage({ content, name, functionCalls });
3805
- if (this.debug) {
3810
+ if (this.options?.debug) {
3806
3811
  debugResponse({ content, name, functionCalls });
3807
3812
  }
3808
3813
  }
@@ -3826,7 +3831,7 @@ var MemoryImpl = class {
3826
3831
  lastItem.chat.functionCalls = functionCalls;
3827
3832
  }
3828
3833
  }
3829
- if (this.debug) {
3834
+ if (this.options?.debug) {
3830
3835
  if (delta && typeof delta === "string") {
3831
3836
  debugResponseDelta(delta);
3832
3837
  } else if (lastItem) {
@@ -3882,10 +3887,10 @@ var MemoryImpl = class {
3882
3887
  }
3883
3888
  };
3884
3889
  var AxMemory = class {
3885
- constructor(limit = defaultLimit, debug = false) {
3890
+ constructor(limit = defaultLimit, options) {
3886
3891
  this.limit = limit;
3887
- this.debug = debug;
3888
- this.defaultMemory = new MemoryImpl(limit, debug);
3892
+ this.options = options;
3893
+ this.defaultMemory = new MemoryImpl(limit, options);
3889
3894
  }
3890
3895
  memories = /* @__PURE__ */ new Map();
3891
3896
  defaultMemory;
@@ -3894,7 +3899,7 @@ var AxMemory = class {
3894
3899
  return this.defaultMemory;
3895
3900
  }
3896
3901
  if (!this.memories.has(sessionId)) {
3897
- this.memories.set(sessionId, new MemoryImpl(this.limit));
3902
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
3898
3903
  }
3899
3904
  return this.memories.get(sessionId);
3900
3905
  }
@@ -3923,15 +3928,15 @@ var AxMemory = class {
3923
3928
  if (!sessionId) {
3924
3929
  this.defaultMemory.reset();
3925
3930
  } else {
3926
- this.memories.set(sessionId, new MemoryImpl(this.limit));
3931
+ this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
3927
3932
  }
3928
3933
  }
3929
3934
  };
3930
- function debugRequest(value) {
3935
+ function debugRequest(value, hideSystemPrompt) {
3931
3936
  if (Array.isArray(value)) {
3932
- logChatRequest(value);
3937
+ logChatRequest(value, hideSystemPrompt);
3933
3938
  } else {
3934
- logChatRequestMessage(value);
3939
+ logChatRequestMessage(value, hideSystemPrompt);
3935
3940
  }
3936
3941
  }
3937
3942
  function debugResponse(value) {
@@ -5416,10 +5421,13 @@ ${errors}
5416
5421
  }
5417
5422
 
5418
5423
  // dsp/datetime.ts
5419
- function parseLLMFriendlyDate(field, dateStr) {
5424
+ function parseLLMFriendlyDate(field, dateStr, required = false) {
5420
5425
  try {
5421
5426
  return _parseLLMFriendlyDate(dateStr);
5422
5427
  } catch (err) {
5428
+ if (field.isOptional && !required) {
5429
+ return;
5430
+ }
5423
5431
  const message = err.message;
5424
5432
  throw new ValidationError({ fields: [field], message, value: dateStr });
5425
5433
  }
@@ -5433,10 +5441,13 @@ function _parseLLMFriendlyDate(dateStr) {
5433
5441
  const date = moment.utc(dateStr, "YYYY-MM-DD").startOf("day");
5434
5442
  return date.toDate();
5435
5443
  }
5436
- function parseLLMFriendlyDateTime(field, dateStr) {
5444
+ function parseLLMFriendlyDateTime(field, dateStr, required = false) {
5437
5445
  try {
5438
5446
  return _parseLLMFriendlyDateTime(dateStr);
5439
5447
  } catch (err) {
5448
+ if (field.isOptional && !required) {
5449
+ return;
5450
+ }
5440
5451
  const message = err.message;
5441
5452
  throw new ValidationError({ fields: [field], message, value: dateStr });
5442
5453
  }
@@ -5505,8 +5516,9 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5505
5516
  if (field.name in values) {
5506
5517
  continue;
5507
5518
  }
5508
- const prefix = field.title + ":";
5509
- let e = matchesContent(content, prefix, xstate.s + 1);
5519
+ const isFirst = xstate.extractedFields.length === 0;
5520
+ const prefix = (isFirst ? "" : "\n") + field.title + ":";
5521
+ let e = matchesContent(content, prefix, xstate.s === 0 ? 0 : xstate.s + 1);
5510
5522
  switch (e) {
5511
5523
  case -1:
5512
5524
  if (streamingValidation && values.length == 0 && !field.isOptional) {
@@ -5534,7 +5546,11 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
5534
5546
  if (parsedValue !== void 0) {
5535
5547
  values[xstate.currField.name] = parsedValue;
5536
5548
  }
5537
- xstate.prevField = { field: xstate.currField, s: xstate.s, e };
5549
+ if (xstate.prevFields) {
5550
+ xstate.prevFields?.push({ field: xstate.currField, s: xstate.s, e });
5551
+ } else {
5552
+ xstate.prevFields = [{ field: xstate.currField, s: xstate.s, e }];
5553
+ }
5538
5554
  }
5539
5555
  checkMissingRequiredFields(xstate, values, index);
5540
5556
  xstate.s = e + prefixLen;
@@ -5559,7 +5575,7 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
5559
5575
  const sigFields = sig.getOutputFields();
5560
5576
  checkMissingRequiredFields(xstate, values, sigFields.length);
5561
5577
  };
5562
- var convertValueToType = (field, val) => {
5578
+ var convertValueToType = (field, val, required = false) => {
5563
5579
  switch (field.type?.name) {
5564
5580
  case "code":
5565
5581
  return extractBlock(val);
@@ -5568,6 +5584,9 @@ var convertValueToType = (field, val) => {
5568
5584
  case "number": {
5569
5585
  const v = Number(val);
5570
5586
  if (Number.isNaN(v)) {
5587
+ if (field.isOptional && !required) {
5588
+ return;
5589
+ }
5571
5590
  throw new Error("Invalid number");
5572
5591
  }
5573
5592
  return v;
@@ -5582,16 +5601,22 @@ var convertValueToType = (field, val) => {
5582
5601
  } else if (v === "false") {
5583
5602
  return false;
5584
5603
  } else {
5604
+ if (field.isOptional && !required) {
5605
+ return;
5606
+ }
5585
5607
  throw new Error("Invalid boolean");
5586
5608
  }
5587
5609
  }
5588
5610
  case "date":
5589
- return parseLLMFriendlyDate(field, val);
5611
+ return parseLLMFriendlyDate(field, val, required);
5590
5612
  case "datetime":
5591
- return parseLLMFriendlyDateTime(field, val);
5613
+ return parseLLMFriendlyDateTime(field, val, required);
5592
5614
  case "class":
5593
5615
  const className = val;
5594
5616
  if (field.type.classes && !field.type.classes.includes(className)) {
5617
+ if (field.isOptional) {
5618
+ return;
5619
+ }
5595
5620
  throw new Error(
5596
5621
  `Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
5597
5622
  );
@@ -5627,11 +5652,11 @@ function* yieldDelta(content, field, s, e, xstate) {
5627
5652
  }
5628
5653
  }
5629
5654
  function* streamValues(sig, content, values, xstate) {
5630
- if (xstate.prevField && !xstate.prevField.field.isInternal) {
5631
- const { field, s, e } = xstate.prevField;
5655
+ for (const prevField of xstate.prevFields ?? []) {
5656
+ const { field, s, e } = prevField;
5632
5657
  yield* yieldDelta(content, field, s, e, xstate);
5633
- xstate.prevField = void 0;
5634
5658
  }
5659
+ xstate.prevFields = void 0;
5635
5660
  if (!xstate.currField || xstate.currField.isInternal) {
5636
5661
  return;
5637
5662
  }
@@ -5712,7 +5737,7 @@ function validateAndParseFieldValue(field, fieldValue) {
5712
5737
  for (const [index, item] of value.entries()) {
5713
5738
  if (item !== void 0) {
5714
5739
  const v = typeof item === "string" ? item.trim() : item;
5715
- value[index] = convertValueToType(field, v);
5740
+ value[index] = convertValueToType(field, v, true);
5716
5741
  }
5717
5742
  }
5718
5743
  } else {
@@ -6337,7 +6362,11 @@ var AxGen = class extends AxProgramWithSignature {
6337
6362
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
6338
6363
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
6339
6364
  const debug = options.debug ?? ai.getOptions().debug;
6340
- const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, debug);
6365
+ const memOptions = {
6366
+ debug: options.debug,
6367
+ debugHideSystemPrompt: options.debugHideSystemPrompt
6368
+ };
6369
+ const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
6341
6370
  let err;
6342
6371
  if (options?.functions && options.functions.length > 0) {
6343
6372
  const promptTemplate = this.options?.promptTemplate ?? AxPromptTemplate;