mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 03:08:33 +00:00
Merge remote-tracking branch 'origin/v2.0' into codex/delegation
This commit is contained in:
commit
34118eedd8
6 files changed
+183
-27
No files matched your search
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import AsyncIterable, Awaitable, Callable
|
||||
import inspect
|
||||
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable
|
||||
from contextlib import aclosing
|
||||
from typing import cast
|
||||
|
||||
from .. import _core
|
||||
@@ -14,7 +16,7 @@ from ..voice.agent_run import RunHooks, reset_run_hooks, set_run_hooks
|
||||
from . import StorageError, Thread
|
||||
from .context import ContextStore
|
||||
|
||||
_Handler = Callable[["A2ASessionContext"], Awaitable[object]]
|
||||
_Handler = Callable[["A2ASessionContext"], Awaitable[object] | AsyncIterator[str]]
|
||||
|
||||
|
||||
class A2ASessionContext:
|
||||
@@ -112,7 +114,14 @@ class _TurnHost:
|
||||
|
||||
token = set_run_hooks(hooks)
|
||||
try:
|
||||
output = await self._handler(context)
|
||||
call = self._handler(context)
|
||||
if inspect.isasyncgen(call):
|
||||
async with aclosing(call):
|
||||
text = await context.stream(_texts(call))
|
||||
# the outcome is the last run the handler streamed, else the text it yielded
|
||||
output: object = results[-1] if results else text
|
||||
else:
|
||||
output = await cast(Awaitable[object], call)
|
||||
except StorageError:
|
||||
raise
|
||||
except Exception as e:
|
||||
@@ -163,6 +172,15 @@ class _TurnHost:
|
||||
)
|
||||
|
||||
|
||||
async def _texts(handler: AsyncIterator[object]) -> AsyncIterator[str]:
|
||||
async for chunk in handler:
|
||||
if not isinstance(chunk, str):
|
||||
raise TypeError(
|
||||
f"an a2a_session handler yields the answer's text as str, not {type(chunk).__name__}"
|
||||
)
|
||||
yield chunk
|
||||
|
||||
|
||||
__all__ = [
|
||||
"A2ASessionContext",
|
||||
]
|
||||
Reference in new issue
Block a user