Spaces:
Paused
Paused
Download src/opencode_api/routes/event.py from AUXteam/opencode-api: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/AUXteam/opencode-api/resolve/bb4ddbd3b0b0dfd7ace5c9004f5bbc6f5ee873a3/src/opencode_api/routes/event.py
- Command line
-
hf download hf://spaces/AUXteam/opencode-api@bb4ddbd3b0b0dfd7ace5c9004f5bbc6f5ee873a3/src/opencode_api/routes/event.py
-
curl -L -o event.py https://huggingface.co/spaces/AUXteam/opencode-api/resolve/bb4ddbd3b0b0dfd7ace5c9004f5bbc6f5ee873a3/src/opencode_api/routes/event.py
1.38 kB
| from fastapi import APIRouter | |
| from fastapi.responses import StreamingResponse | |
| import asyncio | |
| import json | |
| from typing import AsyncIterator | |
| from ..core.bus import Bus, EventInstance | |
| router = APIRouter(tags=["Events"]) | |
| async def subscribe_events(): | |
| async def event_generator() -> AsyncIterator[str]: | |
| queue: asyncio.Queue[EventInstance] = asyncio.Queue() | |
| async def handler(event: EventInstance): | |
| await queue.put(event) | |
| unsubscribe = Bus.subscribe_all(handler) | |
| yield f"data: {json.dumps({'type': 'server.connected', 'payload': {}})}\n\n" | |
| try: | |
| while True: | |
| try: | |
| event = await asyncio.wait_for(queue.get(), timeout=30.0) | |
| yield f"data: {json.dumps({'type': event.type, 'payload': event.payload})}\n\n" | |
| except asyncio.TimeoutError: | |
| yield f"data: {json.dumps({'type': 'server.heartbeat', 'payload': {}})}\n\n" | |
| except asyncio.CancelledError: | |
| pass | |
| finally: | |
| unsubscribe() | |
| return StreamingResponse( | |
| event_generator(), | |
| media_type="text/event-stream", | |
| headers={ | |
| "Cache-Control": "no-cache", | |
| "Connection": "keep-alive", | |
| "X-Accel-Buffering": "no", | |
| } | |
| ) | |