11from app .core .agents .agent import Agent
22from app .core .llm .llm import LLM
33from app .core .prompts import get_writer_prompt
4- from app .utils .enums import CompTemplate , FormatOutPut
5- from app .models .user_output import UserOutput
4+ from app .schemas .enums import CompTemplate , FormatOutPut
65from app .tools .openalex_scholar import OpenAlexScholar
76from app .utils .log_util import logger
8- from app .utils .redis_manager import redis_manager
9- from app .schemas .response import SystemMessage
7+ from app .services .redis_manager import redis_manager
8+ from app .schemas .response import SystemMessage , WriterMessage
109import json
1110from app .core .functions import writer_tools
12- from app .utils .common_utils import get_footnotes
11+ from app .utils .common_utils import split_footnotes
1312from icecream import ic
14- from app .models . model import WriterResponse
13+ from app .schemas . A2A import WriterResponse
1514
1615
1716# 长文本
@@ -27,13 +26,14 @@ def __init__(
2726 max_chat_turns : int = 10 , # 添加最大对话轮次限制
2827 comp_template : CompTemplate = CompTemplate ,
2928 format_output : FormatOutPut = FormatOutPut .Markdown ,
30- user_output : UserOutput = None ,
3129 scholar : OpenAlexScholar = None ,
30+ max_memory : int = 25 , # 添加最大记忆轮次
3231 ) -> None :
33- super ().__init__ (task_id , model , max_chat_turns , user_output )
32+ super ().__init__ (task_id , model , max_chat_turns , max_memory )
3433 self .format_out_put = format_output
3534 self .comp_template = comp_template
3635 self .scholar = scholar
36+ self .is_first_run = True
3737 self .system_prompt = get_writer_prompt (format_output )
3838 self .available_images : list [str ] = []
3939
@@ -52,19 +52,21 @@ async def run(
5252 """
5353 logger .info (f"subtitle是:{ sub_title } " )
5454
55+ if self .is_first_run :
56+ self .is_first_run = False
57+ self .append_chat_history ({"role" : "system" , "content" : self .system_prompt })
58+
5559 if available_images :
5660 self .available_images = available_images
5761 # 拼接成完整URL
5862 image_list = "," .join (available_images )
5963 image_prompt = f"\n 可用的图片链接列表:\n { image_list } \n 请在写作时适当引用这些图片链接。"
60- ic ( image_prompt )
64+ logger . info ( f"image_prompt是: { image_prompt } " )
6165 prompt = prompt + image_prompt
6266
6367 logger .info (f"{ self .__class__ .__name__ } :开始:执行对话" )
6468 self .current_chat_turns += 1 # 重置对话轮次计数器
6569
66- # 更新对话历史
67- self .append_chat_history ({"role" : "system" , "content" : self .system_prompt })
6870 self .append_chat_history ({"role" : "user" , "content" : prompt })
6971
7072 # 获取历史消息用于本次对话
@@ -76,6 +78,8 @@ async def run(
7678 sub_title = sub_title ,
7779 )
7880
81+ footnotes = []
82+
7983 if (
8084 hasattr (response .choices [0 ].message , "tool_calls" )
8185 and response .choices [0 ].message .tool_calls
@@ -92,7 +96,14 @@ async def run(
9296 )
9397
9498 query = json .loads (tool_call .function .arguments )["query" ]
95- footnotes = get_footnotes (query )
99+
100+ await redis_manager .publish_message (
101+ self .task_id ,
102+ WriterMessage (
103+ input = {"query" : query },
104+ ),
105+ )
106+
96107 full_content = response .choices [0 ].message .content
97108 # 更新对话历史 - 添加助手的响应
98109 self .append_chat_history (
@@ -113,10 +124,13 @@ async def run(
113124 )
114125
115126 try :
116- papers = self .scholar .search_papers (query )
127+ papers = await self .scholar .search_papers (query )
117128 except Exception as e :
118- logger .error (f"搜索文献失败: { str (e )} " )
119- return f"搜索文献失败: { str (e )} "
129+ error_msg = f"搜索文献失败: { str (e )} "
130+ logger .error (error_msg )
131+ return WriterResponse (
132+ response_content = error_msg , footnotes = footnotes
133+ )
120134 # TODO: pass to frontend
121135 papers_str = self .scholar .papers_to_str (papers )
122136 logger .info (f"搜索文献结果\n { papers_str } " )
@@ -136,6 +150,9 @@ async def run(
136150 sub_title = sub_title ,
137151 )
138152 response_content = next_response .choices [0 ].message .content
153+ main_text , footnotes = split_footnotes (response_content )
154+ logger .info (f"使用到的footnotes: { footnotes } " )
155+ response_content = main_text
139156 else :
140157 response_content = response .choices [0 ].message .content
141158 self .chat_history .append ({"role" : "assistant" , "content" : response_content })
0 commit comments