feat(app): add native authored stage definition pane

This commit is contained in:
2026-10-21 22:26:34 +01:00
parent 76fb2cdd87
commit d88213deb4
7 changed files with 1529 additions and 0 deletions
@@ -0,0 +1,212 @@
import 'graph_definition.dart';
import 'graph_protocol.dart';
/// The read-only definition of one declared stage, as field rows.
///
/// Mirrors `stageDefinitionFields`/`terminalFields` from `web/src/graph.js`:
/// the same labels, the same order, and the same rule that blank fields are
/// omitted rather than shown as an empty row. Pure data only: nothing here
/// reads run visits, so a definition can be inspected without claiming that a
/// stage executed.
class StageDefinitionField {
const StageDefinitionField(this.label, this.value, {this.block = false});
final String label;
final String value;
/// Multi-line content (prompt, output, outcomes, tools, memory, routes).
final bool block;
}
/// The one stage declaration [key] names, or null when this version does not
/// declare it.
///
/// The last declaration wins, matching the web pane's `new Map(stages)`.
Map<String, dynamic>? stageDefinitionFor(dynamic graph, String key) {
final stages = graph is Map ? graph['stages'] : null;
if (stages is! List) return null;
Map<String, dynamic>? found;
for (final stage in stages) {
if (stage is Map && _text(stage['key']) == key) {
found = Map<String, dynamic>.from(stage);
}
}
return found;
}
/// A stage's effective type: a blank/absent type reads `model`, exactly as
/// `effectiveStageType` in `web/src/templates.js`.
String effectiveStageType(dynamic type) {
final text = _text(type);
return text.trim().isEmpty ? 'model' : text;
}
/// A config key's display label (`configLabel` in `web/src/graph.js`).
String stageConfigLabel(String key) {
switch (key) {
case 'provider':
return 'Provider';
case 'model':
return 'Model';
case 'retries':
return 'Retries';
case 'timeout':
return 'Timeout';
case 'budget_usd':
return 'Budget (USD)';
case 'budget_tokens':
return 'Budget (tokens)';
case 'harness':
return 'Harness';
default:
return key;
}
}
/// The definition fields of one stage declaration, in the web pane's order.
///
/// Only non-blank fields are emitted. Unknown keys are deliberately not
/// interpreted here; the pane keeps the stage's raw JSON so they stay visible.
List<StageDefinitionField> stageDefinitionFields(dynamic stage) {
if (stage is! Map) return const <StageDefinitionField>[];
final fields = <StageDefinitionField>[
StageDefinitionField('Type', effectiveStageType(stage['type'])),
];
final prompt = _text(stage['prompt']);
if (prompt.trim().isNotEmpty) {
fields.add(StageDefinitionField('Prompt', prompt, block: true));
}
final output = _text(stage['output']);
if (output.trim().isNotEmpty) {
fields.add(StageDefinitionField('Output', output, block: true));
}
final edges = stage['edges'];
final outcomes = <String>[];
for (final outcome in stageOutcomes(stage)) {
final raw = edges is Map ? edges[outcome] : null;
final target = resolveTarget(outcome, raw == null ? '' : _text(raw)) ?? '';
final suffix = isTerminalTarget(target) ? ' (terminal)' : '';
outcomes.add('$outcome \u2192 ${nodeLabel(target)}$suffix');
}
if (outcomes.isNotEmpty) {
fields.add(
StageDefinitionField('Outcomes', outcomes.join('\n'), block: true),
);
}
final configs = stage['configs'];
if (configs is Map) {
final entries = configs.entries.toList()
..sort((a, b) => _text(a.key).compareTo(_text(b.key)));
for (final entry in entries) {
final value = _text(entry.value);
if (entry.value == null || value.trim().isEmpty) continue;
fields.add(
StageDefinitionField(stageConfigLabel(_text(entry.key)), value),
);
}
}
final blocks = stage['blocks'];
if (blocks is Map) {
final tools = blocks['tools'];
final text = _text(tools);
if (tools != null && text.trim().isNotEmpty) {
fields.add(StageDefinitionField('Tools', text, block: true));
}
}
final limits = stage['limits'];
if (limits is Map) {
final maxVisits = limits['max_visits'];
final visits = maxVisits is num
? maxVisits
: num.tryParse(_text(maxVisits));
if (visits != null && visits > 0) {
fields.add(StageDefinitionField('Max visits', _text(maxVisits)));
}
final on = _items(limits['on']);
if (on.isNotEmpty) {
fields.add(StageDefinitionField('Limit on', on.join(', ')));
}
}
final memory = stage['memory'];
if (memory is Map && memory.isNotEmpty) {
fields.add(
StageDefinitionField(
'Memory',
[
for (final entry in memory.entries)
'${_text(entry.key)}: ${_text(entry.value)}',
].join('\n'),
block: true,
),
);
}
final notify = stage['notify'];
if (notify is Map) {
final on = _items(notify['on']);
if (on.isNotEmpty) {
fields.add(StageDefinitionField('Notify on', on.join(', ')));
}
final destination = _text(notify['destination']);
if (destination.trim().isNotEmpty) {
fields.add(StageDefinitionField('Notify destination', destination));
}
final artifacts = _items(notify['artifacts']);
if (artifacts.isNotEmpty) {
fields.add(
StageDefinitionField('Notify artifacts', artifacts.join(', ')),
);
}
}
return fields;
}
/// A terminal pane's fields: the declared routes into that target.
///
/// A terminal is part of the definition, not evidence that a run reached it.
List<StageDefinitionField> terminalDefinitionFields(dynamic graph, String key) {
final lines = <String>[
for (final edge in graphEdges(graph))
if (edge.target == key)
'${edge.source} \u00b7 ${edge.label} \u2192 ${nodeLabel(key)}',
];
return lines.isEmpty
? const <StageDefinitionField>[]
: <StageDefinitionField>[
StageDefinitionField(
'Incoming routes',
lines.join('\n'),
block: true,
),
];
}
List<String> _items(dynamic value) {
if (value is! List) return const <String>[];
return <String>[for (final item in value) _text(item)];
}
/// Web `String(value)` for the values the definition fields carry. Numbers
/// that are integral render without a trailing `.0`, as JavaScript does; a
/// complex value stays visible but its exact shape is preserved by the raw
/// JSON section instead.
String _text(dynamic value) {
if (value == null) return '';
if (value is String) return value;
if (value is num) {
if (value.isFinite &&
value == value.roundToDouble() &&
value.abs() < 1e21) {
return value.toInt().toString();
}
return value.toString();
}
return value.toString();
}