feat(quota): add Zhipu AI Coding Plan usage tracking (#756)
- Add zhipuai-coding-plan provider for quota monitoring - Support TOKENS_LIMIT (5-hour window) and TIME_LIMIT (MCP tools monthly) - Update UI provider list and TypeScript types - Register provider in server quota registry
This commit is contained in:
@@ -14,6 +14,7 @@ export const QUOTA_PROVIDERS: QuotaProviderMeta[] = [
|
||||
{ id: 'nano-gpt', name: 'NanoGPT' },
|
||||
{ id: 'openrouter', name: 'OpenRouter' },
|
||||
{ id: 'zai-coding-plan', name: 'z.ai' },
|
||||
{ id: 'zhipuai-coding-plan', name: 'Zhipu AI Coding Plan' },
|
||||
{ id: 'minimax-cn-coding-plan', name: 'MiniMax Coding Plan (minimaxi.com)' },
|
||||
{ id: 'minimax-coding-plan', name: 'MiniMax Coding Plan (minimax.io)' },
|
||||
{ id: 'ollama-cloud', name: 'Ollama Cloud' },
|
||||
|
||||
@@ -9,6 +9,7 @@ export type QuotaProviderId =
|
||||
| 'nano-gpt'
|
||||
| 'openrouter'
|
||||
| 'zai-coding-plan'
|
||||
| 'zhipuai-coding-plan'
|
||||
| 'minimax-coding-plan'
|
||||
| 'minimax-cn-coding-plan'
|
||||
| 'ollama-cloud'
|
||||
|
||||
@@ -71,6 +71,36 @@ type ZaiPayload = {
|
||||
};
|
||||
};
|
||||
|
||||
type ZhipuaiTokensLimit = {
|
||||
type: 'TOKENS_LIMIT';
|
||||
unit?: number;
|
||||
number?: number;
|
||||
nextResetTime?: number;
|
||||
percentage?: number;
|
||||
};
|
||||
|
||||
type ZhipuaiMcpTimeLimit = {
|
||||
type: 'TIME_LIMIT';
|
||||
unit?: number;
|
||||
number?: number;
|
||||
usage?: number;
|
||||
currentValue?: number;
|
||||
remaining?: number;
|
||||
percentage?: number;
|
||||
nextResetTime?: number;
|
||||
usageDetails?: Array<{
|
||||
modelCode?: string;
|
||||
usage?: number;
|
||||
}>;
|
||||
};
|
||||
|
||||
type ZhipuaiPayload = {
|
||||
data?: {
|
||||
limits?: Array<ZhipuaiTokensLimit | ZhipuaiMcpTimeLimit>;
|
||||
level?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type ProviderResult = {
|
||||
providerId: string;
|
||||
providerName: string;
|
||||
@@ -355,6 +385,11 @@ export const listConfiguredQuotaProviders = () => {
|
||||
configured.add('zai-coding-plan');
|
||||
}
|
||||
|
||||
const zhipuaiAuth = normalizeAuthEntry(getAuthEntry(auth, ['zhipuai-coding-plan']));
|
||||
if (zhipuaiAuth && ((zhipuaiAuth as Record<string, unknown>).key || (zhipuaiAuth as Record<string, unknown>).token)) {
|
||||
configured.add('zhipuai-coding-plan');
|
||||
}
|
||||
|
||||
const kimiAuth = normalizeAuthEntry(getAuthEntry(auth, ['kimi-for-coding', 'kimi']));
|
||||
if (kimiAuth && ((kimiAuth as Record<string, unknown>).key || (kimiAuth as Record<string, unknown>).token)) {
|
||||
configured.add('kimi-for-coding');
|
||||
@@ -1259,6 +1294,93 @@ export const fetchZaiQuota = async (): Promise<ProviderResult> => {
|
||||
}
|
||||
};
|
||||
|
||||
export const fetchZhipuaiCodingPlanQuota = async (): Promise<ProviderResult> => {
|
||||
const auth = readAuthFile();
|
||||
const entry = normalizeAuthEntry(getAuthEntry(auth, ['zhipuai-coding-plan'])) as Record<string, unknown> | null;
|
||||
const apiKey = (entry?.key as string | undefined) ?? (entry?.token as string | undefined);
|
||||
|
||||
if (!apiKey) {
|
||||
return buildResult({
|
||||
providerId: 'zhipuai-coding-plan',
|
||||
providerName: 'Zhipu AI Coding Plan',
|
||||
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: 'zhipuai-coding-plan',
|
||||
providerName: 'Zhipu AI Coding Plan',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: `API error: ${response.status}`,
|
||||
});
|
||||
}
|
||||
|
||||
const payload = await response.json() as ZhipuaiPayload;
|
||||
const limits = Array.isArray(payload?.data?.limits) ? payload.data.limits : [];
|
||||
|
||||
const tokensLimit = limits.find((limit): limit is ZhipuaiTokensLimit => limit?.type === 'TOKENS_LIMIT');
|
||||
const mcpToolsTimeLimit = limits.find((limit): limit is ZhipuaiMcpTimeLimit => limit?.type === 'TIME_LIMIT');
|
||||
|
||||
const windows: Record<string, UsageWindow> = {};
|
||||
|
||||
// Handle TOKENS_LIMIT (5-hour window for token usage)
|
||||
if (tokensLimit) {
|
||||
const windowSeconds = resolveWindowSeconds(tokensLimit);
|
||||
const resetAt = tokensLimit?.nextResetTime ? normalizeTimestamp(tokensLimit.nextResetTime) : null;
|
||||
const usedPercent = typeof tokensLimit?.percentage === 'number' ? tokensLimit.percentage : null;
|
||||
|
||||
windows['Tokens'] = toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds,
|
||||
resetAt,
|
||||
});
|
||||
}
|
||||
|
||||
// Handle TIME_LIMIT (MCP tools monthly window)
|
||||
if (mcpToolsTimeLimit) {
|
||||
// TIME_LIMIT unit=5 means 1 month (30 days)
|
||||
const monthSeconds = 30 * 24 * 60 * 60;
|
||||
const resetAt = mcpToolsTimeLimit?.nextResetTime ? normalizeTimestamp(mcpToolsTimeLimit.nextResetTime) : null;
|
||||
const usedPercent = typeof mcpToolsTimeLimit?.percentage === 'number' ? mcpToolsTimeLimit.percentage : null;
|
||||
|
||||
windows['MCP Tools'] = toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds: monthSeconds,
|
||||
resetAt,
|
||||
});
|
||||
}
|
||||
|
||||
return buildResult({
|
||||
providerId: 'zhipuai-coding-plan',
|
||||
providerName: 'Zhipu AI Coding Plan',
|
||||
ok: true,
|
||||
configured: true,
|
||||
usage: { windows },
|
||||
});
|
||||
} catch (error) {
|
||||
return buildResult({
|
||||
providerId: 'zhipuai-coding-plan',
|
||||
providerName: 'Zhipu AI Coding Plan',
|
||||
ok: false,
|
||||
configured: true,
|
||||
error: error instanceof Error ? error.message : 'Request failed',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const NANO_GPT_DAILY_WINDOW_SECONDS = 86400;
|
||||
|
||||
export const fetchNanoGptQuota = async (): Promise<ProviderResult> => {
|
||||
@@ -1384,6 +1506,8 @@ export const fetchQuotaForProvider = async (providerId: string): Promise<Provide
|
||||
return fetchOpenRouterQuota();
|
||||
case 'zai-coding-plan':
|
||||
return fetchZaiQuota();
|
||||
case 'zhipuai-coding-plan':
|
||||
return fetchZhipuaiCodingPlanQuota();
|
||||
default:
|
||||
return buildResult({
|
||||
providerId,
|
||||
|
||||
@@ -26,6 +26,7 @@ These provider IDs are currently dispatchable via `fetchQuotaForProvider(provide
|
||||
| `nano-gpt` | NanoGPT | `providers/nanogpt.js` | `nano-gpt`, `nanogpt`, `nano_gpt` |
|
||||
| `openrouter` | OpenRouter | `providers/openrouter.js` | `openrouter` |
|
||||
| `zai-coding-plan` | z.ai | `providers/zai.js` | `zai-coding-plan`, `zai`, `z.ai` |
|
||||
| `zhipuai-coding-plan` | Zhipu AI Coding Plan | `providers/zhipuai-coding-plan.js` | `zhipuai-coding-plan` |
|
||||
| `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) |
|
||||
|
||||
@@ -16,6 +16,7 @@ import * as nanogpt from './nanogpt.js';
|
||||
import * as openai from './openai.js';
|
||||
import * as openrouter from './openrouter.js';
|
||||
import * as zai from './zai.js';
|
||||
import * as zhipuaiCodingPlan from './zhipuai-coding-plan.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';
|
||||
@@ -46,6 +47,12 @@ const registry = {
|
||||
isConfigured: zai.isConfigured,
|
||||
fetchQuota: zai.fetchQuota
|
||||
},
|
||||
'zhipuai-coding-plan': {
|
||||
providerId: zhipuaiCodingPlan.providerId,
|
||||
providerName: zhipuaiCodingPlan.providerName,
|
||||
isConfigured: zhipuaiCodingPlan.isConfigured,
|
||||
fetchQuota: zhipuaiCodingPlan.fetchQuota
|
||||
},
|
||||
'kimi-for-coding': {
|
||||
providerId: kimi.providerId,
|
||||
providerName: kimi.providerName,
|
||||
@@ -153,6 +160,7 @@ export const fetchCopilotAddonQuota = copilot.fetchQuotaAddon;
|
||||
export const fetchKimiQuota = kimi.fetchQuota;
|
||||
export const fetchOpenRouterQuota = openrouter.fetchQuota;
|
||||
export const fetchZaiQuota = zai.fetchQuota;
|
||||
export const fetchZhipuaiCodingPlanQuota = zhipuaiCodingPlan.fetchQuota;
|
||||
export const fetchNanoGptQuota = nanogpt.fetchQuota;
|
||||
export const fetchMinimaxCodingPlanQuota = minimaxCodingPlan.fetchQuota;
|
||||
export const fetchMinimaxCnCodingPlanQuota = minimaxCnCodingPlan.fetchQuota;
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Zhipu AI Coding Plan quota fetch
|
||||
*
|
||||
* API: https://open.bigmodel.cn/api/monitor/usage/quota/limit
|
||||
*
|
||||
* Response limits:
|
||||
* - TOKENS_LIMIT: Token usage (5-hour rolling window)
|
||||
* - TIME_LIMIT: MCP tools usage (monthly window)
|
||||
*
|
||||
* @typedef {Object} TokensLimit
|
||||
* @property {string} type - 'TOKENS_LIMIT'
|
||||
* @property {number} [unit]
|
||||
* @property {number} [number]
|
||||
* @property {number} [nextResetTime]
|
||||
* @property {number} [percentage]
|
||||
*
|
||||
* @typedef {Object} McpToolsTimeLimit
|
||||
* @property {string} type - 'TIME_LIMIT'
|
||||
* @property {number} [unit]
|
||||
* @property {number} [number]
|
||||
* @property {number} [usage]
|
||||
* @property {number} [currentValue]
|
||||
* @property {number} [remaining]
|
||||
* @property {number} [percentage]
|
||||
* @property {number} [nextResetTime]
|
||||
* @property {Array<{modelCode: string, usage: number}>} [usageDetails]
|
||||
*/
|
||||
import { readAuthFile } from '../../opencode/auth.js';
|
||||
import {
|
||||
getAuthEntry,
|
||||
normalizeAuthEntry,
|
||||
buildResult,
|
||||
toUsageWindow,
|
||||
resolveWindowSeconds,
|
||||
normalizeTimestamp
|
||||
} from '../utils/index.js';
|
||||
|
||||
export const providerId = 'zhipuai-coding-plan';
|
||||
export const providerName = 'Zhipu AI Coding Plan';
|
||||
export const aliases = ['zhipuai-coding-plan'];
|
||||
|
||||
export const isConfigured = () => {
|
||||
const auth = readAuthFile();
|
||||
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
||||
return Boolean(entry?.key || entry?.token);
|
||||
};
|
||||
|
||||
export const fetchQuota = async () => {
|
||||
const auth = readAuthFile();
|
||||
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
|
||||
const apiKey = entry?.key ?? entry?.token;
|
||||
|
||||
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 mcpToolsTimeLimit = limits.find((limit) => limit?.type === 'TIME_LIMIT');
|
||||
|
||||
const windows = {};
|
||||
|
||||
// Handle TOKENS_LIMIT (5-hour window for token usage)
|
||||
if (tokensLimit) {
|
||||
const windowSeconds = resolveWindowSeconds(tokensLimit);
|
||||
const resetAt = tokensLimit?.nextResetTime ? normalizeTimestamp(tokensLimit.nextResetTime) : null;
|
||||
const usedPercent = typeof tokensLimit?.percentage === 'number' ? tokensLimit.percentage : null;
|
||||
|
||||
windows['Tokens'] = toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds,
|
||||
resetAt
|
||||
});
|
||||
}
|
||||
|
||||
// Handle TIME_LIMIT (MCP tools monthly window)
|
||||
if (mcpToolsTimeLimit) {
|
||||
// TIME_LIMIT unit=5 means 1 month (30 days)
|
||||
const monthSeconds = 30 * 24 * 60 * 60;
|
||||
const resetAt = mcpToolsTimeLimit?.nextResetTime ? normalizeTimestamp(mcpToolsTimeLimit.nextResetTime) : null;
|
||||
const usedPercent = typeof mcpToolsTimeLimit?.percentage === 'number' ? mcpToolsTimeLimit.percentage : null;
|
||||
|
||||
windows['MCP Tools'] = toUsageWindow({
|
||||
usedPercent,
|
||||
windowSeconds: monthSeconds,
|
||||
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