1+ import { isDeepStrictEqual } from "node:util"
12import { Duration , Effect , Equal , FiberHandle , Option , Schema , Scope , Semaphore , Stream } from "effect"
23import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect/integration"
34import { define } from "@opencode/plugin/effect/plugin"
@@ -58,21 +59,18 @@ const SsoRequired = Schema.Struct({
5859const InvalidGrant = Schema . Struct ( { error : Schema . Literal ( "invalid_grant" ) } )
5960// Console's 401 when the session behind the access token was revoked or expired.
6061const Unauthorized = Schema . Struct ( { _tag : Schema . Literal ( "Unauthorized" ) } )
61- // The workspace Library: instructions arrive whole, skills as versions whose files download separately.
62- const Library = Schema . Struct ( {
63- instructions : Schema . Array ( Schema . Struct ( { id : Schema . String , content : Schema . String } ) ) . pipe ( Schema . optional ) ,
64- skills : Schema . Array ( Schema . Struct ( { slug : Schema . String , version : Schema . String } ) ) . pipe ( Schema . optional ) ,
62+ // The workspace's instructions arrive whole, its skills as versions whose files download separately.
63+ const ConsoleInstructions = Schema . Struct ( {
64+ instructions : Schema . Array ( Schema . Struct ( { id : Schema . String , content : Schema . String } ) ) ,
6565} )
66- // `library` decodes where it's used, so a malformed one can't fail the config and drop its policies.
67- const ConsoleConfig = Schema . Struct ( {
68- ...ConfigIntegration . Remote . fields ,
69- library : Schema . Unknown . pipe ( Schema . optional ) ,
66+ const ConsoleSkills = Schema . Struct ( {
67+ skills : Schema . Array ( Schema . Struct ( { slug : Schema . String , version : Schema . String } ) ) ,
7068} )
7169const SkillBundle = Schema . Struct ( {
7270 slug : Schema . String ,
7371 files : Schema . Array ( Schema . Struct ( { path : Schema . String , content : Schema . String } ) ) ,
7472} )
75- type LibrarySkill = { readonly skill : Skill . Info ; readonly files : Record < string , string > }
73+ type ConsoleSkill = { readonly skill : Skill . Info ; readonly files : Record < string , string > }
7674const signedOutMessage = "Reconnect OpenCode Console to continue"
7775const ssoMessage = ( organization : string | undefined ) =>
7876 `Sign in with SSO again to use ${ organization ?? "your OpenCode Console organization" } `
@@ -197,13 +195,13 @@ export const OpencodePlugin = define<
197195 type ActiveConnection = Effect . Success < ReturnType < typeof ctx . integration . connection . active > >
198196 let snapshot : {
199197 // The Console's regular OpenCode config, merged as an integration config layer.
200- remote : typeof ConsoleConfig . Type | undefined
198+ remote : Effect . Success < ReturnType < typeof fetchConfig > >
201199 connection : ActiveConnection
202200 organization : string | undefined
203201 } = { remote : undefined , connection : undefined , organization : undefined }
204202 let instructions : InstructionDiscovery . File [ ] = [ ]
205203 // Tagged with the account and workspace they were downloaded for, so a switch hides them right away.
206- let librarySkills : { owner : string ; items : LibrarySkill [ ] } = { owner : "" , items : [ ] }
204+ let consoleSkills : { owner : string ; items : ConsoleSkill [ ] } = { owner : "" , items : [ ] }
207205 const owner = ( from : typeof snapshot ) =>
208206 `${ IntegrationConnection . key ( from . connection ) ?? "" } :${ from . organization ?? "" } `
209207 // A newer config's skill download replaces one still in flight.
@@ -290,31 +288,33 @@ export const OpencodePlugin = define<
290288 ? { ...next . connection , id : Credential . ID . make ( next . connection . id ) }
291289 : next . connection ,
292290 organization : next . organization ,
293- // `make` keeps only the standard config keys, so `library` stays here.
291+ // `make` keeps only the standard config keys, so instructions and skills stay here.
294292 info : next . remote ,
295293 } )
296294 yield * integrate ( document )
297- // Managed `integration.use: deny` makes config layer a policies-only copy, which withholds the Library too.
298- yield * syncLibrary ( next , document !== undefined && ( ! config . integration || integrated === document ) )
295+ // Managed `integration.use: deny` makes config layer a policies-only copy, which withholds instructions and
296+ // skills too. Config keeps the document it holds when handed a deep-equal one, so compare by content.
297+ yield * syncInstructionsAndSkills (
298+ next ,
299+ document !== undefined && ( ! config . integration || isDeepStrictEqual ( integrated , document ) ) ,
300+ )
299301 } )
300- const syncLibrary = ( next : typeof snapshot , allowed : boolean ) => {
301- const library = allowed
302- ? Option . getOrUndefined ( Schema . decodeUnknownOption ( Library ) ( next . remote ?. library ) )
303- : undefined
304- instructions = ( library ?. instructions ?? [ ] ) . map (
302+ const syncInstructionsAndSkills = ( next : typeof snapshot , allowed : boolean ) => {
303+ const remote = allowed ? next . remote : undefined
304+ instructions = ( remote ?. instructions ?? [ ] ) . map (
305305 ( item ) =>
306306 new InstructionDiscovery . File ( { path : `OpenCode Console instruction ${ item . id } ` , content : item . content } ) ,
307307 )
308- if ( ! allowed ) librarySkills = { owner : "" , items : [ ] }
308+ if ( ! allowed ) consoleSkills = { owner : "" , items : [ ] }
309309 // Skills download in the background, so startup and config changes never wait on them. The current ones stay
310310 // until the download replaces them.
311311 return FiberHandle . run (
312312 skillDownload ,
313- loadSkills ( next . connection , library ?. skills ?? [ ] ) . pipe (
313+ loadSkills ( next . connection , remote ?. skills ?? [ ] ) . pipe (
314314 Effect . flatMap ( ( loaded ) => {
315315 // A failed load keeps the skills already shown.
316316 if ( ! loaded ) return Effect . void
317- librarySkills = { owner : owner ( next ) , items : loaded }
317+ consoleSkills = { owner : owner ( next ) , items : loaded }
318318 return skills . reload ( )
319319 } ) ,
320320 ) ,
@@ -450,8 +450,8 @@ export const OpencodePlugin = define<
450450 } )
451451 yield * discovery . transform ( ( editor ) => instructions . forEach ( ( file ) => editor . add ( file ) ) )
452452 yield * skills . transform ( ( editor ) => {
453- if ( librarySkills . owner !== owner ( snapshot ) ) return
454- librarySkills . items . forEach ( ( item ) => editor . add ( item . skill , item . files ) )
453+ if ( consoleSkills . owner !== owner ( snapshot ) ) return
454+ consoleSkills . items . forEach ( ( item ) => editor . add ( item . skill , item . files ) )
455455 } )
456456
457457 const apply = Effect . fn ( "OpencodePlugin.apply" ) ( function * ( next : typeof snapshot ) {
@@ -544,7 +544,26 @@ function fetchConfig(http: HttpClient.HttpClient, value: Credential.Key | Creden
544544 } )
545545 }
546546 const body = yield * HttpClientResponse . filterStatusOk ( response ) . pipe ( Effect . flatMap ( ( ok ) => ok . json ) )
547- return yield * Schema . decodeUnknownEffect ( ConsoleConfig ) ( body )
547+ const remote = yield * Schema . decodeUnknownEffect ( ConfigIntegration . Remote ) ( body )
548+ // Instructions and skills have their own endpoints. A list Console doesn't give counts as empty.
549+ const list = < A > ( path : string , schema : Schema . Decoder < A > ) =>
550+ HttpClient . withScope ( http )
551+ . execute (
552+ HttpClientRequest . get ( `${ ConsoleAccess . server ( value ) } ${ path } ` ) . pipe (
553+ HttpClientRequest . acceptJson ,
554+ HttpClientRequest . setHeaders ( ConsoleAccess . headers ( value ) ) ,
555+ ) ,
556+ )
557+ . pipe (
558+ Effect . flatMap ( ( response ) =>
559+ response . status >= 300
560+ ? Effect . succeed ( undefined )
561+ : HttpClientResponse . schemaBodyJson ( schema ) ( response ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ,
562+ ) ,
563+ )
564+ const instructions = yield * list ( "/api/v2/instructions" , ConsoleInstructions )
565+ const skills = yield * list ( "/api/v2/skills" , ConsoleSkills )
566+ return { ...remote , instructions : instructions ?. instructions ?? [ ] , skills : skills ?. skills ?? [ ] }
548567 } ) ,
549568 ) ,
550569 Effect . scoped ,
@@ -663,7 +682,7 @@ function downloadSkill(http: HttpClient.HttpClient, server: string, slug: string
663682 if ( ! SkillDiscovery . isSafeSegment ( slug ) ) return yield * Effect . fail ( new Error ( `unsafe skill slug: ${ slug } ` ) )
664683 const bundle = yield * follow (
665684 http ,
666- HttpClientRequest . get ( `${ server } /api/v2/library/ skills/${ encodeURIComponent ( slug ) } ` ) . pipe (
685+ HttpClientRequest . get ( `${ server } /api/v2/skills/${ encodeURIComponent ( slug ) } ` ) . pipe (
667686 HttpClientRequest . acceptJson ,
668687 HttpClientRequest . setHeaders ( headers ) ,
669688 ) ,
@@ -680,7 +699,7 @@ function downloadSkill(http: HttpClient.HttpClient, server: string, slug: string
680699 files : Object . fromEntries (
681700 bundle . files . filter ( ( file ) => file . path !== "SKILL.md" ) . map ( ( file ) => [ file . path , file . content ] ) ,
682701 ) ,
683- } satisfies LibrarySkill
702+ } satisfies ConsoleSkill
684703 } )
685704}
686705
0 commit comments