Documentation
¶
Overview ¶
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
* ChatCLI - Knowledge index card (digest) builder. * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * A knowledge context never injects its corpus: attaching one puts only this * digest in the system prompt — a stable, budget-bounded table of contents * that tells the model WHAT the knowledge base covers and HOW to reach it * (passages are auto-retrieved per turn; agent/coder can additionally pull on * demand). A 6MB corpus and a 60MB corpus cost the same handful of tokens per * turn. The output is deterministic for a given context, so it lives in the * cached prompt prefix without busting provider caches.
* ChatCLI - Knowledge-mode ingestion for /context. * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * Knowledge mode turns a flattened documentation corpus (the JSONL emitted by * @docs-flatten, or any directory of docs) into a retrieval-first knowledge * base: the conversation receives only a compact index card, and passages are * pulled on demand. This file owns the ingestion side — parsing the * docs-flatten JSONL schema into the context's file list so every chunk keeps * its provenance (source path, title, repo, commit) instead of arriving as one * opaque multi-megabyte text file.
* ChatCLI - Knowledge-base query surface for the @knowledge tool. * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * PR 1 made knowledge contexts cheap to attach (index card + per-turn push); * this file is the pull side: the manager methods the @knowledge tool uses so * the agent can interrogate an attached corpus on demand — search passages, * read a whole source document, walk the table of contents. Everything is * budget-bounded and works keyless (hybrid retrieval has a BM25 floor).
* ChatCLI - Keyless lexical retrieval (BM25) for knowledge contexts. * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * The embedding-backed retrieval engine needs an API key (Voyage/OpenAI/ * Bedrock); knowledge mode must work without one — the project's keyless-first * rule. This file is that floor: a small pure-Go BM25 index over the same * Segment grain the vector path uses. It is built in memory on demand (a 6MB * corpus tokenizes in well under a second) and combined with cosine scores by * the hybrid retriever when embeddings are available.
* ChatCLI - Keyless BM25 ranking over ad-hoc document lists. * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * The knowledge corpus is not the only thing worth ranking without an API * key: saved sessions (and any future in-memory corpus) need the same * language-neutral scoring. This thin exported wrapper reuses the exact * tokenizer and BM25 scorer the knowledge segments use, so ranking behaves * identically across surfaces instead of each caller growing its own ad-hoc * relevance formula.
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * Semantic retrieval engine for /context. * * Raw whole-file injection blows the window on any non-trivial context. The * engine answers that: it segments a context into passages, embeds them once * (cached on disk per context), and at prompt time returns only the top-k * passages relevant to the current question. Provider-agnostic via the shared * embedding layer; a Null/absent provider disables retrieval and the manager * falls back to the legacy whole-content path with zero regression.
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0 * * Passage segmentation for semantic /context retrieval. * * The legacy FileChunk groups WHOLE files into ~30k-token buckets — the right * grain for "inject everything under a token budget", the wrong grain for * retrieval: a 30k-token chunk is itself too large to embed meaningfully or to * return as a focused answer. Segment is the retrieval grain: line-aware windows * of a few hundred tokens with a small overlap so a match never falls in a seam. * Whole files stay verbatim for non-RAG attachments; segments exist only to be * embedded and ranked.
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
* ChatCLI - Command Line Interface for LLM interaction * Copyright (c) 2024 Edilson Freitas * License: Apache-2.0
Index ¶
- Constants
- func BuildKnowledgeDigest(fc *FileContext, budget int) string
- func FormatKnowledgeHits(query string, hits []KnowledgeHit) string
- func FormatKnowledgeSegmentsBlock(contextName string, segs []Segment) string
- func FormatSegmentsBlock(contextName, query string, segs []Segment) string
- type AttachOptions
- type AttachedContext
- type ChunkStrategy
- type Chunker
- type ContextFilter
- type ContextMetrics
- type DocHit
- type FileChunk
- type FileContext
- type FormatOptions
- type KnowledgeHit
- type Manager
- func (m *Manager) AttachContext(sessionID, contextID string, priority int) error
- func (m *Manager) AttachContextWithOptions(sessionID, contextID string, opts AttachOptions) error
- func (m *Manager) AttachEmbeddingProvider(provider embedding.Provider)
- func (m *Manager) AttachedKnowledge(sessionID string) []*FileContext
- func (m *Manager) BuildPromptMessages(sessionID string, opts FormatOptions) ([]models.Message, error)
- func (m *Manager) BuildRetrievedContextMessages(ctx context.Context, sessionID, query string) ([]models.Message, error)
- func (m *Manager) CreateContext(ctx context.Context, name, description string, paths []string, ...) (*FileContext, error)
- func (m *Manager) DeleteContext(contextID string) error
- func (m *Manager) DetachContext(sessionID, contextID string) error
- func (m *Manager) GetAttachedContexts(sessionID string) ([]*FileContext, error)
- func (m *Manager) GetContext(contextID string) (*FileContext, error)
- func (m *Manager) GetContextByName(name string) (*FileContext, error)
- func (m *Manager) GetMetrics() *ContextMetrics
- func (m *Manager) GetSessionsForContext(contextID string) []string
- func (m *Manager) KnowledgeDigest(fc *FileContext) string
- func (m *Manager) KnowledgeDocument(sessionID, kb, source string, offset int) (page string, total int, nextOffset int, err error)
- func (m *Manager) KnowledgeDocumentByName(name, source string, offset int) (page string, total int, nextOffset int, err error)
- func (m *Manager) KnowledgeSearch(ctx context.Context, sessionID, kb, query string, k int) ([]KnowledgeHit, error)
- func (m *Manager) KnowledgeTOC(sessionID, kb, prefix string) (string, error)
- func (m *Manager) KnowledgeTOCByName(name, prefix string) (string, error)
- func (m *Manager) ListContexts(filter *ContextFilter) ([]*FileContext, error)
- func (m *Manager) MergeContexts(name, description string, contextIDs []string, opts MergeOptions) (*FileContext, error)
- func (m *Manager) RenderContext(name string) (string, error)
- func (m *Manager) RetrievalEnabled() bool
- func (m *Manager) UpdateContext(ctx context.Context, name string, newPaths []string, newMode ProcessingMode, ...) (*FileContext, error)
- type MergeOptions
- type ProcessingMode
- type Processor
- type RetrievalEngine
- func (e *RetrievalEngine) DropCache(contextID string)
- func (e *RetrievalEngine) Enabled() bool
- func (e *RetrievalEngine) Retrieve(ctx context.Context, fc *FileContext, query string, k int) ([]Segment, error)
- func (e *RetrievalEngine) RetrieveHybrid(ctx context.Context, fc *FileContext, query string, k int) ([]Segment, error)
- type ScanOptionsMetadata
- type Segment
- type SegmentOptions
- type Storage
- func (s *Storage) DeleteContext(contextID string) error
- func (s *Storage) ExportContext(ctx *FileContext, targetPath string) error
- func (s *Storage) GetStoragePath() string
- func (s *Storage) ImportContext(sourcePath string) (*FileContext, error)
- func (s *Storage) LoadAllContexts() ([]*FileContext, error)
- func (s *Storage) LoadContext(contextID string) (*FileContext, error)
- func (s *Storage) SaveContext(ctx *FileContext) error
- type ValidationResult
- type Validator
- func (v *Validator) ValidateContext(ctx *FileContext) *ValidationResult
- func (v *Validator) ValidateDescription(description string) error
- func (v *Validator) ValidateMode(mode ProcessingMode) error
- func (v *Validator) ValidateName(name string) error
- func (v *Validator) ValidatePriority(priority int) error
- func (v *Validator) ValidateTags(tags []string) error
- func (v *Validator) ValidateTotalSize(size int64) error
Constants ¶
const ( // Tamanho alvo por chunk (em tokens estimados) DefaultChunkTargetTokens = 30000 // ~120KB de texto // Tamanho máximo por chunk MaxChunkTokens = 50000 // ~200KB de texto // Tamanho mínimo para considerar dividir MinFilesForChunking = 10 )
const ( MaxContextNameLength = 64 MaxTotalSizeBytes = 200 * 1024 * 1024 // 200MB MinNameLength = 3 MaxDescriptionLength = 500 )
const ( // DefaultRetrievalTopK is the passage count injected when --rag is used // without an explicit number. Exported so the CLI flag parser and the engine // share one source of truth. DefaultRetrievalTopK = 8 )
Variables ¶
This section is empty.
Functions ¶
func BuildKnowledgeDigest ¶ added in v1.136.0
func BuildKnowledgeDigest(fc *FileContext, budget int) string
BuildKnowledgeDigest renders the index card for a knowledge context. budget caps the output in bytes; <=0 takes the default. The model-facing scaffolding is literal English on purpose (prompt text, not UI).
func FormatKnowledgeHits ¶ added in v1.136.0
func FormatKnowledgeHits(query string, hits []KnowledgeHit) string
FormatKnowledgeHits renders search results for the tool transcript: each passage cites its knowledge base, source path and position, with a bounded snippet, so the model can follow up with `get` on the exact document.
func FormatKnowledgeSegmentsBlock ¶ added in v1.136.0
FormatKnowledgeSegmentsBlock renders passages pulled from a knowledge base. Same model-facing English scaffolding as FormatSegmentsBlock, with wording that matches the index-card contract: the corpus is searchable, not attached.
func FormatSegmentsBlock ¶ added in v1.133.0
FormatSegmentsBlock renders retrieved passages as a prompt block. The format mirrors formatChunk (literal English/emoji, not i18n — this is model-facing content, and the codebase keeps prompt scaffolding in English on purpose) and annotates each passage with its source file and line range so the model can cite precisely and the user can trace what was injected.
Types ¶
type AttachOptions ¶
type AttachOptions struct {
Priority int
SelectedChunks []int // Vazio = todos os chunks
RetrievalTopK int // > 0 ativa retrieval semântico (top-K trechos por turno)
}
AttachOptions define opções para anexar contextos
type AttachedContext ¶
type AttachedContext struct {
ContextID string `json:"context_id"` // ID do contexto
AttachedAt time.Time `json:"attached_at"` // Quando foi anexado
Priority int `json:"priority"` // Prioridade na ordem de mensagens (menor = primeiro)
SelectedChunks []int `json:"selected_chunks,omitempty"` // CORREÇÃO: Adicionado campo para chunks selecionados
// RetrievalTopK > 0 turns this attachment into semantic-retrieval mode:
// instead of injecting the whole content, only the top-K passages relevant
// to the current turn are injected (query-driven, so it lives in the
// volatile prompt zone, never the cached prefix). 0 = legacy whole-content.
RetrievalTopK int `json:"retrieval_top_k,omitempty"`
}
AttachedContext representa um contexto anexado a uma sessão
type ChunkStrategy ¶
type ChunkStrategy string
ChunkStrategy define a estratégia de divisão
const ( ChunkByDirectory ChunkStrategy = "directory" // Agrupar por diretório ChunkByFileType ChunkStrategy = "filetype" // Agrupar por tipo de arquivo ChunkBySize ChunkStrategy = "size" // Dividir por tamanho ChunkSmart ChunkStrategy = "smart" // Estratégia inteligente híbrida )
type Chunker ¶
type Chunker struct {
// contains filtered or unexported fields
}
Chunker divide arquivos em chunks inteligentes
func NewChunker ¶
NewChunker cria uma nova instância de Chunker
func (*Chunker) DivideIntoChunks ¶
func (c *Chunker) DivideIntoChunks(files []utils.FileInfo, strategy ChunkStrategy) ([]FileChunk, error)
DivideIntoChunks divide arquivos em chunks usando estratégia inteligente
type ContextFilter ¶
type ContextFilter struct {
Tags []string `json:"tags"` // Filtrar por tags
Mode ProcessingMode `json:"mode"` // Filtrar por modo
MinSize int64 `json:"min_size"` // Tamanho mínimo
MaxSize int64 `json:"max_size"` // Tamanho máximo
CreatedAfter *time.Time `json:"created_after"` // Criado após
CreatedBefore *time.Time `json:"created_before"` // Criado antes
NamePattern string `json:"name_pattern"` // Padrão regex para nome
}
ContextFilter filtra contextos ao listar
type ContextMetrics ¶
type ContextMetrics struct {
TotalContexts int `json:"total_contexts"`
AttachedContexts int `json:"attached_contexts"`
TotalFiles int `json:"total_files"`
TotalSizeBytes int64 `json:"total_size_bytes"`
ContextsByMode map[string]int `json:"contexts_by_mode"`
LastUpdated time.Time `json:"last_updated"`
StoragePath string `json:"storage_path"`
}
ContextMetrics contém métricas sobre o uso de contextos
type DocHit ¶ added in v1.164.0
DocHit is one ranked document from RankDocsBM25: the input slice index and its (unnormalized) BM25 score.
func RankDocsBM25 ¶ added in v1.164.0
RankDocsBM25 builds a transient BM25 index over docs and returns up to k hits in descending score order (ties break by ascending index, so results are deterministic). Documents that share no term with the query are absent from the result. The index is throwaway by design — session-sized corpora tokenize in milliseconds, and callers with a persistent corpus should use the knowledge store instead.
type FileChunk ¶
type FileChunk struct {
Index int `json:"index"` // Índice do chunk (1-based)
TotalChunks int `json:"total_chunks"` // Total de chunks
Files []utils.FileInfo `json:"files"` // Arquivos neste chunk
Description string `json:"description"` // Descrição do chunk
TotalSize int64 `json:"total_size"` // Tamanho total
EstTokens int `json:"est_tokens"` // Tokens estimados
}
FileChunk representa um chunk de arquivos
type FileContext ¶
type FileContext struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Files []utils.FileInfo `json:"files"`
Mode ProcessingMode `json:"mode"`
TotalSize int64 `json:"total_size"`
FileCount int `json:"file_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Tags []string `json:"tags"`
Metadata map[string]string `json:"metadata"`
ScanOptions utils.DirectoryScanOptions `json:"-"`
ScanOptionsMetadata ScanOptionsMetadata `json:"scan_options_metadata"`
Chunks []FileChunk `json:"chunks,omitempty"` // Chunks divididos (se modo chunked)
IsChunked bool `json:"is_chunked"` // Se foi dividido em chunks
ChunkStrategy string `json:"chunk_strategy,omitempty"` // Estratégia usada
}
FileContext representa um contexto gerenciado contendo arquivos e metadados
type FormatOptions ¶
type FormatOptions struct {
IncludeMetadata bool `json:"include_metadata"` // Incluir metadados no prompt
IncludeTimestamp bool `json:"include_timestamp"` // Incluir timestamp
Compact bool `json:"compact"` // Formato compacto (sem índice)
Role string `json:"role"`
}
FormatOptions opções para formatar contexto como prompt
type KnowledgeHit ¶ added in v1.136.0
KnowledgeHit is one retrieved passage tagged with its knowledge base.
type Manager ¶
type Manager struct {
Storage *Storage
// contains filtered or unexported fields
}
Manager gerencia contextos de forma thread-safe
func NewManager ¶
NewManager cria uma nova instância do gerenciador de contextos
func (*Manager) AttachContext ¶
AttachContext anexa um contexto a uma sessão (não envia à LLM ainda)
func (*Manager) AttachContextWithOptions ¶
func (m *Manager) AttachContextWithOptions(sessionID, contextID string, opts AttachOptions) error
CORREÇÃO 1: Função refatorada para usar a estrutura de dados correta do Manager. AttachContextWithOptions anexa contexto com opções avançadas
func (*Manager) AttachEmbeddingProvider ¶ added in v1.133.0
AttachEmbeddingProvider wires (or rewires) the embedding provider that powers semantic /context retrieval. A Null/absent provider still yields a live engine: Enabled() stays false (so --rag attachments degrade to whole content exactly as before), while knowledge-mode hybrid retrieval keeps its keyless BM25 floor. Safe to call once at startup; provider-agnostic across backends.
func (*Manager) AttachedKnowledge ¶ added in v1.136.0
func (m *Manager) AttachedKnowledge(sessionID string) []*FileContext
AttachedKnowledge returns the knowledge-mode contexts attached to the session, sorted by name for deterministic listings.
func (*Manager) BuildPromptMessages ¶
func (m *Manager) BuildPromptMessages(sessionID string, opts FormatOptions) ([]models.Message, error)
CORREÇÃO 2: Refatorada para usar a estrutura de dados correta e lidar com chunks selecionados. BuildPromptMessages agora considera chunks selecionados
func (*Manager) BuildRetrievedContextMessages ¶ added in v1.133.0
func (m *Manager) BuildRetrievedContextMessages(ctx context.Context, sessionID, query string) ([]models.Message, error)
BuildRetrievedContextMessages runs per-turn retrieval for every attachment that is query-driven — knowledge contexts (always; hybrid BM25+vectors, no API key required) and --rag attachments (vector-only, needs a provider) — and returns one message per context holding only the passages relevant to query. Returns nil when nothing opted in or the query is empty, so the caller can skip the volatile block entirely.
A failure on a single context is logged and skipped, never fatal: a flaky embedding call must not break the turn. The query embedding happens outside the manager lock because it does network I/O.
func (*Manager) CreateContext ¶
func (m *Manager) CreateContext(ctx context.Context, name, description string, paths []string, mode ProcessingMode, tags []string, force bool) (*FileContext, error)
CreateContext cria um novo contexto a partir de caminhos de arquivos/diretórios
func (*Manager) DeleteContext ¶
DeleteContext remove um contexto permanentemente
func (*Manager) DetachContext ¶
DetachContext remove um contexto anexado de uma sessão
func (*Manager) GetAttachedContexts ¶
func (m *Manager) GetAttachedContexts(sessionID string) ([]*FileContext, error)
GetAttachedContexts retorna os contextos anexados a uma sessão
func (*Manager) GetContext ¶
func (m *Manager) GetContext(contextID string) (*FileContext, error)
GetContext retorna um contexto pelo ID
func (*Manager) GetContextByName ¶
func (m *Manager) GetContextByName(name string) (*FileContext, error)
GetContextByName retorna um contexto pelo nome
func (*Manager) GetMetrics ¶
func (m *Manager) GetMetrics() *ContextMetrics
GetMetrics retorna métricas sobre os contextos
func (*Manager) GetSessionsForContext ¶ added in v1.97.0
GetSessionsForContext returns all session IDs that have the given context attached.
func (*Manager) KnowledgeDigest ¶ added in v1.136.1
func (m *Manager) KnowledgeDigest(fc *FileContext) string
KnowledgeDigest returns fc's index card, memoized per context revision. Prompt assembly calls this every turn; without the memo a 50k-passage corpus would pay an O(corpus) walk and sort on each one.
func (*Manager) KnowledgeDocument ¶ added in v1.136.0
func (m *Manager) KnowledgeDocument(sessionID, kb, source string, offset int) (page string, total int, nextOffset int, err error)
KnowledgeDocument returns one page of a source document (all chunks whose source matches, in corpus order), plus pagination info. offset is a character offset into the assembled document; the next offset is returned when more content remains (0 = done).
func (*Manager) KnowledgeDocumentByName ¶ added in v1.163.0
func (m *Manager) KnowledgeDocumentByName(name, source string, offset int) (page string, total int, nextOffset int, err error)
KnowledgeDocumentByName is the catalog-resolved variant of KnowledgeDocument: it reads from a knowledge base by its stored name, independent of any session attachment. Read-only export surface (MCP resources).
func (*Manager) KnowledgeSearch ¶ added in v1.136.0
func (m *Manager) KnowledgeSearch(ctx context.Context, sessionID, kb, query string, k int) ([]KnowledgeHit, error)
KnowledgeSearch runs hybrid retrieval over the attached knowledge bases (one of them when kb is set) and returns up to k passages per base.
func (*Manager) KnowledgeTOC ¶ added in v1.136.0
KnowledgeTOC lists the source documents of the attached knowledge bases, optionally filtered by a path prefix. Rendering is model-facing English, consistent with the other prompt scaffolding in this package.
func (*Manager) KnowledgeTOCByName ¶ added in v1.163.0
KnowledgeTOCByName is the catalog-resolved variant of KnowledgeTOC: it lists a knowledge base by its stored name, independent of any session attachment. Read-only export surface (MCP resources).
func (*Manager) ListContexts ¶
func (m *Manager) ListContexts(filter *ContextFilter) ([]*FileContext, error)
ListContexts lista todos os contextos com filtro opcional
func (*Manager) MergeContexts ¶
func (m *Manager) MergeContexts(name, description string, contextIDs []string, opts MergeOptions) (*FileContext, error)
MergeContexts mescla múltiplos contextos em um novo
func (*Manager) RenderContext ¶ added in v1.163.0
RenderContext renders one context's content by name for read-only export (MCP resources). Knowledge contexts render their index card — the corpus itself is read per document via KnowledgeTOCByName/KnowledgeDocumentByName.
func (*Manager) RetrievalEnabled ¶ added in v1.133.0
RetrievalEnabled reports whether a real embedding provider backs retrieval.
func (*Manager) UpdateContext ¶ added in v1.35.0
func (m *Manager) UpdateContext(ctx context.Context, name string, newPaths []string, newMode ProcessingMode, newTags []string, newDescription string) (*FileContext, error)
UpdateContext atualiza um contexto existente
type MergeOptions ¶
type MergeOptions struct {
RemoveDuplicates bool `json:"remove_duplicates"` // Remove arquivos duplicados
SortByPath bool `json:"sort_by_path"` // Ordena por caminho
PreferNewer bool `json:"prefer_newer"` // Prefere versões mais recentes em duplicatas
Tags []string `json:"tags"` // Tags para o contexto mesclado
}
MergeOptions configura como contextos devem ser mesclados
type ProcessingMode ¶
type ProcessingMode string
ProcessingMode define o modo de processamento de arquivos no contexto
const ( ModeFull ProcessingMode = "full" // Conteúdo completo ModeSummary ProcessingMode = "summary" // Apenas estrutura ModeChunked ProcessingMode = "chunked" // Dividido em chunks ModeSmart ProcessingMode = "smart" // Seleção inteligente // ModeKnowledge é retrieval-first: o attach injeta só um index card e os // trechos relevantes são recuperados por turno (BM25 keyless + embeddings // quando configurados) — corpora de vários MB sem estourar a janela. ModeKnowledge ProcessingMode = "knowledge" )
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor processa arquivos e diretórios para contextos
func NewProcessor ¶
NewProcessor cria uma nova instância de Processor
func (*Processor) EstimateTokenCount ¶
EstimateTokenCount estima o número de tokens em um conjunto de arquivos
func (*Processor) ProcessPaths ¶
func (p *Processor) ProcessPaths(ctx context.Context, paths []string, mode ProcessingMode) ([]utils.FileInfo, utils.DirectoryScanOptions, error)
ProcessPaths processa múltiplos caminhos baseado no modo
type RetrievalEngine ¶ added in v1.133.0
type RetrievalEngine struct {
// contains filtered or unexported fields
}
RetrievalEngine builds and queries per-context passage vectors.
func NewRetrievalEngine ¶ added in v1.133.0
func NewRetrievalEngine(provider embedding.Provider, baseDir string, logger *zap.Logger) *RetrievalEngine
NewRetrievalEngine wires an engine over an embedding provider. baseDir is the directory where per-context vector caches live (alongside the context JSON). A Null provider is a valid input: vector paths report Enabled()=false while the lexical (keyless) hybrid path stays fully functional.
func (*RetrievalEngine) DropCache ¶ added in v1.133.0
func (e *RetrievalEngine) DropCache(contextID string)
DropCache removes a context's persisted vector file. Called when a context is deleted or its files change wholesale, so no orphaned cache lingers on disk.
func (*RetrievalEngine) Enabled ¶ added in v1.133.0
func (e *RetrievalEngine) Enabled() bool
Enabled reports whether a real embedding provider backs the engine.
func (*RetrievalEngine) Retrieve ¶ added in v1.133.0
func (e *RetrievalEngine) Retrieve(ctx context.Context, fc *FileContext, query string, k int) ([]Segment, error)
Retrieve returns the top-k passages of fc most relevant to query. Segments, the vector-index handle and its prune run come from the same fingerprint cache the hybrid path uses — previously this path re-segmented the context and re-parsed the whole persisted vector JSON on EVERY query, the exact per-call cost the cache exists to avoid. It embeds only segments not already cached, so repeated calls are cheap and never serve a match against stale text.
func (*RetrievalEngine) RetrieveHybrid ¶ added in v1.136.0
func (e *RetrievalEngine) RetrieveHybrid(ctx context.Context, fc *FileContext, query string, k int) ([]Segment, error)
RetrieveHybrid returns the top-k passages of fc most relevant to query, blending keyless BM25 with cosine similarity when an embedding provider is configured. This is the knowledge-mode path: unlike Retrieve it never requires an API key — without a provider it degrades to lexical-only, and a failing embedding call degrades the same way instead of breaking the turn.
Scalability contract: BM25 does RECALL over the whole corpus (in-memory, fingerprint-cached); embeddings only RERANK the candidate pool. Per-query embedding cost is bounded by hybridMaxPool regardless of corpus size — a 60MB corpus is never embedded wholesale, and the vector cache only ever holds passages that some query actually surfaced.
type ScanOptionsMetadata ¶
type ScanOptionsMetadata struct {
MaxTotalSize int64 `json:"max_total_size"`
MaxFilesToProcess int `json:"max_files_to_process"`
Extensions []string `json:"extensions"`
ExcludeDirs []string `json:"exclude_dirs"`
ExcludePatterns []string `json:"exclude_patterns"`
IncludeHidden bool `json:"include_hidden"`
}
ScanOptionsMetadata contém versão serializável das opções de scan
type Segment ¶ added in v1.133.0
type Segment struct {
ID string // stable content hash — the vector-index key
FilePath string
FileType string
StartLine int // 1-based, inclusive
EndLine int // 1-based, inclusive
Content string
}
Segment is one retrievable passage of a file.
func SegmentFiles ¶ added in v1.133.0
func SegmentFiles(files []utils.FileInfo, opts SegmentOptions) []Segment
SegmentFiles splits every file into overlapping, line-aware passages. The output order is deterministic (file order, then top-to-bottom), and segment ids are content hashes so re-segmenting unchanged files yields identical ids — which lets the vector index skip re-embedding work that hasn't changed.
type SegmentOptions ¶ added in v1.133.0
type SegmentOptions struct {
MaxChars int // soft cap per segment (~4 chars/token); default 1200 ≈ 300 tokens
OverlapLines int // lines replayed at the start of the next segment; default 2
}
SegmentOptions tunes how files are split into passages.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage gerencia a persistência de contextos em disco
func NewStorage ¶
NewStorage cria uma nova instância de Storage
func (*Storage) DeleteContext ¶
DeleteContext deleta um contexto do disco
func (*Storage) ExportContext ¶
func (s *Storage) ExportContext(ctx *FileContext, targetPath string) error
ExportContext exporta um contexto para um arquivo específico
func (*Storage) GetStoragePath ¶
GetStoragePath retorna o caminho base de armazenamento
func (*Storage) ImportContext ¶
func (s *Storage) ImportContext(sourcePath string) (*FileContext, error)
ImportContext importa um contexto de um arquivo
func (*Storage) LoadAllContexts ¶
func (s *Storage) LoadAllContexts() ([]*FileContext, error)
LoadAllContexts carrega todos os contextos do disco
func (*Storage) LoadContext ¶
func (s *Storage) LoadContext(contextID string) (*FileContext, error)
LoadContext carrega um contexto do disco
func (*Storage) SaveContext ¶
func (s *Storage) SaveContext(ctx *FileContext) error
SaveContext salva um contexto em disco
type ValidationResult ¶
type ValidationResult struct {
Valid bool `json:"valid"`
Errors []string `json:"errors"`
Warnings []string `json:"warnings"`
}
ValidationResult resultado da validação de um contexto
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator valida contextos e suas operações
func NewValidator ¶
NewValidator cria uma nova instância de Validator
func (*Validator) ValidateContext ¶
func (v *Validator) ValidateContext(ctx *FileContext) *ValidationResult
ValidateContext valida um contexto completo
func (*Validator) ValidateDescription ¶
ValidateDescription valida a descrição de um contexto
func (*Validator) ValidateMode ¶
func (v *Validator) ValidateMode(mode ProcessingMode) error
ValidateMode valida o modo de processamento
func (*Validator) ValidateName ¶
ValidateName valida o nome de um contexto
func (*Validator) ValidatePriority ¶
ValidatePriority valida a prioridade de anexação
func (*Validator) ValidateTags ¶
ValidateTags valida as tags de um contexto
func (*Validator) ValidateTotalSize ¶
ValidateTotalSize valida o tamanho total de arquivos