# Plugin Scaffolding & TypeScript Types

> Create a typed, tested, zero-build PraisonAIUI plugin package in one command with aiui plugins init, and keep your editor's @aiui/sdk declarations in sync with

Create a typed, tested, zero-build PraisonAIUI plugin package in one command with `aiui plugins init`, and keep your editor's `@aiui/sdk` declarations in sync with `aiui plugins types`.

See [UI Plugins](https://ui.praison.ai/docs/features/plugins.md) for the overall architecture and entry-point contract.

## Scaffold a new plugin (`aiui plugins init`)

```bash
aiui plugins init sales-ops --title "Sales Ops"
cd aiui-sales-ops
pip install -e ".[dev]"
aiui plugins doctor
pytest
```

`aiui plugins init` validates the entry-point slug (`[a-z0-9][a-z0-9_-]*`, rejecting path separators), refuses to overwrite non-empty folders unless `--force` is passed, and creates a complete, immediately runnable package:

```text
aiui-sales-ops/
├── pyproject.toml                 # hatchling + [project.entry-points."praisonaiui.plugins"]
├── aiui_sales_ops/
│   ├── __init__.py                # AiuiPlugin with pages(), routes(), static_dir(), lifespan()
│   ├── py.typed                   # PEP 561 marker for mypy / pyright
│   └── static/
│       └── index.js               # Zero-build React ESM page using @aiui/sdk <BlockTree> + useLiveData
├── types/
│   └── aiui-sdk.d.ts              # Exact @aiui/sdk declarations copied from the installed aiui wheel
├── jsconfig.json                  # Wires types/aiui-sdk.d.ts for VS Code / TypeScript checkJs
├── tests/
│   └── test_aiui_sales_ops.py     # Passing contract test using praisonaiui.testing.plugin_test_client
└── README.md
```

### CLI options

| Option | Default | Description |
|---|---|---|
| `NAME` | required | Entry-point slug (e.g. `sales-ops` or `aiui-code`) |
| `--dir, -d` | current directory | Parent folder where `aiui-<name>/` is created |
| `--title, -t` | derived from `NAME` | Human-readable title for the page and README |
| `--force, -f` | off | Overwrite files if the target directory is non-empty |

## Export `@aiui/sdk` types (`aiui plugins types`)

`@aiui/sdk` is provided at runtime by the shell's import map (`/_aiui/externals/@aiui-sdk.js`), so there is no separate npm package to install. Every `aiui` wheel bundles the exact matching `aiui-sdk.d.ts`.

After upgrading `aiui`, refresh your plugin's declarations in one command:

```bash
# Write to a file or directory
aiui plugins types --out types/aiui-sdk.d.ts --force

# Or inspect / pipe via stdout
aiui plugins types --stdout | head -n 40
```

| Option | Default | Description |
|---|---|---|
| `--out, -o` | `./aiui-sdk.d.ts` | Target file or directory (writes `<dir>/aiui-sdk.d.ts` when given a directory) |
| `--stdout` | off | Print `aiui-sdk.d.ts` to stdout instead of writing a file |
| `--force, -f` | off | Overwrite an existing file whose contents differ |

### Editor and TypeScript setup

For **zero-build ESM** plugins, include `types/aiui-sdk.d.ts` in `jsconfig.json`:

```json
{
  "compilerOptions": {
    "checkJs": true,
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler"
  },
  "include": ["types/**/*.d.ts", "**/static/**/*.js"]
}
```

For **TypeScript + Vite** plugins, include `types/aiui-sdk.d.ts` in `tsconfig.json` and mark all 5 shell externals (`react`, `react/jsx-runtime`, `react-dom`, `react-dom/client`, `@aiui/sdk`) as Rollup `external` so your plugin shares the shell's single React instance.

## Python typing (PEP 561)

`praisonaiui` ships a `py.typed` marker and lazy `TYPE_CHECKING` stubs in `src/praisonaiui/__init__.py` while keeping cold `import praisonaiui` under 20 ms. Downstream plugins get full type checking in `mypy`, `pyright`, and Pylance:

```python
from praisonaiui import AiuiPlugin, PageSpec, create_shell_app
from praisonaiui.testing import plugin_test_client
```

## Next steps

- Explore every runtime helper in [Browser SDK (`@aiui/sdk`)](https://ui.praison.ai/docs/features/plugins/browser-sdk.md).
- Mount a strict-CSP host in [Shell Hosting & CSP](https://ui.praison.ai/docs/features/plugins/shell-hosting-and-csp.md).
- Write contract tests in [Testing Plugins](https://ui.praison.ai/docs/features/plugins/testing.md).

## Related pages

- [TypeScript SDK Overview](https://ui.praison.ai/docs/typescript.md)
- [TypeScript SDK Reference](https://ui.praison.ai/docs/api/typescript.md)
- [CLI Usage](https://ui.praison.ai/docs/getting-started/cli.md)
