1use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::HashMap;
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
12#[serde(rename_all = "lowercase")]
13pub enum AiResponseMode {
14 Static,
16 Intelligent,
18 Hybrid,
20}
21
22impl Default for AiResponseMode {
23 fn default() -> Self {
24 Self::Static
25 }
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct AiResponseConfig {
32 #[serde(default)]
34 pub enabled: bool,
35
36 #[serde(default)]
38 pub mode: AiResponseMode,
39
40 pub prompt: Option<String>,
43
44 pub context: Option<String>,
46
47 #[serde(default = "default_temperature")]
49 pub temperature: f32,
50
51 #[serde(default = "default_max_tokens")]
53 pub max_tokens: usize,
54
55 pub schema: Option<Value>,
57
58 #[serde(default = "default_true")]
60 pub cache_enabled: bool,
61}
62
63fn default_temperature() -> f32 {
64 0.7
65}
66
67fn default_max_tokens() -> usize {
68 1024
69}
70
71fn default_true() -> bool {
72 true
73}
74
75impl Default for AiResponseConfig {
76 fn default() -> Self {
77 Self {
78 enabled: false,
79 mode: AiResponseMode::Static,
80 prompt: None,
81 context: None,
82 temperature: default_temperature(),
83 max_tokens: default_max_tokens(),
84 schema: None,
85 cache_enabled: true,
86 }
87 }
88}
89
90impl AiResponseConfig {
91 pub fn new(enabled: bool, mode: AiResponseMode, prompt: String) -> Self {
93 Self {
94 enabled,
95 mode,
96 prompt: Some(prompt),
97 ..Default::default()
98 }
99 }
100
101 pub fn is_active(&self) -> bool {
103 self.enabled && self.mode != AiResponseMode::Static && self.prompt.is_some()
104 }
105}
106
107#[derive(Debug, Clone, Default)]
109pub struct RequestContext {
110 pub method: String,
112 pub path: String,
114 pub path_params: HashMap<String, Value>,
116 pub query_params: HashMap<String, Value>,
118 pub headers: HashMap<String, Value>,
120 pub body: Option<Value>,
122 pub multipart_fields: HashMap<String, Value>,
124 pub multipart_files: HashMap<String, String>,
126}
127
128impl RequestContext {
129 pub fn new(method: String, path: String) -> Self {
131 Self {
132 method,
133 path,
134 ..Default::default()
135 }
136 }
137
138 pub fn with_path_params(mut self, params: HashMap<String, Value>) -> Self {
140 self.path_params = params;
141 self
142 }
143
144 pub fn with_query_params(mut self, params: HashMap<String, Value>) -> Self {
146 self.query_params = params;
147 self
148 }
149
150 pub fn with_headers(mut self, headers: HashMap<String, Value>) -> Self {
152 self.headers = headers;
153 self
154 }
155
156 pub fn with_body(mut self, body: Value) -> Self {
158 self.body = Some(body);
159 self
160 }
161
162 pub fn with_multipart_fields(mut self, fields: HashMap<String, Value>) -> Self {
164 self.multipart_fields = fields;
165 self
166 }
167
168 pub fn with_multipart_files(mut self, files: HashMap<String, String>) -> Self {
170 self.multipart_files = files;
171 self
172 }
173}
174
175pub fn expand_prompt_template(_template: &str, _context: &RequestContext) -> String {
185 unimplemented!("expand_prompt_template has been moved to mockforge-template-expansion crate. Use mockforge_template_expansion::expand_prompt_template instead.")
188}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193 use serde_json::json;
194
195 #[test]
196 fn test_ai_response_config_default() {
197 let config = AiResponseConfig::default();
198 assert!(!config.enabled);
199 assert_eq!(config.mode, AiResponseMode::Static);
200 assert!(!config.is_active());
201 }
202
203 #[test]
204 fn test_ai_response_config_is_active() {
205 let config =
206 AiResponseConfig::new(true, AiResponseMode::Intelligent, "Test prompt".to_string());
207 assert!(config.is_active());
208
209 let config_disabled = AiResponseConfig {
210 enabled: false,
211 mode: AiResponseMode::Intelligent,
212 prompt: Some("Test".to_string()),
213 ..Default::default()
214 };
215 assert!(!config_disabled.is_active());
216 }
217
218 #[test]
219 fn test_request_context_builder() {
220 let mut path_params = HashMap::new();
221 path_params.insert("id".to_string(), json!("123"));
222
223 let context = RequestContext::new("POST".to_string(), "/users/123".to_string())
224 .with_path_params(path_params)
225 .with_body(json!({"name": "John"}));
226
227 assert_eq!(context.method, "POST");
228 assert_eq!(context.path, "/users/123");
229 assert_eq!(context.path_params.get("id"), Some(&json!("123")));
230 assert_eq!(context.body, Some(json!({"name": "John"})));
231 }
232
233 #[test]
234 fn test_expand_prompt_template_basic() {
235 let context = RequestContext::new("GET".to_string(), "/users".to_string());
236 let template = "Method: {{method}}, Path: {{path}}";
237 let expanded = expand_prompt_template(template, &context);
238 assert_eq!(expanded, "Method: GET, Path: /users");
239 }
240
241 #[test]
242 fn test_expand_prompt_template_body() {
243 let body = json!({
244 "message": "Hello",
245 "user": "Alice"
246 });
247 let context = RequestContext::new("POST".to_string(), "/chat".to_string()).with_body(body);
248
249 let template = "User {{body.user}} says: {{body.message}}";
250 let expanded = expand_prompt_template(template, &context);
251 assert_eq!(expanded, "User Alice says: Hello");
252 }
253
254 #[test]
255 fn test_expand_prompt_template_path_params() {
256 let mut path_params = HashMap::new();
257 path_params.insert("id".to_string(), json!("456"));
258 path_params.insert("name".to_string(), json!("test"));
259
260 let context = RequestContext::new("GET".to_string(), "/users/456".to_string())
261 .with_path_params(path_params);
262
263 let template = "Get user {{path.id}} with name {{path.name}}";
264 let expanded = expand_prompt_template(template, &context);
265 assert_eq!(expanded, "Get user 456 with name test");
266 }
267
268 #[test]
269 fn test_expand_prompt_template_query_params() {
270 let mut query_params = HashMap::new();
271 query_params.insert("search".to_string(), json!("term"));
272 query_params.insert("limit".to_string(), json!(10));
273
274 let context = RequestContext::new("GET".to_string(), "/search".to_string())
275 .with_query_params(query_params);
276
277 let template = "Search for {{query.search}} with limit {{query.limit}}";
278 let expanded = expand_prompt_template(template, &context);
279 assert_eq!(expanded, "Search for term with limit 10");
280 }
281
282 #[test]
283 fn test_expand_prompt_template_headers() {
284 let mut headers = HashMap::new();
285 headers.insert("user-agent".to_string(), json!("TestClient/1.0"));
286
287 let context =
288 RequestContext::new("GET".to_string(), "/api".to_string()).with_headers(headers);
289
290 let template = "Request from {{headers.user-agent}}";
291 let expanded = expand_prompt_template(template, &context);
292 assert_eq!(expanded, "Request from TestClient/1.0");
293 }
294
295 #[test]
296 fn test_expand_prompt_template_complex() {
297 let mut path_params = HashMap::new();
298 path_params.insert("id".to_string(), json!("789"));
299
300 let mut query_params = HashMap::new();
301 query_params.insert("format".to_string(), json!("json"));
302
303 let body = json!({"action": "update", "value": 42});
304
305 let context = RequestContext::new("PUT".to_string(), "/api/items/789".to_string())
306 .with_path_params(path_params)
307 .with_query_params(query_params)
308 .with_body(body);
309
310 let template = "{{method}} item {{path.id}} with action {{body.action}} and value {{body.value}} in format {{query.format}}";
311 let expanded = expand_prompt_template(template, &context);
312 assert_eq!(expanded, "PUT item 789 with action update and value 42 in format json");
313 }
314}