opencode-plugin-litellm 0.5.0 โ 0.7.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 +40 -10
- package/package.json +8 -1
- package/src/plugin/build-model.ts +2 -2
- package/src/plugin/index.ts +239 -105
- package/src/types/index.ts +38 -3
- package/src/utils/litellm-api.ts +69 -4
package/README.md
CHANGED
|
@@ -73,12 +73,12 @@ opencode
|
|
|
73
73
|
| ๐ **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
74
74
|
| ๐ก **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
75
75
|
| ๐ท๏ธ **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`. |
|
|
76
|
-
| ๐ง **Modality-aware** |
|
|
76
|
+
| ๐ง **Modality-aware** | Enriches `/v1/models` entries with `/v1/model/info` (`mode`, token limits, capability flags) and hides embedding / image / audio models from the picker. |
|
|
77
77
|
| ๐งช **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`. |
|
|
78
78
|
| ๐ข **Provider extraction** | Pulls `litellm_provider` (or the `provider/model` prefix) into `organizationOwner` so models group correctly in the UI. |
|
|
79
79
|
| ๐ **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars or `provider.litellm.options.apiKey`. |
|
|
80
80
|
| ๐ **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways requiring extra HTTP headers. |
|
|
81
|
-
| โฑ๏ธ **Non-blocking startup** |
|
|
81
|
+
| โฑ๏ธ **Non-blocking startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** for slow remote proxies. Repeat config-hook invocations are a no-op. |
|
|
82
82
|
| ๐ค **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
83
83
|
| ๐ชถ **Zero runtime deps** | Only depends on `@opencode-ai/plugin`. No build step, no bundler. |
|
|
84
84
|
| ๐ **TypeScript strict** | Strict-mode compiled, fully typed public API. |
|
|
@@ -127,6 +127,34 @@ You **do not need to list any models** โ the plugin still discovers them from
|
|
|
127
127
|
|
|
128
128
|
That's the whole config โ every model in your LiteLLM `model_list` will appear in the picker.
|
|
129
129
|
|
|
130
|
+
### Example: governed upstream route with Tuning Engines
|
|
131
|
+
|
|
132
|
+
If your team routes model traffic through Tuning Engines for policy, traces,
|
|
133
|
+
approvals, and usage visibility, add it as an OpenAI-compatible upstream in
|
|
134
|
+
your LiteLLM config. The plugin will discover the alias from LiteLLM just like
|
|
135
|
+
any other `model_list` entry:
|
|
136
|
+
|
|
137
|
+
```yaml
|
|
138
|
+
model_list:
|
|
139
|
+
- model_name: te-gpt-5.4-mini
|
|
140
|
+
litellm_params:
|
|
141
|
+
model: openai/gpt-5.4-mini
|
|
142
|
+
api_key: os.environ/TUNING_ENGINES_API_KEY
|
|
143
|
+
api_base: https://api.tuningengines.com/v1
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Then expose the key to LiteLLM and keep your OpenCode config pointed at the
|
|
147
|
+
same LiteLLM proxy:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
export TUNING_ENGINES_API_KEY=sk-te-...
|
|
151
|
+
litellm --config config.yaml --port 4000
|
|
152
|
+
opencode
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
OpenCode and this plugin still own model discovery and picker wiring. Tuning
|
|
156
|
+
Engines sits on the upstream model route as the governed control plane.
|
|
157
|
+
|
|
130
158
|
### Overriding or curating individual models (optional)
|
|
131
159
|
|
|
132
160
|
If you want to rename a model in the picker, pin its `organizationOwner`, or otherwise hand-curate metadata, add it under `models`. The plugin **preserves your entries verbatim** and only injects discovered models whose key isn't already defined:
|
|
@@ -251,21 +279,23 @@ sequenceDiagram
|
|
|
251
279
|
Plugin->>Plugin: auto-create provider entry
|
|
252
280
|
end
|
|
253
281
|
Plugin->>LL: GET /v1/models (with auth if set)
|
|
254
|
-
LL
|
|
282
|
+
Plugin->>LL: GET /v1/model/info (best-effort, for `mode` + limits)
|
|
283
|
+
LL-->>Plugin: { data: [...models] } + per-model info
|
|
284
|
+
Plugin->>Plugin: enrich models, hide non-chat (embedding/image/audio)
|
|
255
285
|
Plugin->>Plugin: format names, infer modalities, extract owner
|
|
256
286
|
Plugin->>Plugin: bucket each model by transport (chat vs responses)
|
|
257
287
|
Plugin->>OC: merge chat-completions models into provider.litellm
|
|
258
288
|
Plugin->>OC: merge responses models into provider.litellm-responses (lazy)
|
|
259
|
-
OC->>OC: render model picker with all discovered models
|
|
289
|
+
OC->>OC: render model picker with all eligible discovered chat models
|
|
260
290
|
```
|
|
261
291
|
|
|
262
292
|
1. On OpenCode startup the `config` lifecycle hook fires.
|
|
263
293
|
2. If `provider.litellm` exists, its `baseURL` is used. Otherwise common ports are probed.
|
|
264
294
|
3. A health check (`GET /v1/models`) verifies the proxy is reachable and authorized.
|
|
265
|
-
4. Models from the response are converted into OpenCode model entries
|
|
295
|
+
4. Models from the response are enriched with `/v1/model/info` metadata (`mode`, token limits, capability flags โ `/v1/models` omits these for database-defined models) and converted into OpenCode model entries keyed by `id`, with formatted `name`, `organizationOwner`, and inferred `modalities`. Non-chat models (embedding / image / audio) are excluded from the picker.
|
|
266
296
|
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.
|
|
267
297
|
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.
|
|
268
|
-
7. The whole flow is wrapped in a `Promise.race` against a
|
|
298
|
+
7. The whole flow is wrapped in a `Promise.race` against a 20 s timeout so a slow proxy never blocks boot.
|
|
269
299
|
|
|
270
300
|
## ๐ Requirements
|
|
271
301
|
|
|
@@ -391,12 +421,12 @@ src/
|
|
|
391
421
|
โโโ index.ts # Public exports
|
|
392
422
|
โโโ types/index.ts # LiteLLM API types
|
|
393
423
|
โโโ utils/
|
|
394
|
-
โ โโโ litellm-api.ts # health check, discovery, auto-detect
|
|
424
|
+
โ โโโ litellm-api.ts # health check, discovery (/v1/models + /v1/model/info), auto-detect
|
|
395
425
|
โ โโโ format-model-name.ts # owner extraction, name formatting, categorization
|
|
396
426
|
โโโ plugin/
|
|
397
|
-
โโโ index.ts # LiteLLMPlugin entry
|
|
398
|
-
โโโ
|
|
399
|
-
โโโ
|
|
427
|
+
โโโ index.ts # LiteLLMPlugin entry (config hook, enrichment, filtering)
|
|
428
|
+
โโโ discover.ts # V2 Model bucketing (unused by the config hook)
|
|
429
|
+
โโโ build-model.ts # V2 Model entry builder
|
|
400
430
|
```
|
|
401
431
|
|
|
402
432
|
See [`CONTRIBUTING.md`](./CONTRIBUTING.md) for the full contributor workflow.
|
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.7.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",
|
|
@@ -34,12 +34,19 @@
|
|
|
34
34
|
"url": "https://github.com/yuseferi/opencode-litellm/issues"
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/yuseferi/opencode-litellm#readme",
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public",
|
|
39
|
+
"provenance": true
|
|
40
|
+
},
|
|
37
41
|
"dependencies": {
|
|
38
42
|
"@opencode-ai/plugin": "^1.14.0",
|
|
39
43
|
"@opencode-ai/sdk": "^1.2.0"
|
|
40
44
|
},
|
|
41
45
|
"devDependencies": {
|
|
46
|
+
"@semantic-release/changelog": "^7.0.0",
|
|
47
|
+
"@semantic-release/git": "^11.0.1",
|
|
42
48
|
"@types/node": "^25.0.3",
|
|
49
|
+
"semantic-release": "^25.0.8",
|
|
43
50
|
"typescript": "^5.9.3"
|
|
44
51
|
}
|
|
45
52
|
}
|
|
@@ -33,7 +33,7 @@ export function buildModelV2(
|
|
|
33
33
|
name: formatModelName(model),
|
|
34
34
|
capabilities: {
|
|
35
35
|
temperature: true,
|
|
36
|
-
reasoning:
|
|
36
|
+
reasoning: !!model.supports_reasoning,
|
|
37
37
|
attachment: isVision || isAudio,
|
|
38
38
|
toolcall: !!model.supports_function_calling,
|
|
39
39
|
input: {
|
|
@@ -41,7 +41,7 @@ export function buildModelV2(
|
|
|
41
41
|
audio: isAudio,
|
|
42
42
|
image: isVision,
|
|
43
43
|
video: false,
|
|
44
|
-
pdf:
|
|
44
|
+
pdf: !!model.supports_pdf_input,
|
|
45
45
|
},
|
|
46
46
|
output: {
|
|
47
47
|
text: !isImageOut,
|
package/src/plugin/index.ts
CHANGED
|
@@ -2,18 +2,45 @@ import type { Plugin, PluginInput } from '@opencode-ai/plugin'
|
|
|
2
2
|
import {
|
|
3
3
|
autoDetectLiteLLM,
|
|
4
4
|
checkLiteLLMHealth,
|
|
5
|
+
discoverLiteLLMModelInfo,
|
|
5
6
|
discoverLiteLLMModels,
|
|
6
7
|
normalizeBaseURL,
|
|
7
8
|
} from '../utils/litellm-api'
|
|
8
9
|
import {
|
|
9
10
|
formatModelName,
|
|
10
|
-
extractModelOwner,
|
|
11
11
|
categorizeModel,
|
|
12
12
|
} from '../utils/format-model-name'
|
|
13
|
-
import type { LiteLLMModel } from '../types'
|
|
13
|
+
import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
|
|
14
14
|
|
|
15
15
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
16
|
-
|
|
16
|
+
// Covers the sequential 3 s health check plus the parallel 15 s
|
|
17
|
+
// models/model-info fetch phase, with headroom.
|
|
18
|
+
const DISCOVERY_TIMEOUT_MS = 20000
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* OpenCode invokes the `config` hook several times per run with a
|
|
22
|
+
* cumulative config object. Track which model ids we already injected
|
|
23
|
+
* per baseURL so repeat invocations can return early instead of
|
|
24
|
+
* re-querying the proxy.
|
|
25
|
+
*/
|
|
26
|
+
const injectedModelIds = new Map<string, Set<string>>()
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Helper to determine if a provider ID or its configured options indicate
|
|
30
|
+
* compatibility with LiteLLM.
|
|
31
|
+
*/
|
|
32
|
+
function isLiteLLMProvider(
|
|
33
|
+
providerId: string,
|
|
34
|
+
options: Record<string, unknown>,
|
|
35
|
+
): boolean {
|
|
36
|
+
if (providerId === CHAT_PROVIDER_ID) return true
|
|
37
|
+
if (providerId.startsWith('litellm-') || providerId.startsWith('litellm_')) return true
|
|
38
|
+
if (options.litellm === true) return true
|
|
39
|
+
if (options.litellmCompatible === true) return true
|
|
40
|
+
if (options['litellm-compatible'] === true) return true
|
|
41
|
+
if (options.litellm_compatible === true) return true
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
17
44
|
|
|
18
45
|
/**
|
|
19
46
|
* Read `customHeaders` from a provider options block.
|
|
@@ -32,13 +59,39 @@ function readCustomHeaders(
|
|
|
32
59
|
return undefined
|
|
33
60
|
}
|
|
34
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Overlay metadata from `/v1/model/info` onto a `/v1/models` entry.
|
|
64
|
+
* Fields already present on the lean entry win; the info block only
|
|
65
|
+
* fills gaps (notably `mode`, which `/v1/models` omits for
|
|
66
|
+
* database-defined models).
|
|
67
|
+
*/
|
|
68
|
+
function enrichModel(model: LiteLLMModel, info: LiteLLMModelInfo): LiteLLMModel {
|
|
69
|
+
return {
|
|
70
|
+
...model,
|
|
71
|
+
mode: model.mode ?? info.mode,
|
|
72
|
+
max_tokens: model.max_tokens ?? info.max_tokens,
|
|
73
|
+
max_input_tokens: model.max_input_tokens ?? info.max_input_tokens,
|
|
74
|
+
max_output_tokens: model.max_output_tokens ?? info.max_output_tokens,
|
|
75
|
+
supports_function_calling: model.supports_function_calling ?? info.supports_function_calling,
|
|
76
|
+
supports_vision: model.supports_vision ?? info.supports_vision,
|
|
77
|
+
supports_reasoning: model.supports_reasoning ?? info.supports_reasoning,
|
|
78
|
+
supports_pdf_input: model.supports_pdf_input ?? info.supports_pdf_input,
|
|
79
|
+
supports_audio_input: model.supports_audio_input ?? info.supports_audio_input,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
35
83
|
/**
|
|
36
84
|
* Convert a discovered LiteLLM model into an OpenCode config-level
|
|
37
85
|
* model entry (the shape used in `provider.*.models` inside
|
|
38
|
-
* `opencode.json`).
|
|
86
|
+
* `opencode.json`). Returns `null` for non-chat models (embedding,
|
|
87
|
+
* image, audio) โ they can't be used as primary chat models and would
|
|
88
|
+
* clutter the picker.
|
|
39
89
|
*/
|
|
40
|
-
function toConfigModel(model: LiteLLMModel): Record<string, unknown> {
|
|
90
|
+
function toConfigModel(model: LiteLLMModel): Record<string, unknown> | null {
|
|
41
91
|
const type = categorizeModel(model)
|
|
92
|
+
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
42
95
|
const entry: Record<string, unknown> = {
|
|
43
96
|
name: formatModelName(model),
|
|
44
97
|
}
|
|
@@ -51,13 +104,18 @@ function toConfigModel(model: LiteLLMModel): Record<string, unknown> {
|
|
|
51
104
|
if (model.supports_function_calling) {
|
|
52
105
|
entry.tool_call = true
|
|
53
106
|
}
|
|
107
|
+
if (model.supports_reasoning) {
|
|
108
|
+
entry.reasoning = true
|
|
109
|
+
}
|
|
54
110
|
if (model.supports_vision) {
|
|
55
111
|
entry.attachment = true
|
|
56
112
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
113
|
+
const input: Array<'text' | 'image' | 'pdf' | 'audio'> = ['text']
|
|
114
|
+
if (model.supports_vision) input.push('image')
|
|
115
|
+
if (model.supports_pdf_input) input.push('pdf')
|
|
116
|
+
if (model.supports_audio_input) input.push('audio')
|
|
117
|
+
if (input.length > 1) {
|
|
118
|
+
entry.modalities = { input, output: ['text'] }
|
|
61
119
|
}
|
|
62
120
|
return entry
|
|
63
121
|
}
|
|
@@ -92,123 +150,199 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
92
150
|
// Ensure the provider entry exists
|
|
93
151
|
if (!config.provider) config.provider = {}
|
|
94
152
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const options = (existing?.options ?? {}) as Record<string, unknown>
|
|
99
|
-
const configuredBase =
|
|
100
|
-
typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
101
|
-
const configuredKey =
|
|
102
|
-
typeof options.apiKey === 'string' && options.apiKey
|
|
103
|
-
? options.apiKey
|
|
104
|
-
: undefined
|
|
105
|
-
const envKey =
|
|
106
|
-
process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
107
|
-
const apiKey = configuredKey ?? envKey
|
|
108
|
-
const customHeaders = readCustomHeaders(options)
|
|
109
|
-
|
|
110
|
-
// Resolve base URL
|
|
111
|
-
let baseURL: string | null = null
|
|
112
|
-
if (configuredBase) {
|
|
113
|
-
baseURL = normalizeBaseURL(configuredBase)
|
|
114
|
-
} else {
|
|
115
|
-
baseURL = await autoDetectLiteLLM(apiKey, customHeaders)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (!baseURL) {
|
|
119
|
-
console.warn(
|
|
120
|
-
'[opencode-litellm] No LiteLLM proxy found. Configure provider.litellm.options.baseURL or start LiteLLM on port 4000/8000/8080.',
|
|
121
|
-
)
|
|
122
|
-
return
|
|
123
|
-
}
|
|
153
|
+
// Find all matching LiteLLM providers
|
|
154
|
+
const providerIds = Object.keys(config.provider)
|
|
155
|
+
const liteLLMProviders: Array<{ id: string; provider: Record<string, unknown> }> = []
|
|
124
156
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
},
|
|
133
|
-
models: {},
|
|
157
|
+
for (const id of providerIds) {
|
|
158
|
+
const provider = config.provider[id]
|
|
159
|
+
if (provider && typeof provider === 'object') {
|
|
160
|
+
const options = (provider.options ?? {}) as Record<string, unknown>
|
|
161
|
+
if (isLiteLLMProvider(id, options)) {
|
|
162
|
+
liteLLMProviders.push({ id, provider })
|
|
163
|
+
}
|
|
134
164
|
}
|
|
135
165
|
}
|
|
136
166
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
167
|
+
// If no providers are matched (e.g., zero-config auto-detection),
|
|
168
|
+
// fall back to default 'litellm' provider to ensure backwards compatibility.
|
|
169
|
+
if (liteLLMProviders.length === 0) {
|
|
170
|
+
const id = CHAT_PROVIDER_ID
|
|
171
|
+
let provider = config.provider[id] as Record<string, unknown> | undefined
|
|
172
|
+
if (!provider) {
|
|
173
|
+
provider = {
|
|
174
|
+
npm: '@ai-sdk/openai-compatible',
|
|
175
|
+
name: 'LiteLLM (proxy)',
|
|
176
|
+
options: {},
|
|
177
|
+
models: {},
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
liteLLMProviders.push({ id, provider })
|
|
150
181
|
}
|
|
151
182
|
|
|
152
|
-
//
|
|
153
|
-
|
|
154
|
-
provider.
|
|
155
|
-
|
|
183
|
+
// Process each matched provider
|
|
184
|
+
for (const { id: providerId, provider } of liteLLMProviders) {
|
|
185
|
+
const options = (provider.options ?? {}) as Record<string, unknown>
|
|
186
|
+
const configuredBase =
|
|
187
|
+
typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
188
|
+
const configuredKey =
|
|
189
|
+
typeof options.apiKey === 'string' && options.apiKey
|
|
190
|
+
? options.apiKey
|
|
191
|
+
: undefined
|
|
192
|
+
const envKey =
|
|
193
|
+
process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
194
|
+
const apiKey = configuredKey ?? envKey
|
|
195
|
+
const customHeaders = readCustomHeaders(options)
|
|
156
196
|
|
|
157
|
-
|
|
197
|
+
// Resolve base URL
|
|
198
|
+
let baseURL: string | null = null
|
|
199
|
+
if (configuredBase) {
|
|
200
|
+
baseURL = normalizeBaseURL(configuredBase)
|
|
201
|
+
} else {
|
|
202
|
+
baseURL = await autoDetectLiteLLM(apiKey, customHeaders)
|
|
203
|
+
}
|
|
158
204
|
|
|
159
|
-
|
|
160
|
-
const work = async () => {
|
|
161
|
-
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
205
|
+
if (!baseURL) {
|
|
162
206
|
console.warn(
|
|
163
|
-
`[opencode-litellm] LiteLLM
|
|
207
|
+
`[opencode-litellm] No LiteLLM proxy found for provider "${providerId}". Configure options.baseURL or start LiteLLM on port 4000/8000/8080.`,
|
|
164
208
|
)
|
|
165
|
-
|
|
209
|
+
continue
|
|
166
210
|
}
|
|
167
211
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
baseURL!,
|
|
172
|
-
apiKey,
|
|
173
|
-
customHeaders,
|
|
174
|
-
)
|
|
175
|
-
} catch (error) {
|
|
176
|
-
console.warn(
|
|
177
|
-
'[opencode-litellm] Model discovery failed:',
|
|
178
|
-
error instanceof Error ? error.message : String(error),
|
|
179
|
-
)
|
|
180
|
-
return
|
|
212
|
+
// Initialize/Update the provider entry in config
|
|
213
|
+
if (!config.provider[providerId]) {
|
|
214
|
+
config.provider[providerId] = provider
|
|
181
215
|
}
|
|
216
|
+
const actualProvider = config.provider[providerId] as Record<string, unknown>
|
|
182
217
|
|
|
183
|
-
if (
|
|
184
|
-
|
|
185
|
-
'[opencode-litellm] LiteLLM responded but exposed zero models.',
|
|
186
|
-
)
|
|
187
|
-
return
|
|
218
|
+
if (!actualProvider.npm) {
|
|
219
|
+
actualProvider.npm = '@ai-sdk/openai-compatible'
|
|
188
220
|
}
|
|
189
221
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
222
|
+
if (!actualProvider.options) {
|
|
223
|
+
actualProvider.options = { baseURL: `${baseURL}/v1` }
|
|
224
|
+
} else {
|
|
225
|
+
const actualOptions = actualProvider.options as Record<string, unknown>
|
|
226
|
+
if (!actualOptions.baseURL) {
|
|
227
|
+
actualOptions.baseURL = `${baseURL}/v1`
|
|
228
|
+
}
|
|
194
229
|
}
|
|
195
230
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
delete models['_']
|
|
231
|
+
if (!actualProvider.models) {
|
|
232
|
+
actualProvider.models = {}
|
|
199
233
|
}
|
|
200
234
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
235
|
+
const models = actualProvider.models as Record<string, unknown>
|
|
236
|
+
|
|
237
|
+
// Discover models with timeout
|
|
238
|
+
const work = async () => {
|
|
239
|
+
const alreadyInjected = injectedModelIds.get(baseURL!)
|
|
240
|
+
if (
|
|
241
|
+
alreadyInjected &&
|
|
242
|
+
[...alreadyInjected].every((id) => models[id])
|
|
243
|
+
) {
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
248
|
+
console.warn(
|
|
249
|
+
`[opencode-litellm] LiteLLM appears offline or unauthorized for provider "${providerId}" at ${baseURL}`,
|
|
250
|
+
)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// `/v1/models` omits `mode` and capability metadata for
|
|
255
|
+
// database-defined models, so fetch `/v1/model/info` alongside
|
|
256
|
+
// it. The info call is best-effort: without it, classification
|
|
257
|
+
// falls back to id heuristics.
|
|
258
|
+
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
259
|
+
discoverLiteLLMModels(baseURL!, apiKey, customHeaders),
|
|
260
|
+
discoverLiteLLMModelInfo(baseURL!, apiKey, customHeaders),
|
|
261
|
+
])
|
|
205
262
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
263
|
+
if (modelsResult.status === 'rejected') {
|
|
264
|
+
const error = modelsResult.reason
|
|
265
|
+
console.warn(
|
|
266
|
+
`[opencode-litellm] Model discovery failed for provider "${providerId}":`,
|
|
267
|
+
error instanceof Error ? error.message : String(error),
|
|
268
|
+
)
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const discovered = modelsResult.value
|
|
273
|
+
let infoByName: Map<string, LiteLLMModelInfo> | null = null
|
|
274
|
+
if (infoResult.status === 'fulfilled') {
|
|
275
|
+
infoByName = infoResult.value
|
|
276
|
+
} else {
|
|
277
|
+
const reason = infoResult.reason
|
|
278
|
+
console.warn(
|
|
279
|
+
`[opencode-litellm] /v1/model/info unavailable for provider "${providerId}"; non-chat model filtering will use id heuristics only:`,
|
|
280
|
+
reason instanceof Error ? reason.message : String(reason),
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
if (discovered.length === 0) {
|
|
285
|
+
console.warn(
|
|
286
|
+
`[opencode-litellm] LiteLLM responded for provider "${providerId}" but exposed zero models.`,
|
|
287
|
+
)
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let added = 0
|
|
292
|
+
let skipped = 0
|
|
293
|
+
let wildcards = 0
|
|
294
|
+
const unmatched: string[] = []
|
|
295
|
+
for (const model of discovered) {
|
|
296
|
+
// Wildcard entries (`deepseek/*`) are access rules, not
|
|
297
|
+
// callable models โ invoking one sends a literal `*` upstream.
|
|
298
|
+
if (model.id.includes('*')) {
|
|
299
|
+
wildcards++
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
// Don't overwrite user-curated entries
|
|
303
|
+
if (models[model.id]) continue
|
|
304
|
+
const info = infoByName?.get(model.id)
|
|
305
|
+
if (infoByName && !info) unmatched.push(model.id)
|
|
306
|
+
const entry = toConfigModel(info ? enrichModel(model, info) : model)
|
|
307
|
+
if (!entry) {
|
|
308
|
+
skipped++
|
|
309
|
+
continue
|
|
310
|
+
}
|
|
311
|
+
models[model.id] = entry
|
|
312
|
+
added++
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (unmatched.length > 0) {
|
|
316
|
+
console.warn(
|
|
317
|
+
`[opencode-litellm] /v1/model/info has no entry for ${unmatched.length} model(s) on provider "${providerId}"; ` +
|
|
318
|
+
`classification uses id heuristics for: ${unmatched.slice(0, 5).join(', ')}` +
|
|
319
|
+
(unmatched.length > 5 ? `, +${unmatched.length - 5} more` : ''),
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Remove the seed placeholder if real models were discovered
|
|
324
|
+
if (models['_'] && Object.keys(models).length > 1) {
|
|
325
|
+
delete models['_']
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
injectedModelIds.set(baseURL!, new Set(Object.keys(models)))
|
|
329
|
+
|
|
330
|
+
console.log(
|
|
331
|
+
`[opencode-litellm] Discovered ${discovered.length} models for provider "${providerId}" from ${baseURL} ` +
|
|
332
|
+
`(${added} added` +
|
|
333
|
+
(skipped > 0 ? `, ${skipped} non-chat hidden` : '') +
|
|
334
|
+
(wildcards > 0 ? `, ${wildcards} wildcard ignored` : '') +
|
|
335
|
+
')',
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
await Promise.race([
|
|
340
|
+
work(),
|
|
341
|
+
new Promise<void>((resolve) =>
|
|
342
|
+
setTimeout(resolve, DISCOVERY_TIMEOUT_MS),
|
|
343
|
+
),
|
|
344
|
+
])
|
|
345
|
+
}
|
|
212
346
|
},
|
|
213
347
|
}
|
|
214
348
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -15,9 +15,9 @@ export interface LiteLLMModel {
|
|
|
15
15
|
*/
|
|
16
16
|
litellm_provider?: string
|
|
17
17
|
/**
|
|
18
|
-
* Optional capability metadata
|
|
19
|
-
* via `/model/info`
|
|
20
|
-
*
|
|
18
|
+
* Optional capability metadata. Present on `/v1/models` only for some
|
|
19
|
+
* deployments; reliably available via `/v1/model/info` and merged onto
|
|
20
|
+
* the discovered entry by the plugin.
|
|
21
21
|
*
|
|
22
22
|
* Newer LiteLLM versions may expose `'responses'` here for models
|
|
23
23
|
* that must be routed through the OpenAI Responses API rather than
|
|
@@ -29,6 +29,9 @@ export interface LiteLLMModel {
|
|
|
29
29
|
max_output_tokens?: number
|
|
30
30
|
supports_function_calling?: boolean
|
|
31
31
|
supports_vision?: boolean
|
|
32
|
+
supports_reasoning?: boolean
|
|
33
|
+
supports_pdf_input?: boolean
|
|
34
|
+
supports_audio_input?: boolean
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
export interface LiteLLMModelsResponse {
|
|
@@ -36,6 +39,38 @@ export interface LiteLLMModelsResponse {
|
|
|
36
39
|
data: LiteLLMModel[]
|
|
37
40
|
}
|
|
38
41
|
|
|
42
|
+
/**
|
|
43
|
+
* The `model_info` block of a `/v1/model/info` entry. This endpoint
|
|
44
|
+
* reliably carries `mode` (and token limits) even for database-defined
|
|
45
|
+
* models, where `/v1/models` only returns the lean OpenAI schema.
|
|
46
|
+
*/
|
|
47
|
+
export interface LiteLLMModelInfo {
|
|
48
|
+
id?: string
|
|
49
|
+
db_model?: boolean
|
|
50
|
+
/** Alias LiteLLM assigns to the model; mirrors the `/v1/models` id. */
|
|
51
|
+
key?: string
|
|
52
|
+
mode?: string
|
|
53
|
+
max_tokens?: number
|
|
54
|
+
max_input_tokens?: number
|
|
55
|
+
max_output_tokens?: number
|
|
56
|
+
supports_function_calling?: boolean
|
|
57
|
+
supports_vision?: boolean
|
|
58
|
+
supports_reasoning?: boolean
|
|
59
|
+
supports_pdf_input?: boolean
|
|
60
|
+
supports_audio_input?: boolean
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A single entry returned by LiteLLM's `/v1/model/info` endpoint. */
|
|
64
|
+
export interface LiteLLMModelInfoEntry {
|
|
65
|
+
model_name: string
|
|
66
|
+
litellm_params?: Record<string, unknown>
|
|
67
|
+
model_info?: LiteLLMModelInfo
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface LiteLLMModelInfoResponse {
|
|
71
|
+
data?: LiteLLMModelInfoEntry[]
|
|
72
|
+
}
|
|
73
|
+
|
|
39
74
|
export type ModelType = 'chat' | 'embedding' | 'image' | 'audio' | 'unknown'
|
|
40
75
|
|
|
41
76
|
/**
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import type { LiteLLMModel, LiteLLMModelsResponse } from '../types'
|
|
1
|
+
import type { LiteLLMModel, LiteLLMModelInfo, LiteLLMModelInfoResponse, LiteLLMModelsResponse } from '../types'
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_LITELLM_URL = 'http://localhost:4000'
|
|
4
4
|
const MODELS_ENDPOINT = '/v1/models'
|
|
5
|
-
const
|
|
5
|
+
const MODEL_INFO_ENDPOINT = '/v1/model/info'
|
|
6
|
+
// Health checks fail fast so auto-detection stays snappy; the actual
|
|
7
|
+
// discovery fetches get a generous budget because `/v1/model/info`
|
|
8
|
+
// payloads from remote proxies with many database-defined models can
|
|
9
|
+
// be large and slow to generate.
|
|
10
|
+
const HEALTH_TIMEOUT_MS = 3000
|
|
11
|
+
const FETCH_TIMEOUT_MS = 15000
|
|
6
12
|
|
|
7
13
|
/**
|
|
8
14
|
* Normalise a base URL so the rest of the plugin can rely on a
|
|
@@ -45,7 +51,7 @@ export async function checkLiteLLMHealth(
|
|
|
45
51
|
const response = await fetch(buildAPIURL(baseURL), {
|
|
46
52
|
method: 'GET',
|
|
47
53
|
headers: buildHeaders(apiKey, customHeaders),
|
|
48
|
-
signal: AbortSignal.timeout(
|
|
54
|
+
signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
|
|
49
55
|
})
|
|
50
56
|
// 401 still means a server is alive โ we just don't have the right
|
|
51
57
|
// credentials. Surface that as "unhealthy" so the user is prompted
|
|
@@ -66,7 +72,7 @@ export async function discoverLiteLLMModels(
|
|
|
66
72
|
const response = await fetch(url, {
|
|
67
73
|
method: 'GET',
|
|
68
74
|
headers: buildHeaders(apiKey, customHeaders),
|
|
69
|
-
signal: AbortSignal.timeout(
|
|
75
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
70
76
|
})
|
|
71
77
|
|
|
72
78
|
if (!response.ok) {
|
|
@@ -77,6 +83,65 @@ export async function discoverLiteLLMModels(
|
|
|
77
83
|
return data.data ?? []
|
|
78
84
|
}
|
|
79
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Fetch per-model metadata (`mode`, token limits, capability flags)
|
|
88
|
+
* from `/v1/model/info`, keyed by model name. `/v1/models` omits these
|
|
89
|
+
* fields for database-defined models, so classification (e.g. filtering
|
|
90
|
+
* out embedding models) relies on this endpoint.
|
|
91
|
+
*/
|
|
92
|
+
export async function discoverLiteLLMModelInfo(
|
|
93
|
+
baseURL: string = DEFAULT_LITELLM_URL,
|
|
94
|
+
apiKey?: string,
|
|
95
|
+
customHeaders?: Record<string, string>,
|
|
96
|
+
): Promise<Map<string, LiteLLMModelInfo>> {
|
|
97
|
+
const url = buildAPIURL(baseURL, MODEL_INFO_ENDPOINT)
|
|
98
|
+
const response = await fetch(url, {
|
|
99
|
+
method: 'GET',
|
|
100
|
+
headers: buildHeaders(apiKey, customHeaders),
|
|
101
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
if (!response.ok) {
|
|
105
|
+
throw new Error(`LiteLLM responded with HTTP ${response.status} ${response.statusText}`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const data = (await response.json()) as LiteLLMModelInfoResponse
|
|
109
|
+
const infoByName = new Map<string, LiteLLMModelInfo>()
|
|
110
|
+
for (const entry of data.data ?? []) {
|
|
111
|
+
if (!entry.model_info) continue
|
|
112
|
+
// Some deployments set capability flags on the params block rather
|
|
113
|
+
// than inside model_info. Fill those gaps so enrichment sees them.
|
|
114
|
+
const info: LiteLLMModelInfo = { ...entry.model_info }
|
|
115
|
+
const capabilityFlags = [
|
|
116
|
+
'supports_vision',
|
|
117
|
+
'supports_function_calling',
|
|
118
|
+
'supports_reasoning',
|
|
119
|
+
'supports_pdf_input',
|
|
120
|
+
'supports_audio_input',
|
|
121
|
+
] as const
|
|
122
|
+
for (const flag of capabilityFlags) {
|
|
123
|
+
const paramsValue = entry.litellm_params?.[flag]
|
|
124
|
+
if (info[flag] == null && typeof paramsValue === 'boolean') {
|
|
125
|
+
info[flag] = paramsValue
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// Index under every alias LiteLLM may use for this model โ the
|
|
129
|
+
// `/v1/models` id can match any of them depending on how the
|
|
130
|
+
// deployment names its entries (alias vs upstream model string).
|
|
131
|
+
const keys = [
|
|
132
|
+
entry.model_name,
|
|
133
|
+
entry.model_info.key,
|
|
134
|
+
typeof entry.litellm_params?.model === 'string' ? entry.litellm_params.model : undefined,
|
|
135
|
+
]
|
|
136
|
+
for (const key of keys) {
|
|
137
|
+
if (key && !infoByName.has(key)) {
|
|
138
|
+
infoByName.set(key, info)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return infoByName
|
|
143
|
+
}
|
|
144
|
+
|
|
80
145
|
/**
|
|
81
146
|
* Try the most common ports a LiteLLM proxy is started on.
|
|
82
147
|
* The default `litellm --port` is 4000, but 8000 is also widely used
|