ricecoder_external_lsp/
types.rs

1//! Core data structures for external LSP integration
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Configuration for an external LSP server
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct LspServerConfig {
9    /// Language identifier (e.g., "rust", "typescript")
10    pub language: String,
11    /// File extensions this server handles
12    pub extensions: Vec<String>,
13    /// Executable path (can use $PATH)
14    pub executable: String,
15    /// Command line arguments
16    pub args: Vec<String>,
17    /// Environment variables
18    pub env: HashMap<String, String>,
19    /// Initialization options (sent in initialize request)
20    pub init_options: Option<serde_json::Value>,
21    /// Whether this server is enabled
22    pub enabled: bool,
23    /// Timeout for requests in milliseconds
24    pub timeout_ms: u64,
25    /// Maximum restart attempts
26    pub max_restarts: u32,
27    /// Idle timeout before shutdown (0 = never)
28    pub idle_timeout_ms: u64,
29    /// Output mapping rules for transforming LSP responses
30    pub output_mapping: Option<OutputMappingConfig>,
31}
32
33/// Configuration for mapping LSP server output to ricecoder models
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct OutputMappingConfig {
36    /// Mapping rules for completion items
37    pub completion: Option<CompletionMappingRules>,
38    /// Mapping rules for diagnostics
39    pub diagnostics: Option<DiagnosticsMappingRules>,
40    /// Mapping rules for hover information
41    pub hover: Option<HoverMappingRules>,
42    /// Custom transformation functions (by name)
43    pub custom_transforms: Option<HashMap<String, String>>,
44}
45
46/// Mapping rules for completion items
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct CompletionMappingRules {
49    /// JSON path to completion items array
50    pub items_path: String,
51    /// Field mappings for each completion item
52    pub field_mappings: HashMap<String, String>,
53    /// Optional transformation function name
54    pub transform: Option<String>,
55}
56
57/// Mapping rules for diagnostics
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct DiagnosticsMappingRules {
60    /// JSON path to diagnostics array
61    pub items_path: String,
62    /// Field mappings for each diagnostic
63    pub field_mappings: HashMap<String, String>,
64    /// Optional transformation function name
65    pub transform: Option<String>,
66}
67
68/// Mapping rules for hover information
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct HoverMappingRules {
71    /// JSON path to hover content
72    pub content_path: String,
73    /// Field mappings for hover data
74    pub field_mappings: HashMap<String, String>,
75    /// Optional transformation function name
76    pub transform: Option<String>,
77}
78
79/// Registry of all configured LSP servers
80#[derive(Debug, Clone, Serialize, Deserialize, Default)]
81pub struct LspServerRegistry {
82    /// Map of language to server configurations
83    pub servers: HashMap<String, Vec<LspServerConfig>>,
84    /// Global settings
85    pub global: GlobalLspSettings,
86}
87
88/// Global LSP settings
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct GlobalLspSettings {
91    /// Maximum concurrent LSP server processes
92    pub max_processes: usize,
93    /// Default request timeout
94    pub default_timeout_ms: u64,
95    /// Enable fallback to internal providers
96    pub enable_fallback: bool,
97    /// Health check interval
98    pub health_check_interval_ms: u64,
99}
100
101impl Default for GlobalLspSettings {
102    fn default() -> Self {
103        Self {
104            max_processes: 5,
105            default_timeout_ms: 5000,
106            enable_fallback: true,
107            health_check_interval_ms: 30000,
108        }
109    }
110}
111
112
113
114/// State of an LSP client connection
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum ClientState {
117    /// Not started
118    Stopped,
119    /// Starting up
120    Starting,
121    /// Running and healthy
122    Running,
123    /// Unhealthy (failed health checks)
124    Unhealthy,
125    /// Shutting down
126    ShuttingDown,
127    /// Crashed (will attempt restart)
128    Crashed,
129}
130
131/// Health status of an LSP server
132#[derive(Debug, Clone)]
133pub enum HealthStatus {
134    /// Server is healthy
135    Healthy { latency: std::time::Duration },
136    /// Server is unhealthy
137    Unhealthy { reason: String },
138}
139
140/// Source of an LSP result
141#[derive(Debug, Clone)]
142pub enum ResultSource {
143    /// From external LSP server
144    External { server: String },
145    /// From internal fallback provider
146    Internal,
147    /// Merged from multiple sources
148    Merged { sources: Vec<String> },
149}
150
151/// Result from external LSP with source tracking
152pub struct ExternalLspResult<T> {
153    /// The result data
154    pub data: T,
155    /// Source of the result
156    pub source: ResultSource,
157    /// Time taken to get result
158    pub latency: std::time::Duration,
159}
160
161/// Configuration for merging results from multiple sources
162#[derive(Debug, Clone)]
163pub struct MergeConfig {
164    /// Include internal provider results
165    pub include_internal: bool,
166    /// Deduplicate results
167    pub deduplicate: bool,
168}
169
170impl Default for MergeConfig {
171    fn default() -> Self {
172        Self {
173            include_internal: true,
174            deduplicate: true,
175        }
176    }
177}