opencode-plugin-litellm 0.6.0 → 0.7.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/package.json +1 -1
- package/src/plugin/index.ts +182 -144
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": "0.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"description": "OpenCode 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
|
@@ -25,6 +25,23 @@ const DISCOVERY_TIMEOUT_MS = 20000
|
|
|
25
25
|
*/
|
|
26
26
|
const injectedModelIds = new Map<string, Set<string>>()
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Helper to determine if a provider ID or its configured options indicate
|
|
30
|
+
* compatibility with LiteLLM.
|
|
31
|
+
*/
|
|
32
|
+
function isLiteLLMProvider(
|
|
33
|
+
providerId: string,
|
|
34
|
+
options: Record<string, unknown>,
|
|
35
|
+
): boolean {
|
|
36
|
+
if (providerId === CHAT_PROVIDER_ID) return true
|
|
37
|
+
if (providerId.startsWith('litellm-') || providerId.startsWith('litellm_')) return true
|
|
38
|
+
if (options.litellm === true) return true
|
|
39
|
+
if (options.litellmCompatible === true) return true
|
|
40
|
+
if (options['litellm-compatible'] === true) return true
|
|
41
|
+
if (options.litellm_compatible === true) return true
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
|
|
28
45
|
/**
|
|
29
46
|
* Read `customHeaders` from a provider options block.
|
|
30
47
|
*/
|
|
@@ -133,178 +150,199 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
133
150
|
// Ensure the provider entry exists
|
|
134
151
|
if (!config.provider) config.provider = {}
|
|
135
152
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const options = (existing?.options ?? {}) as Record<string, unknown>
|
|
140
|
-
const configuredBase =
|
|
141
|
-
typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
142
|
-
const configuredKey =
|
|
143
|
-
typeof options.apiKey === 'string' && options.apiKey
|
|
144
|
-
? options.apiKey
|
|
145
|
-
: undefined
|
|
146
|
-
const envKey =
|
|
147
|
-
process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
148
|
-
const apiKey = configuredKey ?? envKey
|
|
149
|
-
const customHeaders = readCustomHeaders(options)
|
|
150
|
-
|
|
151
|
-
// Resolve base URL
|
|
152
|
-
let baseURL: string | null = null
|
|
153
|
-
if (configuredBase) {
|
|
154
|
-
baseURL = normalizeBaseURL(configuredBase)
|
|
155
|
-
} else {
|
|
156
|
-
baseURL = await autoDetectLiteLLM(apiKey, customHeaders)
|
|
157
|
-
}
|
|
153
|
+
// Find all matching LiteLLM providers
|
|
154
|
+
const providerIds = Object.keys(config.provider)
|
|
155
|
+
const liteLLMProviders: Array<{ id: string; provider: Record<string, unknown> }> = []
|
|
158
156
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// Create provider entry if it doesn't exist
|
|
167
|
-
if (!existing) {
|
|
168
|
-
config.provider[CHAT_PROVIDER_ID] = {
|
|
169
|
-
npm: '@ai-sdk/openai-compatible',
|
|
170
|
-
name: 'LiteLLM (proxy)',
|
|
171
|
-
options: {
|
|
172
|
-
baseURL: `${baseURL}/v1`,
|
|
173
|
-
},
|
|
174
|
-
models: {},
|
|
157
|
+
for (const id of providerIds) {
|
|
158
|
+
const provider = config.provider[id]
|
|
159
|
+
if (provider && typeof provider === 'object') {
|
|
160
|
+
const options = (provider.options ?? {}) as Record<string, unknown>
|
|
161
|
+
if (isLiteLLMProvider(id, options)) {
|
|
162
|
+
liteLLMProviders.push({ id, provider })
|
|
163
|
+
}
|
|
175
164
|
}
|
|
176
165
|
}
|
|
177
166
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
// Ensure models map exists
|
|
194
|
-
if (!provider.models) {
|
|
195
|
-
provider.models = {}
|
|
167
|
+
// If no providers are matched (e.g., zero-config auto-detection),
|
|
168
|
+
// fall back to default 'litellm' provider to ensure backwards compatibility.
|
|
169
|
+
if (liteLLMProviders.length === 0) {
|
|
170
|
+
const id = CHAT_PROVIDER_ID
|
|
171
|
+
let provider = config.provider[id] as Record<string, unknown> | undefined
|
|
172
|
+
if (!provider) {
|
|
173
|
+
provider = {
|
|
174
|
+
npm: '@ai-sdk/openai-compatible',
|
|
175
|
+
name: 'LiteLLM (proxy)',
|
|
176
|
+
options: {},
|
|
177
|
+
models: {},
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
liteLLMProviders.push({ id, provider })
|
|
196
181
|
}
|
|
197
182
|
|
|
198
|
-
|
|
183
|
+
// Process each matched provider
|
|
184
|
+
for (const { id: providerId, provider } of liteLLMProviders) {
|
|
185
|
+
const options = (provider.options ?? {}) as Record<string, unknown>
|
|
186
|
+
const configuredBase =
|
|
187
|
+
typeof options.baseURL === 'string' ? options.baseURL : undefined
|
|
188
|
+
const configuredKey =
|
|
189
|
+
typeof options.apiKey === 'string' && options.apiKey
|
|
190
|
+
? options.apiKey
|
|
191
|
+
: undefined
|
|
192
|
+
const envKey =
|
|
193
|
+
process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
|
|
194
|
+
const apiKey = configuredKey ?? envKey
|
|
195
|
+
const customHeaders = readCustomHeaders(options)
|
|
199
196
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
) {
|
|
207
|
-
return
|
|
197
|
+
// Resolve base URL
|
|
198
|
+
let baseURL: string | null = null
|
|
199
|
+
if (configuredBase) {
|
|
200
|
+
baseURL = normalizeBaseURL(configuredBase)
|
|
201
|
+
} else {
|
|
202
|
+
baseURL = await autoDetectLiteLLM(apiKey, customHeaders)
|
|
208
203
|
}
|
|
209
204
|
|
|
210
|
-
if (!
|
|
205
|
+
if (!baseURL) {
|
|
211
206
|
console.warn(
|
|
212
|
-
`[opencode-litellm] LiteLLM
|
|
207
|
+
`[opencode-litellm] No LiteLLM proxy found for provider "${providerId}". Configure options.baseURL or start LiteLLM on port 4000/8000/8080.`,
|
|
213
208
|
)
|
|
214
|
-
|
|
209
|
+
continue
|
|
215
210
|
}
|
|
216
211
|
|
|
217
|
-
//
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const [
|
|
222
|
-
discoverLiteLLMModels(baseURL!, apiKey, customHeaders),
|
|
223
|
-
discoverLiteLLMModelInfo(baseURL!, apiKey, customHeaders),
|
|
224
|
-
])
|
|
212
|
+
// Initialize/Update the provider entry in config
|
|
213
|
+
if (!config.provider[providerId]) {
|
|
214
|
+
config.provider[providerId] = provider
|
|
215
|
+
}
|
|
216
|
+
const actualProvider = config.provider[providerId] as Record<string, unknown>
|
|
225
217
|
|
|
226
|
-
if (
|
|
227
|
-
|
|
228
|
-
console.warn(
|
|
229
|
-
'[opencode-litellm] Model discovery failed:',
|
|
230
|
-
error instanceof Error ? error.message : String(error),
|
|
231
|
-
)
|
|
232
|
-
return
|
|
218
|
+
if (!actualProvider.npm) {
|
|
219
|
+
actualProvider.npm = '@ai-sdk/openai-compatible'
|
|
233
220
|
}
|
|
234
221
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (infoResult.status === 'fulfilled') {
|
|
238
|
-
infoByName = infoResult.value
|
|
222
|
+
if (!actualProvider.options) {
|
|
223
|
+
actualProvider.options = { baseURL: `${baseURL}/v1` }
|
|
239
224
|
} else {
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
)
|
|
225
|
+
const actualOptions = actualProvider.options as Record<string, unknown>
|
|
226
|
+
if (!actualOptions.baseURL) {
|
|
227
|
+
actualOptions.baseURL = `${baseURL}/v1`
|
|
228
|
+
}
|
|
245
229
|
}
|
|
246
230
|
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
'[opencode-litellm] LiteLLM responded but exposed zero models.',
|
|
250
|
-
)
|
|
251
|
-
return
|
|
231
|
+
if (!actualProvider.models) {
|
|
232
|
+
actualProvider.models = {}
|
|
252
233
|
}
|
|
253
234
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
235
|
+
const models = actualProvider.models as Record<string, unknown>
|
|
236
|
+
|
|
237
|
+
// Discover models with timeout
|
|
238
|
+
const work = async () => {
|
|
239
|
+
const alreadyInjected = injectedModelIds.get(baseURL!)
|
|
240
|
+
if (
|
|
241
|
+
alreadyInjected &&
|
|
242
|
+
[...alreadyInjected].every((id) => models[id])
|
|
243
|
+
) {
|
|
244
|
+
return
|
|
264
245
|
}
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
skipped++
|
|
272
|
-
continue
|
|
246
|
+
|
|
247
|
+
if (!(await checkLiteLLMHealth(baseURL!, apiKey, customHeaders))) {
|
|
248
|
+
console.warn(
|
|
249
|
+
`[opencode-litellm] LiteLLM appears offline or unauthorized for provider "${providerId}" at ${baseURL}`,
|
|
250
|
+
)
|
|
251
|
+
return
|
|
273
252
|
}
|
|
274
|
-
models[model.id] = entry
|
|
275
|
-
added++
|
|
276
|
-
}
|
|
277
253
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
254
|
+
// `/v1/models` omits `mode` and capability metadata for
|
|
255
|
+
// database-defined models, so fetch `/v1/model/info` alongside
|
|
256
|
+
// it. The info call is best-effort: without it, classification
|
|
257
|
+
// falls back to id heuristics.
|
|
258
|
+
const [modelsResult, infoResult] = await Promise.allSettled([
|
|
259
|
+
discoverLiteLLMModels(baseURL!, apiKey, customHeaders),
|
|
260
|
+
discoverLiteLLMModelInfo(baseURL!, apiKey, customHeaders),
|
|
261
|
+
])
|
|
285
262
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
263
|
+
if (modelsResult.status === 'rejected') {
|
|
264
|
+
const error = modelsResult.reason
|
|
265
|
+
console.warn(
|
|
266
|
+
`[opencode-litellm] Model discovery failed for provider "${providerId}":`,
|
|
267
|
+
error instanceof Error ? error.message : String(error),
|
|
268
|
+
)
|
|
269
|
+
return
|
|
270
|
+
}
|
|
290
271
|
|
|
291
|
-
|
|
272
|
+
const discovered = modelsResult.value
|
|
273
|
+
let infoByName: Map<string, LiteLLMModelInfo> | null = null
|
|
274
|
+
if (infoResult.status === 'fulfilled') {
|
|
275
|
+
infoByName = infoResult.value
|
|
276
|
+
} else {
|
|
277
|
+
const reason = infoResult.reason
|
|
278
|
+
console.warn(
|
|
279
|
+
`[opencode-litellm] /v1/model/info unavailable for provider "${providerId}"; non-chat model filtering will use id heuristics only:`,
|
|
280
|
+
reason instanceof Error ? reason.message : String(reason),
|
|
281
|
+
)
|
|
282
|
+
}
|
|
292
283
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
)
|
|
300
|
-
}
|
|
284
|
+
if (discovered.length === 0) {
|
|
285
|
+
console.warn(
|
|
286
|
+
`[opencode-litellm] LiteLLM responded for provider "${providerId}" but exposed zero models.`,
|
|
287
|
+
)
|
|
288
|
+
return
|
|
289
|
+
}
|
|
301
290
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
291
|
+
let added = 0
|
|
292
|
+
let skipped = 0
|
|
293
|
+
let wildcards = 0
|
|
294
|
+
const unmatched: string[] = []
|
|
295
|
+
for (const model of discovered) {
|
|
296
|
+
// Wildcard entries (`deepseek/*`) are access rules, not
|
|
297
|
+
// callable models — invoking one sends a literal `*` upstream.
|
|
298
|
+
if (model.id.includes('*')) {
|
|
299
|
+
wildcards++
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
// Don't overwrite user-curated entries
|
|
303
|
+
if (models[model.id]) continue
|
|
304
|
+
const info = infoByName?.get(model.id)
|
|
305
|
+
if (infoByName && !info) unmatched.push(model.id)
|
|
306
|
+
const entry = toConfigModel(info ? enrichModel(model, info) : model)
|
|
307
|
+
if (!entry) {
|
|
308
|
+
skipped++
|
|
309
|
+
continue
|
|
310
|
+
}
|
|
311
|
+
models[model.id] = entry
|
|
312
|
+
added++
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (unmatched.length > 0) {
|
|
316
|
+
console.warn(
|
|
317
|
+
`[opencode-litellm] /v1/model/info has no entry for ${unmatched.length} model(s) on provider "${providerId}"; ` +
|
|
318
|
+
`classification uses id heuristics for: ${unmatched.slice(0, 5).join(', ')}` +
|
|
319
|
+
(unmatched.length > 5 ? `, +${unmatched.length - 5} more` : ''),
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Remove the seed placeholder if real models were discovered
|
|
324
|
+
if (models['_'] && Object.keys(models).length > 1) {
|
|
325
|
+
delete models['_']
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
injectedModelIds.set(baseURL!, new Set(Object.keys(models)))
|
|
329
|
+
|
|
330
|
+
console.log(
|
|
331
|
+
`[opencode-litellm] Discovered ${discovered.length} models for provider "${providerId}" from ${baseURL} ` +
|
|
332
|
+
`(${added} added` +
|
|
333
|
+
(skipped > 0 ? `, ${skipped} non-chat hidden` : '') +
|
|
334
|
+
(wildcards > 0 ? `, ${wildcards} wildcard ignored` : '') +
|
|
335
|
+
')',
|
|
336
|
+
)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
await Promise.race([
|
|
340
|
+
work(),
|
|
341
|
+
new Promise<void>((resolve) =>
|
|
342
|
+
setTimeout(resolve, DISCOVERY_TIMEOUT_MS),
|
|
343
|
+
),
|
|
344
|
+
])
|
|
345
|
+
}
|
|
308
346
|
},
|
|
309
347
|
}
|
|
310
348
|
}
|