opencode-plugin-litellm 1.5.0 → 1.6.1
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 +54 -0
- package/package.json +1 -1
- package/src/plugin/index.ts +16 -5
- package/src/plugin/v2.ts +90 -12
- package/src/types/index.ts +2 -0
- package/src/utils/model-cache.ts +58 -6
- package/src/utils/model-compatibility.ts +65 -0
package/README.md
CHANGED
|
@@ -82,6 +82,7 @@ also shows how to configure a provider directly in `opencode.json`.
|
|
|
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. |
|
|
85
|
+
| 💾 **Prompt caching (OpenCode 2.0.28+)** | Chat models whose deployment accepts Anthropic `cache_control` markers are registered with `compatibility.cacheFormat: "anthropic"`, so OpenCode 2 places cache breakpoints as it does for a native Anthropic provider. `modelCompatibility` corrects this per model. |
|
|
85
86
|
| ⏱️ **Bounded startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** (configurable via `LITELLM_REQUEST_TIMEOUT_MS`) for slow remote proxies. V2 model refresh runs in the background. |
|
|
86
87
|
| 📝 **OpenCode logging** | V1 logs go through OpenCode's log API; V2 logs use the V2 plugin host's console logging. |
|
|
87
88
|
| 🤝 **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
@@ -244,6 +245,31 @@ between effort levels from the model picker without hand-curating every entry.
|
|
|
244
245
|
> `mode: "responses"` the plugin routes it to `/v1/responses` automatically;
|
|
245
246
|
> otherwise reach it without `reasoningEffort`. See the FAQ entry below.
|
|
246
247
|
|
|
248
|
+
On OpenCode 2, models whose effective package is `@opencode/ai/providers/anthropic`
|
|
249
|
+
or `@opencode/ai/providers/anthropic-compatible` get the same levels in the
|
|
250
|
+
shape that runtime reads, `{ thinking: { type: "adaptive", display: "summarized" }, effort }`,
|
|
251
|
+
for models LiteLLM reports with `supports_adaptive_thinking`, with `none`
|
|
252
|
+
becoming `thinking: { type: "disabled" }` (dropped when `thinking_always_on`).
|
|
253
|
+
Models without adaptive thinking get no variants on those packages, since
|
|
254
|
+
`reasoningEffort` would be ignored there. To reach Claude over the proxy's
|
|
255
|
+
`/v1/messages` while the rest of the catalog keeps its usual transport, split it
|
|
256
|
+
with [`includeModels`/`excludeModels`](#splitting-one-proxy-into-multiple-providers-includemodels--excludemodels):
|
|
257
|
+
|
|
258
|
+
```jsonc
|
|
259
|
+
{
|
|
260
|
+
"providers": {
|
|
261
|
+
"litellm": {
|
|
262
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
263
|
+
"settings": { "baseURL": "http://localhost:4000/v1", "excludeModels": ["*claude*"] }
|
|
264
|
+
},
|
|
265
|
+
"litellm-claude": {
|
|
266
|
+
"package": "@opencode/ai/providers/anthropic-compatible",
|
|
267
|
+
"settings": { "baseURL": "http://localhost:4000/v1", "includeModels": ["*claude*"] }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
247
273
|
### Responses API models
|
|
248
274
|
|
|
249
275
|
LiteLLM reports `mode` per model on `/v1/model/info` (the plugin fetches it
|
|
@@ -423,6 +449,33 @@ By default the plugin prettifies each discovered id into a display name — `ant
|
|
|
423
449
|
- Like the other options above, `formatModelNames` is part of the cache identity, so toggling it triggers a fresh discovery on the next start instead of serving previously cached names.
|
|
424
450
|
- To rename just a handful of models while keeping smart formatting for the rest, use the per-model `name` override described in [Overriding or curating individual models](#overriding-or-curating-individual-models-optional).
|
|
425
451
|
|
|
452
|
+
### Prompt caching (`modelCompatibility`)
|
|
453
|
+
|
|
454
|
+
On OpenCode 2.0.28+ (earlier 2.x releases ignore the setting), the OpenAI-compatible Chat Completions runtime only places Anthropic-style `cache_control` breakpoints when a model declares `compatibility.cacheFormat: "anthropic"`. The plugin sets that for every discovered Chat Completions model whose `/v1/model/info` entry lists `cache_control` under `supported_openai_params` — which LiteLLM reports for Anthropic and Vertex AI Anthropic deployments — so prompt caching keeps working for Claude behind the proxy. Only the Chat Completions runtimes read this setting: the Anthropic Messages runtime adds its own cache breakpoints, and the Responses runtimes do not use it, so models on those packages are left alone.
|
|
455
|
+
|
|
456
|
+
To correct the inference per model id, set `cacheFormat` to `"anthropic"`, `"alibaba"`, or `null` to remove it:
|
|
457
|
+
|
|
458
|
+
```jsonc
|
|
459
|
+
{
|
|
460
|
+
"providers": {
|
|
461
|
+
"litellm": {
|
|
462
|
+
"settings": {
|
|
463
|
+
"baseURL": "http://localhost:4000/v1",
|
|
464
|
+
"modelCompatibility": {
|
|
465
|
+
"bedrock/claude-sonnet-4": { "cacheFormat": "anthropic" },
|
|
466
|
+
"gateway/claude-opus": { "cacheFormat": null }
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
- Keys are exact model ids as they appear in `/v1/models` (not globs). Only `cacheFormat`, the one key the plugin sets, is accepted.
|
|
475
|
+
- OpenCode's own `providers.<id>.models.<model>.compatibility` is merged over the plugin's value, so it can add or change a key but not remove one the plugin inferred; that is what `null` here is for.
|
|
476
|
+
- Overrides are applied when models are registered, not during discovery, so changing them does not start a new discovery.
|
|
477
|
+
- Neither applies on OpenCode 1.
|
|
478
|
+
|
|
426
479
|
## 🔧 How it works
|
|
427
480
|
|
|
428
481
|
```mermaid
|
|
@@ -601,6 +654,7 @@ src/
|
|
|
601
654
|
│ ├── model-cache.ts # stale-while-revalidate on-disk model cache
|
|
602
655
|
│ ├── model-filter.ts # includeModels/excludeModels glob filtering
|
|
603
656
|
│ ├── model-capabilities.ts # per-model capability flag overrides
|
|
657
|
+
│ ├── model-compatibility.ts # cacheFormat inference and per-model overrides
|
|
604
658
|
│ └── opencode-auth.ts # fallback to OpenCode's /connect-stored credentials
|
|
605
659
|
└── plugin/
|
|
606
660
|
├── index.ts # OpenCode 1 server entry and shared discovery logic
|
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.6.1",
|
|
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",
|
package/src/plugin/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
import { requiresResponsesAPI } from '../utils/transport'
|
|
15
15
|
import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
|
|
16
16
|
import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
|
|
17
|
+
import type { ModelHints } from '../utils/model-cache'
|
|
17
18
|
import {
|
|
18
19
|
buildCacheKey,
|
|
19
20
|
readModelCacheEntry,
|
|
@@ -331,11 +332,14 @@ export function toConfigModel(
|
|
|
331
332
|
* `models` is the `{ id -> entry }` map written into an OpenCode provider
|
|
332
333
|
* config (or translated into `Model.Info` on OpenCode 2). `responsesIds`
|
|
333
334
|
* lists the subset that must be invoked through the OpenAI Responses API;
|
|
334
|
-
* callers route those accordingly without inspecting each entry.
|
|
335
|
+
* callers route those accordingly without inspecting each entry. `hints`
|
|
336
|
+
* carries per-model upstream facts OpenCode 2 maps to `compatibility` and
|
|
337
|
+
* `variants`.
|
|
335
338
|
*/
|
|
336
339
|
export interface DiscoveredModels {
|
|
337
340
|
models: Record<string, unknown>
|
|
338
341
|
responsesIds: string[]
|
|
342
|
+
hints: Record<string, ModelHints>
|
|
339
343
|
}
|
|
340
344
|
|
|
341
345
|
/**
|
|
@@ -343,7 +347,7 @@ export interface DiscoveredModels {
|
|
|
343
347
|
*
|
|
344
348
|
* Pure with respect to plugin config: it performs the network calls,
|
|
345
349
|
* classifies + formats each model, and returns the built entries plus the
|
|
346
|
-
* ids that require the Responses API. The provider's
|
|
350
|
+
* ids that require the Responses API and per-model hints. The provider's
|
|
347
351
|
* `includeModels`/`excludeModels` filters, `modelCapabilities` overrides and
|
|
348
352
|
* `formatModelNames` choice are applied here (not at merge time) so every
|
|
349
353
|
* path that persists or serves a cache — cold discovery and background
|
|
@@ -411,6 +415,7 @@ export async function discoverModels(
|
|
|
411
415
|
|
|
412
416
|
const built: Record<string, unknown> = {}
|
|
413
417
|
const responsesIds: string[] = []
|
|
418
|
+
const hints: Record<string, ModelHints> = {}
|
|
414
419
|
let skipped = 0
|
|
415
420
|
let wildcards = 0
|
|
416
421
|
let filtered = 0
|
|
@@ -440,6 +445,12 @@ export async function discoverModels(
|
|
|
440
445
|
}
|
|
441
446
|
built[model.id] = entry
|
|
442
447
|
if (requiresResponsesAPI(enriched)) responsesIds.push(model.id)
|
|
448
|
+
const params = info?.supported_openai_params
|
|
449
|
+
hints[model.id] = {
|
|
450
|
+
acceptsCacheControl: Array.isArray(params) && params.includes('cache_control'),
|
|
451
|
+
adaptiveThinking: info?.supports_adaptive_thinking === true,
|
|
452
|
+
thinkingAlwaysOn: info?.thinking_always_on === true,
|
|
453
|
+
}
|
|
443
454
|
}
|
|
444
455
|
|
|
445
456
|
if (unmatched.length > 0) {
|
|
@@ -471,7 +482,7 @@ export async function discoverModels(
|
|
|
471
482
|
')',
|
|
472
483
|
)
|
|
473
484
|
|
|
474
|
-
return { models: built, responsesIds }
|
|
485
|
+
return { models: built, responsesIds, hints }
|
|
475
486
|
}
|
|
476
487
|
|
|
477
488
|
/**
|
|
@@ -655,7 +666,7 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
655
666
|
DISCOVERY_TIMEOUT_MS,
|
|
656
667
|
)
|
|
657
668
|
if (discovered && Object.keys(discovered.models).length > 0) {
|
|
658
|
-
writeModelCache(cacheKey, discovered.models, discovered.responsesIds)
|
|
669
|
+
writeModelCache(cacheKey, discovered.models, discovered.responsesIds, discovered.hints)
|
|
659
670
|
log(
|
|
660
671
|
'info',
|
|
661
672
|
`[opencode-litellm] Background-refreshed model cache for ${ctx.baseURL} (${Object.keys(discovered.models).length} models)`,
|
|
@@ -913,7 +924,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
913
924
|
customHeaders,
|
|
914
925
|
)
|
|
915
926
|
injectedModelIds.set(cacheKey, new Set(added))
|
|
916
|
-
writeModelCache(cacheKey, discovered.models, discovered.responsesIds)
|
|
927
|
+
writeModelCache(cacheKey, discovered.models, discovered.responsesIds, discovered.hints)
|
|
917
928
|
}
|
|
918
929
|
}
|
|
919
930
|
},
|
package/src/plugin/v2.ts
CHANGED
|
@@ -21,10 +21,29 @@ import {
|
|
|
21
21
|
import { normalizeBaseURL, autoDetectLiteLLM } from '../utils/litellm-api'
|
|
22
22
|
import { parseModelCapabilities } from '../utils/model-capabilities'
|
|
23
23
|
import type { ModelCapabilities } from '../utils/model-capabilities'
|
|
24
|
+
import {
|
|
25
|
+
inferCompatibility,
|
|
26
|
+
parseModelCompatibility,
|
|
27
|
+
resolveCompatibility,
|
|
28
|
+
} from '../utils/model-compatibility'
|
|
29
|
+
import type { ModelCompatibility } from '../utils/model-compatibility'
|
|
24
30
|
import type { ModelFilters } from '../utils/model-filter'
|
|
31
|
+
import type { ModelHints } from '../utils/model-cache'
|
|
25
32
|
|
|
26
33
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
27
34
|
const OPENAI_COMPATIBLE_PACKAGE = '@opencode/ai/providers/openai-compatible'
|
|
35
|
+
// Of the packages a LiteLLM provider uses, only the Chat Completions ones read
|
|
36
|
+
// `compatibility.cacheFormat`; `openai` (Responses by default), the Responses
|
|
37
|
+
// packages and the Anthropic packages do not.
|
|
38
|
+
const CHAT_COMPLETIONS_PACKAGES = new Set([
|
|
39
|
+
OPENAI_COMPATIBLE_PACKAGE,
|
|
40
|
+
'@opencode/ai/providers/openai/chat',
|
|
41
|
+
])
|
|
42
|
+
const ANTHROPIC_PACKAGES = new Set([
|
|
43
|
+
'@opencode/ai/providers/anthropic',
|
|
44
|
+
'@opencode/ai/providers/anthropic-compatible',
|
|
45
|
+
])
|
|
46
|
+
const ADAPTIVE_THINKING = { type: 'adaptive', display: 'summarized' }
|
|
28
47
|
// Runtime package that talks to `/v1/responses`. OpenCode 2 lets one model
|
|
29
48
|
// override the provider's package, so Responses models stay under their
|
|
30
49
|
// original provider id and no sibling provider is needed here.
|
|
@@ -78,6 +97,7 @@ interface ProviderSource {
|
|
|
78
97
|
pendingPublication: boolean
|
|
79
98
|
filters: ModelFilters
|
|
80
99
|
capabilities: ModelCapabilities
|
|
100
|
+
compatibility: ModelCompatibility
|
|
81
101
|
formatModelNames: boolean
|
|
82
102
|
cacheKey: string
|
|
83
103
|
models: Model.Info[]
|
|
@@ -111,12 +131,44 @@ function numberOrZero(value: unknown): number {
|
|
|
111
131
|
return typeof value === 'number' && Number.isFinite(value) ? value : 0
|
|
112
132
|
}
|
|
113
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Discovery spells every effort level as `{ reasoningEffort }`, which the
|
|
136
|
+
* OpenAI runtimes read. The Anthropic runtimes read `thinking` and `effort`
|
|
137
|
+
* instead, so on those packages the levels are respelled the way OpenCode's
|
|
138
|
+
* own catalog does for adaptive-thinking Claude models. A model that does
|
|
139
|
+
* not report adaptive thinking gets no variants rather than inert ones.
|
|
140
|
+
*/
|
|
141
|
+
function anthropicVariants(
|
|
142
|
+
variants: Model.Info['variants'],
|
|
143
|
+
hints: ModelHints | undefined,
|
|
144
|
+
): Model.Info['variants'] {
|
|
145
|
+
if (!hints?.adaptiveThinking) return []
|
|
146
|
+
return variants.flatMap((variant) => {
|
|
147
|
+
const effort = variant.settings?.reasoningEffort
|
|
148
|
+
if (typeof effort !== 'string') return []
|
|
149
|
+
if (effort === 'none') {
|
|
150
|
+
if (hints.thinkingAlwaysOn) return []
|
|
151
|
+
return [{ ...variant, settings: { thinking: { type: 'disabled' } } }]
|
|
152
|
+
}
|
|
153
|
+
return [{ ...variant, settings: { thinking: ADAPTIVE_THINKING, effort } }]
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface ToProviderModelsOptions {
|
|
158
|
+
/** The provider's runtime package; models not overridden inherit it. */
|
|
159
|
+
providerPackage: string
|
|
160
|
+
responsesIds: ReadonlySet<string>
|
|
161
|
+
hints: Record<string, ModelHints>
|
|
162
|
+
compatibility: ModelCompatibility
|
|
163
|
+
}
|
|
164
|
+
|
|
114
165
|
function toProviderModels(
|
|
115
166
|
providerID: string,
|
|
116
167
|
entries: Record<string, unknown>,
|
|
117
|
-
|
|
168
|
+
options: ToProviderModelsOptions,
|
|
118
169
|
): Model.Info[] {
|
|
119
170
|
const provider = Provider.ID.make(providerID)
|
|
171
|
+
const { responsesIds, hints, compatibility: overrides } = options
|
|
120
172
|
|
|
121
173
|
return Object.entries(entries).flatMap(([modelID, value]) => {
|
|
122
174
|
const entry = asRecord(value)
|
|
@@ -148,10 +200,18 @@ function toProviderModels(
|
|
|
148
200
|
: []
|
|
149
201
|
|
|
150
202
|
const rawVariants = asRecord(entry.variants)
|
|
151
|
-
const
|
|
203
|
+
const openAIVariants = Object.entries(rawVariants).map(([id, settings]) => ({
|
|
152
204
|
id: id as Model.VariantID,
|
|
153
205
|
settings: asRecord(settings),
|
|
154
206
|
})) as Model.Info['variants']
|
|
207
|
+
const pkg = responsesIds.has(modelID) ? RESPONSES_PACKAGE : options.providerPackage
|
|
208
|
+
const variants = ANTHROPIC_PACKAGES.has(pkg)
|
|
209
|
+
? anthropicVariants(openAIVariants, hints[modelID])
|
|
210
|
+
: openAIVariants
|
|
211
|
+
const compatibility = resolveCompatibility(
|
|
212
|
+
inferCompatibility(hints[modelID], CHAT_COMPLETIONS_PACKAGES.has(pkg)),
|
|
213
|
+
Object.hasOwn(overrides, modelID) ? overrides[modelID] : undefined,
|
|
214
|
+
)
|
|
155
215
|
return [
|
|
156
216
|
{
|
|
157
217
|
...defaults,
|
|
@@ -159,6 +219,11 @@ function toProviderModels(
|
|
|
159
219
|
// runtime package for itself, so it keeps the provider id while
|
|
160
220
|
// still reaching the Responses API.
|
|
161
221
|
...(responsesIds.has(modelID) ? { package: RESPONSES_PACKAGE } : {}),
|
|
222
|
+
// `cacheFormat` exists on the host's `Model.Compatibility` (2.0.28+)
|
|
223
|
+
// but not in the 2.0.15 schema this package compiles against.
|
|
224
|
+
...(compatibility
|
|
225
|
+
? { compatibility: compatibility as unknown as Model.Info['compatibility'] }
|
|
226
|
+
: {}),
|
|
162
227
|
name: readString(entry.name) ?? modelID,
|
|
163
228
|
limit: {
|
|
164
229
|
context: numberOrDefault(limit.context, defaults.limit.context),
|
|
@@ -228,6 +293,7 @@ function runtimeSettings(source: ProviderSource): Record<string, unknown> {
|
|
|
228
293
|
'includeModels',
|
|
229
294
|
'excludeModels',
|
|
230
295
|
'modelCapabilities',
|
|
296
|
+
'modelCompatibility',
|
|
231
297
|
'formatModelNames',
|
|
232
298
|
'providerID',
|
|
233
299
|
]) {
|
|
@@ -277,6 +343,7 @@ async function makeProviderSource(
|
|
|
277
343
|
const apiKey = configuredApiKey ?? connectedApiKey
|
|
278
344
|
const filters = readModelFilters(settings)
|
|
279
345
|
const capabilities = parseModelCapabilities(settings.modelCapabilities)
|
|
346
|
+
const compatibility = parseModelCompatibility(settings.modelCompatibility)
|
|
280
347
|
const formatModelNames = readFormatModelNames(settings)
|
|
281
348
|
const detectedBaseURL = configuredBaseURL
|
|
282
349
|
? normalizeBaseURL(configuredBaseURL)
|
|
@@ -293,6 +360,7 @@ async function makeProviderSource(
|
|
|
293
360
|
const cacheEntry = readModelCacheEntry(cacheKey)
|
|
294
361
|
let entries = cacheEntry?.models ?? null
|
|
295
362
|
let responsesIds = cacheEntry?.responsesIds ?? []
|
|
363
|
+
let hints = cacheEntry?.hints ?? {}
|
|
296
364
|
|
|
297
365
|
if (!entries || Object.keys(entries).length === 0) {
|
|
298
366
|
const discovered = await withTimeout(
|
|
@@ -310,10 +378,12 @@ async function makeProviderSource(
|
|
|
310
378
|
if (discovered && Object.keys(discovered.models).length > 0) {
|
|
311
379
|
entries = discovered.models
|
|
312
380
|
responsesIds = discovered.responsesIds
|
|
313
|
-
|
|
381
|
+
hints = discovered.hints
|
|
382
|
+
writeModelCache(cacheKey, entries, responsesIds, hints)
|
|
314
383
|
} else {
|
|
315
384
|
entries = null
|
|
316
385
|
responsesIds = []
|
|
386
|
+
hints = {}
|
|
317
387
|
}
|
|
318
388
|
} else {
|
|
319
389
|
logInfo(
|
|
@@ -321,11 +391,12 @@ async function makeProviderSource(
|
|
|
321
391
|
)
|
|
322
392
|
}
|
|
323
393
|
|
|
394
|
+
const providerPackage = provider?.package || OPENAI_COMPATIBLE_PACKAGE
|
|
324
395
|
return {
|
|
325
396
|
id,
|
|
326
397
|
name: provider?.name || 'LiteLLM (proxy)',
|
|
327
398
|
activation: provider?.activation ?? 'enabled',
|
|
328
|
-
package:
|
|
399
|
+
package: providerPackage,
|
|
329
400
|
settings,
|
|
330
401
|
headers: customHeaders,
|
|
331
402
|
integrationID: provider?.integrationID,
|
|
@@ -339,9 +410,15 @@ async function makeProviderSource(
|
|
|
339
410
|
customHeaders: Object.keys(customHeaders).length > 0 ? customHeaders : undefined,
|
|
340
411
|
filters,
|
|
341
412
|
capabilities,
|
|
413
|
+
compatibility,
|
|
342
414
|
formatModelNames,
|
|
343
415
|
cacheKey,
|
|
344
|
-
models: toProviderModels(id, entries ?? {},
|
|
416
|
+
models: toProviderModels(id, entries ?? {}, {
|
|
417
|
+
providerPackage,
|
|
418
|
+
responsesIds: new Set(responsesIds),
|
|
419
|
+
hints,
|
|
420
|
+
compatibility,
|
|
421
|
+
}),
|
|
345
422
|
ownedModelIds: new Set<string>(),
|
|
346
423
|
}
|
|
347
424
|
}
|
|
@@ -395,13 +472,14 @@ async function refreshProviderSource(
|
|
|
395
472
|
return
|
|
396
473
|
}
|
|
397
474
|
|
|
398
|
-
const models = toProviderModels(
|
|
399
|
-
source.
|
|
400
|
-
discovered.
|
|
401
|
-
|
|
402
|
-
|
|
475
|
+
const models = toProviderModels(source.id, discovered.models, {
|
|
476
|
+
providerPackage: source.package,
|
|
477
|
+
responsesIds: new Set(discovered.responsesIds),
|
|
478
|
+
hints: discovered.hints,
|
|
479
|
+
compatibility: source.compatibility,
|
|
480
|
+
})
|
|
403
481
|
if (JSON.stringify(models) === JSON.stringify(source.models)) {
|
|
404
|
-
writeModelCache(source.cacheKey, discovered.models, discovered.responsesIds)
|
|
482
|
+
writeModelCache(source.cacheKey, discovered.models, discovered.responsesIds, discovered.hints)
|
|
405
483
|
source.refreshRequired = false
|
|
406
484
|
return
|
|
407
485
|
}
|
|
@@ -415,7 +493,7 @@ async function refreshProviderSource(
|
|
|
415
493
|
throw error
|
|
416
494
|
}
|
|
417
495
|
|
|
418
|
-
writeModelCache(source.cacheKey, discovered.models, discovered.responsesIds)
|
|
496
|
+
writeModelCache(source.cacheKey, discovered.models, discovered.responsesIds, discovered.hints)
|
|
419
497
|
source.refreshRequired = false
|
|
420
498
|
logInfo(
|
|
421
499
|
`[opencode-litellm] Refreshed ${models.length} models for provider "${source.id}"; the provider registry was reloaded.`,
|
package/src/types/index.ts
CHANGED
|
@@ -71,6 +71,8 @@ export interface LiteLLMModelInfo {
|
|
|
71
71
|
supported_openai_params?: string[] | null
|
|
72
72
|
supports_pdf_input?: boolean
|
|
73
73
|
supports_audio_input?: boolean
|
|
74
|
+
supports_adaptive_thinking?: boolean
|
|
75
|
+
thinking_always_on?: boolean
|
|
74
76
|
input_cost_per_token?: number
|
|
75
77
|
output_cost_per_token?: number
|
|
76
78
|
cache_read_input_token_cost?: number
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -3,12 +3,33 @@ import { readFileSync, mkdirSync, writeFileSync, renameSync, rmSync } from 'node
|
|
|
3
3
|
import { homedir, tmpdir } from 'node:os'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Facts about a discovered model's upstream that OpenCode 2 turns into
|
|
8
|
+
* `Model.Info.compatibility` and `variants`. They ride alongside the
|
|
9
|
+
* config-shaped model entries (which have no field for them) through
|
|
10
|
+
* discovery and the cache.
|
|
11
|
+
*/
|
|
12
|
+
export interface ModelHints {
|
|
13
|
+
/**
|
|
14
|
+
* The deployment accepts Anthropic-style `cache_control` markers on
|
|
15
|
+
* Chat Completions (`supported_openai_params` lists `cache_control`).
|
|
16
|
+
*/
|
|
17
|
+
acceptsCacheControl: boolean
|
|
18
|
+
/**
|
|
19
|
+
* The model takes `thinking: { type: "adaptive" }` plus an `effort` level
|
|
20
|
+
* (`supports_adaptive_thinking`).
|
|
21
|
+
*/
|
|
22
|
+
adaptiveThinking: boolean
|
|
23
|
+
/** Thinking cannot be turned off (`thinking_always_on`). */
|
|
24
|
+
thinkingAlwaysOn: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
6
27
|
/**
|
|
7
28
|
* Bump when the shape of a cached model entry changes so stale caches
|
|
8
29
|
* from older plugin versions are ignored rather than deserialized into
|
|
9
30
|
* an incompatible structure.
|
|
10
31
|
*/
|
|
11
|
-
const CACHE_VERSION =
|
|
32
|
+
const CACHE_VERSION = 5
|
|
12
33
|
|
|
13
34
|
/**
|
|
14
35
|
* Maximum age of a cache entry before it's treated as a miss. Without a
|
|
@@ -29,6 +50,11 @@ interface ModelCacheFile {
|
|
|
29
50
|
* no transport marker (their shape is the user-facing OpenCode config).
|
|
30
51
|
*/
|
|
31
52
|
responsesIds?: string[]
|
|
53
|
+
/**
|
|
54
|
+
* Per-model facts about the upstream that OpenCode 2 needs but that have
|
|
55
|
+
* no place in a provider-config entry (see `ModelHints`).
|
|
56
|
+
*/
|
|
57
|
+
hints?: Record<string, ModelHints>
|
|
32
58
|
}
|
|
33
59
|
|
|
34
60
|
/**
|
|
@@ -134,6 +160,22 @@ function normalizeResponsesIds(value: unknown): string[] {
|
|
|
134
160
|
return value.filter((id): id is string => typeof id === 'string')
|
|
135
161
|
}
|
|
136
162
|
|
|
163
|
+
/** Keep only well-formed entries of a cached `hints` map. */
|
|
164
|
+
function normalizeHints(value: unknown): Record<string, ModelHints> {
|
|
165
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
166
|
+
const out: Record<string, ModelHints> = {}
|
|
167
|
+
for (const [id, hint] of Object.entries(value as Record<string, unknown>)) {
|
|
168
|
+
if (!hint || typeof hint !== 'object' || Array.isArray(hint)) continue
|
|
169
|
+
const { acceptsCacheControl, adaptiveThinking, thinkingAlwaysOn } = hint as Record<string, unknown>
|
|
170
|
+
out[id] = {
|
|
171
|
+
acceptsCacheControl: acceptsCacheControl === true,
|
|
172
|
+
adaptiveThinking: adaptiveThinking === true,
|
|
173
|
+
thinkingAlwaysOn: thinkingAlwaysOn === true,
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return out
|
|
177
|
+
}
|
|
178
|
+
|
|
137
179
|
/**
|
|
138
180
|
* Read cached model entries for a cache key. Returns `null` on any
|
|
139
181
|
* problem (missing file, parse error, version mismatch, or an entry
|
|
@@ -155,14 +197,18 @@ export function readModelCache(
|
|
|
155
197
|
}
|
|
156
198
|
|
|
157
199
|
/**
|
|
158
|
-
* Read the models
|
|
159
|
-
* parse. Returns `null` under the same conditions as
|
|
160
|
-
* {@link readModelCache} (missing/expired cache), so the
|
|
200
|
+
* Read the models, Responses-API model ids and model hints for a cache key
|
|
201
|
+
* in one parse. Returns `null` under the same conditions as
|
|
202
|
+
* {@link readModelCache} (missing/expired cache), so the three can't
|
|
161
203
|
* disagree within a single lookup.
|
|
162
204
|
*/
|
|
163
205
|
export function readModelCacheEntry(
|
|
164
206
|
cacheKey: string,
|
|
165
|
-
): {
|
|
207
|
+
): {
|
|
208
|
+
models: Record<string, unknown>
|
|
209
|
+
responsesIds: string[]
|
|
210
|
+
hints: Record<string, ModelHints>
|
|
211
|
+
} | null {
|
|
166
212
|
const parsed = readCacheFile(cacheKey)
|
|
167
213
|
if (!parsed) return null
|
|
168
214
|
if (
|
|
@@ -171,7 +217,11 @@ export function readModelCacheEntry(
|
|
|
171
217
|
) {
|
|
172
218
|
return null
|
|
173
219
|
}
|
|
174
|
-
return {
|
|
220
|
+
return {
|
|
221
|
+
models: parsed.models,
|
|
222
|
+
responsesIds: normalizeResponsesIds(parsed.responsesIds),
|
|
223
|
+
hints: normalizeHints(parsed.hints),
|
|
224
|
+
}
|
|
175
225
|
}
|
|
176
226
|
|
|
177
227
|
/**
|
|
@@ -204,6 +254,7 @@ export function writeModelCache(
|
|
|
204
254
|
cacheKey: string,
|
|
205
255
|
models: Record<string, unknown>,
|
|
206
256
|
responsesIds: readonly string[] = [],
|
|
257
|
+
hints: Record<string, ModelHints> = {},
|
|
207
258
|
): void {
|
|
208
259
|
try {
|
|
209
260
|
mkdirSync(cacheDir(), { recursive: true })
|
|
@@ -212,6 +263,7 @@ export function writeModelCache(
|
|
|
212
263
|
savedAt: Date.now(),
|
|
213
264
|
models,
|
|
214
265
|
responsesIds: [...responsesIds],
|
|
266
|
+
hints,
|
|
215
267
|
}
|
|
216
268
|
// Write to a temp file and rename so concurrent OpenCode processes
|
|
217
269
|
// never observe a partially-written JSON file (rename is atomic on
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { ModelHints } from './model-cache'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Values `cacheFormat` accepts. Validated here because OpenCode validates
|
|
5
|
+
* plugin-registered models only when it serializes them, where one bad
|
|
6
|
+
* value fails the whole model list.
|
|
7
|
+
*/
|
|
8
|
+
const CACHE_FORMATS: ReadonlySet<string> = new Set(['anthropic', 'alibaba'])
|
|
9
|
+
|
|
10
|
+
/** `null` removes the `cacheFormat` the plugin inferred. */
|
|
11
|
+
export interface CompatibilityOverride {
|
|
12
|
+
cacheFormat: string | null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Per-model compatibility overrides keyed by exact model id. */
|
|
16
|
+
export type ModelCompatibility = Record<string, CompatibilityOverride>
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Defensively parse the `modelCompatibility` provider option. Non-object
|
|
20
|
+
* input, non-object per-model values and values outside the accepted set
|
|
21
|
+
* are dropped; `null` is kept.
|
|
22
|
+
*/
|
|
23
|
+
export function parseModelCompatibility(raw: unknown): ModelCompatibility {
|
|
24
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return {}
|
|
25
|
+
const out: ModelCompatibility = {}
|
|
26
|
+
for (const [modelId, entry] of Object.entries(raw as Record<string, unknown>)) {
|
|
27
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue
|
|
28
|
+
const { cacheFormat } = entry as Record<string, unknown>
|
|
29
|
+
if (cacheFormat === null || (typeof cacheFormat === 'string' && CACHE_FORMATS.has(cacheFormat))) {
|
|
30
|
+
out[modelId] = { cacheFormat }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return out
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Compatibility OpenCode 2 needs for a discovered model, from what the
|
|
38
|
+
* proxy reported about its upstream. Only the Chat Completions runtimes
|
|
39
|
+
* read `cacheFormat`: Anthropic Messages adds its own cache breakpoints
|
|
40
|
+
* and the Responses runtimes do not use the setting.
|
|
41
|
+
*/
|
|
42
|
+
export function inferCompatibility(
|
|
43
|
+
hints: ModelHints | undefined,
|
|
44
|
+
chatCompletions: boolean,
|
|
45
|
+
): Record<string, string> {
|
|
46
|
+
if (chatCompletions && hints?.acceptsCacheControl) return { cacheFormat: 'anthropic' }
|
|
47
|
+
return {}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Overlay a user override on inferred compatibility. `null` deletes the
|
|
52
|
+
* key. Returns `undefined` when nothing remains so callers can leave
|
|
53
|
+
* `compatibility` unset.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveCompatibility(
|
|
56
|
+
inferred: Record<string, string>,
|
|
57
|
+
override?: CompatibilityOverride,
|
|
58
|
+
): Record<string, string> | undefined {
|
|
59
|
+
const out: Record<string, string> = { ...inferred }
|
|
60
|
+
if (override) {
|
|
61
|
+
if (override.cacheFormat === null) delete out.cacheFormat
|
|
62
|
+
else out.cacheFormat = override.cacheFormat
|
|
63
|
+
}
|
|
64
|
+
return Object.keys(out).length > 0 ? out : undefined
|
|
65
|
+
}
|