156 lines
6.2 KiB
Python
156 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the HEL-879 timing-fix probe for the .12 agent venv (real SDK).
|
|
|
|
Reproduces the manager's two worker-side findings from the contract-approval
|
|
round (sdk-say / sdk-extra payloads), against WHATEVER sources are in the tree
|
|
when run — same script is used before AND after the fix:
|
|
|
|
1. speak_proactive_row on the REAL SDK shape: AgentSession.say is a SYNC
|
|
method returning an awaitable SpeechHandle (awaiting it awaits the whole
|
|
playout), so proactive_start must be published while the handle is still
|
|
playing (before done), with end/cancel on the handle's own done callback.
|
|
2. _after_playout through the ACTUAL bind_voice_session callbacks on a real
|
|
AgentSession: a clearly-attributed stream with text but completed=False
|
|
and receipt=None must NOT produce assistant_final — contract v2 says no
|
|
complete same-turn receipt means cancel, never final. The complete+receipt
|
|
path must still produce final AND the ack (checked in the same probe).
|
|
|
|
Embeds repo voice-module sources in a self-contained stdin script; no files
|
|
written on .12, no network, no service touched.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
MODULES = ["chat_api_client", "receipt", "seam", "llm_chat_api",
|
|
"stt_funasr", "tts_cosyvoice", "worker"]
|
|
|
|
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)
|
|
|
|
from livekit.agents import AgentSession
|
|
from livekit.agents.voice.speech_handle import SpeechHandle
|
|
from livekit.agents.voice.events import SpeechCreatedEvent
|
|
|
|
w = voice.worker
|
|
out = {}
|
|
|
|
# --- 1. proactive say timing on the REAL SpeechHandle shape -------------------
|
|
async def proactive_case():
|
|
events = []
|
|
h = SpeechHandle.create()
|
|
class Say:
|
|
def say(self, *args, **kwargs): # real SDK: sync, returns SpeechHandle
|
|
asyncio.get_running_loop().call_later(.06, h._mark_done)
|
|
return h
|
|
task = asyncio.create_task(w.speak_proactive_row(
|
|
Say(), voice.seam.SubtitleBus(events.append), {"id": 7, "text": "offline sample"}))
|
|
await asyncio.sleep(.015)
|
|
out["proactive_before_playout_finishes"] = {
|
|
"handle_done": h.done(),
|
|
"events": [x["kind"] for x in events]}
|
|
await task
|
|
await asyncio.sleep(.015)
|
|
out["proactive_after_playout_finishes"] = [x["kind"] for x in events]
|
|
out["proactive_payload_fields"] = {k: events[0][k] for k in ("v", "kind", "role", "text", "speech_id", "message_id")}
|
|
|
|
asyncio.run(proactive_case())
|
|
|
|
# --- 2. actual-worker no-receipt/incomplete stream ---------------------------
|
|
async def no_receipt_case():
|
|
events = []
|
|
session = AgentSession()
|
|
ledger = voice.receipt.TurnLedger()
|
|
bus = voice.seam.SubtitleBus(events.append)
|
|
interim = voice.seam.AssistantInterimPublisher(bus)
|
|
async def epoch(): return 1
|
|
confirmed = []
|
|
class Receipts:
|
|
async def confirm(self, *args):
|
|
confirmed.append(args[0]); return True
|
|
llm = types.SimpleNamespace(client=types.SimpleNamespace(current_epoch=epoch))
|
|
w.bind_voice_session(session, llm=llm, ledger=ledger, interim=interim,
|
|
bus=bus, stt=types.SimpleNamespace(), receipts=Receipts())
|
|
h = SpeechHandle.create()
|
|
session.emit("speech_created", SpeechCreatedEvent(
|
|
user_initiated=True, source="generate_reply", speech_handle=h, created_at=1.0))
|
|
stream = types.SimpleNamespace(
|
|
speech_id=h.id, error_phi=None, text="partial without receipt",
|
|
turn_id="incomplete", receipt=None, completed=False)
|
|
ledger.note_stream(stream, h)
|
|
interim.begin(h.id)
|
|
h._mark_done()
|
|
await asyncio.sleep(.06)
|
|
out["no_receipt_incomplete_actual_worker_events"] = [x["kind"] for x in events]
|
|
out["no_receipt_incomplete_confirmed"] = list(confirmed)
|
|
|
|
asyncio.run(no_receipt_case())
|
|
|
|
# --- 3. the complete+receipt path must keep final AND ack --------------------
|
|
async def complete_case():
|
|
events = []
|
|
session = AgentSession()
|
|
ledger = voice.receipt.TurnLedger()
|
|
bus = voice.seam.SubtitleBus(events.append)
|
|
interim = voice.seam.AssistantInterimPublisher(bus)
|
|
async def epoch(): return 1
|
|
confirmed = []
|
|
class Receipts:
|
|
async def confirm(self, *args):
|
|
confirmed.append(args[0]); return True
|
|
llm = types.SimpleNamespace(client=types.SimpleNamespace(current_epoch=epoch))
|
|
w.bind_voice_session(session, llm=llm, ledger=ledger, interim=interim,
|
|
bus=bus, stt=types.SimpleNamespace(), receipts=Receipts())
|
|
h = SpeechHandle.create()
|
|
session.emit("speech_created", SpeechCreatedEvent(
|
|
user_initiated=True, source="generate_reply", speech_handle=h, created_at=1.0))
|
|
receipt = {"message_id": 9, "turn_id": "ok-turn", "epoch": 1,
|
|
"token": "t" * 48, "text_hash": "z" * 64}
|
|
stream = types.SimpleNamespace(
|
|
speech_id=h.id, error_phi=None, text="her complete reply",
|
|
turn_id="ok-turn", receipt=receipt, completed=True)
|
|
ledger.note_stream(stream, h)
|
|
interim.begin(h.id)
|
|
h._mark_done()
|
|
await asyncio.sleep(.06)
|
|
out["complete_with_receipt_actual_worker_events"] = [x["kind"] for x in events]
|
|
out["complete_with_receipt_confirmed"] = list(confirmed)
|
|
|
|
asyncio.run(complete_case())
|
|
|
|
print(json.dumps(out, ensure_ascii=False))
|
|
'''
|
|
|
|
def main() -> int:
|
|
sources = {}
|
|
for name in MODULES:
|
|
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/_hel879_timing_probe_payload.py"
|
|
out.write_text(script, encoding="utf-8")
|
|
print(out)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|