@glodon-aiot/bot-client-sdk 3.26.0-alpha.9 → 3.27.0-alpha.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.
Files changed (72) hide show
  1. package/README.md +246 -225
  2. package/dist/es/AgentSession.mjs +275 -197
  3. package/dist/es/BotClient.mjs +200 -123
  4. package/dist/es/Config.mjs +58 -68
  5. package/dist/es/DialogSession.mjs +123 -118
  6. package/dist/es/EventBus.mjs +38 -34
  7. package/dist/es/Logger.mjs +19 -21
  8. package/dist/es/Session.mjs +338 -284
  9. package/dist/es/api/index.mjs +31 -30
  10. package/dist/es/builtinTools/constants.mjs +12 -5
  11. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +182 -160
  12. package/dist/es/builtinTools/paths.mjs +34 -27
  13. package/dist/es/builtinTools/platformSkills.mjs +248 -0
  14. package/dist/es/builtinTools/schemas.mjs +63 -18
  15. package/dist/es/builtinTools/tools.mjs +368 -244
  16. package/dist/es/index.mjs +62 -61
  17. package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
  18. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +113 -131
  19. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +60 -55
  20. package/dist/es/localFiles/constants.mjs +24 -28
  21. package/dist/es/localFiles/createLocalFilesPlugin.mjs +53 -52
  22. package/dist/es/localFiles/errors.mjs +44 -35
  23. package/dist/es/localFiles/host/connectHostBridge.mjs +20 -15
  24. package/dist/es/localFiles/host/protocol.mjs +27 -14
  25. package/dist/es/localFiles/host/sandboxClient.mjs +297 -127
  26. package/dist/es/localFiles/preview.mjs +33 -0
  27. package/dist/es/localFiles/schemas.mjs +22 -53
  28. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +156 -89
  29. package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
  30. package/dist/es/localMcp/client.mjs +234 -0
  31. package/dist/es/localMcp/endpoints.mjs +94 -0
  32. package/dist/es/localMcp/register.mjs +207 -0
  33. package/dist/es/localMcp/registry.mjs +43 -0
  34. package/dist/es/package.json.mjs +29 -49
  35. package/dist/es/sessionUpload.mjs +49 -0
  36. package/dist/es/utils.mjs +24 -11
  37. package/dist/lib/index.js +48 -40
  38. package/dist/src/AgentSession.d.ts +13 -0
  39. package/dist/src/BotClient.d.ts +13 -1
  40. package/dist/src/Config.d.ts +14 -7
  41. package/dist/src/EventBus.d.ts +3 -0
  42. package/dist/src/Logger.d.ts +15 -8
  43. package/dist/src/PayloadTypes.d.ts +7 -0
  44. package/dist/src/Session.d.ts +19 -2
  45. package/dist/src/api/index.d.ts +2 -2
  46. package/dist/src/builtinTools/command.d.ts +7 -0
  47. package/dist/src/builtinTools/constants.d.ts +10 -2
  48. package/dist/src/builtinTools/paths.d.ts +3 -2
  49. package/dist/src/builtinTools/platformSkills.d.ts +92 -0
  50. package/dist/src/builtinTools/tools.d.ts +13 -0
  51. package/dist/src/constant.d.ts +1 -1
  52. package/dist/src/index.d.ts +2 -1
  53. package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
  54. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +7 -4
  55. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +8 -0
  56. package/dist/src/localFiles/constants.d.ts +4 -13
  57. package/dist/src/localFiles/errors.d.ts +1 -1
  58. package/dist/src/localFiles/host/protocol.d.ts +14 -3
  59. package/dist/src/localFiles/host/sandboxClient.d.ts +50 -0
  60. package/dist/src/localFiles/index.d.ts +9 -5
  61. package/dist/src/localFiles/preview.d.ts +2 -0
  62. package/dist/src/localFiles/schemas.d.ts +0 -2
  63. package/dist/src/localFiles/types.d.ts +2 -22
  64. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +25 -3
  65. package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
  66. package/dist/src/localMcp/client.d.ts +15 -0
  67. package/dist/src/localMcp/endpoints.d.ts +19 -0
  68. package/dist/src/localMcp/register.d.ts +34 -0
  69. package/dist/src/localMcp/registry.d.ts +27 -0
  70. package/dist/src/sessionUpload.d.ts +14 -0
  71. package/dist/src/utils.d.ts +6 -0
  72. package/package.json +4 -24
@@ -1,5 +1,5 @@
1
- import { LOCAL_PYTHON_FUNCTION_CODE as s, WORKSPACE_GRANT_ID as p } from "./constants.mjs";
2
- const t = "只操作用户在当前浏览器里选择的本地文件夹,路径相对该文件夹。云端工作区的文件不要用本工具。", e = {
1
+ import { WORKSPACE_GRANT_ID as p } from "./constants.mjs";
2
+ const e = "只操作用户在当前浏览器里选择的本地文件夹,路径相对该文件夹。云端工作区的文件不要用本工具。", t = {
3
3
  type: "string",
4
4
  enum: [p],
5
5
  description: `工作区 ID。省略时按 ${p} 处理,传其他值会被拒绝。`
@@ -9,16 +9,16 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
9
9
  }), r = {
10
10
  type: "string",
11
11
  description: "文件当前内容的 hash(sha256:<hex>),来自 read_file 或 stat_file 的返回值。"
12
- }, d = {
12
+ }, o = {
13
13
  list_directory: {
14
14
  functionCode: "list_directory",
15
15
  name: "列出本地目录",
16
- description: `${t}列出某个目录的直接子项(目录在前),支持分页。`,
16
+ description: `${e}列出某个目录的直接子项(目录在前),支持分页。`,
17
17
  parameters: {
18
18
  type: "object",
19
19
  required: ["grantId"],
20
20
  properties: {
21
- grantId: e,
21
+ grantId: t,
22
22
  path: i('要列出的目录,省略或传 "." 表示工作区根目录。'),
23
23
  offset: {
24
24
  type: "integer",
@@ -37,12 +37,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
37
37
  read_file: {
38
38
  functionCode: "read_file",
39
39
  name: "读取本地文件",
40
- description: `${t}按行读取文本文件,单次最多返回 1 MiB,超出部分会被截断(truncated: true)。返回内容的 hash 可用于后续修改。`,
40
+ description: `${e}按行读取文本文件,单次最多返回 1 MiB,超出部分会被截断(truncated: true)。返回内容的 hash 可用于后续修改。`,
41
41
  parameters: {
42
42
  type: "object",
43
43
  required: ["grantId", "path"],
44
44
  properties: {
45
- grantId: e,
45
+ grantId: t,
46
46
  path: i("要读取的文件。"),
47
47
  startLine: {
48
48
  type: "integer",
@@ -70,12 +70,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
70
70
  stat_file: {
71
71
  functionCode: "stat_file",
72
72
  name: "查看本地文件信息",
73
- description: `${t}返回文件或目录的类型、大小、修改时间,可选返回 hash。不返回文件内容。`,
73
+ description: `${e}返回文件或目录的类型、大小、修改时间,可选返回 hash。不返回文件内容。`,
74
74
  parameters: {
75
75
  type: "object",
76
76
  required: ["grantId", "path"],
77
77
  properties: {
78
- grantId: e,
78
+ grantId: t,
79
79
  path: i("要查看的文件或目录。"),
80
80
  includeHash: {
81
81
  type: "boolean",
@@ -87,12 +87,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
87
87
  search_files: {
88
88
  functionCode: "search_files",
89
89
  name: "查找本地文件",
90
- description: `${t}按文件名或 glob(支持 *、?、**)查找文件。不含 / 的模式匹配文件名,含 / 的模式匹配相对 path 的路径。`,
90
+ description: `${e}按文件名或 glob(支持 *、?、**)查找文件。不含 / 的模式匹配文件名,含 / 的模式匹配相对 path 的路径。`,
91
91
  parameters: {
92
92
  type: "object",
93
93
  required: ["grantId", "pattern"],
94
94
  properties: {
95
- grantId: e,
95
+ grantId: t,
96
96
  pattern: {
97
97
  type: "string",
98
98
  minLength: 1,
@@ -117,12 +117,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
117
117
  search_text: {
118
118
  functionCode: "search_text",
119
119
  name: "搜索本地文本",
120
- description: `${t}在指定文件或目录(递归)中搜索普通文本(不支持正则),返回所在文件、行号与该行内容。必须指定 path,不能搜索整个文件夹。`,
120
+ description: `${e}在指定文件或目录(递归)中搜索普通文本(不支持正则),返回所在文件、行号与该行内容。必须指定 path,不能搜索整个文件夹。`,
121
121
  parameters: {
122
122
  type: "object",
123
123
  required: ["grantId", "path", "query"],
124
124
  properties: {
125
- grantId: e,
125
+ grantId: t,
126
126
  path: i("搜索范围,可以是文件或目录。"),
127
127
  query: {
128
128
  type: "string",
@@ -159,12 +159,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
159
159
  apply_patch: {
160
160
  functionCode: "apply_patch",
161
161
  name: "修改本地文件",
162
- description: `${t}以查找替换的方式修改 UTF-8 文本文件。每条修改的 oldText 必须在文件中唯一出现(或设置 replaceAll)。执行前需要用户确认。`,
162
+ description: `${e}以查找替换的方式修改 UTF-8 文本文件。每条修改的 oldText 必须在文件中唯一出现(或设置 replaceAll)。执行前需要用户确认。`,
163
163
  parameters: {
164
164
  type: "object",
165
165
  required: ["grantId", "path", "expectedHash", "edits"],
166
166
  properties: {
167
- grantId: e,
167
+ grantId: t,
168
168
  path: i("要修改的文件。"),
169
169
  expectedHash: r,
170
170
  edits: {
@@ -197,12 +197,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
197
197
  write_file: {
198
198
  functionCode: "write_file",
199
199
  name: "写入本地文件",
200
- description: `${t}新建文件,或整体覆盖已有文件(覆盖时必须提供 expectedHash)。修改已有文件优先使用 apply_patch。执行前需要用户确认。`,
200
+ description: `${e}新建文件,或整体覆盖已有文件(覆盖时必须提供 expectedHash)。修改已有文件优先使用 apply_patch。执行前需要用户确认。`,
201
201
  parameters: {
202
202
  type: "object",
203
203
  required: ["grantId", "path", "content"],
204
204
  properties: {
205
- grantId: e,
205
+ grantId: t,
206
206
  path: i("要写入的文件。"),
207
207
  content: {
208
208
  type: "string",
@@ -219,12 +219,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
219
219
  upload_file: {
220
220
  functionCode: "upload_file",
221
221
  name: "上传本地文件",
222
- description: `${t}把本地文件夹中的文件上传到平台文件服务,返回 fileId、name、size 和 url。url 是文件访问地址,可直接用于后续推理或对话。下载回本地文件夹时使用 download_file,source.type 为 platformFile。单文件不超过 50 MB。执行前需要用户确认。`,
222
+ description: `${e}把本地文件夹中的文件上传到平台文件服务,返回 fileId、name、size 和 url。url 是文件访问地址,可直接用于后续推理或对话。下载回本地文件夹时使用 download_file,source.type 为 platformFile。单文件不超过 50 MB。执行前需要用户确认。`,
223
223
  parameters: {
224
224
  type: "object",
225
225
  required: ["grantId", "path"],
226
226
  properties: {
227
- grantId: e,
227
+ grantId: t,
228
228
  path: i("要上传的文件。"),
229
229
  target: {
230
230
  type: "string",
@@ -240,12 +240,12 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
240
240
  download_file: {
241
241
  functionCode: "download_file",
242
242
  name: "下载到本地文件夹",
243
- description: `${t}把会话文件、平台文件或 https 地址保存到本地文件夹。source.type 为 url 时必须同时填写 source.url(完整地址),为 sessionFile 或 platformFile 时必须同时填写 source.fileId。不要只传 type。单文件不超过 50 MB。执行前需要用户确认。`,
243
+ description: `${e}把会话文件、平台文件或 https 地址保存到本地文件夹。source.type 为 url 时必须同时填写 source.url(完整地址),为 sessionFile 或 platformFile 时必须同时填写 source.fileId。不要只传 type。单文件不超过 50 MB。执行前需要用户确认。`,
244
244
  parameters: {
245
245
  type: "object",
246
246
  required: ["grantId", "destPath", "source"],
247
247
  properties: {
248
- grantId: e,
248
+ grantId: t,
249
249
  destPath: i("保存位置(文件路径)。"),
250
250
  source: {
251
251
  type: "object",
@@ -275,38 +275,7 @@ const t = "只操作用户在当前浏览器里选择的本地文件夹,路径
275
275
  }
276
276
  }
277
277
  }
278
- }, o = "在已绑定的本地工程目录中运行已有 .py,路径相对该目录。不要用于云端工作区,不要传 shell 或代码,只能传 path 和 args。", c = {
279
- functionCode: s,
280
- name: "运行本地 Python 脚本",
281
- description: `${o}每次运行需用户单独确认,文件读写授权不能代替。确认后脚本可直接读写该目录并使用标准库,改动立即落盘。不能联网,不能在运行中安装包,也不能访问该目录以外的文件。第三方包写在工程根目录 sandbox-requirements.txt。结果打印到标准输出。code 为 0 只表示已结束,成功看 data.exitCode,超时看 data.timedOut。`,
282
- parameters: {
283
- type: "object",
284
- required: ["grantId", "path"],
285
- properties: {
286
- grantId: e,
287
- path: {
288
- type: "string",
289
- description: "要运行的脚本。必须是已存在、以 .py 结尾的相对路径,以 / 分隔,不能包含 .. 、盘符或绝对路径。"
290
- },
291
- args: {
292
- type: "array",
293
- maxItems: 16,
294
- description: "传给脚本的参数,最多 16 个。每个参数单独一项,不能含空格、引号或 shell 语法,只能包含字母、数字、下划线以及 . / : @ = + - , 。",
295
- items: {
296
- type: "string",
297
- description: "一个脚本参数。"
298
- }
299
- },
300
- timeoutMs: {
301
- type: "integer",
302
- minimum: 1e3,
303
- maximum: 12e4,
304
- description: "超时毫秒数。默认 20000,最大 120000。到时脚本会被终止。"
305
- }
306
- }
307
- }
308
278
  };
309
279
  export {
310
- d as LOCAL_FILE_TOOL_SCHEMAS,
311
- c as LOCAL_PYTHON_TOOL_SCHEMA
280
+ o as LOCAL_FILE_TOOL_SCHEMAS
312
281
  };
@@ -1,46 +1,50 @@
1
- var S = Object.defineProperty, C = Object.defineProperties;
2
- var P = Object.getOwnPropertyDescriptors;
3
- var _ = Object.getOwnPropertySymbols;
4
- var H = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
5
- var p = (r, e, t) => e in r ? S(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, m = (r, e) => {
1
+ var S = Object.defineProperty, b = Object.defineProperties;
2
+ var H = Object.getOwnPropertyDescriptors;
3
+ var k = Object.getOwnPropertySymbols;
4
+ var R = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable;
5
+ var p = (r, e, t) => e in r ? S(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
- H.call(e, t) && p(r, t, e[t]);
8
- if (_)
9
- for (var t of _(e))
10
- R.call(e, t) && p(r, t, e[t]);
7
+ R.call(e, t) && p(r, t, e[t]);
8
+ if (k)
9
+ for (var t of k(e))
10
+ T.call(e, t) && p(r, t, e[t]);
11
11
  return r;
12
- }, k = (r, e) => C(r, P(e));
12
+ }, C = (r, e) => b(r, H(e));
13
13
  var s = (r, e, t) => (p(r, typeof e != "symbol" ? e + "" : e, t), t);
14
- var o = (r, e, t) => new Promise((i, n) => {
15
- var a = (l) => {
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
- n(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
- n(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(a, 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 b from "../../EventBus.mjs";
31
- import { FileSystemAccessAdapter as D } from "../adapters/FileSystemAccessAdapter.mjs";
32
- import { getNativeDirectoryPicker as T } from "../adapters/fsaTypes.mjs";
33
- import { LOCAL_FILES_FUNCTION_CODES as F } from "../constants.mjs";
34
- import { getDownloadUrlAllowlist as W } from "../security/downloadUrlPolicy.mjs";
30
+ import F from "../../Logger.mjs";
31
+ import D from "../../EventBus.mjs";
32
+ import { FileSystemAccessAdapter as _ } from "../adapters/FileSystemAccessAdapter.mjs";
33
+ import { HostBridgeAdapter as W } from "../adapters/HostBridgeAdapter.mjs";
34
+ import { FileSnapshotAdapter as L } from "../adapters/FileSnapshotAdapter.mjs";
35
+ import { getNativeDirectoryPicker as E } from "../adapters/fsaTypes.mjs";
36
+ import { LOCAL_FILES_FUNCTION_CODES as I } from "../constants.mjs";
37
+ import { getDownloadUrlAllowlist as N } from "../security/downloadUrlPolicy.mjs";
35
38
  import { SensitiveFilePolicy as q } from "../security/sensitiveFiles.mjs";
36
- import { resolveLimits as I } from "../limits.mjs";
37
- import { createDefaultSessionFileTransfer as L } from "../transfer/SessionFileTransfer.mjs";
38
- import { createMemoryStore as N } from "./handleStore.mjs";
39
- import { watchDirectoryHandle as E } from "./watchDirectory.mjs";
40
- const u = {
39
+ import { resolveLimits as U } from "../limits.mjs";
40
+ import { createDefaultSessionFileTransfer as j } from "../transfer/SessionFileTransfer.mjs";
41
+ import { createMemoryStore as x } from "./handleStore.mjs";
42
+ import { watchDirectoryHandle as K } from "./watchDirectory.mjs";
43
+ import { WorkspaceFiles as V } from "./WorkspaceFiles.mjs";
44
+ const m = {
41
45
  mode: "readwrite"
42
46
  };
43
- function U(r) {
47
+ function M(r) {
44
48
  const e = r.replace(/\\/g, "/").trim().replace(/^\.\/+/, "").replace(/\/+$/, "");
45
49
  if (!e || e === ".")
46
50
  return "";
@@ -50,10 +54,11 @@ function U(r) {
50
54
  if (!t.some((i) => !i || i === "." || i === ".."))
51
55
  return t.join("/");
52
56
  }
53
- function x(r) {
57
+ function O(r) {
54
58
  return !!r && typeof r == "object" && r.name === "AbortError";
55
59
  }
56
- class X {
60
+ const P = new F("[LocalWorkspace] ");
61
+ class ne {
57
62
  constructor(e) {
58
63
  s(this, "applicationId");
59
64
  s(this, "limits");
@@ -63,17 +68,21 @@ class X {
63
68
  s(this, "transfer");
64
69
  s(this, "fetch");
65
70
  s(this, "commandRunner");
71
+ /** 平台技能同步。由 BotClient 在启用本地工作区后挂上。 */
72
+ s(this, "platformSkills");
66
73
  s(this, "browserRuntime");
74
+ /** 接入方的文件操作入口。不要使用 `adapter`。 */
75
+ s(this, "files");
67
76
  s(this, "store");
68
77
  s(this, "picker");
69
- s(this, "event", new b(!0));
78
+ s(this, "event", new D(!0));
70
79
  s(this, "_status");
71
80
  s(this, "_displayName");
72
81
  s(this, "_adapter", null);
73
82
  s(this, "pendingHandle", null);
74
83
  s(this, "approvalHandler");
75
84
  s(this, "activeCalls", 0);
76
- s(this, "commandRunning", !1);
85
+ s(this, "activeCommands", 0);
77
86
  s(this, "inflight", /* @__PURE__ */ new Set());
78
87
  s(this, "_ready", Promise.resolve());
79
88
  s(this, "stopWatching", () => {
@@ -81,14 +90,16 @@ class X {
81
90
  s(this, "pendingChangePaths", /* @__PURE__ */ new Set());
82
91
  s(this, "changeTimer");
83
92
  s(this, "refreshAllVisible", !1);
84
- var h, c, l, d, f, g, y, v, w, A;
93
+ /** 选择、关闭会推进它。进行中的 restore 若发现已经变了,就不再覆盖当前目录。 */
94
+ s(this, "epoch", 0);
95
+ var h, l, o, c, f, g, v, y, w, A;
85
96
  const t = (h = e.config) != null ? h : {};
86
- this.applicationId = e.applicationId, this.store = (c = e.store) != null ? c : N(), this.picker = e.picker === null ? void 0 : (l = e.picker) != null ? l : T(), this.limits = I(t.limits), this.sensitive = new q(t.sensitiveRulesAppend), this.downloadUrlAllowlist = W(t.downloadUrlAllowlistAppend), this.allowWorkspaceWideTextSearch = !!t.allowWorkspaceWideTextSearch, this.approvalHandler = t.onApprovalRequired;
87
- const i = (d = e.transfer) != null ? d : L(() => {
97
+ this.applicationId = e.applicationId, this.store = (l = e.store) != null ? l : x(), this.picker = e.picker === null ? void 0 : (o = e.picker) != null ? o : E(), this.limits = U(t.limits), this.sensitive = new q(t.sensitiveRulesAppend), this.downloadUrlAllowlist = N(t.downloadUrlAllowlistAppend), this.allowWorkspaceWideTextSearch = !!t.allowWorkspaceWideTextSearch, this.approvalHandler = t.onApprovalRequired;
98
+ const i = (c = e.transfer) != null ? c : j(() => {
88
99
  });
89
100
  this.transfer = {
90
101
  upload: (g = (f = t.transfer) == null ? void 0 : f.upload) != null ? g : i.upload,
91
- resolveDownload: (v = (y = t.transfer) == null ? void 0 : y.resolveDownload) != null ? v : i.resolveDownload
102
+ resolveDownload: (y = (v = t.transfer) == null ? void 0 : v.resolveDownload) != null ? y : i.resolveDownload
92
103
  };
93
104
  const n = (w = e.fetch) != null ? w : typeof fetch == "function" ? fetch : void 0;
94
105
  this.fetch = n ? n.bind(globalThis) : () => Promise.reject(new Error("fetch is not available")), this.commandRunner = e.commandRunner;
@@ -97,7 +108,7 @@ class X {
97
108
  enabled: (a == null ? void 0 : a.enabled) !== !1,
98
109
  bashModuleURL: a == null ? void 0 : a.bashModuleURL,
99
110
  pyodideIndexURL: a == null ? void 0 : a.pyodideIndexURL
100
- }, this._status = this.picker ? "none" : "unsupported";
111
+ }, this._status = this.picker ? "none" : "unsupported", this.files = new V(this);
101
112
  }
102
113
  // -------------------------------------------------------------------------
103
114
  // 状态
@@ -116,7 +127,10 @@ class X {
116
127
  get ready() {
117
128
  return this._ready;
118
129
  }
119
- /** @internal 已授权时可用的 Adapter。 */
130
+ /**
131
+ * @internal 已授权时的磁盘后端,供内置工具使用。
132
+ * 接入方用 `files`:这里的路径必须已规范化,异常里也可能带系统路径。
133
+ */
120
134
  get adapter() {
121
135
  return this._status === "granted" ? this._adapter : null;
122
136
  }
@@ -127,13 +141,19 @@ class X {
127
141
  return `disclosure:${this.applicationId}`;
128
142
  }
129
143
  setStatus(e, t = this._displayName) {
130
- e === this._status && t === this._displayName || (this._status = e, this._displayName = t, this.emit("workspace:changed", {
144
+ e === this._status && t === this._displayName || (this._status = e, this._displayName = t, P.log("status", {
145
+ status: e,
146
+ displayName: t
147
+ }), this.emit("workspace:changed", {
131
148
  status: e,
132
149
  displayName: t
133
150
  }));
134
151
  }
152
+ replaceAdapter(e) {
153
+ this._adapter !== e && this.files.releaseAllUrls(), this._adapter = e;
154
+ }
135
155
  attachHandle(e) {
136
- this.cancelFilesChanged(), this.pendingHandle = e, this._adapter = new D(e), this.setStatus("granted", e.name), this.unwatchDirectory(), this.stopWatching = E(e, (t) => this.notifyFilesChanged(t));
156
+ this.cancelFilesChanged(), this.pendingHandle = e, this.replaceAdapter(new _(e)), this.setStatus("granted", e.name), this.unwatchDirectory(), this.stopWatching = K(e, (t) => this.notifyFilesChanged(t));
137
157
  }
138
158
  // -------------------------------------------------------------------------
139
159
  // 授权
@@ -153,18 +173,13 @@ class X {
153
173
  } catch (t) {
154
174
  return Promise.reject(t);
155
175
  }
156
- return e.then((t) => o(this, null, function* () {
157
- var n, a;
158
- this.cancelAll();
159
- const i = (a = yield (n = t.queryPermission) == null ? void 0 : n.call(t, u)) != null ? a : "granted";
160
- return yield this.store.set(this.handleKey, {
161
- handle: t,
162
- displayName: t.name,
163
- savedAt: Date.now()
164
- }).catch(() => {
165
- }), i === "denied" ? (this.pendingHandle = t, this._adapter = null, this.setStatus("denied", t.name), !1) : (this.attachHandle(t), !0);
166
- }), (t) => {
167
- if (x(t))
176
+ return e.then((t) => (this.epoch += 1, this.cancelAll(), this.attachHandle(t), this.store.set(this.handleKey, {
177
+ handle: t,
178
+ displayName: t.name,
179
+ savedAt: Date.now()
180
+ }).catch(() => {
181
+ }), !0), (t) => {
182
+ if (O(t))
168
183
  return !1;
169
184
  throw t;
170
185
  });
@@ -174,29 +189,51 @@ class X {
174
189
  return this._ready = this.doRestore(), this._ready;
175
190
  }
176
191
  doRestore() {
177
- return o(this, null, function* () {
178
- var i, n, a;
192
+ return d(this, null, function* () {
193
+ var n, a, h;
194
+ const e = this.epoch;
179
195
  if (!this.picker) {
180
196
  this._adapter || this.setStatus("unsupported");
181
197
  return;
182
198
  }
183
- let e;
199
+ let t;
184
200
  try {
185
- e = yield this.store.get(this.handleKey);
186
- } catch (h) {
187
- e = void 0;
201
+ t = yield this.store.get(this.handleKey);
202
+ } catch (l) {
203
+ t = void 0;
188
204
  }
189
- if (!(e != null && e.handle)) {
190
- this.pendingHandle = null, this._adapter = null, this.setStatus("none", void 0);
205
+ if (e !== this.epoch)
206
+ return;
207
+ if (!(t != null && t.handle)) {
208
+ this.pendingHandle = null, this.replaceAdapter(null), this.setStatus("none", void 0);
191
209
  return;
192
210
  }
193
- let t;
211
+ let i;
194
212
  try {
195
- t = (a = yield (n = (i = e.handle).queryPermission) == null ? void 0 : n.call(i, u)) != null ? a : "prompt";
196
- } catch (h) {
197
- t = "prompt";
213
+ i = (h = yield (a = (n = t.handle).queryPermission) == null ? void 0 : a.call(n, m)) != null ? h : "prompt";
214
+ } catch (l) {
215
+ i = "prompt";
216
+ }
217
+ if (e === this.epoch) {
218
+ if (i === "granted" || i === "prompt" && (yield this.canList(t.handle))) {
219
+ if (e !== this.epoch)
220
+ return;
221
+ this.attachHandle(t.handle);
222
+ return;
223
+ }
224
+ this.pendingHandle = t.handle, this.replaceAdapter(null), this.setStatus(i === "denied" ? "denied" : "prompt", t.displayName);
198
225
  }
199
- t === "granted" ? this.attachHandle(e.handle) : (this.pendingHandle = e.handle, this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.displayName));
226
+ });
227
+ }
228
+ /** queryPermission 仍可能是 prompt,但选择框留下的句柄其实列得出目录。 */
229
+ canList(e) {
230
+ return d(this, null, function* () {
231
+ const t = new _(e);
232
+ let i;
233
+ const n = new Promise((l) => {
234
+ i = setTimeout(() => l(!1), 2e3);
235
+ }), a = t.listEntries("").then(() => !0).catch(() => !1), h = yield Promise.race([a, n]);
236
+ return clearTimeout(i), h;
200
237
  });
201
238
  }
202
239
  /** 对已保存的目录重新申请权限。必须在用户点击事件中调用。 */
@@ -206,22 +243,36 @@ class X {
206
243
  return Promise.resolve(!1);
207
244
  let t;
208
245
  try {
209
- t = e.requestPermission(u);
246
+ t = e.requestPermission(m);
210
247
  } catch (i) {
211
248
  return Promise.reject(i);
212
249
  }
213
- return t.then((i) => i === "granted" ? (this.attachHandle(e), !0) : (this._adapter = null, this.setStatus(i === "denied" ? "denied" : "prompt", e.name), !1));
250
+ return t.then((i) => i === "granted" ? (this.attachHandle(e), !0) : (this.replaceAdapter(null), this.setStatus(i === "denied" ? "denied" : "prompt", e.name), !1));
214
251
  }
215
252
  /** 断开工作区并删除持久化句柄。状态立即变为未授权,存储删除在其后完成。 */
216
253
  clear() {
217
- return o(this, null, function* () {
218
- 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(() => {
254
+ return d(this, null, function* () {
255
+ 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(() => {
219
256
  });
220
257
  });
221
258
  }
259
+ /**
260
+ * 用文件列表挂上工作区。路径来自 webkitRelativePath,选完就能列出层级。
261
+ * 写入只留在内存里,不会出现在用户磁盘上。需要落盘时用 pick()。
262
+ */
263
+ attachFileSnapshot(e, t) {
264
+ var h, l, o;
265
+ 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) => ({
266
+ name: c.name,
267
+ webkitRelativePath: c.webkitRelativePath,
268
+ lastModified: c.lastModified,
269
+ blob: c
270
+ }));
271
+ this.attachAdapter(new L(a, n), n);
272
+ }
222
273
  /** 直接挂载一个 Adapter(测试或自定义后端),视为已授权。 */
223
274
  attachAdapter(e, t = e.displayName) {
224
- this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this._adapter = e, this.setStatus("granted", t);
275
+ this.epoch += 1, this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this.replaceAdapter(e), this.setStatus("granted", t);
225
276
  }
226
277
  /**
227
278
  * 通知目录树重读。`paths` 是相对工作区根的目录;省略时重读当前展开的全部目录。
@@ -233,7 +284,7 @@ class X {
233
284
  this.refreshAllVisible = !0;
234
285
  else
235
286
  for (const t of e) {
236
- const i = U(t);
287
+ const i = M(t);
237
288
  i === void 0 ? this.refreshAllVisible = !0 : this.pendingChangePaths.add(i);
238
289
  }
239
290
  clearTimeout(this.changeTimer), this.changeTimer = setTimeout(() => this.flushFilesChanged(), 150);
@@ -241,19 +292,29 @@ class X {
241
292
  }
242
293
  /** @internal 工具执行中遇到权限错误时调用,重新检查并更新状态。 */
243
294
  handlePermissionLost() {
244
- return o(this, null, function* () {
295
+ return d(this, null, function* () {
245
296
  const e = this.pendingHandle;
246
297
  if (!(e != null && e.queryPermission))
247
298
  return;
248
- const t = yield e.queryPermission(u).catch(() => "prompt");
249
- t !== "granted" && (this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.name));
299
+ const t = yield e.queryPermission(m).catch(() => "prompt");
300
+ if (t !== "granted") {
301
+ if (t === "prompt" && this._adapter)
302
+ try {
303
+ yield this._adapter.listEntries("");
304
+ return;
305
+ } catch (i) {
306
+ if ((i && typeof i == "object" ? i.name : void 0) !== "NotAllowedError")
307
+ return;
308
+ }
309
+ this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.replaceAdapter(null), this.setStatus(t === "denied" ? "denied" : "prompt", e.name);
310
+ }
250
311
  });
251
312
  }
252
313
  // -------------------------------------------------------------------------
253
314
  // 外发提示
254
315
  // -------------------------------------------------------------------------
255
316
  isDisclosureAcknowledged() {
256
- return o(this, null, function* () {
317
+ return d(this, null, function* () {
257
318
  try {
258
319
  return !!(yield this.store.get(this.disclosureKey));
259
320
  } catch (e) {
@@ -262,7 +323,7 @@ class X {
262
323
  });
263
324
  }
264
325
  acknowledgeDisclosure() {
265
- return o(this, null, function* () {
326
+ return d(this, null, function* () {
266
327
  yield this.store.set(this.disclosureKey, !0).catch(() => {
267
328
  });
268
329
  });
@@ -290,14 +351,20 @@ class X {
290
351
  e || (e = !0, this.activeCalls--);
291
352
  };
292
353
  }
293
- /** @internal 同一工作区同一时间只跑一个命令;已有命令在运行时返回 null。 */
354
+ /**
355
+ * 占用一个浏览器运行时的命令名额。本地沙箱在时页面不设上限,并发由沙箱的 exec.maxConcurrent 决定。
356
+ * 浏览器运行时一次只放一条。
357
+ */
294
358
  acquireCommand() {
295
- if (this.commandRunning)
359
+ if (this._adapter instanceof W)
360
+ return () => {
361
+ };
362
+ if (this.activeCommands >= 1)
296
363
  return null;
297
- this.commandRunning = !0;
364
+ this.activeCommands++;
298
365
  let e = !1;
299
366
  return () => {
300
- e || (e = !0, this.commandRunning = !1);
367
+ e || (e = !0, this.activeCommands--);
301
368
  };
302
369
  }
303
370
  /** @internal 登记进行中的调用,更换或断开工作区时统一中止。 */
@@ -310,14 +377,14 @@ class X {
310
377
  this.inflight.clear(), e.forEach((t) => t.abort());
311
378
  }
312
379
  handshake() {
313
- const e = Object.fromEntries(F.map((t) => [t, !0]));
380
+ const e = Object.fromEntries(I.map((t) => [t, !0]));
314
381
  return {
315
382
  protocolVersion: "1.0",
316
- capabilities: k(m({}, e), {
383
+ capabilities: C(u({}, e), {
317
384
  cancellation: !0,
318
385
  atomicWrite: !0
319
386
  }),
320
- limits: m({}, this.limits)
387
+ limits: u({}, this.limits)
321
388
  };
322
389
  }
323
390
  // -------------------------------------------------------------------------
@@ -349,10 +416,10 @@ class X {
349
416
  try {
350
417
  this.event.emit(e, t);
351
418
  } catch (i) {
352
- console.error(`[LocalWorkspace] ${e} listener failed`, i);
419
+ P.report(`${e} listener failed`, i);
353
420
  }
354
421
  }
355
422
  }
356
423
  export {
357
- X as LocalWorkspace
424
+ ne as LocalWorkspace
358
425
  };