# knowit-benchai

BenchAI CLI publishes and clones team SOP document repositories.

## Install

```bash
npm install -g knowit-benchai
```

The installed command is:

```bash
benchai help
```

## Configure

Login with your BenchAI account. This creates a personal API key and saves it locally:

```bash
benchai login --email test@example.com
```

The saved API key is used by BenchAI CLI commands and Git HTTP operations. For raw Git prompts, use your account email as the username and the API key as the password.

```bash
export BENCHAI_GIT_BASE_URL=http://47.241.223.57:8083
export BENCHAI_API_KEY=bai_xxx
```

Or save the key once on this machine:

```bash
benchai key set --base-url http://47.241.223.57:8083 bai_xxx
benchai key show
```

The key is stored in `~/.benchai/config.json` with file permission `600`. Environment variables and command flags still override the saved value.

All SOP document commands require an API key, including `list`, `clone`, `pull`, `publish`, and `update`. `benchai login` is the recommended way to obtain and save that key.

## Use

Publish any local folder as a BenchAI SOP document repository:

```bash
benchai publish ./my-experience-docs --name "团队经验文档" --description "团队可复用经验和标准"
```

List available SOP document repositories:

```bash
benchai list
```

The list shows document code, name, and description. Use the code to clone or rename.

Clone one document by code:

```bash
benchai clone ABC123
```

Pull latest changes for a cloned document repository and refresh `UPDATE-RULE.md`:

```bash
benchai pull .
```

Rename one document by code:

```bash
benchai rename ABC123 --name "新的经验文档名称"
```

Create a new API key from your BenchAI account and save it locally:

```bash
benchai key create --email test@example.com --password password123
```

`benchai key create` saves the created token locally by default. Use `--save=false` if you only want to print it.

Publishing and cloning a SOP document repository both ensure `UPDATE-RULE.md` exists. Agents should read it, pull the latest docs before editing, and use:

```bash
benchai pull .
```

Then update reusable experience with:

```bash
benchai update . --message "Update experience documents"
```

when a user request produces reusable experience worth saving.
After a successful commit and push, `benchai update` also submits the document update to the BenchAI backend so reusable lessons can be extracted into the experience fragment library. Pass `--skip-fragments` for pure formatting or maintenance-only changes.

Run `benchai update .` from the root of the cloned document repository. If you are in another folder, pass the document repository path explicitly:

```bash
benchai update /path/to/document-repository --message "Update experience documents"
```

## Capture Codex prompts

Start Codex through BenchAI to record submitted user prompts through Codex `UserPromptSubmit` hooks. Captured prompts are sent to the BenchAI backend, stored in MongoDB, and indexed into Pinecone when Pinecone is configured:

```bash
benchai capture -- codex
```

Codex arguments can be passed after `codex`:

```bash
benchai capture -- codex --model gpt-5.5
```

The command installs a user-level Codex hook in `~/.codex/hooks.json` if needed, then launches Codex with capture enabled for that session. The hook calls:

```bash
POST /api/prompts/capture
```

If the backend call fails, the hook writes a local fallback JSONL file. Use `--log-dir` to choose that fallback directory:

```bash
benchai capture --log-dir ./benchai-prompts -- codex
```

If Codex asks to review hooks, open `/hooks` inside Codex and trust the BenchAI capture hook.

## Maintainers

Build the local wrapper binary:

```bash
npm run build:local
```

Build prebuilt binaries before publishing:

```bash
BENCHAI_DEFAULT_BASE_URL=http://47.241.223.57:8083 npm run build:prebuilds
npm publish --access public
```

If binaries are hosted outside the npm package, set one of these during install:

```bash
BENCHAI_CLI_DOWNLOAD_BASE=https://example.com/benchai npm install -g knowit-benchai
BENCHAI_CLI_DOWNLOAD_URL=https://example.com/benchai-darwin-arm64 npm install -g knowit-benchai
```
