refactor(ai): enhance AI trace integration and OCR normalization
Test Suite / backend-pr-quick (push) Has been skipped
Test Suite / quick-import-pr-quick (push) Has been skipped
Test Suite / backend-full (push) Successful in 3m54s
Test Suite / flutter-quality (push) Failing after 1m29s

- Add FlyerTraceSupplement type for AI trace metadata
- Implement getFlyerTraceSupplements method to fetch trace supplements
- Update AiTraceService to include prompt/rawOutput and counters in flyer traces
- Add persistFlyerTrace method to FlyerImportService for trace persistence
- Enhance AiFlyerParserService to return structured trace data with prompts and retries
- Update FlyerNormalizerService with OCR typo fixes for cheese variants and spröd bakad firre
- Improve Flutter admin panel with selectable text, warnings display, and tooltips
- Add comprehensive tests for AI trace supplements and normalization rules
This commit is contained in:
Nils-Johan Gynther
2026-05-21 19:11:54 +02:00
parent 67a7590525
commit 026323b72a
9 changed files with 681 additions and 67 deletions
@@ -300,10 +300,13 @@ class _AdminAiPanelState extends ConsumerState<AdminAiPanel> {
title: Text(item.fileName ?? item.id),
subtitle: Text(
'${_formatDateTime(item.createdAt)}${item.userLabel}'),
trailing: Chip(
label: Text(item.status.label),
labelStyle: TextStyle(
color: _statusColor(item.status, theme.colorScheme)),
trailing: Tooltip(
message: _statusTooltipText(item),
child: Chip(
label: Text(item.status.label),
labelStyle: TextStyle(
color: _statusColor(item.status, theme.colorScheme)),
),
),
);
},
@@ -349,6 +352,14 @@ class _AdminAiPanelState extends ConsumerState<AdminAiPanel> {
return ListView(
children: [
_TraceMetaCard(detail: detail, formatDateTime: _formatDateTime),
if (detail.warnings.isNotEmpty) ...[
const SizedBox(height: 12),
_WarningsCard(
warnings: detail.warnings,
onCopyWarning: (warning) => _copyText(warning, 'Varning'),
onCopyAll: () => _copyText(detail.warnings.join('\n'), 'Varningar'),
),
],
const SizedBox(height: 12),
_PromptCard(
prompt: prompt,
@@ -375,6 +386,20 @@ class _AdminAiPanelState extends ConsumerState<AdminAiPanel> {
_cachedOutputPrettyJson = next;
return next;
}
String _statusTooltipText(AdminAiTraceListItem item) {
final parts = <String>[];
if (item.status == AdminAiTraceStatus.warning && item.warningsCount > 0) {
parts.add('${item.warningsCount} varning(ar). Välj raden för detaljer och kopiering.');
}
if (item.error != null && item.error!.trim().isNotEmpty) {
parts.add(item.error!.trim());
}
if (parts.isEmpty) {
return 'Inga ytterligare detaljer.';
}
return parts.join('\n');
}
}
class _TraceMetaCard extends StatelessWidget {
@@ -466,15 +491,13 @@ class _PromptCard extends StatelessWidget {
.withValues(alpha: 0.35),
borderRadius: BorderRadius.circular(8),
),
child: Text(
hasPrompt
? value
: 'Prompt är inte tillgänglig i denna fas för vald källa.',
maxLines: expanded ? null : 10,
overflow:
expanded ? TextOverflow.visible : TextOverflow.ellipsis,
style: theme.textTheme.bodySmall
?.copyWith(fontFamily: 'monospace'),
child: SelectionArea(
child: SelectableText(
hasPrompt ? value : 'Prompt saknas för detta spår.',
maxLines: expanded ? null : 10,
style: theme.textTheme.bodySmall
?.copyWith(fontFamily: 'monospace'),
),
),
),
],
@@ -484,15 +507,27 @@ class _PromptCard extends StatelessWidget {
}
}
class _OutputJsonCard extends StatelessWidget {
class _OutputJsonCard extends StatefulWidget {
final String jsonText;
final VoidCallback onCopy;
const _OutputJsonCard({required this.jsonText, required this.onCopy});
@override
State<_OutputJsonCard> createState() => _OutputJsonCardState();
}
class _OutputJsonCardState extends State<_OutputJsonCard> {
static const int _maxPreviewChars = 12000;
bool _expanded = false;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final shouldTruncate = widget.jsonText.length > _maxPreviewChars;
final visibleText =
!_expanded && shouldTruncate ? '${widget.jsonText.substring(0, _maxPreviewChars)}\n' : widget.jsonText;
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
@@ -506,11 +541,20 @@ class _OutputJsonCard extends StatelessWidget {
style: theme.textTheme.titleMedium)),
IconButton(
tooltip: 'Kopiera JSON',
onPressed: onCopy,
onPressed: widget.onCopy,
icon: const Icon(Icons.copy_all),
),
],
),
if (shouldTruncate)
Align(
alignment: Alignment.centerRight,
child: TextButton.icon(
onPressed: () => setState(() => _expanded = !_expanded),
icon: Icon(_expanded ? Icons.unfold_less : Icons.unfold_more),
label: Text(_expanded ? 'Visa mindre' : 'Visa hela outputen'),
),
),
const SizedBox(height: 8),
Container(
width: double.infinity,
@@ -520,12 +564,70 @@ class _OutputJsonCard extends StatelessWidget {
.withValues(alpha: 0.35),
borderRadius: BorderRadius.circular(8),
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text(
jsonText,
style: theme.textTheme.bodySmall
?.copyWith(fontFamily: 'monospace'),
child: SelectionArea(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SelectableText(
visibleText,
style: theme.textTheme.bodySmall
?.copyWith(fontFamily: 'monospace'),
),
),
),
),
],
),
),
);
}
}
class _WarningsCard extends StatelessWidget {
final List<String> warnings;
final void Function(String warning) onCopyWarning;
final VoidCallback onCopyAll;
const _WarningsCard({
required this.warnings,
required this.onCopyWarning,
required this.onCopyAll,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(
child: Text(
'Varningar (${warnings.length})',
style: theme.textTheme.titleMedium,
),
),
IconButton(
tooltip: 'Kopiera alla varningar',
onPressed: onCopyAll,
icon: const Icon(Icons.copy_all),
),
],
),
const SizedBox(height: 8),
...warnings.map(
(warning) => ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: const Icon(Icons.warning_amber_rounded, size: 18),
title: SelectableText(warning),
trailing: IconButton(
tooltip: 'Kopiera varning',
onPressed: () => onCopyWarning(warning),
icon: const Icon(Icons.copy, size: 18),
),
),
),
@@ -54,6 +54,8 @@ Widget _buildPanelApp(AdminRepository repo) {
}
void main() {
final veryLargeOutput = '{"payload":"${List.filled(13050, 'x').join()}"}';
final flyerItem = AdminAiTraceListItem(
id: 'flyer-101',
source: AdminAiTraceSource.flyer,
@@ -68,7 +70,7 @@ void main() {
warningsCount: 2,
hasPrompt: true,
hasOutput: true,
error: null,
error: 'Det finns 2 varningar i detaljvyn.',
);
final flyerDetail = AdminAiTraceDetail(
@@ -84,16 +86,11 @@ void main() {
durationMs: 1880,
retryCount: 1,
chunkCount: 3,
warnings: const ['parse:low_confidence'],
warnings: const ['parse:low_confidence', 'match:no_match'],
error: null,
prompt: 'Prompttext exempel',
rawOutput: '{"ok":true}',
normalizedOutput: const {
'sessionId': 101,
'items': [
{'rawName': 'Tomat'}
],
},
rawOutput: veryLargeOutput,
normalizedOutput: null,
summary: const {'itemCount': 1},
);
@@ -180,7 +177,7 @@ void main() {
expect(find.text('willys-v20.pdf'), findsOneWidget);
});
testWidgets('Prompt and output render and copy actions show snackbars',
testWidgets('Prompt/output are selectable and warning details are visible',
(tester) async {
await tester.binding.setSurfaceSize(const Size(1400, 1200));
final fakeRepo = _FakeAdminRepository(
@@ -200,6 +197,9 @@ void main() {
await tester.pump(const Duration(milliseconds: 500));
expect(find.text('Sammanfattning'), findsOneWidget);
expect(find.text('Varningar (2)'), findsOneWidget);
expect(find.text('parse:low_confidence'), findsOneWidget);
expect(find.text('match:no_match'), findsOneWidget);
final detailScroll = find.byType(Scrollable).last;
await tester.scrollUntilVisible(
find.text('Model Output'),
@@ -209,12 +209,19 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('Model Output'), findsOneWidget);
expect(find.textContaining('"sessionId": 101'), findsOneWidget);
expect(find.byType(SelectableText), findsWidgets);
expect(find.text('Visa hela outputen'), findsOneWidget);
await tester.tap(find.text('Visa hela outputen'));
await tester.pumpAndSettle();
expect(find.text('Visa mindre'), findsOneWidget);
final copyPrompt = find.byTooltip('Kopiera');
final copyOutput = find.byTooltip('Kopiera JSON');
final copyWarnings = find.byTooltip('Kopiera alla varningar');
expect(copyPrompt, findsOneWidget);
expect(copyOutput, findsOneWidget);
expect(copyWarnings, findsOneWidget);
await tester.tap(copyPrompt);
await tester.pumpAndSettle();
@@ -224,6 +231,10 @@ void main() {
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
await tester.tap(copyWarnings);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
addTearDown(() => tester.binding.setSurfaceSize(null));
});
});