diff --git a/packages/cloudflare-workers-ai/README.ja.md b/packages/cloudflare-workers-ai/README.ja.md index 7b028b3..b8b680a 100644 --- a/packages/cloudflare-workers-ai/README.ja.md +++ b/packages/cloudflare-workers-ai/README.ja.md @@ -2,7 +2,7 @@ [English](README.md) | [简体中文](README.zh-CN.md) | 日本語 -[![npm version](https://img.shields.io/badge/npm%20version-0.0.1-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) +[![npm version](https://img.shields.io/badge/npm%20version-0.0.2-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) [![License: MIT](https://img.shields.io/badge/License-MIT-FBBC05.svg)](https://opensource.org/licenses/MIT) [OpenCode](https://opencode.ai) 1.x と [magpie](https://usemagpie.ai) 向けのプロバイダー @@ -145,7 +145,7 @@ sh scripts/sandbox.sh json # サンドボックス内で plugin --json | | | |---|---| -| **バージョン** | [`0.0.1`](CHANGELOG.md) | +| **バージョン** | 0.0.2 | | **ライセンス** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert | | **ランタイム** | Node.js >= 20、ESM のみ | | **作者** | [@Guislabert](https://github.com/Guislabert) | diff --git a/packages/cloudflare-workers-ai/README.md b/packages/cloudflare-workers-ai/README.md index 977f063..66b12d2 100644 --- a/packages/cloudflare-workers-ai/README.md +++ b/packages/cloudflare-workers-ai/README.md @@ -2,7 +2,7 @@ English | [简体中文](README.zh-CN.md) | [日本語](README.ja.md) -[![npm version](https://img.shields.io/badge/npm%20version-0.0.1-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) +[![npm version](https://img.shields.io/badge/npm%20version-0.0.2-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) [![License: MIT](https://img.shields.io/badge/License-MIT-FBBC05.svg)](https://opensource.org/licenses/MIT) A provider plugin for [OpenCode](https://opencode.ai) 1.x and @@ -152,7 +152,7 @@ magpie, agents and sign-ins are never touched. | | | |---|---| -| **Version** | [`0.0.1`](CHANGELOG.md) | +| **Version** | 0.0.2 | | **License** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert | | **Runtime** | Node.js >= 20, ESM only | | **Author** | [@Guislabert](https://github.com/Guislabert) | diff --git a/packages/cloudflare-workers-ai/README.zh-CN.md b/packages/cloudflare-workers-ai/README.zh-CN.md index d8fa1fb..9c8674f 100644 --- a/packages/cloudflare-workers-ai/README.zh-CN.md +++ b/packages/cloudflare-workers-ai/README.zh-CN.md @@ -2,7 +2,7 @@ [English](README.md) | 简体中文 | [日本語](README.ja.md) -[![npm version](https://img.shields.io/badge/npm%20version-0.0.1-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) +[![npm version](https://img.shields.io/badge/npm%20version-0.0.2-EA4335.svg)](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth) [![License: MIT](https://img.shields.io/badge/License-MIT-FBBC05.svg)](https://opensource.org/licenses/MIT) 面向 [OpenCode](https://opencode.ai) 1.x 和 [magpie](https://usemagpie.ai) 的供应商插件: @@ -145,7 +145,7 @@ sh scripts/sandbox.sh json # 沙盒内 plugin --json | | | |---|---| -| **版本** | [`0.0.1`](CHANGELOG.md) | +| **版本** | 0.0.2 | | **许可证** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert | | **运行时** | Node.js >= 20,仅 ESM | | **作者** | [@Guislabert](https://github.com/Guislabert) | diff --git a/packages/cloudflare-workers-ai/package.json b/packages/cloudflare-workers-ai/package.json index 12e97f5..132c46b 100644 --- a/packages/cloudflare-workers-ai/package.json +++ b/packages/cloudflare-workers-ai/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cloudflare-workers-ai-auth", - "version": "0.0.1", + "version": "0.0.2", "private": false, "type": "module", "main": "./index.mjs", diff --git a/packages/cloudflare-workers-ai/src/index.mjs b/packages/cloudflare-workers-ai/src/index.mjs index d35efb8..60105d2 100644 --- a/packages/cloudflare-workers-ai/src/index.mjs +++ b/packages/cloudflare-workers-ai/src/index.mjs @@ -138,6 +138,13 @@ function createDiscovery({ load, initial = {}, ttlMs = DEFAULT_DISCOVERY_TTL_MS, }; } +function paidName(id, name) { + // Name-flag the models Cloudflare serves only behind Workers Paid / AI + // Gateway credits. No `free` flag: the shared 10,000/day Neurons allowance + // is not the same thing as a free model. + return PAID_ONLY_MODELS.has(id) ? `${name} (Paid)` : name; +} + export async function CloudflareWorkersAIPlugin(_input, options = {}) { const accountId = options.accountId ?? ""; const apiKey = options.apiKey ?? ""; @@ -181,7 +188,7 @@ export async function CloudflareWorkersAIPlugin(_input, options = {}) { api: baseURL, models: Object.fromEntries( Object.entries(models).map(([id, m]) => [id, { - name: PAID_ONLY_MODELS.has(id) ? `${m.name} (Paid)` : m.name, + name: paidName(id, m.name), limit: m.limit, reasoning: m.capabilities.reasoning, tool_call: m.capabilities.toolcall, @@ -336,7 +343,10 @@ export async function CloudflareWorkersAIPlugin(_input, options = {}) { // resets daily 00:00 UTC) and best-effort plan detection. const auth = await getAuth?.(); const acct = auth?.metadata?.accountId || accountId; - if (!acct) return { plan: "Workers Free / Paid (confirm in the dashboard)", windows: [] }; + if (!acct || !/^[0-9a-f]{32}$/i.test(acct)) { + // same rule loader() applies; an invalid ID must not reach the API path + return { plan: "Workers Free / Paid (confirm in the dashboard)", windows: [] }; + } let plan = "Workers Free / Paid (confirm in the dashboard)"; try { const res = await fetchImpl(`https://api.cloudflare.com/client/v4/accounts/${acct}`, { @@ -381,7 +391,7 @@ export async function CloudflareWorkersAIPlugin(_input, options = {}) { return Object.fromEntries( Object.entries(models).map(([id, m]) => [ id, - PAID_ONLY_MODELS.has(id) ? { ...m, name: `${m.name} (Paid)` } : m, + PAID_ONLY_MODELS.has(id) ? { ...m, name: paidName(id, m.name) } : m, ]), ); }, diff --git a/packages/cloudflare-workers-ai/test/plugin.test.mjs b/packages/cloudflare-workers-ai/test/plugin.test.mjs index 74cf238..039bf9f 100644 --- a/packages/cloudflare-workers-ai/test/plugin.test.mjs +++ b/packages/cloudflare-workers-ai/test/plugin.test.mjs @@ -280,3 +280,48 @@ test("fetch normalizes template-account URLs onto the effective account base", a await r.fetch(`${r.baseURL}/embeddings`, { method: "POST", body: "{}" }); assert.match(forwarded, /\/embeddings$/); }); + +test("checkedURL rejects non-HTTP(S) baseTemplate at init", async () => { + await assert.rejects( + async () => + CloudflareWorkersAIPlugin({}, { + accountId: ACCOUNT_ID, + baseTemplate: "javascript:alert(1)", + fetchImpl: async () => new Response(JSON.stringify(fakeCatalog())), + }), + /plain HTTP\(S\) base URL/ + ); +}); + +test("interpolateTemplate keeps unknown placeholders intact", async () => { + const plugin = await CloudflareWorkersAIPlugin({}, { + baseTemplate: "https://example.com/${UNKNOWN_KEY}/ai/v1", + fetchImpl: async () => new Response("{}"), + }); + const cfg = {}; + await plugin.config(cfg); + assert.equal( + cfg.provider["cloudflare-workers-ai"].api, + "https://example.com/${UNKNOWN_KEY}/ai/v1", + "an unknown placeholder must survive interpolation untouched" + ); +}); + +test("usage returns the fallback plan for an invalid accountId (no API call)", async () => { + let apiCalled = ""; + const o = opts(); + o.fetchImpl = async (url) => { + if (String(url).includes("models.dev")) { + return new Response(JSON.stringify(fakeCatalog())); + } + apiCalled = url; + return new Response("{}", { status: 200 }); + }; + const plugin = await CloudflareWorkersAIPlugin({}, o); + const u = await plugin.auth.usage(async () => ({ + key: "tok", + metadata: { accountId: "..%2Fnot-hex" }, + })); + assert.equal(u.windows.length, 0, "invalid Account ID must not reach the API path"); + assert.equal(apiCalled, "", "no /accounts/ request was made"); +});