@cmdop/react 0.1.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.
@@ -0,0 +1,538 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CMDOPConfig } from '@cmdop/core';
3
+ export { API_BASE_URL, AuthenticationError, CMDOPConfig, CMDOPError, CancelledError, ConnectionError, DEFAULT_CONFIG, FileInfo, MachinesModule, NotFoundError, PermissionError, ResourceExhaustedError, SessionError, SessionInfo, SessionState, SystemModule, TimeoutError, TransportMode, UnavailableError, VERSION, WorkspacesModule, api, machines, system, workspaces } from '@cmdop/core';
4
+ import { ReactNode } from 'react';
5
+ import { SWRConfiguration } from 'swr';
6
+
7
+ /**
8
+ * Centrifugo WebSocket client for CMDOP
9
+ * Simplified implementation based on @djangocfg/centrifugo patterns
10
+ */
11
+ interface CMDOPWebSocketConfig {
12
+ url: string;
13
+ getToken: () => Promise<string>;
14
+ timeout?: number;
15
+ debug?: boolean;
16
+ }
17
+ interface ConnectionState {
18
+ isConnected: boolean;
19
+ isConnecting: boolean;
20
+ error: Error | null;
21
+ }
22
+ type ConnectionStateListener = (state: ConnectionState) => void;
23
+ /**
24
+ * CMDOP WebSocket client wrapping Centrifuge
25
+ */
26
+ declare class CMDOPWebSocketClient {
27
+ private config;
28
+ private centrifuge;
29
+ private subscriptions;
30
+ private stateListeners;
31
+ private state;
32
+ constructor(config: CMDOPWebSocketConfig);
33
+ /**
34
+ * Connect to Centrifugo server
35
+ */
36
+ connect(): Promise<void>;
37
+ /**
38
+ * Disconnect from server
39
+ */
40
+ disconnect(): void;
41
+ /**
42
+ * Subscribe to a channel
43
+ */
44
+ subscribe<T = unknown>(channel: string, onPublication: (data: T) => void, onError?: (error: Error) => void): () => void;
45
+ /**
46
+ * Unsubscribe from a channel
47
+ */
48
+ unsubscribe(channel: string): void;
49
+ /**
50
+ * Make an RPC call via Centrifugo
51
+ */
52
+ rpc<TRequest, TResponse>(method: string, data: TRequest): Promise<TResponse>;
53
+ /**
54
+ * Publish to a channel (fire-and-forget)
55
+ */
56
+ publish<T>(channel: string, data: T): Promise<void>;
57
+ /**
58
+ * Get current connection state
59
+ */
60
+ getState(): ConnectionState;
61
+ /**
62
+ * Add state change listener
63
+ */
64
+ onStateChange(listener: ConnectionStateListener): () => void;
65
+ private updateState;
66
+ }
67
+
68
+ interface WebSocketContextValue {
69
+ client: CMDOPWebSocketClient | null;
70
+ isConnected: boolean;
71
+ isConnecting: boolean;
72
+ error: Error | null;
73
+ connect: () => Promise<void>;
74
+ disconnect: () => void;
75
+ }
76
+ interface WebSocketProviderProps {
77
+ children: ReactNode;
78
+ /**
79
+ * WebSocket URL (e.g., wss://api.cmdop.com/connection/websocket)
80
+ */
81
+ url: string;
82
+ /**
83
+ * Function to get auth token
84
+ */
85
+ getToken: () => Promise<string>;
86
+ /**
87
+ * Auto-connect on mount
88
+ * @default true
89
+ */
90
+ autoConnect?: boolean;
91
+ /**
92
+ * Enable debug logging
93
+ * @default false
94
+ */
95
+ debug?: boolean;
96
+ }
97
+ declare function WebSocketProvider({ children, url, getToken, autoConnect, debug, }: WebSocketProviderProps): react_jsx_runtime.JSX.Element;
98
+ /**
99
+ * Hook to access WebSocket context
100
+ */
101
+ declare function useWebSocket(): WebSocketContextValue;
102
+
103
+ /**
104
+ * Centrifugo React hooks for CMDOP
105
+ */
106
+ interface UseSubscriptionOptions<T> {
107
+ /**
108
+ * Channel to subscribe to
109
+ */
110
+ channel: string;
111
+ /**
112
+ * Enable/disable subscription
113
+ * @default true
114
+ */
115
+ enabled?: boolean;
116
+ /**
117
+ * Callback when data is received
118
+ */
119
+ onData?: (data: T) => void;
120
+ /**
121
+ * Callback on error
122
+ */
123
+ onError?: (error: Error) => void;
124
+ }
125
+ interface UseSubscriptionResult<T> {
126
+ /**
127
+ * Last received data
128
+ */
129
+ data: T | null;
130
+ /**
131
+ * Subscription error
132
+ */
133
+ error: Error | null;
134
+ /**
135
+ * Whether subscribed
136
+ */
137
+ isSubscribed: boolean;
138
+ }
139
+ /**
140
+ * Subscribe to a Centrifugo channel
141
+ */
142
+ declare function useSubscription<T = unknown>(options: UseSubscriptionOptions<T>): UseSubscriptionResult<T>;
143
+ interface UseRPCOptions {
144
+ /**
145
+ * Callback on error
146
+ */
147
+ onError?: (error: Error) => void;
148
+ }
149
+ interface UseRPCResult {
150
+ /**
151
+ * Make an RPC call
152
+ */
153
+ call: <TRequest, TResponse>(method: string, data: TRequest) => Promise<TResponse>;
154
+ /**
155
+ * Loading state
156
+ */
157
+ isLoading: boolean;
158
+ /**
159
+ * Last error
160
+ */
161
+ error: Error | null;
162
+ /**
163
+ * Reset error state
164
+ */
165
+ reset: () => void;
166
+ }
167
+ /**
168
+ * Make RPC calls via Centrifugo
169
+ */
170
+ declare function useRPC(options?: UseRPCOptions): UseRPCResult;
171
+
172
+ /**
173
+ * useTerminal hook - Real-time terminal connection via WebSocket
174
+ */
175
+ interface TerminalOutput {
176
+ /**
177
+ * Output data (may be base64 encoded)
178
+ */
179
+ data: string;
180
+ /**
181
+ * Timestamp
182
+ */
183
+ timestamp?: string;
184
+ }
185
+ interface TerminalStatus {
186
+ /**
187
+ * Session state
188
+ */
189
+ state: 'active' | 'closed' | 'error';
190
+ /**
191
+ * Exit code if closed
192
+ */
193
+ exitCode?: number;
194
+ }
195
+ interface UseTerminalOptions {
196
+ /**
197
+ * Session ID to connect to
198
+ */
199
+ sessionId: string;
200
+ /**
201
+ * Enable connection
202
+ * @default true
203
+ */
204
+ enabled?: boolean;
205
+ /**
206
+ * Callback when output is received
207
+ */
208
+ onOutput?: (data: string) => void;
209
+ /**
210
+ * Callback when status changes
211
+ */
212
+ onStatus?: (status: TerminalStatus) => void;
213
+ /**
214
+ * Callback on error
215
+ */
216
+ onError?: (error: Error) => void;
217
+ }
218
+ interface UseTerminalResult {
219
+ /**
220
+ * Whether connected to terminal
221
+ */
222
+ isConnected: boolean;
223
+ /**
224
+ * Whether connecting
225
+ */
226
+ isConnecting: boolean;
227
+ /**
228
+ * Connection/operation error
229
+ */
230
+ error: Error | null;
231
+ /**
232
+ * Accumulated output
233
+ */
234
+ output: string;
235
+ /**
236
+ * Current terminal status
237
+ */
238
+ status: TerminalStatus | null;
239
+ /**
240
+ * Send input to terminal
241
+ */
242
+ sendInput: (data: string) => Promise<void>;
243
+ /**
244
+ * Resize terminal
245
+ */
246
+ resize: (cols: number, rows: number) => Promise<void>;
247
+ /**
248
+ * Send signal (e.g., SIGINT)
249
+ */
250
+ signal: (sig: number | string) => Promise<void>;
251
+ /**
252
+ * Clear accumulated output
253
+ */
254
+ clear: () => void;
255
+ }
256
+ /**
257
+ * Hook for real-time terminal interaction via WebSocket
258
+ *
259
+ * @example
260
+ * ```tsx
261
+ * function Terminal({ sessionId }: { sessionId: string }) {
262
+ * const {
263
+ * isConnected,
264
+ * output,
265
+ * sendInput,
266
+ * resize,
267
+ * } = useTerminal({
268
+ * sessionId,
269
+ * onOutput: (data) => console.log('Received:', data),
270
+ * });
271
+ *
272
+ * return (
273
+ * <div>
274
+ * <pre>{output}</pre>
275
+ * <input onKeyDown={(e) => {
276
+ * if (e.key === 'Enter') sendInput(e.currentTarget.value + '\n');
277
+ * }} />
278
+ * </div>
279
+ * );
280
+ * }
281
+ * ```
282
+ */
283
+ declare function useTerminal(options: UseTerminalOptions): UseTerminalResult;
284
+
285
+ /**
286
+ * useAgent hook - AI agent execution via WebSocket
287
+ */
288
+ type AgentEventType = 'token' | 'tool_call' | 'tool_result' | 'thinking' | 'error' | 'done';
289
+ interface AgentEvent {
290
+ /**
291
+ * Event type
292
+ */
293
+ type: AgentEventType;
294
+ /**
295
+ * Event payload
296
+ */
297
+ data: unknown;
298
+ /**
299
+ * Timestamp
300
+ */
301
+ timestamp?: string;
302
+ }
303
+ interface AgentTokenEvent {
304
+ type: 'token';
305
+ data: {
306
+ text: string;
307
+ };
308
+ }
309
+ interface AgentToolCallEvent {
310
+ type: 'tool_call';
311
+ data: {
312
+ id: string;
313
+ name: string;
314
+ input: unknown;
315
+ };
316
+ }
317
+ interface AgentToolResultEvent {
318
+ type: 'tool_result';
319
+ data: {
320
+ id: string;
321
+ output: unknown;
322
+ error?: string;
323
+ };
324
+ }
325
+ interface AgentDoneEvent {
326
+ type: 'done';
327
+ data: {
328
+ text: string;
329
+ usage?: {
330
+ promptTokens: number;
331
+ completionTokens: number;
332
+ totalTokens: number;
333
+ };
334
+ durationMs: number;
335
+ };
336
+ }
337
+ interface AgentErrorEvent {
338
+ type: 'error';
339
+ data: {
340
+ message: string;
341
+ code?: string;
342
+ };
343
+ }
344
+ interface UseAgentOptions {
345
+ /**
346
+ * Session ID for agent context
347
+ */
348
+ sessionId: string;
349
+ /**
350
+ * Enable connection
351
+ * @default true
352
+ */
353
+ enabled?: boolean;
354
+ /**
355
+ * Callback for streaming tokens
356
+ */
357
+ onToken?: (text: string) => void;
358
+ /**
359
+ * Callback for tool calls
360
+ */
361
+ onToolCall?: (toolCall: AgentToolCallEvent['data']) => void;
362
+ /**
363
+ * Callback for tool results
364
+ */
365
+ onToolResult?: (result: AgentToolResultEvent['data']) => void;
366
+ /**
367
+ * Callback when agent completes
368
+ */
369
+ onDone?: (result: AgentDoneEvent['data']) => void;
370
+ /**
371
+ * Callback on error
372
+ */
373
+ onError?: (error: Error) => void;
374
+ }
375
+ interface UseAgentResult {
376
+ /**
377
+ * Run agent with prompt
378
+ */
379
+ run: (prompt: string, options?: RunAgentOptions) => Promise<string>;
380
+ /**
381
+ * Whether agent is running
382
+ */
383
+ isRunning: boolean;
384
+ /**
385
+ * Streaming text (accumulated tokens)
386
+ */
387
+ streamingText: string;
388
+ /**
389
+ * Final result text
390
+ */
391
+ result: string | null;
392
+ /**
393
+ * Current tool calls in progress
394
+ */
395
+ toolCalls: AgentToolCallEvent['data'][];
396
+ /**
397
+ * Error if any
398
+ */
399
+ error: Error | null;
400
+ /**
401
+ * Reset state
402
+ */
403
+ reset: () => void;
404
+ /**
405
+ * Cancel running agent
406
+ */
407
+ cancel: () => Promise<void>;
408
+ }
409
+ interface RunAgentOptions {
410
+ /**
411
+ * Agent mode
412
+ */
413
+ mode?: 'chat' | 'terminal' | 'command' | 'router' | 'planner';
414
+ /**
415
+ * Timeout in seconds
416
+ */
417
+ timeoutSeconds?: number;
418
+ /**
419
+ * JSON schema for structured output
420
+ */
421
+ outputSchema?: string;
422
+ }
423
+ /**
424
+ * Hook for AI agent execution with streaming via WebSocket
425
+ *
426
+ * @example
427
+ * ```tsx
428
+ * function AgentChat({ sessionId }: { sessionId: string }) {
429
+ * const {
430
+ * run,
431
+ * isRunning,
432
+ * streamingText,
433
+ * result,
434
+ * } = useAgent({
435
+ * sessionId,
436
+ * onToken: (text) => console.log('Token:', text),
437
+ * onDone: (result) => console.log('Done:', result),
438
+ * });
439
+ *
440
+ * return (
441
+ * <div>
442
+ * <pre>{streamingText || result}</pre>
443
+ * <button onClick={() => run('Hello!')}>Send</button>
444
+ * </div>
445
+ * );
446
+ * }
447
+ * ```
448
+ */
449
+ declare function useAgent(options: UseAgentOptions): UseAgentResult;
450
+
451
+ interface CMDOPContextValue {
452
+ config: CMDOPConfig;
453
+ isAuthenticated: boolean;
454
+ setToken: (token: string) => void;
455
+ }
456
+ interface CMDOPProviderProps {
457
+ children: ReactNode;
458
+ apiKey?: string;
459
+ token?: string;
460
+ }
461
+ /**
462
+ * Provider for HTTP API authentication
463
+ */
464
+ declare function CMDOPProvider({ children, apiKey, token }: CMDOPProviderProps): react_jsx_runtime.JSX.Element;
465
+ /**
466
+ * Hook to access HTTP API context
467
+ */
468
+ declare function useCMDOP(): CMDOPContextValue;
469
+
470
+ /**
471
+ * Machine type from API
472
+ */
473
+ interface Machine {
474
+ id: string;
475
+ name: string;
476
+ hostname?: string;
477
+ status?: string;
478
+ workspace?: string;
479
+ [key: string]: unknown;
480
+ }
481
+ /**
482
+ * Workspace type from API
483
+ */
484
+ interface Workspace {
485
+ id: string;
486
+ name: string;
487
+ [key: string]: unknown;
488
+ }
489
+ /**
490
+ * Hook for fetching machines list
491
+ */
492
+ interface UseMachinesOptions extends SWRConfiguration {
493
+ page?: number;
494
+ pageSize?: number;
495
+ }
496
+ interface UseMachinesResult {
497
+ machines: Machine[];
498
+ total: number;
499
+ isLoading: boolean;
500
+ isValidating: boolean;
501
+ error: Error | undefined;
502
+ refetch: () => void;
503
+ }
504
+ declare function useMachines(options?: UseMachinesOptions): UseMachinesResult;
505
+ /**
506
+ * Hook for fetching single machine
507
+ */
508
+ interface UseMachineResult {
509
+ machine: Machine | null;
510
+ isLoading: boolean;
511
+ error: Error | undefined;
512
+ refetch: () => void;
513
+ }
514
+ declare function useMachine(machineId: string | undefined, options?: SWRConfiguration): UseMachineResult;
515
+ /**
516
+ * Hook for fetching workspaces list
517
+ */
518
+ interface UseWorkspacesResult {
519
+ workspaces: Workspace[];
520
+ total: number;
521
+ isLoading: boolean;
522
+ isValidating: boolean;
523
+ error: Error | undefined;
524
+ refetch: () => void;
525
+ }
526
+ declare function useWorkspaces(options?: SWRConfiguration): UseWorkspacesResult;
527
+ /**
528
+ * Hook for fetching single workspace
529
+ */
530
+ interface UseWorkspaceResult {
531
+ workspace: Workspace | null;
532
+ isLoading: boolean;
533
+ error: Error | undefined;
534
+ refetch: () => void;
535
+ }
536
+ declare function useWorkspace(workspaceId: string | undefined, options?: SWRConfiguration): UseWorkspaceResult;
537
+
538
+ export { type AgentDoneEvent, type AgentErrorEvent, type AgentEvent, type AgentEventType, type AgentTokenEvent, type AgentToolCallEvent, type AgentToolResultEvent, CMDOPProvider, type CMDOPProviderProps, CMDOPWebSocketClient, type CMDOPWebSocketConfig, type ConnectionState, type Machine, type RunAgentOptions, type TerminalOutput, type TerminalStatus, type UseAgentOptions, type UseAgentResult, type UseMachineResult, type UseMachinesOptions, type UseMachinesResult, type UseRPCOptions, type UseRPCResult, type UseSubscriptionOptions, type UseSubscriptionResult, type UseTerminalOptions, type UseTerminalResult, type UseWorkspaceResult, type UseWorkspacesResult, WebSocketProvider, type WebSocketProviderProps, type Workspace, useAgent, useCMDOP, useMachine, useMachines, useRPC, useSubscription, useTerminal, useWebSocket, useWorkspace, useWorkspaces };