pi-provider-kimi-code 0.3.0 → 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 +99 -51
- package/index.ts +42 -883
- package/package.json +10 -6
- package/src/constants.ts +48 -0
- package/src/device.ts +164 -0
- package/src/models.ts +129 -0
- package/src/oauth.ts +397 -0
- package/src/payload.ts +446 -0
- package/src/stream.ts +299 -0
package/README.md
CHANGED
|
@@ -2,27 +2,41 @@
|
|
|
2
2
|
[](https://www.npmjs.com/package/pi-provider-kimi-code)
|
|
3
3
|
[](./LICENSE)
|
|
4
4
|
|
|
5
|
-
**
|
|
5
|
+
**Use Kimi Code in Pi without losing the Kimi-specific bits.**
|
|
6
6
|
|
|
7
|
-
|
|
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
|
|
12
|
-
- You
|
|
13
|
-
- You
|
|
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`
|
|
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
|
-
##
|
|
30
|
+
## What this package adds
|
|
18
31
|
|
|
19
|
-
- **
|
|
20
|
-
-
|
|
21
|
-
- **
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
- **
|
|
25
|
-
- **
|
|
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
|
-
##
|
|
67
|
+
## Sign in
|
|
54
68
|
|
|
55
|
-
### Browser login
|
|
69
|
+
### Browser login
|
|
56
70
|
|
|
57
|
-
Inside
|
|
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
|
-
##
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
103
|
+
- Context window: `262144` tokens
|
|
104
|
+
- Max output: `32000` tokens
|
|
105
|
+
- Input: text and image
|
|
106
|
+
- Reasoning: enabled
|
|
78
107
|
|
|
79
|
-
|
|
108
|
+
Kimi K2.6 supports Pi's reasoning levels. This provider maps them to Kimi's current reasoning efforts:
|
|
80
109
|
|
|
81
|
-
|
|
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
|
-
##
|
|
123
|
+
## Common knobs
|
|
88
124
|
|
|
89
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
132
|
+
## Notes
|
|
103
133
|
|
|
104
|
-
|
|
134
|
+
### Kimi CLI vs Pi
|
|
105
135
|
|
|
106
|
-
|
|
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
|
-
|
|
138
|
+
### Cache behavior
|
|
109
139
|
|
|
110
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
144
|
+
### Protocol modes
|
|
115
145
|
|
|
116
|
-
|
|
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
|
-
|
|
153
|
+
Use Anthropic mode if your Pi setup depends on Anthropic-style request or tool semantics.
|
|
119
154
|
|
|
120
|
-
|
|
155
|
+
### Why OpenAI-compatible mode is the default
|
|
121
156
|
|
|
122
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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://`.
|
|
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
|
-
|
|
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
|
-
-
|
|
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
|
|
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
|
|