@reactor-team/js-sdk 2.8.1 → 2.9.0

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.
package/dist/index.mjs CHANGED
@@ -50,6 +50,18 @@ var __async = (__this, __arguments, generator) => {
50
50
  });
51
51
  };
52
52
 
53
+ // src/core/FileRef.ts
54
+ var FileRef = class {
55
+ constructor(uploadId, name, mimeType, size) {
56
+ this.uploadId = uploadId;
57
+ this.name = name;
58
+ this.mimeType = mimeType;
59
+ this.size = size;
60
+ /** @internal Marker so sendCommand can detect FileRef values. */
61
+ this.__isFileRef = true;
62
+ }
63
+ };
64
+
53
65
  // src/types.ts
54
66
  var ConflictError = class extends Error {
55
67
  constructor(message) {
@@ -71,7 +83,7 @@ import { z } from "zod";
71
83
  // package.json
72
84
  var package_default = {
73
85
  name: "@reactor-team/js-sdk",
74
- version: "2.8.1",
86
+ version: "2.9.0",
75
87
  description: "Reactor JavaScript frontend SDK",
76
88
  main: "dist/index.js",
77
89
  module: "dist/index.mjs",
@@ -110,7 +122,7 @@ var package_default = {
110
122
  license: "MIT",
111
123
  repository: {
112
124
  type: "git",
113
- url: "https://github.com/reactor-team/js-sdk"
125
+ url: "https://github.com/reactor-team/js-sdk.git"
114
126
  },
115
127
  packageManager: "pnpm@10.12.1",
116
128
  devDependencies: {
@@ -199,6 +211,16 @@ var SessionResponseSchema = CreateSessionResponseSchema.extend({
199
211
  var TerminateSessionRequestSchema = z.object({
200
212
  reason: z.string().optional()
201
213
  });
214
+ var CreateUploadRequestSchema = z.object({
215
+ name: z.string(),
216
+ size: z.number().int().positive(),
217
+ mime_type: z.string()
218
+ });
219
+ var CreateUploadResponseSchema = z.object({
220
+ presigned_id: z.string(),
221
+ presigned_url: z.string(),
222
+ path: z.string()
223
+ });
202
224
  var IceServerCredentialsSchema = z.object({
203
225
  username: z.string(),
204
226
  password: z.string()
@@ -535,6 +557,40 @@ var CoordinatorClient = class {
535
557
  );
536
558
  });
537
559
  }
560
+ /**
561
+ * Allocates an upload slot for a file.
562
+ * Returns a presigned PUT URL the caller can use to upload the file
563
+ * directly to the object store (production) or local runtime (local dev).
564
+ */
565
+ createUpload(sessionId, request) {
566
+ return __async(this, null, function* () {
567
+ console.debug("[CoordinatorClient] Creating upload slot...", {
568
+ sessionId,
569
+ name: request.name,
570
+ size: request.size
571
+ });
572
+ const response = yield fetch(
573
+ `${this.baseUrl}/sessions/${sessionId}/uploads`,
574
+ {
575
+ method: "POST",
576
+ headers: __spreadProps(__spreadValues({}, this.getHeaders()), {
577
+ "Content-Type": "application/json"
578
+ }),
579
+ body: JSON.stringify(request),
580
+ signal: this.signal
581
+ }
582
+ );
583
+ yield this.checkVersionMismatch(response);
584
+ if (!response.ok) {
585
+ const errorText = yield response.text();
586
+ throw new Error(
587
+ `Failed to create upload slot: ${response.status} ${errorText}`
588
+ );
589
+ }
590
+ const data = yield response.json();
591
+ return CreateUploadResponseSchema.parse(data);
592
+ });
593
+ }
538
594
  getSessionId() {
539
595
  return this.currentSessionId;
540
596
  }
@@ -703,12 +759,15 @@ function waitForIceGathering(pc, timeoutMs = 5e3) {
703
759
  }, timeoutMs);
704
760
  });
705
761
  }
706
- function sendMessage(channel, command, data, scope = "application", maxBytes = DEFAULT_MAX_MESSAGE_BYTES) {
762
+ function sendMessage(channel, command, data, scope = "application", maxBytes = DEFAULT_MAX_MESSAGE_BYTES, uploads) {
707
763
  if (channel.readyState !== "open") {
708
764
  throw new Error(`Data channel not open: ${channel.readyState}`);
709
765
  }
710
766
  const jsonData = typeof data === "string" ? JSON.parse(data) : data;
711
767
  const inner = { type: command, data: jsonData };
768
+ if (uploads && Object.keys(uploads).length > 0) {
769
+ inner.uploads = uploads;
770
+ }
712
771
  const payload = { scope, data: inner };
713
772
  const serialized = JSON.stringify(payload);
714
773
  const byteLength = new TextEncoder().encode(serialized).byteLength;
@@ -1117,7 +1176,7 @@ var WebRTCTransportClient = class {
1117
1176
  var _a, _b, _c;
1118
1177
  return (_c = (_b = (_a = this.peerConnection) == null ? void 0 : _a.sctp) == null ? void 0 : _b.maxMessageSize) != null ? _c : void 0;
1119
1178
  }
1120
- sendCommand(command, data, scope = "application") {
1179
+ sendCommand(command, data, scope = "application", uploads) {
1121
1180
  if (!this.dataChannel) {
1122
1181
  throw new Error("[WebRTCTransport] Data channel not available");
1123
1182
  }
@@ -1127,7 +1186,8 @@ var WebRTCTransportClient = class {
1127
1186
  command,
1128
1187
  data,
1129
1188
  scope,
1130
- this.maxMessageBytes
1189
+ this.maxMessageBytes,
1190
+ uploads
1131
1191
  );
1132
1192
  } catch (error) {
1133
1193
  console.warn("[WebRTCTransport] Failed to send message:", error);
@@ -1403,6 +1463,10 @@ var Reactor = class {
1403
1463
  }
1404
1464
  /**
1405
1465
  * Sends a command to the model via the data channel.
1466
+ *
1467
+ * When any value in `data` is a {@link FileRef}, it is automatically
1468
+ * extracted and serialized into a separate `uploads` section on the
1469
+ * wire, keyed by the parameter name. Scalar values remain in `data`.
1406
1470
  */
1407
1471
  sendCommand(command, data, scope = "application") {
1408
1472
  return __async(this, null, function* () {
@@ -1413,7 +1477,28 @@ var Reactor = class {
1413
1477
  return;
1414
1478
  }
1415
1479
  try {
1416
- (_a = this.transportClient) == null ? void 0 : _a.sendCommand(command, data, scope);
1480
+ let uploads;
1481
+ if (data && typeof data === "object" && !Array.isArray(data)) {
1482
+ const scalarData = {};
1483
+ const extractedUploads = {};
1484
+ for (const [key, value] of Object.entries(data)) {
1485
+ if (value instanceof FileRef) {
1486
+ extractedUploads[key] = {
1487
+ upload_id: value.uploadId,
1488
+ name: value.name,
1489
+ mime_type: value.mimeType,
1490
+ size: value.size
1491
+ };
1492
+ } else {
1493
+ scalarData[key] = value;
1494
+ }
1495
+ }
1496
+ if (Object.keys(extractedUploads).length > 0) {
1497
+ uploads = extractedUploads;
1498
+ data = scalarData;
1499
+ }
1500
+ }
1501
+ (_a = this.transportClient) == null ? void 0 : _a.sendCommand(command, data, scope, uploads);
1417
1502
  } catch (error) {
1418
1503
  console.error("[Reactor] Failed to send message:", error);
1419
1504
  this.createError(
@@ -1425,6 +1510,71 @@ var Reactor = class {
1425
1510
  }
1426
1511
  });
1427
1512
  }
1513
+ /**
1514
+ * Uploads a file to the session's object store and returns a {@link FileRef}.
1515
+ *
1516
+ * Flow:
1517
+ * 1. POST /sessions/{id}/uploads → allocate presigned PUT URL
1518
+ * 2. PUT file bytes to the presigned URL
1519
+ * 3. Send `fileUploaded` notification on the runtime data channel
1520
+ * (fires `@file_uploaded` on the model if registered)
1521
+ * 4. Return a `FileRef` to pass into {@link sendCommand}
1522
+ *
1523
+ * Works identically in local and production modes.
1524
+ */
1525
+ uploadFile(file, options) {
1526
+ return __async(this, null, function* () {
1527
+ var _a;
1528
+ if (this.status !== "ready") {
1529
+ throw new Error(
1530
+ `Cannot upload file, status is "${this.status}". Must be "ready".`
1531
+ );
1532
+ }
1533
+ if (!this.coordinatorClient || !this.sessionId) {
1534
+ throw new Error("No active session. Call connect() first.");
1535
+ }
1536
+ const name = (_a = options == null ? void 0 : options.name) != null ? _a : file instanceof File ? file.name : "upload";
1537
+ const mimeType = file.type || "application/octet-stream";
1538
+ const size = file.size;
1539
+ if (size <= 0) {
1540
+ throw new Error("File is empty");
1541
+ }
1542
+ const slot = yield this.coordinatorClient.createUpload(this.sessionId, {
1543
+ name,
1544
+ size,
1545
+ mime_type: mimeType
1546
+ });
1547
+ const putResponse = yield fetch(slot.presigned_url, {
1548
+ method: "PUT",
1549
+ body: file,
1550
+ headers: {
1551
+ "Content-Length": String(size)
1552
+ }
1553
+ });
1554
+ if (!putResponse.ok) {
1555
+ throw new Error(
1556
+ `File upload failed: ${putResponse.status} ${putResponse.statusText}`
1557
+ );
1558
+ }
1559
+ yield this.sendCommand(
1560
+ "fileUploaded",
1561
+ {
1562
+ upload_id: slot.presigned_id,
1563
+ name,
1564
+ mime_type: mimeType,
1565
+ size
1566
+ },
1567
+ "runtime"
1568
+ );
1569
+ console.debug("[Reactor] File uploaded:", {
1570
+ uploadId: slot.presigned_id,
1571
+ name,
1572
+ mimeType,
1573
+ size
1574
+ });
1575
+ return new FileRef(slot.presigned_id, name, mimeType, size);
1576
+ });
1577
+ }
1428
1578
  /**
1429
1579
  * Publishes a MediaStreamTrack to a named sendonly track.
1430
1580
  * The transceiver is already set up from capabilities — this just
@@ -1931,6 +2081,17 @@ var createReactorStore = (initProps, publicState = defaultInitState) => {
1931
2081
  console.error("[ReactorStore] Failed to reconnect:", error);
1932
2082
  throw error;
1933
2083
  }
2084
+ }),
2085
+ uploadFile: (file, options) => __async(null, null, function* () {
2086
+ console.debug("[ReactorStore] Uploading file");
2087
+ try {
2088
+ const result = yield get().internal.reactor.uploadFile(file, options);
2089
+ console.debug("[ReactorStore] File uploaded successfully", result);
2090
+ return result;
2091
+ } catch (error) {
2092
+ console.error("[ReactorStore] Failed to upload file:", error);
2093
+ throw error;
2094
+ }
1934
2095
  })
1935
2096
  });
1936
2097
  });
@@ -2873,6 +3034,7 @@ export {
2873
3034
  AbortError,
2874
3035
  ConflictError,
2875
3036
  DEFAULT_BASE_URL,
3037
+ FileRef,
2876
3038
  Reactor,
2877
3039
  ReactorController,
2878
3040
  ReactorProvider,