| 1 | name: Catalog refresh |
| 2 | |
| 3 | on: |
| 4 | schedule: |
| 5 | - cron: '17 5 * * *' |
| 6 | workflow_dispatch: |
| 7 | |
| 8 | permissions: {} |
| 9 | |
| 10 | concurrency: |
| 11 | group: catalog-refresh |
| 12 | cancel-in-progress: false |
| 13 | |
| 14 | jobs: |
| 15 | refresh: |
| 16 | name: Refresh the model catalog |
| 17 | if: github.repository == 'codewhale-hq/Codewhale' |
| 18 | runs-on: ubuntu-24.04 |
| 19 | timeout-minutes: 20 |
| 20 | permissions: |
| 21 | contents: read |
| 22 | env: |
| 23 | BRANCH: catalog-refresh |
| 24 | steps: |
| 25 | - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 |
| 26 | id: app-token |
| 27 | with: |
| 28 | app-id: ${{ vars.CODEWHALE_APP_ID }} |
| 29 | private-key: ${{ secrets.CODEWHALE_APP_PRIVATE_KEY }} |
| 30 | permission-contents: write |
| 31 | permission-pull-requests: write |
| 32 | - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7 |
| 33 | with: |
| 34 | ref: main |
| 35 | fetch-depth: 0 |
| 36 | token: ${{ steps.app-token.outputs.token }} |
| 37 | persist-credentials: false |
| 38 | - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 |
| 39 | with: |
| 40 | node-version: 22 |
| 41 | - name: Fetch models.dev once |
| 42 | run: | |
| 43 | curl --fail --silent --show-error --location --max-time 60 \ |
| 44 | --user-agent "CodeWhale-catalog-automation (+https://github.com/codewhale-hq/Codewhale)" \ |
| 45 | --output "$RUNNER_TEMP/models-dev-catalog.json" \ |
| 46 | https://models.dev/catalog.json |
| 47 | - name: Decide whether the pin is due |
| 48 | id: pin |
| 49 | run: | |
| 50 | if python3 scripts/catalog/catalog_models_dev.py seed age --max-days 7; then |
| 51 | echo "due=false" >> "$GITHUB_OUTPUT" |
| 52 | else |
| 53 | echo "due=true" >> "$GITHUB_OUTPUT" |
| 54 | fi |
| 55 | - name: Propose new models, re-lock and render |
| 56 | env: |
| 57 | CODEWHALE_MODELS_DEV_PATH: ${{ runner.temp }}/models-dev-catalog.json |
| 58 | run: | |
| 59 | set -euo pipefail |
| 60 | python3 scripts/catalog/catalog_models_dev.py seed discover --write --snapshot "$CODEWHALE_MODELS_DEV_PATH" | tee "$RUNNER_TEMP/discover.txt" |
| 61 | python3 scripts/catalog/catalog_models_dev.py seed lock > "$RUNNER_TEMP/lock-report.txt" |
| 62 | python3 scripts/catalog/catalog_models_dev.py seed render |
| 63 | (cd web && node scripts/derive-facts.mjs) |
| 64 | - name: Decide whether anything material changed |
| 65 | id: change |
| 66 | run: | |
| 67 | material=$(git diff -U0 -- \ |
| 68 | crates/config/assets/models_dev.bundled.json \ |
| 69 | crates/config/assets/catalog_corrections.json \ |
| 70 | scripts/catalog/models_dev_seed.toml \ |
| 71 | | grep -E '^[+-]' | grep -vE '^(\+\+\+|---)' | grep -v '"upstream":' || true) |
| 72 | if [ -n "$material" ] || [ "${{ steps.pin.outputs.due }}" = "true" ]; then |
| 73 | echo "open=true" >> "$GITHUB_OUTPUT" |
| 74 | else |
| 75 | echo "open=false" >> "$GITHUB_OUTPUT" |
| 76 | echo "No catalog change and the pin is under 7 days old; nothing to propose." |
| 77 | fi |
| 78 | - name: Run the catalog checks |
| 79 | id: checks |
| 80 | if: steps.change.outputs.open == 'true' |
| 81 | run: | |
| 82 | status=0 |
| 83 | { |
| 84 | python3 scripts/catalog/catalog_models_dev.py seed render --check || status=1 |
| 85 | python3 scripts/catalog/catalog_models_dev.py seed age --max-days 30 || status=1 |
| 86 | python3 -m unittest scripts/catalog/catalog_models_dev_test.py || status=1 |
| 87 | (cd web && node scripts/check-facts.mjs) || status=1 |
| 88 | } > "$RUNNER_TEMP/checks.txt" 2>&1 |
| 89 | if [ "$status" -eq 0 ]; then echo "ok=true" >> "$GITHUB_OUTPUT"; else echo "ok=false" >> "$GITHUB_OUTPUT"; fi |
| 90 | tail -n 40 "$RUNNER_TEMP/checks.txt" |
| 91 | - name: Push the branch and open or update the pull request |
| 92 | if: steps.change.outputs.open == 'true' |
| 93 | env: |
| 94 | GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 95 | CHECKS_OK: ${{ steps.checks.outputs.ok }} |
| 96 | APP_ID: ${{ vars.CODEWHALE_APP_ID }} |
| 97 | run: | |
| 98 | bot_email="${APP_ID}+codewhale-agent[bot]@users.noreply.github.com" |
| 99 | git config user.name 'codewhale-agent[bot]' |
| 100 | git config user.email "$bot_email" |
| 101 | git checkout -B "$BRANCH" |
| 102 | git add crates/config/assets/models_dev.bundled.json crates/config/assets/catalog_corrections.json \ |
| 103 | scripts/catalog/models_dev_seed.toml scripts/catalog/models_dev_seed.lock.json web/lib/facts.generated.ts |
| 104 | git commit -m "chore(catalog): refresh the Models.dev offline seed" \ |
| 105 | -m "Generated by .github/workflows/catalog-refresh.yml from models.dev. No LLM wrote these rows." |
| 106 | remote_url="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 107 | ls_remote=$(git ls-remote "$remote_url" "refs/heads/$BRANCH") |
| 108 | expected=$(printf '%s\n' "$ls_remote" | cut -f1) |
| 109 | if [ -n "$expected" ]; then |
| 110 | git fetch -q --no-tags "$remote_url" "$expected" |
| 111 | committers=$(git log --format=%ce "origin/main..$expected") |
| 112 | if [ -n "$committers" ] && printf '%s\n' "$committers" | grep -qvxF "$bot_email"; then |
| 113 | echo "::error::$BRANCH carries commits this workflow did not make; merge or close the PR first." |
| 114 | exit 1 |
| 115 | fi |
| 116 | fi |
| 117 | git push --force-with-lease="refs/heads/$BRANCH:$expected" "$remote_url" "HEAD:refs/heads/$BRANCH" |
| 118 | { |
| 119 | echo "Generated by the scheduled catalog refresh from models.dev. No LLM wrote these rows; review them like any other change." |
| 120 | echo |
| 121 | echo "New models from the vendor allowlist. Merging this PR publishes them on the public /models page; rows carry an \`unreviewed\` source until someone checks them against the vendor's docs:" |
| 122 | echo |
| 123 | echo '```text' |
| 124 | cat "$RUNNER_TEMP/discover.txt" |
| 125 | echo '```' |
| 126 | echo |
| 127 | echo "Checks run in the job: \`seed render --check\`, \`seed age --max-days 30\`, \`scripts/catalog/catalog_models_dev_test.py\`, \`web/scripts/check-facts.mjs\`: $([ "$CHECKS_OK" = true ] && echo passed || echo FAILED, see the job log; this PR is a draft)." |
| 128 | echo |
| 129 | echo "To stop proposing a model, add its id to \`[discover].skip\` in \`scripts/catalog/models_dev_seed.toml\`." |
| 130 | } > "$RUNNER_TEMP/pr-body.md" |
| 131 | title="chore(catalog): refresh the Models.dev offline seed" |
| 132 | existing=$(gh pr list --head "$BRANCH" --state open --json number --jq '.[0].number // empty') |
| 133 | if [ -n "$existing" ]; then |
| 134 | gh pr edit "$existing" --title "$title" --body-file "$RUNNER_TEMP/pr-body.md" |
| 135 | if [ "$CHECKS_OK" = true ]; then |
| 136 | gh pr ready "$existing" |
| 137 | else |
| 138 | gh pr ready --undo "$existing" |
| 139 | fi |
| 140 | else |
| 141 | draft="" |
| 142 | [ "$CHECKS_OK" = true ] || draft="--draft" |
| 143 | gh pr create --base main --head "$BRANCH" --title "$title" --body-file "$RUNNER_TEMP/pr-body.md" $draft |
| 144 | fi |
| 145 |