semantic

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package semantic provides local semantic code search using Ollama embeddings.

Index

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

func (c *Chunker) ChunkDir(root string) ([]Chunk, error)

ChunkDir walks root and extracts code chunks from recognized source files.

func (*Chunker) ChunkMemoryDir

func (c *Chunker) ChunkMemoryDir(dir string) []Chunk

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

func NewEmbedder(baseURL, model string) *Embedder

NewEmbedder creates an embedder. baseURL defaults to "http://localhost:11434", model defaults to "nomic-embed-text".

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, text string) ([]float32, error)

Embed converts text to a vector. Returns nil on any error.

func (*Embedder) Healthy

func (e *Embedder) Healthy(ctx context.Context) bool

Healthy reports whether Ollama is reachable and the model is available.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine ties together embedding, chunking, storage, and search.

func NewEngine

func NewEngine(projectRoot string) *Engine

NewEngine creates a semantic search engine for the given project root.

func (*Engine) BuildIndex

func (e *Engine) BuildIndex(ctx context.Context) error

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) Healthy

func (e *Engine) Healthy(ctx context.Context) bool

Healthy reports whether Ollama is reachable.

func (*Engine) IndexSize

func (e *Engine) IndexSize() int

IndexSize returns the number of indexed chunks.

func (*Engine) IsReady

func (e *Engine) IsReady() bool

IsReady reports whether the index has been built at least once.

func (*Engine) RebuildMemoryIndex

func (e *Engine) RebuildMemoryIndex(ctx context.Context) error

RebuildMemoryIndex re-embeds only the memory chunks and saves the index. Call this after a memory document is added or updated at runtime.

func (*Engine) Search

func (e *Engine) Search(ctx context.Context, query string, topK int) ([]SearchResult, error)

Search performs a semantic + keyword hybrid search. Falls back to keyword-only if the embedder is not healthy.

func (*Engine) Shutdown

func (e *Engine) Shutdown()

Shutdown waits for any background indexing to complete, then releases resources. Call before process exit to avoid goroutine leaks.

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

func NewIndex(persistPath string) *Index

NewIndex creates an index with optional persistence path. If persistPath is "", the index is memory-only.

func (*Index) Add

func (idx *Index) Add(chunk Chunk, embedding []float32)

Add appends a chunk with its embedding to the index.

func (*Index) AddBatch

func (idx *Index) AddBatch(chunks []Chunk, embeddings [][]float32)

AddBatch appends multiple chunks with embeddings.

func (*Index) Clear

func (idx *Index) Clear()

Clear removes all entries from the index.

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) Load

func (idx *Index) Load() error

Load reads the index from disk.

func (*Index) RemoveByKind

func (idx *Index) RemoveByKind(kind string)

RemoveByKind removes all entries with the given chunk Kind (e.g. "memory").

func (*Index) Save

func (idx *Index) Save() error

Save persists the index to disk as JSON.

func (*Index) Search

func (idx *Index) Search(queryVec []float32, topK int) []SearchResult

Search finds the top-k chunks most similar to the query embedding. Returns results sorted by descending cosine similarity.

func (*Index) Size

func (idx *Index) Size() int

Size returns the number of indexed chunks.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL