Files
lingji-work-fe/src/components/xt-chat/chat-view/index.vue

162 lines
4.3 KiB
Vue
Raw Normal View History

2025-08-25 18:01:04 +08:00
<script lang="tsx">
import { message as antdMessage, Tooltip } from 'ant-design-vue';
import { BubbleList } from '@/components/xt-chat/xt-bubble';
import SenderInput from './components/sender-input/index.vue';
import RightView from './components/right-view/index.vue';
import { useRoute } from 'vue-router';
import { useChatStore } from '@/stores/modules/chat';
import querySSE from '@/utils/querySSE';
import useChatHandler from './useChatHandler';
import { QUESTION_ROLE, LOADING_ROLE } from './constants';
2025-08-25 18:01:04 +08:00
export default {
props: {
inputInfo: {
type: Object as () => CHAT.TInputInfo,
default: null,
},
},
setup(props, { emit, expose }) {
const chatStore = useChatStore();
const route = useRoute();
const rightViewRef = ref(null);
const bubbleListRef = ref<any>(null);
const sseController = ref<any>(null);
2025-08-25 18:01:04 +08:00
const conversationId = computed(() => {
return route.params.conversationId;
});
const handleSubmit = (message: string) => {
if (generateLoading.value) {
antdMessage.warning('停止生成后可发送');
return;
}
2025-08-27 12:04:23 +08:00
conversationList.value.push({
role: QUESTION_ROLE,
content: message,
});
2025-08-25 18:01:04 +08:00
initSse({ message });
};
const handleCancel = () => {
// 中止当前正在输出的回答
2025-08-27 12:04:23 +08:00
console.log('handleCancel', currentTaskId.value)
if (generateLoading.value) {
bubbleListRef.value?.abortTypingByKey(currentTaskId.value);
sseController.value?.abort?.();
2025-08-25 18:01:04 +08:00
}
if (showRightView.value) {
rightViewRef.value?.abortTyping?.();
}
generateLoading.value = false;
2025-08-25 18:01:04 +08:00
antdMessage.info('取消生成');
};
const initSse = (inputInfo: CHAT.TInputInfo): void => {
if (sseController.value) {
sseController.value.abort?.();
sseController.value = null;
}
2025-08-25 18:01:04 +08:00
try {
const { message } = inputInfo;
generateLoading.value = true;
sseController.value = querySSE({
method: 'POST',
handleMessage,
body: JSON.stringify({
content: message,
session_id: conversationId.value,
agent_id: chatStore.agentInfo.agent_id,
}),
});
2025-08-25 18:01:04 +08:00
} catch (error) {
console.error('Failed to initialize SSE:', error);
antdMessage.error('初始化连接失败');
generateLoading.value = false;
}
};
const {
roles,
showRightView,
rightViewInfo,
currentTaskId,
handleMessage,
conversationList,
generateLoading,
senderRef,
} = useChatHandler({
initSse,
});
2025-08-25 18:01:04 +08:00
watch(
() => props.inputInfo,
(newVal) => {
if (newVal) {
2025-08-27 12:04:23 +08:00
const { message } = newVal;
conversationList.value.push({
role: QUESTION_ROLE,
2025-08-27 12:04:23 +08:00
content: message,
});
initSse(newVal);
}
2025-08-25 18:01:04 +08:00
},
{ deep: true },
);
return () => (
<div class="chat-view-wrap w-full h-full flex">
<section class="flex-1 flex flex-col pt-20px justify-center relative px-16px">
<div class="flex-1 overflow-hidden pb-20px">
<BubbleList
ref={bubbleListRef}
roles={roles}
items={[
...conversationList.value,
generateLoading.value ? { loading: true, role: LOADING_ROLE } : null,
2025-08-25 18:01:04 +08:00
].filter(Boolean)}
/>
</div>
<div class="w-full flex flex-col justify-center items-center">
<SenderInput
ref={senderRef}
class="w-600px"
2025-08-25 18:01:04 +08:00
placeholder="继续追问..."
loading={generateLoading.value}
onSubmit={handleSubmit}
onCancel={handleCancel}
/>
<p class="cts !color-#939499 text-12px !lh-20px my-4px">内容由AI生成仅供参考</p>
</div>
</section>
{/* 右侧展示区域 */}
{showRightView.value && (
<RightView
ref={rightViewRef}
rightViewInfo={rightViewInfo.value}
2025-08-25 18:01:04 +08:00
showRightView={showRightView.value}
onClose={() => (showRightView.value = false)}
/>
)}
</div>
);
},
};
</script>
<style lang="scss" scoped>
@import './style.scss';
</style>