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 t = readTargetedArgs(args, session.config);
  if (args['sequence'] != null) {
    final parsed = parseTapSequence(args['sequence']);
    if (parsed.error != null) return parsed.error!;
    final intervalMs = argInt(args, 'intervalMs') ?? 120;
    if (intervalMs < 0 || intervalMs > 2000) {
      return StructuredResponse.error(
        summary: 'intervalMs must be between 0 and 2000',
        errorKind: GlintErrorKind.invalidArgument,
        nextSteps: const ['omit it for the 120 ms default'],
      );
    }
    return runTapSequence(session, parsed.steps!,
        intervalMs: intervalMs, returnScene: t.returnScene, fetchScene: t.fetchScene);
  }

  // Coordinate tap — bypasses scene resolution; the only path in device mode.
  // In Flutter mode it still gets a changed-signal so raw x,y taps aren't
  // blind about whether anything happened.
  final pt = readPoint(args);
  if (pt != null) {
    final off = await rejectOffScreen(session, 'tap', pointCoordinates(pt.x, pt.y));
    if (off != null) return off;
    return withCoordinateChange(
      session,
      () => coordinateTap(session, pt.x, pt.y),
      returnScene: t.returnScene,
      fetchScene: t.fetchScene,
    );
  }

  final native =
      await resolveNativeTarget(session, glintId: args['glintId'] as String?, text: args['text'] as String?);
  if (native != null) {
    final hit = native.hit;
    if (hit == null) return native.error!;
    return withCoordinateChange(
      session,
      () => coordinateTap(session, hit.x, hit.y, label: hit.label),
      returnScene: t.returnScene,
      fetchScene: t.fetchScene,
    );
  }

  final target = await readTarget(session, args, t, tool: 'tap');
  if (target.error != null) return target.error!;
  final glintId = target.glintId!;
  final refuse = argBool(args, 'refuseNotHittable');

  final arming = await maybeAwaitReady(
    session: session,
    glintId: glintId,
    awaitReady: t.awaitReady,
    ceilingMs: t.readyTimeoutMs,
    toolLabel: 'tap',
  );
  if (arming is ArmingFailed) return arming.envelope;

  final action = await openActionScene(session, snapshot: t.returnScene);
  final scene = action.scene;
  try {
    final interactor = session.interactor..refuseNotHittable = refuse;
    var result = await interactor.run(scene, Tap(SymbolicTarget(glintId)));
    interactor.refuseNotHittable = null;
    final parent = visibleParentOf(result);
    if (parent != null) result = await focusThroughParent(session, scene, glintId, parent);
    var response = StructuredResponse.fromActionResult(result, detail: t.detail);
    if (arming is ArmingReady) response = withArmedMetadata(response, arming);

    if (!result.ok) {
      if (result.errorKind != GlintErrorKind.unresolvedTarget) return response;
      final hints = [
        if (labelHint(scene.root, glintId) case final h?) h,
        if (didYouMean(suggestIds(scene.glintIds, glintId)) case final h?) h,
      ];
      if (scene.overlayRoots.isEmpty) {
        return response.copyWith(nextSteps: [...hints, ...response.nextSteps]);
      }
      return StructuredResponse.error(
        summary: response.summary,
        errorKind: GlintErrorKind.unresolvedTarget,
        detail: 'glintId "$glintId" not found in scene. '
            'A ${scene.hasBarrierOverlay ? "modal" : "non-modal"} overlay is open, '
            'so the scene may have changed since your last get_scene.',
        nextSteps: [
          ...hints,
          'call get_scene to read the current overlay and base-screen ids',
        ],
      );
    }

    // Warn when tapping a base-screen node while a modal barrier is up — the
    // barrier absorbs the touch, so hittable:true can be misleading.
    if (scene.hasBarrierOverlay && !scene.isInOverlay(glintId)) {
      response = response.addWarnings(const [
        'a modal overlay is present; the tap may have landed on the barrier '
            'rather than your target — if the action had no effect, dismiss '
            'the dialog first and retry',
      ]);
    }

    return await appendPostAction(session, response, action.pre,
        returnScene: t.returnScene, fetchScene: t.fetchScene);
  } finally {
    await action.dispose();
  }
}