chat_repository.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import 'dart:convert';
  2. import 'package:injectable/injectable.dart';
  3. import '../../base/base_response.dart';
  4. import '../../utils/atmob_log.dart';
  5. import '../../utils/http_handler.dart';
  6. import '../../utils/sse_parse_util.dart';
  7. import '../api/atmob_stream_api.dart';
  8. import '../api/request/deep_seek_chat_request.dart';
  9. @lazySingleton
  10. class ChatRepository {
  11. final AtmobStreamApi atmobStreamApi;
  12. var tag = "ChatRepository";
  13. ChatRepository(this.atmobStreamApi) {
  14. AtmobLog.d(tag, '$tag....init');
  15. }
  16. Future<Stream<Message>> streamDeepSeek(String chatContent) {
  17. return atmobStreamApi
  18. .deepSeekChat(DeepSeekChatRequest(chatContent))
  19. .then((response) async {
  20. List<String>? contentType = response.headers['Content-Type'];
  21. if (contentType != null) {
  22. for (var value in contentType) {
  23. if (value.contains('text/event-stream')) {
  24. return response.stream;
  25. } else if (value.contains('application/json')) {
  26. BaseResponse<String> baseResponse = BaseResponse.fromJson(
  27. jsonDecode(
  28. await response.stream
  29. .map((bytes) => utf8.decoder.convert(bytes))
  30. .toList()
  31. .then((value) => value.join()),
  32. ),
  33. (json) => json as String,
  34. );
  35. throw ServerErrorException(
  36. baseResponse.code,
  37. baseResponse.message,
  38. );
  39. }
  40. }
  41. }
  42. throw Exception('Invalid content type');
  43. })
  44. .then((stream) => SSEParseUtil.parse(stream));
  45. }
  46. }