Skip to content

Commit 7bd1877

Browse files
♾️ style: Infinite Scroll Nav and Sort Convos by Date/Usage (danny-avila#1708)
* Style: Infinite Scroll and Group convos by date * Style: Infinite Scroll and Group convos by date- Redesign NavBar * Style: Infinite Scroll and Group convos by date- Redesign NavBar - Clean code * Style: Infinite Scroll and Group convos by date- Redesign NavBar - Redesign NewChat Component * Style: Infinite Scroll and Group convos by date- Redesign NavBar - Redesign NewChat Component * Style: Infinite Scroll and Group convos by date- Redesign NavBar - Redesign NewChat Component * Including OpenRouter and Mistral icon * refactor(Conversations): cleanup use of utility functions and typing * refactor(Nav/NewChat): use localStorage `lastConversationSetup` to determine the endpoint to use, as well as icons -> JSX components, remove use of `endpointSelected` * refactor: remove use of `isFirstToday` * refactor(Nav): remove use of `endpointSelected`, consolidate scrolling logic to its own hook `useNavScrolling`, remove use of recoil `conversation` * refactor: Add spinner to bottom of list, throttle fetching, move query hooks to client workspace * chore: sort by `updatedAt` field * refactor: optimize conversation infinite query, use optimistic updates, add conversation helpers for managing pagination, remove unnecessary operations * feat: gen_title route for generating the title for the conversation * style(Convo): change hover bg-color * refactor: memoize groupedConversations and return as array of tuples, correctly update convos pre/post message stream, only call genTitle if conversation is new, make `addConversation` dynamically either add/update depending if convo exists in pages already, reorganize type definitions * style: rename Header NewChat Button -> HeaderNewChat, add NewChatIcon, closely match main Nav New Chat button to ChatGPT * style(NewChat): add hover bg color * style: cleanup comments, match ChatGPT nav styling, redesign search bar, make part of new chat sticky header, move Nav under same parent as outlet/mobilenav, remove legacy code, search only if searchQuery is not empty * feat: add tests for conversation helpers and ensure no duplicate conversations are ever grouped * style: hover bg-color * feat: alt-click on convo item to open conversation in new tab * chore: send error message when `gen_title` fails --------- Co-authored-by: Walber Cardoso <[email protected]>
1 parent 3c041fa commit 7bd1877

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1788
-391
lines changed

api/cache/getLogStores.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const tokenConfig = isEnabled(USE_REDIS) // ttl: 30 minutes
2727
? new Keyv({ store: keyvRedis, ttl: 1800000 })
2828
: new Keyv({ namespace: CacheKeys.TOKEN_CONFIG, ttl: 1800000 });
2929

30+
const genTitle = isEnabled(USE_REDIS) // ttl: 2 minutes
31+
? new Keyv({ store: keyvRedis, ttl: 120000 })
32+
: new Keyv({ namespace: CacheKeys.GEN_TITLE, ttl: 120000 });
33+
3034
const namespaces = {
3135
[CacheKeys.CONFIG_STORE]: config,
3236
pending_req,
@@ -39,6 +43,7 @@ const namespaces = {
3943
registrations: createViolationInstance('registrations'),
4044
logins: createViolationInstance('logins'),
4145
[CacheKeys.TOKEN_CONFIG]: tokenConfig,
46+
[CacheKeys.GEN_TITLE]: genTitle,
4247
};
4348

4449
/**

api/models/Conversation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ module.exports = {
3030
return { message: 'Error saving conversation' };
3131
}
3232
},
33-
getConvosByPage: async (user, pageNumber = 1, pageSize = 14) => {
33+
getConvosByPage: async (user, pageNumber = 1, pageSize = 25) => {
3434
try {
3535
const totalConvos = (await Conversation.countDocuments({ user })) || 1;
3636
const totalPages = Math.ceil(totalConvos / pageSize);
3737
const convos = await Conversation.find({ user })
38-
.sort({ createdAt: -1 })
38+
.sort({ updatedAt: -1 })
3939
.skip((pageNumber - 1) * pageSize)
4040
.limit(pageSize)
4141
.lean();
@@ -45,7 +45,7 @@ module.exports = {
4545
return { message: 'Error getting conversations' };
4646
}
4747
},
48-
getConvosQueried: async (user, convoIds, pageNumber = 1, pageSize = 14) => {
48+
getConvosQueried: async (user, convoIds, pageNumber = 1, pageSize = 25) => {
4949
try {
5050
if (!convoIds || convoIds.length === 0) {
5151
return { conversations: [], pages: 1, pageNumber, pageSize };

api/server/routes/convos.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const express = require('express');
2+
const { CacheKeys } = require('librechat-data-provider');
23
const { getConvosByPage, deleteConvos } = require('~/models/Conversation');
34
const requireJwtAuth = require('~/server/middleware/requireJwtAuth');
5+
const { sleep } = require('~/server/services/AssistantService');
6+
const getLogStores = require('~/cache/getLogStores');
47
const { getConvo, saveConvo } = require('~/models');
58
const { logger } = require('~/config');
69

@@ -29,6 +32,29 @@ router.get('/:conversationId', async (req, res) => {
2932
}
3033
});
3134

35+
router.post('/gen_title', async (req, res) => {
36+
const { conversationId } = req.body;
37+
const titleCache = getLogStores(CacheKeys.GEN_TITLE);
38+
const key = `${req.user.id}-${conversationId}`;
39+
let title = await titleCache.get(key);
40+
41+
if (!title) {
42+
await sleep(2500);
43+
title = await titleCache.get(key);
44+
}
45+
46+
if (title) {
47+
await titleCache.delete(key);
48+
res.status(200).json({ title });
49+
} else {
50+
res
51+
.status(404)
52+
.json({
53+
message: 'Title not found or method not implemented for the conversation\'s endpoint',
54+
});
55+
}
56+
});
57+
3258
router.post('/clear', async (req, res) => {
3359
let filter = {};
3460
const { conversationId, source } = req.body.arg;

api/server/services/AssistantService.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,6 @@ module.exports = {
357357
waitForRun,
358358
getResponse,
359359
handleRun,
360+
sleep,
360361
mapMessagesToSteps,
361362
};

api/server/services/Endpoints/openAI/addTitle.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const { saveConvo } = require('~/models');
1+
const { CacheKeys } = require('librechat-data-provider');
2+
const getLogStores = require('~/cache/getLogStores');
23
const { isEnabled } = require('~/server/utils');
4+
const { saveConvo } = require('~/models');
35

46
const addTitle = async (req, { text, response, client }) => {
57
const { TITLE_CONVO = 'true' } = process.env ?? {};
@@ -16,7 +18,11 @@ const addTitle = async (req, { text, response, client }) => {
1618
return;
1719
}
1820

21+
const titleCache = getLogStores(CacheKeys.GEN_TITLE);
22+
const key = `${req.user.id}-${response.conversationId}`;
23+
1924
const title = await client.titleConvo({ text, responseText: response?.text });
25+
await titleCache.set(key, title);
2026
await saveConvo(req.user.id, {
2127
conversationId: response.conversationId,
2228
title,

client/src/components/Chat/Header.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useOutletContext } from 'react-router-dom';
22
import type { ContextType } from '~/common';
3-
import { EndpointsMenu, PresetsMenu, NewChat } from './Menus';
3+
import { EndpointsMenu, PresetsMenu, HeaderNewChat } from './Menus';
44
import HeaderOptions from './Input/HeaderOptions';
55

66
export default function Header() {
77
const { navVisible } = useOutletContext<ContextType>();
88
return (
99
<div className="sticky top-0 z-10 flex h-14 w-full items-center justify-between bg-white/95 p-2 font-semibold dark:bg-gray-800/90 dark:text-white ">
1010
<div className="hide-scrollbar flex items-center gap-2 overflow-x-auto">
11-
{!navVisible && <NewChat />}
11+
{!navVisible && <HeaderNewChat />}
1212
<EndpointsMenu />
1313
<HeaderOptions />
1414
<PresetsMenu />

client/src/components/Chat/Landing.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import { getEndpointField } from '~/utils';
77
import { useLocalize } from '~/hooks';
88

99
export default function Landing({ Header }: { Header?: ReactNode }) {
10-
const { data: endpointsConfig } = useGetEndpointsQuery();
1110
const { conversation } = useChatContext();
11+
const { data: endpointsConfig } = useGetEndpointsQuery();
12+
1213
const localize = useLocalize();
1314
let { endpoint } = conversation ?? {};
1415
if (
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NewChatIcon } from '~/components/svg';
2+
import { useChatContext } from '~/Providers';
3+
import { useMediaQuery } from '~/hooks';
4+
5+
export default function HeaderNewChat() {
6+
const { newConversation } = useChatContext();
7+
const isSmallScreen = useMediaQuery('(max-width: 768px)');
8+
if (isSmallScreen) {
9+
return null;
10+
}
11+
return (
12+
<button
13+
data-testid="wide-header-new-chat-button"
14+
type="button"
15+
className="btn btn-neutral btn-small border-token-border-medium relative ml-2 flex hidden h-9 w-9 items-center justify-center whitespace-nowrap rounded-lg rounded-lg border focus:ring-0 focus:ring-offset-0 md:flex"
16+
onClick={() => newConversation()}
17+
>
18+
<div className="flex w-full items-center justify-center gap-2">
19+
<NewChatIcon />
20+
</div>
21+
</button>
22+
);
23+
}

client/src/components/Chat/Menus/NewChat.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { default as EndpointsMenu } from './EndpointsMenu';
22
export { default as PresetsMenu } from './PresetsMenu';
3-
export { default as NewChat } from './NewChat';
3+
export { default as HeaderNewChat } from './HeaderNewChat';

0 commit comments

Comments
 (0)