//! Guided (AI-assisted) installation. //! //! This is the only place a model response becomes configuration. The response //! is parsed into the closed `AiProposal` schema, reviewed with mandatory //! evidence and conflict detection, and applied atomically. The model never //! receives secrets and never reaches the executor. use std::path::Path; use phoebus_ai::provider::{ complete_proposal, complete_proposal_at, ModelTransport, ProviderKind, SystemFacts, }; use phoebus_ai::{parse_proposal, review, AiError, ReviewOutcome}; use phoebus_core::InstallConfig; /// Review a full chat-completions response body and apply the accepted changes. pub fn review_response( config: &mut InstallConfig, response_body: &str, ) -> Result { let content = phoebus_ai::provider::extract_content(response_body)?; let proposal = parse_proposal(&content)?; review(config, &proposal) } /// Review a bare proposal (already-extracted JSON) and apply it. pub fn review_proposal_text( config: &mut InstallConfig, text: &str, ) -> Result { let proposal = parse_proposal(text)?; review(config, &proposal) } /// Read a proposal from a file. The file may be a full chat response (as a /// provider returns) or a bare proposal JSON object. Review is atomic, so a /// failed first interpretation leaves the configuration untouched. pub fn review_file(config: &mut InstallConfig, path: &Path) -> Result { let text = std::fs::read_to_string(path) .map_err(|e| AiError::Provider(format!("cannot read {}: {e}", path.display())))?; if let Ok(outcome) = review_response(config, &text) { return Ok(outcome); } review_proposal_text(config, &text) } /// Ask a provider for a proposal and review it (fail-closed). pub fn generate_and_review( config: &mut InstallConfig, facts: &SystemFacts, provider: ProviderKind, model: &str, transport: &dyn ModelTransport, ) -> Result { let proposal = complete_proposal(provider, model, facts, transport)?; review(config, &proposal) } /// As `generate_and_review`, with an explicit endpoint (a self-hosted local /// model on a non-default address). pub fn generate_and_review_at( config: &mut InstallConfig, facts: &SystemFacts, provider: ProviderKind, endpoint: &str, model: &str, transport: &dyn ModelTransport, ) -> Result { let proposal = complete_proposal_at(provider, endpoint, model, facts, transport)?; review(config, &proposal) } #[cfg(test)] mod tests { use super::*; use phoebus_ai::provider::TestTransport; const PROPOSAL: &str = r#"{ "version": 1, "changes": [ {"action":"set_kernel_profile","value":"safe"}, {"action":"set_build_jobs","value":4} ], "rationale": [ {"action":"set_kernel_profile","reason":"stability over novelty","evidence":["proven boot uses gentoo-kernel-bin"]}, {"action":"set_build_jobs","reason":"16 GiB RAM","evidence":["memory_mib=16384"]} ] }"#; #[test] fn proposal_file_is_reviewed_and_applied() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("proposal.json"); std::fs::write(&path, PROPOSAL).unwrap(); let mut config = InstallConfig::default(); let outcome = review_file(&mut config, &path).unwrap(); assert_eq!(outcome.applied.len(), 2); assert_eq!(config.kernel.profile, phoebus_core::KernelProfile::Safe); assert_eq!(config.packages.build_jobs, Some(4)); } #[test] fn chat_response_is_unwrapped() { let transport = TestTransport::proposal(PROPOSAL); let mut config = InstallConfig::default(); let outcome = review_response(&mut config, &transport.response).unwrap(); assert_eq!(outcome.applied.len(), 2); } #[test] fn invalid_proposal_leaves_config_unchanged() { let bad = r#"{"version":1,"changes":[{"action":"set_hostname","value":"bad host"}],"rationale":[{"action":"set_hostname","reason":"x"}]}"#; let mut config = InstallConfig::default(); assert!(review_proposal_text(&mut config, bad).is_err()); assert_eq!(config.system.hostname, "phoebus"); } #[test] fn generate_uses_the_same_review_path() { use phoebus_core::hardware::HardwareReport; let facts = SystemFacts::from_system( &InstallConfig::default(), &HardwareReport::default(), &std::collections::HashMap::new(), true, None, ); let transport = TestTransport::proposal(PROPOSAL); let mut config = InstallConfig::default(); let outcome = generate_and_review(&mut config, &facts, ProviderKind::DeepSeek, "", &transport) .unwrap(); assert_eq!(outcome.applied.len(), 2); } }