name: Claude Code Auto-Triage Issues

on:
  issues:
    types: [ opened ]

jobs:
  claude-issue-triage:
    # Optional: Skip for bot users and direct mentions in the body (handled by claude-dm.yml)
    if: |
      github.event.issue.user.type != 'Bot' &&
      !contains(github.event.issue.body, '@claude')

    runs-on: ubuntu-latest
    timeout-minutes: 30

    permissions:
      contents: read
      issues: write
      pull-requests: read # was write, but we're not altering PRs here
      actions: read
      id-token: write # required to use OIDC to authenticate to Claude Code API

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0 # 1 -> 0: full history helps with git blame, etc.

      - name: Analyze issue and provide help
        uses: anthropics/claude-code-action@v1
        with:
          claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
          # Security: Allow any user to trigger triage (automated issue help is safe)
          github_token: ${{ secrets.GITHUB_TOKEN }}
          allowed_non_write_users: '*'
          # track_progress: true # Enables tracking comments
          show_full_output: ${{ github.event.repository.private }} # security: do not log verbosely in private repo

          # This is an optional setting that allows Claude to read CI results on PRs
          additional_permissions: |
            actions: read

          prompt: |
            REPO: ${{ github.repository }}
            ISSUE NUMBER: #${{ github.event.issue.number }}

            A user has reported an issue. Please help them by:

            1. Deep think about the issue:
               **Understand the problem**: Analyze the issue description and any error messages
               **Search for context**:
               - Use the repository's CLAUDE.md for high level guidance and especially kb/ documentation
               - Look in relevant code files, including kb/ documentation
               **Use web search**: When potentially outside Big-AGI (e.g. user configuration), search the web for similar errors or related issues
               **Provide a solution**:
               - Provide multiple solutions if uncertain, and say so
               - Analyze the code and suggest specific fixes with code examples
               - If possible also suggest fixes or workarounds for immediate relief
               - Reference specific files and line numbers
               - Suggest workarounds for immediate relief if applicable
               - Use web search to find similar issues and solutions
               - Test selectively and even npm install and run build if needed to verify the solution
            2. Always add the 'claude-triage' issue label to indicate this issue was triaged by Claude
            3. Comment with:
               - Very brief thank you note, if applicable
               - Initial assessment
               - Next steps or clarification needed
               - Link duplicates if found
            
            Remember: design values for this codebase: orthogonal features, features that generalize well, modularized and reusable code,
            type-discriminated data, optimized code, zero maintenance burden. Minimize future pain, etc.

            IMPORTANT: You are in READ-ONLY triage mode. Analyze and suggest solutions in your comment, but do NOT attempt to push code changes.
            If you're uncertain, say so and suggest next steps.
            Be welcoming, helpful, professional, solution-focused and no-BS.

          # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
          # or https://docs.claude.com/en/docs/claude-code/cli-reference for available options
          claude_args: |
            --model claude-opus-4-6
            --max-turns 75
            --allowedTools "Edit,Read,Write,WebFetch,WebSearch,Bash(cat:*),Bash(cp:*),Bash(find:*),Bash(git branch:*),Bash(grep:*),Bash(ls:*),Bash(mkdir:*),Bash(npm run:*),Bash(gh issue:*),Bash(gh search:*),Bash(gh label:*),Bash(gh pr:*),SlashCommand"
