Repository navigation
Expand file tree
/
Copy pathcockpit.rs
More file actions
159 lines (144 loc) · 3.8 KB
/
Copy pathcockpit.rs
File metadata and controls
159 lines (144 loc) · 3.8 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! OpenHuman-native overlays and structured control-plane state.
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverlayKind {
Help,
Threads,
Rename,
ConfirmDelete,
Model,
Permissions,
Status,
Usage,
Goal,
Tasks,
Agents,
Skills,
Mcp,
Artifacts,
Approvals,
PlanReview,
Diff,
HistorySearch,
Files,
}
#[derive(Debug, Clone)]
pub struct Overlay {
pub kind: OverlayKind,
pub title: String,
pub rows: Vec<OverlayRow>,
pub selected: usize,
pub filter: String,
pub status: String,
pub input: Option<String>,
}
#[derive(Debug, Clone)]
pub struct OverlayRow {
pub id: String,
pub label: String,
pub detail: String,
pub payload: Value,
}
impl Overlay {
pub fn new(kind: OverlayKind, title: impl Into<String>) -> Self {
Self {
kind,
title: title.into(),
rows: Vec::new(),
selected: 0,
filter: String::new(),
status: String::new(),
input: None,
}
}
pub fn visible_rows(&self) -> Vec<&OverlayRow> {
let filter = self.filter.to_ascii_lowercase();
self.rows
.iter()
.filter(|row| {
filter.is_empty()
|| row.label.to_ascii_lowercase().contains(&filter)
|| row.detail.to_ascii_lowercase().contains(&filter)
})
.collect()
}
pub fn clamp_selection(&mut self) {
let len = self.visible_rows().len();
self.selected = self.selected.min(len.saturating_sub(1));
}
}
#[derive(Debug, Clone)]
pub struct PendingApproval {
pub request_id: String,
pub tool_name: String,
pub summary: String,
pub args: Value,
}
#[derive(Debug, Clone)]
pub struct PendingPlanReview {
pub request_id: String,
pub summary: String,
pub steps: Vec<String>,
}
pub fn unwrap_rpc(mut value: &Value) -> &Value {
loop {
if let Some(next) = value.get("result").or_else(|| value.get("data")) {
value = next;
} else {
return value;
}
}
}
pub fn array_at<'a>(value: &'a Value, keys: &[&str]) -> &'a [Value] {
let value = unwrap_rpc(value);
for key in keys {
if let Some(items) = value.get(*key).and_then(Value::as_array) {
return items;
}
}
&[]
}
pub fn row_from_value(value: &Value, id_keys: &[&str], label_keys: &[&str]) -> OverlayRow {
let string = |keys: &[&str]| {
keys.iter()
.find_map(|key| value.get(*key).and_then(Value::as_str))
.unwrap_or_default()
.to_string()
};
let id = string(id_keys);
let label = string(label_keys);
OverlayRow {
id: id.clone(),
label: if label.is_empty() { id } else { label },
detail: value
.get("description")
.or_else(|| value.get("status"))
.and_then(Value::as_str)
.unwrap_or_default()
.to_string(),
payload: value.clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn overlay_filter_matches_label_and_detail() {
let mut overlay = Overlay::new(OverlayKind::Threads, "Threads");
overlay.rows.push(OverlayRow {
id: "1".into(),
label: "Release prep".into(),
detail: "yesterday".into(),
payload: Value::Null,
});
overlay.filter = "yester".into();
assert_eq!(overlay.visible_rows().len(), 1);
overlay.filter = "missing".into();
assert!(overlay.visible_rows().is_empty());
}
#[test]
fn unwrap_rpc_handles_nested_envelopes() {
let value = serde_json::json!({"result":{"data":{"threads":[]}}});
assert!(unwrap_rpc(&value).get("threads").is_some());
}
}