codetether-agent 4.5.7

A2A-native AI coding agent for the CodeTether ecosystem
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! LSP type definitions based on LSP 3.17 specification
//!
//! These types map directly to the LSP protocol types.

use anyhow::Result;
use lsp_types::{
    ClientCapabilities, CompletionItem, DiagnosticSeverity, DocumentSymbol, Location, Position,
    Range, ServerCapabilities, SymbolInformation, TextDocumentIdentifier, TextDocumentItem,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::{info, warn};

/// LSP client configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspConfig {
    /// Command to spawn the language server
    pub command: String,
    /// Arguments to pass to the language server
    #[serde(default)]
    pub args: Vec<String>,
    /// Root URI for the workspace
    pub root_uri: Option<String>,
    /// File extensions this server handles
    #[serde(default)]
    pub file_extensions: Vec<String>,
    /// Initialization options to pass to the server
    #[serde(default)]
    pub initialization_options: Option<Value>,
    /// Timeout for requests in milliseconds
    #[serde(default = "default_timeout")]
    pub timeout_ms: u64,
}

fn default_timeout() -> u64 {
    30000
}

fn rust_timeout() -> u64 {
    120000
}

impl Default for LspConfig {
    fn default() -> Self {
        Self {
            command: String::new(),
            args: Vec::new(),
            root_uri: None,
            file_extensions: Vec::new(),
            initialization_options: None,
            timeout_ms: default_timeout(),
        }
    }
}

/// JSON-RPC request for LSP
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
    pub jsonrpc: String,
    pub id: i64,
    pub method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcRequest {
    pub fn new(id: i64, method: &str, params: Option<Value>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            method: method.to_string(),
            params,
        }
    }
}

/// JSON-RPC response for LSP
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
    pub jsonrpc: String,
    pub id: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
}

/// JSON-RPC error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
    pub code: i64,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

/// JSON-RPC notification for LSP
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification {
    pub jsonrpc: String,
    pub method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<Value>,
}

impl JsonRpcNotification {
    pub fn new(method: &str, params: Option<Value>) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            method: method.to_string(),
            params,
        }
    }
}

/// Initialize parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeParams {
    pub process_id: Option<i64>,
    pub client_info: ClientInfo,
    pub locale: Option<String>,
    pub root_path: Option<String>,
    pub root_uri: Option<String>,
    pub initialization_options: Option<Value>,
    pub capabilities: ClientCapabilities,
    pub trace: Option<String>,
    pub workspace_folders: Option<Vec<WorkspaceFolder>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClientInfo {
    pub name: String,
    pub version: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkspaceFolder {
    pub uri: String,
    pub name: String,
}

/// Initialize result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
    pub capabilities: ServerCapabilities,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_info: Option<ServerInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerInfo {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

/// DidOpenTextDocument parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidOpenTextDocumentParams {
    pub text_document: TextDocumentItem,
}

/// DidCloseTextDocument parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct DidCloseTextDocumentParams {
    pub text_document: TextDocumentIdentifier,
}

/// DidChangeTextDocument parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct DidChangeTextDocumentParams {
    pub text_document: VersionedTextDocumentIdentifier,
    pub content_changes: Vec<TextDocumentContentChangeEvent>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct VersionedTextDocumentIdentifier {
    pub uri: String,
    pub version: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct TextDocumentContentChangeEvent {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<Range>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range_length: Option<u32>,
    pub text: String,
}

/// Reference context for find references
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceContext {
    pub include_declaration: bool,
}

/// Reference parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceParams {
    pub text_document: TextDocumentIdentifier,
    pub position: Position,
    pub context: ReferenceContext,
}

/// Workspace symbol parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceSymbolParams {
    pub query: String,
}

/// LSP action result - unified response type for the tool
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum LspActionResult {
    /// Go to definition result
    Definition { locations: Vec<LocationInfo> },
    /// Find references result
    References { locations: Vec<LocationInfo> },
    /// Hover result
    Hover {
        contents: String,
        range: Option<RangeInfo>,
    },
    /// Document symbols result
    DocumentSymbols { symbols: Vec<SymbolInfo> },
    /// Workspace symbols result
    WorkspaceSymbols { symbols: Vec<SymbolInfo> },
    /// Go to implementation result
    Implementation { locations: Vec<LocationInfo> },
    /// Completion result
    Completion { items: Vec<CompletionItemInfo> },
    /// Diagnostics result
    Diagnostics { diagnostics: Vec<DiagnosticInfo> },
    /// Error result
    Error { message: String },
}

/// Simplified location info for tool output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocationInfo {
    pub uri: String,
    pub range: RangeInfo,
}

impl From<Location> for LocationInfo {
    fn from(loc: Location) -> Self {
        Self {
            uri: loc.uri.to_string(),
            range: RangeInfo::from(loc.range),
        }
    }
}

/// Simplified range info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RangeInfo {
    pub start: PositionInfo,
    pub end: PositionInfo,
}

impl From<Range> for RangeInfo {
    fn from(range: Range) -> Self {
        Self {
            start: PositionInfo::from(range.start),
            end: PositionInfo::from(range.end),
        }
    }
}

/// Simplified position info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionInfo {
    pub line: u32,
    pub character: u32,
}

impl From<Position> for PositionInfo {
    fn from(pos: Position) -> Self {
        Self {
            line: pos.line,
            character: pos.character,
        }
    }
}

/// Simplified symbol info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolInfo {
    pub name: String,
    #[serde(rename = "type")]
    pub kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub range: Option<RangeInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container_name: Option<String>,
}

impl From<DocumentSymbol> for SymbolInfo {
    fn from(sym: DocumentSymbol) -> Self {
        Self {
            name: sym.name,
            kind: format!("{:?}", sym.kind),
            detail: sym.detail,
            uri: None,
            range: Some(RangeInfo::from(sym.range)),
            container_name: None,
        }
    }
}

impl From<SymbolInformation> for SymbolInfo {
    fn from(sym: SymbolInformation) -> Self {
        Self {
            name: sym.name,
            kind: format!("{:?}", sym.kind),
            detail: None,
            uri: Some(sym.location.uri.to_string()),
            range: Some(RangeInfo::from(sym.location.range)),
            container_name: sym.container_name,
        }
    }
}

/// Simplified completion item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionItemInfo {
    pub label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub insert_text: Option<String>,
}

impl From<CompletionItem> for CompletionItemInfo {
    fn from(item: CompletionItem) -> Self {
        Self {
            label: item.label,
            kind: item.kind.map(|k| format!("{:?}", k)),
            detail: item.detail,
            documentation: item.documentation.map(|d| match d {
                lsp_types::Documentation::String(s) => s,
                lsp_types::Documentation::MarkupContent(mc) => mc.value,
            }),
            insert_text: item.insert_text,
        }
    }
}

/// Simplified diagnostic info for tool output and proactive session context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagnosticInfo {
    pub uri: String,
    pub range: RangeInfo,
    pub severity: Option<String>,
    pub code: Option<String>,
    pub source: Option<String>,
    pub message: String,
}

impl DiagnosticInfo {
    pub fn severity_rank(&self) -> u8 {
        match self.severity.as_deref() {
            Some("error") => 1,
            Some("warning") => 2,
            Some("information") => 3,
            Some("hint") => 4,
            _ => 5,
        }
    }
}

impl From<(String, lsp_types::Diagnostic)> for DiagnosticInfo {
    fn from((uri, diagnostic): (String, lsp_types::Diagnostic)) -> Self {
        let severity = diagnostic.severity.map(|severity| match severity {
            DiagnosticSeverity::ERROR => "error".to_string(),
            DiagnosticSeverity::WARNING => "warning".to_string(),
            DiagnosticSeverity::INFORMATION => "information".to_string(),
            DiagnosticSeverity::HINT => "hint".to_string(),
            _ => "unknown".to_string(),
        });

        let code = diagnostic.code.map(|code| match code {
            lsp_types::NumberOrString::Number(n) => n.to_string(),
            lsp_types::NumberOrString::String(s) => s,
        });

        Self {
            uri,
            range: RangeInfo::from(diagnostic.range),
            severity,
            code,
            source: diagnostic.source,
            message: diagnostic.message,
        }
    }
}

/// Known language server configurations
pub fn get_language_server_config(language: &str) -> Option<LspConfig> {
    match language {
        "rust" => Some(LspConfig {
            command: rust_analyzer_command(),
            args: rust_analyzer_args(),
            file_extensions: vec!["rs".to_string()],
            timeout_ms: rust_timeout(),
            ..Default::default()
        }),
        "typescript" | "javascript" => Some(LspConfig {
            command: "typescript-language-server".to_string(),
            args: vec!["--stdio".to_string()],
            file_extensions: vec![
                "ts".to_string(),
                "tsx".to_string(),
                "js".to_string(),
                "jsx".to_string(),
            ],
            ..Default::default()
        }),
        "python" => Some(LspConfig {
            command: "pylsp".to_string(),
            args: vec![],
            file_extensions: vec!["py".to_string()],
            ..Default::default()
        }),
        "go" => Some(LspConfig {
            command: "gopls".to_string(),
            args: vec!["serve".to_string()],
            file_extensions: vec!["go".to_string()],
            ..Default::default()
        }),
        "c" | "cpp" | "c++" => Some(LspConfig {
            command: "clangd".to_string(),
            args: vec![],
            file_extensions: vec![
                "c".to_string(),
                "cpp".to_string(),
                "cc".to_string(),
                "cxx".to_string(),
                "h".to_string(),
                "hpp".to_string(),
            ],
            ..Default::default()
        }),
        _ => None,
    }
}

fn rust_analyzer_command() -> String {
    if which::which("rust-analyzer").is_ok() {
        "rust-analyzer".to_string()
    } else {
        "rustup".to_string()
    }
}

fn rust_analyzer_args() -> Vec<String> {
    if which::which("rust-analyzer").is_ok() {
        Vec::new()
    } else {
        vec![
            "run".to_string(),
            "stable".to_string(),
            "rust-analyzer".to_string(),
        ]
    }
}

/// Returns the install command for a language server binary, if known.
fn install_command_for(command: &str) -> Option<&'static [&'static str]> {
    match command {
        "rust-analyzer" => Some(&["rustup", "component", "add", "rust-analyzer"]),
        "typescript-language-server" => Some(&[
            "npm",
            "install",
            "-g",
            "typescript-language-server",
            "typescript",
        ]),
        "pylsp" => Some(&["pip", "install", "--user", "python-lsp-server"]),
        "gopls" => Some(&["go", "install", "golang.org/x/tools/gopls@latest"]),
        "clangd" => None, // system package manager varies
        _ => None,
    }
}

/// Ensure a language server binary is available, installing it if possible.
pub async fn ensure_server_installed(config: &LspConfig) -> Result<()> {
    // Check if the binary is already on PATH.
    if which::which(&config.command).is_ok() {
        return Ok(());
    }

    // rust-analyzer is commonly installed via rustup but may not be visible on PATH
    // in the current process environment. Fall back to `rustup run <toolchain> rust-analyzer`.
    if config.command == "rust-analyzer" {
        let rustup_status = tokio::process::Command::new("rustup")
            .args(["run", "stable", "rust-analyzer", "--version"])
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .await;

        if let Ok(status) = rustup_status
            && status.success()
        {
            return Ok(());
        }
    }

    let Some(install_args) = install_command_for(&config.command) else {
        return Err(anyhow::anyhow!(
            "Language server '{}' not found and no auto-install available. Install it manually.",
            config.command,
        ));
    };

    info!(command = %config.command, "Language server not found, installing...");

    let output = tokio::process::Command::new(install_args[0])
        .args(&install_args[1..])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .await?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
        return Err(anyhow::anyhow!(
            "Failed to install '{}' (exit code {:?}). stdout: {} stderr: {}",
            config.command,
            output.status.code(),
            stdout,
            stderr,
        ));
    }

    // Verify installation succeeded
    if which::which(&config.command).is_err() {
        if config.command == "rust-analyzer" {
            let rustup_status = tokio::process::Command::new("rustup")
                .args(["run", "stable", "rust-analyzer", "--version"])
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .status()
                .await;
            if let Ok(status) = rustup_status
                && status.success()
            {
                info!(command = %config.command, "Language server installed and available via rustup run stable");
                return Ok(());
            }
        }
        warn!(command = %config.command, "Install succeeded but binary still not found on PATH");
    } else {
        info!(command = %config.command, "Language server installed successfully");
    }

    Ok(())
}

/// Detect language from file extension
pub fn detect_language_from_path(path: &str) -> Option<&'static str> {
    let ext = path.rsplit('.').next()?;
    match ext {
        "rs" => Some("rust"),
        "ts" | "tsx" => Some("typescript"),
        "js" | "jsx" => Some("javascript"),
        "py" => Some("python"),
        "go" => Some("go"),
        "c" => Some("c"),
        "cpp" | "cc" | "cxx" => Some("cpp"),
        "h" => Some("c"),
        "hpp" => Some("cpp"),
        _ => None,
    }
}

/// Built-in linter server configurations.
/// Returns an `LspConfig` for well-known linter language servers.
pub fn get_linter_server_config(name: &str) -> Option<LspConfig> {
    match name {
        "eslint" => Some(LspConfig {
            command: "vscode-eslint-language-server".to_string(),
            args: vec!["--stdio".to_string()],
            file_extensions: vec![
                "js".to_string(),
                "jsx".to_string(),
                "ts".to_string(),
                "tsx".to_string(),
                "mjs".to_string(),
                "cjs".to_string(),
            ],
            ..Default::default()
        }),
        "biome" => Some(LspConfig {
            command: "biome".to_string(),
            args: vec!["lsp-proxy".to_string()],
            file_extensions: vec![
                "js".to_string(),
                "jsx".to_string(),
                "ts".to_string(),
                "tsx".to_string(),
                "json".to_string(),
                "css".to_string(),
            ],
            ..Default::default()
        }),
        "ruff" => Some(LspConfig {
            command: "ruff".to_string(),
            args: vec!["server".to_string()],
            file_extensions: vec!["py".to_string(), "pyi".to_string()],
            ..Default::default()
        }),
        "stylelint" => Some(LspConfig {
            command: "stylelint-lsp".to_string(),
            args: vec!["--stdio".to_string()],
            file_extensions: vec!["css".to_string(), "scss".to_string(), "less".to_string()],
            ..Default::default()
        }),
        _ => None,
    }
}

/// Convert a config `LspServerEntry` into an `LspConfig`.
impl LspConfig {
    pub fn from_server_entry(
        entry: &crate::config::LspServerEntry,
        root_uri: Option<String>,
    ) -> Self {
        Self {
            command: entry.command.clone(),
            args: entry.args.clone(),
            root_uri,
            file_extensions: entry.file_extensions.clone(),
            initialization_options: entry.initialization_options.clone(),
            timeout_ms: entry.timeout_ms,
        }
    }

    pub fn from_linter_entry(
        name: &str,
        entry: &crate::config::LspLinterEntry,
        root_uri: Option<String>,
    ) -> Option<Self> {
        // Start from the built-in config if the linter name is known,
        // then overlay any user overrides.
        let mut base = if let Some(builtin) = get_linter_server_config(name) {
            builtin
        } else {
            // Fully custom linter — command is required.
            let command = entry.command.as_ref()?;
            LspConfig {
                command: command.clone(),
                ..Default::default()
            }
        };

        // User overrides
        if let Some(cmd) = &entry.command {
            base.command = cmd.clone();
        }
        if !entry.args.is_empty() {
            base.args = entry.args.clone();
        }
        if !entry.file_extensions.is_empty() {
            base.file_extensions = entry.file_extensions.clone();
        }
        if entry.initialization_options.is_some() {
            base.initialization_options = entry.initialization_options.clone();
        }
        base.root_uri = root_uri;
        Some(base)
    }
}

/// Returns all file extensions handled by a named linter (built-in defaults).
pub fn linter_extensions(name: &str) -> &'static [&'static str] {
    match name {
        "eslint" => &["js", "jsx", "ts", "tsx", "mjs", "cjs"],
        "biome" => &["js", "jsx", "ts", "tsx", "json", "css"],
        "ruff" => &["py", "pyi"],
        "stylelint" => &["css", "scss", "less"],
        _ => &[],
    }
}