@ax-llm/ax 10.0.39 → 10.0.41

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
@@ -525,9 +525,11 @@ var AxBaseAI = class {
525
525
  }
526
526
  },
527
527
  async (span) => {
528
- const res = await this._chat2(model, modelConfig, req, options, span);
529
- span.end();
530
- return res;
528
+ try {
529
+ return await this._chat2(model, modelConfig, req, options, span);
530
+ } finally {
531
+ span.end();
532
+ }
531
533
  }
532
534
  );
533
535
  }
@@ -650,9 +652,11 @@ var AxBaseAI = class {
650
652
  }
651
653
  },
652
654
  async (span) => {
653
- const res = await this._embed2(embedModel, req, options, span);
654
- span.end();
655
- return res;
655
+ try {
656
+ return await this._embed2(embedModel, req, options, span);
657
+ } finally {
658
+ span.end();
659
+ }
656
660
  }
657
661
  );
658
662
  }
@@ -720,7 +724,7 @@ ${colorLog.whiteBright(msg.content)}`;
720
724
  const items2 = msg.content.map((v) => {
721
725
  switch (v.type) {
722
726
  case "text":
723
- return `(Text) ${colorLog.whiteBright(v.text)}`;
727
+ return `${colorLog.whiteBright(v.text)}`;
724
728
  case "image":
725
729
  return `(Image, ${v.mimeType}) ${colorLog.whiteBright(v.image.substring(0, 10))}`;
726
730
  default:
@@ -785,6 +789,61 @@ var setResponseAttr = (res, span) => {
785
789
  }
786
790
  };
787
791
 
792
+ // ai/google-vertex/auth.ts
793
+ import { GoogleAuth } from "google-auth-library";
794
+ var GoogleVertexAuth = class {
795
+ auth;
796
+ client;
797
+ currentToken;
798
+ tokenExpiry;
799
+ constructor(config = {}) {
800
+ this.auth = new GoogleAuth({
801
+ scopes: ["https://www.googleapis.com/auth/cloud-platform"],
802
+ ...config
803
+ });
804
+ }
805
+ async getAuthenticatedClient() {
806
+ if (!this.client) {
807
+ this.client = await this.auth.getClient();
808
+ }
809
+ return this.client;
810
+ }
811
+ async getAccessToken() {
812
+ if (this.currentToken && this.tokenExpiry && Date.now() < this.tokenExpiry) {
813
+ return this.currentToken;
814
+ }
815
+ const client = await this.getAuthenticatedClient();
816
+ const tokenResponse = await client.getAccessToken();
817
+ this.currentToken = tokenResponse.token ?? void 0;
818
+ const expiry = this.getExpiry(tokenResponse);
819
+ const fiveMinutes = 5 * 60 * 1e3;
820
+ this.tokenExpiry = expiry - fiveMinutes;
821
+ return this.currentToken;
822
+ }
823
+ /**
824
+ * Get the expiry date from the token response.
825
+ */
826
+ getExpiry(tokenResponse) {
827
+ const oneHour = 3600 * 1e3;
828
+ let expiry = Date.now() + oneHour;
829
+ let responseExpiry = tokenResponse.res?.data?.expiry_date;
830
+ if (responseExpiry) {
831
+ if (typeof responseExpiry === "number") {
832
+ expiry = responseExpiry;
833
+ } else if (responseExpiry instanceof Date) {
834
+ expiry = responseExpiry.getTime();
835
+ } else if (typeof responseExpiry === "string") {
836
+ expiry = new Date(responseExpiry).getTime();
837
+ } else {
838
+ console.warn("Unknown expiry type", responseExpiry);
839
+ }
840
+ } else {
841
+ console.warn("No expiry date found in response", tokenResponse.res?.data);
842
+ }
843
+ return expiry;
844
+ }
845
+ };
846
+
788
847
  // ai/anthropic/types.ts
789
848
  var AxAIAnthropicModel = /* @__PURE__ */ ((AxAIAnthropicModel2) => {
790
849
  AxAIAnthropicModel2["Claude35Sonnet"] = "claude-3-5-sonnet-latest";
@@ -796,6 +855,14 @@ var AxAIAnthropicModel = /* @__PURE__ */ ((AxAIAnthropicModel2) => {
796
855
  AxAIAnthropicModel2["ClaudeInstant12"] = "claude-instant-1.2";
797
856
  return AxAIAnthropicModel2;
798
857
  })(AxAIAnthropicModel || {});
858
+ var AxAIAnthropicVertexModel = /* @__PURE__ */ ((AxAIAnthropicVertexModel2) => {
859
+ AxAIAnthropicVertexModel2["Claude35Haiku"] = "claude-3-5-haiku";
860
+ AxAIAnthropicVertexModel2["Claude35Sonnet"] = "claude-3-5-sonnet";
861
+ AxAIAnthropicVertexModel2["Claude35SonnetV2"] = "claude-3-5-sonnet-v2";
862
+ AxAIAnthropicVertexModel2["Claude3Haiku"] = "claude-3-haiku";
863
+ AxAIAnthropicVertexModel2["Claude3Opus"] = "claude-3-opus";
864
+ return AxAIAnthropicVertexModel2;
865
+ })(AxAIAnthropicVertexModel || {});
799
866
 
800
867
  // ai/anthropic/info.ts
801
868
  var axModelInfoAnthropic = [
@@ -852,8 +919,9 @@ var axAIAnthropicDefaultConfig = () => structuredClone({
852
919
  ...axBaseAIDefaultConfig()
853
920
  });
854
921
  var AxAIAnthropicImpl = class {
855
- constructor(config) {
922
+ constructor(config, isVertex) {
856
923
  this.config = config;
924
+ this.isVertex = isVertex;
857
925
  }
858
926
  getModelConfig() {
859
927
  const { config } = this;
@@ -872,9 +940,41 @@ var AxAIAnthropicImpl = class {
872
940
  }
873
941
  createChatReq = (req) => {
874
942
  const model = req.model;
875
- const apiConfig = {
876
- name: "/messages"
877
- };
943
+ const stream = req.modelConfig?.stream ?? this.config.stream;
944
+ let apiConfig;
945
+ if (this.isVertex) {
946
+ apiConfig = {
947
+ name: stream ? `/models/${model}:streamRawPredict?alt=sse` : `/models/${model}:rawPredict`
948
+ };
949
+ } else {
950
+ apiConfig = {
951
+ name: "/messages"
952
+ };
953
+ }
954
+ let toolsChoice;
955
+ if (req.functionCall && req.functions && req.functions.length > 0) {
956
+ if (typeof req.functionCall === "string") {
957
+ switch (req.functionCall) {
958
+ case "auto":
959
+ toolsChoice = { tool_choice: { type: "auto" } };
960
+ break;
961
+ case "required":
962
+ toolsChoice = { tool_choice: { type: "any" } };
963
+ break;
964
+ case "none":
965
+ throw new Error("functionCall none not supported");
966
+ }
967
+ } else if ("function" in req.functionCall) {
968
+ toolsChoice = {
969
+ tool_choice: {
970
+ type: "tool",
971
+ name: req.functionCall.function.name
972
+ }
973
+ };
974
+ } else {
975
+ throw new Error("Invalid function call type, must be string or object");
976
+ }
977
+ }
878
978
  const system = req.chatPrompt.filter((msg) => msg.role === "system").map((msg) => ({
879
979
  type: "text",
880
980
  text: msg.content,
@@ -889,15 +989,15 @@ var AxAIAnthropicImpl = class {
889
989
  input_schema: v.parameters
890
990
  })
891
991
  );
892
- const stream = req.modelConfig?.stream ?? this.config.stream;
893
992
  const reqValue = {
894
- model,
993
+ ...this.isVertex ? { anthropic_version: "vertex-2023-10-16" } : { model },
895
994
  max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
896
995
  stop_sequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
897
996
  temperature: req.modelConfig?.temperature ?? this.config.temperature,
898
997
  top_p: req.modelConfig?.topP ?? this.config.topP,
899
998
  top_k: req.modelConfig?.topK ?? this.config.topK,
900
- ...tools && tools.length > 0 ? { tools, tool_choice: { type: "auto" } } : {},
999
+ ...toolsChoice,
1000
+ ...tools && tools.length > 0 ? { tools } : {},
901
1001
  ...stream ? { stream: true } : {},
902
1002
  ...system ? { system } : {},
903
1003
  messages
@@ -1045,26 +1145,45 @@ var AxAIAnthropicImpl = class {
1045
1145
  var AxAIAnthropic = class extends AxBaseAI {
1046
1146
  constructor({
1047
1147
  apiKey,
1148
+ projectId,
1149
+ region,
1048
1150
  config,
1049
1151
  options,
1050
1152
  modelMap
1051
1153
  }) {
1052
- if (!apiKey || apiKey === "") {
1053
- throw new Error("Anthropic API key not set");
1154
+ const isVertex = projectId !== void 0 && region !== void 0;
1155
+ let apiURL;
1156
+ let headers;
1157
+ if (isVertex) {
1158
+ apiURL = `https://${region}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${region}/publishers/anthropic/`;
1159
+ if (apiKey) {
1160
+ headers = async () => ({ Authorization: `Bearer ${apiKey}` });
1161
+ } else {
1162
+ const vertexAuth = new GoogleVertexAuth();
1163
+ headers = async () => ({
1164
+ Authorization: `Bearer ${await vertexAuth.getAccessToken()}`
1165
+ });
1166
+ }
1167
+ } else {
1168
+ if (!apiKey) {
1169
+ throw new Error("Anthropic API key not set");
1170
+ }
1171
+ apiURL = "https://api.anthropic.com/v1";
1172
+ headers = async () => ({
1173
+ "anthropic-version": "2023-06-01",
1174
+ "anthropic-beta": "prompt-caching-2024-07-31",
1175
+ "x-api-key": apiKey
1176
+ });
1054
1177
  }
1055
1178
  const _config = {
1056
1179
  ...axAIAnthropicDefaultConfig(),
1057
1180
  ...config
1058
1181
  };
1059
- const aiImpl = new AxAIAnthropicImpl(_config);
1182
+ const aiImpl = new AxAIAnthropicImpl(_config, isVertex);
1060
1183
  super(aiImpl, {
1061
1184
  name: "Anthropic",
1062
- apiURL: "https://api.anthropic.com/v1",
1063
- headers: async () => ({
1064
- "anthropic-version": "2023-06-01",
1065
- "anthropic-beta": "prompt-caching-2024-07-31",
1066
- "x-api-key": apiKey
1067
- }),
1185
+ apiURL,
1186
+ headers,
1068
1187
  modelInfo: axModelInfoAnthropic,
1069
1188
  models: { model: _config.model },
1070
1189
  options,
@@ -2001,61 +2120,6 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
2001
2120
  }
2002
2121
  };
2003
2122
 
2004
- // ai/google-gemini/auth.ts
2005
- import { GoogleAuth } from "google-auth-library";
2006
- var GoogleVertexAuth = class {
2007
- auth;
2008
- client;
2009
- currentToken;
2010
- tokenExpiry;
2011
- constructor(config = {}) {
2012
- this.auth = new GoogleAuth({
2013
- scopes: ["https://www.googleapis.com/auth/cloud-platform"],
2014
- ...config
2015
- });
2016
- }
2017
- async getAuthenticatedClient() {
2018
- if (!this.client) {
2019
- this.client = await this.auth.getClient();
2020
- }
2021
- return this.client;
2022
- }
2023
- async getAccessToken() {
2024
- if (this.currentToken && this.tokenExpiry && Date.now() < this.tokenExpiry) {
2025
- return this.currentToken;
2026
- }
2027
- const client = await this.getAuthenticatedClient();
2028
- const tokenResponse = await client.getAccessToken();
2029
- this.currentToken = tokenResponse.token ?? void 0;
2030
- const expiry = this.getExpiry(tokenResponse);
2031
- const fiveMinutes = 5 * 60 * 1e3;
2032
- this.tokenExpiry = expiry - fiveMinutes;
2033
- return this.currentToken;
2034
- }
2035
- /**
2036
- * Get the expiry date from the token response.
2037
- */
2038
- getExpiry(tokenResponse) {
2039
- const oneHour = 3600 * 1e3;
2040
- let expiry = Date.now() + oneHour;
2041
- let responseExpiry = tokenResponse.res?.data?.expiry_date;
2042
- if (responseExpiry) {
2043
- if (typeof responseExpiry === "number") {
2044
- expiry = responseExpiry;
2045
- } else if (responseExpiry instanceof Date) {
2046
- expiry = responseExpiry.getTime();
2047
- } else if (typeof responseExpiry === "string") {
2048
- expiry = new Date(responseExpiry).getTime();
2049
- } else {
2050
- console.warn("Unknown expiry type", responseExpiry);
2051
- }
2052
- } else {
2053
- console.warn("No expiry date found in response", tokenResponse.res?.data);
2054
- }
2055
- return expiry;
2056
- }
2057
- };
2058
-
2059
2123
  // ai/google-gemini/types.ts
2060
2124
  var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2061
2125
  AxAIGoogleGeminiModel2["Gemini1Pro"] = "gemini-1.0-pro";
@@ -2145,11 +2209,10 @@ var axAIGoogleGeminiDefaultConfig = () => structuredClone({
2145
2209
  ...axBaseAIDefaultConfig()
2146
2210
  });
2147
2211
  var AxAIGoogleGeminiImpl = class {
2148
- constructor(config, isVertex, apiKey, keyFile, options) {
2212
+ constructor(config, isVertex, apiKey, options) {
2149
2213
  this.config = config;
2150
2214
  this.isVertex = isVertex;
2151
2215
  this.apiKey = apiKey;
2152
- this.keyFile = keyFile;
2153
2216
  this.options = options;
2154
2217
  }
2155
2218
  getModelConfig() {
@@ -2426,7 +2489,6 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2426
2489
  apiKey,
2427
2490
  projectId,
2428
2491
  region,
2429
- keyFile,
2430
2492
  config,
2431
2493
  options,
2432
2494
  modelMap
@@ -2439,9 +2501,7 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2439
2501
  if (apiKey) {
2440
2502
  headers = async () => ({ Authorization: `Bearer ${apiKey}` });
2441
2503
  } else {
2442
- const vertexAuth = new GoogleVertexAuth({
2443
- keyFile
2444
- });
2504
+ const vertexAuth = new GoogleVertexAuth();
2445
2505
  headers = async () => ({
2446
2506
  Authorization: `Bearer ${await vertexAuth.getAccessToken()}`
2447
2507
  });
@@ -2457,13 +2517,7 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2457
2517
  ...axAIGoogleGeminiDefaultConfig(),
2458
2518
  ...config
2459
2519
  };
2460
- const aiImpl = new AxAIGoogleGeminiImpl(
2461
- _config,
2462
- isVertex,
2463
- apiKey,
2464
- keyFile,
2465
- options
2466
- );
2520
+ const aiImpl = new AxAIGoogleGeminiImpl(_config, isVertex, apiKey, options);
2467
2521
  super(aiImpl, {
2468
2522
  name: "GoogleGeminiAI",
2469
2523
  apiURL,
@@ -3206,9 +3260,6 @@ var AxAI = class {
3206
3260
  }
3207
3261
  };
3208
3262
 
3209
- // prompts/agent.ts
3210
- import { SpanKind as SpanKind3 } from "@opentelemetry/api";
3211
-
3212
3263
  // dsp/generate.ts
3213
3264
  import { ReadableStream } from "stream/web";
3214
3265
  import { SpanKind as SpanKind2 } from "@opentelemetry/api";
@@ -3397,7 +3448,7 @@ var AxAssertionError = class extends Error {
3397
3448
  extraFields.push({
3398
3449
  name: "error",
3399
3450
  title: "Error In Output",
3400
- description: this.message
3451
+ description: `You must follow the following instructions, "${this.message}".`
3401
3452
  });
3402
3453
  return extraFields;
3403
3454
  };
@@ -3436,18 +3487,6 @@ var assertStreamingAssertions = (asserts, values, xstate, content, final) => {
3436
3487
  }
3437
3488
  }
3438
3489
  };
3439
- var assertRequiredFields = (sig, values) => {
3440
- const fields = sig.getOutputFields();
3441
- const missingFields = fields.filter(
3442
- (f) => !f.isOptional && !(f.name in values)
3443
- );
3444
- if (missingFields.length > 0) {
3445
- throw new AxAssertionError({
3446
- message: `Output must include: ${missingFields.map((f) => `\`${f.title}:\``).join(", ")}`,
3447
- values
3448
- });
3449
- }
3450
- };
3451
3490
 
3452
3491
  // dsp/extract.ts
3453
3492
  import JSON5 from "json5";
@@ -3995,6 +4034,67 @@ var parseMarkdownList = (input) => {
3995
4034
  }
3996
4035
  return list;
3997
4036
  };
4037
+ function mergeDeltas(base, delta) {
4038
+ for (const key of Object.keys(delta)) {
4039
+ const baseValue = base[key];
4040
+ const deltaValue = delta[key];
4041
+ if ((baseValue === void 0 || Array.isArray(baseValue)) && Array.isArray(deltaValue)) {
4042
+ base[key] = [...baseValue ?? [], ...deltaValue];
4043
+ } else if ((baseValue === void 0 || typeof baseValue === "string") && typeof deltaValue === "string") {
4044
+ base[key] = (baseValue ?? "") + deltaValue;
4045
+ } else {
4046
+ base[key] = deltaValue;
4047
+ }
4048
+ }
4049
+ return base;
4050
+ }
4051
+ var LRUCache = class {
4052
+ cache = /* @__PURE__ */ new Map();
4053
+ maxSize;
4054
+ constructor(maxSize) {
4055
+ this.maxSize = maxSize;
4056
+ }
4057
+ get(key) {
4058
+ const value = this.cache.get(key);
4059
+ if (value) {
4060
+ this.cache.delete(key);
4061
+ this.cache.set(key, value);
4062
+ }
4063
+ return value;
4064
+ }
4065
+ set(key, value) {
4066
+ if (this.cache.has(key)) {
4067
+ this.cache.delete(key);
4068
+ } else if (this.cache.size >= this.maxSize) {
4069
+ const firstKey = this.cache.keys().next().value;
4070
+ if (firstKey) {
4071
+ this.cache.delete(firstKey);
4072
+ }
4073
+ }
4074
+ this.cache.set(key, value);
4075
+ }
4076
+ };
4077
+ var globalPrefixCache = new LRUCache(500);
4078
+ function matchesContent(content, prefix, startIndex = 0, prefixCache = globalPrefixCache) {
4079
+ const exactMatchIndex = content.indexOf(prefix, startIndex);
4080
+ if (exactMatchIndex !== -1) {
4081
+ return exactMatchIndex;
4082
+ }
4083
+ const prefixes = prefixCache.get(prefix) ?? Array.from({ length: prefix.length }, (_, i) => prefix.slice(0, i + 1));
4084
+ if (!prefixCache.get(prefix)) {
4085
+ prefixCache.set(prefix, prefixes);
4086
+ }
4087
+ const contentEnd = content.slice(
4088
+ Math.max(startIndex, content.length - prefix.length)
4089
+ );
4090
+ for (let i = 0; i < prefixes.length - 1; i++) {
4091
+ const partialPrefix = prefixes[i];
4092
+ if (contentEnd.endsWith(partialPrefix)) {
4093
+ return -2;
4094
+ }
4095
+ }
4096
+ return -1;
4097
+ }
3998
4098
 
3999
4099
  // dsp/program.ts
4000
4100
  var AxProgramWithSignature = class {
@@ -4027,6 +4127,9 @@ var AxProgramWithSignature = class {
4027
4127
  async forward(_ai, _values, _options) {
4028
4128
  throw new Error("forward() not implemented");
4029
4129
  }
4130
+ async *streamingForward(_ai, _values, _options) {
4131
+ throw new Error("streamingForward() not implemented");
4132
+ }
4030
4133
  setId(id) {
4031
4134
  this.key = { id, custom: true };
4032
4135
  for (const child of this.children) {
@@ -4121,6 +4224,9 @@ var AxProgram = class {
4121
4224
  async forward(_ai, _values, _options) {
4122
4225
  throw new Error("forward() not implemented");
4123
4226
  }
4227
+ async *streamingForward(_ai, _values, _options) {
4228
+ throw new Error("streamingForward() not implemented");
4229
+ }
4124
4230
  setId(id) {
4125
4231
  this.key = { id, custom: true };
4126
4232
  for (const child of this.children) {
@@ -4218,9 +4324,10 @@ ${outputFields}`);
4218
4324
  task.push(formattingRules.trim());
4219
4325
  const desc = this.sig.getDescription();
4220
4326
  if (desc) {
4327
+ const capitalized = capitalizeFirstLetter(desc.trim());
4221
4328
  task.push(
4222
4329
  `## TASK DESCRIPTION
4223
- ${capitalizeFirstLetter(desc.endsWith(".") ? desc : desc + ".")}`
4330
+ ${capitalized.endsWith(".") ? capitalized : capitalized + "."}`
4224
4331
  );
4225
4332
  }
4226
4333
  this.task = {
@@ -4268,16 +4375,42 @@ ${capitalizeFirstLetter(desc.endsWith(".") ? desc : desc + ".")}`
4268
4375
  };
4269
4376
  renderExtraFields = (extraFields) => {
4270
4377
  const prompt = [];
4271
- if (extraFields && extraFields.length > 0) {
4272
- extraFields.forEach((field) => {
4273
- const fn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
4274
- prompt.push(...fn(field, field.description));
4275
- });
4276
- }
4277
- if (prompt.every((v) => v.type === "text")) {
4278
- return prompt.map((v) => v.text).join("\n\n");
4279
- }
4280
- return prompt.reduce(combineConsecutiveStrings("\n"), []);
4378
+ if (!extraFields || extraFields.length === 0) {
4379
+ return prompt;
4380
+ }
4381
+ const groupedFields = extraFields.reduce(
4382
+ (acc, field) => {
4383
+ const title = field.title;
4384
+ if (!acc[title]) {
4385
+ acc[title] = [];
4386
+ }
4387
+ acc[title].push(field);
4388
+ return acc;
4389
+ },
4390
+ {}
4391
+ );
4392
+ const formattedGroupedFields = Object.entries(groupedFields).map(([title, fields]) => {
4393
+ if (fields.length === 1) {
4394
+ const field = fields[0];
4395
+ return {
4396
+ title,
4397
+ name: field.name,
4398
+ description: field.description
4399
+ };
4400
+ } else if (fields.length > 1) {
4401
+ const valuesList = fields.map((field) => `- ${field.description}`).join("\n");
4402
+ return {
4403
+ title,
4404
+ name: fields[0].name,
4405
+ description: valuesList
4406
+ };
4407
+ }
4408
+ }).filter(Boolean);
4409
+ formattedGroupedFields.forEach((field) => {
4410
+ const fn = this.fieldTemplates?.[field.name] ?? this.defaultRenderInField;
4411
+ prompt.push(...fn(field, field.description));
4412
+ });
4413
+ return prompt;
4281
4414
  };
4282
4415
  renderExamples = (data) => {
4283
4416
  const list = [];
@@ -4526,34 +4659,44 @@ function capitalizeFirstLetter(str) {
4526
4659
  }
4527
4660
 
4528
4661
  // dsp/validate.ts
4662
+ var colorLog4 = new ColorLog();
4529
4663
  var ValidationError = class extends Error {
4530
- field;
4531
- value;
4664
+ fields;
4532
4665
  constructor({
4533
4666
  message,
4534
- field,
4535
- value
4667
+ fields
4536
4668
  }) {
4537
4669
  super(message);
4538
- this.field = field;
4539
- this.value = value;
4670
+ this.fields = fields;
4540
4671
  this.name = this.constructor.name;
4541
4672
  Error.captureStackTrace(this, this.constructor);
4542
4673
  }
4543
- getField = () => this.field;
4544
- getValue = () => this.value;
4674
+ getFields = () => this.fields;
4545
4675
  getFixingInstructions = () => {
4546
- const f = this.field;
4547
- const extraFields = [
4548
- {
4549
- name: `outputError`,
4550
- title: `Invalid Output Field`,
4551
- description: `Invalid format for field \`${f.title}\` of type \`${toFieldType(f.type)}\`, format should match: \`${f.description}\``
4552
- }
4553
- ];
4554
- return extraFields;
4676
+ return this.fields.map((field) => ({
4677
+ name: "outputError",
4678
+ title: "Error In Output",
4679
+ description: `Please fix and return the field \`${field.title}\` of type \`${toFieldType(field.type)}\`, ${this.message}.`
4680
+ }));
4555
4681
  };
4556
4682
  };
4683
+ function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId) {
4684
+ mem.add(
4685
+ {
4686
+ role: "user",
4687
+ content: promptTemplate.renderExtraFields(errorFields)
4688
+ },
4689
+ sessionId
4690
+ );
4691
+ mem.addTag("error");
4692
+ if (ai.getOptions().debug) {
4693
+ const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
4694
+ process.stdout.write(colorLog4.red(`
4695
+ Error Correction:
4696
+ ${errors}
4697
+ `));
4698
+ }
4699
+ }
4557
4700
 
4558
4701
  // dsp/datetime.ts
4559
4702
  function parseLLMFriendlyDate(field, dateStr) {
@@ -4561,7 +4704,7 @@ function parseLLMFriendlyDate(field, dateStr) {
4561
4704
  return _parseLLMFriendlyDate(dateStr);
4562
4705
  } catch (err) {
4563
4706
  const message = err.message;
4564
- throw new ValidationError({ field, message, value: dateStr });
4707
+ throw new ValidationError({ fields: [field], message, value: dateStr });
4565
4708
  }
4566
4709
  }
4567
4710
  function _parseLLMFriendlyDate(dateStr) {
@@ -4578,7 +4721,7 @@ function parseLLMFriendlyDateTime(field, dateStr) {
4578
4721
  return _parseLLMFriendlyDateTime(dateStr);
4579
4722
  } catch (err) {
4580
4723
  const message = err.message;
4581
- throw new ValidationError({ field, message, value: dateStr });
4724
+ throw new ValidationError({ fields: [field], message, value: dateStr });
4582
4725
  }
4583
4726
  }
4584
4727
  function _parseLLMFriendlyDateTime(dateTimeStr) {
@@ -4620,41 +4763,73 @@ var formatDateWithTimezone = (date) => {
4620
4763
 
4621
4764
  // dsp/extract.ts
4622
4765
  var extractValues = (sig, values, content) => {
4623
- const xstate = { s: -1 };
4766
+ const xstate = { extractedFields: [], s: -1 };
4624
4767
  streamingExtractValues(sig, values, xstate, content);
4625
- streamingExtractFinalValue(values, xstate, content);
4768
+ streamingExtractFinalValue(sig, values, xstate, content);
4626
4769
  };
4627
- var streamingExtractValues = (sig, values, state, content) => {
4770
+ var checkMissingRequiredFields = (xstate, values, currentIndex) => {
4771
+ const missingFields = [];
4772
+ for (let i = 0; i < currentIndex; i++) {
4773
+ const field = xstate.extractedFields[i];
4774
+ if (field && !field.isOptional && values[field.name] === void 0) {
4775
+ missingFields.push(field);
4776
+ }
4777
+ }
4778
+ if (missingFields.length > 0) {
4779
+ throw new ValidationError({
4780
+ message: `Required ${missingFields.length === 1 ? "field" : "fields"} not found`,
4781
+ fields: missingFields
4782
+ });
4783
+ }
4784
+ };
4785
+ var streamingExtractValues = (sig, values, xstate, content) => {
4786
+ if (content.endsWith("\n")) {
4787
+ return true;
4788
+ }
4628
4789
  const fields = sig.getOutputFields();
4629
- for (const field of fields) {
4790
+ for (const [index, field] of fields.entries()) {
4630
4791
  if (field.name in values) {
4631
4792
  continue;
4632
4793
  }
4633
4794
  const prefix = field.title + ":";
4634
- const e = content.indexOf(prefix, state.s + 1);
4635
- if (e === -1) {
4636
- continue;
4795
+ let e = matchesContent(content, prefix, xstate.s + 1);
4796
+ switch (e) {
4797
+ case -1:
4798
+ continue;
4799
+ // Field is not found, continue to the next field
4800
+ case -2:
4801
+ return true;
4637
4802
  }
4638
- if (state.currField) {
4639
- const val = content.substring(state.s, e).trim().replace(/---+$/, "").trim();
4640
- values[state.currField.name] = validateAndParseFieldValue(
4641
- state.currField,
4642
- val
4643
- );
4803
+ let prefixLen = prefix.length;
4804
+ if (e - 1 >= 0 && content[e - 1] === "\n") {
4805
+ e -= 1;
4806
+ prefixLen += 1;
4807
+ }
4808
+ if (xstate.currField) {
4809
+ const val = content.substring(xstate.s, e).trim();
4810
+ const parsedValue = validateAndParseFieldValue(xstate.currField, val);
4811
+ if (parsedValue !== void 0) {
4812
+ values[xstate.currField.name] = parsedValue;
4813
+ }
4814
+ }
4815
+ checkMissingRequiredFields(xstate, values, index);
4816
+ xstate.s = e + prefixLen;
4817
+ xstate.currField = field;
4818
+ if (!xstate.extractedFields.includes(field)) {
4819
+ xstate.extractedFields.push(field);
4644
4820
  }
4645
- state.s = e + prefix.length;
4646
- state.currField = field;
4647
4821
  }
4648
4822
  };
4649
- var streamingExtractFinalValue = (values, state, content) => {
4650
- if (!state.currField) {
4651
- return;
4823
+ var streamingExtractFinalValue = (sig, values, xstate, content) => {
4824
+ if (xstate.currField) {
4825
+ const val = content.substring(xstate.s).trim();
4826
+ const parsedValue = validateAndParseFieldValue(xstate.currField, val);
4827
+ if (parsedValue !== void 0) {
4828
+ values[xstate.currField.name] = parsedValue;
4829
+ }
4652
4830
  }
4653
- const val = content.substring(state.s).trim().replace(/---+$/, "").trim();
4654
- values[state.currField.name] = validateAndParseFieldValue(
4655
- state.currField,
4656
- val
4657
- );
4831
+ const fields = sig.getOutputFields();
4832
+ checkMissingRequiredFields(xstate, values, fields.length - 1);
4658
4833
  };
4659
4834
  var convertValueToType = (field, val) => {
4660
4835
  switch (field.type?.name) {
@@ -4668,6 +4843,9 @@ var convertValueToType = (field, val) => {
4668
4843
  return v;
4669
4844
  }
4670
4845
  case "boolean": {
4846
+ if (typeof val === "boolean") {
4847
+ return val;
4848
+ }
4671
4849
  const v = val.toLowerCase();
4672
4850
  if (v === "true") {
4673
4851
  return true;
@@ -4682,37 +4860,73 @@ var convertValueToType = (field, val) => {
4682
4860
  case "datetime":
4683
4861
  return parseLLMFriendlyDateTime(field, val);
4684
4862
  case "class":
4685
- if (field.type.classes && !field.type.classes.includes(val)) {
4863
+ const className = val;
4864
+ if (field.type.classes && !field.type.classes.includes(className)) {
4686
4865
  throw new Error(
4687
4866
  `Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
4688
4867
  );
4689
4868
  }
4690
- return val;
4869
+ return className;
4691
4870
  default:
4692
4871
  return val;
4693
4872
  }
4694
4873
  };
4695
- var expectedTypeError = (field, err, value = "") => {
4696
- const exp = field.type?.isArray ? `array of ${field.type.name}` : field.type?.name;
4697
- const message = `Error '${err.message}', expected '${exp}' got '${value}'`;
4698
- return new ValidationError({ message, field, value });
4699
- };
4874
+ function* streamValues(sig, values, xstate, content) {
4875
+ if (!xstate.currField) {
4876
+ return;
4877
+ }
4878
+ const fieldName = xstate.currField.name;
4879
+ if (!xstate.streamedIndex) {
4880
+ xstate.streamedIndex = { [fieldName]: 0 };
4881
+ }
4882
+ if (!xstate.currField.type || !xstate.currField.type.isArray && xstate.currField.type.name === "string") {
4883
+ const pos = xstate.streamedIndex[fieldName] ?? 0;
4884
+ const s = xstate.s + pos;
4885
+ const v = content.substring(s);
4886
+ yield { [fieldName]: pos === 0 ? v.trimStart() : v };
4887
+ xstate.streamedIndex[fieldName] = pos + v.length;
4888
+ return;
4889
+ }
4890
+ for (const key of Object.keys(values)) {
4891
+ const value = values[key];
4892
+ if (Array.isArray(value)) {
4893
+ const s = xstate.streamedIndex[key] ?? 0;
4894
+ const v = value.slice(s);
4895
+ if (v) {
4896
+ yield { [key]: v };
4897
+ xstate.streamedIndex[key] = s + 1;
4898
+ }
4899
+ continue;
4900
+ }
4901
+ if (!xstate.streamedIndex[key]) {
4902
+ yield { [key]: value };
4903
+ xstate.streamedIndex[key] = 1;
4904
+ }
4905
+ }
4906
+ }
4700
4907
  function validateAndParseFieldValue(field, fieldValue) {
4701
- const fv = fieldValue?.toLocaleLowerCase();
4702
- if (!fieldValue || !fv || fv === "" || fv === "null" || fv === "undefined") {
4908
+ if (!fieldValue || fieldValue === "" || fieldValue === "null" || fieldValue === "NULL" || fieldValue === "undefined") {
4703
4909
  if (field.isOptional) {
4704
4910
  return;
4705
4911
  }
4706
- throw expectedTypeError(field, new Error("Empty value"), fieldValue);
4912
+ throw new ValidationError({
4913
+ message: "Required field is missing",
4914
+ fields: [field],
4915
+ value: fieldValue
4916
+ });
4707
4917
  }
4708
- let value = fieldValue;
4918
+ let value;
4709
4919
  if (field.type?.name === "json") {
4710
4920
  try {
4711
4921
  const text = extractBlock(fieldValue);
4712
4922
  value = JSON5.parse(text);
4713
4923
  return value;
4714
4924
  } catch (e) {
4715
- throw expectedTypeError(field, e, fieldValue);
4925
+ throw new ValidationError({
4926
+ message: "Invalid JSON: " + e.message,
4927
+ fields: [field],
4928
+ value: fieldValue
4929
+ });
4716
4930
  }
4717
4931
  }
4718
4932
  if (field.type?.isArray) {
@@ -4726,23 +4940,33 @@ function validateAndParseFieldValue(field, fieldValue) {
4726
4940
  throw new Error("Expected an array");
4727
4941
  }
4728
4942
  } catch (e) {
4729
- throw expectedTypeError(field, e, fieldValue);
4943
+ throw new ValidationError({
4944
+ message: "Invalid Array: " + e.message,
4945
+ fields: [field],
4946
+ value: fieldValue
4947
+ });
4730
4948
  }
4731
4949
  }
4732
- if (Array.isArray(value)) {
4733
- for (const [index, item] of value.entries()) {
4734
- try {
4735
- value[index] = convertValueToType(field, item);
4736
- } catch (e) {
4737
- throw expectedTypeError(field, e, item);
4950
+ try {
4951
+ if (Array.isArray(value)) {
4952
+ for (const [index, item] of value.entries()) {
4953
+ if (item !== void 0) {
4954
+ const v = typeof item === "string" ? item.trim() : item;
4955
+ value[index] = convertValueToType(field, v);
4956
+ }
4738
4957
  }
4739
- }
4740
- } else {
4741
- try {
4958
+ } else {
4742
4959
  value = convertValueToType(field, fieldValue);
4743
- } catch (e) {
4744
- throw expectedTypeError(field, e, fieldValue);
4745
4960
  }
4961
+ } catch (e) {
4962
+ throw new ValidationError({
4963
+ message: e.message,
4964
+ fields: [field],
4965
+ value: fieldValue
4966
+ });
4967
+ }
4968
+ if (typeof value === "string" && value === "") {
4969
+ return void 0;
4746
4970
  }
4747
4971
  return value;
4748
4972
  }
@@ -4911,7 +5135,6 @@ function parseFunctionCalls(ai, functionCalls, values, model) {
4911
5135
  }
4912
5136
 
4913
5137
  // dsp/generate.ts
4914
- var colorLog4 = new ColorLog();
4915
5138
  var AxGen = class extends AxProgramWithSignature {
4916
5139
  promptTemplate;
4917
5140
  asserts;
@@ -4976,7 +5199,7 @@ var AxGen = class extends AxProgramWithSignature {
4976
5199
  );
4977
5200
  return res;
4978
5201
  }
4979
- async forwardCore({
5202
+ async *forwardCore({
4980
5203
  ai,
4981
5204
  mem,
4982
5205
  options
@@ -4992,7 +5215,18 @@ var AxGen = class extends AxProgramWithSignature {
4992
5215
  options
4993
5216
  });
4994
5217
  if (res instanceof ReadableStream) {
4995
- return await this.processSteamingResponse({
5218
+ yield* this.processStreamingResponse({
5219
+ ai,
5220
+ model,
5221
+ res,
5222
+ usageInfo,
5223
+ mem,
5224
+ traceId,
5225
+ sessionId,
5226
+ functions
5227
+ });
5228
+ } else {
5229
+ yield await this.processResponse({
4996
5230
  ai,
4997
5231
  model,
4998
5232
  res,
@@ -5003,18 +5237,8 @@ var AxGen = class extends AxProgramWithSignature {
5003
5237
  functions
5004
5238
  });
5005
5239
  }
5006
- return await this.processResponse({
5007
- ai,
5008
- model,
5009
- res,
5010
- usageInfo,
5011
- mem,
5012
- traceId,
5013
- sessionId,
5014
- functions
5015
- });
5016
5240
  }
5017
- async processSteamingResponse({
5241
+ async *processStreamingResponse({
5018
5242
  ai,
5019
5243
  model,
5020
5244
  res,
@@ -5026,36 +5250,50 @@ var AxGen = class extends AxProgramWithSignature {
5026
5250
  }) {
5027
5251
  const functionCalls = [];
5028
5252
  const values = {};
5029
- const xstate = { s: -1 };
5253
+ const xstate = {
5254
+ extractedFields: [],
5255
+ s: -1
5256
+ };
5030
5257
  let content = "";
5031
5258
  for await (const v of res) {
5032
- for (const result of v.results ?? []) {
5033
- if (v.modelUsage) {
5034
- this.usage.push({ ...usageInfo, ...v.modelUsage });
5035
- }
5036
- if (result.content) {
5037
- content += result.content;
5038
- mem.updateResult({ name: result.name, content }, sessionId);
5039
- assertStreamingAssertions(
5040
- this.streamingAsserts,
5041
- values,
5042
- xstate,
5043
- content,
5044
- false
5045
- );
5046
- streamingExtractValues(this.signature, values, xstate, content);
5047
- assertAssertions(this.asserts, values);
5048
- }
5049
- if (result.functionCalls) {
5050
- mergeFunctionCalls(functionCalls, result.functionCalls);
5051
- mem.updateResult(
5052
- { name: result.name, content, functionCalls },
5053
- sessionId
5054
- );
5055
- }
5056
- if (result.finishReason === "length") {
5057
- throw new Error("Max tokens reached before completion");
5259
+ const result = v.results[0];
5260
+ if (!result) {
5261
+ continue;
5262
+ }
5263
+ if (v.modelUsage) {
5264
+ this.usage.push({ ...usageInfo, ...v.modelUsage });
5265
+ }
5266
+ if (result.content) {
5267
+ content += result.content;
5268
+ mem.updateResult({ name: result.name, content }, sessionId);
5269
+ const skip = streamingExtractValues(
5270
+ this.signature,
5271
+ values,
5272
+ xstate,
5273
+ content
5274
+ );
5275
+ if (skip) {
5276
+ continue;
5058
5277
  }
5278
+ assertStreamingAssertions(
5279
+ this.streamingAsserts,
5280
+ values,
5281
+ xstate,
5282
+ content,
5283
+ false
5284
+ );
5285
+ assertAssertions(this.asserts, values);
5286
+ yield* streamValues(this.signature, values, xstate, content);
5287
+ }
5288
+ if (result.functionCalls) {
5289
+ mergeFunctionCalls(functionCalls, result.functionCalls);
5290
+ mem.updateResult(
5291
+ { name: result.name, content, functionCalls },
5292
+ sessionId
5293
+ );
5294
+ }
5295
+ if (result.finishReason === "length") {
5296
+ throw new Error("Max tokens reached before completion");
5059
5297
  }
5060
5298
  }
5061
5299
  const funcs = parseFunctionCalls(ai, functionCalls, values, model);
@@ -5073,7 +5311,6 @@ var AxGen = class extends AxProgramWithSignature {
5073
5311
  );
5074
5312
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5075
5313
  }
5076
- streamingExtractFinalValue(values, xstate, content);
5077
5314
  assertStreamingAssertions(
5078
5315
  this.streamingAsserts,
5079
5316
  values,
@@ -5081,8 +5318,9 @@ var AxGen = class extends AxProgramWithSignature {
5081
5318
  content,
5082
5319
  true
5083
5320
  );
5321
+ streamingExtractFinalValue(this.signature, values, xstate, content);
5084
5322
  assertAssertions(this.asserts, values);
5085
- return { ...values };
5323
+ yield* streamValues(this.signature, values, xstate, content);
5086
5324
  }
5087
5325
  async processResponse({
5088
5326
  ai,
@@ -5094,45 +5332,47 @@ var AxGen = class extends AxProgramWithSignature {
5094
5332
  functions
5095
5333
  }) {
5096
5334
  const values = {};
5097
- for (const result of res.results ?? []) {
5098
- if (res.modelUsage) {
5099
- this.usage.push({ ...usageInfo, ...res.modelUsage });
5100
- }
5101
- mem.addResult(result, sessionId);
5102
- if (result.content) {
5103
- extractValues(this.signature, values, result.content);
5104
- assertAssertions(this.asserts, values);
5105
- }
5106
- if (result.functionCalls) {
5107
- const funcs = parseFunctionCalls(ai, result.functionCalls, values);
5108
- if (funcs) {
5109
- if (!functions) {
5110
- throw new Error("Functions are not defined");
5111
- }
5112
- const fx = await processFunctions(
5113
- ai,
5114
- functions,
5115
- funcs,
5116
- mem,
5117
- sessionId,
5118
- traceId
5119
- );
5120
- this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5335
+ const result = res.results[0];
5336
+ if (!result) {
5337
+ throw new Error("No result found");
5338
+ }
5339
+ if (res.modelUsage) {
5340
+ this.usage.push({ ...usageInfo, ...res.modelUsage });
5341
+ }
5342
+ mem.addResult(result, sessionId);
5343
+ if (result.content) {
5344
+ extractValues(this.signature, values, result.content);
5345
+ assertAssertions(this.asserts, values);
5346
+ }
5347
+ if (result.functionCalls) {
5348
+ const funcs = parseFunctionCalls(ai, result.functionCalls, values);
5349
+ if (funcs) {
5350
+ if (!functions) {
5351
+ throw new Error("Functions are not defined");
5121
5352
  }
5353
+ const fx = await processFunctions(
5354
+ ai,
5355
+ functions,
5356
+ funcs,
5357
+ mem,
5358
+ sessionId,
5359
+ traceId
5360
+ );
5361
+ this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5122
5362
  }
5123
- if (result.finishReason === "length") {
5124
- throw new Error("Max tokens reached before completion");
5125
- }
5363
+ }
5364
+ if (result.finishReason === "length") {
5365
+ throw new Error("Max tokens reached before completion");
5126
5366
  }
5127
5367
  return { ...values };
5128
5368
  }
5129
- async _forward(ai, values, options, span) {
5369
+ async *_forward2(ai, values, options, span) {
5130
5370
  const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
5131
- const maxRetries = options?.maxRetries ?? this.options?.maxRetries ?? 3;
5132
- const maxSteps = options?.maxSteps ?? this.options?.maxSteps ?? 10;
5133
- const mem = options?.mem ?? this.options?.mem ?? new AxMemory();
5371
+ const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
5372
+ const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
5373
+ const mem = options.mem ?? this.options?.mem ?? new AxMemory();
5134
5374
  let err;
5135
- if (options?.functions && options?.functions.length > 0) {
5375
+ if (options?.functions && options.functions.length > 0) {
5136
5376
  const promptTemplate = this.options?.promptTemplate ?? AxPromptTemplate;
5137
5377
  this.promptTemplate = new promptTemplate(
5138
5378
  this.signature,
@@ -5147,25 +5387,21 @@ var AxGen = class extends AxProgramWithSignature {
5147
5387
  multiStepLoop: for (let n = 0; n < maxSteps; n++) {
5148
5388
  for (let errCount = 0; errCount < maxRetries; errCount++) {
5149
5389
  try {
5150
- const output = await this.forwardCore({
5151
- options,
5152
- ai,
5153
- mem
5154
- });
5155
- const lastMemItem = mem.getLast(options?.sessionId);
5156
- if (lastMemItem) {
5157
- const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
5158
- if (lastMemItem.role === "function") {
5159
- if (!stopFunction || !stopFunctionExecuted) {
5160
- continue multiStepLoop;
5161
- }
5162
- }
5163
- if (!stopFunctionExecuted) {
5164
- assertRequiredFields(this.signature, output);
5390
+ const generator = this.forwardCore({ options, ai, mem });
5391
+ for await (const delta of generator) {
5392
+ if (delta !== void 0) {
5393
+ yield { version: errCount, delta };
5165
5394
  }
5166
5395
  }
5167
- this.trace = { ...values, ...output };
5168
- return output;
5396
+ const lastMemItem = mem.getLast(options?.sessionId);
5397
+ const shouldContinue = this.shouldContinueSteps(
5398
+ lastMemItem,
5399
+ stopFunction
5400
+ );
5401
+ if (shouldContinue) {
5402
+ continue multiStepLoop;
5403
+ }
5404
+ return;
5169
5405
  } catch (e) {
5170
5406
  let errorFields;
5171
5407
  span?.recordException(e);
@@ -5180,23 +5416,13 @@ var AxGen = class extends AxProgramWithSignature {
5180
5416
  throw e;
5181
5417
  }
5182
5418
  if (errorFields) {
5183
- mem.add(
5184
- {
5185
- role: "user",
5186
- content: this.promptTemplate.renderExtraFields(errorFields)
5187
- },
5188
- options?.sessionId
5419
+ handleValidationError(
5420
+ mem,
5421
+ errorFields,
5422
+ ai,
5423
+ this.promptTemplate,
5424
+ options.sessionId
5189
5425
  );
5190
- mem.addTag("error");
5191
- if (ai.getOptions().debug) {
5192
- process.stdout.write(
5193
- colorLog4.red(
5194
- `Error Correction:
5195
- ${JSON.stringify(errorFields, null, 2)}
5196
- `
5197
- )
5198
- );
5199
- }
5200
5426
  }
5201
5427
  }
5202
5428
  }
@@ -5207,35 +5433,73 @@ ${JSON.stringify(errorFields, null, 2)}
5207
5433
  }
5208
5434
  throw new Error(`Max steps reached: ${maxSteps}`);
5209
5435
  }
5210
- async forward(ai, values, options) {
5436
+ shouldContinueSteps(lastMemItem, stopFunction) {
5437
+ const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
5438
+ if (lastMemItem?.role === "function" && stopFunction && stopFunctionExecuted) {
5439
+ return false;
5440
+ }
5441
+ if (lastMemItem?.role === "function") {
5442
+ return true;
5443
+ }
5444
+ return false;
5445
+ }
5446
+ async *_forward1(ai, values, options) {
5211
5447
  const tracer = this.options?.tracer ?? options?.tracer;
5212
5448
  let functions = this.functions;
5213
5449
  if (options?.functions) {
5214
5450
  functions = parseFunctions(options.functions, this.functions);
5215
5451
  }
5216
5452
  if (!tracer) {
5217
- return await this._forward(ai, values, {
5453
+ yield* this._forward2(ai, values, {
5218
5454
  ...options,
5219
5455
  functions
5220
5456
  });
5457
+ return;
5221
5458
  }
5222
5459
  const funcNames = functions?.map((f) => f.name).join(",");
5223
5460
  const attributes = {
5224
5461
  ["generate.signature"]: this.signature.toString(),
5225
5462
  ["generate.functions"]: funcNames ?? ""
5226
5463
  };
5227
- return await tracer.startActiveSpan(
5228
- "Generate",
5229
- {
5230
- kind: SpanKind2.SERVER,
5231
- attributes
5232
- },
5233
- async (span) => {
5234
- const res = this._forward(ai, values, options, span);
5235
- span.end();
5236
- return res;
5464
+ const span = tracer.startSpan("Generate", {
5465
+ kind: SpanKind2.SERVER,
5466
+ attributes
5467
+ });
5468
+ try {
5469
+ yield* this._forward2(
5470
+ ai,
5471
+ values,
5472
+ {
5473
+ ...options,
5474
+ functions
5475
+ },
5476
+ span
5477
+ );
5478
+ } finally {
5479
+ span.end();
5480
+ }
5481
+ }
5482
+ async forward(ai, values, options) {
5483
+ const generator = this._forward1(ai, values, {
5484
+ ...options
5485
+ });
5486
+ let buffer = {};
5487
+ let currentVersion = 0;
5488
+ for await (const item of generator) {
5489
+ if (item.version !== currentVersion) {
5490
+ buffer = {};
5237
5491
  }
5238
- );
5492
+ currentVersion = item.version;
5493
+ buffer = mergeDeltas(buffer, item.delta);
5494
+ }
5495
+ this.trace = { ...values, ...buffer };
5496
+ return buffer;
5497
+ }
5498
+ async *streamingForward(ai, values, options) {
5499
+ yield* this._forward1(ai, values, {
5500
+ ...options,
5501
+ stream: true
5502
+ });
5239
5503
  }
5240
5504
  };
5241
5505
 
@@ -5325,7 +5589,7 @@ var AxAgent = class {
5325
5589
  func: wrappedFunc
5326
5590
  };
5327
5591
  }
5328
- async forward(ai, values, options) {
5592
+ init(ai, options) {
5329
5593
  const _ai = this.ai ?? ai;
5330
5594
  const funcs = [
5331
5595
  ...options?.functions ?? [],
@@ -5336,26 +5600,15 @@ var AxAgent = class {
5336
5600
  const opt2 = { ...options, functions: funcs };
5337
5601
  this.program = new AxGen(this.signature, opt2);
5338
5602
  }
5339
- if (!options?.tracer) {
5340
- return await this.program.forward(_ai, values, opt);
5341
- }
5342
- const attributes = {
5343
- ["agent.name"]: this.name,
5344
- ["agent.description"]: this.description,
5345
- ["agent.subAgents"]: this.subAgentList ?? "none"
5346
- };
5347
- return await options?.tracer.startActiveSpan(
5348
- "Agent",
5349
- {
5350
- kind: SpanKind3.SERVER,
5351
- attributes
5352
- },
5353
- async (span) => {
5354
- const res = await this.program.forward(_ai, values, opt);
5355
- span.end();
5356
- return res;
5357
- }
5358
- );
5603
+ return { _ai, opt };
5604
+ }
5605
+ async forward(ai, values, options) {
5606
+ const { _ai, opt } = this.init(ai, options);
5607
+ return await this.program.forward(_ai, values, opt);
5608
+ }
5609
+ async *streamingForward(ai, values, options) {
5610
+ const { _ai, opt } = this.init(ai, options);
5611
+ return yield* this.program.streamingForward(_ai, values, opt);
5359
5612
  }
5360
5613
  };
5361
5614
  function toCamelCase(inputString) {
@@ -5626,7 +5879,7 @@ var randomSample = (array, n) => {
5626
5879
  };
5627
5880
 
5628
5881
  // db/base.ts
5629
- import { SpanKind as SpanKind4 } from "@opentelemetry/api";
5882
+ import { SpanKind as SpanKind3 } from "@opentelemetry/api";
5630
5883
  var AxDBBase = class {
5631
5884
  name;
5632
5885
  fetch;
@@ -5653,7 +5906,7 @@ var AxDBBase = class {
5653
5906
  return await this.tracer?.startActiveSpan(
5654
5907
  "DB Upsert Request",
5655
5908
  {
5656
- kind: SpanKind4.SERVER,
5909
+ kind: SpanKind3.SERVER,
5657
5910
  attributes: {
5658
5911
  [axSpanAttributes.DB_SYSTEM]: this.name,
5659
5912
  [axSpanAttributes.DB_OPERATION_NAME]: "upsert",
@@ -5663,9 +5916,11 @@ var AxDBBase = class {
5663
5916
  }
5664
5917
  },
5665
5918
  async (span) => {
5666
- const res = await this._upsert(req, update, { span });
5667
- span.end();
5668
- return res;
5919
+ try {
5920
+ return await this._upsert(req, update, { span });
5921
+ } finally {
5922
+ span.end();
5923
+ }
5669
5924
  }
5670
5925
  );
5671
5926
  }
@@ -5685,7 +5940,7 @@ var AxDBBase = class {
5685
5940
  return await this.tracer?.startActiveSpan(
5686
5941
  "DB Batch Upsert Request",
5687
5942
  {
5688
- kind: SpanKind4.SERVER,
5943
+ kind: SpanKind3.SERVER,
5689
5944
  attributes: {
5690
5945
  [axSpanAttributes.DB_SYSTEM]: this.name,
5691
5946
  [axSpanAttributes.DB_OPERATION_NAME]: "upsert",
@@ -5695,9 +5950,11 @@ var AxDBBase = class {
5695
5950
  }
5696
5951
  },
5697
5952
  async (span) => {
5698
- const res = await this._batchUpsert(req, update, { span });
5699
- span.end();
5700
- return res;
5953
+ try {
5954
+ return await this._batchUpsert(req, update, { span });
5955
+ } finally {
5956
+ span.end();
5957
+ }
5701
5958
  }
5702
5959
  );
5703
5960
  }
@@ -5711,7 +5968,7 @@ var AxDBBase = class {
5711
5968
  return await this.tracer?.startActiveSpan(
5712
5969
  "DB Query Request",
5713
5970
  {
5714
- kind: SpanKind4.SERVER,
5971
+ kind: SpanKind3.SERVER,
5715
5972
  attributes: {
5716
5973
  [axSpanAttributes.DB_SYSTEM]: this.name,
5717
5974
  [axSpanAttributes.DB_OPERATION_NAME]: "upsert",
@@ -5721,9 +5978,11 @@ var AxDBBase = class {
5721
5978
  }
5722
5979
  },
5723
5980
  async (span) => {
5724
- const res = await this._query(req, { span });
5725
- span.end();
5726
- return res;
5981
+ try {
5982
+ return await this._query(req, { span });
5983
+ } finally {
5984
+ span.end();
5985
+ }
5727
5986
  }
5728
5987
  );
5729
5988
  }
@@ -6990,6 +7249,122 @@ var AxEmbeddingAdapter = class {
6990
7249
  }
6991
7250
  };
6992
7251
 
7252
+ // ai/mock/api.ts
7253
+ var AxMockAIService = class {
7254
+ constructor(config = {}) {
7255
+ this.config = config;
7256
+ }
7257
+ options = {};
7258
+ metrics = {
7259
+ latency: {
7260
+ chat: { mean: 0, p95: 0, p99: 0, samples: [] },
7261
+ embed: { mean: 0, p95: 0, p99: 0, samples: [] }
7262
+ },
7263
+ errors: {
7264
+ chat: { count: 0, rate: 0, total: 0 },
7265
+ embed: { count: 0, rate: 0, total: 0 }
7266
+ }
7267
+ };
7268
+ getName() {
7269
+ return this.config.name ?? "mock-ai-service";
7270
+ }
7271
+ getModelInfo() {
7272
+ return {
7273
+ name: "mock-model",
7274
+ provider: "mock-provider",
7275
+ promptTokenCostPer1M: 100,
7276
+ completionTokenCostPer1M: 100,
7277
+ ...this.config.modelInfo
7278
+ };
7279
+ }
7280
+ getEmbedModelInfo() {
7281
+ return this.config.embedModelInfo;
7282
+ }
7283
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
7284
+ getFeatures(_model) {
7285
+ return {
7286
+ functions: this.config.features?.functions ?? false,
7287
+ streaming: this.config.features?.streaming ?? false
7288
+ };
7289
+ }
7290
+ getModelMap() {
7291
+ return this.config.modelMap;
7292
+ }
7293
+ getMetrics() {
7294
+ return this.metrics;
7295
+ }
7296
+ async chat(req, _options) {
7297
+ if (this.config.latencyMs) {
7298
+ await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
7299
+ }
7300
+ if (this.config.shouldError) {
7301
+ throw new Error(this.config.errorMessage ?? "Mock chat error");
7302
+ }
7303
+ this.updateMetrics("chat");
7304
+ if (typeof this.config.chatResponse === "function") {
7305
+ return this.config.chatResponse(req);
7306
+ }
7307
+ return this.config.chatResponse ?? {
7308
+ results: [
7309
+ {
7310
+ content: "Mock response",
7311
+ finishReason: "stop"
7312
+ }
7313
+ ],
7314
+ modelUsage: {
7315
+ promptTokens: 10,
7316
+ completionTokens: 5,
7317
+ totalTokens: 15
7318
+ }
7319
+ };
7320
+ }
7321
+ async embed(req, _options) {
7322
+ if (this.config.latencyMs) {
7323
+ await new Promise((resolve) => setTimeout(resolve, this.config.latencyMs));
7324
+ }
7325
+ if (this.config.shouldError) {
7326
+ throw new Error(this.config.errorMessage ?? "Mock embed error");
7327
+ }
7328
+ this.updateMetrics("embed");
7329
+ if (typeof this.config.embedResponse === "function") {
7330
+ return this.config.embedResponse(req);
7331
+ }
7332
+ return this.config.embedResponse ?? {
7333
+ embeddings: [[0.1, 0.2, 0.3]],
7334
+ modelUsage: {
7335
+ promptTokens: 5,
7336
+ completionTokens: 0,
7337
+ totalTokens: 5
7338
+ }
7339
+ };
7340
+ }
7341
+ setOptions(options) {
7342
+ this.options = options;
7343
+ }
7344
+ getOptions() {
7345
+ return this.options;
7346
+ }
7347
+ updateMetrics(type) {
7348
+ const latency = this.config.latencyMs ?? 0;
7349
+ this.metrics.latency[type].samples.push(latency);
7350
+ const samples = this.metrics.latency[type].samples;
7351
+ this.metrics.latency[type].mean = samples.reduce((a, b) => a + b, 0) / samples.length;
7352
+ if (samples.length > 0) {
7353
+ const sortedSamples = [...samples].sort((a, b) => a - b);
7354
+ const p95Index = Math.max(0, Math.floor(sortedSamples.length * 0.95) - 1);
7355
+ this.metrics.latency[type].p95 = sortedSamples[p95Index] ?? latency;
7356
+ const p99Index = Math.max(0, Math.floor(sortedSamples.length * 0.99) - 1);
7357
+ this.metrics.latency[type].p99 = sortedSamples[p99Index] ?? latency;
7358
+ }
7359
+ if (this.config.shouldError) {
7360
+ this.metrics.errors[type].count++;
7361
+ this.metrics.errors[type].total++;
7362
+ const totalRequests = this.metrics.latency[type].samples.length;
7363
+ this.metrics.errors[type].rate = totalRequests > 0 ? this.metrics.errors[type].count / totalRequests : 0;
7364
+ }
7365
+ }
7366
+ };
7367
+
6993
7368
  // prompts/rag.ts
6994
7369
  var AxRAG = class extends AxChainOfThought {
6995
7370
  genQuery;
@@ -7027,6 +7402,7 @@ export {
7027
7402
  AxAI,
7028
7403
  AxAIAnthropic,
7029
7404
  AxAIAnthropicModel,
7405
+ AxAIAnthropicVertexModel,
7030
7406
  AxAIAzureOpenAI,
7031
7407
  AxAICohere,
7032
7408
  AxAICohereEmbedModel,
@@ -7078,6 +7454,7 @@ export {
7078
7454
  AxJSInterpreterPermission,
7079
7455
  AxLLMRequestTypeValues,
7080
7456
  AxMemory,
7457
+ AxMockAIService,
7081
7458
  AxProgram,
7082
7459
  AxProgramWithSignature,
7083
7460
  AxPromptTemplate,