mockforge_core/voice/
hook_transpiler.rs1use crate::intelligent_behavior::{
23 config::IntelligentBehaviorConfig, llm_client::LlmClient, types::LlmGenerationRequest,
24};
25use crate::Result;
26type Hook = serde_json::Value;
29type Condition = serde_json::Value;
30type HookAction = serde_json::Value;
31type HookType = serde_json::Value;
32type LogLevel = serde_json::Value;
33
34pub struct HookTranspiler {
36 llm_client: LlmClient,
38 config: IntelligentBehaviorConfig,
40}
41
42impl HookTranspiler {
43 pub fn new(config: IntelligentBehaviorConfig) -> Self {
45 let behavior_model = config.behavior_model.clone();
46 let llm_client = LlmClient::new(behavior_model);
47
48 Self { llm_client, config }
49 }
50
51 pub async fn transpile(&self, description: &str) -> Result<Hook> {
63 let system_prompt = r#"You are an expert at parsing natural language descriptions of hook logic
65and converting them to structured hook configurations.
66
67A hook consists of:
681. **Name**: A descriptive name for the hook
692. **Hook Type**: When the hook executes (pre_step, post_step, pre_orchestration, post_orchestration)
703. **Condition**: Optional condition that must be met for the hook to execute
714. **Actions**: List of actions to perform when the hook executes
72
73Available hook types:
74- pre_step: Execute before a step
75- post_step: Execute after a step
76- pre_orchestration: Execute before orchestration starts
77- post_orchestration: Execute after orchestration completes
78
79Available conditions:
80- equals: Variable equals value
81- not_equals: Variable not equals value
82- greater_than: Variable greater than numeric value
83- less_than: Variable less than numeric value
84- exists: Variable exists
85- and: All conditions must be true
86- or: At least one condition must be true
87- not: Condition must be false
88
89Available actions:
90- set_variable: Set a variable to a value
91- log: Log a message at a level (trace, debug, info, warn, error)
92- http_request: Make an HTTP request (webhook)
93- command: Execute a command
94- record_metric: Record a metric value
95
96For probability-based failures (e.g., "fail 5% of the time"), you should:
971. Use a condition that checks a random variable or metric
982. Use set_variable to set a failure flag
993. The actual failure injection should be handled by the chaos configuration
100
101For timing constraints (e.g., "instantly", "with delay"), use appropriate hook types or add delay actions.
102
103Return your response as a JSON object with this structure:
104{
105 "name": "string (descriptive hook name)",
106 "hook_type": "pre_step | post_step | pre_orchestration | post_orchestration",
107 "condition": {
108 "type": "condition type",
109 ...condition-specific fields
110 } or null,
111 "actions": [
112 {
113 "type": "action type",
114 ...action-specific fields
115 }
116 ]
117}
118
119Be specific and extract all details from the description. If timing is mentioned (instantly, with delay),
120choose the appropriate hook_type. If conditions are mentioned (for users flagged as VIP), create
121appropriate condition structures."#;
122
123 let user_prompt = format!(
125 "Parse this hook description and convert it to a hook configuration:\n\n{}",
126 description
127 );
128
129 let llm_request = LlmGenerationRequest {
131 system_prompt: system_prompt.to_string(),
132 user_prompt,
133 temperature: 0.2, max_tokens: 2000,
135 schema: None,
136 };
137
138 let response = self.llm_client.generate(&llm_request).await?;
140
141 if !response.is_object() {
144 return Err(crate::Error::generic(format!(
145 "LLM response is not a JSON object. Response: {}",
146 serde_json::to_string(&response).unwrap_or_default()
147 )));
148 }
149
150 Ok(response)
151 }
152
153 }