The GET /chats/shared endpoint was loading full Chat rows including the entire conversation history JSON blob, only to discard it and return SharedChatResponse (id, title, share_id, timestamps). Now uses with_entities() to select only the 5 needed columns, avoiding deserialization of potentially large chat JSON for every shared chat.
This commit is contained in:
1 parent
27c76c677a
commit
35763a352c
2 files changed
+25
-14
No files matched your search
@@ -754,7 +754,7 @@ class ChatTable:
|
|||||||
skip: int = 0,
|
skip: int = 0,
|
||||||
limit: int = 50,
|
limit: int = 50,
|
||||||
db: Optional[Session] = None,
|
db: Optional[Session] = None,
|
||||||
) -> list[ChatModel]:
|
) -> list[SharedChatResponse]:
|
||||||
|
|
||||||
with get_db_context(db) as db:
|
with get_db_context(db) as db:
|
||||||
query = (
|
query = (
|
||||||
@@ -784,13 +784,29 @@ class ChatTable:
|
|||||||
else:
|
else:
|
||||||
query = query.order_by(Chat.updated_at.desc())
|
query = query.order_by(Chat.updated_at.desc())
|
||||||
|
|
||||||
|
# Select only the columns needed for SharedChatResponse
|
||||||
|
# to avoid loading the heavy chat JSON blob
|
||||||
|
query = query.with_entities(
|
||||||
|
Chat.id, Chat.title, Chat.share_id,
|
||||||
|
Chat.updated_at, Chat.created_at,
|
||||||
|
)
|
||||||
|
|
||||||
if skip:
|
if skip:
|
||||||
query = query.offset(skip)
|
query = query.offset(skip)
|
||||||
if limit:
|
if limit:
|
||||||
query = query.limit(limit)
|
query = query.limit(limit)
|
||||||
|
|
||||||
all_chats = query.all()
|
all_chats = query.all()
|
||||||
return [ChatModel.model_validate(chat) for chat in all_chats]
|
return [
|
||||||
|
SharedChatResponse(
|
||||||
|
id=chat[0],
|
||||||
|
title=chat[1],
|
||||||
|
share_id=chat[2],
|
||||||
|
updated_at=chat[3],
|
||||||
|
created_at=chat[4],
|
||||||
|
)
|
||||||
|
for chat in all_chats
|
||||||
|
]
|
||||||
|
|
||||||
def get_chat_list_by_user_id(
|
def get_chat_list_by_user_id(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -887,18 +887,13 @@ async def get_shared_session_user_chat_list(
|
|||||||
if direction:
|
if direction:
|
||||||
filter["direction"] = direction
|
filter["direction"] = direction
|
||||||
|
|
||||||
chat_list = [
|
return Chats.get_shared_chat_list_by_user_id(
|
||||||
SharedChatResponse(**chat.model_dump())
|
user.id,
|
||||||
for chat in Chats.get_shared_chat_list_by_user_id(
|
filter=filter,
|
||||||
user.id,
|
skip=skip,
|
||||||
filter=filter,
|
limit=limit,
|
||||||
skip=skip,
|
db=db,
|
||||||
limit=limit,
|
)
|
||||||
db=db,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
return chat_list
|
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|||||||
Reference in new issue
Block a user