@@ -16,6 +16,7 @@ import { ConfigPolicyPlugin } from "@opencode/core/config/plugin/policy"
1616import { ConfigProviderPlugin } from "@opencode/core/config/plugin/provider"
1717import { ConfigWebSearchPlugin } from "@opencode/core/config/plugin/websearch"
1818import { Credential } from "@opencode/core/credential"
19+ import { InstructionDiscovery } from "@opencode/core/instruction-discovery"
1920import { Integration } from "@opencode/core/integration"
2021import { Model } from "@opencode/core/model"
2122import { ModelResolver } from "@opencode/core/model-resolver"
@@ -358,9 +359,13 @@ describe("OpencodePlugin", () => {
358359 label : initial . label ,
359360 } ) ,
360361 ) . toEqual ( stored . value )
361- expect ( requests ) . toEqual ( [ "/auth/device/token" , "/api/v2/config" ] )
362- // The refresh and the config fetch both say which OpenCode is asking.
363- expect ( agents ) . toEqual ( [ "opencode/beta/1.2.3/test" , "opencode/beta/1.2.3/test" ] )
362+ expect ( requests ) . toEqual ( [
363+ "/auth/device/token" ,
364+ "/api/v2/config" ,
365+ ...( scenario . orgID === "org-missing" ? [ ] : [ "/api/v2/instructions" , "/api/v2/skills" ] ) ,
366+ ] )
367+ // The refresh and every Console fetch say which OpenCode is asking.
368+ expect ( agents ) . toEqual ( requests . map ( ( ) => "opencode/beta/1.2.3/test" ) )
364369 } ) . pipe ( Effect . provideService ( App . Metadata , App . make ( { name : "test" , version : "1.2.3" , channel : "beta" } ) ) ) ,
365370 ( { server } ) => Effect . promise ( ( ) => server . stop ( true ) ) ,
366371 ) ,
@@ -410,6 +415,7 @@ describe("OpencodePlugin", () => {
410415 server : Bun . serve ( {
411416 port : 0 ,
412417 fetch : ( request ) => {
418+ if ( new URL ( request . url ) . pathname !== "/api/v2/config" ) return new Response ( null , { status : 404 } )
413419 authorization . push ( request . headers . get ( "authorization" ) )
414420 requests . push ( `${ request . method } ${ new URL ( request . url ) . pathname } ` )
415421 const origin = new URL ( request . url ) . origin
@@ -695,7 +701,8 @@ describe("OpencodePlugin", () => {
695701 const state = { advertised : false , failing : false , requests : 0 }
696702 const server = Bun . serve ( {
697703 port : 0 ,
698- fetch : ( ) => {
704+ fetch : ( request ) => {
705+ if ( new URL ( request . url ) . pathname !== "/api/v2/config" ) return new Response ( null , { status : 404 } )
699706 state . requests ++
700707 if ( state . failing ) return new Response ( "Unavailable" , { status : 502 } )
701708 return Response . json ( {
@@ -896,7 +903,8 @@ describe("OpencodePlugin", () => {
896903 const state = { sso : false , requests : 0 }
897904 const server = Bun . serve ( {
898905 port : 0 ,
899- fetch : ( ) => {
906+ fetch : ( request ) => {
907+ if ( new URL ( request . url ) . pathname !== "/api/v2/config" ) return new Response ( null , { status : 404 } )
900908 state . requests ++
901909 if ( state . sso )
902910 return Response . json (
@@ -1191,6 +1199,7 @@ describe("OpencodePlugin", () => {
11911199 port : 0 ,
11921200 fetch : async ( request ) => {
11931201 const path = new URL ( request . url ) . pathname
1202+ if ( path === "/api/v2/instructions" || path === "/api/v2/skills" ) return new Response ( null , { status : 404 } )
11941203 const body = request . method === "POST" ? await request . json ( ) : undefined
11951204 requests . push ( {
11961205 method : request . method ,
@@ -1385,6 +1394,8 @@ describe("OpencodePlugin", () => {
13851394 )
13861395 expect ( requests ) . toEqual ( [
13871396 "https://opencode.ai/console/api/v2/config" ,
1397+ "https://opencode.ai/console/api/v2/instructions" ,
1398+ "https://opencode.ai/console/api/v2/skills" ,
13881399 "https://opencode.ai/console/api/websearch" ,
13891400 ] )
13901401 } ) ,
@@ -1525,6 +1536,7 @@ describe("OpencodePlugin", () => {
15251536 if ( url . pathname === "/api/v2/config" ) {
15261537 return Response . json ( { providers : { } , websearch : { provider : "opencode" } } )
15271538 }
1539+ if ( url . pathname . startsWith ( "/api/v2/" ) ) return new Response ( null , { status : 404 } )
15281540 return Response . redirect ( `${ url . origin } /loop` , 302 )
15291541 } ,
15301542 } )
@@ -1568,10 +1580,9 @@ describe("OpencodePlugin", () => {
15681580 authorization : request . headers . get ( "authorization" ) ,
15691581 orgID : request . headers . get ( "x-org-id" ) ,
15701582 } )
1571- if ( url . pathname === "/api/v2/config" ) {
1572- return Response . json ( { providers : { } , library : { skills : [ { slug : "deploy" , version : "1" } ] } } )
1573- }
1574- if ( url . pathname === "/api/v2/library/skills/deploy" ) {
1583+ if ( url . pathname === "/api/v2/config" ) return Response . json ( { providers : { } } )
1584+ if ( url . pathname === "/api/v2/skills" ) return Response . json ( { skills : [ { slug : "deploy" , version : "1" } ] } )
1585+ if ( url . pathname === "/api/v2/skills/deploy" ) {
15751586 // Skill archives come from presigned storage on its own host, where the signature covers the encoding.
15761587 const target = new URL ( "/archive/deploy.json?expires=1&sig=a%20b~c" , url )
15771588 target . hostname = "127.0.0.1"
@@ -1598,7 +1609,7 @@ describe("OpencodePlugin", () => {
15981609
15991610 yield * eventually ( skills . list ( ) , ( list ) => list . some ( ( skill ) => skill . id === Skill . ID . make ( "deploy" ) ) )
16001611 expect ( downloads ) . toContainEqual ( {
1601- path : "/api/v2/library/ skills/deploy" ,
1612+ path : "/api/v2/skills/deploy" ,
16021613 search : "" ,
16031614 authorization : "Bearer secret" ,
16041615 orgID : "org_test" ,
@@ -1614,6 +1625,63 @@ describe("OpencodePlugin", () => {
16141625 ) ,
16151626 )
16161627
1628+ it . effect ( "keeps Console instructions and skills through an edit or a Console error, and clears them on 404" , ( ) =>
1629+ Effect . gen ( function * ( ) {
1630+ const credentials = yield * Credential . Service
1631+ const discovery = yield * InstructionDiscovery . Service
1632+ const skills = yield * Skill . Service
1633+ const state = { instruction : "Use tabs." , lists : 200 }
1634+ const server = {
1635+ fetch : ( request : Request ) => {
1636+ const path = new URL ( request . url ) . pathname
1637+ if ( path === "/api/v2/config" ) return Response . json ( { providers : { } } )
1638+ if ( ( path === "/api/v2/instructions" || path === "/api/v2/skills" ) && state . lists !== 200 )
1639+ return new Response ( null , { status : state . lists } )
1640+ if ( path === "/api/v2/instructions" )
1641+ return Response . json ( { instructions : [ { id : "style" , content : state . instruction } ] } )
1642+ if ( path === "/api/v2/skills" ) return Response . json ( { skills : [ { slug : "deploy" , version : "1" } ] } )
1643+ if ( path === "/api/v2/skills/deploy" ) return Response . json ( skillBundle )
1644+ return new Response ( null , { status : 404 } )
1645+ } ,
1646+ }
1647+ const loaded = Effect . all ( [ discovery . list ( ) , skills . get ( Skill . ID . make ( "deploy" ) ) ] ) . pipe (
1648+ Effect . map ( ( [ files , skill ] ) => ( {
1649+ instructions : Array . isArray ( files )
1650+ ? files . filter ( ( file ) => file . path . startsWith ( "OpenCode Console" ) ) . map ( ( file ) => file . content )
1651+ : [ ] ,
1652+ skill : skill ?. description ,
1653+ } ) ) ,
1654+ )
1655+ yield * credentials . create ( {
1656+ integrationID : Integration . ID . make ( "opencode" ) ,
1657+ value : Credential . Key . make ( { type : "key" , key : "secret" , metadata : { server : "https://console.test" } } ) ,
1658+ } )
1659+ yield * addPlugin ( ) . pipe ( inProcess ( server ) )
1660+ yield * drain
1661+ expect ( yield * loaded ) . toEqual ( { instructions : [ "Use tabs." ] , skill : "Deploys the service." } )
1662+
1663+ // The config is unchanged, so config keeps the Console document it already holds.
1664+ state . instruction = "Use spaces."
1665+ yield * TestClock . adjust ( "1 minute" )
1666+ yield * drain
1667+ expect ( yield * loaded ) . toEqual ( { instructions : [ "Use spaces." ] , skill : "Deploys the service." } )
1668+
1669+ // A Console error keeps both, through the retries.
1670+ state . lists = 503
1671+ yield * TestClock . adjust ( "1 minute" )
1672+ yield * drain
1673+ yield * TestClock . adjust ( "10 seconds" )
1674+ yield * drain
1675+ expect ( yield * loaded ) . toEqual ( { instructions : [ "Use spaces." ] , skill : "Deploys the service." } )
1676+
1677+ // A 404 means the workspace has none.
1678+ state . lists = 404
1679+ yield * TestClock . adjust ( "1 minute" )
1680+ yield * drain
1681+ expect ( yield * loaded ) . toEqual ( { instructions : [ ] , skill : undefined } )
1682+ } ) ,
1683+ )
1684+
16171685 it . live ( "keeps credentials on a same-origin skill redirect" , ( ) =>
16181686 Effect . acquireUseRelease (
16191687 Effect . sync ( ( ) => {
@@ -1623,10 +1691,9 @@ describe("OpencodePlugin", () => {
16231691 fetch : ( request ) => {
16241692 const url = new URL ( request . url )
16251693 downloads . push ( { path : url . pathname , authorization : request . headers . get ( "authorization" ) } )
1626- if ( url . pathname === "/api/v2/config" ) {
1627- return Response . json ( { providers : { } , library : { skills : [ { slug : "deploy" , version : "1" } ] } } )
1628- }
1629- if ( url . pathname === "/api/v2/library/skills/deploy" )
1694+ if ( url . pathname === "/api/v2/config" ) return Response . json ( { providers : { } } )
1695+ if ( url . pathname === "/api/v2/skills" ) return Response . json ( { skills : [ { slug : "deploy" , version : "1" } ] } )
1696+ if ( url . pathname === "/api/v2/skills/deploy" )
16301697 return Response . redirect ( `${ url . origin } /assets/deploy.json` , 302 )
16311698 return Response . json ( skillBundle )
16321699 } ,
@@ -1668,6 +1735,7 @@ describe("OpencodePlugin", () => {
16681735 websearch : { provider : "opencode" } ,
16691736 } )
16701737 }
1738+ if ( url . pathname . startsWith ( "/api/v2/" ) ) return new Response ( null , { status : 404 } )
16711739 return new Response (
16721740 new ReadableStream ( {
16731741 start ( controller ) {
0 commit comments