Files
lingji-work-fe/src/views/home/components/conversation-detail/index.vue

329 lines
10 KiB
Vue
Raw Normal View History

2025-08-20 18:17:23 +08:00
<script lang="tsx">
2025-08-21 11:40:36 +08:00
import { message, Tooltip } from 'ant-design-vue';
2025-08-21 10:54:18 +08:00
import { BubbleList } from '@/components/xt-chat/xt-bubble';
2025-08-20 18:17:23 +08:00
import SenderInput from '../sender-input/index.vue';
import { Typography } from 'ant-design-vue';
2025-08-21 16:26:57 +08:00
import RightView from './rightView.vue';
2025-08-23 11:53:27 +08:00
import TextOverTips from '@/components/text-over-tips/index.vue';
2025-08-21 16:26:57 +08:00
import { useRoute } from 'vue-router';
import markdownit from 'markdown-it';
2025-08-21 11:40:36 +08:00
import { useClipboard } from '@vueuse/core';
import { genRandomId } from '@/utils/tools';
import { useChatStore } from '@/stores/modules/chat';
2025-08-23 11:53:27 +08:00
import { getHeaders } from '@/api/all/chat';
import { EventSourcePolyfill } from 'event-source-polyfill'; // 导入 event-source-polyfill
2025-08-21 16:26:57 +08:00
import type { BubbleListProps } from '@/components/xt-chat/xt-bubble/types';
2025-08-20 18:17:23 +08:00
const QUESTION_ROLE = 'question';
const ANSWER_ROLE = 'text';
2025-08-23 11:53:27 +08:00
const FILE_ROLE = 'file';
const THOUGHT_ROLE = 'thought'; // 新增思考过程角色常量
2025-08-20 18:17:23 +08:00
export default {
setup(props, { emit, expose }) {
const chatStore = useChatStore();
2025-08-20 18:17:23 +08:00
const route = useRoute();
2025-08-21 16:26:57 +08:00
const { copy } = useClipboard();
2025-08-21 11:40:36 +08:00
2025-08-20 18:17:23 +08:00
const senderRef = ref(null);
2025-08-23 11:53:27 +08:00
const eventSource = ref(null);
2025-08-21 16:26:57 +08:00
const rightViewRef = ref(null);
const bubbleListRef = ref<any>(null);
2025-08-20 18:17:23 +08:00
const searchValue = ref('');
const generateLoading = ref(false);
const conversationList = ref([]);
2025-08-21 16:26:57 +08:00
const currentAnswerId = ref<string | null>(null);
const showRightView = ref(false);
const rightViewContent = ref('');
const md = markdownit({
html: true,
breaks: true,
linkify: true,
typographer: true,
});
2025-08-20 18:17:23 +08:00
const conversationId = computed(() => {
return route.params.conversationId;
});
2025-08-21 11:40:36 +08:00
const onCopy = (content: string) => {
copy(content);
message.success('复制成功!');
};
const onRefresh = (tempId: string, tempIndex: number) => {
generateLoading.value = true;
conversationList.value.splice(tempIndex, 1, {
id: tempId,
loading: true,
role: ANSWER_ROLE,
});
};
2025-08-20 18:17:23 +08:00
const handleSubmit = () => {
2025-08-21 11:40:36 +08:00
if (generateLoading.value) {
message.warning('停止生成后可发送');
return;
}
2025-08-20 18:17:23 +08:00
generateLoading.value = true;
conversationList.value.push({
role: QUESTION_ROLE,
content: searchValue.value,
});
2025-08-21 00:24:36 +08:00
2025-08-21 11:40:36 +08:00
const tempId = genRandomId();
2025-08-20 18:17:23 +08:00
const tempIndex = conversationList.value.length;
conversationList.value.push({
id: tempId,
loading: true,
2025-08-21 00:24:36 +08:00
role: ANSWER_ROLE,
2025-08-20 18:17:23 +08:00
});
2025-08-21 16:26:57 +08:00
currentAnswerId.value = tempId;
2025-08-21 00:24:36 +08:00
2025-08-23 11:53:27 +08:00
initSse();
2025-08-20 18:17:23 +08:00
};
const handleCancel = () => {
generateLoading.value = false;
2025-08-21 16:26:57 +08:00
// 中止当前正在输出的回答
if (currentAnswerId.value && bubbleListRef.value?.abortTypingByKey) {
bubbleListRef.value.abortTypingByKey(currentAnswerId.value);
}
if (showRightView.value) {
rightViewRef.value?.abortTyping?.();
}
2025-08-20 18:17:23 +08:00
message.info('取消生成');
};
const roles: BubbleListProps['roles'] = {
[ANSWER_ROLE]: {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
onTypingComplete: () => {
2025-08-21 10:54:18 +08:00
console.log('onTypingComplete');
2025-08-20 18:17:23 +08:00
generateLoading.value = false;
2025-08-21 16:26:57 +08:00
currentAnswerId.value = null;
},
style: {
width: '600px',
margin: '0 auto',
2025-08-20 18:17:23 +08:00
},
},
2025-08-23 11:53:27 +08:00
[FILE_ROLE]: {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
messageRender: (items) => {
return items.map((item) => (
<div class="file-card">
<icon-file class="w-17px h-20px mr-20px color-#6D4CFE" />
<div>
<TextOverTips
context={item.name}
class="font-family-medium color-#211F24 text-14px font-400 lh-22px mb-4px"
/>
<span class="color-#939499 font-family-regular text-12px font-400 lh-22px">创建时间08-04 12:40</span>
</div>
</div>
));
},
style: {
width: '600px',
margin: '0 auto',
},
},
// 新增思考过程角色配置
[THOUGHT_ROLE]: {
placement: 'start',
variant: 'borderless',
style: {
width: '600px',
margin: '0 auto',
},
},
2025-08-20 18:17:23 +08:00
[QUESTION_ROLE]: {
placement: 'end',
2025-08-21 11:40:36 +08:00
shape: 'round',
2025-08-21 16:26:57 +08:00
style: {
width: '600px',
margin: '0 auto',
},
2025-08-20 18:17:23 +08:00
},
};
2025-08-23 11:53:27 +08:00
const onDownload = (content) => {
console.log('onDownload', content);
};
const initSse = () => {
2025-08-23 11:53:27 +08:00
// 先清理可能存在的旧连接
if (eventSource.value) {
eventSource.value.close();
eventSource.value = null;
}
// 构建SSE连接URL
const url = new URL('http://localhost:3000/agent/input');
url.searchParams.append('content', searchValue.value || '测试');
url.searchParams.append('session_id', conversationId.value as string);
url.searchParams.append('agent_id', chatStore.agentInfo?.agent_id || '67890');
try {
eventSource.value = new EventSourcePolyfill(url.toString(), {
headers: { ...getHeaders(), Accept: 'text/event-stream' },
});
searchValue.value = '';
// 任务开始
eventSource.value.addEventListener('start', function () {
conversationList.value.push({
role: ANSWER_ROLE,
content: (
<div class="flex items-center ">
<span class="font-family-medium color-#211F24 text-14px font-400 lh-22px mr-4px">智能思考</span>
<icon-caret-up size={16} class="color-#211F24" />
</div>
),
});
});
eventSource.value.addEventListener('node_update', function (event) {
console.log('Node updated:', event.data);
const data = JSON.parse(event.data);
switch (data.status) {
case 'running':
conversationList.value.push({
content: data.message,
role: ANSWER_ROLE,
});
break;
case 'success':
conversationList.value.push({
content: data.output,
role: ANSWER_ROLE,
});
break;
}
});
eventSource.value.addEventListener('final_result', function (event) {
showRightView.value = true;
const data = JSON.parse(event.data);
const _files = data.output?.files;
rightViewContent.value = _files[0]?.content || '';
conversationList.value.push({
id: currentAnswerId.value,
role: FILE_ROLE,
content: _files,
footer: ({ item }) => {
const nonQuestionElements = conversationList.value.filter((item) => item.role !== QUESTION_ROLE);
const isLastAnswer = nonQuestionElements[nonQuestionElements.length - 1]?.id === item.id;
return (
<div class="flex items-center">
<Tooltip title="下载" onClick={() => onDownload(rightViewContent.value)}>
<icon-download size={16} class="color-#737478 cursor-pointer" />
</Tooltip>
{isLastAnswer && (
<Tooltip
title="重新生成"
onClick={() => onRefresh(currentAnswerId.value, conversationList.value.length)}
>
<icon-refresh size={16} class="color-#737478 cursor-pointer" />
</Tooltip>
)}
</div>
);
},
});
});
eventSource.value.addEventListener('end', function (event) {
generateLoading.value = false;
closeSse();
});
// 处理错误事件
eventSource.value.onerror = function (error) {
console.error('EventSource error:', error);
generateLoading.value = false;
message.error('连接服务器失败');
closeSse();
};
} catch (error) {
console.error('Failed to initialize SSE:', error);
message.error('初始化连接失败');
generateLoading.value = false;
}
};
2025-08-23 11:53:27 +08:00
const closeSse = () => {
2025-08-23 11:53:27 +08:00
if (eventSource.value) {
eventSource.value.close();
eventSource.value = null;
}
// 确保加载状态被重置
generateLoading.value = false;
};
onMounted(() => {
2025-08-23 11:53:27 +08:00
searchValue.value = chatStore.searchValue;
chatStore.clearSearchValue();
searchValue.value && initSse();
});
2025-08-23 11:53:27 +08:00
onUnmounted(() => {
closeSse();
});
2025-08-20 18:17:23 +08:00
return () => (
<div class="conversation-detail-wrap w-full h-full flex">
2025-08-21 16:26:57 +08:00
<section class="flex-1 flex flex-col pt-20px justify-center relative px-16px">
{/* <div class="w-full h-full flex "> */}
<div class="flex-1 overflow-hidden pb-20px">
<BubbleList ref={bubbleListRef} roles={roles} items={conversationList.value} />
</div>
<div class="w-full flex flex-col justify-center items-center">
<SenderInput
class="w-600px"
ref={senderRef}
placeholder="继续追问..."
loading={generateLoading.value}
v-model={searchValue.value}
onSubmit={handleSubmit}
onCancel={handleCancel}
data-ne="123"
2025-08-21 16:26:57 +08:00
/>
<p class="cts !color-#939499 text-12px !lh-20px my-4px">内容由AI生成仅供参考</p>
</div>
{/* </div> */}
</section>
{/* 右侧展示区域 */}
{showRightView.value && (
<RightView
ref={rightViewRef}
rightViewContent={rightViewContent.value}
showRightView={showRightView.value}
onClose={() => (showRightView.value = false)}
/>
)}
2025-08-20 18:17:23 +08:00
</div>
);
},
};
</script>
<style lang="scss" scoped>
@import './style.scss';
</style>