cc2report 1.0.0

Intelligent work report generator for Claude Code that analyzes conversation logs using AI
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use crate::parser::Message;
use serde::Serialize;

#[derive(Debug, Clone)]
pub struct ConversationFlow {
    pub current_topic: Option<Topic>,
    pub topics: Vec<Topic>,
    pub context_stack: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct Topic {
    pub title: String,
    pub user_intent: String,
    pub steps: Vec<WorkStep>,
    pub outcome: TopicOutcome,
    pub started_at: String,
    pub completed_at: Option<String>,
    pub timestamp: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct WorkStep {
    pub description: String,
    pub details: Vec<String>,
    pub result: StepResult,
}

#[derive(Debug, Clone, Serialize)]
pub enum StepResult {
    Success(String),
    Failed(String),
    InProgress,
}

#[derive(Debug, Clone, Serialize)]
pub enum TopicOutcome {
    Completed(String),
    PartiallyCompleted(String),
    Failed(String),
    InProgress,
}

impl Default for ConversationFlow {
    fn default() -> Self {
        Self::new()
    }
}

impl ConversationFlow {
    pub fn new() -> Self {
        Self {
            current_topic: None,
            topics: Vec::new(),
            context_stack: Vec::new(),
        }
    }

    pub fn analyze_user_message(&mut self, content: &str, timestamp: &str) {
        // ユーザーの意図を理解する
        if let Some(intent) = extract_user_intent(content) {
            // 現在のトピックを完了させる
            if let Some(mut topic) = self.current_topic.take() {
                topic.completed_at = Some(timestamp.to_string());
                topic.outcome = determine_topic_outcome(&topic);
                self.topics.push(topic);
            }

            // 新しいトピックを開始
            self.current_topic = Some(Topic {
                title: generate_topic_title(&intent),
                user_intent: intent,
                steps: Vec::new(),
                outcome: TopicOutcome::InProgress,
                started_at: timestamp.to_string(),
                completed_at: None,
                timestamp: timestamp.to_string(),
            });
        }
    }

    pub fn analyze_assistant_response(&mut self, message: &Message) {
        if let Some(topic) = &mut self.current_topic {
            // アシスタントの応答から実際の作業内容を抽出
            if let Some(work_steps) = extract_work_steps(message) {
                topic.steps.extend(work_steps);
            }
        }
    }

    pub fn finalize(&mut self) {
        if let Some(mut topic) = self.current_topic.take() {
            topic.outcome = determine_topic_outcome(&topic);
            self.topics.push(topic);
        }
    }
}

fn extract_user_intent(content: &str) -> Option<String> {
    // 日本語と英語の両方に対応
    let _content_lower = content.to_lowercase();

    // 疑問文や依頼文のパターンを検出
    if content.contains("")
        || content.contains("?")
        || content.contains("してください")
        || content.contains("して欲しい")
        || content.contains("したい")
        || content.contains("please")
        || content.contains("help")
        || content.contains("can you")
        || content.contains("create")
        || content.contains("implement")
        || content.contains("fix")
        || content.contains("作って")
        || content.contains("直して")
        || content.contains("実装")
    {
        // 文章を要約して意図を抽出
        Some(summarize_intent(content))
    } else {
        None
    }
}

fn summarize_intent(content: &str) -> String {
    // 長い文章を要約
    let sentences: Vec<&str> = content.split(&['.', '', '!', ''][..]).collect();
    if let Some(first) = sentences.first() {
        let trimmed = first.trim();
        if trimmed.chars().count() > 50 {
            let chars: Vec<char> = trimmed.chars().take(50).collect();
            format!("{}...", chars.into_iter().collect::<String>())
        } else {
            trimmed.to_string()
        }
    } else {
        content.chars().take(50).collect()
    }
}

fn generate_topic_title(intent: &str) -> String {
    // 意図から簡潔なタイトルを生成
    let intent_lower = intent.to_lowercase();

    // 日本語の場合
    if intent.contains("")
        || intent.contains("実装")
        || intent.contains("修正")
        || intent.contains("追加")
        || intent.contains("改善")
        || intent.contains("解決")
    {
        // 主要な動詞と目的語を抽出
        if let Some(first_sentence) = intent.split(&['', '', '\n'][..]).next() {
            let trimmed = first_sentence.trim();
            if trimmed.chars().count() <= 30 {
                return trimmed.to_string();
            } else {
                let chars: Vec<char> = trimmed.chars().take(30).collect();
                return format!("{}...", chars.into_iter().collect::<String>());
            }
        }
    }

    // 英語の場合 - 主要な動詞を探す
    let action_words = [
        "create",
        "implement",
        "fix",
        "add",
        "update",
        "build",
        "develop",
        "analyze",
        "generate",
        "test",
        "debug",
        "refactor",
        "optimize",
        "setup",
        "configure",
        "install",
        "deploy",
        "write",
        "design",
    ];

    for action in &action_words {
        if intent_lower.contains(action) {
            // 動詞の前後の文脈を含める
            if let Some(pos) = intent_lower.find(action) {
                let start = pos.saturating_sub(10);
                let end = (pos + action.len() + 30).min(intent.len());
                let context = &intent[start..end];

                // 文の境界で切る
                if let Some(sentence_end) = context.find(&['.', '!', '?'][..]) {
                    return context[..sentence_end].trim().to_string();
                }
                return context.trim().to_string();
            }
        }
    }

    // デフォルト:最初の30文字
    if intent.chars().count() > 30 {
        let chars: Vec<char> = intent.chars().take(30).collect();
        format!("{}...", chars.into_iter().collect::<String>())
    } else {
        intent.to_string()
    }
}

fn extract_work_steps(message: &Message) -> Option<Vec<WorkStep>> {
    let mut steps = Vec::new();

    if let Some(content) = &message.content {
        if let Some(array) = content.as_array() {
            let mut current_step: Option<WorkStep> = None;

            for item in array {
                if let Some(obj) = item.as_object() {
                    match obj.get("type").and_then(|v| v.as_str()) {
                        Some("text") => {
                            if let Some(text) = obj.get("text").and_then(|v| v.as_str()) {
                                // テキストから作業内容を抽出
                                if let Some(step_desc) = extract_meaningful_action(text) {
                                    if let Some(step) = current_step.take() {
                                        steps.push(step);
                                    }
                                    current_step = Some(WorkStep {
                                        description: step_desc,
                                        details: Vec::new(),
                                        result: StepResult::InProgress,
                                    });
                                }
                            }
                        }
                        Some("tool_use") => {
                            if let Some(name) = obj.get("name").and_then(|v| v.as_str()) {
                                if let Some(input) = obj.get("input") {
                                    // ツール使用を人間が理解できる形に変換
                                    let human_readable =
                                        convert_tool_use_to_human_readable(name, input);

                                    if let Some(step) = &mut current_step {
                                        step.details.push(human_readable);
                                    } else {
                                        current_step = Some(WorkStep {
                                            description: "作業を実行中".to_string(),
                                            details: vec![human_readable],
                                            result: StepResult::InProgress,
                                        });
                                    }
                                }
                            }
                        }
                        Some("tool_result") => {
                            if let Some(step) = &mut current_step {
                                // 結果を解析
                                if let Some(output) = obj.get("output") {
                                    step.result = analyze_tool_result(output);
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }

            if let Some(step) = current_step {
                steps.push(step);
            }
        }
    }

    if steps.is_empty() {
        None
    } else {
        Some(steps)
    }
}

fn extract_meaningful_action(text: &str) -> Option<String> {
    // "I'll help you..." のような定型文を除外
    let ignore_patterns = [
        "i'll",
        "i will",
        "let me",
        "i'm going to",
        "i need to",
        "let's",
        "お手伝い",
        "させていただき",
        "しましょう",
        "します",
        "ですね",
    ];

    let text_lower = text.to_lowercase();

    // 短すぎるテキストは除外
    if text.len() < 10 {
        return None;
    }

    // 定型文で始まる場合はスキップ
    if ignore_patterns
        .iter()
        .any(|&pattern| text_lower.starts_with(pattern))
    {
        // ただし、文の後半に重要な内容がある場合は抽出
        let sentences: Vec<&str> = text.split(&['.', '', ':', ''][..]).collect();
        if sentences.len() > 1 {
            if let Some(second) = sentences.get(1) {
                let trimmed = second.trim();
                if trimmed.len() > 10 {
                    return Some(trimmed.to_string());
                }
            }
        }
    }

    // 意味のある作業を表す文を探す
    let meaningful_patterns = [
        // 英語のパターン
        "create",
        "implement",
        "fix",
        "add",
        "update",
        "modify",
        "build",
        "analyze",
        "generate",
        "test",
        "debug",
        "refactor",
        "optimize",
        "write",
        "design",
        "plan",
        "search",
        "find",
        "check",
        "verify",
        "install",
        "configure",
        "setup",
        "deploy",
        "run",
        "execute",
        "change",
        "improve",
        "enhance",
        "resolve",
        "solve",
        "handle",
        // 日本語のパターン
        "作成",
        "実装",
        "修正",
        "追加",
        "更新",
        "変更",
        "ビルド",
        "分析",
        "生成",
        "テスト",
        "デバッグ",
        "リファクタリング",
        "最適化",
        "記述",
        "設計",
        "計画",
        "検索",
        "確認",
        "検証",
        "インストール",
        "設定",
        "セットアップ",
        "デプロイ",
        "実行",
        "改善",
        "解決",
        "対応",
        "処理",
    ];

    // 最初の意味のある文を探す
    let sentences: Vec<&str> = text.split(&['.', '', '\n'][..]).collect();
    for sentence in sentences {
        let trimmed = sentence.trim();
        let sentence_lower = trimmed.to_lowercase();

        if meaningful_patterns
            .iter()
            .any(|&pattern| sentence_lower.contains(pattern))
        {
            // 文が長すぎる場合は要約
            if trimmed.chars().count() > 80 {
                let chars: Vec<char> = trimmed.chars().take(80).collect();
                return Some(format!("{}...", chars.into_iter().collect::<String>()));
            }
            return Some(trimmed.to_string());
        }
    }

    // パターンに一致しない場合でも、コロンの後の内容は重要な可能性が高い
    if text.contains(':') || text.contains('') {
        let parts: Vec<&str> = text.split(&[':', ''][..]).collect();
        if parts.len() > 1 {
            let after_colon = parts[1].trim();
            if after_colon.len() > 10 && after_colon.len() < 200 {
                return Some(after_colon.to_string());
            }
        }
    }

    None
}

fn convert_tool_use_to_human_readable(tool_name: &str, input: &serde_json::Value) -> String {
    match tool_name {
        "Read" => {
            if let Some(path) = input.get("file_path").and_then(|v| v.as_str()) {
                let filename = path.split('/').next_back().unwrap_or(path);
                format!("{filename} を読み込み")
            } else {
                "ファイルを読み込み".to_string()
            }
        }
        "Write" => {
            if let Some(path) = input.get("file_path").and_then(|v| v.as_str()) {
                let filename = path.split('/').next_back().unwrap_or(path);
                format!("{filename} を新規作成")
            } else {
                "新規ファイルを作成".to_string()
            }
        }
        "Edit" | "MultiEdit" => {
            if let Some(path) = input.get("file_path").and_then(|v| v.as_str()) {
                let filename = path.split('/').next_back().unwrap_or(path);
                format!("{filename} を編集")
            } else {
                "ファイルを編集".to_string()
            }
        }
        "Bash" => {
            if let Some(cmd) = input.get("command").and_then(|v| v.as_str()) {
                let cmd_parts: Vec<&str> = cmd.split_whitespace().collect();
                match cmd_parts.first() {
                    Some(&"cargo") if cmd_parts.get(1) == Some(&"build") => {
                        "Rustプロジェクトをビルド".to_string()
                    }
                    Some(&"cargo") if cmd_parts.get(1) == Some(&"test") => {
                        "Rustのテストを実行".to_string()
                    }
                    Some(&"cargo") if cmd_parts.get(1) == Some(&"run") => {
                        "プログラムを実行".to_string()
                    }
                    Some(&"git") if cmd_parts.get(1) == Some(&"commit") => {
                        "変更をGitにコミット".to_string()
                    }
                    Some(&"git") if cmd_parts.get(1) == Some(&"status") => {
                        "Gitステータスを確認".to_string()
                    }
                    Some(&"npm") if cmd_parts.get(1) == Some(&"install") => {
                        "npm依存関係をインストール".to_string()
                    }
                    Some(&"echo") => "メッセージを出力".to_string(),
                    Some(&"mkdir") => "ディレクトリを作成".to_string(),
                    _ => {
                        if cmd.len() > 30 {
                            format!("{} コマンドを実行", cmd_parts.first().unwrap_or(&""))
                        } else {
                            format!("{cmd}」を実行")
                        }
                    }
                }
            } else {
                "コマンドを実行".to_string()
            }
        }
        "Grep" => {
            if let Some(pattern) = input.get("pattern").and_then(|v| v.as_str()) {
                let pattern_display = if pattern.chars().count() > 20 {
                    let truncated: String = pattern.chars().take(20).collect();
                    format!("{truncated}...")
                } else {
                    pattern.to_string()
                };
                format!("{pattern_display}」を検索")
            } else {
                "コード内を検索".to_string()
            }
        }
        "TodoWrite" => "TODOリストを更新".to_string(),
        "TodoRead" => "TODOリストを確認".to_string(),
        _ => format!("{tool_name} ツールを使用"),
    }
}

fn analyze_tool_result(output: &serde_json::Value) -> StepResult {
    if let Some(output_str) = output.as_str() {
        if output_str.contains("error")
            || output_str.contains("Error")
            || output_str.contains("failed")
            || output_str.contains("Failed")
        {
            StepResult::Failed(extract_error_summary(output_str))
        } else {
            StepResult::Success("完了".to_string())
        }
    } else if let Some(obj) = output.as_object() {
        if obj.contains_key("error") {
            StepResult::Failed("エラーが発生".to_string())
        } else {
            StepResult::Success("完了".to_string())
        }
    } else {
        StepResult::Success("完了".to_string())
    }
}

fn extract_error_summary(error_text: &str) -> String {
    let lines: Vec<&str> = error_text.lines().collect();
    if let Some(first_error_line) = lines
        .iter()
        .find(|line| line.contains("error") || line.contains("Error"))
    {
        first_error_line.trim().to_string()
    } else {
        "エラーが発生しました".to_string()
    }
}

fn determine_topic_outcome(topic: &Topic) -> TopicOutcome {
    let total_steps = topic.steps.len();
    if total_steps == 0 {
        return TopicOutcome::InProgress;
    }

    let (success_count, failed_count) =
        topic
            .steps
            .iter()
            .fold((0, 0), |(succ, fail), step| match &step.result {
                StepResult::Success(_) => (succ + 1, fail),
                StepResult::Failed(_) => (succ, fail + 1),
                StepResult::InProgress => (succ, fail),
            });

    if failed_count > 0 && success_count == 0 {
        TopicOutcome::Failed("すべての作業が失敗しました".to_string())
    } else if failed_count > 0 {
        TopicOutcome::PartiallyCompleted(format!("{success_count}/{total_steps}の作業が完了"))
    } else if success_count == total_steps {
        TopicOutcome::Completed("すべての作業が正常に完了しました".to_string())
    } else {
        TopicOutcome::InProgress
    }
}