intimacy_analyze_repository.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:get/get_rx/src/rx_types/rx_types.dart';
  4. import 'package:injectable/injectable.dart';
  5. import 'package:keyboard/base/app_base_request.dart';
  6. import '../../base/base_response.dart';
  7. import '../../di/get_it.dart';
  8. import '../../utils/async_util.dart';
  9. import '../../utils/atmob_log.dart';
  10. import '../../utils/http_handler.dart';
  11. import '../../utils/sse_parse_util.dart';
  12. import '../api/atmob_api.dart';
  13. import '../api/atmob_stream_api.dart';
  14. import '../api/request/intimacy_analyze_request.dart';
  15. import '../api/request/intimacy_reply_analyze_request.dart';
  16. import '../api/request/intimacy_reply_chat_request.dart';
  17. import '../api/response/intimacy_analyze_chat_config_response.dart';
  18. import '../api/response/intimacy_analyze_config_response.dart';
  19. import '../api/response/intimacy_analyze_reply_config_response.dart';
  20. import '../api/response/intimacy_analyze_response.dart';
  21. import '../api/response/intimacy_chat_analyze_response.dart';
  22. import '../api/response/intimacy_reply_analyze_response.dart';
  23. /// 亲密度分析Repository层
  24. @LazySingleton()
  25. class IntimacyAnalyzeRepository {
  26. final String tag = "IntimacyAnalyzeRepository";
  27. final AtmobApi atmobApi;
  28. final AtmobStreamApi atmobStreamApi;
  29. /// 亲密度配置
  30. Rxn<IntimacyAnalyzeConfigResponse> intimacyAnalyzeConfig =
  31. Rxn<IntimacyAnalyzeConfigResponse>();
  32. /// 对话分析配置
  33. Rxn<IntimacyAnalyzeChatConfigResponse> intimacyAnalyzeChatConfig =
  34. Rxn<IntimacyAnalyzeChatConfigResponse>();
  35. /// 识图回复配置
  36. Rxn<IntimacyAnalyzeReplyConfigResponse> intimacyAnalyzeReplyConfig =
  37. Rxn<IntimacyAnalyzeReplyConfigResponse>();
  38. IntimacyAnalyzeRepository(this.atmobApi, this.atmobStreamApi) {
  39. AtmobLog.d(tag, '$tag...init');
  40. // 初始化时,刷新配置
  41. _refreshIntimacyAnalyzeConfig();
  42. _refreshIntimacyAnalyzeChatConfig();
  43. _refreshIntimacyAnalyzeReplyConfig();
  44. }
  45. /// 从Getx的依赖注入中,获取实例,适合在没有依赖注入上下文的地方使用
  46. IntimacyAnalyzeRepository getInstance() {
  47. return getIt.get<IntimacyAnalyzeRepository>();
  48. }
  49. /// 刷新亲密度配置
  50. void _refreshIntimacyAnalyzeConfig() {
  51. AsyncUtil.retry(
  52. () => requestIntimacyAnalyzeConfig(),
  53. Duration(seconds: 3),
  54. maxRetry: 100,
  55. ).then((IntimacyAnalyzeConfigResponse configResponse) {
  56. AtmobLog.d(tag, "获取亲密度配置成功: ${configResponse.toJson()}");
  57. intimacyAnalyzeConfig.value = configResponse;
  58. });
  59. }
  60. /// 刷新对话分析配置
  61. void _refreshIntimacyAnalyzeChatConfig() {
  62. AsyncUtil.retry(
  63. () => requestIntimacyAnalyzeChatConfig(),
  64. Duration(seconds: 3),
  65. maxRetry: 100,
  66. ).then((IntimacyAnalyzeChatConfigResponse configResponse) {
  67. AtmobLog.d(tag, "获取对话分析配置成功: ${configResponse.toJson()}");
  68. intimacyAnalyzeChatConfig.value = configResponse;
  69. });
  70. }
  71. /// 刷新识图回复配置
  72. void _refreshIntimacyAnalyzeReplyConfig() {
  73. AsyncUtil.retry(
  74. () => requestIntimacyAnalyzeReplyConfig(),
  75. Duration(seconds: 3),
  76. maxRetry: 100,
  77. ).then((IntimacyAnalyzeReplyConfigResponse configResponse) {
  78. AtmobLog.d(tag, "获取识图回复配置成功: ${configResponse.toJson()}");
  79. intimacyAnalyzeReplyConfig.value = configResponse;
  80. });
  81. }
  82. /// 获取亲密度配置
  83. Future<IntimacyAnalyzeConfigResponse> requestIntimacyAnalyzeConfig() {
  84. return atmobApi
  85. .getIntimacyAnalyzeConfig(AppBaseRequest())
  86. .then(HttpHandler.handle(true));
  87. }
  88. /// 获取对话分析配置
  89. Future<IntimacyAnalyzeChatConfigResponse> requestIntimacyAnalyzeChatConfig() {
  90. return atmobApi
  91. .getIntimacyAnalyzeChatConfig(AppBaseRequest())
  92. .then(HttpHandler.handle(true));
  93. }
  94. /// 对话分析(SSE流式)
  95. Future<Stream<Message>> intimacyChatAnalyze(IntimacyChatAnalyzeRequest request) {
  96. return atmobStreamApi
  97. .intimacyChatAnalyze(request)
  98. .then((response) async {
  99. List<String>? contentType = response.headers['Content-Type'];
  100. if (contentType != null) {
  101. for (var value in contentType) {
  102. if (value.contains('text/event-stream')) {
  103. return response.stream;
  104. } else if (value.contains('application/json')) {
  105. BaseResponse<String> baseResponse = BaseResponse.fromJson(
  106. jsonDecode(
  107. await response.stream
  108. .map((bytes) => utf8.decoder.convert(bytes))
  109. .toList()
  110. .then((value) => value.join()),
  111. ),
  112. (json) => json as String,
  113. );
  114. throw ServerErrorException(
  115. baseResponse.code,
  116. baseResponse.message,
  117. );
  118. }
  119. }
  120. }
  121. throw Exception('Invalid content type');
  122. })
  123. .then((stream) => SSEParseUtil.parse(stream));
  124. }
  125. /// 获取识图回复配置
  126. Future<IntimacyAnalyzeReplyConfigResponse>
  127. requestIntimacyAnalyzeReplyConfig() {
  128. return atmobApi
  129. .getIntimacyAnalyzeReplyConfig(AppBaseRequest())
  130. .then(HttpHandler.handle(true));
  131. }
  132. /// 分析亲密度
  133. Future<IntimacyAnalyzeResponse> getIntimacyAnalyze(
  134. IntimacyAnalyzeRequest request,
  135. ) {
  136. return atmobApi.getIntimacyAnalyze(request).then(HttpHandler.handle(true));
  137. }
  138. /// 识图回复(SSE流式)
  139. Future<Stream<Message>> intimacyReplyAnalyze(IntimacyReplyAnalyzeRequest request) {
  140. return atmobStreamApi
  141. .intimacyReplyAnalyze(request)
  142. .then((response) async {
  143. List<String>? contentType = response.headers['Content-Type'];
  144. if (contentType != null) {
  145. for (var value in contentType) {
  146. if (value.contains('text/event-stream')) {
  147. return response.stream;
  148. } else if (value.contains('application/json')) {
  149. BaseResponse<String> baseResponse = BaseResponse.fromJson(
  150. jsonDecode(
  151. await response.stream
  152. .map((bytes) => utf8.decoder.convert(bytes))
  153. .toList()
  154. .then((value) => value.join()),
  155. ),
  156. (json) => json as String,
  157. );
  158. throw ServerErrorException(
  159. baseResponse.code,
  160. baseResponse.message,
  161. );
  162. }
  163. }
  164. }
  165. throw Exception('Invalid content type');
  166. })
  167. .then((stream) => SSEParseUtil.parse(stream));
  168. }
  169. }