@adminforth/agent 3.1.10 → 3.3.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/ChatSurface.vue +25 -8
- package/dist/custom/{skills/charts/SKILL.md → charts.md} +2 -4
- package/dist/custom/composables/useAgentStore.ts +0 -20
- package/dist/custom/composables/useAgentTransitions.ts +13 -37
- package/dist/custom/utils.ts +5 -0
- 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,121 +0,0 @@
|
|
|
1
|
-
name: mutate_data
|
|
2
|
-
description: Create/update/delete some record of resource or call actions on one or multiple records
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
# Involved tools
|
|
6
|
-
|
|
7
|
-
Use `create_record` for creating records.
|
|
8
|
-
|
|
9
|
-
Use `update_record` for editing records.
|
|
10
|
-
|
|
11
|
-
Use `delete_record` for deleting records.
|
|
12
|
-
|
|
13
|
-
Use `start_custom_action` and `start_custom_bulk_action` for resource actions.
|
|
14
|
-
|
|
15
|
-
# General rules
|
|
16
|
-
|
|
17
|
-
- if there is a dedicated action for some routine (result of `get_resource` tool call, field actions), prefer this to manual updating of records, for example, if you want to approve some comment, prefer calling `approve` action instead of updating `approved` field of comment record (because in action there might be some additional logic like sending notification to user, updating some counters and so on)
|
|
18
|
-
|
|
19
|
-
## Mutation context
|
|
20
|
-
|
|
21
|
-
Before performing any state mutation including action calls edit/delete please fetch record which is going to be edited/deleted and show user record in format field → value (show several most important fields which can help user to understand what exactly record he is going to edit or delete).
|
|
22
|
-
|
|
23
|
-
Every mutation description must describe one exact fetched row. Never combine `_label`, primary key, link, or field values from different rows or different resources in one description.
|
|
24
|
-
|
|
25
|
-
For field values with long texts show only several first words and add "..." at the end.
|
|
26
|
-
Also please add related link to record with will be changed. Build it as `{ADMIN_BASE_PATH}resource/{resourceId}/show/{primary key}`. Use _label from `get_resource_data` as anchor text for link (use markdown link). Links should always be relative paths and must start with `ADMIN_BASE_PATH`. Do not add an extra slash after `ADMIN_BASE_PATH`.
|
|
27
|
-
|
|
28
|
-
Before calling the mutation tool, verify that the `resourceId`, `{primary key}`, `_label`, and all shown fields come from the same exact fetched row.
|
|
29
|
-
|
|
30
|
-
Never show information about more than 10 records in one message. If a mutation plan affects more than 10 records, show only the 10 most important examples plus the total count if known.
|
|
31
|
-
|
|
32
|
-
When creating new record, show user all data which you gonna create.
|
|
33
|
-
|
|
34
|
-
Do not ask user for textual confirmation. Dangerous tools are approved by the runtime approval UI.
|
|
35
|
-
|
|
36
|
-
# Calling actions
|
|
37
|
-
|
|
38
|
-
To call action on some record you can use `start_custom_action` tool, or `start_custom_bulk_action` if you need to perform action on several records at once.
|
|
39
|
-
|
|
40
|
-
Before calling any of this action you should understand whether this action is allowed. User result of `get_resource` tool call and check `action.allowed` - if this attribute is true or is not exists, assume action is allowed. If this attribute is false, action is not allowed, you should warn user that this action is not allowed for him.
|
|
41
|
-
|
|
42
|
-
### Example
|
|
43
|
-
|
|
44
|
-
If you want to block some user you can describe the action by saying:
|
|
45
|
-
|
|
46
|
-
```I am going to block user:
|
|
47
|
-
* Username: john_doe
|
|
48
|
-
* Email: john_doe@example.com
|
|
49
|
-
* IP Country: USA
|
|
50
|
-
* Currently blocked: No // show this field only if it exists in user record
|
|
51
|
-
|
|
52
|
-
View [John Doe]({ADMIN_BASE_PATH}resource/users/show/123)
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Updating
|
|
56
|
-
|
|
57
|
-
You can use tool `update_record` tool it updates fields of record. To update `allowedActions.edit` should be set to true and
|
|
58
|
-
`updated` column `showIn.edit` should be true at the same time. If one of this condition is not met, explain to user that is not allowed to edit
|
|
59
|
-
|
|
60
|
-
In addition to instructions above show user the table of edits (old value/new value)
|
|
61
|
-
|
|
62
|
-
### Examples
|
|
63
|
-
|
|
64
|
-
For example if you gonna modify user record, please share full user info (not only username but also email, ip country - anything which help adminto check that that is correct user). Message could look like this:
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
I am going to update user:
|
|
68
|
-
* Username: john_doe
|
|
69
|
-
* Email: john_doe@example.com
|
|
70
|
-
* IP Country: USA
|
|
71
|
-
I am going to change email from john_doe@example.com to new_email@example.com
|
|
72
|
-
|
|
73
|
-
View [John Doe]({ADMIN_BASE_PATH}resource/users/show/123)
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
## Deleting
|
|
78
|
-
|
|
79
|
-
To delete some record you can use `delete_record` tool. To delete record `allowedActions.delete` should be set to true.
|
|
80
|
-
|
|
81
|
-
### Example
|
|
82
|
-
|
|
83
|
-
If you gonna delete user record, please share full user info (not only username but also email, ip country - anything which help adminto check that that is correct user). Message could look like this:
|
|
84
|
-
|
|
85
|
-
```I am going to delete user:
|
|
86
|
-
* Username: john_doe
|
|
87
|
-
* Email: john_doe@example.com
|
|
88
|
-
* Signed up: 2024 Jan 1
|
|
89
|
-
* IP Country: USA
|
|
90
|
-
|
|
91
|
-
View [John Doe]({ADMIN_BASE_PATH}resource/users/show/123)
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Creating
|
|
95
|
-
|
|
96
|
-
To create new record you can use tool `create_record`. To create record `allowedActions.create` should be set to true.
|
|
97
|
-
|
|
98
|
-
When calling `create_record` tool pass only columns which have `showIn.create` set to true and `backendOnly` is not set to true.
|
|
99
|
-
|
|
100
|
-
For decimal fields please use string values with dot as decimal separator.
|
|
101
|
-
|
|
102
|
-
After creation of new record also show user a link to this record. If several records record were created, show links to all of them in list.
|
|
103
|
-
|
|
104
|
-
If more than 10 records were created, show links or details for only 10 of them and summarize the remaining count.
|
|
105
|
-
|
|
106
|
-
Omit any pictures or file paths, you are not capable of doing it. If they are not required all is good, if they are required, explain to user that you are not able to create record because of this reason.
|
|
107
|
-
|
|
108
|
-
### Working with dates
|
|
109
|
-
|
|
110
|
-
When you create or update date or datetime fields, please use ISO format for this. For example, "2024-01-01" for date and "2024-01-01T12:00:00Z" for datetime. If user provides date in different format, try to parse it and convert to ISO format.
|
|
111
|
-
|
|
112
|
-
### Example
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
I am going to create user:
|
|
117
|
-
* Username: john_doe
|
|
118
|
-
* Email: john_doe@example.com
|
|
119
|
-
|
|
120
|
-
View [John Doe]({ADMIN_BASE_PATH}resource/users/show/421) # 421 is id of new created record
|
|
121
|
-
```
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { type IAdminForth } from "adminforth";
|
|
2
|
-
import { type ApiBasedTool } from "../../tools/apiBasedTools.js";
|
|
3
|
-
export declare function createApiBasedToolsMiddleware(apiBasedTools: Record<string, ApiBasedTool>, adminforth: IAdminForth): import("langchain").AgentMiddleware<undefined, undefined, unknown, readonly (import("@langchain/core/tools").ClientTool | import("@langchain/core/tools").ServerTool)[]>;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { type AdminUser, type IAdminForth } from 'adminforth';
|
|
2
|
-
type AgentToolMeta = {
|
|
3
|
-
requiresHumanApproval?: boolean;
|
|
4
|
-
/** @deprecated Older name of `requiresHumanApproval`, still set by endpoints built for earlier AdminForth versions. */
|
|
5
|
-
isDangerous?: boolean;
|
|
6
|
-
hiddenFromAgents?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export type ApiBasedToolCallParams = {
|
|
9
|
-
adminUser?: AdminUser;
|
|
10
|
-
adminuser?: AdminUser;
|
|
11
|
-
abortSignal?: AbortSignal;
|
|
12
|
-
inputs?: Record<string, unknown>;
|
|
13
|
-
userTimeZone?: string;
|
|
14
|
-
acceptLanguage?: string;
|
|
15
|
-
};
|
|
16
|
-
export type ApiBasedTool = {
|
|
17
|
-
description?: string;
|
|
18
|
-
input_schema?: unknown;
|
|
19
|
-
agent?: AgentToolMeta;
|
|
20
|
-
call: (params?: ApiBasedToolCallParams) => Promise<string>;
|
|
21
|
-
};
|
|
22
|
-
export declare function formatApiBasedToolCall(params: {
|
|
23
|
-
adminforth: IAdminForth;
|
|
24
|
-
adminUser?: AdminUser;
|
|
25
|
-
inputs?: Record<string, unknown>;
|
|
26
|
-
toolName: string;
|
|
27
|
-
userTimeZone?: string;
|
|
28
|
-
}): Promise<string | undefined>;
|
|
29
|
-
export declare function prepareApiBasedTools(adminforth: IAdminForth, hiddenResourceIds?: Iterable<string>): Record<string, ApiBasedTool>;
|
|
30
|
-
export {};
|
|
@@ -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">>;
|