intimacy_analyze_repository.dart 5.9 KB

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