|
| 1 | +from typing import Literal, Optional, Union |
| 2 | + |
| 3 | +from pydantic import BaseModel, Field |
| 4 | + |
| 5 | + |
| 6 | +class VideoContextDTO(BaseModel): |
| 7 | + """Context about a video lecture unit the student is currently viewing. |
| 8 | +
|
| 9 | + Sent from Artemis as part of the optional context array. |
| 10 | + """ |
| 11 | + |
| 12 | + type: Literal["video"] |
| 13 | + lecture_unit_id: int = Field(alias="lectureUnitId", gt=0) |
| 14 | + timestamp: float = Field(ge=0) # in seconds |
| 15 | + |
| 16 | + class Config: |
| 17 | + populate_by_name = True # Allow both snake_case and camelCase |
| 18 | + |
| 19 | + |
| 20 | +class SlidesContextDTO(BaseModel): |
| 21 | + """Context about a slides/PDF lecture unit the student is currently viewing. |
| 22 | +
|
| 23 | + Sent from Artemis as part of the optional context array. |
| 24 | + """ |
| 25 | + |
| 26 | + type: Literal["slides"] |
| 27 | + lecture_unit_id: int = Field(alias="lectureUnitId", gt=0) |
| 28 | + page: int = Field(ge=1) # PDF pages start at 1 |
| 29 | + |
| 30 | + class Config: |
| 31 | + populate_by_name = True # Allow both snake_case and camelCase |
| 32 | + |
| 33 | + |
| 34 | +class CombinedViewContextDTO(BaseModel): |
| 35 | + """Context indicating the student is viewing a lecture unit in the combined |
| 36 | + view. |
| 37 | +
|
| 38 | + Sent from Artemis as part of the optional context array. It nests an optional |
| 39 | + ``slides`` and an optional ``video`` object describing the student's current |
| 40 | + position; at least one of them is always present. Used for RAG filtering to |
| 41 | + scope retrieval to the specific lecture unit. |
| 42 | + """ |
| 43 | + |
| 44 | + type: Literal["combinedView"] |
| 45 | + slides: Optional[SlidesContextDTO] = None |
| 46 | + video: Optional[VideoContextDTO] = None |
| 47 | + |
| 48 | + class Config: |
| 49 | + populate_by_name = True # Allow both snake_case and camelCase |
| 50 | + |
| 51 | + @property |
| 52 | + def lecture_unit_id(self) -> Optional[int]: |
| 53 | + """Derive the lecture unit id from the nested slides/video objects. |
| 54 | +
|
| 55 | + Prefers ``slides.lecture_unit_id`` and falls back to |
| 56 | + ``video.lecture_unit_id``. Returns ``None`` only in the (unexpected) |
| 57 | + case where neither nested object is present. |
| 58 | + """ |
| 59 | + if self.slides is not None: |
| 60 | + return self.slides.lecture_unit_id |
| 61 | + if self.video is not None: |
| 62 | + return self.video.lecture_unit_id |
| 63 | + return None |
| 64 | + |
| 65 | + |
| 66 | +# Union type for all structured context types |
| 67 | +# pylint: disable=invalid-name |
| 68 | +LectureContextDTO = Union[VideoContextDTO, SlidesContextDTO, CombinedViewContextDTO] |
0 commit comments