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

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 (96) hide show
  1. package/README.md +572 -98
  2. package/dist/es/AgentSession.mjs +230 -165
  3. package/dist/es/BotClient.mjs +186 -136
  4. package/dist/es/Config.mjs +89 -26
  5. package/dist/es/Session.mjs +12 -0
  6. package/dist/es/api/index.mjs +42 -33
  7. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  8. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  9. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  10. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  11. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  12. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  13. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  14. package/dist/es/builtinTools/command.mjs +56 -0
  15. package/dist/es/builtinTools/constants.mjs +10 -0
  16. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +236 -0
  17. package/dist/es/builtinTools/overview.mjs +77 -0
  18. package/dist/es/builtinTools/paths.mjs +63 -0
  19. package/dist/es/builtinTools/schemas.mjs +216 -0
  20. package/dist/es/builtinTools/tools.mjs +471 -0
  21. package/dist/es/index.mjs +69 -9
  22. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +264 -0
  23. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +233 -0
  24. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +197 -0
  25. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +54 -0
  26. package/dist/es/localFiles/adapters/fsaTypes.mjs +9 -0
  27. package/dist/es/localFiles/constants.mjs +47 -0
  28. package/dist/es/localFiles/createLocalFilesPlugin.mjs +255 -0
  29. package/dist/es/localFiles/errors.mjs +57 -0
  30. package/dist/es/localFiles/host/connectHostBridge.mjs +54 -0
  31. package/dist/es/localFiles/host/protocol.mjs +27 -0
  32. package/dist/es/localFiles/limits.mjs +22 -0
  33. package/dist/es/localFiles/schemas.mjs +312 -0
  34. package/dist/es/localFiles/security/downloadUrlPolicy.mjs +39 -0
  35. package/dist/es/localFiles/security/glob.mjs +28 -0
  36. package/dist/es/localFiles/security/pathPolicy.mjs +59 -0
  37. package/dist/es/localFiles/security/sensitiveFiles.mjs +115 -0
  38. package/dist/es/localFiles/tools/readTools.mjs +333 -0
  39. package/dist/es/localFiles/tools/runtime.mjs +108 -0
  40. package/dist/es/localFiles/tools/transferTools.mjs +228 -0
  41. package/dist/es/localFiles/tools/writeTools.mjs +173 -0
  42. package/dist/es/localFiles/transfer/SessionFileTransfer.mjs +111 -0
  43. package/dist/es/localFiles/validate.mjs +66 -0
  44. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +358 -0
  45. package/dist/es/localFiles/workspace/handleStore.mjs +79 -0
  46. package/dist/es/localFiles/workspace/watchDirectory.mjs +38 -0
  47. package/dist/es/package.json.mjs +38 -29
  48. package/dist/lib/index.js +373 -5
  49. package/dist/src/AgentSession.d.ts +9 -0
  50. package/dist/src/BotClient.d.ts +12 -2
  51. package/dist/src/Config.d.ts +22 -0
  52. package/dist/src/Session.d.ts +5 -1
  53. package/dist/src/api/index.d.ts +3 -1
  54. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  55. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  56. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  57. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  58. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  59. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  60. package/dist/src/builtinTools/command.d.ts +26 -0
  61. package/dist/src/builtinTools/constants.d.ts +17 -0
  62. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  63. package/dist/src/builtinTools/overview.d.ts +5 -0
  64. package/dist/src/builtinTools/paths.d.ts +15 -0
  65. package/dist/src/builtinTools/schemas.d.ts +3 -0
  66. package/dist/src/builtinTools/tools.d.ts +60 -0
  67. package/dist/src/index.d.ts +4 -2
  68. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +20 -0
  69. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +43 -0
  70. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +21 -0
  71. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +40 -0
  72. package/dist/src/localFiles/adapters/fsaTypes.d.ts +41 -0
  73. package/dist/src/localFiles/constants.d.ts +51 -0
  74. package/dist/src/localFiles/createLocalFilesPlugin.d.ts +11 -0
  75. package/dist/src/localFiles/createLocalPythonPlugin.d.ts +12 -0
  76. package/dist/src/localFiles/errors.d.ts +34 -0
  77. package/dist/src/localFiles/host/connectHostBridge.d.ts +6 -0
  78. package/dist/src/localFiles/host/protocol.d.ts +46 -0
  79. package/dist/src/localFiles/index.d.ts +22 -0
  80. package/dist/src/localFiles/limits.d.ts +3 -0
  81. package/dist/src/localFiles/schemas.d.ts +29 -0
  82. package/dist/src/localFiles/security/downloadUrlPolicy.d.ts +4 -0
  83. package/dist/src/localFiles/security/glob.d.ts +3 -0
  84. package/dist/src/localFiles/security/pathPolicy.d.ts +14 -0
  85. package/dist/src/localFiles/security/sensitiveFiles.d.ts +23 -0
  86. package/dist/src/localFiles/tools/readTools.d.ts +7 -0
  87. package/dist/src/localFiles/tools/runtime.d.ts +43 -0
  88. package/dist/src/localFiles/tools/transferTools.d.ts +4 -0
  89. package/dist/src/localFiles/tools/writeTools.d.ts +4 -0
  90. package/dist/src/localFiles/transfer/SessionFileTransfer.d.ts +40 -0
  91. package/dist/src/localFiles/types.d.ts +319 -0
  92. package/dist/src/localFiles/validate.d.ts +2 -0
  93. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +111 -0
  94. package/dist/src/localFiles/workspace/handleStore.d.ts +11 -0
  95. package/dist/src/localFiles/workspace/watchDirectory.d.ts +7 -0
  96. package/package.json +13 -4
@@ -0,0 +1,197 @@
1
+ var M = Object.defineProperty;
2
+ var g = (n, r) => {
3
+ if (r = Symbol[n])
4
+ return r;
5
+ throw Error("Symbol." + n + " is not defined");
6
+ };
7
+ var N = (n, r, e) => r in n ? M(n, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[r] = e;
8
+ var k = (n, r, e) => (N(n, typeof r != "symbol" ? r + "" : r, e), e);
9
+ var h = (n, r, e) => new Promise((o, t) => {
10
+ var i = (c) => {
11
+ try {
12
+ s(e.next(c));
13
+ } catch (l) {
14
+ t(l);
15
+ }
16
+ }, d = (c) => {
17
+ try {
18
+ s(e.throw(c));
19
+ } catch (l) {
20
+ t(l);
21
+ }
22
+ }, s = (c) => c.done ? o(c.value) : Promise.resolve(c.value).then(i, d);
23
+ s((e = e.apply(n, r)).next());
24
+ });
25
+ var E = (n, r, e) => (r = n[g("asyncIterator")]) ? r.call(n) : (n = n[g("iterator")](), r = {}, e = (o, t) => (t = n[o]) && (r[o] = (i) => new Promise((d, s, c) => (i = t.call(n, i), c = i.done, Promise.resolve(i.value).then((l) => d({ value: l, done: c }), s)))), e("next"), e("return"), r);
26
+ import { domError as a, toChunks as b, isSameOrInside as I } from "./WorkspaceFileAdapter.mjs";
27
+ import { throwIfAborted as p } from "../errors.mjs";
28
+ import { splitPath as f } from "../security/pathPolicy.mjs";
29
+ function D() {
30
+ return {
31
+ kind: "directory",
32
+ children: /* @__PURE__ */ new Map()
33
+ };
34
+ }
35
+ class x {
36
+ constructor(r = {}, e = "memory-workspace") {
37
+ k(this, "root", D());
38
+ this.displayName = e, Object.entries(r).forEach(([o, t]) => this.seed(o, t));
39
+ }
40
+ /**
41
+ * 直接写入文件(含中间目录),用于构造测试数据或模拟外部修改。
42
+ * 以 `/` 结尾的路径表示创建空目录。
43
+ */
44
+ seed(r, e = "", o = Date.now()) {
45
+ const t = r.split("/").filter(Boolean);
46
+ if (r.endsWith("/")) {
47
+ this.resolveDirectory(t, !0);
48
+ return;
49
+ }
50
+ const i = t.pop();
51
+ if (!i)
52
+ return;
53
+ const d = this.resolveDirectory(t, !0), s = typeof e == "string" ? new TextEncoder().encode(e) : e;
54
+ d.children.set(i, {
55
+ kind: "file",
56
+ data: s,
57
+ lastModified: o
58
+ });
59
+ }
60
+ resolveDirectory(r, e = !1) {
61
+ let o = this.root;
62
+ for (const t of r) {
63
+ let i = o.children.get(t);
64
+ if (!i) {
65
+ if (!e)
66
+ throw a("NotFoundError");
67
+ i = D(), o.children.set(t, i);
68
+ }
69
+ if (i.kind !== "directory")
70
+ throw a("TypeMismatchError");
71
+ o = i;
72
+ }
73
+ return o;
74
+ }
75
+ resolveNode(r) {
76
+ const e = f(r), o = e.pop();
77
+ if (o === void 0)
78
+ return this.root;
79
+ const t = this.resolveDirectory(e).children.get(o);
80
+ if (!t)
81
+ throw a("NotFoundError");
82
+ return t;
83
+ }
84
+ stat(r) {
85
+ return h(this, null, function* () {
86
+ var t;
87
+ const e = this.resolveNode(r), o = (t = f(r).pop()) != null ? t : this.displayName;
88
+ return e.kind === "directory" ? {
89
+ name: o,
90
+ kind: "directory"
91
+ } : {
92
+ name: o,
93
+ kind: "file",
94
+ size: e.data.byteLength,
95
+ lastModified: e.lastModified
96
+ };
97
+ });
98
+ }
99
+ listEntries(r, e) {
100
+ return h(this, null, function* () {
101
+ p(e);
102
+ const o = this.resolveDirectory(f(r));
103
+ return Array.from(o.children.entries()).map(([t, i]) => i.kind === "directory" ? {
104
+ name: t,
105
+ kind: "directory"
106
+ } : {
107
+ name: t,
108
+ kind: "file",
109
+ size: i.data.byteLength,
110
+ lastModified: i.lastModified
111
+ });
112
+ });
113
+ }
114
+ readFile(r) {
115
+ return h(this, null, function* () {
116
+ const e = this.resolveNode(r);
117
+ if (e.kind !== "file")
118
+ throw a("TypeMismatchError");
119
+ return new Blob([e.data]);
120
+ });
121
+ }
122
+ writeFile(t, i) {
123
+ return h(this, arguments, function* (r, e, o = {}) {
124
+ const d = f(r), s = d.pop();
125
+ if (!s)
126
+ throw a("TypeMismatchError");
127
+ const c = this.resolveDirectory(d, o.createDirectories), l = c.children.get(s);
128
+ if ((l == null ? void 0 : l.kind) === "directory")
129
+ throw a("TypeMismatchError");
130
+ const y = [];
131
+ let u = 0;
132
+ try {
133
+ for (var O = E(b(e)), W, j, C; W = !(j = yield O.next()).done; W = !1) {
134
+ const m = j.value;
135
+ p(o.signal), y.push(m), u += m.byteLength;
136
+ }
137
+ } catch (j) {
138
+ C = [j];
139
+ } finally {
140
+ try {
141
+ W && (j = O.return) && (yield j.call(O));
142
+ } finally {
143
+ if (C)
144
+ throw C[0];
145
+ }
146
+ }
147
+ p(o.signal);
148
+ const v = new Uint8Array(u);
149
+ let w = 0;
150
+ y.forEach((m) => {
151
+ v.set(m, w), w += m.byteLength;
152
+ }), c.children.set(s, {
153
+ kind: "file",
154
+ data: v,
155
+ lastModified: Date.now()
156
+ });
157
+ });
158
+ }
159
+ createDirectory(r) {
160
+ return h(this, null, function* () {
161
+ this.resolveDirectory(f(r), !0);
162
+ });
163
+ }
164
+ remove(o) {
165
+ return h(this, arguments, function* (r, e = {}) {
166
+ const t = f(r), i = t.pop();
167
+ if (i === void 0)
168
+ throw a("InvalidModificationError");
169
+ const d = this.resolveDirectory(t), s = d.children.get(i);
170
+ if (!s)
171
+ throw a("NotFoundError");
172
+ if (s.kind === "directory" && s.children.size > 0 && !e.recursive)
173
+ throw a("InvalidModificationError");
174
+ d.children.delete(i);
175
+ });
176
+ }
177
+ move(r, e) {
178
+ return h(this, null, function* () {
179
+ const o = f(r), t = f(e), i = o.pop(), d = t.pop();
180
+ if (i === void 0 || d === void 0)
181
+ throw a("InvalidModificationError");
182
+ const s = this.resolveDirectory(o), c = s.children.get(i);
183
+ if (!c)
184
+ throw a("NotFoundError");
185
+ const l = this.resolveDirectory(t);
186
+ if (I(r, e))
187
+ throw a("InvalidModificationError");
188
+ const y = l.children.get(d);
189
+ if (y && (y.kind === "directory" || c.kind === "directory"))
190
+ throw a("InvalidModificationError");
191
+ s.children.delete(i), l.children.set(d, c);
192
+ });
193
+ }
194
+ }
195
+ export {
196
+ x as MemoryWorkspaceAdapter
197
+ };
@@ -0,0 +1,54 @@
1
+ var a = (n, e) => {
2
+ if (e = Symbol[n])
3
+ return e;
4
+ throw Error("Symbol." + n + " is not defined");
5
+ };
6
+ var x = function(n, e) {
7
+ this[0] = n, this[1] = e;
8
+ }, E = (n, e, y) => {
9
+ var d = (t, f, o, l) => {
10
+ try {
11
+ var c = y[t](f), p = (f = c.value) instanceof x, u = c.done;
12
+ Promise.resolve(p ? f[0] : f).then((s) => p ? d(t === "return" ? t : "next", f[1] ? { done: s.done, value: s.value } : s, o, l) : o({ value: s, done: u })).catch((s) => d("throw", s, o, l));
13
+ } catch (s) {
14
+ l(s);
15
+ }
16
+ }, r = (t) => i[t] = (f) => new Promise((o, l) => d(t, f, o, l)), i = {};
17
+ return y = y.apply(n, e), i[Symbol.asyncIterator] = () => i, r("next"), r("throw"), r("return"), i;
18
+ }, h = (n) => {
19
+ var e = n[a("asyncIterator")], y = !1, d, r = {};
20
+ return e == null ? (e = n[a("iterator")](), d = (i) => r[i] = (t) => e[i](t)) : (e = e.call(n), d = (i) => r[i] = (t) => {
21
+ if (y) {
22
+ if (y = !1, i === "throw")
23
+ throw t;
24
+ return t;
25
+ }
26
+ return y = !0, {
27
+ done: !1,
28
+ value: new x(new Promise((f) => {
29
+ var o = e[i](t);
30
+ if (!(o instanceof Object))
31
+ throw TypeError("Object expected");
32
+ f(o);
33
+ }), 1)
34
+ };
35
+ }), r[a("iterator")] = () => r, d("next"), "throw" in e ? d("throw") : r.throw = (i) => {
36
+ throw i;
37
+ }, "return" in e && d("return"), r;
38
+ };
39
+ function O(n, e) {
40
+ return e === n || e.startsWith(`${n}/`);
41
+ }
42
+ function g(n, e = n) {
43
+ return new DOMException(e, n);
44
+ }
45
+ function k(n) {
46
+ return E(this, null, function* () {
47
+ typeof n == "string" ? yield new TextEncoder().encode(n) : n instanceof Uint8Array ? yield n : yield* h(n);
48
+ });
49
+ }
50
+ export {
51
+ g as domError,
52
+ O as isSameOrInside,
53
+ k as toChunks
54
+ };
@@ -0,0 +1,9 @@
1
+ function e() {
2
+ if (typeof window == "undefined")
3
+ return;
4
+ const i = window.showDirectoryPicker;
5
+ return typeof i == "function" ? i.bind(window) : void 0;
6
+ }
7
+ export {
8
+ e as getNativeDirectoryPicker
9
+ };
@@ -0,0 +1,47 @@
1
+ const s = ["list_directory", "read_file", "stat_file", "search_files", "search_text", "apply_patch", "write_file", "upload_file", "download_file"], _ = "builtin_tools", t = {
2
+ dev: _,
3
+ test: _,
4
+ prod: _,
5
+ local: _
6
+ }, E = t, c = "run_python", I = t, n = 2e4, O = 12e4, L = 16, S = "current-workspace", o = 1024, e = 1024 * 1024, T = 50, A = Object.freeze({
7
+ maxReadBytes: 1 * e,
8
+ maxWriteBytes: 5 * e,
9
+ maxSearchResults: 200,
10
+ maxConcurrentRequests: 4,
11
+ maxTransferBytes: T * e,
12
+ toolTimeoutMs: 3e4,
13
+ searchTimeoutMs: 6e4,
14
+ transferTimeoutMs: 10 * 6e4,
15
+ approvalTimeoutMs: 5 * 6e4
16
+ }), N = 200, C = 1e3, a = 8, l = 16, R = 2e3, r = 1 * e, i = 500, D = 3, F = 8 * o, M = Object.freeze(["glodon.com"]), U = Object.freeze([".git", "node_modules", ".svn", ".idea", ".vscode"]), d = Object.freeze(["~$*", ".~lock*", "*.tmp"]), f = ["apply_patch", "write_file", "upload_file", "download_file"], p = ["search_files", "search_text"], H = ["upload_file", "download_file"];
17
+ export {
18
+ F as BINARY_SNIFF_BYTES,
19
+ _ as BUILTIN_TOOLS_PLUGIN_CODE,
20
+ t as BUILTIN_TOOLS_PLUGIN_CODES,
21
+ M as DEFAULT_DOWNLOAD_URL_ALLOWLIST,
22
+ U as DEFAULT_IGNORED_DIRECTORIES,
23
+ d as DEFAULT_IGNORED_FILE_PATTERNS,
24
+ A as DEFAULT_LIMITS,
25
+ o as KiB,
26
+ N as LIST_DIRECTORY_DEFAULT_LIMIT,
27
+ C as LIST_DIRECTORY_MAX_LIMIT,
28
+ s as LOCAL_FILES_FUNCTION_CODES,
29
+ E as LOCAL_FILES_PLUGIN_CODES,
30
+ c as LOCAL_PYTHON_FUNCTION_CODE,
31
+ I as LOCAL_PYTHON_PLUGIN_CODES,
32
+ e as MiB,
33
+ n as PYTHON_DEFAULT_TIMEOUT_MS,
34
+ L as PYTHON_MAX_ARGS,
35
+ O as PYTHON_MAX_TIMEOUT_MS,
36
+ a as SEARCH_DEFAULT_DEPTH,
37
+ p as SEARCH_FUNCTION_CODES,
38
+ l as SEARCH_MAX_DEPTH,
39
+ D as TEXT_SEARCH_MAX_CONTEXT_LINES,
40
+ R as TEXT_SEARCH_MAX_FILES,
41
+ r as TEXT_SEARCH_MAX_FILE_BYTES,
42
+ i as TEXT_SEARCH_MAX_LINE_CHARS,
43
+ T as TRANSFER_FILE_SIZE_LIMIT_MB,
44
+ H as TRANSFER_FUNCTION_CODES,
45
+ S as WORKSPACE_GRANT_ID,
46
+ f as WRITE_FUNCTION_CODES
47
+ };
@@ -0,0 +1,255 @@
1
+ var J = Object.defineProperty, B = Object.defineProperties;
2
+ var K = Object.getOwnPropertyDescriptors;
3
+ var G = Object.getOwnPropertySymbols;
4
+ var z = Object.prototype.hasOwnProperty, Q = Object.prototype.propertyIsEnumerable;
5
+ var E = (e, t, r) => t in e ? J(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, u = (e, t) => {
6
+ for (var r in t || (t = {}))
7
+ z.call(t, r) && E(e, r, t[r]);
8
+ if (G)
9
+ for (var r of G(t))
10
+ Q.call(t, r) && E(e, r, t[r]);
11
+ return e;
12
+ }, f = (e, t) => B(e, K(t));
13
+ var _ = (e, t, r) => (E(e, typeof t != "symbol" ? t + "" : t, r), r);
14
+ var F = (e, t, r) => new Promise((d, s) => {
15
+ var o = (a) => {
16
+ try {
17
+ i(r.next(a));
18
+ } catch (c) {
19
+ s(c);
20
+ }
21
+ }, n = (a) => {
22
+ try {
23
+ i(r.throw(a));
24
+ } catch (c) {
25
+ s(c);
26
+ }
27
+ }, i = (a) => a.done ? d(a.value) : Promise.resolve(a.value).then(o, n);
28
+ i((r = r.apply(e, t)).next());
29
+ });
30
+ import { v4 as U } from "uuid";
31
+ import { LOCAL_FILES_FUNCTION_CODES as V, TRANSFER_FUNCTION_CODES as X, SEARCH_FUNCTION_CODES as Y, WORKSPACE_GRANT_ID as Z } from "./constants.mjs";
32
+ import { cancelledError as g, workspaceNotGranted as $, ToolError as h, ToolCode as m, toToolError as k } from "./errors.mjs";
33
+ import { LOCAL_FILE_TOOL_SCHEMAS as ee } from "./schemas.mjs";
34
+ import { assertGrantId as te } from "./security/pathPolicy.mjs";
35
+ import { listDirectory as re, readFile as ie, statFile as ae, searchFiles as oe, searchText as ne } from "./tools/readTools.mjs";
36
+ import { uploadFile as se, downloadFile as le } from "./tools/transferTools.mjs";
37
+ import { applyPatch as de, writeFile as ce } from "./tools/writeTools.mjs";
38
+ import { validateToolInput as ue } from "./validate.mjs";
39
+ const me = {
40
+ list_directory: re,
41
+ read_file: ie,
42
+ stat_file: ae,
43
+ search_files: oe,
44
+ search_text: ne,
45
+ apply_patch: de,
46
+ write_file: ce,
47
+ upload_file: se,
48
+ download_file: le
49
+ };
50
+ class he {
51
+ constructor(t, r) {
52
+ _(this, "remaining");
53
+ _(this, "startedAt", 0);
54
+ _(this, "handle");
55
+ this.onTimeout = r, this.remaining = t;
56
+ }
57
+ resume() {
58
+ this.handle !== void 0 || this.remaining <= 0 || (this.startedAt = Date.now(), this.handle = setTimeout(() => {
59
+ this.handle = void 0, this.remaining = 0, this.onTimeout();
60
+ }, this.remaining));
61
+ }
62
+ pause() {
63
+ this.handle !== void 0 && (clearTimeout(this.handle), this.handle = void 0, this.remaining -= Date.now() - this.startedAt);
64
+ }
65
+ clear() {
66
+ this.pause(), this.remaining = 0;
67
+ }
68
+ }
69
+ function fe(e, t) {
70
+ return X.includes(t) ? e.limits.transferTimeoutMs : Y.includes(t) ? e.limits.searchTimeoutMs : e.limits.toolTimeoutMs;
71
+ }
72
+ function ge(e) {
73
+ if (typeof e != "string")
74
+ return e;
75
+ try {
76
+ return JSON.parse(e);
77
+ } catch (t) {
78
+ throw new h(m.BadRequest, "参数不是合法的 JSON 对象。");
79
+ }
80
+ }
81
+ function pe(e) {
82
+ if (!e || typeof e != "object" || Array.isArray(e))
83
+ return;
84
+ const t = e;
85
+ return (t.grantId === void 0 || t.grantId === null || t.grantId === "") && (t.grantId = Z), t.grantId;
86
+ }
87
+ function W(e) {
88
+ return new Promise((t, r) => {
89
+ e.aborted && r(g()), e.addEventListener("abort", () => r(g()), {
90
+ once: !0
91
+ });
92
+ });
93
+ }
94
+ function ve(e) {
95
+ return u({
96
+ code: e.code,
97
+ message: e.message
98
+ }, e.data ? {
99
+ data: e.data
100
+ } : {});
101
+ }
102
+ function Ie(e, t, r, d, s) {
103
+ return F(this, null, function* () {
104
+ var N, S, q, P, C, D, R, x;
105
+ const o = {
106
+ requestId: s.requestId || U(),
107
+ applicationId: s.applicationId || e.applicationId,
108
+ sessionId: s.sessionId || "",
109
+ pluginCode: s.pluginCode,
110
+ functionCode: t,
111
+ signal: s.signal
112
+ }, n = {
113
+ requestId: o.requestId,
114
+ sessionId: o.sessionId,
115
+ functionCode: t
116
+ }, i = new AbortController();
117
+ let a = !1;
118
+ const c = new he(fe(e, t), () => {
119
+ a = !0, i.abort();
120
+ }), v = () => i.abort();
121
+ (N = o.signal) == null || N.addEventListener("abort", v, {
122
+ once: !0
123
+ }), (S = o.signal) != null && S.aborted && i.abort();
124
+ let p = null, I, L = !1;
125
+ try {
126
+ const b = ge(r), l = pe(b);
127
+ l != null && te(l);
128
+ const w = ue(b, ee[t].parameters), M = (q = w.path) != null ? q : w.destPath;
129
+ if (typeof M == "string" && (n.path = M), yield e.ready, i.signal.aborted)
130
+ throw g();
131
+ const y = e.adapter;
132
+ if (!y)
133
+ throw $();
134
+ if (p = e.acquireSlot(), !p)
135
+ throw new h(m.TooManyRequests, "同时进行的文件操作过多,请稍后重试。");
136
+ I = e.track(i), L = !0, e.emit("tool:start", u({}, n)), c.resume();
137
+ const H = {
138
+ adapter: y,
139
+ workspaceName: (P = e.displayName) != null ? P : y.displayName,
140
+ limits: e.limits,
141
+ sensitive: e.sensitive,
142
+ downloadUrlAllowlist: e.downloadUrlAllowlist,
143
+ allowWorkspaceWideTextSearch: e.allowWorkspaceWideTextSearch,
144
+ transfer: e.transfer,
145
+ fetch: e.fetch,
146
+ context: f(u({}, o), {
147
+ signal: i.signal
148
+ }),
149
+ signal: i.signal,
150
+ requestApproval: (O, j) => be(e, {
151
+ approvalPath: O,
152
+ preview: j,
153
+ context: o,
154
+ functionCode: t,
155
+ signal: i.signal,
156
+ timer: c,
157
+ base: n
158
+ }),
159
+ progress: (O) => e.emit("tool:progress", u(u({}, n), O))
160
+ }, T = yield Promise.race([me[t](w, H), W(i.signal)]), A = {
161
+ code: m.Ok,
162
+ message: "ok",
163
+ data: T.data
164
+ };
165
+ T.truncated && (A.truncated = !0), (C = T.warnings) != null && C.length && (A.warnings = T.warnings), e.emit("tool:finish", f(u({}, n), {
166
+ code: m.Ok
167
+ })), d(A);
168
+ } catch (b) {
169
+ let l = k(b);
170
+ a ? l = new h(m.Timeout, "操作超时,已中止。") : i.signal.aborted && l.code !== m.Cancelled && (l = g()), ((D = l.data) == null ? void 0 : D.reason) === "workspace_not_granted" && L && e.handlePermissionLost(), l.code === m.Cancelled ? e.emit("tool:cancelled", u({}, n)) : e.emit("tool:error", f(u({}, n), {
171
+ code: l.code,
172
+ message: l.message,
173
+ reason: (R = l.data) == null ? void 0 : R.reason
174
+ })), d(ve(l));
175
+ } finally {
176
+ c.clear(), (x = o.signal) == null || x.removeEventListener("abort", v), p == null || p(), I == null || I();
177
+ }
178
+ });
179
+ }
180
+ function be(e, t) {
181
+ return F(this, null, function* () {
182
+ var i;
183
+ const r = e.getApprovalHandler();
184
+ if (!r)
185
+ throw new h(m.Forbidden, "当前环境未提供操作确认能力,无法执行写入或传输操作。", {
186
+ reason: "approval_unavailable"
187
+ });
188
+ if (t.signal.aborted)
189
+ throw g();
190
+ const d = new AbortController(), s = () => d.abort();
191
+ t.signal.addEventListener("abort", s, {
192
+ once: !0
193
+ });
194
+ const o = {
195
+ id: U(),
196
+ requestId: t.context.requestId,
197
+ sessionId: t.context.sessionId,
198
+ functionCode: t.functionCode,
199
+ path: t.approvalPath,
200
+ workspaceName: (i = e.displayName) != null ? i : "",
201
+ preview: t.preview,
202
+ signal: d.signal
203
+ };
204
+ t.timer.pause(), e.emit("tool:approval-required", f(u({}, t.base), {
205
+ path: t.approvalPath,
206
+ approvalId: o.id
207
+ }));
208
+ let n;
209
+ try {
210
+ const a = yield Promise.race([Promise.resolve().then(() => r(o)), W(d.signal), new Promise((c) => {
211
+ n = setTimeout(() => c(!1), e.limits.approvalTimeoutMs);
212
+ })]);
213
+ if (t.signal.aborted)
214
+ throw g();
215
+ if (a !== !0)
216
+ throw new h(m.Forbidden, "用户拒绝了本次操作。", {
217
+ reason: "user_rejected"
218
+ });
219
+ } catch (a) {
220
+ throw a instanceof h ? a : new h(m.Forbidden, "操作确认失败,已取消本次操作。", {
221
+ reason: "user_rejected"
222
+ });
223
+ } finally {
224
+ clearTimeout(n), t.signal.removeEventListener("abort", s), d.abort(), t.timer.resume();
225
+ }
226
+ });
227
+ }
228
+ function Se({
229
+ pluginCode: e,
230
+ workspace: t
231
+ }) {
232
+ const r = {};
233
+ return V.forEach((d) => {
234
+ r[d] = (s, o, n) => {
235
+ let i = !1;
236
+ Ie(t, d, s, (c) => {
237
+ if (!i) {
238
+ i = !0;
239
+ try {
240
+ o(c);
241
+ } catch (v) {
242
+ console.error("[LocalFiles] callback failed", v);
243
+ }
244
+ }
245
+ }, f(u({}, n), {
246
+ pluginCode: e
247
+ }));
248
+ };
249
+ }), {
250
+ [e]: r
251
+ };
252
+ }
253
+ export {
254
+ Se as createLocalFilesPlugin
255
+ };
@@ -0,0 +1,57 @@
1
+ var i = Object.defineProperty;
2
+ var E = (r, t, e) => t in r ? i(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var a = (r, t, e) => (E(r, typeof t != "symbol" ? t + "" : t, e), e);
4
+ var f = /* @__PURE__ */ ((r) => (r[r.Ok = 0] = "Ok", r[r.BadRequest = 400] = "BadRequest", r[r.Forbidden = 403] = "Forbidden", r[r.NotFound = 404] = "NotFound", r[r.Timeout = 408] = "Timeout", r[r.Conflict = 409] = "Conflict", r[r.TooLarge = 413] = "TooLarge", r[r.TooManyRequests = 429] = "TooManyRequests", r[r.Cancelled = 499] = "Cancelled", r[r.Internal = 500] = "Internal", r[r.NotImplemented = 501] = "NotImplemented", r))(f || {});
5
+ class n extends Error {
6
+ constructor(e, u, s) {
7
+ super(u);
8
+ a(this, "code");
9
+ a(this, "data");
10
+ this.name = "ToolError", this.code = e, this.data = s;
11
+ }
12
+ }
13
+ const w = "尚未授权工作文件夹,请先在助手中选择或恢复访问工作文件夹。";
14
+ function m() {
15
+ return new n(403, w, {
16
+ reason: "workspace_not_granted"
17
+ });
18
+ }
19
+ function c() {
20
+ return new n(499, "操作已取消。");
21
+ }
22
+ function N(r) {
23
+ if (r != null && r.aborted)
24
+ throw c();
25
+ }
26
+ function o(r) {
27
+ if (r instanceof n)
28
+ return r;
29
+ switch (r && typeof r == "object" && "name" in r ? String(r.name) : "") {
30
+ case "NotFoundError":
31
+ return new n(404, "文件或目录不存在。");
32
+ case "TypeMismatchError":
33
+ return new n(400, "路径类型不匹配:目标是目录而不是文件,或路径中的某一段不是目录。");
34
+ case "NotAllowedError":
35
+ case "SecurityError":
36
+ return m();
37
+ case "AbortError":
38
+ return c();
39
+ case "NoModificationAllowedError":
40
+ case "InvalidModificationError":
41
+ case "InvalidStateError":
42
+ return new n(403, "文件当前不可写,可能正被其他程序占用或为只读。");
43
+ case "QuotaExceededError":
44
+ return new n(413, "磁盘空间不足或超出写入配额。");
45
+ default:
46
+ return new n(500, "本地文件操作失败,请稍后重试。");
47
+ }
48
+ }
49
+ export {
50
+ f as ToolCode,
51
+ n as ToolError,
52
+ w as WORKSPACE_NOT_GRANTED_MESSAGE,
53
+ c as cancelledError,
54
+ N as throwIfAborted,
55
+ o as toToolError,
56
+ m as workspaceNotGranted
57
+ };
@@ -0,0 +1,54 @@
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)
8
+ return () => {
9
+ };
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();
19
+ return;
20
+ }
21
+ if (e.type === "fs-changed") {
22
+ const a = Array.isArray(e.paths) ? e.paths.filter((r) => typeof r == "string") : [];
23
+ s.notifyFilesChanged(a);
24
+ return;
25
+ }
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
+ }
42
+ };
43
+ o.addEventListener("message", d), o.postMessage({
44
+ channel: p,
45
+ type: "hello"
46
+ });
47
+ const n = () => {
48
+ o.removeEventListener("message", d), t === n && (t = void 0);
49
+ };
50
+ return t = n, n;
51
+ }
52
+ export {
53
+ u as connectHostBridge
54
+ };