opencode-plugin-litellm 1.12.0 → 1.12.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 +1 -0
- package/package.json +1 -1
- package/src/plugin/index.ts +19 -2
- package/src/plugin/integration.ts +7 -0
- package/src/plugin/v2.ts +20 -1
- package/src/utils/litellm-api.ts +59 -11
package/README.md
CHANGED
|
@@ -436,6 +436,7 @@ Some deployments serve the OpenAI-compatible API and the management API (`/v1/mo
|
|
|
436
436
|
|
|
437
437
|
- If the models endpoint fails but the info endpoint answers, the model list is built from the info response.
|
|
438
438
|
- The health check falls back to the info endpoint on the management host.
|
|
439
|
+
- A LiteLLM **sign-in** (SSO/OIDC) token is only sent to the origin that issued it. When `managementBaseURL` is on a different origin than `baseURL`, the info request goes out without the token: the model list still arrives, but capability metadata from that host does not. An API key (`apiKey` / `LITELLM_API_KEY`) is the operator's own and is still sent to the management host.
|
|
439
440
|
- Changing any of these starts a fresh discovery on the next start.
|
|
440
441
|
|
|
441
442
|
### Splitting one proxy into multiple providers (`includeModels` / `excludeModels`)
|
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.12.
|
|
4
|
+
"version": "1.12.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
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
modelsFromInfoEntries,
|
|
10
10
|
normalizeBaseURL,
|
|
11
11
|
readDiscoveryEndpoints,
|
|
12
|
+
sameOriginURL,
|
|
12
13
|
type DiscoveryEndpoints,
|
|
13
14
|
} from '../utils/litellm-api'
|
|
14
15
|
import {
|
|
@@ -461,8 +462,9 @@ export async function discoverModels(
|
|
|
461
462
|
capabilities: ModelCapabilities = {},
|
|
462
463
|
naming: ModelNaming = {},
|
|
463
464
|
endpoints: DiscoveryEndpoints = {},
|
|
465
|
+
signInCredential = false,
|
|
464
466
|
): Promise<DiscoveredModels | null> {
|
|
465
|
-
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders, endpoints))) {
|
|
467
|
+
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders, endpoints, signInCredential))) {
|
|
466
468
|
log(
|
|
467
469
|
'warn',
|
|
468
470
|
`[opencode-litellm] LiteLLM appears offline or unauthorized for provider "${providerId}" at ${baseURL}`,
|
|
@@ -474,9 +476,22 @@ export async function discoverModels(
|
|
|
474
476
|
// database-defined models, so fetch `/v1/model/info` alongside
|
|
475
477
|
// it. The info call is best-effort: without it, classification
|
|
476
478
|
// falls back to id heuristics.
|
|
479
|
+
// A sign-in's token only goes to the proxy that issued it, so a management
|
|
480
|
+
// host on another origin gets the request without it and answers 401/404:
|
|
481
|
+
// the model list still arrives, without the info enrichment.
|
|
482
|
+
if (
|
|
483
|
+
signInCredential &&
|
|
484
|
+
endpoints.managementBaseURL &&
|
|
485
|
+
!sameOriginURL(endpoints.managementBaseURL, baseURL)
|
|
486
|
+
) {
|
|
487
|
+
log(
|
|
488
|
+
'warn',
|
|
489
|
+
`[opencode-litellm] Not sending the LiteLLM sign-in to the management host ${endpoints.managementBaseURL} for provider "${providerId}": a sign-in token only goes to the proxy that issued it. Model metadata from that host is unavailable.`,
|
|
490
|
+
)
|
|
491
|
+
}
|
|
477
492
|
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
478
493
|
discoverLiteLLMModels(baseURL, apiKey, customHeaders, endpoints),
|
|
479
|
-
fetchLiteLLMModelInfoEntries(baseURL, apiKey, customHeaders, endpoints),
|
|
494
|
+
fetchLiteLLMModelInfoEntries(baseURL, apiKey, customHeaders, endpoints, signInCredential),
|
|
480
495
|
])
|
|
481
496
|
|
|
482
497
|
let discovered: LiteLLMModel[]
|
|
@@ -869,6 +884,7 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
869
884
|
providerName: ctx.providerName,
|
|
870
885
|
},
|
|
871
886
|
ctx.endpoints,
|
|
887
|
+
ctx.session !== undefined,
|
|
872
888
|
),
|
|
873
889
|
DISCOVERY_TIMEOUT_MS,
|
|
874
890
|
)
|
|
@@ -1204,6 +1220,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
1204
1220
|
capabilities,
|
|
1205
1221
|
{ formatModelNames, modelNameTemplate, providerId, providerName },
|
|
1206
1222
|
endpoints,
|
|
1223
|
+
Boolean(sso),
|
|
1207
1224
|
),
|
|
1208
1225
|
DISCOVERY_TIMEOUT_MS,
|
|
1209
1226
|
)
|
|
@@ -45,6 +45,12 @@ export interface ConnectedLiteLLM {
|
|
|
45
45
|
providerID: string | undefined
|
|
46
46
|
/** Id of an OAuth credential, which stays put while its access token rotates. */
|
|
47
47
|
credentialID?: string
|
|
48
|
+
/**
|
|
49
|
+
* `apiKey` is an SSO/OIDC access token. Such a token is bound to the proxy
|
|
50
|
+
* that issued it, so callers must not send it to another origin. A key saved
|
|
51
|
+
* in the dialog is the operator's own and carries no such restriction.
|
|
52
|
+
*/
|
|
53
|
+
signIn: boolean
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
/** The configured proxy URL and gateway headers a sign-in for a provider id uses. */
|
|
@@ -325,6 +331,7 @@ export async function readConnectedConfig(
|
|
|
325
331
|
providerID: readAnswer(answers, 'providerID'),
|
|
326
332
|
credentialID:
|
|
327
333
|
credential.type === 'oauth' && active.type === 'credential' ? active.id : undefined,
|
|
334
|
+
signIn: credential.type === 'oauth',
|
|
328
335
|
}
|
|
329
336
|
} catch {
|
|
330
337
|
return undefined
|
package/src/plugin/v2.ts
CHANGED
|
@@ -89,6 +89,8 @@ interface ProviderSource {
|
|
|
89
89
|
headers: Record<string, string>
|
|
90
90
|
integrationID?: string
|
|
91
91
|
usesConnectionCredential: boolean
|
|
92
|
+
/** `apiKey` is a LiteLLM sign-in token, which only goes to the proxy that issued it. */
|
|
93
|
+
signInCredential: boolean
|
|
92
94
|
baseURL: string
|
|
93
95
|
apiKey?: string
|
|
94
96
|
customHeaders?: Record<string, string>
|
|
@@ -596,6 +598,9 @@ async function makeProviderSource(
|
|
|
596
598
|
// fallback.
|
|
597
599
|
const storedKey = await getOpenCodeStoredApiKey(id)
|
|
598
600
|
const usesConnectionCredential = settingsApiKey === undefined && connectedApiKey !== undefined
|
|
601
|
+
// Only an OAuth connection is a sign-in; a key saved through `/connect` is the
|
|
602
|
+
// operator's own and may go wherever they pointed the management host.
|
|
603
|
+
const signInCredential = usesConnectionCredential && connected?.signIn === true
|
|
599
604
|
const integrationID =
|
|
600
605
|
provider?.integrationID ??
|
|
601
606
|
(usesConnectionCredential ? connectedIntegrationID : undefined)
|
|
@@ -651,6 +656,7 @@ async function makeProviderSource(
|
|
|
651
656
|
capabilities,
|
|
652
657
|
naming,
|
|
653
658
|
endpoints,
|
|
659
|
+
signInCredential,
|
|
654
660
|
),
|
|
655
661
|
DISCOVERY_TIMEOUT_MS,
|
|
656
662
|
)
|
|
@@ -680,6 +686,7 @@ async function makeProviderSource(
|
|
|
680
686
|
headers: customHeaders,
|
|
681
687
|
integrationID,
|
|
682
688
|
usesConnectionCredential,
|
|
689
|
+
signInCredential,
|
|
683
690
|
credentialReloadRequired: false,
|
|
684
691
|
refreshRequired: false,
|
|
685
692
|
fromFallback: provider === undefined,
|
|
@@ -762,6 +769,7 @@ async function refreshProviderSource(
|
|
|
762
769
|
providerName: source.providerName,
|
|
763
770
|
},
|
|
764
771
|
source.endpoints,
|
|
772
|
+
source.signInCredential,
|
|
765
773
|
),
|
|
766
774
|
DISCOVERY_TIMEOUT_MS,
|
|
767
775
|
)
|
|
@@ -848,11 +856,14 @@ async function refreshConnectionCredential(
|
|
|
848
856
|
const settingsApiKey = readString(source.settings.apiKey)
|
|
849
857
|
let nextKey: string | undefined
|
|
850
858
|
let nextUsesConnection: boolean
|
|
859
|
+
let nextSignIn: boolean
|
|
851
860
|
if (connectionKey !== undefined) {
|
|
852
861
|
// An explicit settings apiKey stays first; a connection credential takes
|
|
853
862
|
// over the environment/auto-detected key.
|
|
854
863
|
nextKey = settingsApiKey ?? connectionKey
|
|
855
864
|
nextUsesConnection = settingsApiKey === undefined
|
|
865
|
+
// A sign-in token is bound to the proxy that issued it; a key is not.
|
|
866
|
+
nextSignIn = nextUsesConnection && credential?.type === 'oauth'
|
|
856
867
|
} else {
|
|
857
868
|
// The connection credential is gone (the connection was removed or its
|
|
858
869
|
// credential cleared): fall back through the stored credential / env key
|
|
@@ -863,10 +874,18 @@ async function refreshConnectionCredential(
|
|
|
863
874
|
const envKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
864
875
|
nextKey = settingsApiKey ?? storedKey ?? envKey
|
|
865
876
|
nextUsesConnection = false
|
|
877
|
+
nextSignIn = false
|
|
878
|
+
}
|
|
879
|
+
if (
|
|
880
|
+
nextKey === source.apiKey &&
|
|
881
|
+
nextUsesConnection === source.usesConnectionCredential &&
|
|
882
|
+
nextSignIn === source.signInCredential
|
|
883
|
+
) {
|
|
884
|
+
return false
|
|
866
885
|
}
|
|
867
|
-
if (nextKey === source.apiKey && nextUsesConnection === source.usesConnectionCredential) return false
|
|
868
886
|
source.apiKey = nextKey
|
|
869
887
|
source.usesConnectionCredential = nextUsesConnection
|
|
888
|
+
source.signInCredential = nextSignIn
|
|
870
889
|
source.credentialReloadRequired = true
|
|
871
890
|
return true
|
|
872
891
|
} catch (error) {
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -116,26 +116,41 @@ export async function checkLiteLLMHealth(
|
|
|
116
116
|
apiKey?: string,
|
|
117
117
|
customHeaders?: Record<string, string>,
|
|
118
118
|
endpoints: DiscoveryEndpoints = {},
|
|
119
|
+
signInCredential = false,
|
|
119
120
|
): Promise<boolean> {
|
|
120
|
-
const ping = async (url: string): Promise<boolean> => {
|
|
121
|
+
const ping = async (url: string): Promise<{ ok: boolean; status?: number }> => {
|
|
121
122
|
try {
|
|
122
123
|
const response = await fetch(url, {
|
|
123
124
|
method: 'GET',
|
|
124
|
-
headers: buildHeaders(
|
|
125
|
+
headers: buildHeaders(
|
|
126
|
+
credentialForTarget(url, baseURL, apiKey, signInCredential),
|
|
127
|
+
customHeaders,
|
|
128
|
+
),
|
|
125
129
|
signal: AbortSignal.timeout(HEALTH_TIMEOUT_MS),
|
|
126
130
|
})
|
|
127
131
|
// 401 still means a server is alive — we just don't have the right
|
|
128
132
|
// credentials. Surface that as "unhealthy" so the user is prompted
|
|
129
133
|
// to set LITELLM_API_KEY.
|
|
130
|
-
return response.ok
|
|
134
|
+
return { ok: response.ok, status: response.status }
|
|
131
135
|
} catch {
|
|
132
|
-
return false
|
|
136
|
+
return { ok: false }
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
+
const modelsURL = buildAPIURL(baseURL, endpoints.modelsEndpoint)
|
|
140
|
+
const models = await ping(modelsURL)
|
|
141
|
+
if (models.ok) return true
|
|
142
|
+
const fallbackURL = infoURL(baseURL, endpoints)
|
|
143
|
+
// The info endpoint only deserves a second request when the server answered
|
|
144
|
+
// the models probe but does not serve that path, or when a management host is
|
|
145
|
+
// configured. A network error or a rejected credential fails there too, and
|
|
146
|
+
// retrying would double the health budget (3 s per probe) on the offline path
|
|
147
|
+
// that startup and auto-detection walk.
|
|
148
|
+
const pathMissing =
|
|
149
|
+
models.status === 404 || models.status === 405 || models.status === 501
|
|
150
|
+
if (fallbackURL === modelsURL || (!pathMissing && !endpoints.managementBaseURL)) {
|
|
151
|
+
return false
|
|
152
|
+
}
|
|
153
|
+
return (await ping(fallbackURL)).ok
|
|
139
154
|
}
|
|
140
155
|
|
|
141
156
|
/** Discover all models exposed by a LiteLLM proxy. */
|
|
@@ -167,16 +182,46 @@ function infoURL(baseURL: string, endpoints: DiscoveryEndpoints): string {
|
|
|
167
182
|
)
|
|
168
183
|
}
|
|
169
184
|
|
|
185
|
+
/** Whether two URLs name the same scheme, host and port. */
|
|
186
|
+
export function sameOriginURL(a: string, b: string): boolean {
|
|
187
|
+
try {
|
|
188
|
+
return new URL(a).origin === new URL(b).origin
|
|
189
|
+
} catch {
|
|
190
|
+
return false
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The credential to attach to `url`. A LiteLLM sign-in's access token is
|
|
196
|
+
* bound to the proxy that issued it, so it is withheld from a management host
|
|
197
|
+
* on another origin rather than handed to a server that never issued it. An
|
|
198
|
+
* API key is the operator's own and keeps going wherever they pointed it.
|
|
199
|
+
*/
|
|
200
|
+
function credentialForTarget(
|
|
201
|
+
url: string,
|
|
202
|
+
baseURL: string,
|
|
203
|
+
apiKey: string | undefined,
|
|
204
|
+
signInCredential: boolean,
|
|
205
|
+
): string | undefined {
|
|
206
|
+
if (!signInCredential) return apiKey
|
|
207
|
+
return sameOriginURL(url, normalizeBaseURL(baseURL)) ? apiKey : undefined
|
|
208
|
+
}
|
|
209
|
+
|
|
170
210
|
/** Fetch the raw `/v1/model/info` entries from the management host (or `baseURL`). */
|
|
171
211
|
export async function fetchLiteLLMModelInfoEntries(
|
|
172
212
|
baseURL: string = DEFAULT_LITELLM_URL,
|
|
173
213
|
apiKey?: string,
|
|
174
214
|
customHeaders?: Record<string, string>,
|
|
175
215
|
endpoints: DiscoveryEndpoints = {},
|
|
216
|
+
signInCredential = false,
|
|
176
217
|
): Promise<LiteLLMModelInfoEntry[]> {
|
|
177
|
-
const
|
|
218
|
+
const url = infoURL(baseURL, endpoints)
|
|
219
|
+
const response = await fetch(url, {
|
|
178
220
|
method: 'GET',
|
|
179
|
-
headers: buildHeaders(
|
|
221
|
+
headers: buildHeaders(
|
|
222
|
+
credentialForTarget(url, baseURL, apiKey, signInCredential),
|
|
223
|
+
customHeaders,
|
|
224
|
+
),
|
|
180
225
|
signal: AbortSignal.timeout(getRequestTimeoutMs()),
|
|
181
226
|
})
|
|
182
227
|
|
|
@@ -255,8 +300,11 @@ export async function discoverLiteLLMModelInfo(
|
|
|
255
300
|
apiKey?: string,
|
|
256
301
|
customHeaders?: Record<string, string>,
|
|
257
302
|
endpoints: DiscoveryEndpoints = {},
|
|
303
|
+
signInCredential = false,
|
|
258
304
|
): Promise<Map<string, LiteLLMModelInfo>> {
|
|
259
|
-
return buildModelInfoMap(
|
|
305
|
+
return buildModelInfoMap(
|
|
306
|
+
await fetchLiteLLMModelInfoEntries(baseURL, apiKey, customHeaders, endpoints, signInCredential),
|
|
307
|
+
)
|
|
260
308
|
}
|
|
261
309
|
|
|
262
310
|
/**
|