| 1 | import { NextRequest, NextResponse } from 'next/server'; |
| 2 | |
| 3 | /** |
| 4 | * ╔══════════════════════════════════════════════════════════════════╗ |
| 5 | * ║ POLYBOLOS SDK — Ingestion Endpoint ║ |
| 6 | * ║ Secure webhook for external platform data push ║ |
| 7 | * ║ ║ |
| 8 | * ║ POST /api/sdk/ingest → Accepts Polybolos-format entities ║ |
| 9 | * ║ from external systems (e.g., Anduril Lattice) and merges ║ |
| 10 | * ║ them into the Common Operating Picture. ║ |
| 11 | * ║ ║ |
| 12 | * ║ GET /api/sdk/ingest → Returns current SDK entity count ║ |
| 13 | * ╚══════════════════════════════════════════════════════════════════╝ |
| 14 | */ |
| 15 | |
| 16 | // Share the entity store with the stream endpoint |
| 17 | const globalForSDK = globalThis as unknown as { |
| 18 | sdkEntityStore: Map<string, any>; |
| 19 | sdkLastUpdate: number; |
| 20 | sdkIngestLog: Array<{ source: string; count: number; timestamp: string }>; |
| 21 | }; |
| 22 | |
| 23 | if (!globalForSDK.sdkEntityStore) { |
| 24 | globalForSDK.sdkEntityStore = new Map(); |
| 25 | globalForSDK.sdkLastUpdate = Date.now(); |
| 26 | } |
| 27 | if (!globalForSDK.sdkIngestLog) { |
| 28 | globalForSDK.sdkIngestLog = []; |
| 29 | } |
| 30 | |
| 31 | // Simple API key validation (in production, use proper auth) |
| 32 | const VALID_KEYS = new Set([ |
| 33 | process.env.SDK_INGEST_KEY || 'polybolos-dev-key', |
| 34 | 'lattice-integration-key', |
| 35 | ]); |
| 36 | |
| 37 | export async function POST(request: NextRequest) { |
| 38 | try { |
| 39 | const body = await request.json(); |
| 40 | |
| 41 | // Validate structure |
| 42 | if (!body.source || !body.apiKey || !Array.isArray(body.entities)) { |
| 43 | return NextResponse.json({ |
| 44 | accepted: 0, |
| 45 | rejected: 0, |
| 46 | errors: ['Invalid payload structure. Required: { source, apiKey, entities[] }'], |
| 47 | timestamp: new Date().toISOString(), |
| 48 | }, { status: 400 }); |
| 49 | } |
| 50 | |
| 51 | // Validate API key |
| 52 | if (!VALID_KEYS.has(body.apiKey)) { |
| 53 | return NextResponse.json({ |
| 54 | accepted: 0, |
| 55 | rejected: 0, |
| 56 | errors: ['Invalid API key'], |
| 57 | timestamp: new Date().toISOString(), |
| 58 | }, { status: 401 }); |
| 59 | } |
| 60 | |
| 61 | let accepted = 0; |
| 62 | let rejected = 0; |
| 63 | const errors: string[] = []; |
| 64 | |
| 65 | for (const entity of body.entities) { |
| 66 | // Validate minimum required fields |
| 67 | if (!entity.id || !entity.position?.lat || !entity.position?.lng) { |
| 68 | rejected++; |
| 69 | errors.push(`Entity missing required fields (id, position.lat, position.lng): ${entity.id || 'unknown'}`); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | // Normalize and store |
| 74 | const normalized = { |
| 75 | id: `ext-${body.source}-${entity.id}`, |
| 76 | name: entity.name || `ENTITY-${entity.id}`, |
| 77 | domain: entity.domain || 'LAND', |
| 78 | entityType: entity.entityType || 'TRACK', |
| 79 | position: { |
| 80 | lat: entity.position.lat, |
| 81 | lng: entity.position.lng, |
| 82 | alt: entity.position.alt, |
| 83 | heading: entity.position.heading, |
| 84 | speed: entity.position.speed, |
| 85 | }, |
| 86 | threat: entity.threat || 'NONE', |
| 87 | classification: entity.classification || 'UNCLASSIFIED', |
| 88 | source: { |
| 89 | provider: body.source, |
| 90 | feed: 'ingest-api', |
| 91 | originalId: entity.id, |
| 92 | confidence: entity.confidence || 0.8, |
| 93 | }, |
| 94 | timestamp: entity.timestamp || new Date().toISOString(), |
| 95 | properties: entity.properties || {}, |
| 96 | display: entity.display || { |
| 97 | color: '#D4AF37', |
| 98 | icon: 'dot-gold', |
| 99 | layerType: 'circle', |
| 100 | glow: false, |
| 101 | scale: 1.0, |
| 102 | }, |
| 103 | }; |
| 104 | |
| 105 | globalForSDK.sdkEntityStore.set(normalized.id, normalized); |
| 106 | accepted++; |
| 107 | } |
| 108 | |
| 109 | // Update timestamp to trigger SSE push |
| 110 | globalForSDK.sdkLastUpdate = Date.now(); |
| 111 | |
| 112 | // Log ingestion |
| 113 | globalForSDK.sdkIngestLog.push({ |
| 114 | source: body.source, |
| 115 | count: accepted, |
| 116 | timestamp: new Date().toISOString(), |
| 117 | }); |
| 118 | // Keep log to last 100 entries |
| 119 | if (globalForSDK.sdkIngestLog.length > 100) { |
| 120 | globalForSDK.sdkIngestLog = globalForSDK.sdkIngestLog.slice(-100); |
| 121 | } |
| 122 | |
| 123 | return NextResponse.json({ |
| 124 | accepted, |
| 125 | rejected, |
| 126 | errors: errors.slice(0, 10), // Limit error list |
| 127 | timestamp: new Date().toISOString(), |
| 128 | }); |
| 129 | } catch (e) { |
| 130 | return NextResponse.json({ |
| 131 | accepted: 0, |
| 132 | rejected: 0, |
| 133 | errors: [`Server error: ${e instanceof Error ? e.message : 'Unknown'}`], |
| 134 | timestamp: new Date().toISOString(), |
| 135 | }, { status: 500 }); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | export async function GET() { |
| 140 | return NextResponse.json({ |
| 141 | sdk: 'polybolos', |
| 142 | version: '1.0.0', |
| 143 | entityCount: globalForSDK.sdkEntityStore.size, |
| 144 | recentIngestions: globalForSDK.sdkIngestLog.slice(-10), |
| 145 | timestamp: new Date().toISOString(), |
| 146 | }); |
| 147 | } |