opencode-plugin-litellm 1.7.0 → 1.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 +6 -4
- package/package.json +2 -3
- package/src/plugin/index.ts +21 -2
- package/src/plugin/v2.ts +52 -17
- package/src/utils/model-cache.ts +10 -2
package/README.md
CHANGED
|
@@ -74,11 +74,11 @@ also shows how to configure a provider directly in `opencode.json`.
|
|
|
74
74
|
| 📡 **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
75
75
|
| ⚡ **Stale-while-revalidate** | A warm disk cache supplies models without a network request. New sessions trigger a background refresh, and cache entries expire after 7 days. |
|
|
76
76
|
| 🏷️ **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`. Set `formatModelNames: false` to keep the raw LiteLLM ids instead. |
|
|
77
|
-
| 🧠 **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
|
+
| 🧠 **Modality-aware** | Enriches `/v1/models` entries with `/v1/model/info` (`mode`, token limits, capability flags) and hides embedding / image / audio models from the picker. Declares `temperature` support only for models whose `supported_openai_params` lists it, so OpenCode applies (and the user can set) a sampling temperature on exactly those models. |
|
|
78
78
|
| 💵 **Real pricing** | Maps `input_cost_per_token` / `output_cost_per_token` (and cache read/write costs) 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. |
|
|
79
79
|
| 🧩 **Reasoning-effort variants** | When LiteLLM reports per-model effort support (`supports_low_reasoning_effort`, …), the plugin surfaces each level as a picker variant automatically. |
|
|
80
80
|
| 🧭 **Responses-API routing** | Models LiteLLM reports with `mode: "responses"` are invoked through `/v1/responses` instead of `/v1/chat/completions`, with no extra configuration. OpenCode 2 overrides the runtime package per model; OpenCode 1 gets a `<provider>-responses` sibling. |
|
|
81
|
-
| 🔐 **Auth-aware** | Honours `
|
|
81
|
+
| 🔐 **Auth-aware** | Honours `providers.litellm.settings.apiKey`, OpenCode `/connect` credentials, the box auth store, and `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` on both the V1 server path and the OpenCode 2 plugin host; on OpenCode 2 a **LiteLLM** entry in the connect-provider dialog (key plus optional base URL) connects a gateway entirely from the web UI. A saved credential beats the environment key. |
|
|
82
82
|
| 🌐 **Gateway-friendly** | Supports provider `headers` (and the legacy `customHeaders` option) for proxies behind Cloudflare Access or other API gateways. |
|
|
83
83
|
| 🧩 **Splittable catalog** | `includeModels` / `excludeModels` (glob patterns) let one LiteLLM proxy be divided into several OpenCode providers — e.g. by naming prefix — without hand-maintaining a model list. |
|
|
84
84
|
| 🎚️ **Capability overrides** | `modelCapabilities` forces or retracts per-model capability flags (`supports_vision`, `supports_function_calling`, …) when `/v1/model/info` is unavailable or disagrees with your deployment. |
|
|
@@ -332,7 +332,7 @@ If your LiteLLM proxy requires a master key, expose it via any of these approach
|
|
|
332
332
|
| Env var (alias) | `export LITELLM_MASTER_KEY=sk-...` |
|
|
333
333
|
| Config | `"settings": { "apiKey": "{env:LITELLM_API_KEY}" }` |
|
|
334
334
|
|
|
335
|
-
The env var path lets you commit `opencode.json` without leaking secrets.
|
|
335
|
+
The env var path lets you commit `opencode.json` without leaking secrets. `/connect` credentials are read from OpenCode's auth store (`~/.local/share/opencode/auth.json`) and applied to health checks, model discovery, and completion-time requests on BOTH OpenCode 1 and OpenCode 2. On OpenCode 2, a credential saved through `/connect` is a first-class integration connection: the plugin registers the **LiteLLM** integration with an API-key form, uses the stored key for discovery, and lets OpenCode inject it into completion requests — the key never appears in `opencode.json` or provider settings. A credential the user saves in the box (the active connection or the auth-store entry) wins over `LITELLM_API_KEY` / `LITELLM_MASTER_KEY`; the environment is a box-level fallback, never an override of a saved key.
|
|
336
336
|
|
|
337
337
|
#### Web UI connect (OpenCode 2)
|
|
338
338
|
|
|
@@ -342,7 +342,9 @@ With no configuration at all, `/connect` → **LiteLLM** is enough:
|
|
|
342
342
|
2. Optionally fill the form: **Base URL** (e.g. `https://llm.example.com/v1`) and **Provider ID** (defaults to `litellm`; pick a different id when your config already defines a `litellm` provider, so the connected gateway gets its own entry). Reconnecting with a different id moves the gateway to that provider.
|
|
343
343
|
3. Models appear in the picker without editing `opencode.json` or restarting.
|
|
344
344
|
|
|
345
|
-
Precedence when several sources are configured: explicit provider/plugin settings always win. For the key, the environment (`LITELLM_API_KEY`/`LITELLM_MASTER_KEY`) is
|
|
345
|
+
Precedence when several sources are configured: explicit provider/plugin settings always win. For the key, a credential saved in the box (the active connection or the `/connect` entry) beats the environment (`LITELLM_API_KEY`/`LITELLM_MASTER_KEY`), which is only a fallback; for the base URL, the dialog answer beats `LITELLM_BASE_URL` and local port auto-detection. A provider defined in `opencode.json` (without its own key) picks the saved credential up on the next start, while providers created from the dialog update immediately — reconnecting re-points them in place, including a changed base URL or provider id.
|
|
346
|
+
|
|
347
|
+
The discovered model list is cached per credential: a short hash of the effective key (never the raw key) is part of every cache entry's key, so when the selected credential changes the next discovery starts fresh instead of serving another credential's model list. A `credential.switched` event also recomputes the effective key and reloads the provider registry when it changed — including the first credential saved after setup.
|
|
346
348
|
|
|
347
349
|
### Slow proxies (`LITELLM_REQUEST_TIMEOUT_MS`)
|
|
348
350
|
|
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": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"description": "OpenCode CLI (v1 and v2) and Desktop plugin for LiteLLM proxy support with auto-detection and dynamic model discovery",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./src/index.ts",
|
|
@@ -13,8 +13,7 @@
|
|
|
13
13
|
],
|
|
14
14
|
"scripts": {
|
|
15
15
|
"typecheck": "tsc --noEmit",
|
|
16
|
-
"test": "vitest run"
|
|
17
|
-
"prepublishOnly": "npm run typecheck"
|
|
16
|
+
"test": "vitest run"
|
|
18
17
|
},
|
|
19
18
|
"keywords": [
|
|
20
19
|
"opencode",
|
package/src/plugin/index.ts
CHANGED
|
@@ -287,6 +287,17 @@ export function toConfigModel(
|
|
|
287
287
|
if (model.supports_vision) {
|
|
288
288
|
entry.attachment = true
|
|
289
289
|
}
|
|
290
|
+
// Declare `temperature` only when `/v1/model/info` lists it among the model's
|
|
291
|
+
// `supported_openai_params`. OpenCode sends a sampling temperature (its own
|
|
292
|
+
// per-model default, or one set on the agent) only for models flagged as
|
|
293
|
+
// supporting it, so a model that accepts the parameter would otherwise
|
|
294
|
+
// silently ignore the user's setting. When the parameter is not listed, or
|
|
295
|
+
// `/v1/model/info` is unavailable, the field stays unset and OpenCode keeps
|
|
296
|
+
// its default behavior (some deployments, such as reasoning-only endpoints or
|
|
297
|
+
// guardrail classifiers, reject the parameter outright).
|
|
298
|
+
if (info?.supported_openai_params?.includes('temperature')) {
|
|
299
|
+
entry.temperature = true
|
|
300
|
+
}
|
|
290
301
|
// Only emit `cost` when LiteLLM actually reported a price. Omitting
|
|
291
302
|
// it (rather than defaulting to 0) lets OpenCode/models.dev fall back
|
|
292
303
|
// to their own default instead of us asserting "this model is free"
|
|
@@ -761,9 +772,14 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
761
772
|
the plugin reads the stored key here and applies it to both its own
|
|
762
773
|
discovery fetches and the completion-time provider options. Without it,
|
|
763
774
|
a key-only proxy would fail discovery even though chat works.
|
|
775
|
+
|
|
776
|
+
Precedence rule: the stored credential — the user's explicit
|
|
777
|
+
choice made in the box — wins over the pod environment. The env is a
|
|
778
|
+
box-level fallback for users who have not saved a credential — never an
|
|
779
|
+
override of a user's saved key.
|
|
764
780
|
*/
|
|
765
781
|
const storedKey = await getOpenCodeStoredApiKey(providerId)
|
|
766
|
-
const apiKey = configuredKey ??
|
|
782
|
+
const apiKey = configuredKey ?? storedKey ?? envKey
|
|
767
783
|
// Native `options.headers` and the legacy `customHeaders` both reach
|
|
768
784
|
// discovery and the runtime, so a gateway that only sets one still
|
|
769
785
|
// authenticates the plugin's own fetches.
|
|
@@ -829,9 +845,12 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
829
845
|
// Identity includes the filter/capability/naming config: those
|
|
830
846
|
// are baked into cached entries, so changing them must start a
|
|
831
847
|
// fresh discovery instead of serving the old adjusted view.
|
|
848
|
+
// The cache key is isolated by the effective credential (a fingerprint,
|
|
849
|
+
// never the raw key): a credential change starts a fresh discovery
|
|
850
|
+
// instead of serving another credential's cached model list.
|
|
832
851
|
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities, {
|
|
833
852
|
formatModelNames,
|
|
834
|
-
})
|
|
853
|
+
}, apiKey)
|
|
835
854
|
|
|
836
855
|
// Remember how to reach this proxy so the `event` hook can
|
|
837
856
|
// revalidate its cache in the background on new sessions.
|
package/src/plugin/v2.ts
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
writeModelCache,
|
|
26
26
|
} from '../utils/model-cache'
|
|
27
27
|
import { normalizeBaseURL, autoDetectLiteLLM } from '../utils/litellm-api'
|
|
28
|
+
import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
|
|
28
29
|
import { parseModelCapabilities } from '../utils/model-capabilities'
|
|
29
30
|
import type { ModelCapabilities } from '../utils/model-capabilities'
|
|
30
31
|
import {
|
|
@@ -239,6 +240,8 @@ function toProviderModels(
|
|
|
239
240
|
tools: entry.tool_call === undefined ? defaults.capabilities.tools : entry.tool_call === true,
|
|
240
241
|
input,
|
|
241
242
|
output,
|
|
243
|
+
// Left unset (unknown) unless the model was declared to accept it.
|
|
244
|
+
...(entry.temperature === true ? { temperature: true } : {}),
|
|
242
245
|
},
|
|
243
246
|
cost,
|
|
244
247
|
variants,
|
|
@@ -341,20 +344,25 @@ async function makeProviderSource(
|
|
|
341
344
|
CHAT_PROVIDER_ID
|
|
342
345
|
const configuredBaseURL = readString(settings.baseURL)
|
|
343
346
|
const connectedApiKey = connected?.apiKey
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
//
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
347
|
+
// Precedence rule: an explicit settings/plugin-options apiKey is the
|
|
348
|
+
// operator's deliberate config and stays first; then a credential the user
|
|
349
|
+
// saved in the box — the ACTIVE connection or the `/connect` entry in the
|
|
350
|
+
// box auth store — which wins over the pod environment. The env is a
|
|
351
|
+
// box-level fallback for users who have not saved a credential, never an
|
|
352
|
+
// override of the user's explicit choice.
|
|
353
|
+
const settingsApiKey = readString(providerSettings.apiKey) ?? readString(pluginOptions.apiKey)
|
|
354
|
+
const envApiKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
355
|
+
// The key OpenCode itself stored for this provider in the box auth store
|
|
356
|
+
// (/connect journey). A config-defined provider carries no integrationID, so
|
|
357
|
+
// the host-integration path above cannot see it — read the store directly,
|
|
358
|
+
// exactly like the v1 entrypoint does. Still ahead of the env: a saved
|
|
359
|
+
// credential is the user's explicit choice and beats the box-level fallback.
|
|
360
|
+
const storedKey = await getOpenCodeStoredApiKey(id)
|
|
361
|
+
const usesConnectionCredential = settingsApiKey === undefined && connectedApiKey !== undefined
|
|
354
362
|
const integrationID =
|
|
355
363
|
provider?.integrationID ??
|
|
356
|
-
(usesConnectionCredential
|
|
357
|
-
const apiKey =
|
|
364
|
+
(usesConnectionCredential ? LITELLM_INTEGRATION_ID : undefined)
|
|
365
|
+
const apiKey = settingsApiKey ?? connectedApiKey ?? storedKey ?? envApiKey
|
|
358
366
|
const filters = readModelFilters(settings)
|
|
359
367
|
const capabilities = parseModelCapabilities(settings.modelCapabilities)
|
|
360
368
|
const compatibility = parseModelCompatibility(settings.modelCompatibility)
|
|
@@ -372,9 +380,12 @@ async function makeProviderSource(
|
|
|
372
380
|
}
|
|
373
381
|
|
|
374
382
|
const baseURL = normalizeBaseURL(detectedBaseURL)
|
|
383
|
+
// The cache key is isolated by the effective credential (a fingerprint, never
|
|
384
|
+
// the raw key): a credential change starts a fresh discovery instead of
|
|
385
|
+
// serving another credential's cached model list.
|
|
375
386
|
const cacheKey = buildCacheKey(id, baseURL, filters, capabilities, {
|
|
376
387
|
formatModelNames,
|
|
377
|
-
})
|
|
388
|
+
}, apiKey)
|
|
378
389
|
const cacheEntry = readModelCacheEntry(cacheKey)
|
|
379
390
|
let entries = cacheEntry?.models ?? null
|
|
380
391
|
let responsesIds = cacheEntry?.responsesIds ?? []
|
|
@@ -531,16 +542,40 @@ async function refreshConnectionCredential(
|
|
|
531
542
|
context: Context,
|
|
532
543
|
source: ProviderSource,
|
|
533
544
|
): Promise<boolean> {
|
|
534
|
-
|
|
545
|
+
// The gate is the integration's presence, not the current credential choice:
|
|
546
|
+
// setup may run before the user saves a credential, and the first save must
|
|
547
|
+
// refresh this source instead of leaving it on the environment key.
|
|
548
|
+
if (!source.integrationID) return false
|
|
535
549
|
|
|
536
550
|
try {
|
|
537
551
|
const connection = await context.integration.connection.active(source.integrationID)
|
|
538
552
|
const credential = connection
|
|
539
553
|
? await context.integration.connection.resolve(connection)
|
|
540
554
|
: undefined
|
|
541
|
-
const
|
|
542
|
-
|
|
543
|
-
|
|
555
|
+
const connectionKey = credential?.type === 'key' ? credential.key : undefined
|
|
556
|
+
const settingsApiKey = readString(source.settings.apiKey)
|
|
557
|
+
let nextKey: string | undefined
|
|
558
|
+
let nextUsesConnection: boolean
|
|
559
|
+
if (connectionKey !== undefined) {
|
|
560
|
+
// An explicit settings apiKey stays first; a connection credential takes
|
|
561
|
+
// over the environment/auto-detected key.
|
|
562
|
+
nextKey = settingsApiKey ?? connectionKey
|
|
563
|
+
nextUsesConnection = settingsApiKey === undefined
|
|
564
|
+
} else {
|
|
565
|
+
// The connection credential is gone (the connection was removed or its
|
|
566
|
+
// credential cleared): fall back through the stored credential / env key
|
|
567
|
+
// — the same precedence `makeProviderSource` uses — instead of retaining
|
|
568
|
+
// the previous connection key, which `runtimeSettings` would otherwise
|
|
569
|
+
// materialize into provider settings.
|
|
570
|
+
const storedKey = await getOpenCodeStoredApiKey(source.id)
|
|
571
|
+
const envKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
572
|
+
nextKey = settingsApiKey ?? storedKey ?? envKey
|
|
573
|
+
nextUsesConnection = false
|
|
574
|
+
}
|
|
575
|
+
if (nextKey === source.apiKey && nextUsesConnection === source.usesConnectionCredential) return false
|
|
576
|
+
source.apiKey = nextKey
|
|
577
|
+
source.usesConnectionCredential = nextUsesConnection
|
|
578
|
+
source.credentialReloadRequired = true
|
|
544
579
|
return true
|
|
545
580
|
} catch (error) {
|
|
546
581
|
const message = error instanceof Error ? error.message : String(error)
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -29,7 +29,7 @@ export interface ModelHints {
|
|
|
29
29
|
* from older plugin versions are ignored rather than deserialized into
|
|
30
30
|
* an incompatible structure.
|
|
31
31
|
*/
|
|
32
|
-
const CACHE_VERSION =
|
|
32
|
+
const CACHE_VERSION = 6
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Maximum age of a cache entry before it's treated as a miss. Without a
|
|
@@ -111,8 +111,16 @@ export function buildCacheKey(
|
|
|
111
111
|
filters: { includeModels?: string[]; excludeModels?: string[] },
|
|
112
112
|
capabilities: Record<string, Record<string, boolean>>,
|
|
113
113
|
naming: { formatModelNames?: boolean } = {},
|
|
114
|
+
credential?: string,
|
|
114
115
|
): string {
|
|
115
|
-
|
|
116
|
+
let base = `${providerId}@${baseURL}`
|
|
117
|
+
// The credential fingerprint: a short hash (never the raw key) so a
|
|
118
|
+
// credential change isolates the cached model list — a key that exposes a
|
|
119
|
+
// different set of models must start a fresh discovery instead of serving
|
|
120
|
+
// another credential's entries.
|
|
121
|
+
if (credential) {
|
|
122
|
+
base += `@${createHash('sha256').update(credential).digest('hex').slice(0, 12)}`
|
|
123
|
+
}
|
|
116
124
|
const rawNames = naming.formatModelNames === false
|
|
117
125
|
const hasAdjustments =
|
|
118
126
|
(filters.includeModels?.length ?? 0) > 0 ||
|