| 1 | using AgentForge.Contracts; |
| 2 | using AgentForge.Data; |
| 3 | using AgentForge.Data.Entities; |
| 4 | using AgentForge.Models; |
| 5 | |
| 6 | namespace AgentForge.Plugin.View.Components.Iop; |
| 7 | |
| 8 | internal static class ForgeViewIopConversationTab |
| 9 | { |
| 10 | internal static string Render( |
| 11 | string repoName, |
| 12 | ForgeMergeRequestEntity mergeRequest, |
| 13 | ForgeRepoEntity repo, |
| 14 | IReadOnlyList<(string Author, string Text, DateTimeOffset CreatedAt)> comments, |
| 15 | ForgeCiRunEntity? ciRun, |
| 16 | bool showActions) |
| 17 | { |
| 18 | return ForgeHtml.Fragment( |
| 19 | ForgeHtml.P("iop-meta", ForgeViewIopCiBadge.Render(ciRun)), |
| 20 | ForgeViewIopMrActions.Render(repoName, mergeRequest, repo, showActions), |
| 21 | ForgeViewIopComments.Render(comments), |
| 22 | ForgeViewIopCommentComposer.RenderMergeRequest(repoName, mergeRequest.Number)); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | internal static class ForgeViewIopMrPanel |
| 27 | { |
| 28 | internal static string Render( |
| 29 | string repoName, |
| 30 | ForgeMergeRequestEntity mergeRequest, |
| 31 | ForgeRepoEntity repo, |
| 32 | IReadOnlyList<(string Author, string Text, DateTimeOffset CreatedAt)> comments, |
| 33 | ForgeCiRunEntity? ciRun, |
| 34 | bool showActions, |
| 35 | string iopTab, |
| 36 | ForgeIopTabLinks tabLinks, |
| 37 | MergeRequestDiffResponse? diff, |
| 38 | string? sourceBranchForFiles = null, |
| 39 | IReadOnlyList<string>? handoffTargets = null) |
| 40 | { |
| 41 | var normalizedTab = ForgeIopTabs.Normalize(iopTab); |
| 42 | var changeCount = diff?.Stats.ChangedFiles ?? 0; |
| 43 | var tabBody = normalizedTab switch |
| 44 | { |
| 45 | "changes" => ForgeViewIopChangesTab.Render( |
| 46 | repoName, |
| 47 | mergeRequest, |
| 48 | diff, |
| 49 | sourceBranchForFiles ?? mergeRequest.SourceBranch), |
| 50 | "checks" => ForgeViewIopChecksTab.Render(ciRun), |
| 51 | _ => ForgeViewIopConversationTab.Render( |
| 52 | repoName, |
| 53 | mergeRequest, |
| 54 | repo, |
| 55 | comments, |
| 56 | ciRun, |
| 57 | showActions), |
| 58 | }; |
| 59 | |
| 60 | var handoff = normalizedTab == "conversation" |
| 61 | && showActions |
| 62 | && repo.DriveMode == DriveMode.AgentDriven |
| 63 | && handoffTargets is { Count: > 0 } |
| 64 | ? ForgeViewIopHandoffForm.Render(repoName, mergeRequest.Number, handoffTargets) |
| 65 | : ""; |
| 66 | |
| 67 | return ForgeViewIopPanel.Render( |
| 68 | ForgeViewIopTitle.Render(), |
| 69 | ForgeViewIopMrHeader.Render(repoName, mergeRequest), |
| 70 | ForgeViewIopSubTabs.Render(tabLinks, normalizedTab, comments.Count, changeCount, ciRun is not null), |
| 71 | tabBody, |
| 72 | handoff); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | internal static class ForgeViewIopIssuePanel |
| 77 | { |
| 78 | internal static string Render( |
| 79 | string repoName, |
| 80 | ForgeIssueEntity issue, |
| 81 | IReadOnlyList<(string Author, string Text, DateTimeOffset CreatedAt)> comments) => |
| 82 | ForgeViewIopPanel.Render( |
| 83 | ForgeViewIopTitle.Render(), |
| 84 | ForgeViewIopIssueHeader.Render(repoName, issue), |
| 85 | ForgeViewIopComments.Render(comments), |
| 86 | ForgeViewIopCommentComposer.RenderIssue(repoName, issue.Number)); |
| 87 | } |
| 88 | |
| 89 | internal static class ForgeViewIopBranchPanel |
| 90 | { |
| 91 | internal static string Render( |
| 92 | string repoName, |
| 93 | string branch, |
| 94 | ForgeMergeRequestEntity? mergeRequest, |
| 95 | ForgeRepoEntity repo, |
| 96 | ForgeRepository repository, |
| 97 | string iopTab, |
| 98 | ForgeIopTabLinks tabLinks, |
| 99 | MergeRequestDiffResponse? diff) |
| 100 | { |
| 101 | if (mergeRequest is null) |
| 102 | { |
| 103 | return ForgeViewIopPanel.Render( |
| 104 | ForgeViewIopTitle.Render(), |
| 105 | ForgeViewIopBranchEmpty.Render(repoName, branch)); |
| 106 | } |
| 107 | |
| 108 | var comments = repository.ListMergeRequestComments(mergeRequest.Id) |
| 109 | .Select(c => (c.Author, c.Body, c.CreatedAt)) |
| 110 | .ToList(); |
| 111 | var ciRun = repository.GetLatestCiRun(repo.Id, mergeRequest.Number); |
| 112 | var showActions = mergeRequest.Status == MergeRequestStatus.Open; |
| 113 | |
| 114 | return ForgeViewIopMrPanel.Render( |
| 115 | repoName, |
| 116 | mergeRequest, |
| 117 | repo, |
| 118 | comments, |
| 119 | ciRun, |
| 120 | showActions, |
| 121 | iopTab, |
| 122 | tabLinks, |
| 123 | diff, |
| 124 | branch); |
| 125 | } |
| 126 | } |
| 127 | |