context

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallersOf

func CallersOf(root, defFile, symbol string) ([]string, error)

CallersOf returns repository-relative .go files that reference symbol (defined in defFile) in actual code — not in comments or strings, and not the definition itself. A candidate qualifies when it is import-connected to the definition:

  • same package (same directory as defFile): any code reference to symbol;
  • other package: the file imports defFile's package AND uses a selector ending in symbol (package-qualified use or a method call on a value of the imported type).

git grep -lw is used as a fast prefilter; the AST check removes the false positives the old token-grep produced (comments/strings, same-named symbols in unrelated packages, the defining file). Empty symbol → empty result.

func CollectMemory

func CollectMemory(ctx context.Context, root string, cfg config.Config, query string) (memory.Recalled, error)

CollectMemory loads OpenClaw-style project memory: pinned MEMORY.md plus search hits relevant to query (typically the task title or description). Returns a zero Recalled if memory is disabled or unavailable; errors are non-fatal and reported separately for logging.

func ImportsOf

func ImportsOf(root, file string) ([]string, error)

ImportsOf returns repository-relative paths of files imported by the given file, restricted to the repository's own module. Stdlib and third-party imports are skipped because they live outside the repo. For non-Go files the function returns (nil, nil).

func PrimarySymbolOf

func PrimarySymbolOf(root, file string) string

PrimarySymbolOf returns a best-effort exported symbol from a Go file, used as the seed for CallersOf. Returns "" when no exported symbol exists.

func SetEmbedderFactory

SetEmbedderFactory swaps the embedder constructor used by Collect when the semantic index is enabled. Returns the previous factory so tests can restore it. Intended for tests only — production callers should leave it alone and rely on configuration.

func SetMemoryEmbedderFactory

func SetMemoryEmbedderFactory(f func(config.MemoryConfig) (embedder.Embedder, error)) func(config.MemoryConfig) (embedder.Embedder, error)

SetMemoryEmbedderFactory swaps the constructor used to build the embedder for memory recall. Test-only.

Types

type ContextProfile

type ContextProfile int

ContextProfile controls how much detail is included in the system prompt fragment.

const (
	// ProfileFull includes everything: files, commits, diffs, source code.
	// Used by PM, Planner, and Coder where full context is needed.
	ProfileFull ContextProfile = iota

	// ProfileCompact includes only project type and tree structure.
	// Used by review agents (reviewer, security, QA, UX) that receive
	// source code separately through their payload, saving tokens on cloud models.
	ProfileCompact
)

type ProjectContext

type ProjectContext struct {
	Files               []string          // git ls-files
	RecentCommits       []string          // git log --oneline -20
	UnstagedDiff        string            // git diff HEAD
	AlwaysInclude       map[string]string // filename → content
	SourceFiles         map[string]string // key source files → content (for brownfield)
	IsBrownfield        bool              // true if project has existing source code
	ProjectType         string            // detected project type (go, node, rust, etc.)
	TreeStructure       string            // directory tree summary
	ProgrammingLanguage string            // explicit language setting from project config

	// Memory holds OpenClaw-style recalled project memory. Populated by
	// CollectMemory; SystemPromptFragment renders it.
	Memory memory.Recalled
	// contains filtered or unexported fields
}

ProjectContext is a snapshot of the repository state passed to every agent.

func Collect

func Collect(root string, cfg config.Config) (ProjectContext, error)

Collect gathers repository context from root using git commands.

func (ProjectContext) SemanticSearch

func (p ProjectContext) SemanticSearch(ctx context.Context, query string, k int) ([]string, error)

SemanticSearch returns deduplicated repository-relative file paths for the top-K chunk hits matching query. When the index is disabled or unavailable, it returns (nil, nil).

func (ProjectContext) SemanticSearchSymbols

func (p ProjectContext) SemanticSearchSymbols(ctx context.Context, query string, k int) (map[string][]string, error)

SemanticSearchSymbols returns the top-K chunk hits grouped as a file → declaration-name map, suitable as symbol targets for scoped source rendering. Whole-file chunks (empty name) are skipped because they carry no symbol to scope to. Names are deduplicated per file while preserving score order. Returns (nil, nil) when the index is disabled or unavailable.

func (ProjectContext) SystemPromptFragment

func (p ProjectContext) SystemPromptFragment(profile ...ContextProfile) string

SystemPromptFragment formats the project context as a block to inject into system prompts. Use ProfileFull for agents that need complete context (PM, planner, coder). Use ProfileCompact for review agents that receive source code separately.

type SourceEntry

type SourceEntry struct {
	Content   string
	Truncated bool
}

SourceEntry holds a source file's content as injected into the context. Truncated indicates the body was reduced from the original by chunk-level selection (whole functions/types) rather than included verbatim.

Jump to

Keyboard shortcuts

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