deepwiki-rs 1.3.0

deepwiki-rs(also known as Litho) is a high-performance automatic generation engine for C4 architecture documentation, developed using Rust. It can intelligently analyze project structures, identify core components, parse dependency relationships, and leverage large language models (LLMs) to automatically generate professional architecture 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
use std::{
    fmt::{Display, Formatter},
    path::PathBuf,
};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Code basic information
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct CodeDossier {
    /// Code file name
    pub name: String,
    /// File path
    pub file_path: PathBuf,
    /// Source code summary
    #[schemars(skip)]
    #[serde(default)]
    pub source_summary: String,
    /// Purpose type
    pub code_purpose: CodePurpose,
    /// Importance score
    pub importance_score: f64,
    pub description: Option<String>,
    pub functions: Vec<String>,
    /// Interfaces list
    pub interfaces: Vec<String>,
}

/// Intelligent insight information of code file
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct CodeInsight {
    /// Code basic information
    pub code_dossier: CodeDossier,
    pub detailed_description: String,
    /// Responsibilities
    pub responsibilities: Vec<String>,
    /// Contained interfaces
    pub interfaces: Vec<InterfaceInfo>,
    /// Dependency information
    pub dependencies: Vec<Dependency>,
    pub complexity_metrics: CodeComplexity,
}

/// Interface information
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct InterfaceInfo {
    pub name: String,
    pub interface_type: String, // "function", "method", "class", "trait", etc.
    pub visibility: String,     // "public", "private", "protected"
    pub parameters: Vec<ParameterInfo>,
    pub return_type: Option<String>,
    pub description: Option<String>,
}

/// Parameter information
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct ParameterInfo {
    pub name: String,
    pub param_type: String,
    pub is_optional: bool,
    pub description: Option<String>,
}

/// Dependency information
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct Dependency {
    pub name: String,
    pub path: Option<String>,
    pub is_external: bool,
    pub line_number: Option<usize>,
    pub dependency_type: String, // "import", "use", "include", "require", etc.
    pub version: Option<String>,
}

impl Display for Dependency {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            format!(
                "(name={}, path={}, is_external={},dependency_type={})",
                self.name,
                self.path.as_deref().unwrap_or_default(),
                self.is_external,
                self.dependency_type
            )
        )
    }
}

/// Component complexity metrics
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema)]
pub struct CodeComplexity {
    pub cyclomatic_complexity: f64,
    pub lines_of_code: usize,
    pub number_of_functions: usize,
    pub number_of_classes: usize,
}

/// Code functionality classification enum
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum CodePurpose {
    /// Project execution entry
    #[serde(alias = "Project execution entry")]
    Entry,
    /// Intelligent Agent
    #[serde(alias = "Intelligent Agent")]
    Agent,
    /// Frontend UI page
    #[serde(alias = "Frontend UI page")]
    Page,
    /// Frontend UI component
    #[serde(alias = "Frontend UI component")]
    Widget,
    /// Code module for implementing specific logical functionality
    #[serde(alias = "feature", alias = "specific_feature", alias = "specific-feature", alias = "Code module for implementing specific logical functionality")]
    SpecificFeature,
    /// Data type or model
    #[serde(alias = "Data type or model")]
    Model,
    /// Program internal interface definition
    #[serde(alias = "Program internal interface definition")]
    Types,
    /// Functional tool code for specific scenarios
    #[serde(alias = "Functional tool code for specific scenarios")]
    Tool,
    /// Common, basic utility functions and classes, providing low-level auxiliary functions unrelated to business logic
    #[serde(alias = "Common, basic utility functions and classes, providing low-level auxiliary functions unrelated to business logic")]
    Util,
    /// Configuration
    #[serde(alias = "configuration", alias = "Configuration")]
    Config,
    /// Middleware
    #[serde(alias = "Middleware")]
    Middleware,
    /// Plugin
    #[serde(alias = "Plugin")]
    Plugin,
    /// Router in frontend or backend system
    #[serde(alias = "Router in frontend or backend system")]
    Router,
    /// Database component
    #[serde(alias = "Database component")]
    Database,
    /// Service API for external calls, providing calling capabilities based on HTTP, RPC, IPC and other protocols.
    #[serde(alias = "Service API for external calls, providing calling capabilities based on HTTP, RPC, IPC and other protocols.")]
    Api,
    /// Controller component in MVC architecture, responsible for handling business logic
    #[serde(alias = "Controller component in MVC architecture, responsible for handling business logic")]
    Controller,
    /// Service component in MVC architecture, responsible for handling business rules
    #[serde(alias = "Service component in MVC architecture, responsible for handling business rules")]
    Service,
    /// Collection of related code (functions, classes, resources) with clear boundaries and responsibilities
    #[serde(alias = "Collection of related code (functions, classes, resources) with clear boundaries and responsibilities")]
    Module,
    /// Dependency library
    #[serde(alias = "library", alias = "package", alias = "Dependency library")]
    Lib,
    /// Test component
    #[serde(alias = "testing", alias = "tests", alias = "Test component")]
    Test,
    /// Documentation component
    #[serde(alias = "documentation", alias = "docs", alias = "Documentation component")]
    Doc,
    /// Data Access Layer component
    #[serde(alias = "Data Access Layer component")]
    Dao,
    /// Context component
    #[serde(alias = "Context component")]
    Context,
    /// command-line interface (CLI) commands or message/request handlers
    #[serde(alias = "command-line interface (CLI) commands or message/request handlers", alias = "command-line interface (CLI) commands or message/request handlers")]
    Command,
    /// Other uncategorized or unknown
    #[serde(alias = "unknown", alias = "misc", alias = "miscellaneous", alias = "Other uncategorized or unknown")]
    Other,
}

impl CodePurpose {
    /// Get component type display name
    pub fn display_name(&self) -> &'static str {
        match self {
            CodePurpose::Entry => "Project Execution Entry",
            CodePurpose::Agent => "Intelligent Agent",
            CodePurpose::Page => "Frontend UI Page",
            CodePurpose::Widget => "Frontend UI Component",
            CodePurpose::SpecificFeature => "Specific Logic Functionality",
            CodePurpose::Model => "Data Type or Model",
            CodePurpose::Util => "Basic Utility Functions",
            CodePurpose::Tool => "Functional Tool Code for Specific Scenarios",
            CodePurpose::Config => "Configuration",
            CodePurpose::Middleware => "Middleware",
            CodePurpose::Plugin => "Plugin",
            CodePurpose::Router => "Router Component",
            CodePurpose::Database => "Database Component",
            CodePurpose::Api => "Various Interface Definitions",
            CodePurpose::Controller => "Controller Component",
            CodePurpose::Service => "Service Component",
            CodePurpose::Module => "Module Component",
            CodePurpose::Lib => "Dependency Library",
            CodePurpose::Test => "Test Component",
            CodePurpose::Doc => "Documentation Component",
            CodePurpose::Other => "Other Component",
            CodePurpose::Types => "Program Interface Definition",
            CodePurpose::Dao => "Data Access Layer Component",
            CodePurpose::Context => "Context Component",
            CodePurpose::Command => "Command",
        }
    }
}

impl Display for CodePurpose {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.display_name())
    }
}

impl Default for CodePurpose {
    fn default() -> Self {
        CodePurpose::Other
    }
}

/// Component type mapper, used to map original string types to new enum types
pub struct CodePurposeMapper;

impl CodePurposeMapper {
    /// Intelligent mapping based on file path and name
    pub fn map_by_path_and_name(file_path: &str, file_name: &str) -> CodePurpose {
        let path_lower = file_path.to_lowercase();
        let name_lower = file_name.to_lowercase();

        // Extension-based mapping for SQL-related files
        if name_lower.ends_with(".sqlproj") || name_lower.ends_with(".sql") {
            return CodePurpose::Database;
        }

        // Path-based mapping
        if path_lower.contains("/pages/")
            || path_lower.contains("/views/")
            || path_lower.contains("/screens/")
        {
            return CodePurpose::Page;
        }
        if path_lower.contains("/components/")
            || path_lower.contains("/widgets/")
            || path_lower.contains("/ui/")
        {
            return CodePurpose::Widget;
        }
        if path_lower.contains("/models/")
            || path_lower.contains("/entities/")
            || path_lower.contains("/data/")
        {
            return CodePurpose::Model;
        }
        if path_lower.contains("/utils/")
            || path_lower.contains("/utilities/")
            || path_lower.contains("/helpers/")
        {
            return CodePurpose::Util;
        }
        if path_lower.contains("/config/")
            || path_lower.contains("/configs/")
            || path_lower.contains("/settings/")
        {
            return CodePurpose::Config;
        }
        if path_lower.contains("/middleware/") || path_lower.contains("/middlewares/") {
            return CodePurpose::Middleware;
        }
        if path_lower.contains("/plugin/") {
            return CodePurpose::Plugin;
        }
        if path_lower.contains("/routes/")
            || path_lower.contains("/router/")
            || path_lower.contains("/routing/")
        {
            return CodePurpose::Router;
        }
        if path_lower.contains("/database/")
            || path_lower.contains("/db/")
            || path_lower.contains("/storage/")
        {
            return CodePurpose::Database;
        }
        if path_lower.contains("/dao/")
            || path_lower.contains("/repository/")
            || path_lower.contains("/persistence/")
        {
            return CodePurpose::Dao;
        }
        if path_lower.contains("/context") || path_lower.contains("/ctx/") {
            return CodePurpose::Context;
        }
        if path_lower.contains("/api")
            || path_lower.contains("/endpoint")
            || path_lower.contains("/controller")
            || path_lower.contains("/native_module")
            || path_lower.contains("/bridge")
        {
            return CodePurpose::Api;
        }
        if path_lower.contains("/test/")
            || path_lower.contains("/tests/")
            || path_lower.contains("/__tests__/")
        {
            return CodePurpose::Test;
        }
        if path_lower.contains("/docs/")
            || path_lower.contains("/doc/")
            || path_lower.contains("/documentation/")
        {
            return CodePurpose::Doc;
        }

        // Filename-based mapping
        if name_lower.contains("main") || name_lower.contains("index") || name_lower.contains("app")
        {
            return CodePurpose::Entry;
        }
        if name_lower.contains("page")
            || name_lower.contains("view")
            || name_lower.contains("screen")
        {
            return CodePurpose::Page;
        }
        if name_lower.contains("component") || name_lower.contains("widget") {
            return CodePurpose::Widget;
        }
        if name_lower.contains("model") || name_lower.contains("entity") {
            return CodePurpose::Model;
        }
        if name_lower.contains("util") {
            return CodePurpose::Util;
        }
        if name_lower.contains("config") || name_lower.contains("setting") {
            return CodePurpose::Config;
        }
        if name_lower.contains("middleware") {
            return CodePurpose::Middleware;
        }
        if name_lower.contains("plugin") {
            return CodePurpose::Plugin;
        }
        if name_lower.contains("route") {
            return CodePurpose::Router;
        }
        if name_lower.contains("database") {
            return CodePurpose::Database;
        }
        if name_lower.contains("repository") || name_lower.contains("persistence") {
            return CodePurpose::Dao;
        }
        if name_lower.contains("context") || name_lower.contains("ctx") {
            return CodePurpose::Context;
        }
        if name_lower.contains("api") || name_lower.contains("endpoint") {
            return CodePurpose::Api;
        }
        if name_lower.contains("test") || name_lower.contains("spec") {
            return CodePurpose::Test;
        }
        if name_lower.contains("readme") || name_lower.contains("doc") {
            return CodePurpose::Doc;
        }
        if name_lower.contains("cli") || name_lower.contains("commands") {
            return CodePurpose::Command;
        }

        CodePurpose::Other
    }
}

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

    #[test]
    fn test_sql_file_classification() {
        // .sqlproj files should always be classified as Database
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/MyProject.sqlproj",
                "MyProject.sqlproj"
            ),
            CodePurpose::Database
        );

        // .sql files should always be classified as Database
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/CreateTable.sql",
                "CreateTable.sql"
            ),
            CodePurpose::Database
        );

        // Even in root directory
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/Schema.sql",
                "Schema.sql"
            ),
            CodePurpose::Database
        );

        // Even with mixed case
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/StoredProcedures.SQL",
                "StoredProcedures.SQL"
            ),
            CodePurpose::Database
        );
    }

    #[test]
    fn test_sql_file_in_database_folder() {
        // SQL files in /database/ folder should still be Database
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/database/schema.sql",
                "schema.sql"
            ),
            CodePurpose::Database
        );
    }

    #[test]
    fn test_path_based_classification() {
        // Files in /database/ folder
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/database/connection.cs",
                "connection.cs"
            ),
            CodePurpose::Database
        );

        // Files in /repository/ folder
        assert_eq!(
            CodePurposeMapper::map_by_path_and_name(
                "/src/repository/UserRepository.cs",
                "UserRepository.cs"
            ),
            CodePurpose::Dao
        );
    }
}