cognee-llm 0.1.0

LLM client abstraction (OpenAI-compatible) for the cognee AI-memory pipeline.
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
//! Recording decorator that wraps a real [`Llm`] and captures every response into
//! an [`LlmCassette`].
//!
//! Wrap any `Llm` with [`RecordingLlm`], run the pipeline once, and the decorator
//! passes each call through to the inner LLM unchanged while recording the parsed
//! response keyed by its T1 content hash. The resulting cassette can be replayed
//! (T3) to reproduce the run bit-for-bit, with no API access.
//!
//! Recording happens at the trait's single chokepoint
//! ([`Llm::create_structured_output_with_messages_raw`]) plus [`Llm::generate`] and
//! [`Llm::transcribe_image`], so the recorded `Value` is exactly what the pipeline
//! consumed. The default [`Llm::create_structured_output_raw`] is intentionally not
//! overridden — it funnels into `*_with_messages_raw`, which is already
//! intercepted, so overriding it would record twice.

use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde_json::Value;

use super::cassette::{CassetteEntry, CassetteMethod, LlmCassette, input_hash, vision_hash};
use crate::error::LlmResult;
use crate::llm_trait::Llm;
use crate::types::{GenerationOptions, GenerationResponse, Message, MessageRole};

/// Maximum length (in characters) of the recorded user-input preview.
const PREVIEW_MAX_CHARS: usize = 120;

/// A decorator that wraps an inner [`Llm`], delegates every call unchanged, and
/// records the response into a cassette keyed by the T1 content hash.
///
/// Recorded entries accumulate in memory under a [`Mutex`] and are written to disk
/// by [`flush`](RecordingLlm::flush) (also called best-effort on [`Drop`]), so a
/// long run is not slowed by per-call IO and a crash mid-run still persists what
/// was recorded so far. Identical inputs collapse to one entry (idempotent by
/// hash), which makes concurrent recording from cognify's parallel extraction safe.
pub struct RecordingLlm {
    inner: Arc<dyn Llm>,
    entries: Mutex<BTreeMap<String, CassetteEntry>>,
    path: PathBuf,
}

impl RecordingLlm {
    /// Wrap `inner` and record responses to `path`.
    ///
    /// If `path` already exists and parses as a cassette, its entries seed the
    /// in-memory map so re-recording merges into (rather than clobbers) the
    /// existing file. A missing or unparseable file is ignored — recording starts
    /// from an empty map.
    pub fn new(inner: Arc<dyn Llm>, path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        let entries = match LlmCassette::load(&path) {
            Ok(cassette) => cassette.entries,
            Err(_) => BTreeMap::new(),
        };
        Self {
            inner,
            entries: Mutex::new(entries),
            path,
        }
    }

    /// Insert a recorded entry, keyed by `hash`. Idempotent: re-recording the same
    /// hash overwrites with the (identical) latest response.
    #[allow(clippy::unwrap_used, reason = "lock poison is unrecoverable")]
    fn record(&self, hash: String, entry: CassetteEntry) {
        // lock poison is unrecoverable
        self.entries.lock().unwrap().insert(hash, entry);
    }

    /// Snapshot the recorded entries and write them to the configured path as a
    /// cassette (`model = inner.model()`, `version = 1`).
    #[allow(clippy::unwrap_used, reason = "lock poison is unrecoverable")]
    pub fn flush(&self) -> LlmResult<()> {
        // lock poison is unrecoverable
        let entries = self.entries.lock().unwrap().clone();
        let cassette = LlmCassette {
            version: 1,
            model: self.inner.model().to_string(),
            entries,
        };
        cassette.save(&self.path)
    }
}

/// Best-effort preview of the last user message, truncated to [`PREVIEW_MAX_CHARS`]
/// characters (on a char boundary).
fn user_input_preview(messages: &[Message]) -> String {
    let last_user = messages
        .iter()
        .rev()
        .find(|m| m.role == MessageRole::User)
        .map(|m| m.content.as_str())
        .unwrap_or("");
    last_user.chars().take(PREVIEW_MAX_CHARS).collect()
}

/// Best-effort schema name from the schema's `title` field, if present.
fn schema_name(schema: &Value) -> Option<String> {
    schema
        .get("title")
        .and_then(Value::as_str)
        .map(str::to_string)
}

#[async_trait]
impl Llm for RecordingLlm {
    async fn generate(
        &self,
        messages: Vec<Message>,
        options: Option<GenerationOptions>,
    ) -> LlmResult<GenerationResponse> {
        let hash = input_hash(&messages, None);
        let preview = user_input_preview(&messages);
        let response = self.inner.generate(messages, options).await?;
        self.record(
            hash,
            CassetteEntry {
                method: CassetteMethod::Generate,
                user_input_preview: preview,
                schema_name: None,
                response: Value::String(response.content.clone()),
            },
        );
        Ok(response)
    }

    async fn create_structured_output_with_messages_raw(
        &self,
        messages: Vec<Message>,
        json_schema: &Value,
        options: Option<GenerationOptions>,
    ) -> LlmResult<Value> {
        let hash = input_hash(&messages, Some(json_schema));
        let preview = user_input_preview(&messages);
        let schema_name = schema_name(json_schema);
        let response = self
            .inner
            .create_structured_output_with_messages_raw(messages, json_schema, options)
            .await?;
        self.record(
            hash,
            CassetteEntry {
                method: CassetteMethod::StructuredOutput,
                user_input_preview: preview,
                schema_name,
                response: response.clone(),
            },
        );
        Ok(response)
    }

    async fn transcribe_image(
        &self,
        image_bytes: &[u8],
        mime_type: &str,
        options: Option<GenerationOptions>,
    ) -> LlmResult<String> {
        let hash = vision_hash(image_bytes, mime_type);
        let response = self
            .inner
            .transcribe_image(image_bytes, mime_type, options)
            .await?;
        self.record(
            hash,
            CassetteEntry {
                method: CassetteMethod::TranscribeImage,
                user_input_preview: format!("[{mime_type}]"),
                schema_name: None,
                response: Value::String(response.clone()),
            },
        );
        Ok(response)
    }

    fn model(&self) -> &str {
        self.inner.model()
    }

    fn supports_streaming(&self) -> bool {
        self.inner.supports_streaming()
    }

    fn supports_function_calling(&self) -> bool {
        self.inner.supports_function_calling()
    }

    fn max_context_length(&self) -> u32 {
        self.inner.max_context_length()
    }

    fn supports_vision(&self) -> bool {
        self.inner.supports_vision()
    }
}

impl Drop for RecordingLlm {
    fn drop(&mut self) {
        // Best-effort: persist whatever was recorded even on an abnormal exit.
        // Never panic in `drop`; log and move on.
        if let Err(e) = self.flush() {
            tracing::warn!(
                error = %e,
                path = %self.path.display(),
                "RecordingLlm: failed to flush cassette on drop"
            );
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        reason = "test code — panics are acceptable"
    )]
    use super::*;
    use serde_json::json;
    use std::collections::VecDeque;

    use crate::error::LlmError;

    // A minimal in-test stub LLM. We cannot reuse `cognee_test_utils::MockLlm`
    // here: `cognee-test-utils` depends on `cognee-llm` *without* the `mock`
    // feature, so enabling `mock` on this crate's own test target builds two
    // distinct copies of `cognee-llm` (and thus two distinct `Llm` traits),
    // which fail to unify. A local stub sidesteps that dev-dependency cycle.
    struct StubLlm {
        responses: Mutex<VecDeque<String>>,
    }

    impl StubLlm {
        fn new(responses: Vec<String>) -> Self {
            Self {
                responses: Mutex::new(VecDeque::from(responses)),
            }
        }

        fn pop(&self) -> String {
            // lock poison is unrecoverable
            self.responses
                .lock()
                .unwrap()
                .pop_front()
                .unwrap_or_else(|| r#"{"nodes":[],"relationships":[]}"#.to_string())
        }
    }

    #[async_trait]
    impl Llm for StubLlm {
        async fn generate(
            &self,
            _messages: Vec<Message>,
            _options: Option<GenerationOptions>,
        ) -> LlmResult<GenerationResponse> {
            Ok(GenerationResponse {
                content: self.pop(),
                model: "stub-llm".to_string(),
                usage: None,
                finish_reason: Some("stop".to_string()),
            })
        }

        async fn create_structured_output_with_messages_raw(
            &self,
            _messages: Vec<Message>,
            _json_schema: &Value,
            _options: Option<GenerationOptions>,
        ) -> LlmResult<Value> {
            let raw = self.pop();
            serde_json::from_str(&raw)
                .map_err(|e| LlmError::DeserializationError(format!("StubLlm: invalid JSON: {e}")))
        }

        fn model(&self) -> &str {
            "stub-llm"
        }
    }

    fn graph_msgs() -> Vec<Message> {
        vec![
            Message::system("Extract a knowledge graph."),
            Message::user("Alice met Bob."),
        ]
    }

    #[tokio::test]
    async fn records_structured_output_entry() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let path = dir.path().join("cassette.json");

        let graph = json!({"nodes": [{"name": "Alice"}], "relationships": []});
        let mock: Arc<dyn Llm> = Arc::new(StubLlm::new(vec![graph.to_string()]));
        let recorder = RecordingLlm::new(mock, &path);

        let schema = json!({"title": "KnowledgeGraph", "type": "object"});
        let value = recorder
            .create_structured_output_with_messages_raw(graph_msgs(), &schema, None)
            .await
            .expect("structured output");
        assert_eq!(value, graph);

        recorder.flush().expect("flush");

        let cassette = LlmCassette::load(&path).expect("load cassette");
        assert_eq!(cassette.entries.len(), 1);
        let key = input_hash(&graph_msgs(), Some(&schema));
        let entry = cassette.entries.get(&key).expect("entry present");
        assert_eq!(entry.method, CassetteMethod::StructuredOutput);
        assert_eq!(entry.schema_name.as_deref(), Some("KnowledgeGraph"));
        assert_eq!(entry.response, graph);
        assert_eq!(entry.user_input_preview, "Alice met Bob.");
    }

    #[tokio::test]
    async fn identical_inputs_dedup_to_one_entry() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let path = dir.path().join("cassette.json");

        let graph = json!({"nodes": [], "relationships": []});
        // Two identical responses queued so both calls succeed.
        let mock: Arc<dyn Llm> = Arc::new(StubLlm::new(vec![graph.to_string(), graph.to_string()]));
        let recorder = RecordingLlm::new(mock, &path);

        let schema = json!({"title": "KnowledgeGraph", "type": "object"});
        recorder
            .create_structured_output_with_messages_raw(graph_msgs(), &schema, None)
            .await
            .expect("first call");
        recorder
            .create_structured_output_with_messages_raw(graph_msgs(), &schema, None)
            .await
            .expect("second call");

        recorder.flush().expect("flush");

        let cassette = LlmCassette::load(&path).expect("load cassette");
        assert_eq!(cassette.entries.len(), 1, "identical inputs must dedup");
    }

    #[tokio::test]
    async fn drop_flushes_without_explicit_flush() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let path = dir.path().join("cassette.json");

        {
            let mock: Arc<dyn Llm> = Arc::new(StubLlm::new(vec!["\"hello\"".to_string()]));
            let recorder = RecordingLlm::new(mock, &path);
            recorder
                .generate(graph_msgs(), None)
                .await
                .expect("generate");
            // No explicit flush; rely on Drop.
        }

        assert!(path.exists(), "cassette should exist after drop");
        let cassette = LlmCassette::load(&path).expect("cassette parses after drop");
        assert_eq!(cassette.entries.len(), 1);
        let entry = cassette
            .entries
            .values()
            .next()
            .expect("one recorded entry");
        assert_eq!(entry.method, CassetteMethod::Generate);
        assert_eq!(entry.response, Value::String("\"hello\"".to_string()));
    }

    #[tokio::test]
    async fn re_recording_merges_existing_entries() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let path = dir.path().join("cassette.json");

        let schema = json!({"title": "KnowledgeGraph", "type": "object"});

        // First recording session.
        {
            let graph = json!({"nodes": [{"name": "Alice"}], "relationships": []});
            let mock: Arc<dyn Llm> = Arc::new(StubLlm::new(vec![graph.to_string()]));
            let recorder = RecordingLlm::new(mock, &path);
            recorder
                .create_structured_output_with_messages_raw(graph_msgs(), &schema, None)
                .await
                .expect("first session");
            recorder.flush().expect("flush first");
        }

        // Second session with a different input must keep the first entry.
        {
            let other_msgs = vec![
                Message::system("Extract a knowledge graph."),
                Message::user("Carol knows Dave."),
            ];
            let graph = json!({"nodes": [{"name": "Carol"}], "relationships": []});
            let mock: Arc<dyn Llm> = Arc::new(StubLlm::new(vec![graph.to_string()]));
            let recorder = RecordingLlm::new(mock, &path);
            recorder
                .create_structured_output_with_messages_raw(other_msgs, &schema, None)
                .await
                .expect("second session");
            recorder.flush().expect("flush second");
        }

        let cassette = LlmCassette::load(&path).expect("load merged cassette");
        assert_eq!(cassette.entries.len(), 2, "re-recording must merge");
    }
}