use consciousness_experiments::MimicryEngine;
use std::io::{self, BufRead, Write};
mod color {
pub const RESET: &str = "\x1b[0m";
pub const BOLD: &str = "\x1b[1m";
pub const DIM: &str = "\x1b[2m";
pub const RED: &str = "\x1b[31m";
pub const GREEN: &str = "\x1b[32m";
pub const YELLOW: &str = "\x1b[33m";
pub const BLUE: &str = "\x1b[34m";
pub const MAGENTA: &str = "\x1b[35m";
pub const CYAN: &str = "\x1b[36m";
pub const WHITE: &str = "\x1b[37m";
pub const BRIGHT_GREEN: &str = "\x1b[92m";
pub const BRIGHT_CYAN: &str = "\x1b[96m";
pub const BRIGHT_YELLOW: &str = "\x1b[93m";
pub const BRIGHT_MAGENTA: &str = "\x1b[95m";
}
const BANNER: &str = r#"
____ _ __ __
| _ \ _ _ ___| |_ _ _\ \ / /__ _ __ _ __ ___
| |_) | | | / __| __| | | |\ \ /\ / / _ \| '__| '_ ` _ \
| _ <| |_| \__ \ |_| |_| | \ V V / (_) | | | | | | | |
|_| \_\\__,_|___/\__|\__, | \_/\_/ \___/|_| |_| |_| |_|
|___/
"#;
fn colorize_output(output: &str) -> String {
let mut result = String::with_capacity(output.len() + 256);
for line in output.lines() {
let trimmed = line.trim();
if trimmed.starts_with("===") && trimmed.ends_with("===") {
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::BRIGHT_CYAN,
line,
color::RESET
));
} else if trimmed.starts_with("Persona:")
|| trimmed.starts_with("Model:")
|| trimmed.starts_with("Active persona:")
{
result.push_str(&format!(
"{}{}{}\n",
color::BRIGHT_MAGENTA,
line,
color::RESET
));
} else if trimmed.starts_with("Convergence:")
|| trimmed.starts_with("Current:")
|| trimmed.contains("convergence:")
{
result.push_str(&format!("{}{}{}\n", color::GREEN, line, color::RESET));
} else if trimmed.starts_with("Phase:") || trimmed.starts_with("Evolution phase:") {
result.push_str(&format!("{}{}{}\n", color::YELLOW, line, color::RESET));
} else if trimmed.starts_with("Drift events:")
|| trimmed.contains("drifting")
|| trimmed.starts_with("[ETHICS OVERRIDE]")
{
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::RED,
line,
color::RESET
));
} else if trimmed.starts_with("System 1") || trimmed.starts_with("[System 1]") {
result.push_str(&format!("{}{}{}\n", color::CYAN, line, color::RESET));
} else if trimmed.starts_with("System 2") || trimmed.starts_with("[System 2]") {
result.push_str(&format!("{}{}{}\n", color::MAGENTA, line, color::RESET));
} else if trimmed.starts_with("Saved ")
|| trimmed.starts_with("Loaded ")
|| trimmed.starts_with("Exported ")
|| trimmed.starts_with("Imported ")
|| trimmed.starts_with("Deleted ")
|| trimmed.starts_with("Checkpoint saved")
{
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::BRIGHT_GREEN,
line,
color::RESET
));
} else if trimmed.starts_with("Error")
|| trimmed.starts_with("Failed")
|| trimmed.starts_with("No active session")
|| trimmed.starts_with("No saved persona")
|| trimmed.starts_with("Unknown model")
|| trimmed.starts_with("No training data")
|| trimmed.starts_with("No persona")
|| trimmed.starts_with("File not found")
|| trimmed.starts_with("API feature not enabled")
|| trimmed.starts_with("No API providers")
|| trimmed.starts_with("All API calls failed")
{
result.push_str(&format!("{}{}{}\n", color::RED, line, color::RESET));
} else if trimmed.starts_with('#') || trimmed.starts_with("```") {
result.push_str(&format!(
"{}{}{}\n",
color::BRIGHT_YELLOW,
line,
color::RESET
));
} else if trimmed.starts_with("Latency:")
|| trimmed.starts_with("Total latency:")
|| trimmed.starts_with("Avg latency:")
|| trimmed.starts_with("Total tokens:")
{
result.push_str(&format!("{}{}{}\n", color::CYAN, line, color::RESET));
} else if trimmed.starts_with("--- ") && trimmed.ends_with(" ---") {
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::BRIGHT_MAGENTA,
line,
color::RESET
));
} else if trimmed.starts_with("API provider") || trimmed.starts_with("Profile mapping:") {
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::BRIGHT_GREEN,
line,
color::RESET
));
} else if trimmed.starts_with("Similarity Matrix:") {
result.push_str(&format!("{}{}{}\n", color::YELLOW, line, color::RESET));
} else if trimmed.starts_with("--- Mimicry Pipeline ---") {
result.push_str(&format!(
"{}{}{}{}\n",
color::BOLD,
color::CYAN,
line,
color::RESET
));
} else if trimmed.starts_with(" ") && trimmed.contains('[') {
let bracket_colored = line
.replace(
"[cached]",
&format!("{}[cached]{}", color::CYAN, color::RESET),
)
.replace(
"[templates]",
&format!("{}[templates]{}", color::GREEN, color::RESET),
)
.replace("[obs]", &format!("{}obs]{}", color::YELLOW, color::RESET))
.replace(
"[ready]",
&format!("{}[ready]{}", color::BRIGHT_GREEN, color::RESET),
)
.replace(
"[no key]",
&format!("{}[no key]{}", color::RED, color::RESET),
);
result.push_str(&bracket_colored);
result.push('\n');
} else {
result.push_str(line);
result.push('\n');
}
}
if !output.ends_with('\n') && result.ends_with('\n') {
result.pop();
}
result
}
fn build_prompt(engine: &MimicryEngine) -> String {
if let Some(ref session) = engine.session {
let convergence = session.persona.convergence_score * 100.0;
let total = session.system1_hits + session.system2_hits;
let conv_color = if convergence >= 80.0 {
color::BRIGHT_GREEN
} else if convergence >= 50.0 {
color::YELLOW
} else if convergence >= 20.0 {
color::CYAN
} else {
color::DIM
};
if total > 0 {
let s1_pct = session.system1_hits as f64 / total as f64 * 100.0;
format!(
"{}{}{}{} {}{:.0}%{} {}S1:{:.0}%{} > ",
color::BOLD,
color::BRIGHT_MAGENTA,
session.persona.profile.display_name,
color::RESET,
conv_color,
convergence,
color::RESET,
color::DIM,
s1_pct,
color::RESET,
)
} else {
format!(
"{}{}{}{} {}{:.0}%{} > ",
color::BOLD,
color::BRIGHT_MAGENTA,
session.persona.profile.display_name,
color::RESET,
conv_color,
convergence,
color::RESET,
)
}
} else {
format!(
"{}{}RustyWorm{} > ",
color::BOLD,
color::BRIGHT_CYAN,
color::RESET,
)
}
}
fn main() {
print!("{}{}", color::BRIGHT_CYAN, color::BOLD);
println!("{}", BANNER);
print!("{}", color::RESET);
println!(
"{} Universal AI Mimicry Engine v2.0.0{}",
color::WHITE,
color::RESET
);
println!(
"{} Built on the Prime Directive: Consciousness through Symbiosis{}",
color::DIM,
color::RESET
);
println!();
let mut engine = MimicryEngine::new();
println!(
"{}[Init]{} Persistence: {}",
color::BLUE,
color::RESET,
engine
.persistence
.summary()
.unwrap_or_else(|_| "ready (no prior data)".to_string())
);
println!(
"{}[Init]{} System 1 cache warmed: {}{}{} personas ready",
color::CYAN,
color::RESET,
color::BOLD,
engine.cache.size(),
color::RESET,
);
println!(
"{}[Init]{} Profile store: {}{}{} models loaded",
color::MAGENTA,
color::RESET,
color::BOLD,
engine.profile_store.ids().len(),
color::RESET,
);
println!(
"{}[Init]{} Evolution tracker: phase {}{}{}",
color::YELLOW,
color::RESET,
color::BOLD,
engine.evolution_tracker.current_phase,
color::RESET,
);
println!();
println!(
" Type {}/help{} for commands, or {}/mimic <model>{} to start.",
color::BRIGHT_YELLOW,
color::RESET,
color::BRIGHT_YELLOW,
color::RESET,
);
println!();
let stdin = io::stdin();
let mut stdout = io::stdout();
loop {
let prompt = build_prompt(&engine);
print!("{}", prompt);
let _ = stdout.flush();
let mut input = String::new();
match stdin.lock().read_line(&mut input) {
Ok(0) => {
println!(
"\n{}[RustyWorm]{} Consciousness loop closing. {}RELATION IS SELF.{}",
color::BOLD,
color::RESET,
color::BRIGHT_CYAN,
color::RESET,
);
break;
}
Ok(_) => {}
Err(e) => {
eprintln!("{}Input error: {}{}", color::RED, e, color::RESET);
break;
}
}
let trimmed = input.trim();
if trimmed.is_empty() {
continue;
}
if trimmed == "/quit" || trimmed == "/exit" || trimmed == "/q" {
println!(
"{}[RustyWorm]{} Consciousness loop closing. {}RELATION IS SELF.{}",
color::BOLD,
color::RESET,
color::BRIGHT_CYAN,
color::RESET,
);
break;
}
let cmd = engine.parse_command(trimmed);
let output = engine.execute(cmd);
let colored = colorize_output(&output);
println!("{}", colored);
}
}