Repository navigation
a2a: fix delegation from a worker run as a script - #8602
Conversation
| from livekit.agents.storage import AgentDBStorage # noqa: E402 | ||
| from livekit.agents.storage import AgentDBStorage, SQLiteStorage # noqa: E402 | ||
|
|
||
| server = AgentServer(storage=AgentDBStorage(), agent_name="dentist") | ||
| # AgentDB needs a LiveKit Cloud project; AGENT_STORAGE=sqlite keeps threads in a local file | ||
| storage = SQLiteStorage() if os.environ.get("AGENT_STORAGE") == "sqlite" else AgentDBStorage() | ||
| server = AgentServer(storage=storage, agent_name="dentist") |
There was a problem hiding this comment.
Maybe the user shouldn't specify any storage by default?
And we automatically picked based on whether they use cloud or not?
This way, we can just remove the above code
There was a problem hiding this comment.
I had the same idea initially, we may automatically select the storage for users based where the worker is running and if they connected to lk cloud.
but on the other hand, it feels like the storage should be set explicitly since it controls where the data persisted to, otherwise you may write the data to an unexpected location...
There was a problem hiding this comment.
That's fair, I like the idea of always having storage very explicit. Tho most users are going to use Cloud, so I don't think it's the end of the world if we pick automatically. It's going to be right 99% of the time.
Tho in some scenarios, we should also short-circuit this decision, e.g when using lk agents console
Maybe a good middleground is to log the active storage at the sever startup?
A worker run as a script (`lk agent dev`, `python -m livekit.agents start main.py`) could not delegate from a job process: the spawned process re-imports main.py as __mp_main__ before job_main applies the worker's environment, so AgentServer() copies settings without LIVEKIT_AGENT_ENDPOINTS_URL and delegation_url fails with "the server has not started serving". delegation_url reads LIVEKIT_AGENT_ENDPOINTS_URL again when the copy has none. The e2e runner now runs a path= app as __main__ the way `livekit.agents start` does, instead of preloading it as a module after the environment is applied, so the self-hosted delegation journey takes this path. A call that starts without a thread warns about the delegates it cannot reach, and a delegate's 403 becomes a logged error that says to dispatch the call with thread_id; the model gets only that the delegate is not available.
ff875f1 to
0b0ae05
Compare
0a96b64 to
62c6daa
Compare
A
RemoteAgenton a call shares the call's thread with its delegate (auth.md), reached through LiveKit Cloud or, on a self-hosted server, at the worker's own listener (tools.md). This fixes what still keeps that from working for a worker run withlk agent dev:LIVEKIT_AGENT_ENDPOINTS_URLwhen it delegates. A script runs as__main__, so a spawned job process re-imports it as__mp_main__beforejob_mainapplies the worker's environment, andAgentServer()copies settings without the endpoints URL. On Cloud and self-hosted alike, the call fails at session start withendpoint "backend" is not reachable yet: the server has not started serving.path=app as__main__. It preloaded the app as a module after the environment was applied, so the self-hosted delegation journey passed whilelk agent devfailed. It now takes the same path aslivekit.agents start main.py.thread_id.Context for reviewing and coding agents