opencode-plugin-litellm 0.4.2 โ 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 +41 -22
- package/package.json +8 -1
- package/src/plugin/build-model.ts +2 -2
- package/src/plugin/index.ts +283 -78
- package/src/types/index.ts +38 -3
- package/src/utils/litellm-api.ts +69 -4
package/README.md
CHANGED
|
@@ -52,20 +52,12 @@ npm install opencode-plugin-litellm
|
|
|
52
52
|
"npm": "@ai-sdk/openai-compatible",
|
|
53
53
|
"options": {
|
|
54
54
|
"baseURL": "http://localhost:4000/v1"
|
|
55
|
-
},
|
|
56
|
-
"models": {
|
|
57
|
-
"_": { "name": "seed" }
|
|
58
55
|
}
|
|
59
56
|
}
|
|
60
57
|
}
|
|
61
58
|
}
|
|
62
59
|
```
|
|
63
60
|
|
|
64
|
-
> **Why the `"_"` seed model?** OpenCode only activates the plugin's
|
|
65
|
-
> model-discovery hook when the provider already has at least one model
|
|
66
|
-
> entry. The seed is replaced at startup by the full list from your
|
|
67
|
-
> LiteLLM proxy.
|
|
68
|
-
|
|
69
61
|
```bash
|
|
70
62
|
# 3. Start LiteLLM (if it isn't already)
|
|
71
63
|
litellm --config config.yaml --port 4000
|
|
@@ -81,12 +73,12 @@ opencode
|
|
|
81
73
|
| ๐ **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
82
74
|
| ๐ก **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
83
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`. |
|
|
84
|
-
| ๐ง **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. |
|
|
85
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`. |
|
|
86
78
|
| ๐ข **Provider extraction** | Pulls `litellm_provider` (or the `provider/model` prefix) into `organizationOwner` so models group correctly in the UI. |
|
|
87
79
|
| ๐ **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars or `provider.litellm.options.apiKey`. |
|
|
88
80
|
| ๐ **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways requiring extra HTTP headers. |
|
|
89
|
-
| โฑ๏ธ **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. |
|
|
90
82
|
| ๐ค **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
91
83
|
| ๐ชถ **Zero runtime deps** | Only depends on `@opencode-ai/plugin`. No build step, no bundler. |
|
|
92
84
|
| ๐ **TypeScript strict** | Strict-mode compiled, fully typed public API. |
|
|
@@ -95,7 +87,7 @@ opencode
|
|
|
95
87
|
|
|
96
88
|
### Minimal config (recommended)
|
|
97
89
|
|
|
98
|
-
Point at your LiteLLM proxy
|
|
90
|
+
Point at your LiteLLM proxy โ the plugin discovers all models automatically:
|
|
99
91
|
|
|
100
92
|
```jsonc
|
|
101
93
|
{
|
|
@@ -106,9 +98,6 @@ Point at your LiteLLM proxy and include a seed model so OpenCode registers the p
|
|
|
106
98
|
"npm": "@ai-sdk/openai-compatible",
|
|
107
99
|
"options": {
|
|
108
100
|
"baseURL": "http://localhost:4000/v1"
|
|
109
|
-
},
|
|
110
|
-
"models": {
|
|
111
|
-
"_": { "name": "seed" }
|
|
112
101
|
}
|
|
113
102
|
}
|
|
114
103
|
}
|
|
@@ -138,6 +127,34 @@ You **do not need to list any models** โ the plugin still discovers them from
|
|
|
138
127
|
|
|
139
128
|
That's the whole config โ every model in your LiteLLM `model_list` will appear in the picker.
|
|
140
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
|
+
|
|
141
158
|
### Overriding or curating individual models (optional)
|
|
142
159
|
|
|
143
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:
|
|
@@ -262,21 +279,23 @@ sequenceDiagram
|
|
|
262
279
|
Plugin->>Plugin: auto-create provider entry
|
|
263
280
|
end
|
|
264
281
|
Plugin->>LL: GET /v1/models (with auth if set)
|
|
265
|
-
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)
|
|
266
285
|
Plugin->>Plugin: format names, infer modalities, extract owner
|
|
267
286
|
Plugin->>Plugin: bucket each model by transport (chat vs responses)
|
|
268
287
|
Plugin->>OC: merge chat-completions models into provider.litellm
|
|
269
288
|
Plugin->>OC: merge responses models into provider.litellm-responses (lazy)
|
|
270
|
-
OC->>OC: render model picker with all discovered models
|
|
289
|
+
OC->>OC: render model picker with all eligible discovered chat models
|
|
271
290
|
```
|
|
272
291
|
|
|
273
292
|
1. On OpenCode startup the `config` lifecycle hook fires.
|
|
274
293
|
2. If `provider.litellm` exists, its `baseURL` is used. Otherwise common ports are probed.
|
|
275
294
|
3. A health check (`GET /v1/models`) verifies the proxy is reachable and authorized.
|
|
276
|
-
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.
|
|
277
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.
|
|
278
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.
|
|
279
|
-
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.
|
|
280
299
|
|
|
281
300
|
## ๐ Requirements
|
|
282
301
|
|
|
@@ -402,12 +421,12 @@ src/
|
|
|
402
421
|
โโโ index.ts # Public exports
|
|
403
422
|
โโโ types/index.ts # LiteLLM API types
|
|
404
423
|
โโโ utils/
|
|
405
|
-
โ โโโ litellm-api.ts # health check, discovery, auto-detect
|
|
424
|
+
โ โโโ litellm-api.ts # health check, discovery (/v1/models + /v1/model/info), auto-detect
|
|
406
425
|
โ โโโ format-model-name.ts # owner extraction, name formatting, categorization
|
|
407
426
|
โโโ plugin/
|
|
408
|
-
โโโ index.ts # LiteLLMPlugin entry
|
|
409
|
-
โโโ
|
|
410
|
-
โโโ
|
|
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
|
|
411
430
|
```
|
|
412
431
|
|
|
413
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
|
@@ -1,30 +1,115 @@
|
|
|
1
1
|
import type { Plugin, PluginInput } from '@opencode-ai/plugin'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
autoDetectLiteLLM,
|
|
4
|
+
checkLiteLLMHealth,
|
|
5
|
+
discoverLiteLLMModelInfo,
|
|
6
|
+
discoverLiteLLMModels,
|
|
7
|
+
normalizeBaseURL,
|
|
8
|
+
} from '../utils/litellm-api'
|
|
9
|
+
import {
|
|
10
|
+
formatModelName,
|
|
11
|
+
categorizeModel,
|
|
12
|
+
} from '../utils/format-model-name'
|
|
13
|
+
import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
|
|
3
14
|
|
|
4
15
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
5
|
-
|
|
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
|
+
* Read `customHeaders` from a provider options block.
|
|
30
|
+
*/
|
|
31
|
+
function readCustomHeaders(
|
|
32
|
+
options: Record<string, unknown>,
|
|
33
|
+
): Record<string, string> | undefined {
|
|
34
|
+
const raw = options.customHeaders
|
|
35
|
+
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
36
|
+
const out: Record<string, string> = {}
|
|
37
|
+
for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
|
|
38
|
+
if (typeof v === 'string') out[k] = v
|
|
39
|
+
}
|
|
40
|
+
return Object.keys(out).length > 0 ? out : undefined
|
|
41
|
+
}
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
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
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Convert a discovered LiteLLM model into an OpenCode config-level
|
|
68
|
+
* model entry (the shape used in `provider.*.models` inside
|
|
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.
|
|
72
|
+
*/
|
|
73
|
+
function toConfigModel(model: LiteLLMModel): Record<string, unknown> | null {
|
|
74
|
+
const type = categorizeModel(model)
|
|
75
|
+
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
const entry: Record<string, unknown> = {
|
|
79
|
+
name: formatModelName(model),
|
|
80
|
+
}
|
|
81
|
+
if (model.max_input_tokens || model.max_output_tokens) {
|
|
82
|
+
entry.limit = {
|
|
83
|
+
context: model.max_input_tokens ?? 0,
|
|
84
|
+
output: model.max_output_tokens ?? 0,
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (model.supports_function_calling) {
|
|
88
|
+
entry.tool_call = true
|
|
89
|
+
}
|
|
90
|
+
if (model.supports_reasoning) {
|
|
91
|
+
entry.reasoning = true
|
|
92
|
+
}
|
|
93
|
+
if (model.supports_vision) {
|
|
94
|
+
entry.attachment = true
|
|
95
|
+
}
|
|
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'] }
|
|
102
|
+
}
|
|
103
|
+
return entry
|
|
104
|
+
}
|
|
6
105
|
|
|
7
106
|
/**
|
|
8
107
|
* LiteLLM Plugin for OpenCode.
|
|
9
108
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* **Important:** OpenCode only calls `provider.models` when the
|
|
15
|
-
* provider has at least one model defined in `opencode.json`. You
|
|
16
|
-
* must include a seed model entry โ it can be any model id your
|
|
17
|
-
* proxy exposes. The plugin will discover and add all remaining
|
|
18
|
-
* models automatically.
|
|
19
|
-
*
|
|
20
|
-
* By default, every discovered model is registered under the
|
|
21
|
-
* `litellm` provider โ including OpenAI reasoning-tier models like
|
|
22
|
-
* `gpt-5*`. Most of those models work fine through
|
|
23
|
-
* `/v1/chat/completions` for normal use. If you actually hit the
|
|
24
|
-
* "Function tools with reasoning_effort are not supported" error
|
|
25
|
-
* from OpenAI, declare a sibling `litellm-responses` provider in
|
|
26
|
-
* `opencode.json` (see {@link LiteLLMResponsesPlugin}) and the
|
|
27
|
-
* plugin will route those models through `/v1/responses` instead.
|
|
109
|
+
* Uses the `config` hook to discover models from a LiteLLM proxy and
|
|
110
|
+
* inject them into the provider's `models` map at startup. This is the
|
|
111
|
+
* only reliable way to dynamically populate a provider โ the
|
|
112
|
+
* `provider.models` hook is not called by OpenCode for custom providers.
|
|
28
113
|
*
|
|
29
114
|
* Configure the provider in your `opencode.json`:
|
|
30
115
|
*
|
|
@@ -37,9 +122,6 @@ const RESPONSES_PROVIDER_ID = 'litellm-responses'
|
|
|
37
122
|
* "options": {
|
|
38
123
|
* "baseURL": "http://localhost:4000/v1",
|
|
39
124
|
* "apiKey": "{env:LITELLM_API_KEY}"
|
|
40
|
-
* },
|
|
41
|
-
* "models": {
|
|
42
|
-
* "_": { "name": "seed" }
|
|
43
125
|
* }
|
|
44
126
|
* }
|
|
45
127
|
* }
|
|
@@ -47,65 +129,188 @@ const RESPONSES_PROVIDER_ID = 'litellm-responses'
|
|
|
47
129
|
*/
|
|
48
130
|
export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
49
131
|
return {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
132
|
+
config: async (config: any) => {
|
|
133
|
+
// Ensure the provider entry exists
|
|
134
|
+
if (!config.provider) config.provider = {}
|
|
135
|
+
|
|
136
|
+
const existing = config.provider[CHAT_PROVIDER_ID] as
|
|
137
|
+
| Record<string, unknown>
|
|
138
|
+
| undefined
|
|
139
|
+
const options = (existing?.options ?? {}) as Record<string, unknown>
|
|
140
|
+
const configuredBase =
|
|
141
|
+
typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
142
|
+
const configuredKey =
|
|
143
|
+
typeof options.apiKey === 'string' && options.apiKey
|
|
144
|
+
? options.apiKey
|
|
145
|
+
: undefined
|
|
146
|
+
const envKey =
|
|
147
|
+
process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
148
|
+
const apiKey = configuredKey ?? envKey
|
|
149
|
+
const customHeaders = readCustomHeaders(options)
|
|
150
|
+
|
|
151
|
+
// Resolve base URL
|
|
152
|
+
let baseURL: string | null = null
|
|
153
|
+
if (configuredBase) {
|
|
154
|
+
baseURL = normalizeBaseURL(configuredBase)
|
|
155
|
+
} else {
|
|
156
|
+
baseURL = await autoDetectLiteLLM(apiKey, customHeaders)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!baseURL) {
|
|
160
|
+
console.warn(
|
|
161
|
+
'[opencode-litellm] No LiteLLM proxy found. Configure provider.litellm.options.baseURL or start LiteLLM on port 4000/8000/8080.',
|
|
162
|
+
)
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Create provider entry if it doesn't exist
|
|
167
|
+
if (!existing) {
|
|
168
|
+
config.provider[CHAT_PROVIDER_ID] = {
|
|
60
169
|
npm: '@ai-sdk/openai-compatible',
|
|
61
|
-
|
|
62
|
-
|
|
170
|
+
name: 'LiteLLM (proxy)',
|
|
171
|
+
options: {
|
|
172
|
+
baseURL: `${baseURL}/v1`,
|
|
173
|
+
},
|
|
174
|
+
models: {},
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const provider = config.provider[CHAT_PROVIDER_ID] as Record<
|
|
179
|
+
string,
|
|
180
|
+
unknown
|
|
181
|
+
>
|
|
182
|
+
|
|
183
|
+
// Ensure npm is set
|
|
184
|
+
if (!provider.npm) {
|
|
185
|
+
provider.npm = '@ai-sdk/openai-compatible'
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Ensure options.baseURL is set
|
|
189
|
+
if (!provider.options) {
|
|
190
|
+
provider.options = { baseURL: `${baseURL}/v1` }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Ensure models map exists
|
|
194
|
+
if (!provider.models) {
|
|
195
|
+
provider.models = {}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const models = provider.models as Record<string, unknown>
|
|
199
|
+
|
|
200
|
+
// Discover models with timeout
|
|
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
|
+
|
|
210
|
+
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
211
|
+
console.warn(
|
|
212
|
+
`[opencode-litellm] LiteLLM appears offline or unauthorized at ${baseURL}`,
|
|
213
|
+
)
|
|
214
|
+
return
|
|
215
|
+
}
|
|
216
|
+
|
|
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
|
|
228
|
+
console.warn(
|
|
229
|
+
'[opencode-litellm] Model discovery failed:',
|
|
230
|
+
error instanceof Error ? error.message : String(error),
|
|
231
|
+
)
|
|
232
|
+
return
|
|
233
|
+
}
|
|
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
|
+
|
|
247
|
+
if (discovered.length === 0) {
|
|
248
|
+
console.warn(
|
|
249
|
+
'[opencode-litellm] LiteLLM responded but exposed zero models.',
|
|
250
|
+
)
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let added = 0
|
|
255
|
+
let skipped = 0
|
|
256
|
+
let wildcards = 0
|
|
257
|
+
const unmatched: string[] = []
|
|
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
|
+
}
|
|
265
|
+
// Don't overwrite user-curated entries
|
|
266
|
+
if (models[model.id]) continue
|
|
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
|
+
)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Remove the seed placeholder if real models were discovered
|
|
287
|
+
if (models['_'] && Object.keys(models).length > 1) {
|
|
288
|
+
delete models['_']
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
injectedModelIds.set(baseURL!, new Set(Object.keys(models)))
|
|
292
|
+
|
|
293
|
+
console.log(
|
|
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
|
+
')',
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
await Promise.race([
|
|
303
|
+
work(),
|
|
304
|
+
new Promise<void>((resolve) =>
|
|
305
|
+
setTimeout(resolve, DISCOVERY_TIMEOUT_MS),
|
|
306
|
+
),
|
|
307
|
+
])
|
|
63
308
|
},
|
|
64
309
|
}
|
|
65
310
|
}
|
|
66
311
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
* provider for OpenAI reasoning-tier models.
|
|
70
|
-
*
|
|
71
|
-
* OpenAI's reasoning-tier models (`gpt-5*`, `o1`/`o3`/`o4*`) reject
|
|
72
|
-
* requests that combine `reasoning_effort` with function tools when
|
|
73
|
-
* sent to `/v1/chat/completions`. Routing those models through
|
|
74
|
-
* `/v1/responses` (the OpenAI Responses API) avoids the rejection.
|
|
75
|
-
*
|
|
76
|
-
* This plugin is only active when the `litellm-responses` provider
|
|
77
|
-
* is declared in `opencode.json`. When it is, the chat-only `litellm`
|
|
78
|
-
* provider continues to register every discovered model โ set
|
|
79
|
-
* `chatApiModels` / `responsesApiModels` on either provider's options
|
|
80
|
-
* if you want to control which side a particular model lives on.
|
|
81
|
-
*
|
|
82
|
-
* Example `opencode.json` snippet:
|
|
83
|
-
*
|
|
84
|
-
* {
|
|
85
|
-
* "provider": {
|
|
86
|
-
* "litellm-responses": {
|
|
87
|
-
* "npm": "@ai-sdk/openai",
|
|
88
|
-
* "name": "LiteLLM (responses)",
|
|
89
|
-
* "options": {
|
|
90
|
-
* "baseURL": "http://localhost:4000/v1",
|
|
91
|
-
* "apiKey": "{env:LITELLM_API_KEY}",
|
|
92
|
-
* "compatibility": "strict"
|
|
93
|
-
* }
|
|
94
|
-
* }
|
|
95
|
-
* }
|
|
96
|
-
* }
|
|
97
|
-
*/
|
|
312
|
+
// Re-export the responses plugin for backwards compat, but it's now a no-op.
|
|
313
|
+
// The config hook approach handles all models in a single provider.
|
|
98
314
|
export const LiteLLMResponsesPlugin: Plugin = async (_input: PluginInput) => {
|
|
99
|
-
return {
|
|
100
|
-
provider: {
|
|
101
|
-
id: RESPONSES_PROVIDER_ID,
|
|
102
|
-
models: async (provider) => {
|
|
103
|
-
return discoverBucket('responses', provider, {
|
|
104
|
-
id: RESPONSES_PROVIDER_ID,
|
|
105
|
-
url: '',
|
|
106
|
-
npm: '@ai-sdk/openai',
|
|
107
|
-
})
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
}
|
|
315
|
+
return {}
|
|
111
316
|
}
|
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
|