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