Skip to content

Commit 0e5696d

Browse files
committed
refac
1 parent 70c87a1 commit 0e5696d

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

backend/open_webui/models/chats.py

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def get_chat_list_by_user_id(
796796
skip: int = 0,
797797
limit: int = 50,
798798
db: Optional[Session] = None,
799-
) -> list[ChatModel]:
799+
) -> list[ChatTitleIdResponse]:
800800
with get_db_context(db) as db:
801801
query = db.query(Chat).filter_by(user_id=user_id)
802802
if not include_archived:
@@ -820,13 +820,28 @@ def get_chat_list_by_user_id(
820820
else:
821821
query = query.order_by(Chat.updated_at.desc(), Chat.id)
822822

823+
query = query.with_entities(
824+
Chat.id, Chat.title, Chat.updated_at, Chat.created_at, Chat.last_read_at
825+
)
826+
823827
if skip:
824828
query = query.offset(skip)
825829
if limit:
826830
query = query.limit(limit)
827831

828832
all_chats = query.all()
829-
return [ChatModel.model_validate(chat) for chat in all_chats]
833+
return [
834+
ChatTitleIdResponse.model_validate(
835+
{
836+
'id': chat[0],
837+
'title': chat[1],
838+
'updated_at': chat[2],
839+
'created_at': chat[3],
840+
'last_read_at': chat[4],
841+
}
842+
)
843+
for chat in all_chats
844+
]
830845

831846
def get_chat_title_id_list_by_user_id(
832847
self,
@@ -1233,21 +1248,36 @@ def get_chats_by_folder_id_and_user_id(
12331248
skip: int = 0,
12341249
limit: int = 60,
12351250
db: Optional[Session] = None,
1236-
) -> list[ChatModel]:
1251+
) -> list[ChatTitleIdResponse]:
12371252
with get_db_context(db) as db:
12381253
query = db.query(Chat).filter_by(folder_id=folder_id, user_id=user_id)
12391254
query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
12401255
query = query.filter_by(archived=False)
12411256

12421257
query = query.order_by(Chat.updated_at.desc(), Chat.id)
12431258

1259+
query = query.with_entities(
1260+
Chat.id, Chat.title, Chat.updated_at, Chat.created_at, Chat.last_read_at
1261+
)
1262+
12441263
if skip:
12451264
query = query.offset(skip)
12461265
if limit:
12471266
query = query.limit(limit)
12481267

12491268
all_chats = query.all()
1250-
return [ChatModel.model_validate(chat) for chat in all_chats]
1269+
return [
1270+
ChatTitleIdResponse.model_validate(
1271+
{
1272+
'id': chat[0],
1273+
'title': chat[1],
1274+
'updated_at': chat[2],
1275+
'created_at': chat[3],
1276+
'last_read_at': chat[4],
1277+
}
1278+
)
1279+
for chat in all_chats
1280+
]
12511281

12521282
def get_chats_by_folder_ids_and_user_id(
12531283
self, folder_ids: list[str], user_id: str, db: Optional[Session] = None
@@ -1290,7 +1320,7 @@ def get_chat_list_by_user_id_and_tag_name(
12901320
skip: int = 0,
12911321
limit: int = 50,
12921322
db: Optional[Session] = None,
1293-
) -> list[ChatModel]:
1323+
) -> list[ChatTitleIdResponse]:
12941324
with get_db_context(db) as db:
12951325
query = db.query(Chat).filter_by(user_id=user_id)
12961326
tag_id = tag_name.replace(' ', '_').lower()
@@ -1309,9 +1339,30 @@ def get_chat_list_by_user_id_and_tag_name(
13091339
else:
13101340
raise NotImplementedError(f'Unsupported dialect: {db.bind.dialect.name}')
13111341

1342+
query = query.order_by(Chat.updated_at.desc(), Chat.id)
1343+
1344+
query = query.with_entities(
1345+
Chat.id, Chat.title, Chat.updated_at, Chat.created_at, Chat.last_read_at
1346+
)
1347+
1348+
if skip:
1349+
query = query.offset(skip)
1350+
if limit:
1351+
query = query.limit(limit)
1352+
13121353
all_chats = query.all()
1313-
log.debug(f'all_chats: {all_chats}')
1314-
return [ChatModel.model_validate(chat) for chat in all_chats]
1354+
return [
1355+
ChatTitleIdResponse.model_validate(
1356+
{
1357+
'id': chat[0],
1358+
'title': chat[1],
1359+
'updated_at': chat[2],
1360+
'created_at': chat[3],
1361+
'last_read_at': chat[4],
1362+
}
1363+
)
1364+
for chat in all_chats
1365+
]
13151366

13161367
def add_chat_tag_by_id_and_user_id_and_tag_name(
13171368
self, id: str, user_id: str, tag_name: str, db: Optional[Session] = None

backend/open_webui/routers/chats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,10 @@ async def get_chat_list_by_folder_id(
643643
limit = 10
644644
skip = (page - 1) * limit
645645

646+
chats = Chats.get_chats_by_folder_id_and_user_id(folder_id, user.id, skip=skip, limit=limit, db=db)
646647
return [
647648
{'title': chat.title, 'id': chat.id, 'updated_at': chat.updated_at, 'last_read_at': chat.last_read_at}
648-
for chat in Chats.get_chats_by_folder_id_and_user_id(folder_id, user.id, skip=skip, limit=limit, db=db)
649+
for chat in chats
649650
]
650651

651652
except Exception as e:

0 commit comments

Comments
 (0)