@glodon-aiot/bot-client-sdk 3.26.0-alpha.23 → 3.26.0-alpha.24

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.
Files changed (83) hide show
  1. package/README.md +180 -130
  2. package/dist/es/AgentSession.mjs +148 -129
  3. package/dist/es/BotClient.mjs +154 -136
  4. package/dist/es/Config.mjs +56 -89
  5. package/dist/es/Session.mjs +35 -23
  6. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  7. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  8. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  9. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  10. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  11. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  12. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  13. package/dist/es/builtinTools/command.mjs +56 -0
  14. package/dist/es/builtinTools/constants.mjs +10 -0
  15. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +262 -0
  16. package/dist/es/builtinTools/overview.mjs +77 -0
  17. package/dist/es/builtinTools/paths.mjs +69 -0
  18. package/dist/es/builtinTools/platformSkills.mjs +210 -0
  19. package/dist/es/builtinTools/schemas.mjs +216 -0
  20. package/dist/es/builtinTools/tools.mjs +494 -0
  21. package/dist/es/index.mjs +66 -64
  22. package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
  23. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +185 -113
  24. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +127 -137
  25. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +132 -97
  26. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +32 -28
  27. package/dist/es/localFiles/constants.mjs +24 -29
  28. package/dist/es/localFiles/errors.mjs +21 -19
  29. package/dist/es/localFiles/host/connectHostBridge.mjs +37 -34
  30. package/dist/es/localFiles/host/protocol.mjs +27 -13
  31. package/dist/es/localFiles/host/sandboxClient.mjs +476 -0
  32. package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
  33. package/dist/es/localFiles/preview.mjs +33 -0
  34. package/dist/es/localFiles/schemas.mjs +40 -86
  35. package/dist/es/localFiles/validate.mjs +37 -37
  36. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +167 -86
  37. package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
  38. package/dist/es/localMcp/client.mjs +223 -0
  39. package/dist/es/localMcp/register.mjs +155 -0
  40. package/dist/es/localMcp/registry.mjs +39 -0
  41. package/dist/es/package.json.mjs +38 -29
  42. package/dist/es/sessionUpload.mjs +49 -0
  43. package/dist/lib/index.js +379 -10
  44. package/dist/src/AgentSession.d.ts +2 -0
  45. package/dist/src/Config.d.ts +5 -7
  46. package/dist/src/Session.d.ts +6 -0
  47. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  48. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  49. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  50. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  51. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  52. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  53. package/dist/src/builtinTools/command.d.ts +35 -0
  54. package/dist/src/builtinTools/constants.d.ts +17 -0
  55. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  56. package/dist/src/builtinTools/overview.d.ts +5 -0
  57. package/dist/src/builtinTools/paths.d.ts +16 -0
  58. package/dist/src/builtinTools/platformSkills.d.ts +67 -0
  59. package/dist/src/builtinTools/schemas.d.ts +3 -0
  60. package/dist/src/builtinTools/tools.d.ts +64 -0
  61. package/dist/src/index.d.ts +2 -1
  62. package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
  63. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +4 -1
  64. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +30 -14
  65. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +4 -1
  66. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +22 -0
  67. package/dist/src/localFiles/adapters/fsaTypes.d.ts +2 -0
  68. package/dist/src/localFiles/constants.d.ts +3 -13
  69. package/dist/src/localFiles/host/protocol.d.ts +34 -16
  70. package/dist/src/localFiles/host/sandboxClient.d.ts +104 -0
  71. package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
  72. package/dist/src/localFiles/index.d.ts +11 -6
  73. package/dist/src/localFiles/preview.d.ts +2 -0
  74. package/dist/src/localFiles/schemas.d.ts +2 -5
  75. package/dist/src/localFiles/types.d.ts +16 -23
  76. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +35 -1
  77. package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
  78. package/dist/src/localMcp/client.d.ts +11 -0
  79. package/dist/src/localMcp/register.d.ts +32 -0
  80. package/dist/src/localMcp/registry.d.ts +26 -0
  81. package/dist/src/sessionUpload.d.ts +14 -0
  82. package/package.json +13 -4
  83. package/dist/es/localFiles/createLocalPythonPlugin.mjs +0 -196
@@ -1,43 +1,46 @@
1
- var _ = Object.defineProperty, k = Object.defineProperties;
2
- var S = Object.getOwnPropertyDescriptors;
3
- var w = Object.getOwnPropertySymbols;
4
- var C = Object.prototype.hasOwnProperty, P = Object.prototype.propertyIsEnumerable;
5
- var u = (r, e, t) => e in r ? _(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, f = (r, e) => {
1
+ var P = Object.defineProperty, S = Object.defineProperties;
2
+ var b = Object.getOwnPropertyDescriptors;
3
+ var k = Object.getOwnPropertySymbols;
4
+ var H = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
5
+ var p = (r, e, t) => e in r ? P(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, u = (r, e) => {
6
6
  for (var t in e || (e = {}))
7
- C.call(e, t) && u(r, t, e[t]);
8
- if (w)
9
- for (var t of w(e))
10
- P.call(e, t) && u(r, t, e[t]);
7
+ H.call(e, t) && p(r, t, e[t]);
8
+ if (k)
9
+ for (var t of k(e))
10
+ R.call(e, t) && p(r, t, e[t]);
11
11
  return r;
12
- }, A = (r, e) => k(r, S(e));
13
- var s = (r, e, t) => (u(r, typeof e != "symbol" ? e + "" : e, t), t);
14
- var o = (r, e, t) => new Promise((i, a) => {
15
- var n = (l) => {
12
+ }, C = (r, e) => S(r, b(e));
13
+ var s = (r, e, t) => (p(r, typeof e != "symbol" ? e + "" : e, t), t);
14
+ var d = (r, e, t) => new Promise((i, n) => {
15
+ var a = (o) => {
16
16
  try {
17
- c(t.next(l));
18
- } catch (d) {
19
- a(d);
17
+ l(t.next(o));
18
+ } catch (c) {
19
+ n(c);
20
20
  }
21
- }, h = (l) => {
21
+ }, h = (o) => {
22
22
  try {
23
- c(t.throw(l));
24
- } catch (d) {
25
- a(d);
23
+ l(t.throw(o));
24
+ } catch (c) {
25
+ n(c);
26
26
  }
27
- }, c = (l) => l.done ? i(l.value) : Promise.resolve(l.value).then(n, h);
28
- c((t = t.apply(r, e)).next());
27
+ }, l = (o) => o.done ? i(o.value) : Promise.resolve(o.value).then(a, h);
28
+ l((t = t.apply(r, e)).next());
29
29
  });
30
- import H from "../../EventBus.mjs";
31
- import { FileSystemAccessAdapter as b } from "../adapters/FileSystemAccessAdapter.mjs";
32
- import { getNativeDirectoryPicker as D } from "../adapters/fsaTypes.mjs";
33
- import { LOCAL_FILES_FUNCTION_CODES as T } from "../constants.mjs";
34
- import { getDownloadUrlAllowlist as F } from "../security/downloadUrlPolicy.mjs";
35
- import { SensitiveFilePolicy as W } from "../security/sensitiveFiles.mjs";
36
- import { resolveLimits as q } from "../limits.mjs";
37
- import { createDefaultSessionFileTransfer as N } from "../transfer/SessionFileTransfer.mjs";
38
- import { createMemoryStore as I } from "./handleStore.mjs";
39
- import { watchDirectoryHandle as E } from "./watchDirectory.mjs";
40
- const p = {
30
+ import T from "../../EventBus.mjs";
31
+ import { FileSystemAccessAdapter as _ } from "../adapters/FileSystemAccessAdapter.mjs";
32
+ import { HostBridgeAdapter as F } from "../adapters/HostBridgeAdapter.mjs";
33
+ import { FileSnapshotAdapter as D } from "../adapters/FileSnapshotAdapter.mjs";
34
+ import { getNativeDirectoryPicker as W } from "../adapters/fsaTypes.mjs";
35
+ import { LOCAL_FILES_FUNCTION_CODES as L } from "../constants.mjs";
36
+ import { getDownloadUrlAllowlist as N } from "../security/downloadUrlPolicy.mjs";
37
+ import { SensitiveFilePolicy as E } from "../security/sensitiveFiles.mjs";
38
+ import { resolveLimits as I } from "../limits.mjs";
39
+ import { createDefaultSessionFileTransfer as q } from "../transfer/SessionFileTransfer.mjs";
40
+ import { createMemoryStore as U } from "./handleStore.mjs";
41
+ import { watchDirectoryHandle as j } from "./watchDirectory.mjs";
42
+ import { WorkspaceFiles as x } from "./WorkspaceFiles.mjs";
43
+ const m = {
41
44
  mode: "readwrite"
42
45
  };
43
46
  function K(r) {
@@ -50,10 +53,10 @@ function K(r) {
50
53
  if (!t.some((i) => !i || i === "." || i === ".."))
51
54
  return t.join("/");
52
55
  }
53
- function L(r) {
56
+ function V(r) {
54
57
  return !!r && typeof r == "object" && r.name === "AbortError";
55
58
  }
56
- class J {
59
+ class se {
57
60
  constructor(e) {
58
61
  s(this, "applicationId");
59
62
  s(this, "limits");
@@ -62,15 +65,22 @@ class J {
62
65
  s(this, "allowWorkspaceWideTextSearch");
63
66
  s(this, "transfer");
64
67
  s(this, "fetch");
68
+ s(this, "commandRunner");
69
+ /** 平台技能同步。由 BotClient 在启用本地工作区后挂上。 */
70
+ s(this, "platformSkills");
71
+ s(this, "browserRuntime");
72
+ /** 接入方的文件操作入口。不要使用 `adapter`。 */
73
+ s(this, "files");
65
74
  s(this, "store");
66
75
  s(this, "picker");
67
- s(this, "event", new H(!0));
76
+ s(this, "event", new T(!0));
68
77
  s(this, "_status");
69
78
  s(this, "_displayName");
70
79
  s(this, "_adapter", null);
71
80
  s(this, "pendingHandle", null);
72
81
  s(this, "approvalHandler");
73
82
  s(this, "activeCalls", 0);
83
+ s(this, "activeCommands", 0);
74
84
  s(this, "inflight", /* @__PURE__ */ new Set());
75
85
  s(this, "_ready", Promise.resolve());
76
86
  s(this, "stopWatching", () => {
@@ -78,17 +88,25 @@ class J {
78
88
  s(this, "pendingChangePaths", /* @__PURE__ */ new Set());
79
89
  s(this, "changeTimer");
80
90
  s(this, "refreshAllVisible", !1);
81
- var n, h, c, l, d, m, g, y, v;
82
- const t = (n = e.config) != null ? n : {};
83
- this.applicationId = e.applicationId, this.store = (h = e.store) != null ? h : I(), this.picker = e.picker === null ? void 0 : (c = e.picker) != null ? c : D(), this.limits = q(t.limits), this.sensitive = new W(t.sensitiveRulesAppend), this.downloadUrlAllowlist = F(t.downloadUrlAllowlistAppend), this.allowWorkspaceWideTextSearch = !!t.allowWorkspaceWideTextSearch, this.approvalHandler = t.onApprovalRequired;
84
- const i = (l = e.transfer) != null ? l : N(() => {
91
+ /** 选择、关闭会推进它。进行中的 restore 若发现已经变了,就不再覆盖当前目录。 */
92
+ s(this, "epoch", 0);
93
+ var h, l, o, c, f, g, v, y, w, A;
94
+ const t = (h = e.config) != null ? h : {};
95
+ this.applicationId = e.applicationId, this.store = (l = e.store) != null ? l : U(), this.picker = e.picker === null ? void 0 : (o = e.picker) != null ? o : W(), this.limits = I(t.limits), this.sensitive = new E(t.sensitiveRulesAppend), this.downloadUrlAllowlist = N(t.downloadUrlAllowlistAppend), this.allowWorkspaceWideTextSearch = !!t.allowWorkspaceWideTextSearch, this.approvalHandler = t.onApprovalRequired;
96
+ const i = (c = e.transfer) != null ? c : q(() => {
85
97
  });
86
98
  this.transfer = {
87
- upload: (m = (d = t.transfer) == null ? void 0 : d.upload) != null ? m : i.upload,
88
- resolveDownload: (y = (g = t.transfer) == null ? void 0 : g.resolveDownload) != null ? y : i.resolveDownload
99
+ upload: (g = (f = t.transfer) == null ? void 0 : f.upload) != null ? g : i.upload,
100
+ resolveDownload: (y = (v = t.transfer) == null ? void 0 : v.resolveDownload) != null ? y : i.resolveDownload
89
101
  };
90
- const a = (v = e.fetch) != null ? v : typeof fetch == "function" ? fetch : void 0;
91
- this.fetch = a ? a.bind(globalThis) : () => Promise.reject(new Error("fetch is not available")), this._status = this.picker ? "none" : "unsupported";
102
+ const n = (w = e.fetch) != null ? w : typeof fetch == "function" ? fetch : void 0;
103
+ this.fetch = n ? n.bind(globalThis) : () => Promise.reject(new Error("fetch is not available")), this.commandRunner = e.commandRunner;
104
+ const a = (A = t.commandRuntime) == null ? void 0 : A.browser;
105
+ this.browserRuntime = {
106
+ enabled: (a == null ? void 0 : a.enabled) !== !1,
107
+ bashModuleURL: a == null ? void 0 : a.bashModuleURL,
108
+ pyodideIndexURL: a == null ? void 0 : a.pyodideIndexURL
109
+ }, this._status = this.picker ? "none" : "unsupported", this.files = new x(this);
92
110
  }
93
111
  // -------------------------------------------------------------------------
94
112
  // 状态
@@ -107,7 +125,10 @@ class J {
107
125
  get ready() {
108
126
  return this._ready;
109
127
  }
110
- /** @internal 已授权时可用的 Adapter。 */
128
+ /**
129
+ * @internal 已授权时的磁盘后端,供内置工具使用。
130
+ * 接入方用 `files`:这里的路径必须已规范化,异常里也可能带系统路径。
131
+ */
111
132
  get adapter() {
112
133
  return this._status === "granted" ? this._adapter : null;
113
134
  }
@@ -123,8 +144,11 @@ class J {
123
144
  displayName: t
124
145
  }));
125
146
  }
147
+ replaceAdapter(e) {
148
+ this._adapter !== e && this.files.releaseAllUrls(), this._adapter = e;
149
+ }
126
150
  attachHandle(e) {
127
- this.cancelFilesChanged(), this.pendingHandle = e, this._adapter = new b(e), this.setStatus("granted", e.name), this.unwatchDirectory(), this.stopWatching = E(e, (t) => this.notifyFilesChanged(t));
151
+ this.cancelFilesChanged(), this.pendingHandle = e, this.replaceAdapter(new _(e)), this.setStatus("granted", e.name), this.unwatchDirectory(), this.stopWatching = j(e, (t) => this.notifyFilesChanged(t));
128
152
  }
129
153
  // -------------------------------------------------------------------------
130
154
  // 授权
@@ -144,18 +168,13 @@ class J {
144
168
  } catch (t) {
145
169
  return Promise.reject(t);
146
170
  }
147
- return e.then((t) => o(this, null, function* () {
148
- var a, n;
149
- this.cancelAll();
150
- const i = (n = yield (a = t.queryPermission) == null ? void 0 : a.call(t, p)) != null ? n : "granted";
151
- return yield this.store.set(this.handleKey, {
152
- handle: t,
153
- displayName: t.name,
154
- savedAt: Date.now()
155
- }).catch(() => {
156
- }), i === "denied" ? (this.pendingHandle = t, this._adapter = null, this.setStatus("denied", t.name), !1) : (this.attachHandle(t), !0);
157
- }), (t) => {
158
- if (L(t))
171
+ return e.then((t) => (this.epoch += 1, this.cancelAll(), this.attachHandle(t), this.store.set(this.handleKey, {
172
+ handle: t,
173
+ displayName: t.name,
174
+ savedAt: Date.now()
175
+ }).catch(() => {
176
+ }), !0), (t) => {
177
+ if (V(t))
159
178
  return !1;
160
179
  throw t;
161
180
  });
@@ -165,29 +184,51 @@ class J {
165
184
  return this._ready = this.doRestore(), this._ready;
166
185
  }
167
186
  doRestore() {
168
- return o(this, null, function* () {
169
- var i, a, n;
187
+ return d(this, null, function* () {
188
+ var n, a, h;
189
+ const e = this.epoch;
170
190
  if (!this.picker) {
171
191
  this._adapter || this.setStatus("unsupported");
172
192
  return;
173
193
  }
174
- let e;
194
+ let t;
175
195
  try {
176
- e = yield this.store.get(this.handleKey);
177
- } catch (h) {
178
- e = void 0;
196
+ t = yield this.store.get(this.handleKey);
197
+ } catch (l) {
198
+ t = void 0;
179
199
  }
180
- if (!(e != null && e.handle)) {
181
- this.pendingHandle = null, this._adapter = null, this.setStatus("none", void 0);
200
+ if (e !== this.epoch)
201
+ return;
202
+ if (!(t != null && t.handle)) {
203
+ this.pendingHandle = null, this.replaceAdapter(null), this.setStatus("none", void 0);
182
204
  return;
183
205
  }
184
- let t;
206
+ let i;
185
207
  try {
186
- t = (n = yield (a = (i = e.handle).queryPermission) == null ? void 0 : a.call(i, p)) != null ? n : "prompt";
187
- } catch (h) {
188
- t = "prompt";
208
+ i = (h = yield (a = (n = t.handle).queryPermission) == null ? void 0 : a.call(n, m)) != null ? h : "prompt";
209
+ } catch (l) {
210
+ i = "prompt";
211
+ }
212
+ if (e === this.epoch) {
213
+ if (i === "granted" || i === "prompt" && (yield this.canList(t.handle))) {
214
+ if (e !== this.epoch)
215
+ return;
216
+ this.attachHandle(t.handle);
217
+ return;
218
+ }
219
+ this.pendingHandle = t.handle, this.replaceAdapter(null), this.setStatus(i === "denied" ? "denied" : "prompt", t.displayName);
189
220
  }
190
- t === "granted" ? this.attachHandle(e.handle) : (this.pendingHandle = e.handle, this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.displayName));
221
+ });
222
+ }
223
+ /** queryPermission 仍可能是 prompt,但选择框留下的句柄其实列得出目录。 */
224
+ canList(e) {
225
+ return d(this, null, function* () {
226
+ const t = new _(e);
227
+ let i;
228
+ const n = new Promise((l) => {
229
+ i = setTimeout(() => l(!1), 2e3);
230
+ }), a = t.listEntries("").then(() => !0).catch(() => !1), h = yield Promise.race([a, n]);
231
+ return clearTimeout(i), h;
191
232
  });
192
233
  }
193
234
  /** 对已保存的目录重新申请权限。必须在用户点击事件中调用。 */
@@ -197,22 +238,36 @@ class J {
197
238
  return Promise.resolve(!1);
198
239
  let t;
199
240
  try {
200
- t = e.requestPermission(p);
241
+ t = e.requestPermission(m);
201
242
  } catch (i) {
202
243
  return Promise.reject(i);
203
244
  }
204
- return t.then((i) => i === "granted" ? (this.attachHandle(e), !0) : (this._adapter = null, this.setStatus(i === "denied" ? "denied" : "prompt", e.name), !1));
245
+ return t.then((i) => i === "granted" ? (this.attachHandle(e), !0) : (this.replaceAdapter(null), this.setStatus(i === "denied" ? "denied" : "prompt", e.name), !1));
205
246
  }
206
247
  /** 断开工作区并删除持久化句柄。状态立即变为未授权,存储删除在其后完成。 */
207
248
  clear() {
208
- return o(this, null, function* () {
209
- this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this._adapter = null, this.setStatus(this.picker ? "none" : "unsupported", void 0), yield this.store.delete(this.handleKey).catch(() => {
249
+ return d(this, null, function* () {
250
+ this.epoch += 1, this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this.replaceAdapter(null), this.setStatus(this.picker ? "none" : "unsupported", void 0), yield this.store.delete(this.handleKey).catch(() => {
210
251
  });
211
252
  });
212
253
  }
254
+ /**
255
+ * 用文件列表挂上工作区。路径来自 webkitRelativePath,选完就能列出层级。
256
+ * 写入只留在内存里,不会出现在用户磁盘上。需要落盘时用 pick()。
257
+ */
258
+ attachFileSnapshot(e, t) {
259
+ var h, l, o;
260
+ const i = Array.from(e), n = t || ((l = (h = i[0]) == null ? void 0 : h.webkitRelativePath) == null ? void 0 : l.split(/[/\\]/)[0]) || ((o = i[0]) == null ? void 0 : o.name) || "工作文件夹", a = i.map((c) => ({
261
+ name: c.name,
262
+ webkitRelativePath: c.webkitRelativePath,
263
+ lastModified: c.lastModified,
264
+ blob: c
265
+ }));
266
+ this.attachAdapter(new D(a, n), n);
267
+ }
213
268
  /** 直接挂载一个 Adapter(测试或自定义后端),视为已授权。 */
214
269
  attachAdapter(e, t = e.displayName) {
215
- this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this._adapter = e, this.setStatus("granted", t);
270
+ this.epoch += 1, this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this.replaceAdapter(e), this.setStatus("granted", t);
216
271
  }
217
272
  /**
218
273
  * 通知目录树重读。`paths` 是相对工作区根的目录;省略时重读当前展开的全部目录。
@@ -232,19 +287,29 @@ class J {
232
287
  }
233
288
  /** @internal 工具执行中遇到权限错误时调用,重新检查并更新状态。 */
234
289
  handlePermissionLost() {
235
- return o(this, null, function* () {
290
+ return d(this, null, function* () {
236
291
  const e = this.pendingHandle;
237
292
  if (!(e != null && e.queryPermission))
238
293
  return;
239
- const t = yield e.queryPermission(p).catch(() => "prompt");
240
- t !== "granted" && (this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.name));
294
+ const t = yield e.queryPermission(m).catch(() => "prompt");
295
+ if (t !== "granted") {
296
+ if (t === "prompt" && this._adapter)
297
+ try {
298
+ yield this._adapter.listEntries("");
299
+ return;
300
+ } catch (i) {
301
+ if ((i && typeof i == "object" ? i.name : void 0) !== "NotAllowedError")
302
+ return;
303
+ }
304
+ this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.replaceAdapter(null), this.setStatus(t === "denied" ? "denied" : "prompt", e.name);
305
+ }
241
306
  });
242
307
  }
243
308
  // -------------------------------------------------------------------------
244
309
  // 外发提示
245
310
  // -------------------------------------------------------------------------
246
311
  isDisclosureAcknowledged() {
247
- return o(this, null, function* () {
312
+ return d(this, null, function* () {
248
313
  try {
249
314
  return !!(yield this.store.get(this.disclosureKey));
250
315
  } catch (e) {
@@ -253,7 +318,7 @@ class J {
253
318
  });
254
319
  }
255
320
  acknowledgeDisclosure() {
256
- return o(this, null, function* () {
321
+ return d(this, null, function* () {
257
322
  yield this.store.set(this.disclosureKey, !0).catch(() => {
258
323
  });
259
324
  });
@@ -281,6 +346,22 @@ class J {
281
346
  e || (e = !0, this.activeCalls--);
282
347
  };
283
348
  }
349
+ /**
350
+ * 占用一个浏览器运行时的命令名额。本地沙箱在时页面不设上限,并发由沙箱的 exec.maxConcurrent 决定。
351
+ * 浏览器运行时一次只放一条。
352
+ */
353
+ acquireCommand() {
354
+ if (this._adapter instanceof F)
355
+ return () => {
356
+ };
357
+ if (this.activeCommands >= 1)
358
+ return null;
359
+ this.activeCommands++;
360
+ let e = !1;
361
+ return () => {
362
+ e || (e = !0, this.activeCommands--);
363
+ };
364
+ }
284
365
  /** @internal 登记进行中的调用,更换或断开工作区时统一中止。 */
285
366
  track(e) {
286
367
  return this.inflight.add(e), () => this.inflight.delete(e);
@@ -291,14 +372,14 @@ class J {
291
372
  this.inflight.clear(), e.forEach((t) => t.abort());
292
373
  }
293
374
  handshake() {
294
- const e = Object.fromEntries(T.map((t) => [t, !0]));
375
+ const e = Object.fromEntries(L.map((t) => [t, !0]));
295
376
  return {
296
377
  protocolVersion: "1.0",
297
- capabilities: A(f({}, e), {
378
+ capabilities: C(u({}, e), {
298
379
  cancellation: !0,
299
380
  atomicWrite: !0
300
381
  }),
301
- limits: f({}, this.limits)
382
+ limits: u({}, this.limits)
302
383
  };
303
384
  }
304
385
  // -------------------------------------------------------------------------
@@ -320,7 +401,7 @@ class J {
320
401
  }
321
402
  flushFilesChanged() {
322
403
  this.changeTimer = void 0;
323
- const e = this.refreshAllVisible, t = [...this.pendingChangePaths].sort((i, a) => i < a ? -1 : i > a ? 1 : 0);
404
+ const e = this.refreshAllVisible, t = [...this.pendingChangePaths].sort((i, n) => i < n ? -1 : i > n ? 1 : 0);
324
405
  this.pendingChangePaths.clear(), this.refreshAllVisible = !1, this._status === "granted" && this.emit("files:changed", {
325
406
  paths: e ? [] : t
326
407
  });
@@ -335,5 +416,5 @@ class J {
335
416
  }
336
417
  }
337
418
  export {
338
- J as LocalWorkspace
419
+ se as LocalWorkspace
339
420
  };
@@ -0,0 +1,246 @@
1
+ var A = Object.defineProperty;
2
+ var B = (n, t, e) => t in n ? A(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var p = (n, t, e) => (B(n, typeof t != "symbol" ? t + "" : t, e), e);
4
+ var a = (n, t, e) => new Promise((s, r) => {
5
+ var i = (c) => {
6
+ try {
7
+ h(e.next(c));
8
+ } catch (w) {
9
+ r(w);
10
+ }
11
+ }, o = (c) => {
12
+ try {
13
+ h(e.throw(c));
14
+ } catch (w) {
15
+ r(w);
16
+ }
17
+ }, h = (c) => c.done ? s(c.value) : Promise.resolve(c.value).then(i, o);
18
+ h((e = e.apply(n, t)).next());
19
+ });
20
+ import { ToolError as l, ToolCode as f, toToolError as F, workspaceNotGranted as E } from "../errors.mjs";
21
+ import { mediaTypeOf as k, PREVIEW_TYPE_MESSAGE as R } from "../preview.mjs";
22
+ import { parentPath as d } from "../security/pathPolicy.mjs";
23
+ import { SensitiveFilePolicy as U } from "../security/sensitiveFiles.mjs";
24
+ const b = "路径必须在工作区内,不能包含 ..、盘符或空字符。";
25
+ function m() {
26
+ return new l(f.Forbidden, b, {
27
+ reason: "path_outside_workspace"
28
+ });
29
+ }
30
+ function u(n, t) {
31
+ if (n == null || n === "") {
32
+ if (t)
33
+ return "";
34
+ throw new l(f.BadRequest, "缺少参数 path。");
35
+ }
36
+ if (typeof n != "string")
37
+ throw new l(f.BadRequest, "path 必须是字符串。");
38
+ if (n.includes("\0") || /%(2e|2f|5c|00)/i.test(n))
39
+ throw m();
40
+ const e = n.normalize("NFC").replace(/\\/g, "/").trim();
41
+ if (/^[a-zA-Z]:/.test(e))
42
+ throw m();
43
+ const s = [];
44
+ for (const i of e.split("/"))
45
+ if (!(i === "" || i === ".")) {
46
+ if (i === "..")
47
+ throw m();
48
+ s.push(i);
49
+ }
50
+ const r = s.join("/");
51
+ if (!r && !t)
52
+ throw new l(f.BadRequest, "path 不能指向工作区根目录。");
53
+ return r;
54
+ }
55
+ function g(n) {
56
+ return new l(f.NotImplemented, `当前工作区不能${n}。`);
57
+ }
58
+ function P(n, t, e) {
59
+ return {
60
+ name: e.name,
61
+ path: t,
62
+ kind: e.kind,
63
+ size: e.size,
64
+ lastModified: e.lastModified,
65
+ sensitive: e.kind === "file" && n.sensitive.isSensitive(t)
66
+ };
67
+ }
68
+ class C {
69
+ constructor(t) {
70
+ p(this, "previews", /* @__PURE__ */ new Map());
71
+ p(this, "previewByPath", /* @__PURE__ */ new Map());
72
+ this.host = t;
73
+ }
74
+ stat(t = "") {
75
+ return a(this, null, function* () {
76
+ const e = u(t, !0), s = yield this.run((r) => r.stat(e));
77
+ return P(this.host, e, s);
78
+ });
79
+ }
80
+ list() {
81
+ return a(this, arguments, function* (t = "", e = {}) {
82
+ const s = u(t, !0);
83
+ return (yield this.run((i) => i.listEntries(s, e.signal))).map((i) => {
84
+ const o = s ? `${s}/${i.name}` : i.name;
85
+ return P(this.host, o, i);
86
+ });
87
+ });
88
+ }
89
+ /**
90
+ * 给 `<img>`、`<video>`、`<audio>` 的地址。浏览器自己拉字节,调用方拿不到文件内容。
91
+ * 同一路径在大小和修改时间没变时返回同一个地址,并增加引用。用完调用 `releaseUrl`。
92
+ */
93
+ url(t) {
94
+ return a(this, null, function* () {
95
+ var c, w;
96
+ const e = u(t, !1);
97
+ if (this.assertAccess(e, "read"), !k(e))
98
+ throw new l(f.BadRequest, R);
99
+ const s = yield this.stat(e);
100
+ if (s.kind !== "file")
101
+ throw new l(f.BadRequest, "目标是目录,不是文件。");
102
+ const r = `${(c = s.size) != null ? c : ""}:${(w = s.lastModified) != null ? w : ""}`, i = this.previewByPath.get(e);
103
+ if ((i == null ? void 0 : i.stamp) === r)
104
+ return i.refs += 1, i.url;
105
+ const o = yield this.openPreview(e), h = {
106
+ url: o.url,
107
+ path: e,
108
+ stamp: r,
109
+ refs: 1,
110
+ release: o.release
111
+ };
112
+ return this.previews.set(h.url, h), this.previewByPath.set(e, h), h.url;
113
+ });
114
+ }
115
+ /** 页面不再显示这个地址时调用。引用归零后,blob 地址会撤销,沙箱票据会作废。 */
116
+ releaseUrl(t) {
117
+ const e = this.previews.get(t);
118
+ e && (e.refs -= 1, !(e.refs > 0) && this.dropPreview(e));
119
+ }
120
+ /** 工作区断开时释放还没归还的地址。 */
121
+ releaseAllUrls() {
122
+ const t = [...this.previews.values()];
123
+ this.previews.clear(), this.previewByPath.clear(), t.forEach((e) => e.release());
124
+ }
125
+ read(t) {
126
+ return a(this, null, function* () {
127
+ const e = u(t, !1);
128
+ return this.assertAccess(e, "read"), this.run((s) => a(this, null, function* () {
129
+ if ((yield s.stat(e)).kind !== "file")
130
+ throw new l(f.BadRequest, "目标是目录,不是文件。");
131
+ return s.readFile(e);
132
+ }));
133
+ });
134
+ }
135
+ readText(t) {
136
+ return a(this, null, function* () {
137
+ return (yield this.read(t)).text();
138
+ });
139
+ }
140
+ write(r, i) {
141
+ return a(this, arguments, function* (t, e, s = {}) {
142
+ const o = u(t, !1);
143
+ this.assertAccess(o, "write"), yield this.run((h) => h.writeFile(o, e, {
144
+ createDirectories: s.createDirectories !== !1,
145
+ signal: s.signal
146
+ })), this.host.notifyFilesChanged([d(o)]);
147
+ });
148
+ }
149
+ append(r, i) {
150
+ return a(this, arguments, function* (t, e, s = {}) {
151
+ const o = u(t, !1);
152
+ this.assertAccess(o, "write");
153
+ const h = typeof e == "string" ? new TextEncoder().encode(e) : e;
154
+ yield this.run((c) => a(this, null, function* () {
155
+ let w = new Uint8Array();
156
+ try {
157
+ if ((yield c.stat(o)).kind !== "file")
158
+ throw new l(f.BadRequest, "目标是目录,不是文件。");
159
+ w = new Uint8Array(yield (yield c.readFile(o)).arrayBuffer());
160
+ } catch (y) {
161
+ if (y instanceof l || F(y).code !== f.NotFound)
162
+ throw y;
163
+ }
164
+ const v = new Uint8Array(w.byteLength + h.byteLength);
165
+ v.set(w), v.set(h, w.byteLength), yield c.writeFile(o, v, {
166
+ createDirectories: !0,
167
+ signal: s.signal
168
+ });
169
+ })), this.host.notifyFilesChanged([d(o)]);
170
+ });
171
+ }
172
+ mkdir(t) {
173
+ return a(this, null, function* () {
174
+ const e = u(t, !0);
175
+ e && (yield this.run((s) => a(this, null, function* () {
176
+ if (!s.createDirectory)
177
+ throw g("创建目录");
178
+ yield s.createDirectory(e);
179
+ })), this.host.notifyFilesChanged([d(e)]));
180
+ });
181
+ }
182
+ remove(s) {
183
+ return a(this, arguments, function* (t, e = {}) {
184
+ const r = u(t, !1);
185
+ this.assertAccess(r, "write"), yield this.run((i) => a(this, null, function* () {
186
+ if (!i.remove)
187
+ throw g("删除");
188
+ yield i.remove(r, e);
189
+ })), this.host.notifyFilesChanged([d(r)]);
190
+ });
191
+ }
192
+ move(t, e) {
193
+ return a(this, null, function* () {
194
+ const s = u(t, !1), r = u(e, !1);
195
+ this.assertAccess(s, "write"), this.assertAccess(r, "write"), yield this.run((i) => a(this, null, function* () {
196
+ if (!i.move)
197
+ throw g("移动");
198
+ yield i.move(s, r);
199
+ })), this.host.notifyFilesChanged([d(s), d(r)]);
200
+ });
201
+ }
202
+ openPreview(t) {
203
+ return a(this, null, function* () {
204
+ return this.run((e) => a(this, null, function* () {
205
+ if (e.url)
206
+ return e.url(t);
207
+ const s = yield e.readFile(t), r = URL.createObjectURL(s);
208
+ return {
209
+ url: r,
210
+ release: () => URL.revokeObjectURL(r)
211
+ };
212
+ }));
213
+ });
214
+ }
215
+ dropPreview(t) {
216
+ t.release(), this.previews.delete(t.url), this.previewByPath.get(t.path) === t && this.previewByPath.delete(t.path);
217
+ }
218
+ assertAccess(t, e) {
219
+ const s = this.host.sensitive.match(t, e);
220
+ if (s)
221
+ throw new l(f.Forbidden, U.reasonOf(s), {
222
+ reason: "sensitive_file",
223
+ ruleId: s.id
224
+ });
225
+ }
226
+ run(t) {
227
+ return a(this, null, function* () {
228
+ var s;
229
+ const e = this.host.adapter;
230
+ if (!e)
231
+ throw E();
232
+ try {
233
+ return yield t(e);
234
+ } catch (r) {
235
+ if (r instanceof l)
236
+ throw r;
237
+ const i = F(r);
238
+ throw ((s = i.data) == null ? void 0 : s.reason) === "workspace_not_granted" && (yield this.host.handlePermissionLost()), i;
239
+ }
240
+ });
241
+ }
242
+ }
243
+ export {
244
+ C as WorkspaceFiles,
245
+ u as normalizeFilePath
246
+ };