opencode-plugin-litellm 0.3.0 โ 0.4.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 +70 -3
- package/package.json +1 -1
- package/src/plugin/discover.ts +27 -7
- package/src/plugin/index.ts +24 -1
- package/src/types/index.ts +19 -0
- package/src/utils/litellm-api.ts +10 -5
package/README.md
CHANGED
|
@@ -71,6 +71,7 @@ That's it. No provider block required.
|
|
|
71
71
|
| ๐งช **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`. |
|
|
72
72
|
| ๐ข **Provider extraction** | Pulls `litellm_provider` (or the `provider/model` prefix) into `organizationOwner` so models group correctly in the UI. |
|
|
73
73
|
| ๐ **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars or `provider.litellm.options.apiKey`. |
|
|
74
|
+
| ๐ **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways requiring extra HTTP headers. |
|
|
74
75
|
| โฑ๏ธ **Non-blocking startup** | Discovery is capped at **5 s** โ a slow or offline proxy never delays OpenCode boot. |
|
|
75
76
|
| ๐ค **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
76
77
|
| ๐ชถ **Zero runtime deps** | Only depends on `@opencode-ai/plugin`. No build step, no bundler. |
|
|
@@ -89,9 +90,9 @@ If LiteLLM is on `localhost:4000`, `:8000`, or `:8080`, the plugin self-configur
|
|
|
89
90
|
}
|
|
90
91
|
```
|
|
91
92
|
|
|
92
|
-
### Explicit provider
|
|
93
|
+
### Explicit provider (custom URL or auth)
|
|
93
94
|
|
|
94
|
-
|
|
95
|
+
You **do not need to list any models** โ the plugin still discovers them from `/v1/models` automatically. Use this form only when you need to point at a non-default URL or pass an API key:
|
|
95
96
|
|
|
96
97
|
```jsonc
|
|
97
98
|
{
|
|
@@ -101,6 +102,25 @@ Override the URL, set an API key, or pre-define curated models that the plugin w
|
|
|
101
102
|
"litellm": {
|
|
102
103
|
"npm": "@ai-sdk/openai-compatible",
|
|
103
104
|
"name": "LiteLLM (proxy)",
|
|
105
|
+
"options": {
|
|
106
|
+
"baseURL": "http://litellm.internal.example.com/v1",
|
|
107
|
+
"apiKey": "{env:LITELLM_API_KEY}"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
That's the whole config โ every model in your LiteLLM `model_list` will appear in the picker.
|
|
115
|
+
|
|
116
|
+
### Overriding or curating individual models (optional)
|
|
117
|
+
|
|
118
|
+
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:
|
|
119
|
+
|
|
120
|
+
```jsonc
|
|
121
|
+
{
|
|
122
|
+
"provider": {
|
|
123
|
+
"litellm": {
|
|
104
124
|
"options": {
|
|
105
125
|
"baseURL": "http://litellm.internal.example.com/v1",
|
|
106
126
|
"apiKey": "{env:LITELLM_API_KEY}"
|
|
@@ -116,7 +136,7 @@ Override the URL, set an API key, or pre-define curated models that the plugin w
|
|
|
116
136
|
}
|
|
117
137
|
```
|
|
118
138
|
|
|
119
|
-
|
|
139
|
+
Here, `openai/gpt-4o` keeps your custom name; every other model from the proxy is still discovered and added automatically.
|
|
120
140
|
|
|
121
141
|
### Reasoning models (gpt-5, o1/o3/o4)
|
|
122
142
|
|
|
@@ -177,6 +197,29 @@ If your LiteLLM proxy requires a master key, expose it via either approach:
|
|
|
177
197
|
|
|
178
198
|
The env var path lets you commit `opencode.json` without leaking secrets.
|
|
179
199
|
|
|
200
|
+
### Custom headers (Cloudflare Access, API gateways)
|
|
201
|
+
|
|
202
|
+
If your LiteLLM proxy is behind Cloudflare Access or another gateway that requires extra HTTP headers, use the `customHeaders` option:
|
|
203
|
+
|
|
204
|
+
```jsonc
|
|
205
|
+
{
|
|
206
|
+
"provider": {
|
|
207
|
+
"litellm": {
|
|
208
|
+
"options": {
|
|
209
|
+
"baseURL": "https://litellm.internal.example.com/v1",
|
|
210
|
+
"apiKey": "{env:LITELLM_API_KEY}",
|
|
211
|
+
"customHeaders": {
|
|
212
|
+
"CF-Access-Client-Id": "{env:CF_ACCESS_CLIENT_ID}",
|
|
213
|
+
"CF-Access-Client-Secret": "{env:CF_ACCESS_CLIENT_SECRET}"
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
These headers are included in every request the plugin makes during model discovery (health check and `/v1/models`). To obtain a Cloudflare Access Service Token, follow the [Cloudflare docs](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/).
|
|
222
|
+
|
|
180
223
|
## ๐ง How it works
|
|
181
224
|
|
|
182
225
|
```mermaid
|
|
@@ -266,6 +309,30 @@ The unscoped `opencode-litellm` was already published by another author when thi
|
|
|
266
309
|
Yes โ anything in your LiteLLM `model_list` shows up, including Ollama, Bedrock, Azure, OpenAI, Anthropic, Google, etc. That's the whole point of LiteLLM.
|
|
267
310
|
</details>
|
|
268
311
|
|
|
312
|
+
<details>
|
|
313
|
+
<summary><b>My LiteLLM proxy is behind Cloudflare Access โ how do I authenticate?</b></summary>
|
|
314
|
+
|
|
315
|
+
Cloudflare Access intercepts requests before they reach LiteLLM, so a plain `Authorization: Bearer` header isn't enough. Create a [Cloudflare Access Service Token](https://developers.cloudflare.com/cloudflare-one/identity/service-tokens/) and pass the credentials via `customHeaders`:
|
|
316
|
+
|
|
317
|
+
```jsonc
|
|
318
|
+
{
|
|
319
|
+
"provider": {
|
|
320
|
+
"litellm": {
|
|
321
|
+
"options": {
|
|
322
|
+
"baseURL": "https://litellm.your-company.com/v1",
|
|
323
|
+
"customHeaders": {
|
|
324
|
+
"CF-Access-Client-Id": "{env:CF_ACCESS_CLIENT_ID}",
|
|
325
|
+
"CF-Access-Client-Secret": "{env:CF_ACCESS_CLIENT_SECRET}"
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
The `customHeaders` map works for any gateway that requires extra HTTP headers โ not just Cloudflare.
|
|
334
|
+
</details>
|
|
335
|
+
|
|
269
336
|
<details>
|
|
270
337
|
<summary><b>I get <code>Function tools with reasoning_effort are not supported โฆ in /v1/chat/completions</code> โ what do I do?</b></summary>
|
|
271
338
|
|
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.4.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/discover.ts
CHANGED
|
@@ -40,21 +40,41 @@ function pickTransport(
|
|
|
40
40
|
* `opencode.json` wins), then falls back to env vars, and finally to
|
|
41
41
|
* auto-detecting a local proxy on the common ports.
|
|
42
42
|
*/
|
|
43
|
+
/**
|
|
44
|
+
* Extract the `customHeaders` map from the provider options block.
|
|
45
|
+
* Returns `undefined` when no custom headers are configured.
|
|
46
|
+
*/
|
|
47
|
+
function readCustomHeaders(
|
|
48
|
+
provider: ProviderV2 | undefined,
|
|
49
|
+
): Record<string, string> | undefined {
|
|
50
|
+
const options = (provider?.options ?? {}) as Record<string, unknown>
|
|
51
|
+
const raw = options.customHeaders
|
|
52
|
+
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
53
|
+
const out: Record<string, string> = {}
|
|
54
|
+
for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
|
|
55
|
+
if (typeof v === 'string') out[k] = v
|
|
56
|
+
}
|
|
57
|
+
return Object.keys(out).length > 0 ? out : undefined
|
|
58
|
+
}
|
|
59
|
+
return undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
43
62
|
async function resolveEndpoint(
|
|
44
63
|
provider: ProviderV2 | undefined,
|
|
45
|
-
): Promise<{ baseURL: string; apiKey?: string } | null> {
|
|
64
|
+
): Promise<{ baseURL: string; apiKey?: string; customHeaders?: Record<string, string> } | null> {
|
|
46
65
|
const options = (provider?.options ?? {}) as Record<string, unknown>
|
|
47
66
|
const configuredBase = typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
48
67
|
const configuredKey = typeof options.apiKey === 'string' && options.apiKey ? options.apiKey : undefined
|
|
49
68
|
const envKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
69
|
+
const customHeaders = readCustomHeaders(provider)
|
|
50
70
|
|
|
51
71
|
if (configuredBase) {
|
|
52
|
-
return { baseURL: normalizeBaseURL(configuredBase), apiKey: configuredKey ?? envKey }
|
|
72
|
+
return { baseURL: normalizeBaseURL(configuredBase), apiKey: configuredKey ?? envKey, customHeaders }
|
|
53
73
|
}
|
|
54
74
|
|
|
55
|
-
const detected = await autoDetectLiteLLM(configuredKey ?? envKey)
|
|
75
|
+
const detected = await autoDetectLiteLLM(configuredKey ?? envKey, customHeaders)
|
|
56
76
|
if (!detected) return null
|
|
57
|
-
return { baseURL: normalizeBaseURL(detected), apiKey: configuredKey ?? envKey }
|
|
77
|
+
return { baseURL: normalizeBaseURL(detected), apiKey: configuredKey ?? envKey, customHeaders }
|
|
58
78
|
}
|
|
59
79
|
|
|
60
80
|
/**
|
|
@@ -112,15 +132,15 @@ export async function discoverBucket(
|
|
|
112
132
|
const endpoint = await resolveEndpoint(provider)
|
|
113
133
|
if (!endpoint) return
|
|
114
134
|
|
|
115
|
-
const { baseURL, apiKey } = endpoint
|
|
116
|
-
if (!(await checkLiteLLMHealth(baseURL, apiKey))) {
|
|
135
|
+
const { baseURL, apiKey, customHeaders } = endpoint
|
|
136
|
+
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
117
137
|
console.warn(`[opencode-litellm] LiteLLM appears offline or unauthorized at ${baseURL}`)
|
|
118
138
|
return
|
|
119
139
|
}
|
|
120
140
|
|
|
121
141
|
let models: LiteLLMModel[]
|
|
122
142
|
try {
|
|
123
|
-
models = await discoverLiteLLMModels(baseURL, apiKey)
|
|
143
|
+
models = await discoverLiteLLMModels(baseURL, apiKey, customHeaders)
|
|
124
144
|
} catch (error) {
|
|
125
145
|
console.warn(
|
|
126
146
|
'[opencode-litellm] Model discovery failed:',
|
package/src/plugin/index.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import type { Plugin, PluginInput } from '@opencode-ai/plugin'
|
|
1
|
+
import type { Plugin, PluginInput, Config } from '@opencode-ai/plugin'
|
|
2
2
|
import { discoverBucket } from './discover'
|
|
3
3
|
|
|
4
4
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
5
5
|
const RESPONSES_PROVIDER_ID = 'litellm-responses'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Ensure a provider entry has a `models` map so OpenCode registers it.
|
|
9
|
+
*
|
|
10
|
+
* OpenCode skips providers that have no models defined in the config,
|
|
11
|
+
* which means the `provider.models` hook would never be called. By
|
|
12
|
+
* seeding an empty `models` map we guarantee the provider is created
|
|
13
|
+
* and the hook has a chance to populate it with discovered models.
|
|
14
|
+
*/
|
|
15
|
+
function ensureProviderHasModels(config: Config, providerID: string): void {
|
|
16
|
+
if (!config.provider) return
|
|
17
|
+
const entry = config.provider[providerID]
|
|
18
|
+
if (!entry) return
|
|
19
|
+
if (!entry.models) {
|
|
20
|
+
entry.models = {}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
7
24
|
/**
|
|
8
25
|
* LiteLLM Plugin for OpenCode.
|
|
9
26
|
*
|
|
@@ -38,6 +55,9 @@ const RESPONSES_PROVIDER_ID = 'litellm-responses'
|
|
|
38
55
|
*/
|
|
39
56
|
export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
40
57
|
return {
|
|
58
|
+
config: async (config: Config) => {
|
|
59
|
+
ensureProviderHasModels(config, CHAT_PROVIDER_ID)
|
|
60
|
+
},
|
|
41
61
|
provider: {
|
|
42
62
|
id: CHAT_PROVIDER_ID,
|
|
43
63
|
models: async (provider) => {
|
|
@@ -88,6 +108,9 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
88
108
|
*/
|
|
89
109
|
export const LiteLLMResponsesPlugin: Plugin = async (_input: PluginInput) => {
|
|
90
110
|
return {
|
|
111
|
+
config: async (config: Config) => {
|
|
112
|
+
ensureProviderHasModels(config, RESPONSES_PROVIDER_ID)
|
|
113
|
+
},
|
|
91
114
|
provider: {
|
|
92
115
|
id: RESPONSES_PROVIDER_ID,
|
|
93
116
|
models: async (provider) => {
|
package/src/types/index.ts
CHANGED
|
@@ -78,4 +78,23 @@ export interface LiteLLMOptions {
|
|
|
78
78
|
* the heuristic but is overridden by `responsesApiModels`.
|
|
79
79
|
*/
|
|
80
80
|
chatApiModels?: string[]
|
|
81
|
+
/**
|
|
82
|
+
* Arbitrary HTTP headers to include in every request to the LiteLLM
|
|
83
|
+
* proxy during model discovery (health check + `/v1/models`).
|
|
84
|
+
*
|
|
85
|
+
* Useful for proxies behind Cloudflare Access or other gateways that
|
|
86
|
+
* require extra authentication headers beyond the standard
|
|
87
|
+
* `Authorization: Bearer` token.
|
|
88
|
+
*
|
|
89
|
+
* Example (Cloudflare Access Service Token):
|
|
90
|
+
* ```json
|
|
91
|
+
* {
|
|
92
|
+
* "customHeaders": {
|
|
93
|
+
* "CF-Access-Client-Id": "{env:CF_ACCESS_CLIENT_ID}",
|
|
94
|
+
* "CF-Access-Client-Secret": "{env:CF_ACCESS_CLIENT_SECRET}"
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
customHeaders?: Record<string, string>
|
|
81
100
|
}
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -21,7 +21,7 @@ export function buildAPIURL(baseURL: string, endpoint: string = MODELS_ENDPOINT)
|
|
|
21
21
|
return `${normalizeBaseURL(baseURL)}${endpoint}`
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function buildHeaders(apiKey?: string): Record<string, string> {
|
|
24
|
+
function buildHeaders(apiKey?: string, customHeaders?: Record<string, string>): Record<string, string> {
|
|
25
25
|
const headers: Record<string, string> = {
|
|
26
26
|
'Content-Type': 'application/json',
|
|
27
27
|
}
|
|
@@ -29,6 +29,9 @@ function buildHeaders(apiKey?: string): Record<string, string> {
|
|
|
29
29
|
if (key) {
|
|
30
30
|
headers['Authorization'] = `Bearer ${key}`
|
|
31
31
|
}
|
|
32
|
+
if (customHeaders) {
|
|
33
|
+
Object.assign(headers, customHeaders)
|
|
34
|
+
}
|
|
32
35
|
return headers
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -36,11 +39,12 @@ function buildHeaders(apiKey?: string): Record<string, string> {
|
|
|
36
39
|
export async function checkLiteLLMHealth(
|
|
37
40
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
38
41
|
apiKey?: string,
|
|
42
|
+
customHeaders?: Record<string, string>,
|
|
39
43
|
): Promise<boolean> {
|
|
40
44
|
try {
|
|
41
45
|
const response = await fetch(buildAPIURL(baseURL), {
|
|
42
46
|
method: 'GET',
|
|
43
|
-
headers: buildHeaders(apiKey),
|
|
47
|
+
headers: buildHeaders(apiKey, customHeaders),
|
|
44
48
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
45
49
|
})
|
|
46
50
|
// 401 still means a server is alive โ we just don't have the right
|
|
@@ -56,11 +60,12 @@ export async function checkLiteLLMHealth(
|
|
|
56
60
|
export async function discoverLiteLLMModels(
|
|
57
61
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
58
62
|
apiKey?: string,
|
|
63
|
+
customHeaders?: Record<string, string>,
|
|
59
64
|
): Promise<LiteLLMModel[]> {
|
|
60
65
|
const url = buildAPIURL(baseURL)
|
|
61
66
|
const response = await fetch(url, {
|
|
62
67
|
method: 'GET',
|
|
63
|
-
headers: buildHeaders(apiKey),
|
|
68
|
+
headers: buildHeaders(apiKey, customHeaders),
|
|
64
69
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
65
70
|
})
|
|
66
71
|
|
|
@@ -77,11 +82,11 @@ export async function discoverLiteLLMModels(
|
|
|
77
82
|
* The default `litellm --port` is 4000, but 8000 is also widely used
|
|
78
83
|
* and 8080 is a common reverse-proxy default.
|
|
79
84
|
*/
|
|
80
|
-
export async function autoDetectLiteLLM(apiKey?: string): Promise<string | null> {
|
|
85
|
+
export async function autoDetectLiteLLM(apiKey?: string, customHeaders?: Record<string, string>): Promise<string | null> {
|
|
81
86
|
const commonPorts = [4000, 8000, 8080]
|
|
82
87
|
for (const port of commonPorts) {
|
|
83
88
|
const baseURL = `http://localhost:${port}`
|
|
84
|
-
if (await checkLiteLLMHealth(baseURL, apiKey)) {
|
|
89
|
+
if (await checkLiteLLMHealth(baseURL, apiKey, customHeaders)) {
|
|
85
90
|
return baseURL
|
|
86
91
|
}
|
|
87
92
|
}
|