summaryrefslogtreecommitdiff
path: root/makima/src/llm/transcript_analyzer.rs
blob: 82aa69d940918e563b016a879dae5150d6c17a76 (plain) (blame)
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
//! Transcript analyzer for extracting requirements, decisions, and action items.

use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use crate::db::models::TranscriptEntry;

/// An extracted requirement from the transcript
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ExtractedRequirement {
    pub text: String,
    pub speaker: String,
    pub timestamp: f32,
    pub confidence: f32,
    pub category: Option<String>,  // functional, technical, non-functional, business
}

/// An extracted decision from the transcript
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ExtractedDecision {
    pub text: String,
    pub speaker: String,
    pub timestamp: f32,
    pub confidence: f32,
    pub context: Option<String>,
}

/// An extracted action item from the transcript
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ExtractedActionItem {
    pub text: String,
    pub speaker: String,
    pub timestamp: f32,
    pub assignee: Option<String>,
    pub priority: Option<String>,
}

/// Result of transcript analysis
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct TranscriptAnalysisResult {
    pub requirements: Vec<ExtractedRequirement>,
    pub decisions: Vec<ExtractedDecision>,
    pub action_items: Vec<ExtractedActionItem>,
    pub key_topics: Vec<String>,
    pub suggested_contract_name: Option<String>,
    pub suggested_description: Option<String>,
    pub speaker_summary: Vec<SpeakerStats>,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct SpeakerStats {
    pub speaker: String,
    pub word_count: usize,
    pub speaking_time_seconds: f32,
    pub contribution_percentage: f32,
}

/// Format transcript entries into readable text for LLM analysis
pub fn format_transcript_for_analysis(entries: &[TranscriptEntry]) -> String {
    entries
        .iter()
        .map(|e| format!("[{:.1}s] {}: {}", e.start, e.speaker, e.text))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Calculate speaker statistics from transcript
pub fn calculate_speaker_stats(entries: &[TranscriptEntry]) -> Vec<SpeakerStats> {
    use std::collections::HashMap;

    let mut stats: HashMap<String, (usize, f32)> = HashMap::new();

    for entry in entries {
        let word_count = entry.text.split_whitespace().count();
        let duration = entry.end - entry.start;

        let (count, time) = stats.entry(entry.speaker.clone()).or_insert((0, 0.0));
        *count += word_count;
        *time += duration;
    }

    let total_words: usize = stats.values().map(|(c, _)| c).sum();
    let total_time: f32 = stats.values().map(|(_, t)| t).sum();

    // Suppress unused variable warning
    let _ = total_time;

    stats
        .into_iter()
        .map(|(speaker, (word_count, speaking_time))| SpeakerStats {
            speaker,
            word_count,
            speaking_time_seconds: speaking_time,
            contribution_percentage: if total_words > 0 {
                (word_count as f32 / total_words as f32) * 100.0
            } else {
                0.0
            },
        })
        .collect()
}

/// Build the analysis prompt for the LLM
pub fn build_analysis_prompt(transcript_text: &str) -> String {
    format!(r#"Analyze this meeting/conversation transcript and extract structured information.

TRANSCRIPT:
{}

Extract the following information in JSON format:

1. **Requirements**: Statements where someone expresses a need, want, or must-have. Look for phrases like:
   - "we need to...", "it should...", "must have...", "requirement is..."
   - "the system should...", "users need to be able to..."

2. **Decisions**: Explicit decisions made during the conversation. Look for:
   - "let's go with...", "we decided...", "we'll use...", "agreed to..."
   - "the decision is...", "we're going with..."

3. **Action Items**: Tasks or todos mentioned. Look for:
   - "someone needs to...", "we should...", "next step is..."
   - "I'll do...", "can you...", "TODO:..."

4. **Key Topics**: Main subjects discussed

5. **Suggested Contract Name**: A short name (3-5 words) that captures the main goal

6. **Suggested Description**: A 1-2 sentence description of what should be built/done

Return your analysis as JSON with this structure:
{{
  "requirements": [
    {{"text": "...", "speaker": "Speaker X", "timestamp": 12.5, "confidence": 0.9, "category": "functional"}}
  ],
  "decisions": [
    {{"text": "...", "speaker": "Speaker X", "timestamp": 45.2, "confidence": 0.85, "context": "..."}}
  ],
  "action_items": [
    {{"text": "...", "speaker": "Speaker X", "timestamp": 78.0, "assignee": null, "priority": "high"}}
  ],
  "key_topics": ["topic1", "topic2"],
  "suggested_contract_name": "...",
  "suggested_description": "..."
}}

Be conservative - only extract items with high confidence. If nothing is found for a category, return an empty array."#, transcript_text)
}

/// Parse LLM response into analysis result
pub fn parse_analysis_response(response: &str, speaker_stats: Vec<SpeakerStats>) -> Result<TranscriptAnalysisResult, String> {
    // Try to extract JSON from the response (it might be wrapped in markdown code blocks)
    let json_str = extract_json_from_response(response)?;

    #[derive(Deserialize)]
    struct LlmResponse {
        requirements: Option<Vec<ExtractedRequirement>>,
        decisions: Option<Vec<ExtractedDecision>>,
        action_items: Option<Vec<ExtractedActionItem>>,
        key_topics: Option<Vec<String>>,
        suggested_contract_name: Option<String>,
        suggested_description: Option<String>,
    }

    let parsed: LlmResponse = serde_json::from_str(&json_str)
        .map_err(|e| format!("Failed to parse LLM response as JSON: {}", e))?;

    Ok(TranscriptAnalysisResult {
        requirements: parsed.requirements.unwrap_or_default(),
        decisions: parsed.decisions.unwrap_or_default(),
        action_items: parsed.action_items.unwrap_or_default(),
        key_topics: parsed.key_topics.unwrap_or_default(),
        suggested_contract_name: parsed.suggested_contract_name,
        suggested_description: parsed.suggested_description,
        speaker_summary: speaker_stats,
    })
}

/// Extract JSON from LLM response (handles markdown code blocks)
fn extract_json_from_response(response: &str) -> Result<String, String> {
    // Try to find JSON in code blocks first
    if let Some(start) = response.find("```json") {
        if let Some(end) = response[start..].find("```\n").or_else(|| response[start..].rfind("```")) {
            let json_start = start + 7; // Skip "```json"
            let json_end = start + end;
            if json_end > json_start {
                return Ok(response[json_start..json_end].trim().to_string());
            }
        }
    }

    // Try plain code blocks
    if let Some(start) = response.find("```") {
        let after_start = start + 3;
        if let Some(end) = response[after_start..].find("```") {
            let json_str = &response[after_start..after_start + end];
            // Skip language identifier if present
            let json_str = if let Some(newline) = json_str.find('\n') {
                &json_str[newline + 1..]
            } else {
                json_str
            };
            return Ok(json_str.trim().to_string());
        }
    }

    // Try to find raw JSON (starts with { or [)
    if let Some(start) = response.find('{') {
        if let Some(end) = response.rfind('}') {
            if end > start {
                return Ok(response[start..=end].to_string());
            }
        }
    }

    Err("Could not find JSON in LLM response".to_string())
}

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

    #[test]
    fn test_format_transcript() {
        let entries = vec![
            TranscriptEntry {
                id: "1".to_string(),
                speaker: "Speaker 0".to_string(),
                start: 0.0,
                end: 2.5,
                text: "Hello world".to_string(),
                is_final: true,
            },
        ];

        let formatted = format_transcript_for_analysis(&entries);
        assert!(formatted.contains("[0.0s] Speaker 0: Hello world"));
    }

    #[test]
    fn test_speaker_stats() {
        let entries = vec![
            TranscriptEntry {
                id: "1".to_string(),
                speaker: "Speaker 0".to_string(),
                start: 0.0,
                end: 5.0,
                text: "One two three four five".to_string(),
                is_final: true,
            },
            TranscriptEntry {
                id: "2".to_string(),
                speaker: "Speaker 1".to_string(),
                start: 5.0,
                end: 10.0,
                text: "Six seven eight nine ten".to_string(),
                is_final: true,
            },
        ];

        let stats = calculate_speaker_stats(&entries);
        assert_eq!(stats.len(), 2);

        for s in &stats {
            assert_eq!(s.word_count, 5);
            assert_eq!(s.speaking_time_seconds, 5.0);
            assert!((s.contribution_percentage - 50.0).abs() < 0.1);
        }
    }

    #[test]
    fn test_extract_json_from_response() {
        let response = r#"Here is the analysis:
```json
{"key": "value"}
```
Done."#;

        let json = extract_json_from_response(response).unwrap();
        assert_eq!(json, r#"{"key": "value"}"#);
    }

    #[test]
    fn test_extract_raw_json() {
        let response = r#"Analysis: {"key": "value"}"#;
        let json = extract_json_from_response(response).unwrap();
        assert_eq!(json, r#"{"key": "value"}"#);
    }
}