opencode-plugin-litellm 0.1.0 → 0.2.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 +293 -44
- package/package.json +1 -1
- package/src/plugin/config-hook.ts +5 -2
- package/src/plugin/enhance-config.ts +166 -36
- package/src/plugin/index.ts +16 -1
- package/src/types/index.ts +41 -0
- package/src/utils/format-model-name.ts +29 -0
package/README.md
CHANGED
|
@@ -1,40 +1,81 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# opencode-litellm
|
|
2
4
|
|
|
3
|
-
[
|
|
4
|
-
|
|
5
|
-
[ provider for [OpenCode](https://opencode.ai) with zero configuration.**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/opencode-plugin-litellm)
|
|
8
|
+
[](https://www.npmjs.com/package/opencode-plugin-litellm)
|
|
9
|
+
[](https://github.com/yuseferi/opencode-litellm/actions/workflows/ci.yml)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](./tsconfig.json)
|
|
12
|
+
[](./CONTRIBUTING.md)
|
|
13
|
+
|
|
14
|
+
Auto-detect a running LiteLLM proxy, pull every model from `/v1/models`, and register them in OpenCode.
|
|
15
|
+
**No model lists to hand-maintain. No restart loops. No surprises.**
|
|
6
16
|
|
|
7
|
-
|
|
8
|
-
with **auto-detection** and **dynamic model discovery**.
|
|
17
|
+
[Quickstart](#-quickstart) · [Configuration](#%EF%B8%8F-configuration) · [How it works](#-how-it-works) · [FAQ](#-faq) · [Contributing](./CONTRIBUTING.md)
|
|
9
18
|
|
|
10
|
-
|
|
19
|
+
</div>
|
|
11
20
|
|
|
12
|
-
> **npm package
|
|
13
|
-
>
|
|
21
|
+
> **npm package:** `opencode-plugin-litellm` · **GitHub repo:** `yuseferi/opencode-litellm`
|
|
22
|
+
> The unscoped `opencode-litellm` npm name was already taken by another author.
|
|
14
23
|
|
|
15
|
-
|
|
24
|
+
---
|
|
16
25
|
|
|
17
|
-
|
|
18
|
-
- **Dynamic model discovery** — queries `/v1/models` so you never hand-maintain a model list
|
|
19
|
-
- **OpenAI-compatible** — registers under OpenCode as `@ai-sdk/openai-compatible`
|
|
20
|
-
- **Vendor-aware** — extracts `litellm_provider` (or the `provider/model` prefix) into `organizationOwner`
|
|
21
|
-
- **Smart formatting** — turns `anthropic/claude-3-5-sonnet` into `Claude 3 5 Sonnet` in the picker
|
|
22
|
-
- **Categorization** — chat / embedding / image / audio modalities are inferred from the model `mode` or id
|
|
23
|
-
- **Non-blocking** — discovery is capped at 5s so a slow proxy never blocks OpenCode startup
|
|
26
|
+
## ✨ Why this plugin?
|
|
24
27
|
|
|
25
|
-
|
|
28
|
+
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.
|
|
29
|
+
|
|
30
|
+
`opencode-litellm` removes that loop entirely. It hooks into OpenCode's `config` lifecycle, queries your LiteLLM proxy at startup, and merges the discovered models into your config in memory. The result: every model in `litellm config.yaml` shows up in OpenCode's picker the moment you start it — automatically.
|
|
31
|
+
|
|
32
|
+
## 🚀 Quickstart
|
|
26
33
|
|
|
27
34
|
```bash
|
|
35
|
+
# 1. Install
|
|
28
36
|
npm install opencode-plugin-litellm
|
|
29
|
-
# or
|
|
30
|
-
|
|
37
|
+
# or: bun add opencode-plugin-litellm
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```jsonc
|
|
41
|
+
// 2. Add to opencode.json
|
|
42
|
+
{
|
|
43
|
+
"$schema": "https://opencode.ai/config.json",
|
|
44
|
+
"plugin": ["opencode-plugin-litellm@latest"]
|
|
45
|
+
}
|
|
31
46
|
```
|
|
32
47
|
|
|
33
|
-
|
|
48
|
+
```bash
|
|
49
|
+
# 3. Start LiteLLM (if it isn't already)
|
|
50
|
+
litellm --config config.yaml --port 4000
|
|
51
|
+
|
|
52
|
+
# 4. Run OpenCode — every model in your LiteLLM model_list is now available.
|
|
53
|
+
opencode
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
That's it. No provider block required.
|
|
57
|
+
|
|
58
|
+
## 🎯 Features
|
|
59
|
+
|
|
60
|
+
| | |
|
|
61
|
+
|---|---|
|
|
62
|
+
| 🔍 **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
63
|
+
| 📡 **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
64
|
+
| 🏷️ **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`. |
|
|
65
|
+
| 🧠 **Modality-aware** | Infers `chat` / `embedding` / `image` / `audio` from the model `mode` field or id, and writes proper `modalities` metadata. |
|
|
66
|
+
| 🧪 **Reasoning-aware routing** | Auto-routes `gpt-5*` / `o1`/`o3`/`o4*` models through a sibling `litellm-responses` provider that uses `/v1/responses`, so tools + `reasoning_effort` actually work. Override per model via `responsesApiModels` / `chatApiModels`. |
|
|
67
|
+
| 🏢 **Provider extraction** | Pulls `litellm_provider` (or the `provider/model` prefix) into `organizationOwner` so models group correctly in the UI. |
|
|
68
|
+
| 🔐 **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars or `provider.litellm.options.apiKey`. |
|
|
69
|
+
| ⏱️ **Non-blocking startup** | Discovery is capped at **5 s** — a slow or offline proxy never delays OpenCode boot. |
|
|
70
|
+
| 🤝 **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
71
|
+
| 🪶 **Zero runtime deps** | Only depends on `@opencode-ai/plugin`. No build step, no bundler. |
|
|
72
|
+
| 🔒 **TypeScript strict** | Strict-mode compiled, fully typed public API. |
|
|
73
|
+
|
|
74
|
+
## ⚙️ Configuration
|
|
34
75
|
|
|
35
76
|
### Zero-config (recommended)
|
|
36
77
|
|
|
37
|
-
If
|
|
78
|
+
If LiteLLM is on `localhost:4000`, `:8000`, or `:8080`, the plugin self-configures:
|
|
38
79
|
|
|
39
80
|
```json
|
|
40
81
|
{
|
|
@@ -43,12 +84,11 @@ If a LiteLLM proxy is already running on `localhost:4000`, just add the plugin:
|
|
|
43
84
|
}
|
|
44
85
|
```
|
|
45
86
|
|
|
46
|
-
|
|
47
|
-
your provider list automatically.
|
|
87
|
+
### Explicit provider
|
|
48
88
|
|
|
49
|
-
|
|
89
|
+
Override the URL, set an API key, or pre-define curated models that the plugin will preserve:
|
|
50
90
|
|
|
51
|
-
```
|
|
91
|
+
```jsonc
|
|
52
92
|
{
|
|
53
93
|
"$schema": "https://opencode.ai/config.json",
|
|
54
94
|
"plugin": ["opencode-plugin-litellm@latest"],
|
|
@@ -57,39 +97,248 @@ your provider list automatically.
|
|
|
57
97
|
"npm": "@ai-sdk/openai-compatible",
|
|
58
98
|
"name": "LiteLLM (proxy)",
|
|
59
99
|
"options": {
|
|
60
|
-
"baseURL": "http://
|
|
100
|
+
"baseURL": "http://litellm.internal.example.com/v1",
|
|
61
101
|
"apiKey": "{env:LITELLM_API_KEY}"
|
|
102
|
+
},
|
|
103
|
+
"models": {
|
|
104
|
+
"openai/gpt-4o": {
|
|
105
|
+
"name": "GPT-4o (curated)",
|
|
106
|
+
"organizationOwner": "openai"
|
|
107
|
+
}
|
|
62
108
|
}
|
|
63
109
|
}
|
|
64
110
|
}
|
|
65
111
|
}
|
|
66
112
|
```
|
|
67
113
|
|
|
68
|
-
|
|
69
|
-
|
|
114
|
+
The plugin will **keep your hand-defined `openai/gpt-4o`** and only inject models it discovers that aren't already there.
|
|
115
|
+
|
|
116
|
+
### Reasoning models (gpt-5, o1/o3/o4)
|
|
117
|
+
|
|
118
|
+
OpenAI's reasoning-tier models reject requests that combine `reasoning_effort`
|
|
119
|
+
with function tools when sent to `/v1/chat/completions`. The OpenAI Responses
|
|
120
|
+
API (`/v1/responses`) has no such restriction, so the plugin routes those
|
|
121
|
+
models through a **second provider entry** named `litellm-responses` that
|
|
122
|
+
uses an SDK speaking the Responses API.
|
|
123
|
+
|
|
124
|
+
You don't need to do anything for the default behaviour — the plugin
|
|
125
|
+
detects reasoning-tier models from their id (`gpt-5*`, `o1*`, `o3*`,
|
|
126
|
+
`o4*`) and from LiteLLM's `mode === 'responses'` field, and creates the
|
|
127
|
+
sibling provider lazily.
|
|
128
|
+
|
|
129
|
+
To override the routing per model:
|
|
130
|
+
|
|
131
|
+
```jsonc
|
|
132
|
+
{
|
|
133
|
+
"provider": {
|
|
134
|
+
"litellm": {
|
|
135
|
+
"options": {
|
|
136
|
+
"baseURL": "http://localhost:4000/v1",
|
|
137
|
+
|
|
138
|
+
// "auto" (default) | "chat" | "responses"
|
|
139
|
+
"transport": "auto",
|
|
140
|
+
|
|
141
|
+
// Force these into /v1/responses (highest precedence)
|
|
142
|
+
"responsesApiModels": ["gpt-5-4-high", "my-custom-reasoning-model"],
|
|
143
|
+
|
|
144
|
+
// Force these into /v1/chat/completions
|
|
145
|
+
"chatApiModels": ["o1-mini-cheap"]
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
The two providers share `baseURL` and `apiKey`. Models curated by hand
|
|
153
|
+
under either provider's `models` block are preserved verbatim, and a
|
|
154
|
+
discovered model is skipped if its key already exists under **either**
|
|
155
|
+
provider.
|
|
156
|
+
|
|
157
|
+
> **Note**: this requires LiteLLM ≥ 1.40 (which proxies `/v1/responses`)
|
|
158
|
+
> and an `@ai-sdk/openai` version that supports the Responses API. Older
|
|
159
|
+
> AI SDKs may silently fall back to chat-completions, in which case set
|
|
160
|
+
> `responsesApiModels` to an empty list and fix the upstream LiteLLM
|
|
161
|
+
> config instead (e.g. `use_responses_api: true` per model).
|
|
70
162
|
|
|
71
163
|
### Authentication
|
|
72
164
|
|
|
73
|
-
If your LiteLLM proxy requires a master key, expose it via either:
|
|
165
|
+
If your LiteLLM proxy requires a master key, expose it via either approach:
|
|
166
|
+
|
|
167
|
+
| Method | Example |
|
|
168
|
+
|---|---|
|
|
169
|
+
| Env var | `export LITELLM_API_KEY=sk-...` |
|
|
170
|
+
| Env var (alias) | `export LITELLM_MASTER_KEY=sk-...` |
|
|
171
|
+
| Config | `"options": { "apiKey": "{env:LITELLM_API_KEY}" }` |
|
|
172
|
+
|
|
173
|
+
The env var path lets you commit `opencode.json` without leaking secrets.
|
|
174
|
+
|
|
175
|
+
## 🔧 How it works
|
|
176
|
+
|
|
177
|
+
```mermaid
|
|
178
|
+
sequenceDiagram
|
|
179
|
+
participant OC as OpenCode
|
|
180
|
+
participant Plugin as opencode-litellm
|
|
181
|
+
participant LL as LiteLLM proxy
|
|
182
|
+
|
|
183
|
+
OC->>Plugin: config(initial)
|
|
184
|
+
alt provider.litellm configured
|
|
185
|
+
Plugin->>LL: GET /v1/models @ baseURL
|
|
186
|
+
else not configured
|
|
187
|
+
Plugin->>LL: probe :4000, :8000, :8080
|
|
188
|
+
LL-->>Plugin: 200 OK on one
|
|
189
|
+
Plugin->>Plugin: auto-create provider entry
|
|
190
|
+
end
|
|
191
|
+
Plugin->>LL: GET /v1/models (with auth if set)
|
|
192
|
+
LL-->>Plugin: { data: [...models] }
|
|
193
|
+
Plugin->>Plugin: format names, infer modalities, extract owner
|
|
194
|
+
Plugin->>Plugin: bucket each model by transport (chat vs responses)
|
|
195
|
+
Plugin->>OC: merge chat-completions models into provider.litellm
|
|
196
|
+
Plugin->>OC: merge responses models into provider.litellm-responses (lazy)
|
|
197
|
+
OC->>OC: render model picker with all discovered models
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
1. On OpenCode startup the `config` lifecycle hook fires.
|
|
201
|
+
2. If `provider.litellm` exists, its `baseURL` is used. Otherwise common ports are probed.
|
|
202
|
+
3. A health check (`GET /v1/models`) verifies the proxy is reachable and authorized.
|
|
203
|
+
4. Models from the response are converted into OpenCode model entries with `id`, formatted `name`, `organizationOwner`, and inferred `modalities`.
|
|
204
|
+
5. Each model is bucketed by transport — reasoning-tier models (`gpt-5*`, `o1`/`o3`/`o4*`, or anything with `mode === 'responses'`) go into the `litellm-responses` provider; everything else goes into `litellm`. Per-model overrides via `responsesApiModels` / `chatApiModels` win.
|
|
205
|
+
6. Discovered models are merged on top of any user-defined ones — never overwriting them. A model is skipped if its key already exists under **either** provider.
|
|
206
|
+
7. The whole flow is wrapped in a `Promise.race` against a 5 s timeout so a slow proxy never blocks boot.
|
|
207
|
+
|
|
208
|
+
## 📋 Requirements
|
|
209
|
+
|
|
210
|
+
- [OpenCode](https://opencode.ai) ≥ 0.1.x with plugin support (`@opencode-ai/plugin ^1.0.166`)
|
|
211
|
+
- A running [LiteLLM](https://github.com/BerriAI/litellm) proxy:
|
|
212
|
+
```bash
|
|
213
|
+
pip install 'litellm[proxy]'
|
|
214
|
+
litellm --config config.yaml --port 4000
|
|
215
|
+
```
|
|
216
|
+
- Node.js ≥ 20 (or Bun ≥ 1.0)
|
|
217
|
+
|
|
218
|
+
## 📦 Compatibility matrix
|
|
219
|
+
|
|
220
|
+
| LiteLLM version | OpenCode version | Status |
|
|
221
|
+
|---|---|---|
|
|
222
|
+
| ≥ 1.40 | ≥ 0.1.x | ✅ Tested |
|
|
223
|
+
| 1.30 – 1.39 | ≥ 0.1.x | ⚠️ Should work (older `/v1/models` schema) |
|
|
224
|
+
| < 1.30 | any | ❌ Unsupported |
|
|
225
|
+
|
|
226
|
+
## ❓ FAQ
|
|
227
|
+
|
|
228
|
+
<details>
|
|
229
|
+
<summary><b>Why doesn't a model appear in OpenCode after I add it to LiteLLM?</b></summary>
|
|
230
|
+
|
|
231
|
+
OpenCode reads the plugin output once at startup. After updating `litellm config.yaml`, restart **both** LiteLLM and OpenCode to refresh the model list.
|
|
232
|
+
</details>
|
|
233
|
+
|
|
234
|
+
<details>
|
|
235
|
+
<summary><b>Can I use this with a remote LiteLLM proxy?</b></summary>
|
|
236
|
+
|
|
237
|
+
Yes. Set `provider.litellm.options.baseURL` to your remote URL and (optionally) `apiKey`. Auto-detection only probes `localhost`, but explicit configuration works against any URL.
|
|
238
|
+
</details>
|
|
239
|
+
|
|
240
|
+
<details>
|
|
241
|
+
<summary><b>What happens if LiteLLM is offline at startup?</b></summary>
|
|
242
|
+
|
|
243
|
+
The plugin logs a warning and is a no-op. OpenCode starts normally; you just won't see LiteLLM-discovered models until you restart with the proxy up.
|
|
244
|
+
</details>
|
|
245
|
+
|
|
246
|
+
<details>
|
|
247
|
+
<summary><b>Will my hand-curated model entries be overwritten?</b></summary>
|
|
248
|
+
|
|
249
|
+
No. The merge is additive: anything you've already defined under `provider.litellm.models` is preserved exactly as-is. Discovered models are only added if their key isn't already present.
|
|
250
|
+
</details>
|
|
251
|
+
|
|
252
|
+
<details>
|
|
253
|
+
<summary><b>Why is the npm name <code>opencode-plugin-litellm</code> and not <code>opencode-litellm</code>?</b></summary>
|
|
254
|
+
|
|
255
|
+
The unscoped `opencode-litellm` was already published by another author when this project was started. The GitHub repo and exported plugin symbol still use the cleaner `opencode-litellm` name.
|
|
256
|
+
</details>
|
|
257
|
+
|
|
258
|
+
<details>
|
|
259
|
+
<summary><b>Does this work with Ollama through LiteLLM?</b></summary>
|
|
260
|
+
|
|
261
|
+
Yes — anything in your LiteLLM `model_list` shows up, including Ollama, Bedrock, Azure, OpenAI, Anthropic, Google, etc. That's the whole point of LiteLLM.
|
|
262
|
+
</details>
|
|
263
|
+
|
|
264
|
+
<details>
|
|
265
|
+
<summary><b>I get <code>Function tools with reasoning_effort are not supported … in /v1/chat/completions</code> — what do I do?</b></summary>
|
|
266
|
+
|
|
267
|
+
This error comes from OpenAI: their reasoning-tier models (gpt-5, o1, o3, o4) refuse function-tool calls on `/v1/chat/completions` when `reasoning_effort` is set. They require `/v1/responses` instead.
|
|
268
|
+
|
|
269
|
+
As of `0.2.0`, `opencode-litellm` automatically routes those models through a sibling `litellm-responses` provider that uses the Responses API. If your model id doesn't match the heuristic (e.g. you renamed it in LiteLLM), add it explicitly:
|
|
270
|
+
|
|
271
|
+
```jsonc
|
|
272
|
+
"provider": {
|
|
273
|
+
"litellm": {
|
|
274
|
+
"options": {
|
|
275
|
+
"responsesApiModels": ["my-renamed-gpt-5-high"]
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
The model will appear under the **LiteLLM (responses)** provider in the picker; pick it from there and tool-calling will work.
|
|
282
|
+
</details>
|
|
283
|
+
|
|
284
|
+
<details>
|
|
285
|
+
<summary><b>Why are there suddenly two providers (<code>litellm</code> and <code>litellm-responses</code>) in the picker?</b></summary>
|
|
286
|
+
|
|
287
|
+
Same LiteLLM proxy, different transport. `litellm` talks to `/v1/chat/completions`; `litellm-responses` talks to `/v1/responses`. The split is required for OpenAI reasoning models — see the FAQ entry above.
|
|
288
|
+
|
|
289
|
+
The responses provider is created lazily and only appears if at least one discovered model needs it. To collapse everything back into a single provider, set `"transport": "chat"` in `provider.litellm.options` (you'll lose tool-calling on reasoning models in exchange).
|
|
290
|
+
</details>
|
|
291
|
+
|
|
292
|
+
## 🛠️ Development
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
git clone https://github.com/yuseferi/opencode-litellm.git
|
|
296
|
+
cd opencode-litellm
|
|
297
|
+
npm install
|
|
298
|
+
npm run typecheck
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
The project is intentionally tiny:
|
|
302
|
+
|
|
303
|
+
```
|
|
304
|
+
src/
|
|
305
|
+
├── index.ts # Public exports
|
|
306
|
+
├── types/index.ts # LiteLLM API types
|
|
307
|
+
├── utils/
|
|
308
|
+
│ ├── litellm-api.ts # health check, discovery, auto-detect
|
|
309
|
+
│ └── format-model-name.ts # owner extraction, name formatting, categorization
|
|
310
|
+
└── plugin/
|
|
311
|
+
├── index.ts # LiteLLMPlugin entry
|
|
312
|
+
├── config-hook.ts # OpenCode config-lifecycle hook (5 s timeout)
|
|
313
|
+
└── enhance-config.ts # core merge logic
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
See [`CONTRIBUTING.md`](./CONTRIBUTING.md) for the full contributor workflow.
|
|
317
|
+
|
|
318
|
+
## 🗺️ Roadmap
|
|
319
|
+
|
|
320
|
+
- [ ] Optional cost/latency overlay using LiteLLM's `/spend` and `/health` endpoints
|
|
321
|
+
- [ ] In-memory cache with TTL to avoid re-querying on rapid restarts
|
|
322
|
+
- [ ] Model categorization based on `litellm.proxy.config.model_list[].model_info`
|
|
323
|
+
- [ ] Tests with [vitest](https://vitest.dev/)
|
|
324
|
+
- [ ] `chat.params` hook for injecting LiteLLM routing tags / fallbacks
|
|
325
|
+
|
|
326
|
+
Have an idea? [Open an issue](https://github.com/yuseferi/opencode-litellm/issues/new).
|
|
327
|
+
|
|
328
|
+
## 🙏 Acknowledgements
|
|
329
|
+
|
|
330
|
+
Inspired by [`opencode-lmstudio`](https://github.com/agustif/opencode-lmstudio) by [@agustif](https://github.com/agustif) — the architectural blueprint for OpenCode model-discovery plugins.
|
|
74
331
|
|
|
75
|
-
|
|
76
|
-
- the `LITELLM_API_KEY` (or `LITELLM_MASTER_KEY`) environment variable.
|
|
332
|
+
Built on top of [LiteLLM](https://github.com/BerriAI/litellm) by the [BerriAI](https://github.com/BerriAI) team and [OpenCode](https://opencode.ai) by the OpenCode contributors.
|
|
77
333
|
|
|
78
|
-
##
|
|
334
|
+
## 📄 License
|
|
79
335
|
|
|
80
|
-
|
|
81
|
-
2. If a `litellm` provider is configured, its `baseURL` is used. Otherwise the
|
|
82
|
-
plugin probes `localhost:4000`, `:8000`, `:8080`.
|
|
83
|
-
3. Health check via `GET /v1/models`. If unreachable, the hook is a no-op.
|
|
84
|
-
4. Models from `/v1/models` are turned into OpenCode model entries with
|
|
85
|
-
sensible `name`, `organizationOwner`, and `modalities` fields.
|
|
86
|
-
5. Discovered models are merged on top of any user-configured ones.
|
|
336
|
+
[MIT](./LICENSE) © [Yusef Mohamadi](https://github.com/yuseferi)
|
|
87
337
|
|
|
88
|
-
|
|
338
|
+
---
|
|
89
339
|
|
|
90
|
-
|
|
91
|
-
- A running LiteLLM proxy (`pip install 'litellm[proxy]' && litellm --config config.yaml`)
|
|
340
|
+
<div align="center">
|
|
92
341
|
|
|
93
|
-
|
|
342
|
+
If this project saved you time, consider giving it a ⭐ on [GitHub](https://github.com/yuseferi/opencode-litellm).
|
|
94
343
|
|
|
95
|
-
|
|
344
|
+
</div>
|
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": "0.
|
|
4
|
+
"version": "0.2.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",
|
|
@@ -27,11 +27,14 @@ export function createConfigHook() {
|
|
|
27
27
|
console.error('[opencode-litellm] Config enhancement failed:', error)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const chatCount = config?.provider?.litellm?.models
|
|
31
31
|
? Object.keys(config.provider.litellm.models).length
|
|
32
32
|
: 0
|
|
33
|
+
const responsesCount = config?.provider?.['litellm-responses']?.models
|
|
34
|
+
? Object.keys(config.provider['litellm-responses'].models).length
|
|
35
|
+
: 0
|
|
33
36
|
|
|
34
|
-
if (
|
|
37
|
+
if (chatCount + responsesCount === 0 && config?.provider?.litellm) {
|
|
35
38
|
console.warn('[opencode-litellm] No models loaded — is LiteLLM reachable and is your model_list non-empty?')
|
|
36
39
|
}
|
|
37
40
|
}
|
|
@@ -4,10 +4,16 @@ import {
|
|
|
4
4
|
discoverLiteLLMModels,
|
|
5
5
|
normalizeBaseURL,
|
|
6
6
|
} from '../utils/litellm-api'
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
categorizeModel,
|
|
9
|
+
extractModelOwner,
|
|
10
|
+
formatModelName,
|
|
11
|
+
requiresResponsesAPI,
|
|
12
|
+
} from '../utils/format-model-name'
|
|
13
|
+
import type { LiteLLMModel, Transport, TransportPolicy } from '../types'
|
|
9
14
|
|
|
10
|
-
const
|
|
15
|
+
const CHAT_PROVIDER_KEY = 'litellm'
|
|
16
|
+
const RESPONSES_PROVIDER_KEY = 'litellm-responses'
|
|
11
17
|
|
|
12
18
|
/**
|
|
13
19
|
* Build the OpenCode model entry for a single discovered model.
|
|
@@ -50,34 +56,98 @@ function buildModelEntry(model: LiteLLMModel): Record<string, unknown> {
|
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
59
|
+
* Sanitize an id into a key OpenCode can parse. Most LiteLLM ids are
|
|
60
|
+
* already safe (`gpt-4o`, `anthropic/claude-3-5-sonnet`).
|
|
61
|
+
*/
|
|
62
|
+
function safeKey(id: string): string {
|
|
63
|
+
return /^[a-zA-Z0-9/_.\-:]+$/.test(id) ? id : id.replace(/[^a-zA-Z0-9_-]/g, '_')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Decide which transport bucket a model belongs to. Order of
|
|
68
|
+
* precedence (highest first):
|
|
69
|
+
*
|
|
70
|
+
* 1. Explicit allowlist `responsesApiModels` → 'responses'
|
|
71
|
+
* 2. Explicit denylist `chatApiModels` → 'chat'
|
|
72
|
+
* 3. Global policy `transport: 'chat' | 'responses'`
|
|
73
|
+
* 4. Heuristic via {@link requiresResponsesAPI} → 'responses' or 'chat'
|
|
74
|
+
*/
|
|
75
|
+
function pickTransport(
|
|
76
|
+
model: LiteLLMModel,
|
|
77
|
+
policy: TransportPolicy,
|
|
78
|
+
responsesApiModels: ReadonlySet<string>,
|
|
79
|
+
chatApiModels: ReadonlySet<string>,
|
|
80
|
+
): Transport {
|
|
81
|
+
if (responsesApiModels.has(model.id)) return 'responses'
|
|
82
|
+
if (chatApiModels.has(model.id)) return 'chat'
|
|
83
|
+
if (policy === 'chat') return 'chat'
|
|
84
|
+
if (policy === 'responses') return 'responses'
|
|
85
|
+
return requiresResponsesAPI(model) ? 'responses' : 'chat'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Ensure a provider entry exists on `config.provider[key]` and return
|
|
90
|
+
* it. If we created it just now, `created` is true so callers can log
|
|
91
|
+
* one-time messages.
|
|
92
|
+
*/
|
|
93
|
+
function ensureProvider(
|
|
94
|
+
config: any,
|
|
95
|
+
key: string,
|
|
96
|
+
defaults: () => Record<string, unknown>,
|
|
97
|
+
): { provider: any; created: boolean } {
|
|
98
|
+
if (config.provider[key]) {
|
|
99
|
+
return { provider: config.provider[key], created: false }
|
|
100
|
+
}
|
|
101
|
+
const provider = defaults()
|
|
102
|
+
config.provider[key] = provider
|
|
103
|
+
return { provider, created: true }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Mutates `config` in place: ensures the litellm provider(s) exist,
|
|
108
|
+
* fetches all models from the LiteLLM proxy, and merges them into the
|
|
109
|
+
* appropriate transport bucket (`litellm` for /v1/chat/completions,
|
|
110
|
+
* `litellm-responses` for /v1/responses).
|
|
55
111
|
*/
|
|
56
112
|
export async function enhanceConfig(config: any): Promise<void> {
|
|
57
113
|
if (!config) return
|
|
58
114
|
|
|
59
115
|
if (!config.provider) config.provider = {}
|
|
60
|
-
let
|
|
116
|
+
let chatProvider = config.provider[CHAT_PROVIDER_KEY]
|
|
61
117
|
|
|
62
|
-
// Resolve baseURL & apiKey, either from the user's provider
|
|
63
|
-
// or by auto-detecting a running LiteLLM proxy.
|
|
118
|
+
// Resolve baseURL & apiKey, either from the user's primary provider
|
|
119
|
+
// config or by auto-detecting a running LiteLLM proxy.
|
|
64
120
|
let baseURL: string
|
|
65
121
|
let apiKey: string | undefined
|
|
66
122
|
|
|
67
|
-
if (
|
|
68
|
-
baseURL = normalizeBaseURL(
|
|
69
|
-
apiKey =
|
|
123
|
+
if (chatProvider) {
|
|
124
|
+
baseURL = normalizeBaseURL(chatProvider.options?.baseURL ?? 'http://localhost:4000')
|
|
125
|
+
apiKey =
|
|
126
|
+
chatProvider.options?.apiKey ??
|
|
127
|
+
process.env.LITELLM_API_KEY ??
|
|
128
|
+
process.env.LITELLM_MASTER_KEY
|
|
70
129
|
} else {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
130
|
+
// Allow the user to pre-define ONLY the responses provider; in that
|
|
131
|
+
// case we still need to know the baseURL/apiKey for discovery.
|
|
132
|
+
const preDefinedResponses = config.provider[RESPONSES_PROVIDER_KEY]
|
|
133
|
+
if (preDefinedResponses) {
|
|
134
|
+
baseURL = normalizeBaseURL(preDefinedResponses.options?.baseURL ?? 'http://localhost:4000')
|
|
135
|
+
apiKey =
|
|
136
|
+
preDefinedResponses.options?.apiKey ??
|
|
137
|
+
process.env.LITELLM_API_KEY ??
|
|
138
|
+
process.env.LITELLM_MASTER_KEY
|
|
139
|
+
} else {
|
|
140
|
+
const detected = await autoDetectLiteLLM()
|
|
141
|
+
if (!detected) {
|
|
142
|
+
// Nothing to do — LiteLLM doesn't appear to be running anywhere
|
|
143
|
+
// we know about. Silently bail; this is the no-op case.
|
|
144
|
+
return
|
|
145
|
+
}
|
|
146
|
+
baseURL = normalizeBaseURL(detected)
|
|
147
|
+
apiKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
76
148
|
}
|
|
77
|
-
baseURL = normalizeBaseURL(detected)
|
|
78
|
-
apiKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
79
149
|
|
|
80
|
-
|
|
150
|
+
chatProvider = {
|
|
81
151
|
npm: '@ai-sdk/openai-compatible',
|
|
82
152
|
name: 'LiteLLM (proxy)',
|
|
83
153
|
options: {
|
|
@@ -86,7 +156,7 @@ export async function enhanceConfig(config: any): Promise<void> {
|
|
|
86
156
|
},
|
|
87
157
|
models: {},
|
|
88
158
|
}
|
|
89
|
-
config.provider[
|
|
159
|
+
config.provider[CHAT_PROVIDER_KEY] = chatProvider
|
|
90
160
|
}
|
|
91
161
|
|
|
92
162
|
// Verify the server is actually answering before we hammer /v1/models.
|
|
@@ -99,32 +169,92 @@ export async function enhanceConfig(config: any): Promise<void> {
|
|
|
99
169
|
try {
|
|
100
170
|
models = await discoverLiteLLMModels(baseURL, apiKey)
|
|
101
171
|
} catch (error) {
|
|
102
|
-
console.warn(
|
|
172
|
+
console.warn(
|
|
173
|
+
'[opencode-litellm] Model discovery failed:',
|
|
174
|
+
error instanceof Error ? error.message : String(error),
|
|
175
|
+
)
|
|
103
176
|
return
|
|
104
177
|
}
|
|
105
178
|
|
|
106
179
|
if (models.length === 0) {
|
|
107
|
-
console.warn(
|
|
180
|
+
console.warn(
|
|
181
|
+
'[opencode-litellm] LiteLLM responded but exposed zero models. Check your `model_list` in litellm config.yaml',
|
|
182
|
+
)
|
|
108
183
|
return
|
|
109
184
|
}
|
|
110
185
|
|
|
111
|
-
|
|
112
|
-
const
|
|
186
|
+
// Read the routing policy + per-model overrides from the chat provider's options.
|
|
187
|
+
const policy: TransportPolicy =
|
|
188
|
+
(chatProvider.options?.transport as TransportPolicy | undefined) ?? 'auto'
|
|
189
|
+
const responsesApiModels = new Set<string>(
|
|
190
|
+
Array.isArray(chatProvider.options?.responsesApiModels)
|
|
191
|
+
? chatProvider.options.responsesApiModels
|
|
192
|
+
: [],
|
|
193
|
+
)
|
|
194
|
+
const chatApiModels = new Set<string>(
|
|
195
|
+
Array.isArray(chatProvider.options?.chatApiModels) ? chatProvider.options.chatApiModels : [],
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
// First pass: bucket every discovered model by transport.
|
|
199
|
+
const chatBucket: Record<string, any> = {}
|
|
200
|
+
const responsesBucket: Record<string, any> = {}
|
|
113
201
|
|
|
114
202
|
for (const model of models) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
discovered[key] = buildModelEntry(model)
|
|
203
|
+
const key = safeKey(model.id)
|
|
204
|
+
const entry = buildModelEntry(model)
|
|
205
|
+
const transport = pickTransport(model, policy, responsesApiModels, chatApiModels)
|
|
206
|
+
if (transport === 'responses') {
|
|
207
|
+
responsesBucket[key] = entry
|
|
208
|
+
} else {
|
|
209
|
+
chatBucket[key] = entry
|
|
210
|
+
}
|
|
124
211
|
}
|
|
125
212
|
|
|
126
|
-
|
|
213
|
+
// Lazily create the responses provider only when we actually have
|
|
214
|
+
// something to put in it (or the user already declared one).
|
|
215
|
+
let responsesProvider: any | undefined = config.provider[RESPONSES_PROVIDER_KEY]
|
|
216
|
+
if (!responsesProvider && Object.keys(responsesBucket).length > 0) {
|
|
217
|
+
responsesProvider = {
|
|
218
|
+
npm: '@ai-sdk/openai',
|
|
219
|
+
name: 'LiteLLM (responses)',
|
|
220
|
+
options: {
|
|
221
|
+
baseURL: `${baseURL}/v1`,
|
|
222
|
+
...(apiKey ? { apiKey } : {}),
|
|
223
|
+
// Help the AI SDK route to /v1/responses for reasoning models
|
|
224
|
+
// when the upstream supports it.
|
|
225
|
+
compatibility: 'strict',
|
|
226
|
+
},
|
|
227
|
+
models: {},
|
|
228
|
+
}
|
|
229
|
+
config.provider[RESPONSES_PROVIDER_KEY] = responsesProvider
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Non-destructive merge into each provider. A key is considered
|
|
233
|
+
// "already present" if it exists under EITHER provider, so the user's
|
|
234
|
+
// hand-curated entry wins regardless of which bucket we'd have chosen.
|
|
235
|
+
const mergeIntoProvider = (
|
|
236
|
+
provider: any,
|
|
237
|
+
bucket: Record<string, any>,
|
|
238
|
+
bucketLabel: string,
|
|
239
|
+
): number => {
|
|
240
|
+
if (!provider) return 0
|
|
241
|
+
const existingHere: Record<string, any> = provider.models ?? {}
|
|
242
|
+
const existingChat: Record<string, any> = chatProvider.models ?? {}
|
|
243
|
+
const existingResp: Record<string, any> = responsesProvider?.models ?? {}
|
|
244
|
+
const additions: Record<string, any> = {}
|
|
245
|
+
for (const [key, entry] of Object.entries(bucket)) {
|
|
246
|
+
if (existingHere[key]) continue
|
|
247
|
+
if (existingChat[key] || existingResp[key]) continue
|
|
248
|
+
additions[key] = entry
|
|
249
|
+
}
|
|
250
|
+
if (Object.keys(additions).length === 0) return 0
|
|
251
|
+
provider.models = { ...existingHere, ...additions }
|
|
252
|
+
console.log(
|
|
253
|
+
`[opencode-litellm] Discovered ${Object.keys(additions).length} ${bucketLabel} model(s) from LiteLLM at ${baseURL}`,
|
|
254
|
+
)
|
|
255
|
+
return Object.keys(additions).length
|
|
256
|
+
}
|
|
127
257
|
|
|
128
|
-
|
|
129
|
-
|
|
258
|
+
mergeIntoProvider(chatProvider, chatBucket, 'chat-completions')
|
|
259
|
+
mergeIntoProvider(responsesProvider, responsesBucket, 'responses-API')
|
|
130
260
|
}
|
package/src/plugin/index.ts
CHANGED
|
@@ -8,6 +8,16 @@ import { createConfigHook } from './config-hook'
|
|
|
8
8
|
* the full `model_list` out of `/v1/models` so you don't have to
|
|
9
9
|
* hand-maintain models in `opencode.json`.
|
|
10
10
|
*
|
|
11
|
+
* Discovered models are split into two providers based on the transport
|
|
12
|
+
* required by the upstream model:
|
|
13
|
+
*
|
|
14
|
+
* - `litellm` → /v1/chat/completions (most models)
|
|
15
|
+
* - `litellm-responses` → /v1/responses (gpt-5*, o-series with reasoning)
|
|
16
|
+
*
|
|
17
|
+
* The split is necessary because reasoning-tier OpenAI models reject
|
|
18
|
+
* requests that combine `reasoning_effort` with function tools when sent
|
|
19
|
+
* to chat-completions.
|
|
20
|
+
*
|
|
11
21
|
* Configure (optional):
|
|
12
22
|
*
|
|
13
23
|
* {
|
|
@@ -18,7 +28,12 @@ import { createConfigHook } from './config-hook'
|
|
|
18
28
|
* "name": "LiteLLM (proxy)",
|
|
19
29
|
* "options": {
|
|
20
30
|
* "baseURL": "http://localhost:4000/v1",
|
|
21
|
-
* "apiKey": "{env:LITELLM_API_KEY}"
|
|
31
|
+
* "apiKey": "{env:LITELLM_API_KEY}",
|
|
32
|
+
*
|
|
33
|
+
* // Optional routing controls (added in 0.2.0):
|
|
34
|
+
* "transport": "auto", // "auto" | "chat" | "responses"
|
|
35
|
+
* "responsesApiModels": ["gpt-5-4-high"], // force into /v1/responses
|
|
36
|
+
* "chatApiModels": ["o1-mini"] // force into /v1/chat/completions
|
|
22
37
|
* }
|
|
23
38
|
* }
|
|
24
39
|
* }
|
package/src/types/index.ts
CHANGED
|
@@ -18,6 +18,10 @@ export interface LiteLLMModel {
|
|
|
18
18
|
* Optional capability metadata exposed by some LiteLLM versions
|
|
19
19
|
* via `/model/info` (we ignore it for the lean discovery endpoint
|
|
20
20
|
* but keep the type around for forward-compat).
|
|
21
|
+
*
|
|
22
|
+
* Newer LiteLLM versions may expose `'responses'` here for models
|
|
23
|
+
* that must be routed through the OpenAI Responses API rather than
|
|
24
|
+
* `/v1/chat/completions` (e.g. `gpt-5*`, `o1/o3/o4*` with reasoning).
|
|
21
25
|
*/
|
|
22
26
|
mode?: string
|
|
23
27
|
max_tokens?: number
|
|
@@ -34,7 +38,44 @@ export interface LiteLLMModelsResponse {
|
|
|
34
38
|
|
|
35
39
|
export type ModelType = 'chat' | 'embedding' | 'image' | 'audio' | 'unknown'
|
|
36
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Which OpenAI-compatible HTTP surface a model should be invoked through.
|
|
43
|
+
*
|
|
44
|
+
* - `chat` → `/v1/chat/completions` (most models)
|
|
45
|
+
* - `responses` → `/v1/responses` (gpt-5*, o-series with reasoning)
|
|
46
|
+
*/
|
|
47
|
+
export type Transport = 'chat' | 'responses'
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* User-facing routing override. Defaults to `'auto'`.
|
|
51
|
+
*
|
|
52
|
+
* - `'auto'` → use the heuristic + LiteLLM `mode` field
|
|
53
|
+
* - `'chat'` → force every discovered model into the chat-completions provider
|
|
54
|
+
* - `'responses'` → force every discovered model into the responses provider
|
|
55
|
+
*/
|
|
56
|
+
export type TransportPolicy = 'auto' | Transport
|
|
57
|
+
|
|
37
58
|
export interface LiteLLMOptions {
|
|
38
59
|
baseURL?: string
|
|
39
60
|
apiKey?: string
|
|
61
|
+
/**
|
|
62
|
+
* Routing policy for discovered models. See {@link TransportPolicy}.
|
|
63
|
+
* Defaults to `'auto'`.
|
|
64
|
+
*/
|
|
65
|
+
transport?: TransportPolicy
|
|
66
|
+
/**
|
|
67
|
+
* Explicit allowlist of model ids that MUST be routed through the
|
|
68
|
+
* OpenAI Responses API (`/v1/responses`). Takes priority over the
|
|
69
|
+
* heuristic and over the `transport` policy.
|
|
70
|
+
*
|
|
71
|
+
* Match is exact against the LiteLLM model id (e.g. `"gpt-5-4-high"`).
|
|
72
|
+
*/
|
|
73
|
+
responsesApiModels?: string[]
|
|
74
|
+
/**
|
|
75
|
+
* Explicit denylist of model ids that MUST be routed through chat
|
|
76
|
+
* completions (`/v1/chat/completions`), even if the heuristic would
|
|
77
|
+
* otherwise put them in the responses bucket. Takes priority over
|
|
78
|
+
* the heuristic but is overridden by `responsesApiModels`.
|
|
79
|
+
*/
|
|
80
|
+
chatApiModels?: string[]
|
|
40
81
|
}
|
|
@@ -83,3 +83,32 @@ export function formatModelName(model: LiteLLMModel): string {
|
|
|
83
83
|
|
|
84
84
|
return tokens || id
|
|
85
85
|
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Decide whether a model must be invoked through the OpenAI Responses
|
|
89
|
+
* API (`/v1/responses`) instead of `/v1/chat/completions`.
|
|
90
|
+
*
|
|
91
|
+
* OpenAI's reasoning-tier models (gpt-5*, o1/o3/o4*) reject requests
|
|
92
|
+
* that combine `reasoning_effort` with function tools when sent to
|
|
93
|
+
* `/v1/chat/completions`. The Responses API has no such restriction,
|
|
94
|
+
* so we route those models there.
|
|
95
|
+
*
|
|
96
|
+
* Detection rules (first match wins):
|
|
97
|
+
* 1. `model.mode === 'responses'` (LiteLLM exposes this on newer versions)
|
|
98
|
+
* 2. The model id (after any `provider/` prefix) matches the
|
|
99
|
+
* `gpt-5*` / `o[134]*` family.
|
|
100
|
+
*/
|
|
101
|
+
export function requiresResponsesAPI(model: LiteLLMModel): boolean {
|
|
102
|
+
if (model.mode && model.mode.toLowerCase() === 'responses') return true
|
|
103
|
+
|
|
104
|
+
const id = model.id.toLowerCase()
|
|
105
|
+
const slashIdx = id.indexOf('/')
|
|
106
|
+
const tail = slashIdx >= 0 ? id.slice(slashIdx + 1) : id
|
|
107
|
+
|
|
108
|
+
// gpt-5, gpt-5.4, gpt-5-4-high, gpt-5o, ...
|
|
109
|
+
if (/^gpt-?5(?:[-.].*)?$/.test(tail)) return true
|
|
110
|
+
// o1, o1-mini, o3, o3-mini, o4-mini, o4, ... but NOT "openai", "ollama"
|
|
111
|
+
if (/^o[134](?:[-.].*)?$/.test(tail)) return true
|
|
112
|
+
|
|
113
|
+
return false
|
|
114
|
+
}
|