hippox-drivers 0.3.5

🦛All indivisible atomic driver units in Hippox.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! JSON file driver module
//!
//! This module provides drivers for JSON file operations including
//! reading JSON files, parsing JSON data, writing JSON files,
//! validating JSON syntax, and querying JSON data.
use crate::DriverCallback;
use crate::DriverContext;
use crate::{
    DriverCategory,
    types::{Driver, DriverParameter},
};
use crate::{DriverError, DriverResult, ensure_dir, file_exists, read_file_content, validate_path, write_file_content};
use serde_json::{Value, from_str, json, to_string_pretty};
use std::collections::HashMap;
use tracing::{debug, info};
/// Driver for reading JSON files
#[derive(Debug)]
pub struct JsonReadDriver;
#[async_trait::async_trait]
impl Driver for JsonReadDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        return "json_read";
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        return "Read and parse JSON file content";
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        return "Use this skill when the user wants to read a JSON file, parse configuration data, or extract structured data from .json files";
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "path".to_string(),
                param_type: "string".to_string(),
                description: "Path to the JSON file".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("config.json".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "query".to_string(),
                param_type: "string".to_string(),
                description: "Optional JSONPath query to extract specific data (e.g., '$.users[0].name')".to_string(),
                required: false,
                default: None,
                example: Some(Value::String("$.data.results".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "pretty".to_string(),
                param_type: "boolean".to_string(),
                description: "Pretty-print the JSON output".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "json_read",
            "parameters": {
                "path": "config.json"
            }
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return "{\n  \"name\": \"example\",\n  \"version\": \"1.0\"\n}".to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::Document;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing json_read driver");
        // Extract required parameters
        let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        let query = parameters.get("query").and_then(|v| v.as_str());
        let pretty = parameters.get("pretty").and_then(|v| v.as_bool()).unwrap_or(true);
        debug!("Reading JSON file: {}, query: {:?}", path, query);
        let validated_path = validate_path(path, None).map_err(|e| DriverError::execution(format!("Invalid path: {}", e)))?;
        if !file_exists(&validated_path.to_string_lossy()) {
            return Err(DriverError::execution(format!("JSON file not found: {}", path)));
        }
        let content =
            read_file_content(&validated_path.to_string_lossy()).map_err(|e| DriverError::execution(format!("Failed to read file: {}", e)))?;
        let json_value: Value = from_str(&content).map_err(|e| DriverError::execution(format!("Failed to parse JSON: {}", e)))?;
        let result = if let Some(q) = query {
            let extracted = query_json(&json_value, q)?;
            if pretty {
                to_string_pretty(&extracted).map_err(|e| DriverError::execution(format!("Failed to serialize JSON: {}", e)))?
            } else {
                extracted.to_string()
            }
        } else if pretty {
            to_string_pretty(&json_value).map_err(|e| DriverError::execution(format!("Failed to serialize JSON: {}", e)))?
        } else {
            json_value.to_string()
        };
        info!("JSON read completed: {}", path);
        return Ok(result);
    }
    /// Validates the parameters before execution
    fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
        parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        return Ok(());
    }
}
/// Driver for writing JSON files
#[derive(Debug)]
pub struct JsonWriteDriver;
#[async_trait::async_trait]
impl Driver for JsonWriteDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        return "json_write";
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        return "Write data to JSON file";
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        return "Use this skill when the user wants to create, save, or update a JSON file with structured data";
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "path".to_string(),
                param_type: "string".to_string(),
                description: "Path to save the JSON file".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("output.json".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "data".to_string(),
                param_type: "object".to_string(),
                description: "JSON data to write (can be object or array)".to_string(),
                required: true,
                default: None,
                example: Some(json!({"name": "example", "value": 42})),
                enum_values: None,
            },
            DriverParameter {
                name: "pretty".to_string(),
                param_type: "boolean".to_string(),
                description: "Pretty-print the JSON output".to_string(),
                required: false,
                default: Some(Value::Bool(true)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
            DriverParameter {
                name: "merge".to_string(),
                param_type: "boolean".to_string(),
                description: "Merge with existing JSON file (only works with objects)".to_string(),
                required: false,
                default: Some(Value::Bool(false)),
                example: Some(Value::Bool(true)),
                enum_values: None,
            },
        ];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "json_write",
            "parameters": {
                "path": "output.json",
                "data": {"name": "example", "version": "1.0"}
            }
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return "JSON written to: output.json".to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::Document;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing json_write driver");
        // Extract required parameters
        let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        let data = parameters.get("data").ok_or_else(|| DriverError::missing_parameter("data"))?;
        let pretty = parameters.get("pretty").and_then(|v| v.as_bool()).unwrap_or(true);
        let merge = parameters.get("merge").and_then(|v| v.as_bool()).unwrap_or(false);
        debug!("Writing JSON file: {}, merge: {}", path, merge);
        let validated_path = validate_path(path, None).map_err(|e| DriverError::execution(format!("Invalid path: {}", e)))?;
        if let Some(parent) = validated_path.parent() {
            ensure_dir(&parent.to_string_lossy()).map_err(|e| DriverError::execution(format!("Failed to create directory: {}", e)))?;
        }
        let final_data = if merge && file_exists(&validated_path.to_string_lossy()) {
            let existing_content = read_file_content(&validated_path.to_string_lossy())
                .map_err(|e| DriverError::execution(format!("Failed to read existing file: {}", e)))?;
            let existing_json: Value =
                from_str(&existing_content).map_err(|e| DriverError::execution(format!("Failed to parse existing JSON: {}", e)))?;
            merge_json(&existing_json, data)?
        } else {
            data.clone()
        };
        let content = if pretty {
            to_string_pretty(&final_data).map_err(|e| DriverError::execution(format!("Failed to serialize JSON: {}", e)))?
        } else {
            final_data.to_string()
        };
        write_file_content(&validated_path.to_string_lossy(), &content, false)
            .map_err(|e| DriverError::execution(format!("Failed to write file: {}", e)))?;
        info!("JSON written to: {}", path);
        return Ok(format!("JSON written to: {}", path));
    }
    /// Validates the parameters before execution
    fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
        parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        parameters.get("data").ok_or_else(|| DriverError::missing_parameter("data"))?;
        return Ok(());
    }
}
/// Driver for validating JSON syntax
#[derive(Debug)]
pub struct JsonValidateDriver;
#[async_trait::async_trait]
impl Driver for JsonValidateDriver {
    /// Returns the unique name of this driver
    fn name(&self) -> &str {
        return "json_validate";
    }
    /// Returns a brief description of the driver's functionality
    fn description(&self) -> &str {
        return "Validate JSON file syntax and optional schema validation";
    }
    /// Returns detailed usage guidance for LLMs
    fn usage_hint(&self) -> &str {
        return "Use this skill when the user wants to check if a JSON file is valid or validate against a schema";
    }
    /// Returns the parameter definitions for this driver
    fn parameters(&self) -> Vec<DriverParameter> {
        return vec![
            DriverParameter {
                name: "path".to_string(),
                param_type: "string".to_string(),
                description: "Path to the JSON file to validate".to_string(),
                required: true,
                default: None,
                example: Some(Value::String("data.json".to_string())),
                enum_values: None,
            },
            DriverParameter {
                name: "schema_path".to_string(),
                param_type: "string".to_string(),
                description: "Optional path to JSON Schema file for validation".to_string(),
                required: false,
                default: None,
                example: Some(Value::String("schema.json".to_string())),
                enum_values: None,
            },
        ];
    }
    /// Returns an example call for this driver
    fn example_call(&self) -> DriverResult<Value> {
        return Ok(json!({
            "action": "json_validate",
            "parameters": {
                "path": "data.json"
            }
        }));
    }
    /// Returns an example output from this driver
    fn example_output(&self) -> String {
        return "JSON is valid".to_string();
    }
    /// Returns the category of this driver
    fn category(&self) -> DriverCategory {
        return DriverCategory::Document;
    }
    /// Executes the driver with the given parameters
    async fn execute(
        &self,
        parameters: &HashMap<String, Value>,
        _callback: Option<&dyn DriverCallback>,
        _context: Option<&DriverContext>,
    ) -> DriverResult<String> {
        debug!("Executing json_validate driver");
        // Extract required parameters
        let path = parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        debug!("Validating JSON file: {}", path);
        let validated_path = validate_path(path, None).map_err(|e| DriverError::execution(format!("Invalid path: {}", e)))?;
        if !file_exists(&validated_path.to_string_lossy()) {
            return Err(DriverError::execution(format!("JSON file not found: {}", path)));
        }
        let content =
            read_file_content(&validated_path.to_string_lossy()).map_err(|e| DriverError::execution(format!("Failed to read file: {}", e)))?;
        match from_str::<Value>(&content) {
            Ok(json_value) => {
                let mut output = format!("JSON is valid\n");
                output.push_str(&format!("  Type: {}\n", json_type_name(&json_value)));
                if let Some(schema_path) = parameters.get("schema_path").and_then(|v| v.as_str()) {
                    debug!("Validating against schema: {}", schema_path);
                    let schema_validated_path =
                        validate_path(schema_path, None).map_err(|e| DriverError::execution(format!("Invalid schema path: {}", e)))?;
                    if file_exists(&schema_validated_path.to_string_lossy()) {
                        let schema_content = read_file_content(&schema_validated_path.to_string_lossy())
                            .map_err(|e| DriverError::execution(format!("Failed to read schema file: {}", e)))?;
                        let schema: Value =
                            from_str(&schema_content).map_err(|e| DriverError::execution(format!("Failed to parse schema: {}", e)))?;
                        validate_json_schema(&json_value, &schema)?;
                        output.push_str("  Schema validation passed\n");
                        info!("Schema validation passed for: {}", path);
                    } else {
                        output.push_str(&format!("  Schema file not found: {}\n", schema_path));
                        info!("Schema file not found: {}", schema_path);
                    }
                }
                info!("JSON validation completed: {}", path);
                return Ok(output);
            }
            Err(e) => {
                return Err(DriverError::execution(format!("Invalid JSON: {}", e)));
            }
        }
    }
    /// Validates the parameters before execution
    fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
        parameters.get("path").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("path"))?;
        return Ok(());
    }
}
/// Queries JSON data using a simple dot notation path
///
/// # Arguments
/// * `value` - JSON value to query
/// * `query` - Query path (e.g., "$.data.results" or "$.users[0].name")
///
/// # Returns
/// * `DriverResult<Value>` - Extracted JSON value
fn query_json(value: &Value, query: &str) -> DriverResult<Value> {
    if query == "$" || query == "." {
        return Ok(value.clone());
    }
    let parts: Vec<&str> = query.split('.').collect();
    let mut current = value;
    for part in parts {
        if part.starts_with('$') {
            let key = &part[1..];
            if !key.is_empty() {
                current = current.get(key).ok_or_else(|| DriverError::execution(format!("Path '{}' not found", query)))?;
            }
        } else if part.starts_with('[') && part.ends_with(']') {
            let idx_str = &part[1..part.len() - 1];
            if let Ok(idx) = idx_str.parse::<usize>() {
                if let Some(arr) = current.as_array() {
                    current = arr.get(idx).ok_or_else(|| DriverError::execution(format!("Index {} out of range", idx)))?;
                } else {
                    return Err(DriverError::execution(format!("Cannot index non-array with [{}]", idx_str)));
                }
            } else {
                return Err(DriverError::execution(format!("Invalid array index: {}", idx_str)));
            }
        } else {
            current = current.get(part).ok_or_else(|| DriverError::execution(format!("Path '{}' not found", query)))?;
        }
    }
    return Ok(current.clone());
}
/// Merges two JSON values
///
/// # Arguments
/// * `existing` - Existing JSON value
/// * `new` - New JSON value to merge
///
/// # Returns
/// * `DriverResult<Value>` - Merged JSON value
fn merge_json(existing: &Value, new: &Value) -> DriverResult<Value> {
    match (existing, new) {
        (Value::Object(existing_obj), Value::Object(new_obj)) => {
            let mut merged = existing_obj.clone();
            for (k, v) in new_obj {
                if let Some(existing_val) = merged.get(k) {
                    merged.insert(k.clone(), merge_json(existing_val, v)?);
                } else {
                    merged.insert(k.clone(), v.clone());
                }
            }
            return Ok(Value::Object(merged));
        }
        _ => return Ok(new.clone()),
    }
}
/// Gets the JSON type name as a string
///
/// # Arguments
/// * `value` - JSON value
///
/// # Returns
/// * `&'static str` - Type name
fn json_type_name(value: &Value) -> &'static str {
    return match value {
        Value::Null => "null",
        Value::Bool(_) => "boolean",
        Value::Number(_) => "number",
        Value::String(_) => "string",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
    };
}
/// Validates JSON against a simple schema
///
/// # Arguments
/// * `value` - JSON value to validate
/// * `schema` - Schema to validate against
///
/// # Returns
/// * `DriverResult<()>` - Ok if valid, Err otherwise
fn validate_json_schema(value: &Value, schema: &Value) -> DriverResult<()> {
    // Check required fields
    if let Some(required) = schema.get("required").and_then(|v| v.as_array()) {
        if let Some(obj) = value.as_object() {
            for req in required {
                if let Some(req_str) = req.as_str() {
                    if !obj.contains_key(req_str) {
                        return Err(DriverError::validation("schema", format!("Missing required field: {}", req_str)));
                    }
                }
            }
        }
    }
    // Check property types
    if let Some(properties) = schema.get("properties").and_then(|v| v.as_object()) {
        if let Some(obj) = value.as_object() {
            for (prop_name, prop_schema) in properties {
                if let Some(prop_value) = obj.get(prop_name) {
                    validate_property(prop_value, prop_schema, prop_name)?;
                }
            }
        }
    }
    return Ok(());
}
/// Validates a single property against its schema
///
/// # Arguments
/// * `value` - Property value
/// * `schema` - Property schema
/// * `prop_name` - Property name for error messages
///
/// # Returns
/// * `DriverResult<()>` - Ok if valid, Err otherwise
fn validate_property(value: &Value, schema: &Value, prop_name: &str) -> DriverResult<()> {
    if let Some(expected_type) = schema.get("type").and_then(|v| v.as_str()) {
        let actual_type = json_type_name(value);
        if actual_type != expected_type {
            return Err(DriverError::validation(prop_name, format!("Expected type '{}' but got '{}'", expected_type, actual_type)));
        }
    }
    return Ok(());
}