Skip to content

Commit

Permalink
Merge pull request #55 from mylxsw/develop
Browse files Browse the repository at this point in the history
1. 服务端地址通过环境变量配置,编译时可以指定环境变量来为不同环境打包
2. 支付宝支付支持沙箱环境
3. 增加群聊功能,现在可以把多个大模型拉到一个群里,同时向它们进行提问了
4. 聊一聊页面的历史就来移动到单独的页面,首页只展示最近 3 条
5. 聊天记录支持以图片的形式进行分享
6. 聊天协议由 OpenAI 兼容的 SSE 转换为了 WebSocket,解决最新版 Chrome 浏览器无法使用问题 #36
7. 智慧果使用明细增加日维度的使用记录
8. 其它 Bugfix,如 #37
  • Loading branch information
mylxsw authored Oct 29, 2023
2 parents b09ab4e + 0da7e32 commit c976de6
Show file tree
Hide file tree
Showing 145 changed files with 6,785 additions and 3,685 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.25

COPY build/web/ /data/webroot
COPY nginx.conf /etc/nginx/conf.d/default.conf
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ build-web:
rm -fr build/web/fonts/ && mkdir build/web/fonts
cp -r scripts/s build/web/fonts/s

build-web-samehost:
flutter build web --web-renderer canvaskit --release --dart-define=API_SERVER_URL=/
cd scripts && go run main.go ../build/web/main.dart.js && cd ..
rm -fr build/web/fonts/ && mkdir build/web/fonts
cp -r scripts/s build/web/fonts/s

deploy-web: build-web
cd build && tar -zcvf web.tar.gz web
scp build/web.tar.gz huawei-1:/data/webroot
ssh huawei-1 "cd /data/webroot && tar -zxvf web.tar.gz && rm -rf web.tar.gz app && mv web app"
rm -fr build/web.tar.gz

.PHONY: run build-android build-macos ipa
.PHONY: run build-android build-macos ipa build-web-samehost build-web deploy-web
20 changes: 20 additions & 0 deletions docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

VERSION=1.0.6
VERSION_DATE=202310091100

rm -fr build/web

flutter build web --web-renderer canvaskit --release --dart-define=API_SERVER_URL=/
cd scripts && go run main.go ../build/web/main.dart.js && cd ..
rm -fr build/web/fonts/ && mkdir build/web/fonts
cp -r scripts/s build/web/fonts/s

docker build -t mylxsw/aidea-web:$VERSION .
docker tag mylxsw/aidea-web:$VERSION mylxsw/aidea-web:$VERSION_DATE
docker tag mylxsw/aidea-web:$VERSION mylxsw/aidea-web:latest

docker push mylxsw/aidea-web:$VERSION
docker push mylxsw/aidea-web:$VERSION_DATE
docker push mylxsw/aidea-web:latest

1 change: 1 addition & 0 deletions lib/bloc/background_image_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:askaide/repo/api_server.dart';
import 'package:askaide/repo/model/misc.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

Expand Down
3 changes: 2 additions & 1 deletion lib/bloc/chat_chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:askaide/helper/constant.dart';
import 'package:askaide/repo/api_server.dart';
import 'package:askaide/repo/chat_message_repo.dart';
import 'package:askaide/repo/model/chat_history.dart';
import 'package:askaide/repo/model/misc.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

Expand All @@ -15,7 +16,7 @@ class ChatChatBloc extends Bloc<ChatChatEvent, ChatChatState> {
on<ChatChatLoadRecentHistories>((event, emit) async {
final histories = await _chatMessageRepository.recentChatHistories(
chatAnywhereRoomId,
30,
4,
userId: APIServer().localUserID(),
);

Expand Down
28 changes: 24 additions & 4 deletions lib/bloc/chat_message_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:askaide/repo/model/message.dart';
import 'package:askaide/repo/model/room.dart';
import 'package:askaide/repo/openai_repo.dart';
import 'package:askaide/repo/settings_repo.dart';
import 'package:dart_openai/openai.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

Expand Down Expand Up @@ -268,7 +269,7 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {

// 更新 Room 最后活跃时间
// 这里没有使用 await,因为不需要等待更新完成,让 room 的更新异步的去处理吧
if (!Ability().supportAPIServer()) {
if (!Ability().enableAPIServer()) {
chatMsgRepo.updateRoomLastActiveTime(roomId);
}

Expand Down Expand Up @@ -305,6 +306,7 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
// 等待监听机器人应答消息
final queue = GracefulQueue<ChatStreamRespData>();
try {
RequestFailedException? error;
var listener = queue.listen(const Duration(milliseconds: 10), (items) {
final systemCmds = items.where((e) => e.role == 'system').toList();
if (systemCmds.isNotEmpty) {
Expand Down Expand Up @@ -335,6 +337,13 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
.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!);
}
}
});

await ModelResolver.instance
Expand All @@ -348,6 +357,15 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {

await listener;

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

if (error != null) {
throw error!;
}

// 机器人应答完成,将最后一条机器人应答消息更新到数据库,替换掉思考中消息
waitMessage.isReady = true;
await chatMsgRepo.updateMessage(roomId, waitMessage.id!, waitMessage);
Expand Down Expand Up @@ -388,11 +406,13 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
chatHistory: localChatHistory,
));
} catch (e) {
final error = resolveErrorMessage(e, isChat: true);
await chatMsgRepo.updateMessagePart(
roomId,
sentMessageId,
[
MessagePart('status', 2),
MessagePart('extra', jsonEncode({'error': error.toString()})),
],
);

Expand All @@ -403,7 +423,7 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
waitMessage.id!,
Message(
Role.receiver,
AppLocale.robotHasSomeError,
error.toString(),
id: waitMessage.id,
ts: DateTime.now(),
type: MessageType.system,
Expand All @@ -423,7 +443,7 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
userId: APIServer().localUserID(),
chatHistoryId: localChatHistoryId,
),
error: resolveErrorMessage(e),
error: error,
chatHistory: localChatHistory,
));

Expand All @@ -439,7 +459,7 @@ class ChatMessageBloc extends BlocExt<ChatMessageEvent, ChatMessageState> {
Future<Room?> queryRoomById(
ChatMessageRepository chatMsgRepo, int roomId) async {
Room? room;
if (Ability().supportAPIServer()) {
if (Ability().enableAPIServer()) {
final roomInServer = await APIServer().room(roomId: roomId);
room = Room(
roomInServer.name,
Expand Down
11 changes: 8 additions & 3 deletions lib/bloc/free_count_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:askaide/helper/ability.dart';
import 'package:askaide/repo/api_server.dart';
import 'package:askaide/repo/model/misc.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

Expand All @@ -12,8 +13,11 @@ class FreeCountBloc extends Bloc<FreeCountEvent, FreeCountState> {
FreeCountBloc() : super(FreeCountInitial()) {
// 重新加载所有的模型免费使用次数
on<FreeCountReloadAllEvent>((event, emit) async {
if (Ability().supportLocalOpenAI() || !Ability().supportAPIServer()) {
emit(FreeCountLoadedState(counts: counts));
if (!Ability().enableAPIServer()) {
emit(FreeCountLoadedState(
counts: counts,
needSignin: event.checkSigninStatus,
));
return;
}

Expand All @@ -23,7 +27,8 @@ class FreeCountBloc extends Bloc<FreeCountEvent, FreeCountState> {

// 重新加载指定模型的免费使用次数
on<FreeCountReloadEvent>((event, emit) async {
if (Ability().supportLocalOpenAI() || !Ability().supportAPIServer()) {
if (Ability().usingLocalOpenAIModel(event.model) ||
!Ability().enableAPIServer()) {
emit(FreeCountLoadedState(counts: counts));
return;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/bloc/free_count_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class FreeCountReloadEvent extends FreeCountEvent {
FreeCountReloadEvent({required this.model});
}

class FreeCountReloadAllEvent extends FreeCountEvent {}
class FreeCountReloadAllEvent extends FreeCountEvent {
final bool checkSigninStatus;

FreeCountReloadAllEvent({this.checkSigninStatus = false});
}
3 changes: 2 additions & 1 deletion lib/bloc/free_count_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ final class FreeCountInitial extends FreeCountState {}

class FreeCountLoadedState extends FreeCountState {
final List<FreeModelCount> counts;
final bool needSignin;

FreeModelCount? model(String model) {
model = model.split(':').last;
Expand All @@ -19,5 +20,5 @@ class FreeCountLoadedState extends FreeCountState {
return null;
}

FreeCountLoadedState({required this.counts});
FreeCountLoadedState({required this.counts, this.needSignin = false});
}
5 changes: 4 additions & 1 deletion lib/bloc/gallery_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class GalleryBloc extends Bloc<GalleryEvent, GalleryState> {
id: event.id,
);

emit(GalleryItemLoaded(item: res));
emit(GalleryItemLoaded(
item: res.item,
isInternalUser: res.isInternalUser,
));
});
}
}
3 changes: 2 additions & 1 deletion lib/bloc/gallery_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GalleryLoaded extends GalleryState {

class GalleryItemLoaded extends GalleryState {
final CreativeGallery item;
final bool isInternalUser;

GalleryItemLoaded({required this.item});
GalleryItemLoaded({required this.item, this.isInternalUser = false});
}
Loading

0 comments on commit c976de6

Please sign in to comment.