pi-provider-kimi-code 0.3.1 → 0.3.2

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
@@ -2,27 +2,41 @@
2
2
  [![npm](https://img.shields.io/npm/v/pi-provider-kimi-code)](https://www.npmjs.com/package/pi-provider-kimi-code)
3
3
  [![license](https://img.shields.io/npm/l/pi-provider-kimi-code)](./LICENSE)
4
4
 
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.
5
+ **Use Kimi Code in Pi without losing the Kimi-specific bits.**
6
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-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.
7
+ Pi gives you the harness and extension ecosystem. Kimi CLI gives you the official Kimi Code path. This package sits between them: Kimi Code as a Pi provider, plus the details the basic provider keeps simple — file uploads, cache behavior, session reuse, and a base for Kimi-native tools.
8
+
9
+ ## Why this exists
10
+
11
+ Pi already has a built-in `kimi-coding` provider (see [`pi.dev/docs/.../providers`](https://pi.dev/docs/latest/providers)). It is good basic support: point Pi at Kimi Code and start coding.
12
+
13
+ This extension is for the next layer. If you are building real Pi workflows, the Kimi-specific details start to matter:
14
+
15
+ - **Files should go through Kimi's Files API.** Large images are uploaded to `/files` and referenced as `ms://` IDs instead of being sent as huge inline base64 payloads.
16
+ - **Cache behavior should be visible.** Kimi caches by content prefix, not by `prompt_cache_key`. This repo measures that behavior and keeps the provider aligned with it.
17
+ - **Existing Kimi sessions should carry over.** If you already use `kimi-cli`, this extension can reuse that login instead of making you manage another API key.
18
+ - **Protocol choice should stay available.** The default is OpenAI-compatible mode, but Anthropic-compatible mode is still there when your Pi setup needs it.
19
+
20
+ > On prompt caching: the built-in provider works fine. Kimi's Coding endpoint caches by content prefix hash automatically — neither `cache_control` markers nor `prompt_cache_key` actually drive cache hits. See [docs/caching.md](docs/caching.md) for measurements.
8
21
 
9
22
  ## Who is this for?
10
23
 
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.
24
+ - You use Pi as your coding harness and want Kimi Code inside that workflow.
25
+ - You already use `kimi-cli`, but want Pi's extensions, skills, prompts, themes, and custom UI pieces around the same Kimi Code account.
26
+ - You are building on top of Pi and need the Kimi-specific parts to behave predictably: uploads, cache behavior, model metadata, and protocol choice.
14
27
 
15
- Pay-per-token via `KIMI_API_KEY` also works if you just want to try Kimi in CI or without a subscription.
28
+ Pay-per-token via `KIMI_API_KEY` still works if you just want to try Kimi in CI or without a subscription.
16
29
 
17
- ## Features
30
+ ## What this package adds
18
31
 
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.
32
+ - **Kimi account login in Pi** — sign in with your Kimi Code account. `KIMI_API_KEY` still works for CI or pay-per-token use.
33
+ - **`kimi-cli` session reuse** — if you already signed in with the official CLI, this extension can read `~/.kimi/credentials/kimi-code.json` and skip another browser login.
34
+ - **Kimi Files API upload** — large images are uploaded to `/v1/files` and sent as `ms://` references instead of huge inline base64 payloads.
35
+ - **Measured cache behavior** — Kimi's Coding endpoint caches by content prefix hash with TTL ≥ 5 minutes. This repo documents what changes the cache and what does not.
36
+ - **Live model metadata** — name, context window, reasoning support, and image-input support refresh from Kimi's `/v1/models` endpoint at login / refresh time.
37
+ - **Protocol choice** — OpenAI-compatible mode is the default. Anthropic-compatible mode is still available with `KIMI_CODE_PROTOCOL=anthropic`.
38
+ - **Stream cleanup** — Kimi occasionally emits placeholder text during thinking-only responses; this extension hides those blocks before they reach the Pi UI.
39
+ - **No build step** — Pi loads the TypeScript extension directly.
26
40
 
27
41
  ## Install
28
42
 
@@ -50,11 +64,11 @@ curl -L https://github.com/Leechael/pi-provider-kimi-code/releases/latest/downlo
50
64
  pi install /tmp/pi-provider-kimi-code
51
65
  ```
52
66
 
53
- ## Authentication
67
+ ## Sign in
54
68
 
55
- ### Browser login (recommended)
69
+ ### Browser login
56
70
 
57
- Inside `pi`, run:
71
+ Inside Pi, run:
58
72
 
59
73
  ```
60
74
  /login kimi-coding
@@ -62,6 +76,10 @@ Inside `pi`, run:
62
76
 
63
77
  A browser tab opens, you sign into your Kimi account, and credentials are stored at `~/.pi/agent/auth.json`. Tokens refresh automatically.
64
78
 
79
+ ### Already logged in via `kimi-cli`?
80
+
81
+ If `~/.kimi/credentials/kimi-code.json` exists, `/login kimi-coding` reads that file, refreshes the access token if needed, and finishes without opening a browser. The `kimi-cli` credential file is read-only from this extension's perspective — it is never overwritten. Set `KIMI_SHARE_DIR` to point at a non-default location.
82
+
65
83
  ### API key
66
84
 
67
85
  For CI or pay-per-token use, set `KIMI_API_KEY`:
@@ -70,64 +88,85 @@ For CI or pay-per-token use, set `KIMI_API_KEY`:
70
88
  KIMI_API_KEY=sk-... pi
71
89
  ```
72
90
 
73
- ## Models
91
+ ## Model
92
+
93
+ This provider publishes one Pi model ID:
94
+
95
+ ```
96
+ kimi-coding/kimi-for-coding
97
+ ```
98
+
99
+ Kimi keeps the Coding model behind aliases, so this provider does not hardcode a long model list. When you log in or refresh your token, it asks Kimi for the current model info and updates what Pi sees. If your account is on a newer rollout or an internal test, Pi can pick up the latest model name and context size without waiting for this package to release.
100
+
101
+ The fallback values are:
74
102
 
75
- | ID | Name | Reasoning | Input | Context | Max Output |
76
- | ----------------- | --------------- | --------- | ------------------ | ------- | ---------- |
77
- | `kimi-for-coding` | Kimi for Coding | yes | text, image, video | 262 144 | 32 000 |
103
+ - Context window: `262144` tokens
104
+ - Max output: `32000` tokens
105
+ - Input: text and image
106
+ - Reasoning: enabled
78
107
 
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.
108
+ Kimi K2.6 supports Pi's reasoning levels. This provider maps them to Kimi's current reasoning efforts:
80
109
 
81
- Select it inside `pi`:
110
+ - `off` / `none` — reasoning disabled
111
+ - `minimal` / `low` — low effort
112
+ - `medium` — medium effort
113
+ - `high` / `xhigh` — high effort
114
+
115
+ 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).
116
+
117
+ Select it inside Pi:
82
118
 
83
119
  ```
84
120
  /model kimi-coding/kimi-for-coding
85
121
  ```
86
122
 
87
- ## Environment variables
123
+ ## Common knobs
88
124
 
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 |
125
+ Most users do not need environment variables. Two are worth knowing:
97
126
 
98
- Full list with defaults and the test-script variables: [docs/ENV.md](docs/ENV.md).
127
+ - `KIMI_API_KEY` — static API key for CI or pay-per-token use.
128
+ - `KIMI_CODE_PROTOCOL` — `openai` by default; set to `anthropic` if your Pi setup needs Anthropic-compatible requests.
99
129
 
100
- ## FAQ
130
+ The full 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).
101
131
 
102
- ### How is this different from the official `kimi-cli`?
132
+ ## Notes
103
133
 
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.
134
+ ### Kimi CLI vs Pi
105
135
 
106
- ### Do I need a paid Kimi subscription?
136
+ `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.
107
137
 
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.
138
+ ### Cache behavior
109
139
 
110
- ### Where does my data go?
140
+ 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.
111
141
 
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.
142
+ 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.
113
143
 
114
- ### Is this affiliated with Moonshot AI?
144
+ ### Protocol modes
115
145
 
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.
146
+ Kimi's coding endpoint speaks both Anthropic and OpenAI dialects. This extension defaults to OpenAI-compatible mode.
147
+
148
+ `KIMI_CODE_PROTOCOL` supports:
149
+
150
+ - `openai` — default, uses `/coding/v1/chat/completions`
151
+ - `anthropic` — uses `/coding/v1/messages`
117
152
 
118
- ### Which pi version does this work with?
153
+ Use Anthropic mode if your Pi setup depends on Anthropic-style request or tool semantics.
119
154
 
120
- Any recent [pi-coding-agent](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent).
155
+ ### Why OpenAI-compatible mode is the default
121
156
 
122
- ### Why are there two protocol modes?
157
+ 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.
123
158
 
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.
159
+ 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.
160
+
161
+ ### Is this affiliated with Moonshot AI?
162
+
163
+ 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
164
 
126
165
  ## Troubleshooting
127
166
 
128
167
  ### `pi` reports `fetch failed` even though `curl` works
129
168
 
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.
169
+ 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
170
 
132
171
  ### `/login kimi-coding` prints a device code but the browser never opens
133
172
 
@@ -139,11 +178,19 @@ Your Moonshot account needs an active Kimi Code subscription for the provider to
139
178
 
140
179
  ### Large images fail with a payload error
141
180
 
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.
181
+ 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
182
 
144
183
  ### Prompt cache never seems to hit
145
184
 
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).
185
+ 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:
186
+
187
+ - 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.
188
+ - An `image_url` reference (`ms://<file_id>`) changed between turns. Same rule — see Finding 17.
189
+ - 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.
190
+ - A timestamp, request ID, or randomized header is being interpolated into the prompt.
191
+ - The first ~256 tokens of the prompt differ between turns.
192
+
193
+ `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
194
 
148
195
  ### OpenAI-compatible tools complain about a `developer` role
149
196
 
@@ -151,15 +198,16 @@ In OpenAI mode this extension maps the `developer` role to `system` (Kimi's Codi
151
198
 
152
199
  ## References
153
200
 
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)
201
+ - Pi: [earendil-works/pi](https://github.com/earendil-works/pi)
155
202
  - 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
203
  - Environment variables: [docs/ENV.md](docs/ENV.md)
157
204
  - Testing guide: [docs/TESTING.md](docs/TESTING.md)
205
+ - Cache behavior: [docs/caching.md](docs/caching.md)
158
206
  - Architecture notes: [docs/architecture.md](docs/architecture.md)
159
207
 
160
208
  ## Credits
161
209
 
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).
210
+ 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
211
 
164
212
  ## License
165
213