Skip to content

Commit

Permalink
added validation for selected chat mode, defaulted bot language to us…
Browse files Browse the repository at this point in the history
…er's l2 (#997)
  • Loading branch information
ggurdin authored Nov 13, 2024
1 parent f4da4ac commit 68ab868
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
3 changes: 2 additions & 1 deletion assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -4497,5 +4497,6 @@
"enterDiscussionTopic": "Please enter a discussion topic",
"selectBotChatMode": "Select chat mode",
"messageNotInTargetLang": "Message not in target language",
"other": "Other"
"other": "Other",
"botModeValidation": "Please select a chat mode"
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ extension RoomSettingsRoomExtension on Room {

BotOptionsModel? get _botOptions {
if (isSpace) return null;
return BotOptionsModel.fromJson(
getState(PangeaEventTypes.botOptions)?.content ?? {},
);
final stateEvent = getState(PangeaEventTypes.botOptions);
if (stateEvent == null) return null;
return BotOptionsModel.fromJson(stateEvent.content);
}

Future<bool> _isSuggested() async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:fluffychat/pangea/constants/bot_mode.dart';
import 'package:fluffychat/pangea/models/bot_options_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

class ConversationBotModeDynamicZone extends StatelessWidget {
final BotOptionsModel botOptions;
final String? mode;
final TextEditingController discussionTopicController;
final TextEditingController discussionKeywordsController;
final TextEditingController customSystemPromptController;
Expand All @@ -13,11 +12,11 @@ class ConversationBotModeDynamicZone extends StatelessWidget {

const ConversationBotModeDynamicZone({
super.key,
required this.botOptions,
required this.discussionTopicController,
required this.discussionKeywordsController,
required this.customSystemPromptController,
this.enabled = true,
this.mode,
});

@override
Expand All @@ -33,7 +32,7 @@ class ConversationBotModeDynamicZone extends StatelessWidget {
),
controller: discussionTopicController,
validator: (value) => enabled &&
botOptions.mode == BotMode.discussion &&
mode == BotMode.discussion &&
(value == null || value.isEmpty)
? L10n.of(context)!.enterDiscussionTopic
: null,
Expand Down Expand Up @@ -67,7 +66,7 @@ class ConversationBotModeDynamicZone extends StatelessWidget {
contentPadding: const EdgeInsets.symmetric(horizontal: 28.0),
),
validator: (value) => enabled &&
botOptions.mode == BotMode.custom &&
mode == BotMode.custom &&
(value == null || value.isEmpty)
? L10n.of(context)!.enterPrompt
: null,
Expand All @@ -81,16 +80,16 @@ class ConversationBotModeDynamicZone extends StatelessWidget {

return Column(
children: [
if (botOptions.mode == BotMode.discussion) ...discussionChildren,
if (botOptions.mode == BotMode.custom) ...customChildren,
if (mode == BotMode.discussion) ...discussionChildren,
if (mode == BotMode.custom) ...customChildren,
const SizedBox(height: 12),
CheckboxListTile(
title: Text(
L10n.of(context)!
.conversationBotCustomZone_customTriggerReactionEnabledLabel,
),
enabled: false,
value: botOptions.customTriggerReactionEnabled ?? true,
value: true,
onChanged: null,
),
const SizedBox(height: 12),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ class ConversationBotModeSelect extends StatelessWidget {
final String? initialMode;
final void Function(String?) onChanged;
final bool enabled;
final String? Function(String?)? validator;

const ConversationBotModeSelect({
super.key,
this.initialMode,
required this.onChanged,
this.enabled = true,
this.validator,
});

@override
Expand Down Expand Up @@ -41,6 +43,8 @@ class ConversationBotModeSelect extends StatelessWidget {
),
],
onChanged: enabled ? onChanged : null,
validator: validator,
value: initialMode,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class ConversationBotSettingsDialogState
final TextEditingController customSystemPromptController =
TextEditingController();

bool hasUpdatedMode = false;

@override
void initState() {
super.initState();
Expand All @@ -126,6 +128,8 @@ class ConversationBotSettingsDialogState
discussionKeywordsController.text = botOptions.discussionKeywords ?? "";
discussionTopicController.text = botOptions.discussionTopic ?? "";
customSystemPromptController.text = botOptions.customSystemPrompt ?? "";

hasUpdatedMode = widget.room.botOptions != null;
}

final GlobalKey<FormState> formKey = GlobalKey<FormState>();
Expand All @@ -137,6 +141,7 @@ class ConversationBotSettingsDialogState
}

void onUpdateChatMode(String? mode) {
hasUpdatedMode = true;
setState(() => botOptions.mode = mode ?? BotMode.discussion);
}

Expand Down Expand Up @@ -217,6 +222,7 @@ class ConversationBotSettingsDialogState
customSystemPromptController:
customSystemPromptController,
enabled: addBot,
hasUpdatedMode: hasUpdatedMode,
onUpdateBotMode: onUpdateChatMode,
onUpdateBotLanguage: onUpdateBotLanguage,
onUpdateBotVoice: onUpdateBotVoice,
Expand Down Expand Up @@ -244,6 +250,8 @@ class ConversationBotSettingsDialogState
if (!isValid) return;

updateFromTextControllers();
botOptions.targetLanguage ??= MatrixState
.pangeaController.languageController.userL2?.langCode;

Navigator.of(context).pop(botOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ConversationBotSettingsForm extends StatelessWidget {
final TextEditingController customSystemPromptController;

final bool enabled;
final bool hasUpdatedMode;

final void Function(String?) onUpdateBotMode;
final void Function(String?) onUpdateBotLanguage;
final void Function(String?) onUpdateBotVoice;
Expand All @@ -31,6 +33,7 @@ class ConversationBotSettingsForm extends StatelessWidget {
required this.onUpdateBotVoice,
required this.onUpdateBotLanguageLevel,
this.enabled = true,
this.hasUpdatedMode = false,
});

@override
Expand Down Expand Up @@ -94,17 +97,20 @@ class ConversationBotSettingsForm extends StatelessWidget {
),
),
ConversationBotModeSelect(
initialMode: botOptions.mode,
initialMode: hasUpdatedMode ? botOptions.mode : null,
onChanged: onUpdateBotMode,
enabled: enabled,
validator: (value) {
return value == null ? L10n.of(context)!.botModeValidation : null;
},
),
const SizedBox(height: 12),
ConversationBotModeDynamicZone(
botOptions: botOptions,
discussionTopicController: discussionTopicController,
discussionKeywordsController: discussionKeywordsController,
customSystemPromptController: customSystemPromptController,
enabled: enabled,
mode: hasUpdatedMode ? botOptions.mode : null,
),
],
);
Expand Down

0 comments on commit 68ab868

Please sign in to comment.