fix(ollama): map free-plan monthly window and derive reset from signup date

This commit is contained in:
X-Adam
2026-09-19 09:51:13 +07:00
parent a8c9d3802c
commit 73e021b8a0
3 changed files with 163 additions and 16 deletions

View File

@@ -24,11 +24,43 @@ export async function getIflowUsage(accessToken) {
}
}
const OLLAMA_LIMIT_WINDOWS = {
session: "Session (5h)",
weekly: "Weekly (7d)",
monthly: "Monthly",
};
function addUtcMonths(date, months) {
const total = date.getUTCMonth() + months;
const year = date.getUTCFullYear() + Math.floor(total / 12);
const month = ((total % 12) + 12) % 12;
const lastDay = new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
return new Date(Date.UTC(
year, month, Math.min(date.getUTCDate(), lastDay),
date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(),
));
}
// Free plan: "usage resets monthly from the date you signed up" (ollama.com/pricing).
function nextMonthlyResetFromSignup(createdAt, now = new Date()) {
const anchor = new Date(createdAt);
if (Number.isNaN(anchor.getTime())) return null;
const elapsedMonths = (now.getUTCFullYear() - anchor.getUTCFullYear()) * 12
+ (now.getUTCMonth() - anchor.getUTCMonth());
for (let i = Math.max(0, elapsedMonths); i <= elapsedMonths + 1; i++) {
const candidate = addUtcMonths(anchor, i);
if (candidate > now) return candidate.toISOString();
}
return null;
}
/**
* Ollama Cloud Usage
* GET https://ollama.com/api/usage — session (5h) + weekly (7d) `usage` is a 0..1
* ratio (1.0 = limit reached, e.g. weekly 100% used). No reset timestamp exposed.
* POST https://ollama.com/api/me — plan label (fail-open).
* GET https://ollama.com/api/usage — `limits.<window>.usage` is a 0..1 ratio
* (1.0 = limit reached). Paid plans report session (5h) + weekly (7d); the
* free plan reports a single monthly window. No reset timestamp exposed;
* the free monthly reset is derived from the account's signup date.
* POST https://ollama.com/api/me — plan label + CreatedAt (fail-open).
* Auth: Authorization: Bearer <apiKey>
*/
export async function getOllamaUsage(apiKey, providerSpecificData, proxyOptions = null) {
@@ -84,14 +116,20 @@ export async function getOllamaUsage(apiKey, providerSpecificData, proxyOptions
return { used: usedPct, total: 100, remainingPercentage: 100 - usedPct, resetAt, unlimited: false };
}
const sessionRaw = limits.session?.usage;
const weeklyRaw = limits.weekly?.usage;
const sessionNum = Number(sessionRaw);
const weeklyNum = Number(weeklyRaw);
const hasSession = sessionRaw !== undefined && sessionRaw !== null && !Number.isNaN(sessionNum);
const hasWeekly = weeklyRaw !== undefined && weeklyRaw !== null && !Number.isNaN(weeklyNum);
const monthlyResetAt = planRaw.toLowerCase() === "free" && me?.CreatedAt
? nextMonthlyResetFromSignup(me.CreatedAt)
: null;
if (!hasSession && !hasWeekly) {
const quotas = {};
for (const [key, label] of Object.entries(OLLAMA_LIMIT_WINDOWS)) {
const raw = limits[key]?.usage;
if (raw === undefined || raw === null) continue;
const ratio = Number(raw);
if (Number.isNaN(ratio)) continue;
quotas[label] = ratioQuota(ratio, key === "monthly" ? monthlyResetAt : null);
}
if (Object.keys(quotas).length === 0) {
return {
plan,
message: "Ollama Cloud connected. No usage limits reported.",
@@ -99,10 +137,6 @@ export async function getOllamaUsage(apiKey, providerSpecificData, proxyOptions
};
}
const quotas = {};
if (hasSession) quotas["Session (5h)"] = ratioQuota(sessionNum);
if (hasWeekly) quotas["Weekly (7d)"] = ratioQuota(weeklyNum);
return { plan, quotas };
} catch (error) {
return { message: `Ollama Cloud error: ${error.message}` };