2026-02-27 15:38:28 -08:00
|
|
|
import { buildResult, toUsageWindow, toNumber } from '../utils/index.js';
|
2026-07-12 16:21:38 +03:00
|
|
|
import { readManagedCredential } from '../credentials/providers.js';
|
2026-02-27 15:38:28 -08:00
|
|
|
|
|
|
|
|
export const providerId = 'ollama-cloud';
|
|
|
|
|
export const providerName = 'Ollama Cloud';
|
2026-06-26 19:27:53 +03:00
|
|
|
const aliases = ['ollama-cloud', 'ollamacloud'];
|
2026-02-27 15:38:28 -08:00
|
|
|
|
2026-07-12 16:21:38 +03:00
|
|
|
export const parseOllamaSettingsHtml = (html) => {
|
2026-02-27 15:38:28 -08:00
|
|
|
const windows = {};
|
|
|
|
|
const sessionMatch = html.match(/Session\s+usage[^0-9]*([0-9.]+)%/i);
|
|
|
|
|
if (sessionMatch) {
|
|
|
|
|
windows.session = toUsageWindow({
|
|
|
|
|
usedPercent: toNumber(sessionMatch[1]),
|
|
|
|
|
windowSeconds: null,
|
|
|
|
|
resetAt: null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const weeklyMatch = html.match(/Weekly\s+usage[^0-9]*([0-9.]+)%/i);
|
|
|
|
|
if (weeklyMatch) {
|
|
|
|
|
windows.weekly = toUsageWindow({
|
|
|
|
|
usedPercent: toNumber(weeklyMatch[1]),
|
|
|
|
|
windowSeconds: null,
|
|
|
|
|
resetAt: null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const premiumMatch = html.match(/Premium[^0-9]*([0-9]+)\s*\/\s*([0-9]+)/i);
|
|
|
|
|
if (premiumMatch) {
|
|
|
|
|
const used = toNumber(premiumMatch[1]);
|
|
|
|
|
const total = toNumber(premiumMatch[2]);
|
|
|
|
|
const usedPercent = total && used !== null ? Math.min(100, (used / total) * 100) : null;
|
|
|
|
|
windows.premium = toUsageWindow({
|
|
|
|
|
usedPercent,
|
|
|
|
|
windowSeconds: null,
|
|
|
|
|
resetAt: null,
|
|
|
|
|
valueLabel: `${used ?? 0} / ${total ?? 0}`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return windows;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const isConfigured = () => {
|
2026-07-12 16:21:38 +03:00
|
|
|
return Boolean(readManagedCredential(providerId));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchOllamaCloudUsage = async (credential, fetchImpl = fetch) => {
|
|
|
|
|
const response = await fetchImpl('https://ollama.com/settings', {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: { Cookie: credential.cookie, 'User-Agent': 'OpenChamber quota provider' },
|
|
|
|
|
redirect: 'manual',
|
|
|
|
|
signal: AbortSignal.timeout(15_000),
|
|
|
|
|
});
|
|
|
|
|
if (response.status === 401 || response.status === 403 || (response.status >= 300 && response.status < 400)) {
|
|
|
|
|
throw new Error('Ollama Cloud authentication failed');
|
|
|
|
|
}
|
|
|
|
|
if (!response.ok) throw new Error(`Ollama Cloud returned HTTP ${response.status}`);
|
|
|
|
|
const windows = parseOllamaSettingsHtml(await response.text());
|
|
|
|
|
if (Object.keys(windows).length === 0) throw new Error('Ollama Cloud usage data could not be parsed');
|
|
|
|
|
return windows;
|
2026-02-27 15:38:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const fetchQuota = async () => {
|
2026-07-12 16:21:38 +03:00
|
|
|
const credential = readManagedCredential(providerId);
|
2026-02-27 15:38:28 -08:00
|
|
|
|
2026-07-12 16:21:38 +03:00
|
|
|
if (!credential) {
|
2026-02-27 15:38:28 -08:00
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: false,
|
|
|
|
|
error: 'Not configured'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-07-12 16:21:38 +03:00
|
|
|
const windows = await fetchOllamaCloudUsage(credential);
|
2026-02-27 15:38:28 -08:00
|
|
|
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: true,
|
|
|
|
|
configured: true,
|
|
|
|
|
usage: { windows }
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return buildResult({
|
|
|
|
|
providerId,
|
|
|
|
|
providerName,
|
|
|
|
|
ok: false,
|
|
|
|
|
configured: true,
|
|
|
|
|
error: error instanceof Error ? error.message : 'Request failed'
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-12 16:21:38 +03:00
|
|
|
};
|