-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.js
More file actions
165 lines (147 loc) · 5.51 KB
/
vite.config.js
File metadata and controls
165 lines (147 loc) · 5.51 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* global process */
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import {
analyzeSearchInput,
assessOpenedArticle,
extractArticle,
isOpenAiConfigured,
} from './server/credibilityPipeline.js'
function normalizeEnvValue(value) {
if (typeof value !== 'string') return '';
return value.trim();
}
function isPlaceholderKey(value) {
const normalized = normalizeEnvValue(value).toLowerCase();
return !normalized
|| normalized === 'your_openai_api_key_here';
}
// Server-side proxy plugin for OpenAI API calls
// This keeps the API key on the server side, never exposed to the browser
function openaiProxyPlugin(env) {
return {
name: 'openai-proxy',
configureServer(server) {
const getEnvValue = (viteKey, serverKey) => {
const runtimeEnv = loadEnv(server.config.mode, process.cwd(), '');
return normalizeEnvValue(
process.env[serverKey]
|| (viteKey ? process.env[viteKey] : '')
|| runtimeEnv[serverKey]
|| (viteKey ? runtimeEnv[viteKey] : '')
|| env[serverKey]
|| (viteKey ? env[viteKey] : '')
);
};
const readJsonBody = async (req) => {
let body = '';
for await (const chunk of req) {
body += chunk;
}
return body ? JSON.parse(body) : {};
};
const getOpenAiApiKey = () => getEnvValue('', 'OPENAI_API_KEY');
// GET /api/credibility/status - confirm server-side OpenAI configuration
server.middlewares.use('/api/credibility/status', async (req, res) => {
if (req.method !== 'GET') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: 'Method not allowed' }));
return;
}
const apiKey = getOpenAiApiKey();
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
configured: isOpenAiConfigured(apiKey) && !isPlaceholderKey(apiKey),
}));
});
// POST /api/credibility/search - shared OpenAI credibility pipeline for the search flow
server.middlewares.use('/api/credibility/search', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: 'Method not allowed' }));
return;
}
try {
const apiKey = getOpenAiApiKey();
if (isPlaceholderKey(apiKey)) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
error: 'OpenAI API key not configured. Add OPENAI_API_KEY to your .env file.',
}));
return;
}
const payload = await readJsonBody(req);
const result = await analyzeSearchInput({
text: payload.text,
url: payload.url,
language: payload.language,
fileName: payload.fileName,
apiKey,
});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
} catch (err) {
console.error('Search credibility proxy error:', err);
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: err.message || 'Internal credibility proxy error' }));
}
});
// POST /api/credibility/article - shared OpenAI credibility pipeline for a single opened article
server.middlewares.use('/api/credibility/article', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: 'Method not allowed' }));
return;
}
try {
const payload = await readJsonBody(req);
const apiKey = getOpenAiApiKey();
const article = await assessOpenedArticle(payload.article, {
apiKey: isPlaceholderKey(apiKey) ? '' : apiKey,
language: payload.language || 'en',
});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ article }));
} catch (err) {
console.error('Article credibility proxy error:', err);
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: err.message || 'Internal credibility proxy error' }));
}
});
// POST /api/fetch-url — fetch article content from a URL (CORS-free)
server.middlewares.use('/api/fetch-url', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.end(JSON.stringify({ error: 'Method not allowed' }));
return;
}
try {
const { url } = await readJsonBody(req);
const extracted = await extractArticle(url);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(extracted));
} catch (err) {
console.error('URL fetch error:', err);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
success: false,
error: err.message,
url: '',
}));
}
});
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
return {
plugins: [react(), openaiProxyPlugin(env)],
};
})