@glodon-aiot/bot-client-sdk 3.26.0-alpha.5 → 3.26.0-alpha.9
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 -37
- package/dist/es/AgentSession.mjs +32 -33
- package/dist/es/DialogSession.mjs +39 -40
- package/dist/es/Session.mjs +71 -63
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +136 -128
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +102 -178
- package/dist/es/localFiles/host/connectHostBridge.mjs +38 -39
- package/dist/es/localFiles/host/protocol.mjs +1 -1
- package/dist/es/localFiles/host/sandboxClient.mjs +324 -0
- package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +43 -42
- package/dist/src/Session.d.ts +4 -0
- package/dist/src/builtinTools/command.d.ts +2 -0
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +14 -20
- package/dist/src/localFiles/host/protocol.d.ts +16 -22
- package/dist/src/localFiles/host/sandboxClient.d.ts +62 -0
- package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
- package/package.json +2 -2
|
@@ -1,114 +1,122 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
1
|
+
var z = Object.defineProperty, G = Object.defineProperties;
|
|
2
|
+
var K = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var P = Object.getOwnPropertySymbols;
|
|
4
4
|
var X = Object.prototype.hasOwnProperty, Q = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var
|
|
5
|
+
var C = (e, t, o) => t in e ? z(e, t, { enumerable: !0, configurable: !0, writable: !0, value: o }) : e[t] = o, s = (e, t) => {
|
|
6
6
|
for (var o in t || (t = {}))
|
|
7
|
-
X.call(t, o) &&
|
|
8
|
-
if (
|
|
9
|
-
for (var o of
|
|
10
|
-
Q.call(t, o) &&
|
|
7
|
+
X.call(t, o) && C(e, o, t[o]);
|
|
8
|
+
if (P)
|
|
9
|
+
for (var o of P(t))
|
|
10
|
+
Q.call(t, o) && C(e, o, t[o]);
|
|
11
11
|
return e;
|
|
12
|
-
}, h = (e, t) =>
|
|
13
|
-
var
|
|
14
|
-
var
|
|
12
|
+
}, h = (e, t) => G(e, K(t));
|
|
13
|
+
var D = (e, t, o) => new Promise((a, m) => {
|
|
14
|
+
var d = (n) => {
|
|
15
15
|
try {
|
|
16
|
-
|
|
16
|
+
c(o.next(n));
|
|
17
17
|
} catch (r) {
|
|
18
|
-
|
|
18
|
+
m(r);
|
|
19
19
|
}
|
|
20
|
-
},
|
|
20
|
+
}, f = (n) => {
|
|
21
21
|
try {
|
|
22
|
-
|
|
22
|
+
c(o.throw(n));
|
|
23
23
|
} catch (r) {
|
|
24
|
-
|
|
24
|
+
m(r);
|
|
25
25
|
}
|
|
26
|
-
},
|
|
27
|
-
|
|
26
|
+
}, c = (n) => n.done ? a(n.value) : Promise.resolve(n.value).then(d, f);
|
|
27
|
+
c((o = o.apply(e, t)).next());
|
|
28
28
|
});
|
|
29
29
|
import { v4 as Y } from "uuid";
|
|
30
30
|
import Z from "../Logger.mjs";
|
|
31
31
|
import { HostBridgeAdapter as k } from "../localFiles/adapters/HostBridgeAdapter.mjs";
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
import {
|
|
38
|
-
import {
|
|
39
|
-
import {
|
|
32
|
+
import { domError as ee } from "../localFiles/adapters/WorkspaceFileAdapter.mjs";
|
|
33
|
+
import { SANDBOX_PROTOCOL as te } from "../localFiles/host/protocol.mjs";
|
|
34
|
+
import { cancelledError as T, workspaceNotGranted as oe, ToolError as u, ToolCode as l, toToolError as ne } from "../localFiles/errors.mjs";
|
|
35
|
+
import { validateToolInput as re } from "../localFiles/validate.mjs";
|
|
36
|
+
import { BrowserCommandRunner as ie } from "./browserRuntime/BrowserCommandRunner.mjs";
|
|
37
|
+
import { withOutputLimit as U } from "./command.mjs";
|
|
38
|
+
import { BUILTIN_FUNCTION_CODES as se, SHELL_CWD as le } from "./constants.mjs";
|
|
39
|
+
import { BUILTIN_TOOL_SCHEMAS as ae } from "./schemas.mjs";
|
|
40
|
+
import { shell as de, readFile as ce, writeFile as me, appendFile as ue, runFile as fe, editFile as ge, grep as he, find as _e, loadSkill as be, readSkillResource as ye, runSkillScript as we } from "./tools.mjs";
|
|
40
41
|
const Te = {
|
|
41
|
-
shell:
|
|
42
|
-
read_file:
|
|
43
|
-
write_file:
|
|
42
|
+
shell: de,
|
|
43
|
+
read_file: ce,
|
|
44
|
+
write_file: me,
|
|
44
45
|
append_file: ue,
|
|
45
|
-
run_file:
|
|
46
|
-
edit_file:
|
|
47
|
-
grep:
|
|
48
|
-
find:
|
|
49
|
-
load_skill:
|
|
50
|
-
read_skill_resource:
|
|
51
|
-
run_skill_script:
|
|
52
|
-
},
|
|
53
|
-
function
|
|
54
|
-
var
|
|
46
|
+
run_file: fe,
|
|
47
|
+
edit_file: ge,
|
|
48
|
+
grep: he,
|
|
49
|
+
find: _e,
|
|
50
|
+
load_skill: be,
|
|
51
|
+
read_skill_resource: ye,
|
|
52
|
+
run_skill_script: we
|
|
53
|
+
}, w = new Z("[BuiltinTools] "), j = /* @__PURE__ */ new Set(["shell", "run_file", "run_skill_script"]);
|
|
54
|
+
function pe(e, t, o) {
|
|
55
|
+
var a, m;
|
|
55
56
|
if (e.commandRunner)
|
|
56
|
-
return
|
|
57
|
-
if (t instanceof k
|
|
58
|
-
if (t.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
return U(e.commandRunner);
|
|
58
|
+
if (t instanceof k) {
|
|
59
|
+
if (t.protocolMismatch) {
|
|
60
|
+
const d = `${(a = t.sandboxVersion) != null ? a : ""}@${(m = t.sandboxProtocol) != null ? m : ""}`;
|
|
61
|
+
o.rejectedSandbox !== d && (o.rejectedSandbox = d, w.error("sandbox-protocol", {
|
|
62
|
+
sandboxVersion: t.sandboxVersion,
|
|
63
|
+
sandboxProtocol: t.sandboxProtocol,
|
|
64
|
+
supported: te
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
if (!t.canExec) {
|
|
68
|
+
const d = t.unavailableMessage();
|
|
69
|
+
return {
|
|
70
|
+
exec: () => Promise.reject(ee("NotAllowedError", d))
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
exec: (d) => t.exec(d)
|
|
75
|
+
};
|
|
68
76
|
}
|
|
69
77
|
if (e.browserRuntime.enabled)
|
|
70
|
-
return (o.adapter !== t || !o.runner) && (o.adapter = t, o.runner =
|
|
78
|
+
return (o.adapter !== t || !o.runner) && (o.adapter = t, o.runner = U(new ie({
|
|
71
79
|
adapter: t,
|
|
72
80
|
sensitive: e.sensitive,
|
|
73
81
|
moduleURL: e.browserRuntime.bashModuleURL,
|
|
74
82
|
pyodideIndexURL: e.browserRuntime.pyodideIndexURL
|
|
75
83
|
}))), o.runner;
|
|
76
84
|
}
|
|
77
|
-
function
|
|
85
|
+
function Ie(e) {
|
|
78
86
|
if (typeof e != "string")
|
|
79
87
|
return e;
|
|
80
88
|
try {
|
|
81
89
|
return JSON.parse(e);
|
|
82
90
|
} catch (t) {
|
|
83
|
-
throw new
|
|
91
|
+
throw new u(l.BadRequest, "参数不是合法的 JSON 对象。");
|
|
84
92
|
}
|
|
85
93
|
}
|
|
86
94
|
function xe(e) {
|
|
87
95
|
const t = e instanceof DOMException ? e.name : "", o = e instanceof Error ? e.message : "";
|
|
88
96
|
if (t === "NotFoundError")
|
|
89
|
-
return new
|
|
97
|
+
return new u(l.NotFound, "脚本或目录不存在。");
|
|
90
98
|
if (t === "SecurityError")
|
|
91
|
-
return new
|
|
99
|
+
return new u(l.Forbidden, "命令路径不在工作区内。");
|
|
92
100
|
if (t === "AbortError")
|
|
93
|
-
return o.includes("超时") ? new
|
|
101
|
+
return o.includes("超时") ? new u(l.Timeout, "命令运行超时,已中止。") : o.includes("已有") ? new u(l.TooManyRequests, "已有一个命令在运行。") : T();
|
|
94
102
|
if (t === "NotAllowedError")
|
|
95
|
-
return new
|
|
103
|
+
return o.startsWith("沙箱") ? new u(l.NotImplemented, o) : new u(l.NotImplemented, "当前环境不能执行命令或脚本,需要端侧宿主。");
|
|
96
104
|
}
|
|
97
|
-
function
|
|
105
|
+
function Oe(e) {
|
|
98
106
|
return new Promise((t, o) => {
|
|
99
|
-
e.aborted && o(
|
|
107
|
+
e.aborted && o(T()), e.addEventListener("abort", () => o(T()), {
|
|
100
108
|
once: !0
|
|
101
109
|
});
|
|
102
110
|
});
|
|
103
111
|
}
|
|
104
|
-
function
|
|
112
|
+
function H(e, t = 160) {
|
|
105
113
|
return e.length > t ? `${e.slice(0, t)}…` : e;
|
|
106
114
|
}
|
|
107
|
-
function
|
|
115
|
+
function Ee(e) {
|
|
108
116
|
const t = {};
|
|
109
117
|
for (const o of ["path", "skill_name", "resource_name", "script_name", "pattern", "name"])
|
|
110
118
|
typeof e[o] == "string" && (t[o] = e[o]);
|
|
111
|
-
return typeof e.command == "string" && (t.command =
|
|
119
|
+
return typeof e.command == "string" && (t.command = H(e.command)), typeof e.start_line == "number" && (t.start_line = e.start_line), typeof e.end_line == "number" && (t.end_line = e.end_line), typeof e.content == "string" && (t.contentLength = e.content.length), typeof e.old_text == "string" && (t.oldTextLength = e.old_text.length), typeof e.new_text == "string" && (t.newTextLength = e.new_text.length), Array.isArray(e.args) && (t.argCount = e.args.length), t;
|
|
112
120
|
}
|
|
113
121
|
function Le(e) {
|
|
114
122
|
return s({
|
|
@@ -118,21 +126,21 @@ function Le(e) {
|
|
|
118
126
|
data: e.data
|
|
119
127
|
} : {});
|
|
120
128
|
}
|
|
121
|
-
function
|
|
129
|
+
function He({
|
|
122
130
|
pluginCode: e,
|
|
123
131
|
workspace: t
|
|
124
132
|
}) {
|
|
125
|
-
const o = /* @__PURE__ */ new Map(),
|
|
126
|
-
return
|
|
127
|
-
|
|
133
|
+
const o = /* @__PURE__ */ new Map(), a = {}, m = {};
|
|
134
|
+
return se.forEach((d) => {
|
|
135
|
+
m[d] = (f, c, n) => {
|
|
128
136
|
let r = !1;
|
|
129
|
-
Se(t, o,
|
|
137
|
+
Se(t, o, a, d, f, (L) => {
|
|
130
138
|
if (!r) {
|
|
131
139
|
r = !0;
|
|
132
140
|
try {
|
|
133
|
-
|
|
134
|
-
} catch (
|
|
135
|
-
console.error("[BuiltinTools] callback failed",
|
|
141
|
+
c(L);
|
|
142
|
+
} catch (I) {
|
|
143
|
+
console.error("[BuiltinTools] callback failed", I);
|
|
136
144
|
}
|
|
137
145
|
}
|
|
138
146
|
}, h(s({}, n), {
|
|
@@ -140,97 +148,97 @@ function Ue({
|
|
|
140
148
|
}));
|
|
141
149
|
};
|
|
142
150
|
}), {
|
|
143
|
-
[e]:
|
|
151
|
+
[e]: m
|
|
144
152
|
};
|
|
145
153
|
}
|
|
146
|
-
function Se(e, t, o,
|
|
147
|
-
return
|
|
148
|
-
var N,
|
|
149
|
-
const
|
|
150
|
-
requestId:
|
|
151
|
-
applicationId:
|
|
152
|
-
sessionId:
|
|
153
|
-
pluginCode:
|
|
154
|
-
functionCode:
|
|
155
|
-
signal:
|
|
154
|
+
function Se(e, t, o, a, m, d, f) {
|
|
155
|
+
return D(this, null, function* () {
|
|
156
|
+
var N, R, M, F, A;
|
|
157
|
+
const c = {
|
|
158
|
+
requestId: f.requestId || Y(),
|
|
159
|
+
applicationId: f.applicationId || e.applicationId,
|
|
160
|
+
sessionId: f.sessionId || "",
|
|
161
|
+
pluginCode: f.pluginCode,
|
|
162
|
+
functionCode: a,
|
|
163
|
+
signal: f.signal
|
|
156
164
|
}, n = {
|
|
157
|
-
requestId:
|
|
158
|
-
sessionId:
|
|
159
|
-
functionCode:
|
|
165
|
+
requestId: c.requestId,
|
|
166
|
+
sessionId: c.sessionId,
|
|
167
|
+
functionCode: a
|
|
160
168
|
}, r = new AbortController();
|
|
161
169
|
let p = !1;
|
|
162
|
-
const
|
|
170
|
+
const L = setTimeout(() => {
|
|
163
171
|
p = !0, r.abort();
|
|
164
|
-
}, e.limits.toolTimeoutMs),
|
|
165
|
-
(N =
|
|
172
|
+
}, e.limits.toolTimeoutMs), I = () => r.abort();
|
|
173
|
+
(N = c.signal) == null || N.addEventListener("abort", I, {
|
|
166
174
|
once: !0
|
|
167
|
-
}), (
|
|
168
|
-
let b = null, y = null,
|
|
169
|
-
const
|
|
175
|
+
}), (R = c.signal) != null && R.aborted && r.abort();
|
|
176
|
+
let b = null, y = null, x;
|
|
177
|
+
const S = Date.now();
|
|
170
178
|
try {
|
|
171
|
-
const O =
|
|
172
|
-
if (typeof i.path == "string" && (n.path = i.path),
|
|
173
|
-
throw
|
|
179
|
+
const O = Ie(m), i = re(O, ae[a].parameters);
|
|
180
|
+
if (typeof i.path == "string" && (n.path = i.path), w.log("start", s(s({}, n), Ee(i))), yield e.ready, r.signal.aborted)
|
|
181
|
+
throw T();
|
|
174
182
|
const _ = e.adapter;
|
|
175
183
|
if (!_)
|
|
176
|
-
throw
|
|
184
|
+
throw oe();
|
|
177
185
|
if (b = e.acquireSlot(), !b)
|
|
178
|
-
throw new
|
|
179
|
-
if (
|
|
180
|
-
throw new
|
|
181
|
-
|
|
182
|
-
const v =
|
|
186
|
+
throw new u(l.TooManyRequests, "同时进行的文件操作过多,请稍后重试。");
|
|
187
|
+
if (j.has(a) && (y = e.acquireCommand(), !y))
|
|
188
|
+
throw new u(l.TooManyRequests, "已有一个命令在运行,请等它结束后再试。");
|
|
189
|
+
x = e.track(r), e.emit("tool:start", s({}, n));
|
|
190
|
+
const v = c.sessionId || "default";
|
|
183
191
|
let g = t.get(v);
|
|
184
192
|
g || (g = {
|
|
185
|
-
cwd:
|
|
193
|
+
cwd: le
|
|
186
194
|
}, t.set(v, g));
|
|
187
|
-
const
|
|
195
|
+
const V = pe(e, _, o), $ = g.cwd, J = {
|
|
188
196
|
adapter: _,
|
|
189
|
-
runner:
|
|
197
|
+
runner: V,
|
|
190
198
|
signal: r.signal,
|
|
191
199
|
timeoutMs: e.limits.toolTimeoutMs,
|
|
192
200
|
shellCwd: g
|
|
193
|
-
},
|
|
194
|
-
code:
|
|
201
|
+
}, E = yield Promise.race([Te[a](i, J), Oe(r.signal)]), q = {
|
|
202
|
+
code: l.Ok,
|
|
195
203
|
message: "ok",
|
|
196
|
-
data:
|
|
204
|
+
data: E.data
|
|
197
205
|
};
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
elapsedMs:
|
|
202
|
-
code:
|
|
203
|
-
}),
|
|
206
|
+
E.truncated && (q.truncated = !0);
|
|
207
|
+
const W = Date.now() - S, B = E.data.exitCode;
|
|
208
|
+
w.log("finish", s(s(s(h(s({}, n), {
|
|
209
|
+
elapsedMs: W,
|
|
210
|
+
code: l.Ok
|
|
211
|
+
}), E.truncated ? {
|
|
204
212
|
truncated: !0
|
|
205
|
-
} : {}), typeof
|
|
206
|
-
exitCode:
|
|
207
|
-
} : {}),
|
|
213
|
+
} : {}), typeof B == "number" ? {
|
|
214
|
+
exitCode: B
|
|
215
|
+
} : {}), a === "shell" ? {
|
|
208
216
|
cwd: g.cwd,
|
|
209
|
-
cwdChanged: g.cwd !==
|
|
217
|
+
cwdChanged: g.cwd !== $
|
|
210
218
|
} : {})), e.emit("tool:finish", h(s({}, n), {
|
|
211
|
-
code:
|
|
212
|
-
})),
|
|
219
|
+
code: l.Ok
|
|
220
|
+
})), j.has(a) && e.notifyFilesChanged(), d(q);
|
|
213
221
|
} catch (O) {
|
|
214
|
-
let i = (M = xe(O)) != null ? M :
|
|
215
|
-
p ? i = new
|
|
216
|
-
const _ = Date.now() -
|
|
217
|
-
i.code ===
|
|
222
|
+
let i = (M = xe(O)) != null ? M : ne(O);
|
|
223
|
+
p ? i = new u(l.Timeout, "操作超时,已中止。") : r.signal.aborted && i.code !== l.Cancelled && (i = T());
|
|
224
|
+
const _ = Date.now() - S;
|
|
225
|
+
i.code === l.Cancelled ? (w.log("cancelled", h(s({}, n), {
|
|
218
226
|
elapsedMs: _,
|
|
219
227
|
timedOut: p
|
|
220
|
-
})), e.emit("tool:cancelled", s({}, n))) : (
|
|
228
|
+
})), e.emit("tool:cancelled", s({}, n))) : (w.error("error", h(s({}, n), {
|
|
221
229
|
elapsedMs: _,
|
|
222
230
|
code: i.code,
|
|
223
|
-
message:
|
|
231
|
+
message: H(i.message, 300)
|
|
224
232
|
})), e.emit("tool:error", h(s({}, n), {
|
|
225
233
|
code: i.code,
|
|
226
234
|
message: i.message,
|
|
227
|
-
reason: (
|
|
228
|
-
}))),
|
|
235
|
+
reason: (F = i.data) == null ? void 0 : F.reason
|
|
236
|
+
}))), d(Le(i));
|
|
229
237
|
} finally {
|
|
230
|
-
clearTimeout(
|
|
238
|
+
clearTimeout(L), (A = c.signal) == null || A.removeEventListener("abort", I), b == null || b(), y == null || y(), x == null || x();
|
|
231
239
|
}
|
|
232
240
|
});
|
|
233
241
|
}
|
|
234
242
|
export {
|
|
235
|
-
|
|
243
|
+
He as createBuiltinToolsPlugin
|
|
236
244
|
};
|