Skip to content

Commit

Permalink
解决 Web 端在 iOS 下出现的 WebSocket 连接失败,导致聊天失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Apr 11, 2024
1 parent a0828da commit 654bcbe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 65 deletions.
136 changes: 72 additions & 64 deletions lib/bloc/chat_message_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:askaide/bloc/bloc_manager.dart';
import 'package:askaide/helper/ability.dart';
import 'package:askaide/helper/constant.dart';
import 'package:askaide/helper/error.dart';
import 'package:askaide/helper/logger.dart';
import 'package:askaide/helper/model_resolver.dart';
import 'package:askaide/helper/queue.dart';
import 'package:askaide/lang/lang.dart';
Expand Down Expand Up @@ -342,78 +343,85 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
currentQueue = queue;
try {
RequestFailedException? error;
var listener = queue.listen(const Duration(milliseconds: 10), (items) {
final systemCmds = items.where((e) => e.role == 'system').toList();
if (systemCmds.isNotEmpty) {
for (var element in systemCmds) {
try {
// SYSTEM 命令
// - type: 命令类型
//
// type=summary (默认值)
// - question_id: 问题 ID
// - answer_id: 答案 ID
// - quota_consumed: 消耗的配额
// - token: 消耗的 token
// - info: 提示信息
final cmd = jsonDecode(element.content);

message.serverId = cmd['question_id'];
waitMessage.serverId = cmd['answer_id'];

final quotaConsumed = cmd['quota_consumed'] ?? 0;
final tokenConsumed = cmd['token'] ?? 0;

final info = cmd['info'] ?? '';
if (info != '') {
waitMessage.setExtra({'info': info});
try {
var listener = queue.listen(const Duration(milliseconds: 10), (items) {
final systemCmds = items.where((e) => e.role == 'system').toList();
if (systemCmds.isNotEmpty) {
for (var element in systemCmds) {
try {
// SYSTEM 命令
// - type: 命令类型
//
// type=summary (默认值)
// - question_id: 问题 ID
// - answer_id: 答案 ID
// - quota_consumed: 消耗的配额
// - token: 消耗的 token
// - info: 提示信息
final cmd = jsonDecode(element.content);

message.serverId = cmd['question_id'];
waitMessage.serverId = cmd['answer_id'];

final quotaConsumed = cmd['quota_consumed'] ?? 0;
final tokenConsumed = cmd['token'] ?? 0;

final info = cmd['info'] ?? '';
if (info != '') {
waitMessage.setExtra({'info': info});
}

if (quotaConsumed == 0 && tokenConsumed == 0) {
continue;
}

waitMessage.quotaConsumed = quotaConsumed;
waitMessage.tokenConsumed = tokenConsumed;
} catch (e) {
// ignore: avoid_print
}

if (quotaConsumed == 0 && tokenConsumed == 0) {
continue;
}

waitMessage.quotaConsumed = quotaConsumed;
waitMessage.tokenConsumed = tokenConsumed;
} catch (e) {
// ignore: avoid_print
}
}
}

waitMessage.text += items
.where((e) => e.role != 'system')
.map((e) => e.content)
.join('');
emit(ChatMessageUpdated(waitMessage, processing: true));
waitMessage.text += items
.where((e) => e.role != 'system')
.map((e) => e.content)
.join('');
emit(ChatMessageUpdated(waitMessage, processing: true));

// 失败处理
for (var e in items) {
if (e.code != null && e.code! > 0) {
error = RequestFailedException(e.error ?? '请求处理失败', e.code!);
// 失败处理
for (var e in items) {
if (e.code != null && e.code! > 0) {
error = RequestFailedException(e.error ?? '请求处理失败', e.code!);
}
}
});

await ModelResolver.instance
.request(
room: room,
tempModel: tempModel,
contextMessages: messages.sublist(0, messages.length - 1),
onMessage: queue.add,
maxTokens: room.maxTokens,
)
.whenComplete(queue.finish);

await listener;

waitMessage.text = waitMessage.text.trim();
if (waitMessage.text.isEmpty) {
error = RequestFailedException('机器人没有回答任何内容', 500);
}
});

await ModelResolver.instance
.request(
room: room,
tempModel: tempModel,
contextMessages: messages.sublist(0, messages.length - 1),
onMessage: queue.add,
maxTokens: room.maxTokens,
)
.whenComplete(queue.finish);

await listener;

waitMessage.text = waitMessage.text.trim();
if (waitMessage.text.isEmpty) {
error = RequestFailedException('机器人没有回答任何内容', 500);
}

if (error != null) {
throw error!;
if (error != null) {
throw error!;
}
} catch (e) {
if (waitMessage.text.isEmpty) {
Logger.instance.e('响应过程中出错了: $e');
rethrow;
}
}

// 机器人应答完成,将最后一条机器人应答消息更新到数据库,替换掉思考中消息
Expand Down
2 changes: 1 addition & 1 deletion lib/page/component/chat/chat_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class _ChatInputState extends State<ChatInput> with TickerProviderStateMixin {
}
},
child: LoadingAnimationWidget.beat(
color: customColors.linkColor!,
color: customColors.linkColor ?? Colors.green,
size: 20,
),
);
Expand Down

0 comments on commit 654bcbe

Please sign in to comment.