Skip to main content

ai_agent/utils/permissions/
classifier_shared.rs

1// Source: ~/claudecode/openclaudecode/src/utils/permissions/classifierShared.ts
2#![allow(dead_code)]
3
4//! Shared infrastructure for classifier-based permission systems.
5//!
6//! This module provides common types, schemas, and utilities used by both:
7//! - bash_classifier (semantic Bash command matching)
8//! - yolo_classifier (YOLO mode security classification)
9
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13/// A content block from an API response.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15#[serde(tag = "type")]
16pub enum ContentBlock {
17    #[serde(rename = "text")]
18    Text { text: String },
19    #[serde(rename = "tool_use")]
20    ToolUse {
21        name: String,
22        input: Value,
23        #[serde(skip_serializing_if = "Option::is_none")]
24        id: Option<String>,
25    },
26}
27
28/// Extract tool use block from message content by tool name.
29pub fn extract_tool_use_block<'a>(
30    content: &'a [ContentBlock],
31    tool_name: &str,
32) -> Option<&'a ContentBlock> {
33    content
34        .iter()
35        .find(|b| matches!(b, ContentBlock::ToolUse { name, .. } if name == tool_name))
36}
37
38/// Parse and validate classifier response from tool use block.
39/// Returns None if parsing fails.
40pub fn parse_classifier_response<T: for<'de> Deserialize<'de>>(
41    tool_use_block: &ContentBlock,
42) -> Option<T> {
43    if let ContentBlock::ToolUse { input, .. } = tool_use_block {
44        serde_json::from_value(input.clone()).ok()
45    } else {
46        None
47    }
48}