@adminforth/agent 1.48.1 → 1.49.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.
|
@@ -47,6 +47,70 @@ 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
|
+
|
|
50
114
|
export function createApiBasedToolsMiddleware(
|
|
51
115
|
apiBasedTools: Record<string, ApiBasedTool>,
|
|
52
116
|
adminforth: IAdminForth,
|
|
@@ -62,7 +126,11 @@ export function createApiBasedToolsMiddleware(
|
|
|
62
126
|
return createMiddleware({
|
|
63
127
|
name: "ApiBasedToolsMiddleware",
|
|
64
128
|
async wrapModelCall(request, handler) {
|
|
65
|
-
const
|
|
129
|
+
const { sessionId } = request.runtime.context as { sessionId?: string };
|
|
130
|
+
const enabledApiToolNames = getEnabledApiToolNamesForSession(
|
|
131
|
+
request.state.messages,
|
|
132
|
+
sessionId,
|
|
133
|
+
);
|
|
66
134
|
const tools = [...enabledApiToolNames]
|
|
67
135
|
.filter((toolName) => !alwaysAvailableApiToolNames.has(toolName))
|
|
68
136
|
.map((toolName) => dynamicTools[toolName]);
|
|
@@ -80,9 +148,10 @@ export function createApiBasedToolsMiddleware(
|
|
|
80
148
|
async wrapToolCall(request, handler) {
|
|
81
149
|
const startedAt = Date.now();
|
|
82
150
|
const toolInput = JSON.stringify(request.toolCall.args ?? {});
|
|
83
|
-
const { adminUser, emitToolCallEvent, userTimeZone } = request.runtime.context as {
|
|
151
|
+
const { adminUser, emitToolCallEvent, sessionId, userTimeZone } = request.runtime.context as {
|
|
84
152
|
adminUser: AdminUser;
|
|
85
153
|
emitToolCallEvent: ToolCallEventSink;
|
|
154
|
+
sessionId: string;
|
|
86
155
|
userTimeZone: string;
|
|
87
156
|
};
|
|
88
157
|
const toolArgs = (request.toolCall.args ?? {}) as Record<string, unknown>;
|
|
@@ -120,7 +189,10 @@ export function createApiBasedToolsMiddleware(
|
|
|
120
189
|
if (request.tool) {
|
|
121
190
|
result = await handler(request);
|
|
122
191
|
} else {
|
|
123
|
-
const enabledApiToolNames =
|
|
192
|
+
const enabledApiToolNames = getEnabledApiToolNamesForSession(
|
|
193
|
+
request.state.messages,
|
|
194
|
+
sessionId,
|
|
195
|
+
);
|
|
124
196
|
|
|
125
197
|
if (enabledApiToolNames.has(request.toolCall.name)) {
|
|
126
198
|
result = await handler({
|
|
@@ -137,6 +209,10 @@ export function createApiBasedToolsMiddleware(
|
|
|
137
209
|
}
|
|
138
210
|
}
|
|
139
211
|
|
|
212
|
+
if (sessionId) {
|
|
213
|
+
rememberLoadedToolFromFetchResult(sessionId, result);
|
|
214
|
+
}
|
|
215
|
+
|
|
140
216
|
toolCallTracker.finishSuccess(result);
|
|
141
217
|
return result;
|
|
142
218
|
} catch (error) {
|
package/build.log
CHANGED
|
@@ -62,5 +62,5 @@ custom/speech_recognition_frontend/voiceActivityDetection.ts
|
|
|
62
62
|
custom/speech_recognition_frontend/types/
|
|
63
63
|
custom/speech_recognition_frontend/types/voice-activity-detection.d.ts
|
|
64
64
|
|
|
65
|
-
sent 1,668,700 bytes received
|
|
65
|
+
sent 1,668,700 bytes received 921 bytes 3,339,242.00 bytes/sec
|
|
66
66
|
total size is 1,664,547 speedup is 1.00
|
|
@@ -41,6 +41,53 @@ 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
|
+
}
|
|
44
91
|
export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
45
92
|
const alwaysAvailableApiToolNames = new Set(ALWAYS_AVAILABLE_API_TOOL_NAMES);
|
|
46
93
|
const dynamicTools = Object.fromEntries(Object.entries(apiBasedTools).map(([toolName, apiBasedTool]) => [
|
|
@@ -51,7 +98,8 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
51
98
|
name: "ApiBasedToolsMiddleware",
|
|
52
99
|
wrapModelCall(request, handler) {
|
|
53
100
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
const
|
|
101
|
+
const { sessionId } = request.runtime.context;
|
|
102
|
+
const enabledApiToolNames = getEnabledApiToolNamesForSession(request.state.messages, sessionId);
|
|
55
103
|
const tools = [...enabledApiToolNames]
|
|
56
104
|
.filter((toolName) => !alwaysAvailableApiToolNames.has(toolName))
|
|
57
105
|
.map((toolName) => dynamicTools[toolName]);
|
|
@@ -65,7 +113,7 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
65
113
|
var _a, _b, _c, _d;
|
|
66
114
|
const startedAt = Date.now();
|
|
67
115
|
const toolInput = JSON.stringify((_a = request.toolCall.args) !== null && _a !== void 0 ? _a : {});
|
|
68
|
-
const { adminUser, emitToolCallEvent, userTimeZone } = request.runtime.context;
|
|
116
|
+
const { adminUser, emitToolCallEvent, sessionId, userTimeZone } = request.runtime.context;
|
|
69
117
|
const toolArgs = ((_b = request.toolCall.args) !== null && _b !== void 0 ? _b : {});
|
|
70
118
|
let toolInfo;
|
|
71
119
|
if (request.toolCall.name === "fetch_skill") {
|
|
@@ -99,7 +147,7 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
99
147
|
result = yield handler(request);
|
|
100
148
|
}
|
|
101
149
|
else {
|
|
102
|
-
const enabledApiToolNames =
|
|
150
|
+
const enabledApiToolNames = getEnabledApiToolNamesForSession(request.state.messages, sessionId);
|
|
103
151
|
if (enabledApiToolNames.has(request.toolCall.name)) {
|
|
104
152
|
result = yield handler(Object.assign(Object.assign({}, request), { tool: dynamicTools[request.toolCall.name] }));
|
|
105
153
|
}
|
|
@@ -112,6 +160,9 @@ export function createApiBasedToolsMiddleware(apiBasedTools, adminforth) {
|
|
|
112
160
|
});
|
|
113
161
|
}
|
|
114
162
|
}
|
|
163
|
+
if (sessionId) {
|
|
164
|
+
rememberLoadedToolFromFetchResult(sessionId, result);
|
|
165
|
+
}
|
|
115
166
|
toolCallTracker.finishSuccess(result);
|
|
116
167
|
return result;
|
|
117
168
|
}
|