refactor(agents): clean up agent command implementation

- Remove unused imports (regex, Arc)
- Fix receiver/child tuple order from spawn() call
- Convert CommandEvent bytes to strings properly
- Remove unused variables (_start_time, mut from installations)
- Update function signatures to match new API
- Simplify child process registration by removing child parameter
This commit is contained in:
Vivek R
2025-07-28 20:52:42 +05:30
parent 32a197100a
commit fd49a9e2ab

View File

@@ -2,14 +2,13 @@ use anyhow::Result;
use chrono; use chrono;
use dirs; use dirs;
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use regex;
use reqwest; use reqwest;
use rusqlite::{params, Connection, Result as SqliteResult}; use rusqlite::{params, Connection, Result as SqliteResult};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::process::Stdio; use std::process::Stdio;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
use tauri::{AppHandle, Emitter, Manager, State}; use tauri::{AppHandle, Emitter, Manager, State};
use tauri_plugin_shell::ShellExt; use tauri_plugin_shell::ShellExt;
use tauri_plugin_shell::process::CommandEvent; use tauri_plugin_shell::process::CommandEvent;
@@ -851,13 +850,13 @@ async fn spawn_agent_sidecar(
// Spawn the process // Spawn the process
info!("🚀 Spawning Claude sidecar process..."); info!("🚀 Spawning Claude sidecar process...");
let (mut child, mut receiver) = sidecar_cmd.spawn().map_err(|e| { let (mut receiver, child) = sidecar_cmd.spawn().map_err(|e| {
error!("❌ Failed to spawn Claude sidecar process: {}", e); error!("❌ Failed to spawn Claude sidecar process: {}", e);
format!("Failed to spawn Claude sidecar: {}", e) format!("Failed to spawn Claude sidecar: {}", e)
})?; })?;
// Get the PID // Get the PID from child
let pid = child.pid() as u32; let pid = child.pid();
let now = chrono::Utc::now().to_rfc3339(); let now = chrono::Utc::now().to_rfc3339();
info!("✅ Claude sidecar process spawned successfully with PID: {}", pid); info!("✅ Claude sidecar process spawned successfully with PID: {}", pid);
@@ -881,7 +880,7 @@ async fn spawn_agent_sidecar(
// Shared state for collecting session ID and live output // Shared state for collecting session ID and live output
let session_id = std::sync::Arc::new(Mutex::new(String::new())); let session_id = std::sync::Arc::new(Mutex::new(String::new()));
let live_output = std::sync::Arc::new(Mutex::new(String::new())); let live_output = std::sync::Arc::new(Mutex::new(String::new()));
let start_time = std::time::Instant::now(); let _start_time = std::time::Instant::now();
// Register the process in the registry // Register the process in the registry
registry registry
@@ -890,11 +889,10 @@ async fn spawn_agent_sidecar(
run_id, run_id,
agent_id, agent_id,
agent_name, agent_name,
pid, pid as u32,
project_path.clone(), project_path.clone(),
task.clone(), task.clone(),
execution_model.clone(), execution_model.clone(),
child,
) )
.map_err(|e| format!("Failed to register sidecar process: {}", e))?; .map_err(|e| format!("Failed to register sidecar process: {}", e))?;
info!("📋 Registered sidecar process in registry"); info!("📋 Registered sidecar process in registry");
@@ -914,7 +912,8 @@ async fn spawn_agent_sidecar(
while let Some(event) = receiver.recv().await { while let Some(event) = receiver.recv().await {
match event { match event {
CommandEvent::Stdout(line) => { CommandEvent::Stdout(line_bytes) => {
let line = String::from_utf8_lossy(&line_bytes);
line_count += 1; line_count += 1;
// Log first output // Log first output
@@ -977,7 +976,8 @@ async fn spawn_agent_sidecar(
let _ = app_handle.emit(&format!("agent-output:{}", run_id), &line); let _ = app_handle.emit(&format!("agent-output:{}", run_id), &line);
let _ = app_handle.emit("agent-output", &line); let _ = app_handle.emit("agent-output", &line);
} }
CommandEvent::Stderr(line) => { CommandEvent::Stderr(line_bytes) => {
let line = String::from_utf8_lossy(&line_bytes);
error!("sidecar stderr: {}", line); error!("sidecar stderr: {}", line);
let _ = app_handle.emit(&format!("agent-error:{}", run_id), &line); let _ = app_handle.emit(&format!("agent-error:{}", run_id), &line);
let _ = app_handle.emit("agent-error", &line); let _ = app_handle.emit("agent-error", &line);
@@ -1822,9 +1822,9 @@ pub async fn set_claude_binary_path(db: State<'_, AgentDb>, path: String) -> Res
/// List all available Claude installations on the system /// List all available Claude installations on the system
#[tauri::command] #[tauri::command]
pub async fn list_claude_installations( pub async fn list_claude_installations(
app: AppHandle, _app: AppHandle,
) -> Result<Vec<crate::claude_binary::ClaudeInstallation>, String> { ) -> Result<Vec<crate::claude_binary::ClaudeInstallation>, String> {
let mut installations = crate::claude_binary::discover_claude_installations(); let installations = crate::claude_binary::discover_claude_installations();
if installations.is_empty() { if installations.is_empty() {
return Err("No Claude Code installations found on the system".to_string()); return Err("No Claude Code installations found on the system".to_string());