name: Opencode Agents

on:
  workflow_dispatch:
    inputs:
      target:
        description: Run issue triage or duplicate PR check
        required: true
        type: choice
        options:
          - issue
          - pull_request
      number:
        description: Issue or pull request number
        required: true
        type: number

jobs:
  triage-issue:
    if: inputs.target == 'issue'
    runs-on: blacksmith-4vcpu-ubuntu-2404
    permissions:
      contents: read
      issues: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 1

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Install opencode
        run: |
          version="$(node -e "const fs=require('fs'); const parsed=JSON.parse(fs.readFileSync('constants.json','utf8')); process.stdout.write(String(parsed.opencodeVersion||'').trim().replace(/^v/,''));")"
          curl -fsSL https://opencode.ai/install | bash -s -- --version "$version" --no-modify-path

      - name: Triage issue
        env:
          OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ISSUE_NUMBER: ${{ inputs.number }}
        run: |
          cat > /tmp/issue_prompt.txt <<'PROMPT_EOF'
          The following issue was just opened, triage it:
          PROMPT_EOF
          ISSUE_TITLE="$(gh issue view "$ISSUE_NUMBER" --json title --jq .title)"
          ISSUE_BODY="$(gh issue view "$ISSUE_NUMBER" --json body --jq .body)"
          printf '\nTitle: %s\n\n%s\n' "$ISSUE_TITLE" "$ISSUE_BODY" >> /tmp/issue_prompt.txt
          opencode run --agent triage "$(cat /tmp/issue_prompt.txt)"

  duplicate-prs:
    if: inputs.target == 'pull_request'
    runs-on: blacksmith-4vcpu-ubuntu-2404
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 1

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Install opencode
        run: |
          version="$(node -e "const fs=require('fs'); const parsed=JSON.parse(fs.readFileSync('constants.json','utf8')); process.stdout.write(String(parsed.opencodeVersion||'').trim().replace(/^v/,''));")"
          curl -fsSL https://opencode.ai/install | bash -s -- --version "$version" --no-modify-path

      - name: Build prompt
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ inputs.number }}
        run: |
          {
            echo "Check for duplicate PRs related to this new PR:"
            echo ""
            echo "CURRENT_PR_NUMBER: $PR_NUMBER"
            echo ""
            echo "Title: $(gh pr view \"$PR_NUMBER\" --json title --jq .title)"
            echo ""
            echo "Description:"
            gh pr view "$PR_NUMBER" --json body --jq .body
          } > pr_info.txt

      - name: Check for duplicate PRs
        env:
          OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PR_NUMBER: ${{ inputs.number }}
        run: |
          opencode run --agent duplicate-pr "$(cat pr_info.txt)" > /tmp/comment_output.txt

          {
            echo "_The following comment was made by an LLM, it may be inaccurate:_"
            echo ""
            cat /tmp/comment_output.txt
          } > /tmp/comment_body.txt
          gh pr comment "$PR_NUMBER" --body-file /tmp/comment_body.txt
