ccboard-core 0.12.0

Core library for ccboard - parsers, models, store, watcher
Documentation
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
//! Plugin usage analytics module
//!
//! Aggregates tool usage from SessionMetadata to classify and rank plugins:
//! - Skills (.claude/skills/)
//! - Commands (.claude/commands/)
//! - MCP Servers (mcp__server__tool format)
//! - Agents (Task tool with subagent_type)
//! - Native Tools (Read, Write, Edit, Bash, etc.)
//!
//! Provides metrics on total invocations, cost attribution, dead code detection.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::models::session::SessionMetadata;
use crate::pricing::calculate_cost;

/// Plugin classification by origin
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PluginType {
    /// User-defined skill from .claude/skills/
    Skill,
    /// User-defined command from .claude/commands/
    Command,
    /// Spawned agent via Task tool
    Agent,
    /// MCP server tool (mcp__server__tool format)
    McpServer,
    /// Built-in Claude Code tool
    NativeTool,
}

impl PluginType {
    /// Icon for TUI/Web display
    pub fn icon(&self) -> &'static str {
        match self {
            Self::Skill => "🎓",
            Self::Command => "",
            Self::Agent => "🤖",
            Self::McpServer => "🔌",
            Self::NativeTool => "🛠️",
        }
    }

    /// Human-readable label
    pub fn label(&self) -> &'static str {
        match self {
            Self::Skill => "Skill",
            Self::Command => "Command",
            Self::Agent => "Agent",
            Self::McpServer => "MCP Server",
            Self::NativeTool => "Native Tool",
        }
    }
}

/// Usage statistics for a single plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginUsage {
    /// Plugin identifier (e.g., "rust-expert", "mcp__context7__search")
    pub name: String,
    /// Classification
    pub plugin_type: PluginType,
    /// Icon emoji (computed from plugin_type, serialized for WASM compatibility)
    pub icon: String,
    /// Total invocations across all sessions
    pub total_invocations: usize,
    /// Session IDs where this plugin was used
    pub sessions_used: Vec<String>,
    /// Total cost attributed to this plugin ($)
    pub total_cost: f64,
    /// Average tokens per invocation
    pub avg_tokens_per_invocation: u64,
    /// First usage timestamp
    pub first_seen: DateTime<Utc>,
    /// Last usage timestamp
    pub last_seen: DateTime<Utc>,
}

impl PluginUsage {
    /// Create new plugin usage record
    fn new(
        name: String,
        plugin_type: PluginType,
        invocations: usize,
        session_id: String,
        cost: f64,
        avg_tokens: u64,
        timestamp: DateTime<Utc>,
    ) -> Self {
        Self {
            name,
            icon: plugin_type.icon().to_string(),
            plugin_type,
            total_invocations: invocations,
            sessions_used: vec![session_id],
            total_cost: cost,
            avg_tokens_per_invocation: avg_tokens,
            first_seen: timestamp,
            last_seen: timestamp,
        }
    }

    /// Merge another usage record into this one
    #[allow(dead_code)]
    fn merge(&mut self, other: &Self) {
        self.total_invocations += other.total_invocations;
        if !self.sessions_used.contains(&other.sessions_used[0]) {
            self.sessions_used.push(other.sessions_used[0].clone());
        }
        self.total_cost += other.total_cost;
        if other.first_seen < self.first_seen {
            self.first_seen = other.first_seen;
        }
        if other.last_seen > self.last_seen {
            self.last_seen = other.last_seen;
        }
    }
}

/// Complete plugin analytics data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginAnalytics {
    /// Total plugin count (active + dead)
    pub total_plugins: usize,
    /// Active plugin count (used at least once)
    pub active_plugins: usize,
    /// Dead plugins (defined but never used)
    pub dead_plugins: Vec<String>,
    /// All plugin usage records
    pub plugins: Vec<PluginUsage>,
    /// Top 10 by usage
    pub top_by_usage: Vec<PluginUsage>,
    /// Top 10 by cost
    pub top_by_cost: Vec<PluginUsage>,
    /// Timestamp of computation
    pub computed_at: DateTime<Utc>,
}

impl PluginAnalytics {
    /// Create empty analytics
    pub fn empty() -> Self {
        Self {
            total_plugins: 0,
            active_plugins: 0,
            dead_plugins: Vec::new(),
            plugins: Vec::new(),
            top_by_usage: Vec::new(),
            top_by_cost: Vec::new(),
            computed_at: Utc::now(),
        }
    }
}

/// Classify plugin by name and context
fn classify_plugin(name: &str, skills: &[String], commands: &[String]) -> PluginType {
    // MCP server format: mcp__server__tool
    if name.starts_with("mcp__") {
        return PluginType::McpServer;
    }

    // Agent spawning via Task tool
    if name == "Task" {
        return PluginType::Agent;
    }

    // Check against known skills (case-insensitive)
    let lower_name = name.to_lowercase();
    if skills
        .iter()
        .any(|s| lower_name.contains(&s.to_lowercase()))
    {
        return PluginType::Skill;
    }

    // Check against known commands
    if commands
        .iter()
        .any(|c| lower_name.contains(&c.to_lowercase()))
    {
        return PluginType::Command;
    }

    // Known native tools
    const NATIVE_TOOLS: &[&str] = &[
        "Read",
        "Write",
        "Edit",
        "MultiEdit",
        "Bash",
        "Grep",
        "Glob",
        "WebSearch",
        "WebFetch",
        "NotebookEdit",
        "AskUserQuestion",
        "EnterPlanMode",
        "ExitPlanMode",
        "TaskCreate",
        "TaskUpdate",
        "TaskGet",
        "TaskList",
        "TeamCreate",
        "TeamDelete",
        "SendMessage",
        "Skill",
    ];

    if NATIVE_TOOLS.contains(&name) {
        return PluginType::NativeTool;
    }

    // Default: treat as native tool
    PluginType::NativeTool
}

/// Aggregate plugin usage from sessions
///
/// # Arguments
/// - `sessions`: All sessions to analyze
/// - `available_skills`: Skill names from .claude/skills/*.md
/// - `available_commands`: Command names from .claude/commands/*.md
///
/// # Returns
/// Complete plugin analytics with rankings and dead code detection
pub fn aggregate_plugin_usage(
    sessions: &[Arc<SessionMetadata>],
    available_skills: &[String],
    available_commands: &[String],
) -> PluginAnalytics {
    let mut usage_map: HashMap<String, PluginUsage> = HashMap::new();

    // Aggregate tool usage from all sessions
    for session in sessions {
        // Calculate session cost (proportional attribution)
        // Use first model from models_used, or default to sonnet-4.5
        let model = session
            .models_used
            .first()
            .map(|s| s.as_str())
            .unwrap_or("sonnet-4.5");
        let session_cost = calculate_cost(
            model,
            session.input_tokens,
            session.output_tokens,
            session.cache_creation_tokens,
            session.cache_read_tokens,
        );
        let session_tokens = session.total_tokens;

        // Skip sessions with no tool usage
        if session.tool_usage.is_empty() {
            continue;
        }

        // Total tool calls in session (for proportional cost)
        let total_calls: usize = session.tool_usage.values().sum();
        if total_calls == 0 {
            continue;
        }

        for (tool_name, call_count) in &session.tool_usage {
            // Skip empty tool names (malformed session data)
            if tool_name.is_empty() {
                continue;
            }

            let plugin_type = classify_plugin(tool_name, available_skills, available_commands);

            // Proportional cost attribution
            let tool_cost = session_cost * (*call_count as f64 / total_calls as f64);

            // Average tokens per call (rough approximation)
            let avg_tokens = if *call_count > 0 {
                session_tokens / *call_count as u64
            } else {
                0
            };

            // Use first/last timestamp from session
            let timestamp = session
                .first_timestamp
                .or(session.last_timestamp)
                .unwrap_or_else(Utc::now);

            usage_map
                .entry(tool_name.clone())
                .and_modify(|usage| {
                    usage.total_invocations += call_count;
                    if !usage.sessions_used.contains(&session.id.to_string()) {
                        usage.sessions_used.push(session.id.to_string());
                    }
                    usage.total_cost += tool_cost;

                    // Update timestamps
                    if let Some(first_ts) = session.first_timestamp {
                        if first_ts < usage.first_seen {
                            usage.first_seen = first_ts;
                        }
                    }
                    if let Some(last_ts) = session.last_timestamp {
                        if last_ts > usage.last_seen {
                            usage.last_seen = last_ts;
                        }
                    }

                    // Recalculate average tokens (weighted by invocations)
                    usage.avg_tokens_per_invocation = (usage.avg_tokens_per_invocation
                        * (usage.total_invocations - call_count) as u64
                        + avg_tokens * *call_count as u64)
                        / usage.total_invocations as u64;
                })
                .or_insert_with(|| {
                    PluginUsage::new(
                        tool_name.clone(),
                        plugin_type,
                        *call_count,
                        session.id.to_string(),
                        tool_cost,
                        avg_tokens,
                        timestamp,
                    )
                });
        }
    }

    // Identify dead code (defined but never used)
    let used_names: HashSet<_> = usage_map.keys().map(|s| s.to_lowercase()).collect();
    let dead_plugins: Vec<String> = available_skills
        .iter()
        .chain(available_commands.iter())
        .filter(|name| !used_names.contains(&name.to_lowercase()))
        .cloned()
        .collect();

    // Convert to sorted vector
    let mut plugins: Vec<_> = usage_map.into_values().collect();
    plugins.sort_by(|a, b| b.total_invocations.cmp(&a.total_invocations));

    // Top 10 by usage
    let top_by_usage = plugins.iter().take(10).cloned().collect();

    // Top 10 by cost
    let mut top_by_cost = plugins.clone();
    top_by_cost.sort_by(|a, b| {
        b.total_cost
            .partial_cmp(&a.total_cost)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let top_by_cost = top_by_cost.into_iter().take(10).collect();

    PluginAnalytics {
        total_plugins: plugins.len() + dead_plugins.len(),
        active_plugins: plugins.len(),
        dead_plugins,
        plugins,
        top_by_usage,
        top_by_cost,
        computed_at: Utc::now(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_classify_plugin() {
        let skills = vec!["rust-expert".to_string(), "boldguy-draft".to_string()];
        let commands = vec!["commit".to_string()];

        assert_eq!(
            classify_plugin("rust-expert", &skills, &commands),
            PluginType::Skill
        );
        assert_eq!(
            classify_plugin("mcp__context7__search", &skills, &commands),
            PluginType::McpServer
        );
        assert_eq!(
            classify_plugin("Read", &skills, &commands),
            PluginType::NativeTool
        );
        assert_eq!(
            classify_plugin("Task", &skills, &commands),
            PluginType::Agent
        );
    }

    #[test]
    fn test_aggregate_plugin_usage() {
        use std::path::PathBuf;

        // Create test sessions with tool_usage
        let mut tool_usage1 = HashMap::new();
        tool_usage1.insert("rust-expert".to_string(), 5);
        tool_usage1.insert("mcp__context7__search".to_string(), 3);

        let mut session1 = SessionMetadata::from_path(
            PathBuf::from("/tmp/test-session.jsonl"),
            "test-project".into(),
        );
        session1.tool_usage = tool_usage1;
        session1.total_tokens = 10000;
        session1.first_timestamp = Some(Utc::now());
        session1.last_timestamp = Some(Utc::now());

        let sessions = vec![Arc::new(session1)];
        let skills = vec!["rust-expert".to_string()];
        let commands = vec![];

        let analytics = aggregate_plugin_usage(&sessions, &skills, &commands);

        assert_eq!(analytics.active_plugins, 2);
        assert_eq!(analytics.plugins[0].name, "rust-expert");
        assert_eq!(analytics.plugins[0].total_invocations, 5);
        assert_eq!(analytics.plugins[0].plugin_type, PluginType::Skill);
        assert_eq!(analytics.plugins[1].name, "mcp__context7__search");
        assert_eq!(analytics.plugins[1].plugin_type, PluginType::McpServer);
    }

    #[test]
    fn test_dead_code_detection() {
        let sessions = vec![];
        let skills = vec!["rust-expert".to_string(), "unused-skill".to_string()];
        let commands = vec!["commit".to_string(), "never-used".to_string()];

        let analytics = aggregate_plugin_usage(&sessions, &skills, &commands);

        assert_eq!(analytics.active_plugins, 0);
        assert_eq!(analytics.dead_plugins.len(), 4); // All skills + commands unused
        assert!(analytics.dead_plugins.contains(&"unused-skill".to_string()));
        assert!(analytics.dead_plugins.contains(&"never-used".to_string()));
    }

    #[test]
    fn test_empty_sessions() {
        let sessions = vec![];
        let skills = vec![];
        let commands = vec![];

        let analytics = aggregate_plugin_usage(&sessions, &skills, &commands);

        assert_eq!(analytics.active_plugins, 0);
        assert_eq!(analytics.total_plugins, 0);
        assert!(analytics.plugins.is_empty());
    }
}