Skip to main content

agtrace_sdk/query/
filters.rs

1//! Filter types for session and event queries.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::types::EventPayload;
7
8/// Provider type for filtering sessions.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
10#[serde(rename_all = "snake_case")]
11pub enum Provider {
12    /// Claude Code (Anthropic)
13    ClaudeCode,
14    /// GitHub Copilot Codex
15    Codex,
16    /// Google Gemini
17    Gemini,
18}
19
20impl Provider {
21    pub fn as_str(&self) -> &'static str {
22        match self {
23            Provider::ClaudeCode => "claude_code",
24            Provider::Codex => "codex",
25            Provider::Gemini => "gemini",
26        }
27    }
28}
29
30/// Event type for filtering and classification.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
32#[serde(rename_all = "PascalCase")]
33pub enum EventType {
34    /// Tool/function call from assistant
35    ToolCall,
36    /// Tool execution result
37    ToolResult,
38    /// Assistant message/response
39    Message,
40    /// User input
41    User,
42    /// Reasoning/thinking blocks
43    Reasoning,
44    /// Token usage statistics
45    TokenUsage,
46    /// System notification
47    Notification,
48    /// Slash command invocation
49    SlashCommand,
50    /// Background task queue operation
51    QueueOperation,
52    /// Session summary
53    Summary,
54}
55
56impl EventType {
57    /// Match against EventPayload variant.
58    pub fn matches_payload(self, payload: &EventPayload) -> bool {
59        matches!(
60            (self, payload),
61            (EventType::ToolCall, EventPayload::ToolCall(_))
62                | (EventType::ToolResult, EventPayload::ToolResult(_))
63                | (EventType::Message, EventPayload::Message(_))
64                | (EventType::User, EventPayload::User(_))
65                | (EventType::Reasoning, EventPayload::Reasoning(_))
66                | (EventType::TokenUsage, EventPayload::TokenUsage(_))
67                | (EventType::Notification, EventPayload::Notification(_))
68                | (EventType::SlashCommand, EventPayload::SlashCommand(_))
69                | (EventType::QueueOperation, EventPayload::QueueOperation(_))
70                | (EventType::Summary, EventPayload::Summary(_))
71        )
72    }
73
74    /// Create EventType from EventPayload.
75    pub fn from_payload(payload: &EventPayload) -> Self {
76        match payload {
77            EventPayload::ToolCall(_) => EventType::ToolCall,
78            EventPayload::ToolResult(_) => EventType::ToolResult,
79            EventPayload::Message(_) => EventType::Message,
80            EventPayload::User(_) => EventType::User,
81            EventPayload::Reasoning(_) => EventType::Reasoning,
82            EventPayload::TokenUsage(_) => EventType::TokenUsage,
83            EventPayload::Notification(_) => EventType::Notification,
84            EventPayload::SlashCommand(_) => EventType::SlashCommand,
85            EventPayload::QueueOperation(_) => EventType::QueueOperation,
86            EventPayload::Summary(_) => EventType::Summary,
87        }
88    }
89}
90
91/// Truncate a string to a maximum length, adding ellipsis if truncated.
92pub fn truncate_string(s: &str, max_len: usize) -> String {
93    if s.len() > max_len {
94        format!("{}...", &s.chars().take(max_len - 3).collect::<String>())
95    } else {
96        s.to_string()
97    }
98}
99
100/// Truncate a JSON value recursively.
101pub fn truncate_json_value(value: &serde_json::Value, max_string_len: usize) -> serde_json::Value {
102    match value {
103        serde_json::Value::String(s) => {
104            serde_json::Value::String(truncate_string(s, max_string_len))
105        }
106        serde_json::Value::Array(arr) => serde_json::Value::Array(
107            arr.iter()
108                .take(3)
109                .map(|v| truncate_json_value(v, max_string_len))
110                .collect(),
111        ),
112        serde_json::Value::Object(obj) => serde_json::Value::Object(
113            obj.iter()
114                .take(5)
115                .map(|(k, v)| (k.clone(), truncate_json_value(v, max_string_len)))
116                .collect(),
117        ),
118        _ => value.clone(),
119    }
120}