mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
- Added Peer class for managing peer-to-peer communication, including initialization, invocation, and cancellation of requests. - Introduced Transport abstract base class with StdioTransport and WebSocketTransport implementations for message handling. - Created Star class for error handling in event-driven architecture. - Developed MemoryTransport for testing purposes, allowing in-memory communication between peers. - Implemented unit tests for Peer functionality, protocol message parsing, and runtime integration with plugins. - Established entry point tests to ensure package import and command-line interface functionality.
41 lines
995 B
Python
41 lines
995 B
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
|
|
from astrbot_sdk.runtime.transport import Transport
|
|
|
|
|
|
class MemoryTransport(Transport):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.partner: "MemoryTransport | None" = None
|
|
|
|
async def start(self) -> None:
|
|
self._closed.clear()
|
|
|
|
async def stop(self) -> None:
|
|
self._closed.set()
|
|
|
|
async def send(self, payload: str) -> None:
|
|
if self.partner is None:
|
|
raise RuntimeError("MemoryTransport 未连接 partner")
|
|
await self.partner._dispatch(payload)
|
|
|
|
|
|
def make_transport_pair() -> tuple[MemoryTransport, MemoryTransport]:
|
|
left = MemoryTransport()
|
|
right = MemoryTransport()
|
|
left.partner = right
|
|
right.partner = left
|
|
return left, right
|
|
|
|
|
|
class FakeEnvManager:
|
|
def prepare_environment(self, _plugin) -> Path:
|
|
return Path(__import__("sys").executable)
|
|
|
|
|
|
async def drain_loop() -> None:
|
|
await asyncio.sleep(0.05)
|