@ax-llm/ax 12.0.8 → 12.0.12

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
@@ -320,6 +320,17 @@ var AxAIServiceAuthenticationError = class extends AxAIServiceError {
320
320
  this.name = this.constructor.name;
321
321
  }
322
322
  };
323
+ async function safeReadResponseBody(response) {
324
+ try {
325
+ if (response.headers.get("content-type")?.includes("application/json")) {
326
+ return await response.json();
327
+ }
328
+ const clonedResponse = response.clone();
329
+ return await clonedResponse.text();
330
+ } catch (e) {
331
+ return `[ReadableStream - read failed: ${e.message}]`;
332
+ }
333
+ }
323
334
  function calculateRetryDelay(attempt, config) {
324
335
  const delay = Math.min(
325
336
  config.maxDelayMs,
@@ -413,9 +424,15 @@ var apiCall = async (api, json) => {
413
424
  });
414
425
  clearTimeout(timeoutId);
415
426
  if (res.status === 401 || res.status === 403) {
416
- throw new AxAIServiceAuthenticationError(apiUrl.href, json, res.body, {
417
- metrics
418
- });
427
+ const responseBody = await safeReadResponseBody(res);
428
+ throw new AxAIServiceAuthenticationError(
429
+ apiUrl.href,
430
+ json,
431
+ responseBody,
432
+ {
433
+ metrics
434
+ }
435
+ );
419
436
  }
420
437
  if (res.status >= 400 && shouldRetry(new Error(), res.status, attempt, retryConfig)) {
421
438
  const delay = calculateRetryDelay(attempt, retryConfig);
@@ -433,12 +450,13 @@ var apiCall = async (api, json) => {
433
450
  continue;
434
451
  }
435
452
  if (res.status >= 400) {
453
+ const responseBody = await safeReadResponseBody(res);
436
454
  throw new AxAIServiceStatusError(
437
455
  res.status,
438
456
  res.statusText,
439
457
  apiUrl.href,
440
458
  json,
441
- res.body,
459
+ responseBody,
442
460
  { metrics }
443
461
  );
444
462
  }
@@ -536,7 +554,7 @@ var apiCall = async (api, json) => {
536
554
  error,
537
555
  apiUrl.href,
538
556
  json,
539
- res.body,
557
+ "[ReadableStream - consumed during streaming]",
540
558
  {
541
559
  streamMetrics
542
560
  }
@@ -667,12 +685,12 @@ var ColorLog = class {
667
685
  }
668
686
  };
669
687
 
670
- // ai/debug.ts
688
+ // dsp/loggers.ts
671
689
  var colorLog = new ColorLog();
672
690
  var defaultOutput = (message) => {
673
691
  process.stdout.write(message);
674
692
  };
675
- var createDefaultLogger = (output = defaultOutput) => {
693
+ var axCreateDefaultLogger = (output = defaultOutput) => {
676
694
  return (message, options) => {
677
695
  const tags = options?.tags ?? [];
678
696
  let formattedMessage = message;
@@ -681,12 +699,44 @@ var createDefaultLogger = (output = defaultOutput) => {
681
699
  } else if (tags.includes("success") || tags.includes("responseContent")) {
682
700
  formattedMessage = colorLog.greenBright(formattedMessage);
683
701
  } else if (tags.includes("functionName")) {
684
- formattedMessage = colorLog.whiteBright(formattedMessage);
685
- } else if (tags.includes("functionArg") || tags.includes("systemContent") || tags.includes("assistantContent")) {
702
+ if (tags.includes("firstFunction")) {
703
+ formattedMessage = `
704
+ ${colorLog.whiteBright(formattedMessage)}`;
705
+ } else {
706
+ formattedMessage = `${colorLog.whiteBright(formattedMessage)}`;
707
+ }
708
+ } else if (tags.includes("systemContent") || tags.includes("assistantContent")) {
686
709
  formattedMessage = colorLog.blueBright(formattedMessage);
687
710
  } else if (tags.includes("warning") || tags.includes("discovery")) {
688
711
  formattedMessage = colorLog.yellow(formattedMessage);
712
+ } else if (tags.includes("functionArg")) {
713
+ formattedMessage = "";
714
+ }
715
+ if (tags.includes("responseStart") || tags.includes("systemStart") || tags.includes("userStart")) {
716
+ formattedMessage = `
717
+ ${formattedMessage}`;
718
+ } else if (tags.includes("responseEnd") || tags.includes("systemEnd") || tags.includes("userEnd")) {
719
+ formattedMessage = `${formattedMessage}
720
+ `;
721
+ } else if (tags.includes("assistantStart")) {
722
+ formattedMessage = `
723
+ ${formattedMessage}
724
+ `;
725
+ } else if (tags.includes("error")) {
726
+ formattedMessage = `
727
+ ${formattedMessage}
728
+ `;
729
+ } else if (tags.includes("functionEnd")) {
730
+ formattedMessage = `
731
+ `;
689
732
  }
733
+ output(formattedMessage);
734
+ };
735
+ };
736
+ var axCreateDefaultTextLogger = (output = defaultOutput) => {
737
+ return (message, options) => {
738
+ const tags = options?.tags ?? [];
739
+ let formattedMessage = message;
690
740
  if (tags.includes("responseStart") || tags.includes("systemStart") || tags.includes("userStart")) {
691
741
  formattedMessage = `
692
742
  ${formattedMessage}`;
@@ -708,7 +758,137 @@ ${formattedMessage}
708
758
  output(formattedMessage);
709
759
  };
710
760
  };
711
- var defaultLogger = createDefaultLogger();
761
+ var axCreateOptimizerLogger = (output = (msg) => process.stdout.write(msg)) => {
762
+ const baseLogger = axCreateDefaultLogger(output);
763
+ let isFirstPhase = true;
764
+ return (message, options) => {
765
+ const tags = options?.tags ?? [];
766
+ let formattedMessage = message;
767
+ if (tags.includes("optimizer")) {
768
+ if (tags.includes("start")) {
769
+ const trialsMatch = message.match(/with (\d+) trials?/) || message.match(/(\d+) trials?/);
770
+ const optimizerMatch = message.match(
771
+ /(MIPROv2|BootstrapFewshot|[A-Z][a-zA-Z]+)/
772
+ );
773
+ const optimizerName = optimizerMatch ? optimizerMatch[1] : "Optimizer";
774
+ if (trialsMatch && trialsMatch[1]) {
775
+ formattedMessage = `
776
+ \u250C\u2500 ${optimizerName} optimization (${trialsMatch[1]} trials)
777
+ `;
778
+ } else {
779
+ formattedMessage = `
780
+ \u250C\u2500 ${optimizerName} optimization
781
+ `;
782
+ }
783
+ isFirstPhase = true;
784
+ } else if (tags.includes("config")) {
785
+ if (message.includes("examples") && message.includes("training")) {
786
+ const match = message.match(
787
+ /(\d+) examples for training and (\d+) for validation/
788
+ ) || message.match(/(\d+) training.*?(\d+) validation/);
789
+ if (match && match[1] && match[2]) {
790
+ formattedMessage = `\u2502 Dataset: ${match[1]} training, ${match[2]} validation
791
+ `;
792
+ } else {
793
+ const simpleMatch = message.match(/(\d+) examples/);
794
+ if (simpleMatch && simpleMatch[1]) {
795
+ formattedMessage = `\u2502 Dataset: ${simpleMatch[1]} examples
796
+ `;
797
+ }
798
+ }
799
+ } else if (message.includes("teacher")) {
800
+ formattedMessage = `\u2502 Using teacher model
801
+ `;
802
+ } else {
803
+ formattedMessage = `\u2502 ${message}
804
+ `;
805
+ }
806
+ } else if (tags.includes("phase")) {
807
+ if (isFirstPhase) {
808
+ formattedMessage = `\u251C\u2500 ${message}
809
+ `;
810
+ isFirstPhase = false;
811
+ } else {
812
+ formattedMessage = `\u251C\u2500 ${message}
813
+ `;
814
+ }
815
+ } else if (tags.includes("result")) {
816
+ if (message.includes("Generated") || message.includes("Selected")) {
817
+ const match = message.match(/(\d+)/);
818
+ if (match && match[1]) {
819
+ formattedMessage = `\u2502 \u2713 ${message}
820
+ `;
821
+ } else {
822
+ formattedMessage = `\u2502 \u2713 ${message}
823
+ `;
824
+ }
825
+ } else if (message.includes("configuration")) {
826
+ formattedMessage = `\u2502 Applied best configuration
827
+ `;
828
+ } else {
829
+ formattedMessage = `\u2502 ${message}
830
+ `;
831
+ }
832
+ } else if (tags.includes("progress")) {
833
+ formattedMessage = `\u2502 ${message}
834
+ `;
835
+ } else if (tags.includes("complete")) {
836
+ const scoreMatch = message.match(/(score|performance):\s*([\d.]+)/);
837
+ if (scoreMatch && scoreMatch[2]) {
838
+ const score = parseFloat(scoreMatch[2]);
839
+ const percentage = score <= 1 ? (score * 100).toFixed(1) + "%" : score.toFixed(3);
840
+ formattedMessage = `\u251C\u2500 Complete! Best: ${percentage}
841
+ `;
842
+ } else if (message.includes("Bootstrap")) {
843
+ formattedMessage = `\u251C\u2500 ${message}
844
+ `;
845
+ } else {
846
+ formattedMessage = `\u251C\u2500 Optimization complete
847
+ `;
848
+ }
849
+ } else if (tags.includes("checkpoint")) {
850
+ if (message.includes("Resuming")) {
851
+ formattedMessage = `\u2502 ${message}
852
+ `;
853
+ } else {
854
+ const match = message.match(/checkpoint:\s*(.+)/) || message.match(/Saved\s+(.+)/);
855
+ if (match && match[1]) {
856
+ formattedMessage = `\u2514\u2500 Saved: ${match[1]}
857
+ `;
858
+ } else {
859
+ formattedMessage = `\u2514\u2500 Checkpoint saved
860
+ `;
861
+ }
862
+ }
863
+ }
864
+ } else if (tags.includes("discovery")) {
865
+ if (message.includes("Found") && message.includes("examples")) {
866
+ const match = message.match(/Found (\d+)/);
867
+ if (match && match[1]) {
868
+ formattedMessage = `\u2502 Found ${match[1]} examples
869
+ `;
870
+ }
871
+ }
872
+ }
873
+ if (tags.includes("error")) {
874
+ formattedMessage = `
875
+ \u2717 ${message}
876
+ `;
877
+ } else if (tags.includes("warning")) {
878
+ formattedMessage = `
879
+ \u26A0 ${message}
880
+ `;
881
+ } else if (tags.includes("success") && !tags.includes("optimizer")) {
882
+ formattedMessage = `\u2713 ${message}
883
+ `;
884
+ }
885
+ baseLogger(formattedMessage, options);
886
+ };
887
+ };
888
+ var axDefaultOptimizerLogger = axCreateOptimizerLogger();
889
+
890
+ // ai/debug.ts
891
+ var defaultLogger = axCreateDefaultLogger();
712
892
  var formatChatMessage = (msg, hideContent, hideSystemPrompt) => {
713
893
  switch (msg.role) {
714
894
  case "system":
@@ -785,9 +965,14 @@ var logResponseResult = (r, logger = defaultLogger) => {
785
965
  if (r.functionCalls && r.functionCalls.length > 0) {
786
966
  for (const [i, f2] of r.functionCalls.entries()) {
787
967
  if (f2.function.name) {
788
- logger(`[${i + 1}] ${f2.function.name}`, {
789
- tags: ["functionName"]
790
- });
968
+ const tags = ["functionName"];
969
+ if (i === 0) {
970
+ tags.push("firstFunction");
971
+ }
972
+ if (r.functionCalls.length > 1) {
973
+ tags.push("multipleFunctions");
974
+ }
975
+ logger(`[${i + 1}] ${f2.function.name}`, { tags });
791
976
  }
792
977
  if (f2.function.params) {
793
978
  const params = typeof f2.function.params === "string" ? f2.function.params : JSON.stringify(f2.function.params, null, 2);
@@ -1027,6 +1212,9 @@ var AxBaseAI = class {
1027
1212
  }
1028
1213
  async _chat1(req, options) {
1029
1214
  const model = this.getModel(req.model) ?? req.model ?? this.defaults.model;
1215
+ if (req.chatPrompt && Array.isArray(req.chatPrompt)) {
1216
+ validateAxMessageArray(req.chatPrompt);
1217
+ }
1030
1218
  const modelConfig = {
1031
1219
  ...this.aiImpl.getModelConfig(),
1032
1220
  ...req.modelConfig
@@ -1098,7 +1286,6 @@ var AxBaseAI = class {
1098
1286
  if (chatReq.functions && chatReq.functions.length > 0) {
1099
1287
  functions = chatReq.functions.map((fn2) => this.cleanupFunctionSchema(fn2));
1100
1288
  }
1101
- validateChatPrompt(chatReq.chatPrompt);
1102
1289
  const req = {
1103
1290
  ...chatReq,
1104
1291
  model,
@@ -1149,11 +1336,14 @@ var AxBaseAI = class {
1149
1336
  const res2 = respFn(resp, state);
1150
1337
  res2.sessionId = options?.sessionId;
1151
1338
  if (!res2.modelUsage) {
1152
- res2.modelUsage = {
1153
- ai: this.name,
1154
- model,
1155
- tokens: this.aiImpl.getTokenUsage()
1156
- };
1339
+ const tokenUsage = this.aiImpl.getTokenUsage();
1340
+ if (tokenUsage) {
1341
+ res2.modelUsage = {
1342
+ ai: this.name,
1343
+ model,
1344
+ tokens: tokenUsage
1345
+ };
1346
+ }
1157
1347
  }
1158
1348
  this.modelUsage = res2.modelUsage;
1159
1349
  if (span?.isRecording()) {
@@ -1287,11 +1477,14 @@ var AxBaseAI = class {
1287
1477
  const res = this.aiImpl.createEmbedResp(resValue);
1288
1478
  res.sessionId = options?.sessionId;
1289
1479
  if (!res.modelUsage) {
1290
- res.modelUsage = {
1291
- ai: this.name,
1292
- model: embedModel,
1293
- tokens: this.aiImpl.getTokenUsage()
1294
- };
1480
+ const tokenUsage = this.aiImpl.getTokenUsage();
1481
+ if (tokenUsage) {
1482
+ res.modelUsage = {
1483
+ ai: this.name,
1484
+ model: embedModel,
1485
+ tokens: tokenUsage
1486
+ };
1487
+ }
1295
1488
  }
1296
1489
  this.embedModelUsage = res.modelUsage;
1297
1490
  if (span?.isRecording() && res.modelUsage?.tokens) {
@@ -1455,16 +1648,6 @@ function validateAxMessageArray(values) {
1455
1648
  }
1456
1649
  }
1457
1650
  }
1458
- function validateChatPrompt(chatPrompt) {
1459
- for (let i = 0; i < chatPrompt.length; i++) {
1460
- const message = chatPrompt[i];
1461
- if (message && "content" in message && typeof message.content === "string" && message.content.trim() === "") {
1462
- throw new Error(
1463
- `Chat prompt validation failed: Message at index ${i} has empty content`
1464
- );
1465
- }
1466
- }
1467
- }
1468
1651
  function validateModels(models) {
1469
1652
  const keys = /* @__PURE__ */ new Set();
1470
1653
  for (const model of models) {
@@ -1536,14 +1719,18 @@ var axModelInfoAnthropic = [
1536
1719
  currency: "usd",
1537
1720
  promptTokenCostPer1M: 15,
1538
1721
  completionTokenCostPer1M: 75,
1539
- maxTokens: 32e3
1722
+ maxTokens: 32e3,
1723
+ hasThinkingBudget: true,
1724
+ hasShowThoughts: true
1540
1725
  },
1541
1726
  {
1542
1727
  name: "claude-sonnet-4-20250514" /* Claude4Sonnet */,
1543
1728
  currency: "usd",
1544
1729
  promptTokenCostPer1M: 3,
1545
1730
  completionTokenCostPer1M: 15,
1546
- maxTokens: 64e3
1731
+ maxTokens: 64e3,
1732
+ hasThinkingBudget: true,
1733
+ hasShowThoughts: true
1547
1734
  },
1548
1735
  // 3.7
1549
1736
  {
@@ -1551,7 +1738,9 @@ var axModelInfoAnthropic = [
1551
1738
  currency: "usd",
1552
1739
  promptTokenCostPer1M: 3,
1553
1740
  completionTokenCostPer1M: 15,
1554
- maxTokens: 64e3
1741
+ maxTokens: 64e3,
1742
+ hasThinkingBudget: true,
1743
+ hasShowThoughts: true
1555
1744
  },
1556
1745
  // 3.5
1557
1746
  {
@@ -1607,13 +1796,47 @@ var axModelInfoAnthropic = [
1607
1796
  }
1608
1797
  ];
1609
1798
 
1799
+ // dsp/modelinfo.ts
1800
+ function getModelInfo({
1801
+ model,
1802
+ modelInfo,
1803
+ models
1804
+ }) {
1805
+ const modelEntry = models?.find((v) => v.key === model);
1806
+ const mappedModel = modelEntry && "model" in modelEntry ? modelEntry.model : model;
1807
+ const exactMatch = modelInfo.find((v) => v.name === model);
1808
+ if (exactMatch) return exactMatch;
1809
+ const normalizedName = mappedModel.replace(/^(anthropic\.|openai\.)/, "").replace(/-latest$/, "").replace(/-\d{8}$/, "").replace(/-v\d+:\d+$/, "").replace(/@\d{8}$/, "").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/, "").replace(/-v\d+@\d{8}$/, "").replace(/-v\d+$/, "");
1810
+ const normalizedMatch = modelInfo.find((v) => v.name === normalizedName);
1811
+ if (normalizedMatch) return normalizedMatch;
1812
+ return null;
1813
+ }
1814
+
1610
1815
  // ai/anthropic/api.ts
1611
1816
  var axAIAnthropicDefaultConfig = () => structuredClone({
1612
1817
  model: "claude-3-7-sonnet-latest" /* Claude37Sonnet */,
1818
+ maxTokens: 4e4,
1819
+ // Ensure maxTokens is higher than highest thinking budget
1820
+ thinkingTokenBudgetLevels: {
1821
+ minimal: 1024,
1822
+ low: 5e3,
1823
+ medium: 1e4,
1824
+ high: 2e4,
1825
+ highest: 32e3
1826
+ },
1613
1827
  ...axBaseAIDefaultConfig()
1614
1828
  });
1615
1829
  var axAIAnthropicVertexDefaultConfig = () => structuredClone({
1616
1830
  model: "claude-3-7-sonnet" /* Claude37Sonnet */,
1831
+ maxTokens: 4e4,
1832
+ // Ensure maxTokens is higher than highest thinking budget
1833
+ thinkingTokenBudgetLevels: {
1834
+ minimal: 1024,
1835
+ low: 5e3,
1836
+ medium: 1e4,
1837
+ high: 2e4,
1838
+ highest: 32e3
1839
+ },
1617
1840
  ...axBaseAIDefaultConfig()
1618
1841
  });
1619
1842
  var AxAIAnthropicImpl = class {
@@ -1622,6 +1845,7 @@ var AxAIAnthropicImpl = class {
1622
1845
  this.isVertex = isVertex;
1623
1846
  }
1624
1847
  tokensUsed;
1848
+ currentPromptConfig;
1625
1849
  getTokenUsage() {
1626
1850
  return this.tokensUsed;
1627
1851
  }
@@ -1640,7 +1864,8 @@ var AxAIAnthropicImpl = class {
1640
1864
  n: config.n
1641
1865
  };
1642
1866
  }
1643
- createChatReq = (req) => {
1867
+ createChatReq = (req, config) => {
1868
+ this.currentPromptConfig = config;
1644
1869
  const model = req.model;
1645
1870
  const stream = req.modelConfig?.stream ?? this.config.stream;
1646
1871
  let apiConfig;
@@ -1696,17 +1921,67 @@ var AxAIAnthropicImpl = class {
1696
1921
  const temperature = req.modelConfig?.temperature ?? this.config.temperature;
1697
1922
  const topP = req.modelConfig?.topP ?? this.config.topP;
1698
1923
  const topK = req.modelConfig?.topK ?? this.config.topK;
1924
+ const n = req.modelConfig?.n ?? this.config.n;
1925
+ if (n && n > 1) {
1926
+ throw new Error("Anthropic does not support sampling (n > 1)");
1927
+ }
1928
+ let thinkingConfig;
1929
+ if (this.config.thinking?.budget_tokens) {
1930
+ thinkingConfig = this.config.thinking;
1931
+ }
1932
+ if (config?.thinkingTokenBudget) {
1933
+ const levels = this.config.thinkingTokenBudgetLevels;
1934
+ switch (config.thinkingTokenBudget) {
1935
+ case "none":
1936
+ thinkingConfig = void 0;
1937
+ break;
1938
+ case "minimal":
1939
+ thinkingConfig = {
1940
+ type: "enabled",
1941
+ budget_tokens: levels?.minimal ?? 1024
1942
+ };
1943
+ break;
1944
+ case "low":
1945
+ thinkingConfig = {
1946
+ type: "enabled",
1947
+ budget_tokens: levels?.low ?? 5e3
1948
+ };
1949
+ break;
1950
+ case "medium":
1951
+ thinkingConfig = {
1952
+ type: "enabled",
1953
+ budget_tokens: levels?.medium ?? 1e4
1954
+ };
1955
+ break;
1956
+ case "high":
1957
+ thinkingConfig = {
1958
+ type: "enabled",
1959
+ budget_tokens: levels?.high ?? 2e4
1960
+ };
1961
+ break;
1962
+ case "highest":
1963
+ thinkingConfig = {
1964
+ type: "enabled",
1965
+ budget_tokens: levels?.highest ?? 32e3
1966
+ };
1967
+ break;
1968
+ }
1969
+ }
1699
1970
  const reqValue = {
1700
1971
  ...this.isVertex ? { anthropic_version: "vertex-2023-10-16" } : { model },
1701
1972
  ...maxTokens ? { max_tokens: maxTokens } : {},
1702
1973
  ...stopSequences && stopSequences.length > 0 ? { stop_sequences: stopSequences } : {},
1703
- ...temperature ? { temperature } : {},
1704
- ...topP ? { top_p: topP } : {},
1705
- ...topK ? { top_k: topK } : {},
1974
+ // Only include temperature when thinking is not enabled
1975
+ ...temperature && !thinkingConfig ? { temperature } : {},
1976
+ // Only include top_p when thinking is not enabled, or when it's >= 0.95
1977
+ ...topP && (!thinkingConfig || topP >= 0.95) ? { top_p: topP } : {},
1978
+ // Only include top_k when thinking is not enabled
1979
+ ...topK && !thinkingConfig ? { top_k: topK } : {},
1706
1980
  ...toolsChoice,
1707
1981
  ...tools && tools.length > 0 ? { tools } : {},
1708
1982
  ...stream ? { stream: true } : {},
1709
1983
  ...system ? { system } : {},
1984
+ ...thinkingConfig ? { thinking: thinkingConfig } : {},
1710
1985
  messages
1711
1986
  };
1712
1987
  return [apiConfig, reqValue];
@@ -1716,9 +1991,11 @@ var AxAIAnthropicImpl = class {
1716
1991
  throw new Error(`Anthropic Chat API Error: ${resp.error.message}`);
1717
1992
  }
1718
1993
  const finishReason = mapFinishReason(resp.stop_reason);
1719
- const results = resp.content.map((msg) => {
1994
+ const showThoughts = this.currentPromptConfig?.thinkingTokenBudget !== "none" && this.currentPromptConfig?.showThoughts !== false;
1995
+ const results = resp.content.map((msg, index) => {
1720
1996
  if (msg.type === "tool_use") {
1721
1997
  return {
1998
+ index,
1722
1999
  id: msg.id,
1723
2000
  functionCalls: [
1724
2001
  {
@@ -1733,12 +2010,23 @@ var AxAIAnthropicImpl = class {
1733
2010
  finishReason
1734
2011
  };
1735
2012
  }
2013
+ if ((msg.type === "thinking" || msg.type === "redacted_thinking") && showThoughts) {
2014
+ return {
2015
+ index,
2016
+ thought: msg.thinking,
2017
+ id: resp.id,
2018
+ finishReason
2019
+ };
2020
+ }
1736
2021
  return {
2022
+ index,
1737
2023
  content: msg.type === "text" ? msg.text : "",
1738
2024
  id: resp.id,
1739
2025
  finishReason
1740
2026
  };
1741
- });
2027
+ }).filter(
2028
+ (result) => result.content !== "" || result.thought !== void 0 || result.functionCalls !== void 0
2029
+ );
1742
2030
  this.tokensUsed = {
1743
2031
  promptTokens: resp.usage.input_tokens,
1744
2032
  completionTokens: resp.usage.output_tokens,
@@ -1758,9 +2046,10 @@ var AxAIAnthropicImpl = class {
1758
2046
  const { error } = resp;
1759
2047
  throw new Error(error.message);
1760
2048
  }
2049
+ const index = 0;
1761
2050
  if (resp.type === "message_start") {
1762
2051
  const { message } = resp;
1763
- const results = [{ content: "", id: message.id }];
2052
+ const results = [{ index, content: "", id: message.id }];
1764
2053
  this.tokensUsed = {
1765
2054
  promptTokens: message.usage?.input_tokens ?? 0,
1766
2055
  completionTokens: message.usage?.output_tokens ?? 0,
@@ -1772,7 +2061,18 @@ var AxAIAnthropicImpl = class {
1772
2061
  const { content_block: contentBlock } = resp;
1773
2062
  if (contentBlock.type === "text") {
1774
2063
  return {
1775
- results: [{ content: contentBlock.text }]
2064
+ results: [{ index, content: contentBlock.text }]
2065
+ };
2066
+ }
2067
+ if (contentBlock.type === "thinking") {
2068
+ const showThoughts = this.currentPromptConfig?.thinkingTokenBudget !== "none" && this.currentPromptConfig?.showThoughts !== false;
2069
+ if (showThoughts) {
2070
+ return {
2071
+ results: [{ index, thought: contentBlock.thinking }]
2072
+ };
2073
+ }
2074
+ return {
2075
+ results: [{ index, content: "" }]
1776
2076
  };
1777
2077
  }
1778
2078
  if (contentBlock.type === "tool_use") {
@@ -1789,7 +2089,7 @@ var AxAIAnthropicImpl = class {
1789
2089
  }
1790
2090
  ];
1791
2091
  return {
1792
- results: [{ functionCalls }]
2092
+ results: [{ index, functionCalls }]
1793
2093
  };
1794
2094
  }
1795
2095
  }
@@ -1798,7 +2098,23 @@ var AxAIAnthropicImpl = class {
1798
2098
  const { delta } = resp;
1799
2099
  if (delta.type === "text_delta") {
1800
2100
  return {
1801
- results: [{ content: delta.text }]
2101
+ results: [{ index, content: delta.text }]
2102
+ };
2103
+ }
2104
+ if (delta.type === "thinking_delta") {
2105
+ const showThoughts = this.currentPromptConfig?.thinkingTokenBudget !== "none" && this.currentPromptConfig?.showThoughts !== false;
2106
+ if (showThoughts) {
2107
+ return {
2108
+ results: [{ index, thought: delta.thinking }]
2109
+ };
2110
+ }
2111
+ return {
2112
+ results: [{ index, content: "" }]
2113
+ };
2114
+ }
2115
+ if (delta.type === "signature_delta") {
2116
+ return {
2117
+ results: [{ index, content: "" }]
1802
2118
  };
1803
2119
  }
1804
2120
  if (delta.type === "input_json_delta") {
@@ -1817,7 +2133,7 @@ var AxAIAnthropicImpl = class {
1817
2133
  }
1818
2134
  ];
1819
2135
  return {
1820
- results: [{ functionCalls }]
2136
+ results: [{ index, functionCalls }]
1821
2137
  };
1822
2138
  }
1823
2139
  }
@@ -1829,12 +2145,16 @@ var AxAIAnthropicImpl = class {
1829
2145
  totalTokens: usage.output_tokens
1830
2146
  };
1831
2147
  const results = [
1832
- { content: "", finishReason: mapFinishReason(delta.stop_reason) }
2148
+ {
2149
+ index,
2150
+ content: "",
2151
+ finishReason: mapFinishReason(delta.stop_reason)
2152
+ }
1833
2153
  ];
1834
2154
  return { results };
1835
2155
  }
1836
2156
  return {
1837
- results: [{ content: "" }]
2157
+ results: [{ index, content: "" }]
1838
2158
  };
1839
2159
  };
1840
2160
  };
@@ -1876,6 +2196,20 @@ var AxAIAnthropic = class extends AxBaseAI {
1876
2196
  ...config
1877
2197
  };
1878
2198
  const aiImpl = new AxAIAnthropicImpl(_config, isVertex);
2199
+ const supportFor = (model) => {
2200
+ const mi = getModelInfo({
2201
+ model,
2202
+ modelInfo: axModelInfoAnthropic,
2203
+ models
2204
+ });
2205
+ return {
2206
+ functions: true,
2207
+ streaming: true,
2208
+ hasThinkingBudget: mi?.hasThinkingBudget ?? false,
2209
+ hasShowThoughts: mi?.hasShowThoughts ?? false,
2210
+ functionCot: true
2211
+ };
2212
+ };
1879
2213
  super(aiImpl, {
1880
2214
  name: "Anthropic",
1881
2215
  apiURL,
@@ -1883,7 +2217,7 @@ var AxAIAnthropic = class extends AxBaseAI {
1883
2217
  modelInfo: axModelInfoAnthropic,
1884
2218
  defaults: { model: _config.model },
1885
2219
  options,
1886
- supportFor: { functions: true, streaming: true, functionCot: true },
2220
+ supportFor,
1887
2221
  models
1888
2222
  });
1889
2223
  }
@@ -2013,22 +2347,6 @@ function mapFinishReason(stopReason) {
2013
2347
  }
2014
2348
  }
2015
2349
 
2016
- // dsp/modelinfo.ts
2017
- function getModelInfo({
2018
- model,
2019
- modelInfo,
2020
- models
2021
- }) {
2022
- const modelEntry = models?.find((v) => v.key === model);
2023
- const mappedModel = modelEntry && "model" in modelEntry ? modelEntry.model : model;
2024
- const exactMatch = modelInfo.find((v) => v.name === model);
2025
- if (exactMatch) return exactMatch;
2026
- const normalizedName = mappedModel.replace(/^(anthropic\.|openai\.)/, "").replace(/-latest$/, "").replace(/-\d{8}$/, "").replace(/-v\d+:\d+$/, "").replace(/@\d{8}$/, "").replace(/-\d{2,}(-[a-zA-Z0-9-]+)?$/, "").replace(/-v\d+@\d{8}$/, "").replace(/-v\d+$/, "");
2027
- const normalizedMatch = modelInfo.find((v) => v.name === normalizedName);
2028
- if (normalizedMatch) return normalizedMatch;
2029
- return null;
2030
- }
2031
-
2032
2350
  // ai/openai/chat_types.ts
2033
2351
  var AxAIOpenAIModel = /* @__PURE__ */ ((AxAIOpenAIModel2) => {
2034
2352
  AxAIOpenAIModel2["O1"] = "o1";
@@ -2363,6 +2681,7 @@ var AxAIOpenAIImpl = class {
2363
2681
  })
2364
2682
  );
2365
2683
  return {
2684
+ index: choice.index,
2366
2685
  id: `${choice.index}`,
2367
2686
  content: choice.message.content,
2368
2687
  thought: choice.message.reasoning_content,
@@ -2388,6 +2707,7 @@ var AxAIOpenAIImpl = class {
2388
2707
  }
2389
2708
  const results = choices.map(
2390
2709
  ({
2710
+ index,
2391
2711
  delta: {
2392
2712
  content,
2393
2713
  role,
@@ -2397,11 +2717,11 @@ var AxAIOpenAIImpl = class {
2397
2717
  finish_reason: oaiFinishReason
2398
2718
  }) => {
2399
2719
  const finishReason = mapFinishReason2(oaiFinishReason);
2400
- const functionCalls = toolCalls?.map(({ id: _id, index, function: { name, arguments: params } }) => {
2401
- if (typeof _id === "string" && typeof index === "number" && !sstate.indexIdMap[index]) {
2402
- sstate.indexIdMap[index] = _id;
2720
+ const functionCalls = toolCalls?.map(({ id: _id, index: index2, function: { name, arguments: params } }) => {
2721
+ if (typeof _id === "string" && typeof index2 === "number" && !sstate.indexIdMap[index2]) {
2722
+ sstate.indexIdMap[index2] = _id;
2403
2723
  }
2404
- const id2 = sstate.indexIdMap[index];
2724
+ const id2 = sstate.indexIdMap[index2];
2405
2725
  if (!id2) {
2406
2726
  return null;
2407
2727
  }
@@ -2412,6 +2732,7 @@ var AxAIOpenAIImpl = class {
2412
2732
  };
2413
2733
  }).filter((v) => v !== null);
2414
2734
  return {
2735
+ index,
2415
2736
  content,
2416
2737
  role,
2417
2738
  thought,
@@ -2446,7 +2767,7 @@ var mapFinishReason2 = (finishReason) => {
2446
2767
  }
2447
2768
  };
2448
2769
  function createMessages2(req) {
2449
- return req.chatPrompt.map((msg) => {
2770
+ const openaiReq = req.chatPrompt.map((msg) => {
2450
2771
  switch (msg.role) {
2451
2772
  case "system":
2452
2773
  return { role: "system", content: msg.content };
@@ -2495,7 +2816,7 @@ function createMessages2(req) {
2495
2816
  tool_calls: toolCalls
2496
2817
  };
2497
2818
  }
2498
- if (!msg.content) {
2819
+ if (msg.content === void 0) {
2499
2820
  throw new Error(
2500
2821
  "Assistant content is required when no tool calls are provided"
2501
2822
  );
@@ -2515,6 +2836,7 @@ function createMessages2(req) {
2515
2836
  throw new Error("Invalid role");
2516
2837
  }
2517
2838
  });
2839
+ return openaiReq;
2518
2840
  }
2519
2841
  var AxAIOpenAIBase = class extends AxBaseAI {
2520
2842
  constructor({
@@ -2868,6 +3190,7 @@ var AxAICohereImpl = class {
2868
3190
  }
2869
3191
  const results = [
2870
3192
  {
3193
+ index: 0,
2871
3194
  id: resp.generation_id,
2872
3195
  content: resp.text,
2873
3196
  functionCalls,
@@ -3500,7 +3823,7 @@ var AxAIGoogleGeminiImpl = class {
3500
3823
  createChatResp = (resp) => {
3501
3824
  const results = resp.candidates?.map(
3502
3825
  (candidate) => {
3503
- const result = {};
3826
+ const result = { index: 0 };
3504
3827
  switch (candidate.finishReason) {
3505
3828
  case "MAX_TOKENS":
3506
3829
  result.finishReason = "length";
@@ -3897,6 +4220,7 @@ ${fc}`;
3897
4220
  return {
3898
4221
  results: [
3899
4222
  {
4223
+ index: 0,
3900
4224
  content: resp.generated_text
3901
4225
  }
3902
4226
  ]
@@ -4538,7 +4862,7 @@ var AxAIOpenAIResponsesImpl = class {
4538
4862
  }
4539
4863
  }
4540
4864
  return {
4541
- results: [currentResult],
4865
+ results: [{ ...currentResult, index: 0 }],
4542
4866
  remoteId: id
4543
4867
  };
4544
4868
  }
@@ -4546,6 +4870,7 @@ var AxAIOpenAIResponsesImpl = class {
4546
4870
  createChatStreamResp(streamEvent) {
4547
4871
  const event = streamEvent;
4548
4872
  const baseResult = {
4873
+ index: 0,
4549
4874
  id: "",
4550
4875
  content: "",
4551
4876
  finishReason: "stop"
@@ -5113,7 +5438,7 @@ var AxAIRekaImpl = class {
5113
5438
  completionTokens: usage.output_tokens,
5114
5439
  totalTokens: usage.input_tokens + usage.output_tokens
5115
5440
  } : void 0;
5116
- const results = responses.map((res) => {
5441
+ const results = responses.map((res, index) => {
5117
5442
  const finishReason = mapFinishReason3(res.finish_reason);
5118
5443
  let content;
5119
5444
  if (typeof res.message.content === "string") {
@@ -5122,6 +5447,7 @@ var AxAIRekaImpl = class {
5122
5447
  content = res.message.content.text;
5123
5448
  }
5124
5449
  return {
5450
+ index,
5125
5451
  id: `${id}`,
5126
5452
  content,
5127
5453
  finishReason
@@ -5136,7 +5462,7 @@ var AxAIRekaImpl = class {
5136
5462
  completionTokens: usage.output_tokens,
5137
5463
  totalTokens: usage.input_tokens + usage.output_tokens
5138
5464
  } : void 0;
5139
- const results = responses.map((res) => {
5465
+ const results = responses.map((res, index) => {
5140
5466
  const finishReason = mapFinishReason3(res.finish_reason);
5141
5467
  let content;
5142
5468
  if (typeof res.chunk.content === "string") {
@@ -5145,6 +5471,7 @@ var AxAIRekaImpl = class {
5145
5471
  content = res.chunk.content.text;
5146
5472
  }
5147
5473
  return {
5474
+ index,
5148
5475
  id: `${id}`,
5149
5476
  content,
5150
5477
  finishReason
@@ -5498,111 +5825,325 @@ var AxAIGrok = class extends AxAIOpenAIBase {
5498
5825
  };
5499
5826
 
5500
5827
  // dsp/generate.ts
5501
- import { ReadableStream as ReadableStream2 } from "stream/web";
5828
+ import { ReadableStream as ReadableStream3 } from "stream/web";
5502
5829
  import {
5503
5830
  context as context2,
5504
5831
  SpanKind as SpanKind2,
5505
5832
  trace
5506
5833
  } from "@opentelemetry/api";
5507
5834
 
5508
- // ai/util.ts
5509
- function mergeFunctionCalls(functionCalls, functionCallDeltas) {
5510
- for (const _fc of functionCallDeltas) {
5511
- const fc = functionCalls.find((fc2) => fc2.id === _fc.id);
5512
- if (fc) {
5513
- if (typeof _fc.function.name == "string" && _fc.function.name.length > 0) {
5514
- fc.function.name += _fc.function.name;
5515
- }
5516
- if (typeof _fc.function.params == "string" && _fc.function.params.length > 0) {
5517
- fc.function.params += _fc.function.params;
5518
- }
5519
- if (typeof _fc.function.params == "object") {
5520
- fc.function.params = _fc.function.params;
5521
- }
5522
- } else {
5523
- functionCalls.push(_fc);
5524
- }
5525
- }
5526
- }
5527
-
5528
- // mem/memory.ts
5529
- var defaultLimit = 1e4;
5530
- var MemoryImpl = class {
5531
- constructor(limit = defaultLimit, options) {
5532
- this.limit = limit;
5533
- this.options = options;
5534
- if (limit <= 0) {
5535
- throw Error("argument 'limit' must be greater than 0");
5536
- }
5537
- }
5538
- data = [];
5539
- addMemory(value) {
5540
- if (Array.isArray(value)) {
5541
- this.data.push(...value.map((chat) => ({ chat: structuredClone(chat) })));
5542
- } else {
5543
- this.data.push({
5544
- chat: structuredClone(value)
5545
- });
5546
- }
5547
- if (this.data.length > this.limit) {
5548
- const removeCount = this.data.length - this.limit;
5549
- this.data.splice(0, removeCount);
5550
- }
5835
+ // ai/validate.ts
5836
+ function axValidateChatRequestMessage(item) {
5837
+ if (!item) {
5838
+ throw new Error("Chat request message item cannot be null or undefined");
5551
5839
  }
5552
- add(value) {
5553
- this.addMemory(value);
5554
- if (this.options?.debug) {
5555
- debugRequest(value, this.options?.debugHideSystemPrompt);
5556
- }
5840
+ if (!item.role) {
5841
+ throw new Error("Chat request message must have a role");
5557
5842
  }
5558
- addResultMessage({
5559
- content,
5560
- name,
5561
- functionCalls
5562
- }) {
5563
- const isContentEmpty = typeof content === "string" && content.trim() === "";
5564
- if (isContentEmpty) {
5565
- this.addMemory({ name, role: "assistant", functionCalls });
5566
- } else {
5567
- this.addMemory({ content, name, role: "assistant", functionCalls });
5843
+ switch (item.role) {
5844
+ case "system":
5845
+ if (!item.content || item.content.trim() === "") {
5846
+ throw new Error(
5847
+ "System message content cannot be empty or whitespace-only"
5848
+ );
5849
+ }
5850
+ break;
5851
+ case "user":
5852
+ if (!item.content) {
5853
+ throw new Error("User message content cannot be undefined");
5854
+ }
5855
+ if (typeof item.content === "string") {
5856
+ if (item.content.trim() === "") {
5857
+ throw new Error(
5858
+ "User message content cannot be empty or whitespace-only"
5859
+ );
5860
+ }
5861
+ } else if (Array.isArray(item.content)) {
5862
+ if (item.content.length === 0) {
5863
+ throw new Error("User message content array cannot be empty");
5864
+ }
5865
+ for (let index = 0; index < item.content.length; index++) {
5866
+ const contentItem = item.content[index];
5867
+ if (!contentItem || typeof contentItem !== "object") {
5868
+ throw new Error(
5869
+ `User message content item at index ${index} must be an object`
5870
+ );
5871
+ }
5872
+ if (!contentItem.type) {
5873
+ throw new Error(
5874
+ `User message content item at index ${index} must have a type`
5875
+ );
5876
+ }
5877
+ switch (contentItem.type) {
5878
+ case "text":
5879
+ if (!contentItem.text || contentItem.text.trim() === "") {
5880
+ throw new Error(
5881
+ `User message text content at index ${index} cannot be empty or whitespace-only`
5882
+ );
5883
+ }
5884
+ break;
5885
+ case "image":
5886
+ if (!contentItem.image || contentItem.image.trim() === "") {
5887
+ throw new Error(
5888
+ `User message image content at index ${index} cannot be empty`
5889
+ );
5890
+ }
5891
+ if (!contentItem.mimeType || contentItem.mimeType.trim() === "") {
5892
+ throw new Error(
5893
+ `User message image content at index ${index} must have a mimeType`
5894
+ );
5895
+ }
5896
+ break;
5897
+ case "audio":
5898
+ if (!contentItem.data || contentItem.data.trim() === "") {
5899
+ throw new Error(
5900
+ `User message audio content at index ${index} cannot be empty`
5901
+ );
5902
+ }
5903
+ break;
5904
+ default:
5905
+ throw new Error(
5906
+ `User message content item at index ${index} has unsupported type: ${contentItem.type}`
5907
+ );
5908
+ }
5909
+ }
5910
+ } else {
5911
+ throw new Error(
5912
+ "User message content must be a string or array of content objects"
5913
+ );
5914
+ }
5915
+ break;
5916
+ case "assistant":
5917
+ if (!item.content && !item.functionCalls) {
5918
+ throw new Error(
5919
+ "Assistant message must have either content or function calls"
5920
+ );
5921
+ }
5922
+ if (item.content && typeof item.content !== "string") {
5923
+ throw new Error("Assistant message content must be a string");
5924
+ }
5925
+ if (item.functionCalls && !Array.isArray(item.functionCalls)) {
5926
+ throw new Error("Assistant message function calls must be an array");
5927
+ }
5928
+ break;
5929
+ case "function":
5930
+ if (!item.functionId || item.functionId.trim() === "") {
5931
+ throw new Error("Function message must have a non-empty functionId");
5932
+ }
5933
+ if (item.result === void 0 || item.result === null) {
5934
+ throw new Error("Function message must have a result");
5935
+ }
5936
+ if (typeof item.result !== "string") {
5937
+ throw new Error("Function message result must be a string");
5938
+ }
5939
+ break;
5940
+ default:
5941
+ throw new Error(
5942
+ `Unsupported message role: ${item.role}`
5943
+ );
5944
+ }
5945
+ }
5946
+ function axValidateChatResponseResult(results) {
5947
+ const resultsArray = Array.isArray(results) ? results : [results];
5948
+ if (resultsArray.length === 0) {
5949
+ throw new Error("Chat response results cannot be empty");
5950
+ }
5951
+ for (let arrayIndex = 0; arrayIndex < resultsArray.length; arrayIndex++) {
5952
+ const result = resultsArray[arrayIndex];
5953
+ if (!result) {
5954
+ throw new Error(
5955
+ `Chat response result at index ${arrayIndex} cannot be null or undefined`
5956
+ );
5957
+ }
5958
+ if (typeof result.index !== "number") {
5959
+ throw new Error(
5960
+ `Chat response result at index ${arrayIndex} must have a numeric index`
5961
+ );
5962
+ }
5963
+ if (result.index < 0) {
5964
+ throw new Error(
5965
+ `Chat response result at index ${arrayIndex} must have a non-negative index`
5966
+ );
5967
+ }
5968
+ if (!result.content && !result.thought && !result.functionCalls && !result.finishReason) {
5969
+ throw new Error(
5970
+ `Chat response result at index ${arrayIndex} must have at least one of: content, thought, functionCalls, or finishReason`
5971
+ );
5972
+ }
5973
+ if (result.content !== void 0 && typeof result.content !== "string") {
5974
+ throw new Error(
5975
+ `Chat response result content at index ${arrayIndex} must be a string`
5976
+ );
5977
+ }
5978
+ if (result.thought !== void 0 && typeof result.thought !== "string") {
5979
+ throw new Error(
5980
+ `Chat response result thought at index ${arrayIndex} must be a string`
5981
+ );
5982
+ }
5983
+ if (result.name !== void 0) {
5984
+ if (typeof result.name !== "string") {
5985
+ throw new Error(
5986
+ `Chat response result name at index ${arrayIndex} must be a string`
5987
+ );
5988
+ }
5989
+ if (result.name.trim() === "") {
5990
+ throw new Error(
5991
+ `Chat response result name at index ${arrayIndex} cannot be empty or whitespace-only`
5992
+ );
5993
+ }
5994
+ }
5995
+ if (result.id !== void 0) {
5996
+ if (typeof result.id !== "string") {
5997
+ throw new Error(
5998
+ `Chat response result id at index ${arrayIndex} must be a string`
5999
+ );
6000
+ }
6001
+ if (result.id.trim() === "") {
6002
+ throw new Error(
6003
+ `Chat response result id at index ${arrayIndex} cannot be empty or whitespace-only`
6004
+ );
6005
+ }
6006
+ }
6007
+ if (result.functionCalls !== void 0) {
6008
+ if (!Array.isArray(result.functionCalls)) {
6009
+ throw new Error(
6010
+ `Chat response result functionCalls at index ${arrayIndex} must be an array`
6011
+ );
6012
+ }
6013
+ for (let callIndex = 0; callIndex < result.functionCalls.length; callIndex++) {
6014
+ const functionCall = result.functionCalls[callIndex];
6015
+ if (!functionCall) {
6016
+ throw new Error(
6017
+ `Function call at index ${callIndex} in result ${arrayIndex} cannot be null or undefined`
6018
+ );
6019
+ }
6020
+ if (!functionCall.id || typeof functionCall.id !== "string" || functionCall.id.trim() === "") {
6021
+ throw new Error(
6022
+ `Function call at index ${callIndex} in result ${arrayIndex} must have a non-empty string id`
6023
+ );
6024
+ }
6025
+ if (functionCall.type !== "function") {
6026
+ throw new Error(
6027
+ `Function call at index ${callIndex} in result ${arrayIndex} must have type 'function'`
6028
+ );
6029
+ }
6030
+ if (!functionCall.function) {
6031
+ throw new Error(
6032
+ `Function call at index ${callIndex} in result ${arrayIndex} must have a function object`
6033
+ );
6034
+ }
6035
+ if (!functionCall.function.name || typeof functionCall.function.name !== "string" || functionCall.function.name.trim() === "") {
6036
+ throw new Error(
6037
+ `Function call at index ${callIndex} in result ${arrayIndex} must have a non-empty function name`
6038
+ );
6039
+ }
6040
+ if (functionCall.function.params !== void 0) {
6041
+ if (typeof functionCall.function.params !== "string" && typeof functionCall.function.params !== "object") {
6042
+ throw new Error(
6043
+ `Function call params at index ${callIndex} in result ${arrayIndex} must be a string or object`
6044
+ );
6045
+ }
6046
+ }
6047
+ }
6048
+ }
6049
+ if (result.finishReason !== void 0) {
6050
+ const validFinishReasons = [
6051
+ "stop",
6052
+ "length",
6053
+ "function_call",
6054
+ "content_filter",
6055
+ "error"
6056
+ ];
6057
+ if (!validFinishReasons.includes(result.finishReason)) {
6058
+ throw new Error(
6059
+ `Chat response result finishReason at index ${arrayIndex} must be one of: ${validFinishReasons.join(", ")}`
6060
+ );
6061
+ }
5568
6062
  }
5569
6063
  }
5570
- addResult({
5571
- content,
5572
- name,
5573
- functionCalls
5574
- }) {
5575
- this.addResultMessage({ content, name, functionCalls });
6064
+ }
6065
+
6066
+ // mem/memory.ts
6067
+ var MemoryImpl = class {
6068
+ constructor(options) {
6069
+ this.options = options;
6070
+ }
6071
+ data = [];
6072
+ addRequest(items, index) {
6073
+ this.data.push(
6074
+ ...items.map((item) => {
6075
+ const value = structuredClone(item);
6076
+ return {
6077
+ role: item.role,
6078
+ chat: [{ index, value }]
6079
+ };
6080
+ })
6081
+ );
6082
+ if (this.options?.debug) {
6083
+ debugRequest(items, this.options?.debugHideSystemPrompt);
6084
+ }
6085
+ }
6086
+ addResponse(results) {
6087
+ const chat = results.map((result) => ({
6088
+ index: result.index,
6089
+ value: structuredClone(result)
6090
+ }));
6091
+ this.data.push({
6092
+ role: "assistant",
6093
+ chat
6094
+ });
5576
6095
  if (this.options?.debug) {
5577
- debugResponse({ content, name, functionCalls });
6096
+ for (const result of results) {
6097
+ debugResponse(result);
6098
+ }
5578
6099
  }
5579
6100
  }
5580
6101
  updateResult({
5581
6102
  content,
5582
6103
  name,
5583
6104
  functionCalls,
5584
- delta
6105
+ delta,
6106
+ index
5585
6107
  }) {
5586
6108
  const lastItem = this.data.at(-1);
5587
- if (!lastItem || lastItem.chat.role !== "assistant") {
5588
- throw new Error("No assistant message to update");
6109
+ const log = () => {
6110
+ if (this.options?.debug) {
6111
+ if (delta && typeof delta === "string") {
6112
+ debugResponseDelta(delta);
6113
+ } else if (!delta && (content || functionCalls)) {
6114
+ debugResponse({ content, name, functionCalls, index });
6115
+ }
6116
+ }
6117
+ };
6118
+ if (!lastItem || lastItem.role !== "assistant") {
6119
+ this.data.push({
6120
+ role: "assistant",
6121
+ chat: [
6122
+ { index, value: structuredClone({ content, name, functionCalls }) }
6123
+ ]
6124
+ });
6125
+ log();
6126
+ return;
5589
6127
  }
5590
- if (typeof content === "string" && content.trim() !== "") {
5591
- lastItem.chat.content = content;
6128
+ const chat = lastItem.chat.find((v) => v.index === index);
6129
+ if (!chat) {
6130
+ lastItem.chat.push({
6131
+ index,
6132
+ value: structuredClone({ content, name, functionCalls })
6133
+ });
6134
+ log();
6135
+ return;
5592
6136
  }
5593
- if (name && name.trim() !== "") {
5594
- lastItem.chat.name = name;
6137
+ if ("content" in chat.value && typeof content === "string" && content.trim() !== "") {
6138
+ chat.value.content = content;
5595
6139
  }
5596
- if (functionCalls && functionCalls.length > 0) {
5597
- lastItem.chat.functionCalls = functionCalls;
6140
+ if ("name" in chat.value && name && name.trim() !== "") {
6141
+ chat.value.name = name;
5598
6142
  }
5599
- if (this.options?.debug) {
5600
- if (delta && typeof delta === "string") {
5601
- debugResponseDelta(delta);
5602
- } else if (!delta && (content || functionCalls)) {
5603
- debugResponse({ content, name, functionCalls });
5604
- }
6143
+ if ("functionCalls" in chat.value && functionCalls && functionCalls.length > 0) {
6144
+ chat.value.functionCalls = functionCalls;
5605
6145
  }
6146
+ log();
5606
6147
  }
5607
6148
  addTag(name) {
5608
6149
  const lastItem = this.data.at(-1);
@@ -5621,8 +6162,7 @@ var MemoryImpl = class {
5621
6162
  if (tagIndex === -1) {
5622
6163
  throw new Error(`Tag "${name}" not found`);
5623
6164
  }
5624
- const removedItems = this.data.splice(tagIndex);
5625
- return removedItems.map((item) => item.chat);
6165
+ return this.data.splice(tagIndex);
5626
6166
  }
5627
6167
  removeByTag(name) {
5628
6168
  const indices = this.data.reduce((acc, item, index) => {
@@ -5634,28 +6174,29 @@ var MemoryImpl = class {
5634
6174
  if (indices.length === 0) {
5635
6175
  throw new Error(`No items found with tag "${name}"`);
5636
6176
  }
5637
- return indices.reverse().map((index) => this.data.splice(index, 1).at(0)?.chat).filter(Boolean).reverse();
6177
+ return indices.reverse().map((index) => this.data.splice(index, 1).at(0)).filter((item) => item !== void 0).reverse();
5638
6178
  }
5639
- history() {
5640
- return this.data.map((item) => item.chat);
6179
+ history(index) {
6180
+ const result = [];
6181
+ for (const { role, chat } of this.data) {
6182
+ const value = chat.find((v) => v.index === index)?.value;
6183
+ if (value) {
6184
+ result.push({ role, ...value });
6185
+ }
6186
+ }
6187
+ return result;
5641
6188
  }
5642
6189
  getLast() {
5643
- const lastItem = this.data.at(-1);
5644
- if (!lastItem) return void 0;
5645
- return {
5646
- chat: lastItem.chat,
5647
- tags: lastItem.tags
5648
- };
6190
+ return this.data.at(-1);
5649
6191
  }
5650
6192
  reset() {
5651
6193
  this.data = [];
5652
6194
  }
5653
6195
  };
5654
6196
  var AxMemory = class {
5655
- constructor(limit = defaultLimit, options) {
5656
- this.limit = limit;
6197
+ constructor(options) {
5657
6198
  this.options = options;
5658
- this.defaultMemory = new MemoryImpl(limit, options);
6199
+ this.defaultMemory = new MemoryImpl(options);
5659
6200
  }
5660
6201
  memories = /* @__PURE__ */ new Map();
5661
6202
  defaultMemory;
@@ -5664,15 +6205,34 @@ var AxMemory = class {
5664
6205
  return this.defaultMemory;
5665
6206
  }
5666
6207
  if (!this.memories.has(sessionId)) {
5667
- this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
6208
+ this.memories.set(sessionId, new MemoryImpl(this.options));
5668
6209
  }
5669
6210
  return this.memories.get(sessionId);
5670
6211
  }
5671
- add(value, sessionId) {
5672
- this.getMemory(sessionId).add(value);
5673
- }
5674
- addResult(result, sessionId) {
5675
- this.getMemory(sessionId).addResult(result);
6212
+ addRequest(value, sessionId) {
6213
+ for (const item of value) {
6214
+ axValidateChatRequestMessage(item);
6215
+ }
6216
+ this.getMemory(sessionId).addRequest(value, 0);
6217
+ }
6218
+ addResponse(results, sessionId) {
6219
+ axValidateChatResponseResult(results);
6220
+ this.getMemory(sessionId).addResponse(results);
6221
+ }
6222
+ addFunctionResult({
6223
+ functionId,
6224
+ isError,
6225
+ index,
6226
+ result
6227
+ }, sessionId) {
6228
+ const functionMessage = {
6229
+ role: "function",
6230
+ functionId,
6231
+ isError,
6232
+ result
6233
+ };
6234
+ axValidateChatRequestMessage(functionMessage);
6235
+ this.getMemory(sessionId).addRequest([functionMessage], index);
5676
6236
  }
5677
6237
  updateResult(result, sessionId) {
5678
6238
  this.getMemory(sessionId).updateResult(result);
@@ -5683,8 +6243,8 @@ var AxMemory = class {
5683
6243
  rewindToTag(name, sessionId) {
5684
6244
  return this.getMemory(sessionId).rewindToTag(name);
5685
6245
  }
5686
- history(sessionId) {
5687
- return this.getMemory(sessionId).history();
6246
+ history(index, sessionId) {
6247
+ return this.getMemory(sessionId).history(index);
5688
6248
  }
5689
6249
  getLast(sessionId) {
5690
6250
  return this.getMemory(sessionId).getLast();
@@ -5693,7 +6253,7 @@ var AxMemory = class {
5693
6253
  if (!sessionId) {
5694
6254
  this.defaultMemory.reset();
5695
6255
  } else {
5696
- this.memories.set(sessionId, new MemoryImpl(this.limit, this.options));
6256
+ this.memories.set(sessionId, new MemoryImpl(this.options));
5697
6257
  }
5698
6258
  }
5699
6259
  };
@@ -5774,86 +6334,236 @@ var assertStreamingAssertions = async (asserts, xstate, content, final = false)
5774
6334
  }
5775
6335
  };
5776
6336
 
5777
- // dsp/datetime.ts
5778
- import moment from "moment-timezone";
5779
-
5780
- // dsp/util.ts
5781
- var colorLog3 = new ColorLog();
5782
- var updateProgressBar = (current, total, success, elapsedTime, msg, progressBarWidth = 20) => {
5783
- const percentage = (current / total * 100).toFixed(1);
5784
- const filledBarLength = Math.round(progressBarWidth * current / total);
5785
- const emptyBarLength = progressBarWidth - filledBarLength;
5786
- const filledBar = colorLog3.blueBright("\u2588".repeat(filledBarLength));
5787
- const emptyBar = " ".repeat(emptyBarLength);
5788
- const itemsPerSecond = elapsedTime > 0 ? (current / elapsedTime).toFixed(2) : "0.00";
5789
- process.stdout.write(
5790
- `\r${msg}: ${current} / ${total} (${colorLog3.yellow(percentage)}%): 100%|${filledBar}${emptyBar}| Success: ${success}/${total} [${colorLog3.red(elapsedTime.toFixed(2))}, ${itemsPerSecond}it/s]`
5791
- );
5792
- };
5793
- var validateValue = (field, value) => {
5794
- const ft = field.type ?? { name: "string", isArray: false };
5795
- const validateSingleValue = (expectedType, val) => {
5796
- switch (expectedType) {
5797
- case "class":
5798
- return typeof val === "string";
5799
- case "code":
5800
- return typeof val === "string";
5801
- case "string":
5802
- return typeof val === "string";
5803
- case "number":
5804
- return typeof val === "number";
5805
- case "boolean":
5806
- return typeof val === "boolean";
5807
- case "date":
5808
- return val instanceof Date || typeof val === "string";
5809
- case "datetime":
5810
- return val instanceof Date || typeof val === "string";
5811
- case "json":
5812
- return typeof val === "object" || typeof val === "string";
5813
- default:
5814
- return false;
5815
- }
5816
- };
5817
- const validImage = (val) => {
5818
- if (!val || typeof val !== "object" || !("mimeType" in val) || !("data" in val)) {
5819
- return false;
5820
- }
5821
- return true;
5822
- };
5823
- if (field.type?.name === "image") {
5824
- let msg;
5825
- if (Array.isArray(value)) {
5826
- for (const item of value) {
5827
- if (!validImage(item)) {
5828
- msg = "object ({ mimeType: string; data: string })";
5829
- break;
5830
- }
5831
- }
5832
- } else if (!validImage(value)) {
5833
- msg = "object ({ mimeType: string; data: string })";
5834
- }
5835
- if (msg) {
5836
- throw new Error(
5837
- `Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
5838
- );
5839
- }
5840
- return;
6337
+ // dsp/errors.ts
6338
+ var ValidationError = class extends Error {
6339
+ fields;
6340
+ constructor({
6341
+ message,
6342
+ fields
6343
+ }) {
6344
+ super(message);
6345
+ this.fields = fields;
6346
+ this.name = this.constructor.name;
5841
6347
  }
5842
- const validAudio = (val) => {
5843
- if (!val || typeof val !== "object" || !("data" in val)) {
5844
- return false;
5845
- }
5846
- return true;
6348
+ getFixingInstructions = () => {
6349
+ const toFieldType2 = (type) => {
6350
+ const baseType = (() => {
6351
+ switch (type?.name) {
6352
+ case "string":
6353
+ return "string";
6354
+ case "number":
6355
+ return "number";
6356
+ case "boolean":
6357
+ return "boolean";
6358
+ case "date":
6359
+ return 'date ("YYYY-MM-DD" format)';
6360
+ case "datetime":
6361
+ return 'date time ("YYYY-MM-DD HH:mm Timezone" format)';
6362
+ case "json":
6363
+ return "JSON object";
6364
+ case "class":
6365
+ return "classification class";
6366
+ case "code":
6367
+ return "code";
6368
+ default:
6369
+ return "string";
6370
+ }
6371
+ })();
6372
+ return type?.isArray ? `json array of ${baseType} items` : baseType;
6373
+ };
6374
+ return this.fields.map((field) => ({
6375
+ name: "outputError",
6376
+ title: "Output Correction Required",
6377
+ description: `The section labeled '${field.title}' does not match the expected format of '${toFieldType2(field.type)}'. ${this.message} Please revise your response to ensure it conforms to the specified format.`
6378
+ }));
5847
6379
  };
5848
- if (field.type?.name === "audio") {
5849
- let msg;
5850
- if (Array.isArray(value)) {
5851
- for (const item of value) {
5852
- if (!validAudio(item)) {
5853
- msg = "object ({ data: string; format?: string })";
5854
- break;
6380
+ toString() {
6381
+ const toFieldType2 = (type) => {
6382
+ const baseType = (() => {
6383
+ switch (type?.name) {
6384
+ case "string":
6385
+ return "string";
6386
+ case "number":
6387
+ return "number";
6388
+ case "boolean":
6389
+ return "boolean";
6390
+ case "date":
6391
+ return 'date ("YYYY-MM-DD" format)';
6392
+ case "datetime":
6393
+ return 'date time ("YYYY-MM-DD HH:mm Timezone" format)';
6394
+ case "json":
6395
+ return "JSON object";
6396
+ case "class":
6397
+ return "classification class";
6398
+ case "code":
6399
+ return "code";
6400
+ default:
6401
+ return "string";
5855
6402
  }
5856
- }
6403
+ })();
6404
+ return type?.isArray ? `json array of ${baseType} items` : baseType;
6405
+ };
6406
+ return [
6407
+ `${this.name}: ${this.message}`,
6408
+ ...this.fields.map(
6409
+ (field) => ` - ${field.title}: Expected format '${toFieldType2(field.type)}'`
6410
+ )
6411
+ ].join("\n");
6412
+ }
6413
+ [Symbol.for("nodejs.util.inspect.custom")](_depth, _options) {
6414
+ return this.toString();
6415
+ }
6416
+ };
6417
+
6418
+ // dsp/datetime.ts
6419
+ import moment from "moment-timezone";
6420
+ function parseLLMFriendlyDate(field, dateStr, required = false) {
6421
+ try {
6422
+ return _parseLLMFriendlyDate(dateStr);
6423
+ } catch (err) {
6424
+ if (field.isOptional && !required) {
6425
+ return;
6426
+ }
6427
+ const message = err.message;
6428
+ throw new ValidationError({ fields: [field], message, value: dateStr });
6429
+ }
6430
+ }
6431
+ function _parseLLMFriendlyDate(dateStr) {
6432
+ if (!moment(dateStr, "YYYY-MM-DD", true).isValid()) {
6433
+ throw new Error(
6434
+ 'Invalid date format. Please provide the date in "YYYY-MM-DD" format.'
6435
+ );
6436
+ }
6437
+ const date = moment.utc(dateStr, "YYYY-MM-DD").startOf("day");
6438
+ return date.toDate();
6439
+ }
6440
+ function parseLLMFriendlyDateTime(field, dateStr, required = false) {
6441
+ try {
6442
+ return _parseLLMFriendlyDateTime(dateStr);
6443
+ } catch (err) {
6444
+ if (field.isOptional && !required) {
6445
+ return;
6446
+ }
6447
+ const message = err.message;
6448
+ throw new ValidationError({ fields: [field], message, value: dateStr });
6449
+ }
6450
+ }
6451
+ function _parseLLMFriendlyDateTime(dateTimeStr) {
6452
+ const dateTimeRegex = /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/;
6453
+ const match = dateTimeStr.match(dateTimeRegex);
6454
+ if (!match) {
6455
+ throw new Error(
6456
+ 'Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.'
6457
+ );
6458
+ }
6459
+ const [, dateTime, timeZone] = match;
6460
+ if (!dateTime || !timeZone) {
6461
+ throw new Error(
6462
+ 'Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.'
6463
+ );
6464
+ }
6465
+ const zone = moment.tz.zone(timeZone);
6466
+ if (!zone) {
6467
+ throw new Error(
6468
+ `Unrecognized time zone ${timeZone}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`
6469
+ );
6470
+ }
6471
+ const date = moment.tz(
6472
+ dateTime,
6473
+ ["YYYY-MM-DD HH:mm", "YYYY-MM-DD HH:mm:ss"],
6474
+ zone.name
6475
+ );
6476
+ if (!date.isValid()) {
6477
+ throw new Error(
6478
+ "Invalid date and time values. Please ensure all components are correct."
6479
+ );
6480
+ }
6481
+ return date.utc().toDate();
6482
+ }
6483
+ var formatDateWithTimezone = (date) => {
6484
+ const momentDate = moment(date).utc();
6485
+ return momentDate.format(`YYYY-MM-DD HH:mm:ss UTC`);
6486
+ };
6487
+
6488
+ // dsp/util.ts
6489
+ var colorLog3 = new ColorLog();
6490
+ var updateProgressBar = (current, total, success, elapsedTime, msg, progressBarWidth = 20) => {
6491
+ const percentage = (current / total * 100).toFixed(1);
6492
+ const filledBarLength = Math.round(progressBarWidth * current / total);
6493
+ const emptyBarLength = progressBarWidth - filledBarLength;
6494
+ const filledBar = colorLog3.blueBright("\u2588".repeat(filledBarLength));
6495
+ const emptyBar = " ".repeat(emptyBarLength);
6496
+ const successRate = total > 0 ? (success / total * 100).toFixed(1) : "0.0";
6497
+ const friendlyMsg = msg.includes("Running MIPROv2 optimization") ? "Testing prompt variations" : msg.includes("Tuning Prompt") ? "Generating training examples" : msg;
6498
+ process.stdout.write(
6499
+ `\u2502 ${friendlyMsg}: ${current}/${total} (${colorLog3.yellow(percentage)}%) |${filledBar}${emptyBar}| Success rate: ${colorLog3.greenBright(successRate)}%
6500
+ `
6501
+ );
6502
+ };
6503
+ var validateValue = (field, value) => {
6504
+ const ft = field.type ?? { name: "string", isArray: false };
6505
+ const validateSingleValue = (expectedType, val) => {
6506
+ switch (expectedType) {
6507
+ case "class":
6508
+ return typeof val === "string";
6509
+ case "code":
6510
+ return typeof val === "string";
6511
+ case "string":
6512
+ return typeof val === "string";
6513
+ case "number":
6514
+ return typeof val === "number";
6515
+ case "boolean":
6516
+ return typeof val === "boolean";
6517
+ case "date":
6518
+ return val instanceof Date || typeof val === "string";
6519
+ case "datetime":
6520
+ return val instanceof Date || typeof val === "string";
6521
+ case "json":
6522
+ return typeof val === "object" || typeof val === "string";
6523
+ default:
6524
+ return false;
6525
+ }
6526
+ };
6527
+ const validImage = (val) => {
6528
+ if (!val || typeof val !== "object" || !("mimeType" in val) || !("data" in val)) {
6529
+ return false;
6530
+ }
6531
+ return true;
6532
+ };
6533
+ if (field.type?.name === "image") {
6534
+ let msg;
6535
+ if (Array.isArray(value)) {
6536
+ for (const item of value) {
6537
+ if (!validImage(item)) {
6538
+ msg = "object ({ mimeType: string; data: string })";
6539
+ break;
6540
+ }
6541
+ }
6542
+ } else if (!validImage(value)) {
6543
+ msg = "object ({ mimeType: string; data: string })";
6544
+ }
6545
+ if (msg) {
6546
+ throw new Error(
6547
+ `Validation failed: Expected '${field.name}' to be type '${msg}' instead got '${value}'`
6548
+ );
6549
+ }
6550
+ return;
6551
+ }
6552
+ const validAudio = (val) => {
6553
+ if (!val || typeof val !== "object" || !("data" in val)) {
6554
+ return false;
6555
+ }
6556
+ return true;
6557
+ };
6558
+ if (field.type?.name === "audio") {
6559
+ let msg;
6560
+ if (Array.isArray(value)) {
6561
+ for (const item of value) {
6562
+ if (!validAudio(item)) {
6563
+ msg = "object ({ data: string; format?: string })";
6564
+ break;
6565
+ }
6566
+ }
5857
6567
  } else if (!validAudio(value)) {
5858
6568
  msg = "object ({ data: string; format?: string })";
5859
6569
  }
@@ -5936,18 +6646,24 @@ var parseMarkdownList = (input) => {
5936
6646
  }
5937
6647
  return list;
5938
6648
  };
5939
- function mergeDeltas(base, delta) {
6649
+ function mergeDeltas(base, currentDelta) {
6650
+ const { index, delta, version } = currentDelta;
6651
+ const target = base.find((b) => b.index === index)?.delta;
6652
+ if (!target) {
6653
+ base.push({ index, delta, version });
6654
+ return base;
6655
+ }
5940
6656
  for (const key of Object.keys(delta)) {
5941
- const baseValue = base[key];
6657
+ const baseValue = target[key];
5942
6658
  const deltaValue = delta[key];
5943
6659
  if (baseValue === void 0 && Array.isArray(deltaValue)) {
5944
- base[key] = [...deltaValue];
6660
+ target[key] = [...deltaValue];
5945
6661
  } else if (Array.isArray(baseValue) && Array.isArray(deltaValue)) {
5946
- base[key] = [...baseValue ?? [], ...deltaValue];
6662
+ target[key] = [...baseValue, ...deltaValue];
5947
6663
  } else if ((baseValue === void 0 || typeof baseValue === "string") && typeof deltaValue === "string") {
5948
- base[key] = (baseValue ?? "") + deltaValue;
6664
+ target[key] = `${baseValue ?? ""}${deltaValue}`;
5949
6665
  } else {
5950
- base[key] = deltaValue;
6666
+ target[key] = deltaValue;
5951
6667
  }
5952
6668
  }
5953
6669
  return base;
@@ -5994,19 +6710,15 @@ function matchesContent(content, prefix, startIndex = 0, prefixCache = globalPre
5994
6710
  if (!prefixCache.get(prefix)) {
5995
6711
  prefixCache.set(prefix, prefixes);
5996
6712
  }
5997
- const contentEnd = content.slice(
5998
- Math.max(startIndex, content.length - prefix.length)
5999
- );
6000
- for (let i = 0; i < prefixes.length - 1; i++) {
6713
+ let longestPartialMatch = -1;
6714
+ for (let i = prefixes.length - 1; i >= 0; i--) {
6001
6715
  const partialPrefix = prefixes[i];
6002
- if (partialPrefix === "\n" || partialPrefix === ":") {
6003
- continue;
6004
- }
6005
- if (partialPrefix && contentEnd.endsWith(partialPrefix)) {
6006
- return -2;
6716
+ if (content.endsWith(partialPrefix)) {
6717
+ longestPartialMatch = i;
6718
+ break;
6007
6719
  }
6008
6720
  }
6009
- return -1;
6721
+ return longestPartialMatch >= 0 ? -2 : -1;
6010
6722
  }
6011
6723
  var formatTime = (ms) => {
6012
6724
  const seconds = Math.floor(ms / 1e3);
@@ -6029,11 +6741,10 @@ var updateDetailedProgress = (roundIndex, current, total, elapsedTime, example,
6029
6741
  process.stdout.write("\r\x1B[K");
6030
6742
  const percentage = (current / total * 100).toFixed(1);
6031
6743
  const formattedTime = formatTime(elapsedTime);
6032
- const itemsPerSecond = elapsedTime > 0 ? (current / elapsedTime * 1e3).toFixed(2) : "0.00";
6033
6744
  const eta = calculateETA(current, total, elapsedTime);
6034
- let output = `Round ${roundIndex + 1}/${configInfo.maxRounds}: ${current}/${total} (${percentage}%) [${formattedTime}, ${itemsPerSecond} it/s, ETA: ${eta}]`;
6745
+ let output = `Training round ${roundIndex + 1}/${configInfo.maxRounds}: ${current}/${total} (${percentage}%) [${formattedTime}, ETA: ${eta}]`;
6035
6746
  const successRate = stats.totalCalls > 0 ? stats.successfulDemos / stats.totalCalls * 100 : 0;
6036
- output += ` | Success: ${stats.successfulDemos}/${stats.totalCalls} (${successRate.toFixed(1)}%)`;
6747
+ output += ` | Success rate: ${successRate.toFixed(1)}% (${stats.successfulDemos}/${stats.totalCalls})`;
6037
6748
  if (configInfo.verboseMode || configInfo.debugMode) {
6038
6749
  if (configInfo.costMonitoring) {
6039
6750
  output += `
@@ -6069,913 +6780,365 @@ var updateDetailedProgress = (roundIndex, current, total, elapsedTime, example,
6069
6780
  console.log(output);
6070
6781
  };
6071
6782
 
6072
- // dsp/prompt.ts
6073
- var functionCallInstructions = `
6074
- ## Function Call Instructions
6075
- - Complete the task, using the functions defined earlier in this prompt.
6076
- - Call functions step-by-step, using the output of one function as input to the next.
6077
- - Use the function results to generate the output fields.`;
6078
- var formattingRules = `
6079
- ## Strict Output Formatting Rules
6080
- - Output must strictly follow the defined plain-text \`field name: value\` field format.
6081
- - Output field, values must strictly adhere to the specified output field formatting rules.
6082
- - Do not add any text before or after the output fields, just the field name and value.
6083
- - Do not use code blocks.`;
6084
- var AxPromptTemplate = class {
6085
- sig;
6086
- fieldTemplates;
6087
- task;
6088
- thoughtFieldName;
6089
- functions;
6090
- constructor(sig, options, fieldTemplates) {
6091
- this.sig = sig;
6092
- this.fieldTemplates = fieldTemplates;
6093
- this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
6094
- this.functions = options?.functions;
6095
- const task = [];
6096
- const inArgs = renderDescFields(this.sig.getInputFields());
6097
- const outArgs = renderDescFields(this.sig.getOutputFields());
6098
- task.push(
6099
- `You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
6100
- );
6101
- const funcs = this.functions?.map((f2) => "toFunction" in f2 ? f2.toFunction() : f2)?.flat();
6102
- const funcList = funcs?.map((fn) => `- \`${fn.name}\`: ${formatDescription(fn.description)}`).join("\n");
6103
- if (funcList && funcList.length > 0) {
6104
- task.push(`## Available Functions
6105
- ${funcList}`);
6106
- }
6107
- const inputFields = renderInputFields(this.sig.getInputFields());
6108
- task.push(`## Input Fields
6109
- ${inputFields}`);
6110
- const outputFields = renderOutputFields(this.sig.getOutputFields());
6111
- task.push(`## Output Fields
6112
- ${outputFields}`);
6113
- if (funcList && funcList.length > 0) {
6114
- task.push(functionCallInstructions.trim());
6115
- }
6116
- task.push(formattingRules.trim());
6117
- const desc = this.sig.getDescription();
6118
- if (desc) {
6119
- const text = formatDescription(desc);
6120
- task.push(text);
6783
+ // dsp/extract.ts
6784
+ var extractValues = (sig, values, content, strictMode = false) => {
6785
+ const xstate = { extractedFields: [], streamedIndex: {}, s: -1 };
6786
+ streamingExtractValues(sig, values, xstate, content, { strictMode });
6787
+ streamingExtractFinalValue(sig, values, xstate, content);
6788
+ for (const field of sig.getOutputFields()) {
6789
+ if (field.isInternal) {
6790
+ delete values[field.name];
6121
6791
  }
6122
- this.task = {
6123
- type: "text",
6124
- text: task.join("\n\n")
6125
- };
6126
6792
  }
6127
- renderSingleValueUserContent = (values, renderedExamples, renderedDemos, examplesInSystemPrompt) => {
6128
- const completion = this.renderInputFields(values);
6129
- const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
6130
- const prompt = promptList.filter((v) => v !== void 0);
6131
- return prompt.every((v) => v.type === "text") ? prompt.map((v) => v.text).join("\n") : prompt.reduce(combineConsecutiveStrings("\n"), []);
6132
- };
6133
- render = (values, {
6134
- examples,
6135
- demos
6136
- }) => {
6137
- const renderedExamples = examples ? [
6138
- { type: "text", text: "\n\n## Examples\n" },
6139
- ...this.renderExamples(examples)
6140
- ] : [];
6141
- const renderedDemos = demos ? this.renderDemos(demos) : [];
6142
- const allTextExamples = renderedExamples.every((v) => v.type === "text");
6143
- const allTextDemos = renderedDemos.every((v) => v.type === "text");
6144
- const examplesInSystemPrompt = allTextExamples && allTextDemos;
6145
- let systemContent = this.task.text;
6146
- if (examplesInSystemPrompt) {
6147
- const combinedItems = [
6148
- { type: "text", text: systemContent },
6149
- ...renderedExamples,
6150
- ...renderedDemos
6151
- ];
6152
- combinedItems.reduce(combineConsecutiveStrings(""), []);
6153
- if (combinedItems && combinedItems[0]) {
6154
- systemContent = combinedItems[0].text;
6155
- }
6793
+ };
6794
+ var checkMissingRequiredFields = (xstate, values, outputFields) => {
6795
+ const missingFields = [];
6796
+ for (const field of outputFields) {
6797
+ if (field && !field.isOptional && values[field.name] === void 0) {
6798
+ missingFields.push(field);
6156
6799
  }
6157
- const systemPrompt = {
6158
- role: "system",
6159
- content: systemContent
6160
- };
6161
- if (Array.isArray(values)) {
6162
- let userMessages = [];
6163
- const history = values;
6164
- for (const [index, message] of history.entries()) {
6165
- let content;
6166
- if (index === 0) {
6167
- content = this.renderSingleValueUserContent(
6168
- message.values,
6169
- renderedExamples,
6170
- renderedDemos,
6171
- examplesInSystemPrompt
6172
- );
6173
- } else {
6174
- content = this.renderSingleValueUserContent(
6175
- message.values,
6176
- [],
6177
- [],
6178
- false
6179
- );
6180
- }
6181
- if (message.role === "user") {
6182
- userMessages.push({ role: "user", content });
6800
+ }
6801
+ if (missingFields.length > 0) {
6802
+ throw new ValidationError({
6803
+ message: `Required ${missingFields.length === 1 ? "field" : "fields"} not found`,
6804
+ fields: missingFields
6805
+ });
6806
+ }
6807
+ };
6808
+ var streamingExtractValues = (sig, values, xstate, content, { strictMode, skipEarlyFail } = {}) => {
6809
+ const fields = sig.getOutputFields();
6810
+ let expectedField;
6811
+ for (const [index, field] of fields.entries()) {
6812
+ if (index === xstate.currFieldIndex && !xstate.inAssumedField) {
6813
+ continue;
6814
+ }
6815
+ if (field.name in values && !(index === xstate.currFieldIndex && xstate.inAssumedField)) {
6816
+ continue;
6817
+ }
6818
+ const isFirst = xstate.extractedFields.length === 0;
6819
+ const prefix = (isFirst ? "" : "\n") + field.title + ":";
6820
+ let e = matchesContent(content, prefix, xstate.s);
6821
+ let prefixLen = prefix.length;
6822
+ switch (e) {
6823
+ case -1:
6824
+ if (skipEarlyFail) {
6183
6825
  continue;
6184
6826
  }
6185
- if (message.role !== "assistant") {
6186
- throw new Error("Invalid message role");
6827
+ if (!strictMode && fields.length === 1 && xstate.currField === void 0) {
6828
+ xstate.inAssumedField = true;
6829
+ expectedField = field;
6830
+ prefixLen = 0;
6831
+ e = 0;
6832
+ break;
6187
6833
  }
6188
- if (typeof content !== "string") {
6189
- throw new Error(
6190
- "Assistant message cannot contain non-text content like images, files,etc"
6191
- );
6834
+ if (xstate.currField === void 0 && !field.isOptional) {
6835
+ throw new ValidationError({
6836
+ message: "Expected (Required) field not found",
6837
+ fields: [field]
6838
+ });
6192
6839
  }
6193
- userMessages.push({ role: "assistant", content });
6194
- }
6195
- return [systemPrompt, ...userMessages];
6840
+ expectedField = field.isOptional ? void 0 : field;
6841
+ continue;
6842
+ // Field is not found, continue to the next field
6843
+ case -2:
6844
+ return true;
6845
+ // Partial match at end, skip and gather more content
6846
+ case -3:
6847
+ return true;
6848
+ // String is only whitespace, skip and gather more content
6849
+ case -4:
6850
+ xstate.inBlock = true;
6851
+ return true;
6196
6852
  }
6197
- const userContent = this.renderSingleValueUserContent(
6198
- values,
6199
- renderedExamples,
6200
- renderedDemos,
6201
- examplesInSystemPrompt
6202
- );
6203
- return [systemPrompt, { role: "user", content: userContent }];
6204
- };
6205
- renderExtraFields = (extraFields) => {
6206
- const prompt = [];
6207
- if (!extraFields || extraFields.length === 0) {
6208
- return prompt;
6853
+ if (expectedField && expectedField.name !== field.name) {
6854
+ throw new ValidationError({
6855
+ message: "Expected (Required) field not found",
6856
+ fields: [expectedField]
6857
+ });
6209
6858
  }
6210
- const groupedFields = extraFields.reduce(
6211
- (acc, field) => {
6212
- const title = field.title;
6213
- if (!acc[title]) {
6214
- acc[title] = [];
6215
- }
6216
- acc[title].push(field);
6217
- return acc;
6218
- },
6219
- {}
6220
- );
6221
- const formattedGroupedFields = Object.entries(groupedFields).map(([title, fields]) => {
6222
- if (fields.length === 1) {
6223
- const field = fields[0];
6224
- return {
6225
- title,
6226
- name: field.name,
6227
- description: field.description
6228
- };
6229
- } else if (fields.length > 1) {
6230
- const valuesList = fields.map((field) => `- ${field.description}`).join("\n");
6231
- return {
6232
- title,
6233
- name: fields[0].name,
6234
- description: valuesList
6235
- };
6859
+ if (xstate.currField !== void 0 && xstate.inAssumedField) {
6860
+ xstate.inAssumedField = false;
6861
+ xstate.streamedIndex[xstate.currField.name] = 0;
6862
+ xstate.currField = void 0;
6863
+ }
6864
+ if (xstate.currField) {
6865
+ const val = content.substring(xstate.s, e).trim();
6866
+ const parsedValue = validateAndParseFieldValue(xstate.currField, val);
6867
+ if (parsedValue !== void 0) {
6868
+ values[xstate.currField.name] = parsedValue;
6236
6869
  }
6237
- }).filter(Boolean);
6238
- formattedGroupedFields.forEach((field) => {
6239
- const fn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
6240
- prompt.push(...fn(field, field.description));
6241
- });
6242
- return prompt;
6243
- };
6244
- renderExamples = (data) => {
6245
- const list = [];
6246
- const exampleContext = {
6247
- isExample: true
6248
- };
6249
- for (const [index, item] of data.entries()) {
6250
- const renderedInputItem = this.sig.getInputFields().map(
6251
- (field) => this.renderInField(field, item, {
6252
- ...exampleContext,
6253
- isInputField: true
6254
- })
6255
- ).filter((v) => v !== void 0).flat();
6256
- const renderedOutputItem = this.sig.getOutputFields().map(
6257
- (field) => this.renderInField(field, item, {
6258
- ...exampleContext,
6259
- isInputField: false
6260
- })
6261
- ).filter((v) => v !== void 0).flat();
6262
- const renderedItem = [...renderedInputItem, ...renderedOutputItem];
6263
- if (index > 0 && renderedItem.length > 0 && renderedItem[0]?.type === "text") {
6264
- list.push({ type: "text", text: "---\n\n" });
6870
+ if (xstate.prevFields) {
6871
+ xstate.prevFields?.push({ field: xstate.currField, s: xstate.s, e });
6872
+ } else {
6873
+ xstate.prevFields = [{ field: xstate.currField, s: xstate.s, e }];
6265
6874
  }
6266
- renderedItem.forEach((v) => {
6267
- if ("text" in v) {
6268
- v.text = v.text + "\n";
6269
- }
6270
- list.push(v);
6271
- });
6272
6875
  }
6273
- return list;
6274
- };
6275
- renderDemos = (data) => {
6276
- const list = [];
6277
- const inputFields = this.sig.getInputFields();
6278
- const outputFields = this.sig.getOutputFields();
6279
- const demoContext = {
6280
- isExample: true
6281
- };
6282
- for (const item of data) {
6283
- const inputRenderedItems = inputFields.map(
6284
- (field) => this.renderInField(field, item, {
6285
- ...demoContext,
6286
- isInputField: true
6287
- })
6288
- ).filter((v) => v !== void 0).flat();
6289
- const outputRenderedItems = outputFields.map(
6290
- (field) => this.renderInField(field, item, {
6291
- ...demoContext,
6292
- isInputField: false
6293
- })
6294
- ).filter((v) => v !== void 0).flat();
6295
- const renderedItem = [...inputRenderedItems, ...outputRenderedItems];
6296
- renderedItem.slice(0, -1).forEach((v) => {
6297
- if ("text" in v) {
6298
- v.text = v.text + "\n";
6299
- }
6300
- list.push(v);
6301
- });
6876
+ xstate.s = e + prefixLen;
6877
+ xstate.currField = field;
6878
+ xstate.currFieldIndex = index;
6879
+ if (!xstate.extractedFields.includes(field)) {
6880
+ xstate.extractedFields.push(field);
6302
6881
  }
6303
- return list;
6304
- };
6305
- renderInputFields = (values) => {
6306
- const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values, void 0)).filter((v) => v !== void 0).flat();
6307
- renderedItems.filter((v) => v.type === "text").forEach((v) => {
6308
- v.text = v.text + "\n";
6309
- });
6310
- return renderedItems;
6311
- };
6312
- renderInField = (field, values, context3) => {
6313
- const value = values[field.name];
6314
- if (isEmptyValue(field, value, context3)) {
6315
- return;
6882
+ if (xstate.streamedIndex[field.name] === void 0) {
6883
+ xstate.streamedIndex[field.name] = 0;
6316
6884
  }
6317
- if (field.type) {
6318
- validateValue(field, value);
6885
+ }
6886
+ };
6887
+ var streamingExtractFinalValue = (sig, values, xstate, content) => {
6888
+ if (xstate.currField) {
6889
+ let val = content.substring(xstate.s).trim();
6890
+ const parsedValue = validateAndParseFieldValue(xstate.currField, val);
6891
+ if (parsedValue !== void 0) {
6892
+ values[xstate.currField.name] = parsedValue;
6319
6893
  }
6320
- const processedValue = processValue(field, value);
6321
- const textFieldFn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
6322
- return textFieldFn(field, processedValue);
6323
- };
6324
- defaultRenderInField = (field, value) => {
6325
- if (field.type?.name === "image") {
6326
- const validateImage = (value2) => {
6327
- if (!value2) {
6328
- throw new Error("Image field value is required.");
6329
- }
6330
- if (typeof value2 !== "object") {
6331
- throw new Error("Image field value must be an object.");
6332
- }
6333
- if (!("mimeType" in value2)) {
6334
- throw new Error("Image field must have mimeType");
6894
+ }
6895
+ checkMissingRequiredFields(xstate, values, sig.getOutputFields());
6896
+ };
6897
+ var convertValueToType = (field, val, required = false) => {
6898
+ switch (field.type?.name) {
6899
+ case "code":
6900
+ return extractBlock(val);
6901
+ case "string":
6902
+ return val;
6903
+ case "number": {
6904
+ const v = Number(val);
6905
+ if (Number.isNaN(v)) {
6906
+ if (field.isOptional && !required) {
6907
+ return;
6335
6908
  }
6336
- if (!("data" in value2)) {
6337
- throw new Error("Image field must have data");
6909
+ throw new Error("Invalid number");
6910
+ }
6911
+ return v;
6912
+ }
6913
+ case "boolean": {
6914
+ if (typeof val === "boolean") {
6915
+ return val;
6916
+ }
6917
+ const v = val.toLowerCase();
6918
+ if (v === "true") {
6919
+ return true;
6920
+ } else if (v === "false") {
6921
+ return false;
6922
+ } else {
6923
+ if (field.isOptional && !required) {
6924
+ return;
6338
6925
  }
6339
- return value2;
6340
- };
6341
- let result = [
6342
- { type: "text", text: `${field.title}: ` }
6343
- ];
6344
- if (field.type.isArray) {
6345
- if (!Array.isArray(value)) {
6346
- throw new Error("Image field value must be an array.");
6347
- }
6348
- result = result.concat(
6349
- value.map((v) => {
6350
- const validated = validateImage(v);
6351
- return {
6352
- type: "image",
6353
- mimeType: validated.mimeType,
6354
- image: validated.data
6355
- };
6356
- })
6357
- );
6358
- } else {
6359
- const validated = validateImage(value);
6360
- result.push({
6361
- type: "image",
6362
- mimeType: validated.mimeType,
6363
- image: validated.data
6364
- });
6926
+ throw new Error("Invalid boolean");
6365
6927
  }
6366
- return result;
6367
6928
  }
6368
- if (field.type?.name === "audio") {
6369
- const validateAudio = (value2) => {
6370
- if (!value2) {
6371
- throw new Error("Audio field value is required.");
6372
- }
6373
- if (typeof value2 !== "object") {
6374
- throw new Error("Audio field value must be an object.");
6375
- }
6376
- if (!("data" in value2)) {
6377
- throw new Error("Audio field must have data");
6378
- }
6379
- return value2;
6380
- };
6381
- let result = [
6382
- { type: "text", text: `${field.title}: ` }
6383
- ];
6384
- if (field.type.isArray) {
6385
- if (!Array.isArray(value)) {
6386
- throw new Error("Audio field value must be an array.");
6929
+ case "date":
6930
+ return parseLLMFriendlyDate(field, val, required);
6931
+ case "datetime":
6932
+ return parseLLMFriendlyDateTime(field, val, required);
6933
+ case "class":
6934
+ const className = val;
6935
+ if (field.type.options && !field.type.options.includes(className)) {
6936
+ if (field.isOptional) {
6937
+ return;
6387
6938
  }
6388
- result = result.concat(
6389
- value.map((v) => {
6390
- const validated = validateAudio(v);
6391
- return {
6392
- type: "audio",
6393
- format: validated.format ?? "wav",
6394
- data: validated.data
6395
- };
6396
- })
6939
+ throw new Error(
6940
+ `Invalid class '${val}', expected one of the following: ${field.type.options.join(", ")}`
6397
6941
  );
6398
- } else {
6399
- const validated = validateAudio(value);
6400
- result.push({
6401
- type: "audio",
6402
- format: validated.format ?? "wav",
6403
- data: validated.data
6404
- });
6405
- }
6406
- return result;
6407
- }
6408
- const text = [field.title, ": "];
6409
- if (Array.isArray(value)) {
6410
- text.push("\n");
6411
- text.push(value.map((v) => `- ${v}`).join("\n"));
6412
- } else {
6413
- text.push(value);
6414
- }
6415
- return [{ type: "text", text: text.join("") }];
6416
- };
6417
- };
6418
- var renderDescFields = (list) => list.map((v) => `\`${v.title}\``).join(", ");
6419
- var renderInputFields = (fields) => {
6420
- const rows = fields.map((field) => {
6421
- const name = field.title;
6422
- const type = field.type?.name ? toFieldType(field.type) : "string";
6423
- const requiredMsg = field.isOptional ? `This optional ${type} field may be omitted` : `A ${type} field`;
6424
- const description = field.description ? ` ${formatDescription(field.description)}` : "";
6425
- return `${name}: (${requiredMsg})${description}`.trim();
6426
- });
6427
- return rows.join("\n");
6428
- };
6429
- var renderOutputFields = (fields) => {
6430
- const rows = fields.map((field) => {
6431
- const name = field.title;
6432
- const type = field.type?.name ? toFieldType(field.type) : "string";
6433
- const requiredMsg = field.isOptional ? `Only include this ${type} field if its value is available` : `This ${type} field must be included`;
6434
- let description = "";
6435
- if (field.description && field.description.length > 0) {
6436
- const value = field.type?.name === "class" ? field.description : formatDescription(field.description);
6437
- description = ` ${value}`;
6438
- }
6439
- if (field.type?.options && field.type.options.length > 0) {
6440
- if (description.length > 0) {
6441
- description += `. `;
6442
6942
  }
6443
- description += `Allowed values: ${field.type.options.join(", ")}`;
6444
- }
6445
- return `${name}: (${requiredMsg})${description}`.trim();
6446
- });
6447
- return rows.join("\n");
6943
+ return className;
6944
+ default:
6945
+ return val;
6946
+ }
6448
6947
  };
6449
- var processValue = (field, value) => {
6450
- if (field.type?.name === "date" && value instanceof Date) {
6451
- const v = value.toISOString();
6452
- return v.slice(0, v.indexOf("T"));
6948
+ function* yieldDelta(content, field, s2, e, xstate, index) {
6949
+ const { name: fieldName, isInternal } = field;
6950
+ const { isArray: fieldIsArray, name: fieldTypeName } = field.type ?? {};
6951
+ if (isInternal || fieldIsArray || fieldTypeName && fieldTypeName !== "string" && fieldTypeName !== "code") {
6952
+ return;
6453
6953
  }
6454
- if (field.type?.name === "datetime" && value instanceof Date) {
6455
- return formatDateWithTimezone(value);
6954
+ const pos = xstate.streamedIndex[fieldName] ?? 0;
6955
+ const isFirstChunk = pos === 0;
6956
+ const d1 = content.substring(s2 + pos, e);
6957
+ if (d1.length === 0) {
6958
+ return;
6456
6959
  }
6457
- if (field.type?.name === "image" && typeof value === "object") {
6458
- return value;
6960
+ let d2 = d1.replace(/\s+$/, "");
6961
+ if (xstate.currField?.type?.name === "code") {
6962
+ d2 = d2.replace(/\s*```\s*$/, "");
6459
6963
  }
6460
- if (field.type?.name === "audio" && typeof value === "object") {
6461
- return value;
6964
+ let d3 = isFirstChunk ? d2.trimStart() : d2;
6965
+ if (xstate.currField?.type?.name === "code") {
6966
+ d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
6462
6967
  }
6463
- if (typeof value === "string") {
6464
- return value;
6968
+ if (d3.length > 0) {
6969
+ yield { index, delta: { [fieldName]: d3 } };
6970
+ xstate.streamedIndex[fieldName] = pos + d2.length;
6465
6971
  }
6466
- return JSON.stringify(value, null, 2);
6467
- };
6468
- var toFieldType = (type) => {
6469
- const baseType = (() => {
6470
- switch (type?.name) {
6471
- case "string":
6472
- return "string";
6473
- case "number":
6474
- return "number";
6475
- case "boolean":
6476
- return "boolean";
6477
- case "date":
6478
- return 'date ("YYYY-MM-DD" format)';
6479
- case "datetime":
6480
- return 'date time ("YYYY-MM-DD HH:mm Timezone" format)';
6481
- case "json":
6482
- return "JSON object";
6483
- case "class":
6484
- return "classification class";
6485
- case "code":
6486
- return "code";
6487
- default:
6488
- return "string";
6489
- }
6490
- })();
6491
- return type?.isArray ? `json array of ${baseType} items` : baseType;
6492
- };
6493
- function combineConsecutiveStrings(separator) {
6494
- return (acc, current) => {
6495
- if (current.type === "text") {
6496
- const previous = acc.length > 0 ? acc[acc.length - 1] : null;
6497
- if (previous && previous.type === "text") {
6498
- previous.text += separator + current.text;
6499
- } else {
6500
- acc.push(current);
6501
- }
6502
- } else {
6503
- acc.push(current);
6504
- }
6505
- return acc;
6506
- };
6507
6972
  }
6508
- var isEmptyValue = (field, value, context3) => {
6509
- if (typeof value === "boolean") {
6510
- return false;
6973
+ function* streamValues(sig, content, values, xstate, index) {
6974
+ for (const prevField of xstate.prevFields ?? []) {
6975
+ const { field, s: s2, e } = prevField;
6976
+ yield* yieldDelta(content, field, s2, e, xstate, index);
6511
6977
  }
6512
- if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
6513
- if (context3?.isExample) {
6514
- return true;
6978
+ xstate.prevFields = void 0;
6979
+ if (!xstate.currField || xstate.currField.isInternal) {
6980
+ return;
6981
+ }
6982
+ yield* yieldDelta(
6983
+ content,
6984
+ xstate.currField,
6985
+ xstate.s,
6986
+ content.length,
6987
+ xstate,
6988
+ index
6989
+ );
6990
+ const outputFields = sig.getOutputFields();
6991
+ for (const key of Object.keys(values)) {
6992
+ const field = outputFields.find((f2) => f2.name === key);
6993
+ if (!field || field.isInternal) {
6994
+ continue;
6515
6995
  }
6516
- if (field.isOptional || field.isInternal) {
6517
- return true;
6996
+ const value = values[key];
6997
+ if (Array.isArray(value)) {
6998
+ const s2 = xstate.streamedIndex?.[key] ?? 0;
6999
+ const v = value.slice(s2);
7000
+ if (v && v.length > 0) {
7001
+ yield { index, delta: { [key]: v } };
7002
+ xstate.streamedIndex[key] = s2 + v.length;
7003
+ }
7004
+ continue;
7005
+ }
7006
+ if (!xstate.streamedIndex[key]) {
7007
+ yield { index, delta: { [key]: value } };
7008
+ xstate.streamedIndex[key] = 1;
6518
7009
  }
6519
- const fieldType = context3?.isInputField !== false ? "input" : "output";
6520
- throw new Error(`Value for ${fieldType} field '${field.name}' is required.`);
6521
7010
  }
6522
- return false;
6523
- };
6524
- function formatDescription(str) {
6525
- const value = str.trim();
6526
- return value.length > 0 ? `${value.charAt(0).toUpperCase()}${value.slice(1)}${value.endsWith(".") ? "" : "."}` : "";
6527
7011
  }
6528
-
6529
- // dsp/validate.ts
6530
- var ValidationError = class extends Error {
6531
- fields;
6532
- constructor({
6533
- message,
6534
- fields
6535
- }) {
6536
- super(message);
6537
- this.fields = fields;
6538
- this.name = this.constructor.name;
7012
+ function validateAndParseFieldValue(field, fieldValue) {
7013
+ if (!fieldValue || fieldValue === "" || /^(null|undefined)\s*$/i.test(fieldValue)) {
7014
+ if (field.isOptional) {
7015
+ return;
7016
+ }
7017
+ throw new ValidationError({
7018
+ message: "Required field is missing",
7019
+ fields: [field],
7020
+ value: fieldValue
7021
+ });
6539
7022
  }
6540
- getFixingInstructions = () => {
6541
- return this.fields.map((field) => ({
6542
- name: "outputError",
6543
- title: "Output Correction Required",
6544
- description: `The section labeled '${field.title}' either was not generated by the LLM or does not match the expected format of '${toFieldType(field.type)}'. ${this.message} Please revise your response to ensure it conforms to the specified format.`
6545
- }));
6546
- };
6547
- toString() {
6548
- return [
6549
- `${this.name}: ${this.message}`,
6550
- ...this.fields.map(
6551
- (field) => ` - ${field.title}: Expected format '${toFieldType(field.type)}'`
6552
- )
6553
- ].join("\n");
6554
- }
6555
- [Symbol.for("nodejs.util.inspect.custom")](_depth, _options) {
6556
- return this.toString();
6557
- }
6558
- };
6559
- function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId) {
6560
- mem.add(
6561
- {
6562
- role: "user",
6563
- content: promptTemplate.renderExtraFields(errorFields)
6564
- },
6565
- sessionId
6566
- );
6567
- mem.addTag("error");
6568
- if (ai.getOptions().debug) {
6569
- const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
6570
- const logger = ai.getLogger();
6571
- logger(`\u274C Error Correction:
6572
- ${errors}`, {
6573
- tags: ["error"]
6574
- });
6575
- }
6576
- }
6577
-
6578
- // dsp/datetime.ts
6579
- function parseLLMFriendlyDate(field, dateStr, required = false) {
6580
- try {
6581
- return _parseLLMFriendlyDate(dateStr);
6582
- } catch (err) {
6583
- if (field.isOptional && !required) {
6584
- return;
7023
+ let value;
7024
+ if (field.type?.name === "json") {
7025
+ try {
7026
+ const text = extractBlock(fieldValue);
7027
+ value = JSON.parse(text);
7028
+ return value;
7029
+ } catch (e) {
7030
+ throw new ValidationError({
7031
+ message: "Invalid JSON: " + e.message,
7032
+ fields: [field],
7033
+ value: fieldValue
7034
+ });
6585
7035
  }
6586
- const message = err.message;
6587
- throw new ValidationError({ fields: [field], message, value: dateStr });
6588
7036
  }
6589
- }
6590
- function _parseLLMFriendlyDate(dateStr) {
6591
- if (!moment(dateStr, "YYYY-MM-DD", true).isValid()) {
6592
- throw new Error(
6593
- 'Invalid date format. Please provide the date in "YYYY-MM-DD" format.'
6594
- );
7037
+ if (field.type?.isArray) {
7038
+ try {
7039
+ try {
7040
+ value = JSON.parse(fieldValue);
7041
+ } catch {
7042
+ value = parseMarkdownList(fieldValue);
7043
+ }
7044
+ if (!Array.isArray(value)) {
7045
+ throw new Error("Expected an array");
7046
+ }
7047
+ } catch (e) {
7048
+ throw new ValidationError({
7049
+ message: "Invalid Array: " + e.message,
7050
+ fields: [field],
7051
+ value: fieldValue
7052
+ });
7053
+ }
6595
7054
  }
6596
- const date = moment.utc(dateStr, "YYYY-MM-DD").startOf("day");
6597
- return date.toDate();
6598
- }
6599
- function parseLLMFriendlyDateTime(field, dateStr, required = false) {
6600
7055
  try {
6601
- return _parseLLMFriendlyDateTime(dateStr);
6602
- } catch (err) {
6603
- if (field.isOptional && !required) {
6604
- return;
7056
+ if (Array.isArray(value)) {
7057
+ for (const [index, item] of value.entries()) {
7058
+ if (item !== void 0) {
7059
+ const v = typeof item === "string" ? item.trim() : item;
7060
+ value[index] = convertValueToType(field, v, true);
7061
+ }
7062
+ }
7063
+ } else {
7064
+ value = convertValueToType(field, fieldValue);
6605
7065
  }
6606
- const message = err.message;
6607
- throw new ValidationError({ fields: [field], message, value: dateStr });
7066
+ } catch (e) {
7067
+ throw new ValidationError({
7068
+ message: e.message,
7069
+ fields: [field],
7070
+ value: fieldValue
7071
+ });
7072
+ }
7073
+ if (typeof value === "string" && value === "") {
7074
+ return void 0;
6608
7075
  }
7076
+ return value;
6609
7077
  }
6610
- function _parseLLMFriendlyDateTime(dateTimeStr) {
6611
- const dateTimeRegex = /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}(?::\d{2})?) (.+)$/;
6612
- const match = dateTimeStr.match(dateTimeRegex);
7078
+ var extractBlock = (input) => {
7079
+ const markdownBlockPattern = /```([A-Za-z]*)\n([\s\S]*?)\n```/g;
7080
+ const match = markdownBlockPattern.exec(input);
6613
7081
  if (!match) {
6614
- throw new Error(
6615
- 'Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.'
6616
- );
6617
- }
6618
- const [, dateTime, timeZone] = match;
6619
- if (!dateTime || !timeZone) {
6620
- throw new Error(
6621
- 'Invalid date and time format. Please provide the date and time in "YYYY-MM-DD HH:mm" or "YYYY-MM-DD HH:mm:ss" format, followed by the timezone.'
6622
- );
7082
+ return input;
6623
7083
  }
6624
- const zone = moment.tz.zone(timeZone);
6625
- if (!zone) {
6626
- throw new Error(
6627
- `Unrecognized time zone ${timeZone}. Please provide a valid time zone name, abbreviation, or offset. For example, "America/New_York", or "EST".`
6628
- );
7084
+ if (match.length === 3) {
7085
+ return match[2];
6629
7086
  }
6630
- const date = moment.tz(
6631
- dateTime,
6632
- ["YYYY-MM-DD HH:mm", "YYYY-MM-DD HH:mm:ss"],
6633
- zone.name
6634
- );
6635
- if (!date.isValid()) {
6636
- throw new Error(
6637
- "Invalid date and time values. Please ensure all components are correct."
6638
- );
7087
+ if (match.length === 2) {
7088
+ return match[1];
6639
7089
  }
6640
- return date.utc().toDate();
6641
- }
6642
- var formatDateWithTimezone = (date) => {
6643
- const momentDate = moment(date).utc();
6644
- return momentDate.format(`YYYY-MM-DD HH:mm:ss UTC`);
7090
+ return input;
6645
7091
  };
6646
7092
 
6647
- // dsp/extract.ts
6648
- var extractValues = (sig, values, content) => {
6649
- const xstate = { extractedFields: [], streamedIndex: {}, s: -1 };
6650
- streamingExtractValues(sig, values, xstate, content);
6651
- streamingExtractFinalValue(sig, values, xstate, content);
6652
- for (const field of sig.getOutputFields()) {
6653
- if (field.isInternal) {
6654
- delete values[field.name];
6655
- }
6656
- }
6657
- };
6658
- var checkMissingRequiredFields = (xstate, values, currentIndex) => {
6659
- const missingFields = [];
6660
- for (let i = 0; i < currentIndex; i++) {
6661
- const field = xstate.extractedFields[i];
6662
- if (field && !field.isOptional && values[field.name] === void 0) {
6663
- missingFields.push(field);
7093
+ // dsp/fieldProcessor.ts
7094
+ async function processFieldProcessors(fieldProcessors, values, mem, sessionId) {
7095
+ for (const processor of fieldProcessors) {
7096
+ if (values[processor.field.name] === void 0) {
7097
+ continue;
6664
7098
  }
6665
- }
6666
- if (missingFields.length > 0) {
6667
- throw new ValidationError({
6668
- message: `Required ${missingFields.length === 1 ? "field" : "fields"} not found`,
6669
- fields: missingFields
7099
+ const processFn = processor.process;
7100
+ const result = await processFn(values[processor.field.name], {
7101
+ sessionId,
7102
+ values,
7103
+ done: true
6670
7104
  });
7105
+ addToMemory(processor.field, mem, result, sessionId);
6671
7106
  }
6672
- };
6673
- var streamingExtractValues = (sig, values, xstate, content, streamingValidation = false) => {
6674
- const fields = sig.getOutputFields();
6675
- for (const [index, field] of fields.entries()) {
6676
- if (field.name in values) {
7107
+ }
7108
+ async function processStreamingFieldProcessors(fieldProcessors, content, xstate, mem, values, sessionId, done = false) {
7109
+ for (const processor of fieldProcessors) {
7110
+ if (xstate.currField?.name !== processor.field.name) {
6677
7111
  continue;
6678
7112
  }
6679
- const isFirst = xstate.extractedFields.length === 0;
6680
- const prefix = (isFirst ? "" : "\n") + field.title + ":";
6681
- let e = matchesContent(content, prefix, xstate.s);
6682
- switch (e) {
6683
- case -1:
6684
- if (streamingValidation && values.length == 0 && !field.isOptional) {
6685
- throw new ValidationError({
6686
- message: "Required field not found",
6687
- fields: [field]
6688
- });
6689
- }
6690
- continue;
6691
- // Field is not found, continue to the next field
6692
- case -2:
6693
- return true;
6694
- // Partial match at end, skip and gather more content
6695
- case -3:
6696
- return true;
6697
- // String is only whitespace, skip and gather more content
6698
- case -4:
6699
- xstate.inBlock = true;
6700
- return true;
6701
- }
6702
- let prefixLen = prefix.length;
6703
- if (xstate.currField) {
6704
- const val = content.substring(xstate.s, e).trim();
6705
- const parsedValue = validateAndParseFieldValue(xstate.currField, val);
6706
- if (parsedValue !== void 0) {
6707
- values[xstate.currField.name] = parsedValue;
6708
- }
6709
- if (xstate.prevFields) {
6710
- xstate.prevFields?.push({ field: xstate.currField, s: xstate.s, e });
6711
- } else {
6712
- xstate.prevFields = [{ field: xstate.currField, s: xstate.s, e }];
6713
- }
6714
- }
6715
- checkMissingRequiredFields(xstate, values, index);
6716
- xstate.s = e + prefixLen;
6717
- xstate.currField = field;
6718
- xstate.currFieldIndex = index;
6719
- if (!xstate.extractedFields.includes(field)) {
6720
- xstate.extractedFields.push(field);
6721
- }
6722
- if (xstate.streamedIndex[field.name] === void 0) {
6723
- xstate.streamedIndex[field.name] = 0;
7113
+ let value = content.substring(xstate.s);
7114
+ if (xstate.currField?.type?.name === "code") {
7115
+ value = value.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
7116
+ value = value.replace(/\s*```\s*$/, "");
6724
7117
  }
7118
+ const processFn = processor.process;
7119
+ const result = await processFn(value, {
7120
+ sessionId,
7121
+ values,
7122
+ done
7123
+ });
7124
+ addToMemory(xstate.currField, mem, result, sessionId);
6725
7125
  }
6726
- };
6727
- var streamingExtractFinalValue = (sig, values, xstate, content) => {
6728
- if (xstate.currField) {
6729
- let val = content.substring(xstate.s).trim();
6730
- const parsedValue = validateAndParseFieldValue(xstate.currField, val);
6731
- if (parsedValue !== void 0) {
6732
- values[xstate.currField.name] = parsedValue;
6733
- }
7126
+ }
7127
+ var addToMemory = (field, mem, result, sessionId) => {
7128
+ if (result === void 0 || typeof result === "string" && (result === "" || /^(null|undefined)\s*$/i.test(result))) {
7129
+ return;
6734
7130
  }
6735
- const sigFields = sig.getOutputFields();
6736
- checkMissingRequiredFields(xstate, values, sigFields.length);
6737
- };
6738
- var convertValueToType = (field, val, required = false) => {
6739
- switch (field.type?.name) {
6740
- case "code":
6741
- return extractBlock(val);
6742
- case "string":
6743
- return val;
6744
- case "number": {
6745
- const v = Number(val);
6746
- if (Number.isNaN(v)) {
6747
- if (field.isOptional && !required) {
6748
- return;
6749
- }
6750
- throw new Error("Invalid number");
6751
- }
6752
- return v;
6753
- }
6754
- case "boolean": {
6755
- if (typeof val === "boolean") {
6756
- return val;
6757
- }
6758
- const v = val.toLowerCase();
6759
- if (v === "true") {
6760
- return true;
6761
- } else if (v === "false") {
6762
- return false;
6763
- } else {
6764
- if (field.isOptional && !required) {
6765
- return;
6766
- }
6767
- throw new Error("Invalid boolean");
6768
- }
6769
- }
6770
- case "date":
6771
- return parseLLMFriendlyDate(field, val, required);
6772
- case "datetime":
6773
- return parseLLMFriendlyDateTime(field, val, required);
6774
- case "class":
6775
- const className = val;
6776
- if (field.type.options && !field.type.options.includes(className)) {
6777
- if (field.isOptional) {
6778
- return;
6779
- }
6780
- throw new Error(
6781
- `Invalid class '${val}', expected one of the following: ${field.type.options.join(", ")}`
6782
- );
6783
- }
6784
- return className;
6785
- default:
6786
- return val;
6787
- }
6788
- };
6789
- function* yieldDelta(content, field, s2, e, xstate) {
6790
- const { name: fieldName, isInternal } = field;
6791
- const { isArray: fieldIsArray, name: fieldTypeName } = field.type ?? {};
6792
- if (isInternal || fieldIsArray || fieldTypeName && fieldTypeName !== "string" && fieldTypeName !== "code") {
6793
- return;
6794
- }
6795
- const pos = xstate.streamedIndex[fieldName] ?? 0;
6796
- const isFirstChunk = pos === 0;
6797
- const d1 = content.substring(s2 + pos, e);
6798
- if (d1.length === 0) {
6799
- return;
6800
- }
6801
- let d2 = d1.replace(/\s+$/, "");
6802
- if (xstate.currField?.type?.name === "code") {
6803
- d2 = d2.replace(/\s*```\s*$/, "");
6804
- }
6805
- let d3 = isFirstChunk ? d2.trimStart() : d2;
6806
- if (xstate.currField?.type?.name === "code") {
6807
- d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
6808
- }
6809
- if (d3.length > 0) {
6810
- yield { [fieldName]: d3 };
6811
- xstate.streamedIndex[fieldName] = pos + d2.length;
6812
- }
6813
- }
6814
- function* streamValues(sig, content, values, xstate) {
6815
- for (const prevField of xstate.prevFields ?? []) {
6816
- const { field, s: s2, e } = prevField;
6817
- yield* yieldDelta(content, field, s2, e, xstate);
6818
- }
6819
- xstate.prevFields = void 0;
6820
- if (!xstate.currField || xstate.currField.isInternal) {
6821
- return;
6822
- }
6823
- yield* yieldDelta(
6824
- content,
6825
- xstate.currField,
6826
- xstate.s,
6827
- content.length,
6828
- xstate
6829
- );
6830
- const outputFields = sig.getOutputFields();
6831
- for (const key of Object.keys(values)) {
6832
- const field = outputFields.find((f2) => f2.name === key);
6833
- if (!field || field.isInternal) {
6834
- continue;
6835
- }
6836
- const value = values[key];
6837
- if (Array.isArray(value)) {
6838
- const s2 = xstate.streamedIndex?.[key] ?? 0;
6839
- const v = value.slice(s2);
6840
- if (v && v.length > 0) {
6841
- yield { [key]: v };
6842
- xstate.streamedIndex[key] = s2 + v.length;
6843
- }
6844
- continue;
6845
- }
6846
- if (!xstate.streamedIndex[key]) {
6847
- yield { [key]: value };
6848
- xstate.streamedIndex[key] = 1;
6849
- }
6850
- }
6851
- }
6852
- function validateAndParseFieldValue(field, fieldValue) {
6853
- if (!fieldValue || fieldValue === "" || /^(null|undefined)\s*$/i.test(fieldValue)) {
6854
- if (field.isOptional) {
6855
- return;
6856
- }
6857
- throw new ValidationError({
6858
- message: "Required field is missing",
6859
- fields: [field],
6860
- value: fieldValue
6861
- });
6862
- }
6863
- let value;
6864
- if (field.type?.name === "json") {
6865
- try {
6866
- const text = extractBlock(fieldValue);
6867
- value = JSON.parse(text);
6868
- return value;
6869
- } catch (e) {
6870
- throw new ValidationError({
6871
- message: "Invalid JSON: " + e.message,
6872
- fields: [field],
6873
- value: fieldValue
6874
- });
6875
- }
6876
- }
6877
- if (field.type?.isArray) {
6878
- try {
6879
- try {
6880
- value = JSON.parse(fieldValue);
6881
- } catch {
6882
- value = parseMarkdownList(fieldValue);
6883
- }
6884
- if (!Array.isArray(value)) {
6885
- throw new Error("Expected an array");
6886
- }
6887
- } catch (e) {
6888
- throw new ValidationError({
6889
- message: "Invalid Array: " + e.message,
6890
- fields: [field],
6891
- value: fieldValue
6892
- });
6893
- }
6894
- }
6895
- try {
6896
- if (Array.isArray(value)) {
6897
- for (const [index, item] of value.entries()) {
6898
- if (item !== void 0) {
6899
- const v = typeof item === "string" ? item.trim() : item;
6900
- value[index] = convertValueToType(field, v, true);
6901
- }
6902
- }
6903
- } else {
6904
- value = convertValueToType(field, fieldValue);
6905
- }
6906
- } catch (e) {
6907
- throw new ValidationError({
6908
- message: e.message,
6909
- fields: [field],
6910
- value: fieldValue
6911
- });
6912
- }
6913
- if (typeof value === "string" && value === "") {
6914
- return void 0;
6915
- }
6916
- return value;
6917
- }
6918
- var extractBlock = (input) => {
6919
- const markdownBlockPattern = /```([A-Za-z]*)\n([\s\S]*?)\n```/g;
6920
- const match = markdownBlockPattern.exec(input);
6921
- if (!match) {
6922
- return input;
6923
- }
6924
- if (match.length === 3) {
6925
- return match[2];
6926
- }
6927
- if (match.length === 2) {
6928
- return match[1];
6929
- }
6930
- return input;
6931
- };
6932
-
6933
- // dsp/fieldProcessor.ts
6934
- async function processFieldProcessors(fieldProcessors, values, mem, sessionId) {
6935
- for (const processor of fieldProcessors) {
6936
- if (values[processor.field.name] === void 0) {
6937
- continue;
6938
- }
6939
- const processFn = processor.process;
6940
- const result = await processFn(values[processor.field.name], {
6941
- sessionId,
6942
- values,
6943
- done: true
6944
- });
6945
- addToMemory(processor.field, mem, result, sessionId);
6946
- }
6947
- }
6948
- async function processStreamingFieldProcessors(fieldProcessors, content, xstate, mem, values, sessionId, done = false) {
6949
- for (const processor of fieldProcessors) {
6950
- if (xstate.currField?.name !== processor.field.name) {
6951
- continue;
6952
- }
6953
- let value = content.substring(xstate.s);
6954
- if (xstate.currField?.type?.name === "code") {
6955
- value = value.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
6956
- value = value.replace(/\s*```\s*$/, "");
6957
- }
6958
- const processFn = processor.process;
6959
- const result = await processFn(value, {
6960
- sessionId,
6961
- values,
6962
- done
6963
- });
6964
- addToMemory(xstate.currField, mem, result, sessionId);
6965
- }
6966
- }
6967
- var addToMemory = (field, mem, result, sessionId) => {
6968
- if (result === void 0 || typeof result === "string" && (result === "" || /^(null|undefined)\s*$/i.test(result))) {
6969
- return;
6970
- }
6971
- let resultText = JSON.stringify(
6972
- result,
6973
- (key, value) => typeof value === "bigint" ? Number(value) : value,
6974
- 2
6975
- );
6976
- const text = getFieldProcessingMessage(field, resultText);
6977
- mem.add({ role: "user", content: [{ type: "text", text }] }, sessionId);
6978
- mem.addTag(`processor`, sessionId);
7131
+ let resultText = JSON.stringify(
7132
+ result,
7133
+ (key, value) => typeof value === "bigint" ? Number(value) : value,
7134
+ 2
7135
+ );
7136
+ const text = getFieldProcessingMessage(field, resultText);
7137
+ mem.addRequest(
7138
+ [{ role: "user", content: [{ type: "text", text }] }],
7139
+ sessionId
7140
+ );
7141
+ mem.addTag(`processor`, sessionId);
6979
7142
  };
6980
7143
  function getFieldProcessingMessage(field, resultText) {
6981
7144
  const isCodeField = field.type?.name === "code";
@@ -7233,10 +7396,10 @@ var AxFunctionProcessor = class {
7233
7396
  } : void 0;
7234
7397
  if (!fnSpec.parameters) {
7235
7398
  const res2 = fnSpec.func.length === 1 ? await fnSpec.func(opt) : await fnSpec.func();
7236
- return typeof res2 === "string" ? res2 : JSON.stringify(res2, null, 2);
7399
+ return typeof res2 === "string" ? res2 : res2 === void 0 || res2 === null ? "" : JSON.stringify(res2, null, 2);
7237
7400
  }
7238
7401
  const res = fnSpec.func.length === 2 ? await fnSpec.func(args, opt) : await fnSpec.func(args);
7239
- return typeof res === "string" ? res : JSON.stringify(res, null, 2);
7402
+ return typeof res === "string" ? res : res === void 0 || res === null ? "" : JSON.stringify(res, null, 2);
7240
7403
  };
7241
7404
  execute = async (func, options) => {
7242
7405
  const fnSpec = this.funcList.find(
@@ -7275,7 +7438,17 @@ var parseFunctions = (newFuncs, existingFuncs) => {
7275
7438
  }
7276
7439
  return [...existingFuncs ?? [], ...functions];
7277
7440
  };
7278
- var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, traceId, span, excludeContentFromTelemetry) => {
7441
+ var processFunctions = async ({
7442
+ ai,
7443
+ functionList,
7444
+ functionCalls,
7445
+ mem,
7446
+ sessionId,
7447
+ traceId,
7448
+ span,
7449
+ excludeContentFromTrace,
7450
+ index
7451
+ }) => {
7279
7452
  const funcProc = new AxFunctionProcessor(functionList);
7280
7453
  const functionsExecuted = /* @__PURE__ */ new Set();
7281
7454
  const promises = functionCalls.map((func) => {
@@ -7288,16 +7461,16 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
7288
7461
  const eventData = {
7289
7462
  name: func.name
7290
7463
  };
7291
- if (!excludeContentFromTelemetry) {
7464
+ if (!excludeContentFromTrace) {
7292
7465
  eventData.args = func.args;
7293
7466
  eventData.result = functionResult ?? "";
7294
7467
  }
7295
7468
  span.addEvent("function.call", eventData);
7296
7469
  }
7297
7470
  return {
7298
- role: "function",
7299
7471
  result: functionResult ?? "",
7300
- functionId: func.id
7472
+ functionId: func.id,
7473
+ index
7301
7474
  };
7302
7475
  }).catch((e) => {
7303
7476
  if (e instanceof FunctionError) {
@@ -7307,22 +7480,22 @@ var processFunctions = async (ai, functionList, functionCalls, mem, sessionId, t
7307
7480
  name: func.name,
7308
7481
  message: e.toString()
7309
7482
  };
7310
- if (!excludeContentFromTelemetry) {
7483
+ if (!excludeContentFromTrace) {
7311
7484
  errorEventData.args = func.args;
7312
7485
  errorEventData.fixing_instructions = result;
7313
7486
  }
7314
7487
  span.addEvent("function.error", errorEventData);
7315
7488
  }
7316
- mem.add(
7489
+ mem.addFunctionResult(
7317
7490
  {
7318
- role: "function",
7319
7491
  functionId: func.id,
7320
7492
  isError: true,
7493
+ index,
7321
7494
  result
7322
7495
  },
7323
7496
  sessionId
7324
7497
  );
7325
- mem.addTag("error");
7498
+ mem.addTag("error", sessionId);
7326
7499
  if (ai.getOptions().debug) {
7327
7500
  const logger = ai.getLogger();
7328
7501
  logger(`\u274C Function Error Correction:
@@ -7339,7 +7512,7 @@ ${result}`, {
7339
7512
  const results = await Promise.all(promises);
7340
7513
  for (const result of results) {
7341
7514
  if (result) {
7342
- mem.add(result, sessionId);
7515
+ mem.addFunctionResult(result, sessionId);
7343
7516
  }
7344
7517
  }
7345
7518
  return functionsExecuted;
@@ -7359,36 +7532,41 @@ function parseFunctionCalls(ai, functionCalls, values, model) {
7359
7532
  return funcs;
7360
7533
  }
7361
7534
 
7362
- // dsp/registry.ts
7363
- var AxInstanceRegistry = class {
7364
- reg;
7365
- // To track keys for iteration
7366
- constructor() {
7367
- this.reg = /* @__PURE__ */ new Set();
7368
- }
7369
- register(instance) {
7370
- this.reg.add(instance);
7371
- }
7372
- *[Symbol.iterator]() {
7373
- const items = Array.from(this.reg);
7374
- for (let i = 0; i < items.length; i++) {
7375
- yield items[i];
7376
- }
7377
- }
7378
- };
7379
-
7380
- // dsp/sig.ts
7381
- import { createHash } from "crypto";
7382
-
7383
- // dsp/globals.ts
7384
- var axGlobals = {
7385
- signatureStrict: true
7386
- // Controls reservedNames enforcement in signature parsing/validation
7387
- };
7535
+ // dsp/processResponse.ts
7536
+ import "stream/web";
7388
7537
 
7389
- // dsp/parser.ts
7390
- var SignatureValidationError = class extends Error {
7391
- constructor(message, position, context3, suggestion) {
7538
+ // ai/util.ts
7539
+ function mergeFunctionCalls(functionCalls, functionCallDeltas) {
7540
+ for (const _fc of functionCallDeltas) {
7541
+ const fc = functionCalls.find((fc2) => fc2.id === _fc.id);
7542
+ if (fc) {
7543
+ if (typeof _fc.function.name == "string" && _fc.function.name.length > 0) {
7544
+ fc.function.name += _fc.function.name;
7545
+ }
7546
+ if (typeof _fc.function.params == "string" && _fc.function.params.length > 0) {
7547
+ fc.function.params += _fc.function.params;
7548
+ }
7549
+ if (typeof _fc.function.params == "object") {
7550
+ fc.function.params = _fc.function.params;
7551
+ }
7552
+ } else {
7553
+ functionCalls.push(_fc);
7554
+ }
7555
+ }
7556
+ }
7557
+
7558
+ // dsp/sig.ts
7559
+ import { createHash } from "crypto";
7560
+
7561
+ // dsp/globals.ts
7562
+ var axGlobals = {
7563
+ signatureStrict: true
7564
+ // Controls reservedNames enforcement in signature parsing/validation
7565
+ };
7566
+
7567
+ // dsp/parser.ts
7568
+ var SignatureValidationError = class extends Error {
7569
+ constructor(message, position, context3, suggestion) {
7392
7570
  super(message);
7393
7571
  this.position = position;
7394
7572
  this.context = context3;
@@ -8585,32 +8763,828 @@ function validateFieldType(field, context3) {
8585
8763
  );
8586
8764
  }
8587
8765
  }
8588
- const uniqueOptions = new Set(
8589
- type.options.map((opt) => opt.trim().toLowerCase())
8590
- );
8591
- if (uniqueOptions.size !== type.options.length) {
8592
- throw new AxSignatureValidationError(
8593
- "Duplicate class options found",
8594
- field.name,
8595
- "Each class option must be unique (case-insensitive)"
8596
- );
8766
+ const uniqueOptions = new Set(
8767
+ type.options.map((opt) => opt.trim().toLowerCase())
8768
+ );
8769
+ if (uniqueOptions.size !== type.options.length) {
8770
+ throw new AxSignatureValidationError(
8771
+ "Duplicate class options found",
8772
+ field.name,
8773
+ "Each class option must be unique (case-insensitive)"
8774
+ );
8775
+ }
8776
+ }
8777
+ if (type.name === "code" && type.isArray) {
8778
+ throw new AxSignatureValidationError(
8779
+ "Arrays of code are not commonly supported",
8780
+ field.name,
8781
+ "Consider using a single code field or an array of strings instead"
8782
+ );
8783
+ }
8784
+ if (field.isInternal && context3 === "input") {
8785
+ throw new AxSignatureValidationError(
8786
+ "Internal marker (!) is not allowed on input fields",
8787
+ field.name,
8788
+ "Internal markers are only allowed on output fields"
8789
+ );
8790
+ }
8791
+ }
8792
+
8793
+ // dsp/processResponse.ts
8794
+ async function* processStreamingResponse({
8795
+ res,
8796
+ usage,
8797
+ states,
8798
+ ...args
8799
+ }) {
8800
+ const skipEarlyFail = (args.ai.getFeatures().functionCot ?? false) && args.functions !== void 0 && args.functions.length > 0;
8801
+ for await (const v of res) {
8802
+ if (v.modelUsage) {
8803
+ usage.push(v.modelUsage);
8804
+ }
8805
+ for (const result of v.results) {
8806
+ if (result.content === "" && (!result.functionCalls || result.functionCalls.length === 0)) {
8807
+ continue;
8808
+ }
8809
+ const state = states.find((s2) => s2.index === result.index);
8810
+ if (!state) {
8811
+ throw new Error(`No state found for result (index: ${result.index})`);
8812
+ }
8813
+ yield* _processStreamingResponse({
8814
+ ...args,
8815
+ result,
8816
+ skipEarlyFail,
8817
+ state
8818
+ });
8819
+ }
8820
+ }
8821
+ for (const state of states) {
8822
+ yield* finalizeStreamingResponse({
8823
+ ...args,
8824
+ state
8825
+ });
8826
+ }
8827
+ }
8828
+ async function* _processStreamingResponse({
8829
+ result,
8830
+ mem,
8831
+ sessionId,
8832
+ strictMode,
8833
+ skipEarlyFail,
8834
+ state,
8835
+ signature,
8836
+ streamingFieldProcessors,
8837
+ thoughtFieldName,
8838
+ streamingAsserts,
8839
+ asserts
8840
+ }) {
8841
+ if (result.functionCalls && result.functionCalls.length > 0) {
8842
+ mergeFunctionCalls(state.functionCalls, result.functionCalls);
8843
+ mem.updateResult(
8844
+ {
8845
+ name: result.name,
8846
+ content: result.content,
8847
+ functionCalls: state.functionCalls,
8848
+ delta: result.functionCalls?.[0]?.function?.params,
8849
+ index: result.index
8850
+ },
8851
+ sessionId
8852
+ );
8853
+ } else if (result.content && result.content.length > 0) {
8854
+ if (result.thought && result.thought.length > 0) {
8855
+ yield {
8856
+ index: result.index,
8857
+ delta: { [thoughtFieldName]: result.thought }
8858
+ };
8859
+ }
8860
+ state.content += result.content;
8861
+ mem.updateResult(
8862
+ {
8863
+ name: result.name,
8864
+ content: state.content,
8865
+ delta: result.content,
8866
+ index: result.index
8867
+ },
8868
+ sessionId
8869
+ );
8870
+ const skip = streamingExtractValues(
8871
+ signature,
8872
+ state.values,
8873
+ state.xstate,
8874
+ state.content,
8875
+ { strictMode, skipEarlyFail }
8876
+ );
8877
+ if (skip) {
8878
+ return;
8879
+ }
8880
+ if (streamingAsserts.length !== 0) {
8881
+ await assertStreamingAssertions(
8882
+ streamingAsserts,
8883
+ state.xstate,
8884
+ state.content
8885
+ );
8886
+ }
8887
+ if (streamingFieldProcessors.length !== 0) {
8888
+ await processStreamingFieldProcessors(
8889
+ streamingFieldProcessors,
8890
+ state.content,
8891
+ state.xstate,
8892
+ mem,
8893
+ state.values,
8894
+ sessionId
8895
+ );
8896
+ }
8897
+ yield* streamValues(
8898
+ signature,
8899
+ state.content,
8900
+ state.values,
8901
+ state.xstate,
8902
+ result.index
8903
+ );
8904
+ await assertAssertions(asserts, state.values);
8905
+ } else if (result.thought && result.thought.length > 0) {
8906
+ state.values[thoughtFieldName] = (state.values[thoughtFieldName] ?? "") + result.thought;
8907
+ yield {
8908
+ index: result.index,
8909
+ delta: { [thoughtFieldName]: result.thought }
8910
+ };
8911
+ }
8912
+ if (result.finishReason === "length") {
8913
+ throw new Error(
8914
+ `Max tokens reached before completion
8915
+ Content: ${state.content}`
8916
+ );
8917
+ }
8918
+ }
8919
+ async function* finalizeStreamingResponse({
8920
+ state,
8921
+ signature,
8922
+ ai,
8923
+ model,
8924
+ functions,
8925
+ mem,
8926
+ sessionId,
8927
+ traceId,
8928
+ span,
8929
+ excludeContentFromTrace,
8930
+ streamingAsserts,
8931
+ asserts,
8932
+ fieldProcessors,
8933
+ streamingFieldProcessors
8934
+ }) {
8935
+ const funcs = parseFunctionCalls(ai, state.functionCalls, state.values, model);
8936
+ if (funcs) {
8937
+ if (!functions) {
8938
+ throw new Error("Functions are not defined");
8939
+ }
8940
+ const fx = await processFunctions({
8941
+ ai,
8942
+ functionList: functions,
8943
+ functionCalls: funcs,
8944
+ mem,
8945
+ sessionId,
8946
+ traceId,
8947
+ span,
8948
+ index: state.index,
8949
+ excludeContentFromTrace
8950
+ });
8951
+ state.functionsExecuted = /* @__PURE__ */ new Set([...state.functionsExecuted, ...fx]);
8952
+ } else {
8953
+ streamingExtractFinalValue(
8954
+ signature,
8955
+ state.values,
8956
+ state.xstate,
8957
+ state.content
8958
+ );
8959
+ await assertStreamingAssertions(
8960
+ streamingAsserts,
8961
+ state.xstate,
8962
+ state.content,
8963
+ true
8964
+ );
8965
+ await assertAssertions(asserts, state.values);
8966
+ if (fieldProcessors.length) {
8967
+ await processFieldProcessors(
8968
+ fieldProcessors,
8969
+ state.values,
8970
+ mem,
8971
+ sessionId
8972
+ );
8973
+ }
8974
+ if (streamingFieldProcessors.length !== 0) {
8975
+ await processStreamingFieldProcessors(
8976
+ streamingFieldProcessors,
8977
+ state.content,
8978
+ state.xstate,
8979
+ mem,
8980
+ state.values,
8981
+ sessionId,
8982
+ true
8983
+ );
8984
+ }
8985
+ yield* streamValues(
8986
+ signature,
8987
+ state.content,
8988
+ state.values,
8989
+ state.xstate,
8990
+ state.index
8991
+ );
8992
+ }
8993
+ }
8994
+ async function* processResponse({
8995
+ ai,
8996
+ res,
8997
+ mem,
8998
+ sessionId,
8999
+ traceId,
9000
+ functions,
9001
+ span,
9002
+ strictMode,
9003
+ states,
9004
+ usage,
9005
+ excludeContentFromTrace,
9006
+ asserts,
9007
+ fieldProcessors,
9008
+ thoughtFieldName,
9009
+ signature
9010
+ }) {
9011
+ let results = res.results ?? [];
9012
+ mem.addResponse(results, sessionId);
9013
+ for (const result of results) {
9014
+ const state = states[result.index];
9015
+ if (!state) {
9016
+ throw new Error(`No state found for result (index: ${result.index})`);
9017
+ }
9018
+ if (res.modelUsage) {
9019
+ usage.push(res.modelUsage);
9020
+ }
9021
+ if (result.functionCalls?.length) {
9022
+ const funcs = parseFunctionCalls(ai, result.functionCalls, state.values);
9023
+ if (funcs) {
9024
+ if (!functions) {
9025
+ throw new Error("Functions are not defined");
9026
+ }
9027
+ const fx = await processFunctions({
9028
+ ai,
9029
+ functionList: functions,
9030
+ functionCalls: funcs,
9031
+ mem,
9032
+ sessionId,
9033
+ traceId,
9034
+ span,
9035
+ excludeContentFromTrace,
9036
+ index: result.index
9037
+ });
9038
+ state.functionsExecuted = /* @__PURE__ */ new Set([...state.functionsExecuted, ...fx]);
9039
+ }
9040
+ } else if (result.content) {
9041
+ if (result.thought && result.thought.length > 0) {
9042
+ state.values[thoughtFieldName] = result.thought;
9043
+ }
9044
+ extractValues(signature, state.values, result.content, strictMode);
9045
+ await assertAssertions(asserts, state.values);
9046
+ if (fieldProcessors.length) {
9047
+ await processFieldProcessors(
9048
+ fieldProcessors,
9049
+ state.values,
9050
+ mem,
9051
+ sessionId
9052
+ );
9053
+ }
9054
+ }
9055
+ if (result.finishReason === "length") {
9056
+ throw new Error(
9057
+ `Max tokens reached before completion
9058
+ Content: ${result.content}`
9059
+ );
9060
+ }
9061
+ }
9062
+ const values = states.map((s2) => s2.values);
9063
+ for (const v of values) {
9064
+ for (const field of signature.getOutputFields()) {
9065
+ if (field.isInternal) {
9066
+ delete v[field.name];
9067
+ }
9068
+ }
9069
+ }
9070
+ const outputFields = signature.getOutputFields();
9071
+ const deltas = values.map((v, index) => {
9072
+ const delta = {};
9073
+ for (const field of outputFields) {
9074
+ if (field.isInternal) {
9075
+ continue;
9076
+ }
9077
+ delta[field.name] = v[field.name];
9078
+ }
9079
+ if (v[thoughtFieldName] !== void 0) {
9080
+ delta[thoughtFieldName] = v[thoughtFieldName];
9081
+ }
9082
+ return { index, delta };
9083
+ });
9084
+ for (const delta of deltas) {
9085
+ yield delta;
9086
+ }
9087
+ }
9088
+ function shouldContinueSteps(mem, stopFunction, states, sessionId) {
9089
+ const lastMemItem = mem.getLast(sessionId);
9090
+ if (!lastMemItem) {
9091
+ return true;
9092
+ }
9093
+ for (const [index, state] of states.entries()) {
9094
+ const stopFunctionExecuted = stopFunction && state.functionsExecuted.has(stopFunction);
9095
+ const chat = lastMemItem.chat[index];
9096
+ if (!chat) {
9097
+ throw new Error(`No chat message found for result (index: ${index})`);
9098
+ }
9099
+ const isFunction = lastMemItem.role === "function";
9100
+ const isProcessor = lastMemItem.tags ? lastMemItem.tags.some((tag) => tag === "processor") : false;
9101
+ if (isFunction && stopFunction && stopFunctionExecuted) {
9102
+ return false;
9103
+ }
9104
+ if (!(isFunction || isProcessor)) {
9105
+ return false;
9106
+ }
9107
+ }
9108
+ return true;
9109
+ }
9110
+
9111
+ // dsp/prompt.ts
9112
+ var functionCallInstructions = `
9113
+ ## Function Call Instructions
9114
+ - Complete the task, using the functions defined earlier in this prompt.
9115
+ - Output fields should only be generated after all functions have been called.
9116
+ - Use the function results to generate the output fields.`;
9117
+ var formattingRules = `
9118
+ ## Strict Output Formatting Rules
9119
+ - Output must strictly follow the defined plain-text \`field name: value\` field format.
9120
+ - Output field, values must strictly adhere to the specified output field formatting rules.
9121
+ - No formatting rules should override these **Strict Output Formatting Rules**
9122
+ - Do not add any text before or after the output fields, just the field name and value.
9123
+ - Do not use code blocks.`;
9124
+ var AxPromptTemplate = class {
9125
+ sig;
9126
+ fieldTemplates;
9127
+ task;
9128
+ thoughtFieldName;
9129
+ functions;
9130
+ constructor(sig, options, fieldTemplates) {
9131
+ this.sig = sig;
9132
+ this.fieldTemplates = fieldTemplates;
9133
+ this.thoughtFieldName = options?.thoughtFieldName ?? "thought";
9134
+ this.functions = options?.functions;
9135
+ const task = [];
9136
+ const inArgs = renderDescFields(this.sig.getInputFields());
9137
+ const outArgs = renderDescFields(this.sig.getOutputFields());
9138
+ task.push(
9139
+ `You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
9140
+ );
9141
+ const funcs = this.functions?.map((f2) => "toFunction" in f2 ? f2.toFunction() : f2)?.flat();
9142
+ const funcList = funcs?.map((fn) => `- \`${fn.name}\`: ${formatDescription(fn.description)}`).join("\n");
9143
+ if (funcList && funcList.length > 0) {
9144
+ task.push(`## Available Functions
9145
+ ${funcList}`);
9146
+ }
9147
+ const inputFields = renderInputFields(this.sig.getInputFields());
9148
+ task.push(`## Input Fields
9149
+ ${inputFields}`);
9150
+ const outputFields = renderOutputFields(this.sig.getOutputFields());
9151
+ task.push(`## Output Fields
9152
+ ${outputFields}`);
9153
+ if (funcList && funcList.length > 0) {
9154
+ task.push(functionCallInstructions.trim());
9155
+ }
9156
+ task.push(formattingRules.trim());
9157
+ const desc = this.sig.getDescription();
9158
+ if (desc) {
9159
+ const text = formatDescription(desc);
9160
+ task.push(text);
9161
+ }
9162
+ this.task = {
9163
+ type: "text",
9164
+ text: task.join("\n\n")
9165
+ };
9166
+ }
9167
+ renderSingleValueUserContent = (values, renderedExamples, renderedDemos, examplesInSystemPrompt) => {
9168
+ const completion = this.renderInputFields(values);
9169
+ const promptList = examplesInSystemPrompt ? completion : [...renderedExamples, ...renderedDemos, ...completion];
9170
+ const prompt = promptList.filter((v) => v !== void 0);
9171
+ return prompt.every((v) => v.type === "text") ? prompt.map((v) => v.text).join("\n") : prompt.reduce(combineConsecutiveStrings("\n"), []);
9172
+ };
9173
+ render = (values, {
9174
+ examples,
9175
+ demos
9176
+ }) => {
9177
+ const renderedExamples = examples ? [
9178
+ { type: "text", text: "\n\n## Examples\n" },
9179
+ ...this.renderExamples(examples)
9180
+ ] : [];
9181
+ const renderedDemos = demos ? this.renderDemos(demos) : [];
9182
+ const allTextExamples = renderedExamples.every((v) => v.type === "text");
9183
+ const allTextDemos = renderedDemos.every((v) => v.type === "text");
9184
+ const examplesInSystemPrompt = allTextExamples && allTextDemos;
9185
+ let systemContent = this.task.text;
9186
+ if (examplesInSystemPrompt) {
9187
+ const combinedItems = [
9188
+ { type: "text", text: systemContent },
9189
+ ...renderedExamples,
9190
+ ...renderedDemos
9191
+ ];
9192
+ combinedItems.reduce(combineConsecutiveStrings(""), []);
9193
+ if (combinedItems && combinedItems[0]) {
9194
+ systemContent = combinedItems[0].text;
9195
+ }
9196
+ }
9197
+ const systemPrompt = {
9198
+ role: "system",
9199
+ content: systemContent
9200
+ };
9201
+ if (Array.isArray(values)) {
9202
+ let messages = [];
9203
+ const history = values;
9204
+ let firstItem = true;
9205
+ for (const message of history) {
9206
+ let content;
9207
+ if (firstItem) {
9208
+ content = this.renderSingleValueUserContent(
9209
+ message.values,
9210
+ renderedExamples,
9211
+ renderedDemos,
9212
+ examplesInSystemPrompt
9213
+ );
9214
+ firstItem = false;
9215
+ } else {
9216
+ content = this.renderSingleValueUserContent(
9217
+ message.values,
9218
+ [],
9219
+ [],
9220
+ false
9221
+ );
9222
+ }
9223
+ if (message.role === "user") {
9224
+ messages.push({ role: "user", content });
9225
+ continue;
9226
+ }
9227
+ if (message.role !== "assistant") {
9228
+ throw new Error("Invalid message role");
9229
+ }
9230
+ if (typeof content !== "string") {
9231
+ throw new Error(
9232
+ "Assistant message cannot contain non-text content like images, files,etc"
9233
+ );
9234
+ }
9235
+ messages.push({ role: "assistant", content });
9236
+ }
9237
+ return [systemPrompt, ...messages];
9238
+ }
9239
+ const userContent = this.renderSingleValueUserContent(
9240
+ values,
9241
+ renderedExamples,
9242
+ renderedDemos,
9243
+ examplesInSystemPrompt
9244
+ );
9245
+ return [systemPrompt, { role: "user", content: userContent }];
9246
+ };
9247
+ renderExtraFields = (extraFields) => {
9248
+ const prompt = [];
9249
+ if (!extraFields || extraFields.length === 0) {
9250
+ return prompt;
9251
+ }
9252
+ const groupedFields = extraFields.reduce(
9253
+ (acc, field) => {
9254
+ const title = field.title;
9255
+ if (!acc[title]) {
9256
+ acc[title] = [];
9257
+ }
9258
+ acc[title].push(field);
9259
+ return acc;
9260
+ },
9261
+ {}
9262
+ );
9263
+ const formattedGroupedFields = Object.entries(groupedFields).map(([title, fields]) => {
9264
+ if (fields.length === 1) {
9265
+ const field = fields[0];
9266
+ return {
9267
+ title,
9268
+ name: field.name,
9269
+ description: field.description
9270
+ };
9271
+ } else if (fields.length > 1) {
9272
+ const valuesList = fields.map((field) => `- ${field.description}`).join("\n");
9273
+ return {
9274
+ title,
9275
+ name: fields[0].name,
9276
+ description: valuesList
9277
+ };
9278
+ }
9279
+ }).filter(Boolean);
9280
+ formattedGroupedFields.forEach((field) => {
9281
+ const fn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
9282
+ prompt.push(...fn(field, field.description));
9283
+ });
9284
+ return prompt;
9285
+ };
9286
+ renderExamples = (data) => {
9287
+ const list = [];
9288
+ const exampleContext = {
9289
+ isExample: true
9290
+ };
9291
+ for (const [index, item] of data.entries()) {
9292
+ const renderedInputItem = this.sig.getInputFields().map(
9293
+ (field) => this.renderInField(field, item, {
9294
+ ...exampleContext,
9295
+ isInputField: true
9296
+ })
9297
+ ).filter((v) => v !== void 0).flat();
9298
+ const renderedOutputItem = this.sig.getOutputFields().map(
9299
+ (field) => this.renderInField(field, item, {
9300
+ ...exampleContext,
9301
+ isInputField: false
9302
+ })
9303
+ ).filter((v) => v !== void 0).flat();
9304
+ const renderedItem = [...renderedInputItem, ...renderedOutputItem];
9305
+ if (index > 0 && renderedItem.length > 0 && renderedItem[0]?.type === "text") {
9306
+ list.push({ type: "text", text: "---\n\n" });
9307
+ }
9308
+ renderedItem.forEach((v) => {
9309
+ if ("text" in v) {
9310
+ v.text = v.text + "\n";
9311
+ }
9312
+ list.push(v);
9313
+ });
9314
+ }
9315
+ return list;
9316
+ };
9317
+ renderDemos = (data) => {
9318
+ const list = [];
9319
+ const inputFields = this.sig.getInputFields();
9320
+ const outputFields = this.sig.getOutputFields();
9321
+ const demoContext = {
9322
+ isExample: true
9323
+ };
9324
+ for (const item of data) {
9325
+ const inputRenderedItems = inputFields.map(
9326
+ (field) => this.renderInField(field, item, {
9327
+ ...demoContext,
9328
+ isInputField: true
9329
+ })
9330
+ ).filter((v) => v !== void 0).flat();
9331
+ const outputRenderedItems = outputFields.map(
9332
+ (field) => this.renderInField(field, item, {
9333
+ ...demoContext,
9334
+ isInputField: false
9335
+ })
9336
+ ).filter((v) => v !== void 0).flat();
9337
+ const renderedItem = [...inputRenderedItems, ...outputRenderedItems];
9338
+ renderedItem.slice(0, -1).forEach((v) => {
9339
+ if ("text" in v) {
9340
+ v.text = v.text + "\n";
9341
+ }
9342
+ list.push(v);
9343
+ });
9344
+ }
9345
+ return list;
9346
+ };
9347
+ renderInputFields = (values) => {
9348
+ const renderedItems = this.sig.getInputFields().map((field) => this.renderInField(field, values, void 0)).filter((v) => v !== void 0).flat();
9349
+ renderedItems.filter((v) => v.type === "text").forEach((v) => {
9350
+ v.text = v.text + "\n";
9351
+ });
9352
+ return renderedItems;
9353
+ };
9354
+ renderInField = (field, values, context3) => {
9355
+ const value = values[field.name];
9356
+ if (isEmptyValue(field, value, context3)) {
9357
+ return;
9358
+ }
9359
+ if (field.type) {
9360
+ validateValue(field, value);
9361
+ }
9362
+ const processedValue = processValue(field, value);
9363
+ const textFieldFn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
9364
+ return textFieldFn(field, processedValue);
9365
+ };
9366
+ defaultRenderInField = (field, value) => {
9367
+ if (field.type?.name === "image") {
9368
+ const validateImage = (value2) => {
9369
+ if (!value2) {
9370
+ throw new Error("Image field value is required.");
9371
+ }
9372
+ if (typeof value2 !== "object") {
9373
+ throw new Error("Image field value must be an object.");
9374
+ }
9375
+ if (!("mimeType" in value2)) {
9376
+ throw new Error("Image field must have mimeType");
9377
+ }
9378
+ if (!("data" in value2)) {
9379
+ throw new Error("Image field must have data");
9380
+ }
9381
+ return value2;
9382
+ };
9383
+ let result = [
9384
+ { type: "text", text: `${field.title}: ` }
9385
+ ];
9386
+ if (field.type.isArray) {
9387
+ if (!Array.isArray(value)) {
9388
+ throw new Error("Image field value must be an array.");
9389
+ }
9390
+ result = result.concat(
9391
+ value.map((v) => {
9392
+ const validated = validateImage(v);
9393
+ return {
9394
+ type: "image",
9395
+ mimeType: validated.mimeType,
9396
+ image: validated.data
9397
+ };
9398
+ })
9399
+ );
9400
+ } else {
9401
+ const validated = validateImage(value);
9402
+ result.push({
9403
+ type: "image",
9404
+ mimeType: validated.mimeType,
9405
+ image: validated.data
9406
+ });
9407
+ }
9408
+ return result;
9409
+ }
9410
+ if (field.type?.name === "audio") {
9411
+ const validateAudio = (value2) => {
9412
+ if (!value2) {
9413
+ throw new Error("Audio field value is required.");
9414
+ }
9415
+ if (typeof value2 !== "object") {
9416
+ throw new Error("Audio field value must be an object.");
9417
+ }
9418
+ if (!("data" in value2)) {
9419
+ throw new Error("Audio field must have data");
9420
+ }
9421
+ return value2;
9422
+ };
9423
+ let result = [
9424
+ { type: "text", text: `${field.title}: ` }
9425
+ ];
9426
+ if (field.type.isArray) {
9427
+ if (!Array.isArray(value)) {
9428
+ throw new Error("Audio field value must be an array.");
9429
+ }
9430
+ result = result.concat(
9431
+ value.map((v) => {
9432
+ const validated = validateAudio(v);
9433
+ return {
9434
+ type: "audio",
9435
+ format: validated.format ?? "wav",
9436
+ data: validated.data
9437
+ };
9438
+ })
9439
+ );
9440
+ } else {
9441
+ const validated = validateAudio(value);
9442
+ result.push({
9443
+ type: "audio",
9444
+ format: validated.format ?? "wav",
9445
+ data: validated.data
9446
+ });
9447
+ }
9448
+ return result;
9449
+ }
9450
+ const text = [field.title, ": "];
9451
+ if (Array.isArray(value)) {
9452
+ text.push("\n");
9453
+ text.push(value.map((v) => `- ${v}`).join("\n"));
9454
+ } else {
9455
+ text.push(value);
9456
+ }
9457
+ return [{ type: "text", text: text.join("") }];
9458
+ };
9459
+ };
9460
+ var renderDescFields = (list) => list.map((v) => `\`${v.title}\``).join(", ");
9461
+ var renderInputFields = (fields) => {
9462
+ const rows = fields.map((field) => {
9463
+ const name = field.title;
9464
+ const type = field.type?.name ? toFieldType(field.type) : "string";
9465
+ const requiredMsg = field.isOptional ? `This optional ${type} field may be omitted` : `A ${type} field`;
9466
+ const description = field.description ? ` ${formatDescription(field.description)}` : "";
9467
+ return `${name}: (${requiredMsg})${description}`.trim();
9468
+ });
9469
+ return rows.join("\n");
9470
+ };
9471
+ var renderOutputFields = (fields) => {
9472
+ const rows = fields.map((field) => {
9473
+ const name = field.title;
9474
+ const type = field.type?.name ? toFieldType(field.type) : "string";
9475
+ const requiredMsg = field.isOptional ? `Only include this ${type} field if its value is available` : `This ${type} field must be included`;
9476
+ let description = "";
9477
+ if (field.description && field.description.length > 0) {
9478
+ const value = field.type?.name === "class" ? field.description : formatDescription(field.description);
9479
+ description = ` ${value}`;
9480
+ }
9481
+ if (field.type?.options && field.type.options.length > 0) {
9482
+ if (description.length > 0) {
9483
+ description += `. `;
9484
+ }
9485
+ description += `Allowed values: ${field.type.options.join(", ")}`;
9486
+ }
9487
+ return `${name}: (${requiredMsg})${description}`.trim();
9488
+ });
9489
+ return rows.join("\n");
9490
+ };
9491
+ var processValue = (field, value) => {
9492
+ if (field.type?.name === "date" && value instanceof Date) {
9493
+ const v = value.toISOString();
9494
+ return v.slice(0, v.indexOf("T"));
9495
+ }
9496
+ if (field.type?.name === "datetime" && value instanceof Date) {
9497
+ return formatDateWithTimezone(value);
9498
+ }
9499
+ if (field.type?.name === "image" && typeof value === "object") {
9500
+ return value;
9501
+ }
9502
+ if (field.type?.name === "audio" && typeof value === "object") {
9503
+ return value;
9504
+ }
9505
+ if (typeof value === "string") {
9506
+ return value;
9507
+ }
9508
+ return JSON.stringify(value, null, 2);
9509
+ };
9510
+ var toFieldType = (type) => {
9511
+ const baseType = (() => {
9512
+ switch (type?.name) {
9513
+ case "string":
9514
+ return "string";
9515
+ case "number":
9516
+ return "number";
9517
+ case "boolean":
9518
+ return "boolean";
9519
+ case "date":
9520
+ return 'date ("YYYY-MM-DD" format)';
9521
+ case "datetime":
9522
+ return 'date time ("YYYY-MM-DD HH:mm Timezone" format)';
9523
+ case "json":
9524
+ return "JSON object";
9525
+ case "class":
9526
+ return "classification class";
9527
+ case "code":
9528
+ return "code";
9529
+ default:
9530
+ return "string";
9531
+ }
9532
+ })();
9533
+ return type?.isArray ? `json array of ${baseType} items` : baseType;
9534
+ };
9535
+ function combineConsecutiveStrings(separator) {
9536
+ return (acc, current) => {
9537
+ if (current.type === "text") {
9538
+ const previous = acc.length > 0 ? acc[acc.length - 1] : null;
9539
+ if (previous && previous.type === "text") {
9540
+ previous.text += separator + current.text;
9541
+ } else {
9542
+ acc.push(current);
9543
+ }
9544
+ } else {
9545
+ acc.push(current);
9546
+ }
9547
+ return acc;
9548
+ };
9549
+ }
9550
+ var isEmptyValue = (field, value, context3) => {
9551
+ if (typeof value === "boolean") {
9552
+ return false;
9553
+ }
9554
+ if (!value || (Array.isArray(value) || typeof value === "string") && value.length === 0) {
9555
+ if (context3?.isExample) {
9556
+ return true;
8597
9557
  }
9558
+ if (field.isOptional || field.isInternal) {
9559
+ return true;
9560
+ }
9561
+ const fieldType = context3?.isInputField !== false ? "input" : "output";
9562
+ throw new Error(`Value for ${fieldType} field '${field.name}' is required.`);
8598
9563
  }
8599
- if (type.name === "code" && type.isArray) {
8600
- throw new AxSignatureValidationError(
8601
- "Arrays of code are not commonly supported",
8602
- field.name,
8603
- "Consider using a single code field or an array of strings instead"
8604
- );
9564
+ return false;
9565
+ };
9566
+ function formatDescription(str) {
9567
+ const value = str.trim();
9568
+ return value.length > 0 ? `${value.charAt(0).toUpperCase()}${value.slice(1)}${value.endsWith(".") ? "" : "."}` : "";
9569
+ }
9570
+
9571
+ // dsp/registry.ts
9572
+ var AxInstanceRegistry = class {
9573
+ reg;
9574
+ // To track keys for iteration
9575
+ constructor() {
9576
+ this.reg = /* @__PURE__ */ new Set();
8605
9577
  }
8606
- if (field.isInternal && context3 === "input") {
8607
- throw new AxSignatureValidationError(
8608
- "Internal marker (!) is not allowed on input fields",
8609
- field.name,
8610
- "Internal markers are only allowed on output fields"
8611
- );
9578
+ register(instance) {
9579
+ this.reg.add(instance);
8612
9580
  }
8613
- }
9581
+ *[Symbol.iterator]() {
9582
+ const items = Array.from(this.reg);
9583
+ for (let i = 0; i < items.length; i++) {
9584
+ yield items[i];
9585
+ }
9586
+ }
9587
+ };
8614
9588
 
8615
9589
  // dsp/program.ts
8616
9590
  var AxProgramWithSignature = class {
@@ -8813,6 +9787,28 @@ var AxProgram = class {
8813
9787
  }
8814
9788
  };
8815
9789
 
9790
+ // dsp/validate.ts
9791
+ function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId) {
9792
+ mem.addRequest(
9793
+ [
9794
+ {
9795
+ role: "user",
9796
+ content: promptTemplate.renderExtraFields(errorFields)
9797
+ }
9798
+ ],
9799
+ sessionId
9800
+ );
9801
+ mem.addTag("error", sessionId);
9802
+ if (ai.getOptions().debug) {
9803
+ const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
9804
+ const logger = ai.getLogger();
9805
+ logger(`\u274C Error Correction:
9806
+ ${errors}`, {
9807
+ tags: ["error"]
9808
+ });
9809
+ }
9810
+ }
9811
+
8816
9812
  // dsp/generate.ts
8817
9813
  var AxGen = class extends AxProgramWithSignature {
8818
9814
  promptTemplate;
@@ -8820,10 +9816,8 @@ var AxGen = class extends AxProgramWithSignature {
8820
9816
  streamingAsserts;
8821
9817
  options;
8822
9818
  functions;
8823
- functionsExecuted = /* @__PURE__ */ new Set();
8824
9819
  fieldProcessors = [];
8825
9820
  streamingFieldProcessors = [];
8826
- values = {};
8827
9821
  excludeContentFromTrace = false;
8828
9822
  thoughtFieldName;
8829
9823
  constructor(signature, options) {
@@ -8846,6 +9840,20 @@ var AxGen = class extends AxProgramWithSignature {
8846
9840
  this.functions = parseFunctions(options.functions);
8847
9841
  }
8848
9842
  }
9843
+ createStates(n) {
9844
+ return Array.from({ length: n }, (_, index) => ({
9845
+ index,
9846
+ functionCalls: [],
9847
+ values: {},
9848
+ content: "",
9849
+ functionsExecuted: /* @__PURE__ */ new Set(),
9850
+ xstate: {
9851
+ extractedFields: [],
9852
+ streamedIndex: {},
9853
+ s: -1
9854
+ }
9855
+ }));
9856
+ }
8849
9857
  addAssert = (fn, message) => {
8850
9858
  this.asserts.push({ fn, message });
8851
9859
  };
@@ -8886,7 +9894,6 @@ var AxGen = class extends AxProgramWithSignature {
8886
9894
  const {
8887
9895
  sessionId,
8888
9896
  traceId,
8889
- modelConfig,
8890
9897
  model,
8891
9898
  rateLimiter,
8892
9899
  stream,
@@ -8895,7 +9902,7 @@ var AxGen = class extends AxProgramWithSignature {
8895
9902
  thinkingTokenBudget,
8896
9903
  showThoughts
8897
9904
  } = options ?? {};
8898
- const chatPrompt = mem?.history(sessionId) ?? [];
9905
+ const chatPrompt = mem?.history(0, sessionId) ?? [];
8899
9906
  if (chatPrompt.length === 0) {
8900
9907
  throw new Error("No chat prompt found");
8901
9908
  }
@@ -8904,6 +9911,10 @@ var AxGen = class extends AxProgramWithSignature {
8904
9911
  if (!firstStep && (functionCall === "required" || typeof functionCall === "function")) {
8905
9912
  functionCall = void 0;
8906
9913
  }
9914
+ const modelConfig = {
9915
+ ...options?.modelConfig,
9916
+ ...options?.sampleCount ? { n: options.sampleCount } : {}
9917
+ };
8907
9918
  const res = await ai.chat(
8908
9919
  {
8909
9920
  chatPrompt,
@@ -8936,9 +9947,11 @@ var AxGen = class extends AxProgramWithSignature {
8936
9947
  traceContext
8937
9948
  }) {
8938
9949
  const { sessionId, traceId, functions: _functions } = options ?? {};
8939
- const fastFail = options?.fastFail ?? this.options?.fastFail;
9950
+ const strictMode = options?.strictMode ?? false;
8940
9951
  const model = options.model;
8941
- const functions = _functions?.map((f2) => "toFunction" in f2 ? f2.toFunction() : f2)?.flat();
9952
+ const states = this.createStates(options.sampleCount ?? 1);
9953
+ const usage = this.usage;
9954
+ const functions = _functions?.map((f2) => "toFunction" in f2 ? f2.toFunction() : f2)?.flat() ?? [];
8942
9955
  const res = await this.forwardSendRequest({
8943
9956
  ai,
8944
9957
  mem,
@@ -8946,8 +9959,8 @@ var AxGen = class extends AxProgramWithSignature {
8946
9959
  traceContext,
8947
9960
  firstStep
8948
9961
  });
8949
- if (res instanceof ReadableStream2) {
8950
- yield* this.processStreamingResponse({
9962
+ if (res instanceof ReadableStream3) {
9963
+ yield* processStreamingResponse({
8951
9964
  ai,
8952
9965
  model,
8953
9966
  res,
@@ -8955,12 +9968,21 @@ var AxGen = class extends AxProgramWithSignature {
8955
9968
  traceId,
8956
9969
  sessionId,
8957
9970
  functions,
8958
- fastFail,
8959
- span
9971
+ strictMode,
9972
+ span,
9973
+ states,
9974
+ usage,
9975
+ asserts: this.asserts,
9976
+ streamingAsserts: this.streamingAsserts,
9977
+ fieldProcessors: this.fieldProcessors,
9978
+ streamingFieldProcessors: this.streamingFieldProcessors,
9979
+ thoughtFieldName: this.thoughtFieldName,
9980
+ excludeContentFromTrace: this.excludeContentFromTrace,
9981
+ signature: this.signature
8960
9982
  });
8961
9983
  this.getLogger(ai, options)?.("", { tags: ["responseEnd"] });
8962
9984
  } else {
8963
- yield await this.processResponse({
9985
+ yield* processResponse({
8964
9986
  ai,
8965
9987
  model,
8966
9988
  res,
@@ -8968,233 +9990,19 @@ var AxGen = class extends AxProgramWithSignature {
8968
9990
  traceId,
8969
9991
  sessionId,
8970
9992
  functions,
8971
- span
8972
- });
8973
- }
8974
- }
8975
- async *processStreamingResponse({
8976
- ai,
8977
- model,
8978
- res,
8979
- mem,
8980
- sessionId,
8981
- traceId,
8982
- functions,
8983
- fastFail,
8984
- span
8985
- }) {
8986
- const streamingValidation = fastFail ?? ai.getFeatures(model).functionCot !== true;
8987
- const functionCalls = [];
8988
- this.values = {};
8989
- const xstate = {
8990
- extractedFields: [],
8991
- streamedIndex: {},
8992
- s: -1
8993
- };
8994
- let content = "";
8995
- mem.addResult(
8996
- {
8997
- content: "",
8998
- functionCalls: []
8999
- },
9000
- sessionId
9001
- );
9002
- for await (const v of res) {
9003
- const result = v.results[0];
9004
- if (!result) {
9005
- continue;
9006
- }
9007
- if (v.modelUsage) {
9008
- this.usage.push(v.modelUsage);
9009
- }
9010
- if (result.functionCalls && result.functionCalls.length > 0) {
9011
- mergeFunctionCalls(functionCalls, result.functionCalls);
9012
- mem.updateResult(
9013
- {
9014
- name: result.name,
9015
- content,
9016
- functionCalls,
9017
- delta: result.functionCalls?.[0]?.function?.params
9018
- },
9019
- sessionId
9020
- );
9021
- } else if (result.content && result.content.length > 0) {
9022
- if (result.thought && result.thought.length > 0) {
9023
- yield {
9024
- [this.thoughtFieldName]: result.thought
9025
- };
9026
- }
9027
- content += result.content;
9028
- mem.updateResult(
9029
- { name: result.name, content, delta: result.content },
9030
- sessionId
9031
- );
9032
- const skip = streamingExtractValues(
9033
- this.signature,
9034
- this.values,
9035
- xstate,
9036
- content,
9037
- streamingValidation
9038
- );
9039
- if (skip) {
9040
- continue;
9041
- }
9042
- if (this.streamingAsserts.length !== 0) {
9043
- await assertStreamingAssertions(
9044
- this.streamingAsserts,
9045
- xstate,
9046
- content
9047
- );
9048
- }
9049
- if (this.streamingFieldProcessors.length !== 0) {
9050
- await processStreamingFieldProcessors(
9051
- this.streamingFieldProcessors,
9052
- content,
9053
- xstate,
9054
- mem,
9055
- this.values,
9056
- sessionId
9057
- );
9058
- }
9059
- yield* streamValues(
9060
- this.signature,
9061
- content,
9062
- this.values,
9063
- xstate
9064
- );
9065
- await assertAssertions(this.asserts, this.values);
9066
- } else if (result.thought && result.thought.length > 0) {
9067
- this.values[this.thoughtFieldName] = (this.values[this.thoughtFieldName] ?? "") + result.thought;
9068
- yield {
9069
- [this.thoughtFieldName]: result.thought
9070
- };
9071
- }
9072
- if (result.finishReason === "length") {
9073
- throw new Error(
9074
- `Max tokens reached before completion
9075
- Content: ${content}`
9076
- );
9077
- }
9078
- }
9079
- const funcs = parseFunctionCalls(ai, functionCalls, this.values, model);
9080
- if (funcs) {
9081
- if (!functions) {
9082
- throw new Error("Functions are not defined");
9083
- }
9084
- const fx = await processFunctions(
9085
- ai,
9086
- functions,
9087
- funcs,
9088
- mem,
9089
- sessionId,
9090
- traceId,
9091
9993
  span,
9092
- this.excludeContentFromTrace
9093
- );
9094
- this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
9095
- } else {
9096
- streamingExtractFinalValue(this.signature, this.values, xstate, content);
9097
- await assertStreamingAssertions(
9098
- this.streamingAsserts,
9099
- xstate,
9100
- content,
9101
- true
9102
- );
9103
- await assertAssertions(this.asserts, this.values);
9104
- if (this.fieldProcessors.length) {
9105
- await processFieldProcessors(
9106
- this.fieldProcessors,
9107
- this.values,
9108
- mem,
9109
- sessionId
9110
- );
9111
- }
9112
- if (this.streamingFieldProcessors.length !== 0) {
9113
- await processStreamingFieldProcessors(
9114
- this.streamingFieldProcessors,
9115
- content,
9116
- xstate,
9117
- mem,
9118
- this.values,
9119
- sessionId,
9120
- true
9121
- );
9122
- }
9123
- yield* streamValues(
9124
- this.signature,
9125
- content,
9126
- this.values,
9127
- xstate
9128
- );
9129
- }
9130
- }
9131
- async processResponse({
9132
- ai,
9133
- res,
9134
- mem,
9135
- sessionId,
9136
- traceId,
9137
- functions,
9138
- span
9139
- }) {
9140
- this.values = {};
9141
- let results = res.results ?? [];
9142
- if (results.length > 1) {
9143
- results = results.filter((r) => r.functionCalls);
9144
- }
9145
- for (const result of results) {
9146
- if (res.modelUsage) {
9147
- this.usage.push(res.modelUsage);
9148
- }
9149
- mem.addResult(result, sessionId);
9150
- if (result.functionCalls?.length) {
9151
- const funcs = parseFunctionCalls(ai, result.functionCalls, this.values);
9152
- if (funcs) {
9153
- if (!functions) {
9154
- throw new Error("Functions are not defined");
9155
- }
9156
- const fx = await processFunctions(
9157
- ai,
9158
- functions,
9159
- funcs,
9160
- mem,
9161
- sessionId,
9162
- traceId,
9163
- span,
9164
- this.excludeContentFromTrace
9165
- );
9166
- this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
9167
- }
9168
- } else if (result.content) {
9169
- if (result.thought && result.thought.length > 0) {
9170
- this.values[this.thoughtFieldName] = result.thought;
9171
- }
9172
- extractValues(this.signature, this.values, result.content);
9173
- await assertAssertions(this.asserts, this.values);
9174
- if (this.fieldProcessors.length) {
9175
- await processFieldProcessors(
9176
- this.fieldProcessors,
9177
- this.values,
9178
- mem,
9179
- sessionId
9180
- );
9181
- }
9182
- }
9183
- if (result.finishReason === "length") {
9184
- throw new Error(
9185
- `Max tokens reached before completion
9186
- Content: ${result.content}`
9187
- );
9188
- }
9189
- }
9190
- for (const field of this.signature.getOutputFields()) {
9191
- if (field.isInternal) {
9192
- delete this.values[field.name];
9193
- }
9994
+ strictMode,
9995
+ states,
9996
+ usage,
9997
+ asserts: this.asserts,
9998
+ fieldProcessors: this.fieldProcessors,
9999
+ thoughtFieldName: this.thoughtFieldName,
10000
+ excludeContentFromTrace: this.excludeContentFromTrace,
10001
+ signature: this.signature
10002
+ });
9194
10003
  }
9195
- return { ...this.values };
9196
10004
  }
9197
- async *_forward2(ai, values, options, span, traceContext) {
10005
+ async *_forward2(ai, values, states, options, span, traceContext) {
9198
10006
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
9199
10007
  const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
9200
10008
  const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
@@ -9203,7 +10011,7 @@ Content: ${result.content}`
9203
10011
  debug: this.isDebug(ai, options),
9204
10012
  debugHideSystemPrompt
9205
10013
  };
9206
- const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, memOptions);
10014
+ const mem = options.mem ?? this.options?.mem ?? new AxMemory(memOptions);
9207
10015
  let err;
9208
10016
  if (options?.functions && options.functions.length > 0) {
9209
10017
  const promptTemplateClass = this.options?.promptTemplate ?? AxPromptTemplate;
@@ -9230,7 +10038,7 @@ Content: ${result.content}`
9230
10038
  demos: this.demos
9231
10039
  });
9232
10040
  }
9233
- mem.add(prompt, options?.sessionId);
10041
+ mem.addRequest(prompt, options.sessionId);
9234
10042
  multiStepLoop: for (let n = 0; n < maxSteps; n++) {
9235
10043
  const firstStep = n === 0;
9236
10044
  for (let errCount = 0; errCount < maxRetries; errCount++) {
@@ -9243,15 +10051,20 @@ Content: ${result.content}`
9243
10051
  span,
9244
10052
  traceContext
9245
10053
  });
9246
- for await (const delta of generator) {
9247
- if (delta !== void 0) {
9248
- yield { version: errCount, delta };
10054
+ for await (const result of generator) {
10055
+ if (result !== void 0) {
10056
+ yield {
10057
+ version: errCount,
10058
+ index: result.index,
10059
+ delta: result.delta
10060
+ };
9249
10061
  }
9250
10062
  }
9251
- const lastMemItem = mem.getLast(options?.sessionId);
9252
- const shouldContinue = this.shouldContinueSteps(
9253
- lastMemItem,
9254
- stopFunction
10063
+ const shouldContinue = shouldContinueSteps(
10064
+ mem,
10065
+ stopFunction,
10066
+ states,
10067
+ options?.sessionId
9255
10068
  );
9256
10069
  if (shouldContinue) {
9257
10070
  continue multiStepLoop;
@@ -9307,26 +10120,15 @@ Content: ${result.content}`
9307
10120
  this.signature
9308
10121
  );
9309
10122
  }
9310
- shouldContinueSteps(lastMemItem, stopFunction) {
9311
- const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
9312
- const isFunction = lastMemItem?.chat?.role === "function";
9313
- const isProcessor = lastMemItem?.tags ? lastMemItem.tags.some((tag) => tag === "processor") : false;
9314
- if (isFunction && stopFunction && stopFunctionExecuted) {
9315
- return false;
9316
- }
9317
- if (isFunction || isProcessor) {
9318
- return true;
9319
- }
9320
- return false;
9321
- }
9322
10123
  async *_forward1(ai, values, options) {
10124
+ const states = this.createStates(options.sampleCount ?? 1);
9323
10125
  const tracer = options?.tracer ?? this.options?.tracer ?? ai.getOptions().tracer;
9324
10126
  let functions = this.functions;
9325
10127
  if (options?.functions) {
9326
10128
  functions = parseFunctions(options.functions, this.functions);
9327
10129
  }
9328
10130
  if (!tracer) {
9329
- yield* this._forward2(ai, values, {
10131
+ yield* this._forward2(ai, values, states, {
9330
10132
  ...options,
9331
10133
  functions
9332
10134
  });
@@ -9341,8 +10143,7 @@ Content: ${result.content}`
9341
10143
  ...options?.thinkingTokenBudget ? { thinking_token_budget: options.thinkingTokenBudget } : {},
9342
10144
  ...options?.showThoughts ? { show_thoughts: options.showThoughts } : {},
9343
10145
  ...options?.maxSteps ? { max_steps: options.maxSteps } : {},
9344
- ...options?.maxRetries ? { max_retries: options.maxRetries } : {},
9345
- ...options?.fastFail ? { fast_fail: options.fastFail } : {}
10146
+ ...options?.maxRetries ? { max_retries: options.maxRetries } : {}
9346
10147
  };
9347
10148
  const traceLabel = options.traceLabel ?? this.options?.traceLabel;
9348
10149
  const spanName = traceLabel ? `${traceLabel} (AxGen)` : "AxGen";
@@ -9359,6 +10160,7 @@ Content: ${result.content}`
9359
10160
  yield* this._forward2(
9360
10161
  ai,
9361
10162
  values,
10163
+ states,
9362
10164
  {
9363
10165
  ...options,
9364
10166
  functions
@@ -9367,8 +10169,10 @@ Content: ${result.content}`
9367
10169
  traceContext
9368
10170
  );
9369
10171
  if (!this.excludeContentFromTrace) {
10172
+ const valuesList = states.map((s2) => s2.values);
10173
+ const values2 = valuesList.length === 1 ? valuesList[0] : valuesList;
9370
10174
  span.addEvent("output", {
9371
- content: JSON.stringify(this.values, null, 2)
10175
+ content: JSON.stringify(values2, null, 2)
9372
10176
  });
9373
10177
  }
9374
10178
  } finally {
@@ -9377,17 +10181,18 @@ Content: ${result.content}`
9377
10181
  }
9378
10182
  async forward(ai, values, options) {
9379
10183
  const generator = this._forward1(ai, values, options ?? {});
9380
- let buffer = {};
10184
+ let buffer = [];
9381
10185
  let currentVersion = 0;
9382
- for await (const item of generator) {
9383
- if (item.version !== currentVersion) {
9384
- buffer = {};
10186
+ for await (const delta of generator) {
10187
+ if (delta.version !== currentVersion) {
10188
+ buffer = [];
9385
10189
  }
9386
- currentVersion = item.version;
9387
- buffer = mergeDeltas(buffer, item.delta);
10190
+ currentVersion = delta.version;
10191
+ buffer = mergeDeltas(buffer, delta);
9388
10192
  }
9389
- this.trace = { ...values, ...buffer };
9390
- return buffer;
10193
+ const result = buffer[0]?.delta ?? {};
10194
+ this.trace = { ...values, ...result };
10195
+ return result;
9391
10196
  }
9392
10197
  async *streamingForward(ai, values, options) {
9393
10198
  yield* this._forward1(ai, values, {
@@ -9408,9 +10213,13 @@ Content: ${result.content}`
9408
10213
  var AxGenerateError = class extends Error {
9409
10214
  details;
9410
10215
  constructor(message, details, options) {
9411
- super(message, options);
10216
+ super(message);
9412
10217
  this.name = "AxGenerateError";
9413
10218
  this.details = details;
10219
+ if (options?.cause) {
10220
+ ;
10221
+ this.cause = options.cause;
10222
+ }
9414
10223
  }
9415
10224
  };
9416
10225
  function enhanceError(e, ai, signature) {
@@ -10107,6 +10916,9 @@ var AxBaseOptimizer = class {
10107
10916
  checkpointLoad;
10108
10917
  checkpointInterval;
10109
10918
  resumeFromCheckpoint;
10919
+ // Logging fields
10920
+ logger;
10921
+ verbose;
10110
10922
  // Checkpoint state
10111
10923
  currentRound = 0;
10112
10924
  scoreHistory = [];
@@ -10130,6 +10942,8 @@ var AxBaseOptimizer = class {
10130
10942
  this.checkpointLoad = args.checkpointLoad;
10131
10943
  this.checkpointInterval = args.checkpointInterval ?? 10;
10132
10944
  this.resumeFromCheckpoint = args.resumeFromCheckpoint;
10945
+ this.logger = args.logger;
10946
+ this.verbose = args.verbose;
10133
10947
  const costTracker = new AxDefaultCostTracker({
10134
10948
  maxTokens: 1e6
10135
10949
  });
@@ -10308,8 +11122,14 @@ var AxBaseOptimizer = class {
10308
11122
  async compilePareto(program, metricFn, options) {
10309
11123
  const startTime = Date.now();
10310
11124
  if (options?.verbose) {
10311
- console.log("Starting Pareto optimization using base implementation");
10312
- console.log("This will run multiple single-objective optimizations");
11125
+ this.getLogger(options)?.(
11126
+ "Starting Pareto optimization using base implementation",
11127
+ { tags: ["discovery"] }
11128
+ );
11129
+ this.getLogger(options)?.(
11130
+ "This will run multiple single-objective optimizations",
11131
+ { tags: ["discovery"] }
11132
+ );
10313
11133
  }
10314
11134
  const solutions = await this.generateWeightedSolutions(
10315
11135
  program,
@@ -10323,13 +11143,22 @@ var AxBaseOptimizer = class {
10323
11143
  );
10324
11144
  const allSolutions = [...solutions, ...constraintSolutions];
10325
11145
  if (options?.verbose) {
10326
- console.log(`Generated ${allSolutions.length} candidate solutions`);
11146
+ this.getLogger(options)?.(
11147
+ `Generated ${allSolutions.length} candidate solutions`,
11148
+ { tags: ["discovery"] }
11149
+ );
10327
11150
  }
10328
11151
  const paretoFront = this.findParetoFrontier(allSolutions);
10329
11152
  const hypervolume = this.calculateHypervolume(paretoFront);
10330
11153
  if (options?.verbose) {
10331
- console.log(`Found ${paretoFront.length} non-dominated solutions`);
10332
- console.log(`Hypervolume: ${hypervolume?.toFixed(4) || "N/A"}`);
11154
+ this.getLogger(options)?.(
11155
+ `Found ${paretoFront.length} non-dominated solutions`,
11156
+ { tags: ["discovery"] }
11157
+ );
11158
+ this.getLogger(options)?.(
11159
+ `Hypervolume: ${hypervolume?.toFixed(4) || "N/A"}`,
11160
+ { tags: ["discovery"] }
11161
+ );
10333
11162
  }
10334
11163
  this.updateResourceUsage(startTime);
10335
11164
  this.stats.convergenceInfo.converged = true;
@@ -10367,13 +11196,19 @@ var AxBaseOptimizer = class {
10367
11196
  });
10368
11197
  const objectives = Object.keys(sampleScores);
10369
11198
  if (options?.verbose) {
10370
- console.log(`Detected objectives: ${objectives.join(", ")}`);
11199
+ this.getLogger(options)?.(
11200
+ `Detected objectives: ${objectives.join(", ")}`,
11201
+ { tags: ["discovery"] }
11202
+ );
10371
11203
  }
10372
11204
  const weightCombinations = this.generateWeightCombinations(objectives);
10373
11205
  for (let i = 0; i < weightCombinations.length; i++) {
10374
11206
  const weights = weightCombinations[i];
10375
11207
  if (options?.verbose) {
10376
- console.log(`Optimizing with weights: ${JSON.stringify(weights)}`);
11208
+ this.getLogger(options)?.(
11209
+ `Optimizing with weights: ${JSON.stringify(weights)}`,
11210
+ { tags: ["discovery"] }
11211
+ );
10377
11212
  }
10378
11213
  const weightedMetric = async ({ prediction, example }) => {
10379
11214
  const scores = await metricFn({ prediction, example });
@@ -10405,9 +11240,9 @@ var AxBaseOptimizer = class {
10405
11240
  });
10406
11241
  } catch (error) {
10407
11242
  if (options?.verbose) {
10408
- console.warn(
10409
- `Failed optimization with weights ${JSON.stringify(weights)}:`,
10410
- error
11243
+ this.getLogger(options)?.(
11244
+ `Failed optimization with weights ${JSON.stringify(weights)}: ${error}`,
11245
+ { tags: ["warning"] }
10411
11246
  );
10412
11247
  }
10413
11248
  continue;
@@ -10432,8 +11267,9 @@ var AxBaseOptimizer = class {
10432
11267
  const objectives = Object.keys(sampleScores);
10433
11268
  for (const primaryObjective of objectives) {
10434
11269
  if (options?.verbose) {
10435
- console.log(
10436
- `Optimizing ${primaryObjective} with constraints on other objectives`
11270
+ this.getLogger(options)?.(
11271
+ `Optimizing ${primaryObjective} with constraints on other objectives`,
11272
+ { tags: ["discovery"] }
10437
11273
  );
10438
11274
  }
10439
11275
  const constraintMetric = async ({ prediction, example }) => {
@@ -10470,9 +11306,9 @@ var AxBaseOptimizer = class {
10470
11306
  });
10471
11307
  } catch (error) {
10472
11308
  if (options?.verbose) {
10473
- console.warn(
10474
- `Failed constraint optimization for ${primaryObjective}:`,
10475
- error
11309
+ this.getLogger(options)?.(
11310
+ `Failed constraint optimization for ${primaryObjective}: ${error}`,
11311
+ { tags: ["warning"] }
10476
11312
  );
10477
11313
  }
10478
11314
  continue;
@@ -10707,6 +11543,39 @@ var AxBaseOptimizer = class {
10707
11543
  );
10708
11544
  }
10709
11545
  }
11546
+ /**
11547
+ * Get the logger function with fallback hierarchy:
11548
+ * 1. Explicit logger passed to optimizer
11549
+ * 2. Logger from student AI service
11550
+ * 3. Default optimizer logger
11551
+ * 4. undefined if verbose is false
11552
+ */
11553
+ getLogger(options) {
11554
+ const isVerbose = this.isLoggingEnabled(options);
11555
+ if (!isVerbose) {
11556
+ return void 0;
11557
+ }
11558
+ if (this.logger) {
11559
+ return this.logger;
11560
+ }
11561
+ try {
11562
+ const aiLogger = this.studentAI.getLogger();
11563
+ if (aiLogger) {
11564
+ return aiLogger;
11565
+ }
11566
+ } catch {
11567
+ }
11568
+ return axDefaultOptimizerLogger;
11569
+ }
11570
+ /**
11571
+ * Check if logging is enabled based on verbose settings
11572
+ */
11573
+ isLoggingEnabled(options) {
11574
+ if (options?.verbose !== void 0) {
11575
+ return options.verbose;
11576
+ }
11577
+ return this.verbose ?? true;
11578
+ }
10710
11579
  };
10711
11580
 
10712
11581
  // db/base.ts
@@ -11425,23 +12294,6 @@ var getTopInPercent = (entries, percent = 0.1) => {
11425
12294
  return sortedEntries.slice(0, topTenPercentCount);
11426
12295
  };
11427
12296
 
11428
- // docs/rewriter.ts
11429
- var AxDefaultQueryRewriter = class extends AxGen {
11430
- constructor(options) {
11431
- const signature = `"You are a query rewriter assistant tasked with rewriting a given query to improve its clarity, specificity, and relevance. Your role involves analyzing the query to identify any ambiguities, generalizations, or irrelevant information and then rephrasing it to make it more focused and precise. The rewritten query should be concise, easy to understand, and directly related to the original query. Output only the rewritten query."
11432
- query: string -> rewrittenQuery: string`;
11433
- super(signature, options);
11434
- }
11435
- };
11436
- var AxRewriter = class extends AxGen {
11437
- constructor(options) {
11438
- super(
11439
- '"Rewrite a given text to be clear and concise" original -> rewritten "improved text"',
11440
- options
11441
- );
11442
- }
11443
- };
11444
-
11445
12297
  // funcs/docker.ts
11446
12298
  var AxDockerSession = class {
11447
12299
  apiUrl;
@@ -12289,9 +13141,9 @@ var AxBootstrapFewShot = class extends AxBaseOptimizer {
12289
13141
  this.stats.earlyStopped = true;
12290
13142
  this.stats.earlyStopping.reason = `No improvement for ${this.earlyStoppingPatience} rounds`;
12291
13143
  if (this.verboseMode || this.debugMode) {
12292
- console.log(
12293
- `
12294
- Early stopping triggered after ${roundIndex + 1} rounds. No improvement for ${this.earlyStoppingPatience} rounds.`
13144
+ this.getLogger()?.(
13145
+ `Early stopping after ${roundIndex + 1} rounds (no improvement for ${this.earlyStoppingPatience} rounds)`,
13146
+ { tags: ["optimizer", "warning"] }
12295
13147
  );
12296
13148
  }
12297
13149
  return;
@@ -12302,6 +13154,16 @@ Early stopping triggered after ${roundIndex + 1} rounds. No improvement for ${th
12302
13154
  const maxRounds = options?.maxIterations ?? this.maxRounds;
12303
13155
  this.traces = [];
12304
13156
  this.reset();
13157
+ if (this.verboseMode || this.debugMode) {
13158
+ this.getLogger()?.(
13159
+ `Starting BootstrapFewshot optimization with ${maxRounds} rounds`,
13160
+ { tags: ["optimizer", "start"] }
13161
+ );
13162
+ this.getLogger()?.(
13163
+ `Using ${this.examples.length} examples, max ${this.maxDemos} demos`,
13164
+ { tags: ["optimizer", "config"] }
13165
+ );
13166
+ }
12305
13167
  for (let i = 0; i < maxRounds; i++) {
12306
13168
  await this.compileRound(program, i, metricFn, options);
12307
13169
  if (this.stats.earlyStopped) {
@@ -12318,6 +13180,12 @@ Early stopping triggered after ${roundIndex + 1} rounds. No improvement for ${th
12318
13180
  if (this.traces.length > 0) {
12319
13181
  bestScore = this.stats.successfulDemos / Math.max(1, this.stats.totalCalls);
12320
13182
  }
13183
+ if (this.verboseMode || this.debugMode) {
13184
+ this.getLogger()?.(
13185
+ `Bootstrap complete. Generated ${demos.length} demos with ${bestScore.toFixed(3)} success rate`,
13186
+ { tags: ["optimizer", "complete"] }
13187
+ );
13188
+ }
12321
13189
  return {
12322
13190
  demos,
12323
13191
  stats: this.stats,
@@ -12383,7 +13251,6 @@ var AxMiPRO = class extends AxBaseOptimizer {
12383
13251
  viewDataBatchSize;
12384
13252
  tipAwareProposer;
12385
13253
  fewshotAwareProposer;
12386
- verbose;
12387
13254
  earlyStoppingTrials;
12388
13255
  minImprovementThreshold;
12389
13256
  bayesianOptimization;
@@ -12405,7 +13272,6 @@ var AxMiPRO = class extends AxBaseOptimizer {
12405
13272
  this.viewDataBatchSize = options.viewDataBatchSize ?? 10;
12406
13273
  this.tipAwareProposer = options.tipAwareProposer ?? true;
12407
13274
  this.fewshotAwareProposer = options.fewshotAwareProposer ?? true;
12408
- this.verbose = options.verbose ?? false;
12409
13275
  this.earlyStoppingTrials = options.earlyStoppingTrials ?? 5;
12410
13276
  this.minImprovementThreshold = options.minImprovementThreshold ?? 0.01;
12411
13277
  this.bayesianOptimization = options.bayesianOptimization ?? false;
@@ -12496,8 +13362,10 @@ var AxMiPRO = class extends AxBaseOptimizer {
12496
13362
  * Bootstraps few-shot examples for the program
12497
13363
  */
12498
13364
  async bootstrapFewShotExamples(program, metricFn) {
12499
- if (this.verbose) {
12500
- console.log("Bootstrapping few-shot examples...");
13365
+ if (this.isLoggingEnabled()) {
13366
+ this.getLogger()?.("Bootstrapping few-shot examples...", {
13367
+ tags: ["optimizer", "phase"]
13368
+ });
12501
13369
  }
12502
13370
  const bootstrapper = new AxBootstrapFewShot({
12503
13371
  studentAI: this.studentAI,
@@ -12505,7 +13373,7 @@ var AxMiPRO = class extends AxBaseOptimizer {
12505
13373
  options: {
12506
13374
  maxDemos: this.maxBootstrappedDemos,
12507
13375
  maxRounds: 3,
12508
- verboseMode: this.verbose
13376
+ verboseMode: this.isLoggingEnabled()
12509
13377
  }
12510
13378
  });
12511
13379
  const result = await bootstrapper.compile(program, metricFn, {
@@ -12550,9 +13418,10 @@ var AxMiPRO = class extends AxBaseOptimizer {
12550
13418
  options
12551
13419
  );
12552
13420
  if (checkpoint && checkpoint.optimizerType === "MiPRO") {
12553
- if (this.verbose || options?.verbose) {
12554
- console.log(
12555
- `Resuming from checkpoint at round ${checkpoint.currentRound}`
13421
+ if (this.isLoggingEnabled(options)) {
13422
+ this.getLogger(options)?.(
13423
+ `Resuming from checkpoint at round ${checkpoint.currentRound}`,
13424
+ { tags: ["optimizer", "checkpoint"] }
12556
13425
  );
12557
13426
  }
12558
13427
  this.restoreFromCheckpoint(checkpoint);
@@ -12562,6 +13431,12 @@ var AxMiPRO = class extends AxBaseOptimizer {
12562
13431
  stagnationRounds = checkpoint.stats.convergenceInfo?.stagnationRounds || 0;
12563
13432
  }
12564
13433
  }
13434
+ if (this.isLoggingEnabled(options)) {
13435
+ this.getLogger(options)?.(
13436
+ `Running optimization trials (${this.numTrials} total)`,
13437
+ { tags: ["optimizer", "phase"] }
13438
+ );
13439
+ }
12565
13440
  for (let i = startRound; i < this.numTrials; i++) {
12566
13441
  const config = {
12567
13442
  instruction: instructions[i % instructions.length] || instructions[0] || "",
@@ -12588,6 +13463,12 @@ var AxMiPRO = class extends AxBaseOptimizer {
12588
13463
  bestScore = score;
12589
13464
  bestConfig = config;
12590
13465
  stagnationRounds = 0;
13466
+ if (this.isLoggingEnabled(options)) {
13467
+ this.getLogger(options)?.(
13468
+ `Trial ${i + 1}/${this.numTrials}: New best score ${bestScore.toFixed(3)}`,
13469
+ { tags: ["optimizer", "progress"] }
13470
+ );
13471
+ }
12591
13472
  } else {
12592
13473
  stagnationRounds++;
12593
13474
  }
@@ -12706,38 +13587,53 @@ var AxMiPRO = class extends AxBaseOptimizer {
12706
13587
  this.configureAuto(miproOptions.auto);
12707
13588
  }
12708
13589
  const valset = this.getValidationSet(options) || (miproOptions?.valset ?? this.examples.slice(0, Math.floor(this.examples.length * 0.2)));
12709
- if (this.verbose || options?.verbose) {
12710
- console.log(`Starting MIPROv2 optimization with ${this.numTrials} trials`);
12711
- console.log(
12712
- `Using ${this.examples.length} examples for training and ${valset.length} for validation`
13590
+ if (this.isLoggingEnabled(options)) {
13591
+ this.getLogger(options)?.(
13592
+ `Starting MIPROv2 optimization with ${this.numTrials} trials`,
13593
+ { tags: ["optimizer", "start"] }
13594
+ );
13595
+ this.getLogger(options)?.(
13596
+ `Using ${this.examples.length} examples for training and ${valset.length} for validation`,
13597
+ { tags: ["optimizer", "config"] }
12713
13598
  );
12714
13599
  if (this.teacherAI) {
12715
- console.log("Using separate teacher model for instruction generation");
13600
+ this.getLogger(options)?.(
13601
+ "Using separate teacher model for instruction generation",
13602
+ { tags: ["optimizer", "config"] }
13603
+ );
12716
13604
  }
12717
13605
  }
12718
13606
  let bootstrappedDemos = [];
12719
13607
  if (this.maxBootstrappedDemos > 0) {
12720
13608
  bootstrappedDemos = await this.bootstrapFewShotExamples(program, metricFn);
12721
- if (this.verbose) {
12722
- console.log(
12723
- `Generated ${bootstrappedDemos.length} bootstrapped demonstrations`
13609
+ if (this.isLoggingEnabled(options)) {
13610
+ this.getLogger(options)?.(
13611
+ `Generated ${bootstrappedDemos.length} bootstrapped demonstrations`,
13612
+ { tags: ["optimizer", "result"] }
12724
13613
  );
12725
13614
  }
12726
13615
  }
12727
13616
  let labeledExamples = [];
12728
13617
  if (this.maxLabeledDemos > 0) {
12729
13618
  labeledExamples = this.selectLabeledExamples();
12730
- if (this.verbose) {
12731
- console.log(
12732
- `Selected ${labeledExamples.length} labeled examples from training set`
13619
+ if (this.isLoggingEnabled(options)) {
13620
+ this.getLogger(options)?.(
13621
+ `Selected ${labeledExamples.length} labeled examples from training set`,
13622
+ { tags: ["optimizer", "result"] }
12733
13623
  );
12734
13624
  }
12735
13625
  }
12736
13626
  const instructions = await this.proposeInstructionCandidates(options);
12737
- if (this.verbose) {
12738
- console.log(`Generated ${instructions.length} instruction candidates`);
13627
+ if (this.isLoggingEnabled(options)) {
13628
+ this.getLogger(options)?.(
13629
+ `Generated ${instructions.length} instruction candidates`,
13630
+ { tags: ["optimizer", "result"] }
13631
+ );
12739
13632
  if (this.hasTeacherAI(options)) {
12740
- console.log("Using teacher AI for instruction generation");
13633
+ this.getLogger(options)?.(
13634
+ "Using teacher AI for instruction generation",
13635
+ { tags: ["optimizer", "config"] }
13636
+ );
12741
13637
  }
12742
13638
  }
12743
13639
  const { bestConfig, bestScore } = await this.runOptimization(
@@ -12749,9 +13645,15 @@ var AxMiPRO = class extends AxBaseOptimizer {
12749
13645
  metricFn,
12750
13646
  options
12751
13647
  );
12752
- if (this.verbose || options?.verbose) {
12753
- console.log(`Optimization complete. Best score: ${bestScore}`);
12754
- console.log(`Best configuration: ${JSON.stringify(bestConfig)}`);
13648
+ if (this.isLoggingEnabled(options)) {
13649
+ this.getLogger(options)?.(
13650
+ `Optimization complete. Best score: ${bestScore}`,
13651
+ { tags: ["optimizer", "complete"] }
13652
+ );
13653
+ this.getLogger(options)?.(
13654
+ `Best configuration: ${JSON.stringify(bestConfig)}`,
13655
+ { tags: ["optimizer", "result"] }
13656
+ );
12755
13657
  }
12756
13658
  if (this.checkTargetScore(bestScore)) {
12757
13659
  this.triggerEarlyStopping(
@@ -12878,9 +13780,6 @@ var AxMiPRO = class extends AxBaseOptimizer {
12878
13780
  if (config.minImprovementThreshold !== void 0) {
12879
13781
  this.minImprovementThreshold = config.minImprovementThreshold;
12880
13782
  }
12881
- if (config.verbose !== void 0) {
12882
- this.verbose = config.verbose;
12883
- }
12884
13783
  }
12885
13784
  /**
12886
13785
  * Reset optimizer state for reuse with different programs
@@ -12985,6 +13884,7 @@ var AxMockAIService = class {
12985
13884
  return this.config.chatResponse ?? {
12986
13885
  results: [
12987
13886
  {
13887
+ index: 0,
12988
13888
  content: "Mock response",
12989
13889
  finishReason: "stop"
12990
13890
  }
@@ -15285,7 +16185,6 @@ export {
15285
16185
  AxDBPinecone,
15286
16186
  AxDBWeaviate,
15287
16187
  AxDefaultCostTracker,
15288
- AxDefaultQueryRewriter,
15289
16188
  AxDefaultResultReranker,
15290
16189
  AxDockerSession,
15291
16190
  AxEmbeddingAdapter,
@@ -15312,7 +16211,6 @@ export {
15312
16211
  AxPromptTemplate,
15313
16212
  AxRAG,
15314
16213
  AxRateLimiterTokenUsage,
15315
- AxRewriter,
15316
16214
  AxSignature,
15317
16215
  AxSimpleClassifier,
15318
16216
  AxSimpleClassifierClass,
@@ -15354,6 +16252,10 @@ export {
15354
16252
  axAITogetherDefaultConfig,
15355
16253
  axBaseAIDefaultConfig,
15356
16254
  axBaseAIDefaultCreativeConfig,
16255
+ axCreateDefaultLogger,
16256
+ axCreateDefaultTextLogger,
16257
+ axCreateOptimizerLogger,
16258
+ axDefaultOptimizerLogger,
15357
16259
  axGlobals,
15358
16260
  axModelInfoAnthropic,
15359
16261
  axModelInfoCohere,
@@ -15368,6 +16270,8 @@ export {
15368
16270
  axModelInfoTogether,
15369
16271
  axSpanAttributes,
15370
16272
  axSpanEvents,
16273
+ axValidateChatRequestMessage,
16274
+ axValidateChatResponseResult,
15371
16275
  f,
15372
16276
  s
15373
16277
  };