|
| 1 | +import type { UsageProvider } from "../types" |
| 2 | +import { |
| 3 | + fetchJson, |
| 4 | + getCredential, |
| 5 | + notConfigured, |
| 6 | + resolveWindowLabel, |
| 7 | + safeFetch, |
| 8 | + toNumber, |
| 9 | + toTimestamp, |
| 10 | + toUsageWindow, |
| 11 | +} from "../shared" |
| 12 | + |
| 13 | +const kimiAliases = ["kimi-for-coding", "kimi"] as const |
| 14 | +const kimi: UsageProvider = { |
| 15 | + id: "kimi-for-coding", |
| 16 | + name: "Kimi for Coding", |
| 17 | + aliases: kimiAliases, |
| 18 | + async fetchQuota() { |
| 19 | + const key = getCredential(kimiAliases, ["key", "token"]) |
| 20 | + if (!key) return notConfigured(this.id, this.name) |
| 21 | + return safeFetch(this.id, this.name, async () => { |
| 22 | + const payload = await fetchJson("https://api.kimi.com/coding/v1/usages", { |
| 23 | + headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }, |
| 24 | + }) |
| 25 | + const windows: Record<string, ReturnType<typeof toUsageWindow>> = {} |
| 26 | + const usage = payload?.usage |
| 27 | + if (usage) { |
| 28 | + const limit = toNumber(usage.limit) |
| 29 | + const remaining = toNumber(usage.remaining) |
| 30 | + windows.weekly = toUsageWindow({ |
| 31 | + usedPercent: limit && remaining !== null ? 100 - (remaining / limit) * 100 : null, |
| 32 | + resetAt: toTimestamp(usage.resetTime), |
| 33 | + }) |
| 34 | + } |
| 35 | + for (const item of Array.isArray(payload?.limits) ? payload.limits : []) { |
| 36 | + const duration = toNumber(item?.window?.duration) |
| 37 | + const unit = item?.window?.timeUnit |
| 38 | + const multiplier = unit === "TIME_UNIT_MINUTE" ? 60 : unit === "TIME_UNIT_HOUR" ? 3600 : unit === "TIME_UNIT_DAY" ? 86_400 : null |
| 39 | + const seconds = duration !== null && multiplier ? duration * multiplier : null |
| 40 | + const limit = toNumber(item?.detail?.limit) |
| 41 | + const remaining = toNumber(item?.detail?.remaining) |
| 42 | + windows[resolveWindowLabel(seconds)] = toUsageWindow({ |
| 43 | + usedPercent: limit && remaining !== null ? 100 - (remaining / limit) * 100 : null, |
| 44 | + windowSeconds: seconds, |
| 45 | + resetAt: toTimestamp(item?.detail?.resetTime), |
| 46 | + }) |
| 47 | + } |
| 48 | + return { windows } |
| 49 | + }) |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +const nanoAliases = ["nano-gpt", "nanogpt", "nano_gpt"] as const |
| 54 | +const nanoGpt: UsageProvider = { |
| 55 | + id: "nano-gpt", |
| 56 | + name: "NanoGPT", |
| 57 | + aliases: nanoAliases, |
| 58 | + async fetchQuota() { |
| 59 | + const key = getCredential(nanoAliases, ["key", "token"]) |
| 60 | + if (!key) return notConfigured(this.id, this.name) |
| 61 | + return safeFetch(this.id, this.name, async () => { |
| 62 | + const payload = await fetchJson("https://nano-gpt.com/api/subscription/v1/usage", { |
| 63 | + headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }, |
| 64 | + }) |
| 65 | + const windows: Record<string, ReturnType<typeof toUsageWindow>> = {} |
| 66 | + for (const [label, source] of [["daily", payload?.daily], ["monthly", payload?.monthly]] as const) { |
| 67 | + if (!source) continue |
| 68 | + const fraction = toNumber(source.percentUsed) |
| 69 | + const used = toNumber(source.used) |
| 70 | + const limit = toNumber(source.limit ?? source.limits?.[label]) |
| 71 | + windows[label] = toUsageWindow({ |
| 72 | + usedPercent: fraction !== null ? fraction * 100 : used !== null && limit ? (used / limit) * 100 : null, |
| 73 | + windowSeconds: label === "daily" ? 86_400 : null, |
| 74 | + resetAt: toTimestamp(source.resetAt ?? payload?.period?.currentPeriodEnd), |
| 75 | + }) |
| 76 | + } |
| 77 | + return { windows } |
| 78 | + }) |
| 79 | + }, |
| 80 | +} |
| 81 | + |
| 82 | +const openRouterAliases = ["openrouter"] as const |
| 83 | +const openRouter: UsageProvider = { |
| 84 | + id: "openrouter", |
| 85 | + name: "OpenRouter", |
| 86 | + aliases: openRouterAliases, |
| 87 | + async fetchQuota() { |
| 88 | + const key = getCredential(openRouterAliases, ["key", "token"]) |
| 89 | + if (!key) return notConfigured(this.id, this.name) |
| 90 | + return safeFetch(this.id, this.name, async () => { |
| 91 | + const payload = await fetchJson("https://openrouter.ai/api/v1/credits", { |
| 92 | + headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }, |
| 93 | + }) |
| 94 | + const total = toNumber(payload?.data?.total_credits) |
| 95 | + const used = toNumber(payload?.data?.total_usage) |
| 96 | + const remaining = total !== null && used !== null ? Math.max(0, total - used) : null |
| 97 | + return { |
| 98 | + windows: { |
| 99 | + credits: toUsageWindow({ |
| 100 | + usedPercent: total && used !== null ? (used / total) * 100 : null, |
| 101 | + valueLabel: remaining !== null && total !== null ? `$${remaining.toFixed(2)} / $${total.toFixed(2)}` : null, |
| 102 | + }), |
| 103 | + }, |
| 104 | + } |
| 105 | + }) |
| 106 | + }, |
| 107 | +} |
| 108 | + |
| 109 | +function createTokenLimitProvider(input: { id: string; name: string; aliases: readonly string[]; url: string }): UsageProvider { |
| 110 | + return { |
| 111 | + id: input.id, |
| 112 | + name: input.name, |
| 113 | + aliases: input.aliases, |
| 114 | + async fetchQuota() { |
| 115 | + const key = getCredential(input.aliases, ["key", "token"]) |
| 116 | + if (!key) return notConfigured(this.id, this.name) |
| 117 | + return safeFetch(this.id, this.name, async () => { |
| 118 | + const payload = await fetchJson(input.url, { headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" } }) |
| 119 | + const windows: Record<string, ReturnType<typeof toUsageWindow>> = {} |
| 120 | + for (const limit of Array.isArray(payload?.data?.limits) ? payload.data.limits : []) { |
| 121 | + if (limit?.type !== "TOKENS_LIMIT" && limit?.type !== "TIME_LIMIT") continue |
| 122 | + const duration = toNumber(limit.number) |
| 123 | + const seconds = limit.type === "TIME_LIMIT" ? 30 * 86_400 : limit.unit === 3 && duration ? duration * 3600 : null |
| 124 | + const label = limit.type === "TIME_LIMIT" ? "mcp-tools" : resolveWindowLabel(seconds) |
| 125 | + windows[label] = toUsageWindow({ |
| 126 | + usedPercent: toNumber(limit.percentage), |
| 127 | + windowSeconds: seconds, |
| 128 | + resetAt: toTimestamp(limit.nextResetTime), |
| 129 | + }) |
| 130 | + } |
| 131 | + return { windows } |
| 132 | + }) |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +const zai = createTokenLimitProvider({ |
| 138 | + id: "zai-coding-plan", |
| 139 | + name: "z.ai", |
| 140 | + aliases: ["zai-coding-plan", "zai", "z.ai"], |
| 141 | + url: "https://api.z.ai/api/monitor/usage/quota/limit", |
| 142 | +}) |
| 143 | + |
| 144 | +const zhipu = createTokenLimitProvider({ |
| 145 | + id: "zhipuai-coding-plan", |
| 146 | + name: "Zhipu AI Coding Plan", |
| 147 | + aliases: ["zhipuai-coding-plan", "zhipuai", "zhipu"], |
| 148 | + url: "https://open.bigmodel.cn/api/monitor/usage/quota/limit", |
| 149 | +}) |
| 150 | + |
| 151 | +const waferAliases = ["wafer", "wafer-ai", "wafer_ai", "wafer.ai"] as const |
| 152 | +const wafer: UsageProvider = { |
| 153 | + id: "wafer", |
| 154 | + name: "Wafer.ai", |
| 155 | + aliases: waferAliases, |
| 156 | + async fetchQuota() { |
| 157 | + const key = getCredential(waferAliases, ["key", "token"]) |
| 158 | + if (!key) return notConfigured(this.id, this.name) |
| 159 | + return safeFetch(this.id, this.name, async () => { |
| 160 | + const payload = await fetchJson("https://pass.wafer.ai/v1/inference/quota", { |
| 161 | + headers: { Authorization: `Bearer ${key}`, "Accept-Encoding": "identity" }, |
| 162 | + }) |
| 163 | + const remaining = toNumber(payload?.remaining_included_requests) |
| 164 | + const limit = toNumber(payload?.included_request_limit) |
| 165 | + const startAt = toTimestamp(payload?.window_start) |
| 166 | + const resetAt = toTimestamp(payload?.window_end) |
| 167 | + const seconds = startAt !== null && resetAt !== null ? Math.round((resetAt - startAt) / 1000) : 5 * 3600 |
| 168 | + return { |
| 169 | + windows: { |
| 170 | + [resolveWindowLabel(seconds)]: toUsageWindow({ |
| 171 | + usedPercent: toNumber(payload?.current_period_used_percent), |
| 172 | + windowSeconds: seconds, |
| 173 | + resetAt, |
| 174 | + valueLabel: remaining !== null && limit !== null ? `${remaining} / ${limit}` : null, |
| 175 | + }), |
| 176 | + }, |
| 177 | + } |
| 178 | + }) |
| 179 | + }, |
| 180 | +} |
| 181 | + |
| 182 | +export const apiKeyProviders: UsageProvider[] = [kimi, nanoGpt, openRouter, zai, zhipu, wafer] |
0 commit comments