phoebus_os/installer-rs/crates/phoebus-installer/src/ai_guide.rs
Phoebus b45e61e361 feat(ai): guided AI optimization with DeepSeek/OpenAI providers
Add an OpenAI-compatible provider layer (DeepSeek, OpenAI and a test
transport) that sends only bounded system facts and injects the API key via a
curl config on stdin, never in argv, config or logs. Extend the typed proposal
schema with scoped package.use, build jobs and binary-package preference; every
change now needs a non-empty reason and duplicates are rejected. Wire
--ai/--ai-proposal-file/--ai-facts into the CLI and add provider selection to
the TUI. Deterministic validation and review are unchanged: the model never
reaches the executor and the no-AI path keeps working.
2026-09-20 04:47:11 -07:00

119 lines
4.4 KiB
Rust

//! 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, 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<ReviewOutcome, AiError> {
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<ReviewOutcome, AiError> {
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<ReviewOutcome, AiError> {
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<ReviewOutcome, AiError> {
let proposal = complete_proposal(provider, 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);
}
}