@withgateway/sdk 0.2.6 → 0.2.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.
Files changed (110) hide show
  1. package/README.md +24 -8
  2. package/dist/cli.js +15468 -11263
  3. package/dist/cli.js.map +1 -1
  4. package/dist/cli.mjs +15233 -11028
  5. package/dist/cli.mjs.map +1 -1
  6. package/dist/files/index.d.mts +1 -1
  7. package/dist/files/index.d.ts +1 -1
  8. package/dist/files/index.js +291 -40
  9. package/dist/files/index.js.map +1 -1
  10. package/dist/files/index.mjs +284 -37
  11. package/dist/files/index.mjs.map +1 -1
  12. package/dist/hooks/index.d.mts +1 -1
  13. package/dist/hooks/index.d.ts +1 -1
  14. package/dist/hooks/index.js +203 -47
  15. package/dist/hooks/index.js.map +1 -1
  16. package/dist/hooks/index.mjs +198 -42
  17. package/dist/hooks/index.mjs.map +1 -1
  18. package/dist/{index-BaKu5ZSU.d.mts → index-C3UzD0G3.d.mts} +75 -1
  19. package/dist/{index-BaKu5ZSU.d.ts → index-C3UzD0G3.d.ts} +75 -1
  20. package/dist/{index-CLkMr6VG.d.ts → index-D-_4Ibcn.d.ts} +36 -4
  21. package/dist/{index-CdbmO4aX.d.mts → index-FfK4Nump.d.mts} +36 -4
  22. package/dist/index.d.mts +3 -3
  23. package/dist/index.d.ts +3 -3
  24. package/dist/index.js +2149 -615
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +2053 -566
  27. package/dist/index.mjs.map +1 -1
  28. package/dist/traces/index.js +268 -31
  29. package/dist/traces/index.js.map +1 -1
  30. package/dist/traces/index.mjs +262 -28
  31. package/dist/traces/index.mjs.map +1 -1
  32. package/dist/tracing/index.js +19 -11
  33. package/dist/tracing/index.js.map +1 -1
  34. package/dist/tracing/index.mjs +19 -11
  35. package/dist/tracing/index.mjs.map +1 -1
  36. package/dist/worlds/index.d.mts +886 -117
  37. package/dist/worlds/index.d.ts +886 -117
  38. package/dist/worlds/index.js +1770 -452
  39. package/dist/worlds/index.js.map +1 -1
  40. package/dist/worlds/index.mjs +1752 -434
  41. package/dist/worlds/index.mjs.map +1 -1
  42. package/package.json +14 -10
  43. package/runtime/gatewaysdk/environment/_toml.py +3 -10
  44. package/runtime/gatewaysdk/environment/_world.py +95 -28
  45. package/runtime/gatewaysdk/environment/runtime.py +10 -6
  46. package/runtime/gatewaysdk/environment/schema/__main__.py +35 -0
  47. package/runtime/gatewaysdk/environment/schema/api.py +19 -23
  48. package/runtime/gatewaysdk/environment/schema/batch.py +68 -14
  49. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +4 -2
  50. package/runtime/gatewaysdk/environment/schema/cast.py +264 -0
  51. package/runtime/gatewaysdk/environment/schema/compiler.py +35 -1
  52. package/runtime/gatewaysdk/environment/schema/conform.py +28 -10
  53. package/runtime/gatewaysdk/environment/schema/connector.py +59 -14
  54. package/runtime/gatewaysdk/environment/schema/connectors/custom/handlers.py +38 -1
  55. package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.toml +3 -1
  56. package/runtime/gatewaysdk/environment/schema/credentials.py +20 -0
  57. package/runtime/gatewaysdk/environment/schema/csv_rows.py +117 -0
  58. package/runtime/gatewaysdk/environment/schema/cycles.py +121 -0
  59. package/runtime/gatewaysdk/environment/schema/data.py +21 -5
  60. package/runtime/gatewaysdk/environment/schema/grading.py +95 -13
  61. package/runtime/gatewaysdk/environment/schema/handler_errors.py +44 -0
  62. package/runtime/gatewaysdk/environment/schema/handler_scan.py +175 -0
  63. package/runtime/gatewaysdk/environment/schema/host.py +68 -115
  64. package/runtime/gatewaysdk/environment/schema/host_requests.py +78 -0
  65. package/runtime/gatewaysdk/environment/schema/host_surfaces.py +12 -0
  66. package/runtime/gatewaysdk/environment/schema/ingest.py +5 -18
  67. package/runtime/gatewaysdk/environment/schema/local.py +7 -10
  68. package/runtime/gatewaysdk/environment/schema/openapi_document.py +322 -0
  69. package/runtime/gatewaysdk/environment/schema/openapi_import.py +382 -0
  70. package/runtime/gatewaysdk/environment/schema/openapi_routes.py +441 -0
  71. package/runtime/gatewaysdk/environment/schema/openapi_schemas.py +629 -0
  72. package/runtime/gatewaysdk/environment/schema/openapi_source.py +210 -0
  73. package/runtime/gatewaysdk/environment/schema/platform.py +110 -299
  74. package/runtime/gatewaysdk/environment/schema/platform_import.py +182 -0
  75. package/runtime/gatewaysdk/environment/schema/platform_query.py +95 -0
  76. package/runtime/gatewaysdk/environment/schema/route_bindings.py +176 -0
  77. package/runtime/gatewaysdk/environment/schema/runtime.py +56 -7
  78. package/runtime/gatewaysdk/environment/schema/scaffold.py +64 -5
  79. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +70 -22
  80. package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +54 -4
  81. package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +27 -2
  82. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +62 -0
  83. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +91 -54
  84. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +146 -146
  85. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +11 -7
  86. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/gotchas.md +30 -0
  87. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/new-world-sandbox.md +43 -0
  88. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/openapi-import.md +90 -0
  89. package/runtime/gatewaysdk/environment/schema/store.py +15 -6
  90. package/runtime/gatewaysdk/environment/schema/store_contract.py +119 -0
  91. package/runtime/gatewaysdk/environment/schema/tasks.py +121 -32
  92. package/runtime/gatewaysdk/environment/schema/tools_world.py +32 -10
  93. package/runtime/gatewaysdk/environment/schema/tree_import.py +228 -0
  94. package/runtime/gatewaysdk/environment/schema/ui.py +18 -2
  95. package/runtime/gatewaysdk/environment/schema/validation.py +35 -0
  96. package/runtime/gatewaysdk/environment/schema/verbs.py +8 -2
  97. package/runtime/gatewaysdk/environment/world.py +42 -3
  98. package/runtime/gatewaysdk/environment/world_session_contract.py +57 -0
  99. package/skills/benchmarks-getting-started/SKILL.md +70 -22
  100. package/skills/connector-schema-authoring/SKILL.md +54 -4
  101. package/skills/connector-schema-authoring/connector-worlds.md +21 -7
  102. package/skills/connector-schema-authoring/world-schema.md +144 -83
  103. package/skills/hooks-getting-started/SKILL.md +27 -2
  104. package/skills/world-data-ingestion/ROWS.md +62 -0
  105. package/skills/world-data-ingestion/SKILL.md +91 -54
  106. package/skills/worlds-getting-started/SKILL.md +146 -146
  107. package/skills/worlds-getting-started/references/cli-mcp-map.md +11 -7
  108. package/skills/worlds-getting-started/references/gotchas.md +30 -0
  109. package/skills/worlds-getting-started/references/new-world-sandbox.md +43 -0
  110. package/skills/worlds-getting-started/references/openapi-import.md +90 -0
@@ -1,15 +1,156 @@
1
1
  import { createRequire as __gatewayCreateRequire } from "module";
2
2
  const require = __gatewayCreateRequire(import.meta.url);
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __esm = (fn, res) => function __init() {
5
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
4
+ var __esm = (fn, res, err) => function __init() {
5
+ if (err) throw err[0];
6
+ try {
7
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
8
+ } catch (e) {
9
+ throw err = [e], e;
10
+ }
6
11
  };
7
12
 
13
+ // src/worlds/transport.ts
14
+ function platformTimeoutMs(env = process.env) {
15
+ const raw = env[PLATFORM_TIMEOUT_ENV];
16
+ if (raw === void 0 || raw === "") return PLATFORM_TIMEOUT_DEFAULT_MS;
17
+ const value = Number(raw);
18
+ if (Number.isInteger(value) && value > 0) return value;
19
+ throw new Error(`${PLATFORM_TIMEOUT_ENV} must be a positive whole number of milliseconds, not \`${raw}\`.`);
20
+ }
21
+ var PLATFORM_TIMEOUT_ENV, PLATFORM_TIMEOUT_DEFAULT_MS, CHECK_HOST, RETRY, CAUSES, UNKNOWN_CAUSE, TransportCause, UnreachableError, PlatformTransport;
22
+ var init_transport = __esm({
23
+ "src/worlds/transport.ts"() {
24
+ "use strict";
25
+ PLATFORM_TIMEOUT_ENV = "GATEWAY_TIMEOUT_MS";
26
+ PLATFORM_TIMEOUT_DEFAULT_MS = 12e4;
27
+ CHECK_HOST = "Check GATEWAY_HOST (or --host) points at a running platform, then retry.";
28
+ RETRY = "Retry; if it repeats, check the network between here and the platform.";
29
+ CAUSES = {
30
+ ECONNREFUSED: { reason: "connection refused", next: CHECK_HOST },
31
+ ENOTFOUND: { reason: "DNS lookup failed", next: CHECK_HOST },
32
+ EAI_AGAIN: { reason: "DNS lookup failed", next: CHECK_HOST },
33
+ ETIMEDOUT: { reason: "connect timed out", next: CHECK_HOST },
34
+ UND_ERR_CONNECT_TIMEOUT: { reason: "connect timed out", next: CHECK_HOST },
35
+ EHOSTUNREACH: { reason: "host unreachable", next: CHECK_HOST },
36
+ ENETUNREACH: { reason: "network unreachable", next: RETRY },
37
+ ECONNRESET: { reason: "connection dropped", next: RETRY },
38
+ UND_ERR_SOCKET: { reason: "connection dropped", next: RETRY },
39
+ EPIPE: { reason: "connection dropped", next: RETRY }
40
+ };
41
+ UNKNOWN_CAUSE = { reason: "network error", next: CHECK_HOST };
42
+ TransportCause = class _TransportCause {
43
+ constructor(reason, detail, next) {
44
+ this.reason = reason;
45
+ this.detail = detail;
46
+ this.next = next;
47
+ }
48
+ reason;
49
+ detail;
50
+ next;
51
+ static timeout(timeoutMs) {
52
+ return new _TransportCause(
53
+ `timed out after ${timeoutMs / 1e3}s`,
54
+ null,
55
+ `Retry; if the platform is slow, raise ${PLATFORM_TIMEOUT_ENV} (milliseconds per request).`
56
+ );
57
+ }
58
+ static of(error) {
59
+ const cause = error instanceof Error && error.cause instanceof Error ? error.cause : void 0;
60
+ const code = cause && "code" in cause && typeof cause.code === "string" ? cause.code : null;
61
+ const message = cause?.message ?? (error instanceof Error ? error.message : String(error));
62
+ const known = code && CAUSES[code] || UNKNOWN_CAUSE;
63
+ return new _TransportCause(known.reason, code ? `${code}: ${message}` : message, known.next);
64
+ }
65
+ describe() {
66
+ return this.detail === null ? this.reason : `${this.reason} (${this.detail})`;
67
+ }
68
+ };
69
+ UnreachableError = class extends Error {
70
+ constructor(host, method, route, failure) {
71
+ super(`${method} ${route}: no answer from ${host}: ${failure.describe()}. ${failure.next}`);
72
+ this.host = host;
73
+ this.method = method;
74
+ this.route = route;
75
+ this.failure = failure;
76
+ this.name = "UnreachableError";
77
+ }
78
+ host;
79
+ method;
80
+ route;
81
+ failure;
82
+ };
83
+ PlatformTransport = class {
84
+ constructor(endpoint, timeoutMs = platformTimeoutMs()) {
85
+ this.endpoint = endpoint;
86
+ this.timeoutMs = timeoutMs;
87
+ }
88
+ endpoint;
89
+ timeoutMs;
90
+ /** A request that legitimately takes longer (a sandbox exec, a world create) waits its own budget; every other one waits the default. */
91
+ budget(ownMs) {
92
+ return Math.max(ownMs ?? 0, this.timeoutMs);
93
+ }
94
+ async send(method, route, body, opts = {}) {
95
+ const timeoutMs = this.budget(opts.timeoutMs);
96
+ const payload = body === void 0 ? {} : { body: JSON.stringify(body) };
97
+ const controller = new AbortController();
98
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
99
+ try {
100
+ const res = await fetch(`${this.endpoint.host}${route}`, {
101
+ method,
102
+ headers: {
103
+ "content-type": "application/json",
104
+ accept: "application/json",
105
+ authorization: "Basic " + Buffer.from(`${this.endpoint.publicKey}:${this.endpoint.secretKey}`).toString("base64")
106
+ },
107
+ ...payload,
108
+ signal: controller.signal
109
+ });
110
+ return { status: res.status, ok: res.ok, text: await res.text() };
111
+ } catch (error) {
112
+ const cause = controller.signal.aborted ? TransportCause.timeout(timeoutMs) : TransportCause.of(error);
113
+ throw new UnreachableError(this.endpoint.host, method, route, cause);
114
+ } finally {
115
+ clearTimeout(timer);
116
+ }
117
+ }
118
+ };
119
+ }
120
+ });
121
+
122
+ // src/worlds/browserReconnect.ts
123
+ var init_browserReconnect = __esm({
124
+ "src/worlds/browserReconnect.ts"() {
125
+ "use strict";
126
+ }
127
+ });
128
+
8
129
  // src/worlds/browser.ts
9
130
  var init_browser = __esm({
10
131
  "src/worlds/browser.ts"() {
11
132
  "use strict";
12
133
  init_session();
134
+ init_browserReconnect();
135
+ }
136
+ });
137
+
138
+ // src/worlds/sessionContract.ts
139
+ var CALL_BODY_LIMIT_BYTES, EXPLORATORY_TASK;
140
+ var init_sessionContract = __esm({
141
+ "src/worlds/sessionContract.ts"() {
142
+ "use strict";
143
+ CALL_BODY_LIMIT_BYTES = 4 * 1024 * 1024;
144
+ EXPLORATORY_TASK = Object.freeze({
145
+ instruction: "exploratory session"
146
+ });
147
+ }
148
+ });
149
+
150
+ // src/worlds/taskRef.ts
151
+ var init_taskRef = __esm({
152
+ "src/worlds/taskRef.ts"() {
153
+ "use strict";
13
154
  }
14
155
  });
15
156
 
@@ -21,12 +162,23 @@ var init_session = __esm({
21
162
  "use strict";
22
163
  init_worlds();
23
164
  init_browser();
165
+ init_sessionContract();
166
+ init_taskRef();
167
+ init_taskRef();
24
168
  READY_TIMEOUT_MS = 10 * 6e4;
25
169
  CALL_TIMEOUT_MS = 3 * 6e4;
26
170
  JUDGED_GRADE_TIMEOUT_MS = 15 * 6e4;
27
171
  }
28
172
  });
29
173
 
174
+ // src/worlds/sessionList.ts
175
+ var init_sessionList = __esm({
176
+ "src/worlds/sessionList.ts"() {
177
+ "use strict";
178
+ init_worlds();
179
+ }
180
+ });
181
+
30
182
  // src/worlds/tasks.ts
31
183
  var init_tasks = __esm({
32
184
  "src/worlds/tasks.ts"() {
@@ -67,10 +219,37 @@ var init_taskRun = __esm({
67
219
  }
68
220
  });
69
221
 
70
- // src/worlds/worldDir.ts
71
- import { createHash } from "crypto";
222
+ // src/worlds/checkoutProvenance.ts
223
+ import { execFileSync } from "child_process";
224
+ var init_checkoutProvenance = __esm({
225
+ "src/worlds/checkoutProvenance.ts"() {
226
+ "use strict";
227
+ }
228
+ });
229
+
230
+ // src/worlds/setRun.ts
231
+ var init_setRun = __esm({
232
+ "src/worlds/setRun.ts"() {
233
+ "use strict";
234
+ init_worlds();
235
+ init_checkoutProvenance();
236
+ init_taskRun();
237
+ }
238
+ });
239
+
240
+ // src/worlds/checkoutTarget.ts
72
241
  import * as fs2 from "fs";
73
242
  import * as path2 from "path";
243
+ var init_checkoutTarget = __esm({
244
+ "src/worlds/checkoutTarget.ts"() {
245
+ "use strict";
246
+ }
247
+ });
248
+
249
+ // src/worlds/worldDir.ts
250
+ import { createHash } from "crypto";
251
+ import * as fs3 from "fs";
252
+ import * as path3 from "path";
74
253
  var CONNECTOR_SPEC, WORLD_SCHEMA_MARKERS, WORLD_ROOT_MARKERS;
75
254
  var init_worldDir = __esm({
76
255
  "src/worlds/worldDir.ts"() {
@@ -81,18 +260,30 @@ var init_worldDir = __esm({
81
260
  }
82
261
  });
83
262
 
263
+ // src/worlds/tar.ts
264
+ import * as fs4 from "fs";
265
+ import * as path4 from "path";
266
+ import { gunzipSync } from "zlib";
267
+ var init_tar = __esm({
268
+ "src/worlds/tar.ts"() {
269
+ "use strict";
270
+ }
271
+ });
272
+
84
273
  // src/worlds/hub.ts
85
274
  import { createHash as createHash2 } from "crypto";
86
- import { gzipSync, gunzipSync } from "zlib";
87
- import * as fs3 from "fs";
88
- import * as path3 from "path";
275
+ import { gzipSync } from "zlib";
276
+ import * as fs5 from "fs";
277
+ import * as path5 from "path";
89
278
  var META_RELPATH;
90
279
  var init_hub = __esm({
91
280
  "src/worlds/hub.ts"() {
92
281
  "use strict";
93
282
  init_worlds();
283
+ init_checkoutTarget();
94
284
  init_worldDir();
95
- META_RELPATH = path3.join(".gateway", ".env-metadata.json");
285
+ init_tar();
286
+ META_RELPATH = path5.join(".gateway", ".env-metadata.json");
96
287
  }
97
288
  });
98
289
 
@@ -105,11 +296,20 @@ var init_validate = __esm({
105
296
  }
106
297
  });
107
298
 
299
+ // src/worlds/sandbox.ts
300
+ var init_sandbox = __esm({
301
+ "src/worlds/sandbox.ts"() {
302
+ "use strict";
303
+ init_worlds();
304
+ }
305
+ });
306
+
108
307
  // src/worlds/describe.ts
109
308
  var init_describe = __esm({
110
309
  "src/worlds/describe.ts"() {
111
310
  "use strict";
112
311
  init_worlds();
312
+ init_sandbox();
113
313
  }
114
314
  });
115
315
 
@@ -187,9 +387,9 @@ var init_migrate = __esm({
187
387
  });
188
388
 
189
389
  // src/worlds/ingest.ts
190
- import * as fs4 from "fs";
390
+ import * as fs6 from "fs";
191
391
  import * as os from "os";
192
- import * as path4 from "path";
392
+ import * as path6 from "path";
193
393
  var init_ingest = __esm({
194
394
  "src/worlds/ingest.ts"() {
195
395
  "use strict";
@@ -197,9 +397,17 @@ var init_ingest = __esm({
197
397
  }
198
398
  });
199
399
 
200
- // src/worlds/sandbox.ts
201
- var init_sandbox = __esm({
202
- "src/worlds/sandbox.ts"() {
400
+ // src/worlds/editSessions.ts
401
+ var init_editSessions = __esm({
402
+ "src/worlds/editSessions.ts"() {
403
+ "use strict";
404
+ init_worlds();
405
+ }
406
+ });
407
+
408
+ // src/worlds/dryRun.ts
409
+ var init_dryRun = __esm({
410
+ "src/worlds/dryRun.ts"() {
203
411
  "use strict";
204
412
  init_worlds();
205
413
  }
@@ -217,30 +425,48 @@ function resolveEnv(options) {
217
425
  }
218
426
  return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
219
427
  }
220
- async function apiRequest(options, method, path6, body) {
221
- const { host, publicKey, secretKey } = resolveEnv(options);
222
- const res = await fetch(`${host}${path6}`, {
223
- method,
224
- headers: {
225
- "content-type": "application/json",
226
- authorization: "Basic " + Buffer.from(`${publicKey}:${secretKey}`).toString("base64")
227
- },
228
- ...body === void 0 ? {} : { body: JSON.stringify(body) }
229
- });
230
- const text = await res.text();
231
- if (!res.ok) throw new ApiError(res.status, method, path6, text.slice(0, 400));
232
- return JSON.parse(text);
428
+ function parseErrorBody(text, limit = 600) {
429
+ const raw = { detail: text.slice(0, limit), code: null };
430
+ let parsed;
431
+ try {
432
+ parsed = JSON.parse(text);
433
+ } catch {
434
+ return raw;
435
+ }
436
+ if (parsed === null || typeof parsed !== "object") return raw;
437
+ const { message, code } = parsed;
438
+ const stableCode = typeof code === "string" ? code : null;
439
+ if (typeof message !== "string") return { ...raw, code: stableCode };
440
+ if (stableCode === null)
441
+ return { detail: message.slice(0, limit), code: null };
442
+ return {
443
+ detail: `${message} (${stableCode})`.slice(0, limit),
444
+ code: stableCode
445
+ };
446
+ }
447
+ async function apiRequest(options, method, path8, body, init = {}) {
448
+ const reply = await new PlatformTransport(resolveEnv(options)).send(method, path8, body, init);
449
+ if (!reply.ok) throw new ApiError(reply.status, method, path8, reply.text);
450
+ return JSON.parse(reply.text);
233
451
  }
234
452
  var ApiError;
235
453
  var init_worlds = __esm({
236
454
  "src/worlds/index.ts"() {
237
455
  "use strict";
456
+ init_transport();
457
+ init_transport();
238
458
  init_session();
459
+ init_sessionList();
460
+ init_sessionContract();
461
+ init_taskRef();
239
462
  init_tasks();
240
463
  init_taskSessions();
241
464
  init_taskRun();
465
+ init_setRun();
466
+ init_checkoutProvenance();
242
467
  init_evals();
243
468
  init_browser();
469
+ init_browserReconnect();
244
470
  init_hub();
245
471
  init_validate();
246
472
  init_describe();
@@ -250,12 +476,20 @@ var init_worlds = __esm({
250
476
  init_migrate();
251
477
  init_ingest();
252
478
  init_sandbox();
479
+ init_editSessions();
480
+ init_dryRun();
253
481
  ApiError = class extends Error {
254
- constructor(status, method, path6, body) {
255
- super(`${method} ${path6} -> ${status}: ${body}`);
482
+ constructor(status, method, path8, text) {
483
+ const parsed = parseErrorBody(text);
484
+ super(`${method} ${path8} -> ${status}: ${parsed.detail}`);
256
485
  this.status = status;
486
+ this.text = text;
257
487
  this.name = "ApiError";
488
+ this.code = parsed.code;
258
489
  }
490
+ status;
491
+ text;
492
+ code;
259
493
  };
260
494
  }
261
495
  });
@@ -263,8 +497,8 @@ var init_worlds = __esm({
263
497
  // src/files/index.ts
264
498
  init_worlds();
265
499
  import { createHash as createHash5 } from "crypto";
266
- import * as fs5 from "fs";
267
- import * as path5 from "path";
500
+ import * as fs7 from "fs";
501
+ import * as path7 from "path";
268
502
  import { Readable, Transform } from "stream";
269
503
  import { pipeline } from "stream/promises";
270
504
  var DRIVE_PREFIX = "drive:";
@@ -304,17 +538,20 @@ async function openDownload(url) {
304
538
  if (!res.ok || !res.body) throw new Error(`download failed: storage answered HTTP ${res.status}`);
305
539
  return res;
306
540
  }
307
- async function readFile(options = {}, filePath) {
541
+ async function readFile(options = {}, filePath, limits = {}) {
308
542
  const file = await getFile(options, filePath);
543
+ const size = file.sizeBytes ?? 0;
544
+ if (limits.maxBytes !== void 0 && size > limits.maxBytes)
545
+ throw new Error(`${filePath} is ${size} bytes, over the ${limits.maxBytes}-byte limit`);
309
546
  const res = await openDownload(file.url);
310
547
  return Buffer.from(await res.arrayBuffer());
311
548
  }
312
549
  async function downloadFile(options = {}, filePath, outPath, onProgress) {
313
550
  const file = await getFile(options, filePath);
314
551
  const res = await openDownload(file.url);
315
- fs5.mkdirSync(path5.dirname(path5.resolve(outPath)), { recursive: true });
552
+ fs7.mkdirSync(path7.dirname(path7.resolve(outPath)), { recursive: true });
316
553
  const source = Readable.fromWeb(res.body);
317
- await pipeline(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs5.createWriteStream(outPath));
554
+ await pipeline(source, counting((n) => onProgress?.(n, file.sizeBytes)), fs7.createWriteStream(outPath));
318
555
  return { ...file, out: outPath };
319
556
  }
320
557
  async function requestUpload(options = {}, body) {
@@ -325,7 +562,7 @@ async function confirmUpload(options = {}, fileId, ok = true) {
325
562
  }
326
563
  async function sha256Base64(localPath) {
327
564
  const hash = createHash5("sha256");
328
- for await (const chunk of fs5.createReadStream(localPath)) hash.update(chunk);
565
+ for await (const chunk of fs7.createReadStream(localPath)) hash.update(chunk);
329
566
  return hash.digest("base64");
330
567
  }
331
568
  var CONTENT_TYPES = {
@@ -361,7 +598,7 @@ var CONTENT_TYPES = {
361
598
  ".mp4": "video/mp4"
362
599
  };
363
600
  function contentTypeFor(name) {
364
- return CONTENT_TYPES[path5.extname(name).toLowerCase()] ?? "application/octet-stream";
601
+ return CONTENT_TYPES[path7.extname(name).toLowerCase()] ?? "application/octet-stream";
365
602
  }
366
603
  function counting(report) {
367
604
  let seen = 0;
@@ -388,7 +625,7 @@ async function putPresigned(uploadUrl, body, headers) {
388
625
  if (!res.ok) throw new Error(`upload failed: storage answered HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`);
389
626
  }
390
627
  async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
391
- const stat = fs5.statSync(localPath);
628
+ const stat = fs7.statSync(localPath);
392
629
  if (!stat.isFile()) throw new Error(`${localPath} is not a file`);
393
630
  if (stat.size === 0) throw new Error(`${localPath} is empty; the drive refuses zero-byte uploads`);
394
631
  const contentType = upload.contentType ?? contentTypeFor(localPath);
@@ -396,7 +633,7 @@ async function uploadFile(options = {}, localPath, targetPath, upload = {}) {
396
633
  const target = drivePath(targetPath);
397
634
  const granted = await requestUpload(options, { path: target, contentLength: stat.size, contentType, sha256, ...upload.overwrite ? { overwrite: true } : {} });
398
635
  try {
399
- const stream = fs5.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
636
+ const stream = fs7.createReadStream(localPath).pipe(counting((n) => upload.onProgress?.(n, stat.size)));
400
637
  await putPresigned(granted.uploadUrl, Readable.toWeb(stream), { contentType, contentLength: stat.size, sha256 });
401
638
  } catch (error) {
402
639
  await confirmUpload(options, granted.fileId, false).catch(() => void 0);
@@ -426,6 +663,15 @@ async function deleteFile(options = {}, filePath) {
426
663
  async function moveFile(options = {}, fromPath, toPath) {
427
664
  return apiRequest(options, "POST", `${BASE}/move`, { fromPath: drivePath(fromPath), toPath: drivePath(toPath) });
428
665
  }
666
+ async function fetchToDrive(options = {}, url, targetPath, fetchOptions = {}) {
667
+ return apiRequest(options, "POST", `${BASE}/fetch`, {
668
+ url,
669
+ path: drivePath(targetPath),
670
+ ...fetchOptions.headers?.length ? { headers: fetchOptions.headers } : {},
671
+ ...fetchOptions.contentType ? { contentType: fetchOptions.contentType } : {},
672
+ ...fetchOptions.overwrite ? { overwrite: true } : {}
673
+ });
674
+ }
429
675
  async function createFolder(options = {}, folderPath) {
430
676
  return apiRequest(options, "POST", `${BASE}/folders`, { path: drivePath(folderPath) });
431
677
  }
@@ -443,6 +689,7 @@ export {
443
689
  deleteFile,
444
690
  downloadFile,
445
691
  drivePath,
692
+ fetchToDrive,
446
693
  getFile,
447
694
  isDriveRef,
448
695
  iterateFiles,