name: "Cursor Action"
description: "Run cursor-agent in CI pipelines using the official @cursor/sdk"
author: "PunGrumpy"

branding:
  icon: "terminal"
  color: "blue"

inputs:
  cursor-version:
    description: "(Deprecated) The SDK automatically manages the agent version."
    required: false
    default: "latest"

  api-key:
    description: "Cursor API key for authentication"
    required: true

  prompt:
    description: "Prompt to pass to cursor-agent"
    required: true

  model:
    description: "Model id for the agent (e.g. default, composer-2). The Cursor SDK does not accept auto."
    required: false
    default: "default"

  working-directory:
    description: "Working directory for the agent to operate in"
    required: false
    default: "."

  permissions:
    description: "Validated but NOT enforced: 'read-only' does not stop the agent from editing files or running shell commands. Wired to the SDK in v2."
    required: false
    default: "read-only"

  timeout:
    description: "Timeout in seconds for the agent run"
    required: false
    default: "300"

outputs:
  summary:
    description: "Text summary returned by the cursor-agent"
    value: ${{ steps.agent.outputs.summary }}

  exit-code:
    description: "Exit code from the cursor-agent process"
    value: ${{ steps.agent.outputs.exit-code }}

  status:
    description: "Run status returned by the cursor-agent (finished, error, or cancelled)"
    value: ${{ steps.agent.outputs.status }}

  duration-ms:
    description: "Execution duration in milliseconds"
    value: ${{ steps.agent.outputs.duration-ms }}

  total-tokens:
    description: "Total tokens consumed by the agent run"
    value: ${{ steps.agent.outputs.total-tokens }}

  input-tokens:
    description: "Input tokens consumed by the agent run"
    value: ${{ steps.agent.outputs.input-tokens }}

  output-tokens:
    description: "Output tokens generated by the agent run"
    value: ${{ steps.agent.outputs.output-tokens }}

# @cursor/sdk cannot be bundled: it dynamically imports its own webpack chunks
# at runtime and resolves a native @cursor/sdk-<platform> package. It has to be
# installed as a real dependency tree, which a `node24` action cannot do.
runs:
  using: "composite"
  steps:
    - name: Setup Node.js
      uses: actions/setup-node@v6
      with:
        node-version: "24"
    - name: Install action dependencies
      shell: bash
      run: |
        set -euo pipefail
        cd "$GITHUB_ACTION_PATH"
        npm ci --omit=dev --no-audit --no-fund
    - name: Run Cursor Agent
      id: agent
      shell: bash
      run: node "$GITHUB_ACTION_PATH/dist/index.mjs"
      env:
        "INPUT_API-KEY": ${{ inputs['api-key'] }}
        "INPUT_CURSOR-VERSION": ${{ inputs['cursor-version'] }}
        INPUT_MODEL: ${{ inputs.model }}
        INPUT_PERMISSIONS: ${{ inputs.permissions }}
        INPUT_PROMPT: ${{ inputs.prompt }}
        INPUT_TIMEOUT: ${{ inputs.timeout }}
        "INPUT_WORKING-DIRECTORY": ${{ inputs['working-directory'] }}
