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

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 (101) hide show
  1. package/README.md +555 -98
  2. package/dist/es/AgentSession.mjs +231 -167
  3. package/dist/es/BotClient.mjs +188 -136
  4. package/dist/es/Config.mjs +89 -26
  5. package/dist/es/DialogSession.mjs +39 -40
  6. package/dist/es/Session.mjs +83 -63
  7. package/dist/es/api/index.mjs +42 -33
  8. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  9. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  10. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  11. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  12. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  13. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  14. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  15. package/dist/es/builtinTools/command.mjs +56 -0
  16. package/dist/es/builtinTools/constants.mjs +10 -0
  17. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +244 -0
  18. package/dist/es/builtinTools/overview.mjs +77 -0
  19. package/dist/es/builtinTools/paths.mjs +63 -0
  20. package/dist/es/builtinTools/schemas.mjs +216 -0
  21. package/dist/es/builtinTools/tools.mjs +471 -0
  22. package/dist/es/index.mjs +69 -9
  23. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +264 -0
  24. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +157 -0
  25. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +197 -0
  26. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +54 -0
  27. package/dist/es/localFiles/adapters/fsaTypes.mjs +9 -0
  28. package/dist/es/localFiles/constants.mjs +47 -0
  29. package/dist/es/localFiles/createLocalFilesPlugin.mjs +255 -0
  30. package/dist/es/localFiles/errors.mjs +57 -0
  31. package/dist/es/localFiles/host/connectHostBridge.mjs +53 -0
  32. package/dist/es/localFiles/host/protocol.mjs +27 -0
  33. package/dist/es/localFiles/host/sandboxClient.mjs +324 -0
  34. package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
  35. package/dist/es/localFiles/limits.mjs +22 -0
  36. package/dist/es/localFiles/schemas.mjs +312 -0
  37. package/dist/es/localFiles/security/downloadUrlPolicy.mjs +39 -0
  38. package/dist/es/localFiles/security/glob.mjs +28 -0
  39. package/dist/es/localFiles/security/pathPolicy.mjs +59 -0
  40. package/dist/es/localFiles/security/sensitiveFiles.mjs +115 -0
  41. package/dist/es/localFiles/tools/readTools.mjs +333 -0
  42. package/dist/es/localFiles/tools/runtime.mjs +108 -0
  43. package/dist/es/localFiles/tools/transferTools.mjs +228 -0
  44. package/dist/es/localFiles/tools/writeTools.mjs +173 -0
  45. package/dist/es/localFiles/transfer/SessionFileTransfer.mjs +111 -0
  46. package/dist/es/localFiles/validate.mjs +66 -0
  47. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +358 -0
  48. package/dist/es/localFiles/workspace/handleStore.mjs +79 -0
  49. package/dist/es/localFiles/workspace/watchDirectory.mjs +38 -0
  50. package/dist/es/package.json.mjs +38 -29
  51. package/dist/lib/index.js +374 -5
  52. package/dist/src/AgentSession.d.ts +9 -0
  53. package/dist/src/BotClient.d.ts +12 -2
  54. package/dist/src/Config.d.ts +25 -1
  55. package/dist/src/Session.d.ts +9 -1
  56. package/dist/src/api/index.d.ts +3 -1
  57. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  58. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  59. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  60. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  61. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  62. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  63. package/dist/src/builtinTools/command.d.ts +28 -0
  64. package/dist/src/builtinTools/constants.d.ts +17 -0
  65. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  66. package/dist/src/builtinTools/overview.d.ts +5 -0
  67. package/dist/src/builtinTools/paths.d.ts +15 -0
  68. package/dist/src/builtinTools/schemas.d.ts +3 -0
  69. package/dist/src/builtinTools/tools.d.ts +60 -0
  70. package/dist/src/index.d.ts +4 -2
  71. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +20 -0
  72. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +37 -0
  73. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +21 -0
  74. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +40 -0
  75. package/dist/src/localFiles/adapters/fsaTypes.d.ts +41 -0
  76. package/dist/src/localFiles/constants.d.ts +51 -0
  77. package/dist/src/localFiles/createLocalFilesPlugin.d.ts +11 -0
  78. package/dist/src/localFiles/createLocalPythonPlugin.d.ts +12 -0
  79. package/dist/src/localFiles/errors.d.ts +34 -0
  80. package/dist/src/localFiles/host/connectHostBridge.d.ts +6 -0
  81. package/dist/src/localFiles/host/protocol.d.ts +40 -0
  82. package/dist/src/localFiles/host/sandboxClient.d.ts +62 -0
  83. package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
  84. package/dist/src/localFiles/index.d.ts +22 -0
  85. package/dist/src/localFiles/limits.d.ts +3 -0
  86. package/dist/src/localFiles/schemas.d.ts +29 -0
  87. package/dist/src/localFiles/security/downloadUrlPolicy.d.ts +4 -0
  88. package/dist/src/localFiles/security/glob.d.ts +3 -0
  89. package/dist/src/localFiles/security/pathPolicy.d.ts +14 -0
  90. package/dist/src/localFiles/security/sensitiveFiles.d.ts +23 -0
  91. package/dist/src/localFiles/tools/readTools.d.ts +7 -0
  92. package/dist/src/localFiles/tools/runtime.d.ts +43 -0
  93. package/dist/src/localFiles/tools/transferTools.d.ts +4 -0
  94. package/dist/src/localFiles/tools/writeTools.d.ts +4 -0
  95. package/dist/src/localFiles/transfer/SessionFileTransfer.d.ts +40 -0
  96. package/dist/src/localFiles/types.d.ts +319 -0
  97. package/dist/src/localFiles/validate.d.ts +2 -0
  98. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +111 -0
  99. package/dist/src/localFiles/workspace/handleStore.d.ts +11 -0
  100. package/dist/src/localFiles/workspace/watchDirectory.d.ts +7 -0
  101. package/package.json +13 -4
@@ -0,0 +1,228 @@
1
+ var b = (o, e, a) => new Promise((n, l) => {
2
+ var r = (t) => {
3
+ try {
4
+ i(a.next(t));
5
+ } catch (d) {
6
+ l(d);
7
+ }
8
+ }, s = (t) => {
9
+ try {
10
+ i(a.throw(t));
11
+ } catch (d) {
12
+ l(d);
13
+ }
14
+ }, i = (t) => t.done ? n(t.value) : Promise.resolve(t.value).then(r, s);
15
+ i((a = a.apply(o, e)).next());
16
+ }), B = function(o, e) {
17
+ this[0] = o, this[1] = e;
18
+ }, I = (o, e, a) => {
19
+ var n = (s, i, t, d) => {
20
+ try {
21
+ var h = a[s](i), p = (i = h.value) instanceof B, y = h.done;
22
+ Promise.resolve(p ? i[0] : i).then((c) => p ? n(s === "return" ? s : "next", i[1] ? { done: c.done, value: c.value } : c, t, d) : t({ value: c, done: y })).catch((c) => n("throw", c, t, d));
23
+ } catch (c) {
24
+ d(c);
25
+ }
26
+ }, l = (s) => r[s] = (i) => new Promise((t, d) => n(s, i, t, d)), r = {};
27
+ return a = a.apply(o, e), r[Symbol.asyncIterator] = () => r, l("next"), l("throw"), l("return"), r;
28
+ };
29
+ import { ToolError as w, ToolCode as f, throwIfAborted as v } from "../errors.mjs";
30
+ import { isDownloadUrlAllowedByList as q } from "../security/downloadUrlPolicy.mjs";
31
+ import { basename as L } from "../security/glob.mjs";
32
+ import { normalizeWorkspacePath as N } from "../security/pathPolicy.mjs";
33
+ import { assertNotSensitive as R, requireFile as U, tooLarge as F, statOrNull as k, sha256 as z, readBytes as C } from "./runtime.mjs";
34
+ function A() {
35
+ return new w(f.Forbidden, "下载地址不在允许范围内:只允许 https 协议且域名在白名单中的地址。", {
36
+ reason: "url_not_allowed"
37
+ });
38
+ }
39
+ const $ = (o, e) => b(void 0, null, function* () {
40
+ var p, y;
41
+ const a = N(o.path);
42
+ if (((p = o.target) != null ? p : "session") !== "session")
43
+ throw new w(f.NotImplemented, "目前只支持上传到当前会话(target: session)。");
44
+ R(e, a, "read");
45
+ const l = yield U(e.adapter, a), r = (y = l.size) != null ? y : 0;
46
+ if (r > e.limits.maxTransferBytes)
47
+ throw F(e.limits.maxTransferBytes);
48
+ const s = L(a);
49
+ yield e.requestApproval(a, {
50
+ type: "upload",
51
+ name: s,
52
+ size: r,
53
+ target: "session"
54
+ }), e.progress({
55
+ stage: "reading",
56
+ loaded: 0,
57
+ total: r,
58
+ path: a
59
+ });
60
+ const i = yield e.adapter.readFile(a);
61
+ v(e.signal);
62
+ const t = new File([i], s, {
63
+ type: o.mimeHint || i.type || "application/octet-stream",
64
+ lastModified: l.lastModified
65
+ }), {
66
+ fileId: d,
67
+ url: h
68
+ } = yield e.transfer.upload(t, e.context, ({
69
+ loaded: c,
70
+ total: u
71
+ }) => e.progress({
72
+ stage: "uploading",
73
+ loaded: c,
74
+ total: u,
75
+ path: a
76
+ }));
77
+ return v(e.signal), e.progress({
78
+ stage: "done",
79
+ loaded: r,
80
+ total: r,
81
+ path: a
82
+ }), {
83
+ data: {
84
+ fileId: d,
85
+ name: s,
86
+ size: r,
87
+ url: h
88
+ }
89
+ };
90
+ });
91
+ function P(o) {
92
+ const e = o;
93
+ if (!e || typeof e != "object")
94
+ throw new w(f.BadRequest, "缺少参数 source。");
95
+ if (e.type === "sessionFile" || e.type === "platformFile") {
96
+ if (typeof e.fileId != "string" || !e.fileId)
97
+ throw new w(f.BadRequest, "source.fileId 必须是非空字符串。");
98
+ return {
99
+ type: e.type,
100
+ fileId: e.fileId
101
+ };
102
+ }
103
+ if (e.type === "url") {
104
+ if (typeof e.url != "string" || !e.url)
105
+ throw new w(f.BadRequest, "source.url 必须是非空字符串。");
106
+ return {
107
+ type: "url",
108
+ url: e.url
109
+ };
110
+ }
111
+ throw new w(f.BadRequest, "source.type 必须是 sessionFile、platformFile 或 url 之一。");
112
+ }
113
+ function E(o) {
114
+ return o.type === "sessionFile" ? "会话文件" : o.type === "platformFile" ? "平台文件" : new URL(o.url).hostname;
115
+ }
116
+ function D(o, e, a, n, l) {
117
+ return I(this, null, function* () {
118
+ const r = o.getReader();
119
+ let s = 0;
120
+ try {
121
+ for (; ; ) {
122
+ v(e.signal);
123
+ const {
124
+ done: i,
125
+ value: t
126
+ } = yield new B(r.read());
127
+ if (i)
128
+ break;
129
+ if (s += t.byteLength, s > e.limits.maxTransferBytes)
130
+ throw F(e.limits.maxTransferBytes, "下载内容");
131
+ l.push(t), e.progress({
132
+ stage: "downloading",
133
+ loaded: s,
134
+ total: n,
135
+ path: a
136
+ }), yield t;
137
+ }
138
+ } finally {
139
+ r.cancel().catch(() => {
140
+ });
141
+ }
142
+ });
143
+ }
144
+ const G = (o, e) => b(void 0, null, function* () {
145
+ var x, T, m;
146
+ const a = N(o.destPath, {
147
+ field: "destPath"
148
+ }), n = P(o.source);
149
+ if (n.type === "url" && !q(n.url, e.downloadUrlAllowlist))
150
+ throw A();
151
+ R(e, a, "write");
152
+ const l = yield k(e.adapter, a);
153
+ if ((l == null ? void 0 : l.kind) === "directory")
154
+ throw new w(f.BadRequest, "目标是目录,不能写入文件。");
155
+ if (l && !o.overwrite)
156
+ throw new w(f.Conflict, "目标文件已存在。如需覆盖,请设置 overwrite: true。");
157
+ const r = () => b(void 0, null, function* () {
158
+ if (!l || !o.expectedHash)
159
+ return;
160
+ const g = yield z(yield C(yield e.adapter.readFile(a)));
161
+ if (g !== o.expectedHash)
162
+ throw new w(f.Conflict, "expectedHash 与目标文件当前内容不一致。", {
163
+ currentHash: g
164
+ });
165
+ });
166
+ yield r();
167
+ const s = n.type === "url" ? {
168
+ url: n.url
169
+ } : yield e.transfer.resolveDownload(n, e.context);
170
+ if (s.size !== void 0 && s.size > e.limits.maxTransferBytes)
171
+ throw F(e.limits.maxTransferBytes);
172
+ const i = {
173
+ type: "download",
174
+ source: {
175
+ type: n.type,
176
+ label: E(n)
177
+ },
178
+ name: (x = s.name) != null ? x : L(a),
179
+ size: s.size,
180
+ overwrite: !!l
181
+ };
182
+ yield e.requestApproval(a, i), yield r();
183
+ let t;
184
+ try {
185
+ t = yield e.fetch(s.url, {
186
+ headers: s.headers,
187
+ credentials: "omit",
188
+ redirect: "follow",
189
+ signal: e.signal
190
+ });
191
+ } catch (g) {
192
+ throw v(e.signal), new w(f.Internal, "下载失败,无法连接到文件地址。");
193
+ }
194
+ if (n.type === "url" && t.url && !q(t.url, e.downloadUrlAllowlist))
195
+ throw (T = t.body) == null || T.cancel().catch(() => {
196
+ }), A();
197
+ if (!t.ok || !t.body)
198
+ throw (m = t.body) == null || m.cancel().catch(() => {
199
+ }), new w(t.status === 404 ? f.NotFound : f.Internal, `下载失败(HTTP ${t.status})。`);
200
+ const d = Number(t.headers.get("content-length")), h = Number.isFinite(d) && d > 0 ? d : void 0;
201
+ if (h !== void 0 && h > e.limits.maxTransferBytes)
202
+ throw t.body.cancel().catch(() => {
203
+ }), F(e.limits.maxTransferBytes);
204
+ const p = [];
205
+ yield e.adapter.writeFile(a, D(t.body, e, a, h, p), {
206
+ signal: e.signal
207
+ });
208
+ const y = p.reduce((g, H) => g + H.byteLength, 0), c = new Uint8Array(y);
209
+ let u = 0;
210
+ return p.forEach((g) => {
211
+ c.set(g, u), u += g.byteLength;
212
+ }), e.progress({
213
+ stage: "done",
214
+ loaded: y,
215
+ total: y,
216
+ path: a
217
+ }), {
218
+ data: {
219
+ path: a,
220
+ size: y,
221
+ hash: yield z(c)
222
+ }
223
+ };
224
+ });
225
+ export {
226
+ G as downloadFile,
227
+ $ as uploadFile
228
+ };
@@ -0,0 +1,173 @@
1
+ var C = Object.defineProperty, $ = Object.defineProperties;
2
+ var k = Object.getOwnPropertyDescriptors;
3
+ var q = Object.getOwnPropertySymbols;
4
+ var v = Object.prototype.hasOwnProperty, z = Object.prototype.propertyIsEnumerable;
5
+ var A = (t, e, a) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: a }) : t[e] = a, H = (t, e) => {
6
+ for (var a in e || (e = {}))
7
+ v.call(e, a) && A(t, a, e[a]);
8
+ if (q)
9
+ for (var a of q(e))
10
+ z.call(e, a) && A(t, a, e[a]);
11
+ return t;
12
+ }, W = (t, e) => $(t, k(e));
13
+ var h = (t, e, a) => new Promise((r, n) => {
14
+ var s = (o) => {
15
+ try {
16
+ d(a.next(o));
17
+ } catch (c) {
18
+ n(c);
19
+ }
20
+ }, f = (o) => {
21
+ try {
22
+ d(a.throw(o));
23
+ } catch (c) {
24
+ n(c);
25
+ }
26
+ }, d = (o) => o.done ? r(o.value) : Promise.resolve(o.value).then(s, f);
27
+ d((a = a.apply(t, e)).next());
28
+ });
29
+ import { ToolError as l, ToolCode as w } from "../errors.mjs";
30
+ import { normalizeWorkspacePath as F, parentPath as N } from "../security/pathPolicy.mjs";
31
+ import { assertNotSensitive as D, hasUtf8Bom as P, decodeUtf8Strict as S, tooLarge as B, statOrNull as T, requireFile as _, readBytes as j, sha256 as O } from "./runtime.mjs";
32
+ const u = new Uint8Array([239, 187, 191]);
33
+ function y(t, e) {
34
+ return new l(w.Conflict, e, {
35
+ currentHash: t
36
+ });
37
+ }
38
+ function b(t, e) {
39
+ return h(this, null, function* () {
40
+ var n;
41
+ if (((n = (yield _(t.adapter, e)).size) != null ? n : 0) > t.limits.maxWriteBytes)
42
+ throw B(t.limits.maxWriteBytes, "目标文件");
43
+ const r = yield j(yield t.adapter.readFile(e));
44
+ return {
45
+ bytes: r,
46
+ hash: yield O(r)
47
+ };
48
+ });
49
+ }
50
+ function R(t, e, a) {
51
+ return h(this, null, function* () {
52
+ const {
53
+ hash: r
54
+ } = yield b(t, e);
55
+ if (r !== a)
56
+ throw y(r, "文件在确认期间已被修改,本次写入已取消。请重新读取后再修改。");
57
+ });
58
+ }
59
+ function m(t, e) {
60
+ const a = new TextEncoder().encode(t);
61
+ if (!e)
62
+ return a;
63
+ const r = new Uint8Array(u.byteLength + a.byteLength);
64
+ return r.set(u, 0), r.set(a, u.byteLength), r;
65
+ }
66
+ function M(t, e) {
67
+ let a = 0, r = t.indexOf(e);
68
+ for (; r >= 0; )
69
+ a++, r = t.indexOf(e, r + e.length);
70
+ return a;
71
+ }
72
+ function E(t, e) {
73
+ return !e || t.includes(`\r
74
+ `) ? t : t.replace(/\n/g, `\r
75
+ `);
76
+ }
77
+ function U(t, e, a, r = !1) {
78
+ return h(this, null, function* () {
79
+ return t.progress({
80
+ stage: "writing",
81
+ loaded: 0,
82
+ total: a.byteLength,
83
+ path: e
84
+ }), yield t.adapter.writeFile(e, a, {
85
+ createDirectories: r,
86
+ signal: t.signal
87
+ }), {
88
+ path: e,
89
+ hash: yield O(a),
90
+ size: a.byteLength
91
+ };
92
+ });
93
+ }
94
+ const Q = (t, e) => h(void 0, null, function* () {
95
+ const a = F(t.path);
96
+ if (D(e, a, "write"), !t.edits.length)
97
+ throw new l(w.BadRequest, "参数 edits 不能为空。");
98
+ const {
99
+ bytes: r,
100
+ hash: n
101
+ } = yield b(e, a);
102
+ if (n !== t.expectedHash)
103
+ throw y(n, "expectedHash 与文件当前内容不一致,请重新读取文件后再修改。");
104
+ const s = P(r), f = S(s ? r.subarray(3) : r), d = f.includes(`\r
105
+ `);
106
+ let o = f;
107
+ t.edits.forEach((i, p) => {
108
+ if (typeof (i == null ? void 0 : i.oldText) != "string" || !i.oldText || typeof i.newText != "string")
109
+ throw new l(w.BadRequest, `第 ${p + 1} 条修改无效:oldText 必须是非空字符串,newText 必须是字符串。`);
110
+ const x = E(i.oldText, d), L = E(i.newText, d), g = M(o, x);
111
+ if (g === 0)
112
+ throw new l(w.Conflict, `第 ${p + 1} 条修改的 oldText 在文件中不存在,请重新读取文件确认当前内容。`);
113
+ if (g > 1 && !i.replaceAll)
114
+ throw new l(w.BadRequest, `第 ${p + 1} 条修改的 oldText 在文件中出现了 ${g} 次,请补充上下文使其唯一,或设置 replaceAll。`);
115
+ o = i.replaceAll ? o.split(x).join(L) : o.replace(x, () => L);
116
+ });
117
+ const c = m(o, s);
118
+ if (c.byteLength > e.limits.maxWriteBytes)
119
+ throw B(e.limits.maxWriteBytes, "修改后的内容");
120
+ return yield e.requestApproval(a, {
121
+ type: "text",
122
+ before: f,
123
+ after: o,
124
+ created: !1
125
+ }), yield R(e, a, n), {
126
+ data: yield U(e, a, c)
127
+ };
128
+ }), V = (t, e) => h(void 0, null, function* () {
129
+ const a = F(t.path);
130
+ D(e, a, "write");
131
+ const r = yield T(e.adapter, a);
132
+ if ((r == null ? void 0 : r.kind) === "directory")
133
+ throw new l(w.BadRequest, "目标是目录,不能写入文件。");
134
+ let n, s;
135
+ if (r) {
136
+ const o = yield b(e, a);
137
+ if (s = o.hash, !t.expectedHash)
138
+ throw y(s, "目标文件已存在,覆盖前需要提供 expectedHash(可通过 read_file 或 stat_file 获取)。");
139
+ if (t.expectedHash !== s)
140
+ throw y(s, "expectedHash 与文件当前内容不一致,请重新读取文件后再写入。");
141
+ n = new TextDecoder("utf-8").decode(o.bytes);
142
+ } else if (!t.createDirectories) {
143
+ const o = N(a), c = o ? yield T(e.adapter, o) : {
144
+ kind: "directory"
145
+ };
146
+ if (!c)
147
+ throw new l(w.NotFound, "父目录不存在,可设置 createDirectories 自动创建。");
148
+ if (c.kind !== "directory")
149
+ throw new l(w.BadRequest, "父路径不是目录。");
150
+ }
151
+ const f = new TextEncoder().encode(t.content);
152
+ if (f.byteLength > e.limits.maxWriteBytes)
153
+ throw B(e.limits.maxWriteBytes, "写入内容");
154
+ if (yield e.requestApproval(a, {
155
+ type: "text",
156
+ before: n,
157
+ after: t.content,
158
+ created: !r
159
+ }), s)
160
+ yield R(e, a, s);
161
+ else if (yield T(e.adapter, a))
162
+ throw new l(w.Conflict, "目标文件在确认期间被创建,本次写入已取消。请重新检查后再写入。");
163
+ const d = yield U(e, a, f, t.createDirectories);
164
+ return {
165
+ data: W(H({}, d), {
166
+ created: !r
167
+ })
168
+ };
169
+ });
170
+ export {
171
+ Q as applyPatch,
172
+ V as writeFile
173
+ };
@@ -0,0 +1,111 @@
1
+ var h = (u, c, l) => new Promise((r, f) => {
2
+ var i = (e) => {
3
+ try {
4
+ t(l.next(e));
5
+ } catch (d) {
6
+ f(d);
7
+ }
8
+ }, a = (e) => {
9
+ try {
10
+ t(l.throw(e));
11
+ } catch (d) {
12
+ f(d);
13
+ }
14
+ }, t = (e) => e.done ? r(e.value) : Promise.resolve(e.value).then(i, a);
15
+ t((l = l.apply(u, c)).next());
16
+ });
17
+ import { ToolError as n, ToolCode as o } from "../errors.mjs";
18
+ function w() {
19
+ return new n(o.Internal, "平台文件服务请求失败,请稍后重试。");
20
+ }
21
+ function F(u, c = () => {
22
+ }) {
23
+ const l = () => {
24
+ const i = u();
25
+ if (!i)
26
+ throw w();
27
+ return i;
28
+ }, r = () => {
29
+ const i = c();
30
+ if (!i)
31
+ throw w();
32
+ return i;
33
+ };
34
+ return {
35
+ upload(i, a, t) {
36
+ return h(this, null, function* () {
37
+ if (!a.sessionId)
38
+ throw new n(o.BadRequest, "当前没有可用的会话,无法上传文件。");
39
+ try {
40
+ t == null || t({
41
+ loaded: 0,
42
+ total: i.size
43
+ });
44
+ const e = yield r().postFile(i, (d, p) => t == null ? void 0 : t({
45
+ loaded: d,
46
+ total: p
47
+ }));
48
+ if (!(e != null && e.id) || !e.url)
49
+ throw w();
50
+ return t == null || t({
51
+ loaded: i.size,
52
+ total: i.size
53
+ }), {
54
+ fileId: e.id,
55
+ url: e.url
56
+ };
57
+ } catch (e) {
58
+ throw e instanceof n ? e : w();
59
+ }
60
+ });
61
+ },
62
+ resolveDownload(i, a) {
63
+ return h(this, null, function* () {
64
+ if (i.type === "platformFile")
65
+ return I(r(), i.fileId);
66
+ if (!a.sessionId)
67
+ throw new n(o.BadRequest, "当前没有可用的会话,无法下载会话文件。");
68
+ let t;
69
+ try {
70
+ t = yield l().getApplicationIdSessionFile(a.applicationId, a.sessionId, i.fileId);
71
+ } catch (e) {
72
+ throw w();
73
+ }
74
+ if (!(t != null && t.url))
75
+ throw new n(o.NotFound, "会话文件不存在或暂不可下载。");
76
+ return {
77
+ url: t.url,
78
+ name: t.name,
79
+ size: t.size
80
+ };
81
+ });
82
+ }
83
+ };
84
+ }
85
+ function I(u, c) {
86
+ return h(this, null, function* () {
87
+ let l;
88
+ try {
89
+ l = yield u.getFile(c);
90
+ } catch (f) {
91
+ throw w();
92
+ }
93
+ if (!(l != null && l.url))
94
+ throw new n(o.NotFound, "平台文件不存在或暂不可下载。");
95
+ let r;
96
+ try {
97
+ r = new URL(l.url);
98
+ } catch (f) {
99
+ throw new n(o.Internal, "平台返回的下载地址无效。");
100
+ }
101
+ if (r.protocol !== "https:")
102
+ throw new n(o.Internal, "平台返回的下载地址无效。");
103
+ return {
104
+ url: l.url,
105
+ size: l.bytes
106
+ };
107
+ });
108
+ }
109
+ export {
110
+ F as createDefaultSessionFileTransfer
111
+ };
@@ -0,0 +1,66 @@
1
+ var c = Object.defineProperty, j = Object.defineProperties;
2
+ var L = Object.getOwnPropertyDescriptors;
3
+ var $ = Object.getOwnPropertySymbols;
4
+ var p = Object.prototype.hasOwnProperty, A = Object.prototype.propertyIsEnumerable;
5
+ var g = (i, n, r) => n in i ? c(i, n, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[n] = r, k = (i, n) => {
6
+ for (var r in n || (n = {}))
7
+ p.call(n, r) && g(i, r, n[r]);
8
+ if ($)
9
+ for (var r of $(n))
10
+ A.call(n, r) && g(i, r, n[r]);
11
+ return i;
12
+ }, x = (i, n) => j(i, L(n));
13
+ import { ToolError as I, ToolCode as O } from "./errors.mjs";
14
+ function o(i, n) {
15
+ throw new I(O.BadRequest, i ? `参数 ${i} ${n}` : `参数${n}`);
16
+ }
17
+ function w(i, n) {
18
+ return typeof i != "string" ? i : (n.type === "integer" || n.type === "number") && /^-?\d+(\.\d+)?$/.test(i.trim()) ? Number(i) : n.type === "boolean" && (i === "true" || i === "false") ? i === "true" : i;
19
+ }
20
+ function y(i, n, r) {
21
+ switch (i = w(i, n), n.type) {
22
+ case "string":
23
+ typeof i != "string" && o(r, "必须是字符串。"), n.minLength !== void 0 && i.length < n.minLength && o(r, "不能为空。"), n.maxLength !== void 0 && i.length > n.maxLength && o(r, `不能超过 ${n.maxLength} 字。`);
24
+ break;
25
+ case "integer":
26
+ (typeof i != "number" || !Number.isInteger(i)) && o(r, "必须是整数。");
27
+ break;
28
+ case "number":
29
+ (typeof i != "number" || !Number.isFinite(i)) && o(r, "必须是数字。");
30
+ break;
31
+ case "boolean":
32
+ typeof i != "boolean" && o(r, "必须是布尔值。");
33
+ break;
34
+ case "array":
35
+ if (Array.isArray(i) || o(r, "必须是数组。"), n.minItems !== void 0 && i.length < n.minItems && o(r, "不能为空。"), n.items) {
36
+ const e = n.items;
37
+ return i.map((f, m) => y(f, e, `${r}[${m}]`));
38
+ }
39
+ break;
40
+ case "object":
41
+ return s(i, n, r);
42
+ }
43
+ return n.enum && !n.enum.includes(i) && o(r, `必须是 ${n.enum.join("、")} 之一。`), typeof i == "number" && (n.minimum !== void 0 && i < n.minimum && o(r, `不能小于 ${n.minimum}。`), n.maximum !== void 0 && i > n.maximum && o(r, `不能大于 ${n.maximum}。`)), i;
44
+ }
45
+ function s(i, n, r) {
46
+ var m, d;
47
+ (!i || typeof i != "object" || Array.isArray(i)) && o(r, "必须是对象。");
48
+ const e = i, f = {};
49
+ for (const t of (m = n.required) != null ? m : [])
50
+ (e[t] === void 0 || e[t] === null) && o(r ? `${r}.${t}` : t, "是必填项。");
51
+ for (const [t, b] of Object.entries((d = n.properties) != null ? d : {})) {
52
+ if (e[t] === void 0 || e[t] === null)
53
+ continue;
54
+ const u = r ? `${r}.${t}` : t;
55
+ f[t] = b.oneOf ? (y(e[t], x(k({}, b), {
56
+ oneOf: void 0
57
+ }), u), e[t]) : y(e[t], b, u);
58
+ }
59
+ return f;
60
+ }
61
+ function q(i, n) {
62
+ return s(i, n, "");
63
+ }
64
+ export {
65
+ q as validateToolInput
66
+ };