mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 12:44:18 +08:00
c4630b5c26
Query coding-plan / agent-plan usage for Volcengine Ark via the control-plane OpenAPI (open.volcengineapi.com), which requires account-level AccessKey signing rather than the inference API key. - Implement Volcengine Signature V4 (an AWS SigV4 variant: fixed canonical header order, "HMAC-SHA256" algorithm without the AWS4 prefix, scope ending in "request", service "ark") in services/coding_plan.rs - Auto-detect the plan by probing GetAFPUsage (Agent Plan, AFP five-hour/weekly/monthly quotas) first, falling back to GetCodingPlanUsage (Coding Plan, session/weekly/monthly percentages); a single credential covers whichever plan the account holds - Parse the real response shape: the window label lives in the `Level` field; guard ResetTimestamp <= 0 (session returns -1) - Store account-level credentials on UsageScript (accessKeyId / secretAccessKey), threaded through both the test command and the TOKEN_PLAN auto-refresh path - Add an independent AK/SK input block with a detailed hint pointing to the Volcengine console -> API Access Keys - Add the `monthly` tier label (frontend TIER_I18N_KEYS + tray TIER_LABEL_GROUPS + subscription service) - Sync zh / en / ja / zh-TW locales and the usage-query docs
95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
/**
|
|
* Coding Plan 供应商的 base_url 路由表。
|
|
*
|
|
* 与后端 `src-tauri/src/services/coding_plan.rs::detect_provider` 保持一致:
|
|
* 后端靠 `url.contains(...)` 做子串判断,前端这里用 RegExp 做同效匹配。
|
|
* 新增供应商时改这一处即可(UsageScriptModal 下拉 + useProviderActions
|
|
* 新建自动注入 + 托盘识别全部复用)。
|
|
*/
|
|
import { createUsageScript } from "@/types";
|
|
import { TEMPLATE_TYPES } from "@/config/constants";
|
|
|
|
export interface CodingPlanProviderEntry {
|
|
/** 与后端 QuotaTier 的 `codingPlanProvider` 取值对齐 */
|
|
id: "kimi" | "zhipu" | "minimax" | "zenmux" | "volcengine";
|
|
/** UsageScriptModal 下拉显示用 */
|
|
label: string;
|
|
/** base_url 匹配规则 */
|
|
pattern: RegExp;
|
|
}
|
|
|
|
export const CODING_PLAN_PROVIDERS: readonly CodingPlanProviderEntry[] = [
|
|
{ id: "kimi", label: "Kimi For Coding", pattern: /api\.kimi\.com\/coding/i },
|
|
{
|
|
id: "zhipu",
|
|
label: "Zhipu GLM (智谱)",
|
|
pattern: /bigmodel\.cn|api\.z\.ai/i,
|
|
},
|
|
{
|
|
id: "minimax",
|
|
label: "MiniMax",
|
|
pattern: /api\.minimaxi?\.com|api\.minimax\.io/i,
|
|
},
|
|
{
|
|
id: "zenmux",
|
|
label: "ZenMux",
|
|
pattern: /zenmux\./i,
|
|
},
|
|
{
|
|
// 火山方舟 Agent Plan / Coding Plan。base_url 形如
|
|
// ark.cn-beijing.volces.com/api/coding[/v3];与后端 detect_provider 的
|
|
// `volces.com/api/coding` 子串判断同效。
|
|
id: "volcengine",
|
|
label: "火山方舟 (Volcengine)",
|
|
pattern: /volces\.com\/api\/coding/i,
|
|
},
|
|
] as const;
|
|
|
|
/** 根据 Base URL 自动检测 Coding Plan 供应商;未命中返回 null */
|
|
export function detectCodingPlanProvider(
|
|
baseUrl: string | undefined | null,
|
|
): CodingPlanProviderEntry["id"] | null {
|
|
if (!baseUrl) return null;
|
|
for (const cp of CODING_PLAN_PROVIDERS) {
|
|
if (cp.pattern.test(baseUrl)) return cp.id;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 新建 Claude 供应商时,若 `ANTHROPIC_BASE_URL` 命中 Coding Plan 路由表,
|
|
* 自动把 `meta.usage_script` 标记为 token_plan 并启用。
|
|
*
|
|
* - 仅在 `meta.usage_script` 完全缺失时注入,不覆盖用户/UsageScriptModal 已有配置
|
|
* - 仅对 Claude app 生效:后端 `commands/provider.rs` 的 token_plan 分支只处理 Claude
|
|
* supplier 的 `settings_config.env.ANTHROPIC_BASE_URL`
|
|
* - code 置空:Rust 端走专用 `coding_plan::get_coding_plan_quota`,不执行 JS 脚本
|
|
*/
|
|
export function injectCodingPlanUsageScript<
|
|
T extends {
|
|
settingsConfig?: Record<string, any>;
|
|
meta?: Record<string, any>;
|
|
},
|
|
>(appId: string, provider: T): T {
|
|
if (appId !== "claude") return provider;
|
|
if (provider.meta?.usage_script) return provider;
|
|
|
|
const baseUrl = provider.settingsConfig?.env?.ANTHROPIC_BASE_URL;
|
|
const codingPlanProvider = detectCodingPlanProvider(
|
|
typeof baseUrl === "string" ? baseUrl : null,
|
|
);
|
|
if (!codingPlanProvider) return provider;
|
|
|
|
return {
|
|
...provider,
|
|
meta: {
|
|
...(provider.meta ?? {}),
|
|
usage_script: createUsageScript({
|
|
enabled: true,
|
|
templateType: TEMPLATE_TYPES.TOKEN_PLAN,
|
|
codingPlanProvider,
|
|
}),
|
|
},
|
|
};
|
|
}
|