11import { ToolProtocol } from '../tools/BaseTool.js' ;
22import { join , dirname } from 'path' ;
3- import { promises as fs } from 'fs' ;
3+ import { promises as fs , existsSync } from 'fs' ;
44import { logger } from '../core/Logger.js' ;
55import { discoverFilesRecursively , hasValidFiles } from '../utils/fileDiscovery.js' ;
66
@@ -9,23 +9,43 @@ export class ToolLoader {
99 private readonly EXCLUDED_FILES = [ 'BaseTool.js' , '*.test.js' , '*.spec.js' ] ;
1010
1111 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 ) {
1419 this . TOOLS_DIR = join ( basePath , 'tools' ) ;
20+ logger . debug ( `Using provided base path for tools: ${ this . TOOLS_DIR } ` ) ;
1521 } else {
16- // For backwards compatibility, use the old behavior with process.argv[1]
22+ // For backwards compatibility
1723 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 } ` ) ;
1932 }
20- logger . debug ( `Initialized ToolLoader with directory: ${ this . TOOLS_DIR } ` ) ;
2133 }
2234
2335 async hasTools ( ) : Promise < boolean > {
2436 try {
25- return await hasValidFiles ( this . TOOLS_DIR , {
37+ const hasDistTools = await hasValidFiles ( this . TOOLS_DIR , {
2638 extensions : [ '.js' ] ,
2739 excludePatterns : this . EXCLUDED_FILES ,
2840 } ) ;
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 ;
2949 } catch ( error ) {
3050 logger . debug ( `No tools directory found: ${ ( error as Error ) . message } ` ) ;
3151 return false ;
0 commit comments