@robosystems/client 0.1.11 → 0.1.13

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.
@@ -1,297 +1,251 @@
1
- 'use client'
2
-
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.OperationClient = void 0;
3
5
  /**
4
6
  * General Operations Client for monitoring async operations
5
7
  * Handles graph creation, backups, imports, and other long-running tasks
6
8
  */
7
-
8
- import { cancelOperation, getOperationStatus } from '../sdk.gen'
9
- import { EventType, SSEClient } from './SSEClient'
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
- export class OperationClient {
18
- private sseClients = new Map()
19
- private cleanupTimeouts> = new Map()
20
- private config
21
- private cleanupIntervalMs = 300000 // 5 minutes
22
- private cleanupInterval?
23
-
24
- constructor(config) {
25
- this.config = config
26
-
27
- // Start periodic cleanup check every 5 minutes
28
- this.cleanupInterval = setInterval(() => {
29
- this.performPeriodicCleanup()
30
- }, this.cleanupIntervalMs)
31
- }
32
-
33
- async monitorOperation(
34
- operationId,
35
- options = {}
36
- )> {
37
- return new Promise((resolve, reject) => {
38
- const sseClient = new SSEClient(this.config)
39
- this.sseClients.set(operationId, sseClient)
40
-
41
- const timeoutHandle = options.timeout
42
- ? setTimeout(() => {
43
- this.cleanupClient(operationId)
44
- reject(new Error(`Operation timeout after ${options.timeout}ms`))
45
- }, options.timeout)
46
-
47
-
48
- sseClient
49
- .connect(operationId)
50
- .then(() => {
51
- let result = { success }
52
-
53
- // Track queue updates
54
- if (options.onQueueUpdate) {
55
- sseClient.on(EventType.QUEUE_UPDATE, (data) => {
56
- options.onQueueUpdate!(
57
- data.position || data.queue_position,
58
- data.estimated_wait_seconds || 0
59
- )
60
- })
61
- }
62
-
63
- // Track progress
64
- if (options.onProgress) {
65
- sseClient.on(EventType.OPERATION_PROGRESS, (data) => {
66
- options.onProgress!({
67
- message.message || data.status || 'Processing...',
68
- progressPercent.progress_percent || data.progress,
69
- details,
70
- })
9
+ const sdk_gen_1 = require("../sdk/sdk.gen");
10
+ const SSEClient_1 = require("./SSEClient");
11
+ class OperationClient {
12
+ constructor(config) {
13
+ this.sseClients = new Map();
14
+ this.cleanupTimeouts = new Map();
15
+ this.cleanupIntervalMs = 300000; // 5 minutes
16
+ this.config = config;
17
+ // Start periodic cleanup check every 5 minutes
18
+ this.cleanupInterval = setInterval(() => {
19
+ this.performPeriodicCleanup();
20
+ }, this.cleanupIntervalMs);
21
+ }
22
+ async monitorOperation(operationId, options = {}) {
23
+ return new Promise((resolve, reject) => {
24
+ const sseClient = new SSEClient_1.SSEClient(this.config);
25
+ this.sseClients.set(operationId, sseClient);
26
+ const timeoutHandle = options.timeout
27
+ ? setTimeout(() => {
28
+ this.cleanupClient(operationId);
29
+ reject(new Error(`Operation timeout after ${options.timeout}ms`));
30
+ }, options.timeout)
31
+ : undefined;
32
+ sseClient
33
+ .connect(operationId)
34
+ .then(() => {
35
+ let result = { success: false };
36
+ // Track queue updates
37
+ if (options.onQueueUpdate) {
38
+ sseClient.on(SSEClient_1.EventType.QUEUE_UPDATE, (data) => {
39
+ options.onQueueUpdate(data.position || data.queue_position, data.estimated_wait_seconds || 0);
40
+ });
41
+ }
42
+ // Track progress
43
+ if (options.onProgress) {
44
+ sseClient.on(SSEClient_1.EventType.OPERATION_PROGRESS, (data) => {
45
+ options.onProgress({
46
+ message: data.message || data.status || 'Processing...',
47
+ progressPercent: data.progress_percent || data.progress,
48
+ details: data,
49
+ });
50
+ });
51
+ }
52
+ // Handle completion
53
+ sseClient.on(SSEClient_1.EventType.OPERATION_COMPLETED, (data) => {
54
+ if (timeoutHandle)
55
+ clearTimeout(timeoutHandle);
56
+ result = {
57
+ success: true,
58
+ result: data.result || data,
59
+ metadata: data.metadata,
60
+ };
61
+ // Schedule cleanup after a short delay to ensure all data is received
62
+ this.scheduleCleanup(operationId, 5000);
63
+ resolve(result);
64
+ });
65
+ // Handle errors
66
+ sseClient.on(SSEClient_1.EventType.OPERATION_ERROR, (error) => {
67
+ if (timeoutHandle)
68
+ clearTimeout(timeoutHandle);
69
+ result = {
70
+ success: false,
71
+ error: error.message || error.error || 'Operation failed',
72
+ metadata: error.metadata,
73
+ };
74
+ // Schedule cleanup after a short delay
75
+ this.scheduleCleanup(operationId, 5000);
76
+ resolve(result); // Resolve with error result, not reject
77
+ });
78
+ // Handle cancellation
79
+ sseClient.on(SSEClient_1.EventType.OPERATION_CANCELLED, (data) => {
80
+ if (timeoutHandle)
81
+ clearTimeout(timeoutHandle);
82
+ result = {
83
+ success: false,
84
+ error: 'Operation cancelled',
85
+ metadata: data,
86
+ };
87
+ // Schedule cleanup after a short delay
88
+ this.scheduleCleanup(operationId, 5000);
89
+ resolve(result);
90
+ });
71
91
  })
72
- }
73
-
74
- // Handle completion
75
- sseClient.on(EventType.OPERATION_COMPLETED, (data) => {
76
- if (timeoutHandle) clearTimeout(timeoutHandle)
77
- result = {
78
- success,
79
- result.result || data,
80
- metadata.metadata,
81
- }
82
- // Schedule cleanup after a short delay to ensure all data is received
83
- this.scheduleCleanup(operationId, 5000)
84
- resolve(result)
85
- })
86
-
87
- // Handle errors
88
- sseClient.on(EventType.OPERATION_ERROR, (error) => {
89
- if (timeoutHandle) clearTimeout(timeoutHandle)
90
- result = {
91
- success,
92
- error.message || error.error || 'Operation failed',
93
- metadata.metadata,
94
- }
95
- // Schedule cleanup after a short delay
96
- this.scheduleCleanup(operationId, 5000)
97
- resolve(result) // Resolve with error result, not reject
98
- })
99
-
100
- // Handle cancellation
101
- sseClient.on(EventType.OPERATION_CANCELLED, (data) => {
102
- if (timeoutHandle) clearTimeout(timeoutHandle)
103
- result = {
104
- success,
105
- error: 'Operation cancelled',
106
- metadata,
107
- }
108
- // Schedule cleanup after a short delay
109
- this.scheduleCleanup(operationId, 5000)
110
- resolve(result)
111
- })
112
- })
113
- .catch((error) => {
114
- if (timeoutHandle) clearTimeout(timeoutHandle)
115
- this.cleanupClient(operationId)
116
- reject(error)
117
- })
118
- })
119
- }
120
-
121
- /**
122
- * Monitor multiple operations concurrently
123
- */
124
- async monitorMultiple(
125
- operationIds,
126
- options = {}
127
- )>> {
128
- const results = await Promise.all(
129
- operationIds.map(async (id) => {
130
- const result = await this.monitorOperation(id, options)
131
- return [id, result] as [string, OperationResult]
132
- })
133
- )
134
- return new Map(results)
135
- }
136
-
137
- /**
138
- * Get the current status of an operation (point-in-time check)
139
- */
140
- async getStatus(operationId) {
141
- const response = await getOperationStatus({
142
- path,
143
- })
144
- return response.data
145
- }
146
-
147
- /**
148
- * Cancel a pending or running operation
149
- */
150
- async cancelOperation(operationId) {
151
- // First close any active SSE connection
152
- this.cleanupClient(operationId)
153
-
154
- // Then cancel the operation
155
- await cancelOperationSDK({
156
- path,
157
- })
158
- }
159
-
160
- /**
161
- * Wait for an operation with a simple promise interface
162
- */
163
- async waitForOperation(operationId, timeoutMs?) {
164
- const result = await this.monitorOperation(operationId, {
165
- timeout,
166
- })
167
-
168
- if (!result.success) {
169
- throw new Error(result.error || 'Operation failed')
92
+ .catch((error) => {
93
+ if (timeoutHandle)
94
+ clearTimeout(timeoutHandle);
95
+ this.cleanupClient(operationId);
96
+ reject(error);
97
+ });
98
+ });
170
99
  }
171
-
172
- return result.result
173
- }
174
-
175
- /**
176
- * Monitor operation with async iterator for progress updates
177
- */
178
- async *monitorWithProgress(
179
- operationId
180
- )> {
181
- const progressQueue: (OperationProgress | OperationResult)[] = []
182
- let completed = false
183
- let finalResult | null = null
184
-
185
- // Start monitoring in background
186
- const monitorPromise = this.monitorOperation(operationId, {
187
- onProgress: (progress) => {
188
- progressQueue.push(progress)
189
- },
190
- onQueueUpdate: (position, estimatedWait) => {
191
- progressQueue.push({
192
- message: `Queue position: ${position}`,
193
- progressPercent,
194
- details,
195
- })
196
- },
197
- })
198
-
199
- // Handle completion
200
- monitorPromise
201
- .then((result) => {
202
- finalResult = result
203
- completed = true
204
- })
205
- .catch((error) => {
206
- finalResult = {
207
- success,
208
- error.message,
100
+ /**
101
+ * Monitor multiple operations concurrently
102
+ */
103
+ async monitorMultiple(operationIds, options = {}) {
104
+ const results = await Promise.all(operationIds.map(async (id) => {
105
+ const result = await this.monitorOperation(id, options);
106
+ return [id, result];
107
+ }));
108
+ return new Map(results);
109
+ }
110
+ /**
111
+ * Get the current status of an operation (point-in-time check)
112
+ */
113
+ async getStatus(operationId) {
114
+ const response = await (0, sdk_gen_1.getOperationStatus)({
115
+ path: { operation_id: operationId },
116
+ });
117
+ return response.data;
118
+ }
119
+ /**
120
+ * Cancel a pending or running operation
121
+ */
122
+ async cancelOperation(operationId) {
123
+ // First close any active SSE connection
124
+ this.cleanupClient(operationId);
125
+ // Then cancel the operation
126
+ await (0, sdk_gen_1.cancelOperation)({
127
+ path: { operation_id: operationId },
128
+ });
129
+ }
130
+ /**
131
+ * Wait for an operation with a simple promise interface
132
+ */
133
+ async waitForOperation(operationId, timeoutMs) {
134
+ const result = await this.monitorOperation(operationId, {
135
+ timeout: timeoutMs,
136
+ });
137
+ if (!result.success) {
138
+ throw new Error(result.error || 'Operation failed');
209
139
  }
210
- completed = true
211
- })
212
-
213
- // Yield progress updates come
214
- while (!completed || progressQueue.length > 0) {
215
- if (progressQueue.length > 0) {
216
- yield progressQueue.shift()!
217
- } else if (!completed) {
218
- // Wait for more progress
219
- await new Promise((resolve) => setTimeout(resolve, 100))
220
- }
140
+ return result.result;
221
141
  }
222
-
223
- // Yield final result
224
- if (finalResult) {
225
- yield finalResult
142
+ /**
143
+ * Monitor operation with async iterator for progress updates
144
+ */
145
+ async *monitorWithProgress(operationId) {
146
+ const progressQueue = [];
147
+ let completed = false;
148
+ let finalResult = null;
149
+ // Start monitoring in background
150
+ const monitorPromise = this.monitorOperation(operationId, {
151
+ onProgress: (progress) => {
152
+ progressQueue.push(progress);
153
+ },
154
+ onQueueUpdate: (position, estimatedWait) => {
155
+ progressQueue.push({
156
+ message: `Queue position: ${position}`,
157
+ progressPercent: 0,
158
+ details: { position, estimatedWait },
159
+ });
160
+ },
161
+ });
162
+ // Handle completion
163
+ monitorPromise
164
+ .then((result) => {
165
+ finalResult = result;
166
+ completed = true;
167
+ })
168
+ .catch((error) => {
169
+ finalResult = {
170
+ success: false,
171
+ error: error.message,
172
+ };
173
+ completed = true;
174
+ });
175
+ // Yield progress updates as they come
176
+ while (!completed || progressQueue.length > 0) {
177
+ if (progressQueue.length > 0) {
178
+ yield progressQueue.shift();
179
+ }
180
+ else if (!completed) {
181
+ // Wait for more progress
182
+ await new Promise((resolve) => setTimeout(resolve, 100));
183
+ }
184
+ }
185
+ // Yield final result
186
+ if (finalResult) {
187
+ yield finalResult;
188
+ }
226
189
  }
227
- }
228
-
229
- private cleanupClient(operationId) {
230
- const client = this.sseClients.get(operationId)
231
- if (client) {
232
- client.close()
233
- this.sseClients.delete(operationId)
190
+ cleanupClient(operationId) {
191
+ const client = this.sseClients.get(operationId);
192
+ if (client) {
193
+ client.close();
194
+ this.sseClients.delete(operationId);
195
+ }
196
+ // Clear any cleanup timeout for this operation
197
+ const timeout = this.cleanupTimeouts.get(operationId);
198
+ if (timeout) {
199
+ clearTimeout(timeout);
200
+ this.cleanupTimeouts.delete(operationId);
201
+ }
234
202
  }
235
-
236
- // Clear any cleanup timeout for this operation
237
- const timeout = this.cleanupTimeouts.get(operationId)
238
- if (timeout) {
239
- clearTimeout(timeout)
240
- this.cleanupTimeouts.delete(operationId)
203
+ /**
204
+ * Schedule automatic cleanup of SSE client after a delay
205
+ */
206
+ scheduleCleanup(operationId, delayMs = 60000) {
207
+ // Clear any existing timeout
208
+ const existingTimeout = this.cleanupTimeouts.get(operationId);
209
+ if (existingTimeout) {
210
+ clearTimeout(existingTimeout);
211
+ }
212
+ // Schedule new cleanup
213
+ const timeout = setTimeout(() => {
214
+ this.cleanupClient(operationId);
215
+ this.cleanupTimeouts.delete(operationId);
216
+ }, delayMs);
217
+ this.cleanupTimeouts.set(operationId, timeout);
241
218
  }
242
- }
243
-
244
- /**
245
- * Schedule automatic cleanup of SSE client after a delay
246
- */
247
- private scheduleCleanup(operationId, delayMs = 60000) {
248
- // Clear any existing timeout
249
- const existingTimeout = this.cleanupTimeouts.get(operationId)
250
- if (existingTimeout) {
251
- clearTimeout(existingTimeout)
219
+ /**
220
+ * Perform periodic cleanup of stale SSE connections
221
+ */
222
+ performPeriodicCleanup() {
223
+ // Check each SSE client for staleness
224
+ this.sseClients.forEach((client, operationId) => {
225
+ if (!client.isConnected()) {
226
+ // Clean up disconnected clients immediately
227
+ this.cleanupClient(operationId);
228
+ }
229
+ });
252
230
  }
253
-
254
- // Schedule new cleanup
255
- const timeout = setTimeout(() => {
256
- this.cleanupClient(operationId)
257
- this.cleanupTimeouts.delete(operationId)
258
- }, delayMs)
259
-
260
- this.cleanupTimeouts.set(operationId, timeout)
261
- }
262
-
263
- /**
264
- * Perform periodic cleanup of stale SSE connections
265
- */
266
- private performPeriodicCleanup() {
267
- // Check each SSE client for staleness
268
- this.sseClients.forEach((client, operationId) => {
269
- if (!client.isConnected()) {
270
- // Clean up disconnected clients immediately
271
- this.cleanupClient(operationId)
272
- }
273
- })
274
- }
275
-
276
- /**
277
- * Close all active SSE connections and clean up resources
278
- */
279
- closeAll() {
280
- // Clear periodic cleanup interval
281
- if (this.cleanupInterval) {
282
- clearInterval(this.cleanupInterval)
283
- this.cleanupInterval = undefined
231
+ /**
232
+ * Close all active SSE connections and clean up resources
233
+ */
234
+ closeAll() {
235
+ // Clear periodic cleanup interval
236
+ if (this.cleanupInterval) {
237
+ clearInterval(this.cleanupInterval);
238
+ this.cleanupInterval = undefined;
239
+ }
240
+ // Clear all cleanup timeouts
241
+ this.cleanupTimeouts.forEach((timeout) => {
242
+ clearTimeout(timeout);
243
+ });
244
+ this.cleanupTimeouts.clear();
245
+ // Close all SSE clients
246
+ this.sseClients.forEach((client, operationId) => {
247
+ this.cleanupClient(operationId);
248
+ });
284
249
  }
285
-
286
- // Clear all cleanup timeouts
287
- this.cleanupTimeouts.forEach((timeout) => {
288
- clearTimeout(timeout)
289
- })
290
- this.cleanupTimeouts.clear()
291
-
292
- // Close all SSE clients
293
- this.sseClients.forEach((client, operationId) => {
294
- this.cleanupClient(operationId)
295
- })
296
- }
297
250
  }
251
+ exports.OperationClient = OperationClient;
@@ -0,0 +1,50 @@
1
+ export interface QueryRequest {
2
+ query: string;
3
+ parameters?: Record<string, any>;
4
+ timeout?: number;
5
+ }
6
+ export interface QueryOptions {
7
+ mode?: 'auto' | 'sync' | 'async' | 'stream';
8
+ chunkSize?: number;
9
+ testMode?: boolean;
10
+ maxWait?: number;
11
+ onQueueUpdate?: (position: number, estimatedWait: number) => void;
12
+ onProgress?: (message: string) => void;
13
+ }
14
+ export interface QueryResult {
15
+ data: any[];
16
+ columns: string[];
17
+ row_count: number;
18
+ execution_time_ms: number;
19
+ graph_id?: string;
20
+ timestamp?: string;
21
+ }
22
+ export interface QueuedQueryResponse {
23
+ status: 'queued';
24
+ operation_id: string;
25
+ queue_position: number;
26
+ estimated_wait_seconds: number;
27
+ message: string;
28
+ }
29
+ export declare class QueryClient {
30
+ private sseClient?;
31
+ private config;
32
+ constructor(config: {
33
+ baseUrl: string;
34
+ credentials?: 'include' | 'same-origin' | 'omit';
35
+ headers?: Record<string, string>;
36
+ });
37
+ executeQuery(graphId: string, request: QueryRequest, options?: QueryOptions): Promise<QueryResult | AsyncIterableIterator<any>>;
38
+ private streamQueryResults;
39
+ private waitForQueryCompletion;
40
+ query(graphId: string, cypher: string, parameters?: Record<string, any>): Promise<QueryResult>;
41
+ streamQuery(graphId: string, cypher: string, parameters?: Record<string, any>, chunkSize?: number): AsyncIterableIterator<any>;
42
+ close(): void;
43
+ }
44
+ /**
45
+ * Error thrown when query is queued and maxWait is 0
46
+ */
47
+ export declare class QueuedQueryError extends Error {
48
+ queueInfo: QueuedQueryResponse;
49
+ constructor(queueInfo: QueuedQueryResponse);
50
+ }