@robosystems/client 2.5.3 → 2.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/InvestorClient.d.ts +7 -1
- package/artifacts/InvestorClient.js +24 -14
- package/artifacts/InvestorClient.ts +46 -27
- package/artifacts/LedgerClient.d.ts +26 -18
- package/artifacts/LedgerClient.js +119 -124
- package/artifacts/LedgerClient.ts +183 -243
- package/artifacts/OperationClient.js +24 -1
- package/artifacts/OperationClient.ts +24 -1
- package/artifacts/OperatorClient.d.ts +10 -0
- package/artifacts/OperatorClient.js +72 -17
- package/artifacts/OperatorClient.ts +79 -21
- package/artifacts/QueryClient.d.ts +2 -1
- package/artifacts/QueryClient.js +17 -58
- package/artifacts/QueryClient.ts +21 -68
- package/artifacts/SSEClient.d.ts +1 -0
- package/artifacts/SSEClient.js +56 -7
- package/artifacts/SSEClient.ts +57 -7
- package/artifacts/config.js +18 -4
- package/artifacts/config.ts +18 -4
- package/artifacts/graphql/client.d.ts +10 -0
- package/artifacts/graphql/client.js +33 -21
- package/artifacts/graphql/client.ts +38 -23
- package/artifacts/index.d.ts +2 -2
- package/artifacts/index.js +6 -6
- package/artifacts/index.ts +8 -8
- package/artifacts/rest.d.ts +22 -0
- package/artifacts/rest.js +35 -0
- package/artifacts/rest.ts +63 -0
- package/artifacts/retry.js +8 -3
- package/artifacts/retry.ts +8 -3
- package/package.json +9 -1
- package/sdk/types.gen.d.ts +13 -1
- package/sdk/types.gen.ts +13 -1
- package/types.gen.d.ts +13 -1
- package/types.gen.ts +13 -1
|
@@ -7,6 +7,7 @@ exports.OperationClient = void 0;
|
|
|
7
7
|
* Handles graph creation, backups, imports, and other long-running tasks
|
|
8
8
|
*/
|
|
9
9
|
const sdk_gen_1 = require("../sdk.gen");
|
|
10
|
+
const rest_1 = require("./rest");
|
|
10
11
|
const SSEClient_1 = require("./SSEClient");
|
|
11
12
|
class OperationClient {
|
|
12
13
|
constructor(config) {
|
|
@@ -25,8 +26,9 @@ class OperationClient {
|
|
|
25
26
|
this.sseClients.set(operationId, sseClient);
|
|
26
27
|
const timeoutHandle = options.timeout
|
|
27
28
|
? setTimeout(() => {
|
|
28
|
-
|
|
29
|
+
// Reject before closing: the close settles the promise too.
|
|
29
30
|
reject(new Error(`Operation timeout after ${options.timeout}ms`));
|
|
31
|
+
this.cleanupClient(operationId);
|
|
30
32
|
}, options.timeout)
|
|
31
33
|
: undefined;
|
|
32
34
|
sseClient
|
|
@@ -102,6 +104,25 @@ class OperationClient {
|
|
|
102
104
|
this.scheduleCleanup(operationId, 5000);
|
|
103
105
|
resolve(result);
|
|
104
106
|
});
|
|
107
|
+
// The server ended the stream with its own error (connection cap,
|
|
108
|
+
// unknown operation, a failed stream). The SSE client closes next.
|
|
109
|
+
sseClient.on('error', (err) => {
|
|
110
|
+
if (timeoutHandle)
|
|
111
|
+
clearTimeout(timeoutHandle);
|
|
112
|
+
resolve({ success: false, error: err?.error || 'SSE stream error' });
|
|
113
|
+
});
|
|
114
|
+
// Any other close before a terminal event — a reconnect that failed
|
|
115
|
+
// before opening, cancelOperation(), closeAll(), the periodic
|
|
116
|
+
// cleanup. The SSE client drops its listeners as it closes, so this
|
|
117
|
+
// is the last chance to settle; after a terminal event it is a no-op.
|
|
118
|
+
sseClient.on('closed', () => {
|
|
119
|
+
if (timeoutHandle)
|
|
120
|
+
clearTimeout(timeoutHandle);
|
|
121
|
+
resolve({
|
|
122
|
+
success: false,
|
|
123
|
+
error: 'SSE stream closed before the operation finished',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
105
126
|
})
|
|
106
127
|
.catch((error) => {
|
|
107
128
|
if (timeoutHandle)
|
|
@@ -126,6 +147,7 @@ class OperationClient {
|
|
|
126
147
|
*/
|
|
127
148
|
async getStatus(operationId) {
|
|
128
149
|
const response = await (0, sdk_gen_1.getOperationStatus)({
|
|
150
|
+
...(await (0, rest_1.restCallOptions)(this.config)),
|
|
129
151
|
path: { operation_id: operationId },
|
|
130
152
|
});
|
|
131
153
|
return response.data;
|
|
@@ -138,6 +160,7 @@ class OperationClient {
|
|
|
138
160
|
this.cleanupClient(operationId);
|
|
139
161
|
// Then cancel the operation
|
|
140
162
|
await (0, sdk_gen_1.cancelOperation)({
|
|
163
|
+
...(await (0, rest_1.restCallOptions)(this.config)),
|
|
141
164
|
path: { operation_id: operationId },
|
|
142
165
|
});
|
|
143
166
|
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { cancelOperation as cancelOperationSDK, getOperationStatus } from '../sdk.gen'
|
|
9
9
|
import type { TokenProvider } from './graphql/client'
|
|
10
|
+
import { restCallOptions } from './rest'
|
|
10
11
|
import { EventType, SSEClient } from './SSEClient'
|
|
11
12
|
|
|
12
13
|
export interface OperationProgress {
|
|
@@ -70,8 +71,9 @@ export class OperationClient {
|
|
|
70
71
|
|
|
71
72
|
const timeoutHandle = options.timeout
|
|
72
73
|
? setTimeout(() => {
|
|
73
|
-
|
|
74
|
+
// Reject before closing: the close settles the promise too.
|
|
74
75
|
reject(new Error(`Operation timeout after ${options.timeout}ms`))
|
|
76
|
+
this.cleanupClient(operationId)
|
|
75
77
|
}, options.timeout)
|
|
76
78
|
: undefined
|
|
77
79
|
|
|
@@ -154,6 +156,25 @@ export class OperationClient {
|
|
|
154
156
|
this.scheduleCleanup(operationId, 5000)
|
|
155
157
|
resolve(result)
|
|
156
158
|
})
|
|
159
|
+
|
|
160
|
+
// The server ended the stream with its own error (connection cap,
|
|
161
|
+
// unknown operation, a failed stream). The SSE client closes next.
|
|
162
|
+
sseClient.on('error', (err) => {
|
|
163
|
+
if (timeoutHandle) clearTimeout(timeoutHandle)
|
|
164
|
+
resolve({ success: false, error: err?.error || 'SSE stream error' })
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
// Any other close before a terminal event — a reconnect that failed
|
|
168
|
+
// before opening, cancelOperation(), closeAll(), the periodic
|
|
169
|
+
// cleanup. The SSE client drops its listeners as it closes, so this
|
|
170
|
+
// is the last chance to settle; after a terminal event it is a no-op.
|
|
171
|
+
sseClient.on('closed', () => {
|
|
172
|
+
if (timeoutHandle) clearTimeout(timeoutHandle)
|
|
173
|
+
resolve({
|
|
174
|
+
success: false,
|
|
175
|
+
error: 'SSE stream closed before the operation finished',
|
|
176
|
+
})
|
|
177
|
+
})
|
|
157
178
|
})
|
|
158
179
|
.catch((error) => {
|
|
159
180
|
if (timeoutHandle) clearTimeout(timeoutHandle)
|
|
@@ -184,6 +205,7 @@ export class OperationClient {
|
|
|
184
205
|
*/
|
|
185
206
|
async getStatus(operationId: string): Promise<any> {
|
|
186
207
|
const response = await getOperationStatus({
|
|
208
|
+
...(await restCallOptions(this.config)),
|
|
187
209
|
path: { operation_id: operationId },
|
|
188
210
|
})
|
|
189
211
|
return response.data
|
|
@@ -198,6 +220,7 @@ export class OperationClient {
|
|
|
198
220
|
|
|
199
221
|
// Then cancel the operation
|
|
200
222
|
await cancelOperationSDK({
|
|
223
|
+
...(await restCallOptions(this.config)),
|
|
201
224
|
path: { operation_id: operationId },
|
|
202
225
|
})
|
|
203
226
|
}
|
|
@@ -62,6 +62,16 @@ export interface OperatorClientConfig {
|
|
|
62
62
|
*/
|
|
63
63
|
tokenProvider?: TokenProvider;
|
|
64
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* An operator request the API refused — 402 (credits), 403, 404, 422, 500 —
|
|
67
|
+
* or one that never reached it (`status` undefined). `detail` is the
|
|
68
|
+
* server's `detail` field, or the network error.
|
|
69
|
+
*/
|
|
70
|
+
export declare class OperatorRequestError extends Error {
|
|
71
|
+
status: number | undefined;
|
|
72
|
+
detail: unknown;
|
|
73
|
+
constructor(message: string, status: number | undefined, detail: unknown);
|
|
74
|
+
}
|
|
65
75
|
export declare class OperatorClient {
|
|
66
76
|
private sseClient?;
|
|
67
77
|
private config;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
"use strict";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.QueuedOperatorError = exports.OperatorRunError = exports.OperatorClient = void 0;
|
|
4
|
+
exports.QueuedOperatorError = exports.OperatorRunError = exports.OperatorClient = exports.OperatorRequestError = void 0;
|
|
5
5
|
/**
|
|
6
6
|
* Enhanced AI Operator Client with SSE support
|
|
7
7
|
* Provides intelligent operator execution with automatic strategy selection
|
|
8
8
|
*/
|
|
9
9
|
const sdk_gen_1 = require("../sdk.gen");
|
|
10
|
+
const rest_1 = require("./rest");
|
|
10
11
|
const SSEClient_1 = require("./SSEClient");
|
|
11
12
|
const DEFAULT_POLL_INTERVAL_MS = 2000;
|
|
12
13
|
// Consecutive `/status` failures tolerated before the polling fallback gives
|
|
@@ -23,11 +24,28 @@ const describeEnvelopeError = (err) => {
|
|
|
23
24
|
if (err instanceof Error)
|
|
24
25
|
return err.message;
|
|
25
26
|
if (err && typeof err === 'object' && 'detail' in err)
|
|
26
|
-
return
|
|
27
|
+
return describeDetail(err.detail);
|
|
27
28
|
if (err === undefined || err === null)
|
|
28
29
|
return 'empty response';
|
|
29
30
|
return typeof err === 'string' ? err : JSON.stringify(err);
|
|
30
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* A FastAPI `detail`: a string, an object such as the 402 credit refusal
|
|
34
|
+
* (`{code, message, ...}`), or a 422's list of validation errors.
|
|
35
|
+
*/
|
|
36
|
+
const describeDetail = (detail) => {
|
|
37
|
+
if (typeof detail === 'string')
|
|
38
|
+
return detail;
|
|
39
|
+
if (Array.isArray(detail)) {
|
|
40
|
+
return detail
|
|
41
|
+
.map((d) => (d && typeof d === 'object' && 'msg' in d ? d.msg : String(d)))
|
|
42
|
+
.join('; ');
|
|
43
|
+
}
|
|
44
|
+
if (detail && typeof detail === 'object' && 'message' in detail) {
|
|
45
|
+
return String(detail.message);
|
|
46
|
+
}
|
|
47
|
+
return JSON.stringify(detail);
|
|
48
|
+
};
|
|
31
49
|
/**
|
|
32
50
|
* Shape an operator payload — the sync 200 body, the `operation_completed`
|
|
33
51
|
* event, or the `/status` `result` — into the public result type. All three
|
|
@@ -49,6 +67,28 @@ function toOperatorResult(data) {
|
|
|
49
67
|
}
|
|
50
68
|
return result;
|
|
51
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* An operator request the API refused — 402 (credits), 403, 404, 422, 500 —
|
|
72
|
+
* or one that never reached it (`status` undefined). `detail` is the
|
|
73
|
+
* server's `detail` field, or the network error.
|
|
74
|
+
*/
|
|
75
|
+
class OperatorRequestError extends Error {
|
|
76
|
+
constructor(message, status, detail) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.status = status;
|
|
79
|
+
this.detail = detail;
|
|
80
|
+
this.name = 'OperatorRequestError';
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.OperatorRequestError = OperatorRequestError;
|
|
84
|
+
function throwIfRequestFailed(response) {
|
|
85
|
+
if (response.error === undefined)
|
|
86
|
+
return;
|
|
87
|
+
const status = response.response?.status;
|
|
88
|
+
const error = response.error;
|
|
89
|
+
const detail = error instanceof Error ? error : (error?.detail ?? error);
|
|
90
|
+
throw new OperatorRequestError(`Operator request failed${status ? ` (${status})` : ''}: ${describeEnvelopeError(response.error)}`, status, detail);
|
|
91
|
+
}
|
|
52
92
|
/** Internal: a `/status` verdict that must not be retried. */
|
|
53
93
|
class PollAbort extends Error {
|
|
54
94
|
}
|
|
@@ -72,7 +112,11 @@ class OperatorClient {
|
|
|
72
112
|
force_extended_analysis: request.forceExtendedAnalysis,
|
|
73
113
|
},
|
|
74
114
|
};
|
|
75
|
-
const response = await (0, sdk_gen_1.autoSelectOperator)(
|
|
115
|
+
const response = await (0, sdk_gen_1.autoSelectOperator)({
|
|
116
|
+
...(await (0, rest_1.restCallOptions)(this.config)),
|
|
117
|
+
...data,
|
|
118
|
+
});
|
|
119
|
+
throwIfRequestFailed(response);
|
|
76
120
|
return this.settle(response.data, options);
|
|
77
121
|
}
|
|
78
122
|
/**
|
|
@@ -91,7 +135,11 @@ class OperatorClient {
|
|
|
91
135
|
force_extended_analysis: request.forceExtendedAnalysis,
|
|
92
136
|
},
|
|
93
137
|
};
|
|
94
|
-
const response = await (0, sdk_gen_1.executeSpecificOperator)(
|
|
138
|
+
const response = await (0, sdk_gen_1.executeSpecificOperator)({
|
|
139
|
+
...(await (0, rest_1.restCallOptions)(this.config)),
|
|
140
|
+
...data,
|
|
141
|
+
});
|
|
142
|
+
throwIfRequestFailed(response);
|
|
95
143
|
return this.settle(response.data, options);
|
|
96
144
|
}
|
|
97
145
|
/**
|
|
@@ -140,12 +188,13 @@ class OperatorClient {
|
|
|
140
188
|
this.sseClient = undefined;
|
|
141
189
|
}
|
|
142
190
|
};
|
|
143
|
-
// The stream
|
|
144
|
-
// before opening, retries exhausted,
|
|
145
|
-
//
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
|
|
191
|
+
// The stream ended before a terminal event: a reconnect that failed
|
|
192
|
+
// before opening, retries exhausted, the server's own stream `error`
|
|
193
|
+
// (the per-user connection cap among them), or this client being
|
|
194
|
+
// closed. The SSE client clears its listeners as it closes, so without
|
|
195
|
+
// this the promise would never settle. The run finishes whether or not
|
|
196
|
+
// anyone watches, so a dropped stream follows it over `/status` instead.
|
|
197
|
+
const followByPolling = (streamError) => {
|
|
149
198
|
if (settled)
|
|
150
199
|
return;
|
|
151
200
|
settled = true;
|
|
@@ -154,7 +203,15 @@ class OperatorClient {
|
|
|
154
203
|
return;
|
|
155
204
|
}
|
|
156
205
|
this.sseClient = undefined;
|
|
157
|
-
this.pollForCompletion(operationId, options,
|
|
206
|
+
this.pollForCompletion(operationId, options, streamError).then(resolve, reject);
|
|
207
|
+
};
|
|
208
|
+
sseClient.on('closed', () => {
|
|
209
|
+
followByPolling(new Error('the stream closed before the run finished'));
|
|
210
|
+
});
|
|
211
|
+
// A stream-level failure, not a run failure (those arrive as
|
|
212
|
+
// `operation_error`); the SSE client closes right after emitting it.
|
|
213
|
+
sseClient.on('error', (error) => {
|
|
214
|
+
followByPolling(new Error(error?.error || error?.message || 'stream error'));
|
|
158
215
|
});
|
|
159
216
|
// Listen for progress events
|
|
160
217
|
sseClient.on(SSEClient_1.EventType.OPERATION_PROGRESS, (data) => {
|
|
@@ -191,11 +248,6 @@ class OperatorClient {
|
|
|
191
248
|
finish();
|
|
192
249
|
reject(new OperatorRunError('Agent execution cancelled', 'cancelled', []));
|
|
193
250
|
});
|
|
194
|
-
// Handle generic error event
|
|
195
|
-
sseClient.on('error', (error) => {
|
|
196
|
-
finish();
|
|
197
|
-
reject(new Error(error.error || error.message || 'Agent execution failed'));
|
|
198
|
-
});
|
|
199
251
|
});
|
|
200
252
|
}
|
|
201
253
|
/**
|
|
@@ -211,7 +263,10 @@ class OperatorClient {
|
|
|
211
263
|
for (;;) {
|
|
212
264
|
let status;
|
|
213
265
|
try {
|
|
214
|
-
const response = await (0, sdk_gen_1.getOperationStatus)({
|
|
266
|
+
const response = await (0, sdk_gen_1.getOperationStatus)({
|
|
267
|
+
...(await (0, rest_1.restCallOptions)(this.config)),
|
|
268
|
+
path: { operation_id: operationId },
|
|
269
|
+
});
|
|
215
270
|
if (response.error || !response.data) {
|
|
216
271
|
const httpStatus = response.response?.status;
|
|
217
272
|
const detail = describeEnvelopeError(response.error);
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { autoSelectOperator, executeSpecificOperator, getOperationStatus } from '../sdk.gen'
|
|
9
9
|
import type { AutoSelectOperatorData, ExecuteSpecificOperatorData } from '../types.gen'
|
|
10
10
|
import type { TokenProvider } from './graphql/client'
|
|
11
|
+
import { restCallOptions } from './rest'
|
|
11
12
|
import { EventType, SSEClient } from './SSEClient'
|
|
12
13
|
|
|
13
14
|
export interface OperatorQueryRequest {
|
|
@@ -92,11 +93,28 @@ const describeError = (err: unknown): string => (err instanceof Error ? err.mess
|
|
|
92
93
|
*/
|
|
93
94
|
const describeEnvelopeError = (err: unknown): string => {
|
|
94
95
|
if (err instanceof Error) return err.message
|
|
95
|
-
if (err && typeof err === 'object' && 'detail' in err) return
|
|
96
|
+
if (err && typeof err === 'object' && 'detail' in err) return describeDetail((err as any).detail)
|
|
96
97
|
if (err === undefined || err === null) return 'empty response'
|
|
97
98
|
return typeof err === 'string' ? err : JSON.stringify(err)
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A FastAPI `detail`: a string, an object such as the 402 credit refusal
|
|
103
|
+
* (`{code, message, ...}`), or a 422's list of validation errors.
|
|
104
|
+
*/
|
|
105
|
+
const describeDetail = (detail: unknown): string => {
|
|
106
|
+
if (typeof detail === 'string') return detail
|
|
107
|
+
if (Array.isArray(detail)) {
|
|
108
|
+
return detail
|
|
109
|
+
.map((d) => (d && typeof d === 'object' && 'msg' in d ? d.msg : String(d)))
|
|
110
|
+
.join('; ')
|
|
111
|
+
}
|
|
112
|
+
if (detail && typeof detail === 'object' && 'message' in detail) {
|
|
113
|
+
return String((detail as any).message)
|
|
114
|
+
}
|
|
115
|
+
return JSON.stringify(detail)
|
|
116
|
+
}
|
|
117
|
+
|
|
100
118
|
/**
|
|
101
119
|
* Shape an operator payload — the sync 200 body, the `operation_completed`
|
|
102
120
|
* event, or the `/status` `result` — into the public result type. All three
|
|
@@ -119,6 +137,34 @@ function toOperatorResult(data: Record<string, any>): OperatorResult {
|
|
|
119
137
|
return result
|
|
120
138
|
}
|
|
121
139
|
|
|
140
|
+
/**
|
|
141
|
+
* An operator request the API refused — 402 (credits), 403, 404, 422, 500 —
|
|
142
|
+
* or one that never reached it (`status` undefined). `detail` is the
|
|
143
|
+
* server's `detail` field, or the network error.
|
|
144
|
+
*/
|
|
145
|
+
export class OperatorRequestError extends Error {
|
|
146
|
+
constructor(
|
|
147
|
+
message: string,
|
|
148
|
+
public status: number | undefined,
|
|
149
|
+
public detail: unknown
|
|
150
|
+
) {
|
|
151
|
+
super(message)
|
|
152
|
+
this.name = 'OperatorRequestError'
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function throwIfRequestFailed(response: { error?: unknown; response?: Response }): void {
|
|
157
|
+
if (response.error === undefined) return
|
|
158
|
+
const status = response.response?.status
|
|
159
|
+
const error = response.error as any
|
|
160
|
+
const detail = error instanceof Error ? error : (error?.detail ?? error)
|
|
161
|
+
throw new OperatorRequestError(
|
|
162
|
+
`Operator request failed${status ? ` (${status})` : ''}: ${describeEnvelopeError(response.error)}`,
|
|
163
|
+
status,
|
|
164
|
+
detail
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
122
168
|
/** Internal: a `/status` verdict that must not be retried. */
|
|
123
169
|
class PollAbort extends Error {}
|
|
124
170
|
|
|
@@ -151,7 +197,11 @@ export class OperatorClient {
|
|
|
151
197
|
},
|
|
152
198
|
}
|
|
153
199
|
|
|
154
|
-
const response = await autoSelectOperator(
|
|
200
|
+
const response = await autoSelectOperator({
|
|
201
|
+
...(await restCallOptions(this.config)),
|
|
202
|
+
...data,
|
|
203
|
+
})
|
|
204
|
+
throwIfRequestFailed(response)
|
|
155
205
|
return this.settle(response.data, options)
|
|
156
206
|
}
|
|
157
207
|
|
|
@@ -177,7 +227,11 @@ export class OperatorClient {
|
|
|
177
227
|
},
|
|
178
228
|
}
|
|
179
229
|
|
|
180
|
-
const response = await executeSpecificOperator(
|
|
230
|
+
const response = await executeSpecificOperator({
|
|
231
|
+
...(await restCallOptions(this.config)),
|
|
232
|
+
...data,
|
|
233
|
+
})
|
|
234
|
+
throwIfRequestFailed(response)
|
|
181
235
|
return this.settle(response.data, options)
|
|
182
236
|
}
|
|
183
237
|
|
|
@@ -238,12 +292,13 @@ export class OperatorClient {
|
|
|
238
292
|
}
|
|
239
293
|
}
|
|
240
294
|
|
|
241
|
-
// The stream
|
|
242
|
-
// before opening, retries exhausted,
|
|
243
|
-
//
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
|
|
295
|
+
// The stream ended before a terminal event: a reconnect that failed
|
|
296
|
+
// before opening, retries exhausted, the server's own stream `error`
|
|
297
|
+
// (the per-user connection cap among them), or this client being
|
|
298
|
+
// closed. The SSE client clears its listeners as it closes, so without
|
|
299
|
+
// this the promise would never settle. The run finishes whether or not
|
|
300
|
+
// anyone watches, so a dropped stream follows it over `/status` instead.
|
|
301
|
+
const followByPolling = (streamError: Error) => {
|
|
247
302
|
if (settled) return
|
|
248
303
|
settled = true
|
|
249
304
|
if (this.sseClient !== sseClient) {
|
|
@@ -251,11 +306,17 @@ export class OperatorClient {
|
|
|
251
306
|
return
|
|
252
307
|
}
|
|
253
308
|
this.sseClient = undefined
|
|
254
|
-
this.pollForCompletion(
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
309
|
+
this.pollForCompletion(operationId, options, streamError).then(resolve, reject)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
sseClient.on('closed', () => {
|
|
313
|
+
followByPolling(new Error('the stream closed before the run finished'))
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
// A stream-level failure, not a run failure (those arrive as
|
|
317
|
+
// `operation_error`); the SSE client closes right after emitting it.
|
|
318
|
+
sseClient.on('error', (error) => {
|
|
319
|
+
followByPolling(new Error(error?.error || error?.message || 'stream error'))
|
|
259
320
|
})
|
|
260
321
|
|
|
261
322
|
// Listen for progress events
|
|
@@ -306,12 +367,6 @@ export class OperatorClient {
|
|
|
306
367
|
finish()
|
|
307
368
|
reject(new OperatorRunError('Agent execution cancelled', 'cancelled', []))
|
|
308
369
|
})
|
|
309
|
-
|
|
310
|
-
// Handle generic error event
|
|
311
|
-
sseClient.on('error' as EventType, (error) => {
|
|
312
|
-
finish()
|
|
313
|
-
reject(new Error(error.error || error.message || 'Agent execution failed'))
|
|
314
|
-
})
|
|
315
370
|
})
|
|
316
371
|
}
|
|
317
372
|
|
|
@@ -334,7 +389,10 @@ export class OperatorClient {
|
|
|
334
389
|
for (;;) {
|
|
335
390
|
let status: any
|
|
336
391
|
try {
|
|
337
|
-
const response = await getOperationStatus({
|
|
392
|
+
const response = await getOperationStatus({
|
|
393
|
+
...(await restCallOptions(this.config)),
|
|
394
|
+
path: { operation_id: operationId },
|
|
395
|
+
})
|
|
338
396
|
if (response.error || !response.data) {
|
|
339
397
|
const httpStatus = response.response?.status
|
|
340
398
|
const detail = describeEnvelopeError(response.error)
|
|
@@ -2,10 +2,12 @@ import type { TokenProvider } from './graphql/client';
|
|
|
2
2
|
export interface QueryRequest {
|
|
3
3
|
query: string;
|
|
4
4
|
parameters?: Record<string, any>;
|
|
5
|
+
/** Server-side query timeout in whole seconds (the API accepts 1-300). */
|
|
5
6
|
timeout?: number;
|
|
6
7
|
}
|
|
7
8
|
export interface QueryOptions {
|
|
8
9
|
mode?: 'auto' | 'sync' | 'async' | 'stream';
|
|
10
|
+
/** Rows per streamed chunk (the API accepts 10-10000). */
|
|
9
11
|
chunkSize?: number;
|
|
10
12
|
testMode?: boolean;
|
|
11
13
|
maxWait?: number;
|
|
@@ -39,7 +41,6 @@ export declare class QueryClient {
|
|
|
39
41
|
});
|
|
40
42
|
executeQuery(graphId: string, request: QueryRequest, options?: QueryOptions): Promise<QueryResult | AsyncIterableIterator<any>>;
|
|
41
43
|
private parseNDJSONResponse;
|
|
42
|
-
private streamQueryResults;
|
|
43
44
|
private waitForQueryCompletion;
|
|
44
45
|
query(graphId: string, cypher: string, parameters?: Record<string, any>): Promise<QueryResult>;
|
|
45
46
|
streamQuery(graphId: string, cypher: string, parameters?: Record<string, any>, chunkSize?: number): AsyncIterableIterator<any>;
|
package/artifacts/QueryClient.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.QueuedQueryError = exports.QueryClient = void 0;
|
|
|
7
7
|
* Provides intelligent query execution with automatic strategy selection
|
|
8
8
|
*/
|
|
9
9
|
const sdk_gen_1 = require("../sdk.gen");
|
|
10
|
+
const rest_1 = require("./rest");
|
|
10
11
|
const SSEClient_1 = require("./SSEClient");
|
|
11
12
|
class QueryClient {
|
|
12
13
|
constructor(config) {
|
|
@@ -19,15 +20,17 @@ class QueryClient {
|
|
|
19
20
|
body: {
|
|
20
21
|
query: request.query,
|
|
21
22
|
parameters: request.parameters,
|
|
23
|
+
timeout: request.timeout,
|
|
22
24
|
},
|
|
23
25
|
query: {
|
|
24
26
|
mode: options.mode,
|
|
27
|
+
chunk_size: options.chunkSize,
|
|
25
28
|
test_mode: options.testMode,
|
|
26
29
|
},
|
|
27
30
|
// For streaming mode, don't parse response - get raw Response object
|
|
28
31
|
...(options.mode === 'stream' ? { parseAs: 'stream' } : {}),
|
|
29
32
|
};
|
|
30
|
-
const response = await (0, sdk_gen_1.executeCypher)(data);
|
|
33
|
+
const response = await (0, sdk_gen_1.executeCypher)({ ...(await (0, rest_1.restCallOptions)(this.config)), ...data });
|
|
31
34
|
// Check for errors in the response (network errors, etc.)
|
|
32
35
|
if ('error' in response && response.error) {
|
|
33
36
|
const error = response.error;
|
|
@@ -182,63 +185,10 @@ class QueryClient {
|
|
|
182
185
|
timestamp: new Date().toISOString(),
|
|
183
186
|
};
|
|
184
187
|
}
|
|
185
|
-
async *streamQueryResults(operationId, options) {
|
|
186
|
-
const buffer = [];
|
|
187
|
-
let completed = false;
|
|
188
|
-
let error = null;
|
|
189
|
-
// Set up SSE connection
|
|
190
|
-
this.sseClient = new SSEClient_1.SSEClient(this.config);
|
|
191
|
-
await this.sseClient.connect(operationId);
|
|
192
|
-
// Listen for data chunks
|
|
193
|
-
this.sseClient.on(SSEClient_1.EventType.DATA_CHUNK, (data) => {
|
|
194
|
-
if (Array.isArray(data.rows)) {
|
|
195
|
-
buffer.push(...data.rows);
|
|
196
|
-
}
|
|
197
|
-
else if (data.data) {
|
|
198
|
-
buffer.push(...data.data);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
// Listen for queue updates
|
|
202
|
-
this.sseClient.on(SSEClient_1.EventType.QUEUE_UPDATE, (data) => {
|
|
203
|
-
options.onQueueUpdate?.(data.position, data.estimated_wait_seconds);
|
|
204
|
-
});
|
|
205
|
-
// Listen for progress
|
|
206
|
-
this.sseClient.on(SSEClient_1.EventType.OPERATION_PROGRESS, (data) => {
|
|
207
|
-
options.onProgress?.(data.message);
|
|
208
|
-
});
|
|
209
|
-
// Listen for completion
|
|
210
|
-
this.sseClient.on(SSEClient_1.EventType.OPERATION_COMPLETED, (data) => {
|
|
211
|
-
if (data.result?.data) {
|
|
212
|
-
buffer.push(...data.result.data);
|
|
213
|
-
}
|
|
214
|
-
completed = true;
|
|
215
|
-
});
|
|
216
|
-
// Listen for errors
|
|
217
|
-
this.sseClient.on(SSEClient_1.EventType.OPERATION_ERROR, (err) => {
|
|
218
|
-
error = new Error(err.message || err.error);
|
|
219
|
-
completed = true;
|
|
220
|
-
});
|
|
221
|
-
// Yield buffered results
|
|
222
|
-
while (!completed || buffer.length > 0) {
|
|
223
|
-
if (error)
|
|
224
|
-
throw error;
|
|
225
|
-
if (buffer.length > 0) {
|
|
226
|
-
const chunk = buffer.splice(0, options.chunkSize || 100);
|
|
227
|
-
for (const item of chunk) {
|
|
228
|
-
yield item;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
else if (!completed) {
|
|
232
|
-
// Wait for more data
|
|
233
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
this.sseClient.close();
|
|
237
|
-
this.sseClient = undefined;
|
|
238
|
-
}
|
|
239
188
|
async waitForQueryCompletion(operationId, options) {
|
|
240
189
|
return new Promise((resolve, reject) => {
|
|
241
190
|
const sseClient = new SSEClient_1.SSEClient(this.config);
|
|
191
|
+
this.sseClient = sseClient;
|
|
242
192
|
sseClient
|
|
243
193
|
.connect(operationId)
|
|
244
194
|
.then(() => {
|
|
@@ -261,16 +211,25 @@ class QueryClient {
|
|
|
261
211
|
graph_id: queryResult.graph_id,
|
|
262
212
|
timestamp: queryResult.timestamp || new Date().toISOString(),
|
|
263
213
|
};
|
|
264
|
-
sseClient.close();
|
|
265
214
|
resolve(result);
|
|
215
|
+
sseClient.close();
|
|
266
216
|
});
|
|
267
217
|
sseClient.on(SSEClient_1.EventType.OPERATION_ERROR, (error) => {
|
|
268
|
-
sseClient.close();
|
|
269
218
|
reject(new Error(error.message || error.error));
|
|
219
|
+
sseClient.close();
|
|
270
220
|
});
|
|
271
221
|
sseClient.on(SSEClient_1.EventType.OPERATION_CANCELLED, () => {
|
|
272
|
-
sseClient.close();
|
|
273
222
|
reject(new Error('Query cancelled'));
|
|
223
|
+
sseClient.close();
|
|
224
|
+
});
|
|
225
|
+
sseClient.on('error', (error) => {
|
|
226
|
+
reject(new Error(error?.error || 'Query stream error'));
|
|
227
|
+
});
|
|
228
|
+
// A close before a terminal event (retries exhausted, a reconnect
|
|
229
|
+
// that failed before opening, close()) would otherwise leave this
|
|
230
|
+
// pending. Settle first in the handlers above: they close too.
|
|
231
|
+
sseClient.on('closed', () => {
|
|
232
|
+
reject(new Error('Query stream closed before the query completed'));
|
|
274
233
|
});
|
|
275
234
|
})
|
|
276
235
|
.catch(reject);
|