# Katalyst Bard Justfile

# Default recipe - show available commands
default:
    @just --list

# ─────────────────────────────────────────────────────────────
# Node (the bard framework itself)
# ─────────────────────────────────────────────────────────────

# Install Node dependencies
install:
    npm install

# Build the SPA + bundle the server, then stage into the wheel package dir
build: install
    npm run build
    rm -rf src/katalyst_plugin_bard/web_dist
    mkdir -p src/katalyst_plugin_bard/web_dist
    cp -R web/dist/. src/katalyst_plugin_bard/web_dist/

# Build the Electron desktop shell and stage it beside the web assets
desktop-build: build
    npm run build:desktop
    mkdir -p src/katalyst_plugin_bard/web_dist/desktop
    cp apps/desktop/dist/main.cjs src/katalyst_plugin_bard/web_dist/desktop/
    # A minimal manifest, not a copy: the built bundle sits at the root of the
    # staged directory, so the source `main: dist/main.cjs` would not resolve.
    printf '{\n  "name": "katalyst-bard-desktop",\n  "version": "0.1.0",\n  "private": true,\n  "main": "main.cjs"\n}\n' > src/katalyst_plugin_bard/web_dist/desktop/package.json

# Open bard in a desktop window (requires desktop-build)
desktop: 
    uv run kata tax bard desktop

# Run the Node test suite
test-js:
    npm test

# Typecheck client and server
typecheck-js:
    npm run typecheck
    npm run typecheck -w @katalyst/bard-desktop

# Dev servers: Express on 5273, Vite on 5173 with HMR
dev:
    npm run dev

# Taxonomy API and bard together, writable, for first-run setup.
# An empty repo is fine: bard opens on Setup.  e.g. just dev-setup ./.dev-repo ~/venv/bin/python
dev-setup repo="./.dev-repo" python="":
    scripts/dev-setup.sh {{repo}} {{python}}

# ─────────────────────────────────────────────────────────────
# Python (the taxonomy plugin wrapper)
# ─────────────────────────────────────────────────────────────

# Build a contribution for Bard to load at runtime, e.g. just build-contribution ../forge/ui/design out/design
build-contribution source out:
    uv run kata tax bard build-contribution {{source}} --out {{out}}

# Run the Python test suite
test-py:
    uv run pytest

# Lint Python
lint:
    uv run ruff check src tests

# Check Python formatting (CI gates on this; `just format` fixes it)
format-check:
    uv run ruff format --check src tests

# Format Python
format:
    uv run ruff format src tests

# Typecheck Python
typecheck-py:
    uv run pyright src

# ─────────────────────────────────────────────────────────────
# Everything
# ─────────────────────────────────────────────────────────────

# Full validation, matching CI
check: lint format-check typecheck-py test-py typecheck-js test-js

# Drive Bard in a browser (xspec/): its own server and a fixture contribution on a
# throwaway taxonomy, never yours. Uses the built assets (`just build` first). Pass
# Playwright arguments through, e.g. --grep sidebar.
[positional-arguments]
xspec *args:
    cd xspec && npm ci --no-audit --no-fund --silent && npx playwright install chromium && npx bddgen && npx playwright test "$@"

# Serve bard locally (requires `just build` and a taxonomy checkout)
serve:
    uv run kata tax bard serve

# Build the container image
docker:
    docker build -t katalyst-bard:dev .

# Build the Python wheel (requires `just build` first for assets)
wheel: build
    uv build
