@adminforth/agent 3.1.10 → 3.2.0
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/README.md +71 -38
- package/dist/application/ports.d.ts +3 -1
- package/dist/application/runTurnUseCase.d.ts +3 -3
- package/dist/application/runTurnUseCase.js +8 -6
- package/dist/custom/{skills/charts/SKILL.md → charts.md} +2 -4
- package/dist/domain/systemPrompt.d.ts +3 -2
- package/dist/domain/systemPrompt.js +14 -53
- package/dist/index.d.ts +0 -3
- package/dist/index.js +24 -35
- package/dist/llm/agentRuntime.d.ts +9 -8
- package/dist/llm/agentRuntime.js +134 -106
- package/dist/llm/langGraphLlm.d.ts +3 -0
- package/dist/llm/langGraphLlm.js +10 -2
- package/dist/llm/middleware/mcpToolsMiddleware.d.ts +7 -0
- package/dist/llm/middleware/{apiToolsMiddleware.js → mcpToolsMiddleware.js} +16 -36
- package/dist/tools/agentToolProvider.d.ts +16 -5
- package/dist/tools/agentToolProvider.js +26 -10
- package/dist/tools/fetchToolSchema.d.ts +7 -3
- package/dist/tools/fetchToolSchema.js +29 -22
- package/dist/tools/mcpConnection.d.ts +38 -0
- package/dist/tools/mcpConnection.js +42 -0
- package/dist/tools/mcpTool.d.ts +3 -0
- package/dist/tools/{apiTool.js → mcpTool.js} +7 -23
- package/dist/tools/toolLabels.d.ts +5 -0
- package/dist/tools/toolLabels.js +49 -0
- package/dist/transport/http/context.d.ts +2 -2
- package/dist/transport/http/sessionEndpoints.js +1 -1
- package/package.json +6 -4
- package/dist/custom/skills/analyze_data/SKILL.md +0 -25
- package/dist/custom/skills/fetch_data/SKILL.md +0 -31
- package/dist/custom/skills/mutate_data/SKILL.md +0 -121
- package/dist/llm/middleware/apiToolsMiddleware.d.ts +0 -3
- package/dist/tools/apiBasedTools.d.ts +0 -30
- package/dist/tools/apiBasedTools.js +0 -488
- package/dist/tools/apiTool.d.ts +0 -6
- package/dist/tools/fetchSkill.d.ts +0 -8
- package/dist/tools/fetchSkill.js +0 -51
- package/dist/tools/index.d.ts +0 -4
- package/dist/tools/index.js +0 -32
- package/dist/tools/skills/registry.d.ts +0 -11
- package/dist/tools/skills/registry.js +0 -107
|
@@ -1,488 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { AdminForthDataTypes, logger, } from 'adminforth';
|
|
11
|
-
import dayjs from 'dayjs';
|
|
12
|
-
import timezone from 'dayjs/plugin/timezone.js';
|
|
13
|
-
import utc from 'dayjs/plugin/utc.js';
|
|
14
|
-
import YAML from 'yaml';
|
|
15
|
-
dayjs.extend(utc);
|
|
16
|
-
dayjs.extend(timezone);
|
|
17
|
-
const DEFAULT_USER_TIME_ZONE = 'UTC';
|
|
18
|
-
function hasRegisteredApiToolHandler(schema) {
|
|
19
|
-
return typeof schema.handler === 'function';
|
|
20
|
-
}
|
|
21
|
-
function getInputString(inputs, key) {
|
|
22
|
-
const value = inputs === null || inputs === void 0 ? void 0 : inputs[key];
|
|
23
|
-
return typeof value === 'string' && value ? value : undefined;
|
|
24
|
-
}
|
|
25
|
-
function isHiddenResourceCall(hiddenResourceIds, inputs) {
|
|
26
|
-
const resourceId = getInputString(inputs, 'resourceId');
|
|
27
|
-
return resourceId ? hiddenResourceIds.has(resourceId) : false;
|
|
28
|
-
}
|
|
29
|
-
function getInputArrayLength(inputs, key) {
|
|
30
|
-
const value = inputs === null || inputs === void 0 ? void 0 : inputs[key];
|
|
31
|
-
return Array.isArray(value) ? value.length : undefined;
|
|
32
|
-
}
|
|
33
|
-
function resourceLabel(adminforth, inputs) {
|
|
34
|
-
var _a, _b;
|
|
35
|
-
const resourceId = getInputString(inputs, 'resourceId');
|
|
36
|
-
const resource = adminforth.config.resources.find((res) => res.resourceId === resourceId);
|
|
37
|
-
return (_b = (_a = resource === null || resource === void 0 ? void 0 : resource.label) !== null && _a !== void 0 ? _a : resourceId) !== null && _b !== void 0 ? _b : 'resource';
|
|
38
|
-
}
|
|
39
|
-
function getDataPrefix(inputs) {
|
|
40
|
-
const offset = typeof (inputs === null || inputs === void 0 ? void 0 : inputs.offset) === 'number' ? inputs.offset : undefined;
|
|
41
|
-
const limit = typeof (inputs === null || inputs === void 0 ? void 0 : inputs.limit) === 'number' ? inputs.limit : undefined;
|
|
42
|
-
if (offset !== undefined && limit !== undefined) {
|
|
43
|
-
return `${offset}-${offset + limit} `;
|
|
44
|
-
}
|
|
45
|
-
return limit === undefined ? '' : `${limit} `;
|
|
46
|
-
}
|
|
47
|
-
function actionText(inputs) {
|
|
48
|
-
const actionId = getInputString(inputs, 'actionId');
|
|
49
|
-
return actionId ? ` action ${actionId}` : ' action';
|
|
50
|
-
}
|
|
51
|
-
const TOOL_OVERRIDES = {
|
|
52
|
-
get_resource: {
|
|
53
|
-
wipe_frontend_specific_data: [
|
|
54
|
-
'resource.columns[].filterOptions',
|
|
55
|
-
'resource.columns[].components',
|
|
56
|
-
'resource.options.actions[].customComponent',
|
|
57
|
-
'resource.options.pageInjections',
|
|
58
|
-
],
|
|
59
|
-
format_tool: ({ resourceLabel }) => `Get ${resourceLabel} resource`,
|
|
60
|
-
},
|
|
61
|
-
get_resource_data: {
|
|
62
|
-
format_tool: ({ inputs, resourceLabel }) => (`Get ${getDataPrefix(inputs)}${resourceLabel}`),
|
|
63
|
-
post_process_response: (_a) => __awaiter(void 0, [_a], void 0, function* ({ adminforth, output, inputs, userTimeZone }) {
|
|
64
|
-
var _b;
|
|
65
|
-
if (hasToolError(output)) {
|
|
66
|
-
return output;
|
|
67
|
-
}
|
|
68
|
-
const dateTimeColumnNames = getDateTimeColumnNames(adminforth, inputs);
|
|
69
|
-
if (dateTimeColumnNames.length === 0) {
|
|
70
|
-
return output;
|
|
71
|
-
}
|
|
72
|
-
if (!hasGetResourceDataRows(output)) {
|
|
73
|
-
logger.warn(`Skipping datetime formatting for get_resource_data because response.data is not an array for resource ${(_b = getInputString(inputs, 'resourceId')) !== null && _b !== void 0 ? _b : 'unknown'}`);
|
|
74
|
-
return output;
|
|
75
|
-
}
|
|
76
|
-
const localizedTimeZone = userTimeZone !== null && userTimeZone !== void 0 ? userTimeZone : DEFAULT_USER_TIME_ZONE;
|
|
77
|
-
formatDateTimeColumns(output.data, dateTimeColumnNames, localizedTimeZone);
|
|
78
|
-
return output;
|
|
79
|
-
}),
|
|
80
|
-
},
|
|
81
|
-
aggregate: {
|
|
82
|
-
format_tool: ({ resourceLabel }) => `Aggregate ${resourceLabel}`,
|
|
83
|
-
},
|
|
84
|
-
start_custom_action: {
|
|
85
|
-
format_tool: ({ inputs, resourceLabel }) => `Run ${resourceLabel}${actionText(inputs)}`,
|
|
86
|
-
},
|
|
87
|
-
start_custom_bulk_action: {
|
|
88
|
-
format_tool: ({ inputs, resourceLabel }) => {
|
|
89
|
-
const recordCount = getInputArrayLength(inputs, 'recordIds');
|
|
90
|
-
const recordsText = recordCount === undefined ? '' : ` for ${recordCount} records`;
|
|
91
|
-
return `Run ${resourceLabel}${actionText(inputs)}${recordsText}`;
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
start_bulk_action: {
|
|
95
|
-
format_tool: ({ inputs, resourceLabel }) => {
|
|
96
|
-
const recordCount = getInputArrayLength(inputs, 'recordIds');
|
|
97
|
-
const recordsText = recordCount === undefined ? '' : ` for ${recordCount} records`;
|
|
98
|
-
return `Run ${resourceLabel}${actionText(inputs)}${recordsText}`;
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
create_record: {
|
|
102
|
-
format_tool: ({ resourceLabel }) => `Create ${resourceLabel}`,
|
|
103
|
-
},
|
|
104
|
-
update_record: {
|
|
105
|
-
format_tool: ({ resourceLabel }) => `Update ${resourceLabel}`,
|
|
106
|
-
},
|
|
107
|
-
delete_record: {
|
|
108
|
-
format_tool: ({ resourceLabel }) => `Delete ${resourceLabel}`,
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
function sanitizeForYaml(value) {
|
|
112
|
-
const traversalStack = [];
|
|
113
|
-
const serialized = JSON.stringify(value, function (_key, nestedValue) {
|
|
114
|
-
if (typeof nestedValue === 'function' || typeof nestedValue === 'symbol' || nestedValue === undefined) {
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
if (typeof nestedValue === 'bigint') {
|
|
118
|
-
return nestedValue.toString();
|
|
119
|
-
}
|
|
120
|
-
if (typeof nestedValue !== 'object' || nestedValue === null) {
|
|
121
|
-
return nestedValue;
|
|
122
|
-
}
|
|
123
|
-
if (nestedValue instanceof Map) {
|
|
124
|
-
return Object.fromEntries(nestedValue);
|
|
125
|
-
}
|
|
126
|
-
if (nestedValue instanceof Set) {
|
|
127
|
-
return Array.from(nestedValue.values());
|
|
128
|
-
}
|
|
129
|
-
while (traversalStack.length > 0 && traversalStack[traversalStack.length - 1] !== this) {
|
|
130
|
-
traversalStack.pop();
|
|
131
|
-
}
|
|
132
|
-
if (traversalStack.includes(nestedValue)) {
|
|
133
|
-
return undefined;
|
|
134
|
-
}
|
|
135
|
-
traversalStack.push(nestedValue);
|
|
136
|
-
return nestedValue;
|
|
137
|
-
});
|
|
138
|
-
if (serialized === undefined) {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
return JSON.parse(serialized);
|
|
142
|
-
}
|
|
143
|
-
function wipePath(target, pathParts) {
|
|
144
|
-
if (!target || typeof target !== 'object' || pathParts.length === 0) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const [currentPart, ...rest] = pathParts;
|
|
148
|
-
const isArrayTraversal = currentPart.endsWith('[]');
|
|
149
|
-
const key = isArrayTraversal ? currentPart.slice(0, -2) : currentPart;
|
|
150
|
-
const targetRecord = target;
|
|
151
|
-
if (!(key in targetRecord)) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
if (rest.length === 0) {
|
|
155
|
-
delete targetRecord[key];
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const nextValue = targetRecord[key];
|
|
159
|
-
if (isArrayTraversal) {
|
|
160
|
-
if (!Array.isArray(nextValue)) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
for (const item of nextValue) {
|
|
164
|
-
wipePath(item, rest);
|
|
165
|
-
}
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
wipePath(nextValue, rest);
|
|
169
|
-
}
|
|
170
|
-
function hasToolError(output) {
|
|
171
|
-
return typeof output === 'object' && output !== null && 'error' in output;
|
|
172
|
-
}
|
|
173
|
-
function hasGetResourceDataRows(output) {
|
|
174
|
-
if (typeof output !== 'object' || output === null || !('data' in output)) {
|
|
175
|
-
return false;
|
|
176
|
-
}
|
|
177
|
-
return Array.isArray(output.data);
|
|
178
|
-
}
|
|
179
|
-
function getDateTimeColumnNames(adminforth, inputs) {
|
|
180
|
-
const resourceId = getInputString(inputs, 'resourceId');
|
|
181
|
-
const resource = adminforth.config.resources.find((res) => res.resourceId === resourceId);
|
|
182
|
-
if (!resource) {
|
|
183
|
-
return [];
|
|
184
|
-
}
|
|
185
|
-
return resource.dataSourceColumns
|
|
186
|
-
.filter((column) => column.type === AdminForthDataTypes.DATETIME)
|
|
187
|
-
.map((column) => column.name);
|
|
188
|
-
}
|
|
189
|
-
function formatGmtOffset(offsetMinutes) {
|
|
190
|
-
const sign = offsetMinutes >= 0 ? '+' : '-';
|
|
191
|
-
const absoluteOffsetMinutes = Math.abs(offsetMinutes);
|
|
192
|
-
const hours = Math.floor(absoluteOffsetMinutes / 60);
|
|
193
|
-
const minutes = absoluteOffsetMinutes % 60;
|
|
194
|
-
if (minutes === 0) {
|
|
195
|
-
return `GMT${sign}${hours}`;
|
|
196
|
-
}
|
|
197
|
-
return `GMT${sign}${hours}:${String(minutes).padStart(2, '0')}`;
|
|
198
|
-
}
|
|
199
|
-
function formatDateTimeValue(value, userTimeZone) {
|
|
200
|
-
const localizedValue = dayjs.utc(value).tz(userTimeZone);
|
|
201
|
-
return `${localizedValue.format('DD MMM YYYY, HH:mm:ss.SSS')} (${formatGmtOffset(localizedValue.utcOffset())})`;
|
|
202
|
-
}
|
|
203
|
-
function formatDateTimeColumns(rows, dateTimeColumnNames, userTimeZone) {
|
|
204
|
-
for (const row of rows) {
|
|
205
|
-
for (const columnName of dateTimeColumnNames) {
|
|
206
|
-
const value = row[columnName];
|
|
207
|
-
if (typeof value === 'string' && value) {
|
|
208
|
-
row[columnName] = formatDateTimeValue(value, userTimeZone);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
function applyToolOverride(params) {
|
|
214
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
215
|
-
var _a;
|
|
216
|
-
const { adminforth, adminUser, inputs, output, toolName, userTimeZone } = params;
|
|
217
|
-
const sanitizedOutput = sanitizeForYaml(output);
|
|
218
|
-
const override = TOOL_OVERRIDES[toolName];
|
|
219
|
-
if (!override) {
|
|
220
|
-
return sanitizedOutput;
|
|
221
|
-
}
|
|
222
|
-
for (const path of (_a = override.wipe_frontend_specific_data) !== null && _a !== void 0 ? _a : []) {
|
|
223
|
-
wipePath(sanitizedOutput, path.split('.'));
|
|
224
|
-
}
|
|
225
|
-
return override.post_process_response
|
|
226
|
-
? sanitizeForYaml(yield override.post_process_response({
|
|
227
|
-
adminforth,
|
|
228
|
-
adminUser,
|
|
229
|
-
output: sanitizedOutput,
|
|
230
|
-
inputs,
|
|
231
|
-
userTimeZone,
|
|
232
|
-
}))
|
|
233
|
-
: sanitizedOutput;
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
function endpointPathToToolName(path) {
|
|
237
|
-
return path
|
|
238
|
-
.replace(/^\/+/, '')
|
|
239
|
-
.replace(/[^a-zA-Z0-9_]+/g, '_')
|
|
240
|
-
.replace(/^_+|_+$/g, '');
|
|
241
|
-
}
|
|
242
|
-
function stripAdminApiPrefix(path, adminforth) {
|
|
243
|
-
const configuredBaseUrl = adminforth.config.baseUrl || '';
|
|
244
|
-
const normalizedBaseUrl = configuredBaseUrl.endsWith('/')
|
|
245
|
-
? configuredBaseUrl.slice(0, -1)
|
|
246
|
-
: configuredBaseUrl;
|
|
247
|
-
const apiPrefix = `${normalizedBaseUrl}/adminapi/v1`;
|
|
248
|
-
if (path.startsWith(apiPrefix)) {
|
|
249
|
-
const strippedPath = path.slice(apiPrefix.length);
|
|
250
|
-
return strippedPath.startsWith('/') ? strippedPath : `/${strippedPath}`;
|
|
251
|
-
}
|
|
252
|
-
return path;
|
|
253
|
-
}
|
|
254
|
-
function openApiSchemaPathToToolName(path, adminforth) {
|
|
255
|
-
return endpointPathToToolName(stripAdminApiPrefix(path, adminforth));
|
|
256
|
-
}
|
|
257
|
-
function formatLogNameList(names) {
|
|
258
|
-
return names.length ? names.join(', ') : '(none)';
|
|
259
|
-
}
|
|
260
|
-
export function formatApiBasedToolCall(params) {
|
|
261
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
-
var _a, _b;
|
|
263
|
-
return yield ((_b = (_a = TOOL_OVERRIDES[params.toolName]) === null || _a === void 0 ? void 0 : _a.format_tool) === null || _b === void 0 ? void 0 : _b.call(_a, {
|
|
264
|
-
adminforth: params.adminforth,
|
|
265
|
-
adminUser: params.adminUser,
|
|
266
|
-
inputs: params.inputs,
|
|
267
|
-
resourceLabel: resourceLabel(params.adminforth, params.inputs),
|
|
268
|
-
userTimeZone: params.userTimeZone,
|
|
269
|
-
}));
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
function normalizeDateTimeInputsToUtc(body, adminforth, userTimeZone) {
|
|
273
|
-
if (!userTimeZone || typeof body.resourceId !== 'string') {
|
|
274
|
-
return body;
|
|
275
|
-
}
|
|
276
|
-
const resource = adminforth.config.resources.find((res) => res.resourceId === body.resourceId);
|
|
277
|
-
if (!resource) {
|
|
278
|
-
return body;
|
|
279
|
-
}
|
|
280
|
-
const columnsByName = new Map(resource.dataSourceColumns.map((column) => [column.name, column]));
|
|
281
|
-
const normalizeColumnValue = (value, columnType) => {
|
|
282
|
-
if (Array.isArray(value)) {
|
|
283
|
-
return value.map((item) => normalizeColumnValue(item, columnType));
|
|
284
|
-
}
|
|
285
|
-
if (typeof value !== 'string' || value === '') {
|
|
286
|
-
return value;
|
|
287
|
-
}
|
|
288
|
-
if (columnType === AdminForthDataTypes.DATETIME) {
|
|
289
|
-
return dayjs.tz(value, userTimeZone).utc().toISOString();
|
|
290
|
-
}
|
|
291
|
-
const userDate = dayjs().tz(userTimeZone).format('YYYY-MM-DD');
|
|
292
|
-
return dayjs.tz(`${userDate}T${value}`, userTimeZone).utc().format('HH:mm:ss');
|
|
293
|
-
};
|
|
294
|
-
const normalizeValue = (value, key) => {
|
|
295
|
-
const column = key ? columnsByName.get(key) : undefined;
|
|
296
|
-
if ((column === null || column === void 0 ? void 0 : column.type) === AdminForthDataTypes.DATETIME || (column === null || column === void 0 ? void 0 : column.type) === AdminForthDataTypes.TIME) {
|
|
297
|
-
return normalizeColumnValue(value, column.type);
|
|
298
|
-
}
|
|
299
|
-
if (Array.isArray(value)) {
|
|
300
|
-
return value.map((item) => normalizeValue(item));
|
|
301
|
-
}
|
|
302
|
-
if (!value || typeof value !== 'object') {
|
|
303
|
-
return value;
|
|
304
|
-
}
|
|
305
|
-
const record = value;
|
|
306
|
-
const filterColumn = typeof record.field === 'string' ? columnsByName.get(record.field) : undefined;
|
|
307
|
-
if ('value' in record &&
|
|
308
|
-
((filterColumn === null || filterColumn === void 0 ? void 0 : filterColumn.type) === AdminForthDataTypes.DATETIME || (filterColumn === null || filterColumn === void 0 ? void 0 : filterColumn.type) === AdminForthDataTypes.TIME)) {
|
|
309
|
-
return Object.assign(Object.assign({}, record), { value: normalizeColumnValue(record.value, filterColumn.type) });
|
|
310
|
-
}
|
|
311
|
-
return Object.fromEntries(Object.entries(record).map(([nestedKey, nestedValue]) => [
|
|
312
|
-
nestedKey,
|
|
313
|
-
normalizeValue(nestedValue, nestedKey),
|
|
314
|
-
]));
|
|
315
|
-
};
|
|
316
|
-
return normalizeValue(body);
|
|
317
|
-
}
|
|
318
|
-
const METHODS_WITHOUT_REQUEST_BODY = new Set(['GET', 'HEAD']);
|
|
319
|
-
const TOOL_HANDLER_TIMEOUT_MS = 15000;
|
|
320
|
-
const EMPTY_HANDLER_RESPONSE_ERROR = {
|
|
321
|
-
error: 'EMPTY_HANDLER_RESPONSE',
|
|
322
|
-
message: 'Tool handler completed without returning a response.',
|
|
323
|
-
};
|
|
324
|
-
const TOOL_HANDLER_TIMEOUT_ERROR = {
|
|
325
|
-
error: 'TOOL_HANDLER_TIMEOUT',
|
|
326
|
-
message: `Tool handler timed out after ${TOOL_HANDLER_TIMEOUT_MS / 1000} seconds.`,
|
|
327
|
-
};
|
|
328
|
-
function createDirectToolResponse() {
|
|
329
|
-
const headers = [];
|
|
330
|
-
return {
|
|
331
|
-
headers,
|
|
332
|
-
status: 200,
|
|
333
|
-
setHeader(name, value) {
|
|
334
|
-
headers.push([name, value]);
|
|
335
|
-
},
|
|
336
|
-
setStatus(code, message) {
|
|
337
|
-
this.status = code;
|
|
338
|
-
this.message = message;
|
|
339
|
-
},
|
|
340
|
-
blobStream() {
|
|
341
|
-
throw new Error('blobStream is not available for API-based agent tools');
|
|
342
|
-
},
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
function validationErrorResponse(error, details) {
|
|
346
|
-
return {
|
|
347
|
-
error,
|
|
348
|
-
details,
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
function withToolHandlerTimeout(callback, abortSignal) {
|
|
352
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
353
|
-
const controller = new AbortController();
|
|
354
|
-
const abortHandler = () => controller.abort();
|
|
355
|
-
let timeout;
|
|
356
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
357
|
-
timeout = setTimeout(() => {
|
|
358
|
-
controller.abort();
|
|
359
|
-
resolve(TOOL_HANDLER_TIMEOUT_ERROR);
|
|
360
|
-
}, TOOL_HANDLER_TIMEOUT_MS);
|
|
361
|
-
});
|
|
362
|
-
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.addEventListener('abort', abortHandler, { once: true });
|
|
363
|
-
try {
|
|
364
|
-
return yield Promise.race([
|
|
365
|
-
callback(controller.signal),
|
|
366
|
-
timeoutPromise,
|
|
367
|
-
]);
|
|
368
|
-
}
|
|
369
|
-
finally {
|
|
370
|
-
clearTimeout(timeout);
|
|
371
|
-
abortSignal === null || abortSignal === void 0 ? void 0 : abortSignal.removeEventListener('abort', abortHandler);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
function callOpenApiSchema(params) {
|
|
376
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
377
|
-
const { adminforth, adminUser, abortSignal, inputs, schema, toolName, userTimeZone, acceptLanguage } = params;
|
|
378
|
-
const method = schema.method.toUpperCase();
|
|
379
|
-
const normalizedInputs = normalizeDateTimeInputsToUtc((inputs !== null && inputs !== void 0 ? inputs : {}), adminforth, userTimeZone);
|
|
380
|
-
const hasRequestBody = !METHODS_WITHOUT_REQUEST_BODY.has(method);
|
|
381
|
-
const body = hasRequestBody ? normalizedInputs : {};
|
|
382
|
-
const query = hasRequestBody ? {} : normalizedInputs;
|
|
383
|
-
const requestValidation = adminforth.openApi.validateRequestSchema(schema, body);
|
|
384
|
-
if (!requestValidation.valid) {
|
|
385
|
-
return validationErrorResponse('REQUEST_VALIDATION_FAILED', requestValidation.errors);
|
|
386
|
-
}
|
|
387
|
-
const response = createDirectToolResponse();
|
|
388
|
-
const lang = acceptLanguage !== null && acceptLanguage !== void 0 ? acceptLanguage : "en";
|
|
389
|
-
const tr = (msg, category, trParams, pluralizationNumber) => adminforth.tr(msg, category, lang, trParams, pluralizationNumber);
|
|
390
|
-
const output = yield withToolHandlerTimeout((handlerAbortSignal) => schema.handler({
|
|
391
|
-
body,
|
|
392
|
-
query,
|
|
393
|
-
headers: {},
|
|
394
|
-
cookies: [],
|
|
395
|
-
adminUser,
|
|
396
|
-
response,
|
|
397
|
-
requestUrl: schema.path,
|
|
398
|
-
abortSignal: handlerAbortSignal,
|
|
399
|
-
_raw_express_req: undefined,
|
|
400
|
-
_raw_express_res: undefined,
|
|
401
|
-
tr,
|
|
402
|
-
}), abortSignal);
|
|
403
|
-
if (response.message) {
|
|
404
|
-
return response.status >= 400
|
|
405
|
-
? {
|
|
406
|
-
error: 'HANDLER_ERROR',
|
|
407
|
-
status: response.status,
|
|
408
|
-
response: response.message,
|
|
409
|
-
}
|
|
410
|
-
: response.message;
|
|
411
|
-
}
|
|
412
|
-
if (output === undefined) {
|
|
413
|
-
return EMPTY_HANDLER_RESPONSE_ERROR;
|
|
414
|
-
}
|
|
415
|
-
if (output === null) {
|
|
416
|
-
return { status: response.status };
|
|
417
|
-
}
|
|
418
|
-
const responseValidation = adminforth.openApi.validateResponseSchema(schema, output);
|
|
419
|
-
if (!responseValidation.valid) {
|
|
420
|
-
return validationErrorResponse('RESPONSE_VALIDATION_FAILED', responseValidation.errors);
|
|
421
|
-
}
|
|
422
|
-
return response.status >= 400
|
|
423
|
-
? {
|
|
424
|
-
error: 'HANDLER_ERROR',
|
|
425
|
-
status: response.status,
|
|
426
|
-
response: output,
|
|
427
|
-
}
|
|
428
|
-
: output;
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
function stringifyToolOutput(output) {
|
|
432
|
-
return YAML.stringify(output === undefined ? EMPTY_HANDLER_RESPONSE_ERROR : output);
|
|
433
|
-
}
|
|
434
|
-
function attachDurationMs(output, durationMs) {
|
|
435
|
-
if (output !== null && typeof output === 'object' && !Array.isArray(output)) {
|
|
436
|
-
return Object.assign(Object.assign({}, output), { durationMs });
|
|
437
|
-
}
|
|
438
|
-
return { result: output, durationMs };
|
|
439
|
-
}
|
|
440
|
-
export function prepareApiBasedTools(adminforth, hiddenResourceIds = []) {
|
|
441
|
-
var _a;
|
|
442
|
-
const apiBasedTools = {};
|
|
443
|
-
const openApiSchemas = adminforth.openApi.registeredSchemas;
|
|
444
|
-
const openApiSchemasByToolName = new Map();
|
|
445
|
-
const hiddenResourceIdSet = new Set(hiddenResourceIds);
|
|
446
|
-
for (const schema of openApiSchemas) {
|
|
447
|
-
const toolName = openApiSchemaPathToToolName(schema.path, adminforth);
|
|
448
|
-
if (hasRegisteredApiToolHandler(schema) && !((_a = schema.agent) === null || _a === void 0 ? void 0 : _a.hiddenFromAgents)) {
|
|
449
|
-
openApiSchemasByToolName.set(toolName, schema);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
for (const [toolName, schema] of openApiSchemasByToolName.entries()) {
|
|
453
|
-
apiBasedTools[toolName] = {
|
|
454
|
-
description: schema.description,
|
|
455
|
-
input_schema: schema.request_schema,
|
|
456
|
-
agent: schema.agent,
|
|
457
|
-
call: (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ adminUser, adminuser, abortSignal, inputs, userTimeZone, acceptLanguage } = {}) {
|
|
458
|
-
const startedAt = Date.now();
|
|
459
|
-
if (isHiddenResourceCall(hiddenResourceIdSet, inputs)) {
|
|
460
|
-
return stringifyToolOutput(attachDurationMs({
|
|
461
|
-
error: 'RESOURCE_NOT_AVAILABLE',
|
|
462
|
-
message: 'This resource is not available to the agent.',
|
|
463
|
-
}, Date.now() - startedAt));
|
|
464
|
-
}
|
|
465
|
-
const output = yield callOpenApiSchema({
|
|
466
|
-
adminforth,
|
|
467
|
-
adminUser: adminUser !== null && adminUser !== void 0 ? adminUser : adminuser,
|
|
468
|
-
abortSignal,
|
|
469
|
-
schema,
|
|
470
|
-
toolName,
|
|
471
|
-
inputs,
|
|
472
|
-
userTimeZone,
|
|
473
|
-
acceptLanguage,
|
|
474
|
-
});
|
|
475
|
-
const processedOutput = yield applyToolOverride({
|
|
476
|
-
adminforth,
|
|
477
|
-
adminUser: adminUser !== null && adminUser !== void 0 ? adminUser : adminuser,
|
|
478
|
-
inputs,
|
|
479
|
-
output,
|
|
480
|
-
toolName,
|
|
481
|
-
userTimeZone,
|
|
482
|
-
});
|
|
483
|
-
return stringifyToolOutput(attachDurationMs(processedOutput, Date.now() - startedAt));
|
|
484
|
-
}),
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
return apiBasedTools;
|
|
488
|
-
}
|
package/dist/tools/apiTool.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ApiBasedTool } from "./apiBasedTools.js";
|
|
2
|
-
export declare function createApiTool(toolName: string, apiBasedTool: ApiBasedTool): import("langchain").DynamicStructuredTool<Record<string, unknown> | {
|
|
3
|
-
readonly type: "object";
|
|
4
|
-
readonly properties: {};
|
|
5
|
-
readonly additionalProperties: true;
|
|
6
|
-
}, unknown, unknown, string, unknown, string>;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
export declare function createFetchSkillTool(customComponentsDir: string, pluginCustomFolderPaths?: string[]): Promise<import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
3
|
-
skillName: z.ZodString;
|
|
4
|
-
}, z.core.$strip>, {
|
|
5
|
-
skillName: string;
|
|
6
|
-
}, {
|
|
7
|
-
skillName: string;
|
|
8
|
-
}, string, unknown, "fetch_skill">>;
|
package/dist/tools/fetchSkill.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { tool } from "langchain";
|
|
11
|
-
import { z } from "zod";
|
|
12
|
-
import { listSkillManifests, loadSkillMarkdown, } from "./skills/registry.js";
|
|
13
|
-
const fetchSkillSchema = z.object({
|
|
14
|
-
skillName: z
|
|
15
|
-
.string()
|
|
16
|
-
.describe("Name of the custom AdminForth skill to load, for example fetch_data or mutate_data."),
|
|
17
|
-
});
|
|
18
|
-
function serializeSkillManifests(skillManifests) {
|
|
19
|
-
return skillManifests.map((skill) => ({
|
|
20
|
-
name: skill.name,
|
|
21
|
-
description: skill.description,
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
export function createFetchSkillTool(customComponentsDir_1) {
|
|
25
|
-
return __awaiter(this, arguments, void 0, function* (customComponentsDir, pluginCustomFolderPaths = []) {
|
|
26
|
-
const availableSkills = yield listSkillManifests(customComponentsDir, pluginCustomFolderPaths);
|
|
27
|
-
const availableSkillNames = availableSkills.map((skill) => skill.name);
|
|
28
|
-
return tool((_a) => __awaiter(this, [_a], void 0, function* ({ skillName }) {
|
|
29
|
-
try {
|
|
30
|
-
const skillMarkdown = yield loadSkillMarkdown(skillName, customComponentsDir, pluginCustomFolderPaths);
|
|
31
|
-
if (!skillMarkdown) {
|
|
32
|
-
return [
|
|
33
|
-
`Skill "${skillName}" not found.`,
|
|
34
|
-
"Available skills:",
|
|
35
|
-
JSON.stringify(serializeSkillManifests(availableSkills), null, 2),
|
|
36
|
-
].join("\n");
|
|
37
|
-
}
|
|
38
|
-
return skillMarkdown;
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
return `Failed to load skill "${skillName}": ${error instanceof Error ? error.message : String(error)}`;
|
|
42
|
-
}
|
|
43
|
-
}), {
|
|
44
|
-
name: "fetch_skill",
|
|
45
|
-
description: `Fetch the raw SKILL.md content for a custom AdminForth skill by name.${availableSkillNames.length > 0
|
|
46
|
-
? ` Available skills: ${availableSkillNames.join(", ")}.`
|
|
47
|
-
: ""}`,
|
|
48
|
-
schema: fetchSkillSchema,
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
package/dist/tools/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { ClientTool } from "@langchain/core/tools";
|
|
2
|
-
import type { ApiBasedTool } from "./apiBasedTools.js";
|
|
3
|
-
export declare const ALWAYS_AVAILABLE_API_TOOL_NAMES: readonly ["get_resource"];
|
|
4
|
-
export declare function createAgentTools(customComponentsDir: string, apiBasedTools: Record<string, ApiBasedTool>, pluginCustomFolderPaths?: string[]): Promise<ClientTool[]>;
|
package/dist/tools/index.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { createFetchSkillTool } from "./fetchSkill.js";
|
|
11
|
-
import { createFetchToolSchemaTool } from "./fetchToolSchema.js";
|
|
12
|
-
import { createApiTool } from "./apiTool.js";
|
|
13
|
-
import { createGetUserLocationTool } from "./getUserLocation.js";
|
|
14
|
-
import { createNavigateUserTool } from "./navigateUser.js";
|
|
15
|
-
export const ALWAYS_AVAILABLE_API_TOOL_NAMES = ["get_resource"];
|
|
16
|
-
export function createAgentTools(customComponentsDir_1, apiBasedTools_1) {
|
|
17
|
-
return __awaiter(this, arguments, void 0, function* (customComponentsDir, apiBasedTools, pluginCustomFolderPaths = []) {
|
|
18
|
-
return [
|
|
19
|
-
...ALWAYS_AVAILABLE_API_TOOL_NAMES.map((toolName) => {
|
|
20
|
-
const apiBasedTool = apiBasedTools[toolName];
|
|
21
|
-
if (!apiBasedTool) {
|
|
22
|
-
throw new Error(`Required base API tool "${toolName}" is missing.`);
|
|
23
|
-
}
|
|
24
|
-
return createApiTool(toolName, apiBasedTool);
|
|
25
|
-
}),
|
|
26
|
-
createGetUserLocationTool(),
|
|
27
|
-
createNavigateUserTool(),
|
|
28
|
-
yield createFetchSkillTool(customComponentsDir, pluginCustomFolderPaths),
|
|
29
|
-
yield createFetchToolSchemaTool(apiBasedTools),
|
|
30
|
-
];
|
|
31
|
-
});
|
|
32
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export interface AgentSkillManifest {
|
|
2
|
-
directoryName: string;
|
|
3
|
-
name: string;
|
|
4
|
-
description: string;
|
|
5
|
-
instructions: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function getProjectSkillsDirectoryPath(customComponentsDir: string): string;
|
|
8
|
-
export declare function listProjectSkillManifests(customComponentsDir: string, pluginCustomFolderPaths?: string[]): Promise<AgentSkillManifest[]>;
|
|
9
|
-
export declare function listSkillManifests(customComponentsDir: string, pluginCustomFolderPaths?: string[]): Promise<AgentSkillManifest[]>;
|
|
10
|
-
export declare function loadSkillManifest(skillName: string, customComponentsDir: string, pluginCustomFolderPaths?: string[]): Promise<AgentSkillManifest | null>;
|
|
11
|
-
export declare function loadSkillMarkdown(skillName: string, customComponentsDir: string, pluginCustomFolderPaths?: string[]): Promise<string | null>;
|