/** * 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 Layer from "../Layer.ts"; import * as Option from "../Option.ts"; import * as Schema from "../Schema.ts"; import type { Scope } from "../Scope.ts"; import { ChatError } from "./ChatError.ts"; import { ChatThread } from "./ChatThread.ts"; import * as ChatUi from "./ChatUi.ts"; /** * 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 declare const ButtonClicked: Schema.TaggedStruct<"ButtonClicked", { readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly messageId: Schema.String; readonly userId: Schema.String; readonly raw: Schema.Codec; readonly id: Schema.String; readonly value: Schema.optionalKey; }>; /** * 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 declare const SelectChanged: Schema.TaggedStruct<"SelectChanged", { readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly messageId: Schema.String; readonly userId: Schema.String; readonly raw: Schema.Codec; readonly id: Schema.String; readonly value: Schema.String; }>; /** * 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 declare const ModalSubmitted: Schema.TaggedStruct<"ModalSubmitted", { readonly id: Schema.String; readonly metadata: Schema.optionalKey; readonly values: Schema.$Record; readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly messageId: Schema.optionalKey; readonly userId: Schema.String; readonly raw: Schema.Codec; }>; /** * 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 declare const CommandInvoked: Schema.TaggedStruct<"CommandInvoked", { readonly id: Schema.String; readonly text: Schema.String; readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly userId: Schema.String; readonly raw: Schema.Codec; }>; /** * 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 declare const Interaction: Schema.Union; readonly messageId: Schema.String; readonly userId: Schema.String; readonly raw: Schema.Codec; readonly id: Schema.String; readonly value: Schema.optionalKey; }>, Schema.TaggedStruct<"SelectChanged", { readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly messageId: Schema.String; readonly userId: Schema.String; readonly raw: Schema.Codec; readonly id: Schema.String; readonly value: Schema.String; }>, Schema.TaggedStruct<"ModalSubmitted", { readonly id: Schema.String; readonly metadata: Schema.optionalKey; readonly values: Schema.$Record; readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly messageId: Schema.optionalKey; readonly userId: Schema.String; readonly raw: Schema.Codec; }>, Schema.TaggedStruct<"CommandInvoked", { readonly id: Schema.String; readonly text: Schema.String; readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly userId: Schema.String; readonly raw: Schema.Codec; }>]>; /** * 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; /** * 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 declare 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 declare const OpenModal: Schema.TaggedStruct<"OpenModal", { readonly modal: Schema.TaggedStruct<"Modal", { readonly id: Schema.String; readonly title: Schema.String; readonly submitLabel: Schema.optionalKey; readonly metadata: Schema.optionalKey; readonly elements: Schema.$Array; readonly placeholder: Schema.optionalKey; readonly initial: Schema.optionalKey; readonly optional: Schema.optionalKey; readonly minLength: Schema.optionalKey; readonly maxLength: Schema.optionalKey; }>, Schema.TaggedStruct<"SelectInput", { readonly id: Schema.String; readonly label: Schema.String; readonly placeholder: Schema.optionalKey; readonly options: Schema.$Array>; readonly initial: Schema.optionalKey; readonly optional: Schema.optionalKey; }>, Schema.TaggedStruct<"Text", { readonly text: Schema.String; }>]>>; }>; }>; /** * 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 declare 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 declare const Errors: Schema.TaggedStruct<"Errors", { readonly errors: Schema.$Record; }>; /** * 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 declare const Visibility: Schema.Literals; /** * 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 declare const Respond: Schema.TaggedStruct<"Respond", { readonly markdown: Schema.String; readonly actions: Schema.optionalKey>; readonly value: Schema.optionalKey; }>, Schema.TaggedStruct<"LinkButton", { readonly label: Schema.String; readonly url: Schema.String; }>, Schema.TaggedStruct<"Select", { readonly id: Schema.String; readonly placeholder: Schema.optionalKey; readonly options: Schema.$Array>; readonly selected: Schema.optionalKey; }>]>>>; readonly visibility: Schema.Literals; }>; /** * 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 declare const Defer: Schema.TaggedStruct<"Defer", { readonly command: Schema.TaggedStruct<"CommandInvoked", { readonly id: Schema.String; readonly text: Schema.String; readonly thread: Schema.Struct<{ readonly platform: Schema.String; readonly id: Schema.String; }>; readonly userId: Schema.String; readonly raw: Schema.Codec; }>; readonly visibility: Schema.Literals; }>; /** * 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 declare 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 declare const ack: Ack; /** * Closes a submitted modal. * * @stability unstable * @category replies * @since 4.1.5 */ export declare const close: Close; /** * 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 declare const errors: (errors: E) => FieldErrors; /** * 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 declare const respond: (options: { readonly markdown: string; readonly actions?: ReadonlyArray | undefined; readonly visibility?: Visibility | undefined; }) => Respond; /** * 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 declare const defer: (command: CommandInvoked, options?: { readonly visibility?: Visibility | undefined; }) => Defer; /** * 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; } /** * The type id of definitions and groups. * * @stability unstable * @category type ids * @since 4.1.5 */ export declare 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; /** * 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 declare const encodeValue: (schema: S, value: S["Type"]) => string; /** * 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 declare const decodeValue: (schema: S, encoded: string) => Option.Option; /** * 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 */ 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 declare const button: (id: Id, options: { readonly label: string; readonly style?: ChatUi.ButtonStyle | undefined; readonly value?: Value; }) => Button; /** * 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 declare const select: (id: Id, options: { readonly placeholder?: string | undefined; readonly value?: Value; }) => Select; /** * 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 declare 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>; /** * 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 declare const choice: (options: { readonly label: string; readonly placeholder?: string | undefined; readonly options: ReadonlyArray<{ readonly label: string; readonly value: Value; }>; readonly optional?: Optional | undefined; }) => Choice>; /** * A select in a modal whose options are given when the modal opens. * * @stability unstable * @category fields * @since 4.1.5 */ export interface SelectInput { 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 declare const selectInput: (options: { readonly label: string; readonly placeholder?: string | undefined; readonly value?: Value; readonly optional?: Optional | undefined; }) => SelectInput>; /** * 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 declare const text: (text: string) => 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 { 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 declare const modal: (id: Id, options: { readonly title: string; readonly submitLabel?: string | undefined; readonly metadata?: Metadata; readonly fields: Fs; }) => Modal; /** * 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 declare const command: (id: Id, options: { readonly description: string; readonly argument?: { readonly name: string; readonly description: string; readonly required?: boolean | undefined; } | undefined; }) => Command; /** * Any definition that has a handler. * * @stability unstable * @category definitions * @since 4.1.5 */ export type Definition = Button | Select | Modal | Command; /** * 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 */ type Definitions = G extends Group ? D : never; } /** * Groups definitions so their handlers can be registered together. * * @stability unstable * @category groups * @since 4.1.5 */ export declare const group: >(...definitions: Definitions) => Group; declare const Handler_base: Context.ServiceClass Effect.Effect; readonly submit: (event: ModalSubmitted) => Effect.Effect; readonly command: (event: CommandInvoked) => Effect.Effect; readonly refused: (event: ButtonClicked | SelectChanged | CommandInvoked, error: ChatError) => Effect.Effect; }>; /** * 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 declare class Handler extends Handler_base { } /** * 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 declare const handle: { /** * 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 */ (event: ButtonClicked | SelectChanged, deadline: Duration.Duration): Effect.Effect; /** * 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 */ (event: ModalSubmitted, deadline: Duration.Duration): Effect.Effect; /** * 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 */ (event: CommandInvoked, deadline: Duration.Duration): Effect.Effect; /** * 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 */ (event: Interaction, deadline: Duration.Duration): Effect.Effect; }; /** * 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; } declare const Registry_base: Context.ServiceClass) => Effect.Effect; readonly get: (id: string) => 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 declare class Registry extends Registry_base { } /** * 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 declare const layer: (options?: { readonly unhandled?: ((event: Interaction, why: Unhandled) => Effect.Effect) | undefined; }) => Layer.Layer; export {}; //# sourceMappingURL=ChatInteraction.d.ts.map