@ax-llm/ax 11.0.16 → 11.0.17
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 +491 -241
- package/index.cjs.map +1 -1
- package/index.d.cts +134 -18
- package/index.d.ts +134 -18
- package/index.js +493 -243
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -221,28 +221,24 @@ var AxAIServiceError = class extends Error {
|
|
|
221
221
|
this.timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
222
222
|
this.errorId = crypto.randomUUID();
|
|
223
223
|
this.context = context;
|
|
224
|
+
this.stack = this.toString();
|
|
224
225
|
}
|
|
225
226
|
timestamp;
|
|
226
227
|
errorId;
|
|
227
228
|
context;
|
|
228
229
|
toString() {
|
|
229
|
-
return
|
|
230
|
-
|
|
231
|
-
URL: ${this.url}
|
|
232
|
-
Request Body: ${JSON.stringify(this.requestBody, null, 2)}
|
|
233
|
-
Context: ${JSON.stringify(this.context, null, 2)}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
url: this.url,
|
|
242
|
-
requestBody: this.requestBody,
|
|
243
|
-
context: this.context,
|
|
244
|
-
stack: this.stack
|
|
245
|
-
};
|
|
230
|
+
return [
|
|
231
|
+
`${this.name}: ${this.message}`,
|
|
232
|
+
`URL: ${this.url}`,
|
|
233
|
+
`Request Body: ${JSON.stringify(this.requestBody, null, 2)}`,
|
|
234
|
+
`Context: ${JSON.stringify(this.context, null, 2)}`,
|
|
235
|
+
`Timestamp: ${this.timestamp}`,
|
|
236
|
+
`Error ID: ${this.errorId}`
|
|
237
|
+
].join("\n");
|
|
238
|
+
}
|
|
239
|
+
// For Node.js, override the custom inspect method so console.log shows our custom string.
|
|
240
|
+
[Symbol.for("nodejs.util.inspect.custom")](_depth, _options) {
|
|
241
|
+
return this.toString();
|
|
246
242
|
}
|
|
247
243
|
};
|
|
248
244
|
var AxAIServiceStatusError = class extends AxAIServiceError {
|
|
@@ -560,6 +556,37 @@ var apiCall = async (api, json) => {
|
|
|
560
556
|
}
|
|
561
557
|
};
|
|
562
558
|
|
|
559
|
+
// util/transform.ts
|
|
560
|
+
import {
|
|
561
|
+
TransformStream as TransformStream4
|
|
562
|
+
} from "stream/web";
|
|
563
|
+
var TypeTransformer = class {
|
|
564
|
+
buffer;
|
|
565
|
+
doneCallback;
|
|
566
|
+
transformFn;
|
|
567
|
+
constructor(transformFn, doneCallback) {
|
|
568
|
+
this.transformFn = transformFn;
|
|
569
|
+
this.doneCallback = doneCallback;
|
|
570
|
+
this.buffer = doneCallback ? [] : void 0;
|
|
571
|
+
}
|
|
572
|
+
async transform(obj, controller) {
|
|
573
|
+
const val = this.transformFn(obj);
|
|
574
|
+
if (val) {
|
|
575
|
+
controller.enqueue(val);
|
|
576
|
+
this.buffer?.push(val);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
async flush(controller) {
|
|
580
|
+
await this.doneCallback?.(this.buffer ?? []);
|
|
581
|
+
controller.terminate();
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
var RespTransformStream = class extends TransformStream4 {
|
|
585
|
+
constructor(transformFn, doneCallback) {
|
|
586
|
+
super(new TypeTransformer(transformFn, doneCallback));
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
|
|
563
590
|
// util/log.ts
|
|
564
591
|
var ColorLog = class {
|
|
565
592
|
// ANSI escape codes for different colors
|
|
@@ -591,44 +618,106 @@ var ColorLog = class {
|
|
|
591
618
|
}
|
|
592
619
|
};
|
|
593
620
|
|
|
594
|
-
//
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
621
|
+
// ai/debug.ts
|
|
622
|
+
var colorLog = new ColorLog();
|
|
623
|
+
var formatChatMessage = (msg, hideContent) => {
|
|
624
|
+
switch (msg.role) {
|
|
625
|
+
case "system":
|
|
626
|
+
return `
|
|
627
|
+
${colorLog.blueBright("System:")}
|
|
628
|
+
${colorLog.whiteBright(msg.content)}`;
|
|
629
|
+
case "function":
|
|
630
|
+
return `
|
|
631
|
+
${colorLog.blueBright("Function Result:")}
|
|
632
|
+
${colorLog.whiteBright(msg.result)}`;
|
|
633
|
+
case "user": {
|
|
634
|
+
if (typeof msg.content === "string") {
|
|
635
|
+
return `
|
|
636
|
+
${colorLog.blueBright("User:")}
|
|
637
|
+
${colorLog.whiteBright(msg.content)}`;
|
|
638
|
+
}
|
|
639
|
+
const items = msg.content.map((v) => {
|
|
640
|
+
switch (v.type) {
|
|
641
|
+
case "text":
|
|
642
|
+
return `${colorLog.whiteBright(v.text)}`;
|
|
643
|
+
case "image":
|
|
644
|
+
return `(Image, ${v.mimeType}) ${colorLog.whiteBright(v.image.substring(0, 10))}`;
|
|
645
|
+
default:
|
|
646
|
+
throw new Error("Invalid content type");
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
return `
|
|
650
|
+
${colorLog.blueBright("User:")}
|
|
651
|
+
${items.join("\n")}`;
|
|
652
|
+
}
|
|
653
|
+
case "assistant": {
|
|
654
|
+
if (msg.functionCalls) {
|
|
655
|
+
const fns = msg.functionCalls?.map(({ function: fn }) => {
|
|
656
|
+
const args = typeof fn.params !== "string" ? JSON.stringify(fn.params, null, 2) : fn.params;
|
|
657
|
+
return `${fn.name}(${args})`;
|
|
658
|
+
});
|
|
659
|
+
return `
|
|
660
|
+
${colorLog.blueBright("\nFunctions:")}
|
|
661
|
+
${colorLog.whiteBright(fns.join("\n"))}`;
|
|
662
|
+
}
|
|
663
|
+
return `
|
|
664
|
+
${colorLog.blueBright("\nAssistant:")}
|
|
665
|
+
${hideContent ? "" : colorLog.whiteBright(msg.content ?? "<empty>")}`;
|
|
612
666
|
}
|
|
667
|
+
default:
|
|
668
|
+
throw new Error("Invalid role");
|
|
613
669
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
670
|
+
};
|
|
671
|
+
var logChatRequestMessage = (msg) => {
|
|
672
|
+
process.stdout.write(formatChatMessage(msg) + "\n");
|
|
673
|
+
process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
|
|
674
|
+
};
|
|
675
|
+
var logChatRequest = (chatPrompt) => {
|
|
676
|
+
const items = chatPrompt?.map((msg) => formatChatMessage(msg));
|
|
677
|
+
if (items) {
|
|
678
|
+
process.stdout.write(items.join("\n"));
|
|
679
|
+
process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
|
|
617
680
|
}
|
|
618
681
|
};
|
|
619
|
-
var
|
|
620
|
-
|
|
621
|
-
|
|
682
|
+
var logResponseResult = (r) => {
|
|
683
|
+
if (r.content) {
|
|
684
|
+
process.stdout.write(colorLog.greenBright(r.content));
|
|
685
|
+
}
|
|
686
|
+
if (r.functionCalls) {
|
|
687
|
+
for (const [i, f] of r.functionCalls.entries()) {
|
|
688
|
+
if (f.function.name) {
|
|
689
|
+
if (i > 0) {
|
|
690
|
+
process.stdout.write("\n");
|
|
691
|
+
}
|
|
692
|
+
process.stdout.write(
|
|
693
|
+
`Function ${i + 1} -> ${colorLog.greenBright(f.function.name)}`
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
if (f.function.params) {
|
|
697
|
+
const params = typeof f.function.params === "string" ? f.function.params : JSON.stringify(f.function.params, null, 2);
|
|
698
|
+
process.stdout.write(`${colorLog.greenBright(params)}`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
622
701
|
}
|
|
623
702
|
};
|
|
703
|
+
var logResponse = (resp) => {
|
|
704
|
+
if (!resp.results) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
for (const r of resp.results) {
|
|
708
|
+
logResponseResult(r);
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
var logResponseDelta = (delta) => {
|
|
712
|
+
process.stdout.write(colorLog.greenBright(delta));
|
|
713
|
+
};
|
|
624
714
|
|
|
625
715
|
// ai/base.ts
|
|
626
|
-
var colorLog = new ColorLog();
|
|
627
716
|
var axBaseAIDefaultConfig = () => structuredClone({
|
|
628
717
|
maxTokens: 2e3,
|
|
629
718
|
temperature: 0,
|
|
630
719
|
topK: 40,
|
|
631
|
-
|
|
720
|
+
topP: 0.9
|
|
632
721
|
});
|
|
633
722
|
var AxBaseAI = class {
|
|
634
723
|
constructor(aiImpl, {
|
|
@@ -908,8 +997,8 @@ var AxBaseAI = class {
|
|
|
908
997
|
);
|
|
909
998
|
return res2;
|
|
910
999
|
};
|
|
911
|
-
if (this.debug) {
|
|
912
|
-
logChatRequest(req);
|
|
1000
|
+
if (options?.debug ?? this.debug) {
|
|
1001
|
+
logChatRequest(req["chatPrompt"]);
|
|
913
1002
|
}
|
|
914
1003
|
const rt = options?.rateLimiter ?? this.rt;
|
|
915
1004
|
const rv = rt ? await rt(fn, { modelUsage: this.modelUsage }) : await fn();
|
|
@@ -927,13 +1016,13 @@ var AxBaseAI = class {
|
|
|
927
1016
|
if (span?.isRecording()) {
|
|
928
1017
|
setResponseAttr(res2, span);
|
|
929
1018
|
}
|
|
930
|
-
if (this.debug) {
|
|
1019
|
+
if (options?.debug ?? this.debug) {
|
|
931
1020
|
logResponse(res2);
|
|
932
1021
|
}
|
|
933
1022
|
return res2;
|
|
934
1023
|
};
|
|
935
1024
|
const doneCb = async (_values) => {
|
|
936
|
-
if (this.debug) {
|
|
1025
|
+
if (options?.debug ?? this.debug) {
|
|
937
1026
|
process.stdout.write("\n");
|
|
938
1027
|
}
|
|
939
1028
|
};
|
|
@@ -956,7 +1045,7 @@ var AxBaseAI = class {
|
|
|
956
1045
|
if (span?.isRecording()) {
|
|
957
1046
|
setResponseAttr(res, span);
|
|
958
1047
|
}
|
|
959
|
-
if (this.debug) {
|
|
1048
|
+
if (options?.debug ?? this.debug) {
|
|
960
1049
|
logResponse(res);
|
|
961
1050
|
}
|
|
962
1051
|
span?.end();
|
|
@@ -1047,83 +1136,6 @@ var AxBaseAI = class {
|
|
|
1047
1136
|
return { ...headers, ...await this.headers() };
|
|
1048
1137
|
}
|
|
1049
1138
|
};
|
|
1050
|
-
var logChatRequest = (req) => {
|
|
1051
|
-
const hasAssistant = req.chatPrompt?.some((msg) => msg.role === "assistant");
|
|
1052
|
-
const items = req.chatPrompt?.map((msg) => {
|
|
1053
|
-
if (hasAssistant && msg.role === "system") {
|
|
1054
|
-
return "";
|
|
1055
|
-
}
|
|
1056
|
-
switch (msg.role) {
|
|
1057
|
-
case "system":
|
|
1058
|
-
return `${colorLog.blueBright("System:")}
|
|
1059
|
-
${colorLog.whiteBright(msg.content)}`;
|
|
1060
|
-
case "function":
|
|
1061
|
-
return `${colorLog.blueBright("Function Result:")}
|
|
1062
|
-
${colorLog.whiteBright(msg.result)}`;
|
|
1063
|
-
case "user": {
|
|
1064
|
-
if (typeof msg.content === "string") {
|
|
1065
|
-
return `${colorLog.blueBright("User:")}
|
|
1066
|
-
${colorLog.whiteBright(msg.content)}`;
|
|
1067
|
-
}
|
|
1068
|
-
const items2 = msg.content.map((v) => {
|
|
1069
|
-
switch (v.type) {
|
|
1070
|
-
case "text":
|
|
1071
|
-
return `${colorLog.whiteBright(v.text)}`;
|
|
1072
|
-
case "image":
|
|
1073
|
-
return `(Image, ${v.mimeType}) ${colorLog.whiteBright(v.image.substring(0, 10))}`;
|
|
1074
|
-
default:
|
|
1075
|
-
throw new Error("Invalid content type");
|
|
1076
|
-
}
|
|
1077
|
-
});
|
|
1078
|
-
return `${colorLog.blueBright("User:")}
|
|
1079
|
-
${items2.join("\n")}`;
|
|
1080
|
-
}
|
|
1081
|
-
case "assistant": {
|
|
1082
|
-
if (msg.functionCalls) {
|
|
1083
|
-
const fns = msg.functionCalls?.map(({ function: fn }) => {
|
|
1084
|
-
const args = typeof fn.params !== "string" ? JSON.stringify(fn.params, null, 2) : fn.params;
|
|
1085
|
-
return `${fn.name}(${args})`;
|
|
1086
|
-
});
|
|
1087
|
-
return `${colorLog.blueBright("\nFunctions:")}
|
|
1088
|
-
${colorLog.whiteBright(fns.join("\n"))}`;
|
|
1089
|
-
}
|
|
1090
|
-
return `${colorLog.blueBright("\nAssistant:")}
|
|
1091
|
-
${colorLog.whiteBright(msg.content ?? "<empty>")}`;
|
|
1092
|
-
}
|
|
1093
|
-
default:
|
|
1094
|
-
throw new Error("Invalid role");
|
|
1095
|
-
}
|
|
1096
|
-
});
|
|
1097
|
-
if (items) {
|
|
1098
|
-
process.stdout.write("\n===\n" + items.join("\n") + "\n\n---\n");
|
|
1099
|
-
}
|
|
1100
|
-
};
|
|
1101
|
-
var logResponse = (resp) => {
|
|
1102
|
-
if (!resp.results) {
|
|
1103
|
-
return;
|
|
1104
|
-
}
|
|
1105
|
-
for (const r of resp.results) {
|
|
1106
|
-
if (r.content) {
|
|
1107
|
-
process.stdout.write(colorLog.greenBright(r.content));
|
|
1108
|
-
}
|
|
1109
|
-
if (r.functionCalls) {
|
|
1110
|
-
for (const [i, f] of r.functionCalls.entries()) {
|
|
1111
|
-
if (f.function.name) {
|
|
1112
|
-
if (i > 0) {
|
|
1113
|
-
process.stdout.write("\n\n");
|
|
1114
|
-
}
|
|
1115
|
-
process.stdout.write(
|
|
1116
|
-
`Function ${i + 1} -> ${colorLog.greenBright(f.function.name)} `
|
|
1117
|
-
);
|
|
1118
|
-
}
|
|
1119
|
-
if (f.function.params) {
|
|
1120
|
-
const params = typeof f.function.params === "string" ? f.function.params : JSON.stringify(f.function.params, null, 2);
|
|
1121
|
-
process.stdout.write(`${colorLog.greenBright(params)}`);
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
};
|
|
1127
1139
|
var setResponseAttr = (res, span) => {
|
|
1128
1140
|
if (res.modelUsage) {
|
|
1129
1141
|
span.setAttributes({
|
|
@@ -2747,8 +2759,10 @@ var AxAIGoogleGeminiImpl = class {
|
|
|
2747
2759
|
temperature: req.modelConfig?.temperature ?? this.config.temperature,
|
|
2748
2760
|
topP: req.modelConfig?.topP ?? this.config.topP,
|
|
2749
2761
|
topK: req.modelConfig?.topK ?? this.config.topK,
|
|
2762
|
+
frequencyPenalty: req.modelConfig?.frequencyPenalty ?? this.config.frequencyPenalty,
|
|
2750
2763
|
candidateCount: 1,
|
|
2751
|
-
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences
|
|
2764
|
+
stopSequences: req.modelConfig?.stopSequences ?? this.config.stopSequences,
|
|
2765
|
+
responseMimeType: "text/plain"
|
|
2752
2766
|
};
|
|
2753
2767
|
const safetySettings2 = this.config.safetySettings;
|
|
2754
2768
|
const reqValue = {
|
|
@@ -3693,14 +3707,15 @@ function mergeFunctionCalls(functionCalls, functionCallDeltas) {
|
|
|
3693
3707
|
// mem/memory.ts
|
|
3694
3708
|
var defaultLimit = 1e4;
|
|
3695
3709
|
var MemoryImpl = class {
|
|
3696
|
-
constructor(limit = defaultLimit) {
|
|
3710
|
+
constructor(limit = defaultLimit, debug = false) {
|
|
3697
3711
|
this.limit = limit;
|
|
3712
|
+
this.debug = debug;
|
|
3698
3713
|
if (limit <= 0) {
|
|
3699
3714
|
throw Error("argument 'limit' must be greater than 0");
|
|
3700
3715
|
}
|
|
3701
3716
|
}
|
|
3702
3717
|
data = [];
|
|
3703
|
-
|
|
3718
|
+
addMemory(value) {
|
|
3704
3719
|
if (Array.isArray(value)) {
|
|
3705
3720
|
this.data.push(...value.map((chat) => ({ chat: structuredClone(chat) })));
|
|
3706
3721
|
} else {
|
|
@@ -3713,7 +3728,13 @@ var MemoryImpl = class {
|
|
|
3713
3728
|
this.data.splice(0, removeCount);
|
|
3714
3729
|
}
|
|
3715
3730
|
}
|
|
3716
|
-
|
|
3731
|
+
add(value) {
|
|
3732
|
+
this.addMemory(value);
|
|
3733
|
+
if (this.debug) {
|
|
3734
|
+
debugRequest(value);
|
|
3735
|
+
}
|
|
3736
|
+
}
|
|
3737
|
+
addResultMessage({
|
|
3717
3738
|
content,
|
|
3718
3739
|
name,
|
|
3719
3740
|
functionCalls
|
|
@@ -3721,26 +3742,44 @@ var MemoryImpl = class {
|
|
|
3721
3742
|
if (!content && (!functionCalls || functionCalls.length === 0)) {
|
|
3722
3743
|
return;
|
|
3723
3744
|
}
|
|
3724
|
-
this.
|
|
3745
|
+
this.addMemory({ content, name, role: "assistant", functionCalls });
|
|
3725
3746
|
}
|
|
3726
|
-
|
|
3747
|
+
addResult({
|
|
3727
3748
|
content,
|
|
3728
3749
|
name,
|
|
3729
3750
|
functionCalls
|
|
3751
|
+
}) {
|
|
3752
|
+
this.addResultMessage({ content, name, functionCalls });
|
|
3753
|
+
if (this.debug) {
|
|
3754
|
+
debugResponse({ content, name, functionCalls });
|
|
3755
|
+
}
|
|
3756
|
+
}
|
|
3757
|
+
updateResult({
|
|
3758
|
+
content,
|
|
3759
|
+
name,
|
|
3760
|
+
functionCalls,
|
|
3761
|
+
delta
|
|
3730
3762
|
}) {
|
|
3731
3763
|
const lastItem = this.data.at(-1);
|
|
3732
3764
|
if (!lastItem || lastItem.chat.role !== "assistant") {
|
|
3733
|
-
this.
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3765
|
+
this.addResultMessage({ content, name, functionCalls });
|
|
3766
|
+
} else {
|
|
3767
|
+
if ("content" in lastItem.chat && content) {
|
|
3768
|
+
lastItem.chat.content = content;
|
|
3769
|
+
}
|
|
3770
|
+
if ("name" in lastItem.chat && name) {
|
|
3771
|
+
lastItem.chat.name = name;
|
|
3772
|
+
}
|
|
3773
|
+
if ("functionCalls" in lastItem.chat && functionCalls) {
|
|
3774
|
+
lastItem.chat.functionCalls = functionCalls;
|
|
3775
|
+
}
|
|
3741
3776
|
}
|
|
3742
|
-
if (
|
|
3743
|
-
|
|
3777
|
+
if (this.debug) {
|
|
3778
|
+
if (delta) {
|
|
3779
|
+
debugResponseDelta(delta);
|
|
3780
|
+
} else if (lastItem) {
|
|
3781
|
+
debugResponse({ content, name, functionCalls });
|
|
3782
|
+
}
|
|
3744
3783
|
}
|
|
3745
3784
|
}
|
|
3746
3785
|
addTag(name) {
|
|
@@ -3780,16 +3819,21 @@ var MemoryImpl = class {
|
|
|
3780
3819
|
}
|
|
3781
3820
|
getLast() {
|
|
3782
3821
|
const lastItem = this.data.at(-1);
|
|
3783
|
-
return
|
|
3822
|
+
if (!lastItem) return void 0;
|
|
3823
|
+
return {
|
|
3824
|
+
chat: lastItem.chat,
|
|
3825
|
+
tags: lastItem.tags
|
|
3826
|
+
};
|
|
3784
3827
|
}
|
|
3785
3828
|
reset() {
|
|
3786
3829
|
this.data = [];
|
|
3787
3830
|
}
|
|
3788
3831
|
};
|
|
3789
3832
|
var AxMemory = class {
|
|
3790
|
-
constructor(limit = defaultLimit) {
|
|
3833
|
+
constructor(limit = defaultLimit, debug = false) {
|
|
3791
3834
|
this.limit = limit;
|
|
3792
|
-
this.
|
|
3835
|
+
this.debug = debug;
|
|
3836
|
+
this.defaultMemory = new MemoryImpl(limit, debug);
|
|
3793
3837
|
}
|
|
3794
3838
|
memories = /* @__PURE__ */ new Map();
|
|
3795
3839
|
defaultMemory;
|
|
@@ -3831,6 +3875,19 @@ var AxMemory = class {
|
|
|
3831
3875
|
}
|
|
3832
3876
|
}
|
|
3833
3877
|
};
|
|
3878
|
+
function debugRequest(value) {
|
|
3879
|
+
if (Array.isArray(value)) {
|
|
3880
|
+
logChatRequest(value);
|
|
3881
|
+
} else {
|
|
3882
|
+
logChatRequestMessage(value);
|
|
3883
|
+
}
|
|
3884
|
+
}
|
|
3885
|
+
function debugResponse(value) {
|
|
3886
|
+
logResponseResult(value);
|
|
3887
|
+
}
|
|
3888
|
+
function debugResponseDelta(delta) {
|
|
3889
|
+
logResponseDelta(delta);
|
|
3890
|
+
}
|
|
3834
3891
|
|
|
3835
3892
|
// dsp/asserts.ts
|
|
3836
3893
|
var AxAssertionError = class extends Error {
|
|
@@ -3865,7 +3922,7 @@ var assertAssertions = (asserts, values) => {
|
|
|
3865
3922
|
}
|
|
3866
3923
|
}
|
|
3867
3924
|
};
|
|
3868
|
-
var assertStreamingAssertions = (asserts,
|
|
3925
|
+
var assertStreamingAssertions = (asserts, xstate, content, final = false) => {
|
|
3869
3926
|
if (!xstate.currField || xstate.s === -1 || !asserts || asserts.length === 0) {
|
|
3870
3927
|
return;
|
|
3871
3928
|
}
|
|
@@ -4068,7 +4125,8 @@ ${pointer}`;
|
|
|
4068
4125
|
"image",
|
|
4069
4126
|
"audio",
|
|
4070
4127
|
"datetime",
|
|
4071
|
-
"date"
|
|
4128
|
+
"date",
|
|
4129
|
+
"code"
|
|
4072
4130
|
];
|
|
4073
4131
|
const foundType = types.find((type) => this.match(type));
|
|
4074
4132
|
if (!foundType) {
|
|
@@ -4375,6 +4433,10 @@ var validateValue = (field, value) => {
|
|
|
4375
4433
|
const ft = field.type ?? { name: "string", isArray: false };
|
|
4376
4434
|
const validateSingleValue = (expectedType, val) => {
|
|
4377
4435
|
switch (expectedType) {
|
|
4436
|
+
case "code":
|
|
4437
|
+
return typeof val === "string";
|
|
4438
|
+
case "json":
|
|
4439
|
+
return typeof val === "object";
|
|
4378
4440
|
case "string":
|
|
4379
4441
|
return typeof val === "string";
|
|
4380
4442
|
case "number":
|
|
@@ -4546,7 +4608,10 @@ var LRUCache = class {
|
|
|
4546
4608
|
};
|
|
4547
4609
|
var globalPrefixCache = new LRUCache(500);
|
|
4548
4610
|
function matchesContent(content, prefix, startIndex = 0, prefixCache = globalPrefixCache) {
|
|
4549
|
-
if (
|
|
4611
|
+
if (/^```[a-zA-Z]*\s*$/.test(content)) {
|
|
4612
|
+
return -4;
|
|
4613
|
+
}
|
|
4614
|
+
if (/^[\s`]*$/.test(content)) {
|
|
4550
4615
|
return -3;
|
|
4551
4616
|
}
|
|
4552
4617
|
const exactMatchIndex = content.indexOf(prefix, startIndex);
|
|
@@ -4758,12 +4823,11 @@ var functionCallInstructions = `
|
|
|
4758
4823
|
- Call functions step-by-step, using the output of one function as input to the next.
|
|
4759
4824
|
- Use the function results to generate the output fields.`;
|
|
4760
4825
|
var formattingRules = `
|
|
4761
|
-
## Output Formatting Rules
|
|
4762
|
-
- Output must strictly follow the defined plain-text \`
|
|
4763
|
-
-
|
|
4764
|
-
-
|
|
4765
|
-
- Do not
|
|
4766
|
-
- Do not use JSON to format the output.`;
|
|
4826
|
+
## Strict Output Formatting Rules
|
|
4827
|
+
- Output must strictly follow the defined plain-text \`field name: value\` field format.
|
|
4828
|
+
- Output field, values must strictly adhere to the specified output field formatting rules.
|
|
4829
|
+
- Do not add any text before or after the output fields, just the field name and value.
|
|
4830
|
+
- Do not use code blocks.`;
|
|
4767
4831
|
var AxPromptTemplate = class {
|
|
4768
4832
|
sig;
|
|
4769
4833
|
fieldTemplates;
|
|
@@ -4772,25 +4836,23 @@ var AxPromptTemplate = class {
|
|
|
4772
4836
|
this.sig = sig;
|
|
4773
4837
|
this.fieldTemplates = fieldTemplates;
|
|
4774
4838
|
const task = [];
|
|
4775
|
-
const inArgs =
|
|
4776
|
-
const outArgs =
|
|
4839
|
+
const inArgs = renderDescFields(this.sig.getInputFields());
|
|
4840
|
+
const outArgs = renderDescFields(this.sig.getOutputFields());
|
|
4777
4841
|
task.push(
|
|
4778
4842
|
`You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
|
|
4779
4843
|
);
|
|
4780
4844
|
const funcs = functions?.map(
|
|
4781
4845
|
(f) => "toFunction" in f ? f.toFunction() : f
|
|
4782
4846
|
);
|
|
4783
|
-
const funcList = funcs?.map(
|
|
4784
|
-
(fn) => `- \`${fn.name}\`: ${capitalizeFirstLetter(fn.description)}.`
|
|
4785
|
-
).join("\n");
|
|
4847
|
+
const funcList = funcs?.map((fn) => `- \`${fn.name}\`: ${formatDescription(fn.description)}`).join("\n");
|
|
4786
4848
|
if (funcList && funcList.length > 0) {
|
|
4787
4849
|
task.push(`## Available Functions
|
|
4788
4850
|
${funcList}`);
|
|
4789
4851
|
}
|
|
4790
|
-
const inputFields =
|
|
4852
|
+
const inputFields = renderInputFields(this.sig.getInputFields());
|
|
4791
4853
|
task.push(`## Input Fields
|
|
4792
4854
|
${inputFields}`);
|
|
4793
|
-
const outputFields =
|
|
4855
|
+
const outputFields = renderOutputFields(this.sig.getOutputFields());
|
|
4794
4856
|
task.push(`## Output Fields
|
|
4795
4857
|
${outputFields}`);
|
|
4796
4858
|
if (funcList && funcList.length > 0) {
|
|
@@ -4799,10 +4861,8 @@ ${outputFields}`);
|
|
|
4799
4861
|
task.push(formattingRules.trim());
|
|
4800
4862
|
const desc = this.sig.getDescription();
|
|
4801
4863
|
if (desc) {
|
|
4802
|
-
const
|
|
4803
|
-
|
|
4804
|
-
task.push(`---
|
|
4805
|
-
${text}`);
|
|
4864
|
+
const text = formatDescription(desc);
|
|
4865
|
+
task.push(text);
|
|
4806
4866
|
}
|
|
4807
4867
|
this.task = {
|
|
4808
4868
|
type: "text",
|
|
@@ -4825,7 +4885,7 @@ ${text}`);
|
|
|
4825
4885
|
let systemContent = this.task.text;
|
|
4826
4886
|
if (examplesInSystemPrompt) {
|
|
4827
4887
|
const combinedItems = [
|
|
4828
|
-
{ type: "text", text: systemContent
|
|
4888
|
+
{ type: "text", text: systemContent },
|
|
4829
4889
|
...renderedExamples,
|
|
4830
4890
|
...renderedDemos
|
|
4831
4891
|
];
|
|
@@ -5041,17 +5101,27 @@ ${text}`);
|
|
|
5041
5101
|
}
|
|
5042
5102
|
return [{ type: "text", text: text.join("") }];
|
|
5043
5103
|
};
|
|
5044
|
-
|
|
5045
|
-
|
|
5046
|
-
|
|
5047
|
-
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
});
|
|
5053
|
-
|
|
5054
|
-
|
|
5104
|
+
};
|
|
5105
|
+
var renderDescFields = (list) => list.map((v) => `\`${v.title}\``).join(", ");
|
|
5106
|
+
var renderInputFields = (fields) => {
|
|
5107
|
+
const rows = fields.map((field) => {
|
|
5108
|
+
const name = field.title;
|
|
5109
|
+
const type = field.type?.name ? toFieldType(field.type) : "string";
|
|
5110
|
+
const requiredMsg = field.isOptional ? `This optional ${type} field may be omitted` : `A ${type} field`;
|
|
5111
|
+
const description = field.description ? ` ${formatDescription(field.description)}` : "";
|
|
5112
|
+
return `${name}: (${requiredMsg})${description}`.trim();
|
|
5113
|
+
});
|
|
5114
|
+
return rows.join("\n");
|
|
5115
|
+
};
|
|
5116
|
+
var renderOutputFields = (fields) => {
|
|
5117
|
+
const rows = fields.map((field) => {
|
|
5118
|
+
const name = field.title;
|
|
5119
|
+
const type = field.type?.name ? toFieldType(field.type) : "string";
|
|
5120
|
+
const requiredMsg = field.isOptional ? `Only include this ${type} field is it's value is available` : `This ${type} field must be included`;
|
|
5121
|
+
const description = field.description ? ` ${formatDescription(field.description)}` : "";
|
|
5122
|
+
return `${name}: (${requiredMsg})${description}`.trim();
|
|
5123
|
+
});
|
|
5124
|
+
return rows.join("\n");
|
|
5055
5125
|
};
|
|
5056
5126
|
var processValue = (field, value) => {
|
|
5057
5127
|
if (field.type?.name === "date" && value instanceof Date) {
|
|
@@ -5092,6 +5162,8 @@ var toFieldType = (type) => {
|
|
|
5092
5162
|
return "JSON object";
|
|
5093
5163
|
case "class":
|
|
5094
5164
|
return `classification class (allowed classes: ${type.classes?.join(", ")})`;
|
|
5165
|
+
case "code":
|
|
5166
|
+
return "code";
|
|
5095
5167
|
default:
|
|
5096
5168
|
return "string";
|
|
5097
5169
|
}
|
|
@@ -5125,11 +5197,9 @@ var isEmptyValue = (field, value) => {
|
|
|
5125
5197
|
}
|
|
5126
5198
|
return false;
|
|
5127
5199
|
};
|
|
5128
|
-
function
|
|
5129
|
-
|
|
5130
|
-
|
|
5131
|
-
}
|
|
5132
|
-
return `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
|
5200
|
+
function formatDescription(str) {
|
|
5201
|
+
const value = str.trim();
|
|
5202
|
+
return value.length > 0 ? `${value.charAt(0).toUpperCase()}${value.slice(1)}${value.endsWith(".") ? "" : "."}` : "";
|
|
5133
5203
|
}
|
|
5134
5204
|
|
|
5135
5205
|
// dsp/validate.ts
|
|
@@ -5264,7 +5334,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5264
5334
|
let e = matchesContent(content, prefix, xstate.s + 1);
|
|
5265
5335
|
switch (e) {
|
|
5266
5336
|
case -1:
|
|
5267
|
-
if (streamingValidation &&
|
|
5337
|
+
if (streamingValidation && values.length == 0 && !field.isOptional) {
|
|
5268
5338
|
throw new ValidationError({
|
|
5269
5339
|
message: "Required field not found",
|
|
5270
5340
|
fields: [field]
|
|
@@ -5277,6 +5347,10 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5277
5347
|
// Partial match at end, skip and gather more content
|
|
5278
5348
|
case -3:
|
|
5279
5349
|
return true;
|
|
5350
|
+
// String is only whitespace, skip and gather more content
|
|
5351
|
+
case -4:
|
|
5352
|
+
xstate.inBlock = true;
|
|
5353
|
+
return true;
|
|
5280
5354
|
}
|
|
5281
5355
|
let prefixLen = prefix.length;
|
|
5282
5356
|
if (xstate.currField) {
|
|
@@ -5285,6 +5359,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5285
5359
|
if (parsedValue !== void 0) {
|
|
5286
5360
|
values[xstate.currField.name] = parsedValue;
|
|
5287
5361
|
}
|
|
5362
|
+
xstate.lastDelta = val;
|
|
5288
5363
|
}
|
|
5289
5364
|
checkMissingRequiredFields(xstate, values, index);
|
|
5290
5365
|
xstate.s = e + prefixLen;
|
|
@@ -5297,7 +5372,7 @@ var streamingExtractValues = (sig, values, xstate, content, streamingValidation
|
|
|
5297
5372
|
};
|
|
5298
5373
|
var streamingExtractFinalValue = (sig, values, xstate, content) => {
|
|
5299
5374
|
if (xstate.currField) {
|
|
5300
|
-
|
|
5375
|
+
let val = content.substring(xstate.s).trim();
|
|
5301
5376
|
const parsedValue = validateAndParseFieldValue(xstate.currField, val);
|
|
5302
5377
|
if (parsedValue !== void 0) {
|
|
5303
5378
|
values[xstate.currField.name] = parsedValue;
|
|
@@ -5308,6 +5383,8 @@ var streamingExtractFinalValue = (sig, values, xstate, content) => {
|
|
|
5308
5383
|
};
|
|
5309
5384
|
var convertValueToType = (field, val) => {
|
|
5310
5385
|
switch (field.type?.name) {
|
|
5386
|
+
case "code":
|
|
5387
|
+
return extractBlock(val);
|
|
5311
5388
|
case "string":
|
|
5312
5389
|
return val;
|
|
5313
5390
|
case "number": {
|
|
@@ -5346,47 +5423,79 @@ var convertValueToType = (field, val) => {
|
|
|
5346
5423
|
return val;
|
|
5347
5424
|
}
|
|
5348
5425
|
};
|
|
5349
|
-
function
|
|
5426
|
+
function processStreamingDelta(content, xstate) {
|
|
5350
5427
|
if (!xstate.currField) {
|
|
5351
|
-
return;
|
|
5428
|
+
return null;
|
|
5352
5429
|
}
|
|
5353
|
-
const fieldName = xstate.currField
|
|
5430
|
+
const { name: fieldName, type: fieldType } = xstate.currField ?? {};
|
|
5431
|
+
const { isArray: fieldIsArray, name: fieldTypeName } = fieldType ?? {};
|
|
5354
5432
|
if (!xstate.streamedIndex) {
|
|
5355
|
-
xstate.streamedIndex = {
|
|
5356
|
-
}
|
|
5357
|
-
if (
|
|
5358
|
-
if (
|
|
5359
|
-
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
|
|
5366
|
-
xstate.streamedIndex[fieldName] = pos + v1.length;
|
|
5367
|
-
}
|
|
5368
|
-
return;
|
|
5433
|
+
xstate.streamedIndex = {};
|
|
5434
|
+
}
|
|
5435
|
+
if (fieldIsArray) {
|
|
5436
|
+
if (xstate.streamedIndex[fieldName] === void 0) {
|
|
5437
|
+
xstate.streamedIndex[fieldName] = 0;
|
|
5438
|
+
}
|
|
5439
|
+
return null;
|
|
5440
|
+
}
|
|
5441
|
+
if (fieldTypeName !== "string" && fieldTypeName !== "code") {
|
|
5442
|
+
if (xstate.streamedIndex[fieldName] === void 0) {
|
|
5443
|
+
xstate.streamedIndex[fieldName] = 0;
|
|
5369
5444
|
}
|
|
5445
|
+
return null;
|
|
5446
|
+
}
|
|
5447
|
+
const pos = xstate.streamedIndex[fieldName] ?? 0;
|
|
5448
|
+
const s = xstate.s + pos;
|
|
5449
|
+
const isFirstChunk = pos === 0;
|
|
5450
|
+
const d1 = content.substring(s);
|
|
5451
|
+
let d2 = d1.replace(/\s+$/, "");
|
|
5452
|
+
if (xstate.currField?.type?.name === "code") {
|
|
5453
|
+
d2 = d2.replace(/\s*```\s*$/, "");
|
|
5454
|
+
}
|
|
5455
|
+
let d3 = isFirstChunk ? d2.trimStart() : d2;
|
|
5456
|
+
if (xstate.currField?.type?.name === "code") {
|
|
5457
|
+
d3 = d3.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
|
|
5458
|
+
}
|
|
5459
|
+
if (d3.length > 0) {
|
|
5460
|
+
xstate.streamedIndex[fieldName] = pos + d2.length;
|
|
5461
|
+
}
|
|
5462
|
+
return d3;
|
|
5463
|
+
}
|
|
5464
|
+
function getStreamingDelta(content, xstate) {
|
|
5465
|
+
if (xstate.lastDelta) {
|
|
5466
|
+
processStreamingDelta(xstate.lastDelta, xstate);
|
|
5467
|
+
xstate.lastDelta = void 0;
|
|
5468
|
+
}
|
|
5469
|
+
return processStreamingDelta(content, xstate);
|
|
5470
|
+
}
|
|
5471
|
+
function* streamValues(sig, values, xstate, delta) {
|
|
5472
|
+
if (!xstate.currField) {
|
|
5473
|
+
return;
|
|
5474
|
+
}
|
|
5475
|
+
const fieldName = xstate.currField.name;
|
|
5476
|
+
if (delta && delta.length > 0) {
|
|
5477
|
+
yield { [fieldName]: delta };
|
|
5478
|
+
return;
|
|
5370
5479
|
}
|
|
5371
5480
|
for (const key of Object.keys(values)) {
|
|
5372
5481
|
const value = values[key];
|
|
5373
5482
|
if (Array.isArray(value)) {
|
|
5374
|
-
|
|
5483
|
+
if (xstate.streamedIndex?.[key] === void 0) {
|
|
5484
|
+
throw new Error("Streamed index is not set for array field " + key);
|
|
5485
|
+
}
|
|
5486
|
+
const s = xstate.streamedIndex[key];
|
|
5375
5487
|
const v = value.slice(s);
|
|
5376
5488
|
if (v && v.length > 0) {
|
|
5377
5489
|
yield { [key]: v };
|
|
5378
5490
|
xstate.streamedIndex[key] = s + 1;
|
|
5379
5491
|
}
|
|
5380
|
-
|
|
5381
|
-
}
|
|
5382
|
-
if (!xstate.streamedIndex[key]) {
|
|
5492
|
+
} else {
|
|
5383
5493
|
yield { [key]: value };
|
|
5384
|
-
xstate.streamedIndex[key] = 1;
|
|
5385
5494
|
}
|
|
5386
5495
|
}
|
|
5387
5496
|
}
|
|
5388
5497
|
function validateAndParseFieldValue(field, fieldValue) {
|
|
5389
|
-
if (!fieldValue || fieldValue === "" ||
|
|
5498
|
+
if (!fieldValue || fieldValue === "" || /^(null|undefined)\s*$/i.test(fieldValue)) {
|
|
5390
5499
|
if (field.isOptional) {
|
|
5391
5500
|
return;
|
|
5392
5501
|
}
|
|
@@ -5452,8 +5561,8 @@ function validateAndParseFieldValue(field, fieldValue) {
|
|
|
5452
5561
|
return value;
|
|
5453
5562
|
}
|
|
5454
5563
|
var extractBlock = (input) => {
|
|
5455
|
-
const
|
|
5456
|
-
const match =
|
|
5564
|
+
const markdownBlockPattern = /```([A-Za-z]*)\n([\s\S]*?)\n```/g;
|
|
5565
|
+
const match = markdownBlockPattern.exec(input);
|
|
5457
5566
|
if (!match) {
|
|
5458
5567
|
return input;
|
|
5459
5568
|
}
|
|
@@ -5466,6 +5575,61 @@ var extractBlock = (input) => {
|
|
|
5466
5575
|
return input;
|
|
5467
5576
|
};
|
|
5468
5577
|
|
|
5578
|
+
// dsp/fieldProcessor.ts
|
|
5579
|
+
async function processFieldProcessors(fieldProcessors, values, mem, sessionId) {
|
|
5580
|
+
for (const processor of fieldProcessors) {
|
|
5581
|
+
if (values[processor.field.name] === void 0) {
|
|
5582
|
+
continue;
|
|
5583
|
+
}
|
|
5584
|
+
const result = await processor.process(values[processor.field.name], {
|
|
5585
|
+
sessionId,
|
|
5586
|
+
values,
|
|
5587
|
+
done: true
|
|
5588
|
+
});
|
|
5589
|
+
addToMemory(processor.field, mem, result, sessionId);
|
|
5590
|
+
}
|
|
5591
|
+
}
|
|
5592
|
+
async function processStreamingFieldProcessors(fieldProcessors, content, xstate, mem, values, sessionId, done = false) {
|
|
5593
|
+
for (const processor of fieldProcessors) {
|
|
5594
|
+
if (xstate.currField?.name !== processor.field.name) {
|
|
5595
|
+
continue;
|
|
5596
|
+
}
|
|
5597
|
+
let value = content.substring(xstate.s);
|
|
5598
|
+
if (xstate.currField?.type?.name === "code") {
|
|
5599
|
+
value = value.replace(/^[ ]*```[a-zA-Z0-9]*\n\s*/, "");
|
|
5600
|
+
value = value.replace(/\s*```\s*$/, "");
|
|
5601
|
+
}
|
|
5602
|
+
const result = await processor.process(value, {
|
|
5603
|
+
sessionId,
|
|
5604
|
+
values,
|
|
5605
|
+
done
|
|
5606
|
+
});
|
|
5607
|
+
addToMemory(xstate.currField, mem, result, sessionId);
|
|
5608
|
+
}
|
|
5609
|
+
}
|
|
5610
|
+
var addToMemory = (field, mem, result, sessionId) => {
|
|
5611
|
+
if (result === void 0 || typeof result === "string" && (result === "" || /^(null|undefined)\s*$/i.test(result))) {
|
|
5612
|
+
return;
|
|
5613
|
+
}
|
|
5614
|
+
let resultText = JSON.stringify(
|
|
5615
|
+
result,
|
|
5616
|
+
(key, value) => typeof value === "bigint" ? Number(value) : value,
|
|
5617
|
+
2
|
|
5618
|
+
);
|
|
5619
|
+
const text = getFieldProcessingMessage(field, resultText);
|
|
5620
|
+
mem.add({ role: "user", content: [{ type: "text", text }] }, sessionId);
|
|
5621
|
+
mem.addTag(`processor`, sessionId);
|
|
5622
|
+
};
|
|
5623
|
+
function getFieldProcessingMessage(field, resultText) {
|
|
5624
|
+
const isCodeField = field.type?.name === "code";
|
|
5625
|
+
const fieldTitle = field.title;
|
|
5626
|
+
if (isCodeField) {
|
|
5627
|
+
return `Code in the field "${fieldTitle}" was executed. The code execution produced the following output: ${resultText}`;
|
|
5628
|
+
} else {
|
|
5629
|
+
return `The field "${fieldTitle}" was processed. The field contents were transformed into the following output: ${resultText}`;
|
|
5630
|
+
}
|
|
5631
|
+
}
|
|
5632
|
+
|
|
5469
5633
|
// dsp/jsonschema.ts
|
|
5470
5634
|
var validateJSONSchema = (schema) => {
|
|
5471
5635
|
const errors = [];
|
|
@@ -5683,6 +5847,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5683
5847
|
options;
|
|
5684
5848
|
functions;
|
|
5685
5849
|
functionsExecuted = /* @__PURE__ */ new Set();
|
|
5850
|
+
fieldProcessors = [];
|
|
5851
|
+
streamingFieldProcessors = [];
|
|
5686
5852
|
constructor(signature, options) {
|
|
5687
5853
|
super(signature, { description: options?.description });
|
|
5688
5854
|
this.options = options;
|
|
@@ -5703,6 +5869,24 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5703
5869
|
addStreamingAssert = (fieldName, fn, message) => {
|
|
5704
5870
|
this.streamingAsserts.push({ fieldName, fn, message });
|
|
5705
5871
|
};
|
|
5872
|
+
addFieldProcessor = (fieldName, fn, streaming = false) => {
|
|
5873
|
+
const field = this.signature.getOutputFields().find((f) => f.name === fieldName);
|
|
5874
|
+
if (!field) {
|
|
5875
|
+
throw new Error(`addFieldProcessor: field ${fieldName} not found`);
|
|
5876
|
+
}
|
|
5877
|
+
if (streaming) {
|
|
5878
|
+
const ft = field.type?.name;
|
|
5879
|
+
const isText = !ft || ft === "string" || ft === "code";
|
|
5880
|
+
if (!isText) {
|
|
5881
|
+
throw new Error(
|
|
5882
|
+
`addFieldProcessor: field ${fieldName} is must be a text field`
|
|
5883
|
+
);
|
|
5884
|
+
}
|
|
5885
|
+
this.streamingFieldProcessors.push({ field, process: fn });
|
|
5886
|
+
} else {
|
|
5887
|
+
this.fieldProcessors.push({ field, process: fn });
|
|
5888
|
+
}
|
|
5889
|
+
};
|
|
5706
5890
|
async forwardSendRequest({
|
|
5707
5891
|
ai,
|
|
5708
5892
|
mem,
|
|
@@ -5735,7 +5919,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5735
5919
|
sessionId,
|
|
5736
5920
|
traceId,
|
|
5737
5921
|
rateLimiter,
|
|
5738
|
-
stream
|
|
5922
|
+
stream,
|
|
5923
|
+
debug: false
|
|
5739
5924
|
}
|
|
5740
5925
|
);
|
|
5741
5926
|
return res;
|
|
@@ -5745,7 +5930,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5745
5930
|
mem,
|
|
5746
5931
|
options
|
|
5747
5932
|
}) {
|
|
5748
|
-
const { sessionId, traceId, model, functions
|
|
5933
|
+
const { sessionId, traceId, model, functions } = options ?? {};
|
|
5934
|
+
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
5749
5935
|
const usageInfo = {
|
|
5750
5936
|
ai: ai.getName(),
|
|
5751
5937
|
model: ai.getModelInfo().name
|
|
@@ -5765,7 +5951,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5765
5951
|
traceId,
|
|
5766
5952
|
sessionId,
|
|
5767
5953
|
functions,
|
|
5768
|
-
|
|
5954
|
+
fastFail
|
|
5769
5955
|
});
|
|
5770
5956
|
} else {
|
|
5771
5957
|
yield await this.processResponse({
|
|
@@ -5789,9 +5975,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5789
5975
|
sessionId,
|
|
5790
5976
|
traceId,
|
|
5791
5977
|
functions,
|
|
5792
|
-
|
|
5978
|
+
fastFail
|
|
5793
5979
|
}) {
|
|
5794
|
-
const streamingValidation =
|
|
5980
|
+
const streamingValidation = fastFail ?? ai.getFeatures().functionCot !== true;
|
|
5795
5981
|
const functionCalls = [];
|
|
5796
5982
|
const values = {};
|
|
5797
5983
|
const xstate = {
|
|
@@ -5810,12 +5996,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5810
5996
|
if (result.functionCalls) {
|
|
5811
5997
|
mergeFunctionCalls(functionCalls, result.functionCalls);
|
|
5812
5998
|
mem.updateResult(
|
|
5813
|
-
{
|
|
5999
|
+
{
|
|
6000
|
+
name: result.name,
|
|
6001
|
+
content,
|
|
6002
|
+
functionCalls,
|
|
6003
|
+
delta: result.functionCalls?.[0]?.function?.params
|
|
6004
|
+
},
|
|
5814
6005
|
sessionId
|
|
5815
6006
|
);
|
|
5816
6007
|
} else if (result.content) {
|
|
5817
6008
|
content += result.content;
|
|
5818
|
-
mem.updateResult(
|
|
6009
|
+
mem.updateResult(
|
|
6010
|
+
{ name: result.name, content, delta: result.content },
|
|
6011
|
+
sessionId
|
|
6012
|
+
);
|
|
5819
6013
|
const skip = streamingExtractValues(
|
|
5820
6014
|
this.signature,
|
|
5821
6015
|
values,
|
|
@@ -5826,15 +6020,20 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5826
6020
|
if (skip) {
|
|
5827
6021
|
continue;
|
|
5828
6022
|
}
|
|
5829
|
-
assertStreamingAssertions(
|
|
5830
|
-
this.streamingAsserts,
|
|
5831
|
-
values,
|
|
5832
|
-
xstate,
|
|
5833
|
-
content,
|
|
5834
|
-
false
|
|
5835
|
-
);
|
|
6023
|
+
assertStreamingAssertions(this.streamingAsserts, xstate, content);
|
|
5836
6024
|
assertAssertions(this.asserts, values);
|
|
5837
|
-
|
|
6025
|
+
if (this.streamingFieldProcessors.length !== 0) {
|
|
6026
|
+
await processStreamingFieldProcessors(
|
|
6027
|
+
this.streamingFieldProcessors,
|
|
6028
|
+
content,
|
|
6029
|
+
xstate,
|
|
6030
|
+
mem,
|
|
6031
|
+
values,
|
|
6032
|
+
sessionId
|
|
6033
|
+
);
|
|
6034
|
+
}
|
|
6035
|
+
const delta = getStreamingDelta(content, xstate);
|
|
6036
|
+
yield* streamValues(this.signature, values, xstate, delta);
|
|
5838
6037
|
}
|
|
5839
6038
|
if (result.finishReason === "length") {
|
|
5840
6039
|
throw new Error("Max tokens reached before completion");
|
|
@@ -5856,15 +6055,29 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5856
6055
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
5857
6056
|
} else {
|
|
5858
6057
|
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
5859
|
-
assertStreamingAssertions(
|
|
5860
|
-
this.streamingAsserts,
|
|
5861
|
-
values,
|
|
5862
|
-
xstate,
|
|
5863
|
-
content,
|
|
5864
|
-
true
|
|
5865
|
-
);
|
|
6058
|
+
assertStreamingAssertions(this.streamingAsserts, xstate, content, true);
|
|
5866
6059
|
assertAssertions(this.asserts, values);
|
|
5867
|
-
|
|
6060
|
+
if (this.fieldProcessors.length) {
|
|
6061
|
+
await processFieldProcessors(
|
|
6062
|
+
this.fieldProcessors,
|
|
6063
|
+
values,
|
|
6064
|
+
mem,
|
|
6065
|
+
sessionId
|
|
6066
|
+
);
|
|
6067
|
+
}
|
|
6068
|
+
if (this.streamingFieldProcessors.length !== 0) {
|
|
6069
|
+
await processStreamingFieldProcessors(
|
|
6070
|
+
this.streamingFieldProcessors,
|
|
6071
|
+
content,
|
|
6072
|
+
xstate,
|
|
6073
|
+
mem,
|
|
6074
|
+
values,
|
|
6075
|
+
sessionId,
|
|
6076
|
+
true
|
|
6077
|
+
);
|
|
6078
|
+
}
|
|
6079
|
+
const delta = getStreamingDelta(content, xstate);
|
|
6080
|
+
yield* streamValues(this.signature, values, xstate, delta);
|
|
5868
6081
|
}
|
|
5869
6082
|
}
|
|
5870
6083
|
async processResponse({
|
|
@@ -5905,6 +6118,14 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5905
6118
|
} else if (result.content) {
|
|
5906
6119
|
extractValues(this.signature, values, result.content);
|
|
5907
6120
|
assertAssertions(this.asserts, values);
|
|
6121
|
+
if (this.fieldProcessors.length) {
|
|
6122
|
+
await processFieldProcessors(
|
|
6123
|
+
this.fieldProcessors,
|
|
6124
|
+
values,
|
|
6125
|
+
mem,
|
|
6126
|
+
sessionId
|
|
6127
|
+
);
|
|
6128
|
+
}
|
|
5908
6129
|
}
|
|
5909
6130
|
if (result.finishReason === "length") {
|
|
5910
6131
|
throw new Error("Max tokens reached before completion");
|
|
@@ -5916,7 +6137,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5916
6137
|
const stopFunction = (options?.stopFunction ?? this.options?.stopFunction)?.toLowerCase();
|
|
5917
6138
|
const maxRetries = options.maxRetries ?? this.options?.maxRetries ?? 10;
|
|
5918
6139
|
const maxSteps = options.maxSteps ?? this.options?.maxSteps ?? 10;
|
|
5919
|
-
const
|
|
6140
|
+
const debug = options.debug ?? ai.getOptions().debug;
|
|
6141
|
+
const mem = options.mem ?? this.options?.mem ?? new AxMemory(1e4, debug);
|
|
5920
6142
|
let err;
|
|
5921
6143
|
if (options?.functions && options.functions.length > 0) {
|
|
5922
6144
|
const promptTemplate = this.options?.promptTemplate ?? AxPromptTemplate;
|
|
@@ -5947,6 +6169,9 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5947
6169
|
if (shouldContinue) {
|
|
5948
6170
|
continue multiStepLoop;
|
|
5949
6171
|
}
|
|
6172
|
+
if (debug) {
|
|
6173
|
+
process.stdout.write("\n");
|
|
6174
|
+
}
|
|
5950
6175
|
return;
|
|
5951
6176
|
} catch (e) {
|
|
5952
6177
|
let errorFields;
|
|
@@ -5979,10 +6204,12 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5979
6204
|
}
|
|
5980
6205
|
shouldContinueSteps(lastMemItem, stopFunction) {
|
|
5981
6206
|
const stopFunctionExecuted = stopFunction && this.functionsExecuted.has(stopFunction);
|
|
5982
|
-
|
|
6207
|
+
const isFunction = lastMemItem?.chat?.role === "function";
|
|
6208
|
+
const isProcessor = lastMemItem?.tags ? lastMemItem.tags.some((tag) => tag === "processor") : false;
|
|
6209
|
+
if (isFunction && stopFunction && stopFunctionExecuted) {
|
|
5983
6210
|
return false;
|
|
5984
6211
|
}
|
|
5985
|
-
if (
|
|
6212
|
+
if (isFunction || isProcessor) {
|
|
5986
6213
|
return true;
|
|
5987
6214
|
}
|
|
5988
6215
|
return false;
|
|
@@ -6062,17 +6289,18 @@ function processChildAgentFunction(childFunction, parentValues, parentInputKeys,
|
|
|
6062
6289
|
injectionKeys
|
|
6063
6290
|
);
|
|
6064
6291
|
const originalFunc = processedFunction.func;
|
|
6065
|
-
processedFunction.func = (childArgs, funcOptions) => {
|
|
6292
|
+
processedFunction.func = async (childArgs, funcOptions) => {
|
|
6066
6293
|
const updatedChildArgs = {
|
|
6067
6294
|
...childArgs,
|
|
6068
6295
|
...pick(parentValues, injectionKeys)
|
|
6069
6296
|
};
|
|
6070
6297
|
if (options.debug && injectionKeys.length > 0) {
|
|
6071
6298
|
process.stdout.write(
|
|
6072
|
-
`
|
|
6299
|
+
`
|
|
6300
|
+
Function Params: ${JSON.stringify(updatedChildArgs, null, 2)}`
|
|
6073
6301
|
);
|
|
6074
6302
|
}
|
|
6075
|
-
return originalFunc(updatedChildArgs, funcOptions);
|
|
6303
|
+
return await originalFunc(updatedChildArgs, funcOptions);
|
|
6076
6304
|
};
|
|
6077
6305
|
}
|
|
6078
6306
|
return processedFunction;
|
|
@@ -6098,6 +6326,7 @@ var AxAgent = class {
|
|
|
6098
6326
|
agents;
|
|
6099
6327
|
disableSmartModelRouting;
|
|
6100
6328
|
excludeFieldsFromPassthrough;
|
|
6329
|
+
debug;
|
|
6101
6330
|
name;
|
|
6102
6331
|
// private subAgentList?: string
|
|
6103
6332
|
func;
|
|
@@ -6110,12 +6339,13 @@ var AxAgent = class {
|
|
|
6110
6339
|
agents,
|
|
6111
6340
|
functions
|
|
6112
6341
|
}, options) {
|
|
6113
|
-
const { disableSmartModelRouting, excludeFieldsFromPassthrough } = options ?? {};
|
|
6342
|
+
const { disableSmartModelRouting, excludeFieldsFromPassthrough, debug } = options ?? {};
|
|
6114
6343
|
this.ai = ai;
|
|
6115
6344
|
this.agents = agents;
|
|
6116
6345
|
this.functions = functions;
|
|
6117
6346
|
this.disableSmartModelRouting = disableSmartModelRouting;
|
|
6118
6347
|
this.excludeFieldsFromPassthrough = excludeFieldsFromPassthrough ?? [];
|
|
6348
|
+
this.debug = debug;
|
|
6119
6349
|
if (!name || name.length < 5) {
|
|
6120
6350
|
throw new Error(
|
|
6121
6351
|
`Agent name must be at least 10 characters (more descriptive)`
|
|
@@ -6175,10 +6405,21 @@ var AxAgent = class {
|
|
|
6175
6405
|
if (!ai) {
|
|
6176
6406
|
throw new Error("AI service is required to run the agent");
|
|
6177
6407
|
}
|
|
6408
|
+
const debug = this.getDebug(ai, options);
|
|
6409
|
+
if (debug) {
|
|
6410
|
+
process.stdout.write(`
|
|
6411
|
+
--- Agent Engaged: ${this.name} ---
|
|
6412
|
+
`);
|
|
6413
|
+
}
|
|
6178
6414
|
const ret = await boundFunc(ai, values, {
|
|
6179
6415
|
...options,
|
|
6180
6416
|
model
|
|
6181
6417
|
});
|
|
6418
|
+
if (debug) {
|
|
6419
|
+
process.stdout.write(`
|
|
6420
|
+
--- Agent Done: ${this.name} ---
|
|
6421
|
+
`);
|
|
6422
|
+
}
|
|
6182
6423
|
const sig = this.program.getSignature();
|
|
6183
6424
|
const outFields = sig.getOutputFields();
|
|
6184
6425
|
const result = Object.keys(ret).map((k) => {
|
|
@@ -6209,10 +6450,11 @@ var AxAgent = class {
|
|
|
6209
6450
|
const mm = ai?.getModelList();
|
|
6210
6451
|
const parentSchema = this.program.getSignature().getInputFields();
|
|
6211
6452
|
const parentKeys = parentSchema.map((p) => p.name);
|
|
6453
|
+
const debug = this.getDebug(ai, options);
|
|
6212
6454
|
const agentFuncs = this.agents?.map((agent) => {
|
|
6213
6455
|
const f = agent.getFeatures();
|
|
6214
6456
|
const processOptions = {
|
|
6215
|
-
debug
|
|
6457
|
+
debug,
|
|
6216
6458
|
disableSmartModelRouting: !!this.disableSmartModelRouting,
|
|
6217
6459
|
excludeFieldsFromPassthrough: f.excludeFieldsFromPassthrough,
|
|
6218
6460
|
canConfigureSmartModelRouting: f.canConfigureSmartModelRouting
|
|
@@ -6229,16 +6471,21 @@ var AxAgent = class {
|
|
|
6229
6471
|
...options?.functions ?? this.functions ?? [],
|
|
6230
6472
|
...agentFuncs ?? []
|
|
6231
6473
|
];
|
|
6232
|
-
return { ai, functions };
|
|
6474
|
+
return { ai, functions, debug };
|
|
6233
6475
|
}
|
|
6234
6476
|
async forward(parentAi, values, options) {
|
|
6235
|
-
const { ai, functions } = this.init(parentAi, values, options);
|
|
6236
|
-
return await this.program.forward(ai, values, {
|
|
6477
|
+
const { ai, functions, debug } = this.init(parentAi, values, options);
|
|
6478
|
+
return await this.program.forward(ai, values, {
|
|
6479
|
+
...options,
|
|
6480
|
+
debug,
|
|
6481
|
+
functions
|
|
6482
|
+
});
|
|
6237
6483
|
}
|
|
6238
6484
|
async *streamingForward(parentAi, values, options) {
|
|
6239
|
-
const { ai, functions } = this.init(parentAi, values, options);
|
|
6485
|
+
const { ai, functions, debug } = this.init(parentAi, values, options);
|
|
6240
6486
|
return yield* this.program.streamingForward(ai, values, {
|
|
6241
6487
|
...options,
|
|
6488
|
+
debug,
|
|
6242
6489
|
functions
|
|
6243
6490
|
});
|
|
6244
6491
|
}
|
|
@@ -6262,6 +6509,9 @@ var AxAgent = class {
|
|
|
6262
6509
|
}
|
|
6263
6510
|
this.program.getSignature().setDescription(definition);
|
|
6264
6511
|
}
|
|
6512
|
+
getDebug(ai, options) {
|
|
6513
|
+
return options?.debug ?? this.debug ?? ai?.getOptions()?.debug ?? false;
|
|
6514
|
+
}
|
|
6265
6515
|
};
|
|
6266
6516
|
function toCamelCase(inputString) {
|
|
6267
6517
|
const words = inputString.split(/[^a-zA-Z0-9]/);
|