Переимнованы OutgoingMessage в ServerMessage и IncomingMessage в ClientMessage

This commit is contained in:
Ярослав Малинин 2026-03-30 10:05:19 +03:00
parent dab8cf6335
commit f2d50ae88b
3 changed files with 10 additions and 10 deletions

View file

@ -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):

View file

@ -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}")

View file

@ -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)
"""