refactor(account): destroy Account facade (#22068)

This commit is contained in:
Kit Langton
2026-04-11 14:16:36 -04:00
committed by GitHub
parent d1f05b0f3a
commit 312f10f797
3 changed files with 42 additions and 37 deletions
-15
View File
@@ -7,7 +7,6 @@ import {
HttpClientResponse, HttpClientResponse,
} from "effect/unstable/http" } from "effect/unstable/http"
import { makeRuntime } from "@/effect/run-service"
import { withTransientReadRetry } from "@/util/effect-http-client" import { withTransientReadRetry } from "@/util/effect-http-client"
import { AccountRepo, type AccountRow } from "./repo" import { AccountRepo, type AccountRow } from "./repo"
import { normalizeServerUrl } from "./url" import { normalizeServerUrl } from "./url"
@@ -454,18 +453,4 @@ export namespace Account {
) )
export const defaultLayer = layer.pipe(Layer.provide(AccountRepo.layer), Layer.provide(FetchHttpClient.layer)) export const defaultLayer = layer.pipe(Layer.provide(AccountRepo.layer), Layer.provide(FetchHttpClient.layer))
export const { runPromise } = makeRuntime(Service, defaultLayer)
export async function active(): Promise<Info | undefined> {
return Option.getOrUndefined(await runPromise((service) => service.active()))
}
export async function orgsByAccount(): Promise<readonly AccountOrgs[]> {
return runPromise((service) => service.orgsByAccount())
}
export async function switchOrg(accountID: AccountID, orgID: OrgID) {
return runPromise((service) => service.use(accountID, Option.some(orgID)))
}
} }
+6 -5
View File
@@ -3,6 +3,7 @@ import { Duration, Effect, Match, Option } from "effect"
import { UI } from "../ui" import { UI } from "../ui"
import { AccountID, Account, OrgID, PollExpired, type PollResult } from "@/account" import { AccountID, Account, OrgID, PollExpired, type PollResult } from "@/account"
import { type AccountError } from "@/account/schema" import { type AccountError } from "@/account/schema"
import { AppRuntime } from "@/effect/app-runtime"
import * as Prompt from "../effect/prompt" import * as Prompt from "../effect/prompt"
import open from "open" import open from "open"
@@ -182,7 +183,7 @@ export const LoginCommand = cmd({
}), }),
async handler(args) { async handler(args) {
UI.empty() UI.empty()
await Account.runPromise((_svc) => loginEffect(args.url)) await AppRuntime.runPromise(loginEffect(args.url))
}, },
}) })
@@ -196,7 +197,7 @@ export const LogoutCommand = cmd({
}), }),
async handler(args) { async handler(args) {
UI.empty() UI.empty()
await Account.runPromise((_svc) => logoutEffect(args.email)) await AppRuntime.runPromise(logoutEffect(args.email))
}, },
}) })
@@ -205,7 +206,7 @@ export const SwitchCommand = cmd({
describe: false, describe: false,
async handler() { async handler() {
UI.empty() UI.empty()
await Account.runPromise((_svc) => switchEffect()) await AppRuntime.runPromise(switchEffect())
}, },
}) })
@@ -214,7 +215,7 @@ export const OrgsCommand = cmd({
describe: false, describe: false,
async handler() { async handler() {
UI.empty() UI.empty()
await Account.runPromise((_svc) => orgsEffect()) await AppRuntime.runPromise(orgsEffect())
}, },
}) })
@@ -223,7 +224,7 @@ export const OpenCommand = cmd({
describe: false, describe: false,
async handler() { async handler() {
UI.empty() UI.empty()
await Account.runPromise((_svc) => openEffect()) await AppRuntime.runPromise(openEffect())
}, },
}) })
@@ -11,9 +11,11 @@ import { Session } from "../../session"
import { Config } from "../../config/config" import { Config } from "../../config/config"
import { ConsoleState } from "../../config/console-state" import { ConsoleState } from "../../config/console-state"
import { Account, AccountID, OrgID } from "../../account" import { Account, AccountID, OrgID } from "../../account"
import { AppRuntime } from "../../effect/app-runtime"
import { zodToJsonSchema } from "zod-to-json-schema" import { zodToJsonSchema } from "zod-to-json-schema"
import { errors } from "../error" import { errors } from "../error"
import { lazy } from "../../util/lazy" import { lazy } from "../../util/lazy"
import { Effect, Option } from "effect"
import { WorkspaceRoutes } from "./workspace" import { WorkspaceRoutes } from "./workspace"
import { Agent } from "@/agent/agent" import { Agent } from "@/agent/agent"
@@ -55,11 +57,18 @@ export const ExperimentalRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
const [consoleState, groups] = await Promise.all([Config.getConsoleState(), Account.orgsByAccount()]) const result = await AppRuntime.runPromise(
return c.json({ Effect.gen(function* () {
...consoleState, const config = yield* Config.Service
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0), const account = yield* Account.Service
}) const [state, groups] = yield* Effect.all([config.getConsoleState(), account.orgsByAccount()], { concurrency: "unbounded" })
return {
...state,
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0),
}
}),
)
return c.json(result)
}, },
) )
.get( .get(
@@ -80,17 +89,22 @@ export const ExperimentalRoutes = lazy(() =>
}, },
}), }),
async (c) => { async (c) => {
const [groups, active] = await Promise.all([Account.orgsByAccount(), Account.active()]) const orgs = await AppRuntime.runPromise(
Effect.gen(function* () {
const orgs = groups.flatMap((group) => const account = yield* Account.Service
group.orgs.map((org) => ({ const [groups, active] = yield* Effect.all([account.orgsByAccount(), account.active()], { concurrency: "unbounded" })
accountID: group.account.id, const info = Option.getOrUndefined(active)
accountEmail: group.account.email, return groups.flatMap((group) =>
accountUrl: group.account.url, group.orgs.map((org) => ({
orgID: org.id, accountID: group.account.id,
orgName: org.name, accountEmail: group.account.email,
active: !!active && active.id === group.account.id && active.active_org_id === org.id, accountUrl: group.account.url,
})), orgID: org.id,
orgName: org.name,
active: !!info && info.id === group.account.id && info.active_org_id === org.id,
})),
)
}),
) )
return c.json({ orgs }) return c.json({ orgs })
}, },
@@ -115,7 +129,12 @@ export const ExperimentalRoutes = lazy(() =>
validator("json", ConsoleSwitchBody), validator("json", ConsoleSwitchBody),
async (c) => { async (c) => {
const body = c.req.valid("json") const body = c.req.valid("json")
await Account.switchOrg(AccountID.make(body.accountID), OrgID.make(body.orgID)) await AppRuntime.runPromise(
Effect.gen(function* () {
const account = yield* Account.Service
yield* account.use(AccountID.make(body.accountID), Option.some(OrgID.make(body.orgID)))
}),
)
return c.json(true) return c.json(true)
}, },
) )