@@ -2,6 +2,7 @@ import { BaseTool } from "../tools/BaseTool.js";
2
2
import { fileURLToPath } from "url" ;
3
3
import { dirname , join } from "path" ;
4
4
import { promises as fs } from "fs" ;
5
+ import { cwd } from "process" ;
5
6
6
7
export interface ToolLoaderOptions {
7
8
toolsDir ?: string ;
@@ -18,9 +19,8 @@ export class ToolLoader {
18
19
}
19
20
20
21
private findDefaultToolsDir ( ) : string {
21
- const currentFilePath = fileURLToPath ( import . meta. url ) ;
22
- const currentDir = dirname ( currentFilePath ) ;
23
- return join ( currentDir , ".." , ".." , "dist" , "tools" ) ;
22
+ // Use current working directory + dist/tools as default
23
+ return join ( cwd ( ) , "dist" , "tools" ) ;
24
24
}
25
25
26
26
private isToolFile ( file : string ) : boolean {
@@ -45,30 +45,45 @@ export class ToolLoader {
45
45
46
46
async loadTools ( ) : Promise < BaseTool [ ] > {
47
47
try {
48
+ console . log ( `Loading tools from directory: ${ this . toolsDir } ` ) ;
48
49
const files = await fs . readdir ( this . toolsDir ) ;
50
+ console . log ( `Found files: ${ files . join ( ", " ) } ` ) ;
49
51
50
52
const toolPromises = files
51
53
. filter ( ( file ) => this . isToolFile ( file ) )
52
54
. map ( async ( file ) => {
53
55
try {
54
- const modulePath = `file://${ join ( this . toolsDir , file ) } ` ;
55
- const { default : ToolClass } = await import ( modulePath ) ;
56
+ const fullPath = join ( this . toolsDir , file ) ;
57
+ console . log ( `Loading tool from: ${ fullPath } ` ) ;
58
+ const { default : ToolClass } = await import ( `file://${ fullPath } ` ) ;
56
59
57
- if ( ! ToolClass ) return null ;
60
+ if ( ! ToolClass ) {
61
+ console . log ( `No default export found in ${ file } ` ) ;
62
+ return null ;
63
+ }
58
64
59
65
const tool = new ToolClass ( ) ;
60
- return this . validateTool ( tool ) ? tool : null ;
61
- } catch {
66
+ if ( this . validateTool ( tool ) ) {
67
+ console . log ( `Successfully loaded tool: ${ tool . name } ` ) ;
68
+ return tool ;
69
+ }
70
+ console . log ( `Invalid tool found in ${ file } ` ) ;
71
+ return null ;
72
+ } catch ( error ) {
73
+ console . error ( `Error loading tool ${ file } :` , error ) ;
62
74
return null ;
63
75
}
64
76
} ) ;
65
77
66
78
const tools = ( await Promise . all ( toolPromises ) ) . filter (
67
79
Boolean
68
80
) as BaseTool [ ] ;
81
+ console . log (
82
+ `Loaded ${ tools . length } tools: ${ tools . map ( ( t ) => t . name ) . join ( ", " ) } `
83
+ ) ;
69
84
return tools ;
70
85
} catch ( error ) {
71
- console . error ( `Failed to load tools from ${ this . toolsDir } ` ) ;
86
+ console . error ( `Failed to load tools from ${ this . toolsDir } :` , error ) ;
72
87
return [ ] ;
73
88
}
74
89
}
0 commit comments