feat(agent): prefill default rerank model on agent creation

Prefill rerank_model_id in the agent create dialog with the same
is_default-first, first-active fallback as chat

Extract the selection into frontend/src/utils/modelDefaults.ts and reuse
it in the KB dialog.
main
MidoriKurage 2026-08-10 10:55:36 +08:00 committed by lyingbug
parent 6d833abfc0
commit 1431e9414e
3 changed files with 39 additions and 18 deletions

View File

@ -0,0 +1,22 @@
export interface ModelDefaultCandidate {
id?: string
type: string
status?: string
is_default?: boolean
}
/**
* Pick a creation-time model: prefer the declared default, fall back to the
* first active model.
*/
export function selectInitialModelId(
models: readonly ModelDefaultCandidate[],
modelType: string,
): string | null {
const active = models.filter(
model => Boolean(model.id?.trim())
&& model.type === modelType
&& (!model.status || model.status === 'active'),
)
return active.find(model => model.is_default)?.id?.trim() ?? active[0]?.id?.trim() ?? null
}

View File

@ -1648,6 +1648,7 @@ import {
markContextualGuideDone,
} from '@/config/contextualGuides';
import { useI18n } from 'vue-i18n';
import { selectInitialModelId } from '@/utils/modelDefaults';
import { MessagePlugin } from 'tdesign-vue-next';
import {
createAgent,
@ -2417,13 +2418,15 @@ const removeStarterSuggestion = (index: number) => {
formData.value.config.question_suggestions.starters.items.splice(index, 1);
};
const applyDefaultChatModelIfEmpty = () => {
const applyDefaultModelsIfEmpty = () => {
if (props.mode !== 'create' || !formData.value) return
const chat =
allModels.value.find((m) => m.type === 'KnowledgeQA' && m.is_default)
|| allModels.value.find((m) => m.type === 'KnowledgeQA')
if (!formData.value.config.model_id && chat?.id) {
formData.value.config.model_id = chat.id
const chatModelId = selectInitialModelId(allModels.value, 'KnowledgeQA')
const rerankModelId = selectInitialModelId(allModels.value, 'Rerank')
if (!formData.value.config.model_id && chatModelId) {
formData.value.config.model_id = chatModelId
}
if (!formData.value.config.rerank_model_id && rerankModelId) {
formData.value.config.rerank_model_id = rerankModelId
}
}
@ -3043,7 +3046,7 @@ watch(() => props.visible, async (val) => {
formData.value.description = getPresetDefaultDescription(preset);
}
}
applyDefaultChatModelIfEmpty()
applyDefaultModelsIfEmpty()
}
if (props.initialHighlightField) {

View File

@ -468,8 +468,8 @@ import { KB_EDITOR_FOCUS_SECTION_EVENT, markContextualGuideDone } from '@/config
import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
import { createKnowledgeBase, getKnowledgeBaseById, listKnowledgeFiles, updateKnowledgeBase, rebuildKBIndex } from '@/api/knowledge-base'
import { updateKBConfig, type KBModelConfigRequest } from '@/api/initialization'
import { type ModelConfig } from '@/api/model'
import { useChatResourcesStore } from '@/stores/chatResources'
import { selectInitialModelId } from '@/utils/modelDefaults'
import { useEditorResourcesStore } from '@/stores/editorResources'
import { useUIStore } from '@/stores/ui'
import { useAuthStore } from '@/stores/auth'
@ -699,17 +699,13 @@ const kbCreateNeedsEmbedding = computed(() => {
const applyDefaultModelsIfEmpty = () => {
if (!formData.value || editorMode.value !== 'create') return
const pick = (type: ModelConfig['type']) => {
const list = allModels.value.filter((m) => m.type === type)
return list.find((m) => m.is_default) || list[0]
const chatModelId = selectInitialModelId(allModels.value, 'KnowledgeQA')
const embeddingModelId = selectInitialModelId(allModels.value, 'Embedding')
if (!formData.value.modelConfig.llmModelId && chatModelId) {
formData.value.modelConfig.llmModelId = chatModelId
}
const chat = pick('KnowledgeQA')
const embedding = pick('Embedding')
if (!formData.value.modelConfig.llmModelId && chat?.id) {
formData.value.modelConfig.llmModelId = chat.id
}
if (!formData.value.modelConfig.embeddingModelId && embedding?.id) {
formData.value.modelConfig.embeddingModelId = embedding.id
if (!formData.value.modelConfig.embeddingModelId && embeddingModelId) {
formData.value.modelConfig.embeddingModelId = embeddingModelId
}
}