Persist provider name and model config in the session (#5419)

This commit is contained in:
Will Pfleger
2025-11-20 15:55:32 -05:00
committed by GitHub
parent f4724cbf23
commit 682e315be8
18 changed files with 311 additions and 106 deletions
+9 -7
View File
@@ -230,18 +230,22 @@ impl GooseAcpAgent {
};
let provider = create(&provider_name, model_config).await?;
// Create a shared agent instance
let agent = Agent::new();
agent.update_provider(provider.clone()).await?;
let session = SessionManager::create_session(
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
"ACP Session".to_string(),
SessionType::Hidden,
)
.await?;
let agent = Agent::new();
agent.update_provider(provider.clone(), &session.id).await?;
// Load and add extensions just like the normal CLI
let extensions_to_run: Vec<_> = get_all_extensions()
.into_iter()
.filter(|ext| ext.enabled)
.map(|ext| ext.config)
.collect();
// Add extensions to the agent in parallel
let agent_ptr = Arc::new(agent);
let mut set = JoinSet::new();
let mut waiting_on = HashSet::new();
@@ -257,7 +261,6 @@ impl GooseAcpAgent {
});
}
// Wait for all extensions to load
while let Some(result) = set.join_next().await {
match result {
Ok((name, Ok(_))) => {
@@ -274,7 +277,6 @@ impl GooseAcpAgent {
}
}
// Unwrap the Arc to get the agent back
let agent = Arc::try_unwrap(agent_ptr)
.map_err(|_| anyhow::anyhow!("Failed to unwrap agent Arc"))?;
+9 -5
View File
@@ -21,6 +21,7 @@ use goose::conversation::message::Message;
use goose::model::ModelConfig;
use goose::providers::provider_test::test_provider_configuration;
use goose::providers::{create, providers};
use goose::session::{SessionManager, SessionType};
use serde_json::Value;
use std::collections::HashMap;
@@ -1368,7 +1369,6 @@ pub async fn configure_tool_permissions_dialog() -> anyhow::Result<()> {
.collect();
extensions.push("platform".to_string());
// Sort extensions alphabetically by name
extensions.sort();
let selected_extension_name = cliclack::select("Choose an extension to configure tools")
@@ -1380,8 +1380,6 @@ pub async fn configure_tool_permissions_dialog() -> anyhow::Result<()> {
)
.interact()?;
// Fetch tools for the selected extension
// Load config and get provider/model
let config = Config::global();
let provider_name: String = config
@@ -1393,10 +1391,16 @@ pub async fn configure_tool_permissions_dialog() -> anyhow::Result<()> {
.expect("No model configured. Please set model first");
let model_config = ModelConfig::new(&model)?;
// Create the agent
let session = SessionManager::create_session(
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
"Tool Permission Configuration".to_string(),
SessionType::Hidden,
)
.await?;
let agent = Agent::new();
let new_provider = create(&provider_name, model_config).await?;
agent.update_provider(new_provider).await?;
agent.update_provider(new_provider, &session.id).await?;
if let Some(config) = get_extension_by_name(&selected_extension_name) {
agent
.add_extension(config.clone())
+8 -4
View File
@@ -158,12 +158,17 @@ pub async fn handle_web(
let model_config = goose::model::ModelConfig::new(&model)?;
// Create the agent
let init_session = SessionManager::create_session(
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")),
"Web Agent Initialization".to_string(),
SessionType::Hidden,
)
.await?;
let agent = Agent::new();
let provider = goose::providers::create(&provider_name, model_config).await?;
agent.update_provider(provider).await?;
agent.update_provider(provider, &init_session.id).await?;
// Load and enable extensions from config
let enabled_configs = goose::config::get_enabled_extensions();
for config in enabled_configs {
if let Err(e) = agent.add_extension(config.clone()).await {
@@ -177,7 +182,6 @@ pub async fn handle_web(
auth_token,
};
// Build router
let app = Router::new()
.route("/", get(serve_index))
.route("/session/{session_name}", get(serve_session))
@@ -217,16 +217,20 @@ where
)
.await;
agent
.update_provider(provider_arc as Arc<dyn goose::providers::base::Provider>)
.await?;
let session = SessionManager::create_session(
PathBuf::default(),
"scenario-runner".to_string(),
SessionType::Hidden,
)
.await?;
agent
.update_provider(
provider_arc as Arc<dyn goose::providers::base::Provider>,
&session.id,
)
.await?;
let mut cli_session = CliSession::new(
agent,
session.id,
+51 -27
View File
@@ -149,7 +149,15 @@ async fn offer_extension_debugging_help(
// Create a minimal agent for debugging
let debug_agent = Agent::new();
debug_agent.update_provider(provider).await?;
let session = SessionManager::create_session(
std::env::current_dir()?,
"CLI Session".to_string(),
SessionType::Hidden,
)
.await?;
debug_agent.update_provider(provider, &session.id).await?;
// Add the developer extension if available to help with debugging
let extensions = get_all_extensions();
@@ -166,12 +174,6 @@ async fn offer_extension_debugging_help(
}
}
let session = SessionManager::create_session(
std::env::current_dir()?,
"CLI Session".to_string(),
SessionType::Hidden,
)
.await?;
let mut debug_session = CliSession::new(
debug_agent,
session.id,
@@ -246,11 +248,24 @@ pub struct SessionSettings {
}
pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
// Load config and get provider/model
let config = Config::global();
let (saved_provider, saved_model_config) = if session_config.resume {
if let Some(ref session_id) = session_config.session_id {
match SessionManager::get_session(session_id, false).await {
Ok(session_data) => (session_data.provider_name, session_data.model_config),
Err(_) => (None, None),
}
} else {
(None, None)
}
} else {
(None, None)
};
let provider_name = session_config
.provider
.or(saved_provider)
.or_else(|| {
session_config
.settings
@@ -262,6 +277,7 @@ pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
let model_name = session_config
.model
.or_else(|| saved_model_config.as_ref().map(|mc| mc.model_name.clone()))
.or_else(|| {
session_config
.settings
@@ -271,16 +287,26 @@ pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
.or_else(|| config.get_goose_model().ok())
.expect("No model configured. Run 'goose configure' first");
let temperature = session_config.settings.as_ref().and_then(|s| s.temperature);
let model_config = if session_config.resume
&& saved_model_config
.as_ref()
.is_some_and(|mc| mc.model_name == model_name)
{
let mut config = saved_model_config.unwrap();
if let Some(temp) = session_config.settings.as_ref().and_then(|s| s.temperature) {
config = config.with_temperature(Some(temp));
}
config
} else {
let temperature = session_config.settings.as_ref().and_then(|s| s.temperature);
goose::model::ModelConfig::new(&model_name)
.unwrap_or_else(|e| {
output::render_error(&format!("Failed to create model configuration: {}", e));
process::exit(1);
})
.with_temperature(temperature)
};
let model_config = goose::model::ModelConfig::new(&model_name)
.unwrap_or_else(|e| {
output::render_error(&format!("Failed to create model configuration: {}", e));
process::exit(1);
})
.with_temperature(temperature);
// Create the agent
let agent: Agent = Agent::new();
agent
@@ -304,10 +330,8 @@ pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
process::exit(1);
}
};
// Keep a reference to the provider for display_session_info
let provider_for_display = Arc::clone(&new_provider);
// Log model information at startup
if let Some(lead_worker) = new_provider.as_lead_worker() {
let (lead_model, worker_model) = lead_worker.get_model_info();
tracing::info!(
@@ -319,14 +343,6 @@ pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
tracing::info!("🤖 Using model: {}", model_name);
}
agent
.update_provider(new_provider)
.await
.unwrap_or_else(|e| {
output::render_error(&format!("Failed to initialize agent: {}", e));
process::exit(1);
});
let session_id: String = if session_config.no_session {
let working_dir = std::env::current_dir().expect("Could not get working directory");
let session = SessionManager::create_session(
@@ -362,6 +378,14 @@ pub async fn build_session(session_config: SessionBuilderConfig) -> CliSession {
session_config.session_id.unwrap()
};
agent
.update_provider(new_provider, &session_id)
.await
.unwrap_or_else(|e| {
output::render_error(&format!("Failed to initialize agent: {}", e));
process::exit(1);
});
agent
.extension_manager
.set_context(PlatformExtensionContext {