Skip to content

Commit

Permalink
rework how parameters are set and checked by agent Worker (#141)
Browse files Browse the repository at this point in the history
* rework how parameters are set and checked when interacting with an agent Worker

* add ruff linting
  • Loading branch information
dsa authored Feb 1, 2024
1 parent 94d663b commit 27ea3c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion livekit-agents/livekit/agents/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "0.3.0"
__version__ = "0.3.1"
61 changes: 37 additions & 24 deletions livekit-agents/livekit/agents/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,22 @@ def __init__(
api_secret (str, optional): LiveKit API Secret. Defaults to os.environ.get("LIVEKIT_API_SECRET", "").
"""

ws_url = ws_url or os.environ.get("LIVEKIT_URL") or "ws://localhost:7880"
api_key = api_key or os.environ.get("LIVEKIT_API_KEY")
api_secret = api_secret or os.environ.get("LIVEKIT_API_SECRET")

if not api_key:
raise ValueError(
"No api key provided, set LIVEKIT_API_KEY or use the api-key parameter inside the CLI"
)

if not api_secret:
raise ValueError(
"No api secret provided, set LIVEKIT_API_SECRET or use the api-secret parameter inside the CLI"
)

self._set_url(ws_url)

self._loop = event_loop or asyncio.get_event_loop()
self._lock = asyncio.Lock()
self._request_handler = request_handler
self._wid = "W-" + str(uuid.uuid4())[:12]
self._worker_type = worker_type
self._api_key = api_key
self._api_secret = api_secret
self._api_key = api_key or os.environ.get("LIVEKIT_API_KEY")
self._api_secret = api_secret or os.environ.get("LIVEKIT_API_SECRET")
self._api = None
self._running = False
self._running_jobs: list["JobContext"] = []
self._pending_jobs: Dict[str, asyncio.Future[proto_agent.JobAssignment]] = {}
self._api = api.LiveKitAPI(ws_url, api_key, api_secret)
self._rtc_url = None
self._agent_url = None
ws_url = ws_url or os.environ.get("LIVEKIT_URL")
if ws_url:
self._set_url(ws_url)

def _set_url(self, ws_url: str) -> None:
parse_res = urlparse(ws_url)
Expand All @@ -124,6 +113,17 @@ def _set_url(self, ws_url: str) -> None:
self._rtc_url = url

async def _connect(self) -> protocol.agent.RegisterWorkerResponse:
if not self._rtc_url:
raise ValueError("No WebSocket URL provided, set LIVEKIT_URL env var")

if not self._api_key:
raise ValueError("No API key provided, set LIVEKIT_API_KEY env var")

if not self._api_secret:
raise ValueError("No API secret provided, set LIVEKIT_API_SECRET env var")

self._api = api.LiveKitAPI(self._rtc_url, self._api_key, self._api_secret)

join_jwt = (
api.AccessToken(self._api_key, self._api_secret)
.with_grants(api.VideoGrants(agent=True))
Expand Down Expand Up @@ -280,6 +280,7 @@ async def _shutdown(self) -> None:

async def start(self) -> None:
"""Start the Worker"""

async with self._lock:
if self._running:
raise Exception("worker is already running")
Expand Down Expand Up @@ -310,7 +311,7 @@ def running(self) -> bool:
return self._running

@property
def api(self) -> api.LiveKitAPI:
def api(self) -> api.LiveKitAPI | None:
return self._api


Expand Down Expand Up @@ -390,11 +391,23 @@ def run_app(worker: Worker) -> None:
)
@click.option(
"--url",
help="The websocket URL",
default=worker._rtc_url,
required=True,
envvar="LIVEKIT_URL",
help="LiveKit server or Cloud project WebSocket URL",
default="ws://localhost:7880",
)
@click.option(
"--api-key",
envvar="LIVEKIT_API_KEY",
help="LiveKit server or Cloud project's API key",
required=True,
)
@click.option(
"--api-secret",
envvar="LIVEKIT_API_SECRET",
help="LiveKit server or Cloud project's API secret",
required=True,
)
@click.option("--api-key", help="The API key", default=worker._api_key)
@click.option("--api-secret", help="The API secret", default=worker._api_secret)
def cli(log_level: str, url: str, api_key: str, api_secret: str) -> None:
logging.basicConfig(level=log_level)
worker._set_url(url)
Expand Down

0 comments on commit 27ea3c1

Please sign in to comment.