refactor: extract model selection logic from delegate-task into focused modules
- Create available-models.ts for model availability checking - Create model-selection.ts for category-to-model resolution logic - Update category-resolver, subagent-resolver, and sync modules to import from new focused modules instead of monolithic sources
This commit is contained in:
67
src/tools/delegate-task/model-selection.ts
Normal file
67
src/tools/delegate-task/model-selection.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { FallbackEntry } from "../../shared/model-requirements"
|
||||
import { fuzzyMatchModel } from "../../shared/model-availability"
|
||||
|
||||
function normalizeModel(model?: string): string | undefined {
|
||||
const trimmed = model?.trim()
|
||||
return trimmed || undefined
|
||||
}
|
||||
|
||||
export function resolveModelForDelegateTask(input: {
|
||||
userModel?: string
|
||||
categoryDefaultModel?: string
|
||||
fallbackChain?: FallbackEntry[]
|
||||
availableModels: Set<string>
|
||||
systemDefaultModel?: string
|
||||
}): { model: string; variant?: string } | undefined {
|
||||
const userModel = normalizeModel(input.userModel)
|
||||
if (userModel) {
|
||||
return { model: userModel }
|
||||
}
|
||||
|
||||
const categoryDefault = normalizeModel(input.categoryDefaultModel)
|
||||
if (categoryDefault) {
|
||||
if (input.availableModels.size === 0) {
|
||||
return { model: categoryDefault }
|
||||
}
|
||||
|
||||
const parts = categoryDefault.split("/")
|
||||
const providerHint = parts.length >= 2 ? [parts[0]] : undefined
|
||||
const match = fuzzyMatchModel(categoryDefault, input.availableModels, providerHint)
|
||||
if (match) {
|
||||
return { model: match }
|
||||
}
|
||||
}
|
||||
|
||||
const fallbackChain = input.fallbackChain
|
||||
if (fallbackChain && fallbackChain.length > 0) {
|
||||
if (input.availableModels.size === 0) {
|
||||
const first = fallbackChain[0]
|
||||
const provider = first?.providers?.[0]
|
||||
if (provider) {
|
||||
return { model: `${provider}/${first.model}`, variant: first.variant }
|
||||
}
|
||||
} else {
|
||||
for (const entry of fallbackChain) {
|
||||
for (const provider of entry.providers) {
|
||||
const fullModel = `${provider}/${entry.model}`
|
||||
const match = fuzzyMatchModel(fullModel, input.availableModels, [provider])
|
||||
if (match) {
|
||||
return { model: match, variant: entry.variant }
|
||||
}
|
||||
}
|
||||
|
||||
const crossProviderMatch = fuzzyMatchModel(entry.model, input.availableModels)
|
||||
if (crossProviderMatch) {
|
||||
return { model: crossProviderMatch, variant: entry.variant }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const systemDefaultModel = normalizeModel(input.systemDefaultModel)
|
||||
if (systemDefaultModel) {
|
||||
return { model: systemDefaultModel }
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
Reference in New Issue
Block a user