@glodon-aiot/bot-client-sdk 3.26.0-alpha.12 → 3.26.0-alpha.14
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 +13 -12
- package/dist/es/BotClient.mjs +153 -137
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +99 -82
- package/dist/es/builtinTools/paths.mjs +28 -22
- package/dist/es/builtinTools/platformSkills.mjs +210 -0
- package/dist/es/builtinTools/schemas.mjs +2 -2
- package/dist/es/builtinTools/tools.mjs +256 -233
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +19 -18
- package/dist/es/localFiles/host/protocol.mjs +1 -1
- package/dist/es/localFiles/host/sandboxClient.mjs +171 -100
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +46 -44
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +36 -36
- package/dist/src/builtinTools/command.d.ts +7 -0
- package/dist/src/builtinTools/paths.d.ts +1 -0
- package/dist/src/builtinTools/platformSkills.d.ts +67 -0
- package/dist/src/builtinTools/tools.d.ts +4 -0
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +1 -1
- package/dist/src/localFiles/host/protocol.d.ts +1 -1
- package/dist/src/localFiles/host/sandboxClient.d.ts +15 -0
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +3 -0
- package/package.json +2 -2
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
1
|
+
var K = Object.defineProperty, X = Object.defineProperties;
|
|
2
|
+
var Q = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var C = Object.getOwnPropertySymbols;
|
|
4
|
+
var Y = Object.prototype.hasOwnProperty, Z = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var D = (e, t, o) => t in e ? K(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
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var o of
|
|
10
|
-
|
|
7
|
+
Y.call(t, o) && D(e, o, t[o]);
|
|
8
|
+
if (C)
|
|
9
|
+
for (var o of C(t))
|
|
10
|
+
Z.call(t, o) && D(e, o, t[o]);
|
|
11
11
|
return e;
|
|
12
|
-
},
|
|
13
|
-
var
|
|
12
|
+
}, _ = (e, t) => X(e, Q(t));
|
|
13
|
+
var U = (e, t, o) => new Promise((a, m) => {
|
|
14
14
|
var d = (n) => {
|
|
15
15
|
try {
|
|
16
16
|
c(o.next(n));
|
|
@@ -26,18 +26,18 @@ var D = (e, t, o) => new Promise((a, m) => {
|
|
|
26
26
|
}, c = (n) => n.done ? a(n.value) : Promise.resolve(n.value).then(d, f);
|
|
27
27
|
c((o = o.apply(e, t)).next());
|
|
28
28
|
});
|
|
29
|
-
import { v4 as
|
|
30
|
-
import
|
|
31
|
-
import { HostBridgeAdapter as
|
|
32
|
-
import { domError as
|
|
29
|
+
import { v4 as k } from "uuid";
|
|
30
|
+
import ee from "../Logger.mjs";
|
|
31
|
+
import { HostBridgeAdapter as H } from "../localFiles/adapters/HostBridgeAdapter.mjs";
|
|
32
|
+
import { domError as V } from "../localFiles/adapters/WorkspaceFileAdapter.mjs";
|
|
33
33
|
import { SANDBOX_PROTOCOL as te } from "../localFiles/host/protocol.mjs";
|
|
34
34
|
import { cancelledError as T, workspaceNotGranted as oe, ToolError as u, ToolCode as l, toToolError as ne } from "../localFiles/errors.mjs";
|
|
35
35
|
import { validateToolInput as re } from "../localFiles/validate.mjs";
|
|
36
36
|
import { BrowserCommandRunner as ie } from "./browserRuntime/BrowserCommandRunner.mjs";
|
|
37
|
-
import { withOutputLimit as
|
|
37
|
+
import { withOutputLimit as L } from "./command.mjs";
|
|
38
38
|
import { BUILTIN_FUNCTION_CODES as se, SHELL_CWD as le } from "./constants.mjs";
|
|
39
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
|
|
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 xe } from "./tools.mjs";
|
|
41
41
|
const Te = {
|
|
42
42
|
shell: de,
|
|
43
43
|
read_file: ce,
|
|
@@ -49,16 +49,16 @@ const Te = {
|
|
|
49
49
|
find: _e,
|
|
50
50
|
load_skill: be,
|
|
51
51
|
read_skill_resource: ye,
|
|
52
|
-
run_skill_script:
|
|
53
|
-
},
|
|
54
|
-
function
|
|
52
|
+
run_skill_script: xe
|
|
53
|
+
}, x = new ee("[BuiltinTools] "), j = /* @__PURE__ */ new Set(["shell", "run_file", "run_skill_script"]);
|
|
54
|
+
function we(e, t, o) {
|
|
55
55
|
var a, m;
|
|
56
56
|
if (e.commandRunner)
|
|
57
|
-
return
|
|
58
|
-
if (t instanceof
|
|
57
|
+
return L(e.commandRunner);
|
|
58
|
+
if (t instanceof H) {
|
|
59
59
|
if (t.protocolMismatch) {
|
|
60
60
|
const d = `${(a = t.sandboxVersion) != null ? a : ""}@${(m = t.sandboxProtocol) != null ? m : ""}`;
|
|
61
|
-
o.rejectedSandbox !== d && (o.rejectedSandbox = d,
|
|
61
|
+
o.rejectedSandbox !== d && (o.rejectedSandbox = d, x.error("sandbox-protocol", {
|
|
62
62
|
sandboxVersion: t.sandboxVersion,
|
|
63
63
|
sandboxProtocol: t.sandboxProtocol,
|
|
64
64
|
supported: te
|
|
@@ -67,7 +67,7 @@ function pe(e, t, o) {
|
|
|
67
67
|
if (!t.canExec) {
|
|
68
68
|
const d = t.unavailableMessage();
|
|
69
69
|
return {
|
|
70
|
-
exec: () => Promise.reject(
|
|
70
|
+
exec: () => Promise.reject(V("NotAllowedError", d))
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
return {
|
|
@@ -75,14 +75,29 @@ function pe(e, t, o) {
|
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
if (e.browserRuntime.enabled)
|
|
78
|
-
return (o.adapter !== t || !o.runner) && (o.adapter = t, o.runner =
|
|
78
|
+
return (o.adapter !== t || !o.runner) && (o.adapter = t, o.runner = L(new ie({
|
|
79
79
|
adapter: t,
|
|
80
80
|
sensitive: e.sensitive,
|
|
81
81
|
moduleURL: e.browserRuntime.bashModuleURL,
|
|
82
82
|
pyodideIndexURL: e.browserRuntime.pyodideIndexURL
|
|
83
83
|
}))), o.runner;
|
|
84
84
|
}
|
|
85
|
-
function Ie(e) {
|
|
85
|
+
function Ie(e, t) {
|
|
86
|
+
if (e.commandRunner)
|
|
87
|
+
return L(e.commandRunner);
|
|
88
|
+
if (t instanceof H) {
|
|
89
|
+
if (!t.canExec) {
|
|
90
|
+
const o = t.unavailableMessage();
|
|
91
|
+
return {
|
|
92
|
+
exec: () => Promise.reject(V("NotAllowedError", o))
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
exec: (o) => t.exec(o)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function Ee(e) {
|
|
86
101
|
if (typeof e != "string")
|
|
87
102
|
return e;
|
|
88
103
|
try {
|
|
@@ -91,7 +106,7 @@ function Ie(e) {
|
|
|
91
106
|
throw new u(l.BadRequest, "参数不是合法的 JSON 对象。");
|
|
92
107
|
}
|
|
93
108
|
}
|
|
94
|
-
function
|
|
109
|
+
function Oe(e) {
|
|
95
110
|
const t = e instanceof DOMException ? e.name : "", o = e instanceof Error ? e.message : "";
|
|
96
111
|
if (t === "NotFoundError")
|
|
97
112
|
return new u(l.NotFound, "脚本或目录不存在。");
|
|
@@ -102,21 +117,21 @@ function xe(e) {
|
|
|
102
117
|
if (t === "NotAllowedError")
|
|
103
118
|
return o.startsWith("沙箱") ? new u(l.NotImplemented, o) : new u(l.NotImplemented, "当前环境不能执行命令或脚本,需要端侧宿主。");
|
|
104
119
|
}
|
|
105
|
-
function
|
|
120
|
+
function Re(e) {
|
|
106
121
|
return new Promise((t, o) => {
|
|
107
122
|
e.aborted && o(T()), e.addEventListener("abort", () => o(T()), {
|
|
108
123
|
once: !0
|
|
109
124
|
});
|
|
110
125
|
});
|
|
111
126
|
}
|
|
112
|
-
function
|
|
127
|
+
function $(e, t = 160) {
|
|
113
128
|
return e.length > t ? `${e.slice(0, t)}…` : e;
|
|
114
129
|
}
|
|
115
|
-
function
|
|
130
|
+
function pe(e) {
|
|
116
131
|
const t = {};
|
|
117
132
|
for (const o of ["path", "skill_name", "resource_name", "script_name", "pattern", "name"])
|
|
118
133
|
typeof e[o] == "string" && (t[o] = e[o]);
|
|
119
|
-
return typeof e.command == "string" && (t.command =
|
|
134
|
+
return typeof e.command == "string" && (t.command = $(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;
|
|
120
135
|
}
|
|
121
136
|
function Le(e) {
|
|
122
137
|
return s({
|
|
@@ -126,7 +141,7 @@ function Le(e) {
|
|
|
126
141
|
data: e.data
|
|
127
142
|
} : {});
|
|
128
143
|
}
|
|
129
|
-
function
|
|
144
|
+
function Ve({
|
|
130
145
|
pluginCode: e,
|
|
131
146
|
workspace: t
|
|
132
147
|
}) {
|
|
@@ -134,16 +149,16 @@ function He({
|
|
|
134
149
|
return se.forEach((d) => {
|
|
135
150
|
m[d] = (f, c, n) => {
|
|
136
151
|
let r = !1;
|
|
137
|
-
Se(t, o, a, d, f, (
|
|
152
|
+
Se(t, o, a, d, f, (p) => {
|
|
138
153
|
if (!r) {
|
|
139
154
|
r = !0;
|
|
140
155
|
try {
|
|
141
|
-
c(
|
|
156
|
+
c(p);
|
|
142
157
|
} catch (I) {
|
|
143
158
|
console.error("[BuiltinTools] callback failed", I);
|
|
144
159
|
}
|
|
145
160
|
}
|
|
146
|
-
},
|
|
161
|
+
}, _(s({}, n), {
|
|
147
162
|
pluginCode: e
|
|
148
163
|
}));
|
|
149
164
|
};
|
|
@@ -152,10 +167,10 @@ function He({
|
|
|
152
167
|
};
|
|
153
168
|
}
|
|
154
169
|
function Se(e, t, o, a, m, d, f) {
|
|
155
|
-
return
|
|
156
|
-
var N,
|
|
170
|
+
return U(this, null, function* () {
|
|
171
|
+
var N, M, F, A, v;
|
|
157
172
|
const c = {
|
|
158
|
-
requestId: f.requestId ||
|
|
173
|
+
requestId: f.requestId || k(),
|
|
159
174
|
applicationId: f.applicationId || e.applicationId,
|
|
160
175
|
sessionId: f.sessionId || "",
|
|
161
176
|
pluginCode: f.pluginCode,
|
|
@@ -166,79 +181,81 @@ function Se(e, t, o, a, m, d, f) {
|
|
|
166
181
|
sessionId: c.sessionId,
|
|
167
182
|
functionCode: a
|
|
168
183
|
}, r = new AbortController();
|
|
169
|
-
let
|
|
170
|
-
const
|
|
171
|
-
|
|
184
|
+
let w = !1;
|
|
185
|
+
const p = setTimeout(() => {
|
|
186
|
+
w = !0, r.abort();
|
|
172
187
|
}, e.limits.toolTimeoutMs), I = () => r.abort();
|
|
173
188
|
(N = c.signal) == null || N.addEventListener("abort", I, {
|
|
174
189
|
once: !0
|
|
175
|
-
}), (
|
|
176
|
-
let b = null, y = null,
|
|
190
|
+
}), (M = c.signal) != null && M.aborted && r.abort();
|
|
191
|
+
let b = null, y = null, E;
|
|
177
192
|
const S = Date.now();
|
|
178
193
|
try {
|
|
179
|
-
const O =
|
|
180
|
-
if (typeof i.path == "string" && (n.path = i.path),
|
|
194
|
+
const O = Ee(m), i = re(O, ae[a].parameters);
|
|
195
|
+
if (typeof i.path == "string" && (n.path = i.path), x.log("start", s(s({}, n), pe(i))), yield e.ready, r.signal.aborted)
|
|
181
196
|
throw T();
|
|
182
|
-
const
|
|
183
|
-
if (!
|
|
197
|
+
const g = e.adapter;
|
|
198
|
+
if (!g)
|
|
184
199
|
throw oe();
|
|
185
200
|
if (b = e.acquireSlot(), !b)
|
|
186
201
|
throw new u(l.TooManyRequests, "同时进行的文件操作过多,请稍后重试。");
|
|
187
202
|
if (j.has(a) && (y = e.acquireCommand(), !y))
|
|
188
203
|
throw new u(l.TooManyRequests, "已有一个命令在运行,请等它结束后再试。");
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
let
|
|
192
|
-
|
|
204
|
+
E = e.track(r), e.emit("tool:start", s({}, n));
|
|
205
|
+
const q = c.sessionId || "default";
|
|
206
|
+
let h = t.get(q);
|
|
207
|
+
h || (h = {
|
|
193
208
|
cwd: le
|
|
194
|
-
}, t.set(
|
|
195
|
-
const
|
|
196
|
-
adapter:
|
|
197
|
-
runner:
|
|
209
|
+
}, t.set(q, h));
|
|
210
|
+
const J = we(e, g, o), W = h.cwd, z = {
|
|
211
|
+
adapter: g,
|
|
212
|
+
runner: J,
|
|
198
213
|
signal: r.signal,
|
|
199
214
|
timeoutMs: e.limits.toolTimeoutMs,
|
|
200
|
-
shellCwd:
|
|
201
|
-
|
|
215
|
+
shellCwd: h,
|
|
216
|
+
skills: e.platformSkills,
|
|
217
|
+
skillRunner: Ie(e, g)
|
|
218
|
+
}, R = yield Promise.race([Te[a](i, z), Re(r.signal)]), B = {
|
|
202
219
|
code: l.Ok,
|
|
203
220
|
message: "ok",
|
|
204
|
-
data:
|
|
221
|
+
data: R.data
|
|
205
222
|
};
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
elapsedMs:
|
|
223
|
+
R.truncated && (B.truncated = !0);
|
|
224
|
+
const G = Date.now() - S, P = R.data.exitCode;
|
|
225
|
+
x.log("finish", s(s(s(_(s({}, n), {
|
|
226
|
+
elapsedMs: G,
|
|
210
227
|
code: l.Ok
|
|
211
|
-
}),
|
|
228
|
+
}), R.truncated ? {
|
|
212
229
|
truncated: !0
|
|
213
|
-
} : {}), typeof
|
|
214
|
-
exitCode:
|
|
230
|
+
} : {}), typeof P == "number" ? {
|
|
231
|
+
exitCode: P
|
|
215
232
|
} : {}), a === "shell" ? {
|
|
216
|
-
cwd:
|
|
217
|
-
cwdChanged:
|
|
218
|
-
} : {})), e.emit("tool:finish",
|
|
233
|
+
cwd: h.cwd,
|
|
234
|
+
cwdChanged: h.cwd !== W
|
|
235
|
+
} : {})), e.emit("tool:finish", _(s({}, n), {
|
|
219
236
|
code: l.Ok
|
|
220
|
-
})), j.has(a) && e.notifyFilesChanged(), d(
|
|
237
|
+
})), j.has(a) && e.notifyFilesChanged(), d(B);
|
|
221
238
|
} catch (O) {
|
|
222
|
-
let i = (
|
|
223
|
-
|
|
224
|
-
const
|
|
225
|
-
i.code === l.Cancelled ? (
|
|
226
|
-
elapsedMs:
|
|
227
|
-
timedOut:
|
|
228
|
-
})), e.emit("tool:cancelled", s({}, n))) : (
|
|
229
|
-
elapsedMs:
|
|
239
|
+
let i = (F = Oe(O)) != null ? F : ne(O);
|
|
240
|
+
w ? i = new u(l.Timeout, "操作超时,已中止。") : r.signal.aborted && i.code !== l.Cancelled && (i = T());
|
|
241
|
+
const g = Date.now() - S;
|
|
242
|
+
i.code === l.Cancelled ? (x.log("cancelled", _(s({}, n), {
|
|
243
|
+
elapsedMs: g,
|
|
244
|
+
timedOut: w
|
|
245
|
+
})), e.emit("tool:cancelled", s({}, n))) : (x.error("error", _(s({}, n), {
|
|
246
|
+
elapsedMs: g,
|
|
230
247
|
code: i.code,
|
|
231
|
-
message:
|
|
232
|
-
})), e.emit("tool:error",
|
|
248
|
+
message: $(i.message, 300)
|
|
249
|
+
})), e.emit("tool:error", _(s({}, n), {
|
|
233
250
|
code: i.code,
|
|
234
251
|
message: i.message,
|
|
235
|
-
reason: (
|
|
252
|
+
reason: (A = i.data) == null ? void 0 : A.reason
|
|
236
253
|
}))), d(Le(i));
|
|
237
254
|
} finally {
|
|
238
|
-
clearTimeout(
|
|
255
|
+
clearTimeout(p), (v = c.signal) == null || v.removeEventListener("abort", I), b == null || b(), y == null || y(), E == null || E();
|
|
239
256
|
}
|
|
240
257
|
});
|
|
241
258
|
}
|
|
242
259
|
export {
|
|
243
|
-
|
|
260
|
+
Ve as createBuiltinToolsPlugin
|
|
244
261
|
};
|
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { ToolError as o, ToolCode as i } from "../localFiles/errors.mjs";
|
|
2
|
-
const
|
|
3
|
-
function
|
|
4
|
-
return
|
|
2
|
+
const f = ["input", "output", "uploads", "tmp"], c = "不要使用 input、output、uploads、tmp,也不要新建这些目录。请把文件直接放在工作区已有位置或根目录。", h = /(?:^|[\s'"=;|&<>`:])\/?(?:input|output|uploads|tmp)(?=\/|[\s'"=;|&<>`:]|$)/, d = /(?:^|[\s'"=;|&<>`:])\/?skills(?=\/|[\s'"=;|&<>`:]|$)/;
|
|
3
|
+
function a(t) {
|
|
4
|
+
return d.test(t);
|
|
5
|
+
}
|
|
6
|
+
function w(t) {
|
|
7
|
+
return h.test(t);
|
|
5
8
|
}
|
|
6
9
|
function u(t) {
|
|
7
10
|
const n = t.split("/")[0];
|
|
8
|
-
return
|
|
11
|
+
return f.includes(n);
|
|
9
12
|
}
|
|
10
13
|
function r(t) {
|
|
11
14
|
throw new o(i.Forbidden, t, {
|
|
12
15
|
reason: "path_outside_workspace"
|
|
13
16
|
});
|
|
14
17
|
}
|
|
15
|
-
function
|
|
18
|
+
function l(t) {
|
|
16
19
|
if (typeof t != "string" || t.length === 0)
|
|
17
20
|
throw new o(i.BadRequest, "path 必须是工作区内绝对路径。");
|
|
18
21
|
(t.includes("\0") || t.includes("\\")) && r("路径不能包含反斜杠。");
|
|
@@ -21,19 +24,21 @@ function f(t) {
|
|
|
21
24
|
throw new o(i.BadRequest, "path 必须是工作区内绝对路径,例如 /说明.md。");
|
|
22
25
|
const e = n.split("/").filter((s) => s.length > 0);
|
|
23
26
|
if (e.some((s) => s === "." || s === "..") && r("路径不能包含 . 或 .. 。"), e.length > 0 && u(e[0]))
|
|
24
|
-
throw new o(i.BadRequest,
|
|
27
|
+
throw new o(i.BadRequest, c);
|
|
28
|
+
if (e[0] === "skills")
|
|
29
|
+
throw new o(i.Forbidden, "技能文件只能通过技能工具访问。");
|
|
25
30
|
return e.join("/");
|
|
26
31
|
}
|
|
27
|
-
function
|
|
28
|
-
return
|
|
32
|
+
function R(t) {
|
|
33
|
+
return l(t);
|
|
29
34
|
}
|
|
30
|
-
function
|
|
31
|
-
return
|
|
35
|
+
function S(t) {
|
|
36
|
+
return l(t);
|
|
32
37
|
}
|
|
33
|
-
function
|
|
38
|
+
function g(t) {
|
|
34
39
|
return t ? `/${t}` : "/";
|
|
35
40
|
}
|
|
36
|
-
function
|
|
41
|
+
function I(t) {
|
|
37
42
|
if (t === ".")
|
|
38
43
|
return !0;
|
|
39
44
|
if (!t || t.startsWith("/") || t.includes("\\") || t.includes("\0"))
|
|
@@ -41,7 +46,7 @@ function S(t) {
|
|
|
41
46
|
const n = t.split("/").filter((e) => e.length > 0);
|
|
42
47
|
return n.length > 0 && n.every((e) => e !== "." && e !== "..") && !u(n[0]);
|
|
43
48
|
}
|
|
44
|
-
function
|
|
49
|
+
function T(t, n) {
|
|
45
50
|
if (!t || /[\\/\0]/.test(t) || t === "." || t === "..")
|
|
46
51
|
throw new o(i.BadRequest, "skill_name 必须是单个技能目录名。");
|
|
47
52
|
if (!n || n.startsWith("/") || n.includes("\\") || n.includes("\0"))
|
|
@@ -50,14 +55,15 @@ function g(t, n) {
|
|
|
50
55
|
return (e.length === 0 || e.some((s) => s === "." || s === "..")) && r("资源路径不能跳出技能目录。"), `skills/${t}/${e.join("/")}`;
|
|
51
56
|
}
|
|
52
57
|
export {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
f as SYNTHETIC_DIRS,
|
|
59
|
+
c as SYNTHETIC_DIR_MESSAGE,
|
|
60
|
+
I as isSafeCwd,
|
|
56
61
|
u as isSyntheticCwd,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
a as mentionsSkillDir,
|
|
63
|
+
w as mentionsSyntheticDir,
|
|
64
|
+
R as resolveFilePath,
|
|
65
|
+
S as resolveRunPath,
|
|
66
|
+
l as resolveWorkspacePath,
|
|
67
|
+
T as skillRelative,
|
|
68
|
+
g as toVirtualPath
|
|
63
69
|
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
var k = Object.defineProperty;
|
|
2
|
+
var v = (s, t, n) => t in s ? k(s, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : s[t] = n;
|
|
3
|
+
var f = (s, t, n) => (v(s, typeof t != "symbol" ? t + "" : t, n), n);
|
|
4
|
+
var a = (s, t, n) => new Promise((e, i) => {
|
|
5
|
+
var r = (o) => {
|
|
6
|
+
try {
|
|
7
|
+
d(n.next(o));
|
|
8
|
+
} catch (u) {
|
|
9
|
+
i(u);
|
|
10
|
+
}
|
|
11
|
+
}, l = (o) => {
|
|
12
|
+
try {
|
|
13
|
+
d(n.throw(o));
|
|
14
|
+
} catch (u) {
|
|
15
|
+
i(u);
|
|
16
|
+
}
|
|
17
|
+
}, d = (o) => o.done ? e(o.value) : Promise.resolve(o.value).then(r, l);
|
|
18
|
+
d((n = n.apply(s, t)).next());
|
|
19
|
+
});
|
|
20
|
+
import { ToolError as c, ToolCode as p } from "../localFiles/errors.mjs";
|
|
21
|
+
import { isExpired as w } from "../utils.mjs";
|
|
22
|
+
const b = 20 * 1024 * 1024, I = /^[A-Za-z0-9._-]+$/;
|
|
23
|
+
function h(s) {
|
|
24
|
+
const t = s == null ? void 0 : s.skills;
|
|
25
|
+
return Array.isArray(t) ? t.filter((n) => n && typeof n == "object") : [];
|
|
26
|
+
}
|
|
27
|
+
function m(s) {
|
|
28
|
+
if (!s)
|
|
29
|
+
return !1;
|
|
30
|
+
try {
|
|
31
|
+
return !w(s);
|
|
32
|
+
} catch (t) {
|
|
33
|
+
return !1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function y(s) {
|
|
37
|
+
if (!(typeof s.skillId != "string" || !I.test(s.skillId)) && !(typeof s.name != "string" || !s.name || /[\\/\0]/.test(s.name) || s.name === "." || s.name === "..") && !(typeof s.version != "number" || !Number.isInteger(s.version) || s.version < 1))
|
|
38
|
+
return {
|
|
39
|
+
skillId: s.skillId,
|
|
40
|
+
name: s.name,
|
|
41
|
+
version: s.version
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function S(s) {
|
|
45
|
+
return Array.isArray(s) ? s.flatMap((t) => {
|
|
46
|
+
if (!t || typeof t != "object")
|
|
47
|
+
return [];
|
|
48
|
+
const n = t;
|
|
49
|
+
return typeof n.skillId != "string" || typeof n.name != "string" || typeof n.version != "number" ? [] : [{
|
|
50
|
+
skillId: n.skillId,
|
|
51
|
+
name: n.name,
|
|
52
|
+
version: n.version
|
|
53
|
+
}];
|
|
54
|
+
}) : [];
|
|
55
|
+
}
|
|
56
|
+
class R {
|
|
57
|
+
constructor(t) {
|
|
58
|
+
f(this, "inflight", /* @__PURE__ */ new Map());
|
|
59
|
+
f(this, "busy", /* @__PURE__ */ new Set());
|
|
60
|
+
this.options = t;
|
|
61
|
+
}
|
|
62
|
+
/** 进入会话时调用。不阻塞历史,也不改沙箱就绪。 */
|
|
63
|
+
sync() {
|
|
64
|
+
return this.reconcile().catch(() => {
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
read(t, n, e) {
|
|
68
|
+
return a(this, null, function* () {
|
|
69
|
+
const i = yield this.prepare(t, e);
|
|
70
|
+
return this.requireSandbox().skillRead(this.options.applicationId(), i.skillId, i.version, n, e);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
target(t, n) {
|
|
74
|
+
return a(this, null, function* () {
|
|
75
|
+
const e = yield this.prepare(t, n);
|
|
76
|
+
return {
|
|
77
|
+
applicationId: this.options.applicationId(),
|
|
78
|
+
skillId: e.skillId,
|
|
79
|
+
version: e.version,
|
|
80
|
+
name: e.name
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
track(t, n) {
|
|
85
|
+
const e = `${t}/${n}`;
|
|
86
|
+
this.busy.add(e);
|
|
87
|
+
let i = !1;
|
|
88
|
+
return () => {
|
|
89
|
+
i || (i = !0, this.busy.delete(e), this.prune().catch(() => {
|
|
90
|
+
}));
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
requireSandbox() {
|
|
94
|
+
const t = this.options.getSandbox();
|
|
95
|
+
if (!t)
|
|
96
|
+
throw new c(p.NotImplemented, "当前环境不能加载平台技能,需要端侧宿主。");
|
|
97
|
+
return t;
|
|
98
|
+
}
|
|
99
|
+
prepare(t, n) {
|
|
100
|
+
return a(this, null, function* () {
|
|
101
|
+
const e = this.bound(t);
|
|
102
|
+
if (!e)
|
|
103
|
+
throw new c(p.NotFound, "技能或资源不存在。");
|
|
104
|
+
return yield this.ensure(e, n), e;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
bound(t) {
|
|
108
|
+
if (!I.test(this.options.applicationId()))
|
|
109
|
+
return;
|
|
110
|
+
const n = h({
|
|
111
|
+
skills: this.options.getSkills()
|
|
112
|
+
}).find((e) => e.name === t);
|
|
113
|
+
return n ? y(n) : void 0;
|
|
114
|
+
}
|
|
115
|
+
reconcile() {
|
|
116
|
+
return a(this, null, function* () {
|
|
117
|
+
if (!this.options.getSandbox())
|
|
118
|
+
return;
|
|
119
|
+
const n = h({
|
|
120
|
+
skills: this.options.getSkills()
|
|
121
|
+
}).flatMap((e) => {
|
|
122
|
+
const i = y(e);
|
|
123
|
+
return i ? [i] : [];
|
|
124
|
+
});
|
|
125
|
+
yield Promise.all(n.map((e) => this.ensure(e).catch(() => {
|
|
126
|
+
}))), yield this.prune();
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
ensure(t, n) {
|
|
130
|
+
return a(this, null, function* () {
|
|
131
|
+
const e = `${this.options.applicationId()}/${t.skillId}/${t.version}`, i = this.inflight.get(e);
|
|
132
|
+
if (i)
|
|
133
|
+
return i;
|
|
134
|
+
const r = this.installIfNeeded(t, n).finally(() => {
|
|
135
|
+
this.inflight.get(e) === r && this.inflight.delete(e);
|
|
136
|
+
});
|
|
137
|
+
return this.inflight.set(e, r), r;
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
installIfNeeded(t, n) {
|
|
141
|
+
return a(this, null, function* () {
|
|
142
|
+
var o;
|
|
143
|
+
const e = this.requireSandbox();
|
|
144
|
+
if (e.ready && (yield this.alreadyInstalled(e, t, n)))
|
|
145
|
+
return;
|
|
146
|
+
const i = this.options.getToken();
|
|
147
|
+
if (!m(i))
|
|
148
|
+
throw new c(p.Forbidden, "技能下载令牌不可用。");
|
|
149
|
+
const r = this.options.apiRoot().replace(/\/$/, ""), l = yield ((o = this.options.fetch) != null ? o : fetch)(`${r}/skill/v1/skills/${encodeURIComponent(t.skillId)}/versions/${t.version}/download`, {
|
|
150
|
+
headers: {
|
|
151
|
+
Authorization: `Bearer ${i}`
|
|
152
|
+
},
|
|
153
|
+
signal: n
|
|
154
|
+
});
|
|
155
|
+
if (!l.ok)
|
|
156
|
+
throw new c(p.BadRequest, `技能下载失败(${l.status})。`);
|
|
157
|
+
const d = new Uint8Array(yield l.arrayBuffer());
|
|
158
|
+
if (d.byteLength > b)
|
|
159
|
+
throw new c(p.BadRequest, "技能压缩包超过 20 MB。");
|
|
160
|
+
yield this.waitReady(e, n), !(yield this.alreadyInstalled(e, t, n)) && (yield e.installSkill({
|
|
161
|
+
applicationId: this.options.applicationId(),
|
|
162
|
+
skillId: t.skillId,
|
|
163
|
+
version: t.version,
|
|
164
|
+
name: t.name
|
|
165
|
+
}, d, n));
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
alreadyInstalled(t, n, e) {
|
|
169
|
+
return a(this, null, function* () {
|
|
170
|
+
return S(yield t.skillStatus(this.options.applicationId(), e)).some((r) => r.skillId === n.skillId && r.version === n.version && r.name === n.name);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
prune() {
|
|
174
|
+
return a(this, null, function* () {
|
|
175
|
+
const t = this.options.getSandbox();
|
|
176
|
+
if (!(t != null && t.ready))
|
|
177
|
+
return;
|
|
178
|
+
const n = h({
|
|
179
|
+
skills: this.options.getSkills()
|
|
180
|
+
}).flatMap((i) => typeof i.skillId != "string" || typeof i.version != "number" || i.version < 1 ? [] : [{
|
|
181
|
+
skillId: i.skillId,
|
|
182
|
+
version: i.version
|
|
183
|
+
}]), e = [...this.busy].map((i) => {
|
|
184
|
+
const [r, l] = i.split("/");
|
|
185
|
+
return {
|
|
186
|
+
skillId: r,
|
|
187
|
+
version: Number(l)
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
yield t.skillPrune(this.options.applicationId(), n, e);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
waitReady(t, n) {
|
|
194
|
+
return a(this, null, function* () {
|
|
195
|
+
t.ready || (yield new Promise((e, i) => {
|
|
196
|
+
const r = setInterval(() => {
|
|
197
|
+
if (n != null && n.aborted) {
|
|
198
|
+
clearInterval(r), i(new c(p.Cancelled, "已取消。"));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
t.ready && (clearInterval(r), e());
|
|
202
|
+
}, 50);
|
|
203
|
+
}));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export {
|
|
208
|
+
R as PlatformSkillSync,
|
|
209
|
+
h as listedSkills
|
|
210
|
+
};
|
|
@@ -145,7 +145,7 @@ const e = (i) => ({
|
|
|
145
145
|
load_skill: {
|
|
146
146
|
functionCode: "load_skill",
|
|
147
147
|
name: "加载技能",
|
|
148
|
-
description: "
|
|
148
|
+
description: "按技能名称加载平台技能的完整说明(SKILL.md)。技能不在工程目录里。",
|
|
149
149
|
parameters: {
|
|
150
150
|
type: "object",
|
|
151
151
|
required: ["skill_name"],
|
|
@@ -182,7 +182,7 @@ const e = (i) => ({
|
|
|
182
182
|
run_skill_script: {
|
|
183
183
|
functionCode: "run_skill_script",
|
|
184
184
|
name: "运行技能脚本",
|
|
185
|
-
description: "
|
|
185
|
+
description: "执行平台技能包内的脚本并返回输出。当前目录是工作区根目录,参数里的用户文件按相对工作区根目录传,例如 docs/a.docx。技能自己的资源按脚本文件所在目录查找,不在工程的 skills 目录里。",
|
|
186
186
|
parameters: {
|
|
187
187
|
type: "object",
|
|
188
188
|
required: ["skill_name", "script_name"],
|