@robosystems/client 0.1.12 → 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,245 +1,190 @@
1
- 'use client'
2
-
1
+ 'use client';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.QueuedQueryError = exports.QueryClient = void 0;
3
5
  /**
4
6
  * Enhanced Query Client with SSE support
5
7
  * Provides intelligent query execution with automatic strategy selection
6
8
  */
7
-
8
- import { executeCypherQuery } from '../sdk.gen'
9
- import { ExecuteCypherQueryData } from '../types.gen'
10
- import { EventType, SSEClient } from './SSEClient'
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
- export class QueryClient {
21
- private sseClient?
22
- private config
23
-
24
- constructor(config) {
25
- this.config = config
26
- }
27
-
28
- async executeQuery(
29
- graphId,
30
- request,
31
- options = {}
32
- )> {
33
- const data = {
34
- url: '/v1/{graph_id}/query',
35
- path,
36
- body,
37
- query,
9
+ const sdk_gen_1 = require("../sdk/sdk.gen");
10
+ const SSEClient_1 = require("./SSEClient");
11
+ class QueryClient {
12
+ constructor(config) {
13
+ this.config = config;
38
14
  }
39
-
40
- // Execute the query
41
- const response = await executeCypherQuery(data)
42
- const responseData = response.data
43
-
44
- // Check if this is an immediate response
45
- if (responseData?.data !== undefined && responseData?.columns) {
46
- return {
47
- data.data,
48
- columns.columns,
49
- row_count.row_count || responseData.data.length,
50
- execution_time_ms.execution_time_ms || 0,
51
- graph_id,
52
- timestamp.timestamp || new Date().toISOString(),
53
- }
54
- }
55
-
56
- // Check if this is a queued response
57
- if (responseData?.status === 'queued' && responseData?.operation_id) {
58
- const queuedResponse = responseData
59
-
60
- // Notify about queue status
61
- options.onQueueUpdate?.(queuedResponse.queue_position, queuedResponse.estimated_wait_seconds)
62
-
63
- // If user doesn't want to wait, throw with queue info
64
- if (options.maxWait === 0) {
65
- throw new QueuedQueryError(queuedResponse)
66
- }
67
-
68
- // Use SSE to monitor the operation
69
- if (options.mode === 'stream') {
70
- return this.streamQueryResults(queuedResponse.operation_id, options)
71
- } else {
72
- return this.waitForQueryCompletion(queuedResponse.operation_id, options)
73
- }
74
- }
75
-
76
- // Unexpected response format
77
- throw new Error('Unexpected response format from query endpoint')
78
- }
79
-
80
- private async *streamQueryResults(
81
- operationId,
82
- options
83
- ) {
84
- const buffer = []
85
- let completed = false
86
- let error | null = null
87
-
88
- // Set up SSE connection
89
- this.sseClient = new SSEClient(this.config)
90
- await this.sseClient.connect(operationId)
91
-
92
- // Listen for data chunks
93
- this.sseClient.on(EventType.DATA_CHUNK, (data) => {
94
- if (Array.isArray(data.rows)) {
95
- buffer.push(...data.rows)
96
- } else if (data.data) {
97
- buffer.push(...data.data)
98
- }
99
- })
100
-
101
- // Listen for queue updates
102
- this.sseClient.on(EventType.QUEUE_UPDATE, (data) => {
103
- options.onQueueUpdate?.(data.position, data.estimated_wait_seconds)
104
- })
105
-
106
- // Listen for progress
107
- this.sseClient.on(EventType.OPERATION_PROGRESS, (data) => {
108
- options.onProgress?.(data.message)
109
- })
110
-
111
- // Listen for completion
112
- this.sseClient.on(EventType.OPERATION_COMPLETED, (data) => {
113
- if (data.result?.data) {
114
- buffer.push(...data.result.data)
115
- }
116
- completed = true
117
- })
118
-
119
- // Listen for errors
120
- this.sseClient.on(EventType.OPERATION_ERROR, (err) => {
121
- error = new Error(err.message || err.error)
122
- completed = true
123
- })
124
-
125
- // Yield buffered results
126
- while (!completed || buffer.length > 0) {
127
- if (error) throw error
128
-
129
- if (buffer.length > 0) {
130
- const chunk = buffer.splice(0, options.chunkSize || 100)
131
- for (const item of chunk) {
132
- yield item
15
+ async executeQuery(graphId, request, options = {}) {
16
+ const data = {
17
+ url: '/v1/{graph_id}/query',
18
+ path: { graph_id: graphId },
19
+ body: {
20
+ query: request.query,
21
+ parameters: request.parameters,
22
+ },
23
+ query: {
24
+ mode: options.mode,
25
+ test_mode: options.testMode,
26
+ },
27
+ };
28
+ // Execute the query
29
+ const response = await (0, sdk_gen_1.executeCypherQuery)(data);
30
+ const responseData = response.data;
31
+ // Check if this is an immediate response
32
+ if (responseData?.data !== undefined && responseData?.columns) {
33
+ return {
34
+ data: responseData.data,
35
+ columns: responseData.columns,
36
+ row_count: responseData.row_count || responseData.data.length,
37
+ execution_time_ms: responseData.execution_time_ms || 0,
38
+ graph_id: graphId,
39
+ timestamp: responseData.timestamp || new Date().toISOString(),
40
+ };
133
41
  }
134
- } else if (!completed) {
135
- // Wait for more data
136
- await new Promise((resolve) => setTimeout(resolve, 100))
137
- }
42
+ // Check if this is a queued response
43
+ if (responseData?.status === 'queued' && responseData?.operation_id) {
44
+ const queuedResponse = responseData;
45
+ // Notify about queue status
46
+ options.onQueueUpdate?.(queuedResponse.queue_position, queuedResponse.estimated_wait_seconds);
47
+ // If user doesn't want to wait, throw with queue info
48
+ if (options.maxWait === 0) {
49
+ throw new QueuedQueryError(queuedResponse);
50
+ }
51
+ // Use SSE to monitor the operation
52
+ if (options.mode === 'stream') {
53
+ return this.streamQueryResults(queuedResponse.operation_id, options);
54
+ }
55
+ else {
56
+ return this.waitForQueryCompletion(queuedResponse.operation_id, options);
57
+ }
58
+ }
59
+ // Unexpected response format
60
+ throw new Error('Unexpected response format from query endpoint');
138
61
  }
139
-
140
- this.sseClient.close()
141
- this.sseClient = undefined
142
- }
143
-
144
- private async waitForQueryCompletion(
145
- operationId,
146
- options
147
- ) {
148
- return new Promise((resolve, reject) => {
149
- const sseClient = new SSEClient(this.config)
150
-
151
- sseClient
152
- .connect(operationId)
153
- .then(() => {
154
- let result | null = null
155
-
156
- // Listen for queue updates
157
- sseClient.on(EventType.QUEUE_UPDATE, (data) => {
158
- options.onQueueUpdate?.(data.position, data.estimated_wait_seconds)
159
- })
160
-
161
- // Listen for progress
162
- sseClient.on(EventType.OPERATION_PROGRESS, (data) => {
163
- options.onProgress?.(data.message)
164
- })
165
-
166
- sseClient.on(EventType.OPERATION_COMPLETED, (data) => {
167
- const queryResult = data.result || data
168
- result = {
169
- data.data || [],
170
- columns.columns || [],
171
- row_count.row_count || 0,
172
- execution_time_ms.execution_time_ms || 0,
173
- graph_id.graph_id,
174
- timestamp.timestamp || new Date().toISOString(),
62
+ async *streamQueryResults(operationId, options) {
63
+ const buffer = [];
64
+ let completed = false;
65
+ let error = null;
66
+ // Set up SSE connection
67
+ this.sseClient = new SSEClient_1.SSEClient(this.config);
68
+ await this.sseClient.connect(operationId);
69
+ // Listen for data chunks
70
+ this.sseClient.on(SSEClient_1.EventType.DATA_CHUNK, (data) => {
71
+ if (Array.isArray(data.rows)) {
72
+ buffer.push(...data.rows);
73
+ }
74
+ else if (data.data) {
75
+ buffer.push(...data.data);
76
+ }
77
+ });
78
+ // Listen for queue updates
79
+ this.sseClient.on(SSEClient_1.EventType.QUEUE_UPDATE, (data) => {
80
+ options.onQueueUpdate?.(data.position, data.estimated_wait_seconds);
81
+ });
82
+ // Listen for progress
83
+ this.sseClient.on(SSEClient_1.EventType.OPERATION_PROGRESS, (data) => {
84
+ options.onProgress?.(data.message);
85
+ });
86
+ // Listen for completion
87
+ this.sseClient.on(SSEClient_1.EventType.OPERATION_COMPLETED, (data) => {
88
+ if (data.result?.data) {
89
+ buffer.push(...data.result.data);
90
+ }
91
+ completed = true;
92
+ });
93
+ // Listen for errors
94
+ this.sseClient.on(SSEClient_1.EventType.OPERATION_ERROR, (err) => {
95
+ error = new Error(err.message || err.error);
96
+ completed = true;
97
+ });
98
+ // Yield buffered results
99
+ while (!completed || buffer.length > 0) {
100
+ if (error)
101
+ throw error;
102
+ if (buffer.length > 0) {
103
+ const chunk = buffer.splice(0, options.chunkSize || 100);
104
+ for (const item of chunk) {
105
+ yield item;
106
+ }
175
107
  }
176
- sseClient.close()
177
- resolve(result)
178
- })
179
-
180
- sseClient.on(EventType.OPERATION_ERROR, (error) => {
181
- sseClient.close()
182
- reject(new Error(error.message || error.error))
183
- })
184
-
185
- sseClient.on(EventType.OPERATION_CANCELLED, () => {
186
- sseClient.close()
187
- reject(new Error('Query cancelled'))
188
- })
189
- })
190
- .catch(reject)
191
- })
192
- }
193
-
194
- // Convenience method for simple queries
195
- async query(
196
- graphId,
197
- cypher,
198
- parameters?
199
- ) {
200
- return this.executeQuery(
201
- graphId,
202
- { query, parameters },
203
- { mode }
204
- )
205
- }
206
-
207
- // Streaming query for large results
208
- async *streamQuery(
209
- graphId,
210
- cypher,
211
- parameters?,
212
- chunkSize = 1000
213
- ) {
214
- const result = await this.executeQuery(
215
- graphId,
216
- { query, parameters },
217
- { mode, chunkSize }
218
- )
219
-
220
- if (Symbol.asyncIterator in (result)) {
221
- yield* result
222
- } else {
223
- // If not streaming, yield all results at once
224
- yield* (result).data
108
+ else if (!completed) {
109
+ // Wait for more data
110
+ await new Promise((resolve) => setTimeout(resolve, 100));
111
+ }
112
+ }
113
+ this.sseClient.close();
114
+ this.sseClient = undefined;
115
+ }
116
+ async waitForQueryCompletion(operationId, options) {
117
+ return new Promise((resolve, reject) => {
118
+ const sseClient = new SSEClient_1.SSEClient(this.config);
119
+ sseClient
120
+ .connect(operationId)
121
+ .then(() => {
122
+ let result = null;
123
+ // Listen for queue updates
124
+ sseClient.on(SSEClient_1.EventType.QUEUE_UPDATE, (data) => {
125
+ options.onQueueUpdate?.(data.position, data.estimated_wait_seconds);
126
+ });
127
+ // Listen for progress
128
+ sseClient.on(SSEClient_1.EventType.OPERATION_PROGRESS, (data) => {
129
+ options.onProgress?.(data.message);
130
+ });
131
+ sseClient.on(SSEClient_1.EventType.OPERATION_COMPLETED, (data) => {
132
+ const queryResult = data.result || data;
133
+ result = {
134
+ data: queryResult.data || [],
135
+ columns: queryResult.columns || [],
136
+ row_count: queryResult.row_count || 0,
137
+ execution_time_ms: queryResult.execution_time_ms || 0,
138
+ graph_id: queryResult.graph_id,
139
+ timestamp: queryResult.timestamp || new Date().toISOString(),
140
+ };
141
+ sseClient.close();
142
+ resolve(result);
143
+ });
144
+ sseClient.on(SSEClient_1.EventType.OPERATION_ERROR, (error) => {
145
+ sseClient.close();
146
+ reject(new Error(error.message || error.error));
147
+ });
148
+ sseClient.on(SSEClient_1.EventType.OPERATION_CANCELLED, () => {
149
+ sseClient.close();
150
+ reject(new Error('Query cancelled'));
151
+ });
152
+ })
153
+ .catch(reject);
154
+ });
225
155
  }
226
- }
227
-
228
- // Cancel any active SSE connections
229
- close() {
230
- if (this.sseClient) {
231
- this.sseClient.close()
232
- this.sseClient = undefined
156
+ // Convenience method for simple queries
157
+ async query(graphId, cypher, parameters) {
158
+ return this.executeQuery(graphId, { query: cypher, parameters }, { mode: 'auto' });
159
+ }
160
+ // Streaming query for large results
161
+ async *streamQuery(graphId, cypher, parameters, chunkSize = 1000) {
162
+ const result = await this.executeQuery(graphId, { query: cypher, parameters }, { mode: 'stream', chunkSize });
163
+ if (Symbol.asyncIterator in result) {
164
+ yield* result;
165
+ }
166
+ else {
167
+ // If not streaming, yield all results at once
168
+ yield* result.data;
169
+ }
170
+ }
171
+ // Cancel any active SSE connections
172
+ close() {
173
+ if (this.sseClient) {
174
+ this.sseClient.close();
175
+ this.sseClient = undefined;
176
+ }
233
177
  }
234
- }
235
178
  }
236
-
179
+ exports.QueryClient = QueryClient;
237
180
  /**
238
181
  * Error thrown when query is queued and maxWait is 0
239
182
  */
240
- export class QueuedQueryError extends Error {
241
- constructor(public queueInfo) {
242
- super('Query was queued')
243
- this.name = 'QueuedQueryError'
244
- }
183
+ class QueuedQueryError extends Error {
184
+ constructor(queueInfo) {
185
+ super('Query was queued');
186
+ this.queueInfo = queueInfo;
187
+ this.name = 'QueuedQueryError';
188
+ }
245
189
  }
190
+ exports.QueuedQueryError = QueuedQueryError;