@@ -12,7 +12,34 @@ import { CodebuffConfig } from '@codebuff/common/json-config/constants'
1212
1313export let loadedAgents : Record < string , DynamicAgentTemplate > = { }
1414
15- const agentTemplatesSubdir = [ '.agents' , 'templates' ] as const
15+ const agentTemplatesSubdir = [ '.agents' ] as const
16+
17+ function getAllTsFiles ( dir : string ) : string [ ] {
18+ const files : string [ ] = [ ]
19+
20+ try {
21+ const entries = fs . readdirSync ( dir , { withFileTypes : true } )
22+
23+ for ( const entry of entries ) {
24+ const fullPath = path . join ( dir , entry . name )
25+
26+ if ( entry . isDirectory ( ) ) {
27+ // Recursively scan subdirectories
28+ files . push ( ...getAllTsFiles ( fullPath ) )
29+ } else if (
30+ entry . isFile ( ) &&
31+ entry . name . endsWith ( '.ts' ) &&
32+ ! entry . name . endsWith ( '.d.ts' )
33+ ) {
34+ files . push ( fullPath )
35+ }
36+ }
37+ } catch ( error ) {
38+ // Ignore errors reading directories
39+ }
40+
41+ return files
42+ }
1643
1744export async function loadLocalAgents ( {
1845 verbose = false ,
@@ -28,52 +55,54 @@ export async function loadLocalAgents({
2855 }
2956
3057 try {
31- const files = fs . readdirSync ( agentsDir )
58+ const tsFiles = getAllTsFiles ( agentsDir )
59+
60+ for ( const fullPath of tsFiles ) {
61+ const relativePath = path . relative ( agentsDir , fullPath )
62+ const fileName = relativePath . replace ( / \. t s $ / , '' ) . replace ( / [ / \\ ] / g, '-' )
3263
33- for ( const file of files ) {
34- const fullPath = path . join ( agentsDir , file )
3564 let agentConfig : any
36- if ( file . endsWith ( '.ts' ) ) {
37- let agentModule : any
38- try {
39- agentModule = await import ( fullPath )
40- } catch ( error : any ) {
41- if ( verbose ) {
42- console . error ( 'Error importing agent:' , error )
43- }
44- continue
45- }
46- try {
47- agentConfig = agentModule . default
48- } catch ( error : any ) {
49- console . error ( 'Error loading agent from file:' , fullPath , error )
50- continue
65+ let agentModule : any
66+ try {
67+ agentModule = await import ( fullPath )
68+ } catch ( error : any ) {
69+ if ( verbose ) {
70+ console . error ( 'Error importing agent:' , error )
5171 }
72+ continue
73+ }
74+ try {
75+ agentConfig = agentModule . default
76+ } catch ( error : any ) {
77+ console . error ( 'Error loading agent from file:' , fullPath , error )
78+ continue
79+ }
5280
53- let typedAgentConfig : DynamicAgentConfigParsed
54- try {
55- typedAgentConfig = DynamicAgentConfigSchema . parse ( agentConfig )
56- } catch ( error : any ) {
57- console . error ( 'Invalid agent format:' , fullPath , error )
58- continue
59- }
81+ if ( ! agentConfig ) continue
6082
61- // Convert handleSteps function to string if present
62- let handleStepsString : string | undefined
63- if ( agentConfig . handleSteps ) {
64- handleStepsString = agentConfig . handleSteps . toString ( )
65- }
83+ let typedAgentConfig : DynamicAgentConfigParsed
84+ try {
85+ typedAgentConfig = DynamicAgentConfigSchema . parse ( agentConfig )
86+ } catch ( error : any ) {
87+ console . error ( 'Invalid agent format:' , fullPath , error )
88+ continue
89+ }
90+
91+ // Convert handleSteps function to string if present
92+ let handleStepsString : string | undefined
93+ if ( agentConfig . handleSteps ) {
94+ handleStepsString = agentConfig . handleSteps . toString ( )
95+ }
6696
67- loadedAgents [ file . slice ( 0 , - '.ts' . length ) ] = {
68- ...typedAgentConfig ,
69- systemPrompt : loadFileContents ( typedAgentConfig . systemPrompt ) ,
70- instructionsPrompt : loadFileContents (
71- typedAgentConfig . instructionsPrompt
72- ) ,
73- stepPrompt : loadFileContents ( typedAgentConfig . stepPrompt ) ,
97+ loadedAgents [ fileName ] = {
98+ ...typedAgentConfig ,
99+ systemPrompt : loadFileContents ( typedAgentConfig . systemPrompt ) ,
100+ instructionsPrompt : loadFileContents (
101+ typedAgentConfig . instructionsPrompt
102+ ) ,
103+ stepPrompt : loadFileContents ( typedAgentConfig . stepPrompt ) ,
74104
75- handleSteps : handleStepsString ,
76- }
105+ handleSteps : handleStepsString ,
77106 }
78107 }
79108 } catch ( error ) { }
0 commit comments