Skip to content

Commit

Permalink
refactor: MainLIve (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Apr 27, 2023
1 parent 98a62cd commit 52ec7ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@effect/data": "^0.12.2",
"@effect/io": "^0.25.7",
"dfx": "^0.40.0",
"dfx": "^0.40.3",
"dotenv": "^16.0.3",
"openai": "^3.2.1"
}
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 9 additions & 11 deletions src/AutoThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
Layer,
Option,
Schedule,
Tag,
millis,
pipe,
seconds,
} from "bot/_common"
import { Discord, DiscordREST, Ix, Log, Perms, UI } from "dfx"
import { DiscordGateway } from "dfx/gateway"
import { DiscordGateway, runIx } from "dfx/gateway"

const retryPolicy = pipe(
Schedule.fixed(millis(500)),
Expand All @@ -25,7 +24,6 @@ const retryPolicy = pipe(
Schedule.whileOutput(Duration.lessThanOrEqualTo(seconds(3))),
)

// ==== errors
export class NotValidMessageError extends Data.TaggedClass(
"NotValidMessageError",
)<{
Expand Down Expand Up @@ -72,7 +70,7 @@ const make = Effect.gen(function* ($) {
pipe(
openai.generateTitle(content),
Effect.retry(retryPolicy),
Effect.tapError(log.info),
Effect.tapError(_ => log.info(_)),
),
),
Effect.orElseSucceed(() =>
Expand Down Expand Up @@ -200,15 +198,15 @@ const make = Effect.gen(function* ($) {
),
)

return {
ix: Ix.builder.add(archive).add(edit).add(editModal),
run: handleMessages,
} as const
const runInteractions = pipe(
Ix.builder.add(archive).add(edit).add(editModal),
runIx(Effect.catchAllCause(Effect.logErrorCause), { sync: false }),
)

yield* $(Effect.allPar(runInteractions, handleMessages))
})

export interface AutoThreads extends Effect.Effect.Success<typeof make> {}
export const AutoThreads = Tag<AutoThreads>()
export const AutoThreadsLive = Layer.provide(
ChannelsCacheLive,
Layer.scoped(AutoThreads, make),
Layer.effectDiscard(make),
)
40 changes: 10 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import { AutoThreads, AutoThreadsLive } from "bot/AutoThreads"
import { AutoThreadsLive } from "bot/AutoThreads"
import { BotLive } from "bot/Bot"
import * as OpenAI from "bot/OpenAI"
import { Config, Effect, Layer, pipe } from "bot/_common"
import { Intents, Ix } from "dfx"
import { DiscordGateway, makeLive, runIx } from "dfx/gateway"
import { Intents } from "dfx"
import { makeLive } from "dfx/gateway"
import * as Dotenv from "dotenv"
import { ChannelsCache, ChannelsCacheLive } from "./ChannelsCache.js"

Dotenv.config()

const program = Effect.gen(function* ($) {
const gateway = yield* $(DiscordGateway)
const channels = yield* $(ChannelsCache)
const autoThreads = yield* $(AutoThreads)

const runInteractions = pipe(
Ix.builder.concat(autoThreads.ix),
runIx(Effect.catchAllCause(Effect.logErrorCause)),
)

yield* $(
Effect.allParDiscard(
gateway.run,
channels.run,
autoThreads.run,
runInteractions,
),
)
})

const BotLive = makeLive({
const DiscordLive = makeLive({
token: Config.secret("DISCORD_BOT_TOKEN"),
gateway: {
intents: Config.succeed(
Expand All @@ -42,14 +22,14 @@ const OpenAILive = OpenAI.makeLayer({
organization: Config.optional(Config.secret("OPENAI_ORGANIZATION")),
})

const EnvLive = Layer.provideMerge(
BotLive,
Layer.merge(Layer.provide(OpenAILive, AutoThreadsLive), ChannelsCacheLive),
const MainLive = pipe(
DiscordLive,
Layer.merge(OpenAILive),
Layer.provide(Layer.merge(AutoThreadsLive, BotLive)),
)

pipe(
program,
Effect.provideLayer(EnvLive),
Layer.launch(MainLive),
Effect.tapErrorCause(Effect.logErrorCause),
Effect.runFork,
)

0 comments on commit 52ec7ef

Please sign in to comment.