@reactor-team/js-sdk 1.0.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/.env.example ADDED
@@ -0,0 +1,2 @@
1
+ # NPM authentication token for publishing packages
2
+ NPM_TOKEN=your_npm_token_here
@@ -0,0 +1,7 @@
1
+ node_modules/
2
+ dist/
3
+ build/
4
+ coverage/
5
+ *.min.js
6
+ *.min.css
7
+ pnpm-lock.yaml
package/.prettierrc ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "semi": true,
3
+ "trailingComma": "es5",
4
+ "singleQuote": false,
5
+ "printWidth": 80,
6
+ "tabWidth": 2,
7
+ "useTabs": false,
8
+ "endOfLine": "lf"
9
+ }
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Reactor Frontend SDK
2
+
3
+ ## Overview
4
+
5
+ This is the frontend SDK for Reactor. It provides a set of tools and utilities to build frontend applications that can use the Reactor platform.
6
+
7
+ There are two main ways to use the frontend SDK:
8
+
9
+ 1. **Imperative API**: Use it in any TS/JS application.
10
+ 2. **React API**: Use it in a React applications.
11
+
12
+ ## Building the SDK
13
+
14
+ Set up the environment variables:
15
+
16
+ ```bash
17
+ cp .env.example .env
18
+ ```
19
+
20
+ Then add your NPM_TOKEN to the .env file.
21
+
22
+ Build the SDK:
23
+
24
+ ```bash
25
+ ./publish_package.sh
26
+ ```
@@ -0,0 +1,151 @@
1
+ import { z } from 'zod';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
+ import react, { ReactNode } from 'react';
4
+ import { RemoteVideoTrack } from 'livekit-client';
5
+
6
+ type ReactorStatus = "disconnected" | "connecting" | "waiting" | "ready";
7
+ interface ReactorWaitingInfo {
8
+ position?: number;
9
+ estimatedWaitTime?: number;
10
+ averageWaitTime?: number;
11
+ }
12
+ interface ReactorError {
13
+ code: string;
14
+ message: string;
15
+ timestamp: number;
16
+ recoverable: boolean;
17
+ component: "coordinator" | "gpu" | "livekit";
18
+ retryAfter?: number;
19
+ }
20
+ interface ReactorState$1 {
21
+ status: ReactorStatus;
22
+ waitingInfo?: ReactorWaitingInfo;
23
+ lastError?: ReactorError;
24
+ }
25
+ type ReactorEvent = "statusChanged" | "waitingInfoChanged" | "newMessage" | "fps" | "streamChanged" | "error";
26
+
27
+ declare const OptionsSchema: z.ZodObject<{
28
+ directConnection: z.ZodOptional<z.ZodObject<{
29
+ livekitJwtToken: z.ZodString;
30
+ livekitWsUrl: z.ZodString;
31
+ }, z.core.$strip>>;
32
+ insecureApiKey: z.ZodOptional<z.ZodString>;
33
+ jwtToken: z.ZodOptional<z.ZodString>;
34
+ coordinatorUrl: z.ZodOptional<z.ZodString>;
35
+ modelName: z.ZodString;
36
+ }, z.core.$strip>;
37
+ type Options = z.input<typeof OptionsSchema>;
38
+ type EventHandler = (...args: any[]) => void;
39
+ declare class Reactor {
40
+ private coordinatorClient;
41
+ private machineClient;
42
+ private status;
43
+ private coordinatorUrl;
44
+ private lastError?;
45
+ private waitingInfo?;
46
+ private jwtToken?;
47
+ private insecureApiKey?;
48
+ private directConnection?;
49
+ private modelName;
50
+ private modelVersion;
51
+ constructor(options: Options);
52
+ private eventListeners;
53
+ on(event: ReactorEvent, handler: EventHandler): void;
54
+ off(event: ReactorEvent, handler: EventHandler): void;
55
+ emit(event: ReactorEvent, ...args: any[]): void;
56
+ /**
57
+ * Public method to send a message to the machine.
58
+ * Automatically wraps the message in an application message.
59
+ * @param message The message to send to the machine.
60
+ * @throws Error if not in ready state
61
+ */
62
+ sendMessage(message: any): void;
63
+ /**
64
+ * Connects to the machine via LiveKit and waits for the gpu machine to be ready.
65
+ * Once the machine is ready, the Reactor will establish the LiveKit connection.
66
+ * @param livekitJwtToken The JWT token for LiveKit authentication
67
+ * @param livekitWsUrl The WebSocket URL for LiveKit connection
68
+ */
69
+ private connectToGPUMachine;
70
+ /**
71
+ * Connects to the coordinator and waits for a GPU to be assigned.
72
+ * Once a GPU is assigned, the Reactor will connect to the gpu machine via LiveKit.
73
+ */
74
+ connect(): Promise<void>;
75
+ /**
76
+ * Disconnects from the coordinator and the gpu machine.
77
+ * Ensures cleanup completes even if individual disconnections fail.
78
+ */
79
+ disconnect(): Promise<void>;
80
+ private setStatus;
81
+ private setWaitingInfo;
82
+ getStatus(): ReactorStatus;
83
+ /**
84
+ * Get the current state including status, error, and waiting info
85
+ */
86
+ getState(): ReactorState$1;
87
+ /**
88
+ * Get waiting information when status is 'waiting'
89
+ */
90
+ getWaitingInfo(): ReactorWaitingInfo | undefined;
91
+ /**
92
+ * Get the last error that occurred
93
+ */
94
+ getLastError(): ReactorError | undefined;
95
+ /**
96
+ * Create and store an error
97
+ */
98
+ private createError;
99
+ }
100
+
101
+ interface ReactorState {
102
+ status: ReactorStatus;
103
+ videoTrack: RemoteVideoTrack | null;
104
+ fps?: number;
105
+ waitingInfo?: ReactorWaitingInfo;
106
+ lastError?: ReactorError;
107
+ }
108
+ interface ReactorActions {
109
+ sendMessage(message: any): void;
110
+ connect(): Promise<void>;
111
+ disconnect(): Promise<void>;
112
+ }
113
+ interface ReactorInternalState {
114
+ reactor: Reactor;
115
+ }
116
+ type ReactorStore = ReactorState & ReactorActions & {
117
+ internal: ReactorInternalState;
118
+ };
119
+ interface ReactorInitializationProps extends Options {
120
+ }
121
+
122
+ interface ReactorProviderProps extends ReactorInitializationProps {
123
+ autoConnect?: boolean;
124
+ children: ReactNode;
125
+ }
126
+ declare function ReactorProvider({ children, autoConnect, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
127
+ declare function useReactorStore<T = ReactorStore>(selector: (state: ReactorStore) => T): T;
128
+
129
+ interface ReactorViewProps {
130
+ width?: number;
131
+ height?: number;
132
+ className?: string;
133
+ style?: react.CSSProperties;
134
+ }
135
+ declare function ReactorView({ width, height, className, style, }: ReactorViewProps): react_jsx_runtime.JSX.Element;
136
+
137
+ /**
138
+ * Generic hook for accessing selected parts of the Reactor store.
139
+ *
140
+ * @param selector - A function that selects part of the store state.
141
+ * @returns The selected slice from the store.
142
+ */
143
+ declare function useReactor<T>(selector: (state: ReactorStore) => T): T;
144
+ /**
145
+ * Hook for handling message subscriptions with proper React lifecycle management.
146
+ *
147
+ * @param handler - The message handler function
148
+ */
149
+ declare function useReactorMessage(handler: (message: any) => void): void;
150
+
151
+ export { type Options, Reactor, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, type ReactorWaitingInfo, useReactor, useReactorMessage, useReactorStore };
@@ -0,0 +1,151 @@
1
+ import { z } from 'zod';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
+ import react, { ReactNode } from 'react';
4
+ import { RemoteVideoTrack } from 'livekit-client';
5
+
6
+ type ReactorStatus = "disconnected" | "connecting" | "waiting" | "ready";
7
+ interface ReactorWaitingInfo {
8
+ position?: number;
9
+ estimatedWaitTime?: number;
10
+ averageWaitTime?: number;
11
+ }
12
+ interface ReactorError {
13
+ code: string;
14
+ message: string;
15
+ timestamp: number;
16
+ recoverable: boolean;
17
+ component: "coordinator" | "gpu" | "livekit";
18
+ retryAfter?: number;
19
+ }
20
+ interface ReactorState$1 {
21
+ status: ReactorStatus;
22
+ waitingInfo?: ReactorWaitingInfo;
23
+ lastError?: ReactorError;
24
+ }
25
+ type ReactorEvent = "statusChanged" | "waitingInfoChanged" | "newMessage" | "fps" | "streamChanged" | "error";
26
+
27
+ declare const OptionsSchema: z.ZodObject<{
28
+ directConnection: z.ZodOptional<z.ZodObject<{
29
+ livekitJwtToken: z.ZodString;
30
+ livekitWsUrl: z.ZodString;
31
+ }, z.core.$strip>>;
32
+ insecureApiKey: z.ZodOptional<z.ZodString>;
33
+ jwtToken: z.ZodOptional<z.ZodString>;
34
+ coordinatorUrl: z.ZodOptional<z.ZodString>;
35
+ modelName: z.ZodString;
36
+ }, z.core.$strip>;
37
+ type Options = z.input<typeof OptionsSchema>;
38
+ type EventHandler = (...args: any[]) => void;
39
+ declare class Reactor {
40
+ private coordinatorClient;
41
+ private machineClient;
42
+ private status;
43
+ private coordinatorUrl;
44
+ private lastError?;
45
+ private waitingInfo?;
46
+ private jwtToken?;
47
+ private insecureApiKey?;
48
+ private directConnection?;
49
+ private modelName;
50
+ private modelVersion;
51
+ constructor(options: Options);
52
+ private eventListeners;
53
+ on(event: ReactorEvent, handler: EventHandler): void;
54
+ off(event: ReactorEvent, handler: EventHandler): void;
55
+ emit(event: ReactorEvent, ...args: any[]): void;
56
+ /**
57
+ * Public method to send a message to the machine.
58
+ * Automatically wraps the message in an application message.
59
+ * @param message The message to send to the machine.
60
+ * @throws Error if not in ready state
61
+ */
62
+ sendMessage(message: any): void;
63
+ /**
64
+ * Connects to the machine via LiveKit and waits for the gpu machine to be ready.
65
+ * Once the machine is ready, the Reactor will establish the LiveKit connection.
66
+ * @param livekitJwtToken The JWT token for LiveKit authentication
67
+ * @param livekitWsUrl The WebSocket URL for LiveKit connection
68
+ */
69
+ private connectToGPUMachine;
70
+ /**
71
+ * Connects to the coordinator and waits for a GPU to be assigned.
72
+ * Once a GPU is assigned, the Reactor will connect to the gpu machine via LiveKit.
73
+ */
74
+ connect(): Promise<void>;
75
+ /**
76
+ * Disconnects from the coordinator and the gpu machine.
77
+ * Ensures cleanup completes even if individual disconnections fail.
78
+ */
79
+ disconnect(): Promise<void>;
80
+ private setStatus;
81
+ private setWaitingInfo;
82
+ getStatus(): ReactorStatus;
83
+ /**
84
+ * Get the current state including status, error, and waiting info
85
+ */
86
+ getState(): ReactorState$1;
87
+ /**
88
+ * Get waiting information when status is 'waiting'
89
+ */
90
+ getWaitingInfo(): ReactorWaitingInfo | undefined;
91
+ /**
92
+ * Get the last error that occurred
93
+ */
94
+ getLastError(): ReactorError | undefined;
95
+ /**
96
+ * Create and store an error
97
+ */
98
+ private createError;
99
+ }
100
+
101
+ interface ReactorState {
102
+ status: ReactorStatus;
103
+ videoTrack: RemoteVideoTrack | null;
104
+ fps?: number;
105
+ waitingInfo?: ReactorWaitingInfo;
106
+ lastError?: ReactorError;
107
+ }
108
+ interface ReactorActions {
109
+ sendMessage(message: any): void;
110
+ connect(): Promise<void>;
111
+ disconnect(): Promise<void>;
112
+ }
113
+ interface ReactorInternalState {
114
+ reactor: Reactor;
115
+ }
116
+ type ReactorStore = ReactorState & ReactorActions & {
117
+ internal: ReactorInternalState;
118
+ };
119
+ interface ReactorInitializationProps extends Options {
120
+ }
121
+
122
+ interface ReactorProviderProps extends ReactorInitializationProps {
123
+ autoConnect?: boolean;
124
+ children: ReactNode;
125
+ }
126
+ declare function ReactorProvider({ children, autoConnect, ...props }: ReactorProviderProps): react_jsx_runtime.JSX.Element;
127
+ declare function useReactorStore<T = ReactorStore>(selector: (state: ReactorStore) => T): T;
128
+
129
+ interface ReactorViewProps {
130
+ width?: number;
131
+ height?: number;
132
+ className?: string;
133
+ style?: react.CSSProperties;
134
+ }
135
+ declare function ReactorView({ width, height, className, style, }: ReactorViewProps): react_jsx_runtime.JSX.Element;
136
+
137
+ /**
138
+ * Generic hook for accessing selected parts of the Reactor store.
139
+ *
140
+ * @param selector - A function that selects part of the store state.
141
+ * @returns The selected slice from the store.
142
+ */
143
+ declare function useReactor<T>(selector: (state: ReactorStore) => T): T;
144
+ /**
145
+ * Hook for handling message subscriptions with proper React lifecycle management.
146
+ *
147
+ * @param handler - The message handler function
148
+ */
149
+ declare function useReactorMessage(handler: (message: any) => void): void;
150
+
151
+ export { type Options, Reactor, type ReactorError, type ReactorEvent, ReactorProvider, type ReactorState$1 as ReactorState, type ReactorStatus, ReactorView, type ReactorViewProps, type ReactorWaitingInfo, useReactor, useReactorMessage, useReactorStore };