@glodon-aiot/bot-client-sdk 3.26.0-alpha.5 → 3.26.0-alpha.9

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,233 +1,157 @@
1
- var N = Object.defineProperty;
2
- var f = Object.getOwnPropertySymbols;
3
- var O = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable;
4
- var m = (i, t) => {
5
- if (t = Symbol[i])
1
+ var p = Object.defineProperty;
2
+ var m = (r, t) => {
3
+ if (t = Symbol[r])
6
4
  return t;
7
- throw Error("Symbol." + i + " is not defined");
5
+ throw Error("Symbol." + r + " is not defined");
8
6
  };
9
- var u = (i, t, e) => t in i ? N(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e, b = (i, t) => {
10
- for (var e in t || (t = {}))
11
- O.call(t, e) && u(i, e, t[e]);
12
- if (f)
13
- for (var e of f(t))
14
- T.call(t, e) && u(i, e, t[e]);
15
- return i;
16
- };
17
- var p = (i, t, e) => (u(i, typeof t != "symbol" ? t + "" : t, e), e);
18
- var l = (i, t, e) => new Promise((r, o) => {
19
- var s = (n) => {
7
+ var u = (r, t, e) => t in r ? p(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
8
+ var l = (r, t, e) => (u(r, typeof t != "symbol" ? t + "" : t, e), e);
9
+ var n = (r, t, e) => new Promise((s, a) => {
10
+ var o = (i) => {
20
11
  try {
21
- c(e.next(n));
22
- } catch (d) {
23
- o(d);
12
+ y(e.next(i));
13
+ } catch (c) {
14
+ a(c);
24
15
  }
25
- }, a = (n) => {
16
+ }, d = (i) => {
26
17
  try {
27
- c(e.throw(n));
28
- } catch (d) {
29
- o(d);
18
+ y(e.throw(i));
19
+ } catch (c) {
20
+ a(c);
30
21
  }
31
- }, c = (n) => n.done ? r(n.value) : Promise.resolve(n.value).then(s, a);
32
- c((e = e.apply(i, t)).next());
22
+ }, y = (i) => i.done ? s(i.value) : Promise.resolve(i.value).then(o, d);
23
+ y((e = e.apply(r, t)).next());
33
24
  });
34
- var w = (i, t, e) => (t = i[m("asyncIterator")]) ? t.call(i) : (i = i[m("iterator")](), t = {}, e = (r, o) => (o = i[r]) && (t[r] = (s) => new Promise((a, c, n) => (s = o.call(i, s), n = s.done, Promise.resolve(s.value).then((d) => a({ value: d, done: n }), c)))), e("next"), e("return"), t);
35
- import { domError as h, toChunks as L } from "./WorkspaceFileAdapter.mjs";
36
- import { throwIfAborted as A } from "../errors.mjs";
37
- import { SANDBOX_PROTOCOL as C, HOST_CHANNEL as M } from "../host/protocol.mjs";
38
- const x = 3e4;
39
- function B(i) {
40
- let t = "";
41
- for (let r = 0; r < i.length; r += 32768)
42
- t += String.fromCharCode(...i.subarray(r, r + 32768));
43
- return btoa(t);
44
- }
45
- function k(i) {
46
- const t = atob(i), e = new Uint8Array(t.length);
47
- for (let r = 0; r < t.length; r++)
48
- e[r] = t.charCodeAt(r);
49
- return e;
50
- }
51
- function v(i, t) {
52
- const e = i && typeof i == "object" ? i : {}, r = e.kind === "directory" ? "directory" : "file", o = {
25
+ var f = (r, t, e) => (t = r[m("asyncIterator")]) ? t.call(r) : (r = r[m("iterator")](), t = {}, e = (s, a) => (a = r[s]) && (t[s] = (o) => new Promise((d, y, i) => (o = a.call(r, o), i = o.done, Promise.resolve(o.value).then((c) => d({ value: c, done: i }), y)))), e("next"), e("return"), t);
26
+ import { throwIfAborted as g } from "../errors.mjs";
27
+ import { SandboxClient as v } from "../host/sandboxClient.mjs";
28
+ import { SANDBOX_PROTOCOL as A } from "../host/protocol.mjs";
29
+ import { domError as w, toChunks as M } from "./WorkspaceFileAdapter.mjs";
30
+ function x(r, t) {
31
+ const e = r && typeof r == "object" ? r : {}, s = e.kind === "directory" ? "directory" : "file", a = {
53
32
  name: typeof e.name == "string" && e.name ? e.name : t,
54
- kind: r
33
+ kind: s
55
34
  };
56
- return r === "file" && typeof e.size == "number" && (o.size = e.size), typeof e.lastModified == "number" && (o.lastModified = e.lastModified), o;
35
+ return s === "file" && typeof e.size == "number" && (a.size = e.size), typeof e.lastModified == "number" && (a.lastModified = e.lastModified), a;
57
36
  }
58
- class q {
59
- constructor(t, e = "") {
60
- p(this, "displayName");
61
- /** 宿主最近一次 workspace 消息声明的能力。 */
62
- p(this, "capabilities", /* @__PURE__ */ new Set());
63
- /** 宿主随 sandbox-exec 声明的沙箱包版本和协议。 */
64
- p(this, "sandboxVersion");
65
- p(this, "sandboxProtocol");
66
- p(this, "seq", 0);
67
- p(this, "pending", /* @__PURE__ */ new Map());
68
- this.view = t, this.displayName = e;
37
+ class S {
38
+ constructor(t, e = "", s = new v()) {
39
+ l(this, "displayName");
40
+ l(this, "capabilities", /* @__PURE__ */ new Set());
41
+ l(this, "sandboxVersion");
42
+ l(this, "sandboxProtocol");
43
+ l(this, "sandbox");
44
+ this.displayName = e, this.sandbox = s;
69
45
  }
70
46
  setCapabilities(t, e = {}) {
71
- const r = Array.isArray(t) ? t.filter((o) => typeof o == "string") : [];
72
- this.capabilities = new Set(r), this.sandboxVersion = typeof e.version == "string" ? e.version : void 0, this.sandboxProtocol = typeof e.protocol == "number" ? e.protocol : void 0;
47
+ const s = Array.isArray(t) ? t.filter((a) => typeof a == "string") : [];
48
+ this.capabilities = new Set(s), this.sandboxVersion = typeof e.version == "string" ? e.version : void 0;
49
+ }
50
+ applySandbox(t) {
51
+ this.sandboxProtocol = t == null ? void 0 : t.protocol, this.sandbox.apply(t);
73
52
  }
74
- /** 声明了 sandbox-exec 且协议版本与 SDK 一致时才把命令交给宿主。 */
53
+ /** 沙箱已就绪且协议为 2 时,命令走直连。 */
75
54
  get canExec() {
76
- return this.capabilities.has("sandbox-exec") && this.sandboxProtocol === C;
55
+ return this.sandbox.ready;
77
56
  }
78
- requireCapability(t) {
79
- if (!this.capabilities.has(t))
80
- throw h("NotAllowedError", `宿主未声明 ${t}。`);
57
+ get protocolMismatch() {
58
+ return this.sandboxProtocol !== void 0 && this.sandboxProtocol !== A;
81
59
  }
82
- settle(t) {
83
- const e = this.pending.get(t.id);
84
- if (e) {
85
- if (clearTimeout(e.timer), this.pending.delete(t.id), t.ok === !1) {
86
- e.reject(h(t.name || "NotAllowedError", t.message || t.name || "NotAllowedError"));
87
- return;
88
- }
89
- e.resolve(t);
90
- }
91
- }
92
- call(t, e, r, o = x) {
93
- A(r);
94
- const s = `host-${++this.seq}`;
95
- return new Promise((a, c) => {
96
- const n = setTimeout(() => {
97
- this.pending.delete(s), c(h("AbortError"));
98
- }, o), d = () => {
99
- clearTimeout(n), this.pending.delete(s), c(h("AbortError"));
100
- };
101
- r == null || r.addEventListener("abort", d, {
102
- once: !0
103
- }), this.pending.set(s, {
104
- resolve: (E) => {
105
- r == null || r.removeEventListener("abort", d), a(E);
106
- },
107
- reject: c,
108
- timer: n
109
- }), this.view.postMessage(b({
110
- channel: M,
111
- id: s,
112
- op: t
113
- }, e));
114
- });
60
+ unavailableMessage() {
61
+ return this.sandbox.unavailableMessage();
115
62
  }
116
63
  stat(t) {
117
- return l(this, null, function* () {
118
- const e = yield this.call("stat", {
119
- path: t
120
- });
121
- return v(e.entry, t.split("/").pop() || this.displayName);
64
+ return n(this, null, function* () {
65
+ const e = yield this.sandbox.stat(t);
66
+ return x(e.entry, t.split("/").pop() || this.displayName);
122
67
  });
123
68
  }
124
69
  listEntries(t, e) {
125
- return l(this, null, function* () {
126
- const r = yield this.call("list", {
127
- path: t
128
- }, e);
129
- return (Array.isArray(r.entries) ? r.entries : []).map((s) => v(s, ""));
70
+ return n(this, null, function* () {
71
+ const s = yield this.sandbox.list(t, e);
72
+ return (Array.isArray(s.entries) ? s.entries : []).map((o) => x(o, ""));
130
73
  });
131
74
  }
132
- readFile(t) {
133
- return l(this, null, function* () {
134
- var o;
135
- const e = yield this.call("read", {
136
- path: t
137
- }), r = k((o = e.contentBase64) != null ? o : "");
138
- return new Blob([r]);
75
+ readFile(t, e) {
76
+ return n(this, null, function* () {
77
+ var a;
78
+ const s = yield this.sandbox.read(t, (a = e == null ? void 0 : e.offset) != null ? a : 0, e == null ? void 0 : e.maxBytes);
79
+ return new Blob([s]);
139
80
  });
140
81
  }
141
82
  createDirectory(t) {
142
- return l(this, null, function* () {
143
- this.requireCapability("fs-mutate"), yield this.call("mkdir", {
144
- path: t
145
- });
83
+ return n(this, null, function* () {
84
+ yield this.sandbox.mkdir(t);
146
85
  });
147
86
  }
148
- remove(r) {
149
- return l(this, arguments, function* (t, e = {}) {
150
- this.requireCapability("fs-mutate"), yield this.call("remove", {
151
- path: t,
152
- recursive: e.recursive === !0
153
- });
87
+ remove(s) {
88
+ return n(this, arguments, function* (t, e = {}) {
89
+ yield this.sandbox.remove(t, e.recursive === !0);
154
90
  });
155
91
  }
156
92
  move(t, e) {
157
- return l(this, null, function* () {
158
- this.requireCapability("fs-mutate"), yield this.call("move", {
159
- from: t,
160
- to: e
161
- });
93
+ return n(this, null, function* () {
94
+ yield this.sandbox.move(t, e);
162
95
  });
163
96
  }
164
97
  exec(t) {
165
- return l(this, null, function* () {
166
- var o, s, a;
167
- this.requireCapability("sandbox-exec");
168
- const e = (o = t.timeoutMs) != null ? o : x, r = yield this.call("exec", {
169
- protocol: C,
98
+ return n(this, null, function* () {
99
+ const e = yield this.sandbox.exec({
100
+ op: "exec",
170
101
  cwd: t.cwd,
171
102
  command: t.command,
172
103
  argv: t.argv,
173
- timeoutMs: e
174
- }, t.signal, e + 15e3);
175
- if (r.timedOut)
176
- throw h("AbortError", "命令运行超时。");
177
- return {
178
- exitCode: typeof r.exitCode == "number" ? r.exitCode : 1,
179
- stdout: (s = r.stdout) != null ? s : "",
180
- stderr: (a = r.stderr) != null ? a : "",
181
- cwd: typeof r.cwd == "string" ? r.cwd : void 0
182
- };
104
+ timeoutMs: t.timeoutMs
105
+ }, t.signal, t.onOutput);
106
+ if (e.timedOut)
107
+ throw w("AbortError", "命令运行超时。");
108
+ return e;
183
109
  });
184
110
  }
185
- runPython(t, e, r, o) {
186
- return l(this, null, function* () {
187
- var a, c;
188
- const s = yield this.call("run_python", {
111
+ runPython(t, e, s, a) {
112
+ return n(this, null, function* () {
113
+ return this.sandbox.exec({
114
+ op: "run_python",
189
115
  path: t,
190
116
  args: e,
191
- timeoutMs: r
192
- }, o, r + 15e3);
193
- return {
194
- exitCode: typeof s.exitCode == "number" ? s.exitCode : 1,
195
- timedOut: s.timedOut === !0,
196
- stdout: (a = s.stdout) != null ? a : "",
197
- stderr: (c = s.stderr) != null ? c : ""
198
- };
117
+ timeoutMs: s
118
+ }, a);
199
119
  });
200
120
  }
201
- writeFile(o, s) {
202
- return l(this, arguments, function* (t, e, r = {}) {
203
- const a = [];
121
+ writeFile(a, o) {
122
+ return n(this, arguments, function* (t, e, s = {}) {
123
+ if (typeof e == "string") {
124
+ yield this.sandbox.write(t, new TextEncoder().encode(e), s.createDirectories === !0, s.signal);
125
+ return;
126
+ }
127
+ if (e instanceof Uint8Array) {
128
+ yield this.sandbox.write(t, e, s.createDirectories === !0, s.signal);
129
+ return;
130
+ }
131
+ const d = [];
204
132
  try {
205
- for (var E = w(L(e)), U, F, I; U = !(F = yield E.next()).done; U = !1) {
206
- const y = F.value;
207
- A(r.signal), a.push(y);
133
+ for (var N = f(M(e)), z, L, F; z = !(L = yield N.next()).done; z = !1) {
134
+ const b = L.value;
135
+ g(s.signal), d.push(b);
208
136
  }
209
- } catch (F) {
210
- I = [F];
137
+ } catch (L) {
138
+ F = [L];
211
139
  } finally {
212
140
  try {
213
- U && (F = E.return) && (yield F.call(E));
141
+ z && (L = N.return) && (yield L.call(N));
214
142
  } finally {
215
- if (I)
216
- throw I[0];
143
+ if (F)
144
+ throw F[0];
217
145
  }
218
146
  }
219
- const c = a.reduce((y, g) => y + g.byteLength, 0), n = new Uint8Array(c);
220
- let d = 0;
221
- a.forEach((y) => {
222
- n.set(y, d), d += y.byteLength;
223
- }), yield this.call("write", {
224
- path: t,
225
- contentBase64: B(n),
226
- createDirectories: r.createDirectories === !0
227
- }, r.signal);
147
+ const y = d.reduce((b, h) => b + h.byteLength, 0), i = new Uint8Array(y);
148
+ let c = 0;
149
+ d.forEach((b) => {
150
+ i.set(b, c), c += b.byteLength;
151
+ }), yield this.sandbox.write(t, i, s.createDirectories === !0, s.signal);
228
152
  });
229
153
  }
230
154
  }
231
155
  export {
232
- q as HostBridgeAdapter
156
+ S as HostBridgeAdapter
233
157
  };
@@ -1,54 +1,53 @@
1
1
  import { HostBridgeAdapter as f } from "../adapters/HostBridgeAdapter.mjs";
2
- import { getHostWebView as y, HOST_CHANNEL as p, parseHostMessage as m } from "./protocol.mjs";
3
- let t;
4
- function u(s) {
5
- t == null || t(), t = void 0;
6
- const o = y();
7
- if (!o)
2
+ import { getHostWebView as g, HOST_CHANNEL as l, parseHostMessage as y } from "./protocol.mjs";
3
+ function m(o) {
4
+ if (!o || typeof o != "object")
5
+ return;
6
+ const e = o;
7
+ return {
8
+ state: e.state,
9
+ url: typeof e.url == "string" ? e.url : void 0,
10
+ token: typeof e.token == "string" ? e.token : void 0,
11
+ protocol: typeof e.protocol == "number" ? e.protocol : void 0,
12
+ exitCode: typeof e.exitCode == "number" ? e.exitCode : void 0,
13
+ detail: typeof e.detail == "string" ? e.detail : void 0,
14
+ message: typeof e.message == "string" ? e.message : void 0,
15
+ reason: typeof e.reason == "string" ? e.reason : void 0
16
+ };
17
+ }
18
+ let s;
19
+ function v(o) {
20
+ s == null || s(), s = void 0;
21
+ const e = g();
22
+ if (!e)
8
23
  return () => {
9
24
  };
10
- const i = new f(o), d = (g) => {
11
- const e = m(g.data);
12
- if (e) {
13
- if (e.type === "workspace") {
14
- const a = e.granted === !0, r = typeof e.displayName == "string" ? e.displayName : "";
15
- i.setCapabilities(e.capabilities, {
16
- version: e.sandboxVersion,
17
- protocol: e.sandboxProtocol
18
- }), a ? (i.displayName = r || "工程目录", (s.status !== "granted" || s.displayName !== i.displayName) && s.attachAdapter(i, i.displayName)) : s.status === "granted" && s.clear();
25
+ const i = new f(e), d = (p) => {
26
+ const t = y(p.data);
27
+ if (t) {
28
+ if (t.type === "workspace") {
29
+ const n = t.granted === !0, r = typeof t.displayName == "string" ? t.displayName : "";
30
+ i.setCapabilities(t.capabilities, {
31
+ version: t.sandboxVersion
32
+ }), i.applySandbox(m(t.sandbox)), n ? (i.displayName = r || "工程目录", (o.status !== "granted" || o.displayName !== i.displayName) && o.attachAdapter(i, i.displayName)) : o.status === "granted" && o.clear();
19
33
  return;
20
34
  }
21
- if (e.type === "fs-changed") {
22
- const a = Array.isArray(e.paths) ? e.paths.filter((r) => typeof r == "string") : [];
23
- s.notifyFilesChanged(a);
35
+ if (t.type === "fs-changed") {
36
+ const n = Array.isArray(t.paths) ? t.paths.filter((r) => typeof r == "string") : [];
37
+ o.notifyFilesChanged(n);
24
38
  return;
25
39
  }
26
- typeof e.id == "string" && i.settle({
27
- channel: p,
28
- id: e.id,
29
- ok: e.ok !== !1,
30
- name: typeof e.name == "string" ? e.name : void 0,
31
- entries: e.entries,
32
- entry: e.entry,
33
- contentBase64: typeof e.contentBase64 == "string" ? e.contentBase64 : void 0,
34
- exitCode: typeof e.exitCode == "number" ? e.exitCode : void 0,
35
- timedOut: e.timedOut === !0,
36
- stdout: typeof e.stdout == "string" ? e.stdout : void 0,
37
- stderr: typeof e.stderr == "string" ? e.stderr : void 0,
38
- cwd: typeof e.cwd == "string" ? e.cwd : void 0,
39
- message: typeof e.message == "string" ? e.message : void 0
40
- });
41
40
  }
42
41
  };
43
- o.addEventListener("message", d), o.postMessage({
44
- channel: p,
42
+ e.addEventListener("message", d), e.postMessage({
43
+ channel: l,
45
44
  type: "hello"
46
45
  });
47
- const n = () => {
48
- o.removeEventListener("message", d), t === n && (t = void 0);
46
+ const a = () => {
47
+ e.removeEventListener("message", d), s === a && (s = void 0);
49
48
  };
50
- return t = n, n;
49
+ return s = a, a;
51
50
  }
52
51
  export {
53
- u as connectHostBridge
52
+ v as connectHostBridge
54
53
  };
@@ -1,4 +1,4 @@
1
- const n = "glodon-workspace", i = 1;
1
+ const n = "glodon-workspace", i = 2;
2
2
  function s(t) {
3
3
  let e = t;
4
4
  if (typeof e == "string")