You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.6 KiB
125 lines
3.6 KiB
import 'package:get/get.dart';
|
|
import '../../../data/services/volcano_ai_service.dart';
|
|
import '../controllers/translation_controller.dart';
|
|
import '../controllers/translation_history_controller.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
|
|
class TranslationDetailController extends GetxController {
|
|
// 依赖注入
|
|
final VolcanoAIService _aiService = Get.find<VolcanoAIService>();
|
|
final TranslationHistoryController historyController = Get.find<TranslationHistoryController>();
|
|
final GetStorage _storage = GetStorage();
|
|
|
|
// 存储键前缀
|
|
static const String _summaryKeyPrefix = 'translation_summary_';
|
|
|
|
// 状态变量
|
|
final selectedTabIndex = 0.obs;
|
|
final isGeneratingSummary = false.obs;
|
|
final summaryResult = ''.obs;
|
|
|
|
// 当前会话信息
|
|
late final TranslationItem currentItem;
|
|
late final List<TranslationItem> sessionItems;
|
|
|
|
// 初始化方法
|
|
void initialize(TranslationItem item, List<TranslationItem> items) {
|
|
currentItem = item;
|
|
sessionItems = items;
|
|
|
|
// 尝试从存储中加载摘要
|
|
loadSavedSummary();
|
|
}
|
|
|
|
// 加载保存的摘要
|
|
void loadSavedSummary() {
|
|
final String? savedSummary = _storage.read<String>(_getSummaryKey());
|
|
if (savedSummary != null && savedSummary.isNotEmpty) {
|
|
summaryResult.value = savedSummary;
|
|
}
|
|
}
|
|
|
|
// 保存摘要
|
|
void saveSummary(String summary) {
|
|
if (summary.isNotEmpty) {
|
|
_storage.write(_getSummaryKey(), summary);
|
|
}
|
|
}
|
|
|
|
// 获取摘要的存储键
|
|
String _getSummaryKey() {
|
|
return _summaryKeyPrefix + currentItem.sessionId;
|
|
}
|
|
|
|
// 切换选项卡
|
|
void switchTab(int index) {
|
|
selectedTabIndex.value = index;
|
|
}
|
|
|
|
// 播放翻译项
|
|
void playTranslationItem(TranslationItem item) {
|
|
historyController.playTranslationItem(item);
|
|
}
|
|
|
|
// 播放文本
|
|
void playText(String text) {
|
|
historyController.playText(text);
|
|
}
|
|
|
|
// 获取合并的源文本
|
|
String getCombinedSourceText() {
|
|
return sessionItems.map((e) => e.sourceText).join('\n\n');
|
|
}
|
|
|
|
// 获取合并的翻译文本
|
|
String getCombinedTranslatedText() {
|
|
return sessionItems.map((e) => e.translatedText).join('\n\n');
|
|
}
|
|
|
|
// 提取摘要
|
|
Future<String> extractSummary(String text) async {
|
|
isGeneratingSummary.value = true;
|
|
|
|
try {
|
|
// 构建提示词
|
|
final messages = [
|
|
{'role': 'user', 'content': '请为以下文本生成一个简洁的摘要,突出重点内容:\n\n$text'},
|
|
];
|
|
|
|
// 系统提示词
|
|
const systemPrompt = '''
|
|
你是一个专业的文本摘要助手,擅长从会议记录、对话和文本中提取关键信息并生成简洁的摘要。
|
|
请按照以下格式生成摘要:
|
|
|
|
1. 首先用1-2句话对整体内容进行简要概括
|
|
2. 然后使用项目符号列表(•)列出关键点或重要信息
|
|
3. 如果是对话内容,请特别标注出讨论的结论或达成的共识
|
|
|
|
请遵循以下原则:
|
|
1. 保留原文的核心信息和关键点
|
|
2. 使用简洁、清晰的语言
|
|
3. 保持客观,不添加个人观点
|
|
4. 摘要总长度应该是原文的1/3或更短
|
|
5. 确保列表中的每个要点都简明扼要,突出重点
|
|
''';
|
|
|
|
// 调用AI服务生成摘要
|
|
final result = await _aiService.sendMessage(
|
|
messages: messages,
|
|
systemPrompt: systemPrompt,
|
|
);
|
|
|
|
summaryResult.value = result;
|
|
|
|
// 保存摘要
|
|
saveSummary(result);
|
|
|
|
return result;
|
|
} catch (e) {
|
|
summaryResult.value = '生成摘要失败: ${e.toString()}';
|
|
return '生成摘要失败: ${e.toString()}';
|
|
} finally {
|
|
isGeneratingSummary.value = false;
|
|
}
|
|
}
|
|
}
|