feat: 对话式首页接口对接,逻辑调整

This commit is contained in:
rd
2025-08-26 17:59:42 +08:00
parent 6211c78c07
commit a125f6f092
12 changed files with 320 additions and 178 deletions

View File

@ -6,12 +6,11 @@ import { Typography } from 'ant-design-vue';
import RightView from './components/right-view/index.vue';
import { useRoute } from 'vue-router';
import { genRandomId } from '@/utils/tools';
import { useChatStore } from '@/stores/modules/chat';
import querySSE from '@/utils/querySSE';
import useChatHandler from './useChatHandler';
import { QUESTION_ROLE, ANSWER_ROLE } from './constants';
import { QUESTION_ROLE, LOADING_ROLE } from './constants';
export default {
props: {
@ -25,12 +24,9 @@ export default {
const route = useRoute();
const senderRef = ref(null);
const rightViewRef = ref(null);
const bubbleListRef = ref<any>(null);
const { roles, showRightView, rightViewContent, currentTaskId, handleMessage, conversationList, generateLoading } =
useChatHandler();
const sseController = ref<any>(null);
const conversationId = computed(() => {
return route.params.conversationId;
@ -41,42 +37,49 @@ export default {
antdMessage.warning('停止生成后可发送');
return;
}
conversationList.value.push({
role: QUESTION_ROLE,
content: message,
});
initSse({ message });
};
const handleCancel = () => {
generateLoading.value = false;
// 中止当前正在输出的回答
if (currentTaskId.value && bubbleListRef.value?.abortTypingByKey) {
bubbleListRef.value.abortTypingByKey(currentTaskId.value);
if (generateLoading.value) {
bubbleListRef.value?.abortTypingByKey(currentTaskId.value);
sseController.value?.abort?.();
}
if (showRightView.value) {
rightViewRef.value?.abortTyping?.();
}
generateLoading.value = false;
antdMessage.info('取消生成');
};
const initSse = (inputInfo: CHAT.TInputInfo) => {
const initSse = (inputInfo: CHAT.TInputInfo): void => {
if (sseController.value) {
sseController.value.abort?.();
sseController.value = null;
}
try {
const { message } = inputInfo;
generateLoading.value = true;
conversationList.value.push({
role: QUESTION_ROLE,
content: message,
sseController.value = querySSE({
method: 'POST',
handleMessage,
handleOpen,
body: JSON.stringify({
content: message,
session_id: conversationId.value,
agent_id: chatStore.agentInfo.agent_id,
}),
});
const taskId = genRandomId();
currentTaskId.value = taskId;
const url = `http://localhost:3000/agent/input?content=${message}&session_id=${conversationId.value}&agent_id=${chatStore.agentInfo?.agent_id}`;
querySSE(
{
handleMessage,
},
url,
);
} catch (error) {
console.error('Failed to initialize SSE:', error);
antdMessage.error('初始化连接失败');
@ -84,10 +87,30 @@ export default {
}
};
const {
roles,
showRightView,
rightViewInfo,
currentTaskId,
handleMessage,
handleOpen,
conversationList,
generateLoading,
senderRef,
} = useChatHandler({
initSse,
});
watch(
() => props.inputInfo,
(newVal) => {
newVal && initSse(newVal);
if (newVal) {
conversationList.value.push({
role: QUESTION_ROLE,
content: newVal,
});
initSse(newVal);
}
},
{ deep: true },
);
@ -101,14 +124,14 @@ export default {
roles={roles}
items={[
...conversationList.value,
generateLoading.value ? { loading: true, role: ANSWER_ROLE } : null,
generateLoading.value ? { loading: true, role: LOADING_ROLE } : null,
].filter(Boolean)}
/>
</div>
<div class="w-full flex flex-col justify-center items-center">
<SenderInput
class="w-600px"
ref={senderRef}
class="w-600px"
placeholder="继续追问..."
loading={generateLoading.value}
onSubmit={handleSubmit}
@ -122,7 +145,7 @@ export default {
{showRightView.value && (
<RightView
ref={rightViewRef}
rightViewContent={rightViewContent.value}
rightViewInfo={rightViewInfo.value}
showRightView={showRightView.value}
onClose={() => (showRightView.value = false)}
/>