mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:28:34 +00:00
80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
"""Door configuration selects persistence independently of handler grouping and routing."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from livekit.agents import AgentHandler, AgentServer, JobContext
|
|
from livekit.agents.a2a import A2ASessionContext
|
|
from livekit.agents.storage import Storage
|
|
|
|
from .test_a2a_server import (
|
|
AGENT,
|
|
API_KEY,
|
|
API_SECRET,
|
|
http_client,
|
|
sse_results,
|
|
wire,
|
|
)
|
|
|
|
pytestmark = [pytest.mark.unit]
|
|
|
|
|
|
async def call(ctx: JobContext) -> None:
|
|
pass
|
|
|
|
|
|
def test_empty_partition_keys_fail_at_definition() -> None:
|
|
handler = AgentHandler()
|
|
with pytest.raises(ValueError, match="partition_key"):
|
|
handler.a2a_session(endpoint="text", description="text", partition_key="")(echo)
|
|
with pytest.raises(ValueError, match="partition_key"):
|
|
handler.rtc_session(partition_key="")(call)
|
|
|
|
|
|
async def partitions_of(storage: Storage, thread_id: str) -> set[str]:
|
|
core = await storage._open(thread_id)
|
|
try:
|
|
return {partition_key.partition_key for partition_key in await core.partitions()}
|
|
finally:
|
|
core.close()
|
|
|
|
|
|
async def echo(ctx: A2ASessionContext) -> str:
|
|
return f"heard {ctx.message.text_content}"
|
|
|
|
|
|
async def stream(http: Any, path: str, message: dict[str, Any]) -> list[dict[str, Any]]:
|
|
body = {
|
|
"jsonrpc": "2.0",
|
|
"id": "message/stream",
|
|
"method": "message/stream",
|
|
"params": {"message": message},
|
|
}
|
|
return [r["result"] for r in sse_results(await http.post(path, json=body))]
|
|
|
|
|
|
async def test_a_handler_with_a_partition_of_its_own(thread_storage: Storage) -> None:
|
|
front_desk = AgentHandler()
|
|
front_desk.a2a_session(endpoint="frontend", description="the front desk")(echo)
|
|
back_office = AgentHandler()
|
|
back_office.a2a_session(
|
|
partition_key="back-office", endpoint="backend", description="the back office"
|
|
)(echo)
|
|
front = AgentServer(
|
|
storage=thread_storage, agent_name=AGENT, api_key=API_KEY, api_secret=API_SECRET
|
|
)
|
|
front.add(front_desk, back_office)
|
|
assert set(front._a2a_endpoints) == {"frontend", "backend"}
|
|
|
|
# A thread grant authorizes both doors for the same thread.
|
|
async with http_client(front) as http:
|
|
first = await stream(http, "/frontend/v1", wire("hi", "m1"))
|
|
thread_id = first[0]["task"]["contextId"]
|
|
second = await stream(http, "/backend/v1", wire("hello", "m2", context_id=thread_id))
|
|
assert second[-1]["statusUpdate"]["status"]["state"] == "TASK_STATE_COMPLETED"
|
|
# the same thread, two partitions: the worker's for one door, the other door's own
|
|
assert await partitions_of(thread_storage, thread_id) == {"default", "back-office"}
|