@@ -514,14 +514,15 @@ def format_record(record: ChatRecordResult):
514514 return _dict
515515
516516
517- def get_chat_log_history (session : SessionDep , chat_record_id : int , current_user : CurrentUser ) -> ChatLogHistory :
517+ def get_chat_log_history (session : SessionDep , chat_record_id : int , current_user : CurrentUser , without_steps : bool = False ) -> ChatLogHistory :
518518 """
519519 获取ChatRecord的详细历史记录
520520
521521 Args:
522522 session: 数据库会话
523523 chat_record_id: ChatRecord的ID
524524 current_user: 当前用户
525+ without_steps
525526
526527 Returns:
527528 ChatLogHistory: 包含历史步骤和时间信息的对象
@@ -536,23 +537,15 @@ def get_chat_log_history(session: SessionDep, chat_record_id: int, current_user:
536537
537538 # 2. 查询与该ChatRecord相关的所有ChatLog记录
538539 chat_logs = session .query (ChatLog ).filter (
539- ChatLog .pid == chat_record_id
540+ ChatLog .pid == chat_record_id ,
541+ ChatLog .operate != OperationEnum .GENERATE_RECOMMENDED_QUESTIONS
540542 ).order_by (ChatLog .start_time ).all ()
541543
542544 # 3. 计算总的时间和token信息
543545 total_tokens = 0
544546 steps = []
545547
546548 for log in chat_logs :
547- # 计算单条记录的耗时
548- duration = None
549- if log .start_time and log .finish_time :
550- try :
551- time_diff = log .finish_time - log .start_time
552- duration = time_diff .total_seconds ()
553- except Exception :
554- duration = None
555-
556549 # 计算单条记录的token消耗
557550 log_tokens = 0
558551 if log .token_usage is not None :
@@ -567,16 +560,46 @@ def get_chat_log_history(session: SessionDep, chat_record_id: int, current_user:
567560 # 累加到总token消耗
568561 total_tokens += log_tokens
569562
570- # 创建ChatLogHistoryItem
571- history_item = ChatLogHistoryItem (
572- start_time = log .start_time ,
573- finish_time = log .finish_time ,
574- duration = duration ,
575- total_tokens = log_tokens ,
576- operate = log .operate ,
577- local_operation = log .local_operation
578- )
579- steps .append (history_item )
563+ if not without_steps :
564+ # 计算单条记录的耗时
565+ duration = None
566+ if log .start_time and log .finish_time :
567+ try :
568+ time_diff = log .finish_time - log .start_time
569+ duration = round (time_diff .total_seconds (), 2 )
570+ except Exception :
571+ duration = None
572+
573+ # 获取操作类型的枚举名称
574+ operate_name = None
575+ if log .operate :
576+ # 如果是OperationEnum枚举实例
577+ if isinstance (log .operate , OperationEnum ):
578+ operate_name = log .operate .name
579+ # 如果是字符串,尝试从枚举值获取名称
580+ elif isinstance (log .operate , str ):
581+ try :
582+ # 通过枚举值找到对应的枚举实例
583+ for enum_item in OperationEnum :
584+ if enum_item .value == log .operate :
585+ operate_name = enum_item .name
586+ break
587+ except Exception :
588+ operate_name = log .operate
589+ else :
590+ operate_name = str (log .operate )
591+
592+ # 创建ChatLogHistoryItem
593+ history_item = ChatLogHistoryItem (
594+ start_time = log .start_time ,
595+ finish_time = log .finish_time ,
596+ duration = duration ,
597+ total_tokens = log_tokens ,
598+ operate = operate_name ,
599+ local_operation = log .local_operation
600+ )
601+
602+ steps .append (history_item )
580603
581604 # 4. 计算总耗时(使用ChatRecord的时间)
582605 total_duration = None
0 commit comments