-
-
- {messages.length === 0 ? (
-
- ) : (
-
- )}
-
+ useEffect(() => {
+ axios.get('/api/me', {
+ timeout: 1000,
+ withCredentials: true
+ }).then((res) => {
+ return res.data
+ }).then((user) => {
+ if (user)
+ dispatch(setUser(user))
+ else {
+ console.log('Not login!')
+ window.location.href = "/auth/login";
+ }
+ }).catch((error) => {
+ console.error(error)
+ console.log('Not login!')
+ window.location.href = "/auth/login";
+ })
+ // setUser
+ }, [])
+
+ if (user)
+ return (
+
+
+
+
+
+ {messages.length === 0 ? (
+
+ ) : (
+
+ )}
+
+
-
- );
+ );
+ else
+ return (
+
+
+
+ )
};
export default App;
diff --git a/client/src/components/Main/TextChat.jsx b/client/src/components/Main/TextChat.jsx
index 0de24e7f52..65eae454ce 100644
--- a/client/src/components/Main/TextChat.jsx
+++ b/client/src/components/Main/TextChat.jsx
@@ -18,6 +18,7 @@ export default function TextChat({ messages }) {
const inputRef = useRef(null)
const isComposing = useRef(false);
const dispatch = useDispatch();
+ const { user } = useSelector((state) => state.user);
const convo = useSelector((state) => state.convo);
const { initial } = useSelector((state) => state.models);
const { isSubmitting, stopStream, submission, disabled, model, chatGptLabel, promptPrefix } =
diff --git a/client/src/components/Nav/Logout.jsx b/client/src/components/Nav/Logout.jsx
new file mode 100644
index 0000000000..637c6c57fe
--- /dev/null
+++ b/client/src/components/Nav/Logout.jsx
@@ -0,0 +1,23 @@
+import React, { useState, useContext } from 'react';
+import { useSelector } from 'react-redux';
+import LogOutIcon from '../svg/LogOutIcon';
+
+
+export default function Logout() {
+ const { user } = useSelector((state) => state.user);
+
+ const clickHandler = () => {
+ window.location.href = "/auth/logout";
+ };
+
+ return (
+
+
+ {user?.display || user?.username || 'USER'}
+ Log out
+
+ );
+}
diff --git a/client/src/components/Nav/NavLinks.jsx b/client/src/components/Nav/NavLinks.jsx
index 892471f6df..9c42b5bb39 100644
--- a/client/src/components/Nav/NavLinks.jsx
+++ b/client/src/components/Nav/NavLinks.jsx
@@ -3,16 +3,18 @@ import NavLink from './NavLink';
import LogOutIcon from '../svg/LogOutIcon';
import ClearConvos from './ClearConvos';
import DarkMode from './DarkMode';
+import Logout from './Logout';
export default function NavLinks() {
return (
<>
-
+ {/*
+ /> */}
>
);
}
diff --git a/client/src/store/index.js b/client/src/store/index.js
index d2de0d9de1..22f717892d 100644
--- a/client/src/store/index.js
+++ b/client/src/store/index.js
@@ -5,6 +5,7 @@ import messageReducer from './messageSlice.js'
import modelReducer from './modelSlice.js'
import submitReducer from './submitSlice.js'
import textReducer from './textSlice.js'
+import userReducer from './userReducer.js'
export const store = configureStore({
reducer: {
@@ -13,6 +14,7 @@ export const store = configureStore({
models: modelReducer,
text: textReducer,
submit: submitReducer,
+ user: userReducer,
},
devTools: true,
});
\ No newline at end of file
diff --git a/client/src/store/userReducer.js b/client/src/store/userReducer.js
new file mode 100644
index 0000000000..bf0591333b
--- /dev/null
+++ b/client/src/store/userReducer.js
@@ -0,0 +1,19 @@
+import { createSlice } from '@reduxjs/toolkit';
+
+const initialState = {
+ user: null,
+};
+
+const currentSlice = createSlice({
+ name: 'user',
+ initialState,
+ reducers: {
+ setUser: (state, action) => {
+ state.user = action.payload;
+ },
+ }
+});
+
+export const { setUser } = currentSlice.actions;
+
+export default currentSlice.reducer;
diff --git a/client/src/utils/fetchers.js b/client/src/utils/fetchers.js
index d9cfa54116..3d9a83413e 100644
--- a/client/src/utils/fetchers.js
+++ b/client/src/utils/fetchers.js
@@ -3,10 +3,10 @@ import axios from 'axios';
import useSWR from 'swr';
import useSWRMutation from 'swr/mutation';
-const fetcher = (url) => fetch(url).then((res) => res.json());
+const fetcher = (url) => fetch(url, {credentials: 'include'}).then((res) => res.json());
const postRequest = async (url, { arg }) => {
- return await axios.post(url, { arg });
+ return await axios.post(url, { withCredentials: true, arg });
};
export const swr = (path, successCallback, options) => {
From b0284b6974d867ccfc93ce3184cbe2f96a72e2fb Mon Sep 17 00:00:00 2001
From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech>
Date: Wed, 15 Mar 2023 16:56:47 +0800
Subject: [PATCH 02/11] sync updates and merge with feat-resubmit
---
api/models/Conversation.js | 2 --
api/server/routes/ask.js | 15 +++++++++------
api/server/routes/askBing.js | 26 ++++++++++++++++----------
api/server/routes/askSydney.js | 26 ++++++++++++++++----------
api/server/routes/convos.js | 9 +++++----
5 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/api/models/Conversation.js b/api/models/Conversation.js
index fbed894712..9c30e1575a 100644
--- a/api/models/Conversation.js
+++ b/api/models/Conversation.js
@@ -85,8 +85,6 @@ module.exports = {
update.promptPrefix = null;
}
- console.error(user)
-
return await Conversation.findOneAndUpdate(
{ conversationId: conversationId, user: user },
{ $set: update },
diff --git a/api/server/routes/ask.js b/api/server/routes/ask.js
index f23d5c7dcc..f016a49b30 100644
--- a/api/server/routes/ask.js
+++ b/api/server/routes/ask.js
@@ -37,7 +37,7 @@ router.post('/', async (req, res) => {
});
await saveMessage(userMessage);
- await saveConvo({ ...userMessage, model, ...convo });
+ await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
return await ask({
userMessage,
@@ -178,7 +178,7 @@ const ask = async ({
await saveMessage(gptResponse);
await saveConvo(req?.session?.user?.username, gptResponse);
sendMessage(res, {
- title: await getConvoTitle(conversationId),
+ title: await getConvoTitle(req?.session?.user?.username, conversationId),
final: true,
requestMessage: userMessage,
responseMessage: gptResponse
@@ -188,10 +188,13 @@ const ask = async ({
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
const title = await titleConvo({ model, text, response: gptResponse });
- await saveConvo({
- conversationId,
- title
- });
+ await saveConvo(
+ req?.session?.user?.username,
+ {
+ conversationId,
+ title
+ }
+ );
}
} catch (error) {
console.log(error);
diff --git a/api/server/routes/askBing.js b/api/server/routes/askBing.js
index dcf9138374..0614faf86a 100644
--- a/api/server/routes/askBing.js
+++ b/api/server/routes/askBing.js
@@ -38,7 +38,7 @@ router.post('/', async (req, res) => {
});
await saveMessage(userMessage);
- await saveConvo({ ...userMessage, model, ...convo });
+ await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
return await ask({
isNewConversation,
@@ -108,10 +108,13 @@ const ask = async ({
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
// but in this situation, don't change the conversationId, but create new convo.
if (conversationId != userMessage.conversationId && isNewConversation)
- await saveConvo({
- conversationId: conversationId,
- newConversationId: userMessage.conversationId
- });
+ await saveConvo(
+ req?.session?.user?.username,
+ {
+ conversationId: conversationId,
+ newConversationId: userMessage.conversationId
+ }
+ );
conversationId = userMessage.conversationId;
response.text = response.response;
@@ -132,7 +135,7 @@ const ask = async ({
await saveConvo(req?.session?.user?.username, { ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
sendMessage(res, {
- title: await getConvoTitle(conversationId),
+ title: await getConvoTitle(req?.session?.user?.username, conversationId),
final: true,
requestMessage: userMessage,
responseMessage: response
@@ -142,10 +145,13 @@ const ask = async ({
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
const title = await titleConvo({ model, text, response });
- await saveConvo({
- conversationId,
- title
- });
+ await saveConvo(
+ req?.session?.user?.username,
+ {
+ conversationId,
+ title
+ }
+ );
}
} catch (error) {
console.log(error);
diff --git a/api/server/routes/askSydney.js b/api/server/routes/askSydney.js
index c2cf02f32e..a2017cf7d6 100644
--- a/api/server/routes/askSydney.js
+++ b/api/server/routes/askSydney.js
@@ -38,7 +38,7 @@ router.post('/', async (req, res) => {
});
await saveMessage(userMessage);
- await saveConvo({ ...userMessage, model, ...convo });
+ await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
return await ask({
isNewConversation,
@@ -132,10 +132,13 @@ const ask = async ({
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
// but in this situation, don't change the conversationId, but create new convo.
if (conversationId != userMessage.conversationId && isNewConversation)
- await saveConvo({
- conversationId: conversationId,
- newConversationId: userMessage.conversationId
- });
+ await saveConvo(
+ req?.session?.user?.username,
+ {
+ conversationId: conversationId,
+ newConversationId: userMessage.conversationId
+ }
+ );
conversationId = userMessage.conversationId;
response.text = await handleText(response, true);
@@ -144,7 +147,7 @@ const ask = async ({
await saveConvo(req?.session?.user?.username, { ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
sendMessage(res, {
- title: await getConvoTitle(conversationId),
+ title: await getConvoTitle(req?.session?.user?.username, conversationId),
final: true,
requestMessage: userMessage,
responseMessage: response
@@ -154,10 +157,13 @@ const ask = async ({
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
const title = await titleConvo({ model, text, response });
- await saveConvo({
- conversationId,
- title
- });
+ await saveConvo(
+ req?.session?.user?.username,
+ {
+ conversationId,
+ title
+ }
+ );
}
} catch (error) {
console.log(error);
diff --git a/api/server/routes/convos.js b/api/server/routes/convos.js
index 11dbb7a545..0364684fad 100644
--- a/api/server/routes/convos.js
+++ b/api/server/routes/convos.js
@@ -18,17 +18,18 @@ router.post('/gen_title', async (req, res) => {
const secondMessage = (await getMessages({ conversationId }))[1]
const title = convo.jailbreakConversationId
- ? await getConvoTitle(conversationId)
+ ? await getConvoTitle(req?.session?.user?.username, conversationId)
: await titleConvo({
model: convo?.model,
message: firstMessage?.text,
response: JSON.stringify(secondMessage?.text || '')
});
- await saveConvo(req?.session?.user?.username,
+ await saveConvo(
+ req?.session?.user?.username,
{
- conversationId,
- title
+ conversationId,
+ title
}
)
From aabb19656eb37b5e59b0cf5e0405ff052e6c2036 Mon Sep 17 00:00:00 2001
From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech>
Date: Thu, 16 Mar 2023 13:30:20 +0800
Subject: [PATCH 03/11] feat: combine customgpt to user
---
api/models/CustomGpt.js | 28 ++++++++++++++++------------
api/server/routes/customGpts.js | 8 ++++----
2 files changed, 20 insertions(+), 16 deletions(-)
diff --git a/api/models/CustomGpt.js b/api/models/CustomGpt.js
index 1f02bdc4db..99379c944d 100644
--- a/api/models/CustomGpt.js
+++ b/api/models/CustomGpt.js
@@ -12,16 +12,20 @@ const customGptSchema = mongoose.Schema({
type: String,
required: true
},
+ user: {
+ type: String
+ },
}, { timestamps: true });
const CustomGpt = mongoose.models.CustomGpt || mongoose.model('CustomGpt', customGptSchema);
-const createCustomGpt = async ({ chatGptLabel, promptPrefix, value }) => {
+const createCustomGpt = async ({ chatGptLabel, promptPrefix, value, user }) => {
try {
await CustomGpt.create({
chatGptLabel,
promptPrefix,
- value
+ value,
+ user
});
return { chatGptLabel, promptPrefix, value };
} catch (error) {
@@ -31,22 +35,22 @@ const createCustomGpt = async ({ chatGptLabel, promptPrefix, value }) => {
};
module.exports = {
- getCustomGpts: async (filter) => {
+ getCustomGpts: async (user, filter) => {
try {
- return await CustomGpt.find(filter).exec();
+ return await CustomGpt.find({ ...filter, user: user }).exec();
} catch (error) {
console.error(error);
return { customGpt: 'Error getting customGpts' };
}
},
- updateCustomGpt: async ({ value, ...update }) => {
+ updateCustomGpt: async (user, { value, ...update }) => {
try {
- const customGpt = await CustomGpt.findOne({ value }).exec();
+ const customGpt = await CustomGpt.findOne({ value, user: user }).exec();
if (!customGpt) {
- return await createCustomGpt({ value, ...update });
+ return await createCustomGpt({ value, ...update, user: user });
} else {
- return await CustomGpt.findOneAndUpdate({ value }, update, {
+ return await CustomGpt.findOneAndUpdate({ value, user: user }, update, {
new: true,
upsert: true
}).exec();
@@ -56,9 +60,9 @@ module.exports = {
return { message: 'Error updating customGpt' };
}
},
- updateByLabel: async ({ prevLabel, ...update }) => {
+ updateByLabel: async (user, { prevLabel, ...update }) => {
try {
- return await CustomGpt.findOneAndUpdate({ chatGptLabel: prevLabel }, update, {
+ return await CustomGpt.findOneAndUpdate({ chatGptLabel: prevLabel, user: user }, update, {
new: true,
upsert: true
}).exec();
@@ -67,9 +71,9 @@ module.exports = {
return { message: 'Error updating customGpt' };
}
},
- deleteCustomGpts: async (filter) => {
+ deleteCustomGpts: async (user, filter) => {
try {
- return await CustomGpt.deleteMany(filter).exec();
+ return await CustomGpt.deleteMany({ ...filter, user: user }).exec();
} catch (error) {
console.error(error);
return { customGpt: 'Error deleting customGpts' };
diff --git a/api/server/routes/customGpts.js b/api/server/routes/customGpts.js
index a58143a8b6..d430b39adb 100644
--- a/api/server/routes/customGpts.js
+++ b/api/server/routes/customGpts.js
@@ -8,7 +8,7 @@ const {
} = require('../../models');
router.get('/', async (req, res) => {
- const models = (await getCustomGpts()).map((model) => {
+ const models = (await getCustomGpts(req?.session?.user?.username)).map((model) => {
model = model.toObject();
model._id = model._id.toString();
return model;
@@ -20,8 +20,8 @@ router.post('/delete', async (req, res) => {
const { arg } = req.body;
try {
- await deleteCustomGpts(arg);
- const models = (await getCustomGpts()).map((model) => {
+ await deleteCustomGpts(req?.session?.user?.username, arg);
+ const models = (await getCustomGpts(req?.session?.user?.username)).map((model) => {
model = model.toObject();
model._id = model._id.toString();
return model;
@@ -56,7 +56,7 @@ router.post('/', async (req, res) => {
}
try {
- const dbResponse = await setter(update);
+ const dbResponse = await setter(req?.session?.user?.username, update);
res.status(201).send(dbResponse);
} catch (error) {
console.error(error);
From a47dbe62620c270d363978655c821c222cf7f9c5 Mon Sep 17 00:00:00 2001
From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech>
Date: Thu, 16 Mar 2023 13:45:46 +0800
Subject: [PATCH 04/11] feat: use same icon style in TextChat
---
client/src/components/Models/ModelMenu.jsx | 5 +++--
client/src/utils/index.js | 2 ++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/client/src/components/Models/ModelMenu.jsx b/client/src/components/Models/ModelMenu.jsx
index 3e3333a7cc..5ef04c3ef4 100644
--- a/client/src/components/Models/ModelMenu.jsx
+++ b/client/src/components/Models/ModelMenu.jsx
@@ -17,6 +17,7 @@ import { setText } from '~/store/textSlice';
import GPTIcon from '../svg/GPTIcon';
import BingIcon from '../svg/BingIcon';
import { Button } from '../ui/Button.tsx';
+import { getIconOfModel } from '../../utils';
import {
DropdownMenu,
@@ -33,7 +34,7 @@ export default function ModelMenu() {
const dispatch = useDispatch();
const [modelSave, setModelSave] = useState(false);
const [menuOpen, setMenuOpen] = useState(false);
- const { model, customModel } = useSelector((state) => state.submit);
+ const { model, customModel, promptPrefix, chatGptLabel } = useSelector((state) => state.submit);
const { models, modelMap, initial } = useSelector((state) => state.models);
const { data, isLoading, mutate } = swr(`/api/customGpts`, (res) => {
const fetchedModels = res.map((modelItem) => ({
@@ -138,7 +139,7 @@ export default function ModelMenu() {
const isBing = model === 'bingai' || model === 'sydney';
const colorProps = model === 'chatgpt' ? chatgptColorProps : defaultColorProps;
- const icon = isBing ?
:
;
+ const icon = getIconOfModel({ sender: chatGptLabel || model, isCreatedByUser: false, model, chatGptLabel, promptPrefix, error: false, className: "mr-2" });
return (