microclaw-engine 0.10.1

Channel-independent Agent Engine and default services for MicroClaw
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
use async_trait::async_trait;
use serde_json::json;
use std::sync::Arc;
use tracing::info;

use crate::internal::storage::db::Database;
use crate::internal::storage::db::Memory;
use crate::memory_backend::MemoryBackend;
use microclaw_core::llm_types::ToolDefinition;

use super::{auth_context_from_input, authorize_chat_access, schema_object, Tool, ToolResult};

// ── Search ────────────────────────────────────────────────────────────────────

pub struct StructuredMemorySearchTool {
    memory_backend: Arc<MemoryBackend>,
}

impl StructuredMemorySearchTool {
    pub fn new(db: Arc<Database>, memory_backend: Arc<MemoryBackend>) -> Self {
        let _ = db;
        Self { memory_backend }
    }

    fn filter_visible_memories(chat_id: i64, memories: Vec<Memory>) -> Vec<Memory> {
        memories
            .into_iter()
            .filter(|m| m.chat_id.is_none() || m.chat_id == Some(chat_id))
            .collect()
    }
}

#[async_trait]
impl Tool for StructuredMemorySearchTool {
    fn name(&self) -> &str {
        "structured_memory_search"
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "structured_memory_search".into(),
            description: "Search structured memories extracted from past conversations. Returns memories whose content contains the query string. Leave query empty to list recent visible memories.".into(),
            input_schema: schema_object(
                json!({
                    "query": {
                        "type": "string",
                        "description": "Keyword(s) to search for in memory content; leave empty to list recent visible memories"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of results to return (default 10, max 50)"
                    },
                    "include_archived": {
                        "type": "boolean",
                        "description": "Whether to include archived memories in results (default false)"
                    }
                }),
                &[],
            ),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> ToolResult {
        let query = input
            .get("query")
            .and_then(|v| v.as_str())
            .map(|q| q.trim().to_string())
            .unwrap_or_default();
        let limit = input
            .get("limit")
            .and_then(|v| v.as_u64())
            .map(|n| n.min(50) as usize)
            .unwrap_or(10);
        let include_archived = input
            .get("include_archived")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        let chat_id = auth_context_from_input(&input)
            .map(|a| a.caller_chat_id)
            .unwrap_or(0);

        info!(
            "structured_memory_search: query={query:?} chat_id={chat_id} limit={limit} include_archived={include_archived}"
        );

        let result = if query.is_empty() {
            let mut memories = match self
                .memory_backend
                .get_all_memories_for_chat(Some(chat_id))
                .await
            {
                Ok(m) => m,
                Err(e) => return ToolResult::error(format!("Search failed: {e}")),
            };
            let mut global = match self.memory_backend.get_all_memories_for_chat(None).await {
                Ok(m) => m,
                Err(e) => return ToolResult::error(format!("Search failed: {e}")),
            };
            memories.append(&mut global);
            memories.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
            if !include_archived {
                memories.retain(|m| !m.is_archived);
            }
            memories.truncate(limit);
            Ok(memories)
        } else {
            self.memory_backend
                .search_memories_with_options(chat_id, &query, limit, include_archived, true)
                .await
        };

        match result {
            Ok(memories) if memories.is_empty() => {
                let message = if query.is_empty() {
                    "No visible memories found.".to_string()
                } else {
                    "No memories found matching that query.".to_string()
                };
                ToolResult::success(message)
            }
            Ok(memories) => {
                let original_count = memories.len();
                let memories = Self::filter_visible_memories(chat_id, memories);
                if memories.is_empty() {
                    let message = if query.is_empty() {
                        "No visible memories found.".to_string()
                    } else {
                        "No visible memories found matching that query.".to_string()
                    };
                    return ToolResult::success(message);
                }
                let filtered_count = original_count.saturating_sub(memories.len());
                if filtered_count > 0 {
                    info!(
                        "structured_memory_search: filtered {} cross-chat memories for chat_id={}",
                        filtered_count, chat_id
                    );
                }
                let lines: Vec<String> = memories
                    .iter()
                    .map(|m| {
                        let scope = if m.chat_id.is_none() {
                            "global"
                        } else {
                            "chat"
                        };
                        format!("[id={}] [{}] [{}] {}", m.id, m.category, scope, m.content)
                    })
                    .collect();
                ToolResult::success(lines.join("\n"))
            }
            Err(e) => ToolResult::error(format!("Search failed: {e}")),
        }
    }
}

// ── Delete ────────────────────────────────────────────────────────────────────

pub struct StructuredMemoryDeleteTool {
    memory_backend: Arc<MemoryBackend>,
}

impl StructuredMemoryDeleteTool {
    pub fn new(db: Arc<Database>, memory_backend: Arc<MemoryBackend>) -> Self {
        let _ = db;
        Self { memory_backend }
    }
}

#[async_trait]
impl Tool for StructuredMemoryDeleteTool {
    fn name(&self) -> &str {
        "structured_memory_delete"
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "structured_memory_delete".into(),
            description: "Archive a structured memory by its id (soft delete). Use structured_memory_search first to find the id. You can only archive memories that belong to the current chat or global memories if you are a control chat.".into(),
            input_schema: schema_object(
                json!({
                    "id": {
                        "type": "integer",
                        "description": "The id of the memory to delete"
                    }
                }),
                &["id"],
            ),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> ToolResult {
        let id = match input.get("id").and_then(|v| v.as_i64()) {
            Some(id) => id,
            None => return ToolResult::error("Missing 'id' parameter".into()),
        };

        // Load memory first to check ownership
        let mem = match self.memory_backend.get_memory_by_id(id).await {
            Ok(Some(m)) => m,
            Ok(None) => return ToolResult::error(format!("Memory id={id} not found")),
            Err(e) => return ToolResult::error(format!("DB error: {e}")),
        };

        // Authorize: caller must own the chat or be a control chat; global memories only by control
        if let Some(auth) = auth_context_from_input(&input) {
            match mem.chat_id {
                Some(mem_chat_id) => {
                    if let Err(e) = authorize_chat_access(&input, mem_chat_id) {
                        return ToolResult::error(format!(
                            "{e} (memory id={id}, owner_chat_id={mem_chat_id})"
                        ));
                    }
                }
                None => {
                    // Global memory — requires control chat
                    if !auth.is_control_chat() {
                        return ToolResult::error(format!(
                            "Permission denied: only control chats can delete global memories (caller: {}, memory id={id}, owner_scope=global)",
                            auth.caller_chat_id
                        ));
                    }
                }
            }
        }

        info!("structured_memory_delete: id={id}");

        match self.memory_backend.archive_memory(id).await {
            Ok(true) => ToolResult::success(format!("Memory id={id} archived.")),
            Ok(false) => ToolResult::error(format!("Memory id={id} not found")),
            Err(e) => ToolResult::error(format!("Delete failed: {e}")),
        }
    }
}

// ── Update ────────────────────────────────────────────────────────────────────

pub struct StructuredMemoryUpdateTool {
    memory_backend: Arc<MemoryBackend>,
}

impl StructuredMemoryUpdateTool {
    pub fn new(db: Arc<Database>, memory_backend: Arc<MemoryBackend>) -> Self {
        let _ = db;
        Self { memory_backend }
    }
}

#[async_trait]
impl Tool for StructuredMemoryUpdateTool {
    fn name(&self) -> &str {
        "structured_memory_update"
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "structured_memory_update".into(),
            description: "Update the content or category of an existing structured memory. Use this to correct outdated or wrong memories instead of creating a duplicate.".into(),
            input_schema: schema_object(
                json!({
                    "id": {
                        "type": "integer",
                        "description": "The id of the memory to update"
                    },
                    "content": {
                        "type": "string",
                        "description": "New content for the memory (max 300 characters)"
                    },
                    "category": {
                        "type": "string",
                        "description": "Category: PROFILE, KNOWLEDGE, or EVENT",
                        "enum": ["PROFILE", "KNOWLEDGE", "EVENT"]
                    }
                }),
                &["id", "content"],
            ),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> ToolResult {
        let id = match input.get("id").and_then(|v| v.as_i64()) {
            Some(id) => id,
            None => return ToolResult::error("Missing 'id' parameter".into()),
        };
        let content = match input.get("content").and_then(|v| v.as_str()) {
            Some(c) if !c.trim().is_empty() => c.trim().to_string(),
            _ => return ToolResult::error("Missing or empty 'content' parameter".into()),
        };
        if content.len() > 300 {
            return ToolResult::error("Content exceeds 300 character limit".into());
        }

        // Load memory first to check ownership and get current category
        let mem = match self.memory_backend.get_memory_by_id(id).await {
            Ok(Some(m)) => m,
            Ok(None) => return ToolResult::error(format!("Memory id={id} not found")),
            Err(e) => return ToolResult::error(format!("DB error: {e}")),
        };

        // Authorize same as delete
        if let Some(auth) = auth_context_from_input(&input) {
            match mem.chat_id {
                Some(mem_chat_id) => {
                    if let Err(e) = authorize_chat_access(&input, mem_chat_id) {
                        return ToolResult::error(format!(
                            "{e} (memory id={id}, owner_chat_id={mem_chat_id})"
                        ));
                    }
                }
                None => {
                    if !auth.is_control_chat() {
                        return ToolResult::error(format!(
                            "Permission denied: only control chats can update global memories (caller: {}, memory id={id}, owner_scope=global)",
                            auth.caller_chat_id
                        ));
                    }
                }
            }
        }

        let category = input
            .get("category")
            .and_then(|v| v.as_str())
            .unwrap_or(&mem.category)
            .to_string();

        let valid_categories = ["PROFILE", "KNOWLEDGE", "EVENT"];
        if !valid_categories.contains(&category.as_str()) {
            return ToolResult::error(format!(
                "Invalid category '{category}'. Must be one of: PROFILE, KNOWLEDGE, EVENT"
            ));
        }

        info!("structured_memory_update: id={id}");

        match self
            .memory_backend
            .update_memory_content(id, &content, &category)
            .await
        {
            Ok(true) => ToolResult::success(format!("Memory id={id} updated.")),
            Ok(false) => ToolResult::error(format!("Memory id={id} not found")),
            Err(e) => ToolResult::error(format!("Update failed: {e}")),
        }
    }
}

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

    fn test_db() -> Arc<Database> {
        let dir = std::env::temp_dir().join(format!("mc_smem_{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        Arc::new(Database::new(dir.to_str().unwrap()).unwrap())
    }

    fn test_backend(db: Arc<Database>) -> Arc<MemoryBackend> {
        Arc::new(MemoryBackend::local_only(db))
    }

    #[tokio::test]
    async fn test_search_returns_results() {
        let db = test_db();
        db.insert_memory(Some(100), "User loves Rust programming", "PROFILE")
            .unwrap();
        db.insert_memory(Some(100), "User likes coffee", "PROFILE")
            .unwrap();
        let tool = StructuredMemorySearchTool::new(db.clone(), test_backend(db));
        let result = tool
            .execute(json!({
                "query": "rust",
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(!result.is_error);
        assert!(result.content.contains("Rust"));
        assert!(!result.content.contains("coffee"));
    }

    #[tokio::test]
    async fn test_search_empty_query_lists_no_visible_memories() {
        let db = test_db();
        let tool = StructuredMemorySearchTool::new(db.clone(), test_backend(db));
        let result = tool.execute(json!({"query": "  "})).await;
        assert!(!result.is_error);
        assert!(result.content.contains("No visible memories"));
    }

    #[tokio::test]
    async fn test_delete_own_chat_memory() {
        let db = test_db();
        let id = db.insert_memory(Some(100), "to delete", "EVENT").unwrap();
        let tool = StructuredMemoryDeleteTool::new(db.clone(), test_backend(db.clone()));
        let result = tool
            .execute(json!({
                "id": id,
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(!result.is_error, "{}", result.content);
        let mem = db.get_memory_by_id(id).unwrap().unwrap();
        assert!(mem.is_archived);
    }

    #[tokio::test]
    async fn test_delete_other_chat_denied() {
        let db = test_db();
        let id = db.insert_memory(Some(200), "other chat", "EVENT").unwrap();
        let tool = StructuredMemoryDeleteTool::new(db.clone(), test_backend(db));
        let result = tool
            .execute(json!({
                "id": id,
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(result.is_error);
        assert!(result.content.contains("Permission denied"));
    }

    #[tokio::test]
    async fn test_search_empty_query_lists_recent_visible_memories() {
        let db = test_db();
        db.insert_memory(Some(100), "chat memory", "PROFILE")
            .unwrap();
        db.insert_memory(None, "global memory", "KNOWLEDGE")
            .unwrap();
        let tool = StructuredMemorySearchTool::new(db.clone(), test_backend(db));
        let result = tool
            .execute(json!({
                "limit": 10,
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(!result.is_error, "{}", result.content);
        assert!(result.content.contains("chat memory"));
        assert!(result.content.contains("global memory"));
    }

    #[tokio::test]
    async fn test_update_memory() {
        let db = test_db();
        let id = db
            .insert_memory(Some(100), "User lives in Tokyo", "PROFILE")
            .unwrap();
        let tool = StructuredMemoryUpdateTool::new(db.clone(), test_backend(db.clone()));
        let result = tool
            .execute(json!({
                "id": id,
                "content": "User lives in Osaka",
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(!result.is_error, "{}", result.content);
        let mem = db.get_memory_by_id(id).unwrap().unwrap();
        assert_eq!(mem.content, "User lives in Osaka");
    }

    #[tokio::test]
    async fn test_update_content_too_long() {
        let db = test_db();
        let id = db.insert_memory(Some(100), "short", "EVENT").unwrap();
        let tool = StructuredMemoryUpdateTool::new(db.clone(), test_backend(db));
        let long = "x".repeat(301);
        let result = tool
            .execute(json!({
                "id": id,
                "content": long,
                "__microclaw_auth": {"caller_chat_id": 100, "control_chat_ids": []}
            }))
            .await;
        assert!(result.is_error);
        assert!(result.content.contains("300 character"));
    }
}