-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiBasedTools.ts
More file actions
625 lines (536 loc) · 15.8 KB
/
apiBasedTools.ts
File metadata and controls
625 lines (536 loc) · 15.8 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import {
AdminForthDataTypes,
type AdminUser,
type HttpExtra,
type IAdminForth,
type IAdminForthHttpResponse,
type IHttpServer,
} from 'adminforth';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
import { PassThrough } from 'stream';
import { inspect } from 'util';
import YAML from 'yaml';
dayjs.extend(utc);
dayjs.extend(timezone);
type CookieItem = {
key: string;
value: string;
};
type CapturedEndpointHandlerInput = {
body: Record<string, unknown>;
adminUser?: AdminUser;
query: Record<string, string>;
headers: Record<string, any>;
cookies: CookieItem[];
response: IAdminForthHttpResponse;
requestUrl: string;
abortSignal: AbortSignal;
_raw_express_req: any;
_raw_express_res: any;
tr: (
msg: string,
category: string,
params: any,
pluralizationNumber?: number,
) => Promise<string>;
};
type EndpointWithSchemas = {
method: string;
noAuth?: boolean;
path: string;
description?: string;
request_schema?: unknown;
response_schema?: unknown;
responce_schema?: unknown;
handler: (input: CapturedEndpointHandlerInput) => Promise<any> | any;
};
type CapturedEndpoint = EndpointWithSchemas & {
normalizedResponseSchema?: unknown;
};
type ToolHttpResponse = IAdminForthHttpResponse & {
headers: Array<[string, string]>;
jsonPayload?: unknown;
status: number;
message?: string;
};
type ToolOverrideCallParams = Pick<ApiBasedToolCallParams, 'httpExtra' | 'inputs' | 'userTimeZone'>;
type ToolOverrideContext = {
output: unknown;
adminUser?: AdminUser;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
userTimeZone?: string;
invokeTool: (toolName: string, params?: ToolOverrideCallParams) => Promise<unknown>;
};
type ToolOverride = {
wipe_frontend_specific_data?: readonly string[];
post_process_response?: (params: ToolOverrideContext) => Promise<unknown> | unknown;
};
type GetResourceToolResponse = {
resource: {
columns: Array<{
name: string;
type?: string;
}>;
};
};
type GetResourceDataToolResponse = {
data: Array<Record<string, unknown>>;
total?: number;
options?: Record<string, unknown>;
};
const DEFAULT_USER_TIME_ZONE = 'UTC';
const TOOL_OVERRIDES: Record<string, ToolOverride> = {
get_resource: {
wipe_frontend_specific_data: [
'resource.columns[].filterOptions',
'resource.columns[].components',
'resource.options.actions[].customComponent',
'resource.options.pageInjections',
],
},
get_resource_data: {
post_process_response: async ({ output, inputs, invokeTool, userTimeZone }) => {
if (hasToolError(output)) {
return output;
}
const resourceId = inputs?.resourceId as string;
const getResourceOutput = await invokeTool('get_resource', {
inputs: { resourceId },
});
const dateTimeColumnNames = getDateTimeColumnNames(getResourceOutput);
if (dateTimeColumnNames.length === 0) {
return output;
}
const localizedTimeZone = userTimeZone ?? DEFAULT_USER_TIME_ZONE;
const response = output as GetResourceDataToolResponse;
formatDateTimeColumns(response.data, dateTimeColumnNames, localizedTimeZone);
return response;
},
},
};
export type ApiBasedToolCallParams = {
adminUser?: AdminUser;
adminuser?: AdminUser;
inputs?: Record<string, unknown>;
httpExtra?: Partial<HttpExtra>;
userTimeZone?: string;
};
export type ApiBasedTool = {
description?: string;
input_schema?: unknown;
input_schma?: unknown;
output_schema?: unknown;
call: (params?: ApiBasedToolCallParams) => Promise<string>;
};
function sanitizeForYaml(
value: unknown,
): unknown {
const traversalStack: object[] = [];
const serialized = JSON.stringify(value, function (this: unknown, _key: string, nestedValue: unknown) {
if (typeof nestedValue === 'function' || typeof nestedValue === 'symbol' || nestedValue === undefined) {
return undefined;
}
if (typeof nestedValue === 'bigint') {
return nestedValue.toString();
}
if (typeof nestedValue !== 'object' || nestedValue === null) {
return nestedValue;
}
if (nestedValue instanceof Map) {
return Object.fromEntries(nestedValue);
}
if (nestedValue instanceof Set) {
return Array.from(nestedValue.values());
}
while (traversalStack.length > 0 && traversalStack[traversalStack.length - 1] !== this) {
traversalStack.pop();
}
if (traversalStack.includes(nestedValue)) {
return undefined;
}
traversalStack.push(nestedValue);
return nestedValue;
});
if (serialized === undefined) {
return null;
}
return JSON.parse(serialized);
}
export function serializeUnknownError(error: unknown): Record<string, unknown> {
if (error instanceof Error) {
const errorWithCause = error as Error & { cause?: unknown };
const errorRecord = error as unknown as Record<string, unknown>;
const serialized: Record<string, unknown> = {
name: error.name,
message: error.message,
stack: error.stack,
};
if (errorWithCause.cause !== undefined) {
serialized.cause = serializeUnknownError(errorWithCause.cause);
}
for (const key of Object.getOwnPropertyNames(error)) {
if (key in serialized) {
continue;
}
serialized[key] = errorRecord[key];
}
return serialized;
}
if (typeof error === 'object' && error !== null) {
return {
type: error.constructor?.name ?? 'Object',
inspected: inspect(error, { depth: 6, breakLength: 120 }),
};
}
return {
type: typeof error,
value: error,
};
}
function wipePath(target: unknown, pathParts: string[]): void {
if (!target || typeof target !== 'object' || pathParts.length === 0) {
return;
}
const [currentPart, ...rest] = pathParts;
const isArrayTraversal = currentPart.endsWith('[]');
const key = isArrayTraversal ? currentPart.slice(0, -2) : currentPart;
const targetRecord = target as Record<string, unknown>;
if (!(key in targetRecord)) {
return;
}
if (rest.length === 0) {
delete targetRecord[key];
return;
}
const nextValue = targetRecord[key];
if (isArrayTraversal) {
if (!Array.isArray(nextValue)) {
return;
}
for (const item of nextValue) {
wipePath(item, rest);
}
return;
}
wipePath(nextValue, rest);
}
function hasToolError(output: unknown): output is { error: unknown } {
return typeof output === 'object' && output !== null && 'error' in output;
}
function getDateTimeColumnNames(output: unknown): string[] {
const resource = (output as GetResourceToolResponse).resource;
return resource.columns
.filter((column) => column.type === AdminForthDataTypes.DATETIME)
.map((column) => column.name);
}
function formatGmtOffset(offsetMinutes: number): string {
const sign = offsetMinutes >= 0 ? '+' : '-';
const absoluteOffsetMinutes = Math.abs(offsetMinutes);
const hours = Math.floor(absoluteOffsetMinutes / 60);
const minutes = absoluteOffsetMinutes % 60;
if (minutes === 0) {
return `GMT${sign}${hours}`;
}
return `GMT${sign}${hours}:${String(minutes).padStart(2, '0')}`;
}
function formatDateTimeValue(value: string, userTimeZone: string): string {
const localizedValue = dayjs.utc(value).tz(userTimeZone);
return `${localizedValue.format('DD MMM YYYY, HH:mm:ss.SSS')} (${formatGmtOffset(localizedValue.utcOffset())})`;
}
function formatDateTimeColumns(
rows: Array<Record<string, unknown>>,
dateTimeColumnNames: string[],
userTimeZone: string,
): void {
for (const row of rows) {
for (const columnName of dateTimeColumnNames) {
const value = row[columnName];
if (typeof value === 'string' && value) {
row[columnName] = formatDateTimeValue(value, userTimeZone);
}
}
}
}
async function applyToolOverride(params: {
adminforth: IAdminForth;
adminUser?: AdminUser;
capturedEndpointsByToolName: Record<string, CapturedEndpoint>;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
output: unknown;
toolName: string;
userTimeZone?: string;
}): Promise<unknown> {
const {
adminforth,
adminUser,
capturedEndpointsByToolName,
httpExtra,
inputs,
output,
toolName,
userTimeZone,
} = params;
const sanitizedOutput = sanitizeForYaml(output);
const override = TOOL_OVERRIDES[toolName];
if (!override) {
return sanitizedOutput;
}
for (const path of override.wipe_frontend_specific_data ?? []) {
wipePath(sanitizedOutput, path.split('.'));
}
if (!override.post_process_response) {
return sanitizedOutput;
}
const postProcessedOutput = await override.post_process_response({
output: sanitizedOutput,
adminUser,
httpExtra,
inputs,
userTimeZone,
invokeTool: async (nestedToolName, nestedParams = {}) => {
const nestedEndpoint = capturedEndpointsByToolName[nestedToolName];
if (!nestedEndpoint) {
throw new Error(`Tool ${nestedToolName} is not registered`);
}
const nestedInputs = nestedParams.inputs ?? inputs;
const nestedHttpExtra = nestedParams.httpExtra ?? httpExtra;
const nestedUserTimeZone = nestedParams.userTimeZone ?? userTimeZone;
const nestedOutput = await callCapturedEndpoint({
adminforth,
endpoint: nestedEndpoint,
adminUser,
inputs: nestedInputs,
httpExtra: nestedHttpExtra,
});
return applyToolOverride({
adminforth,
adminUser,
capturedEndpointsByToolName,
httpExtra: nestedHttpExtra,
inputs: nestedInputs,
output: nestedOutput,
toolName: nestedToolName,
userTimeZone: nestedUserTimeZone,
});
},
});
return sanitizeForYaml(postProcessedOutput);
}
function endpointPathToToolName(path: string) {
return path
.replace(/^\/+/, '')
.replace(/[^a-zA-Z0-9_]+/g, '_')
.replace(/^_+|_+$/g, '');
}
function normalizeCookies(cookies?: Partial<HttpExtra>['cookies']): CookieItem[] {
if (!cookies) {
return [];
}
if (Array.isArray(cookies)) {
return cookies;
}
return Object.entries(cookies).map(([key, value]) => ({ key, value }));
}
function createToolResponse(baseResponse?: IAdminForthHttpResponse): ToolHttpResponse {
return {
headers: [],
status: 200,
message: undefined,
setHeader(name, value) {
this.headers.push([name, value]);
baseResponse?.setHeader(name, value);
},
setStatus(code, message) {
this.status = code;
this.message = message;
baseResponse?.setStatus(code, message);
},
blobStream() {
return baseResponse?.blobStream() ?? new PassThrough();
},
};
}
function createRawExpressRequest(params: {
adminUser?: AdminUser;
body: Record<string, unknown>;
cookies: CookieItem[];
headers: Record<string, any>;
method: string;
query: Record<string, string>;
requestUrl: string;
}) {
const cookieHeader = params.cookies
.map(({ key, value }) => `${key}=${value}`)
.join('; ');
return {
adminUser: params.adminUser,
body: params.body,
destroyed: false,
headers: {
...params.headers,
...(cookieHeader ? { cookie: cookieHeader } : {}),
},
method: params.method.toUpperCase(),
on: () => undefined,
query: params.query,
url: params.requestUrl,
};
}
function createRawExpressResponse(response: ToolHttpResponse) {
const rawResponse = {
destroyed: false,
on: () => undefined,
setHeader(name: string, value: string) {
response.setHeader(name, value);
return rawResponse;
},
status(code: number) {
response.status = code;
return rawResponse;
},
send(message: string) {
response.message = message;
return rawResponse;
},
json(payload: unknown) {
response.jsonPayload = payload;
response.message = JSON.stringify(payload);
return rawResponse;
},
write: () => true,
writeHead: () => rawResponse,
writableEnded: false,
end: () => rawResponse,
};
return rawResponse;
}
async function callCapturedEndpoint(params: {
adminforth: IAdminForth;
adminUser?: AdminUser;
endpoint: CapturedEndpoint;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
}) {
const { adminforth, adminUser, endpoint, httpExtra, inputs } = params;
const response = createToolResponse(httpExtra?.response);
const headers = {
'content-type': 'application/json',
...(httpExtra?.headers ?? {}),
};
const body = (inputs ?? httpExtra?.body ?? {}) as Record<string, unknown>;
const query = httpExtra?.query ?? {};
const cookies = normalizeCookies(httpExtra?.cookies);
const requestUrl = httpExtra?.requestUrl ?? `${adminforth.config.baseUrl}/adminapi/v1${endpoint.path}`;
const abortController = new AbortController();
const rawRequest = createRawExpressRequest({
adminUser,
body,
cookies,
headers,
method: endpoint.method,
query,
requestUrl,
});
const rawResponse = createRawExpressResponse(response);
const acceptLanguage = headers['accept-language'];
const tr = (
msg: string,
category: string = 'default',
translationParams: any,
pluralizationNumber?: number,
) => adminforth.tr(msg, category, acceptLanguage, translationParams, pluralizationNumber);
const output = await endpoint.handler({
body,
adminUser,
query,
headers,
cookies,
response,
requestUrl,
abortSignal: abortController.signal,
_raw_express_req: rawRequest,
_raw_express_res: rawResponse,
tr,
});
if (output !== undefined && output !== null) {
return output;
}
if (response.jsonPayload !== undefined) {
return response.jsonPayload;
}
if (response.message !== undefined) {
return response.message;
}
return {
headers: response.headers,
status: response.status,
};
}
export function prepareApiBasedTools(adminforth: IAdminForth): Record<string, ApiBasedTool> {
const capturedEndpoints: CapturedEndpoint[] = [];
const captureServer: IHttpServer = {
setupSpaServer() {},
endpoint: ((options: EndpointWithSchemas) => {
const normalizedResponseSchema = options.response_schema ?? options.responce_schema;
if (!options.request_schema && !normalizedResponseSchema) {
return;
}
capturedEndpoints.push({
...options,
response_schema: normalizedResponseSchema,
normalizedResponseSchema,
});
}) as IHttpServer['endpoint'],
};
adminforth.setupEndpoints(captureServer);
const apiBasedTools: Record<string, ApiBasedTool> = {};
const capturedEndpointsByToolName = Object.fromEntries(
capturedEndpoints.map((endpoint) => [endpointPathToToolName(endpoint.path), endpoint]),
);
for (const endpoint of capturedEndpoints) {
const toolName = endpointPathToToolName(endpoint.path);
apiBasedTools[toolName] = {
description: endpoint.description,
input_schema: endpoint.request_schema,
input_schma: endpoint.request_schema,
output_schema: endpoint.normalizedResponseSchema,
call: async ({ adminUser, adminuser, inputs, httpExtra, userTimeZone } = {}) => {
const output = await callCapturedEndpoint({
adminforth,
endpoint,
adminUser: adminUser ?? adminuser,
inputs,
httpExtra,
});
const processedOutput = await applyToolOverride({
adminforth,
adminUser: adminUser ?? adminuser,
capturedEndpointsByToolName,
httpExtra,
inputs,
output,
toolName,
userTimeZone,
});
return YAML.stringify(processedOutput);
},
};
}
return apiBasedTools;
}
export function serializeApiBasedTool(tool: ApiBasedTool | undefined) {
if (!tool) {
return null;
}
return {
description: tool.description,
input_schema: tool.input_schema,
input_schma: tool.input_schma,
output_schema: tool.output_schema,
call: '[Function]',
};
}