brain

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentHash

func ContentHash(data []byte) string

ContentHash returns the SHA-256 hex digest of data.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens gives a rough token count (~4 chars per token).

func InitLanguageMap

func InitLanguageMap(settingsPaths []string)

InitLanguageMap loads the extension→language mapping from the given settings.json paths (in load order, later entries override earlier by language Name). Must be called before LanguageForExt (typically at brain init or daemon startup). Safe to call multiple times; only the first call takes effect.

func InitLanguageMapFromConfigs

func InitLanguageMapFromConfigs(configs []lsp.LanguageConfig)

InitLanguageMapFromConfigs initializes the ext→language map directly from configs. Useful in tests where settings.json may not be available.

func LanguageForExt

func LanguageForExt(ext string) string

LanguageForExt returns the language name for a file extension (e.g. ".go" → "go"). Returns "" if the extension is not recognized. Uses the LSP pool if initialized, otherwise falls back to the pre-loaded ext map.

func ListMarkdownFiles

func ListMarkdownFiles(brainDir, subdir string) []string

ListMarkdownFiles lists all .md files in a subdirectory of the brain dir.

func LogError

func LogError(format string, args ...any)

func LogInfo

func LogInfo(format string, args ...any)

func LogWarn

func LogWarn(format string, args ...any)

func ParseDependencies

func ParseDependencies(root string) []string

ParseDependencies extracts external dependency names from common config files.

func ParseFile

func ParseFile(filePath string, root string, language string) ([]SymbolInfo, []CallInfo)

ParseFile extracts symbols from a single file via LSP. Returns symbols and nil calls.

func ReadMarkdown

func ReadMarkdown(brainDir, relativePath string) string

ReadMarkdown reads a markdown file from the brain directory. Returns "" if missing.

func RegisterBrainHandlers

func RegisterBrainHandlers(register func(string, func(map[string]any) (map[string]any, error)), cred config.Credential, ctx context.Context)

RegisterBrainHandlers registers brain.* command handlers with the daemon.

func ReloadLanguageMap added in v0.4.2

func ReloadLanguageMap(languagesPaths []string)

ReloadLanguageMap rebuilds the ext→language map from the given paths, unconditionally replacing any previously-loaded map. Unlike InitLanguageMap it is not one-shot — the daemon config watcher calls it when config/languages.json changes on disk.

func RunPhase2

func RunPhase2(ctx context.Context, cred config.Credential, root string, index *BrainIndex, brainDir string) error

RunPhase2 runs all Phase 2 semantic analysis.

func SymbolID

func SymbolID(filePath, name, kind string) string

SymbolID returns a stable 16-char hex identifier for a symbol.

func TruncateToTokens

func TruncateToTokens(text string, maxTokens int) string

TruncateToTokens truncates text to approximately maxTokens.

func WriteMarkdown

func WriteMarkdown(brainDir, relativePath, content string) error

WriteMarkdown writes a markdown file within the brain directory.

Types

type BrainIndex

type BrainIndex struct {
	Project  ProjectMeta  `json:"project"`
	Files    []FileInfo   `json:"files"`
	Symbols  []SymbolInfo `json:"symbols"`
	Imports  []ImportInfo `json:"imports"`
	Calls    []CallInfo   `json:"calls"`
	HubFiles []HubFile    `json:"hub_files"`
}

BrainIndex is the root model serialized to index.json.

type CallInfo

type CallInfo struct {
	CallerID   string `json:"caller_id"`
	CalleeName string `json:"callee_name"`
	FilePath   string `json:"file_path"`
}

CallInfo represents a function/method call.

type DeepDiveMeta

type DeepDiveMeta struct {
	Name        string
	Description string
}

DeepDiveMeta holds the frontmatter metadata from a deep dive file.

func ReadMarkdownFrontmatter

func ReadMarkdownFrontmatter(brainDir, relativePath string) (DeepDiveMeta, string)

ReadMarkdownFrontmatter reads a markdown file and parses YAML frontmatter. Returns the parsed meta and the body (everything after the closing ---). If no frontmatter is present, returns empty meta and the full content as body.

type FileInfo

type FileInfo struct {
	Path         string `json:"path"`
	Language     string `json:"language"`
	SizeBytes    int    `json:"size_bytes"`
	LineCount    int    `json:"line_count"`
	SHA256       string `json:"sha256"`
	IsEntryPoint bool   `json:"is_entry_point"`
	IsConfig     bool   `json:"is_config"`
	IsTest       bool   `json:"is_test"`
}

FileInfo holds metadata for a single project file.

func ScanProject

func ScanProject(root string) []FileInfo

ScanProject crawls the project directory and returns FileInfo for every source file.

func ScanSingleFile

func ScanSingleFile(root, relPath string) *FileInfo

ScanSingleFile scans a single file and returns its FileInfo, or nil if not valid.

type FrameworkResult

type FrameworkResult struct {
	Frameworks []string `json:"frameworks"`
	Patterns   []string `json:"patterns"`
	Testing    []string `json:"testing"`
	CICD       []string `json:"ci_cd"`
}

FrameworkResult holds detection results for frameworks, patterns, testing, and CI/CD.

func DetectFrameworks

func DetectFrameworks(root string, files []FileInfo, externalDeps []string) FrameworkResult

DetectFrameworks heuristically detects frameworks, patterns, testing, and CI/CD.

type HubFile

type HubFile struct {
	Path        string `json:"path"`
	ImportCount int    `json:"import_count"`
}

HubFile represents a highly-imported file.

func FindHubFiles

func FindHubFiles(imports []ImportInfo, topN int) []HubFile

FindHubFiles returns the top N most-imported internal files.

type ImportInfo

type ImportInfo struct {
	SourceFile string `json:"source_file"`
	TargetFile string `json:"target_file"`
	Module     string `json:"module"`
	IsExternal bool   `json:"is_external"`
}

ImportInfo represents an import edge in the dependency graph.

func BuildImportGraph

func BuildImportGraph(root string, files []FileInfo) []ImportInfo

BuildImportGraph extracts import statements and resolves internal targets via LSP.

func ExtractFileImports

func ExtractFileImports(source, filePath string, filePaths map[string]bool, root, language string) []ImportInfo

ExtractFileImports extracts imports from a single file's source.

type ProjectMeta

type ProjectMeta struct {
	Name              string         `json:"name"`
	RootPath          string         `json:"root_path"`
	TotalFiles        int            `json:"total_files"`
	TotalLines        int            `json:"total_lines"`
	Languages         map[string]int `json:"languages"`
	EntryPoints       []string       `json:"entry_points"`
	ConfigFiles       []string       `json:"config_files"`
	ExternalDeps      []string       `json:"external_deps"`
	Frameworks        []string       `json:"frameworks"`
	Patterns          []string       `json:"patterns"`
	TestingFrameworks []string       `json:"testing_frameworks"`
	CICD              []string       `json:"ci_cd"`
}

ProjectMeta holds project-level metadata.

type SymbolInfo

type SymbolInfo struct {
	ID         string   `json:"id"`
	Name       string   `json:"name"`
	Kind       string   `json:"kind"` // LSP SymbolKind name: "Function", "Method", "Class", "Struct", "Interface", etc.
	FilePath   string   `json:"file_path"`
	LineStart  int      `json:"line_start"`
	LineEnd    int      `json:"line_end"`
	Parameters []string `json:"parameters"`
	ReturnType string   `json:"return_type"`
	Decorators []string `json:"decorators"`
	Docstring  string   `json:"docstring"`
	Complexity int      `json:"complexity"`
	Parent     string   `json:"parent"`
}

SymbolInfo represents an extracted symbol (function, class, method).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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