| 1 | import { NextResponse } from 'next/server'; |
| 2 | |
| 3 | /** |
| 4 | * ╔══════════════════════════════════════════════════════════════════╗ |
| 5 | * ║ POLYBOLOS SDK — SSE Stream Endpoint ║ |
| 6 | * ║ Server-Sent Events for real-time entity streaming ║ |
| 7 | * ║ ║ |
| 8 | * ║ GET /api/sdk/stream → Opens an SSE connection that pushes ║ |
| 9 | * ║ normalized Polybolos entities to the client in real-time. ║ |
| 10 | * ╚══════════════════════════════════════════════════════════════════╝ |
| 11 | */ |
| 12 | |
| 13 | // In-memory entity store shared across the SDK endpoints |
| 14 | const globalForSDK = globalThis as unknown as { |
| 15 | sdkEntityStore: Map<string, any>; |
| 16 | sdkLastUpdate: number; |
| 17 | }; |
| 18 | |
| 19 | if (!globalForSDK.sdkEntityStore) { |
| 20 | globalForSDK.sdkEntityStore = new Map(); |
| 21 | globalForSDK.sdkLastUpdate = Date.now(); |
| 22 | } |
| 23 | |
| 24 | export const dynamic = 'force-dynamic'; |
| 25 | |
| 26 | export async function GET() { |
| 27 | const encoder = new TextEncoder(); |
| 28 | |
| 29 | const stream = new ReadableStream({ |
| 30 | start(controller) { |
| 31 | // Send initial status |
| 32 | const statusEvent = { |
| 33 | type: 'status', |
| 34 | timestamp: new Date().toISOString(), |
| 35 | payload: { |
| 36 | connected: true, |
| 37 | entityCount: globalForSDK.sdkEntityStore.size, |
| 38 | feedCount: 9, |
| 39 | latticeStatus: 'disconnected', |
| 40 | lastUpdate: new Date().toISOString(), |
| 41 | uptime: Date.now() - globalForSDK.sdkLastUpdate, |
| 42 | }, |
| 43 | }; |
| 44 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(statusEvent)}\n\n`)); |
| 45 | |
| 46 | // Send heartbeat every 15 seconds |
| 47 | const heartbeat = setInterval(() => { |
| 48 | try { |
| 49 | const hb = { |
| 50 | type: 'heartbeat', |
| 51 | timestamp: new Date().toISOString(), |
| 52 | payload: { entityCount: globalForSDK.sdkEntityStore.size }, |
| 53 | }; |
| 54 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(hb)}\n\n`)); |
| 55 | } catch { |
| 56 | clearInterval(heartbeat); |
| 57 | } |
| 58 | }, 15000); |
| 59 | |
| 60 | // Send entity updates every 5 seconds if data has changed |
| 61 | let lastSentVersion = 0; |
| 62 | const entityPush = setInterval(() => { |
| 63 | try { |
| 64 | if (globalForSDK.sdkLastUpdate > lastSentVersion) { |
| 65 | lastSentVersion = globalForSDK.sdkLastUpdate; |
| 66 | const entities = Array.from(globalForSDK.sdkEntityStore.values()).slice(0, 500); |
| 67 | const event = { |
| 68 | type: 'entity_update', |
| 69 | timestamp: new Date().toISOString(), |
| 70 | payload: entities, |
| 71 | }; |
| 72 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`)); |
| 73 | } |
| 74 | } catch { |
| 75 | clearInterval(entityPush); |
| 76 | clearInterval(heartbeat); |
| 77 | } |
| 78 | }, 5000); |
| 79 | }, |
| 80 | }); |
| 81 | |
| 82 | return new Response(stream, { |
| 83 | headers: { |
| 84 | 'Content-Type': 'text/event-stream', |
| 85 | 'Cache-Control': 'no-cache, no-store, must-revalidate', |
| 86 | 'Connection': 'keep-alive', |
| 87 | 'X-Accel-Buffering': 'no', |
| 88 | }, |
| 89 | }); |
| 90 | } |