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

196 lines
5.1 KiB
Vue
Raw Normal View History

2025-08-25 18:01:04 +08:00
<script lang="tsx">
2025-08-28 23:20:44 +08:00
import { message as antdMessage } from 'ant-design-vue';
2025-08-25 18:01:04 +08:00
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 { useChatStore } from '@/stores/modules/chat';
2025-08-27 18:07:36 +08:00
import { getConversationList } from '@/api/all/chat';
2025-08-25 18:01:04 +08:00
import querySSE from '@/utils/querySSE';
import useChatHandler from './useChatHandler';
2025-08-28 23:20:44 +08:00
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,
},
2025-08-28 12:03:52 +08:00
conversationId: {
type: String,
default: '',
},
2025-08-25 18:01:04 +08:00
},
setup(props, { emit, expose }) {
const chatStore = useChatStore();
2025-08-28 12:03:52 +08:00
// const route = useRoute();
2025-08-25 18:01:04 +08:00
const rightViewRef = ref(null);
const bubbleListRef = ref<any>(null);
const sseController = ref<any>(null);
2025-08-28 15:45:09 +08:00
const searchValue = ref<string>('');
2025-08-25 18:01:04 +08:00
const handleSubmit = (message: string) => {
if (generateLoading.value) {
antdMessage.warning('停止生成后可发送');
return;
}
2025-08-27 12:04:23 +08:00
2025-08-28 15:45:09 +08:00
searchValue.value = '';
conversationList.value.push({
role: QUESTION_ROLE,
content: message,
2025-08-28 23:20:44 +08:00
});
2025-08-25 18:01:04 +08:00
initSse({ message });
};
const clearSseController = () => {
if (sseController.value) {
sseController.value.abort?.();
sseController.value = null;
}
2025-08-28 15:45:09 +08:00
};
2025-08-25 18:01:04 +08:00
const handleCancel = () => {
if (generateLoading.value) {
bubbleListRef.value?.abortTypingByKey(generateTeamRunTaskId.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 = async (inputInfo: CHAT.TInputInfo): Promise<void> => {
clearSseController();
2025-08-25 18:01:04 +08:00
try {
const { message } = inputInfo;
generateLoading.value = true;
sseController.value = await querySSE({
method: 'POST',
handleMessage,
body: JSON.stringify({
content: message,
2025-08-28 12:03:52 +08:00
session_id: props.conversationId,
agent_id: chatStore.agentInfo.agent_id,
}),
});
2025-08-28 23:20:44 +08:00
2025-08-25 18:01:04 +08:00
} catch (error) {
console.error('Failed to initialize SSE:', error);
antdMessage.error('初始化连接失败');
generateLoading.value = false;
}
};
2025-08-28 12:03:52 +08:00
const getConversationInfo = async () => {
const { data, code } = await getConversationList({
session_id: props.conversationId,
agent_id: chatStore.agentInfo.agent_id,
});
if (code === 200) {
const remoteData = (data.list?.flat(1) ?? []).map((v: any) => ({
...v,
teamRunTaskId: v.step_run_id,
}));
conversationList.value = [...conversationList.value, ...remoteData];
2025-08-28 12:03:52 +08:00
}
};
const {
roles,
showRightView,
2025-08-28 12:03:52 +08:00
rightViewData,
generateTeamRunTaskId,
handleMessage,
conversationList,
generateLoading,
senderRef,
} = useChatHandler({
initSse,
});
2025-08-25 18:01:04 +08:00
watch(
() => props.inputInfo,
2025-08-28 12:03:52 +08:00
async (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,
});
2025-08-28 23:20:44 +08:00
initSse(newVal);
}
2025-08-25 18:01:04 +08:00
},
{ deep: true },
);
2025-08-28 12:03:52 +08:00
watch(
() => props.conversationId,
(newVal) => {
if (newVal) {
getConversationInfo();
2025-08-27 18:07:36 +08:00
}
2025-08-28 12:03:52 +08:00
},
{ immediate: true },
);
2025-08-27 18:07:36 +08:00
onUnmounted(() => {
clearSseController();
2025-08-28 15:45:09 +08:00
});
2025-08-25 18:01:04 +08:00
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
2025-08-28 15:45:09 +08:00
v-model={searchValue.value}
2025-08-25 18:01:04 +08:00
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}
2025-08-28 12:03:52 +08:00
rightViewData={rightViewData.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>