opencode-plugin-litellm 1.4.0 → 1.4.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 -1
- package/package.json +2 -2
- package/src/plugin/v2.ts +157 -24
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
# opencode-litellm
|
|
8
8
|
|
|
9
|
-
**[OpenCode
|
|
9
|
+
**[OpenCode CLI (v1 and v2) and Desktop](https://opencode.ai) + [LiteLLM](https://github.com/BerriAI/litellm) provider with zero configuration.**
|
|
10
10
|
|
|
11
11
|
[](https://opencode.ai)
|
|
12
12
|
[](https://opencode.ai)
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "opencode-plugin-litellm",
|
|
4
|
-
"version": "1.4.
|
|
5
|
-
"description": "OpenCode plugin for LiteLLM proxy support with auto-detection and dynamic model discovery",
|
|
4
|
+
"version": "1.4.1",
|
|
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",
|
|
8
8
|
"exports": {
|
package/src/plugin/v2.ts
CHANGED
|
@@ -43,6 +43,23 @@ interface ProviderSource {
|
|
|
43
43
|
customHeaders?: Record<string, string>
|
|
44
44
|
credentialReloadRequired: boolean
|
|
45
45
|
refreshRequired: boolean
|
|
46
|
+
/**
|
|
47
|
+
* True when this source came from option/env/port auto-detection rather than
|
|
48
|
+
* a host-registered provider. A fallback only stands in until the real
|
|
49
|
+
* provider appears; reconcile replaces it at that point so the user's
|
|
50
|
+
* `providers.<id>.settings.baseURL` wins over the guessed endpoint.
|
|
51
|
+
*
|
|
52
|
+
* Distinct from {@link pendingPublication}: a fallback source is always
|
|
53
|
+
* pending until a configured provider supersedes it, whereas a configured
|
|
54
|
+
* source is pending only until its first successful `reload()`.
|
|
55
|
+
*/
|
|
56
|
+
fromFallback: boolean
|
|
57
|
+
/**
|
|
58
|
+
* True until this source has been published to the provider registry by a
|
|
59
|
+
* successful `reload()`. Reconciliation retries (and re-adds) any source
|
|
60
|
+
* still pending, so a transient reload failure does not strand it.
|
|
61
|
+
*/
|
|
62
|
+
pendingPublication: boolean
|
|
46
63
|
filters: ModelFilters
|
|
47
64
|
capabilities: ModelCapabilities
|
|
48
65
|
formatModelNames: boolean
|
|
@@ -285,6 +302,8 @@ async function makeProviderSource(
|
|
|
285
302
|
usesConnectionCredential,
|
|
286
303
|
credentialReloadRequired: false,
|
|
287
304
|
refreshRequired: false,
|
|
305
|
+
fromFallback: provider === undefined,
|
|
306
|
+
pendingPublication: true,
|
|
288
307
|
baseURL,
|
|
289
308
|
apiKey,
|
|
290
309
|
customHeaders: Object.keys(customHeaders).length > 0 ? customHeaders : undefined,
|
|
@@ -414,41 +433,83 @@ async function refreshProviderSources(
|
|
|
414
433
|
await Promise.all(sources.map((source) => refreshProviderSource(context, source, inFlight)))
|
|
415
434
|
}
|
|
416
435
|
|
|
436
|
+
/**
|
|
437
|
+
* LiteLLM-shaped providers the host currently exposes.
|
|
438
|
+
*
|
|
439
|
+
* OpenCode 2 registers user-configured providers *after* plugin setup, so the
|
|
440
|
+
* list has to be re-read on later `provider.updated` events instead of being
|
|
441
|
+
* trusted once at startup. Otherwise a `providers.litellm.settings.baseURL`
|
|
442
|
+
* config is invisible to the plugin and discovery silently falls back to
|
|
443
|
+
* auto-detection.
|
|
444
|
+
*/
|
|
445
|
+
async function listMatchingProviders(context: Context): Promise<ProviderInfo[]> {
|
|
446
|
+
try {
|
|
447
|
+
const providers = (await context.provider.list()).data
|
|
448
|
+
return providers.filter((provider) =>
|
|
449
|
+
isLiteLLMProvider(provider.id, asRecord(provider.settings)),
|
|
450
|
+
)
|
|
451
|
+
} catch (error) {
|
|
452
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
453
|
+
logInfo(`[opencode-litellm] Could not read configured providers: ${message}`)
|
|
454
|
+
return []
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Discover and add sources for LiteLLM providers that have appeared since the
|
|
460
|
+
* last reconciliation. Ids already present are left untouched *unless* the
|
|
461
|
+
* existing entry is a fallback that never came from a host-registered
|
|
462
|
+
* provider — in that case it is replaced, so merging a late provider overrides
|
|
463
|
+
* the option/env/auto-detected endpoint (including its baseURL and
|
|
464
|
+
* credentials). Safe to call repeatedly (startup, `provider.updated`,
|
|
465
|
+
* `session.created`). Returns sources that still need publishing via
|
|
466
|
+
* `reload()` (new providers plus any whose previous publication failed).
|
|
467
|
+
*/
|
|
468
|
+
async function reconcileProviderSources(
|
|
469
|
+
context: Context,
|
|
470
|
+
pluginOptions: Record<string, unknown>,
|
|
471
|
+
sources: Map<string, ProviderSource>,
|
|
472
|
+
): Promise<ProviderSource[]> {
|
|
473
|
+
const changed: ProviderSource[] = []
|
|
474
|
+
for (const provider of await listMatchingProviders(context)) {
|
|
475
|
+
const existing = sources.get(provider.id)
|
|
476
|
+
// Skip sources that are both configured (not a fallback) and already
|
|
477
|
+
// published; a pending configured source must be returned so it is retried.
|
|
478
|
+
if (existing && !existing.fromFallback && !existing.pendingPublication) continue
|
|
479
|
+
const source = await makeProviderSource(context, provider, pluginOptions)
|
|
480
|
+
if (!source) continue
|
|
481
|
+
sources.set(source.id, source)
|
|
482
|
+
changed.push(source)
|
|
483
|
+
}
|
|
484
|
+
return changed
|
|
485
|
+
}
|
|
486
|
+
|
|
417
487
|
const definition = Plugin.define({
|
|
418
488
|
id: 'opencode-litellm',
|
|
419
489
|
async setup(context) {
|
|
420
490
|
initV2Logging()
|
|
421
491
|
|
|
422
492
|
const pluginOptions = asRecord(context.options)
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
493
|
+
const sources = new Map<string, ProviderSource>()
|
|
494
|
+
|
|
495
|
+
// First pass. On OpenCode 2 the host has not registered user-configured
|
|
496
|
+
// providers yet, so this typically finds only built-ins; the event loop
|
|
497
|
+
// below reconciles again when they appear.
|
|
498
|
+
await reconcileProviderSources(context, pluginOptions, sources)
|
|
499
|
+
// With no configured provider, fall back to purely option/env-driven
|
|
500
|
+
// discovery so `LITELLM_BASE_URL` and plugin options keep working.
|
|
501
|
+
if (sources.size === 0) {
|
|
502
|
+
const fallback = await makeProviderSource(context, undefined, pluginOptions)
|
|
503
|
+
if (fallback) sources.set(fallback.id, fallback)
|
|
429
504
|
}
|
|
430
|
-
|
|
431
|
-
const matchingProviders = providers.filter((provider) =>
|
|
432
|
-
isLiteLLMProvider(provider.id, asRecord(provider.settings)),
|
|
433
|
-
)
|
|
434
|
-
const candidates: Array<ProviderInfo | undefined> = matchingProviders.length
|
|
435
|
-
? matchingProviders
|
|
436
|
-
: [undefined]
|
|
437
|
-
const sources: ProviderSource[] = []
|
|
438
|
-
|
|
439
|
-
for (const candidate of candidates) {
|
|
440
|
-
const source = await makeProviderSource(context, candidate, pluginOptions)
|
|
441
|
-
if (source) sources.push(source)
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
if (sources.length === 0) {
|
|
505
|
+
if (sources.size === 0) {
|
|
445
506
|
logInfo(
|
|
446
507
|
'[opencode-litellm] No LiteLLM proxy found. Configure providers.litellm.settings.baseURL or start LiteLLM on port 4000/8000/8080.',
|
|
447
508
|
)
|
|
448
509
|
}
|
|
449
510
|
|
|
450
511
|
const providerRegistration = await context.provider.transform((editor) => {
|
|
451
|
-
for (const source of sources) {
|
|
512
|
+
for (const source of sources.values()) {
|
|
452
513
|
const current = editor.get(source.id)
|
|
453
514
|
if (current) {
|
|
454
515
|
editor.update(source.id, (provider) => {
|
|
@@ -488,16 +549,79 @@ const definition = Plugin.define({
|
|
|
488
549
|
})
|
|
489
550
|
}
|
|
490
551
|
}
|
|
552
|
+
// The initial transform is itself the publication step for sources built
|
|
553
|
+
// during setup, so they are no longer "pending" after this point.
|
|
554
|
+
for (const source of sources.values()) source.pendingPublication = false
|
|
491
555
|
})
|
|
492
556
|
|
|
493
557
|
const controller = new AbortController()
|
|
494
558
|
const inFlight = new Map<string, Promise<void>>()
|
|
559
|
+
|
|
560
|
+
// Reconciliation is kicked off from the event loop, which must not block on
|
|
561
|
+
// discovery. Overlapping triggers are folded into one follow-up pass, so a
|
|
562
|
+
// provider that shows up mid-discovery is neither missed nor discovered
|
|
563
|
+
// twice.
|
|
564
|
+
let reconcileRunning: Promise<void> | null = null
|
|
565
|
+
let reconcileAgain = false
|
|
566
|
+
|
|
567
|
+
const reconcileOnce = async (): Promise<void> => {
|
|
568
|
+
// A newly built source is only "published" once `reload()` succeeds. Until
|
|
569
|
+
// then it stays `pendingPublication`, so the next pass returns and retries
|
|
570
|
+
// it instead of skipping it forever after a transient reload failure.
|
|
571
|
+
let pending: ProviderSource[]
|
|
572
|
+
try {
|
|
573
|
+
pending = await reconcileProviderSources(context, pluginOptions, sources)
|
|
574
|
+
} catch (error) {
|
|
575
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
576
|
+
logInfo(`[opencode-litellm] Could not reconcile configured providers: ${message}`)
|
|
577
|
+
return
|
|
578
|
+
}
|
|
579
|
+
if (pending.length === 0) return
|
|
580
|
+
try {
|
|
581
|
+
await context.provider.reload()
|
|
582
|
+
for (const source of pending) source.pendingPublication = false
|
|
583
|
+
logInfo(
|
|
584
|
+
`[opencode-litellm] Registered ${pending.length} newly configured LiteLLM provider(s).`,
|
|
585
|
+
)
|
|
586
|
+
} catch (error) {
|
|
587
|
+
// Leave `pendingPublication` set so the next trigger retries instead of
|
|
588
|
+
// treating these sources as already published.
|
|
589
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
590
|
+
logInfo(
|
|
591
|
+
`[opencode-litellm] Could not reload providers after discovering a new provider; will retry: ${message}`,
|
|
592
|
+
)
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const reconcileAndReload = async (): Promise<void> => {
|
|
597
|
+
if (reconcileRunning) {
|
|
598
|
+
reconcileAgain = true
|
|
599
|
+
return reconcileRunning
|
|
600
|
+
}
|
|
601
|
+
reconcileRunning = (async () => {
|
|
602
|
+
do {
|
|
603
|
+
reconcileAgain = false
|
|
604
|
+
await reconcileOnce()
|
|
605
|
+
} while (reconcileAgain)
|
|
606
|
+
})().finally(() => {
|
|
607
|
+
reconcileRunning = null
|
|
608
|
+
// A trigger can land in the gap between the loop's final check and this
|
|
609
|
+
// continuation. Without re-checking, that update would be dropped.
|
|
610
|
+
if (reconcileAgain) void reconcileAndReload()
|
|
611
|
+
})
|
|
612
|
+
return reconcileRunning
|
|
613
|
+
}
|
|
614
|
+
|
|
495
615
|
void (async () => {
|
|
496
616
|
try {
|
|
497
617
|
for await (const event of context.event.subscribe({ signal: controller.signal })) {
|
|
618
|
+
if (event.type === 'provider.updated') {
|
|
619
|
+
void reconcileAndReload()
|
|
620
|
+
continue
|
|
621
|
+
}
|
|
498
622
|
if (event.type === 'credential.switched') {
|
|
499
623
|
const changedSources = (await Promise.all(
|
|
500
|
-
sources
|
|
624
|
+
[...sources.values()]
|
|
501
625
|
.filter((source) => source.integrationID === event.data.integrationID)
|
|
502
626
|
.map(async (source) =>
|
|
503
627
|
(await refreshConnectionCredential(context, source)) ? source : undefined,
|
|
@@ -513,7 +637,10 @@ const definition = Plugin.define({
|
|
|
513
637
|
continue
|
|
514
638
|
}
|
|
515
639
|
if (event.type !== 'session.created') continue
|
|
516
|
-
void
|
|
640
|
+
void (async () => {
|
|
641
|
+
await reconcileAndReload()
|
|
642
|
+
await refreshProviderSources(context, [...sources.values()], inFlight)
|
|
643
|
+
})()
|
|
517
644
|
}
|
|
518
645
|
} catch (error) {
|
|
519
646
|
if (controller.signal.aborted) return
|
|
@@ -522,6 +649,12 @@ const definition = Plugin.define({
|
|
|
522
649
|
}
|
|
523
650
|
})()
|
|
524
651
|
|
|
652
|
+
// A provider can register during setup's own auto-detection probes, before
|
|
653
|
+
// the subscription above is established; that `provider.updated` would be
|
|
654
|
+
// lost until the next session. One idempotent pass right after subscribing
|
|
655
|
+
// closes that window.
|
|
656
|
+
void reconcileAndReload()
|
|
657
|
+
|
|
525
658
|
return async () => {
|
|
526
659
|
controller.abort()
|
|
527
660
|
await providerRegistration.dispose()
|