refactor: improve code readability and consistency across multiple files
This commit is contained in:
@@ -217,7 +217,7 @@ fn draw(frame: &mut ratatui::Frame, state: &AppStateRest) {
|
||||
|
||||
// Show toasts at the top if present.
|
||||
for toast in &state.misc.toasts {
|
||||
content_lines.push(format!("[{}] {}", format!("{:?}", toast.kind), toast.message));
|
||||
content_lines.push(format!("[{:?}] {}", toast.kind, toast.message));
|
||||
}
|
||||
|
||||
// Show active overlay name.
|
||||
|
||||
@@ -196,7 +196,10 @@ fn handle_tick(state: &mut AppStateRest) {
|
||||
.turn_events
|
||||
.lock()
|
||||
.map(|mut events| events.drain(..).collect())
|
||||
.unwrap_or_default();
|
||||
.unwrap_or_else(|e| {
|
||||
tracing::error!("turn_events mutex poisoned, recovering");
|
||||
e.into_inner().drain(..).collect()
|
||||
});
|
||||
|
||||
for event in drained {
|
||||
use zesdex_infrastructure::TurnEvent;
|
||||
@@ -209,11 +212,9 @@ fn handle_tick(state: &mut AppStateRest) {
|
||||
}
|
||||
handle_system_note(state, message);
|
||||
}
|
||||
TurnEvent::AssistantMessage(msg) => {
|
||||
state.push_transcript(ChatMessageDisplay::new(
|
||||
RoleWrapper::Assistant,
|
||||
msg.content.unwrap_or_default(),
|
||||
));
|
||||
TurnEvent::AssistantMessage(_msg) => {
|
||||
// AssistantMessage is handled via StreamDone to avoid duplicates.
|
||||
state.dirty = true;
|
||||
}
|
||||
TurnEvent::StreamToken(_token) => {
|
||||
state.dirty = true;
|
||||
@@ -248,6 +249,11 @@ fn handle_tick(state: &mut AppStateRest) {
|
||||
}
|
||||
state.dirty = true;
|
||||
}
|
||||
TurnEvent::ToolResult { .. } => {
|
||||
// Tool result events are logged but the content is already in
|
||||
// the session runtime messages — no transcript push needed here.
|
||||
state.dirty = true;
|
||||
}
|
||||
_ => {
|
||||
state.dirty = true;
|
||||
}
|
||||
@@ -392,11 +398,14 @@ fn handle_abort_turn(state: &mut AppStateRest) {
|
||||
|
||||
fn handle_compact(state: &mut AppStateRest) {
|
||||
tracing::info!("compacting conversation");
|
||||
const KEEP_COUNT: usize = 10;
|
||||
const KEEP_HEAD: usize = 2; // system prompt + tool definitions
|
||||
const KEEP_TAIL: usize = 10; // recent conversation messages
|
||||
if let Some(ref mut rt) = state.session_runtime {
|
||||
if rt.messages.len() > KEEP_COUNT {
|
||||
let keep = rt.messages.split_off(rt.messages.len() - KEEP_COUNT);
|
||||
rt.messages = keep;
|
||||
if rt.messages.len() > KEEP_HEAD + KEEP_TAIL {
|
||||
let tail = rt.messages.split_off(rt.messages.len() - KEEP_TAIL);
|
||||
let head: Vec<_> = rt.messages.drain(..KEEP_HEAD.min(rt.messages.len())).collect();
|
||||
rt.messages = head;
|
||||
rt.messages.extend(tail);
|
||||
let msg_count = rt.messages.len();
|
||||
state.push_transcript(ChatMessageDisplay::new(
|
||||
RoleWrapper::System,
|
||||
@@ -418,14 +427,26 @@ fn handle_open_editor(state: &mut AppStateRest, path: String) {
|
||||
|
||||
fn handle_mcp_add(state: &mut AppStateRest, name: String, command: String) {
|
||||
tracing::info!("adding MCP server: {name}");
|
||||
state.toast_info(format!("MCP server '{name}' registered with command: {command}"));
|
||||
state.mcp_manager.register(&name, &command);
|
||||
state.toast_success(format!("MCP server '{name}' added with command: {command}"));
|
||||
state.dirty = true;
|
||||
}
|
||||
|
||||
fn handle_start_oauth(state: &mut AppStateRest, provider: String) {
|
||||
tracing::info!("starting OAuth for provider: {provider}");
|
||||
state.toast_info(format!("OAuth flow started for {provider}..."));
|
||||
if let Err(e) = webbrowser::open(&format!("https://{provider}.com/auth")) {
|
||||
// Construct provider-specific OAuth authorization URL.
|
||||
let auth_url_owned;
|
||||
let url_to_open = match provider.as_str() {
|
||||
"github" => "https://github.com/login/oauth/authorize",
|
||||
"google" => "https://accounts.google.com/o/oauth2/v2/auth",
|
||||
"anthropic" => "https://anthropic.com/api/oauth/authorize",
|
||||
other => {
|
||||
auth_url_owned = format!("https://{other}.com/auth");
|
||||
&auth_url_owned
|
||||
}
|
||||
};
|
||||
if let Err(e) = webbrowser::open(url_to_open) {
|
||||
tracing::warn!("Failed to open browser for OAuth: {e}");
|
||||
state.toast_error(format!("Failed to open browser: {e}"));
|
||||
}
|
||||
@@ -749,10 +770,14 @@ pub fn handle_daemon_client(
|
||||
if let Some(text) = state.misc.pending_clipboard_copy.take() {
|
||||
conn.send(&DaemonFrame::ClipboardCopy(text))?;
|
||||
}
|
||||
send_daemon_update(&mut conn, state)?;
|
||||
// Only send state update for valid requests, not on EOF/disconnect.
|
||||
if running {
|
||||
send_daemon_update(&mut conn, state)?;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
running = false;
|
||||
// Don't call send_daemon_update — connection is dead.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user