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

feat: search in progress

Daniel Avila6d2f3361d085
diff --git a/api/lib/utils/mergeSort.js b/api/lib/utils/mergeSort.js
new file mode 100644
index 0000000000..660d445664
--- /dev/null
+++ b/api/lib/utils/mergeSort.js
@@ -0,0 +1,29 @@
+function mergeSort(arr, compareFn) {
+ if (arr.length <= 1) {
+ return arr;
+ }
+
+ const mid = Math.floor(arr.length / 2);
+ const leftArr = arr.slice(0, mid);
+ const rightArr = arr.slice(mid);
+
+ return merge(mergeSort(leftArr, compareFn), mergeSort(rightArr, compareFn), compareFn);
+}
+
+function merge(leftArr, rightArr, compareFn) {
+ const result = [];
+ let leftIndex = 0;
+ let rightIndex = 0;
+
+ while (leftIndex < leftArr.length && rightIndex < rightArr.length) {
+ if (compareFn(leftArr[leftIndex], rightArr[rightIndex]) < 0) {
+ result.push(leftArr[leftIndex++]);
+ } else {
+ result.push(rightArr[rightIndex++]);
+ }
+ }
+
+ return result.concat(leftArr.slice(leftIndex)).concat(rightArr.slice(rightIndex));
+}
+
+module.exports = mergeSort;
\ No newline at end of file
diff --git a/api/lib/utils/reduceHits.js b/api/lib/utils/reduceHits.js
new file mode 100644
index 0000000000..ef94af3f5c
--- /dev/null
+++ b/api/lib/utils/reduceHits.js
@@ -0,0 +1,26 @@
+const mergeSort = require('./mergeSort');
+
+function reduceHits(hits) {
+ const counts = {};
+
+ for (const hit of hits) {
+ if (!counts[hit.conversationId]) {
+ counts[hit.conversationId] = 1;
+ } else {
+ counts[hit.conversationId]++;
+ }
+ }
+
+ const result = [];
+
+ for (const [conversationId, count] of Object.entries(counts)) {
+ result.push({
+ conversationId,
+ count
+ });
+ }
+
+ return mergeSort(result, (a, b) => b.count - a.count);
+}
+
+module.exports = reduceHits;
\ No newline at end of file
diff --git a/api/server/routes/search.js b/api/server/routes/search.js
index 65d85dae5a..9336cf2370 100644
--- a/api/server/routes/search.js
+++ b/api/server/routes/search.js
@@ -1,6 +1,7 @@
const express = require('express');
const router = express.Router();
const { Message } = require('../../models/Message');
+const reduceHits = require('../../lib/utils/reduceHits');
// const { MeiliSearch } = require('meilisearch');
router.get('/sync', async function (req, res) {
@@ -15,8 +16,14 @@ router.get('/sync', async function (req, res) {
router.get('/', async function (req, res) {
const { q } = req.query;
const result = await Message.meiliSearch({ query: q });
- console.log(result);
- res.send(result);
+ const sortedHits = reduceHits(result.hits);
+ console.log(sortedHits);
+ res.send(sortedHits);
+});
+
+router.get('/clear', async function (req, res) {
+ await Message.resetIndex();
+ res.send('cleared');
});
module.exports = router;
diff --git a/client/src/App.jsx b/client/src/App.jsx
index 78943710e5..0c54001bfe 100644
--- a/client/src/App.jsx
+++ b/client/src/App.jsx
@@ -15,29 +15,27 @@ const App = () => {
const { messages, messageTree } = useSelector((state) => state.messages);
const { user } = useSelector((state) => state.user);
const { title } = useSelector((state) => state.convo);
- const { conversationId } = useSelector((state) => state.convo);
const [ navVisible, setNavVisible ]= useState(false)
useDocumentTitle(title);
- 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";
+ useEffect(async () => {
+ try {
+ const response = await axios.get('/api/me', {
+ timeout: 1000,
+ withCredentials: true
+ });
+ const user = response.data;
+ 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
+ } catch (error) {
+ console.error(error);
+ console.log('Not login!');
+ window.location.href = '/auth/login';
+ }
}, [])
if (user)
diff --git a/client/src/components/Nav/index.jsx b/client/src/components/Nav/index.jsx
index 9c02a5e1ea..c58db30a8c 100644
--- a/client/src/components/Nav/index.jsx
+++ b/client/src/components/Nav/index.jsx
@@ -3,7 +3,6 @@ import NewChat from './NewChat';
import Spinner from '../svg/Spinner';
import Conversations from '../Conversations';
import NavLinks from './NavLinks';
-import useDidMountEffect from '~/hooks/useDidMountEffect';
import { swr } from '~/utils/fetchers';
import { useDispatch, useSelector } from 'react-redux';
import { increasePage, decreasePage, setPage, setConvos, setPages } from '~/store/convoSlice';
@@ -11,7 +10,7 @@ import { increasePage, decreasePage, setPage, setConvos, setPages } from '~/stor
export default function Nav({ navVisible, setNavVisible }) {
const dispatch = useDispatch();
const [isHovering, setIsHovering] = useState(false);
- const { conversationId, convos, pages, pageNumber, refreshConvoHint } = useSelector(
+ const { conversationId, convos, search, pages, pageNumber, refreshConvoHint } = useSelector(
(state) => state.convo
);
const onSuccess = (data) => {
@@ -25,7 +24,7 @@ export default function Nav({ navVisible, setNavVisible }) {
}
};
- const { data, isLoading, mutate } = swr(`/api/convos?pageNumber=${pageNumber}`, onSuccess, {
+ const { data, isLoading, mutate } = swr(`/api/${search ? 'search?q=' : `convos?pageNumber=${pageNumber}`}`, onSuccess, {
revalidateOnMount: false
});
diff --git a/client/src/store/convoSlice.js b/client/src/store/convoSlice.js
index c862a8e1e7..76e1d34a33 100644
--- a/client/src/store/convoSlice.js
+++ b/client/src/store/convoSlice.js
@@ -15,6 +15,7 @@ const initialState = {
pageNumber: 1,
pages: 1,
refreshConvoHint: 0,
+ search: false,
convos: [],
};