Skip to content

Commit 39d69f2

Browse files
committed
refactor: clean up code formatting and improve readability in orchestrator and UI components
1 parent 6f465ad commit 39d69f2

File tree

3 files changed

+96
-37
lines changed

3 files changed

+96
-37
lines changed

crates/agentic-core/src/orchestrator.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ You MUST generate EXACTLY 3 proposals about this query: "{query}"
2828
}
2929
"#;
3030

31-
3231
#[derive(Deserialize, Debug)]
3332
struct ProposalObject {
3433
context: String,
@@ -47,7 +46,6 @@ struct ProposalsResponse {
4746
proposals: Vec<ProposalItem>,
4847
}
4948

50-
5149
pub async fn generate_proposals(
5250
query: &str,
5351
endpoint: &str,
@@ -97,4 +95,3 @@ pub async fn generate_proposals(
9795
))
9896
}
9997
}
100-

crates/agentic-tui/src/ui/app.rs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ pub struct App {
125125
cloud_response: Option<AtomicNote>,
126126
synthesis_scroll: u16,
127127
coaching_tip: (String, String),
128-
local_tokens_used: u32, // Token count for current local request
129-
cloud_tokens_used: u32, // Token count for current cloud request
128+
local_tokens_used: u32, // Token count for current local request
129+
cloud_tokens_used: u32, // Token count for current cloud request
130130
}
131131

132132
impl App {
@@ -481,9 +481,9 @@ impl App {
481481

482482
// Create a compact area for the synthesis (60% width, ~12 lines height, centered)
483483
let main_area = app_chunks[1];
484-
let synthesis_width = (main_area.width * 60 / 100).max(40).min(80);
484+
let synthesis_width = (main_area.width * 60 / 100).clamp(40, 80);
485485
let synthesis_height = 12.min(main_area.height - 6);
486-
486+
487487
let synthesis_area = Rect::new(
488488
main_area.x + (main_area.width.saturating_sub(synthesis_width)) / 2,
489489
main_area.y + (main_area.height.saturating_sub(synthesis_height)) / 2,
@@ -954,8 +954,28 @@ impl App {
954954
}
955955
}
956956
KeyCode::Right => {
957-
// Scroll down through synthesis content
958-
self.synthesis_scroll += 1;
957+
// Scroll down through synthesis content with bounds checking
958+
if let Some(note) = &self.cloud_response {
959+
// Calculate content height (number of lines when wrapped)
960+
let content_width = 60; // Approximate usable width after borders
961+
let lines: Vec<&str> = note.body_text.lines().collect();
962+
let total_wrapped_lines = lines
963+
.iter()
964+
.map(|line| {
965+
((line.len() as f32 / content_width as f32).ceil()
966+
as u16)
967+
.max(1)
968+
})
969+
.sum::<u16>();
970+
971+
let display_height = 10; // Approximate usable height (12 - 2 for borders)
972+
let max_scroll =
973+
total_wrapped_lines.saturating_sub(display_height);
974+
975+
if self.synthesis_scroll < max_scroll {
976+
self.synthesis_scroll += 1;
977+
}
978+
}
959979
}
960980
KeyCode::Enter | KeyCode::Esc => {
961981
// Fallback: return to normal without saving
@@ -993,11 +1013,11 @@ impl App {
9931013
// Handle regular chat message
9941014
// Store the original user query for metadata
9951015
self.original_user_query = message.clone();
996-
1016+
9971017
// Estimate tokens for local request (rough: chars/4 + prompt overhead)
9981018
self.local_tokens_used = (message.len() / 4) as u32 + 500; // ~500 tokens for prompt template
9991019
self.cloud_tokens_used = 0; // Reset cloud tokens for new session
1000-
1020+
10011021
self.agent_status = AgentStatus::Orchestrating;
10021022
let settings = self.settings.clone();
10031023
let tx = self.agent_tx.clone();
@@ -1016,17 +1036,17 @@ impl App {
10161036
self.edit_buffer.clear();
10171037
}
10181038

1019-
10201039
fn save_synthesis(&self) {
10211040
if let Some(note) = &self.cloud_response {
10221041
// Generate meaningful filename from query and metadata
10231042
let timestamp = chrono::Utc::now();
10241043
let date_part = timestamp.format("%Y-%m-%d").to_string();
1025-
1044+
10261045
// Extract keywords from original user query for filename
1027-
let keywords = self.extract_filename_keywords(&self.original_user_query, &note.header_tags);
1046+
let keywords =
1047+
self.extract_filename_keywords(&self.original_user_query, &note.header_tags);
10281048
let time_suffix = timestamp.format("-%H%M").to_string(); // Add time for uniqueness
1029-
1049+
10301050
let filename = format!("{}-{}{}.md", date_part, keywords, time_suffix);
10311051

10321052
// Get the selected proposal text
@@ -1042,23 +1062,29 @@ impl App {
10421062
let clean_proposal = proposal_text;
10431063

10441064
// Get model names for usage metadata
1045-
let local_model = if self.settings.local_model.is_empty() || self.settings.local_model == "[SELECT]" {
1065+
let local_model = if self.settings.local_model.is_empty()
1066+
|| self.settings.local_model == "[SELECT]"
1067+
{
10461068
"unknown"
10471069
} else {
10481070
&self.settings.local_model
10491071
};
10501072

1051-
let cloud_model = if self.settings.cloud_model.is_empty() || self.settings.cloud_model == "[SELECT]" {
1073+
let cloud_model = if self.settings.cloud_model.is_empty()
1074+
|| self.settings.cloud_model == "[SELECT]"
1075+
{
10521076
"anthropic/claude-3.5-sonnet"
10531077
} else {
10541078
&self.settings.cloud_model
10551079
};
10561080

10571081
// Estimate token breakdown (rough estimates)
10581082
let local_prompt_tokens = (self.original_user_query.len() / 4) as u32 + 200; // Query + template
1059-
let local_completion_tokens = self.local_tokens_used.saturating_sub(local_prompt_tokens);
1060-
let cloud_prompt_tokens = (self.final_prompt.len() / 4) as u32 + 150; // Proposal + synthesis template
1061-
let cloud_completion_tokens = self.cloud_tokens_used.saturating_sub(cloud_prompt_tokens);
1083+
let local_completion_tokens =
1084+
self.local_tokens_used.saturating_sub(local_prompt_tokens);
1085+
let cloud_prompt_tokens = (self.final_prompt.len() / 4) as u32 + 150; // Proposal + synthesis template
1086+
let cloud_completion_tokens =
1087+
self.cloud_tokens_used.saturating_sub(cloud_prompt_tokens);
10621088

10631089
let markdown_content = format!(
10641090
"---\ndate: {}\nprovider: \"OPENROUTER\"\nquery: \"{}\"\nproposal: \"{}\"\ntags: [{}]\n\nusage:\n local_model: \"{}\"\n local_prompt_tokens: {}\n local_completion_tokens: {}\n cloud_model: \"{}\"\n cloud_prompt_tokens: {}\n cloud_completion_tokens: {}\n---\n\n# {}\n\n{}\n",
@@ -1093,7 +1119,7 @@ impl App {
10931119
fn handle_cloud_synthesis(&mut self) {
10941120
// Set status to searching and trigger cloud API call
10951121
self.agent_status = AgentStatus::Searching;
1096-
1122+
10971123
// Estimate tokens for cloud request (prompt + synthesis template)
10981124
self.cloud_tokens_used = (self.final_prompt.len() / 4) as u32 + 300; // ~300 tokens for synthesis template
10991125

@@ -1264,9 +1290,9 @@ impl App {
12641290
"the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with",
12651291
"by", "is", "are", "was", "were", "be", "been", "have", "has", "had", "do", "does",
12661292
"did", "will", "would", "could", "should", "can", "what", "where", "when", "why",
1267-
"how", "who", "which", "that", "this", "these", "those", "i", "you", "he", "she",
1268-
"it", "we", "they", "me", "him", "her", "us", "them", "my", "your", "his", "her",
1269-
"its", "our", "their"
1293+
"how", "who", "which", "that", "this", "these", "those", "i", "you", "he", "she", "it",
1294+
"we", "they", "me", "him", "her", "us", "them", "my", "your", "his", "her", "its",
1295+
"our", "their",
12701296
];
12711297

12721298
// Extract meaningful words from query
@@ -1276,7 +1302,7 @@ impl App {
12761302
.filter_map(|word| {
12771303
// Clean up punctuation
12781304
let clean_word = word.trim_matches(|c: char| !c.is_alphanumeric());
1279-
1305+
12801306
// Filter out stop words and short words
12811307
if clean_word.len() >= 3 && !stop_words.contains(&clean_word) {
12821308
Some(clean_word.to_string())
@@ -1307,5 +1333,4 @@ impl App {
13071333
}
13081334
}
13091335
}
1310-
13111336
}

crates/agentic-tui/src/ui/header.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub fn render_header(
2222
// Dynamic title based on what's actually configured - much smarter!
2323
let title = Title::from(" Agentic v0.1.0 ").alignment(Alignment::Left);
2424

25-
let (status_text, status_color) = build_smart_status_with_color(status, settings, local_tokens, cloud_tokens);
25+
let (status_text, status_color) =
26+
build_smart_status_with_color(status, settings, local_tokens, cloud_tokens);
2627

2728
let status_span = Span::styled(status_text, Style::default().fg(status_color));
2829

@@ -39,7 +40,12 @@ pub fn render_header(
3940
frame.render_widget(header_paragraph, area);
4041
}
4142

42-
fn build_smart_status_with_color(status: AgentStatus, settings: &Settings, local_tokens: u32, cloud_tokens: u32) -> (String, Color) {
43+
fn build_smart_status_with_color(
44+
status: AgentStatus,
45+
settings: &Settings,
46+
local_tokens: u32,
47+
cloud_tokens: u32,
48+
) -> (String, Color) {
4349
// Show actual configuration state with model names always visible
4450
let local_configured = settings.local_model != "[SELECT]";
4551
let cloud_configured =
@@ -72,7 +78,11 @@ fn build_smart_status_with_color(status: AgentStatus, settings: &Settings, local
7278
format!(" | ({})", format_single_tokens(local))
7379
}
7480
AgentStatus::Searching if local > 0 && cloud > 0 => {
75-
format!(" | ({}) + ({})", format_single_tokens(local), format_single_tokens(cloud))
81+
format!(
82+
" | ({}) + ({})",
83+
format_single_tokens(local),
84+
format_single_tokens(cloud)
85+
)
7686
}
7787
AgentStatus::Complete if local > 0 => {
7888
let total = local + cloud;
@@ -82,7 +92,7 @@ fn build_smart_status_with_color(status: AgentStatus, settings: &Settings, local
8292
format!(" | ({})", format_single_tokens(local))
8393
}
8494
}
85-
_ => String::new()
95+
_ => String::new(),
8696
}
8797
};
8898

@@ -103,28 +113,55 @@ fn build_smart_status_with_color(status: AgentStatus, settings: &Settings, local
103113
format!("Ruixen :: {} :: [CONFIGURE API KEY]", local_display)
104114
}
105115
AgentStatus::ValidatingLocal => {
106-
format!("Ruixen :: [CHECKING {}] :: {}", local_display, cloud_display)
116+
format!(
117+
"Ruixen :: [CHECKING {}] :: {}",
118+
local_display, cloud_display
119+
)
107120
}
108121
AgentStatus::ValidatingCloud => {
109-
format!("Ruixen :: {} :: [CHECKING {}]", local_display, cloud_display)
122+
format!(
123+
"Ruixen :: {} :: [CHECKING {}]",
124+
local_display, cloud_display
125+
)
110126
}
111127
AgentStatus::LocalEndpointError => {
112-
format!("Ruixen :: [ERROR: {} UNREACHABLE] :: {}", local_display, cloud_display)
128+
format!(
129+
"Ruixen :: [ERROR: {} UNREACHABLE] :: {}",
130+
local_display, cloud_display
131+
)
113132
}
114133
AgentStatus::CloudEndpointError => {
115-
format!("Ruixen :: {} :: [ERROR: {} UNREACHABLE]", local_display, cloud_display)
134+
format!(
135+
"Ruixen :: {} :: [ERROR: {} UNREACHABLE]",
136+
local_display, cloud_display
137+
)
116138
}
117139
AgentStatus::Orchestrating => {
118140
// Show local tokens during orchestration
119-
format!("Ruixen :: {} :: {}{}", local_display, cloud_display, format_token_display(local_tokens, cloud_tokens, status))
141+
format!(
142+
"Ruixen :: {} :: {}{}",
143+
local_display,
144+
cloud_display,
145+
format_token_display(local_tokens, cloud_tokens, status)
146+
)
120147
}
121148
AgentStatus::Searching => {
122149
// Show local + cloud tokens during synthesis
123-
format!("Ruixen :: {} :: {}{}", local_display, cloud_display, format_token_display(local_tokens, cloud_tokens, status))
150+
format!(
151+
"Ruixen :: {} :: {}{}",
152+
local_display,
153+
cloud_display,
154+
format_token_display(local_tokens, cloud_tokens, status)
155+
)
124156
}
125157
AgentStatus::Complete => {
126158
// Show total bill (red color applied later)
127-
format!("Ruixen :: {} :: {}{}", local_display, cloud_display, format_token_display(local_tokens, cloud_tokens, status))
159+
format!(
160+
"Ruixen :: {} :: {}{}",
161+
local_display,
162+
cloud_display,
163+
format_token_display(local_tokens, cloud_tokens, status)
164+
)
128165
}
129166
};
130167

0 commit comments

Comments
 (0)