Skip to content

Commit ebf2bf8

Browse files
committed
Add a configuration option to enable extended prompt caching for Anthropic models
- should not enable cache with Anthropic in `apps/editor/src/views/panel/backend/message-handlers/handle-prune-context.ts` - in model providers section add boolean setting "extended cache duration for anthropic" - [Fragment] should say 1 hour, not 5 minutes - add missing translations to [Fragment] - keep title, description in 'anthropic' parent [Fragment] - [Fragment] -> 'extended-cache.anthropic' - show 'extended cache for anthropic' only if there is anthropic provider on the list - build_user_content should accept vscode param and check when provider is anthropic if [Fragment] is true. [Pasted text]
1 parent ab8a929 commit ebf2bf8

13 files changed

Lines changed: 172 additions & 8 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,10 @@
789789
"type": "string",
790790
"scope": "resource"
791791
},
792+
"codeWebChat.extendedCacheDurationForAnthropic": {
793+
"type": "boolean",
794+
"scope": "resource"
795+
},
792796
"codeWebChat.contextSizeWarningThreshold": {
793797
"type": "number",
794798
"scope": "resource"

‎apps/editor/src/utils/build-user-content.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import * as vscode from 'vscode'
2+
13
export const build_user_content = (params: {
24
provider_name: string
35
part1: string
46
part2: string
7+
disable_cache?: boolean
58
}): any => {
69
const parse_text_with_images = (text: string) => {
710
if (!text.includes('<cwc-image>')) {
@@ -28,11 +31,17 @@ export const build_user_content = (params: {
2831
}
2932

3033
if (params.provider_name == 'Anthropic') {
34+
const cache_control: any = { type: 'ephemeral' }
35+
const config = vscode.workspace.getConfiguration('codeWebChat')
36+
if (config.get('extendedCacheDurationForAnthropic')) {
37+
cache_control.ttl = '1h'
38+
}
39+
3140
const user_content: any[] = [
3241
{
3342
type: 'text',
3443
text: params.part1,
35-
cache_control: { type: 'ephemeral' }
44+
...(!params.disable_cache && { cache_control })
3645
}
3746
]
3847

‎apps/editor/src/views/panel/backend/message-handlers/handle-prune-context.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ export const handle_prune_context = async (
455455
const user_content = build_user_content({
456456
provider_name: provider.name,
457457
part1,
458-
part2
458+
part2,
459+
disable_cache: true
459460
})
460461

461462
const messages = [

‎apps/editor/src/views/panel/backend/message-handlers/handle-send-to-browser.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import { ConfigPresetFormat } from '../utils/preset-format-converters'
2323
import { MODE } from '@/views/panel/types/main-view-mode'
2424
import { WebPromptType } from '@shared/types/prompt-types'
2525
import { CHATBOTS } from '@shared/constants/chatbots'
26-
import { update_last_used_preset_or_group } from './update-last-used-preset-or-group'
2726
import { dictionary } from '@shared/constants/dictionary'
2827
import {
2928
EDIT_FORMAT_INSTRUCTIONS_WHOLE,
3029
EDIT_FORMAT_INSTRUCTIONS_TRUNCATED,
3130
EDIT_FORMAT_INSTRUCTIONS_BEFORE_AFTER,
3231
EDIT_FORMAT_INSTRUCTIONS_DIFF
3332
} from '@/constants/edit-format-instructions'
33+
import { handle_update_last_used_preset_or_group } from './handle-update-last-used-preset-or-group'
3434

3535
export const handle_send_to_browser = async (params: {
3636
panel_provider: PanelProvider
@@ -74,7 +74,7 @@ export const handle_send_to_browser = async (params: {
7474
const resolved_preset_names = resolution.preset_names
7575

7676
if (params.preset_name !== undefined || params.group_name) {
77-
update_last_used_preset_or_group({
77+
handle_update_last_used_preset_or_group({
7878
panel_provider: params.panel_provider,
7979
preset_name: params.preset_name,
8080
group_name: params.group_name
@@ -515,7 +515,7 @@ async function show_preset_quick_pick(params: {
515515
}
516516
}
517517
}
518-
update_last_used_preset_or_group({
518+
handle_update_last_used_preset_or_group({
519519
panel_provider,
520520
preset_name: selected.preset_name,
521521
group_name: group_name
@@ -544,7 +544,10 @@ async function show_preset_quick_pick(params: {
544544
.filter(Boolean)
545545

546546
if (preset_names.length > 0) {
547-
update_last_used_preset_or_group({ panel_provider, group_name })
547+
handle_update_last_used_preset_or_group({
548+
panel_provider,
549+
group_name
550+
})
548551
do_resolve({ preset_names })
549552
}
550553
} else {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from 'vscode'
2+
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
3+
4+
export const handle_get_extended_cache_duration_for_anthropic = async (
5+
provider: SettingsProvider
6+
): Promise<void> => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
const enabled = config.get<boolean>(
9+
'extendedCacheDurationForAnthropic',
10+
false
11+
)
12+
provider.postMessage({
13+
command: 'EXTENDED_CACHE_DURATION_FOR_ANTHROPIC',
14+
enabled
15+
})
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as vscode from 'vscode'
2+
import { UpdateExtendedCacheDurationForAnthropicMessage } from '@/views/settings/types/messages'
3+
4+
export const handle_update_extended_cache_duration_for_anthropic = async (
5+
message: UpdateExtendedCacheDurationForAnthropicMessage
6+
): Promise<void> => {
7+
await vscode.workspace
8+
.getConfiguration('codeWebChat')
9+
.update(
10+
'extendedCacheDurationForAnthropic',
11+
message.enabled || undefined,
12+
vscode.ConfigurationTarget.Global
13+
)
14+
}

‎apps/editor/src/views/settings/backend/message-handlers/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ export * from './handle-delete-configuration'
3838
export * from './handle-get-configurations'
3939
export * from './handle-get-fix-all-automatically'
4040
export * from './handle-update-fix-all-automatically'
41+
export * from './handle-get-extended-cache-duration-for-anthropic'
42+
export * from './handle-update-extended-cache-duration-for-anthropic'
4143
export * from './handle-open-keybindings'
4244
export * from './handle-open-external-url'

‎apps/editor/src/views/settings/backend/settings-provider.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
handle_open_allow_patterns_settings,
4545
handle_get_fix_all_automatically,
4646
handle_update_fix_all_automatically,
47+
handle_get_extended_cache_duration_for_anthropic,
48+
handle_update_extended_cache_duration_for_anthropic,
4749
handle_open_keybindings,
4850
handle_open_external_url
4951
} from './message-handlers'
@@ -341,6 +343,14 @@ export class SettingsProvider {
341343
await handle_get_fix_all_automatically(this)
342344
} else if (message.command == 'UPDATE_FIX_ALL_AUTOMATICALLY') {
343345
await handle_update_fix_all_automatically(message)
346+
} else if (
347+
message.command == 'GET_EXTENDED_CACHE_DURATION_FOR_ANTHROPIC'
348+
) {
349+
await handle_get_extended_cache_duration_for_anthropic(this)
350+
} else if (
351+
message.command == 'UPDATE_EXTENDED_CACHE_DURATION_FOR_ANTHROPIC'
352+
) {
353+
await handle_update_extended_cache_duration_for_anthropic(message)
344354
} else if (message.command == 'OPEN_KEYBINDINGS') {
345355
await handle_open_keybindings(message)
346356
} else if (message.command == 'OPEN_EXTERNAL_URL') {
@@ -393,6 +403,7 @@ export class SettingsProvider {
393403
void handle_get_send_with_shift_enter(this)
394404
void handle_get_check_new_files(this)
395405
void handle_get_fix_all_automatically(this)
406+
void handle_get_extended_cache_duration_for_anthropic(this)
396407
}
397408
})
398409
)

‎apps/editor/src/views/settings/frontend/Home/Home.tsx‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useRef, useEffect, useCallback } from 'react'
1+
import { useState, useRef, useEffect, useCallback, useMemo } from 'react'
22
import { Layout as UiLayout } from '@ui/components/editor/settings/Layout'
33
import { NavigationItem as UiNavigationItem } from '@ui/components/editor/settings/NavigationItem'
44
import { ModelProvidersSection } from './sections/ModelProvidersSection'
@@ -104,6 +104,7 @@ type Props = {
104104
checkpoint_lifespan: number
105105
edit_format_instructions: EditFormatInstructions
106106
clear_checks_in_workspace_behavior: 'ignore-open-editors' | 'uncheck-all'
107+
extended_cache_duration_for_anthropic: boolean
107108
fix_all_automatically: boolean
108109
set_providers: (providers: ProviderForClient[]) => void
109110
set_edit_context_configs: (configs: ConfigurationForClient[]) => void
@@ -132,6 +133,7 @@ type Props = {
132133
value: 'ignore-open-editors' | 'uncheck-all'
133134
) => void
134135
on_fix_all_automatically_change: (enabled: boolean) => void
136+
on_extended_cache_duration_for_anthropic_change: (enabled: boolean) => void
135137
on_open_keybindings: (search?: string) => void
136138
on_open_editor_settings: () => void
137139
on_open_ignore_patterns_settings: () => void
@@ -181,6 +183,10 @@ export const Home: React.FC<Props> = (props) => {
181183
useState('')
182184
const [stuck_sections, set_stuck_sections] = useState(new Set<NavItem>())
183185

186+
const has_anthropic_provider = useMemo(() => {
187+
return props.providers.some((p) => p.name == 'Anthropic')
188+
}, [props.providers])
189+
184190
const handle_stuck_change = useCallback((id: NavItem, is_stuck: boolean) => {
185191
set_stuck_sections((prev) => {
186192
const is_currently_stuck = prev.has(id)
@@ -420,6 +426,22 @@ export const Home: React.FC<Props> = (props) => {
420426
on_edit_provider={props.on_edit_provider}
421427
on_change_api_key={props.on_change_api_key}
422428
/>
429+
{has_anthropic_provider && (
430+
<UiItem
431+
title={t('model-providers.extended-cache.anthropic.title')}
432+
description={t(
433+
'model-providers.extended-cache.anthropic.description'
434+
)}
435+
slot_right={
436+
<UiToggler
437+
is_on={props.extended_cache_duration_for_anthropic}
438+
on_toggle={
439+
props.on_extended_cache_duration_for_anthropic_change
440+
}
441+
/>
442+
}
443+
/>
444+
)}
423445
</UiGroup>
424446
</UiSection>
425447
<UiSection

‎apps/editor/src/views/settings/frontend/Settings.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export const Settings = () => {
4343
settings_hook.send_with_shift_enter !== undefined &&
4444
settings_hook.check_new_files !== undefined &&
4545
settings_hook.clear_checks_in_workspace_behavior !== undefined &&
46-
settings_hook.fix_all_automatically !== undefined
46+
settings_hook.fix_all_automatically !== undefined &&
47+
settings_hook.extended_cache_duration_for_anthropic !== undefined
4748
)
4849
}, [settings_hook])
4950

@@ -93,6 +94,9 @@ export const Settings = () => {
9394
clear_checks_in_workspace_behavior={
9495
settings_hook.clear_checks_in_workspace_behavior!
9596
}
97+
extended_cache_duration_for_anthropic={
98+
settings_hook.extended_cache_duration_for_anthropic!
99+
}
96100
fix_all_automatically={settings_hook.fix_all_automatically!}
97101
set_providers={settings_hook.set_providers}
98102
set_code_at_cursor_configs={settings_hook.set_code_at_cursor_configs}
@@ -141,6 +145,9 @@ export const Settings = () => {
141145
on_fix_all_automatically_change={
142146
settings_hook.handle_fix_all_automatically_change
143147
}
148+
on_extended_cache_duration_for_anthropic_change={
149+
settings_hook.handle_extended_cache_duration_for_anthropic_change
150+
}
144151
on_open_keybindings={settings_hook.handle_open_keybindings}
145152
on_open_editor_settings={settings_hook.handle_open_editor_settings}
146153
on_open_ignore_patterns_settings={

0 commit comments

Comments
 (0)