opencode-plugin-litellm 1.10.0 → 1.11.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 +26 -24
- package/package.json +5 -1
- package/src/plugin/index.ts +60 -14
- package/src/plugin/v2.ts +58 -25
- package/src/types/index.ts +6 -0
- package/src/utils/format-model-name.ts +39 -1
- package/src/utils/litellm-api.ts +5 -4
- package/src/utils/model-cache.ts +28 -6
package/README.md
CHANGED
|
@@ -18,13 +18,16 @@
|
|
|
18
18
|
[](./LICENSE)
|
|
19
19
|
[](./tsconfig.json)
|
|
20
20
|
[](./CONTRIBUTING.md)
|
|
21
|
+
[](https://github.com/sponsors/yuseferi)
|
|
21
22
|
|
|
22
23
|
Auto-detect a running LiteLLM proxy, pull every model from `/v1/models`, and register them in OpenCode.
|
|
23
24
|
**No model lists to hand-maintain. No restart loops. No surprises.**
|
|
24
25
|
|
|
25
26
|
<img src="https://raw.githubusercontent.com/yuseferi/opencode-litellm/main/assets/architecture.svg" alt="How opencode-litellm works: OpenCode registers discovered LiteLLM models through the provider API and refreshes its provider registry from a disk cache" width="100%" />
|
|
26
27
|
|
|
27
|
-
[Quickstart](#-quickstart) · [Configuration](#%EF%B8%8F-configuration) · [How it works](#-how-it-works) · [FAQ](#-faq) · [Contributing](./CONTRIBUTING.md)
|
|
28
|
+
[Quickstart](#-quickstart) · [Configuration](#%EF%B8%8F-configuration) · [How it works](#-how-it-works) · [FAQ](#-faq) · [Contributing](./CONTRIBUTING.md) · [Sponsor](#-sponsor)
|
|
29
|
+
|
|
30
|
+
**Enjoying the plugin? [⭐ Star the repo](https://github.com/yuseferi/opencode-litellm)** — stars are how other OpenCode users find it.
|
|
28
31
|
|
|
29
32
|
</div>
|
|
30
33
|
|
|
@@ -46,9 +49,9 @@ Maintaining a `models` block in `opencode.json` for every model your LiteLLM pro
|
|
|
46
49
|
|
|
47
50
|
## 🚀 Quickstart
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
Install the plugin from the CLI and configure the shared background service.
|
|
53
|
+
If your OpenCode 2 executable is named `opencode2`, use it in place of
|
|
54
|
+
`opencode` below.
|
|
52
55
|
|
|
53
56
|
```bash
|
|
54
57
|
opencode plugin add opencode-plugin-litellm
|
|
@@ -58,10 +61,6 @@ opencode service set env LITELLM_API_KEY YOUR_API_KEY
|
|
|
58
61
|
|
|
59
62
|
Then open OpenCode and select a discovered model under `litellm/...`.
|
|
60
63
|
|
|
61
|
-
> **RFC status:** OpenCode 2 support is not yet included in the published npm
|
|
62
|
-
> package. For now, `plugin add` installs the previous release, not this RFC
|
|
63
|
-
> implementation.
|
|
64
|
-
|
|
65
64
|
For a local LiteLLM proxy on port 4000, the base URL can be omitted and the
|
|
66
65
|
plugin will detect it automatically. The [configuration section](#%EF%B8%8F-configuration)
|
|
67
66
|
also shows how to configure a provider directly in `opencode.json`.
|
|
@@ -73,7 +72,7 @@ also shows how to configure a provider directly in `opencode.json`.
|
|
|
73
72
|
| 🔍 **Auto-detection** | Probes `localhost:4000`, `:8000`, `:8080` and adopts the first responsive proxy. |
|
|
74
73
|
| 📡 **Dynamic discovery** | Queries `/v1/models` so your OpenCode model picker always reflects your live `model_list`. |
|
|
75
74
|
| ⚡ **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
|
-
| 🏷️ **Smart formatting** |
|
|
75
|
+
| 🏷️ **Smart formatting** | Uses LiteLLM's `model_info.display_name` when set; otherwise 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
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. 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
77
|
| 💵 **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
78
|
| 🧩 **Reasoning-effort variants** | When LiteLLM reports per-model effort support (`supports_low_reasoning_effort`, …), the plugin surfaces each level as a picker variant automatically. |
|
|
@@ -334,7 +333,7 @@ If your LiteLLM proxy requires a master key, expose it via any of these approach
|
|
|
334
333
|
| Env var (alias) | `export LITELLM_MASTER_KEY=sk-...` |
|
|
335
334
|
| Config | `"settings": { "apiKey": "{env:LITELLM_API_KEY}" }` |
|
|
336
335
|
|
|
337
|
-
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
|
+
The env var path lets you commit `opencode.json` without leaking secrets. `/connect` credentials are read from wherever OpenCode keeps them — the provider's connection on OpenCode 2, and OpenCode's auth store (`~/.local/share/opencode/auth.json`) on OpenCode 1 and on 2.x boxes that still write it — 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 config-defined provider keeps its key on an integration named after its own id, so a `litellm-team-a` provider uses the key connected for `litellm-team-a` and falls back to the plugin's `litellm` integration only when that one has no connection. 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.
|
|
338
337
|
|
|
339
338
|
#### Web UI connect (OpenCode 2)
|
|
340
339
|
|
|
@@ -472,7 +471,7 @@ Model classification (tool-call badge, attachments, reasoning, input modalities)
|
|
|
472
471
|
|
|
473
472
|
### Keeping raw model ids in the picker (`formatModelNames`)
|
|
474
473
|
|
|
475
|
-
By default the plugin
|
|
474
|
+
By default the plugin uses the name LiteLLM reports in `model_info.display_name` when it is set, and otherwise prettifies the id — `anthropic/claude-3-5-sonnet` shows up as `Claude 3.5 Sonnet`. If you'd rather see your LiteLLM `model_list` aliases verbatim (for example because your team refers to models by those exact names, or the formatter mangles an internal naming scheme), turn formatting off:
|
|
476
475
|
|
|
477
476
|
```jsonc
|
|
478
477
|
{
|
|
@@ -487,9 +486,11 @@ By default the plugin prettifies each discovered id into a display name — `ant
|
|
|
487
486
|
}
|
|
488
487
|
```
|
|
489
488
|
|
|
490
|
-
-
|
|
489
|
+
- With formatting off, the display name becomes the exact model id as returned by `/v1/models` (provider prefix, version suffixes and all). Nothing else changes — ids, capability flags, pricing and filtering behave exactly as before.
|
|
490
|
+
- With formatting on, a valid `model_info.display_name` replaces the formatted id. Empty, whitespace-only and non-string values are ignored.
|
|
491
|
+
- An explicit `models.<id>.name` always wins; `formatModelNames: false` keeps the raw id even when `display_name` is set.
|
|
491
492
|
- Only a boolean `false` disables formatting; omitting the option or setting anything else keeps the default.
|
|
492
|
-
- 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.
|
|
493
|
+
- 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. A changed `display_name` is picked up by the existing background refresh (on OpenCode 1 it appears after the following start).
|
|
493
494
|
- 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).
|
|
494
495
|
|
|
495
496
|
### Naming models per provider (`modelNameTemplate`)
|
|
@@ -523,13 +524,12 @@ Several providers can point at the same LiteLLM proxy with different keys. Every
|
|
|
523
524
|
|
|
524
525
|
| Placeholder | Value |
|
|
525
526
|
| --- | --- |
|
|
526
|
-
| `{name}` | The display name the plugin
|
|
527
|
+
| `{name}` | The display name the plugin resolved — LiteLLM's `display_name` when valid, otherwise the formatted id, or the raw id when `formatModelNames: false` |
|
|
527
528
|
| `{id}` | The model id exactly as `/v1/models` returned it |
|
|
528
529
|
| `{provider_id}` | The OpenCode provider id, for example `litellm-team-a` |
|
|
529
530
|
| `{provider_name}` | The provider's configured `name`, or its id when no name is set |
|
|
530
531
|
|
|
531
532
|
- The default is `{name}`, so omitting the setting changes nothing.
|
|
532
|
-
- The template runs after name formatting, so it composes with `formatModelNames: false`.
|
|
533
533
|
- An explicit `models.<id>.name` still wins and is never templated.
|
|
534
534
|
- Unknown placeholders are left as written, and substitution is single-pass: a placeholder that appears inside a resolved value stays literal.
|
|
535
535
|
- Non-string and empty values are ignored.
|
|
@@ -604,7 +604,7 @@ sequenceDiagram
|
|
|
604
604
|
2. If `providers.litellm` exists, its `settings.baseURL` is used. Otherwise common ports are probed — that probe is the only health check (3 s fail-fast per port).
|
|
605
605
|
3. With a configured `baseURL` the proxy is not contacted during startup unless the cache is cold; the discovery fetch itself fails fast if the proxy is unreachable.
|
|
606
606
|
4. **Fast path:** if a fresh on-disk cache exists (≤ 7 days old), its models are registered synchronously — startup does not wait on network discovery.
|
|
607
|
-
5. **Cold path:** `/v1/models` and `/v1/model/info` are fetched in parallel. Models are enriched with info metadata (`mode`, token limits, capability flags, per-token pricing — `/v1/models` omits these for database-defined models) and converted into OpenCode model entries with
|
|
607
|
+
5. **Cold path:** `/v1/models` and `/v1/model/info` are fetched in parallel. Models are enriched with info metadata (`mode`, token limits, capability flags, per-token pricing — `/v1/models` omits these for database-defined models) and converted into OpenCode model entries with a resolved `name`, inferred `modalities`, and `cost` (USD/1M tokens, converted from LiteLLM's USD/token). Non-chat models (embedding / image / audio) are excluded from the picker.
|
|
608
608
|
6. Discovered models are added alongside user-defined models — never overwriting them — and persisted to the cache.
|
|
609
609
|
7. On `session.created`, the cache is revalidated in the background (throttled to once per 5 minutes). V2 calls `provider.reload()` when the inventory changes; V1 surfaces refreshed entries on the next OpenCode start. The cold path is capped by a 20 s timeout.
|
|
610
610
|
|
|
@@ -789,6 +789,16 @@ See [`CONTRIBUTING.md`](./CONTRIBUTING.md) for the full contributor workflow.
|
|
|
789
789
|
|
|
790
790
|
Have an idea? [Open an issue](https://github.com/yuseferi/opencode-litellm/issues/new).
|
|
791
791
|
|
|
792
|
+
## 💖 Sponsor
|
|
793
|
+
|
|
794
|
+
This plugin is free, MIT-licensed, and stays that way. If it saves you time, you can
|
|
795
|
+
support its maintenance through **[GitHub Sponsors](https://github.com/sponsors/yuseferi)** —
|
|
796
|
+
one-off or recurring, cancel anytime.
|
|
797
|
+
|
|
798
|
+
[](https://github.com/sponsors/yuseferi)
|
|
799
|
+
|
|
800
|
+
Not in a position to sponsor? A bug report or a PR helps just as much.
|
|
801
|
+
|
|
792
802
|
## 🙏 Acknowledgements
|
|
793
803
|
|
|
794
804
|
Inspired by [`opencode-lmstudio`](https://github.com/agustif/opencode-lmstudio) by [@agustif](https://github.com/agustif) — the architectural blueprint for OpenCode model-discovery plugins.
|
|
@@ -798,11 +808,3 @@ Built on top of [LiteLLM](https://github.com/BerriAI/litellm) by the [BerriAI](h
|
|
|
798
808
|
## 📄 License
|
|
799
809
|
|
|
800
810
|
[MIT](./LICENSE) © [Yusef Mohamadi](https://github.com/yuseferi)
|
|
801
|
-
|
|
802
|
-
---
|
|
803
|
-
|
|
804
|
-
<div align="center">
|
|
805
|
-
|
|
806
|
-
If this project saved you time, consider giving it a ⭐ on [GitHub](https://github.com/yuseferi/opencode-litellm).
|
|
807
|
-
|
|
808
|
-
</div>
|
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.11.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",
|
|
@@ -33,6 +33,10 @@
|
|
|
33
33
|
"url": "https://github.com/yuseferi/opencode-litellm/issues"
|
|
34
34
|
},
|
|
35
35
|
"homepage": "https://github.com/yuseferi/opencode-litellm#readme",
|
|
36
|
+
"funding": {
|
|
37
|
+
"type": "github",
|
|
38
|
+
"url": "https://github.com/sponsors/yuseferi"
|
|
39
|
+
},
|
|
36
40
|
"publishConfig": {
|
|
37
41
|
"access": "public",
|
|
38
42
|
"provenance": true
|
package/src/plugin/index.ts
CHANGED
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
import {
|
|
11
11
|
applyModelNameTemplate,
|
|
12
12
|
DEFAULT_MODEL_NAME_TEMPLATE,
|
|
13
|
-
|
|
13
|
+
normalizeModelDisplayName,
|
|
14
|
+
resolveModelDisplayName,
|
|
14
15
|
categorizeModel,
|
|
15
16
|
type ModelNaming,
|
|
16
17
|
} from '../utils/format-model-name'
|
|
@@ -297,10 +298,15 @@ const USD_PER_TOKEN_TO_PER_MILLION = 1_000_000
|
|
|
297
298
|
|
|
298
299
|
/**
|
|
299
300
|
* The display name a model gets before any provider template is applied —
|
|
300
|
-
*
|
|
301
|
+
* LiteLLM's `model_info.display_name` when valid, the plugin's default
|
|
302
|
+
* formatting, or the raw `/v1/models` id when formatting is disabled.
|
|
301
303
|
*/
|
|
302
|
-
function baseDisplayName(
|
|
303
|
-
|
|
304
|
+
function baseDisplayName(
|
|
305
|
+
model: { id: string },
|
|
306
|
+
formatModelNames: boolean,
|
|
307
|
+
displayName?: unknown,
|
|
308
|
+
): string {
|
|
309
|
+
return resolveModelDisplayName(model, { formatModelNames, displayName })
|
|
304
310
|
}
|
|
305
311
|
|
|
306
312
|
/**
|
|
@@ -310,9 +316,10 @@ function baseDisplayName(model: { id: string }, formatModelNames: boolean): stri
|
|
|
310
316
|
* image, audio) — they can't be used as primary chat models and would
|
|
311
317
|
* clutter the picker.
|
|
312
318
|
*
|
|
313
|
-
* Exported for tests: the
|
|
314
|
-
* prettified id or the raw `/v1/models` id
|
|
315
|
-
* provider's
|
|
319
|
+
* Exported for tests: the display name comes from `info.display_name` when
|
|
320
|
+
* valid, otherwise the prettified id or the raw `/v1/models` id when
|
|
321
|
+
* formatting is disabled, optionally wrapped in the provider's
|
|
322
|
+
* `modelNameTemplate`.
|
|
316
323
|
*/
|
|
317
324
|
export function toConfigModel(
|
|
318
325
|
model: LiteLLMModel,
|
|
@@ -326,7 +333,11 @@ export function toConfigModel(
|
|
|
326
333
|
const providerId = naming.providerId ?? ''
|
|
327
334
|
const entry: Record<string, unknown> = {
|
|
328
335
|
name: applyModelNameTemplate(naming.modelNameTemplate, {
|
|
329
|
-
name: baseDisplayName(
|
|
336
|
+
name: baseDisplayName(
|
|
337
|
+
model,
|
|
338
|
+
naming.formatModelNames !== false,
|
|
339
|
+
info?.display_name,
|
|
340
|
+
),
|
|
330
341
|
id: model.id,
|
|
331
342
|
providerId,
|
|
332
343
|
// Without a configured display name the picker falls back to the id,
|
|
@@ -410,12 +421,16 @@ export function toConfigModel(
|
|
|
410
421
|
* lists the subset that must be invoked through the OpenAI Responses API;
|
|
411
422
|
* callers route those accordingly without inspecting each entry. `hints`
|
|
412
423
|
* carries per-model upstream facts OpenCode 2 maps to `compatibility` and
|
|
413
|
-
* `variants`.
|
|
424
|
+
* `variants`. `displayNames` records each model's valid
|
|
425
|
+
* `model_info.display_name` (when formatting is enabled), so a cached model
|
|
426
|
+
* can be re-rendered for the Responses sibling without losing it; an explicit
|
|
427
|
+
* `models.<id>.name` still wins at merge time.
|
|
414
428
|
*/
|
|
415
429
|
export interface DiscoveredModels {
|
|
416
430
|
models: Record<string, unknown>
|
|
417
431
|
responsesIds: string[]
|
|
418
432
|
hints: Record<string, ModelHints>
|
|
433
|
+
displayNames: Record<string, string>
|
|
419
434
|
}
|
|
420
435
|
|
|
421
436
|
/**
|
|
@@ -492,6 +507,7 @@ export async function discoverModels(
|
|
|
492
507
|
const built: Record<string, unknown> = {}
|
|
493
508
|
const responsesIds: string[] = []
|
|
494
509
|
const hints: Record<string, ModelHints> = {}
|
|
510
|
+
const displayNames: Record<string, string> = {}
|
|
495
511
|
let skipped = 0
|
|
496
512
|
let wildcards = 0
|
|
497
513
|
let filtered = 0
|
|
@@ -520,6 +536,13 @@ export async function discoverModels(
|
|
|
520
536
|
continue
|
|
521
537
|
}
|
|
522
538
|
built[model.id] = entry
|
|
539
|
+
// Record the display name for every built model: an explicit
|
|
540
|
+
// models.<id>.name still wins at merge time, and with formatting
|
|
541
|
+
// disabled the raw id wins, so nothing is recorded.
|
|
542
|
+
if (naming.formatModelNames !== false) {
|
|
543
|
+
const displayName = normalizeModelDisplayName(info?.display_name)
|
|
544
|
+
if (displayName) displayNames[model.id] = displayName
|
|
545
|
+
}
|
|
523
546
|
if (requiresResponsesAPI(enriched)) responsesIds.push(model.id)
|
|
524
547
|
const params = info?.supported_openai_params
|
|
525
548
|
hints[model.id] = {
|
|
@@ -558,7 +581,7 @@ export async function discoverModels(
|
|
|
558
581
|
')',
|
|
559
582
|
)
|
|
560
583
|
|
|
561
|
-
return { models: built, responsesIds, hints }
|
|
584
|
+
return { models: built, responsesIds, hints, displayNames }
|
|
562
585
|
}
|
|
563
586
|
|
|
564
587
|
/**
|
|
@@ -674,7 +697,9 @@ function readHeaderMap(value: unknown): Record<string, string> {
|
|
|
674
697
|
* sibling, so a template that uses `{provider_id}`/`{provider_name}` must
|
|
675
698
|
* resolve to the provider the user actually selects. The entry in the cache
|
|
676
699
|
* stays as discovered — OpenCode 2 reads the same cache with a single
|
|
677
|
-
* provider — so this returns a copy instead of mutating it.
|
|
700
|
+
* provider — so this returns a copy instead of mutating it. `displayName` is
|
|
701
|
+
* the LiteLLM value recorded during discovery (when any), so re-rendering
|
|
702
|
+
* does not lose it.
|
|
678
703
|
*/
|
|
679
704
|
function renameForProvider(
|
|
680
705
|
entry: Record<string, unknown>,
|
|
@@ -682,12 +707,17 @@ function renameForProvider(
|
|
|
682
707
|
naming: ModelNaming,
|
|
683
708
|
providerId: string,
|
|
684
709
|
providerName: string,
|
|
710
|
+
displayName?: string,
|
|
685
711
|
): Record<string, unknown> {
|
|
686
712
|
if (naming.modelNameTemplate === undefined) return entry
|
|
687
713
|
return {
|
|
688
714
|
...entry,
|
|
689
715
|
name: applyModelNameTemplate(naming.modelNameTemplate, {
|
|
690
|
-
name: baseDisplayName(
|
|
716
|
+
name: baseDisplayName(
|
|
717
|
+
{ id: modelId },
|
|
718
|
+
naming.formatModelNames !== false,
|
|
719
|
+
displayName,
|
|
720
|
+
),
|
|
691
721
|
id: modelId,
|
|
692
722
|
providerId,
|
|
693
723
|
providerName,
|
|
@@ -708,6 +738,7 @@ function injectDiscoveredModels(
|
|
|
708
738
|
baseModels: Record<string, unknown>,
|
|
709
739
|
entries: Record<string, unknown>,
|
|
710
740
|
responsesIds: readonly string[],
|
|
741
|
+
displayNames: Record<string, string>,
|
|
711
742
|
baseURL: string,
|
|
712
743
|
apiKey: string | undefined,
|
|
713
744
|
customHeaders: Record<string, string> | undefined,
|
|
@@ -778,6 +809,7 @@ function injectDiscoveredModels(
|
|
|
778
809
|
naming,
|
|
779
810
|
responsesProviderId,
|
|
780
811
|
siblingName,
|
|
812
|
+
displayNames[id],
|
|
781
813
|
)
|
|
782
814
|
}
|
|
783
815
|
added.push(...mergeModels(siblingModels, responsesEntries))
|
|
@@ -822,7 +854,13 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
822
854
|
DISCOVERY_TIMEOUT_MS,
|
|
823
855
|
)
|
|
824
856
|
if (discovered && Object.keys(discovered.models).length > 0) {
|
|
825
|
-
writeModelCache(
|
|
857
|
+
writeModelCache(
|
|
858
|
+
cacheKey,
|
|
859
|
+
discovered.models,
|
|
860
|
+
discovered.responsesIds,
|
|
861
|
+
discovered.hints,
|
|
862
|
+
discovered.displayNames,
|
|
863
|
+
)
|
|
826
864
|
log(
|
|
827
865
|
'info',
|
|
828
866
|
`[opencode-litellm] Background-refreshed model cache for ${ctx.baseURL} (${Object.keys(discovered.models).length} models)`,
|
|
@@ -1117,6 +1155,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1117
1155
|
models,
|
|
1118
1156
|
cached.models,
|
|
1119
1157
|
cached.responsesIds,
|
|
1158
|
+
cached.displayNames,
|
|
1120
1159
|
baseURL,
|
|
1121
1160
|
runtimeKey,
|
|
1122
1161
|
customHeaders,
|
|
@@ -1154,6 +1193,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1154
1193
|
models,
|
|
1155
1194
|
discovered.models,
|
|
1156
1195
|
discovered.responsesIds,
|
|
1196
|
+
discovered.displayNames,
|
|
1157
1197
|
baseURL,
|
|
1158
1198
|
runtimeKey,
|
|
1159
1199
|
customHeaders,
|
|
@@ -1161,7 +1201,13 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1161
1201
|
naming,
|
|
1162
1202
|
)
|
|
1163
1203
|
injectedModelIds.set(cacheKey, new Set(added))
|
|
1164
|
-
writeModelCache(
|
|
1204
|
+
writeModelCache(
|
|
1205
|
+
cacheKey,
|
|
1206
|
+
discovered.models,
|
|
1207
|
+
discovered.responsesIds,
|
|
1208
|
+
discovered.hints,
|
|
1209
|
+
discovered.displayNames,
|
|
1210
|
+
)
|
|
1165
1211
|
}
|
|
1166
1212
|
}
|
|
1167
1213
|
},
|
package/src/plugin/v2.ts
CHANGED
|
@@ -533,23 +533,43 @@ async function makeProviderSource(
|
|
|
533
533
|
const settings = { ...pluginOptions, ...providerSettings }
|
|
534
534
|
const customHeaders = providerHeaders(pluginOptions, settings, provider)
|
|
535
535
|
const configuredBaseURL = readString(settings.baseURL)
|
|
536
|
-
// Credential plus dialog form answers.
|
|
537
|
-
//
|
|
538
|
-
//
|
|
539
|
-
//
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
536
|
+
// Credential plus dialog form answers. Which integration to read depends on
|
|
537
|
+
// how the provider got here:
|
|
538
|
+
// 1. a provider wired to its own integration keeps using exactly that one;
|
|
539
|
+
// 2. a config-defined provider owns no integrationID, but `/connect` files
|
|
540
|
+
// its key under an integration named after the provider id — so for
|
|
541
|
+
// `litellm-a` the key lives in `litellm-a`, not in `litellm`;
|
|
542
|
+
// 3. the plugin's registered integration is the fallback, so a gateway can
|
|
543
|
+
// be connected entirely from the web UI with no hand-written config.
|
|
544
|
+
const idHint = provider?.id ?? readString(pluginOptions.providerID)
|
|
545
|
+
const integrationCandidates: string[] = []
|
|
546
|
+
if (provider?.integrationID) {
|
|
547
|
+
// Explicitly linked: authoritative, never widened to another integration.
|
|
548
|
+
integrationCandidates.push(provider.integrationID)
|
|
549
|
+
} else {
|
|
550
|
+
if (idHint) integrationCandidates.push(idHint)
|
|
551
|
+
if (!integrationCandidates.includes(LITELLM_INTEGRATION_ID)) {
|
|
552
|
+
integrationCandidates.push(LITELLM_INTEGRATION_ID)
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
let connected: ConnectedLiteLLM | undefined
|
|
556
|
+
// The integration that actually supplied the credential. Recorded on the
|
|
557
|
+
// source so refresh and `credential.switched` re-read the same one.
|
|
558
|
+
let connectedIntegrationID: string | undefined
|
|
559
|
+
for (const candidateID of integrationCandidates) {
|
|
560
|
+
const candidate = await readConnectedConfig(context, candidateID)
|
|
561
|
+
// A base URL configured for another proxy turns the sign-in off.
|
|
562
|
+
if (
|
|
563
|
+
!candidate ||
|
|
564
|
+
(candidate.credentialID && !signInReaches(candidate.baseURL, configuredBaseURL))
|
|
565
|
+
) {
|
|
566
|
+
continue
|
|
567
|
+
}
|
|
568
|
+
connected = candidate
|
|
569
|
+
connectedIntegrationID = candidateID
|
|
570
|
+
break
|
|
547
571
|
}
|
|
548
|
-
const id =
|
|
549
|
-
provider?.id ??
|
|
550
|
-
readString(pluginOptions.providerID) ??
|
|
551
|
-
connected?.providerID ??
|
|
552
|
-
CHAT_PROVIDER_ID
|
|
572
|
+
const id = idHint ?? connected?.providerID ?? CHAT_PROVIDER_ID
|
|
553
573
|
const connectedApiKey = connected?.apiKey
|
|
554
574
|
// Precedence rule: an explicit settings/plugin-options apiKey is the
|
|
555
575
|
// operator's deliberate config and stays first; then a credential the user
|
|
@@ -559,16 +579,17 @@ async function makeProviderSource(
|
|
|
559
579
|
// override of the user's explicit choice.
|
|
560
580
|
const settingsApiKey = readString(providerSettings.apiKey) ?? readString(pluginOptions.apiKey)
|
|
561
581
|
const envApiKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
562
|
-
// The key OpenCode
|
|
563
|
-
//
|
|
564
|
-
//
|
|
565
|
-
//
|
|
566
|
-
// credential is the user's explicit choice and beats the box-level
|
|
582
|
+
// The key OpenCode stored for this provider in the box auth store — the
|
|
583
|
+
// `/connect` journey on boxes that still keep `auth.json`. OpenCode 2.3 keeps
|
|
584
|
+
// credentials in its own database instead, so this is the legacy fallback;
|
|
585
|
+
// the connection read above is the current path. Still ahead of the env: a
|
|
586
|
+
// saved credential is the user's explicit choice and beats the box-level
|
|
587
|
+
// fallback.
|
|
567
588
|
const storedKey = await getOpenCodeStoredApiKey(id)
|
|
568
589
|
const usesConnectionCredential = settingsApiKey === undefined && connectedApiKey !== undefined
|
|
569
590
|
const integrationID =
|
|
570
591
|
provider?.integrationID ??
|
|
571
|
-
(usesConnectionCredential ?
|
|
592
|
+
(usesConnectionCredential ? connectedIntegrationID : undefined)
|
|
572
593
|
const apiKey = settingsApiKey ?? connectedApiKey ?? storedKey ?? envApiKey
|
|
573
594
|
const filters = readModelFilters(settings)
|
|
574
595
|
const capabilities = parseModelCapabilities(settings.modelCapabilities)
|
|
@@ -626,7 +647,7 @@ async function makeProviderSource(
|
|
|
626
647
|
entries = discovered.models
|
|
627
648
|
responsesIds = discovered.responsesIds
|
|
628
649
|
hints = discovered.hints
|
|
629
|
-
writeModelCache(cacheKey, entries, responsesIds, hints)
|
|
650
|
+
writeModelCache(cacheKey, entries, responsesIds, hints, discovered.displayNames)
|
|
630
651
|
} else {
|
|
631
652
|
entries = null
|
|
632
653
|
responsesIds = []
|
|
@@ -746,7 +767,13 @@ async function refreshProviderSource(
|
|
|
746
767
|
compatibility: source.compatibility,
|
|
747
768
|
})
|
|
748
769
|
if (JSON.stringify(models) === JSON.stringify(source.models)) {
|
|
749
|
-
writeModelCache(
|
|
770
|
+
writeModelCache(
|
|
771
|
+
source.cacheKey,
|
|
772
|
+
discovered.models,
|
|
773
|
+
discovered.responsesIds,
|
|
774
|
+
discovered.hints,
|
|
775
|
+
discovered.displayNames,
|
|
776
|
+
)
|
|
750
777
|
source.refreshRequired = false
|
|
751
778
|
return
|
|
752
779
|
}
|
|
@@ -760,7 +787,13 @@ async function refreshProviderSource(
|
|
|
760
787
|
throw error
|
|
761
788
|
}
|
|
762
789
|
|
|
763
|
-
writeModelCache(
|
|
790
|
+
writeModelCache(
|
|
791
|
+
source.cacheKey,
|
|
792
|
+
discovered.models,
|
|
793
|
+
discovered.responsesIds,
|
|
794
|
+
discovered.hints,
|
|
795
|
+
discovered.displayNames,
|
|
796
|
+
)
|
|
764
797
|
source.refreshRequired = false
|
|
765
798
|
logInfo(
|
|
766
799
|
`[opencode-litellm] Refreshed ${models.length} models for provider "${source.id}"; the provider registry was reloaded.`,
|
package/src/types/index.ts
CHANGED
|
@@ -59,6 +59,12 @@ export interface LiteLLMModelInfo {
|
|
|
59
59
|
db_model?: boolean
|
|
60
60
|
/** Alias LiteLLM assigns to the model; mirrors the `/v1/models` id. */
|
|
61
61
|
key?: string
|
|
62
|
+
/**
|
|
63
|
+
* Human-readable name configured on the deployment
|
|
64
|
+
* (`model_info.display_name`). Display-only: never used as the model id or
|
|
65
|
+
* in API requests. Absent or malformed values fall back to id formatting.
|
|
66
|
+
*/
|
|
67
|
+
display_name?: string | null
|
|
62
68
|
mode?: string
|
|
63
69
|
max_tokens?: number
|
|
64
70
|
max_input_tokens?: number
|
|
@@ -197,13 +197,51 @@ export function formatModelName(model: Pick<LiteLLMModel, 'id'>): string {
|
|
|
197
197
|
return tokens.join(' ') || id
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Normalise a LiteLLM `model_info.display_name` into a display name.
|
|
202
|
+
*
|
|
203
|
+
* Only a non-empty string counts. The value is trimmed so surrounding
|
|
204
|
+
* whitespace never reaches the picker; everything else (undefined, null,
|
|
205
|
+
* empty, whitespace-only, numbers, objects) is treated as "no display name"
|
|
206
|
+
* and lets the caller fall back to id formatting.
|
|
207
|
+
*/
|
|
208
|
+
export function normalizeModelDisplayName(value: unknown): string | undefined {
|
|
209
|
+
if (typeof value !== 'string') return undefined
|
|
210
|
+
const trimmed = value.trim()
|
|
211
|
+
return trimmed === '' ? undefined : trimmed
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Resolve the display name for a discovered model before any provider
|
|
216
|
+
* template is applied.
|
|
217
|
+
*
|
|
218
|
+
* Precedence:
|
|
219
|
+
* 1. `formatModelNames: false` keeps the raw `/v1/models` id, as before.
|
|
220
|
+
* 2. A valid LiteLLM `model_info.display_name`.
|
|
221
|
+
* 3. The prettified id (`formatModelName`).
|
|
222
|
+
*
|
|
223
|
+
* `display_name` is display-only: callers keep using `model.id` for the
|
|
224
|
+
* model id, requests, routing and filtering.
|
|
225
|
+
*/
|
|
226
|
+
export function resolveModelDisplayName(
|
|
227
|
+
model: Pick<LiteLLMModel, 'id'>,
|
|
228
|
+
options: { formatModelNames?: boolean; displayName?: unknown } = {},
|
|
229
|
+
): string {
|
|
230
|
+
if (options.formatModelNames === false) return model.id
|
|
231
|
+
return normalizeModelDisplayName(options.displayName) ?? formatModelName(model)
|
|
232
|
+
}
|
|
233
|
+
|
|
200
234
|
/**
|
|
201
235
|
* The naming choices that decide how a discovered model is displayed. They
|
|
202
236
|
* are baked into cached model entries, so the same object also feeds the
|
|
203
237
|
* cache identity (see `buildCacheKey` in `model-cache.ts`).
|
|
204
238
|
*/
|
|
205
239
|
export interface ModelNaming {
|
|
206
|
-
/**
|
|
240
|
+
/**
|
|
241
|
+
* Prettify a model id into a display name. A valid LiteLLM
|
|
242
|
+
* `model_info.display_name` takes precedence; `false` keeps the raw id and
|
|
243
|
+
* ignores the display name too.
|
|
244
|
+
*/
|
|
207
245
|
formatModelNames?: boolean
|
|
208
246
|
/**
|
|
209
247
|
* Template applied to every discovered model's display name. Unset (or
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -102,10 +102,11 @@ export async function discoverLiteLLMModels(
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
* Fetch per-model metadata (`mode`, token limits, capability flags
|
|
106
|
-
* from `/v1/model/info`, keyed by model name.
|
|
107
|
-
* fields for database-defined models, so
|
|
108
|
-
* out embedding models) relies on this
|
|
105
|
+
* Fetch per-model metadata (`mode`, token limits, capability flags and the
|
|
106
|
+
* optional `display_name`) from `/v1/model/info`, keyed by model name.
|
|
107
|
+
* `/v1/models` omits these fields for database-defined models, so
|
|
108
|
+
* classification (e.g. filtering out embedding models) relies on this
|
|
109
|
+
* endpoint.
|
|
109
110
|
*/
|
|
110
111
|
export async function discoverLiteLLMModelInfo(
|
|
111
112
|
baseURL: string = DEFAULT_LITELLM_URL,
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { createHash } from 'node:crypto'
|
|
|
2
2
|
import { readFileSync, mkdirSync, writeFileSync, renameSync, rmSync } from 'node:fs'
|
|
3
3
|
import { homedir, tmpdir } from 'node:os'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
|
-
import type
|
|
5
|
+
import { normalizeModelDisplayName, type ModelNaming } from './format-model-name'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Facts about a discovered model's upstream that OpenCode 2 turns into
|
|
@@ -30,7 +30,7 @@ export interface ModelHints {
|
|
|
30
30
|
* from older plugin versions are ignored rather than deserialized into
|
|
31
31
|
* an incompatible structure.
|
|
32
32
|
*/
|
|
33
|
-
const CACHE_VERSION =
|
|
33
|
+
const CACHE_VERSION = 7
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Maximum age of a cache entry before it's treated as a miss. Without a
|
|
@@ -56,6 +56,13 @@ interface ModelCacheFile {
|
|
|
56
56
|
* no place in a provider-config entry (see `ModelHints`).
|
|
57
57
|
*/
|
|
58
58
|
hints?: Record<string, ModelHints>
|
|
59
|
+
/**
|
|
60
|
+
* LiteLLM `model_info.display_name` values that were used for a model's
|
|
61
|
+
* display name. The templated name is already baked into `models`, but
|
|
62
|
+
* OpenCode 1's Responses sibling re-renders names with its own provider
|
|
63
|
+
* placeholders and needs the pre-template display name to do so.
|
|
64
|
+
*/
|
|
65
|
+
displayNames?: Record<string, string>
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
/**
|
|
@@ -183,6 +190,17 @@ function normalizeResponsesIds(value: unknown): string[] {
|
|
|
183
190
|
return value.filter((id): id is string => typeof id === 'string')
|
|
184
191
|
}
|
|
185
192
|
|
|
193
|
+
/** Keep only non-empty string entries of a cached `displayNames` map. */
|
|
194
|
+
function normalizeDisplayNames(value: unknown): Record<string, string> {
|
|
195
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
196
|
+
const out: Record<string, string> = {}
|
|
197
|
+
for (const [id, name] of Object.entries(value as Record<string, unknown>)) {
|
|
198
|
+
const normalized = normalizeModelDisplayName(name)
|
|
199
|
+
if (normalized !== undefined) out[id] = normalized
|
|
200
|
+
}
|
|
201
|
+
return out
|
|
202
|
+
}
|
|
203
|
+
|
|
186
204
|
/** Keep only well-formed entries of a cached `hints` map. */
|
|
187
205
|
function normalizeHints(value: unknown): Record<string, ModelHints> {
|
|
188
206
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
|
|
@@ -220,10 +238,10 @@ export function readModelCache(
|
|
|
220
238
|
}
|
|
221
239
|
|
|
222
240
|
/**
|
|
223
|
-
* Read the models, Responses-API model ids
|
|
224
|
-
* in one parse. Returns `null` under the same
|
|
225
|
-
* {@link readModelCache} (missing/expired cache), so the
|
|
226
|
-
* disagree within a single lookup.
|
|
241
|
+
* Read the models, Responses-API model ids, model hints and LiteLLM display
|
|
242
|
+
* names for a cache key in one parse. Returns `null` under the same
|
|
243
|
+
* conditions as {@link readModelCache} (missing/expired cache), so the four
|
|
244
|
+
* can't disagree within a single lookup.
|
|
227
245
|
*/
|
|
228
246
|
export function readModelCacheEntry(
|
|
229
247
|
cacheKey: string,
|
|
@@ -231,6 +249,7 @@ export function readModelCacheEntry(
|
|
|
231
249
|
models: Record<string, unknown>
|
|
232
250
|
responsesIds: string[]
|
|
233
251
|
hints: Record<string, ModelHints>
|
|
252
|
+
displayNames: Record<string, string>
|
|
234
253
|
} | null {
|
|
235
254
|
const parsed = readCacheFile(cacheKey)
|
|
236
255
|
if (!parsed) return null
|
|
@@ -244,6 +263,7 @@ export function readModelCacheEntry(
|
|
|
244
263
|
models: parsed.models,
|
|
245
264
|
responsesIds: normalizeResponsesIds(parsed.responsesIds),
|
|
246
265
|
hints: normalizeHints(parsed.hints),
|
|
266
|
+
displayNames: normalizeDisplayNames(parsed.displayNames),
|
|
247
267
|
}
|
|
248
268
|
}
|
|
249
269
|
|
|
@@ -278,6 +298,7 @@ export function writeModelCache(
|
|
|
278
298
|
models: Record<string, unknown>,
|
|
279
299
|
responsesIds: readonly string[] = [],
|
|
280
300
|
hints: Record<string, ModelHints> = {},
|
|
301
|
+
displayNames: Record<string, string> = {},
|
|
281
302
|
): void {
|
|
282
303
|
try {
|
|
283
304
|
mkdirSync(cacheDir(), { recursive: true })
|
|
@@ -287,6 +308,7 @@ export function writeModelCache(
|
|
|
287
308
|
models,
|
|
288
309
|
responsesIds: [...responsesIds],
|
|
289
310
|
hints,
|
|
311
|
+
displayNames,
|
|
290
312
|
}
|
|
291
313
|
// Write to a temp file and rename so concurrent OpenCode processes
|
|
292
314
|
// never observe a partially-written JSON file (rename is atomic on
|