|
|
|
@ -126,7 +126,7 @@ class AgentController extends GetxController { |
|
|
|
String _currentResponseId = ''; |
|
|
|
|
|
|
|
final TAG = 'AgentController'; |
|
|
|
|
|
|
|
var AppDir = "".obs; |
|
|
|
// 会话ID |
|
|
|
final String sessionId = 'default_agent'; |
|
|
|
|
|
|
|
@ -153,6 +153,7 @@ class AgentController extends GetxController { |
|
|
|
@override |
|
|
|
void onInit() { |
|
|
|
super.onInit(); |
|
|
|
_initAppDir(); |
|
|
|
_loadTtsPreference(); //加载TTS偏好设置 |
|
|
|
_setupScrollListener(); //设置滚动监听器 |
|
|
|
_loadChatHistory(); //加载聊天历史记录 |
|
|
|
@ -164,6 +165,10 @@ class AgentController extends GetxController { |
|
|
|
startVoiceInput(); //开启语音输入 |
|
|
|
} |
|
|
|
|
|
|
|
Future<void> _initAppDir() async { |
|
|
|
AppDir.value = (await getApplicationDocumentsDirectory()).path; |
|
|
|
} |
|
|
|
|
|
|
|
// 初始化音乐播放状态 |
|
|
|
Future<void> _initMusicStatus() async { |
|
|
|
try { |
|
|
|
@ -291,7 +296,7 @@ class AgentController extends GetxController { |
|
|
|
page: 1, //从第一页开始 |
|
|
|
pageSize: pageSize, //每页20条记录 |
|
|
|
); |
|
|
|
|
|
|
|
Logger.i(TAG, '加载聊天历史记录: $result'); |
|
|
|
if (result.messages.isNotEmpty) { |
|
|
|
// 转换并添加消息到列表 |
|
|
|
final historyMessages = _convertChatMessagesToMessages(result.messages); |
|
|
|
@ -303,7 +308,8 @@ class AgentController extends GetxController { |
|
|
|
// 更新分页状态 |
|
|
|
currentPage.value = 1; |
|
|
|
hasMoreMessages.value = result.totalPages > 1; |
|
|
|
|
|
|
|
print( |
|
|
|
'liwei-------------读取历史记录 ${AppDir.value}${historyMessages[0].imagePath}'); |
|
|
|
Logger.i(TAG, |
|
|
|
'成功加载 ${historyMessages.length} 条聊天记录,总页数: ${result.totalPages}'); |
|
|
|
_logMessageDetails(historyMessages); |
|
|
|
@ -421,7 +427,7 @@ class AgentController extends GetxController { |
|
|
|
'meta: ${msg.meta != null ? "有" : "无"}' |
|
|
|
'}'); |
|
|
|
} |
|
|
|
Logger.d(TAG, '===== 历史消息详情结束 ====='); |
|
|
|
Logger.d(TAG, '===== 历史消息详情结束 ==lishi==='); |
|
|
|
} |
|
|
|
|
|
|
|
// 设置滚动监听器 |
|
|
|
@ -903,10 +909,14 @@ class AgentController extends GetxController { |
|
|
|
} |
|
|
|
|
|
|
|
// 复制图片到应用永久存储目录 |
|
|
|
final permanentImagePath = await _copyImageToPermanentStorage(imagePath); |
|
|
|
// 复制图片到应用永久存储目录 |
|
|
|
final imagePathResult = await _copyImageToPermanentStorage(imagePath); |
|
|
|
final permanentImagePath = imagePathResult['fullPath']!; |
|
|
|
final relativeImagePath = imagePathResult['relativePath']!; |
|
|
|
var ImageUrl = await UploadOss.upload( |
|
|
|
filepath: imagePath, rootDir: 'User/${User.instance.uid}/ChatImages'); |
|
|
|
Logger.i(TAG, '图片上传: $imagePath URL: $ImageUrl'); |
|
|
|
filepath: permanentImagePath, |
|
|
|
rootDir: 'User/${User.instance.uid}/ChatImages'); |
|
|
|
Logger.i(TAG, '图片上传: $permanentImagePath URL: $ImageUrl'); |
|
|
|
// 显示的文本,如果没有提供则使用默认值 |
|
|
|
final displayText = text?.isNotEmpty == true ? text! : '[图片]'; |
|
|
|
|
|
|
|
@ -915,7 +925,7 @@ class AgentController extends GetxController { |
|
|
|
isUser: true, |
|
|
|
text: displayText, |
|
|
|
hasImage: true, |
|
|
|
imagePath: permanentImagePath, |
|
|
|
imagePath: relativeImagePath, |
|
|
|
); |
|
|
|
messages.add(message); |
|
|
|
|
|
|
|
@ -935,7 +945,7 @@ class AgentController extends GetxController { |
|
|
|
|
|
|
|
// 调用Agent Service处理图片,考虑TTS状态 |
|
|
|
await AgentService.processImageInput( |
|
|
|
permanentImagePath, |
|
|
|
relativeImagePath, |
|
|
|
ImageUrl, |
|
|
|
text: text ?? '', |
|
|
|
speakResponse: isTtsEnabled.value, |
|
|
|
@ -948,12 +958,16 @@ class AgentController extends GetxController { |
|
|
|
} |
|
|
|
|
|
|
|
// 将图片复制到应用的永久存储目录 |
|
|
|
Future<String> _copyImageToPermanentStorage(String sourcePath) async { |
|
|
|
Future<Map<String, String>> _copyImageToPermanentStorage( |
|
|
|
String sourcePath) async { |
|
|
|
try { |
|
|
|
final File sourceFile = File(sourcePath); |
|
|
|
if (!await sourceFile.exists()) { |
|
|
|
Logger.e(TAG, '源图片文件不存在: $sourcePath'); |
|
|
|
return sourcePath; // 如果源文件不存在,返回原路径 |
|
|
|
return { |
|
|
|
'fullPath': sourcePath, |
|
|
|
'relativePath': sourcePath |
|
|
|
}; // 如果源文件不存在,返回原路径 |
|
|
|
} |
|
|
|
|
|
|
|
// 创建应用文档目录下的chatImages文件夹 |
|
|
|
@ -967,15 +981,16 @@ class AgentController extends GetxController { |
|
|
|
final fileName = |
|
|
|
'${DateTime.now().millisecondsSinceEpoch}_${sourcePath.split('/').last}'; |
|
|
|
final targetPath = '${imagesDir.path}/$fileName'; |
|
|
|
final relativePath = '/chatImages/$fileName'; |
|
|
|
|
|
|
|
// 复制文件 |
|
|
|
await sourceFile.copy(targetPath); |
|
|
|
Logger.i(TAG, '图片已复制到永久存储: $targetPath'); |
|
|
|
|
|
|
|
return targetPath; |
|
|
|
return {'fullPath': targetPath, 'relativePath': relativePath}; |
|
|
|
} catch (e) { |
|
|
|
Logger.e(TAG, '复制图片到永久存储失败: $e'); |
|
|
|
return sourcePath; // 出错时返回原路径 |
|
|
|
return {'fullPath': sourcePath, 'relativePath': sourcePath}; // 出错时返回原路径 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|