Redesign the stats panel after the developer's feedback on v1 and add limit forecasts, daily week use and token-based estimates (D7, D8). - Panel: a fixed header with range and provider controls, limit tiles with pace chips, meters and forecasts, week use per day, token KPIs and charts, one burn-up chart per limit window with past instances, and a sortable breakdown whose "Other" row expands. One tooltip component for every chart, tile and table. - Core (D7): average and recent-rate forecasts, the reach date whenever use is over pace, days of a long window with even pace and daily budget, window instances derived from resets with a 15-minute tolerance, and estimates from price-weighted tokens where samples are missing. Forecasts start at 10 % of a window, or as soon as it is ahead, so a Codex week far ahead right after its reset warns too. - Settings, popups and the version (1.1.0) follow the new design. Closes #5 Closes #7
69 lines
3.5 KiB
C#
69 lines
3.5 KiB
C#
using System.Text.Json;
|
|
|
|
namespace FinalToken.Core;
|
|
|
|
public sealed record ProviderSettings
|
|
{
|
|
// Setters let source-generated JSON construct defaults before assigning present properties.
|
|
public bool Enabled { get; set; } = true;
|
|
public bool PopupsEnabled { get; set; } = true;
|
|
public bool PopupAhead { get; set; } = true;
|
|
public bool PopupBehind { get; set; } = true;
|
|
public string? CliPath { get; set; }
|
|
}
|
|
|
|
public sealed record Settings
|
|
{
|
|
// Preserve initializers when an older file lacks newly added properties (D5).
|
|
public TimeSpan RefreshInterval { get; set; } = TimeSpan.FromSeconds(60);
|
|
public TimeSpan ProbeInterval { get; set; } = TimeSpan.FromMinutes(5);
|
|
public double PaceThreshold { get; set; } = 10;
|
|
public bool PopupAhead { get; set; } = true;
|
|
public bool PopupBehind { get; set; } = true;
|
|
public InstallMode StreamDeckInstallMode { get; set; } = InstallMode.Ask;
|
|
public string? DismissedPluginVersion { get; set; }
|
|
public bool StartWithOs { get; set; }
|
|
public bool CloseToTray { get; set; } = true;
|
|
public ProviderSettings Claude { get; set; } = new();
|
|
public ProviderSettings Codex { get; set; } = new();
|
|
public ProviderSettings For(Provider provider) => provider == Provider.Claude ? Claude : Codex;
|
|
[System.Text.Json.Serialization.JsonIgnore]
|
|
public bool IsValid => RefreshInterval > TimeSpan.Zero && RefreshInterval <= TimeSpan.FromSeconds(60) &&
|
|
ProbeInterval >= TimeSpan.FromMinutes(1) && ProbeInterval <= TimeSpan.FromMinutes(60) &&
|
|
double.IsFinite(PaceThreshold) && PaceThreshold is > 0 and <= 100 && Claude is not null && Codex is not null &&
|
|
Enum.IsDefined(StreamDeckInstallMode);
|
|
}
|
|
|
|
public static class DataPaths
|
|
{
|
|
public static string DataDirectory => Environment.GetEnvironmentVariable("FINALTOKEN_DATA_DIR") is { Length: > 0 } path
|
|
? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FinalToken");
|
|
public static string ClaudeDirectory => Environment.GetEnvironmentVariable("CLAUDE_CONFIG_DIR") is { Length: > 0 } path
|
|
? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".claude");
|
|
public static string CodexDirectory => Environment.GetEnvironmentVariable("CODEX_HOME") is { Length: > 0 } path
|
|
? Path.GetFullPath(path) : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".codex");
|
|
}
|
|
|
|
public static class SettingsFile
|
|
{
|
|
public static Settings Load(string directory, DiagnosticLog diagnostics)
|
|
{
|
|
try
|
|
{
|
|
var value = JsonSerializer.Deserialize(SharedFile.ReadAllText(Path.Combine(directory, "settings.json")), CoreJsonContext.Default.Settings);
|
|
if (value is { IsValid: true }) return value;
|
|
}
|
|
catch (FileNotFoundException) { return new(); }
|
|
catch (DirectoryNotFoundException) { return new(); }
|
|
catch (Exception e) when (e is IOException or UnauthorizedAccessException or JsonException) { }
|
|
diagnostics.Add(null, "settings-defaults", "Settings invalid or unreadable; using defaults.");
|
|
return new();
|
|
}
|
|
public static Task SaveAsync(string directory, Settings settings, CancellationToken cancellationToken = default)
|
|
{
|
|
if (!settings.IsValid) throw new ArgumentException("Invalid settings.", nameof(settings));
|
|
return AtomicFile.WriteAsync(Path.Combine(directory, "settings.json"),
|
|
JsonSerializer.Serialize(settings, CoreJsonContext.Default.Settings), cancellationToken);
|
|
}
|
|
}
|