Skip to content

Commit

Permalink
Merge pull request #106 from mylxsw/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mylxsw authored Apr 11, 2024
2 parents a004107 + d6b6f90 commit 988b284
Show file tree
Hide file tree
Showing 25 changed files with 1,486 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_windows_app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Flutter
uses: subosito/[email protected]
with:
flutter-version: "3.13.2" # Set flutter version here
flutter-version: "3.19.2" # Set flutter version here

- name: Build Windows app
#run: flutter build windows --release
Expand Down
100 changes: 100 additions & 0 deletions lib/bloc/admin_room_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'package:askaide/repo/api/page.dart';
import 'package:askaide/repo/api_server.dart';
import 'package:askaide/repo/model/message.dart';
import 'package:askaide/repo/model/misc.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

part 'admin_room_event.dart';
part 'admin_room_state.dart';

class AdminRoomBloc extends Bloc<AdminRoomEvent, AdminRoomState> {
AdminRoomBloc() : super(AdminRoomInitial()) {
on<AdminRoomsLoadEvent>((event, emit) async {
final rooms = await APIServer().adminUserRooms(userId: event.userId);
emit(AdminRoomsLoaded(rooms: rooms));
});

on<AdminRoomLoadEvent>((event, emit) async {
final room = await APIServer().adminUserRoom(
userId: event.userId,
roomId: event.roomId,
);
emit(AdminRoomLoaded(room: room));
});

on<AdminRoomRecentlyMessagesLoadEvent>((event, emit) async {
if (event.roomType == 4) {
final messages = await APIServer().adminUserRoomGroupMessages(
userId: event.userId, roomId: event.roomId);
emit(AdminRoomRecentlyMessagesLoaded(
messages: messages
.map((e) => Message(
e.role == 'user' ? Role.sender : Role.receiver,
e.message,
type: MessageType.text,
ts: e.createdAt,
model: e.model,
quotaConsumed: e.quotaConsumed,
tokenConsumed: e.tokenConsumed,
refId: e.pid,
id: e.id,
serverId: e.id,
))
.toList()));
} else {
final messages = await APIServer().adminUserRoomMessages(
userId: event.userId,
roomId: event.roomId,
);
emit(AdminRoomRecentlyMessagesLoaded(
messages: messages
.map((e) => Message(
e.role == 1 ? Role.sender : Role.receiver,
e.message,
type: MessageType.text,
ts: e.createdAt,
model: e.model,
quotaConsumed: e.quotaConsumed,
tokenConsumed: e.tokenConsumed,
refId: e.pid,
id: e.id,
serverId: e.id,
userId: e.userId,
roomId: e.roomId,
))
.toList()));
}
});

on<AdminRecentlyMessagesLoadEvent>((event, emit) async {
final messages = await APIServer().adminRecentlyMessages(
page: event.page,
perPage: event.perPage,
keyword: event.keyword,
);
emit(AdminRecentlyMessagesLoaded(PagedData(
data: messages.data
.map((e) => Message(
e.role == 1 ? Role.sender : Role.receiver,
e.message,
type: MessageType.text,
ts: e.createdAt,
model: e.model,
quotaConsumed: e.quotaConsumed,
tokenConsumed: e.tokenConsumed,
refId: e.pid,
id: e.id,
serverId: e.id,
userId: e.userId,
roomId: e.roomId,
))
.toList(),
page: messages.page,
perPage: messages.perPage,
total: messages.total,
lastPage: messages.lastPage,
)));
});
}
}
41 changes: 41 additions & 0 deletions lib/bloc/admin_room_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
part of 'admin_room_bloc.dart';

@immutable
sealed class AdminRoomEvent {}

class AdminRoomsLoadEvent extends AdminRoomEvent {
final int userId;

AdminRoomsLoadEvent({required this.userId});
}

class AdminRoomLoadEvent extends AdminRoomEvent {
final int userId;
final int roomId;

AdminRoomLoadEvent({required this.roomId, required this.userId});
}

class AdminRoomRecentlyMessagesLoadEvent extends AdminRoomEvent {
final int userId;
final int roomId;
final int roomType;

AdminRoomRecentlyMessagesLoadEvent({
required this.roomId,
required this.userId,
required this.roomType,
});
}

class AdminRecentlyMessagesLoadEvent extends AdminRoomEvent {
final int page;
final int perPage;
final String? keyword;

AdminRecentlyMessagesLoadEvent({
required this.page,
required this.perPage,
this.keyword,
});
}
37 changes: 37 additions & 0 deletions lib/bloc/admin_room_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
part of 'admin_room_bloc.dart';

@immutable
sealed class AdminRoomState {}

final class AdminRoomInitial extends AdminRoomState {}

final class AdminRoomsLoaded extends AdminRoomState {
final List<RoomInServer> rooms;

AdminRoomsLoaded({required this.rooms});
}

final class AdminRoomLoaded extends AdminRoomState {
final RoomInServer room;

AdminRoomLoaded({required this.room});
}

final class AdminRoomRecentlyMessagesLoaded extends AdminRoomState {
final List<Message> messages;

AdminRoomRecentlyMessagesLoaded({required this.messages});
}

class AdminRoomOperationResult extends AdminRoomState {
final bool success;
final String message;

AdminRoomOperationResult(this.success, this.message);
}

class AdminRecentlyMessagesLoaded extends AdminRoomState {
final PagedData<Message> messages;

AdminRecentlyMessagesLoaded(this.messages);
}
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
5 changes: 5 additions & 0 deletions lib/helper/ability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class Ability {
return capabilities.stripeEnabled;
}

/// 是否支持微信支付
bool get enableWechatPay {
return capabilities.wechatPayEnabled;
}

/// 是否支持 ApplePay
bool get enableApplePay {
return capabilities.applePayEnabled;
Expand Down
Loading

0 comments on commit 988b284

Please sign in to comment.