class Index
Represents an index for managing and generating agent.md files from context files.
This class provides functionality to update or create AGENT.md files following the AGENT.md specification for agentic coding tools. It can parse existing agent.md files, update the context section, and generate new files when needed.
Definitions
def initialize(context_path = ".context")
Initialize a new index instance.
Signature
-
parameter
context_path
String
The path to the context directory (default: ".context").
Implementation
def initialize(context_path = ".context")
@context_path = context_path
end
def update_agent_md(agent_md_path = "agent.md")
Update or create an AGENT.md file in the project root with context section This follows the AGENT.md specification for agentic coding tools
Implementation
def update_agent_md(agent_md_path = "agent.md")
context_content = generate_context_section
if File.exist?(agent_md_path)
update_existing_agent_md(agent_md_path, context_content)
else
create_new_agent_md(agent_md_path, context_content)
end
Console.debug("Updated agent.md: #{agent_md_path}")
end
def generate_context_section
Generate just the context section content (without top-level headers)
Implementation
def generate_context_section
sections = []
sections << "This section provides links to documentation from installed packages. It is automatically generated and may be updated by running `bake agent:context:install`."
sections << ""
sections << "**Important:** Before performing any code, documentation, or analysis tasks, always read and apply the full content of any relevant documentation referenced in the following sections. These context files contain authoritative standards and best practices for documentation, code style, and project-specific workflows. **Do not proceed with any actions until you have read and incorporated the guidance from relevant context files.**"
sections << ""
sections << "**Setup Instructions:** If the referenced files are not present or if dependencies have been updated, run `bake agent:context:install` to install the latest context files."
sections << ""
gem_contexts = collect_gem_contexts
if gem_contexts.empty?
sections << "No context files found. Run `bake agent:context:install` to install context from gems."
sections << ""
else
gem_contexts.each do |gem_name, files|
sections << "### #{gem_name}"
sections << ""
# Get gem directory and load index
gem_directory = File.join(@context_path, gem_name)
index = load_gem_index(gem_name, gem_directory)
# Add gem description from index
if index["description"]
sections << index["description"]
sections << ""
end
# Use files from index if available, otherwise fall back to parsing
if index["files"] && !index["files"].empty?
index["files"].each do |file_info|
sections << "#### [#{file_info['title']}](.context/#{gem_name}/#{file_info['path']})"
sections << ""
sections << file_info["description"] if file_info["description"] && !file_info["description"].empty?
sections << ""
end
else
# Fallback to parsing files directly
files.each do |file_path|
if File.exist?(file_path)
title, description = extract_content(file_path)
relative_path = file_path.sub("#{@context_path}/", "")
sections << "#### [#{title}](.context/#{relative_path})"
sections << ""
sections << description if description && !description.empty?
sections << ""
end
end
end
end
end
sections.join("\n")
end
def load_gem_index(gem_name, gem_directory)
Load a gem's index file
Implementation
def load_gem_index(gem_name, gem_directory)
index_path = File.join(gem_directory, "index.yaml")
if File.exist?(index_path)
YAML.load_file(index_path)
else
# Return a fallback index if no index.yaml exists
{
"description" => "Context files for #{gem_name}",
"files" => []
}
end
rescue => error
Console.debug("Error loading index for #{gem_name}: #{error.message}")
# Return a fallback index
{
"description" => "Context files for #{gem_name}",
"files" => []
}
end