Skip to content

Commit

Permalink
Merge pull request #65 from mylxsw/feature/api-mode
Browse files Browse the repository at this point in the history
Feature/api mode
  • Loading branch information
mylxsw authored Nov 27, 2023
2 parents 758a262 + d871417 commit 17d9453
Show file tree
Hide file tree
Showing 47 changed files with 2,744 additions and 572 deletions.
8 changes: 5 additions & 3 deletions lib/bloc/group_chat_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ class GroupChatBloc extends Bloc<GroupChatEvent, GroupChatState> {
await APIServer().chatGroup(event.groupId, cache: !event.forceUpdate);
final states = await stateManager.loadRoomStates(event.groupId);

final defaultChatMembers = await loadDefaultChatMembers(event.groupId);

emit(GroupChatLoaded(
group: group,
states: states,
defaultChatMembers: await loadDefaultChatMembers(event.groupId),
defaultChatMembers: defaultChatMembers.isEmpty
? group.members.map((e) => e.id!).toList()
: defaultChatMembers,
));
});

Expand Down Expand Up @@ -68,8 +72,6 @@ class GroupChatBloc extends Bloc<GroupChatEvent, GroupChatState> {
),
);

Logger.instance.d(resp.toJson());

// 记录默认聊天成员
updateDefaultChatMembers(
event.groupId,
Expand Down
39 changes: 39 additions & 0 deletions lib/bloc/user_api_keys_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:askaide/repo/api/keys.dart';
import 'package:askaide/repo/api_server.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

part 'user_api_keys_event.dart';
part 'user_api_keys_state.dart';

class UserApiKeysBloc extends Bloc<UserApiKeysEvent, UserApiKeysState> {
UserApiKeysBloc() : super(UserApiKeysInitial()) {
// 加载用户 API Key 列表
on<UserApiKeysLoad>((event, emit) async {
final keys = await APIServer().userAPIKeys();
emit(UserApiKeysLoaded(keys: keys));
});

// 加载用户 API Key
on<UserApiKeyLoad>((event, emit) async {
final key = await APIServer().userAPIKeyDetail(id: event.id);
emit(UserApiKeyLoaded(key: key));
});

// 创建用户 API Key
on<UserApiKeyCreate>((event, emit) async {
final key = await APIServer().createAPIKey(name: event.name);
emit(UserApiKeyCreated(key: key));

final keys = await APIServer().userAPIKeys();
emit(UserApiKeysLoaded(keys: keys));
});

// 删除用户 API Key
on<UserApiKeyDelete>((event, emit) async {
await APIServer().deleteAPIKey(id: event.id);
final keys = await APIServer().userAPIKeys();
emit(UserApiKeysLoaded(keys: keys));
});
}
}
24 changes: 24 additions & 0 deletions lib/bloc/user_api_keys_event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'user_api_keys_bloc.dart';

@immutable
sealed class UserApiKeysEvent {}

class UserApiKeysLoad extends UserApiKeysEvent {}

class UserApiKeyLoad extends UserApiKeysEvent {
final int id;

UserApiKeyLoad(this.id);
}

class UserApiKeyCreate extends UserApiKeysEvent {
final String name;

UserApiKeyCreate(this.name);
}

class UserApiKeyDelete extends UserApiKeysEvent {
final int id;

UserApiKeyDelete(this.id);
}
24 changes: 24 additions & 0 deletions lib/bloc/user_api_keys_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
part of 'user_api_keys_bloc.dart';

@immutable
sealed class UserApiKeysState {}

final class UserApiKeysInitial extends UserApiKeysState {}

class UserApiKeysLoaded extends UserApiKeysState {
final List<UserAPIKey> keys;

UserApiKeysLoaded({required this.keys});
}

class UserApiKeyLoaded extends UserApiKeysState {
final UserAPIKey key;

UserApiKeyLoaded({required this.key});
}

class UserApiKeyCreated extends UserApiKeysState {
final String key;

UserApiKeyCreated({required this.key});
}
5 changes: 5 additions & 0 deletions lib/data/migrate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ Future<void> migrate(db, oldVersion, newVersion) async {
await db.execute(
'ALTER TABLE chat_message ADD COLUMN status INTEGER DEFAULT 1;');
}

if (oldVersion < 26) {
await db.execute('ALTER TABLE chat_message ADD COLUMN images TEXT NULL;');
}
}

/// 数据库初始化
Expand Down Expand Up @@ -159,6 +163,7 @@ void initDatabase(db, version) async {
token_consumed INTEGER NULL,
quota_consumed INTEGER NULL,
model TEXT,
images TEXT NULL,
ts INTEGER NOT NULL
)
''');
Expand Down
15 changes: 10 additions & 5 deletions lib/helper/ability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ class Ability {
}

/// 是否支持 Websocket
bool supportWebSocket() {
bool get supportWebSocket {
return capabilities.supportWebsocket;
}

/// 是否支持 API Keys 功能
bool get supportAPIKeys {
return capabilities.supportAPIKeys;
}

/// 更新能力
updateCapabilities(Capabilities capabilities) {
this.capabilities = capabilities;
Expand Down Expand Up @@ -100,7 +105,7 @@ class Ability {
}

/// 是否启用了 OpenAI 自定义设置
bool enableLocalOpenAI() {
bool get enableLocalOpenAI {
return setting.boolDefault(settingOpenAISelfHosted, false);
}

Expand All @@ -111,13 +116,13 @@ class Ability {
}

/// 是否支持翻译功能
bool supportTranslate() {
bool get supportTranslate {
return false;
// return setting.stringDefault(settingAPIServerToken, '') != '';
}

/// 是否支持语音合成功能
bool supportSpeak() {
bool get supportSpeak {
// return setting.stringDefault(settingAPIServerToken, '') != '';
if (PlatformTool.isWeb()) {
return false;
Expand All @@ -127,7 +132,7 @@ class Ability {
}

/// 是否支持图片上传功能
bool supportImageUploader() {
bool get supportImageUploader {
return supportImglocUploader() || supportQiniuUploader();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/helper/constant.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:flutter/material.dart';

// 客户端应用版本号
const clientVersion = '1.0.8';
const clientVersion = '1.0.9';
// 本地数据库版本号
const databaseVersion = 25;
const databaseVersion = 26;

const maxRoomNumForNonVIP = 50;
const coinSign = '个';
Expand Down
2 changes: 1 addition & 1 deletion lib/helper/error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object? resolveHTTPStatusCode(int statusCode,
case 400:
return const LanguageText('请求参数错误');
case 401:
if (Ability().enableLocalOpenAI()) {
if (Ability().enableLocalOpenAI) {
return const LanguageText(AppLocale.openAIAuthFailed);
}

Expand Down
12 changes: 12 additions & 0 deletions lib/helper/global_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:askaide/page/component/chat/file_upload.dart';

class GlobalStore {
static final GlobalStore _instance = GlobalStore._internal();
GlobalStore._internal();

factory GlobalStore() {
return _instance;
}

List<FileUpload> uploadedFiles = [];
}
18 changes: 2 additions & 16 deletions lib/helper/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ModelAggregate {
category: e.category,
tag: e.tag,
avatarUrl: e.avatarUrl,
supportVision: e.supportVision,
),
)
.toList());
Expand Down Expand Up @@ -63,25 +64,10 @@ class ModelAggregate {

/// 根据模型唯一id查找模型
static Future<mm.Model> model(String uid) async {
if (uid.split(':').length == 1) {
uid = '$modelTypeOpenAI:$uid';
}

final supportModels = await models();
// if (uid.startsWith('$modelTypeOpenAI:')) {
// models.addAll(OpenAIRepository.supportModels());
// }

// if (uid.startsWith('$modelTypeDeepAI:')) {
// models.addAll(DeepAIRepository.supportModels());
// }

// if (uid.startsWith('$modelTypeStabilityAI:')) {
// models.addAll(StabilityAIRepository.supportModels());
// }

return supportModels.firstWhere(
(element) => element.uid() == uid,
(element) => element.uid() == uid || element.id == uid,
orElse: () => mm.Model(defaultChatModel, defaultChatModel, 'openai',
category: modelTypeOpenAI),
);
Expand Down
17 changes: 11 additions & 6 deletions lib/helper/model_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:askaide/helper/error.dart';
import 'package:askaide/helper/helper.dart';
import 'package:askaide/repo/api_server.dart';
import 'package:askaide/repo/deepai_repo.dart';
import 'package:askaide/repo/model/chat_message.dart';
import 'package:askaide/repo/model/message.dart';
import 'package:askaide/repo/model/room.dart';
import 'package:askaide/repo/openai_repo.dart';
Expand Down Expand Up @@ -172,7 +173,7 @@ class ModelResolver {
}

/// 构建机器人请求上下文
List<OpenAIChatCompletionChoiceMessageModel> _buildRequestContext(
List<ChatMessage> _buildRequestContext(
Room room,
List<Message> messages,
) {
Expand All @@ -192,10 +193,14 @@ class ModelResolver {
.where((e) => !e.isSystem() && !e.isInitMessage())
.where((e) => !e.statusIsFailed())
.map((e) => e.role == Role.receiver
? OpenAIChatCompletionChoiceMessageModel(
role: OpenAIChatMessageRole.assistant, content: e.text)
: OpenAIChatCompletionChoiceMessageModel(
role: OpenAIChatMessageRole.user, content: e.text))
? ChatMessage(
role: OpenAIChatMessageRole.assistant,
content: e.text,
images: e.images)
: ChatMessage(
role: OpenAIChatMessageRole.user,
content: e.text,
images: e.images))
.toList();

if (contextMessages.length > room.maxContext * 2) {
Expand All @@ -206,7 +211,7 @@ class ModelResolver {
if (room.systemPrompt != null && room.systemPrompt != '') {
contextMessages.insert(
0,
OpenAIChatCompletionChoiceMessageModel(
ChatMessage(
role: OpenAIChatMessageRole.system,
content: room.systemPrompt!,
),
Expand Down
Loading

0 comments on commit 17d9453

Please sign in to comment.