qualia_cli/handlers/inference_policy/
io.rs1use serde::de::DeserializeOwned;
3use std::{io::Read, path::Path};
4pub(super) fn bytes(path: &Path, limit: usize) -> Result<Vec<u8>, String> {
5 let file = std::fs::File::open(path).map_err(|e| format!("Cannot open operator input: {e}"))?;
6 let meta = file.metadata().map_err(|e| e.to_string())?;
7 if !meta.is_file() || meta.len() > limit as u64 {
8 return Err("Operator input exceeds its byte limit or is not a regular file".into());
9 }
10 let mut result = Vec::with_capacity(limit + 1);
11 file.take(limit as u64 + 1)
12 .read_to_end(&mut result)
13 .map_err(|e| e.to_string())?;
14 if result.len() > limit {
15 return Err("Operator input grew beyond its byte limit".into());
16 }
17 Ok(result)
18}
19pub(super) fn json<T: DeserializeOwned>(path: &Path, limit: usize) -> Result<T, String> {
20 serde_json::from_slice(&bytes(path, limit)?).map_err(|e| {
21 format!(
22 "Invalid operator JSON at line {}, column {}",
23 e.line(),
24 e.column()
25 )
26 })
27}
28pub(super) fn policy(path: &Path) -> Result<String, String> {
29 let rules: qualia_core_db::llm_agent::governed::PolicyRules = json(path, 128 * 1024)?;
32 let encoded = serde_json::to_string(&rules)
33 .map_err(|_| "Cannot encode validated operator policy".to_string())?;
34 if encoded.len() > 128 * 1024 {
35 return Err("Encoded operator policy exceeds its byte limit".into());
36 }
37 Ok(encoded)
38}