search

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	KindPrompt  = "prompt"
	KindReply   = "reply"
	KindFailure = "failure"
	KindCommand = "command"
	KindChange  = "change"
	KindSummary = "summary"
	KindMemory  = "memory"
)

Document kinds.

View Source
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

View Source
var ErrBadRequest = errors.New("bad search request")

ErrBadRequest marks invalid search requests.

Functions

func Backfill

func Backfill(ctx context.Context, db *sql.DB) (int, error)

Backfill indexes events that predate the search index. It runs once, at server start, and is idempotent.

func Delete

func Delete(ctx context.Context, tx *sql.Tx, kind, refID string) error

Delete removes the document (kind, ref_id) from the index inside tx, if present; its vectors are removed later by the embedding sweep.

func IndexEvent

func IndexEvent(ctx context.Context, tx *sql.Tx, ev Event) error

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(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 SetSemantic

func SetSemantic(s Semantic)

SetSemantic enables hybrid search (nil disables it). Called once at server start.

func Terms

func Terms(expr string) []string

Terms turns a jieba_query() expression into its top-level terms, dropping punctuation and function words. Each term is a valid FTS5 expression on its own: a quoted phrase, a prefix term, or a parenthesized OR-group (for ASCII words with pinyin alternatives).

func Upsert

func Upsert(ctx context.Context, tx *sql.Tx, d *Doc) error

Upsert writes d into the index inside tx, replacing an existing document with the same (kind, ref_id). The FTS table is external-content, so the old row's text has to be removed explicitly before it is replaced.

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

func DocFromEvent(ev Event) *Doc

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

func (ProjectRef) Resolve

func (ref ProjectRef) Resolve(ctx context.Context, db *sql.DB, owner, deviceID string) (string, error)

Resolve finds the project for ref; deviceID is needed for Cwd lookups. It returns "" when nothing matches.

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

type SemanticHit struct {
	DocID      int64
	Text       string // the best-matching chunk
	Similarity float64
}

SemanticHit is a document found by vector similarity.

Jump to

Keyboard shortcuts

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