@adminforth/agent 1.49.0 → 1.49.1
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.
|
@@ -47,70 +47,6 @@ function getEnabledApiToolNames(messages: unknown[]) {
|
|
|
47
47
|
return enabledToolNames;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
const loadedApiToolNamesBySession = new Map<string, Set<string>>();
|
|
51
|
-
|
|
52
|
-
function getSessionLoadedApiToolNames(sessionId: string) {
|
|
53
|
-
let toolNames = loadedApiToolNamesBySession.get(sessionId);
|
|
54
|
-
|
|
55
|
-
if (!toolNames) {
|
|
56
|
-
toolNames = new Set<string>();
|
|
57
|
-
loadedApiToolNamesBySession.set(sessionId, toolNames);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return toolNames;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function getEnabledApiToolNamesForSession(messages: unknown[], sessionId?: string) {
|
|
64
|
-
const enabledToolNames = getEnabledApiToolNames(messages);
|
|
65
|
-
|
|
66
|
-
if (!sessionId) {
|
|
67
|
-
return enabledToolNames;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
for (const toolName of getSessionLoadedApiToolNames(sessionId)) {
|
|
71
|
-
enabledToolNames.add(toolName);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return enabledToolNames;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function getToolMessageContent(message: unknown) {
|
|
78
|
-
if (!ToolMessage.isInstance(message)) {
|
|
79
|
-
return "";
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return typeof message.content === "string"
|
|
83
|
-
? message.content
|
|
84
|
-
: Array.isArray(message.content)
|
|
85
|
-
? message.content
|
|
86
|
-
.map((block) =>
|
|
87
|
-
typeof block === "string"
|
|
88
|
-
? block
|
|
89
|
-
: "text" in block
|
|
90
|
-
? block.text
|
|
91
|
-
: "",
|
|
92
|
-
)
|
|
93
|
-
.join("")
|
|
94
|
-
: "";
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function rememberLoadedToolFromFetchResult(sessionId: string, result: unknown) {
|
|
98
|
-
if (!ToolMessage.isInstance(result) || result.name !== "fetch_tool_schema") {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
const parsed = JSON.parse(getToolMessageContent(result)) as {
|
|
104
|
-
status?: number;
|
|
105
|
-
name?: string;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
if (parsed.status === 200 && parsed.name) {
|
|
109
|
-
getSessionLoadedApiToolNames(sessionId).add(parsed.name);
|
|
110
|
-
}
|
|
111
|
-
} catch {}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
50
|
export function createApiBasedToolsMiddleware(
|
|
115
51
|
apiBasedTools: Record<string, ApiBasedTool>,
|
|
116
52
|
adminforth: IAdminForth,
|
|
@@ -126,11 +62,7 @@ export function createApiBasedToolsMiddleware(
|
|
|
126
62
|
return createMiddleware({
|
|
127
63
|
name: "ApiBasedToolsMiddleware",
|
|
128
64
|
async wrapModelCall(request, handler) {
|
|
129
|
-
const
|
|
130
|
-
const enabledApiToolNames = getEnabledApiToolNamesForSession(
|
|
131
|
-
request.state.messages,
|
|
132
|
-
sessionId,
|
|
133
|
-
);
|
|
65
|
+
const enabledApiToolNames = getEnabledApiToolNames(request.state.messages);
|
|
134
66
|
const tools = [...enabledApiToolNames]
|
|
135
67
|
.filter((toolName) => !alwaysAvailableApiToolNames.has(toolName))
|
|
136
68
|
.map((toolName) => dynamicTools[toolName]);
|
|
@@ -148,10 +80,9 @@ export function createApiBasedToolsMiddleware(
|
|
|
148
80
|
async wrapToolCall(request, handler) {
|
|
149
81
|
const startedAt = Date.now();
|
|
150
82
|
const toolInput = JSON.stringify(request.toolCall.args ?? {});
|
|
151
|
-
const { adminUser, emitToolCallEvent,
|
|
83
|
+
const { adminUser, emitToolCallEvent, userTimeZone } = request.runtime.context as {
|
|
152
84
|
adminUser: AdminUser;
|
|
153
85
|
emitToolCallEvent: ToolCallEventSink;
|
|
154
|
-
sessionId: string;
|
|
155
86
|
userTimeZone: string;
|
|
156
87
|
};
|
|
157
88
|
const toolArgs = (request.toolCall.args ?? {}) as Record<string, unknown>;
|
|
@@ -189,10 +120,7 @@ export function createApiBasedToolsMiddleware(
|
|
|
189
120
|
if (request.tool) {
|
|
190
121
|
result = await handler(request);
|
|
191
122
|
} else {
|
|
192
|
-
const enabledApiToolNames =
|
|
193
|
-
request.state.messages,
|
|
194
|
-
sessionId,
|
|
195
|
-
);
|
|
123
|
+
const enabledApiToolNames = getEnabledApiToolNames(request.state.messages);
|
|
196
124
|
|
|
197
125
|
if (enabledApiToolNames.has(request.toolCall.name)) {
|
|
198
126
|
result = await handler({
|
|
@@ -209,10 +137,6 @@ export function createApiBasedToolsMiddleware(
|
|
|
209
137
|
}
|
|
210
138
|
}
|
|
211
139
|
|
|
212
|
-
if (sessionId) {
|
|
213
|
-
rememberLoadedToolFromFetchResult(sessionId, result);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
140
|
toolCallTracker.finishSuccess(result);
|
|
217
141
|
return result;
|
|
218
142
|
} catch (error) {
|
|
@@ -41,53 +41,6 @@ function getEnabledApiToolNames(messages) {
|
|
|
41
41
|
}
|
|
42
42
|
return enabledToolNames;
|
|
43
43
|
}
|
|
44
|
-
const loadedApiToolNamesBySession = new Map();
|
|
45
|
-
function getSessionLoadedApiToolNames(sessionId) {
|
|
46
|
-
let toolNames = loadedApiToolNamesBySession.get(sessionId);
|
|
47
|
-
if (!toolNames) {
|
|
48
|
-
toolNames = new Set();
|
|
49
|
-
loadedApiToolNamesBySession.set(sessionId, toolNames);
|
|
50
|
-
}
|
|
51
|
-
return toolNames;
|
|
52
|
-
}
|
|
53
|
-
function getEnabledApiToolNamesForSession(messages, sessionId) {
|
|
54
|
-
const enabledToolNames = getEnabledApiToolNames(messages);
|
|
55
|
-
if (!sessionId) {
|
|
56
|
-
return enabledToolNames;
|
|
57
|
-
}
|
|
58
|
-
for (const toolName of getSessionLoadedApiToolNames(sessionId)) {
|
|
59
|
-
enabledToolNames.add(toolName);
|
|
60
|
-
}
|
|
61
|
-
return enabledToolNames;
|
|
62
|
-
}
|
|
63
|
-
function getToolMessageContent(message) {
|
|
64
|
-
if (!ToolMessage.isInstance(message)) {
|
|
65
|
-
return "";
|
|
66
|
-
}
|
|
67
|
-
return typeof message.content === "string"
|
|
68
|
-
? message.content
|
|
69
|
-
: Array.isArray(message.content)
|
|
70
|
-
? message.content
|
|
71
|
-
.map((block) => typeof block === "string"
|
|
72
|
-
? block
|
|
73
|
-
: "text" in block
|
|
74
|
-
? block.text
|
|
75
|
-
: "")
|
|
76
|
-
.join("")
|
|
77
|
-
: "";
|
|
78
|
-
}
|
|
79
|
-
function rememberLoadedToolFromFetchResult(sessionId, result) {
|
|
80
|
-
if (!ToolMessage.isInstance(result) || result.name !== "fetch_tool_schema") {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
const parsed = JSON.parse(getToolMessageContent(result));
|
|
85
|
-
if (parsed.status === 200 && parsed.name) {
|
|
86
|
-
getSessionLoadedApiToolNames(sessionId).add(parsed.name);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
catch (_a) { }
|
|
90
|
-
}
|
|
91
44
|
export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
92
45
|
const alwaysAvailableApiToolNames = new Set(ALWAYS_AVAILABLE_API_TOOL_NAMES);
|
|
93
46
|
const dynamicTools = Object.fromEntries(Object.entries(apiBasedTools).map(([toolName, apiBasedTool]) => [
|
|
@@ -98,8 +51,7 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
98
51
|
name: "ApiBasedToolsMiddleware",
|
|
99
52
|
wrapModelCall(request, handler) {
|
|
100
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
101
|
-
const
|
|
102
|
-
const enabledApiToolNames = getEnabledApiToolNamesForSession(request.state.messages, sessionId);
|
|
54
|
+
const enabledApiToolNames = getEnabledApiToolNames(request.state.messages);
|
|
103
55
|
const tools = [...enabledApiToolNames]
|
|
104
56
|
.filter((toolName) => !alwaysAvailableApiToolNames.has(toolName))
|
|
105
57
|
.map((toolName) => dynamicTools[toolName]);
|
|
@@ -113,7 +65,7 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
113
65
|
var _a, _b, _c, _d;
|
|
114
66
|
const startedAt = Date.now();
|
|
115
67
|
const toolInput = JSON.stringify((_a = request.toolCall.args) !== null && _a !== void 0 ? _a : {});
|
|
116
|
-
const { adminUser, emitToolCallEvent,
|
|
68
|
+
const { adminUser, emitToolCallEvent, userTimeZone } = request.runtime.context;
|
|
117
69
|
const toolArgs = ((_b = request.toolCall.args) !== null && _b !== void 0 ? _b : {});
|
|
118
70
|
let toolInfo;
|
|
119
71
|
if (request.toolCall.name === "fetch_skill") {
|
|
@@ -147,7 +99,7 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
147
99
|
result = yield handler(request);
|
|
148
100
|
}
|
|
149
101
|
else {
|
|
150
|
-
const enabledApiToolNames =
|
|
102
|
+
const enabledApiToolNames = getEnabledApiToolNames(request.state.messages);
|
|
151
103
|
if (enabledApiToolNames.has(request.toolCall.name)) {
|
|
152
104
|
result = yield handler(Object.assign(Object.assign({}, request), { tool: dynamicTools[request.toolCall.name] }));
|
|
153
105
|
}
|
|
@@ -160,9 +112,6 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
160
112
|
});
|
|
161
113
|
}
|
|
162
114
|
}
|
|
163
|
-
if (sessionId) {
|
|
164
|
-
rememberLoadedToolFromFetchResult(sessionId, result);
|
|
165
|
-
}
|
|
166
115
|
toolCallTracker.finishSuccess(result);
|
|
167
116
|
return result;
|
|
168
117
|
}
|