合并代码,解决冲突
(cherry picked from commit 8b23bd771f04ceb1b9c1e3ae021d75001af5ba2f)
This commit is contained in:
1 parent
23badf7456
commit
93e99793d8
10 files changed
+626
-84
No files matched your search
@@ -32,6 +32,18 @@ interface ChatMessage {
|
||||
rated?: boolean;
|
||||
}
|
||||
|
||||
type RunStatus = 'running' | 'done' | 'error';
|
||||
|
||||
interface ChatRun {
|
||||
runId: number;
|
||||
viewId: number;
|
||||
query: string;
|
||||
messages: ChatMessage[];
|
||||
conversationId: string | null;
|
||||
status: RunStatus;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
// 统一资源格式为数组,兼容 list 或 { data: list } 两种结构
|
||||
const normalizeResources = (resources: any): RetrieverResource[] => {
|
||||
if (!resources) return [];
|
||||
@@ -125,12 +137,35 @@ export function useChat() {
|
||||
*/
|
||||
const isThinking = ref(false);
|
||||
|
||||
const viewId = ref(0);
|
||||
|
||||
const runs = ref<ChatRun[]>([]);
|
||||
const activeRunId = ref<number | null>(null);
|
||||
let runSeq = 0;
|
||||
|
||||
/**
|
||||
* conversationId: 当前会话 ID
|
||||
* 用于在多轮对话中关联上下文,并在反馈时找到对应 ChatLog
|
||||
*/
|
||||
const conversationId = ref<string | null>(null);
|
||||
|
||||
const activateRun = (runId: number) => {
|
||||
const run = runs.value.find((r) => r.runId === runId);
|
||||
if (!run) return;
|
||||
messages.value = run.messages;
|
||||
conversationId.value = run.conversationId;
|
||||
isThinking.value = run.status === 'running';
|
||||
activeRunId.value = runId;
|
||||
};
|
||||
|
||||
const createNewChatView = () => {
|
||||
viewId.value += 1;
|
||||
messages.value = [];
|
||||
conversationId.value = null;
|
||||
isThinking.value = false;
|
||||
activeRunId.value = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* sendMessage
|
||||
* 用途:发送消息并处理 SSE 流式响应
|
||||
@@ -145,6 +180,20 @@ export function useChat() {
|
||||
* agentId: 目标 Agent ID
|
||||
*/
|
||||
const sendMessage = async (query: string, agentId: number, kbType: string = '企业知识库') => {
|
||||
const runViewId = viewId.value;
|
||||
const runMessages = messages.value;
|
||||
const run: ChatRun = {
|
||||
runId: ++runSeq,
|
||||
viewId: runViewId,
|
||||
query,
|
||||
messages: runMessages,
|
||||
conversationId: conversationId.value,
|
||||
status: 'running',
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
runs.value.unshift(run);
|
||||
if (runs.value.length > 30) runs.value = runs.value.slice(0, 30);
|
||||
activeRunId.value = run.runId;
|
||||
console.info('[chat][send] start', {
|
||||
agentId,
|
||||
kbType,
|
||||
@@ -152,19 +201,19 @@ export function useChat() {
|
||||
conversationId: conversationId.value,
|
||||
});
|
||||
// 1. 乐观 UI 更新:立即将用户消息推入列表
|
||||
messages.value.push({ role: 'user', content: query });
|
||||
runMessages.push({ role: 'user', content: query });
|
||||
// Lazily create assistant message only when answer/error arrives.
|
||||
let botMsgIndex: number | null = null;
|
||||
const ensureBotMessage = () => {
|
||||
if (botMsgIndex === null) {
|
||||
botMsgIndex = messages.value.push({
|
||||
botMsgIndex = runMessages.push({
|
||||
role: 'assistant',
|
||||
content: '',
|
||||
retriever_resources: [],
|
||||
attribution_parts: []
|
||||
}) - 1;
|
||||
}
|
||||
return messages.value[botMsgIndex] || null;
|
||||
return runMessages[botMsgIndex] || null;
|
||||
};
|
||||
|
||||
isThinking.value = true;
|
||||
@@ -234,7 +283,14 @@ export function useChat() {
|
||||
conversationId: data?.conversation_id || conversationId.value,
|
||||
});
|
||||
|
||||
if (data.event === 'message') {
|
||||
if (data?.conversation_id) {
|
||||
const cid = String(data.conversation_id);
|
||||
if (!run.conversationId) run.conversationId = cid;
|
||||
if (messages.value === runMessages) conversationId.value = cid;
|
||||
}
|
||||
|
||||
const eventName = String(data.event || '');
|
||||
if (eventName === 'message' || eventName === 'agent_message') {
|
||||
if (typeof data.answer === 'string' && data.answer.length > 0) {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
@@ -246,21 +302,20 @@ export function useChat() {
|
||||
});
|
||||
}
|
||||
if (botMsgIndex !== null) {
|
||||
const currentBotMsg = messages.value[botMsgIndex];
|
||||
const currentBotMsg = runMessages[botMsgIndex];
|
||||
if (currentBotMsg) {
|
||||
applyEventMetadata(currentBotMsg, data);
|
||||
}
|
||||
}
|
||||
} else if (data.event === 'message_end') {
|
||||
} else if (eventName === 'message_end' || eventName === 'workflow_finished') {
|
||||
run.status = 'done';
|
||||
if (messages.value === runMessages) isThinking.value = false;
|
||||
if (botMsgIndex !== null && Number.isInteger(data.chat_log_id)) {
|
||||
const currentBotMsg = messages.value[botMsgIndex];
|
||||
const currentBotMsg = runMessages[botMsgIndex];
|
||||
if (currentBotMsg) {
|
||||
currentBotMsg.chat_log_id = Number(data.chat_log_id);
|
||||
}
|
||||
}
|
||||
if (data.conversation_id) {
|
||||
conversationId.value = data.conversation_id;
|
||||
}
|
||||
if (typeof data.answer === 'string' && data.answer.length > 0) {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
@@ -278,16 +333,22 @@ export function useChat() {
|
||||
});
|
||||
}
|
||||
if (botMsgIndex !== null) {
|
||||
const currentBotMsg = messages.value[botMsgIndex];
|
||||
const currentBotMsg = runMessages[botMsgIndex];
|
||||
if (currentBotMsg) {
|
||||
applyEventMetadata(currentBotMsg, data);
|
||||
}
|
||||
}
|
||||
} else if (data.event === 'error') {
|
||||
} else if (eventName === 'error') {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
botMsg.content += `\n[Error: ${data.message}]`;
|
||||
console.error('[chat][sse] backend error event', data);
|
||||
run.status = 'error';
|
||||
if (messages.value === runMessages) isThinking.value = false;
|
||||
} else if (typeof data.answer === 'string' && data.answer.length > 0) {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
botMsg.content += normalizeStreamText(data.answer);
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore malformed or partial payload fragments.
|
||||
@@ -297,16 +358,24 @@ export function useChat() {
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error('[chat][send] failed', e);
|
||||
run.status = 'error';
|
||||
const botMsg = ensureBotMessage();
|
||||
if (botMsg) {
|
||||
botMsg.content += `\n[Error: ${e.message || 'Network Issue'}]`;
|
||||
}
|
||||
} finally {
|
||||
isThinking.value = false;
|
||||
console.info('[chat][send] end', {
|
||||
messages: messages.value.length,
|
||||
conversationId: conversationId.value,
|
||||
});
|
||||
if (messages.value === runMessages) {
|
||||
isThinking.value = false;
|
||||
console.info('[chat][send] end', {
|
||||
messages: messages.value.length,
|
||||
conversationId: conversationId.value,
|
||||
});
|
||||
} else {
|
||||
console.info('[chat][send] end (detached)', {
|
||||
activeConversationId: conversationId.value,
|
||||
runViewId,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -315,5 +384,5 @@ export function useChat() {
|
||||
return text.replace(/\\n/g, '\n');
|
||||
};
|
||||
|
||||
return { messages, isThinking, conversationId, sendMessage };
|
||||
return { messages, isThinking, conversationId, runs, activeRunId, activateRun, createNewChatView, sendMessage };
|
||||
}
|
||||
Reference in new issue
Block a user