@webneat/codewiki 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,14 +1,20 @@
1
1
  # @webneat/codewiki
2
2
 
3
- Core library for parsing, analyzing, and documenting TypeScript codebases.
3
+ Core library for parsing, analyzing, and documenting TypeScript codebases with dependency tracking and topological ordering.
4
+
5
+ ## Overview
6
+
7
+ `@webneat/codewiki` is a TypeScript codebase analysis engine that builds a comprehensive knowledge graph of your code. It extracts symbols, files, directories, and dependencies, then provides topological ordering to enable AI tools to generate contextual documentation with maximum context for each entity.
4
8
 
5
9
  ## Features
6
10
 
7
- - 🔍 **TypeScript AST Parsing** - Extract elements, imports, and exports
8
- - 📊 **Dependency Analysis** - Build complete dependency graphs
9
- - 🗄️ **SQLite Storage** - Fast, portable database for codebase metadata
10
- - 🤖 **AI-Powered Descriptions** - Generate contextual documentation
11
+ - 🔍 **TypeScript AST Parsing** - Extract symbols, imports, exports, and file structure
12
+ - 📊 **Dependency Analysis** - Build complete dependency graphs between files, symbols, and external packages
13
+ - 🗄️ **SQLite Storage** - Fast, portable database with Drizzle ORM for codebase metadata
14
+ - 📝 **Description Management** - Store and manage contextual documentation with smart ordering
11
15
  - ⚡ **Incremental Updates** - Git-based change detection for fast re-indexing
16
+ - 🎯 **Topological Ordering** - Intelligent entity ordering to enable optimal AI context
17
+ - 🔗 **Relationship Tracking** - Complete import/export relationships and internal symbol links
12
18
 
13
19
  ## Installation
14
20
 
@@ -16,44 +22,187 @@ Core library for parsing, analyzing, and documenting TypeScript codebases.
16
22
  pnpm add @webneat/codewiki
17
23
  ```
18
24
 
19
- ## Usage
25
+ ## Quick Start
20
26
 
21
27
  ```typescript
22
- import { index_project, queries } from '@webneat/codewiki'
28
+ import { create, load } from '@webneat/codewiki'
23
29
 
24
- // Index a TypeScript project
25
- await index_project('/path/to/project', {
30
+ // Create a new wiki instance
31
+ const wiki = await create({
26
32
  db_path: './codewiki.db',
27
- excluded_paths: ['node_modules', 'dist'],
28
- progress_callback: (status) => {
29
- console.log(`${status.phase}: ${status.current}/${status.total}`)
30
- },
33
+ project_path: './my-typescript-project',
34
+ description: 'My awesome TypeScript project'
31
35
  })
32
36
 
33
- // Query the database
34
- const element = await queries.find_element(db, 'MyFunction')
35
- const deps = await queries.element_dependencies(db, element.id)
37
+ // Or load an existing wiki database
38
+ const wiki = await load('./codewiki.db')
39
+
40
+ // Update the wiki with latest changes
41
+ await wiki.update()
42
+
43
+ // Get counts of entities in different states
44
+ const counts = wiki.counts()
45
+ console.log(`Ready: ${counts.symbols.ready} symbols, ${counts.files.ready} files`)
46
+
47
+ // Get next entities to describe (in topological order)
48
+ const toDescribe = wiki.to_describe(10)
49
+ if (toDescribe.type === 'symbols') {
50
+ console.log(`Next symbols to describe: ${toDescribe.items.map(s => s.name).join(', ')}`)
51
+ }
36
52
  ```
37
53
 
38
- ## Architecture
54
+ ## Core API
55
+
56
+ ### CodeWiki Interface
57
+
58
+ The main interface provides access to all entity types:
59
+
60
+ ```typescript
61
+ interface CodeWiki {
62
+ readonly config: Config
63
+ update(): Promise<void> // Re-index the project
64
+ counts(): WikiCounts // Get entity counts by state
65
+ to_describe(limit: number): EntitiesToDescribe // Get next entities for AI description
66
+
67
+ files: FilesWiki // File operations
68
+ symbols: SymbolsWiki // Symbol/function/class operations
69
+ directories: DirectoriesWiki // Directory operations
70
+ dependencies: DependenciesWiki // External dependency operations
71
+ }
72
+ ```
73
+
74
+ ### Entity Operations
75
+
76
+ Each entity type provides a consistent API:
77
+
78
+ ```typescript
79
+ // Search and retrieve
80
+ const files = wiki.files.search({ where: { state: 'ready' }, limit: 10 })
81
+ const file = wiki.files.get(fileId)
82
+
83
+ // Set AI-generated descriptions
84
+ wiki.files.set_description(fileId, 'This file contains utility functions...')
85
+
86
+ // Query relationships
87
+ const importedFiles = wiki.files.imported_by_file(fileId)
88
+ const usingSymbols = wiki.symbols.using_dependency(dependencyId)
89
+ ```
90
+
91
+ ## Entity Types
92
+
93
+ ### Files
94
+ - **States**: `to_scan` → `to_describe` → `ready`
95
+ - **Tracks**: File metadata, imports, exports, symbols
96
+ - **Relationships**: Import relationships with other files and external dependencies
97
+
98
+ ### Symbols
99
+ - **States**: `to_link` → `to_describe` → `ready`
100
+ - **Tracks**: Functions, classes, variables, types
101
+ - **Relationships**: Internal symbol references and external dependency usage
102
+
103
+ ### Directories
104
+ - **States**: `to_describe` → `ready`
105
+ - **Tracks**: Directory structure and contents
106
+ - **Relationships**: Parent-child relationships and contained files
107
+
108
+ ### External Dependencies
109
+ - **States**: `to_describe` → `ready`
110
+ - **Tracks**: npm packages, built-in modules
111
+ - **Relationships**: Usage by files and symbols
112
+
113
+ ## Topological Description Order
39
114
 
40
- This is the core library used by:
41
- - **codewiki-cli** - Command-line interface with MCP server
42
- - **codewiki-vscode** - VSCode extension
115
+ The `to_describe()` method returns entities in an intelligent order that maximizes AI context:
116
+
117
+ 1. **External Dependencies** (no dependencies)
118
+ - Built-in packages first, then by usage count
119
+ 2. **Symbols** (depend on other symbols and dependencies)
120
+ - Fewest missing dependencies first, then by complexity
121
+ 3. **Files** (contain symbols and have imports)
122
+ - Fewest missing descriptions first, then by file size
123
+ 4. **Directories** (contain files and subdirectories)
124
+ - Fewest missing content first, then by total size
125
+
126
+ This ensures that when describing an entity, all its dependencies are already described.
127
+
128
+ ## CLI Usage
129
+
130
+ The package includes a comprehensive CLI:
131
+
132
+ ```bash
133
+ # Initialize and index a project
134
+ codewiki update
135
+
136
+ # Show current status
137
+ codewiki status
138
+
139
+ # Search entities
140
+ codewiki files search --where.state=ready
141
+ codewiki symbols search --where.name="myFunction"
142
+
143
+ # Set descriptions
144
+ codewiki symbols set-description 123 "This function does X..."
145
+
146
+ # Query relationships
147
+ codewiki files imported-by-file 456
148
+ codewiki dependencies used-by-file 456
149
+ ```
150
+
151
+ ## Database Schema
152
+
153
+ Uses SQLite with Drizzle ORM. Key tables:
154
+ - `files` - File metadata and state
155
+ - `symbols` - Function/class/variable definitions
156
+ - `directories` - Directory structure
157
+ - `external_dependencies` - npm packages and built-ins
158
+ - `imports` - File-to-file import relationships
159
+ - `symbol_*_links` - Symbol dependency relationships
160
+
161
+ ## Configuration
162
+
163
+ ```typescript
164
+ type Config = {
165
+ db_path: string // SQLite database file path
166
+ project_path: string // Root directory of TypeScript project
167
+ description?: string // Optional project description
168
+ package_json_path?: string // Optional package.json location
169
+ node_modules_paths?: string[] // Optional node_modules directories
170
+ }
171
+ ```
43
172
 
44
173
  ## Development
45
174
 
46
175
  ```bash
47
- # Build
176
+ # Install dependencies
177
+ pnpm install
178
+
179
+ # Build the library
48
180
  pnpm build
49
181
 
50
- # Test
182
+ # Run tests
51
183
  pnpm test
52
184
 
53
- # Type check
185
+ # Type checking
54
186
  pnpm typecheck
187
+
188
+ # Database operations
189
+ pnpm db:generate # Generate migrations
190
+ pnpm db:push # Push schema changes
191
+ pnpm db:studio # Open Drizzle Studio
55
192
  ```
56
193
 
194
+ ## Architecture
195
+
196
+ This is the core library that powers:
197
+ - **codewiki-cli** - Command-line interface and MCP server
198
+ - **codewiki-vscode** - VSCode extension (planned)
199
+
200
+ The library is designed to be:
201
+ - **Incremental** - Only processes changed files
202
+ - **Performant** - Uses SQLite and efficient AST parsing
203
+ - **Extensible** - Clean separation between parsing, storage, and API layers
204
+ - **Type-safe** - Full TypeScript support throughout
205
+
57
206
  ## License
58
207
 
59
- MIT
208
+ MIT © [Amine Ben hammou](https://github.com/webNeat)
package/dist/bin.cjs CHANGED
@@ -567,7 +567,7 @@ var symbols2 = {
567
567
  where: {}
568
568
  };
569
569
  var external_dependencies2 = {
570
- select: ["id", "package", "subpath", "name", "state", "error"],
570
+ select: ["id", "package", "subpath", "name", "is_builtin", "state", "error"],
571
571
  order_by: ["id", "asc"],
572
572
  limit: 20,
573
573
  offset: 0,
@@ -2928,12 +2928,23 @@ var describe = new import_commander3.Command("describe");
2928
2928
  describe.description("Show the next item to describe");
2929
2929
  describe.action(async () => {
2930
2930
  const wiki = await load_wiki();
2931
- const next = get_next_to_describe(wiki);
2932
- if (!next) return env.log("All items are described!");
2933
- if (next.type === "dependency") return describe_dependency(next.entity);
2934
- env.log(
2935
- `All external dependencies are described, and we don't support describing other items yet (we are implementing the right order ...). You are done for now!`
2936
- );
2931
+ const result = wiki.to_describe(1);
2932
+ if (result.type === "none") {
2933
+ return env.log("All items are described!");
2934
+ }
2935
+ if (result.type === "dependencies" && result.items.length > 0) {
2936
+ return describe_dependency(result.items[0]);
2937
+ }
2938
+ if (result.type === "symbols" && result.items.length > 0) {
2939
+ return describe_symbol(wiki, result.items[0]);
2940
+ }
2941
+ if (result.type === "files" && result.items.length > 0) {
2942
+ return describe_file(result.items[0]);
2943
+ }
2944
+ if (result.type === "directories" && result.items.length > 0) {
2945
+ return describe_directory(result.items[0]);
2946
+ }
2947
+ env.log("No items to describe.");
2937
2948
  });
2938
2949
  function describe_dependency(dep) {
2939
2950
  env.log(import_dedent2.default`
@@ -2949,40 +2960,42 @@ function describe_dependency(dep) {
2949
2960
  \`codewiki dependencies set-description ${dep.id} <markdown-file>\`
2950
2961
  `);
2951
2962
  }
2952
- function get_next_to_describe(wiki) {
2953
- const deps = wiki.dependencies.search({
2954
- where: { state: "to_describe" },
2955
- limit: 1,
2956
- select: ["id", "package", "subpath", "name", "version", "github_repo"]
2957
- });
2958
- if (deps.length > 0) {
2959
- return { type: "dependency", entity: deps[0] };
2960
- }
2961
- const symbols4 = wiki.symbols.search({
2962
- where: { state: "to_describe" },
2963
- limit: 1,
2964
- select: ["id", "name", "type", "file_id", "is_exported"]
2965
- });
2966
- if (symbols4.length > 0) {
2967
- return { type: "symbol", entity: symbols4[0] };
2968
- }
2969
- const files5 = wiki.files.search({
2970
- where: { state: "to_describe" },
2971
- limit: 1,
2972
- select: ["id", "path"]
2973
- });
2974
- if (files5.length > 0) {
2975
- return { type: "file", entity: files5[0] };
2976
- }
2977
- const directories4 = wiki.directories.search({
2978
- where: { state: "to_describe" },
2979
- limit: 1,
2980
- select: ["id", "path"]
2981
- });
2982
- if (directories4.length > 0) {
2983
- return { type: "directory", entity: directories4[0] };
2984
- }
2985
- return null;
2963
+ function describe_symbol(wiki, sym) {
2964
+ const file = wiki.files.get(sym.file_id);
2965
+ env.log(import_dedent2.default`
2966
+ Your task is to describe the following symbol:
2967
+
2968
+ name: ${sym.name}
2969
+ type: ${sym.type}
2970
+ file: ${file?.path || "unknown"}
2971
+ exported: ${sym.is_exported}
2972
+
2973
+ Write a concise developer documentation for this ${sym.type} into a markdown file, then run:
2974
+
2975
+ codewiki symbols set-description ${sym.id} <markdown-file>
2976
+ `);
2977
+ }
2978
+ function describe_file(file) {
2979
+ env.log(import_dedent2.default`
2980
+ Your task is to describe the following file:
2981
+
2982
+ path: ${file.path}
2983
+
2984
+ Write a concise description of this file's purpose and contents into a markdown file, then run:
2985
+
2986
+ codewiki files set-description ${file.id} <markdown-file>
2987
+ `);
2988
+ }
2989
+ function describe_directory(dir) {
2990
+ env.log(import_dedent2.default`
2991
+ Your task is to describe the following directory:
2992
+
2993
+ path: ${dir.path}
2994
+
2995
+ Write a concise description of this directory's purpose and contents into a markdown file, then run:
2996
+
2997
+ codewiki directories set-description ${dir.id} <markdown-file>
2998
+ `);
2986
2999
  }
2987
3000
 
2988
3001
  // src/cli/commands/files/index.ts