execute method
Execute async operation with state management and cost tracking
Implementation
Future<T?> execute(
Future<T> Function() fn, {
double estimatedCost = 0.0,
}) async {
try {
_setState(AIState.loading());
final result = await fn();
_totalCost += estimatedCost;
// Check budget
final budgetExceeded = _checkBudgetExceeded(_totalCost);
final cost = CostSummary(
totalCost: estimatedCost,
);
_setState(AIState.success(
result,
cost: cost,
).copyWith(budgetExceeded: budgetExceeded));
return result;
} catch (e) {
final error = e is Exception ? e : Exception(e.toString());
_setState(AIState.error(error));
return null;
}
}