-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
88 lines (76 loc) · 2.65 KB
/
vite.config.ts
File metadata and controls
88 lines (76 loc) · 2.65 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
import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react"
import { defineConfig, loadEnv, type Plugin } from "vite"
function devApiPlugin(env: Record<string, string>): Plugin {
return {
name: "dev-api",
configureServer(server) {
server.middlewares.use("/api/chat", async (req, res) => {
if (req.method !== "POST") {
res.statusCode = 405
res.end("Method not allowed")
return
}
try {
const chunks: Buffer[] = []
for await (const chunk of req) {
chunks.push(chunk as Buffer)
}
const body = JSON.parse(Buffer.concat(chunks).toString())
const { createHandler } = await import("./src/lib/server/index.ts")
const handler = createHandler({
provider: (env.LLM_PROVIDER as "openai" | "anthropic" | "google") || "openai",
apiKey: env.LLM_API_KEY || "",
model: env.LLM_MODEL || undefined,
})
await handler({ body }, res as any)
} catch (err: any) {
console.error("[dev-api] Error:", err.message || err)
res.statusCode = 500
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify({ error: err.message || "Internal server error" }))
}
})
server.middlewares.use("/api/voice", async (req, res) => {
if (req.method !== "POST") {
res.statusCode = 405
res.end("Method not allowed")
return
}
try {
const chunks: Buffer[] = []
for await (const chunk of req) {
chunks.push(chunk as Buffer)
}
const body = JSON.parse(Buffer.concat(chunks).toString())
const { createVoiceHandler } = await import("./src/lib/server/index.ts")
const handler = createVoiceHandler({
apiKey: env.ELEVENLABS_API_KEY || "",
voiceId: env.ELEVENLABS_VOICE_ID || undefined,
})
await handler({ body }, res as any)
} catch (err: any) {
console.error("[dev-api] Voice error:", err.message || err)
res.statusCode = 500
res.setHeader("Content-Type", "application/json")
res.end(JSON.stringify({ error: err.message || "Internal server error" }))
}
})
},
}
}
export default defineConfig(({ mode }) => {
const env = {
...loadEnv(mode, process.cwd(), "LLM_"),
...loadEnv(mode, process.cwd(), "ELEVENLABS_"),
}
return {
plugins: [react(), tailwindcss(), devApiPlugin(env)],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
}
})