@@ -2,10 +2,12 @@ export * as SkillV2 from "./skill"
22
33import { makeLocationNode } from "./effect/app-node"
44import path from "path"
5- import { Context , Effect , Layer , Schema , Types } from "effect"
5+ import { Context , Effect , Layer , Schema , Stream , Types } from "effect"
6+ import { FileSystemWatcher } from "@opencode-ai/schema/filesystem-watcher"
67import { Skill } from "@opencode-ai/schema/skill"
78import { AgentV2 } from "./agent"
89import { ConfigMarkdown } from "./config/markdown"
10+ import { EventV2 } from "./event"
911import { FSUtil } from "./fs-util"
1012import { PermissionV2 } from "./permission"
1113import { AbsolutePath } from "./schema"
@@ -27,6 +29,8 @@ export type Source = typeof Source.Type
2729export const Info = Skill . Info
2830export type Info = Skill . Info
2931
32+ export const Event = Skill . Event
33+
3034export const available = ( skills : ReadonlyArray < Info > , agent : AgentV2 . Info ) =>
3135 skills . filter ( ( skill ) => PermissionV2 . evaluate ( "skill" , skill . name , agent . permissions ) . effect !== "deny" )
3236
@@ -58,6 +62,7 @@ export const layer = Layer.effect(
5862 Effect . gen ( function * ( ) {
5963 const discovery = yield * SkillDiscovery . Service
6064 const fs = yield * FSUtil . Service
65+ const events = yield * EventV2 . Service
6166
6267 const state = State . create < Data , Draft > ( {
6368 initial : ( ) => ( { sources : [ ] } ) ,
@@ -72,7 +77,15 @@ export const layer = Layer.effect(
7277
7378 const load = Effect . fn ( "SkillV2.load" ) ( function * ( source : Source ) {
7479 const skills : Info [ ] = [ ]
75- if ( source . type === "embedded" ) return [ source . skill ]
80+ if ( source . type === "embedded" ) {
81+ yield * Effect . logDebug ( "skill source loaded" , {
82+ source : Source . key ( source ) ,
83+ type : source . type ,
84+ directories : [ ] ,
85+ skills : [ source . skill . name ] ,
86+ } )
87+ return { skills : [ source . skill ] , directories : [ ] }
88+ }
7689 const directories = source . type === "directory" ? [ source . path ] : yield * discovery . pull ( source . url )
7790 for ( const directory of directories ) {
7891 const files = yield * fs
@@ -101,19 +114,42 @@ export const layer = Layer.effect(
101114 } )
102115 }
103116 }
104- return skills
117+ yield * Effect . logDebug ( "skill source loaded" , {
118+ source : Source . key ( source ) ,
119+ type : source . type ,
120+ directories,
121+ skills : skills . map ( ( skill ) => skill . name ) ,
122+ } )
123+ return { skills, directories }
105124 } )
106125
107- // QUESTION(Dax): Should local skill sources invalidate on filesystem watch
108- // events, following the reload policy chosen for other context sources?
109- const cache = new Map < string , Info [ ] > ( )
126+ const cache = new Map < string , { skills : Info [ ] ; directories : readonly string [ ] } > ( )
127+ const invalidate = Effect . fn ( "SkillV2.invalidateFromWatcher" ) ( function * ( file : string ) {
128+ const invalidated = Array . from ( cache . entries ( ) ) . filter ( ( [ , loaded ] ) =>
129+ loaded . directories . some ( ( directory ) => FSUtil . contains ( directory , file ) ) ,
130+ )
131+ if ( invalidated . length === 0 ) return
132+ for ( const [ key ] of invalidated ) cache . delete ( key )
133+ yield * Effect . logInfo ( "skill cache invalidated" , {
134+ file,
135+ sources : invalidated . map ( ( [ key ] ) => key ) ,
136+ skills : invalidated . flatMap ( ( [ , loaded ] ) => loaded . skills . map ( ( skill ) => skill . name ) ) ,
137+ } )
138+ yield * events . publish ( Event . Updated , { } ) . pipe ( Effect . asVoid )
139+ } )
140+
141+ yield * events . subscribe ( FileSystemWatcher . Event . Updated ) . pipe (
142+ Stream . runForEach ( ( event ) => invalidate ( event . data . file ) ) ,
143+ Effect . forkScoped ( { startImmediately : true } ) ,
144+ )
145+
110146 const list = Effect . fn ( "SkillV2.list" ) ( function * ( ) {
111147 const skills = new Map < string , Info > ( )
112148 for ( const source of state . get ( ) . sources ) {
113149 const key = Source . key ( source )
114150 const loaded = cache . get ( key ) ?? ( yield * load ( source ) )
115151 cache . set ( key , loaded )
116- for ( const skill of loaded ) skills . set ( skill . name , skill )
152+ for ( const skill of loaded . skills ) skills . set ( skill . name , skill )
117153 }
118154 return Array . from ( skills . values ( ) )
119155 } )
@@ -131,4 +167,4 @@ export const layer = Layer.effect(
131167
132168export const locationLayer = layer . pipe ( Layer . provide ( SkillDiscovery . defaultLayer ) )
133169
134- export const node = makeLocationNode ( { service : Service , layer, deps : [ SkillDiscovery . node , FSUtil . node ] } )
170+ export const node = makeLocationNode ( { service : Service , layer, deps : [ SkillDiscovery . node , FSUtil . node , EventV2 . node ] } )
0 commit comments