@@ -13,11 +13,12 @@ import { Env } from "../env"
1313import { applyEdits , modify } from "jsonc-parser"
1414import { InstallationLocal , InstallationVersion } from "@opencode-ai/core/installation/version"
1515import { existsSync } from "fs"
16+ import { Account } from "@/account/account"
1617import { isRecord } from "@/util/record"
1718import type { ConsoleState } from "@opencode-ai/core/v1/config/console-state"
1819import { FSUtil } from "@opencode-ai/core/fs-util"
1920import { InstanceState } from "@/effect/instance-state"
20- import { Context , Duration , Effect , Exit , Fiber , Layer , Schema } from "effect"
21+ import { Context , Duration , Effect , Exit , Fiber , Layer , Option , Schema } from "effect"
2122import { FetchHttpClient , HttpClient , HttpClientRequest } from "effect/unstable/http"
2223import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
2324import { containsPath , type InstanceContext } from "../project/instance-context"
@@ -176,6 +177,7 @@ export const layer = Layer.effect(
176177 Effect . gen ( function * ( ) {
177178 const fs = yield * FSUtil . Service
178179 const authSvc = yield * Auth . Service
180+ const accountSvc = yield * Account . Service
179181 const env = yield * Env . Service
180182 const npmSvc = yield * Npm . Service
181183 const http = yield * HttpClient . HttpClient
@@ -314,6 +316,7 @@ export const layer = Layer.effect(
314316
315317 let result : Info = { }
316318 const authEnv : Record < string , string > = { }
319+ const consoleManagedProviders = new Set < string > ( )
317320 let activeOrgName : string | undefined
318321
319322 const pluginScopeForSource = Effect . fnUntraced ( function * ( source : string ) {
@@ -471,6 +474,44 @@ export const layer = Layer.effect(
471474 yield * Effect . logDebug ( "loaded custom config from OPENCODE_CONFIG_CONTENT" )
472475 }
473476
477+ const activeAccount = Option . getOrUndefined (
478+ yield * accountSvc . active ( ) . pipe ( Effect . catch ( ( ) => Effect . succeed ( Option . none ( ) ) ) ) ,
479+ )
480+ if ( activeAccount ?. active_org_id ) {
481+ const accountID = activeAccount . id
482+ const orgID = activeAccount . active_org_id
483+ const url = activeAccount . url
484+ yield * Effect . gen ( function * ( ) {
485+ const [ configOpt , tokenOpt ] = yield * Effect . all (
486+ [ accountSvc . config ( accountID , orgID ) , accountSvc . token ( accountID ) ] ,
487+ { concurrency : 2 } ,
488+ )
489+ if ( Option . isSome ( tokenOpt ) ) {
490+ process . env [ "OPENCODE_CONSOLE_TOKEN" ] = tokenOpt . value
491+ yield * env . set ( "OPENCODE_CONSOLE_TOKEN" , tokenOpt . value )
492+ }
493+
494+ if ( Option . isSome ( configOpt ) ) {
495+ const source = `${ url } /api/config`
496+ const next = yield * loadConfig ( JSON . stringify ( configOpt . value ) , {
497+ dir : path . dirname ( source ) ,
498+ source,
499+ } )
500+ for ( const providerID of Object . keys ( next . provider ?? { } ) ) {
501+ consoleManagedProviders . add ( providerID )
502+ }
503+ yield * merge ( source , next , "global" )
504+ }
505+ } ) . pipe (
506+ Effect . withSpan ( "Config.loadActiveOrgConfig" ) ,
507+ Effect . catch ( ( err ) =>
508+ Effect . logDebug ( "failed to fetch remote account config" , {
509+ error : err instanceof Error ? err . message : String ( err ) ,
510+ } ) ,
511+ ) ,
512+ )
513+ }
514+
474515 const managedDir = ConfigManaged . managedConfigDir ( )
475516 if ( existsSync ( managedDir ) ) {
476517 for ( const file of [ "opencode.json" , "opencode.jsonc" ] ) {
@@ -546,7 +587,7 @@ export const layer = Layer.effect(
546587 directories,
547588 deps,
548589 consoleState : {
549- consoleManagedProviders : [ ] ,
590+ consoleManagedProviders : Array . from ( consoleManagedProviders ) ,
550591 activeOrgName,
551592 switchableOrgCount : 0 ,
552593 } ,
@@ -635,10 +676,11 @@ export const defaultLayer = layer.pipe(
635676 Layer . provide ( FSUtil . defaultLayer ) ,
636677 Layer . provide ( Env . defaultLayer ) ,
637678 Layer . provide ( Auth . defaultLayer ) ,
679+ Layer . provide ( Account . defaultLayer ) ,
638680 Layer . provide ( Npm . defaultLayer ) ,
639681 Layer . provide ( FetchHttpClient . layer ) ,
640682)
641683
642- export const node = LayerNode . make ( layer , [ FSUtil . node , Auth . node , Env . node , Npm . node , httpClient ] )
684+ export const node = LayerNode . make ( layer , [ FSUtil . node , Auth . node , Account . node , Env . node , Npm . node , httpClient ] )
643685
644686export * as Config from "./config"
0 commit comments