@@ -8,6 +8,7 @@ import { Flag } from "@/flag/flag"
88import { Log } from "../util/log"
99import { Glob } from "../util/glob"
1010import type { MessageV2 } from "./message-v2"
11+ import type { MessageID } from "./schema"
1112import { Effect , Layer , ServiceMap } from "effect"
1213
1314const log = Log . create ( { service : "instruction" } )
@@ -115,6 +116,28 @@ export namespace InstructionPrompt {
115116 return paths
116117 }
117118
119+ export async function system ( ) {
120+ const config = await Config . get ( )
121+ const paths = await systemPaths ( )
122+
123+ const files = Array . from ( paths ) . map ( async ( p ) => {
124+ const content = await Filesystem . readText ( p ) . catch ( ( ) => "" )
125+ if ( ! content ) return ""
126+ return "Instructions from: " + p + "\n" + content
127+ } )
128+
129+ const urls =
130+ config . instructions ?. filter ( ( item ) => item . startsWith ( "https://" ) || item . startsWith ( "http://" ) ) ?? [ ]
131+ const fetches = urls . map ( ( url ) =>
132+ fetch ( url , { signal : AbortSignal . timeout ( 5000 ) } )
133+ . then ( ( res ) => ( res . ok ? res . text ( ) : "" ) )
134+ . catch ( ( ) => "" )
135+ . then ( ( x ) => ( x ? "Instructions from: " + url + "\n" + x : "" ) ) ,
136+ )
137+
138+ return Promise . all ( [ ...files , ...fetches ] ) . then ( ( result ) => result . filter ( Boolean ) )
139+ }
140+
118141 export function loaded ( messages : MessageV2 . WithParts [ ] ) {
119142 const paths = new Set < string > ( )
120143 for ( const msg of messages ) {
@@ -132,8 +155,35 @@ export namespace InstructionPrompt {
132155 return paths
133156 }
134157
158+ export async function find ( dir : string ) {
159+ for ( const file of FILES ) {
160+ const filepath = path . resolve ( path . join ( dir , file ) )
161+ if ( await Filesystem . exists ( filepath ) ) return filepath
162+ }
163+ }
164+
135165 export async function resolve ( messages : MessageV2 . WithParts [ ] , filepath : string , messageID : MessageID ) {
136- return runPromise ( ( svc ) => svc . resolve ( messages , filepath , messageID ) )
166+ const system = await systemPaths ( )
167+ const already = loaded ( messages )
168+ const results : { filepath : string ; content : string } [ ] = [ ]
169+
170+ let current = path . dirname ( path . resolve ( filepath ) )
171+ const root = path . resolve ( Instance . directory )
172+
173+ while ( current . startsWith ( root ) ) {
174+ const found = await find ( current )
175+ if ( found && ! system . has ( found ) && ! already . has ( found ) && ! isClaimed ( messageID , found ) ) {
176+ const content = await Filesystem . readText ( found ) . catch ( ( ) => undefined )
177+ if ( content ) {
178+ claim ( messageID , found )
179+ results . push ( { filepath : found , content : "Instructions from: " + found + "\n" + content } )
180+ }
181+ }
182+ if ( current === root ) break
183+ current = path . dirname ( current )
184+ }
185+
186+ return results
137187 }
138188}
139189
@@ -147,6 +197,11 @@ export namespace Instruction {
147197 export interface EffectInterface {
148198 systemPaths ( ) : Effect . Effect < Set < string > >
149199 system ( ) : Effect . Effect < string [ ] >
200+ resolve (
201+ messages : MessageV2 . WithParts [ ] ,
202+ filepath : string ,
203+ messageID : MessageID ,
204+ ) : Effect . Effect < { filepath : string ; content : string } [ ] >
150205 }
151206
152207 export class Service extends ServiceMap . Service < Service , EffectInterface > ( ) ( "@opencode/Instruction" ) { }
@@ -156,6 +211,8 @@ export namespace Instruction {
156211 Effect . sync ( ( ) : EffectInterface => ( {
157212 systemPaths : ( ) => Effect . promise ( ( ) => InstructionPrompt . systemPaths ( ) ) ,
158213 system : ( ) => Effect . promise ( ( ) => InstructionPrompt . system ( ) ) ,
214+ resolve : ( messages , filepath , messageID ) =>
215+ Effect . promise ( ( ) => InstructionPrompt . resolve ( messages , filepath , messageID ) ) ,
159216 } ) ) ,
160217 )
161218
0 commit comments