MCPcopy Create free account
hub / github.com/arctic-cli/interface / fetchZaiUsageLimits

Function fetchZaiUsageLimits

packages/arctic/src/provider/usage.ts:305–391  ·  view source on GitHub ↗
(provider: Provider.Info)

Source from the content-addressed store, hash-verified

303 const MINIMAX_USAGE_URL = "https://platform.minimax.io/v1/api/openplatform/coding_plan/remains"
304
305 async function fetchZaiUsageLimits(provider: Provider.Info): Promise<{
306 planType?: string
307 allowed?: boolean
308 limitReached?: boolean
309 limits?: { primary?: RateLimitWindowSummary }
310 }> {
311 let token: string | undefined
312 const auth = await Auth.get(provider.id)
313
314 if (auth?.type === "api") {
315 token = auth.key
316 } else if (auth?.type === "oauth") {
317 token = auth.access
318 } else if (auth?.type === "wellknown") {
319 token = auth.token || auth.key
320 }
321
322 if (!token) {
323 token = provider.key ?? provider.options?.apiKey
324 }
325
326 if (!token) {
327 throw new Error("Z.ai authentication is required. Set an API key for provider 'zai-coding-plan'.")
328 }
329
330 const response = await globalThis.fetch(ZAI_USAGE_LIMITS_URL, {
331 headers: {
332 Authorization: `Bearer ${token}`,
333 Accept: "application/json",
334 },
335 })
336
337 if (!response.ok) {
338 if (response.status === 401 || response.status === 403) {
339 throw new Error("Authorization failed. Please check your Z.ai API key.")
340 }
341 throw new Error(`Failed to fetch Z.ai usage limits: ${response.statusText}`)
342 }
343
344 const payload = (await response.json().catch(() => null)) as any
345 const limits = Array.isArray(payload?.data?.limits) ? payload.data.limits : []
346 if (!payload || payload.success !== true || limits.length === 0) {
347 return {}
348 }
349
350 const rows = limits
351 .map((limit: any, idx: number) => formatZaiLimitRow(limit, idx))
352 .filter((row: string | undefined) => row && row.length > 0)
353
354 const detailedPlan = rows.length > 0 ? `Z.ai\n${rows.map((row: string) => `- ${row}`).join("\n")}` : "Z.ai"
355
356 const enriched = limits
357 .map((limit: any) => ({
358 usedPercent: resolveZaiUsedPercent(limit),
359 remaining: typeof limit?.remaining === "number" ? limit.remaining : undefined,
360 }))
361 .filter((limit: { usedPercent?: number; remaining?: number }) => limit.usedPercent !== undefined)
362

Callers 1

fetchSessionUsageFunction · 0.85

Calls 4

formatZaiLimitRowFunction · 0.85
resolveZaiUsedPercentFunction · 0.85
jsonMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected