Skip to content

Commit 4037e88

Browse files
authored
refactor(template-tools/frontend) 优化模板操作工具,前端适配手机 (#120)
* refactor(template-tools): 优化模板操作工具,更新章节处理逻辑,确保章节名称为必需项 * feat(introduction): 添加滚动容器以改善页面布局,优化样式以适应不同屏幕尺寸
1 parent d2a1a36 commit 4037e88

File tree

5 files changed

+328
-396
lines changed

5 files changed

+328
-396
lines changed

backend/ai_system/core_agents/main_agent.py

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,10 @@ def _setup(self):
142142
system_content += f" * writemd工具:支持多种模式(覆盖、追加、修改、插入、智能替换、章节更新)\n"
143143
system_content += f" * update_template工具:专门用于模板操作,支持章节级别更新\n"
144144
system_content += f"- 模板操作工具,让AI更方便地操作模板:\n"
145-
system_content += f" * extract_headers: 快速提取模板中的所有标题,了解结构\n"
146-
system_content += f" * get_structure_summary: 获取模板结构摘要\n"
147145
system_content += f" * analyze_template: 深度分析模板结构和内容\n"
148146
system_content += f" * get_section_content: 查看指定章节内容\n"
149-
system_content += f" * update_section_content: 更新章节内容(支持多种模式)\n"
150-
system_content += f" * add_new_section: 添加新章节\n"
147+
system_content += f" * update_section_content: 更新章节内容\n"
148+
system_content += f" * add_section: 添加新章节\n"
151149
system_content += f"- 生成论文时必须严格遵循模板的格式、结构和风格\n"
152150
system_content += f"- 如果模板有特定的章节要求,请保持这些章节结构\n"
153151
system_content += f"- 最终论文应该是一个完整的、格式规范的学术文档\n"
@@ -199,15 +197,15 @@ def _setup(self):
199197
"type": "function",
200198
"function": {
201199
"name": "update_template",
202-
"description": "专门用于更新论文文件的工具,支持章节级别的更新",
200+
"description": "专门用于更新论文文件的工具,只支持章节级别更新,必须指定章节名称",
203201
"parameters": {
204202
"type": "object",
205203
"properties": {
206204
"template_name": {"type": "string", "description": "论文文件名,默认为paper.md"},
207205
"content": {"type": "string", "description": "要更新的内容"},
208-
"section": {"type": "string", "description": "要更新的章节名称(可选)"}
206+
"section": {"type": "string", "description": "要更新的章节名称(必需)"}
209207
},
210-
"required": ["content"],
208+
"required": ["content", "section"],
211209
},
212210
},
213211
}
@@ -273,45 +271,18 @@ def _setup(self):
273271
},
274272
}
275273

276-
add_new_section_tool = {
274+
add_section_tool = {
277275
"type": "function",
278276
"function": {
279-
"name": "add_new_section",
280-
"description": "在paper.md文件中指定父章节下添加新章节",
277+
"name": "add_section",
278+
"description": "在paper.md文件末尾添加新章节",
281279
"parameters": {
282280
"type": "object",
283281
"properties": {
284-
"parent_section": {"type": "string", "description": "父章节标题"},
285282
"section_title": {"type": "string", "description": "新章节标题"},
286283
"content": {"type": "string", "description": "新章节内容", "default": ""}
287284
},
288-
"required": ["parent_section", "section_title"],
289-
},
290-
},
291-
}
292-
293-
extract_headers_tool = {
294-
"type": "function",
295-
"function": {
296-
"name": "extract_headers",
297-
"description": "从paper.md文件中提取所有标题信息,快速了解文档结构",
298-
"parameters": {
299-
"type": "object",
300-
"properties": {},
301-
"required": [],
302-
},
303-
},
304-
}
305-
306-
get_structure_summary_tool = {
307-
"type": "function",
308-
"function": {
309-
"name": "get_structure_summary",
310-
"description": "获取paper.md文件的内容结构摘要,显示所有标题的层级关系",
311-
"parameters": {
312-
"type": "object",
313-
"properties": {},
314-
"required": [],
285+
"required": ["section_title"],
315286
},
316287
},
317288
}
@@ -330,13 +301,7 @@ def _setup(self):
330301
"analyze_template": self.template_agent_tools.analyze_template,
331302
"get_section_content": self.template_agent_tools.get_section_content,
332303
"update_section_content": self.template_agent_tools.update_section_content,
333-
"add_new_section": self.template_agent_tools.add_new_section,
334-
"remove_section": self.template_agent_tools.remove_section,
335-
"reorder_sections": self.template_agent_tools.reorder_sections,
336-
"format_template": self.template_agent_tools.format_template,
337-
"get_template_help": self.template_agent_tools.get_template_help,
338-
"extract_headers": self.template_agent_tools.extract_headers_from_content,
339-
"get_structure_summary": self.template_agent_tools.get_content_structure_summary
304+
"add_section": self.template_agent_tools.add_section
340305
})
341306

342307
# 将模板工具定义添加到tools列表
@@ -348,9 +313,7 @@ def _setup(self):
348313
analyze_template_tool,
349314
get_section_content_tool,
350315
update_section_content_tool,
351-
add_new_section_tool,
352-
extract_headers_tool,
353-
get_structure_summary_tool
316+
add_section_tool
354317
]
355318
else:
356319
# 没有模板时,只注册基础工具

backend/ai_system/core_tools/file_tools.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,39 +156,36 @@ async def writemd(self, filename: str, content: str, mode: str = "overwrite") ->
156156

157157
return error_msg
158158

159-
async def update_template(self, template_name: str = "paper.md", content: str = "", section: Optional[str] = None) -> str:
159+
async def update_template(self, template_name: str = "paper.md", content: str = "", section: str = "") -> str:
160160
"""
161-
专门用于更新论文文件的工具方法
162-
161+
专门用于更新论文文件的工具方法,只支持章节级别更新
162+
163163
Args:
164164
template_name: 论文文件名,默认为paper.md
165165
content: 要更新的内容
166-
section: 要更新的章节名称(可选
167-
166+
section: 要更新的章节名称(必需
167+
168168
Returns:
169169
操作结果信息
170170
"""
171171
try:
172172
file_path = os.path.join(self.workspace_dir, template_name)
173-
173+
174174
if not os.path.exists(file_path):
175175
return f"模板文件不存在: {template_name}"
176-
176+
177+
if not section.strip():
178+
return f"错误:必须指定章节名称。update_template工具只支持章节级别更新,不支持全文覆盖。"
179+
177180
# 读取原模板内容
178181
with open(file_path, 'r', encoding='utf-8') as f:
179182
original_content = f.read()
180-
181-
if section:
182-
# 如果指定了章节,尝试更新特定章节
183-
updated_content = self._update_section_content(original_content, section, content)
184-
with open(file_path, 'w', encoding='utf-8') as f:
185-
f.write(updated_content)
186-
result = f"成功更新论文文件 {template_name} 的章节 '{section}'"
187-
else:
188-
# 更新整个论文内容
189-
with open(file_path, 'w', encoding='utf-8') as f:
190-
f.write(content)
191-
result = f"成功更新论文文件: {template_name}"
183+
184+
# 更新指定章节
185+
updated_content = self._update_section_content(original_content, section, content)
186+
with open(file_path, 'w', encoding='utf-8') as f:
187+
f.write(updated_content)
188+
result = f"成功更新论文文件 {template_name} 的章节 '{section}'"
192189

193190
# 获取文件信息
194191
file_size = os.path.getsize(file_path)

0 commit comments

Comments
 (0)