diff --git a/api/README.md b/api/README.md index c8dd1d4..a13bce4 100644 --- a/api/README.md +++ b/api/README.md @@ -79,7 +79,7 @@ async with AgentApi("ws://localhost:8000") as agent: ```python import asyncio import websockets -from models import OutgoingMessage, OM +from models import ServerMessage, OM async def main(): uri = "ws://localhost:8000/ws" @@ -95,7 +95,7 @@ async def main(): # 3. Читаем ответ в виде потока событий while True: msg = await ws.recv() - data = OutgoingMessage.model_validate_json(msg) + data = ServerMessage.model_validate_json(msg) match data: case OM.AgentEvent(subtype=OM.AgentEventType.TEXT_CHUNK): diff --git a/api/agent_api.py b/api/agent_api.py index 42722fe..0fb0c2c 100644 --- a/api/agent_api.py +++ b/api/agent_api.py @@ -1,7 +1,7 @@ import logging from typing import AsyncGenerator import aiohttp -from models import IM, OM, IncomingMessage, OutgoingMessage +from models import IM, OM, ClientMessage, ServerMessage logger = logging.getLogger(__name__) @@ -126,7 +126,7 @@ class AgentApi: self._connected = False raise aiohttp.ClientError(f"Failed to send message: {e}") from e - async def listen(self) -> AsyncGenerator[OutgoingMessage, None]: + async def listen(self) -> AsyncGenerator[ServerMessage, None]: """ Читает поток сообщений от сервера и десериализует их. @@ -137,7 +137,7 @@ class AgentApi: - OM.AgentEvent: Возвращает объект события Yields: - OutgoingMessage: Десериализованное сообщение от сервера + ServerMessage: Десериализованное сообщение от сервера Raises: RuntimeError: Если соединение не установлено @@ -158,7 +158,7 @@ class AgentApi: async for msg in self._ws: if msg.type == aiohttp.WSMsgType.TEXT: try: - outgoing_msg = OutgoingMessage.model_validate_json( + outgoing_msg = ServerMessage.model_validate_json( msg.data) logger.debug( f"Received message of type: {outgoing_msg.type}") diff --git a/api/models.py b/api/models.py index 2e36f06..6f41887 100644 --- a/api/models.py +++ b/api/models.py @@ -25,7 +25,7 @@ class IM: """ -IncomingMessage = Annotated[ +ClientMessage = Annotated[ Union[IM.UserMessage,], Field(discriminator="type") ] @@ -33,7 +33,7 @@ IncomingMessage = Annotated[ Объединяет все типы входящих сообщений в одно для удобной автоматической десериализации.\n Pydantic сам определит нужный тип в зависимости от поля ``type``.\n Использование:\n -msg = IncomingMessage.model_validate_json(json) +msg = ClientMessage.model_validate_json(json) """ @@ -100,7 +100,7 @@ class OM: type: Literal[OM.Type.GRACEFUL_DISCONNECT] -OutgoingMessage = Annotated[ +ServerMessage = Annotated[ Union[OM.Status, OM.AgentEvent, OM.Error, OM.GracefulDisconnect], Field(discriminator="type") ] @@ -108,5 +108,5 @@ OutgoingMessage = Annotated[ Объединяет все типы исходящих сообщений в одно для удобной автоматической десериализации.\n Pydantic сам определит нужный тип в зависимости от поля ``type``.\n Использование:\n -msg = OutgoingMessage.model_validate_json(json) +msg = ServerMessage.model_validate_json(json) """