# @oma-home/pi-bailian

English | [简体中文](./README.md)

**@oma-home/pi-bailian** = pi × OhMyAgent × Alibaba Cloud Bailian.

A [pi](https://pi.dev) extension that registers the **Alibaba Cloud Bailian (DashScope)** model provider with a curated model catalog and multi API key management.

> Bailian is **not** a built-in pi provider. pi ships a separate `qwen-token-plan` / `qwen-token-plan-cn` subscription product, but not Bailian's general-purpose OpenAI-compatible endpoint. This extension fills that gap and fixes the foot-guns that make hand-rolled `models.json` entries silently break.

## Why This Extension?

1. **`developer` role 400 error** — pi's `openai-completions` sender uses the newer OpenAI `developer` system role by default; Bailian only accepts `system`. Without this extension, every request fails with `developer is not one of [...]`. We set `supportsDeveloperRole: false` on every model so pi sends `system`.

2. **Thinking models don't stream reasoning** — Bailian's thinking models (Qwen3.x / DeepSeek-V4 / GLM-5.x / Kimi-K2.x / MiniMax-M2.x) use a top-level `enable_thinking` field and stream reasoning via `reasoning_content` — neither is OpenAI-standard. This extension wires `thinkingFormat: "qwen"` + `requiresReasoningContentOnAssistantMessages: true` so reasoning just works.

3. **Multi API key management** — Configure multiple Bailian API keys, each with a name and custom model list. Switch between them with `/bailian` — takes effect immediately.

## Quick Start

### Install

```bash
# From npm (recommended)
pi install npm:@oma-home/pi-bailian

# From GitHub
pi install git:github.com/oma-home/pi-bailian

# Local development
pi -e /path/to/pi-bailian
```

### Configure API Key

**Option 1: Multi-key management (recommended)**

In a pi session, run `/bailian` → **Add API Key**:

1. Enter a name (e.g. `zhangsan`)
2. Enter your API key (get one at [Bailian Console](https://bailian.console.aliyun.com) → API-KEY)
3. Toggle models on/off (multi-select)
4. Save — takes effect immediately

Config is stored at `~/.pi/agent/bailian-keys.json`:

```json
{
  "active": "zhangsan",
  "keys": [
    {
      "name": "zhangsan",
      "apiKey": "sk-xxxxxxxx",
      "models": ["glm-5.2", "kimi-k2.7-code", "qwen3.7-plus"]
    },
    {
      "name": "lisi",
      "apiKey": "sk-bbbbbbbb",
      "models": ["deepseek-v4-pro", "qwen3.7-max"]
    }
  ]
}
```

Switch keys: `/bailian` → **Switch API Key**.

**Option 2: Environment variable (single key)**

```bash
export DASHSCOPE_API_KEY=sk-xxxxxxxx
```

When no config file exists, falls back to this mode with all models available.

### Usage

```bash
# CLI
pi --provider bailian --model qwen3.7-plus

# Interactive
/model              # pick from bailian/* entries
/bailian            # manage API keys
```

## Curated Model Catalog

9 curated models, verified against [official Bailian docs](https://help.aliyun.com/zh/model-studio/model-pricing) (2026-07). Pricing is **华北2(北京) list price** (¥/million tokens).

| Model ID | Context | Reasoning | Input | In / Out (¥/M) | Cache |
|:---|:---|:---|:---|:---|:---|
| deepseek-v4-pro | 1M | ✅ | text | 12 / 24 | ✓ |
| deepseek-v4-flash | 1M | ✅ | text | 1 / 2 | ✓ |
| glm-5.2 | 1M | ✅ | text | 8 / 28 | — |
| kimi-k2.6 | 256K | ✅ | text+image | 6.5 / 27 | ✓ |
| kimi-k2.7-code | 256K | ✅ (thinking-only) | text+image | 6.5 / 27 | ✓ |
| MiniMax-M2.5 | 192K | ✅ (thinking-only) | text | 2.1 / 8.4 | — |
| qwen3.7-flash | 1M | ✅ | text | 0.2 / 0.8 | ✓ |
| qwen3.7-max | 1M | ✅ | text | 12 / 36 | ✓ |
| qwen3.7-plus | 1M | ✅ | text | 2 / 8 | ✓ |

### Cache Pricing

For models with ✓ (supports Bailian context cache), per the official billing rule:
- `cacheRead` = 10% of input price
- `cacheWrite` = 125% of input price

### maxTokens

`maxTokens` is a conservative default. Tune it upward in `index.ts` if your workload needs longer completions.

## Endpoint

```
https://dashscope.aliyuncs.com/compatible-mode/v1
```

Bailian's OpenAI-compatible Chat Completions endpoint. Requests carry `Authorization: Bearer <api-key>`.

## Commands

| Command | Description |
|:---|:---|
| `/bailian` | Open API key management menu |
| ├ Switch API Key | Switch the active API key |
| ├ Add API Key | Add a new API key (name + model selection) |
| └ Remove API Key | Remove a configured API key |

## Development

```bash
git clone https://github.com/oma-home/pi-bailian.git
cd pi-bailian
npm install          # install typecheck deps
npx tsc --noEmit     # typecheck
pi -e .              # local test
```

## Sources

- [Model catalog & pricing](https://help.aliyun.com/zh/model-studio/model-pricing)
- [Text generation](https://help.aliyun.com/zh/model-studio/text-generation-model/)
- [Deep thinking usage](https://help.aliyun.com/zh/model-studio/deep-thinking)
- [Rate limit](https://help.aliyun.com/zh/model-studio/rate-limit)

## License

MIT

---

*Built by [OhMyAgent](https://ohmyagent.ai) — your AI command center.*