@glodon-aiot/bot-client-sdk 3.26.0-alpha.17 → 3.26.0-alpha.19

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.
@@ -2,15 +2,15 @@ var S = Object.defineProperty, b = Object.defineProperties;
2
2
  var C = Object.getOwnPropertyDescriptors;
3
3
  var _ = Object.getOwnPropertySymbols;
4
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) => {
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
- R.call(e, t) && u(r, t, e[t]);
7
+ R.call(e, t) && p(r, t, e[t]);
8
8
  if (_)
9
9
  for (var t of _(e))
10
- H.call(e, t) && u(r, t, e[t]);
10
+ H.call(e, t) && p(r, t, e[t]);
11
11
  return r;
12
12
  }, k = (r, e) => b(r, C(e));
13
- var s = (r, e, t) => (u(r, typeof e != "symbol" ? e + "" : e, t), t);
13
+ var s = (r, e, t) => (p(r, typeof e != "symbol" ? e + "" : e, t), t);
14
14
  var d = (r, e, t) => new Promise((i, n) => {
15
15
  var a = (o) => {
16
16
  try {
@@ -29,8 +29,8 @@ var d = (r, e, t) => new Promise((i, n) => {
29
29
  });
30
30
  import T from "../../EventBus.mjs";
31
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";
32
+ import { FileSnapshotAdapter as F } from "../adapters/FileSnapshotAdapter.mjs";
33
+ import { getNativeDirectoryPicker as D } from "../adapters/fsaTypes.mjs";
34
34
  import { LOCAL_FILES_FUNCTION_CODES as W } from "../constants.mjs";
35
35
  import { getDownloadUrlAllowlist as L } from "../security/downloadUrlPolicy.mjs";
36
36
  import { SensitiveFilePolicy as N } from "../security/sensitiveFiles.mjs";
@@ -38,10 +38,11 @@ import { resolveLimits as E } from "../limits.mjs";
38
38
  import { createDefaultSessionFileTransfer as I } from "../transfer/SessionFileTransfer.mjs";
39
39
  import { createMemoryStore as q } from "./handleStore.mjs";
40
40
  import { watchDirectoryHandle as U } from "./watchDirectory.mjs";
41
+ import { WorkspaceFiles as j } from "./WorkspaceFiles.mjs";
41
42
  const m = {
42
43
  mode: "readwrite"
43
44
  };
44
- function j(r) {
45
+ function x(r) {
45
46
  const e = r.replace(/\\/g, "/").trim().replace(/^\.\/+/, "").replace(/\/+$/, "");
46
47
  if (!e || e === ".")
47
48
  return "";
@@ -51,10 +52,10 @@ function j(r) {
51
52
  if (!t.some((i) => !i || i === "." || i === ".."))
52
53
  return t.join("/");
53
54
  }
54
- function x(r) {
55
+ function K(r) {
55
56
  return !!r && typeof r == "object" && r.name === "AbortError";
56
57
  }
57
- class Z {
58
+ class te {
58
59
  constructor(e) {
59
60
  s(this, "applicationId");
60
61
  s(this, "limits");
@@ -67,6 +68,8 @@ class Z {
67
68
  /** 平台技能同步。由 BotClient 在启用本地工作区后挂上。 */
68
69
  s(this, "platformSkills");
69
70
  s(this, "browserRuntime");
71
+ /** 接入方的文件操作入口。不要使用 `adapter`。 */
72
+ s(this, "files");
70
73
  s(this, "store");
71
74
  s(this, "picker");
72
75
  s(this, "event", new T(!0));
@@ -88,7 +91,7 @@ class Z {
88
91
  s(this, "epoch", 0);
89
92
  var h, l, o, c, f, g, y, v, w, A;
90
93
  const t = (h = e.config) != null ? h : {};
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;
94
+ this.applicationId = e.applicationId, this.store = (l = e.store) != null ? l : q(), this.picker = e.picker === null ? void 0 : (o = e.picker) != null ? o : D(), 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
95
  const i = (c = e.transfer) != null ? c : I(() => {
93
96
  });
94
97
  this.transfer = {
@@ -102,7 +105,7 @@ class Z {
102
105
  enabled: (a == null ? void 0 : a.enabled) !== !1,
103
106
  bashModuleURL: a == null ? void 0 : a.bashModuleURL,
104
107
  pyodideIndexURL: a == null ? void 0 : a.pyodideIndexURL
105
- }, this._status = this.picker ? "none" : "unsupported";
108
+ }, this._status = this.picker ? "none" : "unsupported", this.files = new j(this);
106
109
  }
107
110
  // -------------------------------------------------------------------------
108
111
  // 状态
@@ -121,7 +124,10 @@ class Z {
121
124
  get ready() {
122
125
  return this._ready;
123
126
  }
124
- /** @internal 已授权时可用的 Adapter。 */
127
+ /**
128
+ * @internal 已授权时的磁盘后端,供内置工具使用。
129
+ * 接入方用 `files`:这里的路径必须已规范化,异常里也可能带系统路径。
130
+ */
125
131
  get adapter() {
126
132
  return this._status === "granted" ? this._adapter : null;
127
133
  }
@@ -164,7 +170,7 @@ class Z {
164
170
  savedAt: Date.now()
165
171
  }).catch(() => {
166
172
  }), !0), (t) => {
167
- if (x(t))
173
+ if (K(t))
168
174
  return !1;
169
175
  throw t;
170
176
  });
@@ -253,7 +259,7 @@ class Z {
253
259
  lastModified: c.lastModified,
254
260
  blob: c
255
261
  }));
256
- this.attachAdapter(new D(a, n), n);
262
+ this.attachAdapter(new F(a, n), n);
257
263
  }
258
264
  /** 直接挂载一个 Adapter(测试或自定义后端),视为已授权。 */
259
265
  attachAdapter(e, t = e.displayName) {
@@ -269,7 +275,7 @@ class Z {
269
275
  this.refreshAllVisible = !0;
270
276
  else
271
277
  for (const t of e) {
272
- const i = j(t);
278
+ const i = x(t);
273
279
  i === void 0 ? this.refreshAllVisible = !0 : this.pendingChangePaths.add(i);
274
280
  }
275
281
  clearTimeout(this.changeTimer), this.changeTimer = setTimeout(() => this.flushFilesChanged(), 150);
@@ -359,11 +365,11 @@ class Z {
359
365
  const e = Object.fromEntries(W.map((t) => [t, !0]));
360
366
  return {
361
367
  protocolVersion: "1.0",
362
- capabilities: k(p({}, e), {
368
+ capabilities: k(u({}, e), {
363
369
  cancellation: !0,
364
370
  atomicWrite: !0
365
371
  }),
366
- limits: p({}, this.limits)
372
+ limits: u({}, this.limits)
367
373
  };
368
374
  }
369
375
  // -------------------------------------------------------------------------
@@ -400,5 +406,5 @@ class Z {
400
406
  }
401
407
  }
402
408
  export {
403
- Z as LocalWorkspace
409
+ te as LocalWorkspace
404
410
  };
@@ -0,0 +1,188 @@
1
+ var n = (a, s, t) => new Promise((e, i) => {
2
+ var r = (c) => {
3
+ try {
4
+ l(t.next(c));
5
+ } catch (d) {
6
+ i(d);
7
+ }
8
+ }, o = (c) => {
9
+ try {
10
+ l(t.throw(c));
11
+ } catch (d) {
12
+ i(d);
13
+ }
14
+ }, l = (c) => c.done ? e(c.value) : Promise.resolve(c.value).then(r, o);
15
+ l((t = t.apply(a, s)).next());
16
+ });
17
+ import { ToolError as h, ToolCode as w, toToolError as F, workspaceNotGranted as A } from "../errors.mjs";
18
+ import { parentPath as u } from "../security/pathPolicy.mjs";
19
+ import { SensitiveFilePolicy as k } from "../security/sensitiveFiles.mjs";
20
+ const C = "路径必须在工作区内,不能包含 ..、盘符或空字符。";
21
+ function g() {
22
+ return new h(w.Forbidden, C, {
23
+ reason: "path_outside_workspace"
24
+ });
25
+ }
26
+ function f(a, s) {
27
+ if (a == null || a === "") {
28
+ if (s)
29
+ return "";
30
+ throw new h(w.BadRequest, "缺少参数 path。");
31
+ }
32
+ if (typeof a != "string")
33
+ throw new h(w.BadRequest, "path 必须是字符串。");
34
+ if (a.includes("\0") || /%(2e|2f|5c|00)/i.test(a))
35
+ throw g();
36
+ const t = a.normalize("NFC").replace(/\\/g, "/").trim();
37
+ if (/^[a-zA-Z]:/.test(t))
38
+ throw g();
39
+ const e = [];
40
+ for (const r of t.split("/"))
41
+ if (!(r === "" || r === ".")) {
42
+ if (r === "..")
43
+ throw g();
44
+ e.push(r);
45
+ }
46
+ const i = e.join("/");
47
+ if (!i && !s)
48
+ throw new h(w.BadRequest, "path 不能指向工作区根目录。");
49
+ return i;
50
+ }
51
+ function v(a) {
52
+ return new h(w.NotImplemented, `当前工作区不能${a}。`);
53
+ }
54
+ function p(a, s, t) {
55
+ return {
56
+ name: t.name,
57
+ path: s,
58
+ kind: t.kind,
59
+ size: t.size,
60
+ lastModified: t.lastModified,
61
+ sensitive: t.kind === "file" && a.sensitive.isSensitive(s)
62
+ };
63
+ }
64
+ class D {
65
+ constructor(s) {
66
+ this.host = s;
67
+ }
68
+ stat(s = "") {
69
+ return n(this, null, function* () {
70
+ const t = f(s, !0), e = yield this.run((i) => i.stat(t));
71
+ return p(this.host, t, e);
72
+ });
73
+ }
74
+ list() {
75
+ return n(this, arguments, function* (s = "", t = {}) {
76
+ const e = f(s, !0);
77
+ return (yield this.run((r) => r.listEntries(e, t.signal))).map((r) => {
78
+ const o = e ? `${e}/${r.name}` : r.name;
79
+ return p(this.host, o, r);
80
+ });
81
+ });
82
+ }
83
+ read(s) {
84
+ return n(this, null, function* () {
85
+ const t = f(s, !1);
86
+ return this.assertAccess(t, "read"), this.run((e) => n(this, null, function* () {
87
+ if ((yield e.stat(t)).kind !== "file")
88
+ throw new h(w.BadRequest, "目标是目录,不是文件。");
89
+ return e.readFile(t);
90
+ }));
91
+ });
92
+ }
93
+ readText(s) {
94
+ return n(this, null, function* () {
95
+ return (yield this.read(s)).text();
96
+ });
97
+ }
98
+ write(i, r) {
99
+ return n(this, arguments, function* (s, t, e = {}) {
100
+ const o = f(s, !1);
101
+ this.assertAccess(o, "write"), yield this.run((l) => l.writeFile(o, t, {
102
+ createDirectories: e.createDirectories !== !1,
103
+ signal: e.signal
104
+ })), this.host.notifyFilesChanged([u(o)]);
105
+ });
106
+ }
107
+ append(i, r) {
108
+ return n(this, arguments, function* (s, t, e = {}) {
109
+ const o = f(s, !1);
110
+ this.assertAccess(o, "write");
111
+ const l = typeof t == "string" ? new TextEncoder().encode(t) : t;
112
+ yield this.run((c) => n(this, null, function* () {
113
+ let d = new Uint8Array();
114
+ try {
115
+ if ((yield c.stat(o)).kind !== "file")
116
+ throw new h(w.BadRequest, "目标是目录,不是文件。");
117
+ d = new Uint8Array(yield (yield c.readFile(o)).arrayBuffer());
118
+ } catch (y) {
119
+ if (y instanceof h || F(y).code !== w.NotFound)
120
+ throw y;
121
+ }
122
+ const m = new Uint8Array(d.byteLength + l.byteLength);
123
+ m.set(d), m.set(l, d.byteLength), yield c.writeFile(o, m, {
124
+ createDirectories: !0,
125
+ signal: e.signal
126
+ });
127
+ })), this.host.notifyFilesChanged([u(o)]);
128
+ });
129
+ }
130
+ mkdir(s) {
131
+ return n(this, null, function* () {
132
+ const t = f(s, !0);
133
+ t && (yield this.run((e) => n(this, null, function* () {
134
+ if (!e.createDirectory)
135
+ throw v("创建目录");
136
+ yield e.createDirectory(t);
137
+ })), this.host.notifyFilesChanged([u(t)]));
138
+ });
139
+ }
140
+ remove(e) {
141
+ return n(this, arguments, function* (s, t = {}) {
142
+ const i = f(s, !1);
143
+ this.assertAccess(i, "write"), yield this.run((r) => n(this, null, function* () {
144
+ if (!r.remove)
145
+ throw v("删除");
146
+ yield r.remove(i, t);
147
+ })), this.host.notifyFilesChanged([u(i)]);
148
+ });
149
+ }
150
+ move(s, t) {
151
+ return n(this, null, function* () {
152
+ const e = f(s, !1), i = f(t, !1);
153
+ this.assertAccess(e, "write"), this.assertAccess(i, "write"), yield this.run((r) => n(this, null, function* () {
154
+ if (!r.move)
155
+ throw v("移动");
156
+ yield r.move(e, i);
157
+ })), this.host.notifyFilesChanged([u(e), u(i)]);
158
+ });
159
+ }
160
+ assertAccess(s, t) {
161
+ const e = this.host.sensitive.match(s, t);
162
+ if (e)
163
+ throw new h(w.Forbidden, k.reasonOf(e), {
164
+ reason: "sensitive_file",
165
+ ruleId: e.id
166
+ });
167
+ }
168
+ run(s) {
169
+ return n(this, null, function* () {
170
+ var e;
171
+ const t = this.host.adapter;
172
+ if (!t)
173
+ throw A();
174
+ try {
175
+ return yield s(t);
176
+ } catch (i) {
177
+ if (i instanceof h)
178
+ throw i;
179
+ const r = F(i);
180
+ throw ((e = r.data) == null ? void 0 : e.reason) === "workspace_not_granted" && (yield this.host.handlePermissionLost()), r;
181
+ }
182
+ });
183
+ }
184
+ }
185
+ export {
186
+ D as WorkspaceFiles,
187
+ f as normalizeFilePath
188
+ };
@@ -1,4 +1,4 @@
1
- const e = "@glodon-aiot/bot-client-sdk", s = "3.26.0-alpha.17", 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.19", 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.17", t = "glodoncv",
68
68
  access: "public",
69
69
  registry: "https://registry.npmjs.org/"
70
70
  }, y = {
71
- "@glodon-aiot/apis": "^3.26.0-alpha.17",
71
+ "@glodon-aiot/apis": "^3.26.0-alpha.19",
72
72
  axios: "0.24.0",
73
73
  buffer: "^6.0.3",
74
74
  dayjs: "^1.11.13",
@@ -0,0 +1,49 @@
1
+ var l = (t, e, o) => new Promise((c, n) => {
2
+ var p = (i) => {
3
+ try {
4
+ s(o.next(i));
5
+ } catch (a) {
6
+ n(a);
7
+ }
8
+ }, r = (i) => {
9
+ try {
10
+ s(o.throw(i));
11
+ } catch (a) {
12
+ n(a);
13
+ }
14
+ }, s = (i) => i.done ? c(i.value) : Promise.resolve(i.value).then(p, r);
15
+ s((o = o.apply(t, e)).next());
16
+ });
17
+ import { v4 as u } from "uuid";
18
+ function $(t, e, o) {
19
+ return l(this, null, function* () {
20
+ var s;
21
+ if (!e)
22
+ throw new Error("session is not avalible. id is undefined");
23
+ const c = d(o.name), n = yield t.postUploadTicket({
24
+ files: [c],
25
+ isv: "minio",
26
+ knowledgeId: e,
27
+ type: 1
28
+ }), r = (yield t.uploadMinioFile(n.minio, o)) || ((s = n.minio.fileKeys) == null ? void 0 : s[0]);
29
+ return {
30
+ url: f(n.minio, r),
31
+ name: o.name || c,
32
+ size: o.size
33
+ };
34
+ });
35
+ }
36
+ function d(t) {
37
+ const e = t.split(/[/\\]/).pop() || "", o = /\.([A-Za-z0-9]{1,16})$/.exec(e);
38
+ return o ? `${u()}.${o[1].toLowerCase()}` : u();
39
+ }
40
+ function f(t, e) {
41
+ var n;
42
+ const o = (n = t.endpoints) == null ? void 0 : n[0];
43
+ if (!o || !t.bucket || !e)
44
+ throw new Error("上传没有返回文件地址。");
45
+ return `${t.useSSL ? "https:" : typeof location != "undefined" && location.protocol ? location.protocol : "http:"}//${o}/${t.bucket}/${e}`;
46
+ }
47
+ export {
48
+ $ as uploadSessionFile
49
+ };