feat: add ZhipuAI provider to quota tracking system (#793)
This commit is contained in:
@@ -29,6 +29,7 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
||||
| `minimax-coding-plan` | MiniMax Coding Plan (minimax.io) | `providers/minimax-coding-plan.js` | `minimax-coding-plan` |
|
||||
| `minimax-cn-coding-plan` | MiniMax Coding Plan (minimaxi.com) | `providers/minimax-cn-coding-plan.js` | `minimax-cn-coding-plan` |
|
||||
| `ollama-cloud` | Ollama Cloud | `providers/ollama-cloud.js` | Cookie file at `~/.config/ollama-quota/cookie` (raw session cookie string) |
|
||||
| `zhipuai-coding-plan` | ZhipuAI | `providers/zhipuai.js` | `zhipuai-coding-plan`, `zhipuai`, `zhipu` |
|
||||
|
||||
## Internal-only provider module
|
||||
- `providers/openai.js` exists for logic parity/reuse but is intentionally not registered for dispatcher ID routing.
|
||||
|
||||
@@ -20,5 +20,6 @@ export {
|
||||
fetchNanoGptQuota,
|
||||
fetchMinimaxCodingPlanQuota,
|
||||
fetchMinimaxCnCodingPlanQuota,
|
||||
fetchOllamaCloudQuota
|
||||
fetchOllamaCloudQuota,
|
||||
fetchZhipuaiQuota
|
||||
} from './providers/index.js';
|
||||
|
||||
@@ -19,6 +19,7 @@ import * as zai from './zai.js';
|
||||
import * as minimaxCodingPlan from './minimax-coding-plan.js';
|
||||
import * as minimaxCnCodingPlan from './minimax-cn-coding-plan.js';
|
||||
import * as ollamaCloud from './ollama-cloud.js';
|
||||
import * as zhipuai from './zhipuai.js';
|
||||
|
||||
const registry = {
|
||||
claude: {
|
||||
@@ -92,6 +93,12 @@ const registry = {
|
||||
providerName: ollamaCloud.providerName,
|
||||
isConfigured: ollamaCloud.isConfigured,
|
||||
fetchQuota: ollamaCloud.fetchQuota
|
||||
},
|
||||
'zhipuai-coding-plan': {
|
||||
providerId: zhipuai.providerId,
|
||||
providerName: zhipuai.providerName,
|
||||
isConfigured: zhipuai.isConfigured,
|
||||
fetchQuota: zhipuai.fetchQuota
|
||||
}
|
||||
};
|
||||
|
||||
@@ -150,3 +157,4 @@ export const fetchNanoGptQuota = nanogpt.fetchQuota;
|
||||
export const fetchMinimaxCodingPlanQuota = minimaxCodingPlan.fetchQuota;
|
||||
export const fetchMinimaxCnCodingPlanQuota = minimaxCnCodingPlan.fetchQuota;
|
||||
export const fetchOllamaCloudQuota = ollamaCloud.fetchQuota;
|
||||
export const fetchZhipuaiQuota = zhipuai.fetchQuota;
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import { readAuthFile } from '../../opencode/auth.js';
|
||||
import { readConfigLayers } from '../../opencode/shared.js';
|
||||
import {
|
||||
getAuthEntry,
|
||||
normalizeAuthEntry,
|
||||
buildResult,
|
||||
toUsageWindow,
|
||||
toNumber,
|
||||
toTimestamp,
|
||||
resolveWindowSeconds,
|
||||
resolveWindowLabel,
|
||||
normalizeTimestamp
|
||||
} from '../utils/index.js';
|
||||
|
||||
export const providerId = 'zhipuai-coding-plan';
|
||||
export const providerName = 'ZhipuAI';
|
||||
export const aliases = ['zhipuai-coding-plan', 'zhipuai', 'zhipu'];
|
||||
|
||||
function getApiKey() {
|
||||
const auth = readAuthFile();
|
||||
const oldEntry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
||||
const apiKeyFromOld = oldEntry?.key ?? oldEntry?.token;
|
||||
|
||||
if (apiKeyFromOld) {
|
||||
return apiKeyFromOld;
|
||||
}
|
||||
|
||||
try {
|
||||
const layers = readConfigLayers();
|
||||
const { mergedConfig } = layers;
|
||||
|
||||
for (const alias of aliases) {
|
||||
const providerConfig = mergedConfig?.provider?.[alias];
|
||||
if (providerConfig?.options?.apiKey) {
|
||||
return providerConfig.options.apiKey;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore read errors
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export const isConfigured = () => {
|
||||
return Boolean(getApiKey());
|
||||
};
|
||||
|
||||
export const fetchQuota = async () => {
|
||||
const apiKey = getApiKey();
|
||||
|
||||
if (!apiKey) {
|
||||
return buildResult({
|
||||
providerId,
|
||||
providerName,
|
||||
ok: false,
|
||||
configured: false,
|
||||
error: 'Not configured'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('https://open.bigmodel.cn/api/monitor/usage/quota/limit', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return buildResult({
|
||||
providerId,
|
||||
providerName,
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: `API error: ${response.status}`
|
||||
});
|
||||
}
|
||||
|
||||
const payload = await response.json();
|
||||
const limits = Array.isArray(payload?.data?.limits) ? payload.data.limits : [];
|
||||
const tokensLimit = limits.find((limit) => limit?.type === 'TOKENS_LIMIT');
|
||||
const windowSeconds = resolveWindowSeconds(tokensLimit);
|
||||
const windowLabel = resolveWindowLabel(windowSeconds);
|
||||
const resetAt = tokensLimit?.nextResetTime ? normalizeTimestamp(tokensLimit.nextResetTime) : null;
|
||||
const usedPercent = typeof tokensLimit?.percentage === 'number' ? tokensLimit.percentage : null;
|
||||
|
||||
const windows = {};
|
||||
if (tokensLimit) {
|
||||
windows[windowLabel] = toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds,
|
||||
resetAt
|
||||
});
|
||||
}
|
||||
|
||||
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'
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user