@@ -47,70 +47,6 @@ function getEnabledApiToolNames(messages: unknown[]) {
4747 return enabledToolNames ;
4848}
4949
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-
11450export function createApiBasedToolsMiddleware (
11551 apiBasedTools : Record < string , ApiBasedTool > ,
11652 adminforth : IAdminForth ,
@@ -126,11 +62,7 @@ export function createApiBasedToolsMiddleware(
12662 return createMiddleware ( {
12763 name : "ApiBasedToolsMiddleware" ,
12864 async wrapModelCall ( request , handler ) {
129- const { sessionId } = request . runtime . context as { sessionId ?: string } ;
130- const enabledApiToolNames = getEnabledApiToolNamesForSession (
131- request . state . messages ,
132- sessionId ,
133- ) ;
65+ const enabledApiToolNames = getEnabledApiToolNames ( request . state . messages ) ;
13466 const tools = [ ...enabledApiToolNames ]
13567 . filter ( ( toolName ) => ! alwaysAvailableApiToolNames . has ( toolName ) )
13668 . map ( ( toolName ) => dynamicTools [ toolName ] ) ;
@@ -148,10 +80,9 @@ export function createApiBasedToolsMiddleware(
14880 async wrapToolCall ( request , handler ) {
14981 const startedAt = Date . now ( ) ;
15082 const toolInput = JSON . stringify ( request . toolCall . args ?? { } ) ;
151- const { adminUser, emitToolCallEvent, sessionId , userTimeZone } = request . runtime . context as {
83+ const { adminUser, emitToolCallEvent, userTimeZone } = request . runtime . context as {
15284 adminUser : AdminUser ;
15385 emitToolCallEvent : ToolCallEventSink ;
154- sessionId : string ;
15586 userTimeZone : string ;
15687 } ;
15788 const toolArgs = ( request . toolCall . args ?? { } ) as Record < string , unknown > ;
@@ -189,10 +120,7 @@ export function createApiBasedToolsMiddleware(
189120 if ( request . tool ) {
190121 result = await handler ( request ) ;
191122 } else {
192- const enabledApiToolNames = getEnabledApiToolNamesForSession (
193- request . state . messages ,
194- sessionId ,
195- ) ;
123+ const enabledApiToolNames = getEnabledApiToolNames ( request . state . messages ) ;
196124
197125 if ( enabledApiToolNames . has ( request . toolCall . name ) ) {
198126 result = await handler ( {
@@ -209,10 +137,6 @@ export function createApiBasedToolsMiddleware(
209137 }
210138 }
211139
212- if ( sessionId ) {
213- rememberLoadedToolFromFetchResult ( sessionId , result ) ;
214- }
215-
216140 toolCallTracker . finishSuccess ( result ) ;
217141 return result ;
218142 } catch ( error ) {
0 commit comments