mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 06:30:10 +08:00
fix: make anthropic model provided by cherryin visible to agent (#10695)
This commit is contained in:
parent
004d6d8201
commit
7b3b73d390
@ -1,3 +1,5 @@
|
||||
import { isEmpty } from 'lodash'
|
||||
|
||||
import { ApiModel, ApiModelsFilter, ApiModelsResponse } from '../../../renderer/src/types/apiModels'
|
||||
import { loggerService } from '../../services/LoggerService'
|
||||
import { getAvailableProviders, listAllAvailableModels, transformModelToOpenAI } from '../utils'
|
||||
@ -8,6 +10,10 @@ const logger = loggerService.withContext('ModelsService')
|
||||
|
||||
export type ModelsFilter = ApiModelsFilter
|
||||
|
||||
const isAnthropicProvider = (provider: { type: string; anthropicApiHost?: string }) => {
|
||||
return provider.type === 'anthropic' || !isEmpty(provider.anthropicApiHost?.trim())
|
||||
}
|
||||
|
||||
export class ModelsService {
|
||||
async getModels(filter: ModelsFilter): Promise<ApiModelsResponse> {
|
||||
try {
|
||||
@ -16,9 +22,7 @@ export class ModelsService {
|
||||
let providers = await getAvailableProviders()
|
||||
|
||||
if (filter.providerType === 'anthropic') {
|
||||
providers = providers.filter(
|
||||
(p) => p.type === 'anthropic' || (p.anthropicApiHost !== undefined && p.anthropicApiHost.trim() !== '')
|
||||
)
|
||||
providers = providers.filter(isAnthropicProvider)
|
||||
}
|
||||
|
||||
const models = await listAllAvailableModels(providers)
|
||||
@ -41,6 +45,10 @@ export class ModelsService {
|
||||
continue
|
||||
}
|
||||
|
||||
if (filter.supportAnthropic && model.endpoint_type !== 'anthropic' && !isAnthropicProvider(provider)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const openAIModel = transformModelToOpenAI(model, provider)
|
||||
const fullModelId = openAIModel.id // This is already in format "provider:model_id"
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { CacheService } from '@main/services/CacheService'
|
||||
import { loggerService } from '@main/services/LoggerService'
|
||||
import { reduxService } from '@main/services/ReduxService'
|
||||
import { ApiModel, Model, Provider } from '@types'
|
||||
import { ApiModel, EndpointType, Model, Provider } from '@types'
|
||||
|
||||
const logger = loggerService.withContext('ApiServerUtils')
|
||||
|
||||
@ -114,6 +114,7 @@ export async function validateModelId(model: string): Promise<{
|
||||
error?: ModelValidationError
|
||||
provider?: Provider
|
||||
modelId?: string
|
||||
modelEndpointType?: EndpointType
|
||||
}> {
|
||||
try {
|
||||
if (!model || typeof model !== 'string') {
|
||||
@ -166,7 +167,8 @@ export async function validateModelId(model: string): Promise<{
|
||||
}
|
||||
|
||||
// Check if model exists in provider
|
||||
const modelExists = provider.models?.some((m) => m.id === modelId)
|
||||
const modelInProvider = provider.models?.find((m) => m.id === modelId)
|
||||
const modelExists = !!modelInProvider
|
||||
if (!modelExists) {
|
||||
const availableModels = provider.models?.map((m) => m.id).join(', ') || 'none'
|
||||
return {
|
||||
@ -179,10 +181,13 @@ export async function validateModelId(model: string): Promise<{
|
||||
}
|
||||
}
|
||||
|
||||
const modelEndpointType = modelInProvider?.endpoint_type
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
provider,
|
||||
modelId
|
||||
modelId,
|
||||
modelEndpointType
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error('Error validating model ID', { error, model })
|
||||
|
||||
@ -8,6 +8,7 @@ import { config as apiConfigService } from '@main/apiServer/config'
|
||||
import { validateModelId } from '@main/apiServer/utils'
|
||||
import getLoginShellEnvironment from '@main/utils/shell-env'
|
||||
import { app } from 'electron'
|
||||
import { isEmpty } from 'lodash'
|
||||
|
||||
import { GetAgentSessionResponse } from '../..'
|
||||
import { AgentServiceInterface, AgentStream, AgentStreamEvent } from '../../interfaces/AgentStreamInterface'
|
||||
@ -60,11 +61,20 @@ class ClaudeCodeService implements AgentServiceInterface {
|
||||
})
|
||||
return aiStream
|
||||
}
|
||||
if (
|
||||
(modelInfo.provider?.type !== 'anthropic' &&
|
||||
(modelInfo.provider?.anthropicApiHost === undefined || modelInfo.provider.anthropicApiHost.trim() === '')) ||
|
||||
modelInfo.provider.apiKey === ''
|
||||
) {
|
||||
|
||||
const validateModelInfo: (m: typeof modelInfo) => boolean = (m) => {
|
||||
const { provider, modelEndpointType } = m
|
||||
if (!provider) return false
|
||||
if (isEmpty(provider.apiKey?.trim())) return false
|
||||
|
||||
const isAnthropicType = provider.type === 'anthropic'
|
||||
const isAnthropicEndpoint = modelEndpointType === 'anthropic'
|
||||
const hasValidApiHost = !isEmpty(provider.anthropicApiHost?.trim())
|
||||
|
||||
return !(!isAnthropicType && !isAnthropicEndpoint && !hasValidApiHost)
|
||||
}
|
||||
|
||||
if (!modelInfo.provider || !validateModelInfo(modelInfo)) {
|
||||
logger.error('Anthropic provider configuration is missing', {
|
||||
modelInfo
|
||||
})
|
||||
|
||||
@ -100,7 +100,7 @@ export const AgentModal: React.FC<Props> = ({ agent, trigger, isOpen: _isOpen, o
|
||||
const { addAgent } = useAgents()
|
||||
const { updateAgent } = useUpdateAgent()
|
||||
// hard-coded. We only support anthropic for now.
|
||||
const { models } = useApiModels({ providerType: 'anthropic' })
|
||||
const { models } = useApiModels({ supportAnthropic: true })
|
||||
const isEditing = (agent?: AgentWithTools) => agent !== undefined
|
||||
|
||||
const [form, setForm] = useState<BaseAgentForm>(() => buildAgentForm(agent))
|
||||
|
||||
@ -6,6 +6,7 @@ import { ProviderTypeSchema } from './provider'
|
||||
// Request schema for /v1/models
|
||||
export const ApiModelsFilterSchema = z.object({
|
||||
providerType: ProviderTypeSchema.optional(),
|
||||
supportAnthropic: z.coerce.boolean().optional(),
|
||||
offset: z.coerce.number().min(0).default(0).optional(),
|
||||
limit: z.coerce.number().min(1).default(20).optional()
|
||||
})
|
||||
|
||||
@ -18,7 +18,7 @@ export const getModelFilterByAgentType = (type: AgentType): ApiModelsFilter => {
|
||||
switch (type) {
|
||||
case 'claude-code':
|
||||
return {
|
||||
providerType: 'anthropic'
|
||||
supportAnthropic: true
|
||||
}
|
||||
default:
|
||||
return {}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user