@ax-llm/ax 11.0.23 → 11.0.26
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 +381 -43
- package/index.cjs.map +1 -1
- package/index.d.cts +270 -141
- package/index.d.ts +270 -141
- package/index.js +376 -41
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -649,7 +649,8 @@ ${hideContent ? "" : colorLog.whiteBright(msg.content ?? "<empty>")}`;
|
|
|
649
649
|
}
|
|
650
650
|
};
|
|
651
651
|
var logChatRequestMessage = (msg) => {
|
|
652
|
-
process.stdout.write(formatChatMessage(msg)
|
|
652
|
+
process.stdout.write(`${formatChatMessage(msg)}
|
|
653
|
+
`);
|
|
653
654
|
process.stdout.write(colorLog.blueBright("\nAssistant:\n"));
|
|
654
655
|
};
|
|
655
656
|
var logChatRequest = (chatPrompt) => {
|
|
@@ -967,7 +968,7 @@ var AxBaseAI = class {
|
|
|
967
968
|
return res2;
|
|
968
969
|
};
|
|
969
970
|
if (options?.debug ?? this.debug) {
|
|
970
|
-
logChatRequest(req
|
|
971
|
+
logChatRequest(req.chatPrompt);
|
|
971
972
|
}
|
|
972
973
|
const rt = options?.rateLimiter ?? this.rt;
|
|
973
974
|
const rv = rt ? await rt(fn, { modelUsage: this.modelUsage }) : await fn();
|
|
@@ -1633,6 +1634,7 @@ var AxAIOpenAIModel = /* @__PURE__ */ ((AxAIOpenAIModel2) => {
|
|
|
1633
1634
|
AxAIOpenAIModel2["O1Mini"] = "o1-mini";
|
|
1634
1635
|
AxAIOpenAIModel2["O3Mini"] = "o3-mini";
|
|
1635
1636
|
AxAIOpenAIModel2["GPT4"] = "gpt-4";
|
|
1637
|
+
AxAIOpenAIModel2["GPT45"] = "gpt-4.5-preview";
|
|
1636
1638
|
AxAIOpenAIModel2["GPT4O"] = "gpt-4o";
|
|
1637
1639
|
AxAIOpenAIModel2["GPT4OMini"] = "gpt-4o-mini";
|
|
1638
1640
|
AxAIOpenAIModel2["GPT4ChatGPT4O"] = "chatgpt-4o-latest";
|
|
@@ -1653,6 +1655,12 @@ var AxAIOpenAIEmbedModel = /* @__PURE__ */ ((AxAIOpenAIEmbedModel2) => {
|
|
|
1653
1655
|
|
|
1654
1656
|
// ai/openai/info.ts
|
|
1655
1657
|
var axModelInfoOpenAI = [
|
|
1658
|
+
{
|
|
1659
|
+
name: "gpt-4.5-preview" /* GPT45 */,
|
|
1660
|
+
currency: "usd",
|
|
1661
|
+
promptTokenCostPer1M: 75,
|
|
1662
|
+
completionTokenCostPer1M: 150
|
|
1663
|
+
},
|
|
1656
1664
|
{
|
|
1657
1665
|
name: "o1" /* O1 */,
|
|
1658
1666
|
currency: "usd",
|
|
@@ -3660,7 +3668,7 @@ var AxAI = class {
|
|
|
3660
3668
|
};
|
|
3661
3669
|
|
|
3662
3670
|
// dsp/generate.ts
|
|
3663
|
-
import { ReadableStream as ReadableStream2 } from "stream/web";
|
|
3671
|
+
import { ReadableStream as ReadableStream2 } from "node:stream/web";
|
|
3664
3672
|
import { SpanKind as SpanKind2 } from "@opentelemetry/api";
|
|
3665
3673
|
|
|
3666
3674
|
// ai/util.ts
|
|
@@ -3754,7 +3762,7 @@ var MemoryImpl = class {
|
|
|
3754
3762
|
}
|
|
3755
3763
|
}
|
|
3756
3764
|
if (this.debug) {
|
|
3757
|
-
if (delta) {
|
|
3765
|
+
if (delta && typeof delta === "string") {
|
|
3758
3766
|
debugResponseDelta(delta);
|
|
3759
3767
|
} else if (lastItem) {
|
|
3760
3768
|
debugResponse({ content, name, functionCalls });
|
|
@@ -3887,10 +3895,10 @@ var AxAssertionError = class extends Error {
|
|
|
3887
3895
|
return extraFields;
|
|
3888
3896
|
};
|
|
3889
3897
|
};
|
|
3890
|
-
var assertAssertions = (asserts, values) => {
|
|
3898
|
+
var assertAssertions = async (asserts, values) => {
|
|
3891
3899
|
for (const assert of asserts) {
|
|
3892
3900
|
const { fn, message } = assert;
|
|
3893
|
-
const res = fn(values);
|
|
3901
|
+
const res = await fn(values);
|
|
3894
3902
|
if (res === void 0) {
|
|
3895
3903
|
continue;
|
|
3896
3904
|
}
|
|
@@ -3902,7 +3910,7 @@ var assertAssertions = (asserts, values) => {
|
|
|
3902
3910
|
}
|
|
3903
3911
|
}
|
|
3904
3912
|
};
|
|
3905
|
-
var assertStreamingAssertions = (asserts, xstate, content, final = false) => {
|
|
3913
|
+
var assertStreamingAssertions = async (asserts, xstate, content, final = false) => {
|
|
3906
3914
|
if (!xstate.currField || xstate.s === -1 || !asserts || asserts.length === 0) {
|
|
3907
3915
|
return;
|
|
3908
3916
|
}
|
|
@@ -3915,7 +3923,7 @@ var assertStreamingAssertions = (asserts, xstate, content, final = false) => {
|
|
|
3915
3923
|
const currValue = content.substring(xstate.s);
|
|
3916
3924
|
for (const assert of fieldAsserts) {
|
|
3917
3925
|
const { message, fn } = assert;
|
|
3918
|
-
const res = fn(currValue, final);
|
|
3926
|
+
const res = await fn(currValue, final);
|
|
3919
3927
|
if (res === void 0) {
|
|
3920
3928
|
continue;
|
|
3921
3929
|
}
|
|
@@ -3928,9 +3936,6 @@ var assertStreamingAssertions = (asserts, xstate, content, final = false) => {
|
|
|
3928
3936
|
// dsp/datetime.ts
|
|
3929
3937
|
import moment from "moment-timezone";
|
|
3930
3938
|
|
|
3931
|
-
// dsp/program.ts
|
|
3932
|
-
import "@opentelemetry/api";
|
|
3933
|
-
|
|
3934
3939
|
// dsp/registry.ts
|
|
3935
3940
|
var AxInstanceRegistry = class {
|
|
3936
3941
|
reg;
|
|
@@ -4461,7 +4466,7 @@ function validateField(field) {
|
|
|
4461
4466
|
|
|
4462
4467
|
// dsp/util.ts
|
|
4463
4468
|
var colorLog3 = new ColorLog();
|
|
4464
|
-
var updateProgressBar = (current, total, success, elapsedTime, progressBarWidth = 20
|
|
4469
|
+
var updateProgressBar = (current, total, success, elapsedTime, msg, progressBarWidth = 20) => {
|
|
4465
4470
|
const percentage = (current / total * 100).toFixed(1);
|
|
4466
4471
|
const filledBarLength = Math.round(progressBarWidth * current / total);
|
|
4467
4472
|
const emptyBarLength = progressBarWidth - filledBarLength;
|
|
@@ -4708,6 +4713,7 @@ var AxProgramWithSignature = class {
|
|
|
4708
4713
|
async forward(_ai, _values, _options) {
|
|
4709
4714
|
throw new Error("forward() not implemented");
|
|
4710
4715
|
}
|
|
4716
|
+
// biome-ignore lint/correctness/useYield: just a placeholder
|
|
4711
4717
|
async *streamingForward(_ai, _values, _options) {
|
|
4712
4718
|
throw new Error("streamingForward() not implemented");
|
|
4713
4719
|
}
|
|
@@ -4805,6 +4811,7 @@ var AxProgram = class {
|
|
|
4805
4811
|
async forward(_ai, _values, _options) {
|
|
4806
4812
|
throw new Error("forward() not implemented");
|
|
4807
4813
|
}
|
|
4814
|
+
// biome-ignore lint/correctness/useYield: just a placeholder
|
|
4808
4815
|
async *streamingForward(_ai, _values, _options) {
|
|
4809
4816
|
throw new Error("streamingForward() not implemented");
|
|
4810
4817
|
}
|
|
@@ -4884,9 +4891,7 @@ var AxPromptTemplate = class {
|
|
|
4884
4891
|
task.push(
|
|
4885
4892
|
`You will be provided with the following fields: ${inArgs}. Your task is to generate new fields: ${outArgs}.`
|
|
4886
4893
|
);
|
|
4887
|
-
const funcs = functions?.map(
|
|
4888
|
-
(f) => "toFunction" in f ? f.toFunction() : f
|
|
4889
|
-
);
|
|
4894
|
+
const funcs = functions?.map((f) => "toFunction" in f ? f.toFunction() : f)?.flat();
|
|
4890
4895
|
const funcList = funcs?.map((fn) => `- \`${fn.name}\`: ${formatDescription(fn.description)}`).join("\n");
|
|
4891
4896
|
if (funcList && funcList.length > 0) {
|
|
4892
4897
|
task.push(`## Available Functions
|
|
@@ -5785,10 +5790,10 @@ var AxFunctionProcessor = class {
|
|
|
5785
5790
|
(v) => v.name.localeCompare(func.name) === 0
|
|
5786
5791
|
);
|
|
5787
5792
|
if (!fnSpec) {
|
|
5788
|
-
throw new Error(`Function not found:
|
|
5793
|
+
throw new Error(`Function not found: ${func.name}`);
|
|
5789
5794
|
}
|
|
5790
5795
|
if (!fnSpec.func) {
|
|
5791
|
-
throw new Error(
|
|
5796
|
+
throw new Error(`No handler for function: ${func.name}`);
|
|
5792
5797
|
}
|
|
5793
5798
|
try {
|
|
5794
5799
|
return await this.executeFunction(fnSpec, func, options);
|
|
@@ -5809,9 +5814,11 @@ var parseFunctions = (newFuncs, existingFuncs) => {
|
|
|
5809
5814
|
return f.toFunction();
|
|
5810
5815
|
}
|
|
5811
5816
|
return f;
|
|
5812
|
-
});
|
|
5817
|
+
}).flat();
|
|
5813
5818
|
for (const fn of functions.filter((v) => v.parameters)) {
|
|
5814
|
-
|
|
5819
|
+
if (fn.parameters) {
|
|
5820
|
+
validateJSONSchema(fn.parameters);
|
|
5821
|
+
}
|
|
5815
5822
|
}
|
|
5816
5823
|
return [...existingFuncs ?? [], ...functions];
|
|
5817
5824
|
};
|
|
@@ -5857,11 +5864,11 @@ ${result}
|
|
|
5857
5864
|
return promise;
|
|
5858
5865
|
});
|
|
5859
5866
|
const results = await Promise.all(promises);
|
|
5860
|
-
|
|
5867
|
+
for (const result of results) {
|
|
5861
5868
|
if (result) {
|
|
5862
5869
|
mem.add(result, sessionId);
|
|
5863
5870
|
}
|
|
5864
|
-
}
|
|
5871
|
+
}
|
|
5865
5872
|
return functionsExecuted;
|
|
5866
5873
|
};
|
|
5867
5874
|
function parseFunctionCalls(ai, functionCalls, values, model) {
|
|
@@ -5945,13 +5952,14 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5945
5952
|
model,
|
|
5946
5953
|
rateLimiter,
|
|
5947
5954
|
stream,
|
|
5948
|
-
functions,
|
|
5955
|
+
functions: _functions,
|
|
5949
5956
|
functionCall: _functionCall
|
|
5950
5957
|
} = options ?? {};
|
|
5951
5958
|
const chatPrompt = mem?.history(sessionId) ?? [];
|
|
5952
5959
|
if (chatPrompt.length === 0) {
|
|
5953
5960
|
throw new Error("No chat prompt found");
|
|
5954
5961
|
}
|
|
5962
|
+
const functions = _functions?.map((f) => "toFunction" in f ? f.toFunction() : f)?.flat();
|
|
5955
5963
|
const functionCall = _functionCall ?? this.options?.functionCall;
|
|
5956
5964
|
const res = await ai.chat(
|
|
5957
5965
|
{
|
|
@@ -5976,13 +5984,14 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
5976
5984
|
mem,
|
|
5977
5985
|
options
|
|
5978
5986
|
}) {
|
|
5979
|
-
const { sessionId, traceId, model, functions } = options ?? {};
|
|
5987
|
+
const { sessionId, traceId, model, functions: _functions } = options ?? {};
|
|
5980
5988
|
const fastFail = options?.fastFail ?? this.options?.fastFail;
|
|
5981
5989
|
const modelName = model ?? ai.getDefaultModels().model;
|
|
5982
5990
|
const usageInfo = {
|
|
5983
5991
|
ai: ai.getName(),
|
|
5984
5992
|
model: modelName
|
|
5985
5993
|
};
|
|
5994
|
+
const functions = _functions?.map((f) => "toFunction" in f ? f.toFunction() : f)?.flat();
|
|
5986
5995
|
const res = await this.forwardSendRequest({
|
|
5987
5996
|
ai,
|
|
5988
5997
|
mem,
|
|
@@ -6066,7 +6075,11 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6066
6075
|
streamingValidation
|
|
6067
6076
|
);
|
|
6068
6077
|
if (this.streamingAsserts.length !== 0) {
|
|
6069
|
-
assertStreamingAssertions(
|
|
6078
|
+
await assertStreamingAssertions(
|
|
6079
|
+
this.streamingAsserts,
|
|
6080
|
+
xstate,
|
|
6081
|
+
content
|
|
6082
|
+
);
|
|
6070
6083
|
}
|
|
6071
6084
|
if (this.streamingFieldProcessors.length !== 0) {
|
|
6072
6085
|
await processStreamingFieldProcessors(
|
|
@@ -6082,7 +6095,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6082
6095
|
if (skip) {
|
|
6083
6096
|
continue;
|
|
6084
6097
|
}
|
|
6085
|
-
assertAssertions(this.asserts, values);
|
|
6098
|
+
await assertAssertions(this.asserts, values);
|
|
6086
6099
|
}
|
|
6087
6100
|
if (result.finishReason === "length") {
|
|
6088
6101
|
throw new Error("Max tokens reached before completion");
|
|
@@ -6104,8 +6117,13 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6104
6117
|
this.functionsExecuted = /* @__PURE__ */ new Set([...this.functionsExecuted, ...fx]);
|
|
6105
6118
|
} else {
|
|
6106
6119
|
streamingExtractFinalValue(this.signature, values, xstate, content);
|
|
6107
|
-
assertStreamingAssertions(
|
|
6108
|
-
|
|
6120
|
+
await assertStreamingAssertions(
|
|
6121
|
+
this.streamingAsserts,
|
|
6122
|
+
xstate,
|
|
6123
|
+
content,
|
|
6124
|
+
true
|
|
6125
|
+
);
|
|
6126
|
+
await assertAssertions(this.asserts, values);
|
|
6109
6127
|
if (this.fieldProcessors.length) {
|
|
6110
6128
|
await processFieldProcessors(
|
|
6111
6129
|
this.fieldProcessors,
|
|
@@ -6165,7 +6183,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6165
6183
|
}
|
|
6166
6184
|
} else if (result.content) {
|
|
6167
6185
|
extractValues(this.signature, values, result.content);
|
|
6168
|
-
assertAssertions(this.asserts, values);
|
|
6186
|
+
await assertAssertions(this.asserts, values);
|
|
6169
6187
|
if (this.fieldProcessors.length) {
|
|
6170
6188
|
await processFieldProcessors(
|
|
6171
6189
|
this.fieldProcessors,
|
|
@@ -6283,8 +6301,8 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6283
6301
|
}
|
|
6284
6302
|
const funcNames = functions?.map((f) => f.name).join(",");
|
|
6285
6303
|
const attributes = {
|
|
6286
|
-
|
|
6287
|
-
|
|
6304
|
+
"generate.signature": this.signature.toString(),
|
|
6305
|
+
"generate.functions": funcNames ?? ""
|
|
6288
6306
|
};
|
|
6289
6307
|
const span = tracer.startSpan("Generate", {
|
|
6290
6308
|
kind: SpanKind2.SERVER,
|
|
@@ -6330,7 +6348,7 @@ var AxGen = class extends AxProgramWithSignature {
|
|
|
6330
6348
|
|
|
6331
6349
|
// prompts/agent.ts
|
|
6332
6350
|
function processChildAgentFunction(childFunction, parentValues, parentInputKeys, modelList, options) {
|
|
6333
|
-
|
|
6351
|
+
const processedFunction = { ...childFunction };
|
|
6334
6352
|
if (processedFunction.parameters) {
|
|
6335
6353
|
const childKeys = processedFunction.parameters.properties ? Object.keys(processedFunction.parameters.properties) : [];
|
|
6336
6354
|
const commonKeys = parentInputKeys.filter((key) => childKeys.includes(key)).filter((key) => key !== "model");
|
|
@@ -6402,7 +6420,7 @@ var AxAgent = class {
|
|
|
6402
6420
|
this.debug = debug;
|
|
6403
6421
|
if (!name || name.length < 5) {
|
|
6404
6422
|
throw new Error(
|
|
6405
|
-
|
|
6423
|
+
"Agent name must be at least 10 characters (more descriptive)"
|
|
6406
6424
|
);
|
|
6407
6425
|
}
|
|
6408
6426
|
if (!description || description.length < 20) {
|
|
@@ -6964,8 +6982,8 @@ var AxBootstrapFewShot = class {
|
|
|
6964
6982
|
total,
|
|
6965
6983
|
this.traces.length,
|
|
6966
6984
|
et,
|
|
6967
|
-
|
|
6968
|
-
|
|
6985
|
+
"Tuning Prompt",
|
|
6986
|
+
30
|
|
6969
6987
|
);
|
|
6970
6988
|
if (this.traces.length > maxDemos) {
|
|
6971
6989
|
return;
|
|
@@ -8058,13 +8076,13 @@ var AxHFDataLoader = class {
|
|
|
8058
8076
|
};
|
|
8059
8077
|
|
|
8060
8078
|
// funcs/code.ts
|
|
8061
|
-
import * as _crypto from "crypto";
|
|
8062
|
-
import * as _http from "http";
|
|
8063
|
-
import * as _https from "https";
|
|
8079
|
+
import * as _crypto from "node:crypto";
|
|
8064
8080
|
import * as _fs from "node:fs";
|
|
8065
|
-
import * as
|
|
8066
|
-
import * as
|
|
8067
|
-
import
|
|
8081
|
+
import * as _http from "node:http";
|
|
8082
|
+
import * as _https from "node:https";
|
|
8083
|
+
import * as _os from "node:os";
|
|
8084
|
+
import * as _process from "node:process";
|
|
8085
|
+
import { runInNewContext } from "node:vm";
|
|
8068
8086
|
var AxJSInterpreterPermission = /* @__PURE__ */ ((AxJSInterpreterPermission2) => {
|
|
8069
8087
|
AxJSInterpreterPermission2["FS"] = "node:fs";
|
|
8070
8088
|
AxJSInterpreterPermission2["NET"] = "net";
|
|
@@ -8335,7 +8353,7 @@ var AxTestPrompt = class {
|
|
|
8335
8353
|
successCount++;
|
|
8336
8354
|
}
|
|
8337
8355
|
const et = (/* @__PURE__ */ new Date()).getTime() - st;
|
|
8338
|
-
updateProgressBar(i, total, successCount, et,
|
|
8356
|
+
updateProgressBar(i, total, successCount, et, "Testing Prompt", 30);
|
|
8339
8357
|
}
|
|
8340
8358
|
console.log(
|
|
8341
8359
|
"\nPerformance: ",
|
|
@@ -8501,6 +8519,320 @@ var AxEmbeddingAdapter = class {
|
|
|
8501
8519
|
}
|
|
8502
8520
|
};
|
|
8503
8521
|
|
|
8522
|
+
// ../../node_modules/uuid/dist/esm-node/rng.js
|
|
8523
|
+
import crypto2 from "crypto";
|
|
8524
|
+
var rnds8Pool = new Uint8Array(256);
|
|
8525
|
+
var poolPtr = rnds8Pool.length;
|
|
8526
|
+
function rng() {
|
|
8527
|
+
if (poolPtr > rnds8Pool.length - 16) {
|
|
8528
|
+
crypto2.randomFillSync(rnds8Pool);
|
|
8529
|
+
poolPtr = 0;
|
|
8530
|
+
}
|
|
8531
|
+
return rnds8Pool.slice(poolPtr, poolPtr += 16);
|
|
8532
|
+
}
|
|
8533
|
+
|
|
8534
|
+
// ../../node_modules/uuid/dist/esm-node/stringify.js
|
|
8535
|
+
var byteToHex = [];
|
|
8536
|
+
for (let i = 0; i < 256; ++i) {
|
|
8537
|
+
byteToHex.push((i + 256).toString(16).slice(1));
|
|
8538
|
+
}
|
|
8539
|
+
function unsafeStringify(arr, offset = 0) {
|
|
8540
|
+
return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
|
|
8541
|
+
}
|
|
8542
|
+
|
|
8543
|
+
// ../../node_modules/uuid/dist/esm-node/native.js
|
|
8544
|
+
import crypto3 from "crypto";
|
|
8545
|
+
var native_default = {
|
|
8546
|
+
randomUUID: crypto3.randomUUID
|
|
8547
|
+
};
|
|
8548
|
+
|
|
8549
|
+
// ../../node_modules/uuid/dist/esm-node/v4.js
|
|
8550
|
+
function v4(options, buf, offset) {
|
|
8551
|
+
if (native_default.randomUUID && !buf && !options) {
|
|
8552
|
+
return native_default.randomUUID();
|
|
8553
|
+
}
|
|
8554
|
+
options = options || {};
|
|
8555
|
+
const rnds = options.random || (options.rng || rng)();
|
|
8556
|
+
rnds[6] = rnds[6] & 15 | 64;
|
|
8557
|
+
rnds[8] = rnds[8] & 63 | 128;
|
|
8558
|
+
if (buf) {
|
|
8559
|
+
offset = offset || 0;
|
|
8560
|
+
for (let i = 0; i < 16; ++i) {
|
|
8561
|
+
buf[offset + i] = rnds[i];
|
|
8562
|
+
}
|
|
8563
|
+
return buf;
|
|
8564
|
+
}
|
|
8565
|
+
return unsafeStringify(rnds);
|
|
8566
|
+
}
|
|
8567
|
+
var v4_default = v4;
|
|
8568
|
+
|
|
8569
|
+
// mcp/client.ts
|
|
8570
|
+
var colorLog7 = new ColorLog();
|
|
8571
|
+
var AxMCPClient = class {
|
|
8572
|
+
constructor(transport, options = {}) {
|
|
8573
|
+
this.transport = transport;
|
|
8574
|
+
this.options = options;
|
|
8575
|
+
}
|
|
8576
|
+
functions = [];
|
|
8577
|
+
activeRequests = /* @__PURE__ */ new Map();
|
|
8578
|
+
capabilities = {};
|
|
8579
|
+
async init() {
|
|
8580
|
+
if ("connect" in this.transport) {
|
|
8581
|
+
await this.transport.connect?.();
|
|
8582
|
+
}
|
|
8583
|
+
const { result: res } = await this.sendRequest("initialize", {
|
|
8584
|
+
protocolVersion: "2024-11-05",
|
|
8585
|
+
capabilities: {
|
|
8586
|
+
roots: { listChanged: true },
|
|
8587
|
+
sampling: {}
|
|
8588
|
+
},
|
|
8589
|
+
clientInfo: {
|
|
8590
|
+
name: "AxMCPClient",
|
|
8591
|
+
version: "1.0.0"
|
|
8592
|
+
}
|
|
8593
|
+
});
|
|
8594
|
+
const expectedProtocolVersion = "2024-11-05";
|
|
8595
|
+
if (res.protocolVersion !== expectedProtocolVersion) {
|
|
8596
|
+
throw new Error(
|
|
8597
|
+
`Protocol version mismatch. Expected ${expectedProtocolVersion} but got ${res.protocolVersion}`
|
|
8598
|
+
);
|
|
8599
|
+
}
|
|
8600
|
+
if (res.capabilities.tools) {
|
|
8601
|
+
this.capabilities.tools = true;
|
|
8602
|
+
}
|
|
8603
|
+
if (res.capabilities.resources) {
|
|
8604
|
+
this.capabilities.resources = true;
|
|
8605
|
+
}
|
|
8606
|
+
if (res.capabilities.prompts) {
|
|
8607
|
+
this.capabilities.prompts = true;
|
|
8608
|
+
}
|
|
8609
|
+
await this.sendNotification("notifications/initialized");
|
|
8610
|
+
await this.discoverFunctions();
|
|
8611
|
+
}
|
|
8612
|
+
async discoverFunctions() {
|
|
8613
|
+
if (!this.capabilities.tools) {
|
|
8614
|
+
throw new Error("Tools are not supported");
|
|
8615
|
+
}
|
|
8616
|
+
const { result: res } = await this.sendRequest("tools/list");
|
|
8617
|
+
this.functions = res.tools.map((fn) => {
|
|
8618
|
+
const override = this.options.functionOverrides?.find(
|
|
8619
|
+
(o) => o.name === fn.name
|
|
8620
|
+
);
|
|
8621
|
+
const parameters = fn.inputSchema.properties ? {
|
|
8622
|
+
properties: fn.inputSchema.properties,
|
|
8623
|
+
required: fn.inputSchema.required ?? [],
|
|
8624
|
+
type: fn.inputSchema.type
|
|
8625
|
+
} : void 0;
|
|
8626
|
+
return {
|
|
8627
|
+
name: override?.updates.name ?? fn.name,
|
|
8628
|
+
description: override?.updates.description ?? fn.description,
|
|
8629
|
+
parameters,
|
|
8630
|
+
func: async (args) => {
|
|
8631
|
+
const { result } = await this.sendRequest("tools/call", { name: fn.name, arguments: args });
|
|
8632
|
+
return result;
|
|
8633
|
+
}
|
|
8634
|
+
};
|
|
8635
|
+
});
|
|
8636
|
+
if (this.options.debug) {
|
|
8637
|
+
console.log(
|
|
8638
|
+
colorLog7.yellow(`> Discovered ${this.functions.length} functions:`)
|
|
8639
|
+
);
|
|
8640
|
+
for (const fn of this.functions) {
|
|
8641
|
+
console.log(colorLog7.yellow(` - ${fn.name}: ${fn.description}`));
|
|
8642
|
+
}
|
|
8643
|
+
}
|
|
8644
|
+
}
|
|
8645
|
+
async ping(timeout = 3e3) {
|
|
8646
|
+
const pingPromise = this.sendRequest("ping");
|
|
8647
|
+
const timeoutPromise = new Promise(
|
|
8648
|
+
(_, reject) => setTimeout(
|
|
8649
|
+
() => reject(new Error("Ping response timeout exceeded")),
|
|
8650
|
+
timeout
|
|
8651
|
+
)
|
|
8652
|
+
);
|
|
8653
|
+
const response = await Promise.race([pingPromise, timeoutPromise]);
|
|
8654
|
+
const { result } = response;
|
|
8655
|
+
if (typeof result !== "object" || result === null || Object.keys(result).length !== 0) {
|
|
8656
|
+
throw new Error(`Unexpected ping response: ${JSON.stringify(result)}`);
|
|
8657
|
+
}
|
|
8658
|
+
}
|
|
8659
|
+
toFunction() {
|
|
8660
|
+
return this.functions;
|
|
8661
|
+
}
|
|
8662
|
+
cancelRequest(id) {
|
|
8663
|
+
if (this.activeRequests.has(id)) {
|
|
8664
|
+
this.sendNotification("notifications/cancelled", {
|
|
8665
|
+
requestId: id,
|
|
8666
|
+
reason: "Client cancelled request"
|
|
8667
|
+
});
|
|
8668
|
+
const entry = this.activeRequests.get(id);
|
|
8669
|
+
if (entry) {
|
|
8670
|
+
entry.reject(new Error(`Request ${id} cancelled`));
|
|
8671
|
+
}
|
|
8672
|
+
this.activeRequests.delete(id);
|
|
8673
|
+
}
|
|
8674
|
+
}
|
|
8675
|
+
async sendRequest(method, params = {}) {
|
|
8676
|
+
const requestId = v4_default();
|
|
8677
|
+
const request = {
|
|
8678
|
+
jsonrpc: "2.0",
|
|
8679
|
+
id: requestId,
|
|
8680
|
+
method,
|
|
8681
|
+
params
|
|
8682
|
+
};
|
|
8683
|
+
if (this.options.debug) {
|
|
8684
|
+
console.log(
|
|
8685
|
+
colorLog7.blueBright(
|
|
8686
|
+
`> Sending request ${requestId}:
|
|
8687
|
+
${JSON.stringify(request, null, 2)}`
|
|
8688
|
+
)
|
|
8689
|
+
);
|
|
8690
|
+
}
|
|
8691
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
8692
|
+
this.activeRequests.set(requestId, { reject });
|
|
8693
|
+
this.transport.send(request).then((res) => {
|
|
8694
|
+
this.activeRequests.delete(requestId);
|
|
8695
|
+
if (this.options.debug) {
|
|
8696
|
+
console.log(
|
|
8697
|
+
colorLog7.greenBright(
|
|
8698
|
+
`> Received response for request ${requestId}:
|
|
8699
|
+
${JSON.stringify(res, null, 2)}`
|
|
8700
|
+
)
|
|
8701
|
+
);
|
|
8702
|
+
}
|
|
8703
|
+
if (res !== null && typeof res === "object" && "error" in res) {
|
|
8704
|
+
const errorObj = res;
|
|
8705
|
+
reject(
|
|
8706
|
+
new Error(
|
|
8707
|
+
`RPC Error ${errorObj.error.code}: ${errorObj.error.message}`
|
|
8708
|
+
)
|
|
8709
|
+
);
|
|
8710
|
+
} else if (res !== null && typeof res === "object" && "result" in res) {
|
|
8711
|
+
resolve({ result: res.result });
|
|
8712
|
+
} else {
|
|
8713
|
+
reject(new Error("Invalid response no result or error"));
|
|
8714
|
+
}
|
|
8715
|
+
}).catch((err) => {
|
|
8716
|
+
this.activeRequests.delete(requestId);
|
|
8717
|
+
reject(err);
|
|
8718
|
+
});
|
|
8719
|
+
});
|
|
8720
|
+
const { result } = await responsePromise;
|
|
8721
|
+
return { id: requestId, result };
|
|
8722
|
+
}
|
|
8723
|
+
async sendNotification(method, params = {}) {
|
|
8724
|
+
const notification = {
|
|
8725
|
+
jsonrpc: "2.0",
|
|
8726
|
+
method,
|
|
8727
|
+
params
|
|
8728
|
+
};
|
|
8729
|
+
if (this.options.debug) {
|
|
8730
|
+
console.log(
|
|
8731
|
+
"\u27A1\uFE0F Sending notification:",
|
|
8732
|
+
JSON.stringify(notification, null, 2)
|
|
8733
|
+
);
|
|
8734
|
+
}
|
|
8735
|
+
await this.transport.sendNotification(notification);
|
|
8736
|
+
}
|
|
8737
|
+
};
|
|
8738
|
+
|
|
8739
|
+
// mcp/httpTransport.ts
|
|
8740
|
+
var AxMCPHTTPTransport = class {
|
|
8741
|
+
endpoint = null;
|
|
8742
|
+
sseUrl;
|
|
8743
|
+
eventSource;
|
|
8744
|
+
constructor(sseUrl) {
|
|
8745
|
+
this.sseUrl = sseUrl;
|
|
8746
|
+
}
|
|
8747
|
+
async connect() {
|
|
8748
|
+
return new Promise((resolve, reject) => {
|
|
8749
|
+
this.eventSource = new EventSource(this.sseUrl);
|
|
8750
|
+
this.eventSource.addEventListener("endpoint", (event) => {
|
|
8751
|
+
try {
|
|
8752
|
+
const messageEvent = event;
|
|
8753
|
+
const data = JSON.parse(messageEvent.data);
|
|
8754
|
+
if (!data.uri) {
|
|
8755
|
+
throw new Error("Endpoint URI missing in SSE event data");
|
|
8756
|
+
}
|
|
8757
|
+
this.endpoint = data.uri;
|
|
8758
|
+
resolve();
|
|
8759
|
+
} catch (error) {
|
|
8760
|
+
reject(error);
|
|
8761
|
+
}
|
|
8762
|
+
});
|
|
8763
|
+
this.eventSource.onerror = () => {
|
|
8764
|
+
reject(new Error("Failed to establish SSE connection"));
|
|
8765
|
+
};
|
|
8766
|
+
});
|
|
8767
|
+
}
|
|
8768
|
+
async send(message) {
|
|
8769
|
+
if (!this.endpoint) {
|
|
8770
|
+
throw new Error(
|
|
8771
|
+
"HTTPTransport endpoint is not initialized. Call connect() first."
|
|
8772
|
+
);
|
|
8773
|
+
}
|
|
8774
|
+
const res = await fetch(this.endpoint, {
|
|
8775
|
+
method: "POST",
|
|
8776
|
+
headers: { "Content-Type": "application/json" },
|
|
8777
|
+
body: JSON.stringify(message)
|
|
8778
|
+
});
|
|
8779
|
+
if (!res.ok) {
|
|
8780
|
+
throw new Error(`HTTP error ${res.status}: ${res.statusText}`);
|
|
8781
|
+
}
|
|
8782
|
+
return res.json();
|
|
8783
|
+
}
|
|
8784
|
+
async sendNotification(message) {
|
|
8785
|
+
if (!this.endpoint) {
|
|
8786
|
+
throw new Error(
|
|
8787
|
+
"HTTPTransport endpoint is not initialized. Call connect() first."
|
|
8788
|
+
);
|
|
8789
|
+
}
|
|
8790
|
+
await fetch(this.endpoint, {
|
|
8791
|
+
method: "POST",
|
|
8792
|
+
headers: { "Content-Type": "application/json" },
|
|
8793
|
+
body: JSON.stringify(message)
|
|
8794
|
+
});
|
|
8795
|
+
}
|
|
8796
|
+
};
|
|
8797
|
+
|
|
8798
|
+
// mcp/stdioTransport.ts
|
|
8799
|
+
import { spawn } from "node:child_process";
|
|
8800
|
+
import readline from "node:readline";
|
|
8801
|
+
var AxMCPStdioTransport = class {
|
|
8802
|
+
process;
|
|
8803
|
+
rl;
|
|
8804
|
+
pendingResponses = /* @__PURE__ */ new Map();
|
|
8805
|
+
constructor(config) {
|
|
8806
|
+
this.process = spawn(config.command, config.args ?? [], {
|
|
8807
|
+
env: config.env ? { ...process.env, ...config.env } : process.env
|
|
8808
|
+
});
|
|
8809
|
+
this.rl = readline.createInterface({ input: this.process.stdout });
|
|
8810
|
+
this.rl.on("line", (line) => {
|
|
8811
|
+
const response = JSON.parse(line);
|
|
8812
|
+
const resolver = this.pendingResponses.get(response.id);
|
|
8813
|
+
if (resolver) {
|
|
8814
|
+
resolver(response);
|
|
8815
|
+
this.pendingResponses.delete(response.id);
|
|
8816
|
+
}
|
|
8817
|
+
});
|
|
8818
|
+
}
|
|
8819
|
+
async send(message) {
|
|
8820
|
+
return new Promise((resolve) => {
|
|
8821
|
+
this.pendingResponses.set(message.id, (res) => {
|
|
8822
|
+
resolve(res);
|
|
8823
|
+
});
|
|
8824
|
+
this.process.stdin.write(`${JSON.stringify(message)}
|
|
8825
|
+
`);
|
|
8826
|
+
});
|
|
8827
|
+
}
|
|
8828
|
+
async sendNotification(message) {
|
|
8829
|
+
this.process.stdin.write(`${JSON.stringify(message)}
|
|
8830
|
+
`);
|
|
8831
|
+
}
|
|
8832
|
+
async connect() {
|
|
8833
|
+
}
|
|
8834
|
+
};
|
|
8835
|
+
|
|
8504
8836
|
// ai/multiservice.ts
|
|
8505
8837
|
var AxMultiServiceRouter = class {
|
|
8506
8838
|
services = /* @__PURE__ */ new Map();
|
|
@@ -8752,6 +9084,9 @@ export {
|
|
|
8752
9084
|
AxJSInterpreter,
|
|
8753
9085
|
AxJSInterpreterPermission,
|
|
8754
9086
|
AxLLMRequestTypeValues,
|
|
9087
|
+
AxMCPClient,
|
|
9088
|
+
AxMCPHTTPTransport,
|
|
9089
|
+
AxMCPStdioTransport,
|
|
8755
9090
|
AxMemory,
|
|
8756
9091
|
AxMockAIService,
|
|
8757
9092
|
AxMultiServiceRouter,
|