GitHub

Chat

Post, edit and stream Markdown to chat platforms through one interface, with typed buttons and commands and an in-memory platform for tests.

pyfect.chat is Effect's effect/chat: one Chat service in front of any number of chat platforms. Each platform is an adapter registered with chat_adapter.layer(...), and every call names its target with a ChatThread, so a thread on one platform routes to that platform's adapter. A thread on a platform with no adapter fails with a ChatError whose reason is NoAdapter.

Every call fails with ChatError. Its reason says what happened (RateLimited, Forbidden, TooLarge, Unsupported and the rest) and retry says whether trying again is safe: "retry" when nothing happened, "uncertain" when the write may have landed, "denied" when it will fail again. Adapters retry "retry" failures up to three times, after the platform's retry_after or a backoff (chat_adapter.retry_safe). A ChatError prints only the platform, op, reason and redacted detail, never the cause, so a URL or token in an HTTP error stays out of logs.

Replies that fit

Platforms cap a message's length, and a model's answer often runs past it. chat_markdown.post(thread, markdown) splits Markdown at block boundaries so each part fits, closes a code fence at the end of a part and reopens it at the start of the next, and never cuts inside a grapheme cluster. max_parts bounds the number of messages; past it the call fails with ChatMarkdownError, whose posted, remaining and remaining_fence say what was sent and what was not, or overflow=OverflowFile(...) attaches the rest as a file.

chat_markdown.stream(thread, deltas) writes a stream of deltas as they arrive. With a recipient it uses the platform's native streaming, where it has one; otherwise it posts a message and edits it as it grows, at most once per min_write_interval. When the deltas fail or the call is interrupted, what was received is finished and the error says which text is posted, uncertain or never sent.

Events from the platform go to the ChatHandler, built with chat_handler.layer(handle). It skips the bot's own messages unless deliver_self=True, and runs each event in its own scope.

replies.py
from typing import Never

import pyfect as fx
from pyfect import stream
from pyfect.chat import chat_handler, chat_markdown, chat_test
from pyfect.chat.chat import Chat
from pyfect.chat.chat_event import ChatEvent, MessageCreated
from pyfect.chat.chat_test import ChatTest


# The handler answers every message with a reply split to the platform's limits.
def on_event(event: ChatEvent) -> fx.Effect[None, Never, Chat]:
    if not isinstance(event, MessageCreated):
        return fx.void
    report = "\n\n".join(f"Step {i}: done." for i in range(1, 6))
    return chat_markdown.post(event.thread, report).as_void().or_die()


@fx.gen
def program():
    test = yield from ChatTest()
    thread = yield from test.make_thread()
    yield from test.deliver((yield from test.user_posts(thread, text="status?")))
    for message in (yield from test.messages(thread)):
        print(repr(message.text))

    # Deltas from a model stream in as they arrive: one message, edited as it grows.
    deltas = stream.from_iterable(["Thinking", " about it", "...\n\nDone."])
    result = yield from chat_markdown.stream(thread, deltas)
    print([(part.transport, part.text) for part in result.parts])


app = program.pipe(fx.provide(chat_handler.layer(on_event))).pipe(
    fx.provide(chat_test.layer(message_length=40))
)
fx.run_blocking(app.or_die())

Buttons, selects, modals and commands

chat_interaction defines interactive elements with typed values. ci.button(id, label=..., value=schema) defines a button; .of(value) encodes the value into the element to post, and the click handler gets it decoded. ci.modal defines a dialog of text_input, choice and select_input fields whose submit handler gets each field's value decoded, and ci.command a slash command. A group(...) of definitions becomes a layer with .to_layer(*handlers), each handler made by its definition's .handler(fn), so a handler that takes the wrong value type does not type-check.

Platforms want an answer to an interaction within 3 seconds. ci.handle gives the handler that long and answers Late past it; a late click that opens a modal reports the modal as refused instead of opening it. A click with an unknown id, or a value that no longer decodes, is answered without calling a handler, and ci.layer(unhandled=...) sees it.

interactions.py
from typing import TypedDict

import pyfect as fx
from pyfect import layer
from pyfect import schema as S
from pyfect.chat import chat_interaction as ci
from pyfect.chat import chat_test
from pyfect.chat.chat import Chat
from pyfect.chat.chat_test import ChatTest


class Deploy(TypedDict):
    version: str


# A button whose value is typed: the click handler gets a Deploy, decoded.
approve = ci.button("approve", label="Approve", value=S.Struct(Deploy))


def on_approve(click: ci.Click[Deploy]) -> fx.Effect[ci.ClickReply]:
    return fx.sync(lambda: print("approved", click.value["version"], "by", click.user_id)).as_(
        ci.ack
    )


interactions = (
    ci.group(approve).to_layer(approve.handler(on_approve)).pipe(layer.provide_merge(ci.layer()))
)


@fx.gen
def program():
    chat = yield from Chat()
    test = yield from ChatTest()
    thread = yield from test.make_thread()
    posted = yield from chat.post(
        thread, markdown="Ship v1.4.2?", actions=[approve.of({"version": "v1.4.2"})]
    )
    # The test platform clicks the button and runs the handler within the 3 second deadline.
    click = yield from test.click(posted.id, "approve", user_id="U42")
    print((yield from test.interact(click)))


fx.run_blocking(program.pipe(fx.provide(interactions)).pipe(fx.provide(chat_test.layer())).or_die())

Testing

chat_test.layer() registers an in-memory platform and provides ChatTest, which plays the users: user_posts, click, submit, command, click_stop and close_stream make events, and messages(thread) and calls show what the bot did. fail_next(op, reason) makes the next call fail, with landed=True for a write that went through anyway, and keyword limits such as message_length or min_write_interval set the platform's limits. Its time runs on the test clock.

chat_test.conformance(...) is the suite Effect's adapters pass: a dict of named rules, each an effect, run against an adapter with its own hooks for posting as a user and injecting failures. Run each rule as a test to check an adapter you write.

The adapters have their own pages: Slack, Microsoft Teams, Discord, WhatsApp and Telegram.

Reference: chat, chat_markdown, chat_interaction, chat_test.