feat: 完善词典子图,添加API调用和前端格式化工具
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m5s
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m5s
- 完善词典子图:添加生词本功能 - 创建API调用工具:dictionary_api - 添加前端格式化展示工具:result_formatter.py - 创建通讯录和资讯子图的基本结构 - 更新主图状态结构,添加MainGraphState - 添加subgraph_builder.py用于子图集成
This commit is contained in:
218
frontend/src/components/result_formatter.py
Normal file
218
frontend/src/components/result_formatter.py
Normal file
@@ -0,0 +1,218 @@
|
||||
"""
|
||||
前端格式化展示工具
|
||||
Frontend Formatter and Display Utilities
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, Optional, List
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class FormattedResult:
|
||||
"""格式化后的结果结构"""
|
||||
# 类型:"text", "card", "list", "table"
|
||||
type: str
|
||||
|
||||
# 内容
|
||||
title: str = ""
|
||||
content: str = ""
|
||||
|
||||
# 卡片列表
|
||||
items: List[Dict[str, Any]] = None
|
||||
|
||||
# 元数据
|
||||
metadata: Dict[str, Any] = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.items is None:
|
||||
self.items = []
|
||||
if self.metadata is None:
|
||||
self.metadata = {}
|
||||
|
||||
|
||||
class DictionaryResultFormatter:
|
||||
"""
|
||||
词典子图结果格式化器
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def format_word_entry(word: str, phonetic: str, part_of_speech: str,
|
||||
definitions: List[str], examples: List[str]) -> FormattedResult:
|
||||
"""
|
||||
格式化单词查询结果
|
||||
"""
|
||||
result = FormattedResult(type="card", title=f"📚 {word}")
|
||||
|
||||
content = []
|
||||
if phonetic:
|
||||
content.append(f"🔊 {phonetic}")
|
||||
content.append(f"🏷️ {part_of_speech}")
|
||||
content.append("")
|
||||
content.append("📖 释义:")
|
||||
for i, definition in enumerate(definitions, 1):
|
||||
content.append(f" {i}. {definition}")
|
||||
|
||||
if examples:
|
||||
content.append("")
|
||||
content.append("💡 例句:")
|
||||
for example in examples:
|
||||
content.append(f" \"{example}\"")
|
||||
|
||||
result.content = "\n".join(content)
|
||||
|
||||
# 添加快捷按钮建议
|
||||
result.metadata["suggestions"] = [
|
||||
"添加到生词本",
|
||||
"查看更多例句",
|
||||
"发音练习"
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def format_translation(source_text: str, translated_text: str, confidence: float) -> FormattedResult:
|
||||
"""
|
||||
格式化翻译结果
|
||||
"""
|
||||
result = FormattedResult(type="card", title="🔄 翻译结果")
|
||||
|
||||
content = []
|
||||
content.append(f"原文:{source_text}")
|
||||
content.append(f"译文:{translated_text}")
|
||||
content.append("")
|
||||
content.append(f"🎯 置信度:{confidence:.0%}")
|
||||
|
||||
result.content = "\n".join(content)
|
||||
|
||||
# 添加快捷按钮建议
|
||||
result.metadata["suggestions"] = [
|
||||
"复制译文",
|
||||
"重新翻译",
|
||||
"发音练习"
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def format_daily_word(word: str, phonetic: str, part_of_speech: str,
|
||||
definitions: List[str], examples: List[str]) -> FormattedResult:
|
||||
"""
|
||||
格式化每日一词结果
|
||||
"""
|
||||
result = FormattedResult(type="card", title="🌟 每日一词")
|
||||
|
||||
content = []
|
||||
content.append(f"📚 {word}")
|
||||
if phonetic:
|
||||
content.append(f"🔊 {phonetic}")
|
||||
content.append(f"🏷️ {part_of_speech}")
|
||||
content.append("")
|
||||
|
||||
if definitions:
|
||||
content.append("📖 释义:")
|
||||
for i, definition in enumerate(definitions, 1):
|
||||
content.append(f" {i}. {definition}")
|
||||
|
||||
if examples:
|
||||
content.append("")
|
||||
content.append("💡 例句:")
|
||||
for example in examples:
|
||||
content.append(f" \"{example}\"")
|
||||
|
||||
result.content = "\n".join(content)
|
||||
|
||||
# 添加快捷按钮建议
|
||||
result.metadata["suggestions"] = [
|
||||
"添加到生词本",
|
||||
"用这个词造句",
|
||||
"查看更多单词"
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def format_extracted_terms(terms: List[Dict[str, Any]]) -> FormattedResult:
|
||||
"""
|
||||
格式化提取的术语结果
|
||||
"""
|
||||
result = FormattedResult(type="list", title="📋 提取的术语")
|
||||
result.items = terms
|
||||
|
||||
content = []
|
||||
for i, term in enumerate(terms, 1):
|
||||
content.append(f"{i}. {term.get('term', '')}")
|
||||
content.append(f" 【{term.get('type', '')}】")
|
||||
content.append(f" {term.get('definition', '')}")
|
||||
if term.get('confidence'):
|
||||
content.append(f" 🎯 {term.get('confidence', 0.0):.0%}")
|
||||
content.append("")
|
||||
|
||||
result.content = "\n".join(content)
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def format_word_book(words: List[Dict[str, Any]]) -> FormattedResult:
|
||||
"""
|
||||
格式化生词本结果
|
||||
"""
|
||||
result = FormattedResult(type="list", title="📚 我的生词本")
|
||||
result.items = words
|
||||
|
||||
if words:
|
||||
content = []
|
||||
content.append(f"共 {len(words)} 个单词")
|
||||
content.append("")
|
||||
for i, word in enumerate(words, 1):
|
||||
word_text = word.get('word', '')
|
||||
added_at = word.get('added_at', '')
|
||||
if added_at:
|
||||
content.append(f"{i}. {word_text} (添加于:{added_at})")
|
||||
else:
|
||||
content.append(f"{i}. {word_text}")
|
||||
|
||||
result.content = "\n".join(content)
|
||||
else:
|
||||
result.content = "生词本为空\n\n💡 提示:查询单词后可添加到生词本"
|
||||
|
||||
# 添加快捷按钮建议
|
||||
result.metadata["suggestions"] = [
|
||||
"复习单词",
|
||||
"清空生词本",
|
||||
"导出单词"
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class ContactResultFormatter:
|
||||
"""
|
||||
通讯录子图结果格式化器
|
||||
"""
|
||||
# TODO: 实现通讯录结果格式化
|
||||
pass
|
||||
|
||||
|
||||
class NewsResultFormatter:
|
||||
"""
|
||||
资讯子图结果格式化器
|
||||
"""
|
||||
# TODO: 实现资讯结果格式化
|
||||
pass
|
||||
|
||||
|
||||
# 快捷工具
|
||||
def format_dictionary_output(output: str) -> str:
|
||||
"""
|
||||
快速格式化词典输出
|
||||
把纯文本包装成更好的展示格式
|
||||
"""
|
||||
return output # 暂时保持原样,后续可以加入更多格式处理
|
||||
|
||||
|
||||
# 导出快捷工具
|
||||
formatter = {
|
||||
"dictionary": DictionaryResultFormatter(),
|
||||
"contact": ContactResultFormatter(),
|
||||
"news": NewsResultFormatter()
|
||||
}
|
||||
Reference in New Issue
Block a user