Fix Anthropic API URL and add endpoint path handling (#10229)

* Fix Anthropic API URL and add endpoint path handling

- Remove trailing slash from Anthropic API base URL
- Add isAnthropicProvider utility function
- Update provider settings to show full endpoint URL for Anthropic
- Add migration to clean up existing Anthropic provider URLs

* Update src/renderer/src/pages/settings/ProviderSettings/ProviderSetting.tsx

Co-authored-by: Phantom <59059173+EurFelux@users.noreply.github.com>

---------

Co-authored-by: Phantom <59059173+EurFelux@users.noreply.github.com>
This commit is contained in:
SuYao 2025-09-17 17:23:23 +08:00 committed by GitHub
parent 77535b002a
commit 6afaf6244c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 4 deletions

View File

@ -1020,7 +1020,7 @@ export const PROVIDER_URLS: Record<SystemProviderId, ProviderUrls> = {
},
anthropic: {
api: {
url: 'https://api.anthropic.com/'
url: 'https://api.anthropic.com'
},
websites: {
official: 'https://anthropic.com/',

View File

@ -16,7 +16,13 @@ import { useAppDispatch } from '@renderer/store'
import { updateWebSearchProvider } from '@renderer/store/websearch'
import { isSystemProvider } from '@renderer/types'
import { ApiKeyConnectivity, HealthStatus } from '@renderer/types/healthCheck'
import { formatApiHost, formatApiKeys, getFancyProviderName, isOpenAIProvider } from '@renderer/utils'
import {
formatApiHost,
formatApiKeys,
getFancyProviderName,
isAnthropicProvider,
isOpenAIProvider
} from '@renderer/utils'
import { formatErrorMessage } from '@renderer/utils/error'
import { Button, Divider, Flex, Input, Select, Space, Switch, Tooltip } from 'antd'
import Link from 'antd/es/typography/Link'
@ -212,6 +218,10 @@ const ProviderSetting: FC<Props> = ({ providerId }) => {
if (provider.type === 'azure-openai') {
return formatApiHost(apiHost) + 'openai/v1'
}
if (provider.type === 'anthropic') {
return formatApiHost(apiHost) + 'messages'
}
return formatApiHost(apiHost) + 'responses'
}
@ -361,7 +371,7 @@ const ProviderSetting: FC<Props> = ({ providerId }) => {
</Button>
)}
</Space.Compact>
{isOpenAIProvider(provider) && (
{(isOpenAIProvider(provider) || isAnthropicProvider(provider)) && (
<SettingHelpTextRow style={{ justifyContent: 'space-between' }}>
<SettingHelpText
style={{ marginLeft: 6, marginRight: '1em', whiteSpace: 'break-spaces', wordBreak: 'break-all' }}>

View File

@ -67,7 +67,7 @@ const persistedReducer = persistReducer(
{
key: 'cherry-studio',
storage,
version: 155,
version: 156,
blacklist: ['runtime', 'messages', 'messageBlocks', 'tabs'],
migrate
},

View File

@ -2476,6 +2476,21 @@ const migrateConfig = {
logger.error('migrate 155 error', error as Error)
return state
}
},
'156': (state: RootState) => {
try {
state.llm.providers.forEach((provider) => {
if (provider.id === SystemProviderIds.anthropic) {
if (provider.apiHost.endsWith('/')) {
provider.apiHost = provider.apiHost.slice(0, -1)
}
}
})
return state
} catch (error) {
logger.error('migrate 156 error', error as Error)
return state
}
}
}

View File

@ -205,6 +205,10 @@ export function isOpenAIProvider(provider: Provider): boolean {
return !['anthropic', 'gemini', 'vertexai'].includes(provider.type)
}
export function isAnthropicProvider(provider: Provider): boolean {
return provider.type === 'anthropic'
}
/**
*
* @param {Model} model