/** * 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 type * as DateTime from "../DateTime.ts"; import * as Duration from "../Duration.ts"; import * as Effect from "../Effect.ts"; import * as Stream from "../Stream.ts"; import * as Chat from "./Chat.ts"; import type { File } from "./ChatAdapter.ts"; import { Registry } from "./ChatAdapter.ts"; import { ChatError } 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; } declare const ChatMarkdownError_base: new = {}>(args: import("../Types.ts").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => Cause.YieldableError & { readonly _tag: "ChatMarkdownError"; } & Readonly; /** * 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 declare class ChatMarkdownError extends ChatMarkdownError_base<{ 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"; }; }> { get message(): string; /** * The error without its message text. */ toJSON(): unknown; } /** * 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; } /** * 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 declare const post: (thread: ChatThread, markdown: string, options?: Options & { readonly files?: ReadonlyArray | undefined; readonly actions?: ReadonlyArray | undefined; readonly unattachedNote?: ((files: ReadonlyArray) => string) | undefined; }) => Effect.Effect; /** * 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 declare const stream: (thread: ChatThread, deltas: Stream.Stream, options?: Options & { readonly recipient?: string | undefined; }) => Effect.Effect, Chat.Chat | Registry | R | R2>; export {}; //# sourceMappingURL=ChatMarkdown.d.ts.map