Workers AI usage now reads the day's actual Neurons from Cloudflare's GraphQL analytics, keeps the Used/Left toggle on the quota card, and stops the included free allowance from benching a Paid or unknown plan (`aside`). A 429 with code 3036 gets a Retry-After that runs to the next UTC midnight. Version 0.0.4; dist has 0.0.3, and npm has never had the package.
What was run, at reviewed head 38ddeb132ee6c00698952303fbc4949a84287779:
- `bun test packages/cloudflare-workers-ai` with a sandbox HOME/TMPDIR: 23/23 pass.
- `bun scripts/check.mjs`: ok.
- Merged with origin/main, the whole repo `bun test` with a sandbox HOME: 2900 pass, 3 skip, 0 fail across 245 files.
- Broke code and timing by hand:
- Removing the `!info.errors?.length` guard turns only the new partial-reply assertion red.
- A preload that moves Date.now() 700ms forward on every call fails the old (bdc302a) Retry-After test ("26820 vs ~26819"). The new bracketed test passes 3 of 3 under that preload.
- The earlier review at bdc302a covered the rest of the change. That includes nine mutations, each of which turns a test red.
Not tried:
- A real Paid account's billing and inference, as the author says too.
- A fetch call that straddles exactly 00:00:00 UTC. The Retry-After bracket would fail there, but only inside a window a few milliseconds long.
159 lines
8.5 KiB
Markdown
159 lines
8.5 KiB
Markdown
# @magpie-community/opencode-cloudflare-workers-ai-auth
|
||
|
||
[English](README.md) | [简体中文](README.zh-CN.md) | 日本語
|
||
|
||
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
||
[](https://opensource.org/licenses/MIT)
|
||
|
||
[OpenCode](https://opencode.ai) 1.x と [magpie](https://usemagpie.ai) 向けのプロバイダー
|
||
プラグイン。API トークンで **Cloudflare Workers AI** にサインインし、どのエージェントでも
|
||
使えるプロバイダーとして提供します。[models.dev](https://models.dev) から 28+ モデルを
|
||
リアルタイム取得、有料専用モデルには目印、
|
||
`magpie quota` には 1 日あたりの Neurons 余裕枠が表示されます。
|
||
|
||
> Cloudflare とは提携・承認関係にありません。
|
||
|
||
## 必要なもの
|
||
|
||
| 項目 | 在処 |
|
||
|---|---|
|
||
| Cloudflare Account ID | ダッシュボードの **Account home** → 右sidebar の 32 桁 hex |
|
||
| Cloudflare API トークン | ダッシュボード → アバター → **My Profile** → API Tokens → カスタムトークンを `Account → Workers AI → Edit` で作成(プラン名表示には `Account Settings → Read`、当日の使用済み Neurons 表示には `Account Analytics → Read` も追加) |
|
||
| magpie | 0.1.1110 以降、または OpenCode 1.x |
|
||
|
||
トークンに Zone の選択は不要。Free/Paid 両プランに **1 日 10,000 Neurons** の無料枠が
|
||
つき、8 つのフロンティアモデルだけ Workers Paid または AI Gateway クレジットが必要です(下記の目印)。
|
||
|
||
クォータカードは毎日の無料枠を数値と使用済み/残りの切り替え可能な割合で表示し、UTC 00:00 にリセットします。
|
||
Paid または未確認のプランでは表示のみで、Free と確認できたプランだけ無料枠を使い切るとルーティングを停止します。
|
||
|
||
## magpie へのインストール
|
||
|
||
```sh
|
||
magpie plugin add @magpie-community/opencode-cloudflare-workers-ai-auth # npm から
|
||
magpie plugin add /path/to/folder # ローカルコピー
|
||
```
|
||
|
||
サインイン:
|
||
|
||
```sh
|
||
magpie plugin login cloudflare-workers-ai
|
||
```
|
||
|
||
| 入力 | 内容 |
|
||
|---|---|
|
||
| API Token | 上の Workers AI:Edit トークン |
|
||
| Account ID(任意) | 32 桁 hex。空欄ならプラグイン options の `accountId` を使用 |
|
||
|
||
- サインイン 1 回 = アカウント 1 つ。もう一度サインインするとアカウントを追加
|
||
(例:Free と Paid を並べる)— magpie が自動フェイルオーバーし、`magpie quota`
|
||
には各行が分けて表示されます。
|
||
- 同じ Account IDで再サインインすると同じアカウントを差し替え(トークンをローテーション)。
|
||
- Cloudflare がトークンを拒んだら、magpie がそのアカウントに申し込み直しを要求します。
|
||
|
||
## よく使うコマンド
|
||
|
||
```sh
|
||
magpie providers # サインイン済みか
|
||
magpie provider test cloudflare-workers-ai # 小さな 1 リクエスト(サンドボックスで約 300 ms)
|
||
magpie provider test cloudflare-workers-ai @cf/meta/llama-3.3-70b-instruct-fp8-fast
|
||
magpie quota # プラン + Neurons/日
|
||
magpie claude cloudflare-workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast # Claude Code で使用
|
||
magpie codex cloudflare-workers-ai/@cf/openai/gpt-oss-120b # Codex でも同様
|
||
magpie plugin logout cloudflare-workers-ai # サインインを削除
|
||
```
|
||
|
||
モデル名は `cloudflare-workers-ai/@cf/<org>/<model>` — [models.dev](https://models.dev) の
|
||
カタログ ID そのままで、1 時間ごとに更新されます。
|
||
|
||
### OpenCode 1.x での使い方
|
||
|
||
`opencode.json`:
|
||
|
||
```json
|
||
{ "plugin": ["@magpie-community/opencode-cloudflare-workers-ai-auth"] }
|
||
```
|
||
|
||
その後 `opencode auth login` → Cloudflare Workers AI(同じ 2 項目)。
|
||
|
||
## 有料専用モデル
|
||
|
||
Workers Paid または AI Gateway クレジットが必要な 8 モデルは名前に **(Paid)** を付けます:
|
||
|
||
`@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` です。
|
||
|
||
それ以外のモデルは 1 日 10,000 Neurons の共有枠に含まれ(Paid では超過分に課金)、free バッジは付けません。
|
||
|
||
## オプション
|
||
|
||
```sh
|
||
magpie plugin options opencode-cloudflare-workers-ai-auth '{"accountId": "…", "discoveryTTLMs": 1800000}'
|
||
```
|
||
|
||
| オプション | 既定 | 説明 |
|
||
|---|---|---|
|
||
| `accountId` | — | サインインで空欄のときの既定 Account ID |
|
||
| `apiKey` | — | 既定トークン(サインインのキーが優先) |
|
||
| `baseTemplate` | 下記参照 | 差し込み先の URL テンプレート |
|
||
| `discoveryTTLMs` | `3600000` | カタログ再取得間隔 |
|
||
|
||
**`baseTemplate` のデフォルト**(差し込み先、Account ID はアカウント毎に注入):
|
||
|
||
```
|
||
https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/ai/v1
|
||
```
|
||
|
||
## トラブルシューティング
|
||
|
||
| 表示 | 意味 |
|
||
|---|---|
|
||
| モデル一覧が空・プロバイダーが見えない | `magpie plugin add .` をやり直し、`magpie plugin --json` の `error` を確認(カタログ取得失敗、トークン拒否) |
|
||
| `magpie: the sign-in failed: 需要 API Token` | トークン入力が空 — `magpie plugin login cloudflare-workers-ai` をやり直す |
|
||
| `provider test` で 401 `Authentication error` | トークン拒否:権限(`Workers AI:Edit`)を確認し再サインイン |
|
||
| `provider test` で 403 | トークンと Account ID の不一致 |
|
||
| 400 `No route for that URI` | `plugin add` 再実行後も出るなら、古いプラグインが残っている可能性 — 最新版 URL 正規化で解決済み、要 issue |
|
||
| `Model … absent from the current catalog` | models.dev の cloudflare-workers-ai セクションにない ID — 一覧から選ぶ |
|
||
| (Paid) モデルで 400 | プランが Free:Workers Paid か AI Gateway クレジットが必要 |
|
||
| quota 行がプラン名の代償テキスト | トークンに `Account Settings → Read` がない — 表示だけの問題で Neurons 枠は出る |
|
||
| quota 行が 10,000 / 10,000 残りと表示するのにリクエストが used up your daily free allocation で失敗する | トークンに `Account Analytics → Read` がなく使用量を取得できないため magpie が枠を未使用として描画する — スコープを追加して更新 |
|
||
|
||
## 開発
|
||
|
||
```sh
|
||
node --check src/index.mjs # パース検査(元リポジトリのバグ)
|
||
node --test # 23 テスト、ネットワーク不要
|
||
sh scripts/sandbox.sh login # magpie 0.1.1326 を使い捨て HOME で
|
||
sh scripts/sandbox.sh test # サンドボックス内で provider test
|
||
sh scripts/sandbox.sh json # サンドボックス内で plugin --json
|
||
```
|
||
|
||
`scripts/sandbox.sh` の HOME は `.sandbox/`(git 無視)なので、本物の magpie・エージェント・
|
||
サインインには触れません。
|
||
|
||
## プラグイン契約のメモ
|
||
|
||
- 単一 async プラグイン関数。`config`、`auth.methods`(`password` prompts 付きの 1 つの
|
||
`api` メソッド)、`auth.loader`、`auth.usage`、`provider.models` を書き出す
|
||
- `loader(getAuth, provider)` は契約アカウント形(`auth.key`、`auth.metadata.accountId`)を
|
||
読む。OpenCode の旧マルチメソッド形も受ける
|
||
- `authorize` は `{ type: "success", key, metadata }` / `{ type: "failed", error }` を返す
|
||
- カタログ取得失敗時、`config()` は空のモデル一覧に落として例外化しない。その後アカウント毎の
|
||
`provider.models` がキャッシュ済みカタログを提供
|
||
- 単一ファイル、実行時依存ゼロ、ESM、`node >= 20`
|
||
|
||
## バージョン / ライセンス
|
||
|
||
| | |
|
||
|---|---|
|
||
| **バージョン** | 0.0.4 |
|
||
| **ライセンス** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert |
|
||
| **ランタイム** | Node.js >= 20、ESM のみ |
|
||
| **作者** | [@Guislabert](https://github.com/Guislabert) |
|
||
| **言語** | [English](README.md) · [简体中文](README.zh-CN.md) |
|
||
|
||
MIT © [Guislabert](https://github.com/Guislabert)
|