Made refiner, referencer and builder less strict
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import logging
|
||||
import traceback
|
||||
from typing import Literal
|
||||
|
||||
@@ -99,6 +100,47 @@ class VideoReferenceBuilder:
|
||||
raise ValueError("Duplicate reference in a single window")
|
||||
seen.add(key)
|
||||
|
||||
@staticmethod
|
||||
def _keep_present_references(
|
||||
result: _WindowResult,
|
||||
window: Window[VideoReferenceEvent],
|
||||
) -> _WindowResult:
|
||||
"""Keep the usable part of references that cross a window boundary."""
|
||||
present_positions = {
|
||||
event.id: position
|
||||
for position, event in enumerate(window.present)
|
||||
}
|
||||
references: list[UnresolvedReference] = []
|
||||
seen: set[tuple[tuple[int, ...], str]] = set()
|
||||
|
||||
for reference in result.unresolved:
|
||||
ids = sorted(
|
||||
{event_id for event_id in reference.ids if event_id in present_positions},
|
||||
key=present_positions.__getitem__,
|
||||
)
|
||||
if not ids:
|
||||
logging.warning(
|
||||
"Ignoring reference outside present window: ids=%s",
|
||||
reference.ids,
|
||||
)
|
||||
continue
|
||||
if ids != reference.ids:
|
||||
logging.warning(
|
||||
"Trimming reference to present window: ids=%s -> %s",
|
||||
reference.ids,
|
||||
ids,
|
||||
)
|
||||
|
||||
normalized = reference.model_copy(update={"ids": ids})
|
||||
key = (tuple(ids), normalized.type)
|
||||
if key in seen:
|
||||
logging.warning("Ignoring duplicate reference: ids=%s", ids)
|
||||
continue
|
||||
seen.add(key)
|
||||
references.append(normalized)
|
||||
|
||||
return result.model_copy(update={"unresolved": references})
|
||||
|
||||
def _build_window(
|
||||
self,
|
||||
window: Window[VideoReferenceEvent],
|
||||
@@ -135,6 +177,7 @@ class VideoReferenceBuilder:
|
||||
|
||||
try:
|
||||
result = _WindowResult.model_validate_json(response)
|
||||
result = self._keep_present_references(result, window)
|
||||
self._validate_result(result, window)
|
||||
return result
|
||||
except (ValidationError, ValueError, TypeError):
|
||||
|
||||
Reference in New Issue
Block a user