Skip to content

Commit

Permalink
feat: 技能新建会话时写审计日志
Browse files Browse the repository at this point in the history
  • Loading branch information
zgqgit committed Jun 20, 2024
1 parent fbb04af commit a17252c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/backend/bisheng/api/services/audit_log.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typing import Any
from uuid import UUID

from loguru import logger

from bisheng.api.services.user_service import UserPayload
from bisheng.database.models.audit_log import AuditLog, SystemId, EventType, ObjectType, AuditLogDao
from bisheng.database.models.assistant import AssistantDao
from bisheng.database.models.flow import FlowDao
from bisheng.database.models.group_resource import GroupResourceDao, ResourceTypeEnum
from bisheng.api.errcode.base import UnAuthorizedError
from bisheng.api.v1.schemas import resp_200
Expand Down Expand Up @@ -36,7 +38,7 @@ def create_chat_assistant(cls, user: UserPayload, ip_address: str, assistant_id:
新建助手会话的审计日志
"""
# 获取助手所属的分组
assistant_info = AssistantDao.get_one_assistant(assistant_id)
assistant_info = AssistantDao.get_one_assistant(UUID(assistant_id))
groups = GroupResourceDao.get_resource_group(ResourceTypeEnum.ASSISTANT, assistant_info.id.hex)
group_ids = [one.group_id for one in groups]
audit_log = AuditLog(
Expand All @@ -52,3 +54,25 @@ def create_chat_assistant(cls, user: UserPayload, ip_address: str, assistant_id:
)
AuditLogDao.insert_audit_logs([audit_log])
logger.info(f"act=create_chat_assistant user={user.user_name} assistant={assistant_id}")

@classmethod
def create_chat_flow(cls, user: UserPayload, ip_address: str, flow_id: str):
"""
新建技能会话的审计日志
"""
flow_info = FlowDao.get_flow_by_id(flow_id)
groups = GroupResourceDao.get_resource_group(ResourceTypeEnum.FLOW, flow_info.id.hex)
group_ids = [one.group_id for one in groups]
audit_log = AuditLog(
operator_id=user.user_id,
operator_name=user.user_name,
group_ids=group_ids,
system_id=SystemId.CHAT.value,
event_type=EventType.CREATE_CHAT.value,
object_type=ObjectType.FLOW.value,
object_id=flow_info.id.hex,
object_name=flow_info.name,
ip_address=ip_address,
)
AuditLogDao.insert_audit_logs([audit_log])
logger.info(f"act=create_chat_flow user={user.user_name} flow={flow_id}")
14 changes: 13 additions & 1 deletion src/backend/bisheng/chat/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from loguru import logger
from fastapi import WebSocket, WebSocketDisconnect, status, Request

from bisheng.api.services.audit_log import AuditLogService
from bisheng.api.services.user_service import UserPayload
from bisheng.api.utils import build_flow_no_yield
from bisheng.api.v1.schemas import ChatMessage, ChatResponse, FileResponse
Expand All @@ -21,7 +22,8 @@
from bisheng.chat.utils import process_node_data
from bisheng.database.base import session_getter
from bisheng.database.models.flow import Flow
from bisheng.database.models.user import User
from bisheng.database.models.message import ChatMessageDao
from bisheng.database.models.user import User, UserDao
from bisheng.processing.process import process_tweaks
from bisheng.utils.threadpool import ThreadPoolManager, thread_pool
from bisheng.utils.util import get_cache_key
Expand Down Expand Up @@ -394,6 +396,16 @@ async def _process_when_payload(self, flow_id: str, chat_id: str,
start_resp = ChatResponse(type='begin', category='system', **base_param)
if is_begin:
await self.send_json(flow_id, chat_id, start_resp)
# 判断下是否是首次创建会话
if chat_id:
res = ChatMessageDao.get_messages_by_chat_id(chat_id=chat_id)
if len(res) <= 1: # 说明是新建会话
websocket = self.active_connections[key]
login_user = UserPayload(**{
"user_id": user_id,
"user_name": UserDao.get_user(user_id).user_name,
})
AuditLogService.create_chat_flow(login_user, websocket.client.host, flow_id)
start_resp.type = 'start'

# should input data
Expand Down

0 comments on commit a17252c

Please sign in to comment.