feat: add FinalToken v1 for Claude and Codex usage (#2)
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
This commit was merged in pull request #2.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
namespace FinalToken.Core;
|
||||
|
||||
public static class Pace
|
||||
{
|
||||
// D7. A reset in the past clamps elapsed to one; it never has a future projection.
|
||||
public static PaceResult Calculate(LimitWindow window, DateTimeOffset now, double threshold = 10)
|
||||
{
|
||||
if (window.Length is not { Ticks: > 0 } length || window.ResetsAt is not { } reset ||
|
||||
!double.IsFinite(window.UsedPercent) || window.UsedPercent is < 0 or > 100 ||
|
||||
!double.IsFinite(threshold) || threshold <= 0)
|
||||
return new(PaceState.Unknown);
|
||||
var elapsed = Math.Clamp(1 - (reset - now).TotalSeconds / length.TotalSeconds, 0, 1);
|
||||
var delta = window.UsedPercent - 100 * elapsed;
|
||||
var state = delta >= threshold ? PaceState.Ahead :
|
||||
delta <= -threshold && elapsed >= 0.5 ? PaceState.Behind : PaceState.OnTrack;
|
||||
DateTimeOffset? projection = null;
|
||||
if (state == PaceState.Ahead && window.UsedPercent > 0 && elapsed > 0)
|
||||
{
|
||||
var seconds = (100 - window.UsedPercent) * elapsed * length.TotalSeconds / window.UsedPercent;
|
||||
if (seconds >= 0 && seconds < (reset - now).TotalSeconds) projection = now.AddSeconds(seconds);
|
||||
}
|
||||
return new(state, elapsed, delta, projection);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>D8: retains an alert until it clears its threshold by two points.
|
||||
/// Observe is deterministic and has no side effects beyond its own state.</summary>
|
||||
public sealed class PopupDecider
|
||||
{
|
||||
private readonly Dictionary<(Provider, string), PaceState> states = [];
|
||||
public PopupEvent? Observe(LimitWindow window, PaceResult pace, Settings settings)
|
||||
{
|
||||
var key = (window.Provider, window.Id);
|
||||
var previous = states.GetValueOrDefault(key, PaceState.Unknown);
|
||||
var next = pace.State;
|
||||
if (pace.Delta is { } delta && pace.State != PaceState.Unknown)
|
||||
{
|
||||
if (previous == PaceState.Ahead && delta > settings.PaceThreshold - 2) next = PaceState.Ahead;
|
||||
if (previous == PaceState.Behind && delta < -settings.PaceThreshold + 2 && pace.ElapsedFraction >= 0.5)
|
||||
next = PaceState.Behind;
|
||||
}
|
||||
states[key] = next;
|
||||
if (previous == next || next is not (PaceState.Ahead or PaceState.Behind) ||
|
||||
!settings.For(window.Provider).Enabled || !settings.For(window.Provider).PopupsEnabled ||
|
||||
(next == PaceState.Ahead ? !settings.PopupAhead || !settings.For(window.Provider).PopupAhead :
|
||||
!settings.PopupBehind || !settings.For(window.Provider).PopupBehind)) return null;
|
||||
return new(window.Provider, window.Id, window.Label, next, pace.Delta!.Value, pace.Projection);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user