Skip to content

Commit 4bf805a

Browse files
kitlangtonbalcsida
authored andcommitted
effectify Installation service, drop Effect suffix from namespaces (anomalyco#18266)
1 parent 85011c5 commit 4bf805a

16 files changed

Lines changed: 508 additions & 327 deletions

File tree

packages/opencode/src/account/effect.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { AccountRepo, type AccountRow } from "./repo"
66
import {
77
type AccountError,
88
AccessToken,
9-
Account,
109
AccountID,
1110
DeviceCode,
11+
Info,
1212
RefreshToken,
1313
AccountServiceError,
1414
Login,
@@ -24,10 +24,30 @@ import {
2424
UserCode,
2525
} from "./schema"
2626

27-
export * from "./schema"
27+
export {
28+
AccountID,
29+
type AccountError,
30+
AccountRepoError,
31+
AccountServiceError,
32+
AccessToken,
33+
RefreshToken,
34+
DeviceCode,
35+
UserCode,
36+
Info,
37+
Org,
38+
OrgID,
39+
Login,
40+
PollSuccess,
41+
PollPending,
42+
PollSlow,
43+
PollExpired,
44+
PollDenied,
45+
PollError,
46+
PollResult,
47+
} from "./schema"
2848

2949
export type AccountOrgs = {
30-
account: Account
50+
account: Info
3151
orgs: readonly Org[]
3252
}
3353

@@ -108,10 +128,10 @@ const mapAccountServiceError =
108128
),
109129
)
110130

111-
export namespace AccountEffect {
131+
export namespace Account {
112132
export interface Interface {
113-
readonly active: () => Effect.Effect<Option.Option<Account>, AccountError>
114-
readonly list: () => Effect.Effect<Account[], AccountError>
133+
readonly active: () => Effect.Effect<Option.Option<Info>, AccountError>
134+
readonly list: () => Effect.Effect<Info[], AccountError>
115135
readonly orgsByAccount: () => Effect.Effect<readonly AccountOrgs[], AccountError>
116136
readonly remove: (accountID: AccountID) => Effect.Effect<void, AccountError>
117137
readonly use: (accountID: AccountID, orgID: Option.Option<OrgID>) => Effect.Effect<void, AccountError>

packages/opencode/src/account/index.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import { Effect, Option } from "effect"
22

3-
import {
4-
Account as AccountSchema,
5-
type AccountError,
6-
type AccessToken,
7-
AccountID,
8-
AccountEffect,
9-
OrgID,
10-
} from "./effect"
3+
import { Account as S, type AccountError, type AccessToken, AccountID, Info as Model, OrgID } from "./effect"
114

125
export { AccessToken, AccountID, OrgID } from "./effect"
136

147
import { runtime } from "@/effect/runtime"
158

16-
function runSync<A>(f: (service: AccountEffect.Interface) => Effect.Effect<A, AccountError>) {
17-
return runtime.runSync(AccountEffect.Service.use(f))
9+
function runSync<A>(f: (service: S.Interface) => Effect.Effect<A, AccountError>) {
10+
return runtime.runSync(S.Service.use(f))
1811
}
1912

20-
function runPromise<A>(f: (service: AccountEffect.Interface) => Effect.Effect<A, AccountError>) {
21-
return runtime.runPromise(AccountEffect.Service.use(f))
13+
function runPromise<A>(f: (service: S.Interface) => Effect.Effect<A, AccountError>) {
14+
return runtime.runPromise(S.Service.use(f))
2215
}
2316

2417
export namespace Account {
25-
export const Account = AccountSchema
26-
export type Account = AccountSchema
18+
export const Info = Model
19+
export type Info = Model
2720

28-
export function active(): Account | undefined {
21+
export function active(): Info | undefined {
2922
return Option.getOrUndefined(runSync((service) => service.active()))
3023
}
3124

packages/opencode/src/account/repo.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Effect, Layer, Option, Schema, ServiceMap } from "effect"
33

44
import { Database } from "@/storage/db"
55
import { AccountStateTable, AccountTable } from "./account.sql"
6-
import { AccessToken, Account, AccountID, AccountRepoError, OrgID, RefreshToken } from "./schema"
6+
import { AccessToken, AccountID, AccountRepoError, Info, OrgID, RefreshToken } from "./schema"
77

88
export type AccountRow = (typeof AccountTable)["$inferSelect"]
99

@@ -13,8 +13,8 @@ const ACCOUNT_STATE_ID = 1
1313

1414
export namespace AccountRepo {
1515
export interface Service {
16-
readonly active: () => Effect.Effect<Option.Option<Account>, AccountRepoError>
17-
readonly list: () => Effect.Effect<Account[], AccountRepoError>
16+
readonly active: () => Effect.Effect<Option.Option<Info>, AccountRepoError>
17+
readonly list: () => Effect.Effect<Info[], AccountRepoError>
1818
readonly remove: (accountID: AccountID) => Effect.Effect<void, AccountRepoError>
1919
readonly use: (accountID: AccountID, orgID: Option.Option<OrgID>) => Effect.Effect<void, AccountRepoError>
2020
readonly getRow: (accountID: AccountID) => Effect.Effect<Option.Option<AccountRow>, AccountRepoError>
@@ -40,7 +40,7 @@ export class AccountRepo extends ServiceMap.Service<AccountRepo, AccountRepo.Ser
4040
static readonly layer: Layer.Layer<AccountRepo> = Layer.effect(
4141
AccountRepo,
4242
Effect.gen(function* () {
43-
const decode = Schema.decodeUnknownSync(Account)
43+
const decode = Schema.decodeUnknownSync(Info)
4444

4545
const query = <A>(f: (db: DbClient) => A) =>
4646
Effect.try({

packages/opencode/src/account/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const UserCode = Schema.String.pipe(
3838
)
3939
export type UserCode = Schema.Schema.Type<typeof UserCode>
4040

41-
export class Account extends Schema.Class<Account>("Account")({
41+
export class Info extends Schema.Class<Info>("Account")({
4242
id: AccountID,
4343
email: Schema.String,
4444
url: Schema.String,

packages/opencode/src/auth/effect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const file = path.join(Global.Path.data, "auth.json")
3737

3838
const fail = (message: string) => (cause: unknown) => new AuthError({ message, cause })
3939

40-
export namespace AuthEffect {
40+
export namespace Auth {
4141
export interface Interface {
4242
readonly get: (providerID: string) => Effect.Effect<Info | undefined, AuthError>
4343
readonly all: () => Effect.Effect<Record<string, Info>, AuthError>

packages/opencode/src/auth/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as S from "./effect"
55

66
export { OAUTH_DUMMY_KEY } from "./effect"
77

8-
function runPromise<A>(f: (service: S.AuthEffect.Interface) => Effect.Effect<A, S.AuthError>) {
9-
return runtime.runPromise(S.AuthEffect.Service.use(f))
8+
function runPromise<A>(f: (service: S.Auth.Interface) => Effect.Effect<A, S.AuthError>) {
9+
return runtime.runPromise(S.Auth.Service.use(f))
1010
}
1111

1212
export namespace Auth {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { cmd } from "./cmd"
22
import { Duration, Effect, Match, Option } from "effect"
33
import { UI } from "../ui"
44
import { runtime } from "@/effect/runtime"
5-
import { AccountID, AccountEffect, OrgID, PollExpired, type PollResult } from "@/account/effect"
5+
import { AccountID, Account, OrgID, PollExpired, type PollResult } from "@/account/effect"
66
import { type AccountError } from "@/account/schema"
77
import * as Prompt from "../effect/prompt"
88
import open from "open"
@@ -17,7 +17,7 @@ const isActiveOrgChoice = (
1717
) => Option.isSome(active) && active.value.id === choice.accountID && active.value.active_org_id === choice.orgID
1818

1919
const loginEffect = Effect.fn("login")(function* (url: string) {
20-
const service = yield* AccountEffect.Service
20+
const service = yield* Account.Service
2121

2222
yield* Prompt.intro("Log in")
2323
const login = yield* service.login(url)
@@ -58,7 +58,7 @@ const loginEffect = Effect.fn("login")(function* (url: string) {
5858
})
5959

6060
const logoutEffect = Effect.fn("logout")(function* (email?: string) {
61-
const service = yield* AccountEffect.Service
61+
const service = yield* Account.Service
6262
const accounts = yield* service.list()
6363
if (accounts.length === 0) return yield* println("Not logged in")
6464

@@ -98,7 +98,7 @@ interface OrgChoice {
9898
}
9999

100100
const switchEffect = Effect.fn("switch")(function* () {
101-
const service = yield* AccountEffect.Service
101+
const service = yield* Account.Service
102102

103103
const groups = yield* service.orgsByAccount()
104104
if (groups.length === 0) return yield* println("Not logged in")
@@ -129,7 +129,7 @@ const switchEffect = Effect.fn("switch")(function* () {
129129
})
130130

131131
const orgsEffect = Effect.fn("orgs")(function* () {
132-
const service = yield* AccountEffect.Service
132+
const service = yield* Account.Service
133133

134134
const groups = yield* service.orgsByAccount()
135135
if (groups.length === 0) return yield* println("No accounts found")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ export const UpgradeCommand = {
5858
spinner.stop("Upgrade failed", 1)
5959
if (err instanceof Installation.UpgradeFailedError) {
6060
// necessary because choco only allows install/upgrade in elevated terminals
61-
if (method === "choco" && err.data.stderr.includes("not running from an elevated command shell")) {
61+
if (method === "choco" && err.stderr.includes("not running from an elevated command shell")) {
6262
prompts.log.error("Please run the terminal as Administrator and try again")
6363
} else {
64-
prompts.log.error(err.data.stderr)
64+
prompts.log.error(err.stderr)
6565
}
6666
} else if (err instanceof Error) prompts.log.error(err.message)
6767
prompts.outro("Done")

packages/opencode/src/effect/runtime.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { Effect, Layer, ManagedRuntime } from "effect"
2-
import { AccountEffect } from "@/account/effect"
3-
import { AuthEffect } from "@/auth/effect"
2+
import { Account } from "@/account/effect"
3+
import { Auth } from "@/auth/effect"
44
import { Instances } from "@/effect/instances"
55
import type { InstanceServices } from "@/effect/instances"
6-
import { TruncateEffect } from "@/tool/truncate-effect"
6+
import { Installation } from "@/installation"
7+
import { Truncate } from "@/tool/truncate-effect"
78
import { Instance } from "@/project/instance"
89

910
export const runtime = ManagedRuntime.make(
1011
Layer.mergeAll(
11-
AccountEffect.defaultLayer, //
12-
TruncateEffect.defaultLayer,
12+
Account.defaultLayer, //
13+
Installation.defaultLayer,
14+
Truncate.defaultLayer,
1315
Instances.layer,
14-
).pipe(Layer.provideMerge(AuthEffect.layer)),
16+
).pipe(Layer.provideMerge(Auth.layer)),
1517
)
1618

1719
export function runPromiseInstance<A, E>(effect: Effect.Effect<A, E, InstanceServices>) {

0 commit comments

Comments
 (0)