File size: 9,809 Bytes
b28eb4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * 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.
 *
 * Designed for deployment on Hugging Face Spaces (Docker) or any container host.
 */

import express from 'express'
import cors from 'cors'
import path from 'path'
import { rateLimit } from './middleware/rateLimit'
import { apiKeyAuth } from './middleware/auth'
import { autotuneRoutes } from './routes/autotune'
import { parseltongueRoutes } from './routes/parseltongue'
import { transformRoutes } from './routes/transform'
import { chatRoutes } from './routes/chat'
import { completionsRoutes } from './routes/completions'
import { modelsRoutes } from './routes/models'
import { feedbackRoutes } from './routes/feedback'
import { ultraplinianRoutes } from './routes/ultraplinian'
import { datasetRoutes } from './routes/dataset'
import { metadataRoutes } from './routes/metadata'

const app = express()
const PORT = parseInt(process.env.PORT || '7860', 10) // HF Spaces default

// ── Middleware ─────────────────────────────────────────────────────────
app.use(cors({
  origin: process.env.CORS_ORIGIN === '*'
    ? true
    : [process.env.CORS_ORIGIN || 'https://godmod3.ai'].filter(Boolean),
  credentials: false,
}))
app.use(express.json({ limit: '1mb' }))

// ── Static UI ─────────────────────────────────────────────────────────
app.use(express.static(path.join(__dirname, 'public')))

// ── 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: 'OpenAI-compatible API for ULTRAPLINIAN multi-model racing. Use any OpenAI SDK β€” just swap the base_url.',
    license: 'AGPL-3.0',
    quickstart: {
      python: [
        'from openai import OpenAI',
        'client = OpenAI(base_url="<THIS_URL>/v1", api_key="<YOUR_KEY>")',
        'r = client.chat.completions.create(model="ultraplinian", messages=[{"role":"user","content":"Hello"}])',
        'print(r.choices[0].message.content)',
      ],
      curl: 'curl <THIS_URL>/v1/chat/completions -H "Authorization: Bearer <KEY>" -H "Content-Type: application/json" -d \'{"model":"ultraplinian","messages":[{"role":"user","content":"Hello"}]}\'',
    },
    models: {
      'ultraplinian':          'Race 12 models, return the best (fast tier)',
      'ultraplinian-fast':     'Same as ultraplinian',
      'ultraplinian-standard': 'Race 20 models (standard tier)',
      'ultraplinian-full':     'Race 27 models (full tier)',
      '<any-openrouter-model>': 'Single-model with GODMODE pipeline (e.g. openai/gpt-4o)',
    },
    endpoints: {
      'POST /v1/chat/completions':         'OpenAI-compatible β€” works with any SDK (model routing built in)',
      'GET  /v1/models':                   'OpenAI-compatible β€” list available models',
      'POST /v1/ultraplinian/completions':  'Raw ULTRAPLINIAN with Liquid Response SSE (power users)',
      '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',
      'GET  /v1/dataset/export':            'Export the open research dataset (JSON or JSONL)',
      'GET  /v1/metadata/stats':            'ZDR usage analytics (models, latency, pipeline stats β€” no content)',
      'GET  /v1/metadata/events':           'Raw metadata event log (paginated, content-free)',
    },
    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 (or extra_body in Python SDK)',
      api_key: 'Send Authorization: Bearer <your-api-key> header',
    },
    extra_body_options: {
      note: 'Pass G0DM0D3-specific options via extra_body in the OpenAI Python SDK',
      options: {
        godmode: 'boolean (default: true) β€” enable GODMODE system prompt',
        autotune: 'boolean (default: true) β€” auto-tune parameters to context',
        strategy: "'adaptive'|'precise'|'balanced'|'creative'|'chaotic' (default: adaptive)",
        parseltongue: 'boolean (default: true) β€” obfuscate trigger words',
        stm_modules: "string[] (default: ['hedge_reducer','direct_mode']) β€” post-processing",
        previous_winner: 'string β€” model ID to prioritize in ultraplinian race',
        openrouter_api_key: 'string β€” your OpenRouter key (if server does not provide one)',
      },
    },
    limits: {
      requests_total: parseInt(process.env.RATE_LIMIT_TOTAL || '5', 10) || 'unlimited',
      requests_per_minute: 60,
      requests_per_day: 1000,
      note: 'Research preview β€” each API key gets 5 total requests by default. Set RATE_LIMIT_TOTAL=0 to uncap.',
    },
    source: 'https://github.com/LYS10S/G0DM0D3',
  })
})

// ── OpenAI-compatible routes (primary API surface) ────────────────────
app.use('/v1/chat', apiKeyAuth, rateLimit, completionsRoutes)
app.use('/v1/models', apiKeyAuth, modelsRoutes) // no rate limit on model listing

// ── Power-user routes (raw formats, Liquid Response SSE) ──────────────
app.use('/v1/ultraplinian', apiKeyAuth, rateLimit, ultraplinianRoutes)
app.use('/v1/raw/chat', apiKeyAuth, rateLimit, chatRoutes) // legacy raw format

// ── Engine routes ─────────────────────────────────────────────────────
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)
app.use('/v1/dataset', apiKeyAuth, rateLimit, datasetRoutes)
app.use('/v1/metadata', apiKeyAuth, metadataRoutes) // no rate limit β€” read-only analytics

// ── 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', () => {
  console.log(`
  ╔══════════════════════════════════════════════════════════╗
  β•‘  G0DM0D3 API v0.4.0 β€” OpenAI-Compatible                 β•‘
  β•‘  http://0.0.0.0:${String(PORT).padEnd(5)}                                  β•‘
  ╠══════════════════════════════════════════════════════════╣
  β•‘                                                          β•‘
  β•‘  OPENAI-COMPATIBLE (use any SDK):                        β•‘
  β•‘  POST /v1/chat/completions   model="ultraplinian"        β•‘
  β•‘  GET  /v1/models             list available models        β•‘
  β•‘                                                          β•‘
  β•‘  POWER USER:                                             β•‘
  β•‘  POST /v1/ultraplinian/completions  Raw SSE race         β•‘
  β•‘                                                          β•‘
  β•‘  ENGINES:                                                β•‘
  β•‘  POST /v1/autotune/analyze     Context analysis          β•‘
  β•‘  POST /v1/parseltongue/encode  Text obfuscation          β•‘
  β•‘  POST /v1/transform            STM transforms            β•‘
  β•‘  POST /v1/feedback             Feedback loop             β•‘
  β•‘                                                          β•‘
  β•‘  QUICKSTART:                                             β•‘
  β•‘  from openai import OpenAI                               β•‘
  β•‘  c = OpenAI(base_url=".../v1", api_key="g0d_...")        β•‘
  β•‘  c.chat.completions.create(model="ultraplinian", ...)    β•‘
  β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
  `)
})

export default app