@reactor-team/js-sdk 2.3.1 → 2.4.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.d.mts CHANGED
@@ -24,7 +24,29 @@ interface ReactorState$1 {
24
24
  status: ReactorStatus;
25
25
  lastError?: ReactorError;
26
26
  }
27
- type ReactorEvent = "statusChanged" | "sessionIdChanged" | "newMessage" | "streamChanged" | "error" | "sessionExpirationChanged";
27
+ /**
28
+ * Options for configuring the connect polling behavior.
29
+ */
30
+ interface ConnectOptions {
31
+ /** Maximum number of SDP polling attempts before giving up. Default: 6. */
32
+ maxAttempts?: number;
33
+ }
34
+ interface ConnectionStats {
35
+ /** ICE candidate-pair round-trip time in milliseconds */
36
+ rtt?: number;
37
+ /** ICE candidate type: "host", "srflx", "prflx", or "relay" (TURN) */
38
+ candidateType?: string;
39
+ /** Estimated available outgoing bitrate in bits/second */
40
+ availableOutgoingBitrate?: number;
41
+ /** Received video frames per second */
42
+ framesPerSecond?: number;
43
+ /** Ratio of packets lost (0-1) */
44
+ packetLossRatio?: number;
45
+ /** Network jitter in seconds (from inbound-rtp) */
46
+ jitter?: number;
47
+ timestamp: number;
48
+ }
49
+ type ReactorEvent = "statusChanged" | "sessionIdChanged" | "message" | "runtimeMessage" | "streamChanged" | "error" | "sessionExpirationChanged" | "statsUpdate";
28
50
 
29
51
  declare const PROD_COORDINATOR_URL = "https://api.reactor.inc";
30
52
  declare const OptionsSchema: z.ZodObject<{
@@ -70,14 +92,17 @@ declare class Reactor {
70
92
  unpublishTrack(): Promise<void>;
71
93
  /**
72
94
  * Public method for reconnecting to an existing session, that may have been interrupted but can be recovered.
95
+ * @param options Optional connect options (e.g. maxAttempts for SDP polling)
73
96
  */
74
- reconnect(): Promise<void>;
97
+ reconnect(options?: ConnectOptions): Promise<void>;
75
98
  /**
76
99
  * Connects to the coordinator and waits for a GPU to be assigned.
77
100
  * Once a GPU is assigned, the Reactor will connect to the gpu machine via WebRTC.
78
101
  * If no authentication is provided and not in local mode, an error is thrown.
102
+ * @param jwtToken Optional JWT token for authentication
103
+ * @param options Optional connect options (e.g. maxAttempts for SDP polling)
79
104
  */
80
- connect(jwtToken?: string): Promise<void>;
105
+ connect(jwtToken?: string, options?: ConnectOptions): Promise<void>;
81
106
  /**
82
107
  * Sets up event handlers for the machine client.
83
108
  */
@@ -104,6 +129,7 @@ declare class Reactor {
104
129
  * Get the last error that occurred
105
130
  */
106
131
  getLastError(): ReactorError | undefined;
132
+ getStats(): ConnectionStats | undefined;
107
133
  /**
108
134
  * Create and store an error
109
135
  */
@@ -121,11 +147,11 @@ interface ReactorState {
121
147
  }
122
148
  interface ReactorActions {
123
149
  sendCommand(command: string, data: any, scope?: MessageScope): Promise<void>;
124
- connect(jwtToken?: string): Promise<void>;
150
+ connect(jwtToken?: string, options?: ConnectOptions): Promise<void>;
125
151
  disconnect(recoverable?: boolean): Promise<void>;
126
152
  publishVideoStream(stream: MediaStream): Promise<void>;
127
153
  unpublishVideoStream(): Promise<void>;
128
- reconnect(): Promise<void>;
154
+ reconnect(options?: ConnectOptions): Promise<void>;
129
155
  }
130
156
  interface ReactorInternalState {
131
157
  reactor: Reactor;
@@ -137,12 +163,20 @@ interface ReactorInitializationProps extends Options {
137
163
  jwtToken?: string;
138
164
  }
139
165
 
140
- interface ReactorProviderProps extends ReactorInitializationProps {
166
+ /**
167
+ * Options for the React provider's connect behavior.
168
+ * Extends the core ConnectOptions with autoConnect for the React lifecycle.
169
+ */
170
+ interface ReactorConnectOptions extends ConnectOptions {
171
+ /** Whether to automatically connect when the provider mounts. Default: false. */
141
172
  autoConnect?: boolean;
173
+ }
174
+ interface ReactorProviderProps extends ReactorInitializationProps {
175
+ connectOptions?: ReactorConnectOptions;
142
176
  jwtToken?: string;
143
177
  children: ReactNode;
144
178
  }
145
- declare function ReactorProvider({ children, autoConnect, jwtToken, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
179
+ declare function ReactorProvider({ children, connectOptions, jwtToken, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
146
180
  declare function useReactorStore<T = ReactorStore>(selector: (state: ReactorStore) => T): T;
147
181
 
148
182
  interface ReactorViewProps {
@@ -177,15 +211,30 @@ declare function WebcamStream({ className, style, videoConstraints, showWebcam,
177
211
  */
178
212
  declare function useReactor<T>(selector: (state: ReactorStore) => T): T;
179
213
  /**
180
- * Hook for handling message subscriptions with proper React lifecycle management.
214
+ * Hook for receiving model application messages.
215
+ *
216
+ * Only fires for messages sent by the model via `get_ctx().send()`.
217
+ * Internal platform-level messages (e.g. capabilities) are NOT delivered here.
218
+ *
219
+ * @param handler - Callback invoked with each application message payload.
220
+ */
221
+ declare function useReactorMessage(handler: (message: any) => void): void;
222
+ /**
223
+ * Hook for receiving internal platform-level (runtime) messages.
181
224
  *
182
- * The handler receives the message payload and the scope it arrived on:
183
- * - "application" for model-defined messages (via get_ctx().send())
184
- * - "runtime" for platform-level messages (e.g., capabilities response)
225
+ * This is intended for advanced use cases that need access to the runtime
226
+ * control layer, such as capabilities negotiation. Model application messages
227
+ * sent via `get_ctx().send()` are NOT delivered through this hook — use
228
+ * {@link useReactorMessage} for those.
185
229
  *
186
- * @param handler - The message handler function (message, scope)
230
+ * @param handler - Callback invoked with each runtime message payload.
231
+ */
232
+ declare function useReactorInternalMessage(handler: (message: any) => void): void;
233
+ /**
234
+ * Hook that returns the current connection stats (RTT, etc.).
235
+ * Updates every ~2s while connected. Returns undefined when disconnected.
187
236
  */
188
- declare function useReactorMessage(handler: (message: any, scope: MessageScope) => void): void;
237
+ declare function useStats(): ConnectionStats | undefined;
189
238
 
190
239
  /**
191
240
  * ⚠️ INSECURE: Fetches a JWT token directly from the client.
@@ -200,4 +249,4 @@ declare function useReactorMessage(handler: (message: any, scope: MessageScope)
200
249
  */
201
250
  declare function fetchInsecureJwtToken(apiKey: string, coordinatorUrl?: string): Promise<string>;
202
251
 
203
- export { ConflictError, type MessageScope, type Options, PROD_COORDINATOR_URL, Reactor, ReactorController, type ReactorControllerProps, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, WebcamStream, fetchInsecureJwtToken, useReactor, useReactorMessage, useReactorStore };
252
+ export { ConflictError, type ConnectOptions, type ConnectionStats, type MessageScope, type Options, PROD_COORDINATOR_URL, Reactor, type ReactorConnectOptions, ReactorController, type ReactorControllerProps, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, WebcamStream, fetchInsecureJwtToken, useReactor, useReactorInternalMessage, useReactorMessage, useReactorStore, useStats };
package/dist/index.d.ts CHANGED
@@ -24,7 +24,29 @@ interface ReactorState$1 {
24
24
  status: ReactorStatus;
25
25
  lastError?: ReactorError;
26
26
  }
27
- type ReactorEvent = "statusChanged" | "sessionIdChanged" | "newMessage" | "streamChanged" | "error" | "sessionExpirationChanged";
27
+ /**
28
+ * Options for configuring the connect polling behavior.
29
+ */
30
+ interface ConnectOptions {
31
+ /** Maximum number of SDP polling attempts before giving up. Default: 6. */
32
+ maxAttempts?: number;
33
+ }
34
+ interface ConnectionStats {
35
+ /** ICE candidate-pair round-trip time in milliseconds */
36
+ rtt?: number;
37
+ /** ICE candidate type: "host", "srflx", "prflx", or "relay" (TURN) */
38
+ candidateType?: string;
39
+ /** Estimated available outgoing bitrate in bits/second */
40
+ availableOutgoingBitrate?: number;
41
+ /** Received video frames per second */
42
+ framesPerSecond?: number;
43
+ /** Ratio of packets lost (0-1) */
44
+ packetLossRatio?: number;
45
+ /** Network jitter in seconds (from inbound-rtp) */
46
+ jitter?: number;
47
+ timestamp: number;
48
+ }
49
+ type ReactorEvent = "statusChanged" | "sessionIdChanged" | "message" | "runtimeMessage" | "streamChanged" | "error" | "sessionExpirationChanged" | "statsUpdate";
28
50
 
29
51
  declare const PROD_COORDINATOR_URL = "https://api.reactor.inc";
30
52
  declare const OptionsSchema: z.ZodObject<{
@@ -70,14 +92,17 @@ declare class Reactor {
70
92
  unpublishTrack(): Promise<void>;
71
93
  /**
72
94
  * Public method for reconnecting to an existing session, that may have been interrupted but can be recovered.
95
+ * @param options Optional connect options (e.g. maxAttempts for SDP polling)
73
96
  */
74
- reconnect(): Promise<void>;
97
+ reconnect(options?: ConnectOptions): Promise<void>;
75
98
  /**
76
99
  * Connects to the coordinator and waits for a GPU to be assigned.
77
100
  * Once a GPU is assigned, the Reactor will connect to the gpu machine via WebRTC.
78
101
  * If no authentication is provided and not in local mode, an error is thrown.
102
+ * @param jwtToken Optional JWT token for authentication
103
+ * @param options Optional connect options (e.g. maxAttempts for SDP polling)
79
104
  */
80
- connect(jwtToken?: string): Promise<void>;
105
+ connect(jwtToken?: string, options?: ConnectOptions): Promise<void>;
81
106
  /**
82
107
  * Sets up event handlers for the machine client.
83
108
  */
@@ -104,6 +129,7 @@ declare class Reactor {
104
129
  * Get the last error that occurred
105
130
  */
106
131
  getLastError(): ReactorError | undefined;
132
+ getStats(): ConnectionStats | undefined;
107
133
  /**
108
134
  * Create and store an error
109
135
  */
@@ -121,11 +147,11 @@ interface ReactorState {
121
147
  }
122
148
  interface ReactorActions {
123
149
  sendCommand(command: string, data: any, scope?: MessageScope): Promise<void>;
124
- connect(jwtToken?: string): Promise<void>;
150
+ connect(jwtToken?: string, options?: ConnectOptions): Promise<void>;
125
151
  disconnect(recoverable?: boolean): Promise<void>;
126
152
  publishVideoStream(stream: MediaStream): Promise<void>;
127
153
  unpublishVideoStream(): Promise<void>;
128
- reconnect(): Promise<void>;
154
+ reconnect(options?: ConnectOptions): Promise<void>;
129
155
  }
130
156
  interface ReactorInternalState {
131
157
  reactor: Reactor;
@@ -137,12 +163,20 @@ interface ReactorInitializationProps extends Options {
137
163
  jwtToken?: string;
138
164
  }
139
165
 
140
- interface ReactorProviderProps extends ReactorInitializationProps {
166
+ /**
167
+ * Options for the React provider's connect behavior.
168
+ * Extends the core ConnectOptions with autoConnect for the React lifecycle.
169
+ */
170
+ interface ReactorConnectOptions extends ConnectOptions {
171
+ /** Whether to automatically connect when the provider mounts. Default: false. */
141
172
  autoConnect?: boolean;
173
+ }
174
+ interface ReactorProviderProps extends ReactorInitializationProps {
175
+ connectOptions?: ReactorConnectOptions;
142
176
  jwtToken?: string;
143
177
  children: ReactNode;
144
178
  }
145
- declare function ReactorProvider({ children, autoConnect, jwtToken, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
179
+ declare function ReactorProvider({ children, connectOptions, jwtToken, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
146
180
  declare function useReactorStore<T = ReactorStore>(selector: (state: ReactorStore) => T): T;
147
181
 
148
182
  interface ReactorViewProps {
@@ -177,15 +211,30 @@ declare function WebcamStream({ className, style, videoConstraints, showWebcam,
177
211
  */
178
212
  declare function useReactor<T>(selector: (state: ReactorStore) => T): T;
179
213
  /**
180
- * Hook for handling message subscriptions with proper React lifecycle management.
214
+ * Hook for receiving model application messages.
215
+ *
216
+ * Only fires for messages sent by the model via `get_ctx().send()`.
217
+ * Internal platform-level messages (e.g. capabilities) are NOT delivered here.
218
+ *
219
+ * @param handler - Callback invoked with each application message payload.
220
+ */
221
+ declare function useReactorMessage(handler: (message: any) => void): void;
222
+ /**
223
+ * Hook for receiving internal platform-level (runtime) messages.
181
224
  *
182
- * The handler receives the message payload and the scope it arrived on:
183
- * - "application" for model-defined messages (via get_ctx().send())
184
- * - "runtime" for platform-level messages (e.g., capabilities response)
225
+ * This is intended for advanced use cases that need access to the runtime
226
+ * control layer, such as capabilities negotiation. Model application messages
227
+ * sent via `get_ctx().send()` are NOT delivered through this hook — use
228
+ * {@link useReactorMessage} for those.
185
229
  *
186
- * @param handler - The message handler function (message, scope)
230
+ * @param handler - Callback invoked with each runtime message payload.
231
+ */
232
+ declare function useReactorInternalMessage(handler: (message: any) => void): void;
233
+ /**
234
+ * Hook that returns the current connection stats (RTT, etc.).
235
+ * Updates every ~2s while connected. Returns undefined when disconnected.
187
236
  */
188
- declare function useReactorMessage(handler: (message: any, scope: MessageScope) => void): void;
237
+ declare function useStats(): ConnectionStats | undefined;
189
238
 
190
239
  /**
191
240
  * ⚠️ INSECURE: Fetches a JWT token directly from the client.
@@ -200,4 +249,4 @@ declare function useReactorMessage(handler: (message: any, scope: MessageScope)
200
249
  */
201
250
  declare function fetchInsecureJwtToken(apiKey: string, coordinatorUrl?: string): Promise<string>;
202
251
 
203
- export { ConflictError, type MessageScope, type Options, PROD_COORDINATOR_URL, Reactor, ReactorController, type ReactorControllerProps, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, WebcamStream, fetchInsecureJwtToken, useReactor, useReactorMessage, useReactorStore };
252
+ export { ConflictError, type ConnectOptions, type ConnectionStats, type MessageScope, type Options, PROD_COORDINATOR_URL, Reactor, type ReactorConnectOptions, ReactorController, type ReactorControllerProps, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, WebcamStream, fetchInsecureJwtToken, useReactor, useReactorInternalMessage, useReactorMessage, useReactorStore, useStats };