-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiBasedTools.ts
More file actions
829 lines (689 loc) · 22.9 KB
/
apiBasedTools.ts
File metadata and controls
829 lines (689 loc) · 22.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
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
import {
AdminForthDataTypes,
logger,
type AdminUser,
type HttpExtra,
type IAdminForth,
type IRegisteredApiSchema,
} from 'adminforth';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
import { inspect } from 'util';
import YAML from 'yaml';
dayjs.extend(utc);
dayjs.extend(timezone);
type CookieItem = {
key: string;
value: string;
};
type ToolOverrideCallParams = Pick<ApiBasedToolCallParams, 'httpExtra' | 'inputs' | 'userTimeZone'>;
type ToolOverrideContext = {
adminforth: IAdminForth;
output?: unknown;
adminUser?: AdminUser;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
resourceLabel?: string;
userTimeZone?: string;
invokeTool: (toolName: string, params?: ToolOverrideCallParams) => Promise<unknown>;
};
type ToolOverride = {
wipe_frontend_specific_data?: readonly string[];
format_tool?: (params: ToolOverrideContext) => Promise<string> | string;
post_process_response?: (params: ToolOverrideContext) => Promise<unknown> | unknown;
};
type GetResourceDataToolResponse = {
data: Array<Record<string, unknown>>;
total?: number;
options?: Record<string, unknown>;
};
type DateTimeColumnType = AdminForthDataTypes.DATETIME | AdminForthDataTypes.TIME;
type InternalApiOriginProvider = {
getInternalApiOrigin?: () => string | undefined;
};
const DEFAULT_USER_TIME_ZONE = 'UTC';
function getInputString(inputs: Record<string, unknown> | undefined, key: string) {
const value = inputs?.[key];
return typeof value === 'string' && value ? value : undefined;
}
function isHiddenResourceCall(
hiddenResourceIds: ReadonlySet<string>,
inputs: Record<string, unknown> | undefined,
) {
const resourceId = getInputString(inputs, 'resourceId');
return resourceId ? hiddenResourceIds.has(resourceId) : false;
}
function getInputArrayLength(inputs: Record<string, unknown> | undefined, key: string) {
const value = inputs?.[key];
return Array.isArray(value) ? value.length : undefined;
}
function resourceLabel(adminforth: IAdminForth, inputs: Record<string, unknown> | undefined) {
const resourceId = getInputString(inputs, 'resourceId');
const resource = adminforth.config.resources.find((res) => res.resourceId === resourceId);
return resource?.label ?? resourceId ?? 'resource';
}
function getDataPrefix(inputs: Record<string, unknown> | undefined) {
const offset = typeof inputs?.offset === 'number' ? inputs.offset : undefined;
const limit = typeof inputs?.limit === 'number' ? inputs.limit : undefined;
if (offset !== undefined && limit !== undefined) {
return `${offset}-${offset + limit} `;
}
return limit === undefined ? '' : `${limit} `;
}
function actionText(inputs: Record<string, unknown> | undefined) {
const actionId = getInputString(inputs, 'actionId');
return actionId ? ` action ${actionId}` : ' action';
}
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',
],
format_tool: ({ resourceLabel }) => `Get ${resourceLabel} resource`,
},
get_resource_data: {
format_tool: ({ inputs, resourceLabel }) => (
`Get ${getDataPrefix(inputs)}${resourceLabel}`
),
post_process_response: async ({ adminforth, output, inputs, userTimeZone }) => {
if (hasToolError(output)) {
return output;
}
const dateTimeColumnNames = getDateTimeColumnNames(adminforth, inputs);
if (dateTimeColumnNames.length === 0) {
return output;
}
if (!hasGetResourceDataRows(output)) {
logger.warn(
`Skipping datetime formatting for get_resource_data because response.data is not an array for resource ${getInputString(inputs, 'resourceId') ?? 'unknown'}`,
);
return output;
}
const localizedTimeZone = userTimeZone ?? DEFAULT_USER_TIME_ZONE;
formatDateTimeColumns(output.data, dateTimeColumnNames, localizedTimeZone);
return output;
},
},
aggregate: {
format_tool: ({ resourceLabel }) => `Aggregate ${resourceLabel}`,
},
start_custom_action: {
format_tool: ({ inputs, resourceLabel }) => `Run ${resourceLabel}${actionText(inputs)}`,
},
start_custom_bulk_action: {
format_tool: ({ inputs, resourceLabel }) => {
const recordCount = getInputArrayLength(inputs, 'recordIds');
const recordsText = recordCount === undefined ? '' : ` for ${recordCount} records`;
return `Run ${resourceLabel}${actionText(inputs)}${recordsText}`;
},
},
start_bulk_action: {
format_tool: ({ inputs, resourceLabel }) => {
const recordCount = getInputArrayLength(inputs, 'recordIds');
const recordsText = recordCount === undefined ? '' : ` for ${recordCount} records`;
return `Run ${resourceLabel}${actionText(inputs)}${recordsText}`;
},
},
create_record: {
format_tool: ({ resourceLabel }) => `Create ${resourceLabel}`,
},
update_record: {
format_tool: ({ resourceLabel }) => `Update ${resourceLabel}`,
},
delete_record: {
format_tool: ({ resourceLabel }) => `Delete ${resourceLabel}`,
},
};
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 hasGetResourceDataRows(output: unknown): output is GetResourceDataToolResponse {
if (typeof output !== 'object' || output === null || !('data' in output)) {
return false;
}
return Array.isArray((output as { data?: unknown }).data);
}
function getDateTimeColumnNames(
adminforth: IAdminForth,
inputs: Record<string, unknown> | undefined,
): string[] {
const resourceId = getInputString(inputs, 'resourceId');
const resource = adminforth.config.resources.find((res) => res.resourceId === resourceId);
if (!resource) {
return [];
}
return resource.dataSourceColumns
.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;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
invokeTool: (toolName: string, params?: ToolOverrideCallParams) => Promise<unknown>;
output: unknown;
toolName: string;
userTimeZone?: string;
}): Promise<unknown> {
const {
adminforth,
adminUser,
httpExtra,
inputs,
invokeTool,
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({
adminforth,
output: sanitizedOutput,
adminUser,
httpExtra,
inputs,
userTimeZone,
invokeTool: async (nestedToolName, nestedParams = {}) => {
const nestedInputs = nestedParams.inputs ?? inputs;
const nestedHttpExtra = nestedParams.httpExtra ?? httpExtra;
const nestedUserTimeZone = nestedParams.userTimeZone ?? userTimeZone;
const nestedOutput = await invokeTool(nestedToolName, {
inputs: nestedInputs,
httpExtra: nestedHttpExtra,
userTimeZone: nestedUserTimeZone,
});
return applyToolOverride({
adminforth,
adminUser,
httpExtra: nestedHttpExtra,
inputs: nestedInputs,
invokeTool,
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 stripAdminApiPrefix(path: string, adminforth: IAdminForth) {
const configuredBaseUrl = adminforth.config.baseUrl || '';
const normalizedBaseUrl = configuredBaseUrl.endsWith('/')
? configuredBaseUrl.slice(0, -1)
: configuredBaseUrl;
const apiPrefix = `${normalizedBaseUrl}/adminapi/v1`;
if (path.startsWith(apiPrefix)) {
const strippedPath = path.slice(apiPrefix.length);
return strippedPath.startsWith('/') ? strippedPath : `/${strippedPath}`;
}
return path;
}
function openApiSchemaPathToToolName(path: string, adminforth: IAdminForth) {
return endpointPathToToolName(stripAdminApiPrefix(path, adminforth));
}
function formatLogNameList(names: string[]) {
return names.length ? names.join(', ') : '(none)';
}
export async function formatApiBasedToolCall(params: {
adminforth: IAdminForth;
adminUser?: AdminUser;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
toolName: string;
userTimeZone?: string;
}) {
const formatTool = TOOL_OVERRIDES[params.toolName]?.format_tool;
return await formatTool?.({
adminforth: params.adminforth,
adminUser: params.adminUser,
httpExtra: params.httpExtra,
inputs: params.inputs,
resourceLabel: resourceLabel(params.adminforth, params.inputs),
userTimeZone: params.userTimeZone,
invokeTool: async () => {
throw new Error('Tool info formatting cannot invoke tools');
},
});
}
function normalizeCookies(
cookies?: Partial<HttpExtra>['cookies'] | Record<string, string>,
): CookieItem[] {
if (!cookies) {
return [];
}
if (Array.isArray(cookies)) {
return cookies;
}
return Object.entries(cookies).map(([key, value]) => ({ key, value }));
}
function normalizeDateTimeInputsToUtc(
body: Record<string, unknown>,
adminforth: IAdminForth,
userTimeZone?: string,
): Record<string, unknown> {
if (!userTimeZone || typeof body.resourceId !== 'string') {
return body;
}
const resource = adminforth.config.resources.find((res) => res.resourceId === body.resourceId);
if (!resource) {
return body;
}
const columnsByName = new Map(resource.dataSourceColumns.map((column) => [column.name, column]));
const normalizeColumnValue = (
value: unknown,
columnType: DateTimeColumnType,
): unknown => {
if (Array.isArray(value)) {
return value.map((item) => normalizeColumnValue(item, columnType));
}
if (typeof value !== 'string' || value === '') {
return value;
}
if (columnType === AdminForthDataTypes.DATETIME) {
return dayjs.tz(value, userTimeZone).utc().toISOString();
}
if (columnType === AdminForthDataTypes.TIME) {
const userDate = dayjs().tz(userTimeZone).format('YYYY-MM-DD');
return dayjs.tz(`${userDate}T${value}`, userTimeZone).utc().format('HH:mm:ss');
}
};
const normalizeValue = (value: unknown, key?: string): unknown => {
const column = key ? columnsByName.get(key) : undefined;
if (column?.type === AdminForthDataTypes.DATETIME || column?.type === AdminForthDataTypes.TIME) {
return normalizeColumnValue(value, column.type);
}
if (Array.isArray(value)) {
return value.map((item) => normalizeValue(item));
}
if (!value || typeof value !== 'object') {
return value;
}
const record = value as Record<string, unknown>;
const filterColumn = typeof record.field === 'string' ? columnsByName.get(record.field) : undefined;
if (
'value' in record &&
(filterColumn?.type === AdminForthDataTypes.DATETIME || filterColumn?.type === AdminForthDataTypes.TIME)
) {
return {
...record,
value: normalizeColumnValue(record.value, filterColumn.type),
};
}
return Object.fromEntries(
Object.entries(record).map(([nestedKey, nestedValue]) => [
nestedKey,
normalizeValue(nestedValue, nestedKey),
]),
);
};
return normalizeValue(body) as Record<string, unknown>;
}
const METHODS_WITHOUT_REQUEST_BODY = new Set(['GET', 'HEAD']);
const HEADERS_NOT_FORWARDED_TO_API_TOOL = new Set([
'connection',
'content-length',
'host',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailer',
'transfer-encoding',
'upgrade',
]);
function isAbsoluteHttpUrl(value: string) {
try {
const url = new URL(value);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch {
return false;
}
}
function resolveOpenApiRequestUrl(params: {
adminforth: IAdminForth;
path: string;
toolName: string;
}) {
const internalApiOrigin = (params.adminforth.express as InternalApiOriginProvider)
.getInternalApiOrigin?.();
if (internalApiOrigin) {
const path = isAbsoluteHttpUrl(params.path)
? `${new URL(params.path).pathname}${new URL(params.path).search}`
: params.path;
return new URL(path, internalApiOrigin).toString();
}
throw new Error(
`Tool "${params.toolName}" cannot call OpenAPI path "${params.path}" because internal API origin is unavailable.`,
);
}
function createToolRequestHeaders(
httpExtra: Partial<HttpExtra> | undefined,
userTimeZone?: string,
) {
const headers: Record<string, string> = {};
for (const [name, value] of Object.entries(httpExtra?.headers ?? {})) {
const headerName = name.toLowerCase();
if (typeof value === 'string' && !HEADERS_NOT_FORWARDED_TO_API_TOOL.has(headerName)) {
headers[headerName] = value;
}
}
headers.accept = 'application/json';
headers['content-type'] = 'application/json';
if (userTimeZone) {
headers['x-timezone'] = userTimeZone;
}
const cookieHeader = normalizeCookies(httpExtra?.cookies)
.map(({ key, value }) => `${key}=${value}`)
.join('; ');
if (cookieHeader && !headers.cookie) {
headers.cookie = cookieHeader;
}
return headers;
}
function appendInputsToQueryString(url: string, inputs: Record<string, unknown>) {
const nextUrl = new URL(url);
for (const [key, value] of Object.entries(inputs)) {
if (value === undefined) {
continue;
}
if (Array.isArray(value)) {
for (const item of value) {
nextUrl.searchParams.append(
key,
typeof item === 'object' && item !== null ? JSON.stringify(item) : String(item),
);
}
continue;
}
nextUrl.searchParams.set(
key,
typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value),
);
}
return nextUrl.toString();
}
async function parseOpenApiToolResponse(response: Response) {
const responseText = await response.text();
const payload = responseText && response.headers.get('content-type')?.includes('application/json')
? JSON.parse(responseText)
: responseText;
if (response.ok) {
return responseText ? payload : { status: response.status };
}
return {
error: 'HTTP_ERROR',
status: response.status,
statusText: response.statusText,
response: payload,
};
}
async function callOpenApiSchema(params: {
adminforth: IAdminForth;
httpExtra?: Partial<HttpExtra>;
inputs?: Record<string, unknown>;
schema: IRegisteredApiSchema;
toolName: string;
userTimeZone?: string;
}) {
const { adminforth, httpExtra, inputs, schema, toolName, userTimeZone } = params;
const method = schema.method.toUpperCase();
const body = normalizeDateTimeInputsToUtc(
(inputs ?? httpExtra?.body ?? {}) as Record<string, unknown>,
adminforth,
userTimeZone,
);
const requestUrl = resolveOpenApiRequestUrl({
adminforth,
path: schema.path,
toolName,
});
const hasRequestBody = !METHODS_WITHOUT_REQUEST_BODY.has(method);
logger.info(`Calling OpenAPI tool "${toolName}" with method ${method} at URL ${requestUrl}`);
const response = await fetch(hasRequestBody ? requestUrl : appendInputsToQueryString(requestUrl, body), {
method,
headers: createToolRequestHeaders(httpExtra, userTimeZone),
body: hasRequestBody ? JSON.stringify(body) : undefined,
});
logger.info(`Received response with status ${response.status} from OpenAPI tool "${toolName}"`);
return parseOpenApiToolResponse(response);
}
export function prepareApiBasedTools(
adminforth: IAdminForth,
hiddenResourceIds: Iterable<string> = [],
): Record<string, ApiBasedTool> {
const apiBasedTools: Record<string, ApiBasedTool> = {};
const openApiSchemas = adminforth.openApi.registeredSchemas.filter(
(schema) => schema.request_schema || schema.response_schema,
);
const openApiSchemasByToolName = new Map<string, IRegisteredApiSchema>();
const hiddenResourceIdSet = new Set(hiddenResourceIds);
for (const schema of openApiSchemas) {
const toolName = openApiSchemaPathToToolName(schema.path, adminforth);
openApiSchemasByToolName.set(toolName, schema);
}
logger.info(
`AdminForth Agent OpenAPI APIs: ${formatLogNameList(
adminforth.openApi.registeredSchemas.map((schema) => openApiSchemaPathToToolName(schema.path, adminforth)),
)}`,
);
logger.info(
`AdminForth Agent OpenAPI tools connected: ${formatLogNameList([...openApiSchemasByToolName.keys()])}`,
);
for (const [toolName, schema] of openApiSchemasByToolName.entries()) {
apiBasedTools[toolName] = {
description: schema.description,
input_schema: schema.request_schema,
input_schma: schema.request_schema,
output_schema: schema.response_schema,
call: async ({ adminUser, adminuser, inputs, httpExtra, userTimeZone } = {}) => {
if (isHiddenResourceCall(hiddenResourceIdSet, inputs)) {
return YAML.stringify({
error: 'RESOURCE_NOT_AVAILABLE',
message: 'This resource is not available to the agent.',
});
}
const invokeTool = async (
nextToolName: string,
nextParams: ToolOverrideCallParams = {},
) => {
const nextSchema = openApiSchemasByToolName.get(nextToolName);
if (!nextSchema) {
throw new Error(`Tool ${nextToolName} is not registered in OpenAPI`);
}
return callOpenApiSchema({
adminforth,
schema: nextSchema,
toolName: nextToolName,
inputs: nextParams.inputs,
httpExtra: nextParams.httpExtra,
userTimeZone: nextParams.userTimeZone,
});
};
const output = await invokeTool(toolName, {
inputs,
httpExtra,
userTimeZone,
});
const processedOutput = await applyToolOverride({
adminforth,
adminUser: adminUser ?? adminuser,
httpExtra,
inputs,
invokeTool,
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]',
};
}