Skip to main content

mockforge_intelligence/ai_studio/
persona_generator.rs

1//! AI-powered persona generator
2//!
3//! This module provides functionality to generate and tweak personas using AI.
4//! It creates personas with realistic traits, backstories, and lifecycle configurations
5//! based on natural language descriptions.
6
7use crate::ai_studio::artifact_freezer::{ArtifactFreezer, FreezeMetadata};
8use crate::ai_studio::config::DeterministicModeConfig;
9use crate::intelligent_behavior::llm_client::LlmClient;
10use crate::intelligent_behavior::types::LlmGenerationRequest;
11use crate::intelligent_behavior::IntelligentBehaviorConfig;
12use mockforge_foundation::Result;
13use serde::{Deserialize, Serialize};
14use sha2::{Digest, Sha256};
15use std::collections::hash_map::DefaultHasher;
16use std::collections::HashMap;
17use std::hash::{Hash, Hasher};
18
19/// Persona generator for creating personas from descriptions
20pub struct PersonaGenerator {
21    /// LLM client for generating persona details
22    llm_client: LlmClient,
23    /// Configuration (for accessing LLM provider/model info)
24    config: IntelligentBehaviorConfig,
25}
26
27impl PersonaGenerator {
28    /// Create a new persona generator with default configuration
29    pub fn new() -> Self {
30        let config = IntelligentBehaviorConfig::default();
31        Self {
32            llm_client: LlmClient::new(config.behavior_model.clone()),
33            config,
34        }
35    }
36
37    /// Create a new persona generator with custom configuration
38    pub fn with_config(config: IntelligentBehaviorConfig) -> Self {
39        Self {
40            llm_client: LlmClient::new(config.behavior_model.clone()),
41            config,
42        }
43    }
44
45    /// Generate a persona from natural language description
46    ///
47    /// This method uses AI to generate a complete persona profile including:
48    /// - Realistic traits based on the description
49    /// - A narrative backstory
50    /// - Appropriate lifecycle configuration
51    /// - Domain-specific characteristics
52    ///
53    /// In deterministic mode (ai_mode = generate_once_freeze), this method will
54    /// first check for frozen artifacts before generating new ones.
55    pub async fn generate(
56        &self,
57        request: &PersonaGenerationRequest,
58        ai_mode: Option<crate::ai_studio::config::AiMode>,
59        deterministic_config: Option<&DeterministicModeConfig>,
60    ) -> Result<PersonaGenerationResponse> {
61        // In deterministic mode, check for frozen artifacts first
62        if ai_mode == Some(crate::ai_studio::config::AiMode::GenerateOnceFreeze) {
63            let freezer = ArtifactFreezer::new();
64
65            // Create identifier from description hash
66            let mut hasher = DefaultHasher::new();
67            request.description.hash(&mut hasher);
68            let description_hash = format!("{:x}", hasher.finish());
69
70            // Try to load frozen artifact
71            if let Some(frozen) = freezer.load_frozen("persona", Some(&description_hash)).await? {
72                // Extract persona from frozen content (remove metadata)
73                let mut persona = frozen.content.clone();
74                if let Some(obj) = persona.as_object_mut() {
75                    obj.remove("_frozen_metadata");
76                }
77
78                return Ok(PersonaGenerationResponse {
79                    persona: Some(persona),
80                    message: format!(
81                        "Loaded frozen persona artifact from {} (deterministic mode)",
82                        frozen.path
83                    ),
84                    frozen_artifact: Some(frozen),
85                });
86            }
87        }
88        // Build system prompt for persona generation
89        let system_prompt = r#"You are an expert at creating realistic user personas for API testing.
90Generate a complete persona profile from a natural language description.
91
92For the persona, provide:
931. A unique ID (e.g., "user:premium-001", "customer:churned-002")
942. A descriptive name
953. A business domain (e.g., "ecommerce", "saas", "banking", "healthcare")
964. Realistic traits as key-value pairs (e.g., "subscription_tier": "premium", "spending_level": "high")
975. A narrative backstory explaining the persona's characteristics
986. Optional lifecycle state (e.g., "active", "trial", "churned", "premium")
99
100Return your response as a JSON object with this structure:
101{
102  "id": "string (unique persona ID)",
103  "name": "string (descriptive name)",
104  "domain": "string (business domain)",
105  "traits": {
106    "trait_name": "trait_value",
107    ...
108  },
109  "backstory": "string (narrative description)",
110  "lifecycle_state": "string (optional, e.g., active, trial, churned)",
111  "metadata": {
112    "additional": "metadata fields"
113  }
114}
115
116Make the persona realistic and consistent. Traits should align with the description."#;
117
118        let user_prompt =
119            format!("Generate a persona from this description:\n\n{}", request.description);
120
121        let llm_request = LlmGenerationRequest {
122            system_prompt: system_prompt.to_string(),
123            user_prompt,
124            temperature: 0.7, // Higher temperature for more creative personas
125            max_tokens: 1500,
126            schema: None,
127        };
128
129        // Generate persona from LLM
130        let response = self.llm_client.generate(&llm_request).await?;
131
132        // Parse the response into a persona structure
133        let persona_json = if let Some(_id) = response.get("id") {
134            // Full persona structure
135            response.clone()
136        } else {
137            // Fallback: create a basic persona structure
138            let uuid_str = uuid::Uuid::new_v4().to_string();
139            let short_id = uuid_str.split('-').next().unwrap_or("generated");
140            serde_json::json!({
141                "id": format!("user:generated-{}", short_id),
142                "name": response.get("name").and_then(|v| v.as_str()).unwrap_or("Generated Persona"),
143                "domain": response.get("domain").and_then(|v| v.as_str()).unwrap_or("general"),
144                "traits": response.get("traits").cloned().unwrap_or_else(|| serde_json::json!({})),
145                "backstory": response.get("backstory").and_then(|v| v.as_str()).unwrap_or("AI-generated persona"),
146                "lifecycle_state": response.get("lifecycle_state").and_then(|v| v.as_str()).unwrap_or("active"),
147            })
148        };
149
150        // Convert to the simpler Persona format for response
151        let persona_name = persona_json
152            .get("name")
153            .and_then(|v| v.as_str())
154            .unwrap_or("Generated Persona")
155            .to_string();
156
157        let traits: HashMap<String, String> = persona_json
158            .get("traits")
159            .and_then(|v| v.as_object())
160            .map(|obj| {
161                obj.iter()
162                    .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
163                    .collect()
164            })
165            .unwrap_or_default();
166
167        // Build response persona (using the simpler Persona struct format)
168        let persona_value = serde_json::json!({
169            "name": persona_name,
170            "traits": traits,
171            "id": persona_json.get("id"),
172            "domain": persona_json.get("domain"),
173            "backstory": persona_json.get("backstory"),
174            "lifecycle_state": persona_json.get("lifecycle_state"),
175        });
176
177        // Auto-freeze if enabled
178        let frozen_artifact = if let Some(config) = deterministic_config {
179            if config.enabled && config.is_auto_freeze_enabled() {
180                let freezer = ArtifactFreezer::new();
181
182                // Calculate prompt hash
183                let mut hasher = Sha256::new();
184                hasher.update(request.description.as_bytes());
185                let prompt_hash = format!("{:x}", hasher.finalize());
186
187                // Create metadata
188                let metadata = if config.track_metadata {
189                    Some(FreezeMetadata {
190                        llm_provider: Some(self.config.behavior_model.llm_provider.clone()),
191                        llm_model: Some(self.config.behavior_model.model.clone()),
192                        llm_version: None,
193                        prompt_hash: Some(prompt_hash),
194                        output_hash: None, // Will be calculated by freezer
195                        original_prompt: Some(request.description.clone()),
196                    })
197                } else {
198                    None
199                };
200
201                let freeze_request = crate::ai_studio::artifact_freezer::FreezeRequest {
202                    artifact_type: "persona".to_string(),
203                    content: persona_value.clone(),
204                    format: config.freeze_format.clone(),
205                    path: None,
206                    metadata,
207                };
208
209                freezer.auto_freeze_if_enabled(&freeze_request, config).await?
210            } else {
211                None
212            }
213        } else {
214            None
215        };
216
217        Ok(PersonaGenerationResponse {
218            persona: Some(persona_value),
219            message: format!(
220                "Successfully generated persona '{}' with {} traits{}",
221                persona_name,
222                traits.len(),
223                if frozen_artifact.is_some() {
224                    " (auto-frozen)"
225                } else {
226                    ""
227                }
228            ),
229            frozen_artifact,
230        })
231    }
232
233    /// Tweak an existing persona based on a description
234    ///
235    /// This method modifies an existing persona by adjusting traits, adding new ones,
236    /// or updating the backstory based on the provided description.
237    pub async fn tweak(
238        &self,
239        base_persona: &serde_json::Value,
240        description: &str,
241    ) -> Result<PersonaGenerationResponse> {
242        // Build system prompt for persona tweaking
243        let system_prompt = r#"You are an expert at modifying user personas for API testing.
244Given an existing persona and a description of desired changes, update the persona accordingly.
245
246You can:
247- Modify existing traits
248- Add new traits
249- Update the backstory
250- Change lifecycle state
251- Adjust domain if needed
252
253Return the updated persona in the same JSON structure as the input."#;
254
255        let user_prompt = format!(
256            "Base persona:\n{}\n\nDesired changes: {}\n\nProvide the updated persona.",
257            serde_json::to_string_pretty(base_persona)?,
258            description
259        );
260
261        let llm_request = LlmGenerationRequest {
262            system_prompt: system_prompt.to_string(),
263            user_prompt,
264            temperature: 0.5,
265            max_tokens: 1500,
266            schema: None,
267        };
268
269        // Generate updated persona
270        let response = self.llm_client.generate(&llm_request).await?;
271
272        Ok(PersonaGenerationResponse {
273            persona: Some(response),
274            message: "Successfully updated persona".to_string(),
275            frozen_artifact: None,
276        })
277    }
278}
279
280impl Default for PersonaGenerator {
281    fn default() -> Self {
282        Self::new()
283    }
284}
285
286/// Request for persona generation
287#[derive(Debug, Clone, Serialize, Deserialize)]
288pub struct PersonaGenerationRequest {
289    /// Natural language description
290    pub description: String,
291
292    /// Optional base persona to tweak
293    pub base_persona_id: Option<String>,
294
295    /// Workspace ID for context
296    pub workspace_id: Option<String>,
297}
298
299/// Response from persona generation
300#[derive(Debug, Clone, Serialize, Deserialize)]
301pub struct PersonaGenerationResponse {
302    /// Generated persona (if any)
303    pub persona: Option<serde_json::Value>,
304
305    /// Status message
306    pub message: String,
307
308    /// Frozen artifact (if auto-freeze was enabled)
309    #[serde(skip_serializing_if = "Option::is_none")]
310    pub frozen_artifact: Option<crate::ai_studio::artifact_freezer::FrozenArtifact>,
311}