3
3
* This eliminates duplication between stdio.ts and processParamsGetTools.
4
4
*/
5
5
6
+ import type { ValidateFunction } from 'ajv' ;
6
7
import type { ApifyClient } from 'apify' ;
7
8
8
9
import log from '@apify/log' ;
@@ -12,7 +13,7 @@ import { callActor } from '../tools/actor.js';
12
13
import { getActorOutput } from '../tools/get-actor-output.js' ;
13
14
import { addTool } from '../tools/helpers.js' ;
14
15
import { getActorsAsTools , toolCategories , toolCategoriesEnabledByDefault } from '../tools/index.js' ;
15
- import type { Input , ToolCategory , ToolEntry } from '../types.js' ;
16
+ import type { Input , InternalTool , InternalToolArgs , ToolCategory , ToolEntry } from '../types.js' ;
16
17
import { getExpectedToolsByCategories } from './tools.js' ;
17
18
18
19
// Lazily-computed cache of internal tools by name to avoid circular init issues.
@@ -139,5 +140,37 @@ export async function loadToolsFromInput(
139
140
140
141
// De-duplicate by tool name for safety
141
142
const seen = new Set < string > ( ) ;
142
- return result . filter ( ( entry ) => ! seen . has ( entry . tool . name ) && seen . add ( entry . tool . name ) ) ;
143
+ const filtered = result . filter ( ( entry ) => ! seen . has ( entry . tool . name ) && seen . add ( entry . tool . name ) ) ;
144
+
145
+ // TODO: rework this solition as it was quickly hacked together for hotfix
146
+ // Deep clone except ajvValidate and call functions
147
+
148
+ // holds the original functions of the tools
149
+ const toolFunctions = new Map < string , { ajvValidate ?: ValidateFunction < unknown > ; call ?:( args : InternalToolArgs ) => Promise < object > } > ( ) ;
150
+ for ( const entry of filtered ) {
151
+ if ( entry . type === 'internal' ) {
152
+ toolFunctions . set ( entry . tool . name , { ajvValidate : entry . tool . ajvValidate , call : ( entry . tool as InternalTool ) . call } ) ;
153
+ } else {
154
+ toolFunctions . set ( entry . tool . name , { ajvValidate : entry . tool . ajvValidate } ) ;
155
+ }
156
+ }
157
+
158
+ const cloned = JSON . parse ( JSON . stringify ( filtered , ( key , value ) => {
159
+ if ( key === 'ajvValidate' || key === 'call' ) return undefined ;
160
+ return value ;
161
+ } ) ) as ToolEntry [ ] ;
162
+
163
+ // restore the original functions
164
+ for ( const entry of cloned ) {
165
+ const funcs = toolFunctions . get ( entry . tool . name ) ;
166
+ if ( funcs ) {
167
+ if ( funcs . ajvValidate ) {
168
+ entry . tool . ajvValidate = funcs . ajvValidate ;
169
+ }
170
+ if ( entry . type === 'internal' && funcs . call ) {
171
+ ( entry . tool as InternalTool ) . call = funcs . call ;
172
+ }
173
+ }
174
+ }
175
+ return cloned ;
143
176
}
0 commit comments