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?) ?? 'status';
  if (op == 'screenshot' && session.active != null && (args['udid'] == null || args['apps'] != null)) {
    if (argBool(args, 'latest') ?? false) return _latest(session, args);
    return attachedScreenshot(session, args, request);
  }
  final pooledAndroid = op == 'screenshot' ? _pooledAndroid(session, args['udid'] as String?) : null;
  if (pooledAndroid != null) {
    return session.withApp(
      pooledAndroid,
      () => argBool(args, 'latest') ?? false ? _latest(session, args) : attachedScreenshot(session, args, request),
    );
  }

  final udid = _resolveUdid(session, args['udid'] as String?);
  if (udid == null) {
    return StructuredResponse.error(
      summary: 'no target simulator',
      errorKind: GlintErrorKind.invalidArgument,
      nextSteps: const [
        'pass udid, or attach to an iOS simulator first',
      ],
    );
  }

  if (isPhysicalIosUdid(udid) && op == 'status') {
    final status = await phoneStatus(udid);
    if (status == null) {
      return StructuredResponse.error(
        summary: 'the iPhone $udid did not answer devicectl',
        errorKind: GlintErrorKind.deviceNotConnected,
        nextSteps: const ['connect and unlock the iPhone, then retry'],
      );
    }
    return StructuredResponse(
      summary: '${status['name']} · ${status['model']} · ${status['os']} (${status['build']}) · '
          '${status['connection']}, tunnel ${status['tunnel']} · developer mode ${status['developerMode']}'
          '${status['developerImage'] == true ? '' : ' · developer image not mounted'}'
          '${status['passcodeRequired'] == true ? ' · locked, needs its passcode' : ''}',
      data: {'udid': udid, ...status},
    );
  }
  if (isPhysicalIosUdid(udid) && op == 'network') {
    return _phoneNetwork(session, udid, (args['value'] as String?) ?? 'list');
  }
  if (isPhysicalIosUdid(udid) && op == 'openurl') {
    final url = args['value'] as String?;
    if (url == null || url.isEmpty) return _bad('op=openurl requires value: <url/deeplink>');
    final err = await phoneOpenUrl(udid, url);
    return _result(err, 'opened $url on $udid');
  }
  if (isPhysicalIosUdid(udid)) {
    final ability = switch (op) {
      'status' => Ability.deviceStatus,
      'appearance' => Ability.deviceAppearance,
      'openurl' => Ability.deviceOpenUrl,
      'privacy' => Ability.devicePrivacy,
      'biometric' || 'enrol' || 'unenrol' || 'match' || 'nomatch' => Ability.deviceBiometric,
      _ => null,
    };
    if (ability != null) return StructuredResponse.notOnThisDevice(ability, iphoneAbilities.gaps[ability]!);
  }
  if (_isAndroid(session, udid)) return _android(session, udid, op, args);

  const sim = SimControl();
  switch (op) {
    case 'status':
      final status = await sim.status(udid);
      if (status == null) {
        return StructuredResponse.error(
          summary: 'no simulator with udid $udid',
          errorKind: GlintErrorKind.targetNotFound,
        );
      }
      final title = status.deviceType != null && status.deviceType != status.name
          ? '${status.name} (${status.deviceType})'
          : status.name;
      return StructuredResponse(
        summary: [
          title,
          'os:         ${status.osVersion ?? "?"}',
          'state:      ${status.state}',
          'appearance: ${status.appearance ?? "?"}',
          'textSize:   ${status.contentSize ?? "?"}',
          'biometrics: ${switch (status.biometricEnrolled) {
            true => "enrolled",
            false => "not enrolled",
            null => "?",
          }}',
        ].join('\n'),
        data: {'status': status.toJson()},
      );

    case 'appearance':
      final value = args['value'] as String?;
      if (value != 'light' && value != 'dark') {
        return _bad('op=appearance requires value: light | dark');
      }
      final err = await sim.setAppearance(udid, value!);
      return _result(err, '$udid appearance → $value');

    case 'openurl':
      final url = args['value'] as String?;
      if (url == null || url.isEmpty) {
        return _bad('op=openurl requires value: <url/deeplink>');
      }
      final err = await sim.openUrl(udid, url);
      return _result(err, 'opened $url on $udid');

    case 'screenshot':
      final path = '${Directory.systemTemp.path}/glint-shot-$udid-'
          '${DateTime.now().millisecondsSinceEpoch}.png';
      final shot = await sim.screenshot(udid, path);
      if (shot.error != null || shot.path == null) {
        return StructuredResponse.error(
          summary: shot.error ?? 'screenshot failed',
          errorKind: GlintErrorKind.backendToolError,
        );
      }
      return _savedShot(session, args, shot);

    case 'biometric':
      return _biometric(sim, udid, args);

    case 'privacy':
      final action = args['action'] as String?;
      final service = args['service'] as String?;
      final bundleId = args['bundleId'] as String?;
      if (!const {'grant', 'revoke', 'reset'}.contains(action)) {
        return _bad('op=privacy requires action: grant | revoke | reset');
      }
      if (service == null || service.isEmpty) {
        return _bad('op=privacy requires service (e.g. photos, microphone)');
      }
      if (action != 'reset' && (bundleId == null || bundleId.isEmpty)) {
        return _bad('op=privacy $action requires bundleId');
      }
      if (service == 'notifications') return _iosNotificationsPrivacy(udid, bundleId);
      final err = await sim.privacy(udid, action!, service, bundleId: bundleId);
      return _result(
          err,
          'privacy $action $service'
          '${bundleId != null ? " for $bundleId" : ""} on $udid');

    default:
      return _bad('unknown op: $op '
          '(use status | appearance | openurl | screenshot | privacy)');
  }
}