cloudflare-workers-ai 0.0.2: usage() checks the Account ID, one (Paid) rule, parser tests (#327)
Follow-up to #326 (Guislabert). usage() applies loader()'s 32-hex Account ID check, so an invalid id returns the fallback plan row and never reaches the /accounts/{id} path. The "(Paid)" name rule lives in one paidName helper. Two parser tests cover a non-HTTP(S) baseTemplate and an unknown placeholder. Version 0.0.2. Run at the reviewed head afd7878 (based on origin/main): - bun scripts/check.mjs cloudflare-workers-ai: ok - node --test in the package under a temp HOME: 15 of 15 pass - main's src/index.mjs with the new tests: the invalid-Account-ID usage test fails (14 of 15), so it guards the fix - afd7878 against 114900a: README CHANGELOG links dropped, version 0.0.2; src and tests unchanged Not tried here: a real Cloudflare account (the author's run on 0.0.1 is on #326).
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
[English](README.md) | [简体中文](README.zh-CN.md) | 日本語
|
[English](README.md) | [简体中文](README.zh-CN.md) | 日本語
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
[OpenCode](https://opencode.ai) 1.x と [magpie](https://usemagpie.ai) 向けのプロバイダー
|
[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 |
|
| **ライセンス** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert |
|
||||||
| **ランタイム** | Node.js >= 20、ESM のみ |
|
| **ランタイム** | Node.js >= 20、ESM のみ |
|
||||||
| **作者** | [@Guislabert](https://github.com/Guislabert) |
|
| **作者** | [@Guislabert](https://github.com/Guislabert) |
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
English | [简体中文](README.zh-CN.md) | [日本語](README.ja.md)
|
English | [简体中文](README.zh-CN.md) | [日本語](README.ja.md)
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
A provider plugin for [OpenCode](https://opencode.ai) 1.x and
|
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 |
|
| **License** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert |
|
||||||
| **Runtime** | Node.js >= 20, ESM only |
|
| **Runtime** | Node.js >= 20, ESM only |
|
||||||
| **Author** | [@Guislabert](https://github.com/Guislabert) |
|
| **Author** | [@Guislabert](https://github.com/Guislabert) |
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
[English](README.md) | 简体中文 | [日本語](README.ja.md)
|
[English](README.md) | 简体中文 | [日本語](README.ja.md)
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
[](https://www.npmjs.com/package/@magpie-community/opencode-cloudflare-workers-ai-auth)
|
||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
面向 [OpenCode](https://opencode.ai) 1.x 和 [magpie](https://usemagpie.ai) 的供应商插件:
|
面向 [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 |
|
| **许可证** | [MIT](LICENSE) — Copyright (c) 2026 Guislabert |
|
||||||
| **运行时** | Node.js >= 20,仅 ESM |
|
| **运行时** | Node.js >= 20,仅 ESM |
|
||||||
| **作者** | [@Guislabert](https://github.com/Guislabert) |
|
| **作者** | [@Guislabert](https://github.com/Guislabert) |
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@magpie-community/opencode-cloudflare-workers-ai-auth",
|
"name": "@magpie-community/opencode-cloudflare-workers-ai-auth",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"private": false,
|
"private": false,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./index.mjs",
|
"main": "./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 = {}) {
|
export async function CloudflareWorkersAIPlugin(_input, options = {}) {
|
||||||
const accountId = options.accountId ?? "";
|
const accountId = options.accountId ?? "";
|
||||||
const apiKey = options.apiKey ?? "";
|
const apiKey = options.apiKey ?? "";
|
||||||
@@ -181,7 +188,7 @@ export async function CloudflareWorkersAIPlugin(_input, options = {}) {
|
|||||||
api: baseURL,
|
api: baseURL,
|
||||||
models: Object.fromEntries(
|
models: Object.fromEntries(
|
||||||
Object.entries(models).map(([id, m]) => [id, {
|
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,
|
limit: m.limit,
|
||||||
reasoning: m.capabilities.reasoning,
|
reasoning: m.capabilities.reasoning,
|
||||||
tool_call: m.capabilities.toolcall,
|
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.
|
// resets daily 00:00 UTC) and best-effort plan detection.
|
||||||
const auth = await getAuth?.();
|
const auth = await getAuth?.();
|
||||||
const acct = auth?.metadata?.accountId || accountId;
|
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)";
|
let plan = "Workers Free / Paid (confirm in the dashboard)";
|
||||||
try {
|
try {
|
||||||
const res = await fetchImpl(`https://api.cloudflare.com/client/v4/accounts/${acct}`, {
|
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(
|
return Object.fromEntries(
|
||||||
Object.entries(models).map(([id, m]) => [
|
Object.entries(models).map(([id, m]) => [
|
||||||
id,
|
id,
|
||||||
PAID_ONLY_MODELS.has(id) ? { ...m, name: `${m.name} (Paid)` } : m,
|
PAID_ONLY_MODELS.has(id) ? { ...m, name: paidName(id, m.name) } : m,
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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: "{}" });
|
await r.fetch(`${r.baseURL}/embeddings`, { method: "POST", body: "{}" });
|
||||||
assert.match(forwarded, /\/embeddings$/);
|
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");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user