magpie-blaze-api 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -2
- package/index.mjs +53 -0
- package/opencode.mjs +3 -0
- package/opencode2.mjs +634 -0
- package/package.json +60 -4
- package/src/constants.mjs +157 -0
- package/src/fetch.mjs +126 -0
- package/src/models.mjs +354 -0
- package/src/plugin.mjs +331 -0
- package/src/usage.mjs +337 -0
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magpie-blaze-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode & Magpie provider plugin for BlazeAPI (Claude, GPT, Gemini, DeepSeek, Grok, Kimi, Qwen & 200+ models via OpenAI & Anthropic APIs)",
|
|
5
|
+
"homepage": "https://blazeapi.org",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./opencode.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./opencode2.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.mjs",
|
|
13
|
+
"opencode.mjs",
|
|
14
|
+
"opencode2.mjs",
|
|
15
|
+
"src/",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"opencode",
|
|
21
|
+
"opencode-plugin",
|
|
22
|
+
"opencode-provider",
|
|
23
|
+
"magpie",
|
|
24
|
+
"magpie-plugin",
|
|
25
|
+
"blazeapi",
|
|
26
|
+
"blaze-api",
|
|
27
|
+
"provider",
|
|
28
|
+
"auth"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/suwupnil/magpie-blaze-api.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/suwupnil/magpie-blaze-api/issues"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node --test test/*.test.mjs",
|
|
40
|
+
"lint": "node --check index.mjs opencode.mjs opencode2.mjs src/*.mjs test/*.test.mjs"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20"
|
|
44
|
+
},
|
|
45
|
+
"magpie": {
|
|
46
|
+
"name": "BlazeAPI",
|
|
47
|
+
"icon": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2064%2064%22%20fill%3D%22none%22%3E%3Crect%20width%3D%2264%22%20height%3D%2264%22%20rx%3D%2214%22%20fill%3D%22%23111113%22%2F%3E%3Cpath%20d%3D%22M35.5%2011C36.6%2019.2%2044%2023.6%2044%2033.4C44%2042.3%2037.6%2049%2029.8%2049C22.4%2049%2017%2043.3%2017%2035.8C17%2029.5%2021.1%2024.8%2024.8%2021.2C24.5%2025.1%2026.2%2027.6%2028.4%2028.4C26.9%2022.7%2029.7%2015.6%2035.5%2011Z%22%20fill%3D%22url(%23g)%22%2F%3E%3Cpath%20d%3D%22M31.6%2028.2C34.2%2031.8%2036.2%2034.4%2036.2%2038.2C36.2%2041.9%2033.4%2044.8%2029.9%2044.8C26.7%2044.8%2024.3%2042.3%2024.3%2038.9C24.3%2035.6%2027.1%2032.8%2031.6%2028.2Z%22%20fill%3D%22%23FFF7ED%22%2F%3E%3Cdefs%3E%3ClinearGradient%20id%3D%22g%22%20x1%3D%2217%22%20y1%3D%2211%22%20x2%3D%2244%22%20y2%3D%2249%22%20gradientUnits%3D%22userSpaceOnUse%22%3E%3Cstop%20stop-color%3D%22%23FB923C%22%2F%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22%23EA580C%22%2F%3E%3C/linearGradient%3E%3C/defs%3E%3C/svg%3E",
|
|
48
|
+
"maxConcurrency": 8,
|
|
49
|
+
"summary": {
|
|
50
|
+
"en": "BlazeAPI provider for Claude, GPT, Gemini, DeepSeek and 200+ models via OpenAI & Anthropic APIs.",
|
|
51
|
+
"zh": "BlazeAPI 统一网关:支持 Claude、GPT、Gemini、DeepSeek 等 200+ 模型及免费额度。"
|
|
52
|
+
},
|
|
53
|
+
"options": {
|
|
54
|
+
"scope": "available",
|
|
55
|
+
"protocol": "auto",
|
|
56
|
+
"providerRouting": "",
|
|
57
|
+
"includeSearchModels": true,
|
|
58
|
+
"includeTokV2": true,
|
|
59
|
+
"includeFree": true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export const PROVIDER_ID = "blazeapi";
|
|
2
|
+
export const PROVIDER_NAME = "BlazeAPI";
|
|
3
|
+
export const DEFAULT_BASE_URL = "https://api.blazeapi.org/paid/v1";
|
|
4
|
+
export const DEFAULT_DISCOVERY_TTL_MS = 15 * 60 * 1000;
|
|
5
|
+
export const DEFAULT_ANTHROPIC_VERSION = "2023-06-01";
|
|
6
|
+
|
|
7
|
+
export const SDK = {
|
|
8
|
+
chat: "@ai-sdk/openai-compatible",
|
|
9
|
+
responses: "@ai-sdk/openai",
|
|
10
|
+
anthropic: "@ai-sdk/anthropic",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// Compact inline SVG icon (< 1 KB, well within Magpie's 1 MB limit).
|
|
14
|
+
export const BLAZE_ICON_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" fill="none"><rect width="64" height="64" rx="14" fill="#111113"/><path d="M35.5 11C36.6 19.2 44 23.6 44 33.4C44 42.3 37.6 49 29.8 49C22.4 49 17 43.3 17 35.8C17 29.5 21.1 24.8 24.8 21.2C24.5 25.1 26.2 27.6 28.4 28.4C26.9 22.7 29.7 15.6 35.5 11Z" fill="url(#g)"/><path d="M31.6 28.2C34.2 31.8 36.2 34.4 36.2 38.2C36.2 41.9 33.4 44.8 29.9 44.8C26.7 44.8 24.3 42.3 24.3 38.9C24.3 35.6 27.1 32.8 31.6 28.2Z" fill="#FFF7ED"/><defs><linearGradient id="g" x1="17" y1="11" x2="44" y2="49" gradientUnits="userSpaceOnUse"><stop stop-color="#FB923C"/><stop offset="1" stop-color="#EA580C"/></linearGradient></defs></svg>`;
|
|
15
|
+
|
|
16
|
+
export const BLAZE_ICON_DATA_URI = `data:image/svg+xml;utf8,${encodeURIComponent(BLAZE_ICON_SVG)}`;
|
|
17
|
+
|
|
18
|
+
// Curated seed catalog available before sign-in or when offline.
|
|
19
|
+
export const SEED_MODELS = [
|
|
20
|
+
{
|
|
21
|
+
id: "claude-opus-5-5",
|
|
22
|
+
name: "Claude Opus 5.5",
|
|
23
|
+
protocol: "anthropic",
|
|
24
|
+
context: 1000000,
|
|
25
|
+
output: 128000,
|
|
26
|
+
vision: true,
|
|
27
|
+
reasoning: true,
|
|
28
|
+
cost: { input: 0.72, output: 3.6, cache_read: 0.612 },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: "claude-sonnet-5-5",
|
|
32
|
+
name: "Claude Sonnet 5.5",
|
|
33
|
+
protocol: "anthropic",
|
|
34
|
+
context: 1000000,
|
|
35
|
+
output: 128000,
|
|
36
|
+
vision: true,
|
|
37
|
+
reasoning: true,
|
|
38
|
+
cost: { input: 0.4056, output: 2.03, cache_read: 0.34476 },
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "claude-sonnet-5",
|
|
42
|
+
name: "Claude Sonnet 5",
|
|
43
|
+
protocol: "anthropic",
|
|
44
|
+
context: 1000000,
|
|
45
|
+
output: 128000,
|
|
46
|
+
vision: true,
|
|
47
|
+
reasoning: true,
|
|
48
|
+
cost: { input: 0.1872, output: 0.936, cache_read: 0.15912 },
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "claude-opus-4-8",
|
|
52
|
+
name: "Claude Opus 4.8",
|
|
53
|
+
protocol: "anthropic",
|
|
54
|
+
context: 200000,
|
|
55
|
+
output: 8192,
|
|
56
|
+
vision: true,
|
|
57
|
+
reasoning: true,
|
|
58
|
+
cost: { input: 0.43264, output: 2.08, cache_read: 0.367744 },
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: "claude-haiku-4.5",
|
|
62
|
+
name: "Claude Haiku 4.5",
|
|
63
|
+
protocol: "anthropic",
|
|
64
|
+
context: 200000,
|
|
65
|
+
output: 64000,
|
|
66
|
+
vision: true,
|
|
67
|
+
reasoning: true,
|
|
68
|
+
cost: { input: 0.0624, output: 0.313664, cache_read: 0.05304 },
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "gpt-5.5",
|
|
72
|
+
name: "GPT-5.5",
|
|
73
|
+
protocol: "responses",
|
|
74
|
+
context: 1000000,
|
|
75
|
+
output: 128000,
|
|
76
|
+
vision: true,
|
|
77
|
+
reasoning: true,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "gpt-5.4",
|
|
81
|
+
name: "GPT-5.4",
|
|
82
|
+
protocol: "responses",
|
|
83
|
+
context: 1000000,
|
|
84
|
+
output: 128000,
|
|
85
|
+
vision: true,
|
|
86
|
+
reasoning: true,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
id: "gpt-5.3-codex",
|
|
90
|
+
name: "GPT-5.3 Codex",
|
|
91
|
+
protocol: "responses",
|
|
92
|
+
context: 400000,
|
|
93
|
+
output: 128000,
|
|
94
|
+
vision: true,
|
|
95
|
+
reasoning: true,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: "gpt-4o",
|
|
99
|
+
name: "GPT-4o",
|
|
100
|
+
protocol: "chat",
|
|
101
|
+
context: 128000,
|
|
102
|
+
output: 16384,
|
|
103
|
+
vision: false,
|
|
104
|
+
reasoning: false,
|
|
105
|
+
cost: { input: 0.6, output: 2.4, cache_read: 0.51 },
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: "gemini-2.5-pro",
|
|
109
|
+
name: "Gemini 2.5 Pro",
|
|
110
|
+
protocol: "chat",
|
|
111
|
+
context: 1048576,
|
|
112
|
+
output: 65536,
|
|
113
|
+
vision: true,
|
|
114
|
+
reasoning: true,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: "gemini-2.5-flash",
|
|
118
|
+
name: "Gemini 2.5 Flash",
|
|
119
|
+
protocol: "chat",
|
|
120
|
+
context: 1048576,
|
|
121
|
+
output: 65535,
|
|
122
|
+
vision: true,
|
|
123
|
+
reasoning: true,
|
|
124
|
+
cost: { input: 0.072, output: 0.6, cache_read: 0.0612 },
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
id: "deepseek-v3.2",
|
|
128
|
+
name: "DeepSeek V3.2",
|
|
129
|
+
protocol: "chat",
|
|
130
|
+
context: 160000,
|
|
131
|
+
output: 32768,
|
|
132
|
+
vision: false,
|
|
133
|
+
reasoning: true,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: "free/deepseek-v4.1-flash",
|
|
137
|
+
name: "DeepSeek V4.1 Flash (Free)",
|
|
138
|
+
protocol: "chat",
|
|
139
|
+
context: 128000,
|
|
140
|
+
output: 16000,
|
|
141
|
+
vision: false,
|
|
142
|
+
reasoning: true,
|
|
143
|
+
free: true,
|
|
144
|
+
cost: { input: 0, output: 0, cache_read: 0 },
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
id: "free/gemini-2.5-flash",
|
|
148
|
+
name: "Gemini 2.5 Flash (Free)",
|
|
149
|
+
protocol: "chat",
|
|
150
|
+
context: 131072,
|
|
151
|
+
output: 16000,
|
|
152
|
+
vision: true,
|
|
153
|
+
reasoning: true,
|
|
154
|
+
free: true,
|
|
155
|
+
cost: { input: 0, output: 0, cache_read: 0 },
|
|
156
|
+
},
|
|
157
|
+
];
|
package/src/fetch.mjs
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { DEFAULT_ANTHROPIC_VERSION } from "./constants.mjs";
|
|
2
|
+
|
|
3
|
+
function urlStringOf(input) {
|
|
4
|
+
if (typeof input === "string") return input;
|
|
5
|
+
if (input instanceof URL) return input.href;
|
|
6
|
+
if (input && typeof input === "object" && typeof input.url === "string") {
|
|
7
|
+
return input.url;
|
|
8
|
+
}
|
|
9
|
+
return String(input);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function isAnthropicPath(urlStr) {
|
|
13
|
+
try {
|
|
14
|
+
const pathname = new URL(urlStr).pathname;
|
|
15
|
+
return /\/messages(\/count_tokens)?\/?$/.test(pathname);
|
|
16
|
+
} catch {
|
|
17
|
+
return /\/messages(\/count_tokens)?(\?|$)/.test(urlStr);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function withSignInHeader(res, bodyText, signIn) {
|
|
22
|
+
const headers = new Headers(res.headers);
|
|
23
|
+
headers.delete("content-length");
|
|
24
|
+
headers.delete("content-encoding");
|
|
25
|
+
headers.set("X-Magpie-Sign-In", signIn);
|
|
26
|
+
return new Response(bodyText, {
|
|
27
|
+
status: res.status,
|
|
28
|
+
statusText: res.statusText,
|
|
29
|
+
headers,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function sanitizeProviderRouting(raw) {
|
|
34
|
+
if (typeof raw !== "string") return "";
|
|
35
|
+
const cleaned = raw.replace(/[\r\n]+/g, "").trim();
|
|
36
|
+
if (!cleaned) return "";
|
|
37
|
+
// Allow BlazeAPI routing markers ("cheapest", "fastest", "best") or comma-separated provider IDs.
|
|
38
|
+
if (!/^[a-z0-9._-]+(?:\s*,\s*[a-z0-9._-]+)*$/i.test(cleaned)) return "";
|
|
39
|
+
return cleaned.replace(/\s*,\s*/g, ",");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function classifyAuthError(status, bodyText) {
|
|
43
|
+
const lower = String(bodyText || "").toLowerCase();
|
|
44
|
+
if (status === 401) {
|
|
45
|
+
const mentionsModelOnly =
|
|
46
|
+
(lower.includes("model_not_found") ||
|
|
47
|
+
lower.includes("not available on your plan") ||
|
|
48
|
+
lower.includes("model is not available") ||
|
|
49
|
+
lower.includes("unknown model") ||
|
|
50
|
+
lower.includes("模型不支持")) &&
|
|
51
|
+
!/(api[\s_-]?key|token|auth|credential|unauthorized|revoked|expired)/.test(lower);
|
|
52
|
+
return mentionsModelOnly ? "kept" : "expired";
|
|
53
|
+
}
|
|
54
|
+
if (status === 403) {
|
|
55
|
+
const explicitlyInvalidKey =
|
|
56
|
+
/(invalid|revoked|expired|missing)\s+(api[\s_-]?key|token)/.test(lower) ||
|
|
57
|
+
lower.includes("authentication_error");
|
|
58
|
+
return explicitlyInvalidKey ? "expired" : "kept";
|
|
59
|
+
}
|
|
60
|
+
return "kept";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function createBlazeFetch({
|
|
64
|
+
apiKey,
|
|
65
|
+
providerRouting = "",
|
|
66
|
+
fetchImpl = fetch,
|
|
67
|
+
} = {}) {
|
|
68
|
+
const routingHeader = sanitizeProviderRouting(providerRouting);
|
|
69
|
+
return async function blazeFetch(input, init = {}) {
|
|
70
|
+
const req = typeof Request !== "undefined" && input instanceof Request ? input : null;
|
|
71
|
+
const urlStr = urlStringOf(input);
|
|
72
|
+
const headers = new Headers(init?.headers ?? req?.headers ?? undefined);
|
|
73
|
+
|
|
74
|
+
if (isAnthropicPath(urlStr)) {
|
|
75
|
+
headers.delete("authorization");
|
|
76
|
+
headers.set("x-api-key", apiKey);
|
|
77
|
+
if (!headers.has("anthropic-version")) {
|
|
78
|
+
headers.set("anthropic-version", DEFAULT_ANTHROPIC_VERSION);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
headers.delete("x-api-key");
|
|
82
|
+
headers.set("authorization", `Bearer ${apiKey}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (routingHeader && !headers.has("x-blaze-provider")) {
|
|
86
|
+
headers.set("x-blaze-provider", routingHeader);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const nextInit = {
|
|
90
|
+
...init,
|
|
91
|
+
headers,
|
|
92
|
+
...(req && init?.method === undefined ? { method: req.method } : {}),
|
|
93
|
+
...(req && init?.signal === undefined ? { signal: req.signal } : {}),
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
if (
|
|
97
|
+
req &&
|
|
98
|
+
init?.body === undefined &&
|
|
99
|
+
req.body !== null &&
|
|
100
|
+
req.method !== "GET" &&
|
|
101
|
+
req.method !== "HEAD"
|
|
102
|
+
) {
|
|
103
|
+
nextInit.body = await req.arrayBuffer();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const res = await fetchImpl(urlStr, nextInit);
|
|
107
|
+
|
|
108
|
+
if (res.status === 401 || res.status === 403) {
|
|
109
|
+
const bodyText = await res.text().catch(() => "");
|
|
110
|
+
const signIn = classifyAuthError(res.status, bodyText);
|
|
111
|
+
return withSignInHeader(res, bodyText, signIn);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (res.status === 402 || res.status === 429) {
|
|
115
|
+
const headersOut = new Headers(res.headers);
|
|
116
|
+
headersOut.set("X-Magpie-Sign-In", "kept");
|
|
117
|
+
return new Response(res.body, {
|
|
118
|
+
status: res.status,
|
|
119
|
+
statusText: res.statusText,
|
|
120
|
+
headers: headersOut,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return res;
|
|
125
|
+
};
|
|
126
|
+
}
|
package/src/models.mjs
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_BASE_URL,
|
|
3
|
+
PROVIDER_ID,
|
|
4
|
+
SDK,
|
|
5
|
+
SEED_MODELS,
|
|
6
|
+
} from "./constants.mjs";
|
|
7
|
+
|
|
8
|
+
export function normalizeBaseURL(raw) {
|
|
9
|
+
if (raw === undefined || raw === null || String(raw).trim() === "") {
|
|
10
|
+
return DEFAULT_BASE_URL;
|
|
11
|
+
}
|
|
12
|
+
let url;
|
|
13
|
+
try {
|
|
14
|
+
url = new URL(String(raw).trim());
|
|
15
|
+
} catch {
|
|
16
|
+
throw new Error("Base URL must be a valid http(s) URL");
|
|
17
|
+
}
|
|
18
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
19
|
+
throw new Error("Base URL must use http:// or https://");
|
|
20
|
+
}
|
|
21
|
+
if (url.username || url.password) {
|
|
22
|
+
throw new Error("Base URL must not contain credentials");
|
|
23
|
+
}
|
|
24
|
+
if (url.search || url.hash) {
|
|
25
|
+
throw new Error("Base URL must not include a query string or hash");
|
|
26
|
+
}
|
|
27
|
+
return url.href.replace(/\/+$/, "");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function protocolForModel(id, opts = {}) {
|
|
31
|
+
if (opts?.protocol === "chat") return "chat";
|
|
32
|
+
const s = String(id || "").toLowerCase();
|
|
33
|
+
if (!s) return "chat";
|
|
34
|
+
// Free-tier models run through OpenAI-compatible chat completions.
|
|
35
|
+
if (s.startsWith("free/")) return "chat";
|
|
36
|
+
const bare = s.replace(/^(tokv2|payg|pro|anti|rel|cred)\//, "");
|
|
37
|
+
if (bare.startsWith("claude")) return "anthropic";
|
|
38
|
+
if (
|
|
39
|
+
!bare.startsWith("gpt-oss") &&
|
|
40
|
+
(/^(gpt-[56]|o[134]($|[-.])|codex)/.test(bare) || bare.includes("codex"))
|
|
41
|
+
) {
|
|
42
|
+
return "responses";
|
|
43
|
+
}
|
|
44
|
+
return "chat";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function isReasoningModel(id) {
|
|
48
|
+
const s = String(id || "").toLowerCase();
|
|
49
|
+
if (!s || s.includes("non-reasoning") || s.startsWith("tavily-")) return false;
|
|
50
|
+
const bare = s.replace(/^(free|tokv2|payg|pro|anti|rel|cred)\//, "").replace(/^relay-/, "");
|
|
51
|
+
if (
|
|
52
|
+
/^(claude-(3\.7|4|5|opus|sonnet|haiku|fable))/.test(bare) ||
|
|
53
|
+
/^(gpt-[56]|(openai-)?gpt-oss|o[134]($|[-.])|codex)/.test(bare) ||
|
|
54
|
+
/^gemini-(2\.5|3|4|(flash|pro)-latest)/.test(bare) ||
|
|
55
|
+
/^deepseek-(r1|v3\.1|v3\.2|v4|reasoner)/.test(bare) ||
|
|
56
|
+
/^(qwen3|qwq)/.test(bare) ||
|
|
57
|
+
/^glm-(4\.7|5)/.test(bare) ||
|
|
58
|
+
/^(grok-4|grok-build)/.test(bare) ||
|
|
59
|
+
/^(kimi-k[23]|minimax-m[23]|mimo-v2|magistral|(nvidia-)?nemotron-3)/.test(bare) ||
|
|
60
|
+
/(reasoning|thinking)/.test(bare)
|
|
61
|
+
) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function variantsFor(protocol, reasoning) {
|
|
68
|
+
if (!reasoning) return {};
|
|
69
|
+
if (protocol === "anthropic") {
|
|
70
|
+
return { low: {}, medium: {}, high: {} };
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
low: { reasoningEffort: "low" },
|
|
74
|
+
medium: { reasoningEffort: "medium" },
|
|
75
|
+
high: { reasoningEffort: "high" },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const BRAND_CASING = new Map([
|
|
80
|
+
["gpt", "GPT"],
|
|
81
|
+
["openai", "OpenAI"],
|
|
82
|
+
["nvidia", "NVIDIA"],
|
|
83
|
+
["claude", "Claude"],
|
|
84
|
+
["gemini", "Gemini"],
|
|
85
|
+
["deepseek", "DeepSeek"],
|
|
86
|
+
["qwen", "Qwen"],
|
|
87
|
+
["glm", "GLM"],
|
|
88
|
+
["grok", "Grok"],
|
|
89
|
+
["kimi", "Kimi"],
|
|
90
|
+
["minimax", "MiniMax"],
|
|
91
|
+
["llama", "Llama"],
|
|
92
|
+
["mistral", "Mistral"],
|
|
93
|
+
["tavily", "Tavily"],
|
|
94
|
+
["gemma", "Gemma"],
|
|
95
|
+
["oss", "OSS"],
|
|
96
|
+
["ocr", "OCR"],
|
|
97
|
+
["vl", "VL"],
|
|
98
|
+
["r1", "R1"],
|
|
99
|
+
["pro", "Pro"],
|
|
100
|
+
["flash", "Flash"],
|
|
101
|
+
["lite", "Lite"],
|
|
102
|
+
["mini", "Mini"],
|
|
103
|
+
["nano", "Nano"],
|
|
104
|
+
["opus", "Opus"],
|
|
105
|
+
["sonnet", "Sonnet"],
|
|
106
|
+
["haiku", "Haiku"],
|
|
107
|
+
["fable", "Fable"],
|
|
108
|
+
["codex", "Codex"],
|
|
109
|
+
]);
|
|
110
|
+
|
|
111
|
+
export function formatModelName(id) {
|
|
112
|
+
const s = String(id || "");
|
|
113
|
+
let prefix = "";
|
|
114
|
+
let rest = s;
|
|
115
|
+
const slash = s.indexOf("/");
|
|
116
|
+
if (slash !== -1) {
|
|
117
|
+
prefix = s.slice(0, slash);
|
|
118
|
+
rest = s.slice(slash + 1);
|
|
119
|
+
}
|
|
120
|
+
const formatted = rest
|
|
121
|
+
.split("-")
|
|
122
|
+
.map((part) => {
|
|
123
|
+
const lower = part.toLowerCase();
|
|
124
|
+
if (BRAND_CASING.has(lower)) return BRAND_CASING.get(lower);
|
|
125
|
+
if (/^[a-z]?\d+(\.\d+)*$/i.test(part)) return part.toUpperCase();
|
|
126
|
+
return part ? part.charAt(0).toUpperCase() + part.slice(1) : part;
|
|
127
|
+
})
|
|
128
|
+
.join(" ")
|
|
129
|
+
.replace(/(?<![.\d])(\d+)\s+(?!0\d)(\d{1,2})\b(?![.\d])/g, "$1.$2");
|
|
130
|
+
|
|
131
|
+
if (prefix === "free") return `${formatted} (Free)`;
|
|
132
|
+
if (prefix === "tokv2") return `${formatted} (TokV2)`;
|
|
133
|
+
if (prefix && prefix !== "payg") return `${formatted} (${prefix})`;
|
|
134
|
+
return formatted || s;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const KEY_RESTRICTED_REASONS = new Set([
|
|
138
|
+
"model_not_allowed",
|
|
139
|
+
"model_blocked",
|
|
140
|
+
"key_model_restricted",
|
|
141
|
+
"blocked_by_key",
|
|
142
|
+
]);
|
|
143
|
+
|
|
144
|
+
export function isEligibleModel(raw, opts = {}) {
|
|
145
|
+
if (!raw || typeof raw !== "object") return false;
|
|
146
|
+
const id = String(raw.id || "").trim();
|
|
147
|
+
if (!id || id === "__proto__" || id === "constructor" || id === "prototype") {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const type = raw.type ? String(raw.type).toLowerCase() : "chat";
|
|
152
|
+
if (type === "search") {
|
|
153
|
+
if (opts.includeSearchModels === false) return false;
|
|
154
|
+
} else if (type !== "chat") {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!opts.includeUnavailable) {
|
|
159
|
+
if (raw.status === "down" || raw.globally_available === false) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (opts.includeTokV2 === false && id.startsWith("tokv2/")) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const isFree = raw.is_free === true || id.startsWith("free/");
|
|
169
|
+
if (opts.includeFree === false && isFree) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (KEY_RESTRICTED_REASONS.has(String(raw.unavailable_reason || "").toLowerCase())) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const scope = opts.scope || "available";
|
|
178
|
+
if (scope === "free") {
|
|
179
|
+
return isFree;
|
|
180
|
+
}
|
|
181
|
+
if (scope === "available") {
|
|
182
|
+
return raw.available_for_account !== false && raw.available !== false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function finiteNonNeg(value, fallback = 0) {
|
|
189
|
+
const n = Number(value);
|
|
190
|
+
return Number.isFinite(n) && n >= 0 ? n : fallback;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function positiveInt(value, fallback) {
|
|
194
|
+
const n = Math.trunc(Number(value));
|
|
195
|
+
return Number.isFinite(n) && n > 0 ? n : fallback;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function buildConfigModels(baseURL = DEFAULT_BASE_URL, opts = {}) {
|
|
199
|
+
const out = {};
|
|
200
|
+
for (const m of SEED_MODELS) {
|
|
201
|
+
if (opts.includeFree === false && m.free) continue;
|
|
202
|
+
if (opts.scope === "free" && !m.free) continue;
|
|
203
|
+
const protocol = opts.protocol === "chat" ? "chat" : m.protocol;
|
|
204
|
+
const npm = SDK[protocol] ?? SDK.chat;
|
|
205
|
+
const input = m.vision ? ["text", "image"] : ["text"];
|
|
206
|
+
const entry = {
|
|
207
|
+
name: m.name,
|
|
208
|
+
limit: { context: m.context, output: m.output },
|
|
209
|
+
reasoning: Boolean(m.reasoning),
|
|
210
|
+
tool_call: true,
|
|
211
|
+
attachment: Boolean(m.vision),
|
|
212
|
+
modalities: { input, output: ["text"] },
|
|
213
|
+
variants: variantsFor(protocol, Boolean(m.reasoning)),
|
|
214
|
+
};
|
|
215
|
+
if (m.cost) {
|
|
216
|
+
entry.cost = {
|
|
217
|
+
input: m.cost.input,
|
|
218
|
+
output: m.cost.output,
|
|
219
|
+
...(m.cost.cache_read !== undefined ? { cache_read: m.cost.cache_read } : {}),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (m.free) entry.free = true;
|
|
223
|
+
if (npm !== SDK.chat || baseURL !== DEFAULT_BASE_URL) {
|
|
224
|
+
entry.provider = { npm, api: baseURL };
|
|
225
|
+
}
|
|
226
|
+
out[m.id] = entry;
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function extractRate(raw) {
|
|
232
|
+
const tm = raw?.tok_multiplier;
|
|
233
|
+
let candidate;
|
|
234
|
+
if (typeof tm === "number") {
|
|
235
|
+
candidate = tm;
|
|
236
|
+
} else if (tm && typeof tm === "object") {
|
|
237
|
+
candidate = tm.out ?? tm.in ?? tm.cache;
|
|
238
|
+
}
|
|
239
|
+
const n = Number(candidate);
|
|
240
|
+
if (!Number.isFinite(n) || n <= 0) return undefined;
|
|
241
|
+
return Math.round(n * 100) / 100;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function transformCatalogModels(rawList, baseURL = DEFAULT_BASE_URL, opts = {}) {
|
|
245
|
+
const models = {};
|
|
246
|
+
if (!Array.isArray(rawList)) return models;
|
|
247
|
+
|
|
248
|
+
const useCheapest = String(opts?.providerRouting || "").toLowerCase() === "cheapest";
|
|
249
|
+
|
|
250
|
+
for (const raw of rawList) {
|
|
251
|
+
if (!isEligibleModel(raw, opts)) continue;
|
|
252
|
+
const id = String(raw.id).trim();
|
|
253
|
+
const isSearch = raw.type === "search";
|
|
254
|
+
const protocol = isSearch ? "chat" : protocolForModel(id, opts);
|
|
255
|
+
const npm = SDK[protocol] ?? SDK.chat;
|
|
256
|
+
const reasoning = isSearch ? false : isReasoningModel(id);
|
|
257
|
+
const vision = Boolean(raw.vision);
|
|
258
|
+
const context = positiveInt(raw.context_window, isSearch ? 32768 : 128000);
|
|
259
|
+
const output = positiveInt(raw.max_output, isSearch ? 8192 : 16384);
|
|
260
|
+
|
|
261
|
+
const rawIn = useCheapest && raw.cheapest_price_in_per_m != null ? raw.cheapest_price_in_per_m : raw.price_in_per_m;
|
|
262
|
+
const rawOut = useCheapest && raw.cheapest_price_out_per_m != null ? raw.cheapest_price_out_per_m : raw.price_out_per_m;
|
|
263
|
+
const priceIn = finiteNonNeg(rawIn, 0);
|
|
264
|
+
const priceOut = finiteNonNeg(rawOut, 0);
|
|
265
|
+
const priceCache = finiteNonNeg(raw.price_cache_per_m, priceIn);
|
|
266
|
+
const isFree =
|
|
267
|
+
raw.is_free === true ||
|
|
268
|
+
(id.startsWith("free/") && priceIn === 0 && priceOut === 0);
|
|
269
|
+
|
|
270
|
+
const model = {
|
|
271
|
+
id,
|
|
272
|
+
providerID: PROVIDER_ID,
|
|
273
|
+
name: formatModelName(id),
|
|
274
|
+
api: { id, url: baseURL, npm },
|
|
275
|
+
limit: { context, output },
|
|
276
|
+
capabilities: {
|
|
277
|
+
temperature: !isSearch,
|
|
278
|
+
reasoning,
|
|
279
|
+
attachment: vision,
|
|
280
|
+
toolcall: !isSearch,
|
|
281
|
+
input: { text: true, image: vision },
|
|
282
|
+
output: { text: true },
|
|
283
|
+
interleaved: false,
|
|
284
|
+
},
|
|
285
|
+
cost: {
|
|
286
|
+
input: priceIn,
|
|
287
|
+
output: priceOut,
|
|
288
|
+
cache: { read: priceCache, write: 0 },
|
|
289
|
+
},
|
|
290
|
+
variants: variantsFor(protocol, reasoning),
|
|
291
|
+
status: raw.status === "down" ? "deprecated" : "active",
|
|
292
|
+
release_date: "",
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
if (isFree) {
|
|
296
|
+
model.free = true;
|
|
297
|
+
} else {
|
|
298
|
+
const rate = extractRate(raw);
|
|
299
|
+
if (rate !== undefined) model.rate = rate;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
models[id] = model;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return models;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export async function fetchBlazeModels({
|
|
309
|
+
baseURL = DEFAULT_BASE_URL,
|
|
310
|
+
apiKey,
|
|
311
|
+
fetchImpl = fetch,
|
|
312
|
+
signal,
|
|
313
|
+
opts = {},
|
|
314
|
+
} = {}) {
|
|
315
|
+
const headers = { Accept: "application/json" };
|
|
316
|
+
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
|
317
|
+
|
|
318
|
+
const timeout = AbortSignal.timeout(15_000);
|
|
319
|
+
const combinedSignal = signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
320
|
+
|
|
321
|
+
const res = await fetchImpl(`${baseURL}/models`, {
|
|
322
|
+
method: "GET",
|
|
323
|
+
headers,
|
|
324
|
+
signal: combinedSignal,
|
|
325
|
+
redirect: "error",
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
if (res.status === 401) {
|
|
329
|
+
await res.body?.cancel?.().catch(() => {});
|
|
330
|
+
throw Object.assign(
|
|
331
|
+
new Error("BlazeAPI refused the API key; please sign in again"),
|
|
332
|
+
{ signIn: "expired", status: 401 },
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!res.ok) {
|
|
337
|
+
const text = await res.text().catch(() => "");
|
|
338
|
+
throw new Error(
|
|
339
|
+
`BlazeAPI /models returned HTTP ${res.status}${text ? `: ${text.slice(0, 200)}` : ""}`,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const payload = await res.json();
|
|
344
|
+
const list = Array.isArray(payload?.data)
|
|
345
|
+
? payload.data
|
|
346
|
+
: Array.isArray(payload)
|
|
347
|
+
? payload
|
|
348
|
+
: [];
|
|
349
|
+
|
|
350
|
+
return {
|
|
351
|
+
models: transformCatalogModels(list, baseURL, opts),
|
|
352
|
+
rawCount: list.length,
|
|
353
|
+
};
|
|
354
|
+
}
|