Skip to content

Commit 5b4977d

Browse files
committed
feat: add more chat log operation steps
1 parent 8793154 commit 5b4977d

File tree

6 files changed

+168
-63
lines changed

6 files changed

+168
-63
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""062_update_chat_log_dll
2+
3+
Revision ID: c9ab05247503
4+
Revises: 547df942eb90
5+
Create Date: 2026-01-27 14:20:35.069255
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel.sql.sqltypes
11+
from sqlalchemy.dialects import postgresql
12+
13+
# revision identifiers, used by Alembic.
14+
revision = 'c9ab05247503'
15+
down_revision = '547df942eb90'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.add_column('chat_log', sa.Column('local_operation', sa.Boolean(), nullable=True))
23+
sql = '''
24+
UPDATE chat_log SET local_operation = false
25+
'''
26+
op.execute(sql)
27+
op.alter_column('chat_log', 'local_operation',
28+
existing_type=sa.BOOLEAN(),
29+
nullable=False)
30+
# ### end Alembic commands ###
31+
32+
33+
def downgrade():
34+
# ### commands auto generated by Alembic - please adjust! ###
35+
op.drop_column('chat_log', 'local_operation')
36+
# ### end Alembic commands ###

backend/apps/chat/curd/chat.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import List, Optional
2+
from typing import List, Optional, Union
33

44
import orjson
55
import sqlparse
@@ -62,6 +62,7 @@ def list_recent_questions(session: SessionDep, current_user: CurrentUser, dataso
6262
)
6363
return [record[0] for record in chat_records] if chat_records else []
6464

65+
6566
def rename_chat_with_user(session: SessionDep, current_user: CurrentUser, rename_object: RenameChat) -> str:
6667
chat = session.get(Chat, rename_object.id)
6768
if not chat:
@@ -78,6 +79,7 @@ def rename_chat_with_user(session: SessionDep, current_user: CurrentUser, rename
7879
session.commit()
7980
return brief
8081

82+
8183
def rename_chat(session: SessionDep, rename_object: RenameChat) -> str:
8284
chat = session.get(Chat, rename_object.id)
8385
if not chat:
@@ -104,6 +106,7 @@ def delete_chat(session, chart_id) -> str:
104106

105107
return f'Chat with id {chart_id} has been deleted'
106108

109+
107110
def delete_chat_with_user(session, current_user: CurrentUser, chart_id) -> str:
108111
chat = session.query(Chat).filter(Chat.id == chart_id).first()
109112
if not chat:
@@ -220,6 +223,7 @@ def get_chat_chart_config(session: SessionDep, chat_record_id: int):
220223
pass
221224
return {}
222225

226+
223227
def get_chart_data_with_user(session: SessionDep, current_user: CurrentUser, chat_record_id: int):
224228
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
225229
res = session.execute(stmt)
@@ -230,6 +234,7 @@ def get_chart_data_with_user(session: SessionDep, current_user: CurrentUser, cha
230234
pass
231235
return {}
232236

237+
233238
def get_chat_chart_data(session: SessionDep, chat_record_id: int):
234239
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chat_record_id))
235240
res = session.execute(stmt)
@@ -240,8 +245,10 @@ def get_chat_chart_data(session: SessionDep, chat_record_id: int):
240245
pass
241246
return {}
242247

248+
243249
def get_chat_predict_data_with_user(session: SessionDep, current_user: CurrentUser, chat_record_id: int):
244-
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
250+
stmt = select(ChatRecord.predict_data).where(
251+
and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
245252
res = session.execute(stmt)
246253
for row in res:
247254
try:
@@ -250,6 +257,7 @@ def get_chat_predict_data_with_user(session: SessionDep, current_user: CurrentUs
250257
pass
251258
return {}
252259

260+
253261
def get_chat_predict_data(session: SessionDep, chat_record_id: int):
254262
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chat_record_id))
255263
res = session.execute(stmt)
@@ -607,10 +615,11 @@ def save_analysis_predict_record(session: SessionDep, base_record: ChatRecord, a
607615
return result
608616

609617

610-
def start_log(session: SessionDep, ai_modal_id: int, ai_modal_name: str, operate: OperationEnum, record_id: int,
611-
full_message: list[dict]) -> ChatLog:
618+
def start_log(session: SessionDep, ai_modal_id: int = None, ai_modal_name: str = None, operate: OperationEnum = None,
619+
record_id: int = None, full_message: Union[list[dict], dict] = None,
620+
local_operation: bool = False) -> ChatLog:
612621
log = ChatLog(type=TypeEnum.CHAT, operate=operate, pid=record_id, ai_modal_id=ai_modal_id, base_modal=ai_modal_name,
613-
messages=full_message, start_time=datetime.datetime.now())
622+
messages=full_message, start_time=datetime.datetime.now(), local_operation=local_operation)
614623

615624
result = ChatLog(**log.model_dump())
616625

@@ -623,7 +632,8 @@ def start_log(session: SessionDep, ai_modal_id: int, ai_modal_name: str, operate
623632
return result
624633

625634

626-
def end_log(session: SessionDep, log: ChatLog, full_message: list[dict], reasoning_content: str = None,
635+
def end_log(session: SessionDep, log: ChatLog, full_message: Union[list[dict], dict, str],
636+
reasoning_content: str = None,
627637
token_usage=None) -> ChatLog:
628638
if token_usage is None:
629639
token_usage = {}
@@ -867,6 +877,8 @@ def save_error_message(session: SessionDep, record_id: int, message: str) -> Cha
867877

868878
session.commit()
869879

880+
# todo log error finish
881+
870882
return result
871883

872884

backend/apps/chat/models/chat_model.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ class OperationEnum(Enum):
4040
GENERATE_SQL_WITH_PERMISSIONS = '5'
4141
CHOOSE_DATASOURCE = '6'
4242
GENERATE_DYNAMIC_SQL = '7'
43-
43+
CHOOSE_TABLE = '8'
44+
FILTER_TERMS = '9'
45+
FILTER_SQL_EXAMPLE = '10'
46+
FILTER_CUSTOM_PROMPT = '11'
47+
EXECUTE_SQL = '12'
48+
GENERATE_PICTURE = '13'
4449

4550
class ChatFinishStep(Enum):
4651
GENERATE_SQL = 1
@@ -71,6 +76,7 @@ class ChatLog(SQLModel, table=True):
7176
start_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True))
7277
finish_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True))
7378
token_usage: Optional[dict | None | int] = Field(sa_column=Column(JSONB))
79+
local_operation: bool = Field(default=False)
7480

7581

7682
class Chat(SQLModel, table=True):

0 commit comments

Comments
 (0)