| 7813 | |
| 7814 | |
| 7815 | class OpenAIFormatter: |
| 7816 | @dataclass |
| 7817 | class ReturnedToken: |
| 7818 | index: int |
| 7819 | text_bytes: bytes |
| 7820 | token: Token |
| 7821 | text_offset: int |
| 7822 | |
| 7823 | @dataclass |
| 7824 | class ResponsesOutputItem: |
| 7825 | output_index: int |
| 7826 | item: Dict[str, Any] |
| 7827 | content_index: Optional[int] = None |
| 7828 | |
| 7829 | @dataclass |
| 7830 | class ResponsesStream: |
| 7831 | body: CreateResponseRequest |
| 7832 | response_id: str |
| 7833 | created_at: float |
| 7834 | model: str |
| 7835 | output: List[Dict[str, Any]] = field(default_factory=list) |
| 7836 | sequence_number: int = 0 |
| 7837 | started: bool = False |
| 7838 | assistant_meta: Dict[str, Any] = field(default_factory=dict) |
| 7839 | reasoning_item: Optional["OpenAIFormatter.ResponsesOutputItem"] = None |
| 7840 | message_item: Optional["OpenAIFormatter.ResponsesOutputItem"] = None |
| 7841 | tool_items: Dict[int, "OpenAIFormatter.ResponsesOutputItem"] = field( |
| 7842 | default_factory=dict |
| 7843 | ) |
| 7844 | final_status: Optional[str] = None |
| 7845 | incomplete_details: Optional[Dict[str, Any]] = None |
| 7846 | |
| 7847 | def __init__(self, model: Model) -> None: |
| 7848 | self.model = model |
| 7849 | |
| 7850 | @staticmethod |
| 7851 | def decode_text(data: bytes) -> str: |
| 7852 | return data.decode("utf-8", errors="ignore") |
| 7853 | |
| 7854 | @staticmethod |
| 7855 | def encode_sse_payload(payload: BaseModel | Dict[str, Any]) -> bytes: |
| 7856 | data = ( |
| 7857 | payload.model_dump(mode="json", exclude_none=True) |
| 7858 | if isinstance(payload, BaseModel) |
| 7859 | else payload |
| 7860 | ) |
| 7861 | return ( |
| 7862 | "data: " |
| 7863 | f"{json.dumps(data, ensure_ascii=False, separators=(',', ':'))}\n\n" |
| 7864 | ).encode("utf-8") |
| 7865 | |
| 7866 | @staticmethod |
| 7867 | def next_stream_chunk( |
| 7868 | stream: Iterator[CompletionChunk], |
| 7869 | ) -> Tuple[bool, Optional[CompletionChunk]]: |
| 7870 | try: |
| 7871 | return False, next(stream) |
| 7872 | except StopIteration: |
no outgoing calls
no test coverage detected
searching dependent graphs…