-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
115 lines (106 loc) · 3.97 KB
/
Copy pathvite.config.ts
File metadata and controls
115 lines (106 loc) · 3.97 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
/**
* Vite Configuration
* ==================
* - React plugin for JSX/TSX support
* - Dev-only middleware that proxies /api/chat requests to
* the Gemini SDK so the chatbot works during local development.
* In production (Vercel), the /api/chat.ts serverless function
* handles this instead.
*
* The system prompt is shared via data/system-prompt.ts so
* dev and prod give identical chatbot responses.
*/
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import { configDefaults } from 'vitest/config';
import { SYSTEM_PROMPT } from './api/system-prompt';
export default defineConfig(({ mode }) => {
// Load env vars (without VITE_ prefix) for server-side use
const env = loadEnv(mode, process.cwd(), '');
return {
test: {
globals: true,
environment: 'jsdom',
setupFiles: './test/setup.ts',
exclude: [...configDefaults.exclude, 'e2e/**'],
},
plugins: [
react(),
{
name: 'gemini-dev-proxy',
configureServer(server) {
/**
* Dev middleware: intercepts POST /api/chat and calls
* the Gemini SDK directly (server-side, no key in browser).
*/
server.middlewares.use('/api/chat', async (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({ error: { code: 'METHOD_NOT_ALLOWED', message: 'Use POST.' } })
);
return;
}
let body = '';
req.on('data', (chunk: Buffer) => {
body += chunk.toString();
});
req.on('end', async () => {
try {
const { message } = JSON.parse(body);
if (!message || typeof message !== 'string' || !message.trim()) {
res.statusCode = 400;
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
error: {
code: 'INVALID_INPUT',
message: 'A non-empty "message" is required.',
},
})
);
return;
}
const apiKey = env.GEMINI_API_KEY;
if (!apiKey) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
error: { code: 'CONFIG_ERROR', message: 'GEMINI_API_KEY not set in .env' },
})
);
return;
}
// Dynamic import keeps the SDK out of the client bundle
const { GoogleGenAI } = await import('@google/genai');
const client = new GoogleGenAI({ apiKey });
const response = await client.models.generateContent({
model: 'gemini-2.5-flash',
contents: message.trim(),
config: { systemInstruction: SYSTEM_PROMPT },
});
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ reply: response.text || "I didn't get a response." }));
} catch (error: unknown) {
const err = error instanceof Error ? error : new Error(String(error));
console.error('[Dev API] Gemini error:', err.message);
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(
JSON.stringify({
error: {
code: 'DEV_ERROR',
message: 'Chat error in dev mode. Check terminal.',
},
})
);
}
});
});
},
},
],
};
});