-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
141 lines (131 loc) · 4.14 KB
/
vite.config.ts
File metadata and controls
141 lines (131 loc) · 4.14 KB
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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import type { IncomingMessage } from 'node:http'
import { resolve } from 'node:path'
type ExplainRequest = {
proof?: string
imports?: Array<{ name: string; content: string }>
}
const readJson = async (req: IncomingMessage) =>
new Promise<ExplainRequest>((resolve, reject) => {
let body = ''
req.on('data', (chunk) => {
body += chunk
})
req.on('end', () => {
try {
resolve(body ? JSON.parse(body) : {})
} catch (error) {
reject(error)
}
})
req.on('error', (error) => reject(error))
})
const extractText = (data: any) => {
const responseText = Array.isArray(data?.output)
? data.output
.flatMap((item: any) => item?.content ?? [])
.filter((content: any) => content?.type === 'output_text')
.map((content: any) => content?.text ?? '')
.join('\n')
: ''
const chatText = data?.choices?.[0]?.message?.content
return (responseText || chatText || '').trim()
}
// https://vitejs.dev/config/
export default defineConfig({
base: './',
plugins: [
react(),
{
name: 'openai-proxy',
configureServer(server) {
server.middlewares.use('/api/explain', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405
res.end('Method Not Allowed')
return
}
try {
const apiKey = process.env.OPENAI_API_KEY
if (!apiKey) {
res.statusCode = 500
res.end('OPENAI_API_KEY is not set.')
return
}
const body = await readJson(req)
const proof = body.proof ?? ''
const imports = body.imports ?? []
const importsSection =
imports.length > 0
? imports
.map((entry) => `# ${entry.name}\n${entry.content}`)
.join('\n\n')
: 'None'
const prompt = [
'You are a Lean proof explainer.',
'Explain the proof step by step in clear, plain language.',
'If a tactic depends on an import, mention it briefly.',
'',
'Imports:',
importsSection,
'',
'Proof:',
proof,
].join('\n')
const upstream = await fetch('https://api.openai.com/v1/responses', {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-5-nano',
input: [
{
role: 'system',
content: 'You explain Lean proofs with a technical, concise tone.',
},
{
role: 'user',
content: prompt,
},
],
}),
})
const data = await upstream.json()
if (!upstream.ok) {
res.statusCode = upstream.status
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: data?.error?.message ?? 'OpenAI error.' }))
return
}
const text = extractText(data)
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ text }))
} catch (error) {
res.statusCode = 500
res.end(error instanceof Error ? error.message : 'Server error.')
}
})
},
},
],
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
tactics: resolve(__dirname, 'tactics.html'),
visualizer: resolve(__dirname, 'visualizer.html'),
mathlib: resolve(__dirname, 'mathlib.html'),
'mathlib-city': resolve(__dirname, 'mathlib-city.html'),
'mathlib-vscode': resolve(__dirname, 'mathlib-vscode.html'),
},
},
},
server: {
port: 1421,
strictPort: true,
},
})