zeph-acp 0.18.6

ACP (Agent Client Protocol) server for IDE embedding
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Lightweight LSP-compatible types for ACP extension methods.
//!
//! Positions use 1-based coordinates throughout (ACP/MCP convention).
//! The ACP client (IDE) is responsible for converting to 0-based LSP coordinates.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// 1-based file position (line and character offset).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LspPosition {
    /// 1-based line number.
    pub line: u32,
    /// 1-based character offset.
    pub character: u32,
}

/// A range defined by two positions in a document.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LspRange {
    pub start: LspPosition,
    pub end: LspPosition,
}

/// A location inside a resource (file URI + range).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspLocation {
    /// File URI, e.g. `file:///path/to/file.rs`.
    pub uri: String,
    pub range: LspRange,
}

/// Diagnostic severity values as defined by the LSP specification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "u8", into = "u8")]
pub enum LspDiagnosticSeverity {
    Error = 1,
    Warning = 2,
    Info = 3,
    Hint = 4,
}

impl From<LspDiagnosticSeverity> for u8 {
    fn from(s: LspDiagnosticSeverity) -> u8 {
        s as u8
    }
}

impl TryFrom<u8> for LspDiagnosticSeverity {
    type Error = String;

    fn try_from(v: u8) -> Result<Self, String> {
        match v {
            1 => Ok(Self::Error),
            2 => Ok(Self::Warning),
            3 => Ok(Self::Info),
            4 => Ok(Self::Hint),
            _ => Err(format!("unknown diagnostic severity: {v}")),
        }
    }
}

impl std::fmt::Display for LspDiagnosticSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Error => write!(f, "error"),
            Self::Warning => write!(f, "warning"),
            Self::Info => write!(f, "info"),
            Self::Hint => write!(f, "hint"),
        }
    }
}

/// A compiler or linter diagnostic.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspDiagnostic {
    pub range: LspRange,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub severity: Option<LspDiagnosticSeverity>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// Diagnostic source, e.g. `"rust-analyzer"` or `"clippy"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    pub message: String,
}

/// LSP `SymbolKind` values (matches the LSP specification integer encoding).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "u8", into = "u8")]
pub enum LspSymbolKind {
    File = 1,
    Module = 2,
    Namespace = 3,
    Package = 4,
    Class = 5,
    Method = 6,
    Property = 7,
    Field = 8,
    Constructor = 9,
    Enum = 10,
    Interface = 11,
    Function = 12,
    Variable = 13,
    Constant = 14,
    String = 15,
    Number = 16,
    Boolean = 17,
    Array = 18,
    Object = 19,
    Key = 20,
    Null = 21,
    EnumMember = 22,
    Struct = 23,
    Event = 24,
    Operator = 25,
    TypeParameter = 26,
}

impl From<LspSymbolKind> for u8 {
    fn from(k: LspSymbolKind) -> u8 {
        k as u8
    }
}

impl TryFrom<u8> for LspSymbolKind {
    type Error = String;

    fn try_from(v: u8) -> Result<Self, String> {
        match v {
            1 => Ok(Self::File),
            2 => Ok(Self::Module),
            3 => Ok(Self::Namespace),
            4 => Ok(Self::Package),
            5 => Ok(Self::Class),
            6 => Ok(Self::Method),
            7 => Ok(Self::Property),
            8 => Ok(Self::Field),
            9 => Ok(Self::Constructor),
            10 => Ok(Self::Enum),
            11 => Ok(Self::Interface),
            12 => Ok(Self::Function),
            13 => Ok(Self::Variable),
            14 => Ok(Self::Constant),
            15 => Ok(Self::String),
            16 => Ok(Self::Number),
            17 => Ok(Self::Boolean),
            18 => Ok(Self::Array),
            19 => Ok(Self::Object),
            20 => Ok(Self::Key),
            21 => Ok(Self::Null),
            22 => Ok(Self::EnumMember),
            23 => Ok(Self::Struct),
            24 => Ok(Self::Event),
            25 => Ok(Self::Operator),
            26 => Ok(Self::TypeParameter),
            _ => Err(format!("unknown symbol kind: {v}")),
        }
    }
}

/// Hierarchical document symbol (returned by `lsp/documentSymbols`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspDocumentSymbol {
    pub name: String,
    pub kind: LspSymbolKind,
    pub range: LspRange,
    pub selection_range: LspRange,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub children: Vec<LspDocumentSymbol>,
}

/// Flat symbol information (returned by `lsp/workspaceSymbol`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspSymbolInformation {
    pub name: String,
    pub kind: LspSymbolKind,
    pub location: LspLocation,
}

/// A text edit applied to a document.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspTextEdit {
    pub range: LspRange,
    pub new_text: String,
}

/// A workspace-wide edit (uri → list of edits).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspWorkspaceEdit {
    /// Map of file URI to list of text edits.
    pub changes: HashMap<String, Vec<LspTextEdit>>,
}

/// A code action (quick fix or refactor).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspCodeAction {
    pub title: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    #[serde(default)]
    pub is_preferred: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<LspDiagnostic>,
    /// Only code actions with an edit are returned. `None` values are filtered out agent-side.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub edit: Option<LspWorkspaceEdit>,
}

/// Hover result returned by `lsp/hover`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspHoverResult {
    /// Markdown-formatted hover content (type signature, documentation).
    pub contents: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<LspRange>,
}

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

    #[test]
    fn lsp_position_round_trip() {
        let pos = LspPosition {
            line: 1,
            character: 5,
        };
        let json = serde_json::to_string(&pos).unwrap();
        let decoded: LspPosition = serde_json::from_str(&json).unwrap();
        assert_eq!(pos, decoded);
    }

    #[test]
    fn lsp_range_round_trip() {
        let range = LspRange {
            start: LspPosition {
                line: 1,
                character: 0,
            },
            end: LspPosition {
                line: 3,
                character: 10,
            },
        };
        let json = serde_json::to_string(&range).unwrap();
        let decoded: LspRange = serde_json::from_str(&json).unwrap();
        assert_eq!(range, decoded);
    }

    #[test]
    fn lsp_diagnostic_severity_round_trip() {
        for sev in [
            LspDiagnosticSeverity::Error,
            LspDiagnosticSeverity::Warning,
            LspDiagnosticSeverity::Info,
            LspDiagnosticSeverity::Hint,
        ] {
            let json = serde_json::to_string(&sev).unwrap();
            let decoded: LspDiagnosticSeverity = serde_json::from_str(&json).unwrap();
            assert_eq!(sev, decoded);
        }
    }

    #[test]
    fn lsp_diagnostic_severity_invalid_value() {
        let result: Result<LspDiagnosticSeverity, _> = serde_json::from_str("0");
        assert!(result.is_err());
        let result: Result<LspDiagnosticSeverity, _> = serde_json::from_str("5");
        assert!(result.is_err());
    }

    #[test]
    fn lsp_symbol_kind_round_trip() {
        for kind in [
            LspSymbolKind::Function,
            LspSymbolKind::Struct,
            LspSymbolKind::TypeParameter,
        ] {
            let json = serde_json::to_string(&kind).unwrap();
            let decoded: LspSymbolKind = serde_json::from_str(&json).unwrap();
            assert_eq!(kind, decoded);
        }
    }

    #[test]
    fn lsp_symbol_kind_invalid() {
        let result: Result<LspSymbolKind, _> = serde_json::from_str("0");
        assert!(result.is_err());
        let result: Result<LspSymbolKind, _> = serde_json::from_str("27");
        assert!(result.is_err());
    }

    #[test]
    fn lsp_diagnostic_round_trip() {
        let diag = LspDiagnostic {
            range: LspRange {
                start: LspPosition {
                    line: 10,
                    character: 0,
                },
                end: LspPosition {
                    line: 10,
                    character: 20,
                },
            },
            severity: Some(LspDiagnosticSeverity::Error),
            code: Some("E0001".to_owned()),
            source: Some("rust-analyzer".to_owned()),
            message: "type mismatch".to_owned(),
        };
        let json = serde_json::to_string(&diag).unwrap();
        let decoded: LspDiagnostic = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.message, "type mismatch");
        assert_eq!(decoded.severity, Some(LspDiagnosticSeverity::Error));
    }

    #[test]
    fn lsp_code_action_without_edit_serializes() {
        let action = LspCodeAction {
            title: "Remove unused import".to_owned(),
            kind: Some("quickfix".to_owned()),
            is_preferred: true,
            diagnostics: vec![],
            edit: None,
        };
        let json = serde_json::to_string(&action).unwrap();
        assert!(!json.contains("\"edit\""));
    }

    #[test]
    fn lsp_workspace_edit_round_trip() {
        let mut changes = HashMap::new();
        changes.insert(
            "file:///src/main.rs".to_owned(),
            vec![LspTextEdit {
                range: LspRange {
                    start: LspPosition {
                        line: 1,
                        character: 0,
                    },
                    end: LspPosition {
                        line: 1,
                        character: 5,
                    },
                },
                new_text: "hello".to_owned(),
            }],
        );
        let edit = LspWorkspaceEdit { changes };
        let json = serde_json::to_string(&edit).unwrap();
        let decoded: LspWorkspaceEdit = serde_json::from_str(&json).unwrap();
        assert!(decoded.changes.contains_key("file:///src/main.rs"));
    }

    #[test]
    fn lsp_hover_result_round_trip() {
        let hover = LspHoverResult {
            contents: "**fn** foo() -> i32".to_owned(),
            range: Some(LspRange {
                start: LspPosition {
                    line: 5,
                    character: 4,
                },
                end: LspPosition {
                    line: 5,
                    character: 7,
                },
            }),
        };
        let json = serde_json::to_string(&hover).unwrap();
        let decoded: LspHoverResult = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.contents, hover.contents);
    }

    #[test]
    fn lsp_document_symbol_round_trip() {
        let sym = LspDocumentSymbol {
            name: "my_fn".to_owned(),
            kind: LspSymbolKind::Function,
            range: LspRange {
                start: LspPosition {
                    line: 1,
                    character: 0,
                },
                end: LspPosition {
                    line: 5,
                    character: 1,
                },
            },
            selection_range: LspRange {
                start: LspPosition {
                    line: 1,
                    character: 3,
                },
                end: LspPosition {
                    line: 1,
                    character: 8,
                },
            },
            children: vec![],
        };
        let json = serde_json::to_string(&sym).unwrap();
        let decoded: LspDocumentSymbol = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.name, "my_fn");
        // children should be omitted when empty
        assert!(!json.contains("\"children\""));
    }
}