@@ -49,6 +49,8 @@ export namespace Config {
4949 }
5050
5151 result . agent = result . agent || { }
52+ result . mode = result . mode || { }
53+ result . plugin = result . plugin || [ ]
5254
5355 const directories = [
5456 Global . Path . config ,
@@ -57,109 +59,14 @@ export namespace Config {
5759 ) ) ,
5860 ]
5961
60- const markdownAgents = [
61- ...( await Filesystem . globUp ( "agent/**/*.md" , Global . Path . config , Global . Path . config ) ) ,
62- ...( await Filesystem . globUp ( ".opencode/agent/**/*.md" , Instance . directory , Instance . worktree ) ) ,
63- ]
64- for ( const item of markdownAgents ) {
65- const content = await Bun . file ( item ) . text ( )
66- const md = matter ( content )
67- if ( ! md . data ) continue
68-
69- // Extract relative path from agent folder for nested agents
70- let agentName = path . basename ( item , ".md" )
71- const agentFolderPath = item . includes ( "/.opencode/agent/" )
72- ? item . split ( "/.opencode/agent/" ) [ 1 ]
73- : item . includes ( "/agent/" )
74- ? item . split ( "/agent/" ) [ 1 ]
75- : agentName + ".md"
76-
77- // If agent is in a subfolder, include folder path in name
78- if ( agentFolderPath . includes ( "/" ) ) {
79- const relativePath = agentFolderPath . replace ( ".md" , "" )
80- const pathParts = relativePath . split ( "/" )
81- agentName = pathParts . slice ( 0 , - 1 ) . join ( "/" ) + "/" + pathParts [ pathParts . length - 1 ]
82- }
83-
84- const config = {
85- name : agentName ,
86- ...md . data ,
87- prompt : md . content . trim ( ) ,
88- }
89- const parsed = Agent . safeParse ( config )
90- if ( parsed . success ) {
91- result . agent = mergeDeep ( result . agent , {
92- [ config . name ] : parsed . data ,
93- } )
94- continue
95- }
96- throw new InvalidError ( { path : item } , { cause : parsed . error } )
62+ for ( const dir of directories ) {
63+ await assertValid ( dir )
64+ result . command = mergeDeep ( result . command ?? { } , await loadCommand ( dir ) )
65+ result . agent = mergeDeep ( result . agent , await loadAgent ( dir ) )
66+ result . agent = mergeDeep ( result . agent , await loadMode ( dir ) )
67+ result . plugin . push ( ...( await loadPlugin ( dir ) ) )
9768 }
9869
99- // Load mode markdown files
100- result . mode = result . mode || { }
101- const markdownModes = [
102- ...( await Filesystem . globUp ( "mode/*.md" , Global . Path . config , Global . Path . config ) ) ,
103- ...( await Filesystem . globUp ( ".opencode/mode/*.md" , Instance . directory , Instance . worktree ) ) ,
104- ]
105- for ( const item of markdownModes ) {
106- const content = await Bun . file ( item ) . text ( )
107- const md = matter ( content )
108- if ( ! md . data ) continue
109-
110- const config = {
111- name : path . basename ( item , ".md" ) ,
112- ...md . data ,
113- prompt : md . content . trim ( ) ,
114- }
115- const parsed = Agent . safeParse ( config )
116- if ( parsed . success ) {
117- result . agent = mergeDeep ( result . mode , {
118- [ config . name ] : {
119- ...parsed . data ,
120- mode : "primary" as const ,
121- } ,
122- } )
123- continue
124- }
125- }
126-
127- // Load command markdown files
128- result . command = result . command || { }
129- const markdownCommands = [
130- ...( await Filesystem . globUp ( "command/**/*.md" , Global . Path . config , Global . Path . config ) ) ,
131- ...( await Filesystem . globUp ( ".opencode/command/**/*.md" , Instance . directory , Instance . worktree ) ) ,
132- ]
133- for ( const item of markdownCommands ) {
134- const content = await Bun . file ( item ) . text ( )
135- const md = matter ( content )
136- if ( ! md . data ) continue
137-
138- const name = ( ( ) => {
139- const patterns = [ "/.opencode/command/" , "/command/" ]
140- const pattern = patterns . find ( ( p ) => item . includes ( p ) )
141-
142- if ( pattern ) {
143- const index = item . indexOf ( pattern )
144- return item . slice ( index + pattern . length , - 3 )
145- }
146- return path . basename ( item , ".md" )
147- } ) ( )
148-
149- const config = {
150- name,
151- ...md . data ,
152- template : md . content . trim ( ) ,
153- }
154- const parsed = Command . safeParse ( config )
155- if ( parsed . success ) {
156- result . command = mergeDeep ( result . command , {
157- [ config . name ] : parsed . data ,
158- } )
159- continue
160- }
161- throw new InvalidError ( { path : item } , { cause : parsed . error } )
162- }
16370 // Migrate deprecated mode field to agent field
16471 for ( const [ name , mode ] of Object . entries ( result . mode ) ) {
16572 result . agent = mergeDeep ( result . agent ?? { } , {
@@ -170,14 +77,6 @@ export namespace Config {
17077 } )
17178 }
17279
173- result . plugin = result . plugin || [ ]
174- result . plugin . push (
175- ...[
176- ...( await Filesystem . globUp ( "plugin/*.{ts,js}" , Global . Path . config , Global . Path . config ) ) ,
177- ...( await Filesystem . globUp ( ".opencode/plugin/*.{ts,js}" , Instance . directory , Instance . worktree ) ) ,
178- ] . map ( ( x ) => "file://" + x ) ,
179- )
180-
18180 if ( Flag . OPENCODE_PERMISSION ) {
18281 result . permission = mergeDeep ( result . permission ?? { } , JSON . parse ( Flag . OPENCODE_PERMISSION ) )
18382 }
@@ -218,6 +117,128 @@ export namespace Config {
218117 }
219118 } )
220119
120+ async function assertValid ( dir : string ) {
121+ const ALLOWED_DIRS = new Set ( [ "agent" , "command" , "mode" , "plugin" ] )
122+ const UNEXPECTED_DIR_GLOB = new Bun . Glob ( "*/" )
123+ for await ( const item of UNEXPECTED_DIR_GLOB . scan ( { absolute : true , cwd : dir , onlyFiles : false } ) ) {
124+ const dirname = path . basename ( item )
125+ if ( ! ALLOWED_DIRS . has ( dirname ) ) {
126+ throw new InvalidError ( {
127+ path : dir ,
128+ message : `Unexpected directory "${ dirname } " found in "${ dir } ". Only ${ ALLOWED_DIRS . values ( ) . toArray ( ) . join ( ", " ) } directories are allowed.` ,
129+ } )
130+ }
131+ }
132+ }
133+
134+ const COMMAND_GLOB = new Bun . Glob ( "command/**/*.md" )
135+ async function loadCommand ( dir : string ) {
136+ const result : Record < string , Command > = { }
137+ for await ( const item of COMMAND_GLOB . scan ( { absolute : true , cwd : dir } ) ) {
138+ const content = await Bun . file ( item ) . text ( )
139+ const md = matter ( content )
140+ if ( ! md . data ) continue
141+
142+ const name = ( ( ) => {
143+ const patterns = [ "/.opencode/command/" , "/command/" ]
144+ const pattern = patterns . find ( ( p ) => item . includes ( p ) )
145+
146+ if ( pattern ) {
147+ const index = item . indexOf ( pattern )
148+ return item . slice ( index + pattern . length , - 3 )
149+ }
150+ return path . basename ( item , ".md" )
151+ } ) ( )
152+
153+ const config = {
154+ name,
155+ ...md . data ,
156+ template : md . content . trim ( ) ,
157+ }
158+ const parsed = Command . safeParse ( config )
159+ if ( parsed . success ) {
160+ result [ config . name ] = parsed . data
161+ continue
162+ }
163+ throw new InvalidError ( { path : item } , { cause : parsed . error } )
164+ }
165+ return result
166+ }
167+
168+ const AGENT_GLOB = new Bun . Glob ( "agent/**/*.md" )
169+ async function loadAgent ( dir : string ) {
170+ const result : Record < string , Agent > = { }
171+
172+ for await ( const item of AGENT_GLOB . scan ( { absolute : true , cwd : dir } ) ) {
173+ const content = await Bun . file ( item ) . text ( )
174+ const md = matter ( content )
175+ if ( ! md . data ) continue
176+
177+ // Extract relative path from agent folder for nested agents
178+ let agentName = path . basename ( item , ".md" )
179+ const agentFolderPath = item . includes ( "/.opencode/agent/" )
180+ ? item . split ( "/.opencode/agent/" ) [ 1 ]
181+ : item . includes ( "/agent/" )
182+ ? item . split ( "/agent/" ) [ 1 ]
183+ : agentName + ".md"
184+
185+ // If agent is in a subfolder, include folder path in name
186+ if ( agentFolderPath . includes ( "/" ) ) {
187+ const relativePath = agentFolderPath . replace ( ".md" , "" )
188+ const pathParts = relativePath . split ( "/" )
189+ agentName = pathParts . slice ( 0 , - 1 ) . join ( "/" ) + "/" + pathParts [ pathParts . length - 1 ]
190+ }
191+
192+ const config = {
193+ name : agentName ,
194+ ...md . data ,
195+ prompt : md . content . trim ( ) ,
196+ }
197+ const parsed = Agent . safeParse ( config )
198+ if ( parsed . success ) {
199+ result [ config . name ] = parsed . data
200+ continue
201+ }
202+ throw new InvalidError ( { path : item } , { cause : parsed . error } )
203+ }
204+ return result
205+ }
206+
207+ const MODE_GLOB = new Bun . Glob ( "mode/*.md" )
208+ async function loadMode ( dir : string ) {
209+ const result : Record < string , Agent > = { }
210+ for await ( const item of MODE_GLOB . scan ( { absolute : true , cwd : dir } ) ) {
211+ const content = await Bun . file ( item ) . text ( )
212+ const md = matter ( content )
213+ if ( ! md . data ) continue
214+
215+ const config = {
216+ name : path . basename ( item , ".md" ) ,
217+ ...md . data ,
218+ prompt : md . content . trim ( ) ,
219+ }
220+ const parsed = Agent . safeParse ( config )
221+ if ( parsed . success ) {
222+ result [ config . name ] = {
223+ ...parsed . data ,
224+ mode : "primary" as const ,
225+ }
226+ continue
227+ }
228+ }
229+ return result
230+ }
231+
232+ const PLUGIN_GLOB = new Bun . Glob ( "plugin/*.{ts,js}" )
233+ async function loadPlugin ( dir : string ) {
234+ const plugins : string [ ] = [ ]
235+
236+ for await ( const item of PLUGIN_GLOB . scan ( { absolute : true , cwd : dir } ) ) {
237+ plugins . push ( "file://" + item )
238+ }
239+ return plugins
240+ }
241+
221242 export const McpLocal = z
222243 . object ( {
223244 type : z . literal ( "local" ) . describe ( "Type of MCP server connection" ) ,
0 commit comments