opencode-plugin-litellm 1.2.0 → 1.4.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/README.md +151 -89
- package/package.json +3 -2
- package/src/index.ts +1 -0
- package/src/plugin/index.ts +71 -28
- package/src/plugin/v2.ts +540 -0
- package/src/utils/litellm-api.ts +5 -3
- package/src/utils/model-cache.ts +15 -9
package/README.md
CHANGED
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
|
|
7
7
|
# opencode-litellm
|
|
8
8
|
|
|
9
|
-
**
|
|
9
|
+
**[OpenCode V1 + V2](https://opencode.ai) + [LiteLLM](https://github.com/BerriAI/litellm) provider with zero configuration.**
|
|
10
10
|
|
|
11
11
|
[](https://opencode.ai)
|
|
12
|
+
[](https://opencode.ai)
|
|
12
13
|
[](https://github.com/BerriAI/litellm)
|
|
13
14
|
|
|
14
15
|
[](https://www.npmjs.com/package/opencode-plugin-litellm)
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
Auto-detect a running LiteLLM proxy, pull every model from `/v1/models`, and register them in OpenCode.
|
|
22
23
|
**No model lists to hand-maintain. No restart loops. No surprises.**
|
|
23
24
|
|
|
24
|
-
<img src="https://raw.githubusercontent.com/yuseferi/opencode-litellm/main/assets/architecture.svg" alt="How opencode-litellm works: OpenCode
|
|
25
|
+
<img src="https://raw.githubusercontent.com/yuseferi/opencode-litellm/main/assets/architecture.svg" alt="How opencode-litellm works: OpenCode registers discovered LiteLLM models through the provider API and refreshes its provider registry from a disk cache" width="100%" />
|
|
25
26
|
|
|
26
27
|
[Quickstart](#-quickstart) · [Configuration](#%EF%B8%8F-configuration) · [How it works](#-how-it-works) · [FAQ](#-faq) · [Contributing](./CONTRIBUTING.md)
|
|
27
28
|
|
|
@@ -30,59 +31,60 @@ Auto-detect a running LiteLLM proxy, pull every model from `/v1/models`, and reg
|
|
|
30
31
|
> **npm package:** `opencode-plugin-litellm` · **GitHub repo:** `yuseferi/opencode-litellm`
|
|
31
32
|
> The unscoped `opencode-litellm` npm name was already taken by another author.
|
|
32
33
|
|
|
34
|
+
> **✅ OpenCode V1 + V2 support:** OpenCode **2.0.19** uses native
|
|
35
|
+
> `Plugin.define`, provider-registry model discovery, and in-process refresh.
|
|
36
|
+
> OpenCode **1.18.29+** uses the legacy `server()` entrypoint. Both versions
|
|
37
|
+
> are supported by this package.
|
|
38
|
+
|
|
33
39
|
---
|
|
34
40
|
|
|
35
41
|
## ✨ Why this plugin?
|
|
36
42
|
|
|
37
43
|
Maintaining a `models` block in `opencode.json` for every model your LiteLLM proxy exposes is a chore — every new entry in your `model_list` means a config edit, a restart, and a context-switch.
|
|
38
44
|
|
|
39
|
-
`opencode-litellm` removes that loop entirely. It
|
|
45
|
+
`opencode-litellm` removes that loop entirely. It queries your LiteLLM proxy during plugin setup and registers discovered models with OpenCode's provider registry. New models are refreshed in-process through the V2 provider API, while V1 keeps its config-hook behavior.
|
|
40
46
|
|
|
41
47
|
## 🚀 Quickstart
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"$schema": "https://opencode.ai/config.json",
|
|
47
|
-
"plugin": ["opencode-plugin-litellm@latest"],
|
|
48
|
-
"provider": {
|
|
49
|
-
"litellm": {
|
|
50
|
-
"npm": "@ai-sdk/openai-compatible",
|
|
51
|
-
"options": {
|
|
52
|
-
"baseURL": "http://localhost:4000/v1"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
```
|
|
49
|
+
After a V2-compatible release is published, install the plugin from the CLI
|
|
50
|
+
and configure the shared background service. If your OpenCode 2 executable is
|
|
51
|
+
named `opencode2`, use it in place of `opencode` below.
|
|
58
52
|
|
|
59
53
|
```bash
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
# 3. Run OpenCode — every model in your LiteLLM model_list is now available.
|
|
64
|
-
opencode
|
|
54
|
+
opencode plugin add opencode-plugin-litellm
|
|
55
|
+
opencode service set env LITELLM_BASE_URL https://litellm.internal.example.com/v1
|
|
56
|
+
opencode service set env LITELLM_API_KEY YOUR_API_KEY
|
|
65
57
|
```
|
|
66
58
|
|
|
59
|
+
Then open OpenCode and select a discovered model under `litellm/...`.
|
|
60
|
+
|
|
61
|
+
> **RFC status:** OpenCode 2 support is not yet included in the published npm
|
|
62
|
+
> package. For now, `plugin add` installs the previous release, not this RFC
|
|
63
|
+
> implementation.
|
|
64
|
+
|
|
65
|
+
For a local LiteLLM proxy on port 4000, the base URL can be omitted and the
|
|
66
|
+
plugin will detect it automatically. The [configuration section](#%EF%B8%8F-configuration)
|
|
67
|
+
also shows how to configure a provider directly in `opencode.json`.
|
|
68
|
+
|
|
67
69
|
## 🎯 Features
|
|
68
70
|
|
|
69
71
|
| | |
|
|
70
72
|
|---|---|
|
|
71
73
|
| 🔍 **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
72
74
|
| 📡 **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
73
|
-
| ⚡ **
|
|
74
|
-
| 🏷️ **Smart formatting** | Turns `anthropic/claude-3-5-sonnet` into `Claude 3.5 Sonnet` in the picker — handles versions, sizes, quantizations, and brand-cased names like `gpt-4o`. |
|
|
75
|
+
| ⚡ **Stale-while-revalidate** | A warm disk cache supplies models without a network request. New sessions trigger a background refresh, and cache entries expire after 7 days. |
|
|
76
|
+
| 🏷️ **Smart formatting** | Turns `anthropic/claude-3-5-sonnet` into `Claude 3.5 Sonnet` in the picker — handles versions, sizes, quantizations, and brand-cased names like `gpt-4o`. Set `formatModelNames: false` to keep the raw LiteLLM ids instead. |
|
|
75
77
|
| 🧠 **Modality-aware** | Enriches `/v1/models` entries with `/v1/model/info` (`mode`, token limits, capability flags) and hides embedding / image / audio models from the picker. |
|
|
76
78
|
| 💵 **Real pricing** | Maps `input_cost_per_token` / `output_cost_per_token` (and cache read/write costs) from `/v1/model/info` into OpenCode's `cost` field, so the picker and `/cost` show what the proxy actually bills instead of `$0.00`. Models LiteLLM has no price for are left unpriced, not falsely marked free. |
|
|
77
79
|
| 🧩 **Reasoning-effort variants** | When LiteLLM reports per-model effort support (`supports_low_reasoning_effort`, …), the plugin surfaces each level as a picker variant automatically. |
|
|
78
|
-
| 🔐 **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY
|
|
79
|
-
| 🌐 **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways
|
|
80
|
+
| 🔐 **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY`, `providers.litellm.settings.apiKey`, and OpenCode `/connect` credentials on the V1 server path. |
|
|
81
|
+
| 🌐 **Gateway-friendly** | Supports provider `headers` (and the legacy `customHeaders` option) for proxies behind Cloudflare Access or other API gateways. |
|
|
80
82
|
| 🧩 **Splittable catalog** | `includeModels` / `excludeModels` (glob patterns) let one LiteLLM proxy be divided into several OpenCode providers — e.g. by naming prefix — without hand-maintaining a model list. |
|
|
81
83
|
| 🎚️ **Capability overrides** | `modelCapabilities` forces or retracts per-model capability flags (`supports_vision`, `supports_function_calling`, …) when `/v1/model/info` is unavailable or disagrees with your deployment. |
|
|
82
|
-
| ⏱️ **
|
|
83
|
-
| 📝 **
|
|
84
|
+
| ⏱️ **Bounded startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** (configurable via `LITELLM_REQUEST_TIMEOUT_MS`) for slow remote proxies. V2 model refresh runs in the background. |
|
|
85
|
+
| 📝 **OpenCode logging** | V1 logs go through OpenCode's log API; V2 logs use the V2 plugin host's console logging. |
|
|
84
86
|
| 🤝 **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
85
|
-
|
|
|
87
|
+
| 🔌 **OpenCode V1 + V2** | Supports OpenCode 2's native `Plugin.define`, provider transforms, event subscription, and live model refresh, plus the OpenCode 1.18.29+ legacy `server()` entrypoint. |
|
|
86
88
|
| 🔒 **TypeScript strict** | Strict-mode compiled, fully typed public API. |
|
|
87
89
|
|
|
88
90
|
## ⚙️ Configuration
|
|
@@ -94,11 +96,12 @@ Point at your LiteLLM proxy — the plugin discovers all models automatically:
|
|
|
94
96
|
```jsonc
|
|
95
97
|
{
|
|
96
98
|
"$schema": "https://opencode.ai/config.json",
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
+
"plugins": ["opencode-plugin-litellm@latest"],
|
|
100
|
+
"providers": {
|
|
99
101
|
"litellm": {
|
|
100
|
-
"
|
|
101
|
-
"
|
|
102
|
+
"name": "LiteLLM (proxy)",
|
|
103
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
104
|
+
"settings": {
|
|
102
105
|
"baseURL": "http://localhost:4000/v1"
|
|
103
106
|
}
|
|
104
107
|
}
|
|
@@ -113,12 +116,12 @@ You **do not need to list any models** — the plugin still discovers them from
|
|
|
113
116
|
```jsonc
|
|
114
117
|
{
|
|
115
118
|
"$schema": "https://opencode.ai/config.json",
|
|
116
|
-
"
|
|
117
|
-
"
|
|
119
|
+
"plugins": ["opencode-plugin-litellm@latest"],
|
|
120
|
+
"providers": {
|
|
118
121
|
"litellm": {
|
|
119
|
-
"npm": "@ai-sdk/openai-compatible",
|
|
120
122
|
"name": "LiteLLM (proxy)",
|
|
121
|
-
"
|
|
123
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
124
|
+
"settings": {
|
|
122
125
|
"baseURL": "http://litellm.internal.example.com/v1",
|
|
123
126
|
"apiKey": "{env:LITELLM_API_KEY}"
|
|
124
127
|
}
|
|
@@ -129,6 +132,28 @@ You **do not need to list any models** — the plugin still discovers them from
|
|
|
129
132
|
|
|
130
133
|
That's the whole config — every model in your LiteLLM `model_list` will appear in the picker.
|
|
131
134
|
|
|
135
|
+
An explicit `settings.baseURL` in a LiteLLM provider takes precedence over
|
|
136
|
+
`LITELLM_BASE_URL`. If neither is set, the plugin checks the usual local ports.
|
|
137
|
+
|
|
138
|
+
### OpenCode 1.18.29+
|
|
139
|
+
|
|
140
|
+
The package also exposes the V1 server entrypoint for older OpenCode 1
|
|
141
|
+
installations. Keep the V1 `plugin` / `provider` configuration shape:
|
|
142
|
+
|
|
143
|
+
```jsonc
|
|
144
|
+
{
|
|
145
|
+
"plugin": ["opencode-plugin-litellm@latest"],
|
|
146
|
+
"provider": {
|
|
147
|
+
"litellm": {
|
|
148
|
+
"npm": "@ai-sdk/openai-compatible",
|
|
149
|
+
"options": {
|
|
150
|
+
"baseURL": "http://localhost:4000/v1"
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
132
157
|
### Example: governed upstream route with Tuning Engines
|
|
133
158
|
|
|
134
159
|
If your team routes model traffic through Tuning Engines for policy, traces,
|
|
@@ -159,20 +184,19 @@ Engines sits on the upstream model route as the governed control plane.
|
|
|
159
184
|
|
|
160
185
|
### Overriding or curating individual models (optional)
|
|
161
186
|
|
|
162
|
-
If you want to rename a model in the picker
|
|
187
|
+
If you want to rename a model in the picker or otherwise hand-curate metadata, add it under `models`. The plugin preserves your entries and only injects discovered models whose key isn't already defined:
|
|
163
188
|
|
|
164
189
|
```jsonc
|
|
165
190
|
{
|
|
166
|
-
"
|
|
191
|
+
"providers": {
|
|
167
192
|
"litellm": {
|
|
168
|
-
"
|
|
193
|
+
"settings": {
|
|
169
194
|
"baseURL": "http://litellm.internal.example.com/v1",
|
|
170
195
|
"apiKey": "{env:LITELLM_API_KEY}"
|
|
171
196
|
},
|
|
172
197
|
"models": {
|
|
173
198
|
"openai/gpt-4o": {
|
|
174
|
-
"name": "GPT-4o (curated)"
|
|
175
|
-
"organizationOwner": "openai"
|
|
199
|
+
"name": "GPT-4o (curated)"
|
|
176
200
|
}
|
|
177
201
|
}
|
|
178
202
|
}
|
|
@@ -208,12 +232,13 @@ If your LiteLLM proxy requires a master key, expose it via either approach:
|
|
|
208
232
|
|
|
209
233
|
| Method | Example |
|
|
210
234
|
|---|---|
|
|
235
|
+
| Env var (base URL) | `LITELLM_BASE_URL=https://litellm.internal.example.com/v1` |
|
|
211
236
|
| Env var | `export LITELLM_API_KEY=sk-...` |
|
|
212
237
|
| Env var (alias) | `export LITELLM_MASTER_KEY=sk-...` |
|
|
213
|
-
| Config | `"
|
|
238
|
+
| Config | `"settings": { "apiKey": "{env:LITELLM_API_KEY}" }` |
|
|
214
239
|
| OpenCode `/connect` | Run `/connect`, search for your `litellm` provider entry, and paste the key |
|
|
215
240
|
|
|
216
|
-
The env var path lets you commit `opencode.json` without leaking secrets.
|
|
241
|
+
The env var path lets you commit `opencode.json` without leaking secrets. On OpenCode 1, `/connect` credentials are read from OpenCode's auth store (`~/.local/share/opencode/auth.json`) and applied to health checks, model discovery, and completion-time requests. OpenCode 2 users can use `settings.apiKey` with an environment substitution or set `LITELLM_API_KEY` on the background service as shown in the Quickstart.
|
|
217
242
|
|
|
218
243
|
### Slow proxies (`LITELLM_REQUEST_TIMEOUT_MS`)
|
|
219
244
|
|
|
@@ -227,19 +252,19 @@ The overall discovery cap scales with it (max of 20 s and the request timeout +
|
|
|
227
252
|
|
|
228
253
|
### Custom headers (Cloudflare Access, API gateways)
|
|
229
254
|
|
|
230
|
-
If your LiteLLM proxy is behind Cloudflare Access or another gateway that requires extra HTTP headers, use the `
|
|
255
|
+
If your LiteLLM proxy is behind Cloudflare Access or another gateway that requires extra HTTP headers, use the provider's `headers` map:
|
|
231
256
|
|
|
232
257
|
```jsonc
|
|
233
258
|
{
|
|
234
|
-
"
|
|
259
|
+
"providers": {
|
|
235
260
|
"litellm": {
|
|
236
|
-
"
|
|
261
|
+
"settings": {
|
|
237
262
|
"baseURL": "https://litellm.internal.example.com/v1",
|
|
238
|
-
"apiKey": "{env:LITELLM_API_KEY}"
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
263
|
+
"apiKey": "{env:LITELLM_API_KEY}"
|
|
264
|
+
},
|
|
265
|
+
"headers": {
|
|
266
|
+
"CF-Access-Client-Id": "{env:CF_ACCESS_CLIENT_ID}",
|
|
267
|
+
"CF-Access-Client-Secret": "{env:CF_ACCESS_CLIENT_SECRET}"
|
|
243
268
|
}
|
|
244
269
|
}
|
|
245
270
|
}
|
|
@@ -254,19 +279,19 @@ If your LiteLLM catalog mixes naming conventions from different teams or environ
|
|
|
254
279
|
|
|
255
280
|
```jsonc
|
|
256
281
|
{
|
|
257
|
-
"
|
|
282
|
+
"providers": {
|
|
258
283
|
"litellm": {
|
|
259
|
-
"npm": "@ai-sdk/openai-compatible",
|
|
260
284
|
"name": "Prod",
|
|
261
|
-
"
|
|
285
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
286
|
+
"settings": {
|
|
262
287
|
"baseURL": "http://localhost:4000/v1",
|
|
263
288
|
"includeModels": ["prod/*"]
|
|
264
289
|
}
|
|
265
290
|
},
|
|
266
291
|
"litellm-staging": {
|
|
267
|
-
"npm": "@ai-sdk/openai-compatible",
|
|
268
292
|
"name": "Staging",
|
|
269
|
-
"
|
|
293
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
294
|
+
"settings": {
|
|
270
295
|
"baseURL": "http://localhost:4000/v1",
|
|
271
296
|
"includeModels": ["staging/*"],
|
|
272
297
|
"excludeModels": ["staging/*-canary"]
|
|
@@ -280,6 +305,7 @@ If your LiteLLM catalog mixes naming conventions from different teams or environ
|
|
|
280
305
|
- `excludeModels` is evaluated after and always wins, even over `includeModels`.
|
|
281
306
|
- Patterns support only `*` (any run of characters); everything else is matched literally, so dots in ids like `gpt-4.1` need no escaping.
|
|
282
307
|
- Filtering happens before the on-disk cache is written, so each provider's cached view respects its own filters.
|
|
308
|
+
- Provider IDs beginning with `litellm-` are recognized automatically. Add `settings.litellm: true` to mark a provider with another ID.
|
|
283
309
|
|
|
284
310
|
### Correcting capability flags (`modelCapabilities`)
|
|
285
311
|
|
|
@@ -287,9 +313,9 @@ Model classification (tool-call badge, attachments, reasoning, input modalities)
|
|
|
287
313
|
|
|
288
314
|
```jsonc
|
|
289
315
|
{
|
|
290
|
-
"
|
|
316
|
+
"providers": {
|
|
291
317
|
"litellm": {
|
|
292
|
-
"
|
|
318
|
+
"settings": {
|
|
293
319
|
"baseURL": "http://localhost:4000/v1",
|
|
294
320
|
"modelCapabilities": {
|
|
295
321
|
"te-gpt-5.4-mini": { "supports_function_calling": true },
|
|
@@ -304,8 +330,31 @@ Model classification (tool-call badge, attachments, reasoning, input modalities)
|
|
|
304
330
|
- Overrides apply on top of whatever the proxy reports, with an explicit `false` winning — a flag the proxy never reported can be forced on just the same.
|
|
305
331
|
- Keys are exact model ids as they appear in `/v1/models` (not globs).
|
|
306
332
|
- Overridden flags flow into the picker exactly like natively reported ones, and the adjusted view is what gets persisted to the model cache.
|
|
333
|
+
- When `/v1/model/info` reports no modality flags, discovered chat models are registered as text-only. This avoids OpenCode's image-capable fallback for text-only deployments; set `supports_vision: true` explicitly for a route that has a working multimodal projector.
|
|
307
334
|
- Changing `modelCapabilities` (or `includeModels`/`excludeModels`) starts a fresh discovery on the next start — the cache is scoped by that config — so the picker reflects the new flags immediately.
|
|
308
335
|
|
|
336
|
+
### Keeping raw model ids in the picker (`formatModelNames`)
|
|
337
|
+
|
|
338
|
+
By default the plugin prettifies each discovered id into a display name — `anthropic/claude-3-5-sonnet` shows up as `Claude 3.5 Sonnet`. If you'd rather see your LiteLLM `model_list` aliases verbatim (for example because your team refers to models by those exact names, or the formatter mangles an internal naming scheme), turn formatting off:
|
|
339
|
+
|
|
340
|
+
```jsonc
|
|
341
|
+
{
|
|
342
|
+
"providers": {
|
|
343
|
+
"litellm": {
|
|
344
|
+
"settings": {
|
|
345
|
+
"baseURL": "http://localhost:4000/v1",
|
|
346
|
+
"formatModelNames": false
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
- The display name becomes the exact model id as returned by `/v1/models` (provider prefix, version suffixes and all). Nothing else changes — ids, capability flags, pricing and filtering behave exactly as before.
|
|
354
|
+
- Only a boolean `false` disables formatting; omitting the option or setting anything else keeps the default.
|
|
355
|
+
- Like the other options above, `formatModelNames` is part of the cache identity, so toggling it triggers a fresh discovery on the next start instead of serving previously cached names.
|
|
356
|
+
- To rename just a handful of models while keeping smart formatting for the rest, use the per-model `name` override described in [Overriding or curating individual models](#overriding-or-curating-individual-models-optional).
|
|
357
|
+
|
|
309
358
|
## 🔧 How it works
|
|
310
359
|
|
|
311
360
|
```mermaid
|
|
@@ -315,42 +364,46 @@ sequenceDiagram
|
|
|
315
364
|
participant Cache as disk cache
|
|
316
365
|
participant LL as LiteLLM proxy
|
|
317
366
|
|
|
318
|
-
OC->>Plugin:
|
|
319
|
-
alt
|
|
367
|
+
OC->>Plugin: Plugin.define.setup(ctx)
|
|
368
|
+
alt providers.litellm configured
|
|
320
369
|
Plugin->>Plugin: use configured baseURL
|
|
321
370
|
else not configured
|
|
322
371
|
Plugin->>LL: probe :4000, :8000, :8080 (GET /v1/models, 3 s fail-fast)
|
|
323
372
|
LL-->>Plugin: 200 OK on one
|
|
324
|
-
Plugin->>Plugin:
|
|
373
|
+
Plugin->>Plugin: prepare litellm provider
|
|
325
374
|
end
|
|
326
375
|
Plugin->>Cache: read SWR cache (no network yet)
|
|
327
376
|
alt cache hit
|
|
328
377
|
Cache-->>Plugin: cached models
|
|
329
|
-
Plugin->>OC:
|
|
378
|
+
Plugin->>OC: provider.transform(cached models)
|
|
330
379
|
else cache miss
|
|
331
380
|
Plugin->>LL: GET /v1/models (with auth if set)
|
|
332
381
|
Plugin->>LL: GET /v1/model/info (best-effort)
|
|
333
382
|
LL-->>Plugin: { data: [...models] } + per-model info
|
|
334
383
|
Plugin->>Plugin: enrich models, hide non-chat (embedding/image/audio)
|
|
335
384
|
Plugin->>Plugin: format names, infer modalities + limits + pricing
|
|
336
|
-
Plugin->>OC:
|
|
385
|
+
Plugin->>OC: provider.transform(discovered models)
|
|
337
386
|
Plugin->>Cache: persist for next startup
|
|
338
387
|
end
|
|
339
388
|
Note over Plugin,Cache: on session.created, revalidate cache in<br/>the background (throttled to 5 min)
|
|
340
|
-
OC->>
|
|
389
|
+
OC->>Plugin: session.created event
|
|
390
|
+
Plugin->>LL: refresh model inventory
|
|
391
|
+
Plugin->>OC: provider.reload() when models changed
|
|
341
392
|
```
|
|
342
393
|
|
|
343
|
-
1. On OpenCode startup the `
|
|
344
|
-
2. If `
|
|
394
|
+
1. On OpenCode 2 startup the plugin's `setup` registers a provider transform. OpenCode 1 uses the legacy `config` hook.
|
|
395
|
+
2. If `providers.litellm` exists, its `settings.baseURL` is used. Otherwise common ports are probed — that probe is the only health check (3 s fail-fast per port).
|
|
345
396
|
3. With a configured `baseURL` the proxy is not contacted during startup unless the cache is cold; the discovery fetch itself fails fast if the proxy is unreachable.
|
|
346
|
-
4. **Fast path:** if a fresh on-disk cache exists (≤ 7 days old), its models are
|
|
397
|
+
4. **Fast path:** if a fresh on-disk cache exists (≤ 7 days old), its models are registered synchronously — startup does not wait on network discovery.
|
|
347
398
|
5. **Cold path:** `/v1/models` and `/v1/model/info` are fetched in parallel. Models are enriched with info metadata (`mode`, token limits, capability flags, per-token pricing — `/v1/models` omits these for database-defined models) and converted into OpenCode model entries with formatted `name`, inferred `modalities`, and `cost` (USD/1M tokens, converted from LiteLLM's USD/token). Non-chat models (embedding / image / audio) are excluded from the picker.
|
|
348
|
-
6. Discovered models are
|
|
349
|
-
7. On
|
|
399
|
+
6. Discovered models are added alongside user-defined models — never overwriting them — and persisted to the cache.
|
|
400
|
+
7. On `session.created`, the cache is revalidated in the background (throttled to once per 5 minutes). V2 calls `provider.reload()` when the inventory changes; V1 surfaces refreshed entries on the next OpenCode start. The cold path is capped by a 20 s timeout.
|
|
401
|
+
|
|
402
|
+
> The earlier Desktop limitation applied to OpenCode 1's config-hook path. OpenCode 2 registers models through its provider registry transform.
|
|
350
403
|
|
|
351
404
|
## 📋 Requirements
|
|
352
405
|
|
|
353
|
-
- [OpenCode](https://opencode.ai) ≥ 0.1.
|
|
406
|
+
- [OpenCode](https://opencode.ai) ≥ 2.0.14, or OpenCode 1.18.29+ for the legacy server entrypoint
|
|
354
407
|
- A running [LiteLLM](https://github.com/BerriAI/litellm) proxy:
|
|
355
408
|
```bash
|
|
356
409
|
pip install 'litellm[proxy]'
|
|
@@ -362,8 +415,8 @@ sequenceDiagram
|
|
|
362
415
|
|
|
363
416
|
| LiteLLM version | OpenCode version | Status |
|
|
364
417
|
|---|---|---|
|
|
365
|
-
| ≥ 1.40 | ≥ 0.1.
|
|
366
|
-
| 1.30 – 1.39 | ≥ 0.1.
|
|
418
|
+
| ≥ 1.40 | ≥ 2.0.14 or 1.18.29+ | ✅ Supported |
|
|
419
|
+
| 1.30 – 1.39 | ≥ 2.0.14 or 1.18.29+ | ⚠️ Should work (older `/v1/models` schema) |
|
|
367
420
|
| < 1.30 | any | ❌ Unsupported |
|
|
368
421
|
|
|
369
422
|
## ❓ FAQ
|
|
@@ -372,18 +425,25 @@ sequenceDiagram
|
|
|
372
425
|
<summary><b>Why doesn't a model appear in OpenCode after I add it to LiteLLM?</b></summary>
|
|
373
426
|
|
|
374
427
|
Once LiteLLM exposes the model (restart or hot-reload LiteLLM if you edited
|
|
375
|
-
its `config.yaml`), the plugin picks it up automatically
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
start** — no OpenCode config change needed. To force an immediate refetch,
|
|
428
|
+
its `config.yaml`), the plugin picks it up automatically. OpenCode 2 refreshes
|
|
429
|
+
the provider registry when a new session triggers a changed inventory; OpenCode
|
|
430
|
+
1 serves the refreshed cache on the next start. To force an immediate refetch,
|
|
379
431
|
delete the cache directory (`~/.cache/opencode-litellm/`, or
|
|
380
432
|
`$XDG_CACHE_HOME/opencode-litellm/`).
|
|
381
433
|
</details>
|
|
382
434
|
|
|
435
|
+
<details>
|
|
436
|
+
<summary><b>Why do discovered models appear in the CLI but not OpenCode Desktop?</b></summary>
|
|
437
|
+
|
|
438
|
+
This was an OpenCode 1 config-hook limitation. OpenCode 2 registers discovered
|
|
439
|
+
models through the provider registry transform. If models are still missing in
|
|
440
|
+
OpenCode 2 Desktop, check the plugin log for provider discovery errors.
|
|
441
|
+
</details>
|
|
442
|
+
|
|
383
443
|
<details>
|
|
384
444
|
<summary><b>Can I use this with a remote LiteLLM proxy?</b></summary>
|
|
385
445
|
|
|
386
|
-
Yes. Set `
|
|
446
|
+
Yes. Set `providers.litellm.settings.baseURL` to your remote URL and optionally set `settings.apiKey`. Auto-detection only probes `localhost`, but explicit configuration works against any URL.
|
|
387
447
|
</details>
|
|
388
448
|
|
|
389
449
|
<details>
|
|
@@ -400,7 +460,7 @@ the proxy is reachable again.
|
|
|
400
460
|
<details>
|
|
401
461
|
<summary><b>Will my hand-curated model entries be overwritten?</b></summary>
|
|
402
462
|
|
|
403
|
-
No. The merge is additive: anything you've already defined under `
|
|
463
|
+
No. The merge is additive: anything you've already defined under `providers.litellm.models` is preserved. Discovered models are only added if their key isn't already present.
|
|
404
464
|
</details>
|
|
405
465
|
|
|
406
466
|
<details>
|
|
@@ -418,18 +478,19 @@ Yes — anything in your LiteLLM `model_list` shows up, including Ollama, Bedroc
|
|
|
418
478
|
<details>
|
|
419
479
|
<summary><b>My LiteLLM proxy is behind Cloudflare Access — how do I authenticate?</b></summary>
|
|
420
480
|
|
|
421
|
-
Cloudflare Access intercepts requests before they reach LiteLLM, so a plain `Authorization: Bearer` header isn't enough. Create a [Cloudflare Access Service Token](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/) and pass the credentials via `
|
|
481
|
+
Cloudflare Access intercepts requests before they reach LiteLLM, so a plain `Authorization: Bearer` header isn't enough. Create a [Cloudflare Access Service Token](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/) and pass the credentials via `providers.litellm.headers`:
|
|
422
482
|
|
|
423
483
|
```jsonc
|
|
424
484
|
{
|
|
425
|
-
"
|
|
485
|
+
"providers": {
|
|
426
486
|
"litellm": {
|
|
427
|
-
"
|
|
428
|
-
|
|
429
|
-
"
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
487
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
488
|
+
"settings": {
|
|
489
|
+
"baseURL": "https://litellm.your-company.com/v1"
|
|
490
|
+
},
|
|
491
|
+
"headers": {
|
|
492
|
+
"CF-Access-Client-Id": "{env:CF_ACCESS_CLIENT_ID}",
|
|
493
|
+
"CF-Access-Client-Secret": "{env:CF_ACCESS_CLIENT_SECRET}"
|
|
433
494
|
}
|
|
434
495
|
}
|
|
435
496
|
}
|
|
@@ -474,7 +535,8 @@ src/
|
|
|
474
535
|
│ ├── model-capabilities.ts # per-model capability flag overrides
|
|
475
536
|
│ └── opencode-auth.ts # fallback to OpenCode's /connect-stored credentials
|
|
476
537
|
└── plugin/
|
|
477
|
-
|
|
538
|
+
├── index.ts # OpenCode 1 server entry and shared discovery logic
|
|
539
|
+
└── v2.ts # OpenCode 2 definition, provider transform, event-driven reload
|
|
478
540
|
|
|
479
541
|
test/ # vitest suite for the pure logic
|
|
480
542
|
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "opencode-plugin-litellm",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.4.0",
|
|
5
5
|
"description": "OpenCode plugin for LiteLLM proxy support with auto-detection and dynamic model discovery",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./src/index.ts",
|
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
"provenance": true
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@opencode-ai/plugin": "^1.14.0"
|
|
43
|
+
"@opencode-ai/plugin": "^1.14.0",
|
|
44
|
+
"@opencode/plugin": "^2.0.14"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@semantic-release/changelog": "^7.0.0",
|
package/src/index.ts
CHANGED