mirror of
https://github.com/openai/codex.git
synced 2026-05-03 21:01:55 +03:00
306 lines
9.3 KiB
Rust
306 lines
9.3 KiB
Rust
use std::time::Duration;
|
|
|
|
// Environment filter data models for the TUI
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct EnvironmentRow {
|
|
pub id: String,
|
|
pub label: Option<String>,
|
|
pub is_pinned: bool,
|
|
pub repo_hints: Option<String>, // e.g., "openai/codex"
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct EnvModalState {
|
|
pub query: String,
|
|
pub selected: usize,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
|
|
pub enum ApplyResultLevel {
|
|
Success,
|
|
Partial,
|
|
Error,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct ApplyModalState {
|
|
pub task_id: TaskId,
|
|
pub title: String,
|
|
pub result_message: Option<String>,
|
|
pub result_level: Option<ApplyResultLevel>,
|
|
pub skipped_paths: Vec<String>,
|
|
pub conflict_paths: Vec<String>,
|
|
}
|
|
|
|
use crate::scrollable_diff::ScrollableDiff;
|
|
use codex_cloud_tasks_client::CloudBackend;
|
|
use codex_cloud_tasks_client::TaskId;
|
|
use codex_cloud_tasks_client::TaskSummary;
|
|
use throbber_widgets_tui::ThrobberState;
|
|
|
|
#[derive(Default)]
|
|
pub struct App {
|
|
pub tasks: Vec<TaskSummary>,
|
|
pub selected: usize,
|
|
pub status: String,
|
|
pub diff_overlay: Option<DiffOverlay>,
|
|
pub throbber: ThrobberState,
|
|
pub refresh_inflight: bool,
|
|
pub details_inflight: bool,
|
|
// Environment filter state
|
|
pub env_filter: Option<String>,
|
|
pub env_modal: Option<EnvModalState>,
|
|
pub apply_modal: Option<ApplyModalState>,
|
|
pub environments: Vec<EnvironmentRow>,
|
|
pub env_last_loaded: Option<std::time::Instant>,
|
|
pub env_loading: bool,
|
|
pub env_error: Option<String>,
|
|
// New Task page
|
|
pub new_task: Option<crate::new_task::NewTaskPage>,
|
|
// Apply preflight spinner state
|
|
pub apply_preflight_inflight: bool,
|
|
// Apply action spinner state
|
|
pub apply_inflight: bool,
|
|
// Background enrichment coordination
|
|
pub list_generation: u64,
|
|
pub in_flight: std::collections::HashSet<String>,
|
|
// Background enrichment caches were planned; currently unused.
|
|
}
|
|
|
|
impl App {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
tasks: Vec::new(),
|
|
selected: 0,
|
|
status: "Press r to refresh".to_string(),
|
|
diff_overlay: None,
|
|
throbber: ThrobberState::default(),
|
|
refresh_inflight: false,
|
|
details_inflight: false,
|
|
env_filter: None,
|
|
env_modal: None,
|
|
apply_modal: None,
|
|
environments: Vec::new(),
|
|
env_last_loaded: None,
|
|
env_loading: false,
|
|
env_error: None,
|
|
new_task: None,
|
|
apply_preflight_inflight: false,
|
|
apply_inflight: false,
|
|
list_generation: 0,
|
|
in_flight: std::collections::HashSet::new(),
|
|
}
|
|
}
|
|
|
|
pub fn next(&mut self) {
|
|
if self.tasks.is_empty() {
|
|
return;
|
|
}
|
|
self.selected = (self.selected + 1).min(self.tasks.len().saturating_sub(1));
|
|
}
|
|
|
|
pub fn prev(&mut self) {
|
|
if self.tasks.is_empty() {
|
|
return;
|
|
}
|
|
if self.selected > 0 {
|
|
self.selected -= 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn load_tasks(
|
|
backend: &dyn CloudBackend,
|
|
env: Option<&str>,
|
|
) -> anyhow::Result<Vec<TaskSummary>> {
|
|
// In later milestones, add a small debounce, spinner, and error display.
|
|
let tasks = tokio::time::timeout(Duration::from_secs(5), backend.list_tasks(env)).await??;
|
|
// Hide review-only tasks from the main list.
|
|
let filtered: Vec<TaskSummary> = tasks.into_iter().filter(|t| !t.is_review).collect();
|
|
Ok(filtered)
|
|
}
|
|
|
|
pub struct DiffOverlay {
|
|
pub title: String,
|
|
pub task_id: TaskId,
|
|
pub sd: ScrollableDiff,
|
|
pub can_apply: bool,
|
|
pub diff_lines: Vec<String>,
|
|
// Optional alternate view: conversation text (prompt + assistant messages)
|
|
pub text_lines: Vec<String>,
|
|
pub prompt: Option<String>,
|
|
pub current_view: DetailView,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum DetailView {
|
|
Diff,
|
|
Prompt,
|
|
}
|
|
|
|
/// Internal app events delivered from background tasks.
|
|
/// These let the UI event loop remain responsive and keep the spinner animating.
|
|
#[derive(Debug)]
|
|
pub enum AppEvent {
|
|
TasksLoaded {
|
|
env: Option<String>,
|
|
result: anyhow::Result<Vec<TaskSummary>>,
|
|
},
|
|
// Background diff summary events were planned; removed for now to keep code minimal.
|
|
/// Autodetection of a likely environment id finished
|
|
EnvironmentAutodetected(anyhow::Result<crate::env_detect::AutodetectSelection>),
|
|
/// Background completion of environment list fetch
|
|
EnvironmentsLoaded(anyhow::Result<Vec<EnvironmentRow>>),
|
|
DetailsDiffLoaded {
|
|
id: TaskId,
|
|
title: String,
|
|
diff: String,
|
|
},
|
|
DetailsMessagesLoaded {
|
|
id: TaskId,
|
|
title: String,
|
|
messages: Vec<String>,
|
|
prompt: Option<String>,
|
|
},
|
|
DetailsFailed {
|
|
id: TaskId,
|
|
title: String,
|
|
error: String,
|
|
},
|
|
/// Background completion of new task submission
|
|
NewTaskSubmitted(Result<codex_cloud_tasks_client::CreatedTask, String>),
|
|
/// Background completion of apply preflight when opening modal or on demand
|
|
ApplyPreflightFinished {
|
|
id: TaskId,
|
|
title: String,
|
|
message: String,
|
|
level: ApplyResultLevel,
|
|
skipped: Vec<String>,
|
|
conflicts: Vec<String>,
|
|
},
|
|
/// Background completion of apply action (actual patch application)
|
|
ApplyFinished {
|
|
id: TaskId,
|
|
result: std::result::Result<codex_cloud_tasks_client::ApplyOutcome, String>,
|
|
},
|
|
}
|
|
|
|
// Convenience aliases; currently unused.
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use chrono::Utc;
|
|
|
|
struct FakeBackend {
|
|
// maps env key to titles
|
|
by_env: std::collections::HashMap<Option<String>, Vec<&'static str>>,
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl codex_cloud_tasks_client::CloudBackend for FakeBackend {
|
|
async fn list_tasks(
|
|
&self,
|
|
env: Option<&str>,
|
|
) -> codex_cloud_tasks_client::Result<Vec<TaskSummary>> {
|
|
let key = env.map(|s| s.to_string());
|
|
let titles = self
|
|
.by_env
|
|
.get(&key)
|
|
.cloned()
|
|
.unwrap_or_else(|| vec!["default-a", "default-b"]);
|
|
let mut out = Vec::new();
|
|
for (i, t) in titles.into_iter().enumerate() {
|
|
out.push(TaskSummary {
|
|
id: TaskId(format!("T-{i}")),
|
|
title: t.to_string(),
|
|
status: codex_cloud_tasks_client::TaskStatus::Ready,
|
|
updated_at: Utc::now(),
|
|
environment_id: env.map(|s| s.to_string()),
|
|
environment_label: None,
|
|
summary: codex_cloud_tasks_client::DiffSummary::default(),
|
|
is_review: false,
|
|
});
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
async fn get_task_diff(
|
|
&self,
|
|
_id: TaskId,
|
|
) -> codex_cloud_tasks_client::Result<Option<String>> {
|
|
Err(codex_cloud_tasks_client::Error::Unimplemented(
|
|
"not used in test",
|
|
))
|
|
}
|
|
|
|
async fn get_task_messages(
|
|
&self,
|
|
_id: TaskId,
|
|
) -> codex_cloud_tasks_client::Result<Vec<String>> {
|
|
Ok(vec![])
|
|
}
|
|
async fn get_task_text(
|
|
&self,
|
|
_id: TaskId,
|
|
) -> codex_cloud_tasks_client::Result<codex_cloud_tasks_client::TaskText> {
|
|
Ok(codex_cloud_tasks_client::TaskText {
|
|
prompt: Some("Example prompt".to_string()),
|
|
messages: Vec::new(),
|
|
})
|
|
}
|
|
|
|
async fn apply_task(
|
|
&self,
|
|
_id: TaskId,
|
|
) -> codex_cloud_tasks_client::Result<codex_cloud_tasks_client::ApplyOutcome> {
|
|
Err(codex_cloud_tasks_client::Error::Unimplemented(
|
|
"not used in test",
|
|
))
|
|
}
|
|
|
|
async fn apply_task_preflight(
|
|
&self,
|
|
_id: TaskId,
|
|
) -> codex_cloud_tasks_client::Result<codex_cloud_tasks_client::ApplyOutcome> {
|
|
Err(codex_cloud_tasks_client::Error::Unimplemented(
|
|
"not used in test",
|
|
))
|
|
}
|
|
|
|
async fn create_task(
|
|
&self,
|
|
_env_id: &str,
|
|
_prompt: &str,
|
|
_git_ref: &str,
|
|
_qa_mode: bool,
|
|
) -> codex_cloud_tasks_client::Result<codex_cloud_tasks_client::CreatedTask> {
|
|
Err(codex_cloud_tasks_client::Error::Unimplemented(
|
|
"not used in test",
|
|
))
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn load_tasks_uses_env_parameter() {
|
|
// Arrange: env-specific task titles
|
|
let mut by_env = std::collections::HashMap::new();
|
|
by_env.insert(None, vec!["root-1", "root-2"]);
|
|
by_env.insert(Some("env-A".to_string()), vec!["A-1"]);
|
|
by_env.insert(Some("env-B".to_string()), vec!["B-1", "B-2", "B-3"]);
|
|
let backend = FakeBackend { by_env };
|
|
|
|
// Act + Assert
|
|
let root = load_tasks(&backend, None).await.unwrap();
|
|
assert_eq!(root.len(), 2);
|
|
assert_eq!(root[0].title, "root-1");
|
|
|
|
let a = load_tasks(&backend, Some("env-A")).await.unwrap();
|
|
assert_eq!(a.len(), 1);
|
|
assert_eq!(a[0].title, "A-1");
|
|
|
|
let b = load_tasks(&backend, Some("env-B")).await.unwrap();
|
|
assert_eq!(b.len(), 3);
|
|
assert_eq!(b[2].title, "B-3");
|
|
}
|
|
}
|