coraline 0.8.0

Coraline: semantic code knowledge graph for faster AI-assisted development.
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
#![forbid(unsafe_code)]

//! Memory tools for MCP server.
//!
//! These tools provide access to the project-specific memory system,
//! allowing persistent knowledge storage across sessions.

use std::path::Path;

use serde_json::{Value, json};

use crate::memory::MemoryManager;
use crate::tools::{Tool, ToolError, ToolResult};

/// Tool for writing/updating memories.
pub struct WriteMemoryTool {
    manager: MemoryManager,
}

impl WriteMemoryTool {
    pub fn new(project_root: &Path) -> std::io::Result<Self> {
        Ok(Self {
            manager: MemoryManager::new(project_root)?,
        })
    }
}

impl Tool for WriteMemoryTool {
    fn name(&self) -> &'static str {
        "coraline_write_memory"
    }

    fn description(&self) -> &'static str {
        "Write or update a project memory. Memories persist across sessions and help maintain project context."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Memory name (without .md extension). Use descriptive names like 'project_overview', 'architecture_notes', etc."
                },
                "content": {
                    "type": "string",
                    "description": "Memory content in markdown format."
                }
            },
            "required": ["name", "content"]
        })
    }

    fn execute(&self, params: Value) -> ToolResult {
        let name = params
            .get("name")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'name' parameter"))?;

        let content = params
            .get("content")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'content' parameter"))?;

        let result = self
            .manager
            .write_memory(name, content)
            .map_err(|e| ToolError::internal_error(format!("Failed to write memory: {e}")))?;

        Ok(json!({ "message": result }))
    }
}

/// Tool for reading memories.
pub struct ReadMemoryTool {
    manager: MemoryManager,
}

impl ReadMemoryTool {
    pub fn new(project_root: &Path) -> std::io::Result<Self> {
        Ok(Self {
            manager: MemoryManager::new(project_root)?,
        })
    }
}

impl Tool for ReadMemoryTool {
    fn name(&self) -> &'static str {
        "coraline_read_memory"
    }

    fn description(&self) -> &'static str {
        "Read a project memory by name. Only use when the memory is relevant to the current task."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Memory name to read (without .md extension)."
                }
            },
            "required": ["name"]
        })
    }

    fn execute(&self, params: Value) -> ToolResult {
        let name = params
            .get("name")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'name' parameter"))?;

        let content = self
            .manager
            .read_memory(name)
            .map_err(|e| ToolError::internal_error(format!("Failed to read memory: {e}")))?;

        Ok(json!({ "content": content }))
    }
}

/// Tool for listing all memories.
pub struct ListMemoriesTool {
    manager: MemoryManager,
}

impl ListMemoriesTool {
    pub fn new(project_root: &Path) -> std::io::Result<Self> {
        Ok(Self {
            manager: MemoryManager::new(project_root)?,
        })
    }
}

impl Tool for ListMemoriesTool {
    fn name(&self) -> &'static str {
        "coraline_list_memories"
    }

    fn description(&self) -> &'static str {
        "List all available project memories. Use to discover what knowledge is stored."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {}
        })
    }

    fn execute(&self, _params: Value) -> ToolResult {
        let memories = self
            .manager
            .list_memories()
            .map_err(|e| ToolError::internal_error(format!("Failed to list memories: {e}")))?;

        Ok(json!({ "memories": memories }))
    }
}

/// Tool for deleting memories.
pub struct DeleteMemoryTool {
    manager: MemoryManager,
}

impl DeleteMemoryTool {
    pub fn new(project_root: &Path) -> std::io::Result<Self> {
        Ok(Self {
            manager: MemoryManager::new(project_root)?,
        })
    }
}

impl Tool for DeleteMemoryTool {
    fn name(&self) -> &'static str {
        "coraline_delete_memory"
    }

    fn description(&self) -> &'static str {
        "Delete a project memory. Only use when explicitly requested or when information is outdated."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Memory name to delete (without .md extension)."
                }
            },
            "required": ["name"]
        })
    }

    fn execute(&self, params: Value) -> ToolResult {
        let name = params
            .get("name")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'name' parameter"))?;

        let result = self
            .manager
            .delete_memory(name)
            .map_err(|e| ToolError::internal_error(format!("Failed to delete memory: {e}")))?;

        Ok(json!({ "message": result }))
    }
}

/// Tool for editing memories using regex replacement.
pub struct EditMemoryTool {
    manager: MemoryManager,
}

impl EditMemoryTool {
    pub fn new(project_root: &Path) -> std::io::Result<Self> {
        Ok(Self {
            manager: MemoryManager::new(project_root)?,
        })
    }
}

impl Tool for EditMemoryTool {
    fn name(&self) -> &'static str {
        "coraline_edit_memory"
    }

    fn description(&self) -> &'static str {
        "Edit a memory using pattern replacement. Supports both literal and regex patterns."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Memory name to edit (without .md extension)."
                },
                "pattern": {
                    "type": "string",
                    "description": "Pattern to search for (literal string or regex depending on mode)."
                },
                "replacement": {
                    "type": "string",
                    "description": "Replacement text."
                },
                "mode": {
                    "type": "string",
                    "enum": ["literal", "regex"],
                    "description": "Replacement mode: 'literal' for exact string match, 'regex' for regex pattern.",
                    "default": "literal"
                }
            },
            "required": ["name", "pattern", "replacement"]
        })
    }

    fn execute(&self, params: Value) -> ToolResult {
        let name = params
            .get("name")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'name' parameter"))?;

        let pattern = params
            .get("pattern")
            .and_then(Value::as_str)
            .ok_or_else(|| ToolError::invalid_params("Missing or invalid 'pattern' parameter"))?;

        let replacement = params
            .get("replacement")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                ToolError::invalid_params("Missing or invalid 'replacement' parameter")
            })?;

        let mode = params
            .get("mode")
            .and_then(Value::as_str)
            .unwrap_or("literal");

        // Read current content
        let content = self
            .manager
            .read_memory(name)
            .map_err(|e| ToolError::internal_error(format!("Failed to read memory: {e}")))?;

        // Handle "not found" message
        if content.contains("not found") {
            return Err(ToolError::not_found(format!("Memory '{name}' not found")));
        }

        // Perform replacement
        let new_content = match mode {
            "regex" => {
                let re = regex::Regex::new(pattern).map_err(|e| {
                    ToolError::invalid_params(format!("Invalid regex pattern: {e}"))
                })?;
                re.replace_all(&content, replacement).to_string()
            }
            "literal" => content.replace(pattern, replacement),
            _ => {
                return Err(ToolError::invalid_params(
                    "Mode must be 'literal' or 'regex'",
                ));
            }
        };

        // Write updated content
        let result = self
            .manager
            .write_memory(name, &new_content)
            .map_err(|e| ToolError::internal_error(format!("Failed to write memory: {e}")))?;

        Ok(json!({ "message": result }))
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used)]

    use super::*;
    use std::fs;
    use std::path::Path;
    use tempfile::TempDir;

    fn init_project_root(path: &Path) {
        fs::create_dir_all(path.join(".coraline"))
            .expect("Failed to initialize .coraline directory");
    }

    #[test]
    fn test_write_and_read_memory_tool() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let path_buf = temp_dir.path().to_path_buf();
        init_project_root(&path_buf);
        let write_tool = WriteMemoryTool::new(&path_buf).expect("Failed to create WriteMemoryTool");
        let read_tool = ReadMemoryTool::new(&path_buf).expect("Failed to create ReadMemoryTool");

        let params = json!({
            "name": "test_memory",
            "content": "This is a test memory"
        });

        let result = write_tool
            .execute(params)
            .expect("Failed to execute write_tool");
        assert!(
            result
                .get("message")
                .and_then(Value::as_str)
                .expect("Result should contain message string")
                .contains("written")
        );

        let params = json!({ "name": "test_memory" });
        let result = read_tool
            .execute(params)
            .expect("Failed to execute read_tool");
        assert_eq!(
            result
                .get("content")
                .and_then(Value::as_str)
                .expect("Result should contain content string"),
            "This is a test memory"
        );
    }

    #[test]
    fn test_list_memories_tool() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let path_buf = temp_dir.path().to_path_buf();
        init_project_root(&path_buf);
        let write_tool = WriteMemoryTool::new(&path_buf).expect("Failed to create WriteMemoryTool");
        let list_tool =
            ListMemoriesTool::new(&path_buf).expect("Failed to create ListMemoriesTool");

        write_tool
            .execute(json!({"name": "mem1", "content": "content1"}))
            .expect("Failed to write memory mem1");
        write_tool
            .execute(json!({"name": "mem2", "content": "content2"}))
            .expect("Failed to write memory mem2");

        let result = list_tool
            .execute(json!({}))
            .expect("Failed to execute list_tool");
        let memories = result
            .get("memories")
            .and_then(Value::as_array)
            .expect("Result should contain memories array");
        assert_eq!(memories.len(), 2);
    }

    #[test]
    fn test_delete_memory_tool() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let path_buf = temp_dir.path().to_path_buf();
        init_project_root(&path_buf);
        let write_tool = WriteMemoryTool::new(&path_buf).expect("Failed to create WriteMemoryTool");
        let delete_tool =
            DeleteMemoryTool::new(&path_buf).expect("Failed to create DeleteMemoryTool");

        write_tool
            .execute(json!({"name": "to_delete", "content": "content"}))
            .expect("Failed to write memory to_delete");

        let result = delete_tool
            .execute(json!({"name": "to_delete"}))
            .expect("Failed to execute delete_tool");
        assert!(
            result
                .get("message")
                .and_then(Value::as_str)
                .expect("Result should contain message string")
                .contains("deleted")
        );
    }

    #[test]
    fn test_edit_memory_tool_literal() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let path_buf = temp_dir.path().to_path_buf();
        init_project_root(&path_buf);
        let write_tool = WriteMemoryTool::new(&path_buf).expect("Failed to create WriteMemoryTool");
        let edit_tool = EditMemoryTool::new(&path_buf).expect("Failed to create EditMemoryTool");
        let read_tool = ReadMemoryTool::new(&path_buf).expect("Failed to create ReadMemoryTool");

        write_tool
            .execute(json!({"name": "edit_test", "content": "Hello World"}))
            .expect("Failed to write memory edit_test");

        edit_tool
            .execute(json!({
                "name": "edit_test",
                "pattern": "World",
                "replacement": "Rust",
                "mode": "literal"
            }))
            .expect("Failed to execute edit_tool");

        let result = read_tool
            .execute(json!({"name": "edit_test"}))
            .expect("Failed to execute read_tool");
        assert_eq!(
            result
                .get("content")
                .and_then(Value::as_str)
                .expect("Result should contain content string"),
            "Hello Rust"
        );
    }

    #[test]
    fn test_edit_memory_tool_regex() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory");
        let path_buf = temp_dir.path().to_path_buf();
        init_project_root(&path_buf);
        let write_tool = WriteMemoryTool::new(&path_buf).expect("Failed to create WriteMemoryTool");
        let edit_tool = EditMemoryTool::new(&path_buf).expect("Failed to create EditMemoryTool");
        let read_tool = ReadMemoryTool::new(&path_buf).expect("Failed to create ReadMemoryTool");

        write_tool
            .execute(json!({"name": "regex_test", "content": "version: 1.0.0"}))
            .expect("Failed to write memory regex_test");

        edit_tool
            .execute(json!({
                "name": "regex_test",
                "pattern": r"version: \d+\.\d+\.\d+",
                "replacement": "version: 2.0.0",
                "mode": "regex"
            }))
            .expect("Failed to execute edit_tool with regex");

        let result = read_tool
            .execute(json!({"name": "regex_test"}))
            .expect("Failed to execute read_tool");
        assert_eq!(
            result
                .get("content")
                .and_then(Value::as_str)
                .expect("Result should contain content string"),
            "version: 2.0.0"
        );
    }
}