Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ if __name__ == "__main__":

| `AgentServer` option | What |
| --- | --- |
| `storage` | where threads live; default the local `SQLiteStorage()`, so a server always has storage |
| `storage` | where threads live; default `AgentDBStorage()` on LiveKit Cloud and the local `SQLiteStorage()` on any other server or in console, so a server always has storage |
| `agent_name`, `preload_modules` | see above and below |
| `ws_url`, `api_key`, `api_secret` | the LiveKit project; default `LIVEKIT_URL`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET` |
| `num_idle_processes` | job processes kept warm ahead of dispatch; `min(ceil(cpus), 4)` in production, none in development |
Expand Down
3 changes: 1 addition & 2 deletions examples/airline-support/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import frontend_agent # noqa: E402

from livekit.agents import AgentServer, cli # noqa: E402
from livekit.agents.storage import AgentDBStorage # noqa: E402

server = AgentServer(storage=AgentDBStorage(), agent_name="skyline-support")
server = AgentServer(agent_name="skyline-support")
server.add(frontend_agent.front_desk, backend_agent.back_office)

if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions examples/delegation-voice/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import frontend_agent # noqa: E402

from livekit.agents import AgentServer, cli # noqa: E402
from livekit.agents.storage import AgentDBStorage # noqa: E402

server = AgentServer(storage=AgentDBStorage(), agent_name="delegation-voice")
server = AgentServer(agent_name="delegation-voice")
server.add(frontend_agent.front_desk, backend_agent.back_office)

if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions examples/frontdesk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import frontend_agent # noqa: E402

from livekit.agents import AgentServer, cli # noqa: E402
from livekit.agents.storage import AgentDBStorage # noqa: E402

server = AgentServer(storage=AgentDBStorage(), agent_name="dentist")
server = AgentServer(agent_name="dentist")
server.add(frontend_agent.front_desk, backend_agent.back_office)

if __name__ == "__main__":
Expand Down
3 changes: 1 addition & 2 deletions examples/hotel-receptionist/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import frontend_agent # noqa: E402

from livekit.agents import AgentServer, cli # noqa: E402
from livekit.agents.storage import AgentDBStorage # noqa: E402

server = AgentServer(storage=AgentDBStorage(), agent_name="hotel")
server = AgentServer(agent_name="hotel")
server.add(frontend_agent.front_desk, backend_agent.back_office)

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion python/livekit-agents/livekit/agents/a2a.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def __init__(
self._url: str | None = None
self._config: ServerConfig | None = None

def bind(self, *, storage: Storage, config: ServerConfig) -> None:
def bind(self, *, storage: Storage | None, config: ServerConfig) -> None:
"""Attach the endpoint to a server's storage and configuration."""
self._storage = self._storage or storage
self._config = config
Expand Down
1 change: 1 addition & 0 deletions python/livekit-agents/livekit/agents/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def _run_console(server: AgentServer, connect_addr: str, record: bool) -> None:
setup_logging("debug", "console")
ConsoleCall.addr, ConsoleCall.record = connect_addr, record
server._update_options(ProcessOptions(job_executor_type=JobExecutorType.THREAD))
server._ensure_storage(console=True)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
Expand Down
37 changes: 29 additions & 8 deletions python/livekit-agents/livekit/agents/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ class AgentServer(AgentHandler):

Options left out default from the environment where one applies (``LIVEKIT_URL``,
``LIVEKIT_AGENT_NAME``, ...); ``load_threshold``, ``num_idle_processes`` and the listeners
default differently in development. A local ``SQLiteStorage`` serves the thread API in
development, and on the port ``LIVEKIT_THREAD_API_PORT`` names otherwise.
default differently in development. Without ``storage``, threads live in agent-db on LiveKit
Cloud and in a local ``SQLiteStorage`` on any other server or in console. A local
``SQLiteStorage`` serves the thread API in development, and on the port
``LIVEKIT_THREAD_API_PORT`` names otherwise.
"""

def __init__(
Expand Down Expand Up @@ -164,10 +166,23 @@ def storage(self) -> Storage | None:
"""The session storage every door of this server opens its partition_key with."""
return self._storage

def _ensure_storage(self) -> Storage:
if self._storage is None:
from .storage import SQLiteStorage

def _ensure_storage(self, *, console: bool = False) -> Storage:
"""The storage given, else one picked once: local SQLite for a console call, agent-db
in the server's own project on LiveKit Cloud, and local SQLite on any other server."""
if self._storage is not None:
return self._storage
from urllib.parse import urlparse

from .storage import AgentDBStorage, SQLiteStorage, _rebuild

url = self._config.ws_url
cloud = (urlparse(url).hostname or "").endswith((".livekit.cloud", ".livekit.run"))
if cloud and not console:
# the server's own project, which its options may give without the environment
project = {"url": url, "api_key": self._config.api_key or None}
project["api_secret"] = self._config.api_secret or None
self._storage = _rebuild(AgentDBStorage, project)
else:
self._storage = SQLiteStorage()
return self._storage

Expand Down Expand Up @@ -197,8 +212,8 @@ def add(
raise TypeError(f"not a door: {door!r}")
if door.name in self._a2a_endpoints:
raise ValueError(f"a2a_session(endpoint={door.name!r}) is already served")
storage = self._ensure_storage()
door.bind(storage=storage, config=self._config)
# the default storage is chosen when the server runs, where console can choose it
door.bind(storage=self._storage, config=self._config)
self._a2a_endpoints[door.name] = door
return doors

Expand All @@ -214,6 +229,12 @@ async def _run(self, *, devmode: bool = False, unregistered: bool = False) -> No
extra={"endpoints": unserved},
)
storage = self._ensure_storage()
for door in self._a2a_endpoints.values():
door.bind(storage=storage, config=self._config)
logger.info(
"thread storage",
extra={"storage": type(storage).__name__, "location": storage.threads.location},
)
runtime = self._ensure_runtime()
from .storage import SQLiteStorage

Expand Down
4 changes: 2 additions & 2 deletions rust/crates/lk-agents-storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ impl Namespace {
StorageError::Unavailable(format!("storage runtime: {e}"))
})?;
let clock = self.env.clock.clone();
let workers = Workers::threads(Background::new(Arc::default(), spawner, clock));
let workers =
Workers::threads(Background::new(Arc::default(), spawner, clock));
let storage = SqliteStorage::open(directory, *lease, workers).await?;
let storage = storage.with_telemetry(self.cx.clone());
log::info!(target: lk_agents_abi::LOG_TARGET, "local thread storage in {}", directory.display());
Backend::Local(Arc::new(storage))
}
Config::Cloud { opts, minter } => {
Expand Down
30 changes: 30 additions & 0 deletions tests/host/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""The storage an AgentServer picks when it is given none."""

from __future__ import annotations

import pytest

from livekit.agents import AgentServer
from livekit.agents.storage import AgentDBStorage, SQLiteStorage

CLOUD = {"ws_url": "wss://demo.livekit.cloud", "api_key": "key", "api_secret": "secret"}


@pytest.fixture(autouse=True)
def no_project_env(monkeypatch: pytest.MonkeyPatch, tmp_path: object) -> None:
for name in ("LIVEKIT_URL", "LIVEKIT_API_KEY", "LIVEKIT_API_SECRET"):
monkeypatch.delenv(name, raising=False)
monkeypatch.setenv("LIVEKIT_AGENT_THREADS_DIR", str(tmp_path))


def test_a_cloud_server_configured_by_options_alone_keeps_threads_in_its_project() -> None:
server = AgentServer(**CLOUD)

@server.a2a_session(endpoint="support", description="Answers questions.")
async def support(ctx: object) -> None: ...

assert isinstance(server._ensure_storage(), AgentDBStorage)


def test_the_console_keeps_threads_locally_on_a_cloud_project() -> None:
assert isinstance(AgentServer(**CLOUD)._ensure_storage(console=True), SQLiteStorage)
Loading