[Bugfix] Fix Qwen3-Omni realtime playback interruption and conversation history (#7791)

Signed-off-by: psv666 <2693925048@qq.com>
This commit is contained in:
psv666 authored and GitHub committed 2026-09-20 14:42:17 +00:00
1 parent d3396fc5fb
commit 2d0b03f717
13 files changed
+1292 -68

No files matched your search

+15 -1
View File
@@ -946,6 +946,16 @@ def _project(state: RealtimeProjectionState, event: dict[str, object]) -> list[D
if not isinstance(response_id, str) or not response_id:
return events
if response_is_done(state, response_id):
# Generation may finish long before the client drains its audio.
# A later barge-in still needs to stop that queued playback, but
# must not produce a second response.done for the completed turn.
playback = event.get("playback")
if (
not events
and isinstance(playback, Mapping)
and _int_or(playback.get("sent_ms")) > _int_or(playback.get("committed_ms"))
):
events.append(OutputAudioCleared(response_id=response_id))
return events
committed_ms = event.get("committed_ms")
if isinstance(committed_ms, int | float):
@@ -1320,7 +1330,11 @@ def resolve_cancel_response(state: RealtimeProjectionState, command: CancelRespo
def resolve_clear_output_audio(state: RealtimeProjectionState, command: ClearOutputAudio) -> ResolvedControl:
payload: dict[str, object] = {"type": "output_audio_buffer.clear", "reason": "output_audio_buffer.clear"}
response_id = command.response_id or state.active_response_id or state.last_response_id
if response_is_done(state, response_id):
# A completed response can still own queued playback. Clear the latest
# response in the engine, while keeping late clears away from newer turns.
if response_is_done(state, response_id) and (
state.active_response_id is not None or response_id != state.last_response_id
):
return ResolvedControl(payloads=[], events=[OutputAudioCleared(response_id=_str_or_none(response_id))])
if isinstance(response_id, str) and response_id:
payload["response_id"] = response_id
@@ -1029,6 +1029,16 @@ class DuplexEngineSession:
last_message, audio_text_marks, _ = response_snapshot
self._conversation.pending_item_ids[item_id] = copy.deepcopy(last_message)
self._conversation.pending_item_input_commit_seqs[item_id] = response_snapshot[2]
playback = self._playback_cursor_for_item_id(item_id)
if (
item_id in self._conversation.history_item_placeholders
and playback is not None
and playback.text_requires_complete_audio
):
# Keep the assistant turn between its user inputs even when
# unaligned audio cannot establish any heard text. A later
# full playback ACK fills this same slot from the snapshot.
self._store_history_item_message(item_id, _object_dict(role="assistant", content=""))
if audio_text_marks:
self._conversation.pending_item_audio_text_marks[item_id] = list(copy.deepcopy(audio_text_marks))
pending_audio_ms = self._conversation.pending_truncations_ms.get(item_id)
@@ -1073,10 +1083,10 @@ class DuplexEngineSession:
self._conversation.assistant_response_snapshots.pop(response_id, None)
if message is None:
return pending is not None or removed_placeholder
try:
self._conversation.messages.remove(message)
except ValueError:
pass
# Distinct response items can have identical (including empty) content.
self._conversation.messages = [
candidate for candidate in self._conversation.messages if candidate is not message
]
return True
def truncate_history_item(
@@ -1167,10 +1177,9 @@ class DuplexEngineSession:
if changed and self._message_text_len(message) <= 0:
self._conversation.item_ids.pop(item_id, None)
self._conversation.item_audio_text_marks.pop(item_id, None)
try:
self._conversation.messages.remove(message)
except ValueError:
pass
self._conversation.messages = [
candidate for candidate in self._conversation.messages if candidate is not message
]
if item_id.startswith("item_"):
self._conversation.assistant_response_snapshots.pop(item_id.removeprefix("item_"), None)
elif changed and item_id.startswith("item_"):
+1 -1
View File
@@ -1313,7 +1313,7 @@ class DuplexSessionRunner:
{
"type": "audio.cancelled",
"session_id": session.session_id,
"response_id": session.active_response_id,
"response_id": event.get("response_id") or session.active_response_id,
"reason": cancel_reason,
"cancelled_epoch": session.epoch,
"epoch": session.epoch,
@@ -213,16 +213,27 @@ class Qwen3OmniDuplexPlugin(DuplexModelPlugin):
# The history accessor copies message dicts but preserves content identity.
# Keep payloads privately, never put PCM into public conversation events.
has_audio = bool(payload.get("audio"))
last_content = history[-1].get("content") if history else None
current_is_in_history = (
has_audio
and isinstance(last_content, list)
and any(part.get("type") == "audio_url" for part in last_content)
)
# A committed audio item need not be last: camera/text items can arrive
# before response.create. Search only the unanswered user turn so a new
# payload can never overwrite an earlier, already answered utterance.
current_contents = []
if has_audio:
for message in reversed(history):
if message.get("role") != "user":
break
content = message.get("content")
if isinstance(content, list) and any(part.get("type") == "audio_url" for part in content):
current_contents.append(content)
current_is_in_history = bool(current_contents)
if current_is_in_history:
content = history[-1].get("content")
state.audio_history = [(key, value) for key, value in state.audio_history if key is not content]
state.audio_history.append((content, payload))
# The runner concatenates multiple commits made before a response.
# Associate that single payload with all of its source audio items.
state.audio_history = [
(keys, value)
for keys, value in state.audio_history
if not any(key is content for key in keys for content in current_contents)
]
state.audio_history.append((tuple(current_contents), payload))
# Reserve one of the four audio slots for the actual submitted input,
# including when deferred input has no trailing user history entry.
history_limit = 4 if current_is_in_history or not has_audio else 3
@@ -230,33 +241,75 @@ class Qwen3OmniDuplexPlugin(DuplexModelPlugin):
while len(retained) > 1 and sum(len(value.get("audio", "")) for _, value in retained) > 8 * 1024 * 1024:
retained.pop(0)
state.audio_history = retained
# Match the official demo's unit of history: consecutive user items
# followed by their assistant reply(s). Prune whole turns, before
# formatting, so a camera item cannot rescue an orphaned answer.
turns = []
for index, message in enumerate(history):
role = message.get("role")
if not turns or role == "system" or (role == "user" and turns[-1][1][-1].get("role") != "user"):
turns.append((index, []))
turns[-1][1].append(message)
messages = []
def has_image(message):
content = message.get("content")
return isinstance(content, list) and any(p.get("type") == "image_url" for p in content)
selected = [m for m in history[:-16] if has_image(m)] + history[-16:]
for message in selected:
content = message.get("content")
audio = next((value for key, value in retained if key is content), None)
if audio is not None:
messages.append({"role": "user", "audio_payload": audio})
elif isinstance(content, list) and any(part.get("type") == "image_url" for part in content):
messages.append(message)
elif isinstance(content, str) and content:
messages.append(message)
elif message.get("role") == "user":
# Do not keep an assistant answer after dropping its user audio.
messages = [m for m in messages if has_image(m)]
while messages and messages[0].get("role") == "assistant":
messages.pop(0)
for index, turn in turns:
# Keep the existing recent-message window, rounded outwards to a
# complete turn. Never retain an answer whose input was cut off.
if index + len(turn) <= len(history) - 16:
continue
if turn[0].get("role") == "assistant":
continue
resolved = []
included_audio = set()
for message in turn:
content = message.get("content")
if message.get("role") == "user" and isinstance(content, list):
if any(part.get("type") == "audio_url" for part in content):
audio = next((value for keys, value in retained if any(key is content for key in keys)), None)
if audio is None:
break
if id(audio) not in included_audio:
resolved.append({"role": "user", "audio_payload": audio})
included_audio.add(id(audio))
# Preserve text/images in mixed source items as well.
other_parts = [part for part in content if part.get("type") != "audio_url"]
if other_parts:
resolved.append({"role": "user", "content": other_parts})
continue
resolved.append(message)
else:
messages.extend(resolved)
if has_audio and not current_is_in_history:
# History is context, not a replacement for the current request.
# Re-encoding only history here makes Qwen answer an old question.
messages.append({"role": "user", "audio_payload": payload})
return {**config, "qwen_messages": messages}
@staticmethod
def format_history(messages):
"""Merge user items as Qwen's official web_demo.format_history does.
This is a model-input view, not a mutation of addressable session items.
Empty assistant messages remain boundaries for interrupted/unheard turns.
"""
formatted = []
pending = []
for message in messages:
if message.get("role") == "user":
content = message.get("content")
pending.extend([{"type": "text", "text": content}] if isinstance(content, str) else content or [])
continue
if pending:
formatted.append({"role": "user", "content": pending})
pending = []
formatted.append(message)
if pending:
# The official demo puts media before the final turn's instruction.
media = [part for part in pending if part.get("type") != "text"]
text = [part for part in pending if part.get("type") == "text"]
formatted.append({"role": "user", "content": media + text})
return formatted
@staticmethod
def _trim_prompt_images(messages, images):
"""Drop the oldest images, with their placeholders, down to the prompt limit.
@@ -323,7 +376,7 @@ class Qwen3OmniDuplexPlugin(DuplexModelPlugin):
raise DuplexRuntimeConfigError("Qwen generation requires committed audio or conversation history")
audios = []
images = []
for index, message in enumerate(history):
for message in history:
if "audio_payload" not in message:
content = message.get("content")
if isinstance(content, list):
@@ -343,10 +396,11 @@ class Qwen3OmniDuplexPlugin(DuplexModelPlugin):
continue
content, audio = self._audio_content(message["audio_payload"])
audios.append((audio, 16000))
if runtime_config.get("initial_user_text") and index == len(history) - 1:
content.append({"type": "text", "text": runtime_config["initial_user_text"]})
messages.append({"role": "user", "content": content})
if payload.get("audio") and runtime_config.get("initial_user_text"):
messages.append({"role": "user", "content": runtime_config["initial_user_text"]})
messages, images = self._trim_prompt_images(messages, images)
messages = self.format_history(messages)
prompt = self.processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
mm = {"audio": audios} if audios else {}
if images:
@@ -17,8 +17,9 @@ class QwenDuplexSessionState(DuplexModelSessionState):
"""Mutable model-owned state of one Qwen duplex session (owned by the session runner)."""
audio_buffer: QwenPcmBuffer = field(default_factory=QwenPcmBuffer)
# Keep content objects alongside payloads so identity survives history copies.
audio_history: list[tuple[object, dict]] = field(default_factory=list)
# Each payload can combine several audio commits from one pending turn.
# Keep their content objects so associations survive shallow history copies.
audio_history: list[tuple[tuple[object, ...], dict]] = field(default_factory=list)
input_since_commit: bool = False
speech_since_commit: bool = False
context_locked: bool = False