Skip to content

Commit 312f10f

Browse files
authored
refactor(account): destroy Account facade (#22068)
1 parent d1f05b0 commit 312f10f

3 files changed

Lines changed: 42 additions & 37 deletions

File tree

packages/opencode/src/account/index.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
HttpClientResponse,
88
} from "effect/unstable/http"
99

10-
import { makeRuntime } from "@/effect/run-service"
1110
import { withTransientReadRetry } from "@/util/effect-http-client"
1211
import { AccountRepo, type AccountRow } from "./repo"
1312
import { normalizeServerUrl } from "./url"
@@ -454,18 +453,4 @@ export namespace Account {
454453
)
455454

456455
export const defaultLayer = layer.pipe(Layer.provide(AccountRepo.layer), Layer.provide(FetchHttpClient.layer))
457-
458-
export const { runPromise } = makeRuntime(Service, defaultLayer)
459-
460-
export async function active(): Promise<Info | undefined> {
461-
return Option.getOrUndefined(await runPromise((service) => service.active()))
462-
}
463-
464-
export async function orgsByAccount(): Promise<readonly AccountOrgs[]> {
465-
return runPromise((service) => service.orgsByAccount())
466-
}
467-
468-
export async function switchOrg(accountID: AccountID, orgID: OrgID) {
469-
return runPromise((service) => service.use(accountID, Option.some(orgID)))
470-
}
471456
}

packages/opencode/src/cli/cmd/account.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Duration, Effect, Match, Option } from "effect"
33
import { UI } from "../ui"
44
import { AccountID, Account, OrgID, PollExpired, type PollResult } from "@/account"
55
import { type AccountError } from "@/account/schema"
6+
import { AppRuntime } from "@/effect/app-runtime"
67
import * as Prompt from "../effect/prompt"
78
import open from "open"
89

@@ -182,7 +183,7 @@ export const LoginCommand = cmd({
182183
}),
183184
async handler(args) {
184185
UI.empty()
185-
await Account.runPromise((_svc) => loginEffect(args.url))
186+
await AppRuntime.runPromise(loginEffect(args.url))
186187
},
187188
})
188189

@@ -196,7 +197,7 @@ export const LogoutCommand = cmd({
196197
}),
197198
async handler(args) {
198199
UI.empty()
199-
await Account.runPromise((_svc) => logoutEffect(args.email))
200+
await AppRuntime.runPromise(logoutEffect(args.email))
200201
},
201202
})
202203

@@ -205,7 +206,7 @@ export const SwitchCommand = cmd({
205206
describe: false,
206207
async handler() {
207208
UI.empty()
208-
await Account.runPromise((_svc) => switchEffect())
209+
await AppRuntime.runPromise(switchEffect())
209210
},
210211
})
211212

@@ -214,7 +215,7 @@ export const OrgsCommand = cmd({
214215
describe: false,
215216
async handler() {
216217
UI.empty()
217-
await Account.runPromise((_svc) => orgsEffect())
218+
await AppRuntime.runPromise(orgsEffect())
218219
},
219220
})
220221

@@ -223,7 +224,7 @@ export const OpenCommand = cmd({
223224
describe: false,
224225
async handler() {
225226
UI.empty()
226-
await Account.runPromise((_svc) => openEffect())
227+
await AppRuntime.runPromise(openEffect())
227228
},
228229
})
229230

packages/opencode/src/server/routes/experimental.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import { Session } from "../../session"
1111
import { Config } from "../../config/config"
1212
import { ConsoleState } from "../../config/console-state"
1313
import { Account, AccountID, OrgID } from "../../account"
14+
import { AppRuntime } from "../../effect/app-runtime"
1415
import { zodToJsonSchema } from "zod-to-json-schema"
1516
import { errors } from "../error"
1617
import { lazy } from "../../util/lazy"
18+
import { Effect, Option } from "effect"
1719
import { WorkspaceRoutes } from "./workspace"
1820
import { Agent } from "@/agent/agent"
1921

@@ -55,11 +57,18 @@ export const ExperimentalRoutes = lazy(() =>
5557
},
5658
}),
5759
async (c) => {
58-
const [consoleState, groups] = await Promise.all([Config.getConsoleState(), Account.orgsByAccount()])
59-
return c.json({
60-
...consoleState,
61-
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0),
62-
})
60+
const result = await AppRuntime.runPromise(
61+
Effect.gen(function* () {
62+
const config = yield* Config.Service
63+
const account = yield* Account.Service
64+
const [state, groups] = yield* Effect.all([config.getConsoleState(), account.orgsByAccount()], { concurrency: "unbounded" })
65+
return {
66+
...state,
67+
switchableOrgCount: groups.reduce((count, group) => count + group.orgs.length, 0),
68+
}
69+
}),
70+
)
71+
return c.json(result)
6372
},
6473
)
6574
.get(
@@ -80,17 +89,22 @@ export const ExperimentalRoutes = lazy(() =>
8089
},
8190
}),
8291
async (c) => {
83-
const [groups, active] = await Promise.all([Account.orgsByAccount(), Account.active()])
84-
85-
const orgs = groups.flatMap((group) =>
86-
group.orgs.map((org) => ({
87-
accountID: group.account.id,
88-
accountEmail: group.account.email,
89-
accountUrl: group.account.url,
90-
orgID: org.id,
91-
orgName: org.name,
92-
active: !!active && active.id === group.account.id && active.active_org_id === org.id,
93-
})),
92+
const orgs = await AppRuntime.runPromise(
93+
Effect.gen(function* () {
94+
const account = yield* Account.Service
95+
const [groups, active] = yield* Effect.all([account.orgsByAccount(), account.active()], { concurrency: "unbounded" })
96+
const info = Option.getOrUndefined(active)
97+
return groups.flatMap((group) =>
98+
group.orgs.map((org) => ({
99+
accountID: group.account.id,
100+
accountEmail: group.account.email,
101+
accountUrl: group.account.url,
102+
orgID: org.id,
103+
orgName: org.name,
104+
active: !!info && info.id === group.account.id && info.active_org_id === org.id,
105+
})),
106+
)
107+
}),
94108
)
95109
return c.json({ orgs })
96110
},
@@ -115,7 +129,12 @@ export const ExperimentalRoutes = lazy(() =>
115129
validator("json", ConsoleSwitchBody),
116130
async (c) => {
117131
const body = c.req.valid("json")
118-
await Account.switchOrg(AccountID.make(body.accountID), OrgID.make(body.orgID))
132+
await AppRuntime.runPromise(
133+
Effect.gen(function* () {
134+
const account = yield* Account.Service
135+
yield* account.use(AccountID.make(body.accountID), Option.some(OrgID.make(body.orgID)))
136+
}),
137+
)
119138
return c.json(true)
120139
},
121140
)

0 commit comments

Comments
 (0)