formatting

This commit is contained in:
ggurdin 2025-11-05 16:30:39 -05:00
parent b3261bc630
commit 99c1f44743
No known key found for this signature in database
GPG key ID: A01CB41737CBB478
5 changed files with 144 additions and 74 deletions

View file

@ -1,6 +1,11 @@
import 'dart:async';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:fluffychat/pages/chat/chat.dart';
import 'package:fluffychat/pangea/choreographer/controllers/igc_controller.dart';
import 'package:fluffychat/pangea/choreographer/enums/assistance_state_enum.dart';
@ -22,10 +27,6 @@ import 'package:fluffychat/pangea/learning_settings/models/language_model.dart';
import 'package:fluffychat/pangea/spaces/models/space_model.dart';
import 'package:fluffychat/pangea/subscription/controllers/subscription_controller.dart';
import 'package:fluffychat/pangea/toolbar/controllers/tts_controller.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import '../../../widgets/matrix.dart';
import 'error_service.dart';
import 'it_controller.dart';
@ -68,12 +69,14 @@ class Choreographer {
igc = IgcController(this);
errorService = ErrorService(this);
_textController.addListener(_onChangeListener);
_languageStream = pangeaController.userController.languageStream.stream.listen((update) {
_languageStream =
pangeaController.userController.languageStream.stream.listen((update) {
clear();
setState();
});
_settingsUpdateStream = pangeaController.userController.settingsUpdateStream.stream.listen((_) {
_settingsUpdateStream =
pangeaController.userController.settingsUpdateStream.stream.listen((_) {
setState();
});
_currentAssistanceState = assistanceState;
@ -138,14 +141,15 @@ class Choreographer {
final message = chatController.sendController.text;
final fakeEventId = chatController.sendFakeMessage();
final PangeaRepresentation? originalWritten = choreoRecord?.includedIT == true && translatedText != null
? PangeaRepresentation(
langCode: l1LangCode ?? LanguageKeys.unknownLanguage,
text: translatedText!,
originalWritten: true,
originalSent: false,
)
: null;
final PangeaRepresentation? originalWritten =
choreoRecord?.includedIT == true && translatedText != null
? PangeaRepresentation(
langCode: l1LangCode ?? LanguageKeys.unknownLanguage,
text: translatedText!,
originalWritten: true,
originalSent: false,
)
: null;
PangeaMessageTokens? tokensSent;
PangeaRepresentation? originalSent;
@ -166,7 +170,8 @@ class Choreographer {
}
originalSent = PangeaRepresentation(
langCode: res?.detections.firstOrNull?.langCode ?? LanguageKeys.unknownLanguage,
langCode: res?.detections.firstOrNull?.langCode ??
LanguageKeys.unknownLanguage,
text: message,
originalSent: true,
originalWritten: originalWritten == null,
@ -253,7 +258,8 @@ class Choreographer {
_lastChecked = _textController.text;
if (_textController.editType == EditType.igc || _textController.editType == EditType.itDismissed) {
if (_textController.editType == EditType.igc ||
_textController.editType == EditType.itDismissed) {
_textController.editType = EditType.keyboard;
return;
}
@ -300,7 +306,8 @@ class Choreographer {
}) async {
try {
if (errorService.isError) return;
final SubscriptionStatus canSendStatus = pangeaController.subscriptionController.subscriptionStatus;
final SubscriptionStatus canSendStatus =
pangeaController.subscriptionController.subscriptionStatus;
if (canSendStatus != SubscriptionStatus.subscribed ||
l2Lang == null ||
@ -319,7 +326,9 @@ class Choreographer {
itController.clear();
}
await (isRunningIT ? itController.getTranslationData(_useCustomInput) : igc.getIGCTextData());
await (isRunningIT
? itController.getTranslationData(_useCustomInput)
: igc.getIGCTextData());
} catch (err, stack) {
ErrorHandler.logError(
e: err,
@ -343,9 +352,12 @@ class Choreographer {
void onITChoiceSelect(ITStep step) {
_textController.setSystemText(
_textController.text + step.continuances[step.chosen!].text,
step.continuances[step.chosen!].gold ? EditType.itGold : EditType.itStandard,
step.continuances[step.chosen!].gold
? EditType.itGold
: EditType.itStandard,
);
_textController.selection = TextSelection.collapsed(offset: _textController.text.length);
_textController.selection =
TextSelection.collapsed(offset: _textController.text.length);
_initChoreoRecord();
choreoRecord!.addRecord(_textController.text, step: step);
@ -393,11 +405,14 @@ class Choreographer {
// return;
// }
igc.igcTextData!.matches[matchIndex].match.choices![choiceIndex].selected = true;
igc.igcTextData!.matches[matchIndex].match.choices![choiceIndex]
.selected = true;
final isNormalizationError = l2Lang != null && igc.spanDataController.isNormalizationError(matchIndex, l2Lang!);
final isNormalizationError = l2Lang != null &&
igc.spanDataController.isNormalizationError(matchIndex, l2Lang!);
final match = igc.igcTextData!.matches[matchIndex].copyWith..status = PangeaMatchStatus.accepted;
final match = igc.igcTextData!.matches[matchIndex].copyWith
..status = PangeaMatchStatus.accepted;
igc.igcTextData!.acceptReplacement(
matchIndex,
@ -467,7 +482,8 @@ class Choreographer {
void acceptNormalizationMatches() {
final List<int> indices = [];
for (int i = 0; i < igc.igcTextData!.matches.length; i++) {
final isNormalizationError = l2Lang != null && igc.spanDataController.isNormalizationError(i, l2Lang!);
final isNormalizationError = l2Lang != null &&
igc.spanDataController.isNormalizationError(i, l2Lang!);
if (isNormalizationError) indices.add(i);
}
@ -491,7 +507,11 @@ class Choreographer {
final newMatch = match.copyWith;
newMatch.status = PangeaMatchStatus.automatic;
newMatch.match.length = match.match.choices!.firstWhere((c) => c.isBestCorrection).value.characters.length;
newMatch.match.length = match.match.choices!
.firstWhere((c) => c.isBestCorrection)
.value
.characters
.length;
_textController.setSystemText(
igc.igcTextData!.originalInput,
@ -525,7 +545,8 @@ class Choreographer {
igc.onIgnoreMatch(igc.igcTextData!.matches[matchIndex]);
igc.igcTextData!.matches[matchIndex].status = PangeaMatchStatus.ignored;
final isNormalizationError = l2Lang != null && igc.spanDataController.isNormalizationError(matchIndex, l2Lang!);
final isNormalizationError = l2Lang != null &&
igc.spanDataController.isNormalizationError(matchIndex, l2Lang!);
if (!isNormalizationError) {
_initChoreoRecord();
@ -602,15 +623,18 @@ class Choreographer {
String? get l2LangCode => l2Lang?.langCode;
LanguageModel? get l1Lang => pangeaController.languageController.activeL1Model();
LanguageModel? get l1Lang =>
pangeaController.languageController.activeL1Model();
String? get l1LangCode => l1Lang?.langCode;
String? get userId => pangeaController.userController.userId;
bool get _noChange => _lastChecked != null && _lastChecked == _textController.text;
bool get _noChange =>
_lastChecked != null && _lastChecked == _textController.text;
bool get isRunningIT => choreoMode == ChoreoMode.it && !itController.isTranslationDone;
bool get isRunningIT =>
choreoMode == ChoreoMode.it && !itController.isTranslationDone;
void startLoading() {
_lastChecked = _textController.text;
@ -652,15 +676,18 @@ class Choreographer {
_currentAssistanceState = assistanceState;
}
LayerLinkAndKey get itBarLinkAndKey => MatrixState.pAnyState.layerLinkAndKey(itBarTransformTargetKey);
LayerLinkAndKey get itBarLinkAndKey =>
MatrixState.pAnyState.layerLinkAndKey(itBarTransformTargetKey);
String get itBarTransformTargetKey => 'it_bar$roomId';
LayerLinkAndKey get inputLayerLinkAndKey => MatrixState.pAnyState.layerLinkAndKey(inputTransformTargetKey);
LayerLinkAndKey get inputLayerLinkAndKey =>
MatrixState.pAnyState.layerLinkAndKey(inputTransformTargetKey);
String get inputTransformTargetKey => 'input$roomId';
LayerLinkAndKey get itBotLayerLinkAndKey => MatrixState.pAnyState.layerLinkAndKey(itBotTransformTargetKey);
LayerLinkAndKey get itBotLayerLinkAndKey =>
MatrixState.pAnyState.layerLinkAndKey(itBotTransformTargetKey);
String get itBotTransformTargetKey => 'itBot$roomId';
@ -674,7 +701,8 @@ class Choreographer {
chatController.room,
);
bool get isAutoIGCEnabled => pangeaController.permissionsController.isToolEnabled(
bool get isAutoIGCEnabled =>
pangeaController.permissionsController.isToolEnabled(
ToolSetting.autoIGC,
chatController.room,
);
@ -706,7 +734,10 @@ class Choreographer {
bool get canSendMessage {
// if there's an error, let them send. we don't want to block them from sending in this case
if (errorService.isError || l2Lang == null || l1Lang == null || _timesClicked > 1) {
if (errorService.isError ||
l2Lang == null ||
l1Lang == null ||
_timesClicked > 1) {
return true;
}
@ -725,8 +756,10 @@ class Choreographer {
}
// if they have relevant matches, don't let them send
final hasITMatches = igc.igcTextData!.matches.any((match) => match.isITStart);
final hasIGCMatches = igc.igcTextData!.matches.any((match) => !match.isITStart);
final hasITMatches =
igc.igcTextData!.matches.any((match) => match.isITStart);
final hasIGCMatches =
igc.igcTextData!.matches.any((match) => !match.isITStart);
if ((itEnabled && hasITMatches) || (igcEnabled && hasIGCMatches)) {
return false;
}

View file

@ -1,6 +1,12 @@
import 'dart:async';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:fluffychat/pangea/choreographer/controllers/choreographer.dart';
import 'package:fluffychat/pangea/choreographer/controllers/error_service.dart';
import 'package:fluffychat/pangea/choreographer/controllers/span_data_controller.dart';
@ -10,11 +16,6 @@ import 'package:fluffychat/pangea/choreographer/repo/igc_repo.dart';
import 'package:fluffychat/pangea/choreographer/widgets/igc/span_card.dart';
import 'package:fluffychat/pangea/events/event_wrappers/pangea_message_event.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import '../../common/utils/error_handler.dart';
import '../../common/utils/overlay.dart';
@ -81,8 +82,10 @@ class IgcController {
userId: choreographer.pangeaController.userController.userId!,
userL1: choreographer.l1LangCode!,
userL2: choreographer.l2LangCode!,
enableIGC: choreographer.igcEnabled && choreographer.choreoMode != ChoreoMode.it,
enableIT: choreographer.itEnabled && choreographer.choreoMode != ChoreoMode.it,
enableIGC: choreographer.igcEnabled &&
choreographer.choreoMode != ChoreoMode.it,
enableIT: choreographer.itEnabled &&
choreographer.choreoMode != ChoreoMode.it,
prevMessages: _prevMessages(),
);
@ -101,10 +104,13 @@ class IgcController {
}
final IGCTextData igcTextDataResponse =
await _igcTextDataCache[reqBody.hashCode]!.data.timeout((const Duration(seconds: 10)));
await _igcTextDataCache[reqBody.hashCode]!
.data
.timeout((const Duration(seconds: 10)));
// this will happen when the user changes the input while igc is fetching results
if (igcTextDataResponse.originalInput.trim() != choreographer.currentText.trim()) {
if (igcTextDataResponse.originalInput.trim() !=
choreographer.currentText.trim()) {
return;
}
// get ignored matches from the original igcTextData
@ -120,7 +126,8 @@ class IgcController {
final List<PangeaMatch> filteredMatches = List.from(igcTextData!.matches);
for (final PangeaMatch match in igcTextData!.matches) {
final _IgnoredMatchCacheItem cacheEntry = _IgnoredMatchCacheItem(match: match);
final _IgnoredMatchCacheItem cacheEntry =
_IgnoredMatchCacheItem(match: match);
if (_ignoredMatchCache.containsKey(cacheEntry.hashCode)) {
filteredMatches.remove(match);
@ -139,7 +146,8 @@ class IgcController {
// This will make the loading of span details faster for the user
if (igcTextData?.matches.isNotEmpty ?? false) {
for (int i = 0; i < igcTextData!.matches.length; i++) {
if (!igcTextData!.matches[i].isITStart && choreographer.l2Lang != null) {
if (!igcTextData!.matches[i].isITStart &&
choreographer.l2Lang != null) {
spanDataController.getSpanDetails(i, choreographer.l2Lang!);
}
}
@ -162,7 +170,8 @@ class IgcController {
"itEnabled": choreographer.itEnabled,
"matches": igcTextData?.matches.map((e) => e.toJson()),
},
level: err is TimeoutException ? SentryLevel.warning : SentryLevel.error,
level:
err is TimeoutException ? SentryLevel.warning : SentryLevel.error,
);
clear();
}
@ -225,7 +234,8 @@ class IgcController {
.where(
(e) =>
e.type == EventTypes.Message &&
(e.messageType == MessageTypes.Text || e.messageType == MessageTypes.Audio),
(e.messageType == MessageTypes.Text ||
e.messageType == MessageTypes.Audio),
)
.toList();
@ -236,7 +246,8 @@ class IgcController {
: PangeaMessageEvent(
event: event,
timeline: choreographer.chatController.timeline!,
ownMessage: event.senderId == choreographer.pangeaController.matrixState.client.userID,
ownMessage: event.senderId ==
choreographer.pangeaController.matrixState.client.userID,
).getSpeechToTextLocal()?.transcript.text.trim(); // trim whitespace
if (content == null) continue;
messages.add(

View file

@ -1,14 +1,16 @@
import 'dart:async';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:fluffychat/pangea/choreographer/controllers/choreographer.dart';
import 'package:fluffychat/pangea/choreographer/models/span_data.dart';
import 'package:fluffychat/pangea/choreographer/repo/span_data_repo.dart';
import 'package:fluffychat/pangea/choreographer/utils/normalize_text.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/learning_settings/models/language_model.dart';
import 'package:flutter/foundation.dart';
class _SpanDetailsCacheItem {
Future<SpanDetailsRepoReqAndRes> data;
@ -69,7 +71,8 @@ class SpanDataController {
);
return correctChoice != null &&
normalizeString(correctChoice, spanLanguage.langCode) == normalizeString(errorSpan, spanLanguage.langCode);
normalizeString(correctChoice, spanLanguage.langCode) ==
normalizeString(errorSpan, spanLanguage.langCode);
}
Future<void> getSpanDetails(
@ -78,7 +81,8 @@ class SpanDataController {
bool force = false,
}) async {
final SpanData? span = _getSpan(matchIndex);
if (span == null || (isNormalizationError(matchIndex, spanLanguage) && !force)) return;
if (span == null ||
(isNormalizationError(matchIndex, spanLanguage) && !force)) return;
final req = SpanDetailsRepoReqAndRes(
userL1: choreographer.l1LangCode!,
@ -109,7 +113,8 @@ class SpanDataController {
}
try {
choreographer.igc.igcTextData!.matches[matchIndex].match = (await response).span;
choreographer.igc.igcTextData!.matches[matchIndex].match =
(await response).span;
} catch (err, s) {
ErrorHandler.logError(e: err, s: s, data: req.toJson());
_cache.remove(cacheKey);

View file

@ -1,7 +1,8 @@
import 'package:diacritic/diacritic.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:test/test.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
// The intention of this function is to normalize text for comparison purposes.
// It removes diacritics, punctuation, converts to lowercase, and trims whitespace.
// We would like esta = está, hello! = Hello, etc.
@ -16,11 +17,13 @@ String normalizeString(String input, String languageCode) {
normalized = _applyLanguageSpecificNormalization(normalized, languageCode);
// Step 3: Replace hyphens and other dash-like characters with spaces
normalized = normalized.replaceAll(RegExp(r'[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]'), ' ');
normalized = normalized.replaceAll(
RegExp(r'[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]'), ' ');
// Step 4: Remove punctuation (including Unicode punctuation)
// This removes ASCII and Unicode punctuation while preserving letters, numbers, and spaces
normalized = normalized.replaceAll(RegExp(r'[\p{P}\p{S}]', unicode: true), '');
normalized =
normalized.replaceAll(RegExp(r'[\p{P}\p{S}]', unicode: true), '');
// Step 5: Normalize whitespace (collapse multiple spaces, trim)
normalized = normalized.replaceAll(RegExp(r'\s+'), ' ').trim();
@ -431,11 +434,13 @@ void runNormalizationTests() {
passed++;
print('✓ Test ${i + 1} PASSED: "$input" → "$actual"');
} else {
print('✗ Test ${i + 1} FAILED: "$input" → "$actual" (expected: "$expected")');
print(
'✗ Test ${i + 1} FAILED: "$input" → "$actual" (expected: "$expected")');
}
}
print('\nTest Results: $passed/$total tests passed (${(passed / total * 100).toStringAsFixed(1)}%)');
print(
'\nTest Results: $passed/$total tests passed (${(passed / total * 100).toStringAsFixed(1)}%)');
}
// Main function to run the tests when executed directly
@ -448,7 +453,8 @@ void main() {
final expected = testCase['expected']!;
test('Test ${i + 1}: "$input" should normalize to "$expected"', () {
final actual = normalizeString(input, 'en'); // Default to English for tests
final actual =
normalizeString(input, 'en'); // Default to English for tests
expect(
actual,
equals(expected),

View file

@ -1,3 +1,5 @@
import 'package:flutter/material.dart';
import 'package:fluffychat/l10n/l10n.dart';
import 'package:fluffychat/pangea/bot/utils/bot_style.dart';
import 'package:fluffychat/pangea/choreographer/controllers/choreographer.dart';
@ -7,8 +9,6 @@ import 'package:fluffychat/pangea/choreographer/models/pangea_match_model.dart';
import 'package:fluffychat/pangea/choreographer/models/span_data.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/toolbar/controllers/tts_controller.dart';
import 'package:flutter/material.dart';
import '../../../../widgets/matrix.dart';
import '../../../bot/widgets/bot_face_svg.dart';
import '../choice_array.dart';
@ -54,7 +54,8 @@ class SpanCardState extends State<SpanCard> {
PangeaMatch? get pangeaMatch {
if (widget.choreographer.igc.igcTextData == null) return null;
if (widget.matchIndex >= widget.choreographer.igc.igcTextData!.matches.length) {
if (widget.matchIndex >=
widget.choreographer.igc.igcTextData!.matches.length) {
ErrorHandler.logError(
m: "matchIndex out of bounds in span card",
data: {
@ -74,7 +75,8 @@ class SpanCardState extends State<SpanCard> {
}
SpanChoice? _choiceByIndex(int index) {
if (pangeaMatch?.match.choices == null || pangeaMatch!.match.choices!.length <= index) {
if (pangeaMatch?.match.choices == null ||
pangeaMatch!.match.choices!.length <= index) {
return null;
}
return pangeaMatch?.match.choices?[index];
@ -86,7 +88,8 @@ class SpanCardState extends State<SpanCard> {
}
// if user ever selected the correct choice, automatically select it
final selectedCorrectIndex = pangeaMatch!.match.choices!.indexWhere((choice) {
final selectedCorrectIndex =
pangeaMatch!.match.choices!.indexWhere((choice) {
return choice.selected && choice.isBestCorrection;
});
@ -100,7 +103,8 @@ class SpanCardState extends State<SpanCard> {
final numChoices = pangeaMatch!.match.choices!.length;
for (int i = 0; i < numChoices; i++) {
final choice = _choiceByIndex(i);
if (choice!.timestamp != null && (mostRecent == null || choice.timestamp!.isAfter(mostRecent))) {
if (choice!.timestamp != null &&
(mostRecent == null || choice.timestamp!.isAfter(mostRecent))) {
mostRecent = choice.timestamp;
selectedChoiceIndex = i;
}
@ -151,7 +155,9 @@ class SpanCardState extends State<SpanCard> {
selectedChoice!.timestamp = DateTime.now();
selectedChoice!.selected = true;
setState(
() => (selectedChoice!.isBestCorrection ? BotExpression.gold : BotExpression.surprised),
() => (selectedChoice!.isBestCorrection
? BotExpression.gold
: BotExpression.surprised),
);
}
}
@ -177,7 +183,8 @@ class SpanCardState extends State<SpanCard> {
}
void _showFirstMatch() {
if (widget.choreographer.igc.igcTextData != null && widget.choreographer.igc.igcTextData!.matches.isNotEmpty) {
if (widget.choreographer.igc.igcTextData != null &&
widget.choreographer.igc.igcTextData!.matches.isNotEmpty) {
widget.choreographer.igc.showFirstMatch(context);
} else {
MatrixState.pAnyState.closeOverlay();
@ -235,10 +242,12 @@ class WordMatchContent extends StatelessWidget {
),
)
.toList(),
onPressed: (value, index) => controller._onChoiceSelect(index),
onPressed: (value, index) =>
controller._onChoiceSelect(index),
selectedChoiceIndex: controller.selectedChoiceIndex,
id: controller.pangeaMatch!.hashCode.toString(),
langCode: MatrixState.pangeaController.languageController.activeL2Code(),
langCode: MatrixState.pangeaController.languageController
.activeL2Code(),
),
const SizedBox(height: 12),
PromptAndFeedback(controller: controller),
@ -275,7 +284,9 @@ class WordMatchContent extends StatelessWidget {
child: Opacity(
opacity: controller.selectedChoiceIndex != null ? 1.0 : 0.5,
child: TextButton(
onPressed: controller.selectedChoiceIndex != null ? controller._onReplaceSelected : null,
onPressed: controller.selectedChoiceIndex != null
? controller._onReplaceSelected
: null,
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all<Color>(
(controller.selectedChoice != null
@ -322,7 +333,9 @@ class PromptAndFeedback extends StatelessWidget {
}
return Container(
constraints: controller.pangeaMatch!.isITStart ? null : const BoxConstraints(minHeight: 75.0),
constraints: controller.pangeaMatch!.isITStart
? null
: const BoxConstraints(minHeight: 75.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
@ -352,9 +365,11 @@ class PromptAndFeedback extends StatelessWidget {
loading: controller.fetchingData,
),
],
if (!controller.fetchingData && controller.selectedChoiceIndex == null)
if (!controller.fetchingData &&
controller.selectedChoiceIndex == null)
Text(
controller.pangeaMatch!.match.type.typeName.defaultPrompt(context),
controller.pangeaMatch!.match.type.typeName
.defaultPrompt(context),
style: BotStyle.text(context).copyWith(
fontStyle: FontStyle.italic,
),