Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

adds db connectivity and saving, need to process user messages still

Daniel Avila254f9d7e2683
diff --git a/models/Message.js b/models/Message.js
index f4b76143f1..1529005ff2 100644
--- a/models/Message.js
+++ b/models/Message.js
@@ -1,10 +1,43 @@
-/*
-{
- role: 'assistant',
- id: 'cmpl-6gNfeSy5RUJulgpX1vIlppGdyXjB3',
- parentMessageId: 'e84f127b-201b-4a1d-8b6d-899e7edab5b4',
- conversationId: '97f657cd-be00-490c-94b9-31dba2462a7c',
- text: 'OpenAI is an AI research lab consisting of the for-profit technological company OpenAI LP and its parent company, the non-profit OpenAI Inc. It aims to promote and develop friendly AI in a way that benefits humanity as a whole.'
-}
-*/
+const mongoose = require('mongoose');
+const messageSchema = mongoose.Schema({
+ id: {
+ type: String,
+ unique: true,
+ required: true
+ },
+ conversationId: {
+ type: String,
+ required: true
+ },
+ parentMessageId: {
+ type: String,
+ required: true
+ },
+ sender: {
+ type: String,
+ required: true
+ },
+ text: {
+ type: String,
+ required: true
+ },
+ created: {
+ type: Date,
+ default: Date.now
+ }
+});
+
+const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
+
+module.exports = {
+ saveMessage: async (message) => {
+ const { text, id, parentMessageId, conversationId } = message;
+ await Message.create({
+ id,
+ conversationId,
+ parentMessageId,
+ text
+ });
+ }
+}
\ No newline at end of file
diff --git a/models/dbConnect.js b/models/dbConnect.js
new file mode 100644
index 0000000000..1c3db7e47f
--- /dev/null
+++ b/models/dbConnect.js
@@ -0,0 +1,42 @@
+const mongoose = require('mongoose');
+const MONGO_URI = process.env.MONGO_URI;
+
+if (!MONGO_URI) {
+ throw new Error('Please define the MONGO_URI environment variable inside .env.local');
+}
+
+/**
+ * Global is used here to maintain a cached connection across hot reloads
+ * in development. This prevents connections growing exponentially
+ * during API Route usage.
+ */
+let cached = global.mongoose;
+
+if (!cached) {
+ cached = global.mongoose = { conn: null, promise: null };
+}
+
+async function dbConnect() {
+ if (cached.conn) {
+ return cached.conn;
+ }
+
+ if (!cached.promise) {
+ const opts = {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ bufferCommands: false,
+ // bufferMaxEntries: 0,
+ // useFindAndModify: true,
+ // useCreateIndex: true
+ };
+
+ cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
+ return mongoose;
+ });
+ }
+ cached.conn = await cached.promise;
+ return cached.conn;
+}
+
+module.exports = dbConnect;
diff --git a/server/index.js b/server/index.js
index d0296a56a2..5f67654f6c 100644
--- a/server/index.js
+++ b/server/index.js
@@ -1,6 +1,9 @@
-const path = require('path');
const express = require('express');
const { ask } = require('../app/chatgpt');
+const dbConnect = require('../models/dbConnect');
+const { saveMessage } = require('../models/Message');
+const crypto = require('crypto');
+const path = require('path');
const app = express();
const port = 3050;
const cors = require('cors');
@@ -10,6 +13,8 @@ app.use(express.json());
const projectPath = path.join(__dirname, '..');
app.use(express.static(path.join(projectPath, 'public')));
+dbConnect().then((connection) => console.log('Connected to MongoDB'));
+
app.get('/', function (req, res) {
console.log(path.join(projectPath, 'public', 'index.html'));
res.sendFile(path.join(projectPath, 'public', 'index.html'));
@@ -18,6 +23,7 @@ app.get('/', function (req, res) {
app.post('/ask', async (req, res) => {
console.log(req.body);
const { text, parentMessageId, conversationId } = req.body;
+ const userMessageId = crypto.randomUUID();
res.writeHead(200, {
Connection: 'keep-alive',
@@ -34,11 +40,18 @@ app.post('/ask', async (req, res) => {
res.write(`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`);
i++;
}
- const data = JSON.stringify({...partial, message: true });
+ const data = JSON.stringify({ ...partial, message: true });
res.write(`event: message\ndata: ${data}\n\n`);
};
- const gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
+ let gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
+ if (!!parentMessageId) {
+ console.log('req parent vs res parent', parentMessageId, gptResponse.parentMessageId);
+ gptResponse = { ...gptResponse, parentMessageId, sender: 'GPT' };
+ }
+
+ await saveMessage(gptResponse);
+
res.write(`event: message\ndata: ${JSON.stringify(gptResponse)}\n\n`);
res.end();
});