opencode-plugin-litellm 1.1.0 → 1.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 +29 -1
- package/package.json +1 -1
- package/src/plugin/index.ts +70 -32
- package/src/utils/index.ts +1 -0
- package/src/utils/litellm-api.ts +2 -8
- package/src/utils/model-cache.ts +51 -0
- package/src/utils/model-capabilities.ts +50 -0
package/README.md
CHANGED
|
@@ -78,6 +78,7 @@ opencode
|
|
|
78
78
|
| 🔐 **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars, `provider.litellm.options.apiKey`, or the key you stored via OpenCode's `/connect`. |
|
|
79
79
|
| 🌐 **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways requiring extra HTTP headers. |
|
|
80
80
|
| 🧩 **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
|
+
| 🎚️ **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. |
|
|
81
82
|
| ⏱️ **Non-blocking startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** (configurable via `LITELLM_REQUEST_TIMEOUT_MS`) for slow remote proxies. Repeat config-hook invocations are a no-op. |
|
|
82
83
|
| 📝 **TUI-safe logging** | All plugin logs go through OpenCode's log API (into OpenCode's own log files), never to stdout — the TUI stays intact. |
|
|
83
84
|
| 🤝 **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
@@ -280,6 +281,31 @@ If your LiteLLM catalog mixes naming conventions from different teams or environ
|
|
|
280
281
|
- Patterns support only `*` (any run of characters); everything else is matched literally, so dots in ids like `gpt-4.1` need no escaping.
|
|
281
282
|
- Filtering happens before the on-disk cache is written, so each provider's cached view respects its own filters.
|
|
282
283
|
|
|
284
|
+
### Correcting capability flags (`modelCapabilities`)
|
|
285
|
+
|
|
286
|
+
Model classification (tool-call badge, attachments, reasoning, input modalities) leans on the capability flags LiteLLM reports via `/v1/model/info`. If your proxy doesn't expose that endpoint, or reports a flag that doesn't match your deployment, override flags per model id:
|
|
287
|
+
|
|
288
|
+
```jsonc
|
|
289
|
+
{
|
|
290
|
+
"provider": {
|
|
291
|
+
"litellm": {
|
|
292
|
+
"options": {
|
|
293
|
+
"baseURL": "http://localhost:4000/v1",
|
|
294
|
+
"modelCapabilities": {
|
|
295
|
+
"te-gpt-5.4-mini": { "supports_function_calling": true },
|
|
296
|
+
"openai/gpt-4o": { "supports_vision": false }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
- 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
|
+
- Keys are exact model ids as they appear in `/v1/models` (not globs).
|
|
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
|
+
- 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
|
+
|
|
283
309
|
## 🔧 How it works
|
|
284
310
|
|
|
285
311
|
```mermaid
|
|
@@ -444,9 +470,11 @@ src/
|
|
|
444
470
|
│ ├── litellm-api.ts # health check, discovery (/v1/models + /v1/model/info), auto-detect
|
|
445
471
|
│ ├── format-model-name.ts # name formatting, categorization
|
|
446
472
|
│ ├── model-cache.ts # stale-while-revalidate on-disk model cache
|
|
473
|
+
│ ├── model-filter.ts # includeModels/excludeModels glob filtering
|
|
474
|
+
│ ├── model-capabilities.ts # per-model capability flag overrides
|
|
447
475
|
│ └── opencode-auth.ts # fallback to OpenCode's /connect-stored credentials
|
|
448
476
|
└── plugin/
|
|
449
|
-
└── index.ts # LiteLLMPlugin entry (config hook, enrichment, filtering)
|
|
477
|
+
└── index.ts # LiteLLMPlugin entry (config hook, enrichment, filtering, capability overrides)
|
|
450
478
|
|
|
451
479
|
test/ # vitest suite for the pure logic
|
|
452
480
|
```
|
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.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",
|
package/src/plugin/index.ts
CHANGED
|
@@ -13,9 +13,16 @@ import {
|
|
|
13
13
|
} from '../utils/format-model-name'
|
|
14
14
|
import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
|
|
15
15
|
import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
buildCacheKey,
|
|
18
|
+
readModelCache,
|
|
19
|
+
writeModelCache,
|
|
20
|
+
readModelCacheSavedAt,
|
|
21
|
+
} from '../utils/model-cache'
|
|
17
22
|
import { passesModelFilter } from '../utils/model-filter'
|
|
18
23
|
import type { ModelFilters } from '../utils/model-filter'
|
|
24
|
+
import { applyCapabilityOverrides, parseModelCapabilities } from '../utils/model-capabilities'
|
|
25
|
+
import type { ModelCapabilities } from '../utils/model-capabilities'
|
|
19
26
|
|
|
20
27
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
21
28
|
// Covers the 3 s health check plus the parallel models/model-info fetch
|
|
@@ -73,6 +80,7 @@ interface RefreshContext {
|
|
|
73
80
|
apiKey?: string
|
|
74
81
|
customHeaders?: Record<string, string>
|
|
75
82
|
filters: ModelFilters
|
|
83
|
+
capabilities: ModelCapabilities
|
|
76
84
|
providerId: string
|
|
77
85
|
}
|
|
78
86
|
const refreshContexts = new Map<string, RefreshContext>()
|
|
@@ -149,29 +157,40 @@ function readModelFilters(options: Record<string, unknown>): ModelFilters {
|
|
|
149
157
|
}
|
|
150
158
|
|
|
151
159
|
/**
|
|
152
|
-
* Overlay metadata
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
160
|
+
* Overlay metadata onto a `/v1/models` entry in three tiers: the entry's
|
|
161
|
+
* own fields win, `/v1/model/info` fills gaps (notably `mode`, which
|
|
162
|
+
* `/v1/models` omits for database-defined models), and finally
|
|
163
|
+
* user-configured `modelCapabilities` overrides apply — explicit
|
|
164
|
+
* `false` included — because the user knows their deployment better
|
|
165
|
+
* than either endpoint (issue #25).
|
|
156
166
|
*/
|
|
157
|
-
function enrichModel(
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
model.
|
|
174
|
-
|
|
167
|
+
function enrichModel(
|
|
168
|
+
model: LiteLLMModel,
|
|
169
|
+
info: LiteLLMModelInfo | undefined,
|
|
170
|
+
overrides?: Record<string, boolean>,
|
|
171
|
+
): LiteLLMModel {
|
|
172
|
+
return applyCapabilityOverrides(
|
|
173
|
+
{
|
|
174
|
+
...model,
|
|
175
|
+
mode: model.mode ?? info?.mode,
|
|
176
|
+
max_tokens: model.max_tokens ?? info?.max_tokens,
|
|
177
|
+
max_input_tokens: model.max_input_tokens ?? info?.max_input_tokens,
|
|
178
|
+
max_output_tokens: model.max_output_tokens ?? info?.max_output_tokens,
|
|
179
|
+
supports_function_calling:
|
|
180
|
+
model.supports_function_calling ?? info?.supports_function_calling,
|
|
181
|
+
supports_vision: model.supports_vision ?? info?.supports_vision,
|
|
182
|
+
supports_reasoning: model.supports_reasoning ?? info?.supports_reasoning,
|
|
183
|
+
supports_pdf_input: model.supports_pdf_input ?? info?.supports_pdf_input,
|
|
184
|
+
supports_audio_input: model.supports_audio_input ?? info?.supports_audio_input,
|
|
185
|
+
input_cost_per_token: model.input_cost_per_token ?? info?.input_cost_per_token,
|
|
186
|
+
output_cost_per_token: model.output_cost_per_token ?? info?.output_cost_per_token,
|
|
187
|
+
cache_read_input_token_cost:
|
|
188
|
+
model.cache_read_input_token_cost ?? info?.cache_read_input_token_cost,
|
|
189
|
+
cache_creation_input_token_cost:
|
|
190
|
+
model.cache_creation_input_token_cost ?? info?.cache_creation_input_token_cost,
|
|
191
|
+
},
|
|
192
|
+
overrides,
|
|
193
|
+
)
|
|
175
194
|
}
|
|
176
195
|
|
|
177
196
|
/**
|
|
@@ -258,10 +277,10 @@ function toConfigModel(
|
|
|
258
277
|
*
|
|
259
278
|
* Pure with respect to plugin config: it performs the network calls,
|
|
260
279
|
* classifies + formats each model, and returns a `{ id -> entry }` map.
|
|
261
|
-
* The provider's `includeModels`/`excludeModels` filters
|
|
262
|
-
* here (not at merge time) so
|
|
263
|
-
*
|
|
264
|
-
*
|
|
280
|
+
* The provider's `includeModels`/`excludeModels` filters and
|
|
281
|
+
* `modelCapabilities` overrides are applied here (not at merge time) so
|
|
282
|
+
* every path that persists or serves a cache — cold discovery and
|
|
283
|
+
* background refresh — writes the same adjusted view.
|
|
265
284
|
*
|
|
266
285
|
* Returns `null` when the proxy is unreachable/unauthorized or exposes
|
|
267
286
|
* no models, so callers can distinguish "no data" from "empty result".
|
|
@@ -272,6 +291,7 @@ async function discoverModels(
|
|
|
272
291
|
customHeaders: Record<string, string> | undefined,
|
|
273
292
|
providerId: string,
|
|
274
293
|
filters: ModelFilters = {},
|
|
294
|
+
capabilities: ModelCapabilities = {},
|
|
275
295
|
): Promise<Record<string, unknown> | null> {
|
|
276
296
|
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
277
297
|
log(
|
|
@@ -343,7 +363,7 @@ async function discoverModels(
|
|
|
343
363
|
}
|
|
344
364
|
const info = infoByName?.get(model.id)
|
|
345
365
|
if (infoByName && !info) unmatched.push(model.id)
|
|
346
|
-
const entry = toConfigModel(
|
|
366
|
+
const entry = toConfigModel(enrichModel(model, info, capabilities[model.id]), info)
|
|
347
367
|
if (!entry) {
|
|
348
368
|
skipped++
|
|
349
369
|
continue
|
|
@@ -424,7 +444,14 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
424
444
|
refreshInFlight.add(cacheKey)
|
|
425
445
|
try {
|
|
426
446
|
const built = await withTimeout(
|
|
427
|
-
discoverModels(
|
|
447
|
+
discoverModels(
|
|
448
|
+
ctx.baseURL,
|
|
449
|
+
ctx.apiKey,
|
|
450
|
+
ctx.customHeaders,
|
|
451
|
+
ctx.providerId,
|
|
452
|
+
ctx.filters,
|
|
453
|
+
ctx.capabilities,
|
|
454
|
+
),
|
|
428
455
|
DISCOVERY_TIMEOUT_MS,
|
|
429
456
|
)
|
|
430
457
|
if (built && Object.keys(built).length > 0) {
|
|
@@ -525,6 +552,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
525
552
|
const apiKey = configuredKey ?? envKey ?? storedKey
|
|
526
553
|
const customHeaders = readCustomHeaders(options)
|
|
527
554
|
const filters = readModelFilters(options)
|
|
555
|
+
const capabilities = parseModelCapabilities(options.modelCapabilities)
|
|
528
556
|
|
|
529
557
|
// Resolve base URL
|
|
530
558
|
let baseURL: string | null = null
|
|
@@ -580,11 +608,21 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
580
608
|
|
|
581
609
|
const models = actualProvider.models as Record<string, unknown>
|
|
582
610
|
|
|
583
|
-
|
|
611
|
+
// Identity includes the filter/capability config: those are
|
|
612
|
+
// baked into cached entries, so changing them must start a
|
|
613
|
+
// fresh discovery instead of serving the old adjusted view.
|
|
614
|
+
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities)
|
|
584
615
|
|
|
585
616
|
// Remember how to reach this proxy so the `event` hook can
|
|
586
617
|
// revalidate its cache in the background on new sessions.
|
|
587
|
-
refreshContexts.set(cacheKey, {
|
|
618
|
+
refreshContexts.set(cacheKey, {
|
|
619
|
+
baseURL,
|
|
620
|
+
apiKey,
|
|
621
|
+
customHeaders,
|
|
622
|
+
filters,
|
|
623
|
+
capabilities,
|
|
624
|
+
providerId,
|
|
625
|
+
})
|
|
588
626
|
|
|
589
627
|
// Repeat config-hook invocations within a run are a no-op once
|
|
590
628
|
// we've injected this provider's models.
|
|
@@ -616,7 +654,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
616
654
|
// persist for subsequent startups. Capped by a timeout so a slow
|
|
617
655
|
// proxy never blocks boot.
|
|
618
656
|
const built = await withTimeout(
|
|
619
|
-
discoverModels(baseURL, apiKey, customHeaders, providerId, filters),
|
|
657
|
+
discoverModels(baseURL, apiKey, customHeaders, providerId, filters, capabilities),
|
|
620
658
|
DISCOVERY_TIMEOUT_MS,
|
|
621
659
|
)
|
|
622
660
|
if (built && Object.keys(built).length > 0) {
|
package/src/utils/index.ts
CHANGED
package/src/utils/litellm-api.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { LiteLLMModel, LiteLLMModelInfo, LiteLLMModelInfoResponse, LiteLLMModelsResponse } from '../types'
|
|
2
|
+
import { CAPABILITY_FLAGS } from './model-capabilities'
|
|
2
3
|
|
|
3
4
|
export const DEFAULT_LITELLM_URL = 'http://localhost:4000'
|
|
4
5
|
const MODELS_ENDPOINT = '/v1/models'
|
|
@@ -128,14 +129,7 @@ export async function discoverLiteLLMModelInfo(
|
|
|
128
129
|
// Some deployments set capability flags on the params block rather
|
|
129
130
|
// than inside model_info. Fill those gaps so enrichment sees them.
|
|
130
131
|
const info: LiteLLMModelInfo = { ...entry.model_info }
|
|
131
|
-
const
|
|
132
|
-
'supports_vision',
|
|
133
|
-
'supports_function_calling',
|
|
134
|
-
'supports_reasoning',
|
|
135
|
-
'supports_pdf_input',
|
|
136
|
-
'supports_audio_input',
|
|
137
|
-
] as const
|
|
138
|
-
for (const flag of capabilityFlags) {
|
|
132
|
+
for (const flag of CAPABILITY_FLAGS) {
|
|
139
133
|
const paramsValue = entry.litellm_params?.[flag]
|
|
140
134
|
if (info[flag] == null && typeof paramsValue === 'boolean') {
|
|
141
135
|
info[flag] = paramsValue
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -47,6 +47,57 @@ function cacheFile(cacheKey: string): string {
|
|
|
47
47
|
return join(cacheDir(), `models-${hash}.json`)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Canonical JSON serialization (object keys sorted) so semantically
|
|
52
|
+
* identical filter/capability configs fingerprint identically no matter
|
|
53
|
+
* the key order in the user's `opencode.json`.
|
|
54
|
+
*/
|
|
55
|
+
function canonicalize(value: unknown): string {
|
|
56
|
+
if (Array.isArray(value)) return `[${value.map(canonicalize).join(',')}]`
|
|
57
|
+
if (value && typeof value === 'object') {
|
|
58
|
+
const entries = Object.entries(value as Record<string, unknown>)
|
|
59
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
60
|
+
.map(([k, v]) => `${JSON.stringify(k)}:${canonicalize(v)}`)
|
|
61
|
+
return `{${entries.join(',')}}`
|
|
62
|
+
}
|
|
63
|
+
return JSON.stringify(value)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Cache identity for a provider's model view. `providerId@baseURL`
|
|
68
|
+
* alone isn't enough: `includeModels`/`excludeModels` and
|
|
69
|
+
* `modelCapabilities` are baked into cached entries by discovery, so a
|
|
70
|
+
* config change must not reuse the old cache — it would keep serving
|
|
71
|
+
* the previous adjusted view until a second restart. A fingerprint is
|
|
72
|
+
* appended whenever adjustments exist (pattern order ignored); default
|
|
73
|
+
* configs keep the plain key so existing caches stay warm across
|
|
74
|
+
* plugin upgrades.
|
|
75
|
+
*/
|
|
76
|
+
export function buildCacheKey(
|
|
77
|
+
providerId: string,
|
|
78
|
+
baseURL: string,
|
|
79
|
+
filters: { includeModels?: string[]; excludeModels?: string[] },
|
|
80
|
+
capabilities: Record<string, Record<string, boolean>>,
|
|
81
|
+
): string {
|
|
82
|
+
const base = `${providerId}@${baseURL}`
|
|
83
|
+
const hasAdjustments =
|
|
84
|
+
(filters.includeModels?.length ?? 0) > 0 ||
|
|
85
|
+
(filters.excludeModels?.length ?? 0) > 0 ||
|
|
86
|
+
Object.keys(capabilities).length > 0
|
|
87
|
+
if (!hasAdjustments) return base
|
|
88
|
+
const fingerprint = createHash('sha256')
|
|
89
|
+
.update(
|
|
90
|
+
canonicalize({
|
|
91
|
+
includeModels: filters.includeModels ? [...filters.includeModels].sort() : undefined,
|
|
92
|
+
excludeModels: filters.excludeModels ? [...filters.excludeModels].sort() : undefined,
|
|
93
|
+
capabilities,
|
|
94
|
+
}),
|
|
95
|
+
)
|
|
96
|
+
.digest('hex')
|
|
97
|
+
.slice(0, 12)
|
|
98
|
+
return `${base}@${fingerprint}`
|
|
99
|
+
}
|
|
100
|
+
|
|
50
101
|
/**
|
|
51
102
|
* Read cached model entries for a cache key. Returns `null` on any
|
|
52
103
|
* problem (missing file, parse error, version mismatch, or an entry
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export const CAPABILITY_FLAGS = [
|
|
2
|
+
'supports_vision',
|
|
3
|
+
'supports_function_calling',
|
|
4
|
+
'supports_reasoning',
|
|
5
|
+
'supports_pdf_input',
|
|
6
|
+
'supports_audio_input',
|
|
7
|
+
] as const
|
|
8
|
+
|
|
9
|
+
export type CapabilityFlag = (typeof CAPABILITY_FLAGS)[number]
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Per-model boolean capability overrides keyed by exact model id
|
|
13
|
+
* (`provider.litellm.options.modelCapabilities`). Keys of the inner
|
|
14
|
+
* record are not restricted to `CapabilityFlag`: any boolean capability
|
|
15
|
+
* key LiteLLM reports is accepted, so future flags work without a
|
|
16
|
+
* plugin change.
|
|
17
|
+
*/
|
|
18
|
+
export type ModelCapabilities = Record<string, Record<string, boolean>>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Defensively parse the `modelCapabilities` provider option. Non-object
|
|
22
|
+
* input, non-object per-model values, and non-boolean flag values are
|
|
23
|
+
* dropped; everything else (including explicit `false`) is kept.
|
|
24
|
+
*/
|
|
25
|
+
export function parseModelCapabilities(raw: unknown): ModelCapabilities {
|
|
26
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}
|
|
27
|
+
const out: ModelCapabilities = {}
|
|
28
|
+
for (const [modelId, flags] of Object.entries(raw as Record<string, unknown>)) {
|
|
29
|
+
if (!flags || typeof flags !== 'object' || Array.isArray(flags)) continue
|
|
30
|
+
const parsed: Record<string, boolean> = {}
|
|
31
|
+
for (const [flag, value] of Object.entries(flags as Record<string, unknown>)) {
|
|
32
|
+
if (typeof value === 'boolean') parsed[flag] = value
|
|
33
|
+
}
|
|
34
|
+
if (Object.keys(parsed).length > 0) out[modelId] = parsed
|
|
35
|
+
}
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Overlay user-configured capability flags onto a (possibly enriched)
|
|
41
|
+
* model entry. Explicit `false` wins over whatever the proxy reported,
|
|
42
|
+
* and unknown-to-the-proxy flags are added outright.
|
|
43
|
+
*/
|
|
44
|
+
export function applyCapabilityOverrides<T extends object>(
|
|
45
|
+
model: T,
|
|
46
|
+
overrides?: Readonly<Record<string, boolean>>,
|
|
47
|
+
): T {
|
|
48
|
+
if (!overrides || Object.keys(overrides).length === 0) return model
|
|
49
|
+
return Object.assign({}, model, overrides)
|
|
50
|
+
}
|