clawft-core 0.8.3

Core engine for clawft (agent loop, message bus, context management)
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
//! Semantic indexing for conversation turns.
//!
//! Indexes user + assistant message pairs as embeddings so that past
//! conversations can be searched by semantic similarity. Supports
//! both cross-session and single-session search.
//!
//! This module is gated behind the `vector-memory` feature flag.

use std::collections::HashMap;

use crate::embeddings::{Embedder, EmbeddingError};
use crate::vector_store::VectorStore;

/// A single conversation turn to be indexed.
#[derive(Debug, Clone)]
pub struct ConversationTurn {
    /// The session this turn belongs to.
    pub session_id: String,
    /// The turn number within the session (0-based).
    pub turn_id: usize,
    /// The user's message text.
    pub user_message: String,
    /// The assistant's response text.
    pub assistant_message: String,
    /// Unix timestamp (seconds since epoch).
    pub timestamp: u64,
    /// The model that generated the response.
    pub model: String,
}

/// A search result from the session indexer.
#[derive(Debug, Clone)]
pub struct TurnMatch {
    /// The session this turn belongs to.
    pub session_id: String,
    /// The turn number within the session.
    pub turn_id: usize,
    /// The user's message text.
    pub user_message: String,
    /// The assistant's response text.
    pub assistant_message: String,
    /// Cosine similarity score.
    pub score: f32,
    /// Unix timestamp of the turn.
    pub timestamp: u64,
}

/// Errors that can occur during session indexing.
#[non_exhaustive]
#[derive(Debug)]
pub enum IndexError {
    /// An embedding operation failed.
    Embedding(EmbeddingError),
}

impl std::fmt::Display for IndexError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndexError::Embedding(e) => write!(f, "index embedding error: {e}"),
        }
    }
}

impl std::error::Error for IndexError {}

impl From<EmbeddingError> for IndexError {
    fn from(e: EmbeddingError) -> Self {
        IndexError::Embedding(e)
    }
}

/// Indexes conversation turns for semantic search.
///
/// Each turn is embedded as a combination of the user and assistant messages,
/// then stored in a [`VectorStore`]. Search results can be filtered by
/// session ID.
pub struct SessionIndexer {
    store: VectorStore,
    embedder: Box<dyn Embedder>,
}

impl SessionIndexer {
    /// Create a new session indexer with the given embedder.
    pub async fn new(embedder: Box<dyn Embedder>) -> Self {
        Self {
            store: VectorStore::new(),
            embedder,
        }
    }

    /// Index a conversation turn.
    ///
    /// The embedding is generated from the concatenation of user and assistant
    /// messages: `"User: {user_message}\nAssistant: {assistant_message}"`.
    pub async fn index_turn(&mut self, turn: &ConversationTurn) -> Result<(), IndexError> {
        let combined = format!(
            "User: {}\nAssistant: {}",
            turn.user_message, turn.assistant_message
        );
        let embedding = self.embedder.embed(&combined).await?;

        let id = format!("{}:{}", turn.session_id, turn.turn_id);
        let mut metadata = HashMap::new();
        metadata.insert(
            "session_id".into(),
            serde_json::Value::String(turn.session_id.clone()),
        );
        metadata.insert(
            "turn_id".into(),
            serde_json::Value::Number(turn.turn_id.into()),
        );
        metadata.insert(
            "user_message".into(),
            serde_json::Value::String(turn.user_message.clone()),
        );
        metadata.insert(
            "assistant_message".into(),
            serde_json::Value::String(turn.assistant_message.clone()),
        );
        metadata.insert(
            "model".into(),
            serde_json::Value::String(turn.model.clone()),
        );

        self.store.add_with_timestamp(
            id,
            combined,
            embedding,
            vec![turn.session_id.clone()],
            metadata,
            turn.timestamp,
        );

        Ok(())
    }

    /// Search indexed turns by semantic similarity.
    ///
    /// If `session_id` is `Some`, results are filtered to only that session.
    /// The `top_k` parameter limits the number of results returned.
    pub async fn search_turns(
        &self,
        query: &str,
        session_id: Option<&str>,
        top_k: usize,
    ) -> Result<Vec<TurnMatch>, IndexError> {
        let query_embedding = self.embedder.embed(query).await?;

        // Search more results than needed if filtering by session,
        // to ensure we get enough matches after filtering.
        let search_k = if session_id.is_some() {
            top_k * 10
        } else {
            top_k
        };

        let results = self.store.search(&query_embedding, search_k);

        let mut matches: Vec<TurnMatch> = results
            .into_iter()
            .filter(|r| {
                if let Some(sid) = session_id {
                    r.tags.contains(&sid.to_string())
                } else {
                    true
                }
            })
            .filter_map(|r| {
                // Parse the ID to extract session_id and turn_id.
                let parts: Vec<&str> = r.id.splitn(2, ':').collect();
                if parts.len() != 2 {
                    return None;
                }
                let sid = parts[0].to_string();
                let tid: usize = parts[1].parse().ok()?;

                // Extract user and assistant messages from the combined text.
                let (user_msg, assistant_msg) = parse_combined_text(&r.text);

                Some(TurnMatch {
                    session_id: sid,
                    turn_id: tid,
                    user_message: user_msg,
                    assistant_message: assistant_msg,
                    score: r.score,
                    timestamp: r.timestamp,
                })
            })
            .take(top_k)
            .collect();

        // Already sorted by score from VectorStore::search.
        let _ = &mut matches;
        Ok(matches)
    }

    /// Return the number of indexed turns.
    pub fn len(&self) -> usize {
        self.store.len()
    }

    /// Return `true` if no turns have been indexed.
    pub fn is_empty(&self) -> bool {
        self.store.is_empty()
    }
}

/// Parse a combined "User: ...\nAssistant: ..." text back into components.
fn parse_combined_text(text: &str) -> (String, String) {
    if let Some(idx) = text.find("\nAssistant: ") {
        let user_part = &text[..idx];
        let assistant_part = &text[idx + "\nAssistant: ".len()..];
        let user_msg = user_part.strip_prefix("User: ").unwrap_or(user_part);
        (user_msg.to_string(), assistant_part.to_string())
    } else {
        (text.to_string(), String::new())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embeddings::hash_embedder::HashEmbedder;

    fn make_embedder() -> Box<dyn Embedder> {
        Box::new(HashEmbedder::new(64))
    }

    fn make_turn(
        session_id: &str,
        turn_id: usize,
        user: &str,
        assistant: &str,
    ) -> ConversationTurn {
        ConversationTurn {
            session_id: session_id.into(),
            turn_id,
            user_message: user.into(),
            assistant_message: assistant.into(),
            timestamp: 1000 + turn_id as u64,
            model: "test-model".into(),
        }
    }

    #[tokio::test]
    async fn index_and_search_single_turn() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;
        let turn = make_turn(
            "sess1",
            0,
            "What is Rust?",
            "Rust is a systems programming language",
        );
        indexer.index_turn(&turn).await.unwrap();

        let results = indexer
            .search_turns("Rust programming", None, 5)
            .await
            .unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].session_id, "sess1");
        assert_eq!(results[0].turn_id, 0);
        assert_eq!(results[0].user_message, "What is Rust?");
        assert_eq!(
            results[0].assistant_message,
            "Rust is a systems programming language"
        );
    }

    #[tokio::test]
    async fn cross_session_search() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;

        indexer
            .index_turn(&make_turn(
                "sess1",
                0,
                "What is Rust?",
                "Rust is a systems programming language",
            ))
            .await
            .unwrap();
        indexer
            .index_turn(&make_turn(
                "sess2",
                0,
                "Tell me about Python",
                "Python is an interpreted language",
            ))
            .await
            .unwrap();

        // Search across all sessions.
        let results = indexer
            .search_turns("programming language", None, 10)
            .await
            .unwrap();
        assert_eq!(results.len(), 2);
        // Both sessions should be represented.
        let session_ids: Vec<&str> = results.iter().map(|r| r.session_id.as_str()).collect();
        assert!(session_ids.contains(&"sess1"));
        assert!(session_ids.contains(&"sess2"));
    }

    #[tokio::test]
    async fn session_filtered_search() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;

        indexer
            .index_turn(&make_turn(
                "sess1",
                0,
                "What is Rust?",
                "Rust is a language",
            ))
            .await
            .unwrap();
        indexer
            .index_turn(&make_turn(
                "sess2",
                0,
                "What is Rust?",
                "Rust is a language",
            ))
            .await
            .unwrap();

        // Filter to sess1 only.
        let results = indexer
            .search_turns("Rust language", Some("sess1"), 10)
            .await
            .unwrap();
        assert!(!results.is_empty());
        for r in &results {
            assert_eq!(r.session_id, "sess1");
        }
    }

    #[tokio::test]
    async fn multiple_turns_indexed_correctly() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;

        indexer
            .index_turn(&make_turn("sess1", 0, "First question", "First answer"))
            .await
            .unwrap();
        indexer
            .index_turn(&make_turn("sess1", 1, "Second question", "Second answer"))
            .await
            .unwrap();
        indexer
            .index_turn(&make_turn("sess1", 2, "Third question", "Third answer"))
            .await
            .unwrap();

        assert_eq!(indexer.len(), 3);
        assert!(!indexer.is_empty());

        let results = indexer.search_turns("question", None, 10).await.unwrap();
        assert_eq!(results.len(), 3);
    }

    #[tokio::test]
    async fn empty_indexer_search() {
        let indexer = SessionIndexer::new(make_embedder()).await;
        let results = indexer.search_turns("anything", None, 5).await.unwrap();
        assert!(results.is_empty());
        assert!(indexer.is_empty());
        assert_eq!(indexer.len(), 0);
    }

    #[tokio::test]
    async fn search_results_sorted_by_score() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;

        indexer
            .index_turn(&make_turn(
                "s1",
                0,
                "database optimization query planning",
                "index usage and query optimization",
            ))
            .await
            .unwrap();
        indexer
            .index_turn(&make_turn(
                "s1",
                1,
                "unrelated cooking recipe pasta",
                "boil water add pasta cook 10 minutes",
            ))
            .await
            .unwrap();

        let results = indexer
            .search_turns("database query optimization", None, 2)
            .await
            .unwrap();
        assert_eq!(results.len(), 2);
        assert!(
            results[0].score >= results[1].score,
            "results should be sorted by descending score"
        );
    }

    #[test]
    fn parse_combined_text_normal() {
        let (user, assistant) = parse_combined_text("User: hello\nAssistant: hi there");
        assert_eq!(user, "hello");
        assert_eq!(assistant, "hi there");
    }

    #[test]
    fn parse_combined_text_no_assistant() {
        let (user, assistant) = parse_combined_text("just some text");
        assert_eq!(user, "just some text");
        assert!(assistant.is_empty());
    }

    #[tokio::test]
    async fn top_k_limits_results() {
        let mut indexer = SessionIndexer::new(make_embedder()).await;

        for i in 0..10 {
            indexer
                .index_turn(&make_turn(
                    "s1",
                    i,
                    &format!("question {i}"),
                    &format!("answer {i}"),
                ))
                .await
                .unwrap();
        }

        let results = indexer.search_turns("question", None, 3).await.unwrap();
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn index_error_display() {
        let err = IndexError::Embedding(EmbeddingError::Internal("test".into()));
        assert!(format!("{err}").contains("test"));
    }
}