@glodon-aiot/bot-client-sdk 3.26.0-alpha.9 → 3.27.0-alpha.0
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 +246 -225
- package/dist/es/AgentSession.mjs +275 -197
- package/dist/es/BotClient.mjs +200 -123
- package/dist/es/Config.mjs +58 -68
- package/dist/es/DialogSession.mjs +123 -118
- package/dist/es/EventBus.mjs +38 -34
- package/dist/es/Logger.mjs +19 -21
- package/dist/es/Session.mjs +338 -284
- package/dist/es/api/index.mjs +31 -30
- package/dist/es/builtinTools/constants.mjs +12 -5
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +182 -160
- package/dist/es/builtinTools/paths.mjs +34 -27
- package/dist/es/builtinTools/platformSkills.mjs +248 -0
- package/dist/es/builtinTools/schemas.mjs +63 -18
- package/dist/es/builtinTools/tools.mjs +368 -244
- package/dist/es/index.mjs +62 -61
- package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
- package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +113 -131
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +60 -55
- package/dist/es/localFiles/constants.mjs +24 -28
- package/dist/es/localFiles/createLocalFilesPlugin.mjs +53 -52
- package/dist/es/localFiles/errors.mjs +44 -35
- package/dist/es/localFiles/host/connectHostBridge.mjs +20 -15
- package/dist/es/localFiles/host/protocol.mjs +27 -14
- package/dist/es/localFiles/host/sandboxClient.mjs +297 -127
- package/dist/es/localFiles/preview.mjs +33 -0
- package/dist/es/localFiles/schemas.mjs +22 -53
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +156 -89
- package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
- package/dist/es/localMcp/client.mjs +234 -0
- package/dist/es/localMcp/endpoints.mjs +94 -0
- package/dist/es/localMcp/register.mjs +207 -0
- package/dist/es/localMcp/registry.mjs +43 -0
- package/dist/es/package.json.mjs +29 -49
- package/dist/es/sessionUpload.mjs +49 -0
- package/dist/es/utils.mjs +24 -11
- package/dist/lib/index.js +48 -40
- package/dist/src/AgentSession.d.ts +13 -0
- package/dist/src/BotClient.d.ts +13 -1
- package/dist/src/Config.d.ts +14 -7
- package/dist/src/EventBus.d.ts +3 -0
- package/dist/src/Logger.d.ts +15 -8
- package/dist/src/PayloadTypes.d.ts +7 -0
- package/dist/src/Session.d.ts +19 -2
- package/dist/src/api/index.d.ts +2 -2
- package/dist/src/builtinTools/command.d.ts +7 -0
- package/dist/src/builtinTools/constants.d.ts +10 -2
- package/dist/src/builtinTools/paths.d.ts +3 -2
- package/dist/src/builtinTools/platformSkills.d.ts +92 -0
- package/dist/src/builtinTools/tools.d.ts +13 -0
- package/dist/src/constant.d.ts +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +7 -4
- package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +8 -0
- package/dist/src/localFiles/constants.d.ts +4 -13
- package/dist/src/localFiles/errors.d.ts +1 -1
- package/dist/src/localFiles/host/protocol.d.ts +14 -3
- package/dist/src/localFiles/host/sandboxClient.d.ts +50 -0
- package/dist/src/localFiles/index.d.ts +9 -5
- package/dist/src/localFiles/preview.d.ts +2 -0
- package/dist/src/localFiles/schemas.d.ts +0 -2
- package/dist/src/localFiles/types.d.ts +2 -22
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +25 -3
- package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
- package/dist/src/localMcp/client.d.ts +15 -0
- package/dist/src/localMcp/endpoints.d.ts +19 -0
- package/dist/src/localMcp/register.d.ts +34 -0
- package/dist/src/localMcp/registry.d.ts +27 -0
- package/dist/src/sessionUpload.d.ts +14 -0
- package/dist/src/utils.d.ts +6 -0
- package/package.json +4 -24
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
var A = Object.defineProperty;
|
|
2
|
+
var B = (n, t, e) => t in n ? A(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
|
|
3
|
+
var p = (n, t, e) => (B(n, typeof t != "symbol" ? t + "" : t, e), e);
|
|
4
|
+
var a = (n, t, e) => new Promise((s, r) => {
|
|
5
|
+
var i = (c) => {
|
|
6
|
+
try {
|
|
7
|
+
h(e.next(c));
|
|
8
|
+
} catch (w) {
|
|
9
|
+
r(w);
|
|
10
|
+
}
|
|
11
|
+
}, o = (c) => {
|
|
12
|
+
try {
|
|
13
|
+
h(e.throw(c));
|
|
14
|
+
} catch (w) {
|
|
15
|
+
r(w);
|
|
16
|
+
}
|
|
17
|
+
}, h = (c) => c.done ? s(c.value) : Promise.resolve(c.value).then(i, o);
|
|
18
|
+
h((e = e.apply(n, t)).next());
|
|
19
|
+
});
|
|
20
|
+
import { ToolError as l, ToolCode as f, toToolError as F, workspaceNotGranted as E } from "../errors.mjs";
|
|
21
|
+
import { mediaTypeOf as k, PREVIEW_TYPE_MESSAGE as R } from "../preview.mjs";
|
|
22
|
+
import { parentPath as d } from "../security/pathPolicy.mjs";
|
|
23
|
+
import { SensitiveFilePolicy as U } from "../security/sensitiveFiles.mjs";
|
|
24
|
+
const b = "路径必须在工作区内,不能包含 ..、盘符或空字符。";
|
|
25
|
+
function m() {
|
|
26
|
+
return new l(f.Forbidden, b, {
|
|
27
|
+
reason: "path_outside_workspace"
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function u(n, t) {
|
|
31
|
+
if (n == null || n === "") {
|
|
32
|
+
if (t)
|
|
33
|
+
return "";
|
|
34
|
+
throw new l(f.BadRequest, "缺少参数 path。");
|
|
35
|
+
}
|
|
36
|
+
if (typeof n != "string")
|
|
37
|
+
throw new l(f.BadRequest, "path 必须是字符串。");
|
|
38
|
+
if (n.includes("\0") || /%(2e|2f|5c|00)/i.test(n))
|
|
39
|
+
throw m();
|
|
40
|
+
const e = n.normalize("NFC").replace(/\\/g, "/").trim();
|
|
41
|
+
if (/^[a-zA-Z]:/.test(e))
|
|
42
|
+
throw m();
|
|
43
|
+
const s = [];
|
|
44
|
+
for (const i of e.split("/"))
|
|
45
|
+
if (!(i === "" || i === ".")) {
|
|
46
|
+
if (i === "..")
|
|
47
|
+
throw m();
|
|
48
|
+
s.push(i);
|
|
49
|
+
}
|
|
50
|
+
const r = s.join("/");
|
|
51
|
+
if (!r && !t)
|
|
52
|
+
throw new l(f.BadRequest, "path 不能指向工作区根目录。");
|
|
53
|
+
return r;
|
|
54
|
+
}
|
|
55
|
+
function g(n) {
|
|
56
|
+
return new l(f.NotImplemented, `当前工作区不能${n}。`);
|
|
57
|
+
}
|
|
58
|
+
function P(n, t, e) {
|
|
59
|
+
return {
|
|
60
|
+
name: e.name,
|
|
61
|
+
path: t,
|
|
62
|
+
kind: e.kind,
|
|
63
|
+
size: e.size,
|
|
64
|
+
lastModified: e.lastModified,
|
|
65
|
+
sensitive: e.kind === "file" && n.sensitive.isSensitive(t)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
class C {
|
|
69
|
+
constructor(t) {
|
|
70
|
+
p(this, "previews", /* @__PURE__ */ new Map());
|
|
71
|
+
p(this, "previewByPath", /* @__PURE__ */ new Map());
|
|
72
|
+
this.host = t;
|
|
73
|
+
}
|
|
74
|
+
stat(t = "") {
|
|
75
|
+
return a(this, null, function* () {
|
|
76
|
+
const e = u(t, !0), s = yield this.run((r) => r.stat(e));
|
|
77
|
+
return P(this.host, e, s);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
list() {
|
|
81
|
+
return a(this, arguments, function* (t = "", e = {}) {
|
|
82
|
+
const s = u(t, !0);
|
|
83
|
+
return (yield this.run((i) => i.listEntries(s, e.signal))).map((i) => {
|
|
84
|
+
const o = s ? `${s}/${i.name}` : i.name;
|
|
85
|
+
return P(this.host, o, i);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 给 `<img>`、`<video>`、`<audio>` 的地址。浏览器自己拉字节,调用方拿不到文件内容。
|
|
91
|
+
* 同一路径在大小和修改时间没变时返回同一个地址,并增加引用。用完调用 `releaseUrl`。
|
|
92
|
+
*/
|
|
93
|
+
url(t) {
|
|
94
|
+
return a(this, null, function* () {
|
|
95
|
+
var c, w;
|
|
96
|
+
const e = u(t, !1);
|
|
97
|
+
if (this.assertAccess(e, "read"), !k(e))
|
|
98
|
+
throw new l(f.BadRequest, R);
|
|
99
|
+
const s = yield this.stat(e);
|
|
100
|
+
if (s.kind !== "file")
|
|
101
|
+
throw new l(f.BadRequest, "目标是目录,不是文件。");
|
|
102
|
+
const r = `${(c = s.size) != null ? c : ""}:${(w = s.lastModified) != null ? w : ""}`, i = this.previewByPath.get(e);
|
|
103
|
+
if ((i == null ? void 0 : i.stamp) === r)
|
|
104
|
+
return i.refs += 1, i.url;
|
|
105
|
+
const o = yield this.openPreview(e), h = {
|
|
106
|
+
url: o.url,
|
|
107
|
+
path: e,
|
|
108
|
+
stamp: r,
|
|
109
|
+
refs: 1,
|
|
110
|
+
release: o.release
|
|
111
|
+
};
|
|
112
|
+
return this.previews.set(h.url, h), this.previewByPath.set(e, h), h.url;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/** 页面不再显示这个地址时调用。引用归零后,blob 地址会撤销,沙箱票据会作废。 */
|
|
116
|
+
releaseUrl(t) {
|
|
117
|
+
const e = this.previews.get(t);
|
|
118
|
+
e && (e.refs -= 1, !(e.refs > 0) && this.dropPreview(e));
|
|
119
|
+
}
|
|
120
|
+
/** 工作区断开时释放还没归还的地址。 */
|
|
121
|
+
releaseAllUrls() {
|
|
122
|
+
const t = [...this.previews.values()];
|
|
123
|
+
this.previews.clear(), this.previewByPath.clear(), t.forEach((e) => e.release());
|
|
124
|
+
}
|
|
125
|
+
read(t) {
|
|
126
|
+
return a(this, null, function* () {
|
|
127
|
+
const e = u(t, !1);
|
|
128
|
+
return this.assertAccess(e, "read"), this.run((s) => a(this, null, function* () {
|
|
129
|
+
if ((yield s.stat(e)).kind !== "file")
|
|
130
|
+
throw new l(f.BadRequest, "目标是目录,不是文件。");
|
|
131
|
+
return s.readFile(e);
|
|
132
|
+
}));
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
readText(t) {
|
|
136
|
+
return a(this, null, function* () {
|
|
137
|
+
return (yield this.read(t)).text();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
write(r, i) {
|
|
141
|
+
return a(this, arguments, function* (t, e, s = {}) {
|
|
142
|
+
const o = u(t, !1);
|
|
143
|
+
this.assertAccess(o, "write"), yield this.run((h) => h.writeFile(o, e, {
|
|
144
|
+
createDirectories: s.createDirectories !== !1,
|
|
145
|
+
signal: s.signal
|
|
146
|
+
})), this.host.notifyFilesChanged([d(o)]);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
append(r, i) {
|
|
150
|
+
return a(this, arguments, function* (t, e, s = {}) {
|
|
151
|
+
const o = u(t, !1);
|
|
152
|
+
this.assertAccess(o, "write");
|
|
153
|
+
const h = typeof e == "string" ? new TextEncoder().encode(e) : e;
|
|
154
|
+
yield this.run((c) => a(this, null, function* () {
|
|
155
|
+
let w = new Uint8Array();
|
|
156
|
+
try {
|
|
157
|
+
if ((yield c.stat(o)).kind !== "file")
|
|
158
|
+
throw new l(f.BadRequest, "目标是目录,不是文件。");
|
|
159
|
+
w = new Uint8Array(yield (yield c.readFile(o)).arrayBuffer());
|
|
160
|
+
} catch (y) {
|
|
161
|
+
if (y instanceof l || F(y).code !== f.NotFound)
|
|
162
|
+
throw y;
|
|
163
|
+
}
|
|
164
|
+
const v = new Uint8Array(w.byteLength + h.byteLength);
|
|
165
|
+
v.set(w), v.set(h, w.byteLength), yield c.writeFile(o, v, {
|
|
166
|
+
createDirectories: !0,
|
|
167
|
+
signal: s.signal
|
|
168
|
+
});
|
|
169
|
+
})), this.host.notifyFilesChanged([d(o)]);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
mkdir(t) {
|
|
173
|
+
return a(this, null, function* () {
|
|
174
|
+
const e = u(t, !0);
|
|
175
|
+
e && (yield this.run((s) => a(this, null, function* () {
|
|
176
|
+
if (!s.createDirectory)
|
|
177
|
+
throw g("创建目录");
|
|
178
|
+
yield s.createDirectory(e);
|
|
179
|
+
})), this.host.notifyFilesChanged([d(e)]));
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
remove(s) {
|
|
183
|
+
return a(this, arguments, function* (t, e = {}) {
|
|
184
|
+
const r = u(t, !1);
|
|
185
|
+
this.assertAccess(r, "write"), yield this.run((i) => a(this, null, function* () {
|
|
186
|
+
if (!i.remove)
|
|
187
|
+
throw g("删除");
|
|
188
|
+
yield i.remove(r, e);
|
|
189
|
+
})), this.host.notifyFilesChanged([d(r)]);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
move(t, e) {
|
|
193
|
+
return a(this, null, function* () {
|
|
194
|
+
const s = u(t, !1), r = u(e, !1);
|
|
195
|
+
this.assertAccess(s, "write"), this.assertAccess(r, "write"), yield this.run((i) => a(this, null, function* () {
|
|
196
|
+
if (!i.move)
|
|
197
|
+
throw g("移动");
|
|
198
|
+
yield i.move(s, r);
|
|
199
|
+
})), this.host.notifyFilesChanged([d(s), d(r)]);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
openPreview(t) {
|
|
203
|
+
return a(this, null, function* () {
|
|
204
|
+
return this.run((e) => a(this, null, function* () {
|
|
205
|
+
if (e.url)
|
|
206
|
+
return e.url(t);
|
|
207
|
+
const s = yield e.readFile(t), r = URL.createObjectURL(s);
|
|
208
|
+
return {
|
|
209
|
+
url: r,
|
|
210
|
+
release: () => URL.revokeObjectURL(r)
|
|
211
|
+
};
|
|
212
|
+
}));
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
dropPreview(t) {
|
|
216
|
+
t.release(), this.previews.delete(t.url), this.previewByPath.get(t.path) === t && this.previewByPath.delete(t.path);
|
|
217
|
+
}
|
|
218
|
+
assertAccess(t, e) {
|
|
219
|
+
const s = this.host.sensitive.match(t, e);
|
|
220
|
+
if (s)
|
|
221
|
+
throw new l(f.Forbidden, U.reasonOf(s), {
|
|
222
|
+
reason: "sensitive_file",
|
|
223
|
+
ruleId: s.id
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
run(t) {
|
|
227
|
+
return a(this, null, function* () {
|
|
228
|
+
var s;
|
|
229
|
+
const e = this.host.adapter;
|
|
230
|
+
if (!e)
|
|
231
|
+
throw E();
|
|
232
|
+
try {
|
|
233
|
+
return yield t(e);
|
|
234
|
+
} catch (r) {
|
|
235
|
+
if (r instanceof l)
|
|
236
|
+
throw r;
|
|
237
|
+
const i = F(r);
|
|
238
|
+
throw ((s = i.data) == null ? void 0 : s.reason) === "workspace_not_granted" && (yield this.host.handlePermissionLost()), i;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
export {
|
|
244
|
+
C as WorkspaceFiles,
|
|
245
|
+
u as normalizeFilePath
|
|
246
|
+
};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
var T = Object.defineProperty, b = Object.defineProperties;
|
|
2
|
+
var k = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var g = Object.getOwnPropertySymbols;
|
|
4
|
+
var O = Object.prototype.hasOwnProperty, J = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var M = (t, o, e) => o in t ? T(t, o, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[o] = e, v = (t, o) => {
|
|
6
|
+
for (var e in o || (o = {}))
|
|
7
|
+
O.call(o, e) && M(t, e, o[e]);
|
|
8
|
+
if (g)
|
|
9
|
+
for (var e of g(o))
|
|
10
|
+
J.call(o, e) && M(t, e, o[e]);
|
|
11
|
+
return t;
|
|
12
|
+
}, E = (t, o) => b(t, k(o));
|
|
13
|
+
var u = (t, o, e) => new Promise((n, r) => {
|
|
14
|
+
var i = (s) => {
|
|
15
|
+
try {
|
|
16
|
+
c(e.next(s));
|
|
17
|
+
} catch (f) {
|
|
18
|
+
r(f);
|
|
19
|
+
}
|
|
20
|
+
}, a = (s) => {
|
|
21
|
+
try {
|
|
22
|
+
c(e.throw(s));
|
|
23
|
+
} catch (f) {
|
|
24
|
+
r(f);
|
|
25
|
+
}
|
|
26
|
+
}, c = (s) => s.done ? n(s.value) : Promise.resolve(s.value).then(i, a);
|
|
27
|
+
c((e = e.apply(t, o)).next());
|
|
28
|
+
});
|
|
29
|
+
const d = /* @__PURE__ */ new Map(), N = 2e4;
|
|
30
|
+
function P(t) {
|
|
31
|
+
if (typeof AbortSignal != "undefined" && typeof AbortSignal.timeout == "function") {
|
|
32
|
+
const o = AbortSignal.timeout(N);
|
|
33
|
+
if (!t)
|
|
34
|
+
return o;
|
|
35
|
+
const e = new AbortController(), n = () => e.abort();
|
|
36
|
+
return o.addEventListener("abort", n, {
|
|
37
|
+
once: !0
|
|
38
|
+
}), t.addEventListener("abort", n, {
|
|
39
|
+
once: !0
|
|
40
|
+
}), e.signal;
|
|
41
|
+
}
|
|
42
|
+
return t;
|
|
43
|
+
}
|
|
44
|
+
function m(t) {
|
|
45
|
+
return `${t.url}
|
|
46
|
+
${JSON.stringify(t.headers)}`;
|
|
47
|
+
}
|
|
48
|
+
function C(t) {
|
|
49
|
+
const o = t.trim();
|
|
50
|
+
if (!o)
|
|
51
|
+
return {};
|
|
52
|
+
if (o.startsWith("{") || o.startsWith("["))
|
|
53
|
+
return JSON.parse(o);
|
|
54
|
+
const e = o.split(/\n\n/);
|
|
55
|
+
for (let n = e.length - 1; n >= 0; n -= 1) {
|
|
56
|
+
const r = e[n].split(`
|
|
57
|
+
`).filter((i) => i.startsWith("data:")).map((i) => i.slice(5).trim()).join(`
|
|
58
|
+
`);
|
|
59
|
+
if (r.startsWith("{") || r.startsWith("["))
|
|
60
|
+
return JSON.parse(r);
|
|
61
|
+
}
|
|
62
|
+
throw new Error("MCP 响应不是 JSON");
|
|
63
|
+
}
|
|
64
|
+
function I(t) {
|
|
65
|
+
if (typeof t == "string")
|
|
66
|
+
return t;
|
|
67
|
+
if (!t || typeof t != "object")
|
|
68
|
+
return JSON.stringify(t != null ? t : "");
|
|
69
|
+
const o = t.content;
|
|
70
|
+
return Array.isArray(o) ? o.map((e) => e && typeof e == "object" && typeof e.text == "string" ? e.text : JSON.stringify(e)).join(`
|
|
71
|
+
`) : JSON.stringify(t);
|
|
72
|
+
}
|
|
73
|
+
function A(t) {
|
|
74
|
+
return u(this, null, function* () {
|
|
75
|
+
if (!(t.headers.get("content-type") || "").includes("text/event-stream") || !t.body)
|
|
76
|
+
return t.text();
|
|
77
|
+
const e = t.body.getReader(), n = new TextDecoder();
|
|
78
|
+
let r = "";
|
|
79
|
+
try {
|
|
80
|
+
for (; r.length < 2e6; ) {
|
|
81
|
+
const {
|
|
82
|
+
done: i,
|
|
83
|
+
value: a
|
|
84
|
+
} = yield e.read();
|
|
85
|
+
if (i)
|
|
86
|
+
break;
|
|
87
|
+
r += n.decode(a, {
|
|
88
|
+
stream: !0
|
|
89
|
+
});
|
|
90
|
+
const c = r.split(/\n\n/).filter((s) => /^\s*data:/m.test(s));
|
|
91
|
+
if (c.length > 0)
|
|
92
|
+
return c[c.length - 1];
|
|
93
|
+
}
|
|
94
|
+
return r;
|
|
95
|
+
} finally {
|
|
96
|
+
yield e.cancel().catch(() => {
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function h(t, o, e, n, r) {
|
|
102
|
+
return u(this, null, function* () {
|
|
103
|
+
const i = E(v({}, t.headers), {
|
|
104
|
+
"content-type": "application/json",
|
|
105
|
+
accept: "application/json, text/event-stream"
|
|
106
|
+
});
|
|
107
|
+
e != null && e.id && (i["mcp-session-id"] = e.id), e != null && e.protocol && (i["mcp-protocol-version"] = e.protocol);
|
|
108
|
+
const a = yield r(t.url, {
|
|
109
|
+
method: "POST",
|
|
110
|
+
headers: i,
|
|
111
|
+
body: JSON.stringify(o),
|
|
112
|
+
signal: P(n)
|
|
113
|
+
}), c = yield A(a);
|
|
114
|
+
if (!a.ok)
|
|
115
|
+
throw new Error(`MCP HTTP ${a.status}: ${c.slice(0, 300)}`);
|
|
116
|
+
const s = C(c), f = a.headers.get("mcp-session-id");
|
|
117
|
+
return e && f && (e.id = f), s;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
function S(t) {
|
|
121
|
+
const o = t.error;
|
|
122
|
+
if (!o || typeof o != "object")
|
|
123
|
+
return;
|
|
124
|
+
const e = o.message;
|
|
125
|
+
return typeof e == "string" && e ? e : "MCP 调用失败";
|
|
126
|
+
}
|
|
127
|
+
function H(t) {
|
|
128
|
+
const o = t instanceof Error ? t.message : String(t);
|
|
129
|
+
return /session/i.test(o) || o.includes("MCP HTTP 404");
|
|
130
|
+
}
|
|
131
|
+
function w(t, o, e) {
|
|
132
|
+
return u(this, null, function* () {
|
|
133
|
+
const n = m(t), r = d.get(n);
|
|
134
|
+
if (r != null && r.id)
|
|
135
|
+
return r;
|
|
136
|
+
const i = {
|
|
137
|
+
protocol: "2024-11-05",
|
|
138
|
+
nextId: 1
|
|
139
|
+
}, a = yield h(t, {
|
|
140
|
+
jsonrpc: "2.0",
|
|
141
|
+
id: i.nextId,
|
|
142
|
+
method: "initialize",
|
|
143
|
+
params: {
|
|
144
|
+
protocolVersion: i.protocol,
|
|
145
|
+
capabilities: {},
|
|
146
|
+
clientInfo: {
|
|
147
|
+
name: "bot-client-sdk",
|
|
148
|
+
version: "1.0.0"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}, i, o, e);
|
|
152
|
+
i.nextId += 1;
|
|
153
|
+
const c = S(a);
|
|
154
|
+
if (c)
|
|
155
|
+
throw new Error(c);
|
|
156
|
+
const s = a.result;
|
|
157
|
+
if (s && typeof s == "object") {
|
|
158
|
+
const f = s.protocolVersion;
|
|
159
|
+
typeof f == "string" && f && (i.protocol = f);
|
|
160
|
+
}
|
|
161
|
+
return yield h(t, {
|
|
162
|
+
jsonrpc: "2.0",
|
|
163
|
+
method: "notifications/initialized"
|
|
164
|
+
}, i, o, e), d.set(n, i), i;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
function $(e) {
|
|
168
|
+
return u(this, arguments, function* (t, o = fetch) {
|
|
169
|
+
var c;
|
|
170
|
+
const n = yield w(t, void 0, o), r = yield h(t, {
|
|
171
|
+
jsonrpc: "2.0",
|
|
172
|
+
id: n.nextId,
|
|
173
|
+
method: "tools/list",
|
|
174
|
+
params: {}
|
|
175
|
+
}, n, void 0, o);
|
|
176
|
+
n.nextId += 1;
|
|
177
|
+
const i = S(r);
|
|
178
|
+
if (i)
|
|
179
|
+
throw new Error(i);
|
|
180
|
+
const a = r.result;
|
|
181
|
+
return ((c = a == null ? void 0 : a.tools) != null ? c : []).filter((s) => s.name).map((s) => ({
|
|
182
|
+
name: s.name,
|
|
183
|
+
description: s.description || "",
|
|
184
|
+
inputSchema: s.inputSchema || {
|
|
185
|
+
type: "object",
|
|
186
|
+
properties: {}
|
|
187
|
+
}
|
|
188
|
+
}));
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
function z(i, a, c, s) {
|
|
192
|
+
return u(this, arguments, function* (t, o, e, n, r = fetch) {
|
|
193
|
+
const f = m(t);
|
|
194
|
+
let p = yield w(t, n, r);
|
|
195
|
+
const x = () => h(t, {
|
|
196
|
+
jsonrpc: "2.0",
|
|
197
|
+
id: p.nextId,
|
|
198
|
+
method: "tools/call",
|
|
199
|
+
params: {
|
|
200
|
+
name: o,
|
|
201
|
+
arguments: e && typeof e == "object" ? e : {}
|
|
202
|
+
}
|
|
203
|
+
}, p, n, r);
|
|
204
|
+
let l;
|
|
205
|
+
try {
|
|
206
|
+
l = yield x(), p.nextId += 1;
|
|
207
|
+
} catch (j) {
|
|
208
|
+
if (!H(j))
|
|
209
|
+
throw j;
|
|
210
|
+
d.delete(f), p = yield w(t, n, r), l = yield x(), p.nextId += 1;
|
|
211
|
+
}
|
|
212
|
+
let y = S(l);
|
|
213
|
+
if (y && /session/i.test(y) && (d.delete(f), p = yield w(t, n, r), l = yield x(), p.nextId += 1, y = S(l)), y)
|
|
214
|
+
throw new Error(y);
|
|
215
|
+
return I(l.result);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function _(t) {
|
|
219
|
+
const o = new Set(t.map((e) => m(e)));
|
|
220
|
+
for (const e of d.keys())
|
|
221
|
+
o.has(e) || d.delete(e);
|
|
222
|
+
}
|
|
223
|
+
function B(t, o) {
|
|
224
|
+
const e = m(t);
|
|
225
|
+
o.some((n) => m(n) === e) || d.delete(e);
|
|
226
|
+
}
|
|
227
|
+
export {
|
|
228
|
+
z as callMcpTool,
|
|
229
|
+
B as dropUnusedMcpSession,
|
|
230
|
+
$ as listMcpTools,
|
|
231
|
+
I as mcpToolText,
|
|
232
|
+
C as parseMcpBody,
|
|
233
|
+
_ as retainMcpSessions
|
|
234
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var m = Object.getOwnPropertySymbols;
|
|
3
|
+
var b = Object.prototype.hasOwnProperty, L = Object.prototype.propertyIsEnumerable;
|
|
4
|
+
var y = (t, r, e) => r in t ? g(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e, f = (t, r) => {
|
|
5
|
+
for (var e in r || (r = {}))
|
|
6
|
+
b.call(r, e) && y(t, e, r[e]);
|
|
7
|
+
if (m)
|
|
8
|
+
for (var e of m(r))
|
|
9
|
+
L.call(r, e) && y(t, e, r[e]);
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { getHostBridge as M, parseHostMessage as w } from "../localFiles/host/protocol.mjs";
|
|
13
|
+
const p = /* @__PURE__ */ new Set();
|
|
14
|
+
let c, h = [], s, i;
|
|
15
|
+
function S() {
|
|
16
|
+
return {
|
|
17
|
+
revision: c,
|
|
18
|
+
servers: h
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function B(t) {
|
|
22
|
+
return p.add(t), () => p.delete(t);
|
|
23
|
+
}
|
|
24
|
+
function E(t) {
|
|
25
|
+
let r;
|
|
26
|
+
try {
|
|
27
|
+
r = new URL(t);
|
|
28
|
+
} catch (n) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (r.protocol !== "http:" || r.username || r.password || !r.port)
|
|
32
|
+
return;
|
|
33
|
+
const e = Number(r.port);
|
|
34
|
+
if (!Number.isInteger(e) || e < 1 || e > 65535)
|
|
35
|
+
return;
|
|
36
|
+
const o = r.hostname.toLowerCase();
|
|
37
|
+
if (!(o !== "127.0.0.1" && o !== "localhost" && o !== "::1" && o !== "[::1]"))
|
|
38
|
+
return r.href;
|
|
39
|
+
}
|
|
40
|
+
function a(t) {
|
|
41
|
+
return typeof t == "string" && t.trim() ? t.trim() : void 0;
|
|
42
|
+
}
|
|
43
|
+
function A(t) {
|
|
44
|
+
var e;
|
|
45
|
+
if (!Array.isArray(t))
|
|
46
|
+
return [];
|
|
47
|
+
const r = [];
|
|
48
|
+
for (const o of t) {
|
|
49
|
+
if (!o || typeof o != "object")
|
|
50
|
+
continue;
|
|
51
|
+
const n = o, u = a(n.code), d = (e = a(n.id)) != null ? e : u, l = typeof n.url == "string" ? E(n.url) : void 0;
|
|
52
|
+
if (!d || !l)
|
|
53
|
+
continue;
|
|
54
|
+
const v = a(n.token);
|
|
55
|
+
r.push(f(f({
|
|
56
|
+
id: d,
|
|
57
|
+
url: l
|
|
58
|
+
}, u ? {
|
|
59
|
+
code: u
|
|
60
|
+
} : {}), v ? {
|
|
61
|
+
token: v
|
|
62
|
+
} : {}));
|
|
63
|
+
}
|
|
64
|
+
return r;
|
|
65
|
+
}
|
|
66
|
+
function k(t) {
|
|
67
|
+
const r = t.revision;
|
|
68
|
+
return typeof r != "number" || !Number.isFinite(r) || c !== void 0 && r <= c || !Array.isArray(t.servers) ? !1 : (c = r, h = A(t.servers), p.forEach((e) => e()), !0);
|
|
69
|
+
}
|
|
70
|
+
function H() {
|
|
71
|
+
const t = M();
|
|
72
|
+
if (!t)
|
|
73
|
+
return () => {
|
|
74
|
+
};
|
|
75
|
+
if (i === t && s)
|
|
76
|
+
return s;
|
|
77
|
+
s == null || s();
|
|
78
|
+
const r = (o) => {
|
|
79
|
+
const n = w(o.data);
|
|
80
|
+
!n || n.type !== "local-mcps" || k(n);
|
|
81
|
+
};
|
|
82
|
+
t.addEventListener("message", r), i = t;
|
|
83
|
+
const e = () => {
|
|
84
|
+
t.removeEventListener("message", r), i === t && (i = void 0, s = void 0);
|
|
85
|
+
};
|
|
86
|
+
return s = e, e;
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
k as applyLocalMcpSnapshot,
|
|
90
|
+
H as connectLocalMcpBridge,
|
|
91
|
+
S as currentLocalMcpEndpoints,
|
|
92
|
+
E as loopbackMcpUrl,
|
|
93
|
+
B as subscribeLocalMcpEndpoints
|
|
94
|
+
};
|