Merge branch 'dev' into hugo

This commit is contained in:
hugo committed 2026-06-23 11:22:39 +08:00
commit d5928b42f3
63 files changed
+5759 -1493

No files matched your search

@@ -9,7 +9,6 @@ import {
Check,
ChevronDown,
Database,
Globe,
Link2,
Loader2,
Mic,
@@ -44,12 +43,10 @@ const props = defineProps<{
/** 用对象包裹 Ref,避免 template 自动解包 */
shippingPickerRefHolder: { pickerRef: Ref<HTMLInputElement | null> };
getKbLabel: (kb: string) => string;
browserPanelOpen: boolean;
}>();
const emit = defineEmits<{
send: [];
'toggle-browser': [];
'voice-click': [];
'remove-attachment': [id: string];
'local-files-selected': [event: Event];
@@ -312,16 +309,6 @@ onBeforeUnmount(() => {
</button>
</div>
</div>
<button
type="button"
:disabled="isThinking"
class="flex items-center gap-1 rounded-lg border border-default px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-50"
@click="emit('toggle-browser')"
>
<Globe class="h-3.5 w-3.5 text-brand-500" />
{{ browserPanelOpen ? $t('chat.collapseBrowser') : $t('chat.openBrowser') }}
</button>
</div>
<button
@@ -141,8 +141,6 @@ const page = useChatPageContext();
:shipping-suggested-prompt="page.shippingSuggestedPrompt"
:shipping-picker-ref-holder="page.shippingPickerRefHolder"
:get-kb-label="page.getKbLabel"
:browser-panel-open="page.browserPanelOpen"
@toggle-browser="page.toggleBrowserPanel"
@send="page.handleSend()"
@voice-click="page.handleVoiceFeatureClick"
@remove-attachment="page.removeAttachment"
@@ -3,7 +3,9 @@
* SSE 流式对话核心:消息列表、runs、发送与接收。
*/
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { getAuthHeaders } from '@/shared/utils/auth';
import { applyStreamErrorToMessage, type StreamErrorLabels } from '../utils/streamErrorMessage';
import type { ChatMessage, ChatRun, RetrieverResource } from '../types/chat.types';
const normalizeResources = (resources: unknown) => {
@@ -114,6 +116,7 @@ const applyEventMetadata = (msg: ChatMessage, data: Record<string, unknown>) =>
};
export function useChat() {
const { t } = useI18n();
const messages = ref<ChatMessage[]>([]);
const isThinking = ref(false);
const viewId = ref(0);
@@ -173,6 +176,13 @@ export function useChat() {
return String(message ?? 'Unknown error');
};
const streamErrorLabels = (): StreamErrorLabels => ({
networkDisconnectedEmpty: t('chat.streamError.networkDisconnectedEmpty'),
partialInterrupted: t('chat.streamError.partialInterrupted'),
upstreamDisconnected: t('chat.streamError.upstreamDisconnected'),
generic: t('chat.streamError.generic'),
});
const sendMessage = async (
query: string,
agentId: number,
@@ -320,7 +330,7 @@ export function useChat() {
} else if (eventName === 'error') {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += `\n[Error: ${formatStreamError(data.message)}]`;
applyStreamErrorToMessage(botMsg, formatStreamError(data.message), streamErrorLabels());
run.status = 'error';
if (messages.value === runMessages) isThinking.value = false;
} else if (typeof data.answer === 'string' && data.answer.length > 0) {
@@ -342,7 +352,7 @@ export function useChat() {
const botMsg = ensureBotMessage();
if (botMsg) {
const message = e instanceof Error ? e.message : 'Network Issue';
botMsg.content += `\n[Error: ${message}]`;
applyStreamErrorToMessage(botMsg, message, streamErrorLabels());
}
} finally {
if (messages.value === runMessages) {
@@ -1,4 +1,4 @@
/**
/**
* features/chat/composables/useChatAgent.ts
* 当前 Agent 元数据、知识库选项、推荐问题与用户可切换 Agent 列表。
*/
@@ -96,8 +96,9 @@ export function useChatAgent(options: UseChatAgentOptions) {
const kbDropdownDisabled = computed(() => availableKbOptions.value.length <= 1);
watch(canSelectErpKnowledgeBase, (allowed) => {
// 固资管理默认先走企业知识库,用户可手动切换到 ERP 知识库。
if (allowed) {
selectedKb.value = ERP_KB;
selectedKb.value = DEFAULT_KB;
return;
}
if (selectedKb.value === ERP_KB) {
@@ -35,6 +35,8 @@ export interface ChatMessage {
variant?: 'primary' | 'secondary' | 'danger';
}>;
shipping_actions_used?: boolean;
/** 流式对话是否因网络/上游中断而附带错误提示 */
stream_error?: boolean;
}
export type ChatRunStatus = 'running' | 'done' | 'error';
@@ -0,0 +1,88 @@
/**
* features/chat/utils/streamErrorMessage.ts
* 用途:将 SSE / 浏览器流式读错映射为用户可读文案。
* 依赖:vue-i18n chat.streamError 词条。
* 逻辑:useChat 在 catch 与 error 事件中调用 applyStreamErrorToMessage。
*/
import type { ChatMessage } from '../types/chat.types';
const NETWORK_DISCONNECT_PATTERNS = [
'network error',
'failed to fetch',
'load failed',
'networkerror when attempting to fetch resource',
'network issue',
];
const UPSTREAM_DISCONNECT_PATTERNS = [
'response ended prematurely',
'chunkedencodingerror',
];
export interface StreamErrorLabels {
networkDisconnectedEmpty: string;
partialInterrupted: string;
upstreamDisconnected: string;
generic: string;
}
/**
* 判断是否为浏览器侧 SSE / fetch 连接中断类错误。
*/
export function isNetworkDisconnectError(message: string): boolean {
const normalized = message.trim().toLowerCase();
if (!normalized) return false;
return NETWORK_DISCONNECT_PATTERNS.some(
(pattern) => normalized === pattern || normalized.includes(pattern),
);
}
/**
* 判断是否为上游(Dify)流式响应提前结束。
*/
export function isUpstreamDisconnectError(message: string): boolean {
const normalized = message.trim().toLowerCase();
return UPSTREAM_DISCONNECT_PATTERNS.some((pattern) => normalized.includes(pattern));
}
/**
* 将原始错误信息映射为面向用户的提示文案。
*
* @param rawMessage - 浏览器或后端返回的原始错误
* @param hasContent - 是否已有部分回答内容
* @param labels - i18n 文案集合
*/
export function resolveStreamErrorText(
rawMessage: string,
hasContent: boolean,
labels: StreamErrorLabels,
): string {
if (isNetworkDisconnectError(rawMessage)) {
return hasContent ? labels.partialInterrupted : labels.networkDisconnectedEmpty;
}
if (isUpstreamDisconnectError(rawMessage)) {
return hasContent ? labels.partialInterrupted : labels.upstreamDisconnected;
}
if (/[\u4e00-\u9fff]/.test(rawMessage)) {
return rawMessage;
}
return labels.generic;
}
/**
* 把流式错误写入 assistant 消息:无内容时整段替换,有部分内容时在末尾追加提示。
*/
export function applyStreamErrorToMessage(
msg: ChatMessage,
rawMessage: string,
labels: StreamErrorLabels,
): void {
const hasContent = Boolean(msg.content.trim());
const text = resolveStreamErrorText(rawMessage, hasContent, labels);
msg.stream_error = true;
if (hasContent) {
msg.content += `\n\n${text}`;
} else {
msg.content = text;
}
}
@@ -41,6 +41,7 @@ export function resolveMessageAnchorKey(msg: ChatMessage, index: number): string
}
export function assistantMessageHasError(msg: ChatMessage | null | undefined): boolean {
if (msg?.stream_error) return true;
return String(msg?.content || '').includes('[Error:');
}