handle method

  1. @override
Future<StructuredResponse> handle(
  1. GlintSession session,
  2. CallToolRequest request
)
override

Implementation

@override
Future<StructuredResponse> handle(GlintSession session, CallToolRequest request) async {
  final restart = argBool(request.arguments ?? const {}, 'restart') ?? false;
  final what = restart ? 'hot restart' : 'hot reload';
  if (GlintTool.pausedAtBreakpoint(session, 'a paused isolate cannot apply a $what') case final paused?) {
    return paused;
  }
  final runtime = session.runtime;
  final service = runtime.rawService;
  final isolateId = runtime.flutterIsolateId;
  final method = await _registeredMethod(service, restart ? 'hotRestart' : 'reloadSources');
  if (method == null) {
    return StructuredResponse.error(
      summary: 'no $what service on this app: it was not started by `flutter run`, or that `flutter run` has exited',
      errorKind: GlintErrorKind.unsupportedBackendAction,
      nextSteps: const [
        'relaunch with attach launch:"<project root>" so glint owns the `flutter run`',
        'or ask the user to press r (reload) or R (restart) in their `flutter run` terminal',
      ],
    );
  }
  final watch = Stopwatch()..start();
  final app = session.active;
  final errorsBefore = app?.errors.lastSeq ?? 0;
  var phase = 'reloading';
  final heartbeat = _heartbeat(session, request, watch, () => '$what: $phase');
  try {
    try {
      await service.callMethod(method, isolateId: isolateId).timeout(const Duration(seconds: 90));
    } on RPCError catch (e) {
      return StructuredResponse.error(
        summary: '$what failed',
        errorKind: GlintErrorKind.backendToolError,
        detail: '${e.message}${e.details == null ? '' : ': ${e.details}'}',
        nextSteps: const ['fix the reported error (often a compile error), then call hot_reload again'],
      );
    } on TimeoutException {
      return StructuredResponse.error(
        summary: '$what did not finish within 90s',
        errorKind: GlintErrorKind.backendToolError,
        nextSteps: const ['call get_scene to see whether the app came back', 'check the `flutter run` output'],
      );
    }
    final reloadedMs = watch.elapsedMilliseconds;
    if (restart) await app?.followRestartedIsolate();
    final touches = restart && app != null && app.showTouches ? await app.setShowTouches(true) : null;
    phase = 'settling and watching for errors';
    await _awaitAfterglow(session);
    final errors = app?.errors.since(errorsBefore) ?? const <FlutterErrorEntry>[];
    app?.recheckViewports = true;
    final blankScrollables = await _blankScrollables(session);
    return StructuredResponse(
      summary: '$what done in ${reloadedMs}ms${touches?.on == true ? ' · touches shown' : ''}'
          ' · ${errorsHeadline(errors)}',
      warnings: [
        if (touches != null && touches.failed) 'touches are not shown after the restart: ${touches.detail}',
        for (final e in errors) errorWarning(e, errorsBefore),
        if (blankScrollables > 0) emptyViewportWarning(blankScrollables),
      ],
      nextSteps: errors.isEmpty && blankScrollables == 0
          ? const ['call get_scene to read the updated screen']
          : [
              if (errors.map((e) => e.location).whereType<String>().firstOrNull case final where?)
                'open $where, fix the widget, then hot_reload again'
              else
                'find the widget behind the error in the source, fix it, then hot_reload again',
              'call get_scene to see what is on screen',
            ],
      data: {
        'restart': restart,
        'elapsedMs': reloadedMs,
        'totalMs': watch.elapsedMilliseconds,
        'errors': [for (final e in errors) e.toJson(since: errorsBefore)],
        if (blankScrollables > 0) 'emptyViewports': blankScrollables,
        if (touches != null) 'touches': touches.toJson(),
      },
    );
  } finally {
    heartbeat?.cancel();
  }
}