11import { BaseTool } from "../tools/BaseTool.js" ;
2- import { dirname , join } from "path" ;
2+ import { join , dirname } from "path" ;
33import { promises as fs } from "fs" ;
4- import { statSync } from "fs" ;
5- import { fileURLToPath } from "url" ;
6- import { cwd } from "process" ;
74import { logger } from "./Logger.js" ;
85
9- export interface ToolLoaderOptions {
10- toolsDir ?: string ;
11- exclude ?: string [ ] ;
12- }
13-
146export class ToolLoader {
15- private toolsDir : string ;
16- private exclude : string [ ] ;
7+ private readonly TOOLS_DIR : string ;
8+ private readonly EXCLUDED_FILES = [ "BaseTool.js" , "*.test.js" , "*.spec.js" ] ;
179
18- constructor ( options : ToolLoaderOptions = { } ) {
19- this . exclude = options . exclude || [ "BaseTool.js" , "*.test.js" , "*.spec.js" ] ;
20- this . toolsDir = this . resolveToolsDir ( options . toolsDir ) ;
21- logger . debug ( `Initialized ToolLoader with directory: ${ this . toolsDir } ` ) ;
22- }
23-
24- private resolveToolsDir ( toolsDir ?: string ) : string {
25- if ( toolsDir ) {
26- logger . debug ( `Using provided tools directory: ${ toolsDir } ` ) ;
27- return toolsDir ;
28- }
29-
30- const currentFilePath = fileURLToPath ( import . meta. url ) ;
31- const currentDir = dirname ( currentFilePath ) ;
32- const possiblePaths = [
33- join ( currentDir , ".." , "tools" ) ,
34- join ( currentDir , ".." , ".." , "tools" ) ,
35- join ( cwd ( ) , "dist" , "tools" ) ,
36- join ( cwd ( ) , "build" , "tools" ) ,
37- join ( cwd ( ) , "tools" ) ,
38- ] ;
39-
40- logger . debug (
41- `Searching for tools in possible paths:\n${ possiblePaths . join ( "\n" ) } `
42- ) ;
43-
44- for ( const path of possiblePaths ) {
45- try {
46- if ( statSync ( path ) . isDirectory ( ) ) {
47- logger . debug ( `Found existing tools directory: ${ path } ` ) ;
48- return path ;
49- }
50- } catch ( e ) {
51- logger . debug ( `Path ${ path } not accessible` ) ;
52- }
53- }
54-
55- const fallbackPath = join ( cwd ( ) , "dist" , "tools" ) ;
56- logger . debug (
57- `No valid tools directory found, falling back to: ${ fallbackPath } `
58- ) ;
59- return fallbackPath ;
10+ constructor ( ) {
11+ const mainModulePath = process . argv [ 1 ] ;
12+ this . TOOLS_DIR = join ( dirname ( mainModulePath ) , "tools" ) ;
13+ logger . debug ( `Initialized ToolLoader with directory: ${ this . TOOLS_DIR } ` ) ;
6014 }
6115
6216 private isToolFile ( file : string ) : boolean {
6317 if ( ! file . endsWith ( ".js" ) ) return false ;
64- const isExcluded = this . exclude . some ( ( pattern ) => {
18+ const isExcluded = this . EXCLUDED_FILES . some ( ( pattern ) => {
6519 if ( pattern . includes ( "*" ) ) {
6620 const regex = new RegExp ( pattern . replace ( "*" , ".*" ) ) ;
6721 return regex . test ( file ) ;
@@ -94,22 +48,22 @@ export class ToolLoader {
9448
9549 async loadTools ( ) : Promise < BaseTool [ ] > {
9650 try {
97- logger . debug ( `Attempting to load tools from: ${ this . toolsDir } ` ) ;
51+ logger . debug ( `Attempting to load tools from: ${ this . TOOLS_DIR } ` ) ;
9852
9953 let stats ;
10054 try {
101- stats = await fs . stat ( this . toolsDir ) ;
55+ stats = await fs . stat ( this . TOOLS_DIR ) ;
10256 } catch ( error ) {
10357 logger . error ( `Error accessing tools directory: ${ error } ` ) ;
10458 return [ ] ;
10559 }
10660
10761 if ( ! stats . isDirectory ( ) ) {
108- logger . error ( `Path is not a directory: ${ this . toolsDir } ` ) ;
62+ logger . error ( `Path is not a directory: ${ this . TOOLS_DIR } ` ) ;
10963 return [ ] ;
11064 }
11165
112- const files = await fs . readdir ( this . toolsDir ) ;
66+ const files = await fs . readdir ( this . TOOLS_DIR ) ;
11367 logger . debug ( `Found files in directory: ${ files . join ( ", " ) } ` ) ;
11468
11569 const tools : BaseTool [ ] = [ ] ;
@@ -120,7 +74,7 @@ export class ToolLoader {
12074 }
12175
12276 try {
123- const fullPath = join ( this . toolsDir , file ) ;
77+ const fullPath = join ( this . TOOLS_DIR , file ) ;
12478 logger . debug ( `Attempting to load tool from: ${ fullPath } ` ) ;
12579
12680 const importPath = `file://${ fullPath } ` ;
0 commit comments