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 limit = argInt(args, 'limit') ?? 50;
  final errorsOnly = argBool(args, 'errorsOnly') ?? false;
  final asJson = (args['format'] as String?) == 'json';
  final streamName = args['stream'] as String?;
  final sinceSeq = argInt(args, 'sinceSeq');
  final contains = _nonEmpty(args['contains']);
  final matchSource = _nonEmpty(args['match']);
  final maxChars = argInt(args, 'maxChars') ?? _defaultMaxChars;
  if (argBool(args, 'native') ?? false) return _nativeCrashes(session, asJson);

  AppLogStream? streamFilter;
  if (streamName != null) {
    streamFilter = AppLogStream.values.where((s) => s.name == streamName).firstOrNull;
    if (streamFilter == null) {
      return StructuredResponse.error(
        summary: 'unknown stream: $streamName',
        errorKind: GlintErrorKind.invalidArgument,
        nextSteps: const ['use stderr or logging'],
      );
    }
  }

  if (maxChars < 0) {
    return StructuredResponse.error(
      summary: 'maxChars cannot be negative',
      errorKind: GlintErrorKind.invalidArgument,
      detail: 'got maxChars:$maxChars',
      nextSteps: const ['pass a positive maxChars, or 0 for no per-entry cap'],
    );
  }

  RegExp? pattern;
  if (matchSource != null) {
    try {
      pattern = RegExp(matchSource);
    } on FormatException catch (e) {
      return StructuredResponse.error(
        summary: 'match is not a valid regex',
        errorKind: GlintErrorKind.invalidArgument,
        detail: e.message,
        nextSteps: const [
          r'escape regex characters such as [ ] ( ) . with a backslash, e.g. match:"\[EventTracker\]"',
          'or use contains:"<text>" for a plain case-insensitive substring',
        ],
      );
    }
  }

  final scan = session.appLogs.scan(
    limit: limit,
    errorsOnly: errorsOnly,
    streamFilter: streamFilter,
    sinceSeq: sinceSeq,
    contains: contains,
    pattern: pattern,
  );
  final shown = _fitTotalCap(scan.entries.map((e) => _CutEntry(e, maxChars)).toList());
  final entries = [for (final c in shown) c.entry];
  final filtered = contains != null || pattern != null;

  final body = shown.isEmpty ? '(no entries)${_whyEmpty(session)}' : shown.map((c) => c.render()).join('\n');
  final summary = [
    if (filtered) '${scan.matched} of ${scan.scanned} entries matched ${_filterLabel(contains, matchSource)}',
    body,
  ].join('\n');

  final hidden = scan.matched - shown.length;
  return StructuredResponse(
    summary: summary,
    nextSteps: [
      if (shown.isEmpty) ..._emptyNextSteps(session, filtered: filtered && scan.scanned > 0),
      if (hidden > 0)
        '$hidden older matching entries not shown (limit or the ${_totalCapChars ~/ 1000}k char reply cap); narrow with contains/match or raise limit',
      if (shown.any((c) => c.cut > 0))
        'entries cut at $maxChars chars; pass maxChars:0 to read them whole (narrow with sinceSeq or contains first)',
    ],
    data: {
      'count': entries.length,
      'scanned': scan.scanned,
      'matched': scan.matched,
      'capacity': session.appLogs.capacity,
      'nextSequence': session.appLogs.nextSequence,
      // The rendered summary already carries the entries; only ship the
      // structured array when json is explicitly requested (matches `logs`).
      if (asJson) 'entries': shown.map((c) => c.toJson()).toList(),
    },
  );
}