Spaces:
Running
on
Zero
Running
on
Zero
Create agentscript.js
Browse files- agentscript.js +32 -0
agentscript.js
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const bodyParser = require('body-parser');
|
3 |
+
const axios = require('axios');
|
4 |
+
const { MongoClient } = require('mongodb');
|
5 |
+
|
6 |
+
const app = express();
|
7 |
+
app.use(bodyParser.json());
|
8 |
+
|
9 |
+
const uri = 'your-mongodb-connection-string';
|
10 |
+
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
|
11 |
+
|
12 |
+
client.connect(err => {
|
13 |
+
if (err) throw err;
|
14 |
+
const collection = client.db("code-generation").collection("snippets");
|
15 |
+
|
16 |
+
app.post('/generate-code', async (req, res) => {
|
17 |
+
const { prompt } = req.body;
|
18 |
+
|
19 |
+
try {
|
20 |
+
const response = await axios.post('http://localhost:8000/generate-code', { prompt });
|
21 |
+
const code = response.data.code;
|
22 |
+
await collection.insertOne({ prompt, code });
|
23 |
+
res.json(response.data);
|
24 |
+
} catch (error) {
|
25 |
+
res.status(500).json({ error: 'Error generating code' });
|
26 |
+
}
|
27 |
+
});
|
28 |
+
|
29 |
+
app.listen(5000, () => {
|
30 |
+
console.log('Server running on port 5000');
|
31 |
+
});
|
32 |
+
});
|