dot-agent-core 0.5.0

Core library for dot-agent profile management
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
//! Category Classifier
//!
//! ProfileのファイルをCategoryStoreの定義に基づいて分類する。

use std::collections::HashMap;
use std::path::PathBuf;

use glob::Pattern;

use crate::error::Result;
use crate::Profile;

use super::builtin::CategoryDef;
use super::store::CategoryStore;

/// 分類モード
#[derive(Debug, Clone, Copy, Default)]
pub enum ClassificationMode {
    /// Globパターンマッチング(高速、デフォルト)
    #[default]
    Glob,
    /// LLMによるセマンティック分類(高精度)
    Llm,
}

/// ファイルの分類結果
#[derive(Debug, Clone)]
pub struct FileClassification {
    /// ファイルパス(Profile相対)
    pub path: PathBuf,
    /// 所属カテゴリ(複数可、priority順)
    pub categories: Vec<String>,
    /// 分類の確信度(LLMモード時のみ有効、0.0-1.0)
    pub confidence: Option<f32>,
}

/// Profile全体の分類結果
#[derive(Debug, Clone)]
pub struct ClassifiedProfile {
    /// 元Profile名
    pub profile_name: String,
    /// ファイル分類結果
    pub files: Vec<FileClassification>,
    /// 使用した分類モード
    pub mode: ClassificationMode,
    /// 分類中の警告メッセージ
    pub warnings: Vec<String>,
}

impl ClassifiedProfile {
    /// 特定カテゴリのファイルを取得
    pub fn files_in_category(&self, category: &str) -> Vec<&PathBuf> {
        self.files
            .iter()
            .filter(|f| f.categories.iter().any(|c| c == category))
            .map(|f| &f.path)
            .collect()
    }

    /// カテゴリ別のファイル数を取得
    pub fn category_counts(&self) -> HashMap<String, usize> {
        let mut counts = HashMap::new();
        for file in &self.files {
            for cat in &file.categories {
                *counts.entry(cat.clone()).or_insert(0) += 1;
            }
        }
        counts
    }

    /// 未分類ファイルを取得
    pub fn uncategorized(&self) -> Vec<&PathBuf> {
        self.files
            .iter()
            .filter(|f| f.categories.is_empty())
            .map(|f| &f.path)
            .collect()
    }
}

/// カテゴリ分類器
pub struct CategoryClassifier {
    mode: ClassificationMode,
    store: CategoryStore,
    compiled_patterns: HashMap<String, Vec<Pattern>>,
}

impl CategoryClassifier {
    /// 新規分類器を作成
    pub fn new(mode: ClassificationMode, store: CategoryStore) -> Result<Self> {
        let mut compiled_patterns = HashMap::new();

        for cat in store.all() {
            let patterns = cat
                .patterns
                .iter()
                .map(|p| Pattern::new(p))
                .collect::<std::result::Result<Vec<_>, _>>()?;
            compiled_patterns.insert(cat.name.clone(), patterns);
        }

        Ok(Self {
            mode,
            store,
            compiled_patterns,
        })
    }

    /// ProfileからCategoryStoreを取得して分類器を構築
    pub fn from_profile(profile: &Profile, mode: ClassificationMode) -> Result<Self> {
        let store = profile.category_store()?;
        Self::new(mode, store)
    }

    /// ビルトインカテゴリのみで分類器を構築
    pub fn builtin(mode: ClassificationMode) -> Result<Self> {
        Self::new(mode, CategoryStore::builtin())
    }

    /// 利用可能なカテゴリ名を取得
    pub fn category_names(&self) -> Vec<&str> {
        self.store.names()
    }

    /// カテゴリ定義を取得
    pub fn get_category(&self, name: &str) -> Option<&CategoryDef> {
        self.store.get(name)
    }

    /// Profileを分類
    pub fn classify(&self, profile: &Profile) -> Result<ClassifiedProfile> {
        let files = profile.list_files()?;

        let (classifications, warnings) = match self.mode {
            ClassificationMode::Glob => (self.classify_by_glob(&files)?, Vec::new()),
            ClassificationMode::Llm => self.classify_by_llm_with_warnings(profile, &files)?,
        };

        Ok(ClassifiedProfile {
            profile_name: profile.name.clone(),
            files: classifications,
            mode: self.mode,
            warnings,
        })
    }

    /// Globパターンで分類
    pub fn classify_by_glob(&self, files: &[PathBuf]) -> Result<Vec<FileClassification>> {
        let mut results = Vec::with_capacity(files.len());

        for file in files {
            let file_str = file.to_string_lossy();
            let mut matched_categories = Vec::new();

            for cat in self.store.all() {
                if let Some(patterns) = self.compiled_patterns.get(&cat.name) {
                    if patterns.iter().any(|p| p.matches(&file_str)) {
                        matched_categories.push(cat.name.clone());
                    }
                }
            }

            results.push(FileClassification {
                path: file.clone(),
                categories: matched_categories,
                confidence: None,
            });
        }

        Ok(results)
    }

    /// LLMで分類(警告情報付き)
    fn classify_by_llm_with_warnings(
        &self,
        profile: &Profile,
        files: &[PathBuf],
    ) -> Result<(Vec<FileClassification>, Vec<String>)> {
        use crate::llm::{check_claude_cli, execute_claude};

        let mut warnings = Vec::new();

        if !check_claude_cli() {
            warnings.push("Claude CLI not found, falling back to glob classification".to_string());
            return Ok((self.classify_by_glob(files)?, warnings));
        }

        let mut category_descriptions = String::new();
        for cat in self.store.all() {
            category_descriptions.push_str(&format!("- **{}**: {}\n", cat.name, cat.description));
        }

        let file_list: String = files
            .iter()
            .map(|f| format!("- {}", f.display()))
            .collect::<Vec<_>>()
            .join("\n");

        let prompt = format!(
            r#"Classify the following files into categories based on their purpose.

## Available Categories

{category_descriptions}

## Files to Classify

{file_list}

## Output Format

Output a JSON array where each element has:
- "path": the file path
- "categories": array of category names that apply
- "confidence": confidence score 0.0-1.0

Example:
```json
[
  {{"path": "skills/planning/SKILL.md", "categories": ["plan"], "confidence": 0.9}},
  {{"path": "agents/code-reviewer.md", "categories": ["review"], "confidence": 0.95}}
]
```

Output ONLY the JSON array, no other text.
"#
        );

        let output = match execute_claude(&profile.path, &prompt) {
            Ok(output) => output,
            Err(e) => {
                warnings.push(format!(
                    "LLM classification failed, falling back to glob: {}",
                    e
                ));
                return Ok((self.classify_by_glob(files)?, warnings));
            }
        };

        self.parse_llm_output_with_warnings(&output, files, warnings)
    }

    /// LLM出力をパース(警告情報付き)
    fn parse_llm_output_with_warnings(
        &self,
        output: &str,
        files: &[PathBuf],
        mut warnings: Vec<String>,
    ) -> Result<(Vec<FileClassification>, Vec<String>)> {
        let json_str = extract_json_from_output(output);

        #[derive(serde::Deserialize)]
        struct LlmClassification {
            path: String,
            categories: Vec<String>,
            confidence: Option<f32>,
        }

        let classifications: Vec<LlmClassification> = match serde_json::from_str(json_str) {
            Ok(c) => c,
            Err(e) => {
                warnings.push(format!(
                    "Failed to parse LLM output as JSON, falling back to glob: {}",
                    e
                ));
                return Ok((self.classify_by_glob(files)?, warnings));
            }
        };

        let mut results = Vec::with_capacity(files.len());
        for file in files {
            let file_str = file.to_string_lossy();
            let classification = classifications.iter().find(|c| c.path == file_str.as_ref());

            let (categories, confidence) = match classification {
                Some(c) => {
                    let valid_cats: Vec<String> = c
                        .categories
                        .iter()
                        .filter(|cat| self.store.get(cat).is_some())
                        .cloned()
                        .collect();
                    (valid_cats, c.confidence)
                }
                None => (Vec::new(), None),
            };

            results.push(FileClassification {
                path: file.clone(),
                categories,
                confidence,
            });
        }

        Ok((results, warnings))
    }
}

/// LLM出力からJSON部分を抽出
fn extract_json_from_output(output: &str) -> &str {
    if let Some(start) = output.find("```json") {
        let start = start + 7;
        if let Some(end) = output[start..].find("```") {
            return output[start..start + end].trim();
        }
    }
    if let Some(start) = output.find("```") {
        let start = start + 3;
        if let Some(end) = output[start..].find("```") {
            return output[start..start + end].trim();
        }
    }
    if let Some(start) = output.find('[') {
        if let Some(end) = output.rfind(']') {
            return &output[start..=end];
        }
    }
    output.trim()
}

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

    fn classify_paths(paths: &[&str]) -> Vec<FileClassification> {
        let store = CategoryStore::builtin();
        let classifier = CategoryClassifier::new(ClassificationMode::Glob, store).unwrap();
        let path_bufs: Vec<PathBuf> = paths.iter().map(PathBuf::from).collect();
        classifier.classify_by_glob(&path_bufs).unwrap()
    }

    fn get_categories(results: &[FileClassification], path: &str) -> Vec<String> {
        results
            .iter()
            .find(|f| f.path == PathBuf::from(path))
            .map(|f| f.categories.clone())
            .unwrap_or_default()
    }

    #[test]
    fn test_glob_matches_directory_name() {
        let results = classify_paths(&["skills/planning/SKILL.md"]);
        let cats = get_categories(&results, "skills/planning/SKILL.md");
        assert!(
            cats.contains(&"plan".to_string()),
            "Expected 'plan' in {:?}",
            cats
        );
    }

    #[test]
    fn test_glob_matches_filename() {
        let results = classify_paths(&["agents/code-reviewer.md"]);
        let cats = get_categories(&results, "agents/code-reviewer.md");
        assert!(
            cats.contains(&"review".to_string()),
            "Expected 'review' in {:?}",
            cats
        );
    }

    #[test]
    fn test_glob_matches_multiple_categories() {
        let results = classify_paths(&["skills/tdd-review/SKILL.md"]);
        let cats = get_categories(&results, "skills/tdd-review/SKILL.md");
        assert!(
            cats.contains(&"execute".to_string()),
            "Expected 'execute' in {:?}",
            cats
        );
        assert!(
            cats.contains(&"review".to_string()),
            "Expected 'review' in {:?}",
            cats
        );
    }

    #[test]
    fn test_glob_no_match() {
        let results = classify_paths(&["CLAUDE.md"]);
        let cats = get_categories(&results, "CLAUDE.md");
        assert!(
            cats.is_empty(),
            "Expected no categories for CLAUDE.md, got {:?}",
            cats
        );
    }

    #[test]
    fn test_glob_debug_category() {
        let results = classify_paths(&["agents/build-error-resolver.md"]);
        let cats = get_categories(&results, "agents/build-error-resolver.md");
        assert!(
            cats.contains(&"debug".to_string()),
            "Expected 'debug' in {:?}",
            cats
        );
    }

    #[test]
    fn test_glob_various_patterns() {
        let results = classify_paths(&[
            "skills/brainstorm/SKILL.md",
            "agents/architect.md",
            "rules/coding-style.md",
            "skills/test-runner/SKILL.md",
            "agents/troubleshoot.md",
        ]);

        assert!(
            get_categories(&results, "skills/brainstorm/SKILL.md").contains(&"plan".to_string())
        );
        assert!(get_categories(&results, "agents/architect.md").contains(&"plan".to_string()));
        assert!(get_categories(&results, "rules/coding-style.md").contains(&"execute".to_string()));
        assert!(
            get_categories(&results, "skills/test-runner/SKILL.md").contains(&"review".to_string())
        );
        assert!(get_categories(&results, "agents/troubleshoot.md").contains(&"debug".to_string()));
    }
}