opencode-plugin-litellm 0.4.1 → 0.5.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 +21 -7
- package/package.json +1 -1
- package/src/plugin/index.ts +182 -91
package/README.md
CHANGED
|
@@ -46,7 +46,15 @@ npm install opencode-plugin-litellm
|
|
|
46
46
|
// 2. Add to opencode.json
|
|
47
47
|
{
|
|
48
48
|
"$schema": "https://opencode.ai/config.json",
|
|
49
|
-
"plugin": ["opencode-plugin-litellm@latest"]
|
|
49
|
+
"plugin": ["opencode-plugin-litellm@latest"],
|
|
50
|
+
"provider": {
|
|
51
|
+
"litellm": {
|
|
52
|
+
"npm": "@ai-sdk/openai-compatible",
|
|
53
|
+
"options": {
|
|
54
|
+
"baseURL": "http://localhost:4000/v1"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
50
58
|
}
|
|
51
59
|
```
|
|
52
60
|
|
|
@@ -58,8 +66,6 @@ litellm --config config.yaml --port 4000
|
|
|
58
66
|
opencode
|
|
59
67
|
```
|
|
60
68
|
|
|
61
|
-
That's it. No provider block required.
|
|
62
|
-
|
|
63
69
|
## 🎯 Features
|
|
64
70
|
|
|
65
71
|
| | |
|
|
@@ -79,14 +85,22 @@ That's it. No provider block required.
|
|
|
79
85
|
|
|
80
86
|
## ⚙️ Configuration
|
|
81
87
|
|
|
82
|
-
###
|
|
88
|
+
### Minimal config (recommended)
|
|
83
89
|
|
|
84
|
-
|
|
90
|
+
Point at your LiteLLM proxy — the plugin discovers all models automatically:
|
|
85
91
|
|
|
86
|
-
```
|
|
92
|
+
```jsonc
|
|
87
93
|
{
|
|
88
94
|
"$schema": "https://opencode.ai/config.json",
|
|
89
|
-
"plugin": ["opencode-plugin-litellm@latest"]
|
|
95
|
+
"plugin": ["opencode-plugin-litellm@latest"],
|
|
96
|
+
"provider": {
|
|
97
|
+
"litellm": {
|
|
98
|
+
"npm": "@ai-sdk/openai-compatible",
|
|
99
|
+
"options": {
|
|
100
|
+
"baseURL": "http://localhost:4000/v1"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
90
104
|
}
|
|
91
105
|
```
|
|
92
106
|
|
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.5.0",
|
|
5
5
|
"description": "OpenCode plugin for LiteLLM proxy support with auto-detection and dynamic model discovery",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./src/index.ts",
|
package/src/plugin/index.ts
CHANGED
|
@@ -1,45 +1,74 @@
|
|
|
1
|
-
import type { Plugin, PluginInput
|
|
2
|
-
import {
|
|
1
|
+
import type { Plugin, PluginInput } from '@opencode-ai/plugin'
|
|
2
|
+
import {
|
|
3
|
+
autoDetectLiteLLM,
|
|
4
|
+
checkLiteLLMHealth,
|
|
5
|
+
discoverLiteLLMModels,
|
|
6
|
+
normalizeBaseURL,
|
|
7
|
+
} from '../utils/litellm-api'
|
|
8
|
+
import {
|
|
9
|
+
formatModelName,
|
|
10
|
+
extractModelOwner,
|
|
11
|
+
categorizeModel,
|
|
12
|
+
} from '../utils/format-model-name'
|
|
13
|
+
import type { LiteLLMModel } from '../types'
|
|
3
14
|
|
|
4
15
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
5
|
-
const
|
|
16
|
+
const DISCOVERY_TIMEOUT_MS = 5000
|
|
6
17
|
|
|
7
18
|
/**
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
* Read `customHeaders` from a provider options block.
|
|
20
|
+
*/
|
|
21
|
+
function readCustomHeaders(
|
|
22
|
+
options: Record<string, unknown>,
|
|
23
|
+
): Record<string, string> | undefined {
|
|
24
|
+
const raw = options.customHeaders
|
|
25
|
+
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
26
|
+
const out: Record<string, string> = {}
|
|
27
|
+
for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
|
|
28
|
+
if (typeof v === 'string') out[k] = v
|
|
29
|
+
}
|
|
30
|
+
return Object.keys(out).length > 0 ? out : undefined
|
|
31
|
+
}
|
|
32
|
+
return undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Convert a discovered LiteLLM model into an OpenCode config-level
|
|
37
|
+
* model entry (the shape used in `provider.*.models` inside
|
|
38
|
+
* `opencode.json`).
|
|
14
39
|
*/
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
const entry =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
function toConfigModel(model: LiteLLMModel): Record<string, unknown> {
|
|
41
|
+
const type = categorizeModel(model)
|
|
42
|
+
const entry: Record<string, unknown> = {
|
|
43
|
+
name: formatModelName(model),
|
|
44
|
+
}
|
|
45
|
+
if (model.max_input_tokens || model.max_output_tokens) {
|
|
46
|
+
entry.limit = {
|
|
47
|
+
context: model.max_input_tokens ?? 0,
|
|
48
|
+
output: model.max_output_tokens ?? 0,
|
|
24
49
|
}
|
|
25
50
|
}
|
|
51
|
+
if (model.supports_function_calling) {
|
|
52
|
+
entry.tool_call = true
|
|
53
|
+
}
|
|
54
|
+
if (model.supports_vision) {
|
|
55
|
+
entry.attachment = true
|
|
56
|
+
}
|
|
57
|
+
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
58
|
+
// skip non-chat models from the config — they can't be used as
|
|
59
|
+
// primary chat models and would clutter the picker.
|
|
60
|
+
return entry
|
|
61
|
+
}
|
|
62
|
+
return entry
|
|
26
63
|
}
|
|
27
64
|
|
|
28
65
|
/**
|
|
29
66
|
* LiteLLM Plugin for OpenCode.
|
|
30
67
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* By default, every discovered model is registered under the
|
|
36
|
-
* `litellm` provider — including OpenAI reasoning-tier models like
|
|
37
|
-
* `gpt-5*`. Most of those models work fine through
|
|
38
|
-
* `/v1/chat/completions` for normal use. If you actually hit the
|
|
39
|
-
* "Function tools with reasoning_effort are not supported" error
|
|
40
|
-
* from OpenAI, declare a sibling `litellm-responses` provider in
|
|
41
|
-
* `opencode.json` (see {@link LiteLLMResponsesPlugin}) and the
|
|
42
|
-
* plugin will route those models through `/v1/responses` instead.
|
|
68
|
+
* Uses the `config` hook to discover models from a LiteLLM proxy and
|
|
69
|
+
* inject them into the provider's `models` map at startup. This is the
|
|
70
|
+
* only reliable way to dynamically populate a provider — the
|
|
71
|
+
* `provider.models` hook is not called by OpenCode for custom providers.
|
|
43
72
|
*
|
|
44
73
|
* Configure the provider in your `opencode.json`:
|
|
45
74
|
*
|
|
@@ -59,71 +88,133 @@ function ensureProviderHasModels(config: Config, providerID: string): void {
|
|
|
59
88
|
*/
|
|
60
89
|
export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
61
90
|
return {
|
|
62
|
-
config: async (config:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
91
|
+
config: async (config: any) => {
|
|
92
|
+
// Ensure the provider entry exists
|
|
93
|
+
if (!config.provider) config.provider = {}
|
|
94
|
+
|
|
95
|
+
const existing = config.provider[CHAT_PROVIDER_ID] as
|
|
96
|
+
| Record<string, unknown>
|
|
97
|
+
| undefined
|
|
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
|
+
}
|
|
124
|
+
|
|
125
|
+
// Create provider entry if it doesn't exist
|
|
126
|
+
if (!existing) {
|
|
127
|
+
config.provider[CHAT_PROVIDER_ID] = {
|
|
75
128
|
npm: '@ai-sdk/openai-compatible',
|
|
76
|
-
|
|
77
|
-
|
|
129
|
+
name: 'LiteLLM (proxy)',
|
|
130
|
+
options: {
|
|
131
|
+
baseURL: `${baseURL}/v1`,
|
|
132
|
+
},
|
|
133
|
+
models: {},
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const provider = config.provider[CHAT_PROVIDER_ID] as Record<
|
|
138
|
+
string,
|
|
139
|
+
unknown
|
|
140
|
+
>
|
|
141
|
+
|
|
142
|
+
// Ensure npm is set
|
|
143
|
+
if (!provider.npm) {
|
|
144
|
+
provider.npm = '@ai-sdk/openai-compatible'
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Ensure options.baseURL is set
|
|
148
|
+
if (!provider.options) {
|
|
149
|
+
provider.options = { baseURL: `${baseURL}/v1` }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Ensure models map exists
|
|
153
|
+
if (!provider.models) {
|
|
154
|
+
provider.models = {}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const models = provider.models as Record<string, unknown>
|
|
158
|
+
|
|
159
|
+
// Discover models with timeout
|
|
160
|
+
const work = async () => {
|
|
161
|
+
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
162
|
+
console.warn(
|
|
163
|
+
`[opencode-litellm] LiteLLM appears offline or unauthorized at ${baseURL}`,
|
|
164
|
+
)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let discovered: LiteLLMModel[]
|
|
169
|
+
try {
|
|
170
|
+
discovered = await discoverLiteLLMModels(
|
|
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
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (discovered.length === 0) {
|
|
184
|
+
console.warn(
|
|
185
|
+
'[opencode-litellm] LiteLLM responded but exposed zero models.',
|
|
186
|
+
)
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const model of discovered) {
|
|
191
|
+
// Don't overwrite user-curated entries
|
|
192
|
+
if (models[model.id]) continue
|
|
193
|
+
models[model.id] = toConfigModel(model)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Remove the seed placeholder if real models were discovered
|
|
197
|
+
if (models['_'] && Object.keys(models).length > 1) {
|
|
198
|
+
delete models['_']
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
console.log(
|
|
202
|
+
`[opencode-litellm] Discovered ${discovered.length} models from ${baseURL}`,
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
await Promise.race([
|
|
207
|
+
work(),
|
|
208
|
+
new Promise<void>((resolve) =>
|
|
209
|
+
setTimeout(resolve, DISCOVERY_TIMEOUT_MS),
|
|
210
|
+
),
|
|
211
|
+
])
|
|
78
212
|
},
|
|
79
213
|
}
|
|
80
214
|
}
|
|
81
215
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
* provider for OpenAI reasoning-tier models.
|
|
85
|
-
*
|
|
86
|
-
* OpenAI's reasoning-tier models (`gpt-5*`, `o1`/`o3`/`o4*`) reject
|
|
87
|
-
* requests that combine `reasoning_effort` with function tools when
|
|
88
|
-
* sent to `/v1/chat/completions`. Routing those models through
|
|
89
|
-
* `/v1/responses` (the OpenAI Responses API) avoids the rejection.
|
|
90
|
-
*
|
|
91
|
-
* This plugin is only active when the `litellm-responses` provider
|
|
92
|
-
* is declared in `opencode.json`. When it is, the chat-only `litellm`
|
|
93
|
-
* provider continues to register every discovered model — set
|
|
94
|
-
* `chatApiModels` / `responsesApiModels` on either provider's options
|
|
95
|
-
* if you want to control which side a particular model lives on.
|
|
96
|
-
*
|
|
97
|
-
* Example `opencode.json` snippet:
|
|
98
|
-
*
|
|
99
|
-
* {
|
|
100
|
-
* "provider": {
|
|
101
|
-
* "litellm-responses": {
|
|
102
|
-
* "npm": "@ai-sdk/openai",
|
|
103
|
-
* "name": "LiteLLM (responses)",
|
|
104
|
-
* "options": {
|
|
105
|
-
* "baseURL": "http://localhost:4000/v1",
|
|
106
|
-
* "apiKey": "{env:LITELLM_API_KEY}",
|
|
107
|
-
* "compatibility": "strict"
|
|
108
|
-
* }
|
|
109
|
-
* }
|
|
110
|
-
* }
|
|
111
|
-
* }
|
|
112
|
-
*/
|
|
216
|
+
// Re-export the responses plugin for backwards compat, but it's now a no-op.
|
|
217
|
+
// The config hook approach handles all models in a single provider.
|
|
113
218
|
export const LiteLLMResponsesPlugin: Plugin = async (_input: PluginInput) => {
|
|
114
|
-
return {
|
|
115
|
-
config: async (config: Config) => {
|
|
116
|
-
ensureProviderHasModels(config, RESPONSES_PROVIDER_ID)
|
|
117
|
-
},
|
|
118
|
-
provider: {
|
|
119
|
-
id: RESPONSES_PROVIDER_ID,
|
|
120
|
-
models: async (provider) => {
|
|
121
|
-
return discoverBucket('responses', provider, {
|
|
122
|
-
id: RESPONSES_PROVIDER_ID,
|
|
123
|
-
url: '',
|
|
124
|
-
npm: '@ai-sdk/openai',
|
|
125
|
-
})
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
}
|
|
219
|
+
return {}
|
|
129
220
|
}
|