pi-provider-kimi-code 0.5.1 → 0.6.1

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
@@ -5,28 +5,37 @@
5
5
 
6
6
  **Use Kimi Code in Pi, with the Kimi parts handled.**
7
7
 
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
+ 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, tool schema compatibility, live model metadata, measured cache behavior, and API-tested parameter handling.
9
+
10
+ > **Kimi K2.7 supported.** The provider automatically discovers the current model from Kimi's `/v1/models` endpoint at login, refresh, and `/kimi-settings` time, so new rollouts (including K2.7) are picked up immediately without a package update.
9
11
 
10
12
  ## Why this exists
11
13
 
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
+ Pi is a small harness you adapt to your own workflow. Kimi Code is Moonshot's official coding agent. This package sits between them: Kimi Code as a Pi provider, plus the Kimi-specific details that the basic provider keeps simple.
15
+
16
+ Kimi's API surface goes beyond the LLM itself. It includes file uploads, web search, page fetch, and a growing set of datasource APIs. This extension wires those capabilities into Pi so you can use them alongside the community's existing extensions — MCP servers, custom themes, skills, and any other Pi add-on — without leaving the harness.
13
17
 
14
- - **Use your Kimi account.** Log in with `/login kimi-coding`, or reuse an existing `kimi-cli` session.
18
+ - **Use your Kimi account.** Log in with `/login kimi-coding`, or reuse an existing `kimi-code` session.
15
19
  - **Send files the Kimi way.** Large inline images go through Kimi's Files API and become `ms://` references instead of huge base64 payloads.
16
20
  - **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.
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
+ - **Tested against the live API.** Thinking config, parameter constraints, protocol compatibility, and streaming behavior are all verified against Kimi's Coding endpoint — not assumed from docs. When the API rejects something (wrong `temperature`, unsupported `tool_choice`), the provider strips it before you see a 400.
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.
19
25
 
20
26
  ## What this package adds
21
27
 
22
28
  - 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`.
29
+ - `kimi-code` credential reuse from `~/.kimi/credentials/kimi-code.json`.
24
30
  - Kimi Files API uploads for large inline images.
25
- - Live model metadata from Kimi at login / refresh time.
31
+ - Live model metadata from Kimi at login, refresh, and `/kimi-settings` time.
26
32
  - OpenAI-compatible mode by default, Anthropic-compatible mode on request.
27
- - Kimi K2.6 reasoning level mapping for Pi's reasoning controls.
33
+ - Kimi K2.7 reasoning level mapping for Pi's reasoning controls.
34
+ - Tool schema dedup to stay under Moonshot's 15 KB per-tool-schema limit.
35
+ - K2.7 parameter guard: strips `temperature`, `top_p`, and `tool_choice` values that the API rejects.
28
36
  - Stream cleanup for Kimi's thinking-only placeholder text.
29
- - Optional `moonshot_search` and `moonshot_fetch` tools via `/kimi-settings`.
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.
30
39
  - No build step; Pi loads the TypeScript extension directly.
31
40
 
32
41
  ## Install
@@ -52,6 +61,21 @@ curl -L https://github.com/Leechael/pi-provider-kimi-code/releases/latest/downlo
52
61
  pi install /tmp/pi-provider-kimi-code
53
62
  ```
54
63
 
64
+ ### Programmatic usage
65
+
66
+ Use `KimiCode()` to embed Kimi Code as a provider inside a custom Pi build:
67
+
68
+ ```typescript
69
+ import { main } from "@earendil-works/pi-coding-agent";
70
+ import { KimiCode } from "pi-provider-kimi-code";
71
+
72
+ main(process.argv.slice(2), {
73
+ extensionFactories: [KimiCode({ protocol: "anthropic" })],
74
+ });
75
+ ```
76
+
77
+ `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
+
55
79
  ## Sign in
56
80
 
57
81
  Inside Pi, run:
@@ -62,13 +86,13 @@ Inside Pi, run:
62
86
 
63
87
  A browser tab opens, you sign into your Kimi account, and Pi stores the credential at `~/.pi/agent/auth.json`. Tokens refresh automatically.
64
88
 
65
- If you already use `kimi-cli`, this extension can reuse its credential file:
89
+ If you already use `kimi-code`, this extension can reuse its credential file:
66
90
 
67
91
  ```text
68
92
  ~/.kimi/credentials/kimi-code.json
69
93
  ```
70
94
 
71
- The file is read-only from this extension's side. Set `KIMI_SHARE_DIR` if your `kimi-cli` share directory lives somewhere else.
95
+ The file is read-only from this extension's side. Set `KIMI_SHARE_DIR` if your `kimi-code` share directory lives somewhere else.
72
96
 
73
97
  For CI or pay-per-token use, set `KIMI_API_KEY` instead:
74
98
 
@@ -90,32 +114,20 @@ Select it inside Pi:
90
114
  /model kimi-coding/kimi-for-coding
91
115
  ```
92
116
 
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.
117
+ 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 (e.g., Kimi K2.7) or internal test, Pi can pick up the latest model name and context size without waiting for a package release.
94
118
 
95
119
  Fallback values:
96
120
 
97
- - Context window: `262144` tokens
98
- - Max output: `32000` tokens
121
+ - Context window: `256k` tokens
122
+ - Max output: `32k` tokens
99
123
  - Input: text and image
100
124
  - Reasoning: enabled
101
125
 
102
- Kimi K2.6 supports Pi's reasoning levels. This provider maps them to Kimi's current reasoning efforts:
103
-
104
- - `off` / `none` — reasoning disabled
105
- - `minimal` / `low` — low effort
106
- - `medium` — medium effort
107
- - `high` / `xhigh` — high effort
108
-
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).
110
-
111
- ## Optional Moonshot tools
126
+ The provider maps Pi's reasoning levels to Kimi's `reasoning_effort` and top-level `thinking` parameters. The mapping refreshes automatically on credential refresh. Opening `/kimi-settings` also re-discovers the latest model metadata.
112
127
 
113
- Kimi Coding has server-side search and fetch tools. This extension can expose them to Pi as:
128
+ ## Optional tools
114
129
 
115
- - `moonshot_search`
116
- - `moonshot_fetch`
117
-
118
- They are off by default. Turn them on when you want Kimi's own search/fetch path in the agent loop.
130
+ Kimi Coding has server-side capabilities that this extension can expose as opt-in tools. All tools are off by default. Enable them individually through `/kimi-settings` or JSON config.
119
131
 
120
132
  Inside Pi, run:
121
133
 
@@ -123,25 +135,61 @@ Inside Pi, run:
123
135
  /kimi-settings
124
136
  ```
125
137
 
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.
138
+ That command shows the current server-side model name (e.g. "K2.7 Code High Speed"), your Kimi usage summary, and lets you edit the home or project config. Changes apply to the active session tool set.
139
+
140
+ Configurable settings include protocol mode, upload threshold, and per-tool enable/collapse.
127
141
 
128
142
  Config files are JSON:
129
143
 
130
144
  - Home: `~/.pi/pi-provider-kimi-code.json`
131
145
  - Project override: `<cwd>/.pi/pi-provider-kimi-code.json`
132
146
 
133
- Project config overrides home config with a deep merge. Missing files or missing keys mean both tools stay off.
147
+ Project config overrides home config with a deep merge. Missing files or missing keys mean all tools stay off.
148
+
149
+ ### Available tools
150
+
151
+ | Tool | Description |
152
+ | ----------------- | ---------------------------------------------- |
153
+ | `moonshot_search` | Web search through Kimi's Moonshot service |
154
+ | `moonshot_fetch` | Web page fetch through Kimi's Moonshot service |
155
+ | `kimi_datasource` | Unified Kimi datasource tool |
156
+
157
+ ### Example config
134
158
 
135
159
  ```json
136
160
  {
161
+ "protocol": "openai",
162
+ "uploads": { "thresholdBytes": 1048576 },
137
163
  "tools": {
138
164
  "moonshot_search": { "enabled": true, "default_collapsed": true },
139
- "moonshot_fetch": { "enabled": true, "default_collapsed": true }
165
+ "moonshot_fetch": { "enabled": true, "default_collapsed": true },
166
+ "kimi_datasource": { "enabled": true, "default_collapsed": true }
140
167
  }
141
168
  }
142
169
  ```
143
170
 
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.
171
+ ### Datasource tool
172
+
173
+ `kimi_datasource` calls Kimi's `/tools` endpoint. It accepts `data_source_name` plus optional `api_name` and `params`:
174
+
175
+ - Without `api_name`: returns the datasource description (available APIs).
176
+ - With `api_name` and `params`: calls the specified API.
177
+
178
+ Supported datasource names:
179
+
180
+ | Datasource | Description |
181
+ | ---------------------- | ------------------------------------- |
182
+ | `stock_finance_data` | Chinese stock and financial data |
183
+ | `yahoo_finance` | Yahoo Finance market data |
184
+ | `world_bank_open_data` | World Bank open statistics |
185
+ | `tianyancha` | Chinese enterprise and corporate data |
186
+ | `arxiv` | Academic paper search and metadata |
187
+ | `scholar` | Academic literature search |
188
+ | `yuandian_law` | Chinese legal database and case law |
189
+
190
+ ### Common notes
191
+
192
+ All 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 services; if not, the upstream service can return subscription or whitelist errors.
145
193
 
146
194
  `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
195
 
@@ -152,17 +200,17 @@ If you already use another search or fetch tool, pick one path for a session. Ov
152
200
  Most users do not need environment variables. Two are worth knowing:
153
201
 
154
202
  - `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.
203
+ - `KIMI_CODE_PROTOCOL` — `openai` by default; set to `anthropic` if your Pi setup needs Anthropic-compatible requests. Can also be set through `/kimi-settings` or JSON config.
156
204
 
157
- Moonshot search/fetch tools are configured through `/kimi-settings` or JSON config files, not env vars.
205
+ Tools, protocol, and upload threshold are all configurable through `/kimi-settings` or JSON config files.
158
206
 
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).
207
+ The full env list, including base URL overrides, `kimi-code` path overrides, upload tuning, debug logs, and model metadata overrides, lives in [docs/ENV.md](docs/ENV.md).
160
208
 
161
209
  ## Notes
162
210
 
163
- ### Kimi CLI vs Pi
211
+ ### Kimi Code vs Pi
164
212
 
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.
213
+ `kimi-code` 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
214
 
167
215
  ### Cache behavior
168
216
 
@@ -189,7 +237,7 @@ The OpenAI-compatible protocol uses a dedicated `role: "tool"` message, so the b
189
237
 
190
238
  ### Is this affiliated with Moonshot AI?
191
239
 
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.
240
+ No. This is an independent extension. The login flow is derived from the public implementation in the open-source Kimi Code repository.
193
241
 
194
242
  ## Troubleshooting
195
243
 
@@ -203,17 +251,17 @@ The login flow always prints a verification URL. If your terminal or OS blocks a
203
251
 
204
252
  ### "Access denied" or subscription errors after a successful login
205
253
 
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.
254
+ Your Moonshot account needs an active Kimi Code Plan for the provider to do anything useful. If the same account works in `kimi-code`, re-run `/login kimi-coding` to refresh credentials.
207
255
 
208
- ### Moonshot tools do not show up
256
+ ### Tools do not show up
209
257
 
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.
258
+ 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.
211
259
 
212
260
  If you changed the JSON config by hand, reload it through `/kimi-settings` or start a new Pi session.
213
261
 
214
- ### Moonshot tools return subscription or whitelist errors
262
+ ### Tools return subscription or whitelist errors
215
263
 
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.
264
+ The tools call Moonshot's server-side services. Some accounts may not have access even with an active Kimi Code Plan. The error comes from the upstream service.
217
265
 
218
266
  ### Large images fail with a payload error
219
267
 
@@ -238,15 +286,15 @@ In OpenAI mode this extension maps the `developer` role to `system` (Kimi's Codi
238
286
  ## References
239
287
 
240
288
  - Pi: [earendil-works/pi](https://github.com/earendil-works/pi)
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)
242
289
  - Environment variables: [docs/ENV.md](docs/ENV.md)
290
+ - Programmatic usage: [docs/programmatic-usage.md](docs/programmatic-usage.md)
243
291
  - Testing guide: [docs/TESTING.md](docs/TESTING.md)
244
292
  - Cache behavior: [docs/caching.md](docs/caching.md)
245
293
  - Architecture notes: [docs/architecture.md](docs/architecture.md)
246
294
 
247
295
  ## Credits
248
296
 
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).
297
+ Based on the login implementation from Kimi Code by Moonshot AI. Built as a Pi extension by [@badlogic](https://github.com/badlogic).
250
298
 
251
299
  ## License
252
300