1
1
import { ToolProtocol } from '../tools/BaseTool.js' ;
2
2
import { join , dirname } from 'path' ;
3
- import { promises as fs } from 'fs' ;
3
+ import { promises as fs , existsSync } from 'fs' ;
4
4
import { logger } from '../core/Logger.js' ;
5
5
import { discoverFilesRecursively , hasValidFiles } from '../utils/fileDiscovery.js' ;
6
6
@@ -9,23 +9,43 @@ export class ToolLoader {
9
9
private readonly EXCLUDED_FILES = [ 'BaseTool.js' , '*.test.js' , '*.spec.js' ] ;
10
10
11
11
constructor ( basePath ?: string ) {
12
- if ( basePath ) {
13
- // If basePath is provided, it should be the directory containing the tools folder
12
+ const projectRoot = process . cwd ( ) ;
13
+ const distToolsPath = join ( projectRoot , 'dist' , 'tools' ) ;
14
+
15
+ if ( existsSync ( distToolsPath ) ) {
16
+ this . TOOLS_DIR = distToolsPath ;
17
+ logger . debug ( `Using project's dist/tools directory: ${ this . TOOLS_DIR } ` ) ;
18
+ } else if ( basePath ) {
14
19
this . TOOLS_DIR = join ( basePath , 'tools' ) ;
20
+ logger . debug ( `Using provided base path for tools: ${ this . TOOLS_DIR } ` ) ;
15
21
} else {
16
- // For backwards compatibility, use the old behavior with process.argv[1]
22
+ // For backwards compatibility
17
23
const mainModulePath = process . argv [ 1 ] ;
18
- this . TOOLS_DIR = join ( dirname ( mainModulePath ) , 'tools' ) ;
24
+ const moduleDir = dirname ( mainModulePath ) ;
25
+
26
+ if ( moduleDir . endsWith ( 'dist' ) ) {
27
+ this . TOOLS_DIR = join ( moduleDir , 'tools' ) ;
28
+ } else {
29
+ this . TOOLS_DIR = join ( moduleDir , 'dist' , 'tools' ) ;
30
+ }
31
+ logger . debug ( `Using module path for tools: ${ this . TOOLS_DIR } ` ) ;
19
32
}
20
- logger . debug ( `Initialized ToolLoader with directory: ${ this . TOOLS_DIR } ` ) ;
21
33
}
22
34
23
35
async hasTools ( ) : Promise < boolean > {
24
36
try {
25
- return await hasValidFiles ( this . TOOLS_DIR , {
37
+ const hasDistTools = await hasValidFiles ( this . TOOLS_DIR , {
26
38
extensions : [ '.js' ] ,
27
39
excludePatterns : this . EXCLUDED_FILES ,
28
40
} ) ;
41
+
42
+ if ( hasDistTools ) {
43
+ logger . debug ( `Found tools in ${ this . TOOLS_DIR } ` ) ;
44
+ return true ;
45
+ }
46
+
47
+ logger . debug ( `No tools found in ${ this . TOOLS_DIR } ` ) ;
48
+ return false ;
29
49
} catch ( error ) {
30
50
logger . debug ( `No tools directory found: ${ ( error as Error ) . message } ` ) ;
31
51
return false ;
0 commit comments