ChatMarkdown unstable
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
import { Effect, Stream } from "effect"import { ChatMarkdown, ChatThread } from "effect/chat"
const reply = (deltas: Stream.Stream<string>) => Effect.gen(function*() { const result = yield* ChatMarkdown.stream(ChatThread.make("slack", "T1/C1"), deltas) return result.parts.map((part) => part.messageId) })Constructors
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. filesgo 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 failsdeniedwith detailfile_refusedis 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. WithoutunattachedNotethe first failure fails the call. If the overflow file is refused too, the remaining text is split into messages beyondmaxParts, without files and ending in the note. Anuncertainfailure records the part inuncertainand posts no note. actionsgo with the last part too, which is cut so thatmeasure(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 againstmaxParts. Whenmeasure("", actions)is already over, the call failsTooLargebefore 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
TooLargewithout detailfile_refusedfails the call, even withsubject: "files"or text that measured as fitting. Fallback requiresdeniedplusfile_refused;subjectnever 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.
Signature
declare function post<R = never>(thread: { readonly id: string; readonly platform: string;}, markdown: string, options?: Options<R> & { readonly actions?: readonly Array<{ readonly _tag: "Button"; readonly id: string; readonly label: string; readonly style?: "default" | "primary" | "danger"; readonly value?: string; } | { readonly _tag: "LinkButton"; readonly label: string; readonly url: string; } | { readonly _tag: "Select"; readonly id: string; readonly options: readonly Array<{ readonly label: string; readonly value: string; }>; readonly placeholder?: string; readonly selected?: string; }>; readonly files?: readonly Array<File>; readonly unattachedNote?: (files: readonly Array<File>) => string;}): Effect<Result, ChatMarkdownError<never>, Chat | R>Writes a stream of Markdown deltas as one reply, as the text arrives.
Details
- With a
recipientand an adapter that has a native stream, the reply is written by native appends. Otherwise, or when the first native start fails withUnsupported, it posts and then edits. The result and the span say which ran. - On an adapter without
editthe reply is buffered instead: each part is posted once, when it rolls over or the reply ends, with transportpost. 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% oflimits.streamLength, and rotates once it is 80% oflimits.streamAgeold. Every append leaves room to close the code fence it ends in, and a rollover or rotation closes it. - Writes are paced by
minWriteInterval, onClock. 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
retryoruncertainis dropped, since the next edit carries the whole text. Adeniedfailure, and any failure of a post or of a part's last edit, fails the reply. - A native append that fails
retryis tried again after itsretryAfter, or the write interval, whether or not new text arrived. Three in a row, or oneuncertainfailure, stop the stream, edit the part to its whole known text, the failed chunk included, and carry on by post-then-edit.TooLargestarts the next part with the chunk. Result.stoppedis 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 }), wherelastis the final entry ofResult.parts. Withouteditthey can't be added to the reply: post them in a message of their own withchat.post(thread, { markdown, actions }). - To stop a reply, end
deltaswithStream.interruptWhenorStream.haltWhen, so aResultstill comes back. Interrupting the call leaves only the ids collected throughonPart. - Native writes go to the adapter through the
Registry, so a layer that wrapsChatdoes not see them. - A native part that is rolled over on
TooLargecan end inside an open code fence, since it can no longer be appended to. The next part reopens the fence.
Signature
declare function stream<E, R, R2 = never>(thread: { readonly id: string; readonly platform: string;}, deltas: Stream<string, E, R>, options?: Options<R2> & { readonly recipient?: string;}): Effect<Result, ChatMarkdownError<E>, Chat | Registry | R | R2>Errors
ChatMarkdownError
unstableError for a call that did not deliver its whole input.
Details
postedis text the platform confirmed,uncertainis text sent in a write that faileduncertainand was never confirmed since, andremainingwas never sent. Thetextofposted, thenuncertain, thenremainingis the input, with nothing in two of them.remainingFenceis set whenremainingstarts inside a code fence: the line that opened it, with its info string. Text sent later starts with that line and a newline, thenremaining.causeis aChatErrorfrom a write, the upstream's errorE, orOverflowwhen the input outgrewmaxPartsand 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.
Signature
declare class ChatMarkdownError<E = never> extends YieldableError<this> & { readonly _tag: "ChatMarkdownError";} & Readonly<{ readonly cause: { readonly _tag: "Chat"; readonly error: ChatError; } | { readonly _tag: "Upstream"; readonly error: E; } | { readonly _tag: "Overflow"; }; readonly posted: readonly Array<Part>; readonly remaining: string; readonly remainingFence?: string; readonly uncertain?: string;}> { constructor<E = never>(args: { readonly cause: { readonly _tag: "Chat"; readonly error: ChatError; } | { readonly _tag: "Upstream"; readonly error: E; } | { readonly _tag: "Overflow"; }; readonly posted: readonly Array<Part>; readonly remaining: string; readonly remainingFence?: string; readonly uncertain?: string; }); message: string; toJSON(): unknown;}Models
The options post and stream share.
Details
replyToapplies to the first part.maxPartsdefaults to 5, a provisional number. Past it,overflowdecides:fail, the default, or{ file }, which sends the rest as one trailing file message that does not count againstmaxParts.minWriteIntervalis the floor between two writes. It defaults to the adapter'slimits.minWriteInterval.onPartruns 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.
Signature
interface Options<R> { readonly maxParts?: number; readonly minWriteInterval?: Duration; readonly onPart?: (part: Part) => Effect<void, never, R>; readonly overflow?: "fail" | { readonly file: { readonly name: string; }; }; readonly replyTo?: { readonly authorId?: string; readonly isSelf?: boolean; readonly messageId: string; };}One message a call created.
Details
markdownis 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.textis the slice of the input the part carries, without anything the layer added. Thetextof every part, in order, is the input.transportispostfor a part frompostand for a streamed part posted once to an adapter withoutedit,editfor a streamed part written by post-then-edit andnativefor one written by the adapter's native stream, even if it was finished by an edit.
Signature
interface Part { readonly kind: "text" | "overflowFile"; readonly markdown: string; readonly messageId: string; readonly text: string; readonly timestamp: Utc; readonly transport: "post" | "edit" | "native";}What a call posted.
Details
partsis empty when the input was empty: nothing is posted.stoppedmeans 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 frompost.
Signature
interface Result { readonly parts: readonly Array<Part>; readonly stopped: boolean;}