@glodon-aiot/bot-client-sdk 3.26.0-alpha.14 → 3.26.0-alpha.16

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.
@@ -1,96 +1,99 @@
1
- var S = Object.defineProperty, C = Object.defineProperties;
2
- var P = Object.getOwnPropertyDescriptors;
1
+ var S = Object.defineProperty, b = Object.defineProperties;
2
+ var C = Object.getOwnPropertyDescriptors;
3
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) => {
4
+ var R = Object.prototype.hasOwnProperty, H = Object.prototype.propertyIsEnumerable;
5
+ var u = (r, e, t) => e in r ? S(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, p = (r, e) => {
6
6
  for (var t in e || (e = {}))
7
- H.call(e, t) && p(r, t, e[t]);
7
+ R.call(e, t) && u(r, t, e[t]);
8
8
  if (_)
9
9
  for (var t of _(e))
10
- R.call(e, t) && p(r, t, e[t]);
10
+ H.call(e, t) && u(r, t, e[t]);
11
11
  return r;
12
- }, k = (r, e) => C(r, P(e));
13
- var i = (r, e, t) => (p(r, typeof e != "symbol" ? e + "" : e, t), t);
14
- var o = (r, e, t) => new Promise((s, n) => {
15
- var a = (l) => {
12
+ }, k = (r, e) => b(r, C(e));
13
+ var s = (r, e, t) => (u(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
- 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 ? s(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";
35
- 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 = {
30
+ import T from "../../EventBus.mjs";
31
+ import { FileSystemAccessAdapter as P } from "../adapters/FileSystemAccessAdapter.mjs";
32
+ import { FileSnapshotAdapter as D } from "../adapters/FileSnapshotAdapter.mjs";
33
+ import { getNativeDirectoryPicker as F } from "../adapters/fsaTypes.mjs";
34
+ import { LOCAL_FILES_FUNCTION_CODES as W } from "../constants.mjs";
35
+ import { getDownloadUrlAllowlist as L } from "../security/downloadUrlPolicy.mjs";
36
+ import { SensitiveFilePolicy as N } from "../security/sensitiveFiles.mjs";
37
+ import { resolveLimits as E } from "../limits.mjs";
38
+ import { createDefaultSessionFileTransfer as I } from "../transfer/SessionFileTransfer.mjs";
39
+ import { createMemoryStore as q } from "./handleStore.mjs";
40
+ import { watchDirectoryHandle as U } from "./watchDirectory.mjs";
41
+ const m = {
41
42
  mode: "readwrite"
42
43
  };
43
- function U(r) {
44
+ function j(r) {
44
45
  const e = r.replace(/\\/g, "/").trim().replace(/^\.\/+/, "").replace(/\/+$/, "");
45
46
  if (!e || e === ".")
46
47
  return "";
47
48
  if (e.startsWith("/") || e.includes(":") || e.includes("\0"))
48
49
  return;
49
50
  const t = e.split("/");
50
- if (!t.some((s) => !s || s === "." || s === ".."))
51
+ if (!t.some((i) => !i || i === "." || i === ".."))
51
52
  return t.join("/");
52
53
  }
53
54
  function x(r) {
54
55
  return !!r && typeof r == "object" && r.name === "AbortError";
55
56
  }
56
- class X {
57
+ class Z {
57
58
  constructor(e) {
58
- i(this, "applicationId");
59
- i(this, "limits");
60
- i(this, "sensitive");
61
- i(this, "downloadUrlAllowlist");
62
- i(this, "allowWorkspaceWideTextSearch");
63
- i(this, "transfer");
64
- i(this, "fetch");
65
- i(this, "commandRunner");
59
+ s(this, "applicationId");
60
+ s(this, "limits");
61
+ s(this, "sensitive");
62
+ s(this, "downloadUrlAllowlist");
63
+ s(this, "allowWorkspaceWideTextSearch");
64
+ s(this, "transfer");
65
+ s(this, "fetch");
66
+ s(this, "commandRunner");
66
67
  /** 平台技能同步。由 BotClient 在启用本地工作区后挂上。 */
67
- i(this, "platformSkills");
68
- i(this, "browserRuntime");
69
- i(this, "store");
70
- i(this, "picker");
71
- i(this, "event", new b(!0));
72
- i(this, "_status");
73
- i(this, "_displayName");
74
- i(this, "_adapter", null);
75
- i(this, "pendingHandle", null);
76
- i(this, "approvalHandler");
77
- i(this, "activeCalls", 0);
78
- i(this, "commandRunning", !1);
79
- i(this, "inflight", /* @__PURE__ */ new Set());
80
- i(this, "_ready", Promise.resolve());
81
- i(this, "stopWatching", () => {
68
+ s(this, "platformSkills");
69
+ s(this, "browserRuntime");
70
+ s(this, "store");
71
+ s(this, "picker");
72
+ s(this, "event", new T(!0));
73
+ s(this, "_status");
74
+ s(this, "_displayName");
75
+ s(this, "_adapter", null);
76
+ s(this, "pendingHandle", null);
77
+ s(this, "approvalHandler");
78
+ s(this, "activeCalls", 0);
79
+ s(this, "commandRunning", !1);
80
+ s(this, "inflight", /* @__PURE__ */ new Set());
81
+ s(this, "_ready", Promise.resolve());
82
+ s(this, "stopWatching", () => {
82
83
  });
83
- i(this, "pendingChangePaths", /* @__PURE__ */ new Set());
84
- i(this, "changeTimer");
85
- i(this, "refreshAllVisible", !1);
86
- var h, c, l, d, f, g, y, v, w, A;
84
+ s(this, "pendingChangePaths", /* @__PURE__ */ new Set());
85
+ s(this, "changeTimer");
86
+ s(this, "refreshAllVisible", !1);
87
+ /** 选择、关闭会推进它。进行中的 restore 若发现已经变了,就不再覆盖当前目录。 */
88
+ s(this, "epoch", 0);
89
+ var h, l, o, c, f, g, y, v, w, A;
87
90
  const t = (h = e.config) != null ? h : {};
88
- 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;
89
- const s = (d = e.transfer) != null ? d : L(() => {
91
+ this.applicationId = e.applicationId, this.store = (l = e.store) != null ? l : q(), this.picker = e.picker === null ? void 0 : (o = e.picker) != null ? o : F(), this.limits = E(t.limits), this.sensitive = new N(t.sensitiveRulesAppend), this.downloadUrlAllowlist = L(t.downloadUrlAllowlistAppend), this.allowWorkspaceWideTextSearch = !!t.allowWorkspaceWideTextSearch, this.approvalHandler = t.onApprovalRequired;
92
+ const i = (c = e.transfer) != null ? c : I(() => {
90
93
  });
91
94
  this.transfer = {
92
- upload: (g = (f = t.transfer) == null ? void 0 : f.upload) != null ? g : s.upload,
93
- resolveDownload: (v = (y = t.transfer) == null ? void 0 : y.resolveDownload) != null ? v : s.resolveDownload
95
+ upload: (g = (f = t.transfer) == null ? void 0 : f.upload) != null ? g : i.upload,
96
+ resolveDownload: (v = (y = t.transfer) == null ? void 0 : y.resolveDownload) != null ? v : i.resolveDownload
94
97
  };
95
98
  const n = (w = e.fetch) != null ? w : typeof fetch == "function" ? fetch : void 0;
96
99
  this.fetch = n ? n.bind(globalThis) : () => Promise.reject(new Error("fetch is not available")), this.commandRunner = e.commandRunner;
@@ -135,7 +138,7 @@ class X {
135
138
  }));
136
139
  }
137
140
  attachHandle(e) {
138
- 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));
141
+ this.cancelFilesChanged(), this.pendingHandle = e, this._adapter = new P(e), this.setStatus("granted", e.name), this.unwatchDirectory(), this.stopWatching = U(e, (t) => this.notifyFilesChanged(t));
139
142
  }
140
143
  // -------------------------------------------------------------------------
141
144
  // 授权
@@ -155,17 +158,12 @@ class X {
155
158
  } catch (t) {
156
159
  return Promise.reject(t);
157
160
  }
158
- return e.then((t) => o(this, null, function* () {
159
- var n, a;
160
- this.cancelAll();
161
- const s = (a = yield (n = t.queryPermission) == null ? void 0 : n.call(t, u)) != null ? a : "granted";
162
- return yield this.store.set(this.handleKey, {
163
- handle: t,
164
- displayName: t.name,
165
- savedAt: Date.now()
166
- }).catch(() => {
167
- }), s === "denied" ? (this.pendingHandle = t, this._adapter = null, this.setStatus("denied", t.name), !1) : (this.attachHandle(t), !0);
168
- }), (t) => {
161
+ return e.then((t) => (this.epoch += 1, this.cancelAll(), this.attachHandle(t), this.store.set(this.handleKey, {
162
+ handle: t,
163
+ displayName: t.name,
164
+ savedAt: Date.now()
165
+ }).catch(() => {
166
+ }), !0), (t) => {
169
167
  if (x(t))
170
168
  return !1;
171
169
  throw t;
@@ -176,29 +174,51 @@ class X {
176
174
  return this._ready = this.doRestore(), this._ready;
177
175
  }
178
176
  doRestore() {
179
- return o(this, null, function* () {
180
- var s, n, a;
177
+ return d(this, null, function* () {
178
+ var n, a, h;
179
+ const e = this.epoch;
181
180
  if (!this.picker) {
182
181
  this._adapter || this.setStatus("unsupported");
183
182
  return;
184
183
  }
185
- let e;
184
+ let t;
186
185
  try {
187
- e = yield this.store.get(this.handleKey);
188
- } catch (h) {
189
- e = void 0;
186
+ t = yield this.store.get(this.handleKey);
187
+ } catch (l) {
188
+ t = void 0;
190
189
  }
191
- if (!(e != null && e.handle)) {
190
+ if (e !== this.epoch)
191
+ return;
192
+ if (!(t != null && t.handle)) {
192
193
  this.pendingHandle = null, this._adapter = null, this.setStatus("none", void 0);
193
194
  return;
194
195
  }
195
- let t;
196
+ let i;
196
197
  try {
197
- t = (a = yield (n = (s = e.handle).queryPermission) == null ? void 0 : n.call(s, u)) != null ? a : "prompt";
198
- } catch (h) {
199
- t = "prompt";
198
+ i = (h = yield (a = (n = t.handle).queryPermission) == null ? void 0 : a.call(n, m)) != null ? h : "prompt";
199
+ } catch (l) {
200
+ i = "prompt";
200
201
  }
201
- t === "granted" ? this.attachHandle(e.handle) : (this.pendingHandle = e.handle, this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.displayName));
202
+ if (e === this.epoch) {
203
+ if (i === "granted" || i === "prompt" && (yield this.canList(t.handle))) {
204
+ if (e !== this.epoch)
205
+ return;
206
+ this.attachHandle(t.handle);
207
+ return;
208
+ }
209
+ this.pendingHandle = t.handle, this._adapter = null, this.setStatus(i === "denied" ? "denied" : "prompt", t.displayName);
210
+ }
211
+ });
212
+ }
213
+ /** queryPermission 仍可能是 prompt,但选择框留下的句柄其实列得出目录。 */
214
+ canList(e) {
215
+ return d(this, null, function* () {
216
+ const t = new P(e);
217
+ let i;
218
+ const n = new Promise((l) => {
219
+ i = setTimeout(() => l(!1), 2e3);
220
+ }), a = t.listEntries("").then(() => !0).catch(() => !1), h = yield Promise.race([a, n]);
221
+ return clearTimeout(i), h;
202
222
  });
203
223
  }
204
224
  /** 对已保存的目录重新申请权限。必须在用户点击事件中调用。 */
@@ -208,22 +228,36 @@ class X {
208
228
  return Promise.resolve(!1);
209
229
  let t;
210
230
  try {
211
- t = e.requestPermission(u);
212
- } catch (s) {
213
- return Promise.reject(s);
231
+ t = e.requestPermission(m);
232
+ } catch (i) {
233
+ return Promise.reject(i);
214
234
  }
215
- return t.then((s) => s === "granted" ? (this.attachHandle(e), !0) : (this._adapter = null, this.setStatus(s === "denied" ? "denied" : "prompt", e.name), !1));
235
+ return t.then((i) => i === "granted" ? (this.attachHandle(e), !0) : (this._adapter = null, this.setStatus(i === "denied" ? "denied" : "prompt", e.name), !1));
216
236
  }
217
237
  /** 断开工作区并删除持久化句柄。状态立即变为未授权,存储删除在其后完成。 */
218
238
  clear() {
219
- return o(this, null, function* () {
220
- 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(() => {
239
+ return d(this, null, function* () {
240
+ this.epoch += 1, 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(() => {
221
241
  });
222
242
  });
223
243
  }
244
+ /**
245
+ * 用目录选择框返回的文件列表挂上工作区。路径来自 webkitRelativePath,
246
+ * 不依赖目录句柄,选完就能列出文件夹层级。
247
+ */
248
+ attachFileSnapshot(e, t) {
249
+ var h, l, o;
250
+ 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) => ({
251
+ name: c.name,
252
+ webkitRelativePath: c.webkitRelativePath,
253
+ lastModified: c.lastModified,
254
+ blob: c
255
+ }));
256
+ this.attachAdapter(new D(a, n), n);
257
+ }
224
258
  /** 直接挂载一个 Adapter(测试或自定义后端),视为已授权。 */
225
259
  attachAdapter(e, t = e.displayName) {
226
- this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this._adapter = e, this.setStatus("granted", t);
260
+ this.epoch += 1, this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this.pendingHandle = null, this._adapter = e, this.setStatus("granted", t);
227
261
  }
228
262
  /**
229
263
  * 通知目录树重读。`paths` 是相对工作区根的目录;省略时重读当前展开的全部目录。
@@ -235,27 +269,37 @@ class X {
235
269
  this.refreshAllVisible = !0;
236
270
  else
237
271
  for (const t of e) {
238
- const s = U(t);
239
- s === void 0 ? this.refreshAllVisible = !0 : this.pendingChangePaths.add(s);
272
+ const i = j(t);
273
+ i === void 0 ? this.refreshAllVisible = !0 : this.pendingChangePaths.add(i);
240
274
  }
241
275
  clearTimeout(this.changeTimer), this.changeTimer = setTimeout(() => this.flushFilesChanged(), 150);
242
276
  }
243
277
  }
244
278
  /** @internal 工具执行中遇到权限错误时调用,重新检查并更新状态。 */
245
279
  handlePermissionLost() {
246
- return o(this, null, function* () {
280
+ return d(this, null, function* () {
247
281
  const e = this.pendingHandle;
248
282
  if (!(e != null && e.queryPermission))
249
283
  return;
250
- const t = yield e.queryPermission(u).catch(() => "prompt");
251
- t !== "granted" && (this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.name));
284
+ const t = yield e.queryPermission(m).catch(() => "prompt");
285
+ if (t !== "granted") {
286
+ if (t === "prompt" && this._adapter)
287
+ try {
288
+ yield this._adapter.listEntries("");
289
+ return;
290
+ } catch (i) {
291
+ if ((i && typeof i == "object" ? i.name : void 0) !== "NotAllowedError")
292
+ return;
293
+ }
294
+ this.cancelAll(), this.cancelFilesChanged(), this.unwatchDirectory(), this._adapter = null, this.setStatus(t === "denied" ? "denied" : "prompt", e.name);
295
+ }
252
296
  });
253
297
  }
254
298
  // -------------------------------------------------------------------------
255
299
  // 外发提示
256
300
  // -------------------------------------------------------------------------
257
301
  isDisclosureAcknowledged() {
258
- return o(this, null, function* () {
302
+ return d(this, null, function* () {
259
303
  try {
260
304
  return !!(yield this.store.get(this.disclosureKey));
261
305
  } catch (e) {
@@ -264,7 +308,7 @@ class X {
264
308
  });
265
309
  }
266
310
  acknowledgeDisclosure() {
267
- return o(this, null, function* () {
311
+ return d(this, null, function* () {
268
312
  yield this.store.set(this.disclosureKey, !0).catch(() => {
269
313
  });
270
314
  });
@@ -312,14 +356,14 @@ class X {
312
356
  this.inflight.clear(), e.forEach((t) => t.abort());
313
357
  }
314
358
  handshake() {
315
- const e = Object.fromEntries(F.map((t) => [t, !0]));
359
+ const e = Object.fromEntries(W.map((t) => [t, !0]));
316
360
  return {
317
361
  protocolVersion: "1.0",
318
- capabilities: k(m({}, e), {
362
+ capabilities: k(p({}, e), {
319
363
  cancellation: !0,
320
364
  atomicWrite: !0
321
365
  }),
322
- limits: m({}, this.limits)
366
+ limits: p({}, this.limits)
323
367
  };
324
368
  }
325
369
  // -------------------------------------------------------------------------
@@ -341,7 +385,7 @@ class X {
341
385
  }
342
386
  flushFilesChanged() {
343
387
  this.changeTimer = void 0;
344
- const e = this.refreshAllVisible, t = [...this.pendingChangePaths].sort((s, n) => s < n ? -1 : s > n ? 1 : 0);
388
+ const e = this.refreshAllVisible, t = [...this.pendingChangePaths].sort((i, n) => i < n ? -1 : i > n ? 1 : 0);
345
389
  this.pendingChangePaths.clear(), this.refreshAllVisible = !1, this._status === "granted" && this.emit("files:changed", {
346
390
  paths: e ? [] : t
347
391
  });
@@ -350,11 +394,11 @@ class X {
350
394
  emit(e, t) {
351
395
  try {
352
396
  this.event.emit(e, t);
353
- } catch (s) {
354
- console.error(`[LocalWorkspace] ${e} listener failed`, s);
397
+ } catch (i) {
398
+ console.error(`[LocalWorkspace] ${e} listener failed`, i);
355
399
  }
356
400
  }
357
401
  }
358
402
  export {
359
- X as LocalWorkspace
403
+ Z as LocalWorkspace
360
404
  };
@@ -1,4 +1,4 @@
1
- const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.14", t = "glodoncv", i = "aiot bot client js sdk", n = "module", g = !1, o = [
1
+ const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.16", t = "glodoncv", i = "aiot bot client js sdk", n = "module", g = !1, o = [
2
2
  "aiot",
3
3
  "bot",
4
4
  "bot client",
@@ -68,7 +68,7 @@ const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.14", t = "glodoncv",
68
68
  access: "public",
69
69
  registry: "https://registry.npmjs.org/"
70
70
  }, y = {
71
- "@glodon-aiot/apis": "^3.26.0-alpha.14",
71
+ "@glodon-aiot/apis": "^3.26.0-alpha.16",
72
72
  axios: "0.24.0",
73
73
  buffer: "^6.0.3",
74
74
  dayjs: "^1.11.13",