-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
147 lines (120 loc) Β· 4.23 KB
/
server.js
File metadata and controls
147 lines (120 loc) Β· 4.23 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
import express from 'express'
import cors from 'cors'
import fetch from 'node-fetch'
import dotenv from 'dotenv'
dotenv.config()
const app = express()
const PORT = 3001
app.use(cors())
app.use(express.json())
const BRAVE_API_KEY = process.env.VITE_BRAVE_API_KEY
const BRAVE_API_URL = 'https://api.search.brave.com/res/v1/web/search'
const OPENAI_API_KEY = process.env.VITE_OPENAI_API_KEY
const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions'
// Brave Search Proxy Endpoint
app.post('/api/search', async (req, res) => {
try {
const { query, count = 5 } = req.body
console.log('π Search request:', query)
if (!BRAVE_API_KEY) {
return res.status(500).json({ error: 'Brave API key not configured' })
}
const response = await fetch(
`${BRAVE_API_URL}?q=${encodeURIComponent(query)}&count=${count}`,
{
headers: {
'Accept': 'application/json',
'X-Subscription-Token': BRAVE_API_KEY
}
}
)
if (!response.ok) {
const errorText = await response.text()
console.error('β Brave API error:', response.status, errorText)
return res.status(response.status).json({ error: 'Brave API error', details: errorText })
}
const data = await response.json()
const results = data.web?.results || []
console.log('β
Found', results.length, 'results')
res.json({ results })
} catch (error) {
console.error('β Server error:', error)
res.status(500).json({ error: 'Internal server error', message: error.message })
}
})
// OpenAI API Proxy Endpoint
app.post('/api/openai', async (req, res) => {
try {
const { question, weather, location, recommendation, category } = req.body
console.log('π€ OpenAI request for:', category)
if (!OPENAI_API_KEY) {
return res.status(500).json({ error: 'OpenAI API key not configured' })
}
const systemPrompt = `You are EIGHT, a mystical weather-powered oracle. You speak in short, poetic, actionable phrases.
RULES:
- Use weather as metaphor
- Be confident and declarative
- Keep responses under 15 words
- Include specific recommendations
- NO "I recommend" or "I suggest"
- NO explanations or reasoning
EXAMPLES:
- "Cold seeks warmth. Ramen at Ton Ton awaits."
- "The sky weeps. Honor it. Stay in."
- "Thunder speaks. Jazz answers back."
- "North. The market opens in golden light."`
const userPrompt = `Weather: ${weather.condition}, ${weather.temperature}Β°C
Location: ${location.city}, ${location.country}
Question: "${question}"
Recommendation: ${recommendation}
Category: ${category}
Generate a mystical oracle response (max 15 words) that references the weather and recommends "${recommendation}".`
const response = await fetch(OPENAI_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`
},
body: JSON.stringify({
model: 'gpt-4o-mini',
max_tokens: 100,
temperature: 0.9,
messages: [
{
role: 'system',
content: systemPrompt
},
{
role: 'user',
content: userPrompt
}
]
})
})
if (!response.ok) {
const errorText = await response.text()
console.error('β OpenAI API error:', response.status, errorText)
return res.status(response.status).json({ error: 'OpenAI API error', details: errorText })
}
const data = await response.json()
const message = data.choices[0].message.content
console.log('β
OpenAI response:', message)
res.json({ message })
} catch (error) {
console.error('β OpenAI error:', error)
res.status(500).json({ error: 'Internal server error', message: error.message })
}
})
// Health check
app.get('/health', (req, res) => {
res.json({
status: 'ok',
braveApiConfigured: !!BRAVE_API_KEY,
openaiApiConfigured: !!OPENAI_API_KEY
})
})
app.listen(PORT, () => {
console.log(`π Proxy server running on http://localhost:${PORT}`)
console.log(`π Brave API Key: ${BRAVE_API_KEY ? 'Configured β
' : 'Missing β'}`)
console.log(`π€ OpenAI API Key: ${OPENAI_API_KEY ? 'Configured β
' : 'Missing β'}`)
})