| 1 | #nullable enable |
| 2 | using CascadeIDE.Features.WorkspaceNavigation.Application; |
| 3 | using CascadeIDE.Models.AgentChat; |
| 4 | using CascadeIDE.Services; |
| 5 | |
| 6 | namespace CascadeIDE.Features.Chat; |
| 7 | |
| 8 | /// <summary>0061 applies one-liner для T2 context card (ADR 0174 S3).</summary> |
| 9 | internal static class SedmAppliesResolver |
| 10 | { |
| 11 | public static IReadOnlyList<SedmAppliesEntryPayload> Resolve( |
| 12 | string? workspaceRoot, |
| 13 | string anchorPath, |
| 14 | int maxEntries = 3) |
| 15 | { |
| 16 | if (string.IsNullOrWhiteSpace(workspaceRoot) || string.IsNullOrWhiteSpace(anchorPath)) |
| 17 | return []; |
| 18 | |
| 19 | var root = workspaceRoot.Trim(); |
| 20 | var rel = anchorPath.Replace('\\', '/').Trim(); |
| 21 | var abs = WorkspaceAdrMapResolver.TryResolveAbsoluteDocPath(root, rel) |
| 22 | ?? Path.Combine(root, rel.Replace('/', Path.DirectorySeparatorChar)); |
| 23 | |
| 24 | var correspondence = WorkspaceCorrespondenceResolver.Resolve(root, abs); |
| 25 | if (correspondence.AdrDocPaths.Length == 0) |
| 26 | return []; |
| 27 | |
| 28 | var applies = new List<SedmAppliesEntryPayload>(Math.Min(maxEntries, correspondence.AdrDocPaths.Length)); |
| 29 | foreach (var docPath in correspondence.AdrDocPaths.Take(maxEntries)) |
| 30 | { |
| 31 | var adrRef = WorkspaceAdrMapResolver.GuessAdrPreviewTitle(docPath); |
| 32 | if (string.IsNullOrWhiteSpace(adrRef)) |
| 33 | continue; |
| 34 | |
| 35 | applies.Add(new SedmAppliesEntryPayload( |
| 36 | "adr", |
| 37 | adrRef, |
| 38 | BuildOneLiner(adrRef, correspondence.FeatureLine), |
| 39 | "path_map")); |
| 40 | } |
| 41 | |
| 42 | return applies; |
| 43 | } |
| 44 | |
| 45 | public static string? BuildPathHint( |
| 46 | IReadOnlyList<SedmAppliesEntryPayload>? applies, |
| 47 | string anchorPath) |
| 48 | { |
| 49 | if (applies is not { Count: > 0 }) |
| 50 | return null; |
| 51 | |
| 52 | var chain = string.Join(" → ", applies.Select(static a => $"ADR {a.Ref}")); |
| 53 | return $"{chain} → {anchorPath.Replace('\\', '/')}"; |
| 54 | } |
| 55 | |
| 56 | private static string BuildOneLiner(string adrRef, string featureLine) |
| 57 | { |
| 58 | if (!string.IsNullOrWhiteSpace(featureLine)) |
| 59 | return Truncate(featureLine.Trim(), 56); |
| 60 | return $"ADR {adrRef}"; |
| 61 | } |
| 62 | |
| 63 | private static string Truncate(string text, int max) => |
| 64 | text.Length <= max ? text : text[..max] + "…"; |
| 65 | } |
| 66 | |