opencode-plugin-litellm 1.2.0 → 1.3.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 +36 -3
- package/package.json +1 -1
- package/src/plugin/index.ts +39 -11
- package/src/utils/model-cache.ts +14 -8
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ opencode
|
|
|
71
71
|
| 🔍 **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
72
72
|
| 📡 **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
73
73
|
| ⚡ **Instant startup (SWR)** | Discovered models are cached on disk and loaded synchronously — startup never blocks on the network. A background refresh on new sessions keeps the cache fresh; entries expire after 7 days. |
|
|
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`. |
|
|
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`. Set `formatModelNames: false` to keep the raw LiteLLM ids instead. |
|
|
75
75
|
| 🧠 **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
76
|
| 💵 **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
77
|
| 🧩 **Reasoning-effort variants** | When LiteLLM reports per-model effort support (`supports_low_reasoning_effort`, …), the plugin surfaces each level as a picker variant automatically. |
|
|
@@ -306,6 +306,28 @@ Model classification (tool-call badge, attachments, reasoning, input modalities)
|
|
|
306
306
|
- Overridden flags flow into the picker exactly like natively reported ones, and the adjusted view is what gets persisted to the model cache.
|
|
307
307
|
- 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
308
|
|
|
309
|
+
### Keeping raw model ids in the picker (`formatModelNames`)
|
|
310
|
+
|
|
311
|
+
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:
|
|
312
|
+
|
|
313
|
+
```jsonc
|
|
314
|
+
{
|
|
315
|
+
"provider": {
|
|
316
|
+
"litellm": {
|
|
317
|
+
"options": {
|
|
318
|
+
"baseURL": "http://localhost:4000/v1",
|
|
319
|
+
"formatModelNames": false
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
- 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.
|
|
327
|
+
- Only a boolean `false` disables formatting; omitting the option or setting anything else keeps the default.
|
|
328
|
+
- 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.
|
|
329
|
+
- 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).
|
|
330
|
+
|
|
309
331
|
## 🔧 How it works
|
|
310
332
|
|
|
311
333
|
```mermaid
|
|
@@ -337,7 +359,7 @@ sequenceDiagram
|
|
|
337
359
|
Plugin->>Cache: persist for next startup
|
|
338
360
|
end
|
|
339
361
|
Note over Plugin,Cache: on session.created, revalidate cache in<br/>the background (throttled to 5 min)
|
|
340
|
-
OC->>OC: render model picker with all discovered models
|
|
362
|
+
OC->>OC: render model picker with all discovered models (CLI)
|
|
341
363
|
```
|
|
342
364
|
|
|
343
365
|
1. On OpenCode startup the `config` lifecycle hook fires.
|
|
@@ -348,6 +370,8 @@ sequenceDiagram
|
|
|
348
370
|
6. Discovered models are merged on top of any user-defined ones — never overwriting them — and persisted to the cache.
|
|
349
371
|
7. On every `session.created` event the cache is revalidated in the background (throttled to once per 5 minutes); refreshed entries surface on the next OpenCode start. The whole cold path is capped by a 20 s timeout so a slow proxy never blocks boot.
|
|
350
372
|
|
|
373
|
+
> **OpenCode Desktop:** Dynamic LiteLLM models currently appear in the CLI but may not appear in the Desktop model picker. This is an observed OpenCode Desktop config-hook limitation: plugin mutations are not reflected in the provider state used by the picker. The plugin cannot safely work around this without persisting resolved configuration and credentials. Track [issue #5](https://github.com/yuseferi/opencode-litellm/issues/5) for updates.
|
|
374
|
+
|
|
351
375
|
## 📋 Requirements
|
|
352
376
|
|
|
353
377
|
- [OpenCode](https://opencode.ai) ≥ 0.1.x with plugin support (`@opencode-ai/plugin ^1.14.0`)
|
|
@@ -380,6 +404,15 @@ delete the cache directory (`~/.cache/opencode-litellm/`, or
|
|
|
380
404
|
`$XDG_CACHE_HOME/opencode-litellm/`).
|
|
381
405
|
</details>
|
|
382
406
|
|
|
407
|
+
<details>
|
|
408
|
+
<summary><b>Why do discovered models appear in the CLI but not OpenCode Desktop?</b></summary>
|
|
409
|
+
|
|
410
|
+
OpenCode Desktop currently does not reflect mutations made by a plugin's
|
|
411
|
+
`config` hook in the provider state used by its model picker. The plugin works
|
|
412
|
+
as expected in the CLI, but there is no safe plugin-side workaround yet. This
|
|
413
|
+
is being tracked in [issue #5](https://github.com/yuseferi/opencode-litellm/issues/5).
|
|
414
|
+
</details>
|
|
415
|
+
|
|
383
416
|
<details>
|
|
384
417
|
<summary><b>Can I use this with a remote LiteLLM proxy?</b></summary>
|
|
385
418
|
|
|
@@ -474,7 +507,7 @@ src/
|
|
|
474
507
|
│ ├── model-capabilities.ts # per-model capability flag overrides
|
|
475
508
|
│ └── opencode-auth.ts # fallback to OpenCode's /connect-stored credentials
|
|
476
509
|
└── plugin/
|
|
477
|
-
└── index.ts # LiteLLMPlugin entry (config hook, enrichment, filtering, capability overrides)
|
|
510
|
+
└── index.ts # LiteLLMPlugin entry (config hook, enrichment, filtering, capability overrides, naming)
|
|
478
511
|
|
|
479
512
|
test/ # vitest suite for the pure logic
|
|
480
513
|
```
|
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.3.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",
|
package/src/plugin/index.ts
CHANGED
|
@@ -81,6 +81,7 @@ interface RefreshContext {
|
|
|
81
81
|
customHeaders?: Record<string, string>
|
|
82
82
|
filters: ModelFilters
|
|
83
83
|
capabilities: ModelCapabilities
|
|
84
|
+
formatModelNames: boolean
|
|
84
85
|
providerId: string
|
|
85
86
|
}
|
|
86
87
|
const refreshContexts = new Map<string, RefreshContext>()
|
|
@@ -156,6 +157,10 @@ function readModelFilters(options: Record<string, unknown>): ModelFilters {
|
|
|
156
157
|
}
|
|
157
158
|
}
|
|
158
159
|
|
|
160
|
+
function readFormatModelNames(options: Record<string, unknown>): boolean {
|
|
161
|
+
return options.formatModelNames !== false
|
|
162
|
+
}
|
|
163
|
+
|
|
159
164
|
/**
|
|
160
165
|
* Overlay metadata onto a `/v1/models` entry in three tiers: the entry's
|
|
161
166
|
* own fields win, `/v1/model/info` fills gaps (notably `mode`, which
|
|
@@ -207,17 +212,21 @@ const USD_PER_TOKEN_TO_PER_MILLION = 1_000_000
|
|
|
207
212
|
* `opencode.json`). Returns `null` for non-chat models (embedding,
|
|
208
213
|
* image, audio) — they can't be used as primary chat models and would
|
|
209
214
|
* clutter the picker.
|
|
215
|
+
*
|
|
216
|
+
* Exported for tests: `formatModelNames` decides whether the display
|
|
217
|
+
* name is the prettified id or the raw `/v1/models` id verbatim.
|
|
210
218
|
*/
|
|
211
|
-
function toConfigModel(
|
|
219
|
+
export function toConfigModel(
|
|
212
220
|
model: LiteLLMModel,
|
|
213
221
|
info?: LiteLLMModelInfo,
|
|
222
|
+
formatModelNames = true,
|
|
214
223
|
): Record<string, unknown> | null {
|
|
215
224
|
const type = categorizeModel(model)
|
|
216
225
|
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
217
226
|
return null
|
|
218
227
|
}
|
|
219
228
|
const entry: Record<string, unknown> = {
|
|
220
|
-
name: formatModelName(model),
|
|
229
|
+
name: formatModelNames ? formatModelName(model) : model.id,
|
|
221
230
|
}
|
|
222
231
|
// Some deployments only report the OpenAI-style `max_tokens` total;
|
|
223
232
|
// use it as the context limit when `max_input_tokens` is absent.
|
|
@@ -277,10 +286,11 @@ function toConfigModel(
|
|
|
277
286
|
*
|
|
278
287
|
* Pure with respect to plugin config: it performs the network calls,
|
|
279
288
|
* classifies + formats each model, and returns a `{ id -> entry }` map.
|
|
280
|
-
* The provider's `includeModels`/`excludeModels` filters
|
|
281
|
-
* `modelCapabilities` overrides
|
|
282
|
-
*
|
|
283
|
-
* background refresh — writes the
|
|
289
|
+
* The provider's `includeModels`/`excludeModels` filters,
|
|
290
|
+
* `modelCapabilities` overrides and `formatModelNames` choice are
|
|
291
|
+
* applied here (not at merge time) so every path that persists or
|
|
292
|
+
* serves a cache — cold discovery and background refresh — writes the
|
|
293
|
+
* same adjusted view.
|
|
284
294
|
*
|
|
285
295
|
* Returns `null` when the proxy is unreachable/unauthorized or exposes
|
|
286
296
|
* no models, so callers can distinguish "no data" from "empty result".
|
|
@@ -292,6 +302,7 @@ async function discoverModels(
|
|
|
292
302
|
providerId: string,
|
|
293
303
|
filters: ModelFilters = {},
|
|
294
304
|
capabilities: ModelCapabilities = {},
|
|
305
|
+
formatModelNames = true,
|
|
295
306
|
): Promise<Record<string, unknown> | null> {
|
|
296
307
|
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
297
308
|
log(
|
|
@@ -363,7 +374,11 @@ async function discoverModels(
|
|
|
363
374
|
}
|
|
364
375
|
const info = infoByName?.get(model.id)
|
|
365
376
|
if (infoByName && !info) unmatched.push(model.id)
|
|
366
|
-
const entry = toConfigModel(
|
|
377
|
+
const entry = toConfigModel(
|
|
378
|
+
enrichModel(model, info, capabilities[model.id]),
|
|
379
|
+
info,
|
|
380
|
+
formatModelNames,
|
|
381
|
+
)
|
|
367
382
|
if (!entry) {
|
|
368
383
|
skipped++
|
|
369
384
|
continue
|
|
@@ -451,6 +466,7 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
451
466
|
ctx.providerId,
|
|
452
467
|
ctx.filters,
|
|
453
468
|
ctx.capabilities,
|
|
469
|
+
ctx.formatModelNames,
|
|
454
470
|
),
|
|
455
471
|
DISCOVERY_TIMEOUT_MS,
|
|
456
472
|
)
|
|
@@ -553,6 +569,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
553
569
|
const customHeaders = readCustomHeaders(options)
|
|
554
570
|
const filters = readModelFilters(options)
|
|
555
571
|
const capabilities = parseModelCapabilities(options.modelCapabilities)
|
|
572
|
+
const formatModelNames = readFormatModelNames(options)
|
|
556
573
|
|
|
557
574
|
// Resolve base URL
|
|
558
575
|
let baseURL: string | null = null
|
|
@@ -608,10 +625,12 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
608
625
|
|
|
609
626
|
const models = actualProvider.models as Record<string, unknown>
|
|
610
627
|
|
|
611
|
-
// Identity includes the filter/capability config: those
|
|
612
|
-
// baked into cached entries, so changing them must start a
|
|
628
|
+
// Identity includes the filter/capability/naming config: those
|
|
629
|
+
// are baked into cached entries, so changing them must start a
|
|
613
630
|
// fresh discovery instead of serving the old adjusted view.
|
|
614
|
-
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities
|
|
631
|
+
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities, {
|
|
632
|
+
formatModelNames,
|
|
633
|
+
})
|
|
615
634
|
|
|
616
635
|
// Remember how to reach this proxy so the `event` hook can
|
|
617
636
|
// revalidate its cache in the background on new sessions.
|
|
@@ -621,6 +640,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
621
640
|
customHeaders,
|
|
622
641
|
filters,
|
|
623
642
|
capabilities,
|
|
643
|
+
formatModelNames,
|
|
624
644
|
providerId,
|
|
625
645
|
})
|
|
626
646
|
|
|
@@ -654,7 +674,15 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
654
674
|
// persist for subsequent startups. Capped by a timeout so a slow
|
|
655
675
|
// proxy never blocks boot.
|
|
656
676
|
const built = await withTimeout(
|
|
657
|
-
discoverModels(
|
|
677
|
+
discoverModels(
|
|
678
|
+
baseURL,
|
|
679
|
+
apiKey,
|
|
680
|
+
customHeaders,
|
|
681
|
+
providerId,
|
|
682
|
+
filters,
|
|
683
|
+
capabilities,
|
|
684
|
+
formatModelNames,
|
|
685
|
+
),
|
|
658
686
|
DISCOVERY_TIMEOUT_MS,
|
|
659
687
|
)
|
|
660
688
|
if (built && Object.keys(built).length > 0) {
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -65,25 +65,28 @@ function canonicalize(value: unknown): string {
|
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Cache identity for a provider's model view. `providerId@baseURL`
|
|
68
|
-
* alone isn't enough: `includeModels`/`excludeModels
|
|
69
|
-
* `modelCapabilities` are baked into cached
|
|
70
|
-
* config change must not reuse the old cache
|
|
71
|
-
* the previous adjusted view until a second
|
|
72
|
-
* appended whenever adjustments exist
|
|
73
|
-
* configs keep the plain key so
|
|
74
|
-
* plugin upgrades.
|
|
68
|
+
* alone isn't enough: `includeModels`/`excludeModels`,
|
|
69
|
+
* `modelCapabilities` and `formatModelNames` are baked into cached
|
|
70
|
+
* entries by discovery, so a config change must not reuse the old cache
|
|
71
|
+
* — it would keep serving the previous adjusted view until a second
|
|
72
|
+
* restart. A fingerprint is appended whenever adjustments exist
|
|
73
|
+
* (pattern order ignored); default configs keep the plain key so
|
|
74
|
+
* existing caches stay warm across plugin upgrades.
|
|
75
75
|
*/
|
|
76
76
|
export function buildCacheKey(
|
|
77
77
|
providerId: string,
|
|
78
78
|
baseURL: string,
|
|
79
79
|
filters: { includeModels?: string[]; excludeModels?: string[] },
|
|
80
80
|
capabilities: Record<string, Record<string, boolean>>,
|
|
81
|
+
naming: { formatModelNames?: boolean } = {},
|
|
81
82
|
): string {
|
|
82
83
|
const base = `${providerId}@${baseURL}`
|
|
84
|
+
const rawNames = naming.formatModelNames === false
|
|
83
85
|
const hasAdjustments =
|
|
84
86
|
(filters.includeModels?.length ?? 0) > 0 ||
|
|
85
87
|
(filters.excludeModels?.length ?? 0) > 0 ||
|
|
86
|
-
Object.keys(capabilities).length > 0
|
|
88
|
+
Object.keys(capabilities).length > 0 ||
|
|
89
|
+
rawNames
|
|
87
90
|
if (!hasAdjustments) return base
|
|
88
91
|
const fingerprint = createHash('sha256')
|
|
89
92
|
.update(
|
|
@@ -91,6 +94,9 @@ export function buildCacheKey(
|
|
|
91
94
|
includeModels: filters.includeModels ? [...filters.includeModels].sort() : undefined,
|
|
92
95
|
excludeModels: filters.excludeModels ? [...filters.excludeModels].sort() : undefined,
|
|
93
96
|
capabilities,
|
|
97
|
+
// Omitted (not `true`) when formatting is on, so keys for
|
|
98
|
+
// configs that never touch this option are unchanged.
|
|
99
|
+
formatModelNames: rawNames ? false : undefined,
|
|
94
100
|
}),
|
|
95
101
|
)
|
|
96
102
|
.digest('hex')
|