GeezerVison is showing github.com — fix(opencode): restore v1 account config (#33590) · soequelle/opencode@5647ed8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/config/config.ts +45-3Lines changed: 45 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,11 +13,12 @@ import { Env } from "../env" | 13 | 13 | import { applyEdits, modify } from "jsonc-parser" | 14 | 14 | import { InstallationLocal, InstallationVersion } from "@opencode-ai/core/installation/version" | 15 | 15 | import { existsSync } from "fs" | | 16 | +import { Account } from "@/account/account" | 16 | 17 | import { isRecord } from "@/util/record" | 17 | 18 | import type { ConsoleState } from "@opencode-ai/core/v1/config/console-state" | 18 | 19 | import { FSUtil } from "@opencode-ai/core/fs-util" | 19 | 20 | import { 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" | 21 | 22 | import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http" | 22 | 23 | import { EffectFlock } from "@opencode-ai/core/util/effect-flock" | 23 | 24 | import { containsPath, type InstanceContext } from "../project/instance-context" | @@ -176,6 +177,7 @@ export const layer = Layer.effect( | 176 | 177 | Effect.gen(function* () { | 177 | 178 | const fs = yield* FSUtil.Service | 178 | 179 | const authSvc = yield* Auth.Service | | 180 | + const accountSvc = yield* Account.Service | 179 | 181 | const env = yield* Env.Service | 180 | 182 | const npmSvc = yield* Npm.Service | 181 | 183 | const http = yield* HttpClient.HttpClient | @@ -314,6 +316,7 @@ export const layer = Layer.effect( | 314 | 316 |
| 315 | 317 | let result: Info = {} | 316 | 318 | const authEnv: Record<string, string> = {} | | 319 | + const consoleManagedProviders = new Set<string>() | 317 | 320 | let activeOrgName: string | undefined | 318 | 321 |
| 319 | 322 | const pluginScopeForSource = Effect.fnUntraced(function* (source: string) { | @@ -471,6 +474,44 @@ export const layer = Layer.effect( | 471 | 474 | yield* Effect.logDebug("loaded custom config from OPENCODE_CONFIG_CONTENT") | 472 | 475 | } | 473 | 476 |
| | 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 | + | 474 | 515 | const managedDir = ConfigManaged.managedConfigDir() | 475 | 516 | if (existsSync(managedDir)) { | 476 | 517 | for (const file of ["opencode.json", "opencode.jsonc"]) { | @@ -546,7 +587,7 @@ export const layer = Layer.effect( | 546 | 587 | directories, | 547 | 588 | deps, | 548 | 589 | consoleState: { | 549 | | - consoleManagedProviders: [], | | 590 | + consoleManagedProviders: Array.from(consoleManagedProviders), | 550 | 591 | activeOrgName, | 551 | 592 | switchableOrgCount: 0, | 552 | 593 | }, | @@ -635,10 +676,11 @@ export const defaultLayer = layer.pipe( | 635 | 676 | Layer.provide(FSUtil.defaultLayer), | 636 | 677 | Layer.provide(Env.defaultLayer), | 637 | 678 | Layer.provide(Auth.defaultLayer), | | 679 | + Layer.provide(Account.defaultLayer), | 638 | 680 | Layer.provide(Npm.defaultLayer), | 639 | 681 | Layer.provide(FetchHttpClient.layer), | 640 | 682 | ) | 641 | 683 |
| 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]) | 643 | 685 |
| 644 | 686 | export * as Config from "./config" |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/config/config.test.ts +48-1Lines changed: 48 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { test, expect, describe, afterEach, beforeEach, spyOn } from "bun:test" | 2 | 2 | import { ConfigV1 } from "@opencode-ai/core/v1/config/config" | 3 | | -import { Cause, Effect, Exit, Layer } from "effect" | | 3 | +import { Cause, Effect, Exit, Layer, Option } from "effect" | 4 | 4 | import { NamedError } from "@opencode-ai/core/util/error" | 5 | 5 | import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http" | 6 | 6 | import { NodeFileSystem, NodePath } from "@effect/platform-node" | @@ -13,6 +13,7 @@ import { InstanceRef } from "../../src/effect/instance-ref" | 13 | 13 | import type { InstanceContext } from "../../src/project/instance-context" | 14 | 14 | import { Auth } from "../../src/auth" | 15 | 15 | import { Account } from "../../src/account/account" | | 16 | +import { AccessToken, AccountID, OrgID } from "../../src/account/schema" | 16 | 17 | import { FSUtil } from "@opencode-ai/core/fs-util" | 17 | 18 | import { Env } from "../../src/env" | 18 | 19 | import { | @@ -140,6 +141,7 @@ const clear = (wait = false) => Effect.runPromise(clearEffect(wait)) | 140 | 141 | // Get managed config directory from environment (set in preload.ts) | 141 | 142 | const managedConfigDir = process.env.OPENCODE_TEST_MANAGED_CONFIG_DIR! | 142 | 143 | const originalTestToken = process.env.TEST_TOKEN | | 144 | +const originalConsoleToken = process.env.OPENCODE_CONSOLE_TOKEN | 143 | 145 |
| 144 | 146 | beforeEach(async () => { | 145 | 147 | await clear(true) | @@ -149,6 +151,8 @@ afterEach(async () => { | 149 | 151 | await fs.rm(managedConfigDir, { force: true, recursive: true }).catch(() => {}) | 150 | 152 | if (originalTestToken === undefined) delete process.env.TEST_TOKEN | 151 | 153 | else process.env.TEST_TOKEN = originalTestToken | | 154 | + if (originalConsoleToken === undefined) delete process.env.OPENCODE_CONSOLE_TOKEN | | 155 | + else process.env.OPENCODE_CONSOLE_TOKEN = originalConsoleToken | 152 | 156 | await clear(true) | 153 | 157 | }) | 154 | 158 |
| @@ -560,6 +564,49 @@ it.instance("handles file inclusion with replacement tokens", () => | 560 | 564 | }), | 561 | 565 | ) | 562 | 566 |
| | 567 | +const accountTokenIt = configIt({ | | 568 | + account: Layer.mock(Account.Service)({ | | 569 | + active: () => | | 570 | + Effect.succeed( | | 571 | + Option.some({ | | 572 | + id: AccountID.make("account-1"), | | 573 | + | | 574 | + url: "https://control.example.com", | | 575 | + active_org_id: OrgID.make("org-1"), | | 576 | + }), | | 577 | + ), | | 578 | + activeOrg: () => | | 579 | + Effect.succeed( | | 580 | + Option.some({ | | 581 | + account: { | | 582 | + id: AccountID.make("account-1"), | | 583 | + | | 584 | + url: "https://control.example.com", | | 585 | + active_org_id: OrgID.make("org-1"), | | 586 | + }, | | 587 | + org: { | | 588 | + id: OrgID.make("org-1"), | | 589 | + name: "Example Org", | | 590 | + }, | | 591 | + }), | | 592 | + ), | | 593 | + config: () => | | 594 | + Effect.succeed( | | 595 | + Option.some({ | | 596 | + provider: { opencode: { options: { apiKey: "{env:OPENCODE_CONSOLE_TOKEN}" } } }, | | 597 | + }), | | 598 | + ), | | 599 | + token: () => Effect.succeed(Option.some(AccessToken.make("st_test_token"))), | | 600 | + }), | | 601 | +}) | | 602 | + | | 603 | +accountTokenIt.instance("resolves env templates in account config with account token", () => | | 604 | + Effect.gen(function* () { | | 605 | + const config = yield* Config.use.get() | | 606 | + expect(config.provider?.["opencode"]?.options?.apiKey).toBe("st_test_token") | | 607 | + }), | | 608 | +) | | 609 | + | 563 | 610 | it.instance("validates config schema and throws on invalid fields", () => | 564 | 611 | Effect.gen(function* () { | 565 | 612 | const test = yield* TestInstance | |
|