flutter_agentic_graph 0.2.0
flutter_agentic_graph: ^0.2.0 copied to clipboard
Stateful agent graphs for Flutter — nodes, conditional edges, cycles, checkpointing, and human-in-the-loop. LangGraph-style orchestration for flutter_agentic.
// Example: a draft → review loop with a human approval gate.
//
// Run-through of the core flutter_agentic_graph concepts: shared state,
// cycles via conditional edges, checkpointing, and human-in-the-loop.
// ignore_for_file: avoid_print
import 'package:flutter_agentic_graph/flutter_agentic_graph.dart';
Future<void> main() async {
final graph =
StateGraph(reducers: {'log': Reducers.append})
..addNode('draft', (state, ctx) async {
final attempt = (state['attempts'] as int? ?? 0) + 1;
return {
'draft': 'Draft v$attempt',
'attempts': attempt,
'log': 'drafted v$attempt',
};
})
..addNode('review', (state, ctx) async {
return {
'approved': (state['attempts'] as int) >= 2,
'log': 'reviewed',
};
})
..addNode('confirm', (state, ctx) {
// Dynamic human-in-the-loop: pause until the user answers.
final answer = ctx.resumeValue;
if (answer == null) ctx.interrupt('Publish "${state['draft']}"?');
return {'published': answer == true, 'log': 'confirmed'};
})
..setEntryPoint('draft')
..addEdge('draft', 'review')
..addConditionalEdge(
'review',
(s) => s['approved'] == true ? 'confirm' : 'draft',
)
..addEdge('confirm', StateGraph.end);
final app = graph.compile(
checkpointer: MemoryCheckpointer(),
recursionLimit: 10,
);
var result = await app.invoke({}, threadId: 'demo', onEvent: print);
if (result is GraphInterrupted) {
print('Agent asks: ${result.payload}');
result = await app.resume('demo', resumeValue: true);
}
final done = result as GraphComplete;
print('Published: ${done.state['published']} after ${done.steps} steps');
print('Trail: ${done.state['log']}');
}