pi-provider-kimi-code 0.3.1 → 0.4.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.
package/README.md CHANGED
@@ -1,28 +1,33 @@
1
1
  # Pi extension for Kimi Code
2
+
2
3
  [![npm](https://img.shields.io/npm/v/pi-provider-kimi-code)](https://www.npmjs.com/package/pi-provider-kimi-code)
3
4
  [![license](https://img.shields.io/npm/l/pi-provider-kimi-code)](./LICENSE)
4
5
 
5
- **Reuse your [Kimi Code Plan](https://www.kimi.com/code/docs/en/) inside [pi-coding-agent](https://pi.dev/)** — 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
+ **Use Kimi Code in Pi, with the Kimi parts handled.**
6
7
 
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-for-coding` (the Kimi-k2.6 latest alias, backed by the same Kimi Code Plan that covers K2.6 and K2.5) 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
+ Pi already has a basic `kimi-coding` provider. This extension is for the parts that start to matter once Kimi Code becomes part of your real Pi workflow: account login reuse, file uploads, measured cache behavior, live model metadata, and optional Moonshot search/fetch tools.
8
9
 
9
- ## Who is this for?
10
+ ## Why this exists
10
11
 
11
- - You already pay for a **[Kimi Code Plan](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.6** (and the K2.5 coverage that comes with the Code Plan) as a pi provider.
12
+ Pi is a small harness you adapt to your own workflow. Kimi CLI is Moonshot's official Kimi Code path. This package sits between them: Kimi Code as a Pi provider, plus the Kimi-specific details that the basic provider keeps simple.
14
13
 
15
- Pay-per-token via `KIMI_API_KEY` also works if you just want to try Kimi in CI or without a subscription.
14
+ - **Use your Kimi account.** Log in with `/login kimi-coding`, or reuse an existing `kimi-cli` session.
15
+ - **Send files the Kimi way.** Large inline images go through Kimi's Files API and become `ms://` references instead of huge base64 payloads.
16
+ - **Know what the cache is doing.** Kimi caches by content prefix. This repo measures that behavior instead of pretending `prompt_cache_key` controls it.
17
+ - **Use the safer protocol by default.** OpenAI-compatible mode is the default because tool results are less ambiguous there. Anthropic-compatible mode is still available.
18
+ - **Turn on Kimi-native tools when you want them.** `moonshot_search` and `moonshot_fetch` are opt-in, configurable per user or per project.
16
19
 
17
- ## Features
20
+ ## What this package adds
18
21
 
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 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.
22
+ - Kimi account login in Pi, plus `KIMI_API_KEY` for CI or pay-per-token use.
23
+ - `kimi-cli` credential reuse from `~/.kimi/credentials/kimi-code.json`.
24
+ - Kimi Files API uploads for large inline images.
25
+ - Live model metadata from Kimi at login / refresh time.
26
+ - OpenAI-compatible mode by default, Anthropic-compatible mode on request.
27
+ - Kimi K2.6 reasoning level mapping for Pi's reasoning controls.
28
+ - Stream cleanup for Kimi's thinking-only placeholder text.
29
+ - Optional `moonshot_search` and `moonshot_fetch` tools via `/kimi-settings`.
30
+ - No build step; Pi loads the TypeScript extension directly.
26
31
 
27
32
  ## Install
28
33
 
@@ -43,107 +48,188 @@ pi -e /path/to/pi-provider-kimi-code
43
48
  If you prefer not to use npm, download the tarball from the [latest release](https://github.com/Leechael/pi-provider-kimi-code/releases/latest), extract it, and install from the local path:
44
49
 
45
50
  ```bash
46
- # Download and extract
47
51
  curl -L https://github.com/Leechael/pi-provider-kimi-code/releases/latest/download/pi-provider-kimi-code.tar.gz | tar -xz -C /tmp
48
-
49
- # Install from the extracted directory
50
52
  pi install /tmp/pi-provider-kimi-code
51
53
  ```
52
54
 
53
- ## Authentication
54
-
55
- ### Browser login (recommended)
55
+ ## Sign in
56
56
 
57
- Inside `pi`, run:
57
+ Inside Pi, run:
58
58
 
59
- ```
59
+ ```text
60
60
  /login kimi-coding
61
61
  ```
62
62
 
63
- A browser tab opens, you sign into your Kimi account, and credentials are stored at `~/.pi/agent/auth.json`. Tokens refresh automatically.
63
+ A browser tab opens, you sign into your Kimi account, and Pi stores the credential at `~/.pi/agent/auth.json`. Tokens refresh automatically.
64
+
65
+ If you already use `kimi-cli`, this extension can reuse its credential file:
66
+
67
+ ```text
68
+ ~/.kimi/credentials/kimi-code.json
69
+ ```
64
70
 
65
- ### API key
71
+ The file is read-only from this extension's side. Set `KIMI_SHARE_DIR` if your `kimi-cli` share directory lives somewhere else.
66
72
 
67
- For CI or pay-per-token use, set `KIMI_API_KEY`:
73
+ For CI or pay-per-token use, set `KIMI_API_KEY` instead:
68
74
 
69
75
  ```bash
70
76
  KIMI_API_KEY=sk-... pi
71
77
  ```
72
78
 
73
- ## Models
79
+ ## Model
74
80
 
75
- | ID | Name | Reasoning | Input | Context | Max Output |
76
- | ----------------- | --------------- | --------- | ------------------ | ------- | ---------- |
77
- | `kimi-for-coding` | Kimi for Coding | yes | text, image, video | 262 144 | 32 000 |
81
+ This provider publishes one Pi model ID:
78
82
 
79
- `kimi-for-coding` is the latest alias on the Kimi Code Plan — today it points at Kimi-k2.6. The Plan itself still covers K2.5; the upstream routes older model IDs to the current alias, but this provider only publishes the canonical `kimi-for-coding` entry.
83
+ ```text
84
+ kimi-coding/kimi-for-coding
85
+ ```
80
86
 
81
- Select it inside `pi`:
87
+ Select it inside Pi:
82
88
 
83
- ```
89
+ ```text
84
90
  /model kimi-coding/kimi-for-coding
85
91
  ```
86
92
 
87
- ## Environment variables
93
+ Kimi keeps Coding models behind aliases. Rather than hardcoding a stale model list, this extension asks Kimi for the current model info when you log in or refresh. If your account is on a newer rollout or internal test, Pi can pick up the latest model name and context size without waiting for a package release.
88
94
 
89
- | Variable | Description |
90
- | ---------------------------------- | ------------------------------------------------------ |
91
- | `KIMI_API_KEY` | Static API key (alternative to browser login) |
92
- | `KIMI_CODE_BASE_URL` | Override the API base URL |
93
- | `KIMI_CODE_OAUTH_HOST` | Override the OAuth host |
94
- | `KIMI_CODE_PROTOCOL` | `anthropic` (default) or `openai` |
95
- | `KIMI_CODE_UPLOAD_THRESHOLD_BYTES` | Image auto-upload threshold, default `1048576` (1 MB) |
96
- | `KIMI_CODE_DEBUG` | Set to `1` to print provider-side debug logs |
95
+ Fallback values:
97
96
 
98
- Full list with defaults and the test-script variables: [docs/ENV.md](docs/ENV.md).
97
+ - Context window: `262144` tokens
98
+ - Max output: `32000` tokens
99
+ - Input: text and image
100
+ - Reasoning: enabled
99
101
 
100
- ## FAQ
102
+ Kimi K2.6 supports Pi's reasoning levels. This provider maps them to Kimi's current reasoning efforts:
101
103
 
102
- ### How is this different from the official `kimi-cli`?
104
+ - `off` / `none` — reasoning disabled
105
+ - `minimal` / `low` — low effort
106
+ - `medium` — medium effort
107
+ - `high` / `xhigh` — high effort
103
108
 
104
- `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.
109
+ If Kimi reports a larger context window, the provider uses that value. You can also override the advertised context or model name with `KIMI_MODEL_MAX_CONTEXT_SIZE` / `KIMI_MODEL_NAME`; see [docs/ENV.md](docs/ENV.md).
105
110
 
106
- ### Do I need a paid Kimi subscription?
111
+ ## Optional Moonshot tools
107
112
 
108
- 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.
113
+ Kimi Coding has server-side search and fetch tools. This extension can expose them to Pi as:
109
114
 
110
- ### Where does my data go?
115
+ - `moonshot_search`
116
+ - `moonshot_fetch`
111
117
 
112
- 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.
118
+ They are off by default. Turn them on when you want Kimi's own search/fetch path in the agent loop.
113
119
 
114
- ### Is this affiliated with Moonshot AI?
120
+ Inside Pi, run:
115
121
 
116
- 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.
122
+ ```text
123
+ /kimi-settings
124
+ ```
125
+
126
+ That command shows your current Kimi usage summary and lets you edit the home or project config. Changes apply to the active session tool set.
127
+
128
+ Config files are JSON:
129
+
130
+ - Home: `~/.pi/pi-provider-kimi-code.json`
131
+ - Project override: `<cwd>/.pi/pi-provider-kimi-code.json`
132
+
133
+ Project config overrides home config with a deep merge. Missing files or missing keys mean both tools stay off.
134
+
135
+ ```json
136
+ {
137
+ "tools": {
138
+ "moonshot_search": { "enabled": true, "default_collapsed": true },
139
+ "moonshot_fetch": { "enabled": true, "default_collapsed": true }
140
+ }
141
+ }
142
+ ```
143
+
144
+ Both tools require `/login kimi-coding` OAuth credentials. `KIMI_API_KEY` is not used for these tools. Your account also needs access to Moonshot's server-side search/fetch services; if not, the upstream service can return subscription or whitelist errors.
145
+
146
+ `default_collapsed` controls only the TUI preview. The full tool result still goes to the model; set it to `false` if you want previews expanded by default.
147
+
148
+ If you already use another search or fetch tool, pick one path for a session. Overlapping tools can make the model choose the wrong one.
149
+
150
+ ## Common knobs
151
+
152
+ Most users do not need environment variables. Two are worth knowing:
153
+
154
+ - `KIMI_API_KEY` — static API key for CI or pay-per-token use.
155
+ - `KIMI_CODE_PROTOCOL` — `openai` by default; set to `anthropic` if your Pi setup needs Anthropic-compatible requests.
117
156
 
118
- ### Which pi version does this work with?
157
+ Moonshot search/fetch tools are configured through `/kimi-settings` or JSON config files, not env vars.
119
158
 
120
- Any recent [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent).
159
+ The full env list, including base URL overrides, `kimi-cli` path overrides, upload tuning, debug logs, and model metadata overrides, lives in [docs/ENV.md](docs/ENV.md).
121
160
 
122
- ### Why are there two protocol modes?
161
+ ## Notes
123
162
 
124
- 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.
163
+ ### Kimi CLI vs Pi
164
+
165
+ `kimi-cli` is Moonshot's official terminal agent. Pi is the harness you adapt to your own workflow: extensions, skills, prompts, themes, custom commands, status bars, and UI pieces. This package lets you keep Kimi Code as the model path while staying inside Pi.
166
+
167
+ ### Cache behavior
168
+
169
+ Kimi's cache is content-based. It fires automatically when your prompt prefix matches an earlier request. `prompt_cache_key` and Anthropic `cache_control` markers do not control cache hits on the Coding endpoint.
170
+
171
+ See [docs/caching.md](docs/caching.md) for the measured behavior: TTL, cross-protocol cache reuse, 256-token prefix alignment, and the cases that invalidate cache.
172
+
173
+ ### Protocol modes
174
+
175
+ Kimi's coding endpoint speaks both Anthropic and OpenAI dialects. This extension defaults to OpenAI-compatible mode.
176
+
177
+ `KIMI_CODE_PROTOCOL` supports:
178
+
179
+ - `openai` — default, uses `/coding/v1/chat/completions`
180
+ - `anthropic` — uses `/coding/v1/messages`
181
+
182
+ Use Anthropic mode if your Pi setup depends on Anthropic-style request or tool semantics.
183
+
184
+ ### Why OpenAI-compatible mode is the default
185
+
186
+ The Anthropic-compatible protocol has an awkward tool-result shape: tool output is wrapped inside a `role: "user"` message. That is valid Anthropic format, but Kimi can sometimes read it like a real user message and continue from the wrong premise in multi-turn tool loops.
187
+
188
+ The OpenAI-compatible protocol uses a dedicated `role: "tool"` message, so the boundary is clearer. That is why this extension now defaults to `openai`. The Anthropic path stays available for setups that need it. See [issue #5](https://github.com/Leechael/pi-provider-kimi-code/issues/5) for details.
189
+
190
+ ### Is this affiliated with Moonshot AI?
191
+
192
+ 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.
125
193
 
126
194
  ## Troubleshooting
127
195
 
128
196
  ### `pi` reports `fetch failed` even though `curl` works
129
197
 
130
- 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.
198
+ 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.
131
199
 
132
200
  ### `/login kimi-coding` prints a device code but the browser never opens
133
201
 
134
- 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.
202
+ The login flow always prints a verification URL. If your terminal or OS blocks auto-open, copy the URL and paste it into a browser manually.
135
203
 
136
204
  ### "Access denied" or subscription errors after a successful login
137
205
 
138
206
  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.
139
207
 
208
+ ### Moonshot tools do not show up
209
+
210
+ Run `/kimi-settings` and check whether `moonshot_search` / `moonshot_fetch` are enabled in the home or project config. These tools also require `/login kimi-coding`; `KIMI_API_KEY` is not enough.
211
+
212
+ If you changed the JSON config by hand, reload it through `/kimi-settings` or start a new Pi session.
213
+
214
+ ### Moonshot tools return subscription or whitelist errors
215
+
216
+ The tools call Moonshot's server-side search/fetch services. Some accounts may not have access even with an active Kimi Code Plan. The error comes from the upstream service.
217
+
140
218
  ### Large images fail with a payload error
141
219
 
142
- 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.
220
+ This extension uploads images over `KIMI_CODE_UPLOAD_THRESHOLD_BYTES` (default 1 MB) to Kimi's Files API and references them as `ms://`. Set `KIMI_CODE_DEBUG=1` to see upload decisions in the provider logs.
143
221
 
144
222
  ### Prompt cache never seems to hit
145
223
 
146
- 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).
224
+ Kimi's cache is **content-based**: it fires automatically when your prompt prefix matches an earlier request, independent of any explicit cache key. If `cache_read_input_tokens` (Anthropic) or `cached_tokens` (OpenAI) stays at `0` across calls, something in the prompt is varying between turns. The usual suspects:
225
+
226
+ - On the Anthropic-compat endpoint, the `system` prompt or the `tools` array changed (e.g. an extension changed the tool set mid-session). Either fully invalidates the cache — see [docs/caching.md](docs/caching.md), Finding 11. OpenAI-compat endpoint not yet measured for this scenario.
227
+ - An `image_url` reference (`ms://<file_id>`) changed between turns. Same rule — see Finding 17.
228
+ - You're comparing across two cold `pi` invocations, not within a single session. Cache benefits accrue **within** a session; Pi's system/tools assembly drifts between sessions. See Finding 18.
229
+ - A timestamp, request ID, or randomized header is being interpolated into the prompt.
230
+ - The first ~256 tokens of the prompt differ between turns.
231
+
232
+ `PI_CACHE_RETENTION=none` skips `prompt_cache_key` injection but **does not** disable Kimi's caching (the cache is unconditional). For deterministic measurement, run `scripts/test_e2e.sh` with `KIMI_E2E_ONLY_CACHE=1`. See [docs/caching.md](docs/caching.md) for full mechanics.
147
233
 
148
234
  ### OpenAI-compatible tools complain about a `developer` role
149
235
 
@@ -151,15 +237,16 @@ In OpenAI mode this extension maps the `developer` role to `system` (Kimi's Codi
151
237
 
152
238
  ## References
153
239
 
154
- - 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)
240
+ - Pi: [earendil-works/pi](https://github.com/earendil-works/pi)
155
241
  - 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)
156
242
  - Environment variables: [docs/ENV.md](docs/ENV.md)
157
243
  - Testing guide: [docs/TESTING.md](docs/TESTING.md)
244
+ - Cache behavior: [docs/caching.md](docs/caching.md)
158
245
  - Architecture notes: [docs/architecture.md](docs/architecture.md)
159
246
 
160
247
  ## Credits
161
248
 
162
- 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).
249
+ Based on the login implementation from [`kimi-cli`](https://github.com/MoonshotAI/kimi-cli) by Moonshot AI. Built as a Pi extension by [@badlogic](https://github.com/badlogic).
163
250
 
164
251
  ## License
165
252