@ax-llm/ax 10.0.40 → 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.cjs +245 -138
- package/index.cjs.map +1 -1
- package/index.d.cts +16 -7
- package/index.d.ts +16 -7
- package/index.js +244 -138
- package/index.js.map +1 -1
- package/package.json +1 -1
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
|
|
880
|
-
|
|
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
|
-
|
|
1082
|
-
|
|
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
|
|
1092
|
-
headers
|
|
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,
|
|
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
|
|
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
|
-
|
|
4019
|
-
} else if (typeof baseValue === "string" && typeof deltaValue === "string") {
|
|
4020
|
-
|
|
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
|
-
|
|
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;
|
|
4023
4094
|
}
|
|
4024
4095
|
}
|
|
4025
|
-
return
|
|
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
|
-
|
|
4623
|
-
|
|
4624
|
-
`
|
|
4693
|
+
const errors = errorFields.map((field) => `- ${field.title}: ${field.description}`).join("\n");
|
|
4694
|
+
process.stdout.write(colorLog4.red(`
|
|
4625
4695
|
Error Correction:
|
|
4626
|
-
${
|
|
4627
|
-
`
|
|
4628
|
-
)
|
|
4629
|
-
);
|
|
4696
|
+
${errors}
|
|
4697
|
+
`));
|
|
4630
4698
|
}
|
|
4631
4699
|
}
|
|
4632
4700
|
|
|
@@ -4715,25 +4783,37 @@ var checkMissingRequiredFields = (xstate, values, currentIndex) => {
|
|
|
4715
4783
|
}
|
|
4716
4784
|
};
|
|
4717
4785
|
var streamingExtractValues = (sig, values, xstate, content) => {
|
|
4786
|
+
if (content.endsWith("\n")) {
|
|
4787
|
+
return true;
|
|
4788
|
+
}
|
|
4718
4789
|
const fields = sig.getOutputFields();
|
|
4719
4790
|
for (const [index, field] of fields.entries()) {
|
|
4720
4791
|
if (field.name in values) {
|
|
4721
4792
|
continue;
|
|
4722
4793
|
}
|
|
4723
4794
|
const prefix = field.title + ":";
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
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;
|
|
4802
|
+
}
|
|
4803
|
+
let prefixLen = prefix.length;
|
|
4804
|
+
if (e - 1 >= 0 && content[e - 1] === "\n") {
|
|
4805
|
+
e -= 1;
|
|
4806
|
+
prefixLen += 1;
|
|
4727
4807
|
}
|
|
4728
4808
|
if (xstate.currField) {
|
|
4729
|
-
const val = content.substring(xstate.s, e);
|
|
4809
|
+
const val = content.substring(xstate.s, e).trim();
|
|
4730
4810
|
const parsedValue = validateAndParseFieldValue(xstate.currField, val);
|
|
4731
4811
|
if (parsedValue !== void 0) {
|
|
4732
4812
|
values[xstate.currField.name] = parsedValue;
|
|
4733
4813
|
}
|
|
4734
4814
|
}
|
|
4735
4815
|
checkMissingRequiredFields(xstate, values, index);
|
|
4736
|
-
xstate.s = e +
|
|
4816
|
+
xstate.s = e + prefixLen;
|
|
4737
4817
|
xstate.currField = field;
|
|
4738
4818
|
if (!xstate.extractedFields.includes(field)) {
|
|
4739
4819
|
xstate.extractedFields.push(field);
|
|
@@ -4742,7 +4822,7 @@ var streamingExtractValues = (sig, values, xstate, content) => {
|
|
|
4742
4822
|
};
|
|
4743
4823
|
var streamingExtractFinalValue = (sig, values, xstate, content) => {
|
|
4744
4824
|
if (xstate.currField) {
|
|
4745
|
-
const val = content.substring(xstate.s);
|
|
4825
|
+
const val = content.substring(xstate.s).trim();
|
|
4746
4826
|
const parsedValue = validateAndParseFieldValue(xstate.currField, val);
|
|
4747
4827
|
if (parsedValue !== void 0) {
|
|
4748
4828
|
values[xstate.currField.name] = parsedValue;
|
|
@@ -4763,6 +4843,9 @@ var convertValueToType = (field, val) => {
|
|
|
4763
4843
|
return v;
|
|
4764
4844
|
}
|
|
4765
4845
|
case "boolean": {
|
|
4846
|
+
if (typeof val === "boolean") {
|
|
4847
|
+
return val;
|
|
4848
|
+
}
|
|
4766
4849
|
const v = val.toLowerCase();
|
|
4767
4850
|
if (v === "true") {
|
|
4768
4851
|
return true;
|
|
@@ -4777,17 +4860,18 @@ var convertValueToType = (field, val) => {
|
|
|
4777
4860
|
case "datetime":
|
|
4778
4861
|
return parseLLMFriendlyDateTime(field, val);
|
|
4779
4862
|
case "class":
|
|
4780
|
-
|
|
4863
|
+
const className = val;
|
|
4864
|
+
if (field.type.classes && !field.type.classes.includes(className)) {
|
|
4781
4865
|
throw new Error(
|
|
4782
4866
|
`Invalid class '${val}', expected one of the following: ${field.type.classes.join(", ")}`
|
|
4783
4867
|
);
|
|
4784
4868
|
}
|
|
4785
|
-
return
|
|
4869
|
+
return className;
|
|
4786
4870
|
default:
|
|
4787
4871
|
return val;
|
|
4788
4872
|
}
|
|
4789
4873
|
};
|
|
4790
|
-
function*
|
|
4874
|
+
function* streamValues(sig, values, xstate, content) {
|
|
4791
4875
|
if (!xstate.currField) {
|
|
4792
4876
|
return;
|
|
4793
4877
|
}
|
|
@@ -4796,80 +4880,83 @@ function* streamingValues(sig, values, xstate, content) {
|
|
|
4796
4880
|
xstate.streamedIndex = { [fieldName]: 0 };
|
|
4797
4881
|
}
|
|
4798
4882
|
if (!xstate.currField.type || !xstate.currField.type.isArray && xstate.currField.type.name === "string") {
|
|
4799
|
-
const
|
|
4883
|
+
const pos = xstate.streamedIndex[fieldName] ?? 0;
|
|
4884
|
+
const s = xstate.s + pos;
|
|
4800
4885
|
const v = content.substring(s);
|
|
4801
|
-
yield { [fieldName]: v };
|
|
4802
|
-
xstate.streamedIndex[fieldName] = v.length;
|
|
4886
|
+
yield { [fieldName]: pos === 0 ? v.trimStart() : v };
|
|
4887
|
+
xstate.streamedIndex[fieldName] = pos + v.length;
|
|
4803
4888
|
return;
|
|
4804
4889
|
}
|
|
4805
4890
|
for (const key of Object.keys(values)) {
|
|
4806
4891
|
const value = values[key];
|
|
4807
4892
|
if (Array.isArray(value)) {
|
|
4808
|
-
const s = xstate.streamedIndex[
|
|
4893
|
+
const s = xstate.streamedIndex[key] ?? 0;
|
|
4809
4894
|
const v = value.slice(s);
|
|
4810
4895
|
if (v) {
|
|
4811
|
-
yield { [
|
|
4812
|
-
xstate.streamedIndex[
|
|
4896
|
+
yield { [key]: v };
|
|
4897
|
+
xstate.streamedIndex[key] = s + 1;
|
|
4813
4898
|
}
|
|
4814
4899
|
continue;
|
|
4815
4900
|
}
|
|
4816
|
-
if (!xstate.streamedIndex[
|
|
4817
|
-
yield { [
|
|
4818
|
-
xstate.streamedIndex[
|
|
4901
|
+
if (!xstate.streamedIndex[key]) {
|
|
4902
|
+
yield { [key]: value };
|
|
4903
|
+
xstate.streamedIndex[key] = 1;
|
|
4819
4904
|
}
|
|
4820
4905
|
}
|
|
4821
4906
|
}
|
|
4822
4907
|
function validateAndParseFieldValue(field, fieldValue) {
|
|
4823
|
-
|
|
4824
|
-
if (!fv || !fv || fv === "" || fv === "null" || fv === "NULL" || fv === "undefined") {
|
|
4908
|
+
if (!fieldValue || fieldValue === "" || fieldValue === "null" || fieldValue === "NULL" || fieldValue === "undefined") {
|
|
4825
4909
|
if (field.isOptional) {
|
|
4826
4910
|
return;
|
|
4827
4911
|
}
|
|
4828
4912
|
throw new ValidationError({
|
|
4829
4913
|
message: "Required field is missing",
|
|
4830
4914
|
fields: [field],
|
|
4831
|
-
value:
|
|
4915
|
+
value: fieldValue
|
|
4832
4916
|
});
|
|
4833
4917
|
}
|
|
4834
4918
|
let value;
|
|
4835
4919
|
if (field.type?.name === "json") {
|
|
4836
4920
|
try {
|
|
4837
|
-
const text = extractBlock(
|
|
4921
|
+
const text = extractBlock(fieldValue);
|
|
4838
4922
|
value = JSON5.parse(text);
|
|
4839
4923
|
return value;
|
|
4840
4924
|
} catch (e) {
|
|
4841
4925
|
throw new ValidationError({
|
|
4842
4926
|
message: "Invalid JSON: " + e.message,
|
|
4843
4927
|
fields: [field],
|
|
4844
|
-
value:
|
|
4928
|
+
value: fieldValue
|
|
4845
4929
|
});
|
|
4846
4930
|
}
|
|
4847
4931
|
}
|
|
4848
4932
|
if (field.type?.isArray) {
|
|
4849
4933
|
try {
|
|
4850
4934
|
try {
|
|
4851
|
-
value = JSON5.parse(
|
|
4935
|
+
value = JSON5.parse(fieldValue);
|
|
4852
4936
|
} catch {
|
|
4853
|
-
value = parseMarkdownList(
|
|
4937
|
+
value = parseMarkdownList(fieldValue);
|
|
4854
4938
|
}
|
|
4855
4939
|
if (!Array.isArray(value)) {
|
|
4856
4940
|
throw new Error("Expected an array");
|
|
4857
4941
|
}
|
|
4858
4942
|
} catch (e) {
|
|
4859
4943
|
throw new ValidationError({
|
|
4860
|
-
message: "Invalid
|
|
4944
|
+
message: "Invalid Array: " + e.message,
|
|
4861
4945
|
fields: [field],
|
|
4862
|
-
value:
|
|
4946
|
+
value: fieldValue
|
|
4863
4947
|
});
|
|
4864
4948
|
}
|
|
4865
4949
|
}
|
|
4866
4950
|
try {
|
|
4867
4951
|
if (Array.isArray(value)) {
|
|
4868
4952
|
for (const [index, item] of value.entries()) {
|
|
4869
|
-
|
|
4953
|
+
if (item !== void 0) {
|
|
4954
|
+
const v = typeof item === "string" ? item.trim() : item;
|
|
4955
|
+
value[index] = convertValueToType(field, v);
|
|
4956
|
+
}
|
|
4870
4957
|
}
|
|
4871
4958
|
} else {
|
|
4872
|
-
value = convertValueToType(field,
|
|
4959
|
+
value = convertValueToType(field, fieldValue);
|
|
4873
4960
|
}
|
|
4874
4961
|
} catch (e) {
|
|
4875
4962
|
throw new ValidationError({
|
|
@@ -4878,7 +4965,10 @@ function validateAndParseFieldValue(field, fieldValue) {
|
|
|
4878
4965
|
value: fieldValue
|
|
4879
4966
|
});
|
|
4880
4967
|
}
|
|
4881
|
-
|
|
4968
|
+
if (typeof value === "string" && value === "") {
|
|
4969
|
+
return void 0;
|
|
4970
|
+
}
|
|
4971
|
+
return value;
|
|
4882
4972
|
}
|
|
4883
4973
|
var extractBlock = (input) => {
|
|
4884
4974
|
const jsonBlockPattern = /```([A-Za-z]+)?\s*([\s\S]*?)\s*```/g;
|
|
@@ -5160,7 +5250,10 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5160
5250
|
}) {
|
|
5161
5251
|
const functionCalls = [];
|
|
5162
5252
|
const values = {};
|
|
5163
|
-
const xstate = {
|
|
5253
|
+
const xstate = {
|
|
5254
|
+
extractedFields: [],
|
|
5255
|
+
s: -1
|
|
5256
|
+
};
|
|
5164
5257
|
let content = "";
|
|
5165
5258
|
for await (const v of res) {
|
|
5166
5259
|
const result = v.results[0];
|
|
@@ -5173,6 +5266,15 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5173
5266
|
if (result.content) {
|
|
5174
5267
|
content += result.content;
|
|
5175
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;
|
|
5277
|
+
}
|
|
5176
5278
|
assertStreamingAssertions(
|
|
5177
5279
|
this.streamingAsserts,
|
|
5178
5280
|
values,
|
|
@@ -5180,9 +5282,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5180
5282
|
content,
|
|
5181
5283
|
false
|
|
5182
5284
|
);
|
|
5183
|
-
streamingExtractValues(this.signature, values, xstate, content);
|
|
5184
5285
|
assertAssertions(this.asserts, values);
|
|
5185
|
-
yield*
|
|
5286
|
+
yield* streamValues(this.signature, values, xstate, content);
|
|
5186
5287
|
}
|
|
5187
5288
|
if (result.functionCalls) {
|
|
5188
5289
|
mergeFunctionCalls(functionCalls, result.functionCalls);
|
|
@@ -5219,7 +5320,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5219
5320
|
);
|
|
5220
5321
|
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
5221
5322
|
assertAssertions(this.asserts, values);
|
|
5222
|
-
|
|
5323
|
+
yield* streamValues(this.signature, values, xstate, content);
|
|
5223
5324
|
}
|
|
5224
5325
|
async processResponse({
|
|
5225
5326
|
ai,
|
|
@@ -5288,10 +5389,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5288
5389
|
try {
|
|
5289
5390
|
const generator = this.forwardCore({ options, ai, mem });
|
|
5290
5391
|
for await (const delta of generator) {
|
|
5291
|
-
|
|
5292
|
-
version: errCount,
|
|
5293
|
-
|
|
5294
|
-
};
|
|
5392
|
+
if (delta !== void 0) {
|
|
5393
|
+
yield { version: errCount, delta };
|
|
5394
|
+
}
|
|
5295
5395
|
}
|
|
5296
5396
|
const lastMemItem = mem.getLast(options?.sessionId);
|
|
5297
5397
|
const shouldContinue = this.shouldContinueSteps(
|
|
@@ -5384,8 +5484,13 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5384
5484
|
...options
|
|
5385
5485
|
});
|
|
5386
5486
|
let buffer = {};
|
|
5387
|
-
|
|
5388
|
-
|
|
5487
|
+
let currentVersion = 0;
|
|
5488
|
+
for await (const item of generator) {
|
|
5489
|
+
if (item.version !== currentVersion) {
|
|
5490
|
+
buffer = {};
|
|
5491
|
+
}
|
|
5492
|
+
currentVersion = item.version;
|
|
5493
|
+
buffer = mergeDeltas(buffer, item.delta);
|
|
5389
5494
|
}
|
|
5390
5495
|
this.trace = { ...values, ...buffer };
|
|
5391
5496
|
return buffer;
|
|
@@ -7297,6 +7402,7 @@ export {
|
|
|
7297
7402
|
AxAI,
|
|
7298
7403
|
AxAIAnthropic,
|
|
7299
7404
|
AxAIAnthropicModel,
|
|
7405
|
+
AxAIAnthropicVertexModel,
|
|
7300
7406
|
AxAIAzureOpenAI,
|
|
7301
7407
|
AxAICohere,
|
|
7302
7408
|
AxAICohereEmbedModel,
|