codehere 0.1.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.
- package/ARCHITECTURE.md +240 -0
- package/CHANGELOG.md +44 -0
- package/CONTRIBUTING.md +171 -0
- package/LICENSE +22 -0
- package/README.md +203 -0
- package/dist/ast-parser.d.ts +55 -0
- package/dist/ast-parser.d.ts.map +1 -0
- package/dist/ast-parser.js +331 -0
- package/dist/ast-parser.js.map +1 -0
- package/dist/benchmark.d.ts +39 -0
- package/dist/benchmark.d.ts.map +1 -0
- package/dist/benchmark.js +195 -0
- package/dist/benchmark.js.map +1 -0
- package/dist/cache.d.ts +45 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +182 -0
- package/dist/cache.js.map +1 -0
- package/dist/chat.d.ts +4 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/chat.js +132 -0
- package/dist/chat.js.map +1 -0
- package/dist/code-analysis.d.ts +50 -0
- package/dist/code-analysis.d.ts.map +1 -0
- package/dist/code-analysis.js +327 -0
- package/dist/code-analysis.js.map +1 -0
- package/dist/context.d.ts +44 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +187 -0
- package/dist/context.js.map +1 -0
- package/dist/docs.d.ts +21 -0
- package/dist/docs.d.ts.map +1 -0
- package/dist/docs.js +147 -0
- package/dist/docs.js.map +1 -0
- package/dist/edit.d.ts +38 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +594 -0
- package/dist/edit.js.map +1 -0
- package/dist/embed.d.ts +18 -0
- package/dist/embed.d.ts.map +1 -0
- package/dist/embed.js +479 -0
- package/dist/embed.js.map +1 -0
- package/dist/error-handler.d.ts +76 -0
- package/dist/error-handler.d.ts.map +1 -0
- package/dist/error-handler.js +213 -0
- package/dist/error-handler.js.map +1 -0
- package/dist/formatter.d.ts +25 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +148 -0
- package/dist/formatter.js.map +1 -0
- package/dist/git.d.ts +55 -0
- package/dist/git.d.ts.map +1 -0
- package/dist/git.js +198 -0
- package/dist/git.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +964 -0
- package/dist/index.js.map +1 -0
- package/dist/intelligent-retrieval.d.ts +41 -0
- package/dist/intelligent-retrieval.d.ts.map +1 -0
- package/dist/intelligent-retrieval.js +265 -0
- package/dist/intelligent-retrieval.js.map +1 -0
- package/dist/iterative-refinement.d.ts +31 -0
- package/dist/iterative-refinement.d.ts.map +1 -0
- package/dist/iterative-refinement.js +172 -0
- package/dist/iterative-refinement.js.map +1 -0
- package/dist/learning.d.ts +69 -0
- package/dist/learning.d.ts.map +1 -0
- package/dist/learning.js +233 -0
- package/dist/learning.js.map +1 -0
- package/dist/log.d.ts +4 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +31 -0
- package/dist/log.js.map +1 -0
- package/dist/migrate.d.ts +33 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +133 -0
- package/dist/migrate.js.map +1 -0
- package/dist/monitoring.d.ts +75 -0
- package/dist/monitoring.d.ts.map +1 -0
- package/dist/monitoring.js +248 -0
- package/dist/monitoring.js.map +1 -0
- package/dist/parallel-processor.d.ts +43 -0
- package/dist/parallel-processor.d.ts.map +1 -0
- package/dist/parallel-processor.js +308 -0
- package/dist/parallel-processor.js.map +1 -0
- package/dist/planner.d.ts +47 -0
- package/dist/planner.d.ts.map +1 -0
- package/dist/planner.js +198 -0
- package/dist/planner.js.map +1 -0
- package/dist/policy.d.ts +3 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +26 -0
- package/dist/policy.js.map +1 -0
- package/dist/query-optimizer.d.ts +32 -0
- package/dist/query-optimizer.d.ts.map +1 -0
- package/dist/query-optimizer.js +205 -0
- package/dist/query-optimizer.js.map +1 -0
- package/dist/refactor.d.ts +27 -0
- package/dist/refactor.d.ts.map +1 -0
- package/dist/refactor.js +118 -0
- package/dist/refactor.js.map +1 -0
- package/dist/review.d.ts +31 -0
- package/dist/review.d.ts.map +1 -0
- package/dist/review.js +206 -0
- package/dist/review.js.map +1 -0
- package/dist/scaffold.d.ts +14 -0
- package/dist/scaffold.d.ts.map +1 -0
- package/dist/scaffold.js +85 -0
- package/dist/scaffold.js.map +1 -0
- package/dist/search.d.ts +19 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +198 -0
- package/dist/search.js.map +1 -0
- package/dist/session.d.ts +17 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +301 -0
- package/dist/session.js.map +1 -0
- package/dist/task-verification.d.ts +39 -0
- package/dist/task-verification.d.ts.map +1 -0
- package/dist/task-verification.js +336 -0
- package/dist/task-verification.js.map +1 -0
- package/dist/test_cohere.d.ts +2 -0
- package/dist/test_cohere.d.ts.map +1 -0
- package/dist/test_cohere.js +68 -0
- package/dist/test_cohere.js.map +1 -0
- package/dist/test_env.d.ts +2 -0
- package/dist/test_env.d.ts.map +1 -0
- package/dist/test_env.js +24 -0
- package/dist/test_env.js.map +1 -0
- package/dist/test_retrieval.d.ts +2 -0
- package/dist/test_retrieval.d.ts.map +1 -0
- package/dist/test_retrieval.js +84 -0
- package/dist/test_retrieval.js.map +1 -0
- package/dist/testgen.d.ts +24 -0
- package/dist/testgen.d.ts.map +1 -0
- package/dist/testgen.js +167 -0
- package/dist/testgen.js.map +1 -0
- package/dist/token-optimizer.d.ts +20 -0
- package/dist/token-optimizer.d.ts.map +1 -0
- package/dist/token-optimizer.js +277 -0
- package/dist/token-optimizer.js.map +1 -0
- package/dist/types.d.ts +36 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui.d.ts +54 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +295 -0
- package/dist/ui.js.map +1 -0
- package/dist/verify_db.d.ts +2 -0
- package/dist/verify_db.d.ts.map +1 -0
- package/dist/verify_db.js +52 -0
- package/dist/verify_db.js.map +1 -0
- package/package.json +71 -0
- package/templates/next-page/app/layout.tsx +19 -0
- package/templates/next-page/app/page.tsx +10 -0
- package/templates/next-page/package.json +22 -0
- package/templates/node-api/index.js +57 -0
- package/templates/node-api/package.json +13 -0
package/ARCHITECTURE.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# CodeHere Architecture
|
|
2
|
+
|
|
3
|
+
This document provides a technical overview of CodeHere's architecture. For design decisions and roadmap, see `docs/CODEHERE_LIVING_DOC.md`.
|
|
4
|
+
|
|
5
|
+
## System Overview
|
|
6
|
+
|
|
7
|
+
CodeHere is a local-first, CLI-based coding assistant that uses:
|
|
8
|
+
- **Cohere AI** for embeddings and chat completion
|
|
9
|
+
- **SQLite** (via sql.js) for local vector storage
|
|
10
|
+
- **TypeScript** for type safety
|
|
11
|
+
- **Commander.js** for CLI interface
|
|
12
|
+
|
|
13
|
+
## Core Architecture
|
|
14
|
+
|
|
15
|
+
### 1. Embeddings System
|
|
16
|
+
|
|
17
|
+
**File:** `src/embed.ts`
|
|
18
|
+
|
|
19
|
+
**Responsibilities:**
|
|
20
|
+
- Recursively scan repository for text files
|
|
21
|
+
- Chunk files into 300-token segments (50-token overlap)
|
|
22
|
+
- Generate embeddings via Cohere API
|
|
23
|
+
- Store in SQLite database (`data/embeddings.sqlite`)
|
|
24
|
+
|
|
25
|
+
**Key Functions:**
|
|
26
|
+
- `indexRepository()` - Main indexing function
|
|
27
|
+
- `chunkText()` - Splits text into token-sized chunks
|
|
28
|
+
- `embedText()` - Calls Cohere API with retry logic
|
|
29
|
+
- `updateFileEmbeddings()` - Re-indexes a single file after edit
|
|
30
|
+
|
|
31
|
+
**Incremental Indexing:**
|
|
32
|
+
- Tracks file metadata (path, mtime, hash) in `file_metadata` table
|
|
33
|
+
- Only re-indexes changed/new files
|
|
34
|
+
- Removes deleted files from index
|
|
35
|
+
|
|
36
|
+
### 2. Search System
|
|
37
|
+
|
|
38
|
+
**File:** `src/search.ts`
|
|
39
|
+
|
|
40
|
+
**Responsibilities:**
|
|
41
|
+
- Semantic search using cosine similarity
|
|
42
|
+
- Intelligent context retrieval with ranking
|
|
43
|
+
- File type prioritization
|
|
44
|
+
|
|
45
|
+
**Key Functions:**
|
|
46
|
+
- `searchSimilar()` - Basic cosine similarity search
|
|
47
|
+
- `searchIntelligent()` - Enhanced search with:
|
|
48
|
+
- File type prioritization (source files > docs)
|
|
49
|
+
- Path heuristics (`src/` boost, `test/` penalty)
|
|
50
|
+
- Exact filename matching (3x boost)
|
|
51
|
+
|
|
52
|
+
**Search Flow:**
|
|
53
|
+
1. Embed query using Cohere API
|
|
54
|
+
2. Load all embeddings from SQLite
|
|
55
|
+
3. Calculate cosine similarity
|
|
56
|
+
4. Rank and boost results
|
|
57
|
+
5. Return top N chunks
|
|
58
|
+
|
|
59
|
+
### 3. Edit System
|
|
60
|
+
|
|
61
|
+
**File:** `src/edit.ts`
|
|
62
|
+
|
|
63
|
+
**Responsibilities:**
|
|
64
|
+
- Generate unified diffs from natural language
|
|
65
|
+
- Apply patches safely with validation
|
|
66
|
+
- Never write broken files
|
|
67
|
+
|
|
68
|
+
**Key Functions:**
|
|
69
|
+
- `generateAndApplyEdit()` - Main edit function
|
|
70
|
+
- `generateEditDryRun()` - Preview mode (no write)
|
|
71
|
+
- `applyUnifiedDiff()` - Applies patch using `diff` package
|
|
72
|
+
- `validateSyntax()` - Validates JS/TS syntax before write
|
|
73
|
+
- `fixDuplicateExports()` - Post-processes module.exports
|
|
74
|
+
|
|
75
|
+
**Safety Flow:**
|
|
76
|
+
1. Generate diff via Cohere API
|
|
77
|
+
2. Parse diff stats
|
|
78
|
+
3. Check policy (line limits, folders)
|
|
79
|
+
4. Apply patch to get new content
|
|
80
|
+
5. **Validate syntax** (JS/TS only)
|
|
81
|
+
6. **Only write if valid** (critical safety check)
|
|
82
|
+
7. Update embeddings asynchronously
|
|
83
|
+
|
|
84
|
+
**Critical Safety Rule:**
|
|
85
|
+
```typescript
|
|
86
|
+
// NEVER write if syntax is invalid
|
|
87
|
+
if (!syntaxCheck.valid) {
|
|
88
|
+
return { success: false, error: 'Syntax validation failed. File NOT modified.' };
|
|
89
|
+
}
|
|
90
|
+
writeFileSync(fullPath, newContent, 'utf-8'); // Only if valid
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 4. Policy System
|
|
94
|
+
|
|
95
|
+
**File:** `src/policy.ts`
|
|
96
|
+
|
|
97
|
+
**Responsibilities:**
|
|
98
|
+
- Enforce safety guardrails
|
|
99
|
+
- Prevent dangerous operations
|
|
100
|
+
|
|
101
|
+
**Rules:**
|
|
102
|
+
- Max 50 lines changed per edit
|
|
103
|
+
- Blocks `infra/` and `billing/` folders
|
|
104
|
+
- Single-file only (blocks multi-file patches)
|
|
105
|
+
|
|
106
|
+
### 5. Chat System
|
|
107
|
+
|
|
108
|
+
**File:** `src/chat.ts`
|
|
109
|
+
|
|
110
|
+
**Responsibilities:**
|
|
111
|
+
- Cohere API integration
|
|
112
|
+
- Error handling with retry logic
|
|
113
|
+
- Circuit breaker pattern
|
|
114
|
+
|
|
115
|
+
**Key Functions:**
|
|
116
|
+
- `chatWithContext()` - Chat with code context
|
|
117
|
+
- `generateDiff()` - Generate unified diff from instruction
|
|
118
|
+
|
|
119
|
+
**Error Handling:**
|
|
120
|
+
- Exponential backoff retry
|
|
121
|
+
- Circuit breaker for API failures
|
|
122
|
+
- Clear error messages
|
|
123
|
+
|
|
124
|
+
## Data Storage
|
|
125
|
+
|
|
126
|
+
### SQLite Database
|
|
127
|
+
|
|
128
|
+
**Location:** `data/embeddings.sqlite`
|
|
129
|
+
|
|
130
|
+
**Tables:**
|
|
131
|
+
1. `embeddings`
|
|
132
|
+
- `id` (INTEGER PRIMARY KEY)
|
|
133
|
+
- `filepath` (TEXT)
|
|
134
|
+
- `chunkIndex` (INTEGER)
|
|
135
|
+
- `content` (TEXT)
|
|
136
|
+
- `embedding` (TEXT - JSON array)
|
|
137
|
+
|
|
138
|
+
2. `file_metadata`
|
|
139
|
+
- `filepath` (TEXT PRIMARY KEY)
|
|
140
|
+
- `mtime` (INTEGER)
|
|
141
|
+
- `hash` (TEXT - MD5)
|
|
142
|
+
|
|
143
|
+
**Indexes:**
|
|
144
|
+
- `idx_filepath` on `embeddings(filepath)`
|
|
145
|
+
|
|
146
|
+
### Audit Logs
|
|
147
|
+
|
|
148
|
+
**Location:** `data/logs.jsonl`
|
|
149
|
+
|
|
150
|
+
**Format:** JSON Lines (one JSON object per line)
|
|
151
|
+
|
|
152
|
+
**Fields:**
|
|
153
|
+
- `timestamp` (ISO string)
|
|
154
|
+
- `command` (string)
|
|
155
|
+
- `query` (string, optional)
|
|
156
|
+
- `filepath` (string, optional)
|
|
157
|
+
- `diffStats` (object, optional)
|
|
158
|
+
- `policyStatus` ('allowed' | 'rejected')
|
|
159
|
+
- `retrievedFiles` (array, optional)
|
|
160
|
+
|
|
161
|
+
## CLI Structure
|
|
162
|
+
|
|
163
|
+
**File:** `src/index.ts`
|
|
164
|
+
|
|
165
|
+
**Commands:**
|
|
166
|
+
- `codehere` (no args) - Interactive session
|
|
167
|
+
- `codehere index` - Build embeddings index
|
|
168
|
+
- `codehere ask "<question>"` - Semantic search + chat
|
|
169
|
+
- `codehere explain <file>` - Explain a file
|
|
170
|
+
- `codehere fix <file> "<instruction>"` - Edit a file
|
|
171
|
+
- `codehere fix <file> "<instruction>" --dry-run` - Preview changes
|
|
172
|
+
- `codehere scaffold <template> <targetDir>` - Scaffold app skeleton
|
|
173
|
+
- `codehere debug-search "<query>"` - Debug search results
|
|
174
|
+
|
|
175
|
+
## Templates
|
|
176
|
+
|
|
177
|
+
**Location:** `templates/`
|
|
178
|
+
|
|
179
|
+
**Structure:**
|
|
180
|
+
- Each template is a directory
|
|
181
|
+
- Files are copied recursively to target directory
|
|
182
|
+
- Templates are static (not LLM-generated)
|
|
183
|
+
|
|
184
|
+
**Available Templates:**
|
|
185
|
+
- `node-api` - Simple Node.js HTTP API
|
|
186
|
+
- `next-page` - Minimal Next.js page
|
|
187
|
+
|
|
188
|
+
## Error Handling
|
|
189
|
+
|
|
190
|
+
**Strategy:**
|
|
191
|
+
- Retry with exponential backoff for API calls
|
|
192
|
+
- Circuit breaker for repeated failures
|
|
193
|
+
- Clear, actionable error messages
|
|
194
|
+
- Never crash silently
|
|
195
|
+
|
|
196
|
+
**Error Messages:**
|
|
197
|
+
- Missing API key: Shows setup instructions
|
|
198
|
+
- Missing embeddings DB: Shows how to run `index`
|
|
199
|
+
- Syntax validation failure: Shows error details, file NOT modified
|
|
200
|
+
- Policy rejection: Shows reason
|
|
201
|
+
|
|
202
|
+
## Testing
|
|
203
|
+
|
|
204
|
+
**Location:** `tests/edit.test.ts`
|
|
205
|
+
|
|
206
|
+
**Test Cases:**
|
|
207
|
+
1. Simple function rename
|
|
208
|
+
2. Add logging line
|
|
209
|
+
3. Add try/catch around async call
|
|
210
|
+
4. Edit file with comments
|
|
211
|
+
5. Add parameter with default
|
|
212
|
+
|
|
213
|
+
**Run:** `npm test`
|
|
214
|
+
|
|
215
|
+
## Performance
|
|
216
|
+
|
|
217
|
+
- **Indexing:** ~2 seconds per file (rate-limited by API)
|
|
218
|
+
- **Search:** 5-8 seconds (API call + DB query)
|
|
219
|
+
- **Edit:** 5-8 seconds (diff generation + validation)
|
|
220
|
+
- **Token Optimization:** 64-83% reduction
|
|
221
|
+
|
|
222
|
+
## Security & Privacy
|
|
223
|
+
|
|
224
|
+
- **Local-first:** Embeddings stored locally
|
|
225
|
+
- **No code transmission:** Only chunks sent for embedding
|
|
226
|
+
- **Audit logging:** Complete JSONL trail
|
|
227
|
+
- **Policy enforcement:** Multiple safety checks
|
|
228
|
+
|
|
229
|
+
## Future Architecture (Tier 2+)
|
|
230
|
+
|
|
231
|
+
See `docs/CODEHERE_LIVING_DOC.md` for roadmap:
|
|
232
|
+
- Multi-file support
|
|
233
|
+
- Project graph awareness
|
|
234
|
+
- Test execution integration
|
|
235
|
+
- VS Code extension
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
For design decisions and roadmap, see `docs/CODEHERE_LIVING_DOC.md`.
|
|
240
|
+
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to CodeHere will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2025-11-24
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
- Core CLI commands: `index`, `ask`, `explain`, `fix`, `scaffold`
|
|
12
|
+
- Local-first embeddings storage using SQLite
|
|
13
|
+
- Semantic search with intelligent context retrieval
|
|
14
|
+
- Single-file code editing with syntax validation
|
|
15
|
+
- Policy guardrails (line limits, restricted folders)
|
|
16
|
+
- Dry-run mode for previewing changes
|
|
17
|
+
- Incremental indexing (only re-indexes changed files)
|
|
18
|
+
- Template-based app scaffolding (`node-api`, `next-page`)
|
|
19
|
+
- Integration test suite (5 test cases)
|
|
20
|
+
- Comprehensive documentation (living doc, architecture, contributing guide)
|
|
21
|
+
|
|
22
|
+
### Safety Features
|
|
23
|
+
- Syntax validation before writing files (JS/TS)
|
|
24
|
+
- TypeScript type checking (`tsc --noEmit`)
|
|
25
|
+
- Policy enforcement (max 50 lines, single-file only)
|
|
26
|
+
- Audit logging (JSONL format)
|
|
27
|
+
- Never writes broken files to disk
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
- Module.exports handling on function renames
|
|
31
|
+
- Temp file cleanup in syntax validation
|
|
32
|
+
- Error handling for missing embeddings database
|
|
33
|
+
- Search relevance (prioritizes source files, path heuristics)
|
|
34
|
+
|
|
35
|
+
### Documentation
|
|
36
|
+
- Single living document (`docs/CODEHERE_LIVING_DOC.md`)
|
|
37
|
+
- Architecture guide
|
|
38
|
+
- Contributing guide
|
|
39
|
+
- Changelog (this file)
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
[0.1.0]: https://github.com/muhammadegaa/codehere/releases/tag/v0.1.0
|
|
44
|
+
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# Contributing to CodeHere
|
|
2
|
+
|
|
3
|
+
Thank you for your interest in contributing to CodeHere! This guide will help you understand the architecture, development workflow, and how to make contributions.
|
|
4
|
+
|
|
5
|
+
## Architecture Overview
|
|
6
|
+
|
|
7
|
+
CodeHere is a local-first, CLI-based coding assistant. See `docs/CODEHERE_LIVING_DOC.md` for the complete design and roadmap.
|
|
8
|
+
|
|
9
|
+
### Core Components
|
|
10
|
+
|
|
11
|
+
1. **Embeddings** (`src/embed.ts`)
|
|
12
|
+
- Indexes files, chunks text, generates embeddings via Cohere API
|
|
13
|
+
- Stores in local SQLite database
|
|
14
|
+
- Supports incremental indexing (tracks file mtime + hash)
|
|
15
|
+
|
|
16
|
+
2. **Search** (`src/search.ts`)
|
|
17
|
+
- Semantic search using cosine similarity
|
|
18
|
+
- Intelligent context retrieval with file type prioritization
|
|
19
|
+
- Path heuristics (prioritizes `src/`, penalizes `test/`)
|
|
20
|
+
|
|
21
|
+
3. **Edit** (`src/edit.ts`)
|
|
22
|
+
- Generates unified diffs from natural language instructions
|
|
23
|
+
- Applies patches with syntax validation
|
|
24
|
+
- **Critical safety:** Never writes broken files to disk
|
|
25
|
+
|
|
26
|
+
4. **Policy** (`src/policy.ts`)
|
|
27
|
+
- Enforces safety guardrails (line limits, restricted folders)
|
|
28
|
+
- Single-file only (prevents accidental multi-file changes)
|
|
29
|
+
|
|
30
|
+
5. **Chat** (`src/chat.ts`)
|
|
31
|
+
- Cohere API integration for chat and diff generation
|
|
32
|
+
- Error handling with retry logic
|
|
33
|
+
|
|
34
|
+
### Data Flow
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
User Command → Policy Check → Generate Diff → Apply Patch →
|
|
38
|
+
Syntax Validation → Write File (only if valid) → Update Embeddings (async)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Development Workflow
|
|
42
|
+
|
|
43
|
+
### Prerequisites
|
|
44
|
+
|
|
45
|
+
- Node.js 18+
|
|
46
|
+
- TypeScript 5.0+
|
|
47
|
+
- Cohere API key (get from https://dashboard.cohere.com/api-keys)
|
|
48
|
+
|
|
49
|
+
### Setup
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Clone the repository
|
|
53
|
+
git clone https://github.com/muhammadegaa/codehere.git
|
|
54
|
+
cd codehere/agent
|
|
55
|
+
|
|
56
|
+
# Install dependencies
|
|
57
|
+
npm install
|
|
58
|
+
|
|
59
|
+
# Set up environment
|
|
60
|
+
echo "COHERE_API_KEY=your_key_here" > .env
|
|
61
|
+
|
|
62
|
+
# Build
|
|
63
|
+
npm run build
|
|
64
|
+
|
|
65
|
+
# Run tests
|
|
66
|
+
npm test
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Development Commands
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# Build TypeScript
|
|
73
|
+
npm run build
|
|
74
|
+
|
|
75
|
+
# Run in development mode
|
|
76
|
+
npm run dev
|
|
77
|
+
|
|
78
|
+
# Run tests
|
|
79
|
+
npm test
|
|
80
|
+
|
|
81
|
+
# Install globally (for testing)
|
|
82
|
+
npm install -g .
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Making Changes
|
|
86
|
+
|
|
87
|
+
1. **Create a branch:**
|
|
88
|
+
```bash
|
|
89
|
+
git checkout -b feature/your-feature-name
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
2. **Make your changes:**
|
|
93
|
+
- Follow existing code style
|
|
94
|
+
- Add tests for new features
|
|
95
|
+
- Update documentation if behavior changes
|
|
96
|
+
|
|
97
|
+
3. **Test your changes:**
|
|
98
|
+
```bash
|
|
99
|
+
npm run build
|
|
100
|
+
npm test
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
4. **Update documentation:**
|
|
104
|
+
- If behavior changes, update `docs/CODEHERE_LIVING_DOC.md`
|
|
105
|
+
- If CLI changes, update `README.md`
|
|
106
|
+
- If architecture changes, update `ARCHITECTURE.md`
|
|
107
|
+
|
|
108
|
+
5. **Commit:**
|
|
109
|
+
```bash
|
|
110
|
+
git commit -m "feat: add your feature"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
6. **Push and create PR:**
|
|
114
|
+
```bash
|
|
115
|
+
git push origin feature/your-feature-name
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Code Style
|
|
119
|
+
|
|
120
|
+
- **TypeScript:** Use strict mode, prefer explicit types
|
|
121
|
+
- **Imports:** Use ES modules (`import`/`export`)
|
|
122
|
+
- **Error handling:** Always use try/catch, provide clear error messages
|
|
123
|
+
- **Safety first:** Never write broken files, always validate before writing
|
|
124
|
+
|
|
125
|
+
## Testing
|
|
126
|
+
|
|
127
|
+
- Integration tests in `tests/edit.test.ts`
|
|
128
|
+
- Run with `npm test`
|
|
129
|
+
- Add tests for new features or edge cases
|
|
130
|
+
|
|
131
|
+
## Documentation
|
|
132
|
+
|
|
133
|
+
- **Living Doc:** `docs/CODEHERE_LIVING_DOC.md` - single source of truth
|
|
134
|
+
- **Architecture:** `ARCHITECTURE.md` - technical details
|
|
135
|
+
- **README:** User-facing documentation
|
|
136
|
+
- **Changelog:** `CHANGELOG.md` - release notes
|
|
137
|
+
|
|
138
|
+
**Important:** Always update the living doc when capabilities or behavior change.
|
|
139
|
+
|
|
140
|
+
## Safety Guidelines
|
|
141
|
+
|
|
142
|
+
1. **Never write broken files:**
|
|
143
|
+
- Always validate syntax before writing
|
|
144
|
+
- Use dry-run mode for testing
|
|
145
|
+
- Rollback on validation failure
|
|
146
|
+
|
|
147
|
+
2. **Policy enforcement:**
|
|
148
|
+
- Respect line limits (50 lines max)
|
|
149
|
+
- Single-file only (for now)
|
|
150
|
+
- Block restricted folders
|
|
151
|
+
|
|
152
|
+
3. **Error handling:**
|
|
153
|
+
- Provide clear, actionable error messages
|
|
154
|
+
- Log all operations for audit
|
|
155
|
+
- Never crash silently
|
|
156
|
+
|
|
157
|
+
## Release Process
|
|
158
|
+
|
|
159
|
+
1. Update version in `package.json` (semver)
|
|
160
|
+
2. Update `CHANGELOG.md`
|
|
161
|
+
3. Update `docs/CODEHERE_LIVING_DOC.md` changelog section
|
|
162
|
+
4. Tag release: `git tag v0.1.0`
|
|
163
|
+
5. Publish: `npm publish --access public`
|
|
164
|
+
|
|
165
|
+
## Questions?
|
|
166
|
+
|
|
167
|
+
- Open an issue on GitHub
|
|
168
|
+
- Check `docs/CODEHERE_LIVING_DOC.md` for design decisions
|
|
169
|
+
- Review `ARCHITECTURE.md` for technical details
|
|
170
|
+
|
|
171
|
+
Thank you for contributing! 🚀
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# codehere
|
|
2
|
+
|
|
3
|
+
> Enterprise-grade AI coding assistant with local embeddings and semantic search
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
## 🚀 Installation
|
|
10
|
+
|
|
11
|
+
### Global Installation (Recommended)
|
|
12
|
+
|
|
13
|
+
**Option 1: Install from Local Directory**
|
|
14
|
+
```bash
|
|
15
|
+
# Navigate to the agent directory
|
|
16
|
+
cd codehere/agent
|
|
17
|
+
|
|
18
|
+
# Install globally
|
|
19
|
+
npm install -g .
|
|
20
|
+
|
|
21
|
+
# Verify installation
|
|
22
|
+
codehere --version
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Option 2: Install from npm (after publishing)**
|
|
26
|
+
```bash
|
|
27
|
+
npm install -g codehere
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Once installed globally, you can use `codehere` from any directory in your terminal.
|
|
31
|
+
|
|
32
|
+
### Quick Start
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Set up your Cohere API key (in your project directory)
|
|
36
|
+
echo "COHERE_API_KEY=your_key_here" > .env
|
|
37
|
+
|
|
38
|
+
# Index your codebase
|
|
39
|
+
codehere index
|
|
40
|
+
|
|
41
|
+
# Start interactive session
|
|
42
|
+
codehere
|
|
43
|
+
|
|
44
|
+
# Or use commands directly
|
|
45
|
+
codehere ask "How does authentication work?"
|
|
46
|
+
codehere explain src/utils.ts
|
|
47
|
+
codehere fix src/utils.ts "Add error handling"
|
|
48
|
+
codehere fix src/utils.ts "Add error handling" --dry-run # Preview changes
|
|
49
|
+
codehere scaffold node-api ./playground/todo-api # Scaffold app skeleton
|
|
50
|
+
codehere debug-search "logging" # Debug search results
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## ✨ Features
|
|
54
|
+
|
|
55
|
+
### 🧠 Semantic Code Search
|
|
56
|
+
- Builds local embedding index using Cohere's embedding API
|
|
57
|
+
- Uses cosine similarity to find relevant code chunks
|
|
58
|
+
- Enhanced context with file relationships and dependencies
|
|
59
|
+
|
|
60
|
+
### 🤖 AI-Powered Assistance
|
|
61
|
+
- Context-aware responses using retrieved code
|
|
62
|
+
- Natural language code explanations
|
|
63
|
+
- Intelligent code editing with unified diff generation
|
|
64
|
+
- Enhanced context understanding (15+ chunks)
|
|
65
|
+
|
|
66
|
+
### 🛡️ Enterprise Safety
|
|
67
|
+
- **Line limit enforcement** - Maximum 50 lines changed per edit
|
|
68
|
+
- **Restricted folders** - Protect critical directories (infra/, billing/)
|
|
69
|
+
- **Single-file edits** - Prevent accidental multi-file changes
|
|
70
|
+
- **Audit logging** - Every operation logged to JSONL
|
|
71
|
+
|
|
72
|
+
### 🔧 Enterprise Reliability
|
|
73
|
+
- **Automatic error recovery** - Retry with exponential backoff
|
|
74
|
+
- **Circuit breaker** - Protects against API failures
|
|
75
|
+
- **Health monitoring** - System health checks and metrics
|
|
76
|
+
- **24/7 operation ready** - Robust error handling
|
|
77
|
+
|
|
78
|
+
### 📦 Production Ready
|
|
79
|
+
- Fully tested end-to-end
|
|
80
|
+
- TypeScript for type safety
|
|
81
|
+
- SQLite for local vector storage
|
|
82
|
+
- Comprehensive error handling
|
|
83
|
+
|
|
84
|
+
## 🏗️ Architecture
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
┌─────────────┐
|
|
88
|
+
│ CLI Tool │
|
|
89
|
+
└──────┬──────┘
|
|
90
|
+
│
|
|
91
|
+
├─→ Index: Scan → Chunk → Embed → Store (SQLite)
|
|
92
|
+
├─→ Ask: Query → Embed → Search (15 chunks) → Chat
|
|
93
|
+
├─→ Explain: Read → Chat
|
|
94
|
+
├─→ Fix: Read → Generate Diff → Policy Check → Apply
|
|
95
|
+
└─→ Health: System Status → Metrics
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Tech Stack:**
|
|
99
|
+
- **TypeScript** - Type-safe development
|
|
100
|
+
- **Cohere AI** - Embeddings and chat completion
|
|
101
|
+
- **SQL.js** - In-memory SQLite for vector storage
|
|
102
|
+
- **Commander.js** - CLI interface
|
|
103
|
+
- **Tiktoken** - Token counting and chunking
|
|
104
|
+
|
|
105
|
+
## 📊 Performance
|
|
106
|
+
|
|
107
|
+
- **Indexing:** ~2 seconds per chunk (rate-limited by API)
|
|
108
|
+
- **Retrieval:** <100ms for 200+ chunks
|
|
109
|
+
- **Chat:** ~2-5 seconds per query
|
|
110
|
+
- **Storage:** ~13KB per chunk (1024-dim embeddings)
|
|
111
|
+
|
|
112
|
+
## 🔒 Security & Privacy
|
|
113
|
+
|
|
114
|
+
- **Local-first:** All embeddings stored locally in SQLite
|
|
115
|
+
- **No code transmission:** Only chunks sent to API for embedding
|
|
116
|
+
- **Audit trail:** Complete JSONL logging of all operations
|
|
117
|
+
- **Policy enforcement:** Multiple safety checks before applying changes
|
|
118
|
+
|
|
119
|
+
## 📈 Use Cases
|
|
120
|
+
|
|
121
|
+
### For Developers
|
|
122
|
+
- Understand large codebases quickly
|
|
123
|
+
- Get context-aware answers about code
|
|
124
|
+
- Safely refactor with AI assistance
|
|
125
|
+
- Learn from code explanations
|
|
126
|
+
|
|
127
|
+
### For Teams
|
|
128
|
+
- Onboard new developers faster
|
|
129
|
+
- Document code automatically
|
|
130
|
+
- Enforce coding standards
|
|
131
|
+
- Audit code changes
|
|
132
|
+
|
|
133
|
+
### For Enterprises
|
|
134
|
+
- Code review assistance
|
|
135
|
+
- Compliance logging
|
|
136
|
+
- Safe AI-assisted development
|
|
137
|
+
- Knowledge base building
|
|
138
|
+
|
|
139
|
+
## 📚 Documentation
|
|
140
|
+
|
|
141
|
+
**Main Reference:** See [docs/CODEHERE_LIVING_DOC.md](docs/CODEHERE_LIVING_DOC.md) for the complete design, roadmap, and current capabilities.
|
|
142
|
+
|
|
143
|
+
This is the single source of truth for CodeHere's vision, current state, limitations, and future plans.
|
|
144
|
+
|
|
145
|
+
**Test Results:**
|
|
146
|
+
- ✅ Indexing: Working with retry logic
|
|
147
|
+
- ✅ Retrieval: 15 chunks with file relationships
|
|
148
|
+
- ✅ Chat: Context-aware responses with error recovery
|
|
149
|
+
- ✅ Editing: Diff generation and application
|
|
150
|
+
- ✅ Policies: All safety checks enforced
|
|
151
|
+
- ✅ Logging: Complete audit trail
|
|
152
|
+
- ✅ Health: System monitoring operational
|
|
153
|
+
|
|
154
|
+
## 📁 Project Structure
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
agent/
|
|
158
|
+
├── src/
|
|
159
|
+
│ ├── index.ts # CLI entrypoint
|
|
160
|
+
│ ├── embed.ts # Repository indexing & embeddings
|
|
161
|
+
│ ├── search.ts # Semantic search & retrieval
|
|
162
|
+
│ ├── chat.ts # Cohere API integration
|
|
163
|
+
│ ├── edit.ts # Diff generation & application
|
|
164
|
+
│ ├── policy.ts # Safety policy enforcement
|
|
165
|
+
│ ├── log.ts # Audit logging
|
|
166
|
+
│ ├── types.ts # Type definitions
|
|
167
|
+
│ ├── ui.ts # CLI UI utilities
|
|
168
|
+
│ ├── error-handler.ts # Enterprise error handling
|
|
169
|
+
│ ├── monitoring.ts # Health checks & metrics
|
|
170
|
+
│ └── context.ts # Enhanced context understanding
|
|
171
|
+
├── data/
|
|
172
|
+
│ ├── embeddings.sqlite # Vector database
|
|
173
|
+
│ └── logs.jsonl # Audit log
|
|
174
|
+
├── test/ # Test files
|
|
175
|
+
├── package.json
|
|
176
|
+
├── tsconfig.json
|
|
177
|
+
└── README.md
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 🛠️ Development
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# Development mode
|
|
184
|
+
npm run dev
|
|
185
|
+
|
|
186
|
+
# Build
|
|
187
|
+
npm run build
|
|
188
|
+
|
|
189
|
+
# Run tests
|
|
190
|
+
npm test
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## 📝 License
|
|
194
|
+
|
|
195
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
196
|
+
|
|
197
|
+
## 🤝 Contributing
|
|
198
|
+
|
|
199
|
+
Contributions welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
**Built with ❤️ for developers who want AI assistance without sacrificing control.**
|