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

148 lines
4.5 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 type { BubbleListProps } from 'ant-design-x-vue';
import { useRoute } from 'vue-router';
import SenderInput from '../sender-input/index.vue';
import markdownit from 'markdown-it';
import { Typography } from 'ant-design-vue';
2025-08-21 11:40:36 +08:00
import { useClipboard } from '@vueuse/core';
import { genRandomId } from '@/utils/tools';
2025-08-20 18:17:23 +08:00
const QUESTION_ROLE = 'question';
const ANSWER_ROLE = 'text';
export default {
setup(props, { emit, expose }) {
const route = useRoute();
2025-08-21 11:40:36 +08:00
const { copy, copied, isSupported } = useClipboard();
2025-08-20 18:17:23 +08:00
const senderRef = ref(null);
const searchValue = ref('');
const generateLoading = ref(false);
const conversationList = ref([]);
const md = markdownit({ html: true, breaks: true });
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 00:24:36 +08:00
2025-08-20 18:17:23 +08:00
setTimeout(() => {
2025-08-21 11:40:36 +08:00
const content = `> Render as markdown content to show rich text!`;
2025-08-20 18:17:23 +08:00
searchValue.value = '';
conversationList.value.splice(tempIndex, 1, {
id: tempId,
loading: false,
role: ANSWER_ROLE,
content,
messageRender: (content) => (
<Typography>
<div v-html={md.render(content)}></div>
</Typography>
),
2025-08-21 11:40:36 +08:00
footer: ({ item }) => {
// 判断当前元素是否是最后一个非QUESTION_ROLE的元素
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={() => onCopy(content)}>
<icon-copy size={16} class="mr-12px color-#737478 cursor-pointer" />
</Tooltip>
{isLastAnswer && (
<Tooltip title="重新生成" onClick={() => onRefresh(tempId, tempIndex)}>
<icon-refresh size={16} class="color-#737478 cursor-pointer" />
</Tooltip>
)}
</div>
);
},
2025-08-20 18:17:23 +08:00
});
}, 1000);
};
const handleCancel = () => {
generateLoading.value = false;
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;
},
},
[QUESTION_ROLE]: {
placement: 'end',
2025-08-21 11:40:36 +08:00
shape: 'round',
2025-08-20 18:17:23 +08:00
},
};
return () => (
<div class="conversation-detail-wrap w-full h-full flex">
<div class="flex-1 flex justify-center">
<section class="main w-600px h-full relative flex flex-col pt-20px">
<BubbleList class="flex-1" roles={roles} items={conversationList.value} />
<div class="w-full flex flex-col justify-center items-center">
<SenderInput
class="w-full"
ref={senderRef}
placeholder="继续追问..."
loading={generateLoading.value}
2025-08-21 11:40:36 +08:00
v-model={searchValue.value}
2025-08-20 18:17:23 +08:00
onSubmit={handleSubmit}
onCancel={handleCancel}
/>
<p class="cts !color-#939499 text-12px !lh-20px my-4px">内容由AI生成仅供参考</p>
</div>
</section>
</div>
</div>
);
},
};
</script>
<style lang="scss" scoped>
@import './style.scss';
</style>