|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, List |
| 4 | + |
| 5 | +from tests.e2e._client import client |
| 6 | + |
| 7 | + |
| 8 | +def new_session() -> str: |
| 9 | + session = client.sessions.create() |
| 10 | + return session.id |
| 11 | + |
| 12 | + |
| 13 | +def store_text(session_id: str, text: str) -> None: |
| 14 | + client.sessions.store_message( |
| 15 | + session_id, |
| 16 | + format="acontext", |
| 17 | + blob={ |
| 18 | + "role": "user", |
| 19 | + "parts": [{"type": "text", "text": text}], |
| 20 | + }, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def _extract_texts_from_acontext_messages(items: list[Any]) -> list[str]: |
| 25 | + texts: list[str] = [] |
| 26 | + for message in items: |
| 27 | + for part in getattr(message, "parts", []) or []: |
| 28 | + if getattr(part, "type", None) == "text" and getattr(part, "text", None): |
| 29 | + texts.append(part.text) |
| 30 | + return texts |
| 31 | + |
| 32 | + |
| 33 | +def _extract_texts_from_openai_messages(items: list[Any]) -> list[str]: |
| 34 | + texts: list[str] = [] |
| 35 | + for message in items: |
| 36 | + if isinstance(message, dict): |
| 37 | + content = message.get("content") |
| 38 | + if isinstance(content, str) and content: |
| 39 | + texts.append(content) |
| 40 | + return texts |
| 41 | + |
| 42 | + |
| 43 | +def get_texts(session_id: str, **kwargs: Any) -> List[str]: |
| 44 | + resp = client.sessions.get_messages(session_id, **kwargs) |
| 45 | + format_ = kwargs.get("format", "openai") |
| 46 | + if format_ == "acontext": |
| 47 | + return _extract_texts_from_acontext_messages(resp.items) |
| 48 | + if format_ == "openai": |
| 49 | + return _extract_texts_from_openai_messages(resp.items) |
| 50 | + return [] |
| 51 | + |
| 52 | + |
| 53 | +def banner(title: str) -> None: |
| 54 | + print("\n" + "=" * 80) |
| 55 | + print(title) |
| 56 | + print("=" * 80) |
| 57 | + |
0 commit comments