Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/renderer/modals/SessionSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,33 @@ export function ChatConfig({
</Flex>
</Stack>
)}
{settings?.provider === ModelProviderEnum.Claude && (
<Stack gap="xs">
<Text size="sm" fw="600">
{t('Prompt Cache')}
</Text>

<SegmentedControl
value={settings?.providerOptions?.claude?.cacheTTL ?? 'auto'}
onChange={(value) =>
onSettingsChange({
providerOptions: {
...settings?.providerOptions,
claude: {
...settings?.providerOptions?.claude,
cacheTTL: value as 'auto' | '5m' | '1h',
},
},
})
}
data={[
{ label: t('Auto'), value: 'auto' },
{ label: t('5 min'), value: '5m' },
{ label: t('1 hour'), value: '1h' },
]}
/>
</Stack>
)}
<ReasoningControlConfig settings={settings} onSettingsChange={onSettingsChange} />
</Stack>
)
Expand Down
1 change: 1 addition & 0 deletions src/shared/providers/definitions/claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const claudeProvider = defineProvider({
topP: config.settings.topP,
maxOutputTokens: config.settings.maxTokens,
stream: config.settings.stream,
cacheTTL: config.settings.providerOptions?.claude?.cacheTTL,
extraHeaders: oauthHeaders,
customFetch:
isOAuth && credentialManager ? createBearerOAuthFetch(config.dependencies, credentialManager) : undefined,
Expand Down
21 changes: 19 additions & 2 deletions src/shared/providers/definitions/models/claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Options {
topP?: number
maxOutputTokens?: number
stream?: boolean
cacheTTL?: 'auto' | '5m' | '1h'
extraHeaders?: Record<string, string>
customFetch?: typeof globalThis.fetch
authToken?: string
Expand Down Expand Up @@ -132,14 +133,30 @@ export default class Claude extends AbstractAISDKModel {
}

public async chat(messages: ModelMessage[], options: CallChatCompletionOptions): Promise<StreamTextResult> {
return super.chat(addAnthropicCacheControl(messages), options)
const ttl =
this.options.cacheTTL === '5m'
? '5m'
: this.options.cacheTTL === '1h'
? '1h'
: messages.length >= 10
? '1h'
: '5m'
return super.chat(addAnthropicCacheControl(messages, ttl), options)
}

public async *chatStream<T extends ToolSet>(
messages: ModelMessage[],
options: ChatStreamOptions
): AsyncGenerator<ModelStreamPart<T>> {
yield* super.chatStream<T>(addAnthropicCacheControl(messages), options)
const ttl =
this.options.cacheTTL === '5m'
? '5m'
: this.options.cacheTTL === '1h'
? '1h'
: messages.length >= 10
? '1h'
: '5m'
yield* super.chatStream<T>(addAnthropicCacheControl(messages, ttl), options)
}

// https://docs.anthropic.com/en/docs/api/models
Expand Down
4 changes: 4 additions & 0 deletions src/shared/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ const ProviderBaseInfoSchema = z.discriminatedUnion('isCustom', [
CustomProviderBaseInfoSchema,
])

export const ClaudeCacheTTLSchema = z.enum(['auto', '5m', '1h'])

const ClaudeParamsSchema = z.object({
thinking: z
.object({
Expand All @@ -125,7 +127,9 @@ const ClaudeParamsSchema = z.object({
})
.optional()
.catch(undefined),

effort: z.enum(['low', 'medium', 'high']).optional().catch(undefined),
cacheTTL: ClaudeCacheTTLSchema.optional().catch('auto'),
})

const OpenAIParamsSchema = z.object({
Expand Down