kotadb 2.0.0

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.
Files changed (52) hide show
  1. package/README.md +79 -0
  2. package/package.json +75 -0
  3. package/src/api/auto-reindex.ts +55 -0
  4. package/src/api/openapi/builder.ts +209 -0
  5. package/src/api/openapi/paths.ts +354 -0
  6. package/src/api/openapi/schemas.ts +608 -0
  7. package/src/api/queries.ts +1168 -0
  8. package/src/api/routes.ts +339 -0
  9. package/src/auth/middleware.ts +83 -0
  10. package/src/cli.ts +221 -0
  11. package/src/config/constants.ts +96 -0
  12. package/src/config/environment.ts +96 -0
  13. package/src/config/gitignore.ts +68 -0
  14. package/src/config/index.ts +20 -0
  15. package/src/config/project-root.ts +52 -0
  16. package/src/db/client.ts +72 -0
  17. package/src/db/sqlite/index.ts +35 -0
  18. package/src/db/sqlite/jsonl-exporter.ts +416 -0
  19. package/src/db/sqlite/jsonl-importer.ts +361 -0
  20. package/src/db/sqlite/sqlite-client.ts +536 -0
  21. package/src/index.ts +66 -0
  22. package/src/indexer/ast-parser.ts +146 -0
  23. package/src/indexer/ast-types.ts +54 -0
  24. package/src/indexer/circular-detector.ts +262 -0
  25. package/src/indexer/dependency-extractor.ts +352 -0
  26. package/src/indexer/extractors.ts +54 -0
  27. package/src/indexer/import-resolver.ts +167 -0
  28. package/src/indexer/parsers.ts +177 -0
  29. package/src/indexer/reference-extractor.ts +488 -0
  30. package/src/indexer/repos.ts +245 -0
  31. package/src/indexer/storage.ts +277 -0
  32. package/src/indexer/symbol-extractor.ts +660 -0
  33. package/src/instrument.ts +88 -0
  34. package/src/logging/context.ts +46 -0
  35. package/src/logging/logger.ts +193 -0
  36. package/src/logging/middleware.ts +107 -0
  37. package/src/mcp/github-integration.ts +293 -0
  38. package/src/mcp/headers.ts +101 -0
  39. package/src/mcp/impact-analysis.ts +495 -0
  40. package/src/mcp/jsonrpc.ts +141 -0
  41. package/src/mcp/lifecycle.ts +73 -0
  42. package/src/mcp/server.ts +202 -0
  43. package/src/mcp/session.ts +44 -0
  44. package/src/mcp/spec-validation.ts +491 -0
  45. package/src/mcp/tools.ts +889 -0
  46. package/src/sync/deletion-manifest.ts +210 -0
  47. package/src/sync/index.ts +16 -0
  48. package/src/sync/merge-driver.ts +172 -0
  49. package/src/sync/watcher.ts +221 -0
  50. package/src/types/rate-limit.ts +88 -0
  51. package/src/validation/common-schemas.ts +96 -0
  52. package/src/validation/schemas.ts +187 -0
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # KotaDB Application Layer
2
+
3
+ This directory contains the TypeScript/Bun HTTP API service for indexing and searching code repositories.
4
+
5
+ ## Quick Start
6
+
7
+ ### Prerequisites
8
+
9
+ - [Bun](https://bun.sh) v1.1+
10
+
11
+ ### Install Dependencies
12
+
13
+ ```bash
14
+ bun install
15
+ ```
16
+
17
+ ### Run the Server
18
+
19
+ ```bash
20
+ # Development mode with watch
21
+ bun --watch src/index.ts
22
+
23
+ # Production mode
24
+ bun run src/index.ts
25
+
26
+ # Custom port
27
+ PORT=4000 bun run src/index.ts
28
+ ```
29
+
30
+ The server will start on port 3000 by default. Data is stored in `.kotadb/kota.db` within your project directory.
31
+
32
+ ## Development Commands
33
+
34
+ ### Type Checking and Linting
35
+
36
+ ```bash
37
+ bunx tsc --noEmit # Type-check without emitting files
38
+ bun run lint # Biome linting
39
+ ```
40
+
41
+ ### Testing
42
+
43
+ ```bash
44
+ # Run tests
45
+ bun test
46
+ ```
47
+
48
+ ## Project Structure
49
+
50
+ ```
51
+ src/
52
+ api/ # HTTP routes and database queries
53
+ db/ # SQLite client and database utilities
54
+ indexer/ # Git repository indexing logic
55
+ mcp/ # Model Context Protocol implementation
56
+ types/ # Shared TypeScript types
57
+
58
+ tests/ # Test suite
59
+ api/ # API endpoint tests
60
+ indexer/ # Indexer tests
61
+ mcp/ # MCP protocol tests
62
+ helpers/ # Test utilities
63
+
64
+ scripts/ # Bash scripts for development
65
+ ```
66
+
67
+ ## API Endpoints
68
+
69
+ - `GET /health` - Health check
70
+ - `POST /index` - Index a repository
71
+ - `GET /search?term=query` - Search indexed files
72
+ - `GET /files/recent` - List recently indexed files
73
+ - `POST /mcp` - Model Context Protocol endpoint
74
+
75
+ ## Learn More
76
+
77
+ - [Repository Overview](../README.md)
78
+ - [Architecture Documentation](../CLAUDE.md)
79
+ - [Database Schema](../docs/schema.md)
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "kotadb",
3
+ "version": "2.0.0",
4
+ "description": "Local-only code intelligence tool for CLI agents. SQLite-backed repository indexing and code search via MCP.",
5
+ "type": "module",
6
+ "module": "src/index.ts",
7
+ "bin": {
8
+ "kotadb": "./src/cli.ts"
9
+ },
10
+ "files": [
11
+ "src/**/*.ts",
12
+ "src/**/*.js",
13
+ "!src/**/*.test.ts",
14
+ "!src/**/*.spec.ts"
15
+ ],
16
+ "keywords": [
17
+ "mcp",
18
+ "code-search",
19
+ "code-intelligence",
20
+ "sqlite",
21
+ "cli",
22
+ "claude-code",
23
+ "model-context-protocol"
24
+ ],
25
+ "author": "Jaymin West",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/jayminwest/kotadb.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/jayminwest/kotadb/issues"
33
+ },
34
+ "homepage": "https://github.com/jayminwest/kotadb#readme",
35
+ "engines": {
36
+ "node": ">=18.0.0"
37
+ },
38
+ "scripts": {
39
+ "dev": "bun --watch src/index.ts",
40
+ "start": "bun run src/index.ts",
41
+ "test": "bun test --preload ./tests/setup.ts",
42
+ "check": "bun run typecheck",
43
+ "typecheck": "bunx tsc --noEmit",
44
+ "build": "bun run typecheck",
45
+ "lint": "bunx biome lint",
46
+ "test:validate-env": "! grep -rn 'process\\.env\\.\\(SUPABASE_URL\\|DATABASE_URL\\)\\s*=\\s*[\"\\x27]' tests/ || (echo 'ERROR: Found hardcoded environment variable string assignments in tests/' && exit 1)",
47
+ "prepare": "husky || true",
48
+ "prepublishOnly": "bun run typecheck && bun run lint"
49
+ },
50
+ "dependencies": {
51
+ "@asteasolutions/zod-to-openapi": "^8.2.0",
52
+ "@modelcontextprotocol/sdk": "^1.20.0",
53
+ "@sentry/node": "^10.25.0",
54
+ "@typescript-eslint/parser": "^8.0.0",
55
+ "@typescript-eslint/types": "^8.0.0",
56
+ "bcryptjs": "^2.4.3",
57
+ "cors": "^2.8.5",
58
+ "express": "^4.18.0",
59
+ "zod": "^4.1.12"
60
+ },
61
+ "devDependencies": {
62
+ "@apidevtools/swagger-parser": "^12.1.0",
63
+ "@biomejs/biome": "^1.7.3",
64
+ "@types/bcryptjs": "^2.4.6",
65
+ "@types/cors": "^2.8.19",
66
+ "@types/express": "^4.17.21",
67
+ "@types/node": "^22.9.0",
68
+ "@types/supertest": "^6.0.3",
69
+ "bun-types": "^1.1.10",
70
+ "husky": "^9.1.7",
71
+ "lint-staged": "^16.2.4",
72
+ "supertest": "^7.1.4",
73
+ "typescript": "^5.9.3"
74
+ }
75
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Auto-reindex trigger logic for session-based repository synchronization.
3
+ *
4
+ * NOTE: Queue system removed for local-only v2.0.0 (Issue #591)
5
+ * This module is now a stub that returns "not available" responses.
6
+ * Auto-reindex functionality requires async queue processing which is not
7
+ * available in local-only mode.
8
+ *
9
+ * TODO: For cloud mode restoration, re-implement with pg-boss queue
10
+ */
11
+
12
+ import type { AuthContext } from "@shared/types";
13
+ import { createLogger } from "@logging/logger.js";
14
+
15
+ const logger = createLogger({ module: "api-auto-reindex" });
16
+
17
+ /**
18
+ * Auto-reindex result containing triggered job information.
19
+ */
20
+ export interface AutoReindexResult {
21
+ triggered: boolean;
22
+ jobCount: number;
23
+ jobIds: string[];
24
+ rateLimited?: boolean;
25
+ reason?: string;
26
+ }
27
+
28
+ /**
29
+ * Trigger auto-reindex for authenticated user's project repositories.
30
+ *
31
+ * NOTE: Queue system removed for local-only v2.0.0 (Issue #591)
32
+ * This function now returns a "not available" response.
33
+ * Use MCP tools for synchronous indexing instead.
34
+ *
35
+ * @param context - Authentication context with user ID and key ID
36
+ * @returns Auto-reindex result indicating feature is unavailable
37
+ */
38
+ export async function triggerAutoReindex(
39
+ context: AuthContext,
40
+ ): Promise<AutoReindexResult> {
41
+ const { userId } = context;
42
+
43
+ logger.info("Auto-reindex requested but unavailable in local-only mode", {
44
+ user_id: userId,
45
+ });
46
+
47
+ // Queue system removed - auto-reindex requires async job processing
48
+ // Return a response indicating the feature is unavailable
49
+ return {
50
+ triggered: false,
51
+ jobCount: 0,
52
+ jobIds: [],
53
+ reason: "Auto-reindex unavailable - queue system removed for local-only mode. Use MCP index_repository tool for synchronous indexing.",
54
+ };
55
+ }
@@ -0,0 +1,209 @@
1
+ /**
2
+ * OpenAPI 3.1 spec builder for KotaDB API.
3
+ *
4
+ * Generates complete OpenAPI specification document with:
5
+ * - Info section (title, version, description)
6
+ * - Server definitions (production, staging, local)
7
+ * - Security schemes (API key, JWT bearer)
8
+ * - Path operations from paths.ts
9
+ * - Component schemas from schemas.ts
10
+ */
11
+
12
+ import { OpenAPIRegistry, OpenApiGeneratorV31 } from '@asteasolutions/zod-to-openapi';
13
+ import { registerPaths } from './paths.js';
14
+
15
+ // Cache for pre-computed spec
16
+ let cachedSpec: unknown | null = null;
17
+
18
+ /**
19
+ * Build complete OpenAPI 3.1 specification document
20
+ */
21
+ export function buildOpenAPISpec(): unknown {
22
+ // Return cached spec if available
23
+ if (cachedSpec) {
24
+ return cachedSpec;
25
+ }
26
+
27
+ const startTime = Date.now();
28
+
29
+ // Create registry
30
+ const registry = new OpenAPIRegistry();
31
+
32
+ // Register security schemes
33
+ registry.registerComponent('securitySchemes', 'apiKey', {
34
+ type: 'http',
35
+ scheme: 'bearer',
36
+ bearerFormat: 'API Key',
37
+ description: 'API key authentication using Bearer token. Format: `kota_<tier>_<prefix>_<secret>`. Example: `kota_free_key123_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`',
38
+ });
39
+
40
+ registry.registerComponent('securitySchemes', 'bearerAuth', {
41
+ type: 'http',
42
+ scheme: 'bearer',
43
+ bearerFormat: 'JWT',
44
+ description: 'JWT bearer token authentication. Obtained from Supabase Auth.',
45
+ });
46
+
47
+ // Register all path operations
48
+ registerPaths(registry);
49
+
50
+ // Generate OpenAPI spec
51
+ const generator = new OpenApiGeneratorV31(registry.definitions);
52
+
53
+ const spec = generator.generateDocument({
54
+ openapi: '3.1.0',
55
+ info: {
56
+ title: 'KotaDB API',
57
+ version: '0.1.0', // Match package.json version
58
+ description: `
59
+ # KotaDB API Documentation
60
+
61
+ KotaDB provides a code intelligence API for indexing, searching, and analyzing code repositories.
62
+
63
+ ## Authentication
64
+
65
+ KotaDB supports two authentication methods:
66
+
67
+ ### 1. API Key Authentication (Recommended for SDKs)
68
+
69
+ Use Bearer token format with your API key:
70
+
71
+ \`\`\`
72
+ Authorization: Bearer kota_free_key123_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
73
+ \`\`\`
74
+
75
+ API keys can be generated via the \`POST /api/keys/generate\` endpoint (requires JWT authentication first).
76
+
77
+ ### 2. JWT Bearer Token (For web applications)
78
+
79
+ Use Supabase Auth JWT tokens:
80
+
81
+ \`\`\`
82
+ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
83
+ \`\`\`
84
+
85
+ ## Rate Limiting
86
+
87
+ All authenticated endpoints are rate-limited with dual limits enforced:
88
+
89
+ - **Hourly limit**: 100 requests/hour (free tier)
90
+ - **Daily limit**: 1000 requests/day (free tier)
91
+
92
+ Both limits are checked on each request. Whichever limit is reached first will block the request.
93
+
94
+ Rate limit headers are included in all authenticated endpoint responses:
95
+
96
+ - \`X-RateLimit-Limit\`: Hourly threshold for the tier
97
+ - \`X-RateLimit-Remaining\`: Remaining requests (minimum of hourly and daily remaining)
98
+ - \`X-RateLimit-Reset\`: Unix timestamp when the hourly limit resets
99
+
100
+ When rate limit is exceeded, the API returns HTTP 429 with a \`Retry-After\` header indicating seconds until the limit resets.
101
+
102
+ ## Error Responses
103
+
104
+ All error responses follow a consistent format:
105
+
106
+ \`\`\`json
107
+ {
108
+ "error": "Error message",
109
+ "details": { /* optional additional context */ }
110
+ }
111
+ \`\`\`
112
+
113
+ Common HTTP status codes:
114
+
115
+ - \`400\`: Invalid request parameters
116
+ - \`401\`: Unauthorized (missing/invalid authentication)
117
+ - \`404\`: Resource not found
118
+ - \`429\`: Rate limit exceeded
119
+ - \`500\`: Internal server error
120
+
121
+ ## Endpoints Overview
122
+
123
+ - **Health**: Service availability check
124
+ - **Indexing**: Trigger repository indexing jobs
125
+ - **Jobs**: Track indexing job status and progress
126
+ - **Search**: Search indexed code content
127
+ - **Files**: Retrieve recently indexed files
128
+ - **Projects**: Manage multi-repository projects
129
+ - **API Keys**: Generate and manage API keys
130
+
131
+ `.trim(),
132
+ contact: {
133
+ name: 'KotaDB Support',
134
+ url: 'https://kotadb.com/support',
135
+ },
136
+ license: {
137
+ identifier: 'LicenseRef-Proprietary',
138
+ name: 'Proprietary',
139
+ },
140
+ },
141
+ servers: [
142
+ {
143
+ url: 'https://api.kotadb.com',
144
+ description: 'Production server',
145
+ },
146
+ {
147
+ url: 'https://staging-api.kotadb.com',
148
+ description: 'Staging server',
149
+ },
150
+ {
151
+ url: 'http://localhost:3001',
152
+ description: 'Local development server',
153
+ },
154
+ ],
155
+ tags: [
156
+ {
157
+ name: 'Health',
158
+ description: 'Service health and status endpoints',
159
+ },
160
+ {
161
+ name: 'Indexing',
162
+ description: 'Repository indexing operations',
163
+ },
164
+ {
165
+ name: 'Jobs',
166
+ description: 'Indexing job status and tracking',
167
+ },
168
+ {
169
+ name: 'Search',
170
+ description: 'Code search operations',
171
+ },
172
+ {
173
+ name: 'Files',
174
+ description: 'File listing and retrieval',
175
+ },
176
+ {
177
+ name: 'Projects',
178
+ description: 'Multi-repository project management',
179
+ },
180
+ {
181
+ name: 'API Keys',
182
+ description: 'API key generation and management',
183
+ },
184
+ ],
185
+ });
186
+
187
+ const duration = Date.now() - startTime;
188
+ const pathCount = Object.keys(spec.paths || {}).length;
189
+
190
+ process.stdout.write(JSON.stringify({
191
+ level: 'info',
192
+ module: 'openapi-builder',
193
+ message: 'OpenAPI spec generated',
194
+ duration_ms: duration,
195
+ path_count: pathCount,
196
+ }) + '\n');
197
+
198
+ // Cache the spec
199
+ cachedSpec = spec;
200
+
201
+ return spec;
202
+ }
203
+
204
+ /**
205
+ * Clear cached spec (for testing or hot reload)
206
+ */
207
+ export function clearSpecCache(): void {
208
+ cachedSpec = null;
209
+ }