opencode-plugin-litellm 1.0.0 â 1.0.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 +12 -1
- package/package.json +1 -1
- package/src/plugin/index.ts +56 -17
- package/src/utils/litellm-api.ts +19 -3
package/README.md
CHANGED
|
@@ -77,7 +77,8 @@ opencode
|
|
|
77
77
|
| ð§Đ **Reasoning-effort variants** | When LiteLLM reports per-model effort support (`supports_low_reasoning_effort`, âĶ), the plugin surfaces each level as a picker variant automatically. |
|
|
78
78
|
| ð **Auth-aware** | Honours `LITELLM_API_KEY` / `LITELLM_MASTER_KEY` env vars, `provider.litellm.options.apiKey`, or the key you stored via OpenCode's `/connect`. |
|
|
79
79
|
| ð **Gateway-friendly** | Supports `customHeaders` for proxies behind Cloudflare Access or other API gateways requiring extra HTTP headers. |
|
|
80
|
-
| âąïļ **Non-blocking startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** for slow remote proxies. Repeat config-hook invocations are a no-op. |
|
|
80
|
+
| âąïļ **Non-blocking startup** | Health checks fail fast (3 s); discovery fetches are capped at **15 s** (configurable via `LITELLM_REQUEST_TIMEOUT_MS`) for slow remote proxies. Repeat config-hook invocations are a no-op. |
|
|
81
|
+
| ð **TUI-safe logging** | All plugin logs go through OpenCode's log API (into OpenCode's own log files), never to stdout â the TUI stays intact. |
|
|
81
82
|
| ðĪ **Non-destructive merge** | Only adds models you don't already have configured. Hand-curated entries are preserved verbatim. |
|
|
82
83
|
| ðŠķ **Zero runtime deps** | Only depends on `@opencode-ai/plugin`. No build step, no bundler. |
|
|
83
84
|
| ð **TypeScript strict** | Strict-mode compiled, fully typed public API. |
|
|
@@ -212,6 +213,16 @@ If your LiteLLM proxy requires a master key, expose it via either approach:
|
|
|
212
213
|
|
|
213
214
|
The env var path lets you commit `opencode.json` without leaking secrets. The `/connect` path is useful when you'd rather manage the credential through OpenCode's own auth store (`~/.local/share/opencode/auth.json`) instead of an env var or config file â the plugin reads that file as a fallback and applies the stored key to its own health-check, model-discovery, and completion-time provider requests, so a key-only proxy works end to end.
|
|
214
215
|
|
|
216
|
+
### Slow proxies (`LITELLM_REQUEST_TIMEOUT_MS`)
|
|
217
|
+
|
|
218
|
+
Each discovery request (`/v1/models`, `/v1/model/info`) is capped at 15 s by default. Proxies with many database-defined models â or a gateway in front of LiteLLM â can legitimately take longer. Raise the budget with:
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
export LITELLM_REQUEST_TIMEOUT_MS=60000
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
The overall discovery cap scales with it (max of 20 s and the request timeout + 5 s), so a slow proxy never blocks startup indefinitely but isn't cut off mid-flight either. Invalid values fall back to the default.
|
|
225
|
+
|
|
215
226
|
### Custom headers (Cloudflare Access, API gateways)
|
|
216
227
|
|
|
217
228
|
If your LiteLLM proxy is behind Cloudflare Access or another gateway that requires extra HTTP headers, use the `customHeaders` option:
|
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.0.
|
|
4
|
+
"version": "1.0.1",
|
|
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
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
checkLiteLLMHealth,
|
|
5
5
|
discoverLiteLLMModelInfo,
|
|
6
6
|
discoverLiteLLMModels,
|
|
7
|
+
getRequestTimeoutMs,
|
|
7
8
|
normalizeBaseURL,
|
|
8
9
|
} from '../utils/litellm-api'
|
|
9
10
|
import {
|
|
@@ -15,13 +16,41 @@ import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
|
|
|
15
16
|
import { readModelCache, writeModelCache, readModelCacheSavedAt } from '../utils/model-cache'
|
|
16
17
|
|
|
17
18
|
const CHAT_PROVIDER_ID = 'litellm'
|
|
18
|
-
// Covers the
|
|
19
|
-
//
|
|
20
|
-
|
|
19
|
+
// Covers the 3 s health check plus the parallel models/model-info fetch
|
|
20
|
+
// phase, with headroom. Scales with LITELLM_REQUEST_TIMEOUT_MS so slow
|
|
21
|
+
// proxies aren't cut off by the overall cap either (issue #20).
|
|
22
|
+
const DISCOVERY_TIMEOUT_MS = Math.max(20000, getRequestTimeoutMs() + 5000)
|
|
21
23
|
// Don't revalidate a baseURL's cache more often than this, so a burst
|
|
22
24
|
// of `session.created` events can't generate repeated discovery traffic.
|
|
23
25
|
const REFRESH_MIN_INTERVAL_MS = 5 * 60 * 1000 // 5 minutes
|
|
24
26
|
|
|
27
|
+
type LogLevel = 'info' | 'warn' | 'error' | 'debug'
|
|
28
|
+
|
|
29
|
+
let logClient: PluginInput['client'] | null = null
|
|
30
|
+
|
|
31
|
+
function initLogging(client: PluginInput['client']): void {
|
|
32
|
+
logClient = client
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Route plugin logs through OpenCode's log API instead of stdout.
|
|
37
|
+
* console.* writes straight into the same terminal the OpenCode TUI
|
|
38
|
+
* renders in, corrupting the interface on every background refresh
|
|
39
|
+
* (issue #15); client.app.log lands in OpenCode's log files instead.
|
|
40
|
+
*/
|
|
41
|
+
function log(level: LogLevel, message: string): void {
|
|
42
|
+
if (logClient) {
|
|
43
|
+
void logClient.app
|
|
44
|
+
.log({ body: { service: 'opencode-litellm', level, message } })
|
|
45
|
+
.catch(() => {})
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
// No client wired up (unit tests, unusual embedders) â fall back to
|
|
49
|
+
// the console rather than silently dropping diagnostics.
|
|
50
|
+
if (level === 'info' || level === 'debug') console.log(message)
|
|
51
|
+
else console.warn(message)
|
|
52
|
+
}
|
|
53
|
+
|
|
25
54
|
/**
|
|
26
55
|
* OpenCode invokes the `config` hook several times per run with a
|
|
27
56
|
* cumulative config object. Track which model ids we already injected
|
|
@@ -218,7 +247,8 @@ async function discoverModels(
|
|
|
218
247
|
providerId: string,
|
|
219
248
|
): Promise<Record<string, unknown> | null> {
|
|
220
249
|
if (!(await checkLiteLLMHealth(baseURL, apiKey, customHeaders))) {
|
|
221
|
-
|
|
250
|
+
log(
|
|
251
|
+
'warn',
|
|
222
252
|
`[opencode-litellm] LiteLLM appears offline or unauthorized for provider "${providerId}" at ${baseURL}`,
|
|
223
253
|
)
|
|
224
254
|
return null
|
|
@@ -235,9 +265,10 @@ async function discoverModels(
|
|
|
235
265
|
|
|
236
266
|
if (modelsResult.status === 'rejected') {
|
|
237
267
|
const error = modelsResult.reason
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
268
|
+
log(
|
|
269
|
+
'warn',
|
|
270
|
+
`[opencode-litellm] Model discovery failed for provider "${providerId}": ` +
|
|
271
|
+
(error instanceof Error ? error.message : String(error)),
|
|
241
272
|
)
|
|
242
273
|
return null
|
|
243
274
|
}
|
|
@@ -248,14 +279,16 @@ async function discoverModels(
|
|
|
248
279
|
infoByName = infoResult.value
|
|
249
280
|
} else {
|
|
250
281
|
const reason = infoResult.reason
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
282
|
+
log(
|
|
283
|
+
'warn',
|
|
284
|
+
`[opencode-litellm] /v1/model/info unavailable for provider "${providerId}"; non-chat model filtering will use id heuristics only: ` +
|
|
285
|
+
(reason instanceof Error ? reason.message : String(reason)),
|
|
254
286
|
)
|
|
255
287
|
}
|
|
256
288
|
|
|
257
289
|
if (discovered.length === 0) {
|
|
258
|
-
|
|
290
|
+
log(
|
|
291
|
+
'warn',
|
|
259
292
|
`[opencode-litellm] LiteLLM responded for provider "${providerId}" but exposed zero models.`,
|
|
260
293
|
)
|
|
261
294
|
return null
|
|
@@ -284,14 +317,16 @@ async function discoverModels(
|
|
|
284
317
|
}
|
|
285
318
|
|
|
286
319
|
if (unmatched.length > 0) {
|
|
287
|
-
|
|
320
|
+
log(
|
|
321
|
+
'warn',
|
|
288
322
|
`[opencode-litellm] /v1/model/info has no entry for ${unmatched.length} model(s) on provider "${providerId}"; ` +
|
|
289
323
|
`classification uses id heuristics for: ${unmatched.slice(0, 5).join(', ')}` +
|
|
290
324
|
(unmatched.length > 5 ? `, +${unmatched.length - 5} more` : ''),
|
|
291
325
|
)
|
|
292
326
|
}
|
|
293
327
|
|
|
294
|
-
|
|
328
|
+
log(
|
|
329
|
+
'info',
|
|
295
330
|
`[opencode-litellm] Discovered ${discovered.length} models for provider "${providerId}" from ${baseURL} ` +
|
|
296
331
|
`(${Object.keys(built).length} built` +
|
|
297
332
|
(skipped > 0 ? `, ${skipped} non-chat hidden` : '') +
|
|
@@ -348,7 +383,8 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
348
383
|
)
|
|
349
384
|
if (built && Object.keys(built).length > 0) {
|
|
350
385
|
writeModelCache(cacheKey, built)
|
|
351
|
-
|
|
386
|
+
log(
|
|
387
|
+
'info',
|
|
352
388
|
`[opencode-litellm] Background-refreshed model cache for ${ctx.baseURL} (${Object.keys(built).length} models)`,
|
|
353
389
|
)
|
|
354
390
|
}
|
|
@@ -383,7 +419,8 @@ async function backgroundRefresh(cacheKey: string): Promise<void> {
|
|
|
383
419
|
* }
|
|
384
420
|
* }
|
|
385
421
|
*/
|
|
386
|
-
export const LiteLLMPlugin: Plugin = async (
|
|
422
|
+
export const LiteLLMPlugin: Plugin = async (input: PluginInput) => {
|
|
423
|
+
initLogging(input.client)
|
|
387
424
|
return {
|
|
388
425
|
config: async (config: any) => {
|
|
389
426
|
// Ensure the provider entry exists
|
|
@@ -451,7 +488,8 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
451
488
|
}
|
|
452
489
|
|
|
453
490
|
if (!baseURL) {
|
|
454
|
-
|
|
491
|
+
log(
|
|
492
|
+
'warn',
|
|
455
493
|
`[opencode-litellm] No LiteLLM proxy found for provider "${providerId}". Configure options.baseURL or start LiteLLM on port 4000/8000/8080.`,
|
|
456
494
|
)
|
|
457
495
|
continue
|
|
@@ -520,7 +558,8 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
|
|
|
520
558
|
if (cached && Object.keys(cached).length > 0) {
|
|
521
559
|
const added = mergeModels(models, cached)
|
|
522
560
|
injectedModelIds.set(cacheKey, new Set(added))
|
|
523
|
-
|
|
561
|
+
log(
|
|
562
|
+
'info',
|
|
524
563
|
`[opencode-litellm] Loaded ${Object.keys(cached).length} models from cache for provider "${providerId}" (${baseURL}); refresh happens in the background on new sessions.`,
|
|
525
564
|
)
|
|
526
565
|
continue
|
package/src/utils/litellm-api.ts
CHANGED
|
@@ -8,7 +8,23 @@ const MODEL_INFO_ENDPOINT = '/v1/model/info'
|
|
|
8
8
|
// payloads from remote proxies with many database-defined models can
|
|
9
9
|
// be large and slow to generate.
|
|
10
10
|
const HEALTH_TIMEOUT_MS = 3000
|
|
11
|
-
const
|
|
11
|
+
const DEFAULT_FETCH_TIMEOUT_MS = 15000
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Per-request timeout for discovery fetches (`/v1/models`,
|
|
15
|
+
* `/v1/model/info`), overridable via `LITELLM_REQUEST_TIMEOUT_MS` for
|
|
16
|
+
* proxies that legitimately need longer than the 15 s default (issue
|
|
17
|
+
* #20). Invalid values fall back to the default.
|
|
18
|
+
*/
|
|
19
|
+
export function getRequestTimeoutMs(): number {
|
|
20
|
+
const raw = process.env.LITELLM_REQUEST_TIMEOUT_MS
|
|
21
|
+
if (!raw) return DEFAULT_FETCH_TIMEOUT_MS
|
|
22
|
+
// Number() (not parseInt) so '12.5' or '60abc' fall back to the
|
|
23
|
+
// default instead of silently truncating to a bogus 12 ms timeout.
|
|
24
|
+
const parsed = Number(raw)
|
|
25
|
+
if (!Number.isInteger(parsed) || parsed <= 0) return DEFAULT_FETCH_TIMEOUT_MS
|
|
26
|
+
return parsed
|
|
27
|
+
}
|
|
12
28
|
|
|
13
29
|
/**
|
|
14
30
|
* Normalise a base URL so the rest of the plugin can rely on a
|
|
@@ -72,7 +88,7 @@ export async function discoverLiteLLMModels(
|
|
|
72
88
|
const response = await fetch(url, {
|
|
73
89
|
method: 'GET',
|
|
74
90
|
headers: buildHeaders(apiKey, customHeaders),
|
|
75
|
-
signal: AbortSignal.timeout(
|
|
91
|
+
signal: AbortSignal.timeout(getRequestTimeoutMs()),
|
|
76
92
|
})
|
|
77
93
|
|
|
78
94
|
if (!response.ok) {
|
|
@@ -98,7 +114,7 @@ export async function discoverLiteLLMModelInfo(
|
|
|
98
114
|
const response = await fetch(url, {
|
|
99
115
|
method: 'GET',
|
|
100
116
|
headers: buildHeaders(apiKey, customHeaders),
|
|
101
|
-
signal: AbortSignal.timeout(
|
|
117
|
+
signal: AbortSignal.timeout(getRequestTimeoutMs()),
|
|
102
118
|
})
|
|
103
119
|
|
|
104
120
|
if (!response.ok) {
|