Skip to content

Commit 0ecca1e

Browse files
committed
feat: Rename & Delete Chat
1 parent 72d7104 commit 0ecca1e

File tree

9 files changed

+455
-126
lines changed

9 files changed

+455
-126
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""006_add_chat
2+
3+
Revision ID: ff653d5df198
4+
Revises: 0a6f11be9be4
5+
Create Date: 2025-05-21 15:44:29.763091
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 = 'ff653d5df198'
15+
down_revision = 'e6276ddab06e'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.create_table('chat',
23+
sa.Column('id', sa.Integer(), sa.Identity(always=True), nullable=False),
24+
sa.Column('create_time', sa.DateTime(timezone=True), nullable=True),
25+
sa.Column('create_by', sa.BigInteger(), nullable=True),
26+
sa.Column('brief', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=True),
27+
sa.Column('chat_type', sqlmodel.sql.sqltypes.AutoString(length=20), nullable=False),
28+
sa.Column('datasource', sa.Integer(), nullable=False),
29+
sa.Column('engine_type', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
30+
sa.PrimaryKeyConstraint('id')
31+
)
32+
op.create_table('chat_record',
33+
sa.Column('id', sa.Integer(), sa.Identity(always=True), nullable=False),
34+
sa.Column('chat_id', sa.Integer(), nullable=True),
35+
sa.Column('create_time', sa.DateTime(timezone=True), nullable=True),
36+
sa.Column('create_by', sa.BigInteger(), nullable=True),
37+
sa.Column('datasource', sa.Integer(), nullable=False),
38+
sa.Column('engine_type', sqlmodel.sql.sqltypes.AutoString(length=64), nullable=False),
39+
sa.Column('question', sa.Text(), nullable=True),
40+
sa.Column('full_question', sa.Text(), nullable=True),
41+
sa.Column('answer', sa.Text(), nullable=True),
42+
sa.Column('run_time', sa.Float(), nullable=False),
43+
sa.PrimaryKeyConstraint('id')
44+
)
45+
# ### end Alembic commands ###
46+
47+
48+
def downgrade():
49+
# ### commands auto generated by Alembic - please adjust! ###
50+
op.drop_table('chat_record')
51+
op.drop_table('chat')
52+
# ### end Alembic commands ###

backend/apps/chat/api/chat.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from fastapi.responses import StreamingResponse
33
from sqlmodel import select
44

5-
from apps.chat.curd.chat import list_chats, get_chat_with_records, create_chat, save_question, save_answer
6-
from apps.chat.models.chat_model import CreateChat, ChatRecord
5+
from apps.chat.curd.chat import list_chats, get_chat_with_records, create_chat, save_question, save_answer, rename_chat, \
6+
delete_chat
7+
from apps.chat.models.chat_model import CreateChat, ChatRecord, RenameChat
78
from apps.chat.schemas.chat_base_schema import LLMConfig
89
from apps.chat.schemas.chat_schema import ChatQuestion
910
from apps.chat.schemas.llm import AgentService
@@ -32,6 +33,28 @@ async def list_chat(session: SessionDep, current_user: CurrentUser, chart_id: in
3233
)
3334

3435

36+
@router.post("/rename")
37+
async def rename(session: SessionDep, chat: RenameChat):
38+
try:
39+
return rename_chat(session=session, rename_object=chat)
40+
except Exception as e:
41+
raise HTTPException(
42+
status_code=500,
43+
detail=str(e)
44+
)
45+
46+
47+
@router.get("/delete/{chart_id}")
48+
async def delete(session: SessionDep, chart_id: int):
49+
try:
50+
return delete_chat(session=session, chart_id=chart_id)
51+
except Exception as e:
52+
raise HTTPException(
53+
status_code=500,
54+
detail=str(e)
55+
)
56+
57+
3558
@router.post("/start")
3659
async def start_chat(session: SessionDep, current_user: CurrentUser, create_chat_obj: CreateChat):
3760
try:

backend/apps/chat/curd/chat.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from sqlalchemy import text, and_
66
from sqlmodel import select
77

8-
from apps.chat.models.chat_model import Chat, ChatRecord, CreateChat, ChatInfo
8+
from apps.chat.models.chat_model import Chat, ChatRecord, CreateChat, ChatInfo, RenameChat
99
from apps.chat.schemas.chat_schema import ChatQuestion
1010
from apps.datasource.models.datasource import CoreDatasource
1111
from common.core.deps import SessionDep, CurrentUser
@@ -17,6 +17,32 @@ def list_chats(session: SessionDep, current_user: CurrentUser) -> List[Chat]:
1717
return chart_list
1818

1919

20+
def rename_chat(session: SessionDep, rename_object: RenameChat) -> str:
21+
chat = session.query(Chat).filter(Chat.id == rename_object.id).first()
22+
if not chat:
23+
raise Exception(f"Chat with id {rename_object.id} not found")
24+
25+
chat.brief = rename_object.brief.strip()[:20]
26+
session.add(chat)
27+
session.flush()
28+
session.refresh(chat)
29+
30+
brief = chat.brief
31+
session.commit()
32+
return brief
33+
34+
35+
def delete_chat(session, chart_id) -> str:
36+
chat = session.query(Chat).filter(Chat.id == chart_id).first()
37+
if not chat:
38+
return f'Chat with id {chart_id} has been deleted'
39+
40+
session.delete(chat)
41+
session.commit()
42+
43+
return f'Chat with id {chart_id} has been deleted'
44+
45+
2046
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser) -> ChatInfo:
2147
chat = session.query(Chat).filter(Chat.id == chart_id).first()
2248
if not chat:
@@ -98,6 +124,7 @@ def save_question(session: SessionDep, current_user: CurrentUser, question: Chat
98124

99125
return result
100126

127+
101128
def save_full_question(session: SessionDep, id: int, full_question: str) -> ChatRecord:
102129
if not id:
103130
raise Exception("Record id cannot be None")
@@ -114,6 +141,7 @@ def save_full_question(session: SessionDep, id: int, full_question: str) -> Chat
114141

115142
return result
116143

144+
117145
def save_answer(session: SessionDep, id: int, answer: str) -> ChatRecord:
118146
if not id:
119147
raise Exception("Record id cannot be None")

backend/apps/chat/models/chat_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class CreateChat(BaseModel):
3636
datasource: int = None
3737

3838

39+
class RenameChat(BaseModel):
40+
id: int = None
41+
brief: str = ''
42+
43+
3944
class ChatInfo(BaseModel):
4045
id: Optional[int] = None
4146
create_time: datetime = None

frontend/components.d.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

frontend/src/api/chat.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,22 @@ function startChat(data: any): Promise<ChatInfo> {
147147
return request.post('/chat/start', data)
148148
}
149149

150+
function renameChat(chat_id: number, brief: string): Promise<string> {
151+
return request.post('/chat/rename', {id: chat_id, brief: brief})
152+
}
153+
154+
function deleteChat(id: number): Promise<string> {
155+
return request.get(`/chat/delete/${id}`)
156+
}
157+
150158
export const chatApi = {
151159
toChatRecord,
152160
toChatRecordList,
153161
toChatInfo,
154162
toChatInfoList,
155163
list,
156164
get,
157-
startChat
165+
startChat,
166+
renameChat,
167+
deleteChat
158168
}

0 commit comments

Comments
 (0)