1
1
import { BaseTool } from "../tools/BaseTool.js" ;
2
- import { dirname , join } from "path" ;
2
+ import { join , dirname } from "path" ;
3
3
import { promises as fs } from "fs" ;
4
- import { statSync } from "fs" ;
5
- import { fileURLToPath } from "url" ;
6
- import { cwd } from "process" ;
7
4
import { logger } from "./Logger.js" ;
8
5
9
- export interface ToolLoaderOptions {
10
- toolsDir ?: string ;
11
- exclude ?: string [ ] ;
12
- }
13
-
14
6
export 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" ] ;
17
9
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 } ` ) ;
60
14
}
61
15
62
16
private isToolFile ( file : string ) : boolean {
63
17
if ( ! file . endsWith ( ".js" ) ) return false ;
64
- const isExcluded = this . exclude . some ( ( pattern ) => {
18
+ const isExcluded = this . EXCLUDED_FILES . some ( ( pattern ) => {
65
19
if ( pattern . includes ( "*" ) ) {
66
20
const regex = new RegExp ( pattern . replace ( "*" , ".*" ) ) ;
67
21
return regex . test ( file ) ;
@@ -94,22 +48,22 @@ export class ToolLoader {
94
48
95
49
async loadTools ( ) : Promise < BaseTool [ ] > {
96
50
try {
97
- logger . debug ( `Attempting to load tools from: ${ this . toolsDir } ` ) ;
51
+ logger . debug ( `Attempting to load tools from: ${ this . TOOLS_DIR } ` ) ;
98
52
99
53
let stats ;
100
54
try {
101
- stats = await fs . stat ( this . toolsDir ) ;
55
+ stats = await fs . stat ( this . TOOLS_DIR ) ;
102
56
} catch ( error ) {
103
57
logger . error ( `Error accessing tools directory: ${ error } ` ) ;
104
58
return [ ] ;
105
59
}
106
60
107
61
if ( ! stats . isDirectory ( ) ) {
108
- logger . error ( `Path is not a directory: ${ this . toolsDir } ` ) ;
62
+ logger . error ( `Path is not a directory: ${ this . TOOLS_DIR } ` ) ;
109
63
return [ ] ;
110
64
}
111
65
112
- const files = await fs . readdir ( this . toolsDir ) ;
66
+ const files = await fs . readdir ( this . TOOLS_DIR ) ;
113
67
logger . debug ( `Found files in directory: ${ files . join ( ", " ) } ` ) ;
114
68
115
69
const tools : BaseTool [ ] = [ ] ;
@@ -120,7 +74,7 @@ export class ToolLoader {
120
74
}
121
75
122
76
try {
123
- const fullPath = join ( this . toolsDir , file ) ;
77
+ const fullPath = join ( this . TOOLS_DIR , file ) ;
124
78
logger . debug ( `Attempting to load tool from: ${ fullPath } ` ) ;
125
79
126
80
const importPath = `file://${ fullPath } ` ;
0 commit comments