/** * G0DM0D3 Research Preview API * * Exposes the core engines (AutoTune, Parseltongue, STM, Feedback Loop) * and the flagship ULTRAPLINIAN multi-model racing mode as a REST API. * * Includes opt-in dataset collection for building an open source research dataset. * Enterprise paywall: tier-based access control (free/pro/enterprise). * * Designed for deployment on Hugging Face Spaces (Docker) or any container host. */ import express from 'express' import cors from 'cors' import helmet from 'helmet' import { rateLimit } from './middleware/rateLimit' import { apiKeyAuth } from './middleware/auth' import { tierGate } from './middleware/tierGate' import { autotuneRoutes } from './routes/autotune' import { parseltongueRoutes } from './routes/parseltongue' import { transformRoutes } from './routes/transform' import { chatRoutes } from './routes/chat' import { feedbackRoutes } from './routes/feedback' import { ultraplinianRoutes } from './routes/ultraplinian' import { consortiumRoutes } from './routes/consortium' import { datasetRoutes } from './routes/dataset' import { metadataRoutes } from './routes/metadata' import { researchRoutes } from './routes/research' import { isPublisherEnabled, startPeriodicFlush, shutdownFlush, getPublisherStatus } from './lib/hf-publisher' import { TIER_CONFIGS } from './lib/tiers' import { ULTRAPLINIAN_MODELS } from './lib/ultraplinian' import type { TierConfig } from './lib/tiers' const app = express() const PORT = parseInt(process.env.PORT || '7860', 10) // HF Spaces default // ── Middleware ───────────────────────────────────────────────────────── // CORS: Allow configured origins. When self-hosting, set CORS_ORIGIN=* to allow all. const corsOrigins = process.env.CORS_ORIGIN === '*' ? true // Allow all origins (self-hosted / behind reverse proxy) : [process.env.CORS_ORIGIN || 'https://godmod3.ai', ...(process.env.HF_SPACE_URL ? [process.env.HF_SPACE_URL] : [])].filter(Boolean) app.use(cors({ origin: corsOrigins, credentials: false })) // Security headers via helmet app.use(helmet({ contentSecurityPolicy: { directives: { defaultSrc: ["'none'"], scriptSrc: ["'self'", "'unsafe-inline'"], styleSrc: ["'self'", "'unsafe-inline'", 'https://fonts.googleapis.com'], fontSrc: ['https://fonts.gstatic.com'], connectSrc: ["'self'", 'https://openrouter.ai', 'https://*.openrouter.ai', 'https://*.huggingface.co'], imgSrc: ["'self'", 'data:', 'blob:'], baseUri: ["'none'"], formAction: ["'none'"], frameAncestors: ["'none'"], }, }, crossOriginOpenerPolicy: { policy: 'same-origin' }, crossOriginResourcePolicy: { policy: 'same-origin' }, frameguard: { action: 'deny' }, hsts: { maxAge: 31536000, includeSubDomains: true, preload: true }, referrerPolicy: { policy: 'strict-origin-when-cross-origin' }, permittedCrossDomainPolicies: false, xXssProtection: false, // X-XSS-Protection: 0 (modern best practice — CSP replaces it) })) // Permissions-Policy (not covered by helmet) app.use((_req, res, next) => { res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=(), payment=(), usb=(), magnetometer=(), gyroscope=(), accelerometer=()') next() }) app.use(express.json({ limit: '1mb' })) // ── Health / Info (no auth required) ────────────────────────────────── app.get('/v1/health', (_req, res) => { res.json({ status: 'ok', timestamp: Date.now() }) }) app.get('/v1/info', (_req, res) => { res.json({ name: 'G0DM0D3 Research Preview API', version: '0.4.0', description: 'ULTRAPLINIAN multi-model racing with Liquid Response live upgrades, context-adaptive parameter tuning, text transformation, obfuscation, opt-in open dataset collection, and full Research API for querying the published corpus on HuggingFace.', license: 'AGPL-3.0', flagship: 'POST /v1/ultraplinian/completions', consortium: 'POST /v1/consortium/completions', defaults: { stream: true, liquid_min_delta: 8, note: 'Streaming (Liquid Response) is ON by default. The first good response is served immediately via SSE, then auto-upgraded when a better model beats the current leader by liquid_min_delta score points.', }, tiers: { free: { label: 'Free', limits: TIER_CONFIGS.free.rateLimit, ultraplinian: TIER_CONFIGS.free.ultraplinianTiers, research: TIER_CONFIGS.free.researchAccess }, pro: { label: 'Pro', limits: TIER_CONFIGS.pro.rateLimit, ultraplinian: TIER_CONFIGS.pro.ultraplinianTiers, research: TIER_CONFIGS.pro.researchAccess }, enterprise: { label: 'Enterprise', limits: TIER_CONFIGS.enterprise.rateLimit, ultraplinian: TIER_CONFIGS.enterprise.ultraplinianTiers, research: TIER_CONFIGS.enterprise.researchAccess }, }, endpoints: { 'GET /v1/tier': 'Check your current tier, limits, and feature access', 'POST /v1/ultraplinian/completions': 'ULTRAPLINIAN: Race N models in parallel with Liquid Response (stream=true default). First good response served immediately, auto-upgrades live.', 'POST /v1/consortium/completions': 'CONSORTIUM: Collect ALL model responses, orchestrator synthesizes ground truth from collective intelligence.', 'POST /v1/chat/completions': 'Single-model pipeline with GODMODE + AutoTune + Parseltongue + STM. Also supports model="ultraplinian/*" and model="consortium/*" virtual models.', 'POST /v1/autotune/analyze': 'Analyze message context and compute optimal LLM parameters', 'POST /v1/parseltongue/encode': 'Obfuscate trigger words in text', 'POST /v1/parseltongue/detect': 'Detect trigger words in text', 'POST /v1/transform': 'Apply semantic transformation modules to text', 'POST /v1/feedback': 'Submit quality feedback for the EMA learning loop', 'GET /v1/dataset/stats': 'Dataset collection statistics (Pro+)', 'GET /v1/dataset/export': 'Export the open research dataset (Pro+)', 'GET /v1/metadata/stats': 'ZDR usage analytics (models, latency, pipeline stats — no content)', 'GET /v1/metadata/events': 'Raw metadata event log (Enterprise only)', 'GET /v1/research/info': 'Research dataset schema, repo info (Pro+)', 'GET /v1/research/stats': 'Aggregate stats across all published HF batches (Pro+)', 'GET /v1/research/batches': 'List all published batch files (Pro+)', 'GET /v1/research/batch/*': 'Read a specific batch file (Pro+)', 'GET /v1/research/query': 'Query the full corpus with filters (Enterprise)', 'POST /v1/research/flush': 'Force-flush in-memory buffers to HuggingFace (Enterprise)', 'GET /v1/research/download': 'Download full corpus as streaming JSONL (Enterprise)', 'GET /v1/research/combined-stats': 'Combined in-memory + published stats (Pro+)', }, authentication: { openrouter_key: process.env.OPENROUTER_API_KEY ? 'Server-provided (callers do NOT need their own OpenRouter key)' : 'Caller must provide openrouter_api_key in request body', api_key: 'Send Authorization: Bearer (if server has GODMODE_API_KEY set)', tier_assignment: 'Set GODMODE_TIER_KEYS="enterprise:key1,pro:key2" to assign tiers to keys', }, dataset: { note: 'Opt-in per request via contribute_to_dataset: true. Stores full non-system content as supplied; no automatic PII scrubber. Exportable as JSONL for HuggingFace Datasets.', }, auto_publish: getPublisherStatus(), source: 'https://github.com/LYS10S/G0DM0D3', }) }) // ── Models Endpoint (OpenAI-compatible) ─────────────────────────────── // Enterprise users need this for SDK model discovery app.get('/v1/models', (_req, res) => { const allModels = [ ...ULTRAPLINIAN_MODELS.fast, ...ULTRAPLINIAN_MODELS.standard, ...ULTRAPLINIAN_MODELS.full, ] const created = Math.floor(Date.now() / 1000) // Virtual ULTRAPLINIAN models — race N models, return the best const virtualModels = [ { id: 'ultraplinian/fast', owned_by: 'g0dm0d3' }, { id: 'ultraplinian/standard', owned_by: 'g0dm0d3' }, { id: 'ultraplinian/smart', owned_by: 'g0dm0d3' }, { id: 'ultraplinian/power', owned_by: 'g0dm0d3' }, { id: 'ultraplinian/ultra', owned_by: 'g0dm0d3' }, // CONSORTIUM — hive-mind synthesis from all models { id: 'consortium/fast', owned_by: 'g0dm0d3' }, { id: 'consortium/standard', owned_by: 'g0dm0d3' }, { id: 'consortium/smart', owned_by: 'g0dm0d3' }, { id: 'consortium/power', owned_by: 'g0dm0d3' }, { id: 'consortium/ultra', owned_by: 'g0dm0d3' }, ] res.json({ object: 'list', data: [ // Virtual ULTRAPLINIAN models first ...virtualModels.map(m => ({ id: m.id, object: 'model' as const, created, owned_by: m.owned_by, })), // Individual models ...allModels.map(id => ({ id, object: 'model' as const, created, owned_by: id.split('/')[0] || 'unknown', })), ], }) }) // ── Tier Info Endpoint (authenticated) ──────────────────────────────── app.get('/v1/tier', apiKeyAuth, (req, res) => { const tier = req.tier || 'free' const config: TierConfig = req.tierConfig res.json({ tier: config.name, label: config.label, limits: config.rateLimit, features: { ultraplinian_tiers: config.ultraplinianTiers, max_race_models: config.maxRaceModels, research_access: config.researchAccess, dataset_export_formats: config.datasetExportFormats, can_flush: config.canFlush, can_access_metadata_events: config.canAccessMetadataEvents, can_download_corpus: config.canDownloadCorpus, }, upgrade: tier !== 'enterprise' ? 'Contact sales or set GODMODE_TIER_KEYS to upgrade your API key tier.' : undefined, }) }) // ── Core routes (all tiers) ─────────────────────────────────────────── app.use('/v1/ultraplinian', apiKeyAuth, rateLimit, ultraplinianRoutes) app.use('/v1/consortium', apiKeyAuth, rateLimit, consortiumRoutes) app.use('/v1/chat', apiKeyAuth, rateLimit, chatRoutes) app.use('/v1/autotune', apiKeyAuth, rateLimit, autotuneRoutes) app.use('/v1/parseltongue', apiKeyAuth, rateLimit, parseltongueRoutes) app.use('/v1/transform', apiKeyAuth, rateLimit, transformRoutes) app.use('/v1/feedback', apiKeyAuth, rateLimit, feedbackRoutes) // ── Gated routes ────────────────────────────────────────────────────── // Dataset: Pro+ for export, stats accessible by all app.use('/v1/dataset', apiKeyAuth, rateLimit, tierGate('dataset:export'), datasetRoutes) // Metadata: stats open to all auth'd users, events gated to Enterprise app.use('/v1/metadata', apiKeyAuth, metadataRoutes) // individual route-level gating in metadata routes // Research: Pro+ for read access, Enterprise for full access app.use('/v1/research', apiKeyAuth, rateLimit, tierGate('research:read'), researchRoutes) // ── 404 ─────────────────────────────────────────────────────────────── app.use((_req, res) => { res.status(404).json({ error: 'Not found. See GET /v1/info for available endpoints.' }) }) // ── Error handler ───────────────────────────────────────────────────── app.use((err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => { console.error('[API Error]', err.message) res.status(500).json({ error: 'Internal server error' }) }) // ── Start ───────────────────────────────────────────────────────────── app.listen(PORT, '0.0.0.0', () => { const hfStatus = isPublisherEnabled() ? `ON → ${process.env.HF_DATASET_REPO}` : 'OFF (set HF_TOKEN + HF_DATASET_REPO to enable)' console.log(` ╔══════════════════════════════════════════════════════════╗ ║ G0DM0D3 Research Preview API v0.4.0 ║ ║ Listening on http://0.0.0.0:${PORT} ║ ║ ║ ║ TIERS: ║ ║ FREE 5 req total, 10/min, 50/day ║ ║ PRO unlimited, 60/min, 1000/day ║ ║ ENTERPRISE unlimited, 300/min, 10000/day ║ ║ ║ ║ FLAGSHIP: ║ ║ POST /v1/ultraplinian/completions Multi-model racing ║ ║ POST /v1/consortium/completions Hive-mind synthesis ║ ║ ║ ║ ENGINES (all tiers): ║ ║ POST /v1/chat/completions Single-model + GODMODE ║ ║ POST /v1/autotune/analyze Context analysis ║ ║ POST /v1/parseltongue/encode Text obfuscation ║ ║ POST /v1/transform STM transforms ║ ║ POST /v1/feedback Feedback loop ║ ║ ║ ║ GATED (Pro+): ║ ║ GET /v1/dataset/export Export dataset ║ ║ GET /v1/research/stats Published corpus stats ║ ║ GET /v1/research/batches List HF batch files ║ ║ ║ ║ GATED (Enterprise): ║ ║ GET /v1/research/query Query full corpus ║ ║ GET /v1/research/download Download corpus (JSONL) ║ ║ POST /v1/research/flush Force-flush to HF ║ ║ GET /v1/metadata/events Raw metadata event log ║ ║ ║ ║ TIER CHECK: ║ ║ GET /v1/tier Your tier + limits ║ ║ ║ ║ AUTO-PUBLISH: ${hfStatus.padEnd(39)}║ ╚══════════════════════════════════════════════════════════╝ `) if (!process.env.GODMODE_API_KEY && !process.env.GODMODE_API_KEYS) { console.warn(' ⚠ WARNING: No GODMODE_API_KEY or GODMODE_API_KEYS set — all routes are unauthenticated!') } if (!process.env.GODMODE_TIER_KEYS) { console.warn(' ⚠ WARNING: No GODMODE_TIER_KEYS set — all keys default to free tier') } if (!process.env.HF_TOKEN) { console.warn(' ⚠ WARNING: HF_TOKEN not set — auto-publish to HuggingFace is DISABLED') } else if (!process.env.HF_DATASET_REPO) { console.warn(' ⚠ WARNING: HF_DATASET_REPO not set — auto-publish to HuggingFace is DISABLED (token is set but no target repo)') } // Start periodic HF flush (no-op if not configured) startPeriodicFlush() }) // ── Graceful Shutdown ───────────────────────────────────────────────── // Flush remaining metadata/dataset to HF before the container dies async function gracefulShutdown(signal: string) { console.log(`\n[${signal}] Shutting down...`) await shutdownFlush() process.exit(0) } process.on('SIGTERM', () => gracefulShutdown('SIGTERM')) process.on('SIGINT', () => gracefulShutdown('SIGINT')) export default app