feat(04-01): finalize AgentApi migration

This commit is contained in:
Mikhail Putilovskij 2026-04-17 16:31:48 +03:00
parent cd59d89617
commit 430c82dba1
9 changed files with 225 additions and 350 deletions

View file

@ -1,36 +1,27 @@
import pytest
from core.protocol import SettingsAction
from sdk.agent_session import build_thread_key
from sdk.interface import MessageChunk, MessageResponse, UserSettings
from sdk.prototype_state import PrototypeStateStore
from sdk.real import RealPlatformClient
class FakeAgentSessionClient:
class FakeAgentApi:
def __init__(self) -> None:
self.send_calls: list[tuple[str, str]] = []
self.stream_calls: list[tuple[str, str]] = []
self.calls: list[str] = []
self.last_tokens_used = 0
async def send_message(self, *, thread_key: str, text: str) -> MessageResponse:
self.send_calls.append((thread_key, text))
return MessageResponse(
message_id=thread_key,
response=f"echo:{text}",
tokens_used=3,
finished=True,
)
async def stream_message(self, *, thread_key: str, text: str):
self.stream_calls.append((thread_key, text))
yield MessageChunk(message_id=thread_key, delta=text[:2], finished=False)
yield MessageChunk(message_id=thread_key, delta=text[2:], finished=True, tokens_used=3)
async def send_message(self, text: str):
self.calls.append(text)
yield type("Chunk", (), {"text": text[:2]})()
yield type("Chunk", (), {"text": text[2:]})()
self.last_tokens_used = 3
@pytest.mark.asyncio
async def test_real_platform_client_get_or_create_user_uses_local_state():
client = RealPlatformClient(
agent_sessions=FakeAgentSessionClient(),
agent_api=FakeAgentApi(),
prototype_state=PrototypeStateStore(),
)
@ -45,61 +36,65 @@ async def test_real_platform_client_get_or_create_user_uses_local_state():
@pytest.mark.asyncio
async def test_real_platform_client_send_message_uses_surface_user_thread_identity():
agent_sessions = FakeAgentSessionClient()
async def test_real_platform_client_send_message_collects_stream_output():
agent_api = FakeAgentApi()
client = RealPlatformClient(
agent_sessions=agent_sessions,
agent_api=agent_api,
prototype_state=PrototypeStateStore(),
platform="matrix",
)
thread_key = build_thread_key("matrix", "@alice:example.org", "C1")
result = await client.send_message("@alice:example.org", "C1", "hello")
assert result == MessageResponse(
message_id=thread_key,
response="echo:hello",
message_id="@alice:example.org",
response="hello",
tokens_used=3,
finished=True,
)
assert agent_sessions.send_calls == [(thread_key, "hello")]
assert agent_api.calls == ["hello"]
@pytest.mark.asyncio
async def test_real_platform_client_stream_message_uses_surface_user_thread_identity():
agent_sessions = FakeAgentSessionClient()
async def test_real_platform_client_stream_message_emits_final_tokens_chunk():
agent_api = FakeAgentApi()
client = RealPlatformClient(
agent_sessions=agent_sessions,
agent_api=agent_api,
prototype_state=PrototypeStateStore(),
platform="matrix",
)
thread_key = build_thread_key("matrix", "@alice:example.org", "C1")
chunks = []
async for chunk in client.stream_message("@alice:example.org", "C1", "hello"):
chunks.append(chunk)
assert chunks == [
MessageChunk(
message_id=thread_key,
message_id="@alice:example.org",
delta="he",
finished=False,
tokens_used=0,
),
MessageChunk(
message_id=thread_key,
message_id="@alice:example.org",
delta="llo",
finished=False,
tokens_used=0,
),
MessageChunk(
message_id="@alice:example.org",
delta="",
finished=True,
tokens_used=3,
),
]
assert agent_sessions.stream_calls == [(thread_key, "hello")]
assert agent_api.calls == ["hello"]
@pytest.mark.asyncio
async def test_real_platform_client_settings_are_local():
client = RealPlatformClient(
agent_sessions=FakeAgentSessionClient(),
agent_api=FakeAgentApi(),
prototype_state=PrototypeStateStore(),
platform="matrix",
)