Two follow-ups to the May-2026 trust pass. flutter_markdown_plus migration: - pubspec swaps `flutter_markdown ^0.7.7` (discontinued upstream) for `flutter_markdown_plus ^1.0.3`, the actively-maintained fork. API surface (MarkdownStyleSheet, Markdown, MarkdownBody) is unchanged — the four import sites in welcome, store, flow_output, and theme.dart get an updated package string and that's it. - All Studio analyzer + unit-test suites stay green. Integration test scaffold: - New `test/integration/hub_fixture.dart` boots a real `fai serve` subprocess on a free port against a temp FAI_DATA_DIR, polls until Healthy, exposes a ready HubClient. Idempotent teardown wipes the temp dir. - Resolves the `fai` binary from PATH first, then from `../fai_platform/target/release/fai`. When neither exists, the fixture calls `markTestSkipped` with a clear message — fresh checkouts don't fail. - One canonical test in `capabilities_test.dart` asserts on the bug class the May trust pass surfaced: that `system.approval` appears in `list_capabilities` with `kind=builtin` so Studio's missing-deps check never tries to install it. Plus a contract-shape test that every cap's `kind` is one of the three known wire values. - README documents the cold-start gotcha (first `fai serve` per machine takes ~30s to build the curated-model DB) plus the manual warmup recipe. Not in CI yet — wiring needs the platform build job to publish `fai` as a CI artifact for downstream consumption. Deferred until enough integration tests exist to justify the CI minutes. Signed-off-by: flemming-it <sf@flemming.it> Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
288 lines
9.3 KiB
Dart
288 lines
9.3 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/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.platform.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;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(l.flowsOutputSaveFailed('$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;
|
|
final l = AppLocalizations.of(context)!;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(l.flowsOutputOpenFailed(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),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|