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 args = request.arguments ?? const {};
  final op = (args['op'] as String?) ?? 'map';
  if (!const {'map', 'frontier', 'path', 'reset'}.contains(op)) {
    return StructuredResponse.error(
      summary: 'unknown op: $op',
      errorKind: GlintErrorKind.invalidArgument,
      nextSteps: const ['use map, frontier, path or reset'],
    );
  }
  final app = session.active;
  if (app == null || app.deviceMode) throw SessionNotAttachedError(missing: 'explore');
  final recorder = session.explore;
  if (op == 'reset') return _reset(recorder, app);
  if (op == 'path' && ((args['to'] as String?)?.trim().isEmpty ?? true)) {
    return StructuredResponse.error(
      summary: 'op=path needs to:',
      errorKind: GlintErrorKind.invalidArgument,
      nextSteps: const ['explore op:map lists the screens; pass an id like to:"s3" or its label'],
    );
  }
  final warning = await _refreshCurrent(session, app);
  final key = ExploreRecorder.keyOf(app);
  final graph = recorder.graphFor(key);
  final here = recorder.current(app);
  final now = DateTime.now();
  final warnings = [if (warning != null) warning, ...recorder.takeNotes(key)];
  return switch (op) {
    'frontier' => _frontier(graph, here, now, warnings),
    'path' => _path(graph, here, (args['to'] as String).trim(), warnings),
    _ => _map(graph, here, now, warnings),
  };
}