Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
003ceff7
Unverified
Commit
003ceff7
authored
Aug 04, 2024
by
Timothy Jaeryang Baek
Committed by
GitHub
Aug 04, 2024
Browse files
Merge pull request #4349 from open-webui/dev-scroll
feat: Sidebar infinite scroll (pagination)
parents
1871a7a2
209ccdf6
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
176 additions
and
39 deletions
+176
-39
backend/apps/webui/models/chats.py
backend/apps/webui/models/chats.py
+5
-4
backend/apps/webui/routers/chats.py
backend/apps/webui/routers/chats.py
+8
-2
src/lib/apis/chats/index.ts
src/lib/apis/chats/index.ts
+7
-2
src/lib/components/chat/Chat.svelte
src/lib/components/chat/Chat.svelte
+23
-8
src/lib/components/chat/Messages.svelte
src/lib/components/chat/Messages.svelte
+3
-2
src/lib/components/chat/Settings/Chats.svelte
src/lib/components/chat/Settings/Chats.svelte
+12
-4
src/lib/components/chat/Tags.svelte
src/lib/components/chat/Tags.svelte
+11
-6
src/lib/components/common/Loader.svelte
src/lib/components/common/Loader.svelte
+30
-0
src/lib/components/layout/Sidebar.svelte
src/lib/components/layout/Sidebar.svelte
+64
-7
src/lib/components/layout/Sidebar/ChatItem.svelte
src/lib/components/layout/Sidebar/ChatItem.svelte
+10
-4
src/lib/stores/index.ts
src/lib/stores/index.ts
+3
-0
No files found.
backend/apps/webui/models/chats.py
View file @
003ceff7
...
@@ -250,7 +250,7 @@ class ChatTable:
...
@@ -250,7 +250,7 @@ class ChatTable:
user_id
:
str
,
user_id
:
str
,
include_archived
:
bool
=
False
,
include_archived
:
bool
=
False
,
skip
:
int
=
0
,
skip
:
int
=
0
,
limit
:
int
=
50
,
limit
:
int
=
-
1
,
)
->
List
[
ChatTitleIdResponse
]:
)
->
List
[
ChatTitleIdResponse
]:
with
get_db
()
as
db
:
with
get_db
()
as
db
:
query
=
db
.
query
(
Chat
).
filter_by
(
user_id
=
user_id
)
query
=
db
.
query
(
Chat
).
filter_by
(
user_id
=
user_id
)
...
@@ -260,9 +260,10 @@ class ChatTable:
...
@@ -260,9 +260,10 @@ class ChatTable:
all_chats
=
(
all_chats
=
(
query
.
order_by
(
Chat
.
updated_at
.
desc
())
query
.
order_by
(
Chat
.
updated_at
.
desc
())
# limit cols
# limit cols
.
with_entities
(
.
with_entities
(
Chat
.
id
,
Chat
.
title
,
Chat
.
updated_at
,
Chat
.
created_at
)
Chat
.
id
,
Chat
.
title
,
Chat
.
updated_at
,
Chat
.
created_at
.
limit
(
limit
)
).
all
()
.
offset
(
skip
)
.
all
()
)
)
# result has to be destrctured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass.
# result has to be destrctured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass.
return
[
return
[
...
...
backend/apps/webui/routers/chats.py
View file @
003ceff7
...
@@ -43,9 +43,15 @@ router = APIRouter()
...
@@ -43,9 +43,15 @@ router = APIRouter()
@
router
.
get
(
"/"
,
response_model
=
List
[
ChatTitleIdResponse
])
@
router
.
get
(
"/"
,
response_model
=
List
[
ChatTitleIdResponse
])
@
router
.
get
(
"/list"
,
response_model
=
List
[
ChatTitleIdResponse
])
@
router
.
get
(
"/list"
,
response_model
=
List
[
ChatTitleIdResponse
])
async
def
get_session_user_chat_list
(
async
def
get_session_user_chat_list
(
user
=
Depends
(
get_verified_user
),
skip
:
int
=
0
,
limit
:
int
=
50
user
=
Depends
(
get_verified_user
),
page
:
Optional
[
int
]
=
None
):
):
if
page
is
not
None
:
limit
=
60
skip
=
(
page
-
1
)
*
limit
return
Chats
.
get_chat_title_id_list_by_user_id
(
user
.
id
,
skip
=
skip
,
limit
=
limit
)
return
Chats
.
get_chat_title_id_list_by_user_id
(
user
.
id
,
skip
=
skip
,
limit
=
limit
)
else
:
return
Chats
.
get_chat_title_id_list_by_user_id
(
user
.
id
)
############################
############################
...
...
src/lib/apis/chats/index.ts
View file @
003ceff7
...
@@ -32,10 +32,15 @@ export const createNewChat = async (token: string, chat: object) => {
...
@@ -32,10 +32,15 @@ export const createNewChat = async (token: string, chat: object) => {
return
res
;
return
res
;
};
};
export
const
getChatList
=
async
(
token
:
string
=
''
)
=>
{
export
const
getChatList
=
async
(
token
:
string
=
''
,
page
:
number
|
null
=
null
)
=>
{
let
error
=
null
;
let
error
=
null
;
const
searchParams
=
new
URLSearchParams
();
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/chats/`
,
{
if
(
page
!==
null
)
{
searchParams
.
append
(
'
page
'
,
`
${
page
}
`
);
}
const
res
=
await
fetch
(
`
${
WEBUI_API_BASE_URL
}
/chats/?
${
searchParams
.
toString
()}
`
,
{
method
:
'
GET
'
,
method
:
'
GET
'
,
headers
:
{
headers
:
{
Accept
:
'
application/json
'
,
Accept
:
'
application/json
'
,
...
...
src/lib/components/chat/Chat.svelte
View file @
003ceff7
...
@@ -25,7 +25,8 @@
...
@@ -25,7 +25,8 @@
user,
user,
socket,
socket,
showCallOverlay,
showCallOverlay,
tools
tools,
currentChatPage
} from '$lib/stores';
} from '$lib/stores';
import {
import {
convertMessagesToHistory,
convertMessagesToHistory,
...
@@ -421,7 +422,9 @@
...
@@ -421,7 +422,9 @@
params: params,
params: params,
files: chatFiles
files: chatFiles
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}
}
}
}
};
};
...
@@ -467,7 +470,9 @@
...
@@ -467,7 +470,9 @@
params: params,
params: params,
files: chatFiles
files: chatFiles
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}
}
}
}
};
};
...
@@ -627,7 +632,9 @@
...
@@ -627,7 +632,9 @@
tags: [],
tags: [],
timestamp: Date.now()
timestamp: Date.now()
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await chatId.set(chat.id);
await chatId.set(chat.id);
} else {
} else {
await chatId.set('local');
await chatId.set('local');
...
@@ -703,7 +710,9 @@
...
@@ -703,7 +710,9 @@
})
})
);
);
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
return _responses;
return _responses;
};
};
...
@@ -949,7 +958,9 @@
...
@@ -949,7 +958,9 @@
params: params,
params: params,
files: chatFiles
files: chatFiles
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}
}
}
}
} else {
} else {
...
@@ -1216,7 +1227,9 @@
...
@@ -1216,7 +1227,9 @@
params: params,
params: params,
files: chatFiles
files: chatFiles
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}
}
}
}
} else {
} else {
...
@@ -1381,7 +1394,9 @@
...
@@ -1381,7 +1394,9 @@
if ($settings.saveChatHistory ?? true) {
if ($settings.saveChatHistory ?? true) {
chat = await updateChatById(localStorage.token, _chatId, { title: _title });
chat = await updateChatById(localStorage.token, _chatId, { title: _title });
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}
}
};
};
...
...
src/lib/components/chat/Messages.svelte
View file @
003ceff7
<script lang="ts">
<script lang="ts">
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4 } from 'uuid';
import { chats, config, settings, user as _user, mobile } from '$lib/stores';
import { chats, config, settings, user as _user, mobile
, currentChatPage
} from '$lib/stores';
import { tick, getContext, onMount } from 'svelte';
import { tick, getContext, onMount } from 'svelte';
import { toast } from 'svelte-sonner';
import { toast } from 'svelte-sonner';
...
@@ -90,7 +90,8 @@
...
@@ -90,7 +90,8 @@
history: history
history: history
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
};
};
const confirmEditResponseMessage = async (messageId, content) => {
const confirmEditResponseMessage = async (messageId, content) => {
...
...
src/lib/components/chat/Settings/Chats.svelte
View file @
003ceff7
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import fileSaver from 'file-saver';
import fileSaver from 'file-saver';
const { saveAs } = fileSaver;
const { saveAs } = fileSaver;
import { chats, user, settings } from '$lib/stores';
import { chats, user, settings
, scrollPaginationEnabled, currentChatPage
} from '$lib/stores';
import {
import {
archiveAllChats,
archiveAllChats,
...
@@ -62,7 +62,9 @@
...
@@ -62,7 +62,9 @@
}
}
}
}
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
};
};
const exportChats = async () => {
const exportChats = async () => {
...
@@ -77,7 +79,10 @@
...
@@ -77,7 +79,10 @@
await archiveAllChats(localStorage.token).catch((error) => {
await archiveAllChats(localStorage.token).catch((error) => {
toast.error(error);
toast.error(error);
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
};
};
const deleteAllChatsHandler = async () => {
const deleteAllChatsHandler = async () => {
...
@@ -85,7 +90,10 @@
...
@@ -85,7 +90,10 @@
await deleteAllChats(localStorage.token).catch((error) => {
await deleteAllChats(localStorage.token).catch((error) => {
toast.error(error);
toast.error(error);
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
};
};
const toggleSaveChatHistory = async () => {
const toggleSaveChatHistory = async () => {
...
...
src/lib/components/chat/Tags.svelte
View file @
003ceff7
...
@@ -8,7 +8,13 @@
...
@@ -8,7 +8,13 @@
getTagsById,
getTagsById,
updateChatById
updateChatById
} from '$lib/apis/chats';
} from '$lib/apis/chats';
import { tags as _tags, chats, pinnedChats } from '$lib/stores';
import {
tags as _tags,
chats,
pinnedChats,
currentChatPage,
scrollPaginationEnabled
} from '$lib/stores';
import { createEventDispatcher, onMount } from 'svelte';
import { createEventDispatcher, onMount } from 'svelte';
const dispatch = createEventDispatcher();
const dispatch = createEventDispatcher();
...
@@ -46,11 +52,7 @@
...
@@ -46,11 +52,7 @@
tags: tags
tags: tags
});
});
console.log($_tags);
await _tags.set(await getAllChatTags(localStorage.token));
await _tags.set(await getAllChatTags(localStorage.token));
console.log($_tags);
if ($_tags.map((t) => t.name).includes(tagName)) {
if ($_tags.map((t) => t.name).includes(tagName)) {
if (tagName === 'pinned') {
if (tagName === 'pinned') {
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
...
@@ -62,8 +64,11 @@
...
@@ -62,8 +64,11 @@
dispatch('close');
dispatch('close');
}
}
} else {
} else {
await chats.set(await getChatList(localStorage.token));
// if the tag we deleted is no longer a valid tag, return to main chat list view
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await scrollPaginationEnabled.set(true);
}
}
};
};
...
...
src/lib/components/common/Loader.svelte
0 → 100644
View file @
003ceff7
<script lang="ts">
import { createEventDispatcher, onMount } from 'svelte';
const dispatch = createEventDispatcher();
let loaderElement: HTMLElement;
onMount(() => {
const observer = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
dispatch('visible');
// observer.unobserve(loaderElement); // Stop observing until content is loaded
}
});
},
{
root: null, // viewport
rootMargin: '0px',
threshold: 1.0 // When 100% of the loader is visible
}
);
observer.observe(loaderElement);
});
</script>
<div bind:this={loaderElement}>
<slot />
</div>
src/lib/components/layout/Sidebar.svelte
View file @
003ceff7
...
@@ -11,7 +11,9 @@
...
@@ -11,7 +11,9 @@
showSidebar,
showSidebar,
mobile,
mobile,
showArchivedChats,
showArchivedChats,
pinnedChats
pinnedChats,
scrollPaginationEnabled,
currentChatPage
} from '$lib/stores';
} from '$lib/stores';
import { onMount, getContext, tick } from 'svelte';
import { onMount, getContext, tick } from 'svelte';
...
@@ -34,6 +36,8 @@
...
@@ -34,6 +36,8 @@
import UserMenu from './Sidebar/UserMenu.svelte';
import UserMenu from './Sidebar/UserMenu.svelte';
import ChatItem from './Sidebar/ChatItem.svelte';
import ChatItem from './Sidebar/ChatItem.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import Spinner from '../common/Spinner.svelte';
import Loader from '../common/Loader.svelte';
const BREAKPOINT = 768;
const BREAKPOINT = 768;
...
@@ -50,6 +54,10 @@
...
@@ -50,6 +54,10 @@
let filteredChatList = [];
let filteredChatList = [];
// Pagination variables
let chatListLoading = false;
let allChatsLoaded = false;
$: filteredChatList = $chats.filter((chat) => {
$: filteredChatList = $chats.filter((chat) => {
if (search === '') {
if (search === '') {
return true;
return true;
...
@@ -70,6 +78,29 @@
...
@@ -70,6 +78,29 @@
}
}
});
});
const enablePagination = async () => {
// Reset pagination variables
currentChatPage.set(1);
allChatsLoaded = false;
await chats.set(await getChatList(localStorage.token, $currentChatPage));
// Enable pagination
scrollPaginationEnabled.set(true);
};
const loadMoreChats = async () => {
chatListLoading = true;
currentChatPage.set($currentChatPage + 1);
const newChatList = await getChatList(localStorage.token, $currentChatPage);
// once the bottom of the list has been reached (no results) there is no need to continue querying
allChatsLoaded = newChatList.length === 0;
await chats.set([...$chats, ...newChatList]);
chatListLoading = false;
};
onMount(async () => {
onMount(async () => {
mobile.subscribe((e) => {
mobile.subscribe((e) => {
if ($showSidebar && e) {
if ($showSidebar && e) {
...
@@ -82,9 +113,8 @@
...
@@ -82,9 +113,8 @@
});
});
showSidebar.set(window.innerWidth > BREAKPOINT);
showSidebar.set(window.innerWidth > BREAKPOINT);
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await
chats.set(await getChatList(localStorage.token)
);
await
enablePagination(
);
let touchstart;
let touchstart;
let touchend;
let touchend;
...
@@ -185,7 +215,11 @@
...
@@ -185,7 +215,11 @@
await tick();
await tick();
goto('/');
goto('/');
}
}
await chats.set(await getChatList(localStorage.token));
allChatsLoaded = false;
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
}
}
};
};
...
@@ -410,7 +444,10 @@
...
@@ -410,7 +444,10 @@
class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-none"
class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-none"
placeholder={$i18n.t('Search')}
placeholder={$i18n.t('Search')}
bind:value={search}
bind:value={search}
on:focus={() => {
on:focus={async () => {
// TODO: migrate backend for more scalable search mechanism
scrollPaginationEnabled.set(false);
await chats.set(await getChatList(localStorage.token)); // when searching, load all chats
enrichChatsWithContent($chats);
enrichChatsWithContent($chats);
}}
}}
/>
/>
...
@@ -422,7 +459,7 @@
...
@@ -422,7 +459,7 @@
<button
<button
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
on:click={async () => {
on:click={async () => {
await
chats.set(await getChatList(localStorage.token)
);
await
enablePagination(
);
}}
}}
>
>
{$i18n.t('all')}
{$i18n.t('all')}
...
@@ -431,12 +468,17 @@
...
@@ -431,12 +468,17 @@
<button
<button
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
on:click={async () => {
on:click={async () => {
scrollPaginationEnabled.set(false);
let chatIds = await getChatListByTagName(localStorage.token, tag.name);
let chatIds = await getChatListByTagName(localStorage.token, tag.name);
if (chatIds.length === 0) {
if (chatIds.length === 0) {
await tags.set(await getAllChatTags(localStorage.token));
await tags.set(await getAllChatTags(localStorage.token));
chatIds = await getChatList(localStorage.token);
// if the tag we deleted is no longer a valid tag, return to main chat list view
await enablePagination();
}
}
await chats.set(chatIds);
await chats.set(chatIds);
chatListLoading = false;
}}
}}
>
>
{tag.name}
{tag.name}
...
@@ -527,6 +569,21 @@
...
@@ -527,6 +569,21 @@
}}
}}
/>
/>
{/each}
{/each}
{#if $scrollPaginationEnabled && !allChatsLoaded}
<Loader
on:visible={(e) => {
if (!chatListLoading) {
loadMoreChats();
}
}}
>
<div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
<Spinner className=" size-4" />
<div class=" ">Loading...</div>
</div>
</Loader>
{/if}
</div>
</div>
</div>
</div>
...
...
src/lib/components/layout/Sidebar/ChatItem.svelte
View file @
003ceff7
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
getChatListByTagName,
getChatListByTagName,
updateChatById
updateChatById
} from '$lib/apis/chats';
} from '$lib/apis/chats';
import { chatId, chats, mobile, pinnedChats, showSidebar } from '$lib/stores';
import { chatId, chats, mobile, pinnedChats, showSidebar
, currentChatPage
} from '$lib/stores';
import ChatMenu from './ChatMenu.svelte';
import ChatMenu from './ChatMenu.svelte';
import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
...
@@ -40,7 +40,9 @@
...
@@ -40,7 +40,9 @@
await updateChatById(localStorage.token, id, {
await updateChatById(localStorage.token, id, {
title: _title
title: _title
});
});
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
}
}
};
};
...
@@ -53,14 +55,18 @@
...
@@ -53,14 +55,18 @@
if (res) {
if (res) {
goto(`/c/${res.id}`);
goto(`/c/${res.id}`);
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
}
}
};
};
const archiveChatHandler = async (id) => {
const archiveChatHandler = async (id) => {
await archiveChatById(localStorage.token, id);
await archiveChatById(localStorage.token, id);
await chats.set(await getChatList(localStorage.token));
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
};
};
...
...
src/lib/stores/index.ts
View file @
003ceff7
...
@@ -42,6 +42,9 @@ export const showArchivedChats = writable(false);
...
@@ -42,6 +42,9 @@ export const showArchivedChats = writable(false);
export
const
showChangelog
=
writable
(
false
);
export
const
showChangelog
=
writable
(
false
);
export
const
showCallOverlay
=
writable
(
false
);
export
const
showCallOverlay
=
writable
(
false
);
export
const
scrollPaginationEnabled
=
writable
(
false
);
export
const
currentChatPage
=
writable
(
1
);
export
type
Model
=
OpenAIModel
|
OllamaModel
;
export
type
Model
=
OpenAIModel
|
OllamaModel
;
type
BaseModel
=
{
type
BaseModel
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment