@@ -4,6 +4,8 @@ import { Effect, Layer, Record, Result, Schema, Context } from "effect"
44import { NonNegativeInt } from "@opencode-ai/core/schema"
55import { Global } from "@opencode-ai/core/global"
66import { FSUtil } from "@opencode-ai/core/fs-util"
7+ import { Credential } from "@opencode-ai/core/credential"
8+ import { IntegrationSchema } from "@opencode-ai/core/integration/schema"
79
810export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key"
911
@@ -53,6 +55,7 @@ export const layer = Layer.effect(
5355 Service ,
5456 Effect . gen ( function * ( ) {
5557 const fsys = yield * FSUtil . Service
58+ const credentials = yield * Credential . Service
5659 const decode = Schema . decodeUnknownOption ( Info )
5760
5861 const all = Effect . fn ( "Auth.all" ) ( function * ( ) {
@@ -78,6 +81,31 @@ export const layer = Layer.effect(
7881 yield * fsys
7982 . writeJson ( file , { ...data , [ norm ] : info } , 0o600 )
8083 . pipe ( Effect . mapError ( fail ( "Failed to write auth data" ) ) )
84+
85+ yield * credentials
86+ . create ( {
87+ integrationID : IntegrationSchema . ID . make ( norm ) ,
88+ label : "default" ,
89+ value :
90+ info . type === "api"
91+ ? new Credential . Key ( { type : "key" , key : info . key , metadata : info . metadata } )
92+ : info . type === "oauth"
93+ ? new Credential . OAuth ( {
94+ type : "oauth" ,
95+ access : info . access ,
96+ refresh : info . refresh ,
97+ expires : info . expires ,
98+ metadata : {
99+ ...( info . accountId ? { clientId : info . accountId } : { } ) ,
100+ ...( info . enterpriseUrl ? { clientSecret : info . enterpriseUrl } : { } ) ,
101+ } ,
102+ } )
103+ : new Credential . Key ( {
104+ type : "key" ,
105+ key : info . token ,
106+ } ) ,
107+ } )
108+ . pipe ( Effect . orDie )
81109 } )
82110
83111 const remove = Effect . fn ( "Auth.remove" ) ( function * ( key : string ) {
@@ -86,13 +114,21 @@ export const layer = Layer.effect(
86114 delete data [ key ]
87115 delete data [ norm ]
88116 yield * fsys . writeJson ( file , data , 0o600 ) . pipe ( Effect . mapError ( fail ( "Failed to write auth data" ) ) )
117+
118+ const existing = yield * credentials . list ( IntegrationSchema . ID . make ( norm ) ) . pipe ( Effect . orDie )
119+ for ( const item of existing ) {
120+ yield * credentials . remove ( item . id ) . pipe ( Effect . orDie )
121+ }
89122 } )
90123
91124 return Service . of ( { get, all, set, remove } )
92125 } ) ,
93126)
94127
95- export const defaultLayer = layer . pipe ( Layer . provide ( FSUtil . defaultLayer ) )
128+ export const defaultLayer = layer . pipe (
129+ Layer . provide ( FSUtil . defaultLayer ) ,
130+ Layer . provide ( Credential . defaultLayer ) ,
131+ )
96132
97133export const node = LayerNode . make ( layer , [ FSUtil . node ] )
98134
0 commit comments