-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase-client.ts
More file actions
436 lines (395 loc) · 11.9 KB
/
base-client.ts
File metadata and controls
436 lines (395 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import { Readable } from 'stream';
import createClient, { Client } from 'openapi-fetch';
import { decode } from 'jsonwebtoken';
import type { paths } from '../types/api.types';
import { Routes } from '../types/routes.types';
import { getSdkIdentifier } from '../utils/version';
import {
objectToCamel,
objectToSnake,
ObjectToSnake,
ObjectToCamel
} from 'ts-case-convert';
import {
HTTPValidationError,
GalileoAPIError,
isGalileoAPIStandardErrorData
} from '../types/errors.types';
// Type guards for snake_case and camelCase conversion
type ValidatedSnakeCase<T extends object, TTarget> =
ObjectToSnake<T> extends TTarget ? TTarget : never;
type ValidatedCamelCase<T extends object, TTarget> =
ObjectToCamel<T> extends TTarget ? TTarget : never;
export enum RequestMethod {
GET = 'GET',
PATCH = 'PATCH',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE'
}
export const GENERIC_ERROR_MESSAGE =
'This error has been automatically tracked. Please try again.';
export class BaseClient {
protected apiUrl: string = '';
protected token: string = '';
protected client: Client<paths> | undefined = undefined;
/**
* Make an HTTP request to the Galileo API and return the raw Axios response.
*/
public async makeRequestRaw<T>(
request_method: Method,
endpoint: Routes,
data?: string | Record<string, any> | null,
params?: Record<string, unknown>,
extraHeaders?: Record<string, string>
): Promise<AxiosResponse<T>> {
await this.refreshTokenIfNeeded(endpoint);
let headers: Record<string, any> = {
'X-Galileo-SDK': getSdkIdentifier()
};
if (this.token) {
headers = { ...this.getAuthHeader(this.token), ...headers };
}
headers = { ...headers, ...extraHeaders };
const endpointPath = `${this.apiUrl}/${endpoint
.replace(
'{project_id}',
params && 'project_id' in params ? (params.project_id as string) : ''
)
.replace(
'{log_stream_id}',
params && 'log_stream_id' in params
? (params.log_stream_id as string)
: ''
)
.replace(
'{run_id}',
params && 'run_id' in params ? (params.run_id as string) : ''
)
.replace(
'{job_id}',
params && 'job_id' in params ? (params.job_id as string) : ''
)
.replace(
'{dataset_id}',
params && 'dataset_id' in params ? (params.dataset_id as string) : ''
)
.replace(
'{experiment_id}',
params && 'experiment_id' in params
? (params.experiment_id as string)
: ''
)
.replace(
'{template_id}',
params && 'template_id' in params ? (params.template_id as string) : ''
)
.replace(
'{version}',
params && 'version' in params ? (params.version as string) : ''
)
.replace(
'{trace_id}',
params && 'trace_id' in params ? (params.trace_id as string) : ''
)
.replace(
'{session_id}',
params && 'session_id' in params ? (params.session_id as string) : ''
)
.replace(
'{span_id}',
params && 'span_id' in params ? (params.span_id as string) : ''
)
.replace(
'{user_id}',
params && 'user_id' in params ? (params.user_id as string) : ''
)
.replace(
'{scorer_id}',
params && 'scorer_id' in params ? (params.scorer_id as string) : ''
)
.replace(
'{group_id}',
params && 'group_id' in params ? (params.group_id as string) : ''
)
.replace(
'{tag_id}',
params && 'tag_id' in params ? (params.tag_id as string) : ''
)
.replace(
'{version_index}',
params && 'version_index' in params
? (params.version_index as string)
: ''
)}`;
const config: AxiosRequestConfig = {
method: request_method,
url: endpointPath,
params,
headers,
data
};
const response = await axios.request<T>(config);
return response;
}
public async makeRequestWithConversion<
SourceCamelType extends object,
SourceSnakeType extends object,
ResponseSnakeType extends object,
ResponseCamelType extends object
>(
request_method: Method,
endpoint: Routes,
data: SourceCamelType,
params?: Record<string, unknown>
) {
const request = this.convertToSnakeCase<SourceCamelType, SourceSnakeType>(
data
);
const response = await this.makeRequest<ResponseSnakeType>(
request_method,
endpoint,
request,
params
);
return this.convertToCamelCase<ResponseSnakeType, ResponseCamelType>(
response
);
}
public async makeRequest<T>(
request_method: Method,
endpoint: Routes,
data?: string | Record<string, any> | null,
params?: Record<string, unknown>,
extraHeaders?: Record<string, string>
): Promise<T> {
try {
const response = await this.makeRequestRaw<T>(
request_method,
endpoint,
data,
params,
extraHeaders
);
this.validateAxiosResponse(response);
return response.data;
} catch (error) {
this.validateError(error);
}
}
public async makeStreamingRequest(
request_method: Method,
endpoint: Routes,
data?: string | Record<string, any> | null,
params?: Record<string, unknown>,
extraHeaders?: Record<string, string>
): Promise<Readable> {
await this.refreshTokenIfNeeded(endpoint);
let headers: Record<string, any> = {
'X-Galileo-SDK': getSdkIdentifier()
};
if (this.token) {
headers = { ...this.getAuthHeader(this.token), ...headers };
}
headers = { ...headers, ...extraHeaders };
const endpointPath = `${this.apiUrl}/${endpoint
.replace(
'{project_id}',
params && 'project_id' in params ? (params.project_id as string) : ''
)
.replace(
'{log_stream_id}',
params && 'log_stream_id' in params
? (params.log_stream_id as string)
: ''
)
.replace(
'{run_id}',
params && 'run_id' in params ? (params.run_id as string) : ''
)
.replace(
'{dataset_id}',
params && 'dataset_id' in params ? (params.dataset_id as string) : ''
)
.replace(
'{experiment_id}',
params && 'experiment_id' in params
? (params.experiment_id as string)
: ''
)
.replace(
'{template_id}',
params && 'template_id' in params ? (params.template_id as string) : ''
)
.replace(
'{version}',
params && 'version' in params ? (params.version as string) : ''
)
.replace(
'{user_id}',
params && 'user_id' in params ? (params.user_id as string) : ''
)
.replace(
'{group_id}',
params && 'group_id' in params ? (params.group_id as string) : ''
)}`;
try {
const response = await axios.request<Readable>({
method: request_method,
url: endpointPath,
params,
headers,
data,
responseType: 'stream'
});
this.validateAxiosResponse(response);
return response.data;
} catch (error) {
this.validateError(error);
}
}
public convertToSnakeCase<T extends object, TTarget>(
obj: T
): ValidatedSnakeCase<T, TTarget> {
return objectToSnake<T>(obj) as ValidatedSnakeCase<T, TTarget>;
}
public convertToCamelCase<T extends object, TTarget>(
obj: T
): ValidatedCamelCase<T, TTarget> {
return objectToCamel<T>(obj) as ValidatedCamelCase<T, TTarget>;
}
protected initializeClient(): void {
if (this.apiUrl && this.token) {
this.client = createClient({
baseUrl: this.apiUrl,
headers: {
Authorization: `Bearer ${this.token}`,
'X-Galileo-SDK': getSdkIdentifier()
}
});
}
}
protected getApiUrl(projectType: string): string {
let consoleUrl = process.env.GALILEO_CONSOLE_URL;
if (!consoleUrl && projectType === 'gen_ai') {
return 'https://api.galileo.ai';
}
if (!consoleUrl) {
throw new Error('❗ GALILEO_CONSOLE_URL must be set');
}
if (consoleUrl.includes('localhost') || consoleUrl.includes('127.0.0.1')) {
return 'http://localhost:8088';
}
consoleUrl = consoleUrl
.replace('app.galileo.ai', 'api.galileo.ai')
.replace('console', 'api');
// remove trailing slash
if (consoleUrl.endsWith('/')) {
consoleUrl = consoleUrl.slice(0, -1);
}
return consoleUrl;
}
protected async healthCheck(): Promise<boolean> {
return await this.makeRequest<boolean>(
RequestMethod.GET,
Routes.healthCheck
);
}
protected getAuthHeader(token: string): { Authorization: string } {
return { Authorization: `Bearer ${token}` };
}
protected validateAxiosResponse(response: AxiosResponse): void {
if (response.status >= 300) {
this.generateApiError(response.data, response.status);
}
}
protected async refreshTokenIfNeeded(endpoint: Routes): Promise<void> {
if (
![
Routes.login,
Routes.apiKeyLogin,
Routes.socialLogin,
Routes.refreshToken
].includes(endpoint) &&
this.token
) {
const payload = decode(this.token, { json: true });
if (payload?.exp && payload.exp < Math.floor(Date.now() / 1000)) {
throw new Error(
'Token expired - refreshToken not implemented in base class'
);
}
}
}
protected isHTTPValidationError(
error: unknown
): error is HTTPValidationError {
return typeof error === 'object' && error !== null && 'detail' in error;
}
protected extractErrorDetail(error: unknown): string {
if (this.isHTTPValidationError(error)) {
const httpError = error as HTTPValidationError;
if (typeof httpError.detail === 'string') {
return httpError.detail;
}
// Handle array of validation errors
if (Array.isArray(httpError.detail)) {
return httpError.detail
.map((err) => {
const loc = err.loc ? err.loc.join('.') : 'unknown';
const msg = err.msg || 'validation error';
return `${loc}: ${msg}`;
})
.join('; ');
}
return JSON.stringify(httpError.detail);
}
return error instanceof Error ? error.message : String(error);
}
private validateError(error: unknown): never {
if (axios.isAxiosError(error)) {
if (error.response) {
this.validateAxiosResponse(error.response);
}
const errorMessage = error.message || GENERIC_ERROR_MESSAGE;
throw new Error(`Request failed: ${errorMessage}`);
}
throw error;
}
private generateApiError(error: unknown, statusCode?: number): never {
if (error && typeof error === 'object') {
if ('standard_error' in error) {
if (isGalileoAPIStandardErrorData(error.standard_error)) {
throw new GalileoAPIError(error.standard_error);
} else {
throw new Error(
`❗ Something didn't go quite right. The API returned an error, but the details could not be parsed.`
);
}
} else if ('detail' in error) {
const errorMessage =
typeof error.detail === 'string'
? error.detail
: Array.isArray(error.detail) &&
typeof error.detail[0]?.msg === 'string'
? error.detail?.[0]?.msg
: GENERIC_ERROR_MESSAGE;
if (statusCode) {
throw new Error(
`❗ Something didn't go quite right. The API returned a non-ok status code ${statusCode} with output: ${errorMessage}`
);
} else {
throw new Error(
`❗ Something didn't go quite right. ${errorMessage}`
);
}
} else {
throw new Error(GENERIC_ERROR_MESSAGE);
}
} else {
throw new Error(GENERIC_ERROR_MESSAGE);
}
}
}