opencode-plugin-litellm 0.5.0 โ 0.6.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 +115 -19
- 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.6.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,28 @@ 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>>()
|
|
17
27
|
|
|
18
28
|
/**
|
|
19
29
|
* Read `customHeaders` from a provider options block.
|
|
@@ -32,13 +42,39 @@ function readCustomHeaders(
|
|
|
32
42
|
return undefined
|
|
33
43
|
}
|
|
34
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Overlay metadata from `/v1/model/info` onto a `/v1/models` entry.
|
|
47
|
+
* Fields already present on the lean entry win; the info block only
|
|
48
|
+
* fills gaps (notably `mode`, which `/v1/models` omits for
|
|
49
|
+
* database-defined models).
|
|
50
|
+
*/
|
|
51
|
+
function enrichModel(model: LiteLLMModel, info: LiteLLMModelInfo): LiteLLMModel {
|
|
52
|
+
return {
|
|
53
|
+
...model,
|
|
54
|
+
mode: model.mode ?? info.mode,
|
|
55
|
+
max_tokens: model.max_tokens ?? info.max_tokens,
|
|
56
|
+
max_input_tokens: model.max_input_tokens ?? info.max_input_tokens,
|
|
57
|
+
max_output_tokens: model.max_output_tokens ?? info.max_output_tokens,
|
|
58
|
+
supports_function_calling: model.supports_function_calling ?? info.supports_function_calling,
|
|
59
|
+
supports_vision: model.supports_vision ?? info.supports_vision,
|
|
60
|
+
supports_reasoning: model.supports_reasoning ?? info.supports_reasoning,
|
|
61
|
+
supports_pdf_input: model.supports_pdf_input ?? info.supports_pdf_input,
|
|
62
|
+
supports_audio_input: model.supports_audio_input ?? info.supports_audio_input,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
35
66
|
/**
|
|
36
67
|
* Convert a discovered LiteLLM model into an OpenCode config-level
|
|
37
68
|
* model entry (the shape used in `provider.*.models` inside
|
|
38
|
-
* `opencode.json`).
|
|
69
|
+
* `opencode.json`). Returns `null` for non-chat models (embedding,
|
|
70
|
+
* image, audio) โ they can't be used as primary chat models and would
|
|
71
|
+
* clutter the picker.
|
|
39
72
|
*/
|
|
40
|
-
function toConfigModel(model: LiteLLMModel): Record<string, unknown> {
|
|
73
|
+
function toConfigModel(model: LiteLLMModel): Record<string, unknown> | null {
|
|
41
74
|
const type = categorizeModel(model)
|
|
75
|
+
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
42
78
|
const entry: Record<string, unknown> = {
|
|
43
79
|
name: formatModelName(model),
|
|
44
80
|
}
|
|
@@ -51,13 +87,18 @@ function toConfigModel(model: LiteLLMModel): Record<string, unknown> {
|
|
|
51
87
|
if (model.supports_function_calling) {
|
|
52
88
|
entry.tool_call = true
|
|
53
89
|
}
|
|
90
|
+
if (model.supports_reasoning) {
|
|
91
|
+
entry.reasoning = true
|
|
92
|
+
}
|
|
54
93
|
if (model.supports_vision) {
|
|
55
94
|
entry.attachment = true
|
|
56
95
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
96
|
+
const input: Array<'text' | 'image' | 'pdf' | 'audio'> = ['text']
|
|
97
|
+
if (model.supports_vision) input.push('image')
|
|
98
|
+
if (model.supports_pdf_input) input.push('pdf')
|
|
99
|
+
if (model.supports_audio_input) input.push('audio')
|
|
100
|
+
if (input.length > 1) {
|
|
101
|
+
entry.modalities = { input, output: ['text'] }
|
|
61
102
|
}
|
|
62
103
|
return entry
|
|
63
104
|
}
|
|
@@ -158,6 +199,14 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
158
199
|
|
|
159
200
|
// Discover models with timeout
|
|
160
201
|
const work = async () => {
|
|
202
|
+
const alreadyInjected = injectedModelIds.get(baseURL!)
|
|
203
|
+
if (
|
|
204
|
+
alreadyInjected &&
|
|
205
|
+
[...alreadyInjected].every((id) => models[id])
|
|
206
|
+
) {
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
|
|
161
210
|
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
162
211
|
console.warn(
|
|
163
212
|
`[opencode-litellm] LiteLLM appears offline or unauthorized at ${baseURL}`,
|
|
@@ -165,14 +214,17 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
165
214
|
return
|
|
166
215
|
}
|
|
167
216
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
)
|
|
175
|
-
|
|
217
|
+
// `/v1/models` omits `mode` and capability metadata for
|
|
218
|
+
// database-defined models, so fetch `/v1/model/info` alongside
|
|
219
|
+
// it. The info call is best-effort: without it, classification
|
|
220
|
+
// falls back to id heuristics.
|
|
221
|
+
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
222
|
+
discoverLiteLLMModels(baseURL!, apiKey, customHeaders),
|
|
223
|
+
discoverLiteLLMModelInfo(baseURL!, apiKey, customHeaders),
|
|
224
|
+
])
|
|
225
|
+
|
|
226
|
+
if (modelsResult.status === 'rejected') {
|
|
227
|
+
const error = modelsResult.reason
|
|
176
228
|
console.warn(
|
|
177
229
|
'[opencode-litellm] Model discovery failed:',
|
|
178
230
|
error instanceof Error ? error.message : String(error),
|
|
@@ -180,6 +232,18 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
180
232
|
return
|
|
181
233
|
}
|
|
182
234
|
|
|
235
|
+
const discovered = modelsResult.value
|
|
236
|
+
let infoByName: Map<string, LiteLLMModelInfo> | null = null
|
|
237
|
+
if (infoResult.status === 'fulfilled') {
|
|
238
|
+
infoByName = infoResult.value
|
|
239
|
+
} else {
|
|
240
|
+
const reason = infoResult.reason
|
|
241
|
+
console.warn(
|
|
242
|
+
'[opencode-litellm] /v1/model/info unavailable; non-chat model filtering will use id heuristics only:',
|
|
243
|
+
reason instanceof Error ? reason.message : String(reason),
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
183
247
|
if (discovered.length === 0) {
|
|
184
248
|
console.warn(
|
|
185
249
|
'[opencode-litellm] LiteLLM responded but exposed zero models.',
|
|
@@ -187,10 +251,36 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
187
251
|
return
|
|
188
252
|
}
|
|
189
253
|
|
|
254
|
+
let added = 0
|
|
255
|
+
let skipped = 0
|
|
256
|
+
let wildcards = 0
|
|
257
|
+
const unmatched: string[] = []
|
|
190
258
|
for (const model of discovered) {
|
|
259
|
+
// Wildcard entries (`deepseek/*`) are access rules, not
|
|
260
|
+
// callable models โ invoking one sends a literal `*` upstream.
|
|
261
|
+
if (model.id.includes('*')) {
|
|
262
|
+
wildcards++
|
|
263
|
+
continue
|
|
264
|
+
}
|
|
191
265
|
// Don't overwrite user-curated entries
|
|
192
266
|
if (models[model.id]) continue
|
|
193
|
-
|
|
267
|
+
const info = infoByName?.get(model.id)
|
|
268
|
+
if (infoByName && !info) unmatched.push(model.id)
|
|
269
|
+
const entry = toConfigModel(info ? enrichModel(model, info) : model)
|
|
270
|
+
if (!entry) {
|
|
271
|
+
skipped++
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
models[model.id] = entry
|
|
275
|
+
added++
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (unmatched.length > 0) {
|
|
279
|
+
console.warn(
|
|
280
|
+
`[opencode-litellm] /v1/model/info has no entry for ${unmatched.length} model(s); ` +
|
|
281
|
+
`classification uses id heuristics for: ${unmatched.slice(0, 5).join(', ')}` +
|
|
282
|
+
(unmatched.length > 5 ? `, +${unmatched.length - 5} more` : ''),
|
|
283
|
+
)
|
|
194
284
|
}
|
|
195
285
|
|
|
196
286
|
// Remove the seed placeholder if real models were discovered
|
|
@@ -198,8 +288,14 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
198
288
|
delete models['_']
|
|
199
289
|
}
|
|
200
290
|
|
|
291
|
+
injectedModelIds.set(baseURL!, new Set(Object.keys(models)))
|
|
292
|
+
|
|
201
293
|
console.log(
|
|
202
|
-
`[opencode-litellm] Discovered ${discovered.length} models from ${baseURL}
|
|
294
|
+
`[opencode-litellm] Discovered ${discovered.length} models from ${baseURL} ` +
|
|
295
|
+
`(${added} added` +
|
|
296
|
+
(skipped > 0 ? `, ${skipped} non-chat hidden` : '') +
|
|
297
|
+
(wildcards > 0 ? `, ${wildcards} wildcard ignored` : '') +
|
|
298
|
+
')',
|
|
203
299
|
)
|
|
204
300
|
}
|
|
205
301
|
|
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
|