annotate dictation/webrtc_test.py @ 279:b3b547563ec7

Add Google connector service and agent wiki Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code. Co-authored-by: Copilot <[email protected]> Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 22:22:36 -0700
parents 78699f810817
children 49e9e591c9bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
275
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
1 import asyncio
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
2 import json
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
3 import math
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
4 from pathlib import Path
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
5 import tempfile
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
6 import unittest
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
7 import wave
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
8
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
9 from aiortc import RTCPeerConnection, RTCSessionDescription
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
10 from aiortc.contrib.media import MediaPlayer
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
11 import numpy as np
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
12
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
13 from dictation.config import DictationConfig
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
14 from dictation.server import DictationService, Offer
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
15 from dictation.transcriber import Transcript
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
16
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
17
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
18 class FakeTranscriber:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
19 async def warmup(self):
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
20 return None
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
21
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
22 async def transcribe(self, samples, *, final):
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
23 return Transcript(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
24 "final transcript" if final else "partial transcript",
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
25 "en",
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
26 0.99,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
27 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
28
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
29
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
30 def write_test_audio(path: str) -> None:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
31 sample_rate = 16000
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
32 speech = np.array(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
33 [
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
34 int(1600 * math.sin(2 * math.pi * 440 * index / sample_rate))
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
35 for index in range(sample_rate * 2)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
36 ],
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
37 dtype=np.int16,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
38 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
39 silence = np.zeros(sample_rate, dtype=np.int16)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
40 with wave.open(path, "wb") as output:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
41 output.setnchannels(1)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
42 output.setsampwidth(2)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
43 output.setframerate(sample_rate)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
44 output.writeframes(np.concatenate((speech, silence)).tobytes())
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
45
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
46
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
47 def test_config() -> DictationConfig:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
48 return DictationConfig(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
49 host="127.0.0.1",
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
50 port=8090,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
51 model_dir=Path("/tmp/model"),
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
52 compute_type="int8_float16",
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
53 max_sessions=1,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
54 partial_interval_ms=500,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
55 silence_ms=200,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
56 max_utterance_seconds=5,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
57 speech_threshold=0.01,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
58 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
59
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
60
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
61 class WebRtcTest(unittest.IsolatedAsyncioTestCase):
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
62 async def test_audio_track_returns_transcript_events(self):
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
63 service = DictationService(test_config(), FakeTranscriber())
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
64 await service.start()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
65 client = RTCPeerConnection()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
66 channel = client.createDataChannel("transcripts")
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
67 messages = []
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
68 final_received = asyncio.Event()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
69 audio_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
70 audio_file.close()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
71 write_test_audio(audio_file.name)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
72 player = MediaPlayer(audio_file.name)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
73
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
74 @channel.on("message")
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
75 def on_message(message):
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
76 event = json.loads(message)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
77 messages.append(event)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
78 if event["type"] == "transcript.final":
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
79 final_received.set()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
80
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
81 client.addTrack(player.audio)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
82 offer = await client.createOffer()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
83 await client.setLocalDescription(offer)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
84 answer = await service.accept_offer(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
85 Offer(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
86 sdp=client.localDescription.sdp,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
87 type=client.localDescription.type,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
88 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
89 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
90 await client.setRemoteDescription(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
91 RTCSessionDescription(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
92 sdp=answer["sdp"],
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
93 type=answer["type"],
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
94 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
95 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
96
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
97 try:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
98 try:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
99 await asyncio.wait_for(final_received.wait(), timeout=10)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
100 except TimeoutError as error:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
101 server_states = [
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
102 {
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
103 "connection": session.peer.connectionState,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
104 "ice": session.peer.iceConnectionState,
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
105 "channel": (
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
106 session.channel.readyState
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
107 if session.channel is not None
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
108 else None
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
109 ),
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
110 "audioTask": (
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
111 "missing"
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
112 if session.audio_task is None
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
113 else (
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
114 repr(session.audio_task.exception())
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
115 if session.audio_task.done()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
116 and not session.audio_task.cancelled()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
117 else "running"
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
118 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
119 ),
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
120 }
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
121 for session in service.sessions.values()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
122 ]
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
123 self.fail(
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
124 "Timed out waiting for transcript: "
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
125 f"client={client.connectionState}/"
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
126 f"{client.iceConnectionState}, "
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
127 f"server={server_states}, messages={messages}"
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
128 )
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
129 event_types = [message["type"] for message in messages]
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
130 self.assertIn("ready", event_types)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
131 self.assertIn("speech.started", event_types)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
132 self.assertIn("transcript.partial", event_types)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
133 self.assertIn("transcript.final", event_types)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
134 finally:
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
135 await client.close()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
136 await service.close()
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
137 Path(audio_file.name).unlink(missing_ok=True)
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
138 self.assertEqual(service.sessions, {})
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
139
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
140
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
141 if __name__ == "__main__":
78699f810817 Add Qwen3-VL and WebRTC dictation services
MrJuneJune <me@mrjunejune.com>
parents:
diff changeset
142 unittest.main()