Skip to main content

mockforge_foundation/intelligent_behavior/
mod.rs

1//! Foundational types for intelligent behavior
2//!
3//! These types are shared between `mockforge-core` (which defines the richer
4//! behavior rules, state machines, and MockAI implementation) and consumers
5//! that only need the base request/response types and personas.
6//!
7//! Kept minimal: only pure data with no cross-crate dependencies.
8
9pub mod config;
10pub mod mockai;
11pub mod rule_types;
12pub mod session;
13pub mod session_state;
14pub mod types;
15
16pub use config::{
17    BehaviorModelConfig, IntelligentBehaviorConfig, PerformanceConfig, PersonasConfig,
18    VectorStoreConfig,
19};
20pub use mockai::MockAiBehavior;
21pub use session::{SessionManager, SessionTracking, SessionTrackingMethod};
22pub use session_state::{InteractionRecord, SessionState};
23pub use types::BehaviorRules;
24
25use chrono::{DateTime, Utc};
26use serde::{Deserialize, Serialize};
27use serde_json::Value;
28use std::collections::HashMap;
29
30/// A persona defines consistent data patterns across endpoints
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
33pub struct Persona {
34    /// Persona name (e.g., "commercial_midwest", "hobbyist_urban")
35    pub name: String,
36
37    /// Persona traits (key-value pairs, e.g., "apiary_count": "20-40", "hive_count": "800-1500")
38    #[serde(default)]
39    pub traits: HashMap<String, String>,
40}
41
42impl Persona {
43    /// Get a numeric trait value, parsing ranges like "20-40" or single values.
44    /// Returns the midpoint for ranges, or the value for single numbers.
45    pub fn get_numeric_trait(&self, key: &str) -> Option<u64> {
46        self.traits.get(key).and_then(|value| {
47            if let Some((min_str, max_str)) = value.split_once('-') {
48                if let (Ok(min), Ok(max)) =
49                    (min_str.trim().parse::<u64>(), max_str.trim().parse::<u64>())
50                {
51                    return Some((min + max) / 2);
52                }
53            }
54            value.parse::<u64>().ok()
55        })
56    }
57
58    /// Get a trait value as string.
59    pub fn get_trait(&self, key: &str) -> Option<&String> {
60        self.traits.get(key)
61    }
62}
63
64/// LLM generation request — passed to `LlmClient::generate`.
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct LlmGenerationRequest {
67    /// System prompt (instructions to the model).
68    pub system_prompt: String,
69    /// User prompt (constructed from request context).
70    pub user_prompt: String,
71    /// Sampling temperature (0.0–2.0).
72    #[serde(default = "default_temperature")]
73    pub temperature: f64,
74    /// Maximum tokens to generate.
75    #[serde(default = "default_max_tokens")]
76    pub max_tokens: usize,
77    /// Expected response schema (JSON Schema).
78    pub schema: Option<Value>,
79}
80
81impl LlmGenerationRequest {
82    /// Create a new LLM generation request.
83    pub fn new(system_prompt: impl Into<String>, user_prompt: impl Into<String>) -> Self {
84        Self {
85            system_prompt: system_prompt.into(),
86            user_prompt: user_prompt.into(),
87            temperature: default_temperature(),
88            max_tokens: default_max_tokens(),
89            schema: None,
90        }
91    }
92
93    /// Set temperature.
94    #[must_use]
95    pub fn with_temperature(mut self, temperature: f64) -> Self {
96        self.temperature = temperature;
97        self
98    }
99
100    /// Set max tokens.
101    #[must_use]
102    pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
103        self.max_tokens = max_tokens;
104        self
105    }
106
107    /// Set expected schema.
108    #[must_use]
109    pub fn with_schema(mut self, schema: Value) -> Self {
110        self.schema = Some(schema);
111        self
112    }
113}
114
115fn default_temperature() -> f64 {
116    0.7
117}
118
119fn default_max_tokens() -> usize {
120    1024
121}
122
123/// HTTP request for MockAI processing.
124#[derive(Debug, Clone)]
125pub struct Request {
126    /// HTTP method.
127    pub method: String,
128    /// Request path.
129    pub path: String,
130    /// Request body.
131    pub body: Option<Value>,
132    /// Query parameters.
133    pub query_params: HashMap<String, String>,
134    /// Headers.
135    pub headers: HashMap<String, String>,
136}
137
138/// HTTP response from MockAI.
139#[derive(Debug, Clone)]
140pub struct Response {
141    /// HTTP status code.
142    pub status_code: u16,
143    /// Response body.
144    pub body: Value,
145    /// Response headers.
146    pub headers: HashMap<String, String>,
147}
148
149/// Captured HTTP request/response exchange used for behavioral analysis.
150#[derive(Debug, Clone)]
151pub struct HttpExchange {
152    /// HTTP method.
153    pub method: String,
154    /// Request path.
155    pub path: String,
156    /// Query parameters (raw query string).
157    pub query_params: Option<String>,
158    /// Request headers (JSON string).
159    pub headers: String,
160    /// Request body (optional).
161    pub body: Option<String>,
162    /// Request body encoding.
163    pub body_encoding: String,
164    /// Response status code.
165    pub status_code: Option<i32>,
166    /// Response headers (JSON string).
167    pub response_headers: Option<String>,
168    /// Response body (optional).
169    pub response_body: Option<String>,
170    /// Response body encoding.
171    pub response_body_encoding: Option<String>,
172    /// Timestamp.
173    pub timestamp: DateTime<Utc>,
174}