Some checks failed
Security / Security check (push) Failing after 1s
Bundles the error-UX overhaul and the inline log viewer:
- New `FaiLog` (`~/.fai/logs/studio-errors.log`, 256 KiB rotation,
JSON-per-line). Every operator-visible failure is appended so
`fai admin doctor` and the new viewer can show the trail
without the operator having to reproduce the failure.
- New `showFaiErrorSnack` / `showFaiErrorDialog` helpers wrap
`FaiErrorBox` in copyable surfaces; 27 ad-hoc
`SnackBar(content: Text(e.toString()))` sites swept to use
them (settings, doctor, audit, store, module sheet, system-AI
editor, flow output).
- New `FaiLogViewer` modal (`showFaiLogViewer`) renders log
files inline with line numbers, JSON-key + `[level]` token
colouring, Copy-all, Refresh, Open-externally. Doctor's
daemon-paths panel grows a "View" button next to "Open" for
every `.log` row and now also lists the Studio errors log.
- Today carousel: CTAs now actually re-run search after a
`filterCategory` / `runQuery` story is tapped (was only
flipping the chip state). Fallback story list bumped to 8.
- Editor bumped to git ref carrying 0.15.0 (type-token
colouring + analyzer diagnostics).
Studio bumped to 0.62.0.
Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
282 lines
9.2 KiB
Dart
282 lines
9.2 KiB
Dart
// FaiFlowOutput — type-aware renderer for one entry in
|
|
// `runSavedFlow`'s output map. Reads `FlowOutput` and dispatches
|
|
// to the right widget:
|
|
//
|
|
// - text: SelectableText for plain text; Markdown widget
|
|
// when the (hinted) MIME is text/markdown.
|
|
// - json: indented JSON in a code-block container.
|
|
// - bytes: inline image thumbnail when image/*, otherwise a
|
|
// "{mime}, {size}" summary. Always offers Save-as.
|
|
// - file: "Open" button → SystemActions.openInOs(uri).
|
|
//
|
|
// Stateless on purpose: the dialog wraps it; we never own a
|
|
// future or a controller here.
|
|
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
|
|
|
import '../data/error_presentation.dart';
|
|
import '../data/flow_output.dart';
|
|
import '../data/format.dart';
|
|
import '../data/system_actions.dart';
|
|
import '../l10n/app_localizations.dart';
|
|
import '../theme/theme.dart';
|
|
import '../theme/tokens.dart';
|
|
|
|
class FaiFlowOutput extends StatelessWidget {
|
|
final FlowOutput output;
|
|
|
|
const FaiFlowOutput({super.key, required this.output});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
switch (output) {
|
|
case FlowOutputText(:final text):
|
|
return _TextView(text: text);
|
|
case FlowOutputJson(:final pretty):
|
|
return _CodeBlock(text: pretty, theme: theme);
|
|
case FlowOutputBytes(:final bytes, :final mimeType):
|
|
return _BytesView(bytes: bytes, mimeType: mimeType);
|
|
case FlowOutputFile(:final uri, :final mimeType):
|
|
return _FileView(uri: uri, mimeType: mimeType);
|
|
case FlowOutputUnknown():
|
|
return _CodeBlock(
|
|
text: AppLocalizations.of(context)!.flowsOutputUnknown,
|
|
theme: theme,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Plain-text renderer. Auto-detects markdown via a cheap
|
|
/// heuristic (leading `#` heading, fenced code block, or
|
|
/// numbered/bulleted list) so flows that emit text/markdown
|
|
/// without setting an explicit MIME still render formatted.
|
|
class _TextView extends StatelessWidget {
|
|
final String text;
|
|
|
|
const _TextView({required this.text});
|
|
|
|
static final RegExp _markdownHeuristic = RegExp(
|
|
r'(^|\n)(#{1,6}\s|```|\*\s|-\s|\d+\.\s)',
|
|
);
|
|
|
|
bool get _looksLikeMarkdown =>
|
|
text.length < 64 * 1024 && _markdownHeuristic.hasMatch(text);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
if (_looksLikeMarkdown) {
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(FaiSpace.md),
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
border: Border.all(color: theme.colorScheme.outlineVariant),
|
|
),
|
|
child: MarkdownBody(
|
|
data: text,
|
|
selectable: true,
|
|
onTapLink: (_, href, _) async {
|
|
if (href != null && href.isNotEmpty) {
|
|
await SystemActions.openInOs(href);
|
|
}
|
|
},
|
|
styleSheet: FaiTheme.markdownStyle(theme),
|
|
),
|
|
);
|
|
}
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(FaiSpace.md),
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
border: Border.all(color: theme.colorScheme.outlineVariant),
|
|
),
|
|
child: SelectableText(
|
|
text,
|
|
style: theme.textTheme.bodyMedium?.copyWith(height: 1.5),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _CodeBlock extends StatelessWidget {
|
|
final String text;
|
|
final ThemeData theme;
|
|
|
|
const _CodeBlock({required this.text, required this.theme});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(FaiSpace.md),
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
border: Border.all(color: theme.colorScheme.outlineVariant),
|
|
),
|
|
child: SelectableText(
|
|
text,
|
|
style: FaiTheme.mono(size: 12, color: theme.colorScheme.onSurface),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _BytesView extends StatelessWidget {
|
|
final Uint8List bytes;
|
|
final String mimeType;
|
|
|
|
const _BytesView({required this.bytes, required this.mimeType});
|
|
|
|
bool get _isImage => mimeType.startsWith('image/');
|
|
|
|
/// Reasonable default filename for the Save dialog. Strips the
|
|
/// MIME up to the slash and uses the suffix when known
|
|
/// (image/png → output.png), otherwise a generic `.bin`.
|
|
String _defaultFilename() {
|
|
final slash = mimeType.indexOf('/');
|
|
if (slash < 0 || slash == mimeType.length - 1) return 'output.bin';
|
|
final sub = mimeType.substring(slash + 1);
|
|
// Strip common parameter suffixes (`png; charset=...`).
|
|
final semi = sub.indexOf(';');
|
|
final ext = semi < 0 ? sub : sub.substring(0, semi);
|
|
// `vnd.openxmlformats-…wordprocessingml.document` → docx.
|
|
if (ext.contains('wordprocessingml')) return 'output.docx';
|
|
if (ext.contains('spreadsheetml')) return 'output.xlsx';
|
|
if (ext.contains('presentationml')) return 'output.pptx';
|
|
return 'output.$ext';
|
|
}
|
|
|
|
Future<void> _save(BuildContext context) async {
|
|
final l = AppLocalizations.of(context)!;
|
|
final path = await FilePicker.saveFile(
|
|
dialogTitle: l.flowsOutputSaveAsTitle,
|
|
fileName: _defaultFilename(),
|
|
);
|
|
if (path == null) return;
|
|
try {
|
|
await File(path).writeAsBytes(bytes, flush: true);
|
|
if (!context.mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l.flowsOutputSavedAt(path))));
|
|
} catch (e) {
|
|
if (!context.mounted) return;
|
|
showFaiErrorSnack(context, 'flows.output.save', e);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final l = AppLocalizations.of(context)!;
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(FaiSpace.md),
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
border: Border.all(color: theme.colorScheme.outlineVariant),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
if (_isImage) ...[
|
|
ConstrainedBox(
|
|
constraints: const BoxConstraints(maxHeight: 240),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
child: Image.memory(bytes, fit: BoxFit.contain),
|
|
),
|
|
),
|
|
const SizedBox(height: FaiSpace.sm),
|
|
],
|
|
Text(
|
|
'${mimeType.isEmpty ? l.flowsOutputBytesUnknownMime : mimeType}'
|
|
' · ${humanBytes(bytes.length)}',
|
|
style: theme.textTheme.bodySmall?.copyWith(
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
const SizedBox(height: FaiSpace.sm),
|
|
OutlinedButton.icon(
|
|
icon: const Icon(Icons.save_alt, size: 16),
|
|
label: Text(l.flowsOutputSaveAs),
|
|
onPressed: () => _save(context),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _FileView extends StatelessWidget {
|
|
final String uri;
|
|
final String mimeType;
|
|
|
|
const _FileView({required this.uri, required this.mimeType});
|
|
|
|
Future<void> _open(BuildContext context) async {
|
|
// Most file-URIs come from modules that wrote a temp/output
|
|
// path on the host. SystemActions normalises `file://` for
|
|
// open-in-OS on all three platforms.
|
|
final target = uri.startsWith('file://') ? uri.substring(7) : uri;
|
|
final r = await SystemActions.openInOs(target);
|
|
if (!context.mounted || r.ok) return;
|
|
showFaiErrorSnack(
|
|
context,
|
|
'flows.output.open',
|
|
r.stderr.isEmpty ? 'open failed' : r.stderr,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final l = AppLocalizations.of(context)!;
|
|
return Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.all(FaiSpace.md),
|
|
decoration: BoxDecoration(
|
|
color: theme.colorScheme.surfaceContainerHigh,
|
|
borderRadius: BorderRadius.circular(FaiRadius.sm),
|
|
border: Border.all(color: theme.colorScheme.outlineVariant),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
SelectableText(
|
|
uri,
|
|
style: FaiTheme.mono(size: 11, color: theme.colorScheme.primary),
|
|
),
|
|
if (mimeType.isNotEmpty) ...[
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
mimeType,
|
|
style: theme.textTheme.bodySmall?.copyWith(
|
|
color: theme.colorScheme.onSurfaceVariant,
|
|
),
|
|
),
|
|
],
|
|
const SizedBox(height: FaiSpace.sm),
|
|
OutlinedButton.icon(
|
|
icon: const Icon(Icons.open_in_new, size: 16),
|
|
label: Text(l.flowsOutputOpen),
|
|
onPressed: () => _open(context),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|