feat: 添加 Anthropic 缓存 TTL 注入开关

This commit is contained in:
shaw
2026-04-30 13:38:22 +08:00
parent 094e1171ef
commit 73b872998e
12 changed files with 394 additions and 54 deletions

View File

@@ -439,6 +439,7 @@ export interface SystemSettings {
enable_fingerprint_unification: boolean;
enable_metadata_passthrough: boolean;
enable_cch_signing: boolean;
enable_anthropic_cache_ttl_1h_injection: boolean;
web_search_emulation_enabled?: boolean;
// Payment configuration
@@ -609,6 +610,7 @@ export interface UpdateSettingsRequest {
enable_fingerprint_unification?: boolean;
enable_metadata_passthrough?: boolean;
enable_cch_signing?: boolean;
enable_anthropic_cache_ttl_1h_injection?: boolean;
// Payment configuration
payment_enabled?: boolean;
payment_min_amount?: number;

View File

@@ -5019,6 +5019,8 @@ export default {
metadataPassthroughHint: 'Pass through client\'s original metadata.user_id without rewriting. May improve upstream cache hit rates.',
cchSigning: 'CCH Signing',
cchSigningHint: 'Sign the billing header in forwarded requests with CCH hash. When disabled, the placeholder is preserved.',
anthropicCacheTTL1hInjection: 'Anthropic Cache TTL Injection',
anthropicCacheTTL1hInjectionHint: 'When enabled, existing ephemeral cache_control blocks in Anthropic OAuth/Setup Token request bodies are forced to 1h; response usage is billed back as 5m by default, with account-level TTL billing override taking priority.',
},
webSearchEmulation: {
title: 'Web Search Emulation',

View File

@@ -5178,6 +5178,8 @@ export default {
metadataPassthroughHint: '透传客户端原始 metadata.user_id不进行重写。可能提高上游缓存命中率。',
cchSigning: 'CCH 签名',
cchSigningHint: '对转发请求的 billing header 进行 CCH 哈希签名。关闭时保留原始占位符。',
anthropicCacheTTL1hInjection: 'Anthropic 缓存 TTL 注入',
anthropicCacheTTL1hInjectionHint: '开启后,对 Anthropic OAuth/Setup Token 请求体中已有的 ephemeral 缓存块强制写入 1h响应 usage 默认按 5m 回写计费,账号级 TTL 计费设置优先。',
},
webSearchEmulation: {
title: 'Web Search 模拟',

View File

@@ -3057,6 +3057,31 @@
</div>
<Toggle v-model="form.enable_cch_signing" />
</div>
<!-- Anthropic Cache TTL 1h Injection -->
<div class="flex items-center justify-between">
<div>
<label
class="text-sm font-medium text-gray-700 dark:text-gray-300"
>
{{
t(
"admin.settings.gatewayForwarding.anthropicCacheTTL1hInjection",
)
}}
</label>
<p class="mt-0.5 text-xs text-gray-500 dark:text-gray-400">
{{
t(
"admin.settings.gatewayForwarding.anthropicCacheTTL1hInjectionHint",
)
}}
</p>
</div>
<Toggle
v-model="form.enable_anthropic_cache_ttl_1h_injection"
/>
</div>
</div>
</div>
<!-- Web Search Emulation -->
@@ -5810,6 +5835,7 @@ const form = reactive<SettingsForm>({
enable_fingerprint_unification: true,
enable_metadata_passthrough: false,
enable_cch_signing: false,
enable_anthropic_cache_ttl_1h_injection: false,
// Balance & quota notification
balance_low_notify_enabled: false,
balance_low_notify_threshold: 0,
@@ -6718,6 +6744,8 @@ async function saveSettings() {
enable_fingerprint_unification: form.enable_fingerprint_unification,
enable_metadata_passthrough: form.enable_metadata_passthrough,
enable_cch_signing: form.enable_cch_signing,
enable_anthropic_cache_ttl_1h_injection:
form.enable_anthropic_cache_ttl_1h_injection,
// Payment configuration
payment_enabled: form.payment_enabled,
payment_min_amount: Number(form.payment_min_amount) || 0,

View File

@@ -362,6 +362,7 @@ const baseSettingsResponse = {
enable_fingerprint_unification: true,
enable_metadata_passthrough: false,
enable_cch_signing: false,
enable_anthropic_cache_ttl_1h_injection: false,
payment_enabled: true,
payment_min_amount: 1,
payment_max_amount: 10000,
@@ -567,6 +568,26 @@ describe("admin SettingsView payment visible method controls", () => {
expect(payload).not.toHaveProperty("payment_visible_method_wxpay_enabled");
});
it("submits Anthropic cache TTL injection gateway setting", async () => {
getSettings.mockResolvedValueOnce({
...baseSettingsResponse,
enable_anthropic_cache_ttl_1h_injection: true,
});
const wrapper = mountView();
await flushPromises();
await wrapper.find("form").trigger("submit.prevent");
await flushPromises();
expect(updateSettings).toHaveBeenCalledTimes(1);
expect(updateSettings).toHaveBeenCalledWith(
expect.objectContaining({
enable_anthropic_cache_ttl_1h_injection: true,
}),
);
});
it("updates provider enablement immediately and reloads providers", async () => {
const provider = {
id: 7,