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'smeasurecounts. - Adapters retry
"retry"failures withretry_safeand never retry"uncertain"ones.
Re-exports#
Types#
| Type | Definition | Description |
|---|---|---|
Measure | type Measure = Callable[[str, Sequence[Action]], int] | What a Markdown string costs, with the |
Direction | type 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.
| Parameter | Type | Description |
|---|---|---|
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.
| Parameter | Type | Description |
|---|---|---|
descriptor | Descriptor | |
op | str | |
markdown | str | |
actions | Sequence[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.
| Parameter | Type | Description |
|---|---|---|
effect | Effect[A, ChatError, R] | |
max_retry_after | Duration | 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)
)