handle method
Implementation
@override
Future<StructuredResponse> handle(GlintSession session, CallToolRequest request) async {
final args = request.arguments ?? const {};
final stopOnNoChange = argBool(args, 'stopOnNoChange') ?? true;
final returnScene = argBool(args, 'returnScene') ?? true;
final parsed = parseBatchSteps(args['steps'], maxSteps: _maxSteps);
if (parsed.error != null) return parsed.error!;
final steps = parsed.steps!;
if (observeRefusalForSteps(session, steps) case final refused?) return refused;
final budget = _budget(args);
final budgetMs = budget.ms;
final clock = Stopwatch()..start();
final run = await runBatchSteps(session, steps,
stopOnNoChange: stopOnNoChange, budget: Duration(milliseconds: budgetMs), meta: request.meta);
final overBudget = run.reason == 'budget';
final results = [for (final o in run.outcomes) o.toJson()];
final lines = [for (final o in run.outcomes) o.line()];
String? scene;
if (returnScene && !session.isDeviceMode) {
try {
scene = await session.withScene((s) async => const PlainTextSceneRenderer().render(s));
} on Object {
scene = null;
}
}
final unknownReason = run.outcomes.where((o) => o.changeUnknown).map((o) => o.unknownReason).firstOrNull;
final interrupted = overBudget && run.outcomes.last.interrupted;
final notRun =
overBudget ? [for (var k = run.stoppedAt! + (interrupted ? 1 : 0); k <= steps.length; k++) k] : <int>[];
final header = run.stoppedAt == null
? 'ran ${steps.length}/${steps.length} step(s)'
: interrupted
? 'stopped in step ${run.stoppedAt} of ${steps.length}: the ${budgetMs ~/ 1000} s budget ran out while it was running (interrupted, outcome unknown)'
: overBudget
? 'stopped before step ${run.stoppedAt} of ${steps.length}: the ${budgetMs ~/ 1000} s budget ran out after ${run.completed} step(s)'
: run.reason == 'error'
? 'stopped at step ${run.stoppedAt} of ${steps.length} (error)'
: 'stopped at step ${run.stoppedAt} of ${steps.length}: nothing changed';
return StructuredResponse(
warnings: [
if (budget.asked != null)
'budgetMs ${budget.asked} is ${budget.asked! < _minBudgetMs ? 'below the minimum of $_minBudgetMs' : 'above the cap of $_maxBudgetMs'}; used $budgetMs',
],
summary: [
header,
...lines,
if (scene != null) '',
if (scene != null) '--- scene after batch ---',
if (scene != null) scene.trimRight(),
].join('\n'),
isError: run.reason == 'error' || overBudget,
nextSteps: [
if (overBudget) ...[
if (run.completed > 0) 'steps 1-${run.completed} ran and are done; do not repeat them',
if (interrupted)
'step ${run.stoppedAt} was interrupted and its outcome is unknown: get_scene first, then repeat it only if it did not happen'
else
'continue with a new batch starting at step ${run.stoppedAt}',
if (interrupted && notRun.isNotEmpty) 'steps ${notRun.first}-${notRun.last} did not run',
'give the next batch budgetMs up to $_maxBudgetMs, or fewer steps',
'get_scene shows where the app stands now',
],
if (run.reason == 'error')
'read the failing step\'s detail above, fix its args, and resume from step ${run.stoppedAt}',
if (run.reason == 'noChange')
'step ${run.stoppedAt} did nothing — re-read the scene (or the scene above) before continuing',
if (unknownReason != null) 'a step\'s effect is unknown: $unknownReason',
],
data: {
'steps': results,
'completedSteps': run.completed,
'totalSteps': steps.length,
if (run.stoppedAt != null) 'stoppedAt': run.stoppedAt,
if (run.reason != null) 'reason': run.reason,
if (run.reason == 'error') 'errorKind': results.last['errorKind'],
if (overBudget) 'errorKind': GlintErrorKind.budgetExceeded.name,
if (overBudget)
'detail':
'ran ${run.completed} of ${steps.length} step(s) in ${clock.elapsedMilliseconds ~/ 1000} s against a $budgetMs ms budget; step ${run.stoppedAt} (${steps[run.stoppedAt! - 1].tool}) ${interrupted ? 'was interrupted, outcome unknown' : 'did not start'}',
if (overBudget) 'notRunSteps': notRun,
if (interrupted) 'interruptedStep': run.stoppedAt,
'budgetMs': budgetMs,
'elapsedMs': clock.elapsedMilliseconds,
},
);
}