mnemo_cards/tools/agent/doc_updater.py

111 lines
4 KiB
Python
Raw Normal View History

2025-11-21 11:13:03 +00:00
"""
Script to update documentation for a component using AI agent.
"""
import sys
import argparse
import subprocess
from pathlib import Path
from config import AgentConfig
from cursor_cli_wrapper import CursorCLI, CursorResultStatus
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(description="Update documentation for a component")
parser.add_argument("component", choices=["web_v2", "backend", "common"], help="Component to document")
parser.add_argument("--model", default="claude-3-5-sonnet-20241022", help="LLM model to use")
args = parser.parse_args()
# Load configuration
try:
config = AgentConfig.from_env(args.component)
# Override model if provided in args (though config loads from env, we want to support CLI override if needed,
# but here we primarily rely on env vars set by workflow, so we'll stick to config or args)
# Actually, AgentConfig.from_env reads CURSOR_MODEL.
# If we pass it via args, we should update it.
if args.model:
config.cursor_model = args.model
except ValueError as e:
print(f"❌ Configuration error: {e}")
sys.exit(1)
print(f"🚀 Starting Documentation Update for: {args.component}")
print(f"🤖 Model: {config.cursor_model}")
# Initialize Cursor CLI
cursor_cli = CursorCLI(
project_root=config.project_root,
api_key=config.cursor_api_key,
model=config.cursor_model,
verbose=True
)
# Define the task
task_description = f"""
You are an expert technical writer and software engineer.
Your task is to update the documentation for the '{args.component}' component.
1. Analyze the current codebase in '{config.component_root}'.
2. Check existing documentation in 'ai_docs/{args.component}' and 'README.md' files.
3. Identify missing or outdated documentation.
4. Update or create documentation files to reflect the current state of the code.
- Focus on architecture, API endpoints, setup instructions, and key features.
- Ensure 'README.md' in the component root is up to date.
- If there are significant changes, update 'ai_docs/{args.component}/architecture.md' or similar.
Do NOT modify any code files. ONLY modify markdown documentation files.
"""
print("\n📝 Task Description:")
print(task_description)
print(f"\n{'='*80}\n")
# Run agent
result = cursor_cli.run_agent(
task_description=task_description,
force=True,
max_iterations=5
)
if result.status != CursorResultStatus.SUCCESS:
print(f"\n❌ Documentation update failed: {result.error}")
sys.exit(1)
print(f"\n✅ Agent completed successfully")
print(f" - Files created: {len(result.files_created)}")
print(f" - Files modified: {len(result.files_modified)}")
if not result.files_created and not result.files_modified:
print("\n⚠️ No changes made to documentation.")
sys.exit(0)
# Commit changes
print("\n💾 Committing changes...")
# Git config
subprocess.run(["git", "config", "--global", "user.name", "AI Agent"], check=False)
subprocess.run(["git", "config", "--global", "user.email", "ai-agent@mnemo-cards.com"], check=False)
# Add changes
subprocess.run(["git", "add", "."], cwd=config.project_root, check=True)
# Commit
commit_msg = f"docs({args.component}): Update documentation via AI Agent\n\nModel: {config.cursor_model}"
subprocess.run(["git", "commit", "-m", commit_msg], cwd=config.project_root, check=False)
# Push
print("\n📤 Pushing changes...")
# Pull first
subprocess.run(["git", "pull", "--rebase"], cwd=config.project_root, check=False)
push_result = subprocess.run(["git", "push"], cwd=config.project_root, check=False)
if push_result.returncode == 0:
print("✅ Changes pushed successfully")
else:
print("❌ Failed to push changes")
sys.exit(1)
if __name__ == "__main__":
main()