@ax-llm/ax 10.0.40 → 10.0.42

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
@@ -789,6 +789,61 @@ var setResponseAttr = (res, span) => {
789
789
  }
790
790
  };
791
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
+
792
847
  // ai/anthropic/types.ts
793
848
  var AxAIAnthropicModel = /* @__PURE__ */ ((AxAIAnthropicModel2) => {
794
849
  AxAIAnthropicModel2["Claude35Sonnet"] = "claude-3-5-sonnet-latest";
@@ -800,6 +855,14 @@ var AxAIAnthropicModel = /* @__PURE__ */ ((AxAIAnthropicModel2) => {
800
855
  AxAIAnthropicModel2["ClaudeInstant12"] = "claude-instant-1.2";
801
856
  return AxAIAnthropicModel2;
802
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 || {});
803
866
 
804
867
  // ai/anthropic/info.ts
805
868
  var axModelInfoAnthropic = [
@@ -856,8 +919,9 @@ var axAIAnthropicDefaultConfig = () => structuredClone({
856
919
  ...axBaseAIDefaultConfig()
857
920
  });
858
921
  var AxAIAnthropicImpl = class {
859
- constructor(config) {
922
+ constructor(config, isVertex) {
860
923
  this.config = config;
924
+ this.isVertex = isVertex;
861
925
  }
862
926
  getModelConfig() {
863
927
  const { config } = this;
@@ -876,9 +940,17 @@ var AxAIAnthropicImpl = class {
876
940
  }
877
941
  createChatReq = (req) => {
878
942
  const model = req.model;
879
- const apiConfig = {
880
- name: "/messages"
881
- };
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
+ }
882
954
  let toolsChoice;
883
955
  if (req.functionCall && req.functions && req.functions.length > 0) {
884
956
  if (typeof req.functionCall === "string") {
@@ -917,9 +989,8 @@ var AxAIAnthropicImpl = class {
917
989
  input_schema: v.parameters
918
990
  })
919
991
  );
920
- const stream = req.modelConfig?.stream ?? this.config.stream;
921
992
  const reqValue = {
922
- model,
993
+ ...this.isVertex ? { anthropic_version: "vertex-2023-10-16" } : { model },
923
994
  max_tokens: req.modelConfig?.maxTokens ?? this.config.maxTokens,
924
995
  stop_sequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
925
996
  temperature: req.modelConfig?.temperature ?? this.config.temperature,
@@ -1074,26 +1145,45 @@ var AxAIAnthropicImpl = class {
1074
1145
  var AxAIAnthropic = class extends AxBaseAI {
1075
1146
  constructor({
1076
1147
  apiKey,
1148
+ projectId,
1149
+ region,
1077
1150
  config,
1078
1151
  options,
1079
1152
  modelMap
1080
1153
  }) {
1081
- if (!apiKey || apiKey === "") {
1082
- 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
+ });
1083
1177
  }
1084
1178
  const _config = {
1085
1179
  ...axAIAnthropicDefaultConfig(),
1086
1180
  ...config
1087
1181
  };
1088
- const aiImpl = new AxAIAnthropicImpl(_config);
1182
+ const aiImpl = new AxAIAnthropicImpl(_config, isVertex);
1089
1183
  super(aiImpl, {
1090
1184
  name: "Anthropic",
1091
- apiURL: "https://api.anthropic.com/v1",
1092
- headers: async () => ({
1093
- "anthropic-version": "2023-06-01",
1094
- "anthropic-beta": "prompt-caching-2024-07-31",
1095
- "x-api-key": apiKey
1096
- }),
1185
+ apiURL,
1186
+ headers,
1097
1187
  modelInfo: axModelInfoAnthropic,
1098
1188
  models: { model: _config.model },
1099
1189
  options,
@@ -2030,61 +2120,6 @@ var AxAIDeepSeek = class extends AxAIOpenAI {
2030
2120
  }
2031
2121
  };
2032
2122
 
2033
- // ai/google-gemini/auth.ts
2034
- import { GoogleAuth } from "google-auth-library";
2035
- var GoogleVertexAuth = class {
2036
- auth;
2037
- client;
2038
- currentToken;
2039
- tokenExpiry;
2040
- constructor(config = {}) {
2041
- this.auth = new GoogleAuth({
2042
- scopes: ["https://www.googleapis.com/auth/cloud-platform"],
2043
- ...config
2044
- });
2045
- }
2046
- async getAuthenticatedClient() {
2047
- if (!this.client) {
2048
- this.client = await this.auth.getClient();
2049
- }
2050
- return this.client;
2051
- }
2052
- async getAccessToken() {
2053
- if (this.currentToken && this.tokenExpiry && Date.now() < this.tokenExpiry) {
2054
- return this.currentToken;
2055
- }
2056
- const client = await this.getAuthenticatedClient();
2057
- const tokenResponse = await client.getAccessToken();
2058
- this.currentToken = tokenResponse.token ?? void 0;
2059
- const expiry = this.getExpiry(tokenResponse);
2060
- const fiveMinutes = 5 * 60 * 1e3;
2061
- this.tokenExpiry = expiry - fiveMinutes;
2062
- return this.currentToken;
2063
- }
2064
- /**
2065
- * Get the expiry date from the token response.
2066
- */
2067
- getExpiry(tokenResponse) {
2068
- const oneHour = 3600 * 1e3;
2069
- let expiry = Date.now() + oneHour;
2070
- let responseExpiry = tokenResponse.res?.data?.expiry_date;
2071
- if (responseExpiry) {
2072
- if (typeof responseExpiry === "number") {
2073
- expiry = responseExpiry;
2074
- } else if (responseExpiry instanceof Date) {
2075
- expiry = responseExpiry.getTime();
2076
- } else if (typeof responseExpiry === "string") {
2077
- expiry = new Date(responseExpiry).getTime();
2078
- } else {
2079
- console.warn("Unknown expiry type", responseExpiry);
2080
- }
2081
- } else {
2082
- console.warn("No expiry date found in response", tokenResponse.res?.data);
2083
- }
2084
- return expiry;
2085
- }
2086
- };
2087
-
2088
2123
  // ai/google-gemini/types.ts
2089
2124
  var AxAIGoogleGeminiModel = /* @__PURE__ */ ((AxAIGoogleGeminiModel2) => {
2090
2125
  AxAIGoogleGeminiModel2["Gemini1Pro"] = "gemini-1.0-pro";
@@ -2174,11 +2209,10 @@ var axAIGoogleGeminiDefaultConfig = () => structuredClone({
2174
2209
  ...axBaseAIDefaultConfig()
2175
2210
  });
2176
2211
  var AxAIGoogleGeminiImpl = class {
2177
- constructor(config, isVertex, apiKey, keyFile, options) {
2212
+ constructor(config, isVertex, apiKey, options) {
2178
2213
  this.config = config;
2179
2214
  this.isVertex = isVertex;
2180
2215
  this.apiKey = apiKey;
2181
- this.keyFile = keyFile;
2182
2216
  this.options = options;
2183
2217
  }
2184
2218
  getModelConfig() {
@@ -2455,7 +2489,6 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2455
2489
  apiKey,
2456
2490
  projectId,
2457
2491
  region,
2458
- keyFile,
2459
2492
  config,
2460
2493
  options,
2461
2494
  modelMap
@@ -2468,9 +2501,7 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2468
2501
  if (apiKey) {
2469
2502
  headers = async () => ({ Authorization: `Bearer ${apiKey}` });
2470
2503
  } else {
2471
- const vertexAuth = new GoogleVertexAuth({
2472
- keyFile
2473
- });
2504
+ const vertexAuth = new GoogleVertexAuth();
2474
2505
  headers = async () => ({
2475
2506
  Authorization: `Bearer ${await vertexAuth.getAccessToken()}`
2476
2507
  });
@@ -2486,13 +2517,7 @@ var AxAIGoogleGemini = class extends AxBaseAI {
2486
2517
  ...axAIGoogleGeminiDefaultConfig(),
2487
2518
  ...config
2488
2519
  };
2489
- const aiImpl = new AxAIGoogleGeminiImpl(
2490
- _config,
2491
- isVertex,
2492
- apiKey,
2493
- keyFile,
2494
- options
2495
- );
2520
+ const aiImpl = new AxAIGoogleGeminiImpl(_config, isVertex, apiKey, options);
2496
2521
  super(aiImpl, {
2497
2522
  name: "GoogleGeminiAI",
2498
2523
  apiURL,
@@ -4010,19 +4035,65 @@ var parseMarkdownList = (input) => {
4010
4035
  return list;
4011
4036
  };
4012
4037
  function mergeDeltas(base, delta) {
4013
- const merged = { ...base };
4014
- for (const key in delta) {
4038
+ for (const key of Object.keys(delta)) {
4015
4039
  const baseValue = base[key];
4016
4040
  const deltaValue = delta[key];
4017
- if (Array.isArray(baseValue) && Array.isArray(deltaValue)) {
4018
- merged[key] = [...baseValue, ...deltaValue];
4019
- } else if (typeof baseValue === "string" && typeof deltaValue === "string") {
4020
- merged[key] = baseValue + deltaValue;
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;
4021
4045
  } else {
4022
- merged[key] = deltaValue;
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
+ }
4023
4073
  }
4074
+ this.cache.set(key, value);
4024
4075
  }
4025
- return merged;
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;
4026
4097
  }
4027
4098
 
4028
4099
  // dsp/program.ts
@@ -4619,14 +4690,11 @@ function handleValidationError(mem, errorFields, ai, promptTemplate, sessionId)
4619
4690
  );
4620
4691
  mem.addTag("error");
4621
4692
  if (ai.getOptions().debug) {
4622
- process.stdout.write(
4623
- colorLog4.red(
4624
- `
4693
+ const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
4694
+ process.stdout.write(colorLog4.red(`
4625
4695
  Error Correction:
4626
- ${JSON.stringify(errorFields, null, 2)}
4627
- `
4628
- )
4629
- );
4696
+ ${errors}
4697
+ `));
4630
4698
  }
4631
4699
  }
4632
4700
 
@@ -4721,19 +4789,24 @@ var streamingExtractValues = (sig, values, xstate, content) => {
4721
4789
  continue;
4722
4790
  }
4723
4791
  const prefix = field.title + ":";
4724
- const e = content.indexOf(prefix, xstate.s + 1);
4725
- if (e === -1) {
4726
- continue;
4792
+ let e = matchesContent(content, prefix, xstate.s + 1);
4793
+ switch (e) {
4794
+ case -1:
4795
+ continue;
4796
+ // Field is not found, continue to the next field
4797
+ case -2:
4798
+ return true;
4727
4799
  }
4800
+ let prefixLen = prefix.length;
4728
4801
  if (xstate.currField) {
4729
- const val = content.substring(xstate.s, e);
4802
+ const val = content.substring(xstate.s, e).trim();
4730
4803
  const parsedValue = validateAndParseFieldValue(xstate.currField, val);
4731
4804
  if (parsedValue !== void 0) {
4732
4805
  values[xstate.currField.name] = parsedValue;
4733
4806
  }
4734
4807
  }
4735
4808
  checkMissingRequiredFields(xstate, values, index);
4736
- xstate.s = e + prefix.length;
4809
+ xstate.s = e + prefixLen;
4737
4810
  xstate.currField = field;
4738
4811
  if (!xstate.extractedFields.includes(field)) {
4739
4812
  xstate.extractedFields.push(field);
@@ -4742,7 +4815,7 @@ var streamingExtractValues = (sig, values, xstate, content) => {
4742
4815
  };
4743
4816
  var streamingExtractFinalValue = (sig, values, xstate, content) => {
4744
4817
  if (xstate.currField) {
4745
- const val = content.substring(xstate.s);
4818
+ const val = content.substring(xstate.s).trim();
4746
4819
  const parsedValue = validateAndParseFieldValue(xstate.currField, val);
4747
4820
  if (parsedValue !== void 0) {
4748
4821
  values[xstate.currField.name] = parsedValue;
@@ -4763,6 +4836,9 @@ var convertValueToType = (field, val) => {
4763
4836
  return v;
4764
4837
  }
4765
4838
  case "boolean": {
4839
+ if (typeof val === "boolean") {
4840
+ return val;
4841
+ }
4766
4842
  const v = val.toLowerCase();
4767
4843
  if (v === "true") {
4768
4844
  return true;
@@ -4777,17 +4853,18 @@ var convertValueToType = (field, val) => {
4777
4853
  case "datetime":
4778
4854
  return parseLLMFriendlyDateTime(field, val);
4779
4855
  case "class":
4780
- if (field.type.classes && !field.type.classes.includes(val)) {
4856
+ const className = val;
4857
+ if (field.type.classes && !field.type.classes.includes(className)) {
4781
4858
  throw new Error(
4782
4859
  `Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
4783
4860
  );
4784
4861
  }
4785
- return val;
4862
+ return className;
4786
4863
  default:
4787
4864
  return val;
4788
4865
  }
4789
4866
  };
4790
- function* streamingValues(sig, values, xstate, content) {
4867
+ function* streamValues(sig, values, xstate, content, final = false) {
4791
4868
  if (!xstate.currField) {
4792
4869
  return;
4793
4870
  }
@@ -4795,81 +4872,88 @@ function* streamingValues(sig, values, xstate, content) {
4795
4872
  if (!xstate.streamedIndex) {
4796
4873
  xstate.streamedIndex = { [fieldName]: 0 };
4797
4874
  }
4798
- if (!xstate.currField.type || !xstate.currField.type.isArray && xstate.currField.type.name === "string") {
4799
- const s = xstate.s + (xstate.streamedIndex[fieldName] ?? 0);
4800
- const v = content.substring(s);
4801
- yield { [fieldName]: v };
4802
- xstate.streamedIndex[fieldName] = v.length;
4803
- return;
4875
+ if (!final) {
4876
+ if (!xstate.currField.type || !xstate.currField.type.isArray && xstate.currField.type.name === "string") {
4877
+ const pos = xstate.streamedIndex[fieldName] ?? 0;
4878
+ const s = xstate.s + pos;
4879
+ const v = content.substring(s);
4880
+ const v1 = v.replace(/[\s\n\t]+$/, "");
4881
+ const v2 = pos === 0 ? v1.trimStart() : v1;
4882
+ yield { [fieldName]: v2 };
4883
+ xstate.streamedIndex[fieldName] = pos + v1.length;
4884
+ return;
4885
+ }
4804
4886
  }
4805
4887
  for (const key of Object.keys(values)) {
4806
4888
  const value = values[key];
4807
4889
  if (Array.isArray(value)) {
4808
- const s = xstate.streamedIndex[fieldName] ?? 0;
4890
+ const s = xstate.streamedIndex[key] ?? 0;
4809
4891
  const v = value.slice(s);
4810
4892
  if (v) {
4811
- yield { [fieldName]: v };
4812
- xstate.streamedIndex[fieldName] = s + 1;
4893
+ yield { [key]: v };
4894
+ xstate.streamedIndex[key] = s + 1;
4813
4895
  }
4814
4896
  continue;
4815
4897
  }
4816
- if (!xstate.streamedIndex[fieldName]) {
4817
- yield { [fieldName]: value };
4818
- xstate.streamedIndex[fieldName] = 1;
4898
+ if (!xstate.streamedIndex[key]) {
4899
+ yield { [key]: value };
4900
+ xstate.streamedIndex[key] = 1;
4819
4901
  }
4820
4902
  }
4821
4903
  }
4822
4904
  function validateAndParseFieldValue(field, fieldValue) {
4823
- const fv = fieldValue?.trim();
4824
- if (!fv || !fv || fv === "" || fv === "null" || fv === "NULL" || fv === "undefined") {
4905
+ if (!fieldValue || fieldValue === "" || fieldValue === "null" || fieldValue === "NULL" || fieldValue === "undefined") {
4825
4906
  if (field.isOptional) {
4826
4907
  return;
4827
4908
  }
4828
4909
  throw new ValidationError({
4829
4910
  message: "Required field is missing",
4830
4911
  fields: [field],
4831
- value: fv
4912
+ value: fieldValue
4832
4913
  });
4833
4914
  }
4834
4915
  let value;
4835
4916
  if (field.type?.name === "json") {
4836
4917
  try {
4837
- const text = extractBlock(fv);
4918
+ const text = extractBlock(fieldValue);
4838
4919
  value = JSON5.parse(text);
4839
4920
  return value;
4840
4921
  } catch (e) {
4841
4922
  throw new ValidationError({
4842
4923
  message: "Invalid JSON: " + e.message,
4843
4924
  fields: [field],
4844
- value: fv
4925
+ value: fieldValue
4845
4926
  });
4846
4927
  }
4847
4928
  }
4848
4929
  if (field.type?.isArray) {
4849
4930
  try {
4850
4931
  try {
4851
- value = JSON5.parse(fv);
4932
+ value = JSON5.parse(fieldValue);
4852
4933
  } catch {
4853
- value = parseMarkdownList(fv);
4934
+ value = parseMarkdownList(fieldValue);
4854
4935
  }
4855
4936
  if (!Array.isArray(value)) {
4856
4937
  throw new Error("Expected an array");
4857
4938
  }
4858
4939
  } catch (e) {
4859
4940
  throw new ValidationError({
4860
- message: "Invalid array: " + e.message,
4941
+ message: "Invalid Array: " + e.message,
4861
4942
  fields: [field],
4862
- value: fv
4943
+ value: fieldValue
4863
4944
  });
4864
4945
  }
4865
4946
  }
4866
4947
  try {
4867
4948
  if (Array.isArray(value)) {
4868
4949
  for (const [index, item] of value.entries()) {
4869
- value[index] = convertValueToType(field, item);
4950
+ if (item !== void 0) {
4951
+ const v = typeof item === "string" ? item.trim() : item;
4952
+ value[index] = convertValueToType(field, v);
4953
+ }
4870
4954
  }
4871
4955
  } else {
4872
- value = convertValueToType(field, fv);
4956
+ value = convertValueToType(field, fieldValue);
4873
4957
  }
4874
4958
  } catch (e) {
4875
4959
  throw new ValidationError({
@@ -4878,7 +4962,10 @@ function validateAndParseFieldValue(field, fieldValue) {
4878
4962
  value: fieldValue
4879
4963
  });
4880
4964
  }
4881
- return value ?? fv;
4965
+ if (typeof value === "string" && value === "") {
4966
+ return void 0;
4967
+ }
4968
+ return value;
4882
4969
  }
4883
4970
  var extractBlock = (input) => {
4884
4971
  const jsonBlockPattern = /```([A-Za-z]+)?\s*([\s\S]*?)\s*```/g;
@@ -5160,7 +5247,10 @@ var AxGen = class extends AxProgramWithSignature {
5160
5247
  }) {
5161
5248
  const functionCalls = [];
5162
5249
  const values = {};
5163
- const xstate = { extractedFields: [], s: -1 };
5250
+ const xstate = {
5251
+ extractedFields: [],
5252
+ s: -1
5253
+ };
5164
5254
  let content = "";
5165
5255
  for await (const v of res) {
5166
5256
  const result = v.results[0];
@@ -5173,6 +5263,15 @@ var AxGen = class extends AxProgramWithSignature {
5173
5263
  if (result.content) {
5174
5264
  content += result.content;
5175
5265
  mem.updateResult({ name: result.name, content }, sessionId);
5266
+ const skip = streamingExtractValues(
5267
+ this.signature,
5268
+ values,
5269
+ xstate,
5270
+ content
5271
+ );
5272
+ if (skip) {
5273
+ continue;
5274
+ }
5176
5275
  assertStreamingAssertions(
5177
5276
  this.streamingAsserts,
5178
5277
  values,
@@ -5180,9 +5279,8 @@ var AxGen = class extends AxProgramWithSignature {
5180
5279
  content,
5181
5280
  false
5182
5281
  );
5183
- streamingExtractValues(this.signature, values, xstate, content);
5184
5282
  assertAssertions(this.asserts, values);
5185
- yield* streamingValues(this.signature, values, xstate, content);
5283
+ yield* streamValues(this.signature, values, xstate, content);
5186
5284
  }
5187
5285
  if (result.functionCalls) {
5188
5286
  mergeFunctionCalls(functionCalls, result.functionCalls);
@@ -5210,6 +5308,7 @@ var AxGen = class extends AxProgramWithSignature {
5210
5308
  );
5211
5309
  this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
5212
5310
  }
5311
+ streamingExtractFinalValue(this.signature, values, xstate, content);
5213
5312
  assertStreamingAssertions(
5214
5313
  this.streamingAsserts,
5215
5314
  values,
@@ -5217,9 +5316,8 @@ var AxGen = class extends AxProgramWithSignature {
5217
5316
  content,
5218
5317
  true
5219
5318
  );
5220
- streamingExtractFinalValue(this.signature, values, xstate, content);
5221
5319
  assertAssertions(this.asserts, values);
5222
- return values;
5320
+ yield* streamValues(this.signature, values, xstate, content, true);
5223
5321
  }
5224
5322
  async processResponse({
5225
5323
  ai,
@@ -5288,10 +5386,9 @@ var AxGen = class extends AxProgramWithSignature {
5288
5386
  try {
5289
5387
  const generator = this.forwardCore({ options, ai, mem });
5290
5388
  for await (const delta of generator) {
5291
- yield {
5292
- version: errCount,
5293
- delta
5294
- };
5389
+ if (delta !== void 0) {
5390
+ yield { version: errCount, delta };
5391
+ }
5295
5392
  }
5296
5393
  const lastMemItem = mem.getLast(options?.sessionId);
5297
5394
  const shouldContinue = this.shouldContinueSteps(
@@ -5384,8 +5481,13 @@ var AxGen = class extends AxProgramWithSignature {
5384
5481
  ...options
5385
5482
  });
5386
5483
  let buffer = {};
5387
- for await (const delta of generator) {
5388
- buffer = mergeDeltas(buffer, delta.delta);
5484
+ let currentVersion = 0;
5485
+ for await (const item of generator) {
5486
+ if (item.version !== currentVersion) {
5487
+ buffer = {};
5488
+ }
5489
+ currentVersion = item.version;
5490
+ buffer = mergeDeltas(buffer, item.delta);
5389
5491
  }
5390
5492
  this.trace = { ...values, ...buffer };
5391
5493
  return buffer;
@@ -7297,6 +7399,7 @@ export {
7297
7399
  AxAI,
7298
7400
  AxAIAnthropic,
7299
7401
  AxAIAnthropicModel,
7402
+ AxAIAnthropicVertexModel,
7300
7403
  AxAIAzureOpenAI,
7301
7404
  AxAICohere,
7302
7405
  AxAICohereEmbedModel,