Files
OmniRoute/docs/guides/OPENCODE-V2-PLUGIN.md
Dizzle b345c7f6cd feat(opencode): opencode v2 plugin publishing the OmniRoute catalog (#12870)
opencode v2 loads plugins through a contract the existing
@omniroute/opencode-plugin cannot satisfy: v1 exports plugin factories with an
auth/provider/config/tool hook object, v2 expects a default define({id, setup})
carrying catalog and integration domains. One package would have to satisfy
both loaders from a single entrypoint. An opencode v2 install therefore has no
route to an OmniRoute gateway at all: no model discovery, no combos, no
enrichment.

This adds @omniroute/opencode-plugin-v2, a self-contained package. The v1
plugin is untouched, so v1 users see no move, no migration and no breaking
version. The two packages deliberately share no code and no release: the
mapping logic here began as a port of v1's and now lives in this package, which
keeps either one free to change without a coordinated publish.

The plugin publishes models, combos and auto-combos into the host catalog,
refreshes them lazily behind a 300s TTL, and keeps serving the last known
catalog from an on-disk snapshot when the gateway is unreachable. Publishing is
staged: models and combos are what a catalog is, so they go out as soon as they
are known, while auto-combos, the provider list and the enrichment overlay fold
into the snapshot when they land. Gating the publish on all of them made the
catalog hostage to the slowest source — a gateway that accepts the connection
and never answers /api/combos/auto left everything unpublished until that fetch
timed out, which is longer than a short-lived host stays alive.

Display names carry what the gateway knows about a model: the upstream provider
it routes to, whether it is free, and the budget that comes with it. Those parts
were already fetched and then dropped, so two connections selling the same model
looked identical in the picker. The provider prefix can be turned off with
`providerTag: false`.

The on-disk snapshot carries that overlay too, under a size cap, so a cold start
opens on named models rather than raw ids. The host is asked to reload only when
the catalog or the overlay actually moved, never once per refresh window.

The gateway key comes from the host credential store when one is connected, so
connecting the integration from opencode is enough and no secret needs to sit
in opencode.json; a plugin option and an environment variable remain as
fallbacks, and a host too old to expose a credential store still loads. Nothing
is silent when a key is missing or refused: an absent key is named once at
startup with the three ways to supply one, and an enrichment source the gateway
rejects is reported per endpoint with what the catalog loses. Those three
failures used to be empty catch blocks, which turned a management token the
gateway refuses into a catalog of raw model ids with no explanation.

Tool calling to Gemini keeps working. Gemini answers 400 INVALID_ARGUMENT for
an entire request whose tool declarations carry $schema, $ref or
additionalProperties. The v1 plugin handled it by wrapping fetch and rewriting
the JSON body; v2 does it on the language model, where the tools are still
structured data, and only for Gemini models of this provider. It can be turned
off with geminiSanitization: false, and a host exposing no aisdk domain loads
without it.

The catalog contract itself is a moving target, so the plugin adapts to the
host instead of assuming one shape. The released CLI keeps the aisdk package,
the endpoint (as settings.baseURL), the request headers and the variant options
directly on the model and provider; the current SDK types keep the same
information inside an api block. Writing only the api block yields a catalog
the released CLI lists but cannot route. Rather than key off a version list
that goes stale on the next release, the plugin reads the shape the host seeds
into the catalog draft and publishes accordingly: a seed with a top-level
package and no api block gets both field sets, a seed with an api block gets
that block alone, and an undisclosed seed gets both. None of the legacy keys
collide with a key of the current types, so the two shapes coexist on one
object, variants included.

Four v1 behaviours are deliberately not carried over, because v2 either owns
them or no longer needs them: the plugin-side debug log (the host has its own
logging), the compression-metadata suffix on combo names, the MCP auto-emit
(the v2 host owns MCP), and the omni-sync command plus its background timer
(the TTL and a content fingerprint drive catalog.reload instead).

A refresh never downgrades what is already published: the previous overlay is
carried forward until the new one lands, so names, pricing and the usable
filter no longer drop out for the length of every TTL window. The disk snapshot
is read after the credential is resolved, because it is keyed by that
credential — reading it earlier looked up the identity the options carry rather
than the one in use, and rejected a perfectly good catalog exactly when the
gateway was down.

The tool-schema cleaner now knows where a schema ends and a property name
begins. Stripping keywords by name anywhere in the tree deleted a tool
parameter called `ref` while leaving it in `required`, handing the model a
schema it could not satisfy; a `$ref` it cannot resolve now forwards the tool
untouched instead of widening it to accept anything. Gemini detection is
anchored on the model family, so `gemini-compatible-proxy` is no longer treated
as a Gemini model.

A source the gateway refuses is reported on the library entry point as well,
not only through the plugin, so the usable-provider filter can no longer disable
itself in silence. `providerId` is bounded to a safe character set because it
reaches a filesystem path, `hiddenModels` covers combos as it already covered
models, the Anthropic block gets the gateway root rather than a doubled `/v1`, an unparseable tool schema forwards the tool instead
of failing the request, and the package typechecks under the same settings as
the v1 plugin.

CI mirrors the existing plugin workflow: install, build and test on Node 22 and
24, for both packages. The plugin SDK stays pinned, and the host-shape assertions carry the risk of
a contract move rather than a check against a rolling upstream tag.

Co-authored-by: Max <maxmad64@gmail.com>
2026-09-06 18:36:27 -03:00

8.1 KiB

title, version, lastUpdated
title version lastUpdated
OpenCode v2 plugin — install and configure 3.8.51 2026-09-06

OpenCode v2 plugin — install and configure

@omniroute/opencode-plugin-v2 puts your whole OmniRoute catalog — models, combos and auto-combos — into OpenCode v2's model picker, with display names, pricing and free-tier budgets.

It is a separate package from @omniroute/opencode-plugin because OpenCode v1 and v2 load plugins through different contracts. Pick the one matching your OpenCode major; nothing is shared between them, so upgrading one never forces the other.

Requirements

  • OpenCode v2.
  • A reachable OmniRoute gateway (http://localhost:20128 by default).
  • Node.js 22 or 24.

Install

Add the plugin to opencode.json:

{
  "plugins": [
    {
      "package": "@omniroute/opencode-plugin-v2",
      "options": {
        "providerId": "omniroute",
        "baseURL": "http://localhost:20128"
      }
    }
  ]
}

Models then appear as omniroute/<provider>/<model>; providerId decides that prefix and the id of the integration OpenCode stores the credential under.

Credentials

The plugin looks for a gateway key in three places, in this order:

  1. The credential you connected in OpenCode. The plugin registers an integration, so OpenCode's own auth flow can store the key. Nothing lands in opencode.json — prefer this.
  2. apiKey in the plugin options, for a per-project override. It puts the key in a file you may be committing.
  3. OMNIROUTE_API_KEY in the environment.

With none of the three, the catalog is empty and the plugin says so once at startup instead of leaving you with a silent empty picker.

The management token is a different key

Combos, provider health and enrichment (display names, pricing, free-tier budgets) come from the gateway's /api/* endpoints, which most deployments gate behind a management token rather than the inference key:

"options": {
  "baseURL": "http://localhost:20128",
  "managementReadToken": "<management read token>"
}

Left unset, managementReadToken falls back to apiKey. When a gateway rejects that fallback the catalog still publishes, but with raw model ids instead of display names, no canonical alias dedupe, no pricing and no combos. The plugin warns once per endpoint when that happens, naming the endpoint and what was lost — so a degraded picker is never a mystery.

Options

Key Default Notes
providerId "omniroute" Provider id, integration id, and the prefix models appear under
baseURL required Gateway root; the /v1 suffix is added where needed
apiKey connected credential, then OMNIROUTE_API_KEY Chat key for /v1/*
managementReadToken falls back to apiKey Key for /api/* — usually not the same one
displayName "OmniRoute" Provider name in the picker
timeoutMs 10000 Per-endpoint fetch timeout (auto-combos use 5s)
modelCacheTtlMs 300000 Catalog cache TTL; a disk snapshot warms cold starts
timeouts falls back to timeoutMs Per-endpoint budgets in ms: models, combos, autoCombos, enrichment
enrichment true Fetch names, pricing and free-tier budgets
providerTag true Prefix a display name with the upstream provider it routes to
usableOnly false Keep only providers the gateway reports as provisioned
visibleModels / hiddenModels [] Exact-or-suffix allowlists; deny wins
geminiSanitization true Strip the JSON-Schema keywords Gemini rejects from tool schemas ($ref tools are forwarded untouched)
apiFormat.allowAnthropic false Route allowlisted ids through the Anthropic API block
apiFormat.anthropicModels [] Full model ids routed to Anthropic
logLevel / startupDebug warn / false Logger verbosity

How the catalog stays fresh

The catalog is fetched lazily and cached for modelCacheTtlMs, and a disk snapshot keeps the last known catalog available when the gateway is unreachable — an outage costs you nothing but freshness. Models and combos are published as soon as they arrive; auto-combos, the provider list and the enrichment overlay fold in when they land, so one slow endpoint cannot hold the whole picker hostage.

Names carry what the gateway knows: the upstream provider a model routes to, a [Free] marker and the budget that comes with it — so two connections selling the same model stay distinguishable. Turn the prefix off with "providerTag": false.

There is no manual refresh command: OpenCode v2 commands are prompt templates, not callbacks, so a plugin cannot expose one. Refreshes follow the TTL; the host is asked to reload only when the catalog or the overlay actually changed, never once per refresh.

Tool calling on Gemini

Gemini rejects an entire request whose tool declarations carry $schema or additionalProperties, answering 400 INVALID_ARGUMENT. The plugin strips those keywords from tool schemas bound for a Gemini model of this provider and leaves every other request untouched. A tool carrying a $ref is forwarded untouched rather than stripped, since removing the reference would widen the schema to "accept anything". Set "geminiSanitization": false to turn it off.

Troubleshooting

Symptom Cause
Picker shows no OmniRoute model No key resolved (check the startup warning), or the gateway is unreachable
Raw model ids, no combos, no pricing The management endpoints refused the token — set managementReadToken
A session pinned to opencode-<id>/… The v1 plugin published opencode-<id>; v2 publishes <id> bare, so re-select the model under <id>/…

See also

  • CLI-INTEGRATIONS.md — every setup-* CLI integration, including the lightweight openai-compatible OpenCode setup.
  • REMOTE-MODE.md — pointing a CLI at a remote gateway.