Files
lingji-work-fe/src/components/xt-chat/chat-view/useChatHandler.tsx

409 lines
13 KiB
TypeScript
Raw Normal View History

import { ref } from 'vue';
2025-08-25 18:01:04 +08:00
import type { BubbleListProps } from '@/components/xt-chat/xt-bubble/types';
import markdownit from 'markdown-it';
import { message as antdMessage, Timeline } from 'ant-design-vue';
2025-08-27 17:18:47 +08:00
import {
IconFile,
IconCaretUp,
IconDownload,
IconCaretDown,
IconRefresh,
IconCopy,
} from '@arco-design/web-vue/es/icon';
2025-08-25 18:01:04 +08:00
import { Tooltip } from 'ant-design-vue';
import TextOverTips from '@/components/text-over-tips/index.vue';
import { genRandomId } from '@/utils/tools';
import icon1 from '@/assets/img/agent/icon-end.png';
import icon2 from '@/assets/img/agent/icon-loading.png';
2025-08-25 18:01:04 +08:00
import { useClipboard } from '@vueuse/core';
import {
QUESTION_ROLE,
ANSWER_ROLE,
INTELLECTUAL_THINKING_ROLE,
FILE_ROLE,
LOADING_ROLE,
THOUGHT_ROLE,
ROLE_STYLE,
EnumTeamRunStatus,
} from './constants';
import type { UseChatHandlerReturn } from './constants';
2025-08-25 18:01:04 +08:00
/**
* Hook
* @returns
2025-08-25 18:01:04 +08:00
*/
export default function useChatHandler({ initSse }): UseChatHandlerReturn {
2025-08-25 18:01:04 +08:00
// 在内部定义对话列表
const { copy } = useClipboard();
const senderRef = ref(null);
2025-08-25 18:01:04 +08:00
const conversationList = ref<any[]>([]);
2025-08-25 23:09:08 +08:00
const generateLoading = ref<boolean>(false);
2025-08-25 18:01:04 +08:00
const currentTaskId = ref<string | null>(null);
const showRightView = ref(false);
const rightViewInfo = ref<any>({});
2025-08-27 12:04:23 +08:00
const lastTeamRunTaskId = ref<string | null>(null); // 最近一个对话的id
2025-08-25 18:01:04 +08:00
// 初始化markdown
2025-08-25 18:01:04 +08:00
const md = markdownit({
html: true,
breaks: true,
linkify: true,
typographer: true,
});
// 定义角色配置
2025-08-25 18:01:04 +08:00
const roles: BubbleListProps['roles'] = {
[LOADING_ROLE]: {
placement: 'start',
variant: 'borderless',
style: { ...ROLE_STYLE, paddingLeft: '12px' },
},
[INTELLECTUAL_THINKING_ROLE]: {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
style: ROLE_STYLE,
},
2025-08-25 18:01:04 +08:00
[ANSWER_ROLE]: {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
onTypingComplete: () => {
currentTaskId.value = null;
},
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
[FILE_ROLE]: {
placement: 'start',
variant: 'borderless',
typing: { step: 2, interval: 100 },
messageRender: (items) => {
return items.map((item) => (
<div class="file-card">
<IconFile class="w-24px h-24px 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: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
[THOUGHT_ROLE]: {
placement: 'start',
variant: 'borderless',
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
[QUESTION_ROLE]: {
placement: 'end',
shape: 'round',
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
};
// 下载处理
const onDownload = () => {
console.log('onDownload', rightViewInfo.value);
2025-08-25 18:01:04 +08:00
};
const onCopy = (content: string) => {
copy(content);
antdMessage.success('复制成功!');
};
// 开始处理
const handleOpen = (data: any): void => {
// const { run_id } = data;
// currentTaskId.value = run_id;
// conversationList.value.push({
// key: run_id,
// run_id,
// role: INTELLECTUAL_THINKING_ROLE,
// content: (
// <div class="flex items-center">
// <span class="font-family-medium color-#211F24 text-14px font-400 lh-22px mr-4px">智能思考</span>
// <IconCaretUp size={16} class="color-#211F24" />
// </div>
// ),
// });
2025-08-25 18:01:04 +08:00
};
// 最终结果处理
2025-08-27 17:18:47 +08:00
const handleFileReview = (extra_data: { type: string; data: any }) => {
console.log('handleFileReview', extra_data);
const { type, data } = extra_data;
2025-08-25 18:01:04 +08:00
2025-08-25 23:09:08 +08:00
showRightView.value = true;
2025-08-27 17:18:47 +08:00
rightViewInfo.value = data?.[0] ?? {};
// conversationList.value.push({
// run_id,
// role: FILE_ROLE,
// content: _files,
// style: ANSWER_STYLE,
// footer: ({ item }: { item: any }) => {
// const nonQuestionElements = conversationList.value.filter((item) => item.role !== QUESTION_ROLE);
2025-08-27 12:04:23 +08:00
// const isLastRunTask = nonQuestionElements[nonQuestionElements.length - 1]?.id === item.id;
// },
// });
};
2025-08-25 18:01:04 +08:00
// 重置生成状态
const resetGenerateStatus = () => {
generateLoading.value = false;
};
// // 错误处理
// const handleError = () => {
// resetGenerateStatus();
// antdMessage.error('连接服务器失败');
// };
2025-08-27 12:04:23 +08:00
const onRefresh = (run_id: string) => {
generateLoading.value = true;
conversationList.value = conversationList.value.filter((item) => item.run_id !== run_id);
initSse({ message: senderRef.value?.searchValue });
};
2025-08-27 17:18:47 +08:00
const getAllRunTask = (teamRunTaskId: string) => {
return conversationList.value.filter(
(item) => item.role === ANSWER_ROLE && item.teamRunTaskId === teamRunTaskId && !item.isTeamRunTask,
2025-08-27 12:04:23 +08:00
);
2025-08-27 17:18:47 +08:00
};
const getRunTask = (run_id: string) => {
return conversationList.value.find((item) => item.run_id === run_id && !item.isTeamRunTask);
};
// 设置当前对话所有过程任务展开收起状态
const setRunTaskCollapse = (teamRunTaskId: string, isCollapse: boolean) => {
getAllRunTask(teamRunTaskId).forEach((item) => {
item.content.isCollapse = isCollapse;
});
};
// 获取同一个对话下的最后一个run_task
const getLastRunTask = (teamRunTaskId: string) => {
const allRunTask = getAllRunTask(teamRunTaskId);
2025-08-27 12:04:23 +08:00
return allRunTask[allRunTask.length - 1] ?? {};
};
2025-08-27 17:18:47 +08:00
const getFirstRunTask = (teamRunTaskId: string) => {
const allRunTask = getAllRunTask(teamRunTaskId);
2025-08-27 12:04:23 +08:00
return allRunTask[0] ?? {};
};
2025-08-27 17:18:47 +08:00
// 判断当前对话是否含有过程任务
const hasRunTask = (teamRunTaskId: string) => {
return conversationList.value.some((item) => item.teamRunTaskId === teamRunTaskId && !item.isTeamRunTask);
};
const getTeamRunTask = (teamRunTaskId: string) => {
return conversationList.value.find((item) => item.teamRunTaskId === teamRunTaskId);
};
2025-08-27 12:04:23 +08:00
const isLastRunTask = (data: MESSAGE.Answer, teamRunTaskId: string): boolean => {
const { run_id } = data;
2025-08-27 17:18:47 +08:00
return getLastRunTask(teamRunTaskId).run_id === run_id;
2025-08-27 12:04:23 +08:00
};
const isFirstRunTask = (data: MESSAGE.Answer, teamRunTaskId: string): boolean => {
const { run_id } = data;
2025-08-27 17:18:47 +08:00
return getFirstRunTask(teamRunTaskId).run_id === run_id;
2025-08-27 12:04:23 +08:00
};
const isLastTeamRunTask = (data: MESSAGE.Answer) => {
const { run_id } = data;
const lastElement = conversationList.value[conversationList.value.length - 1];
return lastElement && lastElement.run_id === run_id;
};
// 过程节点开始
const handleRunTaskStart = (data: MESSAGE.Answer) => {
const { run_id } = data;
currentTaskId.value = run_id;
2025-08-25 18:01:04 +08:00
conversationList.value.push({
run_id,
2025-08-27 12:04:23 +08:00
key: run_id,
2025-08-27 17:18:47 +08:00
teamRunTaskId: lastTeamRunTaskId.value,
content: { ...data, runStatus: EnumTeamRunStatus.RunStarted },
output: data.output,
role: ANSWER_ROLE,
messageRender: (data: MESSAGE.Answer) => {
2025-08-27 17:18:47 +08:00
const { node, output, runStatus, isCollapse = true } = data;
const isRulCompleted = runStatus === EnumTeamRunStatus.RunCompleted;
// console.log('messageRender', isCollapse);
let outputEleClass: string = `thought-chain-output border-l-#E6E6E8 border-l-1px pl-12px relative left-6px mb-4px`;
2025-08-27 12:04:23 +08:00
!isLastRunTask(data, lastTeamRunTaskId.value) && (outputEleClass += ' hasLine pb-12px pt-4px');
2025-08-27 17:18:47 +08:00
return (
<>
2025-08-27 12:04:23 +08:00
{isFirstRunTask(data, lastTeamRunTaskId.value) && (
<div class="flex items-center">
<span class="font-family-medium color-#211F24 text-14px font-400 lh-22px mr-4px"></span>
2025-08-27 17:18:47 +08:00
{isCollapse ? (
<IconCaretUp
size={16}
class="color-#211F24 cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, false)}
/>
) : (
<IconCaretDown
size={16}
class="color-#211F24 cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, true)}
/>
)}
2025-08-27 12:04:23 +08:00
</div>
)}
2025-08-27 17:18:47 +08:00
{isCollapse && (
<div class="relative thought-chain-item">
<div class="flex items-center mb-4px">
<img src={isRulCompleted ? icon1 : icon2} width={13} height={13} class="mr-4px" />
<div>{node}</div>
</div>
<div v-html={md.render(output)} class={outputEleClass} />
</div>
2025-08-27 17:18:47 +08:00
)}
</>
);
2025-08-25 18:01:04 +08:00
},
});
};
2025-08-27 12:04:23 +08:00
// 过程节点更新
const handleRunTaskUpdate = (data: MESSAGE.Answer) => {
const { run_id, output } = data;
2025-08-25 18:01:04 +08:00
2025-08-27 12:04:23 +08:00
const existingItem = conversationList.value.find((item) => item.run_id === run_id);
if (existingItem && output) {
existingItem.content.output += output;
2025-08-27 17:18:47 +08:00
existingItem.content.runStatus = EnumTeamRunStatus.RunResponseContent;
2025-08-27 12:04:23 +08:00
}
2025-08-25 18:01:04 +08:00
};
2025-08-27 12:04:23 +08:00
// 过程节点结束
const handleRunTaskEnd = (data: MESSAGE.Answer) => {
2025-08-27 17:18:47 +08:00
const { output } = data;
2025-08-25 18:01:04 +08:00
2025-08-27 17:18:47 +08:00
const existingItem = getRunTask(data.run_id);
2025-08-27 12:04:23 +08:00
if (existingItem) {
existingItem.content.output += output;
2025-08-27 17:18:47 +08:00
existingItem.content.runStatus = EnumTeamRunStatus.RunCompleted;
2025-08-27 12:04:23 +08:00
}
2025-08-25 18:01:04 +08:00
};
2025-08-27 12:04:23 +08:00
// 任务开始
const handleTeamRunTaskStart = (data: MESSAGE.Answer) => {
const { run_id } = data;
2025-08-27 12:04:23 +08:00
lastTeamRunTaskId.value = run_id;
currentTaskId.value = run_id;
conversationList.value.push({
run_id,
isTeamRunTask: true,
2025-08-27 17:18:47 +08:00
teamRunTaskId: lastTeamRunTaskId.value,
2025-08-27 12:04:23 +08:00
key: run_id,
2025-08-27 17:18:47 +08:00
content: { ...data, teamRunStatus: EnumTeamRunStatus.TeamRunStarted },
2025-08-27 12:04:23 +08:00
output: data.output,
role: ANSWER_ROLE,
messageRender: (data: MESSAGE.Answer) => {
return <div v-html={md.render(data.output ?? '')} />;
},
});
2025-08-25 18:01:04 +08:00
};
2025-08-27 12:04:23 +08:00
// 任务更新
const handleTeamRunTaskUpdate = (data: MESSAGE.Answer) => {
const { run_id, output } = data;
2025-08-27 12:04:23 +08:00
const existingItem = conversationList.value.find((item) => item.run_id === run_id);
if (existingItem && output) {
existingItem.content.output += output;
2025-08-27 17:18:47 +08:00
existingItem.content.teamRunStatus = EnumTeamRunStatus.TeamRunResponseContent;
}
};
2025-08-27 12:04:23 +08:00
// 任务结束
const handleTeamRunTaskEnd = (data: MESSAGE.Answer) => {
2025-08-27 17:18:47 +08:00
resetGenerateStatus();
2025-08-27 17:18:47 +08:00
const { run_id, extra_data } = data;
const _hasRunTask = hasRunTask(lastTeamRunTaskId.value);
const _targetTask = _hasRunTask ? getLastRunTask(run_id) : getTeamRunTask(lastTeamRunTaskId.value);
2025-08-27 17:18:47 +08:00
if (isEmpty(_targetTask)) {
return;
}
2025-08-27 17:18:47 +08:00
if (_hasRunTask) {
setRunTaskCollapse(lastTeamRunTaskId.value, false);
} else {
_targetTask.content.teamRunStatus = EnumTeamRunStatus.TeamRunCompleted;
}
2025-08-27 17:18:47 +08:00
if (extra_data) {
handleFileReview(extra_data);
}
2025-08-27 17:18:47 +08:00
_targetTask.footer = () => {
return (
<div class="flex items-center">
{!extra_data
// ? (
// <Tooltip title="下载" onClick={onDownload}>
// <IconDownload size={16} class="color-#737478 cursor-pointer mr-12px" />
// </Tooltip>
// ) :
&& (
<Tooltip title="复制" onClick={() => onCopy(_targetTask.content.output)}>
<IconCopy size={16} class="color-#737478 cursor-pointer mr-12px" />
</Tooltip>
)}
{isLastTeamRunTask(data) && (
<Tooltip title="重新生成" onClick={() => onRefresh(run_id)}>
<IconRefresh size={16} class="color-#737478 cursor-pointer" />
</Tooltip>
)}
</div>
);
};
};
2025-08-27 12:04:23 +08:00
// 消息处理主函数
const handleMessage = (parsedData: { event: string; data: MESSAGE.Answer }) => {
const { data } = parsedData;
const { status } = data;
switch (status) {
2025-08-27 12:04:23 +08:00
case EnumTeamRunStatus.RunStarted:
handleRunTaskStart(data);
break;
case EnumTeamRunStatus.RunResponseContent:
handleRunTaskUpdate(data);
break;
case EnumTeamRunStatus.RunCompleted:
handleRunTaskEnd(data);
break;
case EnumTeamRunStatus.TeamRunStarted:
2025-08-27 12:04:23 +08:00
handleTeamRunTaskStart(data);
2025-08-25 18:01:04 +08:00
break;
case EnumTeamRunStatus.TeamRunResponseContent:
2025-08-27 12:04:23 +08:00
handleTeamRunTaskUpdate(data);
2025-08-25 18:01:04 +08:00
break;
case EnumTeamRunStatus.TeamRunCompleted:
2025-08-27 12:04:23 +08:00
handleTeamRunTaskEnd(data);
2025-08-25 18:01:04 +08:00
break;
default:
break;
}
};
return {
roles,
senderRef,
2025-08-25 18:01:04 +08:00
currentTaskId,
handleMessage,
handleOpen,
2025-08-25 18:01:04 +08:00
generateLoading,
conversationList,
showRightView,
rightViewInfo,
2025-08-25 18:01:04 +08:00
};
}