mirror of
https://github.com/Wei-Shaw/sub2api
synced 2026-12-06 17:32:31 +00:00
#7395 made Codex (and Grok) imports store a base URL ending in /v1, but the usage script still appends a hard-coded /v1/usage, so CC Switch requests /v1/v1/usage, gets a 404 and shows "query failed" for every newly imported Codex/Grok provider. Move the script into ccswitchImport.ts and strip an existing trailing /v1 from {{baseUrl}} before appending /v1/usage, which covers both the imported value and base URLs users edit either way later.
110 lines
3.2 KiB
TypeScript
110 lines
3.2 KiB
TypeScript
import type { GroupPlatform } from '@/types'
|
|
|
|
export const OPENAI_CC_SWITCH_CODEX_MODEL = 'gpt-5.5'
|
|
export const GROK_CC_SWITCH_MODEL = 'grok-4.5'
|
|
|
|
export type CcSwitchClientType = 'claude' | 'gemini'
|
|
|
|
export interface CcSwitchImportConfig {
|
|
app: string
|
|
endpoint: string
|
|
model?: string
|
|
}
|
|
|
|
export interface CcSwitchImportDeeplinkInput {
|
|
baseUrl: string
|
|
platform?: GroupPlatform | null
|
|
clientType: CcSwitchClientType
|
|
providerName: string
|
|
apiKey: string
|
|
usageScript: string
|
|
}
|
|
|
|
/**
|
|
* Balance query CC Switch runs against the imported provider. CC Switch fills
|
|
* `{{baseUrl}}` with the provider's base URL as stored — Codex and Grok imports
|
|
* carry a trailing `/v1` (see `withV1Endpoint`), Claude ones do not, and users
|
|
* may edit it either way afterwards — then evaluates the script, so the URL
|
|
* strips an existing `/v1` instead of blindly appending one (`/v1/v1/usage`
|
|
* is a 404 and CC Switch shows "query failed").
|
|
*/
|
|
export const CC_SWITCH_USAGE_SCRIPT = `({
|
|
request: {
|
|
url: "{{baseUrl}}".replace(/\\/+$/, "").replace(/\\/v1$/, "") + "/v1/usage",
|
|
method: "GET",
|
|
headers: { "Authorization": "Bearer {{apiKey}}" }
|
|
},
|
|
extractor: function(response) {
|
|
const remaining = response?.remaining ?? response?.quota?.remaining ?? response?.balance;
|
|
const unit = response?.unit ?? response?.quota?.unit ?? "USD";
|
|
return {
|
|
isValid: response?.is_active ?? response?.isValid ?? true,
|
|
remaining,
|
|
unit
|
|
};
|
|
}
|
|
})`
|
|
|
|
function withV1Endpoint(baseUrl: string): string {
|
|
const normalizedBaseUrl = baseUrl.replace(/\/+$/, '')
|
|
return normalizedBaseUrl.endsWith('/v1') ? normalizedBaseUrl : `${normalizedBaseUrl}/v1`
|
|
}
|
|
|
|
export function resolveCcSwitchImportConfig(
|
|
platform: GroupPlatform | undefined | null,
|
|
clientType: CcSwitchClientType,
|
|
baseUrl: string
|
|
): CcSwitchImportConfig {
|
|
switch (platform || 'anthropic') {
|
|
case 'antigravity':
|
|
return {
|
|
app: clientType === 'gemini' ? 'gemini' : 'claude',
|
|
endpoint: `${baseUrl.replace(/\/+$/, '')}/antigravity`
|
|
}
|
|
case 'openai':
|
|
return {
|
|
app: 'codex',
|
|
endpoint: withV1Endpoint(baseUrl),
|
|
model: OPENAI_CC_SWITCH_CODEX_MODEL
|
|
}
|
|
case 'gemini':
|
|
return {
|
|
app: 'gemini',
|
|
endpoint: baseUrl
|
|
}
|
|
case 'grok':
|
|
return {
|
|
app: 'grokbuild',
|
|
endpoint: withV1Endpoint(baseUrl),
|
|
model: GROK_CC_SWITCH_MODEL
|
|
}
|
|
default:
|
|
return {
|
|
app: 'claude',
|
|
endpoint: baseUrl
|
|
}
|
|
}
|
|
}
|
|
|
|
export function buildCcSwitchImportDeeplink(input: CcSwitchImportDeeplinkInput): string {
|
|
const config = resolveCcSwitchImportConfig(input.platform, input.clientType, input.baseUrl)
|
|
const entries: [string, string][] = [
|
|
['resource', 'provider'],
|
|
['app', config.app],
|
|
['name', input.providerName],
|
|
['homepage', input.baseUrl],
|
|
['endpoint', config.endpoint],
|
|
['apiKey', input.apiKey],
|
|
['configFormat', 'json'],
|
|
['usageEnabled', 'true'],
|
|
['usageScript', btoa(input.usageScript)],
|
|
['usageAutoInterval', '30']
|
|
]
|
|
|
|
if (config.model) {
|
|
entries.splice(2, 0, ['model', config.model])
|
|
}
|
|
|
|
return `ccswitch://v1/import?${new URLSearchParams(entries).toString()}`
|
|
}
|