opencode-plugin-litellm 0.7.0 โ 0.8.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 +9 -1
- package/package.json +1 -1
- package/src/plugin/build-model.ts +10 -1
- package/src/plugin/discover.ts +22 -7
- package/src/plugin/index.ts +36 -2
- package/src/types/index.ts +11 -0
- package/src/utils/litellm-api.ts +19 -0
package/README.md
CHANGED
|
@@ -74,6 +74,7 @@ opencode
|
|
|
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
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
|
+
| ๐ต **Real pricing** | Maps `input_cost_per_token` / `output_cost_per_token` from `/v1/model/info` into OpenCode's `cost` field, so the picker and `/cost` show what the proxy actually bills instead of `$0.00`. Models LiteLLM has no price for are left unpriced, not falsely marked free. |
|
|
77
78
|
| ๐งช **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
79
|
| ๐ข **Provider extraction** | Pulls `litellm_provider` (or the `provider/model` prefix) into `organizationOwner` so models group correctly in the UI. |
|
|
79
80
|
| ๐ **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars or `provider.litellm.options.apiKey`. |
|
|
@@ -193,6 +194,13 @@ detects reasoning-tier models from their id (`gpt-5*`, `o1*`, `o3*`,
|
|
|
193
194
|
`o4*`) and from LiteLLM's `mode === 'responses'` field, and creates the
|
|
194
195
|
sibling provider lazily.
|
|
195
196
|
|
|
197
|
+
If LiteLLM reports per-model reasoning-effort support (e.g.
|
|
198
|
+
`supports_low_reasoning_effort`, `supports_medium_reasoning_effort`,
|
|
199
|
+
`supports_high_reasoning_effort` in `model_info`), the plugin automatically
|
|
200
|
+
surfaces those as OpenCode variants under the discovered model. Each variant
|
|
201
|
+
sets `reasoningEffort` to the reported level, so you can switch between
|
|
202
|
+
effort levels from the model picker without hand-curating every entry.
|
|
203
|
+
|
|
196
204
|
To override the routing per model:
|
|
197
205
|
|
|
198
206
|
```jsonc
|
|
@@ -292,7 +300,7 @@ sequenceDiagram
|
|
|
292
300
|
1. On OpenCode startup the `config` lifecycle hook fires.
|
|
293
301
|
2. If `provider.litellm` exists, its `baseURL` is used. Otherwise common ports are probed.
|
|
294
302
|
3. A health check (`GET /v1/models`) verifies the proxy is reachable and authorized.
|
|
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`,
|
|
303
|
+
4. Models from the response are enriched with `/v1/model/info` metadata (`mode`, token limits, capability flags, and per-token pricing โ `/v1/models` omits these for database-defined models) and converted into OpenCode model entries keyed by `id`, with formatted `name`, `organizationOwner`, inferred `modalities`, and `cost` (USD/1M tokens, converted from LiteLLM's USD/token). Non-chat models (embedding / image / audio) are excluded from the picker.
|
|
296
304
|
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.
|
|
297
305
|
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.
|
|
298
306
|
7. The whole flow is wrapped in a `Promise.race` against a 20 s timeout so a slow proxy never blocks boot.
|
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.8.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",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Model as ModelV2 } from '@opencode-ai/sdk/v2'
|
|
2
|
-
import type { LiteLLMModel } from '../types'
|
|
2
|
+
import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
|
|
3
3
|
import {
|
|
4
4
|
categorizeModel,
|
|
5
5
|
formatModelName,
|
|
@@ -20,6 +20,7 @@ export function buildModelV2(
|
|
|
20
20
|
providerID: string,
|
|
21
21
|
api: { id: string; url: string; npm: string },
|
|
22
22
|
model: LiteLLMModel,
|
|
23
|
+
info?: LiteLLMModelInfo,
|
|
23
24
|
): ModelV2 {
|
|
24
25
|
const type = categorizeModel(model)
|
|
25
26
|
const isVision = !!model.supports_vision
|
|
@@ -66,5 +67,13 @@ export function buildModelV2(
|
|
|
66
67
|
options: {},
|
|
67
68
|
headers: {},
|
|
68
69
|
release_date: '',
|
|
70
|
+
variants: info?.supports_reasoning_efforts?.length
|
|
71
|
+
? Object.fromEntries(
|
|
72
|
+
info.supports_reasoning_efforts.map((effort) => [
|
|
73
|
+
effort,
|
|
74
|
+
{ reasoningEffort: effort },
|
|
75
|
+
]),
|
|
76
|
+
)
|
|
77
|
+
: undefined,
|
|
69
78
|
}
|
|
70
79
|
}
|
package/src/plugin/discover.ts
CHANGED
|
@@ -2,11 +2,12 @@ import type { Model as ModelV2, Provider as ProviderV2 } from '@opencode-ai/sdk/
|
|
|
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 { requiresResponsesAPI } from '../utils/format-model-name'
|
|
9
|
-
import type { LiteLLMModel, Transport, TransportPolicy } from '../types'
|
|
10
|
+
import type { LiteLLMModel, LiteLLMModelInfo, Transport, TransportPolicy } from '../types'
|
|
10
11
|
import { buildModelV2 } from './build-model'
|
|
11
12
|
|
|
12
13
|
const DISCOVERY_TIMEOUT_MS = 5000
|
|
@@ -138,16 +139,29 @@ export async function discoverBucket(
|
|
|
138
139
|
return
|
|
139
140
|
}
|
|
140
141
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
143
|
+
discoverLiteLLMModels(baseURL, apiKey, customHeaders),
|
|
144
|
+
discoverLiteLLMModelInfo(baseURL, apiKey, customHeaders),
|
|
145
|
+
])
|
|
146
|
+
|
|
147
|
+
if (modelsResult.status === 'rejected') {
|
|
145
148
|
console.warn(
|
|
146
149
|
'[opencode-litellm] Model discovery failed:',
|
|
147
|
-
|
|
150
|
+
modelsResult.reason instanceof Error ? modelsResult.reason.message : String(modelsResult.reason),
|
|
148
151
|
)
|
|
149
152
|
return
|
|
150
153
|
}
|
|
154
|
+
const models = modelsResult.value
|
|
155
|
+
|
|
156
|
+
let infoByName: Map<string, LiteLLMModelInfo> | null = null
|
|
157
|
+
if (infoResult.status === 'fulfilled') {
|
|
158
|
+
infoByName = infoResult.value
|
|
159
|
+
} else {
|
|
160
|
+
console.warn(
|
|
161
|
+
'[opencode-litellm] /v1/model/info unavailable; model costs will be zero:',
|
|
162
|
+
infoResult.reason instanceof Error ? infoResult.reason.message : String(infoResult.reason),
|
|
163
|
+
)
|
|
164
|
+
}
|
|
151
165
|
|
|
152
166
|
if (models.length === 0) {
|
|
153
167
|
console.warn(
|
|
@@ -176,7 +190,8 @@ export async function discoverBucket(
|
|
|
176
190
|
// it with "team not allowed"). Set `api.id` per-model so each
|
|
177
191
|
// entry carries the correct upstream model name.
|
|
178
192
|
const perModelApi = { ...resolvedApi, id: model.id }
|
|
179
|
-
|
|
193
|
+
const info = infoByName?.get(model.id) ?? {}
|
|
194
|
+
out[model.id] = buildModelV2(resolvedApi.id, perModelApi, model, info)
|
|
180
195
|
}
|
|
181
196
|
}
|
|
182
197
|
|
package/src/plugin/index.ts
CHANGED
|
@@ -77,9 +77,19 @@ function enrichModel(model: LiteLLMModel, info: LiteLLMModelInfo): LiteLLMModel
|
|
|
77
77
|
supports_reasoning: model.supports_reasoning ?? info.supports_reasoning,
|
|
78
78
|
supports_pdf_input: model.supports_pdf_input ?? info.supports_pdf_input,
|
|
79
79
|
supports_audio_input: model.supports_audio_input ?? info.supports_audio_input,
|
|
80
|
+
input_cost_per_token: model.input_cost_per_token ?? info.input_cost_per_token,
|
|
81
|
+
output_cost_per_token: model.output_cost_per_token ?? info.output_cost_per_token,
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
|
|
85
|
+
/**
|
|
86
|
+
* OpenCode's `cost` config field is USD per **million** tokens (the
|
|
87
|
+
* models.dev convention); LiteLLM's `/v1/model/info` reports USD per
|
|
88
|
+
* single token. `1e6` bridges the two โ verified against a live
|
|
89
|
+
* `x-litellm-response-cost` header, not just the unit names.
|
|
90
|
+
*/
|
|
91
|
+
const USD_PER_TOKEN_TO_PER_MILLION = 1_000_000
|
|
92
|
+
|
|
83
93
|
/**
|
|
84
94
|
* Convert a discovered LiteLLM model into an OpenCode config-level
|
|
85
95
|
* model entry (the shape used in `provider.*.models` inside
|
|
@@ -87,7 +97,10 @@ function enrichModel(model: LiteLLMModel, info: LiteLLMModelInfo): LiteLLMModel
|
|
|
87
97
|
* image, audio) โ they can't be used as primary chat models and would
|
|
88
98
|
* clutter the picker.
|
|
89
99
|
*/
|
|
90
|
-
function toConfigModel(
|
|
100
|
+
function toConfigModel(
|
|
101
|
+
model: LiteLLMModel,
|
|
102
|
+
info?: LiteLLMModelInfo,
|
|
103
|
+
): Record<string, unknown> | null {
|
|
91
104
|
const type = categorizeModel(model)
|
|
92
105
|
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
93
106
|
return null
|
|
@@ -110,6 +123,16 @@ function toConfigModel(model: LiteLLMModel): Record<string, unknown> | null {
|
|
|
110
123
|
if (model.supports_vision) {
|
|
111
124
|
entry.attachment = true
|
|
112
125
|
}
|
|
126
|
+
// Only emit `cost` when LiteLLM actually reported a price. Omitting
|
|
127
|
+
// it (rather than defaulting to 0) lets OpenCode/models.dev fall back
|
|
128
|
+
// to their own default instead of us asserting "this model is free"
|
|
129
|
+
// for something LiteLLM simply has no price anchor for (e.g. rerank).
|
|
130
|
+
if (model.input_cost_per_token != null || model.output_cost_per_token != null) {
|
|
131
|
+
entry.cost = {
|
|
132
|
+
input: (model.input_cost_per_token ?? 0) * USD_PER_TOKEN_TO_PER_MILLION,
|
|
133
|
+
output: (model.output_cost_per_token ?? 0) * USD_PER_TOKEN_TO_PER_MILLION,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
113
136
|
const input: Array<'text' | 'image' | 'pdf' | 'audio'> = ['text']
|
|
114
137
|
if (model.supports_vision) input.push('image')
|
|
115
138
|
if (model.supports_pdf_input) input.push('pdf')
|
|
@@ -117,6 +140,14 @@ function toConfigModel(model: LiteLLMModel): Record<string, unknown> | null {
|
|
|
117
140
|
if (input.length > 1) {
|
|
118
141
|
entry.modalities = { input, output: ['text'] }
|
|
119
142
|
}
|
|
143
|
+
entry.variants = info?.supports_reasoning_efforts?.length
|
|
144
|
+
? Object.fromEntries(
|
|
145
|
+
info.supports_reasoning_efforts.map((effort) => [
|
|
146
|
+
effort,
|
|
147
|
+
{ reasoningEffort: effort },
|
|
148
|
+
]),
|
|
149
|
+
)
|
|
150
|
+
: undefined
|
|
120
151
|
return entry
|
|
121
152
|
}
|
|
122
153
|
|
|
@@ -303,7 +334,10 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
303
334
|
if (models[model.id]) continue
|
|
304
335
|
const info = infoByName?.get(model.id)
|
|
305
336
|
if (infoByName && !info) unmatched.push(model.id)
|
|
306
|
-
const entry = toConfigModel(
|
|
337
|
+
const entry = toConfigModel(
|
|
338
|
+
info ? enrichModel(model, info) : model,
|
|
339
|
+
info,
|
|
340
|
+
)
|
|
307
341
|
if (!entry) {
|
|
308
342
|
skipped++
|
|
309
343
|
continue
|
package/src/types/index.ts
CHANGED
|
@@ -32,6 +32,14 @@ export interface LiteLLMModel {
|
|
|
32
32
|
supports_reasoning?: boolean
|
|
33
33
|
supports_pdf_input?: boolean
|
|
34
34
|
supports_audio_input?: boolean
|
|
35
|
+
/**
|
|
36
|
+
* USD price per input/output token, reliably available via
|
|
37
|
+
* `/v1/model/info` (`/v1/models` omits pricing). Absent for models
|
|
38
|
+
* LiteLLM has no price anchor for (e.g. rerank) โ treat as "unknown",
|
|
39
|
+
* not "free".
|
|
40
|
+
*/
|
|
41
|
+
input_cost_per_token?: number
|
|
42
|
+
output_cost_per_token?: number
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
export interface LiteLLMModelsResponse {
|
|
@@ -56,8 +64,11 @@ export interface LiteLLMModelInfo {
|
|
|
56
64
|
supports_function_calling?: boolean
|
|
57
65
|
supports_vision?: boolean
|
|
58
66
|
supports_reasoning?: boolean
|
|
67
|
+
supports_reasoning_efforts?: string[]
|
|
59
68
|
supports_pdf_input?: boolean
|
|
60
69
|
supports_audio_input?: boolean
|
|
70
|
+
input_cost_per_token?: number
|
|
71
|
+
output_cost_per_token?: number
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
/** A single entry returned by LiteLLM's `/v1/model/info` endpoint. */
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -125,6 +125,25 @@ export async function discoverLiteLLMModelInfo(
|
|
|
125
125
|
info[flag] = paramsValue
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
|
|
129
|
+
// Add supports reasoning efforts if present in litellm_params
|
|
130
|
+
const reasoningEffortPattern = /^supports_([a-z]+)_reasoning_effort$/
|
|
131
|
+
const efforts = new Set<string>()
|
|
132
|
+
for (const source of [info, entry.litellm_params]) {
|
|
133
|
+
if (!source || typeof source !== 'object') continue
|
|
134
|
+
for (const [key, value] of Object.entries(source)) {
|
|
135
|
+
const match = key.match(reasoningEffortPattern)
|
|
136
|
+
if (match && value === true) {
|
|
137
|
+
efforts.add(match[1])
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (efforts.size > 0) {
|
|
142
|
+
info.supports_reasoning_efforts = [
|
|
143
|
+
...new Set([...(info.supports_reasoning_efforts ?? []), ...efforts]),
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
|
|
128
147
|
// Index under every alias LiteLLM may use for this model โ the
|
|
129
148
|
// `/v1/models` id can match any of them depending on how the
|
|
130
149
|
// deployment names its entries (alias vs upstream model string).
|