pi-provider-kimi-code 0.6.7 → 0.6.9
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 +46 -109
- package/index.ts +231 -87
- package/package.json +5 -5
- package/src/constants.ts +6 -2
- package/src/device.ts +59 -17
- package/src/models.ts +147 -62
- package/src/oauth.ts +232 -68
- package/src/payload.ts +154 -40
- package/src/stream.ts +32 -4
- package/src/usage.ts +4 -42
package/README.md
CHANGED
|
@@ -3,40 +3,35 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/pi-provider-kimi-code)
|
|
4
4
|
[](./LICENSE)
|
|
5
5
|
|
|
6
|
-
**
|
|
6
|
+
> **Kimi Code harness extension for Pi.** Pi v0.82.0 ships a built-in `kimi-coding` provider that handles the LLM layer — K3, adaptive thinking, and a generated model catalog — plus native OAuth sign-in via `/login`. This extension brings the rest of the Kimi Code surface into Pi: shared login state with the official `kimi-code` CLI, Kimi Files API uploads, and Kimi-native tools such as `moonshot_search`, `moonshot_fetch`, and `kimi_datasource`.
|
|
7
7
|
|
|
8
|
-
Pi
|
|
8
|
+
Pi's built-in `kimi-coding` provider handles the LLM. This extension covers the rest: shared login state with the official CLI, file uploads through Kimi's Files API, and Kimi-native tools. It also keeps a legacy provider registration so existing model sessions still load. New users should use Pi's built-in model provider.
|
|
9
9
|
|
|
10
|
-
> **
|
|
10
|
+
> **Live model catalog (legacy).** For backward compatibility, the extension still reads Kimi's live `/v1/models` catalog when it has credentials. New Pi users should rely on the built-in provider's generated catalog instead.
|
|
11
11
|
|
|
12
12
|
## Why this exists
|
|
13
13
|
|
|
14
|
-
Pi is a small harness you adapt to your own workflow. Kimi Code is Moonshot's official coding agent.
|
|
14
|
+
Pi is a small harness you adapt to your own workflow. Kimi Code is Moonshot's official coding agent. Pi now ships a built-in `kimi-coding` provider that covers the LLM, including OAuth login. This extension is for the parts of Kimi Code that Pi's built-in provider does not cover: the account session, file uploads, and Kimi's own tools/datasource APIs.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
Use the built-in provider if you only want to chat with Kimi models. Install this extension when you also want the full Kimi Code surface inside Pi:
|
|
17
17
|
|
|
18
|
-
- **
|
|
18
|
+
- **Share a session with the official `kimi-code` CLI.** Reuse an existing `kimi-code` login, or sync Pi's OAuth login back to `kimi-code` so both CLIs stay authenticated.
|
|
19
19
|
- **Send files the Kimi way.** Large inline images go through Kimi's Files API and become `ms://` references instead of huge base64 payloads.
|
|
20
|
-
- **
|
|
20
|
+
- **Use Kimi-native tools.** `moonshot_search`, `moonshot_fetch`, and `kimi_datasource` are opt-in tools that call Moonshot's server-side services.
|
|
21
21
|
- **Keep Pi's tools working.** Moonshot's API rejects tool schemas over 15 KB — a limit that Pi's extension ecosystem regularly hits ([#16](https://github.com/Leechael/pi-provider-kimi-code/issues/16), [#21](https://github.com/Leechael/pi-provider-kimi-code/issues/21)). This extension automatically deduplicates schemas with `$ref`/`$defs` before sending, so subagents and other extensions don't break.
|
|
22
|
-
- **
|
|
23
|
-
- **Turn on Kimi-native tools when you want them.** `moonshot_search`, `moonshot_fetch`, and `kimi_datasource` are opt-in, configurable per user or per project.
|
|
24
|
-
- **Embed in your own build.** `KimiCode()` factory lets you ship Kimi Code support inside a custom Pi agent with programmatic config overrides — no file-based extension path needed.
|
|
22
|
+
- **Embed in your own build.** The `KimiCode()` factory lets you ship Kimi Code support inside a custom Pi agent with programmatic config overrides — no file-based extension path needed.
|
|
25
23
|
|
|
26
24
|
## What this package adds
|
|
27
25
|
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
- Optional `moonshot_search`, `moonshot_fetch`, and unified datasource `kimi_datasource` tools via `/kimi-settings`.
|
|
38
|
-
- Programmatic `KimiCode()` factory for embedding in custom Pi builds.
|
|
39
|
-
- No build step; Pi loads the TypeScript extension directly.
|
|
26
|
+
- **Credential reuse with `kimi-code`.** Reads `~/.kimi-code/credentials/kimi-code.json` and syncs refreshed OAuth tokens back to it.
|
|
27
|
+
- **Kimi account login in Pi.** `/login kimi-coding` stores credentials in `~/.pi/agent/auth.json`; tokens refresh automatically.
|
|
28
|
+
- **`KIMI_API_KEY` fallback.** For CI or key-based access.
|
|
29
|
+
- **Kimi Files API uploads.** Uploads large inline images and references them as `ms://` instead of inline base64.
|
|
30
|
+
- **Optional Kimi-native tools.** `moonshot_search`, `moonshot_fetch`, and `kimi_datasource` via `/kimi-settings`.
|
|
31
|
+
- **Tool schema dedup.** Collapses repeated `$ref`/`$defs` structures to stay under Moonshot's 15 KB per-tool limit.
|
|
32
|
+
- **Legacy provider registration.** Keeps existing model sessions loading; new users should use Pi's built-in provider.
|
|
33
|
+
- **Programmatic `KimiCode()` factory.** Embed Kimi Code in a custom Pi build with config overrides.
|
|
34
|
+
- **No build step.** Pi loads the TypeScript extension directly.
|
|
40
35
|
|
|
41
36
|
## Install
|
|
42
37
|
|
|
@@ -76,57 +71,32 @@ main(process.argv.slice(2), {
|
|
|
76
71
|
|
|
77
72
|
`KimiCode()` with no arguments behaves identically to the file-based extension. Pass a `KimiCodeConfigPatch` to override defaults (protocol, upload threshold, tools, model parameters). See [docs/programmatic-usage.md](docs/programmatic-usage.md) for the full API.
|
|
78
73
|
|
|
79
|
-
##
|
|
74
|
+
## Authentication
|
|
80
75
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
```text
|
|
84
|
-
/login kimi-coding
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
A browser tab opens, you sign into your Kimi account, and Pi stores the credential at `~/.pi/agent/auth.json`. Tokens refresh automatically. The extension also syncs refreshed credentials to the current `kimi-code` credential file:
|
|
88
|
-
|
|
89
|
-
```text
|
|
90
|
-
~/.kimi-code/credentials/kimi-code.json
|
|
91
|
-
```
|
|
76
|
+
Pi's built-in `kimi-coding` provider already supports OAuth login via `/login`. This extension handles the credential layer around that login:
|
|
92
77
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
```text
|
|
98
|
-
~/.kimi/credentials/kimi-code.json
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Legacy credentials are read-only. Set `KIMI_SHARE_DIR` to override the legacy `~/.kimi` directory.
|
|
102
|
-
|
|
103
|
-
For CI or pay-per-token use, set `KIMI_API_KEY` instead:
|
|
78
|
+
- **Syncs refreshed tokens back to `kimi-code`.** Pi stores credentials at `~/.pi/agent/auth.json`; this extension keeps the official CLI's credential file (`~/.kimi-code/credentials/kimi-code.json`) up to date so both stay authenticated.
|
|
79
|
+
- **Reuses existing `kimi-code` sessions.** If you already use the official CLI, the extension reads its session directly. Set `KIMI_CODE_HOME` if its home directory lives somewhere else.
|
|
80
|
+
- **Supports the legacy `~/.kimi` path.** Read-only. Set `KIMI_SHARE_DIR` to override it.
|
|
81
|
+
- **`KIMI_API_KEY` fallback.** For CI or key-based access:
|
|
104
82
|
|
|
105
83
|
```bash
|
|
106
84
|
KIMI_API_KEY=sk-... pi
|
|
107
85
|
```
|
|
108
86
|
|
|
109
|
-
## Models
|
|
110
|
-
|
|
111
|
-
This provider publishes three Pi model IDs:
|
|
112
|
-
|
|
113
|
-
| Pi model ID | Upstream model | Access |
|
|
114
|
-
| --------------------------------------- | ------------------------ | ------------------------------------------------------ |
|
|
115
|
-
| `kimi-coding/kimi-for-coding` | Kimi K2.7 Code | All Kimi Code members |
|
|
116
|
-
| `kimi-coding/kimi-for-coding-highspeed` | Kimi K2.7 Code HighSpeed | Allegretto and above |
|
|
117
|
-
| `kimi-coding/k3` | Kimi K3 | Moderato: 256K context; Allegretto and above: up to 1M |
|
|
87
|
+
## Models (legacy provider registration)
|
|
118
88
|
|
|
119
|
-
|
|
89
|
+
> **New users:** Use Pi's built-in `kimi-coding` provider for model discovery, reasoning controls, and OAuth login. This extension keeps its own model registration only for backward compatibility with existing sessions.
|
|
120
90
|
|
|
121
|
-
|
|
91
|
+
When this extension does register models, the official catalog determines what it publishes. The following IDs are the fallback set when catalog discovery is unavailable:
|
|
122
92
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
93
|
+
| Pi model ID | Default name | Availability and context |
|
|
94
|
+
| --------------------------------------- | ------------------------ | ------------------------ |
|
|
95
|
+
| `kimi-coding/kimi-for-coding` | Kimi K2.7 Code | Official catalog |
|
|
96
|
+
| `kimi-coding/kimi-for-coding-highspeed` | Kimi K2.7 Code HighSpeed | Official catalog |
|
|
97
|
+
| `kimi-coding/k3` | Kimi K3 | Official catalog |
|
|
128
98
|
|
|
129
|
-
Kimi
|
|
99
|
+
Kimi's official catalog at `https://api.kimi.com/coding/v1/models` is authoritative for model availability and context windows. When discovery is unavailable, the extension uses the fallback models and defaults below.
|
|
130
100
|
|
|
131
101
|
Fallback values:
|
|
132
102
|
|
|
@@ -135,13 +105,13 @@ Fallback values:
|
|
|
135
105
|
- Input: text and image
|
|
136
106
|
- Reasoning: enabled
|
|
137
107
|
|
|
138
|
-
The provider maps Pi's reasoning levels to Kimi's top-level `thinking` parameter.
|
|
108
|
+
The provider maps Pi's reasoning levels to Kimi's top-level `thinking` parameter. It sends `thinking.effort` only when the official catalog advertises the mapped value. The mapping refreshes automatically on credential refresh. Opening `/kimi-settings` also re-discovers the latest model metadata.
|
|
139
109
|
|
|
140
110
|
Switching models or thinking effort invalidates Kimi's existing context cache. Start a new session when switching to avoid re-prefilling a long conversation and consuming extra quota.
|
|
141
111
|
|
|
142
112
|
## Optional tools
|
|
143
113
|
|
|
144
|
-
Kimi
|
|
114
|
+
Kimi Code has server-side capabilities that this extension exposes as opt-in tools. All tools are off by default. Enable them individually through `/kimi-settings` or JSON config.
|
|
145
115
|
|
|
146
116
|
Inside Pi, run:
|
|
147
117
|
|
|
@@ -213,8 +183,8 @@ If you already use another search or fetch tool, pick one path for a session. Ov
|
|
|
213
183
|
|
|
214
184
|
Most users do not need environment variables. Two are worth knowing:
|
|
215
185
|
|
|
216
|
-
- `KIMI_API_KEY` — static API key for CI or
|
|
217
|
-
- `KIMI_CODE_PROTOCOL` — `openai` by default; set to `anthropic`
|
|
186
|
+
- `KIMI_API_KEY` — static API key for CI or key-based access.
|
|
187
|
+
- `KIMI_CODE_PROTOCOL` — `openai` by default; set to `anthropic` for Anthropic-compatible requests.
|
|
218
188
|
|
|
219
189
|
Tools, protocol, and upload threshold are all configurable through `/kimi-settings` or JSON config files.
|
|
220
190
|
|
|
@@ -224,30 +194,15 @@ The full env list, including base URL overrides, `kimi-code` path overrides, upl
|
|
|
224
194
|
|
|
225
195
|
### Kimi Code vs Pi
|
|
226
196
|
|
|
227
|
-
`kimi-code` is Moonshot's official terminal agent. Pi is the harness you adapt to your own workflow
|
|
197
|
+
`kimi-code` is Moonshot's official terminal agent. Pi is the harness you adapt to your own workflow. Pi's built-in `kimi-coding` provider handles the LLM; this extension adds the rest of the Kimi Code surface to Pi.
|
|
228
198
|
|
|
229
199
|
### Cache behavior
|
|
230
200
|
|
|
231
|
-
Kimi's cache is content-based
|
|
232
|
-
|
|
233
|
-
See [docs/caching.md](docs/caching.md) for the measured behavior: TTL, cross-protocol cache reuse, 256-token prefix alignment, device-id sharing, and the cases that invalidate cache.
|
|
201
|
+
Kimi's cache is content-based and keyed purely by prompt prefix. `prompt_cache_key`, Anthropic `cache_control` markers, and device-id headers are ignored. Cache benefits accrue within a session, not across cold starts. See [docs/caching.md](docs/caching.md) for measured behavior.
|
|
234
202
|
|
|
235
203
|
### Protocol modes
|
|
236
204
|
|
|
237
|
-
Kimi's coding endpoint speaks both Anthropic and OpenAI dialects. This extension defaults to OpenAI-compatible mode.
|
|
238
|
-
|
|
239
|
-
`KIMI_CODE_PROTOCOL` supports:
|
|
240
|
-
|
|
241
|
-
- `openai` — default, uses `/coding/v1/chat/completions`
|
|
242
|
-
- `anthropic` — uses `/coding/v1/messages`
|
|
243
|
-
|
|
244
|
-
Use Anthropic mode if your Pi setup depends on Anthropic-style request or tool semantics.
|
|
245
|
-
|
|
246
|
-
### Why OpenAI-compatible mode is the default
|
|
247
|
-
|
|
248
|
-
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.
|
|
249
|
-
|
|
250
|
-
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.
|
|
205
|
+
Kimi's coding endpoint speaks both Anthropic and OpenAI dialects. This extension defaults to OpenAI-compatible mode (`/coding/v1/chat/completions`) because it uses a dedicated `role: "tool"` message, which keeps tool results separate from user messages in multi-turn loops. Anthropic mode (`/coding/v1/messages`) is available if your setup depends on Anthropic-style semantics. See [issue #5](https://github.com/Leechael/pi-provider-kimi-code/issues/5) for details.
|
|
251
206
|
|
|
252
207
|
### Is this affiliated with Moonshot AI?
|
|
253
208
|
|
|
@@ -257,25 +212,19 @@ No. This is an independent extension. The login flow is derived from the public
|
|
|
257
212
|
|
|
258
213
|
### `pi` reports `fetch failed` even though `curl` works
|
|
259
214
|
|
|
260
|
-
Pi
|
|
215
|
+
Pi uses Node's `fetch` / undici stack, which handles `http_proxy` / `https_proxy` / `all_proxy` differently from `curl`. Check the proxy variables in the Pi process's environment. `scripts/test_e2e.sh` prints the effective proxy settings for debugging.
|
|
261
216
|
|
|
262
217
|
### `/login kimi-coding` prints a device code but the browser never opens
|
|
263
218
|
|
|
264
|
-
The login flow always prints a verification URL.
|
|
219
|
+
The login flow always prints a verification URL. Copy it into a browser manually if your terminal or OS blocks auto-open.
|
|
265
220
|
|
|
266
221
|
### "Access denied" or subscription errors after a successful login
|
|
267
222
|
|
|
268
|
-
Your Moonshot account needs an active Kimi Code Plan
|
|
269
|
-
|
|
270
|
-
### Tools do not show up
|
|
271
|
-
|
|
272
|
-
Run `/kimi-settings` and check whether the tool is enabled in the home or project config. All tools also require `/login kimi-coding`; `KIMI_API_KEY` is not enough.
|
|
273
|
-
|
|
274
|
-
If you changed the JSON config by hand, reload it through `/kimi-settings` or start a new Pi session.
|
|
223
|
+
Your Moonshot account needs an active Kimi Code Plan. If the same account works in `kimi-code`, re-run `/login kimi-coding` to refresh credentials.
|
|
275
224
|
|
|
276
|
-
### Tools
|
|
225
|
+
### Tools do not show up or return errors
|
|
277
226
|
|
|
278
|
-
|
|
227
|
+
Run `/kimi-settings` and check whether the tool is enabled in the home or project config. All tools require OAuth login; `KIMI_API_KEY` is not enough. Subscription or whitelist errors come from the upstream Moonshot service.
|
|
279
228
|
|
|
280
229
|
### Large images fail with a payload error
|
|
281
230
|
|
|
@@ -283,19 +232,11 @@ This extension uploads images over `KIMI_CODE_UPLOAD_THRESHOLD_BYTES` (default 1
|
|
|
283
232
|
|
|
284
233
|
### Prompt cache never seems to hit
|
|
285
234
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
- 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.
|
|
289
|
-
- An `image_url` reference (`ms://<file_id>`) changed between turns. Same rule — see Finding 17.
|
|
290
|
-
- 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.
|
|
291
|
-
- A timestamp, request ID, or randomized header is being interpolated into the prompt.
|
|
292
|
-
- The first ~256 tokens of the prompt differ between turns.
|
|
293
|
-
|
|
294
|
-
`PI_CACHE_RETENTION=none` skips `prompt_cache_key` injection but **does not** disable Kimi's caching (the cache is unconditional). For deterministic measurement, run a focused suite under `scripts/e2e/cache/`, starting with `scripts/e2e/cache/ttl.sh`. See [docs/caching.md](docs/caching.md) for full mechanics.
|
|
235
|
+
Cache benefits accrue within a single session. If `cache_read_input_tokens` (Anthropic) or `cached_tokens` (OpenAI) stays at `0`, something in the prompt is varying between turns — a changed system prompt, tool set, `ms://` reference, or timestamp. See [docs/caching.md](docs/caching.md) for the full list of invalidation causes and measurement scripts.
|
|
295
236
|
|
|
296
237
|
### OpenAI-compatible tools complain about a `developer` role
|
|
297
238
|
|
|
298
|
-
In OpenAI mode this extension maps
|
|
239
|
+
In OpenAI mode this extension maps `developer` to `system` because Kimi's Coding endpoint does not recognize `developer`. Use Anthropic mode if your toolchain expects `developer` to round-trip.
|
|
299
240
|
|
|
300
241
|
## References
|
|
301
242
|
|
|
@@ -306,10 +247,6 @@ In OpenAI mode this extension maps the `developer` role to `system` (Kimi's Codi
|
|
|
306
247
|
- Cache behavior: [docs/caching.md](docs/caching.md)
|
|
307
248
|
- Architecture notes: [docs/architecture.md](docs/architecture.md)
|
|
308
249
|
|
|
309
|
-
## Credits
|
|
310
|
-
|
|
311
|
-
Based on the login implementation from Kimi Code by Moonshot AI. Pi was originally created by [@badlogic](https://github.com/badlogic).
|
|
312
|
-
|
|
313
250
|
## License
|
|
314
251
|
|
|
315
252
|
MIT
|