128 lines
3.4 KiB
Python
128 lines
3.4 KiB
Python
"""入站/出站中间件协议、上下文与结果定义。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
from dataclasses import dataclass, field
|
|
from typing import TYPE_CHECKING, Protocol, runtime_checkable
|
|
|
|
if TYPE_CHECKING:
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from yuxi.channel.plugins.protocol import (
|
|
BindingRoute,
|
|
ChannelPlugin,
|
|
DeliveryCapabilities,
|
|
InboundMessage,
|
|
OutboundMessage,
|
|
)
|
|
from yuxi.storage.postgres.model_channel import ChannelSession
|
|
from yuxi.storage.postgres.models_business import Conversation, Message
|
|
|
|
|
|
@dataclass
|
|
class InboundContext:
|
|
"""入站中间件链执行上下文。"""
|
|
|
|
channel_type: str
|
|
account_id: str
|
|
inbound: InboundMessage
|
|
config: dict
|
|
config_mw: dict
|
|
plugin: ChannelPlugin
|
|
db: AsyncSession | None = None
|
|
session: ChannelSession | None = None
|
|
route: BindingRoute | None = None
|
|
run_result: dict | None = None
|
|
|
|
|
|
@dataclass
|
|
class InboundResult:
|
|
"""入站中间件链执行结果。"""
|
|
|
|
accepted: bool
|
|
reason: str | None = None
|
|
run_id: str | None = None
|
|
pairing_code: str | None = None
|
|
qr_content: str | None = None
|
|
qr_reply: OutboundMessage | None = None
|
|
|
|
|
|
@runtime_checkable
|
|
class InboundMiddleware(Protocol):
|
|
"""入站中间件协议。"""
|
|
|
|
name: str
|
|
default_order: int = 0
|
|
|
|
async def process(
|
|
self,
|
|
ctx: InboundContext,
|
|
next_mw: Callable[[], Awaitable[InboundResult]],
|
|
) -> InboundResult:
|
|
"""处理入站上下文并决定是否继续后续中间件。"""
|
|
...
|
|
|
|
async def start(self) -> None:
|
|
"""可选生命周期启动钩子。"""
|
|
|
|
async def stop(self) -> None:
|
|
"""可选生命周期停止钩子。"""
|
|
|
|
|
|
@dataclass
|
|
class OutboundContext:
|
|
"""出站中间件链执行上下文。"""
|
|
|
|
event: dict
|
|
message: OutboundMessage | None
|
|
config: dict
|
|
config_mw: dict
|
|
plugin: ChannelPlugin
|
|
channel_session: ChannelSession | None
|
|
conversation: Conversation | None
|
|
db: AsyncSession
|
|
capabilities: DeliveryCapabilities
|
|
chunks: list[OutboundMessage] = field(default_factory=list)
|
|
payloads: list[dict] = field(default_factory=list)
|
|
sent_ids: list[str | None] = field(default_factory=list)
|
|
chunk_statuses: list[dict] = field(default_factory=list)
|
|
pending_chunk_indexes: list[int] = field(default_factory=list)
|
|
dispatch_result: str = "success"
|
|
retry_after: int | None = None
|
|
# 以下字段由 OutboundDispatcher 注入,供状态更新与指标使用
|
|
db_message: Message | None = None
|
|
labels: dict[str, str] = field(default_factory=dict)
|
|
start_time: float = 0.0
|
|
update_status: Callable[[OutboundContext, str], Awaitable[None]] | None = None
|
|
|
|
|
|
@dataclass
|
|
class OutboundResult:
|
|
"""出站中间件链执行结果。"""
|
|
|
|
status: str
|
|
retry_after: int | None = None
|
|
|
|
|
|
@runtime_checkable
|
|
class OutboundMiddleware(Protocol):
|
|
"""出站中间件协议。"""
|
|
|
|
name: str
|
|
default_order: int = 0
|
|
|
|
async def process(
|
|
self,
|
|
ctx: OutboundContext,
|
|
next_mw: Callable[[], Awaitable[OutboundResult]],
|
|
) -> OutboundResult:
|
|
"""处理出站上下文并决定是否继续后续中间件。"""
|
|
...
|
|
|
|
async def start(self) -> None:
|
|
"""可选生命周期启动钩子。"""
|
|
|
|
async def stop(self) -> None:
|
|
"""可选生命周期停止钩子。"""
|