Files
AILab/bbit_ai/app/db/postgres/aisession.py
T
2025-12-08 18:11:48 +08:00

42 lines
1.4 KiB
Python

from config.pgDb import pg_pool
def insert_session(
user_id: str, ai_id: str, session_id: str, session_title: str, available_module
):
with pg_pool.getConn() as coon:
with coon.cursor() as cur:
cur.execute(
"INSERT INTO ai_chat_sessions (id ,user_id, ai_id, title, available_module, created_at, updated_at) VALUES (%s, %s, %s, %s,%s, NOW(), NOW())",
(session_id, user_id, ai_id, session_title, available_module),
)
coon.commit()
def update_session_updated_at(session_id: str):
with pg_pool.getConn() as conn:
with conn.cursor() as cur:
cur.execute(
"UPDATE ai_chat_sessions SET updated_at = NOW() WHERE id = %s",
(session_id,),
)
conn.commit()
def get_sessions(user_id: str, available_module: str):
with pg_pool.getConn() as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT id, title, updated_at "
"FROM ai_chat_sessions "
"WHERE user_id = %s AND available_module = %s "
"ORDER BY updated_at DESC",
(user_id, available_module),
)
sessions = cur.fetchall()
return [
{"id": row[0], "title": row[1], "updated_at": row[2]}
for row in sessions
]