Files
plugins/packages/cloudflare-workers-ai
yetone 17e5ef7457 all: load on OpenCode 2.x through a default {id, setup} (#657)
nbczw8750 (#657): trae 0.2.26 on OpenCode 2.2.5 fails with "Plugin
must export a default definition with an id and an effect or setup
function. (cause: SchemaError(Missing key at ["default"]))". 2.x
imports a package's exports["."] (a plugin dir and an npm install
alike) and has no V1 path at all, so every package here that serves
models failed the same way, not only trae.

Every one of the 32 model packages now has:
- opencode2.mjs, exports["."]: a copy of scripts/opencode2.mjs, an
  adapter that runs the package's V1 plugins inside 2.x's promise
  API. Config providers become 2.x providers (package pinned so the
  aisdk sdk hook runs), V1 auth methods become integration methods
  (oauth auto/code with forms; api without authorize as a key method),
  V1 credentials are kept in metadata.v1, the loader's baseURL,
  apiKey, headers and fetch are applied through the sdk hook, an
  auth.set renewal is stored per credential, and credential.switched
  /updated reload the account's own model list.
- opencode.mjs, main: re-exports the V1 plugin functions only.
  OpenCode 1.x and magpie's host.js take main. On 1.x, trae 0.2.26's
  main exported a non-plugin (_internal) and 1.19.5 refused it ("Plugin
  export is not a function"); the new main fixes that too.
- both files in "files"; patch version bumped. devin already had 2.x
  and is unchanged.

scripts/check.mjs fails a model package whose 2.x entry has no default
{id, setup}, whose opencode2.mjs differs from the canonical one, whose
entry is missing from files, or whose main exports a non-plugin.

Run at this head (mote):
- HOME=$(mktemp -d) bun test, as CI runs it: 4900 pass, 0 fail
  (4914 tests, 374 files). A first run set globalThis.fetch for the
  whole process from opencode2.test and failed 528 tests in other
  packages; the block is now set and put back around that file's tests.
- bun scripts/check.mjs: every package passes. With the network
  blocked cloudflare-workers-ai fails on its fetched catalog, the same
  on origin/main.
- Real OpenCode 2.2.5 (npm opencode-ai), sandbox HOME, packed trae
  0.2.27: loads with no error; /api/integration lists trae-cn "Trae CN"
  and trae-global "Trae Global" with their browser sign-ins; `auth
  login` starts the trae.cn authorization and waits (cancelled there);
  with a fake credential POSTed to /api/credential, `opencode models`
  lists trae-cn's six models.
- Real 2.2.5 with a fake V1 key plugin through the same adapter
  against a local fake server: `auth login` with a key, `models`,
  `opencode run` reached /real/v1/chat/completions with the loader's
  "Bearer loader-sk-fake" and header, and printed the fake's reply;
  with @ai-sdk/anthropic it reached /real/v1/messages with x-api-key
  from the loader. This run found the key method missing after 2.x's
  second integration transform (a once-only flag outside the
  transform); fixed, and opencode2.test checks a rebuild.
- Real OpenCode 1.19.5, sandbox HOME, trae 0.2.27: `opencode models`
  lists trae-cn and trae-global models; 0.2.26 failed there.

Broken by hand (scratch copy, bun test scripts/ and check.mjs):
- trae's exports back to "./index.mjs": the trae 2.x test fails
  (default.id undefined), check fails trae with "OpenCode 2.x:
  ./index.mjs has no default {id, setup} (plugins#657)".
- zed's opencode2.mjs changed: the copies test fails, check fails zed
  "differs from scripts/opencode2.mjs".
- the key flag back outside the transform: the 1.x plugin test fails
  on the rebuilt sign-ins (key missing).

Not tried: a real sign-in with a real account on 2.x; Windows (the
reporter's OS); chat.headers, chat.params, usage and tool hooks, which
the adapter doesn't carry to 2.x; the tools/pages packages (calc,
goanyapi, layerfs, search1api, aelios, agent-console, chat,
fingerprint, nowledge-mem, kimi-desktop), which still load only on 1.x.
2026-11-03 13:54:26 +08:00
..

@magpie-community/opencode-cloudflare-workers-ai-auth

English | 简体中文 | 日本語

npm version License: MIT

A provider plugin for OpenCode 1.x and magpie that signs in to Cloudflare Workers AI with your API token and serves it as a provider every agent can use: 28+ models discovered live from models.dev, paid-only models flagged, and a daily Neurons allowance row in magpie quota.

Not affiliated with or endorsed by Cloudflare.

What you need

Item Where to find it
Cloudflare Account ID 32 hex characters, on the dashboard's Account home → right sidebar
Cloudflare API token Dashboard → avatar → My Profile → API Tokens → create a Custom Token with Account → Workers AI → Edit (add Account Settings → Read for the plan name and Account Analytics → Read for today's actually-used Neurons in magpie quota)
magpie 0.1.1110 or later, or OpenCode 1.x

No zones need to be selected on the token. Free and Paid plans both get 10,000 Neurons/day free; eight frontier models need Workers Paid or prepaid AI Gateway credits (flagged below).

The quota card shows the daily free allowance as a count and a Used / Left percentage, resetting at 00:00 UTC. Paid or unconfirmed plans keep this window informational; only a confirmed Free plan stops routing when it is spent.

Install in magpie

magpie plugin add @magpie-community/opencode-cloudflare-workers-ai-auth   # from npm
magpie plugin add /path/to/folder                                        # local copy

Then sign in:

magpie plugin login cloudflare-workers-ai
Prompt What to enter
API Token the Workers AI:Edit token from the table above
Account ID (optional) 32 hex characters; leave empty to use the plugin's accountId option
  • Each sign-in is one account. Sign in again to add another one (e.g. a Free account and a Paid account) — magpie fails over between them automatically; magpie quota shows each account's row separately.
  • Sign in again with the same Account ID to replace that account, e.g. to rotate the token. Sign-ins are kept in magpie's account storage; nothing is written outside magpie's config.
  • If Cloudflare refuses the token, magpie marks the account and asks you to sign in again.

Commands you'll actually use

magpie providers                                                     # is the provider signed in?
magpie provider test cloudflare-workers-ai                           # one small request (~300 ms in our sandbox)
magpie provider test cloudflare-workers-ai @cf/meta/llama-3.3-70b-instruct-fp8-fast
magpie quota                                                         # plan + Neurons/day window
magpie claude cloudflare-workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast   # run Claude Code on it
magpie codex cloudflare-workers-ai/@cf/openai/gpt-oss-120b                     # or Codex, same idea
magpie plugin logout cloudflare-workers-ai                           # remove the sign-in

Models are named cloudflare-workers-ai/@cf/<org>/<model> — the same IDs models.dev lists for the provider, refreshed hourly.

In OpenCode 1.x

opencode.json:

{ "plugin": ["@magpie-community/opencode-cloudflare-workers-ai-auth"] }

then opencode auth login → Cloudflare Workers AI (same two fields).

Paid-only models

Eight models are gated by Cloudflare behind Workers Paid or prepaid AI Gateway credits and are flagged (Paid) in the name — that is exactly how the model picker renders it:

@cf/moonshotai/kimi-k2.6, @cf/moonshotai/kimi-k2.7-code, @cf/zai-org/glm-5.2, @cf/zai-org/glm-5.3, @cf/zai-org/glm-5.3-flash, @cf/deepseek-ai/deepseek-v4-flash-0731, @cf/deepseek-ai/deepseek-v4.1-flash-1017, @cf/deepseek-ai/deepseek-v4-pro-0813

Everything else counts only against the shared 10,000 Neurons/day allowance (billed on a Paid account past it), so no model carries a free badge by design.

Options

magpie plugin options opencode-cloudflare-workers-ai-auth '{"accountId": "…", "discoveryTTLMs": 1800000}'
Option Default Notes
accountId — default Account ID used when a sign-in leaves it empty
apiKey — default token (the sign-in's key wins)
baseTemplate see below template target that the Account ID is interpolated into
discoveryTTLMs 3600000 catalog refresh interval

baseTemplate default (insertion target, Account ID interpolated per account):

https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1

Troubleshooting

You see Meaning
Model list empty, provider missing magpie plugin add . again, then magpie plugin --json: the error field says why (catalog fetch failed, or the token was refused)
magpie: the sign-in failed: 需要 API Token the token prompt was left empty — re-run magpie plugin login cloudflare-workers-ai
401 Authentication error from provider test the token was refused: check scope (Workers AI:Edit) or sign in again
403 from provider test token and Account ID don't match
400 No route for that URI if this survives a re-plugin add, it's a stale plugin version — the current proxy normalizes template URLs; file an issue
Model … absent from the current catalog the model id isn't in models.dev's cloudflare-workers-ai section; pick a listed model
400 on a (Paid) model your plan is Free and the model needs Workers Paid or AI Gateway credits
Quota row shows the fallback plan text the token lacks Account Settings → Read; the escalation is purely cosmetic — the Neurons window is still shown
Quota row shows 10,000 / 10,000 remaining but requests fail with used up your daily free allocation the token lacks Account Analytics → Read, so the used amount is unknown and magpie renders the allowance as untouched — grant that scope and refresh

Development

node --check src/index.mjs       # parse guard (the upstream bug that started this fork)
node --test                     # 23 tests, no network needed
sh scripts/sandbox.sh login      # magpie 0.1.1326 in a throwaway HOME
sh scripts/sandbox.sh test       # provider test inside that sandbox
sh scripts/sandbox.sh json       # plugin --json inside that sandbox

scripts/sandbox.sh keeps its HOME in .sandbox/ (git-ignored), so your real magpie, agents and sign-ins are never touched.

Plugin contract notes

  • Single async plugin function; config, auth.methods (one api method with password prompts), auth.loader, auth.usage and provider.models hooks
  • loader(getAuth, provider) reads the contract account shape (auth.key, auth.metadata.accountId); the legacy OpenCode multi-method shape works too
  • authorize returns { type: "success", key, metadata } / { type: "failed", error }
  • On catalog failure at load, config() degrades to an empty model list instead of throwing; the per-account provider.models serves the cached catalog after
  • One file, zero runtime dependencies, ESM, node >= 20

Version / license

Version 0.0.4
License MIT — Copyright (c) 2026 Guislabert
Runtime Node.js >= 20, ESM only
Author @Guislabert
中文 README.zh-CN.md

MIT © Guislabert