Skip to content

Commit

Permalink
chore: implement ui
Browse files Browse the repository at this point in the history
  • Loading branch information
richardshiue committed Dec 26, 2024
1 parent d939d7a commit 36d8a32
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
.onFailure(Log.error);
},
setRagOnly: (ragOnly) async {
emit(state.copyWith(onlyUseSelectedSources: ragOnly));
final payload = UpdateChatSettingsPB.create()
..chatId = ChatId(value: chatId)
..ragOnly = ragOnly;
Expand Down Expand Up @@ -624,12 +625,14 @@ class ChatEvent with _$ChatEvent {
@freezed
class ChatState with _$ChatState {
const factory ChatState({
required bool onlyUseSelectedSources,
required List<String> selectedSourceIds,
required LoadChatMessageStatus loadingState,
required PromptResponseState promptResponseState,
}) = _ChatState;

factory ChatState.initial() => const ChatState(
onlyUseSelectedSources: false,
selectedSourceIds: [],
loadingState: LoadChatMessageStatus.loading,
promptResponseState: PromptResponseState.ready,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ class ChatSettingsCubit extends Cubit<ChatSettingsState> {
List<ChatSource> selectedSources = [];
String filter = '';

void updateOnlyUseSelectedSources(bool onlyUseSelectedSources) {
if (state.onlyUseSelectedSources != onlyUseSelectedSources) {
emit(state.copyWith(onlyUseSelectedSources: onlyUseSelectedSources));
}
}

void updateSelectedSources(List<String> newSelectedSourceIds) {
selectedSourceIds = [...newSelectedSourceIds];
}
Expand Down Expand Up @@ -380,11 +386,13 @@ class ChatSettingsCubit extends Cubit<ChatSettingsState> {
@freezed
class ChatSettingsState with _$ChatSettingsState {
const factory ChatSettingsState({
required bool onlyUseSelectedSources,
required List<ChatSource> visibleSources,
required List<ChatSource> selectedSources,
}) = _ChatSettingsState;

factory ChatSettingsState.initial() => const ChatSettingsState(
onlyUseSelectedSources: false,
visibleSources: [],
selectedSources: [],
);
Expand Down
22 changes: 16 additions & 6 deletions frontend/appflowy_flutter/lib/plugins/ai_chat/chat_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:appflowy_result/appflowy_result.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_chat_core/flutter_chat_core.dart';
Expand Down Expand Up @@ -296,12 +297,21 @@ class _ChatContentPage extends StatelessWidget {
),
);
},
onUpdateSelectedSources: (ids) {
chatBloc.add(
ChatEvent.updateSelectedSources(
selectedSourcesIds: ids,
),
);
onUpdateSelectedSources: (ragOnly, ids) {
if (ragOnly != chatBloc.state.onlyUseSelectedSources) {
chatBloc.add(
ChatEvent.setRagOnly(
ragOnly: ragOnly,
),
);
}
if (!listEquals(ids, chatBloc.state.selectedSourceIds)) {
chatBloc.add(
ChatEvent.updateSelectedSources(
selectedSourcesIds: ids,
),
);
}
},
)
: MobileAIPromptInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DesktopAIPromptInput extends StatefulWidget {
final bool isStreaming;
final void Function() onStopStreaming;
final void Function(String, Map<String, dynamic>) onSubmitted;
final void Function(List<String>) onUpdateSelectedSources;
final void Function(bool, List<String>) onUpdateSelectedSources;

@override
State<DesktopAIPromptInput> createState() => _DesktopAIPromptInputState();
Expand Down Expand Up @@ -497,7 +497,7 @@ class _PromptBottomActions extends StatelessWidget {
final SendButtonState sendButtonState;
final void Function() onSendPressed;
final void Function() onStopStreaming;
final void Function(List<String>) onUpdateSelectedSources;
final void Function(bool, List<String>) onUpdateSelectedSources;

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PromptInputDesktopSelectSourcesButton extends StatefulWidget {
required this.onUpdateSelectedSources,
});

final void Function(List<String>) onUpdateSelectedSources;
final void Function(bool, List<String>) onUpdateSelectedSources;

@override
State<PromptInputDesktopSelectSourcesButton> createState() =>
Expand Down Expand Up @@ -80,6 +80,7 @@ class _PromptInputDesktopSelectSourcesButtonState
return BlocListener<ChatBloc, ChatState>(
listener: (context, state) {
cubit
..updateOnlyUseSelectedSources(state.onlyUseSelectedSources)
..updateSelectedSources(state.selectedSourceIds)
..updateSelectedStatus();
},
Expand All @@ -95,7 +96,10 @@ class _PromptInputDesktopSelectSourcesButtonState
}
},
onClose: () {
widget.onUpdateSelectedSources(cubit.selectedSourceIds);
widget.onUpdateSelectedSources(
cubit.state.onlyUseSelectedSources,
cubit.selectedSourceIds,
);
if (spaceView != null) {
context.read<ChatSettingsCubit>().refreshSources(spaceView);
}
Expand Down Expand Up @@ -188,6 +192,29 @@ class _PopoverContent extends StatelessWidget {
context.read<ChatSettingsCubit>().updateFilter(value),
),
),
Container(
margin: const EdgeInsets.fromLTRB(8, 0, 8, 8),
height: 30,
child: FlowyButton(
text: FlowyText(
LocaleKeys.chat_onlyUseRags.tr(),
overflow: TextOverflow.ellipsis,
),
onTap: () {
context
.read<ChatSettingsCubit>()
.updateOnlyUseSelectedSources(
!state.onlyUseSelectedSources,
);
},
rightIcon: state.onlyUseSelectedSources
? FlowySvg(
FlowySvgs.check_s,
color: Theme.of(context).colorScheme.primary,
)
: null,
),
),
_buildDivider(),
Flexible(
child: ListView(
Expand Down
3 changes: 2 additions & 1 deletion frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@
"addToPageButton": "Add to page",
"addToPageTitle": "Add message to...",
"addToNewPage": "Add to a new page",
"addToNewPageName": "Messages extracted from \"{}\""
"addToNewPageName": "Messages extracted from \"{}\"",
"onlyUseRags": "Only use selected sources to generate response"
},
"trash": {
"text": "Trash",
Expand Down

0 comments on commit 36d8a32

Please sign in to comment.