opencode-kimi-rotator 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,488 @@
1
+ # Kimi API Key Rotator for OpenCode
2
+
3
+ [![npm version](https://img.shields.io/npm/v/opencode-kimi-rotator.svg)](https://www.npmjs.com/package/opencode-kimi-rotator)
4
+ [![npm beta](https://img.shields.io/npm/v/opencode-kimi-rotator/beta.svg?label=beta)](https://www.npmjs.com/package/opencode-kimi-rotator)
5
+ [![npm downloads](https://img.shields.io/npm/dw/opencode-kimi-rotator.svg)](https://www.npmjs.com/package/opencode-kimi-rotator)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7
+
8
+ Automatically rotate between multiple Kimi API keys to handle rate limits and distribute load across accounts.
9
+
10
+ ## What You Get
11
+
12
+ - **Multiple API Key Support** — Store and manage unlimited Kimi API keys
13
+ - **Auto-Rotation** — Automatically switch keys when rate limited
14
+ - **Health-Based Selection** — Smart rotation using health scores and LRU
15
+ - **Three Rotation Strategies**:
16
+ - `round-robin` — Cycle through keys sequentially
17
+ - `health-based` — Use health scores + freshness (default)
18
+ - `sticky` — Stay on one key until rate limited
19
+ - **CLI Management** — Easy commands to add, list, and manage keys
20
+ - **Plugin compatible** — Works alongside other OpenCode plugins (oh-my-opencode, antigravity-auth, etc.)
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ <details open>
27
+ <summary><b>For Humans</b></summary>
28
+
29
+ **Option A: Automatic (Recommended)**
30
+
31
+ ```bash
32
+ curl -fsSL https://raw.githubusercontent.com/deyndev/opencode-kimi-rotator/main/scripts/install.sh | bash
33
+ ```
34
+
35
+ Or for Windows (PowerShell):
36
+
37
+ ```powershell
38
+ irm https://raw.githubusercontent.com/deyndev/opencode-kimi-rotator/main/scripts/install.ps1 | iex
39
+ ```
40
+
41
+ **Option B: Manual setup**
42
+
43
+ 1. **Clone and build the plugin**:
44
+
45
+ ```bash
46
+ git clone https://github.com/deyndev/opencode-kimi-rotator.git
47
+ cd opencode-kimi-rotator
48
+ npm install
49
+ npm run build
50
+ npm run install:plugin
51
+ ```
52
+
53
+ 2. **Add the model to your `~/.config/opencode/opencode.json`**:
54
+
55
+ ```json
56
+ {
57
+ "provider": {
58
+ "anthropic": {
59
+ "name": "Anthropic",
60
+ "models": {
61
+ "kimi-for-coding": {
62
+ "name": "Kimi K2.5 (via Kimi API)",
63
+ "limit": {
64
+ "context": 262144,
65
+ "output": 32768
66
+ }
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ ```
73
+
74
+ 3. **Add your API keys**:
75
+
76
+ ```bash
77
+ opencode-kimi add-key sk-kimi-your-key-here "My Account 1"
78
+ opencode-kimi add-key sk-kimi-another-key "My Account 2"
79
+ ```
80
+
81
+ 4. **Use it:**
82
+
83
+ ```bash
84
+ opencode run "Hello" --model=anthropic/kimi-for-coding
85
+ ```
86
+
87
+ </details>
88
+
89
+ <details>
90
+ <summary><b>For LLM Agents</b></summary>
91
+
92
+ ### Complete Installation Steps
93
+
94
+ **1. Clone and build the plugin:**
95
+
96
+ ```bash
97
+ git clone https://github.com/deyndev/opencode-kimi-rotator.git
98
+ cd opencode-kimi-rotator
99
+ npm install
100
+ npm run install:plugin
101
+ ```
102
+
103
+ **2. Add the plugin and model to `~/.config/opencode/opencode.json`:**
104
+
105
+ Add the plugin to your existing config. The plugin path must use the full absolute path:
106
+
107
+ ```json
108
+ {
109
+ "plugin": [
110
+ "file:///Users/YOUR_USERNAME/.config/opencode/plugins/kimi-rotator.js"
111
+ ],
112
+ "provider": {
113
+ "anthropic": {
114
+ "name": "Anthropic",
115
+ "models": {
116
+ "kimi-for-coding": {
117
+ "name": "Kimi K2.5 (via Kimi API)",
118
+ "limit": { "context": 262144, "output": 32768 }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ > **Important**: Replace `YOUR_USERNAME` with your actual username. Example paths:
127
+ > - macOS: `file:///Users/john/.config/opencode/plugins/kimi-rotator.js`
128
+ > - Linux: `file:///home/john/.config/opencode/plugins/kimi-rotator.js`
129
+
130
+ **3. Configure oh-my-opencode agents in `~/.config/opencode/oh-my-opencode.json`:**
131
+
132
+ You must configure **ALL agents** you want to use with Kimi:
133
+
134
+ ```json
135
+ {
136
+ "google_auth": false,
137
+ "ralph_loop": {
138
+ "enabled": true,
139
+ "default_max_iterations": 100
140
+ },
141
+ "agents": {
142
+ "sisyphus": { "model": "anthropic/kimi-for-coding" },
143
+ "prometheus": { "model": "anthropic/kimi-for-coding" },
144
+ "oracle": { "model": "anthropic/kimi-for-coding" },
145
+ "librarian": { "model": "anthropic/kimi-for-coding" },
146
+ "explore": { "model": "anthropic/kimi-for-coding" },
147
+ "frontend-ui-ux-engineer": { "model": "anthropic/kimi-for-coding" },
148
+ "document-writer": { "model": "anthropic/kimi-for-coding" },
149
+ "multimodal-looker": { "model": "anthropic/kimi-for-coding" },
150
+ "atlas": { "model": "anthropic/kimi-for-coding" }
151
+ }
152
+ }
153
+ ```
154
+
155
+ > **Note**: If you get "model is not valid" errors, make sure the agent is configured in oh-my-opencode.json.
156
+
157
+ **4. Add your Kimi API keys:**
158
+
159
+ ```bash
160
+ opencode-kimi add-key sk-kimi-your-key-here "My Account"
161
+ ```
162
+
163
+ **5. Verify installation:**
164
+
165
+ ```bash
166
+ opencode-kimi list-keys
167
+ ```
168
+
169
+ ### What the plugin does automatically:
170
+ - Sets `ANTHROPIC_BASE_URL` to Kimi's API endpoint
171
+ - Intercepts fetch requests to `api.kimi.com`
172
+ - Rotates API keys based on health scores
173
+ - Shows toast notifications for key rotation
174
+
175
+ </details>
176
+
177
+
178
+ ---
179
+
180
+ ## Models
181
+
182
+ ### Available Model
183
+
184
+ The Kimi Coding API provides a single flagship model:
185
+
186
+ | Model ID | Name | Context | Output |
187
+ |----------|------|---------|--------|
188
+ | `kimi-for-coding` | Kimi K2.5 | 262,144 | 32,768 |
189
+
190
+ Use it as: `anthropic/kimi-for-coding`
191
+
192
+
193
+ <details>
194
+ <summary><b>Full configuration (copy-paste ready)</b></summary>
195
+
196
+ Add this to your `~/.config/opencode/opencode.json`:
197
+
198
+ ```json
199
+ {
200
+ "$schema": "https://opencode.ai/config.json",
201
+ "plugin": [
202
+ "file:///Users/YOUR_USERNAME/.config/opencode/plugins/kimi-rotator.js"
203
+ ],
204
+ "provider": {
205
+ "anthropic": {
206
+ "name": "Anthropic",
207
+ "models": {
208
+ "kimi-for-coding": {
209
+ "name": "Kimi K2.5 (via Kimi API)",
210
+ "limit": {
211
+ "context": 262144,
212
+ "output": 32768
213
+ },
214
+ "modalities": {
215
+ "input": ["text", "image", "video"],
216
+ "output": ["text"]
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ ```
224
+
225
+ </details>
226
+
227
+ ---
228
+
229
+ ## Usage
230
+
231
+ ### Add API Keys
232
+
233
+ ```bash
234
+ # Add a key with optional name
235
+ opencode-kimi add-key sk-kimi-your-key-here "My Account 1"
236
+ opencode-kimi add-key sk-kimi-another-key "My Account 2"
237
+ ```
238
+
239
+ ### List All Keys
240
+
241
+ ```bash
242
+ opencode-kimi list-keys
243
+ ```
244
+
245
+ Output:
246
+ ```
247
+ Kimi API Keys (2 total, strategy: health-based):
248
+
249
+ ● [0] My Account 1
250
+ Key: sk-kim...yDigS
251
+ Health: ██████████ 100%
252
+ Requests: 45/50 successful
253
+ Status: ✓ Active
254
+
255
+ ○ [1] My Account 2
256
+ Key: sk-kim...xxxxx
257
+ Health: ████████░░ 80%
258
+ Requests: 20/25 successful
259
+ Status: ✓ Active
260
+ ```
261
+
262
+ ### Remove a Key
263
+
264
+ ```bash
265
+ opencode-kimi remove-key 1
266
+ ```
267
+
268
+ ### Manually Rotate
269
+
270
+ ```bash
271
+ opencode-kimi rotate
272
+ ```
273
+
274
+ ### Change Rotation Strategy
275
+
276
+ ```bash
277
+ # Round-robin: cycle through keys sequentially
278
+ opencode-kimi set-strategy round-robin
279
+
280
+ # Health-based: smart selection based on health scores (default)
281
+ opencode-kimi set-strategy health-based
282
+
283
+ # Sticky: stay on one key until rate limited
284
+ opencode-kimi set-strategy sticky
285
+ ```
286
+
287
+ ---
288
+
289
+ ## How It Works
290
+
291
+ ### Health Score System
292
+
293
+ Each API key has a health score (0-100):
294
+ - **Initial**: 100
295
+ - **Success**: +2 points
296
+ - **Rate Limited**: -15 points
297
+ - **Failure**: -20 points
298
+
299
+ Keys with health score < 30 are temporarily skipped.
300
+
301
+ ### Rate Limit Handling
302
+
303
+ When a key is rate limited:
304
+ 1. Health score decreases
305
+ 2. Key is marked as rate limited with reset time
306
+ 3. Next request automatically uses next available key
307
+ 4. Once reset time passes, key becomes available again
308
+
309
+ ### Data Storage
310
+
311
+ Keys are stored in `~/.config/opencode/kimi-accounts.json`:
312
+ ```json
313
+ {
314
+ "version": 1,
315
+ "accounts": [
316
+ {
317
+ "key": "sk-kimi-xxx",
318
+ "name": "My Account",
319
+ "addedAt": 1234567890,
320
+ "lastUsed": 1234567890,
321
+ "rateLimitResetTime": 0,
322
+ "healthScore": 100,
323
+ "consecutiveFailures": 0,
324
+ "totalRequests": 50,
325
+ "successfulRequests": 45
326
+ }
327
+ ],
328
+ "activeIndex": 0,
329
+ "rotationStrategy": "health-based"
330
+ }
331
+ ```
332
+
333
+ ---
334
+
335
+ ## Configuration
336
+
337
+ ### Configuration Path (All Platforms)
338
+
339
+ OpenCode uses `~/.config/opencode/` on **all platforms** including Windows.
340
+
341
+ | File | Path |
342
+ |------|------|
343
+ | Main config | `~/.config/opencode/opencode.json` |
344
+ | Accounts | `~/.config/opencode/kimi-accounts.json` |
345
+
346
+ > **Windows users**: `~` resolves to your user home directory (e.g., `C:\Users\YourName`). Do NOT use `%APPDATA%`.
347
+
348
+ ### In Oh My OpenCode
349
+
350
+ To use Kimi models with Oh My OpenCode agents, update `~/.config/opencode/oh-my-opencode.json`:
351
+
352
+ ```json
353
+ {
354
+ "google_auth": false,
355
+ "ralph_loop": {
356
+ "enabled": true,
357
+ "default_max_iterations": 100
358
+ },
359
+ "agents": {
360
+ "sisyphus": { "model": "anthropic/kimi-for-coding" },
361
+ "prometheus": { "model": "anthropic/kimi-for-coding" },
362
+ "oracle": { "model": "anthropic/kimi-for-coding" },
363
+ "librarian": { "model": "anthropic/kimi-for-coding" },
364
+ "explore": { "model": "anthropic/kimi-for-coding" },
365
+ "frontend-ui-ux-engineer": { "model": "anthropic/kimi-for-coding" },
366
+ "document-writer": { "model": "anthropic/kimi-for-coding" },
367
+ "multimodal-looker": { "model": "anthropic/kimi-for-coding" },
368
+ "atlas": { "model": "anthropic/kimi-for-coding" }
369
+ }
370
+ }
371
+ ```
372
+
373
+ ---
374
+
375
+ ## Troubleshooting
376
+
377
+ ### "No Kimi API keys configured"
378
+
379
+ Run:
380
+ ```bash
381
+ opencode-kimi add-key your-api-key
382
+ ```
383
+
384
+ ### All keys rate limited
385
+
386
+ The plugin will wait for the soonest available key. You can:
387
+ 1. Wait for rate limits to reset
388
+ 2. Add more API keys
389
+ 3. Check status with `opencode-kimi list-keys`
390
+
391
+ ### Plugin not loading
392
+
393
+ 1. Ensure plugin is installed: Check `~/.config/opencode/opencode.json` has `"plugin": ["opencode-kimi-rotator@latest"]`
394
+ 2. Verify the plugin package is installed: `npm list -g opencode-kimi-rotator`
395
+ 3. Check OpenCode logs for errors
396
+
397
+ ### Reset Everything
398
+
399
+ If you need to start fresh:
400
+ ```bash
401
+ rm ~/.config/opencode/kimi-accounts.json
402
+ opencode-kimi add-key your-new-api-key
403
+ ```
404
+
405
+ ---
406
+
407
+ ## Plugin Compatibility
408
+
409
+ ### opencode-antigravity-auth
410
+
411
+ Both plugins can work together. List them in your preferred order:
412
+
413
+ ```json
414
+ {
415
+ "plugin": [
416
+ "opencode-kimi-rotator@latest",
417
+ "opencode-antigravity-auth@latest"
418
+ ]
419
+ }
420
+ ```
421
+
422
+ ### oh-my-opencode
423
+
424
+ Configure agent models in `oh-my-opencode.json`:
425
+
426
+ ```json
427
+ {
428
+ "google_auth": false,
429
+ "agents": {
430
+ "sisyphus": { "model": "anthropic/kimi-for-coding" },
431
+ "oracle": { "model": "anthropic/kimi-for-coding" },
432
+ "librarian": { "model": "anthropic/kimi-for-coding" },
433
+ "atlas": { "model": "anthropic/kimi-for-coding" }
434
+ }
435
+ }
436
+ ```
437
+
438
+ ---
439
+
440
+ ## Automatic Installation Script
441
+
442
+ For automated setup, you can use this one-liner:
443
+
444
+ ```bash
445
+ curl -fsSL https://raw.githubusercontent.com/deyndev/opencode-kimi-rotator/main/scripts/install.sh | bash
446
+ ```
447
+
448
+ Or for Windows (PowerShell):
449
+
450
+ ```powershell
451
+ irm https://raw.githubusercontent.com/deyndev/opencode-kimi-rotator/main/scripts/install.ps1 | iex
452
+ ```
453
+
454
+ ---
455
+
456
+ ## Development
457
+
458
+ ### Local Development Setup
459
+
460
+ 1. Clone the repository:
461
+ ```bash
462
+ git clone https://github.com/deyndev/opencode-kimi-rotator.git
463
+ cd opencode-kimi-rotator
464
+ ```
465
+
466
+ 2. Install dependencies and build:
467
+ ```bash
468
+ npm install
469
+ npm run build
470
+ ```
471
+
472
+ 3. Link for local testing:
473
+ ```bash
474
+ npm link
475
+ ```
476
+
477
+ 4. Add to your OpenCode config:
478
+ ```json
479
+ {
480
+ "plugin": ["opencode-kimi-rotator"]
481
+ }
482
+ ```
483
+
484
+ ---
485
+
486
+ ## License
487
+
488
+ MIT License. See [LICENSE](LICENSE) for details.