Files
microfish/backend/app/services/report_agent.py
Kunthawat Greethong afc7afa2f5 fix: 3 fixes for Step 2, Step 4, Step 5
1. Step 5: Use empty model_config_dict={} so camel-ai doesn't spread
   api_key into create() - AsyncOpenAI reads env vars automatically.
   Step 3 unaffected (same env vars, just cleaner ModelFactory call).

2. Step 2: Fix Thai text truncation - isalnum() stripped Thai chars.
   Use re.sub(r'[^\w]', '', username, re.UNICODE) instead.

3. Step 4: Move get_language_instruction() to START of prompt (not end)
   and strengthen wording with MUST/IMPORTANT prefix.
2026-06-22 10:57:57 +07:00

2572 lines
99 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Report Agent服务
使用LangChain + Zep实现ReACT模式的模拟报告生成
功能:
1. 根据模拟需求和Zep图谱信息生成报告
2. 先规划目录结构,然后分段生成
3. 每段采用ReACT多轮思考与反思模式
4. 支持与用户对话,在对话中自主调用检索工具
"""
import os
import json
import time
import re
from typing import Dict, Any, List, Optional, Callable
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from ..config import Config
from ..utils.llm_client import LLMClient
from ..utils.logger import get_logger
from ..utils.locale import get_language_instruction, t
from .zep_tools import (
ZepToolsService,
SearchResult,
InsightForgeResult,
PanoramaResult,
InterviewResult
)
logger = get_logger('mirofish.report_agent')
class ReportLogger:
"""
Report Agent 详细日志记录器
在报告文件夹中生成 agent_log.jsonl 文件,记录每一步详细动作。
每行是一个完整的 JSON 对象,包含时间戳、动作类型、详细内容等。
"""
def __init__(self, report_id: str):
"""
初始化日志记录器
Args:
report_id: 报告ID用于确定日志文件路径
"""
self.report_id = report_id
self.log_file_path = os.path.join(
Config.UPLOAD_FOLDER, 'reports', report_id, 'agent_log.jsonl'
)
self.start_time = datetime.now()
self._ensure_log_file()
def _ensure_log_file(self):
"""确保日志文件所在目录存在"""
log_dir = os.path.dirname(self.log_file_path)
os.makedirs(log_dir, exist_ok=True)
def _get_elapsed_time(self) -> float:
"""获取从开始到现在的耗时(秒)"""
return (datetime.now() - self.start_time).total_seconds()
def log(
self,
action: str,
stage: str,
details: Dict[str, Any],
section_title: str = None,
section_index: int = None
):
"""
记录一条日志
Args:
action: 动作类型,如 'start', 'tool_call', 'llm_response', 'section_complete'
stage: 当前阶段,如 'planning', 'generating', 'completed'
details: 详细内容字典,不截断
section_title: 当前章节标题(可选)
section_index: 当前章节索引(可选)
"""
log_entry = {
"timestamp": datetime.now().isoformat(),
"elapsed_seconds": round(self._get_elapsed_time(), 2),
"report_id": self.report_id,
"action": action,
"stage": stage,
"section_title": section_title,
"section_index": section_index,
"details": details
}
# 追加写入 JSONL 文件
with open(self.log_file_path, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry, ensure_ascii=False) + '\n')
def log_start(self, simulation_id: str, graph_id: str, simulation_requirement: str):
"""记录报告生成开始"""
self.log(
action="report_start",
stage="pending",
details={
"simulation_id": simulation_id,
"graph_id": graph_id,
"simulation_requirement": simulation_requirement,
"message": t('report.taskStarted')
}
)
def log_planning_start(self):
"""记录大纲规划开始"""
self.log(
action="planning_start",
stage="planning",
details={"message": t('report.planningStart')}
)
def log_planning_context(self, context: Dict[str, Any]):
"""记录规划时获取的上下文信息"""
self.log(
action="planning_context",
stage="planning",
details={
"message": t('report.fetchSimContext'),
"context": context
}
)
def log_planning_complete(self, outline_dict: Dict[str, Any]):
"""记录大纲规划完成"""
self.log(
action="planning_complete",
stage="planning",
details={
"message": t('report.planningComplete'),
"outline": outline_dict
}
)
def log_section_start(self, section_title: str, section_index: int):
"""记录章节生成开始"""
self.log(
action="section_start",
stage="generating",
section_title=section_title,
section_index=section_index,
details={"message": t('report.sectionStart', title=section_title)}
)
def log_react_thought(self, section_title: str, section_index: int, iteration: int, thought: str):
"""记录 ReACT 思考过程"""
self.log(
action="react_thought",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"iteration": iteration,
"thought": thought,
"message": t('report.reactThought', iteration=iteration)
}
)
def log_tool_call(
self,
section_title: str,
section_index: int,
tool_name: str,
parameters: Dict[str, Any],
iteration: int
):
"""记录工具调用"""
self.log(
action="tool_call",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"iteration": iteration,
"tool_name": tool_name,
"parameters": parameters,
"message": t('report.toolCall', toolName=tool_name)
}
)
def log_tool_result(
self,
section_title: str,
section_index: int,
tool_name: str,
result: str,
iteration: int
):
"""记录工具调用结果(完整内容,不截断)"""
self.log(
action="tool_result",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"iteration": iteration,
"tool_name": tool_name,
"result": result, # 完整结果,不截断
"result_length": len(result),
"message": t('report.toolResult', toolName=tool_name)
}
)
def log_llm_response(
self,
section_title: str,
section_index: int,
response: str,
iteration: int,
has_tool_calls: bool,
has_final_answer: bool
):
"""记录 LLM 响应(完整内容,不截断)"""
self.log(
action="llm_response",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"iteration": iteration,
"response": response, # 完整响应,不截断
"response_length": len(response),
"has_tool_calls": has_tool_calls,
"has_final_answer": has_final_answer,
"message": t('report.llmResponse', hasToolCalls=has_tool_calls, hasFinalAnswer=has_final_answer)
}
)
def log_section_content(
self,
section_title: str,
section_index: int,
content: str,
tool_calls_count: int
):
"""记录章节内容生成完成(仅记录内容,不代表整个章节完成)"""
self.log(
action="section_content",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"content": content, # 完整内容,不截断
"content_length": len(content),
"tool_calls_count": tool_calls_count,
"message": t('report.sectionContentDone', title=section_title)
}
)
def log_section_full_complete(
self,
section_title: str,
section_index: int,
full_content: str
):
"""
记录章节生成完成
前端应监听此日志来判断一个章节是否真正完成,并获取完整内容
"""
self.log(
action="section_complete",
stage="generating",
section_title=section_title,
section_index=section_index,
details={
"content": full_content,
"content_length": len(full_content),
"message": t('report.sectionComplete', title=section_title)
}
)
def log_report_complete(self, total_sections: int, total_time_seconds: float):
"""记录报告生成完成"""
self.log(
action="report_complete",
stage="completed",
details={
"total_sections": total_sections,
"total_time_seconds": round(total_time_seconds, 2),
"message": t('report.reportComplete')
}
)
def log_error(self, error_message: str, stage: str, section_title: str = None):
"""记录错误"""
self.log(
action="error",
stage=stage,
section_title=section_title,
section_index=None,
details={
"error": error_message,
"message": t('report.errorOccurred', error=error_message)
}
)
class ReportConsoleLogger:
"""
Report Agent 控制台日志记录器
将控制台风格的日志INFO、WARNING等写入报告文件夹中的 console_log.txt 文件。
这些日志与 agent_log.jsonl 不同,是纯文本格式的控制台输出。
"""
def __init__(self, report_id: str):
"""
初始化控制台日志记录器
Args:
report_id: 报告ID用于确定日志文件路径
"""
self.report_id = report_id
self.log_file_path = os.path.join(
Config.UPLOAD_FOLDER, 'reports', report_id, 'console_log.txt'
)
self._ensure_log_file()
self._file_handler = None
self._setup_file_handler()
def _ensure_log_file(self):
"""确保日志文件所在目录存在"""
log_dir = os.path.dirname(self.log_file_path)
os.makedirs(log_dir, exist_ok=True)
def _setup_file_handler(self):
"""设置文件处理器,将日志同时写入文件"""
import logging
# 创建文件处理器
self._file_handler = logging.FileHandler(
self.log_file_path,
mode='a',
encoding='utf-8'
)
self._file_handler.setLevel(logging.INFO)
# 使用与控制台相同的简洁格式
formatter = logging.Formatter(
'[%(asctime)s] %(levelname)s: %(message)s',
datefmt='%H:%M:%S'
)
self._file_handler.setFormatter(formatter)
# 添加到 report_agent 相关的 logger
loggers_to_attach = [
'mirofish.report_agent',
'mirofish.zep_tools',
]
for logger_name in loggers_to_attach:
target_logger = logging.getLogger(logger_name)
# 避免重复添加
if self._file_handler not in target_logger.handlers:
target_logger.addHandler(self._file_handler)
def close(self):
"""关闭文件处理器并从 logger 中移除"""
import logging
if self._file_handler:
loggers_to_detach = [
'mirofish.report_agent',
'mirofish.zep_tools',
]
for logger_name in loggers_to_detach:
target_logger = logging.getLogger(logger_name)
if self._file_handler in target_logger.handlers:
target_logger.removeHandler(self._file_handler)
self._file_handler.close()
self._file_handler = None
def __del__(self):
"""析构时确保关闭文件处理器"""
self.close()
class ReportStatus(str, Enum):
"""报告状态"""
PENDING = "pending"
PLANNING = "planning"
GENERATING = "generating"
COMPLETED = "completed"
FAILED = "failed"
@dataclass
class ReportSection:
"""报告章节"""
title: str
content: str = ""
def to_dict(self) -> Dict[str, Any]:
return {
"title": self.title,
"content": self.content
}
def to_markdown(self, level: int = 2) -> str:
"""转换为Markdown格式"""
md = f"{'#' * level} {self.title}\n\n"
if self.content:
md += f"{self.content}\n\n"
return md
@dataclass
class ReportOutline:
"""报告大纲"""
title: str
summary: str
sections: List[ReportSection]
def to_dict(self) -> Dict[str, Any]:
return {
"title": self.title,
"summary": self.summary,
"sections": [s.to_dict() for s in self.sections]
}
def to_markdown(self) -> str:
"""转换为Markdown格式"""
md = f"# {self.title}\n\n"
md += f"> {self.summary}\n\n"
for section in self.sections:
md += section.to_markdown()
return md
@dataclass
class Report:
"""完整报告"""
report_id: str
simulation_id: str
graph_id: str
simulation_requirement: str
status: ReportStatus
outline: Optional[ReportOutline] = None
markdown_content: str = ""
created_at: str = ""
completed_at: str = ""
error: Optional[str] = None
def to_dict(self) -> Dict[str, Any]:
return {
"report_id": self.report_id,
"simulation_id": self.simulation_id,
"graph_id": self.graph_id,
"simulation_requirement": self.simulation_requirement,
"status": self.status.value,
"outline": self.outline.to_dict() if self.outline else None,
"markdown_content": self.markdown_content,
"created_at": self.created_at,
"completed_at": self.completed_at,
"error": self.error
}
# ═══════════════════════════════════════════════════════════════
# Prompt 模板常量
# ═══════════════════════════════════════════════════════════════
# ── 工具描述 ──
TOOL_DESC_INSIGHT_FORGE = """\
[Deep Insight Retrieval - Powerful Analysis Tool]
This is our powerful retrieval function designed for deep analysis. It will:
1. Automatically decompose your question into multiple sub-questions
2. Retrieve information from multiple dimensions in the simulation graph
3. Integrate results from semantic search, entity analysis, and relationship chain tracking
4. Return the most comprehensive and in-depth retrieval content
[Use Cases]
- Deep analysis of a specific topic
- Understanding multiple aspects of an event
- Gathering rich material to support report sections
[Returned Content]
- Relevant original facts (can be directly quoted)
- Core entity insights
- Relationship chain analysis"""
TOOL_DESC_PANORAMA_SEARCH = """\
[Panorama Search - Get the Full Picture]
This tool retrieves the complete picture of simulation results, especially useful for understanding event evolution. It will:
1. Retrieve all related nodes and relationships
2. Distinguish between currently valid facts and historical/expired facts
3. Help you understand how public sentiment evolved
[Use Cases]
- Understanding the full development timeline of an event
- Comparing sentiment changes across different stages
- Getting comprehensive entity and relationship information
[Returned Content]
- Currently valid facts (latest simulation results)
- Historical/expired facts (evolution records)
- All involved entities"""
TOOL_DESC_QUICK_SEARCH = """\
[Quick Search - Fast Retrieval]
A lightweight fast retrieval tool suitable for simple, direct information queries.
[Use Cases]
- Quickly finding specific information
- Verifying a fact
- Simple information retrieval
[Returned Content]
- List of facts most relevant to the query"""
TOOL_DESC_INTERVIEW_AGENTS = """\
[Deep Interview - Real Agent Interview (Dual Platform)]
Calls the OASIS simulation environment's interview API to conduct real interviews with running simulation agents!
This is NOT LLM simulation - it calls the real interview API to get original responses from simulation agents.
By default, interviews are conducted simultaneously on both Twitter and Reddit platforms for more comprehensive perspectives.
Workflow:
1. Automatically reads persona files to understand all simulation agents
2. Intelligently selects agents most relevant to the interview topic (e.g., students, media, officials)
3. Automatically generates interview questions
4. Calls the /api/simulation/interview/batch endpoint for real interviews on both platforms
5. Consolidates all interview results and provides multi-perspective analysis
[Use Cases]
- Understanding event perspectives from different role viewpoints (What do students think? What about media? What do officials say?)
- Collecting opinions and positions from multiple parties
- Getting real responses from simulation agents (from OASIS simulation environment)
- Making reports more vivid with "interview transcripts"
[Returned Content]
- Identity information of interviewed agents
- Interview responses from each agent on both Twitter and Reddit platforms
- Key quotes (can be directly quoted)
- Interview summary and perspective comparison
[Important] Requires the OASIS simulation environment to be running!"""
# ── 大纲规划 prompt ──
PLAN_SYSTEM_PROMPT = """\
You are a "Future Prediction Report" writing expert with a "God's-eye view" of the simulation world — you can observe the behavior, speech, and interactions of every Agent in the simulation.
[Core Philosophy]
We have built a simulation world and injected a specific "simulation requirement" as a variable. The evolution of the simulation world represents a prediction of what may happen in the future. What you are observing is not "experimental data" but a "rehearsal of the future."
[Your Task]
Write a "Future Prediction Report" that answers:
1. Under the conditions we set, what happened in the future?
2. How did various groups of Agents (people) react and act?
3. What noteworthy future trends and risks does this simulation reveal?
[Report Positioning]
- ✅ This is a simulation-based future prediction report, revealing "if this, then what happens in the future"
- ✅ Focus on prediction outcomes: event trajectory, group reactions, emergent phenomena, potential risks
- ✅ Agent behavior and speech in the simulation world represent predictions of future crowd behavior
- ❌ NOT an analysis of the current state of the real world
- ❌ NOT a generic public sentiment overview
[Chapter Count Limits]
- Minimum 2 chapters, maximum 5 chapters
- No sub-chapters needed; each chapter should directly write complete content
- Content should be concise, focused on core prediction findings
- Chapter structure should be designed by you based on prediction results
Please output the report outline in JSON format as follows:
{
"title": "Report Title",
"summary": "Report Summary (one sentence summarizing core prediction findings)",
"sections": [
{
"title": "Section Title",
"description": "Section Content Description"
}
]
}
Note: The sections array must have a minimum of 2 and maximum of 5 elements!"""
PLAN_USER_PROMPT_TEMPLATE = """\
[Prediction Scenario Setup]
The variable (simulation requirement) we injected into the simulation world: {simulation_requirement}
[Simulation World Scale]
- Number of entities participating in simulation: {total_nodes}
- Number of relationships generated between entities: {total_edges}
- Entity type distribution: {entity_types}
- Number of active Agents: {total_entities}
[Sample of Predicted Future Facts from the Simulation]
{related_facts_json}
Please examine this future rehearsal from a "God's-eye view":
1. Under the conditions we set, what state has the future taken on?
2. How are various groups of people (Agents) reacting and acting?
3. What noteworthy future trends does this simulation reveal?
Design the most appropriate report section structure based on the prediction results.
[Reminder] Report chapter count: minimum 2, maximum 5. Content should be concise and focused on core prediction findings."""
# ── 章节生成 prompt ──
SECTION_SYSTEM_PROMPT_TEMPLATE = """\
You are a "Future Prediction Report" writing expert, currently writing a section of the report.
Report Title: {report_title}
Report Summary: {report_summary}
Prediction Scenario (Simulation Requirement): {simulation_requirement}
Section currently being written: {section_title}
═══════════════════════════════════════════════════════════════
[Core Philosophy]
═══════════════════════════════════════════════════════════════
The simulation world is a rehearsal of the future. We injected specific conditions (simulation requirement) into the simulation world. The behavior and interactions of Agents in the simulation represent predictions of future crowd behavior.
Your task is to:
- Reveal what happened in the future under the set conditions
- Predict how various groups of people (Agents) react and act
- Discover noteworthy future trends, risks, and opportunities
❌ Do NOT write this as an analysis of the current state of the real world
✅ Focus on "what will happen in the future" — the simulation results ARE the predicted future
═══════════════════════════════════════════════════════════════
[Most Important Rules - Must Be Followed]
═══════════════════════════════════════════════════════════════
1. [Must Call Tools to Observe the Simulation World]
- You are observing the future rehearsal from a "God's-eye view"
- All content must come from events and Agent behavior occurring in the simulation world
- Do NOT use your own knowledge to write report content
- Each section must call tools at least 3 times (maximum 5) to observe the simulated world, which represents the future
2. [Must Quote Agent's Original Words and Actions]
- Agent speech and behavior are predictions of future crowd behavior
- Display these predictions using quote format in the report, for example:
> "A certain group of people would say: original content..."
- These quotes are core evidence for simulation predictions
3. [Language Consistency - Quoted Content Must Be Translated to Report Language]
- Content returned by tools may contain expressions in a different language than the report
- The entire report must be written in the language specified by the user
- When quoting content returned by tools in other languages, you must translate it into the report language before including it
- Maintain the original meaning during translation and ensure natural expression
- This rule applies to both body text and quote blocks (> format)
4. [Faithfully Present Prediction Results]
- Report content must reflect the simulation results representing the future in the simulation world
- Do not add information that does not exist in the simulation
- If information on a certain aspect is insufficient, state so honestly
═══════════════════════════════════════════════════════════════
[⚠️ Format Requirements - Extremely Important!]
═══════════════════════════════════════════════════════════════
[One Section = Minimum Content Unit]
- Each section is the minimum chunk unit of the report
- ❌ Do NOT use any Markdown headings (#, ##, ###, #### etc.) within a section
- ❌ Do NOT add the section main title at the beginning of the content
- ✅ Section titles are automatically added by the system; you only need to write plain body content
- ✅ Use **bold text**, paragraph breaks, quotes, and lists to organize content, but do NOT use headings
[Correct Example]
```
This section analyzes the public sentiment propagation dynamics of the event. Through in-depth analysis of simulation data, we found...
**Initial Ignition Phase**
Platform A served as the first site of public sentiment, carrying the core function of information release:
> "Platform A contributed 68% of the initial volume..."
**Emotion Amplification Phase**
Platform B further amplified the event's impact:
- Strong visual impact
- High emotional resonance
```
[Incorrect Example]
```
## Executive Summary ← WRONG! Do not add any headings
### Part 1: Initial Phase ← WRONG! Do not use ### for sub-sections
#### 1.1 Detailed Analysis ← WRONG! Do not use #### for sub-divisions
This section analyzes...
```
═══════════════════════════════════════════════════════════════
[Available Retrieval Tools] (Call 3-5 times per section)
═══════════════════════════════════════════════════════════════
{tools_description}
[Tool Usage Tips - Please mix different tools, do not use only one]
- insight_forge: Deep insight analysis, automatically decomposes questions and retrieves facts and relationships from multiple dimensions
- panorama_search: Panoramic search to understand event overview, timeline, and evolution process
- quick_search: Quickly verify a specific piece of information
- interview_agents: Interview simulation agents to get first-person perspectives and real reactions from different roles
═══════════════════════════════════════════════════════════════
[Workflow]
═══════════════════════════════════════════════════════════════
Each response you can only do ONE of the following two things (NOT both):
Option A - Call a tool:
Output your thoughts, then call a tool using the following format:
<tool_call>
{{"name": "tool_name", "parameters": {{"param_name": "param_value"}}}}
</tool_call>
The system will execute the tool and return the result to you. You do not need to and cannot write tool results yourself.
Option B - Output final content:
When you have gathered enough information through tools, output the section content starting with "Final Answer:".
⚠️ Strictly prohibited:
- Do NOT include both a tool call and Final Answer in the same response
- Do NOT fabricate tool results (Observation); all tool results are injected by the system
- Each response can call at most one tool
═══════════════════════════════════════════════════════════════
[Section Content Requirements]
═══════════════════════════════════════════════════════════════
1. Content must be based on simulation data retrieved by tools
2. Extensively quote original text to demonstrate simulation results
3. Use Markdown format (but headings are prohibited):
- Use **bold text** to highlight key points (replacing sub-headings)
- Use lists (- or 1.2.3.) to organize key points
- Use blank lines to separate paragraphs
- ❌ Do NOT use #, ##, ###, #### or any heading syntax
4. [Quote Format Requirements - Must Be Standalone Paragraphs]
Quotes must be standalone paragraphs with blank lines before and after, not mixed into regular paragraphs:
✅ Correct format:
```
The institution's response was considered lacking in substance.
> "The institution's response mode appears rigid and slow in the rapidly changing social media environment."
This assessment reflects widespread public dissatisfaction.
```
❌ Incorrect format:
```
The institution's response was considered lacking in substance.> "The institution's response mode..." This assessment reflects...
```
5. Maintain logical coherence with other sections
6. [Avoid Repetition] Carefully read the content of completed sections below and do not repeat the same information
7. [Emphasize Again] Do NOT add any headings! Use **bold text** instead of sub-section headings"""
SECTION_USER_PROMPT_TEMPLATE = """\
Completed section content (please read carefully to avoid repetition):
{previous_content}
═══════════════════════════════════════════════════════════════
[Current Task] Write Section: {section_title}
═══════════════════════════════════════════════════════════════
[Important Reminders]
1. Carefully read the completed sections above to avoid repeating the same content!
2. You must call tools to retrieve simulation data before starting
3. Please mix different tools, do not use only one
4. Report content must come from retrieval results, do not use your own knowledge
[⚠️ Format Warning - Must Be Followed]
- ❌ Do not write any headings (#, ##, ###, #### are all prohibited)
- ❌ Do not write "{section_title}" as the beginning
- ✅ Section titles are automatically added by the system
- ✅ Write body text directly, use **bold text** instead of sub-section headings
Please begin:
1. First think (Thought) about what information this section needs
2. Then call a tool (Action) to retrieve simulation data
3. After gathering enough information, output Final Answer (plain body text, no headings)"""
# ── ReACT 循环内消息模板 ──
REACT_OBSERVATION_TEMPLATE = """\
Observation (Retrieval Result):
═══ Tool {tool_name} Returned ═══
{result}
═══════════════════════════════════════════════════════════════
Called tools {tool_calls_count}/{max_tool_calls} times (used: {used_tools_str}){unused_hint}
- If information is sufficient: output section content starting with "Final Answer:" (must quote the above original text)
- If more information is needed: call a tool to continue retrieving
═══════════════════════════════════════════════════════════════"""
REACT_INSUFFICIENT_TOOLS_MSG = (
"[Note] You have only called tools {tool_calls_count} time(s), at least {min_tool_calls} required. "
"Please call tools to get more simulation data before outputting Final Answer. {unused_hint}"
)
REACT_INSUFFICIENT_TOOLS_MSG_ALT = (
"Currently only called tools {tool_calls_count} time(s), at least {min_tool_calls} required. "
"Please call a tool to retrieve simulation data. {unused_hint}"
)
REACT_TOOL_LIMIT_MSG = (
"Tool call limit reached ({tool_calls_count}/{max_tool_calls}), no more tools can be called. "
'Please immediately output section content starting with "Final Answer:" based on the information already gathered.'
)
REACT_UNUSED_TOOLS_HINT = "\n💡 You haven't used: {unused_list}, try different tools for multi-angle information"
REACT_FORCE_FINAL_MSG = "Tool call limit reached. Please directly output Final Answer: and generate section content."
# ── Chat prompt ──
CHAT_SYSTEM_PROMPT_TEMPLATE = """\
You are a concise and efficient simulation prediction assistant.
[Background]
Prediction Condition: {simulation_requirement}
[Generated Analysis Report]
{report_content}
[Rules]
1. Prioritize answering questions based on the report content above
2. Answer directly, avoid lengthy reasoning
3. Only call tools to retrieve more data when the report content is insufficient to answer
4. Answers should be concise, clear, and well-organized
[Available Tools] (use only when needed, maximum 1-2 calls)
{tools_description}
[Tool Call Format]
<tool_call>
{{"name": "tool_name", "parameters": {{"param_name": "param_value"}}}}
</tool_call>
[Response Style]
- Concise and direct, avoid lengthy explanations
- Use > format to quote key content
- Lead with conclusions, then explain reasoning"""
CHAT_OBSERVATION_SUFFIX = "\n\nPlease answer the question concisely."
# ═══════════════════════════════════════════════════════════════
# ReportAgent 主类
# ═══════════════════════════════════════════════════════════════
class ReportAgent:
"""
Report Agent - 模拟报告生成Agent
采用ReACTReasoning + Acting模式
1. 规划阶段:分析模拟需求,规划报告目录结构
2. 生成阶段:逐章节生成内容,每章节可多次调用工具获取信息
3. 反思阶段:检查内容完整性和准确性
"""
# 最大工具调用次数(每个章节)
MAX_TOOL_CALLS_PER_SECTION = 5
# 最大反思轮数
MAX_REFLECTION_ROUNDS = 3
# 对话中的最大工具调用次数
MAX_TOOL_CALLS_PER_CHAT = 2
def __init__(
self,
graph_id: str,
simulation_id: str,
simulation_requirement: str,
llm_client: Optional[LLMClient] = None,
zep_tools: Optional[ZepToolsService] = None
):
"""
初始化Report Agent
Args:
graph_id: 图谱ID
simulation_id: 模拟ID
simulation_requirement: 模拟需求描述
llm_client: LLM客户端可选
zep_tools: Zep工具服务可选
"""
self.graph_id = graph_id
self.simulation_id = simulation_id
self.simulation_requirement = simulation_requirement
self.llm = llm_client or LLMClient()
self.zep_tools = zep_tools or ZepToolsService()
# 工具定义
self.tools = self._define_tools()
# 日志记录器(在 generate_report 中初始化)
self.report_logger: Optional[ReportLogger] = None
# 控制台日志记录器(在 generate_report 中初始化)
self.console_logger: Optional[ReportConsoleLogger] = None
logger.info(t('report.agentInitDone', graphId=graph_id, simulationId=simulation_id))
def _define_tools(self) -> Dict[str, Dict[str, Any]]:
"""定义可用工具"""
return {
"insight_forge": {
"name": "insight_forge",
"description": TOOL_DESC_INSIGHT_FORGE,
"parameters": {
"query": "The question or topic you want to analyze in depth",
"report_context": "Current report section context (optional, helps generate more precise sub-questions)"
}
},
"panorama_search": {
"name": "panorama_search",
"description": TOOL_DESC_PANORAMA_SEARCH,
"parameters": {
"query": "Search query, used for relevance ranking",
"include_expired": "Whether to include expired/historical content (default True)"
}
},
"quick_search": {
"name": "quick_search",
"description": TOOL_DESC_QUICK_SEARCH,
"parameters": {
"query": "Search query string",
"limit": "Number of results to return (optional, default 10)"
}
},
"interview_agents": {
"name": "interview_agents",
"description": TOOL_DESC_INTERVIEW_AGENTS,
"parameters": {
"interview_topic": "Interview topic or requirement description (e.g., 'understand student views on the dormitory incident')",
"max_agents": "Maximum number of agents to interview (optional, default 5, max 10)"
}
}
}
def _execute_tool(self, tool_name: str, parameters: Dict[str, Any], report_context: str = "") -> str:
"""
执行工具调用
Args:
tool_name: 工具名称
parameters: 工具参数
report_context: 报告上下文用于InsightForge
Returns:
工具执行结果(文本格式)
"""
logger.info(t('report.executingTool', toolName=tool_name, params=parameters))
try:
if tool_name == "insight_forge":
query = parameters.get("query", "")
ctx = parameters.get("report_context", "") or report_context
result = self.zep_tools.insight_forge(
graph_id=self.graph_id,
query=query,
simulation_requirement=self.simulation_requirement,
report_context=ctx
)
return result.to_text()
elif tool_name == "panorama_search":
# 广度搜索 - 获取全貌
query = parameters.get("query", "")
include_expired = parameters.get("include_expired", True)
if isinstance(include_expired, str):
include_expired = include_expired.lower() in ['true', '1', 'yes']
result = self.zep_tools.panorama_search(
graph_id=self.graph_id,
query=query,
include_expired=include_expired
)
return result.to_text()
elif tool_name == "quick_search":
# 简单搜索 - 快速检索
query = parameters.get("query", "")
limit = parameters.get("limit", 10)
if isinstance(limit, str):
limit = int(limit)
result = self.zep_tools.quick_search(
graph_id=self.graph_id,
query=query,
limit=limit
)
return result.to_text()
elif tool_name == "interview_agents":
# 深度采访 - 调用真实的OASIS采访API获取模拟Agent的回答双平台
interview_topic = parameters.get("interview_topic", parameters.get("query", ""))
max_agents = parameters.get("max_agents", 5)
if isinstance(max_agents, str):
max_agents = int(max_agents)
max_agents = min(max_agents, 10)
result = self.zep_tools.interview_agents(
simulation_id=self.simulation_id,
interview_requirement=interview_topic,
simulation_requirement=self.simulation_requirement,
max_agents=max_agents
)
return result.to_text()
# ========== 向后兼容的旧工具(内部重定向到新工具) ==========
elif tool_name == "search_graph":
# 重定向到 quick_search
logger.info(t('report.redirectToQuickSearch'))
return self._execute_tool("quick_search", parameters, report_context)
elif tool_name == "get_graph_statistics":
result = self.zep_tools.get_graph_statistics(self.graph_id)
return json.dumps(result, ensure_ascii=False, indent=2)
elif tool_name == "get_entity_summary":
entity_name = parameters.get("entity_name", "")
result = self.zep_tools.get_entity_summary(
graph_id=self.graph_id,
entity_name=entity_name
)
return json.dumps(result, ensure_ascii=False, indent=2)
elif tool_name == "get_simulation_context":
# 重定向到 insight_forge因为它更强大
logger.info(t('report.redirectToInsightForge'))
query = parameters.get("query", self.simulation_requirement)
return self._execute_tool("insight_forge", {"query": query}, report_context)
elif tool_name == "get_entities_by_type":
entity_type = parameters.get("entity_type", "")
nodes = self.zep_tools.get_entities_by_type(
graph_id=self.graph_id,
entity_type=entity_type
)
result = [n.to_dict() for n in nodes]
return json.dumps(result, ensure_ascii=False, indent=2)
else:
return f"Unknown tool: {tool_name}. Please use one of the following tools: insight_forge, panorama_search, quick_search"
except Exception as e:
logger.error(t('report.toolExecFailed', toolName=tool_name, error=str(e)))
return f"Tool execution failed: {str(e)}"
# 合法的工具名称集合,用于裸 JSON 兜底解析时校验
VALID_TOOL_NAMES = {"insight_forge", "panorama_search", "quick_search", "interview_agents"}
def _parse_tool_calls(self, response: str) -> List[Dict[str, Any]]:
"""
从LLM响应中解析工具调用
支持的格式(按优先级):
1. <tool_call>{"name": "tool_name", "parameters": {...}}</tool_call>
2. 裸 JSON响应整体或单行就是一个工具调用 JSON
"""
tool_calls = []
# 格式1: XML风格标准格式
xml_pattern = r'<tool_call>\s*(\{.*?\})\s*</tool_call>'
for match in re.finditer(xml_pattern, response, re.DOTALL):
try:
call_data = json.loads(match.group(1))
tool_calls.append(call_data)
except json.JSONDecodeError:
pass
if tool_calls:
return tool_calls
# 格式2: 兜底 - LLM 直接输出裸 JSON没包 <tool_call> 标签)
# 只在格式1未匹配时尝试避免误匹配正文中的 JSON
stripped = response.strip()
if stripped.startswith('{') and stripped.endswith('}'):
try:
call_data = json.loads(stripped)
if self._is_valid_tool_call(call_data):
tool_calls.append(call_data)
return tool_calls
except json.JSONDecodeError:
pass
# 响应可能包含思考文字 + 裸 JSON尝试提取最后一个 JSON 对象
json_pattern = r'(\{"(?:name|tool)"\s*:.*?\})\s*$'
match = re.search(json_pattern, stripped, re.DOTALL)
if match:
try:
call_data = json.loads(match.group(1))
if self._is_valid_tool_call(call_data):
tool_calls.append(call_data)
except json.JSONDecodeError:
pass
return tool_calls
def _is_valid_tool_call(self, data: dict) -> bool:
"""校验解析出的 JSON 是否是合法的工具调用"""
# 支持 {"name": ..., "parameters": ...} 和 {"tool": ..., "params": ...} 两种键名
tool_name = data.get("name") or data.get("tool")
if tool_name and tool_name in self.VALID_TOOL_NAMES:
# 统一键名为 name / parameters
if "tool" in data:
data["name"] = data.pop("tool")
if "params" in data and "parameters" not in data:
data["parameters"] = data.pop("params")
return True
return False
def _get_tools_description(self) -> str:
"""生成工具描述文本"""
desc_parts = ["Available tools:"]
for name, tool in self.tools.items():
params_desc = ", ".join([f"{k}: {v}" for k, v in tool["parameters"].items()])
desc_parts.append(f"- {name}: {tool['description']}")
if params_desc:
desc_parts.append(f" Parameters: {params_desc}")
return "\n".join(desc_parts)
def plan_outline(
self,
progress_callback: Optional[Callable] = None
) -> ReportOutline:
"""
规划报告大纲
使用LLM分析模拟需求规划报告的目录结构
Args:
progress_callback: 进度回调函数
Returns:
ReportOutline: 报告大纲
"""
logger.info(t('report.startPlanningOutline'))
if progress_callback:
progress_callback("planning", 0, t('progress.analyzingRequirements'))
# 首先获取模拟上下文
context = self.zep_tools.get_simulation_context(
graph_id=self.graph_id,
simulation_requirement=self.simulation_requirement
)
if progress_callback:
progress_callback("planning", 30, t('progress.generatingOutline'))
system_prompt = f"{get_language_instruction()}\n\n{PLAN_SYSTEM_PROMPT}"
user_prompt = PLAN_USER_PROMPT_TEMPLATE.format(
simulation_requirement=self.simulation_requirement,
total_nodes=context.get('graph_statistics', {}).get('total_nodes', 0),
total_edges=context.get('graph_statistics', {}).get('total_edges', 0),
entity_types=list(context.get('graph_statistics', {}).get('entity_types', {}).keys()),
total_entities=context.get('total_entities', 0),
related_facts_json=json.dumps(context.get('related_facts', [])[:10], ensure_ascii=False, indent=2),
)
try:
response = self.llm.chat_json(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
temperature=0.3
)
if progress_callback:
progress_callback("planning", 80, t('progress.parsingOutline'))
# 解析大纲
sections = []
for section_data in response.get("sections", []):
sections.append(ReportSection(
title=section_data.get("title", ""),
content=""
))
outline = ReportOutline(
title=response.get("title", "模拟分析报告"),
summary=response.get("summary", ""),
sections=sections
)
if progress_callback:
progress_callback("planning", 100, t('progress.outlinePlanComplete'))
logger.info(t('report.outlinePlanDone', count=len(sections)))
return outline
except Exception as e:
logger.error(t('report.outlinePlanFailed', error=str(e)))
# 返回默认大纲3个章节作为fallback
return ReportOutline(
title="Future Prediction Report",
summary="Future trends and risk analysis based on simulation predictions",
sections=[
ReportSection(title="Prediction Scenario and Core Findings"),
ReportSection(title="Crowd Behavior Prediction Analysis"),
ReportSection(title="Trend Outlook and Risk Warnings")
]
)
def _generate_section_react(
self,
section: ReportSection,
outline: ReportOutline,
previous_sections: List[str],
progress_callback: Optional[Callable] = None,
section_index: int = 0
) -> str:
"""
使用ReACT模式生成单个章节内容
ReACT循环
1. Thought思考- 分析需要什么信息
2. Action行动- 调用工具获取信息
3. Observation观察- 分析工具返回结果
4. 重复直到信息足够或达到最大次数
5. Final Answer最终回答- 生成章节内容
Args:
section: 要生成的章节
outline: 完整大纲
previous_sections: 之前章节的内容(用于保持连贯性)
progress_callback: 进度回调
section_index: 章节索引(用于日志记录)
Returns:
章节内容Markdown格式
"""
logger.info(t('report.reactGenerateSection', title=section.title))
# 记录章节开始日志
if self.report_logger:
self.report_logger.log_section_start(section.title, section_index)
system_prompt = SECTION_SYSTEM_PROMPT_TEMPLATE.format(
report_title=outline.title,
report_summary=outline.summary,
simulation_requirement=self.simulation_requirement,
section_title=section.title,
tools_description=self._get_tools_description(),
)
system_prompt = f"{get_language_instruction()}\n\n{system_prompt}"
# 构建用户prompt - 每个已完成章节各传入最大4000字
if previous_sections:
previous_parts = []
for sec in previous_sections:
# 每个章节最多4000字
truncated = sec[:4000] + "..." if len(sec) > 4000 else sec
previous_parts.append(truncated)
previous_content = "\n\n---\n\n".join(previous_parts)
else:
previous_content = "(This is the first section)"
user_prompt = SECTION_USER_PROMPT_TEMPLATE.format(
previous_content=previous_content,
section_title=section.title,
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
]
# ReACT循环
tool_calls_count = 0
max_iterations = 5 # 最大迭代轮数
min_tool_calls = 3 # 最少工具调用次数
conflict_retries = 0 # 工具调用与Final Answer同时出现的连续冲突次数
used_tools = set() # 记录已调用过的工具名
all_tools = {"insight_forge", "panorama_search", "quick_search", "interview_agents"}
# 报告上下文用于InsightForge的子问题生成
report_context = f"章节标题: {section.title}\n模拟需求: {self.simulation_requirement}"
for iteration in range(max_iterations):
if progress_callback:
progress_callback(
"generating",
int((iteration / max_iterations) * 100),
t('progress.deepSearchAndWrite', current=tool_calls_count, max=self.MAX_TOOL_CALLS_PER_SECTION)
)
# 调用LLM
response = self.llm.chat(
messages=messages,
temperature=0.5,
max_tokens=4096
)
# 检查 LLM 返回是否为 NoneAPI 异常或内容为空)
if response is None:
logger.warning(t('report.sectionIterNone', title=section.title, iteration=iteration + 1))
# 如果还有迭代次数,添加消息并重试
if iteration < max_iterations - 1:
messages.append({"role": "assistant", "content": "(响应为空)"})
messages.append({"role": "user", "content": "Please continue generating content."})
continue
# 最后一次迭代也返回 None跳出循环进入强制收尾
break
logger.debug(f"LLM响应: {response[:200]}...")
# 解析一次,复用结果
tool_calls = self._parse_tool_calls(response)
has_tool_calls = bool(tool_calls)
has_final_answer = "Final Answer:" in response
# ── 冲突处理LLM 同时输出了工具调用和 Final Answer ──
if has_tool_calls and has_final_answer:
conflict_retries += 1
logger.warning(
t('report.sectionConflict', title=section.title, iteration=iteration+1, conflictCount=conflict_retries)
)
if conflict_retries <= 2:
# 前两次:丢弃本次响应,要求 LLM 重新回复
messages.append({"role": "assistant", "content": response})
messages.append({
"role": "user",
"content": (
"[Format Error] You included both a tool call and Final Answer in the same response, which is not allowed.\n"
"Each response can only do ONE of the following two things:\n"
"- Call a tool (output a <tool_call> block, do not write Final Answer)\n"
"- Output final content (start with 'Final Answer:', do not include <tool_call>)\n"
"Please reply again, doing only one of the two."
),
})
continue
else:
# 第三次:降级处理,截断到第一个工具调用,强制执行
logger.warning(
t('report.sectionConflictDowngrade', title=section.title, conflictCount=conflict_retries)
)
first_tool_end = response.find('</tool_call>')
if first_tool_end != -1:
response = response[:first_tool_end + len('</tool_call>')]
tool_calls = self._parse_tool_calls(response)
has_tool_calls = bool(tool_calls)
has_final_answer = False
conflict_retries = 0
# 记录 LLM 响应日志
if self.report_logger:
self.report_logger.log_llm_response(
section_title=section.title,
section_index=section_index,
response=response,
iteration=iteration + 1,
has_tool_calls=has_tool_calls,
has_final_answer=has_final_answer
)
# ── 情况1LLM 输出了 Final Answer ──
if has_final_answer:
# 工具调用次数不足,拒绝并要求继续调工具
if tool_calls_count < min_tool_calls:
messages.append({"role": "assistant", "content": response})
unused_tools = all_tools - used_tools
unused_hint = f"(这些工具还未使用,推荐用一下他们: {', '.join(unused_tools)}" if unused_tools else ""
messages.append({
"role": "user",
"content": REACT_INSUFFICIENT_TOOLS_MSG.format(
tool_calls_count=tool_calls_count,
min_tool_calls=min_tool_calls,
unused_hint=unused_hint,
),
})
continue
# 正常结束
final_answer = response.split("Final Answer:")[-1].strip()
logger.info(t('report.sectionGenDone', title=section.title, count=tool_calls_count))
if self.report_logger:
self.report_logger.log_section_content(
section_title=section.title,
section_index=section_index,
content=final_answer,
tool_calls_count=tool_calls_count
)
return final_answer
# ── 情况2LLM 尝试调用工具 ──
if has_tool_calls:
# 工具额度已耗尽 → 明确告知,要求输出 Final Answer
if tool_calls_count >= self.MAX_TOOL_CALLS_PER_SECTION:
messages.append({"role": "assistant", "content": response})
messages.append({
"role": "user",
"content": REACT_TOOL_LIMIT_MSG.format(
tool_calls_count=tool_calls_count,
max_tool_calls=self.MAX_TOOL_CALLS_PER_SECTION,
),
})
continue
# 只执行第一个工具调用
call = tool_calls[0]
if len(tool_calls) > 1:
logger.info(t('report.multiToolOnlyFirst', total=len(tool_calls), toolName=call['name']))
if self.report_logger:
self.report_logger.log_tool_call(
section_title=section.title,
section_index=section_index,
tool_name=call["name"],
parameters=call.get("parameters", {}),
iteration=iteration + 1
)
result = self._execute_tool(
call["name"],
call.get("parameters", {}),
report_context=report_context
)
if self.report_logger:
self.report_logger.log_tool_result(
section_title=section.title,
section_index=section_index,
tool_name=call["name"],
result=result,
iteration=iteration + 1
)
tool_calls_count += 1
used_tools.add(call['name'])
# 构建未使用工具提示
unused_tools = all_tools - used_tools
unused_hint = ""
if unused_tools and tool_calls_count < self.MAX_TOOL_CALLS_PER_SECTION:
unused_hint = REACT_UNUSED_TOOLS_HINT.format(unused_list="".join(unused_tools))
messages.append({"role": "assistant", "content": response})
messages.append({
"role": "user",
"content": REACT_OBSERVATION_TEMPLATE.format(
tool_name=call["name"],
result=result,
tool_calls_count=tool_calls_count,
max_tool_calls=self.MAX_TOOL_CALLS_PER_SECTION,
used_tools_str=", ".join(used_tools),
unused_hint=unused_hint,
),
})
continue
# ── 情况3既没有工具调用也没有 Final Answer ──
messages.append({"role": "assistant", "content": response})
if tool_calls_count < min_tool_calls:
# 工具调用次数不足,推荐未用过的工具
unused_tools = all_tools - used_tools
unused_hint = f"(这些工具还未使用,推荐用一下他们: {', '.join(unused_tools)}" if unused_tools else ""
messages.append({
"role": "user",
"content": REACT_INSUFFICIENT_TOOLS_MSG_ALT.format(
tool_calls_count=tool_calls_count,
min_tool_calls=min_tool_calls,
unused_hint=unused_hint,
),
})
continue
# 工具调用已足够LLM 输出了内容但没带 "Final Answer:" 前缀
# 直接将这段内容作为最终答案,不再空转
logger.info(t('report.sectionNoPrefix', title=section.title, count=tool_calls_count))
final_answer = response.strip()
if self.report_logger:
self.report_logger.log_section_content(
section_title=section.title,
section_index=section_index,
content=final_answer,
tool_calls_count=tool_calls_count
)
return final_answer
# 达到最大迭代次数,强制生成内容
logger.warning(t('report.sectionMaxIter', title=section.title))
messages.append({"role": "user", "content": REACT_FORCE_FINAL_MSG})
response = self.llm.chat(
messages=messages,
temperature=0.5,
max_tokens=4096
)
# 检查强制收尾时 LLM 返回是否为 None
if response is None:
logger.error(t('report.sectionForceFailed', title=section.title))
final_answer = t('report.sectionGenFailedContent')
elif "Final Answer:" in response:
final_answer = response.split("Final Answer:")[-1].strip()
else:
final_answer = response
# 记录章节内容生成完成日志
if self.report_logger:
self.report_logger.log_section_content(
section_title=section.title,
section_index=section_index,
content=final_answer,
tool_calls_count=tool_calls_count
)
return final_answer
def generate_report(
self,
progress_callback: Optional[Callable[[str, int, str], None]] = None,
report_id: Optional[str] = None
) -> Report:
"""
生成完整报告(分章节实时输出)
每个章节生成完成后立即保存到文件夹,不需要等待整个报告完成。
文件结构:
reports/{report_id}/
meta.json - 报告元信息
outline.json - 报告大纲
progress.json - 生成进度
section_01.md - 第1章节
section_02.md - 第2章节
...
full_report.md - 完整报告
Args:
progress_callback: 进度回调函数 (stage, progress, message)
report_id: 报告ID可选如果不传则自动生成
Returns:
Report: 完整报告
"""
import uuid
# 如果没有传入 report_id则自动生成
if not report_id:
report_id = f"report_{uuid.uuid4().hex[:12]}"
start_time = datetime.now()
report = Report(
report_id=report_id,
simulation_id=self.simulation_id,
graph_id=self.graph_id,
simulation_requirement=self.simulation_requirement,
status=ReportStatus.PENDING,
created_at=datetime.now().isoformat()
)
# 已完成的章节标题列表(用于进度追踪)
completed_section_titles = []
try:
# 初始化:创建报告文件夹并保存初始状态
ReportManager._ensure_report_folder(report_id)
# 初始化日志记录器(结构化日志 agent_log.jsonl
self.report_logger = ReportLogger(report_id)
self.report_logger.log_start(
simulation_id=self.simulation_id,
graph_id=self.graph_id,
simulation_requirement=self.simulation_requirement
)
# 初始化控制台日志记录器console_log.txt
self.console_logger = ReportConsoleLogger(report_id)
ReportManager.update_progress(
report_id, "pending", 0, t('progress.initReport'),
completed_sections=[]
)
ReportManager.save_report(report)
# 阶段1: 规划大纲
report.status = ReportStatus.PLANNING
ReportManager.update_progress(
report_id, "planning", 5, t('progress.startPlanningOutline'),
completed_sections=[]
)
# 记录规划开始日志
self.report_logger.log_planning_start()
if progress_callback:
progress_callback("planning", 0, t('progress.startPlanningOutline'))
outline = self.plan_outline(
progress_callback=lambda stage, prog, msg:
progress_callback(stage, prog // 5, msg) if progress_callback else None
)
report.outline = outline
# 记录规划完成日志
self.report_logger.log_planning_complete(outline.to_dict())
# 保存大纲到文件
ReportManager.save_outline(report_id, outline)
ReportManager.update_progress(
report_id, "planning", 15, t('progress.outlineDone', count=len(outline.sections)),
completed_sections=[]
)
ReportManager.save_report(report)
logger.info(t('report.outlineSavedToFile', reportId=report_id))
# 阶段2: 逐章节生成(分章节保存)
report.status = ReportStatus.GENERATING
total_sections = len(outline.sections)
generated_sections = [] # 保存内容用于上下文
for i, section in enumerate(outline.sections):
section_num = i + 1
base_progress = 20 + int((i / total_sections) * 70)
# 更新进度
ReportManager.update_progress(
report_id, "generating", base_progress,
t('progress.generatingSection', title=section.title, current=section_num, total=total_sections),
current_section=section.title,
completed_sections=completed_section_titles
)
if progress_callback:
progress_callback(
"generating",
base_progress,
t('progress.generatingSection', title=section.title, current=section_num, total=total_sections)
)
# 生成主章节内容
section_content = self._generate_section_react(
section=section,
outline=outline,
previous_sections=generated_sections,
progress_callback=lambda stage, prog, msg:
progress_callback(
stage,
base_progress + int(prog * 0.7 / total_sections),
msg
) if progress_callback else None,
section_index=section_num
)
section.content = section_content
generated_sections.append(f"## {section.title}\n\n{section_content}")
# 保存章节
ReportManager.save_section(report_id, section_num, section)
completed_section_titles.append(section.title)
# 记录章节完成日志
full_section_content = f"## {section.title}\n\n{section_content}"
if self.report_logger:
self.report_logger.log_section_full_complete(
section_title=section.title,
section_index=section_num,
full_content=full_section_content.strip()
)
logger.info(t('report.sectionSaved', reportId=report_id, sectionNum=f"{section_num:02d}"))
# 更新进度
ReportManager.update_progress(
report_id, "generating",
base_progress + int(70 / total_sections),
t('progress.sectionDone', title=section.title),
current_section=None,
completed_sections=completed_section_titles
)
# 阶段3: 组装完整报告
if progress_callback:
progress_callback("generating", 95, t('progress.assemblingReport'))
ReportManager.update_progress(
report_id, "generating", 95, t('progress.assemblingReport'),
completed_sections=completed_section_titles
)
# 使用ReportManager组装完整报告
report.markdown_content = ReportManager.assemble_full_report(report_id, outline)
report.status = ReportStatus.COMPLETED
report.completed_at = datetime.now().isoformat()
# 计算总耗时
total_time_seconds = (datetime.now() - start_time).total_seconds()
# 记录报告完成日志
if self.report_logger:
self.report_logger.log_report_complete(
total_sections=total_sections,
total_time_seconds=total_time_seconds
)
# 保存最终报告
ReportManager.save_report(report)
ReportManager.update_progress(
report_id, "completed", 100, t('progress.reportComplete'),
completed_sections=completed_section_titles
)
if progress_callback:
progress_callback("completed", 100, t('progress.reportComplete'))
logger.info(t('report.reportGenDone', reportId=report_id))
# 关闭控制台日志记录器
if self.console_logger:
self.console_logger.close()
self.console_logger = None
return report
except Exception as e:
logger.error(t('report.reportGenFailed', error=str(e)))
report.status = ReportStatus.FAILED
report.error = str(e)
# 记录错误日志
if self.report_logger:
self.report_logger.log_error(str(e), "failed")
# 保存失败状态
try:
ReportManager.save_report(report)
ReportManager.update_progress(
report_id, "failed", -1, t('progress.reportFailed', error=str(e)),
completed_sections=completed_section_titles
)
except Exception:
pass # 忽略保存失败的错误
# 关闭控制台日志记录器
if self.console_logger:
self.console_logger.close()
self.console_logger = None
return report
def chat(
self,
message: str,
chat_history: List[Dict[str, str]] = None
) -> Dict[str, Any]:
"""
与Report Agent对话
在对话中Agent可以自主调用检索工具来回答问题
Args:
message: 用户消息
chat_history: 对话历史
Returns:
{
"response": "Agent回复",
"tool_calls": [调用的工具列表],
"sources": [信息来源]
}
"""
logger.info(t('report.agentChat', message=message[:50]))
chat_history = chat_history or []
# 获取已生成的报告内容
report_content = ""
try:
report = ReportManager.get_report_by_simulation(self.simulation_id)
if report and report.markdown_content:
# 限制报告长度,避免上下文过长
report_content = report.markdown_content[:15000]
if len(report.markdown_content) > 15000:
report_content += "\n\n... [报告内容已截断] ..."
except Exception as e:
logger.warning(t('report.fetchReportFailed', error=e))
system_prompt = CHAT_SYSTEM_PROMPT_TEMPLATE.format(
simulation_requirement=self.simulation_requirement,
report_content=report_content if report_content else "(暂无报告)",
tools_description=self._get_tools_description(),
)
system_prompt = f"{get_language_instruction()}\n\n{system_prompt}"
# 构建消息
messages = [{"role": "system", "content": system_prompt}]
# 添加历史对话
for h in chat_history[-10:]: # 限制历史长度
messages.append(h)
# 添加用户消息
messages.append({
"role": "user",
"content": message
})
# ReACT循环简化版
tool_calls_made = []
max_iterations = 2 # 减少迭代轮数
for iteration in range(max_iterations):
response = self.llm.chat(
messages=messages,
temperature=0.5
)
# 解析工具调用
tool_calls = self._parse_tool_calls(response)
if not tool_calls:
# 没有工具调用,直接返回响应
clean_response = re.sub(r'<tool_call>.*?</tool_call>', '', response, flags=re.DOTALL)
clean_response = re.sub(r'\[TOOL_CALL\].*?\)', '', clean_response)
return {
"response": clean_response.strip(),
"tool_calls": tool_calls_made,
"sources": [tc.get("parameters", {}).get("query", "") for tc in tool_calls_made]
}
# 执行工具调用(限制数量)
tool_results = []
for call in tool_calls[:1]: # 每轮最多执行1次工具调用
if len(tool_calls_made) >= self.MAX_TOOL_CALLS_PER_CHAT:
break
result = self._execute_tool(call["name"], call.get("parameters", {}))
tool_results.append({
"tool": call["name"],
"result": result[:1500] # 限制结果长度
})
tool_calls_made.append(call)
# 将结果添加到消息
messages.append({"role": "assistant", "content": response})
observation = "\n".join([f"[{r['tool']}结果]\n{r['result']}" for r in tool_results])
messages.append({
"role": "user",
"content": observation + CHAT_OBSERVATION_SUFFIX
})
# 达到最大迭代,获取最终响应
final_response = self.llm.chat(
messages=messages,
temperature=0.5
)
# 清理响应
clean_response = re.sub(r'<tool_call>.*?</tool_call>', '', final_response, flags=re.DOTALL)
clean_response = re.sub(r'\[TOOL_CALL\].*?\)', '', clean_response)
return {
"response": clean_response.strip(),
"tool_calls": tool_calls_made,
"sources": [tc.get("parameters", {}).get("query", "") for tc in tool_calls_made]
}
class ReportManager:
"""
报告管理器
负责报告的持久化存储和检索
文件结构(分章节输出):
reports/
{report_id}/
meta.json - 报告元信息和状态
outline.json - 报告大纲
progress.json - 生成进度
section_01.md - 第1章节
section_02.md - 第2章节
...
full_report.md - 完整报告
"""
# 报告存储目录
REPORTS_DIR = os.path.join(Config.UPLOAD_FOLDER, 'reports')
@classmethod
def _ensure_reports_dir(cls):
"""确保报告根目录存在"""
os.makedirs(cls.REPORTS_DIR, exist_ok=True)
@classmethod
def _get_report_folder(cls, report_id: str) -> str:
"""获取报告文件夹路径"""
return os.path.join(cls.REPORTS_DIR, report_id)
@classmethod
def _ensure_report_folder(cls, report_id: str) -> str:
"""确保报告文件夹存在并返回路径"""
folder = cls._get_report_folder(report_id)
os.makedirs(folder, exist_ok=True)
return folder
@classmethod
def _get_report_path(cls, report_id: str) -> str:
"""获取报告元信息文件路径"""
return os.path.join(cls._get_report_folder(report_id), "meta.json")
@classmethod
def _get_report_markdown_path(cls, report_id: str) -> str:
"""获取完整报告Markdown文件路径"""
return os.path.join(cls._get_report_folder(report_id), "full_report.md")
@classmethod
def _get_outline_path(cls, report_id: str) -> str:
"""获取大纲文件路径"""
return os.path.join(cls._get_report_folder(report_id), "outline.json")
@classmethod
def _get_progress_path(cls, report_id: str) -> str:
"""获取进度文件路径"""
return os.path.join(cls._get_report_folder(report_id), "progress.json")
@classmethod
def _get_section_path(cls, report_id: str, section_index: int) -> str:
"""获取章节Markdown文件路径"""
return os.path.join(cls._get_report_folder(report_id), f"section_{section_index:02d}.md")
@classmethod
def _get_agent_log_path(cls, report_id: str) -> str:
"""获取 Agent 日志文件路径"""
return os.path.join(cls._get_report_folder(report_id), "agent_log.jsonl")
@classmethod
def _get_console_log_path(cls, report_id: str) -> str:
"""获取控制台日志文件路径"""
return os.path.join(cls._get_report_folder(report_id), "console_log.txt")
@classmethod
def get_console_log(cls, report_id: str, from_line: int = 0) -> Dict[str, Any]:
"""
获取控制台日志内容
这是报告生成过程中的控制台输出日志INFO、WARNING等
与 agent_log.jsonl 的结构化日志不同。
Args:
report_id: 报告ID
from_line: 从第几行开始读取用于增量获取0 表示从头开始)
Returns:
{
"logs": [日志行列表],
"total_lines": 总行数,
"from_line": 起始行号,
"has_more": 是否还有更多日志
}
"""
log_path = cls._get_console_log_path(report_id)
if not os.path.exists(log_path):
return {
"logs": [],
"total_lines": 0,
"from_line": 0,
"has_more": False
}
logs = []
total_lines = 0
with open(log_path, 'r', encoding='utf-8') as f:
for i, line in enumerate(f):
total_lines = i + 1
if i >= from_line:
# 保留原始日志行,去掉末尾换行符
logs.append(line.rstrip('\n\r'))
return {
"logs": logs,
"total_lines": total_lines,
"from_line": from_line,
"has_more": False # 已读取到末尾
}
@classmethod
def get_console_log_stream(cls, report_id: str) -> List[str]:
"""
获取完整的控制台日志(一次性获取全部)
Args:
report_id: 报告ID
Returns:
日志行列表
"""
result = cls.get_console_log(report_id, from_line=0)
return result["logs"]
@classmethod
def get_agent_log(cls, report_id: str, from_line: int = 0) -> Dict[str, Any]:
"""
获取 Agent 日志内容
Args:
report_id: 报告ID
from_line: 从第几行开始读取用于增量获取0 表示从头开始)
Returns:
{
"logs": [日志条目列表],
"total_lines": 总行数,
"from_line": 起始行号,
"has_more": 是否还有更多日志
}
"""
log_path = cls._get_agent_log_path(report_id)
if not os.path.exists(log_path):
return {
"logs": [],
"total_lines": 0,
"from_line": 0,
"has_more": False
}
logs = []
total_lines = 0
with open(log_path, 'r', encoding='utf-8') as f:
for i, line in enumerate(f):
total_lines = i + 1
if i >= from_line:
try:
log_entry = json.loads(line.strip())
logs.append(log_entry)
except json.JSONDecodeError:
# 跳过解析失败的行
continue
return {
"logs": logs,
"total_lines": total_lines,
"from_line": from_line,
"has_more": False # 已读取到末尾
}
@classmethod
def get_agent_log_stream(cls, report_id: str) -> List[Dict[str, Any]]:
"""
获取完整的 Agent 日志(用于一次性获取全部)
Args:
report_id: 报告ID
Returns:
日志条目列表
"""
result = cls.get_agent_log(report_id, from_line=0)
return result["logs"]
@classmethod
def save_outline(cls, report_id: str, outline: ReportOutline) -> None:
"""
保存报告大纲
在规划阶段完成后立即调用
"""
cls._ensure_report_folder(report_id)
with open(cls._get_outline_path(report_id), 'w', encoding='utf-8') as f:
json.dump(outline.to_dict(), f, ensure_ascii=False, indent=2)
logger.info(t('report.outlineSaved', reportId=report_id))
@classmethod
def save_section(
cls,
report_id: str,
section_index: int,
section: ReportSection
) -> str:
"""
保存单个章节
在每个章节生成完成后立即调用,实现分章节输出
Args:
report_id: 报告ID
section_index: 章节索引从1开始
section: 章节对象
Returns:
保存的文件路径
"""
cls._ensure_report_folder(report_id)
# 构建章节Markdown内容 - 清理可能存在的重复标题
cleaned_content = cls._clean_section_content(section.content, section.title)
md_content = f"## {section.title}\n\n"
if cleaned_content:
md_content += f"{cleaned_content}\n\n"
# 保存文件
file_suffix = f"section_{section_index:02d}.md"
file_path = os.path.join(cls._get_report_folder(report_id), file_suffix)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(md_content)
logger.info(t('report.sectionFileSaved', reportId=report_id, fileSuffix=file_suffix))
return file_path
@classmethod
def _clean_section_content(cls, content: str, section_title: str) -> str:
"""
清理章节内容
1. 移除内容开头与章节标题重复的Markdown标题行
2. 将所有 ### 及以下级别的标题转换为粗体文本
Args:
content: 原始内容
section_title: 章节标题
Returns:
清理后的内容
"""
import re
if not content:
return content
content = content.strip()
lines = content.split('\n')
cleaned_lines = []
skip_next_empty = False
for i, line in enumerate(lines):
stripped = line.strip()
# 检查是否是Markdown标题行
heading_match = re.match(r'^(#{1,6})\s+(.+)$', stripped)
if heading_match:
level = len(heading_match.group(1))
title_text = heading_match.group(2).strip()
# 检查是否是与章节标题重复的标题跳过前5行内的重复
if i < 5:
if title_text == section_title or title_text.replace(' ', '') == section_title.replace(' ', ''):
skip_next_empty = True
continue
# 将所有级别的标题(#, ##, ###, ####等)转换为粗体
# 因为章节标题由系统添加,内容中不应有任何标题
cleaned_lines.append(f"**{title_text}**")
cleaned_lines.append("") # 添加空行
continue
# 如果上一行是被跳过的标题,且当前行为空,也跳过
if skip_next_empty and stripped == '':
skip_next_empty = False
continue
skip_next_empty = False
cleaned_lines.append(line)
# 移除开头的空行
while cleaned_lines and cleaned_lines[0].strip() == '':
cleaned_lines.pop(0)
# 移除开头的分隔线
while cleaned_lines and cleaned_lines[0].strip() in ['---', '***', '___']:
cleaned_lines.pop(0)
# 同时移除分隔线后的空行
while cleaned_lines and cleaned_lines[0].strip() == '':
cleaned_lines.pop(0)
return '\n'.join(cleaned_lines)
@classmethod
def update_progress(
cls,
report_id: str,
status: str,
progress: int,
message: str,
current_section: str = None,
completed_sections: List[str] = None
) -> None:
"""
更新报告生成进度
前端可以通过读取progress.json获取实时进度
"""
cls._ensure_report_folder(report_id)
progress_data = {
"status": status,
"progress": progress,
"message": message,
"current_section": current_section,
"completed_sections": completed_sections or [],
"updated_at": datetime.now().isoformat()
}
with open(cls._get_progress_path(report_id), 'w', encoding='utf-8') as f:
json.dump(progress_data, f, ensure_ascii=False, indent=2)
@classmethod
def get_progress(cls, report_id: str) -> Optional[Dict[str, Any]]:
"""获取报告生成进度"""
path = cls._get_progress_path(report_id)
if not os.path.exists(path):
return None
with open(path, 'r', encoding='utf-8') as f:
return json.load(f)
@classmethod
def get_generated_sections(cls, report_id: str) -> List[Dict[str, Any]]:
"""
获取已生成的章节列表
返回所有已保存的章节文件信息
"""
folder = cls._get_report_folder(report_id)
if not os.path.exists(folder):
return []
sections = []
for filename in sorted(os.listdir(folder)):
if filename.startswith('section_') and filename.endswith('.md'):
file_path = os.path.join(folder, filename)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 从文件名解析章节索引
parts = filename.replace('.md', '').split('_')
section_index = int(parts[1])
sections.append({
"filename": filename,
"section_index": section_index,
"content": content
})
return sections
@classmethod
def assemble_full_report(cls, report_id: str, outline: ReportOutline) -> str:
"""
组装完整报告
从已保存的章节文件组装完整报告,并进行标题清理
"""
folder = cls._get_report_folder(report_id)
# 构建报告头部
md_content = f"# {outline.title}\n\n"
md_content += f"> {outline.summary}\n\n"
md_content += f"---\n\n"
# 按顺序读取所有章节文件
sections = cls.get_generated_sections(report_id)
for section_info in sections:
md_content += section_info["content"]
# 后处理:清理整个报告的标题问题
md_content = cls._post_process_report(md_content, outline)
# 保存完整报告
full_path = cls._get_report_markdown_path(report_id)
with open(full_path, 'w', encoding='utf-8') as f:
f.write(md_content)
logger.info(t('report.fullReportAssembled', reportId=report_id))
return md_content
@classmethod
def _post_process_report(cls, content: str, outline: ReportOutline) -> str:
"""
后处理报告内容
1. 移除重复的标题
2. 保留报告主标题(#)和章节标题(##),移除其他级别的标题(###, ####等)
3. 清理多余的空行和分隔线
Args:
content: 原始报告内容
outline: 报告大纲
Returns:
处理后的内容
"""
import re
lines = content.split('\n')
processed_lines = []
prev_was_heading = False
# 收集大纲中的所有章节标题
section_titles = set()
for section in outline.sections:
section_titles.add(section.title)
i = 0
while i < len(lines):
line = lines[i]
stripped = line.strip()
# 检查是否是标题行
heading_match = re.match(r'^(#{1,6})\s+(.+)$', stripped)
if heading_match:
level = len(heading_match.group(1))
title = heading_match.group(2).strip()
# 检查是否是重复标题在连续5行内出现相同内容的标题
is_duplicate = False
for j in range(max(0, len(processed_lines) - 5), len(processed_lines)):
prev_line = processed_lines[j].strip()
prev_match = re.match(r'^(#{1,6})\s+(.+)$', prev_line)
if prev_match:
prev_title = prev_match.group(2).strip()
if prev_title == title:
is_duplicate = True
break
if is_duplicate:
# 跳过重复标题及其后的空行
i += 1
while i < len(lines) and lines[i].strip() == '':
i += 1
continue
# 标题层级处理:
# - # (level=1) 只保留报告主标题
# - ## (level=2) 保留章节标题
# - ### 及以下 (level>=3) 转换为粗体文本
if level == 1:
if title == outline.title:
# 保留报告主标题
processed_lines.append(line)
prev_was_heading = True
elif title in section_titles:
# 章节标题错误使用了#,修正为##
processed_lines.append(f"## {title}")
prev_was_heading = True
else:
# 其他一级标题转为粗体
processed_lines.append(f"**{title}**")
processed_lines.append("")
prev_was_heading = False
elif level == 2:
if title in section_titles or title == outline.title:
# 保留章节标题
processed_lines.append(line)
prev_was_heading = True
else:
# 非章节的二级标题转为粗体
processed_lines.append(f"**{title}**")
processed_lines.append("")
prev_was_heading = False
else:
# ### 及以下级别的标题转换为粗体文本
processed_lines.append(f"**{title}**")
processed_lines.append("")
prev_was_heading = False
i += 1
continue
elif stripped == '---' and prev_was_heading:
# 跳过标题后紧跟的分隔线
i += 1
continue
elif stripped == '' and prev_was_heading:
# 标题后只保留一个空行
if processed_lines and processed_lines[-1].strip() != '':
processed_lines.append(line)
prev_was_heading = False
else:
processed_lines.append(line)
prev_was_heading = False
i += 1
# 清理连续的多个空行保留最多2个
result_lines = []
empty_count = 0
for line in processed_lines:
if line.strip() == '':
empty_count += 1
if empty_count <= 2:
result_lines.append(line)
else:
empty_count = 0
result_lines.append(line)
return '\n'.join(result_lines)
@classmethod
def save_report(cls, report: Report) -> None:
"""保存报告元信息和完整报告"""
cls._ensure_report_folder(report.report_id)
# 保存元信息JSON
with open(cls._get_report_path(report.report_id), 'w', encoding='utf-8') as f:
json.dump(report.to_dict(), f, ensure_ascii=False, indent=2)
# 保存大纲
if report.outline:
cls.save_outline(report.report_id, report.outline)
# 保存完整Markdown报告
if report.markdown_content:
with open(cls._get_report_markdown_path(report.report_id), 'w', encoding='utf-8') as f:
f.write(report.markdown_content)
logger.info(t('report.reportSaved', reportId=report.report_id))
@classmethod
def get_report(cls, report_id: str) -> Optional[Report]:
"""获取报告"""
path = cls._get_report_path(report_id)
if not os.path.exists(path):
# 兼容旧格式检查直接存储在reports目录下的文件
old_path = os.path.join(cls.REPORTS_DIR, f"{report_id}.json")
if os.path.exists(old_path):
path = old_path
else:
return None
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
# 重建Report对象
outline = None
if data.get('outline'):
outline_data = data['outline']
sections = []
for s in outline_data.get('sections', []):
sections.append(ReportSection(
title=s['title'],
content=s.get('content', '')
))
outline = ReportOutline(
title=outline_data['title'],
summary=outline_data['summary'],
sections=sections
)
# 如果markdown_content为空尝试从full_report.md读取
markdown_content = data.get('markdown_content', '')
if not markdown_content:
full_report_path = cls._get_report_markdown_path(report_id)
if os.path.exists(full_report_path):
with open(full_report_path, 'r', encoding='utf-8') as f:
markdown_content = f.read()
return Report(
report_id=data['report_id'],
simulation_id=data['simulation_id'],
graph_id=data['graph_id'],
simulation_requirement=data['simulation_requirement'],
status=ReportStatus(data['status']),
outline=outline,
markdown_content=markdown_content,
created_at=data.get('created_at', ''),
completed_at=data.get('completed_at', ''),
error=data.get('error')
)
@classmethod
def get_report_by_simulation(cls, simulation_id: str) -> Optional[Report]:
"""根据模拟ID获取报告"""
cls._ensure_reports_dir()
for item in os.listdir(cls.REPORTS_DIR):
item_path = os.path.join(cls.REPORTS_DIR, item)
# 新格式:文件夹
if os.path.isdir(item_path):
report = cls.get_report(item)
if report and report.simulation_id == simulation_id:
return report
# 兼容旧格式JSON文件
elif item.endswith('.json'):
report_id = item[:-5]
report = cls.get_report(report_id)
if report and report.simulation_id == simulation_id:
return report
return None
@classmethod
def list_reports(cls, simulation_id: Optional[str] = None, limit: int = 50) -> List[Report]:
"""列出报告"""
cls._ensure_reports_dir()
reports = []
for item in os.listdir(cls.REPORTS_DIR):
item_path = os.path.join(cls.REPORTS_DIR, item)
# 新格式:文件夹
if os.path.isdir(item_path):
report = cls.get_report(item)
if report:
if simulation_id is None or report.simulation_id == simulation_id:
reports.append(report)
# 兼容旧格式JSON文件
elif item.endswith('.json'):
report_id = item[:-5]
report = cls.get_report(report_id)
if report:
if simulation_id is None or report.simulation_id == simulation_id:
reports.append(report)
# 按创建时间倒序
reports.sort(key=lambda r: r.created_at, reverse=True)
return reports[:limit]
@classmethod
def delete_report(cls, report_id: str) -> bool:
"""删除报告(整个文件夹)"""
import shutil
folder_path = cls._get_report_folder(report_id)
# 新格式:删除整个文件夹
if os.path.exists(folder_path) and os.path.isdir(folder_path):
shutil.rmtree(folder_path)
logger.info(t('report.reportFolderDeleted', reportId=report_id))
return True
# 兼容旧格式:删除单独的文件
deleted = False
old_json_path = os.path.join(cls.REPORTS_DIR, f"{report_id}.json")
old_md_path = os.path.join(cls.REPORTS_DIR, f"{report_id}.md")
if os.path.exists(old_json_path):
os.remove(old_json_path)
deleted = True
if os.path.exists(old_md_path):
os.remove(old_md_path)
deleted = True
return deleted