matrixcode-core 0.4.8

MatrixCode Agent Core - Pure logic, no UI
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
// ============================================================================
// IPC Protocol for VSCode Extension Integration
// ============================================================================
// 
// This module defines the message format for communication between
// the VSCode extension and the MatrixCode CLI daemon.
//
// Communication flow:
// 1. VSCode extension spawns CLI with --daemon --json flags
// 2. Extension sends JSON requests via stdin
// 3. CLI streams JSON events via stdout (JSON Lines format)
//
// Example request:
//   {"type":"chat","content":"帮我分析这个函数","context":{"file":"src/main.rs"}}
//
// Example response (streaming):
//   {"type":"text","content":"这是一个"}
//   {"type":"text","content":"简单的函数"}
//   {"type":"tool_use","id":"tool_1","name":"read","input":{"path":"src/main.rs"}}
//   {"type":"tool_result","tool_use_id":"tool_1","content":"..."}
//   {"type":"done","usage":{"input":1234,"output":567}}

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ============================================================================
// Client Requests (from VSCode extension)
// ============================================================================

/// Request from VSCode extension
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum ClientRequest {
    /// Chat message
    Chat {
        content: String,
        #[serde(default)]
        context: Option<RequestContext>,
    },
    
    /// Quick action on code
    QuickAction {
        action: QuickActionType,
        content: String,
        #[serde(default)]
        context: Option<RequestContext>,
        #[serde(default)]
        instructions: Option<String>,
    },
    
    /// Start a new session
    NewSession,
    
    /// Get current status
    Status,
    
    /// Memory operations
    Memory {
        operation: MemoryOperation,
    },
    
    /// Load a specific session
    LoadSession {
        session_id: String,
    },
    
    /// List sessions
    ListSessions,
}

/// Types of quick actions
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum QuickActionType {
    Explain,
    Fix,
    GenerateTests,
    Refactor,
    Optimize,
    Document,
    Translate,
}

/// Memory operations
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MemoryOperation {
    List,
    Search { query: String },
    Add { content: String, category: Option<String> },
    Clear,
    Stats,
}

/// Context information from VSCode
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct RequestContext {
    /// Workspace root path
    #[serde(default)]
    pub workspace: Option<String>,
    
    /// Current file path
    #[serde(default)]
    pub file: Option<String>,
    
    /// File language ID
    #[serde(default)]
    pub language: Option<String>,
    
    /// Selected text range
    #[serde(default)]
    pub selection: Option<Selection>,
    
    /// Diagnostics (errors, warnings) in the selection
    #[serde(default)]
    pub diagnostics: Option<Vec<Diagnostic>>,
    
    /// Additional context (e.g., related files)
    #[serde(default)]
    pub extra_files: Option<Vec<String>>,
}

/// Text selection in editor
#[derive(Debug, Serialize, Deserialize)]
pub struct Selection {
    pub start: Position,
    pub end: Position,
}

/// Position in text
#[derive(Debug, Serialize, Deserialize)]
pub struct Position {
    pub line: u32,
    pub character: u32,
}

/// Diagnostic information
#[derive(Debug, Serialize, Deserialize)]
pub struct Diagnostic {
    pub severity: String,
    pub message: String,
    pub range: Selection,
    #[serde(default)]
    pub source: Option<String>,
    #[serde(default)]
    pub code: Option<String>,
}

// ============================================================================
// Server Events (streamed to VSCode extension)
// ============================================================================

/// Event streamed to VSCode extension
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum StreamEvent {
    /// Text content (streaming)
    Text { content: String },
    
    /// Thinking content (extended thinking mode)
    Thinking { content: String },
    
    /// Tool use request
    ToolUse {
        id: String,
        name: String,
        input: serde_json::Value,
    },
    
    /// Tool execution result
    ToolResult {
        tool_use_id: String,
        content: String,
        #[serde(default)]
        success: bool,
    },
    
    /// Server-side web search result (Anthropic)
    WebSearchResult {
        tool_use_id: String,
        content: String,
    },
    
    /// Error occurred
    Error {
        message: String,
        #[serde(default)]
        code: Option<String>,
    },
    
    /// Request completed
    Done {
        #[serde(default)]
        usage: Option<Usage>,
    },
    
    /// Session started/loaded
    SessionStarted {
        session_id: String,
        #[serde(default)]
        memory_count: Option<usize>,
    },
    
    /// Status response
    StatusResponse {
        session_id: Option<String>,
        message_count: usize,
        total_tokens: u64,
        is_streaming: bool,
    },
    
    /// Memory list response
    MemoryList {
        memories: Vec<MemoryEntry>,
    },
    
    /// Memory stats response
    MemoryStats {
        total: usize,
        by_category: HashMap<String, usize>,
    },
    
    /// Session list response
    SessionList {
        sessions: Vec<SessionInfo>,
    },
    
    /// New memory added
    MemoryAdded {
        category: String,
        content: String,
    },
    
    /// Log message (for debugging)
    Log {
        level: String,
        message: String,
    },
}

/// Token usage information
#[derive(Debug, Serialize, Deserialize)]
pub struct Usage {
    pub input: u64,
    pub output: u64,
    #[serde(default)]
    pub cache_read: Option<u64>,
    #[serde(default)]
    pub cache_write: Option<u64>,
}

/// Memory entry
#[derive(Debug, Serialize, Deserialize)]
pub struct MemoryEntry {
    pub id: String,
    pub category: String,
    pub content: String,
    pub created_at: String,
    #[serde(default)]
    pub project: Option<String>,
}

/// Session information
#[derive(Debug, Serialize, Deserialize)]
pub struct SessionInfo {
    pub id: String,
    #[serde(default)]
    pub name: Option<String>,
    pub created_at: String,
    pub message_count: usize,
    #[serde(default)]
    pub last_used: Option<String>,
}

// ============================================================================
// Helper functions
// ============================================================================

impl StreamEvent {
    /// Create a text event
    pub fn text(content: impl Into<String>) -> Self {
        StreamEvent::Text { content: content.into() }
    }
    
    /// Create a thinking event
    pub fn thinking(content: impl Into<String>) -> Self {
        StreamEvent::Thinking { content: content.into() }
    }
    
    /// Create a tool use event
    pub fn tool_use(id: impl Into<String>, name: impl Into<String>, input: serde_json::Value) -> Self {
        StreamEvent::ToolUse {
            id: id.into(),
            name: name.into(),
            input,
        }
    }
    
    /// Create a tool result event
    pub fn tool_result(tool_use_id: impl Into<String>, content: impl Into<String>, success: bool) -> Self {
        StreamEvent::ToolResult {
            tool_use_id: tool_use_id.into(),
            content: content.into(),
            success,
        }
    }
    
    /// Create an error event
    pub fn error(message: impl Into<String>) -> Self {
        StreamEvent::Error { message: message.into(), code: None }
    }
    
    /// Create a done event
    pub fn done(usage: Option<Usage>) -> Self {
        StreamEvent::Done { usage }
    }
    
    /// Create a session started event
    pub fn session_started(session_id: impl Into<String>, memory_count: Option<usize>) -> Self {
        StreamEvent::SessionStarted {
            session_id: session_id.into(),
            memory_count,
        }
    }
    
    /// Serialize to JSON line
    pub fn to_json_line(&self) -> String {
        serde_json::to_string(self).unwrap_or_default() + "\n"
    }
}

impl Usage {
    /// Create usage from token counts
    pub fn new(input: u64, output: u64) -> Self {
        Usage { input, output, cache_read: None, cache_write: None }
    }
    
    /// Create usage with cache information
    pub fn with_cache(input: u64, output: u64, cache_read: u64, cache_write: u64) -> Self {
        Usage {
            input,
            output,
            cache_read: Some(cache_read),
            cache_write: Some(cache_write),
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_serialize_chat_request() {
        let request = ClientRequest::Chat {
            content: "Hello".to_string(),
            context: None,
        };
        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("\"type\":\"chat\""));
        assert!(json.contains("\"content\":\"Hello\""));
    }
    
    #[test]
    fn test_deserialize_chat_request() {
        let json = "{\"type\":\"chat\",\"content\":\"Hello\",\"context\":null}";
        let request: ClientRequest = serde_json::from_str(json).unwrap();
        match request {
            ClientRequest::Chat { content, .. } => {
                assert_eq!(content, "Hello");
            }
            _ => panic!("Expected Chat request"),
        }
    }
    
    #[test]
    fn test_serialize_stream_event() {
        let event = StreamEvent::text("Hello world");
        let json = event.to_json_line();
        assert!(json.contains("\"type\":\"text\""));
        assert!(json.contains("\"content\":\"Hello world\""));
        assert!(json.ends_with("\n"));
    }
    
    #[test]
    fn test_serialize_tool_use() {
        let event = StreamEvent::tool_use("tool_1", "read", serde_json::json!({"path": "src/main.rs"}));
        let json = event.to_json_line();
        assert!(json.contains("\"type\":\"tool_use\""));
        assert!(json.contains("\"id\":\"tool_1\""));
        assert!(json.contains("\"name\":\"read\""));
    }
    
    #[test]
    fn test_request_context_with_file() {
        let json = "{\"workspace\":\"/project\",\"file\":\"src/main.rs\",\"language\":\"rust\"}";
        let context: RequestContext = serde_json::from_str(json).unwrap();
        assert_eq!(context.workspace, Some("/project".to_string()));
        assert_eq!(context.file, Some("src/main.rs".to_string()));
        assert_eq!(context.language, Some("rust".to_string()));
    }
    
    #[test]
    fn test_quick_action_request() {
        let json = "{\"type\":\"quick_action\",\"action\":\"explain\",\"content\":\"fn main(){}\",\"context\":{\"language\":\"rust\"}}";
        let request: ClientRequest = serde_json::from_str(json).unwrap();
        match request {
            ClientRequest::QuickAction { action, content, .. } => {
                assert_eq!(action, QuickActionType::Explain);
                assert_eq!(content, "fn main(){}");
            }
            _ => panic!("Expected QuickAction request"),
        }
    }
}