Drain to the final text (the ``done`` frame's text, else accumulated event deltas). Raises :class:`AgentStreamError` on an error outcome.
(self)
| 218 | ) |
| 219 | |
| 220 | async def collect(self) -> str: |
| 221 | """Drain to the final text (the ``done`` frame's text, else accumulated |
| 222 | event deltas). Raises :class:`AgentStreamError` on an error outcome.""" |
| 223 | text = "" |
| 224 | async for frame in self: |
| 225 | if isinstance(frame, EventFrame): |
| 226 | payload = frame.payload if isinstance(frame.payload, dict) else {} |
| 227 | text += payload.get("delta") or payload.get("text") or "" |
| 228 | elif isinstance(frame, DoneFrame): |
| 229 | return frame.text or text |
| 230 | elif isinstance(frame, ErrorFrame): |
| 231 | raise _stream_error(frame.code, frame.message) |
| 232 | return text |
| 233 | |
| 234 | def _require_id(self) -> int: |
| 235 | if self._id is None: |