|
|
@@ -1,3 +1,5 @@
|
|
|
+import 'dart:async';
|
|
|
+import 'dart:convert';
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
@@ -21,6 +23,7 @@ import '../../../../utils/error_handler.dart';
|
|
|
import '../../../../utils/http_handler.dart';
|
|
|
import '../../../../utils/image_picker_util.dart';
|
|
|
import '../../../../utils/intimacy_analyze_config_helper.dart';
|
|
|
+import '../../../../utils/sse_parse_util.dart';
|
|
|
import '../../../../utils/upload/upload_file_manager.dart';
|
|
|
import '../../../../utils/upload/upload_scene_type.dart';
|
|
|
import '../../../store/store_page.dart';
|
|
|
@@ -66,6 +69,9 @@ class ScanImageReplyController extends BaseController {
|
|
|
/// 回复语气列表
|
|
|
RxList<String> replyToneList = <String>[].obs;
|
|
|
|
|
|
+ /// 对话分析的订阅(SSE)
|
|
|
+ StreamSubscription<Message>? _intimacyReplyAnalyzeSubscription;
|
|
|
+
|
|
|
ScanImageReplyController(
|
|
|
this.intimacyAnalyzeConfigHelper,
|
|
|
this.uploadFileManager,
|
|
|
@@ -80,6 +86,13 @@ class ScanImageReplyController extends BaseController {
|
|
|
_initReplyModeList();
|
|
|
}
|
|
|
|
|
|
+ @override
|
|
|
+ void onClose() {
|
|
|
+ // 取消SSE流数据订阅
|
|
|
+ _intimacyReplyAnalyzeSubscription?.cancel();
|
|
|
+ super.onClose();
|
|
|
+ }
|
|
|
+
|
|
|
/// 初始化回复语气列表
|
|
|
void _initReplyToneOptionSelectConfigList() {
|
|
|
// 语气列表
|
|
|
@@ -171,7 +184,8 @@ class ScanImageReplyController extends BaseController {
|
|
|
// 上传的图片后端地址
|
|
|
List<String> imageList = [uploadInfo.value?.fileBackendPath ?? ""];
|
|
|
// 选择的回复语气
|
|
|
- String title = currentSelectReplyToneOption.value?.name ?? "";
|
|
|
+ // String title = currentSelectReplyToneOption.value?.name ?? "";
|
|
|
+ String title = "高冷";
|
|
|
// 当前选择的回复模式
|
|
|
String mode = currentReplyMode.value?.value ?? "";
|
|
|
|
|
|
@@ -188,34 +202,56 @@ class ScanImageReplyController extends BaseController {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- // 请求分析截图
|
|
|
- IntimacyReplyAnalyzeResponse response = await intimacyAnalyzeRepository
|
|
|
- .intimacyReplyAnalyze(
|
|
|
- IntimacyReplyAnalyzeRequest(imageList, title, mode),
|
|
|
- );
|
|
|
- var newList =
|
|
|
- response.choices?.map((item) {
|
|
|
- return item.delta?.content ?? "";
|
|
|
- }) ??
|
|
|
- [];
|
|
|
- replyToneList.clear();
|
|
|
- replyToneList.addAll(newList);
|
|
|
- replyToneList.refresh();
|
|
|
- } catch (error) {
|
|
|
- AtmobLog.e(_tag, error.toString());
|
|
|
- if (error is ServerErrorException) {
|
|
|
- // 需要Vip权限
|
|
|
- if (error.code == 1005) {
|
|
|
- ToastUtil.show(error.message);
|
|
|
- StorePage.start();
|
|
|
+ StringBuffer buffer = StringBuffer();
|
|
|
+
|
|
|
+ replyToneList.clear();
|
|
|
+ replyToneList.refresh();
|
|
|
+
|
|
|
+ // 请求分析截图
|
|
|
+ Stream<Message> stream = await intimacyAnalyzeRepository
|
|
|
+ .intimacyReplyAnalyze(
|
|
|
+ IntimacyReplyAnalyzeRequest(imageList, title, mode),
|
|
|
+ );
|
|
|
+ _intimacyReplyAnalyzeSubscription = stream.listen(
|
|
|
+ (message) {
|
|
|
+ // 流数据更新
|
|
|
+ String json = message.data;
|
|
|
+ // 解析json为实体类
|
|
|
+ IntimacyReplyAnalyzeResponse response =
|
|
|
+ IntimacyReplyAnalyzeResponse.fromJson(jsonDecode(json));
|
|
|
+
|
|
|
+ List<String> contentList = response.choices?.map((item) {
|
|
|
+ return item.delta?.content ?? "";
|
|
|
+ }).toList() ?? [];
|
|
|
+ // 数组转字符串
|
|
|
+ String content = contentList.join("");
|
|
|
+
|
|
|
+ // 累计内容
|
|
|
+ buffer.write(content);
|
|
|
+
|
|
|
+ // 刷新列表
|
|
|
+ replyToneList.clear();
|
|
|
+ replyToneList.add(buffer.toString());
|
|
|
+ replyToneList.refresh();
|
|
|
+ },
|
|
|
+ onError: (error) {
|
|
|
+ AtmobLog.e(_tag, error.toString());
|
|
|
+ if (error is ServerErrorException) {
|
|
|
+ // 需要Vip权限
|
|
|
+ if (error.code == 1005) {
|
|
|
+ ToastUtil.show(error.message);
|
|
|
+ StorePage.start();
|
|
|
+ } else {
|
|
|
+ ToastUtil.show(error.message);
|
|
|
+ }
|
|
|
} else {
|
|
|
- ToastUtil.show(error.message);
|
|
|
+ ErrorHandler.toastError(error);
|
|
|
}
|
|
|
- } else {
|
|
|
- ErrorHandler.toastError(error);
|
|
|
- }
|
|
|
- }
|
|
|
+ },
|
|
|
+ onDone: () {
|
|
|
+ // 流关闭
|
|
|
+ },
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/// 删除上传信息
|