mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-24 12:44:18 +08:00
feat(codex): add remote compaction toggle for third-party providers
Write model_providers name as "OpenAI" to let Codex attempt remote compaction through compatible endpoints. Hidden for official providers.
This commit is contained in:
@@ -410,6 +410,10 @@ const TOML_WIRE_API_PATTERN =
|
||||
/^\s*wire_api\s*=\s*(["'])([^"'\r\n]+)\1\s*(?:#.*)?$/;
|
||||
const TOML_MODEL_PROVIDER_LINE_PATTERN =
|
||||
/^\s*model_provider\s*=\s*(["'])([^"'\r\n]+)\1\s*(?:#.*)?$/;
|
||||
const TOML_PROVIDER_NAME_PATTERN =
|
||||
/^\s*name\s*=\s*(["'])([^"'\r\n]+)\1\s*(?:#.*)?$/;
|
||||
const TOML_PROVIDER_NAME_REPLACE_PATTERN =
|
||||
/^(\s*name\s*=\s*)(?:"(?:\\.|[^"\\\r\n])*"|'[^'\r\n]*')(\s*(?:#.*)?)$/;
|
||||
const TOML_GOALS_FEATURE_PATTERN = /^\s*goals\s*=\s*(true|false)\s*(?:#.*)?$/;
|
||||
const TOML_GOALS_FEATURE_REPLACE_PATTERN =
|
||||
/^(\s*goals\s*=\s*)(true|false)(\s*(?:#.*)?)$/;
|
||||
@@ -670,6 +674,9 @@ const escapeTomlBasicString = (value: string): string =>
|
||||
return `\\u${ch.charCodeAt(0).toString(16).padStart(4, "0")}`;
|
||||
});
|
||||
|
||||
const tomlBasicString = (value: string): string =>
|
||||
`"${escapeTomlBasicString(value)}"`;
|
||||
|
||||
const CODEX_CHAT_WIRE_API_VALUES = new Set([
|
||||
"chat",
|
||||
"chat_completions",
|
||||
@@ -908,6 +915,106 @@ export const setCodexGoalMode = (
|
||||
return finalizeTomlText(lines);
|
||||
};
|
||||
|
||||
export const isCodexRemoteCompactionEnabled = (
|
||||
configText: string | undefined | null,
|
||||
): boolean => {
|
||||
try {
|
||||
const raw = typeof configText === "string" ? configText : "";
|
||||
const text = normalizeTomlText(raw);
|
||||
if (!text) return false;
|
||||
|
||||
try {
|
||||
const parsed = parseToml(text) as Record<string, any>;
|
||||
const providerId =
|
||||
typeof parsed.model_provider === "string"
|
||||
? parsed.model_provider.trim()
|
||||
: "";
|
||||
if (!providerId || !isCustomCodexModelProviderId(providerId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parsed.model_providers?.[providerId]?.name === "OpenAI";
|
||||
} catch {
|
||||
// Fall back to line scanning while the user is editing invalid TOML.
|
||||
}
|
||||
|
||||
const lines = text.split("\n");
|
||||
const targetSectionName = getCodexCustomProviderSectionName(text);
|
||||
if (!targetSectionName) return false;
|
||||
|
||||
const sectionRange = getTomlSectionRange(lines, targetSectionName);
|
||||
if (!sectionRange) return false;
|
||||
|
||||
const nameAssignment = findTomlAssignmentInRange(
|
||||
lines,
|
||||
TOML_PROVIDER_NAME_PATTERN,
|
||||
sectionRange.bodyStartIndex,
|
||||
sectionRange.bodyEndIndex,
|
||||
targetSectionName,
|
||||
);
|
||||
|
||||
return nameAssignment?.value === "OpenAI";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const setCodexRemoteCompaction = (
|
||||
configText: string,
|
||||
enabled: boolean,
|
||||
fallbackProviderName?: string,
|
||||
): string => {
|
||||
const normalizedText = normalizeTomlText(configText);
|
||||
const lines = normalizedText ? normalizedText.split("\n") : [];
|
||||
const targetSectionName = getCodexCustomProviderSectionName(normalizedText);
|
||||
|
||||
if (!targetSectionName) {
|
||||
return normalizedText;
|
||||
}
|
||||
|
||||
let targetSectionRange = getTomlSectionRange(lines, targetSectionName);
|
||||
const replacementName = enabled
|
||||
? "OpenAI"
|
||||
: fallbackProviderName?.trim() ||
|
||||
getCodexModelProviderName(normalizedText) ||
|
||||
"custom";
|
||||
const replacementLine = `name = ${tomlBasicString(replacementName)}`;
|
||||
|
||||
if (targetSectionRange) {
|
||||
const nameLine = findTomlLineInRange(
|
||||
lines,
|
||||
TOML_PROVIDER_NAME_REPLACE_PATTERN,
|
||||
targetSectionRange.bodyStartIndex,
|
||||
targetSectionRange.bodyEndIndex,
|
||||
);
|
||||
|
||||
if (nameLine !== -1) {
|
||||
lines[nameLine] = lines[nameLine].replace(
|
||||
TOML_PROVIDER_NAME_REPLACE_PATTERN,
|
||||
`$1${tomlBasicString(replacementName)}$2`,
|
||||
);
|
||||
return finalizeTomlText(lines);
|
||||
}
|
||||
|
||||
lines.splice(
|
||||
getTomlSectionInsertIndex(lines, targetSectionRange),
|
||||
0,
|
||||
replacementLine,
|
||||
);
|
||||
return finalizeTomlText(lines);
|
||||
}
|
||||
|
||||
if (!enabled) return normalizedText;
|
||||
|
||||
if (lines.length > 0 && lines[lines.length - 1].trim() !== "") {
|
||||
lines.push("");
|
||||
}
|
||||
|
||||
lines.push(`[${targetSectionName}]`, replacementLine);
|
||||
targetSectionRange = getTomlSectionRange(lines, targetSectionName);
|
||||
return targetSectionRange ? finalizeTomlText(lines) : normalizedText;
|
||||
};
|
||||
|
||||
// 从 Codex 的 TOML 配置文本中提取 base_url(支持单/双引号)
|
||||
export const extractCodexBaseUrl = (
|
||||
configText: string | undefined | null,
|
||||
|
||||
Reference in New Issue
Block a user