288 lines
13 KiB
Python
288 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the HEL-877/879 real-SDK compatibility check for the .12 agent venv.
|
|
|
|
Embeds the repo voice-module sources in a self-contained stdin script (no files
|
|
written to .12, no network, no service touched) and runs it with
|
|
/opt/voice-agent/agent/venv/bin/python (livekit-agents 1.8.8, livekit 1.1.20).
|
|
|
|
HEL-879 additions: the check drives the WORKER'S ACTUAL callback path with real
|
|
SpeechHandle/SpeechCreatedEvent objects — the handle-without-generation and
|
|
late-cleanup interleavings from the manager's independent review — and verifies
|
|
the assistant_interim delta seam through the real LLMStream machinery.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MODULES = ["chat_api_client", "receipt", "seam", "llm_chat_api"]
|
|
|
|
TEMPLATE = r'''
|
|
import asyncio, base64, json, sys, types
|
|
|
|
SOURCES = json.loads(base64.b64decode("__B64__"))
|
|
|
|
pkg = types.ModuleType("cybergril"); pkg.__path__ = []
|
|
voice = types.ModuleType("cybergril.voice"); voice.__path__ = []
|
|
sys.modules["cybergril"] = pkg
|
|
sys.modules["cybergril.voice"] = voice
|
|
for name, src in SOURCES.items():
|
|
mod = types.ModuleType(f"cybergril.voice.{name}")
|
|
mod.__package__ = "cybergril.voice"
|
|
sys.modules[f"cybergril.voice.{name}"] = mod
|
|
exec(compile(src, f"<cybergril.voice.{name}>", "exec"), mod.__dict__)
|
|
setattr(voice, name, mod)
|
|
|
|
import httpx
|
|
from livekit.agents import llm as lk_llm
|
|
from livekit.agents.llm import LLM, LLMStream, ChatChunk, ChoiceDelta
|
|
|
|
ok = []
|
|
|
|
# 1. real-SDK subclass contracts
|
|
llm_mod = sys.modules["cybergril.voice.llm_chat_api"]
|
|
assert issubclass(llm_mod.CyberGrillLLM, LLM), "CyberGrillLLM must subclass the real llm.LLM"
|
|
assert issubclass(llm_mod.ChatApiLLMStream, LLMStream)
|
|
ok.append("CyberGrillLLM/ChatApiLLMStream subclass real livekit-agents 1.8.8 LLM/LLMStream")
|
|
|
|
# 2. real ChatContext -> latest_user_text
|
|
ctx = lk_llm.ChatContext()
|
|
ctx.add_message(role="assistant", content="早些的一句")
|
|
ctx.add_message(role="user", content="真实SDK链路你好")
|
|
assert llm_mod.latest_user_text(ctx) == "真实SDK链路你好"
|
|
ok.append("latest_user_text reads a real ChatContext (items/role/text_content)")
|
|
|
|
# 3. full turn through the real LLMStream machinery with fake SSE
|
|
FINAL = "真实链路上的一句完整回复。"
|
|
sse = (
|
|
'data: {"t":"chat.delta","epoch":1,"turn_id":null,"delta":"真实链路上"}\n\n'
|
|
'data: {"t":"chat.delta","epoch":1,"turn_id":null,"delta":"的一句完整回复。"}\n\n'
|
|
'data: {"t":"chat.final","epoch":1,"turn_id":null,"utterance":{"text":"' + FINAL + '"},'
|
|
'"receipt":{"message_id":9,"turn_id":null,"epoch":1,"token":"' + "k" * 48 + '","text_hash":"' + "h" * 64 + '"}}\n\n'
|
|
).encode()
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
if request.url.path == "/api/health":
|
|
return httpx.Response(200, json={"epoch": 1})
|
|
return httpx.Response(200, headers={"content-type": "text/event-stream"}, content=sse)
|
|
|
|
receipt_ok = {}
|
|
|
|
async def round_trip():
|
|
client = llm_mod.ChatApiClient("http://x", transport=httpx.MockTransport(handler))
|
|
adapter = llm_mod.CyberGrillLLM("http://x", client=client)
|
|
stream = adapter.chat(chat_ctx=ctx)
|
|
chunks = []
|
|
async for chunk in stream:
|
|
assert isinstance(chunk, ChatChunk) and chunk.delta and chunk.delta.content
|
|
chunks.append(chunk.delta.content)
|
|
assert "".join(chunks) == FINAL, chunks
|
|
assert stream.completed and stream.receipt and stream.receipt["message_id"] == 9
|
|
receipt_ok["text"] = stream.text
|
|
receipt_ok["receipt"] = stream.receipt
|
|
|
|
asyncio.run(round_trip())
|
|
assert receipt_ok["text"] == FINAL
|
|
ok.append("fake-SSE turn through real LLMStream emits ChatChunks, final text and receipt")
|
|
|
|
# 4. phi error turn: spoken verbatim, no receipt
|
|
phi_sse = (
|
|
'data: {"t":"chat.error","epoch":1,"turn_id":null,"phi":"她这会儿没接上话,稍后再试试。你写的话还在。","retryable":true}\n\n'
|
|
).encode()
|
|
|
|
def phi_handler(request: httpx.Request) -> httpx.Response:
|
|
if request.url.path == "/api/health":
|
|
return httpx.Response(200, json={"epoch": 1})
|
|
return httpx.Response(200, headers={"content-type": "text/event-stream"}, content=phi_sse)
|
|
|
|
async def phi_turn():
|
|
adapter = llm_mod.CyberGrillLLM("http://x",
|
|
client=llm_mod.ChatApiClient("http://x", transport=httpx.MockTransport(phi_handler)))
|
|
stream = adapter.chat(chat_ctx=ctx)
|
|
spoken = []
|
|
async for chunk in stream:
|
|
spoken.append(chunk.delta.content)
|
|
assert "".join(spoken) == "她这会儿没接上话,稍后再试试。你写的话还在。"
|
|
assert stream.error_phi and not stream.completed and stream.receipt is None
|
|
|
|
asyncio.run(phi_turn())
|
|
ok.append("chat.error phi is spoken verbatim via ChatChunk and carries no receipt")
|
|
|
|
# 5. token claims parity with the real livekit.api.AccessToken
|
|
from livekit import api
|
|
import jwt as pyjwt
|
|
wire = sys.modules["cybergril.voice.livekit_wire"]
|
|
mine = wire.mint_token("devkey", "s3cret", identity="user-ab", name="用户",
|
|
room="cg-voice-test", room_join=True, ttl_seconds=300, now=1700000000.0)
|
|
sdk_token = (api.AccessToken("devkey", "s3cret").with_identity("user-ab").with_name("用户")
|
|
.with_grants(api.VideoGrants(room_join=True, room="cg-voice-test",
|
|
can_publish=True, can_subscribe=True))
|
|
.to_jwt())
|
|
mine_claims = pyjwt.decode(mine, "s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
|
sdk_claims = pyjwt.decode(sdk_token, "s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
|
assert mine_claims["video"] == sdk_claims["video"], (mine_claims["video"], sdk_claims["video"])
|
|
assert mine_claims["iss"] == sdk_claims["iss"] and mine_claims["sub"] == sdk_claims["sub"]
|
|
assert set(mine_claims) == set(sdk_claims), (sorted(mine_claims), sorted(sdk_claims))
|
|
ok.append("mint_token claims byte-parity with livekit.api.AccessToken (video/iss/sub/keys)")
|
|
|
|
# 6. server token decodes as the SDK Service._auth_header shape
|
|
server_tok = wire.mint_server_token("devkey", "s3cret", ttl_seconds=60, now=1700000000.0)
|
|
sc = pyjwt.decode(server_tok, "s3cret", algorithms=["HS256"], options={"verify_exp": False})
|
|
assert sc["iss"] == "devkey" and sc["sub"] == "" and "video" not in sc
|
|
ok.append("mint_server_token matches SDK no-grant auth header claims")
|
|
|
|
# 7. SpeechHandle/worker API surface used by worker.py exists on the real SDK
|
|
from livekit.agents.voice.speech_handle import SpeechHandle
|
|
for attr in ("wait_for_playout", "interrupted", "exception", "add_done_callback"):
|
|
assert hasattr(SpeechHandle, attr), attr
|
|
from livekit.agents.voice.events import UserInputTranscribedEvent
|
|
assert "is_final" in UserInputTranscribedEvent.model_fields
|
|
ok.append("worker.py SDK surface (SpeechHandle, UserInputTranscribedEvent.is_final) present")
|
|
|
|
# 8. barge-in cancellation closes the SSE response immediately
|
|
state = {"response": None}
|
|
|
|
async def slow_body():
|
|
yield 'data: {"t":"chat.delta","epoch":1,"turn_id":null,"delta":"开头"}\n\n'.encode()
|
|
await asyncio.sleep(30)
|
|
yield 'data: {"t":"chat.final","epoch":1,"turn_id":null}\n\n'.encode()
|
|
|
|
class TrackingTransport(httpx.MockTransport):
|
|
async def handle_async_request(self, request):
|
|
response = await super().handle_async_request(request)
|
|
if request.url.path == "/api/chat":
|
|
state["response"] = response
|
|
return response
|
|
|
|
def slow_handler(request: httpx.Request) -> httpx.Response:
|
|
if request.url.path == "/api/health":
|
|
return httpx.Response(200, json={"epoch": 1})
|
|
return httpx.Response(200, headers={"content-type": "text/event-stream"},
|
|
content=slow_body())
|
|
|
|
async def cancel_case():
|
|
adapter = llm_mod.CyberGrillLLM("http://x",
|
|
client=llm_mod.ChatApiClient("http://x", transport=TrackingTransport(slow_handler)))
|
|
stream = adapter.chat(chat_ctx=ctx)
|
|
task = asyncio.create_task(stream._run())
|
|
await asyncio.sleep(0.2) # first delta consumed
|
|
assert state["response"] is not None and not state["response"].is_closed
|
|
task.cancel()
|
|
try:
|
|
await task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
assert state["response"].is_closed, "adapter must close the HTTP stream on barge-in"
|
|
|
|
asyncio.run(cancel_case())
|
|
ok.append("barge-in task cancel closes the in-flight HTTP stream immediately")
|
|
|
|
# 9. HEL-879: the worker's ACTUAL callback path on real SpeechHandle objects —
|
|
# the manager's two interleavings (handle without generation consuming the
|
|
# next turn's stream; late cleanup) plus interrupt/failure discard order,
|
|
# driven exactly like worker.entrypoint wires them.
|
|
from livekit.agents.voice.speech_handle import SpeechHandle
|
|
from livekit.agents.voice.events import SpeechCreatedEvent
|
|
receipt_mod = sys.modules["cybergril.voice.receipt"]
|
|
seam_mod = sys.modules["cybergril.voice.seam"]
|
|
|
|
async def worker_callback_path():
|
|
ledger = receipt_mod.TurnLedger()
|
|
done_futures = []
|
|
|
|
def on_speech_created(ev): # same guard + wiring as worker._on_speech_created
|
|
if not ev.user_initiated:
|
|
return
|
|
ledger.note_handle(ev.speech_handle, ev.created_at)
|
|
ev.speech_handle.add_done_callback(
|
|
lambda handle=ev.speech_handle: asyncio.get_running_loop().create_task(
|
|
after_playout(handle)
|
|
)
|
|
)
|
|
|
|
async def after_playout(handle): # same order as worker._after_playout
|
|
await handle.wait_for_playout()
|
|
failed = handle.exception() is not None
|
|
if handle.interrupted or failed:
|
|
ledger.discard(handle)
|
|
done_futures.append(("discarded", handle))
|
|
return
|
|
stream = ledger.stream_for(handle)
|
|
done_futures.append((stream, handle))
|
|
|
|
# H1: real handle, its turn is interrupted BEFORE any LLM stream starts.
|
|
h1 = SpeechHandle.create()
|
|
ev1 = SpeechCreatedEvent(user_initiated=True, source="generate_reply",
|
|
speech_handle=h1, created_at=1.0)
|
|
on_speech_created(ev1)
|
|
# H2: the next turn does start a generation (real handle + noted stream).
|
|
h2 = SpeechHandle.create()
|
|
ev2 = SpeechCreatedEvent(user_initiated=True, source="generate_reply",
|
|
speech_handle=h2, created_at=2.0)
|
|
on_speech_created(ev2)
|
|
s2 = types.SimpleNamespace(turn_id="new", receipt={"message_id": 2})
|
|
ledger.note_stream(s2, 2.1)
|
|
# Complete both handles through the SDK's own done-future machinery:
|
|
# H1 was interrupted (public interrupt()) before any generation started;
|
|
# H2 played out cleanly (done, no error).
|
|
h1.interrupt()
|
|
h1._mark_done()
|
|
h2._mark_done()
|
|
await asyncio.sleep(0.05) # let the scheduled done callbacks run
|
|
outcomes = {id(handle): (stream, handle) for stream, handle in done_futures}
|
|
assert outcomes[id(h1)][0] == "discarded", "interrupted handle must discard fail-closed"
|
|
assert outcomes[id(h2)][0] is s2, "the real turn must get its OWN stream"
|
|
# The manager's exact inputs, re-checked through stream_for directly:
|
|
assert ledger.stream_for(h1) is None
|
|
|
|
asyncio.run(worker_callback_path())
|
|
ok.append("worker callback path on real SpeechHandle/SpeechCreatedEvent: no-generation "
|
|
"handle discards, next turn keeps its own stream (explicit binding)")
|
|
|
|
# 10. HEL-879: assistant_interim seam through the REAL LLMStream machinery.
|
|
interim_payloads = []
|
|
async def interim_seam():
|
|
publisher = seam_mod.AssistantInterimPublisher(interim_payloads.append)
|
|
adapter = llm_mod.CyberGrillLLM("http://x", client=llm_mod.ChatApiClient(
|
|
"http://x", transport=httpx.MockTransport(handler)),
|
|
on_assistant_delta=publisher.feed)
|
|
publisher.reset()
|
|
stream = adapter.chat(chat_ctx=ctx)
|
|
async for _ in stream:
|
|
pass
|
|
assert [p["text"] for p in interim_payloads] == ["真实链路上", "真实链路上的一句完整回复。"]
|
|
assert all(p["kind"] == "assistant_interim" and p["role"] == "assistant"
|
|
for p in interim_payloads)
|
|
|
|
asyncio.run(interim_seam())
|
|
ok.append("assistant_interim seam: SSE deltas through real LLMStream feed her "
|
|
"incremental subtitle payloads (replace semantics, reset works)")
|
|
|
|
print("HEL-877/879 real-SDK check on livekit-agents", end=" ")
|
|
import importlib.metadata as md
|
|
print(md.version("livekit-agents") + ":", "ALL PASS")
|
|
for line in ok:
|
|
print(" OK", line)
|
|
'''
|
|
|
|
|
|
def main() -> int:
|
|
sources = {}
|
|
for name in MODULES + ["livekit_wire"]:
|
|
path = ROOT / "src/cybergril/voice" / f"{name}.py"
|
|
sources[name] = path.read_text(encoding="utf-8")
|
|
payload = base64.b64encode(json.dumps(sources).encode()).decode()
|
|
script = TEMPLATE.replace("__B64__", payload)
|
|
out = ROOT / "scripts/_hel877_sdk_check_payload.py"
|
|
out.write_text(script, encoding="utf-8")
|
|
print(out)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import json
|
|
|
|
sys.exit(main())
|