opencode-plugin-litellm 0.10.0 → 0.11.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 CHANGED
@@ -1,3 +1,5 @@
1
+
2
+
1
3
  <div align="center">
2
4
 
3
5
  <img src="https://raw.githubusercontent.com/yuseferi/opencode-litellm/main/assets/logo.svg" alt="opencode-litellm logo" width="128" height="128" />
@@ -244,8 +246,9 @@ If your LiteLLM proxy requires a master key, expose it via either approach:
244
246
  | Env var | `export LITELLM_API_KEY=sk-...` |
245
247
  | Env var (alias) | `export LITELLM_MASTER_KEY=sk-...` |
246
248
  | Config | `"options": { "apiKey": "{env:LITELLM_API_KEY}" }` |
249
+ | OpenCode `/connect` | Run `/connect`, search for your `litellm` provider entry, and paste the key |
247
250
 
248
- The env var path lets you commit `opencode.json` without leaking secrets.
251
+ 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.
249
252
 
250
253
  ### Custom headers (Cloudflare Access, API gateways)
251
254
 
@@ -307,7 +310,7 @@ sequenceDiagram
307
310
 
308
311
  ## 📋 Requirements
309
312
 
310
- - [OpenCode](https://opencode.ai) ≥ 0.1.x with plugin support (`@opencode-ai/plugin ^1.0.166`)
313
+ - [OpenCode](https://opencode.ai) ≥ 0.1.x with plugin support (`@opencode-ai/plugin ^1.14.0`)
311
314
  - A running [LiteLLM](https://github.com/BerriAI/litellm) proxy:
312
315
  ```bash
313
316
  pip install 'litellm[proxy]'
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.10.0",
4
+ "version": "0.11.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",
@@ -11,6 +11,7 @@ import {
11
11
  categorizeModel,
12
12
  } from '../utils/format-model-name'
13
13
  import type { LiteLLMModel, LiteLLMModelInfo } from '../types'
14
+ import { getOpenCodeStoredApiKey } from '../utils/opencode-auth'
14
15
  import { readModelCache, writeModelCache, readModelCacheSavedAt } from '../utils/model-cache'
15
16
 
16
17
  const CHAT_PROVIDER_ID = 'litellm'
@@ -411,8 +412,17 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
411
412
  ? options.apiKey
412
413
  : undefined
413
414
  const envKey =
414
- process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY
415
- const apiKey = configuredKey ?? envKey
415
+ process.env.LITELLM_API_KEY || process.env.LITELLM_MASTER_KEY || undefined
416
+ /*
417
+ Falls back to the key OpenCode itself stored for this provider id via
418
+ '/connect' (~/.local/share/opencode/auth.json). For a custom provider
419
+ like this one there is no automatic auth.json injection (see below), so
420
+ the plugin reads the stored key here and applies it to both its own
421
+ discovery fetches and the completion-time provider options. Without it,
422
+ a key-only proxy would fail discovery even though chat works.
423
+ */
424
+ const storedKey = await getOpenCodeStoredApiKey(providerId)
425
+ const apiKey = configuredKey ?? envKey ?? storedKey
416
426
  const customHeaders = readCustomHeaders(options)
417
427
 
418
428
  // Resolve base URL
@@ -441,12 +451,25 @@ export const LiteLLMPlugin: Plugin = async (_input: PluginInput) => {
441
451
  }
442
452
 
443
453
  if (!actualProvider.options) {
444
- actualProvider.options = { baseURL: `${baseURL}/v1` }
445
- } else {
446
- const actualOptions = actualProvider.options as Record<string, unknown>
447
- if (!actualOptions.baseURL) {
448
- actualOptions.baseURL = `${baseURL}/v1`
449
- }
454
+ actualProvider.options = {}
455
+ }
456
+ const actualOptions = actualProvider.options as Record<string, unknown>
457
+ if (!actualOptions.baseURL) {
458
+ actualOptions.baseURL = `${baseURL}/v1`
459
+ }
460
+ /*
461
+ For a fully custom, config-only provider like this one, OpenCode
462
+ builds the real completion-time AI SDK client straight from
463
+ `options` - there's no separate auth.json auto-injection for
464
+ arbitrary custom providers (that only applies to a handful of
465
+ models.dev-catalog providers with special-cased loaders). So the
466
+ `apiKey` resolved above (config > env var > OpenCode-stored
467
+ credential) must be written back here, or real chat completions
468
+ go out with no Authorization header even though discovery
469
+ succeeded using the same resolved key.
470
+ */
471
+ if (!actualOptions.apiKey && apiKey) {
472
+ actualOptions.apiKey = apiKey
450
473
  }
451
474
 
452
475
  if (!actualProvider.models) {
@@ -0,0 +1,83 @@
1
+ import { readFile } from 'node:fs/promises'
2
+ import { homedir } from 'node:os'
3
+ import { join } from 'node:path'
4
+
5
+ /**
6
+ * Shape of an `api`-type entry in OpenCode's own credential store. OpenCode
7
+ * also stores `oauth` and `wellknown` entries there, but LiteLLM proxies
8
+ * only ever use a plain API key, so that's the only variant we care about.
9
+ */
10
+ interface OpenCodeApiAuthEntry {
11
+ type: 'api'
12
+ key: string
13
+ metadata?: Record<string, string>
14
+ }
15
+
16
+ type OpenCodeAuthEntry = OpenCodeApiAuthEntry | { type: string; [key: string]: unknown }
17
+ type OpenCodeAuthFile = Record<string, OpenCodeAuthEntry>
18
+
19
+ /**
20
+ * OpenCode stores credentials added via the `/connect` command (or
21
+ * `opencode auth login`) in this file, keyed by provider id. The location
22
+ * is fixed and identical across macOS, Linux, and Windows — see
23
+ * https://opencode.ai/docs/troubleshooting/#storage.
24
+ */
25
+ function resolveAuthPath(): string {
26
+ return join(homedir(), '.local', 'share', 'opencode', 'auth.json')
27
+ }
28
+
29
+ // Read once per process. The `config` hook can fire multiple times per
30
+ // run (see plugin/index.ts), and re-reading a credentials file from disk
31
+ // on every invocation is both wasteful and unnecessary — a `/connect` run
32
+ // mid-session wouldn't be picked up by the already-running plugin anyway
33
+ // (model discovery itself is cached per baseURL for the same reason).
34
+ let cachedAuthFile: Promise<OpenCodeAuthFile | null> | null = null
35
+
36
+ async function loadOpenCodeAuthFile(): Promise<OpenCodeAuthFile | null> {
37
+ if (!cachedAuthFile) {
38
+ cachedAuthFile = (async () => {
39
+ try {
40
+ const raw = await readFile(resolveAuthPath(), 'utf-8')
41
+ const parsed: unknown = JSON.parse(raw)
42
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
43
+ return parsed as OpenCodeAuthFile
44
+ }
45
+ } catch {
46
+ // File doesn't exist, isn't readable, or isn't valid JSON — treat
47
+ // as "no stored credentials" rather than failing plugin startup.
48
+ }
49
+ return null
50
+ })()
51
+ }
52
+ return cachedAuthFile
53
+ }
54
+
55
+ /**
56
+ * Look up the API key OpenCode itself stored for a given provider id via
57
+ * the `/connect` command. This is entirely separate from this plugin's
58
+ * own `options.apiKey` / `LITELLM_API_KEY` / `LITELLM_MASTER_KEY`
59
+ * configuration: for a custom provider like this one, OpenCode does not
60
+ * inject stored credentials automatically, so the plugin reads the file
61
+ * directly and applies the key to its own health-check / `/v1/models`
62
+ * discovery fetches and to the completion-time provider `options` (see
63
+ * plugin/index.ts) as a fallback credential source.
64
+ *
65
+ * Returns `undefined` if the file is missing/unreadable, or has no
66
+ * `type: "api"` entry for `providerId`.
67
+ */
68
+ export async function getOpenCodeStoredApiKey(providerId: string): Promise<string | undefined> {
69
+ const auth = await loadOpenCodeAuthFile()
70
+ const entry = auth?.[providerId]
71
+ if (entry && entry.type === 'api' && typeof (entry as OpenCodeApiAuthEntry).key === 'string') {
72
+ const key = (entry as OpenCodeApiAuthEntry).key
73
+ if (key.length > 0) {
74
+ return key
75
+ }
76
+ }
77
+ return undefined
78
+ }
79
+
80
+ /** Test-only: force the next read to hit disk again. */
81
+ export function __resetOpenCodeAuthCacheForTests(): void {
82
+ cachedAuthFile = null
83
+ }