perform method

Future<StructuredResponse> perform(
  1. GlintSession session,
  2. CallToolRequest request, {
  3. ScrollTarget? target,
})

handle, optionally driving target and reporting sideEffect in the data when the route or a watched PageView page changed.

Implementation

Future<StructuredResponse> perform(GlintSession session, CallToolRequest request, {ScrollTarget? target}) async {
  final args = request.arguments ?? const {};
  final dirName = args['direction']! as String;
  final amount = (argNum(args, 'amountFraction') ?? session.config.scrollAmountFraction).toDouble();
  final returnScene = argBool(args, 'returnScene') ?? true;
  final fetchScene = argBool(args, 'fetchScene') ?? false;

  final dir = enumByName(ScrollDirection.values, dirName);
  if (dir == null) {
    return StructuredResponse.error(
      summary: 'unknown scroll direction: $dirName',
      errorKind: GlintErrorKind.invalidArgument,
      nextSteps: ['use one of: ${ScrollDirection.values.map((d) => d.name).join(', ')}'],
    );
  }

  // Device mode: no Flutter viewport probe — use the device's screen size.
  if (session.isDeviceMode) {
    final size = session.device.screenSize;
    if (size == null) {
      return StructuredResponse.error(
        summary: 'device-mode scroll needs a known screen size; none available',
        errorKind: GlintErrorKind.unsupportedBackendAction,
        nextSteps: const [
          'take a `device op:screenshot` and use swipe x1,y1,x2,y2',
        ],
      );
    }
    final line = _swipeLine(dir, amount, (x: 0.0, y: 0.0, w: size.w.toDouble(), h: size.h.toDouble()));
    return coordinateSwipe(session, line.fromX, line.fromY, line.toX, line.toY, 300,
        verb: 'scrolled', holdMs: scrollHoldMs);
  }

  final horizontal = dir == ScrollDirection.left || dir == ScrollDirection.right;
  final action = await openActionScene(session, snapshot: returnScene);
  try {
    final anchor = target?.anchor ??
        (action.semantic == null ? null : await session.scrollAnchorIn(action.scene, action.semantic!));
    final ({double logicalW, double logicalH, double dpr}) vp;
    try {
      vp = await session.viewportIn(action.scene);
    } on GeometryResolveError catch (e) {
      return StructuredResponse.error(
        summary: 'scroll could not measure the viewport',
        errorKind: GlintErrorKind.geometryResolveError,
        detail: e.message,
        nextSteps: const [
          'the screen may be mid-transition — wait_for_settle, then retry',
          'or pass x1,y1,x2,y2 to swipe by coordinates',
        ],
      );
    }
    final keyboard = (await session.uiState()).keyboardBottomPx / vp.dpr;
    final region = swipeRegion((x: 0.0, y: 0.0, w: vp.logicalW, h: vp.logicalH - keyboard), anchor?.clip,
        horizontal: horizontal);
    final line = _swipeLine(dir, amount, region);
    var response = await coordinateSwipe(session, line.fromX, line.fromY, line.toX, line.toY, 300,
        verb: 'scrolled', holdMs: scrollHoldMs);
    if (returnScene && !response.isError) {
      final post = await readPostActionState(session, action.pre,
          includeSceneText: fetchScene, scrollAnchor: anchor, horizontalScroll: horizontal);
      if (post != null) {
        final movedPx = post.scrolledPx;
        final merged = mergeScrollSignal(post.changeCategory, movedPx);
        response = response.mergeData(scrollSignalData(post, merged, movedPx));
        final sideEffect = target == null ? null : sideEffectOf(action.pre, post.snapshot, target.watchedPagedIds);
        if (sideEffect != null) response = response.mergeData({'sideEffect': sideEffect});
        if (merged.changed == false) {
          final reason = await _whyStill(session, action.scene, anchor, dir);
          response = response
              .copyWith(
            summary: '${response.summary}; nothing moved: ${_reasonText[reason]}',
          )
              .mergeData({'reason': reason});
        }
      }
    }
    return response;
  } finally {
    await action.dispose();
  }
}