Files
FinalToken/src/FinalToken.Core/Settings.cs
T
schmidt.florian f8afd11679 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
2026-10-14 22:11:19 +02:00

67 lines
3.4 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 (Exception e) when (e is IOException or UnauthorizedAccessException or JsonException) { }
diagnostics.Add(null, "settings-defaults", "Settings missing or invalid; 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);
}
}