Mechanical conversion of every relative import under v2/ to an absolute v2.* path (from .x / ..x / ...x -> from v2.<pkg>.x) so imports are unambiguous, grep-able, and stable when code is copied/moved between entrypoints (VideoGenerator, CLI, server). Surgical prefix-only rewrite: only the 'from <dots><module>' prefix changed — import names, parentheses, multi-line formatting, comments, and ordering are byte-for-byte preserved (no collapsing, no reorder, no unrelated reformatting). - 471 imports across 125 files; v2/tests/ was already absolute (untouched). - Validated: all 125 files compile, every 'from v2.* import' target resolves to a real module/package, zero relative imports remain (full sweep), CPU mini suite green (202 passed, 2 skipped).
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
"""Streams + typed event taxonomy (design_v3 §12).
|
|
|
|
"A ``Stream`` is one ordered event channel for previews, media chunks, progress,
|
|
logs, finals." Event taxonomy: ``request.*``, ``session.*``, ``artifact.*``,
|
|
``media.{init,chunk,complete}``, ``trace.*``. A ``media.chunk`` must know its
|
|
stream, byte-range/buffer-ref, codec/container, timestamp range, and
|
|
preview-vs-final — invalid combinations are unrepresentable.
|
|
|
|
For v2 the Stream is a simple synchronous ordered channel (the core is
|
|
numpy/sync and CPU-testable); a GPU/server deployment swaps in an async channel
|
|
without changing the event types.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from v2._types import TensorLike
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class OmniEvent:
|
|
type: str # e.g. "request.start", "media.chunk", "artifact.ready", "trace.step"
|
|
request_id: str
|
|
seq: int = 0
|
|
payload: dict[str, object] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class StreamChunk:
|
|
"""A media.chunk payload — invalid field combinations are unrepresentable."""
|
|
stream_id: str
|
|
modality: str # "video" | "audio" | "text"
|
|
seq: int
|
|
data: TensorLike = None # in-proc buffer (frames / samples / token ids)
|
|
codec: str = "raw" # raw | h264 | opus | ...
|
|
ts_start: float = 0.0 # presentation timestamp range (seconds)
|
|
ts_end: float = 0.0
|
|
preview: bool = False # preview vs final
|
|
final: bool = False # last chunk of the stream
|
|
|
|
|
|
class Stream:
|
|
"""Ordered event channel. Sync collector for tests; subscribe for streaming."""
|
|
|
|
def __init__(self, stream_id: str):
|
|
self.stream_id = stream_id
|
|
self.events: list[OmniEvent] = []
|
|
self._subscribers: list = []
|
|
|
|
def emit(self, event: OmniEvent) -> None:
|
|
self.events.append(event)
|
|
for cb in self._subscribers:
|
|
cb(event)
|
|
|
|
def subscribe(self, callback) -> None:
|
|
self._subscribers.append(callback)
|
|
|
|
def __iter__(self):
|
|
return iter(self.events)
|