From 884153aeb88b69a4345e92212f944c4ac0f3ecdc Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 31 Dec 2025 15:23:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BA=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化模型ID传递逻辑,支持请求级别的模型覆盖;完善流式响应的错误处理 --- frontend/src/components/Input-field.vue | 7 ++++-- frontend/src/stores/menu.ts | 5 +++- frontend/src/views/chat/index.vue | 18 +++++++++++---- frontend/src/views/creatChat/creatChat.vue | 10 ++++---- .../chat_pipline/chat_completion_stream.go | 18 +++++++++++++++ internal/application/service/session.go | 23 ++++++++++++++----- internal/handler/session/qa.go | 1 + internal/models/chat/ollama.go | 3 ++- internal/models/chat/remote_api.go | 22 +++++++++++++----- internal/types/interfaces/session.go | 2 ++ 10 files changed, 83 insertions(+), 26 deletions(-) diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index c5ecccf1..583459aa 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -354,12 +354,14 @@ const loadConversationConfig = async () => { try { const response = await getConversationConfig(); conversationConfig.value = response.data; + const modelId = response.data?.summary_model_id || ''; settingsStore.updateConversationModels({ - summaryModelId: response.data?.summary_model_id || '', + summaryModelId: modelId, + selectedChatModelId: modelId, rerankModelId: response.data?.rerank_model_id || '', }); if (!selectedModelId.value) { - selectedModelId.value = response.data?.summary_model_id || ''; + selectedModelId.value = modelId; } ensureModelSelection(); } catch (error) { @@ -437,6 +439,7 @@ const handleModelChange = async (value: string | number | Array // 同步到 store settingsStore.updateConversationModels({ summaryModelId: val, + selectedChatModelId: val, rerankModelId: conversationConfig.value?.rerank_model_id || '', }); diff --git a/frontend/src/stores/menu.ts b/frontend/src/stores/menu.ts index 5f008c8e..2b165e6f 100644 --- a/frontend/src/stores/menu.ts +++ b/frontend/src/stores/menu.ts @@ -34,6 +34,7 @@ export const useMenuStore = defineStore('menuStore', () => { const isFirstSession = ref(false) const firstQuery = ref('') const firstMentionedItems = ref([]) + const firstModelId = ref('') const applyMenuTranslations = () => { menuArr.forEach(item => { @@ -92,9 +93,10 @@ export const useMenuStore = defineStore('menuStore', () => { isFirstSession.value = payload } - const changeFirstQuery = (payload: string, mentionedItems: any[] = []) => { + const changeFirstQuery = (payload: string, mentionedItems: any[] = [], modelId: string = '') => { firstQuery.value = payload firstMentionedItems.value = mentionedItems + firstModelId.value = modelId } return { @@ -102,6 +104,7 @@ export const useMenuStore = defineStore('menuStore', () => { isFirstSession, firstQuery, firstMentionedItems, + firstModelId, clearMenuArr, updatemenuArr, updataMenuChildren, diff --git a/frontend/src/views/chat/index.vue b/frontend/src/views/chat/index.vue index 88e60b41..ebe4bcd8 100644 --- a/frontend/src/views/chat/index.vue +++ b/frontend/src/views/chat/index.vue @@ -61,7 +61,7 @@ const useSettingsStoreInstance = useSettingsStore(); const uiStore = useUIStore(); const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation(); const { t } = useI18n(); -const { menuArr, isFirstSession, firstQuery, firstMentionedItems } = storeToRefs(usemenuStore); +const { menuArr, isFirstSession, firstQuery, firstMentionedItems, firstModelId } = storeToRefs(usemenuStore); const { output, onChunk, isStreaming, isLoading, error, startStream, stopStream } = useStream(); const route = useRoute(); const router = useRouter(); @@ -672,13 +672,21 @@ const handleAgentChunk = (data) => { // If this is an error response without tool data, handle it if (data.response_type === 'error' && !toolName) { - message.content = data.content || t('chat.processError'); + const errorMsg = data.content || t('chat.processError'); + message.content = errorMsg; isReplying.value = false; + loading.value = false; + MessagePlugin.error(errorMsg); + console.error('[Chat Error]', errorMsg); } } else if (data.response_type === 'error') { // Generic error without tool context - message.content = data.content || t('chat.processError'); + const errorMsg = data.content || t('chat.processError'); + message.content = errorMsg; isReplying.value = false; + loading.value = false; + MessagePlugin.error(errorMsg); + console.error('[Chat Error]', errorMsg); } break; @@ -793,8 +801,8 @@ onMounted(async () => { checkmenuTitle(session_id.value) if (firstQuery.value) { scrollLock.value = true; - sendMsg(firstQuery.value, '', firstMentionedItems.value || []); - usemenuStore.changeFirstQuery(''); + sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || []); + usemenuStore.changeFirstQuery('', [], ''); } else { scrollLock.value = false; let data = { diff --git a/frontend/src/views/creatChat/creatChat.vue b/frontend/src/views/creatChat/creatChat.vue index 4fa855ea..9b3de903 100644 --- a/frontend/src/views/creatChat/creatChat.vue +++ b/frontend/src/views/creatChat/creatChat.vue @@ -40,10 +40,10 @@ const { t } = useI18n(); const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation(); const sendMsg = (value: string, modelId: string, mentionedItems: any[]) => { - createNewSession(value, mentionedItems); + createNewSession(value, modelId, mentionedItems); } -async function createNewSession(value: string, mentionedItems: any[] = []) { +async function createNewSession(value: string, modelId: string, mentionedItems: any[] = []) { const selectedKbs = settingsStore.settings.selectedKnowledgeBases || []; const selectedFiles = settingsStore.settings.selectedFiles || []; @@ -63,7 +63,7 @@ async function createNewSession(value: string, mentionedItems: any[] = []) { try { const res = await createSessions(sessionData); if (res.data && res.data.id) { - await navigateToSession(res.data.id, value, mentionedItems); + await navigateToSession(res.data.id, value, modelId, mentionedItems); } else { console.error('[createChat] Failed to create session'); MessagePlugin.error(t('createChat.messages.createFailed')); @@ -74,7 +74,7 @@ async function createNewSession(value: string, mentionedItems: any[] = []) { } } -const navigateToSession = async (sessionId: string, value: string, mentionedItems: any[]) => { +const navigateToSession = async (sessionId: string, value: string, modelId: string, mentionedItems: any[]) => { const now = new Date().toISOString(); let obj = { title: t('createChat.newSessionTitle'), @@ -87,7 +87,7 @@ const navigateToSession = async (sessionId: string, value: string, mentionedItem }; usemenuStore.updataMenuChildren(obj); usemenuStore.changeIsFirstSession(true); - usemenuStore.changeFirstQuery(value, mentionedItems); + usemenuStore.changeFirstQuery(value, mentionedItems, modelId); router.push(`/platform/chat/${sessionId}`); } diff --git a/internal/application/service/chat_pipline/chat_completion_stream.go b/internal/application/service/chat_pipline/chat_completion_stream.go index 3c67cd3c..507415e1 100644 --- a/internal/application/service/chat_pipline/chat_completion_stream.go +++ b/internal/application/service/chat_pipline/chat_completion_stream.go @@ -104,6 +104,24 @@ func (p *PluginChatCompletionStream) OnEvent(ctx context.Context, var finalContent string for response := range responseChan { + // Handle error responses from the stream + if response.ResponseType == types.ResponseTypeError { + logger.Errorf(ctx, "Stream error received: %s", response.Content) + if err := eventBus.Emit(ctx, types.Event{ + ID: fmt.Sprintf("%s-error", uuid.New().String()[:8]), + Type: types.EventType(event.EventError), + SessionID: chatManage.SessionID, + Data: event.ErrorData{ + Error: response.Content, + Stage: "chat_completion_stream", + SessionID: chatManage.SessionID, + }, + }); err != nil { + logger.Errorf(ctx, "Failed to emit error event: %v", err) + } + continue + } + // Emit event for each answer chunk if response.ResponseType == types.ResponseTypeAnswer { finalContent += response.Content diff --git a/internal/application/service/session.go b/internal/application/service/session.go index 8da7e981..d83136c4 100644 --- a/internal/application/service/session.go +++ b/internal/application/service/session.go @@ -461,8 +461,9 @@ func (s *sessionService) KnowledgeQA( // Ensure defaults are set customAgent.EnsureDefaults() - // Override model ID - if customAgent.Config.ModelID != "" { + // Override model ID only if request didn't specify summaryModelID + // Request's summaryModelID has highest priority + if summaryModelID == "" && customAgent.Config.ModelID != "" { chatModelID = customAgent.Config.ModelID logger.Infof(ctx, "Using custom agent's model_id: %s", chatModelID) } @@ -1055,11 +1056,13 @@ func (s *sessionService) SearchKnowledge(ctx context.Context, // AgentQA performs agent-based question answering with conversation history and streaming support // customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults +// summaryModelID is optional - if provided, overrides the model from customAgent config func (s *sessionService) AgentQA( ctx context.Context, session *types.Session, query string, assistantMessageID string, + summaryModelID string, eventBus *event.EventBus, customAgent *types.CustomAgent, knowledgeBaseIDs []string, @@ -1163,15 +1166,23 @@ func (s *sessionService) AgentQA( agentConfig.SearchTargets = searchTargets logger.Infof(ctx, "Agent search targets built: %d targets", len(searchTargets)) - // Get summary model from custom agent config + // Get summary model: prioritize request's summaryModelID, then custom agent config // Note: tenantInfo.ConversationConfig is deprecated, all config comes from customAgent now - summaryModelID := customAgent.Config.ModelID - if summaryModelID == "" { + effectiveModelID := summaryModelID + if effectiveModelID == "" { + effectiveModelID = customAgent.Config.ModelID + } + if effectiveModelID == "" { logger.Warnf(ctx, "No summary model configured for custom agent %s", customAgent.ID) return errors.New("summary model (model_id) is not configured in custom agent settings") } + if summaryModelID != "" { + logger.Infof(ctx, "Using request's summary model override: %s", effectiveModelID) + } else { + logger.Infof(ctx, "Using custom agent's model_id: %s", effectiveModelID) + } - summaryModel, err := s.modelService.GetChatModel(ctx, summaryModelID) + summaryModel, err := s.modelService.GetChatModel(ctx, effectiveModelID) if err != nil { logger.Warnf(ctx, "Failed to get chat model: %v", err) return fmt.Errorf("failed to get chat model: %w", err) diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.go index 075bf7f1..cb2ff297 100644 --- a/internal/handler/session/qa.go +++ b/internal/handler/session/qa.go @@ -443,6 +443,7 @@ func (h *Handler) executeAgentModeQA(reqCtx *qaRequestContext) { reqCtx.session, reqCtx.query, reqCtx.assistantMessage.ID, + reqCtx.summaryModelID, streamCtx.eventBus, reqCtx.customAgent, reqCtx.knowledgeBaseIDs, diff --git a/internal/models/chat/ollama.go b/internal/models/chat/ollama.go index 647ef002..e6791b1b 100644 --- a/internal/models/chat/ollama.go +++ b/internal/models/chat/ollama.go @@ -189,7 +189,8 @@ func (c *OllamaChat) ChatStream( logger.GetLogger(ctx).Errorf("流式聊天请求失败: %v", err) // 发送错误响应 streamChan <- types.StreamResponse{ - ResponseType: types.ResponseTypeAnswer, + ResponseType: types.ResponseTypeError, + Content: err.Error(), Done: true, } } diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go index 99ad7068..4193f7b1 100644 --- a/internal/models/chat/remote_api.go +++ b/internal/models/chat/remote_api.go @@ -419,12 +419,22 @@ func (c *RemoteAPIChat) ChatStream(ctx context.Context, for { response, err := stream.Recv() if err != nil { - // 发送最后一个响应,包含收集到的 tool calls - streamChan <- types.StreamResponse{ - ResponseType: types.ResponseTypeAnswer, - Content: "", - Done: true, - ToolCalls: buildOrderedToolCalls(), + // Check if it's a normal end of stream (io.EOF) + if err.Error() == "EOF" { + // Normal end of stream, send final response with collected tool calls + streamChan <- types.StreamResponse{ + ResponseType: types.ResponseTypeAnswer, + Content: "", + Done: true, + ToolCalls: buildOrderedToolCalls(), + } + } else { + // Actual error, send error response + streamChan <- types.StreamResponse{ + ResponseType: types.ResponseTypeError, + Content: err.Error(), + Done: true, + } } return } diff --git a/internal/types/interfaces/session.go b/internal/types/interfaces/session.go index 1c326f3a..30a7fbe8 100644 --- a/internal/types/interfaces/session.go +++ b/internal/types/interfaces/session.go @@ -49,11 +49,13 @@ type SessionService interface { // AgentQA performs agent-based question answering with conversation history and streaming support // eventBus is optional - if nil, uses service's default EventBus // customAgent is optional - if provided, uses custom agent configuration instead of tenant defaults + // summaryModelID is optional - if provided, overrides the model from customAgent config AgentQA( ctx context.Context, session *types.Session, query string, assistantMessageID string, + summaryModelID string, eventBus *event.EventBus, customAgent *types.CustomAgent, knowledgeBaseIDs []string,