(input: {
provider: Provider.Info
})
| 1358 | } |
| 1359 | |
| 1360 | async function fetchKimiUsage(input: { |
| 1361 | provider: Provider.Info |
| 1362 | }): Promise<Omit<Record, "providerID" | "providerName" | "fetchedAt">> { |
| 1363 | const auth = await Auth.get(input.provider.id) |
| 1364 | if (!auth) { |
| 1365 | throw new Error("Kimi authentication is required. Run `arctic auth login` and select Kimi.") |
| 1366 | } |
| 1367 | |
| 1368 | let token: string | undefined |
| 1369 | if (auth.type === "api") { |
| 1370 | token = auth.key |
| 1371 | } else if (auth.type === "oauth") { |
| 1372 | token = auth.access |
| 1373 | } |
| 1374 | |
| 1375 | if (!token) { |
| 1376 | throw new Error("Kimi authentication token is missing.") |
| 1377 | } |
| 1378 | |
| 1379 | const response = await globalThis.fetch("https://api.kimi.com/coding/v1/usages", { |
| 1380 | headers: { |
| 1381 | Authorization: `Bearer ${token}`, |
| 1382 | }, |
| 1383 | }) |
| 1384 | |
| 1385 | if (!response.ok) { |
| 1386 | if (response.status === 401) throw new Error("Authorization failed. Please check your API key.") |
| 1387 | if (response.status === 404) throw new Error("Usage endpoint not available. Try Kimi For Coding.") |
| 1388 | throw new Error(`Failed to fetch usage: ${response.statusText}`) |
| 1389 | } |
| 1390 | |
| 1391 | const payload = (await response.json()) as any |
| 1392 | |
| 1393 | const weeklyUsage = payload.usage |
| 1394 | const windowedLimits = Array.isArray(payload.limits) ? payload.limits : [] |
| 1395 | |
| 1396 | const fiveHourLimit = windowedLimits.find((l: any) => { |
| 1397 | const window = l?.window || {} |
| 1398 | const duration = parseInt(window.duration) |
| 1399 | const timeUnit = window.timeUnit || "" |
| 1400 | return timeUnit.includes("MINUTE") && duration === 300 |
| 1401 | }) |
| 1402 | |
| 1403 | const primary = resolveKimiLimitFromDetail(fiveHourLimit?.detail, "5h") |
| 1404 | const secondary = resolveKimiLimitFromDetail(weeklyUsage, "Weekly") |
| 1405 | |
| 1406 | const rows = [ |
| 1407 | formatKimiLimitRow("5-hour", fiveHourLimit?.detail), |
| 1408 | formatKimiLimitRow("Weekly", weeklyUsage), |
| 1409 | ].filter((row): row is string => Boolean(row)) |
| 1410 | |
| 1411 | const planType = rows.length > 0 ? `Kimi for Coding\n${rows.map((row) => `- ${row}`).join("\n")}` : "Kimi for Coding" |
| 1412 | |
| 1413 | const limitReached = [primary?.usedPercent, secondary?.usedPercent].some( |
| 1414 | (value) => typeof value === "number" && value >= 100, |
| 1415 | ) |
| 1416 | |
| 1417 | return { |
nothing calls this directly
no test coverage detected