@agentrun/sdk 0.0.3 → 0.0.4
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/README.md +20 -15
- package/dist/agent-runtime/api/index.cjs +907 -0
- package/dist/agent-runtime/api/index.cjs.map +1 -0
- package/dist/agent-runtime/api/index.d.cts +193 -0
- package/dist/agent-runtime/api/index.d.ts +193 -0
- package/dist/agent-runtime/api/index.js +882 -0
- package/dist/agent-runtime/api/index.js.map +1 -0
- package/dist/agent-runtime/index.cjs +2528 -0
- package/dist/agent-runtime/index.cjs.map +1 -0
- package/dist/agent-runtime/index.d.cts +778 -0
- package/dist/agent-runtime/index.d.ts +778 -0
- package/dist/agent-runtime/index.js +2496 -0
- package/dist/agent-runtime/index.js.map +1 -0
- package/dist/base-K1GMfJbH.d.ts +59 -0
- package/dist/base-xcWt5bua.d.cts +59 -0
- package/dist/client-DHXxjuo3.d.ts +58 -0
- package/dist/client-DPUTs69s.d.cts +58 -0
- package/dist/config-07gMelJP.d.cts +125 -0
- package/dist/config-07gMelJP.d.ts +125 -0
- package/dist/control-api-BWD4eua5.d.cts +27 -0
- package/dist/control-api-d-82Sgpr.d.ts +27 -0
- package/dist/credential/api/index.cjs +655 -0
- package/dist/credential/api/index.cjs.map +1 -0
- package/dist/credential/api/index.d.cts +93 -0
- package/dist/credential/api/index.d.ts +93 -0
- package/dist/credential/api/index.js +630 -0
- package/dist/credential/api/index.js.map +1 -0
- package/dist/credential/index.cjs +1286 -0
- package/dist/credential/index.cjs.map +1 -0
- package/dist/credential/index.d.cts +246 -0
- package/dist/credential/index.d.ts +246 -0
- package/dist/credential/index.js +1263 -0
- package/dist/credential/index.js.map +1 -0
- package/dist/data-api-B-4h9_Vf.d.ts +166 -0
- package/dist/data-api-DsG-8JRQ.d.cts +166 -0
- package/dist/exception-DM9H2Rpo.d.cts +68 -0
- package/dist/exception-DM9H2Rpo.d.ts +68 -0
- package/dist/index.cjs +29281 -1427
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -4485
- package/dist/index.d.ts +38 -4485
- package/dist/index.js +29259 -1426
- package/dist/index.js.map +1 -1
- package/dist/integration/builtin/index.cjs +9399 -0
- package/dist/integration/builtin/index.cjs.map +1 -0
- package/dist/integration/builtin/index.d.cts +692 -0
- package/dist/integration/builtin/index.d.ts +692 -0
- package/dist/integration/builtin/index.js +9363 -0
- package/dist/integration/builtin/index.js.map +1 -0
- package/dist/integration/index.cjs +9524 -0
- package/dist/integration/index.cjs.map +1 -0
- package/dist/integration/index.d.cts +98 -0
- package/dist/integration/index.d.ts +98 -0
- package/dist/integration/index.js +9481 -0
- package/dist/integration/index.js.map +1 -0
- package/dist/integration/mastra/index.cjs +9383 -0
- package/dist/integration/mastra/index.cjs.map +1 -0
- package/dist/integration/mastra/index.d.cts +186 -0
- package/dist/integration/mastra/index.d.ts +186 -0
- package/dist/integration/mastra/index.js +9348 -0
- package/dist/integration/mastra/index.js.map +1 -0
- package/dist/model/index.cjs +2392 -0
- package/dist/model/index.cjs.map +1 -0
- package/dist/model/index.d.cts +97 -0
- package/dist/model/index.d.ts +97 -0
- package/dist/model/index.js +2368 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model--I90nCqy.d.cts +66 -0
- package/dist/model--I90nCqy.d.ts +66 -0
- package/dist/model-BV7A6Trb.d.cts +512 -0
- package/dist/model-DGBy-o_L.d.cts +176 -0
- package/dist/model-DGBy-o_L.d.ts +176 -0
- package/dist/model-RiiiZnou.d.ts +512 -0
- package/dist/model-service-D-P2FZNi.d.ts +540 -0
- package/dist/model-service-VpzBb7rV.d.cts +540 -0
- package/dist/resource-CQovFUeh.d.cts +44 -0
- package/dist/resource-DnE_DEka.d.ts +44 -0
- package/dist/sandbox/index.cjs +3557 -0
- package/dist/sandbox/index.cjs.map +1 -0
- package/dist/sandbox/index.d.cts +1598 -0
- package/dist/sandbox/index.d.ts +1598 -0
- package/dist/sandbox/index.js +3532 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/server/adapter/index.cjs +24886 -0
- package/dist/server/adapter/index.cjs.map +1 -0
- package/dist/server/adapter/index.d.cts +68 -0
- package/dist/server/adapter/index.d.ts +68 -0
- package/dist/server/adapter/index.js +24883 -0
- package/dist/server/adapter/index.js.map +1 -0
- package/dist/server/core/index.cjs +313 -0
- package/dist/server/core/index.cjs.map +1 -0
- package/dist/server/core/index.d.cts +58 -0
- package/dist/server/core/index.d.ts +58 -0
- package/dist/server/core/index.js +309 -0
- package/dist/server/core/index.js.map +1 -0
- package/dist/server/index.cjs +25098 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +73 -0
- package/dist/server/index.d.ts +73 -0
- package/dist/server/index.js +25067 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/protocol/index.cjs +1057 -0
- package/dist/server/protocol/index.cjs.map +1 -0
- package/dist/server/protocol/index.d.cts +135 -0
- package/dist/server/protocol/index.d.ts +135 -0
- package/dist/server/protocol/index.js +1053 -0
- package/dist/server/protocol/index.js.map +1 -0
- package/dist/tool-CG0LY-ov.d.cts +155 -0
- package/dist/tool-JJHam0ms.d.ts +155 -0
- package/dist/toolset/api/index.cjs +754 -0
- package/dist/toolset/api/index.cjs.map +1 -0
- package/dist/toolset/api/index.d.cts +207 -0
- package/dist/toolset/api/index.d.ts +207 -0
- package/dist/toolset/api/index.js +727 -0
- package/dist/toolset/api/index.js.map +1 -0
- package/dist/toolset/index.cjs +1945 -0
- package/dist/toolset/index.cjs.map +1 -0
- package/dist/toolset/index.d.cts +182 -0
- package/dist/toolset/index.d.ts +182 -0
- package/dist/toolset/index.js +1920 -0
- package/dist/toolset/index.js.map +1 -0
- package/dist/toolset-BYDvhwRp.d.cts +394 -0
- package/dist/toolset-CSRsJxCb.d.ts +394 -0
- package/dist/utils/index.cjs +994 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +30 -0
- package/dist/utils/index.d.ts +30 -0
- package/dist/utils/index.js +951 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +65 -13
|
@@ -0,0 +1,1920 @@
|
|
|
1
|
+
import * as dotenv from 'dotenv';
|
|
2
|
+
import * as $Devs from '@alicloud/devs20230714';
|
|
3
|
+
import { Authorization, AuthorizationParameters, APIKeyAuthParameter, Toolset, ToolsetSpec, ToolsetSchema, ListToolsetsRequest } from '@alicloud/devs20230714';
|
|
4
|
+
import * as $OpenApi from '@alicloud/openapi-client';
|
|
5
|
+
import * as $Util from '@alicloud/tea-util';
|
|
6
|
+
import YAML from 'js-yaml';
|
|
7
|
+
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __esm = (fn, res) => function __init() {
|
|
13
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
14
|
+
};
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/utils/log.ts
|
|
30
|
+
var LOG_LEVELS, COLORS, Logger, logger;
|
|
31
|
+
var init_log = __esm({
|
|
32
|
+
"src/utils/log.ts"() {
|
|
33
|
+
LOG_LEVELS = {
|
|
34
|
+
debug: 0,
|
|
35
|
+
info: 1,
|
|
36
|
+
warn: 2,
|
|
37
|
+
error: 3
|
|
38
|
+
};
|
|
39
|
+
COLORS = {
|
|
40
|
+
reset: "\x1B[0m",
|
|
41
|
+
bright: "\x1B[1m",
|
|
42
|
+
dim: "\x1B[2m",
|
|
43
|
+
italic: "\x1B[3m",
|
|
44
|
+
blue: "\x1B[34m",
|
|
45
|
+
cyan: "\x1B[36m",
|
|
46
|
+
yellow: "\x1B[33m",
|
|
47
|
+
red: "\x1B[31m"
|
|
48
|
+
};
|
|
49
|
+
Logger = class {
|
|
50
|
+
level = "info";
|
|
51
|
+
// match Python logger name
|
|
52
|
+
prefix = "agentrun-logger";
|
|
53
|
+
setLevel(level) {
|
|
54
|
+
this.level = level;
|
|
55
|
+
}
|
|
56
|
+
shouldLog(level) {
|
|
57
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[this.level];
|
|
58
|
+
}
|
|
59
|
+
getColor(level) {
|
|
60
|
+
switch (level) {
|
|
61
|
+
case "debug":
|
|
62
|
+
return COLORS.cyan;
|
|
63
|
+
case "info":
|
|
64
|
+
return COLORS.blue;
|
|
65
|
+
case "warn":
|
|
66
|
+
return COLORS.yellow;
|
|
67
|
+
case "error":
|
|
68
|
+
return COLORS.red;
|
|
69
|
+
default:
|
|
70
|
+
return COLORS.reset;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// format timestamp like Python: YYYY-MM-DD HH:mm:ss,SSS
|
|
74
|
+
formatTimestamp(d = /* @__PURE__ */ new Date()) {
|
|
75
|
+
const pad = (n, sz = 2) => n.toString().padStart(sz, "0");
|
|
76
|
+
const year = d.getFullYear();
|
|
77
|
+
const month = pad(d.getMonth() + 1);
|
|
78
|
+
const day = pad(d.getDate());
|
|
79
|
+
const hour = pad(d.getHours());
|
|
80
|
+
const minute = pad(d.getMinutes());
|
|
81
|
+
const second = pad(d.getSeconds());
|
|
82
|
+
const ms = pad(d.getMilliseconds(), 3);
|
|
83
|
+
return `${year}-${month}-${day} ${hour}:${minute}:${second},${ms}`;
|
|
84
|
+
}
|
|
85
|
+
// attempt to infer caller file and line by parsing Error.stack
|
|
86
|
+
// helper: parse a single stack frame into {filepath, line, functionName}
|
|
87
|
+
parseFrame(frame) {
|
|
88
|
+
const m = frame.match(/^(?:at\s+)?(?:(.+?)\s+\()?(.*?):(\d+):(\d+)\)?$/);
|
|
89
|
+
if (!m) return null;
|
|
90
|
+
return {
|
|
91
|
+
functionName: m[1] ? m[1].trim() : void 0,
|
|
92
|
+
filepath: m[2],
|
|
93
|
+
line: parseInt(m[3], 10)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
// get caller by fixed stack offset (used in public log methods)
|
|
97
|
+
getCallerByOffset() {
|
|
98
|
+
const err = new Error();
|
|
99
|
+
const stack = err.stack;
|
|
100
|
+
if (!stack) return {};
|
|
101
|
+
const lines = stack.split("\n").map((l) => l.trim());
|
|
102
|
+
for (let i = 3; i < lines.length; i++) {
|
|
103
|
+
let parsed = this.parseFrame(lines[i]);
|
|
104
|
+
if (!parsed) {
|
|
105
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
106
|
+
if (m) {
|
|
107
|
+
const parts = m[1].split(":");
|
|
108
|
+
parts.pop();
|
|
109
|
+
const ln = Number(parts.pop());
|
|
110
|
+
const fp2 = parts.join(":");
|
|
111
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (!parsed) continue;
|
|
115
|
+
const fp = parsed.filepath;
|
|
116
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
117
|
+
continue;
|
|
118
|
+
return { filepath: parsed.filepath, line: parsed.line };
|
|
119
|
+
}
|
|
120
|
+
const cwd = process.cwd();
|
|
121
|
+
for (let i = 0; i < lines.length; i++) {
|
|
122
|
+
let parsed = this.parseFrame(lines[i]);
|
|
123
|
+
if (!parsed) {
|
|
124
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
125
|
+
if (m) {
|
|
126
|
+
const parts = m[1].split(":");
|
|
127
|
+
parts.pop();
|
|
128
|
+
const ln = Number(parts.pop());
|
|
129
|
+
const fp2 = parts.join(":");
|
|
130
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!parsed) continue;
|
|
134
|
+
const fp = parsed.filepath;
|
|
135
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
136
|
+
continue;
|
|
137
|
+
if (fp.includes("/src/utils/log.ts")) continue;
|
|
138
|
+
if (fp.startsWith(cwd)) return { filepath: parsed.filepath, line: parsed.line };
|
|
139
|
+
}
|
|
140
|
+
for (let i = 0; i < lines.length; i++) {
|
|
141
|
+
let parsed = this.parseFrame(lines[i]);
|
|
142
|
+
if (!parsed) {
|
|
143
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
144
|
+
if (m) {
|
|
145
|
+
const parts = m[1].split(":");
|
|
146
|
+
parts.pop();
|
|
147
|
+
const ln = Number(parts.pop());
|
|
148
|
+
const fp2 = parts.join(":");
|
|
149
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (!parsed) continue;
|
|
153
|
+
const fp = parsed.filepath;
|
|
154
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
155
|
+
continue;
|
|
156
|
+
if (fp.includes("/src/utils/log.ts")) continue;
|
|
157
|
+
return { filepath: parsed.filepath, line: parsed.line };
|
|
158
|
+
}
|
|
159
|
+
return {};
|
|
160
|
+
}
|
|
161
|
+
formatMessage(level, message, filepath, line) {
|
|
162
|
+
const timestamp = this.formatTimestamp();
|
|
163
|
+
const color = this.getColor(level);
|
|
164
|
+
const reset = COLORS.reset;
|
|
165
|
+
const levelName = level === "warn" ? "WARNING" : level.toUpperCase();
|
|
166
|
+
const levelStr = `${COLORS.bright}${color}${levelName}${reset}`;
|
|
167
|
+
const nameStr = `${color}[${this.prefix}]${reset}`;
|
|
168
|
+
const tsStr = `${color} ${timestamp}${reset}`;
|
|
169
|
+
const pathInfo = filepath && line !== void 0 ? ` ${COLORS.dim}${COLORS.italic}${filepath}:${line}${reset}` : "";
|
|
170
|
+
const msg = level === "debug" ? `${COLORS.dim}${message}${reset}` : message;
|
|
171
|
+
return `
|
|
172
|
+
${levelStr} ${nameStr}${tsStr}${pathInfo}
|
|
173
|
+
${msg}
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
176
|
+
debug(message, ...args) {
|
|
177
|
+
if (this.shouldLog("debug")) {
|
|
178
|
+
const caller = this.getCallerByOffset();
|
|
179
|
+
console.debug(this.formatMessage("debug", message, caller.filepath, caller.line), ...args);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
info(message, ...args) {
|
|
183
|
+
if (this.shouldLog("info")) {
|
|
184
|
+
const caller = this.getCallerByOffset();
|
|
185
|
+
console.info(this.formatMessage("info", message, caller.filepath, caller.line), ...args);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
warn(message, ...args) {
|
|
189
|
+
if (this.shouldLog("warn")) {
|
|
190
|
+
const caller = this.getCallerByOffset();
|
|
191
|
+
console.warn(this.formatMessage("warn", message, caller.filepath, caller.line), ...args);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
error(message, ...args) {
|
|
195
|
+
if (this.shouldLog("error")) {
|
|
196
|
+
const caller = this.getCallerByOffset();
|
|
197
|
+
console.error(this.formatMessage("error", message, caller.filepath, caller.line), ...args);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
logger = new Logger();
|
|
202
|
+
if (![void 0, null, "", "False", "FALSE", "false", "0"].includes(process.env["AGENTRUN_SDK_DEBUG"])) {
|
|
203
|
+
logger.setLevel("debug");
|
|
204
|
+
if (!globalThis._AGENTRUN_DEBUG_LOGGED) {
|
|
205
|
+
logger.warn("\u542F\u7528 AgentRun SDK \u8C03\u8BD5\u65E5\u5FD7\uFF0C \u79FB\u9664 AGENTRUN_SDK_DEBUG \u73AF\u5883\u53D8\u91CF\u4EE5\u5173\u95ED");
|
|
206
|
+
globalThis._AGENTRUN_DEBUG_LOGGED = true;
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
logger.setLevel("info");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// src/utils/version-check.ts
|
|
215
|
+
var VERSION;
|
|
216
|
+
var init_version_check = __esm({
|
|
217
|
+
"src/utils/version-check.ts"() {
|
|
218
|
+
init_log();
|
|
219
|
+
VERSION = "0.0.4" ;
|
|
220
|
+
if (!process.env.DISABLE_BREAKING_CHANGES_WARNING && !globalThis._AGENTRUN_VERSION_WARNING_SHOWN) {
|
|
221
|
+
globalThis._AGENTRUN_VERSION_WARNING_SHOWN = true;
|
|
222
|
+
logger.warn(
|
|
223
|
+
`\u5F53\u524D\u60A8\u6B63\u5728\u4F7F\u7528 AgentRun Python SDK \u7248\u672C ${VERSION}\u3002\u65E9\u671F\u7248\u672C\u901A\u5E38\u5305\u542B\u8BB8\u591A\u65B0\u529F\u80FD\uFF0C\u8FD9\u4E9B\u529F\u80FD\x1B[1;33m \u53EF\u80FD\u5F15\u5165\u4E0D\u517C\u5BB9\u7684\u53D8\u66F4 \x1B[0m\u3002\u4E3A\u907F\u514D\u6F5C\u5728\u95EE\u9898\uFF0C\u6211\u4EEC\u5F3A\u70C8\u5EFA\u8BAE\x1B[1;32m \u5C06\u4F9D\u8D56\u9501\u5B9A\u4E3A\u6B64\u7248\u672C \x1B[0m\u3002
|
|
224
|
+
You are currently using AgentRun Python SDK version ${VERSION}. Early versions often include many new features, which\x1B[1;33m may introduce breaking changes\x1B[0m. To avoid potential issues, we strongly recommend \x1B[1;32mpinning the dependency to this version\x1B[0m.
|
|
225
|
+
\x1B[2;3m pip install 'agentrun-sdk==${VERSION}' \x1B[0m
|
|
226
|
+
|
|
227
|
+
\u589E\u52A0\x1B[2;3m DISABLE_BREAKING_CHANGES_WARNING=1 \x1B[0m\u5230\u60A8\u7684\u73AF\u5883\u53D8\u91CF\u4EE5\u5173\u95ED\u6B64\u8B66\u544A\u3002
|
|
228
|
+
Add\x1B[2;3m DISABLE_BREAKING_CHANGES_WARNING=1 \x1B[0mto your environment variables to disable this warning.
|
|
229
|
+
|
|
230
|
+
Releases:\x1B[2;3m https://github.com/Serverless-Devs/agentrun-sdk-python/releases\x1B[0m`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
function getEnvWithDefault(defaultValue, ...keys) {
|
|
236
|
+
for (const key of keys) {
|
|
237
|
+
const value = process.env[key];
|
|
238
|
+
if (value !== void 0 && value !== "") {
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return defaultValue;
|
|
243
|
+
}
|
|
244
|
+
var Config;
|
|
245
|
+
var init_config = __esm({
|
|
246
|
+
"src/utils/config.ts"() {
|
|
247
|
+
dotenv.config();
|
|
248
|
+
Config = class _Config {
|
|
249
|
+
_accessKeyId;
|
|
250
|
+
_accessKeySecret;
|
|
251
|
+
_securityToken;
|
|
252
|
+
_accountId;
|
|
253
|
+
_token;
|
|
254
|
+
_regionId;
|
|
255
|
+
_timeout;
|
|
256
|
+
_readTimeout;
|
|
257
|
+
_controlEndpoint;
|
|
258
|
+
_dataEndpoint;
|
|
259
|
+
_devsEndpoint;
|
|
260
|
+
_headers;
|
|
261
|
+
constructor(options = {}) {
|
|
262
|
+
this._accessKeyId = options.accessKeyId ?? getEnvWithDefault("", "AGENTRUN_ACCESS_KEY_ID", "ALIBABA_CLOUD_ACCESS_KEY_ID");
|
|
263
|
+
this._accessKeySecret = options.accessKeySecret ?? getEnvWithDefault("", "AGENTRUN_ACCESS_KEY_SECRET", "ALIBABA_CLOUD_ACCESS_KEY_SECRET");
|
|
264
|
+
this._securityToken = options.securityToken ?? getEnvWithDefault("", "AGENTRUN_SECURITY_TOKEN", "ALIBABA_CLOUD_SECURITY_TOKEN");
|
|
265
|
+
this._accountId = options.accountId ?? getEnvWithDefault("", "AGENTRUN_ACCOUNT_ID", "FC_ACCOUNT_ID");
|
|
266
|
+
this._token = options.token;
|
|
267
|
+
this._regionId = options.regionId ?? getEnvWithDefault("cn-hangzhou", "AGENTRUN_REGION", "FC_REGION");
|
|
268
|
+
this._timeout = options.timeout ?? 6e5;
|
|
269
|
+
this._readTimeout = options.readTimeout ?? 1e8;
|
|
270
|
+
this._controlEndpoint = options.controlEndpoint ?? getEnvWithDefault("", "AGENTRUN_CONTROL_ENDPOINT");
|
|
271
|
+
this._dataEndpoint = options.dataEndpoint ?? getEnvWithDefault("", "AGENTRUN_DATA_ENDPOINT");
|
|
272
|
+
this._devsEndpoint = options.devsEndpoint ?? getEnvWithDefault("", "DEVS_ENDPOINT");
|
|
273
|
+
this._headers = options.headers ?? {};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Create a new Config by merging multiple configs.
|
|
277
|
+
* Later configs take precedence.
|
|
278
|
+
*/
|
|
279
|
+
static withConfigs(...configs) {
|
|
280
|
+
return new _Config().update(...configs);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Update this config with values from other configs.
|
|
284
|
+
* Non-undefined values from later configs take precedence.
|
|
285
|
+
*/
|
|
286
|
+
update(...configs) {
|
|
287
|
+
for (const config2 of configs) {
|
|
288
|
+
if (!config2) continue;
|
|
289
|
+
if (config2._accessKeyId) this._accessKeyId = config2._accessKeyId;
|
|
290
|
+
if (config2._accessKeySecret) this._accessKeySecret = config2._accessKeySecret;
|
|
291
|
+
if (config2._securityToken) this._securityToken = config2._securityToken;
|
|
292
|
+
if (config2._accountId) this._accountId = config2._accountId;
|
|
293
|
+
if (config2._token) this._token = config2._token;
|
|
294
|
+
if (config2._regionId) this._regionId = config2._regionId;
|
|
295
|
+
if (config2._timeout) this._timeout = config2._timeout;
|
|
296
|
+
if (config2._readTimeout) this._readTimeout = config2._readTimeout;
|
|
297
|
+
if (config2._controlEndpoint) this._controlEndpoint = config2._controlEndpoint;
|
|
298
|
+
if (config2._dataEndpoint) this._dataEndpoint = config2._dataEndpoint;
|
|
299
|
+
if (config2._devsEndpoint) this._devsEndpoint = config2._devsEndpoint;
|
|
300
|
+
if (config2._headers && Object.keys(config2._headers).length > 0) {
|
|
301
|
+
this._headers = { ...this._headers, ...config2._headers };
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
get accessKeyId() {
|
|
307
|
+
return this._accessKeyId;
|
|
308
|
+
}
|
|
309
|
+
get accessKeySecret() {
|
|
310
|
+
return this._accessKeySecret;
|
|
311
|
+
}
|
|
312
|
+
get securityToken() {
|
|
313
|
+
return this._securityToken;
|
|
314
|
+
}
|
|
315
|
+
get accountId() {
|
|
316
|
+
if (!this._accountId) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
"Account ID is not set. Please add AGENTRUN_ACCOUNT_ID environment variable or set it in code."
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
return this._accountId;
|
|
322
|
+
}
|
|
323
|
+
get token() {
|
|
324
|
+
return this._token;
|
|
325
|
+
}
|
|
326
|
+
get regionId() {
|
|
327
|
+
return this._regionId || "cn-hangzhou";
|
|
328
|
+
}
|
|
329
|
+
get timeout() {
|
|
330
|
+
return this._timeout || 6e5;
|
|
331
|
+
}
|
|
332
|
+
get readTimeout() {
|
|
333
|
+
return this._readTimeout || 1e8;
|
|
334
|
+
}
|
|
335
|
+
get controlEndpoint() {
|
|
336
|
+
if (this._controlEndpoint) {
|
|
337
|
+
return this._controlEndpoint;
|
|
338
|
+
}
|
|
339
|
+
return `https://agentrun.${this.regionId}.aliyuncs.com`;
|
|
340
|
+
}
|
|
341
|
+
get dataEndpoint() {
|
|
342
|
+
if (this._dataEndpoint) {
|
|
343
|
+
return this._dataEndpoint;
|
|
344
|
+
}
|
|
345
|
+
return `https://${this.accountId}.agentrun-data.${this.regionId}.aliyuncs.com`;
|
|
346
|
+
}
|
|
347
|
+
get devsEndpoint() {
|
|
348
|
+
if (this._devsEndpoint) {
|
|
349
|
+
return this._devsEndpoint;
|
|
350
|
+
}
|
|
351
|
+
return `https://devs.${this.regionId}.aliyuncs.com`;
|
|
352
|
+
}
|
|
353
|
+
get headers() {
|
|
354
|
+
return this._headers;
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
// src/utils/exception.ts
|
|
361
|
+
var AgentRunError, HTTPError, ClientError, ServerError, ResourceNotExistError, ResourceAlreadyExistError;
|
|
362
|
+
var init_exception = __esm({
|
|
363
|
+
"src/utils/exception.ts"() {
|
|
364
|
+
AgentRunError = class _AgentRunError extends Error {
|
|
365
|
+
constructor(message) {
|
|
366
|
+
super(message);
|
|
367
|
+
this.name = "AgentRunError";
|
|
368
|
+
Object.setPrototypeOf(this, _AgentRunError.prototype);
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
HTTPError = class _HTTPError extends AgentRunError {
|
|
372
|
+
statusCode;
|
|
373
|
+
requestId;
|
|
374
|
+
errorCode;
|
|
375
|
+
constructor(statusCode, message, options) {
|
|
376
|
+
super(message);
|
|
377
|
+
this.name = "HTTPError";
|
|
378
|
+
this.statusCode = statusCode;
|
|
379
|
+
this.requestId = options?.requestId;
|
|
380
|
+
this.errorCode = options?.errorCode;
|
|
381
|
+
Object.setPrototypeOf(this, _HTTPError.prototype);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Convert HTTP error to resource-specific error based on status code
|
|
385
|
+
*/
|
|
386
|
+
toResourceError(resourceType, resourceId) {
|
|
387
|
+
if (this.statusCode == 404) return new ResourceNotExistError(resourceType, resourceId);
|
|
388
|
+
else if (this.statusCode == 409) return new ResourceAlreadyExistError(resourceType, resourceId);
|
|
389
|
+
else if ((this.statusCode == 400 || this.statusCode == 500) && this.message.includes("already exists"))
|
|
390
|
+
return new ResourceAlreadyExistError(resourceType, resourceId);
|
|
391
|
+
else return this;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
ClientError = class _ClientError extends HTTPError {
|
|
395
|
+
constructor(statusCode, message, options) {
|
|
396
|
+
super(statusCode, message, options);
|
|
397
|
+
this.name = "ClientError";
|
|
398
|
+
Object.setPrototypeOf(this, _ClientError.prototype);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
ServerError = class _ServerError extends HTTPError {
|
|
402
|
+
constructor(statusCode, message, options) {
|
|
403
|
+
super(statusCode, message, options);
|
|
404
|
+
this.name = "ServerError";
|
|
405
|
+
Object.setPrototypeOf(this, _ServerError.prototype);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
ResourceNotExistError = class _ResourceNotExistError extends ClientError {
|
|
409
|
+
resourceType;
|
|
410
|
+
resourceId;
|
|
411
|
+
constructor(resourceType, resourceId, options) {
|
|
412
|
+
const message = resourceId ? `${resourceType} '${resourceId}' does not exist` : `${resourceType} does not exist`;
|
|
413
|
+
super(404, message, options);
|
|
414
|
+
this.name = "ResourceNotExistError";
|
|
415
|
+
this.resourceType = resourceType;
|
|
416
|
+
this.resourceId = resourceId;
|
|
417
|
+
Object.setPrototypeOf(this, _ResourceNotExistError.prototype);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
ResourceAlreadyExistError = class _ResourceAlreadyExistError extends ClientError {
|
|
421
|
+
resourceType;
|
|
422
|
+
resourceId;
|
|
423
|
+
constructor(resourceType, resourceId, options) {
|
|
424
|
+
const message = resourceId ? `${resourceType} '${resourceId}' already exists` : `${resourceType} already exists`;
|
|
425
|
+
super(409, message, options);
|
|
426
|
+
this.name = "ResourceAlreadyExistError";
|
|
427
|
+
this.resourceType = resourceType;
|
|
428
|
+
this.resourceId = resourceId;
|
|
429
|
+
Object.setPrototypeOf(this, _ResourceAlreadyExistError.prototype);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
var $DevsClient, ToolControlAPI;
|
|
435
|
+
var init_control = __esm({
|
|
436
|
+
"src/toolset/api/control.ts"() {
|
|
437
|
+
init_config();
|
|
438
|
+
init_exception();
|
|
439
|
+
init_log();
|
|
440
|
+
$DevsClient = // @ts-expect-error - ESM interop: default.default exists when imported as ESM namespace
|
|
441
|
+
$Devs.default?.default ?? $Devs.default ?? $Devs;
|
|
442
|
+
ToolControlAPI = class {
|
|
443
|
+
config;
|
|
444
|
+
/**
|
|
445
|
+
* Initialize API client
|
|
446
|
+
* 初始化 API 客户端
|
|
447
|
+
*
|
|
448
|
+
* @param config - Global configuration object / 全局配置对象
|
|
449
|
+
*/
|
|
450
|
+
constructor(config2) {
|
|
451
|
+
this.config = config2;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Get DevS client
|
|
455
|
+
* 获取 DevS 客户端
|
|
456
|
+
*/
|
|
457
|
+
getDevsClient(config2) {
|
|
458
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
459
|
+
let endpoint = cfg.devsEndpoint;
|
|
460
|
+
if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
|
|
461
|
+
endpoint = endpoint.split("://")[1];
|
|
462
|
+
}
|
|
463
|
+
const openApiConfig = new $OpenApi.Config({
|
|
464
|
+
accessKeyId: cfg.accessKeyId,
|
|
465
|
+
accessKeySecret: cfg.accessKeySecret,
|
|
466
|
+
securityToken: cfg.securityToken || void 0,
|
|
467
|
+
regionId: cfg.regionId,
|
|
468
|
+
endpoint,
|
|
469
|
+
connectTimeout: cfg.timeout
|
|
470
|
+
});
|
|
471
|
+
return new $DevsClient(openApiConfig);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Get toolset
|
|
475
|
+
* 获取工具
|
|
476
|
+
*
|
|
477
|
+
* @param name - Tool name / Tool 名称
|
|
478
|
+
* @param headers - Request headers / 请求头
|
|
479
|
+
* @param config - Configuration / 配置
|
|
480
|
+
* @returns Toolset object / ToolSet 对象
|
|
481
|
+
* @throws AgentRuntimeError - Throws on call failure / 调用失败时抛出
|
|
482
|
+
* @throws ClientError - Client error / 客户端错误
|
|
483
|
+
* @throws ServerError - Server error / 服务器错误
|
|
484
|
+
*/
|
|
485
|
+
getToolset = async (params) => {
|
|
486
|
+
const { name, headers, config: config2 } = params;
|
|
487
|
+
try {
|
|
488
|
+
const client = this.getDevsClient(config2);
|
|
489
|
+
const runtime = new $Util.RuntimeOptions({});
|
|
490
|
+
const response = await client.getToolsetWithOptions(name, headers || {}, runtime);
|
|
491
|
+
logger.debug(`API getToolset called, Request ID: ${response.body?.requestId}`);
|
|
492
|
+
if (!response.body) {
|
|
493
|
+
throw new Error("Empty response body");
|
|
494
|
+
}
|
|
495
|
+
return response.body;
|
|
496
|
+
} catch (error) {
|
|
497
|
+
if (error instanceof HTTPError) {
|
|
498
|
+
throw error.toResourceError("ToolSet", name);
|
|
499
|
+
}
|
|
500
|
+
this.handleError(error, name);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
/**
|
|
504
|
+
* List toolsets
|
|
505
|
+
* 枚举 ToolSets
|
|
506
|
+
*
|
|
507
|
+
* @param input - List configuration / 枚举的配置
|
|
508
|
+
* @param headers - Request headers / 请求头
|
|
509
|
+
* @param config - Configuration / 配置
|
|
510
|
+
* @returns ToolSet list / ToolSet 列表
|
|
511
|
+
* @throws AgentRuntimeError - Throws on call failure / 调用失败时抛出
|
|
512
|
+
* @throws ClientError - Client error / 客户端错误
|
|
513
|
+
* @throws ServerError - Server error / 服务器错误
|
|
514
|
+
*/
|
|
515
|
+
listToolsets = async (params) => {
|
|
516
|
+
const { input, headers, config: config2 } = params;
|
|
517
|
+
try {
|
|
518
|
+
const client = this.getDevsClient(config2);
|
|
519
|
+
const runtime = new $Util.RuntimeOptions({});
|
|
520
|
+
const response = await client.listToolsetsWithOptions(input, headers || {}, runtime);
|
|
521
|
+
logger.debug(
|
|
522
|
+
`API listToolsets called, Request ID: ${response?.headers?.["x-acs-request-id"]}`
|
|
523
|
+
);
|
|
524
|
+
if (!response.body) {
|
|
525
|
+
throw new Error("Empty response body");
|
|
526
|
+
}
|
|
527
|
+
return response.body;
|
|
528
|
+
} catch (error) {
|
|
529
|
+
this.handleError(error);
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
/**
|
|
533
|
+
* Create toolset
|
|
534
|
+
* 创建工具集
|
|
535
|
+
*
|
|
536
|
+
* @param input - Toolset input / ToolSet 输入
|
|
537
|
+
* @param headers - Request headers / 请求头
|
|
538
|
+
* @param config - Configuration / 配置
|
|
539
|
+
* @returns Toolset object / ToolSet 对象
|
|
540
|
+
*/
|
|
541
|
+
createToolset = async (params) => {
|
|
542
|
+
const { input, headers, config: config2 } = params;
|
|
543
|
+
try {
|
|
544
|
+
const client = this.getDevsClient(config2);
|
|
545
|
+
const runtime = new $Util.RuntimeOptions({});
|
|
546
|
+
const response = await client.createToolsetWithOptions(
|
|
547
|
+
new $Devs.CreateToolsetRequest({ body: input }),
|
|
548
|
+
headers || {},
|
|
549
|
+
runtime
|
|
550
|
+
);
|
|
551
|
+
logger.debug(`API createToolset called, Request ID: ${response.body?.requestId}`);
|
|
552
|
+
if (!response.body) {
|
|
553
|
+
throw new Error("Empty response body");
|
|
554
|
+
}
|
|
555
|
+
return response.body;
|
|
556
|
+
} catch (error) {
|
|
557
|
+
this.handleError(error, input?.name);
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
/**
|
|
561
|
+
* Update toolset
|
|
562
|
+
* 更新工具集
|
|
563
|
+
*
|
|
564
|
+
* @param name - Toolset name / ToolSet 名称
|
|
565
|
+
* @param input - Toolset input / ToolSet 输入
|
|
566
|
+
* @param headers - Request headers / 请求头
|
|
567
|
+
* @param config - Configuration / 配置
|
|
568
|
+
* @returns Toolset object / ToolSet 对象
|
|
569
|
+
*/
|
|
570
|
+
updateToolset = async (params) => {
|
|
571
|
+
const { name, input, headers, config: config2 } = params;
|
|
572
|
+
try {
|
|
573
|
+
const client = this.getDevsClient(config2);
|
|
574
|
+
const runtime = new $Util.RuntimeOptions({});
|
|
575
|
+
const response = await client.updateToolsetWithOptions(
|
|
576
|
+
name,
|
|
577
|
+
new $Devs.UpdateToolsetRequest({ body: input }),
|
|
578
|
+
headers || {},
|
|
579
|
+
runtime
|
|
580
|
+
);
|
|
581
|
+
logger.debug(`API updateToolset called, Request ID: ${response.body?.requestId}`);
|
|
582
|
+
if (!response.body) {
|
|
583
|
+
throw new Error("Empty response body");
|
|
584
|
+
}
|
|
585
|
+
return response.body;
|
|
586
|
+
} catch (error) {
|
|
587
|
+
this.handleError(error, name);
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
/**
|
|
591
|
+
* Delete toolset
|
|
592
|
+
* 删除工具集
|
|
593
|
+
*
|
|
594
|
+
* @param name - Toolset name / ToolSet 名称
|
|
595
|
+
* @param headers - Request headers / 请求头
|
|
596
|
+
* @param config - Configuration / 配置
|
|
597
|
+
* @returns Toolset object / ToolSet 对象
|
|
598
|
+
*/
|
|
599
|
+
deleteToolset = async (params) => {
|
|
600
|
+
const { name, headers, config: config2 } = params;
|
|
601
|
+
try {
|
|
602
|
+
const client = this.getDevsClient(config2);
|
|
603
|
+
const runtime = new $Util.RuntimeOptions({});
|
|
604
|
+
const response = await client.deleteToolsetWithOptions(name, headers || {}, runtime);
|
|
605
|
+
logger.debug(`API deleteToolset called, Request ID: ${response.body?.requestId}`);
|
|
606
|
+
if (!response.body) {
|
|
607
|
+
throw new Error("Empty response body");
|
|
608
|
+
}
|
|
609
|
+
return response.body;
|
|
610
|
+
} catch (error) {
|
|
611
|
+
this.handleError(error, name);
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
/**
|
|
615
|
+
* Handle API errors
|
|
616
|
+
* 处理 API 错误
|
|
617
|
+
*/
|
|
618
|
+
handleError(error, _resourceName) {
|
|
619
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
620
|
+
const e = error;
|
|
621
|
+
const statusCode = e.statusCode;
|
|
622
|
+
const message = e.message || "Unknown error";
|
|
623
|
+
const requestId = e.data?.requestId;
|
|
624
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
625
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
626
|
+
} else if (statusCode >= 500) {
|
|
627
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
// src/toolset/api/mcp.ts
|
|
637
|
+
var mcp_exports = {};
|
|
638
|
+
__export(mcp_exports, {
|
|
639
|
+
MCPSession: () => MCPSession,
|
|
640
|
+
MCPToolSet: () => MCPToolSet
|
|
641
|
+
});
|
|
642
|
+
var MCPSession, MCPToolSet;
|
|
643
|
+
var init_mcp = __esm({
|
|
644
|
+
"src/toolset/api/mcp.ts"() {
|
|
645
|
+
init_config();
|
|
646
|
+
init_log();
|
|
647
|
+
MCPSession = class {
|
|
648
|
+
url;
|
|
649
|
+
config;
|
|
650
|
+
client;
|
|
651
|
+
// Will be typed when @modelcontextprotocol/sdk is installed
|
|
652
|
+
clientSession;
|
|
653
|
+
constructor(url, config2) {
|
|
654
|
+
this.url = url;
|
|
655
|
+
this.config = Config.withConfigs(config2);
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Create and initialize the session
|
|
659
|
+
* 创建并初始化会话
|
|
660
|
+
*/
|
|
661
|
+
connect = async (_params) => {
|
|
662
|
+
try {
|
|
663
|
+
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');
|
|
664
|
+
const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js');
|
|
665
|
+
const transport = new SSEClientTransport(new URL(this.url));
|
|
666
|
+
this.client = new Client(
|
|
667
|
+
{
|
|
668
|
+
name: "agentrun-nodejs-client",
|
|
669
|
+
version: "1.0.0"
|
|
670
|
+
},
|
|
671
|
+
{
|
|
672
|
+
capabilities: {}
|
|
673
|
+
}
|
|
674
|
+
);
|
|
675
|
+
await this.client.connect(transport);
|
|
676
|
+
return this.client;
|
|
677
|
+
} catch (error) {
|
|
678
|
+
logger.error("Failed to create MCP session:", error);
|
|
679
|
+
throw error;
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
/**
|
|
683
|
+
* Close the session
|
|
684
|
+
* 关闭会话
|
|
685
|
+
*/
|
|
686
|
+
close = async (_params) => {
|
|
687
|
+
if (this.client) {
|
|
688
|
+
try {
|
|
689
|
+
await this.client.close();
|
|
690
|
+
} catch (error) {
|
|
691
|
+
logger.error("Failed to close MCP session:", error);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
/**
|
|
696
|
+
* Get toolsets
|
|
697
|
+
* 获取工具集
|
|
698
|
+
*/
|
|
699
|
+
toolsets(config2) {
|
|
700
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
701
|
+
return new MCPToolSet(this.url + "/toolsets", cfg);
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
MCPToolSet = class {
|
|
705
|
+
url;
|
|
706
|
+
config;
|
|
707
|
+
constructor(url, config2) {
|
|
708
|
+
this.url = url;
|
|
709
|
+
this.config = Config.withConfigs(config2);
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Create a new session
|
|
713
|
+
* 创建新会话
|
|
714
|
+
*/
|
|
715
|
+
newSession(config2) {
|
|
716
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
717
|
+
return new MCPSession(this.url, cfg);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* List available tools (async)
|
|
721
|
+
* 列出可用工具(异步)
|
|
722
|
+
*/
|
|
723
|
+
toolsAsync = async (params) => {
|
|
724
|
+
const session = this.newSession(params?.config);
|
|
725
|
+
try {
|
|
726
|
+
const client = await session.connect();
|
|
727
|
+
const result = await client.listTools();
|
|
728
|
+
return result.tools || [];
|
|
729
|
+
} finally {
|
|
730
|
+
await session.close();
|
|
731
|
+
}
|
|
732
|
+
};
|
|
733
|
+
/**
|
|
734
|
+
* List available tools (sync wrapper)
|
|
735
|
+
* 列出可用工具(同步包装)
|
|
736
|
+
*
|
|
737
|
+
* Note: This is a convenience method that wraps the async version.
|
|
738
|
+
* In Node.js, prefer using toolsAsync() directly.
|
|
739
|
+
*/
|
|
740
|
+
tools(config2) {
|
|
741
|
+
return this.toolsAsync({ config: config2 });
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Call a tool (async)
|
|
745
|
+
* 调用工具(异步)
|
|
746
|
+
*/
|
|
747
|
+
callToolAsync = async (name, args, config2) => {
|
|
748
|
+
const session = this.newSession(config2);
|
|
749
|
+
try {
|
|
750
|
+
const client = await session.connect();
|
|
751
|
+
const result = await client.callTool({
|
|
752
|
+
name,
|
|
753
|
+
arguments: args || {}
|
|
754
|
+
});
|
|
755
|
+
if (result.content && Array.isArray(result.content)) {
|
|
756
|
+
return result.content.map((item) => {
|
|
757
|
+
if (typeof item === "object" && item !== null) {
|
|
758
|
+
if (typeof item.toJSON === "function") {
|
|
759
|
+
return item.toJSON();
|
|
760
|
+
}
|
|
761
|
+
return { ...item };
|
|
762
|
+
}
|
|
763
|
+
return item;
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
return result.content || [];
|
|
767
|
+
} finally {
|
|
768
|
+
await session.close();
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
/**
|
|
772
|
+
* Call a tool (sync wrapper)
|
|
773
|
+
* 调用工具(同步包装)
|
|
774
|
+
*
|
|
775
|
+
* Note: This is a convenience method that wraps the async version.
|
|
776
|
+
* In Node.js, prefer using callToolAsync() directly.
|
|
777
|
+
*/
|
|
778
|
+
callTool(name, args, config2) {
|
|
779
|
+
return this.callToolAsync(name, args, config2);
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
// src/toolset/api/index.ts
|
|
786
|
+
var init_api = __esm({
|
|
787
|
+
"src/toolset/api/index.ts"() {
|
|
788
|
+
init_version_check();
|
|
789
|
+
init_control();
|
|
790
|
+
init_mcp();
|
|
791
|
+
}
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
// src/utils/model.ts
|
|
795
|
+
var init_model = __esm({
|
|
796
|
+
"src/utils/model.ts"() {
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
// src/utils/resource.ts
|
|
801
|
+
function updateObjectProperties(target, source) {
|
|
802
|
+
for (const key in source) {
|
|
803
|
+
if (Object.prototype.hasOwnProperty.call(source, key) && typeof source[key] !== "function" && !key.startsWith("_")) {
|
|
804
|
+
target[key] = source[key];
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
function listAllResourcesFunction(list) {
|
|
809
|
+
return async (params) => {
|
|
810
|
+
const { config: config2, ...restParams } = params ?? {};
|
|
811
|
+
const allResults = [];
|
|
812
|
+
let page = 1;
|
|
813
|
+
const pageSize = 50;
|
|
814
|
+
while (true) {
|
|
815
|
+
const pageResults = await list({
|
|
816
|
+
input: {
|
|
817
|
+
...restParams,
|
|
818
|
+
pageNumber: page,
|
|
819
|
+
pageSize
|
|
820
|
+
},
|
|
821
|
+
config: config2
|
|
822
|
+
});
|
|
823
|
+
page += 1;
|
|
824
|
+
allResults.push(...pageResults);
|
|
825
|
+
if (pageResults.length < pageSize) break;
|
|
826
|
+
}
|
|
827
|
+
const resultSet = /* @__PURE__ */ new Set();
|
|
828
|
+
const results = [];
|
|
829
|
+
for (const item of allResults) {
|
|
830
|
+
const uniqId = item.uniqIdCallback() || "";
|
|
831
|
+
if (!resultSet.has(uniqId) && uniqId) {
|
|
832
|
+
resultSet.add(uniqId);
|
|
833
|
+
results.push(item);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
return results;
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
var init_resource = __esm({
|
|
840
|
+
"src/utils/resource.ts"() {
|
|
841
|
+
init_exception();
|
|
842
|
+
init_log();
|
|
843
|
+
init_model();
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
// src/toolset/model.ts
|
|
848
|
+
var model_exports = {};
|
|
849
|
+
__export(model_exports, {
|
|
850
|
+
ToolInfo: () => ToolInfo,
|
|
851
|
+
ToolSchema: () => ToolSchema,
|
|
852
|
+
ToolSetSchemaType: () => ToolSetSchemaType
|
|
853
|
+
});
|
|
854
|
+
var ToolSetSchemaType, ToolSchema, ToolInfo;
|
|
855
|
+
var init_model2 = __esm({
|
|
856
|
+
"src/toolset/model.ts"() {
|
|
857
|
+
ToolSetSchemaType = /* @__PURE__ */ ((ToolSetSchemaType2) => {
|
|
858
|
+
ToolSetSchemaType2["OPENAPI"] = "OpenAPI";
|
|
859
|
+
ToolSetSchemaType2["MCP"] = "MCP";
|
|
860
|
+
return ToolSetSchemaType2;
|
|
861
|
+
})(ToolSetSchemaType || {});
|
|
862
|
+
ToolSchema = class _ToolSchema {
|
|
863
|
+
// Basic fields / 基本字段
|
|
864
|
+
type;
|
|
865
|
+
description;
|
|
866
|
+
title;
|
|
867
|
+
// Object type fields / 对象类型字段
|
|
868
|
+
properties;
|
|
869
|
+
required;
|
|
870
|
+
additionalProperties;
|
|
871
|
+
// Array type fields / 数组类型字段
|
|
872
|
+
items;
|
|
873
|
+
minItems;
|
|
874
|
+
maxItems;
|
|
875
|
+
// String type fields / 字符串类型字段
|
|
876
|
+
pattern;
|
|
877
|
+
minLength;
|
|
878
|
+
maxLength;
|
|
879
|
+
format;
|
|
880
|
+
// date, date-time, email, uri, etc.
|
|
881
|
+
enum;
|
|
882
|
+
// Number type fields / 数值类型字段
|
|
883
|
+
minimum;
|
|
884
|
+
maximum;
|
|
885
|
+
exclusiveMinimum;
|
|
886
|
+
exclusiveMaximum;
|
|
887
|
+
// Union types / 联合类型
|
|
888
|
+
anyOf;
|
|
889
|
+
oneOf;
|
|
890
|
+
allOf;
|
|
891
|
+
// Default value / 默认值
|
|
892
|
+
default;
|
|
893
|
+
constructor(data) {
|
|
894
|
+
if (data) {
|
|
895
|
+
Object.assign(this, data);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Create ToolSchema from any OpenAPI/JSON Schema
|
|
900
|
+
* 从任意 OpenAPI/JSON Schema 创建 ToolSchema
|
|
901
|
+
*
|
|
902
|
+
* Recursively parses all nested structures, preserving complete schema information.
|
|
903
|
+
* 递归解析所有嵌套结构,保留完整的 schema 信息。
|
|
904
|
+
*/
|
|
905
|
+
static fromAnyOpenAPISchema(schema) {
|
|
906
|
+
if (!schema || typeof schema !== "object") {
|
|
907
|
+
return new _ToolSchema({ type: "string" });
|
|
908
|
+
}
|
|
909
|
+
const s = schema;
|
|
910
|
+
const propertiesRaw = s.properties;
|
|
911
|
+
const properties = propertiesRaw ? Object.fromEntries(
|
|
912
|
+
Object.entries(propertiesRaw).map(([key, value]) => [
|
|
913
|
+
key,
|
|
914
|
+
_ToolSchema.fromAnyOpenAPISchema(value)
|
|
915
|
+
])
|
|
916
|
+
) : void 0;
|
|
917
|
+
const itemsRaw = s.items;
|
|
918
|
+
const items = itemsRaw ? _ToolSchema.fromAnyOpenAPISchema(itemsRaw) : void 0;
|
|
919
|
+
const anyOfRaw = s.anyOf;
|
|
920
|
+
const anyOf = anyOfRaw ? anyOfRaw.map(_ToolSchema.fromAnyOpenAPISchema) : void 0;
|
|
921
|
+
const oneOfRaw = s.oneOf;
|
|
922
|
+
const oneOf = oneOfRaw ? oneOfRaw.map(_ToolSchema.fromAnyOpenAPISchema) : void 0;
|
|
923
|
+
const allOfRaw = s.allOf;
|
|
924
|
+
const allOf = allOfRaw ? allOfRaw.map(_ToolSchema.fromAnyOpenAPISchema) : void 0;
|
|
925
|
+
return new _ToolSchema({
|
|
926
|
+
// Basic fields
|
|
927
|
+
type: s.type,
|
|
928
|
+
description: s.description,
|
|
929
|
+
title: s.title,
|
|
930
|
+
// Object type
|
|
931
|
+
properties,
|
|
932
|
+
required: s.required,
|
|
933
|
+
additionalProperties: s.additionalProperties,
|
|
934
|
+
// Array type
|
|
935
|
+
items,
|
|
936
|
+
minItems: s.minItems,
|
|
937
|
+
maxItems: s.maxItems,
|
|
938
|
+
// String type
|
|
939
|
+
pattern: s.pattern,
|
|
940
|
+
minLength: s.minLength,
|
|
941
|
+
maxLength: s.maxLength,
|
|
942
|
+
format: s.format,
|
|
943
|
+
enum: s.enum,
|
|
944
|
+
// Number type
|
|
945
|
+
minimum: s.minimum,
|
|
946
|
+
maximum: s.maximum,
|
|
947
|
+
exclusiveMinimum: s.exclusiveMinimum,
|
|
948
|
+
exclusiveMaximum: s.exclusiveMaximum,
|
|
949
|
+
// Union types
|
|
950
|
+
anyOf,
|
|
951
|
+
oneOf,
|
|
952
|
+
allOf,
|
|
953
|
+
// Default value
|
|
954
|
+
default: s.default
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Convert to standard JSON Schema format
|
|
959
|
+
* 转换为标准 JSON Schema 格式
|
|
960
|
+
*/
|
|
961
|
+
toJSONSchema() {
|
|
962
|
+
const result = {};
|
|
963
|
+
if (this.type !== void 0) result.type = this.type;
|
|
964
|
+
if (this.description !== void 0) result.description = this.description;
|
|
965
|
+
if (this.title !== void 0) result.title = this.title;
|
|
966
|
+
if (this.properties) {
|
|
967
|
+
result.properties = Object.fromEntries(
|
|
968
|
+
Object.entries(this.properties).map(([k, v]) => [k, v.toJSONSchema()])
|
|
969
|
+
);
|
|
970
|
+
}
|
|
971
|
+
if (this.required) result.required = this.required;
|
|
972
|
+
if (this.additionalProperties !== void 0) {
|
|
973
|
+
result.additionalProperties = this.additionalProperties;
|
|
974
|
+
}
|
|
975
|
+
if (this.items) result.items = this.items.toJSONSchema();
|
|
976
|
+
if (this.minItems !== void 0) result.minItems = this.minItems;
|
|
977
|
+
if (this.maxItems !== void 0) result.maxItems = this.maxItems;
|
|
978
|
+
if (this.pattern) result.pattern = this.pattern;
|
|
979
|
+
if (this.minLength !== void 0) result.minLength = this.minLength;
|
|
980
|
+
if (this.maxLength !== void 0) result.maxLength = this.maxLength;
|
|
981
|
+
if (this.format) result.format = this.format;
|
|
982
|
+
if (this.enum) result.enum = this.enum;
|
|
983
|
+
if (this.minimum !== void 0) result.minimum = this.minimum;
|
|
984
|
+
if (this.maximum !== void 0) result.maximum = this.maximum;
|
|
985
|
+
if (this.exclusiveMinimum !== void 0) {
|
|
986
|
+
result.exclusiveMinimum = this.exclusiveMinimum;
|
|
987
|
+
}
|
|
988
|
+
if (this.exclusiveMaximum !== void 0) {
|
|
989
|
+
result.exclusiveMaximum = this.exclusiveMaximum;
|
|
990
|
+
}
|
|
991
|
+
if (this.anyOf) result.anyOf = this.anyOf.map((s) => s.toJSONSchema());
|
|
992
|
+
if (this.oneOf) result.oneOf = this.oneOf.map((s) => s.toJSONSchema());
|
|
993
|
+
if (this.allOf) result.allOf = this.allOf.map((s) => s.toJSONSchema());
|
|
994
|
+
if (this.default !== void 0) result.default = this.default;
|
|
995
|
+
return result;
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
ToolInfo = class _ToolInfo {
|
|
999
|
+
name;
|
|
1000
|
+
description;
|
|
1001
|
+
parameters;
|
|
1002
|
+
constructor(data) {
|
|
1003
|
+
if (data) {
|
|
1004
|
+
this.name = data.name;
|
|
1005
|
+
this.description = data.description;
|
|
1006
|
+
this.parameters = data.parameters;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* Create ToolInfo from MCP tool
|
|
1011
|
+
* 从 MCP tool 创建 ToolInfo
|
|
1012
|
+
*/
|
|
1013
|
+
static fromMCPTool(tool) {
|
|
1014
|
+
if (!tool) {
|
|
1015
|
+
throw new Error("MCP tool is required");
|
|
1016
|
+
}
|
|
1017
|
+
let toolName;
|
|
1018
|
+
let toolDescription;
|
|
1019
|
+
let inputSchema;
|
|
1020
|
+
if (typeof tool === "object" && tool !== null) {
|
|
1021
|
+
const t = tool;
|
|
1022
|
+
if ("name" in t) {
|
|
1023
|
+
toolName = t.name;
|
|
1024
|
+
toolDescription = t.description;
|
|
1025
|
+
inputSchema = t.inputSchema || t.input_schema;
|
|
1026
|
+
} else {
|
|
1027
|
+
throw new Error(`Unsupported MCP tool format: ${typeof tool}`);
|
|
1028
|
+
}
|
|
1029
|
+
} else {
|
|
1030
|
+
throw new Error(`Unsupported MCP tool format: ${typeof tool}`);
|
|
1031
|
+
}
|
|
1032
|
+
if (!toolName) {
|
|
1033
|
+
throw new Error("MCP tool must have a name");
|
|
1034
|
+
}
|
|
1035
|
+
let parameters;
|
|
1036
|
+
if (inputSchema) {
|
|
1037
|
+
parameters = ToolSchema.fromAnyOpenAPISchema(inputSchema);
|
|
1038
|
+
}
|
|
1039
|
+
return new _ToolInfo({
|
|
1040
|
+
name: toolName,
|
|
1041
|
+
description: toolDescription,
|
|
1042
|
+
parameters: parameters || new ToolSchema({ type: "object", properties: {} })
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
// src/toolset/openapi.ts
|
|
1050
|
+
var openapi_exports = {};
|
|
1051
|
+
__export(openapi_exports, {
|
|
1052
|
+
ApiSet: () => ApiSet,
|
|
1053
|
+
OpenAPI: () => OpenAPI
|
|
1054
|
+
});
|
|
1055
|
+
var OpenAPI, ApiSet;
|
|
1056
|
+
var init_openapi = __esm({
|
|
1057
|
+
"src/toolset/openapi.ts"() {
|
|
1058
|
+
init_config();
|
|
1059
|
+
init_log();
|
|
1060
|
+
OpenAPI = class {
|
|
1061
|
+
_schema;
|
|
1062
|
+
_baseUrl;
|
|
1063
|
+
_defaultHeaders;
|
|
1064
|
+
_defaultQueryParams;
|
|
1065
|
+
_config;
|
|
1066
|
+
_tools = /* @__PURE__ */ new Map();
|
|
1067
|
+
constructor(options) {
|
|
1068
|
+
if (typeof options.schema === "string") {
|
|
1069
|
+
try {
|
|
1070
|
+
this._schema = YAML.load(options.schema);
|
|
1071
|
+
} catch {
|
|
1072
|
+
this._schema = JSON.parse(options.schema);
|
|
1073
|
+
}
|
|
1074
|
+
} else {
|
|
1075
|
+
this._schema = JSON.parse(JSON.stringify(options.schema));
|
|
1076
|
+
}
|
|
1077
|
+
this._resolveAllRefs();
|
|
1078
|
+
this._baseUrl = options.baseUrl || this._extractBaseUrl();
|
|
1079
|
+
this._defaultHeaders = options.headers ? { ...options.headers } : {};
|
|
1080
|
+
this._defaultQueryParams = options.queryParams ? { ...options.queryParams } : {};
|
|
1081
|
+
this._config = options.config;
|
|
1082
|
+
this._parseTools();
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Get the parsed schema
|
|
1086
|
+
*/
|
|
1087
|
+
get schema() {
|
|
1088
|
+
return this._schema;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Get all parsed tools
|
|
1092
|
+
*/
|
|
1093
|
+
get tools() {
|
|
1094
|
+
return Array.from(this._tools.values());
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Get a specific tool by name
|
|
1098
|
+
*/
|
|
1099
|
+
getTool(name) {
|
|
1100
|
+
return this._tools.get(name);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Invoke a tool
|
|
1104
|
+
*/
|
|
1105
|
+
invokeToolAsync = async (name, args = {}, _config) => {
|
|
1106
|
+
const tool = this._tools.get(name);
|
|
1107
|
+
if (!tool) {
|
|
1108
|
+
throw new Error(`Tool '${name}' not found.`);
|
|
1109
|
+
}
|
|
1110
|
+
const method = (tool.method || "get").toUpperCase();
|
|
1111
|
+
let path = tool.path || "/";
|
|
1112
|
+
const queryParams = {
|
|
1113
|
+
...this._defaultQueryParams
|
|
1114
|
+
};
|
|
1115
|
+
const headerParams = { ...this._defaultHeaders };
|
|
1116
|
+
let bodyContent = void 0;
|
|
1117
|
+
for (const [key, value] of Object.entries(args)) {
|
|
1118
|
+
if (key === "body") {
|
|
1119
|
+
bodyContent = value;
|
|
1120
|
+
} else if (path.includes(`{${key}}`)) {
|
|
1121
|
+
path = path.replace(`{${key}}`, String(value));
|
|
1122
|
+
} else if (typeof value === "string" && key.startsWith("X-")) {
|
|
1123
|
+
headerParams[key] = value;
|
|
1124
|
+
} else {
|
|
1125
|
+
queryParams[key] = value;
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
const url = new URL(path, this._baseUrl);
|
|
1129
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
1130
|
+
if (value !== void 0 && value !== null) {
|
|
1131
|
+
url.searchParams.set(key, String(value));
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
try {
|
|
1135
|
+
const fetchOptions = {
|
|
1136
|
+
method,
|
|
1137
|
+
headers: {
|
|
1138
|
+
"Content-Type": "application/json",
|
|
1139
|
+
...headerParams
|
|
1140
|
+
}
|
|
1141
|
+
};
|
|
1142
|
+
if (bodyContent && ["POST", "PUT", "PATCH"].includes(method)) {
|
|
1143
|
+
fetchOptions.body = JSON.stringify(bodyContent);
|
|
1144
|
+
}
|
|
1145
|
+
const response = await fetch(url.toString(), fetchOptions);
|
|
1146
|
+
const responseClone = response.clone();
|
|
1147
|
+
let responseBody;
|
|
1148
|
+
try {
|
|
1149
|
+
responseBody = await response.json();
|
|
1150
|
+
} catch {
|
|
1151
|
+
responseBody = await responseClone.text();
|
|
1152
|
+
}
|
|
1153
|
+
return {
|
|
1154
|
+
status_code: response.status,
|
|
1155
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
1156
|
+
body: responseBody
|
|
1157
|
+
};
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
return {
|
|
1160
|
+
status_code: 0,
|
|
1161
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
/**
|
|
1166
|
+
* Synchronous invoke (wrapper for async)
|
|
1167
|
+
*/
|
|
1168
|
+
invokeToolSync(_name, _args = {}) {
|
|
1169
|
+
throw new Error(
|
|
1170
|
+
"Synchronous invocation is not supported in Node.js. Use invokeToolAsync instead."
|
|
1171
|
+
);
|
|
1172
|
+
}
|
|
1173
|
+
// Alias for Python compatibility
|
|
1174
|
+
invoke_tool = (name, args = {}, _config) => {
|
|
1175
|
+
return this.invokeToolAsync(name, args);
|
|
1176
|
+
};
|
|
1177
|
+
/**
|
|
1178
|
+
* Extract base URL from schema
|
|
1179
|
+
*/
|
|
1180
|
+
_extractBaseUrl() {
|
|
1181
|
+
const servers = this._schema.servers;
|
|
1182
|
+
if (servers && servers.length > 0) {
|
|
1183
|
+
return servers[0].url;
|
|
1184
|
+
}
|
|
1185
|
+
return "";
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* Resolve all $ref references in the schema
|
|
1189
|
+
*/
|
|
1190
|
+
_resolveAllRefs() {
|
|
1191
|
+
this._schema = this._resolveRefs(this._schema, this._schema);
|
|
1192
|
+
}
|
|
1193
|
+
/**
|
|
1194
|
+
* Recursively resolve $ref references
|
|
1195
|
+
*/
|
|
1196
|
+
_resolveRefs(obj, root) {
|
|
1197
|
+
if (obj === null || obj === void 0) {
|
|
1198
|
+
return obj;
|
|
1199
|
+
}
|
|
1200
|
+
if (Array.isArray(obj)) {
|
|
1201
|
+
return obj.map((item) => this._resolveRefs(item, root));
|
|
1202
|
+
}
|
|
1203
|
+
if (typeof obj === "object") {
|
|
1204
|
+
const result = {};
|
|
1205
|
+
const objRecord = obj;
|
|
1206
|
+
if ("$ref" in objRecord && typeof objRecord.$ref === "string") {
|
|
1207
|
+
const refPath = objRecord.$ref;
|
|
1208
|
+
const resolved = this._resolveRef(refPath, root);
|
|
1209
|
+
const resolvedDeep = this._resolveRefs(resolved, root);
|
|
1210
|
+
if (typeof resolvedDeep === "object" && resolvedDeep !== null) {
|
|
1211
|
+
Object.assign(result, resolvedDeep);
|
|
1212
|
+
}
|
|
1213
|
+
for (const [key, value] of Object.entries(objRecord)) {
|
|
1214
|
+
if (key !== "$ref") {
|
|
1215
|
+
result[key] = this._resolveRefs(value, root);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
return result;
|
|
1219
|
+
}
|
|
1220
|
+
for (const [key, value] of Object.entries(objRecord)) {
|
|
1221
|
+
result[key] = this._resolveRefs(value, root);
|
|
1222
|
+
}
|
|
1223
|
+
return result;
|
|
1224
|
+
}
|
|
1225
|
+
return obj;
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Resolve a single $ref path
|
|
1229
|
+
*/
|
|
1230
|
+
_resolveRef(refPath, root) {
|
|
1231
|
+
if (!refPath.startsWith("#/")) {
|
|
1232
|
+
logger.warn(`External $ref not supported: ${refPath}`);
|
|
1233
|
+
return {};
|
|
1234
|
+
}
|
|
1235
|
+
const path = refPath.substring(2).split("/");
|
|
1236
|
+
let current = root;
|
|
1237
|
+
for (const part of path) {
|
|
1238
|
+
if (current === null || current === void 0) {
|
|
1239
|
+
return {};
|
|
1240
|
+
}
|
|
1241
|
+
if (typeof current === "object" && !Array.isArray(current)) {
|
|
1242
|
+
current = current[part];
|
|
1243
|
+
} else {
|
|
1244
|
+
return {};
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
return current;
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Parse tools from OpenAPI paths
|
|
1251
|
+
*/
|
|
1252
|
+
_parseTools() {
|
|
1253
|
+
const paths = this._schema.paths;
|
|
1254
|
+
if (!paths) {
|
|
1255
|
+
return;
|
|
1256
|
+
}
|
|
1257
|
+
for (const [path, pathItem] of Object.entries(paths)) {
|
|
1258
|
+
if (typeof pathItem !== "object" || pathItem === null) {
|
|
1259
|
+
continue;
|
|
1260
|
+
}
|
|
1261
|
+
for (const [method, operation] of Object.entries(pathItem)) {
|
|
1262
|
+
if (!["get", "post", "put", "patch", "delete", "options", "head"].includes(method)) {
|
|
1263
|
+
continue;
|
|
1264
|
+
}
|
|
1265
|
+
if (typeof operation !== "object" || operation === null) {
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1268
|
+
const op = operation;
|
|
1269
|
+
const operationId = op.operationId;
|
|
1270
|
+
if (!operationId) {
|
|
1271
|
+
continue;
|
|
1272
|
+
}
|
|
1273
|
+
const parameters = this._parseParameters(op, path);
|
|
1274
|
+
const tool = {
|
|
1275
|
+
name: operationId,
|
|
1276
|
+
description: op.summary || op.description,
|
|
1277
|
+
parameters,
|
|
1278
|
+
operationId,
|
|
1279
|
+
method,
|
|
1280
|
+
path
|
|
1281
|
+
};
|
|
1282
|
+
this._tools.set(operationId, tool);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* Parse operation parameters into a schema
|
|
1288
|
+
*/
|
|
1289
|
+
_parseParameters(operation, _path) {
|
|
1290
|
+
const result = {
|
|
1291
|
+
type: "object",
|
|
1292
|
+
properties: {},
|
|
1293
|
+
required: []
|
|
1294
|
+
};
|
|
1295
|
+
const params = operation.parameters;
|
|
1296
|
+
if (params && Array.isArray(params)) {
|
|
1297
|
+
for (const param of params) {
|
|
1298
|
+
const name = param.name;
|
|
1299
|
+
const schema = param.schema;
|
|
1300
|
+
const required = param.required;
|
|
1301
|
+
if (!name) continue;
|
|
1302
|
+
result.properties[name] = {
|
|
1303
|
+
type: schema?.type || "string",
|
|
1304
|
+
description: param.description || void 0,
|
|
1305
|
+
...schema
|
|
1306
|
+
};
|
|
1307
|
+
if (required) {
|
|
1308
|
+
result.required.push(name);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
const requestBody = operation.requestBody;
|
|
1313
|
+
if (requestBody) {
|
|
1314
|
+
const content = requestBody.content;
|
|
1315
|
+
if (content) {
|
|
1316
|
+
const jsonContent = content["application/json"];
|
|
1317
|
+
if (jsonContent && jsonContent.schema) {
|
|
1318
|
+
result.properties["body"] = jsonContent.schema;
|
|
1319
|
+
if (requestBody.required) {
|
|
1320
|
+
result.required.push("body");
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
return result;
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
ApiSet = class _ApiSet {
|
|
1329
|
+
_tools;
|
|
1330
|
+
_invoker;
|
|
1331
|
+
// Can be OpenAPI or MCP invoker
|
|
1332
|
+
_baseUrl;
|
|
1333
|
+
_defaultHeaders;
|
|
1334
|
+
_defaultQueryParams;
|
|
1335
|
+
_baseConfig;
|
|
1336
|
+
constructor(tools, invoker, baseUrl, headers, queryParams, config2) {
|
|
1337
|
+
this._tools = new Map(tools.filter((t) => t.name).map((t) => [t.name, t]));
|
|
1338
|
+
this._invoker = invoker;
|
|
1339
|
+
this._baseUrl = baseUrl;
|
|
1340
|
+
this._defaultHeaders = headers ? { ...headers } : {};
|
|
1341
|
+
this._defaultQueryParams = queryParams ? { ...queryParams } : {};
|
|
1342
|
+
this._baseConfig = config2;
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Invoke a tool by name
|
|
1346
|
+
* 调用指定的工具
|
|
1347
|
+
*/
|
|
1348
|
+
invoke = async (name, args = {}, config2) => {
|
|
1349
|
+
if (!this._tools.has(name)) {
|
|
1350
|
+
throw new Error(`Tool '${name}' not found.`);
|
|
1351
|
+
}
|
|
1352
|
+
const convertedArgs = this._convertArguments(args);
|
|
1353
|
+
const effectiveConfig = Config.withConfigs(this._baseConfig, config2);
|
|
1354
|
+
if (typeof this._invoker.invokeToolAsync === "function") {
|
|
1355
|
+
return await this._invoker.invokeToolAsync(name, convertedArgs, effectiveConfig);
|
|
1356
|
+
} else if (typeof this._invoker.invoke_tool === "function") {
|
|
1357
|
+
return await this._invoker.invoke_tool(name, convertedArgs, effectiveConfig);
|
|
1358
|
+
} else if (typeof this._invoker === "function") {
|
|
1359
|
+
return await this._invoker(name, convertedArgs);
|
|
1360
|
+
} else {
|
|
1361
|
+
throw new Error("Invalid invoker provided.");
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
/**
|
|
1365
|
+
* Get all tools
|
|
1366
|
+
* 返回所有工具列表
|
|
1367
|
+
*/
|
|
1368
|
+
get tools() {
|
|
1369
|
+
return Array.from(this._tools.values());
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Get a specific tool
|
|
1373
|
+
* 获取指定名称的工具
|
|
1374
|
+
*/
|
|
1375
|
+
getTool(name) {
|
|
1376
|
+
return this._tools.get(name);
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* Convert arguments from framework-specific types to native JavaScript types
|
|
1380
|
+
* 将常见框架类型转换为 JavaScript 原生类型
|
|
1381
|
+
*
|
|
1382
|
+
* Purpose: Ensure we send JSON-serializable data to OpenAPI
|
|
1383
|
+
* 目的:确保我们发送到 OpenAPI 的 JSON body 是可以被序列化的
|
|
1384
|
+
*/
|
|
1385
|
+
_convertToNative(value) {
|
|
1386
|
+
if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
1387
|
+
return value;
|
|
1388
|
+
}
|
|
1389
|
+
if (Array.isArray(value)) {
|
|
1390
|
+
return value.map((item) => this._convertToNative(item));
|
|
1391
|
+
}
|
|
1392
|
+
if (typeof value === "object") {
|
|
1393
|
+
const obj = value;
|
|
1394
|
+
if (typeof obj.toJSON === "function") {
|
|
1395
|
+
try {
|
|
1396
|
+
return this._convertToNative(obj.toJSON());
|
|
1397
|
+
} catch {
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
const result = {};
|
|
1401
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
1402
|
+
result[k] = this._convertToNative(v);
|
|
1403
|
+
}
|
|
1404
|
+
return result;
|
|
1405
|
+
}
|
|
1406
|
+
return value;
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Convert arguments dictionary
|
|
1410
|
+
* 转换参数字典
|
|
1411
|
+
*/
|
|
1412
|
+
_convertArguments(args) {
|
|
1413
|
+
if (!args || typeof args !== "object") {
|
|
1414
|
+
return args;
|
|
1415
|
+
}
|
|
1416
|
+
const result = {};
|
|
1417
|
+
for (const [k, v] of Object.entries(args)) {
|
|
1418
|
+
result[k] = this._convertToNative(v);
|
|
1419
|
+
}
|
|
1420
|
+
return result;
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Create ApiSet from MCP tools
|
|
1424
|
+
* 从 MCP tools 创建 ApiSet
|
|
1425
|
+
*
|
|
1426
|
+
* @param tools - MCP tools list or single tool / MCP tools 列表或单个工具
|
|
1427
|
+
* @param mcpClient - MCP client (MCPToolSet instance) / MCP 客户端(MCPToolSet 实例)
|
|
1428
|
+
* @param config - Configuration object / 配置对象
|
|
1429
|
+
*/
|
|
1430
|
+
static fromMCPTools(params) {
|
|
1431
|
+
const { tools, mcpClient, config: config2 } = params;
|
|
1432
|
+
const toolInfos = [];
|
|
1433
|
+
let toolArray;
|
|
1434
|
+
if (Array.isArray(tools)) {
|
|
1435
|
+
toolArray = tools;
|
|
1436
|
+
} else if (tools) {
|
|
1437
|
+
toolArray = [tools];
|
|
1438
|
+
} else {
|
|
1439
|
+
toolArray = [];
|
|
1440
|
+
}
|
|
1441
|
+
for (const tool of toolArray) {
|
|
1442
|
+
if (!tool) {
|
|
1443
|
+
continue;
|
|
1444
|
+
}
|
|
1445
|
+
try {
|
|
1446
|
+
const { ToolInfo: ToolInfo2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1447
|
+
const toolInfo = ToolInfo2.fromMCPTool(tool);
|
|
1448
|
+
toolInfos.push(toolInfo);
|
|
1449
|
+
} catch (error) {
|
|
1450
|
+
logger.warn(`Failed to parse MCP tool:`, error);
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
class MCPInvoker {
|
|
1455
|
+
constructor(mcpClient2, config3) {
|
|
1456
|
+
this.mcpClient = mcpClient2;
|
|
1457
|
+
this.config = config3;
|
|
1458
|
+
}
|
|
1459
|
+
invokeToolAsync = async (name, args, config3) => {
|
|
1460
|
+
const cfg = Config.withConfigs(this.config, config3);
|
|
1461
|
+
return await this.mcpClient.callToolAsync(name, args, cfg);
|
|
1462
|
+
};
|
|
1463
|
+
}
|
|
1464
|
+
const invoker = new MCPInvoker(mcpClient, config2);
|
|
1465
|
+
return new _ApiSet(toolInfos, invoker, void 0, void 0, void 0, config2);
|
|
1466
|
+
}
|
|
1467
|
+
};
|
|
1468
|
+
}
|
|
1469
|
+
});
|
|
1470
|
+
|
|
1471
|
+
// src/toolset/toolset.ts
|
|
1472
|
+
var ToolSet;
|
|
1473
|
+
var init_toolset = __esm({
|
|
1474
|
+
"src/toolset/toolset.ts"() {
|
|
1475
|
+
init_config();
|
|
1476
|
+
init_log();
|
|
1477
|
+
init_resource();
|
|
1478
|
+
ToolSet = class _ToolSet {
|
|
1479
|
+
name;
|
|
1480
|
+
uid;
|
|
1481
|
+
kind;
|
|
1482
|
+
description;
|
|
1483
|
+
createdTime;
|
|
1484
|
+
generation;
|
|
1485
|
+
labels;
|
|
1486
|
+
spec;
|
|
1487
|
+
status;
|
|
1488
|
+
_config;
|
|
1489
|
+
constructor(data, config2) {
|
|
1490
|
+
if (data) {
|
|
1491
|
+
updateObjectProperties(this, data);
|
|
1492
|
+
}
|
|
1493
|
+
this._config = config2;
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Get DevS client
|
|
1497
|
+
*/
|
|
1498
|
+
static getClient() {
|
|
1499
|
+
const { ToolSetClient: ToolSetClient2 } = (init_client(), __toCommonJS(client_exports));
|
|
1500
|
+
return new ToolSetClient2();
|
|
1501
|
+
}
|
|
1502
|
+
uniqIdCallback = () => this.name;
|
|
1503
|
+
/**
|
|
1504
|
+
* Create a new ToolSet
|
|
1505
|
+
*/
|
|
1506
|
+
static async create(params) {
|
|
1507
|
+
const { input, config: config2 } = params;
|
|
1508
|
+
return await _ToolSet.getClient().create({ input, config: config2 });
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Delete a ToolSet by Name
|
|
1512
|
+
*/
|
|
1513
|
+
static async delete(params) {
|
|
1514
|
+
const { name, config: config2 } = params;
|
|
1515
|
+
return await _ToolSet.getClient().delete({ name, config: config2 });
|
|
1516
|
+
}
|
|
1517
|
+
/**
|
|
1518
|
+
* Get a ToolSet by Name
|
|
1519
|
+
*/
|
|
1520
|
+
static async get(params) {
|
|
1521
|
+
const { name, config: config2 } = params;
|
|
1522
|
+
return await _ToolSet.getClient().get({ name, config: config2 });
|
|
1523
|
+
}
|
|
1524
|
+
/**
|
|
1525
|
+
* List ToolSets
|
|
1526
|
+
*/
|
|
1527
|
+
static list = async (...args) => {
|
|
1528
|
+
let input;
|
|
1529
|
+
let config2;
|
|
1530
|
+
if (args.length >= 1 && "input" in args[0]) {
|
|
1531
|
+
input = args[0].input;
|
|
1532
|
+
} else {
|
|
1533
|
+
input = args[0];
|
|
1534
|
+
}
|
|
1535
|
+
if (args.length >= 1 && "config" in args[0]) {
|
|
1536
|
+
config2 = args[0].config;
|
|
1537
|
+
} else if (args.length > 1 && args[1] instanceof Config) {
|
|
1538
|
+
config2 = args[1];
|
|
1539
|
+
}
|
|
1540
|
+
return await this.getClient().list({
|
|
1541
|
+
input: {
|
|
1542
|
+
...input
|
|
1543
|
+
},
|
|
1544
|
+
config: config2
|
|
1545
|
+
});
|
|
1546
|
+
};
|
|
1547
|
+
static listAll = async (...args) => {
|
|
1548
|
+
let input;
|
|
1549
|
+
let config2;
|
|
1550
|
+
if (args.length >= 1 && "input" in args[0]) {
|
|
1551
|
+
input = args[0].input;
|
|
1552
|
+
} else {
|
|
1553
|
+
input = args[0];
|
|
1554
|
+
}
|
|
1555
|
+
if (args.length >= 1 && "config" in args[0]) {
|
|
1556
|
+
config2 = args[0].config;
|
|
1557
|
+
} else if (args.length > 1 && args[1] instanceof Config) {
|
|
1558
|
+
config2 = args[1];
|
|
1559
|
+
}
|
|
1560
|
+
return await listAllResourcesFunction(this.list)({ ...input, config: config2 });
|
|
1561
|
+
};
|
|
1562
|
+
/**
|
|
1563
|
+
* Update a ToolSet by Name
|
|
1564
|
+
*/
|
|
1565
|
+
static async update(params) {
|
|
1566
|
+
const { name, input, config: config2 } = params;
|
|
1567
|
+
return await _ToolSet.getClient().update({ name, input, config: config2 });
|
|
1568
|
+
}
|
|
1569
|
+
/**
|
|
1570
|
+
* Delete this toolset
|
|
1571
|
+
*/
|
|
1572
|
+
delete = async (params) => {
|
|
1573
|
+
const config2 = params?.config;
|
|
1574
|
+
if (!this.name) {
|
|
1575
|
+
throw new Error("name is required to delete a ToolSet");
|
|
1576
|
+
}
|
|
1577
|
+
const result = await _ToolSet.delete({
|
|
1578
|
+
name: this.name,
|
|
1579
|
+
config: config2 ?? this._config
|
|
1580
|
+
});
|
|
1581
|
+
updateObjectProperties(this, result);
|
|
1582
|
+
return this;
|
|
1583
|
+
};
|
|
1584
|
+
/**
|
|
1585
|
+
* Update this toolset
|
|
1586
|
+
*/
|
|
1587
|
+
update = async (params) => {
|
|
1588
|
+
const { input, config: config2 } = params;
|
|
1589
|
+
if (!this.name) {
|
|
1590
|
+
throw new Error("name is required to update a ToolSet");
|
|
1591
|
+
}
|
|
1592
|
+
const result = await _ToolSet.update({
|
|
1593
|
+
name: this.name,
|
|
1594
|
+
input,
|
|
1595
|
+
config: config2 ?? this._config
|
|
1596
|
+
});
|
|
1597
|
+
updateObjectProperties(this, result);
|
|
1598
|
+
return this;
|
|
1599
|
+
};
|
|
1600
|
+
/**
|
|
1601
|
+
* Refresh this toolset's data
|
|
1602
|
+
*/
|
|
1603
|
+
refresh = async (params) => {
|
|
1604
|
+
const config2 = params?.config;
|
|
1605
|
+
if (!this.name) {
|
|
1606
|
+
throw new Error("name is required to refresh a ToolSet");
|
|
1607
|
+
}
|
|
1608
|
+
const result = await _ToolSet.get({
|
|
1609
|
+
name: this.name,
|
|
1610
|
+
config: config2 ?? this._config
|
|
1611
|
+
});
|
|
1612
|
+
updateObjectProperties(this, result);
|
|
1613
|
+
return this;
|
|
1614
|
+
};
|
|
1615
|
+
/**
|
|
1616
|
+
* Get toolset type
|
|
1617
|
+
* 获取工具集类型
|
|
1618
|
+
*/
|
|
1619
|
+
type() {
|
|
1620
|
+
return this.spec?.schema?.type;
|
|
1621
|
+
}
|
|
1622
|
+
/**
|
|
1623
|
+
* List tools (async)
|
|
1624
|
+
* 异步获取工具列表,返回统一的 ToolInfo 列表
|
|
1625
|
+
*/
|
|
1626
|
+
listToolsAsync = async (params) => {
|
|
1627
|
+
const { ToolSetSchemaType: ToolSetSchemaType2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1628
|
+
const { ToolInfo: ToolInfo2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1629
|
+
if (this.type() === ToolSetSchemaType2.MCP) {
|
|
1630
|
+
const mcpTools = this.status?.outputs?.tools || [];
|
|
1631
|
+
return mcpTools.map((tool) => ToolInfo2.fromMCPTool(tool));
|
|
1632
|
+
} else if (this.type() === ToolSetSchemaType2.OPENAPI) {
|
|
1633
|
+
const apiset = await this.toApiSet(params);
|
|
1634
|
+
return apiset.tools;
|
|
1635
|
+
}
|
|
1636
|
+
return [];
|
|
1637
|
+
};
|
|
1638
|
+
/**
|
|
1639
|
+
* List tools (sync wrapper)
|
|
1640
|
+
* 同步获取工具列表,返回统一的 ToolInfo 列表
|
|
1641
|
+
*/
|
|
1642
|
+
listTools = (config2) => {
|
|
1643
|
+
return this.listToolsAsync({ config: config2 });
|
|
1644
|
+
};
|
|
1645
|
+
/**
|
|
1646
|
+
* Call tool (async)
|
|
1647
|
+
* 异步调用工具,统一使用 ApiSet 实现
|
|
1648
|
+
*/
|
|
1649
|
+
callToolAsync = async (name, args, config2) => {
|
|
1650
|
+
const apiset = await this.toApiSet({ config: config2 });
|
|
1651
|
+
const { ToolSetSchemaType: ToolSetSchemaType2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1652
|
+
if (this.type() === ToolSetSchemaType2.OPENAPI) {
|
|
1653
|
+
const tool = apiset.getTool(name);
|
|
1654
|
+
if (!tool) {
|
|
1655
|
+
const openApiTools = this.status?.outputs?.openApiTools || [];
|
|
1656
|
+
for (const toolMeta of openApiTools) {
|
|
1657
|
+
if (!toolMeta) continue;
|
|
1658
|
+
if (toolMeta.toolId === name || toolMeta.tool_id === name) {
|
|
1659
|
+
name = toolMeta.toolName || toolMeta.tool_name || name;
|
|
1660
|
+
break;
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
logger.debug(`Invoke tool ${name} with arguments`, args);
|
|
1666
|
+
const result = await apiset.invoke(name, args, config2);
|
|
1667
|
+
logger.debug(`Invoke tool ${name} got result`, result);
|
|
1668
|
+
return result;
|
|
1669
|
+
};
|
|
1670
|
+
/**
|
|
1671
|
+
* Call tool (sync wrapper)
|
|
1672
|
+
* 同步调用工具,统一使用 ApiSet 实现
|
|
1673
|
+
*/
|
|
1674
|
+
callTool = (name, args, config2) => {
|
|
1675
|
+
return this.callToolAsync(name, args, config2);
|
|
1676
|
+
};
|
|
1677
|
+
/**
|
|
1678
|
+
* Convert ToolSet to unified ApiSet object
|
|
1679
|
+
* 将 ToolSet 转换为统一的 ApiSet 对象
|
|
1680
|
+
*/
|
|
1681
|
+
toApiSet = async (params) => {
|
|
1682
|
+
const { ApiSet: ApiSet2 } = (init_openapi(), __toCommonJS(openapi_exports));
|
|
1683
|
+
const { ToolSetSchemaType: ToolSetSchemaType2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1684
|
+
if (this.type() === ToolSetSchemaType2.MCP) {
|
|
1685
|
+
const { MCPToolSet: MCPToolSet2 } = (init_mcp(), __toCommonJS(mcp_exports));
|
|
1686
|
+
const mcpServerConfig = this.status?.outputs?.mcpServerConfig;
|
|
1687
|
+
if (!mcpServerConfig?.url) {
|
|
1688
|
+
throw new Error("MCP server URL is missing.");
|
|
1689
|
+
}
|
|
1690
|
+
const cfg = Config.withConfigs(
|
|
1691
|
+
params?.config,
|
|
1692
|
+
new Config({ headers: mcpServerConfig.headers })
|
|
1693
|
+
);
|
|
1694
|
+
const mcpClient = new MCPToolSet2(mcpServerConfig.url, cfg);
|
|
1695
|
+
const mcpTools = this.status?.outputs?.tools || [];
|
|
1696
|
+
return ApiSet2.fromMCPTools({
|
|
1697
|
+
tools: mcpTools,
|
|
1698
|
+
mcpClient,
|
|
1699
|
+
config: cfg
|
|
1700
|
+
});
|
|
1701
|
+
} else if (this.type() === ToolSetSchemaType2.OPENAPI) {
|
|
1702
|
+
const headers = this._getOpenAPIAuthDefaults().headers;
|
|
1703
|
+
const query = this._getOpenAPIAuthDefaults().query;
|
|
1704
|
+
const { OpenAPI: OpenAPI2 } = (init_openapi(), __toCommonJS(openapi_exports));
|
|
1705
|
+
const openapi = new OpenAPI2({
|
|
1706
|
+
schema: this.spec?.schema?.detail || "{}",
|
|
1707
|
+
baseUrl: this._getOpenAPIBaseUrl(),
|
|
1708
|
+
headers,
|
|
1709
|
+
queryParams: query,
|
|
1710
|
+
config: params?.config
|
|
1711
|
+
});
|
|
1712
|
+
const { ToolInfo: ToolInfo2 } = (init_model2(), __toCommonJS(model_exports));
|
|
1713
|
+
const tools = openapi.tools.map(
|
|
1714
|
+
(t) => new ToolInfo2({
|
|
1715
|
+
name: t.name,
|
|
1716
|
+
description: t.description,
|
|
1717
|
+
parameters: t.parameters
|
|
1718
|
+
})
|
|
1719
|
+
);
|
|
1720
|
+
return new ApiSet2(tools, openapi, void 0, headers, query, params?.config);
|
|
1721
|
+
}
|
|
1722
|
+
throw new Error(`Unsupported ToolSet type: ${this.type()}`);
|
|
1723
|
+
};
|
|
1724
|
+
/**
|
|
1725
|
+
* Get OpenAPI authentication defaults
|
|
1726
|
+
* 获取 OpenAPI 认证默认值
|
|
1727
|
+
*/
|
|
1728
|
+
_getOpenAPIAuthDefaults() {
|
|
1729
|
+
const headers = {};
|
|
1730
|
+
const query = {};
|
|
1731
|
+
const authConfig = this.spec?.authConfig;
|
|
1732
|
+
const authType = authConfig?.type;
|
|
1733
|
+
if (authType === "APIKey") {
|
|
1734
|
+
const key = authConfig?.apiKeyHeaderName;
|
|
1735
|
+
const value = authConfig?.apiKeyValue;
|
|
1736
|
+
if (key && value) {
|
|
1737
|
+
{
|
|
1738
|
+
headers[key] = value;
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
return { headers, query };
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Get OpenAPI base URL
|
|
1746
|
+
* 获取 OpenAPI 基础 URL
|
|
1747
|
+
*/
|
|
1748
|
+
_getOpenAPIBaseUrl() {
|
|
1749
|
+
const outputs = this.status?.outputs;
|
|
1750
|
+
return outputs?.urls?.internetUrl || outputs?.urls?.intranetUrl;
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
});
|
|
1755
|
+
|
|
1756
|
+
// src/toolset/client.ts
|
|
1757
|
+
var client_exports = {};
|
|
1758
|
+
__export(client_exports, {
|
|
1759
|
+
ToolSetClient: () => ToolSetClient
|
|
1760
|
+
});
|
|
1761
|
+
var ToolSetClient;
|
|
1762
|
+
var init_client = __esm({
|
|
1763
|
+
"src/toolset/client.ts"() {
|
|
1764
|
+
init_config();
|
|
1765
|
+
init_exception();
|
|
1766
|
+
init_api();
|
|
1767
|
+
init_toolset();
|
|
1768
|
+
ToolSetClient = class {
|
|
1769
|
+
config;
|
|
1770
|
+
controlApi;
|
|
1771
|
+
constructor(config2) {
|
|
1772
|
+
this.config = config2;
|
|
1773
|
+
this.controlApi = new ToolControlAPI(config2);
|
|
1774
|
+
}
|
|
1775
|
+
/**
|
|
1776
|
+
* Create a ToolSet
|
|
1777
|
+
*/
|
|
1778
|
+
create = async (params) => {
|
|
1779
|
+
const { input, config: config2 } = params;
|
|
1780
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1781
|
+
try {
|
|
1782
|
+
const authConfig = input.spec?.authConfig ? new Authorization({
|
|
1783
|
+
type: input.spec.authConfig.type,
|
|
1784
|
+
parameters: new AuthorizationParameters({
|
|
1785
|
+
apiKeyParameter: new APIKeyAuthParameter({
|
|
1786
|
+
key: input.spec.authConfig.apiKeyHeaderName,
|
|
1787
|
+
value: input.spec.authConfig.apiKeyValue,
|
|
1788
|
+
in: "header"
|
|
1789
|
+
})
|
|
1790
|
+
})
|
|
1791
|
+
}) : void 0;
|
|
1792
|
+
const request = new Toolset({
|
|
1793
|
+
...input,
|
|
1794
|
+
spec: input.spec ? new ToolsetSpec({
|
|
1795
|
+
...input.spec,
|
|
1796
|
+
schema: input.spec.schema ? new ToolsetSchema({
|
|
1797
|
+
...input.spec.schema
|
|
1798
|
+
}) : void 0,
|
|
1799
|
+
authConfig
|
|
1800
|
+
}) : void 0
|
|
1801
|
+
});
|
|
1802
|
+
const result = await this.controlApi.createToolset({
|
|
1803
|
+
input: request,
|
|
1804
|
+
config: cfg
|
|
1805
|
+
});
|
|
1806
|
+
return new ToolSet(result, cfg);
|
|
1807
|
+
} catch (error) {
|
|
1808
|
+
if (error instanceof HTTPError) {
|
|
1809
|
+
throw error.toResourceError("ToolSet", input.name);
|
|
1810
|
+
}
|
|
1811
|
+
throw error;
|
|
1812
|
+
}
|
|
1813
|
+
};
|
|
1814
|
+
/**
|
|
1815
|
+
* Delete a ToolSet by name
|
|
1816
|
+
*/
|
|
1817
|
+
delete = async (params) => {
|
|
1818
|
+
const { name, config: config2 } = params;
|
|
1819
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1820
|
+
try {
|
|
1821
|
+
const result = await this.controlApi.deleteToolset({
|
|
1822
|
+
name,
|
|
1823
|
+
config: cfg
|
|
1824
|
+
});
|
|
1825
|
+
return new ToolSet(result, cfg);
|
|
1826
|
+
} catch (error) {
|
|
1827
|
+
if (error instanceof HTTPError) {
|
|
1828
|
+
throw error.toResourceError("ToolSet", name);
|
|
1829
|
+
}
|
|
1830
|
+
throw error;
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
/**
|
|
1834
|
+
* Update a ToolSet by name
|
|
1835
|
+
*/
|
|
1836
|
+
update = async (params) => {
|
|
1837
|
+
const { name, input, config: config2 } = params;
|
|
1838
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1839
|
+
try {
|
|
1840
|
+
const authConfig = input.spec?.authConfig ? new Authorization({
|
|
1841
|
+
type: input.spec.authConfig.type,
|
|
1842
|
+
parameters: new AuthorizationParameters({
|
|
1843
|
+
apiKeyParameter: new APIKeyAuthParameter({
|
|
1844
|
+
key: input.spec.authConfig.apiKeyHeaderName,
|
|
1845
|
+
value: input.spec.authConfig.apiKeyValue,
|
|
1846
|
+
in: "header"
|
|
1847
|
+
})
|
|
1848
|
+
})
|
|
1849
|
+
}) : void 0;
|
|
1850
|
+
const request = new Toolset({
|
|
1851
|
+
name,
|
|
1852
|
+
...input,
|
|
1853
|
+
spec: input.spec ? new ToolsetSpec({
|
|
1854
|
+
...input.spec,
|
|
1855
|
+
schema: input.spec.schema ? new ToolsetSchema({
|
|
1856
|
+
...input.spec.schema
|
|
1857
|
+
}) : void 0,
|
|
1858
|
+
authConfig
|
|
1859
|
+
}) : void 0
|
|
1860
|
+
});
|
|
1861
|
+
const result = await this.controlApi.updateToolset({
|
|
1862
|
+
name,
|
|
1863
|
+
input: request,
|
|
1864
|
+
config: cfg
|
|
1865
|
+
});
|
|
1866
|
+
return new ToolSet(result, cfg);
|
|
1867
|
+
} catch (error) {
|
|
1868
|
+
if (error instanceof HTTPError) {
|
|
1869
|
+
throw error.toResourceError("ToolSet", name);
|
|
1870
|
+
}
|
|
1871
|
+
throw error;
|
|
1872
|
+
}
|
|
1873
|
+
};
|
|
1874
|
+
/**
|
|
1875
|
+
* Get a ToolSet by name
|
|
1876
|
+
*/
|
|
1877
|
+
get = async (params) => {
|
|
1878
|
+
const { name, config: config2 } = params;
|
|
1879
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1880
|
+
try {
|
|
1881
|
+
const result = await this.controlApi.getToolset({
|
|
1882
|
+
name,
|
|
1883
|
+
config: cfg
|
|
1884
|
+
});
|
|
1885
|
+
return new ToolSet(result, cfg);
|
|
1886
|
+
} catch (error) {
|
|
1887
|
+
if (error instanceof HTTPError) {
|
|
1888
|
+
throw error.toResourceError("ToolSet", name);
|
|
1889
|
+
}
|
|
1890
|
+
throw error;
|
|
1891
|
+
}
|
|
1892
|
+
};
|
|
1893
|
+
/**
|
|
1894
|
+
* List ToolSets
|
|
1895
|
+
*/
|
|
1896
|
+
list = async (params) => {
|
|
1897
|
+
const { input, config: config2 } = params ?? {};
|
|
1898
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1899
|
+
const results = await this.controlApi.listToolsets({
|
|
1900
|
+
input: new ListToolsetsRequest({ ...input }),
|
|
1901
|
+
config: cfg
|
|
1902
|
+
});
|
|
1903
|
+
return results.data?.map((result) => new ToolSet(result, cfg)) || [];
|
|
1904
|
+
};
|
|
1905
|
+
};
|
|
1906
|
+
}
|
|
1907
|
+
});
|
|
1908
|
+
|
|
1909
|
+
// src/toolset/index.ts
|
|
1910
|
+
init_version_check();
|
|
1911
|
+
init_client();
|
|
1912
|
+
init_toolset();
|
|
1913
|
+
init_openapi();
|
|
1914
|
+
init_model2();
|
|
1915
|
+
init_control();
|
|
1916
|
+
init_mcp();
|
|
1917
|
+
|
|
1918
|
+
export { ApiSet, MCPSession, MCPToolSet, OpenAPI, ToolControlAPI, ToolInfo, ToolSchema, ToolSet, ToolSetClient, ToolSetSchemaType };
|
|
1919
|
+
//# sourceMappingURL=index.js.map
|
|
1920
|
+
//# sourceMappingURL=index.js.map
|