opencode-plugin-litellm 1.11.1 → 1.12.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 +29 -0
- package/package.json +1 -1
- package/src/plugin/index.ts +31 -9
- package/src/plugin/v2.ts +15 -2
- package/src/utils/litellm-api.ts +128 -26
- package/src/utils/model-cache.ts +8 -0
package/README.md
CHANGED
|
@@ -409,6 +409,35 @@ If your LiteLLM proxy is behind Cloudflare Access or another gateway that requir
|
|
|
409
409
|
|
|
410
410
|
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/).
|
|
411
411
|
|
|
412
|
+
### Separate management host and custom endpoints
|
|
413
|
+
|
|
414
|
+
Some deployments serve the OpenAI-compatible API and the management API (`/v1/model/info`) from different hosts. Without the info response the plugin only sees bare model ids, so reasoning effort variants and capability flags are missing. Point discovery at the right places:
|
|
415
|
+
|
|
416
|
+
```jsonc
|
|
417
|
+
{
|
|
418
|
+
"provider": {
|
|
419
|
+
"litellm": {
|
|
420
|
+
"options": {
|
|
421
|
+
"baseURL": "https://llm.example.com/v1",
|
|
422
|
+
"managementBaseURL": "https://management.llm.example.com",
|
|
423
|
+
"modelsEndpoint": "/v1/models", // optional
|
|
424
|
+
"modelInfoEndpoint": "/v1/model/info" // optional
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
| Setting | Env fallback | Default |
|
|
432
|
+
|---|---|---|
|
|
433
|
+
| `managementBaseURL` | `LITELLM_MANAGEMENT_BASE_URL` | `baseURL` |
|
|
434
|
+
| `modelsEndpoint` | `LITELLM_MODELS_ENDPOINT` | `/v1/models` |
|
|
435
|
+
| `modelInfoEndpoint` | `LITELLM_MODEL_INFO_ENDPOINT` | `/v1/model/info` |
|
|
436
|
+
|
|
437
|
+
- If the models endpoint fails but the info endpoint answers, the model list is built from the info response.
|
|
438
|
+
- The health check falls back to the info endpoint on the management host.
|
|
439
|
+
- Changing any of these starts a fresh discovery on the next start.
|
|
440
|
+
|
|
412
441
|
### Splitting one proxy into multiple providers (`includeModels` / `excludeModels`)
|
|
413
442
|
|
|
414
443
|
If your LiteLLM catalog mixes naming conventions from different teams or environments (e.g. `prod/*` and `staging/*`), you can point two OpenCode providers at the *same* proxy and have each one surface only its slice:
|
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.12.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",
|
package/src/plugin/index.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { Plugin, PluginInput } from '@opencode-ai/plugin'
|
|
2
2
|
import {
|
|
3
3
|
autoDetectLiteLLM,
|
|
4
|
+
buildModelInfoMap,
|
|
4
5
|
checkLiteLLMHealth,
|
|
5
|
-
discoverLiteLLMModelInfo,
|
|
6
6
|
discoverLiteLLMModels,
|
|
7
|
+
fetchLiteLLMModelInfoEntries,
|
|
7
8
|
getRequestTimeoutMs,
|
|
9
|
+
modelsFromInfoEntries,
|
|
8
10
|
normalizeBaseURL,
|
|
11
|
+
readDiscoveryEndpoints,
|
|
12
|
+
type DiscoveryEndpoints,
|
|
9
13
|
} from '../utils/litellm-api'
|
|
10
14
|
import {
|
|
11
15
|
applyModelNameTemplate,
|
|
@@ -121,6 +125,7 @@ interface RefreshContext {
|
|
|
121
125
|
modelNameTemplate?: string
|
|
122
126
|
providerName: string
|
|
123
127
|
providerId: string
|
|
128
|
+
endpoints: DiscoveryEndpoints
|
|
124
129
|
}
|
|
125
130
|
const refreshContexts = new Map<string, RefreshContext>()
|
|
126
131
|
|
|
@@ -455,8 +460,9 @@ export async function discoverModels(
|
|
|
455
460
|
filters: ModelFilters = {},
|
|
456
461
|
capabilities: ModelCapabilities = {},
|
|
457
462
|
naming: ModelNaming = {},
|
|
463
|
+
endpoints: DiscoveryEndpoints = {},
|
|
458
464
|
): Promise<DiscoveredModels | null> {
|
|
459
|
-
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
465
|
+
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders, endpoints))) {
|
|
460
466
|
log(
|
|
461
467
|
'warn',
|
|
462
468
|
`[opencode-litellm] LiteLLM appears offline or unauthorized for provider "${providerId}" at ${baseURL}`,
|
|
@@ -469,11 +475,24 @@ export async function discoverModels(
|
|
|
469
475
|
// it. The info call is best-effort: without it, classification
|
|
470
476
|
// falls back to id heuristics.
|
|
471
477
|
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
472
|
-
discoverLiteLLMModels(baseURL, apiKey, customHeaders),
|
|
473
|
-
|
|
478
|
+
discoverLiteLLMModels(baseURL, apiKey, customHeaders, endpoints),
|
|
479
|
+
fetchLiteLLMModelInfoEntries(baseURL, apiKey, customHeaders, endpoints),
|
|
474
480
|
])
|
|
475
481
|
|
|
476
|
-
|
|
482
|
+
let discovered: LiteLLMModel[]
|
|
483
|
+
if (modelsResult.status === 'fulfilled') {
|
|
484
|
+
discovered = modelsResult.value
|
|
485
|
+
} else if (infoResult.status === 'fulfilled' && infoResult.value.length > 0) {
|
|
486
|
+
// No usable models endpoint: the info response still names every model.
|
|
487
|
+
const error = modelsResult.reason
|
|
488
|
+
log(
|
|
489
|
+
'warn',
|
|
490
|
+
`[opencode-litellm] Models endpoint failed for provider "${providerId}" (` +
|
|
491
|
+
(error instanceof Error ? error.message : String(error)) +
|
|
492
|
+
'); building the model list from the model info endpoint.',
|
|
493
|
+
)
|
|
494
|
+
discovered = modelsFromInfoEntries(infoResult.value)
|
|
495
|
+
} else {
|
|
477
496
|
const error = modelsResult.reason
|
|
478
497
|
log(
|
|
479
498
|
'warn',
|
|
@@ -483,15 +502,14 @@ export async function discoverModels(
|
|
|
483
502
|
return null
|
|
484
503
|
}
|
|
485
504
|
|
|
486
|
-
const discovered = modelsResult.value
|
|
487
505
|
let infoByName: Map<string, LiteLLMModelInfo> | null = null
|
|
488
506
|
if (infoResult.status === 'fulfilled') {
|
|
489
|
-
infoByName = infoResult.value
|
|
507
|
+
infoByName = buildModelInfoMap(infoResult.value)
|
|
490
508
|
} else {
|
|
491
509
|
const reason = infoResult.reason
|
|
492
510
|
log(
|
|
493
511
|
'warn',
|
|
494
|
-
`[opencode-litellm]
|
|
512
|
+
`[opencode-litellm] Model info endpoint unavailable for provider "${providerId}"; non-chat model filtering will use id heuristics only: ` +
|
|
495
513
|
(reason instanceof Error ? reason.message : String(reason)),
|
|
496
514
|
)
|
|
497
515
|
}
|
|
@@ -850,6 +868,7 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
850
868
|
providerId: ctx.providerId,
|
|
851
869
|
providerName: ctx.providerName,
|
|
852
870
|
},
|
|
871
|
+
ctx.endpoints,
|
|
853
872
|
),
|
|
854
873
|
DISCOVERY_TIMEOUT_MS,
|
|
855
874
|
)
|
|
@@ -1019,6 +1038,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1019
1038
|
const providerName =
|
|
1020
1039
|
typeof provider.name === 'string' && provider.name ? provider.name : providerId
|
|
1021
1040
|
const naming = { formatModelNames, modelNameTemplate, providerId, providerName }
|
|
1041
|
+
const endpoints = readDiscoveryEndpoints(options)
|
|
1022
1042
|
|
|
1023
1043
|
// Resolve base URL
|
|
1024
1044
|
let baseURL: string | null = null
|
|
@@ -1095,7 +1115,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1095
1115
|
formatModelNames,
|
|
1096
1116
|
modelNameTemplate,
|
|
1097
1117
|
providerName,
|
|
1098
|
-
}, sso ? ssoIdentity(sso) : apiKey)
|
|
1118
|
+
}, sso ? ssoIdentity(sso) : apiKey, endpoints)
|
|
1099
1119
|
|
|
1100
1120
|
// Remember how to reach this proxy so the `event` hook can
|
|
1101
1121
|
// revalidate its cache in the background on new sessions.
|
|
@@ -1114,6 +1134,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1114
1134
|
modelNameTemplate,
|
|
1115
1135
|
providerName,
|
|
1116
1136
|
providerId,
|
|
1137
|
+
endpoints,
|
|
1117
1138
|
})
|
|
1118
1139
|
|
|
1119
1140
|
// The Responses sibling (if any) holds a disjoint set of ids, so the
|
|
@@ -1182,6 +1203,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1182
1203
|
filters,
|
|
1183
1204
|
capabilities,
|
|
1184
1205
|
{ formatModelNames, modelNameTemplate, providerId, providerName },
|
|
1206
|
+
endpoints,
|
|
1185
1207
|
),
|
|
1186
1208
|
DISCOVERY_TIMEOUT_MS,
|
|
1187
1209
|
)
|
package/src/plugin/v2.ts
CHANGED
|
@@ -27,7 +27,12 @@ import {
|
|
|
27
27
|
readModelCacheSavedAt,
|
|
28
28
|
writeModelCache,
|
|
29
29
|
} from '../utils/model-cache'
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
normalizeBaseURL,
|
|
32
|
+
autoDetectLiteLLM,
|
|
33
|
+
readDiscoveryEndpoints,
|
|
34
|
+
type DiscoveryEndpoints,
|
|
35
|
+
} from '../utils/litellm-api'
|
|
31
36
|
import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
|
|
32
37
|
import { sameProxy } from '../utils/sso'
|
|
33
38
|
import { parseModelCapabilities } from '../utils/model-capabilities'
|
|
@@ -112,6 +117,7 @@ interface ProviderSource {
|
|
|
112
117
|
formatModelNames: boolean
|
|
113
118
|
modelNameTemplate?: string
|
|
114
119
|
providerName: string
|
|
120
|
+
endpoints: DiscoveryEndpoints
|
|
115
121
|
cacheKey: string
|
|
116
122
|
models: Model.Info[]
|
|
117
123
|
/**
|
|
@@ -472,6 +478,9 @@ function runtimeSettings(source: ProviderSource): Record<string, unknown> {
|
|
|
472
478
|
'modelCompatibility',
|
|
473
479
|
'formatModelNames',
|
|
474
480
|
'modelNameTemplate',
|
|
481
|
+
'managementBaseURL',
|
|
482
|
+
'modelsEndpoint',
|
|
483
|
+
'modelInfoEndpoint',
|
|
475
484
|
'providerID',
|
|
476
485
|
]) {
|
|
477
486
|
delete settings[key]
|
|
@@ -601,6 +610,7 @@ async function makeProviderSource(
|
|
|
601
610
|
// distinguishable.
|
|
602
611
|
const providerName = provider?.name || id
|
|
603
612
|
const naming = { formatModelNames, modelNameTemplate, providerId: id, providerName }
|
|
613
|
+
const endpoints = readDiscoveryEndpoints(settings)
|
|
604
614
|
// Explicit config beats the URL typed into the dialog, which beats the
|
|
605
615
|
// environment and local port probing.
|
|
606
616
|
const detectedBaseURL = configuredBaseURL
|
|
@@ -624,7 +634,7 @@ async function makeProviderSource(
|
|
|
624
634
|
// the raw key): a credential change starts a fresh discovery instead of
|
|
625
635
|
// serving another credential's cached model list. A sign-in is keyed by its
|
|
626
636
|
// credential id, because its access token rotates on every refresh.
|
|
627
|
-
const cacheKey = buildCacheKey(id, baseURL, filters, capabilities, naming, (usesConnectionCredential ? connected?.credentialID : undefined) ?? apiKey)
|
|
637
|
+
const cacheKey = buildCacheKey(id, baseURL, filters, capabilities, naming, (usesConnectionCredential ? connected?.credentialID : undefined) ?? apiKey, endpoints)
|
|
628
638
|
const cacheEntry = readModelCacheEntry(cacheKey)
|
|
629
639
|
let entries = cacheEntry?.models ?? null
|
|
630
640
|
let responsesIds = cacheEntry?.responsesIds ?? []
|
|
@@ -640,6 +650,7 @@ async function makeProviderSource(
|
|
|
640
650
|
filters,
|
|
641
651
|
capabilities,
|
|
642
652
|
naming,
|
|
653
|
+
endpoints,
|
|
643
654
|
),
|
|
644
655
|
DISCOVERY_TIMEOUT_MS,
|
|
645
656
|
)
|
|
@@ -682,6 +693,7 @@ async function makeProviderSource(
|
|
|
682
693
|
formatModelNames,
|
|
683
694
|
modelNameTemplate,
|
|
684
695
|
providerName,
|
|
696
|
+
endpoints,
|
|
685
697
|
cacheKey,
|
|
686
698
|
models: toProviderModels(id, entries ?? {}, {
|
|
687
699
|
providerPackage,
|
|
@@ -749,6 +761,7 @@ async function refreshProviderSource(
|
|
|
749
761
|
providerId: source.id,
|
|
750
762
|
providerName: source.providerName,
|
|
751
763
|
},
|
|
764
|
+
source.endpoints,
|
|
752
765
|
),
|
|
753
766
|
DISCOVERY_TIMEOUT_MS,
|
|
754
767
|
)
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
LiteLLMModel,
|
|
3
|
+
LiteLLMModelInfo,
|
|
4
|
+
LiteLLMModelInfoEntry,
|
|
5
|
+
LiteLLMModelInfoResponse,
|
|
6
|
+
LiteLLMModelsResponse,
|
|
7
|
+
} from '../types'
|
|
2
8
|
import { CAPABILITY_FLAGS } from './model-capabilities'
|
|
3
9
|
import { resolveReasoningEfforts } from './reasoning-efforts'
|
|
4
10
|
|
|
@@ -40,6 +46,51 @@ export function normalizeBaseURL(baseURL: string = DEFAULT_LITELLM_URL): string
|
|
|
40
46
|
return normalized
|
|
41
47
|
}
|
|
42
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Optional overrides for where discovery looks. Each field is left unset when
|
|
51
|
+
* it matches the default, so default configs keep their cache identity.
|
|
52
|
+
*/
|
|
53
|
+
export interface DiscoveryEndpoints {
|
|
54
|
+
/** Host serving the model-info endpoint when it differs from the model host. */
|
|
55
|
+
managementBaseURL?: string
|
|
56
|
+
modelsEndpoint?: string
|
|
57
|
+
modelInfoEndpoint?: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readEndpointPath(raw: unknown, fallback: string): string | undefined {
|
|
61
|
+
if (typeof raw !== 'string' || raw.trim() === '') return undefined
|
|
62
|
+
const trimmed = raw.trim()
|
|
63
|
+
const path = trimmed.startsWith('/') ? trimmed : `/${trimmed}`
|
|
64
|
+
return path === fallback ? undefined : path
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function readNonEmpty(raw: unknown): string | undefined {
|
|
68
|
+
return typeof raw === 'string' && raw.trim() !== '' ? raw.trim() : undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Read `managementBaseURL`, `modelsEndpoint` and `modelInfoEndpoint` from a
|
|
73
|
+
* provider's options/settings, falling back to `LITELLM_MANAGEMENT_BASE_URL`,
|
|
74
|
+
* `LITELLM_MODELS_ENDPOINT` and `LITELLM_MODEL_INFO_ENDPOINT`.
|
|
75
|
+
*/
|
|
76
|
+
export function readDiscoveryEndpoints(options: Record<string, unknown>): DiscoveryEndpoints {
|
|
77
|
+
const management =
|
|
78
|
+
readNonEmpty(options.managementBaseURL) ?? readNonEmpty(process.env.LITELLM_MANAGEMENT_BASE_URL)
|
|
79
|
+
const models = readEndpointPath(
|
|
80
|
+
options.modelsEndpoint ?? process.env.LITELLM_MODELS_ENDPOINT,
|
|
81
|
+
MODELS_ENDPOINT,
|
|
82
|
+
)
|
|
83
|
+
const info = readEndpointPath(
|
|
84
|
+
options.modelInfoEndpoint ?? process.env.LITELLM_MODEL_INFO_ENDPOINT,
|
|
85
|
+
MODEL_INFO_ENDPOINT,
|
|
86
|
+
)
|
|
87
|
+
const endpoints: DiscoveryEndpoints = {}
|
|
88
|
+
if (management) endpoints.managementBaseURL = normalizeBaseURL(management)
|
|
89
|
+
if (models) endpoints.modelsEndpoint = models
|
|
90
|
+
if (info) endpoints.modelInfoEndpoint = info
|
|
91
|
+
return endpoints
|
|
92
|
+
}
|
|
93
|
+
|
|
43
94
|
/** Build a full URL for a given API endpoint. */
|
|
44
95
|
export function buildAPIURL(baseURL: string, endpoint: string = MODELS_ENDPOINT): string {
|
|
45
96
|
return `${normalizeBaseURL(baseURL)}${endpoint}`
|
|
@@ -64,20 +115,27 @@ export async function checkLiteLLMHealth(
|
|
|
64
115
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
65
116
|
apiKey?: string,
|
|
66
117
|
customHeaders?: Record<string, string>,
|
|
118
|
+
endpoints: DiscoveryEndpoints = {},
|
|
67
119
|
): Promise<boolean> {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
120
|
+
const ping = async (url: string): Promise<boolean> => {
|
|
121
|
+
try {
|
|
122
|
+
const response = await fetch(url, {
|
|
123
|
+
method: 'GET',
|
|
124
|
+
headers: buildHeaders(apiKey, customHeaders),
|
|
125
|
+
signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
|
|
126
|
+
})
|
|
127
|
+
// 401 still means a server is alive — we just don't have the right
|
|
128
|
+
// credentials. Surface that as "unhealthy" so the user is prompted
|
|
129
|
+
// to set LITELLM_API_KEY.
|
|
130
|
+
return response.ok
|
|
131
|
+
} catch {
|
|
132
|
+
return false
|
|
133
|
+
}
|
|
80
134
|
}
|
|
135
|
+
if (await ping(buildAPIURL(baseURL, endpoints.modelsEndpoint))) return true
|
|
136
|
+
// A proxy whose model host has no usable models endpoint can still be
|
|
137
|
+
// healthy on the management host.
|
|
138
|
+
return ping(infoURL(baseURL, endpoints))
|
|
81
139
|
}
|
|
82
140
|
|
|
83
141
|
/** Discover all models exposed by a LiteLLM proxy. */
|
|
@@ -85,8 +143,9 @@ export async function discoverLiteLLMModels(
|
|
|
85
143
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
86
144
|
apiKey?: string,
|
|
87
145
|
customHeaders?: Record<string, string>,
|
|
146
|
+
endpoints: DiscoveryEndpoints = {},
|
|
88
147
|
): Promise<LiteLLMModel[]> {
|
|
89
|
-
const url = buildAPIURL(baseURL)
|
|
148
|
+
const url = buildAPIURL(baseURL, endpoints.modelsEndpoint)
|
|
90
149
|
const response = await fetch(url, {
|
|
91
150
|
method: 'GET',
|
|
92
151
|
headers: buildHeaders(apiKey, customHeaders),
|
|
@@ -101,20 +160,21 @@ export async function discoverLiteLLMModels(
|
|
|
101
160
|
return data.data ?? []
|
|
102
161
|
}
|
|
103
162
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
163
|
+
function infoURL(baseURL: string, endpoints: DiscoveryEndpoints): string {
|
|
164
|
+
return buildAPIURL(
|
|
165
|
+
endpoints.managementBaseURL ?? baseURL,
|
|
166
|
+
endpoints.modelInfoEndpoint ?? MODEL_INFO_ENDPOINT,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Fetch the raw `/v1/model/info` entries from the management host (or `baseURL`). */
|
|
171
|
+
export async function fetchLiteLLMModelInfoEntries(
|
|
112
172
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
113
173
|
apiKey?: string,
|
|
114
174
|
customHeaders?: Record<string, string>,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const response = await fetch(
|
|
175
|
+
endpoints: DiscoveryEndpoints = {},
|
|
176
|
+
): Promise<LiteLLMModelInfoEntry[]> {
|
|
177
|
+
const response = await fetch(infoURL(baseURL, endpoints), {
|
|
118
178
|
method: 'GET',
|
|
119
179
|
headers: buildHeaders(apiKey, customHeaders),
|
|
120
180
|
signal: AbortSignal.timeout(getRequestTimeoutMs()),
|
|
@@ -125,8 +185,34 @@ export async function discoverLiteLLMModelInfo(
|
|
|
125
185
|
}
|
|
126
186
|
|
|
127
187
|
const data = (await response.json()) as LiteLLMModelInfoResponse
|
|
188
|
+
return data.data ?? []
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Build a model list from `/v1/model/info` entries, for proxies whose models
|
|
193
|
+
* endpoint is unusable. Duplicate `model_name`s (several deployments behind
|
|
194
|
+
* one name) collapse to the first entry.
|
|
195
|
+
*/
|
|
196
|
+
export function modelsFromInfoEntries(entries: LiteLLMModelInfoEntry[]): LiteLLMModel[] {
|
|
197
|
+
const models = new Map<string, LiteLLMModel>()
|
|
198
|
+
for (const entry of entries) {
|
|
199
|
+
if (!entry.model_name || models.has(entry.model_name)) continue
|
|
200
|
+
const info = entry.model_info
|
|
201
|
+
models.set(entry.model_name, {
|
|
202
|
+
id: entry.model_name,
|
|
203
|
+
object: 'model',
|
|
204
|
+
...(info?.mode !== undefined ? { mode: info.mode } : {}),
|
|
205
|
+
...(info?.max_input_tokens !== undefined ? { max_input_tokens: info.max_input_tokens } : {}),
|
|
206
|
+
...(info?.max_output_tokens !== undefined ? { max_output_tokens: info.max_output_tokens } : {}),
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
return [...models.values()]
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Index `/v1/model/info` entries by every alias LiteLLM may use for a model. */
|
|
213
|
+
export function buildModelInfoMap(entries: LiteLLMModelInfoEntry[]): Map<string, LiteLLMModelInfo> {
|
|
128
214
|
const infoByName = new Map<string, LiteLLMModelInfo>()
|
|
129
|
-
for (const entry of
|
|
215
|
+
for (const entry of entries) {
|
|
130
216
|
if (!entry.model_info) continue
|
|
131
217
|
// Some deployments set capability flags on the params block rather
|
|
132
218
|
// than inside model_info. Fill those gaps so enrichment sees them.
|
|
@@ -157,6 +243,22 @@ export async function discoverLiteLLMModelInfo(
|
|
|
157
243
|
return infoByName
|
|
158
244
|
}
|
|
159
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Fetch per-model metadata (`mode`, token limits, capability flags and the
|
|
248
|
+
* optional `display_name`) from `/v1/model/info`, keyed by model name.
|
|
249
|
+
* `/v1/models` omits these fields for database-defined models, so
|
|
250
|
+
* classification (e.g. filtering out embedding models) relies on this
|
|
251
|
+
* endpoint.
|
|
252
|
+
*/
|
|
253
|
+
export async function discoverLiteLLMModelInfo(
|
|
254
|
+
baseURL: string = DEFAULT_LITELLM_URL,
|
|
255
|
+
apiKey?: string,
|
|
256
|
+
customHeaders?: Record<string, string>,
|
|
257
|
+
endpoints: DiscoveryEndpoints = {},
|
|
258
|
+
): Promise<Map<string, LiteLLMModelInfo>> {
|
|
259
|
+
return buildModelInfoMap(await fetchLiteLLMModelInfoEntries(baseURL, apiKey, customHeaders, endpoints))
|
|
260
|
+
}
|
|
261
|
+
|
|
160
262
|
/**
|
|
161
263
|
* Use an explicit LITELLM_BASE_URL when set; otherwise try `ports` on localhost,
|
|
162
264
|
* by default the most common ones. The default `litellm --port` is 4000, with
|
package/src/utils/model-cache.ts
CHANGED
|
@@ -3,6 +3,7 @@ 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
|
import { normalizeModelDisplayName, type ModelNaming } from './format-model-name'
|
|
6
|
+
import type { DiscoveryEndpoints } from './litellm-api'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Facts about a discovered model's upstream that OpenCode 2 turns into
|
|
@@ -116,6 +117,9 @@ function canonicalize(value: unknown): string {
|
|
|
116
117
|
* `providerName` only matters when a template is set: `{provider_name}`
|
|
117
118
|
* resolves to it, so renaming a provider must start a fresh discovery too.
|
|
118
119
|
* The provider id is already part of the base key.
|
|
120
|
+
*
|
|
121
|
+
* A custom management host or endpoint path changes where model info comes
|
|
122
|
+
* from, so it joins the fingerprint too; defaults add nothing.
|
|
119
123
|
*/
|
|
120
124
|
export function buildCacheKey(
|
|
121
125
|
providerId: string,
|
|
@@ -124,6 +128,7 @@ export function buildCacheKey(
|
|
|
124
128
|
capabilities: Record<string, Record<string, boolean>>,
|
|
125
129
|
naming: ModelNaming = {},
|
|
126
130
|
credential?: string,
|
|
131
|
+
endpoints: DiscoveryEndpoints = {},
|
|
127
132
|
): string {
|
|
128
133
|
let base = `${providerId}@${baseURL}`
|
|
129
134
|
// The credential fingerprint: a short hash (never the raw key) so a
|
|
@@ -135,7 +140,9 @@ export function buildCacheKey(
|
|
|
135
140
|
}
|
|
136
141
|
const rawNames = naming.formatModelNames === false
|
|
137
142
|
const template = naming.modelNameTemplate
|
|
143
|
+
const hasEndpoints = Object.keys(endpoints).length > 0
|
|
138
144
|
const hasAdjustments =
|
|
145
|
+
hasEndpoints ||
|
|
139
146
|
(filters.includeModels?.length ?? 0) > 0 ||
|
|
140
147
|
(filters.excludeModels?.length ?? 0) > 0 ||
|
|
141
148
|
Object.keys(capabilities).length > 0 ||
|
|
@@ -159,6 +166,7 @@ export function buildCacheKey(
|
|
|
159
166
|
excludeModels: filters.excludeModels ? [...filters.excludeModels].sort() : undefined,
|
|
160
167
|
capabilities,
|
|
161
168
|
...namingFingerprint,
|
|
169
|
+
...(hasEndpoints ? { endpoints } : {}),
|
|
162
170
|
}),
|
|
163
171
|
)
|
|
164
172
|
.digest('hex')
|