diff --git a/src-tauri/src/models/agent.rs b/src-tauri/src/models/agent.rs index 87be266..fc5f5be 100644 --- a/src-tauri/src/models/agent.rs +++ b/src-tauri/src/models/agent.rs @@ -1,4 +1,4 @@ -use rusqlite::{params, Connection, OptionalExtension, Result}; +use rusqlite::{params, Connection, Result}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -142,12 +142,17 @@ impl Agent { } pub fn get_default_by_role(conn: &Connection, role: AgentRole) -> Result { + let default_id = match role { + AgentRole::Analyst => DEFAULT_ANALYST_AGENT_ID, + AgentRole::Developer => DEFAULT_DEVELOPER_AGENT_ID, + }; + conn.query_row( "SELECT id, name, role, tool, custom_prompt, is_default, created_at, updated_at FROM agents - WHERE role = ?1 AND is_default = 1 + WHERE id = ?1 AND role = ?2 AND is_default = 1 LIMIT 1", - params![role.as_str()], + params![default_id, role.as_str()], from_row, ) } @@ -198,31 +203,24 @@ impl Agent { )); } - let default_agent_id: String = conn - .query_row( - "SELECT id FROM agents WHERE role = ?1 AND is_default = 1 LIMIT 1", - params![agent.role.as_str()], - |row| row.get(0), - ) - .optional()? - .ok_or_else(|| { - rusqlite::Error::InvalidParameterName(format!( - "No default agent found for role '{}'", - agent.role.as_str() - )) - })?; + let default_agent = Self::get_default_by_role(conn, agent.role.clone()).map_err(|_| { + rusqlite::Error::InvalidParameterName(format!( + "No default agent found for role '{}'", + agent.role.as_str() + )) + })?; match agent.role { AgentRole::Analyst => { conn.execute( "UPDATE watched_trackers SET analyst_agent_id = ?1 WHERE analyst_agent_id = ?2", - params![default_agent_id, id], + params![default_agent.id, id], )?; } AgentRole::Developer => { conn.execute( "UPDATE watched_trackers SET developer_agent_id = ?1 WHERE developer_agent_id = ?2", - params![default_agent_id, id], + params![default_agent.id, id], )?; } }