|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | + |
| 10 | +namespace SemanticDeveloper.Services; |
| 11 | + |
| 12 | +public static class CodexSessionService |
| 13 | +{ |
| 14 | + public sealed class SessionInfo |
| 15 | + { |
| 16 | + public required string Id { get; init; } |
| 17 | + public required string WorkspacePath { get; init; } |
| 18 | + public required DateTimeOffset StartedAt { get; init; } |
| 19 | + public string FirstUserMessage { get; init; } = string.Empty; |
| 20 | + public string RolloutPath { get; init; } = string.Empty; |
| 21 | + } |
| 22 | + |
| 23 | + public static List<SessionInfo> GetSessionsForWorkspace(string workspacePath, int maxResults = 40) |
| 24 | + { |
| 25 | + var results = new List<SessionInfo>(); |
| 26 | + if (string.IsNullOrWhiteSpace(workspacePath)) |
| 27 | + return results; |
| 28 | + |
| 29 | + var normalizedWorkspace = NormalizePath(workspacePath); |
| 30 | + if (string.IsNullOrEmpty(normalizedWorkspace)) |
| 31 | + return results; |
| 32 | + |
| 33 | + var root = GetSessionsRoot(); |
| 34 | + if (string.IsNullOrEmpty(root) || !Directory.Exists(root)) |
| 35 | + return results; |
| 36 | + |
| 37 | + foreach (var yearDir in EnumerateDirectoriesDescending(root)) |
| 38 | + { |
| 39 | + foreach (var monthDir in EnumerateDirectoriesDescending(yearDir)) |
| 40 | + { |
| 41 | + foreach (var dayDir in EnumerateDirectoriesDescending(monthDir)) |
| 42 | + { |
| 43 | + foreach (var file in EnumerateFilesDescending(dayDir, "*.jsonl")) |
| 44 | + { |
| 45 | + if (!TryReadSessionMetadata(file, out var info)) |
| 46 | + continue; |
| 47 | + |
| 48 | + if (!PathsEqual(info.WorkspacePath, normalizedWorkspace)) |
| 49 | + continue; |
| 50 | + |
| 51 | + results.Add(info); |
| 52 | + if (results.Count >= maxResults) |
| 53 | + return OrderByNewest(results); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return OrderByNewest(results); |
| 60 | + } |
| 61 | + |
| 62 | + private static bool TryReadSessionMetadata(string filePath, out SessionInfo info) |
| 63 | + { |
| 64 | + info = default!; |
| 65 | + try |
| 66 | + { |
| 67 | + using var reader = File.OpenText(filePath); |
| 68 | + string? line; |
| 69 | + string? id = null; |
| 70 | + string? cwd = null; |
| 71 | + DateTimeOffset? timestamp = null; |
| 72 | + string? preferredMessage = null; |
| 73 | + string? fallbackMessage = null; |
| 74 | + |
| 75 | + while ((line = reader.ReadLine()) != null) |
| 76 | + { |
| 77 | + if (string.IsNullOrWhiteSpace(line)) |
| 78 | + continue; |
| 79 | + |
| 80 | + JObject obj; |
| 81 | + try |
| 82 | + { |
| 83 | + obj = JObject.Parse(line); |
| 84 | + } |
| 85 | + catch |
| 86 | + { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + var type = obj["type"]?.ToString(); |
| 91 | + if (type == "session_meta" && obj["payload"] is JObject payload) |
| 92 | + { |
| 93 | + id ??= payload["id"]?.ToString(); |
| 94 | + var ts = payload["timestamp"]?.ToString(); |
| 95 | + if (!timestamp.HasValue && !string.IsNullOrWhiteSpace(ts) && DateTimeOffset.TryParse(ts, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var parsedTs)) |
| 96 | + timestamp = parsedTs; |
| 97 | + var workspace = payload["cwd"]?.ToString(); |
| 98 | + if (!string.IsNullOrWhiteSpace(workspace)) |
| 99 | + cwd ??= NormalizePath(workspace); |
| 100 | + } |
| 101 | + else if (type == "response_item" && obj["payload"] is JObject resp) |
| 102 | + { |
| 103 | + var payloadType = resp["type"]?.ToString(); |
| 104 | + var role = resp["role"]?.ToString(); |
| 105 | + if (payloadType == "message" && string.Equals(role, "user", StringComparison.OrdinalIgnoreCase)) |
| 106 | + { |
| 107 | + var raw = ExtractMessageText(resp["content"] as JArray); |
| 108 | + if (string.IsNullOrWhiteSpace(raw)) |
| 109 | + continue; |
| 110 | + |
| 111 | + var sanitized = SanitizeUserMessage(raw); |
| 112 | + if (string.IsNullOrWhiteSpace(sanitized)) |
| 113 | + continue; |
| 114 | + |
| 115 | + if (preferredMessage is null && !IsBoilerplateUserMessage(raw)) |
| 116 | + preferredMessage = sanitized; |
| 117 | + fallbackMessage ??= sanitized; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (id is not null && cwd is not null && timestamp.HasValue && (preferredMessage is not null || fallbackMessage is not null)) |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(cwd) || !timestamp.HasValue) |
| 126 | + return false; |
| 127 | + |
| 128 | + var message = preferredMessage ?? fallbackMessage ?? string.Empty; |
| 129 | + |
| 130 | + info = new SessionInfo |
| 131 | + { |
| 132 | + Id = id, |
| 133 | + WorkspacePath = cwd, |
| 134 | + StartedAt = timestamp.Value, |
| 135 | + FirstUserMessage = message, |
| 136 | + RolloutPath = filePath |
| 137 | + }; |
| 138 | + return true; |
| 139 | + } |
| 140 | + catch |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private static IEnumerable<string> EnumerateDirectoriesDescending(string root) |
| 147 | + { |
| 148 | + try |
| 149 | + { |
| 150 | + return Directory.EnumerateDirectories(root) |
| 151 | + .OrderByDescending(Path.GetFileName, StringComparer.OrdinalIgnoreCase) |
| 152 | + .ToList(); |
| 153 | + } |
| 154 | + catch |
| 155 | + { |
| 156 | + return Array.Empty<string>(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static IEnumerable<string> EnumerateFilesDescending(string root, string searchPattern) |
| 161 | + { |
| 162 | + try |
| 163 | + { |
| 164 | + return Directory.EnumerateFiles(root, searchPattern, SearchOption.TopDirectoryOnly) |
| 165 | + .OrderByDescending(Path.GetFileName, StringComparer.OrdinalIgnoreCase) |
| 166 | + .ToList(); |
| 167 | + } |
| 168 | + catch |
| 169 | + { |
| 170 | + return Array.Empty<string>(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static List<SessionInfo> OrderByNewest(List<SessionInfo> entries) |
| 175 | + => entries.OrderByDescending(e => e.StartedAt).ToList(); |
| 176 | + |
| 177 | + private static string GetSessionsRoot() |
| 178 | + { |
| 179 | + try |
| 180 | + { |
| 181 | + if (OperatingSystem.IsWindows() && WslInterop.IsEnabled) |
| 182 | + { |
| 183 | + var converted = WslInterop.TryConvertToWindowsPath("~/.codex/sessions"); |
| 184 | + if (!string.IsNullOrWhiteSpace(converted)) |
| 185 | + return converted; |
| 186 | + } |
| 187 | + |
| 188 | + var home = Environment.GetEnvironmentVariable("CODEX_HOME"); |
| 189 | + string baseDir = !string.IsNullOrWhiteSpace(home) |
| 190 | + ? home! |
| 191 | + : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".codex"); |
| 192 | + return Path.Combine(baseDir, "sessions"); |
| 193 | + } |
| 194 | + catch |
| 195 | + { |
| 196 | + return Path.Combine(Directory.GetCurrentDirectory(), "sessions"); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private static string NormalizePath(string path) |
| 201 | + { |
| 202 | + if (string.IsNullOrWhiteSpace(path)) |
| 203 | + return string.Empty; |
| 204 | + try |
| 205 | + { |
| 206 | + var full = Path.GetFullPath(path); |
| 207 | + return full.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
| 208 | + } |
| 209 | + catch |
| 210 | + { |
| 211 | + return path.Trim().TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private static bool PathsEqual(string a, string b) |
| 216 | + { |
| 217 | + if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b)) |
| 218 | + return false; |
| 219 | + var comparison = OperatingSystem.IsWindows() |
| 220 | + ? StringComparison.OrdinalIgnoreCase |
| 221 | + : StringComparison.Ordinal; |
| 222 | + return string.Equals(a, b, comparison); |
| 223 | + } |
| 224 | + |
| 225 | + private static string ExtractMessageText(JArray? contentArray) |
| 226 | + { |
| 227 | + if (contentArray is null) |
| 228 | + return string.Empty; |
| 229 | + var builder = new StringBuilder(); |
| 230 | + foreach (var item in contentArray.OfType<JObject>()) |
| 231 | + { |
| 232 | + var text = item["text"]?.ToString(); |
| 233 | + if (string.IsNullOrWhiteSpace(text)) |
| 234 | + continue; |
| 235 | + if (builder.Length > 0) |
| 236 | + builder.Append('\n'); |
| 237 | + builder.Append(text); |
| 238 | + } |
| 239 | + return builder.ToString(); |
| 240 | + } |
| 241 | + |
| 242 | + private static readonly Regex TagRegex = new("<[^>]+>", RegexOptions.Compiled); |
| 243 | + private static readonly Regex WhitespaceRegex = new("\\s+", RegexOptions.Compiled); |
| 244 | + |
| 245 | + private static string SanitizeUserMessage(string text) |
| 246 | + { |
| 247 | + if (string.IsNullOrWhiteSpace(text)) |
| 248 | + return string.Empty; |
| 249 | + |
| 250 | + var normalized = text.Replace("\r\n", "\n"); |
| 251 | + normalized = TagRegex.Replace(normalized, " "); |
| 252 | + normalized = normalized.Replace('`', ' '); |
| 253 | + normalized = WhitespaceRegex.Replace(normalized, " ").Trim(); |
| 254 | + return normalized; |
| 255 | + } |
| 256 | + |
| 257 | + private static bool IsBoilerplateUserMessage(string raw) |
| 258 | + { |
| 259 | + if (string.IsNullOrWhiteSpace(raw)) |
| 260 | + return true; |
| 261 | + var lower = raw.ToLowerInvariant(); |
| 262 | + if (lower.Contains("<user_instructions") || lower.Contains("<environment_context")) |
| 263 | + return true; |
| 264 | + if (lower.Contains("## tui style") || lower.Contains("## tui code conventions")) |
| 265 | + return true; |
| 266 | + if (lower.Contains("run `just fmt`") || lower.Contains("cargo test -p")) |
| 267 | + return true; |
| 268 | + return false; |
| 269 | + } |
| 270 | +} |
0 commit comments