Aasher commited on
Commit
cbe29b5
·
1 Parent(s): fc7c411

refactor(chat): remove db parameter from update_chat_title_from_message and manage DB session internally

Browse files
Files changed (2) hide show
  1. api/routers/chats.py +0 -1
  2. db/crud/chat.py +20 -17
api/routers/chats.py CHANGED
@@ -152,7 +152,6 @@ async def post_message_and_get_response(
152
  if is_first_user_message:
153
  background_tasks.add_task(
154
  chat_crud.update_chat_title_from_message,
155
- db=db,
156
  chat_id=chat.id,
157
  message_content=message_in.content
158
  )
 
152
  if is_first_user_message:
153
  background_tasks.add_task(
154
  chat_crud.update_chat_title_from_message,
 
155
  chat_id=chat.id,
156
  message_content=message_in.content
157
  )
db/crud/chat.py CHANGED
@@ -5,8 +5,12 @@ from typing import Optional
5
  from sqlmodel import Session, select
6
  from sqlmodel.ext.asyncio.session import AsyncSession
7
  from sqlalchemy.orm import selectinload, raiseload
 
8
  from db.models.chat import Chat
 
9
  from db.schemas.chat import ChatUpdate
 
 
10
 
11
  from workflow.title_generator import generate_chat_title
12
 
@@ -48,27 +52,26 @@ async def update_chat_title(db: AsyncSession, chat: Chat, chat_update: ChatUpdat
48
  await db.refresh(chat)
49
  return chat
50
 
51
- async def update_chat_title_from_message(db: AsyncSession, chat_id: uuid.UUID, message_content: str):
52
  """
53
  Generates a title from a message and updates the chat record.
54
- Designed to be run as a background task.
55
  """
56
- try:
57
- chat = await db.get(Chat, chat_id)
58
- if not chat or chat.title != "New Chat":
59
- # Chat doesn't exist or has already been titled, so we exit.
60
- return
 
61
 
62
- new_title = await generate_chat_title(message_content)
63
- if new_title:
64
- chat.title = new_title
65
- db.add(chat)
66
- await db.commit()
67
- print(f"INFO: Background task updated title for chat {chat_id} to '{new_title}'")
68
- except Exception as e:
69
- print(f"ERROR: Background task failed for chat {chat_id}: {e}")
70
- finally:
71
- await db.close() # Ensure the session is closed.
72
 
73
  async def delete_chat(db: AsyncSession, chat: Chat) -> None:
74
  """Deletes a chat from the database."""
 
5
  from sqlmodel import Session, select
6
  from sqlmodel.ext.asyncio.session import AsyncSession
7
  from sqlalchemy.orm import selectinload, raiseload
8
+
9
  from db.models.chat import Chat
10
+ from db.session import async_engine
11
  from db.schemas.chat import ChatUpdate
12
+ from workflow.title_generator import generate_chat_title
13
+
14
 
15
  from workflow.title_generator import generate_chat_title
16
 
 
52
  await db.refresh(chat)
53
  return chat
54
 
55
+ async def update_chat_title_from_message(chat_id: uuid.UUID, message_content: str):
56
  """
57
  Generates a title from a message and updates the chat record.
58
+ Designed to be run as a background task. It creates and closes its own DB session.
59
  """
60
+ async with AsyncSession(async_engine) as db:
61
+ try:
62
+ chat = await db.get(Chat, chat_id)
63
+ # Only update if the chat exists and still has the default title
64
+ if not chat or chat.title != "New Chat":
65
+ return
66
 
67
+ new_title = await generate_chat_title(message_content)
68
+ if new_title:
69
+ chat.title = new_title
70
+ db.add(chat)
71
+ await db.commit()
72
+ print(f"INFO: Background task updated title for chat {chat_id} to '{new_title}'")
73
+ except Exception as e:
74
+ print(f"ERROR: Background task failed for chat {chat_id}: {e}")
 
 
75
 
76
  async def delete_chat(db: AsyncSession, chat: Chat) -> None:
77
  """Deletes a chat from the database."""