Repository: livekit/agents
File: livekit-agents/livekit/agents/cli/cli.py (Python)

Code before:
    ) -> None:
        c = AgentsConsole.get_instance()

        if not url:
            raise ValueError("LiveKit URL must be set")

        async def connect_task() -> None:
            nonlocal room, participant_identity
            if participant_identity is None:
                participant_identity = shortuuid("agent-")
            livekit_token = (
                api.AccessToken(
                    api_key=api_key,
                    api_secret=api_secret,
                )
                .with_kind("agent")
                .with_identity(participant_identity)
                .with_grants(api.VideoGrants(room_join=True, room=room, agent=True))
                .to_jwt()
            )

            room_instance = rtc.Room()

            await room_instance.connect(url, livekit_token)

            await server.run()

        try:
            loop = asyncio.get_event_loop()
            asyncio.set_event_loop(loop)

            loop.run_until_complete(connect_task())
        except _ExitCli:
            raise typer.Exit() from None
        except KeyboardInterrupt:
            logger.warning("exiting forcefully")
            os._exit(1)
        except CLIError as e:
            c.print(" ")
            c.print(f"[error]{e}")
            c.print(" ")
            raise typer.Exit(code=1) from None

    @app.command()
    def download_files() -> None:
        c = AgentsConsole.get_instance()
        c.enabled = True

        _configure_logger(c, logging.DEBUG)

        try:

Review comment (suggestion):
this still registered the worker to the cloud, and after connect to a room the participant joined but the agent actually didn't start.

I think something like this is still needed, like the console mode
```python
            @self._server.once("worker_started")
            def _simulate_job() -> None:
                asyncio.run_coroutine_threadsafe(
                    self._server.simulate_job(
                        "console-room", agent_identity="console", fake_job=True
                    ),
                    self._loop,
                )

            await self._server.run(devmode=True, unregistered=True)
```

Code after:
    ) -> None:
        if participant_identity is None:
            participant_identity = shortuuid("agent-")

        c = AgentsConsole.get_instance()
        _configure_logger(c, log_level.value)

        loop = asyncio.get_event_loop()
        _task: asyncio.Task | None = None

        @server.once("worker_started")
        def _simulate_job() -> None:
            nonlocal _task

            async def simulate_job() -> None:
                async with api.LiveKitAPI(url, api_key, api_secret) as lk_api:
                    room_request = api.ListRoomsRequest(names=[room])
                    active_room = await lk_api.room.list_rooms(room_request)

                    if not active_room.rooms:
                        room_info = await lk_api.room.create_room(api.CreateRoomRequest(name=room))
                    else:
                        room_info = active_room.rooms[0]

                await server.simulate_job(
                    room=room,
                    fake_job=False,
                    room_info=room_info,
                    agent_identity=participant_identity,
                )

            _task = asyncio.create_task(simulate_job())

        try:
            loop.run_until_complete(server.run(devmode=True, unregistered=True))
        except _ExitCli:
            raise typer.Exit() from None
        except KeyboardInterrupt:
            logger.warning("exiting forcefully")
            os._exit(1)
        except CLIError as e:
            c.print(" ")
            c.print(f"[error]{e}")
            c.print(" ")
            raise typer.Exit(code=1) from None

    @app.command()
    def download_files() -> None:
        c = AgentsConsole.get_instance()
        c.enabled = True