diff --git a/.changeset/config.json b/.changeset/config.json index 0d7c5d7293b..67552f02d7a 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -23,6 +23,10 @@ "@effect/ai-openai-compat", "@effect/ai-openrouter", "@effect/atom-react", + "@effect/chat-slack", + "@effect/chat-whatsapp", + "@effect/chat-discord", + "@effect/chat-telegram", "@effect/atom-solid", "@effect/atom-vue", "@effect/docgen", diff --git a/.changeset/effect-chat.md b/.changeset/effect-chat.md new file mode 100644 index 00000000000..857b94d55a8 --- /dev/null +++ b/.changeset/effect-chat.md @@ -0,0 +1,13 @@ +--- +"effect": patch +"@effect/chat-slack": patch +"@effect/chat-discord": patch +"@effect/chat-whatsapp": patch +"@effect/chat-telegram": patch +--- + +Add unstable `effect/chat` APIs and Slack, Discord, WhatsApp and Telegram adapters for chat bots, with grapheme-safe Markdown streaming, buttons, selects, modals, slash commands, private/deferred command replies, and adapter conformance testing. + +Slack webhook and interactivity layers, and the WhatsApp webhook layer, require a `Mac` service. Provide a runtime layer such as `NodeCrypto.layerMac`, or `Mac.layerWebCrypto` with `SecureRandom.layerWebCrypto`. + +`Jwks.makeRemote` accepts a `filter` option that drops raw keys before they are decoded. diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 3380c03bde6..1bb71e099cd 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -164,7 +164,8 @@ jobs: node: "" deno: "" bun: "" - command: pnpm test --max-concurrency=10 + # The full suite exceeds Node's default ~2 GiB heap on this runner. + command: NODE_OPTIONS=--max-old-space-size=4096 pnpm test --max-concurrency=10 # Node 25 and older handle upgrade request bodies differently. - runtime: Node 24 (platform-node) node: 24 diff --git a/.github/workflows/snapshot.yml b/.github/workflows/snapshot.yml index 69e9e25288b..8ff8b2256ea 100644 --- a/.github/workflows/snapshot.yml +++ b/.github/workflows/snapshot.yml @@ -46,4 +46,4 @@ jobs: run: pnpm build - name: Create snapshot id: snapshot - run: pnpm exec pkg-pr-new publish --pnpm --comment=off ./packages/* ./packages/atom/* ./packages/ai/* ./packages/platform/* ./packages/sql/* ./packages/tools/* + run: pnpm exec pkg-pr-new publish --pnpm --comment=off ./packages/* ./packages/atom/* ./packages/ai/* ./packages/chat/* ./packages/platform/* ./packages/sql/* ./packages/tools/* diff --git a/README.md b/README.md index 23384e97a57..590e44bec4b 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,10 @@ This monorepo contains the core `effect` package alongside integration packages | [`@effect/ai-typesafe`](packages/ai/typesafe) | TypeSafe decision provider for the Effect AI modules | [docs](https://effect.website/docs/v4/api/ai-typesafe) | | [`@effect/ai-openai-compat`](packages/ai/openai-compat) | OpenAI-compatible API provider for the Effect AI modules | [docs](https://effect.website/docs/v4/api/ai-openai-compat) | | [`@effect/ai-openrouter`](packages/ai/openrouter) | OpenRouter provider for the Effect AI modules | [docs](https://effect.website/docs/v4/api/ai-openrouter) | +| [`@effect/chat-slack`](packages/chat/slack) | Slack adapter for `effect/chat` | [docs](https://effect.website/docs/v4/api/chat-slack) | +| [`@effect/chat-whatsapp`](packages/chat/whatsapp) | WhatsApp Cloud API adapter for `effect/chat` | [docs](https://effect.website/docs/v4/api/chat-whatsapp) | +| [`@effect/chat-discord`](packages/chat/discord) | Discord adapter for `effect/chat` | [docs](https://effect.website/docs/v4/api/chat-discord) | +| [`@effect/chat-telegram`](packages/chat/telegram) | Telegram Bot API adapter for `effect/chat` | [docs](https://effect.website/docs/v4/api/chat-telegram) | | [`@effect/atom-react`](packages/atom/react) | React bindings for Effect Atom | [docs](https://effect.website/docs/v4/api/atom-react) | | [`@effect/atom-solid`](packages/atom/solid) | SolidJS bindings for Effect Atom | [docs](https://effect.website/docs/v4/api/atom-solid) | | [`@effect/atom-vue`](packages/atom/vue) | Vue bindings for Effect Atom | [docs](https://effect.website/docs/v4/api/atom-vue) | diff --git a/deno.json b/deno.json index ca3138a08f9..632b786f0fd 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "$schema": "https://raw.githubusercontent.com/denoland/deno/refs/heads/main/cli/schemas/config-file.v1.json", "nodeModulesDir": "manual", "unstable": ["bare-node-builtins", "node-globals", "net"], - "workspace": ["./packages/*", "./packages/platform/*"], + "workspace": ["./packages/*", "./packages/chat/*", "./packages/platform/*"], "exclude": [ "**/*.mjs", "**/*.cjs", diff --git a/packages/chat/discord/README.md b/packages/chat/discord/README.md new file mode 100644 index 00000000000..46a2a89ec96 --- /dev/null +++ b/packages/chat/discord/README.md @@ -0,0 +1,27 @@ +# @effect/chat-discord + +A [Discord](https://discord.com) adapter for `effect/chat`, built on [dfx](https://github.com/tim-smart/dfx). Includes structural entries for gateway dispatches and interactions, pure normalisers, default gateway layers, buttons, selects and modals, and every adapter op over dfx's REST client. + +The adapter reads message text, so the application needs the privileged **Message Content** intent, switched on in Discord's developer portal and requested in your dfx gateway config. Without it, message text is empty unless the message mentions the bot, and attachments are missing. + +## Installation + +```sh +npm install effect dfx @effect/chat-discord +``` + +## Links + +- [Website](https://effect.website): documentation, guides, and news. +- [Reference](https://effect.website/docs/v4/api/chat-discord): API documentation for this package. +- [Discord](https://discord.gg/effect-ts): ask questions, share what you're building, and talk to the core team. +- [Community](https://effect.website/community-hub): meetups and events, or bring Effect to your own. +- [Issues](https://github.com/Effect-TS/effect/issues): bug reports and feature requests. + +## Let's talk + +Whether your team is considering Effect, rolling it out, or already running it in production, we'd love to hear from you: what you're building, what works, and what you need from Effect next. + +- **Talk to the maintainers.** Introduce your team on [Discord](https://discord.gg/effect-ts) or email [contact@effectful.co](mailto:contact@effectful.co). We're happy to connect privately on Slack or Discord for feedback and help with adoption. +- **Production support.** We're exploring how to better support teams running Effect in production. If your organization has specific support needs, let's discuss them. +- **Adoption help.** Our [adoption partners](https://effect.website/adoption-partners) offer implementation, consulting, team extension, training, and commercial support. diff --git a/packages/chat/discord/package.json b/packages/chat/discord/package.json new file mode 100644 index 00000000000..6edde31f495 --- /dev/null +++ b/packages/chat/discord/package.json @@ -0,0 +1,71 @@ +{ + "name": "@effect/chat-discord", + "version": "4.1.4", + "type": "module", + "license": "MIT", + "description": "The Discord adapter for effect/chat", + "homepage": "https://effect.website", + "repository": { + "type": "git", + "url": "https://github.com/Effect-TS/effect.git", + "directory": "packages/chat/discord" + }, + "bugs": { + "url": "https://github.com/Effect-TS/effect/issues" + }, + "tags": [ + "typescript", + "chat", + "discord" + ], + "keywords": [ + "typescript", + "chat", + "discord" + ], + "sideEffects": [], + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts", + "./*": "./src/*.ts", + "./internal/*": null, + "./index": null, + "./*/index": null + }, + "files": [ + "src/**/*.ts", + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map", + "AGENTS.md", + "CLAUDE.md", + "ai-docs/**/*" + ], + "publishConfig": { + "access": "public", + "provenance": true, + "exports": { + "./package.json": "./package.json", + ".": "./dist/index.js", + "./*": "./dist/*.js", + "./internal/*": null, + "./index": null, + "./*/index": null + } + }, + "scripts": { + "codegen": "effect-utils codegen", + "build": "tsc -b tsconfig.json && pnpm babel", + "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps", + "check": "tsc -b tsconfig.json" + }, + "devDependencies": { + "dfx": "^1.1.1", + "effect": "workspace:^" + }, + "peerDependencies": { + "dfx": "^1.1.1", + "effect": "workspace:^" + } +} diff --git a/packages/chat/discord/src/DiscordChat.ts b/packages/chat/discord/src/DiscordChat.ts new file mode 100644 index 00000000000..e90c33eb19a --- /dev/null +++ b/packages/chat/discord/src/DiscordChat.ts @@ -0,0 +1,687 @@ +/** + * The Discord adapter for `effect/chat`: the `Registry` layer, the + * structural dispatch and interaction entries, the pure normalisers, the + * gateway layers, address helpers and the scoped post options. It runs on + * dfx: every REST call goes through the `DiscordREST` in context. + * + * **Details** + * + * - Gateway intents: `GUILDS`, `GUILD_MESSAGES`, `GUILD_MESSAGE_REACTIONS`, + * `DIRECT_MESSAGES`, `DIRECT_MESSAGE_REACTIONS` and `MESSAGE_CONTENT`. + * They are set in the consumer's dfx config. + * - `MESSAGE_CONTENT` is a privileged intent: switch it on for the + * application in Discord's developer portal. Without it, `text` is empty + * on every message that doesn't mention the bot, and attachments are + * missing. + * - Bot permissions: View Channel, Send Messages, Send Messages in Threads, + * Create Public Threads, Read Message History and Attach Files. + * - Slash commands need the `applications.commands` scope, and are + * registered by the consumer with the payloads `commandRequests` gives. + * + * @stability unstable + * @since 4.1.5 + */ +import { DiscordConfig } from "dfx/DiscordConfig" +import { DiscordGateway } from "dfx/DiscordGateway" +import { DispatchHook } from "dfx/DiscordGateway/Shard/DispatchHook" +import { DiscordREST } from "dfx/DiscordREST" +import * as Cause from "effect/Cause" +import { Registry } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import type { ChatEvent, MessageCreated, ReactionAdded } from "effect/chat/ChatEvent" +import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ParseMention } from "effect/chat/ChatTest" +import type { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import { dual } from "effect/Function" +import * as HttpClient from "effect/http/HttpClient" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Result from "effect/Result" +import * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import * as Adapter from "./internal/adapter.ts" +import * as Errors from "./internal/errors.ts" +import * as Interactions from "./internal/interactions.ts" +import * as Mentions from "./internal/mentions.ts" +import * as Normalize from "./internal/normalize.ts" +import { CurrentPostOptions } from "./internal/options.ts" + +/** + * The Discord side of the adapter, for what the core has no op for. + * + * **Details** + * + * - `botUserId` comes from `GET /users/@me`, read once. A failed read is + * tried again next time. + * - `dispatch` takes one gateway dispatch, `{ t, d }`, and hands its + * events to the `ChatHandler` one at a time. It reads `d` with a Schema + * and ignores what it doesn't know, so it takes no dfx type. + * - When a `MESSAGE_CREATE` carries no `channel_type`, `dispatch` looks + * the channel up, cached and with a five second timeout. A failed lookup + * is logged and fails with `NotAccepted`. A lookup that would fail the same + * way again, such as a deleted channel, is logged and the event dropped. + * - `interaction` takes one `INTERACTION_CREATE` payload, from the gateway + * or from an HTTP interactions endpoint. It reads it with a Schema, so it + * takes no dfx type, and ignores an interaction it can't express: a + * message or user command, an autocomplete request, a slash command with + * anything but at most one string option. It calls the handler through + * `ChatInteraction.handle` and answers through the interaction callback + * endpoint, which has to land inside Discord's 3 seconds: `deadline` is + * the handler's share, 2 seconds by default. + * - A click is answered with callback type 6, or type 9 for `OpenModal`. + * A submit's `close` is type 6. An `errors` reply is type 4 with the + * ephemeral flag, listing each message as plain text, and the modal closes: Discord + * can't show an error inside a modal, so the descriptor has no + * `modalErrors`. + * - A slash command goes to `Handler.command`. `respond` is callback type + * 4 with the message a `post` sends, `defer` type 5 and `OpenModal` type + * 9, each with the ephemeral flag when private. An `Ack`, which is what a + * command nobody handles gets, sends nothing, so a command another library + * owns is left to it. `withPostOptions` does not reach a command's + * answer, `respond` or `followUp`: a mention in it renders and notifies + * nobody. + * - Commands were recorded in a guild channel over the gateway. A command + * in a direct is from Discord's docs and was not run. + * - A click on a private answer carries the message, with the ephemeral flag + * (64). Its `messageId` tells which answer was clicked. Discord's docs + * reach an interaction's messages only through its token, under + * `/webhooks`, and say nothing of the channel routes `Chat.edit`, + * `delete` and `fetchMessage` use on an ephemeral message. Treat the id + * as one no message op works with. This is from the docs: no op on it was + * run. + * - `followUp` edits the deferred command's original response, with the + * application id and token from its `raw`, so a second call replaces the + * first answer. It fails `NotFound` with detail `interaction_expired` + * from 15 minutes after the command, read from the interaction's id, + * without calling Discord. Otherwise it makes one HTTP request, through + * the layer's `HttpClient` and not dfx's client, and never sends it + * again, unless that `HttpClient` retries by itself: a 429 fails + * `RateLimited`, for the caller to retry or not. + * Discord answers 401 with code 50027 past 15 minutes, which is + * `interaction_expired` too. Code 10015 is mapped from the docs and was + * not seen. + * - A modal that can't be built, a reply to a command that fails the checks + * of a `post`, or a callback that fails, goes to `Handler.refused`. A + * callback that comes back 404 `Unknown interaction` was late: that is + * `NotFound` with detail `interaction_expired`. A failed ack is logged. + * - `guild` reads the channel, cached. `None` for a direct. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class DiscordChat extends Context.Service + readonly dispatch: (dispatch: { + readonly t: string + readonly d: unknown + }) => Effect.Effect + readonly interaction: (interaction: unknown, options?: { + /** The handler's share of Discord's 3 seconds. Default 2 seconds. */ + readonly deadline?: Duration.Duration | undefined + }) => Effect.Effect + readonly guild: (thread: ChatThread) => Effect.Effect, ChatError> +}>()("@effect/chat-discord/DiscordChat") {} + +/** The dispatches `normalize` reads. */ +const handled = new Set(["MESSAGE_CREATE", "MESSAGE_REACTION_ADD", "MESSAGE_REACTION_REMOVE"]) + +const lookupTimeout = Duration.seconds(5) + +/** + * Registers the Discord adapter in the `Registry`. + * + * **Details** + * + * - Every REST call but `followUp` goes through the `DiscordREST` in + * context. dfx waits on its rate-limit buckets and sends again after a + * 429, so a rate limit shows up as a slower call. + * - `HttpClient` downloads attachments from Discord's CDN, with no token, + * and sends `followUp`'s edit, which needs no bot token either. That edit + * skips dfx's client so a 429 is not sent again past the 15 minutes. It + * goes to the `rest.baseUrl` of the `DiscordConfig` in context, or + * Discord's own API when there is none. + * - Content goes out as written: Discord reads Markdown itself. Every write + * sends `allowed_mentions: { parse: [] }` and the `SUPPRESS_EMBEDS` + * flag, on the post and on every edit, unless `withPostOptions` says + * otherwise. A reply does not ping its author. + * - `post` and `edit` fail with `TooLarge` before calling Discord past + * 2,000 code points. + * - A refused file fails `denied` with detail `file_refused` and leaves + * nothing posted: more than 10 files or 10 MiB, by the adapter's own + * check, or any `denied` answer to a post with files. The reason Discord + * gave is the `cause`. + * - A message's elements go out as classic components beside `content`, + * in action rows: five buttons to a row and a select alone in its row, in + * the order given, up to five rows. More fail `TooLarge` before the call. + * An `edit` without `actions` removes the ones there were. + * - A button's id and encoded value share one `custom_id`, joined by one + * character, and so do a modal's id and its metadata. An id may not + * contain U+241F, the join. `limits.actionDataLength` and + * `limits.modalMetadataLength` are 99 code points, the 100 of + * `custom_id` less the join. + * - Button styles: `default` is secondary, `primary` primary and + * `danger` danger. + * - A modal's inputs are Label components around a text input or a string + * select, and a `text` is a text display. Inputs and texts together are + * 5 at most. A modal over that, or with a title or label over 45 + * characters, reaches `Handler.refused` as `TooLarge`. Discord has no + * submit label, so `submitLabel` is not shown. + * - `replyTo` sends a message reference. Discord refuses a reply to a + * message that is gone, unless `withPostOptions({ failIfNotExists: false })` + * lets it through as a plain message. + * - Only `DEFAULT`, `REPLY`, `CHAT_INPUT_COMMAND` and + * `CONTEXT_MENU_COMMAND` messages are messages. A forward, a poll, a + * sticker or a components-only message is `DEFAULT` and comes through, + * often with `text: ""`. Its content stays on `raw`. + * - A deferred command reply arrives empty. Its text comes in a later edit, + * which is not delivered, so only `history` and `fetchMessage` have it. + * - A webhook message has the webhook id as its author id and `isBot`. + * `getUser` on that id fails `NotFound`. + * - A mention is reported when its token is in `content` outside code and + * Discord says it resolved. A reply that pings its author is not a + * mention. Only the bot's user mention is `isSelf`: a mention of its + * managed role is the consumer's to recognise. + * - The address of a thread is its channel id. `startThread` names the + * thread after the message's text, cut to 100 characters, and gives the + * existing thread when the message already has one. + * - `history` of a thread started from a message puts the opener, from the + * parent channel, in place of Discord's placeholder. When the opener is + * deleted it starts at the first reply. A forum post's starter is in the + * thread itself. + * - `getUser` with `in` gives the guild nick, and its `raw` is Discord's + * guild member, whose `roles` are the member's role ids. Without `in`, in + * a direct, or for someone who left the guild, `raw` is the user. + * - The layer provides the `DiscordChat` service as well as registering the + * adapter, and makes every call through the `DiscordREST` it is given. A + * consumer that needs more of Discord than `Chat` has reads either from + * context beside `Chat`. dfx keeps one rate-limit state for both. + * - There is no native streaming: streamed replies post, then edit, at + * least 1.5 seconds apart. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer: Layer.Layer = Layer.effect( + DiscordChat, + Effect.gen(function*() { + const rest = yield* DiscordREST + const http = yield* HttpClient.HttpClient + const registry = yield* Registry + let cached: string | undefined + const botUserId: Effect.Effect = Effect.suspend(() => { + if (cached !== undefined) return Effect.succeed(cached) + return rest.getMyUser().pipe( + Effect.mapError((error) => Errors.chatError("botUserId", Errors.reasonForRestError(error, { write: false }))), + Effect.flatMap((user) => { + if (typeof user.id !== "string" || user.id === "") { + return Effect.fail(Errors.chatError("botUserId", Errors.invalidResponse(false, "missing_user_id"))) + } + cached = user.id + return Effect.succeed(user.id) + }) + ) + }) + const config = yield* Effect.serviceOption(DiscordConfig) + const baseUrl = Option.match(config, { onNone: () => Adapter.defaultBaseUrl, onSome: (_) => _.rest.baseUrl }) + const { adapter, channelType, guild, interaction } = yield* Adapter.make({ rest, http, baseUrl, botUserId }) + yield* registry.register(adapter) + + const refuse = (message: string, error: ChatError) => + Effect.andThen(Effect.logWarning(message, error), Effect.fail(new NotAccepted())) + + const dispatch = Effect.fnUntraced(function*(payload: { readonly t: string; readonly d: unknown }) { + if (!handled.has(payload.t)) return + const handler = yield* ChatHandler + const self = yield* Effect.catch(botUserId, (error) => refuse("discord dispatch: bot user lookup failed", error)) + let result = Normalize.normalize(payload, { botUserId: self }) + if (result._tag === "MissingChannelType") { + const lookup = yield* channelType(result.channelId).pipe( + Effect.timeoutOrElse({ + duration: lookupTimeout, + orElse: () => Effect.fail(Errors.chatError("dispatch", Errors.transport(false, "lookup_timeout"))) + }), + Effect.map(Option.some), + Effect.catch((error) => + error.retry === "denied" + ? Effect.as( + Effect.logWarning("discord dispatch: channel lookup denied, event dropped", error), + Option.none() + ) + : refuse("discord dispatch: channel lookup failed", error) + ) + ) + if (Option.isNone(lookup)) return + result = Normalize.normalize(payload, { botUserId: self, channelType: lookup.value }) + if (result._tag === "MissingChannelType") return + } + for (const event of result.events) yield* handler.handle(event) + }) + + return DiscordChat.of({ botUserId, dispatch, interaction, guild }) + }) +) + +/** + * The address of a Discord channel, thread or direct: its channel id. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const thread: (channelId: string) => ChatThread = Normalize.thread + +/** + * Error for a message whose channel type is in neither the payload nor the + * context, from `normalize`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class MissingChannelType extends Schema.TaggedError( + "@effect/chat-discord/DiscordChat/MissingChannelType" +)("MissingChannelType", { channelId: Schema.String }) {} + +/** + * The normaliser behind `dispatch`, without delivering. + * + * **Details** + * + * - Pure: it never looks a channel up. It reads the channel type from the + * payload's `channel_type`, then from `context.channelType`, Discord's + * numeric type, and fails with `MissingChannelType` when neither has one. + * - Types 10, 11 and 12 are a `thread`, 1 and 3 a `direct`, and every + * other type a `channel`. + * - `MESSAGE_CREATE`, `MESSAGE_REACTION_ADD` and + * `MESSAGE_REACTION_REMOVE` give an event. Every other dispatch, and a + * message type that is not a message, gives none. + * - A custom emoji is `custom:{name}`, its id left on `raw.emoji.id`. + * - `raw` is `d`, undecoded. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalize = ( + dispatch: { readonly t: string; readonly d: unknown }, + context: { readonly botUserId: string; readonly channelType?: number | undefined } +): Result.Result, MissingChannelType> => { + const result = Normalize.normalize(dispatch, context) + return result._tag === "Events" + ? Result.succeed(result.events) + : Result.fail(new MissingChannelType({ channelId: result.channelId })) +} + +/** + * The member on a message's or added reaction's `raw`: the guild, the + * roles and the nick. + * + * **Details** + * + * Pure, with no REST call. `None` for a direct, a webhook message, or a + * payload with no member. Discord sends no member on a removed reaction, so + * it takes none. + * + * @stability unstable + * @category accessors + * @since 4.1.5 + */ +export const member = (event: MessageCreated | ReactionAdded): Option.Option<{ + readonly guildId: string + readonly roles: ReadonlyArray + readonly nick?: string | undefined +}> => Normalize.memberOf(event.raw) + +/** + * The guild on an event's `raw`. + * + * **Details** + * + * Pure, with no REST call. It works for every event, webhook messages and + * removed reactions included. `None` for a direct. + * + * @stability unstable + * @category accessors + * @since 4.1.5 + */ +export const guildId = (event: ChatEvent): Option.Option => Normalize.guildIdOf(event.raw) + +/** + * For a long-running process: subscribes to dfx's gateway and feeds every + * dispatch to `DiscordChat.dispatch`, one at a time. + * + * **Details** + * + * - Intents and sharding stay in the consumer's dfx config. So does the + * gateway session: dfx keeps the session id, the resume URL and the + * sequence in the `ShardStateStore` given to `DiscordGatewayLive`. + * - It reads dfx's stream of dispatches, which a dispatch reaches after its + * sequence is stored. An event that fails with `NotAccepted` is logged + * and dropped, and so is one the process stops in the middle of: Discord + * does not send it again. + * + * @see {@link layerDispatchHook} for a host that is stopped and started again + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerGateway: Layer.Layer = Layer.effectDiscard( + Effect.gen(function*() { + const gateway = yield* DiscordGateway + const discord = yield* DiscordChat + const handler = yield* ChatHandler + yield* gateway.dispatch.pipe( + Stream.runForEach((payload) => { + const t: unknown = payload.t + if (typeof t !== "string") return Effect.void + return discord.dispatch({ t, d: payload.d }).pipe( + Effect.provideService(ChatHandler, handler), + Effect.catchCause((cause) => Effect.logWarning("discord gateway: event dropped", cause)) + ) + }), + Effect.forkScoped + ) + }) +) + +/** + * For a host that is stopped and started again, a Durable Object for one: + * feeds every dispatch to `DiscordChat.dispatch` from dfx's `DispatchHook`, + * before the shard stores the dispatch's sequence. + * + * **When to use** + * + * Use when an event must not be lost to a restart, in place of + * `layerGateway`. Never provide both: every event would be delivered twice. + * + * **Details** + * + * - Provide it to the dfx layer that builds the shards, `DiscordGatewayLive` + * or `ShardLive`, beside a `ShardStateStore` over storage that outlives + * the host. dfx keeps the session id, the resume URL and the sequence + * there, so a host built again sends RESUME, not IDENTIFY, and Discord + * sends what it missed. + * - The handler runs before the sequence is stored. A host stopped in the + * middle of an event never stored that event's sequence, so the RESUME + * brings the event back. + * - An event that fails with `NotAccepted` fails the hook. dfx drops the + * connection, resumes from the last stored sequence and gets the event + * again, after 1 second, doubling with every failure in a row up to 2 + * minutes. An event that is refused every time holds back every event + * after it. + * - A defect in the handler is logged and the event dropped, as a replay + * would only fail the same way. + * - Delivery is at least once. A host stopped after the handler returned and + * before the sequence was stored gets that event a second time, so a + * handler that can't take an event twice remembers the ids it took. + * - The hook runs on the shard's read loop, one dispatch at a time. A + * handler slower than Discord's heartbeat interval, about 41 seconds, + * makes the shard reconnect. + * - Interactions stay with `layerGatewayInteractions`: each has 3 seconds, + * so there is nothing to resume. + * + * **Gotchas** + * + * - Discord replays only while it keeps the session. When it refuses the + * RESUME, dfx identifies again and the events in between are gone. + * - Waking the host, restarting a shard that went quiet and backing off + * after a close Discord won't change its mind on, an invalid token (4004) + * or a disallowed intent (4014), stay with the host: dfx reconnects + * whatever the close code. + * + * **Example** (Resuming from the host's own storage) + * + * ```ts + * import { DiscordChat } from "@effect/chat-discord" + * import { ShardLive } from "dfx/DiscordGateway/Shard" + * import { type ShardState, ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore" + * import { Effect, Layer, Option } from "effect" + * + * // Stands in for storage that outlives the host. + * const storage = new Map() + * + * const SessionStore = Layer.succeed( + * ShardStateStore, + * ShardStateStore.of({ + * forShard: ([id, count]) => { + * const key = `${id}/${count}` + * return { + * get: Effect.sync(() => Option.fromUndefinedOr(storage.get(key))), + * set: (state) => Effect.sync(() => void storage.set(key, state)), + * clear: Effect.sync(() => void storage.delete(key)) + * } + * } + * }) + * ) + * + * // Still needs `DiscordChat`, a `ChatHandler` and the rest of dfx's gateway. + * export const ShardResuming = ShardLive.pipe( + * Layer.provide([DiscordChat.layerDispatchHook, SessionStore]) + * ) + * ``` + * + * @see {@link layerGateway} for a process that runs until it is told to stop + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerDispatchHook: Layer.Layer = Layer.effect( + DispatchHook, + Effect.gen(function*() { + const discord = yield* DiscordChat + const handler = yield* ChatHandler + return (payload) => { + const t: unknown = payload.t + if (typeof t !== "string" || !handled.has(t)) return Effect.void + return discord.dispatch({ t, d: payload.d }).pipe( + Effect.provideService(ChatHandler, handler), + // Refused or interrupted: the sequence stays unstored and a RESUME replays the event. + Effect.catchCause((cause): Effect.Effect => + Cause.hasFails(cause) || Cause.hasInterrupts(cause) + ? Effect.failCause(cause) + : Effect.logError("discord gateway: handler defect, event dropped", cause) + ), + Effect.orDie + ) + } + }) +) + +/** + * For a long-running process: subscribes to dfx's gateway and feeds every + * `INTERACTION_CREATE` to `DiscordChat.interaction`. + * + * **Details** + * + * Interactions run concurrently, since each has its own 3 seconds. It is + * separate from `layerGateway`, so a consumer with no buttons needs no + * `ChatInteraction.Handler`. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerGatewayInteractions: Layer.Layer< + never, + never, + DiscordGateway | DiscordChat | ChatInteraction.Handler +> = Layer.effectDiscard( + Effect.gen(function*() { + const gateway = yield* DiscordGateway + const discord = yield* DiscordChat + const handler = yield* ChatInteraction.Handler + yield* gateway.fromDispatch("INTERACTION_CREATE").pipe( + Stream.mapEffect( + (payload) => + discord.interaction(payload).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.catchCause((cause) => Effect.logWarning("discord gateway: interaction dropped", cause)) + ), + { concurrency: "unbounded" } + ), + Stream.runDrain, + Effect.forkScoped + ) + }) +) + +/** + * The normaliser behind `DiscordChat.interaction`. + * + * **Details** + * + * - Pure. A click on a button gives `ButtonClicked`, with the id and value + * split from the `custom_id`. A string select gives `SelectChanged`, and + * a modal's submit `ModalSubmitted`, its metadata split from the modal's + * `custom_id`. + * - The thread and message come from the `message` Discord sends with the + * interaction, so a modal's submit carries the message whose element + * opened it. A modal a command opened has no message, so its submit has + * no `messageId`, and its thread is the channel. + * - A submit's `values` hold each answered input by id. An input left + * empty is absent. + * - A slash command with no options, or one string option, gives + * `CommandInvoked`: `id` is its name and `text` the option's value or + * the empty string. + * - Anything else, a message or user command, a slash command with other + * options, or a click with no message, is `None`. + * - `raw` is the payload, undecoded. For a command it holds the token that + * answers it, so it is a credential for 15 minutes. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalizeInteraction: (interaction: unknown) => Option.Option = + Interactions.normalize + +/** + * The create payload of each command in `group`, in the group's order, for + * Discord's application commands endpoint. + * + * **Details** + * + * - Each is a `CHAT_INPUT` command with the definition's name and + * description, and one `STRING` option when it has an `argument`. Each + * fits dfx's `ApplicationCommandCreateRequest`. + * - The definitions in `group` that are not commands are skipped. + * + * **Gotchas** + * + * - The adapter registers nothing. Discord's bulk overwrite replaces the + * application's whole list, so the consumer sends these beside its other + * commands, in one call. + * - dfx's gateway runner syncs its own commands the same way when it + * starts, which would delete these. A consumer running both adds these to + * that list, or switches the sync off with `setInteractionsSync(false)`. + * - A field the definition lacks, such as `default_member_permissions`, is + * spread onto the payload by the consumer. + * + * **Example** (Registering the commands beside dfx's own) + * + * ```ts import.meta.vitest + * import { DiscordChat } from "@effect/chat-discord" + * import { DiscordREST } from "dfx/DiscordREST" + * import { Effect } from "effect" + * import { ChatInteraction } from "effect/chat" + * + * const Commands = ChatInteraction.group( + * ChatInteraction.command("summarize", { description: "Summarize this thread" }) + * ) + * + * export const register = Effect.gen(function*() { + * const rest = yield* DiscordREST + * const app = yield* rest.getMyApplication() + * yield* rest.bulkSetApplicationCommands(app.id, [...DiscordChat.commandRequests(Commands)]) + * }) + * ``` + * + * @stability unstable + * @category commands + * @since 4.1.5 + */ +export const commandRequests: (group: ChatInteraction.Group) => ReadonlyArray<{ + /** `CHAT_INPUT`. */ + readonly type: 1 + readonly name: string + readonly description: string + /** One `STRING` option when the command has an `argument`, absent otherwise. */ + readonly options?: + | ReadonlyArray<{ + readonly type: 3 + readonly name: string + readonly description: string + readonly required: boolean + }> + | undefined +}> = Interactions.commandRequests + +/** + * Discord options for every write inside an effect. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface DiscordPostOptions { + /** + * Who a write may notify. Default: nobody, and a reply does not ping its + * author. `everyone` allows `@everyone` and `@here`. + */ + readonly notify?: { + readonly users?: ReadonlyArray | undefined + readonly roles?: ReadonlyArray | undefined + readonly everyone?: boolean | undefined + readonly repliedUser?: boolean | undefined + } | undefined + /** + * Link embeds. Default true, which sends the `SUPPRESS_EMBEDS` flag on + * the post and on every edit, so a link a later edit adds gets no embed + * either. False sends no flag on a post and clears it on an edit. + */ + readonly suppressEmbeds?: boolean | undefined + /** + * What a post with `replyTo` does when that message is gone. Default + * true: Discord refuses the post. False sends `fail_if_not_exists: false`, + * and Discord posts the message without the reference. + */ + readonly failIfNotExists?: boolean | undefined + /** For `startThread`, in minutes. Default: the channel's. */ + readonly autoArchiveDuration?: 60 | 1440 | 4320 | 10080 | undefined +} + +/** + * Scoped Discord options for every write inside `effect`. Nested calls + * merge, the inner one winning field by field. + * + * @stability unstable + * @category combinators + * @since 4.1.5 + */ +export const withPostOptions: { + (options: DiscordPostOptions): (effect: Effect.Effect) => Effect.Effect + (effect: Effect.Effect, options: DiscordPostOptions): Effect.Effect +} = dual( + 2, + (effect: Effect.Effect, options: DiscordPostOptions): Effect.Effect => + Effect.updateService(effect, CurrentPostOptions, (current) => ({ ...current, ...options })) +) + +/** + * Reads one Discord mention token, `<@id>`, `<@!id>`, `<@&id>`, + * `<#id>`, `@everyone` or `@here`, for `ChatTest.checkMessage`, + * `ChatTest.checkEvent` and `ChatTest.conformance`. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const parseMention: ParseMention = (slice) => Option.fromUndefinedOr(Mentions.parseToken(slice)) diff --git a/packages/chat/discord/src/index.ts b/packages/chat/discord/src/index.ts new file mode 100644 index 00000000000..fa6438bdf63 --- /dev/null +++ b/packages/chat/discord/src/index.ts @@ -0,0 +1,12 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as DiscordChat from "./DiscordChat.ts" diff --git a/packages/chat/discord/src/internal/adapter.ts b/packages/chat/discord/src/internal/adapter.ts new file mode 100644 index 00000000000..5d34186b9f7 --- /dev/null +++ b/packages/chat/discord/src/internal/adapter.ts @@ -0,0 +1,681 @@ +import type { DiscordREST } from "dfx/DiscordREST" +import * as Generated from "dfx/DiscordREST/Generated" +import type * as Discord from "dfx/types" +import * as Cache from "effect/Cache" +import { + type ChatAdapter, + checkMessageLength, + type Descriptor, + type File, + type Posted, + retrySafe, + type ThreadInfo, + type User +} from "effect/chat/ChatAdapter" +import { + type ChatError, + type ChatErrorReason, + fileRefused, + interactionExpired, + NotFound, + TooLarge +} from "effect/chat/ChatError" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ChatMessage } from "effect/chat/ChatMessage" +import type * as ChatThread from "effect/chat/ChatThread" +import type { Action } from "effect/chat/ChatUi" +import * as Clock from "effect/Clock" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Exit from "effect/Exit" +import { flow } from "effect/Function" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import * as Errors from "./errors.ts" +import * as Interactions from "./interactions.ts" +import * as Length from "./length.ts" +import * as Normalize from "./normalize.ts" +import { allowedMentions, CurrentPostOptions, flags } from "./options.ts" + +type Json = Schema.Json +type Rest = DiscordREST["Service"] + +/** @internal */ +export const descriptor: Descriptor = { + platform: Normalize.platform, + capabilities: new Set([ + "edit", + "delete", + "fetchMessage", + "history", + "download", + "files", + "replyTo", + "startThread", + "getThread", + "opener", + "getUser", + "buttons", + "selects", + "modals", + "commands", + "ephemeral" + ]), + // Components sit beside `content`, so the elements cost nothing here. + measure: (markdown) => Length.codePoints(markdown), + limits: { + messageLength: Length.messageLength, + filesPerMessage: 10, + uploadBytes: 10 * 1024 * 1024, + minWriteInterval: Duration.millis(1500), + followUpAge: Duration.minutes(15), + ...Interactions.limits + } +} + +const pageSize = 100 + +/** Discord's code for "a thread has already been created for this message". */ +const THREAD_ALREADY_CREATED = 160004 +/** Discord's code for "cannot execute action on this channel type". */ +const WRONG_CHANNEL_TYPE = 50024 +/** Discord's code for "unknown interaction": a callback after the 3 seconds. */ +const UNKNOWN_INTERACTION = 10062 +/** + * Discord's codes for an interaction token it no longer takes. After 15 + * minutes it answered 401 with 50027 "Invalid Webhook Token", recorded in + * EFF-3802. 10015 "Unknown Webhook" is from the docs' list and was not seen. + */ +const SPENT_TOKEN = [50027, 10015] +/** The start of Discord's snowflake clock, in Unix milliseconds. */ +const DISCORD_EPOCH = 1_420_070_400_000 + +/** When a snowflake was made, in Unix milliseconds. */ +const snowflakeMillis = (id: string): number => Number(BigInt(id) >> BigInt("22")) + DISCORD_EPOCH + +const optional = (schema: S) => Schema.optionalKey(Schema.NullOr(schema)) + +const ChannelFields = Schema.Struct({ + id: Schema.String, + type: Schema.Number, + guild_id: optional(Schema.String), + parent_id: optional(Schema.String), + name: optional(Schema.String), + topic: optional(Schema.String), + thread_metadata: optional(Schema.Struct({ create_timestamp: optional(Schema.String) })) +}) + +/** + * What the adapter reads from a channel. + * + * @internal + */ +export type Channel = typeof ChannelFields.Type & { readonly raw: Json } + +const decodeChannel = Schema.decodeUnknownOption(ChannelFields) + +const UserFields = Schema.Struct({ + id: Schema.String, + username: optional(Schema.String), + global_name: optional(Schema.String), + bot: optional(Schema.Boolean) +}) + +const decodeUser = Schema.decodeUnknownOption(UserFields) + +const MemberFields = Schema.Struct({ nick: optional(Schema.String), user: optional(UserFields) }) + +const decodeMember = Schema.decodeUnknownOption(MemberFields) + +const nonEmpty = (value: string | null | undefined): string | undefined => + value === null || value === undefined || value === "" ? undefined : value + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +const snowflake = /^\d{1,20}$/ + +/** + * Discord's API, as dfx's `DiscordConfig` has it by default. + * + * @internal + */ +export const defaultBaseUrl = "https://discord.com/api/v10" + +/** @internal */ +export const make = Effect.fnUntraced(function*(options: { + readonly rest: Rest + readonly http: HttpClient.HttpClient + readonly baseUrl: string + readonly botUserId: Effect.Effect +}) { + const { rest } = options + + /** + * dfx's requests for the interaction webhook, over the plain `HttpClient`. + * dfx's own client sends a request again after a 429, which for + * `followUp` could land past the 15 minutes the expiry check ran on. The + * webhook takes the interaction's token and no bot token. + */ + const webhooks = Generated.make( + HttpClient.mapRequest( + options.http, + flow( + HttpClientRequest.prependUrl(options.baseUrl), + HttpClientRequest.setHeader("User-Agent", "DiscordBot (https://effect.website, 1)") + ) + ) + ) + + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + + /** A message's elements as action rows, checked before any call. */ + const components = (op: string, actions: ReadonlyArray | undefined) => { + const rows = Interactions.rows(actions ?? []) + return Array.isArray(rows) ? Effect.succeed(rows) : fail(op, rows) + } + + /** One REST call, its failure mapped to a `ChatError` and retried when nothing happened. */ + const call = (op: string, effect: Effect.Effect, write = false): Effect.Effect => + retrySafe(Effect.mapError(effect, (error) => Errors.chatError(op, Errors.reasonForRestError(error, { write })))) + + const resolve = (op: string, thread: ChatThread.ChatThread): Effect.Effect => + thread.platform === Normalize.platform && snowflake.test(thread.id) + ? Effect.succeed(thread.id) + : fail(op, Errors.invalidRequest("invalid_thread")) + + const readChannel = (op: string, raw: unknown): Effect.Effect => + Option.match(decodeChannel(raw), { + onNone: () => fail(op, Errors.invalidResponse(false, "invalid_channel")), + onSome: (fields) => Effect.succeed({ ...fields, raw: raw as Json }) + }) + + /** Channels by id, for their type, parent and guild, none of which change. Failures are not kept. */ + const channels = yield* Cache.makeWith( + (id: string) => Effect.flatMap(call("getChannel", rest.getChannel(id)), (raw) => readChannel("getChannel", raw)), + { + capacity: 1024, + timeToLive: (exit) => Exit.isSuccess(exit) ? Duration.hours(1) : Duration.zero + } + ) + + const channel = (op: string, id: string): Effect.Effect => + Cache.get(channels, id).pipe( + Effect.mapError((error) => error.op === op ? error : Errors.chatError(op, error.reason)) + ) + + const toMessage = (raw: unknown, botUserId: string): ChatMessage | undefined => { + const fields = Normalize.decodeMessageFields(raw) + return fields === undefined ? undefined : Normalize.message(raw as Json, fields, { botUserId }) + } + + const getMessage = (op: string, channelId: string, messageId: string): Effect.Effect => + call(op, rest.getMessage(channelId, messageId)).pipe(Effect.map((raw) => raw as unknown as Json)) + + const fetchMessage = Effect.fnUntraced(function*(op: string, thread: ChatThread.ChatThread, messageId: string) { + const channelId = yield* resolve(op, thread) + const botUserId = yield* options.botUserId + const raw = yield* getMessage(op, channelId, messageId) + const message = toMessage(raw, botUserId) + return message === undefined ? yield* fail(op, Errors.notFound("not_a_message")) : message + }) + + /** An optional read: none when the message is gone or the bot may not read it. */ + const orNone = (effect: Effect.Effect): Effect.Effect, ChatError> => + effect.pipe( + Effect.map(Option.some), + Effect.catchIf( + (error) => error.reason._tag === "NotFound" || error.reason._tag === "Forbidden", + () => Effect.succeed(Option.none()) + ) + ) + + // Writes + + const toFile = (file: File) => + new globalThis.File([file.content as Uint8Array], file.name, { type: file.contentType }) + + const post: ChatAdapter["post"] = Effect.fnUntraced(function*(thread, content) { + const op = "post" + const channelId = yield* resolve(op, thread) + const rows = yield* components(op, content.actions) + const postOptions = yield* CurrentPostOptions + yield* checkMessageLength(descriptor, op, content.markdown) + const files = content.files ?? [] + // The adapter's own count and size check refuses the file the way Discord would. + if ( + files.length > descriptor.limits.filesPerMessage || + files.reduce((total, file) => total + file.content.length, 0) > descriptor.limits.uploadBytes + ) { + return yield* fail(op, new TooLarge({ subject: "files", detail: fileRefused })) + } + const create = rest.createMessage(channelId, { + content: content.markdown, + allowed_mentions: allowedMentions(postOptions.notify), + flags: flags(postOptions), + ...(rows.length === 0 ? {} : { components: rows }), + ...(content.replyTo === undefined ? {} : { + message_reference: { + message_id: content.replyTo.messageId, + ...(postOptions.failIfNotExists === false ? { fail_if_not_exists: false } : {}) + } + }), + ...(files.length === 0 ? {} : { attachments: files.map((file, i) => ({ id: String(i), filename: file.name })) }) + }) + const answer = yield* (files.length === 0 + ? call(op, create, true) + // Discord creates the message and its files in one request, so a denied answer left nothing posted. + : call(op, rest.withFiles(files.map(toFile))(create), true).pipe( + Effect.mapError((error) => Errors.chatError(op, Errors.fileRefused(error.reason))) + )) + if (typeof answer.id !== "string" || typeof answer.timestamp !== "string") { + return yield* fail(op, Errors.invalidResponse(true, "missing_id")) + } + const timestamp = DateTime.make(answer.timestamp) + if (Option.isNone(timestamp)) return yield* fail(op, Errors.invalidResponse(true, "invalid_timestamp")) + return { id: answer.id, thread, timestamp: DateTime.toUtc(timestamp.value) } satisfies Posted + }) + + const edit: ChatAdapter["edit"] = Effect.fnUntraced(function*(thread, messageId, content) { + const op = "edit" + const channelId = yield* resolve(op, thread) + const rows = yield* components(op, content.actions) + const postOptions = yield* CurrentPostOptions + yield* checkMessageLength(descriptor, op, content.markdown) + yield* call( + op, + rest.updateMessage(channelId, messageId, { + content: content.markdown, + allowed_mentions: allowedMentions(postOptions.notify), + flags: flags(postOptions), + // Send empty components to clear existing elements. + components: rows + }), + true + ) + }) + + const startThread: ChatAdapter["startThread"] = Effect.fnUntraced(function*(thread, messageId, startOptions) { + const op = "startThread" + const channelId = yield* resolve(op, thread) + const postOptions = yield* CurrentPostOptions + const given = nonEmpty(startOptions?.name?.trim()) + const name = given ?? (yield* Effect.map( + getMessage(op, channelId, messageId), + (raw) => { + const text = isObject(raw) && typeof raw.content === "string" ? raw.content.replace(/\s+/g, " ").trim() : "" + return text === "" ? "Thread" : Length.cut(text, 100) + } + )) + return yield* call( + op, + rest.createThreadFromMessage(channelId, messageId, { + name: Length.cut(name, 100), + ...(postOptions.autoArchiveDuration === undefined + ? {} + : { auto_archive_duration: postOptions.autoArchiveDuration }) + }), + true + ).pipe( + Effect.map((answer) => Normalize.thread(typeof answer.id === "string" ? answer.id : messageId)), + // The thread under a message has the message's id. + Effect.catchIf(Errors.hasCode(THREAD_ALREADY_CREATED), () => Effect.succeed(Normalize.thread(messageId))), + // A message in a thread has no thread of its own: the thread is the answer. + Effect.catchIf(Errors.hasCode(WRONG_CHANNEL_TYPE), (error) => + Effect.flatMap( + channel(op, channelId), + (found) => Normalize.isThreadType(found.type) ? Effect.succeed(thread) : Effect.fail(error) + )) + ) + }) + + // Reads + + /** + * A thread's messages, with the `THREAD_STARTER_MESSAGE` placeholder + * replaced by the opener from the parent channel, or dropped when the + * opener is gone. + */ + const history: ChatAdapter["history"] = (thread, { direction, from }) => + Stream.unwrap(Effect.gen(function*() { + const op = "history" + const channelId = yield* resolve(op, thread) + const botUserId = yield* options.botUserId + const page = (cursor: string | undefined) => + call( + op, + rest.listMessages( + channelId, + direction === "backward" + ? { ...(cursor === undefined ? {} : { before: cursor }), limit: pageSize } + : { after: cursor ?? "0", limit: pageSize } + ) + ).pipe(Effect.map((answer) => { + const found: ReadonlyArray = Array.isArray(answer) ? answer as ReadonlyArray : [] + const sorted = [...found].sort((a, b) => + Normalize.compareIds(idOf(a), idOf(b)) * (direction === "backward" ? -1 : 1) + ) + const last = sorted.at(-1) + return [ + sorted, + found.length < pageSize || last === undefined ? Option.none() : Option.some(idOf(last)) + ] as const + })) + const after = (message: ChatMessage) => + from === undefined || + (direction === "forward" + ? Normalize.compareIds(message.id, from) > 0 + : Normalize.compareIds(message.id, from) < 0) + return Stream.paginate(from, page).pipe( + Stream.mapEffect((raw) => resolveStarter(op, raw, botUserId)), + Stream.filter((message): message is ChatMessage => message !== undefined && after(message)) + ) + })) + + const idOf = (raw: Json): string => isObject(raw) && typeof raw.id === "string" ? raw.id : "" + + const resolveStarter = ( + op: string, + raw: Json, + botUserId: string + ): Effect.Effect => { + const fields = Normalize.decodeMessageFields(raw) + if (fields === undefined) return Effect.succeed(undefined) + if (fields.type !== Normalize.THREAD_STARTER_MESSAGE) { + return Effect.succeed(Normalize.message(raw, fields, { botUserId })) + } + const referenced = isObject(raw) ? raw.referenced_message : undefined + if (referenced === null) return Effect.succeed(undefined) + if (referenced !== undefined) return Effect.succeed(toMessage(referenced, botUserId)) + const reference = fields.message_reference + const channelId = nonEmpty(reference?.channel_id) + const messageId = nonEmpty(reference?.message_id) + if (channelId === undefined || messageId === undefined) return Effect.succeed(undefined) + return orNone(getMessage(op, channelId, messageId)).pipe( + Effect.map((found) => Option.isSome(found) ? toMessage(found.value, botUserId) : undefined) + ) + } + + /** + * The message whose id is the thread's: in the thread for a forum post, + * otherwise in the parent channel. + */ + const opener: ChatAdapter["opener"] = Effect.fnUntraced(function*(thread) { + const op = "opener" + const channelId = yield* resolve(op, thread) + const found = yield* channel(op, channelId) + if (!Normalize.isThreadType(found.type)) return Option.none() + const botUserId = yield* options.botUserId + const inThread = yield* orNone(getMessage(op, channelId, channelId)) + const parent = nonEmpty(found.parent_id) + const raw = Option.isSome(inThread) || parent === undefined + ? inThread + : yield* orNone(getMessage(op, parent, channelId)) + return Option.flatMap(raw, (raw) => Option.fromUndefinedOr(toMessage(raw, botUserId))) + }) + + const getThread: ChatAdapter["getThread"] = Effect.fnUntraced(function*(thread) { + const op = "getThread" + const channelId = yield* resolve(op, thread) + const found = yield* Effect.flatMap(call(op, rest.getChannel(channelId)), (raw) => readChannel(op, raw)) + const kind = Normalize.kindOf(found.type) + const name = kind === "direct" ? undefined : nonEmpty(found.name) + const topic = nonEmpty(found.topic) + const parent = kind === "thread" ? nonEmpty(found.parent_id) : undefined + const created = Option.flatMap( + Option.fromUndefinedOr(nonEmpty(found.thread_metadata?.create_timestamp)), + DateTime.make + ) + const createdAt = Option.isSome(created) ? DateTime.toUtc(created.value) : Normalize.snowflakeTime(found.id) + return { + thread, + kind, + ...(name === undefined ? {} : { name }), + ...(topic === undefined ? {} : { topic }), + ...(parent === undefined ? {} : { parent: Normalize.thread(parent) }), + ...(createdAt === undefined ? {} : { createdAt }), + raw: found.raw + } satisfies ThreadInfo + }) + + const getUser: ChatAdapter["getUser"] = Effect.fnUntraced(function*(id, userOptions) { + const op = "getUser" + const botUserId = yield* options.botUserId + const where = userOptions?.in + const guildId = where === undefined + ? undefined + : nonEmpty((yield* channel(op, yield* resolve(op, where))).guild_id) + if (guildId !== undefined) { + const member = yield* orNone(call(op, rest.getGuildMember(guildId, id))) + if (Option.isSome(member)) { + const fields = Option.getOrUndefined(decodeMember(member.value)) + const user = fields?.user + if (fields !== undefined && user !== null && user !== undefined) { + return { + id: user.id, + name: nonEmpty(fields.nick) ?? nonEmpty(user.global_name) ?? nonEmpty(user.username) ?? user.id, + isBot: user.bot === true, + isSelf: user.id === botUserId, + raw: member.value as unknown as Json + } satisfies User + } + } + } + const raw = yield* call(op, rest.getUser(id)) + const user = Option.getOrUndefined(decodeUser(raw)) + if (user === undefined) return yield* fail(op, Errors.invalidResponse(false, "invalid_user")) + return { + id: user.id, + name: nonEmpty(user.global_name) ?? nonEmpty(user.username) ?? user.id, + isBot: user.bot === true, + isSelf: user.id === botUserId, + raw: raw as unknown as Json + } satisfies User + }) + + const download: ChatAdapter["download"] = (thread, attachment, { maxBytes }) => + Stream.unwrap(Effect.gen(function*() { + const op = "download" + const channelId = yield* resolve(op, thread) + const tooLarge = Errors.chatError(op, new TooLarge({ subject: "files", detail: "max_bytes" })) + if (attachment.size !== undefined && attachment.size > maxBytes) return yield* Effect.fail(tooLarge) + const ids = Normalize.parseAttachmentId(attachment.id) + if (ids === undefined) return yield* fail(op, Errors.invalidRequest("invalid_attachment")) + const raw = yield* getMessage(op, channelId, ids.messageId) + const list = isObject(raw) && Array.isArray(raw.attachments) ? raw.attachments : [] + const found = list.find((item) => isObject(item) && item.id === ids.attachmentId) + const url = isObject(found) && typeof found.url === "string" ? found.url : undefined + if (url === undefined) return yield* fail(op, Errors.notFound("attachment_not_found")) + const response = yield* options.http.execute(HttpClientRequest.get(url)).pipe( + Effect.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error))) + ) + if (response.status < 200 || response.status >= 300) { + return yield* fail(op, Errors.reasonForResponse(response.status, undefined, { write: false })) + } + let received = 0 + return response.stream.pipe( + Stream.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error))), + Stream.mapEffect((chunk) => { + received += chunk.length + return received > maxBytes ? Effect.fail(tooLarge) : Effect.succeed(chunk) + }) + ) + })) + + /** + * The message a `post` of the same content sends with the default post + * options, after the same checks: no mentions and no embeds. + * `withPostOptions` does not reach a command's answer. + */ + const message = Effect.fnUntraced(function*( + op: string, + content: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined } + ) { + const rows = yield* components(op, content.actions) + yield* checkMessageLength(descriptor, op, content.markdown) + return { + content: content.markdown, + allowed_mentions: allowedMentions(undefined), + flags: flags({}), + ...(rows.length === 0 ? {} : { components: rows }) + } + }) + + /** + * Edits the original response of a deferred command in one HTTP request: + * not through `call`, nor dfx's client, whose retries would send again + * after the expiry check. Refused before the call past `followUpAge`, + * read from the interaction's id. A token Discord no longer takes is + * `interaction_expired` too. + */ + const followUp: ChatAdapter["followUp"] = Effect.fnUntraced(function*(deferred, content) { + const op = "followUp" + const to = deferred.command.thread.platform === Normalize.platform && snowflake.test(deferred.command.thread.id) + ? Interactions.followUpTo(deferred.command.raw) + : undefined + if (to === undefined || !snowflake.test(to.id)) return yield* fail(op, Errors.invalidRequest("invalid_interaction")) + const expired = new NotFound({ detail: interactionExpired }) + const now = yield* Clock.currentTimeMillis + if (now - snowflakeMillis(to.id) >= Duration.toMillis(descriptor.limits.followUpAge!)) { + return yield* fail(op, expired) + } + const body = yield* message(op, content) + const ephemeral = deferred.visibility === "ephemeral" ? 1 << 6 : 0 + yield* webhooks.updateOriginalWebhookMessage(to.applicationId, to.token, { + payload: { ...body, flags: body.flags | ephemeral } + }).pipe(Effect.mapError((cause) => { + const error = Errors.chatError(op, Errors.reasonForRestError(cause, { write: true })) + return SPENT_TOKEN.some((code) => Errors.hasCode(code)(error)) + ? Errors.chatError(op, new NotFound({ detail: interactionExpired, cause: error.reason })) + : error + })) + }) + + const adapter: ChatAdapter = { + descriptor, + post, + edit, + followUp, + delete: Effect.fnUntraced(function*(thread, messageId) { + const channelId = yield* resolve("delete", thread) + yield* call("delete", rest.deleteMessage(channelId, messageId), true) + }), + fetchMessage: (thread, messageId) => fetchMessage("fetchMessage", thread, messageId), + history, + download, + startThread, + getThread, + opener, + getUser + } + + /** The guild a channel or thread is in, from the cached channel. */ + const guild = (thread: ChatThread.ChatThread): Effect.Effect, ChatError> => + Effect.flatMap( + resolve("guild", thread), + (id) => Effect.map(channel("guild", id), (found) => Option.fromUndefinedOr(nonEmpty(found.guild_id))) + ) + + /** A channel's type, for `dispatch`. */ + const channelType = (id: string): Effect.Effect => + Effect.map(channel("dispatch", id), (found) => found.type) + + // Interactions + + /** One callback, Discord's answer to an interaction. */ + const callback = ( + op: string, + to: { readonly id: string; readonly token: string }, + payload: Discord.CreateInteractionResponseRequest + ): Effect.Effect => + Effect.asVoid(call(op, rest.createInteractionResponse(to.id, to.token, { payload }), true)) + + const logged = (effect: Effect.Effect) => + Effect.catch(effect, (error) => Effect.logWarning("@effect/chat-discord: an interaction callback failed", error)) + + /** + * Normalises an `INTERACTION_CREATE` payload, runs the handler inside + * `deadline` and answers through the callback endpoint. + */ + const interaction = ( + raw: unknown, + interactionOptions?: { readonly deadline?: Duration.Duration | undefined } + ): Effect.Effect => + Effect.gen(function*() { + const event = Interactions.normalize(raw) + const to = Interactions.answerTo(raw) + if (Option.isNone(event) || to === undefined) return + const reply = yield* ChatInteraction.handle(event.value, interactionOptions?.deadline ?? Duration.seconds(2)) + switch (reply._tag) { + // Too late to answer: Discord shows its own banner. + case "Late": + return + case "Ack": + case "Close": + // Type 6 is no answer to a command, so an unhandled one is left to whoever owns it. + if (event.value._tag === "CommandInvoked") return + return yield* logged(callback("ack", to, Interactions.ackCallback)) + case "Errors": + return yield* logged(callback("errors", to, Interactions.errorsCallback(reply.errors))) + case "OpenModal": { + const from = event.value as + | ChatInteraction.ButtonClicked + | ChatInteraction.SelectChanged + | ChatInteraction.CommandInvoked + const data = Interactions.modalData(reply.modal) + if (!("custom_id" in data)) { + // A click is still answered, so the person sees no failure for a modal that could not be built. + // A command has no silent answer, so it gets none, and Discord shows its own failure. + if (from._tag !== "CommandInvoked") yield* logged(callback("ack", to, Interactions.ackCallback)) + const handler = yield* ChatInteraction.Handler + return yield* handler.refused(from, Errors.chatError("openModal", data)) + } + return yield* answer(from, "openModal", to, Effect.succeed(Interactions.modalCallback(data))) + } + case "Respond": + return yield* answer( + event.value as ChatInteraction.CommandInvoked, + "respond", + to, + Effect.map(message("respond", reply), (body) => Interactions.respondCallback(body, reply.visibility)) + ) + case "Defer": + return yield* answer( + event.value as ChatInteraction.CommandInvoked, + "defer", + to, + Effect.succeed(Interactions.deferCallback(reply.visibility)) + ) + } + }) + + /** + * Sends a reply that must land, or tells `Handler.refused`: a reply that + * fails its checks sends nothing, and a callback that comes back + * `Unknown interaction` was late, which is `interaction_expired`. + */ + const answer = ( + event: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatInteraction.CommandInvoked, + op: string, + to: { readonly id: string; readonly token: string }, + payload: Effect.Effect + ): Effect.Effect => + Effect.gen(function*() { + const handler = yield* ChatInteraction.Handler + yield* Effect.flatMap(payload, (built) => callback(op, to, built)).pipe( + Effect.catch((error) => + handler.refused( + event, + Errors.hasCode(UNKNOWN_INTERACTION)(error) + ? Errors.chatError(op, new NotFound({ detail: interactionExpired, cause: error.reason })) + : error + ) + ) + ) + }) + + return { adapter, guild, channelType, interaction } +}) diff --git a/packages/chat/discord/src/internal/errors.ts b/packages/chat/discord/src/internal/errors.ts new file mode 100644 index 00000000000..4bc44c89973 --- /dev/null +++ b/packages/chat/discord/src/internal/errors.ts @@ -0,0 +1,184 @@ +import { + ChatError, + type ChatErrorReason, + fileRefused as fileRefusedDetail, + Forbidden, + InvalidRequest, + InvalidResponse, + NoAdapter, + NotFound, + PlatformUnavailable, + RateLimited, + TooLarge, + Transport, + Unauthenticated, + Unknown, + Unsupported +} from "effect/chat/ChatError" +import * as Duration from "effect/Duration" +import { platform } from "./normalize.ts" + +/** Discord's JSON error codes, by what they mean. */ +const notFoundCodes = new Set([10003, 10004, 10007, 10008, 10013, 10014, 10015]) +const forbiddenCodes = new Set([20001, 20028, 50001, 50005, 50013, 50021, 50083, 160005, 160006]) +const unauthenticatedCodes = new Set([0, 40001]) +const tooLargeFiles = new Set([40005, 50045]) + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** A 50035 whose errors say a field is too long. */ +const isTooLong = (data: Record): boolean => JSON.stringify(data.errors ?? {}).includes("MAX_LENGTH") + +const withCause = (cause: unknown) => (cause === undefined ? {} : { cause }) + +/** + * The reason for an answer Discord sent: a JSON error `code` when there is + * one, otherwise the HTTP status. `detail` is the code, or `http_{status}`. + * + * @internal + */ +export const reasonForResponse = ( + status: number, + data: unknown, + options: { readonly write: boolean; readonly cause?: unknown } +): ChatErrorReason => { + const body = isObject(data) ? data : {} + const code = typeof body.code === "number" ? body.code : undefined + const cause = withCause(options.cause) + if (status === 429) { + const seconds = typeof body.retry_after === "number" ? body.retry_after : undefined + return new RateLimited({ + detail: code === undefined ? "http_429" : String(code), + ...cause, + ...(seconds === undefined || seconds < 0 ? {} : { retryAfter: Duration.millis(Math.ceil(seconds * 1000)) }) + }) + } + if (status >= 500) { + return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail: `http_${status}`, ...cause }) + } + const detail = code === undefined ? `http_${status}` : String(code) + if (code !== undefined) { + if (notFoundCodes.has(code)) return new NotFound({ detail, ...cause }) + if (forbiddenCodes.has(code)) return new Forbidden({ detail, ...cause }) + if (tooLargeFiles.has(code)) return new TooLarge({ subject: "files", detail, ...cause }) + if (code === 50035 && isTooLong(body)) return new TooLarge({ subject: "text", detail, ...cause }) + if (unauthenticatedCodes.has(code) && status === 401) return new Unauthenticated({ detail, ...cause }) + } + if (status === 401) return new Unauthenticated({ detail, ...cause }) + if (status === 403) return new Forbidden({ detail, ...cause }) + if (status === 404) return new NotFound({ detail, ...cause }) + if (status === 413) return new TooLarge({ subject: "files", detail, ...cause }) + return new InvalidRequest({ detail, ...cause }) +} + +/** + * A request that failed before an answer came. + * + * @internal + */ +export const transport = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => + new Transport({ retry: write ? "uncertain" : "retry", detail, ...withCause(cause) }) + +/** + * An answer the adapter cannot read. A write may have landed. + * + * @internal + */ +export const invalidResponse = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => { + const fields = { detail, ...withCause(cause) } + return write ? new Unknown(fields) : new InvalidResponse(fields) +} + +/** + * The reason for a dfx `DiscordRESTError`, read structurally so no dfx + * type is needed: an `ErrorResponse` or `RatelimitedResponse` carries + * Discord's body in `data`, and an `HttpClientError` carries a `reason`. + * + * @internal + */ +export const reasonForRestError = (error: unknown, options: { readonly write: boolean }): ChatErrorReason => { + const tagged = isObject(error) ? error : {} + const tag = tagged._tag + const response = isObject(tagged.response) ? tagged.response : undefined + const status = typeof response?.status === "number" ? response.status : undefined + if (tag === "ErrorResponse" || tag === "RatelimitedResponse") { + return reasonForResponse(status ?? (tag === "RatelimitedResponse" ? 429 : 400), tagged.data, { + write: options.write, + cause: error + }) + } + if (tag === "HttpClientError" && isObject(tagged.reason)) { + const reason = tagged.reason + const reasonResponse = isObject(reason.response) ? reason.response : undefined + const reasonStatus = typeof reasonResponse?.status === "number" ? reasonResponse.status : undefined + // An error status whose body is not JSON, such as a 413 from the edge, is still Discord's answer. + if (reasonStatus !== undefined && reasonStatus >= 400 && reasonStatus < 500) { + return reasonForResponse(reasonStatus, undefined, { write: options.write, cause: error }) + } + switch (reason._tag) { + case "StatusCodeError": + return reasonForResponse(reasonStatus ?? 500, undefined, { write: options.write, cause: error }) + case "TransportError": + return transport(options.write, "transport", error) + case "EncodeError": + case "InvalidUrlError": + return new InvalidRequest({ detail: String(reason._tag), cause: error }) + default: + return invalidResponse(options.write, String(reason._tag), error) + } + } + return new Unknown({ detail: "unknown", cause: error }) +} + +/** @internal */ +export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason }) + +/** @internal */ +export const invalidRequest = (detail: string): ChatErrorReason => new InvalidRequest({ detail }) + +/** @internal */ +export const notFound = (detail: string): ChatErrorReason => new NotFound({ detail }) + +/** + * Whether a failure carries Discord's JSON error code `code`. + * + * @internal + */ +export const hasCode = (code: number) => (error: ChatError): boolean => error.reason.detail === String(code) + +/** + * A `denied` reason from a post that carries files, as a refused file: the + * same reason with detail `file_refused`, and the reason it replaces, with + * Discord's code, as its `cause`. Any other reason is returned as it is. + * + * @internal + */ +export const fileRefused = (reason: ChatErrorReason): ChatErrorReason => { + if (reason.retry !== "denied" || reason.detail === fileRefusedDetail) return reason + const fields = { detail: fileRefusedDetail, cause: reason } + switch (reason._tag) { + case "Unsupported": + return new Unsupported({ capability: reason.capability, ...fields }) + case "Transport": + return new Transport({ retry: reason.retry, ...fields }) + case "PlatformUnavailable": + return new PlatformUnavailable({ retry: reason.retry, ...fields }) + case "NoAdapter": + return new NoAdapter(fields) + case "Unauthenticated": + return new Unauthenticated(fields) + case "Forbidden": + return new Forbidden(fields) + case "NotFound": + return new NotFound(fields) + case "TooLarge": + return new TooLarge({ ...(reason.subject === undefined ? {} : { subject: reason.subject }), ...fields }) + case "InvalidRequest": + return new InvalidRequest(fields) + case "InvalidResponse": + return new InvalidResponse(fields) + case "RateLimited": + case "Unknown": + return reason + } +} diff --git a/packages/chat/discord/src/internal/interactions.ts b/packages/chat/discord/src/internal/interactions.ts new file mode 100644 index 00000000000..80a3a9f8955 --- /dev/null +++ b/packages/chat/discord/src/internal/interactions.ts @@ -0,0 +1,452 @@ +import type * as Discord from "dfx/types" +import type { ChatErrorReason } from "effect/chat/ChatError" +import { InvalidRequest, TooLarge } from "effect/chat/ChatError" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import { codePoints, cut, messageLength } from "./length.ts" +import * as Normalize from "./normalize.ts" + +/** + * The one character between an id and its value in a `custom_id`, for a + * button and for a modal's metadata. An id may not hold it, so the first one + * in a `custom_id` is the join. + * + * @internal + */ +export const join = "\u241F" + +/** @internal */ +export const limits = { + actionsPerMessage: 25, + actionDataLength: 99, + modalMetadataLength: 99, + modalFields: 5 +} as const + +/** Discord's limits on rows, a row's buttons, a modal's title and an input's label. */ +const rowsPerMessage = 5 +const buttonsPerRow = 5 +const titleLength = 45 +const labelLength = 45 + +/** Discord's interaction callback types. */ +const DEFERRED_UPDATE_MESSAGE = 6 +const CHANNEL_MESSAGE_WITH_SOURCE = 4 +const DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5 +const MODAL = 9 +const EPHEMERAL = 1 << 6 + +/** `custom_id` for an id and an optional value. */ +const customId = (id: string, value: string | undefined): string => value === undefined ? id : id + join + value + +/** An id and its value from a `custom_id`, split at the first join. */ +const splitCustomId = (customId: string): { readonly id: string; readonly value?: string } => { + const at = customId.indexOf(join) + return at === -1 ? { id: customId } : { id: customId.slice(0, at), value: customId.slice(at + 1) } +} + +const buttonStyle = (style: ChatUi.ButtonStyle | undefined): Discord.ButtonStyleTypes => + style === "primary" ? 1 : style === "danger" ? 4 : 2 + +type Row = Discord.ActionRowComponentForMessageRequest +type Component = Row["components"][number] + +/** + * A message's elements as action rows, or why they can't go: five buttons to + * a row and a select alone in its row, in the order given, up to five rows. + * + * @internal + */ +export const rows = (actions: ReadonlyArray): Array | ChatErrorReason => { + if (actions.length > limits.actionsPerMessage) return new TooLarge({ detail: "too_many_actions" }) + const out: Array<{ type: 1; components: Array }> = [] + let open: Array | undefined + for (const action of actions) { + switch (action._tag) { + case "Button": + case "LinkButton": { + if (action._tag === "Button") { + if (action.id.includes(join)) return new InvalidRequest({ detail: "invalid_action_id" }) + if (codePoints(action.id) + codePoints(action.value ?? "") > limits.actionDataLength) { + return new TooLarge({ detail: "action_data_too_long" }) + } + } + const button: Component = action._tag === "Button" + ? { + type: 2, + style: buttonStyle(action.style), + label: action.label, + custom_id: customId(action.id, action.value) + } + : { type: 2, style: 5, label: action.label, url: action.url } + if (open === undefined || open.length === buttonsPerRow) { + open = [button] + out.push({ type: 1, components: open }) + } else { + open.push(button) + } + break + } + case "Select": { + const id = codePoints(action.id) + if (action.options.some((option) => id + codePoints(option.value) > limits.actionDataLength)) { + return new TooLarge({ detail: "action_data_too_long" }) + } + open = undefined + out.push({ + type: 1, + components: [{ + type: 3, + custom_id: action.id, + ...(action.placeholder === undefined ? {} : { placeholder: action.placeholder }), + options: action.options.map((option) => ({ + label: option.label, + value: option.value, + ...(option.value === action.selected ? { default: true } : {}) + })), + min_values: 1, + max_values: 1 + }] + }) + break + } + } + } + return out.length > rowsPerMessage ? new TooLarge({ detail: "too_many_actions" }) : out +} + +/** + * Discord's Markdown escape for any ASCII punctuation, so a modal's text + * shows as written. + */ +const plain = (text: string): string => text.replace(/[!-/:-@[-`{-~]/g, "\\$&") + +type ModalData = Discord.ModalInteractionCallbackRequestData + +/** + * The callback data that opens `modal`, or why it can't open. Inputs are + * Label components around a text input or a string select, and a `Text` is + * a text display. Discord has no submit label, so `submitLabel` is not sent. + * + * @internal + */ +export const modalData = (modal: ChatUi.Modal): ModalData | ChatErrorReason => { + if (modal.id.includes(join)) return new InvalidRequest({ detail: "invalid_modal_id" }) + if (codePoints(modal.id) + codePoints(modal.metadata ?? "") > limits.modalMetadataLength) { + return new TooLarge({ detail: "modal_metadata_too_long" }) + } + if (modal.elements.length > limits.modalFields) return new TooLarge({ detail: "too_many_fields" }) + if (codePoints(modal.title) > titleLength) return new TooLarge({ detail: "title_too_long" }) + const components: Array = [] + for (const element of modal.elements) { + if (element._tag === "Text") { + components.push({ type: 10, content: plain(element.text) }) + continue + } + if (codePoints(element.label) > labelLength) return new TooLarge({ detail: "label_too_long" }) + const required = element.optional !== true + components.push({ + type: 18, + label: element.label, + component: element._tag === "TextInput" + ? { + type: 4, + custom_id: element.id, + style: element.multiline === true ? 2 : 1, + required, + ...(element.placeholder === undefined ? {} : { placeholder: element.placeholder }), + ...(element.initial === undefined ? {} : { value: element.initial }), + ...(element.minLength === undefined ? {} : { min_length: element.minLength }), + ...(element.maxLength === undefined ? {} : { max_length: element.maxLength }) + } + : { + type: 3, + custom_id: element.id, + required, + min_values: required ? 1 : 0, + max_values: 1, + ...(element.placeholder === undefined ? {} : { placeholder: element.placeholder }), + options: element.options.map((option) => ({ + label: option.label, + value: option.value, + ...(option.value === element.initial ? { default: true } : {}) + })) + } + }) + } + return { custom_id: customId(modal.id, modal.metadata), title: modal.title, components } +} + +type Callback = Discord.CreateInteractionResponseRequest + +/** @internal */ +export const ackCallback: Callback = { type: DEFERRED_UPDATE_MESSAGE } + +/** @internal */ +export const modalCallback = (data: ModalData): Callback => ({ type: MODAL, data }) + +/** + * The answer to an `errors` reply: a private message listing each one, as + * plain text, cut to fit one message. The modal closes. + * + * @internal + */ +export const errorsCallback = (errors: Readonly>): Callback => ({ + type: CHANNEL_MESSAGE_WITH_SOURCE, + data: { + content: cut(Object.values(errors).map((message) => "- " + plain(message)).join("\n"), messageLength), + allowed_mentions: { parse: [] }, + flags: EPHEMERAL + } +}) + +/** + * The answer to a `respond` reply: the message a `post` sends, with the + * ephemeral flag when private. + * + * @internal + */ +export const respondCallback = (message: { + readonly content: string + readonly allowed_mentions: Discord.MessageAllowedMentionsRequest + readonly flags: number + readonly components?: ReadonlyArray | undefined +}, visibility: ChatInteraction.Visibility): Callback => ({ + type: CHANNEL_MESSAGE_WITH_SOURCE, + data: { ...message, flags: message.flags | (visibility === "ephemeral" ? EPHEMERAL : 0) } +}) + +/** + * The answer to a `defer` reply. The visibility is fixed here and holds for + * the edit that follows. + * + * @internal + */ +export const deferCallback = (visibility: ChatInteraction.Visibility): Callback => ({ + type: DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE, + ...(visibility === "ephemeral" ? { data: { flags: EPHEMERAL } } : {}) +}) + +// Inbound + +const optional = (schema: S) => Schema.optionalKey(Schema.NullOr(schema)) + +const Input = Schema.Struct({ + type: Schema.Number, + custom_id: optional(Schema.String), + value: optional(Schema.String), + values: optional(Schema.Array(Schema.String)) +}) + +const ModalComponent = Schema.Struct({ + type: Schema.Number, + component: optional(Input), + components: optional(Schema.Array(Input)) +}) + +const CommandOption = Schema.Struct({ + type: Schema.Number, + name: optional(Schema.String), + value: optional(Schema.Unknown) +}) + +const InteractionFields = Schema.Struct({ + id: Schema.String, + application_id: optional(Schema.String), + type: Schema.Number, + token: Schema.String, + channel_id: optional(Schema.String), + member: optional(Schema.Struct({ user: optional(Schema.Struct({ id: Schema.String })) })), + user: optional(Schema.Struct({ id: Schema.String })), + message: optional(Schema.Struct({ id: Schema.String, channel_id: optional(Schema.String) })), + data: Schema.Struct({ + custom_id: optional(Schema.String), + component_type: optional(Schema.Number), + values: optional(Schema.Array(Schema.String)), + components: optional(Schema.Array(ModalComponent)), + name: optional(Schema.String), + type: optional(Schema.Number), + options: optional(Schema.Array(CommandOption)) + }) +}) + +const decodeFields = Schema.decodeUnknownOption(InteractionFields) + +/** Discord's interaction types for a command, a component click and a modal submit. */ +const APPLICATION_COMMAND = 2 +const MESSAGE_COMPONENT = 3 +const MODAL_SUBMIT = 5 +/** Discord's command type for a slash command, and its option type for a string. */ +const CHAT_INPUT = 1 +const STRING = 3 + +/** + * Credentials for answering an interaction. + * + * @internal + */ +export const answerTo = (raw: unknown): { readonly id: string; readonly token: string } | undefined => + Option.getOrUndefined(Option.map(decodeFields(raw), ({ id, token }) => ({ id, token }))) + +/** + * Credentials for editing a deferred command's response. + * + * @internal + */ +export const followUpTo = ( + raw: unknown +): { readonly id: string; readonly applicationId: string; readonly token: string } | undefined => + Option.getOrUndefined( + Option.flatMap(decodeFields(raw), ({ application_id, id, token }) => + typeof application_id === "string" && application_id !== "" + ? Option.some({ id, applicationId: application_id, token }) + : Option.none()) + ) + +/** + * The text of a slash command's options: none, or one string. Undefined for + * anything else. + */ +const commandText = (options: ReadonlyArray | null | undefined): string | undefined => { + if (options === null || options === undefined || options.length === 0) return "" + const [option] = options + return options.length === 1 && option.type === STRING && typeof option.value === "string" ? option.value : undefined +} + +/** + * The interaction a payload is, when it is a click on a button or string + * select, a submit of a modal, or a slash command with at most one string + * option. Anything else, a message or user command for one, is none. + * + * @internal + */ +export const normalize = (raw: unknown): Option.Option => { + const decoded = decodeFields(raw) + if (Option.isNone(decoded)) return Option.none() + const fields = decoded.value + const message = fields.message + const userId = fields.member?.user?.id ?? fields.user?.id + const channelId = message?.channel_id ?? fields.channel_id + if (userId === undefined || channelId === null || channelId === undefined) return Option.none() + const { data } = fields + if (fields.type === APPLICATION_COMMAND) { + const text = commandText(data.options) + // A missing command type is taken as a slash command, which is Discord's default. + return (data.type ?? CHAT_INPUT) !== CHAT_INPUT || typeof data.name !== "string" || data.name === "" || + text === undefined + ? Option.none() + : Option.some(ChatInteraction.CommandInvoked.make({ + id: data.name, + text, + thread: Normalize.thread(channelId), + userId, + raw: raw as Schema.Json + })) + } + const customIdValue = data.custom_id + if (customIdValue === null || customIdValue === undefined) return Option.none() + if (fields.type === MODAL_SUBMIT) return submitted(fields, customIdValue, channelId, userId, raw) + if (message === null || message === undefined) return Option.none() + const origin = { thread: Normalize.thread(channelId), messageId: message.id, userId, raw: raw as Schema.Json } + if (fields.type === MESSAGE_COMPONENT && data.component_type === 2) { + const { id, value } = splitCustomId(customIdValue) + return Option.some(ChatInteraction.ButtonClicked.make({ id, ...(value === undefined ? {} : { value }), ...origin })) + } + if (fields.type === MESSAGE_COMPONENT && data.component_type === 3) { + const value = data.values?.[0] + return value === undefined + ? Option.none() + : Option.some(ChatInteraction.SelectChanged.make({ id: customIdValue, value, ...origin })) + } + return Option.none() +} + +/** A modal's submit. One a command opened has no `message`, and no `messageId`. */ +const submitted = ( + fields: typeof InteractionFields.Type, + customIdValue: string, + channelId: string, + userId: string, + raw: unknown +): Option.Option => { + const values: Record = {} + for (const component of fields.data.components ?? []) { + const inputs = component.component === null || component.component === undefined + ? component.components ?? [] + : [component.component] + for (const input of inputs) { + const value = input.value ?? input.values?.[0] + if (typeof input.custom_id === "string" && value !== null && value !== undefined && value !== "") { + // An own property, so an id such as `__proto__` keeps its answer. + Object.defineProperty(values, input.custom_id, { + value, + enumerable: true, + writable: true, + configurable: true + }) + } + } + } + const { id, value: metadata } = splitCustomId(customIdValue) + const message = fields.message + return Option.some(ChatInteraction.ModalSubmitted.make({ + id, + ...(metadata === undefined ? {} : { metadata }), + values, + thread: Normalize.thread(channelId), + ...(message === null || message === undefined ? {} : { messageId: message.id }), + userId, + raw: raw as Schema.Json + })) +} + +/** + * The create payload of each command in `group`, in the group's order. + * + * @internal + */ +export const commandRequests = (group: ChatInteraction.Group): ReadonlyArray<{ + readonly type: 1 + readonly name: string + readonly description: string + readonly options?: + | ReadonlyArray<{ + readonly type: 3 + readonly name: string + readonly description: string + readonly required: boolean + }> + | undefined +}> => { + const out: Array<{ + readonly type: 1 + readonly name: string + readonly description: string + readonly options?: ReadonlyArray<{ + readonly type: 3 + readonly name: string + readonly description: string + readonly required: boolean + }> + }> = [] + for (const definition of (group.definitions as ReadonlyMap).values()) { + if (definition.kind !== "command") continue + const { argument } = definition + out.push({ + type: CHAT_INPUT, + name: definition.id, + description: definition.description, + ...(argument === undefined ? {} : { + options: [{ + type: STRING, + name: argument.name, + description: argument.description, + required: argument.required + }] + }) + }) + } + return out +} diff --git a/packages/chat/discord/src/internal/length.ts b/packages/chat/discord/src/internal/length.ts new file mode 100644 index 00000000000..6c514f99480 --- /dev/null +++ b/packages/chat/discord/src/internal/length.ts @@ -0,0 +1,44 @@ +/** + * Discord's limit on `content`, in code points, inclusive. + * + * @internal + */ +export const messageLength = 2000 + +/** + * Code points, which is what Discord counts: a recorded probe took 2,000 + * astral emoji (4,000 UTF-16 units) and refused 1,999 letters and a flag. + * + * @internal + */ +export const codePoints = (text: string): number => { + let count = 0 + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i) + if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) { + const low = text.charCodeAt(i + 1) + if (low >= 0xdc00 && low <= 0xdfff) i++ + } + count++ + } + return count +} + +/** + * Cuts `text` to at most `max` code points. + * + * @internal + */ +export const cut = (text: string, max: number): string => { + let count = 0 + for (let i = 0; i < text.length; i++) { + if (count === max) return text.slice(0, i) + const code = text.charCodeAt(i) + if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) { + const low = text.charCodeAt(i + 1) + if (low >= 0xdc00 && low <= 0xdfff) i++ + } + count++ + } + return text +} diff --git a/packages/chat/discord/src/internal/mentions.ts b/packages/chat/discord/src/internal/mentions.ts new file mode 100644 index 00000000000..3bc8a34868f --- /dev/null +++ b/packages/chat/discord/src/internal/mentions.ts @@ -0,0 +1,101 @@ +import type { Mention } from "effect/chat/ChatMessage" + +type Kind = Mention["kind"] + +interface Target { + readonly kind: Kind + readonly id: string +} + +/** A mention token as Discord writes it in `content`. */ +const tokenPattern = /<@!?(\d+)>|<@&(\d+)>|<#(\d+)>|@(everyone|here)/y + +const targetOf = (match: RegExpExecArray): Target => { + const [, user, role, channel, broadcast] = match + if (user !== undefined) return { kind: "user", id: user } + if (role !== undefined) return { kind: "role", id: role } + if (channel !== undefined) return { kind: "channel", id: channel } + return { kind: "broadcast", id: broadcast! } +} + +const tokenAt = (text: string, index: number): RegExpExecArray | null => { + tokenPattern.lastIndex = index + return tokenPattern.exec(text) +} + +/** + * Parses one token, `<@id>`, `<@!id>`, `<@&id>`, `<#id>`, `@everyone` + * or `@here`, as the whole of `slice`. + * + * @internal + */ +export const parseToken = (slice: string): Target | undefined => { + const match = tokenAt(slice, 0) + return match === null || match[0].length !== slice.length ? undefined : targetOf(match) +} + +/** + * A token in `content` outside code, with its offsets. + * + * @internal + */ +export interface Token extends Target { + readonly start: number + readonly end: number +} + +const isEscapable = (char: string | undefined): boolean => char !== undefined && /[^0-9A-Za-z\s]/.test(char) + +/** The index past the closing run of exactly `n` backticks, or -1. */ +const closingRun = (text: string, from: number, n: number): number => { + for (let i = from; i < text.length; i++) { + if (text[i] !== "`") continue + let run = 1 + while (text[i + run] === "`") run++ + if (run === n && i > from) return i + run + i += run - 1 + } + return -1 +} + +/** + * The mention tokens in `content`, in order, skipping code blocks, inline + * code and tokens escaped with a backslash, as Discord's Markdown does. + * + * @internal + */ +export const tokens = (text: string): Array => { + const found: Array = [] + let i = 0 + while (i < text.length) { + const char = text[i] + if (char === "\\" && isEscapable(text[i + 1])) { + i += 2 + continue + } + if (char === "`") { + let n = 1 + while (text[i + n] === "`") n++ + if (n >= 3) { + const end = text.indexOf("```", i + 3) + if (end !== -1) { + i = end + 3 + continue + } + } + const end = closingRun(text, i + n, n) + i = end === -1 ? i + n : end + continue + } + if (char === "<" || char === "@") { + const match = tokenAt(text, i) + if (match !== null) { + found.push({ ...targetOf(match), start: i, end: i + match[0].length }) + i += match[0].length + continue + } + } + i++ + } + return found +} diff --git a/packages/chat/discord/src/internal/normalize.ts b/packages/chat/discord/src/internal/normalize.ts new file mode 100644 index 00000000000..312f1527272 --- /dev/null +++ b/packages/chat/discord/src/internal/normalize.ts @@ -0,0 +1,347 @@ +import type { ChatEvent, ReactionAdded, ReactionRemoved, ThreadKind } from "effect/chat/ChatEvent" +import type { Attachment, ChatMessage, Mention } from "effect/chat/ChatMessage" +import * as ChatThread from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import * as Mentions from "./mentions.ts" + +/** @internal */ +export const platform = "discord" + +/** @internal */ +export const thread = (channelId: string): ChatThread.ChatThread => ChatThread.make(platform, channelId) + +/** + * The message types that are a message: `DEFAULT`, `REPLY`, + * `CHAT_INPUT_COMMAND` and `CONTEXT_MENU_COMMAND`. Inbound and history + * both read it, and a type Discord adds later stays out. + * + * @internal + */ +export const messageTypes: ReadonlySet = new Set([0, 19, 20, 23]) + +/** @internal */ +export const REPLY = 19 + +/** + * The thread starter placeholder in a thread started from a message. + * + * @internal + */ +export const THREAD_STARTER_MESSAGE = 21 + +/** + * Discord's channel type as a `kind`: 10, 11 and 12 are threads, 1 and 3 + * directs, everything else a channel, unknown types included. + * + * @internal + */ +export const kindOf = (channelType: number): ThreadKind => + channelType === 10 || channelType === 11 || channelType === 12 + ? "thread" + : channelType === 1 || channelType === 3 + ? "direct" + : "channel" + +/** @internal */ +export const isThreadType = (channelType: number): boolean => kindOf(channelType) === "thread" + +const optional = (schema: S) => Schema.optionalKey(Schema.NullOr(schema)) + +const UserFields = Schema.Struct({ + id: Schema.String, + username: optional(Schema.String), + global_name: optional(Schema.String), + bot: optional(Schema.Boolean) +}) + +const MemberFields = Schema.Struct({ + nick: optional(Schema.String), + roles: optional(Schema.Array(Schema.String)) +}) + +const AttachmentFields = Schema.Struct({ + id: Schema.String, + filename: optional(Schema.String), + content_type: optional(Schema.String), + size: optional(Schema.Int) +}) + +const ReferenceFields = Schema.Struct({ + type: optional(Schema.Number), + message_id: optional(Schema.String), + channel_id: optional(Schema.String) +}) + +/** + * What the normaliser reads from a message. + * + * @internal + */ +export const MessageFields = Schema.Struct({ + id: Schema.String, + channel_id: Schema.String, + type: Schema.Number, + content: optional(Schema.String), + timestamp: Schema.DateTimeUtcFromString, + author: UserFields, + webhook_id: optional(Schema.String), + guild_id: optional(Schema.String), + channel_type: optional(Schema.Number), + member: optional(MemberFields), + mentions: optional(Schema.Array(Schema.Struct({ id: Schema.String }))), + mention_roles: optional(Schema.Array(Schema.String)), + mention_everyone: optional(Schema.Boolean), + attachments: optional(Schema.Array(AttachmentFields)), + message_reference: optional(ReferenceFields), + referenced_message: optional(Schema.Struct({ author: optional(Schema.Struct({ id: Schema.String })) })) +}) + +/** @internal */ +export type MessageFields = typeof MessageFields.Type + +const ReactionFields = Schema.Struct({ + user_id: Schema.String, + channel_id: Schema.String, + message_id: Schema.String, + guild_id: optional(Schema.String), + message_author_id: optional(Schema.String), + member: optional(MemberFields), + emoji: Schema.Struct({ id: optional(Schema.String), name: optional(Schema.String) }) +}) + +const decodeMessage = Schema.decodeUnknownOption(MessageFields) +const decodeReaction = Schema.decodeUnknownOption(ReactionFields) + +/** @internal */ +export const decodeMessageFields = (raw: unknown): MessageFields | undefined => + Option.getOrUndefined(decodeMessage(raw)) + +/** + * An attachment's id: the message's id and the attachment's, since a download reads the message again. + * + * @internal + */ +export const attachmentId = (messageId: string, id: string): string => `${messageId}/${id}` + +/** @internal */ +export const parseAttachmentId = ( + id: string +): { readonly messageId: string; readonly attachmentId: string } | undefined => { + const parts = id.split("/") + return parts.length === 2 && parts[0] !== "" && parts[1] !== "" + ? { messageId: parts[0], attachmentId: parts[1] } + : undefined +} + +/** + * The mentions in `content`: a token outside code that Discord says + * resolved, through `mentions` for a user, `mention_roles` for a role and + * `mention_everyone` for `@everyone` and `@here`. A channel token has no + * confirming field, so the token alone counts. + */ +const mentionsOf = (fields: MessageFields, text: string, botUserId: string): Array => { + const users = new Set((fields.mentions ?? []).map((user) => user.id)) + const roles = new Set(fields.mention_roles ?? []) + const everyone = fields.mention_everyone === true + const found: Array = [] + for (const token of Mentions.tokens(text)) { + const resolved = token.kind === "user" + ? users.has(token.id) + : token.kind === "role" + ? roles.has(token.id) + : token.kind === "broadcast" + ? everyone + : true + if (!resolved) continue + found.push({ + kind: token.kind, + id: token.id, + start: token.start, + end: token.end, + isSelf: token.kind === "user" && token.id === botUserId + }) + } + return found +} + +const nonEmpty = (value: string | null | undefined): string | undefined => + value === null || value === undefined || value === "" ? undefined : value + +/** + * A message payload, from the gateway or REST, as a `ChatMessage`. + * `undefined` for a type that is not a message. + * + * @internal + */ +export const message = ( + raw: Schema.Json, + fields: MessageFields, + context: { readonly botUserId: string } +): ChatMessage | undefined => { + if (!messageTypes.has(fields.type)) return undefined + const text = fields.content ?? "" + const webhook = nonEmpty(fields.webhook_id) + const name = webhook === undefined + ? nonEmpty(fields.member?.nick) ?? nonEmpty(fields.author.global_name) ?? nonEmpty(fields.author.username) + : nonEmpty(fields.author.username) + const reference = fields.message_reference + const replyId = fields.type === REPLY && (reference?.type ?? 0) === 0 ? nonEmpty(reference?.message_id) : undefined + const replyAuthor = fields.referenced_message?.author?.id + const attachments = (fields.attachments ?? []).map((attachment): Attachment => { + const name = nonEmpty(attachment.filename) + const contentType = nonEmpty(attachment.content_type) + return { + id: attachmentId(fields.id, attachment.id), + ...(name === undefined ? {} : { name }), + ...(contentType === undefined ? {} : { contentType }), + ...(typeof attachment.size === "number" ? { size: attachment.size } : {}) + } + }) + return { + id: fields.id, + thread: thread(fields.channel_id), + author: { + id: webhook ?? fields.author.id, + ...(name === undefined ? {} : { name }), + isBot: webhook !== undefined || fields.author.bot === true, + isSelf: webhook === undefined && fields.author.id === context.botUserId + }, + text, + mentions: mentionsOf(fields, text, context.botUserId), + ...(replyId === undefined + ? {} + : { + replyTo: { + messageId: replyId, + ...(replyAuthor === undefined ? {} : { authorId: replyAuthor, isSelf: replyAuthor === context.botUserId }) + } + }), + attachments, + timestamp: fields.timestamp, + raw + } +} + +/** + * A reaction's emoji: the Unicode emoji, or `custom:{name}` for a guild's own. + * + * @internal + */ +export const emojiOf = ( + emoji: { readonly id?: string | null | undefined; readonly name?: string | null | undefined } +) => + nonEmpty(emoji.id) === undefined + ? nonEmpty(emoji.name) + : `custom:${nonEmpty(emoji.name) ?? emoji.id}` + +/** + * The result of `normalize`: the events, or the channel whose type is missing. + * + * @internal + */ +export type Normalized = + | { readonly _tag: "Events"; readonly events: ReadonlyArray } + | { readonly _tag: "MissingChannelType"; readonly channelId: string } + +const none: Normalized = { _tag: "Events", events: [] } + +const events = (events: ReadonlyArray): Normalized => ({ _tag: "Events", events }) + +/** @internal */ +export const normalize = ( + dispatch: { readonly t: string; readonly d: unknown }, + context: { readonly botUserId: string; readonly channelType?: number | undefined } +): Normalized => { + const raw = dispatch.d as Schema.Json + switch (dispatch.t) { + case "MESSAGE_CREATE": { + const fields = decodeMessageFields(raw) + if (fields === undefined || !messageTypes.has(fields.type)) return none + const channelType = fields.channel_type ?? context.channelType + if (channelType === undefined) return { _tag: "MissingChannelType", channelId: fields.channel_id } + const created = message(raw, fields, context) + return created === undefined ? none : events([{ + _tag: "MessageCreated", + thread: created.thread, + kind: kindOf(channelType), + message: created, + raw + }]) + } + case "MESSAGE_REACTION_ADD": + case "MESSAGE_REACTION_REMOVE": { + const fields = Option.getOrUndefined(decodeReaction(raw)) + const emoji = fields === undefined ? undefined : emojiOf(fields.emoji) + if (fields === undefined || emoji === undefined) return none + const authorId = nonEmpty(fields.message_author_id) + const reaction = { + thread: thread(fields.channel_id), + messageId: fields.message_id, + ...(authorId === undefined ? {} : { messageAuthorId: authorId }), + userId: fields.user_id, + emoji, + raw + } + return events([ + dispatch.t === "MESSAGE_REACTION_ADD" + ? { _tag: "ReactionAdded", ...reaction } satisfies ReactionAdded + : { _tag: "ReactionRemoved", ...reaction } satisfies ReactionRemoved + ]) + } + default: + return none + } +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** @internal */ +export const memberOf = (raw: unknown): Option.Option<{ + readonly guildId: string + readonly roles: ReadonlyArray + readonly nick?: string | undefined +}> => { + if (!isObject(raw)) return Option.none() + const guildId = raw.guild_id + const member = raw.member + if (typeof guildId !== "string" || guildId === "" || !isObject(member)) return Option.none() + const roles = Array.isArray(member.roles) ? member.roles.filter((role) => typeof role === "string") : [] + const nick = typeof member.nick === "string" && member.nick !== "" ? member.nick : undefined + return Option.some({ guildId, roles, ...(nick === undefined ? {} : { nick }) }) +} + +/** @internal */ +export const guildIdOf = (raw: unknown): Option.Option => + isObject(raw) && typeof raw.guild_id === "string" && raw.guild_id !== "" ? Option.some(raw.guild_id) : Option.none() + +const discordEpoch = BigInt(1420070400000) + +/** + * When a snowflake was made. + * + * @internal + */ +export const snowflakeTime = (id: string): DateTime.Utc | undefined => { + try { + return DateTime.makeUnsafe(Number((BigInt(id) >> BigInt(22)) + discordEpoch)) + } catch { + return undefined + } +} + +/** + * Orders two snowflakes. + * + * @internal + */ +export const compareIds = (a: string, b: string): number => { + try { + const x = BigInt(a) + const y = BigInt(b) + return x < y ? -1 : x > y ? 1 : 0 + } catch { + return a.length !== b.length ? a.length - b.length : a < b ? -1 : a > b ? 1 : 0 + } +} diff --git a/packages/chat/discord/src/internal/options.ts b/packages/chat/discord/src/internal/options.ts new file mode 100644 index 00000000000..302266d7e03 --- /dev/null +++ b/packages/chat/discord/src/internal/options.ts @@ -0,0 +1,45 @@ +import * as Context from "effect/Context" + +/** @internal */ +export interface PostOptions { + readonly notify?: { + readonly users?: ReadonlyArray | undefined + readonly roles?: ReadonlyArray | undefined + readonly everyone?: boolean | undefined + readonly repliedUser?: boolean | undefined + } | undefined + readonly suppressEmbeds?: boolean | undefined + readonly failIfNotExists?: boolean | undefined + readonly autoArchiveDuration?: 60 | 1440 | 4320 | 10080 | undefined +} + +/** @internal */ +export const CurrentPostOptions = Context.Reference("@effect/chat-discord/CurrentPostOptions", { + defaultValue: () => ({}) +}) + +/** + * Discord's `SUPPRESS_EMBEDS` message flag. + * + * @internal + */ +export const SUPPRESS_EMBEDS = 1 << 2 + +/** + * The `allowed_mentions` for a write: nobody, unless `notify` says otherwise. + * + * @internal + */ +export const allowedMentions = (notify: PostOptions["notify"]) => ({ + parse: notify?.everyone === true ? ["everyone" as const] : [], + users: notify?.users ?? [], + roles: notify?.roles ?? [], + replied_user: notify?.repliedUser === true +}) + +/** + * The flags for a write. + * + * @internal + */ +export const flags = (options: PostOptions): number => options.suppressEmbeds === false ? 0 : SUPPRESS_EMBEDS diff --git a/packages/chat/discord/test/DiscordChat.test.ts b/packages/chat/discord/test/DiscordChat.test.ts new file mode 100644 index 00000000000..35b67f2159b --- /dev/null +++ b/packages/chat/discord/test/DiscordChat.test.ts @@ -0,0 +1,483 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { assert, describe, it } from "@effect/vitest" +import type { ChatAdapter } from "effect/chat/ChatAdapter" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import * as Option from "effect/Option" +import * as Stream from "effect/Stream" +import * as TestClock from "effect/testing/TestClock" +import { + adapter, + answer, + botUserId, + discordLayer, + errorResponse, + events, + type Fake, + guild, + makeFake, + scratch, + statusCodeError, + step +} from "./utils.ts" + +/** Runs `body` with the adapter over a fake answering from `handlers`. */ +const withAdapter = ( + handlers: Parameters[0], + body: (discord: ChatAdapter, fake: Fake) => Effect.Effect +) => { + const fake = makeFake(handlers) + return Effect.flatMap(adapter, (discord) => body(discord, fake)).pipe( + Effect.provide(discordLayer(fake)) + ) as Effect.Effect +} + +const reason = (effect: Effect.Effect) => + Effect.flip(effect).pipe( + Effect.map((error) => ({ op: error.op, tag: error.reason._tag, detail: error.reason.detail, retry: error.retry })) + ) + +const channel = DiscordChat.thread(scratch) +const posted = step("posts", "channel-plain").response +const answers = { createMessage: () => Effect.succeed(posted) } + +const history = step("history", "thread-history") +const threadId: string = step("history", "thread-start").response.id +const thread = DiscordChat.thread(threadId) +const threadChannel = step("history", "thread-get-channel").response +const forbidden = () => Effect.fail(errorResponse(403, { code: 50001, message: "Missing Access" })) + +const noMentions = { parse: [], users: [], roles: [], replied_user: false } + +describe("post", () => { + it.effect("sends the Markdown as is, notifying nobody and suppressing embeds", () => + withAdapter(answers, (discord, fake) => + Effect.gen(function*() { + yield* discord.post(channel, { markdown: "hi <@1> @everyone **bold**" }) + assert.deepStrictEqual(fake.called("createMessage"), [[scratch, { + content: "hi <@1> @everyone **bold**", + allowed_mentions: noMentions, + flags: 4 + }]]) + }))) + + it.effect("withPostOptions sets mentions, embeds and the reference, merging nested calls", () => + withAdapter(answers, (discord, fake) => + Effect.gen(function*() { + yield* discord.post(channel, { markdown: "hi", replyTo: { messageId: "42" } }).pipe( + DiscordChat.withPostOptions({ notify: { users: ["1"], roles: ["2"], everyone: true, repliedUser: true } }), + DiscordChat.withPostOptions({ suppressEmbeds: false, failIfNotExists: false }) + ) + assert.deepStrictEqual(fake.called("createMessage")[0][1], { + content: "hi", + allowed_mentions: { parse: ["everyone"], users: ["1"], roles: ["2"], replied_user: true }, + flags: 0, + message_reference: { message_id: "42", fail_if_not_exists: false } + }) + }))) + + it.effect("counts the 2,000 limit in code points, failing past it without a call", () => + withAdapter(answers, (discord, fake) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* reason(discord.post(channel, { markdown: "a".repeat(2001) })), { + op: "post", + tag: "TooLarge", + detail: "message_too_long", + retry: "denied" + }) + assert.strictEqual(fake.calls.length, 0) + // 2,000 astral emoji are 4,000 UTF-16 units, and Discord took them. + yield* discord.post(channel, { markdown: "\u{1F600}".repeat(2000) }) + assert.strictEqual(fake.called("createMessage").length, 1) + }))) + + it.effect("maps Discord's failures, sending a write once and a 5xx on it as uncertain", () => + Effect.forEach( + [ + [answer(step("errors", "post-unknown-channel")), { tag: "NotFound", detail: "10003", retry: "denied" }], + [answer(step("errors", "length-2001-ascii")), { tag: "TooLarge", detail: "50035", retry: "denied" }], + [Effect.fail(statusCodeError(502)), { tag: "PlatformUnavailable", detail: "http_502", retry: "uncertain" }] + ] as const, + ([failure, expected]) => + withAdapter({ createMessage: () => failure }, (discord, fake) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* reason(discord.post(channel, { markdown: "x" })), { op: "post", ...expected }) + assert.strictEqual(fake.calls.length, 1) + })), + { discard: true } + )) + + it.effect("a 5xx on a read is retried", () => { + let attempts = 0 + return withAdapter({ + getMessage: () => attempts++ < 1 ? Effect.fail(statusCodeError(503)) : Effect.succeed(posted) + }, (discord) => + Effect.gen(function*() { + const fiber = yield* Effect.forkChild(discord.fetchMessage(channel, posted.id)) + yield* TestClock.adjust("1 second") + assert.strictEqual((yield* Fiber.join(fiber)).id, posted.id) + })) + }) + + it.effect("rejects an address that is not a Discord channel id", () => + withAdapter({}, (discord, fake) => + Effect.gen(function*() { + const result = yield* reason(discord.post(DiscordChat.thread("../C123"), { markdown: "x" })) + assert.strictEqual(result.tag, "InvalidRequest") + assert.strictEqual(fake.calls.length, 0) + }))) + + it.effect("a post answer with an invalid timestamp fails instead of throwing", () => + withAdapter( + { createMessage: () => Effect.succeed({ ...posted, timestamp: "not a date" }) }, + (discord) => + Effect.gen(function*() { + const result = yield* reason(discord.post(channel, { markdown: "x" })) + assert.deepStrictEqual([result.tag, result.detail], ["Unknown", "invalid_timestamp"]) + }) + )) + + it.effect("the adapter's own file count and size check is file_refused, without a call", () => + withAdapter({}, (discord, fake) => + Effect.gen(function*() { + const file = { name: "fixture.txt", contentType: "text/plain", content: new Uint8Array(1) } + const tooMany = Array.from({ length: discord.descriptor.limits.filesPerMessage + 1 }, () => file) + const tooBig = [{ ...file, content: new Uint8Array(discord.descriptor.limits.uploadBytes + 1) }] + for (const files of [tooMany, tooBig]) { + assert.deepStrictEqual(yield* reason(discord.post(channel, { markdown: "x", files })), { + op: "post", + tag: "TooLarge", + detail: "file_refused", + retry: "denied" + }) + } + assert.strictEqual(fake.calls.length, 0) + }))) +}) + +describe("actions", () => { + it.effect("go out as action rows: five buttons to a row and a select alone in its row", () => + withAdapter(answers, (discord, fake) => + Effect.gen(function*() { + yield* discord.post(channel, { + markdown: "x", + actions: [ + { _tag: "Button", id: "go", label: "Go", style: "primary", value: "v" }, + { _tag: "Button", id: "stop", label: "Stop", style: "danger" }, + { _tag: "LinkButton", label: "Docs", url: "https://effect.website" }, + { + _tag: "Select", + id: "pick", + options: [{ label: "A", value: "a" }, { label: "B", value: "b" }], + selected: "b" + }, + ...Array.from({ length: 3 }, (_, i) => ({ _tag: "Button" as const, id: "b" + i, label: "B" + i })) + ] + }) + assert.deepStrictEqual(fake.called("createMessage")[0][1].components, [ + { + type: 1, + components: [ + { type: 2, style: 1, label: "Go", custom_id: "go\u241Fv" }, + { type: 2, style: 4, label: "Stop", custom_id: "stop" }, + { type: 2, style: 5, label: "Docs", url: "https://effect.website" } + ] + }, + { + type: 1, + components: [{ + type: 3, + custom_id: "pick", + options: [{ label: "A", value: "a" }, { label: "B", value: "b", default: true }], + min_values: 1, + max_values: 1 + }] + }, + { type: 1, components: [0, 1, 2].map((i) => ({ type: 2, style: 2, label: "B" + i, custom_id: "b" + i })) } + ]) + }))) + + it.effect("an id holding the join, which would forge a value, fails before the call", () => + withAdapter({}, (discord, fake) => + Effect.gen(function*() { + const result = yield* reason( + discord.post(channel, { markdown: "x", actions: [{ _tag: "Button", id: "a\u241Fb", label: "x" }] }) + ) + assert.strictEqual(result.tag, "InvalidRequest") + assert.strictEqual(fake.calls.length, 0) + }))) +}) + +it.effect("edit sends allowed_mentions and the SUPPRESS_EMBEDS flag", () => + withAdapter({ updateMessage: () => Effect.succeed(posted) }, (discord, fake) => + Effect.gen(function*() { + yield* discord.edit(channel, "1", { markdown: "now with https://effect.website" }) + assert.deepStrictEqual(fake.called("updateMessage"), [[scratch, "1", { + content: "now with https://effect.website", + allowed_mentions: noMentions, + flags: 4, + components: [] + }]]) + }))) + +it.effect("edit fails TooLarge past 2,000 code points without a call", () => + withAdapter({}, (discord, fake) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* reason(discord.edit(channel, "1", { markdown: "a".repeat(2001) })), { + op: "edit", + tag: "TooLarge", + detail: "message_too_long", + retry: "denied" + }) + assert.strictEqual(fake.calls.length, 0) + }))) + +describe("startThread", () => { + it.effect("names the thread after the message, cut to 100 characters", () => + withAdapter({ + getMessage: () => Effect.succeed({ ...posted, content: " first line\nsecond " + "x".repeat(200) }), + createThreadFromMessage: () => Effect.succeed(step("history", "thread-start").response) + }, (discord, fake) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* discord.startThread(channel, threadId), thread) + assert.strictEqual( + fake.called("createThreadFromMessage")[0][2].name, + ("first line second " + "x".repeat(200)).slice(0, 100) + ) + }))) + + it.effect("160004 gives the existing thread, whose id is the message's", () => + withAdapter( + { createThreadFromMessage: () => answer(step("errors", "thread-start-again")) }, + (discord) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* discord.startThread(channel, threadId, { name: "x" }), thread) + }) + )) + + it.effect("50024 on a message in a thread gives the thread, and elsewhere fails", () => + withAdapter({ + createThreadFromMessage: () => answer(step("errors", "forum-start-thread-again")), + getChannel: (id: string) => + Effect.succeed(id === threadId ? threadChannel : step("channels", "get-channel-dm").response) + }, (discord) => + Effect.gen(function*() { + assert.deepStrictEqual(yield* discord.startThread(thread, "1", { name: "x" }), thread) + assert.strictEqual((yield* reason(discord.startThread(channel, "1", { name: "x" }))).detail, "50024") + }))) +}) + +describe("history and opener", () => { + /** `listMessages` over `all`, newest first, as Discord answers with either bound. */ + const listFrom = + (all: ReadonlyArray) => + (_: string, { after, before, limit }: { after?: string; before?: string; limit: number }) => + Effect.succeed( + all.filter((raw) => + (after === undefined || BigInt(raw.id) > BigInt(after)) && + (before === undefined || BigInt(raw.id) < BigInt(before)) + ).sort((a, b) => (BigInt(b.id) > BigInt(a.id) ? 1 : -1)).slice(0, limit) + ) + + it.effect("history pages until a short page", () => { + const many = Array.from( + { length: 250 }, + (_, i) => ({ ...posted, id: String(1570607253965246584n + BigInt(i + 1)) }) + ) + return withAdapter({ listMessages: listFrom(many) }, (discord) => + Effect.gen(function*() { + const found = yield* Stream.runCollect(discord.history(channel, { direction: "backward" })) + assert.deepStrictEqual(Array.from(found, (message) => message.id), many.map((raw) => raw.id).reverse()) + })) + }) + + it.effect("history of a thread started from a message has its opener first, addressed to the parent", () => + withAdapter({ listMessages: listFrom(history.response) }, (discord) => + Effect.gen(function*() { + const [opener, ...replies] = Array.from( + yield* Stream.runCollect(discord.history(thread, { direction: "forward" })) + ) + assert.deepStrictEqual([opener.id, opener.thread], [threadId, channel]) + assert.deepStrictEqual( + replies.map((message) => message.id), + history.response.filter((raw: any) => raw.type !== 21).map((raw: any) => raw.id).reverse() + ) + for (const message of replies) assert.deepStrictEqual(message.thread, thread) + }))) + + it.effect("history of a thread whose opener was deleted starts at the first reply", () => { + const deleted = step("history", "thread-deleted-history").response + const deletedThread = DiscordChat.thread(step("history", "thread-deleted-start").response.id) + return withAdapter({ listMessages: listFrom(deleted) }, (discord) => + Effect.gen(function*() { + const found = yield* Stream.runCollect(discord.history(deletedThread, { direction: "forward" })) + assert.deepStrictEqual(Array.from(found, (message) => message.id), [ + deleted.find((raw: any) => raw.type === 0).id + ]) + })) + }) + + it.effect("opener reads the message in the parent for a thread started from a message", () => + withAdapter({ + getChannel: () => Effect.succeed(threadChannel), + getMessage: (channelId: string) => + answer(step("history", channelId === threadId ? "thread-get-opener-by-thread-id" : "thread-get-opener")) + }, (discord) => + Effect.gen(function*() { + const opener = Option.getOrThrow(yield* discord.opener(thread)) + assert.strictEqual(opener.id, threadId) + assert.deepStrictEqual(opener.thread, channel) + }))) + + it.effect("opener reads a forum post's starter in the post", () => { + const starter = step("history", "forum-get-starter").response + return withAdapter({ + getChannel: () => Effect.succeed(step("history", "forum-post").response), + getMessage: () => Effect.succeed(starter) + }, (discord) => + Effect.gen(function*() { + const opener = Option.getOrThrow(yield* discord.opener(DiscordChat.thread(starter.id))) + assert.deepStrictEqual([opener.id, opener.thread.id], [starter.id, starter.id]) + })) + }) + + it.effect("opener is None for a deleted opener and for a channel", () => + withAdapter({ + getChannel: (id: string) => + Effect.succeed(id === scratch ? step("channels", "get-channel-text").response : threadChannel), + getMessage: () => answer(step("history", "thread-deleted-get-opener")) + }, (discord) => + Effect.gen(function*() { + assert.isTrue(Option.isNone(yield* discord.opener(thread))) + assert.isTrue(Option.isNone(yield* discord.opener(channel))) + }))) + + it.effect("history skips an opener the bot can't read and starts at the first reply", () => { + const { referenced_message: _, ...bare } = history.response.find((raw: any) => raw.type === 21) + const reply = history.response.find((raw: any) => raw.type === 0) + return withAdapter( + { listMessages: () => Effect.succeed([reply, bare]), getMessage: forbidden }, + (discord) => + Effect.gen(function*() { + const found = yield* Stream.runCollect(discord.history(thread, { direction: "forward" })) + assert.deepStrictEqual(Array.from(found, (message) => message.id), [reply.id]) + }) + ) + }) + + it.effect("opener is None when the bot can't read it", () => + withAdapter( + { getChannel: () => Effect.succeed(threadChannel), getMessage: forbidden }, + (discord) => + Effect.gen(function*() { + assert.isTrue(Option.isNone(yield* discord.opener(thread))) + }) + )) +}) + +it.effect("download reads the message again for the URL and fails TooLarge past maxBytes", () => { + const raw = step("posts", "attachment-fixed").response + const attachment = { id: `${raw.id}/${raw.attachments[0].id}` } + const content = new TextEncoder().encode("hello from effect/chat\n") + return withAdapter( + { getMessage: () => Effect.succeed(raw), download: () => Effect.succeed(content) }, + (discord, fake) => + Effect.gen(function*() { + const bytes = yield* Stream.runCollect(discord.download(channel, attachment, { maxBytes: content.length })) + assert.deepStrictEqual(Array.from(bytes), [content]) + assert.deepStrictEqual(fake.called("download")[0], [raw.attachments[0].url]) + const tooLarge = yield* reason(Stream.runCollect(discord.download(channel, attachment, { maxBytes: 5 }))) + assert.deepStrictEqual([tooLarge.tag, tooLarge.detail], ["TooLarge", "max_bytes"]) + }) + ) +}) + +describe("getThread and getUser", () => { + it.effect("getThread gives the kind, name and parent", () => { + const dm = step("channels", "get-channel-dm").response + return withAdapter( + { getChannel: (id: string) => Effect.succeed(id === threadId ? threadChannel : dm) }, + (discord) => + Effect.gen(function*() { + const info = yield* discord.getThread(thread) + assert.deepStrictEqual([info.kind, info.name, info.parent], [ + "thread", + "Fixture thread from a message", + channel + ]) + const direct = yield* discord.getThread(DiscordChat.thread(dm.id)) + assert.deepStrictEqual([direct.kind, direct.name], ["direct", undefined]) + }) + ) + }) + + it.effect("getUser with in gives the guild nick, with the member as raw", () => { + const member = { ...step("users", "get-member-bot").response, nick: "Janny" } + return withAdapter({ + getChannel: () => Effect.succeed(step("channels", "get-channel-text").response), + getGuildMember: () => Effect.succeed(member), + getUser: () => answer(step("users", "get-user-bot")) + }, (discord, fake) => + Effect.gen(function*() { + const user = yield* discord.getUser(botUserId, { in: channel }) + assert.strictEqual(user.name, "Janny") + assert.deepStrictEqual(user.raw, member) + assert.deepStrictEqual(fake.called("getGuildMember"), [[guild, botUserId]]) + assert.strictEqual((yield* discord.getUser(botUserId)).name, "Janitor") + })) + }) +}) + +describe("dispatch without channel_type", () => { + const missing = events("channel-message-without-channel-type")[0] + /** Dispatches `missing` through `DiscordChat.dispatch`, keeping what the handler saw. */ + const dispatcher = (seen: Array) => + Effect.map(DiscordChat.DiscordChat, (discord) => + discord.dispatch(missing).pipe( + Effect.provideService( + ChatHandler, + ChatHandler.of({ handle: (event) => Effect.sync(() => void seen.push(event)) }) + ) + )) + + it.effect("looks the channel up once", () => + withAdapter({ getChannel: () => Effect.succeed(threadChannel) }, (_, fake) => + Effect.gen(function*() { + const seen: Array = [] + const dispatch = yield* dispatcher(seen) + yield* dispatch + yield* dispatch + assert.deepStrictEqual(seen.map((event) => event._tag === "MessageCreated" && event.kind), ["thread", "thread"]) + assert.deepStrictEqual(fake.called("getChannel"), [[scratch]]) + }))) + + it.effect("fails NotAccepted when the lookup fails, and tries again next time", () => { + let attempts = 0 + return withAdapter({ + getChannel: () => + attempts++ < 4 + ? Effect.fail(statusCodeError(500)) + : Effect.succeed(step("channels", "get-channel-text").response) + }, () => + Effect.gen(function*() { + const seen: Array = [] + const dispatch = yield* dispatcher(seen) + const fiber = yield* Effect.forkChild(Effect.flip(dispatch)) + yield* TestClock.adjust("10 seconds") + assert.instanceOf(yield* Fiber.join(fiber), NotAccepted) + assert.strictEqual(seen.length, 0) + yield* dispatch + assert.strictEqual(seen.length, 1) + })) + }) + + it.effect("drops the event when the lookup is denied", () => + withAdapter({ getChannel: () => answer(step("errors", "get-channel-unknown")) }, () => + Effect.gen(function*() { + const seen: Array = [] + yield* Effect.flatten(dispatcher(seen)) + assert.strictEqual(seen.length, 0) + }))) +}) diff --git a/packages/chat/discord/test/commands.test.ts b/packages/chat/discord/test/commands.test.ts new file mode 100644 index 00000000000..838ca43ac01 --- /dev/null +++ b/packages/chat/discord/test/commands.test.ts @@ -0,0 +1,116 @@ +/** + * Slash commands against what Discord sent and what it took, recorded in + * #test of guild 840178841540362250 during EFF-3802: + * `fixtures/recorded/commands.json`. + */ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as TestClock from "effect/testing/TestClock" +import { botUserId, discordLayer, errorResponse, type Fake, makeFake, recordedCommands as recorded } from "./utils.ts" + +// The definitions the capture ran, from eff-3802-capture.ts. +const actions = [ + ChatInteraction.button("cap-ack", { label: "Ack" }).of(), + ChatInteraction.button("cap-open", { label: "Open modal", style: "primary" }).of() +] +const Modal = ChatInteraction.modal("cap-modal", { + title: "Capture", + submitLabel: "Send", + fields: { note: ChatInteraction.textInput({ label: "Note (type err for a field error)" }) } +}) + +/** The capture's handler, by the command's text. */ +const reply = (command: ChatInteraction.CommandInvoked): ChatInteraction.CommandReply => { + switch (command.text) { + case "public": + return ChatInteraction.respond({ + markdown: "**Public answer** with _Markdown_ and buttons", + actions, + visibility: "public" + }) + case "mention": + return ChatInteraction.respond({ markdown: "Private answer mentioning <@" + command.userId + ">" }) + case "modal": + return Modal.open() + case "defer": + return ChatInteraction.defer(command) + case "defer-public": + return ChatInteraction.defer(command, { visibility: "public" }) + default: + return ChatInteraction.respond({ + markdown: "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons", + actions + }) + } +} + +const handler = ChatInteraction.Handler.of({ + click: () => Effect.succeed(ChatInteraction.ack), + submit: () => Effect.succeed(ChatInteraction.close), + command: (command) => Effect.succeed(reply(command)), + refused: (_, error) => Effect.die(error) +}) + +const normalize = (payload: unknown) => Option.getOrThrow(DiscordChat.normalizeInteraction(payload)) + +describe("recorded slash commands", () => { + it.effect("answers each command with the callback Discord took", () => + Effect.gen(function*() { + for (const what of ["private", "public", "mention", "modal", "defer", "deferPublic"]) { + const fake = makeFake({ createInteractionResponse: () => Effect.succeed(null) }) + const payload = recorded.commands[what] + yield* Effect.flatMap(DiscordChat.DiscordChat, (discord) => discord.interaction(payload)).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.provide(discordLayer(fake)) + ) + assert.deepStrictEqual( + fake.called("createInteractionResponse").map(([id, token, options]) => [id, token, options.payload]), + [[payload.id, "", recorded.callbacks[what].payload]], + what + ) + } + })) + + it.effect("follows up with the edit Discord took, and is interaction_expired once the token is spent", () => + Effect.gen(function*() { + const payload = recorded.commands.defer + const deferred = ChatInteraction.defer(normalize(payload) as ChatInteraction.CommandInvoked) + const followUp = (fake: Fake) => + Effect.flatMap( + Chat.Chat, + (chat) => chat.followUp(deferred, { markdown: "**Deferred answer** to defer, with buttons", actions }) + ).pipe(Effect.provide(discordLayer(fake))) + const issued = Number((BigInt(payload.id) >> 22n) + 1420070400000n) + yield* TestClock.setTime(issued + 4_000) + const edited = makeFake({ updateOriginalWebhookMessage: () => Effect.succeed(recorded.followUp.response.body) }) + yield* followUp(edited) + assert.deepStrictEqual( + edited.called("updateOriginalWebhookMessage").map(([applicationId, token, options]) => [ + applicationId, + token, + options.payload + ]), + [[botUserId, "", recorded.followUp.request]] + ) + + // What Discord answered at 15.5 minutes, 401 with 50027, returned here + // early; past 15 minutes the adapter refuses without a call. + const spent = makeFake({ + updateOriginalWebhookMessage: () => + Effect.fail(errorResponse(recorded.expired.edit.status, recorded.expired.edit.body)) + }) + const mapped = yield* Effect.flip(followUp(spent)) + yield* TestClock.setTime(issued + Duration.toMillis(Duration.minutes(15)) + 30_000) + const refused = yield* Effect.flip(followUp(spent)) + assert.deepStrictEqual([mapped, refused].map((error) => [error.op, error.reason._tag, error.reason.detail]), [ + ["followUp", "NotFound", "interaction_expired"], + ["followUp", "NotFound", "interaction_expired"] + ]) + assert.strictEqual(spent.called("updateOriginalWebhookMessage").length, 1) + })) +}) diff --git a/packages/chat/discord/test/conformance.test.ts b/packages/chat/discord/test/conformance.test.ts new file mode 100644 index 00000000000..1da8556bf2c --- /dev/null +++ b/packages/chat/discord/test/conformance.test.ts @@ -0,0 +1,44 @@ +/** + * The conformance rules for files, buttons, modals and commands, against the adapter + * over a stubbed `DiscordREST` that keeps what was posted. Clicks and + * submits are built from the posted components and go through + * `DiscordChat.interaction`, so the callback answers are tested too. + */ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { describe, it } from "@effect/vitest" +import { conformance } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { makeDiscord } from "./discord.ts" +import { discordLayer, scratch } from "./utils.ts" + +const portable = (name: string) => + name === "post/refusedFile" || + name.startsWith("actions/") || + name.startsWith("modal/") || + name.startsWith("edit/") && name.endsWith("Actions") || + name.startsWith("descriptor/measure") && name.endsWith("Actions") || + name.startsWith("command/") || + name === "unsupported/buttons" || + name === "unsupported/selects" || + name === "unsupported/modals" || + name === "unsupported/commands" || + name === "unsupported/ephemeral" + +const rules = (discord: ReturnType) => + conformance({ + platform: "discord", + thread: DiscordChat.thread(scratch), + userPosts: () => Effect.die("unused"), + parseMention: DiscordChat.parseMention, + refuseFiles: discord.refuseFiles, + interactions: discord.interactions + }) + +describe("conformance over the stubbed DiscordREST", () => { + for (const name of Object.keys(rules(makeDiscord())).filter(portable)) { + it.effect(name, () => { + const discord = makeDiscord() + return Effect.andThen(discord.bind, rules(discord)[name]).pipe(Effect.provide(discordLayer(discord.fake))) + }) + } +}) diff --git a/packages/chat/discord/test/discord.ts b/packages/chat/discord/test/discord.ts new file mode 100644 index 00000000000..fd5cdb4deb6 --- /dev/null +++ b/packages/chat/discord/test/discord.ts @@ -0,0 +1,190 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { type Interactions, NoSuchElement } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { answer, botUserId, errorResponse, type Fake, guild, makeFake, recordedCommands, step, tim } from "./utils.ts" + +/** The join between an id and its value in a `custom_id`, as the adapter writes it. */ +const join = "\u241F" + +const idOf = (customId: string): string => customId.split(join)[0] + +/** + * A Discord that keeps the messages the adapter posts, with their + * components, and records every interaction callback. `interactions` is the conformance hook: it builds + * Discord's `INTERACTION_CREATE` payload from what was posted, or a slash + * command's, and sends it through `DiscordChat.interaction`. A command + * starts from the one recorded in `fixtures/recorded/commands.json`, and so + * does the submit of the modal it opened, which has no message. + */ +export const makeDiscord = (channelType = 0) => { + const messages = new Map() + const callbacks: Array<{ readonly id: string; readonly token: string; readonly payload: any }> = [] + let next = 1570700000000000000n + let refuse = 0 + let interactionCount = 0 + const nextId = () => String(next++) + + const unknownMessage = () => Effect.fail(errorResponse(404, { code: 10008, message: "Unknown Message" })) + + const fake: Fake = makeFake({ + getChannel: (id: string) => Effect.succeed({ id, type: channelType, guild_id: guild, name: "scratch" }), + createMessage: (channelId: string, body: any) => { + if ((body.attachments ?? []).length > 0 && refuse > 0) { + refuse-- + // Discord's answer to an oversize upload, from fixtures/recorded/uploads.json. + return answer(step("uploads", "upload-20MiB-plus-1")) + } + const id = nextId() + const message = { + id, + channel_id: channelId, + type: 0, + content: body.content, + timestamp: new Date(Number((BigInt(id) >> 22n) + 1420070400000n)).toISOString(), + author: { id: botUserId, username: "Janitor", bot: true }, + attachments: [], + components: body.components ?? [] + } + messages.set(id, message) + return Effect.succeed(message) + }, + updateMessage: (_channelId: string, id: string, body: any) => { + const message = messages.get(id) + if (message === undefined) return unknownMessage() + message.content = body.content + if (body.components !== undefined) message.components = body.components + return Effect.succeed(message) + }, + getMessage: (_channelId: string, id: string) => { + const message = messages.get(id) + return message === undefined ? unknownMessage() : Effect.succeed(message) + }, + deleteMessage: (_channelId: string, id: string) => + messages.delete(id) ? Effect.succeed(undefined) : unknownMessage(), + listMessages: (channelId: string, params: { after?: string; before?: string; limit: number }) => + Effect.succeed( + [...messages.values()] + .filter((message) => + message.channel_id === channelId && + (params.after === undefined || BigInt(message.id) > BigInt(params.after)) && + (params.before === undefined || BigInt(message.id) < BigInt(params.before)) + ) + .sort((a, b) => (BigInt(b.id) > BigInt(a.id) ? 1 : -1)) + .slice(0, params.limit) + ), + createInteractionResponse: (id: string, token: string, options: { payload: any }) => { + callbacks.push({ id, token, payload: options.payload }) + return Effect.succeed(null) + }, + updateOriginalWebhookMessage: (_applicationId: string, token: string, options: { payload: any }) => { + // Only a token a type 5 callback answered has an original response to edit. + if (!callbacks.some((callback) => callback.token === token && callback.payload.type === 5)) { + return Effect.fail(errorResponse(404, { code: 10015, message: "Unknown Webhook" })) + } + return Effect.succeed({ id: nextId(), content: options.payload.content }) + } + }) + + /** + * The modal the last click or command opened, with the message it was + * opened from, which a command has none of. + */ + let opened: { readonly data: any; readonly channelId: string; readonly message?: any } | undefined + + /** The service the hook delivers through, set by `bind`. */ + let service: DiscordChat.DiscordChat["Service"] | undefined + + const deliver = Effect.fnUntraced(function*(payload: Record) { + const before = callbacks.length + yield* service!.interaction(payload) + return callbacks.slice(before).find((callback) => callback.id === payload.id)?.payload + }) + + const base = (channelId: string, message?: any) => { + const n = ++interactionCount + return { + id: "9" + String(n).padStart(18, "0"), + application_id: botUserId, + token: "token-" + n, + version: 1, + channel_id: channelId, + guild_id: guild, + member: { user: { id: tim, username: "tim" }, roles: [] }, + ...(message === undefined ? {} : { message }) + } + } + + /** The conformance hook. It delivers through the service `bind` saw last. */ + const interactions: Interactions = { + click: Effect.fnUntraced(function*(messageId, id, options) { + const message = messages.get(messageId) + const component = (message?.components ?? []) + .flatMap((row: any) => row.components) + .find((component: any) => typeof component.custom_id === "string" && idOf(component.custom_id) === id) + if (component === undefined) return yield* Effect.fail(new NoSuchElement({ messageId, id })) + const value = component.type === 3 ? options?.option ?? component.options[0].value : undefined + const answer = yield* deliver({ + ...base(message.channel_id, message), + type: 3, + data: { + custom_id: component.custom_id, + component_type: component.type, + ...(value === undefined ? {} : { values: [value] }) + } + }) + opened = answer?.type === 9 ? { data: answer.data, channelId: message.channel_id, message } : undefined + }), + command: Effect.fnUntraced(function*(thread, id, options) { + const text = options?.text ?? "" + const answer = yield* deliver({ + ...recordedCommands.commands.private, + channel: { ...recordedCommands.commands.private.channel, id: thread.id, type: channelType }, + ...base(thread.id), + type: 2, + data: { + id: "1570700000000000999", + name: id, + type: 1, + ...(text === "" ? {} : { options: [{ type: 3, name: "text", value: text }] }) + } + }) + opened = answer?.type === 9 ? { data: answer.data, channelId: thread.id } : undefined + }), + submit: Effect.fnUntraced(function*(values) { + if (opened === undefined) return + const { channelId, data, message } = opened + const components = data.components.map((component: any) => { + if (component.type !== 18) return { type: component.type } + const input = component.component + const value = values[input.custom_id] + return { + type: 18, + component: input.type === 4 + ? { type: 4, custom_id: input.custom_id, value: value ?? "" } + : { type: 3, custom_id: input.custom_id, values: value === undefined ? [] : [value] } + } + }) + yield* deliver({ + ...(message === undefined ? recordedCommands.commandModal.submitClose : {}), + ...base(channelId, message), + type: 5, + data: { custom_id: data.custom_id, components } + }) + // Discord closes the modal on every answer, `errors` included. + opened = undefined + }) + } + + return { + fake, + bind: Effect.tap(DiscordChat.DiscordChat, (discord) => + Effect.sync(() => { + service = discord + })), + interactions, + refuseFiles: () => + Effect.sync(() => { + refuse++ + }) + } +} diff --git a/packages/chat/discord/test/fixtures/events/attachment.json b/packages/chat/discord/test/fixtures/events/attachment.json new file mode 100644 index 00000000000..f073ce93988 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/attachment.json @@ -0,0 +1,79 @@ +{ + "case": "A message with a text attachment, uploaded as multipart; the attachments carry signed CDN URLs, scrubbed", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot, through REST", + "recordedAt": "2026-11-13T01:30Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:30:02.519Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:30:02.447000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570611669015732315", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a message with an attachment, fixed", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [ + { + "url": "https://cdn.discordapp.com/attachments/1570607253965246584/1570611668722122812/fixture.txt?scrubbed", + "size": 23, + "proxy_url": "https://media.discordapp.net/attachments/1570607253965246584/1570611668722122812/fixture.txt?scrubbed", + "id": "1570611668722122812", + "filename": "fixture.txt", + "content_type": "text/plain; charset=utf-8", + "content_scan_version": 0 + } + ], + "guild_id": "840178841540362250" + }, + "scrubbed": [ + "$.attachments[0].url", + "$.attachments[0].proxy_url" + ] + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/channel-message-without-channel-type.json b/packages/chat/discord/test/fixtures/events/channel-message-without-channel-type.json new file mode 100644 index 00000000000..71db98dffa7 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/channel-message-without-channel-type.json @@ -0,0 +1,61 @@ +{ + "case": "The channel-message dispatch with channel_type removed. Discord documents the field as optional, but no recorded delivery lacked it", + "provenance": { + "kind": "derived", + "from": "events/channel-message.json", + "change": "deleted d.channel_type" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:13.140Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:13+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608190062723103", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a plain message in a guild text channel", + "components": [], + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/channel-message.json b/packages/chat/discord/test/fixtures/events/channel-message.json new file mode 100644 index 00000000000..541e43f3685 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/channel-message.json @@ -0,0 +1,65 @@ +{ + "case": "A message by the bot in a guild text channel, delivered with channel_type 0", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot, through REST", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:13.140Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:13+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608190062723103", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a plain message in a guild text channel", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/forum.json b/packages/chat/discord/test/fixtures/events/forum.json new file mode 100644 index 00000000000..fce682efbb7 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/forum.json @@ -0,0 +1,119 @@ +{ + "case": "A forum post: its starter, whose id is the thread's, and a later reply; channel_type 11", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot, through REST", + "recordedAt": "2026-11-13T01:17Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:17:14.964Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:17:14.921000+00:00", + "position": 0, + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608449778098218", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: the starter of a forum post", + "components": [], + "channel_type": 11, + "channel_id": "1570608449778098218", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:16.322Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:17:16.290000+00:00", + "position": 0, + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608455520227379", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a later reply in a forum post", + "components": [], + "channel_type": 11, + "channel_id": "1570608449778098218", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/mentions.json b/packages/chat/discord/test/fixtures/events/mentions.json new file mode 100644 index 00000000000..b16b11dd8b4 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/mentions.json @@ -0,0 +1,491 @@ +{ + "case": "Mention tokens sent through the webhook with allowed_mentions resolving them (or not), and the bot naming itself and a role with parse []: a token counts only outside code and when mentions, mention_roles or mention_everyone says it resolved", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "webhook effect-chat-fixtures and the Janitor bot", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:22.047Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:22.010000+00:00", + "pinned": false, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608227853533387", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <@1564742987727118426> as a plain user token", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:23.497Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:23.426000+00:00", + "pinned": false, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608233792405568", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <@!1564742987727118426> as a nickname token", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:25.076Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:24.944000+00:00", + "pinned": false, + "mentions": [ + { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + } + ], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608240159625278", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <@449108784879763458> another user", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:26.781Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:26.546000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [ + "1570607256838479964" + ], + "mention_everyone": false, + "id": "1570608246878773301", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <@&1570607256838479964> a role", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:28.532Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:28.416000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": true, + "id": "1570608254721982479", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: @everyone that resolves", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:29.956Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:29.899000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": true, + "id": "1570608260942397441", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: @here that resolves", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:31.721Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:31.658000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608268320051301", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: @everyone with parse empty", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:33.208Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:33.157000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608274607440034", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <#1570605581192929301> a channel", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:35.385Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:35.311000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608283641970709", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: `<@1564742987727118426>` in a code span", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:36.882Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:36.848000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608290088484875", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: in a fence\n```\n<@1564742987727118426>\n```", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:38.528Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:38.346000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608296371425382", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: the bot naming <@1564742987727118426> and <@&1570607256838479964> with parse empty", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:40.029Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:39.899000+00:00", + "pinned": false, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [ + "1570607256838479964" + ], + "mention_everyone": false, + "id": "1570608302885179546", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: <@1564742987727118426> and `<@449108784879763458>` and <@&1570607256838479964> and <#1570607253965246584>", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/reactions.json b/packages/chat/discord/test/fixtures/events/reactions.json new file mode 100644 index 00000000000..dc292d12f9a --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/reactions.json @@ -0,0 +1,385 @@ +{ + "case": "MESSAGE_REACTION_ADD and MESSAGE_REACTION_REMOVE in a channel and in a thread, for a Unicode emoji, a skin-toned one and the guild's custom :effectchat:; removals carry no member and no message_author_id", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot, through REST", + "recordedAt": "2026-11-13T01:17Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:17:26.307Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "👍", + "id": null + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:27.600Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "👍🏽", + "id": null + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:28.980Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "effectchat", + "id": "1570607258226790481" + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:31.820Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "emoji": { + "name": "👍", + "id": null + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:33.147Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "emoji": { + "name": "👍🏽", + "id": null + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:34.434Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608483965870101", + "emoji": { + "name": "effectchat", + "id": "1570607258226790481" + }, + "channel_id": "1570607253965246584", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:35.742Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "👍", + "id": null + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:37.218Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "👍🏽", + "id": null + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:38.573Z", + "t": "MESSAGE_REACTION_ADD", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "message_author_id": "1564742987727118426", + "member": { + "user": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "display_name": null, + "discriminator": "5426", + "collectibles": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "emoji": { + "name": "effectchat", + "id": "1570607258226790481" + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:41.260Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "emoji": { + "name": "👍", + "id": null + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:42.704Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "emoji": { + "name": "👍🏽", + "id": null + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:44.046Z", + "t": "MESSAGE_REACTION_REMOVE", + "d": { + "user_id": "1564742987727118426", + "type": 0, + "message_id": "1570608491532386377", + "emoji": { + "name": "effectchat", + "id": "1570607258226790481" + }, + "channel_id": "1570608327602471024", + "burst": false, + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/reply-ping.json b/packages/chat/discord/test/fixtures/events/reply-ping.json new file mode 100644 index 00000000000..b217a6b7933 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/reply-ping.json @@ -0,0 +1,429 @@ +{ + "case": "Replies to the anchor message in #test typed by a person, ping on (Discord puts the replied-to author in mentions with no token in content) and ping off, then the bot's own two replies to a message with replied_user true and false", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "Tim Smart (449108784879763458), typing in the Discord client, then the Janitor bot through REST", + "recordedAt": "2026-11-13T01:17Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:17:05.947Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 19, + "tts": false, + "timestamp": "2026-11-13T01:17:05.904000+00:00", + "referenced_message": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:12:44.305000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570607314732580917", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture anchor for @effect/chat-discord (EFF-3695). Please reply to this message twice: once with the ping on, once with it off.", + "components": [], + "channel_type": 0, + "channel_id": "1570605581192929301", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [] + }, + "pinned": false, + "nonce": "1570608410896891904", + "message_reference": { + "type": 0, + "message_id": "1570607314732580917", + "guild_id": "840178841540362250", + "channel_id": "1570605581192929301" + }, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608411958182022", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Hello", + "components": [], + "channel_type": 0, + "channel_id": "1570605581192929301", + "author": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:12.937Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 19, + "tts": false, + "timestamp": "2026-11-13T01:17:12.887000+00:00", + "referenced_message": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:12:44.305000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570607314732580917", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture anchor for @effect/chat-discord (EFF-3695). Please reply to this message twice: once with the ping on, once with it off.", + "components": [], + "channel_type": 0, + "channel_id": "1570605581192929301", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [] + }, + "pinned": false, + "nonce": "1570608439845978112", + "message_reference": { + "type": 0, + "message_id": "1570607314732580917", + "guild_id": "840178841540362250", + "channel_id": "1570605581192929301" + }, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608441246883942", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Hello ping off", + "components": [], + "channel_type": 0, + "channel_id": "1570605581192929301", + "author": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:19.117Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 19, + "tts": false, + "timestamp": "2026-11-13T01:16:19.019000+00:00", + "referenced_message": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:13+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608190062723103", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a plain message in a guild text channel", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [] + }, + "pinned": false, + "message_reference": { + "type": 0, + "message_id": "1570608190062723103", + "guild_id": "840178841540362250", + "channel_id": "1570607253965246584" + }, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608215308238930", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a reply with the ping on", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:20.669Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 19, + "tts": false, + "timestamp": "2026-11-13T01:16:20.534000+00:00", + "referenced_message": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:13+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608190062723103", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a plain message in a guild text channel", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [] + }, + "pinned": false, + "message_reference": { + "type": 0, + "message_id": "1570608190062723103", + "guild_id": "840178841540362250", + "channel_id": "1570607253965246584" + }, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608221662740501", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a reply with the ping off", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/thread-messages.json b/packages/chat/discord/test/fixtures/events/thread-messages.json new file mode 100644 index 00000000000..4b8fe985107 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/thread-messages.json @@ -0,0 +1,221 @@ +{ + "case": "Replies in a thread started from a message (by the bot and by the webhook, mentioning the bot), in a thread created without a message, and in a thread whose opener was then deleted; channel_type 11", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot and webhook effect-chat-fixtures", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:50.146Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:50.045000+00:00", + "position": 0, + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608345440722956", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a reply in a thread started from a message", + "components": [], + "channel_type": 11, + "channel_id": "1570608327602471024", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:16:52.016Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:51.911000+00:00", + "position": 1, + "pinned": false, + "mentions": [ + { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + } + ], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608353267159146", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a webhook reply in a thread, <@1564742987727118426>", + "components": [], + "channel_type": 11, + "channel_id": "1570608327602471024", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:02.079Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:17:02.032000+00:00", + "position": 0, + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608395717972000", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: the first message in a thread created without a message", + "components": [], + "channel_type": 11, + "channel_id": "1570608387434094622", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:09.492Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:17:09.258000+00:00", + "position": 0, + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608426025877659", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a reply before the opener is deleted", + "components": [], + "channel_type": 11, + "channel_id": "1570608413061283862", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/thread-started.json b/packages/chat/discord/test/fixtures/events/thread-started.json new file mode 100644 index 00000000000..86e20967364 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/thread-started.json @@ -0,0 +1,188 @@ +{ + "case": "What Discord delivers when a thread is started from a message (the THREAD_STARTER_MESSAGE, type 21, in the thread) and when one is created without a message (THREAD_CREATED, type 18, in the parent); both give no event", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "the Janitor bot, through REST", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:47.400Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 21, + "tts": false, + "timestamp": "2026-11-13T01:16:47.289000+00:00", + "position": 0, + "pinned": false, + "message_reference": { + "type": 0, + "message_id": "1570608327602471024", + "guild_id": "840178841540362250", + "channel_id": "1570607253965246584" + }, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608333881081877", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "", + "components": [], + "channel_type": 11, + "channel_id": "1570608327602471024", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:00.121Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 18, + "tts": false, + "timestamp": "2026-11-13T01:17:00.057000+00:00", + "pinned": false, + "message_reference": { + "type": 0, + "guild_id": "840178841540362250", + "channel_id": "1570608387434094622" + }, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608387434094622", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture thread without a message", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + }, + { + "receivedAt": "2026-11-13T01:17:07.792Z", + "t": "MESSAGE_CREATE", + "d": { + "type": 21, + "tts": false, + "timestamp": "2026-11-13T01:17:07.747000+00:00", + "position": 0, + "pinned": false, + "message_reference": { + "type": 0, + "message_id": "1570608413061283862", + "guild_id": "840178841540362250", + "channel_id": "1570607253965246584" + }, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "member": { + "roles": [ + "1570605977017778196" + ], + "premium_since": null, + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2026-11-13T01:07:25.410129+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "id": "1570608419688419446", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "", + "components": [], + "channel_type": 11, + "channel_id": "1570608413061283862", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/events/webhook-message.json b/packages/chat/discord/test/fixtures/events/webhook-message.json new file mode 100644 index 00000000000..2c2dffee2c5 --- /dev/null +++ b/packages/chat/discord/test/fixtures/events/webhook-message.json @@ -0,0 +1,46 @@ +{ + "case": "A message from a webhook: author.id is the webhook id, webhook_id set, no member", + "provenance": { + "kind": "recorded", + "source": "Discord gateway v10 dispatches to the Janitor application (1564742987727118426), logged by a local dfx-free websocket client with the GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS and MESSAGE_CONTENT intents", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "author": "webhook effect-chat-fixtures (1570607260156297307)", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each d verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "dispatches": [ + { + "receivedAt": "2026-11-13T01:16:14.492Z", + "t": "MESSAGE_CREATE", + "d": { + "webhook_id": "1570607260156297307", + "type": 0, + "tts": false, + "timestamp": "2026-11-13T01:16:14.460000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "id": "1570608196186411139", + "flags": 0, + "embeds": [], + "edited_timestamp": null, + "content": "Fixture: a webhook message", + "components": [], + "channel_type": 0, + "channel_id": "1570607253965246584", + "author": { + "username": "effect-chat-fixtures", + "id": "1570607260156297307", + "global_name": null, + "discriminator": "0000", + "bot": true, + "avatar": null + }, + "attachments": [], + "application_id": "1564742987727118426", + "guild_id": "840178841540362250" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/channels.json b/packages/chat/discord/test/fixtures/recorded/channels.json new file mode 100644 index 00000000000..c89b50d78b5 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/channels.json @@ -0,0 +1,181 @@ +{ + "case": "Channel reads: a text channel, a forum, a direct opened with Tim, and a thread", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "recordedAt": "2026-11-13T01:17Z, during EFF-3695", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "calls": [ + { + "step": "get-channel-text", + "method": "GET", + "path": "/channels/1570607253965246584", + "request": null, + "status": 200, + "response": { + "id": "1570607253965246584", + "type": 0, + "last_message_id": "1570608483965870101", + "flags": 0, + "guild_id": "840178841540362250", + "name": "effect-chat-scratch", + "parent_id": null, + "rate_limit_per_user": 0, + "topic": "Scratch channel for @effect/chat-discord fixtures (EFF-3695). Safe to delete.", + "position": 7, + "permission_overwrites": [ + { + "id": "840178841540362250", + "type": 0, + "allow": "0", + "deny": "1024" + } + ], + "nsfw": false + } + }, + { + "step": "get-channel-forum", + "method": "GET", + "path": "/channels/1570607255684915302", + "request": null, + "status": 200, + "response": { + "id": "1570607255684915302", + "type": 15, + "last_message_id": "1570608449778098218", + "flags": 0, + "guild_id": "840178841540362250", + "name": "effect-chat-forum", + "parent_id": null, + "rate_limit_per_user": 0, + "topic": "Scratch channel for @effect/chat-discord fixtures (EFF-3695). Safe to delete.", + "position": 0, + "permission_overwrites": [ + { + "id": "840178841540362250", + "type": 0, + "allow": "0", + "deny": "1024" + } + ], + "nsfw": false, + "available_tags": [], + "default_reaction_emoji": null, + "default_sort_order": null, + "default_forum_layout": 0, + "default_tag_setting": "match_some", + "icon_emoji": null, + "theme_color": null, + "template": "" + } + }, + { + "step": "dm-open", + "method": "POST", + "path": "/users/@me/channels", + "request": { + "recipient_id": "449108784879763458" + }, + "status": 200, + "response": { + "id": "1570608637783572490", + "type": 1, + "last_message_id": null, + "flags": 0, + "recipients": [ + { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ] + } + }, + { + "step": "get-channel-dm", + "method": "GET", + "path": "/channels/1570608637783572490", + "request": null, + "status": 200, + "response": { + "id": "1570608637783572490", + "type": 1, + "last_message_id": null, + "flags": 0, + "recipients": [ + { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ] + } + }, + { + "step": "thread-get-channel", + "method": "GET", + "path": "/channels/1570608327602471024", + "request": null, + "status": 200, + "response": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608353267159146", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280000+00:00" + }, + "message_count": 2, + "member_count": 1, + "total_message_sent": 2, + "member": { + "id": "1570608327602471024", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:16:47.281000+00:00", + "flags": 1, + "muted": false, + "mute_config": null + } + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/commands.json b/packages/chat/discord/test/fixtures/recorded/commands.json new file mode 100644 index 00000000000..c2da91b0129 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/commands.json @@ -0,0 +1,1127 @@ +{ + "case": "Slash commands on Discord, as the gateway delivered them and as the adapter answered, with the clicks and submits that followed, the follow-up edit and what Discord answers after 15 minutes", + "provenance": { + "kind": "recorded", + "source": "The adapter at 53b4d54024 behind a logging proxy (eff-3802-capture.ts), with the Janitor bot (1564742987727118426) on the gateway and Tim running /chat-capture", + "where": "guild 840178841540362250 (Tim's test guild); #test 1570605581192929301. /chat-capture was registered on that guild only and deleted afterwards", + "recordedAt": "2026-11-14T19:54Z to 20:18Z, during EFF-3802", + "body": "Payloads and callback bodies verbatim, apart from the interaction token, which is replaced with . Network headers are left out", + "seenButNotInLog": [ + "Callback type 6 closed the modal a command opened", + "The mention answer rendered <@449108784879763458> as a mention and did not notify", + "Seven followUps on one deferral left one message, edited to the seventh", + "A late command showed 'The application did not respond'" + ] + }, + "commands": { + "private": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571251955899310120", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "modal": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252100829155488", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "options": [ + { + "value": "modal", + "type": 3, + "name": "what" + } + ], + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "mention": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252264956469369", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "options": [ + { + "value": "mention", + "type": 3, + "name": "what" + } + ], + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "public": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252314214236321", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "options": [ + { + "value": "public", + "type": 3, + "name": "what" + } + ], + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "defer": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252354362249429", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "options": [ + { + "value": "defer", + "type": 3, + "name": "what" + } + ], + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1571252315409612851", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "deferPublic": { + "version": 1, + "type": 2, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571254079961964664", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "type": 1, + "options": [ + { + "value": "defer-public", + "type": 3, + "name": "what" + } + ], + "name": "chat-capture", + "id": "1571251308462084127", + "guild_id": "840178841540362250" + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1571252315409612851", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + } + }, + "callbacks": { + "private": { + "status": 204, + "payload": { + "type": 4, + "data": { + "content": "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons", + "allowed_mentions": { + "parse": [], + "users": [], + "roles": [], + "replied_user": false + }, + "flags": 68, + "components": [ + { + "type": 1, + "components": [ + { + "type": 2, + "style": 2, + "label": "Ack", + "custom_id": "cap-ack" + }, + { + "type": 2, + "style": 1, + "label": "Open modal", + "custom_id": "cap-open" + } + ] + } + ] + } + } + }, + "modal": { + "status": 204, + "payload": { + "type": 9, + "data": { + "custom_id": "cap-modal", + "title": "Capture", + "components": [ + { + "type": 18, + "label": "Note (type err for a field error)", + "component": { + "type": 4, + "custom_id": "note", + "style": 1, + "required": true + } + } + ] + } + } + }, + "mention": { + "status": 204, + "payload": { + "type": 4, + "data": { + "content": "Private answer mentioning <@449108784879763458>", + "allowed_mentions": { + "parse": [], + "users": [], + "roles": [], + "replied_user": false + }, + "flags": 68 + } + } + }, + "public": { + "status": 204, + "payload": { + "type": 4, + "data": { + "content": "**Public answer** with _Markdown_ and buttons", + "allowed_mentions": { + "parse": [], + "users": [], + "roles": [], + "replied_user": false + }, + "flags": 4, + "components": [ + { + "type": 1, + "components": [ + { + "type": 2, + "style": 2, + "label": "Ack", + "custom_id": "cap-ack" + }, + { + "type": 2, + "style": 1, + "label": "Open modal", + "custom_id": "cap-open" + } + ] + } + ] + } + } + }, + "defer": { + "status": 204, + "payload": { + "type": 5, + "data": { + "flags": 64 + } + } + }, + "deferPublic": { + "status": 204, + "payload": { + "type": 5 + } + } + }, + "clicks": { + "privateAnswer": { + "version": 1, + "type": 3, + "token": "", + "message": { + "webhook_id": "1564742987727118426", + "type": 20, + "tts": false, + "timestamp": "2026-11-14T19:54:18.950000+00:00", + "pinned": false, + "mentions": [], + "mention_roles": [], + "mention_everyone": false, + "interaction_metadata": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "type": 2, + "name": "chat-capture", + "id": "1571251955899310120", + "command_type": 1, + "authorizing_integration_owners": { + "0": "840178841540362250" + } + }, + "interaction": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "type": 2, + "name": "chat-capture", + "id": "1571251955899310120" + }, + "id": "1571251956796752013", + "flags": 68, + "embeds": [], + "edited_timestamp": null, + "content": "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons", + "components": [ + { + "type": 1, + "id": 1, + "components": [ + { + "type": 2, + "style": 2, + "label": "Ack", + "id": 2, + "custom_id": "cap-ack" + }, + { + "type": 2, + "style": 1, + "label": "Open modal", + "id": 3, + "custom_id": "cap-open" + } + ] + } + ], + "channel_id": "1570605581192929301", + "author": { + "username": "Janitor", + "public_flags": 0, + "primary_guild": null, + "id": "1564742987727118426", + "global_name": null, + "display_name_styles": null, + "discriminator": "5426", + "collectibles": null, + "clan": null, + "bot": true, + "avatar_decoration_data": null, + "avatar": "07aaab1f8e37dd6b088b6f21d1332701" + }, + "attachments": [], + "application_id": "1564742987727118426" + }, + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571251977059573781", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "id": 2, + "custom_id": "cap-ack", + "component_type": 2 + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "privateAnswerCallback": { + "status": 204, + "payload": { + "type": 6 + } + } + }, + "commandModal": { + "submitErrors": { + "version": 1, + "type": 5, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252118004703322", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "custom_id": "cap-modal", + "components": [ + { + "type": 18, + "id": 1, + "component": { + "value": "err", + "type": 4, + "id": 2, + "custom_id": "note" + } + } + ] + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "errorsCallback": { + "status": 204, + "payload": { + "type": 4, + "data": { + "content": "- That is the error you asked for", + "allowed_mentions": { + "parse": [] + }, + "flags": 64 + } + } + }, + "submitClose": { + "version": 1, + "type": 5, + "token": "", + "member": { + "user": { + "username": "timsmart", + "public_flags": 0, + "primary_guild": null, + "id": "449108784879763458", + "global_name": "Tim Smart", + "display_name_styles": null, + "discriminator": "0", + "collectibles": null, + "clan": null, + "avatar_decoration_data": null, + "avatar": "2eded1558c297f57913f8b7e4c6b7efc" + }, + "unusual_dm_activity_until": null, + "roles": [], + "premium_since": null, + "permissions": "18014398509481983", + "pending": false, + "nick": null, + "mute": false, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "flags": 0, + "deaf": false, + "communication_disabled_until": null, + "banner": null, + "avatar": null + }, + "locale": "en-GB", + "id": "1571252196744634570", + "guild_locale": "en-US", + "guild_id": "840178841540362250", + "guild": { + "locale": "en-US", + "id": "840178841540362250", + "features": [] + }, + "entitlements": [], + "entitlement_sku_ids": [], + "data": { + "custom_id": "cap-modal", + "components": [ + { + "type": 18, + "id": 1, + "component": { + "value": "hello", + "type": 4, + "id": 2, + "custom_id": "note" + } + } + ] + }, + "context": 0, + "channel_id": "1570605581192929301", + "channel": { + "type": 0, + "topic": null, + "rate_limit_per_user": 0, + "position": 6, + "permissions": "18014398509481983", + "parent_id": "840178841540362251", + "nsfw": false, + "name": "test", + "last_message_id": "1570720379356581938", + "id": "1570605581192929301", + "guild_id": "840178841540362250", + "flags": 0 + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "attachment_size_limit": 20971520, + "application_id": "1564742987727118426", + "app_permissions": "18014398509481983" + }, + "closeCallback": { + "status": 204, + "payload": { + "type": 6 + } + } + }, + "followUp": { + "request": { + "content": "**Deferred answer** to defer, with buttons", + "allowed_mentions": { + "parse": [], + "users": [], + "roles": [], + "replied_user": false + }, + "flags": 68, + "components": [ + { + "type": 1, + "components": [ + { + "type": 2, + "style": 2, + "label": "Ack", + "custom_id": "cap-ack" + }, + { + "type": 2, + "style": 1, + "label": "Open modal", + "custom_id": "cap-open" + } + ] + } + ] + }, + "response": { + "status": 200, + "body": { + "type": 20, + "content": "**Deferred answer** to defer, with buttons", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-14T19:55:53.917000+00:00", + "edited_timestamp": null, + "flags": 68, + "components": [ + { + "type": 1, + "id": 1, + "components": [ + { + "type": 2, + "id": 2, + "custom_id": "cap-ack", + "style": 2, + "label": "Ack" + }, + { + "type": 2, + "id": 3, + "custom_id": "cap-open", + "style": 1, + "label": "Open modal" + } + ] + } + ], + "id": "1571252355117088918", + "channel_id": "1570605581192929301", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "interaction": { + "id": "1571252354362249429", + "type": 2, + "name": "chat-capture", + "user": { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + }, + "webhook_id": "1564742987727118426", + "interaction_metadata": { + "id": "1571252354362249429", + "type": 2, + "user": { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "authorizing_integration_owners": { + "0": "840178841540362250" + }, + "name": "chat-capture", + "command_type": 1 + } + } + } + }, + "expired": { + "note": "15.5 minutes after the command, sent by the capture script itself with the interaction's token, before the adapter's followUp, which refused without a request. The same answer came three times, for an edit of @original and for a new follow-up", + "edit": { + "method": "PATCH", + "path": "/webhooks/1564742987727118426//messages/@original", + "status": 401, + "body": { + "message": "Invalid Webhook Token", + "code": 50027 + } + }, + "followUp": { + "method": "POST", + "path": "/webhooks/1564742987727118426/", + "status": 401, + "body": { + "message": "Invalid Webhook Token", + "code": 50027 + } + } + } +} diff --git a/packages/chat/discord/test/fixtures/recorded/errors.json b/packages/chat/discord/test/fixtures/recorded/errors.json new file mode 100644 index 00000000000..66c75e03209 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/errors.json @@ -0,0 +1,195 @@ +{ + "case": "Error bodies: 160004 from a second startThread, 50024 from startThread in a forum post, unknown channel, message, user and member, an empty message, and content past the limit", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "calls": [ + { + "step": "thread-start-again", + "method": "POST", + "path": "/channels/1570607253965246584/messages/1570608327602471024/threads", + "request": { + "name": "Fixture thread again" + }, + "status": 400, + "response": { + "message": "A thread has already been created for this message", + "code": 160004 + } + }, + { + "step": "forum-start-thread-again", + "method": "POST", + "path": "/channels/1570608449778098218/messages/1570608449778098218/threads", + "request": { + "name": "again" + }, + "status": 400, + "response": { + "message": "Cannot execute action on this channel type", + "code": 50024 + } + }, + { + "step": "get-channel-unknown", + "method": "GET", + "path": "/channels/1", + "request": null, + "status": 404, + "response": { + "message": "Unknown Channel", + "code": 10003 + } + }, + { + "step": "get-user-unknown", + "method": "GET", + "path": "/users/1", + "request": null, + "status": 404, + "response": { + "message": "Unknown User", + "code": 10013 + } + }, + { + "step": "get-user-webhook", + "method": "GET", + "path": "/users/1570607260156297307", + "request": null, + "status": 404, + "response": { + "message": "Unknown User", + "code": 10013 + } + }, + { + "step": "get-member-unknown", + "method": "GET", + "path": "/guilds/840178841540362250/members/1", + "request": null, + "status": 404, + "response": { + "message": "Unknown User", + "code": 10013 + } + }, + { + "step": "get-message-unknown", + "method": "GET", + "path": "/channels/1570607253965246584/messages/1", + "request": null, + "status": 404, + "response": { + "message": "Unknown Message", + "code": 10008 + } + }, + { + "step": "edit-unknown", + "method": "PATCH", + "path": "/channels/1570607253965246584/messages/1", + "request": { + "content": "x" + }, + "status": 404, + "response": { + "message": "Unknown Message", + "code": 10008 + } + }, + { + "step": "delete-unknown", + "method": "DELETE", + "path": "/channels/1570607253965246584/messages/1", + "request": null, + "status": 404, + "response": { + "message": "Unknown Message", + "code": 10008 + } + }, + { + "step": "post-unknown-channel", + "method": "POST", + "path": "/channels/1/messages", + "request": { + "content": "x" + }, + "status": 404, + "response": { + "message": "Unknown Channel", + "code": 10003 + } + }, + { + "step": "post-empty", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "" + }, + "status": 400, + "response": { + "message": "Cannot send an empty message", + "code": 50006 + } + }, + { + "step": "length-2001-ascii", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "allowed_mentions": { + "parse": [] + } + }, + "status": 400, + "response": { + "message": "Invalid Form Body", + "code": 50035, + "errors": { + "content": { + "_errors": [ + { + "code": "BASE_TYPE_MAX_LENGTH", + "message": "Must be 2000 or fewer in length." + } + ] + } + } + } + }, + { + "step": "length-1999-plus-flag", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa🇬🇧", + "allowed_mentions": { + "parse": [] + } + }, + "status": 400, + "response": { + "message": "Invalid Form Body", + "code": 50035, + "errors": { + "content": { + "_errors": [ + { + "code": "BASE_TYPE_MAX_LENGTH", + "message": "Must be 2000 or fewer in length." + } + ] + } + } + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/history.json b/packages/chat/discord/test/fixtures/recorded/history.json new file mode 100644 index 00000000000..b0e62fc0d33 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/history.json @@ -0,0 +1,1095 @@ +{ + "case": "Thread history: a thread started from a message (THREAD_STARTER_MESSAGE with referenced_message), the opener read in the parent and through the thread id, a thread created without a message, a thread whose opener was deleted (referenced_message null), and a forum post", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "calls": [ + { + "step": "thread-opener", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: a message a thread is started from", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: a message a thread is started from", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:45.792000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608327602471024", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + }, + { + "step": "thread-start", + "method": "POST", + "path": "/channels/1570607253965246584/messages/1570608327602471024/threads", + "request": { + "name": "Fixture thread from a message", + "auto_archive_duration": 60 + }, + "status": 201, + "response": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608333881081877", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280866+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280866+00:00" + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "member": { + "id": "1570608327602471024", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:16:47.281657+00:00", + "flags": 1, + "muted": false, + "mute_config": null + } + } + }, + { + "step": "thread-history", + "method": "GET", + "path": "/channels/1570608327602471024/messages?limit=50", + "request": null, + "status": 200, + "response": [ + { + "type": 0, + "content": "Fixture: a webhook reply in a thread, <@1564742987727118426>", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:51.911000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608353267159146", + "channel_id": "1570608327602471024", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307", + "position": 1 + }, + { + "type": 0, + "content": "Fixture: a reply in a thread started from a message", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:50.045000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608345440722956", + "channel_id": "1570608327602471024", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + }, + { + "type": 21, + "content": "", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:47.289000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608333881081877", + "channel_id": "1570608327602471024", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 0, + "channel_id": "1570607253965246584", + "message_id": "1570608327602471024", + "guild_id": "840178841540362250" + }, + "position": 0, + "referenced_message": { + "type": 0, + "content": "Fixture: a message a thread is started from", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:45.792000+00:00", + "edited_timestamp": null, + "flags": 32, + "components": [], + "id": "1570608327602471024", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "thread": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608353267159146", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280000+00:00" + }, + "message_count": 2, + "member_count": 1, + "total_message_sent": 2 + } + } + } + ] + }, + { + "step": "thread-history-forward", + "method": "GET", + "path": "/channels/1570608327602471024/messages?after=0&limit=50", + "request": null, + "status": 200, + "response": [ + { + "type": 0, + "content": "Fixture: a webhook reply in a thread, <@1564742987727118426>", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:51.911000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608353267159146", + "channel_id": "1570608327602471024", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307", + "position": 1 + }, + { + "type": 0, + "content": "Fixture: a reply in a thread started from a message", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:50.045000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608345440722956", + "channel_id": "1570608327602471024", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + }, + { + "type": 21, + "content": "", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:47.289000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608333881081877", + "channel_id": "1570608327602471024", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 0, + "channel_id": "1570607253965246584", + "message_id": "1570608327602471024", + "guild_id": "840178841540362250" + }, + "position": 0, + "referenced_message": { + "type": 0, + "content": "Fixture: a message a thread is started from", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:45.792000+00:00", + "edited_timestamp": null, + "flags": 32, + "components": [], + "id": "1570608327602471024", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "thread": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608353267159146", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280000+00:00" + }, + "message_count": 2, + "member_count": 1, + "total_message_sent": 2 + } + } + } + ] + }, + { + "step": "thread-get-channel", + "method": "GET", + "path": "/channels/1570608327602471024", + "request": null, + "status": 200, + "response": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608353267159146", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280000+00:00" + }, + "message_count": 2, + "member_count": 1, + "total_message_sent": 2, + "member": { + "id": "1570608327602471024", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:16:47.281000+00:00", + "flags": 1, + "muted": false, + "mute_config": null + } + } + }, + { + "step": "thread-get-opener", + "method": "GET", + "path": "/channels/1570607253965246584/messages/1570608327602471024", + "request": null, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: a message a thread is started from", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:45.792000+00:00", + "edited_timestamp": null, + "flags": 32, + "components": [], + "id": "1570608327602471024", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "thread": { + "id": "1570608327602471024", + "type": 11, + "last_message_id": "1570608353267159146", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread from a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:16:47.280000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:16:47.280000+00:00" + }, + "message_count": 2, + "member_count": 1, + "total_message_sent": 2 + } + } + }, + { + "step": "thread-get-opener-by-thread-id", + "method": "GET", + "path": "/channels/1570608327602471024/messages/1570608327602471024", + "request": null, + "status": 404, + "response": { + "message": "Unknown Message", + "code": 10008 + } + }, + { + "step": "thread-nomessage-start", + "method": "POST", + "path": "/channels/1570607253965246584/threads", + "request": { + "name": "Fixture thread without a message", + "type": 11, + "auto_archive_duration": 60 + }, + "status": 201, + "response": { + "id": "1570608387434094622", + "type": 11, + "last_message_id": null, + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread without a message", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:17:00.057000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:17:00.057000+00:00" + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "member": { + "id": "1570608387434094622", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:17:00.232722+00:00", + "flags": 1, + "muted": false, + "mute_config": null + } + } + }, + { + "step": "thread-nomessage-history", + "method": "GET", + "path": "/channels/1570608387434094622/messages?limit=50", + "request": null, + "status": 200, + "response": [ + { + "type": 0, + "content": "Fixture: the first message in a thread created without a message", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:02.032000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608395717972000", + "channel_id": "1570608387434094622", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + } + ] + }, + { + "step": "thread-deleted-opener", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: an opener that will be deleted", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: an opener that will be deleted", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:06.167000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608413061283862", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + }, + { + "step": "thread-deleted-start", + "method": "POST", + "path": "/channels/1570607253965246584/messages/1570608413061283862/threads", + "request": { + "name": "Fixture thread whose opener is deleted", + "auto_archive_duration": 60 + }, + "status": 201, + "response": { + "id": "1570608413061283862", + "type": 11, + "last_message_id": "1570608419688419446", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture thread whose opener is deleted", + "parent_id": "1570607253965246584", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:17:07.739708+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:17:07.739708+00:00" + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "member": { + "id": "1570608413061283862", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:17:07.740426+00:00", + "flags": 1, + "muted": false, + "mute_config": null + } + } + }, + { + "step": "thread-deleted-history", + "method": "GET", + "path": "/channels/1570608413061283862/messages?limit=50", + "request": null, + "status": 200, + "response": [ + { + "type": 0, + "content": "Fixture: a reply before the opener is deleted", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:09.258000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608426025877659", + "channel_id": "1570608413061283862", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + }, + { + "type": 21, + "content": "", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:07.747000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608419688419446", + "channel_id": "1570608413061283862", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 0, + "channel_id": "1570607253965246584", + "message_id": "1570608413061283862", + "guild_id": "840178841540362250" + }, + "position": 0, + "referenced_message": null + } + ] + }, + { + "step": "thread-deleted-get-opener", + "method": "GET", + "path": "/channels/1570607253965246584/messages/1570608413061283862", + "request": null, + "status": 404, + "response": { + "message": "Unknown Message", + "code": 10008 + } + }, + { + "step": "forum-post", + "method": "POST", + "path": "/channels/1570607255684915302/threads", + "request": { + "name": "Fixture forum post", + "message": { + "content": "Fixture: the starter of a forum post", + "allowed_mentions": { + "parse": [] + } + }, + "auto_archive_duration": 60 + }, + "status": 201, + "response": { + "id": "1570608449778098218", + "type": 11, + "last_message_id": "1570608449778098218", + "flags": 0, + "guild_id": "840178841540362250", + "name": "Fixture forum post", + "parent_id": "1570607255684915302", + "rate_limit_per_user": 0, + "bitrate": 64000, + "user_limit": 0, + "rtc_region": null, + "owner_id": "1564742987727118426", + "thread_metadata": { + "archived": false, + "archive_timestamp": "2026-11-13T01:17:14.921000+00:00", + "auto_archive_duration": 60, + "locked": false, + "create_timestamp": "2026-11-13T01:17:14.921000+00:00" + }, + "message_count": 0, + "member_count": 1, + "total_message_sent": 0, + "member": { + "id": "1570608449778098218", + "user_id": "1564742987727118426", + "join_timestamp": "2026-11-13T01:17:14.926069+00:00", + "flags": 1, + "muted": false, + "mute_config": null + }, + "message": { + "type": 0, + "content": "Fixture: the starter of a forum post", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:14.921000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608449778098218", + "channel_id": "1570608449778098218", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + } + } + }, + { + "step": "forum-history", + "method": "GET", + "path": "/channels/1570608449778098218/messages?limit=50", + "request": null, + "status": 200, + "response": [ + { + "type": 0, + "content": "Fixture: a later reply in a forum post", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:16.290000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608455520227379", + "channel_id": "1570608449778098218", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + }, + { + "type": 0, + "content": "Fixture: the starter of a forum post", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:14.921000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608449778098218", + "channel_id": "1570608449778098218", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + } + ] + }, + { + "step": "forum-get-starter", + "method": "GET", + "path": "/channels/1570608449778098218/messages/1570608449778098218", + "request": null, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: the starter of a forum post", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:17:14.921000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608449778098218", + "channel_id": "1570608449778098218", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "position": 0 + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/posts.json b/packages/chat/discord/test/fixtures/recorded/posts.json new file mode 100644 index 00000000000..14f2c02f064 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/posts.json @@ -0,0 +1,1009 @@ +{ + "case": "Messages as createMessage and the webhook return them, for the posts behind the gateway fixtures", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "recordedAt": "2026-11-13T01:16Z, during EFF-3695", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "calls": [ + { + "step": "channel-plain", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: a plain message in a guild text channel", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: a plain message in a guild text channel", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:13+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608190062723103", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + }, + { + "step": "webhook-plain", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: a webhook message", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: a webhook message", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:14.460000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608196186411139", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "attachment-fixed", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "multipart": { + "content": "Fixture: a message with an attachment, fixed", + "files": [ + "fixture.txt (text/plain, 23 bytes)" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: a message with an attachment, fixed", + "mentions": [], + "mention_roles": [], + "attachments": [ + { + "id": "1570611668722122812", + "filename": "fixture.txt", + "size": 23, + "url": "https://cdn.discordapp.com/attachments/1570607253965246584/1570611668722122812/fixture.txt?scrubbed", + "proxy_url": "https://media.discordapp.net/attachments/1570607253965246584/1570611668722122812/fixture.txt?scrubbed", + "content_type": "text/plain; charset=utf-8", + "content_scan_version": 0 + } + ], + "embeds": [], + "timestamp": "2026-11-13T01:30:02.447000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570611669015732315", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + }, + "scrubbed": [ + "$.attachments[0].url", + "$.attachments[0].proxy_url" + ] + }, + { + "step": "reply-bot-ping-on", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: a reply with the ping on", + "message_reference": { + "message_id": "1570608190062723103" + }, + "allowed_mentions": { + "parse": [], + "replied_user": true + } + }, + "status": 200, + "response": { + "type": 19, + "content": "Fixture: a reply with the ping on", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:19.019000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608215308238930", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 0, + "channel_id": "1570607253965246584", + "message_id": "1570608190062723103", + "guild_id": "840178841540362250" + }, + "referenced_message": { + "type": 0, + "content": "Fixture: a plain message in a guild text channel", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:13+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608190062723103", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + } + }, + { + "step": "reply-bot-ping-off", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: a reply with the ping off", + "message_reference": { + "message_id": "1570608190062723103" + }, + "allowed_mentions": { + "parse": [], + "replied_user": false + } + }, + "status": 200, + "response": { + "type": 19, + "content": "Fixture: a reply with the ping off", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:20.534000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608221662740501", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "message_reference": { + "type": 0, + "channel_id": "1570607253965246584", + "message_id": "1570608190062723103", + "guild_id": "840178841540362250" + }, + "referenced_message": { + "type": 0, + "content": "Fixture: a plain message in a guild text channel", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:13+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608190062723103", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + } + }, + { + "step": "mention-bot", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <@1564742987727118426> as a plain user token", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <@1564742987727118426> as a plain user token", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:22.010000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608227853533387", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-bot-nick", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <@!1564742987727118426> as a nickname token", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <@!1564742987727118426> as a nickname token", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:23.426000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608233792405568", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-user", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <@449108784879763458> another user", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <@449108784879763458> another user", + "mentions": [ + { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:24.944000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608240159625278", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-role", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <@&1570607256838479964> a role", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <@&1570607256838479964> a role", + "mentions": [], + "mention_roles": [ + "1570607256838479964" + ], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:26.546000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608246878773301", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-everyone", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: @everyone that resolves", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: @everyone that resolves", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:28.416000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608254721982479", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": true, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-here", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: @here that resolves", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: @here that resolves", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:29.899000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608260942397441", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": true, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-everyone-suppressed", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: @everyone with parse empty", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: @everyone with parse empty", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:31.658000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608268320051301", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-channel", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <#1570605581192929301> a channel", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <#1570605581192929301> a channel", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:33.157000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608274607440034", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-code-span", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: `<@1564742987727118426>` in a code span", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: `<@1564742987727118426>` in a code span", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:35.311000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608283641970709", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-fence", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: in a fence\n```\n<@1564742987727118426>\n```", + "allowed_mentions": { + "parse": [ + "users", + "roles", + "everyone" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: in a fence\n```\n<@1564742987727118426>\n```", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:36.848000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608290088484875", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + }, + { + "step": "mention-bot-by-bot", + "method": "POST", + "path": "/channels/1570607253965246584/messages", + "request": { + "content": "Fixture: the bot naming <@1564742987727118426> and <@&1570607256838479964> with parse empty", + "allowed_mentions": { + "parse": [] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: the bot naming <@1564742987727118426> and <@&1570607256838479964> with parse empty", + "mentions": [], + "mention_roles": [], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:38.346000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608296371425382", + "channel_id": "1570607253965246584", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + } + }, + { + "step": "mention-mixed", + "method": "POST", + "path": "/webhooks/1570607260156297307/{webhook_token}?wait=true", + "request": { + "content": "Fixture: <@1564742987727118426> and `<@449108784879763458>` and <@&1570607256838479964> and <#1570607253965246584>", + "allowed_mentions": { + "parse": [], + "users": [ + "1564742987727118426" + ], + "roles": [ + "1570607256838479964" + ] + } + }, + "status": 200, + "response": { + "type": 0, + "content": "Fixture: <@1564742987727118426> and `<@449108784879763458>` and <@&1570607256838479964> and <#1570607253965246584>", + "mentions": [ + { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + ], + "mention_roles": [ + "1570607256838479964" + ], + "attachments": [], + "embeds": [], + "timestamp": "2026-11-13T01:16:39.899000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570608302885179546", + "channel_id": "1570607253965246584", + "author": { + "id": "1570607260156297307", + "username": "effect-chat-fixtures", + "avatar": null, + "discriminator": "0000", + "public_flags": 0, + "flags": 0, + "bot": true, + "global_name": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false, + "application_id": "1564742987727118426", + "webhook_id": "1570607260156297307" + } + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/uploads.json b/packages/chat/discord/test/fixtures/recorded/uploads.json new file mode 100644 index 00000000000..0d931b9bc24 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/uploads.json @@ -0,0 +1,218 @@ +{ + "case": "Uploads around Discord's limit for the Janitor bot in a guild with no boosts: 10 MiB + 1 byte and 20 MiB are accepted, 20 MiB + 1 byte and 25 MiB are refused with a 413 and a JSON body", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script. request.files[0] gives the size of the upload, whose bytes are all zero", + "where": "guild 840178841540362250 (Tim's test guild); #test 1570605581192929301", + "recordedAt": "2026-11-13T08:41Z, during EFF-3755", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed. Every posted message was deleted" + }, + "calls": [ + { + "step": "upload-10MiB-plus-1", + "method": "POST", + "path": "/channels/1570605581192929301/messages", + "request": { + "payload_json": { + "content": "effect-chat probe: upload-over-limit", + "allowed_mentions": { + "parse": [] + }, + "attachments": [ + { + "id": 0, + "filename": "probe.bin" + } + ] + }, + "files[0]": { + "filename": "probe.bin", + "bytes": 10485761 + } + }, + "status": 200, + "response": { + "type": 0, + "content": "effect-chat probe: upload-over-limit", + "mentions": [], + "mention_roles": [], + "attachments": [ + { + "id": "1570720143078850580", + "filename": "probe.bin", + "size": 10485761, + "url": "https://cdn.discordapp.com/attachments/1570605581192929301/1570720143078850580/probe.bin?scrubbed", + "proxy_url": "https://media.discordapp.net/attachments/1570605581192929301/1570720143078850580/probe.bin?scrubbed", + "content_type": "application/octet-stream", + "content_scan_version": 0 + } + ], + "embeds": [], + "timestamp": "2026-11-13T08:41:05.398000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570720146094424165", + "channel_id": "1570605581192929301", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + }, + "contentType": "application/json", + "scrubbed": [ + "$.attachments[0].url", + "$.attachments[0].proxy_url" + ] + }, + { + "step": "upload-20MiB", + "method": "POST", + "path": "/channels/1570605581192929301/messages", + "request": { + "payload_json": { + "content": "effect-chat probe: upload-20971520", + "allowed_mentions": { + "parse": [] + }, + "attachments": [ + { + "id": 0, + "filename": "probe.bin" + } + ] + }, + "files[0]": { + "filename": "probe.bin", + "bytes": 20971520 + } + }, + "status": 200, + "response": { + "type": 0, + "content": "effect-chat probe: upload-20971520", + "mentions": [], + "mention_roles": [], + "attachments": [ + { + "id": "1570720378395820032", + "filename": "probe.bin", + "size": 20971520, + "url": "https://cdn.discordapp.com/attachments/1570605581192929301/1570720378395820032/probe.bin?scrubbed", + "proxy_url": "https://media.discordapp.net/attachments/1570605581192929301/1570720378395820032/probe.bin?scrubbed", + "content_type": "application/octet-stream", + "content_scan_version": 0 + } + ], + "embeds": [], + "timestamp": "2026-11-13T08:42:01.012000+00:00", + "edited_timestamp": null, + "flags": 0, + "components": [], + "id": "1570720379356581938", + "channel_id": "1570605581192929301", + "author": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "pinned": false, + "mention_everyone": false, + "tts": false + }, + "contentType": "application/json", + "scrubbed": [ + "$.attachments[0].url", + "$.attachments[0].proxy_url" + ] + }, + { + "step": "upload-20MiB-plus-1", + "method": "POST", + "path": "/channels/1570605581192929301/messages", + "request": { + "payload_json": { + "content": "effect-chat probe: upload-20971521", + "allowed_mentions": { + "parse": [] + }, + "attachments": [ + { + "id": 0, + "filename": "probe.bin" + } + ] + }, + "files[0]": { + "filename": "probe.bin", + "bytes": 20971521 + } + }, + "status": 413, + "response": { + "message": "Request entity too large", + "code": 40005 + }, + "contentType": "application/json" + }, + { + "step": "upload-25MiB", + "method": "POST", + "path": "/channels/1570605581192929301/messages", + "request": { + "payload_json": { + "content": "effect-chat probe: upload-26214400", + "allowed_mentions": { + "parse": [] + }, + "attachments": [ + { + "id": 0, + "filename": "probe.bin" + } + ] + }, + "files[0]": { + "filename": "probe.bin", + "bytes": 26214400 + } + }, + "status": 413, + "response": { + "message": "Request entity too large", + "code": 40005 + }, + "contentType": "application/json" + } + ] +} diff --git a/packages/chat/discord/test/fixtures/recorded/users.json b/packages/chat/discord/test/fixtures/recorded/users.json new file mode 100644 index 00000000000..2808fd78389 --- /dev/null +++ b/packages/chat/discord/test/fixtures/recorded/users.json @@ -0,0 +1,177 @@ +{ + "case": "User and member reads: the bot, Tim, and the bot's own user", + "provenance": { + "kind": "recorded", + "source": "Discord REST v10 with the Janitor bot token, from a local script", + "where": "guild 840178841540362250 (Tim's test guild); #effect-chat-scratch 1570607253965246584, #effect-chat-forum 1570607255684915302 and #test 1570605581192929301", + "recordedAt": "2026-11-13T01:17Z, during EFF-3695", + "body": "Each response verbatim, apart from signed CDN URLs, whose query is replaced; see scrubbed" + }, + "calls": [ + { + "step": "get-user-bot", + "method": "GET", + "path": "/users/1564742987727118426", + "request": null, + "status": 200, + "response": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + }, + { + "step": "get-user-tim", + "method": "GET", + "path": "/users/449108784879763458", + "request": null, + "status": 200, + "response": { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + } + }, + { + "step": "get-member-bot", + "method": "GET", + "path": "/guilds/840178841540362250/members/1564742987727118426", + "request": null, + "status": 200, + "response": { + "avatar": null, + "banner": null, + "communication_disabled_until": null, + "flags": 0, + "joined_at": "2026-11-13T01:07:25.410000+00:00", + "nick": null, + "pending": false, + "premium_since": null, + "roles": [ + "1570605977017778196" + ], + "unusual_dm_activity_until": null, + "collectibles": null, + "display_name_styles": null, + "member_gaming_leaderboard_data": null, + "user": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "mute": false, + "deaf": false + } + }, + { + "step": "get-member-tim", + "method": "GET", + "path": "/guilds/840178841540362250/members/449108784879763458", + "request": null, + "status": 200, + "response": { + "avatar": null, + "banner": null, + "communication_disabled_until": null, + "flags": 0, + "joined_at": "2021-05-07T10:50:45.596000+00:00", + "nick": null, + "pending": false, + "premium_since": null, + "roles": [], + "unusual_dm_activity_until": null, + "collectibles": null, + "display_name_styles": null, + "member_gaming_leaderboard_data": null, + "user": { + "id": "449108784879763458", + "username": "timsmart", + "avatar": "2eded1558c297f57913f8b7e4c6b7efc", + "discriminator": "0", + "public_flags": 0, + "flags": 0, + "banner": null, + "accent_color": null, + "global_name": "Tim Smart", + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null + }, + "mute": false, + "deaf": false + } + }, + { + "step": "get-me", + "method": "GET", + "path": "/users/@me", + "request": null, + "status": 200, + "response": { + "id": "1564742987727118426", + "username": "Janitor", + "avatar": "07aaab1f8e37dd6b088b6f21d1332701", + "discriminator": "5426", + "public_flags": 0, + "flags": 0, + "bot": true, + "banner": null, + "accent_color": null, + "global_name": null, + "avatar_decoration_data": null, + "collectibles": null, + "display_name_styles": null, + "banner_color": null, + "clan": null, + "primary_guild": null, + "mfa_enabled": true, + "locale": "en-US", + "premium_type": 0, + "email": null, + "verified": true, + "bio": "" + } + } + ] +} diff --git a/packages/chat/discord/test/gateway.test.ts b/packages/chat/discord/test/gateway.test.ts new file mode 100644 index 00000000000..cc74ffc9226 --- /dev/null +++ b/packages/chat/discord/test/gateway.test.ts @@ -0,0 +1,191 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { assert, describe, it } from "@effect/vitest" +import * as DiscordConfig from "dfx/DiscordConfig" +import { DiscordWS, type MessageSend, Reconnect } from "dfx/DiscordGateway/DiscordWS" +import { MesssagingLive } from "dfx/DiscordGateway/Messaging" +import * as Shard from "dfx/DiscordGateway/Shard" +import { type ShardState, ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore" +import { MemoryRateLimitStoreLive, RateLimiterLive } from "dfx/RateLimit" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler" +import * as Context from "effect/Context" +import * as Effect from "effect/Effect" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Queue from "effect/Queue" +import * as Redacted from "effect/Redacted" +import * as Socket from "effect/socket/Socket" +import * as TestClock from "effect/testing/TestClock" +import { discordLayer, events, makeFake } from "./utils.ts" + +/** Storage that outlives a host: what a Durable Object keeps between evictions. */ +type Storage = Map + +const sessionStore = (storage: Storage) => + Layer.succeed( + ShardStateStore, + ShardStateStore.of({ + forShard: ([id, count]) => { + const key = `${id}/${count}` + return { + get: Effect.sync(() => Option.fromUndefinedOr(storage.get(key))), + set: (state) => Effect.sync(() => void storage.set(key, state)), + clear: Effect.sync(() => void storage.delete(key)) + } + } + }) + ) + +const stored = (storage: Storage) => storage.get("0/1") + +const hello = { op: 10, d: { heartbeat_interval: 41_250 } } +const ready = { + op: 0, + t: "READY", + s: 1, + d: { session_id: "session-1", resume_gateway_url: "wss://resume.example" } +} +const created = events("channel-message")[0] +const message = (s: number) => ({ op: 0, t: created.t, s, d: created.d }) + +/** Lets the shard's fibers run until `done`. */ +const until = Effect.fnUntraced(function*(done: () => boolean) { + for (let i = 0; i < 1000 && !done(); i++) yield* Effect.yieldNow + assert.isTrue(done(), "the shard did not get there") +}) + +/** + * One host: dfx's real shard over a socket the test drives, with + * `layerDispatchHook` and `storage` behind it. Closing the scope stops the + * host, as an eviction does. + */ +const host = Effect.fnUntraced(function*( + storage: Storage, + handle: (event: ChatEvent.ChatEvent) => Effect.Effect +) { + const inbound = yield* Queue.unbounded() + const written: Array = [] + let onConnecting: Effect.Effect = Effect.void + const socket = Layer.succeed( + DiscordWS, + DiscordWS.of({ + connect: (options) => + Effect.sync(() => { + onConnecting = options.onConnecting + return { + take: Queue.take(inbound), + setUrl: () => Effect.void, + write: (sent) => Effect.sync(() => void written.push(sent)) + } + }) + }) + ) + const context = yield* Layer.build( + Layer.effect(Shard.Shard, Shard.make).pipe( + Layer.provide([socket, MesssagingLive, RateLimiterLive, sessionStore(storage), DiscordChat.layerDispatchHook]), + Layer.provide([ + MemoryRateLimitStoreLive, + DiscordConfig.layer({ token: Redacted.make("token") }), + Layer.succeed(ChatHandler, ChatHandler.of({ handle })), + discordLayer(makeFake({})) + ]) + ) + ) + yield* Context.get(context, Shard.Shard).connect([0, 1]).pipe( + Effect.provideService(Socket.WebSocketConstructor, () => { + throw new Error("the test drives the socket") + }) + ) + /** What the shard wrote with `op`: 2 is IDENTIFY and 6 RESUME. */ + const sent = (op: number): Array => + written.filter((payload): payload is Exclude => payload !== Reconnect).filter((payload) => + payload.op === op + ).map((payload) => payload.d) + return { + receive: (payload: unknown) => Queue.offer(inbound, payload), + sent, + reconnects: () => written.filter((payload) => payload === Reconnect).length, + /** The socket opened again, as dfx's does after it drops the connection. */ + reopened: Effect.suspend(() => onConnecting) + } +}) + +describe("layerDispatchHook", () => { + it.effect("a host built again resumes the stored session, having handled each event before its sequence was stored", () => { + const storage: Storage = new Map() + const seen: Array<{ readonly id: string; readonly storedSequence: number | null | undefined }> = [] + const handle = (event: ChatEvent.ChatEvent) => + Effect.sync(() => { + if (event._tag === "MessageCreated") { + seen.push({ id: event.message.id, storedSequence: stored(storage)?.sequence }) + } + }) + return Effect.gen(function*() { + yield* Effect.scoped(Effect.gen(function*() { + const first = yield* host(storage, handle) + yield* first.receive(hello) + yield* until(() => first.sent(2).length === 1) + assert.strictEqual(first.sent(6).length, 0) + yield* first.receive(ready) + yield* first.receive(message(2)) + yield* until(() => stored(storage)?.sequence === 2) + assert.deepStrictEqual(seen, [{ id: created.d.id, storedSequence: 1 }]) + })) + assert.deepStrictEqual(stored(storage), { + sessionId: "session-1", + resumeUrl: "wss://resume.example", + sequence: 2 + }) + yield* Effect.scoped(Effect.gen(function*() { + const second = yield* host(storage, handle) + yield* second.receive(hello) + yield* until(() => second.sent(6).length === 1) + assert.deepStrictEqual(second.sent(6), [{ token: "token", session_id: "session-1", seq: 2 }]) + assert.strictEqual(second.sent(2).length, 0) + })) + }) + }) + + it.effect("an event refused with NotAccepted is not stored and comes back with the RESUME", () => { + const storage: Storage = new Map() + let attempts = 0 + const handle = () => Effect.suspend(() => attempts++ === 0 ? Effect.fail(new NotAccepted()) : Effect.void) + return Effect.scoped(Effect.gen(function*() { + const gateway = yield* host(storage, handle) + yield* gateway.receive(hello) + yield* gateway.receive(ready) + yield* until(() => stored(storage)?.sequence === 1) + yield* gateway.receive(message(2)) + yield* until(() => gateway.reconnects() === 1) + assert.strictEqual(attempts, 1) + assert.strictEqual(stored(storage)?.sequence, 1) + // dfx waits a second after the first failure before it resumes. + yield* gateway.reopened + yield* gateway.receive(hello) + yield* TestClock.adjust("1 second") + yield* until(() => gateway.sent(6).length === 1) + assert.deepStrictEqual(gateway.sent(6), [{ token: "token", session_id: "session-1", seq: 1 }]) + // Discord replays what came after sequence 1. + yield* gateway.receive(message(2)) + yield* until(() => stored(storage)?.sequence === 2) + assert.strictEqual(attempts, 2) + assert.strictEqual(gateway.reconnects(), 1) + })) + }) + + it.effect("a handler defect is logged and the event dropped, so the events after it still arrive", () => { + const storage: Storage = new Map() + let handled = 0 + const handle = () => Effect.suspend(() => handled++ === 0 ? Effect.die(new Error("bug")) : Effect.void) + return Effect.scoped(Effect.gen(function*() { + const gateway = yield* host(storage, handle) + yield* gateway.receive(hello) + yield* gateway.receive(ready) + yield* gateway.receive(message(2)) + yield* gateway.receive(message(3)) + yield* until(() => stored(storage)?.sequence === 3) + assert.strictEqual(handled, 2) + assert.strictEqual(gateway.reconnects(), 0) + })) + }) +}) diff --git a/packages/chat/discord/test/interactions.test.ts b/packages/chat/discord/test/interactions.test.ts new file mode 100644 index 00000000000..176f37c5d53 --- /dev/null +++ b/packages/chat/discord/test/interactions.test.ts @@ -0,0 +1,228 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { assert, describe, it } from "@effect/vitest" +import * as DiscordConfig from "dfx/DiscordConfig" +import { DiscordRESTLive } from "dfx/DiscordREST" +import { MemoryRateLimitStoreLive } from "dfx/RateLimit" +import * as Chat from "effect/chat/Chat" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Clock from "effect/Clock" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as TestClock from "effect/testing/TestClock" +import { botUserId, discordLayer, type Fake, guild, makeFake, recordedCommands, scratch, tim } from "./utils.ts" + +const message = { id: "1570700000000000001", channel_id: scratch } + +const click = (data: Record, extra: Record = {}) => ({ + id: "1570700000000000100", + application_id: botUserId, + type: 3, + token: "token", + version: 1, + guild_id: guild, + channel_id: scratch, + member: { user: { id: tim } }, + message, + data, + ...extra +}) + +const submit = (components: ReadonlyArray) => click({ custom_id: "form", components }, { type: 5 }) + +const slash = (name: string, options?: ReadonlyArray, data: Record = {}) => ({ + ...recordedCommands.commands.private, + id: "1570700000000000100", + token: "token", + data: { id: "1570700000000000900", name, type: 1, ...(options === undefined ? {} : { options }), ...data } +}) + +const some = (raw: unknown) => Option.getOrThrow(DiscordChat.normalizeInteraction(raw)) + +describe("normalizeInteraction", () => { + it("a button splits its custom_id into id and value at the first join, from a member or a direct's user", () => { + const { member: _, ...direct } = click({ custom_id: "go\u241Fa\u241Fb", component_type: 2 }) + for (const raw of [click({ custom_id: "go\u241Fa\u241Fb", component_type: 2 }), { ...direct, user: { id: tim } }]) { + const clicked = some(raw) + assert.deepStrictEqual( + clicked, + ChatInteraction.ButtonClicked.make({ + id: "go", + value: "a\u241Fb", + thread: DiscordChat.thread(scratch), + messageId: message.id, + userId: tim, + raw: clicked.raw + }) + ) + } + }) + + it("a submit reads each input, a Label's or an action row's, and leaves an empty one out", () => { + const submitted = some(submit([ + { type: 10 }, + { type: 18, component: { type: 4, custom_id: "name", value: "Tim" } }, + { type: 18, component: { type: 4, custom_id: "note", value: "" } }, + { type: 18, component: { type: 3, custom_id: "size", values: ["l"] } }, + { type: 18, component: { type: 3, custom_id: "none", values: [] } }, + { type: 1, components: [{ type: 4, custom_id: "old", value: "row" }] } + ])) + assert(submitted._tag === "ModalSubmitted") + assert.deepStrictEqual(submitted.values, { name: "Tim", size: "l", old: "row" }) + }) + + it("a submit keeps an input whose id is __proto__ as its own field", () => { + const submitted = some(submit([ + { type: 18, component: { type: 4, custom_id: "__proto__", value: "answered" } }, + { type: 18, component: { type: 4, custom_id: "name", value: "Tim" } } + ])) + assert(submitted._tag === "ModalSubmitted") + assert.isTrue(Object.hasOwn(submitted.values, "__proto__")) + assert.deepStrictEqual(Object.entries(submitted.values), [["__proto__", "answered"], ["name", "Tim"]]) + }) + + it("anything not from an element the adapter built, or a command it can't express, is None", () => { + const { message: _, ...noMessage } = click({ custom_id: "go", component_type: 2 }) + for ( + const raw of [ + // A user command, and a slash command with an integer option. + slash("profile", undefined, { type: 2 }), + slash("roll", [{ type: 4, name: "sides", value: 6 }]), + // Autocomplete for a command. + { ...slash("deploy", [{ type: 3, name: "version", value: "v", focused: true }]), type: 4 }, + // A user select, and a click with no message. + click({ custom_id: "users", component_type: 5, values: [tim] }), + noMessage + ] + ) { + assert.isTrue(Option.isNone(DiscordChat.normalizeInteraction(raw))) + } + }) +}) + +describe("interaction", () => { + const handler = (options: { + readonly delay?: Duration.Duration + readonly command?: ChatInteraction.CommandReply + }) => + ChatInteraction.Handler.of({ + click: () => Effect.as(Effect.sleep(options.delay ?? Duration.zero), ChatInteraction.ack), + submit: () => Effect.succeed(ChatInteraction.close), + command: () => Effect.succeed(options.command ?? ChatInteraction.ack), + refused: (_, error) => Effect.die(error) + }) + + const deliver = ( + fake: Fake, + raw: unknown, + handler: ChatInteraction.Handler["Service"], + options?: { readonly deadline?: Duration.Duration } + ) => + Effect.flatMap(DiscordChat.DiscordChat, (discord) => discord.interaction(raw, options)).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.provide(discordLayer(fake)) + ) + + const answering = () => makeFake({ createInteractionResponse: () => Effect.succeed(null) }) + const payloads = (fake: Fake) => fake.called("createInteractionResponse").map(([, , options]) => options.payload) + + it.effect("sends nothing when the handler misses its deadline, 2 seconds by default", () => + Effect.gen(function*() { + const fake = answering() + const late = handler({ delay: Duration.seconds(3) }) + const fiber = yield* Effect.forkChild(deliver(fake, click({ custom_id: "go", component_type: 2 }), late)) + yield* TestClock.adjust("2 seconds") + yield* Fiber.join(fiber) + assert.deepStrictEqual(payloads(fake), []) + const longer = yield* Effect.forkChild( + deliver(fake, click({ custom_id: "go", component_type: 2 }), late, { deadline: Duration.seconds(5) }) + ) + yield* TestClock.adjust("3 seconds") + yield* Fiber.join(longer) + assert.deepStrictEqual(payloads(fake), [{ type: 6 }]) + })) + + it.effect("withPostOptions does not reach a command's answer, which notifies nobody", () => + Effect.gen(function*() { + const fake = answering() + yield* DiscordChat.withPostOptions( + deliver( + fake, + slash("summarize"), + handler({ + command: ChatInteraction.respond({ markdown: "Hi", visibility: "public" }) + }) + ), + { suppressEmbeds: false, notify: { everyone: true } } + ) + assert.deepStrictEqual(payloads(fake), [{ + type: 4, + data: { content: "Hi", allowed_mentions: { parse: [], replied_user: false, roles: [], users: [] }, flags: 4 } + }]) + })) +}) + +it("commandRequests gives a CHAT_INPUT command per definition, with one STRING option for an argument", () => { + const group = ChatInteraction.group( + ChatInteraction.command("summarize", { description: "Summarize this thread" }), + ChatInteraction.button("go", { label: "Go" }), + ChatInteraction.command("note", { description: "Add a note", argument: { name: "text", description: "The note" } }) + ) + assert.deepStrictEqual(DiscordChat.commandRequests(group), [ + { type: 1, name: "summarize", description: "Summarize this thread" }, + { + type: 1, + name: "note", + description: "Add a note", + options: [{ type: 3, name: "text", description: "The note", required: false }] + } + ]) +}) + +it.effect("followUp sends one HTTP request through dfx's layers on a 429 just before 15 minutes, and fails RateLimited", () => + Effect.gen(function*() { + const deferred = ChatInteraction.defer(some(slash("summarize")) as ChatInteraction.CommandInvoked, { + visibility: "public" + }) + const issued = Number((BigInt("1570700000000000100") >> 22n) + 1420070400000n) + const attempts: Array<{ readonly url: string; readonly authorized: boolean; readonly at: number }> = [] + const http = HttpClient.make((request) => + Effect.map(Clock.currentTimeMillis, (at) => { + attempts.push({ url: request.url, authorized: "authorization" in request.headers, at: at - issued }) + return HttpClientResponse.fromWeb( + request, + Response.json({ message: "You are being rate limited.", retry_after: 0.6, global: false }, { + status: 429, + headers: { "retry-after": "0.6" } + }) + ) + }) + ) + // dfx's own REST layers, which wait out a 429 and send again, over an HTTP client that counts. + const layer = DiscordChat.layer.pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(DiscordRESTLive), + Layer.provide([ + DiscordConfig.layer({ token: Redacted.make("bot-token"), rest: { baseUrl: "https://discord.test/api" } }), + MemoryRateLimitStoreLive, + Layer.succeed(HttpClient.HttpClient, http) + ]) + ) + yield* TestClock.setTime(issued + Duration.toMillis(Duration.minutes(15)) - 300) + const fiber = yield* Effect.forkChild(Effect.flip( + Effect.flatMap(Chat.Chat, (chat) => chat.followUp(deferred, { markdown: "Done" })).pipe(Effect.provide(layer)) + )) + yield* TestClock.adjust(Duration.seconds(10)) + const error = yield* Fiber.join(fiber) + assert.deepStrictEqual([error.op, error.reason._tag], ["followUp", "RateLimited"]) + assert.deepStrictEqual(attempts, [{ + url: `https://discord.test/api/webhooks/${botUserId}/token/messages/@original`, + authorized: false, + at: Duration.toMillis(Duration.minutes(15)) - 300 + }]) + })) diff --git a/packages/chat/discord/test/live.test.ts b/packages/chat/discord/test/live.test.ts new file mode 100644 index 00000000000..dbf768847d7 --- /dev/null +++ b/packages/chat/discord/test/live.test.ts @@ -0,0 +1,113 @@ +/** + * The conformance suite against a real Discord guild. Opt-in: it runs only + * when these are set, so CI skips it. + * + * - `DISCORD_LIVE_BOT_TOKEN` and `DISCORD_LIVE_CHANNEL`: the bot and a + * scratch text channel it may write to. Threads that `startThread` makes + * can't be deleted through the core ops, so they are left behind. + * - `DISCORD_LIVE_SECOND_BOT_TOKEN`: a second bot in the guild, for the + * rules that need another identity's message. Without it, + * `DISCORD_LIVE_WEBHOOK_URL`, a webhook on the channel, stands in. Without + * either, those rules fail at `userPosts`. + * + * Of the buttons rules, only the outbound ones run here: `actions/posts`, + * `actions/tooMany`, `actions/dataTooLarge`, the two + * `descriptor/measure*Actions` rules and the `unsupported/buttons` and + * `unsupported/selects` probes. The suite is given no `interactions` hook, + * since nothing can click a button through the API, so no rule here clicks, + * opens or submits a modal, or calls `DiscordChat.interaction` and its + * callbacks. Those rules, and `post/refusedFile`, which needs `refuseFiles`, + * run only over the stubbed REST in `conformance.test.ts`. Live evidence + * for them needs a person clicking in a test guild. + */ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { describe, it } from "@effect/vitest" +import { DiscordConfig, DiscordREST, DiscordRESTMemoryLive } from "dfx" +import * as Chat from "effect/chat/Chat" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { conformance } from "effect/chat/ChatTest" +import type * as ChatThread from "effect/chat/ChatThread" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as Result from "effect/Result" +import * as Schema from "effect/Schema" +import * as Semaphore from "effect/Semaphore" + +const botToken = process.env.DISCORD_LIVE_BOT_TOKEN +const secondToken = process.env.DISCORD_LIVE_SECOND_BOT_TOKEN +const webhookUrl = process.env.DISCORD_LIVE_WEBHOOK_URL +const channel = process.env.DISCORD_LIVE_CHANNEL + +const restLayer = (token: string) => + DiscordRESTMemoryLive.pipe( + Layer.provide(DiscordConfig.layer({ token: Redacted.make(token) })), + Layer.provide(FetchHttpClient.layer) + ) + +describe.skipIf(botToken === undefined || channel === undefined)("live conformance", () => { + const bot = restLayer(botToken!) + const layer = DiscordChat.layer.pipe( + Layer.provideMerge(Chat.layer), + Layer.provideMerge(bot), + Layer.provide(FetchHttpClient.layer) + ) + + /** Posts as the other identity, reads the message back as the bot and normalises it as a `MESSAGE_CREATE`. */ + const userPosts = (thread: ChatThread.ChatThread, text: string): Effect.Effect => + Effect.gen(function*() { + const posted: { readonly id: string } = secondToken !== undefined + ? yield* Effect.flatMap( + DiscordREST, + (rest) => rest.createMessage(thread.id, { content: text, allowed_mentions: { parse: [] } }) + ).pipe( + Effect.provide(restLayer(secondToken)) + ) + : webhookUrl !== undefined + ? yield* Effect.tryPromise(async () => { + const url = new URL(webhookUrl) + url.searchParams.set("wait", "true") + const channelInfo = await fetch(`https://discord.com/api/v10/channels/${thread.id}`, { + headers: { authorization: `Bot ${botToken}` } + }).then((response) => response.json()).then(Schema.decodeUnknownSync(Schema.Struct({ type: Schema.Number }))) + if (channelInfo.type === 11 || channelInfo.type === 10 || channelInfo.type === 12) { + url.searchParams.set("thread_id", thread.id) + } + const response = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ content: text, allowed_mentions: { parse: [] } }) + }) + if (!response.ok) throw new Error(`webhook answered ${response.status}`) + return Schema.decodeUnknownSync(Schema.Struct({ id: Schema.String }))(await response.json()) + }) + : yield* Effect.die(new Error("neither DISCORD_LIVE_SECOND_BOT_TOKEN nor DISCORD_LIVE_WEBHOOK_URL is set")) + const rest = yield* DiscordREST + const message = yield* rest.getMessage(thread.id, posted.id) + const info = yield* rest.getChannel(thread.id) + const discord = yield* DiscordChat.DiscordChat + const result = DiscordChat.normalize({ t: "MESSAGE_CREATE", d: message }, { + botUserId: yield* discord.botUserId, + channelType: info.type + }) + if (Result.isFailure(result) || result.success.length !== 1) { + return yield* Effect.die(new Error("userPosts did not normalise to one event")) + } + return result.success[0] + }).pipe(Effect.provide(layer), Effect.orDie) + + const rules = conformance({ + platform: "discord", + thread: DiscordChat.thread(channel ?? ""), + userPosts, + parseMention: DiscordChat.parseMention + }) + + // The rules share one channel and the project runs tests concurrently, so + // one rule at a time. + const one = Semaphore.makeUnsafe(1) + for (const [name, rule] of Object.entries(rules)) { + it.live(name, () => rule.pipe(Effect.provide(layer), Semaphore.withPermits(one, 1)), 600_000) + } +}) diff --git a/packages/chat/discord/test/normalize.test.ts b/packages/chat/discord/test/normalize.test.ts new file mode 100644 index 00000000000..96e4d1d56a4 --- /dev/null +++ b/packages/chat/discord/test/normalize.test.ts @@ -0,0 +1,140 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatEvent from "effect/chat/ChatEvent" +import * as ChatMessage from "effect/chat/ChatMessage" +import { checkEvent } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as Result from "effect/Result" +import * as fs from "node:fs" +import { botUserId, type Dispatch, events, guild, scratch, tim } from "./utils.ts" + +const role = "1570607256838479964" +const checkOptions = { platform: "discord", parseMention: DiscordChat.parseMention } +const context = { botUserId } + +const normalize = (dispatch: Dispatch): ReadonlyArray => { + const result = DiscordChat.normalize(dispatch, context) + assert(Result.isSuccess(result), "expected events") + return result.success +} + +const created = (dispatch: Dispatch): ChatEvent.MessageCreated => { + const [event, ...rest] = normalize(dispatch) + assert(event._tag === "MessageCreated" && rest.length === 0) + return event +} + +const base = events("channel-message")[0] + +describe("normalize", () => { + it.effect("every recorded dispatch passes checkEvent", () => + Effect.gen(function*() { + let count = 0 + for (const file of fs.readdirSync(new URL("./fixtures/events", import.meta.url))) { + for (const dispatch of events(file.replace(/\.json$/, ""))) { + const result = DiscordChat.normalize(dispatch, { ...context, channelType: 0 }) + assert(Result.isSuccess(result), file) + for (const event of result.success) { + yield* checkEvent(event, checkOptions) + count++ + } + } + } + assert.isAbove(count, 35) + })) + + it("a MESSAGE_CREATE without channel_type fails MissingChannelType, then takes it from the context", () => { + const dispatch = events("channel-message-without-channel-type")[0] + const missing = DiscordChat.normalize(dispatch, context) + assert(Result.isFailure(missing)) + assert.instanceOf(missing.failure, DiscordChat.MissingChannelType) + assert.strictEqual(missing.failure.channelId, scratch) + const kind = (dispatch: Dispatch, channelType: number) => { + const result = DiscordChat.normalize(dispatch, { ...context, channelType }) + assert(Result.isSuccess(result) && result.success[0]._tag === "MessageCreated") + return result.success[0].kind + } + assert.deepStrictEqual([kind(dispatch, 11), kind(dispatch, 1)], ["thread", "direct"]) + // The payload's own type wins over the context. + assert.strictEqual(kind(base, 11), "channel") + }) + + it("a message with an invalid timestamp is dropped, not thrown", () => { + assert.deepStrictEqual(normalize({ t: base.t, d: { ...base.d, timestamp: "not a date" } }), []) + }) + + it("THREAD_STARTER_MESSAGE and THREAD_CREATED give no event", () => { + const dispatches = events("thread-started") + assert.deepStrictEqual(dispatches.map((dispatch) => dispatch.d.type).sort(), [18, 21, 21]) + for (const dispatch of dispatches) assert.deepStrictEqual(normalize(dispatch), []) + }) + + it("a person's reply to the bot is a reply, not a mention, with the ping on or off", () => { + const [pingOn, pingOff] = events("reply-ping").filter((dispatch) => dispatch.d.author.id === tim) + // Discord lists the replied-to bot in mentions when the ping is on, with no token in content. + assert.deepStrictEqual(pingOn.d.mentions.map((user: any) => user.id), [botUserId]) + assert.deepStrictEqual(pingOff.d.mentions, []) + for (const dispatch of [pingOn, pingOff]) { + const event = created(dispatch) + assert.deepStrictEqual(event.message.mentions, []) + assert.isTrue(ChatMessage.repliesToSelf(event.message)) + assert.deepStrictEqual(Option.getOrThrow(DiscordChat.member(event)).roles, dispatch.d.member.roles) + } + }) + + describe("mentions", () => { + const mentionsOf = (event: ChatEvent.MessageCreated) => + event.message.mentions.map((mention) => ({ + kind: mention.kind, + id: mention.id, + isSelf: mention.isSelf, + token: event.message.text.slice(mention.start, mention.end) + })) + const cases: ReadonlyArray< + readonly [string, ReadonlyArray<{ kind: string; id: string; isSelf: boolean; token: string }>] + > = [ + ["Fixture: <@!", [{ kind: "user", id: botUserId, isSelf: true, token: `<@!${botUserId}>` }]], + ["Fixture: <@1564742987727118426> and", [ + { kind: "user", id: botUserId, isSelf: true, token: `<@${botUserId}>` }, + { kind: "role", id: role, isSelf: false, token: `<@&${role}>` }, + { kind: "channel", id: scratch, isSelf: false, token: `<#${scratch}>` } + ]], + ["Fixture: @everyone that", [{ kind: "broadcast", id: "everyone", isSelf: false, token: "@everyone" }]], + // Posted without the ping, so Discord did not resolve it. + ["Fixture: @everyone with parse", []] + ] + for (const [prefix, expected] of cases) { + it(prefix, () => { + const found = events("mentions").filter((dispatch) => String(dispatch.d.content).startsWith(prefix)) + assert.strictEqual(found.length, 1, prefix) + assert.deepStrictEqual(mentionsOf(created(found[0])), expected) + }) + } + + it("a token in code or escaped is not a mention", () => { + const event = created({ + t: base.t, + d: { + ...base.d, + content: "<@1> `<@2>` ``<@3>`` ```\n<@4>\n``` \\<@5> <@6>", + mentions: ["1", "2", "3", "4", "5", "6"].map((id) => ({ id })) + } + }) + assert.deepStrictEqual(event.message.mentions.map((mention) => mention.id), ["1", "6"]) + }) + }) + + it("reactions give the emoji, custom ones by name, and the message's author when added", () => { + const found = events("reactions").map((dispatch) => normalize(dispatch)[0]) + assert.deepStrictEqual( + found.slice(0, 4).map((event) => + event._tag === "ReactionAdded" ? [event.emoji, event.messageAuthorId] : [event._tag] + ), + [["\u{1F44D}", botUserId], ["\u{1F44D}\u{1F3FD}", botUserId], ["custom:effectchat", botUserId], [ + "ReactionRemoved" + ]] + ) + assert.strictEqual(Option.getOrThrow(DiscordChat.guildId(found[0])), guild) + }) +}) diff --git a/packages/chat/discord/test/utils.ts b/packages/chat/discord/test/utils.ts new file mode 100644 index 00000000000..721e0d295e1 --- /dev/null +++ b/packages/chat/discord/test/utils.ts @@ -0,0 +1,156 @@ +import * as DiscordChat from "@effect/chat-discord/DiscordChat" +import { DiscordREST } from "dfx/DiscordREST" +import * as Chat from "effect/chat/Chat" +import type { ChatAdapter } from "effect/chat/ChatAdapter" +import { Registry } from "effect/chat/ChatAdapter" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import type * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as fs from "node:fs" + +/** The Janitor application the fixtures were recorded with. */ +export const botUserId = "1564742987727118426" +export const tim = "449108784879763458" +export const guild = "840178841540362250" +export const scratch = "1570607253965246584" + +export interface Dispatch { + readonly receivedAt?: string + readonly t: string + readonly d: any +} + +/** The dispatches of a gateway fixture in `fixtures/events`. */ +export const events = (name: string): ReadonlyArray => + JSON.parse(fs.readFileSync(new URL(`./fixtures/events/${name}.json`, import.meta.url), "utf8")).dispatches + +interface RecordedCall { + readonly step: string + readonly method: string + readonly path: string + readonly request: any + readonly status: number + readonly response: any +} + +/** + * The slash commands recorded in EFF-3802, `fixtures/recorded/commands.json`. + * The built command payloads start from these. + */ +export const recordedCommands: any = JSON.parse( + fs.readFileSync(new URL("./fixtures/recorded/commands.json", import.meta.url), "utf8") +) + +/** The REST calls of a fixture in `fixtures/recorded`. */ +const recorded = (name: string): ReadonlyArray => + JSON.parse(fs.readFileSync(new URL(`./fixtures/recorded/${name}.json`, import.meta.url), "utf8")).calls + +/** The one recorded call for `step`. */ +export const step = (name: string, stepName: string): RecordedCall => { + const found = recorded(name).filter((call) => call.step === stepName) + if (found.length !== 1) throw new Error(`${name}/${stepName}: ${found.length} calls`) + return found[0] +} + +/** What dfx fails with for a 4xx answer, as the adapter reads it. */ +export const errorResponse = (status: number, data: unknown) => ({ + _tag: status === 429 ? "RatelimitedResponse" : "ErrorResponse", + data, + response: { status }, + message: JSON.stringify(data) +}) + +/** What dfx fails with for a 5xx answer. */ +export const statusCodeError = (status: number) => ({ + _tag: "HttpClientError", + reason: { _tag: "StatusCodeError", response: { status } } +}) + +/** A recorded call as the fake's answer: its body, or dfx's error for it. */ +export const answer = (call: RecordedCall): Effect.Effect => + call.status >= 400 ? Effect.fail(errorResponse(call.status, call.response)) : Effect.succeed(call.response) + +type Handler = (...args: Array) => Effect.Effect + +export interface Fake { + readonly calls: Array<{ readonly method: string; readonly args: ReadonlyArray }> + readonly called: (method: string) => ReadonlyArray> + readonly service: DiscordREST["Service"] +} + +/** + * A `DiscordREST` that answers from `handlers` and records every call. A + * method with no handler dies, so a test sees any call it didn't expect. + */ +export const makeFake = (handlers: Record): Fake => { + const calls: Array<{ method: string; args: ReadonlyArray }> = [] + const service = new Proxy({}, { + get: (_, method: string) => { + if (method === "withFiles") return () => (effect: Effect.Effect) => effect + return (...args: Array) => + Effect.suspend(() => { + calls.push({ method, args }) + const handler = handlers[method] + return handler === undefined ? Effect.die(new Error(`unexpected ${method}`)) : handler(...args) + }) + } + }) as DiscordREST["Service"] + return { + calls, + called: (method) => calls.filter((call) => call.method === method).map((call) => call.args), + service + } +} + +const getMyUser = () => Effect.succeed({ id: botUserId, username: "Janitor", bot: true }) + +const originalResponse = /\/webhooks\/([^/]+)\/([^/]+)\/messages\/@original$/ + +/** + * The fake's `updateOriginalWebhookMessage` as Discord's HTTP answer, for + * `followUp`, which sends over the plain `HttpClient`. + */ +const webhookEdit = (fake: Fake, request: HttpClientRequest.HttpClientRequest, path: RegExpExecArray) => + fake.service.updateOriginalWebhookMessage(decodeURIComponent(path[1]), decodeURIComponent(path[2]), { + payload: request.body._tag === "Uint8Array" ? JSON.parse(new TextDecoder().decode(request.body.body)) : undefined + }).pipe( + Effect.match({ + onSuccess: (body) => Response.json(body), + onFailure: (error: any) => Response.json(error.data ?? {}, { status: error.response.status }) + }), + Effect.map((response) => HttpClientResponse.fromWeb(request, response)) + ) + +/** + * `DiscordChat.layer` over a fake, with `Chat` and an `HttpClient` that + * answers an original response's edit from the fake, and any other request, + * an attachment's download, from its `download` handler. + */ +export const discordLayer = (fake: Fake) => + DiscordChat.layer.pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(Layer.succeed( + DiscordREST, + new Proxy(fake.service, { + get: (target, key) => key === "getMyUser" ? getMyUser : (target as any)[key] + }) + )), + Layer.provide(Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => { + const path = request.method === "PATCH" ? originalResponse.exec(request.url) : null + return path !== null + ? webhookEdit(fake, request, path) + : (fake.service as any).download(request.url).pipe( + Effect.map((bytes: Uint8Array) => HttpClientResponse.fromWeb(request, new Response(bytes))) + ) + }) + )) + ) + +export const adapter: Effect.Effect = Effect.gen(function*() { + const registry = yield* Registry + return yield* registry.get("discord") +}) diff --git a/packages/chat/discord/tsconfig.json b/packages/chat/discord/tsconfig.json new file mode 100644 index 00000000000..e2a8ca19a0d --- /dev/null +++ b/packages/chat/discord/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "references": [ + { "path": "../../effect" } + ] +} diff --git a/packages/chat/slack/README.md b/packages/chat/slack/README.md new file mode 100644 index 00000000000..794487181ec --- /dev/null +++ b/packages/chat/slack/README.md @@ -0,0 +1,47 @@ +# @effect/chat-slack + +A [Slack](https://slack.com) adapter for `effect/chat`. Includes the Events API webhook, a pure event normaliser for Socket Mode or your own transport, a minimal Web API client, native streaming replies with the agent stop button, and the Slack emoji table. + +## Installation + +```sh +npm install effect @effect/chat-slack +``` + +## Signature verification + +`SlackChat.layerWebhook` and `SlackChat.layerInteractivity` require the +`Mac` service; the adapter does not provide a default. Supply +`NodeCrypto.layerMac` from `@effect/platform-node/NodeCrypto`, +`BunCrypto.layerMac`, `DenoCrypto.layerMac`, or `BrowserCrypto.layerMac` +from the corresponding platform package. + +For other runtimes such as Workers, provide +`Mac.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto))`, +using `effect/crypto/Mac`, `effect/Layer`, and `effect/crypto/SecureRandom`. +Pass the chosen layer to each route layer with `Layer.provide`. + +An invalid signing secret fails layer construction as a defect. A verification +error or malformed signature fails closed with HTTP 401. Requests must still +have a digits-only timestamp within five minutes of the current clock. + +## Known limits + +- A post with files has not been tested in a Slack Connect channel. +- A post with files uploads the files, posts the message, then attaches the files to it. If Slack refuses the attach, the adapter deletes the message. A mention in that message can still notify before the delete, for a message that then disappears. With `ChatMarkdown`'s `unattachedNote`, the reply is posted again without the files, so the mention can notify twice. + +## Links + +- [Website](https://effect.website): documentation, guides, and news. +- [Reference](https://effect.website/docs/v4/api/chat-slack): API documentation for this package. +- [Discord](https://discord.gg/effect-ts): ask questions, share what you're building, and talk to the core team. +- [Community](https://effect.website/community-hub): meetups and events, or bring Effect to your own. +- [Issues](https://github.com/Effect-TS/effect/issues): bug reports and feature requests. + +## Let's talk + +Whether your team is considering Effect, rolling it out, or already running it in production, we'd love to hear from you: what you're building, what works, and what you need from Effect next. + +- **Talk to the maintainers.** Introduce your team on [Discord](https://discord.gg/effect-ts) or email [contact@effectful.co](mailto:contact@effectful.co). We're happy to connect privately on Slack or Discord for feedback and help with adoption. +- **Production support.** We're exploring how to better support teams running Effect in production. If your organization has specific support needs, let's discuss them. +- **Adoption help.** Our [adoption partners](https://effect.website/adoption-partners) offer implementation, consulting, team extension, training, and commercial support. diff --git a/packages/chat/slack/package.json b/packages/chat/slack/package.json new file mode 100644 index 00000000000..8ce38e294a9 --- /dev/null +++ b/packages/chat/slack/package.json @@ -0,0 +1,70 @@ +{ + "name": "@effect/chat-slack", + "version": "4.1.4", + "type": "module", + "license": "MIT", + "description": "The Slack adapter for effect/chat", + "homepage": "https://effect.website", + "repository": { + "type": "git", + "url": "https://github.com/Effect-TS/effect.git", + "directory": "packages/chat/slack" + }, + "bugs": { + "url": "https://github.com/Effect-TS/effect/issues" + }, + "tags": [ + "typescript", + "chat", + "slack" + ], + "keywords": [ + "typescript", + "chat", + "slack" + ], + "sideEffects": [], + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts", + "./*": "./src/*.ts", + "./internal/*": null, + "./index": null, + "./*/index": null + }, + "files": [ + "src/**/*.ts", + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map", + "AGENTS.md", + "CLAUDE.md", + "ai-docs/**/*" + ], + "publishConfig": { + "access": "public", + "provenance": true, + "exports": { + "./package.json": "./package.json", + ".": "./dist/index.js", + "./*": "./dist/*.js", + "./internal/*": null, + "./index": null, + "./*/index": null + } + }, + "scripts": { + "codegen": "effect-utils codegen", + "codegen:emoji": "node scripts/generate-emoji.mjs", + "build": "tsc -b tsconfig.json && pnpm babel", + "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps", + "check": "tsc -b tsconfig.json" + }, + "devDependencies": { + "effect": "workspace:^" + }, + "peerDependencies": { + "effect": "workspace:^" + } +} diff --git a/packages/chat/slack/scripts/generate-emoji.mjs b/packages/chat/slack/scripts/generate-emoji.mjs new file mode 100644 index 00000000000..2b54d0d639c --- /dev/null +++ b/packages/chat/slack/scripts/generate-emoji.mjs @@ -0,0 +1,191 @@ +// Generates src/internal/emojiData.ts from a pinned copy of iamcal/emoji-data. +// +// Usage (from packages/chat/slack): node scripts/generate-emoji.mjs +// +// The source is pinned by commit, byte size and SHA-256 digest. Any mismatch +// aborts the run without touching the output. The output is deterministic, so +// a second run produces no diff. +import { createHash } from "node:crypto" +import { writeFileSync } from "node:fs" + +const PROJECT = "iamcal/emoji-data" +const TAG = "v16.0.0" +const COMMIT = "2771d0b1b3af25c069086e68e38f901c3dda8bdf" +const SOURCE_URL = "https://raw.githubusercontent.com/iamcal/emoji-data/" + COMMIT + "/emoji.json" +const SIZE = 1313457 +const SHA256 = "1d602e65be88772bf8cc368ce16b855d719eeddbafe128d471b80203f494d29f" +const COUNTS = { emoji: 1911, names: 1972, variations: 1875 } + +// Copied verbatim from https://github.com/iamcal/emoji-data/blob//LICENSE +const LICENSE = [ + "The MIT License (MIT)", + "", + "Copyright (c) 2013 Cal Henderson", + "", + "Permission is hereby granted, free of charge, to any person obtaining a copy", + "of this software and associated documentation files (the \"Software\"), to deal", + "in the Software without restriction, including without limitation the rights", + "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell", + "copies of the Software, and to permit persons to whom the Software is", + "furnished to do so, subject to the following conditions:", + "", + "The above copyright notice and this permission notice shall be included in all", + "copies or substantial portions of the Software.", + "", + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR", + "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,", + "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE", + "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER", + "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,", + "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE", + "SOFTWARE." +] + +// Skin tone modifier code point -> Slack "skin-tone-N" digit. +const TONES = { "1F3FB": "2", "1F3FC": "3", "1F3FD": "4", "1F3FE": "5", "1F3FF": "6" } + +const OUTPUT = new URL("../src/internal/emojiData.ts", import.meta.url) + +const fail = (message) => { + throw new Error("generate-emoji: " + message) +} + +const response = await fetch(SOURCE_URL) +if (!response.ok) fail("GET " + SOURCE_URL + " returned " + response.status) +const bytes = new Uint8Array(await response.arrayBuffer()) +if (bytes.byteLength !== SIZE) fail("expected " + SIZE + " bytes, got " + bytes.byteLength) +const digest = createHash("sha256").update(bytes).digest("hex") +if (digest !== SHA256) fail("expected sha256 " + SHA256 + ", got " + digest) + +const emoji = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)) + +const SEQUENCE = /^[0-9A-F]{4,6}(-[0-9A-F]{4,6})*$/ +const NAME = /^[a-z0-9_+-]+$/ + +const sequence = (value) => { + if (typeof value !== "string" || !SEQUENCE.test(value)) fail("bad code point sequence " + JSON.stringify(value)) + return value.toLowerCase() +} + +const withoutVariationSelector = (unified) => + unified.split("-").filter((codePoint) => codePoint !== "fe0f").join("-") + +// "" = none, "!" = the unified sequence without U+FE0F, otherwise the sequence. +const nonQualified = (unified, value) => { + if (value === null || value === undefined) return "" + const encoded = sequence(value) + if (encoded === unified) fail("non_qualified equals unified: " + value) + return encoded === withoutVariationSelector(unified) ? "!" : encoded +} + +const names = new Set() +const sequences = new Set() +const claim = (set, value, what) => { + if (set.has(value)) fail("duplicate " + what + " " + value) + set.add(value) +} + +let nameCount = 0 +let variationCount = 0 +const lines = [] +const sorted = [...emoji].sort((a, b) => (a.unified < b.unified ? -1 : a.unified > b.unified ? 1 : 0)) + +for (const entry of sorted) { + if (!Array.isArray(entry.short_names) || entry.short_names[0] !== entry.short_name) { + fail("short_name is not the first short_names entry for " + entry.unified) + } + for (const name of entry.short_names) { + if (typeof name !== "string" || !NAME.test(name)) fail("bad short name " + JSON.stringify(name)) + claim(names, name, "short name") + nameCount++ + } + const unified = sequence(entry.unified) + claim(sequences, unified, "sequence") + const nq = nonQualified(unified, entry.non_qualified) + if (nq !== "") claim(sequences, nq === "!" ? withoutVariationSelector(unified) : nq, "sequence") + + const fields = [entry.short_names.join(","), unified, nq] + if (entry.skin_variations !== undefined) { + // Variations sharing a template (the variation's unified sequence with each + // skin tone modifier replaced by "*") are stored as one group. + const groups = new Map() + for (const key of Object.keys(entry.skin_variations).sort()) { + const modifiers = key.split("-") + const tones = modifiers.map((codePoint) => TONES[codePoint] ?? fail("unknown skin tone key " + key)) + const variation = entry.skin_variations[key] + const variationUnified = sequence(variation.unified) + claim(sequences, variationUnified, "sequence") + const variationNq = nonQualified(variationUnified, variation.non_qualified) + if (variationNq !== "") { + if (variationNq !== "!") fail("unsupported skin variation non_qualified " + variation.non_qualified) + claim(sequences, withoutVariationSelector(variationUnified), "sequence") + } + let index = 0 + const template = variationUnified.split("-").map((codePoint) => { + if (TONES[codePoint.toUpperCase()] === undefined) return codePoint + if (codePoint.toUpperCase() !== modifiers[index++]) fail("skin tone order mismatch in " + variation.unified) + return "*" + }).join("-") + if (index !== modifiers.length) fail("skin tone count mismatch in " + variation.unified) + const group = template + (variationNq === "" ? "" : ":" + variationNq) + if (!groups.has(group)) groups.set(group, []) + groups.get(group).push(tones.join("")) + variationCount++ + // Round trip: the stored form must reproduce the upstream sequence. + let fill = 0 + const expanded = template.split("-").map((codePoint) => (codePoint === "*" ? modifiers[fill++].toLowerCase() : codePoint)) + .join("-") + if (expanded !== variationUnified) fail("template round trip failed for " + variation.unified) + } + fields.push([...groups].map(([group, keys]) => keys.join(",") + ":" + group).join(";")) + } + lines.push(fields.join("|")) +} + +const counts = { emoji: lines.length, names: nameCount, variations: variationCount } +for (const key of Object.keys(COUNTS)) { + if (counts[key] !== COUNTS[key]) fail("expected " + COUNTS[key] + " " + key + ", got " + counts[key]) +} + +const output = [ + "/**", + " * Slack emoji short names and code point sequences.", + " *", + " * Generated by scripts/generate-emoji.mjs. Do not edit.", + " *", + " * Source:", + " * - Project: " + PROJECT, + " * - Tag: " + TAG, + " * - Commit: " + COMMIT, + " * - URL: " + SOURCE_URL, + " * - SHA-256: " + SHA256, + " * - Size: " + SIZE + " bytes", + " * - License: MIT (reproduced below)", + " *", + " * Format: one emoji per line (" + counts.emoji + " emoji, " + counts.names + " short names, " + counts.variations + + " skin tone variations), sorted by unified:", + " *", + " * short_names|unified|non_qualified[|variations]", + " *", + " * - short_names: comma separated, the first one is short_name", + " * - unified: lowercase hex code points joined by \"-\"", + " * - non_qualified: empty for none, \"!\" for unified without U+FE0F, otherwise", + " * a code point sequence", + " * - variations: \";\" separated groups of skin_variations entries, each", + " * keys:template[:non_qualified]. keys is a comma separated list of upstream", + " * skin_variations keys, written as one Slack skin-tone-N digit (2-6) per", + " * modifier. template is the variation's unified sequence with each modifier", + " * replaced by \"*\", filled in key order. non_qualified is empty or \"!\"", + " *", + ...LICENSE.map((line) => (line === "" ? " *" : " * " + line)), + " *", + " * @internal", + " */", + "", + "/** @internal */", + "export const emojiData = `" + lines.join("\n") + "`", + "" +].join("\n") + +writeFileSync(OUTPUT, output) +console.log("wrote " + OUTPUT.pathname + " (" + Buffer.byteLength(output) + " bytes)") diff --git a/packages/chat/slack/src/SlackChat.ts b/packages/chat/slack/src/SlackChat.ts new file mode 100644 index 00000000000..5dbbcb70f36 --- /dev/null +++ b/packages/chat/slack/src/SlackChat.ts @@ -0,0 +1,620 @@ +/** + * The Slack adapter for `effect/chat`: the `Registry` layer, the Events API + * webhook, the Interactivity route for buttons and modals, the pure + * normalisers, address helpers and the scoped post options. + * + * **Details** + * + * - Bot token scopes: `chat:write`, `channels:history`, `groups:history`, + * `im:history`, `mpim:history`, `channels:read`, `groups:read`, + * `im:read`, `mpim:read`, `users:read`, `reactions:read`, `files:read` + * and `files:write`. + * - Event subscriptions, sent to `layerWebhook`'s path: `message.channels`, + * `message.groups`, `message.im`, `message.mpim`, `app_mention`, + * `reaction_added`, `reaction_removed` and `agent_session_stopped`. + * - Buttons, selects and modals need Interactivity, which is its own + * switch in the app settings, off by default, with its own Request URL, + * separate from Event Subscriptions. Point it at `layerInteractivity`'s + * path. With Socket Mode on, Slack sends interactions over the socket and + * never to the URL: pass each payload to `SlackChat.interaction` and send + * its result as the envelope ack's `payload`. + * - Slash commands need the `commands` scope. Declare each in the app + * manifest under `features.slash_commands`, or in the app settings under + * Slash Commands, with its Request URL set to `layerInteractivity`'s path. + * The name, less its slash, is the id of the `ChatInteraction.command`, + * and the manifest's description and usage hint are what Slack shows. + * With Socket Mode on, commands come as `slash_commands` envelopes: pass + * the envelope's `payload` to `SlackChat.interaction` in the same way. + * That path is from Slack's docs and was not run, nor were commands in + * Slack Connect channels or directs. + * - A command can't be run inside a thread: Slack doesn't offer it there. + * A command that acts on a thread has to be told which one. Command names are shared by every app + * in a workspace. "Escape channels, users, and links" is off by default; + * with it on, `text` carries `<@U…|name>` and `<#C…|name>` tokens. + * - Slack's stop button also needs the app declared as an agent with + * `features.agent_view` in the manifest. Slack documents that declaration + * as irreversible. Without it the adapter's session status calls answer + * `not_authorized`; it then stops making them, and replies still work. + * - A click on the stop button stops nothing by itself: the consumer gets a + * `StopRequested` and ends the reply. Until it does, the user sees a + * disabled button. Every click is a separate `StopRequested`, and only + * the first in a thread lists the open stream in `messageIds`. + * + * @stability unstable + * @since 4.1.5 + */ +import { Registry } from "effect/chat/ChatAdapter" +import { type ChatError, ChatErrorReason } from "effect/chat/ChatError" +import type { ChatEvent } from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ParseMention } from "effect/chat/ChatTest" +import { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import type * as Mac from "effect/crypto/Mac" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import { dual } from "effect/Function" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpRouter from "effect/http/HttpRouter" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import type * as Redacted from "effect/Redacted" +import * as Schema from "effect/Schema" +import * as Adapter from "./internal/adapter.ts" +import * as Address from "./internal/address.ts" +import * as Emoji from "./internal/emoji.ts" +import * as Errors from "./internal/errors.ts" +import * as Interactions from "./internal/interactions.ts" +import * as Mentions from "./internal/mentions.ts" +import * as Normalize from "./internal/normalize.ts" +import { CurrentPostOptions } from "./internal/options.ts" +import * as Webhook from "./internal/webhook.ts" +import { SlackWebApi } from "./SlackWebApi.ts" +import * as WebApi from "./SlackWebApi.ts" + +const isRecord = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** + * A Slack user from `users.info`, with guest and Slack Connect status. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface SlackUser { + readonly id: string + readonly teamId: string + readonly name: string + readonly isBot: boolean + readonly isRestricted: boolean + readonly isUltraRestricted: boolean + /** A Slack Connect user from another organisation. */ + readonly isStranger: boolean + readonly deleted: boolean + readonly raw: Schema.Json +} + +/** + * The Slack side of the adapter, for what the core has no op for. + * + * **Details** + * + * - `botUserId` comes from `auth.test`, read once. A failed read is tried + * again next time. + * - `user` calls `users.info` each time. Nothing is cached. + * - `interaction` is the entry for a decoded Interactivity `payload` + * (`block_actions` or `view_submission`), as `DiscordChat.interaction` + * is for Discord, and for a slash command: the form fields of its + * request, or the `payload` of a `slash_commands` envelope, as one + * object. It normalises with `normalizeInteraction`, calls the + * handler through `ChatInteraction.handle` inside `deadline` (2.5 + * seconds by default, the handler's share of Slack's 3), opens a modal + * with `views.open` and calls `Handler.refused` when it can't. + * - A modal's title is held to 24 code points, each input label to 2,000 and + * the modal's id to 255. These are not on the descriptor. A modal past one + * of them is not opened: `Handler.refused` gets `TooLarge`, and the click + * is still acked. A portable modal keeps its title to 24. + * - Slack needs a submit button on a modal with inputs, so a modal with no + * `submitLabel` gets the English "Submit". Pass `submitLabel` to localise + * it. + * - It succeeds with the body to answer Slack with: the JSON of the 200 on + * the Request URL, or the `payload` of the envelope ack on Socket Mode. + * For a command answered with `respond` that body is the message, with + * `response_type` `ephemeral` or `in_channel`, after the checks of a + * `post`; one that fails them goes to `Handler.refused`. A command's + * answer, `respond` or `followUp`, mentions nobody: `withPostOptions` + * does not reach it, so `<@U…>` goes out escaped and shows as text, as a + * `post` without `withPostOptions` does. There is no way to allow a + * mention in it. + * `undefined` is an empty answer: an ack, a close, a late click, a + * deferred command, a command that opened a modal, a command nobody + * handles, or a payload that is not from an element this adapter built. + * - `Late` is a submit or a command whose handler ran past the deadline + * and was interrupted. It is not an answer, since an empty one would close + * the modal, or tell the user the command worked: `layerInteractivity` + * answers 503 for it, and a caller on Socket Mode sends no ack. For a + * command, Slack's desktop client then shows its own private failure + * notice and keeps the text; its iOS client shows nothing. `Late` is + * itself a JSON value, so test for it with `Schema.is(ChatInteraction.Late)` + * before treating the result as a body. + * - `Chat.followUp` on a deferred command posts once to the command's + * `response_url`, which must be on a Slack host, through the layer's + * `HttpClient`. It is a write and is not retried. Slack takes five posts + * to one `response_url` within 30 minutes, each a new message. A spent + * or expired one answers 404 `used_url` or `expired_url`, which is + * `NotFound` with detail `interaction_expired`. A 410 or a JSON `error` + * with either string is mapped the same, from the docs: neither was seen. + * - A click on a private answer has no `message`. Its `messageId` is the + * answer's `container.message_ts`, which tells which answer was clicked + * and which no Web API method reaches: `chat.update` on it answers + * `message_not_found`, so `edit` fails `NotFound`. `raw` has + * `container.is_ephemeral`. A public answer reads back as a + * `bot_message` whose `bot_id` is not the app's own, so it is not + * `isSelf`. + * - `interaction` checks no signature and no app id: that is the + * transport's job. It needs `ChatInteraction.Handler` where it is + * called, so the layer does not need it. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class SlackChat extends Context.Service + readonly user: (id: string) => Effect.Effect + readonly interaction: (payload: unknown, options?: { + /** The handler's share of Slack's 3 seconds. Default 2.5 seconds. */ + readonly deadline?: Duration.Duration | undefined + }) => Effect.Effect +}>()("@effect/chat-slack/SlackChat") {} + +/** + * Registers the Slack adapter in the `Registry`, for one workspace. + * + * **Details** + * + * - `post` and `edit` send the Markdown as one `markdown` block, in which + * a `<` outside inline and fenced code goes as `<`, plus a `text` the + * adapter builds, in which every `<`, `>` and `&` goes as an entity. + * Slack decides who a message mentions from `text`, so no token notifies + * unless `withPostOptions({ notify })` allows it. Allowed tokens stay + * intact in both. + * - That `text` is cut to 16,000 code points on a post and 3,800 UTF-8 + * bytes on an edit, on a grapheme cluster boundary, ending in `…` with + * the allowed tokens the cut left out appended. The cut never fails. + * - Stream appends go as `markdown_text`, which has no `text` beside it. + * A stream decodes the character references for `<`, `@` and `!`, so + * the adapter finds token shapes (`<@`, and ` => + Layer.effect( + SlackChat, + Effect.gen(function*() { + const api = yield* SlackWebApi + const http = yield* HttpClient.HttpClient + const registry = yield* Registry + let cached: string | undefined + const botUserId: Effect.Effect = Effect.suspend(() => { + if (cached !== undefined) return Effect.succeed(cached) + return api.call("auth.test", {}).pipe( + Effect.mapError(Errors.withOp("botUserId")), + Effect.flatMap((answer) => { + const id = isRecord(answer) ? answer.user_id : undefined + if (typeof id !== "string" || id === "") { + return Effect.fail(Errors.chatError("botUserId", Errors.invalidResponse(false, "missing_user_id"))) + } + cached = id + return Effect.succeed(id) + }) + ) + }) + const { adapter, answer, user } = Adapter.make({ + api, + http, + botToken: options.botToken, + teamId: options.teamId, + botUserId, + attachFailed: (fields) => new SlackAttachFailed(fields) + }) + yield* registry.register(adapter) + const interaction = Interactions.make({ api, teamId: options.teamId, answer }) + return SlackChat.of({ teamId: options.teamId, botUserId, user, interaction }) + }) + ).pipe(Layer.provideMerge(WebApi.layer({ botToken: options.botToken }))) + +/** + * The `cause` of a `post` with files that failed `uncertain` with detail + * `attach_failed_message_left`. + * + * **Details** + * + * - The attach failed and the message could not be deleted. The message may + * still exist, with or without its files: an attach that timed out may + * have landed, and a delete that timed out may have too. `messageId` is + * that message and `fileIds` are its files, which are kept. `reason` is + * the attach's own. + * - Its `message` leaves the ids out. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class SlackAttachFailed + extends Schema.TaggedError("@effect/chat-slack/SlackChat/SlackAttachFailed")( + "SlackAttachFailed", + { + thread: ChatThread, + messageId: Schema.String, + fileIds: Schema.Array(Schema.String), + reason: ChatErrorReason + } + ) +{ + override get message(): string { + return `Slack did not attach the files and the message may be left: ${this.reason.message}` + } +} + +/** + * The address of a Slack channel, direct conversation or thread: + * `{team}/{channel}`, or `{team}/{channel}/{threadTs}` for a thread. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const thread: (parts: { + readonly team: string + readonly channel: string + readonly threadTs?: string | undefined +}) => ChatThread = Address.thread + +/** + * The parts of a Slack address, or `None` for another platform's or a + * malformed one. + * + * @stability unstable + * @category destructors + * @since 4.1.5 + */ +export const parts: (thread: ChatThread) => Option.Option<{ + readonly team: string + readonly channel: string + readonly threadTs?: string | undefined +}> = Address.parts + +/** + * `POST {path}`: the Events API webhook. + * + * **Details** + * + * - Checks the `v0` signature with the signing secret and a five minute + * window, then answers `url_verification`. + * - An envelope for another app, another workspace or of another type is + * answered 200 and dropped. + * - The events go through `normalize` to the `ChatHandler`, one at a time. + * - Answers 503, so Slack retries, when the handler fails `NotAccepted`, + * when the bot user can't be read, or when the request, from reading its + * body to the handler's return, takes longer than `deadline` (2.5 seconds + * by default). Each of the three is logged with its own message, the + * unreadable bot user as an error with its `ChatError` and the other two + * as warnings. Answers 413 past `maxBodyBytes` (1 MiB by default). + * - Slack redelivers on a timeout or an error, so an event can arrive twice. + * `ChatEvent.dedupeKey` gives the two deliveries equal keys. + * + * Signature checks use the required `Mac` service. Provide + * `NodeCrypto.layerMac`, `BunCrypto.layerMac`, `DenoCrypto.layerMac`, + * `BrowserCrypto.layerMac`, or `Mac.layerWebCrypto` with + * `SecureRandom.layerWebCrypto` for other runtimes such as Workers. + * Key construction failures are configuration defects; verification errors + * fail the signature check (401). + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerWebhook = (options: { + readonly path: `/${string}` + readonly signingSecret: Redacted.Redacted + readonly appId: string + readonly deadline?: Duration.Duration | undefined + readonly maxBodyBytes?: number | undefined +}): Layer.Layer => + HttpRouter.use(Effect.fnUntraced(function*(router) { + const slack = yield* SlackChat + const handler = yield* ChatHandler + const verify = yield* Webhook.makeVerifier(options.signingSecret) + const webhookOptions: Webhook.Options = { + verify, + appId: options.appId, + teamId: slack.teamId, + botUserId: slack.botUserId, + handle: handler.handle, + deadline: options.deadline ?? Duration.millis(2500), + maxBodyBytes: options.maxBodyBytes ?? 1024 * 1024 + } + yield* router.add("POST", options.path, (request) => Webhook.handleRequest(request, webhookOptions)) + })) + +/** + * `POST {path}`: the Interactivity Request URL, which Slack posts + * form-encoded and separately from the Events API. + * + * **Details** + * + * - Checks the `v0` signature as `layerWebhook` does, decodes the form's + * `payload` and drops one for another app with a 200. + * - It is also the Request URL of a slash command. A command's form has no + * `payload` field and has a `command` field, which is how the route tells + * the two apart. Its fields go to `SlackChat.interaction` as one object, + * after the same signature and app checks. + * - Hands the payload to `SlackChat.interaction` with `deadline` (2.5 + * seconds by default). Its body is the response, and `Late` is answered + * with a 503, so the modal stays open with Slack's own error. Answers 413 + * past `maxBodyBytes` (1 MiB by default). + * - Slack never redelivers an interaction, so a click or submit that is not + * answered in time is lost. A user can submit again, as a new request. + * + * Signature checks use the required `Mac` service. Provide + * `NodeCrypto.layerMac`, `BunCrypto.layerMac`, `DenoCrypto.layerMac`, + * `BrowserCrypto.layerMac`, or `Mac.layerWebCrypto` with + * `SecureRandom.layerWebCrypto` for other runtimes such as Workers. + * Key construction failures are configuration defects; verification errors + * fail the signature check (401). + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerInteractivity = (options: { + readonly path: `/${string}` + readonly signingSecret: Redacted.Redacted + readonly appId: string + readonly deadline?: Duration.Duration | undefined + readonly maxBodyBytes?: number | undefined +}): Layer.Layer => + HttpRouter.use(Effect.fnUntraced(function*(router) { + const slack = yield* SlackChat + const handler = yield* ChatInteraction.Handler + const verify = yield* Webhook.makeVerifier(options.signingSecret) + const interactivityOptions: Webhook.InteractivityOptions = { + verify, + appId: options.appId, + interaction: (payload, interactionOptions) => + Effect.provideService(slack.interaction(payload, interactionOptions), ChatInteraction.Handler, handler), + deadline: options.deadline ?? Interactions.defaultDeadline, + maxBodyBytes: options.maxBodyBytes ?? 1024 * 1024 + } + yield* router.add("POST", options.path, (request) => Webhook.handleInteractivity(request, interactivityOptions)) + })) + +/** + * The normaliser behind `SlackChat.interaction`. + * + * **Details** + * + * - Pure: takes the decoded `payload` of a `block_actions` or + * `view_submission` request, or the fields of a slash command. + * - None for any other payload, and for one that is not from an element + * this adapter built: a block of the consumer's own, a link button, or a + * view the adapter did not open. + * - Every command gives a `CommandInvoked`, known or not: the registry + * decides that later. `id` is `command` less its slash, `text` is `text` + * or the empty string, and the thread is the channel's. + * - A submit's thread and message are those of the click that opened the + * modal, which the adapter packed into `private_metadata` beside the + * consumer's metadata. A modal a command opened has no message, so its + * submit has no `messageId`. + * - The team in an address is `context.teamId`. `raw` is the payload + * without its verification `token`. A command's holds its `response_url`, + * so it is a credential for 30 minutes. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalizeInteraction: ( + payload: unknown, + context: { readonly teamId: string } +) => Option.Option = Interactions.normalize + +/** + * The normaliser, for Socket Mode or a consumer's own transport. + * + * **Details** + * + * - Pure: takes an `event_callback` envelope, gives zero or more events. + * - An envelope for another workspace gives none. The team in an address is + * the workspace, from the envelope's `team_id` or `authorizations`. + * - `message` and `app_mention` for one message give equal events. An + * `app_mention` that carries `edited` gives none. + * - A message subtype other than `file_share`, `thread_broadcast`, + * `bot_message` and `me_message` gives none. + * - Mentions come from the message's `rich_text` block when it has one, + * and only for elements whose token is in `text`, since Slack decides a + * mention from `text`. Without a `rich_text` block they come from `text` + * outside code. + * - The bot's own messages with a `rich_text` block read back with `text` + * rendered from the blocks as Markdown, whole and with its newlines, as + * the stored `text` the adapter sent may be cut. Everyone else's `text` + * is Slack's field as stored. + * - `agent_session_stopped` gives a `StopRequested`. + * - `raw` is the envelope without its verification `token`. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalize: ( + envelope: unknown, + context: { readonly teamId: string; readonly botUserId: string } +) => ReadonlyArray = Normalize.normalize + +/** + * Slack options for every write inside an effect. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface SlackPostOptions { + /** Which mention tokens may notify. Default: none. */ + readonly notify?: { + readonly users?: ReadonlyArray | undefined + readonly groups?: ReadonlyArray | undefined + readonly broadcast?: boolean | undefined + } | undefined + /** + * Link and media previews. Default false, which sends `unfurl_links` and + * `unfurl_media` both false; true sends both true. + * + * **Details** + * + * - It applies to `post` only: `chat.update` has no unfurl parameter. + * An edit that adds a link gets no preview either way, which a live + * check confirmed, so a post-then-edit stream never unfurls. + * - Slack disables unfurling in streaming messages, so it does nothing on + * a native stream. + */ + readonly unfurl?: boolean | undefined + /** Default true where the workspace supports it. */ + readonly nativeStream?: boolean | undefined + /** For a Connect channel, where the recipient is on another team. */ + readonly recipientTeamId?: string | undefined + /** The agent session status at a normal end of a native stream. Default `active`. */ + readonly sessionStatusAtEnd?: "active" | "processing" | "suspended" | "closed" | undefined +} + +/** + * Scoped Slack options for every write inside `effect`. Nested calls merge, + * the inner one winning field by field. + * + * @stability unstable + * @category combinators + * @since 4.1.5 + */ +export const withPostOptions: { + (options: SlackPostOptions): (effect: Effect.Effect) => Effect.Effect + (effect: Effect.Effect, options: SlackPostOptions): Effect.Effect +} = dual( + 2, + (effect: Effect.Effect, options: SlackPostOptions): Effect.Effect => + Effect.updateService(effect, CurrentPostOptions, (current) => ({ ...current, ...options })) +) + +/** + * A Slack emoji name, with or without colons and with at most one skin tone + * suffix (`+1::skin-tone-4`), as Unicode. `None` for a name the table + * doesn't know, such as a custom emoji, and for a name with two tones. + * + * @stability unstable + * @category emoji + * @since 4.1.5 + */ +export const emojiFromName = (name: string): Option.Option => Option.fromUndefinedOr(Emoji.fromName(name)) + +/** + * An emoji as its Slack name, without colons. + * + * @stability unstable + * @category emoji + * @since 4.1.5 + */ +export const emojiToName = (emoji: string): Option.Option => Option.fromUndefinedOr(Emoji.toName(emoji)) + +/** + * Reads one Slack mention token, such as `<@U123>`, ``, + * `<#C123|name>` or ``, for `ChatTest.checkMessage`, + * `ChatTest.checkEvent` and `ChatTest.conformance`. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const parseMention: ParseMention = (slice) => Option.fromUndefinedOr(Mentions.parseToken(slice)) diff --git a/packages/chat/slack/src/SlackWebApi.ts b/packages/chat/slack/src/SlackWebApi.ts new file mode 100644 index 00000000000..663fb609850 --- /dev/null +++ b/packages/chat/slack/src/SlackWebApi.ts @@ -0,0 +1,136 @@ +/** + * The minimal Slack Web API client the adapter is built on. It is public, so + * a consumer can call a method the adapter has no op for, with the same error + * mapping and the same rule for writes. + * + * @stability unstable + * @since 4.1.5 + */ +import { retrySafe } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import * as Context from "effect/Context" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Errors from "./internal/errors.ts" + +/** + * Options for one Web API call. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface CallOptions { + /** + * The call changes something. It is never retried after the request left, + * and a failure that may have landed is `uncertain`. + */ + readonly write?: boolean | undefined + /** Retry `retry` failures, as `ChatAdapter.retrySafe` does. Default true. */ + readonly retry?: boolean | undefined +} + +/** + * The minimal Web API client, with the bot token. + * + * **Details** + * + * - `call` posts form parameters to `https://slack.com/api/{method}`. A + * string goes as it is, any other value as JSON, and `undefined` is left + * out. It succeeds with the answer once Slack says `ok`. + * - An `ok: false` answer fails with the reason for Slack's error code, which + * becomes the reason's `detail`. `ratelimited` and an HTTP 429 give + * `RateLimited` with the `Retry-After` wait. + * - A write never repeats once the request left: a transport failure, a + * server error or an unreadable answer is `uncertain`. A read retries + * `retry` failures unless `retry: false`. + * - The error's `op` is the method name. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class SlackWebApi extends Context.Service>, + options?: CallOptions | undefined + ) => Effect.Effect +}>()("@effect/chat-slack/SlackWebApi") {} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +const formBody = (body: Readonly>): Record => { + const out: Record = {} + for (const [key, value] of Object.entries(body)) { + if (value === undefined) continue + out[key] = typeof value === "string" ? value : JSON.stringify(value) + } + return out +} + +/** + * Builds the client from an `HttpClient` and a bot token. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const make = Effect.fnUntraced(function*(options: { + readonly botToken: Redacted.Redacted + readonly baseUrl?: string | undefined +}) { + const http = yield* HttpClient.HttpClient + const baseUrl = options.baseUrl ?? "https://slack.com/api" + + const once = (method: string, body: Readonly>, write: boolean) => { + const fail = (reason: Parameters[1]) => Effect.fail(Errors.chatError(method, reason)) + const request = HttpClientRequest.post(`${baseUrl}/${method}`).pipe( + HttpClientRequest.bearerToken(Redacted.value(options.botToken)), + HttpClientRequest.bodyUrlParams(formBody(body)) + ) + return http.execute(request).pipe( + Effect.catch((error) => fail(Errors.transport(write, error.reason._tag, error))), + Effect.flatMap((response) => { + const retryAfter = Errors.parseRetryAfter(response.headers["retry-after"]) + if (response.status < 200 || response.status >= 300) { + return fail(Errors.reasonForStatus(response.status, { write, retryAfter })) + } + return response.json.pipe( + Effect.catch((error) => fail(Errors.invalidResponse(write, "invalid_json", error))), + Effect.flatMap((json): Effect.Effect => { + if (!isObject(json) || typeof json.ok !== "boolean") { + return fail(Errors.invalidResponse(write, "invalid_response")) + } + if (json.ok) return Effect.succeed(json) + const code = typeof json.error === "string" ? json.error : "unknown_error" + return fail(Errors.reasonForCode(code, { write, retryAfter })) + }) + ) + }) + ) + } + + return SlackWebApi.of({ + call: (method, body, callOptions) => { + const call = once(method, body, callOptions?.write === true) + return callOptions?.retry === false ? call : retrySafe(call) + } + }) +}) + +/** + * The client as a layer. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly botToken: Redacted.Redacted + readonly baseUrl?: string | undefined +}): Layer.Layer => Layer.effect(SlackWebApi, make(options)) diff --git a/packages/chat/slack/src/index.ts b/packages/chat/slack/src/index.ts new file mode 100644 index 00000000000..78e24fa7cb8 --- /dev/null +++ b/packages/chat/slack/src/index.ts @@ -0,0 +1,18 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as SlackChat from "./SlackChat.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as SlackWebApi from "./SlackWebApi.ts" diff --git a/packages/chat/slack/src/internal/actions.ts b/packages/chat/slack/src/internal/actions.ts new file mode 100644 index 00000000000..86a3667d1e9 --- /dev/null +++ b/packages/chat/slack/src/internal/actions.ts @@ -0,0 +1,202 @@ +/** + * A message's elements as Slack's `actions` block, and what that block + * costs against the 13,200 cap. + */ +import type { Action } from "effect/chat/ChatUi" +import * as Emoji from "./emoji.ts" + +/** + * The `block_id` of the `actions` block the adapter builds, and so the + * mark of a click on one of its own elements. Five ASCII characters, the + * length of the id Slack generates, which `cost` charges. + * + * @internal + */ +export const blockId = "chat1" + +/** + * The one character the adapter reserves: a button or select id may not hold + * it, a link button's `action_id` starts with it, and it separates the + * parts of a modal's `private_metadata`. + * + * @internal + */ +export const join = "\u241F" + +interface PlainText { + readonly type: "plain_text" + readonly text: string +} + +interface SelectOption { + readonly text: PlainText + readonly value: string +} + +/** @internal */ +export type Element = + | { + readonly type: "button" + readonly action_id: string + readonly text: PlainText + readonly value?: string + readonly url?: string + readonly style?: "primary" | "danger" + } + | { + readonly type: "static_select" + readonly action_id: string + readonly placeholder?: PlainText + readonly options: ReadonlyArray + readonly initial_option?: SelectOption + } + +const plainText = (text: string): PlainText => ({ type: "plain_text", text }) + +/** @internal */ +export const option = (label: string, value: string): SelectOption => ({ text: plainText(label), value }) + +/** + * The elements for `actions`, in order. A button's id is its `action_id` + * and its value its `value`. A link button is a button with a `url`, and an + * `action_id` of the join and its index, which no id of the consumer's can + * be. A select's options carry their values, and `selected` is its + * `initial_option`. + * + * @internal + */ +export const elements = (actions: ReadonlyArray): Array => + actions.map((action, index): Element => { + switch (action._tag) { + case "Button": + return { + type: "button", + action_id: action.id, + text: plainText(action.label), + ...(action.value === undefined ? {} : { value: action.value }), + ...(action.style === "primary" || action.style === "danger" ? { style: action.style } : {}) + } + case "LinkButton": + return { type: "button", action_id: join + index, text: plainText(action.label), url: action.url } + case "Select": { + const selected = action.options.find((option) => option.value === action.selected) + return { + type: "static_select", + action_id: action.id, + ...(action.placeholder === undefined ? {} : { placeholder: plainText(action.placeholder) }), + options: action.options.map((entry) => option(entry.label, entry.value)), + ...(selected === undefined ? {} : { initial_option: option(selected.label, selected.value) }) + } + } + } + }) + +/** + * The `actions` block for a message's elements. + * + * @internal + */ +export const block = (actions: ReadonlyArray) => ({ + type: "actions" as const, + block_id: blockId, + elements: elements(actions) +}) + +/** + * The `text` of a message with elements and no Markdown, before escaping: + * the button labels and the select placeholders, joined by a space. + * + * @internal + */ +export const labels = (actions: ReadonlyArray): string => + actions.flatMap((action) => + action._tag === "Select" ? action.placeholder === undefined ? [] : [action.placeholder] : [action.label] + ).join(" ") + +// Cost + +/** The fixed costs of "What `actionsCost` charges". */ +const costs = { + block: 56, + select: 52, + placeholder: 59, + option: 64, + initialOption: 18, + button: 95, + url: 9, + noValue: 11, + style: 18, + separator: 1 +} as const + +/** One UTF-16 unit as Slack's server writes it in JSON. */ +const unitCost = (code: number): number => + code < 0x20 || code > 0x7e ? 6 : code === 0x2f || code === 0x22 || code === 0x5c ? 2 : 1 + +/** + * A string as Slack's server writes it in JSON: 6 for any unit that is not + * printable ASCII, 2 for `/`, `"` and `\\`, 1 otherwise. An id, a value, + * a url and an option label cost this. + * + * @internal + */ +export const stringCost = (text: string): number => { + let cost = 0 + for (let i = 0; i < text.length; i++) cost += unitCost(text.charCodeAt(i)) + return cost +} + +/** + * A button label or a placeholder: `<`, `>` and `&` are stored as + * entities, and an emoji costs the greater of its `:name:` and its escaped + * form. + * + * @internal + */ +export const labelCost = (text: string): number => { + let cost = 0 + for (let i = 0; i < text.length;) { + const emoji = Emoji.longestAt(text, i) + if (emoji !== undefined) { + cost += Math.max(emoji.name.length + 2, 6 * emoji.length) + i += emoji.length + continue + } + const code = text.charCodeAt(i++) + cost += code === 0x3c || code === 0x3e ? 4 : code === 0x26 ? 5 : unitCost(code) + } + return cost +} + +const optionCost = (option: SelectOption): number => + costs.option + stringCost(option.text.text) + stringCost(option.value) + +const elementCost = (element: Element): number => { + if (element.type === "button") { + let cost = costs.button + labelCost(element.text.text) + stringCost(element.action_id) + + stringCost(element.value ?? "") + if (element.url !== undefined) { + cost += costs.url + stringCost(element.url) - (element.value === undefined ? costs.noValue : 0) + } + return element.style === undefined ? cost : cost + costs.style + } + let cost = costs.select + stringCost(element.action_id) + if (element.placeholder !== undefined) cost += costs.placeholder + labelCost(element.placeholder.text) + for (const option of element.options) cost += optionCost(option) + cost += Math.max(0, element.options.length - 1) * costs.separator + if (element.initial_option !== undefined) cost += costs.initialOption + optionCost(element.initial_option) + return cost +} + +/** + * An upper bound on the `actions` block's JSON as Slack's server writes it, + * which the 13,200 cap charges. 0 for no elements. + * + * @internal + */ +export const cost = (elements: ReadonlyArray): number => { + if (elements.length === 0) return 0 + let total = costs.block + (elements.length - 1) * costs.separator + for (const element of elements) total += elementCost(element) + return total +} diff --git a/packages/chat/slack/src/internal/adapter.ts b/packages/chat/slack/src/internal/adapter.ts new file mode 100644 index 00000000000..c38166e2056 --- /dev/null +++ b/packages/chat/slack/src/internal/adapter.ts @@ -0,0 +1,1031 @@ +import { + type ChatAdapter, + type Descriptor, + type File, + type Posted, + retrySafe, + type ThreadInfo +} from "effect/chat/ChatAdapter" +import { + type ChatError, + type ChatErrorReason, + fileRefused, + interactionExpired, + NotFound, + TooLarge, + Unknown, + Unsupported +} from "effect/chat/ChatError" +import type { Defer, Visibility } from "effect/chat/ChatInteraction" +import type { ChatMessage } from "effect/chat/ChatMessage" +import type { ChatThread } from "effect/chat/ChatThread" +import type { Action } from "effect/chat/ChatUi" +import * as Clock from "effect/Clock" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as Result from "effect/Result" +import type * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import * as Address from "./address.ts" +import * as Errors from "./errors.ts" +import * as Escape from "./escape.ts" +import * as Interactions from "./interactions.ts" +import * as Length from "./length.ts" +import * as Normalize from "./normalize.ts" +import { CurrentPostOptions, type SessionStatus } from "./options.ts" +import * as Text from "./text.ts" + +type Json = Schema.Json +type JsonObject = { readonly [key: string]: Json } + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const string = (object: JsonObject, key: string): string | undefined => { + const value = object[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +const object = (object: JsonObject, key: string): JsonObject | undefined => { + const value = object[key] + return isObject(value) ? value : undefined +} + +/** @internal */ +export interface Api { + readonly call: ( + method: string, + body: Readonly>, + options?: { readonly write?: boolean | undefined; readonly retry?: boolean | undefined } + ) => Effect.Effect +} + +/** @internal */ +export interface SlackUser { + readonly id: string + readonly teamId: string + readonly name: string + readonly isBot: boolean + readonly isRestricted: boolean + readonly isUltraRestricted: boolean + readonly isStranger: boolean + readonly deleted: boolean + readonly raw: Json +} + +/** @internal */ +export const descriptor: Descriptor = { + platform: Address.platform, + capabilities: new Set([ + "edit", + "delete", + "fetchMessage", + "history", + "download", + "files", + "startThread", + "getThread", + "opener", + "getUser", + "nativeStream", + "buttons", + "selects", + "modals", + "modalErrors", + "commands", + "ephemeral" + ]), + measure: Length.measure, + limits: { + messageLength: Length.messageLength, + filesPerMessage: 10, + uploadBytes: 1024 * 1024 * 1024, + minWriteInterval: Duration.seconds(1), + streamLength: Length.messageLength, + streamAge: Duration.minutes(10), + followUpAge: Duration.minutes(30), + ...Interactions.limits + } +} + +const pageSize = 200 + +/** + * The hosts a command's `response_url` may point at. A deferral is data that + * may have crossed a queue, so the adapter posts only to Slack. + */ +const isSlackHost = (host: string): boolean => + host === "slack.com" || host.endsWith(".slack.com") || host === "slack-gov.com" || host.endsWith(".slack-gov.com") + +/** + * What a spent `response_url` answers, as plain text with a 404: `used_url` + * after five posts, `expired_url` after 30 minutes. Recorded in EFF-3802. + * The same strings in a JSON `error`, and a 410, are from Slack's docs and + * were never seen; they stay mapped. + */ +const spentResponseUrl = new Set(["expired_url", "used_url"]) + +/** The `response_type` of a command's answer. */ +const responseType = (visibility: Visibility) => visibility === "ephemeral" ? "ephemeral" : "in_channel" + +/** The bound on each call of a file post from `chat.postMessage` on: the post, each attach and the rollback. */ +const fileCallTimeout = Duration.seconds(30) + +/** + * The bound on the whole attach: every attempt, the waits between them and + * the pacing after a 429. Past it the post is rolled back, however long a + * `Retry-After` asked for. + */ +const attachDeadline = Duration.minutes(1) + +/** Codes on `chat.startStream` that mean the feature is off for this thread. */ +const streamUnsupported = new Set(["channel_type_not_supported", "feature_disabled", "not_allowed_token_type"]) + +/** Floor between two edits to one message: the spec's starting number, provisional until measured. */ +const editIntervalMillis = 3_000 + +/** How long a reply's backoff is kept after its last write. */ +const paceIdleMillis = 15 * 60 * 1000 + +/** A reply's pace after a 429, shared by every message the reply writes. */ +interface Backoff { + intervalMillis: number + nextAt: number + lastWrite: number +} + +interface StreamState { + readonly escape: Escape.State + /** The Markdown the stream took, held tails left out. */ + readonly markdown: string + /** What its start and appends sent. */ + readonly sent: string + readonly stopped: boolean + /** + * False while an append is in flight, and after one failed in a way the + * layer repairs: an append cut off or failed `uncertain` may have landed, + * and the repair edit rewrites the message itself. + */ + readonly settled: boolean + /** Whether the cleanup edit has run or been skipped. */ + readonly cleaned: boolean + /** Whether the session status at the end has been set, for a stream stopped by the user. */ + readonly ended: boolean +} + +/** + * Whether the cleanup edit runs for a stream message that holds an allowed + * token. It does: an edit that keeps a real mention sends no second push + * (EFF-3740). If that ever changes, the skip comes back here. + */ +const cleanupWithAllowedToken = true + +/** + * A stream write with every lone surrogate as U+FFFD, as `toWellFormed` + * gives. Slack blanks a message whose surrogate pair is split across two + * appends. + */ +const wellFormed = (text: string): string => + text.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(? (error: ChatError): boolean => error.reason.detail === code + +const userName = (user: JsonObject): string => { + const profile = object(user, "profile") + return (profile === undefined ? undefined : string(profile, "display_name") ?? string(profile, "real_name")) ?? + string(user, "real_name") ?? string(user, "name") ?? string(user, "id") ?? "" +} + +/** @internal */ +export const make = (options: { + readonly api: Api + readonly http: HttpClient.HttpClient + readonly botToken: Redacted.Redacted + readonly teamId: string + readonly botUserId: Effect.Effect + /** Builds the `SlackAttachFailed` cause, which lives in the public module. */ + readonly attachFailed: (fields: { + readonly thread: ChatThread + readonly messageId: string + readonly fileIds: ReadonlyArray + readonly reason: ChatErrorReason + }) => unknown +}) => { + const { api, teamId } = options + const streams = new Map() + let declaredAgent = true + + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + + /** Refuses elements Slack would refuse, or that the adapter could not tell from a consumer's. */ + const checkActions = (op: string, actions: ReadonlyArray) => { + const refusal = Interactions.checkActions(actions) + return refusal === undefined ? Effect.void : fail(op, refusal) + } + + /** + * The pace of the writes that grow one reply: stream appends, the starts + * of its later parts, and edits. + * + * - Edits to one message are at least `editIntervalMillis` apart. + * - After a 429, the next write waits for the retry-after, and each later + * write waits twice the interval it waited before, for the rest of the + * reply. The backoff is keyed by message and shared: a stream started + * with `continues` takes the backoff of the message it continues, and a + * stop with `end: "done"` drops it. + * - Each attempt reads the pace afresh, so a write retried after a 429 + * waits for it. + */ + const backoffs = new Map() + const nextEdit = new Map() + + const evict = (now: number) => { + if (nextEdit.size >= 64) { for (const [key, at] of nextEdit) if (at <= now) nextEdit.delete(key) } + if (backoffs.size >= 64) { + for (const [key, backoff] of backoffs) if (backoff.lastWrite < now - paceIdleMillis) backoffs.delete(key) + } + } + + const paced = ( + messageId: string, + write: Effect.Effect, + kind: "edit" | "stream" + ): Effect.Effect => + Effect.gen(function*() { + const at = Math.max(backoffs.get(messageId)?.nextAt ?? 0, kind === "edit" ? nextEdit.get(messageId) ?? 0 : 0) + const wait = at - (yield* Clock.currentTimeMillis) + if (wait > 0) yield* Effect.sleep(Duration.millis(wait)) + return yield* write.pipe( + Effect.tap(() => + Effect.map(Clock.currentTimeMillis, (now) => { + evict(now) + const backoff = backoffs.get(messageId) + if (backoff !== undefined) { + backoff.nextAt = now + backoff.intervalMillis + backoff.lastWrite = now + } + if (kind === "edit") nextEdit.set(messageId, now + editIntervalMillis) + }) + ), + Effect.tapError((error) => + error.reason._tag !== "RateLimited" ? Effect.void : Effect.map(Clock.currentTimeMillis, (now) => { + evict(now) + const base = kind === "edit" ? editIntervalMillis : Duration.toMillis(descriptor.limits.minWriteInterval) + const backoff = backoffs.get(messageId) ?? { intervalMillis: base, nextAt: now, lastWrite: now } + backoff.intervalMillis *= 2 + const retryAfter = error.retryAfter + backoff.nextAt = now + (retryAfter === undefined ? backoff.intervalMillis : Duration.toMillis(retryAfter)) + backoff.lastWrite = now + backoffs.set(messageId, backoff) + }) + ) + ) + }) + + const call = ( + op: string, + method: string, + body: Readonly>, + callOptions?: { readonly write?: boolean | undefined; readonly retry?: boolean | undefined } + ) => api.call(method, body, callOptions).pipe(Effect.mapError(Errors.withOp(op))) + + const resolve = (op: string, thread: ChatThread): Effect.Effect => { + const parts = Address.parts(thread) + return Option.isSome(parts) && parts.value.team === teamId + ? Effect.succeed(parts.value) + : fail(op, Errors.invalidRequest("invalid_thread")) + } + + const tsOf = (op: string, write: boolean) => (answer: Json): Effect.Effect => { + const ts = isObject(answer) ? string(answer, "ts") : undefined + return ts === undefined ? fail(op, Errors.invalidResponse(write, "missing_ts")) : Effect.succeed(ts) + } + + /** + * Refuses text past 12,000 code points as sent, which Slack would refuse + * with `msg_too_long`. Slack's second count, with emoji as shortcodes, is + * left to Slack: `measure` only bounds it from above, because entities, + * code and Markdown markers render shorter than they are sent. Its refusal + * maps to `TooLarge` and leaves nothing behind. + */ + const checkSent = (op: string, sent: string): Effect.Effect => + Length.codePoints(sent) > Length.messageLength + ? fail(op, new TooLarge({ subject: "text", detail: "msg_too_long" })) + : Effect.void + + /** + * The body of a post or an update: the Markdown as one `markdown` block, + * and a `text` the adapter builds, cut to the call's budget. Slack decides + * mentions from `text`, and builds one with every token intact when none + * is sent, so `markdown_text` is never used on these two calls. + */ + const messageContent = Effect.fnUntraced( + function*(op: string, body: Text.Body) { + yield* checkSent(op, body.sent) + if (body.text === "" && !Text.hasActions(body)) return yield* fail(op, Errors.invalidRequest("no_text")) + if (body.dropped > 0) yield* Effect.annotateCurrentSpan("slack.text.dropped_tokens", body.dropped) + return { blocks: body.blocks, text: body.text } + } + ) + + const messages = (answer: Json): ReadonlyArray => + isObject(answer) && Array.isArray(answer.messages) ? answer.messages : [] + + const nextCursor = (answer: Json): Option.Option => { + const metadata = isObject(answer) ? object(answer, "response_metadata") : undefined + const cursor = metadata === undefined ? undefined : string(metadata, "next_cursor") + return cursor === undefined ? Option.none() : Option.some(cursor) + } + + /** One message by ts, in or out of a thread, as Slack stores it. */ + const fetchRaw = (op: string, channel: string, ts: string): Effect.Effect => + call(op, "conversations.replies", { channel, ts, latest: ts, oldest: ts, inclusive: true, limit: 2 }).pipe( + Effect.flatMap((answer) => { + const found = messages(answer).find((message) => isObject(message) && message.ts === ts) + return found === undefined ? fail(op, Errors.notFound("message_not_found")) : Effect.succeed(found) + }) + ) + + const fetchMessage = Effect.fnUntraced(function*(op: string, thread: ChatThread, messageId: string) { + const parts = yield* resolve(op, thread) + const botUserId = yield* options.botUserId + const raw = yield* fetchRaw(op, parts.channel, messageId) + const message = Normalize.message(raw, { team: teamId, channel: parts.channel, botUserId }) + return message === undefined ? yield* fail(op, Errors.notFound("not_a_message")) : message + }) + + const sessionStatus = (channel: string, threadTs: string, status: SessionStatus): Effect.Effect => + declaredAgent + ? call("nativeStream.stop", "agents.sessions.setStatus", { channel_id: channel, thread_ts: threadTs, status }, { + write: true, + retry: false + }).pipe( + Effect.tapError((error) => + Effect.sync(() => { + if (hasDetail("not_authorized")(error)) declaredAgent = false + }) + ), + Effect.catch((error) => Effect.logDebug("agents.sessions.setStatus failed", error)), + Effect.asVoid + ) + : Effect.void + + // Files + + /** A `denied` failure of an upload call refused a file. */ + const refused = (error: ChatError): ChatError => + error.reason.retry === "denied" ? Errors.chatError(error.op, Errors.fileRefused(error.reason)) : error + + const stage = (file: File): Effect.Effect<{ readonly id: string; readonly title: string }, ChatError> => + Effect.gen(function*() { + const answer = yield* call("post", "files.getUploadURLExternal", { + filename: file.name, + length: file.content.length + }) + const uploadUrl = isObject(answer) ? string(answer, "upload_url") : undefined + const fileId = isObject(answer) ? string(answer, "file_id") : undefined + if (uploadUrl === undefined || fileId === undefined) { + return yield* fail("post", Errors.invalidResponse(false, "missing_upload_url")) + } + // Staging posts nothing, so every failure here is safe to retry. + const response = yield* options.http.execute( + HttpClientRequest.post(uploadUrl).pipe(HttpClientRequest.bodyUint8Array(file.content, file.contentType)) + ).pipe(Effect.mapError((error) => Errors.chatError("post", Errors.transport(false, "upload_failed", error)))) + if (response.status < 200 || response.status >= 300) { + return yield* fail("post", Errors.reasonForStatus(response.status, { write: false })) + } + return { id: fileId, title: file.name } + }).pipe(Effect.mapError(refused)) + + /** One call of the post, the attach or the rollback, cut off after `fileCallTimeout`. */ + const bounded = (effect: Effect.Effect, onTimeout: ChatErrorReason): Effect.Effect => + Effect.timeoutOrElse(effect, { duration: fileCallTimeout, orElse: () => fail("post", onTimeout) }) + + /** + * Upload, post, then attach. The files are completed with no channel, so + * nothing is visible until `chat.postMessage`, and `chat.update` with + * `file_ids` attaches them. Both calls send the same body, with `text` under + * the update budget, so `text` never changes between them. + * + * From the post on, the route runs uninterruptible, with a timeout on each + * call and `attachDeadline` on the attach as a whole, retries and waits + * included. The attach replaces the file list, so it is retried. When it + * still fails, the message is deleted and then the files, best effort. If + * the delete fails or times out, the message may be left, with or without + * its files, and the post fails `uncertain` with a `SlackAttachFailed` as + * its cause. The files are kept so the ids it carries still mean something. + */ + const postFiles = Effect.fnUntraced(function*( + thread: ChatThread, + parts: Address.Parts, + markdown: string, + files: ReadonlyArray, + actions: ReadonlyArray, + notify: Escape.Notify, + unfurl: boolean + ) { + if ( + files.length > descriptor.limits.filesPerMessage || + files.some((file) => file.content.length > descriptor.limits.uploadBytes) + ) { + return yield* fail("post", new TooLarge({ subject: "files", detail: fileRefused })) + } + const content = Text.body(markdown, notify, Text.updateBudget) + const body = yield* messageContent( + "post", + Text.withActions( + content.text === "" ? Text.placeholder(files.map((file) => file.name), Text.updateBudget) : content, + actions + ) + ) + const staged = yield* Effect.forEach(files, stage) + yield* call("post", "files.completeUploadExternal", { files: staged }).pipe(Effect.mapError(refused)) + const fileIds = staged.map((file) => file.id) + const channel = parts.channel + + return yield* Effect.uninterruptible(Effect.gen(function*() { + const removeFiles = bounded( + Effect.forEach(fileIds, (file) => call("post", "files.delete", { file }, { write: true }).pipe(Effect.ignore), { + concurrency: "unbounded", + discard: true + }), + Errors.transport(true, "timeout") + ).pipe(Effect.ignore) + const ts = yield* bounded( + call("post", "chat.postMessage", { + channel, + thread_ts: parts.threadTs, + ...body, + unfurl_links: unfurl, + unfurl_media: unfurl + }, { write: true }).pipe(Effect.flatMap(tsOf("post", true))), + Errors.transport(true, "timeout") + ).pipe(Effect.tapError(() => removeFiles)) + const posted: Posted = { id: ts, thread, timestamp: Normalize.timestamp(ts) } + + const attached = yield* retrySafe(paced( + ts, + bounded( + call("post", "chat.update", { channel, ts, ...body, file_ids: fileIds }, { write: false, retry: false }), + Errors.transport(false, "timeout") + ), + "edit" + )).pipe( + Effect.timeoutOrElse({ + duration: attachDeadline, + orElse: () => fail("post", Errors.transport(false, "attach_deadline")) + }), + Effect.result + ) + if (Result.isSuccess(attached)) return posted + + const error = attached.failure + yield* Effect.annotateCurrentSpan("slack.attach.failed", error.reason._tag) + const deleted = yield* bounded( + call("post", "chat.delete", { channel, ts }, { write: true }), + Errors.transport(true, "timeout") + ).pipe(Effect.result) + if (Result.isSuccess(deleted)) { + yield* removeFiles + return yield* Effect.fail(refused(error)) + } + return yield* fail( + "post", + new Unknown({ + detail: "attach_failed_message_left", + cause: options.attachFailed({ thread, messageId: ts, fileIds, reason: error.reason }) + }) + ) + })) + }) + + // History + + const page = (op: string, method: string, body: Record) => (cursor: string | undefined) => + call(op, method, { ...body, cursor }).pipe( + Effect.map((answer) => [messages(answer), nextCursor(answer)] as const) + ) + + const history: ChatAdapter["history"] = (thread, { direction, from }) => + Stream.unwrap(Effect.gen(function*() { + const parts = yield* resolve("history", thread) + const botUserId = yield* options.botUserId + const context = { team: teamId, channel: parts.channel, botUserId } + const after = (message: ChatMessage) => + from === undefined || + (direction === "forward" + ? Address.compareTs(message.id, from) > 0 + : Address.compareTs(message.id, from) < 0) + const normalized = (raw: Stream.Stream) => + raw.pipe( + Stream.map((message) => Normalize.message(message, context)), + Stream.filter((message): message is ChatMessage => message !== undefined && after(message)) + ) + const reversed = (stream: Stream.Stream) => + Stream.unwrap(Effect.map(Stream.runCollect(stream), (all) => Stream.fromIterable(Array.from(all).reverse()))) + if (parts.threadTs !== undefined) { + // Oldest first, from the thread's start. Slack puts the opener on every page. + const threadTs = parts.threadTs + const bound = from === undefined ? {} : direction === "forward" ? { oldest: from } : { latest: from } + const replies = page("history", "conversations.replies", { + channel: parts.channel, + ts: threadTs, + limit: pageSize, + ...bound + }) + const stream = normalized( + Stream.paginate(undefined as string | undefined, (cursor) => + Effect.map(replies(cursor), ([found, next]) => + [ + cursor === undefined + ? found + : found.filter((message) => !(isObject(message) && message.ts === threadTs)), + next + ] as const)) + ) + return direction === "forward" ? stream : reversed(stream) + } + // Newest first. A reply broadcast to the channel is the thread's. + const bound = from === undefined ? {} : direction === "forward" ? { oldest: from } : { latest: from } + const stream = normalized( + Stream.paginate( + undefined as string | undefined, + page("history", "conversations.history", { channel: parts.channel, limit: pageSize, ...bound }) + ) + ).pipe(Stream.filter((message) => message.thread.id === thread.id)) + return direction === "backward" ? stream : reversed(stream) + })) + + // Native stream + + /** + * The cleanup edit: once a stream is closed, a message whose appends + * differ from what the block path would send is rewritten once as blocks + * plus `text`, which takes out the zero-width spaces. It carries no + * `actions`, since the stop never does. + * + * It is skipped when the block would pass 12,000 and when the edit fails, + * leaving the characters, failing nothing and saying so on the span. A + * message whose last append is unsettled is left to the layer's repair, + * which rewrites it, so it gets no second edit. + * + * An interrupted cleanup leaves the message marked not cleaned, so the + * stop the layer's finaliser sends again runs it again. + */ + const cleanupEdit = Effect.fnUntraced(function*( + op: string, + channel: string, + messageId: string, + state: StreamState, + notify: Escape.Notify + ) { + let tokens = 0 + const block = Escape.escape(state.markdown, notify, { flush: true, onToken: () => tokens++ }).sent + if (block === state.sent) return + const skip = (reason: string) => Effect.annotateCurrentSpan("slack.stream.cleanup", reason) + if (tokens > 0 && !cleanupWithAllowedToken) return yield* skip("allowed_token") + if (!state.settled) return yield* skip("unsettled") + const body = Text.body(state.markdown, notify, Text.updateBudget) + if (Length.codePoints(body.sent) > Length.messageLength) return yield* skip("too_long") + if (body.dropped > 0) yield* Effect.annotateCurrentSpan("slack.text.dropped_tokens", body.dropped) + const edited = yield* paced( + messageId, + call(op, "chat.update", { channel, ts: messageId, blocks: body.blocks, text: body.text }, { + write: true, + retry: false + }), + "edit" + ).pipe(Effect.result) + yield* Effect.annotateCurrentSpan("slack.stream.cleanup", Result.isSuccess(edited) ? "edited" : "failed") + }) + + /** + * Moves the agent session of a stopped stream off `processing`, once. It is + * marked only after the call, so an interrupted one runs again on the stop + * the layer's finaliser sends. + */ + const endSession = ( + channel: string, + threadTs: string | undefined, + messageId: string, + status: SessionStatus + ): Effect.Effect => + Effect.suspend(() => { + if (threadTs === undefined || streams.get(messageId)?.ended === true) return Effect.void + return Effect.andThen( + sessionStatus(channel, threadTs, status), + Effect.sync(() => { + const state = streams.get(messageId) + if (state !== undefined) streams.set(messageId, { ...state, ended: true }) + }) + ) + }) + + const cleanup = (op: string, channel: string, messageId: string, notify: Escape.Notify): Effect.Effect => + Effect.suspend(() => { + const state = streams.get(messageId) + if (state === undefined || state.cleaned) return Effect.void + return Effect.andThen( + cleanupEdit(op, channel, messageId, state, notify), + Effect.sync(() => void streams.set(messageId, { ...state, cleaned: true })) + ) + }) + + const nativeStream: NonNullable = { + start: Effect.fnUntraced(function*(thread, start) { + const op = "nativeStream.start" + const postOptions = yield* CurrentPostOptions + if (postOptions.nativeStream === false) { + return yield* fail(op, new Unsupported({ capability: "nativeStream", detail: "disabled" })) + } + const parts = yield* resolve(op, thread) + if (parts.threadTs === undefined || start.recipient === undefined || start.replyTo !== undefined) { + return yield* fail(op, new Unsupported({ capability: "nativeStream" })) + } + const escaped = Escape.escapeStream(start.markdown, postOptions.notify ?? {}, { flush: true }) + const sent = wellFormed(escaped.sent) + yield* checkSent(op, sent) + const startCall = call(op, "chat.startStream", { + channel: parts.channel, + thread_ts: parts.threadTs, + recipient_user_id: start.recipient, + recipient_team_id: postOptions.recipientTeamId ?? parts.team, + markdown_text: sent === "" ? undefined : sent + }, { write: true, retry: false }).pipe( + Effect.catchIf( + (error) => error.reason.detail !== undefined && streamUnsupported.has(error.reason.detail), + (error) => + fail( + op, + new Unsupported({ + capability: "nativeStream", + ...(error.reason.detail === undefined ? {} : { detail: error.reason.detail }), + cause: error + }) + ) + ) + ) + // A later part of a reply goes at the reply's pace, and keeps it. + const continues = start.continues + const answer = yield* (continues === undefined ? startCall : paced(continues, startCall, "stream")) + const ts = yield* tsOf(op, true)(answer) + const backoff = continues === undefined ? undefined : backoffs.get(continues) + if (backoff !== undefined) backoffs.set(ts, backoff) + streams.set(ts, { + escape: escaped.state, + markdown: start.markdown, + sent, + stopped: false, + settled: true, + cleaned: false, + ended: false + }) + return { id: ts, thread, timestamp: Normalize.timestamp(ts) } + }), + append: Effect.fnUntraced(function*(thread, messageId, markdown, appendOptions) { + const op = "nativeStream.append" + const parts = yield* resolve(op, thread) + const postOptions = yield* CurrentPostOptions + const state = streams.get(messageId) ?? + { + escape: Escape.initialState, + markdown: "", + sent: "", + stopped: false, + settled: true, + cleaned: false, + ended: false + } + const flush = appendOptions?.flush === true + // A trailing high surrogate waits for its other half. + const input = !flush && /[\ud800-\udbff]$/.test(markdown) ? markdown.slice(0, -1) : markdown + const escaped = Escape.escapeStream(input, postOptions.notify ?? {}, { state: state.escape, flush }) + const held = markdown.length - escaped.consumed + if (escaped.sent === "") return { state: "open" as const, held } + const sent = wellFormed(escaped.sent) + yield* checkSent(op, state.sent + sent) + const unsettle = Effect.sync(() => streams.set(messageId, { ...state, settled: false })) + return yield* paced( + messageId, + Effect.andThen( + unsettle, + call(op, "chat.appendStream", { channel: parts.channel, ts: messageId, markdown_text: sent }, { + write: true, + retry: false + }) + ), + "stream" + ).pipe( + Effect.map(() => { + streams.set(messageId, { + escape: escaped.state, + markdown: state.markdown + input.slice(0, escaped.consumed), + sent: state.sent + sent, + stopped: false, + settled: true, + cleaned: false, + ended: false + }) + return { state: "open" as const, held } + }), + Effect.catchIf( + hasDetail("message_not_in_streaming_state"), + () => + Effect.sync(() => { + // The layer carries on by editing the message, and sends no stop. + streams.delete(messageId) + return { state: "closed" as const, held: 0 } + }) + ), + // Slack closed the stream and the reply ends stopped. The layer sends no stop after this, so the + // cleanup and the session status go here. + Effect.catchIf( + hasDetail("stopped_by_user"), + () => + Effect.sync(() => streams.set(messageId, { ...state, stopped: true })).pipe( + Effect.andThen(cleanup(op, parts.channel, messageId, postOptions.notify ?? {})), + Effect.andThen( + endSession(parts.channel, parts.threadTs, messageId, postOptions.sessionStatusAtEnd ?? "active") + ), + Effect.as({ state: "stopped" as const, held: 0 }) + ) + ), + Effect.tapError((error) => + Effect.sync(() => { + if (error.retry === "denied") streams.set(messageId, state) + }) + ) + ) + }), + stop: Effect.fnUntraced(function*(thread, messageId, stopOptions) { + const op = "nativeStream.stop" + const parts = yield* resolve(op, thread) + const postOptions = yield* CurrentPostOptions + const state = streams.get(messageId) + // A rotating stop keeps the reply's backoff for its next part. + const backoff = backoffs.get(messageId) + if (backoff !== undefined && stopOptions.end === "done") { + for (const [key, value] of backoffs) if (value === backoff) backoffs.delete(key) + } + const status = stopOptions.end === "rotating" ? "processing" : postOptions.sessionStatusAtEnd + const body = { channel: parts.channel, ts: messageId } + const stop = status === undefined || !declaredAgent + ? call(op, "chat.stopStream", body, { write: true, retry: false }) + : call(op, "chat.stopStream", { ...body, session_status: status }, { write: true, retry: false }).pipe( + Effect.catchIf(hasDetail("not_authorized"), () => { + declaredAgent = false + return call(op, "chat.stopStream", body, { write: true, retry: false }) + }) + ) + const answer = yield* stop.pipe( + Effect.map(Option.some), + Effect.catchIf(hasDetail("message_not_in_streaming_state"), () => Effect.succeed(Option.none())) + ) + const message = Option.isSome(answer) && isObject(answer.value) ? object(answer.value, "message") : undefined + const context = message === undefined ? undefined : object(message, "ai_context") + const result = Option.isSome(answer) && isObject(answer.value) ? answer.value.result_status : undefined + const stopped = state?.stopped === true || result === "stopped_by_user" || + (context !== undefined && context.result_status === "stopped_by_user") + // Remember stopped_by_user across interruptions, even if a retry finds no open stream. + const current = streams.get(messageId) + if (stopped && current !== undefined) streams.set(messageId, { ...current, stopped: true }) + yield* cleanup(op, parts.channel, messageId, postOptions.notify ?? {}) + if (stopped && stopOptions.end === "done") { + yield* endSession(parts.channel, parts.threadTs, messageId, postOptions.sessionStatusAtEnd ?? "active") + } + // Retain stream state until cleanup and session completion finish. + streams.delete(messageId) + return { stopped } + }) + } + + // Users + + const user = (id: string): Effect.Effect => + call("user", "users.info", { user: id }).pipe( + Effect.flatMap((answer) => { + const raw = isObject(answer) ? object(answer, "user") : undefined + const userId = raw === undefined ? undefined : string(raw, "id") + if (raw === undefined || userId === undefined) { + return fail("user", Errors.invalidResponse(false, "missing_user")) + } + const userTeam = string(raw, "team_id") ?? teamId + return Effect.succeed({ + id: userId, + teamId: userTeam, + name: userName(raw), + isBot: raw.is_bot === true, + isRestricted: raw.is_restricted === true, + isUltraRestricted: raw.is_ultra_restricted === true, + isStranger: raw.is_stranger === true || (userTeam !== teamId && raw.enterprise_user === undefined), + deleted: raw.deleted === true, + raw + }) + }) + ) + + /** A `bot_message` author is a bot id, which `users.info` answers with `NotFound`. */ + const getUser: ChatAdapter["getUser"] = Effect.fnUntraced(function*(id) { + const botUserId = yield* options.botUserId + const found = yield* user(id).pipe(Effect.mapError(Errors.withOp("getUser"))) + return { id: found.id, name: found.name, isBot: found.isBot, isSelf: found.id === botUserId, raw: found.raw } + }) + + /** + * The body of a command's answer, `Respond` or `followUp`: what a `post` + * of the same content sends with the default post options, after the same + * checks, with its `response_type`. `withPostOptions` does not reach it, + * so it notifies nobody and unfurls nothing. + */ + const answer = Effect.fnUntraced(function*( + op: string, + content: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined }, + visibility: Visibility + ) { + const actions = content.actions ?? [] + yield* checkActions(op, actions) + const body = yield* messageContent(op, Text.body(content.markdown, {}, Text.postBudget, actions)) + return { response_type: responseType(visibility), ...body, unfurl_links: false, unfurl_media: false } + }) + + const responseUrl = (op: string, deferred: Defer): Effect.Effect => { + const raw = deferred.command.raw + const value = isObject(raw) ? string(raw, "response_url") : undefined + const url = value === undefined || !URL.canParse(value) ? undefined : new URL(value) + return url !== undefined && url.protocol === "https:" && isSlackHost(url.hostname) + ? Effect.succeed(url) + : fail(op, Errors.invalidRequest("invalid_response_url")) + } + + /** + * One POST to the command's `response_url`, never retried. A spent URL is + * `NotFound` with detail `interaction_expired`. + */ + const followUp: ChatAdapter["followUp"] = Effect.fnUntraced(function*(deferred, content) { + const op = "followUp" + const url = yield* responseUrl(op, deferred) + const body = yield* answer(op, content, deferred.visibility) + const response = yield* options.http.execute( + HttpClientRequest.post(url).pipe(HttpClientRequest.bodyJsonUnsafe(body)) + ).pipe(Effect.mapError((error) => Errors.chatError(op, Errors.transport(true, error.reason._tag, error)))) + if (response.status >= 200 && response.status < 300) return + const text = yield* Effect.orElseSucceed(response.text, () => "") + let code: string | undefined + try { + const parsed: unknown = JSON.parse(text) + code = isObject(parsed) ? string(parsed, "error") : undefined + } catch { + code = text.trim() === "" ? undefined : text.trim() + } + if (response.status === 404 || response.status === 410 || (code !== undefined && spentResponseUrl.has(code))) { + return yield* fail( + op, + new NotFound({ detail: interactionExpired, ...(code === undefined ? {} : { cause: code }) }) + ) + } + return yield* fail(op, Errors.reasonForStatus(response.status, { write: true, cause: code })) + }) + + const adapter: ChatAdapter = { + descriptor, + nativeStream, + followUp, + post: Effect.fnUntraced(function*(thread, content) { + const parts = yield* resolve("post", thread) + if (content.replyTo !== undefined) return yield* fail("post", new Unsupported({ capability: "replyTo" })) + const actions = content.actions ?? [] + yield* checkActions("post", actions) + const postOptions = yield* CurrentPostOptions + const notify = postOptions.notify ?? {} + const unfurl = postOptions.unfurl === true + if (content.files !== undefined && content.files.length > 0) { + return yield* postFiles(thread, parts, content.markdown, content.files, actions, notify, unfurl) + } + const body = yield* messageContent("post", Text.body(content.markdown, notify, Text.postBudget, actions)) + const ts = yield* call("post", "chat.postMessage", { + channel: parts.channel, + thread_ts: parts.threadTs, + ...body, + unfurl_links: unfurl, + unfurl_media: unfurl + }, { write: true }).pipe(Effect.flatMap(tsOf("post", true))) + return { id: ts, thread, timestamp: Normalize.timestamp(ts) } + }), + edit: Effect.fnUntraced(function*(thread, messageId, content) { + const parts = yield* resolve("edit", thread) + const actions = content.actions ?? [] + yield* checkActions("edit", actions) + const postOptions = yield* CurrentPostOptions + const body = yield* messageContent( + "edit", + Text.body(content.markdown, postOptions.notify ?? {}, Text.updateBudget, actions) + ) + yield* retrySafe(paced( + messageId, + call("edit", "chat.update", { channel: parts.channel, ts: messageId, ...body }, { + write: true, + retry: false + }), + "edit" + )) + }), + delete: Effect.fnUntraced(function*(thread, messageId) { + const parts = yield* resolve("delete", thread) + const body = { channel: parts.channel, ts: messageId } + yield* call("delete", "chat.delete", body, { write: true }).pipe( + // Slack refuses to delete an open stream: stop it, then delete. + Effect.catchIf( + hasDetail("cant_delete_message"), + (error) => + call("delete", "chat.stopStream", body, { write: true, retry: false }).pipe( + Effect.matchEffect({ + onFailure: () => Effect.fail(error), + onSuccess: () => { + streams.delete(messageId) + return call("delete", "chat.delete", body, { write: true }) + } + }) + ) + ) + ) + }), + fetchMessage: (thread, messageId) => fetchMessage("fetchMessage", thread, messageId), + history, + download: (thread, attachment, { maxBytes }) => + Stream.unwrap(Effect.gen(function*() { + const op = "download" + yield* resolve(op, thread) + const tooLarge = Errors.chatError(op, new TooLarge({ subject: "files", detail: "max_bytes" })) + if (attachment.size !== undefined && attachment.size > maxBytes) return yield* Effect.fail(tooLarge) + const answer = yield* call(op, "files.info", { file: attachment.id }) + const file = isObject(answer) ? object(answer, "file") : undefined + const url = file === undefined ? undefined : string(file, "url_private_download") ?? string(file, "url_private") + if (url === undefined) return yield* fail(op, Errors.invalidResponse(false, "missing_url")) + const response = yield* options.http.execute( + HttpClientRequest.get(url).pipe(HttpClientRequest.bearerToken(Redacted.value(options.botToken))) + ).pipe(Effect.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error)))) + if (response.status < 200 || response.status >= 300) { + return yield* fail(op, Errors.reasonForStatus(response.status, { write: false })) + } + let received = 0 + return response.stream.pipe( + Stream.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error))), + Stream.mapEffect((chunk) => { + received += chunk.length + return received > maxBytes ? Effect.fail(tooLarge) : Effect.succeed(chunk) + }) + ) + })), + startThread: (thread, messageId) => + resolve("startThread", thread).pipe( + Effect.map((parts) => + parts.threadTs === undefined + ? Address.thread({ team: parts.team, channel: parts.channel, threadTs: messageId }) + : thread + ) + ), + getThread: Effect.fnUntraced(function*(thread) { + const parts = yield* resolve("getThread", thread) + const answer = yield* call("getThread", "conversations.info", { channel: parts.channel }) + const channel = isObject(answer) ? object(answer, "channel") : undefined + if (channel === undefined) return yield* fail("getThread", Errors.invalidResponse(false, "missing_channel")) + const direct = channel.is_im === true || channel.is_mpim === true + const topic = object(channel, "topic") + const topicValue = topic === undefined ? undefined : string(topic, "value") + const name = direct ? undefined : string(channel, "name") + const created = channel.created + const createdAt = parts.threadTs !== undefined + ? Normalize.timestamp(parts.threadTs) + : typeof created === "number" && Number.isFinite(created) + ? DateTime.makeUnsafe(created * 1000) + : undefined + return { + thread, + kind: parts.threadTs !== undefined ? "thread" : direct ? "direct" : "channel", + ...(parts.threadTs === undefined && name !== undefined ? { name } : {}), + ...(parts.threadTs === undefined && topicValue !== undefined ? { topic: topicValue } : {}), + ...(parts.threadTs === undefined + ? {} + : { parent: Address.thread({ team: parts.team, channel: parts.channel }) }), + ...(createdAt === undefined ? {} : { createdAt }), + raw: channel + } satisfies ThreadInfo + }), + opener: Effect.fnUntraced(function*(thread) { + const parts = yield* resolve("opener", thread) + if (parts.threadTs === undefined) return Option.none() + const channelAddress = Address.thread({ team: parts.team, channel: parts.channel }) + return yield* fetchMessage("opener", channelAddress, parts.threadTs).pipe( + Effect.map(Option.some), + Effect.catchIf((error) => error.reason._tag === "NotFound", () => Effect.succeed(Option.none())) + ) + }), + getUser + } + + return { adapter, user, answer } +} diff --git a/packages/chat/slack/src/internal/address.ts b/packages/chat/slack/src/internal/address.ts new file mode 100644 index 00000000000..0854cb5e0c7 --- /dev/null +++ b/packages/chat/slack/src/internal/address.ts @@ -0,0 +1,50 @@ +import * as ChatThread from "effect/chat/ChatThread" +import * as Option from "effect/Option" + +/** @internal */ +export const platform = "slack" + +/** @internal */ +export interface Parts { + readonly team: string + readonly channel: string + readonly threadTs?: string | undefined +} + +/** @internal */ +export const thread = (parts: Parts): ChatThread.ChatThread => + ChatThread.make( + platform, + parts.threadTs === undefined + ? `${parts.team}/${parts.channel}` + : `${parts.team}/${parts.channel}/${parts.threadTs}` + ) + +/** @internal */ +export const parts = (address: ChatThread.ChatThread): Option.Option => { + if (address.platform !== platform) return Option.none() + const segments = address.id.split("/") + if (segments.length < 2 || segments.length > 3 || segments.some((segment) => segment.length === 0)) { + return Option.none() + } + return Option.some( + segments.length === 2 + ? { team: segments[0], channel: segments[1] } + : { team: segments[0], channel: segments[1], threadTs: segments[2] } + ) +} + +/** @internal */ +export const isDirectChannel = (channel: string): boolean => channel.startsWith("D") + +/** Compares two Slack `ts` values in time order. @internal */ +export const compareTs = (a: string, b: string): number => { + const [aSeconds, aMicros = ""] = a.split(".") + const [bSeconds, bMicros = ""] = b.split(".") + const seconds = Number(aSeconds) - Number(bSeconds) + if (seconds !== 0) return seconds < 0 ? -1 : 1 + const width = Math.max(aMicros.length, bMicros.length) + const left = aMicros.padEnd(width, "0") + const right = bMicros.padEnd(width, "0") + return left < right ? -1 : left > right ? 1 : 0 +} diff --git a/packages/chat/slack/src/internal/emoji.ts b/packages/chat/slack/src/internal/emoji.ts new file mode 100644 index 00000000000..1bf176e542c --- /dev/null +++ b/packages/chat/slack/src/internal/emoji.ts @@ -0,0 +1,223 @@ +import { emojiData } from "./emojiData.ts" + +interface Entry { + readonly unified: string + /** skin_variations keyed by Slack tone digits: "3" or "23" */ + readonly variations: ReadonlyMap +} + +interface Table { + readonly byName: ReadonlyMap + /** Emoji string (unified or non_qualified, toned or not) to Slack name without colons */ + readonly bySequence: ReadonlyMap + /** First code point of a multi code point sequence to its UTF-16 lengths, longest first */ + readonly lengthsByFirst: ReadonlyMap> + /** Cost of a lone surrogate, the highest cost of any single code point emoji */ + readonly loneSurrogateCost: number +} + +const decode = (sequence: string): string => { + const codePoints = sequence.split("-") + let out = "" + for (let i = 0; i < codePoints.length; i++) out += String.fromCodePoint(parseInt(codePoints[i], 16)) + return out +} + +const withoutVariationSelector = (emoji: string): string => emoji.replaceAll("\uFE0F", "") + +const toneSuffix = (tones: string): string => { + let out = "" + for (let i = 0; i < tones.length; i++) out += "::skin-tone-" + tones[i] + return out +} + +// Decoded by hand: optimised String.prototype.codePointAt returned wrong values +// around lone surrogates on Node 26.8 (V8), which broke blocksCost. +const codePointAt = (text: string, index: number): number => { + const high = text.charCodeAt(index) + if (high < 0xd800 || high > 0xdbff || index + 1 >= text.length) return high + const low = text.charCodeAt(index + 1) + return low < 0xdc00 || low > 0xdfff ? high : (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000 +} + +const build = (): Table => { + const byName = new Map() + const bySequence = new Map() + const lengths = new Map>() + let loneSurrogateCost = 0 + + const addSequence = (emoji: string, name: string) => { + bySequence.set(emoji, name) + const first = codePointAt(emoji, 0) + if (emoji.length > (first > 0xffff ? 2 : 1)) { + let set = lengths.get(first) + if (set === undefined) lengths.set(first, set = new Set()) + set.add(emoji.length) + } else { + loneSurrogateCost = Math.max(loneSurrogateCost, name.length + 2) + } + } + + for (const line of emojiData.split("\n")) { + const [names, unifiedHex, nonQualified, variationGroups] = line.split("|") + const shortNames = names.split(",") + const shortName = shortNames[0] + const unified = decode(unifiedHex) + addSequence(unified, shortName) + if (nonQualified !== "") { + addSequence(nonQualified === "!" ? withoutVariationSelector(unified) : decode(nonQualified), shortName) + } + const variations = new Map() + if (variationGroups !== undefined) { + for (const group of variationGroups.split(";")) { + const [keys, template, variationNonQualified] = group.split(":") + for (const tones of keys.split(",")) { + let index = 0 + const variation = decode( + template.replace(/\*/g, () => (0x1f3fb + Number(tones[index++]) - 2).toString(16)) + ) + variations.set(tones, variation) + const name = shortName + toneSuffix(tones) + addSequence(variation, name) + if (variationNonQualified === "!") addSequence(withoutVariationSelector(variation), name) + } + } + } + const entry: Entry = { unified, variations } + for (const name of shortNames) byName.set(name, entry) + } + + const lengthsByFirst = new Map>() + for (const [first, set] of lengths) lengthsByFirst.set(first, [...set].sort((a, b) => b - a)) + + return { byName, bySequence, lengthsByFirst, loneSurrogateCost } +} + +let cached: Table | undefined +const table = (): Table => cached ??= build() + +const SKIN_TONE = /^skin-tone-([2-6])$/ + +/** + * Resolve a Slack emoji name to its fully qualified emoji. + * + * Accepts any alias, with or without surrounding colons, followed by at most + * one `::skin-tone-N` suffix (`+1::skin-tone-3`). The toned emoji is the + * upstream `skin_variations` entry with that one tone, or, for a multi-person + * emoji upstream keys only by two tones (`people_holding_hands`), the entry + * with that tone twice. A tone the emoji does not have, and a name with two + * tones, yield `undefined`: no recording shows what Slack sends for a + * two-tone reaction. + * + * @internal + */ +export const fromName = (name: string): string | undefined => { + const bare = name.length >= 2 && name.startsWith(":") && name.endsWith(":") ? name.slice(1, -1) : name + const parts = bare.split("::") + const entry = table().byName.get(parts[0]) + if (entry === undefined) return undefined + if (parts.length === 1) return entry.unified + if (parts.length !== 2) return undefined + const match = SKIN_TONE.exec(parts[1]) + if (match === null) return undefined + const tone = match[1] + return entry.variations.get(tone) ?? entry.variations.get(tone + tone) +} + +/** + * Resolve an emoji (fully qualified or not, toned or not) to its primary Slack + * name without colons, for example `+1` or `+1::skin-tone-3`. + * + * @internal + */ +export const toName = (emoji: string): string | undefined => table().bySequence.get(emoji) + +/** + * The longest emoji in the table that starts at `index`, with its name and + * its length in UTF-16 units, or `undefined`. + * + * @internal + */ +export const longestAt = ( + text: string, + index: number +): { readonly name: string; readonly length: number } | undefined => { + const { bySequence, lengthsByFirst } = table() + const codePoint = codePointAt(text, index) + for (const length of lengthsByFirst.get(codePoint) ?? []) { + const name = index + length <= text.length ? bySequence.get(text.slice(index, index + length)) : undefined + if (name !== undefined) return { name, length } + } + const width = codePoint > 0xffff ? 2 : 1 + const name = bySequence.get(text.slice(index, index + width)) + return name === undefined ? undefined : { name, length: width } +} + +// Code points that are never an emoji on their own but join or modify emoji +// sequences. Charging them more than one keeps blocksCost subadditive: an +// emoji sequence split anywhere costs at least as much as the whole sequence. +const ZERO_WIDTH_JOINER = 0x200d +const COMBINING_ENCLOSING_KEYCAP = 0x20e3 +const ZERO_WIDTH_JOINER_COST = 10 +const COMBINING_ENCLOSING_KEYCAP_COST = 12 +const REGIONAL_INDICATOR_COST = 6 +// Slack charges a lone hair component 1 (the spec's four override rows), but +// with this split measure 1 would charge seven hair sequences below their +// names: 🧑‍🦱 would split as :adult: + joiner + 🦱 for 18 against Slack's 21. +// 4 is the least that keeps every hair sequence at its name. +const HAIR_COMPONENT_COST = 4 + +const codePointCost = (codePoint: number, loneSurrogateCost: number): number => { + if (codePoint === ZERO_WIDTH_JOINER) return ZERO_WIDTH_JOINER_COST + if (codePoint === COMBINING_ENCLOSING_KEYCAP) return COMBINING_ENCLOSING_KEYCAP_COST + if (codePoint >= 0x1f1e6 && codePoint <= 0x1f1ff) return REGIONAL_INDICATOR_COST + if (codePoint >= 0x1f9b0 && codePoint <= 0x1f9b3) return HAIR_COMPONENT_COST + if (codePoint >= 0xd800 && codePoint <= 0xdfff) return loneSurrogateCost + return 1 +} + +/** + * Measure text the way Slack counts it against the 13,200 character + * `msg_blocks_too_long` limit: every emoji counts as its `:short_name:` form + * (`👍🏽` is `:+1::skin-tone-4:`, 17), everything else as one per code point. + * + * The result is the cheapest split of the text into emoji and single code + * points. For every emoji in the table this is exactly its `:short_name:` + * length, and since the cheapest split of `a + b` is at most the cheapest split + * of `a` followed by that of `b`, `blocksCost(a + b) <= blocksCost(a) + + * blocksCost(b)` for all strings. To keep that true when a split lands inside an + * emoji, code points that only occur inside emoji sequences (zero width + * joiner, keycap, regional indicators, hair components) cost more than one + * when they stand alone, and so does a lone surrogate. + * + * Runs in O(text length × candidate sequence lengths). + * + * @internal + */ +export const blocksCost = (text: string): number => { + const { bySequence, lengthsByFirst, loneSurrogateCost } = table() + const length = text.length + // costs[i] is the cheapest cost of text.slice(0, i), -1 when i is inside a code point + const costs = new Int32Array(length + 1).fill(-1) + costs[0] = 0 + for (let i = 0; i < length; i++) { + const before = costs[i] + if (before < 0) continue + const codePoint = codePointAt(text, i) + const width = codePoint > 0xffff ? 2 : 1 + const single = bySequence.get(text.slice(i, i + width)) + const singleCost = before + (single === undefined ? codePointCost(codePoint, loneSurrogateCost) : single.length + 2) + if (costs[i + width] < 0 || singleCost < costs[i + width]) costs[i + width] = singleCost + const candidates = lengthsByFirst.get(codePoint) + if (candidates === undefined) continue + for (const candidate of candidates) { + const end = i + candidate + if (end > length) continue + const name = bySequence.get(text.slice(i, end)) + if (name === undefined) continue + const cost = before + name.length + 2 + if (costs[end] < 0 || cost < costs[end]) costs[end] = cost + } + } + return costs[length] +} diff --git a/packages/chat/slack/src/internal/emojiData.ts b/packages/chat/slack/src/internal/emojiData.ts new file mode 100644 index 00000000000..6ba13f3eb8a --- /dev/null +++ b/packages/chat/slack/src/internal/emojiData.ts @@ -0,0 +1,1965 @@ +/** + * Slack emoji short names and code point sequences. + * + * Generated by scripts/generate-emoji.mjs. Do not edit. + * + * Source: + * - Project: iamcal/emoji-data + * - Tag: v16.0.0 + * - Commit: 2771d0b1b3af25c069086e68e38f901c3dda8bdf + * - URL: https://raw.githubusercontent.com/iamcal/emoji-data/2771d0b1b3af25c069086e68e38f901c3dda8bdf/emoji.json + * - SHA-256: 1d602e65be88772bf8cc368ce16b855d719eeddbafe128d471b80203f494d29f + * - Size: 1313457 bytes + * - License: MIT (reproduced below) + * + * Format: one emoji per line (1911 emoji, 1972 short names, 1875 skin tone variations), sorted by unified: + * + * short_names|unified|non_qualified[|variations] + * + * - short_names: comma separated, the first one is short_name + * - unified: lowercase hex code points joined by "-" + * - non_qualified: empty for none, "!" for unified without U+FE0F, otherwise + * a code point sequence + * - variations: ";" separated groups of skin_variations entries, each + * keys:template[:non_qualified]. keys is a comma separated list of upstream + * skin_variations keys, written as one Slack skin-tone-N digit (2-6) per + * modifier. template is the variation's unified sequence with each modifier + * replaced by "*", filled in key order. non_qualified is empty or "!" + * + * The MIT License (MIT) + * + * Copyright (c) 2013 Cal Henderson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * @internal + */ + +/** @internal */ +export const emojiData = `hash|0023-fe0f-20e3|! +keycap_star|002a-fe0f-20e3|! +zero|0030-fe0f-20e3|! +one|0031-fe0f-20e3|! +two|0032-fe0f-20e3|! +three|0033-fe0f-20e3|! +four|0034-fe0f-20e3|! +five|0035-fe0f-20e3|! +six|0036-fe0f-20e3|! +seven|0037-fe0f-20e3|! +eight|0038-fe0f-20e3|! +nine|0039-fe0f-20e3|! +copyright|00a9-fe0f|! +registered|00ae-fe0f|! +mahjong|1f004| +black_joker|1f0cf| +a|1f170-fe0f|! +b|1f171-fe0f|! +o2|1f17e-fe0f|! +parking|1f17f-fe0f|! +ab|1f18e| +cl|1f191| +cool|1f192| +free|1f193| +id|1f194| +new|1f195| +ng|1f196| +ok|1f197| +sos|1f198| +up|1f199| +vs|1f19a| +flag-ac|1f1e6-1f1e8| +flag-ad|1f1e6-1f1e9| +flag-ae|1f1e6-1f1ea| +flag-af|1f1e6-1f1eb| +flag-ag|1f1e6-1f1ec| +flag-ai|1f1e6-1f1ee| +flag-al|1f1e6-1f1f1| +flag-am|1f1e6-1f1f2| +flag-ao|1f1e6-1f1f4| +flag-aq|1f1e6-1f1f6| +flag-ar|1f1e6-1f1f7| +flag-as|1f1e6-1f1f8| +flag-at|1f1e6-1f1f9| +flag-au|1f1e6-1f1fa| +flag-aw|1f1e6-1f1fc| +flag-ax|1f1e6-1f1fd| +flag-az|1f1e6-1f1ff| +flag-ba|1f1e7-1f1e6| +flag-bb|1f1e7-1f1e7| +flag-bd|1f1e7-1f1e9| +flag-be|1f1e7-1f1ea| +flag-bf|1f1e7-1f1eb| +flag-bg|1f1e7-1f1ec| +flag-bh|1f1e7-1f1ed| +flag-bi|1f1e7-1f1ee| +flag-bj|1f1e7-1f1ef| +flag-bl|1f1e7-1f1f1| +flag-bm|1f1e7-1f1f2| +flag-bn|1f1e7-1f1f3| +flag-bo|1f1e7-1f1f4| +flag-bq|1f1e7-1f1f6| +flag-br|1f1e7-1f1f7| +flag-bs|1f1e7-1f1f8| +flag-bt|1f1e7-1f1f9| +flag-bv|1f1e7-1f1fb| +flag-bw|1f1e7-1f1fc| +flag-by|1f1e7-1f1fe| +flag-bz|1f1e7-1f1ff| +flag-ca|1f1e8-1f1e6| +flag-cc|1f1e8-1f1e8| +flag-cd|1f1e8-1f1e9| +flag-cf|1f1e8-1f1eb| +flag-cg|1f1e8-1f1ec| +flag-ch|1f1e8-1f1ed| +flag-ci|1f1e8-1f1ee| +flag-ck|1f1e8-1f1f0| +flag-cl|1f1e8-1f1f1| +flag-cm|1f1e8-1f1f2| +cn,flag-cn|1f1e8-1f1f3| +flag-co|1f1e8-1f1f4| +flag-cp|1f1e8-1f1f5| +flag-sark|1f1e8-1f1f6| +flag-cr|1f1e8-1f1f7| +flag-cu|1f1e8-1f1fa| +flag-cv|1f1e8-1f1fb| +flag-cw|1f1e8-1f1fc| +flag-cx|1f1e8-1f1fd| +flag-cy|1f1e8-1f1fe| +flag-cz|1f1e8-1f1ff| +de,flag-de|1f1e9-1f1ea| +flag-dg|1f1e9-1f1ec| +flag-dj|1f1e9-1f1ef| +flag-dk|1f1e9-1f1f0| +flag-dm|1f1e9-1f1f2| +flag-do|1f1e9-1f1f4| +flag-dz|1f1e9-1f1ff| +flag-ea|1f1ea-1f1e6| +flag-ec|1f1ea-1f1e8| +flag-ee|1f1ea-1f1ea| +flag-eg|1f1ea-1f1ec| +flag-eh|1f1ea-1f1ed| +flag-er|1f1ea-1f1f7| +es,flag-es|1f1ea-1f1f8| +flag-et|1f1ea-1f1f9| +flag-eu|1f1ea-1f1fa| +flag-fi|1f1eb-1f1ee| +flag-fj|1f1eb-1f1ef| +flag-fk|1f1eb-1f1f0| +flag-fm|1f1eb-1f1f2| +flag-fo|1f1eb-1f1f4| +fr,flag-fr|1f1eb-1f1f7| +flag-ga|1f1ec-1f1e6| +gb,uk,flag-gb|1f1ec-1f1e7| +flag-gd|1f1ec-1f1e9| +flag-ge|1f1ec-1f1ea| +flag-gf|1f1ec-1f1eb| +flag-gg|1f1ec-1f1ec| +flag-gh|1f1ec-1f1ed| +flag-gi|1f1ec-1f1ee| +flag-gl|1f1ec-1f1f1| +flag-gm|1f1ec-1f1f2| +flag-gn|1f1ec-1f1f3| +flag-gp|1f1ec-1f1f5| +flag-gq|1f1ec-1f1f6| +flag-gr|1f1ec-1f1f7| +flag-gs|1f1ec-1f1f8| +flag-gt|1f1ec-1f1f9| +flag-gu|1f1ec-1f1fa| +flag-gw|1f1ec-1f1fc| +flag-gy|1f1ec-1f1fe| +flag-hk|1f1ed-1f1f0| +flag-hm|1f1ed-1f1f2| +flag-hn|1f1ed-1f1f3| +flag-hr|1f1ed-1f1f7| +flag-ht|1f1ed-1f1f9| +flag-hu|1f1ed-1f1fa| +flag-ic|1f1ee-1f1e8| +flag-id|1f1ee-1f1e9| +flag-ie|1f1ee-1f1ea| +flag-il|1f1ee-1f1f1| +flag-im|1f1ee-1f1f2| +flag-in|1f1ee-1f1f3| +flag-io|1f1ee-1f1f4| +flag-iq|1f1ee-1f1f6| +flag-ir|1f1ee-1f1f7| +flag-is|1f1ee-1f1f8| +it,flag-it|1f1ee-1f1f9| +flag-je|1f1ef-1f1ea| +flag-jm|1f1ef-1f1f2| +flag-jo|1f1ef-1f1f4| +jp,flag-jp|1f1ef-1f1f5| +flag-ke|1f1f0-1f1ea| +flag-kg|1f1f0-1f1ec| +flag-kh|1f1f0-1f1ed| +flag-ki|1f1f0-1f1ee| +flag-km|1f1f0-1f1f2| +flag-kn|1f1f0-1f1f3| +flag-kp|1f1f0-1f1f5| +kr,flag-kr|1f1f0-1f1f7| +flag-kw|1f1f0-1f1fc| +flag-ky|1f1f0-1f1fe| +flag-kz|1f1f0-1f1ff| +flag-la|1f1f1-1f1e6| +flag-lb|1f1f1-1f1e7| +flag-lc|1f1f1-1f1e8| +flag-li|1f1f1-1f1ee| +flag-lk|1f1f1-1f1f0| +flag-lr|1f1f1-1f1f7| +flag-ls|1f1f1-1f1f8| +flag-lt|1f1f1-1f1f9| +flag-lu|1f1f1-1f1fa| +flag-lv|1f1f1-1f1fb| +flag-ly|1f1f1-1f1fe| +flag-ma|1f1f2-1f1e6| +flag-mc|1f1f2-1f1e8| +flag-md|1f1f2-1f1e9| +flag-me|1f1f2-1f1ea| +flag-mf|1f1f2-1f1eb| +flag-mg|1f1f2-1f1ec| +flag-mh|1f1f2-1f1ed| +flag-mk|1f1f2-1f1f0| +flag-ml|1f1f2-1f1f1| +flag-mm|1f1f2-1f1f2| +flag-mn|1f1f2-1f1f3| +flag-mo|1f1f2-1f1f4| +flag-mp|1f1f2-1f1f5| +flag-mq|1f1f2-1f1f6| +flag-mr|1f1f2-1f1f7| +flag-ms|1f1f2-1f1f8| +flag-mt|1f1f2-1f1f9| +flag-mu|1f1f2-1f1fa| +flag-mv|1f1f2-1f1fb| +flag-mw|1f1f2-1f1fc| +flag-mx|1f1f2-1f1fd| +flag-my|1f1f2-1f1fe| +flag-mz|1f1f2-1f1ff| +flag-na|1f1f3-1f1e6| +flag-nc|1f1f3-1f1e8| +flag-ne|1f1f3-1f1ea| +flag-nf|1f1f3-1f1eb| +flag-ng|1f1f3-1f1ec| +flag-ni|1f1f3-1f1ee| +flag-nl|1f1f3-1f1f1| +flag-no|1f1f3-1f1f4| +flag-np|1f1f3-1f1f5| +flag-nr|1f1f3-1f1f7| +flag-nu|1f1f3-1f1fa| +flag-nz|1f1f3-1f1ff| +flag-om|1f1f4-1f1f2| +flag-pa|1f1f5-1f1e6| +flag-pe|1f1f5-1f1ea| +flag-pf|1f1f5-1f1eb| +flag-pg|1f1f5-1f1ec| +flag-ph|1f1f5-1f1ed| +flag-pk|1f1f5-1f1f0| +flag-pl|1f1f5-1f1f1| +flag-pm|1f1f5-1f1f2| +flag-pn|1f1f5-1f1f3| +flag-pr|1f1f5-1f1f7| +flag-ps|1f1f5-1f1f8| +flag-pt|1f1f5-1f1f9| +flag-pw|1f1f5-1f1fc| +flag-py|1f1f5-1f1fe| +flag-qa|1f1f6-1f1e6| +flag-re|1f1f7-1f1ea| +flag-ro|1f1f7-1f1f4| +flag-rs|1f1f7-1f1f8| +ru,flag-ru|1f1f7-1f1fa| +flag-rw|1f1f7-1f1fc| +flag-sa|1f1f8-1f1e6| +flag-sb|1f1f8-1f1e7| +flag-sc|1f1f8-1f1e8| +flag-sd|1f1f8-1f1e9| +flag-se|1f1f8-1f1ea| +flag-sg|1f1f8-1f1ec| +flag-sh|1f1f8-1f1ed| +flag-si|1f1f8-1f1ee| +flag-sj|1f1f8-1f1ef| +flag-sk|1f1f8-1f1f0| +flag-sl|1f1f8-1f1f1| +flag-sm|1f1f8-1f1f2| +flag-sn|1f1f8-1f1f3| +flag-so|1f1f8-1f1f4| +flag-sr|1f1f8-1f1f7| +flag-ss|1f1f8-1f1f8| +flag-st|1f1f8-1f1f9| +flag-sv|1f1f8-1f1fb| +flag-sx|1f1f8-1f1fd| +flag-sy|1f1f8-1f1fe| +flag-sz|1f1f8-1f1ff| +flag-ta|1f1f9-1f1e6| +flag-tc|1f1f9-1f1e8| +flag-td|1f1f9-1f1e9| +flag-tf|1f1f9-1f1eb| +flag-tg|1f1f9-1f1ec| +flag-th|1f1f9-1f1ed| +flag-tj|1f1f9-1f1ef| +flag-tk|1f1f9-1f1f0| +flag-tl|1f1f9-1f1f1| +flag-tm|1f1f9-1f1f2| +flag-tn|1f1f9-1f1f3| +flag-to|1f1f9-1f1f4| +flag-tr|1f1f9-1f1f7| +flag-tt|1f1f9-1f1f9| +flag-tv|1f1f9-1f1fb| +flag-tw|1f1f9-1f1fc| +flag-tz|1f1f9-1f1ff| +flag-ua|1f1fa-1f1e6| +flag-ug|1f1fa-1f1ec| +flag-um|1f1fa-1f1f2| +flag-un|1f1fa-1f1f3| +us,flag-us|1f1fa-1f1f8| +flag-uy|1f1fa-1f1fe| +flag-uz|1f1fa-1f1ff| +flag-va|1f1fb-1f1e6| +flag-vc|1f1fb-1f1e8| +flag-ve|1f1fb-1f1ea| +flag-vg|1f1fb-1f1ec| +flag-vi|1f1fb-1f1ee| +flag-vn|1f1fb-1f1f3| +flag-vu|1f1fb-1f1fa| +flag-wf|1f1fc-1f1eb| +flag-ws|1f1fc-1f1f8| +flag-xk|1f1fd-1f1f0| +flag-ye|1f1fe-1f1ea| +flag-yt|1f1fe-1f1f9| +flag-za|1f1ff-1f1e6| +flag-zm|1f1ff-1f1f2| +flag-zw|1f1ff-1f1fc| +koko|1f201| +sa|1f202-fe0f|! +u7121|1f21a| +u6307|1f22f| +u7981|1f232| +u7a7a|1f233| +u5408|1f234| +u6e80|1f235| +u6709|1f236| +u6708|1f237-fe0f|! +u7533|1f238| +u5272|1f239| +u55b6|1f23a| +ideograph_advantage|1f250| +accept|1f251| +cyclone|1f300| +foggy|1f301| +closed_umbrella|1f302| +night_with_stars|1f303| +sunrise_over_mountains|1f304| +sunrise|1f305| +city_sunset|1f306| +city_sunrise|1f307| +rainbow|1f308| +bridge_at_night|1f309| +ocean|1f30a| +volcano|1f30b| +milky_way|1f30c| +earth_africa|1f30d| +earth_americas|1f30e| +earth_asia|1f30f| +globe_with_meridians|1f310| +new_moon|1f311| +waxing_crescent_moon|1f312| +first_quarter_moon|1f313| +moon,waxing_gibbous_moon|1f314| +full_moon|1f315| +waning_gibbous_moon|1f316| +last_quarter_moon|1f317| +waning_crescent_moon|1f318| +crescent_moon|1f319| +new_moon_with_face|1f31a| +first_quarter_moon_with_face|1f31b| +last_quarter_moon_with_face|1f31c| +full_moon_with_face|1f31d| +sun_with_face|1f31e| +star2|1f31f| +stars|1f320| +thermometer|1f321-fe0f|! +mostly_sunny,sun_small_cloud|1f324-fe0f|! +barely_sunny,sun_behind_cloud|1f325-fe0f|! +partly_sunny_rain,sun_behind_rain_cloud|1f326-fe0f|! +rain_cloud|1f327-fe0f|! +snow_cloud|1f328-fe0f|! +lightning,lightning_cloud|1f329-fe0f|! +tornado,tornado_cloud|1f32a-fe0f|! +fog|1f32b-fe0f|! +wind_blowing_face|1f32c-fe0f|! +hotdog|1f32d| +taco|1f32e| +burrito|1f32f| +chestnut|1f330| +seedling|1f331| +evergreen_tree|1f332| +deciduous_tree|1f333| +palm_tree|1f334| +cactus|1f335| +hot_pepper|1f336-fe0f|! +tulip|1f337| +cherry_blossom|1f338| +rose|1f339| +hibiscus|1f33a| +sunflower|1f33b| +blossom|1f33c| +corn|1f33d| +ear_of_rice|1f33e| +herb|1f33f| +four_leaf_clover|1f340| +maple_leaf|1f341| +fallen_leaf|1f342| +leaves|1f343| +mushroom|1f344| +brown_mushroom|1f344-200d-1f7eb| +tomato|1f345| +eggplant|1f346| +grapes|1f347| +melon|1f348| +watermelon|1f349| +tangerine|1f34a| +lemon|1f34b| +lime|1f34b-200d-1f7e9| +banana|1f34c| +pineapple|1f34d| +apple|1f34e| +green_apple|1f34f| +pear|1f350| +peach|1f351| +cherries|1f352| +strawberry|1f353| +hamburger|1f354| +pizza|1f355| +meat_on_bone|1f356| +poultry_leg|1f357| +rice_cracker|1f358| +rice_ball|1f359| +rice|1f35a| +curry|1f35b| +ramen|1f35c| +spaghetti|1f35d| +bread|1f35e| +fries|1f35f| +sweet_potato|1f360| +dango|1f361| +oden|1f362| +sushi|1f363| +fried_shrimp|1f364| +fish_cake|1f365| +icecream|1f366| +shaved_ice|1f367| +ice_cream|1f368| +doughnut|1f369| +cookie|1f36a| +chocolate_bar|1f36b| +candy|1f36c| +lollipop|1f36d| +custard|1f36e| +honey_pot|1f36f| +cake|1f370| +bento|1f371| +stew|1f372| +fried_egg,cooking|1f373| +fork_and_knife|1f374| +tea|1f375| +sake|1f376| +wine_glass|1f377| +cocktail|1f378| +tropical_drink|1f379| +beer|1f37a| +beers|1f37b| +baby_bottle|1f37c| +knife_fork_plate|1f37d-fe0f|! +champagne|1f37e| +popcorn|1f37f| +ribbon|1f380| +gift|1f381| +birthday|1f382| +jack_o_lantern|1f383| +christmas_tree|1f384| +santa|1f385||2,3,4,5,6:1f385-* +fireworks|1f386| +sparkler|1f387| +balloon|1f388| +tada|1f389| +confetti_ball|1f38a| +tanabata_tree|1f38b| +crossed_flags|1f38c| +bamboo|1f38d| +dolls|1f38e| +flags|1f38f| +wind_chime|1f390| +rice_scene|1f391| +school_satchel|1f392| +mortar_board|1f393| +medal|1f396-fe0f|! +reminder_ribbon|1f397-fe0f|! +studio_microphone|1f399-fe0f|! +level_slider|1f39a-fe0f|! +control_knobs|1f39b-fe0f|! +film_frames|1f39e-fe0f|! +admission_tickets|1f39f-fe0f|! +carousel_horse|1f3a0| +ferris_wheel|1f3a1| +roller_coaster|1f3a2| +fishing_pole_and_fish|1f3a3| +microphone|1f3a4| +movie_camera|1f3a5| +cinema|1f3a6| +headphones|1f3a7| +art|1f3a8| +tophat|1f3a9| +circus_tent|1f3aa| +ticket|1f3ab| +clapper|1f3ac| +performing_arts|1f3ad| +video_game|1f3ae| +dart|1f3af| +slot_machine|1f3b0| +8ball|1f3b1| +game_die|1f3b2| +bowling|1f3b3| +flower_playing_cards|1f3b4| +musical_note|1f3b5| +notes|1f3b6| +saxophone|1f3b7| +guitar|1f3b8| +musical_keyboard|1f3b9| +trumpet|1f3ba| +violin|1f3bb| +musical_score|1f3bc| +running_shirt_with_sash|1f3bd| +tennis|1f3be| +ski|1f3bf| +basketball|1f3c0| +checkered_flag|1f3c1| +snowboarder|1f3c2||2,3,4,5,6:1f3c2-* +runner,running|1f3c3||2,3,4,5,6:1f3c3-* +woman-running|1f3c3-200d-2640-fe0f|!|2,3,4,5,6:1f3c3-*-200d-2640-fe0f:! +woman_running_facing_right|1f3c3-200d-2640-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f3c3-*-200d-2640-fe0f-200d-27a1-fe0f:! +man-running|1f3c3-200d-2642-fe0f|!|2,3,4,5,6:1f3c3-*-200d-2642-fe0f:! +man_running_facing_right|1f3c3-200d-2642-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f3c3-*-200d-2642-fe0f-200d-27a1-fe0f:! +person_running_facing_right|1f3c3-200d-27a1-fe0f|!|2,3,4,5,6:1f3c3-*-200d-27a1-fe0f:! +surfer|1f3c4||2,3,4,5,6:1f3c4-* +woman-surfing|1f3c4-200d-2640-fe0f|!|2,3,4,5,6:1f3c4-*-200d-2640-fe0f:! +man-surfing|1f3c4-200d-2642-fe0f|!|2,3,4,5,6:1f3c4-*-200d-2642-fe0f:! +sports_medal|1f3c5| +trophy|1f3c6| +horse_racing|1f3c7||2,3,4,5,6:1f3c7-* +football|1f3c8| +rugby_football|1f3c9| +swimmer|1f3ca||2,3,4,5,6:1f3ca-* +woman-swimming|1f3ca-200d-2640-fe0f|!|2,3,4,5,6:1f3ca-*-200d-2640-fe0f:! +man-swimming|1f3ca-200d-2642-fe0f|!|2,3,4,5,6:1f3ca-*-200d-2642-fe0f:! +weight_lifter|1f3cb-fe0f|!|2,3,4,5,6:1f3cb-* +woman-lifting-weights|1f3cb-fe0f-200d-2640-fe0f|!|2,3,4,5,6:1f3cb-*-200d-2640-fe0f:! +man-lifting-weights|1f3cb-fe0f-200d-2642-fe0f|!|2,3,4,5,6:1f3cb-*-200d-2642-fe0f:! +golfer|1f3cc-fe0f|!|2,3,4,5,6:1f3cc-* +woman-golfing|1f3cc-fe0f-200d-2640-fe0f|!|2,3,4,5,6:1f3cc-*-200d-2640-fe0f:! +man-golfing|1f3cc-fe0f-200d-2642-fe0f|!|2,3,4,5,6:1f3cc-*-200d-2642-fe0f:! +racing_motorcycle|1f3cd-fe0f|! +racing_car|1f3ce-fe0f|! +cricket_bat_and_ball|1f3cf| +volleyball|1f3d0| +field_hockey_stick_and_ball|1f3d1| +ice_hockey_stick_and_puck|1f3d2| +table_tennis_paddle_and_ball|1f3d3| +snow_capped_mountain|1f3d4-fe0f|! +camping|1f3d5-fe0f|! +beach_with_umbrella|1f3d6-fe0f|! +building_construction|1f3d7-fe0f|! +house_buildings|1f3d8-fe0f|! +cityscape|1f3d9-fe0f|! +derelict_house_building|1f3da-fe0f|! +classical_building|1f3db-fe0f|! +desert|1f3dc-fe0f|! +desert_island|1f3dd-fe0f|! +national_park|1f3de-fe0f|! +stadium|1f3df-fe0f|! +house|1f3e0| +house_with_garden|1f3e1| +office|1f3e2| +post_office|1f3e3| +european_post_office|1f3e4| +hospital|1f3e5| +bank|1f3e6| +atm|1f3e7| +hotel|1f3e8| +love_hotel|1f3e9| +convenience_store|1f3ea| +school|1f3eb| +department_store|1f3ec| +factory|1f3ed| +izakaya_lantern,lantern|1f3ee| +japanese_castle|1f3ef| +european_castle|1f3f0| +waving_white_flag|1f3f3-fe0f|! +rainbow-flag|1f3f3-fe0f-200d-1f308|! +transgender_flag|1f3f3-fe0f-200d-26a7-fe0f|! +waving_black_flag|1f3f4| +pirate_flag|1f3f4-200d-2620-fe0f|! +flag-england|1f3f4-e0067-e0062-e0065-e006e-e0067-e007f| +flag-scotland|1f3f4-e0067-e0062-e0073-e0063-e0074-e007f| +flag-wales|1f3f4-e0067-e0062-e0077-e006c-e0073-e007f| +rosette|1f3f5-fe0f|! +label|1f3f7-fe0f|! +badminton_racquet_and_shuttlecock|1f3f8| +bow_and_arrow|1f3f9| +amphora|1f3fa| +skin-tone-2|1f3fb| +skin-tone-3|1f3fc| +skin-tone-4|1f3fd| +skin-tone-5|1f3fe| +skin-tone-6|1f3ff| +rat|1f400| +mouse2|1f401| +ox|1f402| +water_buffalo|1f403| +cow2|1f404| +tiger2|1f405| +leopard|1f406| +rabbit2|1f407| +cat2|1f408| +black_cat|1f408-200d-2b1b| +dragon|1f409| +crocodile|1f40a| +whale2|1f40b| +snail|1f40c| +snake|1f40d| +racehorse|1f40e| +ram|1f40f| +goat|1f410| +sheep|1f411| +monkey|1f412| +rooster|1f413| +chicken|1f414| +dog2|1f415| +service_dog|1f415-200d-1f9ba| +pig2|1f416| +boar|1f417| +elephant|1f418| +octopus|1f419| +shell|1f41a| +bug|1f41b| +ant|1f41c| +bee,honeybee|1f41d| +ladybug,lady_beetle|1f41e| +fish|1f41f| +tropical_fish|1f420| +blowfish|1f421| +turtle|1f422| +hatching_chick|1f423| +baby_chick|1f424| +hatched_chick|1f425| +bird|1f426| +phoenix|1f426-200d-1f525| +black_bird|1f426-200d-2b1b| +penguin|1f427| +koala|1f428| +poodle|1f429| +dromedary_camel|1f42a| +camel|1f42b| +dolphin,flipper|1f42c| +mouse|1f42d| +cow|1f42e| +tiger|1f42f| +rabbit|1f430| +cat|1f431| +dragon_face|1f432| +whale|1f433| +horse|1f434| +monkey_face|1f435| +dog|1f436| +pig|1f437| +frog|1f438| +hamster|1f439| +wolf|1f43a| +bear|1f43b| +polar_bear|1f43b-200d-2744-fe0f|! +panda_face|1f43c| +pig_nose|1f43d| +feet,paw_prints|1f43e| +chipmunk|1f43f-fe0f|! +eyes|1f440| +eye|1f441-fe0f|! +eye-in-speech-bubble|1f441-fe0f-200d-1f5e8-fe0f|! +ear|1f442||2,3,4,5,6:1f442-* +nose|1f443||2,3,4,5,6:1f443-* +lips|1f444| +tongue|1f445| +point_up_2|1f446||2,3,4,5,6:1f446-* +point_down|1f447||2,3,4,5,6:1f447-* +point_left|1f448||2,3,4,5,6:1f448-* +point_right|1f449||2,3,4,5,6:1f449-* +facepunch,punch|1f44a||2,3,4,5,6:1f44a-* +wave|1f44b||2,3,4,5,6:1f44b-* +ok_hand|1f44c||2,3,4,5,6:1f44c-* ++1,thumbsup|1f44d||2,3,4,5,6:1f44d-* +-1,thumbsdown|1f44e||2,3,4,5,6:1f44e-* +clap|1f44f||2,3,4,5,6:1f44f-* +open_hands|1f450||2,3,4,5,6:1f450-* +crown|1f451| +womans_hat|1f452| +eyeglasses|1f453| +necktie|1f454| +shirt,tshirt|1f455| +jeans|1f456| +dress|1f457| +kimono|1f458| +bikini|1f459| +womans_clothes|1f45a| +purse|1f45b| +handbag|1f45c| +pouch|1f45d| +mans_shoe,shoe|1f45e| +athletic_shoe|1f45f| +high_heel|1f460| +sandal|1f461| +boot|1f462| +footprints|1f463| +bust_in_silhouette|1f464| +busts_in_silhouette|1f465| +boy|1f466||2,3,4,5,6:1f466-* +girl|1f467||2,3,4,5,6:1f467-* +man|1f468||2,3,4,5,6:1f468-* +male-farmer|1f468-200d-1f33e||2,3,4,5,6:1f468-*-200d-1f33e +male-cook|1f468-200d-1f373||2,3,4,5,6:1f468-*-200d-1f373 +man_feeding_baby|1f468-200d-1f37c||2,3,4,5,6:1f468-*-200d-1f37c +male-student|1f468-200d-1f393||2,3,4,5,6:1f468-*-200d-1f393 +male-singer|1f468-200d-1f3a4||2,3,4,5,6:1f468-*-200d-1f3a4 +male-artist|1f468-200d-1f3a8||2,3,4,5,6:1f468-*-200d-1f3a8 +male-teacher|1f468-200d-1f3eb||2,3,4,5,6:1f468-*-200d-1f3eb +male-factory-worker|1f468-200d-1f3ed||2,3,4,5,6:1f468-*-200d-1f3ed +man-boy|1f468-200d-1f466| +man-boy-boy|1f468-200d-1f466-200d-1f466| +man-girl|1f468-200d-1f467| +man-girl-boy|1f468-200d-1f467-200d-1f466| +man-girl-girl|1f468-200d-1f467-200d-1f467| +man-man-boy|1f468-200d-1f468-200d-1f466| +man-man-boy-boy|1f468-200d-1f468-200d-1f466-200d-1f466| +man-man-girl|1f468-200d-1f468-200d-1f467| +man-man-girl-boy|1f468-200d-1f468-200d-1f467-200d-1f466| +man-man-girl-girl|1f468-200d-1f468-200d-1f467-200d-1f467| +man-woman-boy|1f468-200d-1f469-200d-1f466| +man-woman-boy-boy|1f468-200d-1f469-200d-1f466-200d-1f466| +man-woman-girl|1f468-200d-1f469-200d-1f467| +man-woman-girl-boy|1f468-200d-1f469-200d-1f467-200d-1f466| +man-woman-girl-girl|1f468-200d-1f469-200d-1f467-200d-1f467| +male-technologist|1f468-200d-1f4bb||2,3,4,5,6:1f468-*-200d-1f4bb +male-office-worker|1f468-200d-1f4bc||2,3,4,5,6:1f468-*-200d-1f4bc +male-mechanic|1f468-200d-1f527||2,3,4,5,6:1f468-*-200d-1f527 +male-scientist|1f468-200d-1f52c||2,3,4,5,6:1f468-*-200d-1f52c +male-astronaut|1f468-200d-1f680||2,3,4,5,6:1f468-*-200d-1f680 +male-firefighter|1f468-200d-1f692||2,3,4,5,6:1f468-*-200d-1f692 +man_with_probing_cane|1f468-200d-1f9af||2,3,4,5,6:1f468-*-200d-1f9af +man_with_white_cane_facing_right|1f468-200d-1f9af-200d-27a1-fe0f|!|2,3,4,5,6:1f468-*-200d-1f9af-200d-27a1-fe0f:! +red_haired_man|1f468-200d-1f9b0||2,3,4,5,6:1f468-*-200d-1f9b0 +curly_haired_man|1f468-200d-1f9b1||2,3,4,5,6:1f468-*-200d-1f9b1 +bald_man|1f468-200d-1f9b2||2,3,4,5,6:1f468-*-200d-1f9b2 +white_haired_man|1f468-200d-1f9b3||2,3,4,5,6:1f468-*-200d-1f9b3 +man_in_motorized_wheelchair|1f468-200d-1f9bc||2,3,4,5,6:1f468-*-200d-1f9bc +man_in_motorized_wheelchair_facing_right|1f468-200d-1f9bc-200d-27a1-fe0f|!|2,3,4,5,6:1f468-*-200d-1f9bc-200d-27a1-fe0f:! +man_in_manual_wheelchair|1f468-200d-1f9bd||2,3,4,5,6:1f468-*-200d-1f9bd +man_in_manual_wheelchair_facing_right|1f468-200d-1f9bd-200d-27a1-fe0f|!|2,3,4,5,6:1f468-*-200d-1f9bd-200d-27a1-fe0f:! +male-doctor|1f468-200d-2695-fe0f|!|2,3,4,5,6:1f468-*-200d-2695-fe0f:! +male-judge|1f468-200d-2696-fe0f|!|2,3,4,5,6:1f468-*-200d-2696-fe0f:! +male-pilot|1f468-200d-2708-fe0f|!|2,3,4,5,6:1f468-*-200d-2708-fe0f:! +man-heart-man|1f468-200d-2764-fe0f-200d-1f468|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f468-*-200d-2764-fe0f-200d-1f468-*:! +man-kiss-man|1f468-200d-2764-fe0f-200d-1f48b-200d-1f468|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f468-*-200d-2764-fe0f-200d-1f48b-200d-1f468-*:! +woman|1f469||2,3,4,5,6:1f469-* +female-farmer|1f469-200d-1f33e||2,3,4,5,6:1f469-*-200d-1f33e +female-cook|1f469-200d-1f373||2,3,4,5,6:1f469-*-200d-1f373 +woman_feeding_baby|1f469-200d-1f37c||2,3,4,5,6:1f469-*-200d-1f37c +female-student|1f469-200d-1f393||2,3,4,5,6:1f469-*-200d-1f393 +female-singer|1f469-200d-1f3a4||2,3,4,5,6:1f469-*-200d-1f3a4 +female-artist|1f469-200d-1f3a8||2,3,4,5,6:1f469-*-200d-1f3a8 +female-teacher|1f469-200d-1f3eb||2,3,4,5,6:1f469-*-200d-1f3eb +female-factory-worker|1f469-200d-1f3ed||2,3,4,5,6:1f469-*-200d-1f3ed +woman-boy|1f469-200d-1f466| +woman-boy-boy|1f469-200d-1f466-200d-1f466| +woman-girl|1f469-200d-1f467| +woman-girl-boy|1f469-200d-1f467-200d-1f466| +woman-girl-girl|1f469-200d-1f467-200d-1f467| +woman-woman-boy|1f469-200d-1f469-200d-1f466| +woman-woman-boy-boy|1f469-200d-1f469-200d-1f466-200d-1f466| +woman-woman-girl|1f469-200d-1f469-200d-1f467| +woman-woman-girl-boy|1f469-200d-1f469-200d-1f467-200d-1f466| +woman-woman-girl-girl|1f469-200d-1f469-200d-1f467-200d-1f467| +female-technologist|1f469-200d-1f4bb||2,3,4,5,6:1f469-*-200d-1f4bb +female-office-worker|1f469-200d-1f4bc||2,3,4,5,6:1f469-*-200d-1f4bc +female-mechanic|1f469-200d-1f527||2,3,4,5,6:1f469-*-200d-1f527 +female-scientist|1f469-200d-1f52c||2,3,4,5,6:1f469-*-200d-1f52c +female-astronaut|1f469-200d-1f680||2,3,4,5,6:1f469-*-200d-1f680 +female-firefighter|1f469-200d-1f692||2,3,4,5,6:1f469-*-200d-1f692 +woman_with_probing_cane|1f469-200d-1f9af||2,3,4,5,6:1f469-*-200d-1f9af +woman_with_white_cane_facing_right|1f469-200d-1f9af-200d-27a1-fe0f|!|2,3,4,5,6:1f469-*-200d-1f9af-200d-27a1-fe0f:! +red_haired_woman|1f469-200d-1f9b0||2,3,4,5,6:1f469-*-200d-1f9b0 +curly_haired_woman|1f469-200d-1f9b1||2,3,4,5,6:1f469-*-200d-1f9b1 +bald_woman|1f469-200d-1f9b2||2,3,4,5,6:1f469-*-200d-1f9b2 +white_haired_woman|1f469-200d-1f9b3||2,3,4,5,6:1f469-*-200d-1f9b3 +woman_in_motorized_wheelchair|1f469-200d-1f9bc||2,3,4,5,6:1f469-*-200d-1f9bc +woman_in_motorized_wheelchair_facing_right|1f469-200d-1f9bc-200d-27a1-fe0f|!|2,3,4,5,6:1f469-*-200d-1f9bc-200d-27a1-fe0f:! +woman_in_manual_wheelchair|1f469-200d-1f9bd||2,3,4,5,6:1f469-*-200d-1f9bd +woman_in_manual_wheelchair_facing_right|1f469-200d-1f9bd-200d-27a1-fe0f|!|2,3,4,5,6:1f469-*-200d-1f9bd-200d-27a1-fe0f:! +female-doctor|1f469-200d-2695-fe0f|!|2,3,4,5,6:1f469-*-200d-2695-fe0f:! +female-judge|1f469-200d-2696-fe0f|!|2,3,4,5,6:1f469-*-200d-2696-fe0f:! +female-pilot|1f469-200d-2708-fe0f|!|2,3,4,5,6:1f469-*-200d-2708-fe0f:! +woman-heart-man|1f469-200d-2764-fe0f-200d-1f468|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f469-*-200d-2764-fe0f-200d-1f468-*:! +woman-heart-woman|1f469-200d-2764-fe0f-200d-1f469|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f469-*-200d-2764-fe0f-200d-1f469-*:! +woman-kiss-man|1f469-200d-2764-fe0f-200d-1f48b-200d-1f468|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f469-*-200d-2764-fe0f-200d-1f48b-200d-1f468-*:! +woman-kiss-woman|1f469-200d-2764-fe0f-200d-1f48b-200d-1f469|!|22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f469-*-200d-2764-fe0f-200d-1f48b-200d-1f469-*:! +family|1f46a| +man_and_woman_holding_hands,woman_and_man_holding_hands,couple|1f46b||2,3,4,5,6:1f46b-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1f469-*-200d-1f91d-200d-1f468-* +two_men_holding_hands,men_holding_hands|1f46c||2,3,4,5,6:1f46c-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1f468-*-200d-1f91d-200d-1f468-* +two_women_holding_hands,women_holding_hands|1f46d||2,3,4,5,6:1f46d-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1f469-*-200d-1f91d-200d-1f469-* +cop|1f46e||2,3,4,5,6:1f46e-* +female-police-officer|1f46e-200d-2640-fe0f|!|2,3,4,5,6:1f46e-*-200d-2640-fe0f:! +male-police-officer|1f46e-200d-2642-fe0f|!|2,3,4,5,6:1f46e-*-200d-2642-fe0f:! +dancers|1f46f| +women-with-bunny-ears-partying,woman-with-bunny-ears-partying|1f46f-200d-2640-fe0f|! +men-with-bunny-ears-partying,man-with-bunny-ears-partying|1f46f-200d-2642-fe0f|! +bride_with_veil|1f470||2,3,4,5,6:1f470-* +woman_with_veil|1f470-200d-2640-fe0f|!|2,3,4,5,6:1f470-*-200d-2640-fe0f:! +man_with_veil|1f470-200d-2642-fe0f|!|2,3,4,5,6:1f470-*-200d-2642-fe0f:! +person_with_blond_hair|1f471||2,3,4,5,6:1f471-* +blond-haired-woman|1f471-200d-2640-fe0f|!|2,3,4,5,6:1f471-*-200d-2640-fe0f:! +blond-haired-man|1f471-200d-2642-fe0f|!|2,3,4,5,6:1f471-*-200d-2642-fe0f:! +man_with_gua_pi_mao|1f472||2,3,4,5,6:1f472-* +man_with_turban|1f473||2,3,4,5,6:1f473-* +woman-wearing-turban|1f473-200d-2640-fe0f|!|2,3,4,5,6:1f473-*-200d-2640-fe0f:! +man-wearing-turban|1f473-200d-2642-fe0f|!|2,3,4,5,6:1f473-*-200d-2642-fe0f:! +older_man|1f474||2,3,4,5,6:1f474-* +older_woman|1f475||2,3,4,5,6:1f475-* +baby|1f476||2,3,4,5,6:1f476-* +construction_worker|1f477||2,3,4,5,6:1f477-* +female-construction-worker|1f477-200d-2640-fe0f|!|2,3,4,5,6:1f477-*-200d-2640-fe0f:! +male-construction-worker|1f477-200d-2642-fe0f|!|2,3,4,5,6:1f477-*-200d-2642-fe0f:! +princess|1f478||2,3,4,5,6:1f478-* +japanese_ogre|1f479| +japanese_goblin|1f47a| +ghost|1f47b| +angel|1f47c||2,3,4,5,6:1f47c-* +alien|1f47d| +space_invader|1f47e| +imp|1f47f| +skull|1f480| +information_desk_person|1f481||2,3,4,5,6:1f481-* +woman-tipping-hand|1f481-200d-2640-fe0f|!|2,3,4,5,6:1f481-*-200d-2640-fe0f:! +man-tipping-hand|1f481-200d-2642-fe0f|!|2,3,4,5,6:1f481-*-200d-2642-fe0f:! +guardsman|1f482||2,3,4,5,6:1f482-* +female-guard|1f482-200d-2640-fe0f|!|2,3,4,5,6:1f482-*-200d-2640-fe0f:! +male-guard|1f482-200d-2642-fe0f|!|2,3,4,5,6:1f482-*-200d-2642-fe0f:! +dancer|1f483||2,3,4,5,6:1f483-* +lipstick|1f484| +nail_care|1f485||2,3,4,5,6:1f485-* +massage|1f486||2,3,4,5,6:1f486-* +woman-getting-massage|1f486-200d-2640-fe0f|!|2,3,4,5,6:1f486-*-200d-2640-fe0f:! +man-getting-massage|1f486-200d-2642-fe0f|!|2,3,4,5,6:1f486-*-200d-2642-fe0f:! +haircut|1f487||2,3,4,5,6:1f487-* +woman-getting-haircut|1f487-200d-2640-fe0f|!|2,3,4,5,6:1f487-*-200d-2640-fe0f:! +man-getting-haircut|1f487-200d-2642-fe0f|!|2,3,4,5,6:1f487-*-200d-2642-fe0f:! +barber|1f488| +syringe|1f489| +pill|1f48a| +kiss|1f48b| +love_letter|1f48c| +ring|1f48d| +gem|1f48e| +couplekiss|1f48f||2,3,4,5,6:1f48f-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1f9d1-*-200d-2764-fe0f-200d-1f48b-200d-1f9d1-*:! +bouquet|1f490| +couple_with_heart|1f491||2,3,4,5,6:1f491-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1f9d1-*-200d-2764-fe0f-200d-1f9d1-*:! +wedding|1f492| +heartbeat|1f493| +broken_heart|1f494| +two_hearts|1f495| +sparkling_heart|1f496| +heartpulse|1f497| +cupid|1f498| +blue_heart|1f499| +green_heart|1f49a| +yellow_heart|1f49b| +purple_heart|1f49c| +gift_heart|1f49d| +revolving_hearts|1f49e| +heart_decoration|1f49f| +diamond_shape_with_a_dot_inside|1f4a0| +bulb|1f4a1| +anger|1f4a2| +bomb|1f4a3| +zzz|1f4a4| +boom,collision|1f4a5| +sweat_drops|1f4a6| +droplet|1f4a7| +dash|1f4a8| +hankey,poop,shit|1f4a9| +muscle|1f4aa||2,3,4,5,6:1f4aa-* +dizzy|1f4ab| +speech_balloon|1f4ac| +thought_balloon|1f4ad| +white_flower|1f4ae| +100|1f4af| +moneybag|1f4b0| +currency_exchange|1f4b1| +heavy_dollar_sign|1f4b2| +credit_card|1f4b3| +yen|1f4b4| +dollar|1f4b5| +euro|1f4b6| +pound|1f4b7| +money_with_wings|1f4b8| +chart|1f4b9| +seat|1f4ba| +computer|1f4bb| +briefcase|1f4bc| +minidisc|1f4bd| +floppy_disk|1f4be| +cd|1f4bf| +dvd|1f4c0| +file_folder|1f4c1| +open_file_folder|1f4c2| +page_with_curl|1f4c3| +page_facing_up|1f4c4| +date|1f4c5| +calendar|1f4c6| +card_index|1f4c7| +chart_with_upwards_trend|1f4c8| +chart_with_downwards_trend|1f4c9| +bar_chart|1f4ca| +clipboard|1f4cb| +pushpin|1f4cc| +round_pushpin|1f4cd| +paperclip|1f4ce| +straight_ruler|1f4cf| +triangular_ruler|1f4d0| +bookmark_tabs|1f4d1| +ledger|1f4d2| +notebook|1f4d3| +notebook_with_decorative_cover|1f4d4| +closed_book|1f4d5| +book,open_book|1f4d6| +green_book|1f4d7| +blue_book|1f4d8| +orange_book|1f4d9| +books|1f4da| +name_badge|1f4db| +scroll|1f4dc| +memo,pencil|1f4dd| +telephone_receiver|1f4de| +pager|1f4df| +fax|1f4e0| +satellite_antenna|1f4e1| +loudspeaker|1f4e2| +mega|1f4e3| +outbox_tray|1f4e4| +inbox_tray|1f4e5| +package|1f4e6| +e-mail|1f4e7| +incoming_envelope|1f4e8| +envelope_with_arrow|1f4e9| +mailbox_closed|1f4ea| +mailbox|1f4eb| +mailbox_with_mail|1f4ec| +mailbox_with_no_mail|1f4ed| +postbox|1f4ee| +postal_horn|1f4ef| +newspaper|1f4f0| +iphone|1f4f1| +calling|1f4f2| +vibration_mode|1f4f3| +mobile_phone_off|1f4f4| +no_mobile_phones|1f4f5| +signal_strength|1f4f6| +camera|1f4f7| +camera_with_flash|1f4f8| +video_camera|1f4f9| +tv|1f4fa| +radio|1f4fb| +vhs|1f4fc| +film_projector|1f4fd-fe0f|! +prayer_beads|1f4ff| +twisted_rightwards_arrows|1f500| +repeat|1f501| +repeat_one|1f502| +arrows_clockwise|1f503| +arrows_counterclockwise|1f504| +low_brightness|1f505| +high_brightness|1f506| +mute|1f507| +speaker|1f508| +sound|1f509| +loud_sound|1f50a| +battery|1f50b| +electric_plug|1f50c| +mag|1f50d| +mag_right|1f50e| +lock_with_ink_pen|1f50f| +closed_lock_with_key|1f510| +key|1f511| +lock|1f512| +unlock|1f513| +bell|1f514| +no_bell|1f515| +bookmark|1f516| +link|1f517| +radio_button|1f518| +back|1f519| +end|1f51a| +on|1f51b| +soon|1f51c| +top|1f51d| +underage|1f51e| +keycap_ten|1f51f| +capital_abcd|1f520| +abcd|1f521| +1234|1f522| +symbols|1f523| +abc|1f524| +fire|1f525| +flashlight|1f526| +wrench|1f527| +hammer|1f528| +nut_and_bolt|1f529| +hocho,knife|1f52a| +gun|1f52b| +microscope|1f52c| +telescope|1f52d| +crystal_ball|1f52e| +six_pointed_star|1f52f| +beginner|1f530| +trident|1f531| +black_square_button|1f532| +white_square_button|1f533| +red_circle|1f534| +large_blue_circle|1f535| +large_orange_diamond|1f536| +large_blue_diamond|1f537| +small_orange_diamond|1f538| +small_blue_diamond|1f539| +small_red_triangle|1f53a| +small_red_triangle_down|1f53b| +arrow_up_small|1f53c| +arrow_down_small|1f53d| +om_symbol|1f549-fe0f|! +dove_of_peace|1f54a-fe0f|! +kaaba|1f54b| +mosque|1f54c| +synagogue|1f54d| +menorah_with_nine_branches|1f54e| +clock1|1f550| +clock2|1f551| +clock3|1f552| +clock4|1f553| +clock5|1f554| +clock6|1f555| +clock7|1f556| +clock8|1f557| +clock9|1f558| +clock10|1f559| +clock11|1f55a| +clock12|1f55b| +clock130|1f55c| +clock230|1f55d| +clock330|1f55e| +clock430|1f55f| +clock530|1f560| +clock630|1f561| +clock730|1f562| +clock830|1f563| +clock930|1f564| +clock1030|1f565| +clock1130|1f566| +clock1230|1f567| +candle|1f56f-fe0f|! +mantelpiece_clock|1f570-fe0f|! +hole|1f573-fe0f|! +man_in_business_suit_levitating|1f574-fe0f|!|2,3,4,5,6:1f574-* +sleuth_or_spy|1f575-fe0f|!|2,3,4,5,6:1f575-* +female-detective|1f575-fe0f-200d-2640-fe0f|!|2,3,4,5,6:1f575-*-200d-2640-fe0f:! +male-detective|1f575-fe0f-200d-2642-fe0f|!|2,3,4,5,6:1f575-*-200d-2642-fe0f:! +dark_sunglasses|1f576-fe0f|! +spider|1f577-fe0f|! +spider_web|1f578-fe0f|! +joystick|1f579-fe0f|! +man_dancing|1f57a||2,3,4,5,6:1f57a-* +linked_paperclips|1f587-fe0f|! +lower_left_ballpoint_pen|1f58a-fe0f|! +lower_left_fountain_pen|1f58b-fe0f|! +lower_left_paintbrush|1f58c-fe0f|! +lower_left_crayon|1f58d-fe0f|! +raised_hand_with_fingers_splayed|1f590-fe0f|!|2,3,4,5,6:1f590-* +middle_finger,reversed_hand_with_middle_finger_extended|1f595||2,3,4,5,6:1f595-* +spock-hand|1f596||2,3,4,5,6:1f596-* +black_heart|1f5a4| +desktop_computer|1f5a5-fe0f|! +printer|1f5a8-fe0f|! +three_button_mouse|1f5b1-fe0f|! +trackball|1f5b2-fe0f|! +frame_with_picture|1f5bc-fe0f|! +card_index_dividers|1f5c2-fe0f|! +card_file_box|1f5c3-fe0f|! +file_cabinet|1f5c4-fe0f|! +wastebasket|1f5d1-fe0f|! +spiral_note_pad|1f5d2-fe0f|! +spiral_calendar_pad|1f5d3-fe0f|! +compression|1f5dc-fe0f|! +old_key|1f5dd-fe0f|! +rolled_up_newspaper|1f5de-fe0f|! +dagger_knife|1f5e1-fe0f|! +speaking_head_in_silhouette|1f5e3-fe0f|! +left_speech_bubble|1f5e8-fe0f|! +right_anger_bubble|1f5ef-fe0f|! +ballot_box_with_ballot|1f5f3-fe0f|! +world_map|1f5fa-fe0f|! +mount_fuji|1f5fb| +tokyo_tower|1f5fc| +statue_of_liberty|1f5fd| +japan|1f5fe| +moyai|1f5ff| +grinning|1f600| +grin|1f601| +joy|1f602| +smiley|1f603| +smile|1f604| +sweat_smile|1f605| +laughing,satisfied|1f606| +innocent|1f607| +smiling_imp|1f608| +wink|1f609| +blush|1f60a| +yum|1f60b| +relieved|1f60c| +heart_eyes|1f60d| +sunglasses|1f60e| +smirk|1f60f| +neutral_face|1f610| +expressionless|1f611| +unamused|1f612| +sweat|1f613| +pensive|1f614| +confused|1f615| +confounded|1f616| +kissing|1f617| +kissing_heart|1f618| +kissing_smiling_eyes|1f619| +kissing_closed_eyes|1f61a| +stuck_out_tongue|1f61b| +stuck_out_tongue_winking_eye|1f61c| +stuck_out_tongue_closed_eyes|1f61d| +disappointed|1f61e| +worried|1f61f| +angry|1f620| +rage|1f621| +cry|1f622| +persevere|1f623| +triumph|1f624| +disappointed_relieved|1f625| +frowning|1f626| +anguished|1f627| +fearful|1f628| +weary|1f629| +sleepy|1f62a| +tired_face|1f62b| +grimacing|1f62c| +sob|1f62d| +open_mouth|1f62e| +face_exhaling|1f62e-200d-1f4a8| +hushed|1f62f| +cold_sweat|1f630| +scream|1f631| +astonished|1f632| +flushed|1f633| +sleeping|1f634| +dizzy_face|1f635| +face_with_spiral_eyes|1f635-200d-1f4ab| +no_mouth|1f636| +face_in_clouds|1f636-200d-1f32b-fe0f|! +mask|1f637| +smile_cat|1f638| +joy_cat|1f639| +smiley_cat|1f63a| +heart_eyes_cat|1f63b| +smirk_cat|1f63c| +kissing_cat|1f63d| +pouting_cat|1f63e| +crying_cat_face|1f63f| +scream_cat|1f640| +slightly_frowning_face|1f641| +slightly_smiling_face|1f642| +head_shaking_horizontally|1f642-200d-2194-fe0f|! +head_shaking_vertically|1f642-200d-2195-fe0f|! +upside_down_face|1f643| +face_with_rolling_eyes|1f644| +no_good|1f645||2,3,4,5,6:1f645-* +woman-gesturing-no|1f645-200d-2640-fe0f|!|2,3,4,5,6:1f645-*-200d-2640-fe0f:! +man-gesturing-no|1f645-200d-2642-fe0f|!|2,3,4,5,6:1f645-*-200d-2642-fe0f:! +ok_woman|1f646||2,3,4,5,6:1f646-* +woman-gesturing-ok|1f646-200d-2640-fe0f|!|2,3,4,5,6:1f646-*-200d-2640-fe0f:! +man-gesturing-ok|1f646-200d-2642-fe0f|!|2,3,4,5,6:1f646-*-200d-2642-fe0f:! +bow|1f647||2,3,4,5,6:1f647-* +woman-bowing|1f647-200d-2640-fe0f|!|2,3,4,5,6:1f647-*-200d-2640-fe0f:! +man-bowing|1f647-200d-2642-fe0f|!|2,3,4,5,6:1f647-*-200d-2642-fe0f:! +see_no_evil|1f648| +hear_no_evil|1f649| +speak_no_evil|1f64a| +raising_hand|1f64b||2,3,4,5,6:1f64b-* +woman-raising-hand|1f64b-200d-2640-fe0f|!|2,3,4,5,6:1f64b-*-200d-2640-fe0f:! +man-raising-hand|1f64b-200d-2642-fe0f|!|2,3,4,5,6:1f64b-*-200d-2642-fe0f:! +raised_hands|1f64c||2,3,4,5,6:1f64c-* +person_frowning|1f64d||2,3,4,5,6:1f64d-* +woman-frowning|1f64d-200d-2640-fe0f|!|2,3,4,5,6:1f64d-*-200d-2640-fe0f:! +man-frowning|1f64d-200d-2642-fe0f|!|2,3,4,5,6:1f64d-*-200d-2642-fe0f:! +person_with_pouting_face|1f64e||2,3,4,5,6:1f64e-* +woman-pouting|1f64e-200d-2640-fe0f|!|2,3,4,5,6:1f64e-*-200d-2640-fe0f:! +man-pouting|1f64e-200d-2642-fe0f|!|2,3,4,5,6:1f64e-*-200d-2642-fe0f:! +pray|1f64f||2,3,4,5,6:1f64f-* +rocket|1f680| +helicopter|1f681| +steam_locomotive|1f682| +railway_car|1f683| +bullettrain_side|1f684| +bullettrain_front|1f685| +train2|1f686| +metro|1f687| +light_rail|1f688| +station|1f689| +tram|1f68a| +train|1f68b| +bus|1f68c| +oncoming_bus|1f68d| +trolleybus|1f68e| +busstop|1f68f| +minibus|1f690| +ambulance|1f691| +fire_engine|1f692| +police_car|1f693| +oncoming_police_car|1f694| +taxi|1f695| +oncoming_taxi|1f696| +car,red_car|1f697| +oncoming_automobile|1f698| +blue_car|1f699| +truck|1f69a| +articulated_lorry|1f69b| +tractor|1f69c| +monorail|1f69d| +mountain_railway|1f69e| +suspension_railway|1f69f| +mountain_cableway|1f6a0| +aerial_tramway|1f6a1| +ship|1f6a2| +rowboat|1f6a3||2,3,4,5,6:1f6a3-* +woman-rowing-boat|1f6a3-200d-2640-fe0f|!|2,3,4,5,6:1f6a3-*-200d-2640-fe0f:! +man-rowing-boat|1f6a3-200d-2642-fe0f|!|2,3,4,5,6:1f6a3-*-200d-2642-fe0f:! +speedboat|1f6a4| +traffic_light|1f6a5| +vertical_traffic_light|1f6a6| +construction|1f6a7| +rotating_light|1f6a8| +triangular_flag_on_post|1f6a9| +door|1f6aa| +no_entry_sign|1f6ab| +smoking|1f6ac| +no_smoking|1f6ad| +put_litter_in_its_place|1f6ae| +do_not_litter|1f6af| +potable_water|1f6b0| +non-potable_water|1f6b1| +bike|1f6b2| +no_bicycles|1f6b3| +bicyclist|1f6b4||2,3,4,5,6:1f6b4-* +woman-biking|1f6b4-200d-2640-fe0f|!|2,3,4,5,6:1f6b4-*-200d-2640-fe0f:! +man-biking|1f6b4-200d-2642-fe0f|!|2,3,4,5,6:1f6b4-*-200d-2642-fe0f:! +mountain_bicyclist|1f6b5||2,3,4,5,6:1f6b5-* +woman-mountain-biking|1f6b5-200d-2640-fe0f|!|2,3,4,5,6:1f6b5-*-200d-2640-fe0f:! +man-mountain-biking|1f6b5-200d-2642-fe0f|!|2,3,4,5,6:1f6b5-*-200d-2642-fe0f:! +walking|1f6b6||2,3,4,5,6:1f6b6-* +woman-walking|1f6b6-200d-2640-fe0f|!|2,3,4,5,6:1f6b6-*-200d-2640-fe0f:! +woman_walking_facing_right|1f6b6-200d-2640-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f6b6-*-200d-2640-fe0f-200d-27a1-fe0f:! +man-walking|1f6b6-200d-2642-fe0f|!|2,3,4,5,6:1f6b6-*-200d-2642-fe0f:! +man_walking_facing_right|1f6b6-200d-2642-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f6b6-*-200d-2642-fe0f-200d-27a1-fe0f:! +person_walking_facing_right|1f6b6-200d-27a1-fe0f|!|2,3,4,5,6:1f6b6-*-200d-27a1-fe0f:! +no_pedestrians|1f6b7| +children_crossing|1f6b8| +mens|1f6b9| +womens|1f6ba| +restroom|1f6bb| +baby_symbol|1f6bc| +toilet|1f6bd| +wc|1f6be| +shower|1f6bf| +bath|1f6c0||2,3,4,5,6:1f6c0-* +bathtub|1f6c1| +passport_control|1f6c2| +customs|1f6c3| +baggage_claim|1f6c4| +left_luggage|1f6c5| +couch_and_lamp|1f6cb-fe0f|! +sleeping_accommodation|1f6cc||2,3,4,5,6:1f6cc-* +shopping_bags|1f6cd-fe0f|! +bellhop_bell|1f6ce-fe0f|! +bed|1f6cf-fe0f|! +place_of_worship|1f6d0| +octagonal_sign|1f6d1| +shopping_trolley|1f6d2| +hindu_temple|1f6d5| +hut|1f6d6| +elevator|1f6d7| +wireless|1f6dc| +playground_slide|1f6dd| +wheel|1f6de| +ring_buoy|1f6df| +hammer_and_wrench|1f6e0-fe0f|! +shield|1f6e1-fe0f|! +oil_drum|1f6e2-fe0f|! +motorway|1f6e3-fe0f|! +railway_track|1f6e4-fe0f|! +motor_boat|1f6e5-fe0f|! +small_airplane|1f6e9-fe0f|! +airplane_departure|1f6eb| +airplane_arriving|1f6ec| +satellite|1f6f0-fe0f|! +passenger_ship|1f6f3-fe0f|! +scooter|1f6f4| +motor_scooter|1f6f5| +canoe|1f6f6| +sled|1f6f7| +flying_saucer|1f6f8| +skateboard|1f6f9| +auto_rickshaw|1f6fa| +pickup_truck|1f6fb| +roller_skate|1f6fc| +large_orange_circle|1f7e0| +large_yellow_circle|1f7e1| +large_green_circle|1f7e2| +large_purple_circle|1f7e3| +large_brown_circle|1f7e4| +large_red_square|1f7e5| +large_blue_square|1f7e6| +large_orange_square|1f7e7| +large_yellow_square|1f7e8| +large_green_square|1f7e9| +large_purple_square|1f7ea| +large_brown_square|1f7eb| +heavy_equals_sign|1f7f0| +pinched_fingers|1f90c||2,3,4,5,6:1f90c-* +white_heart|1f90d| +brown_heart|1f90e| +pinching_hand|1f90f||2,3,4,5,6:1f90f-* +zipper_mouth_face|1f910| +money_mouth_face|1f911| +face_with_thermometer|1f912| +nerd_face|1f913| +thinking_face|1f914| +face_with_head_bandage|1f915| +robot_face|1f916| +hugging_face|1f917| +the_horns,sign_of_the_horns|1f918||2,3,4,5,6:1f918-* +call_me_hand|1f919||2,3,4,5,6:1f919-* +raised_back_of_hand|1f91a||2,3,4,5,6:1f91a-* +left-facing_fist|1f91b||2,3,4,5,6:1f91b-* +right-facing_fist|1f91c||2,3,4,5,6:1f91c-* +handshake|1f91d||2,3,4,5,6:1f91d-*;23,24,25,26,32,34,35,36,42,43,45,46,52,53,54,56,62,63,64,65:1faf1-*-200d-1faf2-* +crossed_fingers,hand_with_index_and_middle_fingers_crossed|1f91e||2,3,4,5,6:1f91e-* +i_love_you_hand_sign|1f91f||2,3,4,5,6:1f91f-* +face_with_cowboy_hat|1f920| +clown_face|1f921| +nauseated_face|1f922| +rolling_on_the_floor_laughing|1f923| +drooling_face|1f924| +lying_face|1f925| +face_palm|1f926||2,3,4,5,6:1f926-* +woman-facepalming|1f926-200d-2640-fe0f|!|2,3,4,5,6:1f926-*-200d-2640-fe0f:! +man-facepalming|1f926-200d-2642-fe0f|!|2,3,4,5,6:1f926-*-200d-2642-fe0f:! +sneezing_face|1f927| +face_with_raised_eyebrow,face_with_one_eyebrow_raised|1f928| +star-struck,grinning_face_with_star_eyes|1f929| +zany_face,grinning_face_with_one_large_and_one_small_eye|1f92a| +shushing_face,face_with_finger_covering_closed_lips|1f92b| +face_with_symbols_on_mouth,serious_face_with_symbols_covering_mouth|1f92c| +face_with_hand_over_mouth,smiling_face_with_smiling_eyes_and_hand_covering_mouth|1f92d| +face_vomiting,face_with_open_mouth_vomiting|1f92e| +exploding_head,shocked_face_with_exploding_head|1f92f| +pregnant_woman|1f930||2,3,4,5,6:1f930-* +breast-feeding|1f931||2,3,4,5,6:1f931-* +palms_up_together|1f932||2,3,4,5,6:1f932-* +selfie|1f933||2,3,4,5,6:1f933-* +prince|1f934||2,3,4,5,6:1f934-* +person_in_tuxedo|1f935||2,3,4,5,6:1f935-* +woman_in_tuxedo|1f935-200d-2640-fe0f|!|2,3,4,5,6:1f935-*-200d-2640-fe0f:! +man_in_tuxedo|1f935-200d-2642-fe0f|!|2,3,4,5,6:1f935-*-200d-2642-fe0f:! +mrs_claus,mother_christmas|1f936||2,3,4,5,6:1f936-* +shrug|1f937||2,3,4,5,6:1f937-* +woman-shrugging|1f937-200d-2640-fe0f|!|2,3,4,5,6:1f937-*-200d-2640-fe0f:! +man-shrugging|1f937-200d-2642-fe0f|!|2,3,4,5,6:1f937-*-200d-2642-fe0f:! +person_doing_cartwheel|1f938||2,3,4,5,6:1f938-* +woman-cartwheeling|1f938-200d-2640-fe0f|!|2,3,4,5,6:1f938-*-200d-2640-fe0f:! +man-cartwheeling|1f938-200d-2642-fe0f|!|2,3,4,5,6:1f938-*-200d-2642-fe0f:! +juggling|1f939||2,3,4,5,6:1f939-* +woman-juggling|1f939-200d-2640-fe0f|!|2,3,4,5,6:1f939-*-200d-2640-fe0f:! +man-juggling|1f939-200d-2642-fe0f|!|2,3,4,5,6:1f939-*-200d-2642-fe0f:! +fencer|1f93a| +wrestlers|1f93c| +woman-wrestling|1f93c-200d-2640-fe0f|! +man-wrestling|1f93c-200d-2642-fe0f|! +water_polo|1f93d||2,3,4,5,6:1f93d-* +woman-playing-water-polo|1f93d-200d-2640-fe0f|!|2,3,4,5,6:1f93d-*-200d-2640-fe0f:! +man-playing-water-polo|1f93d-200d-2642-fe0f|!|2,3,4,5,6:1f93d-*-200d-2642-fe0f:! +handball|1f93e||2,3,4,5,6:1f93e-* +woman-playing-handball|1f93e-200d-2640-fe0f|!|2,3,4,5,6:1f93e-*-200d-2640-fe0f:! +man-playing-handball|1f93e-200d-2642-fe0f|!|2,3,4,5,6:1f93e-*-200d-2642-fe0f:! +diving_mask|1f93f| +wilted_flower|1f940| +drum_with_drumsticks|1f941| +clinking_glasses|1f942| +tumbler_glass|1f943| +spoon|1f944| +goal_net|1f945| +first_place_medal|1f947| +second_place_medal|1f948| +third_place_medal|1f949| +boxing_glove|1f94a| +martial_arts_uniform|1f94b| +curling_stone|1f94c| +lacrosse|1f94d| +softball|1f94e| +flying_disc|1f94f| +croissant|1f950| +avocado|1f951| +cucumber|1f952| +bacon|1f953| +potato|1f954| +carrot|1f955| +baguette_bread|1f956| +green_salad|1f957| +shallow_pan_of_food|1f958| +stuffed_flatbread|1f959| +egg|1f95a| +glass_of_milk|1f95b| +peanuts|1f95c| +kiwifruit|1f95d| +pancakes|1f95e| +dumpling|1f95f| +fortune_cookie|1f960| +takeout_box|1f961| +chopsticks|1f962| +bowl_with_spoon|1f963| +cup_with_straw|1f964| +coconut|1f965| +broccoli|1f966| +pie|1f967| +pretzel|1f968| +cut_of_meat|1f969| +sandwich|1f96a| +canned_food|1f96b| +leafy_green|1f96c| +mango|1f96d| +moon_cake|1f96e| +bagel|1f96f| +smiling_face_with_3_hearts|1f970| +yawning_face|1f971| +smiling_face_with_tear|1f972| +partying_face|1f973| +woozy_face|1f974| +hot_face|1f975| +cold_face|1f976| +ninja|1f977||2,3,4,5,6:1f977-* +disguised_face|1f978| +face_holding_back_tears|1f979| +pleading_face|1f97a| +sari|1f97b| +lab_coat|1f97c| +goggles|1f97d| +hiking_boot|1f97e| +womans_flat_shoe|1f97f| +crab|1f980| +lion_face|1f981| +scorpion|1f982| +turkey|1f983| +unicorn_face|1f984| +eagle|1f985| +duck|1f986| +bat|1f987| +shark|1f988| +owl|1f989| +fox_face|1f98a| +butterfly|1f98b| +deer|1f98c| +gorilla|1f98d| +lizard|1f98e| +rhinoceros|1f98f| +shrimp|1f990| +squid|1f991| +giraffe_face|1f992| +zebra_face|1f993| +hedgehog|1f994| +sauropod|1f995| +t-rex|1f996| +cricket|1f997| +kangaroo|1f998| +llama|1f999| +peacock|1f99a| +hippopotamus|1f99b| +parrot|1f99c| +raccoon|1f99d| +lobster|1f99e| +mosquito|1f99f| +microbe|1f9a0| +badger|1f9a1| +swan|1f9a2| +mammoth|1f9a3| +dodo|1f9a4| +sloth|1f9a5| +otter|1f9a6| +orangutan|1f9a7| +skunk|1f9a8| +flamingo|1f9a9| +oyster|1f9aa| +beaver|1f9ab| +bison|1f9ac| +seal|1f9ad| +guide_dog|1f9ae| +probing_cane|1f9af| +bone|1f9b4| +leg|1f9b5||2,3,4,5,6:1f9b5-* +foot|1f9b6||2,3,4,5,6:1f9b6-* +tooth|1f9b7| +superhero|1f9b8||2,3,4,5,6:1f9b8-* +female_superhero|1f9b8-200d-2640-fe0f|!|2,3,4,5,6:1f9b8-*-200d-2640-fe0f:! +male_superhero|1f9b8-200d-2642-fe0f|!|2,3,4,5,6:1f9b8-*-200d-2642-fe0f:! +supervillain|1f9b9||2,3,4,5,6:1f9b9-* +female_supervillain|1f9b9-200d-2640-fe0f|!|2,3,4,5,6:1f9b9-*-200d-2640-fe0f:! +male_supervillain|1f9b9-200d-2642-fe0f|!|2,3,4,5,6:1f9b9-*-200d-2642-fe0f:! +safety_vest|1f9ba| +ear_with_hearing_aid|1f9bb||2,3,4,5,6:1f9bb-* +motorized_wheelchair|1f9bc| +manual_wheelchair|1f9bd| +mechanical_arm|1f9be| +mechanical_leg|1f9bf| +cheese_wedge|1f9c0| +cupcake|1f9c1| +salt|1f9c2| +beverage_box|1f9c3| +garlic|1f9c4| +onion|1f9c5| +falafel|1f9c6| +waffle|1f9c7| +butter|1f9c8| +mate_drink|1f9c9| +ice_cube|1f9ca| +bubble_tea|1f9cb| +troll|1f9cc| +standing_person|1f9cd||2,3,4,5,6:1f9cd-* +woman_standing|1f9cd-200d-2640-fe0f|!|2,3,4,5,6:1f9cd-*-200d-2640-fe0f:! +man_standing|1f9cd-200d-2642-fe0f|!|2,3,4,5,6:1f9cd-*-200d-2642-fe0f:! +kneeling_person|1f9ce||2,3,4,5,6:1f9ce-* +woman_kneeling|1f9ce-200d-2640-fe0f|!|2,3,4,5,6:1f9ce-*-200d-2640-fe0f:! +woman_kneeling_facing_right|1f9ce-200d-2640-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f9ce-*-200d-2640-fe0f-200d-27a1-fe0f:! +man_kneeling|1f9ce-200d-2642-fe0f|!|2,3,4,5,6:1f9ce-*-200d-2642-fe0f:! +man_kneeling_facing_right|1f9ce-200d-2642-fe0f-200d-27a1-fe0f|!|2,3,4,5,6:1f9ce-*-200d-2642-fe0f-200d-27a1-fe0f:! +person_kneeling_facing_right|1f9ce-200d-27a1-fe0f|!|2,3,4,5,6:1f9ce-*-200d-27a1-fe0f:! +deaf_person|1f9cf||2,3,4,5,6:1f9cf-* +deaf_woman|1f9cf-200d-2640-fe0f|!|2,3,4,5,6:1f9cf-*-200d-2640-fe0f:! +deaf_man|1f9cf-200d-2642-fe0f|!|2,3,4,5,6:1f9cf-*-200d-2642-fe0f:! +face_with_monocle|1f9d0| +adult|1f9d1||2,3,4,5,6:1f9d1-* +farmer|1f9d1-200d-1f33e||2,3,4,5,6:1f9d1-*-200d-1f33e +cook|1f9d1-200d-1f373||2,3,4,5,6:1f9d1-*-200d-1f373 +person_feeding_baby|1f9d1-200d-1f37c||2,3,4,5,6:1f9d1-*-200d-1f37c +mx_claus|1f9d1-200d-1f384||2,3,4,5,6:1f9d1-*-200d-1f384 +student|1f9d1-200d-1f393||2,3,4,5,6:1f9d1-*-200d-1f393 +singer|1f9d1-200d-1f3a4||2,3,4,5,6:1f9d1-*-200d-1f3a4 +artist|1f9d1-200d-1f3a8||2,3,4,5,6:1f9d1-*-200d-1f3a8 +teacher|1f9d1-200d-1f3eb||2,3,4,5,6:1f9d1-*-200d-1f3eb +factory_worker|1f9d1-200d-1f3ed||2,3,4,5,6:1f9d1-*-200d-1f3ed +technologist|1f9d1-200d-1f4bb||2,3,4,5,6:1f9d1-*-200d-1f4bb +office_worker|1f9d1-200d-1f4bc||2,3,4,5,6:1f9d1-*-200d-1f4bc +mechanic|1f9d1-200d-1f527||2,3,4,5,6:1f9d1-*-200d-1f527 +scientist|1f9d1-200d-1f52c||2,3,4,5,6:1f9d1-*-200d-1f52c +astronaut|1f9d1-200d-1f680||2,3,4,5,6:1f9d1-*-200d-1f680 +firefighter|1f9d1-200d-1f692||2,3,4,5,6:1f9d1-*-200d-1f692 +people_holding_hands|1f9d1-200d-1f91d-200d-1f9d1||22,23,24,25,26,32,33,34,35,36,42,43,44,45,46,52,53,54,55,56,62,63,64,65,66:1f9d1-*-200d-1f91d-200d-1f9d1-* +person_with_probing_cane|1f9d1-200d-1f9af||2,3,4,5,6:1f9d1-*-200d-1f9af +person_with_white_cane_facing_right|1f9d1-200d-1f9af-200d-27a1-fe0f|!|2,3,4,5,6:1f9d1-*-200d-1f9af-200d-27a1-fe0f:! +red_haired_person|1f9d1-200d-1f9b0||2,3,4,5,6:1f9d1-*-200d-1f9b0 +curly_haired_person|1f9d1-200d-1f9b1||2,3,4,5,6:1f9d1-*-200d-1f9b1 +bald_person|1f9d1-200d-1f9b2||2,3,4,5,6:1f9d1-*-200d-1f9b2 +white_haired_person|1f9d1-200d-1f9b3||2,3,4,5,6:1f9d1-*-200d-1f9b3 +person_in_motorized_wheelchair|1f9d1-200d-1f9bc||2,3,4,5,6:1f9d1-*-200d-1f9bc +person_in_motorized_wheelchair_facing_right|1f9d1-200d-1f9bc-200d-27a1-fe0f|!|2,3,4,5,6:1f9d1-*-200d-1f9bc-200d-27a1-fe0f:! +person_in_manual_wheelchair|1f9d1-200d-1f9bd||2,3,4,5,6:1f9d1-*-200d-1f9bd +person_in_manual_wheelchair_facing_right|1f9d1-200d-1f9bd-200d-27a1-fe0f|!|2,3,4,5,6:1f9d1-*-200d-1f9bd-200d-27a1-fe0f:! +family_adult_adult_child|1f9d1-200d-1f9d1-200d-1f9d2| +family_adult_adult_child_child|1f9d1-200d-1f9d1-200d-1f9d2-200d-1f9d2| +family_adult_child|1f9d1-200d-1f9d2| +family_adult_child_child|1f9d1-200d-1f9d2-200d-1f9d2| +health_worker|1f9d1-200d-2695-fe0f|!|2,3,4,5,6:1f9d1-*-200d-2695-fe0f:! +judge|1f9d1-200d-2696-fe0f|!|2,3,4,5,6:1f9d1-*-200d-2696-fe0f:! +pilot|1f9d1-200d-2708-fe0f|!|2,3,4,5,6:1f9d1-*-200d-2708-fe0f:! +child|1f9d2||2,3,4,5,6:1f9d2-* +older_adult|1f9d3||2,3,4,5,6:1f9d3-* +bearded_person|1f9d4||2,3,4,5,6:1f9d4-* +woman_with_beard|1f9d4-200d-2640-fe0f|!|2,3,4,5,6:1f9d4-*-200d-2640-fe0f:! +man_with_beard|1f9d4-200d-2642-fe0f|!|2,3,4,5,6:1f9d4-*-200d-2642-fe0f:! +person_with_headscarf|1f9d5||2,3,4,5,6:1f9d5-* +person_in_steamy_room|1f9d6||2,3,4,5,6:1f9d6-* +woman_in_steamy_room|1f9d6-200d-2640-fe0f|!|2,3,4,5,6:1f9d6-*-200d-2640-fe0f:! +man_in_steamy_room|1f9d6-200d-2642-fe0f|!|2,3,4,5,6:1f9d6-*-200d-2642-fe0f:! +person_climbing|1f9d7||2,3,4,5,6:1f9d7-* +woman_climbing|1f9d7-200d-2640-fe0f|!|2,3,4,5,6:1f9d7-*-200d-2640-fe0f:! +man_climbing|1f9d7-200d-2642-fe0f|!|2,3,4,5,6:1f9d7-*-200d-2642-fe0f:! +person_in_lotus_position|1f9d8||2,3,4,5,6:1f9d8-* +woman_in_lotus_position|1f9d8-200d-2640-fe0f|!|2,3,4,5,6:1f9d8-*-200d-2640-fe0f:! +man_in_lotus_position|1f9d8-200d-2642-fe0f|!|2,3,4,5,6:1f9d8-*-200d-2642-fe0f:! +mage|1f9d9||2,3,4,5,6:1f9d9-* +female_mage|1f9d9-200d-2640-fe0f|!|2,3,4,5,6:1f9d9-*-200d-2640-fe0f:! +male_mage|1f9d9-200d-2642-fe0f|!|2,3,4,5,6:1f9d9-*-200d-2642-fe0f:! +fairy|1f9da||2,3,4,5,6:1f9da-* +female_fairy|1f9da-200d-2640-fe0f|!|2,3,4,5,6:1f9da-*-200d-2640-fe0f:! +male_fairy|1f9da-200d-2642-fe0f|!|2,3,4,5,6:1f9da-*-200d-2642-fe0f:! +vampire|1f9db||2,3,4,5,6:1f9db-* +female_vampire|1f9db-200d-2640-fe0f|!|2,3,4,5,6:1f9db-*-200d-2640-fe0f:! +male_vampire|1f9db-200d-2642-fe0f|!|2,3,4,5,6:1f9db-*-200d-2642-fe0f:! +merperson|1f9dc||2,3,4,5,6:1f9dc-* +mermaid|1f9dc-200d-2640-fe0f|!|2,3,4,5,6:1f9dc-*-200d-2640-fe0f:! +merman|1f9dc-200d-2642-fe0f|!|2,3,4,5,6:1f9dc-*-200d-2642-fe0f:! +elf|1f9dd||2,3,4,5,6:1f9dd-* +female_elf|1f9dd-200d-2640-fe0f|!|2,3,4,5,6:1f9dd-*-200d-2640-fe0f:! +male_elf|1f9dd-200d-2642-fe0f|!|2,3,4,5,6:1f9dd-*-200d-2642-fe0f:! +genie|1f9de| +female_genie|1f9de-200d-2640-fe0f|! +male_genie|1f9de-200d-2642-fe0f|! +zombie|1f9df| +female_zombie|1f9df-200d-2640-fe0f|! +male_zombie|1f9df-200d-2642-fe0f|! +brain|1f9e0| +orange_heart|1f9e1| +billed_cap|1f9e2| +scarf|1f9e3| +gloves|1f9e4| +coat|1f9e5| +socks|1f9e6| +red_envelope|1f9e7| +firecracker|1f9e8| +jigsaw|1f9e9| +test_tube|1f9ea| +petri_dish|1f9eb| +dna|1f9ec| +compass|1f9ed| +abacus|1f9ee| +fire_extinguisher|1f9ef| +toolbox|1f9f0| +bricks|1f9f1| +magnet|1f9f2| +luggage|1f9f3| +lotion_bottle|1f9f4| +thread|1f9f5| +yarn|1f9f6| +safety_pin|1f9f7| +teddy_bear|1f9f8| +broom|1f9f9| +basket|1f9fa| +roll_of_paper|1f9fb| +soap|1f9fc| +sponge|1f9fd| +receipt|1f9fe| +nazar_amulet|1f9ff| +ballet_shoes|1fa70| +one-piece_swimsuit|1fa71| +briefs|1fa72| +shorts|1fa73| +thong_sandal|1fa74| +light_blue_heart|1fa75| +grey_heart|1fa76| +pink_heart|1fa77| +drop_of_blood|1fa78| +adhesive_bandage|1fa79| +stethoscope|1fa7a| +x-ray|1fa7b| +crutch|1fa7c| +yo-yo|1fa80| +kite|1fa81| +parachute|1fa82| +boomerang|1fa83| +magic_wand|1fa84| +pinata|1fa85| +nesting_dolls|1fa86| +maracas|1fa87| +flute|1fa88| +harp|1fa89| +shovel|1fa8f| +ringed_planet|1fa90| +chair|1fa91| +razor|1fa92| +axe|1fa93| +diya_lamp|1fa94| +banjo|1fa95| +military_helmet|1fa96| +accordion|1fa97| +long_drum|1fa98| +coin|1fa99| +carpentry_saw|1fa9a| +screwdriver|1fa9b| +ladder|1fa9c| +hook|1fa9d| +mirror|1fa9e| +window|1fa9f| +plunger|1faa0| +sewing_needle|1faa1| +knot|1faa2| +bucket|1faa3| +mouse_trap|1faa4| +toothbrush|1faa5| +headstone|1faa6| +placard|1faa7| +rock|1faa8| +mirror_ball|1faa9| +identification_card|1faaa| +low_battery|1faab| +hamsa|1faac| +folding_hand_fan|1faad| +hair_pick|1faae| +khanda|1faaf| +fly|1fab0| +worm|1fab1| +beetle|1fab2| +cockroach|1fab3| +potted_plant|1fab4| +wood|1fab5| +feather|1fab6| +lotus|1fab7| +coral|1fab8| +empty_nest|1fab9| +nest_with_eggs|1faba| +hyacinth|1fabb| +jellyfish|1fabc| +wing|1fabd| +leafless_tree|1fabe| +goose|1fabf| +anatomical_heart|1fac0| +lungs|1fac1| +people_hugging|1fac2| +pregnant_man|1fac3||2,3,4,5,6:1fac3-* +pregnant_person|1fac4||2,3,4,5,6:1fac4-* +person_with_crown|1fac5||2,3,4,5,6:1fac5-* +fingerprint|1fac6| +moose|1face| +donkey|1facf| +blueberries|1fad0| +bell_pepper|1fad1| +olive|1fad2| +flatbread|1fad3| +tamale|1fad4| +fondue|1fad5| +teapot|1fad6| +pouring_liquid|1fad7| +beans|1fad8| +jar|1fad9| +ginger_root|1fada| +pea_pod|1fadb| +root_vegetable|1fadc| +splatter|1fadf| +melting_face|1fae0| +saluting_face|1fae1| +face_with_open_eyes_and_hand_over_mouth|1fae2| +face_with_peeking_eye|1fae3| +face_with_diagonal_mouth|1fae4| +dotted_line_face|1fae5| +biting_lip|1fae6| +bubbles|1fae7| +shaking_face|1fae8| +face_with_bags_under_eyes|1fae9| +hand_with_index_finger_and_thumb_crossed|1faf0||2,3,4,5,6:1faf0-* +rightwards_hand|1faf1||2,3,4,5,6:1faf1-* +leftwards_hand|1faf2||2,3,4,5,6:1faf2-* +palm_down_hand|1faf3||2,3,4,5,6:1faf3-* +palm_up_hand|1faf4||2,3,4,5,6:1faf4-* +index_pointing_at_the_viewer|1faf5||2,3,4,5,6:1faf5-* +heart_hands|1faf6||2,3,4,5,6:1faf6-* +leftwards_pushing_hand|1faf7||2,3,4,5,6:1faf7-* +rightwards_pushing_hand|1faf8||2,3,4,5,6:1faf8-* +bangbang|203c-fe0f|! +interrobang|2049-fe0f|! +tm|2122-fe0f|! +information_source|2139-fe0f|! +left_right_arrow|2194-fe0f|! +arrow_up_down|2195-fe0f|! +arrow_upper_left|2196-fe0f|! +arrow_upper_right|2197-fe0f|! +arrow_lower_right|2198-fe0f|! +arrow_lower_left|2199-fe0f|! +leftwards_arrow_with_hook|21a9-fe0f|! +arrow_right_hook|21aa-fe0f|! +watch|231a| +hourglass|231b| +keyboard|2328-fe0f|! +eject|23cf-fe0f|! +fast_forward|23e9| +rewind|23ea| +arrow_double_up|23eb| +arrow_double_down|23ec| +black_right_pointing_double_triangle_with_vertical_bar|23ed-fe0f|! +black_left_pointing_double_triangle_with_vertical_bar|23ee-fe0f|! +black_right_pointing_triangle_with_double_vertical_bar|23ef-fe0f|! +alarm_clock|23f0| +stopwatch|23f1-fe0f|! +timer_clock|23f2-fe0f|! +hourglass_flowing_sand|23f3| +double_vertical_bar|23f8-fe0f|! +black_square_for_stop|23f9-fe0f|! +black_circle_for_record|23fa-fe0f|! +m|24c2-fe0f|! +black_small_square|25aa-fe0f|! +white_small_square|25ab-fe0f|! +arrow_forward|25b6-fe0f|! +arrow_backward|25c0-fe0f|! +white_medium_square|25fb-fe0f|! +black_medium_square|25fc-fe0f|! +white_medium_small_square|25fd| +black_medium_small_square|25fe| +sunny|2600-fe0f|! +cloud|2601-fe0f|! +umbrella|2602-fe0f|! +snowman|2603-fe0f|! +comet|2604-fe0f|! +phone,telephone|260e-fe0f|! +ballot_box_with_check|2611-fe0f|! +umbrella_with_rain_drops|2614| +coffee|2615| +shamrock|2618-fe0f|! +point_up|261d-fe0f|!|2,3,4,5,6:261d-* +skull_and_crossbones|2620-fe0f|! +radioactive_sign|2622-fe0f|! +biohazard_sign|2623-fe0f|! +orthodox_cross|2626-fe0f|! +star_and_crescent|262a-fe0f|! +peace_symbol|262e-fe0f|! +yin_yang|262f-fe0f|! +wheel_of_dharma|2638-fe0f|! +white_frowning_face|2639-fe0f|! +relaxed|263a-fe0f|! +female_sign|2640-fe0f|! +male_sign|2642-fe0f|! +aries|2648| +taurus|2649| +gemini|264a| +cancer|264b| +leo|264c| +virgo|264d| +libra|264e| +scorpius|264f| +sagittarius|2650| +capricorn|2651| +aquarius|2652| +pisces|2653| +chess_pawn|265f-fe0f|! +spades|2660-fe0f|! +clubs|2663-fe0f|! +hearts|2665-fe0f|! +diamonds|2666-fe0f|! +hotsprings|2668-fe0f|! +recycle|267b-fe0f|! +infinity|267e-fe0f|! +wheelchair|267f| +hammer_and_pick|2692-fe0f|! +anchor|2693| +crossed_swords|2694-fe0f|! +medical_symbol,staff_of_aesculapius|2695-fe0f|! +scales|2696-fe0f|! +alembic|2697-fe0f|! +gear|2699-fe0f|! +atom_symbol|269b-fe0f|! +fleur_de_lis|269c-fe0f|! +warning|26a0-fe0f|! +zap|26a1| +transgender_symbol|26a7-fe0f|! +white_circle|26aa| +black_circle|26ab| +coffin|26b0-fe0f|! +funeral_urn|26b1-fe0f|! +soccer|26bd| +baseball|26be| +snowman_without_snow|26c4| +partly_sunny|26c5| +thunder_cloud_and_rain|26c8-fe0f|! +ophiuchus|26ce| +pick|26cf-fe0f|! +helmet_with_white_cross|26d1-fe0f|! +chains|26d3-fe0f|! +broken_chain|26d3-fe0f-200d-1f4a5|! +no_entry|26d4| +shinto_shrine|26e9-fe0f|! +church|26ea| +mountain|26f0-fe0f|! +umbrella_on_ground|26f1-fe0f|! +fountain|26f2| +golf|26f3| +ferry|26f4-fe0f|! +boat,sailboat|26f5| +skier|26f7-fe0f|! +ice_skate|26f8-fe0f|! +person_with_ball|26f9-fe0f|!|2,3,4,5,6:26f9-* +woman-bouncing-ball|26f9-fe0f-200d-2640-fe0f|!|2,3,4,5,6:26f9-*-200d-2640-fe0f:! +man-bouncing-ball|26f9-fe0f-200d-2642-fe0f|!|2,3,4,5,6:26f9-*-200d-2642-fe0f:! +tent|26fa| +fuelpump|26fd| +scissors|2702-fe0f|! +white_check_mark|2705| +airplane|2708-fe0f|! +email,envelope|2709-fe0f|! +fist|270a||2,3,4,5,6:270a-* +hand,raised_hand|270b||2,3,4,5,6:270b-* +v|270c-fe0f|!|2,3,4,5,6:270c-* +writing_hand|270d-fe0f|!|2,3,4,5,6:270d-* +pencil2|270f-fe0f|! +black_nib|2712-fe0f|! +heavy_check_mark|2714-fe0f|! +heavy_multiplication_x|2716-fe0f|! +latin_cross|271d-fe0f|! +star_of_david|2721-fe0f|! +sparkles|2728| +eight_spoked_asterisk|2733-fe0f|! +eight_pointed_black_star|2734-fe0f|! +snowflake|2744-fe0f|! +sparkle|2747-fe0f|! +x|274c| +negative_squared_cross_mark|274e| +question|2753| +grey_question|2754| +grey_exclamation|2755| +exclamation,heavy_exclamation_mark|2757| +heavy_heart_exclamation_mark_ornament|2763-fe0f|! +heart|2764-fe0f|! +heart_on_fire|2764-fe0f-200d-1f525|! +mending_heart|2764-fe0f-200d-1fa79|! +heavy_plus_sign|2795| +heavy_minus_sign|2796| +heavy_division_sign|2797| +arrow_right|27a1-fe0f|! +curly_loop|27b0| +loop|27bf| +arrow_heading_up|2934-fe0f|! +arrow_heading_down|2935-fe0f|! +arrow_left|2b05-fe0f|! +arrow_up|2b06-fe0f|! +arrow_down|2b07-fe0f|! +black_large_square|2b1b| +white_large_square|2b1c| +star|2b50| +o|2b55| +wavy_dash|3030-fe0f|! +part_alternation_mark|303d-fe0f|! +congratulations|3297-fe0f|! +secret|3299-fe0f|!` diff --git a/packages/chat/slack/src/internal/errors.ts b/packages/chat/slack/src/internal/errors.ts new file mode 100644 index 00000000000..4c304329965 --- /dev/null +++ b/packages/chat/slack/src/internal/errors.ts @@ -0,0 +1,192 @@ +import { + ChatError, + type ChatErrorReason, + fileRefused as fileRefusedDetail, + Forbidden, + InvalidRequest, + InvalidResponse, + NoAdapter, + NotFound, + PlatformUnavailable, + RateLimited, + TooLarge, + Transport, + Unauthenticated, + Unknown, + Unsupported +} from "effect/chat/ChatError" +import * as Duration from "effect/Duration" +import { platform } from "./address.ts" + +const unauthenticated = new Set([ + "invalid_auth", + "not_authed", + "account_inactive", + "token_revoked", + "token_expired", + "org_login_required" +]) + +const forbidden = new Set([ + "access_denied", + "accesslimited", + "missing_scope", + "not_in_channel", + "is_archived", + "channel_is_archived", + "no_permission", + "not_allowed_token_type", + "not_authorized", + "restricted_action", + "restricted_action_read_only_channel", + "restricted_action_thread_only_channel", + "restricted_action_non_threadable_channel", + "cant_update_message", + "cant_delete_message", + "cant_delete_file", + "edit_window_closed", + "ekm_access_denied", + "enterprise_is_restricted", + "team_access_not_granted", + "posting_to_general_channel_denied", + "message_not_owned_by_app", + "messages_tab_disabled", + "message_limit_exceeded", + "thread_locked", + "user_is_restricted", + "feature_disabled", + "channel_type_not_supported" +]) + +const notFoundCodes = new Set([ + "channel_not_found", + "message_not_found", + "thread_not_found", + "user_not_found", + "users_not_found", + "file_not_found", + "file_deleted", + "bot_not_found", + "team_not_found" +]) + +const tooLarge = new Set(["msg_too_long", "msg_blocks_too_long"]) + +const serverErrors = new Set(["internal_error", "fatal_error", "service_unavailable", "request_timeout"]) + +/** + * Maps the `error` of an `ok: false` answer to a reason. `detail` is always + * Slack's error code, which holds no message text or ids. + * + * @internal + */ +export const reasonForCode = ( + code: string, + options: { readonly write: boolean; readonly retryAfter?: Duration.Duration | undefined } +): ChatErrorReason => { + if (code === "ratelimited" || code === "rate_limited") { + return new RateLimited({ + detail: code, + ...(options.retryAfter === undefined ? {} : { retryAfter: options.retryAfter }) + }) + } + if (unauthenticated.has(code)) return new Unauthenticated({ detail: code }) + if (forbidden.has(code)) return new Forbidden({ detail: code }) + if (notFoundCodes.has(code)) return new NotFound({ detail: code }) + if (tooLarge.has(code)) return new TooLarge({ subject: "text", detail: code }) + if (code === "file_too_large" || code === "too_many_files") return new TooLarge({ subject: "files", detail: code }) + if (serverErrors.has(code)) { + return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail: code }) + } + return new InvalidRequest({ detail: code }) +} + +/** @internal */ +export const reasonForStatus = ( + status: number, + options: { readonly write: boolean; readonly retryAfter?: Duration.Duration | undefined; readonly cause?: unknown } +): ChatErrorReason => { + const detail = `http_${status}` + const cause = options.cause === undefined ? {} : { cause: options.cause } + if (status === 429) { + return new RateLimited({ + detail, + ...cause, + ...(options.retryAfter === undefined ? {} : { retryAfter: options.retryAfter }) + }) + } + if (status >= 500) return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail, ...cause }) + if (status === 401) return new Unauthenticated({ detail, ...cause }) + if (status === 403) return new Forbidden({ detail, ...cause }) + if (status === 404) return new NotFound({ detail, ...cause }) + if (status === 413) return new TooLarge({ detail, ...cause }) + return new InvalidRequest({ detail, ...cause }) +} + +/** A request that failed before an answer came. @internal */ +export const transport = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => + new Transport({ retry: write ? "uncertain" : "retry", detail, ...(cause === undefined ? {} : { cause }) }) + +/** An answer the adapter cannot read. A write may have landed. @internal */ +export const invalidResponse = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => { + const fields = { detail, ...(cause === undefined ? {} : { cause }) } + return write ? new Unknown(fields) : new InvalidResponse(fields) +} + +/** @internal */ +export const parseRetryAfter = (value: string | undefined): Duration.Duration | undefined => { + if (value === undefined) return undefined + const seconds = Number(value) + return Number.isFinite(seconds) && seconds >= 0 ? Duration.seconds(seconds) : undefined +} + +/** @internal */ +export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason }) + +/** @internal */ +export const withOp = (op: string) => (error: ChatError): ChatError => + error.op === op ? error : new ChatError({ platform: error.platform, op, reason: error.reason }) + +/** @internal */ +export const invalidRequest = (detail: string): ChatErrorReason => new InvalidRequest({ detail }) + +/** @internal */ +export const notFound = (detail: string): ChatErrorReason => new NotFound({ detail }) + +/** + * A `denied` reason from an upload call, or from the attach of a post whose + * rollback left nothing behind, as a refused file: the same reason with + * detail `file_refused`, and the reason it replaces, with Slack's code, as + * its `cause`. Any other reason is returned as it is. + * + * @internal + */ +export const fileRefused = (reason: ChatErrorReason): ChatErrorReason => { + if (reason.retry !== "denied" || reason.detail === fileRefusedDetail) return reason + const fields = { detail: fileRefusedDetail, cause: reason } + switch (reason._tag) { + case "Unsupported": + return new Unsupported({ capability: reason.capability, ...fields }) + case "Transport": + return new Transport({ retry: reason.retry, ...fields }) + case "PlatformUnavailable": + return new PlatformUnavailable({ retry: reason.retry, ...fields }) + case "NoAdapter": + return new NoAdapter(fields) + case "Unauthenticated": + return new Unauthenticated(fields) + case "Forbidden": + return new Forbidden(fields) + case "NotFound": + return new NotFound(fields) + case "TooLarge": + return new TooLarge({ ...(reason.subject === undefined ? {} : { subject: reason.subject }), ...fields }) + case "InvalidRequest": + return new InvalidRequest(fields) + case "InvalidResponse": + return new InvalidResponse(fields) + case "RateLimited": + case "Unknown": + return reason + } +} diff --git a/packages/chat/slack/src/internal/escape.ts b/packages/chat/slack/src/internal/escape.ts new file mode 100644 index 00000000000..3c48007fe78 --- /dev/null +++ b/packages/chat/slack/src/internal/escape.ts @@ -0,0 +1,428 @@ +/** + * Escaping for what the adapter sends Slack. + * + * - The Markdown, in a `markdown` block or in `markdown_text`: a `<` + * outside inline and fenced code becomes `<`, unless the scoped notify + * options allow that token. + * - The `text` beside a `markdown` block, from which Slack decides who is + * mentioned: every `<`, `>` and `&` becomes an entity, in code too, and + * only the tokens the block keeps intact stay intact. + * - A stream append, which has no `text`: `escapeStream` puts U+200B after + * whatever spells the `<` of a token shape, in prose and in code alike. + */ + +/** @internal */ +export interface Notify { + readonly users?: ReadonlyArray | undefined + readonly groups?: ReadonlyArray | undefined + readonly broadcast?: boolean | undefined +} + +interface Fence { + readonly char: string + readonly length: number +} + +/** + * Where an escaper left off: the fence open at the start of the current line, + * and the raw text of that line consumed so far. + * + * @internal + */ +export interface State { + readonly fence: Fence | undefined + readonly line: string +} + +/** @internal */ +export const initialState: State = { fence: undefined, line: "" } + +/** @internal */ +export interface Escaped { + /** What to send for the consumed part of the input. */ + readonly sent: string + /** How many characters of the input were consumed. The rest is held. */ + readonly consumed: number + readonly state: State +} + +/** Past this many held characters, an undecided `<` is escaped anyway. */ +const holdBound = 300 + +const tokenPattern = /^<(@([UW][A-Z0-9]+)|!subteam\^([A-Z0-9]+)|!(here|channel|everyone))(\|[^<>]*)?>/ + +const allowsAny = (notify: Notify): boolean => + (notify.users?.length ?? 0) > 0 || (notify.groups?.length ?? 0) > 0 || notify.broadcast === true + +const allowedToken = (rest: string, notify: Notify): number => { + const match = tokenPattern.exec(rest) + if (match === null) return 0 + const [token, , user, group, broadcast] = match + if (user !== undefined) return notify.users?.includes(user) ? token.length : 0 + if (group !== undefined) return notify.groups?.includes(group) ? token.length : 0 + return broadcast !== undefined && notify.broadcast === true ? token.length : 0 +} + +const openingFence = /^ {0,3}(`{3,}|~{3,})(.*)$/ +const closingFence = /^ {0,3}(`{3,}|~{3,})[ \t]*\r?$/ + +const opensFence = (line: string): Fence | undefined => { + const match = openingFence.exec(line) + if (match === null) return undefined + const marker = match[1] + if (marker[0] === "`" && match[2].includes("`")) return undefined + return { char: marker[0], length: marker.length } +} + +const closesFence = (line: string, fence: Fence): boolean => { + const match = closingFence.exec(line) + return match !== null && match[1][0] === fence.char && match[1].length >= fence.length +} + +const backtickRun = (line: string, start: number): number => { + let end = start + while (line[end] === "`") end++ + return end - start +} + +/** + * The code spans of one line, as `[start, end)` pairs, and the position of a + * backtick run that opens a span the line has not closed yet. + */ +const codeSpans = (line: string): { readonly spans: Array; readonly open: number } => { + const spans: Array = [] + let i = 0 + let open = -1 + while (i < line.length) { + if (line[i] !== "`") { + i++ + continue + } + const length = backtickRun(line, i) + let j = i + length + let closed = -1 + while (j < line.length) { + if (line[j] === "`") { + const run = backtickRun(line, j) + if (run === length) { + closed = j + break + } + j += run + } else { + j++ + } + } + if (closed === -1) { + if (open === -1) open = i + i += length + } else { + spans.push([i, closed + length]) + i = closed + length + } + } + return { spans, open } +} + +/** + * Escapes `text`, continuing from `state`. Without `flush`, a `<` whose + * meaning the rest of the stream could still change is held: an unclosed + * token that the notify options might allow, or one after a backtick the line + * has not closed yet. With `flush`, or once the held text passes a bound, + * it is escaped. + * + * @internal + */ +export const escape = ( + text: string, + notify: Notify, + options?: { + readonly state?: State | undefined + readonly flush?: boolean | undefined + /** Write every `<`, `>` and `&` as an entity, apart from allowed tokens. */ + readonly entities?: boolean | undefined + /** Called with the span in the output of each allowed token kept intact. */ + readonly onToken?: ((start: number, end: number) => void) | undefined + } +): Escaped => { + const state = options?.state ?? initialState + const flush = options?.flush === true + const entities = options?.entities === true + const full = state.line + text + const offset = state.line.length + const anyAllowed = allowsAny(notify) + let fence = state.fence + let out = "" + let lineStart = 0 + const emit = (from: number, to: number, piece?: string) => { + if (to <= offset) return + if (piece !== undefined) out += piece + else { + const slice = full.slice(Math.max(from, offset), to) + out += entities ? textEntities(slice) : slice + } + } + while (lineStart <= full.length) { + const newline = full.indexOf("\n", lineStart) + const complete = newline !== -1 + const lineEnd = complete ? newline : full.length + const line = full.slice(lineStart, lineEnd) + if (fence !== undefined) { + emit(lineStart, lineEnd) + if (complete && closesFence(line, fence)) fence = undefined + } else { + const opener = complete ? opensFence(line) : undefined + const { open, spans } = opener === undefined ? codeSpans(line) : { open: -1, spans: [] } + let i = 0 + let span = 0 + while (i < line.length) { + while (span < spans.length && spans[span][1] <= i) span++ + if (span < spans.length && spans[span][0] <= i) { + emit(lineStart + i, lineStart + spans[span][1]) + i = spans[span][1] + continue + } + const next = line.indexOf("<", i) + const stop = next === -1 ? line.length : Math.min(next, span < spans.length ? spans[span][0] : line.length) + if (stop > i) { + emit(lineStart + i, lineStart + stop) + i = stop + continue + } + const at = lineStart + i + const rest = line.slice(i) + const undecided = !flush && !complete && full.length - at <= holdBound && ( + (open !== -1 && open < i) || + (anyAllowed && !rest.includes(">") && /^<(@|!)?[\w^|]*$/.test(rest)) + ) + if (undecided) { + return { + sent: out, + consumed: at - offset, + state: { fence, line: full.slice(lineStart, at) } + } + } + const token = allowedToken(rest, notify) + if (token > 0) { + const start = out.length + emit(at, at + token, at >= offset ? rest.slice(0, token) : undefined) + if (at >= offset) options?.onToken?.(start, out.length) + i += token + } else { + emit(at, at + 1, at >= offset ? "<" : undefined) + i += 1 + } + } + if (opener !== undefined) fence = opener + } + if (!complete) { + return { sent: out, consumed: full.length - offset, state: { fence, line } } + } + emit(lineEnd, lineEnd + 1) + lineStart = lineEnd + 1 + } + return { sent: out, consumed: full.length - offset, state: { fence, line: "" } } +} + +const entityOf: Record = { "&": "&", "<": "<", ">": ">" } + +const textEntities = (text: string): string => text.replace(/[&<>]/g, (char) => entityOf[char]) + +const zeroWidthSpace = "\u200B" + +/** The character references a stream decodes for `<`, `@` and `!`, each only with its semicolon. */ +const references: ReadonlyArray = [ + [/&(?:lt|LT|#0*60|#[xX]0*3[cC]);/y, "<"], + [/&(?:commat|#0*64|#[xX]0*40);/y, "@"], + [/&(?:excl|#0*33|#[xX]0*21);/y, "!"] +] + +const referenceNames = ["lt", "LT", "commat", "excl"] + +/** Whether `text`, from its `&` on, could still grow into one of `references`. */ +const unfinishedReference = (text: string): boolean => { + const body = text.slice(1) + return /^#(?:[0-9]*|[xX][0-9a-fA-F]*)$/.test(body) || referenceNames.some((name) => name.startsWith(body)) +} + +/** The start of a token an allowed one could still complete, label included. */ +const unclosedToken = /^<[@!][\w^]*(?:\|[^<>]*)?$/ + +interface Decoded { + readonly char: string + /** How many characters of the input spell it. */ + readonly length: number +} + +/** + * The character at `i` in the decoded view of `line`, which must be within + * it. A reference may have any number of leading zeros. + */ +const decodeAt = (line: string, i: number): Decoded => { + if (line[i] === "&") { + for (const [pattern, char] of references) { + pattern.lastIndex = i + const match = pattern.exec(line) + if (match !== null) return { char, length: match[0].length } + } + } + return { char: line[i], length: 1 } +} + +/** + * The character at `i` in the decoded view of `line`. `undefined` past + * the end of the text, and `unfinished` where the next chunk could still + * change it. + */ +const decodedAt = (line: string, i: number, ended: boolean): Decoded | "unfinished" | undefined => { + if (i >= line.length) return ended ? undefined : "unfinished" + const decoded = decodeAt(line, i) + return decoded.char === "&" && !ended && unfinishedReference(line.slice(i)) ? "unfinished" : decoded +} + +/** `line` from `i` to its first `>`, or to its end, decoded, with the end in `line` of each decoded character. */ +const decodeToken = (line: string, i: number): { readonly text: string; readonly ends: ReadonlyArray } => { + const close = line.indexOf(">", i) + const end = close === -1 ? line.length : close + 1 + let text = "" + const ends: Array = [] + for (let j = i; j < end;) { + const decoded = decodeAt(line, j) + text += decoded.char + j += decoded.length + ends.push(j) + } + return { text, ends } +} + +/** + * Escapes a stream's Markdown, continuing from `state`. Slack decodes the + * character references for `<`, `@` and `!` in a stream, joins one split + * across two appends, and has no `text` to send beside it, so: + * + * - Token shapes are found on the decoded view, which decodes those + * references only with their semicolon, in prose and in code alike. A + * shape is `<@`, or ` { + const state = options?.state ?? initialState + const flush = options?.flush === true + const full = state.line + text + const offset = state.line.length + const anyAllowed = allowsAny(notify) + let fence = state.fence + let out = "" + let lineStart = 0 + const emit = (from: number, to: number) => { + if (to > offset) out += full.slice(Math.max(from, offset), to) + } + while (lineStart <= full.length) { + const newline = full.indexOf("\n", lineStart) + const complete = newline !== -1 + const lineEnd = complete ? newline : full.length + const line = full.slice(lineStart, lineEnd) + const inFence = fence !== undefined + const opener = !inFence && complete ? opensFence(line) : undefined + const { open, spans } = inFence || opener !== undefined ? { open: -1, spans: [] } : codeSpans(line) + const hold = (i: number): Escaped => { + const at = lineStart + i + return { sent: out, consumed: at - offset, state: { fence, line: full.slice(lineStart, at) } } + } + let i = 0 + let span = 0 + while (i < line.length) { + const next = line.slice(i).search(/[<&]/) + if (next === -1) { + emit(lineStart + i, lineEnd) + break + } + emit(lineStart + i, lineStart + i + next) + i += next + const at = lineStart + i + // What an earlier write sent can't be held. + const ended = complete || flush || at < offset || full.length - at > holdBound + const first = decodedAt(line, i, ended) + if (first === "unfinished") return hold(i) + if (first === undefined || first.char !== "<") { + // An `&` that spells no `<`. What it spells is decided where it is used. + emit(at, at + 1) + i += 1 + continue + } + const second = decodedAt(line, i + first.length, ended) + if (second === "unfinished") return hold(i) + let shape = second?.char === "@" + // Where the text that makes it a shape ends. + let decided = i + first.length + (second?.length ?? 0) + if (second?.char === "!") { + const third = decodedAt(line, decided, ended) + if (third === "unfinished") return hold(i) + shape = third !== undefined && third.length === 1 && /[A-Za-z]/.test(third.char) + decided += third?.length ?? 0 + } + if (shape) { + const token = decodeToken(line, i) + const length = allowedToken(token.text, notify) + if (length > 0) { + emit(at, lineStart + token.ends[length - 1]) + i = token.ends[length - 1] + continue + } + if (!ended && anyAllowed && unclosedToken.test(token.text)) return hold(i) + // A shape an earlier write ended inside, which that write could not see, gets the space at the + // start of this one: Slack reads the writes joined. + if (lineStart + decided > offset) { + emit(at, at + first.length) + out += zeroWidthSpace + } + i += first.length + continue + } + if (first.length > 1) { + // A reference for `<` that starts no token shape is left as written. + emit(at, at + first.length) + i += first.length + continue + } + while (span < spans.length && spans[span][1] <= i) span++ + const inCode = inFence || opener !== undefined || (span < spans.length && spans[span][0] <= i) + if (!inCode && !ended && open !== -1 && open < i) return hold(i) + if (inCode) emit(at, at + 1) + else if (at >= offset) out += "<" + i += 1 + } + if (inFence) { + if (complete && closesFence(line, fence!)) fence = undefined + } else if (opener !== undefined) { + fence = opener + } + if (!complete) { + return { sent: out, consumed: full.length - offset, state: { fence, line } } + } + emit(lineEnd, lineEnd + 1) + lineStart = lineEnd + 1 + } + return { sent: out, consumed: full.length - offset, state: { fence, line: "" } } +} diff --git a/packages/chat/slack/src/internal/interactions.ts b/packages/chat/slack/src/internal/interactions.ts new file mode 100644 index 00000000000..58a334796c9 --- /dev/null +++ b/packages/chat/slack/src/internal/interactions.ts @@ -0,0 +1,417 @@ +/** + * Slack's side of buttons, selects, modals and slash commands: the checks on + * a message's elements, the view a modal opens as, and the interactivity + * payloads and command forms. + */ +import { + type ChatError, + type ChatErrorReason, + interactionExpired, + InvalidRequest, + NotFound, + TooLarge +} from "effect/chat/ChatError" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import * as Actions from "./actions.ts" +import * as Address from "./address.ts" +import * as Errors from "./errors.ts" +import { codePoints } from "./length.ts" + +type Json = Schema.Json +type JsonObject = { readonly [key: string]: Json } + +/** @internal */ +export const limits = { + actionsPerMessage: 25, + actionDataLength: 2_000, + modalMetadataLength: 2_800, + modalFields: 100 +} as const + +/** Slack's limit on an `action_id` and a `callback_id`, in code points. */ +const idLength = 255 +/** Slack's limit on a modal's title. */ +const titleLength = 24 +/** Slack's limit on an input's label. */ +const labelLength = 2_000 + +/** The submit label when the modal has none: Slack needs one on a view with inputs. */ +const defaultSubmitLabel = "Submit" + +/** The handler's share of Slack's 3 seconds. @internal */ +export const defaultDeadline = Duration.millis(2_500) + +/** + * Why a message's elements can't go, if they can't: more than 25, an id + * holding the join or longer than 255, or an id plus a value longer than + * 2,000. All count code points. + * + * @internal + */ +export const checkActions = (actions: ReadonlyArray): ChatErrorReason | undefined => { + if (actions.length > limits.actionsPerMessage) return new TooLarge({ detail: "too_many_actions" }) + for (const action of actions) { + if (action._tag === "LinkButton") continue + if (action.id.includes(Actions.join)) return new InvalidRequest({ detail: "invalid_action_id" }) + const id = codePoints(action.id) + if (id > idLength) return new TooLarge({ detail: "action_id_too_long" }) + const values = action._tag === "Button" ? [action.value ?? ""] : action.options.map((option) => option.value) + if (values.some((value) => id + codePoints(value) > limits.actionDataLength)) { + return new TooLarge({ detail: "action_data_too_long" }) + } + } + return undefined +} + +// Modals + +/** Marks a `private_metadata` the adapter packed. */ +const metadataTag = "chat1" + +/** Where a click or a command happened, which a submit gives back. A command has no message. */ +interface Origin { + readonly channel: string + readonly threadTs?: string | undefined + readonly messageId?: string | undefined +} + +/** + * Consumer metadata is last so it may contain the join delimiter. + */ +const packMetadata = (origin: Origin, metadata: string | undefined): string => + [ + metadataTag, + origin.channel, + origin.threadTs ?? "", + origin.messageId ?? "", + ...(metadata === undefined ? [] : [metadata]) + ].join(Actions.join) + +const unpackMetadata = ( + packed: string +): { readonly origin: Origin; readonly metadata: string | undefined } | undefined => { + const parts = packed.split(Actions.join) + if (parts.length < 4 || parts[0] !== metadataTag || parts[1] === "") return undefined + return { + origin: { + channel: parts[1], + threadTs: parts[2] === "" ? undefined : parts[2], + messageId: parts[3] === "" ? undefined : parts[3] + }, + metadata: parts.length === 4 ? undefined : parts.slice(4).join(Actions.join) + } +} + +const plainText = (text: string) => ({ type: "plain_text", text }) as const + +const inputBlock = (element: ChatUi.TextInput | ChatUi.SelectInput) => ({ + type: "input", + block_id: element.id, + label: plainText(element.label), + optional: element.optional === true, + element: element._tag === "TextInput" + ? { + type: "plain_text_input", + action_id: element.id, + multiline: element.multiline === true, + ...(element.placeholder === undefined ? {} : { placeholder: plainText(element.placeholder) }), + ...(element.initial === undefined ? {} : { initial_value: element.initial }), + ...(element.minLength === undefined ? {} : { min_length: element.minLength }), + ...(element.maxLength === undefined ? {} : { max_length: element.maxLength }) + } + : Actions.elements([{ + _tag: "Select", + id: element.id, + options: element.options, + ...(element.placeholder === undefined ? {} : { placeholder: element.placeholder }), + ...(element.initial === undefined ? {} : { selected: element.initial }) + }])[0] +}) + +/** + * The view that opens `modal` from `from`, or why it can't open. + * + * - Inputs are `input` blocks whose `block_id` is the field id, and a + * `Text` is a `section` of plain text. + * - The modal's id is the `callback_id`. `private_metadata` holds the + * click's channel, thread and message beside the consumer's metadata. For + * a command the message slot is empty. + * - The modal's id plus its metadata is held to 2,800 code points, the id to + * 255, the title to 24 and each label to 2,000. + * + * @internal + */ +export const view = ( + modal: ChatUi.Modal, + from: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatInteraction.CommandInvoked +): { readonly view: Readonly> } | { readonly refusal: ChatErrorReason } => { + const refuse = (refusal: ChatErrorReason) => ({ refusal }) + const id = codePoints(modal.id) + if (id + codePoints(modal.metadata ?? "") > limits.modalMetadataLength) { + return refuse(new TooLarge({ detail: "modal_metadata_too_long" })) + } + if (id > idLength) return refuse(new TooLarge({ detail: "modal_id_too_long" })) + if (modal.elements.length > limits.modalFields) return refuse(new TooLarge({ detail: "too_many_fields" })) + if (codePoints(modal.title) > titleLength) return refuse(new TooLarge({ detail: "title_too_long" })) + const parts = Address.parts(from.thread) + if (Option.isNone(parts)) return refuse(new InvalidRequest({ detail: "invalid_thread" })) + const blocks: Array = [] + for (const element of modal.elements) { + if (element._tag === "Text") { + blocks.push({ type: "section", text: plainText(element.text) }) + continue + } + if (codePoints(element.label) > labelLength) return refuse(new TooLarge({ detail: "label_too_long" })) + blocks.push(inputBlock(element)) + } + return { + view: { + type: "modal", + callback_id: modal.id, + title: plainText(modal.title), + submit: plainText(modal.submitLabel ?? defaultSubmitLabel), + private_metadata: packMetadata( + { ...parts.value, messageId: from._tag === "CommandInvoked" ? undefined : from.messageId }, + modal.metadata + ), + blocks + } + } +} + +// Inbound + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const isJson = Schema.is(Schema.Json) + +const string = (object: JsonObject | undefined, key: string): string | undefined => { + const value = object?.[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +const object = (object: JsonObject | undefined, key: string): JsonObject | undefined => { + const value = object?.[key] + return isObject(value) ? value : undefined +} + +/** The payload without its verification `token`. */ +const raw = (payload: JsonObject): Json => { + const { token: _, ...rest } = payload + return rest +} + +const threadOf = (teamId: string, origin: Origin) => + Address.thread({ + team: teamId, + channel: origin.channel, + threadTs: origin.threadTs === origin.messageId ? undefined : origin.threadTs + }) + +/** An own property, so an id such as `__proto__` keeps its answer. */ +const setOwn = (record: Record, key: string, value: string) => + Object.defineProperty(record, key, { value, enumerable: true, writable: true, configurable: true }) + +const clicked = (payload: JsonObject, teamId: string): Option.Option => { + const actions = payload.actions + const action = Array.isArray(actions) && actions.length === 1 && isObject(actions[0]) ? actions[0] : undefined + const id = string(action, "action_id") + // Only an element in the adapter's own block, and not a link button, whose click carries nothing. + if (action === undefined || id === undefined || action.block_id !== Actions.blockId || id.startsWith(Actions.join)) { + return Option.none() + } + const container = object(payload, "container") + const message = object(payload, "message") + const channel = string(container, "channel_id") ?? string(object(payload, "channel"), "id") + const messageId = string(container, "message_ts") ?? string(message, "ts") + const userId = string(object(payload, "user"), "id") + if (channel === undefined || messageId === undefined || userId === undefined) return Option.none() + const threadTs = string(message, "thread_ts") ?? string(container, "thread_ts") + const origin = { thread: threadOf(teamId, { channel, threadTs, messageId }), messageId, userId, raw: raw(payload) } + if (action.type === "button") { + const value = typeof action.value === "string" ? action.value : undefined + return Option.some(ChatInteraction.ButtonClicked.make({ id, ...(value === undefined ? {} : { value }), ...origin })) + } + const value = object(action, "selected_option")?.value + return action.type === "static_select" && typeof value === "string" + ? Option.some(ChatInteraction.SelectChanged.make({ id, value, ...origin })) + : Option.none() +} + +/** The value of one input in `view.state.values`, or nothing for an empty one. */ +const inputValue = (input: Json): string | undefined => { + if (!isObject(input)) return undefined + const value = input.type === "static_select" ? object(input, "selected_option")?.value : input.value + return typeof value === "string" && value !== "" ? value : undefined +} + +const submitted = (payload: JsonObject, teamId: string): Option.Option => { + const view = object(payload, "view") + const id = string(view, "callback_id") + const packed = string(view, "private_metadata") + const unpacked = packed === undefined ? undefined : unpackMetadata(packed) + const userId = string(object(payload, "user"), "id") + if (id === undefined || unpacked === undefined || userId === undefined) return Option.none() + const values: Record = {} + const state = object(object(view, "state"), "values") ?? {} + for (const blockId of Object.keys(state)) { + const inputs = object(state, blockId) + if (inputs === undefined) continue + for (const input of Object.values(inputs)) { + const value = inputValue(input) + if (value !== undefined) setOwn(values, blockId, value) + } + } + const { metadata, origin } = unpacked + return Option.some(ChatInteraction.ModalSubmitted.make({ + id, + ...(metadata === undefined ? {} : { metadata }), + values, + thread: threadOf(teamId, origin), + ...(origin.messageId === undefined ? {} : { messageId: origin.messageId }), + userId, + raw: raw(payload) + })) +} + +/** Whether `payload` is a slash command's fields: a `command` and no `type`. @internal */ +export const isCommand = (payload: unknown): boolean => + isObject(payload) && payload.type === undefined && typeof payload.command === "string" + +const invoked = (payload: JsonObject, teamId: string): Option.Option => { + const command = string(payload, "command") + const channel = string(payload, "channel_id") + const userId = string(payload, "user_id") + const id = command?.startsWith("/") === true ? command.slice(1) : command + if (id === undefined || id === "" || channel === undefined || userId === undefined) return Option.none() + const text = typeof payload.text === "string" ? payload.text : "" + return Option.some(ChatInteraction.CommandInvoked.make({ + id, + text, + // A command can't be typed in a thread, so its address is the channel's. + thread: Address.thread({ team: teamId, channel }), + userId, + raw: raw(payload) + })) +} + +/** + * The interaction a decoded `payload` is: a click on a button or select in + * an `actions` block the adapter built, a submit of a view it opened, or a + * slash command's fields. Anything else, a link button's click and a value + * that is not JSON included, is none. + * + * @internal + */ +export const normalize = ( + payload: unknown, + context: { readonly teamId: string } +): Option.Option => { + if (!isObject(payload) || !isJson(payload)) return Option.none() + if (payload.type === "block_actions") return clicked(payload, context.teamId) + if (payload.type === "view_submission") return submitted(payload, context.teamId) + if (isCommand(payload)) return invoked(payload, context.teamId) + return Option.none() +} + +/** @internal */ +export interface Api { + readonly call: ( + method: string, + body: Readonly>, + options?: { readonly write?: boolean | undefined; readonly retry?: boolean | undefined } + ) => Effect.Effect +} + +/** A trigger past its 3 seconds is the same refusal `handle` gives a late click. */ +const expired = (error: ChatError): ChatError => + error.reason.detail === "expired_trigger_id" + ? Errors.chatError("openModal", new NotFound({ detail: interactionExpired, cause: error.reason })) + : error + +/** + * `SlackChat.interaction`: normalises, calls the handler through + * `ChatInteraction.handle` and gives the body to answer with. + * + * - An ack, a close, a late click, a deferral and a payload that is not the + * adapter's give `undefined`, an empty answer. + * - `errors` gives `response_action: "errors"`, keyed by field id. + * - A late submit or command gives `Late`. + * - `OpenModal` calls `views.open` with the click's or command's + * `trigger_id` before answering. A modal that can't be built, or that + * Slack refuses, goes to `Handler.refused`, and the click is still acked. + * - `Respond` gives the message as the body, with its `response_type`. One + * that fails the checks of a `post` goes to `Handler.refused` and gives + * an empty answer. + * + * @internal + */ +export const make = (options: { + readonly api: Api + readonly teamId: string + readonly answer: ( + op: string, + content: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined }, + visibility: ChatInteraction.Visibility + ) => Effect.Effect>, ChatError> +}) => +( + payload: unknown, + interactionOptions?: { readonly deadline?: Duration.Duration | undefined } +): Effect.Effect => + Effect.gen(function*() { + const event = normalize(payload, { teamId: options.teamId }) + if (Option.isNone(event)) return undefined + const reply = yield* ChatInteraction.handle(event.value, interactionOptions?.deadline ?? defaultDeadline) + switch (reply._tag) { + case "Late": + // A late click is acked: its handler runs on and `handle` refuses a modal it opens. + return event.value._tag === "ButtonClicked" || event.value._tag === "SelectChanged" ? undefined : reply + case "Ack": + case "Close": + case "Defer": + return undefined + case "Errors": + return { response_action: "errors", errors: reply.errors } + case "Respond": { + const command = event.value as ChatInteraction.CommandInvoked + const handler = yield* ChatInteraction.Handler + return yield* options.answer("respond", reply, reply.visibility).pipe( + // The body is a post's, which is JSON. + Effect.map((body): Json | undefined => body as Json), + Effect.catch((error) => Effect.as(handler.refused(command, error), undefined)) + ) + } + case "OpenModal": { + const click = event.value as + | ChatInteraction.ButtonClicked + | ChatInteraction.SelectChanged + | ChatInteraction.CommandInvoked + const handler = yield* ChatInteraction.Handler + const built = view(reply.modal, click) + if ("refusal" in built) { + yield* handler.refused(click, Errors.chatError("openModal", built.refusal)) + return undefined + } + const triggerId = isObject(payload) ? string(payload, "trigger_id") : undefined + if (triggerId === undefined) { + yield* handler.refused(click, Errors.chatError("openModal", new InvalidRequest({ detail: "no_trigger_id" }))) + return undefined + } + yield* options.api.call("views.open", { trigger_id: triggerId, view: built.view }, { + write: true, + retry: false + }) + .pipe( + Effect.mapError(Errors.withOp("openModal")), + Effect.catch((error) => handler.refused(click, expired(error))) + ) + return undefined + } + } + }) diff --git a/packages/chat/slack/src/internal/length.ts b/packages/chat/slack/src/internal/length.ts new file mode 100644 index 00000000000..232e002b265 --- /dev/null +++ b/packages/chat/slack/src/internal/length.ts @@ -0,0 +1,41 @@ +import type { Action } from "effect/chat/ChatUi" +import * as Actions from "./actions.ts" +import { blocksCost } from "./emoji.ts" + +/** `msg_too_long`: code points of `markdown_text` as sent, inclusive. @internal */ +export const messageLength = 12_000 + +/** @internal */ +export const codePoints = (text: string): number => { + let count = 0 + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i) + if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) { + const low = text.charCodeAt(i + 1) + if (low >= 0xdc00 && low <= 0xdfff) i++ + } + count++ + } + return count +} + +const lessThan = (text: string): number => { + let count = 0 + for (let i = text.indexOf("<"); i !== -1; i = text.indexOf("<", i + 1)) count++ + return count +} + +/** + * What a Markdown string and the elements beside it cost against + * `messageLength`, as an upper bound that adds up. It is the larger of two + * counts: code points plus three for every `<`, which may be sent as + * `<`, and Slack's 13,200 count, scaled to 12,000, of the text with each + * emoji as its shortcode plus the `actions` block's stored JSON. + * + * @internal + */ +export const measure = (markdown: string, actions?: ReadonlyArray): number => + Math.max( + codePoints(markdown) + 3 * lessThan(markdown), + Math.ceil((blocksCost(markdown) + Actions.cost(Actions.elements(actions ?? []))) * 10 / 11) + ) diff --git a/packages/chat/slack/src/internal/mentions.ts b/packages/chat/slack/src/internal/mentions.ts new file mode 100644 index 00000000000..f343b83d862 --- /dev/null +++ b/packages/chat/slack/src/internal/mentions.ts @@ -0,0 +1,225 @@ +import type { Mention } from "effect/chat/ChatMessage" + +type Kind = Mention["kind"] + +interface Target { + readonly kind: Kind + readonly id: string +} + +/** A mention token in `text`, as Slack writes it. */ +const tokenPattern = + /<(@([UW][A-Z0-9]+)|!subteam\^([A-Z0-9]+)|#(C[A-Z0-9]+|G[A-Z0-9]+)|!(here|channel|everyone))(\|[^<>]*)?>/g + +/** + * Parses one token, `<@U123>`, ``, `<#C123|name>` or + * ``, as the whole of `slice`. + * + * @internal + */ +export const parseToken = (slice: string): Target | undefined => { + tokenPattern.lastIndex = 0 + const match = tokenPattern.exec(slice) + if (match === null || match.index !== 0 || match[0].length !== slice.length) return undefined + return targetOf(match) +} + +const targetOf = (match: RegExpExecArray): Target => { + const [, , user, group, channel, broadcast] = match + if (user !== undefined) return { kind: "user", id: user } + if (group !== undefined) return { kind: "role", id: group } + if (channel !== undefined) return { kind: "channel", id: channel } + return { kind: "broadcast", id: broadcast! } +} + +interface Token extends Target { + readonly start: number + readonly end: number +} + +const tokens = (text: string, skipCode: boolean): Array => { + const code: Array = [] + if (skipCode) { + const codePattern = /```[\s\S]*?```|`[^`\n]+`/g + for (let match = codePattern.exec(text); match !== null; match = codePattern.exec(text)) { + code.push([match.index, match.index + match[0].length]) + } + } + const found: Array = [] + tokenPattern.lastIndex = 0 + for (let match = tokenPattern.exec(text); match !== null; match = tokenPattern.exec(text)) { + const start = match.index + if (code.some(([from, to]) => start >= from && start < to)) continue + found.push({ ...targetOf(match), start, end: start + match[0].length }) + } + return found +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +interface Item extends Target { + readonly real: boolean +} + +/** + * The items of a message's `rich_text` blocks that a token in `text` can + * stand for, in order. A `user`, `usergroup`, `channel` or `broadcast` + * element is a real mention, unless it has code style or sits in a + * preformatted section. A token-shaped string in text elements (adjacent + * ones joined) is a literal, as is a code-styled element. `undefined` when + * the message has no `rich_text` block. + */ +const richTextItems = (blocks: unknown): Array | undefined => { + if (!Array.isArray(blocks)) return undefined + const rich = blocks.filter((block) => isObject(block) && block.type === "rich_text") + if (rich.length === 0) return undefined + const items: Array = [] + let pending = "" + const flushText = () => { + if (pending === "") return + for (const token of tokens(pending, false)) items.push({ kind: token.kind, id: token.id, real: false }) + pending = "" + } + const walk = (element: unknown, inCode: boolean): void => { + if (!isObject(element)) return + const code = inCode || element.type === "rich_text_preformatted" + const style = element.style + const literal = code || (isObject(style) && style.code === true) + let target: Target | undefined + switch (element.type) { + case "text": + if (typeof element.text === "string") pending += element.text + return + case "user": + if (typeof element.user_id === "string") target = { kind: "user", id: element.user_id } + break + case "usergroup": + if (typeof element.usergroup_id === "string") target = { kind: "role", id: element.usergroup_id } + break + case "channel": + if (typeof element.channel_id === "string") target = { kind: "channel", id: element.channel_id } + break + case "broadcast": + if (typeof element.range === "string") target = { kind: "broadcast", id: element.range } + break + } + if (target !== undefined) { + flushText() + items.push({ ...target, real: !literal }) + return + } + if (Array.isArray(element.elements)) { + flushText() + for (const child of element.elements) walk(child, code) + flushText() + } + } + for (const block of rich) walk(block, false) + flushText() + return items +} + +const sameTarget = (a: Target, b: Target): boolean => a.kind === b.kind && a.id === b.id + +/** Past this many item and token pairs, the alignment is skipped for the fallback. */ +const maxAlignment = 1_000_000 + +/** + * Aligns the tokens in `text` with the block's items, in order. Every real + * item takes a token it equals, a literal item may take one or be passed + * over, and a token may stand for no item. Of the alignments, the one that + * pairs the most items wins, the earliest pairing first. Gives the token + * index of each real item, or `undefined` when no alignment pairs them all. + */ +const align = (items: ReadonlyArray, found: ReadonlyArray): Array | undefined => { + const n = items.length + const m = found.length + if ((n + 1) * (m + 1) > maxAlignment) return undefined + const impossible = -1 + const width = m + 1 + // best[i * width + j]: the most pairs for items i.. and tokens j.., or + // impossible when some real item among i.. can't be paired. Row n, no + // items left, is all zero. + const best = new Int32Array((n + 1) * width) + const pairs = (i: number, j: number): number => { + if (j === m || !sameTarget(items[i], found[j])) return impossible + const rest = best[(i + 1) * width + j + 1] + return rest === impossible ? impossible : rest + 1 + } + for (let i = n - 1; i >= 0; i--) { + for (let j = m; j >= 0; j--) { + let score = pairs(i, j) + if (!items[i].real) score = Math.max(score, best[(i + 1) * width + j]) + if (j < m) score = Math.max(score, best[i * width + j + 1]) + best[i * width + j] = score + } + } + if (best[0] === impossible) return undefined + const out: Array = [] + let i = 0 + let j = 0 + // Every step moves i or j forward, so this ends after n + m steps. + while (i < n) { + const score = best[i * width + j] + if (pairs(i, j) === score) { + if (items[i].real) out.push(j) + i++ + j++ + } else if (!items[i].real && best[(i + 1) * width + j] === score) { + i++ + } else if (j < m && best[i * width + j + 1] === score) { + j++ + } else { + return undefined + } + } + return out +} + +/** + * The mentions of a message with offsets into `text`. + * + * - With a `rich_text` block, the mentions are its real mention elements + * whose token is in `text`, since Slack decides a mention from `text`. A + * real element with no token there is not reported, however long the + * message. Offsets come from aligning the tokens in `text` with the + * block's items, in order. When no alignment places every real item, + * because `text` has fewer occurrences of a token than the block, each + * real item takes the next unused token it equals, and one with none left + * is not reported. `text` never adds a mention. + * - Without one, `text` is scanned, skipping backtick code spans and fences. + * That covers Block Kit posts, attachment-only posts and system messages. + * + * @internal + */ +export const extract = ( + text: string, + blocks: unknown, + botUserId: string +): Array => { + const toMention = (token: Token): Mention => ({ + kind: token.kind, + id: token.id, + start: token.start, + end: token.end, + isSelf: token.kind === "user" && token.id === botUserId + }) + const items = richTextItems(blocks) + if (items === undefined) return tokens(text, true).map(toMention) + const found = tokens(text, false) + const aligned = align(items, found) + if (aligned !== undefined) return aligned.map((index) => toMention(found[index])) + const used = new Set() + const mentions: Array = [] + let from = 0 + for (const item of items) { + if (!item.real) continue + let index = found.findIndex((token, k) => k >= from && !used.has(k) && sameTarget(item, token)) + if (index < 0) index = found.findIndex((token, k) => !used.has(k) && sameTarget(item, token)) + if (index < 0) continue + used.add(index) + from = index + 1 + mentions.push(toMention(found[index])) + } + return mentions.sort((x, y) => x.start - y.start) +} diff --git a/packages/chat/slack/src/internal/normalize.ts b/packages/chat/slack/src/internal/normalize.ts new file mode 100644 index 00000000000..45c979b785c --- /dev/null +++ b/packages/chat/slack/src/internal/normalize.ts @@ -0,0 +1,237 @@ +import type * as ChatEvent from "effect/chat/ChatEvent" +import type * as ChatMessage from "effect/chat/ChatMessage" +import type * as ChatThread from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import type * as Schema from "effect/Schema" +import { isDirectChannel, thread } from "./address.ts" +import * as Emoji from "./emoji.ts" +import * as Mentions from "./mentions.ts" +import * as RichText from "./richText.ts" + +/** + * The message subtypes that count as a message, for inbound events and for + * every read path alike. Anything else is rejected, so a subtype Slack adds + * later stays out until someone decides about it. + * + * @internal + */ +export const messageSubtypes: ReadonlySet = new Set([ + "file_share", + "thread_broadcast", + "bot_message", + "me_message" +]) + +type JsonObject = { readonly [key: string]: Schema.Json } + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const string = (object: JsonObject, key: string): string | undefined => { + const value = object[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +const tsPattern = /^\d+\.\d+$/ + +/** When a Slack `ts` was sent. @internal */ +export const timestamp = (ts: string): DateTime.Utc => DateTime.makeUnsafe(Math.floor(Number(ts) * 1000)) + +/** @internal */ +export interface MessageContext { + readonly team: string + readonly channel: string + readonly botUserId: string +} + +const authorName = (message: JsonObject): string | undefined => { + const profile = message.user_profile + if (isObject(profile)) { + const name = string(profile, "display_name") ?? string(profile, "real_name") ?? string(profile, "name") + if (name !== undefined) return name + } + const bot = message.bot_profile + if (isObject(bot)) { + const name = string(bot, "name") + if (name !== undefined) return name + } + return string(message, "username") +} + +const attachments = (message: JsonObject): Array => { + const files = message.files + if (!Array.isArray(files)) return [] + const out: Array = [] + for (const file of files) { + if (!isObject(file)) continue + const id = string(file, "id") + if (id === undefined) continue + const name = string(file, "name") + const contentType = string(file, "mimetype") + const size = file.size + out.push({ + id, + ...(name === undefined ? {} : { name }), + ...(contentType === undefined ? {} : { contentType }), + ...(typeof size === "number" && Number.isSafeInteger(size) && size >= 0 ? { size } : {}) + }) + } + return out +} + +/** + * A Slack message payload as a `ChatMessage`, from an event or any read + * path. `undefined` when it is not a message: a subtype off the list, or no + * author. + * + * - The address is the channel when `thread_ts` is missing or equal to + * `ts`, and the thread otherwise. It never depends on later replies. + * - A `bot_message` has `bot_id` and no `user`: `author.id` is the + * `bot_id`, with `isBot` true. + * - `text` is Slack's field as stored, except for a message the bot itself + * wrote that has a `rich_text` block. Its `text` is rendered from the + * blocks as Markdown, and its mentions are the mention elements outside + * code, at the offsets the renderer wrote. The adapter's own `text` is + * often cut, and the blocks hold the whole message. Slack's field stays on + * `raw`. + * + * @internal + */ +export const message = (raw: unknown, context: MessageContext): ChatMessage.ChatMessage | undefined => { + if (!isObject(raw)) return undefined + const subtype = string(raw, "subtype") + if (subtype !== undefined && !messageSubtypes.has(subtype)) return undefined + const ts = string(raw, "ts") + if (ts === undefined || !tsPattern.test(ts)) return undefined + const user = string(raw, "user") + const botId = string(raw, "bot_id") + const authorId = user ?? botId + if (authorId === undefined) return undefined + const threadTs = string(raw, "thread_ts") + const isSelf = user !== undefined && user === context.botUserId + const rendered = isSelf && Array.isArray(raw.blocks) && RichText.hasRichText(raw.blocks) + ? RichText.render(raw.blocks, context.botUserId) + : undefined + const text = rendered?.text ?? (typeof raw.text === "string" ? raw.text : "") + const name = authorName(raw) + return { + id: ts, + thread: thread({ + team: context.team, + channel: context.channel, + threadTs: threadTs === undefined || threadTs === ts ? undefined : threadTs + }), + author: { + id: authorId, + ...(name === undefined ? {} : { name }), + isBot: botId !== undefined || subtype === "bot_message" || user === context.botUserId, + isSelf + }, + text, + mentions: rendered === undefined ? Mentions.extract(text, raw.blocks, context.botUserId) : [...rendered.mentions], + attachments: attachments(raw), + timestamp: timestamp(ts), + raw + } +} + +const kindOf = (address: ChatThread.ChatThread, channel: string, channelType: string | undefined) => { + if (address.id.split("/").length === 3) return "thread" as const + return channelType === "im" || channelType === "mpim" || isDirectChannel(channel) + ? "direct" as const + : "channel" as const +} + +const forWorkspace = (envelope: JsonObject, teamId: string): boolean => { + if (envelope.team_id === teamId) return true + const authorizations = envelope.authorizations + return Array.isArray(authorizations) && + authorizations.some((authorization) => isObject(authorization) && authorization.team_id === teamId) +} + +/** The envelope as `raw`, without the deprecated verification `token`. */ +const envelopeRaw = (envelope: JsonObject): Schema.Json => { + if (!("token" in envelope)) return envelope + const { token: _, ...rest } = envelope + return rest +} + +/** An emoji name as Slack writes it, as Unicode, or `custom:{name}`. @internal */ +export const emoji = (name: string): string => Emoji.fromName(name) ?? `custom:${name}` + +/** + * The events in an `event_callback` envelope. + * + * @internal + */ +export const normalize = ( + envelope: unknown, + context: { readonly teamId: string; readonly botUserId: string } +): ReadonlyArray => { + if (!isObject(envelope) || envelope.type !== "event_callback") return [] + if (!forWorkspace(envelope, context.teamId)) return [] + const event = envelope.event + if (!isObject(event)) return [] + const raw = envelopeRaw(envelope) + const team = context.teamId + switch (event.type) { + case "message": + case "app_mention": { + if (event.type === "app_mention" && event.edited !== undefined) return [] + const channel = string(event, "channel") + if (channel === undefined) return [] + const normalized = message(event, { team, channel, botUserId: context.botUserId }) + if (normalized === undefined) return [] + return [{ + _tag: "MessageCreated", + thread: normalized.thread, + kind: kindOf(normalized.thread, channel, string(event, "channel_type")), + message: normalized, + raw + }] + } + case "reaction_added": + case "reaction_removed": { + const item = event.item + if (!isObject(item) || item.type !== "message") return [] + const channel = string(item, "channel") + const messageId = string(item, "ts") + const userId = string(event, "user") + const name = string(event, "reaction") + if (channel === undefined || messageId === undefined || userId === undefined || name === undefined) return [] + const messageAuthorId = string(event, "item_user") + const eventTs = string(event, "event_ts") + return [{ + _tag: event.type === "reaction_added" ? "ReactionAdded" : "ReactionRemoved", + thread: thread({ team, channel }), + messageId, + ...(messageAuthorId === undefined ? {} : { messageAuthorId }), + userId, + emoji: emoji(name), + ...(eventTs === undefined || !tsPattern.test(eventTs) ? {} : { timestamp: timestamp(eventTs) }), + raw + }] + } + case "agent_session_stopped": { + const channel = string(event, "channel") + const threadTs = string(event, "thread_ts") + const userId = string(event, "user") + const id = string(event, "event_ts") + if (channel === undefined || threadTs === undefined || userId === undefined || id === undefined) return [] + const streams = event.streaming_message_ts + return [{ + _tag: "StopRequested", + thread: thread({ team, channel, threadTs }), + id, + userId, + messageIds: Array.isArray(streams) + ? streams.filter((ts): ts is string => typeof ts === "string" && ts.length > 0) + : typeof streams === "string" && streams.length > 0 + ? [streams] + : [], + raw + }] + } + default: + return [] + } +} diff --git a/packages/chat/slack/src/internal/options.ts b/packages/chat/slack/src/internal/options.ts new file mode 100644 index 00000000000..76ccc9d26ae --- /dev/null +++ b/packages/chat/slack/src/internal/options.ts @@ -0,0 +1,22 @@ +import * as Context from "effect/Context" + +/** @internal */ +export type SessionStatus = "active" | "processing" | "suspended" | "closed" + +/** @internal */ +export interface PostOptions { + readonly notify?: { + readonly users?: ReadonlyArray | undefined + readonly groups?: ReadonlyArray | undefined + readonly broadcast?: boolean | undefined + } | undefined + readonly unfurl?: boolean | undefined + readonly nativeStream?: boolean | undefined + readonly recipientTeamId?: string | undefined + readonly sessionStatusAtEnd?: SessionStatus | undefined +} + +/** @internal */ +export const CurrentPostOptions = Context.Reference("@effect/chat-slack/CurrentPostOptions", { + defaultValue: () => ({}) +}) diff --git a/packages/chat/slack/src/internal/richText.ts b/packages/chat/slack/src/internal/richText.ts new file mode 100644 index 00000000000..02a574ea0f7 --- /dev/null +++ b/packages/chat/slack/src/internal/richText.ts @@ -0,0 +1,351 @@ +/** + * Renders a message's `rich_text` blocks as Markdown, for the `text` of the + * bot's own messages. Slack stores the adapter's `markdown` block as + * `rich_text`, and the stored `text` is the adapter's `text`, which may be + * cut. Rendering the blocks gives the whole message back, with its newlines. + * + * The renderer is total: an element it does not know gives its `text`, or + * nothing. + */ +import type { Mention } from "effect/chat/ChatMessage" +import * as Emoji from "./emoji.ts" + +type JsonObject = { readonly [key: string]: unknown } + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const stringOf = (object: JsonObject, key: string): string | undefined => { + const value = object[key] + return typeof value === "string" ? value : undefined +} + +const elementsOf = (object: JsonObject): ReadonlyArray => Array.isArray(object.elements) ? object.elements : [] + +/** @internal */ +export const hasRichText = (blocks: unknown): boolean => + Array.isArray(blocks) && blocks.some((block) => isObject(block) && block.type === "rich_text") + +interface Style { + readonly bold: boolean + readonly italic: boolean + readonly strike: boolean + readonly code: boolean +} + +const plain: Style = { bold: false, italic: false, strike: false, code: false } + +const styleOf = (element: JsonObject): Style => { + const style = element.style + if (!isObject(style)) return plain + return { + bold: style.bold === true, + italic: style.italic === true, + strike: style.strike === true, + code: style.code === true + } +} + +const sameStyle = (a: Style, b: Style) => + a.bold === b.bold && a.italic === b.italic && a.strike === b.strike && a.code === b.code + +const opening = (style: Style): string => + (style.bold ? "**" : "") + (style.italic ? "*" : "") + (style.strike ? "~~" : "") + +const closing = (style: Style): string => + (style.strike ? "~~" : "") + (style.italic ? "*" : "") + (style.bold ? "**" : "") + +const longestRun = (text: string, char: string): number => { + let longest = 0 + let run = 0 + for (const c of text) { + run = c === char ? run + 1 : 0 + if (run > longest) longest = run + } + return longest +} + +/** One element's content: plain text, or a mention token. */ +type Piece = + | { readonly _tag: "text"; readonly text: string; readonly markdown?: true } + | { readonly _tag: "mention"; readonly token: string; readonly kind: Mention["kind"]; readonly id: string } + +const emojiText = (element: JsonObject): string => { + const unicode = stringOf(element, "unicode") + if (unicode !== undefined && /^[0-9a-f]+(-[0-9a-f]+)*$/i.test(unicode)) { + const points = unicode.split("-").map((hex) => parseInt(hex, 16)) + if (points.every((point) => point <= 0x10ffff)) return String.fromCodePoint(...points) + } + const name = stringOf(element, "name") + if (name === undefined) return "" + const tone = element.skin_tone + const full = typeof tone === "number" ? `${name}::skin-tone-${tone}` : name + return Emoji.fromName(full) ?? Emoji.fromName(name) ?? `:${full}:` +} + +const pieceOf = (element: JsonObject): Piece => { + switch (element.type) { + case "text": + return { _tag: "text", text: stringOf(element, "text") ?? "" } + case "link": { + const url = stringOf(element, "url") ?? "" + const text = stringOf(element, "text") + // Slack stores a bare URL as a link whose text is the URL without its scheme. + const bare = text === undefined || text === "" || text === url || + text === url.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "") + return { _tag: "text", text: bare ? url : `[${escapeInline(text)}](${url})`, markdown: true } + } + case "user": { + const id = stringOf(element, "user_id") + if (id !== undefined) return { _tag: "mention", token: `<@${id}>`, kind: "user", id } + break + } + case "usergroup": { + const id = stringOf(element, "usergroup_id") + if (id !== undefined) return { _tag: "mention", token: ``, kind: "role", id } + break + } + case "channel": { + const id = stringOf(element, "channel_id") + if (id !== undefined) return { _tag: "mention", token: `<#${id}>`, kind: "channel", id } + break + } + case "broadcast": { + const id = stringOf(element, "range") + if (id !== undefined) return { _tag: "mention", token: ``, kind: "broadcast", id } + break + } + case "emoji": + return { _tag: "text", text: emojiText(element) } + case "date": + return { _tag: "text", text: stringOf(element, "fallback") ?? "" } + case "color": + return { _tag: "text", text: stringOf(element, "value") ?? "" } + } + return { _tag: "text", text: stringOf(element, "text") ?? "" } +} + +const pieceText = (piece: Piece): string => piece._tag === "text" ? piece.text : piece.token + +const wordChar = /[\p{L}\p{N}]/u +const asciiPunctuation = /[!-/:-@[-`{-~]/ + +/** + * Backslash-escapes what Markdown would read as inline syntax, so a text + * element's `*literal*` reads back literal. An underscore between two + * letters or digits opens nothing and is left alone, as is a backslash + * before anything but punctuation or a line end. `<` and `&` are left + * alone: the adapter sends them as literal text either way. + */ +const escapeInline = (text: string): string => + text.replace(/[*_`~[\]\\]/g, (char, at: number) => { + const before = text[at - 1] ?? "" + const after = text[at + 1] ?? "" + if (char === "_" && wordChar.test(before) && wordChar.test(after)) return char + if (char === "\\" && after !== "" && after !== "\n" && !asciiPunctuation.test(after)) return char + return "\\" + char + }) + +/** A line start that would open a heading, list or quote, or underline the line before it. */ +const blockStart = /^( {0,3})(?:([#]{1,6}|[-+])(?=[ \t]|$)|(>)|(\d{1,9})([.)])(?=[ \t]|$)|([-=])(?=[-= \t]*$))/ + +const escapeLineStart = (line: string): string => + line.replace( + blockStart, + (_, indent: string, marker?: string, quote?: string, digits?: string, delimiter?: string, rule?: string) => + marker !== undefined + ? indent + "\\" + marker + : quote !== undefined + ? indent + "\\>" + : digits !== undefined + ? indent + digits + "\\" + delimiter + : indent + "\\" + rule + ) + +/** A text element's content as Markdown that reads back as the same text. */ +const literal = (text: string, atLineStart: boolean): string => + escapeInline(text) + .split("\n") + .map((line, index) => index > 0 || atLineStart ? escapeLineStart(line) : line) + .join("\n") + +/** + * Markdown that renders `text` as written, as the start of a message. + * + * @internal + */ +export const escapeMarkdown = (text: string): string => literal(text, true) + +interface Writer { + out: string + readonly mentions: Array + readonly botUserId: string +} + +const write = (writer: Writer, text: string): void => { + writer.out += text +} + +/** Writes a piece outside code: a link as built, other text escaped, a mention as its token. */ +const writePiece = (writer: Writer, piece: Piece, text = pieceText(piece)): void => { + if (piece._tag === "mention") return mention(writer, piece) + if (piece.markdown === true) return write(writer, text) + write(writer, literal(text, writer.out === "" || writer.out.endsWith("\n"))) +} + +const mention = (writer: Writer, piece: Extract): void => { + const start = writer.out.length + writer.out += piece.token + writer.mentions.push({ + kind: piece.kind, + id: piece.id, + start, + end: writer.out.length, + isSelf: piece.kind === "user" && piece.id === writer.botUserId + }) +} + +/** Starts a new line unless the output is empty or already ends one. */ +const newline = (writer: Writer): void => { + if (writer.out !== "" && !writer.out.endsWith("\n")) writer.out += "\n" +} + +/** + * Inline elements, grouped into runs of one style so markers wrap a run and + * not each element. Whitespace at either end of a styled run goes outside + * its markers, where Markdown needs it. Code is a span, and a token in it is + * a literal. + */ +const inline = (writer: Writer, elements: ReadonlyArray): void => { + const items = elements.filter(isObject).map((element) => ({ piece: pieceOf(element), style: styleOf(element) })) + let i = 0 + while (i < items.length) { + const style = items[i].style + let j = i + 1 + while (j < items.length && sameStyle(items[j].style, style)) j++ + const run = items.slice(i, j).map((item) => item.piece) + i = j + if (style.code) { + const content = run.map(pieceText).join("") + const fence = "`".repeat(longestRun(content, "`") + 1) + // A backtick at either end would join the fence, and Markdown strips + // one space from each end of a span that starts and ends with one. + const pad = content.startsWith("`") || content.endsWith("`") || + (content.startsWith(" ") && content.endsWith(" ") && content.trim() !== "") + ? " " + : "" + write(writer, opening(style) + fence + pad + content + pad + fence + closing(style)) + continue + } + const open = opening(style) + if (open === "") { + for (const piece of run) writePiece(writer, piece) + continue + } + const first = run[0] + const last = run[run.length - 1] + const lead = first._tag === "text" ? /^\s*/.exec(first.text)![0] : "" + const trail = last._tag === "text" ? /\s*$/.exec(last.text)![0] : "" + if (run.length === 1 && first._tag === "text" && lead.length === first.text.length) { + writePiece(writer, first) + continue + } + write(writer, lead + open) + run.forEach((piece, k) => { + let text = pieceText(piece) + if (k === 0) text = text.slice(lead.length) + if (k === run.length - 1) text = text.slice(0, text.length - trail.length) + writePiece(writer, piece, text) + }) + write(writer, closing(style) + trail) + } +} + +/** Prefixes each line written since `from`, and moves the mentions in it to match. */ +const prefixLines = (writer: Writer, from: number, first: string, rest: string): void => { + const body = writer.out.slice(from) + const lines = body.split("\n") + let rewritten = "" + const shifts: Array = [] + let position = from + lines.forEach((line, index) => { + const prefix = index === 0 ? first : rest + shifts.push([position, prefix.length]) + rewritten += (index === 0 ? "" : "\n") + prefix + line + position += line.length + 1 + }) + writer.out = writer.out.slice(0, from) + rewritten + // Adjust mention offsets for inserted prefixes. + for (let m = 0; m < writer.mentions.length; m++) { + const mention = writer.mentions[m] + if (mention.start < from) continue + let shift = 0 + for (const [at, length] of shifts) if (at <= mention.start) shift += length + writer.mentions[m] = { ...mention, start: mention.start + shift, end: mention.end + shift } + } +} + +const block = (writer: Writer, element: unknown): void => { + if (!isObject(element)) return + switch (element.type) { + case "rich_text_section": + return inline(writer, elementsOf(element)) + case "rich_text_preformatted": { + // A fence holds no mention: every token is literal. + const content = elementsOf(element).filter(isObject).map((child) => pieceText(pieceOf(child))).join("") + const fence = "`".repeat(Math.max(3, longestRun(content, "`") + 1)) + write(writer, fence + "\n" + content + (content.endsWith("\n") ? "" : "\n") + fence) + return + } + case "rich_text_quote": { + const from = writer.out.length + inline(writer, elementsOf(element)) + return prefixLines(writer, from, "> ", "> ") + } + case "rich_text_list": { + const indent = " ".repeat(typeof element.indent === "number" ? Math.max(0, element.indent) : 0) + const ordered = element.style === "ordered" + const offset = typeof element.offset === "number" ? element.offset : 0 + elementsOf(element).forEach((item, index) => { + if (index > 0) write(writer, "\n") + const marker = ordered ? `${offset + index + 1}. ` : "- " + const from = writer.out.length + block(writer, item) + prefixLines(writer, from, indent + marker, indent + " ".repeat(marker.length)) + }) + return + } + } + const text = stringOf(element, "text") + if (text !== undefined) write(writer, text) +} + +/** + * The Markdown of a message's blocks, with the mentions at the offsets the + * renderer wrote. A mention is a `user`, `usergroup`, `channel` or + * `broadcast` element outside code. Blocks other than `rich_text` give + * their `text` when it is a string, and nothing otherwise. + * + * @internal + */ +export const render = (blocks: ReadonlyArray, botUserId: string): { + readonly text: string + readonly mentions: ReadonlyArray +} => { + const writer: Writer = { out: "", mentions: [], botUserId } + for (const item of blocks) { + if (!isObject(item)) continue + if (item.type === "rich_text") { + for (const element of elementsOf(item)) { + newline(writer) + block(writer, element) + } + } else { + const text = stringOf(item, "text") + if (text !== undefined && text !== "") { + newline(writer) + write(writer, text) + } + } + } + return { text: writer.out, mentions: writer.mentions } +} diff --git a/packages/chat/slack/src/internal/text.ts b/packages/chat/slack/src/internal/text.ts new file mode 100644 index 00000000000..de740e892f5 --- /dev/null +++ b/packages/chat/slack/src/internal/text.ts @@ -0,0 +1,252 @@ +/** + * The `text` the adapter sends beside a `markdown` block on + * `chat.postMessage` and `chat.update`. Slack decides who a message + * mentions from this field, not from the blocks, so the adapter always + * builds it itself: Slack's own would carry every token intact. + */ +import type { Action } from "effect/chat/ChatUi" +import * as Actions from "./actions.ts" +import * as Escape from "./escape.ts" +import { codePoints } from "./length.ts" +import { escapeMarkdown } from "./richText.ts" + +/** + * The budget for `text` on `chat.postMessage`, in code points. Slack cuts a + * longer one there without an error, and the cut can split an entity or a + * token. + * + * @internal + */ +export const postTextLength = 16_000 + +/** + * The budget for `text` on `chat.update`, in UTF-8 bytes. Slack refuses a + * `text` over 4,000 bytes with `msg_too_long`. + * + * Provisional: whether Slack checks the 4,000 before or after it rewrites the + * text (it wraps a URL in `<…>`) is not known yet, so this keeps a reserve. + * + * @internal + */ +export const updateTextBytes = 3_800 + +/** @internal */ +export interface Budget { + readonly unit: "codePoints" | "utf8" + readonly limit: number +} + +/** @internal */ +export const postBudget: Budget = { unit: "codePoints", limit: postTextLength } + +/** @internal */ +export const updateBudget: Budget = { unit: "utf8", limit: updateTextBytes } + +/** @internal */ +export const utf8Bytes = (text: string): number => { + let count = 0 + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i) + if (code < 0x80) count += 1 + else if (code < 0x800) count += 2 + else if (code >= 0xd800 && code <= 0xdbff && i + 1 < text.length) { + const low = text.charCodeAt(i + 1) + if (low >= 0xdc00 && low <= 0xdfff) { + count += 4 + i++ + } else { + count += 3 + } + } else { + // A lone surrogate goes out as U+FFFD, which is three bytes too. + count += 3 + } + } + return count +} + +/** @internal */ +export const cost = (text: string, unit: Budget["unit"]): number => unit === "utf8" ? utf8Bytes(text) : codePoints(text) + +const marker = "…" +const separator = " " + +let segmenter: Intl.Segmenter | undefined + +const entity = /&(?:lt|gt|amp);/g + +/** @internal */ +export interface Built { + readonly text: string + /** Allowed tokens that a cut had no room for. */ + readonly dropped: number +} + +/** + * Builds the `text` for a Markdown body and cuts it to `budget`. + * + * - Every `<`, `>` and `&` is an entity, in code too. The tokens the notify + * options allow, outside code, stay intact, as they do in the block. + * - Past the budget, the text is cut on a grapheme cluster boundary that + * splits no entity and no token, and ends in `…`. Each allowed token not + * in the kept part is appended once, in order, after a space. The marker + * and the spaces come out of the budget. + * - When the tokens alone don't fit, those that fit are kept and the rest + * counted in `dropped`. + * + * @internal + */ +export const build = (markdown: string, notify: Escape.Notify, budget: Budget): Built => { + const spans: Array = [] + const text = Escape.escape(markdown, notify, { + flush: true, + entities: true, + onToken: (start, end) => spans.push([start, end]) + }).sent + const tokens = spans.map(([start, end]) => text.slice(start, end)) + if (cost(text, budget.unit) <= budget.limit) return { text, dropped: 0 } + + // Offsets inside an entity or a token, where no cut may fall. + const inside = new Uint8Array(text.length + 1) + const atomic = (start: number, end: number) => inside.fill(1, start + 1, end) + for (const [start, end] of spans) atomic(start, end) + entity.lastIndex = 0 + for (let match = entity.exec(text); match !== null; match = entity.exec(text)) { + atomic(match.index, match.index + match[0].length) + } + + // The tail is the marker plus each distinct token not yet in the head. + const distinct = [...new Set(tokens)] + const unit = budget.unit + const tokenCost = (token: string) => cost(separator + token, unit) + let tail = cost(marker, unit) + for (const token of distinct) tail += tokenCost(token) + + // Every code point boundary outside an entity or a token where the head + // and the tail fit, from the start until the head alone is over. + const candidates: Array = [] + const inHead = new Set() + let head = 0 + let span = 0 + let i = 0 + for (;;) { + for (; span < spans.length && spans[span][1] <= i; span++) { + const token = tokens[span] + if (!inHead.has(token)) { + inHead.add(token) + tail -= tokenCost(token) + } + } + if (inside[i] === 0 && head + tail <= budget.limit) candidates.push(i) + if (i >= text.length) break + const code = text.charCodeAt(i) + const pair = code >= 0xd800 && code <= 0xdbff && i + 1 < text.length && + text.charCodeAt(i + 1) >= 0xdc00 && text.charCodeAt(i + 1) <= 0xdfff + // A lone surrogate goes out as U+FFFD, three bytes. + head += unit === "codePoints" ? 1 : pair ? 4 : code < 0x80 ? 1 : code < 0x800 ? 2 : 3 + i += pair ? 2 : 1 + if (head > budget.limit) break + } + + // The last of them on a grapheme cluster boundary. + segmenter ??= new Intl.Segmenter(undefined, { granularity: "grapheme" }) + let best = -1 + let candidate = 0 + // Iterate boundaries: Bun's containing() reports incorrect offsets for astral text. + for (const { index } of segmenter.segment(text)) { + while (candidate < candidates.length && candidates[candidate] < index) candidate++ + if (candidate === candidates.length) break + if (candidates[candidate] === index) best = index + } + + if (best >= 0) { + const kept = text.slice(0, best) + let out = kept + marker + for (const token of distinct) if (!kept.includes(token)) out += separator + token + return { text: out, dropped: 0 } + } + // Not even the tokens fit: keep those that do, in order. + let out = marker + let used = cost(marker, unit) + let dropped = 0 + for (const token of distinct) { + const extra = tokenCost(token) + if (used + extra <= budget.limit) { + out += separator + token + used += extra + } else { + dropped++ + } + } + return { text: out, dropped } +} + +/** @internal */ +export interface Body { + /** The Markdown as sent in the block, for the length check. */ + readonly sent: string + readonly blocks: ReadonlyArray + readonly text: string + readonly dropped: number +} + +/** + * The content of a `chat.postMessage` or `chat.update`: the Markdown as one + * `markdown` block, escaped as `markdown_text` was, then the elements as + * one `actions` block, and the `text` from `build`. + * + * With elements and no Markdown there is no `markdown` block, and `text` + * is the button labels joined by a space, escaped. Slack's own fallback, + * which appends the word "button" to each label, never appears. + * + * @internal + */ +export const body = ( + markdown: string, + notify: Escape.Notify, + budget: Budget, + actions: ReadonlyArray = [] +): Body => { + if (markdown === "" && actions.length > 0) { + const built = build(Actions.labels(actions), {}, budget) + return withActions({ sent: "", blocks: [], text: built.text, dropped: built.dropped }, actions) + } + const sent = Escape.escape(markdown, notify, { flush: true }).sent + const built = build(markdown, notify, budget) + return withActions( + { sent, blocks: [{ type: "markdown", text: sent }], text: built.text, dropped: built.dropped }, + actions + ) +} + +/** + * `body` with the elements' `actions` block last, when there are any. + * + * @internal + */ +export const withActions = (body: Body, actions: ReadonlyArray): Body => + actions.length === 0 ? body : { ...body, blocks: [...body.blocks, Actions.block(actions)] } + +/** + * Whether `body` has an `actions` block, which is content enough without + * `text`. + * + * @internal + */ +export const hasActions = (body: Body): boolean => + body.blocks.some((block) => (block as { readonly type?: unknown }).type === "actions") + +/** + * The content of a post with files and no Markdown: the file names joined by + * ", ", escaped as Markdown in the block so they render as written, and with + * entities in `text`. No token in a name is allowed. It stays as the + * message. + * + * @internal + */ +export const placeholder = (names: ReadonlyArray, budget: Budget): Body => { + const joined = names.join(", ") + const sent = Escape.escape(escapeMarkdown(joined), {}, { flush: true }).sent + const built = build(joined, {}, budget) + return { sent, blocks: [{ type: "markdown", text: sent }], text: built.text, dropped: built.dropped } +} diff --git a/packages/chat/slack/src/internal/webhook.ts b/packages/chat/slack/src/internal/webhook.ts new file mode 100644 index 00000000000..4420de7893e --- /dev/null +++ b/packages/chat/slack/src/internal/webhook.ts @@ -0,0 +1,224 @@ +import type { ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import type { NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Clock from "effect/Clock" +import * as Mac from "effect/crypto/Mac" +import type * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as HttpServerResponse from "effect/http/HttpServerResponse" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import { normalize } from "./normalize.ts" + +/** How far a request's timestamp may be from now, as Slack recommends. */ +const maxSkewSeconds = 60 * 5 + +const encoder = new TextEncoder() + +/** + * Checks Slack's `v0` request signature: an HMAC-SHA256 of + * `v0:{timestamp}:{body}` with the signing secret, and a timestamp within + * five minutes of now. + * + * @internal + */ +export const makeVerifier = Effect.fnUntraced(function*(signingSecret: Redacted.Redacted) { + const mac = yield* Mac.Mac + // Invalid signing secrets are configuration defects, not route errors. + const key = yield* Mac.key(Mac.hmacSha256, encoder.encode(Redacted.value(signingSecret))).pipe(Effect.orDie) + return (headers: { readonly [key: string]: string | undefined }, body: Uint8Array): Effect.Effect => + Effect.gen(function*() { + const timestamp = headers["x-slack-request-timestamp"] + const signature = headers["x-slack-signature"] + if (timestamp === undefined || signature === undefined || !/^\d+$/.test(timestamp)) return false + if (!/^v0=(?:[0-9a-fA-F]{2})+$/.test(signature)) return false + const hex = signature.slice(3) + const tag = new Uint8Array(hex.length / 2) + for (let i = 0; i < tag.length; i++) tag[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16) + const now = yield* Clock.currentTimeMillis + if (Math.abs(now / 1000 - Number(timestamp)) > maxSkewSeconds) return false + const prefix = encoder.encode(`v0:${timestamp}:`) + const message = new Uint8Array(prefix.length + body.length) + message.set(prefix) + message.set(body, prefix.length) + // Crypto failures fail closed, like a mismatched signature. + return yield* mac.verify(key, tag, message).pipe(Effect.catch(() => Effect.succeed(false))) + }) +}) + +/** The body, or `None` once it passes `maxBytes`. @internal */ +export const readBody = ( + request: HttpServerRequest.HttpServerRequest, + maxBytes: number +): Effect.Effect> => { + const length = request.headers["content-length"] + if (length !== undefined && Number(length) > maxBytes) return Effect.succeed(Option.none()) + return Effect.gen(function*() { + const chunks: Array = [] + let total = 0 + const tooLarge = yield* request.stream.pipe( + Stream.takeUntil((chunk) => { + total += chunk.length + chunks.push(chunk) + return total > maxBytes + }), + Stream.runDrain, + Effect.as(false), + Effect.catch(() => Effect.succeed(true)) + ) + if (tooLarge || total > maxBytes) return Option.none() + const body = new Uint8Array(total) + let offset = 0 + for (const chunk of chunks) { + body.set(chunk, offset) + offset += chunk.length + } + return Option.some(body) + }) +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** An own property, so a field such as `__proto__` stays a field. */ +const setOwn = (record: Record, key: string, value: string) => + Object.defineProperty(record, key, { value, enumerable: true, writable: true, configurable: true }) + +const ok = HttpServerResponse.empty({ status: 200 }) +const status = (code: number) => HttpServerResponse.empty({ status: code }) + +/** + * What is logged with each 503 of the Events API route. + * + * @internal + */ +export const unavailable = { + botUser: "slack webhook: the bot user could not be read, answered 503", + notAccepted: "slack webhook: the handler refused the event, answered 503", + deadline: "slack webhook: the request passed its deadline, answered 503" +} as const + +/** @internal */ +export interface Options { + readonly verify: (headers: { readonly [key: string]: string | undefined }, body: Uint8Array) => Effect.Effect + readonly appId: string + readonly teamId: string + readonly botUserId: Effect.Effect + readonly handle: (event: ChatEvent.ChatEvent) => Effect.Effect + readonly deadline: Duration.Duration + readonly maxBodyBytes: number +} + +/** + * One Events API request. + * + * - 413 past the body limit, 401 for a bad signature, 400 for a body that + * is not JSON. + * - `url_verification` is answered with its challenge. + * - An envelope for another app or workspace, or of another type, is + * answered 200 and dropped, so Slack does not retry it. + * - 503 when the bot user can't be read, the handler fails `NotAccepted`, + * or the whole request (including its body and verification) takes longer + * than the deadline. Slack then retries. Each is logged with its own + * message, since the status alone does not say which it was. + * + * @internal + */ +export const handleRequest = ( + request: HttpServerRequest.HttpServerRequest, + options: Options +): Effect.Effect => + Effect.gen(function*() { + const body = yield* readBody(request, options.maxBodyBytes) + if (Option.isNone(body)) return status(413) + if (!(yield* options.verify(request.headers, body.value))) return status(401) + let envelope: unknown + try { + envelope = JSON.parse(new TextDecoder().decode(body.value)) + } catch { + return status(400) + } + if (!isObject(envelope)) return status(400) + if (envelope.type === "url_verification") { + return typeof envelope.challenge === "string" + ? HttpServerResponse.text(envelope.challenge) + : status(400) + } + if (envelope.type !== "event_callback" || envelope.api_app_id !== options.appId) return ok + const callback = envelope + const botUserId = yield* Effect.catch( + Effect.asSome(options.botUserId), + (error) => Effect.as(Effect.logError(unavailable.botUser, error), Option.none()) + ) + if (Option.isNone(botUserId)) return status(503) + const events = normalize(callback, { teamId: options.teamId, botUserId: botUserId.value }) + return yield* Effect.forEach(events, options.handle, { discard: true }).pipe( + Effect.as(ok), + Effect.catchTag("NotAccepted", () => Effect.as(Effect.logWarning(unavailable.notAccepted), status(503))) + ) + }).pipe( + Effect.timeoutOption(options.deadline), + Effect.flatMap(Option.match({ + onNone: () => Effect.as(Effect.logWarning(unavailable.deadline), status(503)), + onSome: Effect.succeed + })) + ) + +/** @internal */ +export interface InteractivityOptions { + readonly verify: Options["verify"] + readonly appId: string + readonly interaction: ( + payload: unknown, + options: { readonly deadline: Duration.Duration } + ) => Effect.Effect + readonly deadline: Duration.Duration + readonly maxBodyBytes: number +} + +const isLate = Schema.is(ChatInteraction.Late) + +/** + * One Interactivity request: a form-encoded body whose `payload` field + * holds the JSON, or a slash command's, whose fields are the payload. + * + * - 413 past the body limit, 401 for a bad signature, 400 for a body with + * neither a `payload` that is a JSON object nor a `command`. + * - A payload or command for another app is answered 200 and dropped. + * - `Late` is answered 503, which keeps a modal open. Anything else is a + * 200, with the body as JSON when there is one. + * + * @internal + */ +export const handleInteractivity = ( + request: HttpServerRequest.HttpServerRequest, + options: InteractivityOptions +): Effect.Effect => + Effect.gen(function*() { + const body = yield* readBody(request, options.maxBodyBytes) + if (Option.isNone(body)) return status(413) + if (!(yield* options.verify(request.headers, body.value))) return status(401) + const form = new URLSearchParams(new TextDecoder().decode(body.value)) + const field = form.get("payload") + let payload: unknown + if (field === null && form.has("command")) { + // A slash command: its fields are the payload. A repeated field keeps its first value. + const fields: Record = {} + for (const [key, value] of form) if (!Object.hasOwn(fields, key)) setOwn(fields, key, value) + payload = fields + } else { + try { + payload = field === null ? undefined : JSON.parse(field) + } catch { + return status(400) + } + } + if (!isObject(payload)) return status(400) + if (payload.api_app_id !== options.appId) return ok + const answer = yield* options.interaction(payload, { deadline: options.deadline }) + if (answer === undefined) return ok + return isLate(answer) ? status(503) : HttpServerResponse.jsonUnsafe(answer) + }) diff --git a/packages/chat/slack/test/SlackChat.test.ts b/packages/chat/slack/test/SlackChat.test.ts new file mode 100644 index 00000000000..f2d2b3b5cfa --- /dev/null +++ b/packages/chat/slack/test/SlackChat.test.ts @@ -0,0 +1,726 @@ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { SlackWebApi } from "@effect/chat-slack/SlackWebApi" +import { assert, describe, it } from "@effect/vitest" +import { checkMessage } from "effect/chat/ChatTest" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import * as Option from "effect/Option" +import * as Stream from "effect/Stream" +import * as TestClock from "effect/testing/TestClock" +import { + adapter, + authTest, + botUserId, + channel, + checkOptions, + fixture, + hang, + makeFake, + slackLayer, + team +} from "./utils.ts" + +const root = "1794509110.999799" +const channelThread = SlackChat.thread({ team, channel }) +const replyThread = SlackChat.thread({ team, channel, threadTs: root }) + +const run = ( + fake: ReturnType, + effect: Effect.Effect +) => effect.pipe(Effect.provide(slackLayer(fake))) as Effect.Effect + +const posted = (ts: string) => ({ ok: true, channel, ts }) + +const reason = (effect: Effect.Effect) => + Effect.flip(effect).pipe( + Effect.map((error) => ({ op: error.op, tag: error.reason._tag, detail: error.reason.detail, retry: error.retry })) + ) + +describe("post", () => { + it.effect("sends a markdown block and an escaped text, with unfurls off", () => { + const fake = makeFake({ "chat.postMessage": posted("1794509200.000100") }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.post(replyThread, { markdown: "hi <@U1> `<@U1>` & >" }) + assert.deepStrictEqual(fake.called("chat.postMessage")[0].params, { + channel, + thread_ts: root, + blocks: JSON.stringify([{ type: "markdown", text: "hi <@U1> `<@U1>` & >" }]), + text: "hi <@U1> `<@U1>` & >", + unfurl_links: "false", + unfurl_media: "false" + }) + }) + ) + }) + + it.effect("withPostOptions allows a token and turns unfurls on", () => { + const fake = makeFake({ "chat.postMessage": posted("1794509200.000101") }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.post(channelThread, { markdown: "hi <@U1> <@U2>" }).pipe( + SlackChat.withPostOptions({ notify: { users: ["U1"] } }), + SlackChat.withPostOptions({ unfurl: true }) + ) + const params = fake.called("chat.postMessage")[0].params + assert.deepStrictEqual(JSON.parse(params.blocks), [{ type: "markdown", text: "hi <@U1> <@U2>" }]) + assert.strictEqual(params.text, "hi <@U1> <@U2>") + assert.strictEqual(params.unfurl_links, "true") + }) + ) + }) + + it.effect("never sends an empty text: a post or edit with no Markdown fails before the call", () => { + const fake = makeFake({}) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + assert.deepStrictEqual(yield* reason(slack.post(channelThread, { markdown: "" })), { + op: "post", + tag: "InvalidRequest", + detail: "no_text", + retry: "denied" + }) + assert.strictEqual((yield* reason(slack.edit(channelThread, "1.2", { markdown: "" }))).detail, "no_text") + assert.strictEqual(fake.calls.length, 0) + }) + ) + }) + + it.effect("leaves the emoji count to Slack", () => { + const fake = makeFake({ + "chat.postMessage": [posted("1794509200.000100"), { ok: false, error: "msg_blocks_too_long" }] + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + // 14,000 if `<` cost 4, 11,000 as Slack counts it. + yield* slack.post(channelThread, { markdown: "😀".repeat(1_000) + "<".repeat(1_000) }) + // 1,400 😀 are 14,000 with shortcodes: Slack refuses, nothing is posted. + const failure = yield* reason(slack.post(channelThread, { markdown: "😀".repeat(1_400) })) + assert.deepStrictEqual(failure, { op: "post", tag: "TooLarge", detail: "msg_blocks_too_long", retry: "denied" }) + }) + ) + }) +}) + +describe("buttons and selects", () => { + const button = { _tag: "Button", id: "go", label: "Go ", value: "v" } as const + + it.effect("with no Markdown, the text is the escaped labels", () => { + const fake = makeFake({ "chat.postMessage": posted("1794509200.000100") }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.post(channelThread, { markdown: "", actions: [button] }) + assert.strictEqual(fake.called("chat.postMessage")[0].params.text, "Go <!channel>") + }) + ) + }) + + it.effect("an id holding the adapter's separator is refused before any call", () => { + const fake = makeFake({}) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const failure = yield* reason( + slack.post(channelThread, { markdown: "x", actions: [{ ...button, id: "a\u241Fb" }] }) + ) + assert.deepStrictEqual([failure.tag, failure.detail], ["InvalidRequest", "invalid_action_id"]) + assert.deepStrictEqual(fake.calls, []) + }) + ) + }) +}) + +describe("files", () => { + const upload = (ids: ReadonlyArray) => { + let next = 0 + return { + "files.getUploadURLExternal": () => { + const id = ids[next++] + return { ok: true, upload_url: `https://files.slack.test/upload/${id}`, file_id: id } + }, + "files.completeUploadExternal": { ok: true, files: ids.map((id) => ({ id })) } + } + } + const file = (name: string) => ({ name, contentType: "text/plain", content: new TextEncoder().encode("hello") }) + const ts = "1794509300.000100" + + it.effect("uploads, completes with no channel, posts, then attaches with the same body", () => { + const fake = makeFake({ ...upload(["F1", "F2"]), "chat.postMessage": posted(ts) }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.post(replyThread, { markdown: "a".repeat(5_000), files: [file("a.txt"), file("b.txt")] }) + assert.deepStrictEqual( + fake.calls.map((call) => call.method).filter((method) => !method.startsWith("/upload/")), + [ + "files.getUploadURLExternal", + "files.getUploadURLExternal", + "files.completeUploadExternal", + "chat.postMessage", + "chat.update" + ] + ) + assert.deepStrictEqual(fake.called("files.completeUploadExternal")[0].params, { + files: JSON.stringify([{ id: "F1", title: "a.txt" }, { id: "F2", title: "b.txt" }]) + }) + const post = fake.called("chat.postMessage")[0].params + assert.deepStrictEqual(fake.called("chat.update")[0].params, { + channel, + ts, + blocks: post.blocks, + text: post.text, + file_ids: JSON.stringify(["F1", "F2"]) + }) + // The text is cut to the update budget from the post on. + assert.isAtMost(new TextEncoder().encode(post.text).length, 3_800) + }) + ) + }) + + it.effect("a post with files and no Markdown carries the escaped file names", () => { + const fake = makeFake({ ...upload(["F1", "F2"]), "chat.postMessage": posted(ts) }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.post(channelThread, { markdown: "", files: [file("*a*.txt"), file("<@U1>.csv")] }) + const post = fake.called("chat.postMessage")[0].params + assert.deepStrictEqual(JSON.parse(post.blocks), [{ type: "markdown", text: "\\*a\\*.txt, <@U1>.csv" }]) + assert.strictEqual(post.text, "*a*.txt, <@U1>.csv") + }) + ) + }) + + it.effect("a long Retry-After on the attach is cut off by its deadline and rolled back, even when interrupted", () => { + const fake = makeFake({ + ...upload(["F1"]), + "chat.postMessage": posted(ts), + "chat.update": { status: 429, headers: { "retry-after": "3600" } } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const fiber = yield* Effect.forkChild(slack.post(channelThread, { markdown: "x", files: [file("a")] })) + yield* TestClock.adjust("1 second") + const interrupting = yield* Effect.forkChild(Fiber.interrupt(fiber)) + yield* TestClock.adjust("61 seconds") + yield* Fiber.join(interrupting) + assert.strictEqual(fake.called("chat.update").length, 1) + assert.deepStrictEqual(fake.called("chat.delete")[0].params, { channel, ts }) + assert.deepStrictEqual(fake.called("files.delete").map((call) => call.params.file), ["F1"]) + }) + ) + }) + + it.effect("a rollback that can't delete the message fails uncertain with SlackAttachFailed", () => { + const fake = makeFake({ + ...upload(["F1", "F2"]), + "chat.postMessage": posted(ts), + "chat.update": { ok: false, error: "file_not_found" }, + "chat.delete": { ok: false, error: "cant_delete_message" } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const error = yield* Effect.flip(slack.post(replyThread, { markdown: "x", files: [file("a"), file("b")] })) + assert.deepStrictEqual( + [error.reason._tag, error.reason.detail, error.retry], + ["Unknown", "attach_failed_message_left", "uncertain"] + ) + const cause = error.reason.cause + assert.instanceOf(cause, SlackChat.SlackAttachFailed) + assert.deepStrictEqual(cause.thread, replyThread) + assert.strictEqual(cause.messageId, ts) + assert.deepStrictEqual(cause.fileIds, ["F1", "F2"]) + // The files are kept, so the ids it carries still mean something. + assert.strictEqual(fake.called("files.delete").length, 0) + }) + ) + }) +}) + +describe("errors", () => { + it.effect("maps Slack's answers, and never repeats a write that may have landed", () => { + const fake = makeFake({ + "chat.update": { ok: false, error: "invalid_auth" }, + "chat.postMessage": { status: 500 }, + "conversations.info": { status: 429, headers: { "retry-after": "7" } } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + assert.deepStrictEqual(yield* reason(slack.edit(channelThread, "1.2", { markdown: "x" })), { + op: "edit", + tag: "Unauthenticated", + detail: "invalid_auth", + retry: "denied" + }) + assert.deepStrictEqual(yield* reason(slack.post(channelThread, { markdown: "x" })), { + op: "post", + tag: "PlatformUnavailable", + detail: "http_500", + retry: "uncertain" + }) + assert.strictEqual(fake.called("chat.postMessage").length, 1) + const api = yield* SlackWebApi + const limited = yield* Effect.flip(api.call("conversations.info", { channel }, { retry: false })) + assert.strictEqual(limited.reason._tag, "RateLimited") + assert.deepStrictEqual(limited.retryAfter, Duration.seconds(7)) + }) + ) + }) +}) + +describe("reads", () => { + it.effect("fetchMessage normalises what Slack stored", () => { + const replies = fixture("markdown-mentions").calls.find((call) => call.method === "conversations.replies")! + .response + const fake = makeFake({ "auth.test": authTest, "conversations.replies": replies }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const message = yield* slack.fetchMessage(channelThread, "1794509003.143939") + yield* checkMessage(message, checkOptions) + assert.strictEqual(message.mentions.length, 2) + // The thread's channel, with both bounds at the message, inclusive, or Slack leaves the message out. + const { channel: requested, inclusive, latest, oldest } = fake.called("conversations.replies")[0].params + assert.deepStrictEqual({ channel: requested, latest, oldest, inclusive }, { + channel, + latest: "1794509003.143939", + oldest: "1794509003.143939", + inclusive: "true" + }) + }) + ) + }) + + it.effect("thread history pages in both directions, from exclusive", () => { + const message = (ts: string) => ({ ts, thread_ts: root, user: "U080YLYB277", text: ts }) + const fake = makeFake({ + "auth.test": authTest, + "conversations.replies": [ + { + ok: true, + messages: [message(root), message("1794509111.000001")], + response_metadata: { next_cursor: "page2" } + }, + { ok: true, messages: [message(root), message("1794509112.000001"), message("1794509113.000001")] } + ] + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const forward = yield* Stream.runCollect(slack.history(replyThread, { direction: "forward" })) + assert.deepStrictEqual(forward.map((m) => m.id), [ + root, + "1794509111.000001", + "1794509112.000001", + "1794509113.000001" + ]) + const backward = yield* Stream.runCollect( + slack.history(replyThread, { direction: "backward", from: "1794509113.000001" }) + ) + assert.deepStrictEqual(backward.map((m) => m.id), ["1794509112.000001", root]) + }) + ) + }) + + it.effect("getThread", () => { + const fake = makeFake({ + "conversations.info": { + ok: true, + channel: { id: channel, name: "chat-testing-123", created: 1794400000, topic: { value: "tests" } } + } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const info = yield* slack.getThread(channelThread) + assert.deepStrictEqual([info.kind, info.name, info.topic], ["channel", "chat-testing-123", "tests"]) + const thread = yield* slack.getThread(replyThread) + assert.strictEqual(thread.kind, "thread") + assert.deepStrictEqual(thread.parent, channelThread) + }) + ) + }) +}) + +describe("delete", () => { + it.effect("stops an open stream first", () => { + const fake = makeFake({ + "chat.delete": [{ ok: false, error: "cant_delete_message" }, { ok: true }], + "chat.stopStream": { ok: true } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.delete(replyThread, "1794509111.134189") + assert.deepStrictEqual(fake.calls.map((call) => call.method), ["chat.delete", "chat.stopStream", "chat.delete"]) + }) + ) + }) +}) + +describe("nativeStream", () => { + const ts = "1794509111.134189" + + it.effect("is unsupported off a thread, without a recipient, or when turned off", () => + run( + makeFake({}), + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + const cases = [ + native.start(channelThread, { markdown: "x", recipient: "U1" }), + native.start(replyThread, { markdown: "x" }), + native.start(replyThread, { markdown: "x", recipient: "U1" }).pipe( + SlackChat.withPostOptions({ nativeStream: false }) + ) + ] + for (const start of cases) assert.strictEqual((yield* reason(start)).tag, "Unsupported") + }) + )) + + it.effect("closed, too large and stopped", () => { + const fake = makeFake({ + "chat.startStream": posted(ts), + "chat.appendStream": [ + { ok: false, error: "message_not_in_streaming_state" }, + { ok: false, error: "stopped_by_user" } + ], + "agents.sessions.setStatus": { ok: true } + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "a".repeat(11_990), recipient: "U1" }) + assert.strictEqual( + (yield* reason(native.append(replyThread, ts, "b".repeat(11), { flush: true }))).tag, + "TooLarge" + ) + assert.strictEqual(fake.called("chat.appendStream").length, 0) + assert.deepStrictEqual(yield* native.append(replyThread, ts, "c", { flush: true }), { + state: "closed", + held: 0 + }) + const suspended = SlackChat.withPostOptions({ sessionStatusAtEnd: "suspended" }) + assert.deepStrictEqual(yield* native.append(replyThread, ts, "d", { flush: true }).pipe(suspended), { + state: "stopped", + held: 0 + }) + // The layer sends no stop after a stopped append, so the append moves the session off processing. + assert.deepStrictEqual(fake.called("agents.sessions.setStatus").map((call) => call.params), [{ + channel_id: channel, + thread_ts: root, + status: "suspended" + }]) + }) + ) + }) + + it.effect("rotates with processing, and stops asking once Slack says not_authorized", () => { + const fake = makeFake({ + "chat.stopStream": (params) => + params.session_status !== undefined + ? { ok: false, error: "not_authorized" } + : { ok: true, message: { ts, ai_context: { result_status: "stopped_by_user" } } }, + "agents.sessions.setStatus": { ok: false, error: "not_authorized" } + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + assert.deepStrictEqual(yield* native.stop(replyThread, ts, { end: "rotating" }), { stopped: true }) + assert.deepStrictEqual( + fake.called("chat.stopStream").map((call) => call.params.session_status), + ["processing", undefined] + ) + // Not a declared agent: no more session status, and a reply never fails over it. + assert.deepStrictEqual(yield* native.stop(replyThread, ts, { end: "done" }), { stopped: true }) + assert.strictEqual(fake.called("chat.stopStream").length, 3) + assert.isUndefined(fake.called("chat.stopStream")[2].params.session_status) + assert.strictEqual(fake.called("agents.sessions.setStatus").length, 0) + }) + ) + }) + + it.effect("after a 429 the reply's next part waits, then doubles its interval, and another reply does not", () => { + const next = "1794509201.000100" + const other = "1794509202.000100" + const fake = makeFake({ + "chat.appendStream": [{ status: 429, headers: { "retry-after": "3" } }, posted(other), posted(next)], + "chat.stopStream": { ok: true, message: { ts } }, + "chat.startStream": [posted(other), posted(next)] + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + const recipient = "U080YLYB277" + assert.strictEqual((yield* reason(native.append(replyThread, ts, "a", { flush: true }))).tag, "RateLimited") + yield* native.stop(replyThread, ts, { end: "rotating" }) + // Another reply in the thread starts and appends at once. + yield* native.start(replyThread, { markdown: "x", recipient }) + yield* native.append(replyThread, other, "y", { flush: true }) + assert.strictEqual(fake.called("chat.appendStream").length, 2) + // The reply's next part waits for the retry-after. + const start = yield* Effect.forkChild(native.start(replyThread, { markdown: "b", recipient, continues: ts })) + yield* TestClock.adjust("2999 millis") + assert.strictEqual(fake.called("chat.startStream").length, 1) + yield* TestClock.adjust("1 millis") + assert.strictEqual((yield* Fiber.join(start)).id, next) + // Its appends keep the doubled interval, two seconds. + const append = yield* Effect.forkChild(native.append(replyThread, next, "c", { flush: true })) + yield* TestClock.adjust("1999 millis") + assert.strictEqual(fake.called("chat.appendStream").length, 2) + yield* TestClock.adjust("1 millis") + yield* Fiber.join(append) + assert.strictEqual(fake.called("chat.appendStream").length, 3) + }) + ) + }) + + it.effect("an edit retried after a 429 keeps the doubled interval", () => { + const fake = makeFake({ + "chat.update": [{ status: 429, headers: { "retry-after": "1" } }, { ok: true }, { ok: true }] + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + const first = yield* Effect.forkChild(slack.edit(replyThread, ts, { markdown: "a" })) + yield* TestClock.adjust("1 second") + yield* Fiber.join(first) + assert.strictEqual(fake.called("chat.update").length, 2) + // Six seconds, twice the three second floor. + const second = yield* Effect.forkChild(slack.edit(replyThread, ts, { markdown: "ab" })) + yield* TestClock.adjust("5999 millis") + assert.strictEqual(fake.called("chat.update").length, 2) + yield* TestClock.adjust("1 millis") + yield* Fiber.join(second) + assert.strictEqual(fake.called("chat.update").length, 3) + }) + ) + }) + + it.effect("edits to one message are three seconds apart", () => { + const other = "1794509200.000200" + const fake = makeFake({ "chat.update": { ok: true } }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* adapter + yield* slack.edit(replyThread, ts, { markdown: "a" }) + const second = yield* Effect.forkChild(slack.edit(replyThread, ts, { markdown: "ab" })) + // Another message is not held back. + yield* slack.edit(replyThread, other, { markdown: "x" }) + yield* TestClock.adjust("2999 millis") + assert.strictEqual(fake.called("chat.update").length, 2) + yield* TestClock.adjust("1 millis") + yield* Fiber.join(second) + assert.strictEqual(fake.called("chat.update").length, 3) + }) + ) + }) + + describe("the cleanup edit", () => { + const z = "\u200B" + const recipient = "U080YLYB277" + + it.effect("rewrites a stream whose appends differ from the block, once, after the stop, with no actions", () => { + const fake = makeFake({ "chat.startStream": posted(ts), "chat.appendStream": posted(ts) }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "Hi <@U2> ", recipient }) + yield* native.append(replyThread, ts, "and `<@U3>` a < b", { flush: true }) + assert.strictEqual(fake.called("chat.startStream")[0].params.markdown_text, `Hi <${z}@U2> `) + assert.strictEqual(fake.called("chat.appendStream")[0].params.markdown_text, `and \`<${z}@U3>\` a < b`) + yield* native.stop(replyThread, ts, { end: "done" }) + assert.deepStrictEqual(fake.calls.map((call) => call.method).slice(-2), ["chat.stopStream", "chat.update"]) + assert.deepStrictEqual( + fake.called("chat.update").map((call): Record => ({ + ...call.params, + blocks: JSON.parse(call.params.blocks) + })), + [{ + channel, + ts, + blocks: [{ type: "markdown", text: "Hi <@U2> and `<@U3>` a < b" }], + text: "Hi <@U2> and `<@U3>` a < b" + }] + ) + }) + ) + }) + + it.effect("runs again on a stop sent after one interrupted during it", () => { + const fake = makeFake({ + "chat.startStream": posted(ts), + "chat.stopStream": [{ ok: true, result_status: "stopped_by_user" }, { + ok: false, + error: "message_not_in_streaming_state" + }], + "chat.update": [hang, posted(ts)], + "agents.sessions.setStatus": { ok: true } + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "<@U2>", recipient }) + const first = yield* Effect.forkChild(native.stop(replyThread, ts, { end: "done" })) + yield* TestClock.adjust("1 millis") + assert.strictEqual(fake.called("chat.update").length, 1) + yield* Fiber.interrupt(first) + // Slack answers this one as not streaming, so the user's stop is known from the first. + assert.deepStrictEqual(yield* native.stop(replyThread, ts, { end: "done" }), { stopped: true }) + assert.strictEqual(fake.called("chat.update").length, 2) + assert.strictEqual(fake.called("agents.sessions.setStatus").length, 1) + }) + ) + }) + + it.effect("runs when an append finds the stream stopped by the user, and only once", () => { + const fake = makeFake({ + "chat.startStream": posted(ts), + "chat.appendStream": { ok: false, error: "stopped_by_user" }, + "chat.stopStream": { ok: false, error: "message_not_in_streaming_state" } + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "a <@U2>", recipient }) + yield* native.append(replyThread, ts, " b", { flush: true }) + assert.deepStrictEqual(fake.called("chat.update").map((call) => JSON.parse(call.params.blocks)), [ + [{ type: "markdown", text: "a <@U2>" }] + ]) + assert.deepStrictEqual(yield* native.stop(replyThread, ts, { end: "done" }), { stopped: true }) + assert.strictEqual(fake.called("chat.update").length, 1) + }) + ) + }) + + it.effect("runs, with the session status, on the stops after that append is cut off", () => { + const fake = makeFake({ + "chat.startStream": posted(ts), + "chat.appendStream": { ok: false, error: "stopped_by_user" }, + "chat.stopStream": { ok: false, error: "message_not_in_streaming_state" }, + "chat.update": [hang, posted(ts)], + "agents.sessions.setStatus": [hang, { ok: true }] + }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "a <@U2>", recipient }) + // Cut off during the cleanup. + const appended = yield* Effect.forkChild(native.append(replyThread, ts, " b", { flush: true })) + yield* TestClock.adjust("1 millis") + yield* Fiber.interrupt(appended) + assert.strictEqual(fake.called("chat.update").length, 1) + assert.strictEqual(fake.called("agents.sessions.setStatus").length, 0) + // The finaliser's stop, cut off during the session status. + const stopping = yield* Effect.forkChild(native.stop(replyThread, ts, { end: "done" })) + yield* TestClock.adjust("1 millis") + yield* Fiber.interrupt(stopping) + assert.strictEqual(fake.called("chat.update").length, 2) + assert.strictEqual(fake.called("agents.sessions.setStatus").length, 1) + assert.deepStrictEqual(yield* native.stop(replyThread, ts, { end: "done" }), { stopped: true }) + assert.deepStrictEqual(fake.called("agents.sessions.setStatus").map((call) => call.params), [ + { channel_id: channel, thread_ts: root, status: "active" }, + { channel_id: channel, thread_ts: root, status: "active" } + ]) + // A stop sent again sets it no third time. + yield* native.stop(replyThread, ts, { end: "done" }) + assert.strictEqual(fake.called("agents.sessions.setStatus").length, 2) + assert.strictEqual(fake.called("chat.update").length, 2) + }) + ) + }) + }) + + it.effect("holds a trailing high surrogate and sends any other lone surrogate as U+FFFD", () => { + const fake = makeFake({ "chat.startStream": posted(ts), "chat.appendStream": posted(ts) }) + return run( + fake, + Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + yield* native.start(replyThread, { markdown: "x\udc00", recipient: "U1" }) + assert.deepStrictEqual(yield* native.append(replyThread, ts, "a\ud83d", undefined), { state: "open", held: 1 }) + assert.deepStrictEqual(yield* native.append(replyThread, ts, "\ud83d", undefined), { state: "open", held: 1 }) + // With an unclosed < as well, the longer tail. + assert.deepStrictEqual(yield* native.append(replyThread, ts, "\ud83d\ude00 <\ud83d", undefined), { + state: "open", + held: 2 + }) + assert.deepStrictEqual(yield* native.append(replyThread, ts, "<\ud83d", { flush: true }), { + state: "open", + held: 0 + }) + assert.strictEqual(fake.called("chat.startStream")[0].params.markdown_text, "x\ufffd") + assert.deepStrictEqual( + fake.called("chat.appendStream").map((call) => call.params.markdown_text), + ["a", "😀 ", "<\ufffd"] + ) + }) + ) + }) +}) + +describe("SlackChat", () => { + it.effect("botUserId and user", () => { + const fake = makeFake({ + "auth.test": authTest, + "users.info": { + ok: true, + user: { + id: "U0EXT", + team_id: "T0OTHER", + name: "ext", + is_restricted: true, + is_stranger: true, + profile: { display_name: "Ext" } + } + } + }) + return run( + fake, + Effect.gen(function*() { + const slack = yield* SlackChat.SlackChat + assert.strictEqual(yield* slack.botUserId, botUserId) + const user = yield* slack.user("U0EXT") + assert.include(user, { id: "U0EXT", teamId: "T0OTHER", name: "Ext", isRestricted: true, isStranger: true }) + }) + ) + }) + + it("emoji in both directions", () => { + assert.deepStrictEqual(SlackChat.emojiFromName(":+1::skin-tone-3:"), Option.some("👍🏼")) + assert.deepStrictEqual(SlackChat.emojiToName("👍"), Option.some("+1")) + assert.deepStrictEqual(SlackChat.emojiToName("❤"), Option.some("heart")) + assert.isTrue(Option.isNone(SlackChat.emojiFromName("party-parrot"))) + }) +}) diff --git a/packages/chat/slack/test/actions.test.ts b/packages/chat/slack/test/actions.test.ts new file mode 100644 index 00000000000..05e79bb89cf --- /dev/null +++ b/packages/chat/slack/test/actions.test.ts @@ -0,0 +1,66 @@ +/** + * `actionsCost` against rows measured in EFF-3738, one per charge it makes: + * each row is the `actions` block's elements as posted, and what the 13,200 + * cap charged for it, found by bisecting the text beside it. + */ +import { cost, type Element, option } from "@effect/chat-slack/internal/actions" +import { assert, describe, it } from "@effect/vitest" + +const opts = (n: number, label = (i: number) => "Option " + i, value = (i: number) => "o" + i) => + Array.from({ length: n }, (_, i) => option(label(i + 1), value(i + 1))) + +const S = (o: { + readonly id?: string + readonly ph?: string | null + readonly options?: ReadonlyArray> + readonly initial?: ReturnType +} = {}): Element => ({ + type: "static_select", + action_id: o.id ?? "sel1", + ...(o.ph === null ? {} : { placeholder: { type: "plain_text", text: o.ph ?? "Pick one" } }), + options: o.options ?? opts(3), + ...(o.initial === undefined ? {} : { initial_option: o.initial }) +}) + +const B = (o: { + readonly id?: string + readonly label?: string + readonly url?: string + readonly value?: string | null +} = {}): Element => ({ + type: "button", + text: { type: "plain_text", text: o.label ?? "Test button" }, + action_id: o.id ?? "eff3738_noop", + ...(o.url === undefined ? {} : { url: o.url }), + ...(o.value === null ? {} : { value: o.value ?? "v" }) +}) + +const one = (label: string) => opts(1, () => label, () => "a") + +/** Name, elements, what Slack charged, and whether every string in it is ASCII. */ +const rows: ReadonlyArray, number, boolean]> = [ + ["V1", [S({ options: one("A"), ph: null })], 173, true], + ["V2", [S({ options: one("A") })], 240, true], + ["V6", [S({ options: opts(10, () => "A", () => "a") })], 843, true], + ["V10", [S({ options: opts(3), initial: option("Option 2", "o2") })], 490, true], + ["V15", [B(), S({ options: opts(3) }), B({ id: "b2" })], 628, true], + ["V18", [S({ options: one("é".repeat(20)) })], 359, false], + ["V20", [B()], 170, true], + ["V21", [B({ url: "https://effect.website/docs", value: null })], 197, true], + ["V22", [B({ url: "https://effect.website/docs", value: "v" })], 209, true], + ["W1", [B({ label: "é".repeat(20) })], 279, false], + ["W2", [B({ label: "\u{1F644}" })], 183, false], + ["W4", [S({ options: one("A"), ph: "\u{1F644}" })], 256, false], + ["W8", [B({ label: "<&>".repeat(10) })], 289, true], + ["W13", [S({ options: one("\u{1D49C}".repeat(5)) })], 299, false] +] + +describe("actionsCost", () => { + it("is never below what Slack charged, and at most 5 over on ASCII, for the measured rows", () => { + for (const [name, elements, charged, ascii] of rows) { + const charge = cost(elements) + assert.isAtLeast(charge, charged, name) + if (ascii) assert.isAtMost(charge, charged + 5, name) + } + }) +}) diff --git a/packages/chat/slack/test/commands.test.ts b/packages/chat/slack/test/commands.test.ts new file mode 100644 index 00000000000..032fcfaf66d --- /dev/null +++ b/packages/chat/slack/test/commands.test.ts @@ -0,0 +1,120 @@ +/** + * Slash commands against what Slack sent and what it took, recorded on + * janitor2 in test-janitor during EFF-3802: `fixtures/recorded/commands.json`. + * Slack's signature on these requests was not verified, see the fixture's + * provenance. + */ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import { authTest, fixture, makeFake, slackLayer, team } from "./utils.ts" + +const recorded = fixture("commands") +const channel = "C0CJMJ5UMUY" + +// The definitions the capture ran, from eff-3802-capture.ts. +const actions = [ + ChatInteraction.button("cap-ack", { label: "Ack" }).of(), + ChatInteraction.button("cap-open", { label: "Open modal", style: "primary" }).of() +] + +/** The capture's handler, by the command's text. */ +const reply = (command: ChatInteraction.CommandInvoked): ChatInteraction.CommandReply => { + switch (command.text) { + case "public": + return ChatInteraction.respond({ + markdown: "**Public answer** with _Markdown_ and buttons", + actions, + visibility: "public" + }) + case "mention": + return ChatInteraction.respond({ markdown: "Private answer mentioning <@" + command.userId + ">" }) + case "defer": + return ChatInteraction.defer(command) + default: + return ChatInteraction.respond({ + markdown: "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons", + actions + }) + } +} + +const handler = Layer.succeed(ChatInteraction.Handler, { + click: () => Effect.die("unused"), + submit: () => Effect.die("unused"), + command: (command) => Effect.succeed(reply(command)), + refused: (_, error) => Effect.die(error) +}) + +const normalize = (payload: unknown) => Option.getOrThrow(SlackChat.normalizeInteraction(payload, { teamId: team })) + +const interact = (fake: ReturnType, payload: unknown) => + Effect.flatMap(SlackChat.SlackChat, (slack) => slack.interaction(payload, { deadline: Duration.seconds(1) })).pipe( + Effect.provide(handler), + Effect.provide(slackLayer(fake)) + ) + +describe("recorded slash commands", () => { + it.live("answers each command with the body Slack rendered", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest }) + for (const what of ["private", "public", "mention", "defer"]) { + const answer = yield* interact(fake, recorded.commands[what]) + assert.deepStrictEqual(answer, recorded.answers[what] ?? undefined, what) + } + })) + + it("takes a click on a private answer, which has no message, from its container", () => { + const payload = recorded.clicks.privateAnswer + assert.isFalse("message" in payload) + const click = normalize(payload) + assert.strictEqual(click._tag, "ButtonClicked") + // The id names where the click came from. chat.update on it answered + // message_not_found: a private answer is not a message Chat can reach. + assert.deepStrictEqual([click.id, (click as ChatInteraction.ButtonClicked).messageId, click.thread.id], [ + "cap-ack", + payload.container.message_ts, + `${team}/${channel}` + ]) + }) + + it.effect("follows up with the body Slack rendered, and maps a spent and an expired response_url", () => + Effect.gen(function*() { + const command = normalize(recorded.commands.defer) as ChatInteraction.CommandInvoked + const deferred = ChatInteraction.defer(command) + const followUp = (answer: { readonly status: number; readonly contentType: string; readonly body: string }) => { + const fake = makeFake({ + "auth.test": authTest, + "response_url": { status: answer.status, headers: { "content-type": answer.contentType }, body: answer.body } + }) + const sent = Effect.flatMap( + Chat.Chat, + (chat) => chat.followUp(deferred, { markdown: "**Deferred answer** to defer, with buttons", actions }) + ).pipe( + Effect.provide(slackLayer(fake)) + ) + return { fake, sent } + } + + const ok = followUp(recorded.responseUrl.followUp.response) + yield* ok.sent + const [{ params: { url, ...body } }] = ok.fake.called("response_url") + assert.strictEqual(url, new URL((command.raw as any).response_url).href) + assert.deepStrictEqual(body, recorded.responseUrl.followUp.request) + + for (const answer of [recorded.responseUrl.spent.response, recorded.responseUrl.expired.responses[0]]) { + const error = yield* Effect.flip(followUp(answer).sent) + assert.deepStrictEqual([error.op, error.reason._tag, error.reason.detail, (error.reason as any).cause], [ + "followUp", + "NotFound", + "interaction_expired", + answer.body + ]) + } + })) +}) diff --git a/packages/chat/slack/test/conformance.test.ts b/packages/chat/slack/test/conformance.test.ts new file mode 100644 index 00000000000..513d8d5b14f --- /dev/null +++ b/packages/chat/slack/test/conformance.test.ts @@ -0,0 +1,47 @@ +/** + * The conformance rules against the adapter over a stubbed Web API that keeps + * what was posted. Clicks, submits and commands are built from the posted + * blocks and the opened views and go through `SlackChat.interaction`. + */ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { describe, it } from "@effect/vitest" +import { conformance } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { makeSlack } from "./slack.ts" +import { channel, slackLayer, team } from "./utils.ts" + +/** Rules that need a user's message, a thread's history or a stored file, which the stub does not keep: live.test.ts runs them. */ +const liveOnly = new Set([ + "post/files", + "download/maxBytes", + "history/sameAsInbound", + "message/threadStable", + "inbound/fetchable", + "inbound/notSelf", + "nativeStream/concatenates", + "nativeStream/appendAfterStop", + "unsupported/getUser" +]) + +const options = { + platform: "slack", + thread: SlackChat.thread({ team, channel }), + userPosts: () => Effect.die("unused"), + parseMention: SlackChat.parseMention +} + +describe("conformance over the stubbed Web API", () => { + const names = Object.keys(conformance({ + ...options, + interactions: { click: () => Effect.void, submit: () => Effect.void, command: () => Effect.void }, + refuseFiles: () => Effect.void + })).filter((name) => !liveOnly.has(name)) + + for (const name of names) { + it.effect(name, () => { + const slack = makeSlack() + const rules = conformance({ ...options, interactions: slack.interactions, refuseFiles: slack.refuseFiles }) + return Effect.andThen(slack.bind, rules[name]).pipe(Effect.provide(slackLayer(slack.fake))) + }) + } +}) diff --git a/packages/chat/slack/test/emoji.test.ts b/packages/chat/slack/test/emoji.test.ts new file mode 100644 index 00000000000..d321e9fd98a --- /dev/null +++ b/packages/chat/slack/test/emoji.test.ts @@ -0,0 +1,77 @@ +import { blocksCost } from "@effect/chat-slack/internal/emoji" +import { emojiData } from "@effect/chat-slack/internal/emojiData" +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { assert, describe, it } from "@effect/vitest" +import * as Option from "effect/Option" + +const decode = (hex: string): string => String.fromCodePoint(...hex.split("-").map((point) => parseInt(point, 16))) + +/** An emoji, and the same without U+FE0F where the table says it is also written so. */ +const written = (emoji: string, nonQualified: string | undefined) => + nonQualified === "!" ? [emoji, emoji.replaceAll("\uFE0F", "")] : [emoji] + +// Every emoji string the generated table defines, read from the table directly +// so the fuzz does not trust emoji.ts to parse it. +const sequences: ReadonlyArray = emojiData.split("\n").flatMap((line) => { + const [, unified, nonQualified, groups] = line.split("|") + const variations = groups === undefined ? [] : groups.split(";").flatMap((group) => { + const [keys, template, variationNonQualified] = group.split(":") + return keys.split(",").flatMap((tones) => { + let index = 0 + const emoji = decode(template.replace(/\*/g, () => (0x1f3fb + Number(tones[index++]) - 2).toString(16))) + return written(emoji, variationNonQualified) + }) + }) + return [...written(decode(unified), nonQualified), ...variations] +}) + +describe("emoji", () => { + it("gives a single tone, or the same tone twice where upstream keys only by two", () => { + assert.deepStrictEqual(SlackChat.emojiFromName("handshake::skin-tone-3"), Option.some("🤝🏼")) + assert.deepStrictEqual(SlackChat.emojiFromName("people_holding_hands::skin-tone-4"), Option.some("🧑🏽‍🤝‍🧑🏽")) + assert.isTrue(Option.isNone(SlackChat.emojiFromName("people_holding_hands::skin-tone-1"))) + }) + + // Also the guard for optimised String.prototype.codePointAt, which returned + // wrong values around lone surrogates on Node 26.8 (V8). + it("blocksCost is subadditive", () => { + const extras = [ + "a", + " ", + "1", + "#", + "\u200D", + "\uFE0F", + "\u20E3", + "🏽", + "🇺", + "🇸", + "🦰", + "\u{E0067}", + "\uD83D", + "\uDE00" + ] + let seed = 1 + const random = (n: number) => { + seed = (seed * 1103515245 + 12345) & 0x7fffffff + return seed % n + } + const piece = () => { + const kind = random(3) + if (kind === 0) return extras[random(extras.length)] + const sequence = sequences[random(sequences.length)] + if (kind === 1) return sequence + const from = random(sequence.length + 1) + const to = random(sequence.length + 1) + return sequence.slice(Math.min(from, to), Math.max(from, to)) + } + for (let i = 0; i < 20_000; i++) { + let text = "" + for (let j = 1 + random(8); j > 0; j--) text += piece() + const cut = random(text.length + 1) + const a = text.slice(0, cut) + const b = text.slice(cut) + assert.isAtMost(blocksCost(text), blocksCost(a) + blocksCost(b), JSON.stringify([a, b])) + } + }) +}) diff --git a/packages/chat/slack/test/escape.test.ts b/packages/chat/slack/test/escape.test.ts new file mode 100644 index 00000000000..8e4f948b8ef --- /dev/null +++ b/packages/chat/slack/test/escape.test.ts @@ -0,0 +1,25 @@ +import { escape } from "@effect/chat-slack/internal/escape" +import { assert, describe, it } from "@effect/vitest" + +const sent = (text: string, notify = {}) => escape(text, notify, { flush: true }).sent + +describe("escape", () => { + it("escapes < outside code only", () => { + assert.strictEqual(sent("a <@U1> b"), "a <@U1> b") + assert.strictEqual(sent("`<@U1>` and ``x y``"), "`<@U1>` and ``x y``") + assert.strictEqual(sent("```\n<@U1>\n```\n<@U1>"), "```\n<@U1>\n```\n<@U1>") + assert.strictEqual(sent("~~~ts\nif (a < b) {}\n~~~"), "~~~ts\nif (a < b) {}\n~~~") + assert.strictEqual(sent("a `b < c"), "a `b < c") + assert.strictEqual(sent("\\<@U1>"), "\\<@U1>") + }) + + it("keeps the tokens the notify options allow", () => { + const notify = { users: ["U1"], groups: ["S1"], broadcast: true } + assert.strictEqual( + sent("<@U1> <@U2> ", notify), + "<@U1> <@U2> <!subteam^S2> " + ) + assert.strictEqual(sent("<@U1|name>", { users: ["U1"] }), "<@U1|name>") + assert.strictEqual(sent("", {}), "<!channel>") + }) +}) diff --git a/packages/chat/slack/test/events.test.ts b/packages/chat/slack/test/events.test.ts new file mode 100644 index 00000000000..8b0a744b80d --- /dev/null +++ b/packages/chat/slack/test/events.test.ts @@ -0,0 +1,214 @@ +/** + * Events API deliveries captured from Slack (see fixtures/events). Each body + * is the string Slack sent, so these run the webhook on real bytes. + */ +import { handleRequest, makeVerifier } from "@effect/chat-slack/internal/webhook" +import * as SlackChat from "@effect/chat-slack/SlackChat" +import * as NodeCrypto from "@effect/platform-node/NodeCrypto" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { dedupeKey } from "effect/chat/ChatEvent" +import { checkEvent, sameMessage } from "effect/chat/ChatTest" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as fs from "node:fs" +import { appId, botUserId, channel, checkOptions, team } from "./utils.ts" + +interface Delivery { + readonly headers: Record + readonly body: string +} + +const captured = (name: string): ReadonlyArray => + JSON.parse(fs.readFileSync(new URL(`./fixtures/events/${name}.json`, import.meta.url), "utf8")).deliveries + +const cases = fs.readdirSync(new URL("./fixtures/events/", import.meta.url)) + .filter((file) => file.endsWith(".json")) + .map((file) => file.slice(0, -5)) + +const context = { teamId: team, botUserId } + +// Signed here, with a test secret: the capture never logs Slack's signature. +const secret = "captured-delivery-test-secret" + +const sign = async (timestamp: string, body: string) => { + const encoder = new TextEncoder() + const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [ + "sign" + ]) + const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder.encode(`v0:${timestamp}:${body}`))) + return "v0=" + Array.from(mac, (byte) => byte.toString(16).padStart(2, "0")).join("") +} + +/** One delivery through the webhook, with its captured content type. */ +const deliver = Effect.fnUntraced(function*(delivery: Delivery) { + const timestamp = String(Math.floor(Date.now() / 1000)) + const signature = yield* Effect.promise(() => sign(timestamp, delivery.body)) + const web = new Request("http://localhost/slack/events", { + method: "POST", + body: delivery.body, + headers: { + "content-type": delivery.headers["content-type"], + "x-slack-request-timestamp": timestamp, + "x-slack-signature": signature + } + }) + const events: Array = [] + const response = yield* handleRequest(HttpServerRequest.fromWeb(web), { + verify: yield* makeVerifier(Redacted.make(secret)).pipe(Effect.provide(NodeCrypto.layerMac)), + appId, + teamId: team, + botUserId: Effect.succeed(botUserId), + handle: (event) => Effect.sync(() => void events.push(event)), + deadline: Duration.seconds(2), + maxBodyBytes: 1024 * 1024 + }) + assert.strictEqual(response.status, 200) + return events +}) + +/** The events of every delivery of a case, in delivery order. */ +const deliveries = (name: string) => Effect.forEach(captured(name), deliver) + +const created = (events: ReadonlyArray): ChatEvent.MessageCreated => { + assert.strictEqual(events.length, 1) + const event = events[0] + assert(event._tag === "MessageCreated") + return event +} + +const mentions = (event: ChatEvent.MessageCreated) => + event.message.mentions.map(({ end, id, isSelf, start }) => ({ + id, + isSelf, + text: event.message.text.slice(start, end) + })) + +const partsOf = (thread: ChatEvent.MessageCreated["thread"]) => Option.getOrThrow(SlackChat.parts(thread)) + +const selfMention = (label = "") => [{ id: botUserId, isSelf: true, text: `<@${botUserId}${label}>` }] + +describe("captured Events API deliveries", () => { + it.live("every delivery passes the webhook and normalises as SlackChat.normalize does", () => + Effect.gen(function*() { + for (const name of cases) { + for (const delivery of captured(name)) { + const events = yield* deliver(delivery) + assert.deepStrictEqual(events, SlackChat.normalize(JSON.parse(delivery.body), context), name) + for (const event of events) yield* checkEvent(event, checkOptions) + } + } + })) + + it.live("a thread reply and a thread_broadcast belong to the root's thread", () => + Effect.gen(function*() { + const root = created((yield* deliveries("thread-root"))[0]).message.id + const reply = created((yield* deliveries("thread-reply"))[0]) + assert.deepStrictEqual(partsOf(reply.thread), { team, channel, threadTs: root }) + // Slack sent a message_changed for the broadcast before the broadcast itself. + const [, broadcast] = yield* deliveries("thread-broadcast") + assert.deepStrictEqual(partsOf(created(broadcast).thread), { team, channel, threadTs: root }) + })) + + it.live("a mention only in code, a Block Kit section or an attachment is not reported", () => + Effect.gen(function*() { + for (const name of ["code-only-mention", "block-kit-mention", "attachment-mention"]) { + const event = created((yield* deliveries(name))[0]) + assert.deepStrictEqual(event.message.mentions, [], name) + } + })) +}) + +type Body = { readonly event: Record } + +/** The `message` and `app_mention` deliveries of one message, matched by ts. */ +const pairsOf = (name: string) => { + const bodies = captured(name).map((delivery): Body => JSON.parse(delivery.body)) + return bodies.flatMap((mention) => { + if (mention.event.type !== "app_mention" || mention.event.edited !== undefined) return [] + const message = bodies.filter((body) => + body.event.type === "message" && body.event.ts === mention.event.ts && body.event.hidden === undefined + ) + assert.strictEqual(message.length, 1, name) + return [[message[0], mention] as const] + }) +} + +describe("captured deliveries for messages a person typed", () => { + it.live("each message and app_mention pair normalises to the same event", () => + Effect.gen(function*() { + const found: Array = [] + for (const name of cases) { + for (const [message, mention] of pairsOf(name)) { + const fromMessage = created(SlackChat.normalize(message, context)) + const fromMention = created(SlackChat.normalize(mention, context)) + assert.strictEqual(dedupeKey(fromMessage), dedupeKey(fromMention), name) + assert.isTrue(sameMessage(fromMessage.message, fromMention.message), name) + const { raw: _a, ...left } = fromMessage.message + const { raw: _b, ...right } = fromMention.message + assert.deepStrictEqual(left, right, name) + assert.deepStrictEqual(fromMessage.thread, fromMention.thread, name) + assert.deepStrictEqual( + mentions(fromMessage), + selfMention(String(message.event.subtype) === "me_message" ? "|janitor2" : "") + ) + found.push(name) + } + } + assert.deepStrictEqual(found.sort(), [ + "human-edit-keeps-mention", + "human-edit-removes-mention", + "human-file-mention", + "human-me-message", + "human-mention", + "human-thread-broadcast" + ]) + })) + + it.live("an edit gives nothing, including the app_mention Slack sends when it adds a mention", () => + Effect.gen(function*() { + const [original, changed, mention] = yield* deliveries("human-edit-adds-mention") + assert.deepStrictEqual(created(original).message.mentions, []) + assert.deepStrictEqual(changed, []) + assert.deepStrictEqual(mention, []) + })) + + it.live("reactions, with a skin tone and a custom emoji", () => + Effect.gen(function*() { + const root = created((yield* deliveries("human-plain"))[0]).message.id + const reactions = (yield* deliveries("human-reactions")).map((events) => { + assert.strictEqual(events.length, 1) + const event = events[0] + assert(event._tag === "ReactionAdded" || event._tag === "ReactionRemoved") + assert.strictEqual(event.messageId, root) + return event + }) + assert.deepStrictEqual(reactions.map((event) => [event._tag, event.emoji]), [ + ["ReactionAdded", "👍"], + ["ReactionRemoved", "👍"], + ["ReactionAdded", "👍🏼"], + ["ReactionRemoved", "👍🏼"], + ["ReactionAdded", "custom:effect"], + ["ReactionRemoved", "custom:effect"] + ]) + assert.strictEqual(new Set(reactions.map(dedupeKey)).size, 6) + })) +}) + +describe("captured deliveries for stops", () => { + it.live("only the first click on Stop names the open stream", () => + Effect.gen(function*() { + const results = yield* deliveries("stop-stream") + const root = created(results[0]).message.id + const stream = created(results[2]) + assert.strictEqual(stream.message.text, "") + const stops = results.flat().filter((event) => event._tag === "StopRequested") + assert.deepStrictEqual(stops.map((event) => event.messageIds), [[stream.message.id], [], []]) + for (const stop of stops) assert.deepStrictEqual(partsOf(stop.thread), { team, channel, threadTs: root }) + // Each click is its own request. + assert.strictEqual(new Set(stops.map(dedupeKey)).size, 3) + })) +}) diff --git a/packages/chat/slack/test/fixtures/events/attachment-mention.json b/packages/chat/slack/test/fixtures/events/attachment-mention.json new file mode 100644 index 00000000000..74a41ebfa90 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/attachment-mention.json @@ -0,0 +1,41 @@ +{ + "case": "A mention only inside a legacy attachment", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516879.362179", + "request": { + "text": "attachment carrier [EFF-3694 20:54:24]", + "attachments": [ + { + "text": "attachment <@U0CCLMD4S1L>", + "fallback": "attachment" + } + ] + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:40.254Z", + "headers": { + "x-slack-request-timestamp": "1794516879", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516879.362179\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"attachment carrier [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"attachments\":[{\"id\":1,\"fallback\":\"attachment\",\"text\":\"attachment <@U0CCLMD4S1L>\"}],\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"oLy\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"attachment carrier [EFF-3694 20:54:24]\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516879.362179\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH85DKJ3Y\",\"event_time\":1794516879,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/block-kit-mention.json b/packages/chat/slack/test/fixtures/events/block-kit-mention.json new file mode 100644 index 00000000000..7e151cb52b7 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/block-kit-mention.json @@ -0,0 +1,44 @@ +{ + "case": "A mention only inside a Block Kit section; the fallback text has none", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516878.111629", + "request": { + "text": "block kit fallback [EFF-3694 20:54:24]", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "section <@U0CCLMD4S1L> [EFF-3694 20:54:24]" + } + } + ] + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:38.438Z", + "headers": { + "x-slack-request-timestamp": "1794516878", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516878.111629\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"block kit fallback [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"section\",\"block_id\":\"us67M\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"section <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"verbatim\":false}}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516878.111629\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH3T1C5V1\",\"event_time\":1794516878,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/code-only-mention.json b/packages/chat/slack/test/fixtures/events/code-only-mention.json new file mode 100644 index 00000000000..d2e15b7e38f --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/code-only-mention.json @@ -0,0 +1,35 @@ +{ + "case": "A mention only inside an inline code span", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516876.861899", + "request": { + "markdown_text": "in code `<@U0CCLMD4S1L>` only [EFF-3694 20:54:24]" + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:37.834Z", + "headers": { + "x-slack-request-timestamp": "1794516876", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516876.861899\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"in code `<@U0CCLMD4S1L>` only [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"PuZn8E\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"in code \"},{\"type\":\"text\",\"text\":\"<@U0CCLMD4S1L>\",\"style\":{\"code\":true}},{\"type\":\"text\",\"text\":\" only [EFF-3694 20:54:24]\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516876.861899\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH4R6DQ4D\",\"event_time\":1794516876,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-edit-adds-mention.json b/packages/chat/slack/test/fixtures/events/human-edit-adds-mention.json new file mode 100644 index 00000000000..78ca2481690 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-edit-adds-mention.json @@ -0,0 +1,37 @@ +{ + "case": "H3: a message without a mention, edited to add one. The edit gave a message_changed and an app_mention carrying edited", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:26:28.004Z", + "headers": { + "x-slack-request-timestamp": "1794525987", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U080YLYB277\",\"ts\":\"1794525987.734039\",\"client_msg_id\":\"5b3a793b-0ca1-41d7-99d9-9590f7f50ac4\",\"text\":\"H3 no mention yet\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"KyCld\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H3 no mention yet\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794525987.734039\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHD9B5VSM\",\"event_time\":1794525987,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:26:44.999Z", + "headers": { + "x-slack-request-timestamp": "1794526004", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526004.000000\"},\"client_msg_id\":\"5b3a793b-0ca1-41d7-99d9-9590f7f50ac4\",\"text\":\"H3 <@U0CCLMD4S1L> mention added\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"TlWRb\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H3 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention added\"}]}]}],\"ts\":\"1794525987.734039\",\"source_team\":\"T05479Z6JBW\",\"user_team\":\"T05479Z6JBW\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525987.734039\",\"client_msg_id\":\"5b3a793b-0ca1-41d7-99d9-9590f7f50ac4\",\"text\":\"H3 no mention yet\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"KyCld\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H3 no mention yet\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526004.001100\",\"event_ts\":\"1794526004.001100\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH63YKJN9\",\"event_time\":1794526004,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:26:45.073Z", + "headers": { + "x-slack-request-timestamp": "1794526024", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"user\":\"U080YLYB277\",\"ts\":\"1794525987.734039\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526004.000000\"},\"client_msg_id\":\"5b3a793b-0ca1-41d7-99d9-9590f7f50ac4\",\"text\":\"H3 <@U0CCLMD4S1L> mention added\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"TlWRb\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H3 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention added\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794525987.734039\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH55TL6QK\",\"event_time\":1794525987,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-edit-keeps-mention.json b/packages/chat/slack/test/fixtures/events/human-edit-keeps-mention.json new file mode 100644 index 00000000000..bacad1c34f9 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-edit-keeps-mention.json @@ -0,0 +1,45 @@ +{ + "case": "H5: a message that mentions the bot, edited with the mention kept, then a message_changed after thread replies", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:27:37.287Z", + "headers": { + "x-slack-request-timestamp": "1794526057", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U080YLYB277\",\"ts\":\"1794526057.057629\",\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> first\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"FehUf\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" first\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526057.057629\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHF3YMK1A\",\"event_time\":1794526057,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:27:37.363Z", + "headers": { + "x-slack-request-timestamp": "1794526057", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"user\":\"U080YLYB277\",\"ts\":\"1794526057.057629\",\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> first\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"FehUf\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" first\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526057.057629\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW1MT7AT\",\"event_time\":1794526057,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:27:44.903Z", + "headers": { + "x-slack-request-timestamp": "1794526064", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526064.000000\"},\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> edited\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526057.057629\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526058.717659\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"zXiSr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" edited\"}]}]}],\"ts\":\"1794526057.057629\",\"source_team\":\"T05479Z6JBW\",\"user_team\":\"T05479Z6JBW\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794526057.057629\",\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> first\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526057.057629\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526058.717659\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":true,\"last_read\":\"1794526057.057629\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"FehUf\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" first\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526064.002200\",\"event_ts\":\"1794526064.002200\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH6450HAR\",\"event_time\":1794526064,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:27:55.212Z", + "headers": { + "x-slack-request-timestamp": "1794526074", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526064.000000\"},\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> edited\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526057.057629\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526072.959879\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"zXiSr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" edited\"}]}]}],\"ts\":\"1794526057.057629\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794526057.057629\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526064.000000\"},\"client_msg_id\":\"08e4fbe1-2069-4589-be8e-9d80d0c2ab5b\",\"text\":\"H5 <@U0CCLMD4S1L> edited\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526057.057629\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526072.959879\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"zXiSr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H5 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" edited\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526074.002900\",\"event_ts\":\"1794526074.002900\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH645QLDT\",\"event_time\":1794526074,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-edit-removes-mention.json b/packages/chat/slack/test/fixtures/events/human-edit-removes-mention.json new file mode 100644 index 00000000000..afbee0b6713 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-edit-removes-mention.json @@ -0,0 +1,45 @@ +{ + "case": "H4: a message that mentions the bot, a message_changed after its first thread reply, then the edit removing the mention", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:27:05.063Z", + "headers": { + "x-slack-request-timestamp": "1794526024", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U080YLYB277\",\"ts\":\"1794526024.326099\",\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"R6gDK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526024.326099\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH9DMH2Q6\",\"event_time\":1794526024,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:27:05.201Z", + "headers": { + "x-slack-request-timestamp": "1794526024", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"user\":\"U080YLYB277\",\"ts\":\"1794526024.326099\",\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"R6gDK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526024.326099\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH9DMMFSS\",\"event_time\":1794526024,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:27:17.159Z", + "headers": { + "x-slack-request-timestamp": "1794526037", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526024.326099\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526036.095559\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"R6gDK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"ts\":\"1794526024.326099\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794526024.326099\",\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526024.326099\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526036.095559\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"R6gDK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526036.001800\",\"event_ts\":\"1794526036.001800\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH6429XJR\",\"event_time\":1794526036,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:27:21.734Z", + "headers": { + "x-slack-request-timestamp": "1794526041", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"edited\":{\"user\":\"U080YLYB277\",\"ts\":\"1794526041.000000\"},\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 mention removed\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526024.326099\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526036.095559\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"VOPJM\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 mention removed\"}]}]}],\"ts\":\"1794526024.326099\",\"source_team\":\"T05479Z6JBW\",\"user_team\":\"T05479Z6JBW\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794526024.326099\",\"client_msg_id\":\"1b1e1d6f-67c3-4518-bfe2-2617a8297f5b\",\"text\":\"H4 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794526024.326099\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526036.095559\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":true,\"last_read\":\"1794526024.326099\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"R6gDK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H4 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526041.001900\",\"event_ts\":\"1794526041.001900\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH55XPQ1Z\",\"event_time\":1794526041,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-file-mention.json b/packages/chat/slack/test/fixtures/events/human-file-mention.json new file mode 100644 index 00000000000..be316de3df0 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-file-mention.json @@ -0,0 +1,55 @@ +{ + "case": "H8: a file upload whose comment mentions the bot (file_share message and app_mention), then a message_changed after its first thread reply", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:29:07.390Z", + "headers": { + "x-slack-request-timestamp": "1794526147", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"text\":\"H8 <@U0CCLMD4S1L> file\",\"files\":[{\"id\":\"F0CDY7WPKU5\",\"created\":1793617539,\"timestamp\":1793617539,\"mimetype\":\"image\\/svg+xml\",\"filetype\":\"svg\",\"pretty_type\":\"SVG\",\"user\":\"U080YLYB277\",\"user_team\":\"T05479Z6JBW\",\"editable\":false,\"size\":1023,\"mode\":\"hosted\",\"is_external\":false,\"external_type\":\"\",\"is_public\":true,\"public_url_shared\":false,\"display_as_bot\":false,\"username\":\"\",\"name\":\"T3.svg\",\"title\":\"T3.svg\",\"is_modified_by_ai\":false,\"url_private\":\"https:\\/\\/scrubbed.invalid\\/url_private\",\"url_private_download\":\"https:\\/\\/scrubbed.invalid\\/url_private_download\",\"media_display_type\":\"unknown\",\"permalink\":\"https:\\/\\/scrubbed.invalid\\/permalink\",\"permalink_public\":\"https:\\/\\/scrubbed.invalid\\/permalink_public\",\"skipped_shares\":true,\"has_rich_preview\":false,\"file_access\":\"visible\"}],\"upload\":false,\"user\":\"U080YLYB277\",\"display_as_bot\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"vmQq2\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H8 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" file\"}]}]}],\"ts\":\"1794526147.127619\",\"client_msg_id\":\"bfaeb4c3-06ae-4962-9914-45b76bf4236f\",\"channel\":\"C0CH1LGJ4F3\",\"subtype\":\"file_share\",\"event_ts\":\"1794526147.127619\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH64E434M\",\"event_time\":1794526147,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "permalink", + "permalink_public", + "url_private", + "url_private_download" + ] + }, + { + "loggedAt": "2026-11-12T23:29:07.554Z", + "headers": { + "x-slack-request-timestamp": "1794526147", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"text\":\"H8 <@U0CCLMD4S1L> file\",\"files\":[{\"id\":\"F0CDY7WPKU5\",\"created\":1793617539,\"timestamp\":1793617539,\"mimetype\":\"image\\/svg+xml\",\"filetype\":\"svg\",\"pretty_type\":\"SVG\",\"user\":\"U080YLYB277\",\"user_team\":\"T05479Z6JBW\",\"editable\":false,\"size\":1023,\"mode\":\"hosted\",\"is_external\":false,\"external_type\":\"\",\"is_public\":true,\"public_url_shared\":false,\"display_as_bot\":false,\"username\":\"\",\"name\":\"T3.svg\",\"title\":\"T3.svg\",\"is_modified_by_ai\":false,\"url_private\":\"https:\\/\\/scrubbed.invalid\\/url_private\",\"url_private_download\":\"https:\\/\\/scrubbed.invalid\\/url_private_download\",\"media_display_type\":\"unknown\",\"permalink\":\"https:\\/\\/scrubbed.invalid\\/permalink\",\"permalink_public\":\"https:\\/\\/scrubbed.invalid\\/permalink_public\",\"is_starred\":false,\"skipped_shares\":true,\"has_rich_preview\":false,\"file_access\":\"visible\"}],\"upload\":false,\"user\":\"U080YLYB277\",\"display_as_bot\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"vmQq2\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H8 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" file\"}]}]}],\"ts\":\"1794526147.127619\",\"client_msg_id\":\"bfaeb4c3-06ae-4962-9914-45b76bf4236f\",\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526147.127619\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH9E0NN14\",\"event_time\":1794526147,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}", + "scrubbed": [ + "permalink", + "permalink_public", + "url_private", + "url_private_download" + ] + }, + { + "loggedAt": "2026-11-12T23:29:25.636Z", + "headers": { + "x-slack-request-timestamp": "1794526187", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"text\":\"H8 <@U0CCLMD4S1L> file\",\"files\":[{\"id\":\"F0CDY7WPKU5\",\"created\":1793617539,\"timestamp\":1793617539,\"mimetype\":\"image\\/svg+xml\",\"filetype\":\"svg\",\"pretty_type\":\"SVG\",\"user\":\"U080YLYB277\",\"user_team\":\"T05479Z6JBW\",\"editable\":false,\"size\":1023,\"mode\":\"hosted\",\"is_external\":false,\"external_type\":\"\",\"is_public\":true,\"public_url_shared\":false,\"display_as_bot\":false,\"username\":\"\",\"name\":\"T3.svg\",\"title\":\"T3.svg\",\"is_modified_by_ai\":false,\"url_private\":\"https:\\/\\/scrubbed.invalid\\/url_private\",\"url_private_download\":\"https:\\/\\/scrubbed.invalid\\/url_private_download\",\"media_display_type\":\"unknown\",\"permalink\":\"https:\\/\\/scrubbed.invalid\\/permalink\",\"permalink_public\":\"https:\\/\\/scrubbed.invalid\\/permalink_public\",\"skipped_shares\":true,\"has_rich_preview\":false,\"file_access\":\"visible\"}],\"upload\":false,\"user\":\"U080YLYB277\",\"display_as_bot\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"vmQq2\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H8 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" file\"}]}]}],\"type\":\"message\",\"client_msg_id\":\"bfaeb4c3-06ae-4962-9914-45b76bf4236f\",\"thread_ts\":\"1794526147.127619\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526163.740459\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"ts\":\"1794526147.127619\"},\"previous_message\":{\"text\":\"H8 <@U0CCLMD4S1L> file\",\"files\":[{\"id\":\"F0CDY7WPKU5\",\"created\":1793617539,\"timestamp\":1793617539,\"mimetype\":\"image\\/svg+xml\",\"filetype\":\"svg\",\"pretty_type\":\"SVG\",\"user\":\"U080YLYB277\",\"user_team\":\"T05479Z6JBW\",\"editable\":false,\"size\":1023,\"mode\":\"hosted\",\"is_external\":false,\"external_type\":\"\",\"is_public\":true,\"public_url_shared\":false,\"display_as_bot\":false,\"username\":\"\",\"name\":\"T3.svg\",\"title\":\"T3.svg\",\"is_modified_by_ai\":false,\"url_private\":\"https:\\/\\/scrubbed.invalid\\/url_private\",\"url_private_download\":\"https:\\/\\/scrubbed.invalid\\/url_private_download\",\"media_display_type\":\"unknown\",\"permalink\":\"https:\\/\\/scrubbed.invalid\\/permalink\",\"permalink_public\":\"https:\\/\\/scrubbed.invalid\\/permalink_public\",\"is_starred\":false,\"skipped_shares\":true,\"has_rich_preview\":false,\"file_access\":\"visible\"}],\"upload\":false,\"user\":\"U080YLYB277\",\"display_as_bot\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"vmQq2\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H8 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" file\"}]}]}],\"type\":\"message\",\"ts\":\"1794526147.127619\",\"client_msg_id\":\"bfaeb4c3-06ae-4962-9914-45b76bf4236f\",\"thread_ts\":\"1794526147.127619\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526163.740459\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526164.003900\",\"event_ts\":\"1794526164.003900\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW201CQP\",\"event_time\":1794526164,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "permalink", + "permalink_public", + "url_private", + "url_private_download" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-me-message.json b/packages/chat/slack/test/fixtures/events/human-me-message.json new file mode 100644 index 00000000000..f7fd7a1ddc4 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-me-message.json @@ -0,0 +1,29 @@ +{ + "case": "H7: a /me line that mentions the bot; the app_mention arrived before the message, both with subtype me_message", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:28:30.070Z", + "headers": { + "x-slack-request-timestamp": "1794526109", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"subtype\":\"me_message\",\"user\":\"U080YLYB277\",\"ts\":\"1794526109.798279\",\"text\":\"H7 waves at <@U0CCLMD4S1L|janitor2>\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"xhNF0\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H7 waves at \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526109.798279\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH564V6R1\",\"event_time\":1794526109,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:28:30.533Z", + "headers": { + "x-slack-request-timestamp": "1794526109", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"me_message\",\"user\":\"U080YLYB277\",\"ts\":\"1794526109.798279\",\"text\":\"H7 waves at <@U0CCLMD4S1L|janitor2>\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"c\\/w\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H7 waves at \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526109.798279\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CJ5PX7ETA\",\"event_time\":1794526109,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-mention.json b/packages/chat/slack/test/fixtures/events/human-mention.json new file mode 100644 index 00000000000..fee715cdaca --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-mention.json @@ -0,0 +1,37 @@ +{ + "case": "H2: a message that mentions the bot (message and app_mention), then the message_changed Slack sent for it after its first thread reply", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:26:14.684Z", + "headers": { + "x-slack-request-timestamp": "1794525974", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U080YLYB277\",\"ts\":\"1794525974.374799\",\"client_msg_id\":\"01614a30-5733-45d7-8932-c69a80d4b5f7\",\"text\":\"H2 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"NcHtr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H2 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794525974.374799\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH55QC66P\",\"event_time\":1794525974,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:26:14.751Z", + "headers": { + "x-slack-request-timestamp": "1794525974", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"user\":\"U080YLYB277\",\"ts\":\"1794525974.374799\",\"client_msg_id\":\"01614a30-5733-45d7-8932-c69a80d4b5f7\",\"text\":\"H2 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"NcHtr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H2 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794525974.374799\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH55QEKB5\",\"event_time\":1794525974,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:26:38.409Z", + "headers": { + "x-slack-request-timestamp": "1794525997", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"client_msg_id\":\"01614a30-5733-45d7-8932-c69a80d4b5f7\",\"text\":\"H2 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525974.374799\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794525996.616959\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"NcHtr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H2 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}],\"ts\":\"1794525974.374799\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525974.374799\",\"client_msg_id\":\"01614a30-5733-45d7-8932-c69a80d4b5f7\",\"text\":\"H2 <@U0CCLMD4S1L> mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525974.374799\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794525996.616959\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"NcHtr\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H2 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" mention\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794525997.001000\",\"event_ts\":\"1794525997.001000\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHB72ALMQ\",\"event_time\":1794525997,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-plain.json b/packages/chat/slack/test/fixtures/events/human-plain.json new file mode 100644 index 00000000000..22ae496892f --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-plain.json @@ -0,0 +1,29 @@ +{ + "case": "H1: a plain message without a mention, then the message_changed Slack sent for it after thread replies were added", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:25:54.192Z", + "headers": { + "x-slack-request-timestamp": "1794525954", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U080YLYB277\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH9DE3TF0\",\"event_time\":1794525953,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:30:54.029Z", + "headers": { + "x-slack-request-timestamp": "1794526253", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":2,\"reply_users_count\":2,\"latest_reply\":\"1794526252.008029\",\"reply_users\":[\"U080YLYB277\",\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}],\"ts\":\"1794525953.901839\"},\"previous_message\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":2,\"reply_users_count\":2,\"latest_reply\":\"1794526252.008029\",\"reply_users\":[\"U080YLYB277\",\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526253.007100\",\"event_ts\":\"1794526253.007100\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW2CD895\",\"event_time\":1794526253,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-reactions.json b/packages/chat/slack/test/fixtures/events/human-reactions.json new file mode 100644 index 00000000000..790c86f8f30 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-reactions.json @@ -0,0 +1,61 @@ +{ + "case": "Reactions on H1: +1, +1::skin-tone-3 and the custom emoji effect, each added and then removed", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:30:00.643Z", + "headers": { + "x-slack-request-timestamp": "1794526200", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_added\",\"user\":\"U080YLYB277\",\"reaction\":\"+1\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526200.004600\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CJ5Q7DLUQ\",\"event_time\":1794526200,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX2FkZGVkIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:30:02.690Z", + "headers": { + "x-slack-request-timestamp": "1794526202", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_removed\",\"user\":\"U080YLYB277\",\"reaction\":\"+1\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526202.004800\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH64LTKGV\",\"event_time\":1794526202,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX3JlbW92ZWQiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:30:15.245Z", + "headers": { + "x-slack-request-timestamp": "1794526215", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_added\",\"user\":\"U080YLYB277\",\"reaction\":\"+1::skin-tone-3\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526215.005300\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CJ5QAF5JL\",\"event_time\":1794526215,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX2FkZGVkIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:30:19.605Z", + "headers": { + "x-slack-request-timestamp": "1794526219", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_removed\",\"user\":\"U080YLYB277\",\"reaction\":\"+1::skin-tone-3\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526219.005500\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH9EARFE2\",\"event_time\":1794526219,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX3JlbW92ZWQiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:30:25.029Z", + "headers": { + "x-slack-request-timestamp": "1794526224", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_added\",\"user\":\"U080YLYB277\",\"reaction\":\"effect\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526224.005800\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHF4KCNMA\",\"event_time\":1794526224,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX2FkZGVkIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + }, + { + "loggedAt": "2026-11-12T23:30:28.577Z", + "headers": { + "x-slack-request-timestamp": "1794526228", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"reaction_removed\",\"user\":\"U080YLYB277\",\"reaction\":\"effect\",\"item\":{\"type\":\"message\",\"channel\":\"C0CH1LGJ4F3\",\"ts\":\"1794525953.901839\",\"channel_type\":\"group\"},\"item_user\":\"U080YLYB277\",\"event_ts\":\"1794526228.006000\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH64R4FBP\",\"event_time\":1794526228,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6InJlYWN0aW9uX3JlbW92ZWQiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/human-thread-broadcast.json b/packages/chat/slack/test/fixtures/events/human-thread-broadcast.json new file mode 100644 index 00000000000..4e0099b6b9e --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/human-thread-broadcast.json @@ -0,0 +1,37 @@ +{ + "case": "H9: a thread reply under H1 that mentions the bot, sent with also send to channel: thread_broadcast message, a message_changed, and a thread_broadcast app_mention", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "Tim Smart (U080YLYB277), typing in the Slack client", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:29:47.283Z", + "headers": { + "x-slack-request-timestamp": "1794526187", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"thread_broadcast\",\"user\":\"U080YLYB277\",\"thread_ts\":\"1794525953.901839\",\"root\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526187.055299\",\"reply_users\":[\"U080YLYB277\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}]},\"ts\":\"1794526187.055299\",\"client_msg_id\":\"2343e36e-bd61-44a6-b1c2-449558ab3285\",\"text\":\"H9 <@U0CCLMD4S1L> broadcast\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"iH0Dg\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H9 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" broadcast\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526187.055299\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHB7PAKTL\",\"event_time\":1794526187,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:29:47.445Z", + "headers": { + "x-slack-request-timestamp": "1794526187", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"subtype\":\"thread_broadcast\",\"user\":\"U080YLYB277\",\"thread_ts\":\"1794525953.901839\",\"root\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526187.055299\",\"reply_users\":[\"U080YLYB277\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}]},\"type\":\"message\",\"client_msg_id\":\"2343e36e-bd61-44a6-b1c2-449558ab3285\",\"text\":\"H9 <@U0CCLMD4S1L> broadcast\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"iH0Dg\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H9 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" broadcast\"}]}]}],\"ts\":\"1794526187.055299\"},\"previous_message\":{\"subtype\":\"thread_broadcast\",\"user\":\"U080YLYB277\",\"thread_ts\":\"1794525953.901839\",\"root\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526187.055299\",\"reply_users\":[\"U080YLYB277\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}]},\"type\":\"message\",\"ts\":\"1794526187.055299\",\"client_msg_id\":\"2343e36e-bd61-44a6-b1c2-449558ab3285\",\"text\":\"H9 <@U0CCLMD4S1L> broadcast\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"iH0Dg\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H9 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" broadcast\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526187.004100\",\"event_ts\":\"1794526187.004100\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW22MCSK\",\"event_time\":1794526187,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}" + }, + { + "loggedAt": "2026-11-12T23:29:47.627Z", + "headers": { + "x-slack-request-timestamp": "1794526187", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"app_mention\",\"subtype\":\"thread_broadcast\",\"user\":\"U080YLYB277\",\"thread_ts\":\"1794525953.901839\",\"root\":{\"user\":\"U080YLYB277\",\"type\":\"message\",\"ts\":\"1794525953.901839\",\"client_msg_id\":\"0bdcb308-6582-479d-92bf-52a76ea9362c\",\"text\":\"H1 plain message, no mention\",\"team\":\"T05479Z6JBW\",\"thread_ts\":\"1794525953.901839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526187.055299\",\"reply_users\":[\"U080YLYB277\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"yB8jc\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H1 plain message, no mention\"}]}]}]},\"ts\":\"1794526187.055299\",\"client_msg_id\":\"2343e36e-bd61-44a6-b1c2-449558ab3285\",\"text\":\"H9 <@U0CCLMD4S1L> broadcast\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"iH0Dg\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"H9 \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" broadcast\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526187.055299\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW22QR4P\",\"event_time\":1794526187,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6ImFwcF9tZW50aW9uIiwidGlkIjoiVDA1NDc5WjZKQlciLCJhaWQiOiJBMENDSEJIREs3WCIsImNpZCI6IkMwQ0gxTEdKNEYzIn0\"}" + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/stop-stream.json b/packages/chat/slack/test/fixtures/events/stop-stream.json new file mode 100644 index 00000000000..4f1979d5175 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/stop-stream.json @@ -0,0 +1,102 @@ +{ + "case": "STOP-1: a root message, a native stream in its thread, and three clicks on Stop. Only the first agent_session_stopped names the stream; the later two carry an empty streaming_message_ts while it was still open", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), as janitor production answered or as the session script posted; the stops by Tim Smart (U080YLYB277) clicking Stop", + "recordedAt": "2026-11-12T23:25:50Z to 2026-11-12T23:36:10Z, a session run with Jarvis on EFF-3694", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "deliveries": [ + { + "loggedAt": "2026-11-12T23:32:49.208Z", + "headers": { + "x-slack-request-timestamp": "1794526369", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794526368.981839\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 <@U080YLYB277> click Stop on the stream in this thread\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"lXXdG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 \"},{\"type\":\"user\",\"user_id\":\"U080YLYB277\"},{\"type\":\"text\",\"text\":\" click Stop on the stream in this thread\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526368.981839\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH65863B7\",\"event_time\":1794526368,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T23:32:49.250Z", + "headers": { + "x-slack-request-timestamp": "1794526369", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 <@U080YLYB277> click Stop on the stream in this thread\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"xLw\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 \"},{\"type\":\"user\",\"user_id\":\"U080YLYB277\"},{\"type\":\"text\",\"text\":\" click Stop on the stream in this thread\"}]}]}],\"agent_session\":{\"status\":\"processing\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"date_status_processing_expire\":1794529969,\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"processing\",\"date_status_processing_expire\":1794529969,\"is_stoppable\":true}]},\"ts\":\"1794526368.981839\"},\"previous_message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794526368.981839\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 <@U080YLYB277> click Stop on the stream in this thread\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"VbU\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 \"},{\"type\":\"user\",\"user_id\":\"U080YLYB277\"},{\"type\":\"text\",\"text\":\" click Stop on the stream in this thread\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526369.007200\",\"event_ts\":\"1794526369.007200\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHDANGUHX\",\"event_time\":1794526369,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T23:32:49.992Z", + "headers": { + "x-slack-request-timestamp": "1794526369", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794526369.136369\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794526368.981839\",\"parent_user_id\":\"U0CCLMD4S1L\",\"streaming_state\":\"in_progress\",\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794526369.136369\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHDANPU0Z\",\"event_time\":1794526369,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T23:33:17.113Z", + "headers": { + "x-slack-request-timestamp": "1794526397", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"agent_session_stopped\",\"channel\":\"C0CH1LGJ4F3\",\"user\":\"U080YLYB277\",\"streaming_message_ts\":[\"1794526369.136369\"],\"thread_ts\":\"1794526368.981839\",\"event_ts\":\"1794526397.043711\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHDAQKQHX\",\"event_time\":1794526397,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false}" + }, + { + "loggedAt": "2026-11-12T23:34:50.495Z", + "headers": { + "x-slack-request-timestamp": "1794526490", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"agent_session_stopped\",\"channel\":\"C0CH1LGJ4F3\",\"user\":\"U080YLYB277\",\"streaming_message_ts\":[],\"thread_ts\":\"1794526368.981839\",\"event_ts\":\"1794526490.421161\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH65KMBU5\",\"event_time\":1794526490,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false}" + }, + { + "loggedAt": "2026-11-12T23:35:01.679Z", + "headers": { + "x-slack-request-timestamp": "1794526501", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"agent_session_stopped\",\"channel\":\"C0CH1LGJ4F3\",\"user\":\"U080YLYB277\",\"streaming_message_ts\":[],\"thread_ts\":\"1794526368.981839\",\"event_ts\":\"1794526501.600290\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHDB1MMB3\",\"event_time\":1794526501,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false}" + }, + { + "loggedAt": "2026-11-12T23:36:07.103Z", + "headers": { + "x-slack-request-timestamp": "1794526566", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 streaming. Click Stop. tick 1 tick 2 tick 3 tick 4 tick 5 tick 6 tick 7 tick 8 tick 9 tick 10 tick 11 tick 12 tick 13 tick 14 tick 15 tick 16 tick 17 tick 18 tick 19 tick 20 tick 21 tick 22 tick 23 tick 24 tick 25 tick 26 tick 27 tick 28 tick 29 tick 30 tick 31 (append after click)\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794526368.981839\",\"parent_user_id\":\"U0CCLMD4S1L\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"pCbIiy\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 streaming. Click Stop. tick 1 tick 2 tick 3 tick 4 tick 5 tick 6 tick 7 tick 8 tick 9 tick 10 tick 11 tick 12 tick 13 tick 14 tick 15 tick 16 tick 17 tick 18 tick 19 tick 20 tick 21 tick 22 tick 23 tick 24 tick 25 tick 26 tick 27 tick 28 tick 29 tick 30 tick 31 (append after click)\"}]}]}],\"ai_context\":{\"type\":\"default\",\"result_status\":\"stopped_by_user\",\"stopped_by_user_id\":\"U080YLYB277\"},\"streaming_state\":\"completed\",\"ts\":\"1794526369.136369\"},\"previous_message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794526369.136369\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 streaming. Click Stop. tick 1 tick 2 tick 3 tick 4 tick 5\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794526368.981839\",\"parent_user_id\":\"U0CCLMD4S1L\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"WhicAV\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 streaming. Click Stop. tick 1 tick 2 tick 3 tick 4 tick 5\"}]}]}],\"ai_context\":{\"type\":\"default\",\"result_status\":\"none\",\"stopped_by_user_id\":\"U080YLYB277\"},\"streaming_state\":\"in_progress\"},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526566.007700\",\"event_ts\":\"1794526566.007700\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGW3DNVF1\",\"event_time\":1794526566,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T23:36:07.506Z", + "headers": { + "x-slack-request-timestamp": "1794526566", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 <@U080YLYB277> click Stop on the stream in this thread\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794526368.981839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526369.136369\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"NCu5\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 \"},{\"type\":\"user\",\"user_id\":\"U080YLYB277\"},{\"type\":\"text\",\"text\":\" click Stop on the stream in this thread\"}]}]}],\"agent_session\":{\"status\":\"active\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"active\",\"is_stoppable\":true}]},\"ts\":\"1794526368.981839\"},\"previous_message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794526368.981839\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"STOP-1 <@U080YLYB277> click Stop on the stream in this thread\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794526368.981839\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794526369.136369\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"N7JH\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"STOP-1 \"},{\"type\":\"user\",\"user_id\":\"U080YLYB277\"},{\"type\":\"text\",\"text\":\" click Stop on the stream in this thread\"}]}]}],\"agent_session\":{\"status\":\"processing\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"date_status_processing_expire\":1794529969,\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"processing\",\"date_status_processing_expire\":1794529969,\"is_stoppable\":true}]}},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794526566.007800\",\"event_ts\":\"1794526566.007800\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHB90A5B4\",\"event_time\":1794526566,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/thread-broadcast.json b/packages/chat/slack/test/fixtures/events/thread-broadcast.json new file mode 100644 index 00000000000..2589c2162ff --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/thread-broadcast.json @@ -0,0 +1,50 @@ +{ + "case": "A thread reply that mentions the bot, sent with also send to channel", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516866.701379", + "request": { + "thread_ts": "1794516865.426549", + "reply_broadcast": true, + "markdown_text": "broadcast to <@U0CCLMD4S1L> [EFF-3694 20:54:24]" + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:27.584Z", + "headers": { + "x-slack-request-timestamp": "1794516867", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"subtype\":\"thread_broadcast\",\"bot_id\":\"B0CCQH77VA5\",\"thread_ts\":\"1794516865.426549\",\"root\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794516866.701379\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}]},\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"broadcast to <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"g1vhxK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"broadcast to \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"ts\":\"1794516866.701379\"},\"previous_message\":{\"subtype\":\"thread_broadcast\",\"bot_id\":\"B0CCQH77VA5\",\"thread_ts\":\"1794516865.426549\",\"root\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794516866.701379\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}]},\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516866.701379\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"broadcast to <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"g1vhxK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"broadcast to \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794516866.058500\",\"event_ts\":\"1794516866.058500\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CHC158P41\",\"event_time\":1794516866,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T20:54:27.744Z", + "headers": { + "x-slack-request-timestamp": "1794516866", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"thread_broadcast\",\"bot_id\":\"B0CCQH77VA5\",\"thread_ts\":\"1794516865.426549\",\"root\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":1,\"reply_users_count\":1,\"latest_reply\":\"1794516866.701379\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}]},\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516866.701379\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"broadcast to <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"g1vhxK\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"broadcast to \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516866.701379\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH3SZSGKV\",\"event_time\":1794516866,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/thread-reply.json b/packages/chat/slack/test/fixtures/events/thread-reply.json new file mode 100644 index 00000000000..e59a7fcb24a --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/thread-reply.json @@ -0,0 +1,36 @@ +{ + "case": "A thread reply that mentions the bot", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516867.999329", + "request": { + "thread_ts": "1794516865.426549", + "markdown_text": "reply to <@U0CCLMD4S1L> [EFF-3694 20:54:24]" + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:29.606Z", + "headers": { + "x-slack-request-timestamp": "1794516868", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516867.999329\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"reply to <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"parent_user_id\":\"U0CCLMD4S1L\",\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"nx\\/ZVl\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"reply to \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516867.999329\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CGUNNJ3U7\",\"event_time\":1794516867,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/events/thread-root.json b/packages/chat/slack/test/fixtures/events/thread-root.json new file mode 100644 index 00000000000..b401941b859 --- /dev/null +++ b/packages/chat/slack/test/fixtures/events/thread-root.json @@ -0,0 +1,61 @@ +{ + "case": "A root message that mentions the bot, then the message_changed deliveries Slack sent for it as replies and a native stream were added under it", + "provenance": { + "source": "Slack Events API deliveries to the janitor2 app (A0CCHBHDK7X), logged by janitor production's EFF-3709 fixture capture (Effect-TS/janitor a751115)", + "channel": "chat-testing-123 (C0CH1LGJ4F3), a private channel", + "author": "the janitor2 bot user (U0CCLMD4S1L), through the bot token", + "recordedAt": "2026-11-12T20:54:52.189Z", + "body": "The delivered JSON string, verbatim apart from the envelope token member, which the capture removes, and the members listed in a delivery's scrubbed, whose URLs are replaced", + "headers": "The capture's allowlist. The signature is never logged" + }, + "actions": [ + { + "action": "chat.postMessage", + "ts": "1794516865.426549", + "request": { + "markdown_text": "hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]" + } + } + ], + "deliveries": [ + { + "loggedAt": "2026-11-12T20:54:26.289Z", + "headers": { + "x-slack-request-timestamp": "1794516865", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"user\":\"U0CCLMD4S1L\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"channel\":\"C0CH1LGJ4F3\",\"event_ts\":\"1794516865.426549\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH85BHSS2\",\"event_time\":1794516865,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T20:54:49.961Z", + "headers": { + "x-slack-request-timestamp": "1794516888", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":2,\"reply_users_count\":1,\"latest_reply\":\"1794516867.999329\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"agent_session\":{\"status\":\"processing\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"date_status_processing_expire\":1794520488,\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"processing\",\"date_status_processing_expire\":1794520488,\"is_stoppable\":true}]},\"ts\":\"1794516865.426549\"},\"previous_message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":2,\"reply_users_count\":1,\"latest_reply\":\"1794516867.999329\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}]},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794516888.059400\",\"event_ts\":\"1794516888.059400\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CJ4FFFCUQ\",\"event_time\":1794516888,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + }, + { + "loggedAt": "2026-11-12T20:54:51.170Z", + "headers": { + "x-slack-request-timestamp": "1794516891", + "content-type": "application/json" + }, + "body": "{\"team_id\":\"T05479Z6JBW\",\"context_team_id\":\"T05479Z6JBW\",\"context_enterprise_id\":null,\"api_app_id\":\"A0CCHBHDK7X\",\"event\":{\"type\":\"message\",\"subtype\":\"message_changed\",\"message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":3,\"reply_users_count\":1,\"latest_reply\":\"1794516888.347839\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"agent_session\":{\"status\":\"active\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"active\",\"is_stoppable\":true}]},\"ts\":\"1794516865.426549\"},\"previous_message\":{\"user\":\"U0CCLMD4S1L\",\"type\":\"message\",\"ts\":\"1794516865.426549\",\"bot_id\":\"B0CCQH77VA5\",\"app_id\":\"A0CCHBHDK7X\",\"text\":\"hello <@U0CCLMD4S1L> [EFF-3694 20:54:24]\",\"team\":\"T05479Z6JBW\",\"bot_profile\":{\"id\":\"B0CCQH77VA5\",\"deleted\":false,\"name\":\"Janitor\",\"updated\":1793135255,\"app_id\":\"A0CCHBHDK7X\",\"user_id\":\"U0CCLMD4S1L\",\"icons\":{\"image_36\":\"https:\\/\\/scrubbed.invalid\\/image_36\",\"image_48\":\"https:\\/\\/scrubbed.invalid\\/image_48\",\"image_72\":\"https:\\/\\/scrubbed.invalid\\/image_72\"},\"team_id\":\"T05479Z6JBW\"},\"thread_ts\":\"1794516865.426549\",\"reply_count\":3,\"reply_users_count\":1,\"latest_reply\":\"1794516888.347839\",\"reply_users\":[\"U0CCLMD4S1L\"],\"is_locked\":false,\"subscribed\":false,\"blocks\":[{\"type\":\"rich_text\",\"block_id\":\"DQGPyG\",\"elements\":[{\"type\":\"rich_text_section\",\"elements\":[{\"type\":\"text\",\"text\":\"hello \"},{\"type\":\"user\",\"user_id\":\"U0CCLMD4S1L\"},{\"type\":\"text\",\"text\":\" [EFF-3694 20:54:24]\"}]}]}],\"agent_session\":{\"status\":\"processing\",\"agent_bot_user_ids\":[\"U0CCLMD4S1L\"],\"encoded_initiator_user_id\":\"U080YLYB277\",\"date_status_processing_expire\":1794520488,\"agent_statuses\":[{\"agent_bot_user_id\":\"U0CCLMD4S1L\",\"status\":\"processing\",\"date_status_processing_expire\":1794520488,\"is_stoppable\":true}]}},\"channel\":\"C0CH1LGJ4F3\",\"hidden\":true,\"ts\":\"1794516890.059800\",\"event_ts\":\"1794516890.059800\",\"channel_type\":\"group\"},\"type\":\"event_callback\",\"event_id\":\"Ev0CH3T3DM35\",\"event_time\":1794516890,\"authorizations\":[{\"enterprise_id\":null,\"team_id\":\"T05479Z6JBW\",\"user_id\":\"U0CCLMD4S1L\",\"is_bot\":true,\"is_enterprise_install\":false}],\"is_ext_shared_channel\":false,\"event_context\":\"4-eyJldCI6Im1lc3NhZ2UiLCJ0aWQiOiJUMDU0NzlaNkpCVyIsImFpZCI6IkEwQ0NIQkhESzdYIiwiY2lkIjoiQzBDSDFMR0o0RjMifQ\"}", + "scrubbed": [ + "image_36", + "image_48", + "image_72" + ] + } + ] +} diff --git a/packages/chat/slack/test/fixtures/recorded/commands.json b/packages/chat/slack/test/fixtures/recorded/commands.json new file mode 100644 index 00000000000..4e4115081a6 --- /dev/null +++ b/packages/chat/slack/test/fixtures/recorded/commands.json @@ -0,0 +1,682 @@ +{ + "case": "Slash commands on Slack, as Slack sent them to the Request URL and as the adapter answered, with the clicks and submits that followed and what each response_url answered", + "provenance": { + "kind": "recorded", + "source": "The adapter at 53b4d54024 behind a logging proxy (eff-3802-capture.ts), with Tim running /chat-capture on janitor2 (A0CCHBHDK7X)", + "where": "Private channel test-janitor (C0CJMJ5UMUY) in T05479Z6JBW, over the Request URL. Not over Socket Mode", + "recordedAt": "2026-11-14T20:07Z to 20:46Z, during EFF-3802", + "body": "Form fields and payloads verbatim, apart from the verification token, the response_url path and trigger_id, which are replaced with . Network headers are left out. Each answer is the adapter's response body verbatim", + "signature": "Not verified against Slack's secret: the proxy re-signed each request with a throwaway key, so the adapter's verifier ran on the proxy's signature", + "seenButNotInLog": [ + "Every answer here rendered on desktop: a markdown block and an actions block, privately and publicly, in the response body and through response_url", + "The mention answer showed <@U080YLYB277> as literal text, so it mentioned nobody", + "chat.update on the ts of two private answers answered message_not_found, and conversations.history did not list them", + "A late command (503 at 2,505 ms) showed Slackbot's private failure notice on desktop and nothing on iOS", + "The command is not offered inside a thread" + ] + }, + "commands": { + "private": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + }, + "modal": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "modal", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + }, + "mention": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "mention", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + }, + "public": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "public", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + }, + "defer": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "defer", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + }, + "spent": { + "token": "", + "team_id": "T05479Z6JBW", + "team_domain": "effectfulworkspace", + "channel_id": "C0CJMJ5UMUY", + "channel_name": "test-janitor", + "user_id": "U080YLYB277", + "user_name": "tim.smart", + "command": "/chat-capture", + "text": "spent", + "api_app_id": "A0CCHBHDK7X", + "is_enterprise_install": "false", + "response_url": "https://hooks.slack.com/commands/T05479Z6JBW/", + "trigger_id": "" + } + }, + "answers": { + "private": { + "response_type": "ephemeral", + "blocks": [ + { + "type": "markdown", + "text": "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons" + }, + { + "type": "actions", + "block_id": "chat1", + "elements": [ + { + "type": "button", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack" + } + }, + { + "type": "button", + "action_id": "cap-open", + "text": { + "type": "plain_text", + "text": "Open modal" + }, + "style": "primary" + } + ] + } + ], + "text": "**Private answer** with _Markdown_, a list:\n\n- one\n- two\n\nand buttons", + "unfurl_links": false, + "unfurl_media": false + }, + "mention": { + "response_type": "ephemeral", + "blocks": [ + { + "type": "markdown", + "text": "Private answer mentioning <@U080YLYB277>" + } + ], + "text": "Private answer mentioning <@U080YLYB277>", + "unfurl_links": false, + "unfurl_media": false + }, + "public": { + "response_type": "in_channel", + "blocks": [ + { + "type": "markdown", + "text": "**Public answer** with _Markdown_ and buttons" + }, + { + "type": "actions", + "block_id": "chat1", + "elements": [ + { + "type": "button", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack" + } + }, + { + "type": "button", + "action_id": "cap-open", + "text": { + "type": "plain_text", + "text": "Open modal" + }, + "style": "primary" + } + ] + } + ], + "text": "**Public answer** with _Markdown_ and buttons", + "unfurl_links": false, + "unfurl_media": false + }, + "defer": null + }, + "clicks": { + "privateAnswer": { + "type": "block_actions", + "user": { + "id": "U080YLYB277", + "username": "tim.smart", + "name": "tim.smart", + "team_id": "T05479Z6JBW" + }, + "api_app_id": "A0CCHBHDK7X", + "token": "", + "container": { + "type": "message", + "message_ts": "1794686832.000300", + "channel_id": "C0CJMJ5UMUY", + "is_ephemeral": true + }, + "trigger_id": "", + "team": { + "id": "T05479Z6JBW", + "domain": "effectfulworkspace" + }, + "enterprise": null, + "is_enterprise_install": false, + "channel": { + "id": "C0CJMJ5UMUY", + "name": "privategroup" + }, + "state": { + "values": {} + }, + "response_url": "https://hooks.slack.com/actions/T05479Z6JBW/", + "actions": [ + { + "type": "button", + "block_id": "chat1", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack", + "emoji": true + }, + "action_ts": "1794686835.443730" + } + ] + }, + "publicAnswer": { + "type": "block_actions", + "user": { + "id": "U080YLYB277", + "username": "tim.smart", + "name": "tim.smart", + "team_id": "T05479Z6JBW" + }, + "api_app_id": "A0CCHBHDK7X", + "token": "", + "container": { + "type": "message", + "message_ts": "1794686949.699459", + "channel_id": "C0CJMJ5UMUY", + "is_ephemeral": false + }, + "trigger_id": "", + "team": { + "id": "T05479Z6JBW", + "domain": "effectfulworkspace" + }, + "enterprise": null, + "is_enterprise_install": false, + "channel": { + "id": "C0CJMJ5UMUY", + "name": "privategroup" + }, + "message": { + "subtype": "bot_message", + "text": "**Public answer** with _Markdown_ and buttons", + "type": "message", + "ts": "1794686949.699459", + "bot_id": "B0CHV4Y8LLR", + "blocks": [ + { + "type": "rich_text", + "block_id": "D5Nptu", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Public answer", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": " with " + }, + { + "type": "text", + "text": "Markdown", + "style": { + "italic": true + } + }, + { + "type": "text", + "text": " and buttons" + } + ] + } + ] + }, + { + "type": "actions", + "block_id": "chat1", + "elements": [ + { + "type": "button", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack", + "emoji": true + } + }, + { + "type": "button", + "action_id": "cap-open", + "text": { + "type": "plain_text", + "text": "Open modal", + "emoji": true + }, + "style": "primary" + } + ] + } + ] + }, + "state": { + "values": {} + }, + "response_url": "https://hooks.slack.com/actions/T05479Z6JBW/", + "actions": [ + { + "type": "button", + "block_id": "chat1", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack", + "emoji": true + }, + "action_ts": "1794686972.679850" + } + ] + }, + "privateFollowUp": { + "type": "block_actions", + "user": { + "id": "U080YLYB277", + "username": "tim.smart", + "name": "tim.smart", + "team_id": "T05479Z6JBW" + }, + "api_app_id": "A0CCHBHDK7X", + "token": "", + "container": { + "type": "message", + "message_ts": "1794687008.000500", + "channel_id": "C0CJMJ5UMUY", + "is_ephemeral": true + }, + "trigger_id": "", + "team": { + "id": "T05479Z6JBW", + "domain": "effectfulworkspace" + }, + "enterprise": null, + "is_enterprise_install": false, + "channel": { + "id": "C0CJMJ5UMUY", + "name": "privategroup" + }, + "state": { + "values": {} + }, + "response_url": "https://hooks.slack.com/actions/T05479Z6JBW/", + "actions": [ + { + "type": "button", + "block_id": "chat1", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack", + "emoji": true + }, + "action_ts": "1794687011.299505" + } + ] + } + }, + "commandModal": { + "viewsOpen": { + "trigger_id": "", + "view": { + "type": "modal", + "callback_id": "cap-modal", + "title": { + "type": "plain_text", + "text": "Capture" + }, + "submit": { + "type": "plain_text", + "text": "Send" + }, + "private_metadata": "chat1␟C0CJMJ5UMUY␟␟", + "blocks": [ + { + "type": "input", + "block_id": "note", + "label": { + "type": "plain_text", + "text": "Note (type err for a field error)" + }, + "optional": false, + "element": { + "type": "plain_text_input", + "action_id": "note", + "multiline": false + } + } + ] + } + }, + "submitErrors": { + "type": "view_submission", + "team": { + "id": "T05479Z6JBW", + "domain": "effectfulworkspace" + }, + "user": { + "id": "U080YLYB277", + "username": "tim.smart", + "name": "tim.smart", + "team_id": "T05479Z6JBW" + }, + "api_app_id": "A0CCHBHDK7X", + "token": "", + "trigger_id": "", + "view": { + "id": "V0CHM1EJ691", + "team_id": "T05479Z6JBW", + "type": "modal", + "blocks": [ + { + "type": "input", + "block_id": "note", + "label": { + "type": "plain_text", + "text": "Note (type err for a field error)", + "emoji": true + }, + "optional": false, + "dispatch_action": false, + "element": { + "type": "plain_text_input", + "action_id": "note", + "multiline": false, + "dispatch_action_config": { + "trigger_actions_on": [ + "on_enter_pressed" + ] + } + } + } + ], + "private_metadata": "chat1␟C0CJMJ5UMUY␟␟", + "callback_id": "cap-modal", + "state": { + "values": { + "note": { + "note": { + "type": "plain_text_input", + "value": "err" + } + } + } + }, + "hash": "1794686859.YVYq8DJX", + "title": { + "type": "plain_text", + "text": "Capture", + "emoji": true + }, + "clear_on_close": false, + "notify_on_close": false, + "close": null, + "submit": { + "type": "plain_text", + "text": "Send", + "emoji": true + }, + "previous_view_id": null, + "root_view_id": "V0CHM1EJ691", + "app_id": "A0CCHBHDK7X", + "external_id": "", + "app_installed_team_id": "T05479Z6JBW", + "bot_id": "B0CCQH77VA5" + }, + "response_urls": [], + "is_enterprise_install": false, + "enterprise": null + }, + "errorsAnswer": { + "response_action": "errors", + "errors": { + "note": "That is the error you asked for" + } + }, + "submitClose": { + "type": "view_submission", + "team": { + "id": "T05479Z6JBW", + "domain": "effectfulworkspace" + }, + "user": { + "id": "U080YLYB277", + "username": "tim.smart", + "name": "tim.smart", + "team_id": "T05479Z6JBW" + }, + "api_app_id": "A0CCHBHDK7X", + "token": "", + "trigger_id": "", + "view": { + "id": "V0CHM1EJ691", + "team_id": "T05479Z6JBW", + "type": "modal", + "blocks": [ + { + "type": "input", + "block_id": "note", + "label": { + "type": "plain_text", + "text": "Note (type err for a field error)", + "emoji": true + }, + "optional": false, + "dispatch_action": false, + "element": { + "type": "plain_text_input", + "action_id": "note", + "multiline": false, + "dispatch_action_config": { + "trigger_actions_on": [ + "on_enter_pressed" + ] + } + } + } + ], + "private_metadata": "chat1␟C0CJMJ5UMUY␟␟", + "callback_id": "cap-modal", + "state": { + "values": { + "note": { + "note": { + "type": "plain_text_input", + "value": "Hi" + } + } + } + }, + "hash": "1794686859.YVYq8DJX", + "title": { + "type": "plain_text", + "text": "Capture", + "emoji": true + }, + "clear_on_close": false, + "notify_on_close": false, + "close": null, + "submit": { + "type": "plain_text", + "text": "Send", + "emoji": true + }, + "previous_view_id": null, + "root_view_id": "V0CHM1EJ691", + "app_id": "A0CCHBHDK7X", + "external_id": "", + "app_installed_team_id": "T05479Z6JBW", + "bot_id": "B0CCQH77VA5" + }, + "response_urls": [], + "is_enterprise_install": false, + "enterprise": null + }, + "closeAnswer": null + }, + "responseUrl": { + "followUp": { + "request": { + "response_type": "ephemeral", + "blocks": [ + { + "type": "markdown", + "text": "**Deferred answer** to defer, with buttons" + }, + { + "type": "actions", + "block_id": "chat1", + "elements": [ + { + "type": "button", + "action_id": "cap-ack", + "text": { + "type": "plain_text", + "text": "Ack" + } + }, + { + "type": "button", + "action_id": "cap-open", + "text": { + "type": "plain_text", + "text": "Open modal" + }, + "style": "primary" + } + ] + } + ], + "text": "**Deferred answer** to defer, with buttons", + "unfurl_links": false, + "unfurl_media": false + }, + "response": { + "status": 200, + "contentType": "text/html", + "body": "ok" + } + }, + "spent": { + "note": "Seven posts on the spent command's deferral, one a second: 1 to 5 answered 200 ok, 6 and 7 this", + "okStatuses": [ + 200, + 200, + 200, + 200, + 200 + ], + "response": { + "status": 404, + "contentType": "text/html", + "body": "used_url" + } + }, + "expired": { + "note": "30.5 minutes after the command, on all three deferrals", + "responses": [ + { + "status": 404, + "contentType": "text/html", + "body": "expired_url" + }, + { + "status": 404, + "contentType": "text/html", + "body": "expired_url" + }, + { + "status": 404, + "contentType": "text/html", + "body": "expired_url" + } + ] + } + }, + "late": { + "status": 503, + "ms": 2505, + "body": "" + } +} diff --git a/packages/chat/slack/test/fixtures/recorded/file-share.json b/packages/chat/slack/test/fixtures/recorded/file-share.json new file mode 100644 index 00000000000..fc6e3bdc858 --- /dev/null +++ b/packages/chat/slack/test/fixtures/recorded/file-share.json @@ -0,0 +1,351 @@ +{ + "case": "upload a small text file into a channel thread with an initial_comment mentioning the bot; read back", + "recordedAt": "2026-11-12T18:43:52.706Z", + "calls": [ + { + "method": "chat.postMessage", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "markdown_text": "Fixture: file share thread root" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509028.346289", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509028.346289", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: file share thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "kS27k6", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: file share thread root" + } + ] + } + ] + } + ] + } + } + }, + { + "method": "files.getUploadURLExternal", + "encoding": "form", + "note": "files.getUploadURLExternal requires form encoding", + "request": { + "filename": "fixture.txt", + "length": 51 + }, + "httpStatus": 200, + "response": { + "ok": true, + "upload_url": "https://scrubbed.invalid/upload_url", + "file_id": "F0CH6RHTMRU" + } + }, + { + "method": "", + "request": { + "body": "<51 bytes text/plain>" + }, + "httpStatus": 200, + "response": { + "text": "OK - 51" + } + }, + { + "method": "files.completeUploadExternal", + "encoding": "json", + "request": { + "files": [ + { + "id": "F0CH6RHTMRU", + "title": "fixture.txt" + } + ], + "channel_id": "C0CH1LGJ4F3", + "thread_ts": "1794509028.346289", + "initial_comment": "Fixture: file for <@U0CCLMD4S1L>" + }, + "httpStatus": 200, + "response": { + "ok": true, + "files": [ + { + "id": "F0CH6RHTMRU", + "created": 1794509028, + "timestamp": 1794509028, + "mimetype": "", + "filetype": "", + "pretty_type": "", + "user": "U0CCLMD4S1L", + "user_team": "T05479Z6JBW", + "editable": false, + "size": 51, + "mode": "hosted", + "is_external": false, + "external_type": "", + "is_public": false, + "public_url_shared": false, + "display_as_bot": false, + "username": "", + "name": "fixture.txt", + "title": "fixture.txt", + "is_modified_by_ai": false, + "url_private": "https://scrubbed.invalid/url_private", + "url_private_download": "https://scrubbed.invalid/url_private_download", + "media_display_type": "unknown", + "permalink": "https://scrubbed.invalid/permalink", + "permalink_public": "https://scrubbed.invalid/permalink_public", + "comments_count": 0, + "is_starred": false, + "shares": {}, + "channels": [], + "groups": [], + "ims": [], + "has_more_shares": false, + "has_rich_preview": false, + "file_access": "visible" + } + ] + } + }, + { + "method": "conversations.replies", + "encoding": "form", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509028.346289", + "limit": 10 + }, + "httpStatus": 200, + "response": { + "ok": true, + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509028.346289", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: file share thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509028.346289", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509028.831169", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "kS27k6", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: file share thread root" + } + ] + } + ] + } + ] + }, + { + "text": "Fixture: file for <@U0CCLMD4S1L>", + "files": [ + { + "id": "F0CH6RHTMRU", + "created": 1794509028, + "timestamp": 1794509028, + "mimetype": "text/plain", + "filetype": "text", + "pretty_type": "Plain Text", + "user": "U0CCLMD4S1L", + "user_team": "T05479Z6JBW", + "editable": true, + "size": 51, + "mode": "snippet", + "is_external": false, + "external_type": "", + "is_public": false, + "public_url_shared": false, + "display_as_bot": false, + "username": "", + "name": "fixture.txt", + "title": "fixture.txt", + "is_modified_by_ai": false, + "url_private": "https://scrubbed.invalid/url_private", + "url_private_download": "https://scrubbed.invalid/url_private_download", + "permalink": "https://scrubbed.invalid/permalink", + "permalink_public": "https://scrubbed.invalid/permalink_public", + "edit_link": "https://scrubbed.invalid/edit_link", + "preview": "hello from the @effect/chat-slack fixture recorder\n", + "preview_highlight": "
\n
\n
hello from the @effect/chat-slack fixture recorder
\n
\n
\n
\n", + "lines": 2, + "lines_more": 0, + "preview_is_truncated": false, + "is_starred": false, + "skipped_shares": true, + "has_rich_preview": false, + "file_access": "visible" + } + ], + "upload": true, + "user": "U0CCLMD4S1L", + "display_as_bot": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "mP=", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: file for " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + } + ] + } + ] + } + ], + "type": "message", + "ts": "1794509028.831169", + "bot_id": "B0CCQH77VA5", + "thread_ts": "1794509028.346289", + "parent_user_id": "U0CCLMD4S1L" + } + ], + "has_more": false + } + }, + { + "method": "conversations.history", + "encoding": "form", + "note": "thread replies are not in history (expected empty/other message)", + "request": { + "channel": "C0CH1LGJ4F3", + "latest": "1794509028.831169", + "inclusive": true, + "limit": 1 + }, + "httpStatus": 200, + "response": { + "ok": true, + "latest": "1794509028.831169", + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509028.346289", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: file share thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509028.346289", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509028.831169", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "kS27k6", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: file share thread root" + } + ] + } + ] + } + ] + } + ], + "has_more": true, + "pin_count": 0, + "channel_actions_ts": null, + "channel_actions_count": 0, + "response_metadata": { + "next_cursor": "bmV4dF90czoxNzk0NTA5MDA4MDc5NjA5" + } + } + } + ] +} diff --git a/packages/chat/slack/test/fixtures/recorded/markdown-mentions.json b/packages/chat/slack/test/fixtures/recorded/markdown-mentions.json new file mode 100644 index 00000000000..75d06d08952 --- /dev/null +++ b/packages/chat/slack/test/fixtures/recorded/markdown-mentions.json @@ -0,0 +1,425 @@ +{ + "case": "markdown_text with bot mention, channel token, tokens inside inline code and fenced code, <, bold, link; read back", + "recordedAt": "2026-11-12T18:43:24.731Z", + "calls": [ + { + "method": "chat.postMessage", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "markdown_text": "Fixture mentions: hi <@U0CCLMD4S1L> in <#C0CH1LGJ4F3>.\nInline code: `<@U0CCLMD4S1L> <#C0CH1LGJ4F3>`\n```\nfenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>\n```\nEscaped: <not a tag> and a raw < sign, **bold** and [x](https://effect.website)", + "unfurl_links": false, + "unfurl_media": false + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509003.143939", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509003.143939", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture mentions: hi <@U0CCLMD4S1L> in <#C0CH1LGJ4F3>.\nInline code: `<@U0CCLMD4S1L> <#C0CH1LGJ4F3>`\n```fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>```\nEscaped: <not a tag> and a raw < sign, *bold* and ", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "UfdV4+", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture mentions: hi " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": " in " + }, + { + "type": "channel", + "channel_id": "C0CH1LGJ4F3" + }, + { + "type": "text", + "text": "." + }, + { + "type": "text", + "text": "\n" + }, + { + "type": "text", + "text": "Inline code: " + }, + { + "type": "text", + "text": "<@U0CCLMD4S1L> <#C0CH1LGJ4F3>", + "style": { + "code": true + } + } + ] + }, + { + "type": "rich_text_preformatted", + "elements": [ + { + "type": "text", + "text": "fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>" + } + ] + }, + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Escaped: " + }, + { + "type": "text", + "text": "<" + }, + { + "type": "text", + "text": "not a tag" + }, + { + "type": "text", + "text": ">" + }, + { + "type": "text", + "text": " and a raw < sign, " + }, + { + "type": "text", + "text": "bold", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": " and " + }, + { + "type": "link", + "url": "https://effect.website", + "text": "x" + } + ] + } + ] + } + ] + } + } + }, + { + "method": "conversations.replies", + "encoding": "form", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509003.143939", + "limit": 10 + }, + "httpStatus": 200, + "response": { + "ok": true, + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509003.143939", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture mentions: hi <@U0CCLMD4S1L> in <#C0CH1LGJ4F3>.\nInline code: `<@U0CCLMD4S1L> <#C0CH1LGJ4F3>`\n```fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>```\nEscaped: <not a tag> and a raw < sign, *bold* and ", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "UfdV4+", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture mentions: hi " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": " in " + }, + { + "type": "channel", + "channel_id": "C0CH1LGJ4F3" + }, + { + "type": "text", + "text": "." + }, + { + "type": "text", + "text": "\n" + }, + { + "type": "text", + "text": "Inline code: " + }, + { + "type": "text", + "text": "<@U0CCLMD4S1L> <#C0CH1LGJ4F3>", + "style": { + "code": true + } + } + ] + }, + { + "type": "rich_text_preformatted", + "elements": [ + { + "type": "text", + "text": "fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>" + } + ] + }, + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Escaped: " + }, + { + "type": "text", + "text": "<" + }, + { + "type": "text", + "text": "not a tag" + }, + { + "type": "text", + "text": ">" + }, + { + "type": "text", + "text": " and a raw < sign, " + }, + { + "type": "text", + "text": "bold", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": " and " + }, + { + "type": "link", + "url": "https://effect.website", + "text": "x" + } + ] + } + ] + } + ] + } + ], + "has_more": false + } + }, + { + "method": "conversations.history", + "encoding": "form", + "request": { + "channel": "C0CH1LGJ4F3", + "latest": "1794509003.143939", + "inclusive": true, + "limit": 1 + }, + "httpStatus": 200, + "response": { + "ok": true, + "latest": "1794509003.143939", + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509003.143939", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture mentions: hi <@U0CCLMD4S1L> in <#C0CH1LGJ4F3>.\nInline code: `<@U0CCLMD4S1L> <#C0CH1LGJ4F3>`\n```fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>```\nEscaped: <not a tag> and a raw < sign, *bold* and ", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "UfdV4+", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture mentions: hi " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": " in " + }, + { + "type": "channel", + "channel_id": "C0CH1LGJ4F3" + }, + { + "type": "text", + "text": "." + }, + { + "type": "text", + "text": "\n" + }, + { + "type": "text", + "text": "Inline code: " + }, + { + "type": "text", + "text": "<@U0CCLMD4S1L> <#C0CH1LGJ4F3>", + "style": { + "code": true + } + } + ] + }, + { + "type": "rich_text_preformatted", + "elements": [ + { + "type": "text", + "text": "fenced <@U0CCLMD4S1L> <#C0CH1LGJ4F3>" + } + ] + }, + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Escaped: " + }, + { + "type": "text", + "text": "<" + }, + { + "type": "text", + "text": "not a tag" + }, + { + "type": "text", + "text": ">" + }, + { + "type": "text", + "text": " and a raw < sign, " + }, + { + "type": "text", + "text": "bold", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": " and " + }, + { + "type": "link", + "url": "https://effect.website", + "text": "x" + } + ] + } + ] + } + ] + } + ], + "has_more": true, + "pin_count": 0, + "channel_actions_ts": null, + "channel_actions_count": 0, + "response_metadata": { + "next_cursor": "bmV4dF90czoxNzk0NTA5MDAwMzUyNzg5" + } + } + } + ] +} diff --git a/packages/chat/slack/test/fixtures/recorded/stream.json b/packages/chat/slack/test/fixtures/recorded/stream.json new file mode 100644 index 00000000000..6e3d4fd2557 --- /dev/null +++ b/packages/chat/slack/test/fixtures/recorded/stream.json @@ -0,0 +1,587 @@ +{ + "case": "chat.startStream/appendStream/stopStream in a channel thread (recipient = the bot itself), update/delete while open, append and stop after stop, read back", + "recordedAt": "2026-11-12T18:45:13.099Z", + "calls": [ + { + "method": "chat.postMessage", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "markdown_text": "Fixture: streaming thread root" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509110.999799", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509110.999799", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: streaming thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "s71/d3", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: streaming thread root" + } + ] + } + ] + } + ] + } + } + }, + { + "method": "chat.startStream", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "thread_ts": "1794509110.999799", + "recipient_user_id": "U0CCLMD4S1L", + "recipient_team_id": "T05479Z6JBW" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + } + }, + { + "method": "chat.appendStream", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "markdown_text": "Streaming **part one**. " + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + } + }, + { + "method": "chat.appendStream", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "markdown_text": "Part two with <@U0CCLMD4S1L>." + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + } + }, + { + "method": "conversations.replies", + "encoding": "form", + "note": "read while stream is open", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509110.999799", + "limit": 10 + }, + "httpStatus": 200, + "response": { + "ok": true, + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509110.999799", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: streaming thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509111.134189", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "s71/d3", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: streaming thread root" + } + ] + } + ] + } + ], + "agent_session": { + "status": "processing", + "agent_bot_user_ids": [ + "U0CCLMD4S1L" + ], + "date_status_processing_expire": 1794512711, + "agent_statuses": [ + { + "agent_bot_user_id": "U0CCLMD4S1L", + "status": "processing", + "date_status_processing_expire": 1794512711, + "is_stoppable": true + } + ] + } + }, + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509111.134189", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Streaming *part one*. Part two with <@U0CCLMD4S1L>.", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "parent_user_id": "U0CCLMD4S1L", + "streaming_state": "in_progress", + "blocks": [ + { + "type": "rich_text", + "block_id": "NylRdI", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Streaming " + }, + { + "type": "text", + "text": "part one", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": ". Part two with " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": "." + } + ] + } + ] + } + ] + } + ], + "has_more": false + } + }, + { + "method": "chat.update", + "encoding": "json", + "note": "update while stream is open", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "markdown_text": "update while open" + }, + "httpStatus": 200, + "response": { + "ok": false, + "error": "streaming_state_conflict" + } + }, + { + "method": "chat.delete", + "encoding": "json", + "note": "delete while stream is open", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + }, + "httpStatus": 200, + "response": { + "ok": false, + "error": "cant_delete_message" + } + }, + { + "method": "chat.stopStream", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509111.134189", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Streaming *part one*. Part two with <@U0CCLMD4S1L>.", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "parent_user_id": "U0CCLMD4S1L", + "blocks": [ + { + "type": "rich_text", + "block_id": "NylRdI", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Streaming " + }, + { + "type": "text", + "text": "part one", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": ". Part two with " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": "." + } + ] + } + ] + } + ], + "streaming_state": "completed" + } + } + }, + { + "method": "chat.appendStream", + "encoding": "json", + "note": "append after stop", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "markdown_text": "after stop" + }, + "httpStatus": 200, + "response": { + "ok": false, + "error": "message_not_in_streaming_state" + } + }, + { + "method": "chat.stopStream", + "encoding": "json", + "note": "second stop", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189" + }, + "httpStatus": 200, + "response": { + "ok": false, + "error": "message_not_in_streaming_state" + } + }, + { + "method": "conversations.replies", + "encoding": "form", + "note": "read after stop", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509110.999799", + "limit": 10 + }, + "httpStatus": 200, + "response": { + "ok": true, + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509110.999799", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: streaming thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509111.134189", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "s71/d3", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: streaming thread root" + } + ] + } + ] + } + ], + "agent_session": { + "status": "active", + "agent_bot_user_ids": [ + "U0CCLMD4S1L" + ], + "agent_statuses": [ + { + "agent_bot_user_id": "U0CCLMD4S1L", + "status": "active", + "is_stoppable": true + } + ] + } + }, + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509111.134189", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Streaming *part one*. Part two with <@U0CCLMD4S1L>.", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "parent_user_id": "U0CCLMD4S1L", + "blocks": [ + { + "type": "rich_text", + "block_id": "NylRdI", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Streaming " + }, + { + "type": "text", + "text": "part one", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": ". Part two with " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": "." + } + ] + } + ] + } + ], + "streaming_state": "completed" + } + ], + "has_more": false + } + }, + { + "method": "chat.update", + "encoding": "json", + "note": "update after stop", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "markdown_text": "Streaming part one. Part two (edited after stop)." + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509111.134189", + "text": "Streaming part one. Part two (edited after stop).", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "edited": { + "user": "U0CCLMD4S1L", + "ts": "1794509113.000000" + }, + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Streaming part one. Part two (edited after stop).", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509110.999799", + "parent_user_id": "U0CCLMD4S1L", + "blocks": [ + { + "type": "rich_text", + "block_id": "8CcUt3", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Streaming part one. Part two (edited after stop)." + } + ] + } + ] + } + ], + "streaming_state": "completed" + } + } + } + ] +} diff --git a/packages/chat/slack/test/fixtures/recorded/thread-broadcast.json b/packages/chat/slack/test/fixtures/recorded/thread-broadcast.json new file mode 100644 index 00000000000..9a657e55bf3 --- /dev/null +++ b/packages/chat/slack/test/fixtures/recorded/thread-broadcast.json @@ -0,0 +1,405 @@ +{ + "case": "thread reply with reply_broadcast: true mentioning the bot; read back via history (channel copy) and replies", + "recordedAt": "2026-11-12T18:43:26.438Z", + "calls": [ + { + "method": "chat.postMessage", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "markdown_text": "Fixture: broadcast thread root" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509004.758279", + "message": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.758279", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "21wkcy", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast thread root" + } + ] + } + ] + } + ] + } + } + }, + { + "method": "chat.postMessage", + "encoding": "json", + "request": { + "channel": "C0CH1LGJ4F3", + "thread_ts": "1794509004.758279", + "reply_broadcast": true, + "markdown_text": "Fixture: broadcast reply to <@U0CCLMD4S1L>" + }, + "httpStatus": 200, + "response": { + "ok": true, + "channel": "C0CH1LGJ4F3", + "ts": "1794509004.811879", + "message": { + "subtype": "thread_broadcast", + "bot_id": "B0CCQH77VA5", + "thread_ts": "1794509004.758279", + "root": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.758279", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509004.758279", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509004.811879", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "21wkcy", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast thread root" + } + ] + } + ] + } + ] + }, + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.811879", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast reply to <@U0CCLMD4S1L>", + "blocks": [ + { + "type": "rich_text", + "block_id": "lMh39u", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast reply to " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + } + ] + } + ] + } + ] + } + } + }, + { + "method": "conversations.history", + "encoding": "form", + "request": { + "channel": "C0CH1LGJ4F3", + "latest": "1794509004.811879", + "inclusive": true, + "limit": 1 + }, + "httpStatus": 200, + "response": { + "ok": true, + "latest": "1794509004.811879", + "messages": [ + { + "subtype": "thread_broadcast", + "bot_id": "B0CCQH77VA5", + "thread_ts": "1794509004.758279", + "root": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.758279", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509004.758279", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509004.811879", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "21wkcy", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast thread root" + } + ] + } + ] + } + ] + }, + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.811879", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast reply to <@U0CCLMD4S1L>", + "blocks": [ + { + "type": "rich_text", + "block_id": "lMh39u", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast reply to " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + } + ] + } + ] + } + ] + } + ], + "has_more": true, + "pin_count": 0, + "channel_actions_ts": null, + "channel_actions_count": 0, + "response_metadata": { + "next_cursor": "bmV4dF90czoxNzk0NTA5MDA0NzU4Mjc5" + } + } + }, + { + "method": "conversations.replies", + "encoding": "form", + "request": { + "channel": "C0CH1LGJ4F3", + "ts": "1794509004.758279", + "limit": 10 + }, + "httpStatus": 200, + "response": { + "ok": true, + "messages": [ + { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.758279", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509004.758279", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509004.811879", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "21wkcy", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast thread root" + } + ] + } + ] + } + ] + }, + { + "subtype": "thread_broadcast", + "bot_id": "B0CCQH77VA5", + "thread_ts": "1794509004.758279", + "root": { + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.758279", + "bot_id": "B0CCQH77VA5", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast thread root", + "team": "T05479Z6JBW", + "bot_profile": { + "id": "B0CCQH77VA5", + "deleted": false, + "name": "Janitor", + "updated": 1793135255, + "app_id": "A0CCHBHDK7X", + "user_id": "U0CCLMD4S1L", + "icons": { + "image_36": "https://scrubbed.invalid/image_36", + "image_48": "https://scrubbed.invalid/image_48", + "image_72": "https://scrubbed.invalid/image_72" + }, + "team_id": "T05479Z6JBW" + }, + "thread_ts": "1794509004.758279", + "reply_count": 1, + "reply_users_count": 1, + "latest_reply": "1794509004.811879", + "reply_users": [ + "U0CCLMD4S1L" + ], + "is_locked": false, + "subscribed": false, + "blocks": [ + { + "type": "rich_text", + "block_id": "21wkcy", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast thread root" + } + ] + } + ] + } + ] + }, + "user": "U0CCLMD4S1L", + "type": "message", + "ts": "1794509004.811879", + "app_id": "A0CCHBHDK7X", + "text": "Fixture: broadcast reply to <@U0CCLMD4S1L>", + "blocks": [ + { + "type": "rich_text", + "block_id": "lMh39u", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Fixture: broadcast reply to " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + } + ] + } + ] + } + ] + } + ], + "has_more": false + } + } + ] +} diff --git a/packages/chat/slack/test/fixtures/synthetic/own-messages.json b/packages/chat/slack/test/fixtures/synthetic/own-messages.json new file mode 100644 index 00000000000..1d7e6881fac --- /dev/null +++ b/packages/chat/slack/test/fixtures/synthetic/own-messages.json @@ -0,0 +1,444 @@ +{ + "synthetic": "Hand-written, not read back from Slack: the test workspace was not available for EFF-3750. The shapes follow the recorded rich_text in ../recorded and Slack's rich_text docs. Replace each with a recording when one is made.", + "case": "Stage 0 fixture 13: the bot's own posts read back, for the constructs and message kinds the recordings lack (strike, quotes, lists, named links, broadcasts, user groups, emoji, buttons, file-only, literal Markdown characters, code span padding).", + "cases": [ + { + "name": "strike, bold italic and a code span holding a backtick", + "markdown": "~~gone~~ ***both*** ``a ` b``", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000100", + "text": "~~gone~~ ***both*** ``a ` b``", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "gone", + "style": { + "strike": true + } + }, + { + "type": "text", + "text": " " + }, + { + "type": "text", + "text": "both", + "style": { + "bold": true, + "italic": true + } + }, + { + "type": "text", + "text": " " + }, + { + "type": "text", + "text": "a ` b", + "style": { + "code": true + } + } + ] + } + ] + } + ] + } + }, + { + "name": "a quote over two lines with a mention", + "markdown": "> quoted <@U0CCLMD4S1L>\n> second line", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000200", + "text": "> quoted <@U0CCLMD4S1L>\n> second line", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_quote", + "elements": [ + { + "type": "text", + "text": "quoted " + }, + { + "type": "user", + "user_id": "U0CCLMD4S1L" + }, + { + "type": "text", + "text": "\nsecond line" + } + ] + } + ] + } + ] + } + }, + { + "name": "a bullet list with a nested item, then an ordered list from 3", + "markdown": "- one\n - nested\n- two\n3. three\n4. four", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000300", + "text": "- one\n - nested\n- two\n3. three\n4. four", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_list", + "style": "bullet", + "indent": 0, + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "one" + } + ] + } + ] + }, + { + "type": "rich_text_list", + "style": "bullet", + "indent": 1, + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "nested" + } + ] + } + ] + }, + { + "type": "rich_text_list", + "style": "bullet", + "indent": 0, + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "two" + } + ] + } + ] + }, + { + "type": "rich_text_list", + "style": "ordered", + "indent": 0, + "offset": 2, + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "three" + } + ] + }, + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "four" + } + ] + } + ] + } + ] + } + ] + } + }, + { + "name": "a named link, a broadcast, a user group, a toned emoji and a custom one", + "markdown": "see [the docs](https://effect.website) 👍🏼 :party-parrot:", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000400", + "text": "see <https://effect.website|the docs> :+1::skin-tone-3: :party-parrot:", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "see " + }, + { + "type": "link", + "url": "https://effect.website", + "text": "the docs" + }, + { + "type": "text", + "text": " " + }, + { + "type": "broadcast", + "range": "here" + }, + { + "type": "text", + "text": " " + }, + { + "type": "usergroup", + "usergroup_id": "S0123ABC" + }, + { + "type": "text", + "text": " " + }, + { + "type": "emoji", + "name": "+1", + "unicode": "1f44d-1f3fc", + "skin_tone": 3 + }, + { + "type": "text", + "text": " " + }, + { + "type": "emoji", + "name": "party-parrot" + } + ] + } + ] + } + ] + } + }, + { + "name": "with buttons: the actions block adds nothing to text", + "markdown": "Pick one", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000500", + "text": "Pick one", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Pick one" + } + ] + } + ] + }, + { + "type": "actions", + "block_id": "act", + "elements": [ + { + "type": "button", + "action_id": "a1", + "value": "yes", + "text": { + "type": "plain_text", + "text": "Approve" + } + }, + { + "type": "button", + "action_id": "a2", + "value": "no", + "text": { + "type": "plain_text", + "text": "Deny" + } + } + ] + } + ] + } + }, + { + "name": "file-only: the placeholder is the escaped file names", + "markdown": "report.pdf, data.csv", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000600", + "text": "report.pdf, data.csv", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "report.pdf, data.csv" + } + ] + } + ] + } + ], + "files": [ + { + "id": "F0SYN00001", + "name": "report.pdf", + "title": "report.pdf", + "mimetype": "application/pdf", + "size": 1024, + "url_private": "https://files.slack.com/files-pri/T05479Z6JBW-F0SYN00001/report.pdf" + }, + { + "id": "F0SYN00002", + "name": "data.csv", + "title": "data.csv", + "mimetype": "text/csv", + "size": 12, + "url_private": "https://files.slack.com/files-pri/T05479Z6JBW-F0SYN00002/data.csv" + } + ] + } + }, + { + "name": "Markdown characters a text element holds as literals", + "markdown": "\\*not italic\\*, \\`not code\\`, \\~not struck\\~, \\[not\\](a link), snake_case, \\_edge\\_, C:\\dir\\\\\\*\n\\# not a heading\n\\- not a list\n1\\. not ordered\n\\> not a quote\n**2 \\* 3**", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000700", + "text": "*not italic*, `not code`, ~not struck~, [not](a link), snake_case, _edge_, C:\\dir\\*\n# not a heading\n- not a list\n1. not ordered\n> not a quote\n*2 * 3*", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "*not italic*, `not code`, ~not struck~, [not](a link), snake_case, _edge_, C:\\dir\\*\n# not a heading\n- not a list\n1. not ordered\n> not a quote\n" + }, + { + "type": "text", + "text": "2 * 3", + "style": { + "bold": true + } + } + ] + } + ] + } + ] + } + }, + { + "name": "a code span with a space at each end", + "markdown": "pad ` foo ` end", + "message": { + "type": "message", + "user": "U0CCLMD4S1L", + "bot_id": "B0CCHBHDK7X", + "app_id": "A0CCHBHDK7X", + "team": "T05479Z6JBW", + "ts": "1794600000.000800", + "text": "pad ` foo ` end", + "blocks": [ + { + "type": "rich_text", + "block_id": "syn", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "pad " + }, + { + "type": "text", + "text": " foo ", + "style": { + "code": true + } + }, + { + "type": "text", + "text": " end" + } + ] + } + ] + } + ] + } + } + ] +} diff --git a/packages/chat/slack/test/interactions.test.ts b/packages/chat/slack/test/interactions.test.ts new file mode 100644 index 00000000000..49be542e129 --- /dev/null +++ b/packages/chat/slack/test/interactions.test.ts @@ -0,0 +1,273 @@ +/** + * Interactivity: the normaliser on built payloads, the answers + * `SlackChat.interaction` gives, `layerInteractivity` over HTTP, and a + * command's `followUp`. The recorded command payloads are in + * `commands.test.ts`. + */ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import * as NodeCrypto from "@effect/platform-node/NodeCrypto" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type { ChatError } from "effect/chat/ChatError" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as ChatUi from "effect/chat/ChatUi" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as HttpRouter from "effect/http/HttpRouter" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import { appId, authTest, channel, commandFields, makeFake, responseUrl, slackLayer, team } from "./utils.ts" + +const join = "\u241F" +const user = { id: "U080YLYB277", username: "tim", team_id: team } + +const click = (action: Record, message: Record = {}) => ({ + type: "block_actions", + api_app_id: appId, + token: "verification", + trigger_id: "trigger-1", + user, + team: { id: team }, + container: { type: "message", message_ts: "1794600001.000100", channel_id: channel }, + channel: { id: channel }, + message: { ts: "1794600001.000100", ...message }, + actions: [{ block_id: "chat1", ...action }] +}) + +const submit = (privateMetadata: string, values: Record) => ({ + type: "view_submission", + api_app_id: appId, + user, + team: { id: team }, + trigger_id: "trigger-2", + view: { id: "V1", callback_id: "form", private_metadata: privateMetadata, state: { values } }, + response_urls: [] +}) + +const input = (value: string | null) => ({ field: { type: "plain_text_input", value } }) + +describe("normalizeInteraction", () => { + const normalize = (payload: unknown) => SlackChat.normalizeInteraction(payload, { teamId: team }) + + it("ignores what the adapter did not build", () => { + assert.isTrue(Option.isNone(normalize(click({ type: "button", action_id: join + "0", url: "https://x.test" })))) + assert.isTrue(Option.isNone(normalize(click({ type: "button", action_id: "go", block_id: "theirs" })))) + assert.isTrue(Option.isNone(normalize(submit("theirs", {})))) + }) + + it("gives a click on a thread's root the channel, and a raw payload without the token", () => { + const event = Option.getOrThrow( + normalize(click({ type: "button", action_id: "go" }, { thread_ts: "1794600001.000100" })) + ) + assert.strictEqual(event.thread.id, `${team}/${channel}`) + assert.isFalse("token" in (event.raw as object)) + }) + + it("gives a submit the consumer's metadata, which may hold the join, and own keys only", () => { + const metadata = `a${join}b` + const packed = ["chat1", channel, "1794600000.000100", "1794600001.000100", metadata].join(join) + // As JSON.parse gives it, with `__proto__` an own key. + const values = JSON.parse( + JSON.stringify({ + proto: input("own"), + empty: input(null), + pick: { pick: { type: "static_select", selected_option: { value: "b" } } }, + none: { none: { type: "static_select", selected_option: null } } + }).replace(`"proto"`, `"__proto__"`) + ) + const event = Option.getOrThrow(normalize(submit(packed, values))) + assert.strictEqual(event._tag, "ModalSubmitted") + if (event._tag !== "ModalSubmitted") return + assert.strictEqual(event.metadata, metadata) + assert.isTrue(Object.hasOwn(event.values, "__proto__")) + assert.strictEqual(event.values["__proto__"], "own") + assert.deepStrictEqual(Object.keys(event.values).sort(), ["__proto__", "pick"]) + }) +}) + +const recording = () => { + const refused: Array = [] + let onClick: () => Effect.Effect = () => Effect.succeed(ChatInteraction.ack) + let onSubmit: () => Effect.Effect = () => Effect.succeed(ChatInteraction.close) + let onCommand: (command: ChatInteraction.CommandInvoked) => Effect.Effect = () => + Effect.succeed(ChatInteraction.ack) + return { + refused, + set click(f: typeof onClick) { + onClick = f + }, + set submit(f: typeof onSubmit) { + onSubmit = f + }, + set command(f: typeof onCommand) { + onCommand = f + }, + layer: Layer.succeed(ChatInteraction.Handler, { + click: () => onClick(), + submit: () => onSubmit(), + command: (command) => onCommand(command), + refused: (_, error) => Effect.sync(() => void refused.push(error)) + }) + } +} + +const modal = ChatUi.Modal.make({ + id: "form", + title: "Form", + elements: [ChatUi.TextInput.make({ id: "field", label: "Field" })] +}) + +/** What the adapter packs for a modal a click on a channel message opened. */ +const packed = ["chat1", channel, "", "1794600001.000100", ""].join(join) + +describe("SlackChat.interaction", () => { + const interact = (fake: ReturnType, handler: ReturnType, payload: unknown) => + Effect.gen(function*() { + const slack = yield* SlackChat.SlackChat + return yield* slack.interaction(payload, { deadline: Duration.millis(50) }) + }).pipe(Effect.provide(handler.layer), Effect.provide(slackLayer(fake))) + + it.live("gives Late for a late submit and nothing for a late click", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest }) + const handler = recording() + handler.submit = () => Effect.as(Effect.never, ChatInteraction.close) + handler.click = () => Effect.as(Effect.never, ChatInteraction.ack) + const late = yield* interact(fake, handler, submit(packed, {})) + assert.deepStrictEqual(late, ChatInteraction.Late.make({})) + assert.isUndefined(yield* interact(fake, handler, click({ type: "button", action_id: "go" }))) + })) + + // Keep validation and API error mapping independent of the handler's wall-clock deadline. + it.effect("refuses a modal Slack refuses to Handler.refused, still acking", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest, "views.open": { ok: false, error: "expired_trigger_id" } }) + const handler = recording() + handler.click = () => Effect.succeed(ChatInteraction.OpenModal.make({ modal })) + assert.isUndefined(yield* interact(fake, handler, click({ type: "button", action_id: "go" }))) + assert.deepStrictEqual( + handler.refused.map((error) => [error.op, error.reason._tag, error.reason.detail]), + [["openModal", "NotFound", "interaction_expired"]] + ) + })) +}) + +describe("followUp", () => { + it.effect("refuses a response_url off Slack before any call", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest }) + const failure = (url: string) => + Chat.Chat.pipe( + Effect.flatMap((chat) => + chat.followUp( + ChatInteraction.defer(ChatInteraction.CommandInvoked.make({ + id: "summarize", + text: "", + thread: SlackChat.thread({ team, channel }), + userId: "U080YLYB277", + raw: { response_url: url } + })), + { markdown: "x" } + ) + ), + Effect.flip, + Effect.provide(slackLayer(fake)) + ) + for (const url of ["https://hooks.slack.com.evil.test/commands/1", responseUrl.replace("https:", "http:")]) { + const error = yield* failure(url) + assert.deepStrictEqual([error.op, error.reason._tag, error.reason.detail], [ + "followUp", + "InvalidRequest", + "invalid_response_url" + ]) + } + assert.strictEqual(fake.called("response_url").length, 0) + })) +}) + +describe("layerInteractivity", () => { + const secret = "8f742231b10e8888abcd99yyyzzz85a5" + + const sign = async (timestamp: string, body: string) => { + const encoder = new TextEncoder() + const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, [ + "sign" + ]) + const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder.encode(`v0:${timestamp}:${body}`))) + return "v0=" + Array.from(mac, (byte) => byte.toString(16).padStart(2, "0")).join("") + } + + const post = async ( + payload: unknown, + options?: { readonly signature?: string; readonly form?: Array<[string, string]> } + ) => { + const body = new URLSearchParams(options?.form ?? { payload: JSON.stringify(payload) }).toString() + const timestamp = String(Math.floor(Date.now() / 1000)) + return new Request("http://localhost/slack/interactivity", { + method: "POST", + body, + headers: { + "content-type": "application/x-www-form-urlencoded", + "x-slack-request-timestamp": timestamp, + "x-slack-signature": options?.signature ?? await sign(timestamp, body) + } + }) + } + + const serve = (handler: ReturnType) => { + const app = SlackChat.layerInteractivity({ + path: "/slack/interactivity", + signingSecret: Redacted.make(secret), + appId, + deadline: Duration.millis(50) + }).pipe( + Layer.provide(SlackChat.layer({ botToken: Redacted.make("xoxb-test"), teamId: team })), + Layer.provide(Chat.layer), + Layer.provide(NodeCrypto.layerMac), + Layer.provide(handler.layer), + Layer.provide(makeFake({ "auth.test": authTest }).layer) + ) + const { dispose, handler: web } = HttpRouter.toWebHandler(app, { disableLogger: true }) + const send = (payload: unknown, options?: Parameters[1]) => + Effect.promise(() => post(payload, options).then(web)) + return { dispose: Effect.promise(() => dispose()), send } + } + + it.live("takes a command's form, keeping a repeated field's first value, and answers a late one 503", () => + Effect.gen(function*() { + const handler = recording() + const { dispose, send } = serve(handler) + const seen: Array = [] + handler.command = (event) => + Effect.sync(() => seen.push(event.text)).pipe( + Effect.as(ChatInteraction.respond({ markdown: "Hi", visibility: "public" })) + ) + const form = (text: string) => + [...Object.entries(commandFields("/deploy", text)), ["text", "second"]] as Array<[string, string]> + const answered = yield* send(undefined, { form: form("first") }) + assert.strictEqual(answered.status, 200) + assert.propertyVal(yield* Effect.promise(() => answered.json()), "response_type", "in_channel") + assert.deepStrictEqual(seen, ["first"]) + + handler.command = () => Effect.as(Effect.never, ChatInteraction.ack) + assert.strictEqual((yield* send(undefined, { form: form("late") })).status, 503) + yield* dispose + })) + + it.live("answers errors as JSON, and a bad signature 401", () => + Effect.gen(function*() { + const handler = recording() + const { dispose, send } = serve(handler) + handler.submit = () => Effect.succeed(ChatInteraction.errors({ field: "Too short" })) + const errors = yield* send(submit(packed, {})) + assert.strictEqual(errors.status, 200) + assert.deepStrictEqual(yield* Effect.promise(() => errors.json()), { + response_action: "errors", + errors: { field: "Too short" } + }) + + assert.strictEqual((yield* send(submit(packed, {}), { signature: "v0=00" })).status, 401) + yield* dispose + })) +}) diff --git a/packages/chat/slack/test/live.test.ts b/packages/chat/slack/test/live.test.ts new file mode 100644 index 00000000000..5cf50c115c4 --- /dev/null +++ b/packages/chat/slack/test/live.test.ts @@ -0,0 +1,86 @@ +/** + * The conformance suite against a real Slack workspace. Opt-in: it runs only + * when these are set, so CI skips it. + * + * - `SLACK_LIVE_BOT_TOKEN`, `SLACK_LIVE_TEAM_ID` and `SLACK_LIVE_CHANNEL`: the + * bot and a channel it may write to. + * - `SLACK_LIVE_USER_TOKEN`: a user token with `chat:write` and + * `channels:history`, for the rules that need another identity's + * message. Without it those rules fail at `userPosts`. + */ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import * as SlackWebApi from "@effect/chat-slack/SlackWebApi" +import { describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { conformance } from "effect/chat/ChatTest" +import type * as ChatThread from "effect/chat/ChatThread" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as Semaphore from "effect/Semaphore" + +const botToken = process.env.SLACK_LIVE_BOT_TOKEN +const userToken = process.env.SLACK_LIVE_USER_TOKEN +const teamId = process.env.SLACK_LIVE_TEAM_ID +const channel = process.env.SLACK_LIVE_CHANNEL + +const field = (u: unknown, key: string): any => + typeof u === "object" && u !== null && !Array.isArray(u) ? (u as Record)[key] : undefined + +describe.skipIf(botToken === undefined || teamId === undefined || channel === undefined)("live conformance", () => { + const layer = SlackChat.layer({ botToken: Redacted.make(botToken!), teamId: teamId! }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(FetchHttpClient.layer) + ) + + /** Posts as the user, reads the message back and normalises it as an inbound `message` event. */ + const userPosts = (thread: ChatThread.ChatThread, text: string): Effect.Effect => + Effect.gen(function*() { + if (userToken === undefined) return yield* Effect.die(new Error("SLACK_LIVE_USER_TOKEN is not set")) + const user = yield* SlackWebApi.make({ botToken: Redacted.make(userToken) }) + const bot = yield* SlackWebApi.make({ botToken: Redacted.make(botToken!) }) + const parts = SlackChat.parts(thread) + if (parts._tag === "None") return yield* Effect.die(new Error("not a Slack thread")) + const { threadTs } = parts.value + const posted = yield* user.call("chat.postMessage", { + channel: parts.value.channel, + text, + ...(threadTs === undefined ? {} : { thread_ts: threadTs }) + }, { write: true }) + const ts = field(posted, "ts") + const read = yield* user.call("conversations.replies", { + channel: parts.value.channel, + ts, + latest: ts, + oldest: ts, + inclusive: true, + limit: 1 + }) + const messages = field(read, "messages") + const message = Array.isArray(messages) ? messages.find((m: any) => m.ts === ts) : undefined + const auth = yield* bot.call("auth.test", {}) + const events = SlackChat.normalize({ + type: "event_callback", + team_id: parts.value.team, + event: { ...message, type: "message", channel: parts.value.channel, event_ts: ts } + }, { teamId: parts.value.team, botUserId: field(auth, "user_id") ?? "" }) + if (events.length !== 1) return yield* Effect.die(new Error(`userPosts normalised to ${events.length} events`)) + return events[0] + }).pipe(Effect.provide(FetchHttpClient.layer), Effect.orDie) + + const rules = conformance({ + platform: "slack", + thread: SlackChat.thread({ team: teamId ?? "", channel: channel ?? "" }), + userPosts, + parseMention: SlackChat.parseMention + }) + + // The rules share one channel and the project runs tests concurrently, so + // one rule at a time. + const one = Semaphore.makeUnsafe(1) + for (const [name, rule] of Object.entries(rules)) { + it.live(name, () => rule.pipe(Effect.provide(layer), Semaphore.withPermits(one, 1)), 600_000) + } +}) diff --git a/packages/chat/slack/test/normalize.test.ts b/packages/chat/slack/test/normalize.test.ts new file mode 100644 index 00000000000..1b56a6ead89 --- /dev/null +++ b/packages/chat/slack/test/normalize.test.ts @@ -0,0 +1,122 @@ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { checkEvent } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { botUserId, checkOptions, fixture, messageEnvelope, team } from "./utils.ts" + +const context = { teamId: team, botUserId } + +const created = (events: ReadonlyArray): ChatEvent.MessageCreated => { + assert.strictEqual(events.length, 1) + const event = events[0] + assert(event._tag === "MessageCreated") + return event +} + +const normalize = (message: Record, options?: Parameters[1]) => + created(SlackChat.normalize(messageEnvelope(message, options), context)) + +const section = ( + elements: Array +) => [{ type: "rich_text", elements: [{ type: "rich_text_section", elements }] }] + +describe("normalize", () => { + it.effect("without rich_text, text is scanned outside code", () => + Effect.gen(function*() { + const event = normalize({ + ts: "1794509003.000001", + user: "U080YLYB277", + text: "hey <@U0CCLMD4S1L> `<@U999>` and " + }) + yield* checkEvent(event, checkOptions) + assert.deepStrictEqual(event.message.mentions.map((mention) => mention.id), [botUserId, "here"]) + assert.isTrue(event.message.mentions[0].isSelf) + })) + + it.effect("text never adds a mention the rich_text block lacks", () => + Effect.gen(function*() { + // Block items: literal U1, real U1, real U2, literal bot. + const event = normalize({ + ts: "1794509003.000002", + user: "U080YLYB277", + text: `<@U1AAAA> <@U2BBBB> <@${botUserId}>`, + blocks: section([ + { type: "text", text: "<@U1AAAA>" }, + { type: "user", user_id: "U1AAAA" }, + { type: "text", text: " " }, + { type: "user", user_id: "U2BBBB" }, + { type: "text", text: ` <@${botUserId}>` } + ]) + }) + yield* checkEvent(event, checkOptions) + assert.deepStrictEqual( + event.message.mentions.map(({ id, isSelf, start }) => ({ id, isSelf, start })), + [{ id: "U1AAAA", isSelf: false, start: 0 }, { id: "U2BBBB", isSelf: false, start: 10 }] + ) + })) + + it.effect("a real mention with no token in text, past a literal, ends", () => + Effect.gen(function*() { + // Block items: real U1, literal U9, real U2. Text has only U1. + const event = normalize({ + ts: "1794509003.000004", + user: "U080YLYB277", + text: "<@U1AAAA>", + blocks: section([ + { type: "user", user_id: "U1AAAA" }, + { type: "text", text: " <@U9ZZZZ> " }, + { type: "user", user_id: "U2BBBB" } + ]) + }) + yield* checkEvent(event, checkOptions) + assert.deepStrictEqual(event.message.mentions.map(({ id, start }) => ({ id, start })), [{ + id: "U1AAAA", + start: 0 + }]) + })) + + it("files are attachments, with an id and no URL", () => { + const reply = fixture("file-share").calls.flatMap((call) => call.response?.messages ?? []) + .find((message: any) => message.ts === "1794509028.831169") + assert.deepStrictEqual(normalize(reply).message.attachments, [{ + id: "F0CH6RHTMRU", + name: "fixture.txt", + contentType: "text/plain", + size: 51 + }]) + }) + + it("a D or mpim channel is a direct", () => { + const message = { ts: "1794509003.000002", user: "U080YLYB277", text: "hi" } + assert.strictEqual(normalize(message, { channel: "D0CCGDG0E75" }).kind, "direct") + assert.strictEqual(normalize(message, { channel: "C0GROUP", channelType: "mpim" }).kind, "direct") + }) + + it("a bot_message's author is its bot_id", () => { + const event = normalize({ + ts: "1794509003.000004", + subtype: "bot_message", + bot_id: "B0WEBHOOK", + username: "incoming-webhook", + text: "deploy done <@U0CCLMD4S1L>" + }) + assert.deepStrictEqual(event.message.author, { + id: "B0WEBHOOK", + name: "incoming-webhook", + isBot: true, + isSelf: false + }) + }) + + it("drops other subtypes and other workspaces", () => { + const message = { ts: "1794509003.000005", user: "U080YLYB277", text: "<@U0CCLMD4S1L>" } + assert.deepStrictEqual(SlackChat.normalize(messageEnvelope({ ...message, subtype: "channel_join" }), context), []) + assert.deepStrictEqual(SlackChat.normalize(messageEnvelope(message, { teamId: "T0OTHER" }), context), []) + }) + + it("drops the verification token from raw", () => { + const event = normalize({ ts: "1794509003.000006", user: "U080YLYB277", text: "hi" }) + assert.isFalse("token" in (event.raw as object)) + }) +}) diff --git a/packages/chat/slack/test/richText.test.ts b/packages/chat/slack/test/richText.test.ts new file mode 100644 index 00000000000..8a90710a987 --- /dev/null +++ b/packages/chat/slack/test/richText.test.ts @@ -0,0 +1,65 @@ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { checkMessage } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import * as fs from "node:fs" +import { botUserId, checkOptions, fixture, messageEnvelope, team } from "./utils.ts" + +const context = { teamId: team, botUserId } + +const read = Effect.fnUntraced(function*(message: Record) { + const events = SlackChat.normalize(messageEnvelope(message), context) + assert.strictEqual(events.length, 1) + const event = events[0] as ChatEvent.MessageCreated + assert.strictEqual(event._tag, "MessageCreated") + // Includes that each mention's span parses as its token. + yield* checkMessage(event.message, checkOptions) + return event.message +}) + +/** A message a recorded read returned, by ts. */ +const recorded = (name: string, ts: string): Record => + fixture(name).calls.flatMap((call) => call.response?.messages ?? []).find((message: any) => message.ts === ts) + +const synthetic: { + readonly cases: ReadonlyArray<{ readonly name: string; readonly markdown: string; readonly message: any }> +} = JSON.parse(fs.readFileSync(new URL("./fixtures/synthetic/own-messages.json", import.meta.url), "utf8")) + +describe("the bot's own messages read back", () => { + it.effect("give the Markdown they were posted with, from recordings", () => + Effect.gen(function*() { + // The fixture posted raw markdown_text with `<not a tag>` typed + // as entities. Through the adapter the same message is ``. + const mentionsPost = fixture("markdown-mentions").calls[0].request.markdown_text as string + const cases: ReadonlyArray = [ + ["markdown-mentions", "1794509003.143939", mentionsPost.replace("<not a tag>", "")], + // Two appends. + ["stream", "1794509111.134189", `Streaming **part one**. Part two with <@${botUserId}>.`] + ] + for (const [name, ts, markdown] of cases) { + const message = recorded(name, ts) + const read_ = yield* read(message) + assert.strictEqual(read_.text, markdown, name) + // Slack's own field stays on raw. + assert.strictEqual((read_.raw as any).text, message.text, name) + } + // Slack stored the markdown-mentions text with its fence on one line. + assert.notStrictEqual(recorded("markdown-mentions", "1794509003.143939").text, mentionsPost) + })) + + it.effect("give the Markdown they were posted with, from synthetic messages", () => + Effect.gen(function*() { + for (const { markdown, message, name } of synthetic.cases) { + assert.strictEqual((yield* read(message)).text, markdown, name) + } + })) + + it.effect("someone else's message keeps Slack's text", () => + Effect.gen(function*() { + const { message } = synthetic.cases.find(({ name }) => name.startsWith("Markdown characters"))! + const { app_id: _app, bot_id: _bot, ...rest } = message + const read_ = yield* read({ ...rest, user: "U080YLYB277" }) + assert.strictEqual(read_.text, message.text) + })) +}) diff --git a/packages/chat/slack/test/slack.ts b/packages/chat/slack/test/slack.ts new file mode 100644 index 00000000000..0d17e552e32 --- /dev/null +++ b/packages/chat/slack/test/slack.ts @@ -0,0 +1,160 @@ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { type Interactions, NoSuchElement } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { appId, authTest, botUserId, commandFields, makeFake, team } from "./utils.ts" + +const tim = "U080YLYB277" + +/** + * A Slack that keeps the messages the adapter posts, with their blocks, and + * records every view it opens. `interactions` is the conformance hook: it + * builds Slack's `block_actions` and `view_submission` payloads from what + * was posted and opened, and a slash command's form fields, and sends them + * through `SlackChat.interaction`. A command's + * `response_url` answers `ok`. `refuseFiles` makes the next file upload + * fail. + */ +export const makeSlack = () => { + const messages = new Map() + const views: Array = [] + let next = 1794600000 + let viewCount = 0 + let refuse = false + + const fake = makeFake({ + "auth.test": authTest, + "chat.postMessage": (params) => { + const ts = `${next++}.000100` + messages.set(ts, { + type: "message", + ts, + channel: params.channel, + ...(params.thread_ts === undefined ? {} : { thread_ts: params.thread_ts }), + user: botUserId, + bot_id: "B0CCQH77VA5", + app_id: appId, + team, + text: params.text, + blocks: JSON.parse(params.blocks) + }) + return { ok: true, channel: params.channel, ts } + }, + "chat.update": (params) => { + const message = messages.get(params.ts) + if (message === undefined) return { ok: false, error: "message_not_found" } + message.text = params.text + message.blocks = JSON.parse(params.blocks) + return { ok: true, channel: params.channel, ts: params.ts } + }, + "chat.delete": (params) => messages.delete(params.ts) ? { ok: true } : { ok: false, error: "message_not_found" }, + "conversations.replies": (params) => { + const message = messages.get(params.ts) + return message === undefined ? { ok: false, error: "thread_not_found" } : { ok: true, messages: [message] } + }, + "views.open": (params) => { + const view = { ...JSON.parse(params.view), id: "V" + ++viewCount } + views.push(view) + return { ok: true, view } + }, + "conversations.history": (params) => ({ + ok: true, + messages: [...messages.values()].filter((message) => message.channel === params.channel).reverse() + }), + "files.getUploadURLExternal": { ok: true, upload_url: "https://files.slack.test/upload/F1", file_id: "F1" }, + "files.completeUploadExternal": () => + refuse ? { ok: false, error: "invalid_arguments" } : { ok: true, files: [{ id: "F1" }] }, + "response_url": { status: 200, body: "ok" } + }) + + /** The view the last click opened, until a submit closes it. */ + let opened: any + + /** The service the hook delivers through, set by `bind`. */ + let service: SlackChat.SlackChat["Service"] | undefined + + const deliver = (payload: Record) => + service!.interaction({ ...payload, token: "verification", api_app_id: appId }) + + const user = { id: tim, username: "tim", name: "tim", team_id: team } + + const interactions: Interactions = { + command: Effect.fnUntraced(function*(thread, id, options) { + const parts = SlackChat.parts(thread) + if (parts._tag === "None") return + const before = views.length + const { token: _, ...fields } = commandFields("/" + id, options?.text ?? "", { channel_id: parts.value.channel }) + yield* deliver(fields) + opened = views.length > before ? views[views.length - 1] : undefined + }), + click: Effect.fnUntraced(function*(messageId, id, options) { + const message = messages.get(messageId) + const block = message?.blocks.find((block: any) => block.type === "actions") + const element = block?.elements.find((element: any) => element.action_id === id) + if (element === undefined) return yield* Effect.fail(new NoSuchElement({ messageId, id })) + const before = views.length + const value = element.type === "static_select" ? options?.option ?? element.options[0].value : undefined + yield* deliver({ + type: "block_actions", + user, + team: { id: team, domain: "effectfulworkspace" }, + container: { type: "message", message_ts: messageId, channel_id: message.channel, is_ephemeral: false }, + trigger_id: "trigger-" + messageId, + channel: { id: message.channel, name: "scratch" }, + message, + state: { values: {} }, + response_url: "https://hooks.slack.test/actions", + actions: [{ + ...element, + block_id: block.block_id, + ...(value === undefined ? {} : { + selected_option: element.options.find((option: any) => option.value === value) + }), + action_ts: "1794600000.000001" + }] + }) + opened = views.length > before ? views[views.length - 1] : undefined + }), + submit: Effect.fnUntraced(function*(values) { + if (opened === undefined) return + const state: Record = {} + for (const block of opened.blocks) { + if (block.type !== "input") continue + const input = block.element + const value = values[block.block_id] + state[block.block_id] = { + [input.action_id]: input.type === "plain_text_input" + ? { type: input.type, value: value ?? null } + : { + type: input.type, + selected_option: value === undefined + ? null + : input.options.find((option: any) => option.value === value) ?? null + } + } + } + const answer: any = yield* deliver({ + type: "view_submission", + team: { id: team, domain: "effectfulworkspace" }, + user, + trigger_id: "trigger-submit", + view: { ...opened, team_id: team, state: { values: state }, hash: "1794600000.abc", app_id: appId }, + response_urls: [] + }) + // An `errors` answer, or none in time, keeps the view open. + if (answer?.response_action !== "errors" && answer?._tag !== "Late") opened = undefined + }) + } + + return { + fake, + bind: Effect.tap(SlackChat.SlackChat, (slack) => + Effect.sync(() => { + service = slack + })), + interactions, + refuseFiles: () => + Effect.sync(() => { + refuse = true + }) + } +} diff --git a/packages/chat/slack/test/text.test.ts b/packages/chat/slack/test/text.test.ts new file mode 100644 index 00000000000..2d47ffd8fea --- /dev/null +++ b/packages/chat/slack/test/text.test.ts @@ -0,0 +1,190 @@ +import * as Escape from "@effect/chat-slack/internal/escape" +import * as Text from "@effect/chat-slack/internal/text" +import * as SlackChat from "@effect/chat-slack/SlackChat" +import { assert, describe, it } from "@effect/vitest" +import * as Effect from "effect/Effect" +import { adapter, channel, makeFake, slackLayer, team } from "./utils.ts" + +const codePoints = (text: string) => [...text].length +const bytes = (text: string) => new TextEncoder().encode(text).length +const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" }) +const graphemes = (text: string) => [...segmenter.segment(text)] +const isBoundary = (text: string, index: number) => + index === text.length || graphemes(text).some((segment) => segment.index === index) + +/** + * The CI half of the Slack mention rule: the same assertions the live rule + * makes on stored messages, made here on the request bodies the adapter + * builds for `chat.postMessage` and `chat.update`. Stream appends are under + * "stream bodies". + */ +describe("request bodies", () => { + const thread = SlackChat.thread({ team, channel }) + const allowed = { users: ["U1"], broadcast: true } + /** The tokens `notify` lets through, of those the alphabet can spell. */ + const allowedTokens = (notify: Escape.Notify): ReadonlyArray => [ + ...(notify.users?.includes("U1") ? ["<@U1>"] : []), + ...(notify.broadcast === true ? [""] : []) + ] + + // Pieces that spell tokens, entity forms, code and multi-unit characters. + const alphabet = ["<", "@U1", "!here", ">", "&", "lt;", "`", "x", "👍🏽", "\n"] + const words = (length: number): Array => + length === 0 ? [""] : words(length - 1).flatMap((word) => alphabet.map((piece) => word + piece)) + const short = [1, 2, 3].flatMap(words) + const withToken = short.filter((word) => word.includes("<")) + + /** Repeats `word` until the text is past both budgets while the block stays within 12,000. */ + const long = (word: string, notify: Escape.Notify): string => { + const sent = (markdown: string) => codePoints(Escape.escape(markdown, notify, { flush: true }).sent) + let times = Math.floor(11_900 / sent(word)) + while (sent(word.repeat(times)) > 12_000) times = Math.floor(times / 2) + return word.repeat(times) + } + + const check = (op: "post" | "edit", markdown: string, notify: Escape.Notify, params: Record) => { + const label = `${op} ${JSON.stringify(markdown.slice(0, 40))} ${JSON.stringify(notify)}` + const block = JSON.parse(params.blocks)[0].text as string + const text = params.text + assert.isUndefined(params.markdown_text, label) + assert.isAbove(text.length, 0, label) + if (op === "post") assert.isAtMost(codePoints(text), Text.postTextLength, label) + else assert.isAtMost(bytes(text), Text.updateTextBytes, label) + // No token survives in text but an allowed one, and every & starts an entity. + let rest = text + for (const token of allowedTokens(notify)) rest = rest.replaceAll(token, "") + assert.notMatch(rest, /[<>]|&(?!lt;|gt;|amp;)/, label) + // An allowed token the block sends intact is in text too, cut or not. + for (const token of allowedTokens(notify)) { + if (block.includes(token)) assert.include(text, token, label) + } + // A cut is a prefix of the whole text, on a grapheme boundary and outside an entity. + const whole = Text.build(markdown, notify, { unit: "codePoints", limit: Infinity }).text + if (text !== whole) { + const head = text.slice(0, text.lastIndexOf("…")) + assert.isTrue(whole.startsWith(head), label) + assert.notMatch(head, /&[a-z]*$/, label) + assert.isTrue(isBoundary(whole, head.length), label) + } + } + + it.effect("hold the mention, budget and cut rules over a generated alphabet", () => { + const fake = makeFake({ "chat.postMessage": { ok: true, channel, ts: "1.1" } }) + return Effect.gen(function*() { + const slack = yield* adapter + let count = 0 + let cuts = 0 + for (const notify of [{}, allowed] as ReadonlyArray) { + const inputs = [ + ...short, + ...[1, 2].flatMap(words).map((word) => long(word, notify)), + // 3,300 `&` are 16,500 code points of text, so a token in the word is past either cut. + ...(notify === allowed ? withToken.map((word) => "&".repeat(3_300) + word) : []) + ] + for (const markdown of inputs) { + for (const op of ["post", "edit"] as const) { + const before = fake.calls.length + // A new message id for each edit, so pacing never waits. + const write = op === "post" + ? slack.post(thread, { markdown }) + : slack.edit(thread, `1.${count + 2}`, { markdown }) + yield* write.pipe(SlackChat.withPostOptions({ notify })) + const call = fake.calls[fake.calls.length - 1] + assert.strictEqual(fake.calls.length, before + 1) + assert.strictEqual(call.method, op === "post" ? "chat.postMessage" : "chat.update") + check(op, markdown, notify, call.params) + if (call.params.text.includes("…")) cuts++ + count++ + } + } + } + assert.strictEqual(count, 2 * (2 * (short.length + 110) + withToken.length)) + // The long inputs reach both budgets. + assert.isAbove(cuts, 2 * withToken.length) + }).pipe(Effect.provide(slackLayer(fake))) + }, 60_000) +}) + +/** + * The CI half of the Slack mention rule for a stream: every input split at + * every position, across two appends with the held tail sent again as the + * layer does, and across the start and an append, where nothing is held. + * Slack joins a reference split across writes, so the check is on what the + * writes add up to, decoded. Forms without a semicolon decode to nothing, so + * they must give no token. + */ +describe("stream bodies", () => { + const thread = SlackChat.thread({ team, channel, threadTs: "1794509110.999799" }) + const token = /<(?:@[UW][A-Z0-9]+|!subteam\^[A-Z0-9]+|!(?:here|channel|everyone))(?:\|[^<>]*)?>/g + /** Slack's decoding on a stream, in code too, which is wider than Slack. */ + const decode = (text: string) => + text.replace(/&(?:lt|LT|#0*60|#[xX]0*3[cC]);/g, "<").replace(/&(?:commat|#0*64|#[xX]0*40);/g, "@").replace( + /&(?:excl|#0*33|#[xX]0*21);/g, + "!" + ) + const tokens = (text: string) => decode(text).match(token) ?? [] + const alphabet = ["<", "<", "&l", "t;", "<", "@U1", "@", "!", "here", "`", "\n", "x", ">", "👍🏽"] + const words = (length: number): Array => + length === 0 ? [""] : words(length - 1).flatMap((word) => alphabet.map((piece) => word + piece)) + /** Whole tokens, which the generated words are too short to spell. */ + const seeds = [ + "<@U1>", + "", + "<@U1>", + "<@U1>", + "<!here>", + "`<@U1>`", + "<@U1>", + "<@U1>", + "<@U1|hello world>", + "<@U1> <@U1>" + ] + + it.effect("hold the mention rule at every split, and leave no U+200B after the stream", () => { + let count = 0 + // A new message for each stream, so the edit pacing never waits. + const fake = makeFake({ "chat.startStream": () => ({ ok: true, channel, ts: `1.${count++}` }) }) + return Effect.gen(function*() { + const native = (yield* adapter).nativeStream! + let cleaned = 0 + for (const notify of [{}, { users: ["U1"] }] as ReadonlyArray) { + const allowed = (found: string) => notify.users !== undefined && /^<@U1[|>]/.test(found) + for (const word of [...seeds, ...[1, 2].flatMap(words)]) { + for (let at = 0; at <= word.length; at++) { + for (const across of ["appends", "start"] as const) { + const label = `${JSON.stringify(word)} at ${at} across ${across} ${JSON.stringify(notify)}` + const before = fake.calls.length + const first = word.slice(0, at) + yield* Effect.gen(function*() { + if (across === "start") { + const { id: ts } = yield* native.start(thread, { markdown: "s " + first, recipient: "U080YLYB277" }) + yield* native.append(thread, ts, word.slice(at), { flush: true }) + return yield* native.stop(thread, ts, { end: "done" }) + } + const { id: ts } = yield* native.start(thread, { markdown: "s ", recipient: "U080YLYB277" }) + const { held } = yield* native.append(thread, ts, first, undefined) + yield* native.append(thread, ts, first.slice(first.length - held) + word.slice(at), { flush: true }) + yield* native.stop(thread, ts, { end: "done" }) + }).pipe(SlackChat.withPostOptions({ notify })) + const calls = fake.calls.slice(before) + const streamed = calls.filter((call) => call.method !== "chat.update" && call.params.markdown_text) + .map((call) => call.params.markdown_text).join("") + if (across === "appends") { + // Held text keeps every allowed token whole, and nothing else. + assert.deepStrictEqual(tokens(streamed), tokens("s " + word).filter(allowed), label) + } else { + // The start holds nothing, so it may break an allowed token, but never makes one. + assert.deepStrictEqual(tokens(streamed).filter((found) => !allowed(found)), [], label) + } + const update = calls.find((call) => call.method === "chat.update") + const left = update === undefined ? streamed : update.params.blocks + update.params.text + assert.notInclude(left, "\u200B", label) + if (update !== undefined) cleaned++ + } + } + } + } + assert.isAbove(cleaned, 0) + }).pipe(Effect.provide(slackLayer(fake))) + }, 120_000) +}) diff --git a/packages/chat/slack/test/utils.ts b/packages/chat/slack/test/utils.ts new file mode 100644 index 00000000000..c70b5d1d8fb --- /dev/null +++ b/packages/chat/slack/test/utils.ts @@ -0,0 +1,161 @@ +import * as SlackChat from "@effect/chat-slack/SlackChat" +import * as Chat from "effect/chat/Chat" +import type { ChatAdapter } from "effect/chat/ChatAdapter" +import { Registry } from "effect/chat/ChatAdapter" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import type * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as fs from "node:fs" + +export const team = "T05479Z6JBW" +export const botUserId = "U0CCLMD4S1L" +export const appId = "A0CCHBHDK7X" +export const channel = "C0CH1LGJ4F3" + +export const checkOptions = { platform: "slack", parseMention: SlackChat.parseMention } + +interface RecordedCall { + readonly method: string + readonly label?: string + readonly request: Record + readonly response: any +} + +export const fixture = (name: string): { readonly calls: ReadonlyArray; readonly [key: string]: any } => + JSON.parse(fs.readFileSync(new URL(`./fixtures/recorded/${name}.json`, import.meta.url), "utf8")) + +/** + * An `event_callback` envelope built around a message as a Web API read + * returned it. Reconstructed, not delivered by Slack: the captured + * deliveries are in `fixtures/events`. + */ +export const messageEnvelope = ( + message: Record, + options?: { + readonly type?: "message" | "app_mention" | undefined + readonly channel?: string | undefined + readonly channelType?: string | undefined + readonly teamId?: string | undefined + } +) => ({ + token: "verification-token", + team_id: options?.teamId ?? team, + api_app_id: appId, + type: "event_callback", + event_id: "Ev01", + event_time: 1794509000, + authorizations: [{ team_id: options?.teamId ?? team, user_id: botUserId, is_bot: true }], + event: { + ...message, + type: options?.type ?? "message", + channel: options?.channel ?? channel, + ...(options?.type === "app_mention" ? {} : { channel_type: options?.channelType ?? "channel" }), + event_ts: message.ts + } +}) + +export interface FakeCall { + readonly method: string + /** The form fields of a Web API call, or the JSON body of a `response_url` post with its `url`. */ + readonly params: Record +} + +type Answer = + | Record + | { readonly status: number; readonly headers?: Record; readonly body?: unknown } + | typeof hang + +/** An answer that never comes. */ +export const hang = { hang: true } as const + +/** + * A scripted Slack. Each method answers from a queue of answers, the last + * one repeating, or `{ ok: true }` when it has none. + */ +export const makeFake = ( + script: Record | ((params: Record) => Answer)> +) => { + const calls: Array = [] + const queues = new Map>() + for (const [method, answer] of Object.entries(script)) { + if (Array.isArray(answer)) queues.set(method, [...answer]) + } + const respond = (request: HttpClientRequest.HttpClientRequest, url: URL): Response | typeof hang => { + // A command's `response_url` takes JSON, and is recorded as one method. + const responseUrl = url.pathname.startsWith("/commands/") + const method = responseUrl ? "response_url" : url.pathname.replace(/^\/api\//, "") + const body = request.body + const text = body._tag === "Uint8Array" ? body.text ?? new TextDecoder().decode(body.body) : "" + const params = responseUrl ? { url: url.href, ...JSON.parse(text) } : Object.fromEntries(new URLSearchParams(text)) + calls.push({ method, params }) + const entry = script[method] + let answer: Answer + if (typeof entry === "function") answer = entry(params) + else if (Array.isArray(entry)) { + const queue = queues.get(method)! + answer = queue.length > 1 ? queue.shift()! : queue[0] + } else answer = (entry as Answer | undefined) ?? { ok: true } + if (answer === hang) return hang + if ("status" in answer && typeof answer.status === "number") { + const { body, headers, status } = answer as Extract + // A string goes as it is: a `response_url` answers in plain text. + const text = body === undefined ? "" : typeof body === "string" ? body : JSON.stringify(body) + return new Response(text, { status, headers: headers ?? {} }) + } + return new Response(JSON.stringify(answer), { status: 200, headers: { "content-type": "application/json" } }) + } + const client = HttpClient.make((request, url) => + Effect.suspend(() => { + const response = respond(request, url) + return response instanceof Response + ? Effect.succeed(HttpClientResponse.fromWeb(request, response)) + : Effect.never + }) + ) + return { + calls, + called: (method: string) => calls.filter((call) => call.method === method), + layer: Layer.succeed(HttpClient.HttpClient, client) + } +} + +export const authTest = { ok: true, user_id: botUserId, team_id: team } + +/** A command's `response_url`, on Slack's host as the adapter requires. */ +export const responseUrl = `https://hooks.slack.com/commands/${team}/1794600000/abcdef` + +/** + * A slash command's form fields: the form recorded in + * `fixtures/recorded/commands.json`, with the command, its text, the + * channel, and a `response_url` and `trigger_id` the tests can use. + */ +export const commandFields = ( + command: string, + text: string, + overrides?: Record +): Record => ({ + ...fixture("commands").commands.private, + token: "verification", + channel_id: channel, + channel_name: "scratch", + command, + text, + response_url: responseUrl, + trigger_id: "trigger-command", + ...overrides +}) + +/** `Chat`, `Registry` and the Slack adapter on a fake. */ +export const slackLayer = (fake: ReturnType) => + SlackChat.layer({ botToken: Redacted.make("xoxb-test"), teamId: team }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(fake.layer) + ) + +export const adapter: Effect.Effect = Effect.gen(function*() { + const registry = yield* Registry + return yield* registry.get("slack") +}) diff --git a/packages/chat/slack/test/webhook.test.ts b/packages/chat/slack/test/webhook.test.ts new file mode 100644 index 00000000000..d5a36574036 --- /dev/null +++ b/packages/chat/slack/test/webhook.test.ts @@ -0,0 +1,225 @@ +import { handleRequest, makeVerifier, type Options } from "@effect/chat-slack/internal/webhook" +import * as SlackChat from "@effect/chat-slack/SlackChat" +import * as NodeCrypto from "@effect/platform-node/NodeCrypto" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import { ChatError, PlatformUnavailable } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler" +import * as CryptoError from "effect/crypto/CryptoError" +import * as Mac from "effect/crypto/Mac" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import * as HttpRouter from "effect/http/HttpRouter" +import * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as HttpServerResponse from "effect/http/HttpServerResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import { TestClock } from "effect/testing" +import { appId, authTest, botUserId, makeFake, messageEnvelope, team } from "./utils.ts" + +const secret = "8f742231b10e8888abcd99yyyzzz85a5" + +const sign = async (timestamp: string, body: string, key = secret) => { + const encoder = new TextEncoder() + const cryptoKey = await crypto.subtle.importKey( + "raw", + encoder.encode(key), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign"] + ) + const mac = new Uint8Array(await crypto.subtle.sign("HMAC", cryptoKey, encoder.encode(`v0:${timestamp}:${body}`))) + return "v0=" + Array.from(mac, (byte) => byte.toString(16).padStart(2, "0")).join("") +} + +const unavailable = new ChatError({ + platform: "slack", + op: "auth.test", + reason: new PlatformUnavailable({ retry: "retry", detail: "http_503" }) +}) + +const now = () => String(Math.floor(Date.now() / 1000)) + +const request = async (body: string, options?: { readonly timestamp?: string; readonly key?: string }) => { + const timestamp = options?.timestamp ?? now() + return new Request("http://localhost/slack/events", { + method: "POST", + body, + headers: { + "content-type": "application/json", + "x-slack-request-timestamp": timestamp, + "x-slack-signature": await sign(timestamp, body, options?.key) + } + }) +} + +const message = messageEnvelope({ ts: "1794509003.000001", user: "U080YLYB277", text: "hi <@U0CCLMD4S1L>" }) + +const handle = Effect.fnUntraced(function*(options: Partial, web: Request) { + const verify = yield* makeVerifier(Redacted.make(secret)).pipe(Effect.provide(NodeCrypto.layerMac)) + const events: Array = [] + const response = yield* handleRequest(HttpServerRequest.fromWeb(web), { + verify, + appId, + teamId: team, + botUserId: Effect.succeed(botUserId), + handle: (event) => Effect.sync(() => void events.push(event)), + deadline: Duration.seconds(2), + maxBodyBytes: 1024 * 1024, + ...options + }) + return { status: response.status, response, events } +}) + +describe("webhook", () => { + it.live("rejects a bad, stale or missing signature", () => + Effect.gen(function*() { + const body = JSON.stringify(message) + const wrongKey = yield* handle({}, yield* Effect.promise(() => request(body, { key: "other" }))) + assert.strictEqual(wrongKey.status, 401) + const stale = String(Math.floor(Date.now() / 1000) - 6 * 60) + const old = yield* handle({}, yield* Effect.promise(() => request(body, { timestamp: stale }))) + assert.strictEqual(old.status, 401) + const unsigned = yield* handle({}, new Request("http://localhost/", { method: "POST", body })) + assert.strictEqual(unsigned.status, 401) + const malformed = yield* Effect.promise(() => request(body)) + malformed.headers.set("x-slack-signature", "v0=gg") + assert.strictEqual((yield* handle({}, malformed)).status, 401) + assert.strictEqual(wrongKey.events.length + old.events.length + unsigned.events.length, 0) + })) + + it.live("413 past the body limit", () => + Effect.gen(function*() { + const big = yield* handle({ maxBodyBytes: 100 }, yield* Effect.promise(() => request("x".repeat(101)))) + assert.strictEqual(big.status, 413) + })) + + it.live("answers url_verification with its challenge", () => + Effect.gen(function*() { + const body = JSON.stringify({ + token: "t", + challenge: "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P", + type: "url_verification" + }) + const result = yield* handle({}, yield* Effect.promise(() => request(body))) + assert.strictEqual(result.status, 200) + const web = HttpServerResponse.toWeb(result.response) + assert.strictEqual( + yield* Effect.promise(() => web.text()), + "3eZbrw1aBm2rZgRNFdxV2595E9CY3gmdALWMmHkvFXO7tYXAYM8P" + ) + })) + + it.live("drops another app's event with 200", () => + Effect.gen(function*() { + const result = yield* handle( + {}, + yield* Effect.promise(() => request(JSON.stringify({ ...message, api_app_id: "A0OTHER" }))) + ) + assert.strictEqual(result.status, 200) + assert.strictEqual(result.events.length, 0) + })) + + it.live("503 so Slack retries: no bot user, NotAccepted, past the deadline", () => + Effect.gen(function*() { + const web = Effect.promise(() => request(JSON.stringify(message))) + const noBot = yield* handle({ botUserId: Effect.fail(unavailable) }, yield* web) + const refused = yield* handle({ handle: () => Effect.fail(new NotAccepted()) }, yield* web) + const slow = yield* handle({ handle: () => Effect.never, deadline: Duration.millis(20) }, yield* web) + assert.deepStrictEqual([noBot, refused, slow].map(({ status }) => status), [503, 503, 503]) + assert.strictEqual(noBot.events.length, 0) + })) + + it.live("the deadline covers the bot user lookup", () => + Effect.gen(function*() { + const slowBot = yield* handle( + { botUserId: Effect.never, deadline: Duration.millis(20) }, + yield* Effect.promise(() => request(JSON.stringify(message))) + ) + assert.strictEqual(slowBot.status, 503) + assert.strictEqual(slowBot.events.length, 0) + })) + + it.effect("layerWebhook bounds a request whose body never finishes", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest }) + const events: Array = [] + const app = SlackChat.layerWebhook({ + path: "/slack/events", + signingSecret: Redacted.make(secret), + appId, + deadline: Duration.seconds(2) + }).pipe( + Layer.provide(SlackChat.layer({ botToken: Redacted.make("xoxb-test"), teamId: team })), + Layer.provide(Chat.layer), + Layer.provide(NodeCrypto.layerMac), + Layer.provide(Layer.succeed(ChatHandler, { handle: (event) => Effect.sync(() => void events.push(event)) })), + Layer.provide(fake.layer) + ) + const router = yield* HttpRouter.toHttpEffect(app) + let cancelled = false + const web = new Request("http://localhost/slack/events", { + method: "POST", + body: new ReadableStream({ + cancel: () => { + cancelled = true + } + }), + duplex: "half", + headers: { "x-slack-request-timestamp": now(), "x-slack-signature": "v0=" + "0".repeat(64) } + } as RequestInit) + const fiber = yield* router.pipe( + Effect.provideService(HttpServerRequest.HttpServerRequest, HttpServerRequest.fromWeb(web)), + Effect.forkChild + ) + yield* TestClock.adjust(2000) + const response = yield* Fiber.join(fiber) + assert.strictEqual(response.status, 503) + assert.strictEqual(events.length, 0) + assert.isTrue(cancelled) + })) + + it.live("layerWebhook uses the supplied Mac and fails closed on crypto errors", () => + Effect.gen(function*() { + const fake = makeFake({ "auth.test": authTest }) + const events: Array = [] + let fail = false + const mac = Layer.succeed(Mac.Mac, { + generateKey: () => Effect.die("unused"), + sign: () => Effect.die("unused"), + verify: (key) => { + assert.strictEqual(key.algorithm, Mac.hmacSha256) + return fail + ? Effect.fail( + new CryptoError.CryptoError({ + module: "Mac", + method: "verify", + reason: new CryptoError.InvalidKey({ description: "test failure" }) + }) + ) + : Effect.succeed(true) + } + }) + const app = SlackChat.layerWebhook({ path: "/slack/events", signingSecret: Redacted.make(secret), appId }).pipe( + Layer.provide(SlackChat.layer({ botToken: Redacted.make("xoxb-test"), teamId: team })), + Layer.provide(Chat.layer), + Layer.provide(mac), + Layer.provide(Layer.succeed(ChatHandler, { handle: (event) => Effect.sync(() => void events.push(event)) })), + Layer.provide(fake.layer) + ) + const { dispose, handler } = HttpRouter.toWebHandler(app, { disableLogger: true }) + const send = Effect.promise(async () => { + const web = await request(JSON.stringify(message)) + web.headers.set("x-slack-signature", "v0=abcd") + return handler(web) + }) + const response = yield* send + fail = true + assert.strictEqual((yield* send).status, 401) + yield* Effect.promise(() => dispose()) + assert.strictEqual(response.status, 200) + assert.strictEqual(events.length, 1) + })) +}) diff --git a/packages/chat/slack/tsconfig.json b/packages/chat/slack/tsconfig.json new file mode 100644 index 00000000000..e2a8ca19a0d --- /dev/null +++ b/packages/chat/slack/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "references": [ + { "path": "../../effect" } + ] +} diff --git a/packages/chat/teams/README.md b/packages/chat/teams/README.md new file mode 100644 index 00000000000..94d2fa0ae8b --- /dev/null +++ b/packages/chat/teams/README.md @@ -0,0 +1,116 @@ +# @effect/chat-teams + +A [Microsoft Teams](https://www.microsoft.com/microsoft-teams) adapter for `effect/chat`, over the Bot Framework Connector. It includes the messaging endpoint, a pure activity normaliser, and the address helpers. + +This package is private. Nothing in it has run against a real tenant, the default capabilities included. It stays private until every check under [Release checks](#release-checks) has tenant evidence. + +## Setup + +One layer serves one Entra tenant through a single-tenant Azure Bot registration: + +1. Register an app in Entra ID and create a client secret, or use a certificate or managed identity through a credentials provider. +2. Create an Azure Bot resource of type single-tenant with that app id, turn on the Microsoft Teams channel, and point its messaging endpoint at the route given to `TeamsChat.layerWebhook`. +3. Install the bot's app package in the tenant. + +```ts +import * as TeamsChat from "@effect/chat-teams/TeamsChat" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" + +const Teams = TeamsChat.layerWebhook({ path: "/api/messages" }).pipe( + Layer.provideMerge(TeamsChat.layer({ + appId: "", + tenantId: "", + credentials: Redacted.make("") + })) +) +``` + +`TeamsChat.layer` needs `HttpClient`, the chat `Registry`, `Mac` and `Digest`. `TeamsChat.layerWebhook` needs `HttpRouter`, a `ChatHandler`, `HttpClient` and `Signature`. The adapter never calls `crypto.subtle` itself. Importing Bot Framework's public keys goes through `Jwk.importPublic`, which needs a `WebCrypto` reference whatever backend provides `Signature`. + +The `HttpClient` must add no credentials of its own and follow no redirects. The adapter decides per request which host gets the bot's token, asks Fetch for `redirect: "manual"`, and fails a response that came from another URL than the one it sent to. With a client other than `FetchHttpClient`, or middleware that rewrites requests, keep both properties. + +The cloud's endpoints and host allowlists come from `TeamsChat.publicCloud` or a `Cloud` of your own. They are configuration and are never read from an activity. + +## Capabilities + +Every capability below is unverified against a tenant. They fall in three groups. + +**On by default:** `post`, `edit`, `delete`, `getThread` and `getUser`. `getUser` needs `in`, the thread whose roster to read. + +**Implemented, off by default.** Turn these on with the `unverified` option, at your own risk. Turning one on advertises a capability no tenant has confirmed: + +| Option | Turns on | Also needs | +| ------------- | ------------------------------------------- | ----------------- | +| `buttons` | `buttons` (buttons and link buttons) | `interactionKeys` | +| `modals` | `modals`, `modalErrors` (dialogs) | `buttons` | +| `commands` | `commands`, `ephemeral` (targeted messages) | | +| `startThread` | `startThread` | | +| `download` | `download` | | + +A dialog opens only from a button whose id is listed in `modalButtons`. Any other `OpenModal` is refused as `Unsupported`. + +**Not implemented in this version.** These are `Unsupported` whatever the options: + +- `fetchMessage`, `history` and `opener`. They need Microsoft Graph, which is deferred. +- `replyTo` (quoted replies). +- Files: `filesPerMessage` is 0, and the size reservation holds nothing for them. +- Native streams. Streams are posted and then edited. +- Selects. + +The element caps are the adapter's own, since Teams publishes none: 6 elements per message and 10 fields per modal. `actionDataLength` (1,000) and `modalMetadataLength` (4,000) count the whole signed token that carries the data, envelope included. With a GUID app and tenant that leaves about 500 ASCII characters for a button's id and value. A dialog token also carries the modal's definition, so its id, metadata, title, labels, options and texts share about 1,880 bytes of JSON. A modal over that is refused `TooLarge` before anything opens. Check 13 may raise the cap once the dialog card's real size limit is known. + +## Security + +- Inbound requests need a Bot Framework token: RS256, with a key endorsed for `msteams`, the cloud's issuer, the app id as audience, and a `serviceurl` claim equal to the activity's `serviceUrl`. The channel must be `msteams` and the tenant the configured one. Anything else is answered 403. The OpenID metadata and the key set are fetched with redirects off. Metadata over 64 KiB, or with a `jwks_uri` that is not a URL, is answered 503. +- The bot's token goes only to a Connector host from the cloud's allowlist, over `https:` on the default port. A download sends it only when the first URL is on its own class's allowlist. `redirectHosts` admits a host as a redirect target, never as the first hop, and a redirect hop gets no token. +- Button and dialog tokens are signed (HS256) and bound to the app, the tenant and the conversation. They are not encrypted: anyone who sees a card can read a button's id and value, and a modal's metadata. Do not put secrets in them. +- Button tokens have no replay protection. A click can be sent again for as long as the conversation exists. Dialog tokens also bind the user and the exact conversation id of the click, and expire after an hour. Signing does not stop a submit from arriving twice, so handlers still authorise and stay idempotent. +- A command's private answer goes as a targeted message to the user who sent the command. If that fails, the answer is not posted in public. + +## Release checks + +None of these has run. They need a Microsoft 365 developer tenant, an Azure Bot resource and a public tunnel to the endpoint. Each check gates only what it names. A failed check removes that capability, or blocks release when it gates the base. + +| Gate | Checks | If a check fails | +| ------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| Base: `post`, `edit`, `delete`, events, user ids | 1, 12, 4, 8, 9, 10, 16, 7, 18 | No release. Check 4 failing for an op brings in a `serviceUrl` resolver first. Check 10 failing changes the docs only. | +| `startThread` | 12 | Absent | +| `buttons` | 1, 9, 2, 3 | Absent, or absent from private answers only | +| `modals` | 17, the `task/fetch` and `task/submit` envelopes, the older-client fallback, `Ack` on a declared button leaving nothing open, 2, 13 | Absent | +| `modalErrors` | 13 | Absent: `Errors` then closes the dialog and sends a private message | +| `commands`, `ephemeral` | 3, 4 | Absent. Targeted activities are still classified first and never reach anything public. | +| Native streams | 5, 4 | Stays absent, so post-then-edit | +| `replyTo` | 14 | Stays absent | +| Files | 1, and the attachment header limits Teams accepts | Stay absent | +| `download` | 18 | Absent | +| Reaction events | 11 | Unknown types pass through as the raw string | +| Graph reads | 6, and history ordering | Deferred | +| Selects | 15 | Not in v1, so it gates nothing | + +The checks: + +1. The text form and the card form each arrive as one message with an id that `PUT` can edit, and `PUT` can switch between them. For files, an attachments-only activity with a card and images is one message. +2. The deadline for `adaptiveCard/action`, `task/fetch` and `task/submit` answers, and the failure answer that shows nothing unsafe. +3. The wire shape of a slash command in each scope: a leading slash or not, where the targeted flag appears (expected `recipient.isTargeted`), whether personal chats show the slash menu, no public fallback on failure, no Prompt Preview on a public answer, and a click on a private answer making no public activity. +4. Edit, delete, streaming and targeted sends all work through the global `serviceUrl`, and a targeted send works through the inbound `serviceUrl`. +5. Streaming over raw REST in group chats and channels, with a stored `replyTo` after the inbound request has returned, and a second stream answering the same message after a rotation. +6. Whether the Connector's activity id equals the Graph message id, and the Graph chat id of a personal bot chat. +7. Member lookup, targeted recipients and mention entities accept an Entra object id. +8. How `<`, `&` and `$` are escaped in `extendedmarkdown`, and whether bot messages unfurl links. +9. The real size edge for text and for cards. It sets `Edge` and the reservation `R`, and may only raise a term. +10. Whether a non-2xx answer is redelivered, and whether a retry keeps the activity id. +11. Reaction `type` strings beyond the six documented ones. +12. Which route makes a new top-level channel post and returns its id, and that the id is the reply chain's root. +13. Whether `ContinueTask` re-renders the dialog in place, where the errors show, and the size cap of a dialog card. +14. Quoted replies over raw REST in channels and group chats. +15. That `Input.ChoiceSet` has no submit-on-change in current Teams clients. +16. The issuer, audience and endorsements on inbound tokens for a single-tenant registration. +17. Whether an `adaptiveCard/action` answer can carry a task continuation that opens a dialog. If it can, `modalButtons` goes away. +18. The hosts actually seen for `serviceUrl`, image `contentUrl` and `downloadUrl` in each cloud and scope, and their redirect chains. These are written into the cloud presets. Nothing is added at runtime. + +## Links + +- [Website](https://effect.website): documentation, guides, and news. +- [Discord](https://discord.gg/effect-ts): ask questions, share what you're building, and talk to the core team. +- [Issues](https://github.com/Effect-TS/effect/issues): bug reports and feature requests. diff --git a/packages/chat/teams/package.json b/packages/chat/teams/package.json new file mode 100644 index 00000000000..06c78cbb9e8 --- /dev/null +++ b/packages/chat/teams/package.json @@ -0,0 +1,70 @@ +{ + "name": "@effect/chat-teams", + "version": "4.1.4", + "private": true, + "type": "module", + "license": "MIT", + "description": "The Microsoft Teams adapter for effect/chat", + "homepage": "https://effect.website", + "repository": { + "type": "git", + "url": "https://github.com/Effect-TS/effect.git", + "directory": "packages/chat/teams" + }, + "bugs": { + "url": "https://github.com/Effect-TS/effect/issues" + }, + "tags": [ + "typescript", + "chat", + "teams" + ], + "keywords": [ + "typescript", + "chat", + "teams" + ], + "sideEffects": [], + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts", + "./*": "./src/*.ts", + "./internal/*": null, + "./index": null, + "./*/index": null + }, + "files": [ + "src/**/*.ts", + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map", + "AGENTS.md", + "CLAUDE.md", + "ai-docs/**/*" + ], + "publishConfig": { + "access": "public", + "provenance": true, + "exports": { + "./package.json": "./package.json", + ".": "./dist/index.js", + "./*": "./dist/*.js", + "./internal/*": null, + "./index": null, + "./*/index": null + } + }, + "scripts": { + "codegen": "effect-utils codegen", + "build": "tsc -b tsconfig.json && pnpm babel", + "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps", + "check": "tsc -b tsconfig.json" + }, + "devDependencies": { + "effect": "workspace:^" + }, + "peerDependencies": { + "effect": "workspace:^" + } +} diff --git a/packages/chat/teams/src/TeamsChat.ts b/packages/chat/teams/src/TeamsChat.ts new file mode 100644 index 00000000000..a15fd6bf79e --- /dev/null +++ b/packages/chat/teams/src/TeamsChat.ts @@ -0,0 +1,466 @@ +/** + * The Microsoft Teams adapter for `effect/chat`, over the Bot Framework + * Connector: the `Registry` layer, the messaging endpoint, the pure + * normaliser and address helpers. + * + * **Details** + * + * - One layer serves one Entra tenant, with a single-tenant Azure Bot + * registration. The cloud's endpoints and host allowlists come as a unit + * from `publicCloud` or a `Cloud` of the consumer's own. They are + * privileged configuration and never read from an activity. + * - Nothing here has run against a real tenant, the default capabilities + * included. The package stays private until every release check in the + * README has tenant evidence. + * - Implemented but unverified, and off by default: `buttons`, `modals`, + * `modalErrors`, `commands`, `ephemeral`, `startThread` and + * `download`, each behind an `unverified` option. Turning one on + * advertises a capability no tenant has confirmed. + * - Not implemented in this version, whatever the options: reading + * messages (`fetchMessage`, `history`, `opener`, which need Microsoft + * Graph), `replyTo`, files, native streams and selects. They are + * `Unsupported`. + * + * @stability unstable + * @since 4.1.5 + */ +import { Registry } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import type { ChatEvent } from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ParseMention } from "effect/chat/ChatTest" +import type { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import type * as Digest from "effect/crypto/Digest" +import type * as Mac from "effect/crypto/Mac" +import type * as Signature from "effect/crypto/Signature" +import type * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpRouter from "effect/http/HttpRouter" +import type { SecretJwk } from "effect/jose/Jwk" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import type * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Adapter from "./internal/adapter.ts" +import * as Address from "./internal/address.ts" +import * as Auth from "./internal/auth.ts" +import * as Connector from "./internal/connector.ts" +import * as Envelope from "./internal/envelope.ts" +import * as Interactions from "./internal/interactions.ts" +import * as Normalize from "./internal/normalize.ts" +import * as Webhook from "./internal/webhook.ts" + +/** + * The endpoints and host allowlists of one Microsoft cloud. + * + * **Details** + * + * - `connectorUrl` is where thread ops go. Ops tied to an inbound activity + * (an invoke's answer, a command's answer, `followUp`) go to that + * activity's `serviceUrl`, which must be `https:` on the default port + * with a host exactly in `connectorHosts`, checked before a token is + * attached. There is no fallback from one to the other. + * - `openIdMetadataUrl` and `issuer` check inbound tokens. The metadata's + * `jwks_uri` must be on the metadata's own host. + * - `fileHostSuffixes` and `redirectHosts` bound downloads. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Cloud { + /** The Connector base for thread ops, such as `https://smba.trafficmanager.net/teams`. */ + readonly connectorUrl: string + /** The exact hosts an activity's `serviceUrl` may name. */ + readonly connectorHosts: ReadonlyArray + /** Bot Framework's OpenID metadata. Its `jwks_uri` must be on the same host. */ + readonly openIdMetadataUrl: string + /** The `iss` of an inbound token. */ + readonly issuer: string + /** The Entra authority, without the tenant: `https://login.microsoftonline.com`. */ + readonly loginUrl: string + /** The scope of a Connector token. */ + readonly connectorScope: string + /** The host suffixes of a personal chat's pre-authorised file URL, matched at a label boundary. */ + readonly fileHostSuffixes: ReadonlyArray + /** Extra exact hosts a download may be redirected to. */ + readonly redirectHosts: ReadonlyArray +} + +/** + * The public Azure cloud: Connector `smba.trafficmanager.net`, Bot + * Framework's OpenID metadata, Entra at `login.microsoftonline.com`, and + * `sharepoint.com` for files. No extra redirect hosts. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const publicCloud: Cloud = Connector.publicCloud + +/** + * How the adapter gets its Connector token: the bot's client secret, for + * client credentials against the tenant, or a provider that gets a token + * for a scope, for certificates or managed identities. + * + * **Details** + * + * A token is cached until five minutes before `expiresAt`. A token + * without `expiresAt` is not cached, and the provider is called for every + * request. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Credentials = + | Redacted.Redacted + | ((scope: string) => Effect.Effect<{ + readonly token: Redacted.Redacted + readonly expiresAt?: DateTime.DateTime | undefined + }, ChatError>) + +/** + * Capabilities whose live checks have not passed. Each is off by default + * and, when off, is absent from the descriptor and its op fails + * `Unsupported`. + * + * **Details** + * + * - `buttons`: buttons and link buttons on a card. Needs + * `interactionKeys`. Selects are never supported. + * - `modals`: dialogs opened by the buttons named in `modalButtons`, with + * field errors. Needs `buttons`. + * - `commands`: agent slash commands, answered in private or in public, + * and `followUp` for 24 hours. Also turns on `ephemeral`. + * - `startThread`: the reply chain under a channel post. + * - `download`: files on inbound messages. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Unverified { + readonly buttons?: boolean | undefined + readonly modals?: boolean | undefined + readonly commands?: boolean | undefined + readonly startThread?: boolean | undefined + readonly download?: boolean | undefined +} + +/** + * The Teams side of the adapter. + * + * **Details** + * + * - `call` sends one Connector request, with the bot's token, for what the + * adapter has no op for. `path` starts with `/v3/` and has its segments + * encoded. A `serviceUrl` routes it to an inbound activity's Connector, + * after the allowlist check. A write is never retried once sent, and a + * failure that may have landed is `uncertain`. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class TeamsChat extends Context.Service> | undefined + } + ) => Effect.Effect +}>()("@effect/chat-teams/TeamsChat") {} + +/** + * Registers the Teams adapter in the `Registry`, for one tenant. + * + * **Details** + * + * - `post` and `edit` without elements send the Markdown as `text` with + * `textFormat: "extendedmarkdown"`, with every `<` and `&` as an entity, + * so no HTML and no `` mention forms, and every `$` outside code as + * `\$`, so Teams reads no math. Mentions notify nobody. + * - With elements, the message is one Adaptive Card and no `text`: + * `TextBlock`, `CodeBlock` and `Table` elements from the Markdown, then + * `ActionSet` rows. An `edit` may switch between the two forms. + * - `limits.messageLength` is a budget, not Microsoft's number. It is the + * provisional size `Edge` (50,000 UTF-16 units of activity JSON, half of + * the 100 KB Microsoft documents, until a live check sets it) less a fixed + * reservation `R` for the card's structure, the auxiliary members and + * 4,000 units of mention entities: 44,288 today. `measure` prices each + * unit by what escaping and card structure can make of it. + * - A button carries a signed token (HS256 JWT) under the card data key + * `effect`, bound to the app, the tenant and the conversation. It is + * signed, not encrypted: anyone who sees the card can read the id and + * value. A token has no replay protection: a click can be sent again + * while the conversation exists. A dialog token also binds the user and + * expires after an hour. + * - The adapter's own caps, as Teams publishes none: 6 elements per + * message, 10 fields per modal, and two caps counted as the signed token + * that carries the data, envelope included. `actionDataLength` is 1,000 + * units of button token, about 500 ASCII characters of id plus value with + * a GUID app and tenant. `modalMetadataLength` is 4,000 units of dialog + * token, and the whole token counts: the modal's id, metadata, title, + * labels, options and texts together get about 1,880 bytes of JSON. A + * modal over it is refused `TooLarge` before anything opens. + * A message with every element at its cap still fits `messageLength`. + * - The `HttpClient` must add no credentials of its own and follow no + * redirects. The adapter decides per request which host gets the bot's + * token, asks Fetch for `redirect: "manual"` and fails a response from + * any other URL than the one it sent to. A client other than + * `FetchHttpClient`, or middleware that rewrites requests, must keep + * that. + * - `getUser` needs `in`, the thread whose roster to read: without it + * it fails `InvalidRequest` with `thread_required`. A user's id is the + * Entra object id when Teams sends one, and the Connector id (`29:…`, + * `28:…`) otherwise; `raw` keeps the Connector record. + * - The layer provides `TeamsChat` beside registering the adapter. It + * needs `Mac` and `Digest` for the interaction tokens; the adapter + * never calls `crypto.subtle` itself. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly appId: string + readonly tenantId: string + readonly credentials: Credentials + readonly cloud?: Cloud | undefined + /** + * The keys that sign button and dialog tokens. The first signs, and any + * verifies by its `kid`, so a rotation adds a key in front and drops the + * old one later. Each needs a distinct `kid` of 1 to 16 characters from + * `[A-Za-z0-9_-]` and at least 32 bytes. Without keys, there are no + * buttons or modals. + */ + readonly interactionKeys?: readonly [Redacted.Redacted, ...Array>] | undefined + /** The ids of the buttons that open a dialog. Only these may answer `OpenModal`. */ + readonly modalButtons?: ReadonlyArray | undefined + readonly unverified?: Unverified | undefined +}): Layer.Layer => + Layer.effect( + TeamsChat, + Effect.gen(function*() { + const http = yield* HttpClient.HttpClient + const registry = yield* Registry + const cloud = options.cloud ?? publicCloud + const tokens = Connector.makeTokens({ + http, + cloud, + appId: options.appId, + tenantId: options.tenantId, + credentials: options.credentials + }) + const connector = Connector.make({ http, cloud, token: tokens }) + const envelope = options.interactionKeys === undefined + ? undefined + : yield* Envelope.make({ appId: options.appId, tenantId: options.tenantId, keys: options.interactionKeys }) + const modalButtons = new Set(options.modalButtons ?? []) + const { adapter, answer } = Adapter.make({ + connector, + http, + cloud, + appId: options.appId, + tenantId: options.tenantId, + envelope, + unverified: options.unverified ?? {}, + token: tokens(cloud.connectorScope), + modalButtons + }) + yield* registry.register(adapter) + const service = TeamsChat.of({ + appId: options.appId, + tenantId: options.tenantId, + cloud, + call: (method, path, body, callOptions) => connector.call(method, path, body, callOptions) + }) + Adapter.internals.set(service, { capabilities: adapter.descriptor.capabilities, envelope, modalButtons, answer }) + return service + }) + ) + +/** + * `POST {path}`: the bot's messaging endpoint. + * + * **Details** + * + * - Checks the bearer token: RS256 with a key from Bot Framework's key set + * that is endorsed for `msteams`, the cloud's issuer, the app id as + * audience, five minutes of skew and an `exp`. Then the token's + * `serviceurl` must equal the activity's `serviceUrl`, `channelId` + * must be `msteams` and the tenant the layer's. Any failure is 403. A + * key set that can't be read is 503. + * - A targeted activity, one only the bot can see, is classified before + * anything else, whatever its type. A targeted message takes the command + * path: its first word names the command and the rest is its `text`. One + * that names no registered command is acked and nothing is sent. A + * targeted invoke goes to the interaction handler. Any other targeted + * activity is answered 200 and dropped. None reaches the `ChatHandler`, + * `onUnhandled`, or anything public. + * - Invokes go to `ChatInteraction.Handler` within `interactionDeadline` + * (4 seconds by default). Without a `Handler` in context, every invoke + * and every targeted message gets an empty 200. + * - Other activities go through `normalize` to the `ChatHandler`: 200 + * when it succeeds, 503 when it fails `NotAccepted` or a message can't + * be normalised. An activity with no event goes to `onUnhandled` and is + * answered 200. Past `deadline` (10 seconds by default) the handler is + * interrupted and the answer is 503. Each 503 is logged with its reason. + * - Answers 413 past `maxBodyBytes` (1 MiB by default). + * + * The metadata and the key set are fetched with redirects off, and the + * metadata is read up to 64 KiB. A metadata document or `jwks_uri` that + * can't be read is 503. The `HttpClient` has the same requirements as for + * `layer`. + * + * Token checks need the `Signature` service. Importing the key set's + * public keys goes through `Jwk.importPublic`, which needs a `WebCrypto` + * reference whatever crypto backend provides `Signature`. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerWebhook = (options: { + readonly path: `/${string}` + readonly deadline?: Duration.Input | undefined + readonly interactionDeadline?: Duration.Input | undefined + readonly maxBodyBytes?: number | undefined + readonly onUnhandled?: ((activity: Schema.Json) => Effect.Effect) | undefined +}): Layer.Layer< + never, + never, + HttpRouter.HttpRouter | TeamsChat | ChatHandler | HttpClient.HttpClient | Signature.Signature +> => + HttpRouter.use(Effect.fnUntraced(function*(router) { + const teams = yield* TeamsChat + const internals = Adapter.internals.get(teams) + if (internals === undefined) return yield* Effect.die(new Error("TeamsChat.layerWebhook needs TeamsChat.layer")) + const handler = yield* ChatHandler + const interactionHandler = yield* Effect.serviceOption(ChatInteraction.Handler) + const interactionRegistry = yield* Effect.serviceOption(ChatInteraction.Registry) + const verify = yield* Auth.makeVerifier({ cloud: teams.cloud, appId: teams.appId, tenantId: teams.tenantId }) + const interactionOptions: Interactions.Options = { + capabilities: internals.capabilities, + envelope: internals.envelope, + modalButtons: internals.modalButtons, + answer: internals.answer, + deadline: options.interactionDeadline === undefined + ? Interactions.defaultDeadline + : Duration.fromInputUnsafe(options.interactionDeadline) + } + const webhookOptions: Webhook.Options = { + verify, + context: { + appId: teams.appId, + connectorHosts: new Set(teams.cloud.connectorHosts), + fileHostSuffixes: teams.cloud.fileHostSuffixes + }, + handle: handler.handle, + onUnhandled: options.onUnhandled ?? (() => Effect.void), + invoke: (activity) => + Option.isNone(interactionHandler) + ? Effect.succeed({ status: 200 }) + : Effect.provideService( + Interactions.invoke(activity, interactionOptions), + ChatInteraction.Handler, + interactionHandler.value + ), + command: (activity) => + Option.isNone(interactionHandler) || Option.isNone(interactionRegistry) + ? Effect.void + : Effect.provideService( + Interactions.command(activity, { + ...interactionOptions, + isRegistered: (id) => + Effect.map(interactionRegistry.value.get(id), (entry) => entry?.definition.kind === "command") + }), + ChatInteraction.Handler, + interactionHandler.value + ), + deadline: options.deadline ?? Duration.seconds(10), + maxBodyBytes: options.maxBodyBytes ?? 1024 * 1024 + } + yield* router.add("POST", options.path, (request) => Webhook.handleRequest(request, webhookOptions)) + })) + +/** + * The address of a Teams conversation: the Bot Framework conversation id as + * it is. A channel thread is the channel's id followed by + * `;messageid={root}`. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const thread: (conversationId: string) => ChatThread = Address.thread + +/** + * The parts of a Teams address, or `None` for another platform's or a + * malformed one. + * + * @stability unstable + * @category destructors + * @since 4.1.5 + */ +export const parts: (thread: ChatThread) => Option.Option<{ + readonly conversationId: string + readonly base: string + readonly root?: string | undefined +}> = Address.parts + +/** + * The normaliser, for a consumer's own transport. It checks no token: that + * is the transport's job. + * + * **Details** + * + * - Pure: takes an activity, gives zero or more events. + * - A `message` gives a `MessageCreated`. Its `text` is Teams' `text` as + * delivered, with `…` for each mention, where `mentions` point. + * A channel post is addressed to the channel, a reply to the post's + * thread. + * - `messageReaction` gives a `ReactionAdded` or `ReactionRemoved` per + * reaction. The six types Teams documents are Unicode emoji; any other is + * `custom:{type}`. + * - A targeted activity, a card submit and anything else give none. + * - `raw` is the activity. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalize = ( + activity: unknown, + context: { readonly appId: string; readonly cloud?: Cloud | undefined } +): ReadonlyArray => { + const cloud = context.cloud ?? publicCloud + return Normalize.normalize(activity, { + appId: context.appId, + connectorHosts: new Set(cloud.connectorHosts), + fileHostSuffixes: cloud.fileHostSuffixes + }) +} + +/** + * Reads one Teams mention, `…`, for `ChatTest`. The tag holds a + * display name and no id, so this is always `None`; a message's + * `mentions` come from its entities. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const parseMention: ParseMention = () => Option.none() diff --git a/packages/chat/teams/src/index.ts b/packages/chat/teams/src/index.ts new file mode 100644 index 00000000000..ce93f54b6bf --- /dev/null +++ b/packages/chat/teams/src/index.ts @@ -0,0 +1,12 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as TeamsChat from "./TeamsChat.ts" diff --git a/packages/chat/teams/src/internal/adapter.ts b/packages/chat/teams/src/internal/adapter.ts new file mode 100644 index 00000000000..c6b643f7246 --- /dev/null +++ b/packages/chat/teams/src/internal/adapter.ts @@ -0,0 +1,364 @@ +import { + type ChatAdapter, + checkMessageLength, + type Descriptor, + type Posted, + type ThreadInfo, + type User +} from "effect/chat/ChatAdapter" +import { type Capability, type ChatError, type ChatErrorReason, interactionExpired } from "effect/chat/ChatError" +import type { Defer } from "effect/chat/ChatInteraction" +import type { Attachment } from "effect/chat/ChatMessage" +import type { ChatThread } from "effect/chat/ChatThread" +import type { Action } from "effect/chat/ChatUi" +import * as Clock from "effect/Clock" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type * as HttpClient from "effect/http/HttpClient" +import * as Option from "effect/Option" +import type * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import * as Address from "./address.ts" +import { type Cloud, type Connector, segment } from "./connector.ts" +import * as Download from "./download.ts" +import type * as Envelope from "./envelope.ts" +import * as Errors from "./errors.ts" +import * as Length from "./length.ts" +import { botId, isObject, object, string, userId } from "./normalize.ts" +import * as Render from "./render.ts" + +/** + * The capabilities a layer may turn on that no tenant run has confirmed + * yet. Each is off by default. + * + * @internal + */ +export interface Unverified { + readonly buttons?: boolean | undefined + readonly modals?: boolean | undefined + readonly commands?: boolean | undefined + readonly startThread?: boolean | undefined + readonly download?: boolean | undefined +} + +/** The adapter's own cap on elements in one message. @internal */ +export const actionsPerMessage = Render.actionsPerRow + +/** + * The adapter's own cap on an element's id plus value, counted as the + * signed token that carries them: 329 units of it are the envelope with a + * GUID app and tenant, which leaves about 500 ASCII characters. + * + * @internal + */ +export const actionDataLength = 1_000 + +/** + * The adapter's own cap on a modal's id plus metadata, counted as the whole + * signed dialog token that carries them: the claims, the metadata and the + * modal's definition (title, labels, options, texts). With a GUID app and + * tenant and every id at its longest, the claims take at most 1,483 units, + * which leaves about 1,880 bytes for the rest of the modal's JSON. + * + * @internal + */ +export const modalMetadataLength = 4_000 + +/** The adapter's own cap on a modal's inputs and texts. @internal */ +export const modalFields = 10 + +/** How long a targeted message can be answered. @internal */ +export const followUpAge = Duration.hours(24) + +/** @internal */ +export const capabilitiesOf = (unverified: Unverified, hasKeys: boolean): ReadonlySet => { + const set = new Set(["edit", "delete", "getThread", "getUser"]) + if (unverified.startThread === true) set.add("startThread") + if (unverified.download === true) set.add("download") + if (hasKeys && unverified.buttons === true) { + set.add("buttons") + if (unverified.modals === true) { + set.add("modals") + set.add("modalErrors") + } + } + if (unverified.commands === true) { + set.add("commands") + set.add("ephemeral") + } + return set +} + +/** @internal */ +export const makeDescriptor = (capabilities: ReadonlySet, tokenLength: Length.TokenLength): Descriptor => ({ + platform: Address.platform, + capabilities, + measure: Length.makeMeasure(tokenLength), + limits: { + messageLength: Length.messageLength, + filesPerMessage: 0, + uploadBytes: 0, + minWriteInterval: Duration.seconds(1), + ...(capabilities.has("buttons") ? { actionsPerMessage, actionDataLength } : {}), + ...(capabilities.has("modals") ? { modalMetadataLength, modalFields } : {}), + ...(capabilities.has("commands") ? { followUpAge } : {}) + } +}) + +/** A fallback token length for a layer without keys: the largest a token can be. */ +const noKeys: Length.TokenLength = () => 0 + +/** @internal */ +export interface Options { + readonly connector: Connector + readonly http: HttpClient.HttpClient + readonly cloud: Cloud + readonly appId: string + readonly tenantId: string + readonly envelope: Envelope.Envelope | undefined + readonly unverified: Unverified + readonly token: Effect.Effect, ChatError> + /** The ids of the buttons that open a dialog. */ + readonly modalButtons: ReadonlySet +} + +/** @internal */ +export interface Made { + readonly adapter: ChatAdapter + /** + * Sends a command's answer through the inbound `serviceUrl` in `raw`: + * private as a targeted message to the command's user, public as a new + * post. A failure is never retried in the other form. + */ + readonly answer: ( + op: string, + raw: Schema.Json, + content: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined }, + visibility: "ephemeral" | "public" + ) => Effect.Effect +} + +const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + +/** @internal */ +export const make = (options: Options): Made => { + const capabilities = capabilitiesOf(options.unverified, options.envelope !== undefined) + const descriptor = makeDescriptor(capabilities, options.envelope?.tokenLength ?? noKeys) + const { connector } = options + const self = botId(options.appId) + + const unsupported = (op: string, capability: Capability) => fail(op, Errors.unsupported(capability)) + + const address = (op: string, thread: ChatThread): Effect.Effect => { + const parts = Address.parts(thread) + return Option.isSome(parts) ? Effect.succeed(parts.value) : fail(op, Errors.invalidRequest("invalid_thread")) + } + + const checkId = (op: string, id: string): Effect.Effect => + id.length === 0 || id.length > Address.maxIdLength ? fail(op, Errors.invalidRequest("invalid_id")) : Effect.void + + /** + * The activity body for Markdown and elements: the text form without + * elements, one card with them. Checks capabilities and limits first. + */ + const body = ( + op: string, + conversationId: string, + content: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined } + ): Effect.Effect, ChatError> => + Effect.gen(function*() { + const actions = content.actions ?? [] + if (actions.some((action) => action._tag === "Select")) return yield* unsupported(op, "selects") + if (actions.length > 0 && (!capabilities.has("buttons") || options.envelope === undefined)) { + return yield* unsupported(op, "buttons") + } + yield* checkMessageLength(descriptor, op, content.markdown, actions) + if (actions.length === 0) return Render.textActivity(content.markdown) + if (actions.length > actionsPerMessage) return yield* fail(op, Errors.tooLarge(undefined, "too_many_actions")) + const tokens = new Map() + for (const action of actions) { + if (action._tag !== "Button") continue + if (options.envelope!.tokenLength(action) > actionDataLength) { + return yield* fail(op, Errors.tooLarge(undefined, "action_data_too_long")) + } + const token = yield* Effect.mapError( + options.envelope!.signButton(conversationId, { + id: action.id, + ...(action.value === undefined ? {} : { value: action.value }) + }), + (reason) => Errors.chatError(op, reason) + ) + tokens.set(action, token) + } + return Render.cardActivity( + content.markdown, + actions, + (button) => tokens.get(button)!, + (button) => options.modalButtons.has(button.id) + ) + }) + + const now = Effect.map(Clock.currentTimeMillis, (millis) => DateTime.makeUnsafe(millis)) + + const activityId = (op: string, answer: Schema.Json | undefined): Effect.Effect => { + const id = isObject(answer) ? string(answer, "id") : undefined + return id === undefined ? fail(op, Errors.invalidResponse(true, "missing_id")) : Effect.succeed(id) + } + + const activities = (conversationId: string) => `/v3/conversations/${segment(conversationId)}/activities` + + const adapter: ChatAdapter = { + descriptor, + post: Effect.fnUntraced(function*(thread, content) { + const op = "post" + const parts = yield* address(op, thread) + if (content.files !== undefined && content.files.length > 0) return yield* unsupported(op, "files") + if (content.replyTo !== undefined) return yield* unsupported(op, "replyTo") + const activity = yield* body(op, parts.conversationId, content) + const answer = yield* connector.call("POST", activities(parts.conversationId), activity, { write: true, op }) + const id = yield* activityId(op, answer) + return { id, thread, timestamp: yield* now } satisfies Posted + }), + edit: Effect.fnUntraced(function*(thread, messageId, content) { + const op = "edit" + const parts = yield* address(op, thread) + yield* checkId(op, messageId) + const activity = yield* body(op, parts.conversationId, content) + yield* connector.call( + "PUT", + `${activities(parts.conversationId)}/${segment(messageId)}`, + { ...activity, id: messageId }, + { write: true, op } + ) + }), + delete: Effect.fnUntraced(function*(thread, messageId) { + const op = "delete" + const parts = yield* address(op, thread) + yield* checkId(op, messageId) + yield* connector.call("DELETE", `${activities(parts.conversationId)}/${segment(messageId)}`, undefined, { + write: true, + op + }) + }), + fetchMessage: () => unsupported("fetchMessage", "fetchMessage"), + history: () => Stream.fail(Errors.chatError("history", Errors.unsupported("history"))), + download: (_thread: ChatThread, attachment: Attachment, downloadOptions: { readonly maxBytes: number }) => + capabilities.has("download") + ? Download.download(attachment.id, { + maxBytes: downloadOptions.maxBytes, + token: options.token, + connectorHosts: new Set(options.cloud.connectorHosts), + fileHostSuffixes: options.cloud.fileHostSuffixes, + redirectHosts: new Set(options.cloud.redirectHosts), + http: options.http + }) + : Stream.fail(Errors.chatError("download", Errors.unsupported("download"))), + startThread: Effect.fnUntraced(function*(thread, messageId) { + const op = "startThread" + if (!capabilities.has("startThread")) return yield* unsupported(op, "startThread") + const parts = yield* address(op, thread) + yield* checkId(op, messageId) + if (parts.root !== undefined) return parts.root === messageId ? thread : Address.threadOf(parts.base, messageId) + if (!Address.isChannel(parts.base)) return yield* fail(op, Errors.invalidRequest("not_a_channel")) + if (!/^\d+$/.test(messageId)) return yield* fail(op, Errors.invalidRequest("invalid_id")) + return Address.threadOf(parts.base, messageId) + }), + getThread: Effect.fnUntraced(function*(thread) { + const op = "getThread" + const parts = yield* address(op, thread) + const raw = yield* connector.call( + "GET", + `/v3/conversations/${segment(parts.base)}/pagedmembers`, + undefined, + { op, query: { pageSize: "1" } } + ) + const info: ThreadInfo = { + thread, + kind: Address.kind(parts.conversationId), + ...(parts.root === undefined ? {} : { parent: Address.thread(parts.base) }), + raw: raw ?? null + } + return info + }), + opener: () => unsupported("opener", "opener"), + getUser: Effect.fnUntraced(function*(id, userOptions) { + const op = "getUser" + if (userOptions?.in === undefined) return yield* fail(op, Errors.invalidRequest("thread_required")) + const parts = yield* address(op, userOptions.in) + yield* checkId(op, id) + const raw = yield* connector.call( + "GET", + `/v3/conversations/${segment(parts.base)}/members/${segment(id)}`, + undefined, + { op } + ) + const connectorId = isObject(raw) ? string(raw, "id") : undefined + const canonical = isObject(raw) ? userId(raw) : undefined + if (!isObject(raw) || connectorId === undefined || canonical === undefined) { + return yield* fail(op, Errors.invalidResponse(false, "invalid_member")) + } + const user: User = { + id: canonical, + name: string(raw, "name") ?? string(raw, "givenName") ?? canonical, + isBot: connectorId.startsWith("28:"), + isSelf: connectorId === self, + raw + } + return user + }), + followUp: Effect.fnUntraced(function*(deferred: Defer, content) { + const op = "followUp" + if (!capabilities.has("commands")) return yield* unsupported(op, "commands") + const raw = deferred.command.raw + const sent = isObject(raw) ? string(raw, "timestamp") : undefined + const at = sent === undefined ? undefined : Option.getOrUndefined(DateTime.make(sent)) + const current = yield* Clock.currentTimeMillis + if (at === undefined || current - DateTime.toEpochMillis(at) > Duration.toMillis(followUpAge)) { + return yield* fail(op, Errors.notFound(interactionExpired)) + } + yield* answer(op, raw, content, deferred.visibility) + }) + } + + const answer: Made["answer"] = Effect.fnUntraced(function*(op, raw, content, visibility) { + if (!isObject(raw)) return yield* fail(op, Errors.invalidRequest("invalid_command")) + const serviceUrl = string(raw, "serviceUrl") + const conversationId = string(object(raw, "conversation"), "id") + const messageId = string(raw, "id") + const from = object(raw, "from") + const recipientId = string(from, "id") + if ( + serviceUrl === undefined || conversationId === undefined || messageId === undefined || + recipientId === undefined + ) { + return yield* fail(op, Errors.invalidRequest("invalid_command")) + } + for (const id of [conversationId, messageId, recipientId]) yield* checkId(op, id) + const activity = yield* body(op, conversationId, content) + if (visibility === "ephemeral") { + yield* connector.call("POST", activities(conversationId), { + ...activity, + // Only the id: `Length.A` reserves it, and an unbounded name would break the size bound. + recipient: { id: recipientId }, + entities: [{ type: "targetedMessageInfo", messageId }] + }, { write: true, op, serviceUrl, query: { isTargetedActivity: "true" } }) + } else { + yield* connector.call("POST", activities(conversationId), activity, { write: true, op, serviceUrl }) + } + }) + + return { adapter, answer } +} + +/** What `layerWebhook` needs from the layer that registered the adapter. @internal */ +export interface Internals { + readonly capabilities: ReadonlySet + readonly envelope: Envelope.Envelope | undefined + readonly modalButtons: ReadonlySet + readonly answer: Made["answer"] +} + +/** Keyed by the `TeamsChat` service the layer built. @internal */ +export const internals = new WeakMap() diff --git a/packages/chat/teams/src/internal/address.ts b/packages/chat/teams/src/internal/address.ts new file mode 100644 index 00000000000..28316d77b73 --- /dev/null +++ b/packages/chat/teams/src/internal/address.ts @@ -0,0 +1,67 @@ +import type { ThreadKind } from "effect/chat/ChatEvent" +import * as ChatThread from "effect/chat/ChatThread" +import * as Option from "effect/Option" + +/** @internal */ +export const platform = "teams" + +const replyChain = ";messageid=" + +/** + * A Teams address is the Bot Framework conversation id as it is. A channel + * thread is the channel's id with `;messageid={root}` after it. + * + * @internal + */ +export const thread = (conversationId: string): ChatThread.ChatThread => ChatThread.make(platform, conversationId) + +/** @internal */ +export interface Parts { + /** The conversation id, as the Connector takes it. */ + readonly conversationId: string + /** The conversation without a reply chain suffix. */ + readonly base: string + /** The root message of a channel thread. */ + readonly root?: string | undefined +} + +/** @internal */ +export const parts = (address: ChatThread.ChatThread): Option.Option => { + if (address.platform !== platform || address.id.length === 0 || address.id.length > maxIdLength) { + return Option.none() + } + const at = address.id.indexOf(replyChain) + if (at === -1) return Option.some({ conversationId: address.id, base: address.id }) + const base = address.id.slice(0, at) + const root = address.id.slice(at + replyChain.length) + if (base.length === 0 || !/^\d+$/.test(root)) return Option.none() + return Option.some({ conversationId: address.id, base, root }) +} + +/** The conversation id without its reply chain suffix. @internal */ +export const baseConversation = (conversationId: string): string => { + const at = conversationId.indexOf(replyChain) + return at === -1 ? conversationId : conversationId.slice(0, at) +} + +/** @internal */ +export const threadOf = (base: string, root: string): ChatThread.ChatThread => thread(`${base}${replyChain}${root}`) + +/** A personal chat: a one-to-one conversation with the bot. @internal */ +export const isDirect = (conversationId: string): boolean => conversationId.startsWith("a:") + +/** A team channel, as opposed to a group or personal chat. @internal */ +export const isChannel = (conversationId: string): boolean => + /@thread\.(tacv2|skype)$/.test(baseConversation(conversationId)) + +/** @internal */ +export const kind = (conversationId: string): ThreadKind => + conversationId.includes(replyChain) ? "thread" : isDirect(conversationId) ? "direct" : "channel" + +/** + * The longest id the adapter puts in a request: a conversation, message, + * user or tenant id. A longer one fails `InvalidRequest` before the call. + * + * @internal + */ +export const maxIdLength = 256 diff --git a/packages/chat/teams/src/internal/auth.ts b/packages/chat/teams/src/internal/auth.ts new file mode 100644 index 00000000000..b5c8d22d0d0 --- /dev/null +++ b/packages/chat/teams/src/internal/auth.ts @@ -0,0 +1,196 @@ +import * as Clock from "effect/Clock" +import type * as Signature from "effect/crypto/Signature" +import * as Effect from "effect/Effect" +import * as Exit from "effect/Exit" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import type * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Jwks from "effect/jose/Jwks" +import * as Jwt from "effect/jose/Jwt" +import * as Schema from "effect/Schema" +import * as Scope from "effect/Scope" +import * as Semaphore from "effect/Semaphore" +import * as Stream from "effect/Stream" +import { type Cloud, noRedirects, send } from "./connector.ts" + +/** The longest `Authorization` header read. Anything longer is refused unread. */ +const maxAuthorizationLength = 16 * 1024 + +/** How long after a failed metadata or key set fetch the next is tried. */ +const retryAfter = 30_000 + +/** The longest OpenID metadata document read. */ +const maxMetadataBytes = 64 * 1024 + +const Claims = Schema.Struct({ + serviceurl: Schema.optionalKey(Schema.String), + serviceUrl: Schema.optionalKey(Schema.String) +}) + +/** + * Keeps a key only when its `endorsements` is an array of strings holding + * `msteams`. A key without them is dropped: fails closed. + * + * @internal + */ +export const endorsedForTeams = (jwk: { readonly [key: string]: unknown }): boolean => { + const endorsements = jwk.endorsements + return Array.isArray(endorsements) && endorsements.every((e) => typeof e === "string") && + endorsements.includes("msteams") +} + +const parseUrl = (input: string): URL | undefined => { + try { + return new URL(input) + } catch { + return undefined + } +} + +/** The body, or `undefined` once it passes `maxBytes` or can't be read. Never reads past the limit. */ +const readBounded = ( + response: HttpClientResponse.HttpClientResponse, + maxBytes: number +): Effect.Effect => { + const declared = Number(response.headers["content-length"]) + if (Number.isFinite(declared) && declared > maxBytes) return Effect.succeed(undefined) + const body = new Uint8Array(maxBytes) + return Stream.runFoldEffect(response.stream, () => 0, (length, chunk) => + chunk.length > maxBytes - length + ? Effect.fail("too_large" as const) + : Effect.sync(() => { + body.set(chunk, length) + return length + chunk.length + })).pipe( + Effect.map((length) => body.subarray(0, length)), + Effect.orElseSucceed(() => undefined) + ) +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** @internal */ +export type Verdict = "ok" | "forbidden" | "unavailable" + +/** What the token is checked against, from the activity's body. @internal */ +export interface Activity { + readonly serviceUrl: unknown + readonly channelId: unknown + readonly tenantId: string | undefined +} + +/** The tenant of an activity: `conversation.tenantId`, else `channelData.tenant.id`. @internal */ +export const tenantOf = (activity: Record): string | undefined => { + const conversation = activity.conversation + if (isObject(conversation) && typeof conversation.tenantId === "string") return conversation.tenantId + const channelData = activity.channelData + if (isObject(channelData) && isObject(channelData.tenant) && typeof channelData.tenant.id === "string") { + return channelData.tenant.id + } + return undefined +} + +/** + * Checks a Bot Framework bearer token: RS256 with a key endorsed for + * `msteams` from the cloud's key set, the cloud's issuer, the app id as + * audience, five minutes of skew and a required `exp`. Then the + * `serviceurl` claim must equal the activity's `serviceUrl`, its + * `channelId` must be `msteams` and its tenant the configured one. + * + * The key set is found lazily from the cloud's OpenID metadata, whose + * `jwks_uri` must be `https:` on the metadata's own host. Neither fetch + * follows a redirect: the key set's client has Fetch's redirects off for + * each request, refreshes included. The metadata is read to at most 64 KiB. + * Metadata that can't be fetched, read or parsed is `unavailable`. A key set that can't be read is `unavailable`, so + * the webhook answers 503 and Teams retries; every other failure is + * `forbidden`. + * + * @internal + */ +export const makeVerifier = Effect.fnUntraced(function*(options: { + readonly cloud: Cloud + readonly appId: string + readonly tenantId: string +}) { + const http = yield* HttpClient.HttpClient + const scope = yield* Effect.scope + const signature = yield* Effect.context() + const lock = Semaphore.makeUnsafe(1) + let keys: Jwks.Jwks["Service"] | undefined + let failedAt = Number.NEGATIVE_INFINITY + + const jwksUri = Effect.gen(function*() { + const metadataUrl = parseUrl(options.cloud.openIdMetadataUrl) + if (metadataUrl === undefined) return yield* Effect.fail("metadata_url" as const) + const response = yield* send(http, HttpClientRequest.get(metadataUrl.href), false, "openIdMetadata") + if (response.status !== 200) return yield* Effect.fail("metadata_status" as const) + const body = yield* readBounded(response, maxMetadataBytes) + if (body === undefined || body.length === 0) return yield* Effect.fail("metadata_size" as const) + let json: unknown + try { + json = JSON.parse(new TextDecoder().decode(body)) + } catch { + return yield* Effect.fail("metadata_json" as const) + } + const uri = isObject(json) ? json.jwks_uri : undefined + const parsed = typeof uri === "string" ? parseUrl(uri) : undefined + if (parsed === undefined) return yield* Effect.fail("metadata_jwks_uri" as const) + if (parsed.protocol !== "https:" || parsed.port !== "" || parsed.hostname !== metadataUrl.hostname) { + return yield* Effect.fail("metadata_jwks_host" as const) + } + return parsed.href + }).pipe(Effect.scoped) + + // The key set, found once. A failure is not retried for `retryAfter`, so + // unauthenticated requests can't drive a metadata fetch each. + const keySet: Effect.Effect = lock.withPermit(Effect.gen(function*() { + if (keys !== undefined) return keys + const now = yield* Clock.currentTimeMillis + if (now - failedAt < retryAfter) return undefined + const made = yield* jwksUri.pipe( + Effect.flatMap((url) => Jwks.makeRemote({ url, filter: endorsedForTeams })), + Effect.provideService(Scope.Scope, scope), + Effect.provideService(HttpClient.HttpClient, noRedirects(http)), + Effect.option + ) + if (made._tag === "None") { + failedAt = now + return undefined + } + keys = made.value + return keys + })) + + return (authorization: string | undefined, activity: Activity): Effect.Effect => + Effect.gen(function*() { + if (authorization === undefined || authorization.length > maxAuthorizationLength) return "forbidden" + const match = /^Bearer ([A-Za-z0-9_.-]+)$/.exec(authorization) + if (match === null) return "forbidden" + const resolved = yield* keySet + if (resolved === undefined) return "unavailable" + const exit = yield* Jwt.verifyWithKeySet(Claims, match[1], { + algorithms: ["RS256"], + issuer: options.cloud.issuer, + audience: options.appId, + clockTolerance: "5 minutes", + requiredClaims: ["exp"] + }).pipe( + Effect.provideService(Jwks.Jwks, resolved), + Effect.provideContext(signature), + Effect.exit + ) + if (Exit.isFailure(exit)) { + const unavailable = exit.cause.reasons.some((reason) => + reason._tag === "Fail" && reason.error._tag === "JoseError" && + reason.error.reason._tag === "KeySetUnavailable" + ) + return unavailable ? "unavailable" : "forbidden" + } + const claims = exit.value.claims + const claimed = claims.serviceurl ?? claims.serviceUrl + if (typeof activity.serviceUrl !== "string" || claimed !== activity.serviceUrl) return "forbidden" + if (activity.channelId !== "msteams") return "forbidden" + if (activity.tenantId !== options.tenantId) return "forbidden" + return "ok" + }) +}) diff --git a/packages/chat/teams/src/internal/connector.ts b/packages/chat/teams/src/internal/connector.ts new file mode 100644 index 00000000000..435676f414d --- /dev/null +++ b/packages/chat/teams/src/internal/connector.ts @@ -0,0 +1,292 @@ +import { retrySafe } from "effect/chat/ChatAdapter" +import type { ChatError, ChatErrorReason } from "effect/chat/ChatError" +import * as Clock from "effect/Clock" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import type * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Semaphore from "effect/Semaphore" +import * as Errors from "./errors.ts" + +/** + * The endpoints and hosts of one Microsoft cloud. All of it is privileged + * configuration: nothing here is ever read from an activity. + * + * @internal + */ +export interface Cloud { + /** The Connector base for thread ops, such as `https://smba.trafficmanager.net/teams`. */ + readonly connectorUrl: string + /** The exact hosts an activity's `serviceUrl` may name. */ + readonly connectorHosts: ReadonlyArray + /** Bot Framework's OpenID metadata. Its `jwks_uri` must be on the same host. */ + readonly openIdMetadataUrl: string + /** The `iss` of an inbound token. */ + readonly issuer: string + /** The Entra authority, without the tenant: `https://login.microsoftonline.com`. */ + readonly loginUrl: string + /** The scope of a Connector token. */ + readonly connectorScope: string + /** The host suffixes of a personal chat's pre-authorised file URL. */ + readonly fileHostSuffixes: ReadonlyArray + /** Extra exact hosts a download may be redirected to. */ + readonly redirectHosts: ReadonlyArray +} + +/** @internal */ +export const publicCloud: Cloud = { + connectorUrl: "https://smba.trafficmanager.net/teams", + connectorHosts: ["smba.trafficmanager.net"], + openIdMetadataUrl: "https://login.botframework.com/v1/.well-known/openidconfiguration", + issuer: "https://api.botframework.com", + loginUrl: "https://login.microsoftonline.com", + connectorScope: "https://api.botframework.com/.default", + fileHostSuffixes: ["sharepoint.com"], + redirectHosts: [] +} + +/** A token and when it stops working. @internal */ +export interface Token { + readonly token: Redacted.Redacted + readonly expiresAt?: DateTime.DateTime | undefined +} + +/** @internal */ +export type Credentials = + | Redacted.Redacted + | ((scope: string) => Effect.Effect) + +/** How long before `expiresAt` a cached token is dropped. */ +const refreshMargin = Duration.minutes(5) + +/** + * The trust step for a Connector base URL: `https:`, the default port, no + * userinfo and an exact host in the allowlist. Gives the URL without a + * trailing slash, or `undefined`. + * + * @internal + */ +export const trustedServiceUrl = (url: string, hosts: ReadonlyArray): string | undefined => { + let parsed: URL + try { + parsed = new URL(url) + } catch { + return undefined + } + if ( + parsed.protocol !== "https:" || parsed.port !== "" || parsed.username !== "" || parsed.password !== "" || + parsed.search !== "" || parsed.hash !== "" || !hosts.includes(parsed.hostname) + ) { + return undefined + } + return parsed.origin + parsed.pathname.replace(/\/+$/, "") +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** + * Gets a token by client credentials, or from the consumer's provider, and + * caches it until five minutes before it expires. A token with no expiry is + * not cached. + * + * @internal + */ +export const makeTokens = (options: { + readonly http: HttpClient.HttpClient + readonly cloud: Cloud + readonly appId: string + readonly tenantId: string + readonly credentials: Credentials +}) => { + const cache = new Map; readonly until: number }>() + const lock = Semaphore.makeUnsafe(1) + const fail = (reason: ChatErrorReason) => Effect.fail(Errors.chatError("token", reason)) + + const clientCredentials = (secret: Redacted.Redacted) => (scope: string): Effect.Effect => + Effect.gen(function*() { + const url = `${options.cloud.loginUrl}/${encodeURIComponent(options.tenantId)}/oauth2/v2.0/token` + const request = HttpClientRequest.post(url).pipe( + HttpClientRequest.bodyUrlParams({ + grant_type: "client_credentials", + client_id: options.appId, + client_secret: Redacted.value(secret), + scope + }) + ) + const response = yield* send(options.http, request, false, "token") + if (response.status !== 200) { + return yield* fail(Errors.reasonForStatus(response.status, { write: false })) + } + const json = yield* response.json.pipe( + Effect.catch((cause) => fail(Errors.invalidResponse(false, "invalid_json", cause))) + ) + if (!isObject(json) || typeof json.access_token !== "string" || json.access_token === "") { + return yield* fail(Errors.invalidResponse(false, "missing_access_token")) + } + const now = yield* Clock.currentTimeMillis + const expiresIn = typeof json.expires_in === "number" ? json.expires_in : Number(json.expires_in) + return { + token: Redacted.make(json.access_token), + ...(Number.isFinite(expiresIn) && expiresIn > 0 + ? { expiresAt: DateTime.makeUnsafe(now + expiresIn * 1000) } + : {}) + } + }) + + const provider = Redacted.isRedacted(options.credentials) + ? clientCredentials(options.credentials) + : options.credentials + + return (scope: string): Effect.Effect, ChatError> => + lock.withPermit(Effect.gen(function*() { + const now = yield* Clock.currentTimeMillis + const cached = cache.get(scope) + if (cached !== undefined && cached.until > now) return cached.token + cache.delete(scope) + const token = yield* retrySafe(provider(scope)) + if (token.expiresAt !== undefined) { + const until = DateTime.toEpochMillis(token.expiresAt) - Duration.toMillis(refreshMargin) + if (until > now) cache.set(scope, { token: token.token, until }) + } + return token.token + })) +} + +/** + * The client with Fetch's redirects off, for every request it sends, + * including those made by code the adapter hands it to, such as the key set. + * + * @internal + */ +export const noRedirects = (http: HttpClient.HttpClient): HttpClient.HttpClient => + HttpClient.transform( + http, + (effect) => Effect.provideService(effect, FetchHttpClient.RequestInit, { redirect: "manual" }) + ) + +/** + * The URL a request goes to, with its query parameters and without a hash. + * + * @internal + */ +export const effectiveUrl = (request: HttpClientRequest.HttpClientRequest): string | undefined => { + const url = Option.getOrUndefined(HttpClientRequest.toUrl(request)) + if (url === undefined) return undefined + url.hash = "" + return url.href +} + +/** + * Sends one request with redirects off. A response from another URL than + * the request's, query included, means something followed a redirect, and + * is refused. + * + * @internal + */ +export const send = ( + http: HttpClient.HttpClient, + request: HttpClientRequest.HttpClientRequest, + write: boolean, + op: string +): Effect.Effect => + noRedirects(http).execute(request).pipe( + Effect.catch((error) => Effect.fail(Errors.chatError(op, Errors.transport(write, error.reason._tag, error)))), + Effect.flatMap((response) => + response.url !== effectiveUrl(request) + ? Effect.fail(Errors.chatError(op, Errors.untrustedUrl())) + : Effect.succeed(response) + ) + ) + +/** @internal */ +export interface CallOptions { + readonly write?: boolean | undefined + readonly retry?: boolean | undefined + /** + * The activity's own `serviceUrl`, for an op tied to an inbound activity. + * It is checked against the allowlist before a token is attached. Absent, + * the cloud's global Connector URL is used. + */ + readonly serviceUrl?: string | undefined + readonly query?: Readonly> | undefined + /** The op for the error. Default: the method and path. */ + readonly op?: string | undefined +} + +/** @internal */ +export interface Connector { + readonly call: ( + method: "GET" | "POST" | "PUT" | "DELETE", + path: string, + body: unknown, + options?: CallOptions | undefined + ) => Effect.Effect +} + +/** + * The Connector client. `path` starts with `/v3/` and has its segments + * encoded by the caller. + * + * @internal + */ +export const make = (options: { + readonly http: HttpClient.HttpClient + readonly cloud: Cloud + readonly token: (scope: string) => Effect.Effect, ChatError> +}): Connector => { + const once = ( + method: "GET" | "POST" | "PUT" | "DELETE", + path: string, + body: unknown, + callOptions: CallOptions | undefined + ): Effect.Effect => { + const write = callOptions?.write === true + const op = callOptions?.op ?? `${method} ${path.split("/").slice(0, 3).join("/")}` + const fail = (reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + return Effect.gen(function*() { + const base = callOptions?.serviceUrl === undefined + ? options.cloud.connectorUrl + : trustedServiceUrl(callOptions.serviceUrl, options.cloud.connectorHosts) + if (base === undefined) return yield* fail(Errors.untrustedUrl()) + const token = yield* Effect.mapError(options.token(options.cloud.connectorScope), Errors.withOp(op)) + let request = HttpClientRequest.make(method)(base + path).pipe( + HttpClientRequest.bearerToken(Redacted.value(token)), + HttpClientRequest.setHeader("accept", "application/json") + ) + if (callOptions?.query !== undefined) request = HttpClientRequest.setUrlParams(request, callOptions.query) + if (body !== undefined) request = HttpClientRequest.bodyJsonUnsafe(request, body) + const response = yield* send(options.http, request, write, op) + const retryAfter = Errors.parseRetryAfter(response.headers["retry-after"]) + if (response.status < 200 || response.status >= 300) { + const json = yield* Effect.orElseSucceed(response.json, () => undefined) + const error = isObject(json) && isObject(json.error) ? json.error : undefined + const code = typeof error?.code === "string" ? error.code : undefined + return yield* fail(Errors.reasonForStatus(response.status, { write, retryAfter, code })) + } + const text = yield* response.text.pipe( + Effect.catch((cause) => fail(Errors.invalidResponse(write, "invalid_body", cause))) + ) + if (text.trim() === "") return undefined + try { + return JSON.parse(text) as Schema.Json + } catch (cause) { + return yield* fail(Errors.invalidResponse(write, "invalid_json", cause)) + } + }).pipe(Effect.scoped) + } + return { + call: (method, path, body, callOptions) => { + const call = once(method, path, body, callOptions) + return callOptions?.retry === false ? call : retrySafe(call) + } + } +} + +/** Encodes one path segment. @internal */ +export const segment = (value: string): string => encodeURIComponent(value) diff --git a/packages/chat/teams/src/internal/download.ts b/packages/chat/teams/src/internal/download.ts new file mode 100644 index 00000000000..bafa3187dc5 --- /dev/null +++ b/packages/chat/teams/src/internal/download.ts @@ -0,0 +1,292 @@ +import type { ChatError, ChatErrorReason } from "effect/chat/ChatError" +import type * as ChatMessage from "effect/chat/ChatMessage" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import type * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as Stream from "effect/Stream" +import * as Errors from "./errors.ts" + +const op = "download" + +/** The content type of a personal chat file the bot may fetch without credentials. @internal */ +export const fileDownloadInfo = "application/vnd.microsoft.teams.file.download.info" + +/** + * An attachment id is one of: + * + * - `connector:{url}`: a Connector attachment view, fetched with the bot token. + * - `preauth:{url}`: a pre-authenticated file URL, fetched with no credentials. + * The URL itself grants access, so it is never logged. + * - `url:{url}`: any other URL. It is kept so the message shows the file, + * but `download` refuses it. + * + * @internal + */ +export type Kind = "connector" | "preauth" | "url" + +const kinds: ReadonlyArray = ["connector", "preauth", "url"] + +/** + * The path of a Connector attachment view: + * `/{region}/v3/attachments/{id}/views/{view}`, where the region segment may be + * absent. Each segment is letters, digits, `_` or `-`, and the URL has no + * query. + * + * @internal + */ +export const connectorPath = /^(?:\/[A-Za-z0-9_-]+)?\/v3\/attachments\/[A-Za-z0-9_-]+\/views\/[A-Za-z0-9_-]+$/ + +/** Whether `host` is `suffix` or ends with `.{suffix}`. @internal */ +export const underSuffix = (host: string, suffixes: ReadonlyArray): boolean => + suffixes.some((raw) => { + const suffix = raw.toLowerCase().replace(/^\./, "") + return suffix.length > 0 && (host === suffix || host.endsWith(`.${suffix}`)) + }) + +const isIpLiteral = (host: string): boolean => host.startsWith("[") || /^\d+\.\d+\.\d+\.\d+$/.test(host) + +const parse = (input: string, base?: string): URL | undefined => { + try { + return new URL(input, base) + } catch { + return undefined + } +} + +/** + * Parses `input` and checks the parts every hop needs: `https:`, no userinfo, + * the default port and a host name rather than an IP literal. The fragment is + * dropped, since it is never sent. + * + * @internal + */ +export const safeUrl = (input: string, base?: string): URL | undefined => { + const url = parse(input, base) + if ( + url === undefined || url.protocol !== "https:" || url.username !== "" || url.password !== "" || + url.port !== "" || url.hostname.length === 0 || isIpLiteral(url.hostname) + ) { + return undefined + } + url.hash = "" + return url +} + +const isObject = (value: unknown): value is Record => + typeof value === "object" && value !== null && !Array.isArray(value) + +const string = (record: Record, key: string): string | undefined => { + const value = record[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +const size = (record: Record): number | undefined => { + const value = record.size + return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : undefined +} + +/** + * Turns a Bot Framework activity attachment into a chat attachment. + * + * - `connector:` when `contentUrl` has the origin of the activity's + * `serviceUrl`, its host is in `connectorHosts` and its path is + * {@link connectorPath}. + * - `preauth:` for a file download info attachment in a personal chat whose + * `content.downloadUrl` is under one of `fileHostSuffixes`. Its + * `contentType` is Teams' own, so it is left out. + * - `url:` for any other `https:` or `http:` `contentUrl`. + * + * An attachment with no usable URL, such as a card or the HTML copy of the + * message, is `undefined`. + * + * @internal + */ +export const classify = (attachment: unknown, context: { + readonly serviceUrl: string + readonly conversationType?: string | undefined + readonly connectorHosts: ReadonlySet + readonly fileHostSuffixes: ReadonlyArray +}): ChatMessage.Attachment | undefined => { + if (!isObject(attachment)) return undefined + const contentType = string(attachment, "contentType") + const name = string(attachment, "name") + const content = isObject(attachment.content) ? attachment.content : undefined + const make = (id: string, type: string | undefined, bytes: number | undefined): ChatMessage.Attachment => ({ + id, + ...(name === undefined ? {} : { name }), + ...(type === undefined ? {} : { contentType: type }), + ...(bytes === undefined ? {} : { size: bytes }) + }) + + if (contentType === fileDownloadInfo && context.conversationType === "personal" && content !== undefined) { + const raw = string(content, "downloadUrl") + const url = raw === undefined ? undefined : safeUrl(raw) + if (url !== undefined && underSuffix(url.hostname, context.fileHostSuffixes)) { + return make(`preauth:${url.href}`, undefined, size(content) ?? size(attachment)) + } + } + + const raw = string(attachment, "contentUrl") + if (raw === undefined) return undefined + const type = contentType === fileDownloadInfo ? undefined : contentType + const url = safeUrl(raw) + const service = parse(context.serviceUrl) + if ( + url !== undefined && service !== undefined && url.origin === service.origin && + context.connectorHosts.has(url.hostname) && url.search === "" && connectorPath.test(url.pathname) + ) { + return make(`connector:${url.href}`, type, size(attachment)) + } + const other = parse(raw) + if (other === undefined || (other.protocol !== "https:" && other.protocol !== "http:")) return undefined + return make(`url:${other.href}`, type, size(attachment)) +} + +/** + * Splits an attachment id into its kind and URL. + * + * @internal + */ +export const parseId = (id: string): { readonly kind: Kind; readonly url: string } | undefined => { + const at = id.indexOf(":") + if (at === -1) return undefined + const kind = kinds.find((kind) => kind === id.slice(0, at)) + return kind === undefined ? undefined : { kind, url: id.slice(at + 1) } +} + +/** @internal */ +export interface DownloadOptions { + readonly maxBytes: number + /** The bot token. It is read only for a `connector:` id, and sent on its first request only. */ + readonly token: Effect.Effect, ChatError> + /** Exact Connector hosts. */ + readonly connectorHosts: ReadonlySet + /** Host suffixes for pre-authenticated file URLs, matched at a label boundary. */ + readonly fileHostSuffixes: ReadonlyArray + /** Exact hosts a redirect may also go to. Empty by default. */ + readonly redirectHosts?: ReadonlySet | undefined + readonly http?: HttpClient.HttpClient | undefined +} + +/** The most redirects one download follows. @internal */ +export const maxRedirects = 3 + +const redirects: ReadonlySet = new Set([301, 302, 303, 307, 308]) + +const fail = (reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + +/** + * Streams the file behind an attachment id. + * + * Every hop must pass {@link safeUrl}. The first must have a host in the id's + * own allowlist (exact `connectorHosts` for `connector:`, `fileHostSuffixes` + * for `preauth:`), and a redirect a host there or in `redirectHosts`. A + * redirect host never receives the token. The first hop of a + * `connector:` id must also have the {@link connectorPath} shape. A URL that + * fails fails `Forbidden` with detail `untrusted_url`. + * + * Requests carry only the headers set here: the bearer token, on the first + * hop of a `connector:` id. Redirects are read by hand, at most + * {@link maxRedirects} of them, and only 301, 302, 303, 307 and 308, each sent + * again as a GET. Fetch runs with `redirect: "manual"`, and a response from + * another URL than the request's is refused, since a client followed a + * redirect without the checks. + * + * Errors and spans hold the host only: a `preauth:` URL is a credential. + * The client's own span is turned off for the same reason, as it records the + * whole URL. + * + * @internal + */ +export function download( + id: string, + options: DownloadOptions & { readonly http: HttpClient.HttpClient } +): Stream.Stream +export function download( + id: string, + options: DownloadOptions +): Stream.Stream +export function download( + id: string, + options: DownloadOptions +): Stream.Stream { + return Stream.unwrap(Effect.gen(function*() { + const parsed = parseId(id) + if (parsed === undefined) return yield* fail(Errors.invalidRequest("attachment_id")) + if (parsed.kind === "url") return yield* fail(Errors.unsupported("download", "url_attachment")) + const { kind } = parsed + const redirectHosts = options.redirectHosts ?? new Set() + // The id's own allowlist. `redirectHosts` never vouches for the first + // hop, since the first hop of a `connector:` id is the one with the token. + const own = (url: URL): boolean => + kind === "connector" + ? options.connectorHosts.has(url.hostname) + : underSuffix(url.hostname, options.fileHostSuffixes) + const allowed = (url: URL): boolean => own(url) || redirectHosts.has(url.hostname) + + const first = safeUrl(parsed.url) + if ( + first === undefined || !own(first) || + (kind === "connector" && (first.search !== "" || !connectorPath.test(first.pathname))) + ) { + return yield* fail(Errors.untrustedUrl()) + } + + const http = HttpClient.withScope(options.http ?? (yield* HttpClient.HttpClient)) + const tooLarge = Errors.chatError(op, Errors.tooLarge("files", "max_bytes")) + + const send = (url: URL, hop: number) => + Effect.gen(function*() { + let request = HttpClientRequest.get(url.href) + if (kind === "connector" && hop === 0) { + const token = yield* Effect.mapError(options.token, Errors.withOp(op)) + request = HttpClientRequest.bearerToken(request, Redacted.value(token)) + } + const sent = Option.getOrUndefined(HttpClientRequest.toUrl(request))?.href + const response = yield* http.execute(request).pipe( + Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }), + Effect.provideService(HttpClient.TracerDisabledWhen, () => true), + Effect.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag))) + ) + yield* Effect.annotateCurrentSpan("http.response.status_code", response.status) + if (response.url !== sent) return yield* fail(Errors.untrustedUrl()) + return response + }).pipe( + Effect.withSpan("TeamsChat.download.request", { + kind: "client", + attributes: { "server.address": url.hostname, "teams.download.hop": hop } + }) + ) + + let url = first + let response: HttpClientResponse.HttpClientResponse + for (let hop = 0;; hop++) { + response = yield* send(url, hop) + if (!redirects.has(response.status)) break + if (hop === maxRedirects) return yield* fail(Errors.invalidResponse(false, "too_many_redirects")) + const location = response.headers.location + if (location === undefined) return yield* fail(Errors.invalidResponse(false, "missing_location")) + const next = safeUrl(location, url.href) + if (next === undefined || !allowed(next)) return yield* fail(Errors.untrustedUrl()) + url = next + } + + if (response.status < 200 || response.status >= 300) { + return yield* fail(Errors.reasonForStatus(response.status, { write: false })) + } + const length = Number(response.headers["content-length"]) + if (Number.isFinite(length) && length > options.maxBytes) return yield* Effect.fail(tooLarge) + let received = 0 + return response.stream.pipe( + Stream.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag))), + Stream.mapEffect((chunk) => { + received += chunk.length + return received > options.maxBytes ? Effect.fail(tooLarge) : Effect.succeed(chunk) + }) + ) + })) +} diff --git a/packages/chat/teams/src/internal/envelope.ts b/packages/chat/teams/src/internal/envelope.ts new file mode 100644 index 00000000000..5f93fbe7810 --- /dev/null +++ b/packages/chat/teams/src/internal/envelope.ts @@ -0,0 +1,426 @@ +/** + * The envelope a button or dialog carries in its `effect` data key: a compact + * HS256 JWT bound to the bot, the tenant and the conversation. A dialog token + * also binds the user it opened for and holds the modal's definition, so a + * submit is checked against what was signed. + */ +import type { NonEmptyReadonlyArray } from "effect/Array" +import type { ChatErrorReason } from "effect/chat/ChatError" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Clock from "effect/Clock" +import * as Digest from "effect/crypto/Digest" +import * as Mac from "effect/crypto/Mac" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Base64Url from "effect/encoding/Base64Url" +import * as Jwk from "effect/jose/Jwk" +import * as Jwt from "effect/jose/Jwt" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as Schema from "effect/Schema" +import { baseConversation, maxIdLength } from "./address.ts" +import { invalidRequest, tooLarge } from "./errors.ts" + +/** The data key that holds the token. A modal field may not use it. @internal */ +export const reservedKey = "effect" + +/** + * Bounds on what a token holds, in UTF-16 code units. `definition` bounds the + * UTF-8 bytes of a modal's JSON without its metadata. + * + * @internal + */ +export const limits = { + id: maxIdLength, + kid: 16, + buttonValue: 2_000, + metadata: 2_800, + text: 4_000, + fields: 100, + options: 100, + definition: 16_000 +} as const + +/** The default max length of a text input. @internal */ +export const defaultMaxLength = limits.text + +/** How long a dialog token is good for when the caller gives no ttl. @internal */ +export const defaultDialogTtl = Duration.hours(1) + +const typ = { button: "teams-button", dialog: "teams-dialog" } as const + +// Schemas + +const bounded = (max: number) => Schema.String.check(Schema.isMaxLength(max)) +const Id = bounded(limits.id) +const Text = bounded(limits.text) +const Hash = Schema.String.check(Schema.isBetweenLength(43, 43)) +const Length = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: limits.text })) + +const SelectOption = Schema.Struct({ label: Text, value: Text }) +const Options = Schema.Array(SelectOption).check(Schema.isMaxLength(limits.options)) + +const Definition = Schema.TaggedStruct("Modal", { + id: Id, + title: Text, + submitLabel: Schema.optionalKey(Text), + elements: Schema.Array(Schema.Union([ + Schema.TaggedStruct("TextInput", { + id: Id, + label: Text, + multiline: Schema.optionalKey(Schema.Boolean), + placeholder: Schema.optionalKey(Text), + initial: Schema.optionalKey(Text), + optional: Schema.optionalKey(Schema.Boolean), + minLength: Schema.optionalKey(Length), + maxLength: Schema.optionalKey(Length) + }), + Schema.TaggedStruct("SelectInput", { + id: Id, + label: Text, + placeholder: Schema.optionalKey(Text), + options: Options, + initial: Schema.optionalKey(Text), + optional: Schema.optionalKey(Schema.Boolean) + }), + Schema.TaggedStruct("Text", { text: Text }) + ])).check(Schema.isMaxLength(limits.fields)) +}) + +const ButtonClaims = Schema.Struct({ + aud: Id, + tid: Id, + cnv: Hash, + id: Id, + val: Schema.optionalKey(bounded(limits.buttonValue)) +}) + +const DialogClaims = Schema.Struct({ + aud: Id, + tid: Id, + cnv: Hash, + cid: Id, + sub: Id, + exp: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER })), + mid: Schema.optionalKey(Id), + def: Definition, + md: Schema.optionalKey(bounded(limits.metadata)) +}) + +// Lengths + +const encoder = new TextEncoder() +const utf8Length = (text: string): number => encoder.encode(text).length +const base64Length = (bytes: number): number => Math.ceil(bytes * 4 / 3) +/** A string of `n` code units is at most 6 bytes each in JSON, plus quotes. */ +const jsonString = (n: number): number => 6 * n + 2 +/** An HS256 signature is 32 bytes. */ +const signatureLength = base64Length(32) +/** The conversation hash: base64url of a SHA-256 digest. */ +const hashLength = base64Length(32) + +const headerJson = (kid: string, type: string): string => JSON.stringify({ alg: "HS256", kid, typ: type }) + +/** + * The longest token `signButton` or `signDialog` can produce. A longer value + * is refused before it is parsed. + * + * @internal + */ +export const maxTokenLength: number = (() => { + const header = utf8Length(headerJson("", typ.dialog)) + limits.kid + const button = utf8Length(JSON.stringify({ aud: "", tid: "", cnv: "", id: "", val: "" })) - 2 * 5 + + 3 * jsonString(limits.id) + jsonString(hashLength) + jsonString(limits.buttonValue) + const dialog = + utf8Length(JSON.stringify({ aud: "", tid: "", cnv: "", cid: "", sub: "", exp: 0, mid: "", def: 0, md: "" })) - + 2 * 7 - 2 + 5 * jsonString(limits.id) + jsonString(hashLength) + String(Number.MAX_SAFE_INTEGER).length + + limits.definition + jsonString(limits.metadata) + return base64Length(header) + 1 + base64Length(Math.max(button, dialog)) + 1 + signatureLength +})() + +/** @internal */ +export interface ButtonContext { + readonly appId: string + readonly tenantId: string + /** The kid of the signing key. */ + readonly kid: string +} + +const buttonClaims = ( + context: Omit, + cnv: string, + button: { readonly id: string; readonly value?: string | undefined } +): typeof ButtonClaims.Type => ({ + aud: context.appId, + tid: context.tenantId, + cnv, + id: button.id, + ...(button.value === undefined ? {} : { val: button.value }) +}) + +/** + * The exact length of the token `signButton` produces for `button`, without + * signing it. The header and claims are deterministic JSON and the hash and + * signature have fixed lengths. + * + * @internal + */ +export const buttonTokenLength = ( + context: ButtonContext, + button: { readonly id: string; readonly value?: string | undefined } +): number => + base64Length(utf8Length(headerJson(context.kid, typ.button))) + 1 + + base64Length(utf8Length(JSON.stringify(buttonClaims(context, "x".repeat(hashLength), button)))) + 1 + + signatureLength + +// Checks + +/** Whether a field of `modal` uses the reserved `effect` id. @internal */ +export const hasReservedField = (modal: ChatUi.Modal): boolean => + modal.elements.some((element) => element._tag !== "Text" && element.id === reservedKey) + +const definitionOf = (modal: ChatUi.Modal): typeof Definition.Type => { + const { metadata: _, ...definition } = modal + return definition +} + +const isDefinition = Schema.is(Definition) + +/** + * Why `modal` cannot go in a dialog token, if it can't: a field with the + * reserved id, or more than the limits allow. + * + * @internal + */ +export const checkModal = (modal: ChatUi.Modal): ChatErrorReason | undefined => { + if (hasReservedField(modal)) return invalidRequest("reserved_field_id") + if (modal.metadata !== undefined && modal.metadata.length > limits.metadata) { + return tooLarge(undefined, "modal_metadata_too_long") + } + const definition = definitionOf(modal) + if (!isDefinition(definition) || utf8Length(JSON.stringify(definition)) > limits.definition) { + return tooLarge(undefined, "modal_too_large") + } + return undefined +} + +/** Why `button` cannot go in a button token, if it can't. @internal */ +export const checkButton = ( + button: { readonly id: string; readonly value?: string | undefined } +): ChatErrorReason | undefined => + button.id.length > limits.id + ? tooLarge(undefined, "action_id_too_long") + : button.value !== undefined && button.value.length > limits.buttonValue + ? tooLarge(undefined, "action_data_too_long") + : undefined + +/** + * The values of a dialog submit, checked against the signed definition, or + * nothing when they don't match it. + * + * - Keys are the ids of the inputs. The reserved `effect` key is skipped. + * - Every value is a string. An optional input left empty has no key. + * - A text input is held to its `minLength` and `maxLength` (default 4,000), + * and a select to its options. + * + * @internal + */ +export const validateSubmission = ( + definition: ChatUi.Modal, + values: unknown +): Option.Option> => { + if (typeof values !== "object" || values === null || Array.isArray(values)) return Option.none() + const input = values as Record + const fields = new Map() + for (const element of definition.elements) { + if (element._tag !== "Text") fields.set(element.id, element) + } + for (const key of Object.keys(input)) { + if (key !== reservedKey && !fields.has(key)) return Option.none() + } + const out: Record = {} + for (const [id, field] of fields) { + const value = Object.hasOwn(input, id) ? input[id] : undefined + if (value !== undefined && typeof value !== "string") return Option.none() + if (value === undefined || value === "") { + if (field.optional === true) continue + return Option.none() + } + if (field._tag === "TextInput") { + if (value.length < (field.minLength ?? 0) || value.length > (field.maxLength ?? defaultMaxLength)) { + return Option.none() + } + } else if (!field.options.some((option) => option.value === value)) { + return Option.none() + } + Object.defineProperty(out, id, { value, enumerable: true, writable: true, configurable: true }) + } + return Option.some(out) +} + +// Envelope + +/** @internal */ +export interface Button { + readonly id: string + readonly value?: string | undefined +} + +/** @internal */ +export interface Dialog { + /** + * The conversation id the click came from, as Teams sent it. A submit + * must come from the same one. + */ + readonly conversationId: string + /** The message the click came from. Absent when a command opened it. */ + readonly messageId?: string | undefined + readonly modal: ChatUi.Modal +} + +/** @internal */ +export interface Envelope { + /** Fails with the reason `button` is over the limits. */ + readonly signButton: (conversationId: string, button: Button) => Effect.Effect + /** The button the token was signed for, if it was signed for this conversation. */ + readonly verifyButton: ( + token: unknown, + activity: { readonly conversationId: string } + ) => Effect.Effect> + /** Fails with the reason the modal or an id is refused. */ + readonly signDialog: ( + dialog: Dialog & { readonly userId: string; readonly ttl?: Duration.Input | undefined } + ) => Effect.Effect + /** + * The dialog the token was signed for, if it is unexpired, for this user, + * and from exactly this conversation id. + */ + readonly verifyDialog: ( + token: unknown, + activity: { readonly conversationId: string; readonly userId: string } + ) => Effect.Effect> + /** `buttonTokenLength` with this envelope's app, tenant and signing key. */ + readonly tokenLength: (button: Button) => number +} + +const validKid = /^[A-Za-z0-9_-]{1,16}$/ + +/** + * Builds the envelope. The first key signs, and a token is verified with the + * key its `kid` names. A key without a valid `kid`, a repeated `kid` or + * a key under 32 bytes is a defect. + * + * @internal + */ +export const make = Effect.fnUntraced(function*(options: { + readonly appId: string + readonly tenantId: string + readonly keys: NonEmptyReadonlyArray> +}): Effect.fn.Return { + const keys = new Map>() + for (const input of options.keys) { + const kid = Redacted.value(input).kid + if (kid === undefined || !validKid.test(kid)) { + return yield* Effect.die(new Error("Each Teams interaction key needs a kid of 1 to 16 [A-Za-z0-9_-]")) + } + if (keys.has(kid)) return yield* Effect.die(new Error(`The Teams interaction key kid "${kid}" is repeated`)) + keys.set(kid, yield* Effect.orDie(Jwk.importSecret(input, "HS256"))) + } + const signing = keys.get(Redacted.value(options.keys[0]).kid!)! + const context: ButtonContext = { appId: options.appId, tenantId: options.tenantId, kid: signing.kid! } + const mac = yield* Mac.Mac + const digest = yield* Digest.Digest + + const hash = (conversationId: string): Effect.Effect => + Effect.map( + digest.digest(Digest.sha256, encoder.encode(baseConversation(conversationId))), + (bytes) => Base64Url.encode(bytes) + ) + + const sign = (schema: S, claims: S["Type"], type: string) => + Jwt.sign(schema, claims, signing, { typ: type }).pipe( + Effect.map(Redacted.value), + Effect.provideService(Mac.Mac, mac), + Effect.orDie + ) + + const verify =
( + schema: Schema.ConstraintDecoder, + token: unknown, + type: string, + conversationId: string, + policy: Jwt.VerifyOptions + ): Effect.Effect> => { + if (typeof token !== "string" || token.length > maxTokenLength) return Effect.succeedNone + return Effect.gen(function*() { + const header = (yield* Jwt.decodeUnverified(token)).protectedHeader + const key = header.kid === undefined ? undefined : keys.get(header.kid) + if (key === undefined) return Option.none() + const verified = yield* Jwt.verify(schema, token, key, { ...policy, audience: options.appId }) + if (verified.protectedHeader.typ !== type) return Option.none() + const claims = verified.claims as A & { readonly tid: string; readonly cnv: string } + if (claims.tid !== options.tenantId || claims.cnv !== (yield* hash(conversationId))) return Option.none() + return Option.some(verified.claims) + }).pipe( + Effect.provideService(Mac.Mac, mac), + Effect.catch(() => Effect.succeedNone) + ) + } + + return { + signButton: (conversationId, button) => { + const refusal = checkButton(button) + if (refusal !== undefined) return Effect.fail(refusal) + return Effect.flatMap( + hash(conversationId), + (cnv) => sign(ButtonClaims, buttonClaims(context, cnv, button), typ.button) + ) + }, + verifyButton: (token, activity) => + Effect.map( + verify(ButtonClaims, token, typ.button, activity.conversationId, {}), + Option.map((claims) => ({ id: claims.id, ...(claims.val === undefined ? {} : { value: claims.val }) })) + ), + signDialog: (dialog) => { + const refusal = checkModal(dialog.modal) ?? + (dialog.userId.length > limits.id || (dialog.messageId?.length ?? 0) > limits.id || + dialog.conversationId.length > limits.id + ? invalidRequest("id_too_long") + : undefined) + if (refusal !== undefined) return Effect.fail(refusal) + return Effect.gen(function*() { + const now = yield* Clock.currentTimeMillis + const exp = Math.floor((now + Duration.toMillis(dialog.ttl ?? defaultDialogTtl)) / 1000) + const { metadata } = dialog.modal + return yield* sign(DialogClaims, { + aud: options.appId, + tid: options.tenantId, + cnv: yield* hash(dialog.conversationId), + cid: dialog.conversationId, + sub: dialog.userId, + exp, + ...(dialog.messageId === undefined ? {} : { mid: dialog.messageId }), + def: definitionOf(dialog.modal), + ...(metadata === undefined ? {} : { md: metadata }) + }, typ.dialog) + }) + }, + verifyDialog: (token, activity) => + Effect.map( + verify(DialogClaims, token, typ.dialog, activity.conversationId, { + subject: activity.userId, + requiredClaims: ["exp", "sub"] + }), + (verified) => + verified.pipe( + Option.filter((claims) => claims.cid === activity.conversationId), + Option.map((claims): Dialog => ({ + conversationId: claims.cid, + ...(claims.mid === undefined ? {} : { messageId: claims.mid }), + modal: { ...claims.def, ...(claims.md === undefined ? {} : { metadata: claims.md }) } + })) + ) + ), + tokenLength: (button) => buttonTokenLength(context, button) + } +}) diff --git a/packages/chat/teams/src/internal/errors.ts b/packages/chat/teams/src/internal/errors.ts new file mode 100644 index 00000000000..480779072af --- /dev/null +++ b/packages/chat/teams/src/internal/errors.ts @@ -0,0 +1,94 @@ +import { + type Capability, + ChatError, + type ChatErrorReason, + Forbidden, + InvalidRequest, + InvalidResponse, + NotFound, + PlatformUnavailable, + RateLimited, + TooLarge, + Transport, + Unauthenticated, + Unknown, + Unsupported +} from "effect/chat/ChatError" +import * as Duration from "effect/Duration" +import { platform } from "./address.ts" + +/** + * The reason for an HTTP status from the Connector. `detail` is + * `http_{status}`, or the Connector's error `code` when it sent one, which + * holds no message text or ids. + * + * @internal + */ +export const reasonForStatus = ( + status: number, + options: { + readonly write: boolean + readonly code?: string | undefined + readonly retryAfter?: Duration.Duration | undefined + readonly cause?: unknown + } +): ChatErrorReason => { + const detail = options.code !== undefined && /^[A-Za-z0-9_.-]{1,64}$/.test(options.code) + ? options.code + : `http_${status}` + const cause = options.cause === undefined ? {} : { cause: options.cause } + if (status === 429) { + return new RateLimited({ + detail, + ...cause, + ...(options.retryAfter === undefined ? {} : { retryAfter: options.retryAfter }) + }) + } + if (status >= 500) return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail, ...cause }) + if (status === 401) return new Unauthenticated({ detail, ...cause }) + if (status === 403) return new Forbidden({ detail, ...cause }) + if (status === 404) return new NotFound({ detail, ...cause }) + if (status === 413) return new TooLarge({ detail, ...cause }) + return new InvalidRequest({ detail, ...cause }) +} + +/** A request that failed before an answer came. @internal */ +export const transport = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => + new Transport({ retry: write ? "uncertain" : "retry", detail, ...(cause === undefined ? {} : { cause }) }) + +/** An answer the adapter cannot read. A write may have landed. @internal */ +export const invalidResponse = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => { + const fields = { detail, ...(cause === undefined ? {} : { cause }) } + return write ? new Unknown(fields) : new InvalidResponse(fields) +} + +/** @internal */ +export const parseRetryAfter = (value: string | undefined): Duration.Duration | undefined => { + if (value === undefined) return undefined + const seconds = Number(value) + return Number.isFinite(seconds) && seconds >= 0 ? Duration.seconds(seconds) : undefined +} + +/** @internal */ +export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason }) + +/** @internal */ +export const withOp = (op: string) => (error: ChatError): ChatError => + error.op === op ? error : new ChatError({ platform: error.platform, op, reason: error.reason }) + +/** @internal */ +export const invalidRequest = (detail: string): ChatErrorReason => new InvalidRequest({ detail }) + +/** @internal */ +export const notFound = (detail: string): ChatErrorReason => new NotFound({ detail }) + +/** @internal */ +export const unsupported = (capability: Capability, detail?: string): ChatErrorReason => + new Unsupported({ capability, ...(detail === undefined ? {} : { detail }) }) + +/** @internal */ +export const tooLarge = (subject: "text" | "files" | undefined, detail: string): ChatErrorReason => + new TooLarge({ ...(subject === undefined ? {} : { subject }), detail }) + +/** A URL outside the adapter's allowlists. Its host is the only part kept. @internal */ +export const untrustedUrl = (): ChatErrorReason => new Forbidden({ detail: "untrusted_url" }) diff --git a/packages/chat/teams/src/internal/interactions.ts b/packages/chat/teams/src/internal/interactions.ts new file mode 100644 index 00000000000..b3c8885f9f5 --- /dev/null +++ b/packages/chat/teams/src/internal/interactions.ts @@ -0,0 +1,312 @@ +import type { Capability, ChatError } from "effect/chat/ChatError" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ChatThread } from "effect/chat/ChatThread" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import type { Made } from "./adapter.ts" +import { modalFields, modalMetadataLength } from "./adapter.ts" +import * as Address from "./address.ts" +import * as Envelope from "./envelope.ts" +import * as Errors from "./errors.ts" +import { isObject, object, string, threadOf, userId } from "./normalize.ts" +import * as Render from "./render.ts" + +type JsonObject = { readonly [key: string]: Schema.Json } + +/** The default share of Teams' invoke timeout the handler gets. @internal */ +export const defaultDeadline = Duration.seconds(4) + +/** An answer to an invoke: the HTTP status and the JSON body, if any. @internal */ +export interface InvokeResponse { + readonly status: number + readonly body?: Schema.Json | undefined +} + +const isLate = Schema.is(ChatInteraction.Late) + +const ok: InvokeResponse = { status: 200 } + +/** What an invoke that fails a check gets. It names no reason. */ +const safeFailure = (name: string): InvokeResponse => + name === "adaptiveCard/action" + ? { + status: 200, + body: { + statusCode: 400, + type: "application/vnd.microsoft.error", + value: { code: "BadRequest", message: "This action could not be completed." } + } + } + : { status: 200, body: { task: { type: "message", value: "This action could not be completed." } } } + +const executeAck: InvokeResponse = { + status: 200, + body: { statusCode: 200, type: "application/vnd.microsoft.activity.message", value: "" } +} + +/** @internal */ +export interface Options { + readonly capabilities: ReadonlySet + readonly envelope: Envelope.Envelope | undefined + /** The ids of the buttons that open a dialog. */ + readonly modalButtons: ReadonlySet + readonly answer: Made["answer"] + readonly deadline: Duration.Duration +} + +const refused = (event: ChatInteraction.ButtonClicked | ChatInteraction.CommandInvoked, error: ChatError) => + Effect.flatMap(ChatInteraction.Handler, (handler) => handler.refused(event, error)) + +/** + * Where the message an invoke came from lives. A card posted as a channel + * post has the channel as its address, as `normalize` gives it. + */ +const cardThread = (conversationId: string | undefined, messageId: string | undefined): ChatThread | undefined => { + if (conversationId === undefined || conversationId.length > Address.maxIdLength) return undefined + const parts = Address.parts(Address.thread(conversationId)) + if (Option.isNone(parts)) return undefined + return parts.value.root !== undefined && parts.value.root === messageId + ? Address.thread(parts.value.base) + : Address.thread(conversationId) +} + +/** The adaptive card of a dialog, built only from the signed definition. */ +const dialogCard = ( + modal: ChatUi.Modal, + token: string, + submitted?: { + readonly values: Readonly> + readonly errors: Readonly> + } +) => { + // A dialog sent again with errors keeps what the user entered, not the + // definition's `initial`. Lookups are own properties only, so a field id + // such as `constructor` reads nothing inherited. + const own = (record: Readonly>, key: string): string | undefined => + Object.hasOwn(record, key) && typeof record[key] === "string" ? record[key] : undefined + const valueOf = (element: ChatUi.TextInput | ChatUi.SelectInput) => + submitted === undefined ? element.initial : own(submitted.values, element.id) + const body: Array = [] + for (const element of modal.elements) { + if (element._tag === "Text") { + body.push(Render.textBlock(Render.escapeText(Render.wellFormed(element.text)))) + continue + } + if (element._tag === "TextInput") { + body.push({ + type: "Input.Text", + id: element.id, + label: element.label, + isMultiline: element.multiline === true, + isRequired: element.optional !== true, + maxLength: element.maxLength ?? Envelope.defaultMaxLength, + ...(element.placeholder === undefined ? {} : { placeholder: element.placeholder }), + ...(valueOf(element) === undefined ? {} : { value: valueOf(element) }) + }) + } else { + body.push({ + type: "Input.ChoiceSet", + id: element.id, + label: element.label, + style: "compact", + isRequired: element.optional !== true, + choices: element.options.map((option) => ({ title: option.label, value: option.value })), + ...(element.placeholder === undefined ? {} : { placeholder: element.placeholder }), + ...(valueOf(element) === undefined ? {} : { value: valueOf(element) }) + }) + } + const error = submitted === undefined ? undefined : own(submitted.errors, element.id) + if (error !== undefined) { + body.push({ ...Render.textBlock(Render.escapeText(Render.wellFormed(error))), color: "attention" }) + } + } + return { + task: { + type: "continue", + value: { + title: modal.title, + card: { + contentType: Render.cardContentType, + content: { + ...Render.card(body), + actions: [{ + type: "Action.Submit", + title: modal.submitLabel ?? "Submit", + data: { [Envelope.reservedKey]: token } + }] + } + } + } + } + } +} + +const modalRefusal = (modal: ChatUi.Modal) => + modal.elements.length > modalFields + ? Errors.tooLarge(undefined, "too_many_modal_fields") + : Envelope.checkModal(modal) + +/** + * One verified invoke, or a card's `Action.Submit` fallback, which comes as + * a `message` with a `value`. + * + * - `adaptiveCard/action` and the fallback are clicks. A click can't open + * a dialog there: an `OpenModal` goes to `Handler.refused` as + * `Unsupported`. + * - `task/fetch` is a click on a button named in `modalButtons`, whose + * `OpenModal` opens the dialog, signed for the clicking user. + * - `task/submit` is a dialog submit, checked against its signed + * definition. + * - A token that does not verify, or values that do not match the + * definition, get the safe failure and dispatch nothing. + * + * @internal + */ +export const invoke = ( + activity: JsonObject, + options: Options +): Effect.Effect => + Effect.gen(function*() { + const name = activity.type === "message" ? "message" : string(activity, "name") ?? "" + const failure = safeFailure(name) + const envelope = options.envelope + if (envelope === undefined || !options.capabilities.has("buttons")) return failure + const conversationId = string(object(activity, "conversation"), "id") + const from = object(activity, "from") + const user = from === undefined ? undefined : userId(from) + if (conversationId === undefined || user === undefined) return failure + const value = object(activity, "value") + const data = name === "adaptiveCard/action" + ? object(object(value, "action"), "data") + : name === "message" + ? value + : object(value, "data") + const token = data?.[Envelope.reservedKey] + + if (name === "task/submit") { + if (!options.capabilities.has("modals")) return failure + const dialog = yield* envelope.verifyDialog(token, { conversationId, userId: user }) + if (Option.isNone(dialog)) return failure + const values = Envelope.validateSubmission(dialog.value.modal, data) + // The thread of the click that opened it, from the signed conversation + // id and message, never from the submit. + const origin = cardThread(dialog.value.conversationId, dialog.value.messageId) + if (Option.isNone(values) || origin === undefined) return failure + const modal = dialog.value.modal + const event = ChatInteraction.ModalSubmitted.make({ + id: modal.id, + ...(modal.metadata === undefined ? {} : { metadata: modal.metadata }), + values: values.value, + thread: origin, + ...(dialog.value.messageId === undefined ? {} : { messageId: dialog.value.messageId }), + userId: user, + raw: activity + }) + const reply = yield* ChatInteraction.handle(event, options.deadline) + if (isLate(reply)) return failure + if (reply._tag === "Errors") { + return { + status: 200, + body: dialogCard(modal, token as string, { values: values.value, errors: reply.errors }) as Schema.Json + } + } + return ok + } + + if (name !== "adaptiveCard/action" && name !== "message" && name !== "task/fetch") return failure + const button = yield* envelope.verifyButton(token, { conversationId }) + const messageId = string(activity, "replyToId") + const thread = cardThread(conversationId, messageId) + if (Option.isNone(button) || thread === undefined || messageId === undefined) return failure + const opensDialog = name === "task/fetch" + if (opensDialog !== options.modalButtons.has(button.value.id)) return failure + const event = ChatInteraction.ButtonClicked.make({ + id: button.value.id, + ...(button.value.value === undefined ? {} : { value: button.value.value }), + thread, + messageId, + userId: user, + raw: activity + }) + const reply = yield* ChatInteraction.handle(event, options.deadline) + const acked = name === "adaptiveCard/action" ? executeAck : name === "message" ? ok : failure + if (isLate(reply) || reply._tag === "Ack") return acked + if (!opensDialog || !options.capabilities.has("modals")) { + yield* refused(event, Errors.chatError("openModal", Errors.unsupported("modals"))) + return acked + } + const refusal = modalRefusal(reply.modal) + if (refusal !== undefined) { + yield* refused(event, Errors.chatError("openModal", refusal)) + return failure + } + const signed = yield* Effect.result( + envelope.signDialog({ conversationId, userId: user, messageId, modal: reply.modal }) + ) + // `modalMetadataLength` holds the whole signed token: the definition, + // the metadata and every claim. Signing is local, so the check is exact + // and nothing has opened yet. + const tooLong = signed._tag === "Success" && signed.success.length > modalMetadataLength + if (signed._tag === "Failure" || tooLong) { + const reason = signed._tag === "Failure" ? signed.failure : Errors.tooLarge(undefined, "modal_too_large") + yield* refused(event, Errors.chatError("openModal", reason)) + return failure + } + return { status: 200, body: dialogCard(reply.modal, signed.success) as Schema.Json } + }) + +/** Leading `…` tokens, spaces and one slash, before a command's name. */ +const commandPrefix = /^(?:\s|[^<]*<\/at>)*\/?/ + +/** + * A verified targeted message. Its first word names the command, and the + * rest is the command's `text`. A message whose first word names no + * registered command is acked and nothing is sent. Nothing here ever + * reaches `ChatHandler` or posts in public unless the handler asks. + * + * @internal + */ +export const command = ( + activity: JsonObject, + options: Options & { readonly isRegistered: (id: string) => Effect.Effect } +): Effect.Effect => + Effect.gen(function*() { + if (!options.capabilities.has("commands")) return + const text = typeof activity.text === "string" ? activity.text : "" + const rest = text.replace(commandPrefix, "") + const match = /^(\S+)\s*([\s\S]*)$/.exec(rest) + const from = object(activity, "from") + const user = from === undefined ? undefined : userId(from) + const at = threadOf(activity) + if (match === null || user === undefined || at === undefined) return + const id = match[1] + if (!(yield* options.isRegistered(id))) return + const event = ChatInteraction.CommandInvoked.make({ + id, + text: match[2], + thread: at.thread, + userId: user, + raw: activity + }) + const reply = yield* ChatInteraction.handle(event, options.deadline) + if (isLate(reply)) return + switch (reply._tag) { + case "Ack": + case "Defer": + return + case "OpenModal": + return yield* refused(event, Errors.chatError("openModal", Errors.unsupported("modals"))) + case "Respond": + return yield* options.answer("respond", activity, { + markdown: reply.markdown, + ...(reply.actions === undefined ? {} : { actions: reply.actions }) + }, reply.visibility).pipe(Effect.catch((error) => refused(event, error))) + } + }) + +/** Whether a `message` is a card's `Action.Submit` fallback. @internal */ +export const isSubmitFallback = (activity: JsonObject): boolean => + activity.type === "message" && isObject(activity.value) && Envelope.reservedKey in activity.value diff --git a/packages/chat/teams/src/internal/length.ts b/packages/chat/teams/src/internal/length.ts new file mode 100644 index 00000000000..bd875764012 --- /dev/null +++ b/packages/chat/teams/src/internal/length.ts @@ -0,0 +1,170 @@ +/** + * What a message costs against the Connector's limit, counted in UTF-16 + * units of `JSON.stringify(activity)`, the serialised activity body. + * + * Every fixed size here comes from serialising the templates in + * `render.ts`, so the numbers follow the renderer. + */ +import type { Action } from "effect/chat/ChatUi" +import { maxIdLength } from "./address.ts" +import * as Render from "./render.ts" + +const size = (value: unknown): number => JSON.stringify(value).length + +/** An element in an array, with the comma before or after it. */ +const element = (value: unknown): number => size(value) + 1 + +/** + * The size of a serialised activity body the Connector takes, in UTF-16 + * units. + * + * Provisional: Microsoft documents no exact number, and this is not verified + * against the Connector yet. + * + * @internal + */ +export const Edge = 50_000 + +/** The text form with empty `text`. @internal */ +export const S = size(Render.textActivity("")) + +/** The card form with an empty card body. @internal */ +export const T = size(Render.cardActivityOf([])) + +/** One table cell and its column definition. @internal */ +export const K = element(Render.tableCell("")) + element(Render.column) + +/** + * The largest element group a source line can add: a `TextBlock`, a + * `CodeBlock` (with `PlainText` as its language), or a `Table` with one + * row of one cell, which is also more than a new row of one cell. + * + * @internal + */ +export const E = Math.max( + element(Render.textBlock("")), + element(Render.codeBlock("", "PlainText")), + element(Render.table([[""]])) +) + +const id = "x".repeat(maxIdLength) + +/** + * The members the adapter may add to an activity besides what the renderer + * gives, each id at `maxIdLength`: `replyToId`, `recipient`, and + * `entities` holding a `streaminfo` entity, the AI-generated label and a + * `targetedMessageInfo` entity. Ids are sent without JSON escapes. + * + * @internal + */ +export const A = size({ + replyToId: id, + recipient: { id }, + entities: [ + { type: "streaminfo", streamId: id, streamType: "informative", streamSequence: Number.MAX_SAFE_INTEGER }, + { + type: "https://schema.org/Message", + "@type": "Message", + "@context": "https://schema.org", + "@id": "", + additionalType: ["AIGeneratedContent"] + }, + { type: "targetedMessageInfo", messageId: id } + ] +}) - 1 + +/** + * Units set aside for mention entities. + * + * Provisional: the adapter does not send mentions in v1. + * + * @internal + */ +export const M_units = 4_000 + +/** Units set aside for file attachments: none, files are not sent in v1. @internal */ +export const F_units = 0 + +/** + * The fixed part of a message: the larger of the two forms with the first + * element of the card. The first source line has no line break before it to + * pay for its element, so `E` is reserved once for it. + * + * @internal + */ +export const skeleton = Math.max(S, T + E) + +/** @internal */ +export const R = skeleton + A + M_units + F_units + +/** @internal */ +export const messageLength = Edge - R + +/** + * What one UTF-16 unit of the source can cost once rendered and serialised. + * A line break pays for the element its line may start, and a pipe for the + * table cell and column it may add. + */ +const unit = (code: number): number => { + switch (code) { + case 0x0a: + case 0x0d: + return 2 + E + case 0x7c: + return 1 + K + case 0x22: + case 0x5c: + case 0x09: + return 2 + case 0x24: + return 3 + case 0x3c: + return 4 + case 0x26: + return 5 + default: + return code < 0x20 ? 6 : 1 + } +} + +/** @internal */ +export const weight = (text: string): number => { + let total = 0 + for (let i = 0; i < text.length; i++) total += unit(text.charCodeAt(i)) + return total +} + +const row = element(Render.actionSet([])) + +/** @internal */ +export type TokenLength = (button: { readonly id: string; readonly value?: string | undefined }) => number + +const actionCost = (action: Action, tokenLength: TokenLength): number => { + switch (action._tag) { + case "Button": + return element(Render.buttonAction({ ...action, label: "" }, "")) + 2 * weight(action.label) + + 2 * tokenLength(action) + row + case "LinkButton": + return element(Render.linkAction({ ...action, label: "", url: "" })) + weight(action.label) + + weight(action.url) + row + case "Select": + // The adapter refuses a select before it renders anything. + return 0 + } +} + +/** + * The descriptor's `measure`: the weight of each unit of the Markdown plus + * the cost of each action, where a button's cost holds its token twice (the + * action and its fallback) at `tokenLength`, and each action pays for a + * whole `ActionSet` row. With `R` added it is never less than the size of + * either form of the activity. + * + * @internal + */ +export const makeMeasure = + (tokenLength: TokenLength) => (markdown: string, actions?: ReadonlyArray): number => { + let total = weight(markdown) + for (const action of actions ?? []) total += actionCost(action, tokenLength) + return total + } diff --git a/packages/chat/teams/src/internal/normalize.ts b/packages/chat/teams/src/internal/normalize.ts new file mode 100644 index 00000000000..34ad72f0001 --- /dev/null +++ b/packages/chat/teams/src/internal/normalize.ts @@ -0,0 +1,248 @@ +import type * as ChatEvent from "effect/chat/ChatEvent" +import type * as ChatMessage from "effect/chat/ChatMessage" +import type * as ChatThread from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import * as Option from "effect/Option" +import type * as Schema from "effect/Schema" +import * as Address from "./address.ts" +import * as Download from "./download.ts" + +type JsonObject = { readonly [key: string]: Schema.Json } + +/** @internal */ +export const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +/** @internal */ +export const string = (object: JsonObject | undefined, key: string): string | undefined => { + const value = object?.[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +/** @internal */ +export const object = (object: JsonObject | undefined, key: string): JsonObject | undefined => { + const value = object?.[key] + return isObject(value) ? value : undefined +} + +/** @internal */ +export interface Context { + readonly appId: string + readonly connectorHosts: ReadonlySet + readonly fileHostSuffixes: ReadonlyArray +} + +/** The bot's own Connector id. @internal */ +export const botId = (appId: string): string => `28:${appId}` + +/** + * The public id of a user: the Entra object id for a person, the Connector + * id (`29:…`, `28:…`) for a bot or anyone without one. + * + * @internal + */ +export const userId = (account: JsonObject): string | undefined => + string(account, "aadObjectId") ?? string(account, "id") + +/** + * Whether the activity was sent to the bot alone, as a targeted message. + * Read from `recipient.isTargeted`. + * + * @internal + */ +export const isTargeted = (activity: JsonObject): boolean => object(activity, "recipient")?.isTargeted === true + +/** + * Where an activity's message lives, and what kind of address it is. A + * channel post's conversation names the post itself as its reply chain + * root: such a post is addressed to the channel, and is the opener of the + * thread under it. + * + * @internal + */ +export const threadOf = ( + activity: JsonObject +): { readonly thread: ChatThread.ChatThread; readonly kind: ChatEvent.ThreadKind } | undefined => { + const conversationId = string(object(activity, "conversation"), "id") + if (conversationId === undefined || conversationId.length > Address.maxIdLength) return undefined + const parts = Address.parts(Address.thread(conversationId)) + if (Option.isNone(parts)) return undefined + const id = string(activity, "id") + if (parts.value.root !== undefined && parts.value.root === id) { + return { thread: Address.thread(parts.value.base), kind: "channel" } + } + const conversationType = string(object(activity, "conversation"), "conversationType") + return { + thread: Address.thread(conversationId), + kind: parts.value.root !== undefined ? "thread" : conversationType === "personal" ? "direct" : "channel" + } +} + +const timestamp = (activity: JsonObject): DateTime.Utc | undefined => { + const value = string(activity, "timestamp") + if (value === undefined) return undefined + return Option.getOrUndefined(DateTime.make(value).pipe(Option.map(DateTime.toUtc))) +} + +const mentionKind = (mentioned: JsonObject, conversationId: string): ChatMessage.Mention["kind"] => { + if (string(mentioned, "type") === "tag") return "role" + const id = string(mentioned, "id") ?? "" + if (id === Address.baseConversation(conversationId) || id.startsWith("19:")) return "channel" + return "user" +} + +/** + * The mention entities, in order, located in `text` by their `` text. + * An entity whose text is not found is left out. + */ +const mentions = (activity: JsonObject, text: string, self: string): Array => { + const entities = activity.entities + if (!Array.isArray(entities)) return [] + const conversationId = string(object(activity, "conversation"), "id") ?? "" + const out: Array = [] + let from = 0 + for (const entity of entities) { + if (!isObject(entity) || entity.type !== "mention") continue + const mentioned = object(entity, "mentioned") + const token = string(entity, "text") + const id = mentioned === undefined ? undefined : userId(mentioned) + if (mentioned === undefined || token === undefined || id === undefined) continue + const start = text.indexOf(token, from) + if (start === -1) continue + from = start + token.length + out.push({ + kind: mentionKind(mentioned, conversationId), + id, + start, + end: start + token.length, + isSelf: string(mentioned, "id") === self + }) + } + return out +} + +const attachments = (activity: JsonObject, context: Context): Array => { + const list = activity.attachments + const serviceUrl = string(activity, "serviceUrl") + if (!Array.isArray(list) || serviceUrl === undefined) return [] + const conversationType = string(object(activity, "conversation"), "conversationType") + const out: Array = [] + for (const attachment of list) { + if (isObject(attachment) && attachment.contentType === "text/html") continue + const classified = Download.classify(attachment, { + serviceUrl, + ...(conversationType === undefined ? {} : { conversationType }), + connectorHosts: context.connectorHosts, + fileHostSuffixes: context.fileHostSuffixes + }) + if (classified !== undefined) out.push(classified) + } + return out +} + +/** + * A `message` activity as a `ChatMessage`. + * + * - `text` is Teams' `text` as delivered, with `…` for each + * mention, which is where `mentions` point. + * - The author id follows `userId`. `raw` keeps the Connector id. + * + * @internal + */ +export const message = (activity: JsonObject, context: Context): ChatMessage.ChatMessage | undefined => { + const id = string(activity, "id") + const from = object(activity, "from") + const author = from === undefined ? undefined : userId(from) + const at = threadOf(activity) + const time = timestamp(activity) + if (id === undefined || from === undefined || author === undefined || at === undefined || time === undefined) { + return undefined + } + const self = botId(context.appId) + const connectorId = string(from, "id") ?? "" + const text = typeof activity.text === "string" ? activity.text : "" + const name = string(from, "name") + return { + id, + thread: at.thread, + author: { + id: author, + ...(name === undefined ? {} : { name }), + isBot: connectorId.startsWith("28:"), + isSelf: connectorId === self + }, + text, + mentions: mentions(activity, text, self), + attachments: attachments(activity, context), + timestamp: time, + raw: activity + } +} + +const reactionEmoji: Readonly> = { + like: "\u{1F44D}", + heart: "\u2764\uFE0F", + laugh: "\u{1F606}", + surprised: "\u{1F62E}", + sad: "\u{1F622}", + angry: "\u{1F621}" +} + +/** + * A Teams reaction type as an emoji: the six documented types as Unicode, + * any other as `custom:{type}`. + * + * @internal + */ +export const emoji = (type: string): string => reactionEmoji[type] ?? `custom:${type}` + +const reactions = (activity: JsonObject): Array => { + const messageId = string(activity, "replyToId") + const from = object(activity, "from") + const user = from === undefined ? undefined : userId(from) + const at = threadOf(activity) + if (messageId === undefined || user === undefined || at === undefined) return [] + const time = timestamp(activity) + const out: Array = [] + for (const [key, tag] of [["reactionsAdded", "ReactionAdded"], ["reactionsRemoved", "ReactionRemoved"]] as const) { + const list = activity[key] + if (!Array.isArray(list)) continue + for (const reaction of list) { + const type = isObject(reaction) ? string(reaction, "type") : undefined + if (type === undefined) continue + out.push({ + _tag: tag, + thread: at.thread, + messageId, + userId: user, + emoji: emoji(type), + ...(time === undefined ? {} : { timestamp: time }), + raw: activity + }) + } + } + return out +} + +/** + * A verified, untargeted activity as zero or more events. A `message` + * carrying a card submit (`value`) is an interaction, not a message, and + * gives none here. + * + * @internal + */ +export const normalize = (activity: unknown, context: Context): ReadonlyArray => { + if (!isObject(activity) || isTargeted(activity)) return [] + switch (activity.type) { + case "message": { + if (activity.value !== undefined) return [] + const at = threadOf(activity) + const created = message(activity, context) + if (at === undefined || created === undefined) return [] + return [{ _tag: "MessageCreated", thread: at.thread, kind: at.kind, message: created, raw: activity }] + } + case "messageReaction": + return reactions(activity) + default: + return [] + } +} diff --git a/packages/chat/teams/src/internal/render.ts b/packages/chat/teams/src/internal/render.ts new file mode 100644 index 00000000000..6e780f500ca --- /dev/null +++ b/packages/chat/teams/src/internal/render.ts @@ -0,0 +1,312 @@ +/** + * Renders Markdown as a Bot Framework activity for Teams. A message is sent + * in one of two forms, never mixed: text, when it has no actions, and an + * Adaptive Card, when it has. + * + * The renderer keeps to the rules that make `Length.measure` an upper + * bound: a source line gives at most one element, a table row has at most + * one cell more than it has pipes, each source unit is sent at most once, + * and nothing is sent beyond the templates below and the source text. + */ +import type { Action, Button, LinkButton } from "effect/chat/ChatUi" + +/** @internal */ +export const cardContentType = "application/vnd.microsoft.card.adaptive" + +/** + * The most actions in one `ActionSet` row: the number of actions Teams + * shows in a row before it moves the rest into an overflow menu. + * + * @internal + */ +export const actionsPerRow = 6 + +/** Lone surrogates become U+FFFD, which JSON sends as one unit. @internal */ +export const wellFormed = (text: string): string => + text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?` mention or entity + * forms in text Teams reads as Markdown. + * + * @internal + */ +export const escapeText = (text: string): string => text.replace(/[<&]/g, (c) => c === "<" ? "<" : "&") + +/** @internal */ +export const textActivity = (markdown: string): Record => ({ + type: "message", + text: escapeDollars(escapeText(wellFormed(markdown))), + textFormat: "extendedmarkdown" +}) + +/** @internal */ +export const textBlock = (text: string) => ({ type: "TextBlock", text, wrap: true }) + +/** @internal */ +export const codeBlock = (codeSnippet: string, language: string) => ({ type: "CodeBlock", codeSnippet, language }) + +/** @internal */ +export const column = { width: 1 } + +/** @internal */ +export const tableCell = (text: string) => ({ type: "TableCell", items: [textBlock(text)] }) + +/** @internal */ +export const tableRow = (cells: ReadonlyArray) => ({ type: "TableRow", cells: cells.map(tableCell) }) + +/** A table with one column for each cell of its widest row. @internal */ +export const table = (rows: ReadonlyArray>) => ({ + type: "Table", + firstRowAsHeaders: true, + columns: Array.from({ length: Math.max(0, ...rows.map((row) => row.length)) }, () => column), + rows: rows.map(tableRow) +}) + +/** @internal */ +export const actionSet = (actions: ReadonlyArray) => ({ type: "ActionSet", actions }) + +/** @internal */ +export const card = (body: ReadonlyArray) => ({ + type: "AdaptiveCard", + version: "1.5", + body, + msteams: { width: "Full" } +}) + +/** @internal */ +export const cardActivityOf = (body: ReadonlyArray): Record => ({ + type: "message", + attachments: [{ contentType: cardContentType, content: card(body) }] +}) + +const styles = { primary: "positive", danger: "destructive" } as const + +/** + * `Action.Execute`, with `Action.Submit` as the fallback for clients + * without Universal Actions. `token` carries the button's id and value. + * + * @internal + */ +export const buttonAction = (button: Button, token: string) => { + const title = wellFormed(button.label) + const style = button.style === "primary" || button.style === "danger" ? { style: styles[button.style] } : {} + return { + type: "Action.Execute", + title, + verb: "effect", + data: { effect: token }, + ...style, + fallback: { type: "Action.Submit", title, data: { effect: token } } + } +} + +/** + * A button that opens a dialog: an `Action.Submit` with `task/fetch`, + * smaller than the `Action.Execute` and fallback the measure prices. + * + * @internal + */ +export const dialogAction = (button: Button, token: string) => ({ + type: "Action.Submit", + title: wellFormed(button.label), + data: { msteams: { type: "task/fetch" }, effect: token } +}) + +/** @internal */ +export const linkAction = (link: LinkButton) => ({ + type: "Action.OpenUrl", + title: wellFormed(link.label), + url: wellFormed(link.url) +}) + +/** + * The card form. Selects are not rendered: the adapter refuses them first. + * + * @internal + */ +export const cardActivity = ( + markdown: string, + actions: ReadonlyArray, + encode: (button: Button) => string, + opensDialog: (button: Button) => boolean = () => false +): Record => { + const body: Array = blocks(markdown) + const rendered: Array = [] + for (const action of actions) { + if (action._tag === "Button") { + rendered.push(opensDialog(action) ? dialogAction(action, encode(action)) : buttonAction(action, encode(action))) + } else if (action._tag === "LinkButton") rendered.push(linkAction(action)) + } + for (let i = 0; i < rendered.length; i += actionsPerRow) { + body.push(actionSet(rendered.slice(i, i + actionsPerRow))) + } + return cardActivityOf(body) +} + +interface Fence { + readonly marker: string + readonly language: string +} + +const fenceOpen = (line: string): Fence | undefined => { + const match = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line) + if (match === null) return undefined + const [, marker, info] = match + if (marker[0] === "`" && info.includes("`")) return undefined + const word = info.trim().split(/\s+/)[0] + return { marker, language: word === "" ? "PlainText" : word } +} + +const fenceCloses = (fence: Fence, line: string): boolean => { + const match = /^ {0,3}(`{3,}|~{3,})[ \t]*$/.exec(line) + return match !== null && match[1][0] === fence.marker[0] && match[1].length >= fence.marker.length +} + +/** + * Escapes each `$` outside code as `\$`, so that Teams reads no math in + * the text form. A `$` the Markdown already escapes, after an odd run of + * backslashes, is left alone, and so is code: fenced blocks and code spans + * on one line. One unit per `$` at most, as `Length.weight` prices it. + * + * @internal + */ +export const escapeDollars = (markdown: string): string => { + const parts = markdown.split(/(\r\n|\r|\n)/) + let fence: Fence | undefined + for (let i = 0; i < parts.length; i += 2) { + const line = parts[i] + if (fence !== undefined) { + if (fenceCloses(fence, line)) fence = undefined + continue + } + fence = fenceOpen(line) + if (fence === undefined) parts[i] = escapeLine(line) + } + return parts.join("") +} + +const escapeLine = (line: string): string => { + let out = "" + let i = 0 + while (i < line.length) { + const char = line[i] + if (char === "`") { + let run = 1 + while (line[i + run] === "`") run++ + const ticks = "`".repeat(run) + const close = findRun(line, ticks, i + run) + const end = close === -1 ? i + run : close + run + out += line.slice(i, end) + i = end + continue + } + if (char === "$") { + let backslashes = 0 + while (line[i - 1 - backslashes] === "\\") backslashes++ + out += backslashes % 2 === 0 ? "\\$" : "$" + } else out += char + i++ + } + return out +} + +/** The index of a run of exactly `ticks` from `from`, or -1. */ +const findRun = (line: string, ticks: string, from: number): number => { + for (let at = line.indexOf(ticks, from); at !== -1; at = line.indexOf(ticks, at + 1)) { + if (line[at - 1] !== "`" && line[at + ticks.length] !== "`") return at + } + return -1 +} + +const delimiterRow = /^[ \t]*\|?(?:[ \t]*:?-+:?[ \t]*\|)*[ \t]*:?-+:?[ \t]*\|?[ \t]*$/ + +const isDelimiter = (line: string): boolean => line.includes("|") && delimiterRow.test(line) + +/** The cells of a table row. There are never more than its pipes plus one. */ +const cells = (line: string): Array => { + let row = line.trim() + if (row.startsWith("|")) row = row.slice(1) + if (row.endsWith("|") && !row.endsWith("\\|")) row = row.slice(0, -1) + return row.split(/(? escapeText(cell.trim().replace(/\\\|/g, "|"))) +} + +/** + * One line of a paragraph, with the block syntax a TextBlock cannot show + * taken off: quote markers and heading markers are dropped, and the first + * list marker is kept. `> - # x` becomes `- x`. + */ +const flatten = (line: string): string => { + let rest = line + let marker = "" + for (;;) { + const quote = /^ {0,3}> ?/.exec(rest) + if (quote !== null) { + rest = rest.slice(quote[0].length) + continue + } + if (marker === "") { + const list = /^[ \t]*(?:[-*+]|\d{1,9}[.)])(?:[ \t]+|$)/.exec(rest) + if (list !== null) { + marker = list[0] + rest = rest.slice(marker.length) + continue + } + } + const heading = /^ {0,3}#{1,6}(?:[ \t]+|$)/.exec(rest) + if (heading !== null) { + rest = rest.slice(heading[0].length) + continue + } + return marker + rest + } +} + +/** + * The card body for Markdown: `TextBlock`, `CodeBlock` and `Table` + * elements in the order of the source. Consecutive lines of text share one + * `TextBlock`, joined by `\n`. + * + * @internal + */ +export const blocks = (markdown: string): Array => { + const lines = wellFormed(markdown).split(/\r\n|\r|\n/) + const body: Array = [] + let text: Array = [] + const flush = () => { + let start = 0 + let end = text.length + while (start < end && text[start].trim() === "") start++ + while (end > start && text[end - 1].trim() === "") end-- + if (start < end) body.push(textBlock(escapeText(text.slice(start, end).join("\n")))) + text = [] + } + let i = 0 + while (i < lines.length) { + const line = lines[i] + const fence = fenceOpen(line) + if (fence !== undefined) { + flush() + const code: Array = [] + i++ + while (i < lines.length && !fenceCloses(fence, lines[i])) code.push(lines[i++]) + i++ + body.push(codeBlock(code.join("\n"), fence.language)) + continue + } + if (line.includes("|") && i + 1 < lines.length && isDelimiter(lines[i + 1])) { + flush() + const rows = [cells(line)] + i += 2 + while (i < lines.length && lines[i].includes("|") && fenceOpen(lines[i]) === undefined) { + rows.push(cells(lines[i++])) + } + body.push(table(rows)) + continue + } + text.push(flatten(line)) + i++ + } + flush() + return body +} diff --git a/packages/chat/teams/src/internal/webhook.ts b/packages/chat/teams/src/internal/webhook.ts new file mode 100644 index 00000000000..cada89286fc --- /dev/null +++ b/packages/chat/teams/src/internal/webhook.ts @@ -0,0 +1,147 @@ +import type * as ChatEvent from "effect/chat/ChatEvent" +import type { NotAccepted } from "effect/chat/ChatHandler" +import type * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as HttpServerResponse from "effect/http/HttpServerResponse" +import * as Option from "effect/Option" +import type * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import { tenantOf, type Verdict } from "./auth.ts" +import type { InvokeResponse } from "./interactions.ts" +import { isSubmitFallback } from "./interactions.ts" +import { type Context, isObject, isTargeted, normalize } from "./normalize.ts" + +type JsonObject = { readonly [key: string]: Schema.Json } + +/** The body, or `None` once it passes `maxBytes`. @internal */ +export const readBody = ( + request: HttpServerRequest.HttpServerRequest, + maxBytes: number +): Effect.Effect> => { + const length = request.headers["content-length"] + if (length !== undefined && Number(length) > maxBytes) return Effect.succeed(Option.none()) + return Effect.gen(function*() { + const chunks: Array = [] + let total = 0 + const tooLarge = yield* request.stream.pipe( + Stream.takeUntil((chunk) => { + total += chunk.length + chunks.push(chunk) + return total > maxBytes + }), + Stream.runDrain, + Effect.as(false), + Effect.catch(() => Effect.succeed(true)) + ) + if (tooLarge || total > maxBytes) return Option.none() + const body = new Uint8Array(total) + let offset = 0 + for (const chunk of chunks) { + body.set(chunk, offset) + offset += chunk.length + } + return Option.some(body) + }) +} + +const status = (code: number) => HttpServerResponse.empty({ status: code }) + +/** + * What is logged with each 503 of the route. + * + * @internal + */ +export const unavailable = { + keys: "teams webhook: the signing keys could not be read, answered 503", + normalize: "teams webhook: the activity could not be normalised, answered 503", + notAccepted: "teams webhook: the handler refused the event, answered 503", + deadline: "teams webhook: the request passed its deadline, answered 503" +} as const + +/** @internal */ +export interface Options { + readonly verify: (authorization: string | undefined, activity: { + readonly serviceUrl: unknown + readonly channelId: unknown + readonly tenantId: string | undefined + }) => Effect.Effect + readonly context: Context + readonly handle: (event: ChatEvent.ChatEvent) => Effect.Effect + readonly onUnhandled: (activity: JsonObject) => Effect.Effect + readonly invoke: (activity: JsonObject) => Effect.Effect + readonly command: (activity: JsonObject) => Effect.Effect + readonly deadline: Duration.Input + readonly maxBodyBytes: number +} + +const respond = (response: InvokeResponse) => + response.body === undefined + ? status(response.status) + : HttpServerResponse.jsonUnsafe(response.body, { status: response.status }) + +/** + * One Bot Framework activity. + * + * - 413 past the body limit, 400 for a body that is not a JSON object. + * - 403 when the token, its `serviceurl`, the channel or the tenant does + * not check out, and 503 when the signing keys can't be read. + * - A targeted activity is classified first, whatever its type: a message + * takes the command path, an invoke goes to the interaction handler, and + * anything else is answered 200 and dropped. None reaches `ChatHandler` + * or `onUnhandled`. + * - An invoke, or a card's submit fallback, goes to the interaction + * handler, whose answer is the response. + * - Every other activity is normalised and handed to the `ChatHandler` + * one event at a time: 200 when all succeed, 503 when one fails + * `NotAccepted`, or when a `message` can't be normalised. Any other + * activity that gives no event goes to `onUnhandled` and is answered 200. + * - 503 past `deadline`, the handler interrupted. Each 503 is logged with + * its own message. + * + * @internal + */ +export const handleRequest = ( + request: HttpServerRequest.HttpServerRequest, + options: Options +): Effect.Effect => + Effect.gen(function*() { + const body = yield* readBody(request, options.maxBodyBytes) + if (Option.isNone(body)) return status(413) + let activity: unknown + try { + activity = JSON.parse(new TextDecoder().decode(body.value)) + } catch { + return status(400) + } + if (!isObject(activity)) return status(400) + const verdict = yield* options.verify(request.headers.authorization, { + serviceUrl: activity.serviceUrl, + channelId: activity.channelId, + tenantId: tenantOf(activity) + }) + if (verdict === "forbidden") return status(403) + if (verdict === "unavailable") return yield* Effect.as(Effect.logWarning(unavailable.keys), status(503)) + if (isTargeted(activity)) { + if (activity.type === "message") return yield* Effect.as(options.command(activity), status(200)) + if (activity.type === "invoke") return respond(yield* options.invoke(activity)) + return status(200) + } + if (activity.type === "invoke") return respond(yield* options.invoke(activity)) + if (isSubmitFallback(activity)) return respond(yield* options.invoke(activity)) + const events = normalize(activity, options.context) + if (events.length === 0 && activity.type === "message") { + return yield* Effect.as(Effect.logWarning(unavailable.normalize), status(503)) + } + if (events.length === 0) return yield* Effect.as(options.onUnhandled(activity), status(200)) + return yield* Effect.forEach(events, options.handle, { discard: true }).pipe( + Effect.as(status(200)), + Effect.catchTag("NotAccepted", () => Effect.as(Effect.logWarning(unavailable.notAccepted), status(503))) + ) + }).pipe( + Effect.timeoutOption(options.deadline), + Effect.flatMap(Option.match({ + onNone: () => Effect.as(Effect.logWarning(unavailable.deadline), status(503)), + onSome: Effect.succeed + })) + ) diff --git a/packages/chat/teams/test/conformance.test.ts b/packages/chat/teams/test/conformance.test.ts new file mode 100644 index 00000000000..17e8a38179a --- /dev/null +++ b/packages/chat/teams/test/conformance.test.ts @@ -0,0 +1,156 @@ +/** + * The conformance suite against the default layer, over a stubbed Connector + * that answers every write. Capabilities behind `unverified` are off, so + * their rules check that the descriptor leaves them out and the ops refuse. + */ +import * as Adapter from "@effect/chat-teams/internal/adapter" +import * as Length from "@effect/chat-teams/internal/length" +import * as TeamsChat from "@effect/chat-teams/TeamsChat" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import { conformance } from "effect/chat/ChatTest" +import * as Digest from "effect/crypto/Digest" +import * as Mac from "effect/crypto/Mac" +import * as SecureRandom from "effect/crypto/SecureRandom" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" + +const appId = "00000000-0000-0000-0000-0000000000a1" +const tenantId = "00000000-0000-0000-0000-0000000000b2" +const conversationId = "19:conformance@thread.tacv2" +const thread = TeamsChat.thread(conversationId) + +const json = (body: unknown, status = 200) => + new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }) + +const makeConnector = (sent: Array<{ readonly url: URL; readonly body: string }> = []) => { + let next = 1_700_000_000_000 + const posted = new Set() + return HttpClient.make((request, url) => + Effect.sync(() => { + if (request.body._tag === "Uint8Array") sent.push({ url, body: new TextDecoder().decode(request.body.body) }) + let response: Response + if (url.host === "login.microsoftonline.com") { + response = json({ access_token: "token", expires_in: 3600, token_type: "Bearer" }) + } else if (request.method === "POST") { + const id = String(next++) + posted.add(id) + response = json({ id }) + } else if (request.method === "PUT" || request.method === "DELETE") { + const id = decodeURIComponent(url.pathname.split("/").pop()!) + const known = posted.has(id) + if (request.method === "DELETE") posted.delete(id) + response = known ? json({}) : json({ error: { code: "ActivityNotFound" } }, 404) + } else if (url.pathname.includes("/members/")) { + response = json({ id: "29:user", aadObjectId: "user", name: "User" }) + } else response = json({ members: [] }) + // As Fetch does: the response names the URL it came from, query included. + Object.defineProperty(response, "url", { value: url.href }) + return HttpClientResponse.fromWeb(request, response) + }) + ) +} + +const layer = ( + extra: Partial[0]> = {}, + sent?: Array<{ readonly url: URL; readonly body: string }> +) => + TeamsChat.layer({ appId, tenantId, credentials: Redacted.make("secret"), ...extra }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(Layer.succeed(HttpClient.HttpClient, makeConnector(sent))), + Layer.provide(Layer.mergeAll(Digest.layerWebCrypto, Mac.layerWebCrypto)), + Layer.provide(SecureRandom.layerWebCrypto) + ) + +let sequence = 0 + +const options = { + platform: "teams", + thread, + parseMention: TeamsChat.parseMention, + userPosts: (at: typeof thread, text: string) => + Effect.sync(() => { + const parts = TeamsChat.parts(at) + const id = String(1_800_000_000_000 + sequence++) + const events = TeamsChat.normalize({ + type: "message", + id, + timestamp: new Date().toISOString(), + serviceUrl: "https://smba.trafficmanager.net/teams", + channelId: "msteams", + from: { id: "29:user", aadObjectId: "user", name: "User" }, + recipient: { id: `28:${appId}`, name: "Bot" }, + conversation: { + id: parts._tag === "Some" ? parts.value.conversationId : conversationId, + conversationType: "channel", + tenantId + }, + channelData: { tenant: { id: tenantId } }, + textFormat: "plain", + text + }, { appId }) + return events[0]! + }) +} + +describe("conformance over the stubbed Connector", () => { + const names = Object.keys(conformance(options)) + for (const name of names) { + it.effect(name, () => conformance(options)[name].pipe(Effect.provide(layer()))) + } +}) + +describe("reads over the stubbed Connector", () => { + it.effect("getThread sends its query and accepts the answer", () => + Effect.gen(function*() { + const chat = yield* Chat.Chat + const info = yield* chat.getThread(thread) + assert.strictEqual(info.kind, "channel") + }).pipe(Effect.provide(layer()))) +}) + +describe("buttons over the stubbed Connector", () => { + const buttons = layer({ + interactionKeys: [Redacted.make({ kty: "oct", kid: "k1", k: "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc" })], + unverified: { buttons: true } + }) + + it.effect("counts the signed envelope against actionDataLength", () => + Effect.gen(function*() { + const chat = yield* Chat.Chat + // The raw id and value fit the cap; with the envelope they do not. + const over = { _tag: "Button", id: "b", label: "B", value: "v".repeat(Adapter.actionDataLength - 1) } as const + const error = yield* Effect.flip(chat.post(thread, { markdown: "x", actions: [over] })) + assert.strictEqual(error.reason._tag, "TooLarge") + const posted = yield* chat.post(thread, { markdown: "x", actions: [{ ...over, value: "v".repeat(400) }] }) + assert.isString(posted.id) + }).pipe(Effect.provide(buttons))) +}) + +describe("command answers over the stubbed Connector", () => { + it.effect("a private answer names its recipient by id alone, so it stays in the size bound", () => { + const sent: Array<{ readonly url: URL; readonly body: string }> = [] + return Effect.gen(function*() { + const service = yield* TeamsChat.TeamsChat + const raw = { + type: "message", + id: "1", + serviceUrl: "https://smba.trafficmanager.net/teams", + from: { id: "29:user", aadObjectId: "user", name: "n".repeat(10_000) }, + conversation: { id: conversationId } + } + yield* Adapter.internals.get(service)!.answer( + "respond", + raw, + { markdown: "a".repeat(Length.messageLength) }, + "ephemeral" + ) + const answer = sent.find(({ url }) => url.searchParams.get("isTargetedActivity") === "true")! + assert.deepStrictEqual(JSON.parse(answer.body).recipient, { id: "29:user" }) + assert.isAtMost(answer.body.length, Length.Edge) + }).pipe(Effect.provide(layer({}, sent))) + }) +}) diff --git a/packages/chat/teams/test/download.test.ts b/packages/chat/teams/test/download.test.ts new file mode 100644 index 00000000000..3ccbe1f4eb0 --- /dev/null +++ b/packages/chat/teams/test/download.test.ts @@ -0,0 +1,109 @@ +import * as Download from "@effect/chat-teams/internal/download" +import { assert, describe, it } from "@effect/vitest" +import type { ChatError } from "effect/chat/ChatError" +import * as Context from "effect/Context" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Redacted from "effect/Redacted" +import * as Stream from "effect/Stream" + +interface Sent { + readonly url: string + readonly authorization: string | undefined + readonly redirect: RequestRedirect | undefined +} + +const serve = (handler: (url: string) => Response) => { + const sent: Array = [] + const http = HttpClient.make((request, url, _signal, fiber) => + Effect.sync(() => { + sent.push({ + url: url.href, + authorization: request.headers.authorization, + redirect: Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit)?.redirect + }) + return HttpClientResponse.fromWeb(request, handler(url.href)) + }) + ) + return { sent, http } +} + +const connectorUrl = "https://smba.trafficmanager.net/amer/v3/attachments/0-cus-d1-abc/views/original" + +const options = (http: HttpClient.HttpClient, maxBytes = 1024) => ({ + http, + maxBytes, + token: Effect.succeed(Redacted.make("secret")), + connectorHosts: new Set(["smba.trafficmanager.net"]), + fileHostSuffixes: ["sharepoint.com"], + redirectHosts: new Set(["cdn.example.com"]) +}) + +const reasonOf = (stream: Stream.Stream) => + Stream.runDrain(stream).pipe(Effect.flip, Effect.map((error) => error.reason)) + +describe("download", () => { + it.effect("sends the token to the Connector and streams the body", () => + Effect.gen(function*() { + const { http, sent } = serve(() => new Response("hello")) + const bytes = yield* Stream.mkUint8Array(Download.download(`connector:${connectorUrl}`, options(http))) + assert.strictEqual(new TextDecoder().decode(bytes), "hello") + assert.deepStrictEqual(sent, [{ url: connectorUrl, authorization: "Bearer secret", redirect: "manual" }]) + })) + + it.effect("sends the token on the first hop only, and never to an untrusted URL", () => + Effect.gen(function*() { + const { http, sent } = serve((url) => + new Response(null, { + status: 302, + headers: { location: url === connectorUrl ? "https://cdn.example.com/file" : "https://evil.example/file" } + }) + ) + const redirected = yield* reasonOf(Download.download(`connector:${connectorUrl}`, options(http))) + assert.deepStrictEqual([redirected._tag, redirected.detail], ["Forbidden", "untrusted_url"]) + assert.deepStrictEqual(sent.map((request) => [request.url, request.authorization]), [ + [connectorUrl, "Bearer secret"], + ["https://cdn.example.com/file", undefined] + ]) + + // A redirect host named as the first hop. + const id = "connector:https://cdn.example.com/v3/attachments/a/views/original" + const first = yield* reasonOf(Download.download(id, options(http))) + assert.deepStrictEqual([first._tag, first.detail], ["Forbidden", "untrusted_url"]) + assert.strictEqual(sent.length, 2) + })) + + it.effect("fails TooLarge past maxBytes", () => + Effect.gen(function*() { + const { http } = serve(() => new Response(new Uint8Array(16))) + const reason = yield* reasonOf(Download.download(`connector:${connectorUrl}`, options(http, 8))) + assert.strictEqual(reason._tag, "TooLarge") + })) +}) + +describe("classify", () => { + it("refuses a lookalike file host", () => { + const context = { + serviceUrl: "https://smba.trafficmanager.net/amer/", + conversationType: "personal", + connectorHosts: new Set(["smba.trafficmanager.net"]), + fileHostSuffixes: ["sharepoint.com"] + } + const attachment = (host: string) => ({ + contentType: Download.fileDownloadInfo, + contentUrl: `https://${host}/personal/a/report.pdf`, + name: "report.pdf", + content: { downloadUrl: `https://${host}/download.aspx?tempauth=x` } + }) + assert.deepStrictEqual(Download.classify(attachment("contoso.sharepoint.com"), context), { + id: "preauth:https://contoso.sharepoint.com/download.aspx?tempauth=x", + name: "report.pdf" + }) + assert.deepStrictEqual(Download.classify(attachment("evilsharepoint.com"), context), { + id: "url:https://evilsharepoint.com/personal/a/report.pdf", + name: "report.pdf" + }) + }) +}) diff --git a/packages/chat/teams/test/envelope.test.ts b/packages/chat/teams/test/envelope.test.ts new file mode 100644 index 00000000000..d68724a43e6 --- /dev/null +++ b/packages/chat/teams/test/envelope.test.ts @@ -0,0 +1,66 @@ +import * as Envelope from "@effect/chat-teams/internal/envelope" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatUi from "effect/chat/ChatUi" +import { Digest, Mac, SecureRandom } from "effect/crypto" +import * as Effect from "effect/Effect" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" + +const layer = Layer.mergeAll(Digest.layerWebCrypto, Mac.layerWebCrypto).pipe( + Layer.provideMerge(SecureRandom.layerWebCrypto) +) + +const k1 = Redacted.make({ kty: "oct", kid: "k1", k: "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc" } as const) +const k2 = Redacted.make({ kty: "oct", kid: "k2", k: "CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg" } as const) + +const options = { appId: "app-1", tenantId: "tenant-1" } +const conversation = "19:abc@thread.tacv2" + +describe("envelope", () => { + it.effect("refuses a button from another conversation, a tampered token and an unknown kid", () => + Effect.gen(function*() { + const envelope = yield* Envelope.make({ ...options, keys: [k1] }) + const token = yield* envelope.signButton(conversation, { id: "approve" }) + assert.isTrue(Option.isNone(yield* envelope.verifyButton(token, { conversationId: "19:other@thread.tacv2" }))) + + const [header, payload, signature] = token.split(".") + const tampered = `${header}.${payload}.${signature.slice(0, -2)}${signature.endsWith("AA") ? "BA" : "AA"}` + assert.isTrue(Option.isNone(yield* envelope.verifyButton(tampered, { conversationId: conversation }))) + + const other = yield* Envelope.make({ ...options, keys: [k2] }) + const unknownKid = yield* other.signButton(conversation, { id: "approve" }) + assert.isTrue(Option.isNone(yield* envelope.verifyButton(unknownKid, { conversationId: conversation }))) + + const rotated = yield* Envelope.make({ ...options, keys: [k2, k1] }) + assert.isTrue(Option.isSome(yield* rotated.verifyButton(token, { conversationId: conversation }))) + }).pipe(Effect.provide(layer))) + + it.effect("binds a dialog to its user and reply chain, and a submit to its definition", () => + Effect.gen(function*() { + const envelope = yield* Envelope.make({ ...options, keys: [k1] }) + const modal: ChatUi.Modal = { + _tag: "Modal", + id: "deploy", + title: "Deploy", + metadata: "m", + elements: [ + { _tag: "SelectInput", id: "env", label: "Env", options: [{ label: "Prod", value: "prod" }] }, + { _tag: "TextInput", id: "note", label: "Note", optional: true } + ] + } + const opened = `${conversation};messageid=123` + const token = yield* envelope.signDialog({ conversationId: opened, userId: "u1", messageId: "123", modal }) + assert.isTrue(Option.isNone(yield* envelope.verifyDialog(token, { conversationId: opened, userId: "u2" }))) + // The same channel, another reply chain: refused, so a submit keeps the click's origin. + const moved = `${conversation};messageid=456` + assert.isTrue(Option.isNone(yield* envelope.verifyDialog(token, { conversationId: moved, userId: "u1" }))) + const verified = yield* envelope.verifyDialog(token, { conversationId: opened, userId: "u1" }) + assert.deepStrictEqual(verified, Option.some({ conversationId: opened, messageId: "123", modal })) + assert.deepStrictEqual( + Envelope.validateSubmission(modal, { effect: token, env: "prod", note: "" }), + Option.some({ env: "prod" }) + ) + assert.isTrue(Option.isNone(Envelope.validateSubmission(modal, { env: "staging" }))) + }).pipe(Effect.provide(layer))) +}) diff --git a/packages/chat/teams/test/length.test.ts b/packages/chat/teams/test/length.test.ts new file mode 100644 index 00000000000..98f899081bb --- /dev/null +++ b/packages/chat/teams/test/length.test.ts @@ -0,0 +1,37 @@ +import * as Length from "@effect/chat-teams/internal/length" +import * as Render from "@effect/chat-teams/internal/render" +import { assert, describe, it } from "@effect/vitest" +import type * as ChatUi from "effect/chat/ChatUi" + +describe("render", () => { + it("measure bounds each serialised form", () => { + const tokenLength = (button: { readonly id: string; readonly value?: string | undefined }) => + 20 + button.id.length + (button.value?.length ?? 0) + const measure = Length.makeMeasure(tokenLength) + const actions: ReadonlyArray = [ + { _tag: "Button", id: "approve", label: "Yes & ", style: "danger", value: "v" }, + { _tag: "LinkButton", label: "Docs", url: "https://example.com/?a=1&b=<2>" } + ] + const cards: ReadonlyArray< + readonly [string, ReadonlyArray, ((button: ChatUi.Button) => boolean)?] + > = [ + ["```\nx\n```\np\n\nq\n\nr", actions], + ["> - # x", actions], + ["| a | b |\n| - | - |\n| c | d |", actions], + // A dialog button's label with lone surrogates. + ["x", [{ _tag: "Button", id: "open", label: "\uD800".repeat(20_000) }], () => true] + ] + for (const [markdown, actions, opensDialog] of cards) { + const card = Render.cardActivity(markdown, actions, (button) => "t".repeat(tokenLength(button)), opensDialog) + assert.isAtMost(JSON.stringify(card).length, measure(markdown, actions) + Length.R, markdown) + } + const dollars = "$".repeat(10_000) + "\\$" + assert.isAtMost(JSON.stringify(Render.textActivity(dollars)).length, measure(dollars) + Length.R) + }) + + it("escapes each dollar outside code, so Teams reads no math", () => { + const markdown = "$x$ \\$y \\\\$z `$c$`\n```\n$$\n```\n$" + const sent = "\\$x\\$ \\$y \\\\\\$z `$c$`\n```\n$$\n```\n\\$" + assert.strictEqual(Render.textActivity(markdown).text, sent) + }) +}) diff --git a/packages/chat/teams/test/webhook.test.ts b/packages/chat/teams/test/webhook.test.ts new file mode 100644 index 00000000000..bc367ba4d8f --- /dev/null +++ b/packages/chat/teams/test/webhook.test.ts @@ -0,0 +1,264 @@ +/** + * The messaging endpoint through `layerWebhook`, with tokens signed by a + * key in a stubbed Bot Framework key set. + */ +import { modalMetadataLength } from "@effect/chat-teams/internal/adapter" +import * as Envelope from "@effect/chat-teams/internal/envelope" +import * as TeamsChat from "@effect/chat-teams/TeamsChat" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type { ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Context from "effect/Context" +import * as Digest from "effect/crypto/Digest" +import * as Mac from "effect/crypto/Mac" +import * as SecureRandom from "effect/crypto/SecureRandom" +import * as Signature from "effect/crypto/Signature" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as HttpRouter from "effect/http/HttpRouter" +import { Jwk, Jwt } from "effect/jose" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as Schema from "effect/Schema" + +const appId = "00000000-0000-0000-0000-0000000000a1" +const tenantId = "00000000-0000-0000-0000-0000000000b2" +const serviceUrl = "https://smba.trafficmanager.net/emea/" +const keysUrl = "https://login.botframework.com/v1/.well-known/keys" + +const crypto = Layer.mergeAll(Digest.layerWebCrypto, Mac.layerWebCrypto, Signature.layerWebCrypto).pipe( + Layer.provideMerge(SecureRandom.layerWebCrypto) +) + +const Claims = Schema.Struct({ + iss: Schema.String, + aud: Schema.String, + exp: Schema.Number, + serviceurl: Schema.String +}) + +const activity = (overrides?: Record) => ({ + type: "message", + id: "1800000000001", + timestamp: "2026-11-15T10:00:00.000Z", + serviceUrl, + channelId: "msteams", + from: { id: "29:user", aadObjectId: "user", name: "User" }, + recipient: { id: `28:${appId}`, name: "Bot" }, + conversation: { id: "a:direct", conversationType: "personal", tenantId }, + channelData: { tenant: { id: tenantId } }, + text: "hello", + ...overrides +}) + +const setup = Effect.fnUntraced(function*(options?: { + readonly jwksUri?: string + readonly teams?: Partial[0]> + readonly interactions?: ChatInteraction.Handler["Service"] +}) { + const pair = yield* Jwk.generateKeyPair({ algorithm: "RS256", kid: "k1" }) + const key = yield* Jwk.importPrivate(pair.privateKey, "RS256") + const token = (claimed = serviceUrl) => + Effect.map( + Jwt.sign( + Claims, + { + iss: "https://api.botframework.com", + aud: appId, + exp: Math.floor(Date.now() / 1000) + 600, + serviceurl: claimed + }, + key, + { kid: "k1" } + ), + Redacted.value + ) + const fetched: Array<{ readonly url: string; readonly redirect: RequestRedirect | undefined }> = [] + const http = HttpClient.make((request, url, _signal, fiber) => + Effect.sync(() => { + fetched.push({ + url: url.href, + redirect: Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit)?.redirect + }) + const body = url.href === keysUrl + ? { keys: [{ ...pair.publicKey, endorsements: ["msteams"] }] } + : url.pathname.endsWith("/openidconfiguration") + ? { jwks_uri: options?.jwksUri ?? keysUrl } + : {} + return HttpClientResponse.fromWeb(request, Response.json(body)) + }) + ) + const events: Array = [] + const app = TeamsChat.layerWebhook({ path: "/api/messages" }).pipe( + Layer.provide(TeamsChat.layer({ appId, tenantId, credentials: Redacted.make("secret"), ...options?.teams })), + Layer.provide(Chat.layer), + Layer.provide( + options?.interactions === undefined + ? Layer.empty + : Layer.succeed(ChatInteraction.Handler, options.interactions) + ), + Layer.provide(Layer.succeed(ChatHandler, { handle: (event) => Effect.sync(() => void events.push(event)) })), + Layer.provide(Layer.succeed(HttpClient.HttpClient, http)), + Layer.provide(crypto) + ) + const { dispose, handler } = HttpRouter.toWebHandler(app, { disableLogger: true }) + yield* Effect.addFinalizer(() => Effect.promise(() => dispose())) + const exchange = (body: unknown, authorization?: string) => + Effect.promise(() => + handler( + new Request("http://localhost/api/messages", { + method: "POST", + body: JSON.stringify(body), + headers: { + "content-type": "application/json", + ...(authorization === undefined ? {} : { authorization }) + } + }) + ) + ) + const send = (body: unknown, authorization?: string) => + Effect.map(exchange(body, authorization), (response) => response.status) + return { events, exchange, fetched, send, token } +}) + +describe("layerWebhook", () => { + it.effect("answers 403 without a valid token, and for a token bound to another serviceUrl", () => + Effect.gen(function*() { + const { events, send, token } = yield* setup() + assert.strictEqual(yield* send(activity()), 403) + assert.strictEqual(yield* send(activity(), "Bearer not.a.token"), 403) + const other = yield* token("https://smba.trafficmanager.net/amer/") + assert.strictEqual(yield* send(activity(), `Bearer ${other}`), 403) + const wrongTenant = activity({ conversation: { id: "a:direct", conversationType: "personal", tenantId: "x" } }) + assert.strictEqual(yield* send(wrongTenant, `Bearer ${yield* token()}`), 403) + assert.strictEqual(events.length, 0) + }).pipe(Effect.provide(crypto))) + + it.effect("hands a verified message to the ChatHandler, and drops a targeted one", () => + Effect.gen(function*() { + const { events, send, token } = yield* setup() + const bearer = `Bearer ${yield* token()}` + assert.strictEqual(yield* send(activity(), bearer), 200) + assert.strictEqual( + yield* send(activity({ id: "1800000000002", recipient: { id: `28:${appId}`, isTargeted: true } }), bearer), + 200 + ) + assert.strictEqual(events.length, 1) + assert.strictEqual(events[0]._tag, "MessageCreated") + }).pipe(Effect.provide(crypto))) + + it.effect("fetches the metadata and the key set with redirects off", () => + Effect.gen(function*() { + const { fetched, send, token } = yield* setup() + assert.strictEqual(yield* send(activity(), `Bearer ${yield* token()}`), 200) + const discovery = fetched.filter((request) => request.url.startsWith("https://login.botframework.com/")) + assert.deepStrictEqual(discovery.map((request) => request.url), [ + "https://login.botframework.com/v1/.well-known/openidconfiguration", + keysUrl + ]) + assert.isTrue(discovery.every((request) => request.redirect === "manual")) + }).pipe(Effect.provide(crypto))) + + it.effect("answers 503 for metadata whose jwks_uri is not a URL", () => + Effect.gen(function*() { + const { events, send, token } = yield* setup({ jwksUri: "not a URL" }) + assert.strictEqual(yield* send(activity(), `Bearer ${yield* token()}`), 503) + assert.strictEqual(events.length, 0) + }).pipe(Effect.provide(crypto))) +}) + +describe("dialogs through layerWebhook", () => { + const interactionKey = Redacted.make( + { kty: "oct", kid: "k1", k: "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc" } as const + ) + const channel = "19:abc@thread.tacv2" + // A click on a card posted as a channel post: the root of its own reply chain. + const clicked = `${channel};messageid=123` + const modal: ChatUi.Modal = { + _tag: "Modal", + id: "deploy", + title: "Deploy", + elements: [ + { _tag: "TextInput", id: "constructor", label: "Name", initial: "first" }, + { _tag: "TextInput", id: "note", label: "Note", initial: "draft" } + ] + } + + const open = Effect.fnUntraced(function*(opens: ChatUi.Modal) { + const submits: Array = [] + const refusals: Array = [] + const { exchange, token } = yield* setup({ + teams: { interactionKeys: [interactionKey], modalButtons: ["open"], unverified: { buttons: true, modals: true } }, + interactions: { + click: () => Effect.succeed(ChatInteraction.OpenModal.make({ modal: opens })), + submit: (event) => + Effect.sync(() => { + submits.push(event) + return ChatInteraction.errors({ note: "Too short" }) + }), + command: () => Effect.succeed(ChatInteraction.ack), + refused: (_, error) => Effect.sync(() => void refusals.push(error)) + } + }) + const bearer = `Bearer ${yield* token()}` + const invoke = (overrides: Record) => + exchange( + activity({ + type: "invoke", + conversation: { id: clicked, conversationType: "channel", tenantId }, + ...overrides + }), + bearer + ).pipe(Effect.flatMap((response) => Effect.promise((): Promise => response.json()))) + const envelope = yield* Envelope.make({ appId, tenantId, keys: [interactionKey] }) + const button = yield* envelope.signButton(channel, { id: "open" }) + const opened = yield* invoke({ name: "task/fetch", replyToId: "123", value: { data: { effect: button } } }) + const dialog: string | undefined = opened?.task?.value?.card?.content?.actions?.[0]?.data?.effect + const submit = (values: Record) => + invoke({ name: "task/submit", value: { data: { effect: dialog ?? null, ...values } } }) + return { dialog, refusals, submit, submits } + }) + + it.effect("opens a dialog only when its whole signed token fits modalMetadataLength", () => + Effect.gen(function*() { + // Short id and no metadata: the definition alone makes the token long. + const withText = (length: number): ChatUi.Modal => ({ + ...modal, + elements: [{ _tag: "Text", text: "x".repeat(length) }, ...modal.elements] + }) + const big = yield* open(withText(3_500)) + assert.isUndefined(big.dialog) + assert.strictEqual(big.refusals[0]?.reason._tag, "TooLarge") + + const small = yield* open(withText(1_000)) + assert.isString(small.dialog) + assert.isAtMost(small.dialog!.length, modalMetadataLength) + assert.strictEqual(small.refusals.length, 0) + }).pipe(Effect.provide(crypto))) + + it.effect("a submit keeps the thread and message of the click", () => + Effect.gen(function*() { + const { submit, submits } = yield* open(modal) + yield* submit({ constructor: "a", note: "b" }) + assert.strictEqual(submits.length, 1) + assert.deepStrictEqual(submits[0].thread, TeamsChat.thread(channel)) + assert.strictEqual(submits[0].messageId, "123") + }).pipe(Effect.provide(crypto))) + + it.effect("an error reply keeps what the user entered, and reads no inherited key", () => + Effect.gen(function*() { + const { submit } = yield* open(modal) + const reply = yield* submit({ constructor: "edited", note: "b" }) + const body: ReadonlyArray = reply.task.value.card.content.body + const inputs = body.filter((element) => element.type === "Input.Text") + assert.deepStrictEqual(inputs.map((input) => input.value), ["edited", "b"]) + const texts = body.filter((element) => element.type === "TextBlock").map((element) => element.text) + assert.deepStrictEqual(texts, ["Too short"]) + }).pipe(Effect.provide(crypto))) +}) diff --git a/packages/chat/teams/tsconfig.json b/packages/chat/teams/tsconfig.json new file mode 100644 index 00000000000..e2a8ca19a0d --- /dev/null +++ b/packages/chat/teams/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "references": [ + { "path": "../../effect" } + ] +} diff --git a/packages/chat/telegram/README.md b/packages/chat/telegram/README.md new file mode 100644 index 00000000000..9e8a2d8b926 --- /dev/null +++ b/packages/chat/telegram/README.md @@ -0,0 +1,122 @@ +# @effect/chat-telegram + +A [Telegram](https://telegram.org) adapter for `effect/chat`, over the Bot API. It includes a webhook endpoint, a long-polling loop, a pure update normaliser, and the address helpers. + +The live checks ran against production on 16 Nov 2026, and their results are below. Not verified: what a bystander sees of a command. Files, buttons and commands stay behind the `unverified` options. + +## Setup + +Create a bot with [@BotFather](https://t.me/BotFather) and keep its token. Pick one transport: a webhook, or polling for a long-running process. Telegram allows only one at a time. + +```ts +import * as TelegramChat from "@effect/chat-telegram/TelegramChat" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" + +const token = Redacted.make("") +const secretToken = Redacted.make("<32 to 256 characters of A-Za-z0-9_->") + +const Webhook = TelegramChat.layerWebhook({ path: "/telegram", secretToken }).pipe( + Layer.provideMerge(TelegramChat.layer({ token })) +) + +const Polling = TelegramChat.layerPolling().pipe( + Layer.provideMerge(TelegramChat.layer({ token })) +) +``` + +`TelegramChat.layer` needs `HttpClient` and the chat `Registry`. `TelegramChat.layerWebhook` also needs `HttpRouter` and a `ChatHandler`, and `TelegramChat.layerPolling` a `ChatHandler`. Clicks and commands go to `ChatInteraction.Handler` when one is in context. + +In a group, a bot in privacy mode receives commands, replies to its own messages and service messages, but not a plain `@bot` mention. To see mentions, turn privacy mode off in @BotFather or make the bot an admin. + +Register the webhook once with `TelegramChat.setWebhook({ url, secretToken })`, which sets exactly the `allowed_updates` the adapter reads. The adapter never calls it, or `deleteWebhook`, on its own. Polling fails to start with `InvalidRequest` and detail `conflict` while a webhook is set. + +`environment: "test"` uses Telegram's [test environment](https://core.telegram.org/bots/features#dedicated-test-environment). `apiUrl` points at a local Bot API server. + +## Capabilities + +By default the descriptor advertises `edit`, `delete`, `replyTo`, `download`, `getThread` and `getUser`, alongside `post`. Posts and edits are rich messages (`sendRichMessage`). `getUser` needs `in`, the chat to look the user up in. + +These capabilities are implemented but stay off until their live checks pass, on top of the base checks. Turn them on with the `unverified` option, at your own risk: + +| Option | Turns on | Live checks, besides the base ones | +| ---------- | ---------- | ---------------------------------- | +| `files` | `files` | 6 | +| `buttons` | `buttons` | 8 | +| `commands` | `commands` | 9 | + +An edit of a message posted with files drops its media (live check 6). + +Not supported in this version: `ephemeral`, `fetchMessage`, `history`, `opener`, `startThread`, selects, modals, native streams, reactions (outbound and as events), and typing. A private answer to a command is refused through `ChatInteraction.Handler.refused` with `Unsupported`, and is never posted in public. A `message_reaction` update goes to `onUnhandled`. + +Updates from business connections, a channel's direct messages and guest mode are never dispatched: they go to `onUnhandled`. A business chat's button is answered so the client stops waiting. + +## Security + +- The bot token is in the URL path of every Bot API call. The adapter turns off the HTTP client's own spans for these calls, writes its own span with the path as `/bot/{method}`, never follows a redirect, and keeps the token out of every error. +- The webhook needs the `X-Telegram-Bot-Api-Secret-Token` header, compared in constant time, before it reads the body. Server spans record no headers by default. An app that records them (`HttpMiddleware.TracerHeaderFilter`) must add `TelegramChat.secretHeader` to `Headers.CurrentRedactedNames`, keeping the names already there, in the layer it serves. `Layer.provide` on the webhook alone does not reach the server span, `Layer.provideMerge` does: + + ```ts + import * as Effect from "effect/Effect" + import * as Headers from "effect/http/Headers" + + const RedactSecret = Layer.effect( + Headers.CurrentRedactedNames, + Effect.gen(function*() { + return [...yield* Headers.CurrentRedactedNames, TelegramChat.secretHeader] + }) + ) + + const App = Webhook.pipe(Layer.provideMerge(RedactSecret)) + ``` +- Consumer Markdown is escaped so that it reaches no raw HTML, image, math, spoiler, highlight, or link outside `http`, `https` and `mailto`. A `tg://user` link stays only for users listed in `withPostOptions({ notify: { users } })`. Telegram honors only some backslash escapes, and it closes a link at an escaped `]`, so the other escapes become numeric character references, and a refused link or definition is broken at its `(` or its opening `[`. A kept link is sent with its destination verbatim, as Telegram reads it, and without its title. It is kept only when its `[` is on the same line, its label has no other bracket in it and no `]` comes right before it, and its destination has balanced parentheses at most three deep, the least CommonMark lets a parser support. A line whose first `[` meets a `]:` is kept as a definition only at the top level after a blank line, with a plain label that starts the line (no other bracket, code span, autolink or escaped `]`) and stays within 999 characters once escaped, and with nothing after its destination but a title. Otherwise its `[` is escaped. +- Where Markdown is ambiguous the escaping errs toward plain text: a table keeps its pipes only after a blank line or a heading; a `*` or `_` with space on both sides is escaped, as Telegram would open emphasis with it; and past 15 delimiters that could open emphasis, or 4,000 code spans, footnote references and `[` that could open a link or reference in one paragraph, or 700 footnote references in a message, the rest are shown as written. Telegram still pairs emphasis more loosely than CommonMark: `a* b*` is italic. +- A click is dispatched only when its `data` matches a button in Telegram's own copy of the message, and Telegram already refuses data that is not on the current keyboard (live check 8). Neither says that this user may press it. + +## Redelivery + +Telegram sends an update again until it gets a 200, clicks and commands included. Polling sees it again until the offset moves past it. The webhook answers 503 when the `ChatHandler` fails `NotAccepted` or passes `deadline` (10 seconds), and Telegram retries. Polling retries an update up to `maxAttempts` times, then logs it and moves on. + +Telegram delivers webhook updates in order: while one is refused, the ones after it wait. This was seen in one chat. Whether updates for other chats wait too was not tried, so assume a refused update can stall every chat, and keep `NotAccepted` for short outages. Telegram retries after about 0.3, 2, 4, 8, 16 and 32 seconds, keeping the `update_id`, and gives up on a webhook request after 60 seconds. + +Clicks and commands are different. Their handlers have a deadline of their own, and when it passes the update counts as answered: the webhook answers 200 and polling moves its offset. Telegram does not send it again. + +- A click handler has `clickDeadline` (2.5 seconds). Then the click is answered so the button stops spinning, and the handler keeps running, detached. If it fails or the process dies after that, nothing repeats the click. +- A command handler has `commandDeadline` (10 seconds). Then it is interrupted. If it had not committed, the command is lost. +- A handler that fails, rather than passing its deadline, is answered and refused through `ChatInteraction.Handler.refused`, not redelivered. + +A handler whose work must not be lost or done twice keys it with `TelegramChat.deliveryKey(event.raw, { botId })`. The adapter keeps no store, so this is the consumer's to do: + +1. In one transaction, insert the key under a unique constraint, together with a work item that a worker picks up, also after a crash. +2. Return only after the commit: success from the `ChatHandler`, `Ack` for a click, `Defer` for a command. The worker does the work, and for a command sends the `followUp`. +3. When the insert hits the constraint, the work is already safe: answer the same way at once. A repeated command answers `Defer`, never `Ack`, which would turn it into `MessageCreated`. + +A crash before the commit leaves nothing, and the repeat does the work. A concurrent repeat waits on the constraint for the first transaction. This holds only while the transaction commits within the deadline above: for clicks and commands a late commit is not repeated. External side effects, such as a payment, still need their own idempotency key (the delivery key works). Nothing here gives exactly-once effects. + +## Live checks + +Checks 1 to 16 ran on production on 16 Nov 2026, with a bot, a second non-admin bot, one person and a forum supergroup. That also covers check 17, except for check 9's bystander. `test/live.test.ts` runs the conformance suite against the Bot API only when all of `TELEGRAM_LIVE_BOT_TOKEN`, `TELEGRAM_LIVE_BOT2_TOKEN` and `TELEGRAM_LIVE_CHAT` are set, so CI skips it. + +1. Rich messages render on macOS desktop and mobile clients. Not seen: Web, Android and older clients. +2. The rich-Markdown dialect, the escapes Telegram honors, and its readings of links, definitions and emphasis. The escaping follows what it found. +3. The limits: 32,768 rendered characters (non-ASCII text counts less), 500 blocks with a list counting one per item, 16 levels of nesting, 20 table columns, about 4,387 inline nodes per paragraph and about 773 references to one footnote. A code block is one block. `measure` stays under each of these. +4. No link preview on rich posts or edits. +5. A plain `@user` notifies nobody, and an allowed `tg://user` link is a mention. +6. Rich-message media over multipart `attach://`: up to 50 files in one message, 51 is refused with 413; 10 MB files; file names and MIME types kept, except that Telegram turns some characters outside Latin-1, such as `–`, into spaces. An edit drops the media. A download through the adapter is byte for byte. +7. Webhook redelivery after a 503 or a dropped connection, as described under Redelivery. Not run: the polling side, and whether a refused update holds back other chats. +8. Callback queries carry Telegram's copy of `reply_markup`, which an edit replaces. Telegram keeps at most 100 buttons and 8 per row, and drops the rest without an error. A click answered at the 2.5-second deadline stops spinning about 4 seconds after the tap. Telegram itself refuses, with `DATA_INVALID`, a click whose `data` is not on the message's current keyboard: forged, tampered, from another message, or from a keyboard an edit replaced. The adapter's own check is a second layer. +9. Commands, with the author only. A typed command and one picked from the menu arrive alike. In a group, a command registered with `is_ephemeral` arrives with `message_id` 0, an `ephemeral_message_id` and `receiver_user`, and goes only to the command handler; in a private chat it is an ordinary command. A private answer is refused with `Unsupported` and nothing is posted. A public follow-up within 15 seconds replies to the command, and later fails `NotFound` with `interaction_expired`. Not tried: a bystander. +10. The error responses behind the error mapping. +11. Forum topics and the General topic. Topics in private chats were not tried. +12. Reaction updates reach an admin bot that asks for `message_reaction`, which the adapter does not. Each carries the user's whole set before and after, as `old_reaction` and `new_reaction`: add, change and remove each give one update. They normalise to no event. Not tried: custom and paid emoji, and `actor_chat`. +13. Edit pacing: one edit every 3 seconds in a group gets no 429. Back-to-back edits got one after 19, with `retry_after` 43. +14. `getChatMember` as a non-admin works for members and non-members, and fails `NotFound` for another bot. +15. A plain `@bot` mention does not reach a bot in privacy mode. A reply does. +16. Inbound rich messages: a bot never receives another bot's messages, so they arrive only as a reply target, with `rich_message` and no `text`. +17. A production smoke check of 1, 4, 5, 9, 13 and 15: all ran, except check 9's bystander. + +## Links + +- [Website](https://effect.website): documentation, guides, and news. +- [Discord](https://discord.gg/effect-ts): ask questions, share what you're building, and talk to the core team. +- [Issues](https://github.com/Effect-TS/effect/issues): bug reports and feature requests. diff --git a/packages/chat/telegram/package.json b/packages/chat/telegram/package.json new file mode 100644 index 00000000000..462ef4c2c78 --- /dev/null +++ b/packages/chat/telegram/package.json @@ -0,0 +1,73 @@ +{ + "name": "@effect/chat-telegram", + "version": "4.1.4", + "type": "module", + "license": "MIT", + "description": "The Telegram Bot API adapter for effect/chat", + "homepage": "https://effect.website", + "repository": { + "type": "git", + "url": "https://github.com/Effect-TS/effect.git", + "directory": "packages/chat/telegram" + }, + "bugs": { + "url": "https://github.com/Effect-TS/effect/issues" + }, + "tags": [ + "typescript", + "chat", + "telegram" + ], + "keywords": [ + "typescript", + "chat", + "telegram" + ], + "sideEffects": [], + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts", + "./*": "./src/*.ts", + "./internal/*": null, + "./index": null, + "./*/index": null + }, + "files": [ + "src/**/*.ts", + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map", + "AGENTS.md", + "CLAUDE.md", + "ai-docs/**/*" + ], + "publishConfig": { + "access": "public", + "provenance": true, + "exports": { + "./package.json": "./package.json", + ".": "./dist/index.js", + "./*": "./dist/*.js", + "./internal/*": null, + "./index": null, + "./*/index": null + } + }, + "scripts": { + "codegen": "effect-utils codegen", + "build": "tsc -b tsconfig.json && pnpm babel", + "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps", + "check": "tsc -b tsconfig.json" + }, + "devDependencies": { + "@types/mdast": "^4.0.4", + "effect": "workspace:^", + "mdast-util-from-markdown": "^2.0.2", + "mdast-util-gfm": "^3.1.0", + "micromark-extension-gfm": "^3.0.0" + }, + "peerDependencies": { + "effect": "workspace:^" + } +} diff --git a/packages/chat/telegram/src/TelegramBotApi.ts b/packages/chat/telegram/src/TelegramBotApi.ts new file mode 100644 index 00000000000..41a9b9a5b1f --- /dev/null +++ b/packages/chat/telegram/src/TelegramBotApi.ts @@ -0,0 +1,282 @@ +/** + * The minimal Bot API client the Telegram adapter is built on. It is public, + * so a consumer can call a method the adapter has no op for, with the same + * error mapping, the same rule for writes and the same care for the token. + * + * **Details** + * + * The bot token is part of every request's path, and `HttpClient` records + * the path of a request in its span and in its errors. So the client's own + * span is turned off for every request made here, and each call gets a span + * `TelegramBotApi.{method}` whose `url.path` is `/bot/{method}`. + * Every failure becomes a `ChatError` whose `cause` holds no request, + * response or URL. + * + * @stability unstable + * @since 4.1.5 + */ +import { type File, retrySafe } from "effect/chat/ChatAdapter" +import type { ChatError, ChatErrorReason } from "effect/chat/ChatError" +import { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import type * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import * as Address from "./internal/address.ts" +import * as Errors from "./internal/errors.ts" + +/** + * Which Telegram environment a bot lives in. The test environment has its + * own accounts and bots, and its methods sit under `/bot/test/`. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Environment = "production" | "test" + +/** + * The cause of a `NotFound` with detail `chat_migrated`: the group became + * a supergroup, which has a new chat id. The adapter does not follow it, + * since the consumer is the one that stores the old address. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class TelegramChatMigrated extends Schema.TaggedError( + "@effect/chat-telegram/TelegramBotApi/TelegramChatMigrated" +)("TelegramChatMigrated", { + thread: ChatThread +}) { + override get message(): string { + return "The Telegram group became a supergroup with a new address" + } +} + +/** + * Options for one Bot API call. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface CallOptions { + /** + * The call changes something. It is never repeated once the request left, + * and a failure that may have landed is `uncertain`. + */ + readonly write?: boolean | undefined + /** Files sent as parts of a `multipart/form-data` body, each under its `field`. */ + readonly files?: ReadonlyArray<{ readonly field: string; readonly file: File }> | undefined + /** Retry `retry` failures, as `ChatAdapter.retrySafe` does. Default true. */ + readonly retry?: boolean | undefined +} + +/** + * The Bot API client, with the bot's token. + * + * **Details** + * + * - `call` posts `params` to a method and succeeds with its `result`. + * Objects in `params` are sent as JSON; with `files` the body is + * multipart and each object is a JSON string part. + * - An answer with `ok: false` fails with the reason for its `error_code` + * and `parameters`. Its `detail` is a fixed string, never Telegram's + * description, which can quote the message. A `migrate_to_chat_id` gives + * `NotFound` with detail `chat_migrated` and a `TelegramChatMigrated` + * cause. + * - `file` streams a file by its `file_path` from `getFile`, failing + * `TooLarge` once more than `maxBytes` arrive. Redirects are refused. + * - `botId` is the numeric id in front of the token's colon. + * - The error's `op` is the method. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class TelegramBotApi extends Context.Service> | undefined, + options?: CallOptions | undefined + ) => Effect.Effect + readonly file: (filePath: string, options: { readonly maxBytes: number }) => Stream.Stream +}>()("@effect/chat-telegram/TelegramBotApi") {} + +/** + * The cloud Bot API server. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const defaultApiUrl = "https://api.telegram.org" + +const methodName = /^[A-Za-z]{1,64}$/ +const filePath = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*$/ + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +const formValue = (value: unknown): string => typeof value === "string" ? value : JSON.stringify(value) + +/** + * Builds the client from an `HttpClient` and a bot token. + * + * **Details** + * + * Fails `Unauthenticated` with detail `invalid_token` when the token has + * no numeric bot id in front of a colon. `apiUrl` is for a local Bot API + * server and is privileged configuration. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const make = Effect.fnUntraced(function*(options: { + readonly token: Redacted.Redacted + readonly environment?: Environment | undefined + readonly apiUrl?: string | undefined +}) { + const http = yield* HttpClient.HttpClient + const environment = options.environment ?? "production" + const apiUrl = (options.apiUrl ?? defaultApiUrl).replace(/\/+$/, "") + const botId = /^(\d{1,20}):[A-Za-z0-9_-]+$/.exec(Redacted.value(options.token))?.[1] + if (botId === undefined) { + return yield* Effect.fail(Errors.chatError("layer", Errors.unauthenticatedToken)) + } + const suffix = environment === "test" ? "/test" : "" + const methodUrl = (method: string) => `${apiUrl}/bot${Redacted.value(options.token)}${suffix}/${method}` + const fileUrl = (path: string) => `${apiUrl}/file/bot${Redacted.value(options.token)}${suffix}/${path}` + + // Redirects are never followed: the next hop would see the token in the path. + const send = (request: HttpClientRequest.HttpClientRequest, op: string, write: boolean) => + Effect.flatMap(Effect.serviceOption(FetchHttpClient.RequestInit), (init) => + http.execute(request).pipe( + Effect.provideService(HttpClient.TracerDisabledWhen, () => true), + Effect.provideService(FetchHttpClient.RequestInit, { + ...(init._tag === "Some" ? init.value : {}), + redirect: "manual" + }), + Effect.mapError((error) => Errors.chatError(op, Errors.transport(write, error.reason._tag))) + )) + + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + + const decode = (op: string, write: boolean, response: HttpClientResponse.HttpClientResponse) => + response.json.pipe( + Effect.matchEffect({ + onFailure: () => + response.status >= 200 && response.status < 300 + ? fail(op, Errors.invalidResponse(write, "invalid_json")) + : fail(op, Errors.reasonForApiError({ code: response.status, description: "" }, { write })), + onSuccess: (body): Effect.Effect => { + if (isObject(body) && body.ok === true && "result" in body) return Effect.succeed(body.result as Schema.Json) + const error = Errors.apiError(body, response.status) ?? + (response.status >= 200 && response.status < 300 + ? undefined + : { code: response.status, description: "" }) + if (error === undefined) return fail(op, Errors.invalidResponse(write, "invalid_answer")) + const cause = error.migrateTo === undefined + ? undefined + : new TelegramChatMigrated({ thread: Address.thread(error.migrateTo) }) + return fail(op, Errors.reasonForApiError(error, { write, cause })) + } + }) + ) + + const once = (method: string, params: Readonly>, callOptions: CallOptions) => { + const write = callOptions.write === true + let request = HttpClientRequest.post(methodUrl(method)) + if (callOptions.files !== undefined && callOptions.files.length > 0) { + const form = new FormData() + for (const [key, value] of Object.entries(params)) { + if (value !== undefined) form.append(key, formValue(value)) + } + for (const { field, file } of callOptions.files) { + form.append( + field, + new Blob([file.content as Uint8Array], { type: file.contentType }), + file.name + ) + } + request = HttpClientRequest.bodyFormData(request, form) + } else { + request = HttpClientRequest.bodyJsonUnsafe(request, params) + } + return Effect.flatMap(send(request, method, write), (response) => decode(method, write, response)).pipe( + Effect.withSpan(`TelegramBotApi.${method}`, { + kind: "client", + attributes: { + "http.request.method": "POST", + "url.path": `/bot${suffix}/${method}`, + "telegram.method": method + } + }) + ) + } + + const call: TelegramBotApi["Service"]["call"] = (method, params = {}, callOptions = {}) => { + if (!methodName.test(method)) return fail(method, Errors.invalidRequest("invalid_method")) + const effect = once(method, params, callOptions) + return callOptions.retry === false ? effect : retrySafe(effect) + } + + const file: TelegramBotApi["Service"]["file"] = (path, { maxBytes }) => + Stream.unwrap( + Effect.gen(function*() { + const op = "download" + if (!filePath.test(path) || path.split("/").some((segment) => segment === "." || segment === "..")) { + return yield* fail(op, Errors.invalidResponse(false, "untrusted_file_path")) + } + const url = fileUrl(path) + const response = yield* send(HttpClientRequest.get(url), op, false) + if (response.status >= 300 && response.status < 400) { + return yield* fail(op, Errors.invalidResponse(false, "redirect")) + } + if (response.url !== url) return yield* fail(op, Errors.invalidResponse(false, "redirect")) + if (response.status < 200 || response.status >= 300) { + return yield* fail(op, Errors.reasonForApiError({ code: response.status, description: "" }, { write: false })) + } + const tooLarge = Errors.chatError(op, Errors.tooLarge("files", "max_bytes")) + const length = Number(response.headers["content-length"]) + if (Number.isFinite(length) && length > maxBytes) return yield* Effect.fail(tooLarge) + let received = 0 + return response.stream.pipe( + Stream.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag))), + Stream.mapEffect((chunk) => { + received += chunk.length + return received > maxBytes ? Effect.fail(tooLarge) : Effect.succeed(chunk) + }) + ) + }).pipe( + Effect.withSpan("TelegramBotApi.file", { + kind: "client", + attributes: { "http.request.method": "GET", "url.path": "/file/bot" } + }) + ) + ) + + return TelegramBotApi.of({ botId, environment, call, file }) +}) + +/** + * The client as a layer. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly token: Redacted.Redacted + readonly environment?: Environment | undefined + readonly apiUrl?: string | undefined +}): Layer.Layer => Layer.effect(TelegramBotApi, make(options)) diff --git a/packages/chat/telegram/src/TelegramChat.ts b/packages/chat/telegram/src/TelegramChat.ts new file mode 100644 index 00000000000..24b47981c3f --- /dev/null +++ b/packages/chat/telegram/src/TelegramChat.ts @@ -0,0 +1,527 @@ +/** + * The Telegram adapter for `effect/chat`, over the Bot API: the `Registry` + * layer, the webhook and polling transports, the pure normaliser and the + * address helpers. + * + * **Details** + * + * - Raw REST over `HttpClient`, through `TelegramBotApi`, which keeps the + * bot token out of spans and errors. + * - Posts and edits are rich messages (`sendRichMessage`, + * `editMessageText` with `rich_message`), in Markdown escaped so that + * consumer text reaches no raw HTML, image, math, spoiler or non-web + * link. + * - The base live checks ran against production. Files, buttons and + * commands need further checks, so they are behind `unverified` + * options, off by default. + * - There is no `ephemeral`: a private answer to a command is refused + * through `Handler.refused`, never posted. `fetchMessage`, `history`, + * `opener`, `startThread`, selects, modals, native streams and reaction + * events are not in this version. + * + * @stability unstable + * @since 4.1.5 + */ +import { Registry } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import type { ChatEvent } from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import type * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ParseMention } from "effect/chat/ChatTest" +import type { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import { dual } from "effect/Function" +import type * as HttpClient from "effect/http/HttpClient" +import * as HttpRouter from "effect/http/HttpRouter" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Adapter from "./internal/adapter.ts" +import * as Address from "./internal/address.ts" +import * as Commands from "./internal/commands.ts" +import * as Errors from "./internal/errors.ts" +import * as Normalize from "./internal/normalize.ts" +import { CurrentPostOptions } from "./internal/options.ts" +import * as Polling from "./internal/polling.ts" +import * as Receive from "./internal/receive.ts" +import * as Webhook from "./internal/webhook.ts" +import * as TelegramBotApi from "./TelegramBotApi.ts" + +/** + * Capabilities that are built but stay off until their live checks pass. + * + * **Details** + * + * - `files`: rich-message media sent as multipart `attach://` parts, + * up to 10 per post and 50 MB each. + * - `buttons`: inline keyboards. A click is dispatched only when its + * `data` matches a button in Telegram's copy of the message. + * - `commands`: messages that start with a `bot_command` for this bot. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Unverified { + readonly files?: boolean | undefined + readonly buttons?: boolean | undefined + readonly commands?: boolean | undefined +} + +/** + * What became of one update given to `receive`. + * + * **Details** + * + * - `accepted`: its event was accepted, or its click or command was + * answered, refused or not. + * - `unhandled`: it gave nothing and went to `onUnhandled`. + * - `notAccepted`: the `ChatHandler` failed `NotAccepted` or passed + * `deadline`. + * - `invalid`: it is not an update, or it should give an event and could + * not be read. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Outcome = "accepted" | "unhandled" | "notAccepted" | "invalid" + +/** + * Options for running updates. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface ReceiveOptions { + /** Updates that give no event, the excluded contexts among them. */ + readonly onUnhandled?: ((update: Schema.Json) => Effect.Effect) | undefined + /** How long `ChatHandler.handle` has before the update is `notAccepted`. Default 10 seconds. */ + readonly deadline?: Duration.Input | undefined + /** + * How long a click handler has before the click is answered. Default 2.5 + * seconds. The handler then keeps running, detached, and the update is + * `accepted`: Telegram does not send it again. + */ + readonly clickDeadline?: Duration.Input | undefined + /** + * How long a command handler has. Default 10 seconds. The handler is then + * interrupted, and the update is `accepted`: Telegram does not send it + * again. + */ + readonly commandDeadline?: Duration.Input | undefined +} + +/** + * The Telegram adapter's own service. + * + * **Details** + * + * - `botId` is the numeric id in front of the token's colon. + * - `receive` runs one update, already authenticated and parsed, as both + * transports do, for a consumer's own transport. It needs a + * `ChatHandler`. Clicks and commands go to `ChatInteraction.Handler` + * when there is one in context: without it a click is answered and + * dropped, and a command is an ordinary message. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class TelegramChat extends Context.Service Effect.Effect +}>()("@effect/chat-telegram/TelegramChat") {} + +/** + * Registers the Telegram adapter in the `Registry` and provides the + * `TelegramChat` and `TelegramBotApi` services. + * + * **Details** + * + * - The descriptor has `edit`, `delete`, `replyTo`, `download`, + * `getThread` and `getUser`, beside `post`, and whatever `unverified` + * turns on. + * - `environment: "test"` uses Telegram's test environment. + * - `apiUrl` is for a local Bot API server and is privileged configuration. + * - Fails `Unauthenticated` when the token has no bot id. The token is not + * checked against Telegram here; the first call does that. + * - The bot's username, which commands and `@` mentions need, is read with + * `getMe` on the first message and kept. While it can't be read, a + * message update is `invalid`, and Telegram sends it again. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly token: Redacted.Redacted + readonly environment?: TelegramBotApi.Environment | undefined + readonly apiUrl?: string | undefined + readonly unverified?: Unverified | undefined +}): Layer.Layer => + Layer.effect( + TelegramChat, + Effect.gen(function*() { + const api = yield* TelegramBotApi.TelegramBotApi + const registry = yield* Registry + const capabilities = Adapter.capabilitiesOf(options.unverified ?? {}) + const { adapter, answer } = Adapter.make({ api, capabilities }) + yield* registry.register(adapter) + let username: string | undefined + const env: Receive.Environment = { + api, + capabilities, + answer, + username: Effect.suspend(() => + username !== undefined ? Effect.succeed(username) : api.call("getMe").pipe( + Effect.flatMap((me) => { + const name = Normalize.isObject(me) ? Normalize.string(me, "username") : undefined + if (name === undefined) { + return Effect.fail(Errors.chatError("getMe", Errors.invalidResponse(false, "missing_username"))) + } + username = name + return Effect.succeed(name) + }) + ) + ) + } + return TelegramChat.of({ + botId: api.botId, + environment: api.environment, + receive: Effect.fnUntraced(function*(update, receiveOptions) { + const handler = yield* ChatHandler + return yield* Receive.receive(update, env, receiveOptions ?? {}, handler.handle) + }) + }) + }) + ).pipe(Layer.provideMerge(TelegramBotApi.layer(options))) + +/** + * The header Telegram sends the webhook secret in. An app that records + * server request headers must add it to `Headers.CurrentRedactedNames`, + * keeping the names already there, in the layer it serves: + * `Layer.provideMerge` reaches the server span, `Layer.provide` on the + * webhook alone does not. The README has the setup. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const secretHeader: string = Webhook.secretHeader + +/** + * `POST {path}`: the bot's webhook. + * + * **Details** + * + * - The `X-Telegram-Bot-Api-Secret-Token` header must equal + * `secretToken`, compared in constant time. Otherwise the answer is 401 + * and the body is not read. The layer fails unless `secretToken` is 32 to + * 256 characters of `A-Za-z0-9_-`. + * - 413 past `maxBodyBytes` (1 MiB by default), 400 for a body that is not + * an update. + * - The update goes through `receive`: 200 for `accepted` and + * `unhandled`, 503 for `notAccepted` and `invalid`, which Telegram + * sends again. A click or command is answered 200 after its reply + * attempt. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerWebhook = ( + options: ReceiveOptions & { + readonly path: `/${string}` + readonly secretToken: Redacted.Redacted + readonly maxBodyBytes?: number | undefined + } +): Layer.Layer => + HttpRouter.use(Effect.fnUntraced(function*(router) { + const secret = Redacted.value(options.secretToken) + if (!Webhook.validSecret(secret)) { + return yield* Effect.fail(Errors.chatError("layerWebhook", Errors.invalidRequest("invalid_secret_token"))) + } + const telegram = yield* TelegramChat + const context = yield* Effect.context() + const encoded = new TextEncoder().encode(secret) + yield* router.add("POST", options.path, (request) => + Webhook.handleRequest(request, { + secret: encoded, + maxBodyBytes: options.maxBodyBytes ?? 1024 * 1024, + receive: (update) => Effect.provideContext(telegram.receive(update, options), context) + })) + })) + +/** + * Long polling with `getUpdates`, for a long-running process. + * + * **Details** + * + * - Updates run one at a time, in order, through `receive`. An update is + * confirmed only after it ran, so a process that dies part way sees it + * again. + * - `notAccepted` and `invalid` are retried up to `maxAttempts` times (3 + * by default) with a backoff, then logged as an error and skipped: + * `getUpdates` is one queue, and retrying forever would stall the bot. + * - The layer fails with `InvalidRequest` and detail `conflict` when + * Telegram answers 409 at start: a webhook is set, or another process + * polls. It never deletes a webhook itself. A later 409 stops polling + * with an error log. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerPolling = (options: ReceiveOptions & { + /** The long-poll timeout. Default 30 seconds. */ + readonly timeout?: Duration.Input | undefined + readonly maxAttempts?: number | undefined +} = {}): Layer.Layer => + Layer.effectDiscard(Effect.gen(function*() { + const telegram = yield* TelegramChat + const api = yield* TelegramBotApi.TelegramBotApi + const context = yield* Effect.context() + const pollingOptions: Polling.Options = { + api, + timeout: options.timeout === undefined ? Duration.seconds(30) : Duration.fromInputUnsafe(options.timeout), + maxAttempts: Math.max(1, options.maxAttempts ?? 3), + receive: (update) => Effect.provideContext(telegram.receive(update, options), context) + } + const first = yield* Polling.getUpdates(api, undefined, Duration.zero).pipe(Effect.mapError(Polling.conflict)) + yield* Effect.forkScoped(Polling.loop(first, pollingOptions)) + })) + +/** + * Registers the webhook with exactly the `allowed_updates` the adapter + * reads. It is never called by the adapter itself. + * + * @stability unstable + * @category webhooks + * @since 4.1.5 + */ +export const setWebhook = (options: { + readonly url: string + readonly secretToken: Redacted.Redacted + readonly maxConnections?: number | undefined + readonly dropPendingUpdates?: boolean | undefined +}): Effect.Effect => + Effect.gen(function*() { + const secret = Redacted.value(options.secretToken) + if (!Webhook.validSecret(secret)) { + return yield* Effect.fail(Errors.chatError("setWebhook", Errors.invalidRequest("invalid_secret_token"))) + } + const api = yield* TelegramBotApi.TelegramBotApi + yield* api.call("setWebhook", { + url: options.url, + secret_token: secret, + allowed_updates: Receive.allowedUpdates, + ...(options.maxConnections === undefined ? {} : { max_connections: options.maxConnections }), + ...(options.dropPendingUpdates === undefined ? {} : { drop_pending_updates: options.dropPendingUpdates }) + }, { write: true }) + }) + +/** + * The `setMyCommands` payload for a group's commands. + * + * **Details** + * + * - Fails `InvalidRequest` with detail `invalid_command_names` when a + * command id is not `[a-z0-9_]{1,32}`. The portable rule allows `-`; + * Telegram does not. + * - An `argument` has no Telegram counterpart and is appended to the + * description. + * - Commands are registered as ordinary, not ephemeral: this version has + * no `ephemeral`. + * + * @stability unstable + * @category commands + * @since 4.1.5 + */ +export const commandRequests = ( + group: ChatInteraction.Group +): Effect.Effect< + { readonly commands: ReadonlyArray<{ readonly command: string; readonly description: string }> }, + ChatError +> => { + const result = Commands.botCommands(group) + return "invalid" in result + ? Effect.fail(Errors.chatError("commandRequests", Errors.invalidRequest("invalid_command_names"))) + : Effect.succeed(result) +} + +/** + * Sends a group's commands with `setMyCommands`. + * + * @stability unstable + * @category commands + * @since 4.1.5 + */ +export const setCommands = ( + group: ChatInteraction.Group, + options?: { readonly scope?: Schema.Json | undefined; readonly languageCode?: string | undefined } +): Effect.Effect => + Effect.gen(function*() { + const payload = yield* commandRequests(group) + const api = yield* TelegramBotApi.TelegramBotApi + yield* api.call("setMyCommands", { + commands: payload.commands, + ...(options?.scope === undefined ? {} : { scope: options.scope }), + ...(options?.languageCode === undefined ? {} : { language_code: options.languageCode }) + }, { write: true }) + }) + +/** + * The address of a chat, or of a forum topic in it. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const thread: (chatId: string | number, messageThreadId?: string | number | undefined) => ChatThread = + Address.thread + +/** + * The parts of a Telegram address, or `None` for another platform's or a + * malformed one. + * + * @stability unstable + * @category destructors + * @since 4.1.5 + */ +export const parts: (thread: ChatThread) => Option.Option<{ + readonly chatId: string + readonly messageThreadId?: string | undefined +}> = Address.parts + +/** + * What the normalisers need to know about the bot. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface NormalizeContext { + readonly botId: string + /** The bot's username without `@`, from `getMe`. */ + readonly botUsername: string + readonly environment?: TelegramBotApi.Environment | undefined +} + +/** + * The normaliser, for a consumer's own transport. It checks no secret: + * that is the transport's job. + * + * **Details** + * + * - A `message` or `channel_post` with text, a caption, a file or rich + * content gives a `MessageCreated`. `text` is verbatim, and empty for + * an inbound rich message, whose content is only in `raw`. + * - `text_mention` entities are mentions by id. An `@username` is a + * mention only when it names the bot. + * - The author is `sender_chat` when there is one, else `from`. + * - A private (ephemeral) message, a service message, and anything from a + * business connection, a channel's direct messages or guest mode give + * none, as does every other update. Commands are messages here too: + * `receive` is what routes them. + * - `raw` is the update. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalize: (update: unknown, context: NormalizeContext) => ReadonlyArray = Normalize.normalize + +/** + * The click or command of an update, as `receive` would dispatch it. + * + * **Details** + * + * A click is given only when its `data` is the `callback_data` of a button + * in Telegram's own copy of the message. That proves the bot put the button + * there, not that it is still current or that this user may press it. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalizeInteraction = ( + update: unknown, + context: NormalizeContext +): Option.Option => { + if (!Normalize.isObject(update)) return Option.none() + return Option.fromUndefinedOr(Receive.clickEvent(update) ?? Receive.commandEvent(update, context)) +} + +/** + * The delivery key of an update, or of an event or interaction whose `raw` + * is one: `{botId}.{update_id}.{date}`, with `test.` in front in the test + * environment. Telegram sends an update again until it gets a 200 (or an + * offset moves past it), clicks and commands included, so a handler whose + * work must not be lost or run twice commits this key and a recoverable + * work item in one transaction before it returns. A click or command + * past its deadline counts as answered and is not sent again, so that + * holds only for a commit within the deadline. The README has the recipe. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const deliveryKey = ( + raw: unknown, + context: { readonly botId: string; readonly environment?: TelegramBotApi.Environment | undefined } +): Option.Option => Option.fromUndefinedOr(Normalize.deliveryId(raw, context)) + +/** + * Reads a mention for `ChatTest`. A mention's text is a display name or an + * `@username`, neither of which holds the id, so this is always `None`; + * a message's `mentions` come from its entities. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const parseMention: ParseMention = () => Option.none() + +/** + * Telegram options for every write inside an effect. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface TelegramPostOptions { + /** + * Users whose `[name](tg://user?id=…)` links in the Markdown stay links. + * Any other `tg://` link becomes text. Default: none. + */ + readonly notify?: { readonly users?: ReadonlyArray | undefined } | undefined + /** Sends posts with `disable_notification`. */ + readonly silent?: boolean | undefined +} + +/** + * Scoped Telegram options for every write inside `effect`. Nested calls + * merge, the inner one winning field by field. Answers to commands and + * follow-ups ignore them. + * + * @stability unstable + * @category combinators + * @since 4.1.5 + */ +export const withPostOptions: { + (options: TelegramPostOptions): (effect: Effect.Effect) => Effect.Effect + (effect: Effect.Effect, options: TelegramPostOptions): Effect.Effect +} = dual( + 2, + (effect: Effect.Effect, options: TelegramPostOptions): Effect.Effect => + Effect.updateService(effect, CurrentPostOptions, (current) => ({ ...current, ...options })) +) diff --git a/packages/chat/telegram/src/index.ts b/packages/chat/telegram/src/index.ts new file mode 100644 index 00000000000..7d3abb55339 --- /dev/null +++ b/packages/chat/telegram/src/index.ts @@ -0,0 +1,18 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as TelegramBotApi from "./TelegramBotApi.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as TelegramChat from "./TelegramChat.ts" diff --git a/packages/chat/telegram/src/internal/actions.ts b/packages/chat/telegram/src/internal/actions.ts new file mode 100644 index 00000000000..07befe4091a --- /dev/null +++ b/packages/chat/telegram/src/internal/actions.ts @@ -0,0 +1,97 @@ +import type { ChatErrorReason } from "effect/chat/ChatError" +import type * as ChatUi from "effect/chat/ChatUi" +import * as Errors from "./errors.ts" + +/** The adapter's own cap, as Telegram publishes no button count. @internal */ +export const actionsPerMessage = 100 + +/** `callback_data` is 1 to 64 bytes, less the separator. @internal */ +export const actionDataLength = 63 + +/** Between a button's id and its value in `callback_data`. @internal */ +export const separator = "\u001f" + +const encoder = new TextEncoder() + +const bytes = (s: string) => encoder.encode(s).length + +/** A button's `callback_data`: its id, and its value after the separator when it has one. @internal */ +export const callbackData = (button: { readonly id: string; readonly value?: string | undefined }): string => + button.value === undefined ? button.id : button.id + separator + button.value + +/** The id and value of `callback_data`. @internal */ +export const parseData = (data: string): { readonly id: string; readonly value?: string | undefined } => { + const at = data.indexOf(separator) + return at === -1 ? { id: data } : { id: data.slice(0, at), value: data.slice(at + 1) } +} + +/** A label short enough to share a row with two others. */ +const shortLabel = 12 + +const perRow = 3 + +type KeyboardButton = + | { readonly text: string; readonly callback_data: string; readonly style?: string } + | { readonly text: string; readonly url: string } + +/** + * The inline keyboard of a message's actions, or the reason it can't be + * sent: a select (`Unsupported`), too many elements, data past 63 bytes, + * an id holding the separator, or a link that is not http or https. + * + * @internal + */ +export const keyboard = ( + actions: ReadonlyArray +): { readonly inline_keyboard: ReadonlyArray> } | ChatErrorReason => { + if (actions.some((action) => action._tag === "Select")) return Errors.unsupported("selects") + if (actions.length > actionsPerMessage) return Errors.tooLarge(undefined, "too_many_actions") + const buttons: Array = [] + for (const action of actions) { + if (action._tag === "Button") { + if (action.id.includes(separator)) return Errors.invalidRequest("invalid_action_id") + if (bytes(action.id) + bytes(action.value ?? "") > actionDataLength) { + return Errors.tooLarge(undefined, "action_data_too_long") + } + buttons.push({ + text: action.label, + callback_data: callbackData(action), + ...(action.style === "primary" || action.style === "danger" ? { style: action.style } : {}) + }) + } else if (action._tag === "LinkButton") { + if (!/^https?:\/\//i.test(action.url)) return Errors.invalidRequest("untrusted_url") + buttons.push({ text: action.label, url: action.url }) + } + } + const rows: Array> = [] + let row: Array = [] + for (const button of buttons) { + const short = button.text.length <= shortLabel + if (!short || row.length === perRow || row.some((b) => b.text.length > shortLabel)) { + if (row.length > 0) rows.push(row) + row = [] + } + row.push(button) + } + if (row.length > 0) rows.push(row) + return { inline_keyboard: rows } +} + +/** Every `callback_data` in a message's `reply_markup`. @internal */ +export const keyboardData = (replyMarkup: unknown): ReadonlySet => { + const data = new Set() + const rows = typeof replyMarkup === "object" && replyMarkup !== null + ? (replyMarkup as { readonly inline_keyboard?: unknown }).inline_keyboard + : undefined + if (!Array.isArray(rows)) return data + for (const row of rows) { + if (!Array.isArray(row)) continue + for (const button of row) { + const value = typeof button === "object" && button !== null + ? (button as { readonly callback_data?: unknown }).callback_data + : undefined + if (typeof value === "string") data.add(value) + } + } + return data +} diff --git a/packages/chat/telegram/src/internal/adapter.ts b/packages/chat/telegram/src/internal/adapter.ts new file mode 100644 index 00000000000..3c458e4beed --- /dev/null +++ b/packages/chat/telegram/src/internal/adapter.ts @@ -0,0 +1,333 @@ +import { + type ChatAdapter, + checkMessageLength, + type Descriptor, + type File, + type Posted, + type ThreadInfo +} from "effect/chat/ChatAdapter" +import { type Capability, type ChatError, type ChatErrorReason, interactionExpired } from "effect/chat/ChatError" +import type * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ChatThread } from "effect/chat/ChatThread" +import type * as ChatUi from "effect/chat/ChatUi" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as Stream from "effect/Stream" +import type { TelegramBotApi } from "../TelegramBotApi.ts" +import * as Actions from "./actions.ts" +import * as Address from "./address.ts" +import * as Errors from "./errors.ts" +import * as Markdown from "./markdown.ts" +import { integer, isObject, messageOf, object, string } from "./normalize.ts" +import { CurrentPostOptions } from "./options.ts" + +/** Capabilities that are built but not yet confirmed against Telegram. @internal */ +export interface Unverified { + readonly files?: boolean | undefined + readonly buttons?: boolean | undefined + readonly commands?: boolean | undefined +} + +/** @internal */ +export const capabilitiesOf = (unverified: Unverified): ReadonlySet => { + const set = new Set(["edit", "delete", "replyTo", "download", "getThread", "getUser"]) + if (unverified.files === true) set.add("files") + if (unverified.buttons === true) set.add("buttons") + if (unverified.commands === true) set.add("commands") + return set +} + +/** How long Telegram takes an answer to a command, from the command's `date`. @internal */ +export const followUpAge = Duration.seconds(15) + +/** The largest file the cloud Bot API server hands out. @internal */ +export const downloadLimit = 20 * 1024 * 1024 + +/** @internal */ +export const descriptorOf = (capabilities: ReadonlySet): Descriptor => ({ + platform: Address.platform, + capabilities, + measure: (markdown) => Markdown.measure(markdown), + limits: { + messageLength: Markdown.messageLength, + filesPerMessage: capabilities.has("files") ? Markdown.filesPerMessage : 0, + uploadBytes: 50 * 1024 * 1024, + minWriteInterval: Duration.seconds(3), + ...(capabilities.has("buttons") + ? { actionsPerMessage: Actions.actionsPerMessage, actionDataLength: Actions.actionDataLength } + : {}), + ...(capabilities.has("commands") ? { followUpAge } : {}) + } +}) + +/** Where a command's answer goes: the chat it came from, and the message to reply to, if any. @internal */ +export interface Origin { + readonly thread: ChatThread + readonly replyTo?: number | undefined + /** The command's `date`, in seconds. */ + readonly date: number +} + +/** + * Where a command's answers go. A reply to an ephemeral command would have + * to be ephemeral itself, and a reply would quote the private prompt, so + * a public answer to one is a new post. + * + * @internal + */ +export const originOf = (command: ChatInteraction.CommandInvoked): Origin | undefined => { + const raw = command.raw + const message = isObject(raw) ? messageOf(raw) : undefined + const id = integer(message, "message_id") + const date = integer(message, "date") + if (id === undefined || date === undefined) return undefined + return { thread: command.thread, ...(id === 0 ? {} : { replyTo: id }), date } +} + +/** @internal */ +export const make = (options: { + readonly api: TelegramBotApi["Service"] + readonly capabilities: ReadonlySet +}) => { + const { api, capabilities } = options + const descriptor = descriptorOf(capabilities) + + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + + const unsupported = (op: string, capability: Capability) => fail(op, Errors.unsupported(capability)) + + const resolve = (op: string, thread: ChatThread): Effect.Effect => { + const parts = Address.parts(thread) + return Option.isNone(parts) ? fail(op, Errors.invalidRequest("invalid_thread")) : Effect.succeed(parts.value) + } + + const target = (parts: Address.Parts) => ({ + chat_id: Number(parts.chatId), + ...(parts.messageThreadId === undefined ? {} : { message_thread_id: Number(parts.messageThreadId) }) + }) + + /** A message id Telegram can act on, or `NotFound` without a call: a private answer's label reaches nothing. */ + const messageId = (op: string, id: string): Effect.Effect => { + const n = Address.messageId(id) + return n === undefined ? fail(op, Errors.notFound("message_not_found")) : Effect.succeed(n) + } + + const markup = (op: string, actions: ReadonlyArray) => { + if (actions.length === 0) return Effect.succeed(undefined) + if (actions.some((action) => action._tag === "Select")) return unsupported(op, "selects") + if (!capabilities.has("buttons")) return unsupported(op, "buttons") + const keyboard = Actions.keyboard(actions) + return "inline_keyboard" in keyboard ? Effect.succeed(keyboard) : fail(op, keyboard) + } + + const checkFiles = (op: string, files: ReadonlyArray) => { + if (files.length === 0) return Effect.void + if (!capabilities.has("files")) return unsupported(op, "files") + if (files.length > descriptor.limits.filesPerMessage) return fail(op, Errors.refusedFile) + if (files.some((file) => file.content.length > descriptor.limits.uploadBytes)) return fail(op, Errors.refusedFile) + return Effect.void + } + + const richMessage = Effect.fnUntraced(function*(markdown: string, files: number) { + const postOptions = yield* CurrentPostOptions + return { + markdown: Markdown.render(markdown, { + mentions: new Set(postOptions.notify?.users ?? []), + files + }), + skip_entity_detection: true, + ...(files === 0 ? {} : { + media: Array.from({ length: files }, (_, i) => ({ + id: `f${i + 1}`, + media: { type: "document", media: `attach://f${i + 1}` } + })) + }) + } + }) + + const posted = (op: string, thread: ChatThread, answer: unknown): Effect.Effect => { + const message = isObject(answer) ? answer : undefined + const id = integer(message, "message_id") + const date = integer(message, "date") + if (id === undefined || id === 0) return fail(op, Errors.invalidResponse(true, "missing_message_id")) + return Effect.map( + date === undefined ? DateTime.now : Effect.succeed(DateTime.makeUnsafe(date * 1000)), + (timestamp) => ({ id: String(id), thread, timestamp }) + ) + } + + /** One `sendRichMessage`. `retry` false for answers, which are never retried. */ + const send = Effect.fnUntraced(function*(op: string, thread: ChatThread, content: { + readonly markdown: string + readonly actions?: ReadonlyArray | undefined + readonly files?: ReadonlyArray | undefined + readonly replyTo?: number | undefined + readonly retry?: boolean | undefined + }) { + const parts = yield* resolve(op, thread) + const actions = content.actions ?? [] + const files = content.files ?? [] + const replyMarkup = yield* markup(op, actions) + yield* checkFiles(op, files) + yield* checkMessageLength(descriptor, op, content.markdown, actions) + if (content.markdown.trim() === "" && files.length === 0) { + return yield* fail(op, Errors.invalidRequest("empty_message")) + } + const postOptions = yield* CurrentPostOptions + const answer = yield* api.call("sendRichMessage", { + ...target(parts), + rich_message: yield* richMessage(content.markdown, files.length), + ...(content.replyTo === undefined + ? {} + : { reply_parameters: { message_id: content.replyTo, allow_sending_without_reply: false } }), + ...(replyMarkup === undefined ? {} : { reply_markup: replyMarkup }), + ...(postOptions.silent === true ? { disable_notification: true } : {}) + }, { + write: true, + retry: content.retry, + files: files.map((file, i) => ({ field: `f${i + 1}`, file })) + }).pipe(Effect.mapError(Errors.withOp(op))) + return yield* posted(op, thread, answer) + }) + + const post: ChatAdapter["post"] = Effect.fnUntraced(function*(thread, content) { + const replyTo = content.replyTo === undefined ? undefined : yield* messageId("post", content.replyTo.messageId) + return yield* send("post", thread, { ...content, replyTo }) + }) + + const edit: ChatAdapter["edit"] = Effect.fnUntraced(function*(thread, id, content) { + const op = "edit" + const parts = yield* resolve(op, thread) + const message = yield* messageId(op, id) + const actions = content.actions ?? [] + const replyMarkup = yield* markup(op, actions) + yield* checkMessageLength(descriptor, op, content.markdown, actions) + if (content.markdown.trim() === "") return yield* fail(op, Errors.invalidRequest("empty_message")) + yield* api.call("editMessageText", { + chat_id: Number(parts.chatId), + message_id: message, + rich_message: yield* richMessage(content.markdown, 0), + // An edit replaces the whole message: no actions removes the keyboard. + reply_markup: replyMarkup ?? { inline_keyboard: [] } + }, { write: true }).pipe( + Effect.catchIf( + (error) => error.reason._tag === "InvalidRequest" && error.reason.detail === "not_modified", + () => Effect.void + ), + Effect.mapError(Errors.withOp(op)) + ) + }) + + const deleteMessage: ChatAdapter["delete"] = Effect.fnUntraced(function*(thread, id) { + const op = "delete" + const parts = yield* resolve(op, thread) + const message = yield* messageId(op, id) + yield* api.call("deleteMessage", { chat_id: Number(parts.chatId), message_id: message }, { write: true }).pipe( + Effect.mapError(Errors.withOp(op)) + ) + }) + + const download: ChatAdapter["download"] = (thread, attachment, { maxBytes }) => + Stream.unwrap(Effect.gen(function*() { + const op = "download" + yield* resolve(op, thread) + const limit = Math.min(maxBytes, downloadLimit) + const tooLarge = Errors.tooLarge("files", "max_bytes") + if (attachment.size !== undefined && attachment.size > limit) return yield* fail(op, tooLarge) + const answer = yield* api.call("getFile", { file_id: attachment.id }).pipe(Effect.mapError(Errors.withOp(op))) + const info = isObject(answer) ? answer : undefined + const path = string(info, "file_path") + const size = integer(info, "file_size") + if (size !== undefined && size > limit) return yield* fail(op, tooLarge) + if (path === undefined) return yield* fail(op, Errors.invalidResponse(false, "missing_file_path")) + return api.file(path, { maxBytes: limit }) + })) + + const getThread: ChatAdapter["getThread"] = Effect.fnUntraced(function*(thread) { + const op = "getThread" + const parts = yield* resolve(op, thread) + const answer = yield* api.call("getChat", { chat_id: Number(parts.chatId) }).pipe( + Effect.mapError(Errors.withOp(op)) + ) + const chat = isObject(answer) ? answer : undefined + if (parts.messageThreadId !== undefined) { + return { thread, kind: "thread", parent: Address.thread(parts.chatId), raw: answer } satisfies ThreadInfo + } + const name = string(chat, "title") ?? + ([string(chat, "first_name"), string(chat, "last_name")].filter((part) => part !== undefined).join(" ") || + undefined) + const topic = string(chat, "description") + return { + thread, + kind: chat?.type === "private" ? "direct" : "channel", + ...(name === undefined ? {} : { name }), + ...(topic === undefined ? {} : { topic }), + raw: answer + } satisfies ThreadInfo + }) + + const getUser: ChatAdapter["getUser"] = Effect.fnUntraced(function*(id, userOptions) { + const op = "getUser" + if (userOptions?.in === undefined) return yield* fail(op, Errors.invalidRequest("in_required")) + const parts = yield* resolve(op, userOptions.in) + if (!/^[1-9]\d{0,15}$/.test(id)) return yield* fail(op, Errors.notFound("user_not_found")) + const answer = yield* api.call("getChatMember", { chat_id: Number(parts.chatId), user_id: Number(id) }).pipe( + Effect.mapError(Errors.withOp(op)) + ) + const user = object(isObject(answer) ? answer : undefined, "user") + if (user === undefined || String(integer(user, "id")) !== id) { + return yield* fail(op, Errors.invalidResponse(false, "missing_user")) + } + const name = [string(user, "first_name"), string(user, "last_name")].filter((part) => part !== undefined).join(" ") + return { + id, + name: name === "" ? string(user, "username") ?? id : name, + isBot: user.is_bot === true, + isSelf: id === api.botId, + raw: answer + } + }) + + /** A public answer to a command. Never retried, so a timeout can't post it twice. @internal */ + const answer = (op: string, origin: Origin, content: { + readonly markdown: string + readonly actions?: ReadonlyArray | undefined + }): Effect.Effect => + send(op, origin.thread, { ...content, replyTo: origin.replyTo, retry: false }).pipe( + Effect.provideService(CurrentPostOptions, {}) + ) + + const followUp: ChatAdapter["followUp"] = Effect.fnUntraced(function*(deferred, content) { + const op = "followUp" + if (!capabilities.has("commands")) return yield* unsupported(op, "commands") + if (deferred.visibility === "ephemeral") return yield* unsupported(op, "ephemeral") + const origin = originOf(deferred.command) + if (origin === undefined) return yield* fail(op, Errors.invalidRequest("invalid_command")) + const now = yield* DateTime.now + const deadline = (origin.date + 1) * 1000 + Duration.toMillis(followUpAge) + if (DateTime.toEpochMillis(now) > deadline) return yield* fail(op, Errors.notFound(interactionExpired)) + yield* answer(op, origin, content) + }) + + const adapter: ChatAdapter = { + descriptor, + post, + edit, + delete: deleteMessage, + fetchMessage: () => unsupported("fetchMessage", "fetchMessage"), + history: () => Stream.fail(Errors.chatError("history", Errors.unsupported("history"))), + download, + startThread: () => unsupported("startThread", "startThread"), + getThread, + opener: () => unsupported("opener", "opener"), + getUser, + followUp + } + + return { adapter, answer } +} + +/** @internal */ +export type Made = ReturnType diff --git a/packages/chat/telegram/src/internal/address.ts b/packages/chat/telegram/src/internal/address.ts new file mode 100644 index 00000000000..a53f7d2e75b --- /dev/null +++ b/packages/chat/telegram/src/internal/address.ts @@ -0,0 +1,33 @@ +import * as ChatThread from "effect/chat/ChatThread" +import * as Option from "effect/Option" + +/** @internal */ +export const platform = "telegram" + +const chatId = /^-?[1-9]\d{0,15}$/ +const topicId = /^[1-9]\d{0,9}$/ + +/** The address of a chat, or of a topic in it. @internal */ +export const thread = (chat: string | number, messageThreadId?: string | number | undefined): ChatThread.ChatThread => + ChatThread.make(platform, messageThreadId === undefined ? String(chat) : `${chat}/${messageThreadId}`) + +/** The parts of an address. @internal */ +export interface Parts { + readonly chatId: string + readonly messageThreadId?: string | undefined +} + +/** @internal */ +export const parts = (address: ChatThread.ChatThread): Option.Option => { + if (address.platform !== platform) return Option.none() + const [chat, topic, ...rest] = address.id.split("/") + if (rest.length > 0 || !chatId.test(chat)) return Option.none() + if (topic === undefined) return Option.some({ chatId: chat }) + return topicId.test(topic) ? Option.some({ chatId: chat, messageThreadId: topic }) : Option.none() +} + +/** The prefix of the label a private answer's message id gets: `e{ephemeral_message_id}.{date}`. @internal */ +export const ephemeralPrefix = "e" + +/** A message id as Telegram takes it, or undefined. @internal */ +export const messageId = (id: string): number | undefined => /^[1-9]\d{0,15}$/.test(id) ? Number(id) : undefined diff --git a/packages/chat/telegram/src/internal/commands.ts b/packages/chat/telegram/src/internal/commands.ts new file mode 100644 index 00000000000..0099432edd1 --- /dev/null +++ b/packages/chat/telegram/src/internal/commands.ts @@ -0,0 +1,37 @@ +import type * as ChatInteraction from "effect/chat/ChatInteraction" + +/** @internal */ +export interface BotCommand { + readonly command: string + readonly description: string +} + +/** + * The `commands` of `setMyCommands` for a group's command definitions, or + * the ids Telegram can't take: its names are `[a-z0-9_]{1,32}`. A + * command's `argument` has no Telegram counterpart and is appended to its + * description, cut to 256 characters. + * + * @internal + */ +export const botCommands = ( + group: ChatInteraction.Group +): { readonly commands: ReadonlyArray } | { readonly invalid: ReadonlyArray } => { + const commands: Array = [] + const invalid: Array = [] + for (const definition of (group.definitions as ReadonlyMap).values()) { + if (definition.kind !== "command") continue + if (!/^[a-z0-9_]{1,32}$/.test(definition.id)) { + invalid.push(definition.id) + continue + } + const argument = definition.argument === undefined + ? "" + : ` <${definition.argument.name}${definition.argument.required ? "" : "?"}>: ${definition.argument.description}` + commands.push({ + command: definition.id, + description: Array.from(definition.description + argument).slice(0, 256).join("") + }) + } + return invalid.length > 0 ? { invalid } : { commands } +} diff --git a/packages/chat/telegram/src/internal/errors.ts b/packages/chat/telegram/src/internal/errors.ts new file mode 100644 index 00000000000..1fbf2d29166 --- /dev/null +++ b/packages/chat/telegram/src/internal/errors.ts @@ -0,0 +1,144 @@ +import { + type Capability, + ChatError, + type ChatErrorReason, + fileRefused, + Forbidden, + InvalidRequest, + InvalidResponse, + NotFound, + PlatformUnavailable, + RateLimited, + TooLarge, + Transport, + Unauthenticated, + Unknown, + Unsupported +} from "effect/chat/ChatError" +import * as Duration from "effect/Duration" +import { platform } from "./address.ts" + +/** @internal */ +export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason }) + +/** @internal */ +export const withOp = (op: string) => (error: ChatError): ChatError => chatError(op, error.reason) + +/** @internal */ +export const unsupported = (capability: Capability): ChatErrorReason => new Unsupported({ capability }) + +/** @internal */ +export const invalidRequest = (detail: string): ChatErrorReason => new InvalidRequest({ detail }) + +/** @internal */ +export const notFound = (detail: string): ChatErrorReason => new NotFound({ detail }) + +/** @internal */ +export const tooLarge = (subject: "text" | "files" | undefined, detail: string): ChatErrorReason => + new TooLarge({ ...(subject === undefined ? {} : { subject }), detail }) + +/** @internal */ +export const unauthenticatedToken: ChatErrorReason = new Unauthenticated({ detail: "invalid_token" }) + +/** @internal */ +export const refusedFile: ChatErrorReason = new TooLarge({ subject: "files", detail: fileRefused }) + +/** The error Telegram answers with, without its description. @internal */ +export interface ApiError { + readonly code: number + readonly description: string + readonly retryAfter?: number | undefined + readonly migrateTo?: number | undefined +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** The error in a body that says `ok: false`, or undefined. @internal */ +export const apiError = (body: unknown, status: number): ApiError | undefined => { + if (!isObject(body) || body.ok !== false) return undefined + const parameters = isObject(body.parameters) ? body.parameters : {} + return { + code: typeof body.error_code === "number" ? body.error_code : status, + description: typeof body.description === "string" ? body.description.toLowerCase() : "", + ...(typeof parameters.retry_after === "number" ? { retryAfter: parameters.retry_after } : {}), + ...(typeof parameters.migrate_to_chat_id === "number" ? { migrateTo: parameters.migrate_to_chat_id } : {}) + } +} + +/** + * Descriptions that refine a 400 or a 403, matched as substrings. Each gives + * a fixed detail: a description can quote the message, so it never goes + * into an error. The table grows from captured answers (live check 10). + */ +const refinements: ReadonlyArray ChatErrorReason, string]> = [ + ["message is not modified", invalidRequest, "not_modified"], + ["message to edit not found", notFound, "message_not_found"], + ["message to delete not found", notFound, "message_not_found"], + ["message to be replied not found", notFound, "reply_target_not_found"], + ["message not found", notFound, "message_not_found"], + ["chat not found", notFound, "chat_not_found"], + ["thread not found", notFound, "thread_not_found"], + ["user not found", notFound, "user_not_found"], + ["participant_id_invalid", notFound, "user_not_found"], + ["message can't be deleted", (detail) => new Forbidden({ detail }), "cannot_delete"], + ["message can't be edited", (detail) => new Forbidden({ detail }), "cannot_edit"], + ["not enough rights", (detail) => new Forbidden({ detail }), "not_enough_rights"], + ["have no rights", (detail) => new Forbidden({ detail }), "not_enough_rights"], + ["bot was blocked", (detail) => new Forbidden({ detail }), "blocked"], + ["bot was kicked", (detail) => new Forbidden({ detail }), "kicked"], + ["bot is not a member", (detail) => new Forbidden({ detail }), "not_a_member"], + // A rich message's limits, as live check 3 found them. + ["rich_message_text_too_long", (detail) => tooLarge("text", detail), "message_too_long"], + ["rich_message_too_large", (detail) => tooLarge("text", detail), "message_too_large"], + ["rich_message_blocks_too_many", (detail) => tooLarge("text", detail), "too_many_blocks"], + ["rich_message_depth_invalid", invalidRequest, "too_deep"], + ["rich_message_table_cols_too_many", invalidRequest, "too_many_columns"], + ["rich_message_empty", invalidRequest, "empty_message"], + ["rich_message_markdown_invalid", invalidRequest, "invalid_markdown"], + ["too long", (detail) => tooLarge("text", detail), "message_too_long"], + ["file is too big", () => refusedFile, fileRefused], + ["wrong file", () => refusedFile, fileRefused] +] + +/** The detail of a chat that became a supergroup. @internal */ +export const chatMigrated = "chat_migrated" + +/** + * Maps one Telegram error. `cause` is attached as given: the caller passes + * nothing that holds a URL or message text. + * + * @internal + */ +export const reasonForApiError = ( + error: ApiError, + options: { readonly write: boolean; readonly cause?: unknown } +): ChatErrorReason => { + const cause = options.cause === undefined ? {} : { cause: options.cause } + if (error.code === 429) { + return new RateLimited({ + detail: "telegram_429", + ...(error.retryAfter === undefined ? {} : { retryAfter: Duration.seconds(error.retryAfter) }) + }) + } + if (error.migrateTo !== undefined) return new NotFound({ detail: chatMigrated, ...cause }) + if (error.code === 401 || error.code === 404) return new Unauthenticated({ detail: `telegram_${error.code}` }) + if (error.code >= 500) { + return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail: `telegram_${error.code}` }) + } + if (error.code === 409) return invalidRequest("conflict") + if (error.code === 400 || error.code === 403) { + for (const [needle, make, detail] of refinements) { + if (error.description.includes(needle)) return make(detail) + } + return error.code === 403 ? new Forbidden({ detail: "telegram_403" }) : invalidRequest("telegram_400") + } + return invalidRequest(`telegram_${error.code}`) +} + +/** A request that failed before an answer came. A write may have landed. No cause: it would hold the URL. @internal */ +export const transport = (write: boolean, detail: string): ChatErrorReason => + new Transport({ retry: write ? "uncertain" : "retry", detail }) + +/** An answer the adapter cannot read. A write may have landed. @internal */ +export const invalidResponse = (write: boolean, detail: string): ChatErrorReason => + write ? new Unknown({ detail }) : new InvalidResponse({ detail }) diff --git a/packages/chat/telegram/src/internal/markdown.ts b/packages/chat/telegram/src/internal/markdown.ts new file mode 100644 index 00000000000..41af949f43a --- /dev/null +++ b/packages/chat/telegram/src/internal/markdown.ts @@ -0,0 +1,805 @@ +/** + * Rich Markdown for `sendRichMessage`: the escaping that keeps consumer + * Markdown from reaching Telegram's raw HTML, images, math, spoilers and + * non-web links, and the `measure` that bounds what it costs. + * + * @internal + */ + +/** What one line break or block marker costs, so that a message in budget stays under 500 blocks. @internal */ +export const block = 65 + +/** Telegram's rich message limit. It counts rendered characters, which UTF-8 bytes of the source bound. @internal */ +export const richLimit = 32_768 + +/** Files one post may carry. @internal */ +export const filesPerMessage = 10 + +/** The most nesting and table columns Telegram renders. @internal */ +export const maxDepth = 16 + +/** @internal */ +export const maxColumns = 20 + +/** The reference to the `n`th file of a post, at the end of the Markdown. @internal */ +export const mediaReference = (n: number): string => `\n\n![](tg://document?id=f${n})` + +/** + * What the budget keeps back for media references and the fence a post + * with files may need to close: each reference's bytes and its block, one + * block for the message's first, which `measure` does not charge, and 8 + * bytes for the fence. + * + * @internal + */ +export const reservation = filesPerMessage * (32 + block) + block + 8 + +/** @internal */ +export const messageLength = richLimit - reservation + +const twoByteEscapes = new Set(["$", "=", "|", "!", "[", "]", "*", "_", "~", "`"]) +const blockMarkers = new Set([">", "-", "+", "*", ".", ")"]) + +const isDigit = (c: string) => c >= "0" && c <= "9" + +/** + * A sum of per-character costs, each fixed or depending only on what comes + * before the character on its own line, which makes it subadditive: + * + * - a code point in the Basic Multilingual Plane costs its UTF-8 length, + * and each UTF-16 surrogate 3, enough for the U+FFFD a lone one becomes. + * NUL costs 3 too, as Markdown turns it into U+FFFD. + * - `<` costs 4, a backslash 5 (the numeric reference, such as `]`, + * that an escape Telegram ignores becomes), and + * `$ = | ! [ ] * _ ~` and the backtick 2, their escaped length or more, + * whether or not they are escaped in context. + * - `\n` and `\r` cost `block`, as does a block marker (`>`, a bullet, + * the dot or parenthesis of a number) before any other text on its line. + * + * @internal + */ +export const measure = (markdown: string): number => { + let cost = 0 + let prefix = true + for (let i = 0; i < markdown.length; i++) { + const code = markdown.charCodeAt(i) + if (code === 10 || code === 13) { + cost += block + prefix = true + continue + } + if (code >= 0xd800 && code <= 0xdfff) { + cost += 3 + prefix = false + continue + } + const c = markdown[i] + if (prefix) { + if (c === " " || c === "\t" || isDigit(c)) { + cost += 1 + continue + } + if (blockMarkers.has(c)) { + cost += block + continue + } + prefix = false + } + cost += c === "\\" + ? 5 + : code === 0 + ? 3 + : c === "<" + ? 4 + : twoByteEscapes.has(c) + ? 2 + : code < 0x80 + ? 1 + : code < 0x800 + ? 2 + : 3 + } + return cost +} + +const punctuation = /[!-/:-@[-`{-~]/ + +/** A line that opens a fence: up to three spaces, then three or more backticks or tildes. */ +const fenceOpen = /^( {0,3})(`{3,}|~{3,})(.*)$/ + +interface Fence { + readonly char: string + readonly length: number + readonly indent: number +} + +const opens = (line: string): Fence | undefined => { + const match = fenceOpen.exec(line) + if (match === null || (match[2][0] === "`" && match[3].includes("`"))) return undefined + return { char: match[2][0], length: match[2].length, indent: match[1].length } +} + +const closes = (line: string, fence: Fence): boolean => { + const match = /^ {0,3}(`+|~+)[ \t]*$/.exec(line) + return match !== null && match[1][0] === fence.char && match[1].length >= fence.length +} + +/** A line that may open a list item or footnote, which an indented fence could belong to. */ +const container = /^[ \t>]*(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$)|^[ \t>]*\[\^/ + +/** A line GFM may read as the start of another block, which ends a table and is no paragraph. */ +const blockStart = /^(?: {0,3}[#>\-*+_~`<=[]| {0,3}\d{1,9}[.)]| {4}| {0,3}\t)/ + +/** Where a line's container markers end, as far as `startsBlock` needs to know. */ +const quotes = /^(?: {0,3}> ?)* {0,3}/ + +/** + * Whether a line starts a block wherever it is, so that it can be no + * paragraph's continuation: an ATX heading, or a list item with content + * that may interrupt a paragraph (a bullet, or an ordered item numbered 1 + * or following one of its list). Tabs and other lines say no, which is + * safe: the inline count goes on. + */ +const startsBlock = (line: string, previousItem: string | undefined): string | undefined | true => { + const rest = line.slice(quotes.exec(line)![0].length) + if (/^(?:#{1,6}(?: |$)|[-+*] +\S)/.test(rest)) return true + const item = /^(\d{1,9})([.)]) +\S/.exec(rest) + if (item === null) return undefined + const key = line.slice(0, line.length - rest.length) + item[2] + return item[1] === "1" || key === previousItem ? key : undefined +} + +const delimiterRow = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/ + +/** The cells of a table row, split at unescaped pipes as GFM does, even inside code. */ +const cellCount = (line: string): number => { + let count = 1 + for (let i = 0; i < line.length; i++) { + if (line[i] === "\\") i++ + else if (line[i] === "|") count++ + } + const trimmed = line.trim() + if (trimmed.startsWith("|")) count-- + if (trimmed.length > 1 && trimmed.endsWith("|") && !trimmed.endsWith("\\|")) count-- + return count +} + +/** + * A blank line as CommonMark defines it: only spaces and tabs. Other + * whitespace, such as U+00A0, is paragraph text and ends nothing. + */ +const isBlank = (line: string): boolean => /^[ \t]*$/.test(line) + +/** Whether a line can head a table: GFM reads no cell in a lone `|`. */ +const hasPipe = (line: string) => (cellCount(line) > 1 || line.trim().startsWith("|")) && line.trim() !== "|" + +/** The decoded form of a link destination, as a Markdown parser and then a URL parser read it. */ +const decodeDestination = (raw: string): { readonly unescaped: string; readonly decoded: string } | undefined => { + let unescaped = raw.replace(/\\([!-/:-@[-`{-~])/g, "$1") + unescaped = unescaped.replace(/&#(\d{1,7});/g, (_, n: string) => String.fromCodePoint(Math.min(Number(n), 0x10ffff))) + .replace(/&#[xX]([0-9a-fA-F]{1,6});/g, (_, n: string) => String.fromCodePoint(Math.min(parseInt(n, 16), 0x10ffff))) + .replace( + /&(amp|lt|gt|quot|apos);/g, + (_, name: string) => + name === "amp" ? "&" : name === "lt" ? "<" : name === "gt" ? ">" : name === "quot" ? "\"" : "'" + ) + // Any other named reference could be one of HTML's many, such as `:`. + if (/&[A-Za-z][A-Za-z0-9]*;/.test(unescaped)) return undefined + try { + return { unescaped, decoded: decodeURIComponent(unescaped) } + } catch { + return undefined + } +} + +const allowedScheme = /^(?:https?:\/\/|mailto:)/i + +/** Whether a link to `raw` may stay: http, https and mailto, or a mention the post allows. */ +const allowedDestination = (raw: string, mentions: ReadonlySet): boolean => { + const destination = decodeDestination(raw) + if (destination === undefined) return false + if (allowedScheme.test(destination.unescaped) && allowedScheme.test(destination.decoded)) return true + const mention = /^tg:\/\/user\?id=(\d{1,20})$/.exec(destination.decoded) + return mention !== null && destination.unescaped === destination.decoded && mentions.has(mention[1]) +} + +const blank = (c: string | undefined): boolean => c === " " || c === "\t" + +/** + * The deepest parentheses a kept destination may nest. CommonMark lets a + * parser refuse deeper ones once it supports three, and GFM refuses past + * 32 in a link, which would leave the rest of the link as Markdown. + */ +const maxDestinationDepth = 3 + +/** + * The destination and optional title from `start`, as CommonMark reads + * them when they are on this line: a destination not in angle brackets, up + * to a space, a control character or a `)` it did not open, with its + * parentheses balanced and at most three deep, then a title after a space. `end` is past the + * title and the blanks after it. + */ +const destinationAndTitle = ( + line: string, + start: number +): { readonly destination: string; readonly end: number } | undefined => { + let i = start + while (blank(line[i])) i++ + if (line[i] === "<") return undefined + const from = i + let depth = 0 + for (; i < line.length; i++) { + const c = line[i] + if (c === "\\" && punctuation.test(line[i + 1] ?? "")) { + i++ + continue + } + const code = c.charCodeAt(0) + if (code <= 0x20 || code === 0x7f) break + if (c === "(") { + if (++depth > maxDestinationDepth) return undefined + } else if (c === ")") { + if (depth === 0) break + depth-- + } + } + if (depth > 0) return undefined + const destination = line.slice(from, i) + const spaced = blank(line[i]) + while (blank(line[i])) i++ + const open = line[i] + if (spaced && (open === "\"" || open === "'" || open === "(")) { + const close = open === "(" ? ")" : open + for (i++; i < line.length && line[i] !== close; i++) { + if (line[i] === "\\" && punctuation.test(line[i + 1] ?? "")) i++ + else if (open === "(" && line[i] === "(") return undefined + } + if (i >= line.length) return undefined + i++ + while (blank(line[i])) i++ + } + return { destination, end: i } +} + +/** + * A link's `(destination "title")` from the `(` at `start`, when all of it + * is on this line. A tail that goes on to the next line, as CommonMark + * allows, is not one: the link is then refused. + */ +const linkTail = (line: string, start: number): { readonly destination: string; readonly end: number } | undefined => { + const tail = destinationAndTitle(line, start + 1) + return tail !== undefined && line[tail.end] === ")" ? { destination: tail.destination, end: tail.end + 1 } : undefined +} + +/** + * The punctuation whose backslash escape Telegram honors. Before any other + * punctuation the backslash stays as text and the character is read as + * syntax: `\` is bold. `\]` hides the bracket from emphasis but still + * closes a link or a definition label, so it is not here either. + */ +const honored = new Set("!#$()*+-.=>[\\_`{|}~") + +/** A consumer's escape of `c` as Telegram reads it: kept, or a numeric character reference. */ +const escapeOf = (c: string): string => honored.has(c) ? "\\" + c : `&#${c.charCodeAt(0)};` + +/** + * A kept destination as Telegram needs it. Telegram reads a destination + * verbatim, with no escapes, so they are decoded, a parenthesis or + * backslash percent-encoded, and a `<` too, which would end the link. + */ +const destinationOf = (raw: string): string => + raw.replace(/\\([!-/:-@[-`{-~])| { + const c = escaped ?? match + return percentEncoded[c] ?? c + }) + +const percentEncoded: Readonly> = { + "(": "%28", + ")": "%29", + "\\": "%5C", + "<": "%3C", + ">": "%3E" +} + +/** + * The destination of a reference definition after `]:`, when the rest of + * the line is a nonempty destination and an optional title. Anything else + * after it, a title that goes on to the next line, or a destination there, + * makes it no definition here. + */ +const definitionDestination = (line: string, start: number): string | undefined => { + const tail = destinationAndTitle(line, start) + return tail !== undefined && tail.end === line.length ? tail.destination : undefined +} + +/** The most characters CommonMark reads between a label's brackets. */ +const maxLabelLength = 999 + +/** + * The most characters the renderer can make of a simple label: an escape + * becomes at most `%5C<`, and each of `$ = | * _ ~` at most two. The + * label it sends, not the consumer's, must fit in 999. + */ +const renderedLabelBound = (text: string): number => { + let n = 0 + for (let i = 0; i < text.length; i++) { + if (text[i] === "\\") { + n += 7 + i++ + } else n += "$=|*_~".includes(text[i]) ? 2 : 1 + } + return n +} + +/** + * A label CommonMark and the renderer both close at its first `]`: no + * other bracket, code span or autolink in it, and no escape at its end. + */ +const simpleLabel = /^(?:[^\\[\]`<]|\\.)*$/ + +/** + * The reference definition the line whose first `[` is at `start` may + * keep: a simple label, not blank, not a footnote's and at most 999 + * characters once rendered, closed by `]:`, then a destination it allows. + */ +const keptDefinition = ( + line: string, + start: number, + mentions: ReadonlySet +): { readonly close: number; readonly destination: string } | undefined => { + const close = line.indexOf("]", start) + const text = line.slice(start + 1, close) + if ( + close < 0 || line[close + 1] !== ":" || !simpleLabel.test(text) || isBlank(text) || text[0] === "^" || + renderedLabelBound(text) > maxLabelLength + ) return undefined + const destination = definitionDestination(line, close + 2) + return destination !== undefined && allowedDestination(destination, mentions) ? { close, destination } : undefined +} + +/** + * The end of the code span whose opening backticks are at `start`, found as + * CommonMark finds it: the next run of as many backticks on the line. A + * run with no partner on the line opens no span: the renderer escapes it, + * so no span can pair across lines. In a table row a span holding a pipe + * is refused, since GFM splits cells before it reads code. + */ +const codeSpanEnd = (line: string, start: number, tableRow: boolean): number | undefined => { + let length = 0 + while (line[start + length] === "`") length++ + for (let j = start + length; j < line.length;) { + if (line[j] !== "`") { + j++ + continue + } + let run = 0 + while (line[j + run] === "`") run++ + if (run === length) return tableRow && line.slice(start, j).includes("|") ? undefined : j + run + j += run + } + return undefined +} + +/** + * Where GFM starts a bare URL link. It runs to the next space, taking + * backslashes and backticks with it, so inside one an escape is not + * needed and a backtick opens no span. + */ +const bareUrl = /^(?:https?:\/\/|www\.)/i + +/** A thematic break, whose delimiters stand alone. */ +const thematicBreak = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/ + +const autolink = /^<([A-Za-z][A-Za-z0-9+.-]{1,31}):[^\s<>`\p{Cc}]*>/u + +/** + * What one paragraph may already nest. `open` counts every delimiter that + * could open emphasis, and none is taken back for one that may close it: + * whether a run closes, and what, depends on flanking rules and links that + * this renderer does not follow, and a node needs at least one opener. + * `containers` is the most container depth of the paragraph's lines, as a + * lazy continuation line stays in containers it does not repeat. `nodes` + * counts the code spans, links and footnote references, which Telegram + * caps per paragraph. + */ +interface Inline { + open: number + containers: number + nodes: number +} + +/** The inline nodes one paragraph may hold. Telegram refuses a paragraph past about 4,387. @internal */ +export const maxInlineNodes = 4000 + +/** What one footnote reference costs against `maxInlineNodes`: its superscript, anchor and link. */ +const footnoteNodes = 3 + +/** The footnote references one message may hold. Telegram refuses about 774 to one footnote. @internal */ +export const maxFootnoteReferences = 700 + +interface LineContext { + readonly tableRow: boolean + /** Whether GFM may read the line as a table row, which splits code spans at pipes. */ + readonly cells: boolean + readonly widePipes: boolean + readonly setext: boolean + readonly inline: Inline + readonly mentions: ReadonlySet + /** Footnote references so far in the message. */ + readonly footnotes: { count: number } + /** + * Whether a definition may start this line: after a blank line, the + * start of the text or another definition, outside containers. Set to + * whether this line kept one. + */ + readonly definitions: { allowed: boolean } +} + +/** + * The container markers at the start of a line, with those past the depth + * limit escaped, and where the line's text starts. + */ +const renderPrefix = (line: string): { readonly text: string; readonly rest: number; readonly depth: number } => { + let out = "" + let i = 0 + let depth = 0 + let indent = 0 + for (;;) { + const space = /^[ \t]*/.exec(line.slice(i))![0] + indent += space.length + out += space + i += space.length + const marker = /^(?:>|[-+*](?=[ \t]|$)|\d{1,9}[.)](?=[ \t]|$))/.exec(line.slice(i)) + if (marker === null) break + depth++ + const token = marker[0] + const escape = depth + Math.floor(indent / 2) >= maxDepth + out += escape ? token.slice(0, -1) + "\\" + token.slice(-1) : token + i += token.length + } + return { text: out, rest: i, depth: depth + Math.floor(indent / 2) } +} + +const renderLine = (line: string, context: LineContext): string => { + const prefix = renderPrefix(line) + // A footnote definition opens a block, so not behind a tab or four + // spaces: there, after a definition, GFM reads one with a label `^a`. + const indent = /[ \t]*$/.exec(line.slice(0, prefix.rest))![0] + const footnote = !indent.includes("\t") && indent.length < 4 && /^\[\^[\w-]+\]:/.test(line.slice(prefix.rest)) + const inline = context.inline + inline.containers = Math.max(inline.containers, prefix.depth) + const limit = Math.max(0, maxDepth - 1 - inline.containers) + /** Counts `n` inline nodes, or says no when the paragraph has no room left. */ + const node = (n: number): boolean => { + if (inline.nodes + n > maxInlineNodes) return false + inline.nodes += n + return true + } + // Telegram reads a definition wherever the line's first `[` closes at a + // `]:`, also past other brackets, code or autolinks, and GFM reads one + // at the start of a footnote's text. One is kept only where CommonMark + // reads the same, with a label and destination both accept. Otherwise + // the line's `[` is escaped before it is read, so no raw destination is + // sent behind a label that is text. + const shaped = line[prefix.rest] === "[" && + line.includes("]:", footnote ? line.indexOf("]:", prefix.rest) + 2 : prefix.rest) + const definition = shaped && context.definitions.allowed && prefix.depth === 0 ? + keptDefinition(line, prefix.rest, context.mentions) : + undefined + context.definitions.allowed = false + // Where the line's opening `[` is in `out`, while it may close on a later line. + const label = line[prefix.rest] === "[" ? prefix.text.length : undefined + // Where the `[` of a label with no other bracket in it is, until a `]` + // closes it. Only such a label keeps its destination: around a nested + // label, a reference or a footnote, whether the outer brackets make a + // link depends on definitions and on Telegram's precedence, and a raw + // tail would be read as Markdown. + let opener: number | undefined + // Where each `[` not yet closed on this line is in `out`. + const unclosed: Array = [] + let out = prefix.text + let urlEnd = -1 + let i = prefix.rest + if (shaped && definition === undefined) { + out += "\\[" + i++ + } + while (i < line.length) { + const c = line[i] + const next = line[i + 1] + if (i >= urlEnd && bareUrl.test(line.slice(i, i + 8))) { + urlEnd = i + while (urlEnd < line.length && line[urlEnd] !== " " && line[urlEnd] !== "\t") urlEnd++ + } + const inUrl = i < urlEnd + // In a bare URL a backslash escapes nothing, outside one it does: as + // `%5C` it is the same character to a browser and inert to both. + if (c === "\\" && inUrl && next !== undefined && punctuation.test(next)) { + out += "%5C" + i++ + continue + } + if (c === "\\" && next !== undefined && punctuation.test(next)) { + out += escapeOf(next) + i += 2 + continue + } + switch (c) { + case "`": { + const end = inUrl ? undefined : codeSpanEnd(line, i, context.cells) + if (end !== undefined && node(1)) { + out += line.slice(i, end) + i = end + continue + } + while (line[i] === "`") { + out += "\\`" + i++ + } + continue + } + case "<": { + const match = inUrl ? null : autolink.exec(line.slice(i)) + if (match !== null && /^(?:https?|mailto)$/i.test(match[1]) && node(1)) { + out += match[0] + i += match[0].length + } else { + out += "<" + i++ + } + continue + } + case "$": + out += "\\$" + i++ + continue + case "=": + out += context.setext ? "=" : "\\=" + i++ + continue + case "|": + out += context.tableRow && !context.widePipes ? "|" : "\\|" + i++ + continue + case "!": + out += next === "[" ? "\\!" : "!" + i++ + continue + case "[": + // A footnote reference, unless it opens the line's footnote definition. + if (next === "^" && !(footnote && i === prefix.rest)) { + if (context.footnotes.count >= maxFootnoteReferences || !node(footnoteNodes)) { + out += "\\[" + i++ + continue + } + context.footnotes.count++ + opener = undefined + } else if (!inUrl) { + // Each `[` that could open a link or a reference counts as a node. + if (!node(1)) { + out += "\\[" + i++ + continue + } + opener = i + } + unclosed.push(out.length) + out += "[" + i++ + continue + case "]": { + // Telegram closes a link at `]` even when it is escaped, so a refused + // link loses its `(`. A kept destination is sent verbatim, which is + // inert only if it is read as one: a link needs an opener on its + // line, and not `[a][b](…)` or `[a][](…)`, where `[a]` may be the link. + const plain = opener !== undefined && line[opener - 1] !== "]" + const labelOpener = opener + opener = undefined + unclosed.pop() + if (next === "(") { + const tail = linkTail(line, i + 1) + if (plain && tail !== undefined && allowedDestination(tail.destination, context.mentions)) { + out += "](" + destinationOf(tail.destination) + ")" + i = tail.end + continue + } + out += "]\\(" + i += 2 + continue + } + // The `[` at the line's start counted as a node, so it is still a label. + if (definition !== undefined && i === definition.close && labelOpener === prefix.rest) { + context.definitions.allowed = true + return out + "]: " + destinationOf(definition.destination) + } + out += "]" + i++ + continue + } + case "*": + case "_": + case "~": { + let end = i + while (line[end] === c) end++ + const run = line.slice(i, end) + const after = line[end] + const spaced = (ch: string | undefined) => ch === undefined || ch === " " || ch === "\t" + // A run with space on both sides is text to CommonMark, but Telegram + // opens emphasis with it: `2 * 3 * 4` (live check 2). + if (spaced(i === prefix.rest ? undefined : line[i - 1]) && spaced(after) && !thematicBreak.test(line)) { + out += run.split("").map((char) => "\\" + char).join("") + i = end + continue + } + // A run of n delimiters can open n levels: `***a*b*c*` is three. + // One followed by a space or the line's end can't open. + if (!spaced(after)) { + if (inline.open + run.length > limit) { + out += run.split("").map((char) => "\\" + char).join("") + i = end + continue + } + inline.open += run.length + } + out += run + i = end + continue + } + default: + out += c + i++ + } + } + // A label left open at the start of a line could close on a later one, + // as a definition's. + if (label !== undefined && unclosed[0] === label) out = out.slice(0, label) + "\\" + out.slice(label) + return out +} + +/** @internal */ +export interface RenderOptions { + /** User ids whose `[name](tg://user?id=…)` links may stay. */ + readonly mentions?: ReadonlySet | undefined + /** How many files the post references at the end. */ + readonly files?: number | undefined +} + +/** + * Consumer Markdown as rich Markdown that keeps to what `measure` charged: + * + * - Fences pass verbatim, as do code spans, which are found left to right + * as CommonMark finds them. A fence indented by one to three spaces after + * a list item could belong to it, so from such a fence on nothing is + * verbatim. Inside a bare URL a backtick opens no span. + * - `<` becomes `<` unless it opens an http, https or mailto autolink, + * so there is no raw HTML, also after a bare URL, which would take a + * backslash with it. `$` and `=` are escaped (math, marks), except `=` + * under a paragraph of plain top-level lines (a setext heading), as is `|` outside tables + * (spoilers), and `!` before `[` (images). + * - A backslash escape Telegram ignores becomes a numeric character + * reference, so `\<` is no HTML and `\]` closes no link. + * - A link or reference definition whose destination, decoded as a + * Markdown parser and then a URL parser would, is not http, https or + * mailto is text: a link loses its `(`, a definition its opening `[`. + * A `tg://user` link stays for an id in `mentions`. A kept link needs its + * `[` and its whole tail on its line, a label with no other bracket in + * it, and no `]` right before it. A line whose first `[` meets a `]:` + * is a definition to Telegram, and is kept only at the top level after + * a blank line or another definition, with a label that starts the + * line, holds no other bracket, code span, autolink or escaped `]`, is + * not blank or a footnote's and is at most 999 characters, and with + * nothing after its destination but a title. Otherwise its `[` is + * escaped. Either needs balanced parentheses and no control character + * in its destination. Its destination is sent as Telegram reads it, + * verbatim, and its title is dropped. + * - A `*`, `_` or `~` run with space on both sides is escaped, outside a + * thematic break: Telegram would open emphasis with it. + * - Code spans, `[` that could open a link or reference, and footnote + * references past `maxInlineNodes` in a paragraph, or footnote + * references past `maxFootnoteReferences` in the message, are text. A + * `[` that starts a line and is not closed on it is escaped, as it could + * open a definition whose label ends on a later line. + * - Container markers and emphasis past 16 levels are escaped. Every + * delimiter that could open emphasis counts as a level until the + * paragraph ends, at a blank line or a line that surely starts a block, + * and a lazy continuation line keeps the paragraph's container depth. + * - A table keeps its pipes only when its header is at the start of a line + * after a blank line, a heading or the start of the text, where it can + * be no paragraph's continuation. A table wider than 20 columns has its + * pipes escaped. A table ends at a line that may start another block. + * - `files` references are added at the end, after closing a fence left + * open. + * + * @internal + */ +export const render = (markdown: string, options: RenderOptions = {}): string => { + const mentions = options.mentions ?? new Set() + const lines = markdown.replace(/\r\n?/g, "\n").replace(/\0/g, "\ufffd").split("\n") + const fenced: Array = [] + let fence: Fence | undefined + let containers = false + let verbatim = true + for (const line of lines) { + if (fence !== undefined) { + fenced.push(true) + if (closes(line, fence)) fence = undefined + continue + } + const opened = verbatim ? opens(line) : undefined + if (opened !== undefined && opened.indent > 0 && containers) verbatim = false + else if (opened !== undefined) { + fence = opened + fenced.push(true) + continue + } + if (container.test(line)) containers = true + fenced.push(false) + } + const tableRows = new Map() + const cellLines = new Set() + for (let i = 0; i + 1 < lines.length; i++) { + if (fenced[i] || fenced[i + 1] || tableRows.has(i) || blockStart.test(lines[i])) continue + // Neither row indented, nor the header after a line it could continue. + if (/^\s/.test(lines[i]) || /^\s/.test(lines[i + 1])) continue + const above = i === 0 ? "" : lines[i - 1] + if (!isBlank(above) && !/^#{1,6}(?: |$)/.test(above)) continue + if (!delimiterRow.test(lines[i + 1]) || !lines[i + 1].includes("|") || !hasPipe(lines[i])) continue + // A delimiter row that is also a list item ends the paragraph first. + if (/^ {0,3}-[ \t]+\S/.test(lines[i + 1])) continue + const columns = cellCount(lines[i]) + if (columns !== cellCount(lines[i + 1])) continue + let end = i + 2 + while (end < lines.length && !fenced[end] && !isBlank(lines[end]) && !blockStart.test(lines[end])) end++ + const rows = Array.from({ length: end - i }, (_, k) => i + k) + for (const row of rows) tableRows.set(row, columns > maxColumns || cellCount(lines[row]) > maxColumns) + // GFM ends a table at fewer lines than `blockStart` names, so a line + // past its rows here may still be one of its rows there. + for (let k = end; k < lines.length && !fenced[k] && !isBlank(lines[k]); k++) cellLines.add(k) + i = end - 1 + } + const inline: Inline = { open: 0, containers: 0, nodes: 0 } + const footnotes = { count: 0 } + const definitions = { allowed: true } + let item: string | undefined + // Whether every line since the last blank one is plain text at the top + // level: no container, definition, table or other block that a `=` under + // it could be the lazy continuation of. + let plain = true + const out = lines.map((line, i) => { + const blank = isBlank(line) + const setext = plain && i > 0 && !isBlank(lines[i - 1]) && /^ {0,3}=+[ \t]*$/.test(line) + plain = !fenced[i] && (blank || (plain && !/^\s/.test(line) && !blockStart.test(line) && !line.includes("|"))) + if (fenced[i]) { + definitions.allowed = false + return line + } + const wide = tableRows.get(i) + const starts = blank ? undefined : startsBlock(line, item) + item = typeof starts === "string" ? starts : undefined + if (blank || starts !== undefined || wide !== undefined) { + inline.open = 0 + inline.containers = 0 + inline.nodes = 0 + } + const rendered = renderLine(line, { + tableRow: wide !== undefined, + cells: wide !== undefined || cellLines.has(i), + widePipes: wide === true, + setext, + inline, + mentions, + footnotes, + definitions + }) + if (blank) definitions.allowed = true + return rendered + }) + let text = out.join("\n") + const files = options.files ?? 0 + if (files > 0) { + if (fence !== undefined) text += "\n" + fence.char.repeat(fence.length) + for (let n = 1; n <= files; n++) text += mediaReference(n) + } + return text +} diff --git a/packages/chat/telegram/src/internal/normalize.ts b/packages/chat/telegram/src/internal/normalize.ts new file mode 100644 index 00000000000..b29ba8a004d --- /dev/null +++ b/packages/chat/telegram/src/internal/normalize.ts @@ -0,0 +1,279 @@ +import * as ChatEvent from "effect/chat/ChatEvent" +import type * as ChatMessage from "effect/chat/ChatMessage" +import type { ChatThread } from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import type * as Schema from "effect/Schema" +import * as Address from "./address.ts" + +/** @internal */ +export type JsonObject = { readonly [key: string]: Schema.Json } + +/** @internal */ +export const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +/** @internal */ +export const object = (o: JsonObject | undefined, key: string): JsonObject | undefined => { + const value = o?.[key] + return isObject(value) ? value : undefined +} + +/** @internal */ +export const string = (o: JsonObject | undefined, key: string): string | undefined => { + const value = o?.[key] + return typeof value === "string" ? value : undefined +} + +/** @internal */ +export const integer = (o: JsonObject | undefined, key: string): number | undefined => { + const value = o?.[key] + return typeof value === "number" && Number.isSafeInteger(value) ? value : undefined +} + +/** What normalising needs to know about the bot. @internal */ +export interface Context { + readonly botId: string + /** The bot's username, without `@`, from `getMe`. */ + readonly botUsername?: string | undefined + readonly environment?: "production" | "test" | undefined +} + +/** + * Whether a message belongs to a context v1 leaves out: a business + * connection, a channel's direct messages chat, or guest mode. + * + * @internal + */ +export const isExcluded = (message: JsonObject): boolean => + "business_connection_id" in message || + "guest_query_id" in message || + "guest_bot_caller_user" in message || + "guest_bot_caller_chat" in message || + "direct_messages_topic" in message || + object(message, "chat")?.is_direct_messages === true + +/** Whether a message is a private answer or input: `message_id` 0 with an `ephemeral_message_id`. @internal */ +export const isEphemeral = (message: JsonObject): boolean => + integer(message, "message_id") === 0 && integer(message, "ephemeral_message_id") !== undefined + +/** The address and kind of the chat a message is in. @internal */ +export const threadOf = ( + message: JsonObject +): { readonly thread: ChatThread; readonly kind: ChatEvent.ThreadKind } | undefined => { + const chat = object(message, "chat") + const chatId = integer(chat, "id") + if (chat === undefined || chatId === undefined) return undefined + const topic = message.is_topic_message === true ? integer(message, "message_thread_id") : undefined + if (topic !== undefined) return { thread: Address.thread(chatId, topic), kind: "thread" } + return { thread: Address.thread(chatId), kind: chat.type === "private" ? "direct" : "channel" } +} + +/** A message's id as events carry it: the real id, or `e{ephemeral_message_id}.{date}` for a private one. @internal */ +export const messageIdOf = (message: JsonObject): string | undefined => { + const id = integer(message, "message_id") + if (id === undefined) return undefined + if (id !== 0) return String(id) + const ephemeral = integer(message, "ephemeral_message_id") + return ephemeral === undefined ? undefined : `${Address.ephemeralPrefix}${ephemeral}.${integer(message, "date") ?? 0}` +} + +const displayName = (user: JsonObject) => { + const name = [string(user, "first_name"), string(user, "last_name")].filter((part) => part !== undefined).join(" ") + return name === "" ? string(user, "username") : name +} + +const authorOf = (message: JsonObject, context: Context): ChatMessage.Author | undefined => { + const senderChat = object(message, "sender_chat") + const senderChatId = integer(senderChat, "id") + if (senderChat !== undefined && senderChatId !== undefined) { + const title = string(senderChat, "title") + return { id: String(senderChatId), ...(title === undefined ? {} : { name: title }), isBot: false, isSelf: false } + } + const from = object(message, "from") + const fromId = integer(from, "id") + if (from === undefined || fromId === undefined) return undefined + const name = displayName(from) + return { + id: String(fromId), + ...(name === undefined ? {} : { name }), + isBot: from.is_bot === true, + isSelf: String(fromId) === context.botId + } +} + +const mentionsOf = (text: string, entities: ReadonlyArray, context: Context) => { + const mentions: Array = [] + const self = context.botUsername?.toLowerCase() + for (const entity of entities) { + if (!isObject(entity)) continue + const start = integer(entity, "offset") + const length = integer(entity, "length") + if (start === undefined || length === undefined || start < 0 || start + length > text.length) continue + const end = start + length + if (entity.type === "text_mention") { + const user = integer(object(entity, "user"), "id") + if (user !== undefined) { + mentions.push({ kind: "user", id: String(user), start, end, isSelf: String(user) === context.botId }) + } + } else if (entity.type === "mention" && self !== undefined && text.slice(start + 1, end).toLowerCase() === self) { + mentions.push({ kind: "user", id: context.botId, start, end, isSelf: true }) + } + } + return mentions +} + +const attachmentsOf = (message: JsonObject): Array => { + const out: Array = [] + const add = (file: JsonObject | undefined) => { + const id = string(file, "file_id") + if (id === undefined) return + const name = string(file, "file_name") + const contentType = string(file, "mime_type") + const size = integer(file, "file_size") + out.push({ + id, + ...(name === undefined ? {} : { name }), + ...(contentType === undefined ? {} : { contentType }), + ...(size === undefined ? {} : { size }) + }) + } + // An animation comes with a copy of itself as `document`, for older clients. + const animation = object(message, "animation") + add(animation ?? object(message, "document")) + for (const key of ["audio", "video", "voice"]) add(object(message, key)) + const photo = message.photo + if (Array.isArray(photo) && photo.length > 0) { + let largest: JsonObject | undefined + for (const size of photo) { + if (!isObject(size)) continue + const area = (integer(size, "width") ?? 0) * (integer(size, "height") ?? 0) + const best = (integer(largest, "width") ?? 0) * (integer(largest, "height") ?? 0) + if (largest === undefined || area > best) largest = size + } + add(largest) + } + return out +} + +const replyToOf = (message: JsonObject, context: Context): ChatMessage.ReplyTo | undefined => { + const reply = object(message, "reply_to_message") + const id = integer(reply, "message_id") + if (reply === undefined || id === undefined || id === 0) return undefined + // A message in a topic replies to the topic's creation message, which is not a reply. + if ( + message.is_topic_message === true && + (id === integer(message, "message_thread_id") || "forum_topic_created" in reply) + ) { + return undefined + } + const author = integer(object(reply, "from"), "id") + return author === undefined + ? { messageId: String(id) } + : { messageId: String(id), authorId: String(author), isSelf: String(author) === context.botId } +} + +/** The message of an update that carries one: `message` or `channel_post`. @internal */ +export const messageOf = (update: JsonObject): JsonObject | undefined => + object(update, "message") ?? object(update, "channel_post") + +/** The text of a message, verbatim, and its entities. @internal */ +export const textOf = ( + message: JsonObject +): { readonly text: string; readonly entities: ReadonlyArray } => { + const text = string(message, "text") + if (text !== undefined) return { text, entities: Array.isArray(message.entities) ? message.entities : [] } + const caption = string(message, "caption") ?? "" + return { text: caption, entities: Array.isArray(message.caption_entities) ? message.caption_entities : [] } +} + +/** + * The `MessageCreated` of an update's message, or undefined for one that + * gives none: an excluded context, a private (ephemeral) message, a message + * with no id, and a service message, with no text, caption, file or rich + * content. `invalid` is a message that should give one and can't be read. + * + * @internal + */ +export const messageCreated = ( + update: JsonObject, + context: Context +): ChatEvent.MessageCreated | "none" | "invalid" => { + const message = messageOf(update) + if (message === undefined || isExcluded(message) || integer(message, "message_id") === 0) return "none" + const { entities, text } = textOf(message) + const attachments = attachmentsOf(message) + if (text === "" && attachments.length === 0 && !("rich_message" in message)) return "none" + const at = threadOf(message) + const id = messageIdOf(message) + const author = authorOf(message, context) + const date = integer(message, "date") + if (at === undefined || id === undefined || author === undefined || date === undefined) return "invalid" + const replyTo = replyToOf(message, context) + return ChatEvent.MessageCreated.make({ + thread: at.thread, + kind: at.kind, + message: { + id, + thread: at.thread, + author, + text, + mentions: mentionsOf(text, entities, context), + ...(replyTo === undefined ? {} : { replyTo }), + attachments, + timestamp: DateTime.makeUnsafe(date * 1000), + raw: message + }, + raw: update + }) +} + +/** A command typed as a message: its lowercased name and what follows it. @internal */ +export interface Command { + readonly id: string + readonly text: string +} + +/** + * The command a message holds: its first entity is a `bot_command` at + * offset 0, naming this bot or no bot. `undefined` when a command names a + * bot and the bot's username is not known. + * + * @internal + */ +export const commandOf = (message: JsonObject, context: Context): Command | undefined => { + const { entities, text } = textOf(message) + const first = entities[0] + if (!isObject(first) || first.type !== "bot_command" || integer(first, "offset") !== 0) return undefined + const length = integer(first, "length") + if (length === undefined || length < 2 || length > text.length) return undefined + // The entity's length is Telegram's: it never covers a `-`, which `commandRequests` refuses. + const match = /^\/([A-Za-z0-9_-]{1,32})(?:@([A-Za-z0-9_]+))?$/.exec(text.slice(0, length)) + if (match === null) return undefined + if (match[2] !== undefined && match[2].toLowerCase() !== context.botUsername?.toLowerCase()) return undefined + let rest = text.slice(length) + if (rest.startsWith(" ") || rest.startsWith("\n")) rest = rest.slice(1) + return { id: match[1].toLowerCase(), text: rest } +} + +/** The date of an update's message or callback message, 0 when there is none. */ +const dateOf = (update: JsonObject): number => + integer(messageOf(update), "date") ?? integer(object(object(update, "callback_query"), "message"), "date") ?? 0 + +/** + * The delivery id of an update: `{botId}.{update_id}.{date}`, with `test.` + * in front in the test environment. + * + * @internal + */ +export const deliveryId = (update: unknown, context: Pick): string | undefined => { + if (!isObject(update)) return undefined + const id = integer(update, "update_id") + if (id === undefined) return undefined + return `${context.environment === "test" ? "test." : ""}${context.botId}.${id}.${dateOf(update)}` +} + +/** The events of an update, as `TelegramChat.normalize` gives them. @internal */ +export const normalize = (update: unknown, context: Context): ReadonlyArray => { + if (!isObject(update)) return [] + const event = messageCreated(update, context) + return typeof event === "string" ? [] : [event] +} diff --git a/packages/chat/telegram/src/internal/options.ts b/packages/chat/telegram/src/internal/options.ts new file mode 100644 index 00000000000..e25ba08f048 --- /dev/null +++ b/packages/chat/telegram/src/internal/options.ts @@ -0,0 +1,12 @@ +import * as Context from "effect/Context" + +/** @internal */ +export interface PostOptions { + readonly notify?: { readonly users?: ReadonlyArray | undefined } | undefined + readonly silent?: boolean | undefined +} + +/** @internal */ +export const CurrentPostOptions = Context.Reference("@effect/chat-telegram/CurrentPostOptions", { + defaultValue: () => ({}) +}) diff --git a/packages/chat/telegram/src/internal/polling.ts b/packages/chat/telegram/src/internal/polling.ts new file mode 100644 index 00000000000..73a7631ad57 --- /dev/null +++ b/packages/chat/telegram/src/internal/polling.ts @@ -0,0 +1,86 @@ +import type { ChatError } from "effect/chat/ChatError" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type { TelegramBotApi } from "../TelegramBotApi.ts" +import * as Errors from "./errors.ts" +import { integer, isObject } from "./normalize.ts" +import { allowedUpdates, type Outcome } from "./receive.ts" + +/** @internal */ +export interface Options { + readonly api: TelegramBotApi["Service"] + readonly timeout: Duration.Duration + readonly maxAttempts: number + readonly receive: (update: unknown) => Effect.Effect +} + +const isConflict = (error: ChatError) => error.reason._tag === "InvalidRequest" && error.reason.detail === "conflict" + +/** One `getUpdates`, from `offset`. @internal */ +export const getUpdates = (api: TelegramBotApi["Service"], offset: number | undefined, timeout: Duration.Duration) => + api.call("getUpdates", { + ...(offset === undefined ? {} : { offset }), + timeout: Math.floor(Duration.toSeconds(timeout)), + allowed_updates: allowedUpdates + }).pipe(Effect.map((result) => Array.isArray(result) ? result : [])) + +/** + * Runs one update, retrying `notAccepted` and `invalid` up to + * `maxAttempts` times with a backoff from one second. Then it is logged + * and skipped: `getUpdates` is one ordered queue, and retrying forever + * would stall every chat. + * + * @internal + */ +export const runUpdate = (update: unknown, options: Options): Effect.Effect => + Effect.gen(function*() { + for (let attempt = 1;; attempt++) { + const outcome = yield* options.receive(update) + if (outcome === "accepted" || outcome === "unhandled") return + if (attempt >= options.maxAttempts) { + return yield* Effect.logError("telegram polling: an update was not accepted, skipped").pipe( + Effect.annotateLogs({ "telegram.update_id": isObject(update) ? integer(update, "update_id") : undefined }) + ) + } + yield* Effect.sleep(Duration.seconds(2 ** (attempt - 1))) + } + }) + +/** + * The polling loop. `first` is the answer of the call the layer made + * itself. Each update's offset is confirmed by the next call only once the + * update was run, so a process that dies part way sees it again. + * + * @internal + */ +export const loop = (first: ReadonlyArray, options: Options): Effect.Effect => + Effect.gen(function*() { + let batch = first + let offset: number | undefined + for (;;) { + for (const update of batch) { + yield* runUpdate(update, options) + const id = isObject(update) ? integer(update, "update_id") : undefined + if (id !== undefined) offset = id + 1 + } + const next = yield* Effect.result(getUpdates(options.api, offset, options.timeout)) + if (next._tag === "Success") { + batch = next.success + continue + } + batch = [] + if (isConflict(next.failure)) { + return yield* Effect.logError( + "telegram polling: getUpdates answered 409, another poller or a webhook is active; polling stopped" + ) + } + yield* Effect.logWarning("telegram polling: getUpdates failed, retrying").pipe( + Effect.annotateLogs({ "chat.error.reason": next.failure.reason._tag }) + ) + yield* Effect.sleep(Duration.seconds(5)) + } + }) + +/** The error the layer fails with on a 409 at start. @internal */ +export const conflict = (error: ChatError): ChatError => + isConflict(error) ? Errors.chatError("layerPolling", Errors.invalidRequest("conflict")) : error diff --git a/packages/chat/telegram/src/internal/receive.ts b/packages/chat/telegram/src/internal/receive.ts new file mode 100644 index 00000000000..b387dadd8fa --- /dev/null +++ b/packages/chat/telegram/src/internal/receive.ts @@ -0,0 +1,273 @@ +import type { Capability, ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import type { NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Option from "effect/Option" +import * as Schema from "effect/Schema" +import type { TelegramBotApi } from "../TelegramBotApi.ts" +import * as Actions from "./actions.ts" +import type { Made } from "./adapter.ts" +import { originOf } from "./adapter.ts" +import * as Errors from "./errors.ts" +import { + commandOf, + type Context, + integer, + isEphemeral, + isExcluded, + isObject, + type JsonObject, + messageCreated, + messageIdOf, + messageOf, + object, + string, + threadOf +} from "./normalize.ts" + +/** + * What became of one update. + * + * - `accepted`: its event was accepted, or its interaction was answered. + * - `unhandled`: it gave nothing, and went to `onUnhandled`. + * - `notAccepted`: the `ChatHandler` refused its event, or it passed the deadline. + * - `invalid`: it should give an event and could not be read. + * + * @internal + */ +export type Outcome = "accepted" | "unhandled" | "notAccepted" | "invalid" + +/** @internal */ +export const defaults = { + deadline: Duration.seconds(10), + clickDeadline: Duration.millis(2500), + commandDeadline: Duration.seconds(10) +} as const + +/** The update types the adapter reads, for `allowed_updates`. @internal */ +export const allowedUpdates = ["message", "channel_post", "callback_query"] as const + +/** @internal */ +export interface Options { + readonly onUnhandled?: ((update: Schema.Json) => Effect.Effect) | undefined + readonly deadline?: Duration.Input | undefined + readonly clickDeadline?: Duration.Input | undefined + readonly commandDeadline?: Duration.Input | undefined +} + +/** @internal */ +export interface Environment { + readonly api: TelegramBotApi["Service"] + readonly capabilities: ReadonlySet + readonly answer: Made["answer"] + /** The bot's username, read once from `getMe`. */ + readonly username: Effect.Effect +} + +const isLate = Schema.is(ChatInteraction.Late) + +const duration = (input: Duration.Input | undefined, fallback: Duration.Duration) => + input === undefined ? fallback : Duration.fromInputUnsafe(input) + +const refused = ( + handler: ChatInteraction.Handler["Service"], + event: ChatInteraction.ButtonClicked | ChatInteraction.CommandInvoked, + error: ChatError +) => handler.refused(event, error) + +const answerQuery = (api: TelegramBotApi["Service"], id: string) => + api.call("answerCallbackQuery", { callback_query_id: id }, { write: true, retry: false }).pipe( + Effect.catch((error) => + Effect.logWarning("telegram: answerCallbackQuery failed").pipe( + Effect.annotateLogs({ "chat.error.reason": error.reason._tag }) + ) + ) + ) + +/** A callback the adapter leaves alone: inline mode, a game, or an excluded context. */ +const isForeignCallback = (query: JsonObject): boolean => { + const message = object(query, "message") + return message === undefined || typeof query.data !== "string" || isExcluded(message) +} + +/** + * The click of a `callback_query`, only when its `data` is the + * `callback_data` of a button in Telegram's own copy of the message: + * `data` comes from the client and can be anything. Undefined for an + * inaccessible message, a message with no keyboard, or a mismatch. + * + * @internal + */ +export const clickEvent = (update: JsonObject): ChatInteraction.ButtonClicked | undefined => { + const query = object(update, "callback_query") + const message = object(query, "message") + const data = string(query, "data") + if (query === undefined || message === undefined || data === undefined || isForeignCallback(query)) return undefined + const user = integer(object(query, "from"), "id") + const at = threadOf(message) + const messageId = messageIdOf(message) + if ( + integer(message, "date") === 0 || !Actions.keyboardData(message.reply_markup).has(data) || + user === undefined || at === undefined || messageId === undefined + ) return undefined + const parsed = Actions.parseData(data) + return ChatInteraction.ButtonClicked.make({ + id: parsed.id, + ...(parsed.value === undefined ? {} : { value: parsed.value }), + thread: at.thread, + messageId, + userId: String(user), + raw: update + }) +} + +/** A click. Every click is answered, since the client shows a progress bar until it is. */ +const click = ( + update: JsonObject, + id: string, + env: Environment, + options: Options, + handler: Option.Option +): Effect.Effect => + Effect.gen(function*() { + const answer = answerQuery(env.api, id) + if (Option.isNone(handler) || !env.capabilities.has("buttons")) return yield* answer + const event = clickEvent(update) + if (event === undefined) { + yield* Effect.logWarning("telegram: a click matched no button on its message, answered and dropped").pipe( + Effect.annotateLogs({ "chat.interaction.reason": "InvalidValue" }) + ) + return yield* answer + } + const reply = yield* Effect.provideService( + ChatInteraction.handle(event, duration(options.clickDeadline, defaults.clickDeadline)), + ChatInteraction.Handler, + handler.value + ) + yield* answer + if (!isLate(reply) && reply._tag === "OpenModal") { + yield* refused(handler.value, event, Errors.chatError("openModal", Errors.unsupported("modals"))) + } + }) + +/** The command of a message update, for this bot. @internal */ +export const commandEvent = (update: JsonObject, context: Context): ChatInteraction.CommandInvoked | undefined => { + const message = messageOf(update) + if (message === undefined || isExcluded(message)) return undefined + const found = commandOf(message, context) + const at = threadOf(message) + // As in `normalize`: an anonymous admin's `from` is Telegram's shared stand-in, the chat is the sender. + const user = integer(object(message, "sender_chat"), "id") ?? integer(object(message, "from"), "id") + if (found === undefined || at === undefined || user === undefined) return undefined + return ChatInteraction.CommandInvoked.make({ + id: found.id, + text: found.text, + thread: at.thread, + userId: String(user), + raw: update + }) +} + +/** + * A command. `false` when it was not handled: no handler, no + * `commands`, or an `Ack`, which means no definition matched. + */ +const command = ( + event: ChatInteraction.CommandInvoked, + env: Environment, + options: Options, + handler: Option.Option +): Effect.Effect => + Effect.gen(function*() { + if (Option.isNone(handler) || !env.capabilities.has("commands")) return false + const reply = yield* Effect.provideService( + ChatInteraction.handle(event, duration(options.commandDeadline, defaults.commandDeadline)), + ChatInteraction.Handler, + handler.value + ) + if (isLate(reply)) return true + switch (reply._tag) { + case "Ack": + return false + case "Defer": + return true + case "OpenModal": + yield* refused(handler.value, event, Errors.chatError("openModal", Errors.unsupported("modals"))) + return true + case "Respond": { + // No `ephemeral` in this version: a private answer is refused, never posted. + if (reply.visibility === "ephemeral") { + yield* refused(handler.value, event, Errors.chatError("respond", Errors.unsupported("ephemeral"))) + return true + } + const origin = originOf(event) + if (origin === undefined) return true + yield* env.answer("respond", origin, { + markdown: reply.markdown, + ...(reply.actions === undefined ? {} : { actions: reply.actions }) + }).pipe(Effect.catch((error) => refused(handler.value, event, error))) + return true + } + } + }) + +/** + * One update, as both transports run it. + * + * - A click goes to `ChatInteraction.Handler` and is answered. + * - An ephemeral message only ever takes the command path. Whatever its + * answer, it never reaches `ChatHandler` or `onUnhandled`. + * - A command goes to `ChatInteraction.Handler`. One answered `Ack` + * continues as an ordinary message. + * - A message with content becomes `MessageCreated` for `ChatHandler`, + * within `deadline`. + * - Anything else, including the excluded contexts, goes to `onUnhandled`. + * + * @internal + */ +export const receive = ( + update: unknown, + env: Environment, + options: Options, + handle: (event: ChatEvent.ChatEvent) => Effect.Effect +): Effect.Effect => + Effect.gen(function*() { + if (!isObject(update) || integer(update, "update_id") === undefined) return "invalid" + const unhandled = Effect.as(options.onUnhandled?.(update) ?? Effect.void, "unhandled" as const) + const handler = yield* Effect.serviceOption(ChatInteraction.Handler) + const query = object(update, "callback_query") + if (query !== undefined) { + if (isForeignCallback(query)) { + // A business chat's button is answered, so its client stops waiting, but never dispatched. + const id = string(query, "id") + const message = object(query, "message") + if (id !== undefined && message !== undefined && isExcluded(message)) yield* answerQuery(env.api, id) + return yield* unhandled + } + const id = string(query, "id") + if (id === undefined) return "invalid" + return yield* Effect.as(click(update, id, env, options, handler), "accepted" as const) + } + const message = messageOf(update) + if (message === undefined || isExcluded(message)) return yield* unhandled + const username = yield* Effect.option(env.username) + if (Option.isNone(username)) return "invalid" + const context: Context = { botId: env.api.botId, botUsername: username.value, environment: env.api.environment } + const found = commandEvent(update, context) + if (isEphemeral(message)) { + if (found !== undefined) yield* command(found, env, options, handler) + return "accepted" + } + if (integer(message, "message_id") === 0) return yield* unhandled + if (found !== undefined && (yield* command(found, env, options, handler))) return "accepted" + const event = messageCreated(update, context) + if (event === "none") return yield* unhandled + if (event === "invalid") return "invalid" + return yield* handle(event).pipe( + Effect.timeoutOption(duration(options.deadline, defaults.deadline)), + Effect.map(Option.match({ onNone: () => "notAccepted" as const, onSome: () => "accepted" as const })), + Effect.catchTag("NotAccepted", () => Effect.succeed("notAccepted" as const)) + ) + }) diff --git a/packages/chat/telegram/src/internal/webhook.ts b/packages/chat/telegram/src/internal/webhook.ts new file mode 100644 index 00000000000..a34f9c77730 --- /dev/null +++ b/packages/chat/telegram/src/internal/webhook.ts @@ -0,0 +1,101 @@ +import { timingSafeEqual } from "effect/crypto/Mac" +import * as Effect from "effect/Effect" +import type * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as HttpServerResponse from "effect/http/HttpServerResponse" +import * as Option from "effect/Option" +import * as Stream from "effect/Stream" +import type { Outcome } from "./receive.ts" + +/** The header Telegram sends the webhook secret in. @internal */ +export const secretHeader = "x-telegram-bot-api-secret-token" + +/** Telegram's rule for a secret, with the adapter's floor of 32 characters. @internal */ +export const validSecret = (secret: string): boolean => /^[A-Za-z0-9_-]{32,256}$/.test(secret) + +const encoder = new TextEncoder() + +/** Whether a header holds the secret, compared in constant time. @internal */ +export const checkSecret = (header: string | undefined, secret: Uint8Array): boolean => + header !== undefined && timingSafeEqual(encoder.encode(header), secret) + +/** The body, or `None` once it passes `maxBytes`. @internal */ +export const readBody = ( + request: HttpServerRequest.HttpServerRequest, + maxBytes: number +): Effect.Effect> => { + const length = request.headers["content-length"] + if (length !== undefined && Number(length) > maxBytes) return Effect.succeed(Option.none()) + return Effect.gen(function*() { + const chunks: Array = [] + let total = 0 + const failed = yield* request.stream.pipe( + Stream.takeUntil((chunk) => { + total += chunk.length + chunks.push(chunk) + return total > maxBytes + }), + Stream.runDrain, + Effect.as(false), + Effect.catch(() => Effect.succeed(true)) + ) + if (failed || total > maxBytes) return Option.none() + const body = new Uint8Array(total) + let offset = 0 + for (const chunk of chunks) { + body.set(chunk, offset) + offset += chunk.length + } + return Option.some(body) + }) +} + +const status = (code: number) => HttpServerResponse.empty({ status: code }) + +/** What is logged with each 503. @internal */ +export const unavailable = { + invalid: "telegram webhook: the update could not be read, answered 503", + notAccepted: "telegram webhook: the handler refused the event or passed the deadline, answered 503" +} as const + +/** + * One webhook request. + * + * - 401 without the secret, before the body is read. 413 past + * `maxBodyBytes`, 400 for a body that is not an update. + * - 200 once the update was accepted, answered or sent to `onUnhandled`. + * - 503 when its event was not accepted in time, or could not be read. + * + * @internal + */ +export const handleRequest = ( + request: HttpServerRequest.HttpServerRequest, + options: { + readonly secret: Uint8Array + readonly maxBodyBytes: number + readonly receive: (update: unknown) => Effect.Effect + } +): Effect.Effect => + Effect.gen(function*() { + if (!checkSecret(request.headers[secretHeader], options.secret)) return status(401) + const body = yield* readBody(request, options.maxBodyBytes) + if (Option.isNone(body)) return status(413) + let update: unknown + try { + update = JSON.parse(new TextDecoder().decode(body.value)) + } catch { + return status(400) + } + if (typeof update !== "object" || update === null || typeof (update as any).update_id !== "number") { + return status(400) + } + const outcome = yield* options.receive(update) + switch (outcome) { + case "accepted": + case "unhandled": + return status(200) + case "invalid": + return yield* Effect.as(Effect.logWarning(unavailable.invalid), status(503)) + case "notAccepted": + return yield* Effect.as(Effect.logWarning(unavailable.notAccepted), status(503)) + } + }) diff --git a/packages/chat/telegram/test/TelegramChat.test.ts b/packages/chat/telegram/test/TelegramChat.test.ts new file mode 100644 index 00000000000..8351fd93894 --- /dev/null +++ b/packages/chat/telegram/test/TelegramChat.test.ts @@ -0,0 +1,405 @@ +/** + * The adapter's own guarantees, over a fake Bot API, that the conformance + * suite does not see: credentials stay out of errors, spans and logs, the + * webhook's answers, private answers, clicks, excluded contexts, delivery + * keys, polling and deadlines. + */ +import * as TelegramChat from "@effect/chat-telegram/TelegramChat" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type { ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler, NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Deferred from "effect/Deferred" +import * as Effect from "effect/Effect" +import * as Exit from "effect/Exit" +import * as Fiber from "effect/Fiber" +import * as Headers from "effect/http/Headers" +import * as HttpMiddleware from "effect/http/HttpMiddleware" +import * as HttpRouter from "effect/http/HttpRouter" +import * as Layer from "effect/Layer" +import * as Logger from "effect/Logger" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as TestClock from "effect/testing/TestClock" +import * as Tracer from "effect/Tracer" +import { + apiError, + botId, + callbackUpdate, + chatId, + commandUpdate, + context, + type FakeApi, + makeApi, + messageUpdate, + telegramLayer, + thread, + token +} from "./utils.ts" + +const secret = "s".repeat(32) + +/** What reached the consumer, by kind. */ +const makeRecorder = (options?: { + readonly command?: () => Effect.Effect + readonly click?: Effect.Effect + readonly handle?: Effect.Effect +}) => { + const events: Array = [] + const clicks: Array = [] + const commands: Array = [] + const refused: Array = [] + const unhandled: Array = [] + const handler = ChatHandler.of({ + handle: (event) => options?.handle ?? Effect.sync(() => void events.push(event)) + }) + const interactions = ChatInteraction.Handler.of({ + click: (event) => Effect.as(options?.click ?? Effect.sync(() => void clicks.push(event)), ChatInteraction.ack), + submit: () => Effect.succeed(ChatInteraction.close), + command: (event) => + Effect.andThen( + Effect.sync(() => void commands.push(event)), + options?.command?.() ?? Effect.succeed(ChatInteraction.ack) + ), + refused: (_, error) => Effect.sync(() => void refused.push(error)) + }) + const onUnhandled = (update: Schema.Json) => Effect.sync(() => void unhandled.push(update)) + return { events, clicks, commands, refused, unhandled, handler, interactions, onUnhandled } +} + +type Recorder = ReturnType + +/** Runs updates through `TelegramChat.receive`, as the transports do. */ +const receive = (api: FakeApi, recorder: Recorder, ...updates: ReadonlyArray) => + Effect.gen(function*() { + const telegram = yield* TelegramChat.TelegramChat + const outcomes: Array = [] + for (const update of updates) { + outcomes.push(yield* telegram.receive(update, { onUnhandled: recorder.onUnhandled })) + } + return outcomes + }).pipe( + Effect.provideService(ChatHandler, recorder.handler), + Effect.provideService(ChatInteraction.Handler, recorder.interactions), + Effect.provide(telegramLayer(api)) + ) + +const spanTracer = () => { + const spans: Array = [] + const tracer = Tracer.make({ + span(options) { + const span = new Tracer.NativeSpan(options) + spans.push(span) + return span + } + }) + return { spans, tracer } +} + +/** What makes a message private: id 0, with its own private id. */ +const ephemeral = { message_id: 0, ephemeral_message_id: 7 } + +/** A click on `data`, on a message whose keyboard has one `approve` button. */ +const click = (data: string, date = 1_800_000_000) => + callbackUpdate({ + message_id: 5, + date, + chat: { id: chatId, type: "supergroup" }, + text: "x", + reply_markup: { inline_keyboard: [[{ text: "Yes", callback_data: "approve" }]] } + }, data) + +/** Yields until `done` holds. */ +const until = Effect.fnUntraced(function*(done: () => boolean) { + while (!done()) yield* Effect.yieldNow +}) + +it.effect("keeps the token out of errors, spans and logs", () => + Effect.gen(function*() { + const api = makeApi() + const { spans, tracer } = spanTracer() + const logs: Array = [] + const logger = Logger.formatJson.pipe(Logger.map((line): void => void logs.push(line))) + api.next.set("sendRichMessage", apiError(401, "Unauthorized")) + const exit = yield* Effect.flatMap(Chat.Chat, (chat) => chat.post(thread, { markdown: "hi" })).pipe( + Effect.tapCause((cause) => Effect.logError("post failed", cause)), + Effect.exit, + Effect.provide(telegramLayer(api)), + Effect.provide(Logger.layer([logger])), + Effect.provideService(Tracer.Tracer, tracer) + ) + assert.isTrue(Exit.isFailure(exit)) + assert.isTrue(spans.some((span) => span.name === "TelegramBotApi.sendRichMessage")) + const seen = [ + JSON.stringify(exit), + ...logs, + ...spans.map((span) => JSON.stringify([span.name, [...span.attributes], span.events])) + ].join("\n") + assert.isFalse(seen.includes(token.split(":")[1])) + })) + +it.effect("maps the rich message errors captured in live check 10", () => + Effect.gen(function*() { + const captured: ReadonlyArray = [ + ["RICH_MESSAGE_TOO_LARGE", "TooLarge", "message_too_large"], + ["RICH_MESSAGE_DEPTH_INVALID", "InvalidRequest", "too_deep"] + ] + for (const [description, tag, detail] of captured) { + const api = makeApi() + api.next.set("sendRichMessage", apiError(400, `Bad Request: ${description}`)) + const error = yield* Effect.flatMap(Chat.Chat, (chat) => chat.post(thread, { markdown: "hi" })).pipe( + Effect.flip, + Effect.provide(telegramLayer(api)) + ) + assert.deepStrictEqual([error.reason._tag, (error.reason as { readonly detail?: string }).detail], [tag, detail]) + } + })) + +describe("layerWebhook", () => { + const webhook = Effect.fnUntraced(function*(recorder: Recorder, server: Layer.Layer = Layer.empty) { + const app = TelegramChat.layerWebhook({ path: "/telegram", secretToken: Redacted.make(secret), maxBodyBytes: 4096 }) + .pipe( + Layer.provide(telegramLayer(makeApi())), + Layer.provide(Layer.succeed(ChatHandler, recorder.handler)), + Layer.provideMerge(server) + ) + const { dispose, handler } = HttpRouter.toWebHandler(app, { disableLogger: true }) + yield* Effect.addFinalizer(() => Effect.promise(() => dispose())) + return (body: string, header?: string) => + Effect.promise(() => + handler( + new Request("http://localhost/telegram", { + method: "POST", + body, + headers: header === undefined ? {} : { [TelegramChat.secretHeader]: header } + }) + ) + ).pipe(Effect.map((response) => response.status)) + }) + const update = JSON.stringify(messageUpdate({ text: "hi" })) + + it.effect("needs the secret, then answers for the body", () => + Effect.gen(function*() { + const recorder = makeRecorder() + const send = yield* webhook(recorder) + assert.strictEqual(yield* send(update), 401) + assert.strictEqual(yield* send(update, "t".repeat(32)), 401) + assert.strictEqual(recorder.events.length, 0) + assert.strictEqual(yield* send("x".repeat(5000), secret), 413) + assert.strictEqual(yield* send("{", secret), 400) + assert.strictEqual(yield* send(JSON.stringify({ message: {} }), secret), 400) + assert.strictEqual(yield* send(update, secret), 200) + assert.strictEqual(recorder.events.length, 1) + })) + + it.effect("answers 503 when the handler does not accept", () => + Effect.gen(function*() { + const send = yield* webhook(makeRecorder({ handle: Effect.fail(new NotAccepted({})) })) + assert.strictEqual(yield* send(update, secret), 503) + })) + + it.effect("keeps the secret out of server spans that capture every header, set up as the README says", () => + Effect.gen(function*() { + const { spans, tracer } = spanTracer() + const RedactSecret = Layer.effect( + Headers.CurrentRedactedNames, + Effect.map(Headers.CurrentRedactedNames, (names) => [...names, TelegramChat.secretHeader]) + ) + const send = yield* webhook( + makeRecorder(), + Layer.mergeAll( + RedactSecret, + Layer.succeed(HttpMiddleware.TracerHeaderFilter, () => true), + Layer.succeed(Tracer.Tracer, tracer) + ) + ) + assert.strictEqual(yield* send(update, secret), 200) + const attributes = new Map(spans.find((span) => span.kind === "server")?.attributes) + assert.strictEqual(attributes.get("http.request.header.x-telegram-bot-api-secret-token"), "") + assert.isFalse(JSON.stringify(spans.map((span) => [...span.attributes])).includes(secret)) + })) + + it.effect("refuses a short secret", () => + Effect.gen(function*() { + const app = TelegramChat.layerWebhook({ path: "/telegram", secretToken: Redacted.make("short") }).pipe( + Layer.provide(telegramLayer(makeApi())), + Layer.provide(Layer.succeed(ChatHandler, makeRecorder().handler)), + Layer.provide(HttpRouter.layer) + ) + assert.isTrue(Exit.isFailure(yield* Layer.build(app).pipe(Effect.scoped, Effect.exit))) + })) +}) + +describe("private messages", () => { + const command = commandUpdate("deploy", "", ephemeral) + + it.effect("refuses an ephemeral answer and never posts it", () => + Effect.gen(function*() { + const api = makeApi() + const recorder = makeRecorder({ command: () => Effect.succeed(ChatInteraction.respond({ markdown: "secret" })) }) + assert.deepStrictEqual(yield* receive(api, recorder, command), ["accepted"]) + assert.deepStrictEqual(recorder.refused.map((error) => error.reason._tag), ["Unsupported"]) + assert.strictEqual(api.sent("sendRichMessage").length, 0) + assert.strictEqual(recorder.events.length + recorder.unhandled.length, 0) + })) + + it.effect("answers an ephemeral command in public without quoting it", () => + Effect.gen(function*() { + const api = makeApi() + const recorder = makeRecorder({ + command: () => Effect.succeed(ChatInteraction.respond({ markdown: "done", visibility: "public" })) + }) + yield* receive(api, recorder, command) + const [sent] = api.sent("sendRichMessage") + assert.strictEqual(sent.chat_id, chatId) + assert.isUndefined(sent.reply_parameters) + })) + + it.effect("keeps an ephemeral message that is not a command from every handler", () => + Effect.gen(function*() { + const recorder = makeRecorder() + yield* receive(makeApi(), recorder, messageUpdate({ text: "psst", ...ephemeral })) + assert.strictEqual(recorder.events.length + recorder.unhandled.length + recorder.commands.length, 0) + })) +}) + +it.effect("dispatches only data from Telegram's copy of the keyboard, and answers every click", () => + Effect.gen(function*() { + const api = makeApi() + const recorder = makeRecorder() + // A message Telegram can no longer show the bot comes with date 0. + yield* receive(api, recorder, click("approve"), click("forged"), click("approve", 0)) + assert.deepStrictEqual(recorder.clicks.map((click) => [click.id, click.messageId]), [["approve", "5"]]) + assert.strictEqual(api.sent("answerCallbackQuery").length, 3) + })) + +it.effect("sends business and channel direct-message updates to onUnhandled, and answers their clicks", () => + Effect.gen(function*() { + const api = makeApi() + const recorder = makeRecorder() + const business = { message_id: 3, date: 1, chat: { id: 1, type: "private" }, business_connection_id: "b" } + yield* receive( + api, + recorder, + messageUpdate({ text: "hi", business_connection_id: "b" }), + messageUpdate({ text: "hi", chat: { id: chatId, type: "supergroup", is_direct_messages: true } }), + callbackUpdate({ ...business, reply_markup: { inline_keyboard: [[{ text: "a", callback_data: "a" }]] } }, "a") + ) + assert.strictEqual(recorder.unhandled.length, 3) + assert.strictEqual(recorder.events.length + recorder.clicks.length, 0) + assert.strictEqual(api.sent("answerCallbackQuery").length, 1) + })) + +it("keys deliveries by bot, update and date, and by environment", () => { + const update = messageUpdate({ text: "hi" }) + const key = (environment?: "test") => Option.getOrThrow(TelegramChat.deliveryKey(update, { botId, environment })) + assert.strictEqual(key(), `${botId}.${update.update_id}.${update.message.date}`) + assert.strictEqual(key("test"), `test.${key()}`) +}) + +it.effect("confirms a polled update only after it ran", () => + Effect.gen(function*() { + const api = makeApi() + const recorder = makeRecorder() + const last = messageUpdate({ text: "two" }) + api.updates.push([messageUpdate({ text: "one" }), last]) + yield* Layer.build( + TelegramChat.layerPolling().pipe( + Layer.provideMerge(telegramLayer(api)), + Layer.provide(Layer.succeed(ChatHandler, recorder.handler)) + ) + ) + yield* until(() => api.sent("getUpdates").length >= 2) + const [start, next] = api.sent("getUpdates") + assert.isUndefined(start.offset) + assert.strictEqual(next.offset, last.update_id + 1) + assert.strictEqual(recorder.events.length, 2) + })) + +describe("deadlines", () => { + it.effect("answers a click at its deadline and lets its handler run on, so a late commit is never repeated", () => + Effect.gen(function*() { + const api = makeApi() + const release = yield* Deferred.make() + const steps: Array = [] + const recorder = makeRecorder({ + click: Effect.andThen( + Effect.sync(() => void steps.push("started")), + Effect.andThen(Deferred.await(release), Effect.sync(() => void steps.push("committed"))) + ) + }) + const fiber = yield* Effect.forkChild(receive(api, recorder, click("approve"))) + yield* until(() => steps.length > 0) + yield* TestClock.adjust("2499 millis") + assert.strictEqual(api.sent("answerCallbackQuery").length, 0) + yield* TestClock.adjust("1 millis") + assert.deepStrictEqual(yield* Fiber.join(fiber), ["accepted"]) + assert.strictEqual(api.sent("answerCallbackQuery").length, 1) + yield* Deferred.succeed(release, void 0) + yield* until(() => steps.length > 1) + assert.deepStrictEqual(steps, ["started", "committed"]) + })) + + it.effect("refuses a message whose handler outlives its deadline, so Telegram redelivers it", () => + Effect.gen(function*() { + let started = false + const recorder = makeRecorder({ handle: Effect.andThen(Effect.sync(() => void (started = true)), Effect.never) }) + const fiber = yield* Effect.forkChild(receive(makeApi(), recorder, messageUpdate({ text: "hi" }))) + yield* until(() => started) + yield* TestClock.adjust("9999 millis") + assert.isUndefined(fiber.pollUnsafe()) + yield* TestClock.adjust("1 millis") + assert.deepStrictEqual(yield* Fiber.join(fiber), ["notAccepted"]) + })) + + it.effect("interrupts a command at its deadline and still counts it answered, never as a message", () => + Effect.gen(function*() { + const steps: Array = [] + const recorder = makeRecorder({ + command: () => Effect.onInterrupt(Effect.never, () => Effect.sync(() => void steps.push("interrupted"))) + }) + const fiber = yield* Effect.forkChild(receive(makeApi(), recorder, commandUpdate("deploy", "prod"))) + yield* until(() => recorder.commands.length > 0) + yield* TestClock.adjust("9999 millis") + assert.isUndefined(fiber.pollUnsafe()) + yield* TestClock.adjust("1 millis") + assert.deepStrictEqual(yield* Fiber.join(fiber), ["accepted"]) + yield* until(() => steps.length > 0) + assert.strictEqual(recorder.events.length + recorder.unhandled.length, 0) + })) +}) + +it.effect("gives an anonymous admin's command the group as its user", () => + Effect.gen(function*() { + const recorder = makeRecorder() + // Telegram's stand-in `from` for anonymous group admins. + const anonymous = { id: 1087968824, is_bot: true, first_name: "Group", username: "GroupAnonymousBot" } + yield* receive( + makeApi(), + recorder, + commandUpdate("deploy", "", { from: anonymous, sender_chat: { id: -1001, type: "supergroup", title: "G" } }) + ) + assert.deepStrictEqual(recorder.commands.map((command) => command.userId), ["-1001"]) + })) + +it("gives an animation one attachment, not its document copy too", () => { + const file = { file_id: "anim", file_unique_id: "u", mime_type: "video/mp4" } + const [event] = TelegramChat.normalize( + messageUpdate({ animation: { ...file, width: 1, height: 1, duration: 1 }, document: file }), + context + ) + assert.deepStrictEqual(event._tag === "MessageCreated" ? event.message.attachments.map((a) => a.id) : [], ["anim"]) +}) + +it.effect("commandRequests refuses ids Telegram can't register", () => + Effect.gen(function*() { + const ok = ChatInteraction.group(ChatInteraction.command("deploy", { description: "Deploy" })) + const bad = ChatInteraction.group(ChatInteraction.command("ship-it", { description: "Ship" })) + assert.deepStrictEqual(yield* TelegramChat.commandRequests(ok), { + commands: [{ command: "deploy", description: "Deploy" }] + }) + assert.isTrue(Exit.isFailure(yield* Effect.exit(TelegramChat.commandRequests(bad)))) + })) diff --git a/packages/chat/telegram/test/conformance.test.ts b/packages/chat/telegram/test/conformance.test.ts new file mode 100644 index 00000000000..75cbb5209b0 --- /dev/null +++ b/packages/chat/telegram/test/conformance.test.ts @@ -0,0 +1,29 @@ +/** + * The conformance suite against the adapter with every `unverified` + * capability on, over a fake Bot API that keeps what was sent. Clicks and + * commands are built from what was sent and go through + * `TelegramChat.receive`. + */ +import * as TelegramChat from "@effect/chat-telegram/TelegramChat" +import { describe, it } from "@effect/vitest" +import { conformance } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { makeApi, makeInteractions, telegramLayer, thread, userPosts } from "./utils.ts" + +const options = { platform: "telegram", thread, userPosts, parseMention: TelegramChat.parseMention } + +describe("conformance over a fake Bot API", () => { + const names = Object.keys(conformance({ + ...options, + interactions: { click: () => Effect.void, submit: () => Effect.void, command: () => Effect.void } + })) + + for (const name of names) { + it.effect(name, () => { + const api = makeApi() + const { bind, interactions } = makeInteractions(api) + const rules = conformance({ ...options, interactions }) + return Effect.andThen(bind, rules[name]).pipe(Effect.provide(telegramLayer(api))) + }) + } +}) diff --git a/packages/chat/telegram/test/gfm.ts b/packages/chat/telegram/test/gfm.ts new file mode 100644 index 00000000000..556e6ab4639 --- /dev/null +++ b/packages/chat/telegram/test/gfm.ts @@ -0,0 +1,111 @@ +import type * as Mdast from "mdast" +import { fromMarkdown } from "mdast-util-from-markdown" +import { gfmFromMarkdown } from "mdast-util-gfm" +import { gfm } from "micromark-extension-gfm" + +/** What a GFM parse of rendered Markdown holds that the renderer promises it never will. */ +export interface Findings { + readonly html: ReadonlyArray + readonly images: number + readonly links: ReadonlyArray + readonly maxDepth: number + readonly maxColumns: number + readonly blocks: number + /** Text that reached the parser with an unescaped `|`, `$` or `=`, Telegram's spoiler, math and mark syntax. */ + readonly active: ReadonlyArray + /** + * Text holding a backslash escape Telegram does not honor, which it shows + * as a backslash and then reads the character as syntax (live check 2). + */ + readonly unhonored: ReadonlyArray + /** + * The most inline nodes in one paragraph, as the renderer counts them: + * code spans, links and references written with `[` or `<`, and footnote + * references at three each. Bare URLs are not counted. + */ + readonly inline: number +} + +/** Punctuation whose backslash escape Telegram honors. */ +const honored = /[!#$()*+\-.=>[\\_`{|}~]/ + +const nesting = new Set(["blockquote", "listItem", "emphasis", "strong", "delete"]) +const blockTypes = new Set([ + "paragraph", + "heading", + "code", + "thematicBreak", + "listItem", + "tableRow", + "blockquote", + "html" +]) + +export const inspect = (markdown: string): Findings => { + const html: Array = [] + const links: Array = [] + let images = 0 + let maxDepth = 0 + let maxColumns = 0 + let blocks = 0 + const active: Array = [] + const unhonored: Array = [] + let inline = 0 + /** The inline nodes under `node`, as `Findings.inline` counts them. */ + const count = (node: Mdast.Nodes): number => { + const own = node.type === "inlineCode" + ? 1 + : node.type === "footnoteReference" + ? 3 + : (node.type === "link" || node.type === "linkReference") && + /[[<]/.test(markdown[node.position?.start.offset ?? -1] ?? "") + ? 1 + : 0 + return "children" in node + ? node.children.reduce((sum, child) => sum + count(child as Mdast.Nodes), own) + : own + } + const visit = (node: Mdast.Nodes, depth: number) => { + const d = nesting.has(node.type) ? depth + 1 : depth + maxDepth = Math.max(maxDepth, d) + if (blockTypes.has(node.type)) blocks++ + if (node.type === "paragraph" || node.type === "heading" || node.type === "tableCell") { + inline = Math.max(inline, count(node)) + } + switch (node.type) { + case "text": { + if (node.position === undefined) break + const source = markdown.slice(node.position.start.offset, node.position.end.offset) + if (/(?:^|[^\\])(?:\\\\)*[|$=]/.test(source)) active.push(source) + // One pass, so that an escape right after another is still seen. + for (let i = 0; i < source.length; i++) { + if (source[i] !== "\\") continue + const next = source[++i] + if (next !== undefined && /[!-/:-@[-`{-~]/.test(next) && !honored.test(next)) unhonored.push(source) + } + break + } + case "html": + html.push(node.value) + break + case "image": + case "imageReference": + images++ + break + case "link": + links.push(node.url) + // An autolink's text is its URL, which no parser reads as syntax. + if (markdown[node.position?.start.offset ?? -1] === "<") return + break + case "definition": + links.push(node.url) + break + case "table": + for (const row of node.children) maxColumns = Math.max(maxColumns, row.children.length) + break + } + if ("children" in node) { for (const child of node.children) visit(child as Mdast.Nodes, d) } + } + visit(fromMarkdown(markdown, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] }), 0) + return { html, images, links, maxDepth, maxColumns, blocks, active, unhonored, inline } +} diff --git a/packages/chat/telegram/test/live.test.ts b/packages/chat/telegram/test/live.test.ts new file mode 100644 index 00000000000..42e2e40fb37 --- /dev/null +++ b/packages/chat/telegram/test/live.test.ts @@ -0,0 +1,73 @@ +/** + * The conformance suite against the real Bot API. Opt-in: it runs only when + * all three of these are set, so CI skips it. + * + * - `TELEGRAM_LIVE_BOT_TOKEN` and `TELEGRAM_LIVE_CHAT`: the bot and a group + * it may write to, as a chat id. + * - `TELEGRAM_LIVE_BOT2_TOKEN`: a second bot in the group, for the rules + * that need another identity's message. Its message is sent with + * `sendMessage` and normalised as an inbound update, since bots never + * receive each other's messages. + * + * Only the default capabilities run: files, buttons and commands stay off + * until their own live checks pass. + */ +import * as TelegramBotApi from "@effect/chat-telegram/TelegramBotApi" +import * as TelegramChat from "@effect/chat-telegram/TelegramChat" +import { describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { conformance } from "effect/chat/ChatTest" +import type * as ChatThread from "effect/chat/ChatThread" +import * as Effect from "effect/Effect" +import * as FetchHttpClient from "effect/http/FetchHttpClient" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import * as Semaphore from "effect/Semaphore" + +const botToken = process.env.TELEGRAM_LIVE_BOT_TOKEN +const secondToken = process.env.TELEGRAM_LIVE_BOT2_TOKEN +const chat = process.env.TELEGRAM_LIVE_CHAT + +describe.skipIf(botToken === undefined || secondToken === undefined || chat === undefined)("live conformance", () => { + const layer = TelegramChat.layer({ token: Redacted.make(botToken!) }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(FetchHttpClient.layer) + ) + + /** Posts as the second bot and normalises the answer as the update the first bot would get. */ + const userPosts = (thread: ChatThread.ChatThread, text: string): Effect.Effect => + Effect.gen(function*() { + const parts = TelegramChat.parts(thread) + if (parts._tag === "None") return yield* Effect.die(new Error("not a Telegram thread")) + const second = yield* TelegramBotApi.make({ token: Redacted.make(secondToken!) }) + const message = yield* second.call("sendMessage", { + chat_id: Number(parts.value.chatId), + ...(parts.value.messageThreadId === undefined + ? {} + : { message_thread_id: Number(parts.value.messageThreadId) }), + text + }, { write: true }) + const bot = yield* TelegramBotApi.TelegramBotApi + const me = yield* bot.call("getMe") + const events = TelegramChat.normalize({ update_id: 1, message }, { + botId: bot.botId, + botUsername: (me as { readonly username: string }).username + }) + if (events.length !== 1) return yield* Effect.die(new Error("userPosts did not normalise to one event")) + return events[0] + }).pipe(Effect.provide(layer), Effect.provide(FetchHttpClient.layer), Effect.orDie) + + const rules = conformance({ + platform: "telegram", + thread: TelegramChat.thread(chat ?? "0"), + userPosts, + parseMention: TelegramChat.parseMention + }) + + // The rules share one group and Telegram paces group writes, so one rule at a time. + const one = Semaphore.makeUnsafe(1) + for (const [name, rule] of Object.entries(rules)) { + it.live(name, () => rule.pipe(Effect.provide(layer), Semaphore.withPermits(one, 1)), 600_000) + } +}) diff --git a/packages/chat/telegram/test/markdown.test.ts b/packages/chat/telegram/test/markdown.test.ts new file mode 100644 index 00000000000..152ccadd6e1 --- /dev/null +++ b/packages/chat/telegram/test/markdown.test.ts @@ -0,0 +1,198 @@ +/** + * The rendered Markdown stays within what `measure` charged and Telegram's + * limits, and carries no raw HTML, image, math, spoiler or link outside + * http, https and mailto. A GFM parser reads the output; live check 2 + * confirms Telegram's parser agrees with it. + */ +import { + maxColumns, + maxDepth, + maxFootnoteReferences, + maxInlineNodes, + measure, + messageLength, + render, + reservation +} from "@effect/chat-telegram/internal/markdown" +import { assert, describe, it } from "@effect/vitest" +import * as fc from "fast-check" +import { inspect } from "./gfm.ts" + +/** The users whose `tg://user` links stay links. */ +const mentions = new Set(["2"]) + +/** Fails with what a GFM parse of `render(input)` finds that the renderer promises it never holds. */ +const assertInert = (input: string) => { + const out = render(input, { mentions }) + const found = inspect(out) + const context = JSON.stringify(input) + " rendered " + JSON.stringify(out) + assert.deepStrictEqual(found.html, [], context) + assert.strictEqual(found.images, 0, context) + assert.deepStrictEqual( + found.links.filter((url) => !/^(?:https?:\/\/|mailto:)/i.test(url) && url !== "tg://user?id=2"), + [], + context + ) + assert.deepStrictEqual(found.active, [], context) + assert.deepStrictEqual(found.unhonored, [], context) + assert.isAtMost(found.maxDepth, maxDepth, context) + assert.isAtMost(found.maxColumns, maxColumns, context) + assert.isAtMost(found.inline, maxInlineNodes, context) + // The most blocks Telegram renders in one message. + if (measure(input) <= messageLength) assert.isAtMost(found.blocks, 500, context) +} + +describe("render", () => { + it("escapes the hostile corpus", () => { + // Live check 2 found that Telegram ignores the escapes `\]` and `\<`, reads + // destinations verbatim, reads definitions more loosely than CommonMark + // and opens emphasis between spaces. + const cases: ReadonlyArray = [ + ["hi ", "<b>hi</b> <javascript:alert(1)>"], + ["![a](https://e.com/a.png) ![b][r]", "\\![a](https://e.com/a.png) \\![b][r]"], + ["[me](tg://user?id=1) [ok](tg://user?id=2)", "[me]\\(tg://user?id\\=1) [ok](tg://user?id=2)"], + ["| a | b |\n|---|---|\n| 1 | 2 |", "| a | b |\n|---|---|\n| 1 | 2 |"], + [">>>>>>>>>>>>>>>>> x", ">>>>>>>>>>>>>>>\\>\\> x"], + [ + "[x\\](tg://resolve?domain=telegram) \\x\\", + "\\[x](tg://resolve?domain\\=telegram) <b>x</b>" + ], + [ + "[a](https://e.com/?q=1&x=2 \"t\") [b](https://e.com/a_b*c\\(d\\))", + "[a](https://e.com/?q=1&x=2) [b](https://e.com/a_b*c%28d%29)" + ], + [ + "[a][b]: tg://x\n\n [c]: tg://x\n\ntext\n[d]: https://e.com", + "\\[a][b]: tg://x\n\n \\[c]: tg://x\n\ntext\n\\[d]: https://e.com" + ], + ["2 * 3 * 4 a _ b\n\n***", "2 \\* 3 \\* 4 a \\_ b\n\n***"], + // a definition is refused once, keeps no title, and loses no text + [ + "[a[b]: https://e.com\n\n[^ a]: https://e.com\n\n[c]: https://e.com \"t\"\n[d]: https://e.com d", + "\\[a[b]: https://e.com\n\n\\[^ a]: https://e.com\n\n[c]: https://e.com\n\\[d]: https://e.com d" + ], + // a label that renders to 999 characters stays a definition, one of 1,000 does not + ["[" + "$".repeat(499) + "]: mailto:x", "[" + "\\$".repeat(499) + "]: mailto:x"], + ["[" + "$".repeat(500) + "]: mailto:x", "\\[" + "\\$".repeat(500) + "]: mailto:x"], + // a destination keeps parentheses three deep, not four + ["[a](https://e.com/(((x))))", "[a](https://e.com/(((x))))"], + ["[a](https://e.com/((((x)))))", "[a]\\(https://e.com/((((x)))))"] + ] + for (const [input, expected] of cases) assert.strictEqual(render(input, { mentions }), expected) + }) + + it("leaves a GFM parser no HTML, image, foreign link, math, spoiler or excess nesting", () => { + for ( + const input of [ + // a fence may be indented up to three spaces + " ~~~\nx\n~~~\n[ping](tg://user?id=123) ", + // a heading ends a table + "| a | b |\n|---|---|\n# heading\n||secret||", + // a lazy continuation line stays in the containers it does not repeat + ">".repeat(15) + " x\n" + "_".repeat(14) + "x" + "_".repeat(14), + "2. <\n x_\n=", + // a footnote's text is no table + "[^a]: x|y\n---|---\n||secret||", + // nor a lone pipe a header + "|\n-|", + // a table goes on past a line it does not end at + "|a|\n|---|\n[a]: mailto:x\nb `|`", + // a line of U+00A0 is text, so it ends no paragraph + ">".repeat(15) + " x\n\u00a0\n" + "_".repeat(14) + "x" + "_".repeat(14), + // a reference may turn the outer link into text, and its destination into Markdown + "[a][inner](mailto:[pwn](tg://user?id=1))\n\n[inner]: https://safe.com", + // unbalanced parentheses end no destination + "[](mailto:$(> )", + // a code span or an autolink hides no label's end from CommonMark + "[>\\]: mailto:]:[^a]", + // indented, a footnote's label is a definition's + "[a]: mailto:x\n [^a]: tg://x", + // nor is an autolink with a control character one + "" + ] + ) assertInert(input) + const hostile = fc.string({ + unit: fc.constantFrom( + ..."<>$=|![]()*_~`#-+.:/\\&;\n \t\"'0a", + "~~~", + "```", + " ~~~", + "- ", + "> ", + "1. ", + "https://e.com", + "www.e.co", + "x@e.co", + "tg://user?id=1", + "", + "](", + "[^", + "[^a]: ", + "|---|", + "\\]", + "\\<", + "\\`", + "[a]", + "\n[a]: mailto:x\n", + // whitespace that is not a blank line to CommonMark + "\u00a0", + "\u3000", + "\v", + "\f" + ), + maxLength: 60 + }) + fc.assert(fc.property(hostile, assertInert), { numRuns: 500 }) + }) + + it("keeps output that fills the budget under 500 blocks", () => { + for (const line of ["- - - a\n", "1. a\n"]) { + let input = "" + while (measure(input + line) <= messageLength) input += line + assertInert(input) + } + }) + + it("caps the code spans, references and footnote references Telegram refuses past a limit", () => { + const spans = render("`a` ".repeat(maxInlineNodes + 10)) + assert.strictEqual(spans.match(/(? "x[^1]".repeat(100)).join("\n\n") + "\n\n[^1]: n") + assert.strictEqual(references.match(/(? { + const row = (cell: string) => "|" + ` ${cell} |`.repeat(21) + assert.isFalse(/(^|[^\\])\|/.test(render(row("c") + "\n" + row("-")))) + }) + + it("closes an open fence before the media references", () => { + assert.strictEqual(render("```\n", { files: 1 }), "```\n\n```\n\n![](tg://document?id=f1)") + }) + + it("never exceeds measure, with the reservation for files", () => { + const markdown = fc.string({ + unit: fc.oneof( + fc.constantFrom( + ..."<>$=|![]()*_~`#-+.:/\\&;\n\r \t0123456789abc", + "\u0000", + "é", + "€", + "😀", + "\ud800", + "tg://", + "https://" + ), + fc.string({ maxLength: 3 }) + ), + maxLength: 200 + }) + const bytes = (s: string) => new TextEncoder().encode(s).length + fc.assert(fc.property(markdown, fc.integer({ min: 0, max: 10 }), (input, files) => { + assert.isAtMost(bytes(render(input)), measure(input)) + assert.isAtMost(bytes(render(input, { files })), measure(input) + reservation) + })) + }) +}) diff --git a/packages/chat/telegram/test/utils.ts b/packages/chat/telegram/test/utils.ts new file mode 100644 index 00000000000..06790921d4b --- /dev/null +++ b/packages/chat/telegram/test/utils.ts @@ -0,0 +1,205 @@ +import * as TelegramChat from "@effect/chat-telegram/TelegramChat" +import * as Chat from "effect/chat/Chat" +import type { ChatEvent } from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import { type Interactions, NoSuchElement } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import type * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" + +/** A marker token: no error, span or log may contain it. */ +export const token = "123456789:AAH-marker_TOKEN_must_not_leak" +export const botId = "123456789" +const username = "effect_bot" +export const chatId = -1001234567890 +export const thread = TelegramChat.thread(chatId) +const user = { id: 4242, is_bot: false, first_name: "Ada" } + +const ok = (result: unknown) => ({ status: 200, body: { ok: true, result } }) + +/** A Bot API error answer, as Telegram sends it. */ +export const apiError = (code: number, description: string, parameters?: unknown) => ({ + status: code, + body: { ok: false, error_code: code, description, ...(parameters === undefined ? {} : { parameters }) } +}) + +type Answer = { readonly status: number; readonly body: unknown } + +const readBody = (request: HttpClientRequest.HttpClientRequest): any => { + const raw = request.body + if (raw._tag === "Uint8Array") return JSON.parse(new TextDecoder().decode(raw.body)) + if (raw._tag !== "FormData") return {} + const body: Record = {} + for (const [key, value] of raw.formData.entries()) { + if (typeof value !== "string") body[key] = value + else { + try { + body[key] = JSON.parse(value) + } catch { + body[key] = value + } + } + } + return body +} + +/** + * A Bot API that keeps the messages the adapter sends by id. `next` holds + * one-shot answers for a method. + */ +export const makeApi = () => { + const calls: Array<{ readonly method: string; readonly body: any }> = [] + const messages = new Map() + const next = new Map() + /** The batches `getUpdates` gives, in order. Once they run out it never answers. */ + const updates: Array> = [] + let count = 100 + + const answer = (method: string, body: any): Answer => { + switch (method) { + case "getMe": + return ok({ id: Number(botId), is_bot: true, first_name: "Effect", username }) + case "sendRichMessage": { + const id = ++count + messages.set(id, body) + return ok({ + message_id: id, + date: Math.floor(Date.now() / 1000), + chat: { id: body.chat_id, type: "supergroup", title: "Effect" }, + ...(body.reply_markup === undefined ? {} : { reply_markup: body.reply_markup }) + }) + } + case "editMessageText": + if (!messages.has(body.message_id)) return apiError(400, "Bad Request: message to edit not found") + messages.set(body.message_id, { ...messages.get(body.message_id), ...body }) + return ok(true) + case "deleteMessage": + if (!messages.delete(body.message_id)) return apiError(400, "Bad Request: message to delete not found") + return ok(true) + case "getChat": + return ok({ id: body.chat_id, type: "supergroup", title: "Effect" }) + case "getChatMember": + return ok({ status: "member", user: { ...user, id: body.user_id } }) + case "getFile": + return ok({ file_id: body.file_id, file_unique_id: "u", file_size: 5, file_path: "documents/file_1.txt" }) + case "answerCallbackQuery": + return ok(true) + default: + return apiError(404, "Not Found") + } + } + + const client = HttpClient.make((request, url) => + Effect.suspend(() => { + const file = /^\/file\/bot[^/]+\/(.+)$/.exec(url.pathname) + if (file !== null) { + calls.push({ method: "file", body: file[1] }) + return Effect.succeed(HttpClientResponse.fromWeb(request, new Response(new TextEncoder().encode("hello")))) + } + const method = url.pathname.split("/").pop()! + const body = readBody(request) + calls.push({ method, body }) + let scripted = next.get(method) + if (scripted !== undefined) next.delete(method) + if (scripted === undefined && method === "getUpdates") { + const batch = updates.shift() + if (batch === undefined) return Effect.never + scripted = ok(batch) + } + const { body: json, status } = scripted ?? answer(method, body) + return Effect.succeed(HttpClientResponse.fromWeb( + request, + new Response(JSON.stringify(json), { status, headers: { "content-type": "application/json" } }) + )) + }) + ) + + return { + calls, + messages, + next, + updates, + sent: (method: string) => calls.filter((call) => call.method === method).map((call) => call.body), + layer: Layer.succeed(HttpClient.HttpClient, client) + } +} + +export type FakeApi = ReturnType + +/** `Chat`, `Registry` and the adapter on a fake Bot API, with every capability on. */ +export const telegramLayer = (api: FakeApi) => + TelegramChat.layer({ token: Redacted.make(token), unverified: { files: true, buttons: true, commands: true } }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(api.layer) + ) + +let updateId = 900_000 + +/** An update with one message from `from` in the test supergroup. */ +export const messageUpdate = (message: Record, from: object = user) => { + const id = ++updateId + return { + update_id: id, + message: { + message_id: id, + date: Math.floor(Date.now() / 1000), + chat: { id: chatId, type: "supergroup", title: "Effect" }, + from, + ...message + } + } +} + +/** A command message, with its `bot_command` entity. */ +export const commandUpdate = (name: string, text = "", extra?: Record) => + messageUpdate({ + text: text === "" ? `/${name}` : `/${name} ${text}`, + entities: [{ type: "bot_command", offset: 0, length: name.length + 1 }], + ...extra + }) + +/** A click on `data` from `user`, on the message as Telegram keeps it. */ +export const callbackUpdate = (message: Record, data: string) => ({ + update_id: ++updateId, + callback_query: { id: "cq" + updateId, from: user, chat_instance: "1", data, message } +}) + +export const context = { botId, botUsername: username } + +/** The user's text, as `ChatTest.conformance` wants it. */ +export const userPosts = (_thread: unknown, text: string): Effect.Effect => + Effect.sync(() => TelegramChat.normalize(messageUpdate({ text }), context)[0]) + +const noHandler = Layer.succeed(ChatHandler, ChatHandler.of({ handle: () => Effect.void })) + +/** The conformance hook: clicks and commands go through `TelegramChat.receive`. */ +export const makeInteractions = (api: FakeApi) => { + let service: TelegramChat.TelegramChat["Service"] | undefined + const bind = Effect.tap(TelegramChat.TelegramChat, (telegram) => + Effect.sync(() => { + service = telegram + })) + const receive = (update: unknown) => service!.receive(update).pipe(Effect.provide(noHandler), Effect.asVoid) + const interactions: Interactions = { + click: Effect.fnUntraced(function*(messageId, id) { + const sent = api.messages.get(Number(messageId)) + const buttons: Array<{ readonly callback_data?: string }> = sent?.reply_markup?.inline_keyboard?.flat() ?? [] + const button = buttons.find((b) => b.callback_data?.split("\u001f")[0] === id) + if (button?.callback_data === undefined) return yield* Effect.fail(new NoSuchElement({ messageId, id })) + yield* receive(callbackUpdate({ + message_id: Number(messageId), + date: Math.floor(Date.now() / 1000), + chat: { id: sent.chat_id, type: "supergroup", title: "Effect" }, + from: { id: Number(botId), is_bot: true, first_name: "Effect" }, + text: "x", + reply_markup: sent.reply_markup + }, button.callback_data)) + }), + submit: () => Effect.void, + command: (_thread, id, options) => receive(commandUpdate(id, options?.text ?? "")) + } + return { bind, interactions } +} diff --git a/packages/chat/telegram/tsconfig.json b/packages/chat/telegram/tsconfig.json new file mode 100644 index 00000000000..e2a8ca19a0d --- /dev/null +++ b/packages/chat/telegram/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "references": [ + { "path": "../../effect" } + ] +} diff --git a/packages/chat/whatsapp/README.md b/packages/chat/whatsapp/README.md new file mode 100644 index 00000000000..2c079ab9f3c --- /dev/null +++ b/packages/chat/whatsapp/README.md @@ -0,0 +1,61 @@ +# @effect/chat-whatsapp + +A [WhatsApp Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api) adapter for `effect/chat`, for one business phone number. Includes the webhook, a pure normaliser for your own transport, a minimal Graph API client, delivery keys, template messages and read receipts. + +## Installation + +```sh +npm install effect @effect/chat-whatsapp +``` + +## Signature verification + +`WhatsAppChat.layerWebhook` requires the `Mac` service; the adapter does not +provide a default. Supply `NodeCrypto.layerMac` from +`@effect/platform-node/NodeCrypto`, `BunCrypto.layerMac`, +`DenoCrypto.layerMac`, or `BrowserCrypto.layerMac` from the corresponding +platform package. + +For other runtimes such as Workers, provide +`Mac.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto))`. + +## Known limits + +- Groups are not supported. Group webhooks are logged and dropped, and an op on a group address fails. +- WhatsApp has no edit or delete, so a streamed reply is posted part by part. +- Outside the 24 hour customer service window only templates can be sent, with `WhatsAppChat.sendTemplate`. +- A user can tap more than one reply button of a message, or pick one list row twice. Each tap is its own click. + +## Live checks + +Checked on Graph API `v23.0` from a Meta test number to one phone. The default `baseUrl` is `v24.0`, where these have not been repeated. + +- Meta counts text bodies, interactive bodies, reply button titles, list row titles and row ids in code points. The adapter counts UTF-16 units, which is never less. Captions, the list button and section titles were not measured. A reply button id is counted in UTF-8 bytes, up to 256, and the adapter checks it that way. +- The rendered formatting displays as intended, and literal `*`, `_` and escapes display as typed. +- A `.md` file sent as `text/plain` arrives with its name. +- A media header beside reply buttons or a link button renders. +- Meta gave up on a webhook response after about 22 seconds. The default `deadline` is 10 seconds. +- Six messages to one user in 4.4 seconds were accepted. That is one burst, not a sustained rate, so `minWriteInterval` stays at Meta's documented 6 seconds. + +Not yet checked: + +- Whether error 131047 comes back on the send or as a later status. +- Sending to a BSUID from a production number. A test number only sends to its allow-listed phone numbers. +- Sending from a production number whose business has Messaging accounts and a payment method, with and without `messagingAccountId`. +- Groups. + +## Links + +- [Website](https://effect.website): documentation, guides, and news. +- [Reference](https://effect.website/docs/v4/api/chat-whatsapp): API documentation for this package. +- [Discord](https://discord.gg/effect-ts): ask questions, share what you're building, and talk to the core team. +- [Community](https://effect.website/community-hub): meetups and events, or bring Effect to your own. +- [Issues](https://github.com/Effect-TS/effect/issues): bug reports and feature requests. + +## Let's talk + +Whether your team is considering Effect, rolling it out, or already running it in production, we'd love to hear from you: what you're building, what works, and what you need from Effect next. + +- **Talk to the maintainers.** Introduce your team on [Discord](https://discord.gg/effect-ts) or email [contact@effectful.co](mailto:contact@effectful.co). We're happy to connect privately on Slack or Discord for feedback and help with adoption. +- **Production support.** We're exploring how to better support teams running Effect in production. If your organization has specific support needs, let's discuss them. +- **Adoption help.** Our [adoption partners](https://effect.website/adoption-partners) offer implementation, consulting, team extension, training, and commercial support. diff --git a/packages/chat/whatsapp/package.json b/packages/chat/whatsapp/package.json new file mode 100644 index 00000000000..510868a6da5 --- /dev/null +++ b/packages/chat/whatsapp/package.json @@ -0,0 +1,69 @@ +{ + "name": "@effect/chat-whatsapp", + "version": "4.1.4", + "type": "module", + "license": "MIT", + "description": "The WhatsApp Cloud API adapter for effect/chat", + "homepage": "https://effect.website", + "repository": { + "type": "git", + "url": "https://github.com/Effect-TS/effect.git", + "directory": "packages/chat/whatsapp" + }, + "bugs": { + "url": "https://github.com/Effect-TS/effect/issues" + }, + "tags": [ + "typescript", + "chat", + "whatsapp" + ], + "keywords": [ + "typescript", + "chat", + "whatsapp" + ], + "sideEffects": [], + "exports": { + "./package.json": "./package.json", + ".": "./src/index.ts", + "./*": "./src/*.ts", + "./internal/*": null, + "./index": null, + "./*/index": null + }, + "files": [ + "src/**/*.ts", + "dist/**/*.js", + "dist/**/*.js.map", + "dist/**/*.d.ts", + "dist/**/*.d.ts.map", + "AGENTS.md", + "CLAUDE.md", + "ai-docs/**/*" + ], + "publishConfig": { + "access": "public", + "provenance": true, + "exports": { + "./package.json": "./package.json", + ".": "./dist/index.js", + "./*": "./dist/*.js", + "./internal/*": null, + "./index": null, + "./*/index": null + } + }, + "scripts": { + "codegen": "effect-utils codegen", + "build": "tsc -b tsconfig.json && pnpm babel", + "babel": "babel dist --plugins annotate-pure-calls --out-dir dist --source-maps", + "check": "tsc -b tsconfig.json" + }, + "devDependencies": { + "effect": "workspace:^" + }, + "peerDependencies": { + "effect": "workspace:^" + } +} diff --git a/packages/chat/whatsapp/src/WhatsAppChat.ts b/packages/chat/whatsapp/src/WhatsAppChat.ts new file mode 100644 index 00000000000..899cc3260ba --- /dev/null +++ b/packages/chat/whatsapp/src/WhatsAppChat.ts @@ -0,0 +1,486 @@ +/** + * The WhatsApp Cloud API adapter for `effect/chat`: the `Registry` layer + * for one business phone number, the webhook, the pure normaliser, delivery + * keys, templates, read receipts and the scoped post options. + * + * **Details** + * + * - Cloud API only. One adapter is one business phone number, and a + * `Registry` holds one adapter per platform. + * - An address is `{phoneNumberId}/u/{user}`, where `user` is a phone + * number in digits or a business-scoped user id (BSUID), such as + * `US.13491208655302741918`. `{phoneNumberId}/g/{groupId}` is + * reserved for groups, which are not supported yet: every op on it fails + * `InvalidRequest` with detail `group_unsupported`, and group webhooks + * are logged and dropped. + * - Inbound messages carry the BSUID form when the webhook has a BSUID, and + * the phone number only when it has none. The adapter never rewrites an + * address: a reply goes where the thread says. `WhatsAppUpdate.Alias` + * reports a phone number and a BSUID that name one user. + * - A business may only write freely within 24 hours of the user's last + * message. Outside it, a `post` fails `Forbidden` with detail + * `outsideWindow`, and `sendTemplate` is the way to write. + * - The access token needs `whatsapp_business_messaging`. Subscribe the app + * to the WhatsApp Business Account's `messages` field. + * + * @stability unstable + * @since 4.1.5 + */ +import { type Posted, Registry } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { ChatHandler, type NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import type { ParseMention } from "effect/chat/ChatTest" +import type { ChatThread } from "effect/chat/ChatThread" +import * as Context from "effect/Context" +import type * as Mac from "effect/crypto/Mac" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import { dual } from "effect/Function" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpRouter from "effect/http/HttpRouter" +import * as Layer from "effect/Layer" +import * as Option from "effect/Option" +import type * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Adapter from "./internal/adapter.ts" +import * as Address from "./internal/address.ts" +import * as Errors from "./internal/errors.ts" +import * as Normalize from "./internal/normalize.ts" +import { CurrentPostOptions } from "./internal/options.ts" +import * as Webhook from "./internal/webhook.ts" +import { WhatsAppGraphApi } from "./WhatsAppGraphApi.ts" +import * as GraphApi from "./WhatsAppGraphApi.ts" +import type { WhatsAppUpdate } from "./WhatsAppUpdate.ts" + +/** + * The detail of a `post` refused outside the 24 hour customer service + * window, Graph error 131047. The reason is `Forbidden`. Write with + * `sendTemplate` instead. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const outsideWindow: string = Errors.outsideWindow + +/** + * A template message: its approved name, its language code, such as + * `en_US`, and Meta's `components` for its parameters, sent as they are. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Template { + readonly name: string + readonly language: string + readonly components?: ReadonlyArray | undefined +} + +/** + * What one webhook delivery holds, in order: messages and reactions, clicks, + * and updates. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Inbound = + | ChatEvent.MessageCreated + | ChatEvent.ReactionAdded + | ChatInteraction.ButtonClicked + | ChatInteraction.SelectChanged + | WhatsAppUpdate + +/** + * The WhatsApp side of the adapter, for what the core has no op for. + * + * **Details** + * + * - `sendTemplate` sends an approved template, which is how v1 writes + * outside the 24 hour window. A template needs Meta's approval first; a + * Direct Send template, where Meta offers it, does not need a separate + * review. An authentication template with one-tap, zero-tap or copy-code + * buttons can't be sent to a BSUID: Meta refuses it with error 131062, + * which is `InvalidRequest`. Send those to a phone number address. + * - `markRead` marks a received message read, which shows blue ticks, and + * with `typing` shows the typing indicator until the next message or for + * up to 25 seconds. + * - `receive` runs one webhook delivery, already verified and parsed, as + * `layerWebhook` does, for a consumer's own transport. It needs a + * `ChatHandler`, and a `ChatInteraction.Handler` when there are + * clicks: without one a click is logged and dropped. `deadline` is a + * click handler's share, 3 seconds by default. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class WhatsAppChat extends Context.Service Effect.Effect + readonly markRead: ( + thread: ChatThread, + messageId: string, + options?: { readonly typing?: boolean | undefined } + ) => Effect.Effect + readonly receive: (envelope: unknown, options?: { + readonly onUpdate?: ((update: WhatsAppUpdate) => Effect.Effect) | undefined + readonly deadline?: Duration.Duration | undefined + }) => Effect.Effect +}>()("@effect/chat-whatsapp/WhatsAppChat") {} + +/** + * Registers the WhatsApp adapter in the `Registry`, for one business phone + * number. + * + * **Details** + * + * - Capabilities: `replyTo`, `files`, `download`, `getThread`, + * `opener`, `buttons` and `selects`. Every other op fails + * `Unsupported`. WhatsApp has no edit, so `ChatMarkdown` posts each + * part of a streamed reply once. + * - Every `post` is one send, and every check runs before any upload or + * send. `Posted` means Meta accepted the message, not that it was + * delivered: a later `failed` status goes to `onUpdate`. A send that + * failed after the request left is `uncertain`. + * - Markdown is rendered to WhatsApp's formatting: bold as `*b*`, italics + * as `_i_`, strikethrough as `~s~`, a link as `label (url)`, a heading + * as bold, a table without its delimiter row and a fence without its info + * string. Each replacement is used only when it is no longer than the + * Markdown it replaces, so the text never grows; anything else is sent as + * typed, including reference links and their definitions, and code fenced + * inside a quote or a list item. + * - `descriptor.measure` and the label limits count UTF-16 units. In live + * checks on Graph `v23.0`, Meta counted text bodies, interactive bodies, + * reply button titles, list row titles and row ids in code points, which a + * UTF-16 count never undercounts: it is stricter only for characters + * outside the Basic Multilingual Plane, such as emoji. Captions, the list + * button and section titles were not measured, and are counted the same + * way. + * - Link previews are off unless `withPostOptions({ linkPreview: true })`. + * - One file per post. It is uploaded, then sent as one media message with + * the text as its caption, or as the header of a message with reply + * buttons or a link button. Refused with `TooLarge`, subject `files` and + * detail `file_refused`, before anything is sent: more than one file, a + * MIME type outside Meta's table, `image/webp`, a file past its type's + * size limit, audio with text or actions, text over 1,024 beside a file, + * and a file beside a list. An upload Meta refuses as invalid or too large + * fails the same way, so `ChatMarkdown`'s `unattachedNote` applies. `text/markdown` is uploaded as + * `text/plain`, with its bytes and name unchanged. + * - Actions: one to three buttons with labels up to 20 go as reply buttons; + * a lone link button with a label up to 20 as a link button; otherwise + * buttons and select options become the rows of one list, at most ten, + * with labels up to 24. Link buttons beside anything else, or with a + * longer label, are appended to the text as `Label: url` lines. A body + * beside reply buttons or a link button is held to 1,024, which + * `descriptor.measure` counts. A post with actions and no text gets the + * labels, joined by ", ", as its text. + * - The list's button is the first select's placeholder, or + * `withPostOptions({ listButton })`, "Options" by default, up to 20. + * With buttons and a select, or two selects, each section has a title up + * to 24: the select's placeholder, or `buttonsSection`, "Actions" by + * default, for the buttons. + * - Refused before the call: more than ten rows (`TooLarge`, + * `list_rows`), a label past its limit (`TooLarge`, + * `label_too_long`), an id holding U+241F, two reply buttons with one + * label, and two elements with one id and value (`InvalidRequest`). A + * select's `selected` and a button's `style` are ignored. + * - A reply button's id on the wire is `b`, the id, U+241F and the value, + * and Meta counts it in UTF-8 bytes, up to 256. Over that, the post is + * refused before the call with `TooLarge`, `action_data_too_long`, + * even when the id and value are within `limits.actionDataLength`. + * - `getThread` is built from the address alone, as `direct`. `opener` + * is always `None`. + * - The pair rate limit is one message every 6 seconds to one user, which is + * `limits.minWriteInterval`. Meta accepts short bursts above that rate + * but refuses a sustained faster pace with error 131056, `RateLimited`. + * - `messagingAccountId` is sent as `messaging_account_id` on every + * message. Include it to say which Messaging account sends when the + * access token can send from more than one on the phone number. Meta + * documents it as optional for now, required for such a token on the + * newest Graph version from H1 2027, and on every version from H1 2028. + * Without it, Meta picks the account from the token. Not yet checked + * against a production number. + * - Built against Graph API `v24.0`, the default `baseUrl`. The + * adapter's live checks ran on `v23.0`. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly accessToken: Redacted.Redacted + readonly phoneNumberId: string + readonly messagingAccountId?: string | undefined + readonly baseUrl?: string | undefined +}): Layer.Layer => + Layer.effect( + WhatsAppChat, + Effect.gen(function*() { + const graph = yield* WhatsAppGraphApi + const http = yield* HttpClient.HttpClient + const registry = yield* Registry + const { adapter, markRead, sendTemplate } = Adapter.make({ + graph, + http, + accessToken: options.accessToken, + phoneNumberId: options.phoneNumberId, + messagingAccountId: options.messagingAccountId + }) + yield* registry.register(adapter) + const context = { phoneNumberId: options.phoneNumberId } + return WhatsAppChat.of({ + phoneNumberId: options.phoneNumberId, + sendTemplate, + markRead, + receive: (envelope, receiveOptions) => + Effect.flatMap(ChatHandler, (handler) => + Webhook.receive(Normalize.normalize(envelope, context), { + handle: handler.handle, + onUpdate: receiveOptions?.onUpdate, + clickDeadline: Effect.succeed(receiveOptions?.deadline ?? Duration.seconds(3)) + })) + }) + }) + ).pipe(Layer.provideMerge(GraphApi.layer({ accessToken: options.accessToken, baseUrl: options.baseUrl }))) + +/** + * The address of a WhatsApp user: `{phoneNumberId}/u/{user}`, where + * `user` is a phone number in digits or a BSUID. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const thread = (parts: { readonly phoneNumberId: string; readonly user: string }): ChatThread => + Address.thread({ phoneNumberId: parts.phoneNumberId, kind: "user", id: parts.user }) + +/** + * The parts of a WhatsApp address, or `None` for another platform's or a + * malformed one. A group address, which is reserved, has `kind` `group`. + * + * @stability unstable + * @category destructors + * @since 4.1.5 + */ +export const parts: (thread: ChatThread) => Option.Option<{ + readonly phoneNumberId: string + readonly kind: "user" | "group" + readonly id: string +}> = Address.parts + +/** + * `GET {path}` and `POST {path}`: the webhook's subscription check and its + * deliveries. + * + * **Details** + * + * - `GET` answers Meta's check with `hub.challenge` when `hub.verify_token` + * is `verifyToken`, and 403 otherwise. + * - `POST` checks `X-Hub-Signature-256` with the app secret (401), reads + * at most `maxBodyBytes` (3 MiB by default, 413), and drops a delivery + * for another object or another phone number with a 200. + * - The updates run in order. Messages and reactions go to the + * `ChatHandler`, clicks to `ChatInteraction.handle`, and statuses and + * changed ids to `onUpdate`. A removed reaction is dropped. + * - A `NotAccepted` stops the delivery and answers 503, as does a request + * that takes longer than `deadline`, 10 seconds by default. Meta then + * delivers the whole batch again, for up to 7 days, so a batch that + * always runs past the deadline repeats until then. At the deadline the + * work is interrupted without waiting for its finalizers. In a live check + * Meta gave up on a response after about 22 seconds and redelivered. + * - A click never causes a 503. It gets what is left of the deadline, less + * a margin. `Ack` and `Late` count as handled, and a late handler keeps + * running. `OpenModal` goes to `Handler.refused` as + * `Unsupported("modals")`, which is awaited only within the click's share. Without a `ChatInteraction.Handler` in the + * layer's context, a click is logged and dropped. + * - Delivery is at least once. A message, a reaction or a click can arrive + * twice, a click even at the same time as its first delivery. Drop + * repeated events with `ChatEvent.dedupeKey`; a click handler for which + * a second run is unsafe must claim `deliveryKey` atomically, before any + * side effect. The adapter keeps no seen-store. Deliveries are not in + * order either: order by the updates' timestamps. + * - A user can tap more than one reply button of a message, or pick one + * list row twice. Each tap is a click of its own, with its own + * `deliveryKey`, so a choice that must be made once needs its own + * claim. + * + * Signature checks use the required `Mac` service. Provide + * `NodeCrypto.layerMac`, `BunCrypto.layerMac`, `DenoCrypto.layerMac`, + * `BrowserCrypto.layerMac`, or `Mac.layerWebCrypto` with + * `SecureRandom.layerWebCrypto` for other runtimes such as Workers. + * Key construction failures are configuration defects; verification errors + * fail the signature check (401). + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layerWebhook = (options: { + readonly path: `/${string}` + readonly appSecret: Redacted.Redacted + readonly verifyToken: Redacted.Redacted + readonly onUpdate?: ((update: WhatsAppUpdate) => Effect.Effect) | undefined + readonly deadline?: Duration.Duration | undefined + readonly maxBodyBytes?: number | undefined +}): Layer.Layer => + HttpRouter.use(Effect.fnUntraced(function*(router) { + const whatsapp = yield* WhatsAppChat + const handler = yield* ChatHandler + const interactions = yield* Effect.serviceOption(ChatInteraction.Handler) + const verify = yield* Webhook.makeVerifier(options.appSecret) + const context = { phoneNumberId: whatsapp.phoneNumberId } + const webhookOptions: Webhook.Options = { + verify, + normalize: (envelope) => Normalize.normalize(envelope, context), + handle: handler.handle, + onUpdate: options.onUpdate, + deadline: options.deadline ?? Duration.seconds(10), + maxBodyBytes: options.maxBodyBytes ?? 3 * 1024 * 1024 + } + yield* router.add( + "GET", + options.path, + (request) => Effect.succeed(Webhook.handleVerification(request, options.verifyToken)) + ) + yield* router.add("POST", options.path, (request) => { + const response = Webhook.handleRequest(request, webhookOptions) + return Option.isSome(interactions) + ? Effect.provideService(response, ChatInteraction.Handler, interactions.value) + : response + }) + })) + +/** + * The normaliser, for a consumer's own transport. + * + * **Details** + * + * - Pure: takes a parsed webhook body, gives what it holds, in order. A + * change for another phone number, and a group message, give nothing. + * - A message's `thread` is the sender's BSUID address when the webhook + * has one, and its phone number address otherwise. `kind` is + * `direct`. Text is as the user typed it, a caption for media, and empty + * for a message with neither, such as a location. Media are attachments, + * with the media id as their `id`. + * - `raw` of an event or a click is the change's `metadata`, the + * sender's entry of `contacts`, with `wa_id` when Meta shares the + * number, and the one message. `message.raw` is the message. + * - A reaction with an emoji is `ReactionAdded`; a removed reaction gives + * nothing. + * - A reply button or a list row the adapter built is `ButtonClicked` or + * `SelectChanged`, whose `messageId` is the message that carried it. + * Any other interactive reply gives nothing. + * - Statuses give `Status`, and `Alias` too when they carry both ids. A + * `system` message about a changed number or id gives `UserIdChanged`. + * + * @stability unstable + * @category normalization + * @since 4.1.5 + */ +export const normalize: ( + envelope: unknown, + context: { readonly phoneNumberId: string } +) => ReadonlyArray = Normalize.normalize + +const encodeKey = (parts: ReadonlyArray) => parts.map(encodeURIComponent).join("/") + +const phoneNumberOf = (thread: ChatThread) => thread.id.split("/")[0] + +/** + * The key of one delivery of a click or a reaction: + * `{phoneNumberId}/{messageId}`, the id of the webhook message that carried + * it, each part percent-encoded. + * + * **When to use** + * + * Use to make a click handler safe to run twice. Meta redelivers a batch + * that was not answered in time, so a click can run again, even at the same + * time as the first run. Claim the key atomically, such as with an insert + * that fails on a duplicate, before any side effect, and skip the click when + * the claim fails. + * + * @stability unstable + * @category getters + * @since 4.1.5 + */ +export const deliveryKey = ( + event: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatEvent.ReactionAdded +): string => { + const raw = event.raw as { readonly messages?: ReadonlyArray<{ readonly id?: unknown }> } + const id = Array.isArray(raw?.messages) && typeof raw.messages[0]?.id === "string" ? raw.messages[0].id : "" + return encodeKey([phoneNumberOf(event.thread), id]) +} + +/** + * The key of an update, for dropping a redelivered one, its parts + * percent-encoded and joined by `/`. + * + * **Details** + * + * - `Status`: `{phoneNumberId}/status/{messageId}/{status}`. + * - `UserIdChanged`: `{phoneNumberId}/user_changed/{id}`. + * - `Alias`: `{phoneNumberId}/alias/{messageId}/{status}/{phone}/{userId}`. + * + * @stability unstable + * @category getters + * @since 4.1.5 + */ +export const updateKey = (update: WhatsAppUpdate): string => { + const phoneNumberId = phoneNumberOf(update.thread) + switch (update._tag) { + case "Status": + return encodeKey([phoneNumberId, "status", update.messageId, update.status]) + case "UserIdChanged": + return encodeKey([phoneNumberId, "user_changed", update.id]) + case "Alias": + return encodeKey([phoneNumberId, "alias", update.messageId, update.status, update.phone, update.userId]) + } +} + +/** + * WhatsApp options for every write inside an effect. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface WhatsAppPostOptions { + /** A preview of the first link in a text message. Default false. */ + readonly linkPreview?: boolean | undefined + /** The button that opens a list, when no select has a placeholder. Default "Options", up to 20. */ + readonly listButton?: string | undefined + /** The title of a list's buttons section beside a select. Default "Actions", up to 24. */ + readonly buttonsSection?: string | undefined +} + +/** + * Scoped WhatsApp options for every write inside `effect`. Nested calls + * merge, the inner one winning field by field. + * + * @stability unstable + * @category combinators + * @since 4.1.5 + */ +export const withPostOptions: { + (options: WhatsAppPostOptions): (effect: Effect.Effect) => Effect.Effect + (effect: Effect.Effect, options: WhatsAppPostOptions): Effect.Effect +} = dual( + 2, + (effect: Effect.Effect, options: WhatsAppPostOptions): Effect.Effect => + Effect.updateService(effect, CurrentPostOptions, (current) => ({ ...current, ...options })) +) + +/** + * WhatsApp has no mention tokens outside groups, so nothing is a mention: + * for `ChatTest.checkMessage`, `ChatTest.checkEvent` and + * `ChatTest.conformance`. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const parseMention: ParseMention = () => Option.none() diff --git a/packages/chat/whatsapp/src/WhatsAppGraphApi.ts b/packages/chat/whatsapp/src/WhatsAppGraphApi.ts new file mode 100644 index 00000000000..12e0a7164f2 --- /dev/null +++ b/packages/chat/whatsapp/src/WhatsAppGraphApi.ts @@ -0,0 +1,146 @@ +/** + * The minimal Graph API client the WhatsApp adapter is built on. It is + * public, so a consumer can call an endpoint the adapter has no op for, with + * the same error mapping and the same rule for writes. + * + * @stability unstable + * @since 4.1.5 + */ +import { retrySafe } from "effect/chat/ChatAdapter" +import type { ChatError, ChatErrorReason } from "effect/chat/ChatError" +import * as Context from "effect/Context" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Errors from "./internal/errors.ts" + +/** + * Options for one Graph call. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface CallOptions { + /** Default `GET`, or `POST` when there is a `body` or a `form`. */ + readonly method?: "GET" | "POST" | "DELETE" | undefined + /** Sent as JSON. */ + readonly body?: Readonly> | undefined + /** Sent as `multipart/form-data`, as a media upload is. */ + readonly form?: FormData | undefined + /** Query parameters. */ + readonly params?: Readonly> | undefined + /** + * The call changes something. It is never repeated once the request left, + * and a failure that may have landed is `uncertain`. + */ + readonly write?: boolean | undefined + /** Retry `retry` failures, as `ChatAdapter.retrySafe` does. Default true. */ + readonly retry?: boolean | undefined +} + +/** + * The minimal Graph API client, with the access token. + * + * **Details** + * + * - `call` requests `{baseUrl}/{path}` with the token as a bearer token, + * and succeeds with the JSON of a 2xx answer. + * - Another answer fails with the reason for its Graph error code, whose + * `detail` is `graph_{code}` and whose `cause` is the Graph `error` + * object, or for its HTTP status when it has no Graph error. + * - A write never repeats once the request left: a transport failure, a + * server error or an unreadable answer is `uncertain`. A refusal that + * Meta answers before sending, such as a rate limit, is retried unless + * `retry: false`, as a read's `retry` failures are. + * - The error's `op` is the path. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class WhatsAppGraphApi extends Context.Service Effect.Effect +}>()("@effect/chat-whatsapp/WhatsAppGraphApi") {} + +/** + * The Graph API version the adapter was written against. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const defaultBaseUrl = "https://graph.facebook.com/v24.0" + +/** + * Builds the client from an `HttpClient` and a system user access token. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const make = Effect.fnUntraced(function*(options: { + readonly accessToken: Redacted.Redacted + readonly baseUrl?: string | undefined +}) { + const http = yield* HttpClient.HttpClient + const baseUrl = options.baseUrl ?? defaultBaseUrl + + const once = (path: string, callOptions: CallOptions) => { + const write = callOptions.write === true + const fail = (reason: ChatErrorReason) => Effect.fail(Errors.chatError(path, reason)) + const method = callOptions.method ?? + (callOptions.body !== undefined || callOptions.form !== undefined ? "POST" : "GET") + let request = HttpClientRequest.make(method)(`${baseUrl}/${path}`).pipe( + HttpClientRequest.bearerToken(Redacted.value(options.accessToken)) + ) + if (callOptions.params !== undefined) request = HttpClientRequest.setUrlParams(request, callOptions.params) + if (callOptions.form !== undefined) request = HttpClientRequest.bodyFormData(request, callOptions.form) + else if (callOptions.body !== undefined) request = HttpClientRequest.bodyJsonUnsafe(request, callOptions.body) + return http.execute(request).pipe( + Effect.catch((error) => fail(Errors.transport(write, error.reason._tag, error))), + Effect.flatMap((response) => + response.json.pipe( + Effect.matchEffect({ + onFailure: (error) => + response.status >= 200 && response.status < 300 + ? fail(Errors.invalidResponse(write, "invalid_json", error)) + : fail(Errors.reasonForStatus(response.status, { write })), + onSuccess: (json): Effect.Effect => { + if (response.status >= 200 && response.status < 300) return Effect.succeed(json as Schema.Json) + const graph = Errors.graphError(json) + const cause = typeof json === "object" && json !== null && "error" in json ? json.error : json + return fail( + graph === undefined + ? Errors.reasonForStatus(response.status, { write, cause }) + : Errors.reasonForGraphError(graph, { write, cause }) + ) + } + }) + ) + ) + ) + } + + return WhatsAppGraphApi.of({ + call: (path, callOptions = {}) => { + const call = once(path, callOptions) + return callOptions.retry === false ? call : retrySafe(call) + } + }) +}) + +/** + * The client as a layer. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options: { + readonly accessToken: Redacted.Redacted + readonly baseUrl?: string | undefined +}): Layer.Layer => Layer.effect(WhatsAppGraphApi, make(options)) diff --git a/packages/chat/whatsapp/src/WhatsAppUpdate.ts b/packages/chat/whatsapp/src/WhatsAppUpdate.ts new file mode 100644 index 00000000000..60c942a1843 --- /dev/null +++ b/packages/chat/whatsapp/src/WhatsAppUpdate.ts @@ -0,0 +1,142 @@ +/** + * What WhatsApp reports beside messages: delivery statuses, a user's changed + * id, and the link between a phone number and a business-scoped user id. + * They go to the `onUpdate` of `WhatsAppChat.layerWebhook` and + * `WhatsAppChat.receive`, not to the `ChatHandler`. + * + * @stability unstable + * @since 4.1.5 + */ +import { ChatErrorReason } from "effect/chat/ChatError" +import { ChatThread } from "effect/chat/ChatThread" +import * as Schema from "effect/Schema" + +/** + * Schema for the status of a message the business sent. + * + * **Details** + * + * - `thread` is the recipient's address, with the business-scoped user id + * when the status carries one and the phone number when it does not. + * - `messageId` is the id `post` gave. A `failed` status has the + * `reason` for Meta's error, mapped as a refused call is. + * - `phone` and `userId` are the recipient's phone number and + * business-scoped user id, each when the status carries it. + * - `raw` is the change's `metadata` and the one status. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Status = Schema.TaggedStruct("Status", { + thread: ChatThread, + messageId: Schema.String, + status: Schema.Literals(["sent", "delivered", "read", "failed"]), + timestamp: Schema.optionalKey(Schema.DateTimeUtc), + reason: Schema.optionalKey(ChatErrorReason), + phone: Schema.optionalKey(Schema.String), + userId: Schema.optionalKey(Schema.String), + raw: Schema.Json +}) + +/** + * The status of a message the business sent. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Status = typeof Status.Type + +/** + * Schema for a user's changed phone number or business-scoped user id, from + * a `system` message. + * + * **Details** + * + * - `thread` is the user's new address. A thread kept under the old one no + * longer receives messages from the user. + * - `id` is the system message's id. `change` is Meta's `type`: + * `user_changed_number` or `user_changed_user_id`. + * - `userId`, `previousUserId` and `phone` are there when Meta sends + * them. `previousPhone` is the system message's `from`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const UserIdChanged = Schema.TaggedStruct("UserIdChanged", { + thread: ChatThread, + id: Schema.String, + change: Schema.Literals(["user_changed_number", "user_changed_user_id"]), + userId: Schema.optionalKey(Schema.String), + previousUserId: Schema.optionalKey(Schema.String), + phone: Schema.optionalKey(Schema.String), + previousPhone: Schema.optionalKey(Schema.String), + timestamp: Schema.optionalKey(Schema.DateTimeUtc), + raw: Schema.Json +}) + +/** + * A user's changed phone number or business-scoped user id. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type UserIdChanged = typeof UserIdChanged.Type + +/** + * Schema for a phone number and a business-scoped user id that name one + * user. + * + * **Details** + * + * - Given for every `sent`, `delivered` or `read` status that carries + * both, beside the `Status`. Nothing is remembered between batches, so the + * same pair comes again and again. + * - A message posted to a phone number address reports back with the + * user's business-scoped id, which is how a consumer learns that the + * inbound address (`thread`, always the business-scoped form) and the + * phone number address are one user. The adapter never rewrites an + * address. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Alias = Schema.TaggedStruct("Alias", { + thread: ChatThread, + phone: Schema.String, + userId: Schema.String, + messageId: Schema.String, + status: Schema.Literals(["sent", "delivered", "read"]), + raw: Schema.Json +}) + +/** + * A phone number and a business-scoped user id that name one user. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Alias = typeof Alias.Type + +/** + * Schema for any update. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const WhatsAppUpdate = Schema.Union([Status, UserIdChanged, Alias]) + +/** + * Any update. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type WhatsAppUpdate = typeof WhatsAppUpdate.Type diff --git a/packages/chat/whatsapp/src/index.ts b/packages/chat/whatsapp/src/index.ts new file mode 100644 index 00000000000..84c2ba8db9e --- /dev/null +++ b/packages/chat/whatsapp/src/index.ts @@ -0,0 +1,24 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as WhatsAppChat from "./WhatsAppChat.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as WhatsAppGraphApi from "./WhatsAppGraphApi.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as WhatsAppUpdate from "./WhatsAppUpdate.ts" diff --git a/packages/chat/whatsapp/src/internal/actions.ts b/packages/chat/whatsapp/src/internal/actions.ts new file mode 100644 index 00000000000..e21c36a161d --- /dev/null +++ b/packages/chat/whatsapp/src/internal/actions.ts @@ -0,0 +1,219 @@ +import { type ChatErrorReason, InvalidRequest, TooLarge } from "effect/chat/ChatError" +import type { Action, Button, LinkButton, Select } from "effect/chat/ChatUi" +import { defaultButtonsSection, defaultListButton, type PostOptions } from "./options.ts" +import { shortBodyLength, utf8Bytes } from "./text.ts" + +/** Between an element's id and its value in a wire id. @internal */ +export const join = "\u241F" + +/** @internal */ +export const limits = { actionsPerMessage: 10, actionDataLength: 198 } as const + +/** Meta counts a reply button's id in UTF-8 bytes, unlike every other limit. */ +const replyIdBytes = 256 +const replyLabelLength = 20 +const rowTitleLength = 24 +const listButtonLength = 20 +const sectionTitleLength = 24 +const maxRows = 10 + +/** What the measure adds for a layout whose body is held to 1,024 rather than 4,096. @internal */ +export const shortBodyOffset = 4096 - shortBodyLength + +/** @internal */ +export type Kind = "none" | "reply" | "cta" | "list" + +/** @internal */ +export interface Shape { + readonly kind: Kind + /** The link buttons that go as `Label: url` lines after the body. */ + readonly appended: ReadonlyArray +} + +/** The layout of a message's actions, which depends on the actions alone. @internal */ +export const shape = (actions: ReadonlyArray): Shape => { + const buttons = actions.filter((action): action is Button => action._tag === "Button") + const links = actions.filter((action): action is LinkButton => action._tag === "LinkButton") + const selects = actions.filter((action) => action._tag === "Select") + if (buttons.length === 0 && selects.length === 0) { + return links.length === 1 && links[0].label.length <= replyLabelLength + ? { kind: "cta", appended: [] } + : { kind: "none", appended: links } + } + const reply = selects.length === 0 && buttons.length <= 3 && + buttons.every((button) => button.label.length <= replyLabelLength) + return { kind: reply ? "reply" : "list", appended: links } +} + +/** The body of a post with actions and no Markdown: the labels joined by ", ". @internal */ +export const generatedBody = (actions: ReadonlyArray): string => + actions.map((action) => + action._tag === "Select" + ? action.placeholder ?? action.options.map((option) => option.label).join(", ") + : action.label + ).join(", ") + +/** @internal */ +export const linkLines = (links: ReadonlyArray): string => + links.map((link) => link.label + ": " + link.url).join("\n") + +/** + * What a message's actions add to the measure of its Markdown: the offset of + * a 1,024 body, the appended link lines with their separator, and the + * generated body, which is always counted. + * + * @internal + */ +export const measure = (actions: ReadonlyArray): number => { + if (actions.length === 0) return 0 + const { appended, kind } = shape(actions) + return (kind === "reply" || kind === "cta" ? shortBodyOffset : 0) + generatedBody(actions).length + + (appended.length === 0 ? 0 : 2 + linkLines(appended).length) +} + +/** The body as sent: the rendered Markdown, or the generated body, then the link lines. @internal */ +export const body = (rendered: string, actions: ReadonlyArray): string => { + const { appended, kind } = shape(actions) + const base = rendered.trim() !== "" ? rendered : kind === "none" ? "" : generatedBody(actions) + const lines = linkLines(appended) + return base === "" ? lines : lines === "" ? base : base + "\n\n" + lines +} + +/** @internal */ +export const buttonWireId = (button: Button): string => + "b" + button.id + (button.value === undefined ? "" : join + button.value) + +/** @internal */ +export const optionWireId = (select: Select, value: string): string => "s" + select.id + join + value + +/** A wire id read back: the tag, then the id and value split at the first U+241F. @internal */ +export const parseWireId = ( + wire: string +): { readonly kind: "button"; readonly id: string; readonly value?: string } | { + readonly kind: "select" + readonly id: string + readonly value: string +} | undefined => { + const rest = wire.slice(1) + const at = rest.indexOf(join) + if (wire[0] === "b") { + return at < 0 + ? { kind: "button", id: rest } + : { kind: "button", id: rest.slice(0, at), value: rest.slice(at + 1) } + } + if (wire[0] === "s" && at >= 0) return { kind: "select", id: rest.slice(0, at), value: rest.slice(at + 1) } + return undefined +} + +/** The `interactive` object of a message, less its body and header. @internal */ +export type Interactive = + | { readonly type: "button"; readonly action: unknown } + | { readonly type: "cta_url"; readonly action: unknown } + | { readonly type: "list"; readonly action: unknown } + +/** @internal */ +export interface Plan { + readonly kind: Kind + readonly interactive?: Interactive | undefined +} + +const tooLarge = (detail: string) => new TooLarge({ detail }) +const invalid = (detail: string) => new InvalidRequest({ detail }) + +/** + * Checks a message's actions and builds their `interactive` object. Every + * refusal is found here, before any call. + * + * @internal + */ +export const plan = (actions: ReadonlyArray, options: PostOptions): Plan | ChatErrorReason => { + if (actions.length === 0) return { kind: "none" } + if (actions.length > limits.actionsPerMessage) return tooLarge("too_many_actions") + for (const action of actions) { + if (action._tag === "LinkButton") { + if (action.label.length > rowTitleLength) return tooLarge("label_too_long") + continue + } + if (action.id.includes(join)) return invalid("invalid_action_id") + if (action._tag === "Button") { + if (action.id.length + (action.value?.length ?? 0) > limits.actionDataLength) { + return tooLarge("action_data_too_long") + } + } else { + if (action.options.length === 0) return invalid("empty_select") + for (const option of action.options) { + if (action.id.length + option.value.length > limits.actionDataLength) return tooLarge("action_data_too_long") + } + } + } + const { kind } = shape(actions) + const buttons = actions.filter((action): action is Button => action._tag === "Button") + const selects = actions.filter((action): action is Select => action._tag === "Select") + const unique = (ids: ReadonlyArray) => new Set(ids).size === ids.length + switch (kind) { + case "none": + return { kind } + case "cta": { + const link = actions.find((action): action is LinkButton => action._tag === "LinkButton")! + return { + kind, + interactive: { + type: "cta_url", + action: { name: "cta_url", parameters: { display_text: link.label, url: link.url } } + } + } + } + case "reply": { + const ids = buttons.map(buttonWireId) + if (!unique(buttons.map((button) => button.label))) return invalid("duplicate_label") + if (!unique(ids)) return invalid("duplicate_id") + if (ids.some((id) => utf8Bytes(id) > replyIdBytes)) return tooLarge("action_data_too_long") + return { + kind, + interactive: { + type: "button", + action: { + buttons: buttons.map((button, i) => ({ type: "reply", reply: { id: ids[i], title: button.label } })) + } + } + } + } + case "list": { + const sections: Array< + { readonly title: string; readonly rows: Array<{ readonly id: string; readonly title: string }> } + > = [] + const listButton = options.listButton ?? defaultListButton + if (buttons.length > 0) { + sections.push({ + title: options.buttonsSection ?? defaultButtonsSection, + rows: buttons.map((button) => ({ id: buttonWireId(button), title: button.label })) + }) + } + for (const select of selects) { + sections.push({ + title: select.placeholder ?? listButton, + rows: select.options.map((option) => ({ id: optionWireId(select, option.value), title: option.label })) + }) + } + const rows = sections.flatMap((section) => section.rows) + if (rows.length > maxRows) return tooLarge("list_rows") + if (rows.some((row) => row.title.length > rowTitleLength)) return tooLarge("label_too_long") + if (!unique(rows.map((row) => row.id))) return invalid("duplicate_id") + const opener = selects.find((select) => select.placeholder !== undefined)?.placeholder ?? listButton + if (opener.length > listButtonLength) return tooLarge("label_too_long") + if (sections.length > 1 && sections.some((section) => section.title.length > sectionTitleLength)) { + return tooLarge("label_too_long") + } + return { + kind, + interactive: { + type: "list", + action: { + button: opener, + sections: sections.map((section) => sections.length > 1 ? section : { rows: section.rows }) + } + } + } + } + } +} diff --git a/packages/chat/whatsapp/src/internal/adapter.ts b/packages/chat/whatsapp/src/internal/adapter.ts new file mode 100644 index 00000000000..1841a532aa7 --- /dev/null +++ b/packages/chat/whatsapp/src/internal/adapter.ts @@ -0,0 +1,300 @@ +import { type ChatAdapter, checkMessageLength, type Descriptor, type File, type Posted } from "effect/chat/ChatAdapter" +import { + type ChatError, + type ChatErrorReason, + fileRefused, + InvalidRequest, + TooLarge, + Unsupported +} from "effect/chat/ChatError" +import type { ChatThread } from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import type * as HttpClient from "effect/http/HttpClient" +import * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import type * as Schema from "effect/Schema" +import * as Stream from "effect/Stream" +import type { WhatsAppGraphApi } from "../WhatsAppGraphApi.ts" +import * as Actions from "./actions.ts" +import * as Address from "./address.ts" +import * as Errors from "./errors.ts" +import { CurrentPostOptions } from "./options.ts" +import * as Text from "./text.ts" + +type Json = Schema.Json +type JsonObject = { readonly [key: string]: Json } + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const string = (object: JsonObject | undefined, key: string): string | undefined => { + const value = object?.[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +/** @internal */ +export const descriptor: Descriptor = { + platform: Address.platform, + capabilities: new Set(["replyTo", "files", "download", "getThread", "opener", "buttons", "selects"]), + measure: (markdown, actions) => Text.measure(markdown) + Actions.measure(actions ?? []), + limits: { + messageLength: Text.messageLength, + filesPerMessage: 1, + uploadBytes: 100 * 1024 * 1024, + minWriteInterval: Duration.seconds(6), + ...Actions.limits + } +} + +type MediaType = "image" | "video" | "audio" | "document" + +const megabytes = (n: number) => n * 1024 * 1024 + +/** Meta's supported media, by MIME type, with the largest upload of each. */ +const mediaTypes: Record = { + "image/jpeg": { type: "image", maxBytes: megabytes(5) }, + "image/png": { type: "image", maxBytes: megabytes(5) }, + "video/mp4": { type: "video", maxBytes: megabytes(16) }, + "video/3gpp": { type: "video", maxBytes: megabytes(16) }, + "audio/aac": { type: "audio", maxBytes: megabytes(16) }, + "audio/amr": { type: "audio", maxBytes: megabytes(16) }, + "audio/mpeg": { type: "audio", maxBytes: megabytes(16) }, + "audio/mp4": { type: "audio", maxBytes: megabytes(16) }, + "audio/ogg": { type: "audio", maxBytes: megabytes(16) }, + "text/plain": { type: "document", maxBytes: megabytes(100) }, + "application/pdf": { type: "document", maxBytes: megabytes(100) }, + "application/msword": { type: "document", maxBytes: megabytes(100) }, + "application/vnd.ms-excel": { type: "document", maxBytes: megabytes(100) }, + "application/vnd.ms-powerpoint": { type: "document", maxBytes: megabytes(100) }, + "application/vnd.openxmlformats-officedocument.wordprocessingml.document": { + type: "document", + maxBytes: megabytes(100) + }, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": { type: "document", maxBytes: megabytes(100) }, + "application/vnd.openxmlformats-officedocument.presentationml.presentation": { + type: "document", + maxBytes: megabytes(100) + } +} + +/** The MIME type a file is uploaded as: without parameters, and `text/markdown` as `text/plain`. @internal */ +export const uploadType = (contentType: string): string => { + const base = contentType.split(";")[0].trim().toLowerCase() + return base === "text/markdown" ? "text/plain" : base +} + +const refused = new TooLarge({ subject: "files", detail: fileRefused }) + +/** A template message: its name, its language and Meta's `components`. @internal */ +export interface Template { + readonly name: string + readonly language: string + readonly components?: ReadonlyArray | undefined +} + +/** @internal */ +export const make = (options: { + readonly graph: WhatsAppGraphApi["Service"] + readonly http: HttpClient.HttpClient + readonly accessToken: Redacted.Redacted + readonly phoneNumberId: string + readonly messagingAccountId?: string | undefined +}) => { + const { graph, phoneNumberId } = options + const account = options.messagingAccountId === undefined ? {} : { messaging_account_id: options.messagingAccountId } + + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(Errors.chatError(op, reason)) + + const withOp = (op: string) => (error: ChatError) => Errors.chatError(op, error.reason) + + const unsupported = (op: string, capability: Unsupported["capability"]) => fail(op, new Unsupported({ capability })) + + /** The user of a thread this adapter can write to. */ + const resolve = (op: string, thread: ChatThread): Effect.Effect => { + const parts = Address.parts(thread) + if (Option.isNone(parts)) return fail(op, new InvalidRequest({ detail: "invalid_thread" })) + if (parts.value.kind === "group") return fail(op, new InvalidRequest({ detail: "group_unsupported" })) + if (parts.value.phoneNumberId !== phoneNumberId) { + return fail(op, new InvalidRequest({ detail: "other_phone_number" })) + } + return Effect.succeed(parts.value.id) + } + + const recipient = (user: string) => Address.isBsuid(user) ? { recipient: user } : { to: user } + + const send = Effect.fnUntraced(function*(op: string, thread: ChatThread, user: string, message: JsonObject) { + const answer = yield* graph.call(`${phoneNumberId}/messages`, { + body: { messaging_product: "whatsapp", recipient_type: "individual", ...account, ...recipient(user), ...message }, + write: true + }).pipe(Effect.mapError(withOp(op))) + const messages = isObject(answer) && Array.isArray(answer.messages) ? answer.messages : [] + const id = isObject(messages[0]) ? string(messages[0], "id") : undefined + if (id === undefined) return yield* fail(op, Errors.invalidResponse(true, "missing_message_id")) + const timestamp = yield* DateTime.now + return { id, thread, timestamp } satisfies Posted + }) + + /** The checks on a file, before anything is sent: its media type, or the refusal. */ + const checkFile = ( + files: ReadonlyArray, + caption: string, + plan: Actions.Plan + ): ChatErrorReason | { readonly file: File; readonly mime: string; readonly type: MediaType } => { + if (files.length > 1) return refused + const file = files[0] + const mime = uploadType(file.contentType) + const media = Object.hasOwn(mediaTypes, mime) ? mediaTypes[mime] : undefined + if (media === undefined || file.content.length > media.maxBytes) return refused + if (plan.kind === "list") return refused + if (media.type === "audio" && (caption.trim() !== "" || plan.kind !== "none")) return refused + if (caption.length > Text.shortBodyLength) return refused + return { file, mime, type: media.type } + } + + const upload = (op: string, file: File, mime: string): Effect.Effect => { + const form = new FormData() + form.append("messaging_product", "whatsapp") + form.append("type", mime) + form.append("file", new Blob([file.content as Uint8Array], { type: mime }), file.name) + return graph.call(`${phoneNumberId}/media`, { form }).pipe( + // Meta refusing the file itself is a file refusal: nothing was sent, so the text can go without it. + Effect.mapError((error) => + Errors.chatError( + op, + error.reason._tag === "TooLarge" || error.reason._tag === "InvalidRequest" + ? new TooLarge({ subject: "files", detail: fileRefused, cause: error.reason }) + : error.reason + ) + ), + Effect.flatMap((answer) => { + const id = isObject(answer) ? string(answer, "id") : undefined + return id === undefined ? fail(op, Errors.invalidResponse(false, "missing_media_id")) : Effect.succeed(id) + }) + ) + } + + const post: ChatAdapter["post"] = Effect.fnUntraced(function*(thread, content) { + const op = "post" + const user = yield* resolve(op, thread) + const actions = content.actions ?? [] + const postOptions = yield* CurrentPostOptions + const plan = Actions.plan(actions, postOptions) + if (!("kind" in plan)) return yield* fail(op, plan) + yield* checkMessageLength(descriptor, op, content.markdown, actions) + const text = Actions.body(Text.render(content.markdown), actions) + const files = content.files ?? [] + const context = content.replyTo === undefined ? {} : { context: { message_id: content.replyTo.messageId } } + let header: JsonObject | undefined + if (files.length > 0) { + const checked = checkFile(files, text, plan) + if (!("file" in checked)) return yield* fail(op, checked) + const { file, mime, type } = checked + const id = yield* upload(op, file, mime) + const media: JsonObject = type === "document" ? { id, filename: file.name } : { id } + if (plan.interactive === undefined) { + const caption = type === "audio" || text.trim() === "" ? {} : { caption: text } + return yield* send(op, thread, user, { ...context, type, [type]: { ...media, ...caption } }) + } + header = { type, [type]: media } + } + if (plan.interactive !== undefined) { + return yield* send(op, thread, user, { + ...context, + type: "interactive", + interactive: { + type: plan.interactive.type, + ...(header === undefined ? {} : { header }), + body: { text }, + action: plan.interactive.action as Json + } + }) + } + if (text.trim() === "") return yield* fail(op, new InvalidRequest({ detail: "empty_message" })) + return yield* send(op, thread, user, { + ...context, + type: "text", + text: { body: text, preview_url: postOptions.linkPreview === true } + }) + }) + + const download: ChatAdapter["download"] = (thread, attachment, { maxBytes }) => + Stream.unwrap(Effect.gen(function*() { + const op = "download" + yield* resolve(op, thread) + const tooLarge = Errors.chatError(op, new TooLarge({ subject: "files", detail: "max_bytes" })) + if (attachment.size !== undefined && attachment.size > maxBytes) return yield* Effect.fail(tooLarge) + const answer = yield* graph.call(encodeURIComponent(attachment.id), { + params: { phone_number_id: phoneNumberId } + }).pipe(Effect.mapError(withOp(op))) + const info = isObject(answer) ? answer : undefined + const url = string(info, "url") + if (url === undefined) return yield* fail(op, Errors.invalidResponse(false, "missing_url")) + const size = info?.file_size + if (typeof size === "number" && size > maxBytes) return yield* Effect.fail(tooLarge) + const response = yield* options.http.execute( + HttpClientRequest.get(url).pipe(HttpClientRequest.bearerToken(Redacted.value(options.accessToken))) + ).pipe(Effect.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error)))) + if (response.status < 200 || response.status >= 300) { + return yield* fail(op, Errors.reasonForStatus(response.status, { write: false })) + } + let received = 0 + return response.stream.pipe( + Stream.mapError((error) => Errors.chatError(op, Errors.transport(false, error.reason._tag, error))), + Stream.mapEffect((chunk) => { + received += chunk.length + return received > maxBytes ? Effect.fail(tooLarge) : Effect.succeed(chunk) + }) + ) + })) + + const adapter: ChatAdapter = { + descriptor, + post, + edit: () => unsupported("edit", "edit"), + delete: () => unsupported("delete", "delete"), + fetchMessage: () => unsupported("fetchMessage", "fetchMessage"), + history: () => Stream.fail(Errors.chatError("history", new Unsupported({ capability: "history" }))), + download, + startThread: () => unsupported("startThread", "startThread"), + getThread: (thread) => + Effect.map(resolve("getThread", thread), (user) => ({ + thread, + kind: "direct" as const, + raw: { phoneNumberId, user } + })), + opener: (thread) => Effect.as(resolve("opener", thread), Option.none()), + getUser: () => unsupported("getUser", "getUser"), + followUp: () => unsupported("followUp", "commands") + } + + const sendTemplate = (thread: ChatThread, template: Template): Effect.Effect => + Effect.flatMap(resolve("sendTemplate", thread), (user) => + send("sendTemplate", thread, user, { + type: "template", + template: { + name: template.name, + language: { code: template.language }, + ...(template.components === undefined ? {} : { components: template.components }) + } + })) + + const markRead = ( + thread: ChatThread, + messageId: string, + markOptions?: { readonly typing?: boolean | undefined } + ): Effect.Effect => + Effect.flatMap(resolve("markRead", thread), () => + graph.call(`${phoneNumberId}/messages`, { + body: { + messaging_product: "whatsapp", + status: "read", + message_id: messageId, + ...(markOptions?.typing === true ? { typing_indicator: { type: "text" } } : {}) + }, + write: true + }).pipe(Effect.mapError(withOp("markRead")), Effect.asVoid)) + + return { adapter, sendTemplate, markRead } +} diff --git a/packages/chat/whatsapp/src/internal/address.ts b/packages/chat/whatsapp/src/internal/address.ts new file mode 100644 index 00000000000..ce9a969c923 --- /dev/null +++ b/packages/chat/whatsapp/src/internal/address.ts @@ -0,0 +1,41 @@ +import * as ChatThread from "effect/chat/ChatThread" +import * as Option from "effect/Option" + +/** @internal */ +export const platform = "whatsapp" + +/** @internal */ +export interface Parts { + readonly phoneNumberId: string + readonly kind: "user" | "group" + readonly id: string +} + +const digits = /^\d+$/ + +/** + * A business-scoped user id, such as `US.13491208655302741918`, or a parent + * one, such as `US.ENT.11815799212886844830`. The grammar is read from + * examples, not from a specification. + * + * @internal + */ +export const isBsuid = (id: string): boolean => /^[A-Z]{2}(?:\.[A-Z0-9]+)+$/.test(id) + +/** A phone number in digits, or a BSUID. @internal */ +export const isUser = (id: string): boolean => digits.test(id) || isBsuid(id) + +/** @internal */ +export const thread = (parts: Parts): ChatThread.ChatThread => + ChatThread.make(platform, `${parts.phoneNumberId}/${parts.kind === "user" ? "u" : "g"}/${parts.id}`) + +/** @internal */ +export const parts = (address: ChatThread.ChatThread): Option.Option => { + if (address.platform !== platform) return Option.none() + const segments = address.id.split("/") + if (segments.length !== 3 || !digits.test(segments[0]) || segments[2].length === 0) return Option.none() + const [phoneNumberId, kind, id] = segments + if (kind === "u") return isUser(id) ? Option.some({ phoneNumberId, kind: "user", id }) : Option.none() + if (kind === "g") return Option.some({ phoneNumberId, kind: "group", id }) + return Option.none() +} diff --git a/packages/chat/whatsapp/src/internal/errors.ts b/packages/chat/whatsapp/src/internal/errors.ts new file mode 100644 index 00000000000..873b11bd6f8 --- /dev/null +++ b/packages/chat/whatsapp/src/internal/errors.ts @@ -0,0 +1,103 @@ +import { + ChatError, + type ChatErrorReason, + Forbidden, + InvalidRequest, + InvalidResponse, + NotFound, + PlatformUnavailable, + RateLimited, + TooLarge, + Transport, + Unauthenticated, + Unknown +} from "effect/chat/ChatError" +import { platform } from "./address.ts" + +/** The detail of a send refused outside the 24 hour window, Graph error 131047. @internal */ +export const outsideWindow = "graph_131047" + +const unauthenticated = new Set([0, 102, 190]) + +const forbidden = new Set([ + 3, + 10, + 368, + 131030, + 131031, + 131042, + 131045, + 131047, + 131049, + 131050, + 133010 +]) + +const rateLimited = new Set([4, 17, 32, 613, 80007, 130429, 131048, 131056]) + +const unavailable = new Set([1, 2, 131000, 131016]) + +/** An error a Graph answer carries, in an `error` field or in a status's `errors`. @internal */ +export interface GraphError { + readonly code: number + readonly subcode?: number | undefined +} + +const isObject = (u: unknown): u is Record => typeof u === "object" && u !== null && !Array.isArray(u) + +/** The Graph error in an answer's body, if it has one. @internal */ +export const graphError = (body: unknown): GraphError | undefined => { + const error = isObject(body) && isObject(body.error) ? body.error : body + if (!isObject(error) || typeof error.code !== "number") return undefined + return { code: error.code, ...(typeof error.error_subcode === "number" ? { subcode: error.error_subcode } : {}) } +} + +/** + * Maps a Graph error code to a reason. `detail` is `graph_{code}`, which + * holds no message text or ids. The Graph error itself is the `cause`. + * + * @internal + */ +export const reasonForGraphError = ( + error: GraphError, + options: { readonly write: boolean; readonly cause?: unknown } +): ChatErrorReason => { + const fields = { detail: `graph_${error.code}`, ...(options.cause === undefined ? {} : { cause: options.cause }) } + if (rateLimited.has(error.code)) return new RateLimited(fields) + if (unauthenticated.has(error.code)) return new Unauthenticated(fields) + if (forbidden.has(error.code) || (error.code >= 200 && error.code < 300)) return new Forbidden(fields) + if (unavailable.has(error.code)) { + return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", ...fields }) + } + if (error.code === 100 && error.subcode === 33) return new NotFound(fields) + if (error.code === 131053) return new TooLarge({ subject: "files", ...fields }) + return new InvalidRequest(fields) +} + +/** @internal */ +export const reasonForStatus = ( + status: number, + options: { readonly write: boolean; readonly cause?: unknown } +): ChatErrorReason => { + const fields = { detail: `http_${status}`, ...(options.cause === undefined ? {} : { cause: options.cause }) } + if (status === 429) return new RateLimited(fields) + if (status >= 500) return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", ...fields }) + if (status === 401) return new Unauthenticated(fields) + if (status === 403) return new Forbidden(fields) + if (status === 404) return new NotFound(fields) + if (status === 413) return new TooLarge(fields) + return new InvalidRequest(fields) +} + +/** A request that failed before an answer came. A write may have landed. @internal */ +export const transport = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => + new Transport({ retry: write ? "uncertain" : "retry", detail, ...(cause === undefined ? {} : { cause }) }) + +/** An answer the adapter cannot read. A write may have landed. @internal */ +export const invalidResponse = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => { + const fields = { detail, ...(cause === undefined ? {} : { cause }) } + return write ? new Unknown(fields) : new InvalidResponse(fields) +} + +/** @internal */ +export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason }) diff --git a/packages/chat/whatsapp/src/internal/normalize.ts b/packages/chat/whatsapp/src/internal/normalize.ts new file mode 100644 index 00000000000..b9e5fa3b394 --- /dev/null +++ b/packages/chat/whatsapp/src/internal/normalize.ts @@ -0,0 +1,254 @@ +import type * as ChatEvent from "effect/chat/ChatEvent" +import type * as ChatInteraction from "effect/chat/ChatInteraction" +import type { Attachment, ChatMessage } from "effect/chat/ChatMessage" +import type { ChatThread } from "effect/chat/ChatThread" +import * as DateTime from "effect/DateTime" +import type * as Schema from "effect/Schema" +import type * as Update from "../WhatsAppUpdate.ts" +import { parseWireId } from "./actions.ts" +import * as Address from "./address.ts" +import { reasonForGraphError } from "./errors.ts" + +type Json = Schema.Json +type JsonObject = { readonly [key: string]: Json } + +/** What one webhook delivery holds, in order. @internal */ +export type Inbound = + | ChatEvent.MessageCreated + | ChatEvent.ReactionAdded + | ChatInteraction.ButtonClicked + | ChatInteraction.SelectChanged + | Update.WhatsAppUpdate + +/** A phone number without its formatting: Meta may show `display_phone_number` with `+`, spaces or dashes. */ +const digits = (phone: string) => phone.replace(/\D/g, "") + +const isObject = (u: unknown): u is JsonObject => typeof u === "object" && u !== null && !Array.isArray(u) + +const string = (object: JsonObject | undefined, key: string): string | undefined => { + const value = object?.[key] + return typeof value === "string" && value.length > 0 ? value : undefined +} + +const object = (object: JsonObject | undefined, key: string): JsonObject | undefined => { + const value = object?.[key] + return isObject(value) ? value : undefined +} + +const array = (value: Json | undefined): ReadonlyArray => Array.isArray(value) ? value : [] + +const timestamp = (value: string | undefined) => { + const seconds = Number(value) + return value !== undefined && Number.isFinite(seconds) ? DateTime.makeUnsafe(seconds * 1000) : undefined +} + +const media = ["image", "video", "audio", "document", "sticker"] as const + +/** Whether a message is from a group, which the adapter does not support yet. @internal */ +export const isGroupMessage = (message: Json): boolean => + isObject(message) && (message.group_id !== undefined || string(object(message, "context"), "group_id") !== undefined) + +/** The messages of a delivery that are from groups. @internal */ +export const groupMessages = (envelope: unknown): number => { + let count = 0 + if (!isObject(envelope)) return count + for (const entry of array(envelope.entry)) { + for (const change of array(isObject(entry) ? entry.changes : undefined)) { + const value = isObject(change) ? object(change, "value") : undefined + for (const message of array(value?.messages)) if (isGroupMessage(message)) count++ + for (const status of array(value?.statuses)) if (isObject(status) && status.recipient_type === "group") count++ + } + } + return count +} + +/** A changed phone number or BSUID, addressed by the user's new id. */ +const fromSystem = ( + value: JsonObject, + metadata: JsonObject, + phoneNumberId: string, + message: JsonObject, + id: string, + at: DateTime.Utc | undefined +): ReadonlyArray => { + const system = object(message, "system") + const change = string(system, "type") + if (change !== "user_changed_number" && change !== "user_changed_user_id") return [] + const userId = string(system, "user_id") + const phone = string(system, "wa_id") + const next = userId ?? phone + if (next === undefined || !Address.isUser(next)) return [] + const previousUserId = string(system, "previous_user_id") + const previousPhone = string(message, "from") + return [{ + _tag: "UserIdChanged", + thread: Address.thread({ phoneNumberId, kind: "user", id: next }), + id, + change, + ...(userId === undefined ? {} : { userId }), + ...(previousUserId === undefined ? {} : { previousUserId }), + ...(phone === undefined ? {} : { phone }), + ...(previousPhone === undefined ? {} : { previousPhone }), + ...(at === undefined ? {} : { timestamp: at }), + raw: { metadata, contacts: array(value.contacts).filter(isObject), messages: [message] } + }] +} + +const fromMessage = ( + value: JsonObject, + metadata: JsonObject, + phoneNumberId: string, + message: Json +): ReadonlyArray => { + if (!isObject(message) || isGroupMessage(message)) return [] + const id = string(message, "id") + const at = timestamp(string(message, "timestamp")) + const type = string(message, "type") + // A changed BSUID can arrive with no sender at all: the new id is only in `system`. + if (type === "system") return id === undefined ? [] : fromSystem(value, metadata, phoneNumberId, message, id, at) + const contacts = array(value.contacts).filter(isObject) + const from = string(message, "from") + const fromUserId = string(message, "from_user_id") + const contact = + contacts.find((contact) => + (fromUserId !== undefined && contact.user_id === fromUserId) || (from !== undefined && contact.wa_id === from) + ) ?? contacts[0] + const user = fromUserId ?? string(contact, "user_id") ?? from + if (id === undefined || user === undefined || !Address.isUser(user)) return [] + const thread = Address.thread({ phoneNumberId, kind: "user", id: user }) + const raw: Json = { metadata, contacts: contact === undefined ? [] : [contact], messages: [message] } + switch (type) { + case "reaction": { + const reaction = object(message, "reaction") + const emoji = string(reaction, "emoji") + const messageId = string(reaction, "message_id") + // An empty emoji is a removed reaction, which is dropped. + if (emoji === undefined || messageId === undefined) return [] + return [{ + _tag: "ReactionAdded", + thread, + messageId, + userId: user, + emoji, + ...(at === undefined ? {} : { timestamp: at }), + raw + }] + } + case "interactive": { + const interactive = object(message, "interactive") + const reply = object(interactive, "button_reply") ?? object(interactive, "list_reply") + const wire = string(reply, "id") + const parsed = wire === undefined ? undefined : parseWireId(wire) + const messageId = string(object(message, "context"), "id") + if (parsed === undefined || messageId === undefined) return [] + const origin = { thread, messageId, userId: user, raw } + return parsed.kind === "select" + ? [{ _tag: "SelectChanged", id: parsed.id, value: parsed.value, ...origin }] + : [{ + _tag: "ButtonClicked", + id: parsed.id, + ...(parsed.value === undefined ? {} : { value: parsed.value }), + ...origin + }] + } + case "unsupported": + case "request_welcome": + return [] + } + const attachments: Array = [] + let text = string(object(message, "text"), "body") ?? string(object(message, "button"), "text") ?? "" + for (const kind of media) { + const item = object(message, kind) + const mediaId = string(item, "id") + if (mediaId === undefined) continue + const name = string(item, "filename") + const contentType = string(item, "mime_type") + attachments.push({ + id: mediaId, + ...(name === undefined ? {} : { name }), + ...(contentType === undefined ? {} : { contentType }) + }) + text = string(item, "caption") ?? text + } + const context = object(message, "context") + const replyId = string(context, "id") + const quoted = string(context, "from") + const name = string(object(contact, "profile"), "name") + const chatMessage: ChatMessage = { + id, + thread, + author: { id: user, ...(name === undefined ? {} : { name }), isBot: false, isSelf: false }, + text, + mentions: [], + ...(replyId === undefined ? {} : { + replyTo: { + messageId: replyId, + ...(quoted === undefined + ? {} + : { isSelf: digits(quoted) === digits(string(metadata, "display_phone_number") ?? "") }) + } + }), + attachments, + timestamp: at ?? DateTime.makeUnsafe(0), + raw: message + } + return [{ _tag: "MessageCreated", thread, kind: "direct", message: chatMessage, raw }] +} + +const statuses = new Set(["sent", "delivered", "read", "failed"]) + +const fromStatus = (metadata: JsonObject, phoneNumberId: string, status: Json): ReadonlyArray => { + if (!isObject(status) || status.recipient_type === "group") return [] + const messageId = string(status, "id") + const value = string(status, "status") + const phone = string(status, "recipient_id") + const userId = string(status, "recipient_user_id") + const user = userId ?? phone + if (messageId === undefined || value === undefined || !statuses.has(value) || user === undefined) return [] + if (!Address.isUser(user)) return [] + const thread: ChatThread = Address.thread({ phoneNumberId, kind: "user", id: user }) + const raw: Json = { metadata, statuses: [status] } + const at = timestamp(string(status, "timestamp")) + const error = array(status.errors).find(isObject) + const code = error?.code + const out: Array = [{ + _tag: "Status", + thread, + messageId, + status: value as Update.Status["status"], + ...(at === undefined ? {} : { timestamp: at }), + ...(value === "failed" && typeof code === "number" + ? { reason: reasonForGraphError({ code }, { write: false, cause: error }) } + : {}), + ...(phone === undefined ? {} : { phone }), + ...(userId === undefined ? {} : { userId }), + raw + }] + if (value !== "failed" && phone !== undefined && userId !== undefined) { + out.push({ _tag: "Alias", thread, phone, userId, messageId, status: value as Update.Alias["status"], raw }) + } + return out +} + +/** @internal */ +export const normalize = (envelope: unknown, context: { readonly phoneNumberId: string }): ReadonlyArray => { + const out: Array = [] + if (!isObject(envelope) || envelope.object !== "whatsapp_business_account") return out + for (const entry of array(envelope.entry)) { + for (const change of array(isObject(entry) ? entry.changes : undefined)) { + if (!isObject(change) || change.field !== "messages") continue + const value = object(change, "value") + const metadata = object(value, "metadata") + if ( + value === undefined || metadata === undefined || string(metadata, "phone_number_id") !== context.phoneNumberId + ) { + continue + } + for (const message of array(value.messages)) { + out.push(...fromMessage(value, metadata, context.phoneNumberId, message)) + } + for (const status of array(value.statuses)) out.push(...fromStatus(metadata, context.phoneNumberId, status)) + } + } + return out +} diff --git a/packages/chat/whatsapp/src/internal/options.ts b/packages/chat/whatsapp/src/internal/options.ts new file mode 100644 index 00000000000..593117ec5c7 --- /dev/null +++ b/packages/chat/whatsapp/src/internal/options.ts @@ -0,0 +1,19 @@ +import * as Context from "effect/Context" + +/** @internal */ +export interface PostOptions { + readonly linkPreview?: boolean | undefined + readonly listButton?: string | undefined + readonly buttonsSection?: string | undefined +} + +/** @internal */ +export const CurrentPostOptions = Context.Reference("@effect/chat-whatsapp/CurrentPostOptions", { + defaultValue: () => ({}) +}) + +/** @internal */ +export const defaultListButton = "Options" + +/** @internal */ +export const defaultButtonsSection = "Actions" diff --git a/packages/chat/whatsapp/src/internal/text.ts b/packages/chat/whatsapp/src/internal/text.ts new file mode 100644 index 00000000000..d73defd394b --- /dev/null +++ b/packages/chat/whatsapp/src/internal/text.ts @@ -0,0 +1,495 @@ +/** + * Markdown to WhatsApp text. + * + * Each construct the renderer knows has a candidate replacement, rendered + * bottom-up, which is emitted only when it is no longer than the + * construct's source in code points, UTF-16 units and UTF-8 bytes. Otherwise + * the source is emitted as it is. Text between constructs is always kept, so + * the output is never longer than the input in any of the three units. + */ + +/** + * The longest text message, and the measure's unit: UTF-16 code units, which + * are never fewer than the code points Meta counts. @internal + */ +export const messageLength = 4096 + +/** The longest caption, and the longest body beside reply buttons or a link button. @internal */ +export const shortBodyLength = 1024 + +/** @internal */ +export const measure = (markdown: string): number => markdown.length + +const codePoints = (s: string): number => { + let n = 0 + for (const _ of s) n++ + return n +} + +/** @internal */ +export const utf8Bytes = (s: string): number => { + let n = 0 + for (const char of s) { + const code = char.codePointAt(0)! + n += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4 + } + return n +} + +/** Whether `candidate` is no longer than `source` in all three units. @internal */ +export const fits = (candidate: string, source: string): boolean => + candidate.length <= source.length && codePoints(candidate) <= codePoints(source) && + utf8Bytes(candidate) <= utf8Bytes(source) + +const pick = (candidate: string, source: string): string => fits(candidate, source) ? candidate : source + +/** The characters WhatsApp formats with, which a decoded reference must not produce. */ +const formatting = new Set(["*", "_", "~", "`"]) + +const entities: Record = { + amp: "&", + lt: "<", + gt: ">", + quot: "\"", + apos: "'", + nbsp: " ", + copy: "©", + reg: "®", + trade: "™", + hellip: "…", + mdash: "—", + ndash: "–", + lsquo: "‘", + rsquo: "’", + ldquo: "“", + rdquo: "”", + laquo: "«", + raquo: "»", + bull: "•", + middot: "·", + deg: "°", + plusmn: "±", + times: "×", + divide: "÷", + minus: "−", + euro: "€", + pound: "£", + yen: "¥", + cent: "¢", + sect: "§", + para: "¶", + larr: "←", + rarr: "→", + uarr: "↑", + darr: "↓", + harr: "↔", + lArr: "⇐", + rArr: "⇒", + hArr: "⇔", + le: "≤", + ge: "≥", + ne: "≠", + asymp: "≈", + infin: "∞", + frac12: "½", + frac14: "¼", + frac34: "¾", + iexcl: "¡", + iquest: "¿" +} + +const decodeReference = (reference: string): string | undefined => { + const body = reference.slice(1, -1) + let decoded: string | undefined + if (body.startsWith("#")) { + const code = body[1] === "x" || body[1] === "X" + ? Number.parseInt(body.slice(2), 16) + : Number.parseInt(body.slice(1), 10) + decoded = code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff) ? "�" : String.fromCodePoint(code) + } else { + decoded = Object.hasOwn(entities, body) ? entities[body] : undefined + } + return decoded === undefined || formatting.has(decoded) ? undefined : decoded +} + +const isWhitespace = (char: string | undefined) => char === undefined || /\s/u.test(char) +const isPunctuation = (char: string | undefined) => char !== undefined && /[\p{P}\p{S}]/u.test(char) +const isAsciiPunctuation = (char: string | undefined) => char !== undefined && /[!-/:-@[-`{-~]/.test(char) + +interface Atom { + readonly end: number + readonly text: string +} + +const runLength = (s: string, i: number, char: string) => { + let j = i + while (s[j] === char) j++ + return j - i +} + +/** A code span from the backtick run at `i`, or the run itself when it has no match. */ +const codeSpan = (s: string, i: number): Atom => { + const n = runLength(s, i, "`") + let j = i + n + while (j < s.length) { + if (s[j] !== "`") { + j++ + continue + } + const m = runLength(s, j, "`") + if (m === n) return { end: j + m, text: s.slice(i, j + m) } + j += m + } + return { end: i + n, text: s.slice(i, i + n) } +} + +const autolink = /^<([A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*)>/ +const emailAutolink = + /^<([A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*)>/ +const html = /^(?:|<\/?[A-Za-z][A-Za-z0-9-]*(?:\s[^<>]*)?\/?>)/ +const reference = /^&(?:#[0-9]{1,7};|#[xX][0-9A-Fa-f]{1,6};|[A-Za-z][A-Za-z0-9]{1,31};)/ + +/** The index of the `]` closing the `[` at `i`, skipping escapes and code spans. */ +const closeBracket = (s: string, i: number): number => { + let depth = 0 + let j = i + while (j < s.length) { + const char = s[j] + if (char === "\\") j += 2 + else if (char === "`") j = codeSpan(s, j).end + else { + if (char === "[") depth++ + else if (char === "]" && --depth === 0) return j + j++ + } + } + return -1 +} + +/** An inline destination and optional title from the `(` at `i`: the URL and the index after `)`. */ +const destination = (s: string, i: number): { readonly url: string; readonly end: number } | undefined => { + let j = i + 1 + while (s[j] === " " || s[j] === "\t") j++ + let url: string + if (s[j] === "<") { + const close = s.indexOf(">", j) + if (close < 0 || s.slice(j + 1, close).includes("<")) return undefined + url = s.slice(j + 1, close) + j = close + 1 + } else { + const start = j + let depth = 0 + while (j < s.length && !/\s/.test(s[j])) { + if (s[j] === "\\" && isAsciiPunctuation(s[j + 1])) j++ + else if (s[j] === "(") depth++ + else if (s[j] === ")") { + if (depth === 0) break + depth-- + } + j++ + } + if (depth !== 0) return undefined + url = s.slice(start, j) + } + const afterUrl = j + while (s[j] === " " || s[j] === "\t") j++ + if (j > afterUrl && (s[j] === "\"" || s[j] === "'" || s[j] === "(")) { + const close = s[j] === "(" ? ")" : s[j] + let k = j + 1 + while (k < s.length && s[k] !== close) k += s[k] === "\\" ? 2 : 1 + if (k >= s.length) return undefined + j = k + 1 + while (s[j] === " " || s[j] === "\t") j++ + } + return s[j] === ")" ? { url, end: j + 1 } : undefined +} + +/** A link or image from the `[` at `i`, `start` being the `!` of an image. */ +const link = (s: string, start: number, i: number): Atom | undefined => { + if (s[i + 1] === "^") { + const close = s.indexOf("]", i) + return close < 0 ? undefined : { end: close + 1, text: s.slice(start, close + 1) } + } + const close = closeBracket(s, i) + if (close < 0) return undefined + if (s[close + 1] === "[") { + const end = s.indexOf("]", close + 1) + return end < 0 ? undefined : { end: end + 1, text: s.slice(start, end + 1) } + } + // A shortcut reference, or plain brackets: either way it is kept as typed. + const verbatim = { end: close + 1, text: s.slice(start, close + 1) } + if (s[close + 1] !== "(") return verbatim + const target = destination(s, close + 1) + if (target === undefined) return verbatim + const label = s.slice(i + 1, close) + const shown = start < i ? label : inline(label) + const candidate = label === "" || label === target.url ? target.url : shown + " (" + target.url + ")" + return { end: target.end, text: pick(candidate, s.slice(start, target.end)) } +} + +/** The construct at `i` that is kept or replaced as a whole, if any. */ +const atom = (s: string, i: number): Atom | undefined => { + switch (s[i]) { + case "\\": + return isAsciiPunctuation(s[i + 1]) ? { end: i + 2, text: s.slice(i, i + 2) } : undefined + case "`": + return codeSpan(s, i) + case "<": { + const rest = s.slice(i) + const match = autolink.exec(rest) ?? emailAutolink.exec(rest) + if (match !== null) return { end: i + match[0].length, text: pick(match[1], match[0]) } + const tag = html.exec(rest) + return tag === null ? undefined : { end: i + tag[0].length, text: tag[0] } + } + case "!": + return s[i + 1] === "[" ? link(s, i, i + 1) : undefined + case "[": + return link(s, i, i) + case "&": { + const match = reference.exec(s.slice(i, i + 40)) + if (match === null) return undefined + const decoded = decodeReference(match[0]) + return { end: i + match[0].length, text: decoded === undefined ? match[0] : pick(decoded, match[0]) } + } + } + return undefined +} + +const leftFlanking = (before: string | undefined, after: string | undefined) => + !isWhitespace(after) && (!isPunctuation(after) || isWhitespace(before) || isPunctuation(before)) + +const rightFlanking = (before: string | undefined, after: string | undefined) => + !isWhitespace(before) && (!isPunctuation(before) || isWhitespace(after) || isPunctuation(after)) + +const canOpen = (s: string, i: number, n: number) => { + const before = s[i - 1] + const after = s[i + n] + const left = leftFlanking(before, after) + return s[i] === "_" ? left && (!rightFlanking(before, after) || isPunctuation(before)) : left +} + +const canClose = (s: string, i: number, n: number) => { + const before = s[i - 1] + const after = s[i + n] + const right = rightFlanking(before, after) + return s[i] === "_" ? right && (!leftFlanking(before, after) || isPunctuation(after)) : right +} + +/** Emphasis or strikethrough from the delimiter run at `i`, if it has a closer. */ +const emphasis = (s: string, i: number): Atom | undefined => { + const char = s[i] + const n = runLength(s, i, char) + if (n > 2 || !canOpen(s, i, n)) return undefined + let j = i + n + while (j < s.length) { + const skipped = s[j] === "*" || s[j] === "_" || s[j] === "~" ? undefined : atom(s, j) + if (skipped !== undefined) { + j = skipped.end + continue + } + if (s[j] !== char) { + j++ + continue + } + const m = runLength(s, j, char) + if (m === n && j > i + n && canClose(s, j, m)) { + const marker = char === "~" ? "~" : n === 2 ? "*" : "_" + const candidate = marker + inline(s.slice(i + n, j)) + marker + return { end: j + m, text: pick(candidate, s.slice(i, j + m)) } + } + j += m + } + return undefined +} + +/** One line of inline Markdown. @internal */ +export const inline = (s: string): string => { + let out = "" + let i = 0 + while (i < s.length) { + const char = s[i] + const found = char === "*" || char === "_" || char === "~" ? emphasis(s, i) : atom(s, i) + if (found !== undefined) { + out += found.text + i = found.end + } else if (char === "*" || char === "_" || char === "~") { + const n = runLength(s, i, char) + out += s.slice(i, i + n) + i += n + } else { + out += char + i++ + } + } + return out +} + +const fenceOpen = /^( {0,3})(`{3,}|~{3,})(.*)$/ +const atxHeading = /^ {0,3}(#{1,6})(?:[ \t]+(.*))?$/ +const setextUnderline = /^ {0,3}(?:=+|-+)[ \t]*$/ +const thematicBreak = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/ +const definition = /^ {0,3}\[[^\]]+\]:/ +const blockStart = /^(?: {4}|\t| {0,3}(?:[>#|]|[-*+](?:[ \t]|$)|\d{1,9}[.)](?:[ \t]|$)|`{3}|~{3}))/ +const tableDelimiter = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/ +const quoteMarker = /^ {0,3}>[ \t]?/ +const listMarker = /^( {0,3}(?:[-*+]|\d{1,9}[.)]))([ \t]+|$)/ + +const cells = (row: string) => row.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").length + +const isBlank = (line: string) => line.trim() === "" + +/** + * The quotes and list items a line opens, outermost first, and the rest of + * the line. A quote is 0, and a list item the indentation its content needs. + */ +const containers = (line: string): { readonly stack: ReadonlyArray; readonly rest: string } => { + const stack: Array = [] + let rest = line + while (true) { + const quote = quoteMarker.exec(rest) + if (quote !== null) { + stack.push(0) + rest = rest.slice(quote[0].length) + continue + } + const item = listMarker.exec(rest) + if (item === null || thematicBreak.test(rest)) break + // Five spaces or more after the marker start indented code, whose content starts after one. + const width = item[2] === "" || item[2].length >= 5 ? item[1].length + 1 : item[0].length + stack.push(width) + rest = rest.slice(Math.min(width, item[0].length)) + } + return { stack, rest } +} + +/** + * A line with the tabs of its indentation and container markers expanded to + * the next multiple of four columns, so a tab only partly taken by a + * container leaves its other columns. Used to find blocks, never as output. + */ +const expandIndent = (line: string): string => { + let out = "" + for (let k = 0; k < line.length; k++) { + const char = line[k] + if (char === "\t") out += " ".repeat(4 - (out.length % 4)) + else if (/[ >\-*+.)0-9]/.test(char)) out += char + else return out + line.slice(k) + } + return out +} + +/** + * What is left of a line, with its indentation expanded, inside the + * containers of `stack`, or `undefined` when the line ends one of them. + */ +const inside = (line: string, stack: ReadonlyArray): string | undefined => { + let rest = line + for (const width of stack) { + if (width === 0) { + const quote = quoteMarker.exec(rest) + if (quote === null) return undefined + rest = rest.slice(quote[0].length) + continue + } + if (isBlank(rest)) return "" + if (!rest.startsWith(" ".repeat(width))) return undefined + rest = rest.slice(width) + } + return rest +} + +/** Whether a line starts a block, which ends a definition or a table that was being kept as typed. */ +const opensBlock = (line: string) => + fenceOpen.test(line) || atxHeading.test(line) || thematicBreak.test(line) || containers(line).stack.length > 0 + +/** + * Markdown as WhatsApp text, never longer than the Markdown in code points, + * UTF-16 units or UTF-8 bytes. + * + * @internal + */ +export const render = (markdown: string): string => { + const lines = markdown.split(/(?<=\n)/) + const body = (k: number) => lines[k].replace(/\r?\n$/, "") + const eol = (k: number) => lines[k].slice(body(k).length) + let out = "" + let i = 0 + while (i < lines.length) { + const line = body(i) + const { rest, stack } = containers(expandIndent(line)) + const fence = fenceOpen.exec(rest) + if (fence !== null && !(fence[2][0] === "`" && fence[3].includes("`"))) { + // The closer is matched inside the opener's quotes and list items. A line outside them ends the code too. + const closing = new RegExp("^ {0,3}" + (fence[2][0] === "~" ? "~" : "`") + "{" + fence[2].length + ",}[ \\t]*$") + let j = i + 1 + let closed = false + for (; j < lines.length; j++) { + const code = inside(expandIndent(body(j)), stack) + if (code === undefined) break + if (closing.test(code)) { + closed = true + break + } + } + if (!closed) { + out += lines.slice(i, j).join("") + i = j + continue + } + const content = lines.slice(i + 1, j).join("") + const source = lines.slice(i, j + 1).join("") + const candidate = "```" + eol(i) + content + "```" + eol(j) + out += stack.length > 0 || content.includes("```") ? source : pick(candidate, source) + i = j + 1 + continue + } + if (definition.test(rest)) { + // A destination or title may continue on the next lines, up to a blank line or the start of a block. + out += lines[i] + let j = i + 1 + while (j < lines.length) { + const next = inside(expandIndent(body(j)), stack) ?? expandIndent(body(j)) + if (isBlank(next) || opensBlock(next)) break + out += lines[j++] + } + i = j + continue + } + if (i + 1 < lines.length && line.includes("|") && tableDelimiter.test(body(i + 1)) && body(i + 1).includes("|")) { + if (cells(line) === cells(body(i + 1))) { + out += lines[i] + let j = i + 2 + while (j < lines.length && !isBlank(body(j)) && !opensBlock(expandIndent(body(j)))) out += lines[j++] + i = j + continue + } + } + const heading = atxHeading.exec(line) + if (heading !== null) { + const content = (heading[2] ?? "").replace(/(?:^|[ \t]+)#+[ \t]*$/, "").trim() + out += (content === "" ? line : pick("*" + inline(content) + "*", line)) + eol(i) + i++ + continue + } + if ( + i + 1 < lines.length && !isBlank(line) && !blockStart.test(line) && !thematicBreak.test(line) && + !definition.test(line) && (i === 0 || isBlank(body(i - 1))) && setextUnderline.test(body(i + 1)) + ) { + const source = line + eol(i) + body(i + 1) + out += pick("*" + inline(line.trim()) + "*", source) + eol(i + 1) + i += 2 + continue + } + // Indented code, measured inside its quotes and list items with tabs expanded, is kept as typed. + if (/^ {4}/.test(rest) || thematicBreak.test(line)) { + out += lines[i] + i++ + continue + } + // The text is cut from the line as typed, so the output keeps its tabs. + const text = containers(line).rest + out += line.slice(0, line.length - text.length) + inline(text) + eol(i) + i++ + } + return out +} diff --git a/packages/chat/whatsapp/src/internal/webhook.ts b/packages/chat/whatsapp/src/internal/webhook.ts new file mode 100644 index 00000000000..8d0afc769af --- /dev/null +++ b/packages/chat/whatsapp/src/internal/webhook.ts @@ -0,0 +1,234 @@ +import { Unsupported } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import type { NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as Clock from "effect/Clock" +import * as Mac from "effect/crypto/Mac" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import type * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as HttpServerResponse from "effect/http/HttpServerResponse" +import * as Option from "effect/Option" +import * as Redacted from "effect/Redacted" +import * as Stream from "effect/Stream" +import type { WhatsAppUpdate } from "../WhatsAppUpdate.ts" +import { chatError } from "./errors.ts" +import { groupMessages, type Inbound } from "./normalize.ts" + +const encoder = new TextEncoder() + +/** + * Checks Meta's `X-Hub-Signature-256`: `sha256=` and the hex HMAC-SHA256 + * of the raw body with the app secret. + * + * @internal + */ +export const makeVerifier = Effect.fnUntraced(function*(appSecret: Redacted.Redacted) { + const mac = yield* Mac.Mac + // An invalid app secret is a configuration defect, not a route error. + const key = yield* Mac.key(Mac.hmacSha256, encoder.encode(Redacted.value(appSecret))).pipe(Effect.orDie) + return (headers: { readonly [key: string]: string | undefined }, body: Uint8Array): Effect.Effect => { + const signature = headers["x-hub-signature-256"] + if (signature === undefined || !/^sha256=(?:[0-9a-fA-F]{2})+$/.test(signature)) return Effect.succeed(false) + const hex = signature.slice(7) + const tag = new Uint8Array(hex.length / 2) + for (let i = 0; i < tag.length; i++) tag[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16) + // Crypto failures fail closed, like a mismatched signature. + return mac.verify(key, tag, body).pipe(Effect.catch(() => Effect.succeed(false))) + } +}) + +/** The body, or `None` once it passes `maxBytes`. @internal */ +export const readBody = ( + request: HttpServerRequest.HttpServerRequest, + maxBytes: number +): Effect.Effect> => { + const length = request.headers["content-length"] + if (length !== undefined && Number(length) > maxBytes) return Effect.succeed(Option.none()) + return Effect.gen(function*() { + const chunks: Array = [] + let total = 0 + const failed = yield* request.stream.pipe( + Stream.takeUntil((chunk) => { + total += chunk.length + chunks.push(chunk) + return total > maxBytes + }), + Stream.runDrain, + Effect.as(false), + Effect.catch(() => Effect.succeed(true)) + ) + if (failed || total > maxBytes) return Option.none() + const body = new Uint8Array(total) + let offset = 0 + for (const chunk of chunks) { + body.set(chunk, offset) + offset += chunk.length + } + return Option.some(body) + }) +} + +const ok = HttpServerResponse.empty({ status: 200 }) +const status = (code: number) => HttpServerResponse.empty({ status: code }) + +/** What is logged with each 503, and with a dropped delivery. @internal */ +export const logged = { + notAccepted: "whatsapp webhook: an update was refused, answered 503", + deadline: "whatsapp webhook: the request passed its deadline, answered 503", + noHandler: "whatsapp webhook: a click arrived with no ChatInteraction.Handler, dropped", + group: "whatsapp webhook: group messages are not supported, dropped" +} as const + +/** + * Meta's subscription check: `hub.mode=subscribe` with the verify token is + * answered with `hub.challenge`, anything else 403. + * + * @internal + */ +export const handleVerification = ( + request: HttpServerRequest.HttpServerRequest, + verifyToken: Redacted.Redacted +): HttpServerResponse.HttpServerResponse => { + const params = new URL(request.url, "http://localhost").searchParams + const challenge = params.get("hub.challenge") + return params.get("hub.mode") === "subscribe" && params.get("hub.verify_token") === Redacted.value(verifyToken) && + challenge !== null + ? HttpServerResponse.text(challenge) + : status(403) +} + +/** @internal */ +export interface ReceiveOptions { + readonly handle: (event: ChatEvent.ChatEvent) => Effect.Effect + readonly onUpdate?: ((update: WhatsAppUpdate) => Effect.Effect) | undefined + /** How long the click about to run may take. */ + readonly clickDeadline: Effect.Effect +} + +/** + * Runs one delivery's updates in order. A `NotAccepted` stops the rest. + * A click never fails: `Ack` and `Late` count as handled, and + * `OpenModal` is refused with `Unsupported("modals")`. + * + * @internal + */ +export const receive = ( + items: ReadonlyArray, + options: ReceiveOptions +): Effect.Effect => + Effect.forEach(items, (item): Effect.Effect => { + switch (item._tag) { + case "MessageCreated": + case "ReactionAdded": + return options.handle(item) + case "Status": + case "UserIdChanged": + case "Alias": + return options.onUpdate === undefined ? Effect.void : options.onUpdate(item) + case "ButtonClicked": + case "SelectChanged": + return Effect.flatMap(Effect.serviceOption(ChatInteraction.Handler), (handler) => { + if (Option.isNone(handler)) return Effect.logWarning(logged.noHandler) + return options.clickDeadline.pipe( + Effect.flatMap((deadline) => ChatInteraction.handle(item, deadline)), + Effect.provideService(ChatInteraction.Handler, handler.value), + Effect.flatMap((reply) => + reply._tag === "OpenModal" + // Awaited only for what is left of the click's share, then left running. + ? Effect.forkDetach( + handler.value.refused(item, chatError("openModal", new Unsupported({ capability: "modals" }))) + ).pipe( + Effect.flatMap((fiber) => + Effect.flatMap(options.clickDeadline, (left) => Effect.timeoutOption(Fiber.await(fiber), left)) + ), + Effect.asVoid + ) + : Effect.void + ) + ) + }) + } + }, { discard: true }) + +/** @internal */ +export interface Options { + readonly verify: (headers: { readonly [key: string]: string | undefined }, body: Uint8Array) => Effect.Effect + readonly normalize: (envelope: unknown) => ReadonlyArray + readonly handle: ReceiveOptions["handle"] + readonly onUpdate?: ReceiveOptions["onUpdate"] + readonly deadline: Duration.Duration + readonly maxBodyBytes: number +} + +/** What a click leaves of the deadline for the rest of the request. */ +const clickMargin = 250 + +/** + * One webhook POST. + * + * - 413 past the body limit, 401 for a bad signature, 400 for a body that + * is not a JSON object. Another `object` than + * `whatsapp_business_account` is answered 200 and dropped. + * - 503 when an update fails `NotAccepted`, or the whole request takes + * longer than the deadline. Meta then delivers the batch again. + * - A click gets what is left of the deadline, less a margin, so a slow + * click handler is detached rather than passing the deadline. + * - At the deadline the work is interrupted without waiting for its + * finalizers, so a slow release can't hold the 503 past Meta's cutoff. + * + * @internal + */ +export const handleRequest = ( + request: HttpServerRequest.HttpServerRequest, + options: Options +): Effect.Effect => + // Masked, so a cancellation can't land between forking the worker and + // arranging its interruption; only the wait itself is interruptible. + Effect.uninterruptibleMask((restore) => + Effect.gen(function*() { + const fiber = yield* Effect.forkDetach(work(request, options)) + // Interrupted without being awaited, here and at the deadline, so no finalizer delays the answer. + const stop = Effect.asVoid(Effect.forkDetach(Fiber.interrupt(fiber))) + const response = yield* Effect.onInterrupt( + restore(Effect.timeoutOption(Fiber.join(fiber), options.deadline)), + () => stop + ) + if (Option.isSome(response)) return response.value + yield* stop + yield* Effect.logWarning(logged.deadline) + return status(503) + }) + ) + +const work = ( + request: HttpServerRequest.HttpServerRequest, + options: Options +): Effect.Effect => + Effect.gen(function*() { + const until = (yield* Clock.currentTimeMillis) + Duration.toMillis(options.deadline) + const body = yield* readBody(request, options.maxBodyBytes) + if (Option.isNone(body)) return status(413) + if (!(yield* options.verify(request.headers, body.value))) return status(401) + let envelope: unknown + try { + envelope = JSON.parse(new TextDecoder().decode(body.value)) + } catch { + return status(400) + } + if (typeof envelope !== "object" || envelope === null || Array.isArray(envelope)) return status(400) + if ((envelope as { readonly object?: unknown }).object !== "whatsapp_business_account") return ok + if (groupMessages(envelope) > 0) yield* Effect.logDebug(logged.group) + return yield* receive(options.normalize(envelope), { + handle: options.handle, + onUpdate: options.onUpdate, + clickDeadline: Effect.map( + Clock.currentTimeMillis, + (now) => Duration.millis(Math.max(0, until - now - clickMargin)) + ) + }).pipe( + Effect.as(ok), + Effect.catchTag("NotAccepted", () => Effect.as(Effect.logWarning(logged.notAccepted), status(503))) + ) + }) diff --git a/packages/chat/whatsapp/test/WhatsAppChat.test.ts b/packages/chat/whatsapp/test/WhatsAppChat.test.ts new file mode 100644 index 00000000000..05485d90dd4 --- /dev/null +++ b/packages/chat/whatsapp/test/WhatsAppChat.test.ts @@ -0,0 +1,423 @@ +import { handleRequest, handleVerification, makeVerifier } from "@effect/chat-whatsapp/internal/webhook" +import * as WhatsAppChat from "@effect/chat-whatsapp/WhatsAppChat" +import type { WhatsAppUpdate } from "@effect/chat-whatsapp/WhatsAppUpdate" +import * as NodeCrypto from "@effect/platform-node/NodeCrypto" +import { assert, describe, it } from "@effect/vitest" +import * as Chat from "effect/chat/Chat" +import type { File } from "effect/chat/ChatAdapter" +import type { ChatError } from "effect/chat/ChatError" +import type * as ChatEvent from "effect/chat/ChatEvent" +import { NotAccepted } from "effect/chat/ChatHandler" +import * as ChatInteraction from "effect/chat/ChatInteraction" +import * as ChatMarkdown from "effect/chat/ChatMarkdown" +import type { Action } from "effect/chat/ChatUi" +import * as Deferred from "effect/Deferred" +import * as Duration from "effect/Duration" +import * as Effect from "effect/Effect" +import * as Fiber from "effect/Fiber" +import * as HttpServerRequest from "effect/http/HttpServerRequest" +import * as Redacted from "effect/Redacted" +import * as Scheduler from "effect/Scheduler" +import { + bsuid, + graphError, + inbound, + makeGraph, + phone, + phoneNumberId, + statuses, + thread, + whatsappLayer +} from "./utils.ts" + +const post = ( + content: { + readonly markdown: string + readonly actions?: ReadonlyArray + readonly files?: ReadonlyArray + }, + to = thread +) => Effect.flatMap(Chat.Chat, (chat) => chat.post(to, content)) + +const run = (graph: ReturnType, effect: Effect.Effect) => + effect.pipe(Effect.provide(whatsappLayer(graph))) + +const failure = (graph: ReturnType, effect: Effect.Effect) => + run(graph, Effect.flip(effect)).pipe(Effect.map((error) => error.reason)) + +const file = (name: string, contentType: string, size = 8): File => ({ + name, + contentType, + content: new Uint8Array(size) +}) + +const button = (id: string, label: string, value?: string): Action => ({ + _tag: "Button", + id, + label, + ...(value === undefined ? {} : { value }) +}) + +describe("post", () => { + it.effect("sends rendered text to a BSUID as recipient and a phone number as to, without a preview", () => + Effect.gen(function*() { + const graph = makeGraph() + yield* run(graph, post({ markdown: "**hi** [docs](https://e.com)" })) + yield* run(graph, post({ markdown: "hi" }, WhatsAppChat.thread({ phoneNumberId, user: phone }))) + const [first, second] = graph.sent() + assert.deepStrictEqual(first, { + messaging_product: "whatsapp", + recipient_type: "individual", + recipient: bsuid, + type: "text", + text: { body: "*hi* docs (https://e.com)", preview_url: false } + }) + assert.strictEqual(second.to, phone) + })) + + it.effect("lays out reply buttons, a link button, a list and appended links", () => + Effect.gen(function*() { + const graph = makeGraph() + yield* run(graph, post({ markdown: "", actions: [button("a", "Yes"), button("b", "No", "v")] })) + yield* run(graph, post({ markdown: "x", actions: [{ _tag: "LinkButton", label: "Docs", url: "https://e.com" }] })) + yield* run( + graph, + post({ + markdown: "x", + actions: [ + button("a", "Yes"), + { _tag: "Select", id: "env", placeholder: "Environment", options: [{ label: "Prod", value: "p" }] }, + { _tag: "LinkButton", label: "Docs", url: "https://e.com" } + ] + }) + ) + const [reply, cta, list] = graph.sent().map((body) => body.interactive) + assert.deepStrictEqual(reply, { + type: "button", + body: { text: "Yes, No" }, + action: { + buttons: [ + { type: "reply", reply: { id: "ba", title: "Yes" } }, + { type: "reply", reply: { id: "bb\u241Fv", title: "No" } } + ] + } + }) + assert.deepStrictEqual(cta.action, { + name: "cta_url", + parameters: { display_text: "Docs", url: "https://e.com" } + }) + assert.deepStrictEqual(list, { + type: "list", + body: { text: "x\n\nDocs: https://e.com" }, + action: { + button: "Environment", + sections: [ + { title: "Actions", rows: [{ id: "ba", title: "Yes" }] }, + { title: "Environment", rows: [{ id: "senv\u241Fp", title: "Prod" }] } + ] + } + }) + })) + + it.effect("refuses before any call", () => + Effect.gen(function*() { + const graph = makeGraph() + const rows = Array.from({ length: 6 }, (_, i) => button("b" + i, "b" + i)) + const select: Action = { + _tag: "Select", + id: "s", + options: Array.from({ length: 5 }, (_, i) => ({ label: "o" + i, value: "o" + i })) + } + const refusals: ReadonlyArray, string, string]> = [ + [post({ markdown: "x", actions: [...rows, select] }), "TooLarge", "list_rows"], + [post({ markdown: "x", actions: [button("a", "x".repeat(25))] }), "TooLarge", "label_too_long"], + [ + post({ markdown: "x", actions: [button("a", "Same"), button("b", "Same")] }), + "InvalidRequest", + "duplicate_label" + ], + [post({ markdown: "x", actions: [button("a\u241F", "A")] }), "InvalidRequest", "invalid_action_id"], + // The wire id "bx\u241F" + 126 "é" is 257 UTF-8 bytes, though within actionDataLength. + [ + post({ markdown: "x", actions: [button("x", "Yes", "é".repeat(126))] }), + "TooLarge", + "action_data_too_long" + ], + [ + post({ markdown: "x" }, { platform: "whatsapp", id: phoneNumberId + "/u/not-a-user" }), + "InvalidRequest", + "invalid_thread" + ], + [ + post({ markdown: "x" }, { platform: "whatsapp", id: phoneNumberId + "/g/120363" }), + "InvalidRequest", + "group_unsupported" + ], + [ + post({ markdown: "x", files: [file("a.txt", "text/plain"), file("b.txt", "text/plain")] }), + "TooLarge", + "file_refused" + ], + [post({ markdown: "x", files: [file("a.webp", "image/webp")] }), "TooLarge", "file_refused"], + [post({ markdown: "x", files: [file("a.png", "image/png", 5 * 1024 * 1024 + 1)] }), "TooLarge", "file_refused"], + [post({ markdown: "x", files: [file("a.ogg", "audio/ogg")] }), "TooLarge", "file_refused"], + [post({ markdown: "x".repeat(1025), files: [file("a.txt", "text/plain")] }), "TooLarge", "file_refused"], + [ + post({ markdown: "x", actions: rows.slice(0, 4), files: [file("a.txt", "text/plain")] }), + "TooLarge", + "file_refused" + ] + ] + for (const [effect, tag, detail] of refusals) { + const reason = yield* failure(graph, effect) + assert.deepStrictEqual([reason._tag, reason.detail], [tag, detail]) + } + assert.deepStrictEqual(graph.calls, []) + })) + + it.effect("names the messaging account on every send when one is configured", () => + Effect.gen(function*() { + const graph = makeGraph() + yield* Effect.gen(function*() { + yield* post({ markdown: "hi" }) + const whatsapp = yield* WhatsAppChat.WhatsAppChat + yield* whatsapp.sendTemplate(thread, { name: "hello", language: "en_US" }) + }).pipe(Effect.provide(whatsappLayer(graph, { messagingAccountId: "ma-1" }))) + assert.deepStrictEqual(graph.sent().map((body) => body.messaging_account_id), ["ma-1", "ma-1"]) + })) + + it.effect("uploads a file as the header of reply buttons, and Markdown as text/plain", () => + Effect.gen(function*() { + const graph = makeGraph() + yield* run( + graph, + post({ markdown: "notes", actions: [button("a", "Ok")], files: [file("a.md", "text/markdown")] }) + ) + const [upload, send] = graph.calls + assert.strictEqual(upload.body.type, "text/plain") + assert.strictEqual(upload.body.file.name, "a.md") + assert.deepStrictEqual(send.body.interactive.header, { + type: "document", + document: { id: "media-1", filename: "a.md" } + }) + })) + + it.effect("posts the text alone when Meta refuses the upload", () => + Effect.gen(function*() { + const graph = makeGraph() + graph.next.set(phoneNumberId + "/media", graphError(131053)) + yield* run( + graph, + ChatMarkdown.post(thread, "Report", { + files: [file("a.txt", "text/plain")], + unattachedNote: () => " (not attached)", + minWriteInterval: Duration.zero + }) + ) + assert.deepStrictEqual(graph.sent().map((body) => body.text.body), ["Report (not attached)"]) + })) + + it.effect("maps 131047 to Forbidden outside the window", () => + Effect.gen(function*() { + const graph = makeGraph() + graph.next.set(phoneNumberId + "/messages", graphError(131047)) + const reason = yield* failure(graph, post({ markdown: "x" })) + assert.deepStrictEqual([reason._tag, reason.detail], ["Forbidden", WhatsAppChat.outsideWindow]) + })) +}) + +const secret = "app-secret" + +const sign = async (body: string) => { + const key = await crypto.subtle.importKey( + "raw", + new TextEncoder().encode(secret), + { name: "HMAC", hash: "SHA-256" }, + false, + ["sign"] + ) + const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(body))) + return "sha256=" + Array.from(mac, (byte) => byte.toString(16).padStart(2, "0")).join("") +} + +const deliver = Effect.fnUntraced(function*( + envelope: unknown, + options?: { + readonly signature?: string + readonly refuse?: (event: ChatEvent.ChatEvent) => boolean + readonly handle?: (event: ChatEvent.ChatEvent) => Effect.Effect + readonly onClick?: ChatInteraction.ClickReply + readonly onRefused?: (error: ChatError) => Effect.Effect + readonly deadline?: Duration.Duration + } +) { + const body = JSON.stringify(envelope) + const signature = options?.signature ?? (yield* Effect.promise(() => sign(body))) + const verify = yield* makeVerifier(Redacted.make(secret)).pipe(Effect.provide(NodeCrypto.layerMac)) + const seen: Array = [] + const refused: Array = [] + const response = yield* handleRequest( + HttpServerRequest.fromWeb( + new Request("http://localhost/whatsapp", { method: "POST", body, headers: { "x-hub-signature-256": signature } }) + ), + { + verify, + normalize: (envelope) => WhatsAppChat.normalize(envelope, { phoneNumberId }), + handle: (event) => + options?.refuse?.(event) === true + ? Effect.fail(new NotAccepted({})) + : options?.handle !== undefined + ? options.handle(event) + : Effect.sync(() => void seen.push(event._tag)), + onUpdate: (update: WhatsAppUpdate) => Effect.sync(() => void seen.push(update._tag)), + deadline: options?.deadline ?? Duration.seconds(4), + maxBodyBytes: 1024 * 1024 + } + ).pipe(Effect.provideService( + ChatInteraction.Handler, + ChatInteraction.Handler.of({ + click: (event) => Effect.sync(() => (seen.push(event._tag), options?.onClick ?? ChatInteraction.ack)), + submit: () => Effect.succeed(ChatInteraction.close), + command: () => Effect.succeed(ChatInteraction.ack), + refused: (_, error) => + Effect.andThen(Effect.sync(() => void refused.push(error)), options?.onRefused?.(error) ?? Effect.void) + }) + )) + return { status: response.status, seen, refused } +}) + +describe("webhook", () => { + const text = inbound({ type: "text", text: { body: "hi" } }) + + it.effect("checks the signature and answers Meta's subscription check", () => + Effect.gen(function*() { + assert.strictEqual((yield* deliver(text, { signature: "sha256=00" })).status, 401) + assert.strictEqual((yield* deliver(text)).status, 200) + const check = (token: string) => + handleVerification( + HttpServerRequest.fromWeb( + new Request(`http://localhost/whatsapp?hub.mode=subscribe&hub.verify_token=${token}&hub.challenge=42`) + ), + Redacted.make("verify") + ).status + assert.deepStrictEqual([check("verify"), check("other")], [200, 403]) + })) + + it.effect("runs updates in order, drops a removed reaction and stops at NotAccepted with a 503", () => + Effect.gen(function*() { + const batch = inbound({ type: "text", text: { body: "hi" } }) + const value = batch.entry[0].changes[0].value as any + value.messages.push( + { ...value.messages[0], id: "wamid.r1", type: "reaction", reaction: { message_id: "wamid.out1", emoji: "" } }, + { ...value.messages[0], id: "wamid.r2", type: "reaction", reaction: { message_id: "wamid.out1", emoji: "👍" } } + ) + value.statuses = [{ + id: "wamid.out1", + status: "read", + timestamp: "1794600001", + recipient_id: phone, + recipient_user_id: bsuid + }] + const ok = yield* deliver(batch) + assert.deepStrictEqual([ok.status, ok.seen], [200, ["MessageCreated", "ReactionAdded", "Status", "Alias"]]) + const refused = yield* deliver(batch, { refuse: (event) => event._tag === "MessageCreated" }) + assert.deepStrictEqual([refused.status, refused.seen], [503, []]) + })) + + it.live("answers 503 at the deadline without waiting for a slow finalizer", () => + Effect.gen(function*() { + const start = Date.now() + const result = yield* deliver(text, { + handle: () => Effect.never.pipe(Effect.ensuring(Effect.sleep("5 seconds"))), + deadline: Duration.millis(50) + }) + assert.strictEqual(result.status, 503) + assert.isBelow(Date.now() - start, 2000) + })) + + it.live("interrupts the worker when the request is cancelled at any step", () => + Effect.gen(function*() { + // Each budget makes the request yield, and be cancelled, at a different step. + for (let budget = 1; budget <= 40; budget++) { + let started = false + let stopped = false + const request = handleRequest( + HttpServerRequest.fromWeb(new Request("http://localhost/whatsapp", { method: "POST", body: "{}" })), + { + verify: () => + Effect.andThen( + Effect.sync(() => (started = true)), + Effect.onInterrupt(Effect.never, () => Effect.sync(() => (stopped = true))) + ), + normalize: () => [], + handle: () => Effect.void, + deadline: Duration.seconds(4), + maxBodyBytes: 1024 + } + ).pipe(Effect.provideService(Scheduler.MaxOpsBeforeYield, budget)) + const fiber = yield* Effect.forkChild(request, { startImmediately: true }) + yield* Fiber.interrupt(fiber) + yield* Effect.sleep("20 millis") + assert.isFalse(started && !stopped, `MaxOpsBeforeYield ${budget}`) + } + })) + + it.live("hands a click to the handler and refuses a modal as Unsupported", () => + Effect.gen(function*() { + const click = inbound({ + type: "interactive", + context: { from: "15550783881", id: "wamid.out1" }, + interactive: { type: "list_reply", list_reply: { id: "senv\u241Fp\u241Fq", title: "Prod" } } + }) + const modal = ChatInteraction.modal("m", { title: "M", fields: {} }).open() + const refusal = yield* Deferred.make() + // The refusal callback never finishes, and the click is still answered 200. + const result = yield* deliver(click, { + onClick: modal, + onRefused: (error) => Effect.andThen(Deferred.succeed(refusal, error), Effect.never), + deadline: Duration.millis(500) + }) + assert.deepStrictEqual([result.status, result.seen], [200, ["SelectChanged"]]) + assert.strictEqual((yield* Deferred.await(refusal)).reason._tag, "Unsupported") + })) +}) + +describe("normalize", () => { + it("reports a changed BSUID that arrives with no sender or contact", () => { + const envelope = inbound({ + type: "system", + system: { type: "user_changed_user_id", user_id: "US.99", body: "changed" } + }) + const value = envelope.entry[0].changes[0].value as any + delete value.contacts + delete value.messages[0].from + delete value.messages[0].from_user_id + const [update] = WhatsAppChat.normalize(envelope, { phoneNumberId }) + assert.deepStrictEqual( + update._tag === "UserIdChanged" && [update.thread.id, update.userId, WhatsAppChat.updateKey(update)], + [ + `${phoneNumberId}/u/US.99`, + "US.99", + `${phoneNumberId}/user_changed/${value.messages[0].id}` + ] + ) + }) +}) + +describe("keys", () => { + it("keys an update by its parts, escaped", () => { + const updates = WhatsAppChat.normalize( + statuses({ id: "wamid.a/b", status: "delivered", recipient_id: phone, recipient_user_id: bsuid }), + { phoneNumberId } + ) + assert.deepStrictEqual( + updates.map((update) => + update._tag === "Status" || update._tag === "Alias" ? WhatsAppChat.updateKey(update) : "" + ), + [ + `${phoneNumberId}/status/wamid.a%2Fb/delivered`, + `${phoneNumberId}/alias/wamid.a%2Fb/delivered/${phone}/${bsuid}` + ] + ) + }) +}) diff --git a/packages/chat/whatsapp/test/conformance.test.ts b/packages/chat/whatsapp/test/conformance.test.ts new file mode 100644 index 00000000000..6976205ab61 --- /dev/null +++ b/packages/chat/whatsapp/test/conformance.test.ts @@ -0,0 +1,28 @@ +/** + * The conformance suite against the adapter over a fake Graph API that keeps + * what was sent. Clicks are built from the sent reply buttons and list rows + * and go through `WhatsAppChat.receive`. + */ +import * as WhatsAppChat from "@effect/chat-whatsapp/WhatsAppChat" +import { describe, it } from "@effect/vitest" +import { conformance } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import { makeGraph, makeInteractions, thread, userPosts, whatsappLayer } from "./utils.ts" + +const options = { platform: "whatsapp", thread, userPosts, parseMention: WhatsAppChat.parseMention } + +describe("conformance over a fake Graph API", () => { + const names = Object.keys(conformance({ + ...options, + interactions: { click: () => Effect.void, submit: () => Effect.void } + })) + + for (const name of names) { + it.effect(name, () => { + const graph = makeGraph() + const { bind, interactions } = makeInteractions(graph) + const rules = conformance({ ...options, interactions }) + return Effect.andThen(bind, rules[name]).pipe(Effect.provide(whatsappLayer(graph))) + }) + } +}) diff --git a/packages/chat/whatsapp/test/text.test.ts b/packages/chat/whatsapp/test/text.test.ts new file mode 100644 index 00000000000..f123941d4ed --- /dev/null +++ b/packages/chat/whatsapp/test/text.test.ts @@ -0,0 +1,104 @@ +import { fits, render } from "@effect/chat-whatsapp/internal/text" +import { assert, describe, it } from "@effect/vitest" +import * as fc from "fast-check" + +const cases: ReadonlyArray = [ + ["**b** and __b__", "*b* and *b*"], + ["*i* and _i_", "_i_ and _i_"], + ["~~s~~ and ~s~", "~s~ and ~s~"], + ["**a *b* c**", "*a _b_ c*"], + ["[label](https://e.com \"title\")", "label (https://e.com)"], + ["![alt](https://e.com/a.png)", "alt (https://e.com/a.png)"], + ["[](https://e.com)", "https://e.com"], + ["", "https://e.com"], + ["# Title #", "*Title*"], + ["Title\n===\n", "*Title*\n"], + ["#", "#"], + ["| a | b |\n|---|---|\n| 1 | 2 |", "| a | b |\n| 1 | 2 |"], + ["~~~ts\n**x**\n~~~\n", "```\n**x**\n```\n"], + ["& < é 😀", "& < \u00e9 \u{1F600}"], + ["≫⃒ ≪⃒ * *", "≫⃒ ≪⃒ * *"], + ["[a][r] and [r]\n\n[r]: https://e.com", "[a][r] and [r]\n\n[r]: https://e.com"], + ["`**x**` ``a`` \\*x\\* **x**", "`**x**` ``a`` \\*x\\* *x*"], + ["a_b_c and 2*3*4", "a_b_c and 2_3_4"], + [" **indented**", " **indented**"], + ["- **item**\n> **quote**", "- *item*\n> *quote*"], + ["[**x**] and ![**y**]\n\n[**x**]: https://e.com", "[**x**] and ![**y**]\n\n[**x**]: https://e.com"], + ["[r]:\n \n \"**t**\"\n\n**x**", "[r]:\n \n \"**t**\"\n\n*x*"], + ["> ~~~js\n> **x**\n> ~~~\n\n**y**", "> ~~~js\n> **x**\n> ~~~\n\n*y*"], + ["- ```\n **x**\n ```", "- ```\n **x**\n ```"], + ["> ~~~js\n> > ~~~\n> **x**\n> ~~~\n\n**y**", "> ~~~js\n> > ~~~\n> **x**\n> ~~~\n\n*y*"], + ["- > ~~~\n > ~~~\n > **x**\n > ~~~", "- > ~~~\n > ~~~\n > **x**\n > ~~~"], + ["> ~~~\n> **x**\n\n**y**", "> ~~~\n> **x**\n\n*y*"], + ["[r]: /url\n~~~js\n\n**x**\n~~~", "[r]: /url\n```\n\n**x**\n```"], + ["| a |\n|---|\n~~~\n\n**x**\n~~~", "| a |\n```\n\n**x**\n```"], + ["- ~~~js\n\t ~~~\n **x**\n ~~~", "- ~~~js\n\t ~~~\n **x**\n ~~~"], + [">\t ~~~\n>\t **x**\n>\t ~~~", ">\t ~~~\n>\t **x**\n>\t ~~~"] +] + +const longer = (input: string) => { + const output = render(input) + return fits(output, input) ? undefined : output +} + +describe("render", () => { + it("renders each construct", () => { + for (const [input, expected] of cases) assert.strictEqual(render(input), expected, input) + }) + + it("never grows Markdown, in code points, UTF-16 units or UTF-8 bytes", () => { + const token = fc.constantFrom( + "*", + "**", + "_", + "__", + "~", + "~~", + "`", + "```", + "#", + "# ", + "[", + "]", + "(", + ")", + "![", + "<", + ">", + "&", + ";", + "\\", + "|", + "|---|", + "-", + "=", + "\n", + " ", + "a", + "\u00e9", + "\u{1F600}", + "https://e.com", + "&", + "&#", + "≫⃒", + "~~~", + "x", + "> ", + "- ", + " ", + "[r]: u" + ) + fc.assert(fc.property(fc.array(token, { maxLength: 40 }), (tokens) => longer(tokens.join("")) === undefined)) + }) + + it("never grows a character reference", () => { + const numbers = [0, 1, 0x2a, 0x7f, 0x80, 0x7ff, 0x800, 0xd800, 0xffff, 0x10000, 0x10ffff, 0x110000] + const references = [ + ...["amp", "lt", "nbsp", "hellip", "mdash", "frac34", "iquest", "nGt", "ast"].map((name) => "&" + name), + ...numbers.flatMap((n) => ["&#" + n, "&#x" + n.toString(16)]) + ] + for (const reference of references) { + for (const input of [reference, reference + ";"]) assert.strictEqual(longer(input), undefined, input) + } + }) +}) diff --git a/packages/chat/whatsapp/test/utils.ts b/packages/chat/whatsapp/test/utils.ts new file mode 100644 index 00000000000..0da50cdb3de --- /dev/null +++ b/packages/chat/whatsapp/test/utils.ts @@ -0,0 +1,197 @@ +import * as WhatsAppChat from "@effect/chat-whatsapp/WhatsAppChat" +import * as Chat from "effect/chat/Chat" +import type { ChatEvent } from "effect/chat/ChatEvent" +import { ChatHandler } from "effect/chat/ChatHandler" +import { type Interactions, NoSuchElement } from "effect/chat/ChatTest" +import * as Effect from "effect/Effect" +import * as HttpClient from "effect/http/HttpClient" +import type * as HttpClientRequest from "effect/http/HttpClientRequest" +import * as HttpClientResponse from "effect/http/HttpClientResponse" +import * as Layer from "effect/Layer" +import * as Redacted from "effect/Redacted" + +export const phoneNumberId = "106540352242922" +export const business = "15550783881" +export const bsuid = "US.13491208655302741918" +export const phone = "16505551234" +export const thread = WhatsAppChat.thread({ phoneNumberId, user: bsuid }) + +export interface FakeCall { + readonly method: string + readonly path: string + readonly body: any +} + +/** An answer with a status, or a JSON body for a 200. */ +export type Answer = { readonly status: number; readonly body?: unknown } | Record + +/** A Graph error answer, as Meta sends it. */ +export const graphError = (code: number, status = 400, subcode?: number): Answer => ({ + status, + body: { error: { message: "refused", type: "OAuthException", code, error_subcode: subcode, fbtrace_id: "A" } } +}) + +/** + * A Graph API that keeps the messages the adapter sends by id, and the media + * it uploads. `next` holds one-shot answers for a path. + */ +export const makeGraph = () => { + const calls: Array = [] + const messages = new Map() + const media = new Map() + const next = new Map() + let count = 0 + + const answer = async (request: HttpClientRequest.HttpClientRequest, url: URL): Promise => { + const path = url.host === "lookaside.test" ? url.pathname : url.pathname.replace(/^\/v[\d.]+\//, "") + const raw = request.body + let body: any + if (raw._tag === "FormData") body = Object.fromEntries(raw.formData.entries()) + else if (raw._tag === "Uint8Array") body = JSON.parse(new TextDecoder().decode(raw.body)) + calls.push({ method: request.method, path, body }) + const scripted = next.get(path) + if (scripted !== undefined) { + next.delete(path) + return scripted + } + if (url.host === "lookaside.test") return media.get(path.slice(1))!.bytes + if (path === `${phoneNumberId}/media`) { + const id = "media-" + ++count + media.set(id, { type: body.type, bytes: new Uint8Array(await (body.file as Blob).arrayBuffer()) }) + return { id } + } + if (path === `${phoneNumberId}/messages`) { + if (body.status === "read") return { success: true } + const id = "wamid.out" + ++count + messages.set(id, body) + const to = body.to ?? body.recipient + return { + messaging_product: "whatsapp", + contacts: [{ input: to, ...(body.to === undefined ? { user_id: to } : { wa_id: to }) }], + messages: [{ id }] + } + } + const stored = media.get(path) + if (stored === undefined) return graphError(100, 400, 33) + return { url: `https://lookaside.test/${path}`, mime_type: stored.type, file_size: stored.bytes.length, id: path } + } + + const client = HttpClient.make((request, url) => + Effect.promise(async () => { + const result = await answer(request, url) + const web = result instanceof Uint8Array + ? new Response(result as Uint8Array, { status: 200 }) + : "status" in result && typeof result.status === "number" + ? new Response(JSON.stringify(result.body ?? {}), { status: result.status }) + : new Response(JSON.stringify(result), { status: 200, headers: { "content-type": "application/json" } }) + return HttpClientResponse.fromWeb(request, web) + }) + ) + + return { + calls, + messages, + next, + sent: () => calls.filter((call) => call.path === `${phoneNumberId}/messages`).map((call) => call.body), + layer: Layer.succeed(HttpClient.HttpClient, client) + } +} + +/** `Chat`, `Registry` and the WhatsApp adapter on a fake Graph API. */ +export const whatsappLayer = ( + graph: ReturnType, + options?: { readonly messagingAccountId?: string } +) => + WhatsAppChat.layer({ accessToken: Redacted.make("EAAG-test"), phoneNumberId, ...options }).pipe( + Layer.provideMerge(Chat.layer), + Layer.provide(graph.layer) + ) + +let inboundCount = 0 + +/** A webhook delivery with one message from the test user. */ +export const inbound = (message: Record, options?: { readonly phoneNumberId?: string }) => ({ + object: "whatsapp_business_account", + entry: [{ + id: "102290129340398", + changes: [{ + field: "messages", + value: { + messaging_product: "whatsapp", + metadata: { display_phone_number: business, phone_number_id: options?.phoneNumberId ?? phoneNumberId }, + contacts: [{ profile: { name: "Sheena Nelson" }, user_id: bsuid, wa_id: phone }], + messages: [{ + from: phone, + from_user_id: bsuid, + id: "wamid.in" + ++inboundCount, + timestamp: "1794600000", + ...message + }] + } + }] + }] +}) + +/** A webhook delivery with statuses. */ +export const statuses = (...items: ReadonlyArray>) => ({ + object: "whatsapp_business_account", + entry: [{ + id: "102290129340398", + changes: [{ + field: "messages", + value: { + messaging_product: "whatsapp", + metadata: { display_phone_number: business, phone_number_id: phoneNumberId }, + statuses: items + } + }] + }] +}) + +const join = "\u241F" + +/** The user's text, as `ChatTest.conformance` wants it. */ +export const userPosts = (_thread: unknown, text: string): Effect.Effect => + Effect.sync(() => + WhatsAppChat.normalize(inbound({ type: "text", text: { body: text } }), { phoneNumberId })[0] as ChatEvent + ) + +/** + * The conformance hook: builds the `button_reply` or `list_reply` of a + * reply button or list row the adapter sent, and delivers it through + * `WhatsAppChat.receive`. + */ +export const makeInteractions = (graph: ReturnType) => { + let service: WhatsAppChat.WhatsAppChat["Service"] | undefined + const bind = Effect.tap(WhatsAppChat.WhatsAppChat, (whatsapp) => + Effect.sync(() => { + service = whatsapp + })) + const interactions: Interactions = { + click: Effect.fnUntraced(function*(messageId, id, options) { + const interactive = graph.messages.get(messageId)?.interactive + const choices: Array<{ readonly id: string; readonly title: string }> = interactive?.type === "button" + ? interactive.action.buttons.map((button: any) => button.reply) + : interactive?.type === "list" + ? interactive.action.sections.flatMap((section: any) => section.rows) + : [] + const choice = choices.find((choice) => { + if (choice.id.startsWith("b")) return choice.id.slice(1).split(join)[0] === id + const [select, ...value] = choice.id.slice(1).split(join) + return select === id && (options?.option === undefined || value.join(join) === options.option) + }) + if (choice === undefined) return yield* Effect.fail(new NoSuchElement({ messageId, id })) + const kind = interactive.type === "button" ? "button_reply" : "list_reply" + yield* service!.receive(inbound({ + type: "interactive", + context: { from: business, id: messageId }, + interactive: { type: kind, [kind]: choice } + })).pipe( + Effect.provideService(ChatHandler, ChatHandler.of({ handle: () => Effect.void })), + Effect.orDie + ) + }), + submit: () => Effect.void + } + return { bind, interactions } +} diff --git a/packages/chat/whatsapp/tsconfig.json b/packages/chat/whatsapp/tsconfig.json new file mode 100644 index 00000000000..e2a8ca19a0d --- /dev/null +++ b/packages/chat/whatsapp/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig", + "extends": "../../../tsconfig.base.json", + "include": ["src"], + "references": [ + { "path": "../../effect" } + ] +} diff --git a/packages/effect/package.json b/packages/effect/package.json index 46e5ae979ac..7eba8cffa52 100644 --- a/packages/effect/package.json +++ b/packages/effect/package.json @@ -34,6 +34,7 @@ ".": "./src/index.ts", "./testing": "./src/testing/index.ts", "./ai": "./src/ai/index.ts", + "./chat": "./src/chat/index.ts", "./cli": "./src/cli/index.ts", "./cluster": "./src/cluster/index.ts", "./crypto": "./src/crypto/index.ts", @@ -58,6 +59,7 @@ "./workers": "./src/workers/index.ts", "./*": "./src/*.ts", "./ai/internal/*": null, + "./chat/internal/*": null, "./cli/internal/*": null, "./cluster/internal/*": null, "./crypto/internal/*": null, @@ -94,6 +96,7 @@ ".": "./dist/index.js", "./testing": "./dist/testing/index.js", "./ai": "./dist/ai/index.js", + "./chat": "./dist/chat/index.js", "./cli": "./dist/cli/index.js", "./cluster": "./dist/cluster/index.js", "./crypto": "./dist/crypto/index.js", diff --git a/packages/effect/src/chat/Chat.ts b/packages/effect/src/chat/Chat.ts new file mode 100644 index 00000000000..f5a7d3effe3 --- /dev/null +++ b/packages/effect/src/chat/Chat.ts @@ -0,0 +1,164 @@ +/** + * The routed outbound chat service. + * + * `Chat` has the ops of every registered adapter and routes each call by the + * address's platform. `Chat.layer` provides the router and an empty + * `ChatAdapter.Registry`; adapter layers stack on top with + * `Layer.provideMerge`, and a platform that is off is a layer left out. + * + * **Example** (Posting through the router) + * + * ```ts + * import { Effect } from "effect" + * import { Chat, ChatThread } from "effect/chat" + * + * const program = Effect.gen(function*() { + * const chat = yield* Chat.Chat + * const posted = yield* chat.post(ChatThread.make("slack", "T1/C1"), { markdown: "Hello" }) + * return posted.id + * }) + * ``` + * + * @stability unstable + * @since 4.1.5 + */ +import * as Context from "../Context.ts" +import * as Effect from "../Effect.ts" +import * as Layer from "../Layer.ts" +import * as Stream from "../Stream.ts" +import type { ChatAdapter, Descriptor, Ops, User } from "./ChatAdapter.ts" +import { Registry } from "./ChatAdapter.ts" +import { ChatError, NoAdapter } from "./ChatError.ts" +import type { ChatThread } from "./ChatThread.ts" +import { RetriesDisabled } from "./internal/retries.ts" + +/** + * Service that routes chat operations to the adapter registered for each + * address's platform. + * + * **Details** + * + * - Every op takes an address and goes to the adapter for + * `thread.platform`, failing with `NoAdapter` when there is none. + * - `getUser` takes the platform first, because a bare user id names none. + * - `followUp` is routed by the platform of the deferred command's thread. + * One call per deferral is the portable contract: the first is the + * answer, and a second is platform-specific, a new message on Slack (up to + * five) and an overwrite of the first on Discord. It is rendered as + * `respond` is, so it never mentions anyone: Slack shows the token as + * text and Discord renders it and notifies nobody. + * - `descriptor` returns a platform's capabilities and limits. + * - Each call runs in a `Chat.{op}` span whose attributes are the platform, the + * op and, on failure, the error reason and retry value. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class Chat extends Context.Service< + Chat, + Omit & { + readonly getUser: ( + platform: string, + id: string, + options?: { readonly in?: ChatThread | undefined } + ) => Effect.Effect + readonly descriptor: (platform: string) => Effect.Effect + } +>()("effect/chat/Chat") {} + +const spanAttributes = (platform: string, op: string) => ({ "chat.platform": platform, "chat.op": op }) + +const annotateFailure = (error: ChatError) => + Effect.annotateCurrentSpan({ "chat.error.reason": error.reason._tag, "chat.error.retry": error.retry }) + +const make = Effect.sync(() => { + const adapters = new Map() + + const lookup = (platform: string, op: string): Effect.Effect => + Effect.suspend(() => { + const adapter = adapters.get(platform) + return adapter === undefined + ? Effect.fail(new ChatError({ platform, op, reason: new NoAdapter({}) })) + : Effect.succeed(adapter) + }) + + const route = ( + platform: string, + op: string, + f: (adapter: ChatAdapter) => Effect.Effect + ): Effect.Effect => + lookup(platform, op).pipe( + Effect.flatMap(f), + Effect.tapError(annotateFailure), + Effect.withSpan(`Chat.${op}`, { attributes: spanAttributes(platform, op) }) + ) + + const routeStream = ( + platform: string, + op: string, + f: (adapter: ChatAdapter) => Stream.Stream + ): Stream.Stream => + Stream.unwrap(Effect.map(lookup(platform, op), f)).pipe( + Stream.tapError(annotateFailure), + Stream.withSpan(`Chat.${op}`, { attributes: spanAttributes(platform, op) }) + ) + + const registry = Registry.of({ + register: (adapter) => Effect.sync(() => void adapters.set(adapter.descriptor.platform, adapter)), + get: (platform) => lookup(platform, "get"), + descriptors: Effect.sync(() => Array.from(adapters.values(), (adapter) => adapter.descriptor)) + }) + + const chat = Chat.of({ + post: (thread, content) => route(thread.platform, "post", (adapter) => adapter.post(thread, content)), + edit: (thread, messageId, content) => + route(thread.platform, "edit", (adapter) => adapter.edit(thread, messageId, content)), + delete: (thread, messageId) => route(thread.platform, "delete", (adapter) => adapter.delete(thread, messageId)), + fetchMessage: (thread, messageId) => + route(thread.platform, "fetchMessage", (adapter) => adapter.fetchMessage(thread, messageId)), + history: (thread, options) => + routeStream(thread.platform, "history", (adapter) => adapter.history(thread, options)), + download: (thread, attachment, options) => + routeStream(thread.platform, "download", (adapter) => adapter.download(thread, attachment, options)), + startThread: (thread, messageId, options) => + route(thread.platform, "startThread", (adapter) => adapter.startThread(thread, messageId, options)), + getThread: (thread) => route(thread.platform, "getThread", (adapter) => adapter.getThread(thread)), + opener: (thread) => route(thread.platform, "opener", (adapter) => adapter.opener(thread)), + getUser: (platform, id, options) => route(platform, "getUser", (adapter) => adapter.getUser(id, options)), + followUp: (deferred, content) => + route(deferred.command.thread.platform, "followUp", (adapter) => adapter.followUp(deferred, content)), + descriptor: (platform) => route(platform, "descriptor", (adapter) => Effect.succeed(adapter.descriptor)) + }) + + return Context.make(Registry, registry).pipe(Context.add(Chat, chat)) +}) + +/** + * Layer that provides the `Chat` router and an empty `ChatAdapter.Registry`. + * + * **When to use** + * + * Use as the base of a chat setup. Adapter layers go on top with + * `Layer.provideMerge`. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer: Layer.Layer = Layer.effectContext(make) + +/** + * Runs an effect with adapter retries turned off, so every adapter call inside + * it makes one attempt. + * + * **Gotchas** + * + * The name is provisional. + * + * @stability unstable + * @category combinators + * @since 4.1.5 + */ +export const withoutRetries = (effect: Effect.Effect): Effect.Effect => + Effect.provideService(effect, RetriesDisabled, true) diff --git a/packages/effect/src/chat/ChatAdapter.ts b/packages/effect/src/chat/ChatAdapter.ts new file mode 100644 index 00000000000..fd76e737f1d --- /dev/null +++ b/packages/effect/src/chat/ChatAdapter.ts @@ -0,0 +1,444 @@ +/** + * What a chat platform adapter implements. + * + * An adapter is a set of `Ops` plus a static `Descriptor` of what the + * platform supports. Adapters register in the `Registry`, which `Chat` + * routes through by `thread.platform`. + * + * **Details** + * + * - Every op exists on every platform. Where the platform has no equivalent + * the op fails with `Unsupported`, naming the capability. + * - A post notifies nobody unless asked: mentions in Markdown do not notify by + * default. + * - Length is checked before the call, with `checkMessageLength`, in the + * unit the descriptor's `measure` counts. + * - Adapters retry `retry` failures with `retrySafe` and never retry + * `uncertain` ones. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Context from "../Context.ts" +import type * as DateTime from "../DateTime.ts" +import * as Duration from "../Duration.ts" +import * as Effect from "../Effect.ts" +import * as Layer from "../Layer.ts" +import type * as Option from "../Option.ts" +import type * as Schema from "../Schema.ts" +import type * as Scope from "../Scope.ts" +import type * as Stream from "../Stream.ts" +import type { Capability } from "./ChatError.ts" +import { ChatError, TooLarge } from "./ChatError.ts" +import type { ThreadKind } from "./ChatEvent.ts" +import type { Defer } from "./ChatInteraction.ts" +import type { Attachment, ChatMessage, ReplyTo } from "./ChatMessage.ts" +import type { ChatThread } from "./ChatThread.ts" +import type { Action } from "./ChatUi.ts" +import { RetriesDisabled } from "./internal/retries.ts" + +/** + * Static description of a platform: its capabilities and limits, readable + * without a call to the platform. + * + * **Details** + * + * - An op whose capability is missing from `capabilities` fails with + * `Unsupported`. + * - `limits.messageLength` is the platform's own limit for one message, in + * the platform's own unit. Where every message also carries a fixed + * overhead that the text does not, such as a card's structure, it is that + * limit less a fixed reservation for the overhead, and the adapter's docs + * give both numbers. + * - `measure` gives what a Markdown string costs against + * `limits.messageLength` and `limits.streamLength`. It is pure and makes + * no call to the platform. It is an upper bound, never less than what the + * adapter sends for the text, and it adds up: + * `measure(a + b) <= measure(a) + measure(b)`. It measures ASCII letters + * at one each. + * - `measure` takes the `actions` a message carries as an optional second + * argument. Where text and elements share one budget (Slack) they add to + * the cost, and elsewhere the argument is ignored. With it, + * `measure(md, actions) <= measure(md) + measure("", actions)`, so a caller + * can set room aside for the elements before it cuts the text, + * `measure(md, actions) >= measure(md)`, and `measure(md, [])` is + * `measure(md)`. A `measure` with one parameter fits the type. + * - `limits.minWriteInterval` is the default floor between two writes to one + * thread, for streamed and multi-part posts. + * - `limits.streamLength` and `limits.streamAge` apply to native streams + * only. `streamLength` is never more than `messageLength`, so a stream's + * text always fits an `edit`. + * - `limits.actionsPerMessage`, `limits.actionDataLength`, + * `limits.modalMetadataLength` and `limits.modalFields` are there exactly + * when the matching capability is (`buttons` for the first two, `modals` + * for the last two). Each is the platform's number in the platform's unit, + * as `messageLength` is. Where the platform publishes none, it is the + * adapter's own cap, documented as the adapter's, and set so that a + * message holding every element at its cap still fits `messageLength`. + * The adapter checks them exactly and fails + * `TooLarge`: before the call for a message's elements, and before it + * opens a modal. + * - `limits.followUpAge` is there exactly when `commands` is: how long after + * a command its `followUp` still works. + * + * **Gotchas** + * + * Title and label limits of a modal are not on the descriptor. The adapter + * checks them the same way, and its docs list them. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Descriptor { + readonly platform: string + readonly capabilities: ReadonlySet + readonly measure: (markdown: string, actions?: ReadonlyArray) => number + readonly limits: { + readonly messageLength: number + readonly filesPerMessage: number + readonly uploadBytes: number + readonly minWriteInterval: Duration.Duration + readonly streamLength?: number | undefined + readonly streamAge?: Duration.Duration | undefined + /** How many elements one message's `actions` may hold. */ + readonly actionsPerMessage?: number | undefined + /** The most an element's id and encoded value may take together. */ + readonly actionDataLength?: number | undefined + /** The most a modal's id and its encoded metadata may take together, after what the adapter adds. */ + readonly modalMetadataLength?: number | undefined + /** How many inputs and texts one modal may hold. */ + readonly modalFields?: number | undefined + /** How long after a command its `followUp` still works. */ + readonly followUpAge?: Duration.Duration | undefined + } +} + +/** + * A file to upload with a post. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface File { + readonly name: string + readonly contentType: string + readonly content: Uint8Array +} + +/** + * The message a successful post created. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Posted { + readonly id: string + readonly thread: ChatThread + readonly timestamp: DateTime.Utc +} + +/** + * What the platform says about an address. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface ThreadInfo { + readonly thread: ChatThread + readonly kind: ThreadKind + readonly name?: string | undefined + readonly topic?: string | undefined + readonly parent?: ChatThread | undefined + readonly createdAt?: DateTime.Utc | undefined + readonly raw: Schema.Json +} + +/** + * A user as the platform reports them. + * + * **Details** + * + * - `name` is the guild nick when `in` was given and the platform has one. + * - `raw` is the platform's own record of the user. What the model has no + * field for, such as a Slack user's team or a Discord member's roles, is + * read from there. Each adapter documents what its `raw` holds. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface User { + readonly id: string + readonly name: string + readonly isBot: boolean + readonly isSelf: boolean + readonly raw: Schema.Json +} + +/** + * The operations of one platform. `Chat` has the same ops, routed by + * `thread.platform`. + * + * **Details** + * + * - `post` takes standard Markdown that must fit `limits.messageLength`. It + * never splits or truncates: the adapter checks the length itself and fails + * with `TooLarge` without calling the platform. Mentions in it notify nobody. + * - A `post` shows no link preview unless requested through the adapter's + * scoped `withPostOptions`. This rule does not cover edits or native streams. + * - `fetchMessage` fails with `NotFound` for a message that is gone. + * - `history` pages lazily. `backward` runs newest to oldest, `forward` + * oldest to newest, and `from` is exclusive. A thread's oldest message is its + * opener, whose `thread` is the thread's parent: the one message in a + * thread's history that is not addressed to that thread. Without an opener, + * history starts at the first reply, whether the opener was deleted or the + * platform's thread root is not a message. A `backward` read that stops + * early has not reached the opener: `opener` reads it by itself. + * - `download` fails with `TooLarge` as soon as more than `maxBytes` arrive. + * It streams, so a file is never held whole unless the caller asks: + * `Stream.mkUint8Array` collects the stream into one array. + * - `startThread` returns the thread under `messageId`, created when there is + * none. It is get-or-create, and the only way from an opener to its thread. + * - `opener` is the message a thread hangs off: `None` for a channel or + * direct address, or when it was deleted. + * - `actions` is a flat list of elements beside the Markdown, and the + * adapter lays out the rows. A platform without an element fails + * `Unsupported` before the call, naming `buttons` or `selects`. An + * `edit` replaces the whole message, so an edit without `actions` + * removes them. + * - `followUp` sends the answer a command's `Defer` promised, to whoever + * its `visibility` names. It is routed by `deferred.command.thread` and + * never retried. It takes no files, and `withPostOptions` does not reach + * it. The contract is one call per deferral: a second posts another + * message on Slack and replaces the first on Discord. An adapter does not + * stop it, and `ChatTest`'s fake fails it. It fails + * `NotFound` with detail `interaction_expired` past `limits.followUpAge`, + * and `Unsupported` without `commands`. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Ops { + readonly post: (thread: ChatThread, content: { + readonly markdown: string + readonly replyTo?: ReplyTo | undefined + readonly files?: ReadonlyArray | undefined + readonly actions?: ReadonlyArray | undefined + }) => Effect.Effect + readonly edit: ( + thread: ChatThread, + messageId: string, + content: { + readonly markdown: string + readonly actions?: ReadonlyArray | undefined + } + ) => Effect.Effect + readonly delete: (thread: ChatThread, messageId: string) => Effect.Effect + readonly fetchMessage: (thread: ChatThread, messageId: string) => Effect.Effect + readonly history: (thread: ChatThread, options: { + readonly direction: "backward" | "forward" + readonly from?: string | undefined + }) => Stream.Stream + readonly download: ( + thread: ChatThread, + attachment: Attachment, + options: { readonly maxBytes: number } + ) => Stream.Stream + readonly startThread: ( + thread: ChatThread, + messageId: string, + options?: { readonly name?: string | undefined } + ) => Effect.Effect + readonly getThread: (thread: ChatThread) => Effect.Effect + readonly opener: (thread: ChatThread) => Effect.Effect, ChatError> + readonly getUser: ( + id: string, + options?: { readonly in?: ChatThread | undefined } + ) => Effect.Effect + readonly followUp: (deferred: Defer, content: { + readonly markdown: string + readonly actions?: ReadonlyArray | undefined + }) => Effect.Effect +} + +/** + * A platform's native streaming of one message, used by `ChatMarkdown.stream` + * when present. It is adapter-facing and not on `Chat`. + * + * **Details** + * + * - `start` fails with `Unsupported` when the platform refuses the feature + * for this thread or recipient, and the caller falls back to post-then-edit. + * It holds nothing back: all of `markdown` is sent. Do not end `markdown` + * inside a surrogate pair: a lone surrogate is sent as U+FFFD. + * - `append` is never retried by the adapter. `open` means the text was + * added apart from its last `held` UTF-16 code units, `stopped` means + * nothing was added because a user stopped the stream, and `closed` means + * nothing was added because the stream is closed or expired. + * - `held` counts the trailing code units the adapter could not send yet, + * such as an unclosed `<` or a high surrogate whose other half has not + * arrived. The caller keeps `markdown.slice(markdown.length - held)` and + * sends it again at the front of the next append. `flush` holds nothing + * back and sends a lone surrogate as U+FFFD. A `TooLarge` failure means + * nothing was added and the stream is still open. + * - `stop` closes the stream. `stopped` says whether a user had clicked stop + * on it. It succeeds with `stopped: false` on a stream that is already + * closed. + * - `continues` on `start` is the message id of the reply's previous part, + * absent for a reply's first message. It tells the adapter which reply the + * stream belongs to, so state kept per reply, such as a rate limit's + * backoff, carries over a rotation and never passes to another reply. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface NativeStream { + readonly start: (thread: ChatThread, options: { + readonly markdown: string + readonly replyTo?: ReplyTo | undefined + readonly recipient?: string | undefined + readonly continues?: string | undefined + }) => Effect.Effect + readonly append: ( + thread: ChatThread, + messageId: string, + markdown: string, + options?: { readonly flush?: boolean | undefined } + ) => Effect.Effect< + { readonly state: "open" | "stopped" | "closed"; readonly held: number }, + ChatError + > + readonly stop: ( + thread: ChatThread, + messageId: string, + options: { readonly end: "done" | "rotating" } + ) => Effect.Effect<{ readonly stopped: boolean }, ChatError> +} + +/** + * What an adapter package builds: the platform's ops, its descriptor and, + * when the platform has one, its native stream. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface ChatAdapter extends Ops { + readonly descriptor: Descriptor + readonly nativeStream?: NativeStream | undefined +} + +/** + * Service that holds the adapters by platform. `Chat.layer` provides it. + * + * **Details** + * + * `get` fails with `NoAdapter` for a platform with no adapter registered. + * Registering a second adapter for a platform replaces the first. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class Registry extends Context.Service Effect.Effect + readonly get: (platform: string) => Effect.Effect + readonly descriptors: Effect.Effect> +}>()("effect/chat/ChatAdapter/Registry") {} + +/** + * Creates the layer that registers an adapter in the `Registry`. + * + * **When to use** + * + * Use when building an adapter package's layer. A platform that is off is a + * layer left out. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = ( + adapter: Effect.Effect +): Layer.Layer | Registry> => + Layer.effectDiscard(Effect.gen(function*() { + const registry = yield* Registry + yield* registry.register(yield* adapter) + })) + +/** + * Fails with `TooLarge` when Markdown does not fit the descriptor's + * `limits.messageLength`, measured with the descriptor's `measure`. + * + * **When to use** + * + * Use when implementing `post`, `edit` and `NativeStream.start` in an adapter, + * so a message that is too long fails without a call to the platform. + * + * **Details** + * + * Pass the string the adapter is about to send, after escaping. For a + * `measure` that counts what escaping adds, measuring the escaped string + * gives the exact cost, so a post is neither refused when the platform would + * take it nor passed when the platform would refuse it on length. Pass the + * message's `actions` too, so they are measured with it. + * + * @stability unstable + * @category validation + * @since 4.1.5 + */ +export const checkMessageLength = ( + descriptor: Descriptor, + op: string, + markdown: string, + actions?: ReadonlyArray | undefined +): Effect.Effect => + descriptor.measure(markdown, actions ?? []) <= descriptor.limits.messageLength ? Effect.void : Effect.fail( + new ChatError({ + platform: descriptor.platform, + op, + reason: new TooLarge({ subject: "text", detail: "message_too_long" }) + }) + ) + +const maxRetries = 3 +const baseDelay = Duration.millis(500) + +/** + * Retries an adapter request with the policy adapters share. + * + * **Details** + * + * - Only failures whose retry value is `retry` are retried, up to three + * times. `uncertain` and `denied` failures are returned at once. + * - The wait is the error's `retryAfter` when it has one, otherwise an + * exponential backoff from 500 milliseconds. + * - Inside `Chat.withoutRetries` the request runs once. + * + * @stability unstable + * @category error handling + * @since 4.1.5 + */ +export const retrySafe = (effect: Effect.Effect): Effect.Effect => + Effect.flatMap(RetriesDisabled, (disabled) => { + if (disabled) { + return effect + } + const loop = (attempt: number): Effect.Effect => + Effect.catchIf( + effect, + (error) => error.retry === "retry" && attempt < maxRetries, + (error) => + Effect.flatMap( + Effect.sleep(error.retryAfter ?? Duration.times(baseDelay, 2 ** attempt)), + () => loop(attempt + 1) + ) + ) + return loop(0) + }) diff --git a/packages/effect/src/chat/ChatError.ts b/packages/effect/src/chat/ChatError.ts new file mode 100644 index 00000000000..f94eaf4b654 --- /dev/null +++ b/packages/effect/src/chat/ChatError.ts @@ -0,0 +1,684 @@ +/** + * The error every chat operation fails with. + * + * A `ChatError` records the platform and op, and holds one of twelve reasons. + * Each reason has a three-way retry value: `retry` means nothing happened and + * the request is safe to send again, `uncertain` means a write may or may not + * have landed, and `denied` means sending again gives the same answer. + * + * **Details** + * + * - `detail` is redacted: a platform code such as `channel_not_found`, never + * message text, file names or ids. `file_refused` is fixed by the core: on + * a `denied` reason it promises a file was refused and nothing is left + * posted. It overrides platform error names; the original code stays on `cause`. + * - `cause` is the error underneath (an HTTP error, a client library error, a + * `SchemaError`). It is unredacted and can hold URLs, ids and payload + * values. Schema encoding keeps it and `toJSON` leaves it out, so logging an + * error does not print it. + * + * @stability unstable + * @since 4.1.5 + */ +import type * as Duration from "../Duration.ts" +import * as Predicate from "../Predicate.ts" +import * as Schema from "../Schema.ts" + +/** + * Schema for the retry value of a chat error. + * + * **Details** + * + * - `retry`: nothing happened, safe to send again. + * - `uncertain`: the write may or may not have landed. Adapters never retry it. + * - `denied`: sending again gives the same answer. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Retry = Schema.Literals(["retry", "uncertain", "denied"]) + +/** + * The retry value of a chat error. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Retry = typeof Retry.Type + +/** + * Schema for a capability name from `ChatAdapter.Descriptor`. + * + * **Details** + * + * - `buttons` covers buttons and link buttons, `selects` selects on a + * message, and `modals` opening a modal from a click or a command. + * - `modalErrors` is field errors put back into an open modal. A platform + * without it answers `errors` some other way, so no op fails for it. + * - `commands` is slash commands arriving, and `followUp` working. + * - `ephemeral` is a command's answer that only the person who ran it sees. + * A platform with `commands` and without it refuses a private answer. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Capability = Schema.Literals([ + "edit", + "delete", + "fetchMessage", + "history", + "download", + "files", + "replyTo", + "startThread", + "getThread", + "opener", + "getUser", + "nativeStream", + "buttons", + "selects", + "modals", + "modalErrors", + "commands", + "ephemeral" +]) + +/** + * A capability name from `ChatAdapter.Descriptor`. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Capability = typeof Capability.Type + +/** + * Detail for a refused file on any `denied` reason: nothing is left posted. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const fileRefused = "file_refused" + +/** + * Detail for a modal returned after the deadline: the reason is `NotFound` + * and the op `openModal`. + * + * @stability unstable + * @category constants + * @since 4.1.5 + */ +export const interactionExpired = "interaction_expired" + +const detail = Schema.optionalKey(Schema.String) + +const cause = Schema.optional(Schema.Defect()) + +const withoutCause = (json: unknown): unknown => { + if (Predicate.isObject(json) && "cause" in json) { + const { cause: _, ...rest } = json as Record + return rest + } + return json +} + +const reasonMessage = (reason: { readonly _tag: string; readonly detail?: string | undefined }): string => + reason.detail === undefined ? reason._tag : `${reason._tag}: ${reason.detail}` + +/** + * Reason for a `ChatError`: the platform rate limited the request. + * + * **Details** + * + * Retry value `retry`: nothing happened. `retryAfter` is the wait the platform asked for, when it gave one. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class RateLimited extends Schema.TaggedError("effect/chat/ChatError/RateLimited")("RateLimited", { + retryAfter: Schema.optionalKey(Schema.Duration), + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "retry" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for an `ChatError`: the platform has no equivalent for the op. + * + * **Details** + * + * Retry value `denied`. Every op exists on every platform and fails with this where the platform has no equivalent; `capability` names what is missing. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class Unsupported extends Schema.TaggedError("effect/chat/ChatError/Unsupported")("Unsupported", { + capability: Capability, + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: the request failed in transport. + * + * **Details** + * + * Retry value `retry` when the request never left, `uncertain` when it did and no answer came. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class Transport extends Schema.TaggedError("effect/chat/ChatError/Transport")("Transport", { + retry: Retry, + detail, + cause +}) { + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: the platform answered with a server error. + * + * **Details** + * + * Retry value `retry` for reads, `uncertain` for writes. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class PlatformUnavailable + extends Schema.TaggedError("effect/chat/ChatError/PlatformUnavailable")("PlatformUnavailable", { + retry: Retry, + detail, + cause + }) +{ + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: no adapter is registered for the address's platform. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class NoAdapter extends Schema.TaggedError("effect/chat/ChatError/NoAdapter")("NoAdapter", { + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for an `ChatError`: the platform refused the credentials: a bad or revoked token. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class Unauthenticated + extends Schema.TaggedError("effect/chat/ChatError/Unauthenticated")("Unauthenticated", { + detail, + cause + }) +{ + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: the platform refused the request: a missing scope, not in the channel, an archived thread. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class Forbidden extends Schema.TaggedError("effect/chat/ChatError/Forbidden")("Forbidden", { + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: the thread, message, user or attachment is gone. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class NotFound extends Schema.TaggedError("effect/chat/ChatError/NotFound")("NotFound", { + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for a `ChatError`: the request is past a size limit: message length, file size or `download`'s `maxBytes`. + * + * **Details** + * + * - Retry value `denied`. + * - `subject` says what was too large, when the adapter can tell: `text` for + * the message text, `files` for the files sent with it. An adapter sets it + * on every refusal of `post`, `edit` and `NativeStream` it can attribute. + * - Usually raised before the call. A platform-side size refusal carries + * the platform's error name in `detail`, unless a file was refused: then + * it is `file_refused`. `ChatMarkdown` keys its fallback on that detail, + * not on `subject`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class TooLarge extends Schema.TaggedError("effect/chat/ChatError/TooLarge")("TooLarge", { + subject: Schema.optionalKey(Schema.Literals(["text", "files"])), + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for an `ChatError`: the platform refused the request as malformed. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class InvalidRequest + extends Schema.TaggedError("effect/chat/ChatError/InvalidRequest")("InvalidRequest", { + detail, + cause + }) +{ + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for an `ChatError`: the platform answered with something the adapter cannot decode. + * + * **Details** + * + * Retry value `denied`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class InvalidResponse + extends Schema.TaggedError("effect/chat/ChatError/InvalidResponse")("InvalidResponse", { + detail, + cause + }) +{ + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "denied" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * Reason for an `ChatError`: the request failed for a reason the adapter cannot classify. + * + * **Details** + * + * Retry value `uncertain`: a write may or may not have landed. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class Unknown extends Schema.TaggedError("effect/chat/ChatError/Unknown")("Unknown", { + detail, + cause +}) { + /** + * The retry value of this reason, fixed per reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return "uncertain" + } + + override get message(): string { + return reasonMessage(this) + } + + override toJSON(): unknown { + return withoutCause(super.toJSON()) + } +} + +/** + * One of the twelve reasons a `ChatError` holds. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export type ChatErrorReason = + | RateLimited + | Unsupported + | Transport + | PlatformUnavailable + | NoAdapter + | Unauthenticated + | Forbidden + | NotFound + | TooLarge + | InvalidRequest + | InvalidResponse + | Unknown + +/** + * Schema for the twelve reasons a `ChatError` holds. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ChatErrorReason: Schema.Union<[ + typeof RateLimited, + typeof Unsupported, + typeof Transport, + typeof PlatformUnavailable, + typeof NoAdapter, + typeof Unauthenticated, + typeof Forbidden, + typeof NotFound, + typeof TooLarge, + typeof InvalidRequest, + typeof InvalidResponse, + typeof Unknown +]> = Schema.Union([ + RateLimited, + Unsupported, + Transport, + PlatformUnavailable, + NoAdapter, + Unauthenticated, + Forbidden, + NotFound, + TooLarge, + InvalidRequest, + InvalidResponse, + Unknown +]) + +const TypeId = "~effect/chat/ChatError" as const + +/** + * The error every chat operation fails with. + * + * **Details** + * + * - `op` is the operation that failed: `post`, `edit`, `history` and so on. + * - `cause` is the reason, so a failed span's stack trace carries the whole + * chain: the `ChatError`, the reason and the error under it. + * - `retry` and `retryAfter` delegate to the reason. + * - `message` is built from the platform, op, reason and `detail`, never from + * the cause. + * + * **Example** (Matching on a reason) + * + * ```ts import.meta.vitest + * import { Effect } from "effect" + * import { ChatError } from "effect/chat" + * + * const failing = Effect.fail( + * new ChatError.ChatError({ + * platform: "slack", + * op: "post", + * reason: new ChatError.NotFound({ detail: "channel_not_found" }) + * }) + * ) + * + * const program = failing.pipe( + * Effect.catchReason("ChatError", "NotFound", (reason) => Effect.succeed(reason.detail)) + * ) + * + * await Effect.runPromise(program) // => "channel_not_found" + * ``` + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class ChatError extends Schema.TaggedError("effect/chat/ChatError")("ChatError", { + platform: Schema.String, + op: Schema.String, + reason: ChatErrorReason +}) { + /** + * Marks the value as a `ChatError` for `isChatError`. + * + * @since 4.1.5 + */ + readonly [TypeId] = TypeId + + /** + * The reason, so the chain under it ends up in a failed span's stack trace. + * + * @since 4.1.5 + */ + override readonly cause = this.reason + + /** + * The retry value of the reason. + * + * @since 4.1.5 + */ + get retry(): Retry { + return this.reason.retry + } + + /** + * The wait the platform asked for before a retry, when the reason is + * `RateLimited` and it gave one. + * + * @since 4.1.5 + */ + get retryAfter(): Duration.Duration | undefined { + return this.reason._tag === "RateLimited" ? this.reason.retryAfter : undefined + } + + override get message(): string { + return `${this.platform}.${this.op}: ${this.reason.message}` + } + + override toJSON(): unknown { + return { + _tag: this._tag, + platform: this.platform, + op: this.op, + reason: this.reason.toJSON() + } + } +} + +/** + * Returns whether a value is a `ChatError`. + * + * @stability unstable + * @category guards + * @since 4.1.5 + */ +export const isChatError = (u: unknown): u is ChatError => Predicate.hasProperty(u, TypeId) diff --git a/packages/effect/src/chat/ChatEvent.ts b/packages/effect/src/chat/ChatEvent.ts new file mode 100644 index 00000000000..44e40a7c658 --- /dev/null +++ b/packages/effect/src/chat/ChatEvent.ts @@ -0,0 +1,221 @@ +/** + * Inbound events from a chat platform. + * + * Adapters deliver a `ChatEvent` to the consumer's `ChatHandler`. Delivery is + * at-least-once: `dedupeKey` gives two deliveries of one event the same key, + * and the library keeps no seen-store. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Schema from "../Schema.ts" +import { ChatMessage } from "./ChatMessage.ts" +import * as ChatThread from "./ChatThread.ts" + +/** + * Schema for what an address turned out to be: a channel, a thread or a direct + * conversation. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ThreadKind = Schema.Literals(["channel", "thread", "direct"]) + +/** + * What an address turned out to be: a channel, a thread or a direct + * conversation. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ThreadKind = typeof ThreadKind.Type + +/** + * Schema for a message that arrived in a thread. + * + * **Details** + * + * `thread` equals `message.thread`. `raw` is the delivery envelope; the + * message payload is on `message.raw`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const MessageCreated = Schema.TaggedStruct("MessageCreated", { + thread: ChatThread.ChatThread, + kind: ThreadKind, + message: ChatMessage, + raw: Schema.Json +}).annotate({ identifier: "ChatEvent.MessageCreated" }) + +/** + * A message that arrived in a thread. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type MessageCreated = typeof MessageCreated.Type + +const reaction = { + thread: ChatThread.ChatThread, + messageId: Schema.String, + messageAuthorId: Schema.optionalKey(Schema.String), + userId: Schema.String, + emoji: Schema.String, + timestamp: Schema.optionalKey(Schema.DateTimeUtc), + raw: Schema.Json +} + +/** + * Schema for a reaction a user added to a message. + * + * **Details** + * + * - `thread` is where the platform says it happened. On Slack this is the + * channel, even for a message in a thread; `Chat.fetchMessage` returns the + * message with its real address. + * - `emoji` is a Unicode emoji, or `custom:{name}` for one the adapter's table + * does not know. + * - `messageAuthorId` and `timestamp` are missing when the platform does not + * send them. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ReactionAdded = Schema.TaggedStruct("ReactionAdded", reaction).annotate({ + identifier: "ChatEvent.ReactionAdded" +}) + +/** + * A reaction a user added to a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ReactionAdded = typeof ReactionAdded.Type + +/** + * Schema for a reaction a user removed from a message. + * + * **Details** + * + * The fields mean the same as on `ReactionAdded`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ReactionRemoved = Schema.TaggedStruct("ReactionRemoved", reaction).annotate({ + identifier: "ChatEvent.ReactionRemoved" +}) + +/** + * A reaction a user removed from a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ReactionRemoved = typeof ReactionRemoved.Type + +/** + * Schema for a user asking the bot to stop working in a thread, as Slack's + * stop button does. + * + * **Details** + * + * - Nothing has been stopped. The thread's streams stay open and keep taking + * text until the consumer ends them, and the platform shows a disabled + * button until then. + * - `messageIds` holds the streams the platform named. It is empty when no + * stream was open, but an empty list does not mean none is: Slack names + * the stream only on the first click in a thread. End the thread's reply + * whatever it holds. + * - `id` is the platform's id for the request, Slack's `event_ts`. Two + * deliveries of one request share it. + * - It has no `kind`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const StopRequested = Schema.TaggedStruct("StopRequested", { + thread: ChatThread.ChatThread, + id: Schema.String, + userId: Schema.String, + messageIds: Schema.Array(Schema.String), + raw: Schema.Json +}).annotate({ identifier: "ChatEvent.StopRequested" }) + +/** + * A user asking the bot to stop working in a thread. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type StopRequested = typeof StopRequested.Type + +/** + * Schema for an inbound chat event. + * + * **Gotchas** + * + * The union is open: later releases add members such as edits and deletes, so + * a `switch` over `_tag` needs a `default`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ChatEvent = Schema.Union([MessageCreated, ReactionAdded, ReactionRemoved, StopRequested]).annotate({ + identifier: "ChatEvent" +}) + +/** + * An inbound chat event. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ChatEvent = typeof ChatEvent.Type + +/** + * Returns the key two deliveries of one event share. + * + * **When to use** + * + * Use to drop redelivered events: delivery is at-least-once and the library + * keeps no seen-store. + * + * **Details** + * + * - A message is `message/{thread}/{id}`, with the thread in its + * `{platform}:{id}` form. + * - A reaction is `reaction/{messageId}/{userId}/{emoji}/{added|removed}`. + * - A stop request is `stop/{thread}/{id}`. + * - Slack's `message` and `app_mention` for one message share a key. + * + * @stability unstable + * @category getters + * @since 4.1.5 + */ +export const dedupeKey = (event: ChatEvent): string => { + switch (event._tag) { + case "MessageCreated": + return `message/${ChatThread.toString(event.message.thread)}/${event.message.id}` + case "ReactionAdded": + return `reaction/${event.messageId}/${event.userId}/${event.emoji}/added` + case "ReactionRemoved": + return `reaction/${event.messageId}/${event.userId}/${event.emoji}/removed` + case "StopRequested": + return `stop/${ChatThread.toString(event.thread)}/${event.id}` + } +} diff --git a/packages/effect/src/chat/ChatHandler.ts b/packages/effect/src/chat/ChatHandler.ts new file mode 100644 index 00000000000..e0b6260e2cf --- /dev/null +++ b/packages/effect/src/chat/ChatHandler.ts @@ -0,0 +1,102 @@ +/** + * The service a consumer provides to receive chat events. + * + * Adapters take the `ChatHandler` service from context and call `handle` for + * every inbound event. The contract is "accept, fast": success means the event + * is the consumer's. The platform can still deliver it again, so drop + * repeats with `ChatEvent.dedupeKey`. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Context from "../Context.ts" +import * as Effect from "../Effect.ts" +import * as Layer from "../Layer.ts" +import * as Schema from "../Schema.ts" +import type * as Scope from "../Scope.ts" +import type { ChatEvent } from "./ChatEvent.ts" + +/** + * Error for an event that was not accepted. + * + * **Details** + * + * A handler fails with it to refuse an event, and an adapter's entry point + * fails with it when it could not finish normalising one. Slack answers 503 so + * the event is redelivered; the Discord gateway layer drops it. + * + * **Gotchas** + * + * It has no fields, so whoever raises it logs the reason first. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class NotAccepted extends Schema.TaggedError("effect/chat/ChatHandler/NotAccepted")( + "NotAccepted", + {} +) {} + +/** + * Service that receives inbound chat events, provided by the consumer. + * + * **Details** + * + * Adapters take the service and call `handle`. Success hands the event to the + * consumer, and `NotAccepted` refuses it. Success does not stop the platform + * from delivering the event again: delivery is at-least-once. + * + * **Gotchas** + * + * Forking the work and returning loses events on a crash. How durable an + * accepted event is belongs to the consumer. + * + * @see {@link layer} for the only constructor + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class ChatHandler extends Context.Service Effect.Effect +}>()("effect/chat/ChatHandler") {} + +/** + * Creates the `ChatHandler` layer from a function that handles one event. + * + * **Details** + * + * - The bot's own messages are dropped before `handle` runs, unless + * `deliverSelf` is `true`. Other bots' messages are delivered, flagged with + * `author.isBot`. + * - Every event runs in a `ChatHandler.handle` span and its own `Scope`. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = ( + handle: (event: ChatEvent) => Effect.Effect, + options?: { readonly deliverSelf?: boolean | undefined } +): Layer.Layer> => + Layer.effect( + ChatHandler, + Effect.contextWith((context: Context.Context>) => { + const deliverSelf = options?.deliverSelf === true + return Effect.succeed(ChatHandler.of({ + handle: (event) => { + if (!deliverSelf && event._tag === "MessageCreated" && event.message.author.isSelf) { + return Effect.void + } + return handle(event).pipe( + Effect.scoped, + Effect.provideContext(context), + Effect.withSpan("ChatHandler.handle", { + attributes: { "chat.platform": event.thread.platform, "chat.op": "handle" } + }) + ) + } + })) + }) + ) diff --git a/packages/effect/src/chat/ChatInteraction.ts b/packages/effect/src/chat/ChatInteraction.ts new file mode 100644 index 00000000000..68560824a4a --- /dev/null +++ b/packages/effect/src/chat/ChatInteraction.ts @@ -0,0 +1,1850 @@ +/** + * Typed buttons, selects, modals and slash commands, and the service that + * receives their clicks, submits and commands. + * + * A definition carries an element's id and the schemas of its values. It + * builds the element to post (`of`, `open`) and gives the typed slot of its + * handler, the way `Rpc` and `RpcGroup` do. A `group` of definitions becomes a + * layer of handlers, and `layer` provides the `Handler` service that adapters + * call. + * + * **Example** (A button that opens a modal) + * + * ```ts + * import { Effect, Layer, Schema } from "effect" + * import { ChatInteraction } from "effect/chat" + * + * const Deploy = Schema.Struct({ version: Schema.String }) + * + * const Reject = ChatInteraction.button("reject", { label: "Reject", style: "danger", value: Deploy }) + * const RejectReason = ChatInteraction.modal("reject-reason", { + * title: "Reject deploy", + * metadata: Deploy, + * fields: { + * reason: ChatInteraction.textInput({ label: "Reason", multiline: true }) + * } + * }) + * + * const Interactions = ChatInteraction.group(Reject, RejectReason).toLayer({ + * reject: (click) => Effect.succeed(RejectReason.open({ metadata: click.value })), + * "reject-reason": (submit) => + * submit.values.reason.length < 10 + * ? Effect.succeed(ChatInteraction.errors({ reason: "Say a bit more." })) + * : Effect.succeed(ChatInteraction.close) + * }).pipe(Layer.provideMerge(ChatInteraction.layer())) + * ``` + * + * **Details** + * + * - Clicks, submits and commands are not `ChatEvent`s. They need an answer + * inside the platform's 3 seconds, so they go to `Handler`, which answers + * by return value and has no error channel. + * - Slack and Discord don't redeliver a click, a submit or a command, so + * interactions carry no dedupe key. An adapter whose platform does + * redeliver them documents a delivery key of its own. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Context from "../Context.ts" +import type * as Duration from "../Duration.ts" +import * as Effect from "../Effect.ts" +import * as Exit from "../Exit.ts" +import * as Fiber from "../Fiber.ts" +import * as Layer from "../Layer.ts" +import * as Option from "../Option.ts" +import * as Schema from "../Schema.ts" +import * as SchemaAST from "../SchemaAST.ts" +import type { Scope } from "../Scope.ts" +import { ChatError, interactionExpired, NotFound } from "./ChatError.ts" +import { ChatThread } from "./ChatThread.ts" +import * as ChatUi from "./ChatUi.ts" + +// ----------------------------------------------------------------------------- +// events +// ----------------------------------------------------------------------------- + +const origin = { + thread: ChatThread, + messageId: Schema.String, + userId: Schema.String, + raw: Schema.Json +} + +/** + * Schema for a click on a button. + * + * **Details** + * + * - `value` is still encoded, and absent for a button with no value. + * - `thread` and `messageId` are where the message carrying the button + * lives. `raw` is the platform's interaction payload. + * + * **Gotchas** + * + * On a command's private answer, `messageId` still tells which answer was + * clicked, but no message op works with it: `edit`, `delete` and + * `fetchMessage` can't reach a private answer. On Slack `chat.update` on it + * answered `message_not_found` live, and `edit` fails `NotFound`. On + * Discord only the interaction's token reaches it, which is from Discord's + * docs and was not run. Only `raw` tells the two apart: Slack's + * `container.is_ephemeral`, Discord's `message.flags` with 64. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export const ButtonClicked = Schema.TaggedStruct("ButtonClicked", { + id: Schema.String, + value: Schema.optionalKey(Schema.String), + ...origin +}) + +/** + * A click on a button. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export type ButtonClicked = typeof ButtonClicked.Type + +/** + * Schema for a choice in a select on a message. `value` is the chosen + * option's value, still encoded. `messageId` is as on `ButtonClicked`: on a + * command's private answer it tells which answer it was, and no message op + * works with it. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export const SelectChanged = Schema.TaggedStruct("SelectChanged", { + id: Schema.String, + value: Schema.String, + ...origin +}) + +/** + * A choice in a select on a message. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export type SelectChanged = typeof SelectChanged.Type + +/** + * Schema for a modal submit. + * + * **Details** + * + * - `thread` and `messageId` are those of the click that opened the modal. + * A modal a command opened carries the command's `thread` and no + * `messageId`. A modal opened from a command's private answer carries + * that answer's id, which no message op works with, as on + * `ButtonClicked`. + * - `metadata` is still encoded. + * - `values` are by field id, still encoded. An optional field left empty + * has no key. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export const ModalSubmitted = Schema.TaggedStruct("ModalSubmitted", { + id: Schema.String, + metadata: Schema.optionalKey(Schema.String), + values: Schema.Record(Schema.String, Schema.String), + thread: ChatThread, + messageId: Schema.optionalKey(Schema.String), + userId: Schema.String, + raw: Schema.Json +}) + +/** + * A modal submit. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export type ModalSubmitted = typeof ModalSubmitted.Type + +/** + * Schema for a slash command. + * + * **Details** + * + * - `id` is the command's name, without the slash. + * - `text` is what was typed after the name, as the platform sent it, and + * empty when nothing was. It is not normalised. + * - `thread` is where the command was typed. On Slack that is always a + * channel or a direct: Slack does not deliver an app's command typed in a + * thread. There is no `messageId`, since a command comes from no message. + * - `raw` is the platform's payload. It holds what `Chat.followUp` needs, + * so it is a credential for as long as `limits.followUpAge`. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export const CommandInvoked = Schema.TaggedStruct("CommandInvoked", { + id: Schema.String, + text: Schema.String, + thread: ChatThread, + userId: Schema.String, + raw: Schema.Json +}) + +/** + * A slash command. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export type CommandInvoked = typeof CommandInvoked.Type + +/** + * Schema for what an adapter hands to `Handler`. + * + * **Details** + * + * Not part of `ChatEvent`: these need an answer inside the platform's 3 + * seconds. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export const Interaction = Schema.Union([ButtonClicked, SelectChanged, ModalSubmitted, CommandInvoked]) + +/** + * A click, a select choice, a modal submit or a slash command. + * + * @stability unstable + * @category events + * @since 4.1.5 + */ +export type Interaction = typeof Interaction.Type + +// ----------------------------------------------------------------------------- +// replies +// ----------------------------------------------------------------------------- + +/** + * Schema for the reply that acknowledges a click. It is also what the handler + * service answers a command with no definition, which the adapter sends + * nothing for. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Ack = Schema.TaggedStruct("Ack", {}) + +/** + * The reply that acknowledges a click. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Ack = typeof Ack.Type + +/** + * Schema for the reply that opens a modal. A click or a command can return + * it. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const OpenModal = Schema.TaggedStruct("OpenModal", { modal: ChatUi.Modal }) + +/** + * The reply that opens a modal. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type OpenModal = typeof OpenModal.Type + +/** + * Schema for the reply that closes a submitted modal. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Close = Schema.TaggedStruct("Close", {}) + +/** + * The reply that closes a submitted modal. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Close = typeof Close.Type + +/** + * Schema for the reply that puts a message under each named field of a + * submitted modal. `errors` is by field id. + * + * **Details** + * + * With `modalErrors` the modal stays open with the messages under its + * fields. Without it the adapter answers some other way: on Discord a private + * reply lists them and the modal closes. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Errors = Schema.TaggedStruct("Errors", { + errors: Schema.Record(Schema.String, Schema.String) +}) + +/** + * The reply that puts a message under each named field. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Errors = typeof Errors.Type + +/** + * What a click's handler may answer. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type ClickReply = Ack | OpenModal + +/** + * What a submit's handler may answer. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type SubmitReply = Close | Errors + +/** + * Schema for who sees the answer to a command: `"ephemeral"` for the person + * who ran it, `"public"` for the channel. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Visibility = Schema.Literals(["ephemeral", "public"]) + +/** + * Who sees the answer to a command. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Visibility = typeof Visibility.Type + +/** + * Schema for the reply that answers a command with a message, at once. + * + * **Details** + * + * `markdown` and `actions` are rendered and checked the way `post` renders + * and checks them, with the default post options. `withPostOptions` does + * not reach it, so it never mentions anyone: on Slack a mention token is + * escaped and shows as text, and on Discord it renders and notifies nobody. + * The message gets no id: it cannot be edited, deleted or fetched + * afterwards. A click on its elements carries a `messageId`, which for a + * private answer tells which answer it was and works with no message op. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Respond = Schema.TaggedStruct("Respond", { + markdown: Schema.String, + actions: Schema.optionalKey(Schema.Array(ChatUi.Action)), + visibility: Visibility +}) + +/** + * The reply that answers a command with a message. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Respond = typeof Respond.Type + +/** + * Schema for the reply that says a command's answer comes later, and the + * value `Chat.followUp` takes to send it. + * + * **Details** + * + * - It is plain data, so it can go on a queue to another process. + * - `visibility` is fixed here. `followUp` has none of its own. + * - The first `followUp` for it is the answer. A second one is + * platform-specific: Slack posts another message, up to five, and Discord + * overwrites the first. Neither adapter stops it. + * + * **Gotchas** + * + * It holds the command's `raw`, which is a credential for as long as + * `limits.followUpAge`. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Defer = Schema.TaggedStruct("Defer", { + command: CommandInvoked, + visibility: Visibility +}) + +/** + * The reply that says a command's answer comes later. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Defer = typeof Defer.Type + +/** + * What the handler service may answer a command with. + * + * **Details** + * + * `Ack` is what a command with no definition gets, and the adapter sends + * nothing for it. A typed command handler cannot return it. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type CommandReply = Ack | Respond | OpenModal | Defer + +/** + * Schema for what `handle` gives when the handler ran past the deadline. + * + * **Gotchas** + * + * `Late` is never sent to a platform. It is a tagged struct, so it is itself + * a JSON value: test for it with `Schema.is(ChatInteraction.Late)`. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const Late = Schema.TaggedStruct("Late", {}) + +/** + * What `handle` gives when the handler ran past the deadline. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export type Late = typeof Late.Type + +/** + * `Errors`, with the field ids it names kept in the type, so a typed submit + * handler can refuse a field the modal does not have. The extra property + * exists only in the type. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export interface FieldErrors extends Errors { + readonly "~fields"?: Field | undefined +} + +/** + * Acknowledges a click. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const ack: Ack = Ack.make({}) + +/** + * Closes a submitted modal. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const close: Close = Close.make({}) + +/** + * Puts a message under each named field of a submitted modal. + * + * **Details** + * + * In a typed submit handler, only the modal's input ids are accepted. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const errors = ( + errors: E +): FieldErrors => Errors.make({ errors }) + +/** + * Answers a command with a message, at once. + * + * **Details** + * + * `visibility` defaults to `"ephemeral"`: only the person who ran the + * command sees it. A platform without `ephemeral` refuses a private answer + * and sends nothing. It never sends it publicly instead. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const respond = (options: { + readonly markdown: string + readonly actions?: ReadonlyArray | undefined + readonly visibility?: Visibility | undefined +}): Respond => + Respond.make({ + markdown: options.markdown, + ...optional("actions", options.actions), + visibility: options.visibility ?? "ephemeral" + }) + +/** + * Answers a command with a promise of a later answer, sent with + * `Chat.followUp`. + * + * **When to use** + * + * Use when the work does not fit in the deadline. The handler returns the + * deferral and hands it to whatever does the work. + * + * **Details** + * + * - `visibility` defaults to `"ephemeral"`, and holds for the follow-up. + * - One `Chat.followUp` per deferral is the portable contract. The first + * call is the answer. A second is platform-specific: a new message on + * Slack, up to five, and an overwrite of the first on Discord. + * + * @stability unstable + * @category replies + * @since 4.1.5 + */ +export const defer = (command: CommandInvoked, options?: { + readonly visibility?: Visibility | undefined +}): Defer => Defer.make({ command, visibility: options?.visibility ?? "ephemeral" }) + +// ----------------------------------------------------------------------------- +// typed events +// ----------------------------------------------------------------------------- + +/** + * A click as a button's handler receives it. + * + * **Details** + * + * `value` is decoded with the button's value schema, and `undefined` when it + * has none. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Click { + readonly id: string + readonly value: Value + readonly thread: ChatThread + readonly messageId: string + readonly userId: string + readonly raw: Schema.Json +} + +/** + * A choice as a select's handler receives it. `value` is the chosen option, + * decoded. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Selected { + readonly id: string + readonly value: Value + readonly thread: ChatThread + readonly messageId: string + readonly userId: string + readonly raw: Schema.Json +} + +/** + * A submit as a modal's handler receives it, with the metadata and values + * decoded. + * + * **Details** + * + * - `values` has one key per input and none for `text`. An optional input + * left empty is `undefined`. + * - `messageId` is the message whose element opened the modal, and + * `undefined` when a command opened it. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Submit { + readonly id: string + readonly metadata: Metadata + readonly values: Values + readonly thread: ChatThread + readonly messageId: string | undefined + readonly userId: string + readonly raw: Schema.Json +} + +// ----------------------------------------------------------------------------- +// values +// ----------------------------------------------------------------------------- + +/** + * The type id of definitions and groups. + * + * @stability unstable + * @category type ids + * @since 4.1.5 + */ +export const TypeId = "~effect/chat/ChatInteraction" + +/** + * A value's schema. + * + * **Details** + * + * It may not need services, because it is encoded inside `of` and `open`, + * which are plain functions. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ValueSchema = Schema.Codec + +type TypeOf = S extends ValueSchema ? S["Type"] : Fallback + +interface Wire { + readonly raw: boolean + readonly encode: (value: unknown) => unknown + readonly decode: (encoded: unknown) => Option.Option +} + +const wires = new WeakMap() + +const stringOnly = (ast: SchemaAST.AST): boolean => { + switch (ast._tag) { + case "String": + case "TemplateLiteral": + return true + case "Literal": + return typeof ast.literal === "string" + case "Union": + return ast.types.length > 0 && ast.types.every(stringOnly) + default: + return false + } +} + +const wireOf = (schema: ValueSchema): Wire => { + let wire = wires.get(schema) + if (wire === undefined) { + const codec = Schema.toCodecJson(schema) + const decode = Schema.decodeUnknownOption(codec) + wire = { + raw: stringOnly(SchemaAST.toEncoded(codec.ast)), + encode: Schema.encodeUnknownSync(codec), + decode: (encoded) => { + try { + return decode(encoded) + } catch { + return Option.none() + } + } + } + wires.set(schema, wire) + } + return wire +} + +/** + * Encodes a value into the string an element carries. + * + * **When to use** + * + * Use when routing clicks yourself. `of` and `open` already run it. + * + * **Details** + * + * The value goes through `Schema.toCodecJson`. The form on the wire is decided + * once per schema, from the codec's encoded side, never per value: + * + * - when the encoded side can only be a string (`String`, a brand of it, + * string literals, a template literal, or a union of those), the string is + * sent as it is + * - otherwise the encoded value goes through `JSON.stringify`, and back + * through `JSON.parse` + * + * **Gotchas** + * + * Throws when the value has no JSON form, as `Schema.encodeSync` does. + * + * @stability unstable + * @category values + * @since 4.1.5 + */ +export const encodeValue = (schema: S, value: S["Type"]): string => { + const wire = wireOf(schema) + const encoded = wire.encode(value) + if (wire.raw) return encoded as string + const json = JSON.stringify(encoded) + if (json === undefined) throw new Error("ChatInteraction.encodeValue: the value has no JSON form") + return json +} + +/** + * Decodes the string an element carried, as `encodeValue` wrote it. + * + * **Details** + * + * Gives none for a string that no longer decodes, which is what a value + * posted under an older schema can do. + * + * @stability unstable + * @category values + * @since 4.1.5 + */ +export const decodeValue = (schema: S, encoded: string): Option.Option => { + const wire = wireOf(schema) + if (wire.raw) return wire.decode(encoded) + let parsed: unknown + try { + parsed = JSON.parse(encoded) + } catch { + return Option.none() + } + return wire.decode(parsed) +} + +const optional = (key: K, value: V | undefined): { readonly [P in K]?: V } => + (value === undefined ? {} : { [key]: value }) as { readonly [P in K]?: V } + +// ----------------------------------------------------------------------------- +// definitions +// ----------------------------------------------------------------------------- + +/** + * A button definition. + * + * **Details** + * + * `of` gives the element to post. It takes the value when the definition has + * a schema for one, and `overrides` change the label or style of this one + * element. + * + * **Gotchas** + * + * `of` throws when the value cannot be encoded (a schema with no JSON form), + * as `Schema.encodeSync` does. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export interface Button { + readonly [TypeId]: typeof TypeId + readonly kind: "button" + readonly id: Id + readonly label: string + readonly style: ChatUi.ButtonStyle | undefined + readonly value: Value + readonly of: ( + ...args: Value extends ValueSchema ? [value: Value["Type"], overrides?: Button.Overrides] + : [overrides?: Button.Overrides] + ) => ChatUi.Button +} + +/** + * Types that go with a button definition. + * + * @stability unstable + * @since 4.1.5 + */ +export declare namespace Button { + /** + * What one element may change from its definition. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ + export interface Overrides { + readonly label?: string | undefined + readonly style?: ChatUi.ButtonStyle | undefined + } +} + +/** + * Defines a button. + * + * **Details** + * + * - The id names the handler in a `group`. It is sent to the platform as is, + * and counts toward `limits.actionDataLength` with the encoded value. + * - With no `value` schema the button carries no value, and its click's + * `value` is `undefined`. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export const button = ( + id: Id, + options: { + readonly label: string + readonly style?: ChatUi.ButtonStyle | undefined + readonly value?: Value + } +): Button => { + const value = options.value as Value + return { + [TypeId]: TypeId, + kind: "button", + id, + label: options.label, + style: options.style, + value, + of: ((...args: ReadonlyArray): ChatUi.Button => { + const overrides: Button.Overrides | undefined = value === undefined ? args[0] : args[1] + return { + _tag: "Button", + id, + label: overrides?.label ?? options.label, + ...optional("style", overrides?.style ?? options.style), + ...optional("value", value === undefined ? undefined : encodeValue(value, args[0])) + } + }) as Button["of"] + } +} + +/** + * A select definition, for a message. Its options are given at the post, to + * `of`. + * + * **Gotchas** + * + * `of` throws when an option's value cannot be encoded. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export interface Select { + readonly [TypeId]: typeof TypeId + readonly kind: "select" + readonly id: Id + readonly placeholder: string | undefined + readonly value: Value + readonly of: (options: { + readonly options: ReadonlyArray<{ readonly label: string; readonly value: TypeOf }> + readonly selected?: TypeOf | undefined + }) => ChatUi.Select +} + +/** + * Defines a single-choice select for a message. + * + * **Details** + * + * With no `value` schema the options' values are plain strings. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export const select = ( + id: Id, + options: { + readonly placeholder?: string | undefined + readonly value?: Value + } +): Select => { + const value = options.value as Value + const encode = (option: unknown): string => value === undefined ? option as string : encodeValue(value, option) + return { + [TypeId]: TypeId, + kind: "select", + id, + placeholder: options.placeholder, + value, + of: (args) => ({ + _tag: "Select", + id, + ...optional("placeholder", options.placeholder), + options: args.options.map((option) => ({ label: option.label, value: encode(option.value) })), + ...optional("selected", args.selected === undefined ? undefined : encode(args.selected)) + }) + } +} + +/** + * A text input in a modal, one line or several. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export interface TextInput { + readonly [TypeId]: typeof TypeId + readonly kind: "textInput" + readonly label: string + readonly multiline: boolean + readonly placeholder: string | undefined + readonly optional: Optional + readonly minLength: number | undefined + readonly maxLength: number | undefined +} + +/** + * Defines a text input for a modal. Its id is its key in `fields`. + * + * **Details** + * + * Its value is a `string`, or `string | undefined` when `optional` is true. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export const textInput = (options: { + readonly label: string + readonly multiline?: boolean | undefined + readonly placeholder?: string | undefined + readonly optional?: Optional | undefined + readonly minLength?: number | undefined + readonly maxLength?: number | undefined +}): TextInput> => ({ + [TypeId]: TypeId, + kind: "textInput", + label: options.label, + multiline: options.multiline ?? false, + placeholder: options.placeholder, + optional: (options.optional ?? false) as Optional, + minLength: options.minLength, + maxLength: options.maxLength +}) + +/** + * A select in a modal whose options are fixed in the definition. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export interface Choice { + readonly [TypeId]: typeof TypeId + readonly kind: "choice" + readonly label: string + readonly placeholder: string | undefined + readonly options: ReadonlyArray<{ readonly label: string; readonly value: Value }> + readonly optional: Optional +} + +/** + * Defines a select for a modal whose options are fixed here. Its value is the + * union of the option values. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export const choice = (options: { + readonly label: string + readonly placeholder?: string | undefined + readonly options: ReadonlyArray<{ readonly label: string; readonly value: Value }> + readonly optional?: Optional | undefined +}): Choice> => ({ + [TypeId]: TypeId, + kind: "choice", + label: options.label, + placeholder: options.placeholder, + options: options.options, + optional: (options.optional ?? false) as Optional +}) + +/** + * A select in a modal whose options are given when the modal opens. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export interface SelectInput< + out Value extends ValueSchema | undefined = undefined, + out Optional extends boolean = false +> { + readonly [TypeId]: typeof TypeId + readonly kind: "selectInput" + readonly label: string + readonly placeholder: string | undefined + readonly value: Value + readonly optional: Optional +} + +/** + * Defines a select for a modal. Its id is its key in `fields`, and its + * options are given to the modal's `open`. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export const selectInput = < + Value extends ValueSchema | undefined = undefined, + const Optional extends boolean = false +>(options: { + readonly label: string + readonly placeholder?: string | undefined + readonly value?: Value + readonly optional?: Optional | undefined +}): SelectInput> => ({ + [TypeId]: TypeId, + kind: "selectInput", + label: options.label, + placeholder: options.placeholder, + value: options.value as Value, + optional: (options.optional ?? false) as Optional +}) + +/** + * Plain text between a modal's inputs. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export interface Text { + readonly [TypeId]: typeof TypeId + readonly kind: "text" + readonly text: string +} + +/** + * Defines plain text for a modal. It has no value, so its key is not in + * `submit.values`. + * + * **Gotchas** + * + * The text is plain, not Markdown. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export const text = (text: string): Text => ({ [TypeId]: TypeId, kind: "text", text }) + +/** + * Any field of a modal. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export type Field = TextInput | Choice | SelectInput | Text + +/** + * A modal's fields by id, shown in key order. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export interface Fields { + readonly [id: string]: Field +} + +type Maybe = Optional extends true ? A | undefined : A + +/** + * The decoded value of one field. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export type FieldValue = F extends TextInput ? Maybe + : F extends Choice ? Maybe + : F extends SelectInput ? Maybe, Optional> + : never + +/** + * `submit.values`: one key per input, none for `text`. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export type Values = { + readonly [K in keyof Fs as Fs[K] extends Text ? never : K]: FieldValue +} + +/** + * The options `open` must be given: one list per `selectInput` field. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export type SelectOptions = { + readonly [K in keyof Fs as Fs[K] extends SelectInput ? K : never]: Fs[K] extends + SelectInput ? ReadonlyArray<{ readonly label: string; readonly value: TypeOf }> + : never +} + +/** + * What a modal's `open` takes: the metadata when the modal has a schema for + * it, the options of each `selectInput`, and initial values. + * + * @stability unstable + * @category fields + * @since 4.1.5 + */ +export type OpenOptions = + & (Metadata extends ValueSchema ? { readonly metadata: Metadata["Type"] } : {}) + & ({} extends SelectOptions ? {} : { readonly options: SelectOptions }) + & { readonly initial?: { readonly [K in keyof Values]?: Values[K] | undefined } | undefined } + +/** + * A modal definition. + * + * **Details** + * + * `open` gives the reply that opens the modal. A click's or a command's + * handler can return it. + * + * **Gotchas** + * + * `open` throws when the metadata or an option's value cannot be encoded. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export interface Modal< + out Id extends string, + in out Metadata extends ValueSchema | undefined = undefined, + in out Fs extends Fields = {} +> { + readonly [TypeId]: typeof TypeId + readonly kind: "modal" + readonly id: Id + readonly title: string + readonly submitLabel: string | undefined + readonly metadata: Metadata + readonly fields: Fs + readonly open: ( + ...args: {} extends OpenOptions ? [options?: OpenOptions] + : [options: OpenOptions] + ) => OpenModal +} + +/** + * Defines a modal. + * + * **Details** + * + * - The id names the submit's handler in a `group`. With the encoded + * metadata it counts toward `limits.modalMetadataLength`. + * - Inputs and texts together count toward `limits.modalFields`. + * + * **Gotchas** + * + * A modal the platform will not take can only fail after the click's or + * command's handler returned it: the adapter opens nothing and calls + * `Handler.refused`. A portable modal has a title of 24 or less, labels of 45 + * or less, at most 5 fields, and an id plus metadata under 100 characters. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export const modal = < + const Id extends string, + const Fs extends Fields, + Metadata extends ValueSchema | undefined = undefined +>( + id: Id, + options: { + readonly title: string + readonly submitLabel?: string | undefined + readonly metadata?: Metadata + readonly fields: Fs + } +): Modal => { + const metadata = options.metadata as Metadata + const open = (args?: { + readonly metadata?: unknown + readonly options?: { readonly [id: string]: ReadonlyArray<{ readonly label: string; readonly value: unknown }> } + readonly initial?: { readonly [id: string]: unknown } + }): OpenModal => { + const elements: Array = [] + for (const [field, definition] of Object.entries(options.fields)) { + const initial = args?.initial?.[field] + switch (definition.kind) { + case "text": { + elements.push({ _tag: "Text", text: definition.text }) + break + } + case "textInput": { + elements.push({ + _tag: "TextInput", + id: field, + label: definition.label, + ...optional("multiline", definition.multiline ? true : undefined), + ...optional("placeholder", definition.placeholder), + ...optional("initial", initial as string | undefined), + ...optional("optional", definition.optional ? true : undefined), + ...optional("minLength", definition.minLength), + ...optional("maxLength", definition.maxLength) + }) + break + } + case "choice": + case "selectInput": { + const schema: ValueSchema | undefined = definition.kind === "selectInput" ? definition.value : undefined + const encode = (value: unknown): string => schema === undefined ? value as string : encodeValue(schema, value) + const choices = definition.kind === "choice" ? definition.options : args?.options?.[field] ?? [] + elements.push({ + _tag: "SelectInput", + id: field, + label: definition.label, + ...optional("placeholder", definition.placeholder), + options: choices.map((option) => ({ label: option.label, value: encode(option.value) })), + ...optional("initial", initial === undefined ? undefined : encode(initial)), + ...optional("optional", definition.optional ? true : undefined) + }) + break + } + } + } + return OpenModal.make({ + modal: { + _tag: "Modal", + id, + title: options.title, + ...optional("submitLabel", options.submitLabel), + ...optional("metadata", metadata === undefined ? undefined : encodeValue(metadata, args?.metadata)), + elements + } + }) + } + return { + [TypeId]: TypeId, + kind: "modal", + id, + title: options.title, + submitLabel: options.submitLabel, + metadata, + fields: options.fields, + open: open as Modal["open"] + } +} + +/** + * A slash command definition: a name and at most one text argument. + * + * **Details** + * + * `description` and `argument` are what registration needs. Slack reads its + * own from the app manifest, so there they are documentation. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export interface Command { + readonly [TypeId]: typeof TypeId + readonly kind: "command" + readonly id: Id + readonly description: string + readonly argument: { + readonly name: string + readonly description: string + readonly required: boolean + } | undefined +} + +/** + * Defines a slash command. + * + * **Details** + * + * - The id is the command's name without the slash. It shares the registry's + * id space with buttons, selects and modals. + * - Slack sends everything typed after the name as one string. Discord gets + * one `STRING` option named `argument.name`, and a command with no + * `argument` has no options. + * - `argument.required` defaults to false. Discord's client enforces it. + * Slack cannot, so a portable handler still checks for an empty `text`. + * + * **Gotchas** + * + * A portable name matches `^[a-z0-9_-]{1,31}$` and a portable description is + * 100 characters or less. Neither is checked here. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export const command = (id: Id, options: { + readonly description: string + readonly argument?: { + readonly name: string + readonly description: string + readonly required?: boolean | undefined + } | undefined +}): Command => ({ + [TypeId]: TypeId, + kind: "command", + id, + description: options.description, + argument: options.argument === undefined ? undefined : { + name: options.argument.name, + description: options.argument.description, + required: options.argument.required ?? false + } +}) + +/** + * Any definition that has a handler. + * + * @stability unstable + * @category definitions + * @since 4.1.5 + */ +export type Definition = Button | Select | Modal | Command + +// ----------------------------------------------------------------------------- +// decoding +// ----------------------------------------------------------------------------- + +const matches = (definition: Definition, event: Interaction): boolean => { + switch (event._tag) { + case "ButtonClicked": + return definition.kind === "button" + case "SelectChanged": + return definition.kind === "select" + case "ModalSubmitted": + return definition.kind === "modal" + case "CommandInvoked": + return definition.kind === "command" + } +} + +const decodeSubmit = (definition: Modal, event: ModalSubmitted): Option.Option => { + let metadata: unknown = undefined + if (definition.metadata !== undefined) { + if (event.metadata === undefined) return Option.none() + const decoded = decodeValue(definition.metadata, event.metadata) + if (Option.isNone(decoded)) return decoded + metadata = decoded.value + } + const values: Record = {} + for (const [id, field] of Object.entries(definition.fields as Fields)) { + if (field.kind === "text") continue + const encoded = Object.hasOwn(event.values, id) ? event.values[id] : undefined + if (encoded === undefined) { + if (!field.optional) return Option.none() + values[id] = undefined + continue + } + switch (field.kind) { + case "textInput": { + values[id] = encoded + break + } + case "choice": { + if (!field.options.some((option) => option.value === encoded)) return Option.none() + values[id] = encoded + break + } + case "selectInput": { + if (field.value === undefined) { + values[id] = encoded + break + } + const decoded = decodeValue(field.value, encoded) + if (Option.isNone(decoded)) return decoded + values[id] = decoded.value + break + } + } + } + return Option.some({ ...base(event), metadata, values }) +} + +const base = (event: ButtonClicked | SelectChanged | ModalSubmitted) => ({ + id: event.id, + thread: event.thread, + messageId: event.messageId, + userId: event.userId, + raw: event.raw +}) + +/** The event as the definition's handler takes it, or none when its value no longer decodes. */ +const decodeEvent = (definition: Definition, event: Interaction): Option.Option => { + switch (event._tag) { + case "ButtonClicked": { + if (definition.kind !== "button") return Option.none() + if (definition.value === undefined) return Option.some({ ...base(event), value: undefined }) + if (event.value === undefined) return Option.none() + return Option.map(decodeValue(definition.value, event.value), (value) => ({ ...base(event), value })) + } + case "SelectChanged": { + if (definition.kind !== "select") return Option.none() + if (definition.value === undefined) return Option.some({ ...base(event), value: event.value }) + return Option.map(decodeValue(definition.value, event.value), (value) => ({ ...base(event), value })) + } + case "ModalSubmitted": { + return definition.kind === "modal" ? decodeSubmit(definition, event) : Option.none() + } + case "CommandInvoked": { + return definition.kind === "command" ? Option.some(event) : Option.none() + } + } +} + +const fallback = (event: Interaction): Ack | Close => event._tag === "ModalSubmitted" ? close : ack + +// ----------------------------------------------------------------------------- +// groups +// ----------------------------------------------------------------------------- + +/** + * The handler a definition asks for. None has an error channel. + * + * **Details** + * + * A button or a select answers `ack` or a modal to open. A modal answers + * `close` or `errors` naming its own inputs. A command answers `respond`, + * a modal to open or `defer`, and never `ack`: Discord has no silent answer + * to a command. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ +export type HandlerFor = D extends Button + ? (click: Click>) => Effect.Effect + : D extends Select ? + (selected: Selected>) => Effect.Effect + : D extends Modal ? ( + submit: Submit, Values> + ) => Effect.Effect & string>, never, any> + : D extends Command ? (command: CommandInvoked) => Effect.Effect + : never + +/** + * One handler per id in the group. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ +export type HandlersFrom = { + readonly [Current in D as Current["id"]]: HandlerFor +} + +/** + * The services a set of handlers needs. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ +export type HandlersServices = { + readonly [K in keyof Handlers]: Handlers[K] extends (...args: any) => Effect.Effect ? R : never +}[keyof Handlers] + +/** + * A set of definitions whose handlers are registered together. + * + * **Details** + * + * - `toLayer` registers the handlers, with their services provided. It needs + * the `Registry` that `ChatInteraction.layer` provides. + * - `of` is the identity, for handlers written away from `toLayer`. + * + * **Gotchas** + * + * Two definitions with one id, in one group or across groups, are a defect + * when the layer is built. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ +export interface Group { + readonly [TypeId]: typeof TypeId + readonly definitions: ReadonlyMap + readonly add: >(...definitions: More) => Group + readonly merge: >>( + ...groups: Groups + ) => Group> + readonly toLayer: , EX = never, RX = never>( + build: Handlers | Effect.Effect + ) => Layer.Layer | HandlersServices | Registry> + readonly of: >(handlers: Handlers) => Handlers +} + +/** + * Types that go with a group. + * + * @stability unstable + * @since 4.1.5 + */ +export declare namespace Group { + /** + * The definitions of a group. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ + export type Definitions = G extends Group ? D : never +} + +const groupLists = new WeakMap, ReadonlyArray>() + +const makeGroup = (list: ReadonlyArray): Group => { + const self: Group = { + [TypeId]: TypeId, + definitions: new Map(list.map((definition) => [definition.id, definition])), + add: (...definitions) => makeGroup([...list, ...definitions]), + merge: (...groups) => makeGroup([...list, ...groups.flatMap((group) => groupLists.get(group) ?? [])]) as any, + toLayer: (build) => + Layer.effectDiscard(Effect.gen(function*() { + const handlers: Record Effect.Effect> = + Effect.isEffect(build) + ? yield* build + : build as any + const context = yield* Effect.context() + const registry = yield* Registry + yield* registry.register(list.map((definition): Registered => ({ + definition, + handle: (event) => { + const typed = decodeEvent(definition, event) + return Option.isNone(typed) + ? Effect.succeed(fallback(event)) + : Effect.provideContext(handlers[definition.id](typed.value), context) + } + }))) + })) as any, + of: (handlers) => handlers + } + groupLists.set(self, list) + return self +} + +/** + * Groups definitions so their handlers can be registered together. + * + * @stability unstable + * @category groups + * @since 4.1.5 + */ +export const group = >( + ...definitions: Definitions +): Group => makeGroup(definitions) + +// ----------------------------------------------------------------------------- +// handler service and registry +// ----------------------------------------------------------------------------- + +/** + * What an adapter calls with an interaction. Public, for a consumer that + * routes clicks itself. + * + * **Details** + * + * - The contract is to answer inside the platform's 3 seconds. There is no + * error channel: a handler that cannot do its work still has to answer. + * - `refused` is called when a click's handler returned `OpenModal` and no + * modal opened, after the click was acked: by the adapter (the platform + * has no `modals`, the modal's id plus metadata is over + * `limits.modalMetadataLength`, it has more than `limits.modalFields` + * fields, a title or label is too long, or the platform refused the call) + * or by `handle` (the reply came after the deadline). + * - A command's reply can be refused too: a private answer without + * `ephemeral`, an element the platform lacks, an answer over + * `limits.messageLength` or an element limit, a modal that cannot open, + * or a refusal by the platform. A submit's replies always have an answer + * the adapter can give. + * - `command` answers `Ack` for a command it has no definition for, and the + * adapter sends nothing for that. On Discord another library in the same + * process can still answer it. + * + * **Gotchas** + * + * Neither platform redelivers a click or a submit, so there is no dedupe key. + * A submit can still arrive twice for one modal: a handler that runs past + * the deadline is interrupted, the platform shows its own error, the modal + * stays open and the user can submit again. Nothing marks the second one as + * a repeat, and the first run may have been cut off part way. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class Handler extends Context.Service Effect.Effect + readonly submit: (event: ModalSubmitted) => Effect.Effect + readonly command: (event: CommandInvoked) => Effect.Effect + readonly refused: ( + event: ButtonClicked | SelectChanged | CommandInvoked, + error: ChatError + ) => Effect.Effect +}>()("effect/chat/ChatInteraction/Handler") {} + +const lateClick = (event: ButtonClicked | SelectChanged) => + new ChatError({ + platform: event.thread.platform, + op: "openModal", + reason: new NotFound({ detail: interactionExpired }) + }) + +/** + * Calls `Handler.click`, `Handler.submit` or `Handler.command` and gives + * the reply, or `Late` when `deadline` passes first. + * + * **When to use** + * + * Use to deliver an interaction from an adapter. This is the one place the + * deadline rule lives. Both adapters and + * `ChatTest` call it, and a third-party adapter should. + * + * **Details** + * + * - A late click leaves its handler running, detached from the caller. When + * it later returns `OpenModal`, the modal is dropped and `Handler.refused` + * is called with a `ChatError` whose reason is `NotFound`, op `openModal`, + * detail `interaction_expired`, platform `event.thread.platform`. + * - A late submit or command interrupts its handler. `Late` is given at the + * deadline, without waiting for the handler's finalizers to finish. A + * command handler that cannot answer in time returns `defer` first. + * - `deadline` is the handler's share of the platform's 3 seconds, not the 3 + * seconds. The adapter subtracts what its own answer costs. This function + * knows no platform. + * + * **Gotchas** + * + * A late click's handler keeps running only if the runtime lets it. One that + * stops work once the response is sent (Cloudflare Workers without + * `waitUntil`) kills it. + * + * @stability unstable + * @category handling + * @since 4.1.5 + */ +export const handle: { + (event: ButtonClicked | SelectChanged, deadline: Duration.Duration): Effect.Effect + (event: ModalSubmitted, deadline: Duration.Duration): Effect.Effect + (event: CommandInvoked, deadline: Duration.Duration): Effect.Effect + ( + event: Interaction, + deadline: Duration.Duration + ): Effect.Effect +} = (event: Interaction, deadline: Duration.Duration): Effect.Effect => + // Keep startup and the deadline handoff atomic with respect to caller interruption. + Effect.uninterruptibleMask((restore) => + Effect.gen(function*() { + const handler = yield* Handler + // Detached, so the reply at the deadline waits for neither a late click nor a late submit's cleanup. + const fiber: Fiber.Fiber = yield* Effect.forkDetach( + event._tag === "ModalSubmitted" + ? handler.submit(event) + : event._tag === "CommandInvoked" + ? handler.command(event) + : handler.click(event) + ) + const exit = yield* Effect.onInterrupt( + restore(Effect.timeoutOption(Fiber.await(fiber), deadline)), + () => Fiber.interrupt(fiber) + ) + if (Option.isSome(exit)) return yield* exit.value + if (event._tag === "ModalSubmitted" || event._tag === "CommandInvoked") { + // Interrupt without delaying the deadline reply. + yield* Effect.forkDetach(Fiber.interrupt(fiber)) + return late + } + yield* Effect.forkDetach(Effect.flatMap( + Fiber.await(fiber), + (exit) => + Exit.isSuccess(exit) && exit.value._tag === "OpenModal" + ? handler.refused(event, lateClick(event)) + : Effect.void + )) + return late + }) + ) + +const late: Late = Late.make({}) + +/** + * One registered definition with its handler, services already provided. + * + * **Details** + * + * `handle` answers `ack` or `close` without calling the handler when the + * event does not match the definition or its value no longer decodes. A + * command that does not match is answered `ack`. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export interface Registered { + readonly definition: Definition + readonly handle: (event: Interaction) => Effect.Effect +} + +/** + * Where groups put their handlers. `ChatInteraction.layer` provides it. + * + * **Details** + * + * `register` dies when an id is already registered. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class Registry extends Context.Service) => Effect.Effect + readonly get: (id: string) => Effect.Effect +}>()("effect/chat/ChatInteraction/Registry") {} + +/** + * Why an interaction reached no handler, or why its handler's reply was not + * carried out. + * + * **Details** + * + * - `UnknownId`: no definition has the id, or it is a definition of another + * kind. + * - `InvalidValue`: the value or metadata no longer decodes, or a required + * input is missing. + * - `ReplyRefused`: `Handler.refused` was called, with the adapter's error. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Unhandled = + | { readonly reason: "UnknownId" | "InvalidValue" } + | { readonly reason: "ReplyRefused"; readonly error: ChatError } + +/** + * The `Handler` that routes by id to the registered definitions, and an + * empty `Registry`. Group layers go on top with `Layer.provideMerge`. + * + * **Details** + * + * - An interaction with an unknown id, or with a value that no longer + * decodes, is logged and answered `ack` or `close`. `unhandled` sees it + * first. It cannot change the answer. + * - A command with an unknown id is answered `ack`, which the adapter sends + * nothing for, and logged at debug level: on Discord it may belong to + * another library in the same process. + * - `Handler.refused` is routed to `unhandled` too, as `ReplyRefused`, and + * logged. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options?: { + readonly unhandled?: ((event: Interaction, why: Unhandled) => Effect.Effect) | undefined +}): Layer.Layer => + Layer.effectContext(Effect.gen(function*() { + const context = yield* Effect.context() + const entries = new Map() + const report = (event: Interaction, why: Unhandled, message: string): Effect.Effect => + Effect.andThen( + options?.unhandled === undefined ? Effect.void : Effect.provideContext(options.unhandled(event, why), context), + (event._tag === "CommandInvoked" && why.reason === "UnknownId" + ? Effect.logDebug(message) + : Effect.logWarning(message)).pipe(Effect.annotateLogs({ + "chat.platform": event.thread.platform, + "chat.interaction.id": event.id, + "chat.interaction.reason": why.reason + })) + ) + const route = (event: Interaction): Effect.Effect => + Effect.suspend(() => { + const entry = entries.get(event.id) + if (entry === undefined || !matches(entry.definition, event)) { + return Effect.as( + report(event, { reason: "UnknownId" }, "ChatInteraction: no handler for this id"), + fallback(event) + ) + } + if (Option.isNone(decodeEvent(entry.definition, event))) { + return Effect.as( + report(event, { reason: "InvalidValue" }, "ChatInteraction: the value no longer decodes"), + fallback(event) + ) + } + return entry.handle(event) + }) + const registry = Registry.of({ + register: (registered) => + Effect.suspend(() => { + const ids = new Set() + for (const entry of registered) { + const id = entry.definition.id + if (entries.has(id) || ids.has(id)) { + return Effect.die(new Error("ChatInteraction: the id \"" + id + "\" is already registered")) + } + ids.add(id) + } + for (const entry of registered) entries.set(entry.definition.id, entry) + return Effect.void + }), + get: (id) => Effect.sync(() => entries.get(id)) + }) + const handler = Handler.of({ + click: (event) => route(event) as Effect.Effect, + submit: (event) => route(event) as Effect.Effect, + command: (event) => route(event) as Effect.Effect, + refused: (event, error) => + report(event, { reason: "ReplyRefused", error }, "ChatInteraction: the reply was not carried out").pipe( + Effect.annotateLogs("chat.error", error.message) + ) + }) + return Context.make(Handler, handler).pipe(Context.add(Registry, registry)) + })) diff --git a/packages/effect/src/chat/ChatMarkdown.ts b/packages/effect/src/chat/ChatMarkdown.ts new file mode 100644 index 00000000000..8dbfa9984eb --- /dev/null +++ b/packages/effect/src/chat/ChatMarkdown.ts @@ -0,0 +1,1405 @@ +/** + * Posting long and streamed Markdown through `Chat`. + * + * `post` splits Markdown that outgrows one message into parts at block + * boundaries. `stream` writes a stream of Markdown deltas as it arrives, by + * the adapter's native stream when it has one, otherwise by posting and then + * editing, and on an adapter without `edit` by posting each part once. Both + * sit above the adapter ops and use one splitter. + * + * **Example** (Streaming a reply) + * + * ```ts + * import { Effect, Stream } from "effect" + * import { ChatMarkdown, ChatThread } from "effect/chat" + * + * const reply = (deltas: Stream.Stream) => + * Effect.gen(function*() { + * const result = yield* ChatMarkdown.stream(ChatThread.make("slack", "T1/C1"), deltas) + * return result.parts.map((part) => part.messageId) + * }) + * ``` + * + * @stability unstable + * @since 4.1.5 + */ +import * as Cause from "../Cause.ts" +import * as Clock from "../Clock.ts" +import * as Data from "../Data.ts" +import type * as DateTime from "../DateTime.ts" +import * as Duration from "../Duration.ts" +import * as Effect from "../Effect.ts" +import * as Exit from "../Exit.ts" +import * as Fiber from "../Fiber.ts" +import * as Latch from "../Latch.ts" +import * as Option from "../Option.ts" +import * as Scope from "../Scope.ts" +import * as Stream from "../Stream.ts" +import * as Chat from "./Chat.ts" +import type { Descriptor, File, NativeStream, Posted } from "./ChatAdapter.ts" +import { Registry } from "./ChatAdapter.ts" +import { ChatError, fileRefused, TooLarge } from "./ChatError.ts" +import type { ReplyTo } from "./ChatMessage.ts" +import type { ChatThread } from "./ChatThread.ts" +import type { Action } from "./ChatUi.ts" + +/** + * One message a call created. + * + * **Details** + * + * - `markdown` is the Markdown as it went out, with a reopened or closed code + * fence and a consumer's unattached note included. It is empty for the + * overflow file. + * - `text` is the slice of the input the part carries, without anything the + * layer added. The `text` of every part, in order, is the input. + * - `transport` is `post` for a part from `post` and for a streamed part + * posted once to an adapter without `edit`, `edit` for a streamed part + * written by post-then-edit and `native` for one written by the adapter's + * native stream, even if it was finished by an edit. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Part { + readonly messageId: string + readonly timestamp: DateTime.Utc + readonly markdown: string + readonly text: string + readonly kind: "text" | "overflowFile" + readonly transport: "post" | "edit" | "native" +} + +/** + * What a call posted. + * + * **Details** + * + * - `parts` is empty when the input was empty: nothing is posted. + * - `stopped` means a user clicked stop on this reply (Slack's stop button). + * It can be true with the whole text delivered, when the consumer ignored + * the click. It is always false from `post`. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Result { + readonly parts: ReadonlyArray + readonly stopped: boolean +} + +/** + * Error for a call that did not deliver its whole input. + * + * **Details** + * + * - `posted` is text the platform confirmed, `uncertain` is text sent in a + * write that failed `uncertain` and was never confirmed since, and + * `remaining` was never sent. The `text` of `posted`, then `uncertain`, + * then `remaining` is the input, with nothing in two of them. + * - `remainingFence` is set when `remaining` starts inside a code fence: + * the line that opened it, with its info string. Text sent later starts + * with that line and a newline, then `remaining`. + * - `cause` is a `ChatError` from a write, the upstream's error `E`, or + * `Overflow` when the input outgrew `maxParts` and the overflow file. + * + * **Gotchas** + * + * It is not a `Schema.TaggedError`: it is generic in `E` and carries + * message text. `message` and `toJSON` leave the text out. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class ChatMarkdownError extends Data.TaggedError("ChatMarkdownError")<{ + readonly posted: ReadonlyArray + readonly remaining: string + readonly remainingFence?: string | undefined + readonly uncertain?: string | undefined + readonly cause: + | { readonly _tag: "Chat"; readonly error: ChatError } + | { readonly _tag: "Upstream"; readonly error: E } + | { readonly _tag: "Overflow" } +}> { + override get message(): string { + const cause = this.cause._tag === "Chat" ? "Chat(" + this.cause.error.reason._tag + ")" : this.cause._tag + return "ChatMarkdown failed after " + this.posted.length + " part(s): " + cause + } + + /** + * The error without its message text. + */ + override toJSON(): unknown { + return { + _tag: this._tag, + posted: this.posted.map((part) => ({ messageId: part.messageId, kind: part.kind, transport: part.transport })), + cause: this.cause._tag === "Chat" ? { _tag: "Chat", error: this.cause.error.toJSON() } : { _tag: this.cause._tag } + } + } +} + +/** + * The options `post` and `stream` share. + * + * **Details** + * + * - `replyTo` applies to the first part. + * - `maxParts` defaults to 5, a provisional number. Past it, `overflow` + * decides: `fail`, the default, or `{ file }`, which sends the rest as one + * trailing file message that does not count against `maxParts`. + * - `minWriteInterval` is the floor between two writes. It defaults to the + * adapter's `limits.minWriteInterval`. + * - `onPart` runs for every message the call creates, the overflow file + * included, uninterruptibly once its post returns, so an interrupt can + * neither lose an id nor post the message twice. It has to be quick. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Options { + readonly replyTo?: ReplyTo | undefined + readonly maxParts?: number | undefined + readonly overflow?: "fail" | { readonly file: { readonly name: string } } | undefined + readonly minWriteInterval?: Duration.Duration | undefined + readonly onPart?: ((part: Part) => Effect.Effect) | undefined +} + +// ----------------------------------------------------------------------------- +// Markdown scanning +// ----------------------------------------------------------------------------- + +interface Fence { + readonly marker: string + readonly line: string +} + +interface Line { + /** The index after the line's newline. */ + readonly end: number + /** The fence open after the line. */ + readonly fence: Fence | undefined + /** A part can end after this line: it is outside a fence, and blank or closing a fence. */ + readonly boundary: boolean +} + +const fencePattern = /^ {0,3}(`{3,}|~{3,})(.*)$/ + +const fenceAfter = ( + fence: Fence | undefined, + line: string +): { readonly fence: Fence | undefined; readonly closed: boolean } => { + const match = fencePattern.exec(line) + if (match === null) return { fence, closed: false } + const marker = match[1] + if (fence === undefined) { + return marker[0] === "`" && match[2].includes("`") + ? { fence, closed: false } + : { fence: { marker, line: line.trimStart() }, closed: false } + } + return marker[0] === fence.marker[0] && marker.length >= fence.marker.length && match[2].trim() === "" + ? { fence: undefined, closed: true } + : { fence, closed: false } +} + +/** + * The lines of `text`, read from a state of `fence`. Only complete lines + * count, unless `complete` says the text is finished. + */ +const scanLines = (text: string, fence: Fence | undefined, complete: boolean): Array => { + const lines: Array = [] + let start = 0 + while (start < text.length) { + const newline = text.indexOf("\n", start) + if (newline === -1 && !complete) break + const end = newline === -1 ? text.length : newline + 1 + const line = text.slice(start, newline === -1 ? text.length : newline).replace(/\r$/, "") + const step = fenceAfter(fence, line) + fence = step.fence + lines.push({ end, fence, boundary: fence === undefined && (step.closed || line.trim() === "") }) + start = end + } + return lines +} + +const fenceAt = (lines: ReadonlyArray, initial: Fence | undefined, index: number): Fence | undefined => { + let fence = initial + for (const line of lines) { + if (line.end > index) break + fence = line.fence + } + return fence +} + +const closing = (fence: Fence | undefined, markdown: string): string => + fence === undefined ? "" : (markdown === "" || markdown.endsWith("\n") ? "" : "\n") + fence.marker + +const reopening = (fence: Fence | undefined): string => fence === undefined ? "" : fence.line + "\n" + +const isBlank = (text: string) => text.trim() === "" + +/** Markdown for `text` written as a whole message: an open fence is closed. */ +const render = (prefix: string, text: string, fence: Fence | undefined): string => { + const markdown = prefix + text + return markdown + closing(fenceAt(scanLines(text, fence, true), fence, text.length), markdown) +} + +/** + * Where to cut `text` so that `prefix`, the cut text and a closing fence + * measure at most `room`: after the last block boundary that fits, else after + * the last line, else after the last whitespace, else at the last character. + * Never inside a grapheme cluster, and never at 0. + */ +const fitCut = ( + measure: (markdown: string) => number, + text: string, + fence: Fence | undefined, + prefix: string, + room: number +): number => { + const lines = scanLines(text, fence, false) + const size = (cut: number) => { + const markdown = prefix + text.slice(0, cut) + return measure(markdown + closing(fenceAt(lines, fence, cut), markdown)) + } + const fits = (cut: number) => size(cut) <= room + let low = 0 + let high = text.length + while (low < high) { + const middle = Math.ceil((low + high) / 2) + if (fits(middle)) low = middle + else high = middle - 1 + } + const most = low + const candidates: Array = [] + for (let i = lines.length - 1; i >= 0; i--) { + if (lines[i].boundary && lines[i].end <= most) candidates.push(lines[i].end) + } + for (let i = lines.length - 1; i >= 0; i--) { + if (lines[i].end <= most) candidates.push(lines[i].end) + } + for (let i = most - 1; i > 0; i--) { + if (/\s/.test(text[i])) { + candidates.push(i + 1) + break + } + } + candidates.push(most) + const clusters = graphemes(text) + for (const candidate of candidates) { + const cut = clusterStart(clusters, text, candidate) + if (cut > 0 && fits(cut)) return cut + } + // Not even one cluster fits: the cut goes after the first one, and the write fails `TooLarge`. + return clusterStart(clusters, text, most) || clusterEnd(clusters, 0) +} + +let segmenter: Intl.Segmenter | undefined + +/** The grapheme clusters of `text`. `Intl.Segmenter` is created on first use. */ +const graphemes = (text: string): Intl.Segments => + (segmenter ??= new Intl.Segmenter(undefined, { granularity: "grapheme" })).segment(text) + +/** The grapheme cluster boundary at or before `cut`. */ +const clusterStart = (clusters: Intl.Segments, text: string, cut: number): number => + cut <= 0 || cut >= text.length ? cut : clusters.containing(cut)!.index + +/** The end of the grapheme cluster that contains `index`. */ +const clusterEnd = (clusters: Intl.Segments, index: number): number => { + const cluster = clusters.containing(index)! + return cluster.index + cluster.segment.length +} + +/** Where the last grapheme cluster of `text` that is not whitespace starts, or -1 for blank text. */ +const lastContentStart = (text: string): number => { + const end = text.trimEnd().length + return end === 0 ? -1 : graphemes(text).containing(end - 1)!.index +} + +/** Past this many held characters, a held line is released up to its last whitespace. */ +const holdLimit = 300 + +/** + * What an append may send of `pending` before the input is finished: up to + * the last complete line, unless inside a code fence. + */ +const releasable = (pending: string, fence: Fence | undefined): string => { + const lines = scanLines(pending, fence, false) + const complete = lines.length === 0 ? 0 : lines[lines.length - 1].end + if (complete === pending.length) return pending + if (fenceAt(lines, fence, complete) === undefined) { + const tail = pending.slice(complete) + if (tail.length <= holdLimit) return pending.slice(0, complete) + const space = tail.search(/\s\S*$/) + if (space !== -1) return pending.slice(0, complete + space + 1) + } + // The next delta may still extend the trailing cluster, even inside a fence. + // A split surrogate may complete a code point that extends the preceding cluster. + const text = /[\ud800-\udbff]$/.test(pending) ? pending.slice(0, -1) : pending + let last = 0 + for (const cluster of graphemes(text)) last = cluster.index + return pending.slice(0, last) +} + +const utf8Length = (text: string): number => { + let bytes = 0 + for (let i = 0; i < text.length; i++) { + const code = text.charCodeAt(i) + bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code >= 0xd800 && code <= 0xdfff ? 2 : 3 + } + return bytes +} + +// ----------------------------------------------------------------------------- +// the reply +// ----------------------------------------------------------------------------- + +const defaultMaxParts = 5 +/** Consecutive `retry` failures of appends on one stream before the repair. Provisional. */ +const appendRetryLimit = 3 +/** A native stream rolls over at the next block boundary past this share of `streamLength`. */ +const softShare = 0.85 +/** A native stream rotates once it is this share of `streamAge` old. */ +const ageShare = 0.8 +/** How long finishing a failed or interrupted reply may take. */ +const finishTimeout = Duration.seconds(5) + +type FailureCause = ChatMarkdownError["cause"] + +interface Current { + readonly start: number + readonly fence: Fence | undefined + readonly prefix: string + readonly transport: Part["transport"] + /** Appending to an open native stream. */ + native: boolean + posted: Posted | undefined + /** The input index the platform confirmed up to. */ + confirmed: number + /** The input index a write that failed `uncertain` reached. */ + uncertain: number + /** Native: the input index appended up to. */ + sent: number + /** The input length when this part was last written. */ + attempted: number + /** The Markdown the platform last confirmed. */ + markdown: string + startedAt: number + retries: number + /** A stop to send, and what follows it. Kept until the platform answers, so a stop cut off is sent again. */ + closing: Closing | undefined + /** A user clicked stop during a repair: the part is written up to here, then the reply ends. */ + stoppedAt: number | undefined +} + +interface Closing { + readonly end: "done" | "rotating" + readonly onStop: (stopped: boolean) => void +} + +interface ReplyOptions extends Options { + readonly thread: ChatThread + readonly initial: string + readonly deltas?: Stream.Stream | undefined + readonly descriptor: Descriptor + readonly chat: Chat.Chat["Service"] + readonly native?: NativeStream | undefined + readonly recipient?: string | undefined + readonly files?: ReadonlyArray | undefined + readonly actions?: ReadonlyArray | undefined + readonly unattachedNote?: ((files: ReadonlyArray) => string) | undefined +} + +const firstError = (exit: Exit.Exit): ChatError | undefined => + Option.getOrUndefined(Exit.findErrorOption(exit)) + +/** Whether a write was cut off before the platform answered. */ +const unanswered = (exit: Exit.Exit): boolean => + Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause) + +/** Whether the adapter guarantees the files were refused and nothing remains posted. */ +const isFileRefusal = (exit: Exit.Exit): boolean => { + const reason = firstError(exit)?.reason + return reason?.retry === "denied" && reason.detail === fileRefused +} + +const reply = (options: ReplyOptions): Effect.Effect, R | R2> => + Effect.suspend(() => { + const { chat, descriptor, thread } = options + const measure = descriptor.measure + const limits = descriptor.limits + const interval = Duration.toMillis(options.minWriteInterval ?? limits.minWriteInterval) + let maxParts = Math.max(1, options.maxParts ?? defaultMaxParts) + const overflow = options.overflow ?? "fail" + const streamLength = Math.min(limits.streamLength ?? limits.messageLength, limits.messageLength) + const streamAge = limits.streamAge === undefined ? undefined : Duration.toMillis(limits.streamAge) + const onPart = options.onPart ?? (() => Effect.void) + const native = options.native + const latch = Latch.makeUnsafe(false) + /** Opens when the upstream fails, which ends the loop even while a write is in flight. */ + const upstreamFailed = Latch.makeUnsafe(false) + /** Holds the write fibers, so they outlive the loop and the finalizer can wait for them. */ + const writes = Scope.makeUnsafe() + + let input = options.initial + let upstream: Exit.Exit | undefined = options.deltas === undefined ? Exit.void : undefined + let state: { readonly _tag: "Running" } | { readonly _tag: "Done"; readonly stopped: boolean } | { + readonly _tag: "Failed" + readonly cause: FailureCause + } = { _tag: "Running" } + let finishing = false + let nativeAllowed = native !== undefined && options.recipient !== undefined + /** Without `edit`, a streamed part is posted once, when it is final. */ + const buffered = options.deltas !== undefined && !descriptor.capabilities.has("edit") + let renderedNatively = false + let lastWrite = Number.NEGATIVE_INFINITY + let retryAt: number | undefined + let overflowStart: number | undefined + let overflowBytes = 0 + let overflowUncertain = false + let overflowFilesRefused = false + let note: string | undefined + const parts: Array = [] + + const isRunning = () => state._tag === "Running" + const fail = (cause: FailureCause) => { + if (isRunning()) state = { _tag: "Failed", cause } + } + const failChat = (error: ChatError) => fail({ _tag: "Chat", error }) + const done = (stopped: boolean) => { + if (isRunning()) state = { _tag: "Done", stopped } + } + + const newPart = (start: number, fence: Fence | undefined): Current => ({ + start, + fence, + prefix: reopening(fence), + transport: options.deltas === undefined ? "post" : nativeAllowed ? "native" : buffered ? "post" : "edit", + native: nativeAllowed, + posted: undefined, + confirmed: start, + uncertain: start, + sent: start, + attempted: -1, + markdown: "", + startedAt: 0, + retries: 0, + closing: undefined, + stoppedAt: undefined + }) + + let current = newPart(0, undefined) + + const toPart = (part: Current, posted: Posted, markdown: string, end: number): Part => ({ + messageId: posted.id, + timestamp: posted.timestamp, + markdown, + text: input.slice(part.start, end), + kind: "text", + transport: part.transport + }) + + /** The open fence at `index` of the input, read from the current part's complete lines. */ + const fenceAtInput = (part: Current, index: number): Fence | undefined => { + const text = input.slice(part.start, index) + return fenceAt(scanLines(text, part.fence, false), part.fence, text.length) + } + + const now = Clock.currentTimeMillis + + /** Waits for the floor since the previous write returned. */ + const pace: Effect.Effect = Effect.flatMap(now, (time) => { + const wait = lastWrite + interval - time + return wait > 0 ? Effect.sleep(Duration.millis(wait)) : Effect.void + }) + const wrote = Effect.flatMap(now, (time) => Effect.sync(() => void (lastWrite = time))) + + /** + * Sends an edit or a stop after the floor. Both are safe to send again, so + * they stay interruptible. `commit` records the answer, or that there was + * none, before an interrupt can land. + */ + const write = ( + effect: Effect.Effect, + commit: (exit: Exit.Exit) => void + ): Effect.Effect => + Effect.andThen( + pace, + Effect.uninterruptibleMask((restore) => + Effect.exit(restore(effect)).pipe( + Effect.ensuring(wrote), + Effect.flatMap((exit) => Effect.sync(() => commit(exit))) + ) + ) + ) + + /** The post, start or append in flight. */ + let inflight: Fiber.Fiber | undefined + + /** + * Sends a post, a start or an append after the floor. Sending one again + * could repeat text, so neither an interrupt of the reply nor a failure of + * the upstream cuts it off: it runs in a fiber of its own, outside the + * loop, which commits the answer, and the finalizer waits for it. Only the + * finish timeout cuts it off, and `commit` then records that there was no + * answer. + */ + const send = ( + effect: Effect.Effect, + commit: (exit: Exit.Exit, time: number) => Effect.Effect + ): Effect.Effect, never, R2> => + Effect.andThen( + pace, + Effect.uninterruptibleMask((restore) => + Effect.forkIn( + Effect.uninterruptibleMask((restoreRequest) => + Effect.exit(restoreRequest(effect)).pipe( + Effect.ensuring(wrote), + Effect.tap((exit) => Effect.flatMap(now, (time) => commit(exit, time))), + Effect.ensuring(Effect.sync(() => void (inflight = undefined))) + ) + ), + writes + ).pipe(Effect.flatMap((fiber) => { + inflight = fiber + return restore(Fiber.join(fiber)) + })) + ) + ) + + /** + * Creates a message, records it and runs `onPart` for it, in one commit, + * so an interrupt can neither lose an id nor make the finalizer create the + * message again. + */ + const create = ( + effect: Effect.Effect, + part: (posted: Posted) => Part, + commit: (exit: Exit.Exit, time: number) => void + ): Effect.Effect, never, R2> => + send(effect, (exit, time) => { + commit(exit, time) + return Exit.isSuccess(exit) ? onPart(part(exit.value)) : Effect.void + }) + + const finishPart = (end: number) => { + const part = current + parts.push(toPart(part, part.posted!, part.markdown, end)) + current = newPart(end, fenceAtInput(part, end)) + if (parts.length >= maxParts) { + overflowStart = end + overflowBytes = utf8Length(input.slice(end)) + } + } + + /** Ends the reply. Blank text left over is given to the last part, since it can't be posted alone. */ + const finishReply = (from: number) => { + const rest = input.slice(from) + const last = parts.length - 1 + if (rest !== "" && last >= 0) parts[last] = { ...parts[last], text: parts[last].text + rest } + done(false) + } + + const hasFiles = (options.files?.length ?? 0) > 0 + const actions = options.actions ?? [] + const hasActions = actions.length > 0 + /** What the reply's last part costs, measured with the actions it carries. */ + const measureLast = hasActions ? (markdown: string) => measure(markdown, actions) : measure + /** The last part takes the files and the actions. */ + const hasExtras = hasFiles || hasActions + /** + * Set when not even the text's last grapheme cluster fits beside the + * actions: the text is posted without them, and the files and the + * actions follow in a message of their own. + */ + let extrasAlone = false + /** Whether the note posted on its own takes the actions. */ + let noteActions = false + /** Set when the actions fit beside neither the text nor the note: they follow in a message of their own. */ + let actionsAfter = false + + // --- post-then-edit -------------------------------------------------------- + + /** + * Writes the current part as `input[start, end)`. `last` marks the part's + * final write, and `withFiles` the reply's last part, which takes the files. + */ + const writeEdit = (end: number, last: boolean, withFiles: boolean): Effect.Effect => + Effect.suspend(() => { + const part = current + const markdown = render(part.prefix, input.slice(part.start, end), part.fence) + part.attempted = input.length + if (part.posted === undefined) return postPart(part, end, markdown, withFiles && hasExtras) + if (part.confirmed === end && part.markdown === markdown) return Effect.void + const edit = chat.edit(thread, part.posted.id, { markdown }) + return write(last ? edit : Chat.withoutRetries(edit), (exit) => { + if (Exit.isSuccess(exit)) { + part.confirmed = part.uncertain = end + part.markdown = markdown + return + } + // Cut off, so it may have landed. The finalizer edits again. + if (unanswered(exit)) part.uncertain = Math.max(part.uncertain, end) + const error = firstError(exit) + if (error === undefined) return + if (error.retry === "uncertain") part.uncertain = Math.max(part.uncertain, end) + if (last || error.retry === "denied") failChat(error) + }) + }) + + const postPart = ( + part: Current, + end: number, + markdown: string, + withFiles: boolean + ): Effect.Effect => { + const files = withFiles && hasFiles ? options.files! : undefined + const unattachedNote = files === undefined ? undefined : options.unattachedNote + const withActions = withFiles && hasActions + const settle = (exit: Exit.Exit, markdown: string) => { + if (Exit.isSuccess(exit)) { + part.posted = exit.value + part.markdown = markdown + part.confirmed = part.uncertain = end + return + } + if (unanswered(exit)) part.uncertain = end + const error = firstError(exit) + if (error === undefined) return + if (error.retry === "uncertain") part.uncertain = end + failChat(error) + } + const attempt = ( + markdown: string, + files: ReadonlyArray | undefined, + fallback: boolean, + withActions: boolean + ) => + create( + chat.post(thread, { + markdown, + ...(parts.length === 0 && options.replyTo !== undefined ? { replyTo: options.replyTo } : {}), + ...(files === undefined ? {} : { files }), + ...(withActions ? { actions } : {}) + }), + (posted) => toPart(part, posted, markdown, end), + (exit) => { + // Left unsettled: the post goes out again without the files. + if (fallback && isFileRefusal(exit)) return + settle(exit, markdown) + } + ) + const withoutFiles = () => { + // Replace refused files with the unattached note. + const text = unattachedNote!(files!) + if (measureLast(markdown + text) <= limits.messageLength) { + return Effect.asVoid(attempt(markdown + text, undefined, false, withActions)) + } + // The note is the last message now, so it takes the actions, or they follow it on their own. + noteActions = withActions && measure(text, actions) <= limits.messageLength + actionsAfter = withActions && !noteActions + // With no text to post, this part is the note. + if (isBlank(markdown)) return Effect.asVoid(attempt(text, undefined, false, noteActions)) + note = text + return Effect.asVoid(attempt(markdown, undefined, false, false)) + } + if (overflowFilesRefused && files !== undefined) return withoutFiles() + return Effect.flatMap( + attempt(markdown, files, unattachedNote !== undefined, withActions), + (exit) => unattachedNote !== undefined && isFileRefusal(exit) ? withoutFiles() : Effect.void + ) + } + + /** Posts a message after the text that is not part of it: the unattached note, or the actions alone. */ + const postAfter = (text: string, withActions: boolean): Effect.Effect => { + const part = (posted: Posted): Part => ({ + messageId: posted.id, + timestamp: posted.timestamp, + markdown: text, + text: "", + kind: "text", + transport: "post" + }) + const post = chat.post(thread, { markdown: text, ...(withActions ? { actions } : {}) }) + return Effect.flatMap( + create(post, part, (exit) => { + if (Exit.isSuccess(exit)) return void parts.push(part(exit.value)) + const error = firstError(exit) + if (error !== undefined) failChat(error) + }), + () => isRunning() ? finishAfter : Effect.void + ) + } + + /** + * Ends the reply once the text is posted: the unattached note that did + * not fit the last part follows, then the actions when they fit beside + * neither. The actions' message counts against `maxParts`, and with no + * part left the reply fails `Overflow`. + */ + const finishAfter: Effect.Effect = Effect.suspend(() => { + if (note !== undefined) { + const text = note + note = undefined + return postAfter(text, noteActions) + } + if (actionsAfter) { + actionsAfter = false + if (parts.length >= maxParts) { + fail({ _tag: "Overflow" }) + return Effect.void + } + return postAfter("", true) + } + done(false) + return Effect.void + }) + + /** + * Posts the files and the actions in a message of their own, after the + * text. It counts against `maxParts`, and there is no text left to put in + * an overflow file, so with no part left the reply fails `Overflow`. + */ + const postExtras: Effect.Effect = Effect.suspend(() => { + extrasAlone = false + if (parts.length >= maxParts) { + fail({ _tag: "Overflow" }) + return Effect.void + } + const part = newPart(input.length, undefined) + current = part + return Effect.flatMap(postPart(part, input.length, "", true), () => { + if (!isRunning()) return Effect.void + finishPart(input.length) + return finishAfter + }) + }) + + const stepEdit = (final: boolean): Effect.Effect => + Effect.suspend(() => { + const part = current + if (part.stoppedAt !== undefined) { + // A user clicked stop during a repair: settle what was sent, then end the reply. + return Effect.map(writeEdit(part.stoppedAt, false, false), () => { + if (!isRunning()) return + finishPart(part.confirmed) + done(true) + }) + } + const body = input.slice(part.start) + if (part.posted === undefined && isBlank(body) && !(final && hasExtras && parts.length === 0)) { + if (final) finishReply(part.start) + return Effect.void + } + const end = (last: boolean): Effect.Effect => { + if (!last || !isRunning()) return Effect.void + return extrasAlone ? postExtras : finishAfter + } + const cutAt = (cut: number): Effect.Effect => { + // Only blank text follows the cut: this is the last part, so it takes the files, the actions and that text. + const last = final && isBlank(input.slice(cut)) + return Effect.flatMap(writeEdit(cut, true, last && !extrasAlone), () => { + if (!isRunning()) return Effect.void + finishPart(last ? input.length : cut) + return end(last) + }) + } + /** A cut that would end the reply has to leave room for the actions. */ + const lastFits = (cut: number) => + !final || !hasActions || extrasAlone || !isBlank(input.slice(cut)) || + measureLast(render(part.prefix, input.slice(part.start, cut), part.fence)) <= limits.messageLength + const cut = measure(render(part.prefix, body, part.fence)) > limits.messageLength + ? part.start + fitCut(measure, body, part.fence, part.prefix, limits.messageLength) + : input.length + if (!lastFits(cut)) { + // Reserve the last grapheme for a part with actions. If they cannot + // fit together, send the actions in their own message. + const keep = lastContentStart(body) + const tailStart = part.start + keep + const tailFence = fenceAtInput(part, tailStart) + const tail = render(reopening(tailFence), input.slice(tailStart), tailFence) + if (keep > 0 && measureLast(tail) <= limits.messageLength) { + return cutAt( + part.start + fitCut(measure, body.slice(0, keep), part.fence, part.prefix, limits.messageLength) + ) + } + extrasAlone = true + } + if (cut < input.length) return cutAt(cut) + if (buffered && !final) { + // Nothing to post until the part is final: it rolls over or the reply ends. + part.attempted = input.length + return Effect.void + } + return Effect.flatMap(writeEdit(input.length, final, final && !extrasAlone), () => { + if (final && isRunning()) finishPart(input.length) + return end(final) + }) + }) + + // --- native ---------------------------------------------------------------- + + /** Sends the stop the current part is closing with. A stop carries no text, so its failures are ignored. */ + const sendStop = (part: Current, closing: Closing): Effect.Effect => + write(native!.stop(thread, part.posted!.id, { end: closing.end }), (exit) => { + // Cut off, so the stream may still be open. The finalizer sends the stop again. + if (unanswered(exit)) return + part.closing = undefined + part.native = false + closing.onStop(Exit.isSuccess(exit) && exit.value.stopped) + }) + + /** Ends a native part at `end` once its stop is answered. The reply ends when `last`, or when a user had clicked stop. */ + const rollNative = (part: Current, end: number, last: boolean) => { + part.closing = { + end: last ? "done" : "rotating", + onStop: (stopped) => { + finishPart(end) + if (stopped) done(true) + else if (last) finishReply(end) + } + } + } + + /** + * Stops the stream and carries on by post-then-edit: the next edit writes + * the part's whole known text, the chunk that failed included. + */ + const repair = (part: Current) => { + part.closing = { + end: "rotating", + onStop: (stopped) => { + part.attempted = -1 + if (stopped) part.stoppedAt = Math.max(part.uncertain, part.sent) + } + } + } + + const startNative = (final: boolean): Effect.Effect => + Effect.suspend(() => { + const part = current + const pending = input.slice(part.start) + const text = final ? pending : releasable(pending, part.fence) + if (isBlank(text)) { + if (final) finishReply(part.start) + return Effect.void + } + part.attempted = input.length + // Measured with the closing fence, so a start leaves room to close it. + const whole = render(part.prefix, text, part.fence) + const cut = measure(whole) > streamLength + ? fitCut(measure, text, part.fence, part.prefix, streamLength) + : text.length + const end = part.start + cut + const roll = cut < text.length + const markdown = roll || final ? render(part.prefix, text.slice(0, cut), part.fence) : part.prefix + text + return Effect.asVoid( + create( + native!.start(thread, { + markdown, + ...(parts.length === 0 && options.replyTo !== undefined ? { replyTo: options.replyTo } : {}), + ...(parts.length === 0 ? {} : { continues: parts[parts.length - 1].messageId }), + recipient: options.recipient + }), + (posted) => toPart(part, posted, markdown, end), + (exit, time) => { + if (Exit.isSuccess(exit)) { + renderedNatively = true + part.posted = exit.value + part.markdown = markdown + part.confirmed = part.uncertain = part.sent = end + part.startedAt = time + if (roll || final) rollNative(part, end, !roll) + return + } + if (unanswered(exit)) part.uncertain = end + const error = firstError(exit) + if (error === undefined) return + if (error.reason._tag === "Unsupported" && !renderedNatively) { + nativeAllowed = false + current = newPart(part.start, part.fence) + return + } + if (error.retry === "uncertain") part.uncertain = end + failChat(error) + } + ) + ) + }) + + /** + * What the next append sends, and whether the part ends after it. Every + * append leaves room to close the code fence it ends in, so a part that + * rolls over on length always ends with its fence closed. `rotate` sends + * just that closing fence, for a stream about to rotate on age. + */ + const nextAppend = (part: Current, final: boolean, rotate: boolean) => { + const pending = input.slice(part.sent) + const fence = fenceAtInput(part, part.sent) + const lines = scanLines(pending, fence, false) + const size = (text: string) => measure(part.markdown + text) + const closeAt = (piece: string) => closing(fenceAt(lines, fence, piece.length), part.markdown + piece) + const closeOnly = { piece: "", extra: closeAt(""), roll: true } + if (rotate) return closeOnly + const soft = softShare * streamLength + const piece = final ? pending : releasable(pending, fence) + const reserve = final + ? closing(fenceAt(scanLines(pending, fence, true), fence, pending.length), part.markdown + pending) + : closeAt(piece) + const extra = final ? reserve : "" + if (size(piece + reserve) <= soft) return { piece, extra, roll: false } + for (const line of lines) { + if (!line.boundary || line.end > piece.length) continue + const measured = size(pending.slice(0, line.end)) + if (measured > streamLength) break + if (measured >= soft) return { piece: pending.slice(0, line.end), extra: "", roll: true } + } + if (size(piece + reserve) <= streamLength) return { piece, extra, roll: false } + const cut = pending.slice(0, fitCut(measure, pending, fence, part.markdown, streamLength)) + // Not even the smallest cut fits with its closing fence: send just the closing fence. + return size(cut + closeAt(cut)) <= streamLength ? { piece: cut, extra: closeAt(cut), roll: true } : closeOnly + } + + /** + * Appends the next piece. What follows, a rollover, a repair or a retry, + * is recorded in the commit, so an interrupt can't lose it. + */ + const appendNative = (final: boolean, rotate: boolean): Effect.Effect => + Effect.suspend(() => { + const part = current + const { extra, piece, roll } = nextAppend(part, final, rotate) + if (piece === "" && extra === "") { + if (roll || final) rollNative(part, part.sent, !roll) + return Effect.void + } + part.attempted = input.length + const markdown = piece + extra + const commit = ( + exit: Exit.Exit<{ readonly state: string; readonly held: number }, ChatError>, + time: number + ) => { + if (Exit.isSuccess(exit)) { + const { held, state } = exit.value + if (state === "stopped") { + part.native = false + finishPart(part.sent) + done(true) + } else if (state === "closed") { + part.native = false + part.attempted = -1 + } else { + const added = Math.max(0, markdown.length - held) + part.retries = 0 + part.sent += Math.min(piece.length, added) + part.confirmed = part.uncertain = part.sent + part.markdown += markdown.slice(0, added) + if (roll || (final && part.sent === input.length)) rollNative(part, part.sent, !roll) + } + return + } + // Cut off by the finish timeout, so it may have landed. + if (unanswered(exit)) { + part.uncertain = part.sent + piece.length + return + } + const error = firstError(exit) + if (error === undefined) return + if (error.reason._tag === "TooLarge") { + // Retry the chunk in the next part. + rollNative(part, part.sent, false) + } else if (error.retry === "retry") { + part.retries++ + if (part.retries >= appendRetryLimit) repair(part) + else retryAt = time + (error.retryAfter === undefined ? interval : Duration.toMillis(error.retryAfter)) + } else if (error.retry === "uncertain") { + part.uncertain = part.sent + piece.length + repair(part) + } else { + failChat(error) + part.closing = { end: "done", onStop: () => {} } + } + } + return Effect.asVoid( + send( + native!.append(thread, part.posted!.id, markdown, { flush: final || roll }), + (exit, time) => Effect.sync(() => commit(exit, time)) + ) + ) + }) + + const stepNative = (final: boolean): Effect.Effect => + Effect.suspend(() => { + const part = current + if (part.posted === undefined) return startNative(final) + if (final || streamAge === undefined) return appendNative(final, false) + return Effect.flatMap(now, (time) => appendNative(final, time - part.startedAt >= ageShare * streamAge)) + }) + + // --- overflow -------------------------------------------------------------- + + const stepOverflow = (final: boolean): Effect.Effect => + Effect.suspend(() => { + const start = overflowStart! + const rest = input.slice(start) + if (isBlank(rest)) { + if (final) finishReply(start) + return Effect.void + } + if (overflow === "fail" || overflowBytes > limits.uploadBytes) { + fail({ _tag: "Overflow" }) + return Effect.void + } + // The upload is skipped when the reply failed or was interrupted. + if (!final || finishing) { + if (finishing) done(false) + return Effect.void + } + const file: File = { + name: overflow.file.name, + contentType: "text/markdown", + content: new TextEncoder().encode(rest) + } + const part = (markdown: string) => (posted: Posted): Part => ({ + messageId: posted.id, + timestamp: posted.timestamp, + markdown, + text: rest, + kind: "overflowFile", + transport: "post" + }) + // Attach consumer files to the final message. + const attempt = (markdown: string, files: ReadonlyArray, fallback: boolean) => + create( + chat.post(thread, { markdown, files: [file, ...files], ...(hasActions ? { actions } : {}) }), + part(markdown), + (exit) => { + if (Exit.isSuccess(exit)) { + parts.push(part(markdown)(exit.value)) + overflowStart = input.length + return done(false) + } + if (fallback && isFileRefusal(exit)) return + if (unanswered(exit)) overflowUncertain = true + const error = firstError(exit) + if (error === undefined) return + overflowUncertain = error.retry === "uncertain" + failChat(error) + } + ) + const files = options.files ?? [] + const unattachedNote = files.length === 0 ? undefined : options.unattachedNote + return Effect.asVoid(Effect.map(attempt("", files, unattachedNote !== undefined), (exit) => { + if (unattachedNote === undefined || !isFileRefusal(exit)) return + // The overflow file was refused too. Drain the remaining text through + // the normal splitter, without files or another maxParts overflow. + overflowFilesRefused = true + overflowStart = undefined + maxParts = Infinity + nativeAllowed = false + current.native = false + })) + }) + + // --- the loop -------------------------------------------------------------- + + /** Whether there is a write to make. A stop is sent even after the reply failed. */ + const active = () => isRunning() || current.closing !== undefined + + const hasWork = (): boolean => { + if (!isRunning() || upstream !== undefined || current.closing !== undefined) return true + if (overflowStart !== undefined) { + return !isBlank(input.slice(overflowStart)) && (overflow === "fail" || overflowBytes > limits.uploadBytes) + } + const part = current + if (input.length <= part.attempted) return false + if (!part.native) return !isBlank(input.slice(part.start)) + return part.posted === undefined + ? !isBlank(releasable(input.slice(part.start), part.fence)) + : releasable(input.slice(part.sent), fenceAtInput(part, part.sent)) !== "" + } + + /** Waits until there is something to write, or a retry is due. */ + const awaitWork: Effect.Effect = Effect.suspend(() => { + if (retryAt !== undefined) { + const at = retryAt + retryAt = undefined + return Effect.flatMap(now, (time) => time < at ? Effect.sleep(Duration.millis(at - time)) : Effect.void) + } + if (hasWork()) return Effect.void + latch.closeUnsafe() + return Effect.andThen(latch.await, awaitWork) + }) + + const step = (final: boolean): Effect.Effect => { + const closing = current.closing + if (closing !== undefined) return sendStop(current, closing) + if (overflowStart !== undefined) return stepOverflow(final) + return current.native ? stepNative(final) : stepEdit(final) + } + + const loop: Effect.Effect = Effect.suspend(() => + active() ? + Effect.flatMap(awaitWork, () => { + if (!active()) return Effect.void + if (current.closing === undefined && upstream !== undefined && Exit.isFailure(upstream)) return Effect.void + // The write takes whatever arrived while it waited for the floor. + return Effect.andThen(pace, Effect.suspend(() => step(upstream !== undefined))).pipe(Effect.andThen(loop)) + }) : + Effect.void + ) + + /** Writes what was received as the end of the reply, closing an open fence, within a short timeout. */ + const finish: Effect.Effect = Effect.suspend(() => { + finishing = true + const drain: Effect.Effect = Effect.suspend(() => + active() ? Effect.andThen(step(true), drain) : Effect.void + ) + // A write in flight commits first. Interruptible, so the timeout can cut it short in the interrupt finalizer. + const awaitInflight = Effect.suspend(() => inflight === undefined ? Effect.void : Fiber.await(inflight)) + return Effect.timeoutOption(Effect.interruptible(Effect.andThen(awaitInflight, drain)), finishTimeout).pipe( + Effect.andThen(Effect.suspend(() => inflight === undefined ? Effect.void : Fiber.interrupt(inflight))), + Effect.asVoid + ) + }) + + const toError = (cause: FailureCause): ChatMarkdownError => { + const posted = parts.slice() + let uncertain = "" + let from: number + if (overflowStart !== undefined) { + from = overflowStart + if (overflowUncertain) { + uncertain = input.slice(from) + from = input.length + } + } else { + const part = current + if (part.posted !== undefined) posted.push(toPart(part, part.posted, part.markdown, part.confirmed)) + from = Math.max(part.confirmed, part.uncertain) + uncertain = input.slice(part.confirmed, from) + } + // Read from the start of the input, so it holds whichever part the cut fell in. + const fence = from === input.length + ? undefined + : fenceAt(scanLines(input.slice(0, from), undefined, false), undefined, from) + return new ChatMarkdownError({ + posted, + remaining: input.slice(from), + ...(fence === undefined ? {} : { remainingFence: fence.line }), + ...(uncertain === "" ? {} : { uncertain }), + cause + }) + } + + const consume: Effect.Effect = options.deltas === undefined + ? Effect.void + : Effect.flatMap( + Effect.exit(Stream.runForEach(options.deltas, (chunk) => + Effect.sync(() => { + input += chunk + if (overflowStart !== undefined) overflowBytes += utf8Length(chunk) + latch.openUnsafe() + }))), + (exit) => + Effect.sync(() => { + upstream = exit + latch.openUnsafe() + if (Exit.isFailure(exit)) upstreamFailed.openUnsafe() + }) + ) + + return Effect.gen(function*() { + const fiber = yield* Effect.forkChild(consume) + // A failed upstream ends the loop at once, so the finish timeout starts even while a write is in flight. + yield* Effect.onInterrupt( + Effect.raceFirst(loop, upstreamFailed.await), + () => Effect.andThen(Fiber.interrupt(fiber), finish) + ) + yield* Fiber.interrupt(fiber) + if (upstream !== undefined && Exit.isFailure(upstream) && (isRunning() || state._tag === "Done")) { + const cause = upstream.cause + state = { _tag: "Running" } + yield* finish + const error = Cause.findErrorOption(cause) + if (Option.isNone(error)) return yield* Effect.failCause(cause as Cause.Cause) + state = { _tag: "Failed", cause: { _tag: "Upstream", error: error.value } } + } else if (current.closing !== undefined || inflight !== undefined) { + // The reply failed already, and the race cut off its stop or write. The finish sends only that stop, + // within the timeout, since a failed reply writes no more text, and keeps the error it failed with. + yield* finish + } + yield* Effect.annotateCurrentSpan("chat.markdown.transport", renderedNatively ? "native" : current.transport) + if (state._tag === "Failed") return yield* Effect.fail(toError(state.cause)) + return { parts, stopped: state._tag === "Done" && state.stopped } + }).pipe(Effect.ensuring(Scope.close(writes, Exit.void))) + }) + +// ----------------------------------------------------------------------------- +// the calls +// ----------------------------------------------------------------------------- + +/** + * Posts Markdown of any length, split into as many messages as it needs. + * + * **Details** + * + * - A part ends at a block boundary: after a blank line or a closed code + * fence. A block too long for one message is cut at a line, closing the code + * fence it is in and reopening it in the next part. Length is measured with + * the adapter's `measure`. + * - Each part is posted once and never edited. Parts are paced by + * `minWriteInterval`. + * - `files` go with the last part, or with the overflow file when there is + * one. Blank text after the last cut stays with the last part. + * - With `unattachedNote`, a post with files that fails `denied` with + * detail `file_refused` is tried once more without them, ending in the note, + * or followed by the note when it does not fit. Any failure of that post + * fails the call. Without `unattachedNote` the first failure fails the + * call. If the overflow file is refused too, the remaining text is split + * into messages beyond `maxParts`, without files and ending in the note. + * An `uncertain` failure records the part in `uncertain` and posts no note. + * - `actions` go with the last part too, which is cut so that + * `measure(part, actions) <= limits.messageLength`. The parts before it + * are filled without room for them. When not even the text's last grapheme + * cluster fits beside the actions, the files and the actions go in a + * message of their own after the text. Either can add a part, which counts + * against `maxParts`. When `measure("", actions)` is already over, the + * call fails `TooLarge` before anything is posted. + * - An unattached note posted on its own takes the actions. When they don't + * fit beside it, they follow it in a message of their own, which counts + * against `maxParts`. + * - A `TooLarge` without detail `file_refused` fails the call, even with + * `subject: "files"` or text that measured as fitting. Fallback requires + * `denied` plus `file_refused`; `subject` never controls it. + * - An empty input posts nothing and succeeds with no parts, unless there are + * files or actions, which then go out in a message of their own. + * + * **Gotchas** + * + * A failure in the middle leaves the parts already posted in place. The error + * says what was posted and what was not. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const post = ( + thread: ChatThread, + markdown: string, + options?: Options & { + readonly files?: ReadonlyArray | undefined + readonly actions?: ReadonlyArray | undefined + readonly unattachedNote?: ((files: ReadonlyArray) => string) | undefined + } +): Effect.Effect => + Effect.gen(function*() { + const chat = yield* Chat.Chat + const failed = (error: ChatError) => + new ChatMarkdownError({ posted: [], remaining: markdown, cause: { _tag: "Chat", error } }) + const descriptor = yield* Effect.mapError(chat.descriptor(thread.platform), failed) + const actions = options?.actions ?? [] + if (actions.length > 0 && descriptor.measure("", actions) > descriptor.limits.messageLength) { + return yield* Effect.fail(failed( + new ChatError({ + platform: thread.platform, + op: "post", + reason: new TooLarge({ detail: "actions_too_long" }) + }) + )) + } + return yield* reply({ ...options, thread, initial: markdown, descriptor, chat }) + }).pipe(Effect.withSpan("ChatMarkdown.post", { attributes: { "chat.platform": thread.platform } })) + +/** + * Writes a stream of Markdown deltas as one reply, as the text arrives. + * + * **Details** + * + * - With a `recipient` and an adapter that has a native stream, the reply is + * written by native appends. Otherwise, or when the first native start fails + * with `Unsupported`, it posts and then edits. The result and the span say + * which ran. + * - On an adapter without `edit` the reply is buffered instead: each part is + * posted once, when it rolls over or the reply ends, with transport + * `post`. Nothing shows until then. An upstream failure or an interrupt + * posts what was received, as above. + * - Text that outgrows a message rolls over into a new one at a block + * boundary, and each new message counts against `maxParts`. A native stream + * rolls over at the next block boundary past 85% of `limits.streamLength`, + * and rotates once it is 80% of `limits.streamAge` old. Every append leaves + * room to close the code fence it ends in, and a rollover or rotation closes + * it. + * - Writes are paced by `minWriteInterval`, on `Clock`. The next write is + * scheduled only after the previous one returns, and takes all the text that + * arrived meanwhile. + * - An append holds back the unfinished last line, unless inside a code fence. + * An edit closes an open code fence. + * - An intermediate edit that fails `retry` or `uncertain` is dropped, since + * the next edit carries the whole text. A `denied` failure, and any failure + * of a post or of a part's last edit, fails the reply. + * - A native append that fails `retry` is tried again after its + * `retryAfter`, or the write interval, whether or not new text arrived. + * Three in a row, or one `uncertain` failure, stop the stream, edit the part + * to its whole known text, the failed chunk included, and carry on by + * post-then-edit. `TooLarge` starts the next part with the chunk. + * - `Result.stopped` is true when a user clicked stop on the reply. The + * upstream is then interrupted. + * - When the upstream fails or the call is interrupted, what was received is + * written out, with an open code fence closed, within a short timeout, and + * left posted. The timeout starts at once, even while a write is in + * flight. A post, start or append in flight is not cut off by the failure + * or the interrupt, since sending it again could repeat text: the finalizer + * waits for it, then sends what follows, a stop cut off by the interrupt + * included. Only the timeout cuts a write off, and its text is then + * reported as `uncertain`. + * + * **Gotchas** + * + * - There are no files: post them after the result, when it is known whether + * the reply stopped. + * - There are no actions either. Add them after the result with + * `chat.edit(thread, last.messageId, { markdown: last.markdown, actions })`, + * where `last` is the final entry of `Result.parts`. Without `edit` they + * can't be added to the reply: post them in a message of their own with + * `chat.post(thread, { markdown, actions })`. + * - To stop a reply, end `deltas` with `Stream.interruptWhen` or + * `Stream.haltWhen`, so a `Result` still comes back. Interrupting the call + * leaves only the ids collected through `onPart`. + * - Native writes go to the adapter through the `Registry`, so a layer that + * wraps `Chat` does not see them. + * - A native part that is rolled over on `TooLarge` can end inside an open + * code fence, since it can no longer be appended to. The next part reopens + * the fence. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const stream = ( + thread: ChatThread, + deltas: Stream.Stream, + options?: Options & { + readonly recipient?: string | undefined + } +): Effect.Effect, Chat.Chat | Registry | R | R2> => + Effect.gen(function*() { + const chat = yield* Chat.Chat + const registry = yield* Registry + const adapter = yield* Effect.mapError( + registry.get(thread.platform), + (error) => new ChatMarkdownError({ posted: [], remaining: "", cause: { _tag: "Chat", error } }) + ) + const native = adapter.descriptor.capabilities.has("nativeStream") ? adapter.nativeStream : undefined + return yield* reply({ + ...options, + thread, + initial: "", + deltas, + descriptor: adapter.descriptor, + chat, + native + }) + }).pipe(Effect.withSpan("ChatMarkdown.stream", { attributes: { "chat.platform": thread.platform } })) diff --git a/packages/effect/src/chat/ChatMessage.ts b/packages/effect/src/chat/ChatMessage.ts new file mode 100644 index 00000000000..ec96a3df8c3 --- /dev/null +++ b/packages/effect/src/chat/ChatMessage.ts @@ -0,0 +1,213 @@ +/** + * Messages read from a chat platform. + * + * A `ChatMessage` has the same shape on every read path: inbound events, + * `Chat.fetchMessage` and `Chat.history`. Its `text` is the platform's own + * content string, verbatim, and its mentions are data with offsets into that + * text. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Schema from "../Schema.ts" +import { ChatThread } from "./ChatThread.ts" + +/** + * Schema for the author of a message. + * + * **Details** + * + * `name` is a best-effort display name: whatever the payload carries. There + * is no lookup, so inbound messages and history can differ. Other bots' + * messages are delivered with `isBot: true`; `isSelf` marks the bot's own. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Author = Schema.Struct({ + id: Schema.String, + name: Schema.optionalKey(Schema.String), + isBot: Schema.Boolean, + isSelf: Schema.Boolean +}).annotate({ identifier: "ChatMessage.Author" }) + +/** + * The author of a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Author = typeof Author.Type + +/** + * Schema for a mention inside a message's text. + * + * **Details** + * + * `start` and `end` are offsets into `ChatMessage.text`, in UTF-16 code + * units, so `text.slice(start, end)` is the platform's own token for the + * mention. `id` is the platform id, or `here`, `channel` or `everyone` for + * a broadcast. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Mention = Schema.Struct({ + kind: Schema.Literals(["user", "role", "channel", "broadcast"]), + id: Schema.String, + start: Schema.Int, + end: Schema.Int, + isSelf: Schema.Boolean +}).annotate({ identifier: "ChatMessage.Mention" }) + +/** + * A mention inside a message's text. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Mention = typeof Mention.Type + +/** + * Schema for a file attached to a message. + * + * **Gotchas** + * + * An attachment never carries a URL. `Chat.download` resolves the id again. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Attachment = Schema.Struct({ + id: Schema.String, + name: Schema.optionalKey(Schema.String), + contentType: Schema.optionalKey(Schema.String), + size: Schema.optionalKey(Schema.Int) +}).annotate({ identifier: "ChatMessage.Attachment" }) + +/** + * A file attached to a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Attachment = typeof Attachment.Type + +/** + * Schema for the message a message replies to. + * + * **Details** + * + * - `authorId` is there when the platform says who wrote the message + * replied to. Discord leaves it out once that message is deleted. + * - `isSelf` says whether that author is the bot. An adapter sets it on + * every message it reads that has an `authorId`. A write ignores both. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ReplyTo = Schema.Struct({ + messageId: Schema.String, + authorId: Schema.optionalKey(Schema.String), + isSelf: Schema.optionalKey(Schema.Boolean) +}).annotate({ identifier: "ChatMessage.ReplyTo" }) + +/** + * The message a message replies to. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ReplyTo = typeof ReplyTo.Type + +/** + * Schema for a message read from a chat platform. + * + * **Details** + * + * - `thread` is the address the message was posted to, the same on every read + * path. It never changes: a message a thread hangs off stays in the parent, + * also once it has replies. `Chat.startThread(message.thread, message.id)` + * is the way from such a message to its thread. + * - `text` is the platform's own content string, verbatim, with mention tokens + * and code fences in place. One exception: Slack renders the bot's own + * messages with `rich_text` blocks from those blocks as Markdown. + * - `raw` is the platform's message payload, as a JSON value. + * + * **Gotchas** + * + * `text` is a different format from outbound Markdown and cannot be posted + * back as is. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ChatMessage = Schema.Struct({ + id: Schema.String, + thread: ChatThread, + author: Author, + /** The platform's content verbatim, except Slack renders the bot's own rich_text blocks as Markdown. */ + text: Schema.String, + mentions: Schema.Array(Mention), + replyTo: Schema.optionalKey(ReplyTo), + attachments: Schema.Array(Attachment), + timestamp: Schema.DateTimeUtc, + raw: Schema.Json +}).annotate({ identifier: "ChatMessage" }) + +/** + * A message read from a chat platform. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ChatMessage = typeof ChatMessage.Type + +/** + * Returns whether the message mentions the bot. + * + * **Gotchas** + * + * A reply to the bot is not a mention. Whether it counts as one is the + * consumer's policy. + * + * @see {@link repliesToSelf} for a reply to one of the bot's messages + * + * @stability unstable + * @category predicates + * @since 4.1.5 + */ +export const mentionsSelf = (message: ChatMessage): boolean => message.mentions.some((mention) => mention.isSelf) + +/** + * Returns whether the message replies to one of the bot's own messages. + * + * **When to use** + * + * Use with `mentionsSelf` when a reply to the bot should count as addressed + * to it, whether or not the reply pings. + * + * **Gotchas** + * + * - It reads `replyTo.isSelf`, so it is false when the platform does not say + * who wrote the message replied to. + * - A message in a Slack thread is not a reply to the thread's first + * message: Slack messages carry no `replyTo`. + * + * @see {@link mentionsSelf} for a mention of the bot + * + * @stability unstable + * @category predicates + * @since 4.1.5 + */ +export const repliesToSelf = (message: ChatMessage): boolean => message.replyTo?.isSelf === true diff --git a/packages/effect/src/chat/ChatTest.ts b/packages/effect/src/chat/ChatTest.ts new file mode 100644 index 00000000000..9bb4075ae6f --- /dev/null +++ b/packages/effect/src/chat/ChatTest.ts @@ -0,0 +1,2660 @@ +/** + * Testing tools for chat adapters and their consumers. + * + * `layer` builds a whole chat in memory: `Chat`, its registry and one fake + * adapter with configurable capabilities and fault injection. `conformance` + * is the suite of rules every adapter obeys, as plain Effects keyed by rule + * name. `checkMessage`, `checkEvent` and `checkInteraction` assert the + * invariants every message, event and interaction holds, for fixture tests + * and live runs. + * + * **Details** + * + * The fake adapter's mention syntax is `<@id>` for a user, `<@&id>` for a + * role, `<#id>` for a channel and `` for a broadcast. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Clock from "../Clock.ts" +import * as Context from "../Context.ts" +import * as DateTime from "../DateTime.ts" +import * as Duration from "../Duration.ts" +import * as Effect from "../Effect.ts" +import * as Exit from "../Exit.ts" +import * as Layer from "../Layer.ts" +import * as Option from "../Option.ts" +import * as Schema from "../Schema.ts" +import * as Stream from "../Stream.ts" +import * as Chat from "./Chat.ts" +import type { ChatAdapter, Descriptor, File, NativeStream, Posted } from "./ChatAdapter.ts" +import { checkMessageLength, Registry, retrySafe } from "./ChatAdapter.ts" +import type { Capability, ChatErrorReason } from "./ChatError.ts" +import { + ChatError, + fileRefused, + Forbidden, + interactionExpired, + InvalidRequest, + isChatError, + NotFound, + RateLimited, + TooLarge, + Transport, + Unsupported +} from "./ChatError.ts" +import type { ThreadKind } from "./ChatEvent.ts" +import * as ChatEvent from "./ChatEvent.ts" +import { ChatHandler } from "./ChatHandler.ts" +import type { NotAccepted } from "./ChatHandler.ts" +import * as ChatInteraction from "./ChatInteraction.ts" +import type { Attachment, Mention, ReplyTo } from "./ChatMessage.ts" +import * as ChatMessage from "./ChatMessage.ts" +import * as ChatThread from "./ChatThread.ts" +import type * as ChatUi from "./ChatUi.ts" + +/** + * Reads a slice of `ChatMessage.text` as one of the platform's mention + * tokens. + * + * **Details** + * + * The core does not know any platform's syntax, so the caller supplies it. A + * slice that is not a mention token returns `None`. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ParseMention = (slice: string) => Option.Option<{ + readonly kind: Mention["kind"] + readonly id: string +}> + +/** + * One call the fake adapter received, in order. Every attempt is a call, so a + * retried request shows up once per attempt. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export interface Call { + readonly op: string + readonly thread: ChatThread.ChatThread + readonly messageId?: string | undefined + readonly markdown?: string | undefined + readonly files?: ReadonlyArray | undefined + readonly outcome: "ok" | ChatErrorReason["_tag"] +} + +/** + * Error for a click on an element the message does not carry, or on a + * message that is gone. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class NoSuchElement extends Schema.TaggedError("effect/chat/ChatTest/NoSuchElement")( + "NoSuchElement", + { messageId: Schema.String, id: Schema.String } +) {} + +/** + * Service that controls the fake adapter built by `layer`. + * + * **Details** + * + * - `makeThread` creates a fresh address: a channel by default, a direct + * conversation, or a thread under a user's message in a fresh channel. + * - `userPosts` puts another identity's message in a thread and returns its + * event without delivering it. `deliver` delivers an event the way an + * adapter does, by calling the `ChatHandler` service. + * - `messages` is what a thread shows now, oldest first, and `calls` is every + * call the adapter received. + * - `failNext` makes the next `times` calls of `op` fail with `reason`. + * `landed` makes the write happen anyway, which is what `uncertain` means. + * The native stream's ops are `nativeStream.start`, `nativeStream.append` + * and `nativeStream.stop`. + * - `refuseFiles` makes the next `times` posts with files fail `denied` with + * detail `file_refused`, storing nothing. Text-only posts do not consume it. + * - `clickStop` is a user clicking stop on a native stream. It closes + * nothing, as on Slack: the stream keeps taking text, and + * `NativeStream.stop` then reports `stopped: true`. + * - `closeStream` closes a native stream from outside, so the next append + * gives `closed`, or `stopped` after a click. + * - `click` builds the click on element `id` of a message from what the + * fake stored at `post` or `edit`, without delivering it. For a select, + * `option` is the encoded value of the option to choose, the first by + * default. It fails `NoSuchElement` when the message has no such element, + * or no such option, or is gone. + * - `command` builds a command typed at `thread`, without delivering it. + * `text` defaults to the empty string. The fake has no registration, so + * any id can be typed. + * - `submit` builds the submit of a modal. It takes the click or the command + * that opened it, because an `OpenModal` reply does not say where it was + * opened from and a `ModalSubmitted` has to. A command gives a submit + * with no `messageId`. `values` are by field id, still encoded, and an + * empty one is left out, as for an optional field left empty. + * - `interact` delivers an interaction the way an adapter does: through + * `ChatInteraction.handle` with the layer's `interactionDeadline`, then + * the fake's own checks on an `OpenModal` (the `modals` capability, + * `limits.modalMetadataLength` against the modal's id plus its encoded + * metadata, and `limits.modalFields`), calling `Handler.refused` when one + * fails. It succeeds with the handler's reply as it returned it, or + * `Late`. An `OpenModal` shows in `calls` as op `openModal`. + * - For a command, `interact` runs the checks of `post` on a `Respond` + * (the element capabilities, `limits.messageLength` and the element + * limits), and refuses a private `Respond` or `Defer` without + * `ephemeral`. A `Respond` shows in `calls` as op `respond`, and a + * `followUp` as op `followUp`, each with the command's thread and the + * Markdown. Neither is stored as a message. + * - The fake's `followUp` succeeds once for a `Defer` that `interact` + * returned, inside `limits.followUpAge` on `Clock`. Later, a second time, + * or for a deferral the fake never saw, it fails `NotFound` with detail + * `interaction_expired`. Failing the second call is stricter than either + * platform, to catch a handler that relies on more than one: Slack posts + * another message and Discord replaces the first. + * + * @stability unstable + * @category services + * @since 4.1.5 + */ +export class ChatTest extends Context.Service Effect.Effect + readonly userPosts: (thread: ChatThread.ChatThread, message: { + readonly text: string + readonly authorId?: string | undefined + readonly isBot?: boolean | undefined + readonly mentionsSelf?: boolean | undefined + readonly replyTo?: ReplyTo | undefined + readonly files?: ReadonlyArray | undefined + }) => Effect.Effect + readonly deliver: (event: ChatEvent.ChatEvent) => Effect.Effect + readonly messages: (thread: ChatThread.ChatThread) => Effect.Effect> + readonly calls: Effect.Effect> + readonly failNext: (op: string, reason: ChatErrorReason, options?: { + readonly times?: number | undefined + readonly landed?: boolean | undefined + }) => Effect.Effect + readonly refuseFiles: (options?: { readonly times?: number | undefined }) => Effect.Effect + readonly clickStop: (messageId: string) => Effect.Effect + readonly closeStream: (messageId: string) => Effect.Effect + readonly click: (messageId: string, id: string, options?: { + readonly userId?: string | undefined + readonly option?: string | undefined + }) => Effect.Effect + readonly command: (thread: ChatThread.ChatThread, id: string, options?: { + readonly text?: string | undefined + readonly userId?: string | undefined + }) => Effect.Effect + readonly submit: ( + from: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatInteraction.CommandInvoked, + opened: ChatInteraction.OpenModal, + values: Readonly>, + options?: { readonly userId?: string | undefined } + ) => Effect.Effect + readonly interact: { + (event: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged): Effect.Effect< + ChatInteraction.ClickReply | ChatInteraction.Late, + never, + ChatInteraction.Handler + > + (event: ChatInteraction.ModalSubmitted): Effect.Effect< + ChatInteraction.SubmitReply | ChatInteraction.Late, + never, + ChatInteraction.Handler + > + (event: ChatInteraction.CommandInvoked): Effect.Effect< + ChatInteraction.CommandReply | ChatInteraction.Late, + never, + ChatInteraction.Handler + > + } +}>()("effect/chat/ChatTest") {} + +// ----------------------------------------------------------------------------- +// fake adapter +// ----------------------------------------------------------------------------- + +const allCapabilities: ReadonlyArray = [ + "edit", + "delete", + "fetchMessage", + "history", + "download", + "files", + "replyTo", + "startThread", + "getThread", + "opener", + "getUser", + "nativeStream", + "buttons", + "selects", + "modals", + "modalErrors", + "commands", + "ephemeral" +] + +/** Counts code points. */ +const codePoints = (markdown: string): number => { + let count = 0 + for (const _ of markdown) count++ + return count +} + +interface StoredStream { + readonly startedAt: number + open: boolean + clicked: boolean +} + +/** An append holds back an unclosed `<` tail, as Slack does, up to this many characters. */ +const heldBound = 100 + +/** Every lone surrogate as U+FFFD, as `String.prototype.toWellFormed` gives. */ +const wellFormed = (text: string): string => + text.replace(/[\ud800-\udbff](?![\udc00-\udfff])|(? + readonly replyTo?: ReplyTo | undefined + readonly attachments: ReadonlyArray<{ readonly attachment: Attachment; readonly content: Uint8Array }> + readonly timestamp: DateTime.Utc + actions: ReadonlyArray +} + +interface Fault { + readonly reason: ChatErrorReason + remaining: number + readonly landed: boolean +} + +const mentionPattern = /<(@&|@|#|!)([^<>\s]+)>/g + +const mentionKinds: Record = { + "@": "user", + "@&": "role", + "#": "channel", + "!": "broadcast" +} + +const parseMentions = (text: string, selfId: string): ReadonlyArray => + Array.from(text.matchAll(mentionPattern), (match) => { + const kind = mentionKinds[match[1]] + return { + kind, + id: match[2], + start: match.index, + end: match.index + match[0].length, + isSelf: kind === "user" && match[2] === selfId + } + }) + +const downloadChunkSize = 1024 + +const makeFake = (options: { + readonly platform: string + readonly selfId: string + readonly capabilities: ReadonlyArray + readonly limits: Partial + readonly measure: Descriptor["measure"] + readonly interactionDeadline: Duration.Duration +}) => { + const { platform, selfId } = options + const capabilities = new Set(options.capabilities) + const messageLength = options.limits.messageLength ?? 4000 + const hasActions = capabilities.has("buttons") || capabilities.has("selects") + const descriptor: Descriptor = { + platform, + capabilities, + measure: options.measure, + limits: { + messageLength, + filesPerMessage: 10, + uploadBytes: 10 * 1024 * 1024, + minWriteInterval: Duration.zero, + ...(capabilities.has("nativeStream") ? { streamLength: messageLength, streamAge: Duration.minutes(10) } : {}), + ...(hasActions ? { actionsPerMessage: 25, actionDataLength: 100 } : {}), + ...(capabilities.has("modals") ? { modalMetadataLength: 100, modalFields: 5 } : {}), + ...(capabilities.has("commands") ? { followUpAge: Duration.minutes(15) } : {}), + ...options.limits + } + } + const streams = new Map() + const threads = new Map() + const messages = new Map() + const order: Array = [] + const threadUnder = new Map() + const users = new Map([[selfId, { isBot: true }]]) + const calls: Array = [] + /** Deferrals `interact` returned, by the command's raw id, with when they were made. */ + const deferrals = new Map() + let refusedFiles = 0 + const faults = new Map>() + let counter = 0 + const nextId = (prefix: string) => prefix + String(++counter) + + const chatError = (op: string, reason: ChatErrorReason) => new ChatError({ platform, op, reason }) + const fail = (op: string, reason: ChatErrorReason) => Effect.fail(chatError(op, reason)) + + const toMessage = (message: StoredMessage): ChatMessage.ChatMessage => ({ + id: message.id, + thread: message.thread, + author: { + id: message.authorId, + name: message.isSelf ? "test-bot" : message.authorId, + isBot: message.isBot, + isSelf: message.isSelf + }, + text: message.text, + mentions: message.mentions, + ...(message.replyTo === undefined ? {} : { + replyTo: message.replyTo.authorId === undefined + ? message.replyTo + : { ...message.replyTo, isSelf: message.replyTo.authorId === selfId } + }), + attachments: message.attachments.map((stored) => stored.attachment), + timestamp: message.timestamp, + raw: { + type: "message", + id: message.id, + channel: message.thread.id, + user: message.authorId, + text: message.text + } + }) + + const findThread = (op: string, thread: ChatThread.ChatThread) => + Effect.suspend(() => { + const stored = threads.get(thread.id) + return stored === undefined ? fail(op, new NotFound({ detail: "thread_not_found" })) : Effect.succeed(stored) + }) + + const listMessages = (stored: StoredThread): Array => { + const list: Array = [] + if (stored.openerId !== undefined) { + const opener = messages.get(stored.openerId) + if (opener !== undefined) list.push(opener) + } + for (const id of order) { + const message = messages.get(id) + if (message !== undefined && ChatThread.equals(message.thread, stored.thread)) list.push(message) + } + return list + } + + const findMessage = (op: string, thread: ChatThread.ChatThread, messageId: string) => + Effect.flatMap(findThread(op, thread), (stored) => { + const message = listMessages(stored).find((message) => message.id === messageId) + return message === undefined + ? fail(op, new NotFound({ detail: "message_not_found" })) + : Effect.succeed(message) + }) + + const store = (message: Omit) => + Effect.map(DateTime.now, (timestamp) => { + const stored: StoredMessage = { ...message, id: nextId("m"), timestamp } + messages.set(stored.id, stored) + order.push(stored.id) + return stored + }) + + const storeFiles = (files: ReadonlyArray | undefined) => + (files ?? []).map((file) => ({ + attachment: { + id: nextId("f"), + name: file.name, + contentType: file.contentType, + size: file.content.length + }, + content: file.content + })) + + const createThread = ( + op: string, + thread: ChatThread.ChatThread, + messageId: string, + name: string | undefined + ) => + Effect.flatMap(findMessage(op, thread, messageId), (message) => { + const existing = threadUnder.get(message.id) + if (existing !== undefined) return Effect.succeed(existing) + const parent = threads.get(message.thread.id)! + if (parent.kind === "thread" || !ChatThread.equals(message.thread, thread)) { + return fail(op, new InvalidRequest({ detail: "cannot_thread_reply" })) + } + return Effect.map(DateTime.now, (createdAt) => { + const created = ChatThread.make(platform, thread.id + "/" + message.id) + threads.set(created.id, { + thread: created, + kind: "thread", + name, + parent: thread, + openerId: message.id, + createdAt + }) + threadUnder.set(message.id, created) + return created + }) + }) + + /** The capabilities a message's elements need. */ + const actionNeeds = (actions: ReadonlyArray | undefined): Array => { + const needs: Array = [] + if (actions?.some((action) => action._tag !== "Select")) needs.push("buttons") + if (actions?.some((action) => action._tag === "Select")) needs.push("selects") + return needs + } + + /** The element limits, counted in code points, as both adapters count them. */ + const checkActions = (op: string, actions: ReadonlyArray) => + Effect.suspend(() => { + const { actionDataLength, actionsPerMessage } = descriptor.limits + if (actionsPerMessage !== undefined && actions.length > actionsPerMessage) { + return fail(op, new TooLarge({ detail: "too_many_actions" })) + } + const data = actions.flatMap((action) => + action._tag === "Button" + ? [codePoints(action.id) + codePoints(action.value ?? "")] + : action._tag === "Select" + ? action.options.map((option) => codePoints(action.id) + codePoints(option.value)) + : [] + ) + if (actionDataLength !== undefined && data.some((length) => length > actionDataLength)) { + return fail(op, new TooLarge({ detail: "action_data_too_long" })) + } + return Effect.void + }) + + /** Why a modal would not open, if it would not. */ + const modalRefusal = (modal: ChatUi.Modal): ChatError | undefined => { + const { modalFields, modalMetadataLength } = descriptor.limits + if (!capabilities.has("modals")) return chatError("openModal", new Unsupported({ capability: "modals" })) + if ( + modalMetadataLength !== undefined && + codePoints(modal.id) + codePoints(modal.metadata ?? "") > modalMetadataLength + ) { + return chatError("openModal", new TooLarge({ detail: "modal_metadata_too_long" })) + } + if (modalFields !== undefined && modal.elements.length > modalFields) { + return chatError("openModal", new TooLarge({ detail: "too_many_fields" })) + } + return undefined + } + + /** The capabilities a command's answer needs. */ + const answerNeeds = ( + visibility: ChatInteraction.Visibility, + actions?: ReadonlyArray | undefined + ): Array => [...(visibility === "ephemeral" ? ["ephemeral" as const] : []), ...actionNeeds(actions)] + + /** The id the fake gave a command it built, which keys its deferral. */ + const commandKey = (command: ChatInteraction.CommandInvoked): string | undefined => { + const raw = command.raw + const id = typeof raw === "object" && raw !== null ? (raw as { readonly id?: unknown }).id : undefined + return typeof id === "string" ? id : undefined + } + + const takeFault = (op: string): Fault | undefined => { + const queue = faults.get(op) + const fault = queue?.[0] + if (fault === undefined) return undefined + fault.remaining-- + if (fault.remaining <= 0) queue!.shift() + return fault + } + + const attempt = ( + call: Omit, + needs: ReadonlyArray, + body: Effect.Effect, + retries: boolean = true + ): Effect.Effect => + (retries ? retrySafe : (effect: X) => effect)(Effect.suspend(() => { + const record = (outcome: Call["outcome"]) => calls.push({ ...call, outcome }) + const missing = needs.find((capability) => !descriptor.capabilities.has(capability)) + if (missing !== undefined) { + record("Unsupported") + return fail(call.op, new Unsupported({ capability: missing })) + } + if (call.op === "post" && (call.files?.length ?? 0) > 0 && refusedFiles > 0) { + refusedFiles-- + record("Forbidden") + return fail(call.op, new Forbidden({ detail: fileRefused })) + } + const fault = takeFault(call.op) + if (fault !== undefined) { + record(fault.reason._tag) + const failure = fail(call.op, fault.reason) + return fault.landed ? Effect.flatMap(Effect.exit(body), () => failure) : failure + } + return body.pipe( + Effect.tap(() => Effect.sync(() => record("ok"))), + Effect.tapError((error) => Effect.sync(() => record(error.reason._tag))) + ) + })) + + /** Whether a native stream is open on the message, closing it once it is past `limits.streamAge`. */ + const isOpen = (messageId: string) => + Effect.map(Clock.currentTimeMillis, (now) => { + const stream = streams.get(messageId) + if (stream === undefined || !stream.open) return false + const age = descriptor.limits.streamAge + if (age !== undefined && now - stream.startedAt >= Duration.toMillis(age)) stream.open = false + return stream.open + }) + + const streamLength = () => descriptor.limits.streamLength ?? descriptor.limits.messageLength + + const nativeStream: NativeStream = { + start: (thread, content) => + attempt( + { op: "nativeStream.start", thread, markdown: content.markdown }, + content.replyTo === undefined ? ["nativeStream"] : ["nativeStream", "replyTo"], + Effect.gen(function*() { + yield* findThread("nativeStream.start", thread) + yield* checkMessageLength(descriptor, "nativeStream.start", content.markdown) + if (descriptor.measure(content.markdown) > streamLength()) { + return yield* fail("nativeStream.start", new TooLarge({ subject: "text", detail: "msg_too_long" })) + } + let replyTo: ReplyTo | undefined + if (content.replyTo !== undefined) { + const target = yield* findMessage("nativeStream.start", thread, content.replyTo.messageId) + replyTo = { messageId: target.id, authorId: target.authorId } + } + const stored = yield* store({ + thread, + authorId: selfId, + isBot: true, + isSelf: true, + text: wellFormed(content.markdown), + mentions: [], + replyTo, + attachments: [], + actions: [] + }) + streams.set(stored.id, { startedAt: yield* Clock.currentTimeMillis, open: true, clicked: false }) + return { id: stored.id, thread, timestamp: stored.timestamp } satisfies Posted + }) + ), + append: (thread, messageId, markdown, appendOptions) => + attempt( + { op: "nativeStream.append", thread, messageId, markdown }, + ["nativeStream"], + Effect.gen(function*() { + const message = yield* findMessage("nativeStream.append", thread, messageId) + const stream = streams.get(message.id) + if (stream === undefined || !(yield* isOpen(message.id))) { + return { state: stream?.clicked === true ? "stopped" as const : "closed" as const, held: 0 } + } + let held = 0 + const flush = appendOptions?.flush === true + const open = markdown.lastIndexOf("<") + if (!flush && open !== -1 && !markdown.includes(">", open)) { + held = markdown.length - open + if (held > heldBound) held = 0 + } + // A trailing high surrogate waits for its other half. + if (!flush && held === 0 && /[\ud800-\udbff]$/.test(markdown)) held = 1 + const sent = wellFormed(markdown.slice(0, markdown.length - held)) + if (descriptor.measure(message.text + sent) > streamLength()) { + return yield* fail("nativeStream.append", new TooLarge({ subject: "text", detail: "msg_too_long" })) + } + message.text += sent + return { state: "open" as const, held } + }), + false + ), + stop: (thread, messageId) => + attempt( + { op: "nativeStream.stop", thread, messageId }, + ["nativeStream"], + Effect.gen(function*() { + const message = yield* findMessage("nativeStream.stop", thread, messageId) + const stream = streams.get(message.id) + if (stream === undefined || !(yield* isOpen(message.id))) return { stopped: false } + stream.open = false + return { stopped: stream.clicked } + }) + ) + } + + const streamFor = (op: string, messageId: string) => + Effect.suspend(() => { + const stream = streams.get(messageId) + return stream === undefined + ? Effect.die(new Error("ChatTest." + op + ": no native stream " + messageId)) + : Effect.succeed(stream) + }) + + const adapter: ChatAdapter = { + descriptor, + ...(capabilities.has("nativeStream") ? { nativeStream } : {}), + post: (thread, content) => { + const files = content.files ?? [] + const needs: Array = [] + if (files.length > 0) needs.push("files") + if (content.replyTo !== undefined) needs.push("replyTo") + needs.push(...actionNeeds(content.actions)) + return attempt( + { op: "post", thread, markdown: content.markdown, files: content.files }, + needs, + Effect.gen(function*() { + yield* findThread("post", thread) + yield* checkMessageLength(descriptor, "post", content.markdown, content.actions) + yield* checkActions("post", content.actions ?? []) + if ( + files.length > descriptor.limits.filesPerMessage || + files.some((file) => file.content.length > descriptor.limits.uploadBytes) + ) { + return yield* fail("post", new TooLarge({ subject: "files", detail: fileRefused })) + } + let replyTo: ReplyTo | undefined + if (content.replyTo !== undefined) { + const target = yield* findMessage("post", thread, content.replyTo.messageId) + replyTo = { messageId: target.id, authorId: target.authorId } + } + const stored = yield* store({ + thread, + authorId: selfId, + isBot: true, + isSelf: true, + text: content.markdown, + mentions: [], + replyTo, + attachments: storeFiles(files), + actions: content.actions ?? [] + }) + return { id: stored.id, thread, timestamp: stored.timestamp } satisfies Posted + }) + ) + }, + edit: (thread, messageId, content) => + attempt( + { op: "edit", thread, messageId, markdown: content.markdown }, + ["edit", ...actionNeeds(content.actions)], + Effect.gen(function*() { + const message = yield* findMessage("edit", thread, messageId) + if (!message.isSelf) return yield* fail("edit", new Forbidden({ detail: "cant_update_message" })) + if (yield* isOpen(message.id)) { + return yield* fail("edit", new InvalidRequest({ detail: "streaming_state_conflict" })) + } + yield* checkMessageLength(descriptor, "edit", content.markdown, content.actions) + yield* checkActions("edit", content.actions ?? []) + message.text = content.markdown + message.mentions = [] + message.actions = content.actions ?? [] + }) + ), + delete: (thread, messageId) => + attempt( + { op: "delete", thread, messageId }, + ["delete"], + Effect.gen(function*() { + const message = yield* findMessage("delete", thread, messageId) + if (!message.isSelf) return yield* fail("delete", new Forbidden({ detail: "cant_delete_message" })) + if (yield* isOpen(message.id)) { + return yield* fail("delete", new InvalidRequest({ detail: "cant_delete_message" })) + } + messages.delete(message.id) + }) + ), + fetchMessage: (thread, messageId) => + attempt( + { op: "fetchMessage", thread, messageId }, + ["fetchMessage"], + Effect.map(findMessage("fetchMessage", thread, messageId), toMessage) + ), + history: (thread, options) => + Stream.unwrap(attempt( + { op: "history", thread, messageId: options.from }, + ["history"], + Effect.gen(function*() { + const stored = yield* findThread("history", thread) + const list = listMessages(stored).map(toMessage) + if (options.direction === "backward") list.reverse() + if (options.from === undefined) return Stream.fromIterable(list) + const index = list.findIndex((message) => message.id === options.from) + if (index === -1) return yield* fail("history", new NotFound({ detail: "message_not_found" })) + return Stream.fromIterable(list.slice(index + 1)) + }) + )), + download: (thread, attachment, options) => + Stream.unwrap(attempt( + { op: "download", thread }, + ["download"], + Effect.suspend(() => { + let content: Uint8Array | undefined + for (const message of messages.values()) { + const found = message.attachments.find((stored) => stored.attachment.id === attachment.id) + if (found !== undefined) content = found.content + } + if (content === undefined) return fail("download", new NotFound({ detail: "file_not_found" })) + const chunks: Array = [] + for (let offset = 0; offset < content.length && offset < options.maxBytes; offset += downloadChunkSize) { + const end = Math.min(offset + downloadChunkSize, content.length) + if (end > options.maxBytes) break + chunks.push(content.slice(offset, end)) + } + const tooLarge = content.length > options.maxBytes + ? Stream.fail(chatError("download", new TooLarge({ detail: "max_bytes" }))) + : Stream.empty + return Effect.succeed(Stream.concat(Stream.fromIterable(chunks), tooLarge)) + }) + )), + startThread: (thread, messageId, startOptions) => + attempt( + { op: "startThread", thread, messageId }, + ["startThread"], + createThread("startThread", thread, messageId, startOptions?.name) + ), + getThread: (thread) => + attempt( + { op: "getThread", thread }, + ["getThread"], + Effect.map(findThread("getThread", thread), (stored) => ({ + thread: stored.thread, + kind: stored.kind, + name: stored.name, + parent: stored.parent, + createdAt: stored.createdAt, + raw: { id: stored.thread.id, kind: stored.kind } + })) + ), + opener: (thread) => + attempt( + { op: "opener", thread }, + ["opener"], + Effect.map(findThread("opener", thread), (stored) => { + const opener = stored.openerId === undefined ? undefined : messages.get(stored.openerId) + return opener === undefined ? Option.none() : Option.some(toMessage(opener)) + }) + ), + getUser: (id, userOptions) => + attempt( + { op: "getUser", thread: userOptions?.in ?? ChatThread.make(platform, "") }, + ["getUser"], + Effect.suspend(() => { + const user = users.get(id) + return user === undefined + ? fail("getUser", new NotFound({ detail: "user_not_found" })) + : Effect.succeed({ + id, + name: id === selfId ? "test-bot" : id, + isBot: user.isBot, + isSelf: id === selfId, + raw: { id } + }) + }) + ), + followUp: (deferred, content) => + attempt( + { op: "followUp", thread: deferred.command.thread, markdown: content.markdown }, + ["commands", ...answerNeeds(deferred.visibility, content.actions)], + Effect.gen(function*() { + const key = commandKey(deferred.command) + const stored = key === undefined ? undefined : deferrals.get(key) + const age = descriptor.limits.followUpAge + const now = yield* Clock.currentTimeMillis + if ( + stored === undefined || stored.used || + (age !== undefined && now - stored.at >= Duration.toMillis(age)) + ) { + return yield* fail("followUp", new NotFound({ detail: interactionExpired })) + } + yield* checkMessageLength(descriptor, "followUp", content.markdown, content.actions) + yield* checkActions("followUp", content.actions ?? []) + stored.used = true + }), + false + ) + } + + const addThread = (kind: "channel" | "direct") => + Effect.map(DateTime.now, (createdAt) => { + const thread = ChatThread.make(platform, nextId(kind === "channel" ? "c" : "d")) + threads.set(thread.id, { thread, kind, createdAt }) + return thread + }) + + const userPosts: ChatTest["Service"]["userPosts"] = Effect.fnUntraced(function*(thread, message) { + const stored = threads.get(thread.id) + if (stored === undefined) return yield* Effect.die(new Error("ChatTest.userPosts: unknown thread")) + const authorId = message.authorId ?? "user" + const isBot = message.isBot ?? false + users.set(authorId, { isBot }) + const text = message.mentionsSelf === true ? "<@" + selfId + "> " + message.text : message.text + const replyTarget = message.replyTo === undefined ? undefined : messages.get(message.replyTo.messageId) + const created = yield* store({ + thread, + authorId, + isBot, + isSelf: false, + text, + mentions: parseMentions(text, selfId), + // The author is read from the stored message, as a platform does, not from the caller. + replyTo: message.replyTo === undefined ? undefined : { + messageId: message.replyTo.messageId, + ...(replyTarget === undefined ? {} : { authorId: replyTarget.authorId }) + }, + attachments: storeFiles(message.files), + actions: [] + }) + return ChatEvent.MessageCreated.make({ + thread, + kind: stored.kind, + message: toMessage(created), + raw: { type: "event_callback", event_id: nextId("e") } + }) + }) + + const service = ChatTest.of({ + platform, + selfId, + makeThread: (kind = "channel") => { + if (kind !== "thread") return addThread(kind) + return Effect.gen(function*() { + const channel = yield* addThread("channel") + const event = yield* userPosts(channel, { text: "thread opener" }) + const message = (event as ChatEvent.MessageCreated).message + return yield* Effect.orDie(createThread("makeThread", channel, message.id, undefined)) + }) + }, + userPosts, + deliver: (event) => Effect.flatMap(ChatHandler, (handler) => handler.handle(event)), + messages: (thread) => + Effect.sync(() => { + const stored = threads.get(thread.id) + return stored === undefined ? [] : listMessages(stored).map(toMessage) + }), + calls: Effect.sync(() => calls.slice()), + refuseFiles: (options) => + Effect.sync(() => { + refusedFiles += options?.times ?? 1 + }), + failNext: (op, reason, failOptions) => + Effect.sync(() => { + const queue = faults.get(op) ?? [] + queue.push({ reason, remaining: failOptions?.times ?? 1, landed: failOptions?.landed ?? false }) + faults.set(op, queue) + }), + clickStop: (messageId) => Effect.map(streamFor("clickStop", messageId), (stream) => void (stream.clicked = true)), + closeStream: (messageId) => Effect.map(streamFor("closeStream", messageId), (stream) => void (stream.open = false)), + click: (messageId, id, clickOptions) => + Effect.suspend((): Effect.Effect< + ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged, + NoSuchElement + > => { + const message = messages.get(messageId) + const element = message?.actions.find((action) => action._tag !== "LinkButton" && action.id === id) + if (message === undefined || element === undefined || element._tag === "LinkButton") { + return Effect.fail(new NoSuchElement({ messageId, id })) + } + const origin = { + id, + thread: message.thread, + messageId, + userId: clickOptions?.userId ?? "user", + raw: { type: "interaction", id: nextId("i"), message: messageId, element: id } + } + if (element._tag === "Button") { + return Effect.succeed(ChatInteraction.ButtonClicked.make({ + ...origin, + ...(element.value === undefined ? {} : { value: element.value }) + })) + } + const option = clickOptions?.option === undefined + ? element.options[0] + : element.options.find((option) => option.value === clickOptions.option) + return option === undefined + ? Effect.fail(new NoSuchElement({ messageId, id })) + : Effect.succeed(ChatInteraction.SelectChanged.make({ ...origin, value: option.value })) + }), + command: (thread, id, commandOptions) => + Effect.sync(() => { + const text = commandOptions?.text ?? "" + return ChatInteraction.CommandInvoked.make({ + id, + text, + thread, + userId: commandOptions?.userId ?? "user", + raw: { type: "command", id: nextId("i"), command: id, text } + }) + }), + submit: (from, opened, values, submitOptions) => + Effect.sync(() => + ChatInteraction.ModalSubmitted.make({ + id: opened.modal.id, + ...(opened.modal.metadata === undefined ? {} : { metadata: opened.modal.metadata }), + values: Object.fromEntries(Object.entries(values).filter(([, value]) => value !== "")), + thread: from.thread, + ...(from._tag === "CommandInvoked" ? {} : { messageId: from.messageId }), + userId: submitOptions?.userId ?? from.userId, + raw: { type: "submission", id: nextId("i"), modal: opened.modal.id } + }) + ), + interact: ((event: ChatInteraction.Interaction) => + Effect.gen(function*() { + const reply = yield* ChatInteraction.handle(event, options.interactionDeadline) + if (event._tag === "ModalSubmitted") return reply + let refusal: ChatError | undefined + switch (reply._tag) { + case "OpenModal": { + refusal = modalRefusal(reply.modal) + calls.push({ + op: "openModal", + thread: event.thread, + ...(event._tag === "CommandInvoked" ? {} : { messageId: event.messageId }), + outcome: refusal === undefined ? "ok" : refusal.reason._tag + }) + break + } + case "Respond": { + const exit = yield* Effect.exit(attempt( + { op: "respond", thread: event.thread, markdown: reply.markdown }, + answerNeeds(reply.visibility, reply.actions), + Effect.andThen( + checkMessageLength(descriptor, "respond", reply.markdown, reply.actions), + checkActions("respond", reply.actions ?? []) + ), + false + )) + refusal = Exit.isFailure(exit) ? Option.getOrUndefined(Exit.findErrorOption(exit)) : undefined + break + } + case "Defer": { + const missing = answerNeeds(reply.visibility).find((capability) => !capabilities.has(capability)) + if (missing !== undefined) { + refusal = chatError("defer", new Unsupported({ capability: missing })) + break + } + const key = event._tag === "CommandInvoked" ? commandKey(event) : undefined + if (key !== undefined) deferrals.set(key, { at: yield* Clock.currentTimeMillis, used: false }) + break + } + } + if (refusal !== undefined) { + const handler = yield* ChatInteraction.Handler + yield* handler.refused(event, refusal) + } + return reply + })) as ChatTest["Service"]["interact"] + }) + + return { adapter, service } +} + +/** + * Creates a whole chat in memory: `Chat`, its registry and one fake adapter. + * + * **When to use** + * + * Use to test code that posts and reads through `Chat`, and to run the + * conformance suite against the fake adapter. + * + * **Details** + * + * - The platform defaults to `test` and the bot's user id to `bot`. + * - Every capability is on by default. + * - `measure` defaults to a count of code points, and the fake checks every + * length with it. + * - The default limits are 4000 per message, 10 files of up to 10 MiB, and no + * write interval. With `nativeStream` on, a stream takes up to + * `messageLength` and lasts 10 minutes. + * - The native stream behaves the way Slack's was found to. An append past + * `limits.streamLength` fails with `TooLarge` and adds nothing. A stream + * past `limits.streamAge`, on `Clock`, is closed. An append without + * `flush` holds back an unclosed `<` tail of up to 100 characters, or a + * trailing high surrogate. Any other lone surrogate is stored as U+FFFD. + * An open stream refuses `edit` and `delete`. + * - With `buttons` or `selects` on, a message takes 25 elements, and an + * element's id plus encoded value 100 code points. With `modals` on, a + * modal's id plus encoded metadata takes 100 code points, and a modal 5 + * fields. Each limit is there only with its capability. + * - `interact` gives a handler 3 seconds by default, on `Clock`. + * - With `commands` on, `limits.followUpAge` is 15 minutes. + * - Two layers merged with different platforms share one `Chat`, so routing + * can be tested. The `ChatTest` service in context is the last one merged. + * + * @stability unstable + * @category layers + * @since 4.1.5 + */ +export const layer = (options?: { + readonly platform?: string | undefined + readonly selfId?: string | undefined + readonly capabilities?: ReadonlyArray | undefined + readonly limits?: Partial | undefined + readonly measure?: Descriptor["measure"] | undefined + readonly interactionDeadline?: Duration.Duration | undefined +}): Layer.Layer => + Layer.effect( + ChatTest, + Effect.gen(function*() { + const registry = yield* Registry + const { adapter, service } = makeFake({ + platform: options?.platform ?? "test", + selfId: options?.selfId ?? "bot", + capabilities: options?.capabilities ?? allCapabilities, + limits: options?.limits ?? {}, + measure: options?.measure ?? codePoints, + interactionDeadline: options?.interactionDeadline ?? Duration.seconds(3) + }) + yield* registry.register(adapter) + return service + }) + ).pipe(Layer.provideMerge(Chat.layer)) + +// ----------------------------------------------------------------------------- +// invariants +// ----------------------------------------------------------------------------- + +/** + * Error for a conformance rule or invariant that did not hold. + * + * **Details** + * + * `rule` names what failed, and `expected` and `actual` say how. A platform + * error behind the failure is in `cause`. + * + * @stability unstable + * @category errors + * @since 4.1.5 + */ +export class ConformanceFailure extends Schema.TaggedError( + "effect/chat/ChatTest/ConformanceFailure" +)("ConformanceFailure", { + rule: Schema.String, + expected: Schema.String, + actual: Schema.String, + cause: Schema.optionalKey(Schema.Defect()) +}) { + override get message(): string { + return this.rule + ": expected " + this.expected + ", got " + this.actual + } +} + +const sameMentions = (a: ReadonlyArray, b: ReadonlyArray): boolean => + a.length === b.length && + a.every((mention, i) => { + const other = b[i] + return mention.kind === other.kind && mention.id === other.id && mention.start === other.start && + mention.end === other.end && mention.isSelf === other.isSelf + }) + +const sameReplyTo = (a: ReplyTo | undefined, b: ReplyTo | undefined): boolean => + a === undefined || b === undefined + ? a === b + : a.messageId === b.messageId && a.authorId === b.authorId && a.isSelf === b.isSelf + +/** + * Returns whether two reads of a message are the same message. + * + * **Details** + * + * It compares `id`, `thread`, `author.id`, `author.isBot`, `author.isSelf`, + * `text`, `mentions`, `replyTo`, `timestamp` and the attachment ids in order. + * `raw`, `author.name` and the optional attachment fields are left out. + * + * @stability unstable + * @category comparisons + * @since 4.1.5 + */ +export const sameMessage = (a: ChatMessage.ChatMessage, b: ChatMessage.ChatMessage): boolean => + a.id === b.id && + ChatThread.equals(a.thread, b.thread) && + a.author.id === b.author.id && + a.author.isBot === b.author.isBot && + a.author.isSelf === b.author.isSelf && + a.text === b.text && + sameMentions(a.mentions, b.mentions) && + sameReplyTo(a.replyTo, b.replyTo) && + DateTime.Equivalence(a.timestamp, b.timestamp) && + a.attachments.length === b.attachments.length && + a.attachments.every((attachment, i) => attachment.id === b.attachments[i].id) + +const canonicalJson = (value: unknown): string => + JSON.stringify(value, (_, v) => + v !== null && typeof v === "object" && !Array.isArray(v) + ? Object.fromEntries(Object.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) + : v) + +const failure = (rule: string, expected: string, actual: string, cause?: unknown) => + new ConformanceFailure({ rule, expected, actual, ...(cause === undefined ? {} : { cause }) }) + +const check = (rule: string, condition: boolean, expected: string, actual: () => string) => + condition ? Effect.void : Effect.fail(failure(rule, expected, actual())) + +const MessageJson = Schema.toCodecJson(ChatMessage.ChatMessage) +const EventJson = Schema.toCodecJson(ChatEvent.ChatEvent) + +const roundTrip = >(rule: string, codec: S, value: S["Type"]) => + Effect.gen(function*() { + const encoded = yield* Schema.encodeEffect(codec)(value) + const decoded = yield* Schema.decodeEffect(codec)(encoded) + const again = yield* Schema.encodeEffect(codec)(decoded) + yield* check( + rule, + canonicalJson(encoded) === canonicalJson(again), + "an unchanged value after encoding and decoding", + () => canonicalJson(again) + ) + return decoded as S["Type"] + }).pipe( + Effect.catchTag( + "SchemaError", + (error) => Effect.fail(failure(rule, "a value that encodes and decodes", "SchemaError", error)) + ) + ) + +const checkThread = Effect.fnUntraced(function*(rule: string, thread: ChatThread.ChatThread, platform: string) { + yield* check(rule, thread.platform === platform, "platform " + platform, () => "platform " + thread.platform) + const decoded = ChatThread.fromString(ChatThread.toString(thread)) + yield* check( + rule, + Option.isSome(decoded) && ChatThread.equals(decoded.value, thread), + "a thread that survives its string codec", + () => ChatThread.toString(thread) + ) +}) + +const checkMessageWith = Effect.fnUntraced(function*(rule: string, message: ChatMessage.ChatMessage, options: { + readonly platform: string + readonly parseMention: ParseMention +}) { + yield* checkThread(rule, message.thread, options.platform) + const mentions = [...message.mentions].sort((a, b) => a.start - b.start) + let end = 0 + for (const mention of mentions) { + yield* check( + rule, + mention.start >= end && mention.start < mention.end && mention.end <= message.text.length, + "mentions inside text without overlap", + () => "mention at " + mention.start + "-" + mention.end + ) + end = mention.end + const parsed = options.parseMention(message.text.slice(mention.start, mention.end)) + yield* check( + rule, + Option.isSome(parsed) && parsed.value.kind === mention.kind && parsed.value.id === mention.id, + "a " + mention.kind + " mention token at " + mention.start + "-" + mention.end, + () => Option.isSome(parsed) ? "a " + parsed.value.kind + " token" : "no mention token" + ) + } + for (const attachment of message.attachments) { + yield* check( + rule, + attachment.id.length > 0 && !("url" in attachment) && !/^[a-z]+:\/\//i.test(attachment.id), + "an attachment with an id and no URL", + () => "attachment " + (attachment.id.length > 0 ? "with a URL" : "without an id") + ) + } + if (message.replyTo !== undefined) { + const { authorId, isSelf } = message.replyTo + yield* check( + rule, + (authorId === undefined) === (isSelf === undefined), + "replyTo.isSelf set exactly when replyTo.authorId is", + () => "authorId " + authorId + ", isSelf " + isSelf + ) + } + const decoded = yield* roundTrip(rule, MessageJson, message) + yield* check(rule, sameMessage(message, decoded), "the same message after decoding", () => "a different message") +}) + +/** + * Checks the invariants every message holds, for fixture tests and live runs. + * + * **Details** + * + * - Every mention sits inside `text`, mentions do not overlap, and each slice + * parses with `parseMention` as a token of the mention's kind and id. + * - `thread.platform` is the platform, and the thread survives its string + * codec. + * - The message encodes and decodes through its Schema unchanged, `raw` + * included. + * - Every attachment has an id and no URL. + * - `replyTo.isSelf` is set exactly when `replyTo.authorId` is. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const checkMessage = (message: ChatMessage.ChatMessage, options: { + readonly platform: string + readonly parseMention: ParseMention +}): Effect.Effect => checkMessageWith("checkMessage", message, options) + +/** + * Checks the invariants every event holds, for fixture tests and live runs. + * + * **Details** + * + * - A message event's message passes `checkMessage`, and the event's thread is + * the message's. + * - `thread.platform` is the platform, and the thread survives its string + * codec. + * - The event encodes and decodes through its Schema unchanged, `raw` + * included, and two decodes have the same `dedupeKey`. + * - A reaction's `dedupeKey` differs between added and removed. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const checkEvent = (event: ChatEvent.ChatEvent, options: { + readonly platform: string + readonly parseMention: ParseMention +}): Effect.Effect => + Effect.gen(function*() { + const rule = "checkEvent" + yield* checkThread(rule, event.thread, options.platform) + if (event._tag === "MessageCreated") { + yield* checkMessageWith(rule, event.message, options) + yield* check( + rule, + ChatThread.equals(event.thread, event.message.thread), + "the event's thread equal to the message's", + () => ChatThread.toString(event.thread) + ) + } else if (event._tag !== "StopRequested") { + const { _tag, ...fields } = event + const flipped = _tag === "ReactionAdded" + ? ChatEvent.ReactionRemoved.make(fields) + : ChatEvent.ReactionAdded.make(fields) + yield* check( + rule, + ChatEvent.dedupeKey(flipped) !== ChatEvent.dedupeKey(event), + "different keys for an added and a removed reaction", + () => ChatEvent.dedupeKey(event) + ) + } + const first = yield* roundTrip(rule, EventJson, event) + const second = yield* roundTrip(rule, EventJson, event) + const key = ChatEvent.dedupeKey(event) + yield* check( + rule, + ChatEvent.dedupeKey(first) === key && ChatEvent.dedupeKey(second) === key, + "the same dedupeKey for every decode", + () => ChatEvent.dedupeKey(first) + " and " + ChatEvent.dedupeKey(second) + ) + }) + +const InteractionJson = Schema.toCodecJson(ChatInteraction.Interaction) + +/** + * Checks the invariants every interaction holds, for a click, a submit or a + * command from a fixture. + * + * **Details** + * + * - `thread.platform` is the platform, and the thread survives its string + * codec. + * - The interaction encodes and decodes through its Schema unchanged, `raw` + * included. + * - A `ModalSubmitted`'s `messageId` is non-empty when it is there. A modal + * a command opened has none. + * - A `CommandInvoked` has a non-empty `id`. + * + * It takes no `parseMention` and checks no dedupe key, since an interaction + * carries neither. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const checkInteraction = (interaction: ChatInteraction.Interaction, options: { + readonly platform: string +}): Effect.Effect => + Effect.gen(function*() { + const rule = "checkInteraction" + yield* checkThread(rule, interaction.thread, options.platform) + if (interaction._tag === "ModalSubmitted") { + yield* check(rule, interaction.messageId !== "", "a submit with no messageId or a non-empty one", () => + "an empty messageId") + } + if (interaction._tag === "CommandInvoked") { + yield* check(rule, interaction.id !== "", "a command with a non-empty id", () => + "an empty id") + } + yield* roundTrip(rule, InteractionJson, interaction) + }) + +// ----------------------------------------------------------------------------- +// conformance +// ----------------------------------------------------------------------------- + +/** + * The click hook of `conformance`: a way to click, for the adapter under + * test. + * + * **Details** + * + * - `click` clicks the element `id` on a message the suite posted, through + * the adapter's real entry, and fails `NoSuchElement` when the message + * carries no such element. `option` is as in `ChatTest.click`. + * - `submit` submits the modal the last click or command opened, and + * submits it again after an `errors` reply. It does nothing when no modal + * is open: the rule then fails on the submit its handler never saw. + * - `command` sends a command typed at `thread` through the adapter's real + * entry. The command rules exist only when it is given. It is optional, so + * a hook written before commands still fits. + * - All three need `ChatInteraction.Handler` and nothing else. The suite + * provides its own recording handler, so the entry must take its handler + * at the call, not at layer build. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export interface Interactions { + readonly click: (messageId: string, id: string, options?: { + readonly option?: string | undefined + }) => Effect.Effect + readonly submit: ( + values: Readonly> + ) => Effect.Effect + readonly command?: + | ((thread: ChatThread.ChatThread, id: string, options?: { + readonly text?: string | undefined + }) => Effect.Effect) + | undefined +} + +/** What the suite's own `ChatInteraction.Handler` received, and how it answers. */ +class Recording { + readonly clicks: Array = [] + readonly submits: Array = [] + readonly commands: Array = [] + readonly commandReplies: Array = [] + readonly refused: Array = [] + onClick: (event: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged) => ChatInteraction.ClickReply = () => + ChatInteraction.ack + onSubmit: (event: ChatInteraction.ModalSubmitted) => ChatInteraction.SubmitReply = () => ChatInteraction.close + onCommand: (event: ChatInteraction.CommandInvoked) => ChatInteraction.CommandReply = (event) => + ChatInteraction.respond({ markdown: conformanceText(event.id), visibility: "public" }) + readonly handler = ChatInteraction.Handler.of({ + click: (event) => + Effect.sync(() => { + this.clicks.push(event) + return this.onClick(event) + }), + submit: (event) => + Effect.sync(() => { + this.submits.push(event) + return this.onSubmit(event) + }), + command: (event) => + Effect.sync(() => { + this.commands.push(event) + const reply = this.onCommand(event) + this.commandReplies.push(reply) + return reply + }), + refused: (_, error) => Effect.sync(() => void this.refused.push(error)) + }) +} + +const conformanceButton = (rule: string): ChatUi.Button => ({ _tag: "Button", id: "conformance", label: rule }) + +const conformanceSelect = (rule: string): ChatUi.Select => ({ + _tag: "Select", + id: "conformance", + placeholder: rule, + options: [{ label: "a", value: "a" }, { label: "b", value: "b" }] +}) + +const describeExit = (exit: Exit.Exit): string => { + if (Exit.isSuccess(exit)) return "success" + const error = Exit.findErrorOption(exit) + if (Option.isNone(error)) return "a defect or interruption" + return describeError(error.value) +} + +const describeError = (error: unknown): string => { + if (!isChatError(error)) return String(error) + return error.reason._tag === "Unsupported" + ? "Unsupported(" + error.reason.capability + ")" + : error.reason._tag +} + +const conformanceText = (rule: string) => "conformance " + rule + +class Tester { + readonly created: Array<{ readonly thread: ChatThread.ChatThread; readonly id: string }> = [] + readonly rule: string + readonly chat: Chat.Chat["Service"] + readonly descriptor: Descriptor + readonly options: ConformanceOptions + constructor(rule: string, chat: Chat.Chat["Service"], descriptor: Descriptor, options: ConformanceOptions) { + this.rule = rule + this.chat = chat + this.descriptor = descriptor + this.options = options + } + + get thread() { + return this.options.thread + } + + readonly recording = new Recording() + + /** Clicks through the hook, with the suite's recording handler. */ + clickExit(messageId: string, id: string, options?: { readonly option?: string | undefined }) { + const interactions = this.options.interactions! + return Effect.exit(Effect.provideService( + interactions.click(messageId, id, options), + ChatInteraction.Handler, + this.recording.handler + )) + } + + click(messageId: string, id: string, options?: { readonly option?: string | undefined }) { + return Effect.flatMap( + this.clickExit(messageId, id, options), + (exit) => + Exit.isSuccess(exit) ? Effect.void : Effect.fail(failure( + this.rule, + "a click on element " + id + " of message " + messageId, + describeExit(exit), + Option.getOrUndefined(Exit.findErrorOption(exit)) + )) + ) + } + + /** Sends a command through the hook, with the suite's recording handler. */ + command(id: string, options?: { readonly text?: string | undefined }) { + return Effect.provideService( + this.options.interactions!.command!(this.thread, id, options), + ChatInteraction.Handler, + this.recording.handler + ) + } + + submit(values: Readonly>) { + return Effect.provideService( + this.options.interactions!.submit(values), + ChatInteraction.Handler, + this.recording.handler + ) + } + + /** Checks a received interaction with `checkInteraction`, under this rule's name. */ + checkInteraction(interaction: ChatInteraction.Interaction) { + return Effect.mapError( + checkInteraction(interaction, { platform: this.options.platform }), + (error) => failure(this.rule, error.expected, error.actual, error.cause) + ) + } + + /** A click whose handler answers with `modal`, and what the adapter did with it. */ + openModal(modal: ChatInteraction.OpenModal) { + return Effect.gen({ self: this }, function*() { + // A modal opens from a click, on a select when there are no buttons. + const element = this.has("buttons") || !this.has("selects") + ? conformanceButton(this.rule) + : conformanceSelect(this.rule) + const posted = yield* this.post({ actions: [element] }) + this.recording.onClick = () => modal + yield* this.click(posted.id, "conformance") + return posted + }) + } + + has(capability: Capability) { + return this.descriptor.capabilities.has(capability) + } + + call(effect: Effect.Effect): Effect.Effect { + return Effect.mapError(effect, (error) => { + const listed = error.reason._tag === "Unsupported" && this.has(error.reason.capability) + return failure( + this.rule, + "success", + describeError(error) + (listed ? " for a listed capability" : ""), + error + ) + }) + } + + collect(stream: Stream.Stream): Effect.Effect, ConformanceFailure> { + return this.call(Stream.runCollect(stream)) + } + + check(condition: boolean, expected: string, actual: () => string) { + return check(this.rule, condition, expected, actual) + } + + post( + content: { + readonly markdown?: string + readonly replyTo?: ReplyTo + readonly files?: ReadonlyArray + readonly actions?: ReadonlyArray + }, + thread = this.thread + ) { + return Effect.tap( + this.call(this.chat.post(thread, { markdown: conformanceText(this.rule), ...content })), + (posted) => Effect.sync(() => this.created.push({ thread, id: posted.id })) + ) + } + + userPosts(thread = this.thread) { + return Effect.flatMap( + this.options.userPosts(thread, conformanceText(this.rule)), + (event) => + event._tag === "MessageCreated" + ? Effect.succeed(event) + : Effect.fail(failure(this.rule, "a MessageCreated event from userPosts", event._tag)) + ) + } + + expectReason( + tag: ChatErrorReason["_tag"], + effect: Effect.Effect, + onSuccess?: (a: A) => void, + refine?: { readonly expected: string; readonly test: (reason: ChatErrorReason) => boolean } + ): Effect.Effect { + return Effect.flatMap(Effect.exit(effect), (exit) => { + if (Exit.isSuccess(exit)) onSuccess?.(exit.value) + const error = Exit.findErrorOption(exit) + const matches = Option.isSome(error) && isChatError(error.value) && error.value.reason._tag === tag && + (refine === undefined || refine.test(error.value.reason)) + return matches ? Effect.void : Effect.fail(failure( + this.rule, + refine === undefined ? tag : refine.expected, + describeExit(exit), + Option.getOrUndefined(error) + )) + }) + } + + /** + * Calls the op behind `capability`. When the descriptor lacks it, the op + * must fail with `Unsupported` naming it. When it is listed, the op must + * not fail with `Unsupported` at all, whichever capability that names, so a + * descriptor that lies in either direction fails. + */ + probe(capability: Capability): Effect.Effect { + const listed = this.has(capability) + const expected = listed ? "no Unsupported for listed " + capability : "Unsupported(" + capability + ")" + const expect = (effect: Effect.Effect, onSuccess?: (a: A) => void) => + Effect.flatMap(Effect.exit(effect), (exit) => { + if (Exit.isSuccess(exit)) onSuccess?.(exit.value) + const error = Exit.findErrorOption(exit) + const reason = Option.isSome(error) && isChatError(error.value) ? error.value.reason : undefined + const holds = listed + ? Exit.isSuccess(exit) || (reason !== undefined && reason._tag !== "Unsupported") + : reason?._tag === "Unsupported" && reason.capability === capability + return holds ? Effect.void : Effect.fail(failure( + this.rule, + expected, + describeExit(exit), + Option.getOrUndefined(error) + )) + }) + const track = (posted: Posted) => void this.created.push({ thread: this.thread, id: posted.id }) + const { chat, thread } = this + const text = conformanceText(this.rule) + switch (capability) { + case "edit": + return Effect.flatMap(this.post({}), (posted) => expect(chat.edit(thread, posted.id, { markdown: text }))) + case "delete": + return Effect.flatMap(this.post({}), (posted) => expect(chat.delete(thread, posted.id))) + case "fetchMessage": + return Effect.flatMap(this.post({}), (posted) => expect(chat.fetchMessage(thread, posted.id))) + case "history": + return expect(Stream.runDrain(Stream.take(chat.history(thread, { direction: "backward" }), 1))) + case "download": + return expect(Stream.runDrain(chat.download(thread, { id: "conformance-missing" }, { maxBytes: 1 }))) + case "files": + return expect(chat.post(thread, { markdown: text, files: [conformanceFile(this.rule)] }), track) + case "replyTo": + return Effect.flatMap( + this.post({}), + (target) => expect(chat.post(thread, { markdown: text, replyTo: { messageId: target.id } }), track) + ) + case "startThread": + return Effect.flatMap(this.post({}), (posted) => expect(chat.startThread(thread, posted.id))) + case "getThread": + return expect(chat.getThread(thread)) + case "opener": + return expect(chat.opener(thread)) + case "getUser": + return Effect.flatMap( + this.userPosts(), + (event) => expect(chat.getUser(this.options.platform, event.message.author.id)) + ) + case "buttons": + return expect(chat.post(thread, { markdown: text, actions: [conformanceButton(this.rule)] }), track) + case "selects": + return expect(chat.post(thread, { markdown: text, actions: [conformanceSelect(this.rule)] }), track) + case "modals": + // Probing a modal requires the interaction hook and a clickable element. + return this.options.interactions === undefined || !(this.has("buttons") || this.has("selects")) + ? Effect.void + : Effect.gen({ self: this }, function*() { + yield* this.openModal(probeModal.open()) + const refusal = this.recording.refused.find((error) => error.reason._tag === "Unsupported") + const holds = listed + ? refusal === undefined + : refusal?.reason._tag === "Unsupported" && refusal.reason.capability === "modals" + yield* this.check( + holds, + listed ? "no refusal Unsupported for listed modals" : "a refusal Unsupported(modals)", + () => + this.recording.refused.length === 0 + ? "no refusal" + : this.recording.refused.map(describeError).join(", ") + ) + }) + case "modalErrors": + // Its absence means a degraded answer, not a refusal, so it has no inverse. + return Effect.void + case "commands": + // A deferral built by hand, which no adapter can follow up: it must fail, and not with Unsupported when listed. + return expect(chat.followUp( + ChatInteraction.defer( + ChatInteraction.CommandInvoked.make({ + id: "conformance", + text: "", + thread, + userId: "conformance", + raw: {} + }), + { visibility: "public" } + ), + { markdown: text } + )) + case "ephemeral": + // Probing a private answer requires the command hook. + return this.options.interactions?.command === undefined + ? Effect.void + : Effect.gen({ self: this }, function*() { + this.recording.onCommand = () => ChatInteraction.respond({ markdown: text }) + yield* this.command("conformance") + const refusal = this.recording.refused.find((error) => error.reason._tag === "Unsupported") + const holds = listed + ? refusal === undefined + : refusal?.reason._tag === "Unsupported" && refusal.reason.capability === "ephemeral" + yield* this.check( + holds, + listed ? "no refusal Unsupported for listed ephemeral" : "a refusal Unsupported(ephemeral)", + () => + this.recording.refused.length === 0 + ? "no refusal" + : this.recording.refused.map(describeError).join(", ") + ) + }) + case "nativeStream": + return Effect.flatMap( + Registry, + (registry) => + Effect.flatMap(this.call(registry.get(this.options.platform)), (adapter) => + this.check( + (adapter.nativeStream !== undefined) === listed, + listed ? "a nativeStream, as the descriptor lists it" : "no nativeStream, as the descriptor lacks it", + () => adapter.nativeStream === undefined ? "no nativeStream" : "a nativeStream" + )) + ) + } + } + + get cleanup(): Effect.Effect { + return Effect.suspend(() => { + if (!this.has("delete")) return Effect.void + const created = this.created.splice(0).reverse() + return Effect.forEach(created, ({ id, thread }) => Effect.ignore(this.chat.delete(thread, id)), { + discard: true + }) + }) + } +} + +const conformanceFile = (rule: string): File => ({ + name: "conformance.txt", + contentType: "text/plain", + content: new TextEncoder().encode(conformanceText(rule) + " " + "x".repeat(64)) +}) + +const findIn = (messages: ReadonlyArray, id: string) => + messages.find((message) => message.id === id) + +/** A refusal of the text must not blame the files, or `ChatMarkdown` would post again without them. */ +const textTooLarge = { + expected: "TooLarge without subject files or detail file_refused", + test: (reason: ChatErrorReason) => + reason._tag === "TooLarge" && reason.subject !== "files" && reason.detail !== fileRefused +} + +const mentionLike = "@everyone @here <@U0000> <@&1> <#C0000>" + +/** Text a platform takes in any quantity: no emoji, which Slack limits separately. */ +const measureFitsBlock = "a < b & c > d\n\n```ts\nconst x = 1 < 2\n```\n\n\u{1D400}\u{1D401} caf\u00e9\n\n" + +const measureSamples: ReadonlyArray = [ + "", + "x", + "hello world", + "<", + "a < b", + "<@U0000>", + "& > &", + "```\ncode < 1\n```\n", + "```", + "\u{1D400}\u{1D401}", + "\u{1F600}\u{1F44D}\u{1F3FD}", + ":grinning:", + "caf\u00e9" +] + +const probeModal = ChatInteraction.modal("conformance-modal", { + title: "Conformance", + fields: { answer: ChatInteraction.textInput({ label: "Answer" }) } +}) + +/** The fixed schemas of `actions/valueRoundTrips`, each with the value posted. */ +const valueCases: ReadonlyArray<{ readonly schema: ChatInteraction.ValueSchema; readonly value: unknown }> = [ + { schema: Schema.String, value: "v1.4.2" }, + { schema: Schema.Struct({ version: Schema.String, attempt: Schema.Number }), value: { version: "v1", attempt: 2 } }, + { schema: Schema.Union([Schema.String, Schema.Number]), value: 123 }, + { schema: Schema.Union([Schema.String, Schema.Number]), value: "123" }, + { schema: Schema.String, value: "a < b & \"c\" \u{1D400}" } +] + +const sameValue = (a: unknown, b: unknown) => typeof a === typeof b && canonicalJson(a) === canonicalJson(b) + +interface ConformanceOptions { + readonly platform: string + readonly thread: ChatThread.ChatThread + readonly userPosts: (thread: ChatThread.ChatThread, text: string) => Effect.Effect + readonly parseMention: ParseMention + readonly refuseFiles?: (() => Effect.Effect) | undefined + readonly interactions?: Interactions | undefined + readonly failNext?: + | ((op: string, reason: ChatErrorReason, options?: { + readonly times?: number | undefined + readonly landed?: boolean | undefined + }) => Effect.Effect) + | undefined +} + +/** + * Creates the conformance suite, as plain Effects keyed by rule name, so any + * test runner can run them. + * + * **Details** + * + * - Each rule needs the adapter under test registered in `Chat` and a way to + * make another identity's message appear in `thread`, which must be a + * channel the suite may write to. + * - A rule is never skipped. When the descriptor lacks a capability a rule + * needs, the rule becomes its inverse: the op fails with `Unsupported`. + * There is an `unsupported/{capability}` rule for every capability. When + * the capability is listed, it calls the op and fails if the op fails with + * `Unsupported`, so a descriptor that lies in either direction fails. + * - A listed capability never fails with `Unsupported`, which every rule + * checks as it goes. + * - The three `retries/*` rules exist only when `failNext` is given. + * `post/refusedFile` exists only when `refuseFiles` is given. + * - The rules that need a click exist only when `interactions` is given, + * since neither platform has an API that clicks. A live run leaves it out + * and runs only the outbound rules. The suite provides its own recording + * `ChatInteraction.Handler` to the hook, so the rules still need only + * `Chat` and `Registry`. + * - `modalErrors` has no inverse rule: without it an `errors` reply is a + * degraded answer, not a refusal. + * - The `command/*` rules and `unsupported/ephemeral` exist only when + * `interactions.command` is given. `unsupported/commands` always runs: it + * calls `followUp` with a deferral the suite builds by hand, which must + * fail `Unsupported` without `commands`, and fail some other way with it. + * - Every rule deletes what it posted when it ends, pass or fail, when the + * platform can delete. + * + * **Gotchas** + * + * Rule names are stable, an existing rule is never tightened, and a new rule + * arrives only behind a new capability or with a changeset saying third-party + * adapters may start failing. `unsupported/buttons` and + * `unsupported/selects` are such rules: an adapter that ignores `actions` + * and posts anyway fails them. So is `unsupported/commands`: an adapter whose + * `followUp` does anything but fail `Unsupported` fails it. The `retries/retrySafe` rule waits for real + * time, so run the suite on a live `Clock`. + * + * @stability unstable + * @category testing + * @since 4.1.5 + */ +export const conformance = (options: { + readonly platform: string + readonly thread: ChatThread.ChatThread + readonly userPosts: (thread: ChatThread.ChatThread, text: string) => Effect.Effect + readonly parseMention: ParseMention + readonly refuseFiles?: (() => Effect.Effect) | undefined + readonly interactions?: Interactions | undefined + readonly failNext?: + | ((op: string, reason: ChatErrorReason, options?: { + readonly times?: number | undefined + readonly landed?: boolean | undefined + }) => Effect.Effect) + | undefined +}): Readonly>> => { + const rules: Record> = {} + const { parseMention, platform } = options + + const rule = ( + name: string, + needs: ReadonlyArray, + body: (t: Tester) => Effect.Effect + ) => { + rules[name] = Effect.gen(function*() { + const chat = yield* Chat.Chat + const descriptor = yield* Effect.mapError( + chat.descriptor(platform), + (error) => failure(name, "a registered adapter", describeError(error), error) + ) + const t = new Tester(name, chat, descriptor, options) + const missing = needs.find((capability) => !t.has(capability)) + return yield* (missing === undefined ? body(t) : t.probe(missing)).pipe( + Effect.ensuring(t.cleanup) + ) + }) + } + + rule("post/fetchable", ["fetchMessage"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({}) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check( + posted.id === message.id && ChatThread.equals(posted.thread, t.thread) && + ChatThread.equals(message.thread, t.thread), + "the posted message, addressed to " + ChatThread.toString(t.thread), + () => "message " + message.id + " in " + ChatThread.toString(message.thread) + ) + })) + + rule("post/authorIsSelf", ["fetchMessage"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({}) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check( + message.author.isSelf && message.author.isBot, + "author.isSelf and author.isBot", + () => "isSelf " + message.author.isSelf + ", isBot " + message.author.isBot + ) + })) + + rule("post/replyTo", ["replyTo", "fetchMessage"], (t) => + Effect.gen(function*() { + const target = yield* t.post({}) + const reply = yield* t.post({ replyTo: { messageId: target.id } }) + const message = yield* t.call(t.chat.fetchMessage(t.thread, reply.id)) + yield* t.check( + message.replyTo?.messageId === target.id, + "replyTo " + target.id, + () => "replyTo " + message.replyTo?.messageId + ) + })) + + rule("post/files", ["files", "download", "fetchMessage"], (t) => + Effect.gen(function*() { + const file = conformanceFile(t.rule) + const posted = yield* t.post({ files: [file] }) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check(message.attachments.length === 1, "one attachment", () => message.attachments.length + "") + const chunks = yield* t.collect( + t.chat.download(t.thread, message.attachments[0], { maxBytes: file.content.length }) + ) + const bytes = new Uint8Array(chunks.reduce((n, chunk) => n + chunk.length, 0)) + chunks.reduce((offset, chunk) => (bytes.set(chunk, offset), offset + chunk.length), 0) + yield* t.check( + bytes.length === file.content.length && bytes.every((byte, i) => byte === file.content[i]), + "the uploaded bytes", + () => bytes.length + " different bytes" + ) + })) + + const refuseFiles = options.refuseFiles + if (refuseFiles !== undefined) { + rule("post/refusedFile", ["files", "history"], (t) => + Effect.gen(function*() { + const before = yield* t.collect(t.chat.history(t.thread, { direction: "forward" })) + yield* refuseFiles() + const exit = yield* Effect.exit(t.chat.post(t.thread, { + markdown: conformanceText(t.rule), + files: [conformanceFile(t.rule)] + })) + if (Exit.isSuccess(exit)) t.created.push({ thread: t.thread, id: exit.value.id }) + const after = yield* t.collect(t.chat.history(t.thread, { direction: "forward" })) + const beforeIds = new Set(before.map((message) => message.id)) + for (const message of after) { + if (message.author.isSelf && !beforeIds.has(message.id) && !t.created.some(({ id }) => id === message.id)) { + t.created.push({ thread: message.thread, id: message.id }) + } + } + const error = Exit.findErrorOption(exit) + const reason = Option.isSome(error) ? error.value.reason : undefined + const listed = reason?._tag === "Unsupported" && t.has(reason.capability) + yield* t.check( + !listed && reason?.retry === "denied" && reason.detail === fileRefused, + "denied with detail file_refused, not Unsupported for a listed capability", + () => describeExit(exit) + (listed ? " for a listed capability" : "") + ) + yield* t.check( + after.length === before.length && after.every((message, i) => message.id === before[i].id), + "no new message", + () => after.length + " messages after, " + before.length + " before" + ) + })) + } + + rule("post/tooLarge", [], (t) => + t.expectReason( + "TooLarge", + t.chat.post(t.thread, { markdown: "x".repeat(t.descriptor.limits.messageLength + 1) }), + (posted) => t.created.push({ thread: t.thread, id: posted.id }), + textTooLarge + )) + + rule("descriptor/measureFits", [], (t) => + Effect.gen(function*() { + const { measure } = t.descriptor + const limit = t.descriptor.limits.messageLength + const largest = (make: (n: number) => string) => { + let low = 0 + let high = limit + while (low < high) { + const middle = Math.ceil((low + high) / 2) + if (measure(make(middle)) <= limit) low = middle + else high = middle - 1 + } + return make(low) + } + const blocks = largest((n) => measureFitsBlock.repeat(n)) + const markdown = largest((n) => blocks + "x".repeat(n)) + yield* t.post({ markdown }) + })) + + rule("descriptor/measureAdds", [], (t) => + Effect.gen(function*() { + const { measure } = t.descriptor + for (const a of measureSamples) { + for (const b of measureSamples) { + yield* t.check( + measure(a + b) <= measure(a) + measure(b), + "measure(a + b) <= measure(a) + measure(b)", + () => JSON.stringify(a) + " + " + JSON.stringify(b) + " measures " + measure(a + b) + ) + } + } + const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + yield* t.check(measure(letters) === letters.length, "ASCII letters measured at one each", () => + letters.length + " letters measure " + measure(letters)) + })) + + rule("post/noMentions", ["fetchMessage"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ markdown: mentionLike }) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check(message.mentions.length === 0, "no mentions", () => message.mentions.length + " mentions") + })) + + const goneId = Effect.fnUntraced(function*(t: Tester) { + const posted = yield* t.post({}) + if (!t.has("delete")) return posted.id + "0" + yield* t.call(t.chat.delete(t.thread, posted.id)) + return posted.id + }) + + rule("edit/keepsId", ["edit", "fetchMessage"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ markdown: conformanceText(t.rule) + " before" }) + const before = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.call(t.chat.edit(t.thread, posted.id, { markdown: conformanceText(t.rule) + " after" })) + const after = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check( + after.id === posted.id && after.text !== before.text, + "the same id with new text", + () => "id " + after.id + (after.text === before.text ? " with the old text" : "") + ) + })) + + rule("edit/unknownId", ["edit"], (t) => + Effect.flatMap( + goneId(t), + (id) => t.expectReason("NotFound", t.chat.edit(t.thread, id, { markdown: conformanceText(t.rule) })) + )) + + rule("edit/tooLarge", ["edit"], (t) => + Effect.flatMap(t.post({}), (posted) => + t.expectReason( + "TooLarge", + t.chat.edit(t.thread, posted.id, { markdown: "x".repeat(t.descriptor.limits.messageLength + 1) }), + undefined, + textTooLarge + ))) + + rule("delete/gone", ["delete", "fetchMessage"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({}) + yield* t.call(t.chat.delete(t.thread, posted.id)) + yield* t.expectReason("NotFound", t.chat.fetchMessage(t.thread, posted.id)) + })) + + rule( + "delete/unknownId", + ["delete"], + (t) => Effect.flatMap(goneId(t), (id) => t.expectReason("NotFound", t.chat.delete(t.thread, id))) + ) + + rule("history/order", ["history"], (t) => + Effect.gen(function*() { + const a = yield* t.post({ markdown: conformanceText(t.rule) + " a" }) + const b = yield* t.post({ markdown: conformanceText(t.rule) + " b" }) + const c = yield* t.post({ markdown: conformanceText(t.rule) + " c" }) + const backward = yield* t.collect(Stream.take(t.chat.history(t.thread, { direction: "backward" }), 3)) + const forward = yield* t.collect( + Stream.take(t.chat.history(t.thread, { direction: "forward", from: a.id }), 2) + ) + const ids = (messages: ReadonlyArray) => messages.map((m) => m.id).join(",") + yield* t.check(ids(backward) === [c.id, b.id, a.id].join(","), "backward " + [c.id, b.id, a.id], () => + "backward " + ids(backward)) + yield* t.check(ids(forward) === [b.id, c.id].join(","), "forward " + [b.id, c.id], () => + "forward " + ids(forward)) + })) + + rule("history/fromExclusive", ["history"], (t) => + Effect.gen(function*() { + const a = yield* t.post({ markdown: conformanceText(t.rule) + " a" }) + const b = yield* t.post({ markdown: conformanceText(t.rule) + " b" }) + const before = yield* t.collect(Stream.take(t.chat.history(t.thread, { direction: "backward", from: b.id }), 1)) + const after = yield* t.collect(Stream.take(t.chat.history(t.thread, { direction: "forward", from: a.id }), 1)) + yield* t.check( + before[0]?.id === a.id && after[0]?.id === b.id, + "the message next to from, not from itself", + () => "backward " + before[0]?.id + ", forward " + after[0]?.id + ) + })) + + rule("history/sameAsInbound", ["history"], (t) => + Effect.gen(function*() { + const event = yield* t.userPosts() + const history = yield* t.collect(Stream.take(t.chat.history(t.thread, { direction: "backward" }), 20)) + const read = findIn(history, event.message.id) + yield* t.check( + read !== undefined && sameMessage(event.message, read), + "the inbound message in history", + () => read === undefined ? "no such message" : "a different message" + ) + })) + + rule("history/startsWithOpener", ["history", "startThread"], (t) => + Effect.gen(function*() { + const opener = yield* t.post({ markdown: conformanceText(t.rule) + " opener" }) + const thread = yield* t.call(t.chat.startThread(t.thread, opener.id, { name: conformanceText(t.rule) })) + yield* t.post({ markdown: conformanceText(t.rule) + " reply" }, thread) + const first = yield* t.collect(Stream.take(t.chat.history(thread, { direction: "forward" }), 1)) + yield* t.check( + first[0]?.id === opener.id && ChatThread.equals(first[0].thread, t.thread), + "the opener first, addressed to " + ChatThread.toString(t.thread), + () => + first[0] === undefined + ? "an empty history" + : "message " + first[0].id + " in " + ChatThread.toString(first[0].thread) + ) + })) + + rule("message/threadStable", ["fetchMessage", "history", "startThread"], (t) => + Effect.gen(function*() { + const event = yield* t.userPosts() + const id = event.message.id + const read = Effect.gen(function*() { + const fetched = yield* t.call(t.chat.fetchMessage(t.thread, id)) + const history = yield* t.collect(Stream.take(t.chat.history(t.thread, { direction: "backward" }), 20)) + return [fetched, findIn(history, id)] as const + }) + const before = yield* read + const thread = yield* t.call(t.chat.startThread(t.thread, id, { name: conformanceText(t.rule) })) + yield* t.post({ markdown: conformanceText(t.rule) + " reply" }, thread) + const after = yield* read + const reads = [event.message, ...before, ...after] + yield* t.check( + reads.every((message) => message !== undefined && ChatThread.equals(message.thread, t.thread)), + "every read addressed to " + ChatThread.toString(t.thread), + () => reads.map((message) => message === undefined ? "missing" : ChatThread.toString(message.thread)).join(", ") + ) + })) + + rule("inbound/fetchable", ["fetchMessage"], (t) => + Effect.gen(function*() { + const event = yield* t.userPosts() + const message = yield* t.call(t.chat.fetchMessage(event.thread, event.message.id)) + yield* t.check(sameMessage(event.message, message), "the inbound message", () => "a different message") + })) + + rule("inbound/notSelf", [], (t) => + Effect.gen(function*() { + const event = yield* t.userPosts() + yield* t.check(!event.message.author.isSelf, "author.isSelf false", () => "author.isSelf true") + yield* checkEvent(event, { platform, parseMention }).pipe( + Effect.mapError((error) => failure(t.rule, error.expected, error.actual, error.cause)) + ) + })) + + rule("download/maxBytes", ["files", "download", "fetchMessage"], (t) => + Effect.gen(function*() { + const file = conformanceFile(t.rule) + const posted = yield* t.post({ files: [file] }) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check(message.attachments.length === 1, "one attachment", () => message.attachments.length + "") + yield* t.expectReason( + "TooLarge", + Stream.runDrain(t.chat.download(t.thread, message.attachments[0], { maxBytes: 8 })) + ) + })) + + rule("startThread/idempotent", ["startThread"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({}) + const first = yield* t.call(t.chat.startThread(t.thread, posted.id, { name: conformanceText(t.rule) })) + const second = yield* t.call(t.chat.startThread(t.thread, posted.id)) + yield* t.check( + ChatThread.equals(first, second), + ChatThread.toString(first), + () => ChatThread.toString(second) + ) + })) + + rule("opener/noneOutsideThread", ["opener"], (t) => + Effect.flatMap( + t.call(t.chat.opener(t.thread)), + (opener) => t.check(Option.isNone(opener), "no opener", () => "an opener") + )) + + rule("descriptor/streamFitsMessage", ["nativeStream"], (t) => { + const { messageLength, streamLength } = t.descriptor.limits + return t.check( + streamLength !== undefined && streamLength <= messageLength, + "limits.streamLength at most " + messageLength, + () => "limits.streamLength " + streamLength + ) + }) + + const openStream = Effect.fnUntraced(function*(t: Tester, markdown: string) { + const registry = yield* Registry + const adapter = yield* t.call(registry.get(platform)) + const native = adapter.nativeStream! + const event = yield* t.userPosts() + const posted = yield* t.call(native.start(t.thread, { markdown, recipient: event.message.author.id })) + t.created.push({ thread: t.thread, id: posted.id }) + return { native, posted } + }) + + rule("nativeStream/concatenates", ["nativeStream", "fetchMessage"], (t) => + Effect.gen(function*() { + const first = conformanceText(t.rule) + " one" + const { native, posted } = yield* openStream(t, first) + const appended = yield* t.call(native.append(t.thread, posted.id, " two", { flush: true })).pipe( + Effect.ensuring(Effect.ignore(native.stop(t.thread, posted.id, { end: "done" }))) + ) + yield* t.check(appended.state === "open" && appended.held === 0, "an open append holding nothing", () => + appended.state + " holding " + appended.held) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check(message.text === first + " two", JSON.stringify(first + " two"), () => + JSON.stringify(message.text)) + })) + + rule("nativeStream/appendAfterStop", ["nativeStream", "fetchMessage"], (t) => + Effect.gen(function*() { + const first = conformanceText(t.rule) + const { native, posted } = yield* openStream(t, first) + yield* t.call(native.stop(t.thread, posted.id, { end: "done" })) + const appended = yield* t.call(native.append(t.thread, posted.id, " after", { flush: true })) + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check( + appended.state === "closed" && message.text === first, + "closed, with the text unchanged", + () => appended.state + " with " + JSON.stringify(message.text) + ) + })) + + // --- buttons and modals ---------------------------------------------------- + + const limit = (t: Tester, name: "actionsPerMessage" | "actionDataLength" | "modalMetadataLength" | "modalFields") => + Effect.flatMap( + t.check( + t.descriptor.limits[name] !== undefined, + "limits." + name + " on a descriptor with its capability", + () => "no limits." + name + ), + () => Effect.succeed(t.descriptor.limits[name]!) + ) + + rule("actions/posts", ["buttons"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ actions: [conformanceButton(t.rule)] }) + if (!t.has("fetchMessage")) return + const message = yield* t.call(t.chat.fetchMessage(t.thread, posted.id)) + yield* t.check(message.id === posted.id, "the posted message " + posted.id, () => "message " + message.id) + })) + + rule("actions/tooMany", ["buttons"], (t) => + Effect.gen(function*() { + const most = yield* limit(t, "actionsPerMessage") + const actions = Array.from({ length: most + 1 }, (_, i): ChatUi.Button => ({ + _tag: "Button", + id: "c" + i, + label: "c" + i + })) + yield* t.expectReason( + "TooLarge", + t.chat.post(t.thread, { markdown: conformanceText(t.rule), actions }), + (posted) => t.created.push({ thread: t.thread, id: posted.id }) + ) + })) + + rule("actions/dataTooLarge", ["buttons"], (t) => + Effect.gen(function*() { + const most = yield* limit(t, "actionDataLength") + const id = most >= "conformance".length ? "conformance" : "c".repeat(most + 1) + const value = "x".repeat(most + 1 - id.length) + const button: ChatUi.Button = { _tag: "Button", id, label: "c", ...(value === "" ? {} : { value }) } + yield* t.expectReason( + "TooLarge", + t.chat.post(t.thread, { markdown: conformanceText(t.rule), actions: [button] }), + (posted) => t.created.push({ thread: t.thread, id: posted.id }) + ) + })) + + rule("descriptor/measureAddsActions", ["buttons"], (t) => + Effect.gen(function*() { + const { measure } = t.descriptor + const button = conformanceButton(t.rule) + const sets: ReadonlyArray> = [ + [button], + [{ ...button, value: "a < b & \u{1D400}" }], + [button, { _tag: "LinkButton", label: "Link", url: "https://example.com/?a=1&b=<2>" }], + [conformanceSelect(t.rule)] + ] + for (const markdown of measureSamples) { + yield* t.check(measure(markdown, []) === measure(markdown), "measure(md, []) equal to measure(md)", () => + JSON.stringify(markdown) + " measures " + measure(markdown, []) + " and " + measure(markdown)) + for (const actions of sets) { + const both = measure(markdown, actions) + yield* t.check( + both <= measure(markdown) + measure("", actions) && both >= measure(markdown), + "measure(md) <= measure(md, actions) <= measure(md) + measure(\"\", actions)", + () => + JSON.stringify(markdown) + " with " + actions.map((action) => + action._tag + ) + " measures " + both + ) + } + } + })) + + rule("descriptor/measureFitsActions", ["buttons"], (t) => + Effect.gen(function*() { + const { measure } = t.descriptor + const most = t.descriptor.limits.messageLength + // A short label, so a platform whose labels are short still takes the button. + const actions = [{ ...conformanceButton(t.rule), label: "fits", value: "x" }] + let low = 0 + let high = most + while (low < high) { + const middle = Math.ceil((low + high) / 2) + if (measure("x".repeat(middle), actions) <= most) low = middle + else high = middle - 1 + } + yield* t.post({ markdown: "x".repeat(low), actions }) + })) + + const interactions = options.interactions + if (interactions !== undefined) { + rule("actions/valueRoundTrips", ["buttons"], (t) => + Effect.gen(function*() { + const buttons = valueCases.map(({ schema }, i) => + ChatInteraction.button("v" + i, { label: "v" + i, value: schema }) + ) + const posted = yield* t.post({ actions: buttons.map((button, i) => button.of(valueCases[i].value)) }) + for (const [i, { schema, value }] of valueCases.entries()) { + yield* t.click(posted.id, "v" + i) + const click = t.recording.clicks[i] + const decoded = click?._tag === "ButtonClicked" && click.value !== undefined + ? ChatInteraction.decodeValue(schema, click.value) + : Option.none() + yield* t.check( + Option.isSome(decoded) && sameValue(decoded.value, value), + "a value that decodes to " + canonicalJson(value), + () => click === undefined ? "no click" : "value " + JSON.stringify(click.value) + ) + } + })) + + rule("actions/absentValue", ["buttons"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ actions: [conformanceButton(t.rule)] }) + yield* t.click(posted.id, "conformance") + const click = t.recording.clicks[0] + yield* t.check( + click?._tag === "ButtonClicked" && !("value" in click), + "a click with no value key", + () => click === undefined ? "no click" : "value " + JSON.stringify(click.value) + ) + })) + + rule("actions/origin", ["buttons"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ actions: [conformanceButton(t.rule)] }) + yield* t.click(posted.id, "conformance") + const click = t.recording.clicks[0] + yield* t.check( + click !== undefined && click.messageId === posted.id && ChatThread.equals(click.thread, posted.thread), + "a click on " + posted.id + " in " + ChatThread.toString(posted.thread), + () => click === undefined ? "no click" : click.messageId + " in " + ChatThread.toString(click.thread) + ) + yield* t.checkInteraction(click) + })) + + rule("actions/selectValue", ["selects"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ actions: [conformanceSelect(t.rule)] }) + yield* t.click(posted.id, "conformance", { option: "b" }) + const event = t.recording.clicks[0] + yield* t.check( + event?._tag === "SelectChanged" && event.value === "b", + "SelectChanged with value b", + () => event === undefined ? "no click" : event._tag + " with value " + JSON.stringify(event.value) + ) + yield* t.checkInteraction(event) + })) + + rule("edit/removesActions", ["buttons", "edit"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({ actions: [conformanceButton(t.rule)] }) + yield* t.call(t.chat.edit(t.thread, posted.id, { markdown: conformanceText(t.rule) + " edited" })) + const exit = yield* t.clickExit(posted.id, "conformance") + const error = Exit.findErrorOption(exit) + yield* t.check( + Option.isSome(error) && error.value._tag === "NoSuchElement", + "NoSuchElement", + () => describeExit(exit) + ) + })) + + rule("edit/addsActions", ["buttons", "edit"], (t) => + Effect.gen(function*() { + const posted = yield* t.post({}) + yield* t.call(t.chat.edit(t.thread, posted.id, { + markdown: conformanceText(t.rule) + " edited", + actions: [conformanceButton(t.rule)] + })) + yield* t.click(posted.id, "conformance") + yield* t.check(t.recording.clicks.length === 1, "one click", () => t.recording.clicks.length + " clicks") + })) + + rule("modal/origin", ["modals"], (t) => + Effect.gen(function*() { + const posted = yield* t.openModal(probeModal.open()) + yield* t.submit({ answer: "conformance" }) + const submit = t.recording.submits[0] + yield* t.check( + submit !== undefined && submit.messageId === posted.id && ChatThread.equals(submit.thread, posted.thread), + "a submit from " + posted.id + " in " + ChatThread.toString(posted.thread), + () => submit === undefined ? "no submit" : submit.messageId + " in " + ChatThread.toString(submit.thread) + ) + yield* t.checkInteraction(submit) + })) + + rule("modal/metadataRoundTrips", ["modals"], (t) => + Effect.gen(function*() { + const Metadata = Schema.Struct({ version: Schema.String }) + const withMetadata = ChatInteraction.modal("conformance-meta", { + title: "Conformance", + metadata: Metadata, + fields: { answer: ChatInteraction.textInput({ label: "Answer" }) } + }) + const value = { version: "v1 < \u{1D400}" } + yield* t.openModal(withMetadata.open({ metadata: value })) + yield* t.submit({ answer: "conformance" }) + yield* t.openModal(probeModal.open()) + yield* t.submit({ answer: "conformance" }) + const [first, second] = t.recording.submits + const decoded = first?.metadata === undefined + ? Option.none() + : ChatInteraction.decodeValue(Metadata, first.metadata) + yield* t.check( + Option.isSome(decoded) && sameValue(decoded.value, value), + "metadata that decodes to " + canonicalJson(value), + () => first === undefined ? "no submit" : "metadata " + JSON.stringify(first.metadata) + ) + yield* t.check( + second !== undefined && !("metadata" in second), + "a submit with no metadata key", + () => second === undefined ? "no submit" : "metadata " + JSON.stringify(second.metadata) + ) + })) + + const refusedTooLarge = Effect.fnUntraced(function*(t: Tester) { + yield* t.submit({}) + const refusal = t.recording.refused[0] + yield* t.check( + refusal?.reason._tag === "TooLarge" && t.recording.submits.length === 0, + "a refusal TooLarge and no modal", + () => + (refusal === undefined ? "no refusal" : "a refusal " + describeError(refusal)) + + (t.recording.submits.length === 0 ? "" : " and a submit") + ) + }) + + rule("modal/metadataTooLarge", ["modals"], (t) => + Effect.gen(function*() { + const most = yield* limit(t, "modalMetadataLength") + const id = "conformance-big" + const big = ChatInteraction.modal(id, { + title: "Conformance", + metadata: Schema.String, + fields: { answer: ChatInteraction.textInput({ label: "Answer" }) } + }) + yield* t.openModal(big.open({ metadata: "x".repeat(Math.max(0, most + 1 - id.length)) })) + yield* refusedTooLarge(t) + })) + + rule("modal/tooManyFields", ["modals"], (t) => + Effect.gen(function*() { + const most = yield* limit(t, "modalFields") + const fields: Record = {} + for (let i = 0; i <= most; i++) fields["f" + i] = ChatInteraction.textInput({ label: "F" + i }) + yield* t.openModal(ChatInteraction.modal("conformance-many", { title: "Conformance", fields }).open()) + yield* refusedTooLarge(t) + })) + + // `modalErrors` means nothing without `modals`, so the rule needs both. + rule("modal/errorsKeepOpen", ["modals", "modalErrors"], (t) => + Effect.gen(function*() { + t.recording.onSubmit = () => + t.recording.submits.length === 1 ? ChatInteraction.errors({ answer: "Again." }) : ChatInteraction.close + yield* t.openModal(probeModal.open()) + yield* t.submit({ answer: "first" }) + yield* t.submit({ answer: "second" }) + const second = t.recording.submits[1] + yield* t.check( + second !== undefined && second.id === "conformance-modal", + "a second submit of the same modal", + () => t.recording.submits.length + " submits" + ) + })) + + rule("unsupported/modals", ["buttons"], (t) => t.probe("modals")) + } + + if (interactions?.command !== undefined) { + const commandId = "conformance-command" + + rule("command/arrives", ["commands"], (t) => + Effect.gen(function*() { + const text = "deploy v1 < \u{1D400}" + yield* t.command(commandId, { text }) + const command = t.recording.commands[0] + yield* t.check( + command !== undefined && command.id === commandId && command.text === text && + ChatThread.equals(command.thread, t.thread), + "command " + commandId + " with text " + JSON.stringify(text) + " in " + ChatThread.toString(t.thread), + () => + command === undefined + ? "no command" + : "command " + command.id + " with text " + JSON.stringify(command.text) + " in " + + ChatThread.toString(command.thread) + ) + yield* t.checkInteraction(command) + })) + + rule("command/emptyText", ["commands"], (t) => + Effect.gen(function*() { + yield* t.command(commandId) + const command = t.recording.commands[0] + yield* t.check( + command !== undefined && command.text === "", + "a command with text \"\"", + () => command === undefined ? "no command" : "text " + JSON.stringify(command.text) + ) + })) + + const noRefusal = (t: Tester) => + t.check( + t.recording.commands.length === 1 && t.recording.refused.length === 0, + "one command and no refusal", + () => + t.recording.commands.length + " commands and refusals " + + (t.recording.refused.length === 0 ? "none" : t.recording.refused.map(describeError).join(", ")) + ) + + rule("command/responds", ["commands"], (t) => + Effect.gen(function*() { + t.recording.onCommand = () => + ChatInteraction.respond({ markdown: conformanceText(t.rule), visibility: "public" }) + yield* t.command(commandId) + yield* noRefusal(t) + })) + + rule("command/respondsPrivately", ["commands", "ephemeral"], (t) => + Effect.gen(function*() { + t.recording.onCommand = () => ChatInteraction.respond({ markdown: conformanceText(t.rule) }) + yield* t.command(commandId) + yield* noRefusal(t) + })) + + rule("command/respondTooLarge", ["commands"], (t) => + Effect.gen(function*() { + const markdown = "x".repeat(t.descriptor.limits.messageLength + 1) + t.recording.onCommand = () => ChatInteraction.respond({ markdown, visibility: "public" }) + yield* t.command(commandId) + const refusal = t.recording.refused[0] + yield* t.check( + refusal !== undefined && textTooLarge.test(refusal.reason), + "a refusal " + textTooLarge.expected, + () => refusal === undefined ? "no refusal" : "a refusal " + describeError(refusal) + ) + })) + + rule("command/followUp", ["commands"], (t) => + Effect.gen(function*() { + t.recording.onCommand = (command) => ChatInteraction.defer(command, { visibility: "public" }) + yield* t.command(commandId) + const deferred = t.recording.commandReplies[0] + if (deferred?._tag !== "Defer") { + return yield* Effect.fail( + failure(t.rule, "a Defer reply", deferred === undefined ? "no command" : deferred._tag) + ) + } + yield* noRefusal(t) + yield* t.call(t.chat.followUp(deferred, { markdown: conformanceText(t.rule) })) + })) + + rule("command/modalOrigin", ["commands", "modals"], (t) => + Effect.gen(function*() { + t.recording.onCommand = () => probeModal.open() + yield* t.command(commandId) + yield* t.submit({ answer: "conformance" }) + const submit = t.recording.submits[0] + yield* t.check( + submit !== undefined && submit.messageId === undefined && ChatThread.equals(submit.thread, t.thread), + "a submit in " + ChatThread.toString(t.thread) + " with no messageId", + () => + submit === undefined + ? "no submit" + : "a submit in " + ChatThread.toString(submit.thread) + " with messageId " + submit.messageId + ) + yield* t.checkInteraction(submit) + })) + + rule("unsupported/ephemeral", ["commands"], (t) => t.probe("ephemeral")) + } + + for (const capability of allCapabilities) { + if (capability === "modals" || capability === "modalErrors" || capability === "ephemeral") continue + rule("unsupported/" + capability, [], (t) => t.probe(capability)) + } + + const failNext = options.failNext + if (failNext !== undefined) { + rule("retries/noneWithoutRetries", [], (t) => + Effect.gen(function*() { + yield* failNext("post", new RateLimited({})) + yield* t.expectReason( + "RateLimited", + Chat.withoutRetries(t.chat.post(t.thread, { markdown: conformanceText(t.rule) })), + (posted) => t.created.push({ thread: t.thread, id: posted.id }) + ) + })) + + rule("retries/retrySafe", [], (t) => + Effect.gen(function*() { + const retryAfter = Duration.millis(100) + yield* failNext("post", new RateLimited({ retryAfter })) + const start = yield* DateTime.now + yield* t.post({}) + const elapsed = DateTime.distance(start, yield* DateTime.now) + yield* t.check( + // One millisecond of slack: a timer and the clock round to whole milliseconds apart. + Duration.toMillis(elapsed) + 1 >= Duration.toMillis(retryAfter), + "a retry after " + Duration.format(retryAfter), + () => "a retry after " + Duration.format(elapsed) + ) + })) + + rule("retries/neverUncertain", [], (t) => + Effect.gen(function*() { + yield* failNext("post", new Transport({ retry: "uncertain" })) + yield* t.expectReason( + "Transport", + t.chat.post(t.thread, { markdown: conformanceText(t.rule) }), + (posted) => t.created.push({ thread: t.thread, id: posted.id }) + ) + })) + } + + return rules +} diff --git a/packages/effect/src/chat/ChatThread.ts b/packages/effect/src/chat/ChatThread.ts new file mode 100644 index 00000000000..92082ef56b8 --- /dev/null +++ b/packages/effect/src/chat/ChatThread.ts @@ -0,0 +1,132 @@ +/** + * Addresses for chat threads. + * + * A `ChatThread` names a channel, a thread or a direct conversation on one + * platform. The address itself does not say which of the three it is: + * `ChatEvent.MessageCreated.kind` and `Chat.getThread` do. The `id` is opaque + * to the core and owned by the platform's adapter. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Effect from "../Effect.ts" +import * as Option from "../Option.ts" +import * as Schema from "../Schema.ts" +import * as SchemaIssue from "../SchemaIssue.ts" +import * as SchemaTransformation from "../SchemaTransformation.ts" + +/** + * Schema for the address messages go to: a channel, a thread or a direct + * conversation on one platform. + * + * **Details** + * + * `id` is opaque to the core and owned by the adapter. Slack's is + * `{team}/{channel}` or `{team}/{channel}/{thread_ts}`, Discord's is the + * channel snowflake. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ChatThread = Schema.Struct({ + platform: Schema.String, + id: Schema.String +}).annotate({ identifier: "ChatThread" }) + +/** + * The address messages go to: a channel, a thread or a direct conversation on + * one platform. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ChatThread = typeof ChatThread.Type + +/** + * Creates a thread address from a platform name and the adapter's id. + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const make = (platform: string, id: string): ChatThread => ({ platform, id }) + +/** + * Returns whether two thread addresses name the same platform and id. + * + * @stability unstable + * @category comparisons + * @since 4.1.5 + */ +export const equals = (self: ChatThread, that: ChatThread): boolean => + self.platform === that.platform && self.id === that.id + +/** + * Formats a thread address as `{platform}:{id}`. + * + * **When to use** + * + * Use to build session keys, entity ids and URLs from an address. + * + * **Gotchas** + * + * The format is frozen once released, because consumers persist it. + * + * @see {@link fromString} for the inverse + * + * @stability unstable + * @category converting + * @since 4.1.5 + */ +export const toString = (thread: ChatThread): string => `${thread.platform}:${thread.id}` + +/** + * Parses a `{platform}:{id}` string into a thread address. + * + * **Details** + * + * The platform is everything before the first `:`, so an id may contain + * `:`. Both parts must be non-empty, otherwise the result is `None`. + * + * @see {@link toString} for the inverse + * + * @stability unstable + * @category converting + * @since 4.1.5 + */ +export const fromString = (value: string): Option.Option => { + const index = value.indexOf(":") + if (index <= 0 || index === value.length - 1) { + return Option.none() + } + return Option.some(make(value.slice(0, index), value.slice(index + 1))) +} + +/** + * Schema that decodes a `{platform}:{id}` string into a `ChatThread` and + * encodes it back. + * + * **Gotchas** + * + * The string format is frozen once released, because consumers persist it. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const FromString: Schema.Codec = Schema.String.pipe( + Schema.decodeTo( + ChatThread, + SchemaTransformation.transformEffect({ + decode: (value, options) => + Option.match(fromString(value), { + onNone: () => + Effect.fail(new SchemaIssue.InvalidValue({ expected: "a {platform}:{id} string" }, value, options)), + onSome: Effect.succeed + }), + encode: (thread) => Effect.succeed(toString(thread)) + }) + ) +) diff --git a/packages/effect/src/chat/ChatUi.ts b/packages/effect/src/chat/ChatUi.ts new file mode 100644 index 00000000000..0c138b00f8f --- /dev/null +++ b/packages/effect/src/chat/ChatUi.ts @@ -0,0 +1,320 @@ +/** + * The plain elements of buttons, selects and modals. + * + * These are what goes on the wire, into `ChatTest` and into storage. Every + * element is a Schema tagged struct and every value in one is already a + * string. `ChatInteraction` holds the typed definitions that build them and + * receive their clicks. A consumer that routes clicks itself builds them with + * each schema's own `make`. + * + * **Details** + * + * - `post` and `edit` take a flat list of `Action`s beside the Markdown, + * and the adapter lays out the rows. + * - Text in a modal is plain, not Markdown, because a Slack modal cannot hold + * a `markdown` block. + * + * @stability unstable + * @since 4.1.5 + */ +import * as Schema from "../Schema.ts" + +/** + * Schema for the style of a button. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ButtonStyle = Schema.Literals(["default", "primary", "danger"]) + +/** + * The style of a button. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ButtonStyle = typeof ButtonStyle.Type + +/** + * Schema for a button on a message. + * + * **Details** + * + * `value` is the encoded value, absent when the definition has no value + * schema. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Button = Schema.TaggedStruct("Button", { + id: Schema.String, + label: Schema.String, + style: Schema.optionalKey(ButtonStyle), + value: Schema.optionalKey(Schema.String) +}) + +/** + * A button on a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Button = typeof Button.Type + +/** + * Schema for a button that opens a URL. + * + * **Details** + * + * It produces no click, so it has no id and no handler. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const LinkButton = Schema.TaggedStruct("LinkButton", { + label: Schema.String, + url: Schema.String +}) + +/** + * A button that opens a URL. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type LinkButton = typeof LinkButton.Type + +/** + * Schema for one option of a select. `value` is the encoded value. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const SelectOption = Schema.Struct({ + label: Schema.String, + value: Schema.String +}) + +/** + * One option of a select. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type SelectOption = typeof SelectOption.Type + +/** + * Schema for a single-choice select on a message. + * + * **Details** + * + * `selected` is the `value` of the option shown as chosen. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Select = Schema.TaggedStruct("Select", { + id: Schema.String, + placeholder: Schema.optionalKey(Schema.String), + options: Schema.Array(SelectOption), + selected: Schema.optionalKey(Schema.String) +}) + +/** + * A single-choice select on a message. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Select = typeof Select.Type + +/** + * Schema for what `post` and `edit` take in `actions`. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Action = Schema.Union([Button, LinkButton, Select]) + +/** + * What `post` and `edit` take in `actions`: a button, a link button or a + * select. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Action = typeof Action.Type + +/** + * Schema for a text input in a modal, one line or several. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const TextInput = Schema.TaggedStruct("TextInput", { + id: Schema.String, + label: Schema.String, + multiline: Schema.optionalKey(Schema.Boolean), + placeholder: Schema.optionalKey(Schema.String), + initial: Schema.optionalKey(Schema.String), + optional: Schema.optionalKey(Schema.Boolean), + minLength: Schema.optionalKey(Schema.Int), + maxLength: Schema.optionalKey(Schema.Int) +}) + +/** + * A text input in a modal. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type TextInput = typeof TextInput.Type + +/** + * Schema for a single-choice select in a modal. + * + * **Details** + * + * Both `ChatInteraction.choice` and `ChatInteraction.selectInput` fields + * become this. `initial` is the `value` of the option chosen when the modal + * opens. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const SelectInput = Schema.TaggedStruct("SelectInput", { + id: Schema.String, + label: Schema.String, + placeholder: Schema.optionalKey(Schema.String), + options: Schema.Array(SelectOption), + initial: Schema.optionalKey(Schema.String), + optional: Schema.optionalKey(Schema.Boolean) +}) + +/** + * A single-choice select in a modal. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type SelectInput = typeof SelectInput.Type + +/** + * Schema for plain text in a modal. + * + * **Details** + * + * Not Markdown: a Slack modal cannot hold a `markdown` block. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Text = Schema.TaggedStruct("Text", { + text: Schema.String +}) + +/** + * Plain text in a modal. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Text = typeof Text.Type + +/** + * Schema for one element of a modal. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const ModalElement = Schema.Union([TextInput, SelectInput, Text]) + +/** + * One element of a modal: a text input, a select or plain text. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type ModalElement = typeof ModalElement.Type + +/** + * Schema for a modal. + * + * **Details** + * + * - `metadata` is the consumer's encoded metadata. The adapter adds the + * thread and message of the click that opened the modal beside it. + * - The elements are shown in order. + * + * @stability unstable + * @category schemas + * @since 4.1.5 + */ +export const Modal = Schema.TaggedStruct("Modal", { + id: Schema.String, + title: Schema.String, + submitLabel: Schema.optionalKey(Schema.String), + metadata: Schema.optionalKey(Schema.String), + elements: Schema.Array(ModalElement) +}) + +/** + * A modal. + * + * @stability unstable + * @category models + * @since 4.1.5 + */ +export type Modal = typeof Modal.Type + +/** + * Creates a button that opens a URL. + * + * **When to use** + * + * Use to put a link beside the buttons of a message. It is the one element + * with no definition in `ChatInteraction`, since it produces no click. + * + * **Example** (Posting a link button) + * + * ```ts + * import { Effect } from "effect" + * import { Chat, ChatThread, ChatUi } from "effect/chat" + * + * const program = Effect.gen(function*() { + * const chat = yield* Chat.Chat + * yield* chat.post(ChatThread.make("slack", "T1/C1"), { + * markdown: "The diff is ready.", + * actions: [ChatUi.linkButton({ label: "Diff", url: "https://example.com/diff" })] + * }) + * }) + * ``` + * + * @stability unstable + * @category constructors + * @since 4.1.5 + */ +export const linkButton = (options: { + readonly label: string + readonly url: string +}): LinkButton => LinkButton.make({ label: options.label, url: options.url }) diff --git a/packages/effect/src/chat/index.ts b/packages/effect/src/chat/index.ts new file mode 100644 index 00000000000..ab206ecce22 --- /dev/null +++ b/packages/effect/src/chat/index.ts @@ -0,0 +1,72 @@ +/** + * @stability unstable + * @since 4.1.5 + */ + +// @barrel: Auto-generated exports. Do not edit manually. + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as Chat from "./Chat.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatAdapter from "./ChatAdapter.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatError from "./ChatError.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatEvent from "./ChatEvent.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatHandler from "./ChatHandler.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatInteraction from "./ChatInteraction.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatMarkdown from "./ChatMarkdown.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatMessage from "./ChatMessage.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatTest from "./ChatTest.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatThread from "./ChatThread.ts" + +/** + * @stability unstable + * @since 4.1.5 + */ +export * as ChatUi from "./ChatUi.ts" diff --git a/packages/effect/src/chat/internal/retries.ts b/packages/effect/src/chat/internal/retries.ts new file mode 100644 index 00000000000..2782f492e79 --- /dev/null +++ b/packages/effect/src/chat/internal/retries.ts @@ -0,0 +1,7 @@ +import * as Context from "../../Context.ts" +import { constFalse } from "../../Function.ts" + +/** @internal */ +export const RetriesDisabled = Context.Reference("effect/chat/Chat/RetriesDisabled", { + defaultValue: constFalse +}) diff --git a/packages/effect/src/jose/Jwks.ts b/packages/effect/src/jose/Jwks.ts index e03c64a1318..5d5d94a60ba 100644 --- a/packages/effect/src/jose/Jwks.ts +++ b/packages/effect/src/jose/Jwks.ts @@ -228,6 +228,17 @@ export interface RemoteOptions { * Maximum number of keys in the set. Defaults to 64. */ readonly maxKeys?: number | undefined + /** + * Keeps only the raw keys it returns `true` for, before the set is decoded. + * + * **Details** + * + * It sees each member of `keys` as the endpoint sent it, with members such + * as `x5c` or a provider's own `endorsements` that decoding drops. Keys + * that are not JSON objects are dropped without a call. `maxKeys` counts + * the set before filtering, so a filter cannot raise the limit. + */ + readonly filter?: ((jwk: { readonly [key: string]: unknown }) => boolean) | undefined } interface Snapshot { @@ -302,6 +313,7 @@ export const makeRemote = Effect.fnUntraced(function*( const timeout = yield* positiveMillis("timeout", options.timeout ?? "5 seconds") const maxResponseBytes = yield* positiveInteger("maxResponseBytes", options.maxResponseBytes ?? 131072) const maxKeys = yield* positiveInteger("maxKeys", options.maxKeys ?? 64) + const filter = options.filter const client = HttpClient.withScope(yield* HttpClient.HttpClient) const scope = yield* Effect.scope @@ -337,12 +349,18 @@ export const makeRemote = Effect.fnUntraced(function*( body.set(chunk, length) return Effect.succeed(length + chunk.length) }) - const keySet = yield* Schema.decodeUnknownEffect(KeySet)(parseJsonObject(body.subarray(0, length))).pipe( - Effect.mapError((cause) => unavailable("The key set document is invalid", cause)) - ) - if (keySet.keys.length > maxKeys) { + const document = parseJsonObject(body.subarray(0, length)) + const rawKeys = document?.keys + if (Array.isArray(rawKeys) && rawKeys.length > maxKeys) { return yield* unavailable(`The key set has more than ${maxKeys} keys`) } + const filtered = filter === undefined || !Array.isArray(rawKeys) ? document : { + ...document, + keys: rawKeys.filter((jwk) => typeof jwk === "object" && jwk !== null && !Array.isArray(jwk) && filter(jwk)) + } + const keySet = yield* Schema.decodeUnknownEffect(KeySet)(filtered).pipe( + Effect.mapError((cause) => unavailable("The key set document is invalid", cause)) + ) return { keys: supportedKeys(keySet), expiresAt: (yield* Clock.currentTimeMillis) + timeToLive } }).pipe( Effect.scoped, diff --git a/packages/effect/test/chat/Chat.test.ts b/packages/effect/test/chat/Chat.test.ts new file mode 100644 index 00000000000..6989bcd66e8 --- /dev/null +++ b/packages/effect/test/chat/Chat.test.ts @@ -0,0 +1,52 @@ +import { assert, describe, it } from "@effect/vitest" +import { Effect, Layer, Tracer } from "effect" +import { Chat, ChatTest, ChatThread } from "effect/chat" + +const recordSpans = () => { + const spans: Array = [] + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options) + spans.push(span) + return span + } + }) + return { spans, tracer } +} + +describe("Chat", () => { + it.effect("routes each address to its platform's adapter", () => + Effect.gen(function*() { + const chat = yield* Chat.Chat + const test = yield* ChatTest.ChatTest + const b = yield* test.makeThread() + yield* chat.post(b, { markdown: "to b" }) + const missing = yield* Effect.flip(chat.post(ChatThread.make("c", "x"), { markdown: "nowhere" })) + assert.strictEqual(missing.reason._tag, "NoAdapter") + assert.deepStrictEqual((yield* test.messages(b)).map((message) => message.text), ["to b"]) + }).pipe(Effect.provide(Layer.mergeAll(ChatTest.layer({ platform: "a" }), ChatTest.layer({ platform: "b" }))))) + + it.effect("sets only platform, op, error reason and retry value on its spans", () => + Effect.gen(function*() { + const { spans, tracer } = recordSpans() + const chat = yield* Chat.Chat + const test = yield* ChatTest.ChatTest + const thread = yield* test.makeThread() + yield* Effect.gen(function*() { + const posted = yield* chat.post(thread, { markdown: "hello" }) + yield* Effect.exit(chat.fetchMessage(thread, posted.id + "0")) + }).pipe(Effect.withTracer(tracer)) + assert.deepStrictEqual( + spans.map((span) => [span.name, Object.fromEntries(span.attributes)]), + [ + ["Chat.post", { "chat.platform": "test", "chat.op": "post" }], + ["Chat.fetchMessage", { + "chat.platform": "test", + "chat.op": "fetchMessage", + "chat.error.reason": "NotFound", + "chat.error.retry": "denied" + }] + ] + ) + }).pipe(Effect.provide(ChatTest.layer()))) +}) diff --git a/packages/effect/test/chat/ChatError.test.ts b/packages/effect/test/chat/ChatError.test.ts new file mode 100644 index 00000000000..f4644a8da73 --- /dev/null +++ b/packages/effect/test/chat/ChatError.test.ts @@ -0,0 +1,70 @@ +import { assert, describe, it } from "@effect/vitest" +import { Duration, Effect, Schema } from "effect" +import { ChatError } from "effect/chat" + +const cause = new Error("https://example.com/files/secret.txt") + +const reasons: ReadonlyArray = [ + new ChatError.RateLimited({ retryAfter: Duration.seconds(3), detail: "ratelimited", cause }), + new ChatError.Unsupported({ capability: "edit", detail: "no_edit", cause }), + new ChatError.Transport({ retry: "uncertain", detail: "timeout", cause }), + new ChatError.PlatformUnavailable({ retry: "retry", detail: "503", cause }), + new ChatError.NoAdapter({ detail: "none", cause }), + new ChatError.Unauthenticated({ detail: "invalid_auth", cause }), + new ChatError.Forbidden({ detail: "not_in_channel", cause }), + new ChatError.NotFound({ detail: "channel_not_found", cause }), + new ChatError.TooLarge({ detail: "msg_too_long", cause }), + new ChatError.InvalidRequest({ detail: "invalid_blocks", cause }), + new ChatError.InvalidResponse({ detail: "bad_json", cause }), + new ChatError.Unknown({ detail: "unknown", cause }) +] + +describe("ChatError", () => { + it("fixes the retry value per reason", () => { + assert.deepStrictEqual( + reasons.map((reason) => reason.retry), + [ + "retry", + "denied", + "uncertain", + "retry", + "denied", + "denied", + "denied", + "denied", + "denied", + "denied", + "denied", + "uncertain" + ] + ) + }) + + it.effect("round-trips every reason through its Schema, cause included", () => + Effect.gen(function*() { + const codec = Schema.toCodecJson(ChatError.ChatError) + for (const reason of reasons) { + const error = new ChatError.ChatError({ platform: "slack", op: "post", reason }) + const decoded = yield* Schema.decodeEffect(codec)(yield* Schema.encodeEffect(codec)(error)) + assert.strictEqual(decoded.reason._tag, reason._tag) + assert.strictEqual(decoded.reason.detail, reason.detail) + assert.strictEqual(decoded.retry, error.retry) + assert.instanceOf(decoded.reason.cause, Error) + assert.strictEqual((decoded.reason.cause as Error).message, cause.message) + } + })) + + it("leaves cause out of toJSON and message", () => { + for (const reason of reasons) { + const error = new ChatError.ChatError({ platform: "slack", op: "post", reason }) + assert.notInclude(JSON.stringify(error), "secret") + assert.notInclude(JSON.stringify(reason), "secret") + assert.notInclude(error.message, "secret") + } + }) + + it("delegates retryAfter to the reason", () => { + const error = new ChatError.ChatError({ platform: "slack", op: "post", reason: reasons[0] }) + assert.deepStrictEqual(error.retryAfter, Duration.seconds(3)) + }) +}) diff --git a/packages/effect/test/chat/ChatEvent.test.ts b/packages/effect/test/chat/ChatEvent.test.ts new file mode 100644 index 00000000000..788bcbe9fa6 --- /dev/null +++ b/packages/effect/test/chat/ChatEvent.test.ts @@ -0,0 +1,23 @@ +import { assert, describe, it } from "@effect/vitest" +import * as ChatEvent from "effect/chat/ChatEvent" + +const thread = { platform: "slack", id: "T1/C1/1794454017.568519" } + +describe("ChatEvent", () => { + describe("StopRequested", () => { + const event: ChatEvent.StopRequested = { + _tag: "StopRequested", + thread, + id: "1794454026.522466", + userId: "U1", + messageIds: ["1794454017.726189"], + raw: { event: { type: "agent_session_stopped" } } + } + + it("dedupeKey is stop/{thread}/{id}", () => { + assert.strictEqual(ChatEvent.dedupeKey(event), "stop/slack:T1/C1/1794454017.568519/1794454026.522466") + // A redelivery has the same key, whatever streams it lists. + assert.strictEqual(ChatEvent.dedupeKey({ ...event, messageIds: [] }), ChatEvent.dedupeKey(event)) + }) + }) +}) diff --git a/packages/effect/test/chat/ChatInteraction.test.ts b/packages/effect/test/chat/ChatInteraction.test.ts new file mode 100644 index 00000000000..3ccc1e4c6fc --- /dev/null +++ b/packages/effect/test/chat/ChatInteraction.test.ts @@ -0,0 +1,298 @@ +import { assert, describe, it } from "@effect/vitest" +import { Deferred, Duration, Effect, Exit, Fiber, Layer, Option, Scheduler, Schema } from "effect" +import { Chat, ChatError, ChatInteraction, ChatTest, ChatThread } from "effect/chat" +import { TestClock } from "effect/testing" + +const Deploy = Schema.Struct({ version: Schema.String }) +const thread = ChatThread.make("test", "c1") +const origin = { thread, messageId: "m1", userId: "u1", raw: null } + +const clicked = (id: string, value?: string) => + ChatInteraction.ButtonClicked.make({ id, ...origin, ...(value === undefined ? {} : { value }) }) + +const invoked = (id: string, text = "") => + ChatInteraction.CommandInvoked.make({ id, text, thread, userId: "u1", raw: { id: "i1" } }) + +const handlerWith = (options: { + readonly click?: Effect.Effect + readonly submit?: Effect.Effect + readonly command?: (command: ChatInteraction.CommandInvoked) => ChatInteraction.CommandReply + readonly refused?: (error: ChatError.ChatError) => Effect.Effect +}) => + ChatInteraction.Handler.of({ + click: () => options.click ?? Effect.succeed(ChatInteraction.ack), + submit: () => options.submit ?? Effect.succeed(ChatInteraction.close), + command: (command) => Effect.succeed(options.command?.(command) ?? ChatInteraction.ack), + refused: (_, error) => options.refused?.(error) ?? Effect.void + }) + +describe("ChatInteraction", () => { + it("encodes a value by its schema: a string-only schema as it is, anything else as JSON", () => { + const StringOrNumber = Schema.Union([Schema.String, Schema.Number]) + assert.strictEqual(ChatInteraction.encodeValue(Schema.String, "v1.4.2"), "v1.4.2") + assert.deepStrictEqual(ChatInteraction.decodeValue(Schema.String, "\"quoted\""), Option.some("\"quoted\"")) + assert.strictEqual(ChatInteraction.encodeValue(StringOrNumber, "123"), "\"123\"") + assert.deepStrictEqual(ChatInteraction.decodeValue(StringOrNumber, "123"), Option.some(123)) + }) + + it("modal.open builds the elements in field order, with encoded metadata and options", () => { + const Reason = ChatInteraction.modal("reason", { + title: "Why", + metadata: Deploy, + fields: { + intro: ChatInteraction.text("Tell us."), + reason: ChatInteraction.textInput({ label: "Reason", multiline: true, maxLength: 200 }), + env: ChatInteraction.selectInput({ label: "Env", value: Schema.Number, optional: true }) + } + }) + const opened = Reason.open({ + metadata: { version: "v1" }, + options: { env: [{ label: "Prod", value: 1 }] }, + initial: { env: 1 } + }) + assert.deepStrictEqual(opened.modal, { + _tag: "Modal", + id: "reason", + title: "Why", + metadata: "{\"version\":\"v1\"}", + elements: [ + { _tag: "Text", text: "Tell us." }, + { _tag: "TextInput", id: "reason", label: "Reason", multiline: true, maxLength: 200 }, + { + _tag: "SelectInput", + id: "env", + label: "Env", + options: [{ label: "Prod", value: "1" }], + initial: "1", + optional: true + } + ] + }) + }) + + describe("layer and groups", () => { + const Approve = ChatInteraction.button("approve", { label: "Approve", value: Deploy }) + const Reason = ChatInteraction.modal("reason", { + title: "Why", + metadata: Deploy, + fields: { + reason: ChatInteraction.textInput({ label: "Reason" }), + severity: ChatInteraction.choice({ label: "Severity", options: [{ label: "Low", value: "low" }] }), + note: ChatInteraction.textInput({ label: "Note", optional: true }) + } + }) + const Ship = ChatInteraction.command("ship", { + description: "Ship a version", + argument: { name: "version", description: "The version" } + }) + const seen: Array = [] + const unhandled: Array = [] + const Interactions = ChatInteraction.group(Approve).add(Reason, Ship).toLayer({ + ship: (command) => Effect.succeed(ChatInteraction.respond({ markdown: "Shipping " + command.text })), + approve: (click) => + Effect.sync(() => seen.push(click.value)).pipe(Effect.as(Reason.open({ metadata: click.value }))), + reason: (submit) => + Effect.sync(() => { + seen.push({ metadata: submit.metadata, values: submit.values }) + return submit.values.reason.length < 3 ? ChatInteraction.errors({ reason: "Longer." }) : ChatInteraction.close + }) + }).pipe(Layer.provideMerge(ChatInteraction.layer({ + unhandled: (event, why) => Effect.sync(() => void unhandled.push(event.id + ":" + why.reason)) + }))) + + it.effect("routes a click and a submit to the typed handlers", () => + Effect.gen(function*() { + seen.length = 0 + const handler = yield* ChatInteraction.Handler + const reply = yield* handler.click(clicked("approve", Approve.of({ version: "v1" }).value)) + assert.strictEqual(reply._tag, "OpenModal") + const submitted = ChatInteraction.ModalSubmitted.make({ + id: "reason", + metadata: (reply as ChatInteraction.OpenModal).modal.metadata!, + values: { reason: "ok", severity: "low" }, + ...origin + }) + assert.deepStrictEqual(yield* handler.submit(submitted), ChatInteraction.errors({ reason: "Longer." })) + assert.deepStrictEqual(seen, [ + { version: "v1" }, + { metadata: { version: "v1" }, values: { reason: "ok", severity: "low", note: undefined } } + ]) + }).pipe(Effect.provide(Interactions))) + + it.effect("routes a command to its handler, and answers an unknown one Ack", () => + Effect.gen(function*() { + unhandled.length = 0 + const handler = yield* ChatInteraction.Handler + assert.deepStrictEqual(yield* handler.command(invoked("ship", "v1")), { + _tag: "Respond", + markdown: "Shipping v1", + visibility: "ephemeral" + }) + assert.deepStrictEqual(yield* handler.command(invoked("nope")), ChatInteraction.ack) + // A command and a button share one id space: a command named like a button is not routed to it. + assert.deepStrictEqual(yield* handler.command(invoked("approve")), ChatInteraction.ack) + assert.deepStrictEqual(unhandled, ["nope:UnknownId", "approve:UnknownId"]) + }).pipe(Effect.provide(Interactions))) + + it.effect("answers an unknown id or a value that no longer decodes without a handler", () => + Effect.gen(function*() { + seen.length = 0 + unhandled.length = 0 + const handler = yield* ChatInteraction.Handler + assert.deepStrictEqual(yield* handler.click(clicked("nope")), ChatInteraction.ack) + assert.deepStrictEqual(yield* handler.click(clicked("approve", "{")), ChatInteraction.ack) + assert.deepStrictEqual(yield* handler.click(clicked("reason")), ChatInteraction.ack) + const badChoice = ChatInteraction.ModalSubmitted.make({ + id: "reason", + metadata: "{\"version\":\"v1\"}", + values: { reason: "long enough", severity: "high" }, + ...origin + }) + assert.deepStrictEqual(yield* handler.submit(badChoice), ChatInteraction.close) + assert.deepStrictEqual(seen, []) + assert.deepStrictEqual(unhandled, [ + "nope:UnknownId", + "approve:InvalidValue", + "reason:UnknownId", + "reason:InvalidValue" + ]) + }).pipe(Effect.provide(Interactions))) + + it.effect("dies on an id registered twice", () => + Effect.gen(function*() { + const twice = ChatInteraction.group(Approve).toLayer({ approve: () => Effect.succeed(ChatInteraction.ack) }) + const exit = yield* Effect.exit(Layer.build(twice.pipe(Layer.provideMerge(Interactions)))) + assert.isTrue(exit._tag === "Failure") + }).pipe(Effect.scoped)) + }) + + describe("handle", () => { + const deadline = Duration.seconds(3) + + it.effect("interrupts the handler when cancelled during startup", () => + Effect.gen(function*() { + const release = yield* Deferred.make() + yield* Effect.gen(function*() { + // Cancel at successive scheduler yields, including between fork and cleanup registration. + for (let budget = 1; budget <= 40; budget++) { + let started = false + let stopped = false + const handler = handlerWith({ + click: Effect.acquireUseRelease( + Effect.sync(() => { + started = true + }), + () => Deferred.await(release), + () => + Effect.sync(() => { + stopped = true + }) + ) + }) + const fiber = yield* ChatInteraction.handle(clicked("a"), deadline).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.provideService(Scheduler.MaxOpsBeforeYield, budget), + Effect.forkChild({ startImmediately: true }) + ) + yield* Fiber.interrupt(fiber) + yield* TestClock.adjust("1 millis") + assert.isFalse(started && !stopped, `MaxOpsBeforeYield ${budget}`) + } + }).pipe(Effect.ensuring(Deferred.succeed(release, ChatInteraction.ack))) + })) + + it.effect("gives Late for a late click, and refuses its modal with interaction_expired", () => + Effect.gen(function*() { + const refused = yield* Deferred.make() + const opened = ChatInteraction.modal("m", { title: "M", fields: {} }).open() + const handler = handlerWith({ + click: Effect.as(Effect.sleep("5 seconds"), opened), + refused: (error) => Deferred.succeed(refused, error).pipe(Effect.asVoid) + }) + const fiber = yield* ChatInteraction.handle(clicked("a"), deadline).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.forkChild + ) + yield* TestClock.adjust("3 seconds") + const reply = yield* Fiber.join(fiber) + assert.isTrue(Schema.is(ChatInteraction.Late)(reply)) + // The click's handler is still running, and its modal can no longer open. + yield* TestClock.adjust("2 seconds") + const error = yield* Deferred.await(refused) + assert.strictEqual(error.op, "openModal") + assert.strictEqual(error.platform, "test") + assert.strictEqual(error.reason._tag, "NotFound") + assert.strictEqual(error.reason.detail, ChatError.interactionExpired) + })) + + it.effect("gives Late at the deadline without waiting for a late submit's cleanup", () => + Effect.gen(function*() { + const cleaned = yield* Deferred.make() + const handler = handlerWith({ + submit: Effect.never.pipe( + Effect.onInterrupt(() => Effect.andThen(Effect.sleep("5 seconds"), Deferred.succeed(cleaned, undefined))) + ) + }) + const submitted = ChatInteraction.ModalSubmitted.make({ id: "m", values: {}, ...origin }) + const fiber = yield* ChatInteraction.handle(submitted, deadline).pipe( + Effect.provideService(ChatInteraction.Handler, handler), + Effect.forkChild + ) + yield* TestClock.adjust("3 seconds") + const exit = fiber.pollUnsafe() + assert.isDefined(exit) + assert.isTrue(Exit.isSuccess(exit!) && Schema.is(ChatInteraction.Late)(exit.value)) + // The interrupt was requested, and the cleanup still runs to its end. + assert.isFalse(yield* Deferred.isDone(cleaned)) + yield* TestClock.adjust("5 seconds") + yield* Deferred.await(cleaned) + })) + }) + + describe("ChatTest.interact", () => { + it.effect("follows up a deferral once, inside followUpAge", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const chat = yield* Chat.Chat + const channel = yield* test.makeThread() + const handler = handlerWith({ command: (command) => ChatInteraction.defer(command) }) + const interact = (command: ChatInteraction.CommandInvoked) => + Effect.provideService(test.interact(command), ChatInteraction.Handler, handler) + const first = yield* interact(yield* test.command(channel, "summarize")) + yield* chat.followUp(first as ChatInteraction.Defer, { markdown: "Done" }) + const twice = yield* Effect.flip(chat.followUp(first as ChatInteraction.Defer, { markdown: "Again" })) + assert.strictEqual(twice.reason.detail, ChatError.interactionExpired) + const second = yield* interact(yield* test.command(channel, "summarize")) + yield* TestClock.adjust("15 minutes") + const expired = yield* Effect.flip(chat.followUp(second as ChatInteraction.Defer, { markdown: "Late" })) + assert.strictEqual(expired.reason._tag, "NotFound") + assert.strictEqual(expired.reason.detail, ChatError.interactionExpired) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("refuses a private answer without ephemeral, and sends nothing", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const channel = yield* test.makeThread() + const refused: Array = [] + const handler = handlerWith({ + command: (command) => + command.text === "defer" ? ChatInteraction.defer(command) : ChatInteraction.respond({ markdown: "Secret" }), + refused: (error) => Effect.sync(() => void refused.push(error)) + }) + for (const text of ["respond", "defer"]) { + yield* Effect.provideService( + test.interact(yield* test.command(channel, "secret", { text })), + ChatInteraction.Handler, + handler + ) + } + assert.deepStrictEqual( + refused.map((error) => error.reason._tag === "Unsupported" && error.reason.capability), + ["ephemeral", "ephemeral"] + ) + assert.deepStrictEqual((yield* test.calls).map((call) => [call.op, call.outcome]), [["respond", "Unsupported"]]) + }).pipe(Effect.provide(ChatTest.layer({ + capabilities: ChatError.Capability.literals.filter((capability) => capability !== "ephemeral") + })))) + }) +}) diff --git a/packages/effect/test/chat/ChatMarkdown.test.ts b/packages/effect/test/chat/ChatMarkdown.test.ts new file mode 100644 index 00000000000..93443eaa0be --- /dev/null +++ b/packages/effect/test/chat/ChatMarkdown.test.ts @@ -0,0 +1,623 @@ +import { assert, describe, it } from "@effect/vitest" +import { type Cause, Deferred, Duration, Effect, Fiber, Queue, Stream } from "effect" +import { Chat, ChatAdapter, ChatError, ChatInteraction, ChatMarkdown, ChatTest } from "effect/chat" +import type { ChatThread, ChatUi } from "effect/chat" +import { TestClock } from "effect/testing" + +const codePoints = (markdown: string) => [...markdown].length + +const joined = (parts: ReadonlyArray) => parts.map((part) => part.text).join("") + +const paragraphs = (count: number, size: number) => + Array.from({ length: count }, (_, i) => ("Paragraph " + i + " ").padEnd(size, "x")).join("\n\n") + +const settle = Effect.forEach(Array.from({ length: 50 }), () => Effect.yieldNow, { discard: true }) + +const setup = Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const thread = yield* test.makeThread() + return { test, thread } +}) + +const texts = (test: ChatTest.ChatTest["Service"], thread: ChatThread.ChatThread) => + Effect.map(test.messages(thread), (messages) => messages.map((message) => message.text)) + +/** Runs `stream` over a queue the test feeds, ends or fails. */ +const feed = Effect.fnUntraced(function*( + thread: ChatThread.ChatThread, + options?: ChatMarkdown.Options & { readonly recipient?: string | undefined } +) { + const queue = yield* Queue.unbounded() + const fiber = yield* Effect.forkChild(ChatMarkdown.stream(thread, Stream.fromQueue(queue), options)) + const send = (chunk: string) => Effect.andThen(Queue.offer(queue, chunk), settle) + const fail = (error: string) => Effect.andThen(Queue.fail(queue, error), settle) + const close = Effect.andThen(Queue.end(queue), settle) + const end = Effect.andThen(Queue.end(queue), Fiber.join(fiber)) + return { fiber, send, fail, close, end } +}) + +/** Registers the test adapter with its native stream changed by `f`. */ +const patchNative = Effect.fnUntraced(function*( + f: (native: ChatAdapter.NativeStream) => Partial +) { + const test = yield* ChatTest.ChatTest + const registry = yield* ChatAdapter.Registry + const adapter = yield* registry.get(test.platform) + const native = adapter.nativeStream! + yield* registry.register({ ...adapter, nativeStream: { ...native, ...f(native) } }) +}) + +/** Holds the first native `append` or `stop` until `release` completes. */ +const holdFirst = Effect.fnUntraced(function*(op: "append" | "stop") { + const entered = yield* Deferred.make() + const release = yield* Deferred.make() + let first = true + const hold = (effect: Effect.Effect) => + Effect.suspend(() => { + if (!first) return effect + first = false + return Deferred.succeed(entered, undefined).pipe(Effect.andThen(Deferred.await(release)), Effect.andThen(effect)) + }) + yield* patchNative((native) => + op === "append" + ? { append: (...args) => hold(native.append(...args)) } + : { stop: (...args) => hold(native.stop(...args)) } + ) + return { entered, release } +}) + +describe("ChatMarkdown", () => { + describe("post", () => { + it.effect("splits at block boundaries", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const input = paragraphs(10, 40) + const result = yield* ChatMarkdown.post(thread, input, { maxParts: 10 }) + assert.isTrue(result.parts.length > 1) + assert.strictEqual(joined(result.parts), input) + for (const part of result.parts) assert.isAtMost(codePoints(part.markdown), 100) + for (const part of result.parts.slice(0, -1)) assert.isTrue(part.text.endsWith("\n\n"), part.text) + assert.deepStrictEqual(yield* texts(test, thread), result.parts.map((part) => part.markdown)) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 100 } })))) + + it.effect("closes a code fence and reopens it in the next part", () => + Effect.gen(function*() { + const { thread } = yield* setup + const input = "```ts\n" + "const a = 1\n".repeat(10) + "```\n" + const result = yield* ChatMarkdown.post(thread, input) + assert.isTrue(result.parts.length > 1) + assert.strictEqual(joined(result.parts), input) + assert.isTrue(result.parts[0].markdown.endsWith("\n```"), result.parts[0].markdown) + for (const part of result.parts.slice(1)) assert.isTrue(part.markdown.startsWith("```ts\n"), part.markdown) + for (const part of result.parts) assert.isAtMost(codePoints(part.markdown), 60) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 60 } })))) + + it.effect("replies with the first part only", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const event = yield* test.userPosts(thread, { text: "question" }) + assert(event._tag === "MessageCreated") + const replyTo = { messageId: event.message.id } + const result = yield* ChatMarkdown.post(thread, paragraphs(3, 20), { replyTo }) + const messages = yield* test.messages(thread) + const replies = result.parts.map((part) => messages.find((message) => message.id === part.messageId)?.replyTo) + assert.deepStrictEqual(replies.map((reply) => reply?.messageId), [event.message.id, undefined, undefined]) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 } })))) + + it.effect("remainingFence is the fence remaining was cut in, and reopening it gives the rest back", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const code = Array.from({ length: 6 }, (_, i) => "const line" + i + " = " + i).join("\n") + const input = "Intro\n\n~~~ts title\n" + code + "\n~~~\n\nOutro" + const error = yield* Effect.flip(ChatMarkdown.post(thread, input, { maxParts: 2 })) + assert.strictEqual(error.cause._tag, "Overflow") + assert.strictEqual(joined(error.posted) + error.remaining, input) + assert.isTrue(error.remaining.startsWith("const line"), error.remaining) + assert.strictEqual(error.remainingFence, "~~~ts title") + // Sent again with the fence reopened, the thread reads as the input does. + yield* ChatMarkdown.post(thread, error.remainingFence + "\n" + error.remaining) + const sent = yield* texts(test, thread) + assert.isTrue(sent.every((text) => (text.match(/^~~~/gm) ?? []).length % 2 === 0), sent.join("\n---\n")) + assert.deepStrictEqual( + sent.flatMap((text) => text.split("\n")).filter((line) => line.startsWith("const line")), + code.split("\n") + ) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 60 } })))) + + const files = [{ name: "a.txt", contentType: "text/plain", content: new TextEncoder().encode("a") }] + + it.effect("ends in the unattached note when the files are refused", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("post", new ChatError.Forbidden({ detail: "file_refused" })) + const result = yield* ChatMarkdown.post(thread, "Report", { + files, + unattachedNote: (files) => "\n\n(" + files.length + " file not attached)" + }) + assert.deepStrictEqual(result.parts.map((part) => [part.markdown, part.text]), [[ + "Report\n\n(1 file not attached)", + "Report" + ]]) + const messages = yield* test.messages(thread) + assert.deepStrictEqual(messages.map((message) => message.attachments.length), [0]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("fails on a TooLarge that does not blame the files", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("post", new ChatError.TooLarge({ detail: "msg_blocks_too_long" })) + const error = yield* Effect.flip(ChatMarkdown.post(thread, "Report", { + files, + unattachedNote: () => " (file not attached)" + })) + assert.strictEqual(error.cause._tag === "Chat" && error.cause.error.reason._tag, "TooLarge") + assert.deepStrictEqual([error.posted, error.uncertain, error.remaining], [[], undefined, "Report"]) + assert.deepStrictEqual(yield* texts(test, thread), []) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("does not post a note for an uncertain file write", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("post", new ChatError.Transport({ retry: "uncertain", detail: "file_refused" }), { + landed: true + }) + const error = yield* Effect.flip(ChatMarkdown.post(thread, "Report", { + files, + unattachedNote: () => " (file not attached)" + })) + assert.deepStrictEqual([error.posted, error.uncertain, error.remaining], [[], "Report", ""]) + assert.deepStrictEqual(yield* texts(test, thread), ["Report"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("keeps the files and blank text that follow the last cut", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const input = "abcde\n\n" + const result = yield* ChatMarkdown.post(thread, input, { files }) + assert.deepStrictEqual(result.parts.map((part) => [part.markdown, part.text]), [["abcde", input]]) + const messages = yield* test.messages(thread) + assert.deepStrictEqual(messages.map((message) => message.attachments.length), [1]) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 5 } })))) + + it.effect("preserves overflow as text when files are refused", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.refuseFiles({ times: 2 }) + const input = paragraphs(5, 20) + const result = yield* ChatMarkdown.post(thread, input, { + files, + maxParts: 1, + overflow: { file: { name: "rest.md" } }, + unattachedNote: () => " (not attached)" + }) + assert.strictEqual(joined(result.parts), input) + const messages = yield* test.messages(thread) + assert.strictEqual(messages.map((message) => message.text).join(""), input + " (not attached)") + assert.isTrue(messages.every((message) => message.attachments.length === 0)) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 } })))) + + it.effect("sends the files with the overflow file", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const input = paragraphs(5, 20) + const result = yield* ChatMarkdown.post(thread, input, { + files, + maxParts: 2, + overflow: { file: { name: "rest.md" } } + }) + assert.deepStrictEqual(result.parts.map((part) => part.kind), ["text", "text", "overflowFile"]) + assert.strictEqual(joined(result.parts), input) + const last = (yield* test.messages(thread)).at(-1)! + assert.deepStrictEqual(last.attachments.map((attachment) => attachment.name), ["rest.md", "a.txt"]) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 } })))) + + const button = ChatInteraction.button("go", { label: "Go" }).of() + /** One element costs `cost`, so a test can leave as little room beside it as it needs. */ + const costing = (cost: number) => (markdown: string, actions?: ReadonlyArray) => + codePoints(markdown) + cost * (actions?.length ?? 0) + + it.effect("fails TooLarge before posting when the actions alone do not fit", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const error = yield* Effect.flip(ChatMarkdown.post(thread, "a", { actions: [button] })) + assert.strictEqual(error.cause._tag === "Chat" && error.cause.error.reason._tag, "TooLarge") + assert.deepStrictEqual(yield* test.messages(thread), []) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 9 }, measure: costing(10) })))) + + it.effect("fills the parts before the last one without leaving room for the actions", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const result = yield* ChatMarkdown.post(thread, "a".repeat(25), { actions: [button] }) + assert.deepStrictEqual(result.parts.map((part) => part.text), ["a".repeat(24), "a"]) + yield* test.click(result.parts[1].messageId, "go") + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 }, measure: costing(29) })))) + + it.effect("posts the actions in a message of their own when no text fits beside them", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const result = yield* ChatMarkdown.post(thread, "Hello", { actions: [button], files }) + assert.deepStrictEqual(result.parts.map((part) => part.text), ["Hello", ""]) + const messages = yield* test.messages(thread) + assert.deepStrictEqual(messages.map((message) => [message.text, message.attachments.length]), [ + ["Hello", 0], + ["", 1] + ]) + assert.strictEqual((yield* Effect.flip(test.click(result.parts[0].messageId, "go")))._tag, "NoSuchElement") + yield* test.click(result.parts[1].messageId, "go") + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 }, measure: costing(30) })))) + + it.effect("fails Overflow when the actions' own message has no part left", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const error = yield* Effect.flip(ChatMarkdown.post(thread, "Hello", { actions: [button], maxParts: 1 })) + assert.strictEqual(error.cause._tag, "Overflow") + assert.deepStrictEqual([error.posted.map((part) => part.text), error.remaining], [["Hello"], ""]) + assert.strictEqual((yield* test.messages(thread)).length, 1) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 }, measure: costing(30) })))) + + it.effect("moves the actions to an unattached note posted on its own", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("post", new ChatError.Forbidden({ detail: "file_refused" })) + const result = yield* ChatMarkdown.post(thread, "a".repeat(20), { + actions: [button], + files, + unattachedNote: () => " note" + }) + assert.deepStrictEqual(result.parts.map((part) => part.markdown), ["a".repeat(20), " note"]) + assert.strictEqual((yield* Effect.flip(test.click(result.parts[0].messageId, "go")))._tag, "NoSuchElement") + yield* test.click(result.parts[1].messageId, "go") + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 }, measure: costing(10) })))) + + it.effect("posts the actions after an unattached note that has no room for them", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("post", new ChatError.Forbidden({ detail: "file_refused" })) + const result = yield* ChatMarkdown.post(thread, "a".repeat(20), { + actions: [button], + files, + unattachedNote: () => "n".repeat(25) + }) + assert.deepStrictEqual(result.parts.map((part) => part.markdown), ["a".repeat(20), "n".repeat(25), ""]) + for (const part of result.parts.slice(0, 2)) { + assert.strictEqual((yield* Effect.flip(test.click(part.messageId, "go")))._tag, "NoSuchElement") + } + yield* test.click(result.parts[2].messageId, "go") + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 }, measure: costing(10) })))) + + it.effect("never cuts inside a grapheme cluster on a hard cut", () => + Effect.gen(function*() { + const { thread } = yield* setup + // The 10th code unit is the "e" of "e\u0301", and the flag is two code points of one cluster. + const input = "aaaaaaaaae\u0301bbbbbbb\u{1F1EF}\u{1F1F5}cc" + const result = yield* ChatMarkdown.post(thread, input) + assert.strictEqual(joined(result.parts), input) + assert.deepStrictEqual(result.parts.map((part) => part.text), [ + "aaaaaaaaa", + "e\u0301bbbbbbb", + "\u{1F1EF}\u{1F1F5}cc" + ]) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 10 } })))) + + it.effect("keeps message text out of the error's message and JSON", () => + Effect.gen(function*() { + const { thread } = yield* setup + const error = yield* Effect.flip(ChatMarkdown.post(thread, paragraphs(5, 20), { maxParts: 1 })) + assert.notInclude(error.message, "Paragraph") + assert.notInclude(JSON.stringify(error.toJSON()), "Paragraph") + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 30 } })))) + }) + + describe("stream", () => { + it.effect("posts each part once, when it is final, without edit", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread) + yield* send("one\n\n") + assert.deepStrictEqual(yield* test.calls, []) + yield* send("two ".repeat(10) + "\n\nthree") + const result = yield* end + assert.deepStrictEqual(result.parts.map((part) => part.transport), ["post", "post"]) + assert.deepStrictEqual(yield* texts(test, thread), ["one\n\n" + "two ".repeat(10) + "\n\n", "three"]) + }).pipe(Effect.provide(ChatTest.layer({ capabilities: ["files"], limits: { messageLength: 50 } })))) + + it.effect("writes natively with a recipient, sending held text again", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("hello\n") + yield* send("x < y\n") + yield* send("```ts\ncode") + const result = yield* end + assert.deepStrictEqual(result.parts.map((part) => [part.transport, part.text]), [[ + "native", + "hello\nx < y\n```ts\ncode" + ]]) + assert.deepStrictEqual(yield* texts(test, thread), ["hello\nx < y\n```ts\ncode\n```"]) + const calls = yield* test.calls + assert.deepStrictEqual(calls.map((call) => [call.op, call.markdown]), [ + ["nativeStream.start", "hello\n"], + ["nativeStream.append", "x < y\n"], + ["nativeStream.append", "< y\n```ts\ncod"], + ["nativeStream.append", "< y\n```ts\ncode\n```"], + ["nativeStream.stop", undefined] + ]) + }).pipe(Effect.provide(ChatTest.layer()))) + + for ( + const [where, prefix, first, rest, whole, close] of [ + // A delta that ends in half a surrogate pair, inside a fence where whole lines are not held. + ["inside a fence", "~~~\n", "🇺\uD83C", "\uDDF8", "🇺🇸", "\n~~~"], + // A line past the hold limit with no whitespace to release up to. + ["in a long line", "x".repeat(301), "1", "\uFE0F\u20E3", "1️⃣", ""] + ] + ) { + it.effect(`holds a grapheme cluster split across deltas ${where}`, () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread, { recipient: "U1" }) + yield* send(prefix + first) + assert.deepStrictEqual((yield* test.calls).map((call) => call.markdown), [prefix]) + yield* send(rest + "\n") + assert.deepStrictEqual((yield* test.calls).map((call) => call.markdown), [prefix, whole + "\n"]) + yield* send("z") + const result = yield* end + assert.strictEqual(joined(result.parts), prefix + whole + "\nz") + assert.deepStrictEqual(yield* texts(test, thread), [prefix + whole + "\nz" + close]) + }).pipe(Effect.provide(ChatTest.layer()))) + } + + it.effect("rolls a native stream over at a block boundary", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread, { recipient: "U1" }) + const input = paragraphs(6, 28) + "\n\n" + for (const chunk of input.split(/(?<=\n\n)/)) yield* send(chunk) + const result = yield* end + assert.isTrue(result.parts.length > 1) + assert.strictEqual(joined(result.parts), input) + for (const part of result.parts) { + assert.strictEqual(part.transport, "native") + assert.isTrue(part.text.endsWith("\n\n"), part.text) + } + const messages = yield* texts(test, thread) + assert.deepStrictEqual(messages, result.parts.map((part) => part.markdown)) + for (const message of messages) assert.isAtMost(codePoints(message), 100) + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 100 } })))) + + it.effect("forces a rollover on a TooLarge append", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("one\n") + yield* test.failNext("nativeStream.append", new ChatError.TooLarge({ detail: "msg_too_long" })) + yield* send("two\n") + const result = yield* end + assert.deepStrictEqual(result.parts.map((part) => part.text), ["one\n", "two\n"]) + assert.deepStrictEqual(yield* texts(test, thread), ["one\n", "two\n"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("falls back to post-then-edit when the start is Unsupported, closing an open fence", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + yield* test.failNext("nativeStream.start", new ChatError.Unsupported({ capability: "nativeStream" })) + const result = yield* ChatMarkdown.stream(thread, Stream.make("one\n", "```ts\ncode"), { recipient: "U1" }) + assert.deepStrictEqual(result.parts.map((part) => part.transport), ["edit"]) + assert.deepStrictEqual(yield* texts(test, thread), ["one\n```ts\ncode\n```"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("ends the reply stopped when an append comes back stopped", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const ids: Array = [] + const { fiber, send } = yield* feed(thread, { + recipient: "U1", + onPart: (part) => Effect.sync(() => ids.push(part.messageId)) + }) + yield* send("one\n") + yield* test.clickStop(ids[0]) + yield* test.closeStream(ids[0]) + yield* send("two\n") + const result = yield* Fiber.join(fiber) + assert.isTrue(result.stopped) + assert.strictEqual(joined(result.parts), "one\n") + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("carries on by edit when the stream is closed", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const ids: Array = [] + const { end, send } = yield* feed(thread, { + recipient: "U1", + onPart: (part) => Effect.sync(() => ids.push(part.messageId)) + }) + yield* send("one\n") + yield* test.closeStream(ids[0]) + yield* send("two\n") + const result = yield* end + assert.deepStrictEqual(result.parts.map((part) => [part.transport, part.text]), [["native", "one\ntwo\n"]]) + assert.deepStrictEqual(yield* texts(test, thread), ["one\ntwo\n"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("does not post again when interrupted while onPart runs", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const entered = yield* Deferred.make() + const release = yield* Deferred.make() + const { fiber, send } = yield* feed(thread, { + recipient: "U1", + onPart: () => Effect.andThen(Deferred.succeed(entered, undefined), Deferred.await(release)) + }) + yield* send("hello\n") + yield* Deferred.await(entered) + const interrupt = yield* Effect.forkChild(Fiber.interrupt(fiber)) + yield* settle + yield* Deferred.succeed(release, undefined) + yield* Fiber.join(interrupt) + assert.deepStrictEqual(yield* texts(test, thread), ["hello\n"]) + assert.include((yield* test.calls).map((call) => call.op), "nativeStream.stop") + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("repairs with the retried chunk while the upstream is paused", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { send } = yield* feed(thread, { recipient: "U1" }) + yield* send("one\n") + yield* test.failNext("nativeStream.append", new ChatError.RateLimited({ retryAfter: Duration.millis(100) }), { + times: 3 + }) + yield* send("two\n") + yield* TestClock.adjust("200 millis") + yield* settle + const ops = (yield* test.calls).map((call) => call.op) + assert.strictEqual(ops.filter((op) => op === "nativeStream.append").length, 3) + assert.include(ops, "nativeStream.stop") + assert.deepStrictEqual(yield* texts(test, thread), ["one\ntwo\n"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("partitions an uncertain repair followed by a denied edit", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { fiber, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("one\n") + yield* test.failNext("nativeStream.append", new ChatError.Unknown({}), { landed: true }) + yield* test.failNext("edit", new ChatError.Unknown({}), { landed: true }) + yield* send("two\n") + yield* test.failNext("edit", new ChatError.Forbidden({})) + yield* send("three\n") + const error = yield* Effect.flip(Fiber.join(fiber)) + assert.deepStrictEqual([joined(error.posted), error.uncertain, error.remaining], ["one\n", "two\n", "three\n"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("leaves room in a native stream to close a code fence", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("~~~\n") + yield* send("a".repeat(16)) + const result = yield* end + assert.strictEqual(joined(result.parts), "~~~\n" + "a".repeat(16)) + const messages = yield* texts(test, thread) + assert.isTrue(messages.length > 1) + for (const message of messages) { + assert.isTrue(message.endsWith("\n~~~"), message) + assert.isAtMost(codePoints(message), 20) + } + }).pipe(Effect.provide(ChatTest.layer({ limits: { messageLength: 20 } })))) + + it.effect("takes the text that arrived while waiting for the write interval", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { end, send } = yield* feed(thread) + yield* send("a\n") + yield* send("b\n") + yield* send("c\n") + yield* TestClock.adjust("1 second") + yield* settle + const calls = yield* test.calls + assert.deepStrictEqual(calls.map((call) => [call.op, call.markdown]), [["post", "a\n"], ["edit", "a\nb\nc\n"]]) + const ended = yield* Effect.forkChild(end) + yield* settle + yield* TestClock.adjust("1 second") + const result = yield* Fiber.join(ended) + assert.strictEqual(joined(result.parts), "a\nb\nc\n") + }).pipe(Effect.provide(ChatTest.layer({ limits: { minWriteInterval: Duration.seconds(1) } })))) + + it.effect("a rotated stream's start names the part it continues", () => + Effect.gen(function*() { + const { thread } = yield* setup + const continues: Array = [] + yield* patchNative((native) => ({ + start: (thread, options) => + Effect.suspend(() => { + continues.push(options.continues) + return native.start(thread, options) + }) + })) + const { end, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("one\n") + yield* TestClock.adjust("9 minutes") + yield* send("two\n") + const result = yield* end + assert.deepStrictEqual(continues, [undefined, result.parts[0].messageId]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("sends a stop again when interrupted during it", () => + Effect.gen(function*() { + const { thread } = yield* setup + const chat = yield* Chat.Chat + const { entered } = yield* holdFirst("stop") + const ids: Array = [] + const { close, fiber, send } = yield* feed(thread, { + recipient: "U1", + onPart: (part) => Effect.sync(() => void ids.push(part.messageId)) + }) + yield* send("hello\n") + yield* close + yield* Deferred.await(entered) + yield* Fiber.interrupt(fiber) + // An open native stream would refuse the edit. + yield* chat.edit(thread, ids[0], { markdown: "edited" }) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("keeps a rollover due after an append that was in flight when interrupted", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { entered, release } = yield* holdFirst("append") + const { fiber, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("~~~\none\n") + yield* TestClock.adjust("9 minutes") + yield* send("two\n") + yield* Deferred.await(entered) + const interrupt = yield* Effect.forkChild(Fiber.interrupt(fiber)) + yield* settle + yield* Deferred.succeed(release, undefined) + yield* Fiber.join(interrupt) + assert.deepStrictEqual(yield* texts(test, thread), ["~~~\none\n~~~", "~~~\ntwo\n~~~"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("bounds an append in flight when the upstream fails", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const { entered, release } = yield* holdFirst("append") + const { fail, fiber, send } = yield* feed(thread, { recipient: "U1" }) + yield* send("one\n") + yield* send("two\n") + yield* Deferred.await(entered) + yield* fail("boom") + yield* TestClock.adjust("6 seconds") + const finished = fiber.pollUnsafe() !== undefined + yield* Deferred.succeed(release, undefined) + const error = yield* Effect.flip(Fiber.join(fiber)) + assert.isTrue(finished) + assert.deepStrictEqual(error.cause, { _tag: "Upstream", error: "boom" }) + assert.deepStrictEqual([joined(error.posted), error.uncertain, error.remaining], ["one\n", "two\n", ""]) + yield* settle + assert.deepStrictEqual(yield* texts(test, thread), ["one\n"]) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("stops the stream after a denied append when the upstream then fails", () => + Effect.gen(function*() { + const { test, thread } = yield* setup + const chat = yield* Chat.Chat + const { entered, release } = yield* holdFirst("stop") + const ids: Array = [] + const { fail, fiber, send } = yield* feed(thread, { + recipient: "U1", + onPart: (part) => Effect.sync(() => void ids.push(part.messageId)) + }) + yield* send("one\n") + yield* test.failNext("nativeStream.append", new ChatError.Forbidden({})) + yield* send("two\n") + yield* Deferred.await(entered) + yield* fail("boom") + yield* Deferred.succeed(release, undefined) + const error = yield* Effect.flip(Fiber.join(fiber)) + assert.strictEqual(error.cause._tag === "Chat" && error.cause.error.reason._tag, "Forbidden") + assert.deepStrictEqual([joined(error.posted), error.uncertain, error.remaining], ["one\n", undefined, "two\n"]) + assert.deepStrictEqual(yield* texts(test, thread), ["one\n"]) + // An open native stream would refuse the edit. + yield* chat.edit(thread, ids[0], { markdown: "edited" }) + }).pipe(Effect.provide(ChatTest.layer()))) + }) +}) diff --git a/packages/effect/test/chat/ChatTest.test.ts b/packages/effect/test/chat/ChatTest.test.ts new file mode 100644 index 00000000000..8626313129c --- /dev/null +++ b/packages/effect/test/chat/ChatTest.test.ts @@ -0,0 +1,237 @@ +import { assert, describe, it } from "@effect/vitest" +import { Effect, Exit, Option } from "effect" +import { + Chat, + ChatAdapter, + ChatError, + ChatHandler, + ChatInteraction, + ChatMessage, + ChatTest, + ChatThread +} from "effect/chat" + +const kinds = { "@": "user", "@&": "role", "#": "channel", "!": "broadcast" } as const + +const parseMention: ChatTest.ParseMention = (slice) => { + const match = /^<(@&|@|#|!)([^<>\s]+)>$/.exec(slice) + return match === null ? Option.none() : Option.some({ kind: kinds[match[1] as keyof typeof kinds], id: match[2] }) +} + +/** The conformance click hook, over the fake's own entry. */ +const interactionsFor = (test: ChatTest.ChatTest["Service"]): ChatTest.Interactions => { + let open: { + readonly from: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatInteraction.CommandInvoked + readonly opened: ChatInteraction.OpenModal + } | undefined + const deliver = Effect.fnUntraced(function*( + from: ChatInteraction.ButtonClicked | ChatInteraction.SelectChanged | ChatInteraction.CommandInvoked + ) { + const handler = yield* ChatInteraction.Handler + let refused = false + const reply = yield* test.interact(from as ChatInteraction.CommandInvoked).pipe( + Effect.provideService(ChatInteraction.Handler, { + ...handler, + refused: (event, error) => + Effect.andThen(Effect.sync(() => void (refused = true)), handler.refused(event, error)) + }) + ) + open = reply._tag === "OpenModal" && !refused ? { from, opened: reply } : undefined + }) + return { + click: (messageId, id, options) => Effect.flatMap(test.click(messageId, id, options), deliver), + command: (thread, id, options) => Effect.flatMap(test.command(thread, id, options), deliver), + submit: Effect.fnUntraced(function*(values) { + if (open === undefined) return + const reply = yield* test.interact(yield* test.submit(open.from, open.opened, values)) + if (reply._tag !== "Errors") open = undefined + }) + } +} + +const rulesFor = (test: ChatTest.ChatTest["Service"], thread: ChatThread.ChatThread) => + ChatTest.conformance({ + platform: test.platform, + thread, + userPosts: (thread, text) => test.userPosts(thread, { text }), + parseMention, + refuseFiles: test.refuseFiles, + failNext: test.failNext, + interactions: interactionsFor(test) + }) + +const suite = Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + return rulesFor(test, yield* test.makeThread()) +}) + +const ruleNames = Object.keys(ChatTest.conformance({ + platform: "test", + thread: ChatThread.make("test", "c0"), + userPosts: () => Effect.die("unused"), + parseMention, + refuseFiles: () => Effect.void, + failNext: () => Effect.void, + interactions: { click: () => Effect.void, submit: () => Effect.void, command: () => Effect.void } +})) + +const capabilities = ChatError.Capability.literals + +const runRule = (rule: string, options?: Parameters[0]) => + Effect.flatMap(suite, (rules) => rules[rule]).pipe(Effect.provide(ChatTest.layer(options))) + +describe("ChatTest", () => { + describe("conformance with every capability", () => { + for (const rule of ruleNames) { + it.live(rule, () => runRule(rule)) + } + }) + + // With a capability off, a gated rule only probes it, as unsupported/{capability} does. + describe("conformance without any capability", () => { + for (const rule of ruleNames.filter((name) => name.startsWith("unsupported/"))) { + it.live(rule, () => runRule(rule, { capabilities: [] })) + } + }) + + // Paths that all-on and all-off miss: a probe gated on another capability, and a modal opened from a select. + for ( + const [rule, off] of [ + ["modal/origin", "buttons"], + ["unsupported/modals", "modals"], + ["modal/errorsKeepOpen", "modalErrors"], + ["unsupported/ephemeral", "ephemeral"] + ] as const + ) { + it.live(`conformance ${rule} without ${off}`, () => + runRule(rule, { capabilities: capabilities.filter((c) => c !== off) })) + } + + it("lists the hook rules only with their hooks", () => { + const withoutHooks = Object.keys(ChatTest.conformance({ + platform: "test", + thread: ChatThread.make("test", "c0"), + userPosts: () => Effect.die("unused"), + parseMention + })) + assert.include(withoutHooks, "unsupported/buttons") + for (const name of ["post/refusedFile", "actions/valueRoundTrips", "command/arrives", "retries/retrySafe"]) { + assert.notInclude(withoutHooks, name) + assert.include(ruleNames, name) + } + }) + + it.effect("fails unsupported/{capability} for a descriptor that leaves out a working op", () => + Effect.gen(function*() { + const registry = yield* ChatAdapter.Registry + const adapter = yield* registry.get("test") + const listed = new Set(adapter.descriptor.capabilities) + listed.delete("edit") + yield* registry.register({ ...adapter, descriptor: { ...adapter.descriptor, capabilities: listed } }) + const failure = yield* Effect.flip(Effect.flatMap(suite, (rules) => rules["unsupported/edit"])) + assert.strictEqual(failure.rule, "unsupported/edit") + assert.strictEqual(failure.actual, "success") + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("fails a rule whose listed capability fails with Unsupported", () => + Effect.gen(function*() { + const registry = yield* ChatAdapter.Registry + const adapter = yield* registry.get("test") + yield* registry.register({ + ...adapter, + edit: () => + Effect.fail( + new ChatError.ChatError({ + platform: "test", + op: "edit", + reason: new ChatError.Unsupported({ capability: "edit" }) + }) + ) + }) + const failure = yield* Effect.flip(Effect.flatMap(suite, (rules) => rules["edit/keepsId"])) + assert.strictEqual(failure.actual, "Unsupported(edit) for a listed capability") + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("post/refusedFile cleans up a refused write that landed", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const thread = yield* test.makeThread() + const rules = ChatTest.conformance({ + platform: test.platform, + thread, + parseMention, + userPosts: (thread, text) => test.userPosts(thread, { text }), + refuseFiles: () => + test.failNext("post", new ChatError.Forbidden({ detail: ChatError.fileRefused }), { landed: true }) + }) + const error = yield* Effect.flip(rules["post/refusedFile"]) + assert.strictEqual(error.expected, "no new message") + assert.deepStrictEqual(yield* test.messages(thread), []) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.live("deletes every bot message the suite posts", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const thread = yield* test.makeThread() + yield* Effect.forEach(Object.values(rulesFor(test, thread)), (rule) => rule, { discard: true }) + const left = (yield* test.messages(thread)).filter((message) => message.author.isSelf) + assert.deepStrictEqual(left.map((message) => message.text), []) + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("checkInteraction rejects an empty messageId", () => + Effect.gen(function*() { + const submitted = ChatInteraction.ModalSubmitted.make({ + id: "ask", + values: {}, + thread: ChatThread.make("test", "c1"), + messageId: "", + userId: "u", + raw: null + }) + const failure = yield* Effect.flip(ChatTest.checkInteraction(submitted, { platform: "test" })) + assert.strictEqual(failure.actual, "an empty messageId") + })) + + it.effect("repliesToSelf is true only for a reply to the bot's message, and is not a mention", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const chat = yield* Chat.Chat + const thread = yield* test.makeThread() + const message = (event: { readonly _tag: string; readonly message?: ChatMessage.ChatMessage }) => { + assert(event._tag === "MessageCreated" && event.message !== undefined) + return event.message + } + const own = yield* chat.post(thread, { markdown: "from the bot" }) + const plain = message(yield* test.userPosts(thread, { text: "no reply" })) + const toBot = message(yield* test.userPosts(thread, { text: "to the bot", replyTo: { messageId: own.id } })) + const toUser = message(yield* test.userPosts(thread, { text: "to a person", replyTo: { messageId: plain.id } })) + assert.deepStrictEqual([plain, toBot, toUser].map(ChatMessage.repliesToSelf), [false, true, false]) + assert.isFalse(ChatMessage.mentionsSelf(toBot)) + // checkMessage rejects an author without isSelf, and isSelf without an author. + for (const replyTo of [{ messageId: own.id, authorId: test.selfId }, { messageId: own.id, isSelf: true }]) { + const exit = yield* Effect.exit( + ChatTest.checkMessage({ ...toBot, replyTo }, { platform: "test", parseMention }) + ) + assert.isTrue(Exit.isFailure(exit)) + } + }).pipe(Effect.provide(ChatTest.layer()))) + + it.effect("ChatHandler.layer drops the bot's own messages unless deliverSelf", () => + Effect.gen(function*() { + const test = yield* ChatTest.ChatTest + const thread = yield* test.makeThread() + const own = yield* test.userPosts(thread, { text: "own" }) + const self = own._tag === "MessageCreated" + ? { ...own, message: { ...own.message, author: { ...own.message.author, isSelf: true } } } + : own + const received: Array = [] + const handler = ChatHandler.layer((event) => Effect.sync(() => void received.push(event._tag))) + yield* test.deliver(self).pipe(Effect.provide(handler)) + yield* test.deliver(own).pipe(Effect.provide(handler)) + yield* test.deliver(self).pipe(Effect.provide(ChatHandler.layer( + (event) => Effect.sync(() => void received.push("self " + event._tag)), + { deliverSelf: true } + ))) + assert.deepStrictEqual(received, ["MessageCreated", "self MessageCreated"]) + }).pipe(Effect.provide(ChatTest.layer()))) +}) diff --git a/packages/effect/test/jose/Jwks.test.ts b/packages/effect/test/jose/Jwks.test.ts index 4f3faba8881..f56b9add1df 100644 --- a/packages/effect/test/jose/Jwks.test.ts +++ b/packages/effect/test/jose/Jwks.test.ts @@ -121,6 +121,28 @@ it.effect("caches remote key sets, coalesces concurrent refreshes, and refreshes assert.strictEqual(requests, 4) }).pipe(Effect.provide(layer))) +it.effect("filters raw keys before decoding, and counts maxKeys before filtering", () => + Effect.gen(function*() { + const endorsed = { ...ec, kid: "endorsed", endorsements: ["msteams"] } + const document = { keys: [endorsed, { ...ec, kid: "other", endorsements: ["skype"] }, { ...ec, kid: "bare" }, 1] } + const filter = (jwk: { readonly [key: string]: unknown }) => + Array.isArray(jwk.endorsements) && jwk.endorsements.includes("msteams") + const client = serve(() => Effect.succeed(new Response(JSON.stringify(document)))) + + const keys = Context.get( + yield* Layer.build(Jwks.layerRemote({ url, filter }).pipe(Layer.provide(client))), + Jwks.Jwks + ) + assert.strictEqual((yield* keys.resolve({ algorithm: "ES256" })).kid, "endorsed") + assertReason(yield* keys.resolve({ algorithm: "ES256", kid: "bare" }).pipe(Effect.flip), "KeyNotFound") + + const limited = Context.get( + yield* Layer.build(Jwks.layerRemote({ url, filter, maxKeys: 3 }).pipe(Layer.provide(client))), + Jwks.Jwks + ) + assertReason(yield* limited.resolve({ algorithm: "ES256" }).pipe(Effect.flip), "KeySetUnavailable") + }).pipe(Effect.provide(layer))) + it.effect("refetches an expired key set at once when timeToLive is shorter than cooldown", () => Effect.gen(function*() { let requests = 0 diff --git a/packages/effect/typetest/chat/ChatInteraction.tst.ts b/packages/effect/typetest/chat/ChatInteraction.tst.ts new file mode 100644 index 00000000000..d2d74b8f67d --- /dev/null +++ b/packages/effect/typetest/chat/ChatInteraction.tst.ts @@ -0,0 +1,108 @@ +import { Context, Effect, type Layer, Schema } from "effect" +import { ChatInteraction } from "effect/chat" +import { describe, expect, it } from "tstyche" + +const EnvId = Schema.String.pipe(Schema.brand("EnvId")) +type EnvId = typeof EnvId.Type + +class DeployQueue extends Context.Service Effect.Effect +}>()("DeployQueue") {} + +const Deploy = Schema.Struct({ version: Schema.String }) + +const Approve = ChatInteraction.button("approve", { label: "Approve", value: Deploy }) +const Refresh = ChatInteraction.button("refresh", { label: "Refresh" }) +const PickEnv = ChatInteraction.select("pick-env", { value: EnvId }) +const RejectReason = ChatInteraction.modal("reject-reason", { + title: "Reject deploy", + metadata: Deploy, + fields: { + intro: ChatInteraction.text("Tell the author why."), + reason: ChatInteraction.textInput({ label: "Reason", multiline: true }), + note: ChatInteraction.textInput({ label: "Note", optional: true }), + severity: ChatInteraction.choice({ + label: "Severity", + options: [{ label: "Low", value: "low" }, { label: "High", value: "high" }] + }), + env: ChatInteraction.selectInput({ label: "Environment", value: EnvId }) + } +}) +const Feedback = ChatInteraction.modal("feedback", { + title: "Feedback", + fields: { body: ChatInteraction.textInput({ label: "Feedback" }) } +}) +const Ship = ChatInteraction.command("ship", { description: "Ship a version" }) + +describe("ChatInteraction", () => { + it("of takes the value its schema asks for", () => { + expect(Approve.of).type.not.toBeCallableWith() + expect(Approve.of).type.not.toBeCallableWith({ version: 1 }) + expect(Refresh.of).type.not.toBeCallableWith({ version: "v1" }) + }) + + it("open needs the metadata and the selectInput options", () => { + expect(RejectReason.open).type.not.toBeCallableWith() + expect(RejectReason.open).type.not.toBeCallableWith({ metadata: { version: "v1" } }) + expect(Feedback.open()).type.toBe() + }) + + it("types each handler from its definition", () => { + const Group = ChatInteraction.group(Approve, Refresh, PickEnv, RejectReason, Feedback) + const layer = Group.toLayer(Effect.gen(function*() { + const queue = yield* DeployQueue + return { + approve: (click) => { + expect(click.value).type.toBe<{ readonly version: string }>() + return queue.offer(click.value).pipe(Effect.as(ChatInteraction.ack)) + }, + refresh: (click) => { + expect(click.value).type.toBe() + return Effect.succeed(Feedback.open()) + }, + "pick-env": (selected) => { + expect(selected.value).type.toBe() + return Effect.succeed(ChatInteraction.ack) + }, + "reject-reason": (submit) => { + expect(submit.values).type.toBe<{ + readonly reason: string + readonly note: string | undefined + readonly severity: "low" | "high" + readonly env: EnvId + }>() + expect(submit.metadata).type.toBe<{ readonly version: string }>() + return Effect.succeed(ChatInteraction.errors({ reason: "Say a bit more." })) + }, + feedback: (submit) => { + expect(submit.metadata).type.toBe() + return Effect.succeed(ChatInteraction.close) + } + } + })) + expect(layer).type.toBe>() + }) + + it("refuses a missing handler, a failing handler, a reply of the wrong kind and an error for an unknown field", () => { + const Group = ChatInteraction.group(Approve, RejectReason, Ship) + const valid = { + approve: () => Effect.succeed(ChatInteraction.ack), + "reject-reason": () => Effect.succeed(ChatInteraction.close), + ship: (command: ChatInteraction.CommandInvoked) => Effect.succeed(ChatInteraction.defer(command)) + } + expect(Group.toLayer(valid)).type.toBe>() + const { ship: _, ...missing } = valid + expect(Group.toLayer).type.not.toBeCallableWith(missing) + expect(Group.toLayer).type.not.toBeCallableWith({ ...valid, ship: () => Effect.fail("boom") }) + expect(Group.toLayer).type.not.toBeCallableWith({ ...valid, approve: () => Effect.succeed(ChatInteraction.close) }) + expect(Group.toLayer).type.not.toBeCallableWith({ + ...valid, + "reject-reason": () => Effect.succeed(Feedback.open()) + }) + expect(Group.toLayer).type.not.toBeCallableWith({ ...valid, ship: () => Effect.succeed(ChatInteraction.ack) }) + expect(Group.toLayer).type.not.toBeCallableWith({ + ...valid, + "reject-reason": () => Effect.succeed(ChatInteraction.errors({ intro: "A text has no error." })) + }) + }) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bebfa46784d..4f921417ebb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -348,6 +348,51 @@ importers: specifier: ^3.5.45 version: 3.5.45(typescript@7.0.2) + packages/chat/discord: + devDependencies: + dfx: + specifier: ^1.1.1 + version: 1.1.1(effect@packages+effect) + effect: + specifier: workspace:^ + version: link:../../effect + + packages/chat/slack: + devDependencies: + effect: + specifier: workspace:^ + version: link:../../effect + + packages/chat/teams: + devDependencies: + effect: + specifier: workspace:^ + version: link:../../effect + + packages/chat/telegram: + devDependencies: + '@types/mdast': + specifier: ^4.0.4 + version: 4.0.4 + effect: + specifier: workspace:^ + version: link:../../effect + mdast-util-from-markdown: + specifier: ^2.0.2 + version: 2.1.0(supports-color@10.2.2) + mdast-util-gfm: + specifier: ^3.1.0 + version: 3.1.0(supports-color@10.2.2) + micromark-extension-gfm: + specifier: ^3.0.0 + version: 3.0.0 + + packages/chat/whatsapp: + devDependencies: + effect: + specifier: workspace:^ + version: link:../../effect + packages/effect: devDependencies: '@types/node': @@ -4145,12 +4190,21 @@ packages: '@types/babel__traverse@7.28.0': resolution: {integrity: sha512-8PvcXf70gTDZBgt9ptxJ8elBeBjcLOAcOtoO/mPJjtji1+CdGbHgm77om1GrsPxsiE+uXIpNSK64UYaIwQXd4Q==} + '@types/body-parser@1.19.6': + resolution: {integrity: sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==} + '@types/bun@1.4.4': resolution: {integrity: sha512-9wkRDXUXK124pEnAutE6n/l18j+i9GC5IAHK8TmX/2nlySETmky0OU6yL5U8pW1AoKh++AN9auDCr647BfurjQ==} '@types/chai@5.2.3': resolution: {integrity: sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==} + '@types/connect@3.4.38': + resolution: {integrity: sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==} + + '@types/debug@4.1.13': + resolution: {integrity: sha512-KSVgmQmzMwPlmtljOomayoR89W4FynCAi3E8PPs7vmDVPe84hT+vGPKkJfThkmXs0x0jAaa9U8uW8bbfyS2fWw==} + '@types/deep-eql@4.0.2': resolution: {integrity: sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==} @@ -4169,12 +4223,21 @@ packages: '@types/estree@1.0.10': resolution: {integrity: sha512-lTyOMnOcmRBzdCv6/4zUZCrEhV0xrqQiUZBZtVcbmLHBXKLnN8q89v/wKRF/Q0HbwVsqbIG3o5HDoFl9sduM+g==} + '@types/express-serve-static-core@4.19.9': + resolution: {integrity: sha512-QP2ESEe/ImWY0HDwNAnK9PvEffUyhLTnWkk7KXzHfyeWAnlrDe1fN77bXl6ia8KT3wPlmA7t9/VPRpnf4Ex9sg==} + + '@types/express@4.17.25': + resolution: {integrity: sha512-dVd04UKsfpINUnK0yBoYHDF3xu7xVH4BuDotC/xGuycx4CgbP48X/KF/586bcObxT0HENHXEU8Nqtu6NR+eKhw==} + '@types/gensync@1.0.5': resolution: {integrity: sha512-MbsRCT7mTikHwKZ0X+LVUTLRrZZRLipTuXEO9qOYO+zmjMVk81axyClMROf6uoPD9MRVu46bx8zoR0Ad9q3NAg==} '@types/graceful-fs@4.1.9': resolution: {integrity: sha512-olP3sd1qOEe5dXTSaFvQG+02VdRXcdytWLAZsAq1PecU8uqQAhkrnbli7DagjtXKW/Bl7YJbUsa8MPcuc8LHEQ==} + '@types/http-errors@2.0.5': + resolution: {integrity: sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg==} + '@types/istanbul-lib-coverage@2.0.6': resolution: {integrity: sha512-2QF/t/auWm0lsy8XtKVPG19v3sSOQlJe/YHZgfjb/KBBHOGSV+J2q/S671rcq9uTBrLAXmZpqJiaQbMT+zNU1w==} @@ -4190,12 +4253,27 @@ packages: '@types/jsesc@2.5.1': resolution: {integrity: sha512-9VN+6yxLOPLOav+7PwjZbxiID2bVaeq0ED4qSQmdQTdjnXJSaCVKTR58t15oqH1H5t8Ng2ZX1SabJVoN9Q34bw==} + '@types/mdast@4.0.4': + resolution: {integrity: sha512-kGaNbPh1k7AFzgpud/gMdvIm5xuECykRR+JnWKQno9TAXVa6WIVCGTPvYGekIDL4uwCZQSYbUxNBSb1aUo79oA==} + + '@types/mime@1.3.5': + resolution: {integrity: sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==} + + '@types/ms@2.1.0': + resolution: {integrity: sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA==} + '@types/node@26.6.6': resolution: {integrity: sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog==} '@types/nodemailer@8.0.2': resolution: {integrity: sha512-c7M5ox8p0nEOfbJ2E9Qcmt8/QCu/VzsiAhzBiZbvky2PhKZDHpKB3sQHBM5MEZNkCfBOaS6S9//AGPRTNB7IaA==} + '@types/qs@6.15.1': + resolution: {integrity: sha512-GZHUBZR9hckSUhrxmp1nG6NwdpM9fCunJwyThLW1X3AyHgd9IlHb6VANpQQqDr2o/qQp6McZ3y/IA2rVzKzSbw==} + + '@types/range-parser@1.2.7': + resolution: {integrity: sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==} + '@types/react-dom@19.3.0': resolution: {integrity: sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q==} peerDependencies: @@ -4216,6 +4294,15 @@ packages: '@types/semver@7.8.0': resolution: {integrity: sha512-1mAINjtQCXXeLkJ9ehXkwOcBpqtLxiVtKhpUf83DdRNdQKV0iXZpaHYqRr7nj+wvxuJzoAmAwXI+sCNMv1CzLQ==} + '@types/send@0.17.6': + resolution: {integrity: sha512-Uqt8rPBE8SY0RK8JB1EzVOIZ32uqy8HwdxCnoCOsYrvnswqmFZ/k+9Ikidlk/ImhsdvBsloHbAlewb2IEBV/Og==} + + '@types/send@1.2.1': + resolution: {integrity: sha512-arsCikDvlU99zl1g69TcAB3mzZPpxgw0UQnaHeC1Nwb015xp8bknZv5rIfri9xTOcMuaVgvabfIRA7PSZVuZIQ==} + + '@types/serve-static@1.15.10': + resolution: {integrity: sha512-tRs1dB+g8Itk72rlSI2ZrW6vZg0YrLI81iQSTkMmOqnqCaNr/8Ek4VwWcN5vZgCYWbg/JJSGBlUaYGAOP73qBw==} + '@types/ssh2-streams@0.1.13': resolution: {integrity: sha512-faHyY3brO9oLEA0QlcO8N2wT7R0+1sHWZvQ+y3rMLwdY1ZyS1z0W3t65j9PqT4HmQ6ALzNe7RZlNuCNE0wBSWA==} @@ -4231,6 +4318,9 @@ packages: '@types/swagger2openapi@7.0.4': resolution: {integrity: sha512-ffMqzciTDihOKH4Q//9Ond1yb5JP1P5FC/aFPsLK4blea1Fwk2aYctiNCkAh5etDYFswFXS+5LV/vuGkf+PU6A==} + '@types/unist@3.0.3': + resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==} + '@types/whatwg-mimetype@3.0.2': resolution: {integrity: sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA==} @@ -4799,6 +4889,9 @@ packages: capnweb@0.12.0: resolution: {integrity: sha512-jgZ/LMtMVTi+RVlooIslH78Gg23XbDTdvcLghWx3oz17D6u5GuJARt+uhZk/wcTo+f81eeabfnow4d3zLBj+JQ==} + ccount@2.0.1: + resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==} + chai@6.3.0: resolution: {integrity: sha512-XWAtwJ6OHO+tj0EKCs0Y2UamnyOxseZWltU4x2U2wh8g4AigdjwvtUjvLP2tqkA/avxHEtzxNaqGq/YGNwckKg==} engines: {node: '>=18'} @@ -4814,6 +4907,9 @@ packages: change-case@5.4.4: resolution: {integrity: sha512-HRQyTk2/YPEkt9TnUPbOpr64Uw3KOicFWPVBb+xiHvd6eBx/qPr9xqfBFDT8P2vWsvvz4jbEkfDe71W3VyNu2w==} + character-entities@2.0.2: + resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==} + chokidar@5.0.0: resolution: {integrity: sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw==} engines: {node: '>= 20.19.0'} @@ -4984,6 +5080,9 @@ packages: decimal.js@10.6.0: resolution: {integrity: sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==} + decode-named-character-reference@1.3.0: + resolution: {integrity: sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q==} + deep-extend@0.6.0: resolution: {integrity: sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==} engines: {node: '>=4.0.0'} @@ -5075,10 +5174,25 @@ packages: peerDependencies: typescript: ^5.4.4 || ^6.0.2 + devlop@1.1.0: + resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==} + + dfx@1.1.1: + resolution: {integrity: sha512-g22vKD6+WtOX6a6PoUjZoryJGp3Ut//ve6jjvEFQ8ONB0zNRkDGdf4IVitNmzZcW8G9Ewsr1FR47A98UWWbmIA==} + peerDependencies: + effect: ^4.0.0 + diacritics-map@0.1.0: resolution: {integrity: sha512-3omnDTYrGigU0i4cJjvaKwD52B8aoqyX/NEIkukFFkogBemsIbhSa1O414fpTp5nuszJG6lvQ5vBvDVNCbSsaQ==} engines: {node: '>=0.8.0'} + discord-api-types@0.38.58: + resolution: {integrity: sha512-6utghVho47hBzmx8nIp+pKEDDNIlVk6On/J5Y1Q529payNLZjBsrw+mt+G5Y3NYRE3kgta6eBx5hRYVyfHGdOA==} + + discord-verify@1.2.0: + resolution: {integrity: sha512-8qlrMROW8DhpzWWzgNq9kpeLDxKanWa4EDVoj/ASVv2nr+dSr4JPmu2tFSydf3hAGI/OIJTnZyD0JulMYIxx4w==} + engines: {node: '>=16'} + docker-compose@1.5.0: resolution: {integrity: sha512-O+eD6c63+BJORUWddXA7uAlI6H1KLDPd/aS14k73nXYpFliM48C3xrrKB3sXsR1Fp/rHUZQe7+fJ+QA7+jepyw==} engines: {node: '>= 6.0.0'} @@ -5192,6 +5306,10 @@ packages: resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==} engines: {node: '>=10'} + escape-string-regexp@5.0.0: + resolution: {integrity: sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==} + engines: {node: '>=12'} + escodegen@2.1.0: resolution: {integrity: sha512-2NlIDTwUWJN0mRPQOdtQBzbUHvdGY2P1VXSyU83Q3xKxM7WHX2Ql8dKq782Q9TgQUNOLEzEYu9bzLNj1q88I5w==} engines: {node: '>=6.0'} @@ -5952,6 +6070,9 @@ packages: long@5.3.2: resolution: {integrity: sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==} + longest-streak@3.1.0: + resolution: {integrity: sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g==} + loose-envify@1.4.0: resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==} hasBin: true @@ -6008,12 +6129,48 @@ packages: resolution: {integrity: sha512-TurLymbyLyo+kAUUAV9ggR9EPcDjP/ctlv9QAFiqUH7c+t6FlsbivPo9OKTU8xdOx9oNd2drW/Fi5RRElQbUqA==} engines: {node: '>=0.10.0'} + markdown-table@3.1.0: + resolution: {integrity: sha512-xpeSCdM+B+6P3ncfgaB7FoDbtJ30xAe6Z18TGT7AgXJsHqCElCICZovQqdRMm7kHgpTyYv36EupqewoFFSB8vw==} + marky@1.3.0: resolution: {integrity: sha512-ocnPZQLNpvbedwTy9kNrQEsknEfgvcLMvOtz3sFeWApDq1MXH1TqkCIx58xlpESsfwQOnuBO9beyQuNGzVvuhQ==} math-random@1.0.4: resolution: {integrity: sha512-rUxjysqif/BZQH2yhd5Aaq7vXMSx9NdEsQcyA07uEzIvxgI7zIr33gGsh+RU0/XjmQpCW7RsVof1vlkvQVCK5A==} + mdast-util-find-and-replace@3.0.4: + resolution: {integrity: sha512-z4B5c6jrnJ1ltOQemhWYPBSAo6y42Z8Gt2yEgfgNTMcLSU59B8bfj8Z8AnAlJrBGLnqkdAUZ7jdDEqQw3KuipQ==} + + mdast-util-from-markdown@2.1.0: + resolution: {integrity: sha512-KRLgmPaP2H1ul2bpoJkOS+9pAcDAAj9Nu1mqLnh8NLeJG5tlvj42TH80PIGBpROWlRXLk0lDrWNuZCFy/tsB4A==} + + mdast-util-gfm-autolink-literal@2.1.0: + resolution: {integrity: sha512-Jx6QSK8wDo558Bcf7q+1pe0v81g2PVs09Hlvu9xJHmCFRF3sAPW7vXFVIAwBncs3sde92WbsVqNxwdqViA2XnQ==} + + mdast-util-gfm-footnote@2.1.0: + resolution: {integrity: sha512-sqpDWlsHn7Ac9GNZQMeUzPQSMzR6Wv0WKRNvQRg0KqHh02fpTz69Qc1QSseNX29bhz1ROIyNyxExfawVKTm1GQ==} + + mdast-util-gfm-strikethrough@2.0.1: + resolution: {integrity: sha512-OuJHqvr455pwu2OaOrir7dbzqs4jlWKOtlu9L6GjcIjjQwNyw02VntQsr/Ek6/A13vgnXfUhBekWRUP1OkNivw==} + + mdast-util-gfm-table@2.0.1: + resolution: {integrity: sha512-fiIjPrAy4kz68/eFbPHhsiWDGHhqUSVoU1XsfLM5DsEPXw7MxgiVVyulUggFRckVWeT1KXQXkg5vm3kv4oMSOA==} + + mdast-util-gfm-task-list-item@2.0.1: + resolution: {integrity: sha512-jXVO1hvB2hs82g1esi7I9nIJDqiijbQLIyH2jDUb5s7jIAZMr/79h0VHSq7XHKTqpLARRm+DDNjduB7S9+6XZA==} + + mdast-util-gfm@3.1.0: + resolution: {integrity: sha512-0ulfdQOM3ysHhCJ1p06l0b0VKlhU0wuQs3thxZQagjcjPrlFRqY215uZGHHJan9GEAXd9MbfPjFJz+qMkVR6zQ==} + + mdast-util-phrasing@4.1.0: + resolution: {integrity: sha512-TqICwyvJJpBwvGAMZjj4J2n0X8QWp21b9l0o7eXyVJ25YNWYbJDVIyD1bZXE6WtV6RmKJVYmQAKWa0zWOABz2w==} + + mdast-util-to-markdown@2.2.3: + resolution: {integrity: sha512-dImdB6VZRMRac1SwAW6o1c0ESk/7pFFAMCMuKM+9v7LpTSthMM5nzXpqKw4Mjo66aXVKhNkyrP4bMB9MurPyWg==} + + mdast-util-to-string@4.0.0: + resolution: {integrity: sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg==} + mdn-data@2.27.1: resolution: {integrity: sha512-9Yubnt3e8A0OKwxYSXyhLymGW4sCufcLG6VdiDdUGVkPhpqLxlvP5vl1983gQjJl3tqbrM731mjaZaP68AgosQ==} @@ -6081,6 +6238,93 @@ packages: engines: {node: '>=20.19.4'} hasBin: true + micromark-core-commonmark@2.0.4: + resolution: {integrity: sha512-wxEeE8v8XVvOrxn1TZj74qYhAtTQsSqufHVS3uNVyT1MupXxhyaHk8/9xDNPh9BmL77QNDgWTkZ3RsYHB2ry7w==} + + micromark-extension-gfm-autolink-literal@2.1.0: + resolution: {integrity: sha512-oOg7knzhicgQ3t4QCjCWgTmfNhvQbDDnJeVu9v81r7NltNCVmhPy1fJRX27pISafdjL+SVc4d3l48Gb6pbRypw==} + + micromark-extension-gfm-footnote@2.1.0: + resolution: {integrity: sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw==} + + micromark-extension-gfm-strikethrough@2.1.0: + resolution: {integrity: sha512-ADVjpOOkjz1hhkZLlBiYA9cR2Anf8F4HqZUO6e5eDcPQd0Txw5fxLzzxnEkSkfnD0wziSGiv7sYhk/ktvbf1uw==} + + micromark-extension-gfm-table@2.1.2: + resolution: {integrity: sha512-pRzm4kDTu0MjlmBkxmS9yYhw60nncfcEwu9NNdPFSQEFXS95ZKyIIyTSHu/o3ReBUrLKYEq+7YaXCRn/bPB4MA==} + + micromark-extension-gfm-tagfilter@2.0.0: + resolution: {integrity: sha512-xHlTOmuCSotIA8TW1mDIM6X2O1SiX5P9IuDtqGonFhEK0qgRI4yeC6vMxEV2dgyr2TiD+2PQ10o+cOhdVAcwfg==} + + micromark-extension-gfm-task-list-item@2.1.0: + resolution: {integrity: sha512-qIBZhqxqI6fjLDYFTBIa4eivDMnP+OZqsNwmQ3xNLE4Cxwc+zfQEfbs6tzAo2Hjq+bh6q5F+Z8/cksrLFYWQQw==} + + micromark-extension-gfm@3.0.0: + resolution: {integrity: sha512-vsKArQsicm7t0z2GugkCKtZehqUm31oeGBV/KVSorWSy8ZlNAv7ytjFhvaryUiCUJYqs+NoE6AFhpQvBTM6Q4w==} + + micromark-factory-destination@2.0.1: + resolution: {integrity: sha512-Xe6rDdJlkmbFRExpTOmRj9N3MaWmbAgdpSrBQvCFqhezUn4AHqJHbaEnfbVYYiexVSs//tqOdY/DxhjdCiJnIA==} + + micromark-factory-label@2.0.1: + resolution: {integrity: sha512-VFMekyQExqIW7xIChcXn4ok29YE3rnuyveW3wZQWWqF4Nv9Wk5rgJ99KzPvHjkmPXF93FXIbBp6YdW3t71/7Vg==} + + micromark-factory-space@2.1.0: + resolution: {integrity: sha512-fS8hnLIjnjvdQIj39Geug8wWsR0HrYZ43KShKxNfwT7t2LOHo/LbWZEzEaSOjwtjdCsjoE6syHhonEzW0zv0+Q==} + + micromark-factory-title@2.0.1: + resolution: {integrity: sha512-5bZ+3CjhAd9eChYTHsjy6TGxpOFSKgKKJPJxr293jTbfry2KDoWkhBb6TcPVB4NmzaPhMs1Frm9AZH7OD4Cjzw==} + + micromark-factory-whitespace@2.0.1: + resolution: {integrity: sha512-Ob0nuZ3PKt/n0hORHyvoD9uZhr+Za8sFoP+OnMcnWK5lngSzALgQYKMr9RJVOWLqQYuyn6ulqGWSXdwf6F80lQ==} + + micromark-util-character@2.1.1: + resolution: {integrity: sha512-wv8tdUTJ3thSFFFJKtpYKOYiGP2+v96Hvk4Tu8KpCAsTMs6yi+nVmGh1syvSCsaxz45J6Jbw+9DD6g97+NV67Q==} + + micromark-util-chunked@2.0.1: + resolution: {integrity: sha512-QUNFEOPELfmvv+4xiNg2sRYeS/P84pTW0TCgP5zc9FpXetHY0ab7SxKyAQCNCc1eK0459uoLI1y5oO5Vc1dbhA==} + + micromark-util-classify-character@2.0.1: + resolution: {integrity: sha512-K0kHzM6afW/MbeWYWLjoHQv1sgg2Q9EccHEDzSkxiP/EaagNzCm7T/WMKZ3rjMbvIpvBiZgwR3dKMygtA4mG1Q==} + + micromark-util-combine-extensions@2.0.1: + resolution: {integrity: sha512-OnAnH8Ujmy59JcyZw8JSbK9cGpdVY44NKgSM7E9Eh7DiLS2E9RNQf0dONaGDzEG9yjEl5hcqeIsj4hfRkLH/Bg==} + + micromark-util-decode-numeric-character-reference@2.0.2: + resolution: {integrity: sha512-ccUbYk6CwVdkmCQMyr64dXz42EfHGkPQlBj5p7YVGzq8I7CtjXZJrubAYezf7Rp+bjPseiROqe7G6foFd+lEuw==} + + micromark-util-decode-string@2.0.1: + resolution: {integrity: sha512-nDV/77Fj6eH1ynwscYTOsbK7rR//Uj0bZXBwJZRfaLEJ1iGBR6kIfNmlNqaqJf649EP0F3NWNdeJi03elllNUQ==} + + micromark-util-edit-map@1.0.0: + resolution: {integrity: sha512-Pa2ljlsEL6sVwFaYeyrOLSYbQt73JvGbPOYFq+9AElXiSnfI5Q4465RRZ1sHv7lDjDOnGRDDaqPXqClqWK/q1Q==} + + micromark-util-encode@2.0.1: + resolution: {integrity: sha512-c3cVx2y4KqUnwopcO9b/SCdo2O67LwJJ/UyqGfbigahfegL9myoEFoDYZgkT7f36T0bLrM9hZTAaAyH+PCAXjw==} + + micromark-util-html-tag-name@2.0.1: + resolution: {integrity: sha512-2cNEiYDhCWKI+Gs9T0Tiysk136SnR13hhO8yW6BGNyhOC4qYFnwF1nKfD3HFAIXA5c45RrIG1ub11GiXeYd1xA==} + + micromark-util-normalize-identifier@2.0.1: + resolution: {integrity: sha512-sxPqmo70LyARJs0w2UclACPUUEqltCkJ6PhKdMIDuJ3gSf/Q+/GIe3WKl0Ijb/GyH9lOpUkRAO2wp0GVkLvS9Q==} + + micromark-util-resolve-all@2.0.1: + resolution: {integrity: sha512-VdQyxFWFT2/FGJgwQnJYbe1jjQoNTS4RjglmSjTUlpUMa95Htx9NHeYW4rGDJzbjvCsl9eLjMQwGeElsqmzcHg==} + + micromark-util-sanitize-uri@2.0.1: + resolution: {integrity: sha512-9N9IomZ/YuGGZZmQec1MbgxtlgougxTodVwDzzEouPKo3qFWvymFHWcnDi2vzV1ff6kas9ucW+o3yzJK9YB1AQ==} + + micromark-util-subtokenize@2.1.0: + resolution: {integrity: sha512-XQLu552iSctvnEcgXw6+Sx75GflAPNED1qx7eBJ+wydBb2KCbRZe+NwvIEEMM83uml1+2WSXpBAcp9IUCgCYWA==} + + micromark-util-symbol@2.0.1: + resolution: {integrity: sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q==} + + micromark-util-types@2.0.3: + resolution: {integrity: sha512-oxB2Ik03hI0gv+VNn9tnh1t1YEe9MDPptViAEgfdf3YQHsn0pzGTgCdlSCJXcwhqm8phaEuM7zeEu3QQzVBrPg==} + + micromark@4.0.3: + resolution: {integrity: sha512-oGYfQzHSG5dOMovQcJ3fyTmZlWAWpi0XA0sJwJs+i6OT88o1+Jtw/8z0CmdowSLxGhhFK89rQ/oXph/wN02PNw==} + micromatch@4.0.8: resolution: {integrity: sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==} engines: {node: '>=8.6'} @@ -7340,6 +7584,18 @@ packages: unenv@2.0.0-rc.24: resolution: {integrity: sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==} + unist-util-is@6.0.1: + resolution: {integrity: sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g==} + + unist-util-stringify-position@4.0.0: + resolution: {integrity: sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ==} + + unist-util-visit-parents@6.1.0: + resolution: {integrity: sha512-vKXxz6O38sKDj1mtx/k2cWvkkFBuYGx6uGrlzBkVeWCRF/F8sdI0j3Wbkz/5rVNRRuVc8UrgRdbCxNhmYXgP3w==} + + unist-util-visit@5.2.0: + resolution: {integrity: sha512-fpsYs/VVoZxfGY9/j3FQpZmN8wdLJaV1Pt38AaJNZu9uKIHcFU3halCdBveEq1HGKO1OEfJY65w1zvoFV1b3qQ==} + unpipe@1.0.0: resolution: {integrity: sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==} engines: {node: '>= 0.8'} @@ -7686,6 +7942,9 @@ packages: zod@4.6.7: resolution: {integrity: sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==} + zwitch@2.0.4: + resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} + snapshots: '@adobe/css-tools@4.5.0': {} @@ -10324,6 +10583,12 @@ snapshots: dependencies: '@babel/types': 7.29.11 + '@types/body-parser@1.19.6': + dependencies: + '@types/connect': 3.4.38 + '@types/node': 26.6.6 + optional: true + '@types/bun@1.4.4': dependencies: bun-types: 1.4.4 @@ -10333,6 +10598,15 @@ snapshots: '@types/deep-eql': 4.0.2 assertion-error: 2.0.1 + '@types/connect@3.4.38': + dependencies: + '@types/node': 26.6.6 + optional: true + + '@types/debug@4.1.13': + dependencies: + '@types/ms': 2.1.0 + '@types/deep-eql@4.0.2': {} '@types/deno@2.7.0': {} @@ -10352,12 +10626,31 @@ snapshots: '@types/estree@1.0.10': {} + '@types/express-serve-static-core@4.19.9': + dependencies: + '@types/node': 26.6.6 + '@types/qs': 6.15.1 + '@types/range-parser': 1.2.7 + '@types/send': 1.2.1 + optional: true + + '@types/express@4.17.25': + dependencies: + '@types/body-parser': 1.19.6 + '@types/express-serve-static-core': 4.19.9 + '@types/qs': 6.15.1 + '@types/serve-static': 1.15.10 + optional: true + '@types/gensync@1.0.5': {} '@types/graceful-fs@4.1.9': dependencies: '@types/node': 26.6.6 + '@types/http-errors@2.0.5': + optional: true + '@types/istanbul-lib-coverage@2.0.6': {} '@types/istanbul-lib-report@3.0.3': @@ -10375,6 +10668,15 @@ snapshots: '@types/jsesc@2.5.1': {} + '@types/mdast@4.0.4': + dependencies: + '@types/unist': 3.0.3 + + '@types/mime@1.3.5': + optional: true + + '@types/ms@2.1.0': {} + '@types/node@26.6.6': dependencies: undici-types: 8.9.0 @@ -10383,6 +10685,12 @@ snapshots: dependencies: '@types/node': 26.6.6 + '@types/qs@6.15.1': + optional: true + + '@types/range-parser@1.2.7': + optional: true + '@types/react-dom@19.3.0(@types/react@19.3.0)': dependencies: '@types/react': 19.3.0 @@ -10401,6 +10709,24 @@ snapshots: '@types/semver@7.8.0': {} + '@types/send@0.17.6': + dependencies: + '@types/mime': 1.3.5 + '@types/node': 26.6.6 + optional: true + + '@types/send@1.2.1': + dependencies: + '@types/node': 26.6.6 + optional: true + + '@types/serve-static@1.15.10': + dependencies: + '@types/http-errors': 2.0.5 + '@types/node': 26.6.6 + '@types/send': 0.17.6 + optional: true + '@types/ssh2-streams@0.1.13': dependencies: '@types/node': 26.6.6 @@ -10421,6 +10747,8 @@ snapshots: '@types/node': 26.6.6 openapi-types: 12.1.3 + '@types/unist@3.0.3': {} + '@types/whatwg-mimetype@3.0.2': {} '@types/ws@8.18.2': @@ -10971,6 +11299,8 @@ snapshots: capnweb@0.12.0: {} + ccount@2.0.1: {} + chai@6.3.0: {} chalk@4.1.2: @@ -10982,6 +11312,8 @@ snapshots: change-case@5.4.4: {} + character-entities@2.0.2: {} + chokidar@5.0.0: dependencies: readdirp: 5.1.1 @@ -11151,6 +11483,10 @@ snapshots: decimal.js@10.6.0: {} + decode-named-character-reference@1.3.0: + dependencies: + character-entities: 2.0.2 + deep-extend@0.6.0: {} deepmerge@4.3.1: {} @@ -11244,8 +11580,26 @@ snapshots: transitivePeerDependencies: - supports-color + devlop@1.1.0: + dependencies: + dequal: 2.0.3 + + dfx@1.1.1(effect@packages+effect): + dependencies: + discord-api-types: 0.38.58 + effect: link:packages/effect + optionalDependencies: + discord-verify: 1.2.0 + diacritics-map@0.1.0: {} + discord-api-types@0.38.58: {} + + discord-verify@1.2.0: + dependencies: + '@types/express': 4.17.25 + optional: true + docker-compose@1.5.0: dependencies: yaml: 2.9.1 @@ -11386,6 +11740,8 @@ snapshots: escape-string-regexp@4.0.0: {} + escape-string-regexp@5.0.0: {} + escodegen@2.1.0: dependencies: esprima: 4.0.1 @@ -12170,6 +12526,8 @@ snapshots: long@5.3.2: {} + longest-streak@3.1.0: {} + loose-envify@1.4.0: dependencies: js-tokens: 4.0.0 @@ -12234,10 +12592,118 @@ snapshots: markdown-link@0.1.1: {} + markdown-table@3.1.0: {} + marky@1.3.0: {} math-random@1.0.4: {} + mdast-util-find-and-replace@3.0.4: + dependencies: + '@types/mdast': 4.0.4 + escape-string-regexp: 5.0.0 + unist-util-is: 6.0.1 + unist-util-visit-parents: 6.1.0 + + mdast-util-from-markdown@2.1.0(supports-color@10.2.2): + dependencies: + '@types/mdast': 4.0.4 + '@types/unist': 3.0.3 + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + mdast-util-to-string: 4.0.0 + micromark: 4.0.3(supports-color@10.2.2) + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-decode-string: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + unist-util-stringify-position: 4.0.0 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-autolink-literal@2.1.0: + dependencies: + '@types/mdast': 4.0.4 + ccount: 2.0.1 + devlop: 1.1.0 + mdast-util-find-and-replace: 3.0.4 + micromark-util-character: 2.1.1 + + mdast-util-gfm-footnote@2.1.0(supports-color@10.2.2): + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.1.0(supports-color@10.2.2) + mdast-util-to-markdown: 2.2.3 + micromark-util-normalize-identifier: 2.0.1 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-strikethrough@2.0.1(supports-color@10.2.2): + dependencies: + '@types/mdast': 4.0.4 + mdast-util-from-markdown: 2.1.0(supports-color@10.2.2) + mdast-util-to-markdown: 2.2.3 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-table@2.0.1(supports-color@10.2.2): + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + markdown-table: 3.1.0 + mdast-util-from-markdown: 2.1.0(supports-color@10.2.2) + mdast-util-to-markdown: 2.2.3 + mdast-util-to-string: 4.0.0 + unist-util-visit: 5.2.0 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm-task-list-item@2.0.1(supports-color@10.2.2): + dependencies: + '@types/mdast': 4.0.4 + devlop: 1.1.0 + mdast-util-from-markdown: 2.1.0(supports-color@10.2.2) + mdast-util-to-markdown: 2.2.3 + transitivePeerDependencies: + - supports-color + + mdast-util-gfm@3.1.0(supports-color@10.2.2): + dependencies: + mdast-util-from-markdown: 2.1.0(supports-color@10.2.2) + mdast-util-gfm-autolink-literal: 2.1.0 + mdast-util-gfm-footnote: 2.1.0(supports-color@10.2.2) + mdast-util-gfm-strikethrough: 2.0.1(supports-color@10.2.2) + mdast-util-gfm-table: 2.0.1(supports-color@10.2.2) + mdast-util-gfm-task-list-item: 2.0.1(supports-color@10.2.2) + mdast-util-to-markdown: 2.2.3 + transitivePeerDependencies: + - supports-color + + mdast-util-phrasing@4.1.0: + dependencies: + '@types/mdast': 4.0.4 + unist-util-is: 6.0.1 + + mdast-util-to-markdown@2.2.3: + dependencies: + '@types/mdast': 4.0.4 + '@types/unist': 3.0.3 + longest-streak: 3.1.0 + mdast-util-phrasing: 4.1.0 + mdast-util-to-string: 4.0.0 + micromark-util-character: 2.1.1 + micromark-util-classify-character: 2.0.1 + micromark-util-decode-string: 2.0.1 + micromark-util-html-tag-name: 2.0.1 + unist-util-visit: 5.2.0 + zwitch: 2.0.4 + + mdast-util-to-string@4.0.0: + dependencies: + '@types/mdast': 4.0.4 + mdn-data@2.27.1: {} memoize-one@5.2.1: {} @@ -12418,6 +12884,203 @@ snapshots: - supports-color - utf-8-validate + micromark-core-commonmark@2.0.4: + dependencies: + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + micromark-factory-destination: 2.0.1 + micromark-factory-label: 2.0.1 + micromark-factory-space: 2.1.0 + micromark-factory-title: 2.0.1 + micromark-factory-whitespace: 2.0.1 + micromark-util-character: 2.1.1 + micromark-util-chunked: 2.0.1 + micromark-util-classify-character: 2.0.1 + micromark-util-edit-map: 1.0.0 + micromark-util-html-tag-name: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-subtokenize: 2.1.0 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm-autolink-literal@2.1.0: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm-footnote@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-core-commonmark: 2.0.4 + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm-strikethrough@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-util-chunked: 2.0.1 + micromark-util-classify-character: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm-table@2.1.2: + dependencies: + devlop: 1.1.0 + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm-tagfilter@2.0.0: + dependencies: + micromark-util-types: 2.0.3 + + micromark-extension-gfm-task-list-item@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-extension-gfm@3.0.0: + dependencies: + micromark-extension-gfm-autolink-literal: 2.1.0 + micromark-extension-gfm-footnote: 2.1.0 + micromark-extension-gfm-strikethrough: 2.1.0 + micromark-extension-gfm-table: 2.1.2 + micromark-extension-gfm-tagfilter: 2.0.0 + micromark-extension-gfm-task-list-item: 2.1.0 + micromark-util-combine-extensions: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-factory-destination@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-factory-label@2.0.1: + dependencies: + devlop: 1.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-factory-space@2.1.0: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-types: 2.0.3 + + micromark-factory-title@2.0.1: + dependencies: + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-factory-whitespace@2.0.1: + dependencies: + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-util-character@2.1.1: + dependencies: + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-util-chunked@2.0.1: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-classify-character@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-util-combine-extensions@2.0.1: + dependencies: + micromark-util-chunked: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-util-decode-numeric-character-reference@2.0.2: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-decode-string@2.0.1: + dependencies: + decode-named-character-reference: 1.3.0 + micromark-util-character: 2.1.1 + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-symbol: 2.0.1 + + micromark-util-edit-map@1.0.0: + dependencies: + micromark-util-types: 2.0.3 + + micromark-util-encode@2.0.1: {} + + micromark-util-html-tag-name@2.0.1: {} + + micromark-util-normalize-identifier@2.0.1: + dependencies: + micromark-util-symbol: 2.0.1 + + micromark-util-resolve-all@2.0.1: + dependencies: + micromark-util-types: 2.0.3 + + micromark-util-sanitize-uri@2.0.1: + dependencies: + micromark-util-character: 2.1.1 + micromark-util-encode: 2.0.1 + micromark-util-symbol: 2.0.1 + + micromark-util-subtokenize@2.1.0: + dependencies: + devlop: 1.1.0 + micromark-util-chunked: 2.0.1 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + + micromark-util-symbol@2.0.1: {} + + micromark-util-types@2.0.3: {} + + micromark@4.0.3(supports-color@10.2.2): + dependencies: + '@types/debug': 4.1.13 + debug: 4.4.3(supports-color@10.2.2) + decode-named-character-reference: 1.3.0 + devlop: 1.1.0 + micromark-core-commonmark: 2.0.4 + micromark-factory-space: 2.1.0 + micromark-util-character: 2.1.1 + micromark-util-chunked: 2.0.1 + micromark-util-combine-extensions: 2.0.1 + micromark-util-decode-numeric-character-reference: 2.0.2 + micromark-util-edit-map: 1.0.0 + micromark-util-encode: 2.0.1 + micromark-util-normalize-identifier: 2.0.1 + micromark-util-resolve-all: 2.0.1 + micromark-util-sanitize-uri: 2.0.1 + micromark-util-subtokenize: 2.1.0 + micromark-util-symbol: 2.0.1 + micromark-util-types: 2.0.3 + transitivePeerDependencies: + - supports-color + micromatch@4.0.8: dependencies: braces: 3.0.3 @@ -13897,6 +14560,25 @@ snapshots: dependencies: pathe: 2.0.3 + unist-util-is@6.0.1: + dependencies: + '@types/unist': 3.0.3 + + unist-util-stringify-position@4.0.0: + dependencies: + '@types/unist': 3.0.3 + + unist-util-visit-parents@6.1.0: + dependencies: + '@types/unist': 3.0.3 + unist-util-is: 6.0.1 + + unist-util-visit@5.2.0: + dependencies: + '@types/unist': 3.0.3 + unist-util-is: 6.0.1 + unist-util-visit-parents: 6.1.0 + unpipe@1.0.0: {} unplugin-utils@0.2.5: @@ -14163,3 +14845,5 @@ snapshots: readable-stream: 4.8.0 zod@4.6.7: {} + + zwitch@2.0.4: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index b36f0d6155d..83cd9417dba 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -5,6 +5,7 @@ packages: - packages/* - packages/ai/* - packages/atom/* + - packages/chat/* - packages/platform/* - packages/sql/* - packages/tools/* diff --git a/scripts/circular.mjs b/scripts/circular.mjs index 39fead0e60c..300792d0704 100644 --- a/scripts/circular.mjs +++ b/scripts/circular.mjs @@ -3,7 +3,7 @@ import * as glob from "glob" import madge from "madge" madge( - glob.globSync(["packages/*/src/**/*.ts", "packages/ai/*/src/**/*.ts", "packages/platform/*/src/**/*.ts"], { + glob.globSync(["packages/*/src/**/*.ts", "packages/ai/*/src/**/*.ts", "packages/chat/*/src/**/*.ts", "packages/platform/*/src/**/*.ts"], { ignore: [ "packages/sql-sqlite-bun/**", "packages/experimental/src/EventLogServer/Cloudflare.ts" diff --git a/scripts/clean.mjs b/scripts/clean.mjs index aad0b637ebc..ad0de4ad0bf 100644 --- a/scripts/clean.mjs +++ b/scripts/clean.mjs @@ -1,7 +1,7 @@ import * as Glob from "glob" import * as Fs from "node:fs" -const dirs = [".", ...Glob.sync("packages/*/"), ...Glob.sync("packages/sql/*/"), ...Glob.sync("packages/tools/*/"), ...Glob.sync("packages/ai/*/"), ...Glob.sync("packages/atom/*/"), ...Glob.sync("packages/platform/*/")] +const dirs = [".", ...Glob.sync("packages/*/"), ...Glob.sync("packages/sql/*/"), ...Glob.sync("packages/tools/*/"), ...Glob.sync("packages/ai/*/"), ...Glob.sync("packages/atom/*/"), ...Glob.sync("packages/chat/*/"), ...Glob.sync("packages/platform/*/")] dirs.forEach((pkg) => { const files = [".tsbuildinfo", "tsconfig.tsbuildinfo", "tsconfig.fixtures.tsbuildinfo", "tsconfig.src.tsbuildinfo", "docs", "build", "dist", "coverage"] if (pkg !== ".") { diff --git a/tsconfig.packages.json b/tsconfig.packages.json index e92c8e8fa6b..8dc98fa56a8 100644 --- a/tsconfig.packages.json +++ b/tsconfig.packages.json @@ -13,6 +13,11 @@ { "path": "packages/ai/typesafe" }, { "path": "packages/ai/openrouter" }, { "path": "packages/atom/react" }, + { "path": "packages/chat/slack" }, + { "path": "packages/chat/whatsapp" }, + { "path": "packages/chat/discord" }, + { "path": "packages/chat/teams" }, + { "path": "packages/chat/telegram" }, { "path": "packages/atom/vue" }, { "path": "packages/atom/solid" }, { "path": "packages/opentelemetry" }, diff --git a/tsconfig.tests.json b/tsconfig.tests.json index 494126e37f6..742bdd83457 100644 --- a/tsconfig.tests.json +++ b/tsconfig.tests.json @@ -54,6 +54,16 @@ "@effect/ai-typesafe/*": ["./packages/ai/typesafe/src/*.ts"], "@effect/ai-openrouter": ["./packages/ai/openrouter/src/index.ts"], "@effect/ai-openrouter/*": ["./packages/ai/openrouter/src/*.ts"], + "@effect/chat-slack": ["./packages/chat/slack/src/index.ts"], + "@effect/chat-slack/*": ["./packages/chat/slack/src/*.ts"], + "@effect/chat-whatsapp": ["./packages/chat/whatsapp/src/index.ts"], + "@effect/chat-whatsapp/*": ["./packages/chat/whatsapp/src/*.ts"], + "@effect/chat-discord": ["./packages/chat/discord/src/index.ts"], + "@effect/chat-discord/*": ["./packages/chat/discord/src/*.ts"], + "@effect/chat-teams": ["./packages/chat/teams/src/index.ts"], + "@effect/chat-teams/*": ["./packages/chat/teams/src/*.ts"], + "@effect/chat-telegram": ["./packages/chat/telegram/src/index.ts"], + "@effect/chat-telegram/*": ["./packages/chat/telegram/src/*.ts"], "@effect/atom-react": ["./packages/atom/react/src/index.ts"], "@effect/atom-react/*": ["./packages/atom/react/src/*.ts"], "@effect/atom-vue": ["./packages/atom/vue/src/index.ts"], diff --git a/vitest.config.ts b/vitest.config.ts index 5a19ae900fc..09beb64028b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -133,6 +133,15 @@ export default defineConfig({ ...project("@effect/ai-typesafe", "packages/ai/typesafe"), ...project("@effect/ai-openai-compat", "packages/ai/openai-compat"), ...project("@effect/ai-openrouter", "packages/ai/openrouter"), + ...project("@effect/chat-slack", "packages/chat/slack"), + ...project("@effect/chat-whatsapp", "packages/chat/whatsapp"), + // dfx imports `effect`, which has to be the same instance as the tests' + // for things like `Redacted` to work, so vite resolves it too. + ...project("@effect/chat-discord", "packages/chat/discord", true, { + test: { server: { deps: { inline: ["dfx"] } } } + }), + ...project("@effect/chat-teams", "packages/chat/teams"), + ...project("@effect/chat-telegram", "packages/chat/telegram"), ...project("@effect/atom-react", "packages/atom/react", true, { test: { environment: "jsdom",