core: add the solution, Core engine, CLI and tests

Core implements the CLI-only collectors for Claude and Codex (D2 to D4),
the history store (D5), the refresh engine (D6), pace and popup
decisions (D7, D8), the snapshot contract (D9) and the Stream Deck
installer contract (D11). finaltoken status runs one refresh and prints
the snapshot. The app and plugin projects are placeholders so the
solution file is complete before they are built in parallel.

Refs #1
This commit is contained in:
Florian Schmidt
2026-10-14 19:09:54 +02:00
parent db890c5f68
commit 14223b05df
60 changed files with 2287 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
using System.Text.Json;
namespace FinalToken.Core;
public sealed record ProviderSettings
{
public bool Enabled { get; init; } = true;
public bool PopupsEnabled { get; init; } = true;
public bool PopupAhead { get; init; } = true;
public bool PopupBehind { get; init; } = true;
public string? CliPath { get; init; }
}
public sealed record Settings
{
public TimeSpan RefreshInterval { get; init; } = TimeSpan.FromSeconds(60);
public TimeSpan ProbeInterval { get; init; } = TimeSpan.FromMinutes(5);
public double PaceThreshold { get; init; } = 10;
public bool PopupAhead { get; init; } = true;
public bool PopupBehind { get; init; } = true;
public InstallMode StreamDeckInstallMode { get; init; } = InstallMode.Ask;
public string? DismissedPluginVersion { get; init; }
public bool StartWithOs { get; init; }
public bool CloseToTray { get; init; } = true;
public ProviderSettings Claude { get; init; } = new();
public ProviderSettings Codex { get; init; } = 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(File.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);
}
}