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 type = args['type']! as String;
  final titleRaw = args['title']! as String;
  final fields = IssueFields.fromArgs(args);
  final includeContext = argBool(args, 'includeContext') ?? false;
  final dryRun = argBool(args, 'dryRun') ?? false;

  if (!const {'bug', 'ux', 'feature'}.contains(type)) {
    return StructuredResponse.error(
      summary: 'unknown type: $type',
      errorKind: GlintErrorKind.invalidArgument,
      nextSteps: const ['use one of: bug, ux, feature'],
    );
  }

  final identities = sessionIdentities(session);
  final redactor = IdentityRedactor(appNames: identities.apps, deviceNames: identities.devices);
  final redact = redactor.forSharing;

  final title = redact(titleRaw);
  final sections = renderIssueSections(fields, session.actionLog);
  final fullBody = redact(
    await _composeBody(
      session: session,
      fields: fields,
      sections: sections.text,
      includeContext: includeContext,
    ),
  );
  final seqWarnings = [
    if (sections.missingSeqs.isNotEmpty)
      'failureSeqs ${sections.missingSeqs.join(', ')} are not in the action log (older than its last '
          '${session.actionLog.capacity} calls, or cleared); the issue says so',
    ...redactor.warnings,
  ];
  final labels = labelsForType(type);
  final redactedNote = redactor.counts.describe();
  final redactedData = redactor.counts.toJson();

  if (dryRun) {
    // The summary already carries the composed body readably; don't ship a
    // second raw copy in `pasteBody`. deepLink stays (the fileable form).
    final deepLink = composeIssueDeepLink(
      title: title,
      body: fullBody,
      labels: labels,
    );
    final nudges = issueNudges(type, fields, session.actionLog);
    return StructuredResponse(
      summary:
          'dry-run: composed body but did not file${redactedNote == null ? '' : ' (redacted $redactedNote)'}:\n\n# '
          '$title\n\n$fullBody'
          '${nudges.isEmpty ? '' : '\n\nstill empty (optional):\n${nudges.map((n) => '- $n').join('\n')}'}',
      data: {
        'dryRun': true,
        'type': type,
        'title': title,
        'labels': labels,
        'deepLink': deepLink,
        'missingSections': nudges,
        'redacted': redactedData,
      },
      warnings: seqWarnings,
      nextSteps: [
        'only names glint knows from this session are redacted; check the draft for any app, company, host or '
            'device name before approving',
        if (nudges.isNotEmpty) 'fill what you can from the list above and dry-run again; filing as is also works',
        'show the user the issue above; after they approve, call report_issue again without dryRun',
      ],
    );
  }

  final filed = await fileWithGh(
    run: run,
    repo: issueRepo,
    title: title,
    body: fullBody,
    labels: labels,
  );
  if (filed.url != null) {
    final dropped = filed.droppedLabels;
    return StructuredResponse(
      summary: 'filed: ${filed.url}'
          '${dropped.isEmpty ? '' : ' (without ${dropped.join(", ")}: not labels of this repo)'}'
          '${redactedNote == null ? '' : ' · redacted $redactedNote'}',
      data: {
        'filed': true,
        'method': 'gh-cli',
        'url': filed.url,
        'type': type,
        'title': title,
        'labels': filed.applied,
        if (dropped.isNotEmpty) 'droppedLabels': dropped,
        'redacted': redactedData,
      },
      warnings: seqWarnings,
      nextSteps: [
        'mention the URL to the user: ${filed.url}',
        if (dropped.isNotEmpty)
          'maintainer: `gh label create ${dropped.first} --repo $issueRepo` '
              'makes the skipped label(s) stick next time',
      ],
    );
  }

  final saved = saveFullIssueBody(fullBody, prefix: 'glint');
  final capped = capDeepLinkBody(fullBody, savedTo: saved);
  final deepLink = composeIssueDeepLink(
    title: title,
    body: capped,
    labels: filed.applied,
  );
  return StructuredResponse(
    summary: 'gh CLI ${filed.reason}; open this pre-filled URL '
        'instead${redactedNote == null ? '' : ' (redacted $redactedNote)'}:\n\n$deepLink',
    data: {
      'filed': false,
      'method': 'paste-ready',
      'type': type,
      'title': title,
      'labels': filed.applied,
      'pasteBody': fullBody,
      if (saved != null) 'fullBodyPath': saved,
      'deepLink': deepLink,
      'repo': issueRepo,
      'redacted': redactedData,
    },
    warnings: [
      filed.reason,
      ...seqWarnings,
      if (capped.length < fullBody.length)
        'the URL carries the first $kDeepLinkBodyMax chars of the body; '
            'the full report is at $saved',
    ],
    nextSteps: const [
      'open the deep-link URL — title, body, and labels are pre-filled',
      'install `gh` (https://cli.github.com/) + `gh auth login` for '
          'one-call filing next time',
    ],
  );
}