chain-studio/lib/pages/flows.dart
flemming-it 63985267d5
Some checks failed
Security / Security check (push) Failing after 1s
feat(studio): pass store catalog to editor + Add-module-source dialog
Two host-side pieces that complete the editor 0.18 quick-fix
loop:

  - FlowsPage now fetches the store catalog (`searchStore` with
    a 500-item ceiling) on open and hands it as `storeCapabilities`
    to the FlowEditorPage. The editor's analyzer uses it to
    decide whether an unknown-cap fix shows "Install …" or the
    Add-source alternative.

  - New `_AddModuleSourceDialog` — wired through the editor's
    `onAddModuleSource` callback. Asks the operator for a URL
    or local .fai bundle path; passes the value into
    `HubService.installModule(source: …)`. Honest about its
    limits: includes a card pointing the operator at
    `fai install --link <path>` for unpacked module source
    directories (which the gRPC install API doesn't currently
    support — that's CLI-only).

Studio bumped to 0.65.0; editor path-override pulls in 0.18.0.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
2026-06-09 00:22:09 +02:00

273 lines
9.4 KiB
Dart

// FlowsPage — Studio's flow surface. Thin host wrapper around
// the swappable fai_studio_flow_editor package: loads the
// hub's installed capabilities, hands them + a Studio-side
// FlowRunDriver to the editor, and lets the editor own
// everything else (file list, three-tab body, properties
// panel, run progress, …).
//
// Pre-0.52 versions split file-listing and run-dialog state
// across this page. v0.52 moves all of that into the editor
// package so a swap is one pubspec change — Studio doesn't
// need to know what's inside the editor any more.
import 'package:fai_studio_flow_editor/fai_studio_flow_editor.dart';
import 'package:flutter/material.dart';
import '../data/flow_run_driver.dart';
import '../data/hub.dart';
class FlowsPage extends StatefulWidget {
/// Pre-load this flow when the editor first builds. Studio
/// keeps the parameter so the Cmd+K palette + welcome page
/// can deep-link straight into a specific flow.
final String? initialFlowName;
const FlowsPage({super.key, this.initialFlowName});
@override
State<FlowsPage> createState() => _FlowsPageState();
}
class _FlowsPageState extends State<FlowsPage> {
late Future<List<String>> _capabilities;
late final StudioFlowRunDriver _driver;
/// Snapshot of bare capability names the public store can
/// install. Refreshed once at open so the editor's analyzer
/// can decide between "Install" (in store) and "Add source"
/// (not in store) without a per-keystroke network call.
List<String> _storeCaps = const [];
@override
void initState() {
super.initState();
_driver = StudioFlowRunDriver();
_capabilities = _loadCapabilities();
_loadStoreCapabilities();
}
Future<void> _loadStoreCapabilities() async {
try {
final items = await HubService.instance.searchStore(limit: 500);
if (!mounted) return;
// Store items carry a list of provided capabilities. We
// ingest all of them flat so a single store entry
// shipping multiple caps still reports each as installable.
// Falls back to the item name for legacy entries.
final caps = <String>{};
for (final item in items) {
if (item.requiresCapabilities.isNotEmpty) {
caps.addAll(item.requiresCapabilities);
}
caps.add(item.name);
}
setState(() => _storeCaps = caps.toList()..sort());
} catch (_) {
// Soft-fail: empty list disables the Install button on
// unknown-cap fixes, but Add-source remains available.
}
}
/// Pulls the capability identifiers the picker dialog
/// offers when adding a step. `<capability>@<version>` so
/// the operator can see exactly what they'll be wiring in,
/// matching the format the `use:` field expects. Soft-fails
/// to an empty list if the hub is unreachable — the editor
/// falls back to a free-form text field in that case.
Future<List<String>> _loadCapabilities() async {
try {
final caps = await HubService.instance.allCapabilities();
final entries =
caps.map((c) => '${c.capability}@${c.version}').toSet().toList()
..sort();
return entries;
} catch (_) {
return const <String>[];
}
}
/// Quick-fix handler — invoked when the operator clicks
/// `Install <capability>` on an analyzer issue. Parses the
/// editor-supplied `provider/name@version` spec into the
/// Hub's install API and returns the refreshed capability
/// list so the editor can re-analyze without waiting for the
/// parent widget rebuild.
Future<List<String>?> _onInstallCapability(String capability) async {
return _runInstall(source: capability);
}
/// `Add source for <cap>…` handler. Prompts the operator
/// for a local path or URL, then routes through the same
/// install RPC. Returns the refreshed capability list on
/// success, null on cancel / failure.
Future<List<String>?> _onAddModuleSource(String capability) async {
final source = await _AddModuleSourceDialog.show(context, capability);
if (source == null || source.isEmpty) return null;
return _runInstall(source: source);
}
Future<List<String>?> _runInstall({required String source}) async {
try {
await HubService.instance.installModule(source: source);
final caps = await HubService.instance.allCapabilities();
final updated = caps
.map((c) => '${c.capability}@${c.version}')
.toSet()
.toList()
..sort();
if (mounted) {
setState(() => _capabilities = Future.value(updated));
}
return updated;
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).removeCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Install failed: $e'),
behavior: SnackBarBehavior.floating,
),
);
}
return null;
}
}
@override
Widget build(BuildContext context) {
final locale = Localizations.localeOf(context);
final editorLocale = locale.languageCode == 'de'
? FlowEditorLocale.de
: FlowEditorLocale.en;
return FutureBuilder<List<String>>(
future: _capabilities,
builder: (context, snap) {
final caps = snap.data ?? const <String>[];
return FlowEditorPage(
initialFlowName: widget.initialFlowName,
locale: editorLocale,
runDriver: _driver,
availableCapabilities: caps,
storeCapabilities: _storeCaps,
onInstallCapability: _onInstallCapability,
onAddModuleSource: _onAddModuleSource,
);
},
);
}
}
/// Modal asking the operator where a missing capability can be
/// installed from — either a local module directory (the
/// `--link` install path) or a remote URL the hub can resolve.
/// Returns the typed source string on submit, null on cancel.
class _AddModuleSourceDialog extends StatefulWidget {
final String capability;
const _AddModuleSourceDialog({required this.capability});
static Future<String?> show(BuildContext context, String capability) {
return showDialog<String>(
context: context,
builder: (_) => _AddModuleSourceDialog(capability: capability),
);
}
@override
State<_AddModuleSourceDialog> createState() => _AddModuleSourceDialogState();
}
class _AddModuleSourceDialogState extends State<_AddModuleSourceDialog> {
final TextEditingController _ctrl = TextEditingController();
String _source = '';
@override
void dispose() {
_ctrl.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return AlertDialog(
title: const Text('Add module source'),
content: SizedBox(
width: 480,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'`${widget.capability}` is not in the public store. '
'Point the hub at a `.fai` bundle URL or a local bundle path '
'and the hub will install + verify it.',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
TextField(
controller: _ctrl,
autofocus: true,
onChanged: (v) => setState(() => _source = v.trim()),
decoration: const InputDecoration(
labelText: 'URL or path to .fai bundle',
hintText: 'https://example.com/foo-0.1.0.fai',
border: OutlineInputBorder(),
isDense: true,
),
),
const SizedBox(height: 12),
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerHigh,
borderRadius: BorderRadius.circular(6),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Developing a local module?',
style: theme.textTheme.labelMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 4),
Text(
'Studio installs from URLs + packed bundles only. For an '
'unpacked source directory, use the CLI:',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 6),
SelectableText(
'fai install --link /path/to/module',
style: theme.textTheme.bodySmall?.copyWith(
fontFamily: 'JetBrains Mono',
fontSize: 11,
color: theme.colorScheme.onSurface,
),
),
],
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel'),
),
FilledButton(
onPressed: _source.isEmpty
? null
: () => Navigator.of(context).pop(_source),
child: const Text('Install'),
),
],
);
}
}