feat(quota): add Wafer.ai quota provider (#1312)
* feat(quota): add Wafer.ai quota provider - New provider: wafer.js fetches from https://pass.wafer.ai/v1/inference/quota - Auth: reads wafer/wafer-ai/wafer_ai keys from auth file - Timeout: AbortSignal.timeout(15_000) with timeoutSignal.aborted detection - Response: parses remaining/limit/overage/usedPercent/window_end/plan_tier - valueLabel: planTier + remaining/limit + overage suffix - Window: 5h (18000s) via resolveWindowLabel - Cross-runtime: added to web registry, UI types, VS Code dispatcher * fix(quota): wafer provider fixes — auth alias, decompression, logo - Add 'wafer.ai' auth alias to match actual auth key format - Use 'Accept-Encoding: identity' header to fix Bun fetch decompression issue with Cloudflare-backed responses - Match copilot valueLabel format: 'planTier · X / Y left' - Add wafer logo alias so Providers and Usage pages resolve to the same wafer.ai logo from models.dev * style(quota): fix indentation of timeoutSignal declaration * fix(quota): derive window duration from API instead of hardcoding Compute windowSeconds from window_end - window_start timestamps, with WAFER_WINDOW_SECONDS (5h) as fallback if timestamps are missing. --------- Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
Bohdan Triapitsyn
parent
6970cb45c0
commit
4cfe7c80a3
@@ -23,6 +23,8 @@ const LOGO_ALIAS = new Map<string, string>([
|
|||||||
['evroc-ai', 'evroc'],
|
['evroc-ai', 'evroc'],
|
||||||
['evrocai', 'evroc'],
|
['evrocai', 'evroc'],
|
||||||
['ollama-cloud', 'ollama'],
|
['ollama-cloud', 'ollama'],
|
||||||
|
['wafer-ai', 'wafer.ai'],
|
||||||
|
['wafer', 'wafer.ai'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const normalizeProviderId = (providerId: string | null | undefined) => {
|
const normalizeProviderId = (providerId: string | null | undefined) => {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ export const QUOTA_PROVIDERS: QuotaProviderMeta[] = [
|
|||||||
{ id: 'minimax-cn-coding-plan', name: 'MiniMax Coding Plan (minimaxi.com)' },
|
{ id: 'minimax-cn-coding-plan', name: 'MiniMax Coding Plan (minimaxi.com)' },
|
||||||
{ id: 'minimax-coding-plan', name: 'MiniMax Coding Plan (minimax.io)' },
|
{ id: 'minimax-coding-plan', name: 'MiniMax Coding Plan (minimax.io)' },
|
||||||
{ id: 'ollama-cloud', name: 'Ollama Cloud' },
|
{ id: 'ollama-cloud', name: 'Ollama Cloud' },
|
||||||
|
{ id: 'wafer', name: 'Wafer.ai' },
|
||||||
];
|
];
|
||||||
|
|
||||||
export const QUOTA_PROVIDER_MAP = QUOTA_PROVIDERS.reduce<
|
export const QUOTA_PROVIDER_MAP = QUOTA_PROVIDERS.reduce<
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ export type QuotaProviderId =
|
|||||||
| 'zhipuai-coding-plan'
|
| 'zhipuai-coding-plan'
|
||||||
| 'minimax-coding-plan'
|
| 'minimax-coding-plan'
|
||||||
| 'minimax-cn-coding-plan'
|
| 'minimax-cn-coding-plan'
|
||||||
| 'ollama-cloud';
|
| 'ollama-cloud'
|
||||||
|
| 'wafer';
|
||||||
|
|
||||||
export interface UsageWindow {
|
export interface UsageWindow {
|
||||||
usedPercent: number | null;
|
usedPercent: number | null;
|
||||||
|
|||||||
@@ -97,10 +97,19 @@ type ZhipuaiMcpTimeLimit = {
|
|||||||
type ZhipuaiPayload = {
|
type ZhipuaiPayload = {
|
||||||
data?: {
|
data?: {
|
||||||
limits?: Array<ZhipuaiTokensLimit | ZhipuaiMcpTimeLimit>;
|
limits?: Array<ZhipuaiTokensLimit | ZhipuaiMcpTimeLimit>;
|
||||||
level?: string;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type WaferPayload = {
|
||||||
|
remaining_included_requests?: number | string;
|
||||||
|
included_request_limit?: number | string;
|
||||||
|
overage_request_count?: number | string;
|
||||||
|
current_period_used_percent?: number | string;
|
||||||
|
window_start?: number | string;
|
||||||
|
window_end?: number | string;
|
||||||
|
plan_tier?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type ProviderResult = {
|
export type ProviderResult = {
|
||||||
providerId: string;
|
providerId: string;
|
||||||
providerName: string;
|
providerName: string;
|
||||||
@@ -439,6 +448,11 @@ export const listConfiguredQuotaProviders = () => {
|
|||||||
configured.add('ollama-cloud');
|
configured.add('ollama-cloud');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const waferAuth = normalizeAuthEntry(getAuthEntry(auth, ['wafer', 'wafer-ai', 'wafer_ai', 'wafer.ai']));
|
||||||
|
if (waferAuth && ((waferAuth as Record<string, unknown>).key || (waferAuth as Record<string, unknown>).token)) {
|
||||||
|
configured.add('wafer');
|
||||||
|
}
|
||||||
|
|
||||||
return Array.from(configured);
|
return Array.from(configured);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1738,6 +1752,116 @@ export const fetchNanoGptQuota = async (): Promise<ProviderResult> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const WAFER_QUOTA_URL = 'https://pass.wafer.ai/v1/inference/quota';
|
||||||
|
const WAFER_WINDOW_SECONDS = 5 * 3600;
|
||||||
|
|
||||||
|
export const fetchWaferQuota = async (): Promise<ProviderResult> => {
|
||||||
|
const auth = readAuthFile();
|
||||||
|
const entry = normalizeAuthEntry(getAuthEntry(auth, ['wafer', 'wafer-ai', 'wafer_ai', 'wafer.ai'])) as Record<string, unknown> | null;
|
||||||
|
const apiKey = (entry?.key as string | undefined) ?? (entry?.token as string | undefined);
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
return buildResult({
|
||||||
|
providerId: 'wafer',
|
||||||
|
providerName: 'Wafer.ai',
|
||||||
|
ok: false,
|
||||||
|
configured: false,
|
||||||
|
error: 'Not configured',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeoutSignal = AbortSignal.timeout(15_000);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(WAFER_QUOTA_URL, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Accept-Encoding': 'identity',
|
||||||
|
},
|
||||||
|
signal: timeoutSignal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return buildResult({
|
||||||
|
providerId: 'wafer',
|
||||||
|
providerName: 'Wafer.ai',
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: `API error: ${response.status}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = await response.json() as WaferPayload;
|
||||||
|
const remaining = toNumber(payload?.remaining_included_requests);
|
||||||
|
const limit = toNumber(payload?.included_request_limit);
|
||||||
|
const overage = toNumber(payload?.overage_request_count);
|
||||||
|
const usedPercentRaw = toNumber(payload?.current_period_used_percent);
|
||||||
|
const windowStart = toTimestamp(payload?.window_start);
|
||||||
|
const windowEnd = toTimestamp(payload?.window_end);
|
||||||
|
const planTier = asNonEmptyString(payload?.plan_tier);
|
||||||
|
|
||||||
|
if (remaining === null && limit === null && overage === null && usedPercentRaw === null) {
|
||||||
|
return buildResult({
|
||||||
|
providerId: 'wafer',
|
||||||
|
providerName: 'Wafer.ai',
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: 'No quota data in response',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasOverage = overage !== null && overage > 0;
|
||||||
|
const usedPercent = hasOverage
|
||||||
|
? Math.max(0, usedPercentRaw ?? 0)
|
||||||
|
: Math.max(0, Math.min(100, usedPercentRaw ?? 0));
|
||||||
|
|
||||||
|
const windowSeconds = windowStart !== null && windowEnd !== null
|
||||||
|
? Math.round((windowEnd - windowStart) / 1000)
|
||||||
|
: WAFER_WINDOW_SECONDS;
|
||||||
|
const windowLabel = resolveWindowLabel(windowSeconds);
|
||||||
|
|
||||||
|
let valueLabel: string | null = null;
|
||||||
|
if (remaining !== null && limit !== null) {
|
||||||
|
const parts: string[] = [];
|
||||||
|
if (planTier) parts.push(planTier);
|
||||||
|
parts.push(`${remaining} / ${limit} left`);
|
||||||
|
if (hasOverage) parts.push(`+${overage} overage`);
|
||||||
|
valueLabel = parts.join(' · ');
|
||||||
|
}
|
||||||
|
|
||||||
|
const windows: Record<string, UsageWindow> = {};
|
||||||
|
windows[windowLabel] = toUsageWindow({
|
||||||
|
usedPercent,
|
||||||
|
windowSeconds,
|
||||||
|
resetAt: windowEnd,
|
||||||
|
valueLabel,
|
||||||
|
});
|
||||||
|
|
||||||
|
return buildResult({
|
||||||
|
providerId: 'wafer',
|
||||||
|
providerName: 'Wafer.ai',
|
||||||
|
ok: true,
|
||||||
|
configured: true,
|
||||||
|
usage: { windows },
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const isTimeout = error instanceof DOMException && error.name === 'AbortError' && timeoutSignal.aborted;
|
||||||
|
const isParseError = error instanceof SyntaxError;
|
||||||
|
return buildResult({
|
||||||
|
providerId: 'wafer',
|
||||||
|
providerName: 'Wafer.ai',
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: isTimeout
|
||||||
|
? 'Request timed out'
|
||||||
|
: isParseError
|
||||||
|
? 'Invalid response from provider'
|
||||||
|
: (error instanceof Error ? error.message : 'Request failed'),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const fetchQuotaForProvider = async (providerId: string): Promise<ProviderResult> => {
|
export const fetchQuotaForProvider = async (providerId: string): Promise<ProviderResult> => {
|
||||||
switch (providerId) {
|
switch (providerId) {
|
||||||
case 'claude':
|
case 'claude':
|
||||||
@@ -1766,6 +1890,8 @@ export const fetchQuotaForProvider = async (providerId: string): Promise<Provide
|
|||||||
return fetchZaiQuota();
|
return fetchZaiQuota();
|
||||||
case 'zhipuai-coding-plan':
|
case 'zhipuai-coding-plan':
|
||||||
return fetchZhipuaiCodingPlanQuota();
|
return fetchZhipuaiCodingPlanQuota();
|
||||||
|
case 'wafer':
|
||||||
|
return fetchWaferQuota();
|
||||||
default:
|
default:
|
||||||
return buildResult({
|
return buildResult({
|
||||||
providerId,
|
providerId,
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
|||||||
| `minimax-coding-plan` | MiniMax Coding Plan (minimax.io) | `providers/minimax-coding-plan.js` | `minimax-coding-plan` |
|
| `minimax-coding-plan` | MiniMax Coding Plan (minimax.io) | `providers/minimax-coding-plan.js` | `minimax-coding-plan` |
|
||||||
| `minimax-cn-coding-plan` | MiniMax Coding Plan (minimaxi.com) | `providers/minimax-cn-coding-plan.js` | `minimax-cn-coding-plan` |
|
| `minimax-cn-coding-plan` | MiniMax Coding Plan (minimaxi.com) | `providers/minimax-cn-coding-plan.js` | `minimax-cn-coding-plan` |
|
||||||
| `ollama-cloud` | Ollama Cloud | `providers/ollama-cloud.js` | Cookie file at `~/.config/ollama-quota/cookie` (raw session cookie string) |
|
| `ollama-cloud` | Ollama Cloud | `providers/ollama-cloud.js` | Cookie file at `~/.config/ollama-quota/cookie` (raw session cookie string) |
|
||||||
|
| `wafer` | Wafer.ai | `providers/wafer.js` | `wafer`, `wafer-ai`, `wafer_ai`, `wafer.ai` |
|
||||||
|
|
||||||
## Internal-only provider module
|
## Internal-only provider module
|
||||||
- `providers/openai.js` exists for logic parity/reuse but is intentionally not registered for dispatcher ID routing.
|
- `providers/openai.js` exists for logic parity/reuse but is intentionally not registered for dispatcher ID routing.
|
||||||
|
|||||||
@@ -21,5 +21,6 @@ export {
|
|||||||
fetchMinimaxCodingPlanQuota,
|
fetchMinimaxCodingPlanQuota,
|
||||||
fetchMinimaxCnCodingPlanQuota,
|
fetchMinimaxCnCodingPlanQuota,
|
||||||
fetchOllamaCloudQuota,
|
fetchOllamaCloudQuota,
|
||||||
fetchZhipuaiQuota
|
fetchZhipuaiQuota,
|
||||||
|
fetchWaferQuota
|
||||||
} from './providers/index.js';
|
} from './providers/index.js';
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import * as zhipuaiCodingPlan from './zhipuai-coding-plan.js';
|
|||||||
import * as minimaxCodingPlan from './minimax-coding-plan.js';
|
import * as minimaxCodingPlan from './minimax-coding-plan.js';
|
||||||
import * as minimaxCnCodingPlan from './minimax-cn-coding-plan.js';
|
import * as minimaxCnCodingPlan from './minimax-cn-coding-plan.js';
|
||||||
import * as ollamaCloud from './ollama-cloud.js';
|
import * as ollamaCloud from './ollama-cloud.js';
|
||||||
|
import * as wafer from './wafer.js';
|
||||||
|
|
||||||
const registry = {
|
const registry = {
|
||||||
claude: {
|
claude: {
|
||||||
@@ -99,6 +100,12 @@ const registry = {
|
|||||||
providerName: ollamaCloud.providerName,
|
providerName: ollamaCloud.providerName,
|
||||||
isConfigured: ollamaCloud.isConfigured,
|
isConfigured: ollamaCloud.isConfigured,
|
||||||
fetchQuota: ollamaCloud.fetchQuota
|
fetchQuota: ollamaCloud.fetchQuota
|
||||||
|
},
|
||||||
|
wafer: {
|
||||||
|
providerId: wafer.providerId,
|
||||||
|
providerName: wafer.providerName,
|
||||||
|
isConfigured: wafer.isConfigured,
|
||||||
|
fetchQuota: wafer.fetchQuota
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -158,4 +165,5 @@ export const fetchNanoGptQuota = nanogpt.fetchQuota;
|
|||||||
export const fetchMinimaxCodingPlanQuota = minimaxCodingPlan.fetchQuota;
|
export const fetchMinimaxCodingPlanQuota = minimaxCodingPlan.fetchQuota;
|
||||||
export const fetchMinimaxCnCodingPlanQuota = minimaxCnCodingPlan.fetchQuota;
|
export const fetchMinimaxCnCodingPlanQuota = minimaxCnCodingPlan.fetchQuota;
|
||||||
export const fetchOllamaCloudQuota = ollamaCloud.fetchQuota;
|
export const fetchOllamaCloudQuota = ollamaCloud.fetchQuota;
|
||||||
|
export const fetchWaferQuota = wafer.fetchQuota;
|
||||||
export const fetchZhipuaiQuota = zhipuaiCodingPlan.fetchQuota;
|
export const fetchZhipuaiQuota = zhipuaiCodingPlan.fetchQuota;
|
||||||
|
|||||||
@@ -0,0 +1,131 @@
|
|||||||
|
import { readAuthFile } from '../../opencode/auth.js';
|
||||||
|
import {
|
||||||
|
getAuthEntry,
|
||||||
|
normalizeAuthEntry,
|
||||||
|
buildResult,
|
||||||
|
toUsageWindow,
|
||||||
|
toNumber,
|
||||||
|
toTimestamp,
|
||||||
|
resolveWindowLabel,
|
||||||
|
asNonEmptyString
|
||||||
|
} from '../utils/index.js';
|
||||||
|
|
||||||
|
export const providerId = 'wafer';
|
||||||
|
export const providerName = 'Wafer.ai';
|
||||||
|
export const aliases = ['wafer', 'wafer-ai', 'wafer_ai', 'wafer.ai'];
|
||||||
|
|
||||||
|
const WAFER_QUOTA_URL = 'https://pass.wafer.ai/v1/inference/quota';
|
||||||
|
const WAFER_WINDOW_SECONDS = 5 * 3600;
|
||||||
|
|
||||||
|
export const isConfigured = () => {
|
||||||
|
const auth = readAuthFile();
|
||||||
|
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
||||||
|
return Boolean(entry?.key || entry?.token);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const fetchQuota = async () => {
|
||||||
|
const auth = readAuthFile();
|
||||||
|
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
||||||
|
const apiKey = entry?.key ?? entry?.token;
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
return buildResult({
|
||||||
|
providerId,
|
||||||
|
providerName,
|
||||||
|
ok: false,
|
||||||
|
configured: false,
|
||||||
|
error: 'Not configured'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeoutSignal = AbortSignal.timeout(15_000);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(WAFER_QUOTA_URL, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Accept-Encoding': 'identity'
|
||||||
|
},
|
||||||
|
signal: timeoutSignal
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return buildResult({
|
||||||
|
providerId,
|
||||||
|
providerName,
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: `API error: ${response.status}`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = await response.json();
|
||||||
|
const remaining = toNumber(payload?.remaining_included_requests);
|
||||||
|
const limit = toNumber(payload?.included_request_limit);
|
||||||
|
const overage = toNumber(payload?.overage_request_count);
|
||||||
|
const usedPercentRaw = toNumber(payload?.current_period_used_percent);
|
||||||
|
const windowStart = toTimestamp(payload?.window_start);
|
||||||
|
const windowEnd = toTimestamp(payload?.window_end);
|
||||||
|
const planTier = asNonEmptyString(payload?.plan_tier);
|
||||||
|
|
||||||
|
if (remaining === null && limit === null && overage === null && usedPercentRaw === null) {
|
||||||
|
return buildResult({
|
||||||
|
providerId,
|
||||||
|
providerName,
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: 'No quota data in response'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasOverage = overage !== null && overage > 0;
|
||||||
|
const usedPercent = hasOverage
|
||||||
|
? Math.max(0, usedPercentRaw ?? 0)
|
||||||
|
: Math.max(0, Math.min(100, usedPercentRaw ?? 0));
|
||||||
|
|
||||||
|
const windowSeconds = windowStart !== null && windowEnd !== null
|
||||||
|
? Math.round((windowEnd - windowStart) / 1000)
|
||||||
|
: WAFER_WINDOW_SECONDS;
|
||||||
|
const windowLabel = resolveWindowLabel(windowSeconds);
|
||||||
|
|
||||||
|
let valueLabel = null;
|
||||||
|
if (remaining !== null && limit !== null) {
|
||||||
|
const parts = [];
|
||||||
|
if (planTier) parts.push(planTier);
|
||||||
|
parts.push(`${remaining} / ${limit} left`);
|
||||||
|
if (hasOverage) parts.push(`+${overage} overage`);
|
||||||
|
valueLabel = parts.join(' · ');
|
||||||
|
}
|
||||||
|
|
||||||
|
const windows = {};
|
||||||
|
windows[windowLabel] = toUsageWindow({
|
||||||
|
usedPercent,
|
||||||
|
windowSeconds,
|
||||||
|
resetAt: windowEnd,
|
||||||
|
valueLabel
|
||||||
|
});
|
||||||
|
|
||||||
|
return buildResult({
|
||||||
|
providerId,
|
||||||
|
providerName,
|
||||||
|
ok: true,
|
||||||
|
configured: true,
|
||||||
|
usage: { windows }
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const isTimeout = error instanceof DOMException && error.name === 'AbortError' && timeoutSignal.aborted;
|
||||||
|
const isParseError = error instanceof SyntaxError;
|
||||||
|
return buildResult({
|
||||||
|
providerId,
|
||||||
|
providerName,
|
||||||
|
ok: false,
|
||||||
|
configured: true,
|
||||||
|
error: isTimeout
|
||||||
|
? 'Request timed out'
|
||||||
|
: isParseError
|
||||||
|
? 'Invalid response from provider'
|
||||||
|
: (error instanceof Error ? error.message : 'Request failed')
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user