opencode-codebase-index 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/README.md +233 -0
- package/commands/find.md +13 -0
- package/commands/index.md +11 -0
- package/commands/search.md +9 -0
- package/dist/index.cjs +31492 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +31433 -0
- package/dist/index.js.map +1 -0
- package/native/codebase-index-native.darwin-arm64.node +0 -0
- package/package.json +83 -0
- package/skill/SKILL.md +91 -0
package/README.md
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# opencode-codebase-index
|
|
2
|
+
|
|
3
|
+
Semantic codebase indexing and search for OpenCode. Find code by meaning, not just keywords.
|
|
4
|
+
|
|
5
|
+
## When to Use
|
|
6
|
+
|
|
7
|
+
| Scenario | Tool | Why |
|
|
8
|
+
| ------------------------------- | ------------------- | ----------------------------- |
|
|
9
|
+
| Don't know function/class names | `codebase_search` | Natural language → code |
|
|
10
|
+
| Exploring unfamiliar codebase | `codebase_search` | Finds related code by meaning |
|
|
11
|
+
| Know exact identifier | `grep` | Faster, finds all occurrences |
|
|
12
|
+
| Need ALL matches | `grep` | Semantic returns top N only |
|
|
13
|
+
|
|
14
|
+
**Best workflow:** Semantic search for discovery → grep for precision.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install opencode-codebase-index
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Add to your `opencode.json`:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{
|
|
26
|
+
"plugin": ["opencode-codebase-index"]
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Tools
|
|
31
|
+
|
|
32
|
+
### `codebase_search`
|
|
33
|
+
|
|
34
|
+
Search code by describing what it does. Returns focused results (5-10 files).
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
"find the user authentication logic"
|
|
38
|
+
"code that handles database connections"
|
|
39
|
+
"error handling middleware for HTTP requests"
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Good queries describe behavior:**
|
|
43
|
+
|
|
44
|
+
- "function that validates email addresses"
|
|
45
|
+
- "middleware that checks JWT tokens"
|
|
46
|
+
- "error handling for payment failures"
|
|
47
|
+
|
|
48
|
+
**Use grep instead for:**
|
|
49
|
+
|
|
50
|
+
- Exact names: `validateEmail`, `UserService`
|
|
51
|
+
- Keywords: `TODO`, `FIXME`
|
|
52
|
+
- Literals: `401`, `error`
|
|
53
|
+
|
|
54
|
+
### `index_codebase`
|
|
55
|
+
|
|
56
|
+
Create or update the semantic index. Incremental indexing is fast (~50ms when nothing changed).
|
|
57
|
+
|
|
58
|
+
| Parameter | Type | Default | Description |
|
|
59
|
+
| ---------------- | ------- | ------- | ----------------------- |
|
|
60
|
+
| `force` | boolean | false | Reindex from scratch |
|
|
61
|
+
| `estimateOnly` | boolean | false | Show cost estimate only |
|
|
62
|
+
|
|
63
|
+
### `index_status`
|
|
64
|
+
|
|
65
|
+
Check if the codebase is indexed and ready for search.
|
|
66
|
+
|
|
67
|
+
### `index_health_check`
|
|
68
|
+
|
|
69
|
+
Remove stale entries from deleted files.
|
|
70
|
+
|
|
71
|
+
## Slash Commands
|
|
72
|
+
|
|
73
|
+
Copy the commands from `commands/` to your project's `.opencode/command/` directory:
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cp -r node_modules/opencode-codebase-index/commands/* .opencode/command/
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Available commands:
|
|
80
|
+
|
|
81
|
+
| Command | Description |
|
|
82
|
+
| ------------------- | ----------------------------------- |
|
|
83
|
+
| `/search <query>` | Semantic search for code by meaning |
|
|
84
|
+
| `/index` | Create or update the semantic index |
|
|
85
|
+
| `/find <query>` | Hybrid search (semantic + grep) |
|
|
86
|
+
|
|
87
|
+
## Configuration
|
|
88
|
+
|
|
89
|
+
Optional configuration in `.opencode/codebase-index.json`:
|
|
90
|
+
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"embeddingProvider": "auto",
|
|
94
|
+
"scope": "project",
|
|
95
|
+
"indexing": {
|
|
96
|
+
"autoIndex": false,
|
|
97
|
+
"watchFiles": true,
|
|
98
|
+
"maxFileSize": 1048576
|
|
99
|
+
},
|
|
100
|
+
"search": {
|
|
101
|
+
"maxResults": 20,
|
|
102
|
+
"minScore": 0.1,
|
|
103
|
+
"hybridWeight": 0.5,
|
|
104
|
+
"contextLines": 0
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
| Option | Default | Description |
|
|
110
|
+
| ------------------------ | ------------- | ---------------------------------------------------------------- |
|
|
111
|
+
| `embeddingProvider` | `"auto"` | `auto`, `github-copilot`, `openai`, `google`, `ollama` |
|
|
112
|
+
| `scope` | `"project"` | `project` (local) or `global` (shared) |
|
|
113
|
+
| `indexing.autoIndex` | `false` | Auto-index on plugin load |
|
|
114
|
+
| `indexing.watchFiles` | `true` | Watch for file changes and re-index |
|
|
115
|
+
| `indexing.maxFileSize` | `1048576` | Max file size in bytes (1MB) |
|
|
116
|
+
| `search.maxResults` | `20` | Max results to return |
|
|
117
|
+
| `search.minScore` | `0.1` | Minimum similarity score |
|
|
118
|
+
| `search.hybridWeight` | `0.5` | Keyword vs semantic balance (0=semantic only, 1=keyword only) |
|
|
119
|
+
| `search.contextLines` | `0` | Extra lines to include before/after each match |
|
|
120
|
+
|
|
121
|
+
### Embedding Providers
|
|
122
|
+
|
|
123
|
+
Uses OpenCode's authentication. Auto-detected in order:
|
|
124
|
+
|
|
125
|
+
1. **GitHub Copilot** - Uses Copilot API
|
|
126
|
+
2. **OpenAI** - Uses OpenAI API
|
|
127
|
+
3. **Google** - Uses Gemini API
|
|
128
|
+
4. **Ollama** - Local, requires `nomic-embed-text` or similar
|
|
129
|
+
|
|
130
|
+
## How It Works
|
|
131
|
+
|
|
132
|
+
1. **Parsing** - Tree-sitter extracts semantic chunks (functions, classes, etc.)
|
|
133
|
+
2. **Embedding** - Chunks converted to vectors via embedding API
|
|
134
|
+
3. **Storage** - Vectors stored locally using usearch
|
|
135
|
+
4. **Search** - Query embedded and compared via cosine similarity + keyword matching
|
|
136
|
+
|
|
137
|
+
Index stored in `.opencode/index/` within your project.
|
|
138
|
+
|
|
139
|
+
## Performance
|
|
140
|
+
|
|
141
|
+
- **Incremental indexing**: ~50ms when no files changed
|
|
142
|
+
- **Full index**: Depends on codebase size (Express.js: ~30s for 472 chunks)
|
|
143
|
+
- **Search latency**: ~800-1000ms (embedding API call)
|
|
144
|
+
- **Token savings**: 99%+ vs reading all files
|
|
145
|
+
|
|
146
|
+
## Requirements
|
|
147
|
+
|
|
148
|
+
- Node.js >= 18
|
|
149
|
+
- Rust toolchain (for building native module)
|
|
150
|
+
|
|
151
|
+
## Building
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
npm run build # Full build (TS + Rust)
|
|
155
|
+
npm run build:ts # TypeScript only
|
|
156
|
+
npm run test # Run tests
|
|
157
|
+
npm run typecheck # TypeScript type checking
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Local Development
|
|
161
|
+
|
|
162
|
+
To test the plugin locally without publishing to npm:
|
|
163
|
+
|
|
164
|
+
1. Build the plugin:
|
|
165
|
+
```bash
|
|
166
|
+
npm run build
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
2. Deploy to OpenCode's plugin cache:
|
|
170
|
+
```bash
|
|
171
|
+
rm -rf ~/.cache/opencode/node_modules/opencode-codebase-index
|
|
172
|
+
mkdir -p ~/.cache/opencode/node_modules/opencode-codebase-index
|
|
173
|
+
cp -R dist native commands skill package.json ~/.cache/opencode/node_modules/opencode-codebase-index/
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
3. Create a loader in your test project:
|
|
177
|
+
```bash
|
|
178
|
+
mkdir -p .opencode/plugin
|
|
179
|
+
echo 'export { default } from "$HOME/.cache/opencode/node_modules/opencode-codebase-index/dist/index.js"' > .opencode/plugin/codebase-index.ts
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
4. Run `opencode` in your test project.
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
1. Fork the repository
|
|
187
|
+
2. Create a feature branch: `git checkout -b feature/my-feature`
|
|
188
|
+
3. Make your changes
|
|
189
|
+
4. Run the build: `npm run build`
|
|
190
|
+
5. Test locally using the steps above
|
|
191
|
+
6. Commit your changes: `git commit -m "feat: add my feature"`
|
|
192
|
+
7. Push to your fork: `git push origin feature/my-feature`
|
|
193
|
+
8. Open a pull request
|
|
194
|
+
|
|
195
|
+
CI will automatically run tests and type checking on your PR.
|
|
196
|
+
|
|
197
|
+
### Project Structure
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
├── src/
|
|
201
|
+
│ ├── index.ts # Plugin entry point
|
|
202
|
+
│ ├── config/ # Configuration schema
|
|
203
|
+
│ ├── embeddings/ # Embedding provider detection and API
|
|
204
|
+
│ ├── indexer/ # Core indexing logic
|
|
205
|
+
│ ├── tools/ # OpenCode tool definitions
|
|
206
|
+
│ ├── utils/ # File collection, cost estimation
|
|
207
|
+
│ ├── native/ # Rust native module wrapper
|
|
208
|
+
│ └── watcher/ # File change watcher
|
|
209
|
+
├── native/
|
|
210
|
+
│ └── src/ # Rust native module (tree-sitter, usearch)
|
|
211
|
+
├── tests/ # Unit tests (vitest)
|
|
212
|
+
├── commands/ # Slash command definitions
|
|
213
|
+
├── skill/ # Agent skill guidance
|
|
214
|
+
└── .github/workflows/ # CI/CD (test, build, publish)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Native Module
|
|
218
|
+
|
|
219
|
+
The Rust native module handles:
|
|
220
|
+
- Tree-sitter parsing for semantic chunking
|
|
221
|
+
- xxHash for fast file hashing
|
|
222
|
+
- usearch for vector storage and similarity search
|
|
223
|
+
|
|
224
|
+
To rebuild the native module:
|
|
225
|
+
```bash
|
|
226
|
+
npm run build:native
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Requires Rust toolchain installed.
|
|
230
|
+
|
|
231
|
+
## License
|
|
232
|
+
|
|
233
|
+
MIT
|
package/commands/find.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Find code using hybrid approach (semantic + grep)
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Find code related to: $ARGUMENTS
|
|
6
|
+
|
|
7
|
+
Strategy:
|
|
8
|
+
1. First use `codebase_search` to find semantically related code
|
|
9
|
+
2. From the results, identify specific function/class names
|
|
10
|
+
3. Use grep to find all occurrences of those identifiers
|
|
11
|
+
4. Combine findings into a comprehensive answer
|
|
12
|
+
|
|
13
|
+
If the semantic index doesn't exist, run `index_codebase` first.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Index the codebase for semantic search
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Run the `index_codebase` tool to create or update the semantic search index.
|
|
6
|
+
|
|
7
|
+
Show progress and final statistics including:
|
|
8
|
+
- Number of files processed
|
|
9
|
+
- Number of chunks indexed
|
|
10
|
+
- Tokens used
|
|
11
|
+
- Duration
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Search codebase by meaning using semantic search
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Use the `codebase_search` tool to find code related to: $ARGUMENTS
|
|
6
|
+
|
|
7
|
+
If the index doesn't exist yet, run `index_codebase` first.
|
|
8
|
+
|
|
9
|
+
Return the most relevant results with file paths and line numbers.
|