Documentation
¶
Overview ¶
Package semantic provides local semantic code search using Ollama embeddings.
Index ¶
- func FormatResults(query string, results []SearchResult) string
- func InstallInstructions() string
- type Chunk
- type Chunker
- type Embedder
- type Engine
- func (e *Engine) BuildIndex(ctx context.Context) error
- func (e *Engine) BuildIndexAsync()
- func (e *Engine) Healthy(ctx context.Context) bool
- func (e *Engine) IndexSize() int
- func (e *Engine) IsReady() bool
- func (e *Engine) RebuildMemoryIndex(ctx context.Context) error
- func (e *Engine) Search(ctx context.Context, query string, topK int) ([]SearchResult, error)
- func (e *Engine) Shutdown()
- type Index
- func (idx *Index) Add(chunk Chunk, embedding []float32)
- func (idx *Index) AddBatch(chunks []Chunk, embeddings [][]float32)
- func (idx *Index) Clear()
- func (idx *Index) HybridSearch(query string, queryVec []float32, topK int) []SearchResult
- func (idx *Index) KeywordSearch(query string, topK int) []SearchResult
- func (idx *Index) Load() error
- func (idx *Index) RemoveByKind(kind string)
- func (idx *Index) Save() error
- func (idx *Index) Search(queryVec []float32, topK int) []SearchResult
- func (idx *Index) Size() int
- type SearchResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatResults ¶
func FormatResults(query string, results []SearchResult) string
FormatResults returns a human-readable summary of search results.
func InstallInstructions ¶
func InstallInstructions() string
InstallInstructions returns a human-readable string telling how to install Ollama.
Types ¶
type Chunk ¶
type Chunk struct {
ID string `json:"id"` // e.g. "file.go:42:func:DoThing"
File string `json:"file"` // relative path
Line int `json:"line"` // 1-based line number
Content string `json:"content"` // the code text (signature + body)
Language string `json:"language"` // "go", "typescript", "python", "rust"
Kind string `json:"kind"` // "function", "method", "struct", "interface", "class", "enum"
Doc string `json:"doc"` // preceding comment, if any
}
Chunk is a code fragment suitable for embedding and search.
type Chunker ¶
type Chunker struct{}
Chunker extracts code chunks from source files. The concrete implementation is selected at build time:
- chunker_treesitter.go (build tag: treesitter) — AST-level extraction via tree-sitter
- chunker_regex.go (build tag: !treesitter) — regex fallback
func NewChunker ¶
func NewChunker() *Chunker
NewChunker creates a chunker (implementation chosen at build time).
func (*Chunker) ChunkDir ¶
ChunkDir walks root and extracts code chunks from recognized source files.
func (*Chunker) ChunkMemoryDir ¶
ChunkMemoryDir reads markdown files from dir (typically .ok/memory/) and returns them as individual chunks, each a whole file with its heading as doc. Returns nil if the directory does not exist.
This method is shared by both the regex and tree-sitter chunker implementations since it handles markdown files, not source code.
type Embedder ¶
type Embedder struct {
// contains filtered or unexported fields
}
Embedder talks to a local Ollama instance to generate text embeddings.
func NewEmbedder ¶
NewEmbedder creates an embedder. baseURL defaults to "http://localhost:11434", model defaults to "nomic-embed-text".
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine ties together embedding, chunking, storage, and search.
func (*Engine) BuildIndex ¶
BuildIndex synchronously extracts chunks, embeds them, and stores results. This can take several minutes on large codebases. For background indexing, use BuildIndexAsync.
func (*Engine) BuildIndexAsync ¶
func (e *Engine) BuildIndexAsync()
BuildIndexAsync starts indexing in the background. Returns immediately. Progress is reflected in IndexSize() and IsReady().
func (*Engine) RebuildMemoryIndex ¶
RebuildMemoryIndex re-embeds only the memory chunks and saves the index. Call this after a memory document is added or updated at runtime.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index stores code chunks and their embeddings, supporting cosine-similarity search and JSON persistence.
func NewIndex ¶
NewIndex creates an index with optional persistence path. If persistPath is "", the index is memory-only.
func (*Index) HybridSearch ¶
func (idx *Index) HybridSearch(query string, queryVec []float32, topK int) []SearchResult
HybridSearch combines semantic + keyword results with reciprocal rank fusion (RRF).
func (*Index) KeywordSearch ¶
func (idx *Index) KeywordSearch(query string, topK int) []SearchResult
KeywordSearch finds chunks whose content/doc/file contains the query string.
func (*Index) RemoveByKind ¶
RemoveByKind removes all entries with the given chunk Kind (e.g. "memory").
type SearchResult ¶
type SearchResult struct {
Chunk Chunk `json:"chunk"`
Score float32 `json:"score"` // 0–1, higher = more similar
MatchType string `json:"matchType"` // "semantic" or "keyword"
}
SearchResult is a single match from a vector search.