forked from kaushikchaturvedula/FORGE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.py
More file actions
313 lines (237 loc) · 9.27 KB
/
Copy pathevents.py
File metadata and controls
313 lines (237 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Qwen-Omni-Realtime event (de)serialization — the one place protocol drift lives.
The realtime API is OpenAI-Realtime-compatible: client events like ``session.update``,
``input_audio_buffer.append``, ``input_image_buffer.append``, ``response.create`` and
``conversation.item.create``; server events like ``response.audio.delta``,
``response.audio_transcript.delta``, ``response.function_call_arguments.delta/.done``,
and the VAD ``input_audio_buffer.speech_started/stopped``.
The English docs lag the Chinese docs on a few names, so parsing is tolerant: we map
known event ``type`` strings to small internal dataclasses and surface anything else as
``UnknownEvent`` (carrying the raw payload) rather than crashing. If a name turns out
different at the first live run, it is corrected here and nowhere else.
This module is pure (no I/O) and fully unit-testable without a session.
"""
from __future__ import annotations
import base64
import json
from dataclasses import dataclass, field
from typing import Any
# ── Internal server-event dataclasses (Qwen -> FORGE) ────────────────────────
@dataclass
class SessionCreated:
session_id: str | None = None
@dataclass
class SessionUpdated:
session: dict[str, Any] = field(default_factory=dict) # the server's accepted config
@dataclass
class SpeechStarted:
"""Server VAD detected the user started speaking — drives barge-in."""
@dataclass
class SpeechStopped:
pass
@dataclass
class InputAudioCommitted:
"""The server committed/emptied the input audio buffer after a turn."""
@dataclass
class InputTranscriptDelta:
"""Partial transcription of the technician's speech."""
text: str
@dataclass
class InputTranscriptDone:
text: str
@dataclass
class AudioDelta:
"""A chunk of synthesized output audio (PCM16 @ 24 kHz), raw bytes."""
audio: bytes
@dataclass
class OutputTranscriptDelta:
"""Partial transcription of FORGE's own spoken reply."""
text: str
@dataclass
class OutputTranscriptDone:
text: str
@dataclass
class FunctionCallArgumentsDelta:
call_id: str
name: str
delta: str
@dataclass
class FunctionCallDone:
call_id: str
name: str
arguments: dict[str, Any]
@dataclass
class ResponseCreated:
response_id: str | None = None
@dataclass
class ResponseDone:
response_id: str | None = None
@dataclass
class ResponseAudioDone:
"""The model finished emitting AUDIO for a response (may precede the text done)."""
@dataclass
class RealtimeError:
message: str
code: str | None = None
@dataclass
class UnknownEvent:
type: str
raw: dict[str, Any] = field(default_factory=dict)
ServerEvent = (
SessionCreated
| SessionUpdated
| SpeechStarted
| SpeechStopped
| InputAudioCommitted
| InputTranscriptDelta
| InputTranscriptDone
| AudioDelta
| OutputTranscriptDelta
| OutputTranscriptDone
| FunctionCallArgumentsDelta
| FunctionCallDone
| ResponseCreated
| ResponseDone
| ResponseAudioDone
| RealtimeError
| UnknownEvent
)
def parse_server_event(raw: dict[str, Any]) -> ServerEvent:
"""Map a raw Qwen realtime event dict to an internal dataclass."""
etype = raw.get("type", "")
if etype == "session.created":
return SessionCreated(session_id=(raw.get("session") or {}).get("id"))
if etype == "session.updated":
return SessionUpdated(session=raw.get("session", {}) or {})
if etype == "input_audio_buffer.speech_started":
return SpeechStarted()
if etype == "input_audio_buffer.speech_stopped":
return SpeechStopped()
if etype == "input_audio_buffer.committed":
return InputAudioCommitted()
# User speech transcription (turn-detection / transcription events).
if etype == "conversation.item.input_audio_transcription.delta":
return InputTranscriptDelta(text=raw.get("delta", ""))
if etype == "conversation.item.input_audio_transcription.completed":
return InputTranscriptDone(text=raw.get("transcript", ""))
# Assistant audio + its transcript.
if etype in ("response.audio.delta", "response.output_audio.delta"):
b64 = raw.get("delta", "")
try:
audio = base64.b64decode(b64) if b64 else b""
except (ValueError, TypeError):
audio = b""
return AudioDelta(audio=audio)
if etype in (
"response.audio_transcript.delta",
"response.output_audio_transcript.delta",
):
return OutputTranscriptDelta(text=raw.get("delta", ""))
if etype in (
"response.audio_transcript.done",
"response.output_audio_transcript.done",
):
return OutputTranscriptDone(text=raw.get("transcript", ""))
# Function calling.
if etype == "response.function_call_arguments.delta":
return FunctionCallArgumentsDelta(
call_id=raw.get("call_id", ""),
name=raw.get("name", ""),
delta=raw.get("delta", ""),
)
if etype == "response.function_call_arguments.done":
return FunctionCallDone(
call_id=raw.get("call_id", ""),
name=raw.get("name", ""),
arguments=_safe_json(raw.get("arguments", "")),
)
if etype == "response.created":
return ResponseCreated(response_id=(raw.get("response") or {}).get("id"))
if etype == "response.done":
return ResponseDone(response_id=(raw.get("response") or {}).get("id"))
if etype in ("response.audio.done", "response.output_audio.done"):
return ResponseAudioDone()
if etype == "error":
err = raw.get("error", raw)
return RealtimeError(message=err.get("message", "unknown error"), code=err.get("code"))
return UnknownEvent(type=etype, raw=raw)
def _safe_json(text: str) -> dict[str, Any]:
if not text:
return {}
try:
parsed = json.loads(text)
return parsed if isinstance(parsed, dict) else {"value": parsed}
except (ValueError, TypeError):
return {}
# ── Client-event builders (FORGE -> Qwen) ────────────────────────────────────
def session_update(
*,
instructions: str,
tools: list[dict[str, Any]],
voice: str,
vad_type: str = "server_vad",
enable_vad: bool = True,
tools_format: str = "flat",
tool_choice: str = "",
) -> dict[str, Any]:
"""Build a session.update — sent once at session open to configure the flat
session (full instructions + the whole grounded tool catalog).
Field values match the live DashScope realtime spec: audio format is "pcm"
(input 16 kHz, output 24 kHz, mono 16-bit), and turn_detection is a server-VAD
object with threshold + silence_duration_ms (server VAD auto-creates the response
on end-of-speech). ``tools_format`` selects flat (OpenAI-Realtime) vs nested."""
turn_detection = (
{"type": vad_type, "threshold": 0.5, "silence_duration_ms": 800}
if enable_vad
else None
)
session: dict[str, Any] = {
"modalities": ["text", "audio"],
"voice": voice,
"instructions": instructions,
"input_audio_format": "pcm",
"output_audio_format": "pcm",
"input_audio_transcription": {"model": "gummy-realtime-v1"},
"turn_detection": turn_detection,
}
# Only advertise tools when present — avoids sending an empty/odd tools field.
formatted = [_format_tool(t, tools_format) for t in tools]
if formatted:
session["tools"] = formatted
if tool_choice: # only send when explicitly configured (unsupported field can break registration)
session["tool_choice"] = tool_choice
return {"type": "session.update", "session": session}
def _format_tool(tool: dict[str, Any], fmt: str) -> dict[str, Any]:
"""Emit a tool entry in the requested shape. Input schemas are nested
({type:function, function:{name,description,parameters}})."""
fn = tool.get("function", tool) if tool.get("type") == "function" else tool
name, desc, params = fn.get("name"), fn.get("description"), fn.get("parameters")
if fmt == "nested":
return {"type": "function", "function": {"name": name, "description": desc, "parameters": params}}
return {"type": "function", "name": name, "description": desc, "parameters": params}
def input_audio_append(pcm: bytes) -> dict[str, Any]:
return {
"type": "input_audio_buffer.append",
"audio": base64.b64encode(pcm).decode("ascii"),
}
def input_image_append(jpeg: bytes) -> dict[str, Any]:
return {
"type": "input_image_buffer.append",
"image": base64.b64encode(jpeg).decode("ascii"),
}
def input_audio_commit() -> dict[str, Any]:
return {"type": "input_audio_buffer.commit"}
def response_create() -> dict[str, Any]:
return {"type": "response.create"}
def response_cancel() -> dict[str, Any]:
return {"type": "response.cancel"}
def function_call_output(call_id: str, output: Any) -> dict[str, Any]:
"""Return a tool result to the model, then it continues the spoken turn."""
text = output if isinstance(output, str) else json.dumps(output)
return {
"type": "conversation.item.create",
"item": {
"type": "function_call_output",
"call_id": call_id,
"output": text,
},
}