Skip to content

Add GitHub Action to automate the creation of LLMs.txt and LLMs-full.txt #8

Add GitHub Action to automate the creation of LLMs.txt and LLMs-full.txt

Add GitHub Action to automate the creation of LLMs.txt and LLMs-full.txt #8

Workflow file for this run

name: Update llms.txt and llms-full.txt in subdirectories
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
permissions:
contents: write
pull-requests: write
jobs:
auto-docs:
if: ${{ !startsWith(github.head_ref, 'docs/') }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Install Cursor CLI
run: |
curl https://cursor.com/install -fsS | bash
echo "$HOME/.cursor/bin" >> $GITHUB_PATH
- name: Configure git
run: |
git config user.name "Cursor Agent"
git config user.email "cursoragent@cursor.com"
- name: Detect changed subdirectories
id: detect-changes
run: |
changed_files=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- docs/)
changed_subdirs=""
for file in $changed_files; do
subdir=$(echo "$file" | sed -n 's|^docs/\([^/]*\)/.*|\1|p')
if [ -n "$subdir" ] && [ -f "docs/$subdir/llms.txt" ] && [ -f "docs/$subdir/llms-full.txt" ]; then
if [[ ! "$changed_subdirs" =~ (^|[[:space:]])"$subdir"($|[[:space:]]) ]]; then
changed_subdirs="$changed_subdirs $subdir"
fi
fi
done
changed_subdirs=$(echo "$changed_subdirs" | xargs)
echo "changed_subdirs=$changed_subdirs" >> $GITHUB_OUTPUT
echo "Found changed subdirectories: $changed_subdirs"
- name: Generate llms.txt updates (no commit/push/comment)
if: steps.detect-changes.outputs.changed_subdirs != ''
env:
MODEL: gpt-5
CURSOR_API_KEY: ${{ secrets.CURSOR_API_KEY }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CHANGED_SUBDIRS: ${{ steps.detect-changes.outputs.changed_subdirs }}
run: |
# Create a wrapper script to ensure proper completion
cat > update_llms.sh << 'EOF'
#!/bin/bash
set -euo pipefail
echo "=== Starting LLMs update process ==="
echo "Changed subdirectories: $CHANGED_SUBDIRS"
cursor-agent -p "You are operating in a GitHub Actions runner to update documentation summary files.
The GitHub CLI is available as \`gh\` and authenticated via \`GH_TOKEN\`. Git is available.
CRITICAL: Do NOT create branches, commit, push, or post PR comments. Only modify files in the working directory as needed. You MUST complete this task and exit cleanly.
# Context:
- Repo: ${{ github.repository }}
- PR Number: ${{ github.event.pull_request.number }}
- Base Ref: ${{ github.base_ref }}
- Head Ref: ${{ github.head_ref }}
- Changed Subdirectories: $CHANGED_SUBDIRS
# Task:
For each subdirectory in CHANGED_SUBDIRS, check if llms.txt and llms-full.txt need updates based on PR changes.
# Step-by-step process:
1. Get PR diff: \`gh pr diff ${{ github.event.pull_request.number }}\`
2. For each subdirectory in CHANGED_SUBDIRS:
- Read current docs/[subdirectory]/llms.txt
- Read current docs/[subdirectory]/llms-full.txt
- Analyze if the PR changes require updates to these files
- If yes: Update the files (maintain format/style)
- If no: Leave files unchanged
3. Print completion status and exit
# COMPLETION REQUIREMENTS:
- You MUST end with one of these exact phrases:
* \"TASK_COMPLETED_WITH_UPDATES\" (if you modified any files)
* \"TASK_COMPLETED_NO_UPDATES\" (if no files needed changes)
- Do NOT wait for additional input
- Do NOT create commits, branches, or comments
- Exit immediately after printing completion status
# File Guidelines:
- llms.txt = concise summary/index of subdirectory docs
- llms-full.txt = comprehensive guide with code examples
- Only update files that actually need changes based on PR diff
- Maintain existing format and style
Remember: Complete the analysis, make any necessary file updates, print completion status, and exit immediately.
" --force --model "$MODEL" --output-format=text
echo "=== Cursor agent completed ==="
EOF
chmod +x update_llms.sh
# Run with timeout and explicit completion handling
timeout 1800 ./update_llms.sh || {
exit_code=$?
if [ $exit_code -eq 124 ]; then
echo "=== Process timed out after 30 minutes ==="
else
echo "=== Process completed with exit code $exit_code ==="
fi
exit 0
}
echo "=== LLMs update process finished ==="
- name: Commit changes directly to PR branch
if: steps.detect-changes.outputs.changed_subdirs != ''
id: commit_changes
run: |
echo "changes_committed=false" >> "$GITHUB_OUTPUT"
# Stage all changes
git add -A
# Check if there are any changes to commit
if git diff --staged --quiet; then
echo "No llms.txt changes to commit. Skipping."
exit 0
fi
# Show what changed
echo "Changes detected:"
git diff --staged --name-only
git diff --staged
# Commit changes directly to the PR branch
COMMIT_MSG="docs: update llms summaries for PR #${{ github.event.pull_request.number }}"
git commit -m "$COMMIT_MSG"
git push origin ${{ github.head_ref }}
echo "changes_committed=true" >> "$GITHUB_OUTPUT"
- name: Comment on PR about updates
if: steps.commit_changes.outputs.changes_committed == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
changed_llms_files=$(git diff HEAD~1 --name-only | grep -E "llms(-full)?\.txt$" | head -10)
COMMENT_FILE="${RUNNER_TEMP}/llms-update-comment.md"
{
echo "🤖 **LLMs summaries updated**"
echo ""
echo "Updated the following documentation summary files based on your changes:"
echo ""
for file in $changed_llms_files; do
echo "- \`$file\`"
done
echo ""
echo "_This comment will be updated if you make more changes to the PR._"
echo ""
echo "<!-- auto-update-llms -->"
} > "$COMMENT_FILE"
if gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE" --edit-last; then
echo "Updated existing PR comment."
else
gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE"
echo "Posted new PR comment."
fi