LoveLogicAI commited on
Commit
bb18e70
·
verified ·
1 Parent(s): 16d3355
Files changed (1) hide show
  1. tests/debug_join.py +129 -0
tests/debug_join.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Debug the join->ready hang: server up? client connect OK? bot crash visible?
2
+
3
+ Runs the three join-path stages in the foreground and prints everything.
4
+ """
5
+
6
+ import asyncio
7
+ import json
8
+ import os
9
+ import socket
10
+ import subprocess
11
+ import sys
12
+ import tarfile
13
+ import time
14
+ import urllib.request
15
+ from pathlib import Path
16
+
17
+ ROOT = Path("/tmp/freestream")
18
+ REPO = "https://huggingface.co/datasets/LoveLogicAI/freestream/resolve/main"
19
+ VOICE_URL = ("https://huggingface.co/rhasspy/piper-voices/resolve/main/"
20
+ "en/en_US/amy/medium/en_US-amy-medium{ext}")
21
+ GH_LATEST = "https://api.github.com/repos/livekit/livekit-server/releases/latest"
22
+
23
+ os.environ["HF_HOME"] = "/tmp/hf"
24
+ os.environ["FREESTREAM_VOICE_DIR"] = str(ROOT / "voices")
25
+ os.environ["FREESTREAM_WHISPER_MODEL"] = "base"
26
+ os.environ["PYTHONPATH"] = str(ROOT)
27
+
28
+
29
+ def fetch(url, dest):
30
+ dest.parent.mkdir(parents=True, exist_ok=True)
31
+ with urllib.request.urlopen(url, timeout=120) as r:
32
+ data = r.read()
33
+ dest.write_bytes(data)
34
+ return data
35
+
36
+
37
+ FILES = ["scripts/freestream.py",
38
+ "freestream/__init__.py", "freestream/events.py", "freestream/presence.py",
39
+ "freestream/persona.py", "freestream/bot.py",
40
+ "freestream/asr.py", "freestream/avatar.py", "freestream/agent.py"]
41
+ for f in FILES:
42
+ fetch(f"{REPO}/{f}", ROOT / f)
43
+ print("[debug] code fetched", flush=True)
44
+
45
+ if not (ROOT / "livekit-server").exists():
46
+ req = urllib.request.Request(GH_LATEST, headers={"Accept": "application/vnd.github+json"})
47
+ with urllib.request.urlopen(req, timeout=60) as r:
48
+ release = json.loads(r.read())
49
+ asset = next(a for a in release["assets"]
50
+ if "linux_amd64" in a["name"] and a["name"].endswith(".tar.gz"))
51
+ fetch(asset["browser_download_url"], ROOT / asset["name"])
52
+ with tarfile.open(ROOT / asset["name"]) as tf:
53
+ tf.extract("livekit-server", str(ROOT))
54
+ config = ROOT / "lk.yaml"
55
+ config.write_text("""
56
+ port: 7880
57
+ rtc:
58
+ use_external_ip: false
59
+ use_ice_lite: true
60
+ tcp_port: 7881
61
+ port_range_start: 50000
62
+ port_range_end: 50100
63
+ keys:
64
+ devkey: secret
65
+ """)
66
+ server_log = open(ROOT / "server.log", "w")
67
+ server = subprocess.Popen([str(ROOT / "livekit-server"), "--config", str(config)],
68
+ stdout=server_log, stderr=subprocess.STDOUT)
69
+ for _ in range(60):
70
+ try:
71
+ with socket.create_connection(("127.0.0.1", 7880), timeout=1):
72
+ break
73
+ except OSError:
74
+ time.sleep(0.5)
75
+ print("[debug] server up", flush=True)
76
+
77
+ for ext, name in ((".onnx", "en_US-amy-medium.onnx"), (".onnx.json", "en_US-amy-medium.onnx.json")):
78
+ dest = ROOT / "voices" / name
79
+ if not dest.exists():
80
+ fetch(VOICE_URL.format(ext=ext), dest)
81
+
82
+ from livekit import api, rtc # noqa: E402
83
+
84
+ # stage 1: can a plain client connect?
85
+ token = (api.AccessToken("devkey", "secret").with_identity("debugger")
86
+ .with_grants(api.VideoGrants(room_join=True, room="qa-room")).to_jwt())
87
+
88
+
89
+ async def plain_connect():
90
+ room = rtc.Room()
91
+ t0 = time.perf_counter()
92
+ await asyncio.wait_for(room.connect("ws://localhost:7880", token), timeout=30)
93
+ print(f"[debug] stage1 plain client connect OK in {time.perf_counter()-t0:.2f}s",
94
+ flush=True)
95
+ await room.disconnect()
96
+
97
+
98
+ asyncio.run(plain_connect())
99
+
100
+ # stage 2: run the bot directly in the foreground for 90s and show its output
101
+ sdir = ROOT / "dbg-session"
102
+ sdir.mkdir(exist_ok=True)
103
+ bot_token = (api.AccessToken("devkey", "secret").with_identity("freestream-bot-dbg")
104
+ .with_name("Bot").with_grants(api.VideoGrants(room_join=True, room="qa-room",
105
+ can_publish=True,
106
+ can_subscribe=True)).to_jwt())
107
+ env = dict(os.environ)
108
+ env["PYTHONPATH"] = str(ROOT)
109
+ print("[debug] stage2 launching bot in foreground (90s cap)...", flush=True)
110
+ try:
111
+ r = subprocess.run(
112
+ [sys.executable, "-m", "freestream.bot",
113
+ "--url", "ws://localhost:7880", "--token", bot_token, "--room", "qa-room",
114
+ "--bot-name", "DebugBot", "--image", "identity/x.png",
115
+ "--voice", "piper-en_US-amy-medium",
116
+ "--session-id", "dbg001", "--session-dir", str(sdir)],
117
+ cwd=str(ROOT), capture_output=True, text=True, timeout=90, env=env)
118
+ print("[debug] bot exit", r.returncode)
119
+ print("[debug] bot stdout:\n" + r.stdout[-3000:])
120
+ print("[debug] bot stderr:\n" + r.stderr[-3000:])
121
+ except subprocess.TimeoutExpired as e:
122
+ print("[debug] bot ran the full 90s (no crash). stdout:\n"
123
+ + (e.stdout or b"").decode()[-3000:]
124
+ + "\nstderr:\n" + (e.stderr or b"").decode()[-3000:], flush=True)
125
+
126
+ state = (sdir / "state.json")
127
+ print("[debug] state.json:", state.read_text() if state.exists() else "MISSING", flush=True)
128
+ server.terminate()
129
+ print("[debug] done", flush=True)