Skip to content
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
2 changes: 2 additions & 0 deletions apps/cluster/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"@alchemy.run/sql": "catalog:",
"@effect/ai-openai-compat": "catalog:",
"@effect/ai-openrouter": "catalog:",
"@effect/chat-discord": "catalog:",
"@effect/chat-slack": "catalog:",
"@effect/platform-cloudflare": "catalog:",
"@effect/platform-node": "catalog:",
"@effect/sql-pg": "catalog:",
Expand Down
120 changes: 41 additions & 79 deletions apps/cluster/src/Chat/Discord/Api.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as DiscordChat from "@effect/chat-discord/DiscordChat"
import * as DiscordConfig from "dfx/DiscordConfig"
import { type DiscordREST, type DiscordRESTError, DiscordRESTLive } from "dfx/DiscordREST"
import { MemoryRateLimitStoreLive } from "dfx/RateLimit"
import type * as Discord from "dfx/types"
import * as DateTime from "effect/DateTime"
import type * as ChatMessages from "effect/chat/ChatMessage"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import type * as Redacted from "effect/Redacted"
import type * as HttpClient from "effect/http/HttpClient"
import type { ChatMessage } from "../Platform.ts"
import { type ChatMessage, type ChatThread, withoutMentions } from "../Platform.ts"

/**
* What Discord ingress and Discord's `ChatPlatform` share (EFF-3020): dfx's
* REST client, the bot's own id and how a Discord message becomes a
* `ChatMessage`.
* REST client, the bot's managed role and how an `effect/chat` message
* becomes a session's `ChatMessage`.
*/

/** dfx's REST client with the bot token, rate limited in memory. */
Expand All @@ -21,27 +21,9 @@ export const restLayer = (
): Layer.Layer<DiscordREST, never, HttpClient.HttpClient> =>
DiscordRESTLive.pipe(Layer.provide([DiscordConfig.layer({ token }), MemoryRateLimitStoreLive]))

/** The channel types the bot starts a thread in: text and announcement channels. */
export const THREAD_PARENT_TYPES: ReadonlySet<number> = new Set([0, 5])
/** Announcement, public and private threads. */
export const THREAD_TYPES: ReadonlySet<number> = new Set([10, 11, 12])
/** Plain messages and replies; joins, pins, thread starters and the like aren't. */
export const MESSAGE_TYPES: ReadonlySet<number> = new Set([0, 19])

/** Ask Discord for no link previews on the bot's posts. */
export const SUPPRESS_EMBEDS = 1 << 2

/** The bot's own user id, read once; a failed read is tried again next time. */
export const rememberBotUserId = (
rest: DiscordREST["Service"],
): Effect.Effect<string, DiscordRESTError> => {
let botId: string | undefined
return Effect.suspend(() =>
botId !== undefined
? Effect.succeed(botId)
: Effect.map(rest.getMyUser(), (user) => (botId = user.id)),
)
}
/** A session's Discord thread as an `effect/chat` address: its channel id. */
export const address = (thread: Extract<ChatThread, { readonly _tag: "Discord" }>) =>
DiscordChat.thread(thread.threadId)

/**
* The bot's managed role, identified by Discord's tag rather than its display
Expand Down Expand Up @@ -79,92 +61,72 @@ export const errorCode = (error: DiscordRESTError): number | undefined =>
error._tag === "ErrorResponse" ? error.data.code : undefined

/**
* A Discord attachment's id in a `ChatAttachment`: its channel, message and
* attachment ids. CDN links expire, so a download reads the message again for
* a fresh one.
* A Discord attachment's id in a `ChatAttachment`: its channel, then
* `effect/chat`'s id, the message and attachment ids. A download reads the
* message again, in its own channel, for a fresh CDN link.
*/
export const attachmentId = (channelId: string, messageId: string, id: string): string =>
`${channelId}/${messageId}/${id}`
export const attachmentId = (channelId: string, id: string): string => `${channelId}/${id}`

export const parseAttachmentId = (
id: string,
):
| { readonly channelId: string; readonly messageId: string; readonly attachmentId: string }
| undefined => {
): { readonly channelId: string; readonly id: string } | undefined => {
const [channelId, messageId, attachment, ...rest] = id.split("/")
return channelId && messageId && attachment && rest.length === 0
? { channelId, messageId, attachmentId: attachment }
? { channelId, id: `${messageId}/${attachment}` }
: undefined
}

/** The fields of a Discord message `toChatMessage` reads. */
export type DiscordMessage = Pick<
Discord.MessageResponse,
"id" | "channel_id" | "content" | "timestamp" | "attachments"
> & {
readonly author: Discord.UserResponse
readonly mentions: ReadonlyArray<{ readonly id: string }>
readonly mention_roles: ReadonlyArray<string>
/** The message a reply answers; null once it is deleted. */
readonly referenced_message?: { readonly author: { readonly id: string } } | null | undefined
}

/** The author's membership: `member` on a gateway message, a guild member read over REST, or none. */
/** The author's membership: on a gateway message or reaction, a guild member read over REST, or none. */
export interface DiscordMember {
readonly nick?: string | null | undefined
readonly roles?: ReadonlyArray<string> | undefined
}

/**
* The author is admitted with the role in `member.roles`; a bot account,
* the janitor bot included, is context only whatever its roles. The name is
* the server nickname, else the display name, else the username.
* the janitor bot included, is context only whatever its roles.
*/
export const toAuthor = (
user: Pick<Discord.UserResponse, "id" | "username" | "global_name" | "bot">,
author: { readonly id: string; readonly name?: string | undefined; readonly isBot: boolean },
member: DiscordMember | undefined,
roleId: string,
): ChatMessage["author"] => ({
id: user.id,
name: member?.nick ?? user.global_name ?? user.username,
class:
user.bot === true ? "bot" : member?.roles?.includes(roleId) === true ? "admitted" : "outsider",
id: author.id,
name: member?.nick || author.name || author.id,
class: author.isBot ? "bot" : member?.roles?.includes(roleId) === true ? "admitted" : "outsider",
})

/** Mentions of the bot, `<@id>` or the older `<@!id>`. */
const botMention = (botUserId: string) => new RegExp(`<@!?${botUserId}>`, "g")

/**
* The author as `toAuthor` classes them. A reply to one of the bot's
* messages counts as a mention: it is addressed to the bot whether or not
* the reply pings (EFF-3137).
* An `effect/chat` Discord message as a session's, with mentions of the bot
* and of its managed role removed. A reply to one of the bot's messages
* counts as a mention: it is addressed to the bot whether or not the reply
* pings (EFF-3137).
*/
export const toChatMessage = (
message: DiscordMessage,
member: DiscordMember | undefined,
roleId: string,
message: ChatMessages.ChatMessage,
author: ChatMessage["author"],
botUserId: string,
botRoleId?: string,
): ChatMessage => {
const mentionsBotRole = botRoleId !== undefined && message.mention_roles.includes(botRoleId)
const answers = message.referenced_message?.author.id
const isBot = (mention: ChatMessages.Mention) =>
mention.isSelf || (mention.kind === "role" && mention.id === botRoleId)
const answers = message.replyTo?.authorId
return {
id: message.id,
author: toAuthor(message.author, member, roleId),
text: (mentionsBotRole ? message.content.replaceAll(`<@&${botRoleId}>`, "") : message.content)
.replace(botMention(botUserId), "")
.trim(),
mentionsBot:
message.mentions.some((user) => user.id === botUserId) ||
mentionsBotRole ||
answers === botUserId,
sentAt: DateTime.makeUnsafe(message.timestamp),
author,
text: withoutMentions(message, isBot).trim(),
mentionsBot: message.mentions.some(isBot) || answers === botUserId,
sentAt: message.timestamp,
attachments: message.attachments.map((attachment) => ({
id: attachmentId(message.channel_id, message.id, attachment.id),
name: attachment.filename,
mediaType: attachment.content_type ?? "application/octet-stream",
size: attachment.size,
id: attachmentId(message.thread.id, attachment.id),
name: attachment.name ?? attachment.id,
mediaType: attachment.contentType ?? "application/octet-stream",
size: Math.max(0, Math.floor(attachment.size ?? 0)),
})),
...(answers === undefined ? {} : { answers }),
}
}

/** Whether the message mentions a role: only then is the bot's managed role read. */
export const mentionsRole = (message: ChatMessages.ChatMessage): boolean =>
message.mentions.some((mention) => mention.kind === "role")
14 changes: 6 additions & 8 deletions apps/cluster/src/Chat/Discord/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as Option from "effect/Option"
import * as References from "effect/References"
import * as FetchHttpClient from "effect/http/FetchHttpClient"
import * as Socket from "effect/socket/Socket"
import { restLayer } from "./Api.ts"
import { chatBotGate } from "../Config.ts"
import { DiscordBotConfig, discordSettings } from "./Config.ts"
import * as Ingress from "./Ingress.ts"
Expand All @@ -31,7 +30,8 @@ import * as Ingress from "./Ingress.ts"
* quiet.
*
* It is ingress only: each `MESSAGE_CREATE` and `MESSAGE_REACTION_ADD` goes
* through `Ingress` before its sequence is stored, so a message that
* through `@effect/chat-discord`'s `DiscordChat.dispatch` to `Ingress`'s
* `ChatHandler` before its sequence is stored, so a message that
* couldn't reach its session comes back with the RESUME, and the message
* ids already handled are kept here to drop the ones a RESUME replays. A
* 12-hour spike measured two evictions with a live socket in six hours,
Expand Down Expand Up @@ -206,7 +206,7 @@ export const ChatDiscordGatewayLive = (sessions: Layer.Layer<Ingress.DiscordSess
DiscordBotConfig.layerFrom(config),
sessions,
SeenLive,
restLayer(config.botToken).pipe(Layer.provide(FetchHttpClient.layer)),
FetchHttpClient.layer,
]),
),
),
Expand Down Expand Up @@ -384,11 +384,9 @@ export const ChatDiscordGatewayLive = (sessions: Layer.Layer<Ingress.DiscordSess
]),
// Runs before the sequence is stored: a failure reconnects and resumes from it.
Effect.provideService(DispatchHook, (payload) =>
payload.t === "MESSAGE_CREATE"
? Effect.orDie(ingress.handle(payload.d))
: payload.t === "MESSAGE_REACTION_ADD"
? Effect.orDie(ingress.handleReaction(payload.d))
: Effect.void,
payload.t === "MESSAGE_CREATE" || payload.t === "MESSAGE_REACTION_ADD"
? Effect.orDie(ingress.dispatch({ t: payload.t, d: payload.d }))
: Effect.void,
),
Effect.provideService(References.MinimumLogLevel, "Trace"),
Effect.provideService(References.CurrentLoggers, new Set([watcher])),
Expand Down
Loading