Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / infra / agents / agentd / src / triage_ui.rs

triage_ui.rs136 lines · 4.8 KiBRaw
1//! Triage UI: dashboard section, task-page result box, and the
2//! "Start fix task" route that turns a triaged issue into a normal task.
3
4use axum::{
5 extract::{Path, State},
6 http::{HeaderMap, StatusCode},
7 response::{IntoResponse, Redirect, Response},
8};
9use maud::{html, Markup};
10
11use crate::models::{branch_name, Task};
12use crate::state::AppState;
13use crate::store::TriageRow;
14
15fn issue_url(repo: &str, n: u64) -> String {
16 format!("https://github.com/{repo}/issues/{n}")
17}
18
19fn result_field<'a>(v: &'a serde_json::Value, k: &str) -> Option<&'a str> {
20 v.get(k).and_then(|x| x.as_str())
21}
22
23fn labels_line(v: &serde_json::Value) -> String {
24 let list = |k: &str, sign: &str| -> Vec<String> {
25 v.get(k)
26 .and_then(|x| x.as_array())
27 .map(|a| a.iter().filter_map(|l| l.as_str()).map(|l| format!("{sign}{l}")).collect())
28 .unwrap_or_default()
29 };
30 let mut all = list("labels_added", "+");
31 all.extend(list("labels_removed", "−"));
32 if all.is_empty() {
33 "labels unchanged".to_string()
34 } else {
35 all.join(" ")
36 }
37}
38
39fn fix_button(issue: u64) -> Markup {
40 html! {
41 form method="post" action=(format!("/triage/{issue}/fix")) class="inline" {
42 button type="submit" { "Start fix task" }
43 }
44 }
45}
46
47fn result_summary(raw: Option<&str>, repo: &str) -> Markup {
48 let v: serde_json::Value = raw.and_then(|r| serde_json::from_str(r).ok()).unwrap_or_default();
49 if v.is_null() {
50 return html! { span class="muted" { "no result yet" } };
51 }
52 html! {
53 span { (result_field(&v, "type").unwrap_or("?")) " · " (result_field(&v, "app").unwrap_or("?"))
54 " · " (result_field(&v, "confidence").unwrap_or("?")) " confidence · " (labels_line(&v)) }
55 @if let Some(url) = result_field(&v, "comment_url") {
56 @if url.starts_with("https://github.com/") {
57 " · " a href=(url) { "comment" }
58 }
59 } @else if v.get("skipped_writes").and_then(|x| x.as_bool()) == Some(true) {
60 " · " span class="muted" { "GitHub not updated (no token)" }
61 }
62 @if let Some(dup) = v.get("duplicate_of").and_then(|x| x.as_u64()) {
63 " · possible duplicate of " a href=(issue_url(repo, dup)) { "#" (dup) }
64 }
65 }
66}
67
68/// Dashboard "Triage" section: the most recent automatic triages.
69pub fn dashboard_section(rows: &[TriageRow], repo: &str) -> Markup {
70 html! {
71 @if !rows.is_empty() {
72 section class="dash-section" {
73 h2 { "Triage" }
74 @for r in rows {
75 div class="task-row" {
76 span class="badge" { "triage" }
77 a href=(issue_url(repo, r.issue_number)) { "#" (r.issue_number) }
78 a href=(format!("/tasks/{}", r.task_id)) { "task " (r.task_id) }
79 span class="reason" { (r.status) }
80 span class="small" { (result_summary(r.result_json.as_deref(), repo)) }
81 (fix_button(r.issue_number))
82 }
83 }
84 }
85 }
86 }
87}
88
89/// Side-panel block for a triage task: badge, issue link, outcome, fix button.
90pub fn task_panel_block(task: &Task, repo: &str) -> Markup {
91 let Some(n) = task.issue_number.map(|n| n as u64) else {
92 return html! {};
93 };
94 html! {
95 h3 { span class="badge" { "triage" } " Issue " a href=(issue_url(repo, n)) { "#" (n) } }
96 @if let Some(s) = &task.summary {
97 div class="p-row" { (s) }
98 }
99 (fix_button(n))
100 }
101}
102
103/// `POST /triage/:issue/fix`: create a normal interactive task from the issue
104/// (Plan first on, issue linked) and open it.
105pub async fn fix_route(
106 State(state): State<AppState>,
107 headers: HeaderMap,
108 Path(issue): Path<u64>,
109) -> Response {
110 if let Some(reject) = crate::ui::require_auth(&state, &headers) {
111 return reject;
112 }
113 if let Some(reject) = crate::ui::require_origin(&state, &headers) {
114 return reject;
115 }
116 let found = match crate::issues::fetch_issue(&state.config, issue).await {
117 Ok(i) => i,
118 Err(e) => return (StatusCode::BAD_GATEWAY, format!("could not fetch issue #{issue}: {e}")).into_response(),
119 };
120 let (title, prompt, scope) = crate::issues::prefill_from_issue(&found);
121 let id = match state.store.create_task(
122 &title,
123 &prompt,
124 &scope.unwrap_or_default(),
125 &state.config.default_model,
126 60,
127 2.0,
128 ) {
129 Ok(id) => id,
130 Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, e).into_response(),
131 };
132 let _ = state.store.set_branch(id, &branch_name(id, &title));
133 let _ = state.store.set_plan_gate(id, true);
134 let _ = state.store.set_issue_number(id, issue as i64);
135 Redirect::to(&format!("/tasks/{id}")).into_response()
136}