Files
clide/lib/builtin/claude/src/claude_stats.dart
T
jpmschweitzerandClaude Opus 4.8 6d0ebab721 chore: adopt Dart 3.9 toolchain — honest floor + tall-style reformat (T-353)
Raise the declared minimums in pubspec.yaml to what our deps already
require: Flutter >=3.35.0 / Dart >=3.9.0 (was 3.19.0 / 3.5.0). alchemist
0.12 needs Flutter 3.32; Dart 3.9 first ships in Flutter 3.35, so 3.35 is
the binding floor. Pin the exact build toolchain in .fvmrc (Flutter
3.44.1).

Moving to the Dart 3.9 language level switches `dart format` to the new
"tall" style and enables two new lints. This commit is the resulting
mechanical churn, isolated from any behaviour change:
  - whole-tree `dart format` reformat (tall style)
  - `dart fix` for unnecessary_underscores + use_null_aware_elements

No runtime behaviour change; `make test` green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 12:11:53 +02:00

66 lines
2.1 KiB
Dart

/// Reads Claude Code's `~/.claude/stats-cache.json` — per-day activity
/// (message / session / tool-call counts) — for the meta sidebar (T-141).
/// Flutter-free so it unit-tests under `dart test`. This is activity, not the
/// account budget; the budget isn't programmatically exposed (see
/// project memory / GitHub anthropics/claude-code#44328).
library;
import 'dart:convert';
class DailyActivity {
const DailyActivity({required this.date, required this.messageCount, required this.sessionCount, required this.toolCallCount});
final String date; // "YYYY-MM-DD" (sorts chronologically as a string)
final int messageCount;
final int sessionCount;
final int toolCallCount;
}
class ClaudeStats {
const ClaudeStats({this.lastComputed, this.daily = const []});
final String? lastComputed;
final List<DailyActivity> daily;
/// The most recent day on record, or null if there's no activity.
DailyActivity? get latest {
if (daily.isEmpty) return null;
return daily.reduce((a, b) => a.date.compareTo(b.date) >= 0 ? a : b);
}
int get activeDays => daily.length;
int get lifetimeMessages => daily.fold(0, (a, d) => a + d.messageCount);
int get lifetimeSessions => daily.fold(0, (a, d) => a + d.sessionCount);
int get lifetimeToolCalls => daily.fold(0, (a, d) => a + d.toolCallCount);
}
/// Parse the stats-cache JSON. Returns empty stats on any malformed input —
/// the sidebar degrades to "no activity" rather than throwing.
ClaudeStats parseClaudeStats(String jsonStr) {
Object? j;
try {
j = jsonDecode(jsonStr);
} catch (_) {
return const ClaudeStats();
}
if (j is! Map) return const ClaudeStats();
final daily = <DailyActivity>[];
final da = j['dailyActivity'];
if (da is List) {
for (final e in da) {
if (e is! Map) continue;
daily.add(
DailyActivity(
date: '${e['date']}',
messageCount: _int(e['messageCount']),
sessionCount: _int(e['sessionCount']),
toolCallCount: _int(e['toolCallCount']),
),
);
}
}
return ClaudeStats(lastComputed: j['lastComputedDate'] as String?, daily: daily);
}
int _int(Object? v) => v is num ? v.toInt() : 0;