FinalToken v1: a NativeAOT tray app for Windows 11 and CachyOS that tracks Claude Code and Codex usage and limits through their CLIs only, with no provider sign-in and no tokens (R1-R7, D1-D11). - Core: incremental collectors for Claude transcripts and Codex rollouts, limit probes through `claude -p /usage` and `codex app-server`, a journaled history store, the refresh engine, pace and popup decisions, and the snapshot.json contract. - App (Avalonia): stats panel with limit bars, pace, projections, token totals, charts, limit history and breakdowns; tray, settings, non-activating pace popups, single-instance activation, start with the OS, and the Off/Ask/Automatic Stream Deck plugin install. - Stream Deck plugin (SDK v2, NativeAOT) for Elgato and OpenDeck. - finaltoken-cli, tools/publish.py, the documentation check and the agent review workflow. Closes #1
29 lines
1.5 KiB
C#
29 lines
1.5 KiB
C#
using System.Text.Json;
|
|
|
|
namespace FinalToken.Core.Collectors;
|
|
|
|
public static class TranscriptParser
|
|
{
|
|
public static UsageEvent? Parse(string line, DiagnosticLog diagnostics)
|
|
{
|
|
try
|
|
{
|
|
using var document = JsonDocument.Parse(line);
|
|
var root = document.RootElement;
|
|
if (root.Text("type") != "assistant") return null;
|
|
var message = root.Field("message");
|
|
if (message.Text("model") == "<synthetic>") return null;
|
|
if (message.Field("usage").ValueKind is JsonValueKind.Undefined or JsonValueKind.Null) return null;
|
|
var counts = JsonFields.Counts(message.Field("usage"), true);
|
|
if (root.Text("requestId") is not { Length: > 0 } && counts == new TokenCounts()) return null;
|
|
if (message.Text("id") is not { Length: > 0 } id || root.Text("requestId") is not { Length: > 0 } request ||
|
|
root.Field("timestamp").Timestamp() is not { } time) throw new FormatException();
|
|
// Length prefixes avoid collisions between ids that contain separators (D3).
|
|
return new(Provider.Claude, time, message.Text("model"), root.Text("cwd"),
|
|
counts, $"claude:{id.Length}:{id}{request.Length}:{request}");
|
|
}
|
|
catch (Exception e) when (e is JsonException or FormatException or OverflowException)
|
|
{ diagnostics.Add(Provider.Claude, "transcript-parse", "Malformed assistant usage entry skipped."); return null; }
|
|
}
|
|
}
|