|
| 1 | +//! # System Prompt Slots — Customizing agent personality without overriding core behavior |
| 2 | +//! |
| 3 | +//! Demonstrates the slot-based system prompt customization API: |
| 4 | +//! 1. Create a session with custom role, guidelines, and response style |
| 5 | +//! 2. Send a prompt and verify the agent responds according to the custom persona |
| 6 | +//! 3. Show that core agentic capabilities (tool use, etc.) are preserved |
| 7 | +//! |
| 8 | +//! ```bash |
| 9 | +//! cd crates/code |
| 10 | +//! cargo run --example test_prompt_slots |
| 11 | +//! ``` |
| 12 | +
|
| 13 | +use a3s_code_core::{Agent, SessionOptions, SystemPromptSlots}; |
| 14 | +use std::path::PathBuf; |
| 15 | + |
| 16 | +fn find_config() -> PathBuf { |
| 17 | + if let Ok(p) = std::env::var("A3S_CONFIG") { |
| 18 | + return PathBuf::from(p); |
| 19 | + } |
| 20 | + let home = dirs::home_dir().expect("no home dir"); |
| 21 | + let home_cfg = home.join(".a3s/config.hcl"); |
| 22 | + if home_cfg.exists() { |
| 23 | + return home_cfg; |
| 24 | + } |
| 25 | + let project_cfg = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../.a3s/config.hcl"); |
| 26 | + if project_cfg.exists() { |
| 27 | + return project_cfg; |
| 28 | + } |
| 29 | + panic!("Config not found. Create ~/.a3s/config.hcl or set A3S_CONFIG"); |
| 30 | +} |
| 31 | + |
| 32 | +#[tokio::main] |
| 33 | +async fn main() -> anyhow::Result<()> { |
| 34 | + tracing_subscriber::fmt() |
| 35 | + .with_env_filter("a3s_code_core=info") |
| 36 | + .init(); |
| 37 | + |
| 38 | + let config = find_config(); |
| 39 | + println!("Config: {}", config.display()); |
| 40 | + |
| 41 | + let agent = Agent::new(config.to_str().unwrap()).await?; |
| 42 | + println!("Agent created ✓\n"); |
| 43 | + |
| 44 | + let workspace = tempfile::tempdir()?; |
| 45 | + |
| 46 | + // --- Test 1: Custom role only --- |
| 47 | + println!("═══ Test 1: Custom role ═══"); |
| 48 | + let opts = SessionOptions::new().with_prompt_slots(SystemPromptSlots { |
| 49 | + role: Some("You are a senior Rust developer who specializes in async programming.".into()), |
| 50 | + ..Default::default() |
| 51 | + }); |
| 52 | + let session = agent.session(workspace.path().to_str().unwrap(), Some(opts))?; |
| 53 | + let result = session |
| 54 | + .send("What is your area of expertise? Reply in one sentence.", None) |
| 55 | + .await?; |
| 56 | + println!("Response: {}\n", result.text.trim()); |
| 57 | + assert!(!result.text.is_empty(), "should get a response"); |
| 58 | + |
| 59 | + // --- Test 2: Role + guidelines + response style --- |
| 60 | + println!("═══ Test 2: Role + guidelines + response style ═══"); |
| 61 | + let opts = SessionOptions::new().with_prompt_slots(SystemPromptSlots { |
| 62 | + role: Some("You are a Python code reviewer.".into()), |
| 63 | + guidelines: Some("Always check for type hints. Flag any use of `eval()`.".into()), |
| 64 | + response_style: Some("Reply in bullet points. Be concise.".into()), |
| 65 | + ..Default::default() |
| 66 | + }); |
| 67 | + let session = agent.session(workspace.path().to_str().unwrap(), Some(opts))?; |
| 68 | + |
| 69 | + // Write a Python file for the agent to review |
| 70 | + std::fs::write( |
| 71 | + workspace.path().join("app.py"), |
| 72 | + r#" |
| 73 | +def add(a, b): |
| 74 | + return eval(f"{a} + {b}") |
| 75 | +
|
| 76 | +def greet(name): |
| 77 | + return "Hello " + name |
| 78 | +"#, |
| 79 | + )?; |
| 80 | + |
| 81 | + let result = session |
| 82 | + .send( |
| 83 | + "Review the file app.py and list any issues you find.", |
| 84 | + None, |
| 85 | + ) |
| 86 | + .await?; |
| 87 | + println!("Response:\n{}\n", result.text.trim()); |
| 88 | + assert!(result.tool_calls_count > 0, "should have used read_file tool"); |
| 89 | + |
| 90 | + // --- Test 3: Extra instructions only (backward compat style) --- |
| 91 | + println!("═══ Test 3: Extra instructions ═══"); |
| 92 | + let opts = SessionOptions::new().with_prompt_slots(SystemPromptSlots { |
| 93 | + extra: Some("Always end your response with '— A3S'".into()), |
| 94 | + ..Default::default() |
| 95 | + }); |
| 96 | + let session = agent.session(workspace.path().to_str().unwrap(), Some(opts))?; |
| 97 | + let result = session |
| 98 | + .send("Say hello.", None) |
| 99 | + .await?; |
| 100 | + println!("Response: {}\n", result.text.trim()); |
| 101 | + |
| 102 | + // --- Test 4: Verify tools still work (core behavior preserved) --- |
| 103 | + println!("═══ Test 4: Core tool behavior preserved ═══"); |
| 104 | + let opts = SessionOptions::new().with_prompt_slots(SystemPromptSlots { |
| 105 | + role: Some("You are a minimalist file manager.".into()), |
| 106 | + guidelines: Some("Only create files when explicitly asked.".into()), |
| 107 | + ..Default::default() |
| 108 | + }); |
| 109 | + let session = agent.session(workspace.path().to_str().unwrap(), Some(opts))?; |
| 110 | + let result = session |
| 111 | + .send( |
| 112 | + "Create a file called test.txt with the content 'prompt slots work'. Then read it back.", |
| 113 | + None, |
| 114 | + ) |
| 115 | + .await?; |
| 116 | + println!("Response: {}\n", result.text.trim()); |
| 117 | + assert!(result.tool_calls_count > 0, "should have used tools"); |
| 118 | + let test_file = workspace.path().join("test.txt"); |
| 119 | + assert!(test_file.exists(), "test.txt should exist"); |
| 120 | + |
| 121 | + println!("═══ All prompt slots tests passed ✓ ═══"); |
| 122 | + Ok(()) |
| 123 | +} |
0 commit comments