mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 14:41:24 +08:00
feat(model): enhance parseModelId to handle identifiers without provider prefix and improve edge case handling
This commit is contained in:
parent
4173fcbb98
commit
eb57f50cfe
@ -118,13 +118,42 @@ describe('model', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should handle model identifiers without provider prefix', () => {
|
||||||
|
expect(parseModelId('claude-3-sonnet')).toEqual({
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: 'claude-3-sonnet'
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(parseModelId('gpt-4')).toEqual({
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: 'gpt-4'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should return undefined for invalid inputs', () => {
|
it('should return undefined for invalid inputs', () => {
|
||||||
expect(parseModelId(undefined)).toBeUndefined()
|
expect(parseModelId(undefined)).toBeUndefined()
|
||||||
expect(parseModelId('')).toBeUndefined()
|
expect(parseModelId('')).toBeUndefined()
|
||||||
expect(parseModelId('no-colon')).toBeUndefined()
|
expect(parseModelId(' ')).toBeUndefined()
|
||||||
expect(parseModelId(':missing-provider')).toBeUndefined()
|
})
|
||||||
expect(parseModelId('missing-model:')).toBeUndefined()
|
|
||||||
expect(parseModelId(':')).toBeUndefined()
|
it('should handle edge cases with colons', () => {
|
||||||
|
// Colon at start - treat as modelId without provider
|
||||||
|
expect(parseModelId(':missing-provider')).toEqual({
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: ':missing-provider'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Colon at end - treat everything before as modelId
|
||||||
|
expect(parseModelId('missing-model:')).toEqual({
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: 'missing-model'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Only colon - treat as modelId without provider
|
||||||
|
expect(parseModelId(':')).toEqual({
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: ':'
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle edge cases', () => {
|
it('should handle edge cases', () => {
|
||||||
|
|||||||
@ -87,7 +87,7 @@ export const apiModelAdapter = (model: ApiModel): AdaptedApiModel => {
|
|||||||
* where modelId may contain additional colons (e.g., "openrouter:anthropic/claude-3.5-sonnet:free")
|
* where modelId may contain additional colons (e.g., "openrouter:anthropic/claude-3.5-sonnet:free")
|
||||||
*
|
*
|
||||||
* @param modelIdentifier - The full model identifier string
|
* @param modelIdentifier - The full model identifier string
|
||||||
* @returns Object with providerId and modelId, or undefined if invalid
|
* @returns Object with providerId and modelId. If no provider prefix found, providerId will be undefined
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* parseModelId("openrouter:anthropic/claude-3.5-sonnet:free")
|
* parseModelId("openrouter:anthropic/claude-3.5-sonnet:free")
|
||||||
@ -98,20 +98,38 @@ export const apiModelAdapter = (model: ApiModel): AdaptedApiModel => {
|
|||||||
* // => { providerId: "anthropic", modelId: "claude-3-sonnet" }
|
* // => { providerId: "anthropic", modelId: "claude-3-sonnet" }
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* parseModelId("invalid") // => undefined
|
* parseModelId("claude-3-sonnet")
|
||||||
|
* // => { providerId: undefined, modelId: "claude-3-sonnet" }
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* parseModelId("") // => undefined
|
||||||
*/
|
*/
|
||||||
export function parseModelId(modelIdentifier: string | undefined): { providerId: string; modelId: string } | undefined {
|
export function parseModelId(
|
||||||
if (!modelIdentifier || typeof modelIdentifier !== 'string') {
|
modelIdentifier: string | undefined
|
||||||
|
): { providerId: string | undefined; modelId: string } | undefined {
|
||||||
|
if (!modelIdentifier || typeof modelIdentifier !== 'string' || modelIdentifier.trim() === '') {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const colonIndex = modelIdentifier.indexOf(':')
|
const colonIndex = modelIdentifier.indexOf(':')
|
||||||
|
|
||||||
// Must contain at least one colon and have content on both sides
|
// No colon found or colon at the start - treat entire string as modelId
|
||||||
if (colonIndex <= 0 || colonIndex >= modelIdentifier.length - 1) {
|
if (colonIndex <= 0) {
|
||||||
return undefined
|
return {
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: modelIdentifier
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Colon at the end - treat everything before as modelId
|
||||||
|
if (colonIndex >= modelIdentifier.length - 1) {
|
||||||
|
return {
|
||||||
|
providerId: undefined,
|
||||||
|
modelId: modelIdentifier.substring(0, colonIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Standard format: "provider:modelId"
|
||||||
return {
|
return {
|
||||||
providerId: modelIdentifier.substring(0, colonIndex),
|
providerId: modelIdentifier.substring(0, colonIndex),
|
||||||
modelId: modelIdentifier.substring(colonIndex + 1)
|
modelId: modelIdentifier.substring(colonIndex + 1)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user