@glodon-aiot/bot-client-sdk 3.26.0-alpha.21 → 3.26.0-alpha.23
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 +130 -180
- package/dist/es/BotClient.mjs +136 -154
- package/dist/es/Config.mjs +88 -51
- package/dist/es/Session.mjs +23 -35
- package/dist/es/index.mjs +64 -66
- package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +113 -185
- package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +137 -127
- package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +97 -132
- package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +28 -32
- package/dist/es/localFiles/constants.mjs +29 -24
- package/dist/es/localFiles/createLocalPythonPlugin.mjs +196 -0
- package/dist/es/localFiles/errors.mjs +19 -21
- package/dist/es/localFiles/host/connectHostBridge.mjs +34 -37
- package/dist/es/localFiles/host/protocol.mjs +13 -27
- package/dist/es/localFiles/schemas.mjs +86 -40
- package/dist/es/localFiles/validate.mjs +37 -37
- package/dist/es/localFiles/workspace/LocalWorkspace.mjs +87 -161
- package/dist/es/package.json.mjs +29 -38
- package/dist/lib/index.js +9 -373
- package/dist/src/Config.d.ts +7 -5
- package/dist/src/Session.d.ts +0 -6
- package/dist/src/index.d.ts +1 -2
- package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +1 -4
- package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +14 -30
- package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +1 -4
- package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +0 -22
- package/dist/src/localFiles/adapters/fsaTypes.d.ts +0 -2
- package/dist/src/localFiles/constants.d.ts +13 -3
- package/dist/src/localFiles/host/protocol.d.ts +16 -34
- package/dist/src/localFiles/index.d.ts +6 -11
- package/dist/src/localFiles/schemas.d.ts +5 -2
- package/dist/src/localFiles/types.d.ts +23 -16
- package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +1 -32
- package/package.json +4 -13
- package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +0 -201
- package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +0 -356
- package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +0 -350
- package/dist/es/builtinTools/browserRuntime/assets.mjs +0 -9
- package/dist/es/builtinTools/browserRuntime/loadBash.mjs +0 -17
- package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +0 -96
- package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +0 -242
- package/dist/es/builtinTools/command.mjs +0 -56
- package/dist/es/builtinTools/constants.mjs +0 -10
- package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +0 -261
- package/dist/es/builtinTools/overview.mjs +0 -77
- package/dist/es/builtinTools/paths.mjs +0 -69
- package/dist/es/builtinTools/platformSkills.mjs +0 -210
- package/dist/es/builtinTools/schemas.mjs +0 -216
- package/dist/es/builtinTools/tools.mjs +0 -494
- package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +0 -160
- package/dist/es/localFiles/host/sandboxClient.mjs +0 -420
- package/dist/es/localFiles/host/sandboxFrames.mjs +0 -21
- package/dist/es/localFiles/preview.mjs +0 -33
- package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +0 -246
- package/dist/es/sessionUpload.mjs +0 -49
- package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +0 -26
- package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +0 -67
- package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +0 -51
- package/dist/src/builtinTools/browserRuntime/assets.d.ts +0 -13
- package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +0 -3
- package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +0 -44
- package/dist/src/builtinTools/command.d.ts +0 -35
- package/dist/src/builtinTools/constants.d.ts +0 -17
- package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +0 -11
- package/dist/src/builtinTools/overview.d.ts +0 -5
- package/dist/src/builtinTools/paths.d.ts +0 -16
- package/dist/src/builtinTools/platformSkills.d.ts +0 -67
- package/dist/src/builtinTools/schemas.d.ts +0 -3
- package/dist/src/builtinTools/tools.d.ts +0 -64
- package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +0 -24
- package/dist/src/localFiles/host/sandboxClient.d.ts +0 -81
- package/dist/src/localFiles/host/sandboxFrames.d.ts +0 -9
- package/dist/src/localFiles/preview.d.ts +0 -2
- package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +0 -63
- package/dist/src/sessionUpload.d.ts +0 -14
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
const e = `// Pyodide 专用 Worker。PythonRunner 用这份源码创建 Blob Module Worker,不经过集成方的打包器,
|
|
2
|
-
// 所以这里只能写浏览器直接能跑的 JS,不能有 import/export 语句和顶层 await。
|
|
3
|
-
//
|
|
4
|
-
// init { indexURL, packages, runner } → ready | failed { message }
|
|
5
|
-
// run { id, files, dirs, cwd, mode, target, argv, stdin } → result { id, exitCode, stdout, stderr, written, removed, createdDirs, removedDirs }
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
const ROOTS = ['/workspace'];
|
|
9
|
-
const OUTPUT_LIMIT = 16 * 1024 * 1024;
|
|
10
|
-
|
|
11
|
-
let pyodide = null;
|
|
12
|
-
let runPython = null;
|
|
13
|
-
let sink = null;
|
|
14
|
-
|
|
15
|
-
/** Emscripten 的 ErrnoError 不是 Error,只带 errno 和 code。 */
|
|
16
|
-
function describe(error) {
|
|
17
|
-
if (error && error.message) return error.message;
|
|
18
|
-
if (error && (error.code || error.errno !== undefined)) return \`文件系统错误 \${error.code || \`errno \${error.errno}\`}\`;
|
|
19
|
-
return String(error);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function networkDenied(url) {
|
|
23
|
-
return new TypeError(\`浏览器里的 Python 不能访问网络:\${url}\`);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** 只放行 indexURL 下的资源(Pyodide 自身和白名单 wheel),其余请求一律拒绝。 */
|
|
27
|
-
function guardNetwork(indexURL) {
|
|
28
|
-
const realFetch = globalThis.fetch ? globalThis.fetch.bind(globalThis) : null;
|
|
29
|
-
const resolve = (input) => {
|
|
30
|
-
const raw = typeof input === 'string' || input instanceof URL ? String(input) : input && input.url;
|
|
31
|
-
try {
|
|
32
|
-
return new URL(raw, indexURL).href;
|
|
33
|
-
} catch {
|
|
34
|
-
return String(raw);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
globalThis.fetch = (input, init) => {
|
|
38
|
-
const url = resolve(input);
|
|
39
|
-
if (!realFetch || !url.startsWith(indexURL)) return Promise.reject(networkDenied(url));
|
|
40
|
-
return realFetch(input, init);
|
|
41
|
-
};
|
|
42
|
-
for (const name of ['XMLHttpRequest', 'WebSocket', 'EventSource']) {
|
|
43
|
-
globalThis[name] = class {
|
|
44
|
-
constructor(url) {
|
|
45
|
-
throw networkDenied(url === undefined ? name : resolve(url));
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function collector() {
|
|
52
|
-
const decoder = new TextDecoder();
|
|
53
|
-
let text = '';
|
|
54
|
-
let size = 0;
|
|
55
|
-
let dropped = false;
|
|
56
|
-
return {
|
|
57
|
-
write(bytes) {
|
|
58
|
-
if (dropped || size + bytes.length > OUTPUT_LIMIT) {
|
|
59
|
-
dropped = true;
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
size += bytes.length;
|
|
63
|
-
text += decoder.decode(bytes, { stream: true });
|
|
64
|
-
},
|
|
65
|
-
finish() {
|
|
66
|
-
return text + decoder.decode() + (dropped ? '\\n…输出超过 16 MB,后面的内容已丢弃\\n' : '');
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function init({ indexURL, packages, runner }) {
|
|
72
|
-
guardNetwork(indexURL);
|
|
73
|
-
const { loadPyodide } = await import(indexURL + 'pyodide.mjs');
|
|
74
|
-
pyodide = await loadPyodide({ indexURL, env: { HOME: '/home/pyodide', MPLBACKEND: 'Agg' } });
|
|
75
|
-
const stream = (name) => ({
|
|
76
|
-
write(bytes) {
|
|
77
|
-
if (sink) sink[name].write(bytes);
|
|
78
|
-
return bytes.length;
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
pyodide.setStdout(stream('stdout'));
|
|
82
|
-
pyodide.setStderr(stream('stderr'));
|
|
83
|
-
runPython = pyodide.runPython(runner, { globals: pyodide.toPy({ PACKAGES: packages }) });
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function dirname(path) {
|
|
87
|
-
const index = path.lastIndexOf('/');
|
|
88
|
-
return index <= 0 ? '/' : path.slice(0, index);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** 主脚本、\`-c\` 代码,以及主脚本目录和 cwd 下的 .py 文件里出现的 import。 */
|
|
92
|
-
function sourcesToScan({ files, cwd, mode, target }) {
|
|
93
|
-
const decoder = new TextDecoder();
|
|
94
|
-
const sources = mode === 'c' ? [target] : [];
|
|
95
|
-
const dirs = new Set([cwd]);
|
|
96
|
-
if (mode === 'file') dirs.add(dirname(target));
|
|
97
|
-
for (const file of files) {
|
|
98
|
-
if (file.path.endsWith('.py') && (file.path === target || dirs.has(dirname(file.path)))) {
|
|
99
|
-
sources.push(decoder.decode(file.data));
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return sources;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async function loadPackages(sources, stderr) {
|
|
106
|
-
const encoder = new TextEncoder();
|
|
107
|
-
const report = (message) => stderr.write(encoder.encode(\`\${message}\\n\`));
|
|
108
|
-
for (const source of sources) {
|
|
109
|
-
try {
|
|
110
|
-
await pyodide.loadPackagesFromImports(source, { messageCallback() {}, errorCallback: report });
|
|
111
|
-
} catch (error) {
|
|
112
|
-
report(\`加载 Python 包失败:\${describe(error)}\`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function lstat(path) {
|
|
118
|
-
try {
|
|
119
|
-
return pyodide.FS.lstat(path);
|
|
120
|
-
} catch {
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function removeTree(path) {
|
|
126
|
-
const { FS } = pyodide;
|
|
127
|
-
const stat = lstat(path);
|
|
128
|
-
if (!stat) return;
|
|
129
|
-
if (!FS.isDir(stat.mode)) {
|
|
130
|
-
FS.unlink(path);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
for (const name of FS.readdir(path)) {
|
|
134
|
-
if (name !== '.' && name !== '..') removeTree(\`\${path}/\${name}\`);
|
|
135
|
-
}
|
|
136
|
-
FS.rmdir(path);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function walk(path, files, dirs) {
|
|
140
|
-
const { FS } = pyodide;
|
|
141
|
-
for (const name of FS.readdir(path)) {
|
|
142
|
-
if (name === '.' || name === '..') continue;
|
|
143
|
-
const child = \`\${path}/\${name}\`;
|
|
144
|
-
const stat = lstat(child);
|
|
145
|
-
if (!stat) continue;
|
|
146
|
-
if (FS.isDir(stat.mode)) {
|
|
147
|
-
dirs.add(child);
|
|
148
|
-
walk(child, files, dirs);
|
|
149
|
-
} else if (FS.isFile(stat.mode)) {
|
|
150
|
-
files.set(child, FS.readFile(child));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function sameBytes(a, b) {
|
|
156
|
-
if (a.length !== b.length) return false;
|
|
157
|
-
for (let index = 0; index < a.length; index += 1) if (a[index] !== b[index]) return false;
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function execute(message) {
|
|
162
|
-
const { FS } = pyodide;
|
|
163
|
-
const output = { stdout: collector(), stderr: collector() };
|
|
164
|
-
await loadPackages(sourcesToScan(message), output.stderr);
|
|
165
|
-
|
|
166
|
-
const before = new Map();
|
|
167
|
-
const beforeDirs = new Set(ROOTS);
|
|
168
|
-
// 上次运行停在某个根目录里时,Emscripten 不允许删除当前目录。
|
|
169
|
-
FS.chdir('/');
|
|
170
|
-
for (const root of ROOTS) {
|
|
171
|
-
removeTree(root);
|
|
172
|
-
FS.mkdirTree(root);
|
|
173
|
-
}
|
|
174
|
-
for (const dir of message.dirs) {
|
|
175
|
-
FS.mkdirTree(dir);
|
|
176
|
-
beforeDirs.add(dir);
|
|
177
|
-
}
|
|
178
|
-
for (const file of message.files) {
|
|
179
|
-
FS.mkdirTree(dirname(file.path));
|
|
180
|
-
FS.writeFile(file.path, file.data);
|
|
181
|
-
before.set(file.path, file.data);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
sink = output;
|
|
185
|
-
let exitCode;
|
|
186
|
-
const argv = pyodide.toPy(message.argv);
|
|
187
|
-
try {
|
|
188
|
-
exitCode = runPython(message.mode, message.target, argv, message.cwd, message.stdin);
|
|
189
|
-
} finally {
|
|
190
|
-
argv.destroy();
|
|
191
|
-
sink = null;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const after = new Map();
|
|
195
|
-
const afterDirs = new Set();
|
|
196
|
-
for (const root of ROOTS) if (lstat(root)) walk(root, after, afterDirs);
|
|
197
|
-
|
|
198
|
-
const written = [];
|
|
199
|
-
for (const [path, data] of after) {
|
|
200
|
-
const old = before.get(path);
|
|
201
|
-
if (!old || !sameBytes(old, data)) written.push({ path, data });
|
|
202
|
-
}
|
|
203
|
-
return {
|
|
204
|
-
exitCode,
|
|
205
|
-
stdout: output.stdout.finish(),
|
|
206
|
-
stderr: output.stderr.finish(),
|
|
207
|
-
written,
|
|
208
|
-
removed: [...before.keys()].filter((path) => !after.has(path)),
|
|
209
|
-
createdDirs: [...afterDirs].filter((dir) => !beforeDirs.has(dir)),
|
|
210
|
-
removedDirs: [...beforeDirs].filter((dir) => !ROOTS.includes(dir) && !afterDirs.has(dir)),
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
self.onmessage = (event) => {
|
|
215
|
-
const message = event.data;
|
|
216
|
-
if (message.type === 'init') {
|
|
217
|
-
init(message).then(
|
|
218
|
-
() => self.postMessage({ type: 'ready' }),
|
|
219
|
-
(error) => self.postMessage({ type: 'failed', message: describe(error) })
|
|
220
|
-
);
|
|
221
|
-
} else if (message.type === 'run') {
|
|
222
|
-
execute(message).then(
|
|
223
|
-
(result) => self.postMessage({ type: 'result', id: message.id, ...result }),
|
|
224
|
-
(error) =>
|
|
225
|
-
self.postMessage({
|
|
226
|
-
type: 'result',
|
|
227
|
-
id: message.id,
|
|
228
|
-
exitCode: 1,
|
|
229
|
-
stdout: '',
|
|
230
|
-
stderr: \`Python 运行环境出错:\${describe(error)}\\n\`,
|
|
231
|
-
written: [],
|
|
232
|
-
removed: [],
|
|
233
|
-
createdDirs: [],
|
|
234
|
-
removedDirs: [],
|
|
235
|
-
})
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
`;
|
|
240
|
-
export {
|
|
241
|
-
e as default
|
|
242
|
-
};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
var d = Object.defineProperty, l = Object.defineProperties;
|
|
2
|
-
var N = Object.getOwnPropertyDescriptors;
|
|
3
|
-
var s = Object.getOwnPropertySymbols;
|
|
4
|
-
var a = Object.prototype.hasOwnProperty, f = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var A = (e, n, t) => n in e ? d(e, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[n] = t, T = (e, n) => {
|
|
6
|
-
for (var t in n || (n = {}))
|
|
7
|
-
a.call(n, t) && A(e, t, n[t]);
|
|
8
|
-
if (s)
|
|
9
|
-
for (var t of s(n))
|
|
10
|
-
f.call(n, t) && A(e, t, n[t]);
|
|
11
|
-
return e;
|
|
12
|
-
}, _ = (e, n) => l(e, N(n));
|
|
13
|
-
var O = (e, n, t) => new Promise((u, o) => {
|
|
14
|
-
var U = (r) => {
|
|
15
|
-
try {
|
|
16
|
-
c(t.next(r));
|
|
17
|
-
} catch (i) {
|
|
18
|
-
o(i);
|
|
19
|
-
}
|
|
20
|
-
}, C = (r) => {
|
|
21
|
-
try {
|
|
22
|
-
c(t.throw(r));
|
|
23
|
-
} catch (i) {
|
|
24
|
-
o(i);
|
|
25
|
-
}
|
|
26
|
-
}, c = (r) => r.done ? u(r.value) : Promise.resolve(r.value).then(U, C);
|
|
27
|
-
c((t = t.apply(e, n)).next());
|
|
28
|
-
});
|
|
29
|
-
const R = 16e3, D = `
|
|
30
|
-
…输出已截断`;
|
|
31
|
-
function M(e) {
|
|
32
|
-
if (e.length <= 16e3)
|
|
33
|
-
return e;
|
|
34
|
-
let n = 16e3;
|
|
35
|
-
const t = e.charCodeAt(n - 1);
|
|
36
|
-
return t >= 55296 && t <= 56319 && (n -= 1), e.slice(0, n) + D;
|
|
37
|
-
}
|
|
38
|
-
function h(e) {
|
|
39
|
-
return {
|
|
40
|
-
exec(t) {
|
|
41
|
-
return O(this, null, function* () {
|
|
42
|
-
const u = yield e.exec(t);
|
|
43
|
-
return _(T({}, u), {
|
|
44
|
-
stdout: M(u.stdout),
|
|
45
|
-
stderr: M(u.stderr)
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
export {
|
|
52
|
-
R as MAX_COMMAND_OUTPUT_CHARS,
|
|
53
|
-
D as OUTPUT_TRUNCATED_NOTE,
|
|
54
|
-
M as limitOutput,
|
|
55
|
-
h as withOutputLimit
|
|
56
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const _ = ["shell", "read_file", "write_file", "append_file", "run_file", "edit_file", "grep", "find", "load_skill", "read_skill_resource", "run_skill_script"], e = 81920, s = 81920, l = 2e4, i = ".", n = "/workspace", o = [".py", ".sh", ".js"];
|
|
2
|
-
export {
|
|
3
|
-
_ as BUILTIN_FUNCTION_CODES,
|
|
4
|
-
l as MAX_COMMAND_CHARS,
|
|
5
|
-
e as MAX_READ_BYTES,
|
|
6
|
-
s as MAX_WRITE_BYTES,
|
|
7
|
-
o as RUNNABLE_EXTENSIONS,
|
|
8
|
-
i as SHELL_CWD,
|
|
9
|
-
n as WORKSPACE_MOUNT
|
|
10
|
-
};
|
|
@@ -1,261 +0,0 @@
|
|
|
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
|
-
for (var o in t || (t = {}))
|
|
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
|
-
return e;
|
|
12
|
-
}, _ = (e, t) => X(e, Q(t));
|
|
13
|
-
var U = (e, t, o) => new Promise((a, m) => {
|
|
14
|
-
var d = (n) => {
|
|
15
|
-
try {
|
|
16
|
-
c(o.next(n));
|
|
17
|
-
} catch (r) {
|
|
18
|
-
m(r);
|
|
19
|
-
}
|
|
20
|
-
}, f = (n) => {
|
|
21
|
-
try {
|
|
22
|
-
c(o.throw(n));
|
|
23
|
-
} catch (r) {
|
|
24
|
-
m(r);
|
|
25
|
-
}
|
|
26
|
-
}, c = (n) => n.done ? a(n.value) : Promise.resolve(n.value).then(d, f);
|
|
27
|
-
c((o = o.apply(e, t)).next());
|
|
28
|
-
});
|
|
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
|
-
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 L } 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 xe } from "./tools.mjs";
|
|
41
|
-
const Te = {
|
|
42
|
-
shell: de,
|
|
43
|
-
read_file: ce,
|
|
44
|
-
write_file: me,
|
|
45
|
-
append_file: ue,
|
|
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: xe
|
|
53
|
-
}, x = new ee("[BuiltinTools] "), j = /* @__PURE__ */ new Set(["shell", "run_file", "run_skill_script"]);
|
|
54
|
-
function we(e, t, o) {
|
|
55
|
-
var a, m;
|
|
56
|
-
if (e.commandRunner)
|
|
57
|
-
return L(e.commandRunner);
|
|
58
|
-
if (t instanceof H) {
|
|
59
|
-
if (t.protocolMismatch) {
|
|
60
|
-
const d = `${(a = t.sandboxVersion) != null ? a : ""}@${(m = t.sandboxProtocol) != null ? m : ""}`;
|
|
61
|
-
o.rejectedSandbox !== d && (o.rejectedSandbox = d, x.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(V("NotAllowedError", d))
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
exec: (d) => t.exec(d)
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
if (e.browserRuntime.enabled)
|
|
78
|
-
return (o.adapter !== t || !o.runner) && (o.adapter = t, o.runner = L(new ie({
|
|
79
|
-
adapter: t,
|
|
80
|
-
sensitive: e.sensitive,
|
|
81
|
-
moduleURL: e.browserRuntime.bashModuleURL,
|
|
82
|
-
pyodideIndexURL: e.browserRuntime.pyodideIndexURL
|
|
83
|
-
}))), o.runner;
|
|
84
|
-
}
|
|
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) {
|
|
101
|
-
if (typeof e != "string")
|
|
102
|
-
return e;
|
|
103
|
-
try {
|
|
104
|
-
return JSON.parse(e);
|
|
105
|
-
} catch (t) {
|
|
106
|
-
throw new u(l.BadRequest, "参数不是合法的 JSON 对象。");
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
function Oe(e) {
|
|
110
|
-
const t = e instanceof DOMException ? e.name : "", o = e instanceof Error ? e.message : "";
|
|
111
|
-
if (t === "NotFoundError")
|
|
112
|
-
return new u(l.NotFound, "脚本或目录不存在。");
|
|
113
|
-
if (t === "SecurityError")
|
|
114
|
-
return new u(l.Forbidden, "命令路径不在工作区内。");
|
|
115
|
-
if (t === "AbortError")
|
|
116
|
-
return o.includes("超时") ? new u(l.Timeout, "命令运行超时,已中止。") : o.includes("已有") ? new u(l.TooManyRequests, "已有一个命令在运行。") : T();
|
|
117
|
-
if (t === "NotAllowedError")
|
|
118
|
-
return o.startsWith("沙箱") ? new u(l.NotImplemented, o) : new u(l.NotImplemented, "当前环境不能执行命令或脚本,需要端侧宿主。");
|
|
119
|
-
}
|
|
120
|
-
function Re(e) {
|
|
121
|
-
return new Promise((t, o) => {
|
|
122
|
-
e.aborted && o(T()), e.addEventListener("abort", () => o(T()), {
|
|
123
|
-
once: !0
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
function $(e, t = 160) {
|
|
128
|
-
return e.length > t ? `${e.slice(0, t)}…` : e;
|
|
129
|
-
}
|
|
130
|
-
function pe(e) {
|
|
131
|
-
const t = {};
|
|
132
|
-
for (const o of ["path", "skill_name", "resource_name", "script_name", "pattern", "name"])
|
|
133
|
-
typeof e[o] == "string" && (t[o] = e[o]);
|
|
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;
|
|
135
|
-
}
|
|
136
|
-
function Le(e) {
|
|
137
|
-
return s({
|
|
138
|
-
code: e.code,
|
|
139
|
-
message: e.message
|
|
140
|
-
}, e.data ? {
|
|
141
|
-
data: e.data
|
|
142
|
-
} : {});
|
|
143
|
-
}
|
|
144
|
-
function Ve({
|
|
145
|
-
pluginCode: e,
|
|
146
|
-
workspace: t
|
|
147
|
-
}) {
|
|
148
|
-
const o = /* @__PURE__ */ new Map(), a = {}, m = {};
|
|
149
|
-
return se.forEach((d) => {
|
|
150
|
-
m[d] = (f, c, n) => {
|
|
151
|
-
let r = !1;
|
|
152
|
-
Se(t, o, a, d, f, (p) => {
|
|
153
|
-
if (!r) {
|
|
154
|
-
r = !0;
|
|
155
|
-
try {
|
|
156
|
-
c(p);
|
|
157
|
-
} catch (I) {
|
|
158
|
-
console.error("[BuiltinTools] callback failed", I);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}, _(s({}, n), {
|
|
162
|
-
pluginCode: e
|
|
163
|
-
}));
|
|
164
|
-
};
|
|
165
|
-
}), {
|
|
166
|
-
[e]: m
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
function Se(e, t, o, a, m, d, f) {
|
|
170
|
-
return U(this, null, function* () {
|
|
171
|
-
var N, M, F, A, v;
|
|
172
|
-
const c = {
|
|
173
|
-
requestId: f.requestId || k(),
|
|
174
|
-
applicationId: f.applicationId || e.applicationId,
|
|
175
|
-
sessionId: f.sessionId || "",
|
|
176
|
-
pluginCode: f.pluginCode,
|
|
177
|
-
functionCode: a,
|
|
178
|
-
signal: f.signal
|
|
179
|
-
}, n = {
|
|
180
|
-
requestId: c.requestId,
|
|
181
|
-
sessionId: c.sessionId,
|
|
182
|
-
functionCode: a
|
|
183
|
-
}, r = new AbortController();
|
|
184
|
-
let w = !1;
|
|
185
|
-
const p = setTimeout(() => {
|
|
186
|
-
w = !0, r.abort();
|
|
187
|
-
}, e.limits.toolTimeoutMs), I = () => r.abort();
|
|
188
|
-
(N = c.signal) == null || N.addEventListener("abort", I, {
|
|
189
|
-
once: !0
|
|
190
|
-
}), (M = c.signal) != null && M.aborted && r.abort();
|
|
191
|
-
let b = null, y = null, E;
|
|
192
|
-
const S = Date.now();
|
|
193
|
-
try {
|
|
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)
|
|
196
|
-
throw T();
|
|
197
|
-
const g = e.adapter;
|
|
198
|
-
if (!g)
|
|
199
|
-
throw oe();
|
|
200
|
-
if (b = e.acquireSlot(), !b)
|
|
201
|
-
throw new u(l.TooManyRequests, "同时进行的文件操作过多,请稍后重试。");
|
|
202
|
-
if (j.has(a) && (y = e.acquireCommand(), !y))
|
|
203
|
-
throw new u(l.TooManyRequests, "已有一个命令在运行,请等它结束后再试。");
|
|
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 = {
|
|
208
|
-
cwd: le
|
|
209
|
-
}, t.set(q, h));
|
|
210
|
-
const J = we(e, g, o), W = h.cwd, z = {
|
|
211
|
-
adapter: g,
|
|
212
|
-
runner: J,
|
|
213
|
-
signal: r.signal,
|
|
214
|
-
timeoutMs: e.limits.toolTimeoutMs,
|
|
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 = {
|
|
219
|
-
code: l.Ok,
|
|
220
|
-
message: "ok",
|
|
221
|
-
data: R.data
|
|
222
|
-
};
|
|
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,
|
|
227
|
-
code: l.Ok
|
|
228
|
-
}), R.truncated ? {
|
|
229
|
-
truncated: !0
|
|
230
|
-
} : {}), typeof P == "number" ? {
|
|
231
|
-
exitCode: P
|
|
232
|
-
} : {}), a === "shell" ? {
|
|
233
|
-
cwd: h.cwd,
|
|
234
|
-
cwdChanged: h.cwd !== W
|
|
235
|
-
} : {})), e.emit("tool:finish", _(s({}, n), {
|
|
236
|
-
code: l.Ok
|
|
237
|
-
})), j.has(a) && e.notifyFilesChanged(), d(B);
|
|
238
|
-
} catch (O) {
|
|
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,
|
|
247
|
-
code: i.code,
|
|
248
|
-
message: $(i.message, 300)
|
|
249
|
-
})), e.emit("tool:error", _(s({}, n), {
|
|
250
|
-
code: i.code,
|
|
251
|
-
message: i.message,
|
|
252
|
-
reason: (A = i.data) == null ? void 0 : A.reason
|
|
253
|
-
}))), d(Le(i));
|
|
254
|
-
} finally {
|
|
255
|
-
clearTimeout(p), (v = c.signal) == null || v.removeEventListener("abort", I), b == null || b(), y == null || y(), E == null || E();
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
export {
|
|
260
|
-
Ve as createBuiltinToolsPlugin
|
|
261
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
function c(r) {
|
|
2
|
-
return `预览:
|
|
3
|
-
${r.length > 500 ? r.slice(0, 500) : r}`;
|
|
4
|
-
}
|
|
5
|
-
function O(r) {
|
|
6
|
-
let n;
|
|
7
|
-
try {
|
|
8
|
-
n = JSON.parse(r);
|
|
9
|
-
} catch (e) {
|
|
10
|
-
return `JSON 无法完整解析。
|
|
11
|
-
${c(r)}`;
|
|
12
|
-
}
|
|
13
|
-
if (Array.isArray(n)) {
|
|
14
|
-
const e = n[0], t = e && typeof e == "object" && !Array.isArray(e) ? `元素字段:${Object.keys(e).slice(0, 20).join(", ") || "(无)"}。` : `元素类型:${e === null ? "null" : typeof e}。`;
|
|
15
|
-
return `JSON 数组,长度 ${n.length}。${t}
|
|
16
|
-
${c(r)}`;
|
|
17
|
-
}
|
|
18
|
-
if (n && typeof n == "object") {
|
|
19
|
-
const e = n, t = Object.keys(e), s = t.filter((i) => Array.isArray(e[i])).slice(0, 10).map((i) => `${i} (${e[i].length})`), o = s.length ? `数组字段:${s.join("、")}。` : "";
|
|
20
|
-
return `JSON 对象。字段:${t.slice(0, 40).join(", ") || "(无)"}。${o}
|
|
21
|
-
${c(r)}`;
|
|
22
|
-
}
|
|
23
|
-
return `JSON ${n === null ? "null" : typeof n}。
|
|
24
|
-
${c(r)}`;
|
|
25
|
-
}
|
|
26
|
-
function h(r, n) {
|
|
27
|
-
var a, p;
|
|
28
|
-
const e = r.match(/<([A-Za-z_][\w:.-]*)\b/), t = (a = e == null ? void 0 : e[1]) != null ? a : "未知", s = /* @__PURE__ */ new Map(), o = new RegExp("<([A-Za-z_][\\w:.-]*)\\b", "g");
|
|
29
|
-
let i, u = !1;
|
|
30
|
-
for (; i = o.exec(r); ) {
|
|
31
|
-
const l = i[1];
|
|
32
|
-
if (!u && l === t) {
|
|
33
|
-
u = !0;
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
if (s.set(l, ((p = s.get(l)) != null ? p : 0) + 1), s.size > 30)
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
const f = Array.from(s.entries()).slice(0, 20).map(([l, v]) => `${l} (${v})`).join("、");
|
|
40
|
-
return `${n} 根元素 ${t}。直接或嵌套子元素:${f || "(无)"}。
|
|
41
|
-
${c(r)}`;
|
|
42
|
-
}
|
|
43
|
-
function g(r, n, e) {
|
|
44
|
-
var f;
|
|
45
|
-
const t = r.split(/\r?\n/).filter((a) => a.length > 0), s = (f = t[0]) != null ? f : "", o = s ? s.split(n).length : 0, i = Math.max(t.length - 1, 0), u = t.slice(0, 4).join(`
|
|
46
|
-
`);
|
|
47
|
-
return `${e},${o} 列,数据行 ${i}。表头:${s}
|
|
48
|
-
预览:
|
|
49
|
-
${u}`;
|
|
50
|
-
}
|
|
51
|
-
function j(r) {
|
|
52
|
-
const n = $(r);
|
|
53
|
-
return n === ".json" || n === ".xml" || n === ".svg" || n === ".csv" || n === ".tsv";
|
|
54
|
-
}
|
|
55
|
-
function m(r, n) {
|
|
56
|
-
const e = $(r);
|
|
57
|
-
return e === ".json" ? O(n) : e === ".svg" ? h(n, "SVG") : e === ".xml" ? h(n, "XML") : e === ".tsv" ? g(n, " ", "TSV") : e === ".csv" ? g(n, ",", "CSV") : c(n);
|
|
58
|
-
}
|
|
59
|
-
function $(r) {
|
|
60
|
-
var t;
|
|
61
|
-
const n = (t = r.split("/").pop()) != null ? t : "", e = n.lastIndexOf(".");
|
|
62
|
-
return e >= 0 ? n.slice(e).toLowerCase() : "";
|
|
63
|
-
}
|
|
64
|
-
const S = /* @__PURE__ */ new Set([".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".ico", ".tif", ".tiff"]);
|
|
65
|
-
function w(r) {
|
|
66
|
-
return S.has($(r));
|
|
67
|
-
}
|
|
68
|
-
function A(r, n) {
|
|
69
|
-
return $(r) === ".pdf" ? !0 : !n || n.byteLength < 4 ? !1 : n[0] === 37 && n[1] === 80 && n[2] === 68 && n[3] === 70;
|
|
70
|
-
}
|
|
71
|
-
export {
|
|
72
|
-
$ as extensionOf,
|
|
73
|
-
w as isImagePath,
|
|
74
|
-
A as isPdf,
|
|
75
|
-
j as isStructuredPath,
|
|
76
|
-
m as structureOverview
|
|
77
|
-
};
|