From 234050df9faf6eb427b15527e0c5cd4710366a51 Mon Sep 17 00:00:00 2001 From: MrKan Date: Sun, 19 Apr 2026 15:12:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B0=D0=BA=D1=82=D1=83=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B4=D0=BE=D0=BA=D1=83=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B8=20manual=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 41 +++++++---------------------------------- tests/manual.py | 9 +++++++-- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 496c876..470317e 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ from lambda_agent_api import AgentApi WebSocket API SDK для взаимодействия с AI-агентом. +## Release Notes +# v1.1 +- Добавлен параметр `chat_id` в конструктор `AgentAPI`. Нужен для разделения истории сообщений по чатам/веткам. +- `AgentAPI.connect()` вызывает `AgentBusyException`, если выбранный чат уже занят другим API клиентом. + ## Установка В `master` всегда будет актуальная рабочая версия. ```bash @@ -14,42 +19,10 @@ pip install git+https://git.lambda.coredump.ru/platform/agent_api.git ## Быстрый старт (с использованием AgentApi) -```python -import asyncio - -from lambda_agent_api.agent_api import AgentApi -from lambda_agent_api.server import MsgEventTextChunk +**Рабочий REPL пример: [tests/manual.py](tests/manual.py).** -async def main(): - api = AgentApi("agent-1", "ws://localhost:8000/ws") - - await api.connect() - try: - response = await api.send_message("Привет, агент!") - - async for chunk in response: - if isinstance(chunk, MsgEventTextChunk): - print(chunk.text, end="", flush=True) - elif isinstance(chunk, MsgEventToolCallChunk): - print(f"Tool call started: {chunk.tool_name}") - elif isinstance(chunk, MsgEventToolResult): - print(f"Tool result: {chunk.result}") - elif isinstance(chunk, MsgEventCustomUpdate): - print(f"Progress update: {chunk.payload}") - elif isinstance(chunk, MsgEventEnd): - print(f"Generation ended, tokens used: {chunk.tokens_used}") - - finally: - await api.close() - - -asyncio.run(main()) -``` - -> `AgentApi.send_message()` возвращает стриминг-итерируемый объект, который может выдавать не только текстовые чанки, но и события инструментов (`MsgEventToolCallChunk`, `MsgEventToolResult`, `MsgEventCustomUpdate`) и финальный `MsgEventEnd`. - -## Предполагаемое использование +## Предполагаемое управление подключениями ```python from lambda_agent_api.agent_api import AgentApi diff --git a/tests/manual.py b/tests/manual.py index b801a2d..0ba6e0a 100644 --- a/tests/manual.py +++ b/tests/manual.py @@ -1,7 +1,7 @@ import asyncio import traceback -from lambda_agent_api.agent_api import AgentApi +from lambda_agent_api.agent_api import AgentApi, AgentBusyException from lambda_agent_api.server import MsgEventTextChunk, MsgEventToolCallChunk, MsgEventToolResult @@ -10,7 +10,12 @@ async def main(): chat_id = input("Chat id: ") or 0 api = AgentApi("agent-1", "ws://localhost:8000/agent_ws/", chat_id=chat_id) - await api.connect() + try: + await api.connect() + except AgentBusyException: + print(f"Чат {chat_id} занят другим клиентом") + return + while True: try: prompt = await asyncio.get_event_loop().run_in_executor(None, input, ">>> ")