-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClaudeCodeInfo.cs
More file actions
47 lines (40 loc) · 1.43 KB
/
Copy pathClaudeCodeInfo.cs
File metadata and controls
47 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace ClaudeUsageMonitor;
/// <summary>
/// Reads the installed Claude Code version to build an accurate User-Agent header.
/// Result is cached — claude --version is only executed once per process lifetime.
/// </summary>
internal static partial class ClaudeCodeInfo
{
private static string? _cachedVersion;
internal static string UserAgent => $"claude-code/{Version}";
internal static string Version => _cachedVersion ??= ReadVersion();
private static string ReadVersion()
{
try
{
var psi = new ProcessStartInfo("claude", "--version")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
using var proc = Process.Start(psi)!;
var readTask = proc.StandardOutput.ReadToEndAsync();
if (!proc.WaitForExit(3000))
{
try { proc.Kill(); } catch { }
return "2.1.69";
}
var output = readTask.Result.Trim();
var match = VersionRegex().Match(output);
if (match.Success) return match.Value;
}
catch { }
// Fallback if claude binary is not found or version cannot be parsed
return "2.1.69";
}
[GeneratedRegex(@"\d+\.\d+\.\d+")]
private static partial Regex VersionRegex();
}