Underground-Digital/Workflow-Engine
0
1import pytz2from flask_login import current_user3 4from core.app.app_config.easy_ui_based_app.agent.manager import AgentConfigManager5from core.tools.tool_manager import ToolManager6from extensions.ext_database import db7from models.account import Account8from models.model import App, Conversation, EndUser, Message, MessageAgentThought9 10 11class AgentService:12 @classmethod13 def get_agent_logs(cls, app_model: App, conversation_id: str, message_id: str) -> dict:14 """15 Service to get agent logs16 """17 conversation: Conversation = (18 db.session.query(Conversation)19 .filter(20 Conversation.id == conversation_id,21 Conversation.app_id == app_model.id,22 )23 .first()24 )25 26 if not conversation:27 raise ValueError(f"Conversation not found: {conversation_id}")28 29 message: Message = (30 db.session.query(Message)31 .filter(32 Message.id == message_id,33 Message.conversation_id == conversation_id,34 )35 .first()36 )37 38 if not message:39 raise ValueError(f"Message not found: {message_id}")40 41 agent_thoughts: list[MessageAgentThought] = message.agent_thoughts42 43 if conversation.from_end_user_id:44 # only select name field45 executor = (46 db.session.query(EndUser, EndUser.name).filter(EndUser.id == conversation.from_end_user_id).first()47 )48 else:49 executor = (50 db.session.query(Account, Account.name).filter(Account.id == conversation.from_account_id).first()51 )52 53 if executor:54 executor = executor.name55 else:56 executor = "Unknown"57 58 timezone = pytz.timezone(current_user.timezone)59 60 result = {61 "meta": {62 "status": "success",63 "executor": executor,64 "start_time": message.created_at.astimezone(timezone).isoformat(),65 "elapsed_time": message.provider_response_latency,66 "total_tokens": message.answer_tokens + message.message_tokens,67 "agent_mode": app_model.app_model_config.agent_mode_dict.get("strategy", "react"),68 "iterations": len(agent_thoughts),69 },70 "iterations": [],71 "files": message.message_files,72 }73 74 agent_config = AgentConfigManager.convert(app_model.app_model_config.to_dict())75 agent_tools = agent_config.tools76 77 def find_agent_tool(tool_name: str):78 for agent_tool in agent_tools:79 if agent_tool.tool_name == tool_name:80 return agent_tool81 82 for agent_thought in agent_thoughts:83 tools = agent_thought.tools84 tool_labels = agent_thought.tool_labels85 tool_meta = agent_thought.tool_meta86 tool_inputs = agent_thought.tool_inputs_dict87 tool_outputs = agent_thought.tool_outputs_dict88 tool_calls = []89 for tool in tools:90 tool_name = tool91 tool_label = tool_labels.get(tool_name, tool_name)92 tool_input = tool_inputs.get(tool_name, {})93 tool_output = tool_outputs.get(tool_name, {})94 tool_meta_data = tool_meta.get(tool_name, {})95 tool_config = tool_meta_data.get("tool_config", {})96 if tool_config.get("tool_provider_type", "") != "dataset-retrieval":97 tool_icon = ToolManager.get_tool_icon(98 tenant_id=app_model.tenant_id,99 provider_type=tool_config.get("tool_provider_type", ""),100 provider_id=tool_config.get("tool_provider", ""),101 )102 if not tool_icon:103 tool_entity = find_agent_tool(tool_name)104 if tool_entity:105 tool_icon = ToolManager.get_tool_icon(106 tenant_id=app_model.tenant_id,107 provider_type=tool_entity.provider_type,108 provider_id=tool_entity.provider_id,109 )110 else:111 tool_icon = ""112 113 tool_calls.append(114 {115 "status": "success" if not tool_meta_data.get("error") else "error",116 "error": tool_meta_data.get("error"),117 "time_cost": tool_meta_data.get("time_cost", 0),118 "tool_name": tool_name,119 "tool_label": tool_label,120 "tool_input": tool_input,121 "tool_output": tool_output,122 "tool_parameters": tool_meta_data.get("tool_parameters", {}),123 "tool_icon": tool_icon,124 }125 )126 127 result["iterations"].append(128 {129 "tokens": agent_thought.tokens,130 "tool_calls": tool_calls,131 "tool_raw": {132 "inputs": agent_thought.tool_input,133 "outputs": agent_thought.observation,134 },135 "thought": agent_thought.thought,136 "created_at": agent_thought.created_at.isoformat(),137 "files": agent_thought.files,138 }139 )140 141 return result142 