knowledge

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package knowledge implements bee's per-project on-disk knowledge store.

records are markdown files with a YAML frontmatter header carrying freeform tags, an explicit priority, and an optional expiration. the scanner returns entries cheaply (header only); record bodies are loaded lazily so listings stay fast even on large stores.

Index

Constants

View Source
const (
	TagPersonal = "personal"
	TagGuidance = "guidance"
	TagProject  = "project"
	TagExternal = "external"
)

well-known tag names used by the migration shim when converting legacy type-tagged files. callers should treat these as ordinary tag strings — nothing in the store validates against this list.

View Source
const DefaultPriority = 3

DefaultPriority is the assumed priority when frontmatter omits the field.

View Source
const FrontmatterMaxLines = 30

FrontmatterMaxLines bounds the byte budget for header parsing. anything past this is treated as body.

View Source
const IndexFileName = "INDEX.md"

IndexFileName is the rebuilt-on-write index inside StoreDirName. it is excluded from scans so the index entry never appears as a record.

View Source
const MaxEntries = 200

MaxEntries caps how many entries ScanStore will return per call. larger stores still load but the tail is dropped after mtime sort.

View Source
const MaxResults = 5

MaxResults caps the records Query returns regardless of caller intent.

View Source
const StoreDirName = "store"

StoreDirName is the leaf directory bee writes records into.

Variables

This section is empty.

Functions

func AgeSince

func AgeSince(modified time.Time) string

AgeSince renders a short natural-language age string for modified. used to head each record in the assembled system prompt.

func Body

func Body(path string) (string, error)

Body returns the record's content with the frontmatter stripped.

func ExtractTags

func ExtractTags(ctx context.Context, p llm.Provider, model, userQuery string) ([]string, error)

ExtractTags drives a side-channel LLM call to map a free-text query onto 1-3 tag keywords. callers feed the result back into Query via Options. errors degrade quietly to (nil, err) so the agent loop can keep going on the deterministic phase-1 score.

func Index

func Index() (string, error)

Index returns the INDEX.md path inside StoreDir.

func ListEntries

func ListEntries(dir string) ([]string, error)

ListEntries returns every *.md path under dir except the INDEX. callers use it to cheaply count records without firing the parser.

func RebuildIndex

func RebuildIndex(dir string) error

RebuildIndex rewrites INDEX.md as a markdown table sorted by priority desc then name asc. callers run this after each WriteRecord but may also invoke directly to repair an out-of-sync index.

func StalenessNote

func StalenessNote(expiresAt time.Time) string

StalenessNote returns a one-line warning if the record's expiration has passed. zero-value (never-expires) records always return "".

func StoreDir

func StoreDir() (string, error)

StoreDir returns the directory bee will read/write knowledge records to.

resolution order:

  1. $BEE_STORE_DIR (must be absolute + non-near-root)
  2. if cwd is inside a git repo: ~/.bee/store/<slug-of-git-root>
  3. ~/.bee/store

when only the legacy path exists ( ~/.bee/projects/<slug>/memory or ~/.bee/memory ) StoreDir returns it as-is so existing data still loads.

func WriteRecord

func WriteRecord(dir string, r Record) (string, error)

WriteRecord persists r to dir/<r.Name>.md. validates frontmatter, writes via a temp file + rename so a crash never leaves a partial record, and finally refreshes the INDEX. returns the absolute path of the new file.

Types

type Entry

type Entry struct {
	Path        string    `json:"path"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Tags        []string  `json:"tags,omitempty"`
	Priority    int       `json:"priority"`
	ExpiresAt   time.Time `json:"expires_at,omitempty"`
	Modified    time.Time `json:"modified"`
}

Entry is the lightweight header view of a record file: just the parsed frontmatter plus stat metadata. body fetched separately via Body().

func ReadEntry

func ReadEntry(path string) (Entry, error)

ReadEntry stats and parses the frontmatter of one file into an Entry. missing or unparseable frontmatter degrades to a name-only record so the caller still sees the file in listings.

func ScanStore

func ScanStore(ctx context.Context, dir string) ([]Entry, error)

ScanStore walks dir for *.md files, parses each frontmatter in parallel, and returns entries sorted mtime desc, capped at MaxEntries. INDEX.md is excluded. a missing dir returns (nil, nil) so callers can fall through. repeat calls return a cached slice when the dir mtime is unchanged.

type Options

type Options struct {
	// HintTags are query-derived tags merged with the user-supplied ones.
	// populate from a side query when you want phase-2 hint extraction.
	HintTags []string
	// Now overrides the wall clock for testing. zero = time.Now().
	Now time.Time
	// Exclude paths that already surfaced this turn so the budget goes to
	// fresh candidates.
	Exclude map[string]bool
}

Options tunes query behavior. zero values pick sensible defaults.

type Record

type Record struct {
	Entry
	Body string
}

Record is the full file: header plus body.

func Query

func Query(ctx context.Context, dir, userQuery string, limit int, opts Options) ([]Record, error)

Query scores every entry in dir against the user's plain-text query and returns the top-N records (header + body). scoring is deterministic and purely structural — no LLM call is made. Callers wanting phase-2 hint extraction supply Options.HintTags from a side query.

returns (nil, nil) for missing or empty stores so callers can fall through without first stat'ing.

Jump to

Keyboard shortcuts

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