|
|
@@ -13,11 +13,12 @@ import { Env } from "../env"
|
|
|
import { applyEdits, modify } from "jsonc-parser"
|
|
|
import { InstallationLocal, InstallationVersion } from "@opencode-ai/core/installation/version"
|
|
|
import { existsSync } from "fs"
|
|
|
+import { Account } from "@/account/account"
|
|
|
import { isRecord } from "@/util/record"
|
|
|
import type { ConsoleState } from "@opencode-ai/core/v1/config/console-state"
|
|
|
import { FSUtil } from "@opencode-ai/core/fs-util"
|
|
|
import { InstanceState } from "@/effect/instance-state"
|
|
|
-import { Context, Duration, Effect, Exit, Fiber, Layer, Schema } from "effect"
|
|
|
+import { Context, Duration, Effect, Exit, Fiber, Layer, Option, Schema } from "effect"
|
|
|
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"
|
|
|
import { EffectFlock } from "@opencode-ai/core/util/effect-flock"
|
|
|
import { containsPath, type InstanceContext } from "../project/instance-context"
|
|
|
@@ -176,6 +177,7 @@ export const layer = Layer.effect(
|
|
|
Effect.gen(function* () {
|
|
|
const fs = yield* FSUtil.Service
|
|
|
const authSvc = yield* Auth.Service
|
|
|
+ const accountSvc = yield* Account.Service
|
|
|
const env = yield* Env.Service
|
|
|
const npmSvc = yield* Npm.Service
|
|
|
const http = yield* HttpClient.HttpClient
|
|
|
@@ -314,6 +316,7 @@ export const layer = Layer.effect(
|
|
|
|
|
|
let result: Info = {}
|
|
|
const authEnv: Record<string, string> = {}
|
|
|
+ const consoleManagedProviders = new Set<string>()
|
|
|
let activeOrgName: string | undefined
|
|
|
|
|
|
const pluginScopeForSource = Effect.fnUntraced(function* (source: string) {
|
|
|
@@ -471,6 +474,44 @@ export const layer = Layer.effect(
|
|
|
yield* Effect.logDebug("loaded custom config from OPENCODE_CONFIG_CONTENT")
|
|
|
}
|
|
|
|
|
|
+ const activeAccount = Option.getOrUndefined(
|
|
|
+ yield* accountSvc.active().pipe(Effect.catch(() => Effect.succeed(Option.none()))),
|
|
|
+ )
|
|
|
+ if (activeAccount?.active_org_id) {
|
|
|
+ const accountID = activeAccount.id
|
|
|
+ const orgID = activeAccount.active_org_id
|
|
|
+ const url = activeAccount.url
|
|
|
+ yield* Effect.gen(function* () {
|
|
|
+ const [configOpt, tokenOpt] = yield* Effect.all(
|
|
|
+ [accountSvc.config(accountID, orgID), accountSvc.token(accountID)],
|
|
|
+ { concurrency: 2 },
|
|
|
+ )
|
|
|
+ if (Option.isSome(tokenOpt)) {
|
|
|
+ process.env["OPENCODE_CONSOLE_TOKEN"] = tokenOpt.value
|
|
|
+ yield* env.set("OPENCODE_CONSOLE_TOKEN", tokenOpt.value)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Option.isSome(configOpt)) {
|
|
|
+ const source = `${url}/api/config`
|
|
|
+ const next = yield* loadConfig(JSON.stringify(configOpt.value), {
|
|
|
+ dir: path.dirname(source),
|
|
|
+ source,
|
|
|
+ })
|
|
|
+ for (const providerID of Object.keys(next.provider ?? {})) {
|
|
|
+ consoleManagedProviders.add(providerID)
|
|
|
+ }
|
|
|
+ yield* merge(source, next, "global")
|
|
|
+ }
|
|
|
+ }).pipe(
|
|
|
+ Effect.withSpan("Config.loadActiveOrgConfig"),
|
|
|
+ Effect.catch((err) =>
|
|
|
+ Effect.logDebug("failed to fetch remote account config", {
|
|
|
+ error: err instanceof Error ? err.message : String(err),
|
|
|
+ }),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
const managedDir = ConfigManaged.managedConfigDir()
|
|
|
if (existsSync(managedDir)) {
|
|
|
for (const file of ["opencode.json", "opencode.jsonc"]) {
|
|
|
@@ -546,7 +587,7 @@ export const layer = Layer.effect(
|
|
|
directories,
|
|
|
deps,
|
|
|
consoleState: {
|
|
|
- consoleManagedProviders: [],
|
|
|
+ consoleManagedProviders: Array.from(consoleManagedProviders),
|
|
|
activeOrgName,
|
|
|
switchableOrgCount: 0,
|
|
|
},
|
|
|
@@ -635,10 +676,11 @@ export const defaultLayer = layer.pipe(
|
|
|
Layer.provide(FSUtil.defaultLayer),
|
|
|
Layer.provide(Env.defaultLayer),
|
|
|
Layer.provide(Auth.defaultLayer),
|
|
|
+ Layer.provide(Account.defaultLayer),
|
|
|
Layer.provide(Npm.defaultLayer),
|
|
|
Layer.provide(FetchHttpClient.layer),
|
|
|
)
|
|
|
|
|
|
-export const node = LayerNode.make(layer, [FSUtil.node, Auth.node, Env.node, Npm.node, httpClient])
|
|
|
+export const node = LayerNode.make(layer, [FSUtil.node, Auth.node, Account.node, Env.node, Npm.node, httpClient])
|
|
|
|
|
|
export * as Config from "./config"
|