|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test script to verify typed chat messages reach the LiveKit agent.""" |
| 3 | + |
| 4 | +import asyncio |
| 5 | +import json |
| 6 | +import requests |
| 7 | +import urllib3 |
| 8 | + |
| 9 | +# Suppress InsecureRequestWarning for self-signed cert |
| 10 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 11 | + |
| 12 | +from livekit import rtc |
| 13 | + |
| 14 | +SPACE_ID = "c2e34ba8-8819-489a-b9b0-2e604a33d89b" |
| 15 | +HUB_URL = "https://127.0.0.1:8084" |
| 16 | + |
| 17 | + |
| 18 | +async def test_chat(): |
| 19 | + # Get token from Hub API |
| 20 | + print("Getting token from Hub API...") |
| 21 | + resp = requests.post( |
| 22 | + f"{HUB_URL}/api/livekit/token", |
| 23 | + json={"space_id": SPACE_ID, "identity": "test-script-user"}, |
| 24 | + verify=False, |
| 25 | + timeout=10, |
| 26 | + ) |
| 27 | + print(f"Token response: {resp.status_code}") |
| 28 | + if resp.status_code != 200: |
| 29 | + print(resp.text) |
| 30 | + return |
| 31 | + |
| 32 | + data = resp.json() |
| 33 | + token = data["token"] |
| 34 | + url = data["url"] |
| 35 | + room_name = data["room"] |
| 36 | + |
| 37 | + print(f"Got token for room: {room_name}") |
| 38 | + print(f"LiveKit URL: {url}") |
| 39 | + |
| 40 | + room = rtc.Room() |
| 41 | + |
| 42 | + @room.on("data_received") |
| 43 | + def on_data(pkt: rtc.DataPacket): |
| 44 | + try: |
| 45 | + msg = json.loads(pkt.data.decode()) |
| 46 | + print(f"[DATA] Received: {msg}") |
| 47 | + except Exception: |
| 48 | + print(f"[DATA] Raw: {pkt.data}") |
| 49 | + |
| 50 | + @room.on("participant_connected") |
| 51 | + def on_participant(participant: rtc.RemoteParticipant): |
| 52 | + print(f"[ROOM] Participant joined: {participant.identity}") |
| 53 | + |
| 54 | + print("Connecting to room...") |
| 55 | + await room.connect(url, token) |
| 56 | + print(f"Connected! State: {room.connection_state}") |
| 57 | + |
| 58 | + # List participants |
| 59 | + print(f"Remote participants: {[p.identity for p in room.remote_participants.values()]}") |
| 60 | + |
| 61 | + # Wait for agent to join |
| 62 | + print("Waiting 3s for agent to join...") |
| 63 | + await asyncio.sleep(3) |
| 64 | + |
| 65 | + print(f"Remote participants now: {[p.identity for p in room.remote_participants.values()]}") |
| 66 | + |
| 67 | + # Send a chat message |
| 68 | + msg = {"type": "chat", "text": "Hello from test script! What is 2+2?", "ts": 1234567890} |
| 69 | + payload = json.dumps(msg).encode() |
| 70 | + print(f"Sending chat message: {msg['text']}") |
| 71 | + await room.local_participant.publish_data(payload, reliable=True) |
| 72 | + |
| 73 | + # Wait for agent to process and respond |
| 74 | + print("Waiting 8s for agent response...") |
| 75 | + await asyncio.sleep(8) |
| 76 | + |
| 77 | + await room.disconnect() |
| 78 | + print("Disconnected. Check .marvain-agent.log for 'Received typed chat'") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + asyncio.run(test_chat()) |
| 83 | + |
0 commit comments