@superatomai/sdk-web 0.0.5

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.cjs ADDED
@@ -0,0 +1,1611 @@
1
+ 'use strict';
2
+
3
+ var zod = require('zod');
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var ExpressionSchema = zod.z.object({
11
+ $exp: zod.z.string(),
12
+ $deps: zod.z.array(zod.z.string()).optional()
13
+ });
14
+ var BindingSchema = zod.z.object({
15
+ $bind: zod.z.string(),
16
+ $transform: zod.z.array(
17
+ zod.z.object({
18
+ name: zod.z.string(),
19
+ args: zod.z.array(zod.z.any()).optional()
20
+ })
21
+ ).optional()
22
+ });
23
+ var ForDirectiveSchema = zod.z.object({
24
+ in: zod.z.union([ExpressionSchema, BindingSchema, zod.z.string()]),
25
+ as: zod.z.string(),
26
+ key: zod.z.string().optional(),
27
+ index: zod.z.string().optional()
28
+ });
29
+ var QuerySpecSchema = zod.z.object({
30
+ graphql: zod.z.string().optional(),
31
+ sql: zod.z.string().optional(),
32
+ variables: zod.z.record(zod.z.string(), zod.z.any()).optional(),
33
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional(),
34
+ key: zod.z.string().optional(),
35
+ refetchPolicy: zod.z.enum(["cache-first", "network-only", "cache-and-network"]).optional(),
36
+ dependencies: zod.z.array(zod.z.string()).optional()
37
+ });
38
+ var UIElementSchema = zod.z.lazy(
39
+ () => zod.z.object({
40
+ id: zod.z.string(),
41
+ type: zod.z.string(),
42
+ key: zod.z.union([zod.z.string(), ExpressionSchema]).optional(),
43
+ props: zod.z.record(zod.z.string(), zod.z.any()).optional(),
44
+ query: QuerySpecSchema.optional(),
45
+ if: ExpressionSchema.optional(),
46
+ elseIf: ExpressionSchema.optional(),
47
+ for: ForDirectiveSchema.optional(),
48
+ "link-to": zod.z.union([
49
+ zod.z.string(),
50
+ ExpressionSchema,
51
+ BindingSchema,
52
+ zod.z.object({
53
+ ui: zod.z.union([zod.z.string(), ExpressionSchema, BindingSchema]),
54
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional()
55
+ })
56
+ ]).optional(),
57
+ _meta: zod.z.object({
58
+ id: zod.z.string().optional(),
59
+ version: zod.z.string().optional(),
60
+ created: zod.z.string().optional(),
61
+ lastModified: zod.z.string().optional()
62
+ }).optional(),
63
+ children: zod.z.any().optional(),
64
+ else: UIElementSchema.optional(),
65
+ slots: zod.z.record(zod.z.string(), zod.z.union([UIElementSchema, zod.z.array(UIElementSchema)])).optional(),
66
+ platform: zod.z.object({
67
+ web: zod.z.any().optional(),
68
+ ios: zod.z.any().optional(),
69
+ android: zod.z.any().optional()
70
+ }).optional()
71
+ })
72
+ );
73
+ var UIComponentSchema = zod.z.object({
74
+ id: zod.z.string(),
75
+ name: zod.z.string().optional(),
76
+ props: zod.z.record(zod.z.string(), zod.z.any()).optional(),
77
+ states: zod.z.record(zod.z.string(), zod.z.any()).optional(),
78
+ methods: zod.z.record(
79
+ zod.z.string(),
80
+ zod.z.object({
81
+ fn: zod.z.string(),
82
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional()
83
+ })
84
+ ).optional(),
85
+ effects: zod.z.array(
86
+ zod.z.object({
87
+ fn: zod.z.string(),
88
+ deps: zod.z.array(zod.z.string()).optional()
89
+ })
90
+ ).optional(),
91
+ data: zod.z.record(zod.z.string(), zod.z.any()).optional(),
92
+ render: UIElementSchema,
93
+ query: QuerySpecSchema.optional()
94
+ });
95
+ var DSLRendererPropsSchema = zod.z.object({
96
+ dsl: UIComponentSchema,
97
+ data: zod.z.record(zod.z.string(), zod.z.any()).optional(),
98
+ context: zod.z.record(zod.z.string(), zod.z.any()).optional()
99
+ });
100
+ var ExpressionSchema2 = zod.z.object({
101
+ $exp: zod.z.string(),
102
+ $deps: zod.z.array(zod.z.string()).optional()
103
+ });
104
+ var BindingSchema2 = zod.z.object({
105
+ $bind: zod.z.string(),
106
+ $transform: zod.z.array(
107
+ zod.z.object({
108
+ name: zod.z.string(),
109
+ args: zod.z.array(zod.z.any()).optional()
110
+ })
111
+ ).optional()
112
+ });
113
+ var ForDirectiveSchema2 = zod.z.object({
114
+ in: zod.z.union([ExpressionSchema2, BindingSchema2, zod.z.string()]),
115
+ as: zod.z.string(),
116
+ key: zod.z.string().optional(),
117
+ index: zod.z.string().optional()
118
+ });
119
+ var QuerySpecSchema2 = zod.z.object({
120
+ graphql: zod.z.string().optional(),
121
+ sql: zod.z.string().optional(),
122
+ variables: zod.z.record(zod.z.string(), zod.z.any()).optional(),
123
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional(),
124
+ key: zod.z.string().optional(),
125
+ refetchPolicy: zod.z.enum(["cache-first", "network-only", "cache-and-network"]).optional(),
126
+ dependencies: zod.z.array(zod.z.string()).optional()
127
+ });
128
+ var UIElementSchema2 = zod.z.lazy(
129
+ () => zod.z.object({
130
+ id: zod.z.string(),
131
+ type: zod.z.string(),
132
+ key: zod.z.union([zod.z.string(), ExpressionSchema2]).optional(),
133
+ props: zod.z.record(zod.z.string(), zod.z.any()).optional(),
134
+ query: QuerySpecSchema2.optional(),
135
+ if: ExpressionSchema2.optional(),
136
+ elseIf: ExpressionSchema2.optional(),
137
+ for: ForDirectiveSchema2.optional(),
138
+ "link-to": zod.z.union([
139
+ zod.z.string(),
140
+ ExpressionSchema2,
141
+ BindingSchema2,
142
+ zod.z.object({
143
+ ui: zod.z.union([zod.z.string(), ExpressionSchema2, BindingSchema2]),
144
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional()
145
+ })
146
+ ]).optional(),
147
+ _meta: zod.z.object({
148
+ id: zod.z.string().optional(),
149
+ version: zod.z.string().optional(),
150
+ created: zod.z.string().optional(),
151
+ lastModified: zod.z.string().optional()
152
+ }).optional(),
153
+ children: zod.z.any().optional(),
154
+ else: UIElementSchema2.optional(),
155
+ slots: zod.z.record(zod.z.string(), zod.z.union([UIElementSchema2, zod.z.array(UIElementSchema2)])).optional(),
156
+ platform: zod.z.object({
157
+ web: zod.z.any().optional(),
158
+ ios: zod.z.any().optional(),
159
+ android: zod.z.any().optional()
160
+ }).optional()
161
+ })
162
+ );
163
+ var UIComponentSchema2 = zod.z.object({
164
+ id: zod.z.string(),
165
+ name: zod.z.string().optional(),
166
+ props: zod.z.record(zod.z.string(), zod.z.any()).optional(),
167
+ states: zod.z.record(zod.z.string(), zod.z.any()).optional(),
168
+ methods: zod.z.record(
169
+ zod.z.string(),
170
+ zod.z.object({
171
+ fn: zod.z.string(),
172
+ params: zod.z.record(zod.z.string(), zod.z.any()).optional()
173
+ })
174
+ ).optional(),
175
+ effects: zod.z.array(
176
+ zod.z.object({
177
+ fn: zod.z.string(),
178
+ deps: zod.z.array(zod.z.string()).optional()
179
+ })
180
+ ).optional(),
181
+ data: zod.z.record(zod.z.string(), zod.z.any()).optional(),
182
+ render: UIElementSchema2,
183
+ query: QuerySpecSchema2.optional()
184
+ });
185
+ var DSLRendererPropsSchema2 = zod.z.object({
186
+ dsl: UIComponentSchema2,
187
+ data: zod.z.record(zod.z.string(), zod.z.any()).optional(),
188
+ context: zod.z.record(zod.z.string(), zod.z.any()).optional()
189
+ });
190
+
191
+ // src/schemas.ts
192
+ var MessageParticipantSchema = zod.z.object({
193
+ id: zod.z.string().optional(),
194
+ type: zod.z.string().optional()
195
+ });
196
+ var MessageSchema = zod.z.object({
197
+ id: zod.z.string(),
198
+ type: zod.z.string(),
199
+ from: MessageParticipantSchema,
200
+ to: MessageParticipantSchema.optional(),
201
+ payload: zod.z.unknown()
202
+ });
203
+ var AuthLoginPayloadSchema = zod.z.object({
204
+ login_data: zod.z.string()
205
+ // Base64 encoded login data (username + SHA-1 hashed password)
206
+ });
207
+ var AuthLoginRequestMessageSchema = zod.z.object({
208
+ id: zod.z.string(),
209
+ type: zod.z.literal("AUTH_LOGIN_REQ"),
210
+ from: MessageParticipantSchema,
211
+ to: MessageParticipantSchema.optional(),
212
+ payload: AuthLoginPayloadSchema
213
+ });
214
+ var AuthLoginResponsePayloadSchema = zod.z.object({
215
+ success: zod.z.boolean(),
216
+ error: zod.z.string().optional(),
217
+ data: zod.z.unknown().optional()
218
+ // User ID or additional auth data
219
+ });
220
+ var AuthLoginResponseMessageSchema = zod.z.object({
221
+ id: zod.z.string(),
222
+ type: zod.z.literal("AUTH_LOGIN_RES"),
223
+ from: MessageParticipantSchema,
224
+ to: MessageParticipantSchema.optional(),
225
+ payload: AuthLoginResponsePayloadSchema
226
+ });
227
+ var AuthVerifyPayloadSchema = zod.z.object({
228
+ token: zod.z.string()
229
+ // Base64 encoded authentication token
230
+ });
231
+ var AuthVerifyRequestMessageSchema = zod.z.object({
232
+ id: zod.z.string(),
233
+ type: zod.z.literal("AUTH_VERIFY_REQ"),
234
+ from: MessageParticipantSchema,
235
+ to: MessageParticipantSchema.optional(),
236
+ payload: AuthVerifyPayloadSchema
237
+ });
238
+ var AuthVerifyResponsePayloadSchema = zod.z.object({
239
+ success: zod.z.boolean(),
240
+ error: zod.z.string().optional(),
241
+ data: zod.z.unknown().optional()
242
+ });
243
+ var AuthVerifyResponseMessageSchema = zod.z.object({
244
+ id: zod.z.string(),
245
+ type: zod.z.literal("AUTH_VERIFY_RES"),
246
+ from: MessageParticipantSchema,
247
+ to: MessageParticipantSchema.optional(),
248
+ payload: AuthVerifyResponsePayloadSchema
249
+ });
250
+ var UserPromptRequestPayloadSchema = zod.z.object({
251
+ prompt: zod.z.string(),
252
+ SA_RUNTIME: zod.z.object({
253
+ threadId: zod.z.string(),
254
+ uiBlockId: zod.z.string()
255
+ }).optional()
256
+ });
257
+ var UserPromptRequestMessageSchema = zod.z.object({
258
+ id: zod.z.string(),
259
+ type: zod.z.literal("USER_PROMPT_REQ"),
260
+ from: MessageParticipantSchema,
261
+ to: MessageParticipantSchema.optional(),
262
+ payload: UserPromptRequestPayloadSchema
263
+ });
264
+ var ComponentSchema = zod.z.object({
265
+ id: zod.z.string(),
266
+ name: zod.z.string(),
267
+ type: zod.z.string(),
268
+ description: zod.z.string(),
269
+ props: zod.z.object({
270
+ query: zod.z.string().optional(),
271
+ title: zod.z.string().optional(),
272
+ description: zod.z.string().optional(),
273
+ config: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
274
+ }),
275
+ category: zod.z.string().optional(),
276
+ keywords: zod.z.array(zod.z.string()).optional()
277
+ });
278
+ var UserPromptResponsePayloadSchema = zod.z.object({
279
+ success: zod.z.boolean(),
280
+ error: zod.z.string().optional(),
281
+ data: zod.z.object({
282
+ component: ComponentSchema.optional()
283
+ }).optional(),
284
+ uiBlockId: zod.z.string().optional(),
285
+ threadId: zod.z.string().optional()
286
+ });
287
+ var UserPromptResponseMessageSchema = zod.z.object({
288
+ id: zod.z.string(),
289
+ type: zod.z.literal("USER_PROMPT_RES"),
290
+ from: MessageParticipantSchema,
291
+ to: MessageParticipantSchema.optional(),
292
+ payload: UserPromptResponsePayloadSchema
293
+ });
294
+ var UserPromptSuggestionsRequestPayloadSchema = zod.z.object({
295
+ prompt: zod.z.string(),
296
+ limit: zod.z.number().int().positive().default(5)
297
+ });
298
+ var UserPromptSuggestionsRequestMessageSchema = zod.z.object({
299
+ id: zod.z.string(),
300
+ type: zod.z.literal("USER_PROMPT_SUGGESTIONS_REQ"),
301
+ from: MessageParticipantSchema,
302
+ to: MessageParticipantSchema.optional(),
303
+ payload: UserPromptSuggestionsRequestPayloadSchema
304
+ });
305
+ var UserPromptSuggestionsResponsePayloadSchema = zod.z.object({
306
+ success: zod.z.boolean(),
307
+ error: zod.z.string().optional(),
308
+ data: zod.z.object({
309
+ prompt: zod.z.string(),
310
+ suggestions: zod.z.array(ComponentSchema),
311
+ count: zod.z.number(),
312
+ message: zod.z.string().optional()
313
+ }).optional()
314
+ });
315
+ var UserPromptSuggestionsResponseMessageSchema = zod.z.object({
316
+ id: zod.z.string(),
317
+ type: zod.z.literal("USER_PROMPT_SUGGESTIONS_RES"),
318
+ from: MessageParticipantSchema,
319
+ to: MessageParticipantSchema.optional(),
320
+ payload: UserPromptSuggestionsResponsePayloadSchema
321
+ });
322
+ var BundleRequestPayloadSchema = zod.z.object({});
323
+ var BundleRequestMessageSchema = zod.z.object({
324
+ id: zod.z.string(),
325
+ type: zod.z.literal("BUNDLE_REQ"),
326
+ from: MessageParticipantSchema,
327
+ to: MessageParticipantSchema.optional(),
328
+ payload: BundleRequestPayloadSchema
329
+ });
330
+ var BundleChunkPayloadSchema = zod.z.object({
331
+ chunk: zod.z.string(),
332
+ chunkIndex: zod.z.number(),
333
+ totalChunks: zod.z.number(),
334
+ isComplete: zod.z.boolean(),
335
+ progress: zod.z.number()
336
+ });
337
+ var BundleChunkMessageSchema = zod.z.object({
338
+ id: zod.z.string(),
339
+ type: zod.z.literal("BUNDLE_CHUNK"),
340
+ from: MessageParticipantSchema,
341
+ to: MessageParticipantSchema.optional(),
342
+ payload: BundleChunkPayloadSchema
343
+ });
344
+ var BundleErrorPayloadSchema = zod.z.object({
345
+ error: zod.z.string()
346
+ });
347
+ var BundleErrorMessageSchema = zod.z.object({
348
+ id: zod.z.string(),
349
+ type: zod.z.literal("BUNDLE_RES"),
350
+ from: MessageParticipantSchema,
351
+ to: MessageParticipantSchema.optional(),
352
+ payload: BundleErrorPayloadSchema
353
+ });
354
+ var DataRequestPayloadSchema = zod.z.object({
355
+ collection: zod.z.string(),
356
+ op: zod.z.string(),
357
+ params: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
358
+ SA_RUNTIME: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
359
+ });
360
+ var DataRequestMessageSchema = zod.z.object({
361
+ id: zod.z.string(),
362
+ type: zod.z.literal("DATA_REQ"),
363
+ from: MessageParticipantSchema,
364
+ to: MessageParticipantSchema.optional(),
365
+ payload: DataRequestPayloadSchema
366
+ });
367
+ var DataResponsePayloadSchema = zod.z.object({
368
+ collection: zod.z.string(),
369
+ op: zod.z.string(),
370
+ data: zod.z.unknown(),
371
+ executionMs: zod.z.number().optional(),
372
+ error: zod.z.string().optional()
373
+ });
374
+ var DataResponseMessageSchema = zod.z.object({
375
+ id: zod.z.string(),
376
+ type: zod.z.literal("DATA_RES"),
377
+ from: MessageParticipantSchema,
378
+ to: MessageParticipantSchema.optional(),
379
+ payload: DataResponsePayloadSchema
380
+ });
381
+ var UILogEntrySchema = zod.z.object({
382
+ timestamp: zod.z.number(),
383
+ level: zod.z.enum(["info", "error", "warn", "debug"]),
384
+ message: zod.z.string(),
385
+ type: zod.z.enum(["explanation", "query", "general"]).optional(),
386
+ data: zod.z.record(zod.z.string(), zod.z.unknown()).optional()
387
+ });
388
+ var UILogsPayloadSchema = zod.z.object({
389
+ logs: zod.z.array(UILogEntrySchema)
390
+ });
391
+ var UILogsMessageSchema = zod.z.object({
392
+ id: zod.z.string(),
393
+ // uiBlockId
394
+ type: zod.z.literal("UI_LOGS"),
395
+ from: MessageParticipantSchema,
396
+ to: MessageParticipantSchema.optional(),
397
+ payload: UILogsPayloadSchema
398
+ });
399
+ var UsersRequestPayloadSchema = zod.z.object({
400
+ operation: zod.z.enum(["create", "update", "delete", "getAll", "getOne"]),
401
+ data: zod.z.object({
402
+ username: zod.z.string().optional(),
403
+ password: zod.z.string().optional()
404
+ }).optional()
405
+ });
406
+ var UsersRequestMessageSchema = zod.z.object({
407
+ id: zod.z.string(),
408
+ type: zod.z.literal("USERS"),
409
+ from: MessageParticipantSchema,
410
+ to: MessageParticipantSchema.optional(),
411
+ payload: UsersRequestPayloadSchema
412
+ });
413
+ var UsersResponsePayloadSchema = zod.z.object({
414
+ success: zod.z.boolean(),
415
+ error: zod.z.string().optional(),
416
+ data: zod.z.object({
417
+ username: zod.z.string().optional(),
418
+ message: zod.z.string().optional(),
419
+ users: zod.z.array(zod.z.object({
420
+ username: zod.z.string(),
421
+ wsIds: zod.z.array(zod.z.string())
422
+ })).optional(),
423
+ user: zod.z.object({
424
+ username: zod.z.string(),
425
+ wsIds: zod.z.array(zod.z.string())
426
+ }).optional(),
427
+ count: zod.z.number().optional()
428
+ }).optional()
429
+ });
430
+ zod.z.object({
431
+ id: zod.z.string(),
432
+ type: zod.z.literal("USERS_RES"),
433
+ from: MessageParticipantSchema,
434
+ to: MessageParticipantSchema.optional(),
435
+ payload: UsersResponsePayloadSchema
436
+ });
437
+ var DashboardsRequestPayloadSchema = zod.z.object({
438
+ operation: zod.z.enum(["create", "update", "delete", "getAll", "getOne"]),
439
+ data: zod.z.object({
440
+ dashboardId: zod.z.string().optional(),
441
+ dashboard: DSLRendererPropsSchema.optional()
442
+ }).optional()
443
+ });
444
+ var DashboardsRequestMessageSchema = zod.z.object({
445
+ id: zod.z.string(),
446
+ type: zod.z.literal("DASHBOARDS"),
447
+ from: MessageParticipantSchema,
448
+ to: MessageParticipantSchema.optional(),
449
+ payload: DashboardsRequestPayloadSchema
450
+ });
451
+ var DashboardsResponsePayloadSchema = zod.z.object({
452
+ success: zod.z.boolean(),
453
+ error: zod.z.string().optional(),
454
+ data: zod.z.object({
455
+ dashboardId: zod.z.string().optional(),
456
+ dashboard: DSLRendererPropsSchema.optional(),
457
+ dashboards: zod.z.array(zod.z.object({
458
+ dashboardId: zod.z.string(),
459
+ dashboard: DSLRendererPropsSchema
460
+ })).optional(),
461
+ count: zod.z.number().optional(),
462
+ message: zod.z.string().optional()
463
+ }).optional()
464
+ });
465
+ zod.z.object({
466
+ id: zod.z.string(),
467
+ type: zod.z.literal("DASHBOARDS_RES"),
468
+ from: MessageParticipantSchema,
469
+ to: MessageParticipantSchema.optional(),
470
+ payload: DashboardsResponsePayloadSchema
471
+ });
472
+ var ReportsRequestPayloadSchema = zod.z.object({
473
+ operation: zod.z.enum(["create", "update", "delete", "getAll", "getOne"]),
474
+ data: zod.z.object({
475
+ reportId: zod.z.string().optional(),
476
+ report: DSLRendererPropsSchema2.optional()
477
+ }).optional()
478
+ });
479
+ var ReportsRequestMessageSchema = zod.z.object({
480
+ id: zod.z.string(),
481
+ type: zod.z.literal("REPORTS"),
482
+ from: MessageParticipantSchema,
483
+ to: MessageParticipantSchema.optional(),
484
+ payload: ReportsRequestPayloadSchema
485
+ });
486
+ var ReportsResponsePayloadSchema = zod.z.object({
487
+ success: zod.z.boolean(),
488
+ error: zod.z.string().optional(),
489
+ data: zod.z.object({
490
+ reportId: zod.z.string().optional(),
491
+ report: DSLRendererPropsSchema2.optional(),
492
+ reports: zod.z.array(zod.z.object({
493
+ reportId: zod.z.string(),
494
+ report: DSLRendererPropsSchema2
495
+ })).optional(),
496
+ count: zod.z.number().optional(),
497
+ message: zod.z.string().optional()
498
+ }).optional()
499
+ });
500
+ zod.z.object({
501
+ id: zod.z.string(),
502
+ type: zod.z.literal("REPORTS_RES"),
503
+ from: MessageParticipantSchema,
504
+ to: MessageParticipantSchema.optional(),
505
+ payload: ReportsResponsePayloadSchema
506
+ });
507
+ var ClientConfigSchema = zod.z.object({
508
+ userId: zod.z.string(),
509
+ projectId: zod.z.string(),
510
+ baseUrl: zod.z.string().default("wss://ws.superatom.ai/websocket"),
511
+ type: zod.z.string().default("runtime"),
512
+ maxReconnectAttempts: zod.z.number().min(0).default(Infinity),
513
+ initialReconnectDelay: zod.z.number().min(0).default(1e3),
514
+ maxReconnectDelay: zod.z.number().min(0).default(3e4),
515
+ defaultTimeout: zod.z.number().min(0).default(3e4),
516
+ debug: zod.z.boolean().default(false)
517
+ });
518
+
519
+ // src/services/index.ts
520
+ var services_exports = {};
521
+ __export(services_exports, {
522
+ BindingSchema: () => BindingSchema,
523
+ DSLRendererPropsSchema: () => DSLRendererPropsSchema,
524
+ ExpressionSchema: () => ExpressionSchema,
525
+ ForDirectiveSchema: () => ForDirectiveSchema,
526
+ QuerySpecSchema: () => QuerySpecSchema,
527
+ UIComponentSchema: () => UIComponentSchema,
528
+ UIElementSchema: () => UIElementSchema,
529
+ createDashboard: () => createDashboard,
530
+ createReport: () => createReport,
531
+ createUser: () => createUser,
532
+ deleteDashboard: () => deleteDashboard,
533
+ deleteReport: () => deleteReport,
534
+ deleteUser: () => deleteUser,
535
+ getAllDashboards: () => getAllDashboards,
536
+ getAllReports: () => getAllReports,
537
+ getAllUsers: () => getAllUsers,
538
+ getComponentSuggestions: () => getComponentSuggestions,
539
+ getDashboard: () => getDashboard,
540
+ getReport: () => getReport,
541
+ getUser: () => getUser,
542
+ requestBundle: () => requestBundle,
543
+ requestData: () => requestData,
544
+ sendAuthLoginRequest: () => sendAuthLoginRequest,
545
+ sendAuthVerifyRequest: () => sendAuthVerifyRequest,
546
+ sendUserPromptRequest: () => sendUserPromptRequest,
547
+ sendUserPromptSuggestionsRequest: () => sendUserPromptSuggestionsRequest,
548
+ updateDashboard: () => updateDashboard,
549
+ updateReport: () => updateReport,
550
+ updateUser: () => updateUser
551
+ });
552
+
553
+ // src/services/auth.ts
554
+ async function sendAuthLoginRequest(client, loginDataBase64, timeout) {
555
+ const messageId = `login_${Date.now()}`;
556
+ const message = AuthLoginRequestMessageSchema.parse({
557
+ id: messageId,
558
+ type: "AUTH_LOGIN_REQ",
559
+ from: {
560
+ type: "runtime"
561
+ },
562
+ to: {
563
+ type: "data-agent"
564
+ },
565
+ payload: {
566
+ login_data: loginDataBase64
567
+ }
568
+ });
569
+ const response = await client.sendWithResponse(message, timeout);
570
+ return response;
571
+ }
572
+ async function sendAuthVerifyRequest(client, token, timeout) {
573
+ const messageId = `verify_${Date.now()}`;
574
+ const message = AuthVerifyRequestMessageSchema.parse({
575
+ id: messageId,
576
+ type: "AUTH_VERIFY_REQ",
577
+ from: {
578
+ type: "runtime"
579
+ },
580
+ to: {
581
+ type: "data-agent"
582
+ },
583
+ payload: {
584
+ token
585
+ }
586
+ });
587
+ const response = await client.sendWithResponse(message, timeout);
588
+ return response;
589
+ }
590
+
591
+ // src/services/userPrompt.ts
592
+ async function sendUserPromptRequest(client, prompt, threadId, uiBlockId, timeout) {
593
+ const messageId = `msg_${Date.now()}`;
594
+ const message = UserPromptRequestMessageSchema.parse({
595
+ id: messageId,
596
+ type: "USER_PROMPT_REQ",
597
+ from: {
598
+ type: "runtime"
599
+ },
600
+ to: {
601
+ type: "data-agent"
602
+ },
603
+ payload: {
604
+ prompt,
605
+ SA_RUNTIME: {
606
+ threadId,
607
+ uiBlockId
608
+ }
609
+ }
610
+ });
611
+ const response = await client.sendWithResponse(message, timeout);
612
+ return response;
613
+ }
614
+ async function sendUserPromptSuggestionsRequest(client, prompt, limit = 5, timeout) {
615
+ if (!prompt || prompt.trim().length === 0) {
616
+ return null;
617
+ }
618
+ const messageId = `suggestions_${Date.now()}`;
619
+ const message = UserPromptSuggestionsRequestMessageSchema.parse({
620
+ id: messageId,
621
+ type: "USER_PROMPT_SUGGESTIONS_REQ",
622
+ from: {
623
+ type: "runtime"
624
+ },
625
+ to: {
626
+ type: "data-agent"
627
+ },
628
+ payload: {
629
+ prompt,
630
+ limit
631
+ }
632
+ });
633
+ const response = await client.sendWithResponse(message, timeout);
634
+ return response;
635
+ }
636
+
637
+ // src/services/bundle.ts
638
+ async function requestBundle(client, options) {
639
+ const messageId = `msg_${Date.now()}_${Math.random().toString(36).substring(7)}`;
640
+ const message = BundleRequestMessageSchema.parse({
641
+ id: messageId,
642
+ type: "BUNDLE_REQ",
643
+ from: { type: "runtime" },
644
+ to: { type: "data-agent" },
645
+ payload: {}
646
+ });
647
+ const chunks = [];
648
+ let totalChunks = 0;
649
+ const unsubscribe = client.onMessage((msg) => {
650
+ try {
651
+ if (msg.type === "BUNDLE_CHUNK" && msg.id?.startsWith(messageId)) {
652
+ const chunkMessage = BundleChunkMessageSchema.parse(msg);
653
+ const payload = chunkMessage.payload;
654
+ chunks[payload.chunkIndex] = payload.chunk;
655
+ totalChunks = payload.totalChunks;
656
+ if (options?.onProgress) {
657
+ options.onProgress(payload.progress);
658
+ }
659
+ }
660
+ } catch (error) {
661
+ console.error("[SuperAtom] Failed to parse bundle chunk:", error);
662
+ }
663
+ });
664
+ client.send(message);
665
+ const timeout = options?.timeout ?? 6e4;
666
+ const startTime = Date.now();
667
+ return new Promise((resolve, reject) => {
668
+ const checkInterval = setInterval(() => {
669
+ if (Date.now() - startTime > timeout) {
670
+ clearInterval(checkInterval);
671
+ unsubscribe();
672
+ reject(new Error(`Bundle request timeout after ${timeout}ms`));
673
+ return;
674
+ }
675
+ if (totalChunks > 0 && chunks.length === totalChunks) {
676
+ clearInterval(checkInterval);
677
+ unsubscribe();
678
+ const bundle = chunks.join("");
679
+ resolve(bundle);
680
+ }
681
+ }, 100);
682
+ });
683
+ }
684
+
685
+ // src/services/data.ts
686
+ async function requestData(client, options) {
687
+ const messageId = `msg_${Date.now()}_${Math.random().toString(36).substring(7)}`;
688
+ const message = DataRequestMessageSchema.parse({
689
+ id: messageId,
690
+ type: "DATA_REQ",
691
+ from: { type: "runtime" },
692
+ to: { type: "data-agent" },
693
+ payload: {
694
+ collection: options.collection,
695
+ op: options.operation,
696
+ params: options.params
697
+ }
698
+ });
699
+ const response = await client.sendWithResponse(message, options.timeout);
700
+ const payload = response.payload;
701
+ return {
702
+ data: payload.data,
703
+ error: payload.error,
704
+ executionMs: payload.executionMs
705
+ };
706
+ }
707
+
708
+ // src/services/component.ts
709
+ async function getComponentSuggestions(client, query, options) {
710
+ const messageId = `msg_${Date.now()}_${Math.random().toString(36).substring(7)}`;
711
+ const message = {
712
+ id: messageId,
713
+ type: "USER_PROMPT_REQ",
714
+ to: { type: "data-agent" },
715
+ payload: {
716
+ prompt: query
717
+ }
718
+ };
719
+ const response = await client.sendWithResponse(message, options?.timeout);
720
+ const payload = response.payload;
721
+ return {
722
+ component: payload.component,
723
+ error: payload.error
724
+ };
725
+ }
726
+
727
+ // src/services/users.ts
728
+ async function createUser(client, username, password, timeout) {
729
+ const messageId = `users_create_${Date.now()}`;
730
+ const message = UsersRequestMessageSchema.parse({
731
+ id: messageId,
732
+ type: "USERS",
733
+ from: { type: "admin" },
734
+ to: { type: "data-agent" },
735
+ payload: {
736
+ operation: "create",
737
+ data: {
738
+ username,
739
+ password
740
+ }
741
+ }
742
+ });
743
+ const response = await client.sendWithResponse(message, timeout);
744
+ const payload = response.payload;
745
+ return {
746
+ success: payload.success,
747
+ error: payload.error,
748
+ message: payload.data?.message,
749
+ username: payload.data?.username
750
+ };
751
+ }
752
+ async function updateUser(client, username, password, timeout) {
753
+ const messageId = `users_update_${Date.now()}`;
754
+ const message = UsersRequestMessageSchema.parse({
755
+ id: messageId,
756
+ type: "USERS",
757
+ from: { type: "admin" },
758
+ to: { type: "data-agent" },
759
+ payload: {
760
+ operation: "update",
761
+ data: {
762
+ username,
763
+ password
764
+ }
765
+ }
766
+ });
767
+ const response = await client.sendWithResponse(message, timeout);
768
+ const payload = response.payload;
769
+ return {
770
+ success: payload.success,
771
+ error: payload.error,
772
+ message: payload.data?.message,
773
+ username: payload.data?.username
774
+ };
775
+ }
776
+ async function deleteUser(client, username, timeout) {
777
+ const messageId = `users_delete_${Date.now()}`;
778
+ const message = UsersRequestMessageSchema.parse({
779
+ id: messageId,
780
+ type: "USERS",
781
+ from: { type: "admin" },
782
+ to: { type: "data-agent" },
783
+ payload: {
784
+ operation: "delete",
785
+ data: {
786
+ username
787
+ }
788
+ }
789
+ });
790
+ const response = await client.sendWithResponse(message, timeout);
791
+ const payload = response.payload;
792
+ return {
793
+ success: payload.success,
794
+ error: payload.error,
795
+ message: payload.data?.message,
796
+ username: payload.data?.username
797
+ };
798
+ }
799
+ async function getAllUsers(client, timeout) {
800
+ const messageId = `users_getall_${Date.now()}`;
801
+ const message = UsersRequestMessageSchema.parse({
802
+ id: messageId,
803
+ type: "USERS",
804
+ from: { type: "admin" },
805
+ to: { type: "data-agent" },
806
+ payload: {
807
+ operation: "getAll"
808
+ }
809
+ });
810
+ const response = await client.sendWithResponse(message, timeout);
811
+ const payload = response.payload;
812
+ return {
813
+ success: payload.success,
814
+ error: payload.error,
815
+ users: payload.data?.users,
816
+ count: payload.data?.count,
817
+ message: payload.data?.message
818
+ };
819
+ }
820
+ async function getUser(client, username, timeout) {
821
+ const messageId = `users_getone_${Date.now()}`;
822
+ const message = UsersRequestMessageSchema.parse({
823
+ id: messageId,
824
+ type: "USERS",
825
+ from: { type: "admin" },
826
+ to: { type: "data-agent" },
827
+ payload: {
828
+ operation: "getOne",
829
+ data: {
830
+ username
831
+ }
832
+ }
833
+ });
834
+ const response = await client.sendWithResponse(message, timeout);
835
+ const payload = response.payload;
836
+ return {
837
+ success: payload.success,
838
+ error: payload.error,
839
+ user: payload.data?.user,
840
+ message: payload.data?.message
841
+ };
842
+ }
843
+
844
+ // src/services/dashboards/index.ts
845
+ async function createDashboard(client, dashboardId, dashboard, timeout) {
846
+ const messageId = `dashboards_create_${Date.now()}`;
847
+ const message = DashboardsRequestMessageSchema.parse({
848
+ id: messageId,
849
+ type: "DASHBOARDS",
850
+ from: { type: "admin" },
851
+ to: { type: "data-agent" },
852
+ payload: {
853
+ operation: "create",
854
+ data: {
855
+ dashboardId,
856
+ dashboard
857
+ }
858
+ }
859
+ });
860
+ const response = await client.sendWithResponse(message, timeout);
861
+ const payload = response.payload;
862
+ return {
863
+ success: payload.success,
864
+ error: payload.error,
865
+ message: payload.data?.message,
866
+ dashboardId: payload.data?.dashboardId,
867
+ dashboard: payload.data?.dashboard
868
+ };
869
+ }
870
+ async function updateDashboard(client, dashboardId, dashboard, timeout) {
871
+ const messageId = `dashboards_update_${Date.now()}`;
872
+ const message = DashboardsRequestMessageSchema.parse({
873
+ id: messageId,
874
+ type: "DASHBOARDS",
875
+ from: { type: "admin" },
876
+ to: { type: "data-agent" },
877
+ payload: {
878
+ operation: "update",
879
+ data: {
880
+ dashboardId,
881
+ dashboard
882
+ }
883
+ }
884
+ });
885
+ const response = await client.sendWithResponse(message, timeout);
886
+ const payload = response.payload;
887
+ return {
888
+ success: payload.success,
889
+ error: payload.error,
890
+ message: payload.data?.message,
891
+ dashboardId: payload.data?.dashboardId,
892
+ dashboard: payload.data?.dashboard
893
+ };
894
+ }
895
+ async function deleteDashboard(client, dashboardId, timeout) {
896
+ const messageId = `dashboards_delete_${Date.now()}`;
897
+ const message = DashboardsRequestMessageSchema.parse({
898
+ id: messageId,
899
+ type: "DASHBOARDS",
900
+ from: { type: "admin" },
901
+ to: { type: "data-agent" },
902
+ payload: {
903
+ operation: "delete",
904
+ data: {
905
+ dashboardId
906
+ }
907
+ }
908
+ });
909
+ const response = await client.sendWithResponse(message, timeout);
910
+ const payload = response.payload;
911
+ return {
912
+ success: payload.success,
913
+ error: payload.error,
914
+ message: payload.data?.message,
915
+ dashboardId: payload.data?.dashboardId
916
+ };
917
+ }
918
+ async function getAllDashboards(client, timeout) {
919
+ const messageId = `dashboards_getall_${Date.now()}`;
920
+ const message = DashboardsRequestMessageSchema.parse({
921
+ id: messageId,
922
+ type: "DASHBOARDS",
923
+ from: { type: "admin" },
924
+ to: { type: "data-agent" },
925
+ payload: {
926
+ operation: "getAll"
927
+ }
928
+ });
929
+ const response = await client.sendWithResponse(message, timeout);
930
+ const payload = response.payload;
931
+ return {
932
+ success: payload.success,
933
+ error: payload.error,
934
+ dashboards: payload.data?.dashboards,
935
+ count: payload.data?.count,
936
+ message: payload.data?.message
937
+ };
938
+ }
939
+ async function getDashboard(client, dashboardId, timeout) {
940
+ const messageId = `dashboards_getone_${Date.now()}`;
941
+ const message = DashboardsRequestMessageSchema.parse({
942
+ id: messageId,
943
+ type: "DASHBOARDS",
944
+ from: { type: "admin" },
945
+ to: { type: "data-agent" },
946
+ payload: {
947
+ operation: "getOne",
948
+ data: {
949
+ dashboardId
950
+ }
951
+ }
952
+ });
953
+ const response = await client.sendWithResponse(message, timeout);
954
+ const payload = response.payload;
955
+ return {
956
+ success: payload.success,
957
+ error: payload.error,
958
+ dashboardId: payload.data?.dashboardId,
959
+ dashboard: payload.data?.dashboard,
960
+ message: payload.data?.message
961
+ };
962
+ }
963
+
964
+ // src/services/reports/index.ts
965
+ async function createReport(client, reportId, report, timeout) {
966
+ const messageId = `reports_create_${Date.now()}`;
967
+ const message = ReportsRequestMessageSchema.parse({
968
+ id: messageId,
969
+ type: "REPORTS",
970
+ from: { type: "admin" },
971
+ to: { type: "data-agent" },
972
+ payload: {
973
+ operation: "create",
974
+ data: {
975
+ reportId,
976
+ report
977
+ }
978
+ }
979
+ });
980
+ const response = await client.sendWithResponse(message, timeout);
981
+ const payload = response.payload;
982
+ return {
983
+ success: payload.success,
984
+ error: payload.error,
985
+ message: payload.data?.message,
986
+ reportId: payload.data?.reportId,
987
+ report: payload.data?.report
988
+ };
989
+ }
990
+ async function updateReport(client, reportId, report, timeout) {
991
+ const messageId = `reports_update_${Date.now()}`;
992
+ const message = ReportsRequestMessageSchema.parse({
993
+ id: messageId,
994
+ type: "REPORTS",
995
+ from: { type: "admin" },
996
+ to: { type: "data-agent" },
997
+ payload: {
998
+ operation: "update",
999
+ data: {
1000
+ reportId,
1001
+ report
1002
+ }
1003
+ }
1004
+ });
1005
+ const response = await client.sendWithResponse(message, timeout);
1006
+ const payload = response.payload;
1007
+ return {
1008
+ success: payload.success,
1009
+ error: payload.error,
1010
+ message: payload.data?.message,
1011
+ reportId: payload.data?.reportId,
1012
+ report: payload.data?.report
1013
+ };
1014
+ }
1015
+ async function deleteReport(client, reportId, timeout) {
1016
+ const messageId = `reports_delete_${Date.now()}`;
1017
+ const message = ReportsRequestMessageSchema.parse({
1018
+ id: messageId,
1019
+ type: "REPORTS",
1020
+ from: { type: "admin" },
1021
+ to: { type: "data-agent" },
1022
+ payload: {
1023
+ operation: "delete",
1024
+ data: {
1025
+ reportId
1026
+ }
1027
+ }
1028
+ });
1029
+ const response = await client.sendWithResponse(message, timeout);
1030
+ const payload = response.payload;
1031
+ return {
1032
+ success: payload.success,
1033
+ error: payload.error,
1034
+ message: payload.data?.message,
1035
+ reportId: payload.data?.reportId
1036
+ };
1037
+ }
1038
+ async function getAllReports(client, timeout) {
1039
+ const messageId = `reports_getall_${Date.now()}`;
1040
+ const message = ReportsRequestMessageSchema.parse({
1041
+ id: messageId,
1042
+ type: "REPORTS",
1043
+ from: { type: "admin" },
1044
+ to: { type: "data-agent" },
1045
+ payload: {
1046
+ operation: "getAll"
1047
+ }
1048
+ });
1049
+ const response = await client.sendWithResponse(message, timeout);
1050
+ const payload = response.payload;
1051
+ return {
1052
+ success: payload.success,
1053
+ error: payload.error,
1054
+ reports: payload.data?.reports,
1055
+ count: payload.data?.count,
1056
+ message: payload.data?.message
1057
+ };
1058
+ }
1059
+ async function getReport(client, reportId, timeout) {
1060
+ const messageId = `reports_getone_${Date.now()}`;
1061
+ const message = ReportsRequestMessageSchema.parse({
1062
+ id: messageId,
1063
+ type: "REPORTS",
1064
+ from: { type: "admin" },
1065
+ to: { type: "data-agent" },
1066
+ payload: {
1067
+ operation: "getOne",
1068
+ data: {
1069
+ reportId
1070
+ }
1071
+ }
1072
+ });
1073
+ const response = await client.sendWithResponse(message, timeout);
1074
+ const payload = response.payload;
1075
+ return {
1076
+ success: payload.success,
1077
+ error: payload.error,
1078
+ reportId: payload.data?.reportId,
1079
+ report: payload.data?.report,
1080
+ message: payload.data?.message
1081
+ };
1082
+ }
1083
+
1084
+ // src/client.ts
1085
+ var SuperatomClient = class {
1086
+ constructor(config) {
1087
+ this.socket = null;
1088
+ this.reconnectAttempts = 0;
1089
+ this.reconnectTimer = null;
1090
+ this.shouldReconnect = true;
1091
+ this.messageHandlers = /* @__PURE__ */ new Set();
1092
+ this.connectionHandlers = /* @__PURE__ */ new Set();
1093
+ this.disconnectionHandlers = /* @__PURE__ */ new Set();
1094
+ this.errorHandlers = /* @__PURE__ */ new Set();
1095
+ this.connectPromise = null;
1096
+ this.connectResolve = null;
1097
+ const parsed = ClientConfigSchema.parse(config);
1098
+ this.config = parsed;
1099
+ }
1100
+ /**
1101
+ * Get WebSocket URL with query parameters
1102
+ */
1103
+ getWebSocketUrl() {
1104
+ const params = new URLSearchParams({
1105
+ userId: this.config.userId,
1106
+ projectId: this.config.projectId,
1107
+ type: this.config.type
1108
+ });
1109
+ return `${this.config.baseUrl}?${params.toString()}`;
1110
+ }
1111
+ /**
1112
+ * Calculate reconnection delay with exponential backoff
1113
+ */
1114
+ calculateReconnectDelay() {
1115
+ const delay = Math.min(
1116
+ this.config.initialReconnectDelay * Math.pow(2, this.reconnectAttempts),
1117
+ this.config.maxReconnectDelay
1118
+ );
1119
+ return delay + Math.random() * 1e3;
1120
+ }
1121
+ /**
1122
+ * Schedule reconnection attempt
1123
+ */
1124
+ scheduleReconnect() {
1125
+ if (!this.shouldReconnect) {
1126
+ return;
1127
+ }
1128
+ if (this.reconnectAttempts >= this.config.maxReconnectAttempts) {
1129
+ const error = new Error("Max reconnection attempts reached");
1130
+ this.log("error", error.message);
1131
+ this.errorHandlers.forEach((handler) => handler(error));
1132
+ return;
1133
+ }
1134
+ const delay = this.calculateReconnectDelay();
1135
+ this.log("info", `Scheduling reconnection attempt ${this.reconnectAttempts + 1} in ${delay}ms`);
1136
+ this.reconnectTimer = setTimeout(() => {
1137
+ this.reconnectAttempts++;
1138
+ this.connect();
1139
+ }, delay);
1140
+ }
1141
+ /**
1142
+ * Setup WebSocket event handlers
1143
+ */
1144
+ setupSocketHandlers() {
1145
+ if (!this.socket) {
1146
+ return;
1147
+ }
1148
+ this.socket.onopen = () => {
1149
+ this.log("info", "Connected to SuperAtom");
1150
+ this.reconnectAttempts = 0;
1151
+ if (this.connectResolve) {
1152
+ this.connectResolve();
1153
+ this.connectResolve = null;
1154
+ }
1155
+ this.connectionHandlers.forEach((handler) => handler());
1156
+ };
1157
+ this.socket.onmessage = (event) => {
1158
+ try {
1159
+ const parsed = JSON.parse(event.data);
1160
+ const message = MessageSchema.parse(parsed);
1161
+ this.messageHandlers.forEach((handler) => handler(message));
1162
+ } catch (error) {
1163
+ this.log("error", "Failed to parse message:", error);
1164
+ const err = error instanceof Error ? error : new Error("Parse error");
1165
+ this.errorHandlers.forEach((handler) => handler(err));
1166
+ }
1167
+ };
1168
+ this.socket.onclose = (event) => {
1169
+ this.log("info", "Disconnected from SuperAtom", {
1170
+ code: event.code,
1171
+ reason: event.reason,
1172
+ wasClean: event.wasClean
1173
+ });
1174
+ this.disconnectionHandlers.forEach((handler) => handler());
1175
+ if (this.shouldReconnect && !event.wasClean) {
1176
+ this.scheduleReconnect();
1177
+ }
1178
+ };
1179
+ this.socket.onerror = (error) => {
1180
+ this.log("error", "WebSocket error:", error);
1181
+ this.errorHandlers.forEach((handler) => handler(error));
1182
+ };
1183
+ }
1184
+ /**
1185
+ * Connect to SuperAtom WebSocket server
1186
+ * Returns a Promise that resolves when the connection is established
1187
+ * The Promise never rejects - failures are logged silently and auto-reconnect is triggered
1188
+ */
1189
+ connect() {
1190
+ if (this.socket?.readyState === WebSocket.OPEN) {
1191
+ this.log("warn", "Already connected");
1192
+ return Promise.resolve();
1193
+ }
1194
+ if (this.socket?.readyState === WebSocket.CONNECTING && this.connectPromise) {
1195
+ this.log("warn", "Connection already in progress");
1196
+ return this.connectPromise;
1197
+ }
1198
+ this.connectPromise = new Promise((resolve) => {
1199
+ this.connectResolve = resolve;
1200
+ });
1201
+ try {
1202
+ this.socket = new WebSocket(this.getWebSocketUrl());
1203
+ this.setupSocketHandlers();
1204
+ } catch (error) {
1205
+ this.log("error", "Failed to create WebSocket:", error);
1206
+ const err = error instanceof Error ? error : new Error("Connection failed");
1207
+ this.errorHandlers.forEach((handler) => handler(err));
1208
+ this.scheduleReconnect();
1209
+ }
1210
+ return this.connectPromise;
1211
+ }
1212
+ /**
1213
+ * Disconnect from WebSocket server
1214
+ */
1215
+ disconnect() {
1216
+ this.shouldReconnect = false;
1217
+ if (this.reconnectTimer) {
1218
+ clearTimeout(this.reconnectTimer);
1219
+ this.reconnectTimer = null;
1220
+ }
1221
+ if (this.socket) {
1222
+ this.socket.close(1e3, "Client disconnect");
1223
+ this.socket = null;
1224
+ }
1225
+ this.reconnectAttempts = 0;
1226
+ }
1227
+ /**
1228
+ * Update client configuration
1229
+ */
1230
+ updateConfig(config) {
1231
+ const merged = { ...this.config, ...config };
1232
+ const parsed = ClientConfigSchema.parse(merged);
1233
+ this.config = parsed;
1234
+ }
1235
+ /**
1236
+ * Reconnect with new configuration
1237
+ * Disconnects from existing connection and reconnects with updated config
1238
+ * Returns a Promise that resolves when the new connection is established
1239
+ */
1240
+ reconnectWithConfig(config) {
1241
+ this.updateConfig(config);
1242
+ if (this.socket) {
1243
+ this.disconnect();
1244
+ }
1245
+ this.shouldReconnect = true;
1246
+ return this.connect();
1247
+ }
1248
+ /**
1249
+ * Send a message to the server
1250
+ */
1251
+ send(message) {
1252
+ if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
1253
+ throw new Error("WebSocket is not connected");
1254
+ }
1255
+ const fullMessage = {
1256
+ ...message,
1257
+ from: message.from || { type: "runtime" }
1258
+ };
1259
+ try {
1260
+ this.socket.send(JSON.stringify(fullMessage));
1261
+ } catch (error) {
1262
+ this.log("error", "Failed to send message:", error);
1263
+ throw error;
1264
+ }
1265
+ }
1266
+ /**
1267
+ * Send a message and wait for a response with the same ID
1268
+ * Uses closure-based approach - no global state management needed
1269
+ */
1270
+ async sendWithResponse(message, timeout) {
1271
+ const messageId = message.id || `msg_${Date.now()}_${Math.random().toString(36).substring(7)}`;
1272
+ const messageWithId = { ...message, id: messageId };
1273
+ const timeoutMs = timeout ?? this.config.defaultTimeout;
1274
+ this.send(messageWithId);
1275
+ return new Promise((resolve, reject) => {
1276
+ let isResolved = false;
1277
+ const responseHandler = (msg) => {
1278
+ if (msg.id === messageId && !isResolved) {
1279
+ isResolved = true;
1280
+ clearTimeout(timer);
1281
+ unsubscribe();
1282
+ resolve(msg);
1283
+ }
1284
+ };
1285
+ const unsubscribe = this.onMessage(responseHandler);
1286
+ const timer = setTimeout(() => {
1287
+ if (!isResolved) {
1288
+ isResolved = true;
1289
+ unsubscribe();
1290
+ reject(new Error(`Request timeout after ${timeoutMs}ms`));
1291
+ }
1292
+ }, timeoutMs);
1293
+ });
1294
+ }
1295
+ /**
1296
+ * Register message handler
1297
+ */
1298
+ onMessage(handler) {
1299
+ this.messageHandlers.add(handler);
1300
+ return () => this.messageHandlers.delete(handler);
1301
+ }
1302
+ /**
1303
+ * Register connection handler
1304
+ */
1305
+ onConnect(handler) {
1306
+ this.connectionHandlers.add(handler);
1307
+ return () => this.connectionHandlers.delete(handler);
1308
+ }
1309
+ /**
1310
+ * Register disconnection handler
1311
+ */
1312
+ onDisconnect(handler) {
1313
+ this.disconnectionHandlers.add(handler);
1314
+ return () => this.disconnectionHandlers.delete(handler);
1315
+ }
1316
+ /**
1317
+ * Register error handler
1318
+ */
1319
+ onError(handler) {
1320
+ this.errorHandlers.add(handler);
1321
+ return () => this.errorHandlers.delete(handler);
1322
+ }
1323
+ /**
1324
+ * Check if client is connected
1325
+ */
1326
+ isConnected() {
1327
+ return this.socket?.readyState === WebSocket.OPEN;
1328
+ }
1329
+ /**
1330
+ * Get current reconnection attempts
1331
+ */
1332
+ getReconnectAttempts() {
1333
+ return this.reconnectAttempts;
1334
+ }
1335
+ /**
1336
+ * Send a message and wait for response with timeout (alias for sendWithResponse)
1337
+ */
1338
+ async ask(message, options) {
1339
+ return this.sendWithResponse(message, options?.timeout);
1340
+ }
1341
+ // ==================== Service Methods ====================
1342
+ // These methods delegate to service functions for better code organization
1343
+ /**
1344
+ * Send authentication login request
1345
+ * Delegates to auth service
1346
+ */
1347
+ async sendAuthLoginRequest(loginDataBase64, timeout) {
1348
+ this.log("info", "Sending auth login request");
1349
+ return sendAuthLoginRequest(this, loginDataBase64, timeout);
1350
+ }
1351
+ /**
1352
+ * Send authentication verify request
1353
+ * Delegates to auth service
1354
+ */
1355
+ async sendAuthVerifyRequest(token, timeout) {
1356
+ this.log("info", "Sending auth verify request");
1357
+ return sendAuthVerifyRequest(this, token, timeout);
1358
+ }
1359
+ /**
1360
+ * Send a user prompt request
1361
+ * Delegates to user prompt service
1362
+ */
1363
+ async sendUserPromptRequest(prompt, threadId, uiBlockId, timeout) {
1364
+ this.log("info", "Sending user prompt request");
1365
+ return sendUserPromptRequest(this, prompt, threadId, uiBlockId, timeout);
1366
+ }
1367
+ /**
1368
+ * Send a user prompt suggestions request
1369
+ * Delegates to user prompt service
1370
+ */
1371
+ async sendUserPromptSuggestionsRequest(prompt, limit = 5, timeout) {
1372
+ this.log("info", "Sending user prompt suggestions request");
1373
+ return sendUserPromptSuggestionsRequest(this, prompt, limit, timeout);
1374
+ }
1375
+ /**
1376
+ * Request bundle from server
1377
+ * Delegates to bundle service
1378
+ */
1379
+ async requestBundle(options) {
1380
+ this.log("info", "Requesting bundle from server");
1381
+ return requestBundle(this, options);
1382
+ }
1383
+ /**
1384
+ * Request data from a collection
1385
+ * Delegates to data service
1386
+ */
1387
+ async requestData(options) {
1388
+ this.log("info", `Requesting data from collection: ${options.collection}, operation: ${options.operation}`);
1389
+ return requestData(this, options);
1390
+ }
1391
+ /**
1392
+ * Get AI-powered component suggestions based on a query
1393
+ * Delegates to component service
1394
+ */
1395
+ async getComponentSuggestions(query, options) {
1396
+ this.log("info", "Requesting component suggestions for query:", query);
1397
+ return getComponentSuggestions(this, query, options);
1398
+ }
1399
+ // ==================== User Management Methods ====================
1400
+ // These methods delegate to user service for admin user CRUD operations
1401
+ /**
1402
+ * Create a new user
1403
+ * Delegates to users service
1404
+ */
1405
+ async createUser(username, password, timeout) {
1406
+ this.log("info", "Creating user:", username);
1407
+ return createUser(this, username, password, timeout);
1408
+ }
1409
+ /**
1410
+ * Update an existing user
1411
+ * Delegates to users service
1412
+ */
1413
+ async updateUser(username, password, timeout) {
1414
+ this.log("info", "Updating user:", username);
1415
+ return updateUser(this, username, password, timeout);
1416
+ }
1417
+ /**
1418
+ * Delete a user
1419
+ * Delegates to users service
1420
+ */
1421
+ async deleteUser(username, timeout) {
1422
+ this.log("info", "Deleting user:", username);
1423
+ return deleteUser(this, username, timeout);
1424
+ }
1425
+ /**
1426
+ * Get all users
1427
+ * Delegates to users service
1428
+ */
1429
+ async getAllUsers(timeout) {
1430
+ this.log("info", "Fetching all users");
1431
+ return getAllUsers(this, timeout);
1432
+ }
1433
+ /**
1434
+ * Get a specific user by username
1435
+ * Delegates to users service
1436
+ */
1437
+ async getUser(username, timeout) {
1438
+ this.log("info", "Fetching user:", username);
1439
+ return getUser(this, username, timeout);
1440
+ }
1441
+ // ==================== Dashboard Management Methods ====================
1442
+ // These methods delegate to dashboard service for admin dashboard CRUD operations
1443
+ /**
1444
+ * Create a new dashboard
1445
+ * Delegates to dashboards service
1446
+ */
1447
+ async createDashboard(dashboardId, dashboard, timeout) {
1448
+ this.log("info", "Creating dashboard:", dashboardId);
1449
+ return createDashboard(this, dashboardId, dashboard, timeout);
1450
+ }
1451
+ /**
1452
+ * Update an existing dashboard
1453
+ * Delegates to dashboards service
1454
+ */
1455
+ async updateDashboard(dashboardId, dashboard, timeout) {
1456
+ this.log("info", "Updating dashboard:", dashboardId);
1457
+ return updateDashboard(this, dashboardId, dashboard, timeout);
1458
+ }
1459
+ /**
1460
+ * Delete a dashboard
1461
+ * Delegates to dashboards service
1462
+ */
1463
+ async deleteDashboard(dashboardId, timeout) {
1464
+ this.log("info", "Deleting dashboard:", dashboardId);
1465
+ return deleteDashboard(this, dashboardId, timeout);
1466
+ }
1467
+ /**
1468
+ * Get all dashboards
1469
+ * Delegates to dashboards service
1470
+ */
1471
+ async getAllDashboards(timeout) {
1472
+ this.log("info", "Fetching all dashboards");
1473
+ return getAllDashboards(this, timeout);
1474
+ }
1475
+ /**
1476
+ * Get a specific dashboard by ID
1477
+ * Delegates to dashboards service
1478
+ */
1479
+ async getDashboard(dashboardId, timeout) {
1480
+ this.log("info", "Fetching dashboard:", dashboardId);
1481
+ return getDashboard(this, dashboardId, timeout);
1482
+ }
1483
+ // ==================== Report Management Methods ====================
1484
+ // These methods delegate to report service for admin report CRUD operations
1485
+ /**
1486
+ * Create a new report
1487
+ * Delegates to reports service
1488
+ */
1489
+ async createReport(reportId, report, timeout) {
1490
+ this.log("info", "Creating report:", reportId);
1491
+ return createReport(this, reportId, report, timeout);
1492
+ }
1493
+ /**
1494
+ * Update an existing report
1495
+ * Delegates to reports service
1496
+ */
1497
+ async updateReport(reportId, report, timeout) {
1498
+ this.log("info", "Updating report:", reportId);
1499
+ return updateReport(this, reportId, report, timeout);
1500
+ }
1501
+ /**
1502
+ * Delete a report
1503
+ * Delegates to reports service
1504
+ */
1505
+ async deleteReport(reportId, timeout) {
1506
+ this.log("info", "Deleting report:", reportId);
1507
+ return deleteReport(this, reportId, timeout);
1508
+ }
1509
+ /**
1510
+ * Get all reports
1511
+ * Delegates to reports service
1512
+ */
1513
+ async getAllReports(timeout) {
1514
+ this.log("info", "Fetching all reports");
1515
+ return getAllReports(this, timeout);
1516
+ }
1517
+ /**
1518
+ * Get a specific report by ID
1519
+ * Delegates to reports service
1520
+ */
1521
+ async getReport(reportId, timeout) {
1522
+ this.log("info", "Fetching report:", reportId);
1523
+ return getReport(this, reportId, timeout);
1524
+ }
1525
+ /**
1526
+ * Internal logging
1527
+ */
1528
+ log(level, ...args) {
1529
+ if (!this.config.debug && level === "debug") {
1530
+ return;
1531
+ }
1532
+ console[level]("[SuperAtom]", ...args);
1533
+ }
1534
+ };
1535
+
1536
+ // src/setup.ts
1537
+ function setup(components) {
1538
+ if (typeof window === "undefined") {
1539
+ throw new Error("setup() can only be called in a browser environment");
1540
+ }
1541
+ window.SUPERATOM = window.SUPERATOM || {};
1542
+ window.SA = window.SUPERATOM;
1543
+ window.SUPERATOM.COMPONENTS = components;
1544
+ if (window.SUPERATOM.runtime?.componentsLoaded) {
1545
+ window.SUPERATOM.runtime.componentsLoaded();
1546
+ }
1547
+ }
1548
+ function getComponents() {
1549
+ if (typeof window === "undefined") {
1550
+ return void 0;
1551
+ }
1552
+ return window.SUPERATOM?.COMPONENTS;
1553
+ }
1554
+ function getComponent(name) {
1555
+ if (typeof window === "undefined") {
1556
+ return void 0;
1557
+ }
1558
+ return window.SUPERATOM?.COMPONENTS?.[name];
1559
+ }
1560
+ function hasComponents() {
1561
+ if (typeof window === "undefined") {
1562
+ return false;
1563
+ }
1564
+ return !!window.SUPERATOM?.COMPONENTS;
1565
+ }
1566
+
1567
+ // src/index.ts
1568
+ var SDK_VERSION = "0.1.0";
1569
+
1570
+ exports.AuthLoginPayloadSchema = AuthLoginPayloadSchema;
1571
+ exports.AuthLoginRequestMessageSchema = AuthLoginRequestMessageSchema;
1572
+ exports.AuthLoginResponseMessageSchema = AuthLoginResponseMessageSchema;
1573
+ exports.AuthLoginResponsePayloadSchema = AuthLoginResponsePayloadSchema;
1574
+ exports.AuthVerifyPayloadSchema = AuthVerifyPayloadSchema;
1575
+ exports.AuthVerifyRequestMessageSchema = AuthVerifyRequestMessageSchema;
1576
+ exports.AuthVerifyResponseMessageSchema = AuthVerifyResponseMessageSchema;
1577
+ exports.AuthVerifyResponsePayloadSchema = AuthVerifyResponsePayloadSchema;
1578
+ exports.BundleChunkMessageSchema = BundleChunkMessageSchema;
1579
+ exports.BundleChunkPayloadSchema = BundleChunkPayloadSchema;
1580
+ exports.BundleErrorMessageSchema = BundleErrorMessageSchema;
1581
+ exports.BundleErrorPayloadSchema = BundleErrorPayloadSchema;
1582
+ exports.BundleRequestMessageSchema = BundleRequestMessageSchema;
1583
+ exports.BundleRequestPayloadSchema = BundleRequestPayloadSchema;
1584
+ exports.ClientConfigSchema = ClientConfigSchema;
1585
+ exports.ComponentSchema = ComponentSchema;
1586
+ exports.DataRequestMessageSchema = DataRequestMessageSchema;
1587
+ exports.DataRequestPayloadSchema = DataRequestPayloadSchema;
1588
+ exports.DataResponseMessageSchema = DataResponseMessageSchema;
1589
+ exports.DataResponsePayloadSchema = DataResponsePayloadSchema;
1590
+ exports.MessageParticipantSchema = MessageParticipantSchema;
1591
+ exports.MessageSchema = MessageSchema;
1592
+ exports.SDK_VERSION = SDK_VERSION;
1593
+ exports.SuperatomClient = SuperatomClient;
1594
+ exports.UILogEntrySchema = UILogEntrySchema;
1595
+ exports.UILogsMessageSchema = UILogsMessageSchema;
1596
+ exports.UILogsPayloadSchema = UILogsPayloadSchema;
1597
+ exports.UserPromptRequestMessageSchema = UserPromptRequestMessageSchema;
1598
+ exports.UserPromptRequestPayloadSchema = UserPromptRequestPayloadSchema;
1599
+ exports.UserPromptResponseMessageSchema = UserPromptResponseMessageSchema;
1600
+ exports.UserPromptResponsePayloadSchema = UserPromptResponsePayloadSchema;
1601
+ exports.UserPromptSuggestionsRequestMessageSchema = UserPromptSuggestionsRequestMessageSchema;
1602
+ exports.UserPromptSuggestionsRequestPayloadSchema = UserPromptSuggestionsRequestPayloadSchema;
1603
+ exports.UserPromptSuggestionsResponseMessageSchema = UserPromptSuggestionsResponseMessageSchema;
1604
+ exports.UserPromptSuggestionsResponsePayloadSchema = UserPromptSuggestionsResponsePayloadSchema;
1605
+ exports.getComponent = getComponent;
1606
+ exports.getComponents = getComponents;
1607
+ exports.hasComponents = hasComponents;
1608
+ exports.services = services_exports;
1609
+ exports.setup = setup;
1610
+ //# sourceMappingURL=index.cjs.map
1611
+ //# sourceMappingURL=index.cjs.map