grimoire

package
v1.8.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package grimoire provides .grimoire project context file handling.

Index

Constants

View Source
const (
	PriorityGlobal   = 10 // ~/.celeste/grimoire.md
	PriorityProject  = 20 // /repo-root/.grimoire
	PriorityFragment = 30 // /repo-root/.celeste/grimoire/*.md
	PriorityLocal    = 40 // /repo-root/.grimoire.local
)

Priority levels for grimoire sources (lowest to highest).

View Source
const MaxIncludeDepth = 3

MaxIncludeDepth is the maximum nesting depth for @include resolution.

View Source
const MaxSize = 25 * 1024 // 25KB

MaxSize is the maximum total grimoire context size in bytes.

Variables

This section is empty.

Functions

func GenerateTemplate

func GenerateTemplate(info *ProjectInfo, dir string) string

GenerateTemplate creates a starter .grimoire file from project info.

func GrimoireMeta added in v1.8.3

func GrimoireMeta(dir string) string

GrimoireMeta returns a metadata header block for the grimoire file. Includes timestamp, git hash, and branch so staleness can be detected.

func Init

func Init(dir string) (string, error)

Init creates a .grimoire file in the given directory. Returns the path to the created file, or error if one already exists.

func ResolveIncludes

func ResolveIncludes(g *Grimoire, baseDir string) error

ResolveIncludes expands @./path and @~/path references in the Incantations section. It never returns an error for individual file failures; instead, it records errors on the individual IncludeRef entries.

Types

type GitSnapshot

type GitSnapshot struct {
	Branch        string
	MainBranch    string
	Status        string // short status output
	RecentCommits string // last 5 commits oneline
	UserName      string
}

GitSnapshot captures the current git state for system prompt injection.

func CaptureGitSnapshot

func CaptureGitSnapshot(workDir string) *GitSnapshot

CaptureGitSnapshot runs 5 git commands in parallel and returns the snapshot. Returns nil if not in a git repo. Truncates status to 2KB max.

func (*GitSnapshot) FormatForPrompt

func (s *GitSnapshot) FormatForPrompt() string

FormatForPrompt formats the snapshot for system prompt injection.

type Grimoire

type Grimoire struct {
	Sources      []string          // file paths that contributed (ordered by priority)
	Bindings     []string          // project facts (language, structure, commands)
	Rituals      []string          // behavioral rules (always/never do)
	Incantations []IncludeRef      // @path includes with resolved content
	Wards        []string          // protected areas
	Hooks        []HookEntry       // pre/post tool execution commands
	RawSections  map[string]string // unparsed section content by heading
	Meta         GrimoireMetadata  // embedded metadata (last updated, git hash, etc.)
}

Grimoire represents a fully resolved project context.

func LoadAll

func LoadAll(startDir string) (*Grimoire, error)

LoadAll discovers, parses, and merges all grimoire sources for the given directory.

func Merge

func Merge(grimoires ...*Grimoire) *Grimoire

Merge combines multiple grimoires into one, preserving order. Later grimoires take precedence for RawSections keys.

func Parse

func Parse(content string, baseDir string) (*Grimoire, error)

Parse parses a .grimoire markdown file into a Grimoire struct. It splits on ## headings and maps content to known section types.

func (*Grimoire) IsEmpty

func (g *Grimoire) IsEmpty() bool

IsEmpty returns true if the grimoire has no content.

func (*Grimoire) Render

func (g *Grimoire) Render() string

Render produces the grimoire content for injection into a system prompt.

func (*Grimoire) StalenessInfo added in v1.8.3

func (g *Grimoire) StalenessInfo(currentDir string) string

StalenessInfo returns how stale the grimoire is relative to current git state. Returns a human-readable message, or "" if metadata is unavailable.

func (*Grimoire) TotalSize

func (g *Grimoire) TotalSize() int

TotalSize returns the total byte size of the rendered grimoire.

type GrimoireMetadata added in v1.8.3

type GrimoireMetadata struct {
	LastUpdated    time.Time
	GitHash        string
	GitBranch      string
	GitCommitCount int
}

GrimoireMetadata holds the embedded metadata from the grimoire file.

type GrimoireSource

type GrimoireSource struct {
	Path     string
	Priority int
}

GrimoireSource represents a discovered .grimoire file with its priority.

func Discover

func Discover(startDir string) ([]GrimoireSource, error)

Discover walks upward from startDir to the filesystem root, collecting all .grimoire sources ordered by priority (lowest first).

type HookEntry

type HookEntry struct {
	Phase    string // "PreToolUse" or "PostToolUse"
	ToolName string // tool name pattern (e.g., "bash", "write_file")
	Command  string // shell command to execute
}

HookEntry represents a pre/post tool execution hook.

type IncludeRef

type IncludeRef struct {
	Path     string // original @path from grimoire
	Resolved string // absolute path after resolution
	Content  string // file content (empty if unresolved)
	Error    string // non-empty if resolution failed
}

IncludeRef represents an @include directive with its resolved content.

type ProjectInfo

type ProjectInfo struct {
	Language     string
	ModulePath   string
	TestCommand  string
	LintCommand  string
	BuildCommand string
	EntryPoint   string
	Framework    string
}

ProjectInfo holds detected project metadata.

func DetectProject

func DetectProject(dir string) (*ProjectInfo, error)

DetectProject inspects the given directory and returns project information.

Jump to

Keyboard shortcuts

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