Documentation
¶
Overview ¶
Package search maintains the full-text index and answers scoped queries.
The index holds derived, short-form documents rather than raw event JSON: the user's own prompt text, the agent's final replies, failed commands with their error head, shell commands without output, changed file paths, and session summaries. Every document points back to its source (kind + ref_id) for traceability.
Index ¶
- Constants
- Variables
- func Backfill(ctx context.Context, db *sql.DB) (int, error)
- func Delete(ctx context.Context, tx *sql.Tx, kind, refID string) error
- func FuseSessionRanks(streams [][]model.SearchResult, limit int) []model.SearchResult
- func IndexEvent(ctx context.Context, tx *sql.Tx, ev Event) error
- func MinSimilarities() (inject, searchFloor float64)
- func ResolveProject(ctx context.Context, db *sql.DB, owner, projectID, gitIdentity, name string) (string, error)
- func Search(ctx context.Context, db *sql.DB, ownerUserID string, req model.SearchRequest) (*model.SearchResponse, error)
- func SearchSessions(ctx context.Context, db *sql.DB, owner string, req model.SearchRequest) (*model.SearchResponse, error)
- func SearchWithOptions(ctx context.Context, db *sql.DB, ownerUserID string, req model.SearchRequest, ...) (*model.SearchResponse, error)
- func SetSemantic(s Semantic)
- func Terms(expr string) []string
- func Upsert(ctx context.Context, tx *sql.Tx, d *Doc) error
- type Doc
- type Event
- type Options
- type ProjectRef
- type Semantic
- type SemanticHit
Constants ¶
const ( KindPrompt = "prompt" KindReply = "reply" KindFailure = "failure" KindCommand = "command" KindChange = "change" KindSummary = "summary" KindMemory = "memory" )
Document kinds.
const ( MarkOpen = "\uE000" MarkClose = "\uE001" )
Snippet highlight markers. Brackets read well in terminals; the web UI asks for private-use code points, which cannot be confused with captured text.
Variables ¶
var ErrBadRequest = errors.New("bad search request")
ErrBadRequest marks invalid search requests.
Functions ¶
func Backfill ¶
Backfill indexes events that predate the search index. It runs once, at server start, and is idempotent.
func Delete ¶
Delete removes the document (kind, ref_id) from the index inside tx, if present; its vectors are removed later by the embedding sweep.
func FuseSessionRanks ¶ added in v0.1.4
func FuseSessionRanks(streams [][]model.SearchResult, limit int) []model.SearchResult
FuseSessionRanks gives each retrieval view one vote per distinct session. Repeated matching passages cannot give a long session extra votes. Earlier streams supply the representative source; score ties keep their stable order.
func IndexEvent ¶
IndexEvent indexes one event inside tx (no-op for events without a doc).
func MinSimilarities ¶
func MinSimilarities() (inject, searchFloor float64)
MinSimilarities returns the current semantic thresholds (see Semantic.MinSimilarities); zero when semantic search is off.
func ResolveProject ¶
func ResolveProject(ctx context.Context, db *sql.DB, owner, projectID, gitIdentity, name string) (string, error)
ResolveProject finds an existing project by ID, Git identity or name (the same matching rules as project creation), without creating one.
func Search ¶
func Search(ctx context.Context, db *sql.DB, ownerUserID string, req model.SearchRequest) (*model.SearchResponse, error)
Search runs req for the owner. Keyword search segments the query with jieba; results matching every term come first, then partial matches. When semantic search is available, its results are fused with the keyword results by Reciprocal Rank Fusion.
func SearchSessions ¶ added in v0.1.4
func SearchSessions(ctx context.Context, db *sql.DB, owner string, req model.SearchRequest) (*model.SearchResponse, error)
SearchSessions combines session summaries with individual records using independent BM25 rankings, then returns distinct sessions. Both views use the same owner/project/time filters as Search. No summary or answer is generated.
func SearchWithOptions ¶ added in v0.1.4
func SearchWithOptions(ctx context.Context, db *sql.DB, ownerUserID string, req model.SearchRequest, opts Options) (*model.SearchResponse, error)
SearchWithOptions may return completed keyword hits and semantic hits when only its keyword budget expires. Caller cancellation and other errors remain errors. An empty fallback is also an error, not a successful empty search.
func SetSemantic ¶
func SetSemantic(s Semantic)
SetSemantic enables hybrid search (nil disables it). Called once at server start.
Types ¶
type Doc ¶
type Doc struct {
Kind string
RefID string // event_id, or session_id for summaries
ProjectID string
SessionID string
DeviceID string
AgentType string
OccurredAt time.Time
Title string
Body string
Files string
}
Doc is one searchable unit.
func DocFromEvent ¶
DocFromEvent derives the searchable document for an event, or nil when the event carries nothing worth finding later.
type Event ¶
type Event struct {
EventID string
EventType string
ProjectID string
SessionID string
DeviceID string
AgentType string
OccurredAt time.Time
Payload []byte
}
Event is the subset of an ingested event the indexer needs.
type Options ¶ added in v0.1.4
type Options struct {
// KeywordTimeout reserves time for semantic retrieval when a hybrid keyword
// query is slow. Zero, keyword-only mode, or no semantic provider is unbounded.
KeywordTimeout time.Duration
// SemanticQuery optionally supplies a compact semantic query while lexical
// matching and filters still use the original request.
SemanticQuery string
// Parallel overlaps semantic retrieval with keyword work. Both branches are
// joined before returning, including on cancellation or a keyword failure.
Parallel bool
}
Options controls latency-sensitive callers without changing normal search.
type ProjectRef ¶
type ProjectRef struct {
ID string `json:"project_id,omitempty"`
GitIdentity string `json:"git_identity,omitempty"`
Name string `json:"project_name,omitempty"`
Cwd string `json:"cwd,omitempty"`
}
ProjectRef identifies a project the way different callers can: by ID, by Git identity or name (clients that inspect their working directory), or by a working directory on the calling device (remote MCP tools, which cannot inspect the caller's filesystem but know the agent's cwd).
type Semantic ¶
type Semantic interface {
SemanticSearch(ctx context.Context, query string, projectIDs []string, k int) ([]SemanticHit, error)
// MinSimilarities returns the model's thresholds: for injecting a
// meaning-only match into an agent's context, and for keeping it in
// search results at all.
MinSimilarities() (inject, searchFloor float64)
}
Semantic finds documents similar in meaning to a query. It returns no hits (and no error) when semantic search is unavailable at the moment.
type SemanticHit ¶
SemanticHit is a document found by vector similarity.