-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvite.config.js
More file actions
119 lines (115 loc) · 4.49 KB
/
vite.config.js
File metadata and controls
119 lines (115 loc) · 4.49 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
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
// Custom plugin: serves /api/interview/signed-url during dev
// so the ElevenLabs API key never touches the client bundle.
function elevenLabsSignedUrlPlugin() {
let apiKey = ''
let agentId = ''
return {
name: 'elevenlabs-signed-url',
configResolved(config) {
// loadEnv with '' prefix loads ALL env vars (not just VITE_)
const env = loadEnv('', config.root, '')
apiKey = env.ELEVENLABS_API_KEY || env.VITE_ELEVENLABS_API_KEY || ''
agentId = env.VITE_ELEVENLABS_AGENT_ID || ''
},
configureServer(server) {
server.middlewares.use('/api/interview/signed-url', async (_req, res) => {
if (!apiKey || !agentId) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'ELEVENLABS_API_KEY or VITE_ELEVENLABS_AGENT_ID not set' }))
return
}
try {
const resp = await fetch(
`https://api.elevenlabs.io/v1/convai/conversation/get_signed_url?agent_id=${agentId}`,
{ headers: { 'xi-api-key': apiKey } }
)
if (!resp.ok) {
const text = await resp.text()
res.writeHead(resp.status, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: text }))
return
}
const data = await resp.json()
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ signedUrl: data.signed_url }))
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: err.message }))
}
})
// List all past conversations
server.middlewares.use('/api/interview/conversations', async (req, res, next) => {
// only handle exact path (not sub-paths like /conversations/conv_xxx)
const url = new URL(req.url, 'http://localhost')
if (url.pathname !== '/' && url.pathname !== '') return next()
if (!apiKey || !agentId) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'keys not configured' }))
return
}
try {
const resp = await fetch(
`https://api.elevenlabs.io/v1/convai/conversations?agent_id=${agentId}`,
{ headers: { 'xi-api-key': apiKey } }
)
const data = await resp.json()
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(data))
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: err.message }))
}
})
// Fetch a specific conversation's full transcript
server.middlewares.use('/api/interview/conversation/', async (req, res) => {
if (!apiKey) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'keys not configured' }))
return
}
// extract conversation_id from the URL (e.g. /api/interview/conversation/conv_xxx)
const convId = req.url.replace(/^\//, '').split('?')[0]
if (!convId) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'missing conversation_id' }))
return
}
try {
const resp = await fetch(
`https://api.elevenlabs.io/v1/convai/conversations/${convId}`,
{ headers: { 'xi-api-key': apiKey } }
)
const data = await resp.json()
res.writeHead(resp.ok ? 200 : resp.status, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(data))
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: err.message }))
}
})
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), elevenLabsSignedUrlPlugin()],
server: {
proxy: {
// Health-check ping — just hits the gateway root to see if it's alive
'/api/gateway-health': {
target: 'http://127.0.0.1:18789',
changeOrigin: true,
rewrite: () => '/',
},
// WebSocket proxy for the OpenClaw gateway (used by pipeline init)
'/ws/gateway': {
target: 'ws://127.0.0.1:18789',
ws: true,
changeOrigin: true,
rewrite: () => '/',
},
}
}
})