opencode-plugin-litellm 1.2.0 → 1.4.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 +151 -89
- package/package.json +3 -2
- package/src/index.ts +1 -0
- package/src/plugin/index.ts +71 -28
- package/src/plugin/v2.ts +540 -0
- package/src/utils/litellm-api.ts +5 -3
- package/src/utils/model-cache.ts +15 -9
package/src/plugin/index.ts
CHANGED
|
@@ -28,17 +28,30 @@ const CHAT_PROVIDER_ID = 'litellm'
|
|
|
28
28
|
// Covers the 3 s health check plus the parallel models/model-info fetch
|
|
29
29
|
// phase, with headroom. Scales with LITELLM_REQUEST_TIMEOUT_MS so slow
|
|
30
30
|
// proxies aren't cut off by the overall cap either (issue #20).
|
|
31
|
-
const DISCOVERY_TIMEOUT_MS = Math.max(20000, getRequestTimeoutMs() + 5000)
|
|
31
|
+
export const DISCOVERY_TIMEOUT_MS = Math.max(20000, getRequestTimeoutMs() + 5000)
|
|
32
32
|
// Don't revalidate a baseURL's cache more often than this, so a burst
|
|
33
33
|
// of `session.created` events can't generate repeated discovery traffic.
|
|
34
34
|
const REFRESH_MIN_INTERVAL_MS = 5 * 60 * 1000 // 5 minutes
|
|
35
35
|
|
|
36
36
|
type LogLevel = 'info' | 'warn' | 'error' | 'debug'
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
type LogDetails = {
|
|
39
|
+
service: string
|
|
40
|
+
level: LogLevel
|
|
41
|
+
message: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let logWriter: ((details: LogDetails) => void) | null = null
|
|
39
45
|
|
|
40
46
|
function initLogging(client: PluginInput['client']): void {
|
|
41
|
-
|
|
47
|
+
logWriter = (details) => {
|
|
48
|
+
void client.app.log({ body: details }).catch(() => {})
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** OpenCode 2 does not expose a server log method through its plugin context. */
|
|
53
|
+
export function initV2Logging(): void {
|
|
54
|
+
logWriter = null
|
|
42
55
|
}
|
|
43
56
|
|
|
44
57
|
/**
|
|
@@ -48,10 +61,8 @@ function initLogging(client: PluginInput['client']): void {
|
|
|
48
61
|
* (issue #15); client.app.log lands in OpenCode's log files instead.
|
|
49
62
|
*/
|
|
50
63
|
function log(level: LogLevel, message: string): void {
|
|
51
|
-
if (
|
|
52
|
-
|
|
53
|
-
.log({ body: { service: 'opencode-litellm', level, message } })
|
|
54
|
-
.catch(() => {})
|
|
64
|
+
if (logWriter) {
|
|
65
|
+
logWriter({ service: 'opencode-litellm', level, message })
|
|
55
66
|
return
|
|
56
67
|
}
|
|
57
68
|
// No client wired up (unit tests, unusual embedders) — fall back to
|
|
@@ -81,6 +92,7 @@ interface RefreshContext {
|
|
|
81
92
|
customHeaders?: Record<string, string>
|
|
82
93
|
filters: ModelFilters
|
|
83
94
|
capabilities: ModelCapabilities
|
|
95
|
+
formatModelNames: boolean
|
|
84
96
|
providerId: string
|
|
85
97
|
}
|
|
86
98
|
const refreshContexts = new Map<string, RefreshContext>()
|
|
@@ -93,7 +105,7 @@ const refreshInFlight = new Set<string>()
|
|
|
93
105
|
* wins. Clears the timer either way so a resolved discovery can't keep
|
|
94
106
|
* a short-lived process alive waiting on a pending `setTimeout`.
|
|
95
107
|
*/
|
|
96
|
-
function withTimeout<T>(
|
|
108
|
+
export function withTimeout<T>(
|
|
97
109
|
promise: Promise<T>,
|
|
98
110
|
timeoutMs: number,
|
|
99
111
|
): Promise<T | null> {
|
|
@@ -108,7 +120,7 @@ function withTimeout<T>(
|
|
|
108
120
|
* Helper to determine if a provider ID or its configured options indicate
|
|
109
121
|
* compatibility with LiteLLM.
|
|
110
122
|
*/
|
|
111
|
-
function isLiteLLMProvider(
|
|
123
|
+
export function isLiteLLMProvider(
|
|
112
124
|
providerId: string,
|
|
113
125
|
options: Record<string, unknown>,
|
|
114
126
|
): boolean {
|
|
@@ -124,7 +136,7 @@ function isLiteLLMProvider(
|
|
|
124
136
|
/**
|
|
125
137
|
* Read `customHeaders` from a provider options block.
|
|
126
138
|
*/
|
|
127
|
-
function readCustomHeaders(
|
|
139
|
+
export function readCustomHeaders(
|
|
128
140
|
options: Record<string, unknown>,
|
|
129
141
|
): Record<string, string> | undefined {
|
|
130
142
|
const raw = options.customHeaders
|
|
@@ -144,7 +156,7 @@ function readCustomHeaders(
|
|
|
144
156
|
* several OpenCode providers). Non-string entries are dropped; an empty
|
|
145
157
|
* result means "don't filter".
|
|
146
158
|
*/
|
|
147
|
-
function readModelFilters(options: Record<string, unknown>): ModelFilters {
|
|
159
|
+
export function readModelFilters(options: Record<string, unknown>): ModelFilters {
|
|
148
160
|
const readPatterns = (raw: unknown): string[] | undefined => {
|
|
149
161
|
if (!Array.isArray(raw)) return undefined
|
|
150
162
|
const out = raw.filter((v): v is string => typeof v === 'string')
|
|
@@ -156,6 +168,10 @@ function readModelFilters(options: Record<string, unknown>): ModelFilters {
|
|
|
156
168
|
}
|
|
157
169
|
}
|
|
158
170
|
|
|
171
|
+
export function readFormatModelNames(options: Record<string, unknown>): boolean {
|
|
172
|
+
return options.formatModelNames !== false
|
|
173
|
+
}
|
|
174
|
+
|
|
159
175
|
/**
|
|
160
176
|
* Overlay metadata onto a `/v1/models` entry in three tiers: the entry's
|
|
161
177
|
* own fields win, `/v1/model/info` fills gaps (notably `mode`, which
|
|
@@ -207,17 +223,21 @@ const USD_PER_TOKEN_TO_PER_MILLION = 1_000_000
|
|
|
207
223
|
* `opencode.json`). Returns `null` for non-chat models (embedding,
|
|
208
224
|
* image, audio) — they can't be used as primary chat models and would
|
|
209
225
|
* clutter the picker.
|
|
226
|
+
*
|
|
227
|
+
* Exported for tests: `formatModelNames` decides whether the display
|
|
228
|
+
* name is the prettified id or the raw `/v1/models` id verbatim.
|
|
210
229
|
*/
|
|
211
|
-
function toConfigModel(
|
|
230
|
+
export function toConfigModel(
|
|
212
231
|
model: LiteLLMModel,
|
|
213
232
|
info?: LiteLLMModelInfo,
|
|
233
|
+
formatModelNames = true,
|
|
214
234
|
): Record<string, unknown> | null {
|
|
215
235
|
const type = categorizeModel(model)
|
|
216
236
|
if (type === 'embedding' || type === 'image' || type === 'audio') {
|
|
217
237
|
return null
|
|
218
238
|
}
|
|
219
239
|
const entry: Record<string, unknown> = {
|
|
220
|
-
name: formatModelName(model),
|
|
240
|
+
name: formatModelNames ? formatModelName(model) : model.id,
|
|
221
241
|
}
|
|
222
242
|
// Some deployments only report the OpenAI-style `max_tokens` total;
|
|
223
243
|
// use it as the context limit when `max_input_tokens` is absent.
|
|
@@ -228,8 +248,8 @@ function toConfigModel(
|
|
|
228
248
|
output: model.max_output_tokens ?? 0,
|
|
229
249
|
}
|
|
230
250
|
}
|
|
231
|
-
if (model.supports_function_calling) {
|
|
232
|
-
entry.tool_call =
|
|
251
|
+
if (model.supports_function_calling != null) {
|
|
252
|
+
entry.tool_call = model.supports_function_calling
|
|
233
253
|
}
|
|
234
254
|
if (model.supports_reasoning) {
|
|
235
255
|
entry.reasoning = true
|
|
@@ -258,9 +278,13 @@ function toConfigModel(
|
|
|
258
278
|
if (model.supports_vision) input.push('image')
|
|
259
279
|
if (model.supports_pdf_input) input.push('pdf')
|
|
260
280
|
if (model.supports_audio_input) input.push('audio')
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
281
|
+
// LiteLLM often omits capability flags for database-defined models.
|
|
282
|
+
// Do not omit `modalities` in that case: OpenCode's fallback for an
|
|
283
|
+
// unknown custom model includes image input, which makes text-only
|
|
284
|
+
// llama.cpp routes receive image parts and fail with "image input is
|
|
285
|
+
// not supported". Text-only is the safe default until a proxy reports
|
|
286
|
+
// a positive capability.
|
|
287
|
+
entry.modalities = { input, output: ['text'] }
|
|
264
288
|
entry.variants = info?.supports_reasoning_efforts?.length
|
|
265
289
|
? Object.fromEntries(
|
|
266
290
|
info.supports_reasoning_efforts.map((effort) => [
|
|
@@ -277,21 +301,23 @@ function toConfigModel(
|
|
|
277
301
|
*
|
|
278
302
|
* Pure with respect to plugin config: it performs the network calls,
|
|
279
303
|
* classifies + formats each model, and returns a `{ id -> entry }` map.
|
|
280
|
-
* The provider's `includeModels`/`excludeModels` filters
|
|
281
|
-
* `modelCapabilities` overrides
|
|
282
|
-
*
|
|
283
|
-
* background refresh — writes the
|
|
304
|
+
* The provider's `includeModels`/`excludeModels` filters,
|
|
305
|
+
* `modelCapabilities` overrides and `formatModelNames` choice are
|
|
306
|
+
* applied here (not at merge time) so every path that persists or
|
|
307
|
+
* serves a cache — cold discovery and background refresh — writes the
|
|
308
|
+
* same adjusted view.
|
|
284
309
|
*
|
|
285
310
|
* Returns `null` when the proxy is unreachable/unauthorized or exposes
|
|
286
311
|
* no models, so callers can distinguish "no data" from "empty result".
|
|
287
312
|
*/
|
|
288
|
-
async function discoverModels(
|
|
313
|
+
export async function discoverModels(
|
|
289
314
|
baseURL: string,
|
|
290
315
|
apiKey: string | undefined,
|
|
291
316
|
customHeaders: Record<string, string> | undefined,
|
|
292
317
|
providerId: string,
|
|
293
318
|
filters: ModelFilters = {},
|
|
294
319
|
capabilities: ModelCapabilities = {},
|
|
320
|
+
formatModelNames = true,
|
|
295
321
|
): Promise<Record<string, unknown> | null> {
|
|
296
322
|
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
297
323
|
log(
|
|
@@ -363,7 +389,11 @@ async function discoverModels(
|
|
|
363
389
|
}
|
|
364
390
|
const info = infoByName?.get(model.id)
|
|
365
391
|
if (infoByName && !info) unmatched.push(model.id)
|
|
366
|
-
const entry = toConfigModel(
|
|
392
|
+
const entry = toConfigModel(
|
|
393
|
+
enrichModel(model, info, capabilities[model.id]),
|
|
394
|
+
info,
|
|
395
|
+
formatModelNames,
|
|
396
|
+
)
|
|
367
397
|
if (!entry) {
|
|
368
398
|
skipped++
|
|
369
399
|
continue
|
|
@@ -451,6 +481,7 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
451
481
|
ctx.providerId,
|
|
452
482
|
ctx.filters,
|
|
453
483
|
ctx.capabilities,
|
|
484
|
+
ctx.formatModelNames,
|
|
454
485
|
),
|
|
455
486
|
DISCOVERY_TIMEOUT_MS,
|
|
456
487
|
)
|
|
@@ -553,6 +584,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
553
584
|
const customHeaders = readCustomHeaders(options)
|
|
554
585
|
const filters = readModelFilters(options)
|
|
555
586
|
const capabilities = parseModelCapabilities(options.modelCapabilities)
|
|
587
|
+
const formatModelNames = readFormatModelNames(options)
|
|
556
588
|
|
|
557
589
|
// Resolve base URL
|
|
558
590
|
let baseURL: string | null = null
|
|
@@ -608,10 +640,12 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
608
640
|
|
|
609
641
|
const models = actualProvider.models as Record<string, unknown>
|
|
610
642
|
|
|
611
|
-
// Identity includes the filter/capability config: those
|
|
612
|
-
// baked into cached entries, so changing them must start a
|
|
643
|
+
// Identity includes the filter/capability/naming config: those
|
|
644
|
+
// are baked into cached entries, so changing them must start a
|
|
613
645
|
// fresh discovery instead of serving the old adjusted view.
|
|
614
|
-
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities
|
|
646
|
+
const cacheKey = buildCacheKey(providerId, baseURL, filters, capabilities, {
|
|
647
|
+
formatModelNames,
|
|
648
|
+
})
|
|
615
649
|
|
|
616
650
|
// Remember how to reach this proxy so the `event` hook can
|
|
617
651
|
// revalidate its cache in the background on new sessions.
|
|
@@ -621,6 +655,7 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
621
655
|
customHeaders,
|
|
622
656
|
filters,
|
|
623
657
|
capabilities,
|
|
658
|
+
formatModelNames,
|
|
624
659
|
providerId,
|
|
625
660
|
})
|
|
626
661
|
|
|
@@ -654,7 +689,15 @@ export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
|
654
689
|
// persist for subsequent startups. Capped by a timeout so a slow
|
|
655
690
|
// proxy never blocks boot.
|
|
656
691
|
const built = await withTimeout(
|
|
657
|
-
discoverModels(
|
|
692
|
+
discoverModels(
|
|
693
|
+
baseURL,
|
|
694
|
+
apiKey,
|
|
695
|
+
customHeaders,
|
|
696
|
+
providerId,
|
|
697
|
+
filters,
|
|
698
|
+
capabilities,
|
|
699
|
+
formatModelNames,
|
|
700
|
+
),
|
|
658
701
|
DISCOVERY_TIMEOUT_MS,
|
|
659
702
|
)
|
|
660
703
|
if (built && Object.keys(built).length > 0) {
|