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,130 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
using FinalToken.Core;
|
||||
|
||||
namespace FinalToken.App;
|
||||
|
||||
public static class Ui
|
||||
{
|
||||
public static TextBlock Text(string text, double size = 13) => new() { Text = text, FontSize = size, TextWrapping = TextWrapping.Wrap };
|
||||
public static StackPanel Stack(params Control[] children)
|
||||
{
|
||||
var stack = new StackPanel { Spacing = 10 };
|
||||
foreach (var child in children) stack.Children.Add(child);
|
||||
return stack;
|
||||
}
|
||||
public static Border Card(Control content) => new() { Child = content, Padding = new Thickness(18), CornerRadius = new CornerRadius(9), BorderThickness = new Thickness(1), BorderBrush = Brush.Parse("#558795A5") };
|
||||
public static Button Button(string text, Action action)
|
||||
{
|
||||
var button = new Button { Content = text };
|
||||
button.Click += (_, _) => action();
|
||||
return button;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PanelWindow : Window
|
||||
{
|
||||
private readonly PanelViewModel model;
|
||||
private readonly StackPanel body = new() { Spacing = 16, Margin = new Thickness(24) };
|
||||
public PanelWindow(PanelViewModel model, Action refresh, Action settings, Action quit, Action reload)
|
||||
{
|
||||
this.model = model;
|
||||
Title = $"FinalToken {typeof(Program).Assembly.GetName().Version?.ToString(3)}"; Width = 1160; Height = 920; MinWidth = 720; MinHeight = 500;
|
||||
var header = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 12 };
|
||||
header.Children.Add(Ui.Text("FinalToken", 28));
|
||||
header.Children.Add(Ui.Button("Refresh now", refresh));
|
||||
header.Children.Add(Ui.Button("Settings", settings));
|
||||
header.Children.Add(Ui.Button("Quit", quit));
|
||||
var range = new ComboBox { ItemsSource = new[] { "24 hours", "7 days", "30 days", "90 days", "All history" }, SelectedIndex = (int)model.Range, MinWidth = 150 };
|
||||
range.SelectionChanged += (_, _) => { model.Range = (HistoryRange)range.SelectedIndex; reload(); };
|
||||
var providers = new ComboBox { ItemsSource = new[] { "All providers", "Claude", "Codex" }, SelectedIndex = 0, MinWidth = 150 };
|
||||
providers.SelectionChanged += (_, _) => { model.ProviderFilter = providers.SelectedIndex == 0 ? null : (Provider)(providers.SelectedIndex - 1); reload(); };
|
||||
var filters = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 12 };
|
||||
filters.Children.Add(range); filters.Children.Add(providers);
|
||||
var dock = new DockPanel { Margin = new Thickness(0, 18, 0, 0) };
|
||||
var top = Ui.Stack(header, filters); top.Margin = new Thickness(24, 0, 24, 0);
|
||||
DockPanel.SetDock(top, Dock.Top); dock.Children.Add(top);
|
||||
dock.Children.Add(new ScrollViewer { Content = body }); Content = dock;
|
||||
model.PropertyChanged += (_, _) => RenderModel();
|
||||
RenderModel();
|
||||
}
|
||||
public void RenderModel()
|
||||
{
|
||||
body.Children.Clear();
|
||||
body.Children.Add(Ui.Text(model.Message));
|
||||
if (model.Snapshot is { } snapshot)
|
||||
{
|
||||
foreach (var provider in snapshot.Providers.Where(p => model.ProviderFilter is null || p.Provider == model.ProviderFilter))
|
||||
{
|
||||
var card = Ui.Stack(Ui.Text(provider.Provider.ToString(), 22), Ui.Text($"{provider.Status} {provider.Reason}"));
|
||||
foreach (var window in provider.Windows)
|
||||
{
|
||||
var w = window.Window; var pace = window.Pace;
|
||||
var title = Ui.Text($"{w.Label} {w.UsedPercent:0.##}% {Display.State(pace.State)} {(pace.Delta is { } d ? $"{d:+0.0;-0.0;0} points" : "")}", 15);
|
||||
title.Foreground = PaceBar.Colour(pace.State);
|
||||
card.Children.Add(title);
|
||||
card.Children.Add(new PaceBar(window) { Height = 14 });
|
||||
card.Children.Add(Ui.Text($"Reset: {(w.ResetsAt is { } reset ? Display.Duration(reset - DateTimeOffset.UtcNow) + " · " + reset.LocalDateTime.ToString("g") : "unknown")} Sample age: {Display.Duration(DateTimeOffset.UtcNow - w.SampleTime)}" +
|
||||
(pace.Projection is { } projection ? $" Reaches 100%: {Display.Projection(projection)}" : "")));
|
||||
}
|
||||
card.Children.Add(Ui.Text("Tokens · provider totals", 15));
|
||||
card.Children.Add(Ui.Text("Today " + Display.Tokens(provider.Today)));
|
||||
card.Children.Add(Ui.Text("Session " + Display.Tokens(provider.Session)));
|
||||
card.Children.Add(Ui.Text("Week " + Display.Tokens(provider.Week)));
|
||||
card.Children.Add(Ui.Text("Cache read is included in Codex input; reasoning is included in output.", 11));
|
||||
body.Children.Add(Ui.Card(card));
|
||||
}
|
||||
}
|
||||
var h = model.History;
|
||||
body.Children.Add(Ui.Card(Ui.Stack(Ui.Text("Tokens per hour", 19), Legend(h.Hourly), new UsageChart { Values = h.Hourly })));
|
||||
body.Children.Add(Ui.Card(Ui.Stack(Ui.Text("Tokens per day", 19), Legend(h.Daily), new UsageChart { Values = h.Daily, Interval = TimeSpan.FromDays(1) })));
|
||||
foreach (var limit in h.Limits)
|
||||
body.Children.Add(Ui.Card(Ui.Stack(Ui.Text(limit.Label, 19), Ui.Text("Blue: used · dashed green: expected pace · gaps: window reset", 11), new UsageChart { Values = limit.Values, Percent = true })));
|
||||
body.Children.Add(Breakdown("By model", h.Models));
|
||||
body.Children.Add(Breakdown("By project", h.Projects));
|
||||
var diagnostics = Ui.Stack(Ui.Text("Recent diagnostics", 19));
|
||||
foreach (var d in model.Diagnostics) diagnostics.Children.Add(Ui.Text($"{d.Time.LocalDateTime:g} · {d.Provider} · {d.Code} · {d.Summary}"));
|
||||
if (model.Diagnostics.Count == 0) diagnostics.Children.Add(Ui.Text("No recent problems."));
|
||||
body.Children.Add(Ui.Card(diagnostics));
|
||||
}
|
||||
private static Control Legend(IReadOnlyList<ChartValue> values)
|
||||
{
|
||||
var legend = new WrapPanel { Orientation = Orientation.Horizontal };
|
||||
var index = 0;
|
||||
foreach (var series in values.Select(v => v.Series).Distinct())
|
||||
{
|
||||
var label = Ui.Text("● " + series, 12); label.Foreground = UsageChart.SeriesColour(index++);
|
||||
label.Margin = new Thickness(0, 0, 14, 0); legend.Children.Add(label);
|
||||
}
|
||||
return legend;
|
||||
}
|
||||
private static Border Breakdown(string title, IReadOnlyList<BreakdownRow> rows)
|
||||
{
|
||||
var table = Ui.Stack(Ui.Text(title, 19));
|
||||
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("3*,1*,1*,1*,1*,1*,1*,1*"), MinWidth = 900 };
|
||||
var headers = new[] { "Name", "Total", "Share", "Input", "Cache read", "Cache write", "Output", "Reasoning" };
|
||||
AddRow(headers, 0);
|
||||
var index = 1;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var c = row.Counts;
|
||||
AddRow([row.Name, c.Total.ToString("N0"), row.Share.ToString("P1"), c.Input.ToString("N0"), c.CacheRead.ToString("N0"),
|
||||
c.CacheWrite.ToString("N0"), c.Output.ToString("N0"), c.ReasoningOutput.ToString("N0")], index++);
|
||||
}
|
||||
table.Children.Add(new ScrollViewer { Content = grid, HorizontalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Auto,
|
||||
VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Disabled });
|
||||
if (rows.Count == 0) table.Children.Add(Ui.Text("No tokens in this range."));
|
||||
return Ui.Card(table);
|
||||
void AddRow(string[] values, int row)
|
||||
{
|
||||
grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
for (var column = 0; column < values.Length; column++)
|
||||
{
|
||||
var text = Ui.Text(values[column], 11); text.Margin = new Thickness(0, 4, 8, 4);
|
||||
Grid.SetRow(text, row); Grid.SetColumn(text, column); grid.Children.Add(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user