Posting long and streamed Markdown through Chat (Effect's
effect/chat/ChatMarkdown).
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 use one splitter::
@fx.gen
def reply(deltas: Stream[str]):
result = yield from chat_markdown.stream(chat_thread.make("slack", "T1/C1"), deltas)
return [part.message_id for part in result.parts]Types#
| Type | Definition | Description |
|---|---|---|
Cause | type Cause[E] = ChatCause | UpstreamCause[E] | OverflowCause |
Classes#
Functions#
post#
post[R = Never](thread, markdown, *, reply_to=None, max_parts=None, overflow=None, min_write_interval=None, on_part=None, files=(), actions=(), unattached_note=None) -> Effect[Result, ChatMarkdownError, _chat.Chat | R]Posts Markdown of any length, split into as many messages as it needs.
- 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
fence it is in and reopening it in the next part, measured with the
adapter's
measure. Each part is posted once, paced bymin_write_interval(default: the adapter's). reply_toapplies to the first part.max_partsdefaults to 5; past it the call failsOverflow, or withoverflow=OverflowFile(name)sends the rest as one trailing file.filesgo with the last part, or with the overflow file. Withunattached_note, a post whose files are refused ("denied"with detailfile_refused) is tried once more without them, ending in the note, or followed by it when it does not fit.actionsgo with the last part, cut so it fits beside them; when not even the last character does, the files and actions go in a message of their own. Whenmeasure("", actions)is over the limit the call failsTooLargebefore posting.on_partruns for every message created, uninterruptibly once its post returns; it has to be quick.- An empty input posts nothing, unless there are files or actions.
A failure in the middle leaves the parts already posted in place; the error says what was posted and what was not.
| Parameter | Type | Description |
|---|---|---|
thread | ChatThread | |
markdown | str | |
reply_to | ReplyTo | None = None | |
max_parts | int | None = None | |
overflow | OverflowFile | None = None | |
min_write_interval | Duration | None = None | |
on_part | Callable[[Part], Effect[None, Never, R]] | None = None | |
files | Sequence[File] = () | |
actions | Sequence[Action] = () | |
unattached_note | Callable[[Sequence[File]], str] | None = None |
Returns Effect[Result, ChatMarkdownError, _chat.Chat | R]
def post[R = Never](
thread: ChatThread,
markdown: str,
*,
reply_to: ReplyTo | None = None,
max_parts: int | None = None,
overflow: OverflowFile | None = None,
min_write_interval: Duration | None = None,
on_part: Callable[[Part], Effect[None, Never, R]] | None = None,
files: Sequence[File] = (),
actions: Sequence[Action] = (),
unattached_note: Callable[[Sequence[File]], str] | None = None,
) -> Effect[Result, ChatMarkdownError, _chat.Chat | R]:
"""Posts Markdown of any length, split into as many messages as it needs.
- 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
fence it is in and reopening it in the next part, measured with the
adapter's ``measure``. Each part is posted once, paced by
``min_write_interval`` (default: the adapter's).
- ``reply_to`` applies to the first part. ``max_parts`` defaults to 5;
past it the call fails ``Overflow``, or with
``overflow=OverflowFile(name)`` sends the rest as one trailing file.
- ``files`` go with the last part, or with the overflow file. With
``unattached_note``, a post whose files are refused (``"denied"`` with
detail ``file_refused``) is tried once more without them, ending in
the note, or followed by it when it does not fit.
- ``actions`` go with the last part, cut so it fits beside them; when
not even the last character does, the files and actions go in a
message of their own. When ``measure("", actions)`` is over the limit
the call fails ``TooLarge`` before posting.
- ``on_part`` runs for every message created, uninterruptibly once its
post returns; it has to be quick.
- An empty input posts nothing, unless there are files or actions.
A failure in the middle leaves the parts already posted in place; the
error says what was posted and what was not.
"""
@fx.gen
def run():
chat = yield from _chat.Chat()
def failed(error: ChatError) -> ChatMarkdownError:
return ChatMarkdownError(posted=(), remaining=markdown, cause=ChatCause(error))
descriptor: Descriptor = yield from chat.descriptor(thread.platform).map_error(failed)
if actions and descriptor.measure("", actions) > descriptor.limits.message_length:
return (
yield from fx.fail(
failed(
ChatError(
platform=thread.platform,
op="post",
reason=TooLarge(detail="actions_too_long"),
)
)
)
)
settings: _Settings[Never, R] = _Settings(
thread=thread,
initial=markdown,
descriptor=descriptor,
chat=chat,
reply_to=reply_to,
max_parts=max_parts,
overflow=overflow,
min_write_interval=min_write_interval,
on_part=on_part,
files=files,
actions=actions,
unattached_note=unattached_note,
)
result: Result = yield from _Reply(settings).run()
return result
return run.with_span("ChatMarkdown.post", attributes={"chat.platform": thread.platform})stream#
stream[E = Never, RR = Never](thread, deltas, *, recipient=None, reply_to=None, max_parts=None, overflow=None, min_write_interval=None, on_part=None) -> Effect[Result, ChatMarkdownError[E], _chat.Chat | Registry | RR]Writes a stream of Markdown deltas as one reply, as the text arrives.
- With a
recipientand an adapter with a native stream, the reply is written by native appends; otherwise, or when the first native start failsUnsupported, it posts and then edits. On an adapter withoutediteach part is posted once, when it rolls over or the reply ends. - Text that outgrows a message rolls over at a block boundary, each new
message counting against
max_parts. A native stream rolls over at the next boundary past 85% oflimits.stream_lengthand rotates at 80% oflimits.stream_age. Every append leaves room to close the code fence it ends in. - Writes are paced by
min_write_intervalonClock; each 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 fence. - An intermediate edit that fails
"retry"or"uncertain"is dropped, since the next carries the whole text; a"denied"one, and any failure of a post or a part's last edit, fails the reply. A native append failing"retry"is tried again after itsretry_after; three in a row, or one"uncertain", stop the stream and carry on by post-then-edit.TooLargestarts the next part with the chunk. Result.stoppedis true when a user clicked stop.- When the deltas fail or the call is interrupted, what was received is
written out within a short timeout and left posted. A write in flight
is waited for, not cut off; only the timeout cuts it off, and its text
is then reported as
uncertain.
There are no files or actions: post them after the result. To stop a
reply, end deltas with interrupt_when or halt_when so a
Result still comes back. Native writes go through the Registry,
so a layer wrapping Chat does not see them.
| Parameter | Type | Description |
|---|---|---|
thread | ChatThread | |
deltas | Stream[str, E, RR] | |
recipient | str | None = None | |
reply_to | ReplyTo | None = None | |
max_parts | int | None = None | |
overflow | OverflowFile | None = None | |
min_write_interval | Duration | None = None | |
on_part | Callable[[Part], Effect[None, Never, RR]] | None = None |
Returns Effect[Result, ChatMarkdownError[E], _chat.Chat | Registry | RR]
def stream[E = Never, RR = Never](
thread: ChatThread,
deltas: Stream[str, E, RR],
*,
recipient: str | None = None,
reply_to: ReplyTo | None = None,
max_parts: int | None = None,
overflow: OverflowFile | None = None,
min_write_interval: Duration | None = None,
on_part: Callable[[Part], Effect[None, Never, RR]] | None = None,
) -> Effect[Result, ChatMarkdownError[E], _chat.Chat | Registry | RR]:
"""Writes a stream of Markdown deltas as one reply, as the text arrives.
- With a ``recipient`` and an adapter with a native stream, the reply is
written by native appends; otherwise, or when the first native start
fails ``Unsupported``, it posts and then edits. On an adapter without
``edit`` each part is posted once, when it rolls over or the reply
ends.
- Text that outgrows a message rolls over at a block boundary, each new
message counting against ``max_parts``. A native stream rolls over at
the next boundary past 85% of ``limits.stream_length`` and rotates at
80% of ``limits.stream_age``. Every append leaves room to close the
code fence it ends in.
- Writes are paced by ``min_write_interval`` on ``Clock``; each 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 fence.
- An intermediate edit that fails ``"retry"`` or ``"uncertain"`` is
dropped, since the next carries the whole text; a ``"denied"`` one,
and any failure of a post or a part's last edit, fails the reply. A
native append failing ``"retry"`` is tried again after its
``retry_after``; three in a row, or one ``"uncertain"``, stop the
stream and carry on by post-then-edit. ``TooLarge`` starts the next
part with the chunk.
- ``Result.stopped`` is true when a user clicked stop.
- When the deltas fail or the call is interrupted, what was received is
written out within a short timeout and left posted. A write in flight
is waited for, not cut off; only the timeout cuts it off, and its text
is then reported as ``uncertain``.
There are no files or actions: post them after the result. To stop a
reply, end ``deltas`` with ``interrupt_when`` or ``halt_when`` so a
``Result`` still comes back. Native writes go through the ``Registry``,
so a layer wrapping ``Chat`` does not see them.
"""
require_stream(deltas, "deltas")
@fx.gen
def run():
chat = yield from _chat.Chat()
registry = yield from Registry()
adapter = yield from registry.get(thread.platform).map_error(
lambda error: ChatMarkdownError[E](posted=(), remaining="", cause=ChatCause(error))
)
native = (
adapter.native_stream if "nativeStream" in adapter.descriptor.capabilities else None
)
settings: _Settings[E, RR] = _Settings(
thread=thread,
initial="",
deltas=deltas,
descriptor=adapter.descriptor,
chat=chat,
native=native,
recipient=recipient,
reply_to=reply_to,
max_parts=max_parts,
overflow=overflow,
min_write_interval=min_write_interval,
on_part=on_part,
)
result: Result = yield from _Reply(settings).run()
return result
return run.with_span("ChatMarkdown.stream", attributes={"chat.platform": thread.platform})