[Bugfix][MiniCPM-o] Checkpoint playback before final input commit (#6821)
Signed-off-by: Gao Han <hgaoaf@connect.ust.hk>
This commit is contained in:
1 parent
b81aeb7b86
commit
705e781b0e
7 files changed
+334
-29
No files matched your search
@@ -412,11 +412,13 @@ class PlaybackLedger:
|
||||
class ConversationHistory:
|
||||
messages: list[dict[str, object]] = field(default_factory=list)
|
||||
item_ids: dict[str, dict[str, object]] = field(default_factory=dict)
|
||||
history_item_placeholders: dict[str, dict[str, object]] = field(default_factory=dict)
|
||||
item_audio_text_marks: dict[str, list[DuplexAssistantAudioTextMark]] = field(default_factory=dict)
|
||||
pending_item_ids: dict[str, dict[str, object]] = field(default_factory=dict)
|
||||
pending_item_audio_text_marks: dict[str, list[DuplexAssistantAudioTextMark]] = field(default_factory=dict)
|
||||
pending_item_input_commit_seqs: dict[str, int] = field(default_factory=dict)
|
||||
pending_truncations_ms: dict[str, int] = field(default_factory=dict)
|
||||
hard_truncations_ms: dict[str, int] = field(default_factory=dict)
|
||||
last_assistant_full_message: dict[str, object] | None = None
|
||||
last_assistant_audio_text_marks: list[DuplexAssistantAudioTextMark] = field(default_factory=list)
|
||||
assistant_response_snapshots: dict[str, tuple[dict[str, object], tuple[DuplexAssistantAudioTextMark, ...], int]] = (
|
||||
@@ -471,7 +473,8 @@ class DuplexSession:
|
||||
|
||||
@property
|
||||
def history(self) -> tuple[dict[str, object], ...]:
|
||||
return tuple(dict(message) for message in self._conversation.messages)
|
||||
placeholders = {id(message) for message in self._conversation.history_item_placeholders.values()}
|
||||
return tuple(dict(message) for message in self._conversation.messages if id(message) not in placeholders)
|
||||
|
||||
@property
|
||||
def active_request_id(self) -> str | None:
|
||||
@@ -521,6 +524,11 @@ class DuplexSession:
|
||||
return response_id in self._conversation.assistant_response_snapshots
|
||||
|
||||
def playback_ack_is_too_late(self, response_id: str, item_id: str) -> bool:
|
||||
# An earlier ACK reserved this item's exact history position. Later
|
||||
# ACKs update that slot in place, so they cannot append an old
|
||||
# assistant turn after a newer user input.
|
||||
if item_id in self._conversation.item_ids or item_id in self._conversation.history_item_placeholders:
|
||||
return False
|
||||
input_commit_seq = self._conversation.pending_item_input_commit_seqs.get(item_id)
|
||||
if input_commit_seq is None:
|
||||
snapshot = self._conversation.assistant_response_snapshots.get(response_id)
|
||||
@@ -530,6 +538,56 @@ class DuplexSession:
|
||||
input_commit_seq = self._response.active_response_input_commit_seq
|
||||
return input_commit_seq is not None and self.input_commit_seq > input_commit_seq
|
||||
|
||||
def reserve_history_item(self, item_id: str) -> None:
|
||||
"""Reserve the current history position for a response-owned item."""
|
||||
if item_id in self._conversation.item_ids or item_id in self._conversation.history_item_placeholders:
|
||||
return
|
||||
placeholder: dict[str, object] = {}
|
||||
self._conversation.history_item_placeholders[item_id] = placeholder
|
||||
self._conversation.messages.append(placeholder)
|
||||
|
||||
def _store_history_item_message(
|
||||
self,
|
||||
item_id: str,
|
||||
message: dict[str, object],
|
||||
) -> dict[str, object]:
|
||||
"""Materialize or update a response item without changing its order."""
|
||||
existing = self._conversation.item_ids.get(item_id)
|
||||
if existing is not None:
|
||||
if existing is not message:
|
||||
existing.clear()
|
||||
existing.update(message)
|
||||
return existing
|
||||
|
||||
placeholder = self._conversation.history_item_placeholders.pop(item_id, None)
|
||||
if placeholder is not None:
|
||||
for index, candidate in enumerate(self._conversation.messages):
|
||||
if candidate is placeholder:
|
||||
self._conversation.messages[index] = message
|
||||
break
|
||||
else:
|
||||
self._conversation.messages.append(message)
|
||||
elif not any(candidate is message for candidate in self._conversation.messages):
|
||||
self._conversation.messages.append(message)
|
||||
self._conversation.item_ids[item_id] = message
|
||||
return message
|
||||
|
||||
def _discard_history_item_placeholder(self, item_id: str) -> bool:
|
||||
placeholder = self._conversation.history_item_placeholders.pop(item_id, None)
|
||||
if placeholder is None:
|
||||
return False
|
||||
self._conversation.messages = [
|
||||
candidate for candidate in self._conversation.messages if candidate is not placeholder
|
||||
]
|
||||
return True
|
||||
|
||||
def release_response_history_snapshot(self, response_id: str | None) -> None:
|
||||
"""Release a final response snapshot after its playback fully commits."""
|
||||
if response_id is None or response_id == self.active_response_id:
|
||||
return
|
||||
self._conversation.assistant_response_snapshots.pop(response_id, None)
|
||||
self._conversation.hard_truncations_ms.pop(f"item_{response_id}", None)
|
||||
|
||||
@property
|
||||
def playback(self) -> DuplexPlaybackView:
|
||||
return self._playback.current.snapshot()
|
||||
@@ -997,7 +1055,11 @@ class DuplexSession:
|
||||
committed_text = ""
|
||||
if commit_text and committed_text:
|
||||
message = {"role": "assistant", "content": committed_text}
|
||||
self._conversation.messages.append(message)
|
||||
item_id = f"item_{response_id}" if response_id is not None else None
|
||||
if item_id is not None and item_id in self._conversation.history_item_placeholders:
|
||||
self._store_history_item_message(item_id, message)
|
||||
else:
|
||||
self._conversation.messages.append(message)
|
||||
effective_playback_policy = playback_commit_policy or self.config.playback_commit_policy
|
||||
if (
|
||||
response_id is not None
|
||||
@@ -1006,6 +1068,8 @@ class DuplexSession:
|
||||
and effective_playback_policy == DuplexPlaybackCommitPolicy.ACK_ONLY.value
|
||||
):
|
||||
self.register_history_item(f"item_{response_id}", None)
|
||||
elif response_id is not None and not assistant_text:
|
||||
self._discard_history_item_placeholder(f"item_{response_id}")
|
||||
self._response.assistant_text_buffer.clear()
|
||||
if not preserve_request:
|
||||
self._response.active_request_id = None
|
||||
@@ -1034,7 +1098,7 @@ class DuplexSession:
|
||||
self._conversation.pending_item_input_commit_seqs[item_id] = response_snapshot[2]
|
||||
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.pop(item_id, None)
|
||||
pending_audio_ms = self._conversation.pending_truncations_ms.get(item_id)
|
||||
if pending_audio_ms is not None:
|
||||
self.truncate_history_item(
|
||||
item_id,
|
||||
@@ -1042,30 +1106,11 @@ class DuplexSession:
|
||||
playback=self._playback_cursor_for_item_id(item_id),
|
||||
)
|
||||
return
|
||||
pending_audio_ms = self._conversation.pending_truncations_ms.pop(item_id, None)
|
||||
if pending_audio_ms is not None:
|
||||
self._truncate_message_to_audio_ms(
|
||||
message,
|
||||
audio_end_ms=pending_audio_ms,
|
||||
marks=(
|
||||
response_audio_text_marks
|
||||
if response_audio_text_marks is not None
|
||||
else self._response.assistant_audio_text_marks or self._conversation.last_assistant_audio_text_marks
|
||||
),
|
||||
playback=self._playback_cursor_for_item_id(item_id),
|
||||
)
|
||||
if self._message_text_len(message) <= 0:
|
||||
try:
|
||||
self._conversation.messages.remove(message)
|
||||
except ValueError:
|
||||
pass
|
||||
return
|
||||
self._conversation.item_ids[item_id] = message
|
||||
message = self._store_history_item_message(item_id, message)
|
||||
pending_audio_ms = self._conversation.pending_truncations_ms.get(item_id)
|
||||
self._conversation.pending_item_ids.pop(item_id, None)
|
||||
self._conversation.pending_item_audio_text_marks.pop(item_id, None)
|
||||
self._conversation.pending_item_input_commit_seqs.pop(item_id, None)
|
||||
if response_id is not None:
|
||||
self._conversation.assistant_response_snapshots.pop(response_id, None)
|
||||
if message.get("role") == "assistant":
|
||||
marks = (
|
||||
response_audio_text_marks
|
||||
@@ -1074,6 +1119,12 @@ class DuplexSession:
|
||||
)
|
||||
if marks:
|
||||
self._conversation.item_audio_text_marks[item_id] = list(marks)
|
||||
if pending_audio_ms is not None:
|
||||
self.truncate_history_item(
|
||||
item_id,
|
||||
audio_end_ms=pending_audio_ms,
|
||||
playback=self._playback_cursor_for_item_id(item_id),
|
||||
)
|
||||
|
||||
def delete_history_item(self, item_id: str) -> bool:
|
||||
response_id = item_id.removeprefix("item_") if item_id.startswith("item_") else None
|
||||
@@ -1083,10 +1134,12 @@ class DuplexSession:
|
||||
self._conversation.pending_item_audio_text_marks.pop(item_id, None)
|
||||
self._conversation.pending_item_input_commit_seqs.pop(item_id, None)
|
||||
self._conversation.pending_truncations_ms.pop(item_id, None)
|
||||
self._conversation.hard_truncations_ms.pop(item_id, None)
|
||||
removed_placeholder = self._discard_history_item_placeholder(item_id)
|
||||
if response_id is not None:
|
||||
self._conversation.assistant_response_snapshots.pop(response_id, None)
|
||||
if message is None:
|
||||
return pending is not None
|
||||
return pending is not None or removed_placeholder
|
||||
try:
|
||||
self._conversation.messages.remove(message)
|
||||
except ValueError:
|
||||
@@ -1099,8 +1152,43 @@ class DuplexSession:
|
||||
*,
|
||||
audio_end_ms: int,
|
||||
playback: DuplexPlaybackCursor | DuplexPlaybackView | None = None,
|
||||
hard: bool = False,
|
||||
) -> bool:
|
||||
playback = playback or self._playback_cursor_for_item_id(item_id)
|
||||
audio_end_ms = max(0, int(audio_end_ms))
|
||||
if hard:
|
||||
previous_cap_ms = self._conversation.hard_truncations_ms.get(item_id)
|
||||
self._conversation.hard_truncations_ms[item_id] = (
|
||||
audio_end_ms if previous_cap_ms is None else min(previous_cap_ms, audio_end_ms)
|
||||
)
|
||||
hard_cap_ms = self._conversation.hard_truncations_ms.get(item_id)
|
||||
if hard_cap_ms is not None:
|
||||
audio_end_ms = min(audio_end_ms, hard_cap_ms)
|
||||
response_id = item_id.removeprefix("item_") if item_id.startswith("item_") else None
|
||||
response_snapshot = (
|
||||
self._conversation.assistant_response_snapshots.get(response_id) if response_id is not None else None
|
||||
)
|
||||
if response_snapshot is not None:
|
||||
full_message, full_marks, _ = response_snapshot
|
||||
message = copy.deepcopy(full_message)
|
||||
changed = self._truncate_message_to_audio_ms(
|
||||
message,
|
||||
audio_end_ms=audio_end_ms,
|
||||
marks=list(full_marks),
|
||||
playback=playback,
|
||||
)
|
||||
if not changed or self._message_text_len(message) <= 0:
|
||||
self._conversation.pending_truncations_ms[item_id] = max(0, int(audio_end_ms))
|
||||
return False
|
||||
self._store_history_item_message(item_id, message)
|
||||
if full_marks:
|
||||
self._conversation.item_audio_text_marks[item_id] = list(copy.deepcopy(full_marks))
|
||||
self._conversation.pending_item_ids.pop(item_id, None)
|
||||
self._conversation.pending_item_audio_text_marks.pop(item_id, None)
|
||||
self._conversation.pending_item_input_commit_seqs.pop(item_id, None)
|
||||
self._conversation.pending_truncations_ms.pop(item_id, None)
|
||||
return True
|
||||
|
||||
message = self._conversation.item_ids.get(item_id)
|
||||
if message is None:
|
||||
pending = self._conversation.pending_item_ids.get(item_id)
|
||||
|
||||
Reference in new issue
Block a user