-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
199 lines (175 loc) · 6.08 KB
/
server.js
File metadata and controls
199 lines (175 loc) · 6.08 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
const sqlite3 = require('sqlite3').verbose();
const { v4: uuidv4 } = require('uuid');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(bodyParser.json());
app.use(express.static('client/build'));
// Database setup
const db = new sqlite3.Database('./prompts.db');
// Initialize database
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS prompts (
id TEXT PRIMARY KEY,
original_text TEXT NOT NULL,
enhanced_text TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
model_used TEXT DEFAULT 'deepseek/deepseek-v3-0324',
enhancement_notes TEXT
)`);
});
// Novita API configuration for DeepSeek V3
const NOVITA_API_KEY = process.env.NOVITA_API_KEY;
const NOVITA_API_URL = 'https://api.novita.ai/v3/openai/chat/completions';
// Helper function to enhance prompt using Novita's DeepSeek V3 model
async function enhancePrompt(originalText) {
try {
const response = await axios.post(NOVITA_API_URL, {
model: 'deepseek/deepseek-v3-0324',
messages: [
{
role: 'system',
content: `You are an expert at enhancing prompts for coding agents like Cursor. Your job is to take a user's prompt and make it more effective and clear, but keep it concise.
Key principles:
1. Make the prompt clearer and more specific
2. Add programming language context when relevant
3. Keep the enhanced version similar in length to the original
4. Focus on clarity and structure, not extensive detail
5. Return ONLY the enhanced prompt with no prefixes, explanations, or formatting
6. Do not add "Here's an enhanced version:" or any introductory text
7. Do not add quotes around the response
Transform the user's prompt into a better version that will give good results from a coding agent, but keep it concise and direct.`
},
{
role: 'user',
content: `Please enhance this prompt to make it much more effective for a coding agent: "${originalText}"`
}
],
temperature: 0.3,
max_tokens: 2000,
response_format: { "type": "text" }
}, {
headers: {
'Authorization': `Bearer ${NOVITA_API_KEY}`,
'Content-Type': 'application/json'
},
timeout: 30000
});
let enhancedText = response.data.choices[0].message.content;
// Remove unwanted prefixes that might slip through
const unwantedPrefixes = [
"Here's a significantly enhanced version of your prompt that will be much more effective for a coding agent:",
"Here's an enhanced version of your prompt:",
"Here's the enhanced prompt:",
"Enhanced prompt:",
"Here's a better version:",
"Here's the improved prompt:",
"Improved prompt:"
];
for (const prefix of unwantedPrefixes) {
if (enhancedText.startsWith(prefix)) {
enhancedText = enhancedText.substring(prefix.length).trim();
}
}
// Remove surrounding quotes if present
if ((enhancedText.startsWith('"') && enhancedText.endsWith('"')) ||
(enhancedText.startsWith("'") && enhancedText.endsWith("'"))) {
enhancedText = enhancedText.substring(1, enhancedText.length - 1).trim();
}
return enhancedText;
} catch (error) {
console.error('Error enhancing prompt:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', error.response.data);
}
if (error.code === 'ECONNABORTED') {
console.error('Request timed out');
}
throw new Error('Failed to enhance prompt');
}
}
// API Routes
// Enhance a prompt
app.post('/api/enhance', async (req, res) => {
try {
const { text } = req.body;
if (!text || text.trim().length === 0) {
return res.status(400).json({ error: 'Text is required' });
}
const enhancedText = await enhancePrompt(text);
// Save to database
const promptId = uuidv4();
db.run(
'INSERT INTO prompts (id, original_text, enhanced_text) VALUES (?, ?, ?)',
[promptId, text, enhancedText],
function(err) {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Failed to save prompt' });
}
res.json({
id: promptId,
original: text,
enhanced: enhancedText,
timestamp: new Date().toISOString()
});
}
);
} catch (error) {
console.error('Enhancement error:', error);
res.status(500).json({ error: error.message });
}
});
// Get all prompts
app.get('/api/prompts', (req, res) => {
db.all('SELECT * FROM prompts ORDER BY created_at DESC', (err, rows) => {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Failed to fetch prompts' });
}
res.json(rows);
});
});
// Get a specific prompt
app.get('/api/prompts/:id', (req, res) => {
const { id } = req.params;
db.get('SELECT * FROM prompts WHERE id = ?', [id], (err, row) => {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Failed to fetch prompt' });
}
if (!row) {
return res.status(404).json({ error: 'Prompt not found' });
}
res.json(row);
});
});
// Delete a prompt
app.delete('/api/prompts/:id', (req, res) => {
const { id } = req.params;
db.run('DELETE FROM prompts WHERE id = ?', [id], function(err) {
if (err) {
console.error('Database error:', err);
return res.status(500).json({ error: 'Failed to delete prompt' });
}
if (this.changes === 0) {
return res.status(404).json({ error: 'Prompt not found' });
}
res.json({ message: 'Prompt deleted successfully' });
});
});
// Serve React app
app.get('*', (req, res) => {
res.sendFile(__dirname + '/client/build/index.html');
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Web app available at http://localhost:${PORT}`);
});