Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use unique symbols for config tags #113

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"license": "ISC",
"devDependencies": {
"@octokit/types": "^12.3.0",
"@types/node": "^20.10.1",
"@types/node": "^20.10.2",
"tsc-watch": "^6.0.4",
"typescript": "^5.3.2"
},
"dependencies": {
"@effect/platform-node": "^0.33.4",
"@effect/schema": "^0.51.1",
"cron-parser": "^4.9.0",
"dfx": "^0.77.2",
"dfx": "^0.77.3",
"dotenv": "^16.3.1",
"effect": "2.0.0-next.58",
"gpt-tokenizer": "^2.1.2",
Expand Down
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

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

12 changes: 9 additions & 3 deletions src/AutoThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,16 @@ const make = ({ topicKeyword }: AutoThreadsOptions) =>
yield* _(handleMessages, Effect.forkScoped)
})

export const AutoThreadsOptions = Context.Tag<AutoThreadsOptions>()
export const layerOptions = LayerUtils.config(AutoThreadsOptions)
export interface AutoThreadsConfig {
readonly _: unique symbol
}
export const AutoThreadsConfig = Context.Tag<
AutoThreadsConfig,
AutoThreadsOptions
>()
export const layerConfig = LayerUtils.config(AutoThreadsConfig)
export const layer = Layer.scopedDiscard(
Effect.flatMap(AutoThreadsOptions, make),
Effect.flatMap(AutoThreadsConfig, make),
).pipe(
Layer.provide(ChannelsCacheLive),
Layer.provide(OpenAI.layer),
Expand Down
10 changes: 6 additions & 4 deletions src/Github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { OctokitResponse } from "@octokit/types"
import { LayerUtils } from "bot/_common"
import {
Chunk,
Config,
ConfigSecret,
Context,
Effect,
Expand All @@ -14,15 +13,15 @@ import {
} from "effect"
import { Octokit } from "octokit"

export interface GithubConfig {
export interface GithubConfigValue {
readonly token: ConfigSecret.ConfigSecret
}

export class GithubError extends Data.TaggedError("GithubError")<{
readonly reason: unknown
}> {}

const make = ({ token }: GithubConfig) => {
const make = ({ token }: GithubConfigValue) => {
const octokit = new Octokit({ auth: ConfigSecret.value(token) })

const rest = octokit.rest
Expand Down Expand Up @@ -66,7 +65,10 @@ const make = ({ token }: GithubConfig) => {
return { octokit, token, request, wrap, stream }
}

export const GithubConfig = Context.Tag<GithubConfig>()
export interface GithubConfig {
readonly _: unique symbol
}
export const GithubConfig = Context.Tag<GithubConfig, GithubConfigValue>()
export const layerConfig = LayerUtils.config(GithubConfig)

export interface Github extends ReturnType<typeof make> {}
Expand Down
11 changes: 7 additions & 4 deletions src/NoEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChannelsCache, ChannelsCacheLive } from "bot/ChannelsCache"
import { LayerUtils } from "bot/_common"
import { Discord, DiscordREST } from "dfx"
import { DiscordGateway, DiscordLive } from "dfx/gateway"
import { Config, Effect, Data, Layer, pipe, Context } from "effect"
import { Context, Effect, Layer, pipe } from "effect"

export interface NoEmbedOptions {
readonly topicKeyword: string
Expand Down Expand Up @@ -94,9 +94,12 @@ const make = ({ topicKeyword, urlWhitelist }: NoEmbedOptions) =>
)
})

export const NoEmbedOptions = Context.Tag<NoEmbedOptions>()
export const layerOptions = LayerUtils.config(NoEmbedOptions)
export interface NoEmbedConfig {
readonly _: unique symbol
}
export const NoEmbedConfig = Context.Tag<NoEmbedConfig, NoEmbedOptions>()
export const layerConfig = LayerUtils.config(NoEmbedConfig)

export const layer = Layer.effectDiscard(
Effect.flatMap(NoEmbedOptions, make),
Effect.flatMap(NoEmbedConfig, make),
).pipe(Layer.provide(ChannelsCacheLive), Layer.provide(DiscordLive))
10 changes: 6 additions & 4 deletions src/OpenAI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LayerUtils } from "bot/_common"
import * as Str from "bot/utils/String"
import {
Config,
ConfigSecret,
Context,
Data,
Expand Down Expand Up @@ -171,12 +170,15 @@ The title of this chat is "${title}".`,
} as const
}

export const OpenAIOptions = Context.Tag<OpenAIOptions>()
export const layerOptions = LayerUtils.config(OpenAIOptions)
export interface OpenAIConfig {
readonly _: unique symbol
}
export const OpenAIConfig = Context.Tag<OpenAIConfig, OpenAIOptions>()
export const layerConfig = LayerUtils.config(OpenAIConfig)

export interface OpenAI extends ReturnType<typeof make> {}
export const OpenAI = Context.Tag<OpenAI>()
export const layer = Layer.effect(OpenAI, Effect.map(OpenAIOptions, make))
export const layer = Layer.effect(OpenAI, Effect.map(OpenAIConfig, make))

const cleanTitle = (_: string) =>
pipe(Str.firstParagraph(_), Str.removeQuotes, Str.removePeriod)
Expand Down
6 changes: 1 addition & 5 deletions src/Summarizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { MemberCache, MemberCacheLive } from "bot/MemberCache"
import { Messages, MessagesLive } from "bot/Messages"
import { Http } from "bot/_common"
import { Discord, DiscordREST, Ix } from "dfx"
import {
DiscordIxLive,
InteractionsRegistry,
InteractionsRegistryLive,
} from "dfx/gateway"
import { DiscordIxLive, InteractionsRegistry } from "dfx/gateway"
import {
Cause,
Chunk,
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const DiscordConfigLive = DiscordConfig.layerConfig({
},
})

const OpenAIOptions = OpenAI.layerOptions({
const OpenAIOptions = OpenAI.layerConfig({
apiKey: Config.secret("OPENAI_API_KEY"),
organization: Config.option(Config.secret("OPENAI_ORGANIZATION")),
})

const AutoThreadsOptions = AutoThreads.layerOptions({
const AutoThreadsOptions = AutoThreads.layerConfig({
topicKeyword: Config.withDefault(
Config.string("AUTOTHREADS_KEYWORD"),
"[threads]",
),
})

const NoEmbedOptions = NoEmbed.layerOptions({
const NoEmbedOptions = NoEmbed.layerConfig({
topicKeyword: Config.withDefault(
Config.string("NOEMBED_KEYWORD"),
"[noembed]",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Layer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Config, Context, Effect, Layer } from "effect"

export const config =
<A>(tag: Context.Tag<A, A>) =>
<I, A>(tag: Context.Tag<I, A>) =>
(config: Config.Config.Wrap<A>) =>
Layer.effect(tag, Effect.config(Config.unwrap(config)))