pi-provider-kimi-code 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +107 -29
  2. package/index.ts +737 -356
  3. package/package.json +45 -12
package/README.md CHANGED
@@ -1,16 +1,38 @@
1
- # pi-provider-kimi-code
2
-
1
+ # Pi extension for Kimi Code
3
2
  [![npm](https://img.shields.io/npm/v/pi-provider-kimi-code)](https://www.npmjs.com/package/pi-provider-kimi-code)
3
+ [![license](https://img.shields.io/npm/l/pi-provider-kimi-code)](./LICENSE)
4
+
5
+ **Reuse your [Kimi Code Membership](https://www.kimi.com/code/docs/en/) inside [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent)** — no separate API credits, no second billing dashboard. Every request draws from your Kimi Code membership quota (the 5-hour token bucket) instead of billing per-token on the Moonshot Open Platform.
6
+
7
+ It's **"Claude Code for Kimi"** — log in with your Kimi account, with `KIMI_API_KEY` still supported as a fallback for CI. You get Kimi K2.5, Kimi K2 Thinking Turbo, and the umbrella `kimi-code` model with a 262K-token context window, automatic prompt caching, automatic large-image and video upload, and compatibility with both Anthropic-style and OpenAI-style clients.
8
+
9
+ ## Who is this for?
10
+
11
+ - You already pay for a **[Kimi Code Membership](https://www.kimi.com/code/docs/en/)** and want to use it inside `pi-coding-agent` instead of the official `kimi-cli` — see [MoonshotAI/kimi-cli#757](https://github.com/MoonshotAI/kimi-cli/issues/757) for the canonical feature request this extension answers.
12
+ - You want **"Claude Code for Kimi"**: log in with your Kimi account instead of buying separate API credits. (`KIMI_API_KEY` is also supported as a fallback for CI.)
13
+ - You're in the [pi / pi-mono](https://github.com/badlogic/pi-mono) ecosystem and want **Kimi K2.5** or **Kimi K2 Thinking Turbo** as a pi provider.
4
14
 
5
- A [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent) custom provider extension that adds [Kimi Code](https://kimi.com) models with OAuth device-code login.
15
+ Pay-per-token via `KIMI_API_KEY` also works if you just want to try Kimi in CI or without a subscription.
16
+
17
+ ## Features
18
+
19
+ - **Browser login with your Kimi account** — reuse your Kimi Code Membership without buying separate API credits. Credentials are stored locally and refreshed automatically. `KIMI_API_KEY` is also accepted for pay-per-token or CI use.
20
+ - **262K-token context window** on all three registered models.
21
+ - **Automatic prompt caching** — binds Kimi's prompt cache to your pi session so repeated calls hit the cache cheaply (observed TTL ~5-10 minutes). Honors pi's `PI_CACHE_RETENTION=none` if you want to disable caching entirely.
22
+ - **Automatic large-image and video upload** — images over 1 MB and all videos are uploaded to Kimi ahead of time, so you don't hit inline payload limits.
23
+ - **Works with both Anthropic- and OpenAI-compatible modes** — use whichever one your pi setup expects.
24
+ - **Stream cleaning** — Kimi occasionally leaks placeholder blocks into the stream during thinking phases; this extension catches and hides them so your pi UI stays clean.
25
+ - **Zero dependencies, zero build step** — the extension loads directly as TypeScript, nothing to compile.
6
26
 
7
27
  ## Install
8
28
 
29
+ From npm:
30
+
9
31
  ```bash
10
32
  pi install npm:pi-provider-kimi-code
11
33
  ```
12
34
 
13
- Or load without installing:
35
+ Or load a local checkout without installing:
14
36
 
15
37
  ```bash
16
38
  pi -e /path/to/pi-provider-kimi-code
@@ -18,19 +40,19 @@ pi -e /path/to/pi-provider-kimi-code
18
40
 
19
41
  ## Authentication
20
42
 
21
- ### OAuth (recommended)
43
+ ### Browser login (recommended)
22
44
 
23
- Inside pi, run:
45
+ Inside `pi`, run:
24
46
 
25
47
  ```
26
48
  /login kimi-coding
27
49
  ```
28
50
 
29
- This starts the device-code flow — a browser window opens, you authorize, and credentials are stored automatically.
51
+ A browser tab opens, you sign into your Kimi account, and credentials are stored at `~/.pi/agent/auth.json`. Tokens refresh automatically.
30
52
 
31
- ### API Key
53
+ ### API key
32
54
 
33
- Set the `KIMI_API_KEY` environment variable:
55
+ For CI or pay-per-token use, set `KIMI_API_KEY`:
34
56
 
35
57
  ```bash
36
58
  KIMI_API_KEY=sk-... pi
@@ -38,38 +60,94 @@ KIMI_API_KEY=sk-... pi
38
60
 
39
61
  ## Models
40
62
 
41
- | ID | Name | Reasoning | Input | Context | Max Output |
42
- |---------------------------|--------------------------------|-----------|-------------|----------|------------|
43
- | `kimi-code` | Kimi Code (powered by kimi-k2.5) | yes | text, image | 262 144 | 32 768 |
44
- | `kimi-k2.5` | Kimi K2.5 | yes | text, image | 262 144 | 32 768 |
45
- | `kimi-k2-thinking-turbo` | Kimi K2 Thinking Turbo | yes | text | 262 144 | 32 768 |
63
+ | ID | Name | Reasoning | Input | Context | Max Output |
64
+ | ------------------------ | -------------------------------- | --------- | ----------- | ------- | ---------- |
65
+ | `kimi-code` | Kimi Code (powered by kimi-k2.5) | yes | text, image | 262 144 | 32 000 |
66
+ | `kimi-k2.5` | Kimi K2.5 | yes | text, image | 262 144 | 32 000 |
67
+ | `kimi-k2-thinking-turbo` | Kimi K2 Thinking Turbo | yes | text | 262 144 | 32 000 |
46
68
 
47
- Select a model inside pi:
69
+ Select a model inside `pi`:
48
70
 
49
71
  ```
50
72
  /model kimi-coding/kimi-code
51
73
  ```
52
74
 
53
- ## Environment Overrides
75
+ ## Environment variables
76
+
77
+ | Variable | Description |
78
+ | ---------------------------------- | ------------------------------------------------------ |
79
+ | `KIMI_API_KEY` | Static API key (alternative to browser login) |
80
+ | `KIMI_CODE_BASE_URL` | Override the API base URL |
81
+ | `KIMI_CODE_OAUTH_HOST` | Override the OAuth host |
82
+ | `KIMI_CODE_PROTOCOL` | `anthropic` (default) or `openai` |
83
+ | `KIMI_CODE_UPLOAD_THRESHOLD_BYTES` | Image auto-upload threshold, default `1048576` (1 MB) |
84
+ | `KIMI_CODE_DEBUG` | Set to `1` to print provider-side debug logs |
85
+
86
+ Full list with defaults and the test-script variables: [docs/ENV.md](docs/ENV.md).
87
+
88
+ ## FAQ
89
+
90
+ ### How is this different from the official `kimi-cli`?
91
+
92
+ `kimi-cli` is Moonshot's own terminal agent. It's a full CLI — it replaces pi, you can't use it as a pi provider. This extension is the bridge: it lets you keep pi as your harness and point it at your Kimi Code Membership for inference, reusing the same login flow `kimi-cli` v1.3+ ships.
93
+
94
+ ### Do I need a paid Kimi subscription?
95
+
96
+ For browser login, yes — whatever your Moonshot account is entitled to is what the provider can access. Without a plan you'll hit rate limits quickly. For pay-per-token usage, set `KIMI_API_KEY` instead.
97
+
98
+ ### Where does my data go?
99
+
100
+ Requests go only to `api.kimi.com` (Moonshot's servers). Login credentials are stored locally at `~/.pi/agent/auth.json`, readable only by your user. Nothing is uploaded to any third party.
101
+
102
+ ### Is this affiliated with Moonshot AI?
103
+
104
+ No. This is an independent extension. The login flow is derived from the public implementation in the open-source [`kimi-cli`](https://github.com/MoonshotAI/kimi-cli) repository.
105
+
106
+ ### Which pi version does this work with?
107
+
108
+ Any recent [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent).
109
+
110
+ ### Why are there two protocol modes?
111
+
112
+ Kimi's coding endpoint speaks both Anthropic and OpenAI dialects. Anthropic mode has better visibility into cache hits, so it's the default. Switch via `KIMI_CODE_PROTOCOL=openai` if something in your pi setup prefers the OpenAI path.
113
+
114
+ ## Troubleshooting
115
+
116
+ ### `pi` reports `fetch failed` even though `curl` works
117
+
118
+ pi runs on Node's `fetch` / undici stack, which handles `http_proxy` / `https_proxy` / `all_proxy` differently from `curl`. Verify those variables in the pi process's environment. The bundled smoke-test script `scripts/test_e2e.sh` prints the effective proxy-related environment for easier debugging.
119
+
120
+ ### `/login kimi-coding` prints a device code but the browser never opens
121
+
122
+ The login flow always prints a verification URL — opening it is the terminal's job. If your terminal or OS blocks auto-open, copy the URL and paste it into a browser manually.
123
+
124
+ ### "Access denied" or subscription errors after a successful login
125
+
126
+ Your Moonshot account needs an active Kimi Code subscription for the provider to do anything useful. If the same account works in `kimi-cli`, re-run `/login kimi-coding` to refresh credentials.
127
+
128
+ ### Large images fail with a payload error
129
+
130
+ This extension uploads images over `KIMI_CODE_UPLOAD_THRESHOLD_BYTES` (default 1 MB) to Kimi's Files API and references them as `ms://`. Videos always upload. Set `KIMI_CODE_DEBUG=1` to see upload decisions in the provider logs.
131
+
132
+ ### Prompt cache never seems to hit
133
+
134
+ The cache is keyed off your pi session, which changes between sessions. Long-lived sessions get cache hits within a few minutes; cold sessions won't. Also check `PI_CACHE_RETENTION` — if it's set to `none`, this extension intentionally skips cache-key injection. For deterministic measurement, run the cache-TTL probe with a fixed key — see [docs/TESTING.md](docs/TESTING.md).
135
+
136
+ ### OpenAI-compatible tools complain about a `developer` role
54
137
 
55
- - `KIMI_CODE_BASE_URL` — override the default API base URL (`https://api.kimi.com/coding`)
56
- - `KIMI_CODE_OAUTH_HOST` — override the OAuth host
57
- - `KIMI_OAUTH_HOST` — fallback OAuth host override for compatibility
138
+ In OpenAI mode this extension maps the `developer` role to `system` (Kimi's Coding endpoint does not recognize `developer`). If something in your toolchain expects `developer` to round-trip, use Anthropic mode instead.
58
139
 
59
- ## How It Works
140
+ ## References
60
141
 
61
- - Registers provider `kimi-coding` with base URL `https://api.kimi.com/coding`
62
- - Uses `api: "anthropic-messages"` — Kimi's API is wire-compatible with the Anthropic Messages format
63
- - Adds a small `streamSimple` wrapper to suppress Kimi's leaked `(Empty response: ...)` placeholder blocks
64
- - Sends the same `KimiCLI/1.28.0` + `X-Msh-*` headers as current `kimi-cli`
65
- - Persists a stable device ID at `~/.pi/providers/kimi-coding/device_id`
66
- - OAuth uses [RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628) device authorization grant against `https://auth.kimi.com`
67
- - Zero dependencies — types from `@mariozechner/pi-ai` and `@mariozechner/pi-coding-agent` are provided by the pi runtime
68
- - Zero build step — pi loads TypeScript directly via jiti
142
+ - Upstream harness: [badlogic/pi-mono](https://github.com/badlogic/pi-mono) · [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent)
143
+ - Upstream login implementation and feature request: [MoonshotAI/kimi-cli](https://github.com/MoonshotAI/kimi-cli) · [kimi-cli#757](https://github.com/MoonshotAI/kimi-cli/issues/757)
144
+ - Environment variables: [docs/ENV.md](docs/ENV.md)
145
+ - Testing guide: [docs/TESTING.md](docs/TESTING.md)
146
+ - Architecture notes: [docs/architecture.md](docs/architecture.md)
69
147
 
70
148
  ## Credits
71
149
 
72
- This extension is based on the OAuth implementation from [kimi-cli](https://github.com/MoonshotAI/kimi-cli) by Moonshot AI.
150
+ Based on the login implementation from [`kimi-cli`](https://github.com/MoonshotAI/kimi-cli) by Moonshot AI. Built as an extension for [`pi-coding-agent`](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent) by [@badlogic](https://github.com/badlogic).
73
151
 
74
152
  ## License
75
153