@reactor-team/js-sdk 2.9.0 → 2.9.1

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
@@ -83,7 +83,7 @@ import { z } from "zod";
83
83
  // package.json
84
84
  var package_default = {
85
85
  name: "@reactor-team/js-sdk",
86
- version: "2.9.0",
86
+ version: "2.9.1",
87
87
  description: "Reactor JavaScript frontend SDK",
88
88
  main: "dist/index.js",
89
89
  module: "dist/index.mjs",
@@ -1032,45 +1032,20 @@ var WebRTCTransportClient = class {
1032
1032
  // ─────────────────────────────────────────────────────────────────────────
1033
1033
  // Connection Lifecycle
1034
1034
  // ─────────────────────────────────────────────────────────────────────────
1035
- connect(tracks) {
1035
+ warmup() {
1036
1036
  return __async(this, null, function* () {
1037
- this.setStatus("connecting");
1038
- this.resetTransportTimings();
1039
- const iceServers = yield this.fetchIceServers();
1040
- this.peerConnection = createPeerConnection({ iceServers });
1041
- this.setupPeerConnectionHandlers();
1042
- this.dataChannel = createDataChannel(this.peerConnection);
1043
- this.setupDataChannelHandlers();
1044
- this.transceiverMap.clear();
1045
- for (const track of tracks) {
1046
- const transceiver = this.peerConnection.addTransceiver(track.kind, {
1047
- direction: track.direction
1037
+ if (!this.cachedIceServers) {
1038
+ this.cachedIceServers = this.fetchIceServers();
1039
+ this.cachedIceServers.catch(() => {
1048
1040
  });
1049
- this.transceiverMap.set(track.name, {
1050
- name: track.name,
1051
- kind: track.kind,
1052
- direction: track.direction,
1053
- transceiver
1054
- });
1055
- console.debug(
1056
- `[WebRTCTransport] Transceiver added: "${track.name}" (${track.kind}, ${track.direction})`
1057
- );
1058
1041
  }
1059
- const sdpOffer = yield createOffer(this.peerConnection);
1060
- const trackMapping = this.buildTrackMapping(tracks);
1061
- yield this.sendSdpOffer(sdpOffer, trackMapping, "POST");
1062
- const answerResponse = yield this.pollSdpAnswer();
1063
- this.iceStartTime = performance.now();
1064
- yield setRemoteDescription(
1065
- this.peerConnection,
1066
- answerResponse.sdp_answer
1067
- );
1068
- console.debug("[WebRTCTransport] Remote description set");
1042
+ yield this.cachedIceServers;
1069
1043
  });
1070
1044
  }
1071
- reconnect(tracks) {
1045
+ prepare(tracks) {
1072
1046
  return __async(this, null, function* () {
1073
1047
  this.setStatus("connecting");
1048
+ this.resetTransportTimings();
1074
1049
  this.stopPing();
1075
1050
  this.stopStatsPolling();
1076
1051
  if (this.dataChannel) {
@@ -1083,8 +1058,8 @@ var WebRTCTransportClient = class {
1083
1058
  }
1084
1059
  this.peerConnected = false;
1085
1060
  this.dataChannelOpen = false;
1086
- this.resetTransportTimings();
1087
- const iceServers = yield this.fetchIceServers();
1061
+ const iceServers = this.cachedIceServers ? yield this.cachedIceServers : yield this.fetchIceServers();
1062
+ this.cachedIceServers = void 0;
1088
1063
  this.peerConnection = createPeerConnection({ iceServers });
1089
1064
  this.setupPeerConnectionHandlers();
1090
1065
  this.dataChannel = createDataChannel(this.peerConnection);
@@ -1100,17 +1075,35 @@ var WebRTCTransportClient = class {
1100
1075
  direction: track.direction,
1101
1076
  transceiver
1102
1077
  });
1078
+ console.debug(
1079
+ `[WebRTCTransport] Transceiver added: "${track.name}" (${track.kind}, ${track.direction})`
1080
+ );
1103
1081
  }
1104
- const sdpOffer = yield createOffer(this.peerConnection);
1105
- const trackMapping = this.buildTrackMapping(tracks);
1106
- yield this.sendSdpOffer(sdpOffer, trackMapping, "PUT");
1082
+ this.pendingSdpOffer = yield createOffer(this.peerConnection);
1083
+ this.pendingTrackMapping = this.buildTrackMapping(tracks);
1084
+ console.debug("[WebRTCTransport] SDP offer prepared");
1085
+ });
1086
+ }
1087
+ connect(reconnect = false) {
1088
+ return __async(this, null, function* () {
1089
+ if (!this.pendingSdpOffer || !this.pendingTrackMapping) {
1090
+ throw new Error(
1091
+ "[WebRTCTransport] No prepared connection. Call prepare() first."
1092
+ );
1093
+ }
1094
+ const method = reconnect ? "PUT" : "POST";
1095
+ const sdpOffer = this.pendingSdpOffer;
1096
+ const trackMapping = this.pendingTrackMapping;
1097
+ this.pendingSdpOffer = void 0;
1098
+ this.pendingTrackMapping = void 0;
1099
+ yield this.sendSdpOffer(sdpOffer, trackMapping, method);
1107
1100
  const answerResponse = yield this.pollSdpAnswer();
1108
1101
  this.iceStartTime = performance.now();
1109
1102
  yield setRemoteDescription(
1110
1103
  this.peerConnection,
1111
1104
  answerResponse.sdp_answer
1112
1105
  );
1113
- console.debug("[WebRTCTransport] Remote description set (reconnect)");
1106
+ console.debug("[WebRTCTransport] Remote description set");
1114
1107
  });
1115
1108
  }
1116
1109
  disconnect() {
@@ -1429,10 +1422,16 @@ var WebRTCTransportClient = class {
1429
1422
  import { z as z2 } from "zod";
1430
1423
  var LOCAL_COORDINATOR_URL = "http://localhost:8080";
1431
1424
  var DEFAULT_BASE_URL = "https://api.reactor.inc";
1425
+ var TrackHintSchema = z2.object({
1426
+ name: z2.string(),
1427
+ kind: z2.enum(["video", "audio"]),
1428
+ direction: z2.enum(["recvonly", "sendonly"])
1429
+ });
1432
1430
  var OptionsSchema = z2.object({
1433
1431
  apiUrl: z2.string().default(DEFAULT_BASE_URL),
1434
1432
  modelName: z2.string(),
1435
- local: z2.boolean().default(false)
1433
+ local: z2.boolean().default(false),
1434
+ modelTracks: z2.array(TrackHintSchema).optional()
1436
1435
  });
1437
1436
  var Reactor = class {
1438
1437
  constructor(options) {
@@ -1446,6 +1445,9 @@ var Reactor = class {
1446
1445
  if (this.local && options.apiUrl === void 0) {
1447
1446
  this.coordinatorUrl = LOCAL_COORDINATOR_URL;
1448
1447
  }
1448
+ if (validatedOptions.modelTracks) {
1449
+ this.presetTracks = validatedOptions.modelTracks;
1450
+ }
1449
1451
  }
1450
1452
  on(event, handler) {
1451
1453
  if (!this.eventListeners.has(event)) {
@@ -1520,7 +1522,9 @@ var Reactor = class {
1520
1522
  * (fires `@file_uploaded` on the model if registered)
1521
1523
  * 4. Return a `FileRef` to pass into {@link sendCommand}
1522
1524
  *
1523
- * Works identically in local and production modes.
1525
+ * In local mode, the presigned URL returned by the runtime is rewritten
1526
+ * to use the SDK-configured base URL (scheme + host), so port-forwarded
1527
+ * setups work correctly (REA-1573).
1524
1528
  */
1525
1529
  uploadFile(file, options) {
1526
1530
  return __async(this, null, function* () {
@@ -1544,7 +1548,16 @@ var Reactor = class {
1544
1548
  size,
1545
1549
  mime_type: mimeType
1546
1550
  });
1547
- const putResponse = yield fetch(slot.presigned_url, {
1551
+ let presignedUrl = slot.presigned_url;
1552
+ if (this.local) {
1553
+ const target = new URL(this.coordinatorUrl);
1554
+ const parsed = new URL(presignedUrl);
1555
+ parsed.protocol = target.protocol;
1556
+ parsed.hostname = target.hostname;
1557
+ parsed.port = target.port;
1558
+ presignedUrl = parsed.toString();
1559
+ }
1560
+ const putResponse = yield fetch(presignedUrl, {
1548
1561
  method: "PUT",
1549
1562
  body: file,
1550
1563
  headers: {
@@ -1646,7 +1659,8 @@ var Reactor = class {
1646
1659
  });
1647
1660
  this.setupTransportHandlers();
1648
1661
  }
1649
- yield this.transportClient.reconnect(this.tracks);
1662
+ yield this.transportClient.prepare(this.tracks);
1663
+ yield this.transportClient.connect(true);
1650
1664
  } catch (error) {
1651
1665
  if (isAbortError(error)) return;
1652
1666
  console.error("[Reactor] Failed to reconnect:", error);
@@ -1692,34 +1706,49 @@ var Reactor = class {
1692
1706
  initialResponse.state
1693
1707
  );
1694
1708
  this.setStatus("waiting");
1695
- const tPoll = performance.now();
1696
- const sessionResponse = yield this.coordinatorClient.pollSessionReady();
1697
- const sessionPollingMs = performance.now() - tPoll;
1698
- this.sessionResponse = sessionResponse;
1699
- this.capabilities = sessionResponse.capabilities;
1700
- this.tracks = sessionResponse.capabilities.tracks;
1701
- this.emit("capabilitiesReceived", this.capabilities);
1702
- console.debug(
1703
- "[Reactor] Session ready, transport:",
1704
- sessionResponse.selected_transport.protocol,
1705
- "tracks:",
1706
- this.tracks.length
1707
- );
1708
- const protocol = sessionResponse.selected_transport.protocol;
1709
- if (protocol !== "webrtc") {
1710
- throw new Error(`Unsupported transport protocol: ${protocol}`);
1711
- }
1712
1709
  this.transportClient = new WebRTCTransportClient({
1713
1710
  baseUrl: this.coordinatorUrl,
1714
- sessionId: sessionResponse.session_id,
1711
+ sessionId: initialResponse.session_id,
1715
1712
  jwtToken: this.local ? "local" : jwtToken,
1716
1713
  webrtcVersion: REACTOR_WEBRTC_VERSION,
1717
1714
  maxPollAttempts: options == null ? void 0 : options.maxAttempts
1718
1715
  });
1719
1716
  this.setupTransportHandlers();
1720
- const tTransport = performance.now();
1721
- yield this.transportClient.connect(this.tracks);
1722
- const transportConnectingMs = performance.now() - tTransport;
1717
+ let sessionPollingMs;
1718
+ let transportConnectingMs;
1719
+ if (this.presetTracks) {
1720
+ const tParallel = performance.now();
1721
+ const [sessionResponse] = yield Promise.all([
1722
+ this.coordinatorClient.pollSessionReady(),
1723
+ this.transportClient.prepare(this.presetTracks)
1724
+ ]);
1725
+ sessionPollingMs = performance.now() - tParallel;
1726
+ this.sessionResponse = sessionResponse;
1727
+ this.capabilities = sessionResponse.capabilities;
1728
+ this.tracks = this.presetTracks;
1729
+ this.emit("capabilitiesReceived", this.capabilities);
1730
+ const tConnect = performance.now();
1731
+ yield this.transportClient.connect();
1732
+ transportConnectingMs = performance.now() - tConnect;
1733
+ } else {
1734
+ this.transportClient.warmup();
1735
+ const tPoll = performance.now();
1736
+ const sessionResponse = yield this.coordinatorClient.pollSessionReady();
1737
+ sessionPollingMs = performance.now() - tPoll;
1738
+ this.sessionResponse = sessionResponse;
1739
+ this.capabilities = sessionResponse.capabilities;
1740
+ this.tracks = sessionResponse.capabilities.tracks;
1741
+ this.emit("capabilitiesReceived", this.capabilities);
1742
+ const protocol = sessionResponse.selected_transport.protocol;
1743
+ if (protocol !== "webrtc") {
1744
+ throw new Error(`Unsupported transport protocol: ${protocol}`);
1745
+ }
1746
+ const tTransport = performance.now();
1747
+ yield this.transportClient.prepare(this.tracks);
1748
+ yield this.transportClient.connect();
1749
+ transportConnectingMs = performance.now() - tTransport;
1750
+ }
1751
+ console.debug("[Reactor] Session ready, tracks:", this.tracks.length);
1723
1752
  this.connectionTimings = {
1724
1753
  sessionCreationMs: sessionCreationMs + sessionPollingMs,
1725
1754
  transportConnectingMs,
@@ -2379,18 +2408,24 @@ function ReactorView({
2379
2408
  }
2380
2409
 
2381
2410
  // src/react/ReactorController.tsx
2382
- import React, { useState as useState3, useCallback } from "react";
2411
+ import React, { useState as useState3, useCallback, useRef as useRef4 } from "react";
2383
2412
  import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
2413
+ function isFileParam(ps) {
2414
+ return ps.type === "file" || ps.type === "object" && ps.format === "file-reference";
2415
+ }
2384
2416
  function ReactorController({
2385
2417
  className,
2386
2418
  style
2387
2419
  }) {
2388
- const { sendCommand, status } = useReactor((state) => ({
2420
+ const { sendCommand, uploadFile, status } = useReactor((state) => ({
2389
2421
  sendCommand: state.sendCommand,
2422
+ uploadFile: state.uploadFile,
2390
2423
  status: state.status
2391
2424
  }));
2392
2425
  const [commands, setCommands] = useState3({});
2393
2426
  const [formValues, setFormValues] = useState3({});
2427
+ const formRef = useRef4(formValues);
2428
+ formRef.current = formValues;
2394
2429
  const [expandedCommands, setExpandedCommands] = useState3({});
2395
2430
  React.useEffect(() => {
2396
2431
  if (status === "disconnected") {
@@ -2437,15 +2472,17 @@ function ReactorController({
2437
2472
  initialExpanded[commandName] = false;
2438
2473
  Object.entries(commandSchema.schema).forEach(
2439
2474
  ([paramName, paramSchema]) => {
2440
- var _a2, _b;
2441
- if (paramSchema.type === "number") {
2475
+ var _a2;
2476
+ if (isFileParam(paramSchema)) {
2477
+ initialValues[commandName][paramName] = null;
2478
+ } else if (paramSchema.default !== void 0) {
2479
+ initialValues[commandName][paramName] = paramSchema.default;
2480
+ } else if (paramSchema.type === "number" || paramSchema.type === "integer") {
2442
2481
  initialValues[commandName][paramName] = (_a2 = paramSchema.minimum) != null ? _a2 : 0;
2443
2482
  } else if (paramSchema.type === "string") {
2444
2483
  initialValues[commandName][paramName] = "";
2445
2484
  } else if (paramSchema.type === "boolean") {
2446
2485
  initialValues[commandName][paramName] = false;
2447
- } else if (paramSchema.type === "integer") {
2448
- initialValues[commandName][paramName] = (_b = paramSchema.minimum) != null ? _b : 0;
2449
2486
  }
2450
2487
  }
2451
2488
  );
@@ -2472,12 +2509,16 @@ function ReactorController({
2472
2509
  const handleCommandSubmit = useCallback(
2473
2510
  (commandName) => __async(null, null, function* () {
2474
2511
  const commandSchema = commands[commandName];
2475
- const formData = formValues[commandName] || {};
2512
+ const formData = formRef.current[commandName] || {};
2476
2513
  const data = {};
2477
2514
  Object.keys(commandSchema.schema).forEach((paramName) => {
2478
- var _a, _b;
2515
+ var _a;
2479
2516
  const paramSchema = commandSchema.schema[paramName];
2480
2517
  let value = formData[paramName];
2518
+ if (value instanceof FileRef) {
2519
+ data[paramName] = value;
2520
+ return;
2521
+ }
2481
2522
  if (paramSchema.type === "number" && typeof value === "string") {
2482
2523
  value = parseFloat(value) || 0;
2483
2524
  } else if (paramSchema.type === "integer" && typeof value === "string") {
@@ -2488,10 +2529,8 @@ function ReactorController({
2488
2529
  if (value !== void 0 && value !== "" && value !== null) {
2489
2530
  data[paramName] = value;
2490
2531
  } else if (paramSchema.required) {
2491
- if (paramSchema.type === "number") {
2532
+ if (paramSchema.type === "number" || paramSchema.type === "integer") {
2492
2533
  data[paramName] = (_a = paramSchema.minimum) != null ? _a : 0;
2493
- } else if (paramSchema.type === "integer") {
2494
- data[paramName] = (_b = paramSchema.minimum) != null ? _b : 0;
2495
2534
  } else if (paramSchema.type === "string") {
2496
2535
  data[paramName] = "";
2497
2536
  } else if (paramSchema.type === "boolean") {
@@ -2499,14 +2538,28 @@ function ReactorController({
2499
2538
  }
2500
2539
  }
2501
2540
  });
2502
- console.log(`Executing command: ${commandName}`, data);
2503
2541
  yield sendCommand(commandName, data);
2504
2542
  }),
2505
- [formValues, sendCommand, commands]
2543
+ [sendCommand, commands]
2506
2544
  );
2507
2545
  const renderInput = (commandName, paramName, paramSchema) => {
2508
2546
  var _a, _b;
2509
2547
  const value = (_b = (_a = formValues[commandName]) == null ? void 0 : _a[paramName]) != null ? _b : "";
2548
+ if (isFileParam(paramSchema)) {
2549
+ return /* @__PURE__ */ jsx3(
2550
+ FileParamInput,
2551
+ {
2552
+ commandName,
2553
+ paramName,
2554
+ description: paramSchema.description,
2555
+ value,
2556
+ onChange: handleInputChange,
2557
+ disabled: status !== "ready",
2558
+ uploadFile
2559
+ },
2560
+ `${commandName}-${paramName}`
2561
+ );
2562
+ }
2510
2563
  if (paramSchema.type === "number" || paramSchema.type === "integer") {
2511
2564
  const isInteger = paramSchema.type === "integer";
2512
2565
  const step = isInteger ? 1 : 0.1;
@@ -2695,10 +2748,11 @@ function ReactorController({
2695
2748
  const renderCommand = (commandName, commandSchema) => {
2696
2749
  const hasParams = Object.keys(commandSchema.schema).length > 0;
2697
2750
  const isExpanded = expandedCommands[commandName];
2698
- const hasSliderInputs = Object.values(commandSchema.schema).some(
2699
- (paramSchema) => (paramSchema.type === "number" || paramSchema.type === "integer") && typeof paramSchema.minimum === "number" && typeof paramSchema.maximum === "number"
2751
+ const params = Object.values(commandSchema.schema);
2752
+ const allSliders = params.length > 0 && params.every(
2753
+ (ps) => (ps.type === "number" || ps.type === "integer") && typeof ps.minimum === "number" && typeof ps.maximum === "number"
2700
2754
  );
2701
- const showExecuteButton = !hasSliderInputs;
2755
+ const showExecuteButton = !allSliders;
2702
2756
  return /* @__PURE__ */ jsxs2(
2703
2757
  "div",
2704
2758
  {
@@ -2836,9 +2890,92 @@ function ReactorController({
2836
2890
  Object.entries(commands).map(([commandName, commandSchema]) => /* @__PURE__ */ jsx3("div", { children: renderCommand(commandName, commandSchema) }, commandName))
2837
2891
  ] }) }) });
2838
2892
  }
2893
+ function FileParamInput({
2894
+ commandName,
2895
+ paramName,
2896
+ description,
2897
+ value,
2898
+ onChange,
2899
+ disabled,
2900
+ uploadFile
2901
+ }) {
2902
+ const inputRef = useRef4(null);
2903
+ const [uploading, setUploading] = useState3(false);
2904
+ const fileRef = value instanceof FileRef ? value : null;
2905
+ const handleFile = (e) => __async(null, null, function* () {
2906
+ var _a;
2907
+ const file = (_a = e.target.files) == null ? void 0 : _a[0];
2908
+ if (!file) return;
2909
+ e.target.value = "";
2910
+ setUploading(true);
2911
+ try {
2912
+ const ref = yield uploadFile(file);
2913
+ onChange(commandName, paramName, ref);
2914
+ } catch (err) {
2915
+ console.error("[FileParamInput] Upload failed:", err);
2916
+ } finally {
2917
+ setUploading(false);
2918
+ }
2919
+ });
2920
+ return /* @__PURE__ */ jsxs2("div", { style: { marginBottom: "8px" }, children: [
2921
+ /* @__PURE__ */ jsxs2("label", { style: { fontSize: "12px", color: "#666", display: "block" }, children: [
2922
+ paramName,
2923
+ /* @__PURE__ */ jsx3("span", { style: { color: "#888", fontSize: "11px" }, children: " (file)" }),
2924
+ description && ` - ${description}`
2925
+ ] }),
2926
+ /* @__PURE__ */ jsxs2(
2927
+ "div",
2928
+ {
2929
+ style: {
2930
+ display: "flex",
2931
+ alignItems: "center",
2932
+ gap: "8px",
2933
+ marginTop: "4px"
2934
+ },
2935
+ children: [
2936
+ /* @__PURE__ */ jsx3(
2937
+ "input",
2938
+ {
2939
+ ref: inputRef,
2940
+ type: "file",
2941
+ accept: "image/*",
2942
+ onChange: handleFile,
2943
+ style: { display: "none" }
2944
+ }
2945
+ ),
2946
+ /* @__PURE__ */ jsx3(
2947
+ "button",
2948
+ {
2949
+ disabled: disabled || uploading,
2950
+ onClick: () => {
2951
+ var _a;
2952
+ return (_a = inputRef.current) == null ? void 0 : _a.click();
2953
+ },
2954
+ style: {
2955
+ padding: "4px 12px",
2956
+ fontSize: "12px",
2957
+ backgroundColor: "#007bff",
2958
+ color: "white",
2959
+ border: "none",
2960
+ borderRadius: "4px",
2961
+ cursor: disabled || uploading ? "default" : "pointer",
2962
+ opacity: disabled || uploading ? 0.5 : 1
2963
+ },
2964
+ children: uploading ? "Uploading\u2026" : fileRef ? "Change" : "Choose File"
2965
+ }
2966
+ ),
2967
+ fileRef && /* @__PURE__ */ jsxs2("span", { style: { fontSize: "11px", color: "#28a745" }, children: [
2968
+ "\\u2713 ",
2969
+ fileRef.name
2970
+ ] })
2971
+ ]
2972
+ }
2973
+ )
2974
+ ] });
2975
+ }
2839
2976
 
2840
2977
  // src/react/WebcamStream.tsx
2841
- import { useEffect as useEffect4, useRef as useRef4, useState as useState4 } from "react";
2978
+ import { useEffect as useEffect4, useRef as useRef5, useState as useState4 } from "react";
2842
2979
  import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
2843
2980
  function WebcamStream({
2844
2981
  track,
@@ -2860,7 +2997,7 @@ function WebcamStream({
2860
2997
  unpublish: state.unpublish,
2861
2998
  reactor: state.internal.reactor
2862
2999
  }));
2863
- const videoRef = useRef4(null);
3000
+ const videoRef = useRef5(null);
2864
3001
  const startWebcam = () => __async(null, null, function* () {
2865
3002
  console.debug("[WebcamPublisher] Starting webcam");
2866
3003
  try {