name: Publish npm packages

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Extract version from tag
        run: |
          echo "VERSION=$(echo $GITHUB_REF_NAME | sed 's/^v//')" >> $GITHUB_ENV

      - name: Configure npm auth
        run: |
          echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

      - name: Update package versions
        run: |
          cd packages/sdk-ts && npm version $VERSION --no-git-tag-version
          cd ../opencode-plugin && npm version $VERSION --no-git-tag-version

      - name: Publish agentlens-sdk
        run: |
          cd packages/sdk-ts
          npm install
          npm run build
          npm publish --access public

      - name: Publish opencode-agentlens
        run: |
          cd packages/opencode-plugin
          npm install
          npm run build
          npm publish --access public
