Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@hung319/opencode-code-index

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@hung319/opencode-code-index

Semantic codebase indexing and search for OpenCode - find code by meaning, not just keywords

latest
Source
npmnpm
Version
0.6.1
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

@hung319/opencode-code-index

npm version License: MIT Downloads Build Status Node.js

Stop grepping for concepts. Start searching for meaning.

@hung319/opencode-code-index brings semantic understanding to your OpenCode workflow. Instead of guessing function names or grepping for keywords, ask your codebase questions in plain English.

Forked from opencode-codebase-index with optimizations for large codebases.

🚀 Why Use This?

  • 🧠 Semantic Search: Finds "user authentication" logic even if the function is named check_creds.
  • Blazing Fast Indexing: Powered by a Rust native module using tree-sitter and usearch. Incremental updates take milliseconds.
  • 🌿 Branch-Aware: Seamlessly handles git branch switches — reuses embeddings, filters stale results.
  • 🔒 Privacy Focused: Your vector index is stored locally in your project.
  • 🔌 Model Agnostic: Works out-of-the-box with GitHub Copilot, OpenAI, Gemini, or local Ollama models.

⚡ Quick Start

  • Install the plugin

    npm install @hung319/opencode-code-index
    
  • Add to opencode.json

    {
      "plugin": ["@hung319/opencode-code-index"]
    }
    
  • Index your codebase Run /index or ask the agent to index your codebase. This only needs to be done once — subsequent updates are incremental.

  • Start Searching Ask:

    "Find the function that handles credit card validation errors"

🔍 See It In Action

Scenario: You're new to a codebase and need to fix a bug in the payment flow.

Without Plugin (grep):

  • grep "payment" . → 500 results (too many)
  • grep "card" . → 200 results (mostly UI)
  • grep "stripe" . → 50 results (maybe?)

With opencode-codebase-index: You ask: "Where is the payment validation logic?"

Plugin returns:

src/services/billing.ts:45  (Class PaymentValidator)
src/utils/stripe.ts:12      (Function validateCardToken)
src/api/checkout.ts:89      (Route handler for /pay)

🎯 When to Use What

ScenarioToolWhy
Don't know the function namecodebase_searchSemantic search finds by meaning
Exploring unfamiliar codebasecodebase_searchDiscovers related code across files
Just need to find locationscodebase_peekReturns metadata only, saves ~90% tokens
Know exact identifiergrepFaster, finds all occurrences
Need ALL matchesgrepSemantic returns top N only
Mixed discovery + precision/find (hybrid)Best of both worlds

Rule of thumb: codebase_peek to find locations → Read to examine → grep for precision.

📊 Token Usage

In our testing across open-source codebases (axios, express), we observed up to 90% reduction in token usage for conceptual queries like "find the error handling middleware".

Why It Saves Tokens

  • Without plugin: Agent explores files, reads code, backtracks, explores more
  • With plugin: Semantic search returns relevant code immediately → less exploration

Key Takeaways

  • Significant savings possible: Up to 90% reduction in the best cases
  • Results vary: Savings depend on query type, codebase structure, and agent behavior
  • Best for discovery: Conceptual queries benefit most; exact identifier lookups should use grep
  • Complements existing tools: Provides a faster initial signal, doesn't replace grep/explore

When the Plugin Helps Most

  • Conceptual queries: "Where is the authentication logic?" (no keywords to grep for)
  • Unfamiliar codebases: You don't know what to search for yet
  • Large codebases: Semantic search scales better than exhaustive exploration

🛠️ How It Works

graph TD
    subgraph Indexing
    A[Source Code] -->|Tree-sitter| B[Semantic Chunks]
    B -->|Embedding Model| C[Vectors]
    C -->|uSearch| D[(Vector Store)]
    C -->|SQLite| G[(Embeddings DB)]
    B -->|BM25| E[(Inverted Index)]
    B -->|Branch Catalog| G
    end

    subgraph Searching
    Q[User Query] -->|Embedding Model| V[Query Vector]
    V -->|Cosine Similarity| D
    Q -->|BM25| E
    G -->|Branch Filter| F
    D --> F[Hybrid Fusion]
    E --> F
    F --> R[Ranked Results]
    end
  • Parsing: We use tree-sitter to intelligently parse your code into meaningful blocks (functions, classes, interfaces). JSDoc comments and docstrings are automatically included with their associated code.

Supported Languages: TypeScript, JavaScript, Python, Rust, Go, Java, C#, Ruby, Bash, C, C++, JSON, TOML, YAML 2. Chunking: Large blocks are split with overlapping windows to preserve context across chunk boundaries. 3. Embedding: These blocks are converted into vector representations using your configured AI provider. 4. Storage: Embeddings are stored in SQLite (deduplicated by content hash) and vectors in usearch with F16 quantization for 50% memory savings. A branch catalog tracks which chunks exist on each branch. 5. Hybrid Search: Combines semantic similarity (vectors) with BM25 keyword matching, filtered by current branch.

Performance characteristics:

  • Incremental indexing: ~50ms check time — only re-embeds changed files
  • Smart chunking: Understands code structure to keep functions whole, with overlap for context
  • Native speed: Core logic written in Rust for maximum performance
  • Memory efficient: F16 vector quantization reduces index size by 50%
  • Branch-aware: Automatically tracks which chunks exist on each git branch
  • Provider validation: Detects embedding provider/model changes and requires rebuild to prevent garbage results

🌿 Branch-Aware Indexing

The plugin automatically detects git branches and optimizes indexing across branch switches.

How It Works

When you switch branches, code changes but embeddings for unchanged content remain the same. The plugin:

  • Stores embeddings by content hash: Embeddings are deduplicated across branches
  • Tracks branch membership: A lightweight catalog tracks which chunks exist on each branch
  • Filters search results: Queries only return results relevant to the current branch

Benefits

ScenarioWithout Branch AwarenessWith Branch Awareness
Switch to feature branchRe-index everythingInstant — reuse existing embeddings
Return to mainRe-index everythingInstant — catalog already exists
Search on branchMay return stale resultsOnly returns current branch's code

Automatic Behavior

  • Branch detection: Automatically reads from .git/HEAD
  • Re-indexing on switch: Triggers when you switch branches (via file watcher)
  • Legacy migration: Automatically migrates old indexes on first run
  • Garbage collection: Health check removes orphaned embeddings and chunks

Storage Structure

.opencode/index/
├── codebase.db           # SQLite: embeddings, chunks, branch catalog
├── vectors.usearch       # Vector index (uSearch)
├── inverted-index.json   # BM25 keyword index
└── file-hashes.json      # File change detection

🧰 Tools Available

The plugin exposes these tools to the OpenCode agent:

The primary tool. Searches code by describing behavior.

  • Use for: Discovery, understanding flows, finding logic when you don't know the names.
  • Example: "find the middleware that sanitizes input"

Writing good queries:

✅ Good queries (describe behavior)❌ Bad queries (too vague)
"function that validates email format""email"
"error handling for failed API calls""error"
"middleware that checks authentication""auth middleware"
"code that calculates shipping costs""shipping"
"where user permissions are checked""permissions"

codebase_peek

Token-efficient discovery. Returns only metadata (file, line, name, type) without code content.

  • Use for: Finding WHERE code is before deciding what to read. Saves ~90% tokens vs codebase_search.
  • Example output:
    [1] function "validatePayment" at src/billing.ts:45-67 (score: 0.92)
    [2] class "PaymentProcessor" at src/processor.ts:12-89 (score: 0.87)
    
    Use Read tool to examine specific files.
    
  • Workflow: codebase_peek → find locations → Read specific files

index_codebase

Manually trigger indexing.

  • Use for: Forcing a re-index or checking stats.
  • Parameters: force (rebuild all), estimateOnly (check costs), verbose (show skipped files and parse failures).

index_status

Checks if the index is ready and healthy.

index_health_check

Maintenance tool to remove stale entries from deleted files and orphaned embeddings/chunks from the database.

index_metrics

Returns collected metrics about indexing and search performance. Requires debug.enabled and debug.metrics to be true.

  • Metrics include: Files indexed, chunks created, cache hit rate, search timing breakdown, GC stats, embedding API call stats.

index_logs

Returns recent debug logs with optional filtering.

  • Parameters: category (optional: search, embedding, cache, gc, branch), level (optional: error, warn, info, debug), limit (default: 50).

🎮 Slash Commands

The plugin automatically registers these slash commands:

CommandDescription
/search <query>Pure Semantic Search. Best for "How does X work?"
/find <query>Hybrid Search. Combines semantic search + grep. Best for "Find usage of X".
/indexUpdate Index. Forces a refresh of the codebase index.
/statusCheck Status. Shows if indexed, chunk count, and provider info.

⚙️ Configuration

Zero-config by default (uses auto mode). Customize in .opencode/codebase-index.json:

{
  "embeddingProvider": "auto",
  "scope": "project",
  "indexing": {
    "autoIndex": false,
    "watchFiles": true,
    "maxFileSize": 1048576,
    "maxChunksPerFile": 100,
    "semanticOnly": false,
    "autoGc": true,
    "gcIntervalDays": 7,
    "gcOrphanThreshold": 100,
    "requireProjectMarker": true
  },
  "search": {
    "maxResults": 20,
    "minScore": 0.1,
    "hybridWeight": 0.5,
    "contextLines": 0
  },
  "debug": {
    "enabled": false,
    "logLevel": "info",
    "metrics": false
  }
}

Options Reference

OptionDefaultDescription
embeddingProvider"auto"Which AI to use: auto, github-copilot, openai, google, ollama
scope"project"project = index per repo, global = shared index across repos
indexing
autoIndexfalseAutomatically index on plugin load
watchFilestrueRe-index when files change
maxFileSize1048576Skip files larger than this (bytes). Default: 1MB
maxChunksPerFile100Maximum chunks to index per file (controls token costs for large files)
semanticOnlyfalseWhen true, only index semantic nodes (functions, classes) and skip generic blocks
retries3Number of retry attempts for failed embedding API calls
retryDelayMs1000Delay between retries in milliseconds
autoGctrueAutomatically run garbage collection to remove orphaned embeddings/chunks
gcIntervalDays7Run GC on initialization if last GC was more than N days ago
gcOrphanThreshold100Run GC after indexing if orphan count exceeds this threshold
requireProjectMarkertrueRequire a project marker (.git, package.json, etc.) to enable file watching and auto-indexing. Prevents accidentally indexing large directories like home. Set to false to index any directory.
search
maxResults20Maximum results to return
minScore0.1Minimum similarity score (0-1). Lower = more results
hybridWeight0.5Balance between keyword (1.0) and semantic (0.0) search
contextLines0Extra lines to include before/after each match
debug
enabledfalseEnable debug logging and metrics collection
logLevel"info"Log level: error, warn, info, debug
logSearchtrueLog search operations with timing breakdown
logEmbeddingtrueLog embedding API calls (success, error, rate-limit)
logCachetrueLog cache hits and misses
logGctrueLog garbage collection operations
logBranchtrueLog branch detection and switches
metricsfalseEnable metrics collection (indexing stats, search timing, cache performance)

Embedding Providers

The plugin automatically detects available credentials in this order:

  • GitHub Copilot (Free if you have it)
  • OpenAI (Standard Embeddings)
  • Google (Gemini Embeddings)
  • Ollama (Local/Private - requires nomic-embed-text)

Rate Limits by Provider

Each provider has different rate limits. The plugin automatically adjusts concurrency and delays:

ProviderConcurrencyDelayBest For
GitHub Copilot14sSmall codebases (<1k files)
OpenAI3500msMedium codebases
Google5200msMedium-large codebases
Ollama5NoneLarge codebases (10k+ files)

For large codebases, use Ollama locally to avoid rate limits:

# Install the embedding model
ollama pull nomic-embed-text
// .opencode/codebase-index.json
{
  "embeddingProvider": "ollama"
}

📈 Performance

The plugin is built for speed with a Rust native module. Here are typical performance numbers (Apple M1):

Parsing (tree-sitter)

FilesChunksTime
1001,200~7ms
5006,000~32ms

Vector Search (usearch)

Index SizeSearch TimeThroughput
1,000 vectors0.7ms1,400 ops/sec
5,000 vectors1.2ms850 ops/sec
10,000 vectors1.3ms780 ops/sec

Database Operations (SQLite with batch)

Operation1,000 items10,000 items
Insert chunks4ms44ms
Add to branch2ms22ms
Check embedding exists<0.01ms<0.01ms

Batch vs Sequential Performance

Batch operations provide significant speedups:

OperationSequentialBatchSpeedup
Insert 1,000 chunks38ms4ms~10x
Add 1,000 to branch29ms2ms~14x
Insert 1,000 embeddings59ms40ms~1.5x

Run benchmarks yourself: npx tsx benchmarks/run.ts

🎯 Choosing a Provider

Use this decision tree to pick the right embedding provider:

                    ┌─────────────────────────┐
                    │ Do you have Copilot?    │
                    └───────────┬─────────────┘
                          ┌─────┴─────┐
                         YES          NO
                          │            │
              ┌───────────▼───────┐    │
              │ Codebase < 1k     │    │
              │ files?            │    │
              └─────────┬─────────┘    │
                  ┌─────┴─────┐        │
                 YES          NO       │
                  │            │       │
                  ▼            │       │
           ┌──────────┐        │       │
           │ Copilot  │        │       │
           │ (free)   │        │       │
           └──────────┘        │       │
                               ▼       ▼
                    ┌─────────────────────────┐
                    │ Need fastest indexing?  │
                    └───────────┬─────────────┘
                          ┌─────┴─────┐
                         YES          NO
                          │            │
                          ▼            ▼
                   ┌──────────┐ ┌──────────────┐
                   │ Ollama   │ │ OpenAI or    │
                   │ (local)  │ │ Google       │
                   └──────────┘ └──────────────┘

Provider Comparison

ProviderSpeedCostPrivacyBest For
OllamaFastestFreeFullLarge codebases, privacy-sensitive
GitHub CopilotSlow (rate limited)Free*CloudSmall codebases, existing subscribers
OpenAIMedium~$0.0001/1K tokensCloudGeneral use
GoogleFastFree tier availableCloudMedium-large codebases

*Requires active Copilot subscription

Setup by Provider

Ollama (Recommended for large codebases)

ollama pull nomic-embed-text
{ "embeddingProvider": "ollama" }

OpenAI

export OPENAI_API_KEY=sk-...
{ "embeddingProvider": "openai" }

Google

export GOOGLE_API_KEY=...
{ "embeddingProvider": "google" }

GitHub Copilot No setup needed if you have an active Copilot subscription.

{ "embeddingProvider": "github-copilot" }

⚠️ Tradeoffs

Be aware of these characteristics:

AspectReality
Search latency~800-1000ms per query (embedding API call)
First indexTakes time depending on codebase size (e.g., ~30s for 500 chunks)
Requires APINeeds an embedding provider (Copilot, OpenAI, Google, or local Ollama)
Token costsUses embedding tokens (free with Copilot, minimal with others)
Best forDiscovery and exploration, not exhaustive matching

💻 Local Development

  • Build:

    npm run build
    
  • Register in Test Project (use file:// URL in opencode.json):

    {
      "plugin": [
        "file:///path/to/opencode-codebase-index"
      ]
    }
    

    This loads directly from your source directory, so changes take effect after rebuilding.

🤝 Contributing

  • Fork the repository
  • Create a feature branch: git checkout -b feature/my-feature
  • Make your changes and add tests
  • Run checks: npm run build && npm run test:run && npm run lint
  • Commit: git commit -m "feat: add my feature"
  • Push and open a pull request

CI will automatically run tests and type checking on your PR.

Project Structure

├── src/
│   ├── index.ts              # Plugin entry point
│   ├── config/               # Configuration schema
│   ├── embeddings/           # Provider detection and API calls
│   ├── indexer/              # Core indexing logic + inverted index
│   ├── git/                  # Git utilities (branch detection)
│   ├── tools/                # OpenCode tool definitions
│   ├── utils/                # File collection, cost estimation
│   ├── native/               # Rust native module wrapper
│   └── watcher/              # File/git change watcher
├── native/
│   └── src/                  # Rust: tree-sitter, usearch, xxhash, SQLite
├── tests/                    # Unit tests (vitest)
├── commands/                 # Slash command definitions
├── skill/                    # Agent skill guidance
└── .github/workflows/        # CI/CD (test, build, publish)

Native Module

The Rust native module handles performance-critical operations:

  • tree-sitter: Language-aware code parsing with JSDoc/docstring extraction
  • usearch: High-performance vector similarity search with F16 quantization
  • SQLite: Persistent storage for embeddings, chunks, and branch catalog
  • BM25 inverted index: Fast keyword search for hybrid retrieval
  • xxhash: Fast content hashing for change detection

Rebuild with: npm run build:native (requires Rust toolchain)

License

MIT

Keywords

opencode

FAQs

Package last updated on 12 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts