Skip to content

Commit

Permalink
feat: Summarizer (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored May 22, 2023
1 parent 3c24b86 commit 13b3d24
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 27 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.1.7",
"@types/node": "^20.2.3",
"prettier": "^2.8.8",
"tsc-watch": "^6.0.4",
"typescript": "^5.0.4"
},
"dependencies": {
"@effect-http/client": "^0.26.1",
"@effect/data": "^0.12.2",
"@effect/io": "^0.25.12",
"dfx": "^0.45.7",
"@effect/stream": "^0.21.1",
"dfx": "^0.45.8",
"dotenv": "^16.0.3",
"openai": "^3.2.1"
}
Expand Down
40 changes: 23 additions & 17 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/AutoThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const make = ({ topicKeyword }: AutoThreadsOptions) =>
),
Effect.catchTags({
NotValidMessageError: () => Effect.unit(),
DiscordRESTError: logRESTError,
DiscordRESTError: logRESTError(log),
}),
Effect.catchAllCause(Effect.logErrorCause),
),
Expand Down
53 changes: 53 additions & 0 deletions src/MemberCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
Cache,
Data,
Duration,
Effect,
Layer,
Request,
RequestResolver,
Tag,
pipe,
} from "bot/_common"
import { Discord, DiscordREST } from "dfx"
import { DiscordRESTError, ResponseDecodeError } from "dfx/DiscordREST"

export class GetMember extends Data.TaggedClass("GetMember")<{
readonly guildId: Discord.Snowflake
readonly userId: Discord.Snowflake
}> {}

const make = Effect.gen(function* (_) {
const rest = yield* _(DiscordREST)

interface GetMember
extends Request.Request<
DiscordRESTError | ResponseDecodeError,
Discord.GuildMember
> {
readonly _tag: "GetMember"
readonly guildId: Discord.Snowflake
readonly userId: Discord.Snowflake
}
const GetMember = Request.tagged<GetMember>("GetMember")

const resolver = RequestResolver.fromFunctionEffect(
({ guildId, userId }: GetMember) =>
Effect.flatMap(rest.getGuildMember(guildId, userId), _ => _.json),
)

const cache = yield* _(Request.makeCache(1000, Duration.days(1)))

return {
get: (guildId: Discord.Snowflake, userId: Discord.Snowflake) =>
pipe(
Effect.request(GetMember({ guildId, userId }), resolver),
Effect.withRequestCache(cache),
Effect.withRequestCaching("on"),
),
} as const
})

export interface MemberCache extends Effect.Effect.Success<typeof make> {}
export const MemberCache = Tag<MemberCache>()
export const MemberCacheLive = Layer.effect(MemberCache, make)
5 changes: 3 additions & 2 deletions src/Mentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OpenAI, OpenAIMessage } from "bot/OpenAI"
import { Data, Effect, Layer, pipe } from "bot/_common"
import { logRESTError } from "bot/utils/Errors"
import * as Str from "bot/utils/String"
import { Discord, DiscordREST } from "dfx"
import { Discord, DiscordREST, Log } from "dfx"
import { DiscordGateway } from "dfx/DiscordGateway"

class NonEligibleMessage extends Data.TaggedClass("NonEligibleMessage")<{
Expand All @@ -15,6 +15,7 @@ const make = Effect.gen(function* (_) {
const gateway = yield* _(DiscordGateway)
const channels = yield* _(ChannelsCache)
const openai = yield* _(OpenAI)
const log = yield* _(Log.Log)

const botUser = yield* _(
rest.getCurrentUser(),
Expand Down Expand Up @@ -96,7 +97,7 @@ ${msg.content}`,
Effect.catchTags({
NonEligibleMessage: _ => Effect.unit(),
NoSuchElementException: _ => Effect.unit(),
DiscordRESTError: logRESTError,
DiscordRESTError: logRESTError(log),
}),
Effect.catchAllCause(Effect.logErrorCause),
),
Expand Down
5 changes: 3 additions & 2 deletions src/NoEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChannelsCache, ChannelsCacheLive } from "bot/ChannelsCache"
import { Config, Data, Effect, Layer, pipe } from "bot/_common"
import { logRESTError } from "bot/utils/Errors"
import { Discord, DiscordREST } from "dfx"
import { Discord, DiscordREST, Log } from "dfx"
import { DiscordGateway } from "dfx/gateway"

class NotValidMessageError extends Data.TaggedClass("NotValidMessageError")<{
Expand All @@ -17,6 +17,7 @@ const make = ({ topicKeyword }: NoEmbedOptions) =>
const gateway = yield* _(DiscordGateway)
const rest = yield* _(DiscordREST)
const channels = yield* _(ChannelsCache)
const log = yield* _(Log.Log)

const getChannel = (guildId: string, id: string) =>
Effect.flatMap(channels.get(guildId, id), _ =>
Expand Down Expand Up @@ -57,7 +58,7 @@ const make = ({ topicKeyword }: NoEmbedOptions) =>
),
Effect.catchTags({
NotValidMessageError: () => Effect.unit(),
DiscordRESTError: logRESTError,
DiscordRESTError: logRESTError(log),
}),
Effect.catchAllCause(Effect.logErrorCause),
)
Expand Down
Loading

0 comments on commit 13b3d24

Please sign in to comment.