mockforge-ui 0.3.88

Admin UI for MockForge - web-based interface for managing mock servers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Voice + LLM Interface API handlers for Admin UI
//!
//! Provides endpoints for processing voice commands and generating OpenAPI specs
//! using natural language commands powered by LLM.

use axum::{
    extract::{Json, State},
    http::StatusCode,
    response::Json as ResponseJson,
};
use mockforge_core::intelligent_behavior::IntelligentBehaviorConfig;
use mockforge_core::voice::{
    command_parser::{ParsedWorkspaceCreation, VoiceCommandParser},
    hook_transpiler::HookTranspiler,
    spec_generator::VoiceSpecGenerator,
    workspace_builder::WorkspaceBuilder,
    workspace_scenario_generator::{GeneratedWorkspaceScenario, WorkspaceScenarioGenerator},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::handlers::workspaces::WorkspaceState;
use crate::models::ApiResponse;

/// Request to process a voice command
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessVoiceCommandRequest {
    /// The voice command text (transcribed from speech or typed)
    pub command: String,
    /// Optional conversation ID for multi-turn interactions
    #[serde(default)]
    pub conversation_id: Option<String>,
}

/// Response from processing a voice command
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessVoiceCommandResponse {
    /// The original command
    pub command: String,
    /// Parsed command structure
    pub parsed: ParsedCommandData,
    /// Generated OpenAPI spec (as JSON)
    pub spec: Option<Value>,
    /// Optional error message
    pub error: Option<String>,
}

/// Parsed command data structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedCommandData {
    /// API type/category
    pub api_type: String,
    /// API title
    pub title: String,
    /// API description
    pub description: String,
    /// List of endpoints
    pub endpoints: Vec<Value>,
    /// List of data models
    pub models: Vec<Value>,
}

/// Process a voice command and generate an OpenAPI spec
///
/// POST /api/v2/voice/process
pub async fn process_voice_command(
    Json(request): Json<ProcessVoiceCommandRequest>,
) -> Result<ResponseJson<ApiResponse<ProcessVoiceCommandResponse>>, StatusCode> {
    if request.command.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    // Create parser with default config
    let config = IntelligentBehaviorConfig::default();
    let parser = VoiceCommandParser::new(config);

    // Parse the command
    let parsed = match parser.parse_command(&request.command).await {
        Ok(parsed) => parsed,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!("Failed to parse command: {}", e))));
        }
    };

    // Generate OpenAPI spec
    let spec_generator = VoiceSpecGenerator::new();
    let spec_result = spec_generator.generate_spec(&parsed).await;
    let spec = match spec_result {
        Ok(spec) => {
            // Convert spec to JSON and include title/version in response
            let mut spec_json = serde_json::to_value(&spec.spec).unwrap_or(Value::Null);
            // Add title and version to the spec JSON for easier frontend access
            if let Value::Object(ref mut obj) = spec_json {
                if let Some(Value::Object(ref mut info)) = obj.get_mut("info") {
                    // Ensure title and version are present
                    if !info.contains_key("title") {
                        info.insert("title".to_string(), Value::String(parsed.title.clone()));
                    }
                    if !info.contains_key("version") {
                        info.insert("version".to_string(), Value::String("1.0.0".to_string()));
                    }
                }
            }
            Some(spec_json)
        }
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!("Failed to generate spec: {}", e))));
        }
    };

    // Convert parsed command to response format
    let parsed_data = ParsedCommandData {
        api_type: parsed.api_type.clone(),
        title: parsed.title.clone(),
        description: parsed.description.clone(),
        endpoints: parsed
            .endpoints
            .iter()
            .map(|e| serde_json::to_value(e).unwrap_or(Value::Null))
            .collect(),
        models: parsed
            .models
            .iter()
            .map(|m| serde_json::to_value(m).unwrap_or(Value::Null))
            .collect(),
    };

    let response = ProcessVoiceCommandResponse {
        command: request.command,
        parsed: parsed_data,
        spec,
        error: None,
    };

    Ok(ResponseJson(ApiResponse::success(response)))
}

/// Request to transpile a natural language hook description
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranspileHookRequest {
    /// Natural language description of the hook logic
    pub description: String,
}

/// Response from transpiling a hook description
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranspileHookResponse {
    /// The original description
    pub description: String,
    /// Transpiled hook configuration (as YAML)
    pub hook_yaml: Option<String>,
    /// Transpiled hook configuration (as JSON)
    pub hook_json: Option<Value>,
    /// Optional error message
    pub error: Option<String>,
}

/// Transpile a natural language hook description to hook configuration
///
/// POST /api/v2/voice/transpile-hook
pub async fn transpile_hook(
    Json(request): Json<TranspileHookRequest>,
) -> Result<ResponseJson<ApiResponse<TranspileHookResponse>>, StatusCode> {
    if request.description.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    // Create transpiler with default config
    let config = IntelligentBehaviorConfig::default();
    let transpiler = HookTranspiler::new(config);

    // Transpile the description
    let hook = match transpiler.transpile(&request.description).await {
        Ok(hook) => hook,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to transpile hook: {}",
                e
            ))));
        }
    };

    // Convert hook to YAML and JSON
    let hook_yaml = match serde_yaml::to_string(&hook) {
        Ok(yaml) => Some(yaml),
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to serialize hook to YAML: {}",
                e
            ))));
        }
    };

    let hook_json = match serde_json::to_value(&hook) {
        Ok(json) => Some(json),
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to serialize hook to JSON: {}",
                e
            ))));
        }
    };

    let response = TranspileHookResponse {
        description: request.description,
        hook_yaml,
        hook_json,
        error: None,
    };

    Ok(ResponseJson(ApiResponse::success(response)))
}

/// Request to create a workspace scenario
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkspaceScenarioRequest {
    /// Natural language description of the scenario
    pub description: String,
}

/// Response from creating a workspace scenario
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkspaceScenarioResponse {
    /// The original description
    pub description: String,
    /// Generated workspace scenario
    pub scenario: Option<GeneratedWorkspaceScenario>,
    /// Optional error message
    pub error: Option<String>,
}

/// Create a workspace scenario from natural language description
///
/// POST /api/v2/voice/create-workspace-scenario
pub async fn create_workspace_scenario(
    Json(request): Json<CreateWorkspaceScenarioRequest>,
) -> Result<ResponseJson<ApiResponse<CreateWorkspaceScenarioResponse>>, StatusCode> {
    if request.description.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    // Create parser with default config
    let config = IntelligentBehaviorConfig::default();
    let parser = VoiceCommandParser::new(config);

    // Parse the scenario description
    let parsed = match parser.parse_workspace_scenario_command(&request.description).await {
        Ok(parsed) => parsed,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to parse scenario description: {}",
                e
            ))));
        }
    };

    // Generate the workspace scenario
    let generator = WorkspaceScenarioGenerator::new();
    let scenario = match generator.generate_scenario(&parsed).await {
        Ok(scenario) => Some(scenario),
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to generate workspace scenario: {}",
                e
            ))));
        }
    };

    let response = CreateWorkspaceScenarioResponse {
        description: request.description,
        scenario,
        error: None,
    };

    Ok(ResponseJson(ApiResponse::success(response)))
}

/// Request to create a workspace from natural language
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkspaceRequest {
    /// Natural language description of the workspace
    pub description: String,
}

/// Response from parsing workspace creation command (preview)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkspacePreviewResponse {
    /// The original description
    pub description: String,
    /// Parsed workspace creation data (for preview)
    pub parsed: ParsedWorkspaceCreation,
    /// Optional error message
    pub error: Option<String>,
}

/// Request to confirm and create workspace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfirmCreateWorkspaceRequest {
    /// Parsed workspace creation data (from preview)
    pub parsed: ParsedWorkspaceCreation,
}

/// Response from creating a workspace
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkspaceResponse {
    /// Workspace ID
    pub workspace_id: String,
    /// Workspace name
    pub name: String,
    /// Creation log
    pub creation_log: Vec<String>,
    /// Number of endpoints created
    pub endpoint_count: usize,
    /// Number of personas created
    pub persona_count: usize,
    /// Number of scenarios created
    pub scenario_count: usize,
    /// Whether reality continuum is configured
    pub has_reality_continuum: bool,
    /// Whether drift budget is configured
    pub has_drift_budget: bool,
    /// Optional error message
    pub error: Option<String>,
}

/// Parse workspace creation command and return preview
///
/// POST /api/v2/voice/create-workspace-preview
pub async fn create_workspace_preview(
    Json(request): Json<CreateWorkspaceRequest>,
) -> Result<ResponseJson<ApiResponse<CreateWorkspacePreviewResponse>>, StatusCode> {
    if request.description.trim().is_empty() {
        return Err(StatusCode::BAD_REQUEST);
    }

    // Create parser with default config
    let config = IntelligentBehaviorConfig::default();
    let parser = VoiceCommandParser::new(config);

    // Parse the workspace creation command
    let parsed = match parser.parse_workspace_creation_command(&request.description).await {
        Ok(parsed) => parsed,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to parse workspace creation command: {}",
                e
            ))));
        }
    };

    let response = CreateWorkspacePreviewResponse {
        description: request.description,
        parsed,
        error: None,
    };

    Ok(ResponseJson(ApiResponse::success(response)))
}

/// Confirm and create workspace from parsed command
///
/// POST /api/v2/voice/create-workspace-confirm
pub async fn create_workspace_confirm(
    State(state): State<WorkspaceState>,
    Json(request): Json<ConfirmCreateWorkspaceRequest>,
) -> Result<ResponseJson<ApiResponse<CreateWorkspaceResponse>>, StatusCode> {
    // Create workspace builder
    let mut builder = WorkspaceBuilder::new();

    // Get mutable access to workspace registry from state
    let mut registry = state.registry.write().await;

    // Build workspace
    let built = match builder.build_workspace(&mut registry, &request.parsed).await {
        Ok(built) => built,
        Err(e) => {
            return Ok(ResponseJson(ApiResponse::error(format!(
                "Failed to create workspace: {}",
                e
            ))));
        }
    };

    let endpoint_count = built
        .openapi_spec
        .as_ref()
        .map(|s| s.all_paths_and_operations().len())
        .unwrap_or(0);

    let response = CreateWorkspaceResponse {
        workspace_id: built.workspace_id,
        name: built.name,
        creation_log: built.creation_log,
        endpoint_count,
        persona_count: built.personas.len(),
        scenario_count: built.scenarios.len(),
        has_reality_continuum: built.reality_continuum.is_some(),
        has_drift_budget: built.drift_budget.is_some(),
        error: None,
    };

    Ok(ResponseJson(ApiResponse::success(response)))
}