GitHub

chat_adapter

API reference for chat_adapter.

What a chat platform adapter implements (Effect's effect/chat/ChatAdapter).

An adapter is a set of Ops plus a static Descriptor of what the platform supports. Adapters register in the Registry, which Chat routes through by thread.platform.

  • Every op exists on every platform. Where the platform has no equivalent the op fails with Unsupported, naming the capability.
  • A post notifies nobody unless asked: mentions in Markdown do not notify.
  • Length is checked before the call, with check_message_length, in the unit the descriptor's measure counts.
  • Adapters retry "retry" failures with retry_safe and never retry "uncertain" ones.

Re-exports

Types

TypeDefinitionDescription
Measuretype Measure = Callable[[str, Sequence[Action]], int]

What a Markdown string costs, with the actions its message carries: pure, an upper bound of what the adapter sends, subadditive, ASCII letters at one each, and measure(md, ()) == measure(md).

Directiontype Direction = Literal['backward', 'forward']

Classes

Functions

layer

layer[E = Never](adapter: Effect[ChatAdapter, E, Scope]) -> Layer[Never, E, Registry]
layer[E, R](adapter: Effect[ChatAdapter, E, Scope | R]) -> Layer[Never, E, R | Registry]

The layer that registers adapter in the Registry. A platform that is off is a layer left out.

ParameterTypeDescription
adapter
@overload
def layer[E = Never](adapter: Effect[ChatAdapter, E, Scope]) -> Layer[Never, E, Registry]: ...
@overload
def layer[E, R](adapter: Effect[ChatAdapter, E, Scope | R]) -> Layer[Never, E, R | Registry]: ...
def layer[E, R](adapter: Effect[ChatAdapter, E, Scope | R]) -> Layer[Never, E, R | Registry]:
    """The layer that registers ``adapter`` in the ``Registry``. A platform
    that is off is a layer left out."""
    require_effect(adapter, "adapter")

    @fx.gen
    def register():
        registry = yield from Registry()
        made = yield from adapter
        yield from registry.register(made)

    return _layer_effect_discard(register)

check_message_length

check_message_length(descriptor, op, markdown, actions=()) -> Effect[None, ChatError]

Fails TooLarge when markdown (with its actions) does not fit limits.message_length, measured with the descriptor's measure. Pass the string the adapter is about to send, after escaping.

ParameterTypeDescription
descriptorDescriptor
opstr
markdownstr
actionsSequence[Action] = ()

Returns Effect[None, ChatError]

def check_message_length(
    descriptor: Descriptor, op: str, markdown: str, actions: Sequence[Action] = ()
) -> Effect[None, ChatError]:
    """Fails ``TooLarge`` when ``markdown`` (with its ``actions``) does not
    fit ``limits.message_length``, measured with the descriptor's
    ``measure``. Pass the string the adapter is about to send, after
    escaping."""
    if descriptor.measure(markdown, actions) <= descriptor.limits.message_length:
        return fx.void
    return fx.fail(
        ChatError(
            platform=descriptor.platform,
            op=op,
            reason=TooLarge(subject="text", detail="message_too_long"),
        )
    )

retry_safe

retry_safe[A, R](effect, *, max_retry_after=None) -> Effect[A, ChatError, R]

Retries an adapter request with the shared policy: only "retry" failures, up to three times, after the error's retry_after or an exponential backoff from 500 milliseconds. Inside chat.without_retries the request runs once.

A retry_after longer than max_retry_after is not waited out: the error comes back at once, the wait on it. None, the default, waits out any, as Effect does.

ParameterTypeDescription
effectEffect[A, ChatError, R]
max_retry_afterDuration | None = None

Returns Effect[A, ChatError, R]

def retry_safe[A, R](
    effect: Effect[A, ChatError, R], *, max_retry_after: Duration | None = None
) -> Effect[A, ChatError, R]:
    """Retries an adapter request with the shared policy: only ``"retry"``
    failures, up to three times, after the error's ``retry_after`` or an
    exponential backoff from 500 milliseconds. Inside
    ``chat.without_retries`` the request runs once.

    A ``retry_after`` longer than ``max_retry_after`` is not waited out: the
    error comes back at once, the wait on it. ``None``, the default, waits
    out any, as Effect does."""
    require_effect(effect, "effect")
    return RetriesDisabled().flat_map(
        lambda disabled: effect if disabled else _retrying(effect, 0, max_retry_after)
    )