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 Annotated, Literal
|
||||
|
||||
@@ -180,6 +181,49 @@ class StructureBuilder:
|
||||
"pending.event_ids must reference events from the current window"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _keep_window_references(
|
||||
result: _BuildResult,
|
||||
window: Window[Event],
|
||||
) -> _BuildResult:
|
||||
"""Drop impossible image references and repair technical pending IDs."""
|
||||
visual_ids = {
|
||||
event.id
|
||||
for event in window.past + window.present
|
||||
if event.type == "vis"
|
||||
}
|
||||
elements: list[StructureElement] = []
|
||||
for element in result.elements:
|
||||
if isinstance(element, ImageElement) and element.event_id not in visual_ids:
|
||||
logging.warning("Ignoring image outside the current window: id=%s", element.event_id)
|
||||
continue
|
||||
elements.append(element)
|
||||
|
||||
context = result.context
|
||||
if context.pending is not None:
|
||||
window_ids = {
|
||||
event.id
|
||||
for event in window.past + window.present + window.future
|
||||
}
|
||||
pending_ids = [
|
||||
event_id
|
||||
for event_id in context.pending.event_ids
|
||||
if event_id in window_ids
|
||||
]
|
||||
if pending_ids != context.pending.event_ids:
|
||||
logging.warning(
|
||||
"Trimming pending IDs to the current window: %s -> %s",
|
||||
context.pending.event_ids,
|
||||
pending_ids,
|
||||
)
|
||||
pending = (
|
||||
context.pending.model_copy(update={"event_ids": pending_ids})
|
||||
if pending_ids else None
|
||||
)
|
||||
context = context.model_copy(update={"pending": pending})
|
||||
|
||||
return result.model_copy(update={"elements": elements, "context": context})
|
||||
|
||||
def _build_window(self, window: Window[Event]) -> _BuildResult:
|
||||
"""Build document elements from a single timeline window."""
|
||||
messages = [
|
||||
@@ -211,6 +255,7 @@ class StructureBuilder:
|
||||
|
||||
try:
|
||||
result = _BuildResult.model_validate_json(response)
|
||||
result = self._keep_window_references(result, window)
|
||||
self._validate_result(result, window)
|
||||
return result
|
||||
except (ValidationError, ValueError, TypeError):
|
||||
|
||||
Reference in New Issue
Block a user