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

397 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';
2025-08-27 17:41:10 +08:00
import { message as antdMessage } 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';
2025-08-27 17:41:10 +08:00
import { genRandomId, exactFormatTime } from '@/utils/tools';
2025-08-25 18:01:04 +08:00
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,
LOADING_ROLE,
ROLE_STYLE,
EnumTeamRunStatus,
2025-08-27 18:07:36 +08:00
REMOTE_USER_ROLE,
REMOTE_ASSISTANT_ROLE,
2025-08-27 17:41:10 +08:00
FILE_TYPE_MAP,
} 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);
const generateTeamRunTaskId = ref<string | null>(null);
2025-08-25 18:01:04 +08:00
const showRightView = ref(false);
2025-08-28 12:03:52 +08:00
const rightViewData = 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: () => {
// generateTeamRunTaskId.value = null;
// },
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
2025-08-27 18:07:36 +08:00
[QUESTION_ROLE]: {
placement: 'end',
shape: 'round',
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
2025-08-27 18:07:36 +08:00
[REMOTE_USER_ROLE]: {
2025-08-25 18:01:04 +08:00
placement: 'end',
shape: 'round',
style: ROLE_STYLE,
2025-08-25 18:01:04 +08:00
},
2025-08-27 18:07:36 +08:00
[REMOTE_ASSISTANT_ROLE]: {
placement: 'start',
variant: 'borderless',
style: ROLE_STYLE,
2025-08-27 18:12:14 +08:00
messageRender: (message: string) => {
2025-08-28 12:03:52 +08:00
return <div v-html={md.render(message)} />;
},
footer: (params) => {
const { content, item } = params as { content: string; item: MESSAGE.Answer };
const isShow = conversationList.value[conversationList.value.length - 1].run_id === item.run_id;
return (
<div class="flex items-center">
<Tooltip title="复制" onClick={() => onCopy(content)}>
<IconCopy size={16} class="color-#737478 cursor-pointer mr-12px" />
</Tooltip>
{isShow && (
<Tooltip title="重新生成" onClick={() => handleRemoteRefresh(item)}>
<IconRefresh size={16} class="color-#737478 cursor-pointer" />
</Tooltip>
)}
</div>
);
2025-08-27 18:12:14 +08:00
},
2025-08-27 18:07:36 +08:00
},
2025-08-25 18:01:04 +08:00
};
// 下载处理
const onDownload = () => {
2025-08-28 12:03:52 +08:00
console.log('onDownload', rightViewData.value);
2025-08-25 18:01:04 +08:00
};
const onCopy = (content: string) => {
copy(content);
antdMessage.success('复制成功!');
};
// 重置生成状态
const resetGenerateStatus = () => {
generateLoading.value = false;
generateTeamRunTaskId.value = null;
};
2025-08-28 12:03:52 +08:00
const handleRemoteRefresh = (item: MESSAGE.Answer) => {
generateLoading.value = true;
const targetIndex = conversationList.value.findIndex(
(v) => v.teamRunTaskId === item.teamRunTaskId && v.run_id === item.run_id && v.role === REMOTE_ASSISTANT_ROLE,
);
const message = conversationList.value[targetIndex - 1]?.content;
conversationList.value.splice(targetIndex, 1);
initSse({ message });
};
2025-08-27 12:04:23 +08:00
const onRefresh = (run_id: string) => {
generateLoading.value = true;
2025-08-28 12:03:52 +08:00
const targetIndex = conversationList.value.findIndex((v) => v.teamRunTaskId === run_id);
conversationList.value = conversationList.value.filter((item) => item.teamRunTaskId !== run_id);
const message = conversationList.value[targetIndex - 1]?.content;
initSse({ message });
2025-08-27 12:04:23 +08:00
};
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);
};
2025-08-28 12:03:52 +08:00
// 设置当前对话所有思考过程任务展开收起状态
2025-08-27 17:18:47 +08:00
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 17:41:10 +08:00
const isLastRunTask = (run_id: string): boolean => {
return getLastRunTask(lastTeamRunTaskId.value).run_id === run_id;
2025-08-27 12:04:23 +08:00
};
2025-08-27 17:41:10 +08:00
const isFirstRunTask = (run_id: string): boolean => {
return getFirstRunTask(lastTeamRunTaskId.value).run_id === run_id;
2025-08-27 12:04:23 +08:00
};
// 过程节点开始
const handleRunTaskStart = (data: MESSAGE.Answer) => {
const { run_id } = data;
// generateTeamRunTaskId.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:41:10 +08:00
const { node, output, runStatus, isCollapse = true, customRender } = data;
2025-08-27 17:18:47 +08:00
const isRulCompleted = runStatus === EnumTeamRunStatus.RunCompleted;
let outputEleClass: string = `thought-chain-output border-l-#E6E6E8 border-l-1px pl-12px relative left-6px mb-4px`;
2025-08-27 17:41:10 +08:00
!isLastRunTask(run_id) && (outputEleClass += ' hasLine pb-12px pt-4px');
2025-08-27 17:18:47 +08:00
return (
<>
2025-08-27 17:41:10 +08:00
{isFirstRunTask(run_id) && (
<div
class="flex items-center mb-8px cursor-pointer"
onClick={() => setRunTaskCollapse(lastTeamRunTaskId.value, !isCollapse)}
>
2025-08-27 12:04:23 +08:00
<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 " />
2025-08-27 17:18:47 +08:00
) : (
<IconCaretDown size={16} class="color-#211F24" />
2025-08-27 17:18:47 +08:00
)}
2025-08-27 12:04:23 +08:00
</div>
)}
2025-08-27 17:41:10 +08:00
{isCollapse ? (
2025-08-27 17:18:47 +08:00
<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:41:10 +08:00
) : (
customRender?.()
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;
generateTeamRunTaskId.value = run_id;
2025-08-27 12:04:23 +08:00
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:41:10 +08:00
const { run_id, extra_data, output } = data;
2025-08-27 17:18:47 +08:00
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-28 12:03:52 +08:00
// 含有思考过程,折叠思考过程,展示结果
2025-08-27 17:18:47 +08:00
if (_hasRunTask) {
setRunTaskCollapse(lastTeamRunTaskId.value, false);
2025-08-28 12:03:52 +08:00
if (extra_data) {
showRightView.value = true;
rightViewData.value = extra_data.data;
}
2025-08-27 17:41:10 +08:00
_targetTask.content.customRender = () => (
<>
<div v-html={md.render(output)} />
2025-08-28 12:03:52 +08:00
{extra_data && (
<div class="file-card mt-10px">
<IconFile class="w-24px h-24px mr-16px color-#6D4CFE" />
<div>
<TextOverTips
context={FILE_TYPE_MAP?.[extra_data.data?.file_type] ?? '-'}
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">
{exactFormatTime(dayjs().unix())}
</span>
</div>
2025-08-27 17:41:10 +08:00
</div>
2025-08-28 12:03:52 +08:00
)}
2025-08-27 17:41:10 +08:00
</>
);
2025-08-28 12:03:52 +08:00
} else {
_targetTask.content.teamRunStatus = EnumTeamRunStatus.TeamRunCompleted;
}
2025-08-27 18:07:36 +08:00
2025-08-27 17:18:47 +08:00
_targetTask.footer = () => {
2025-08-28 12:03:52 +08:00
const isShow = conversationList.value[conversationList.value.length - 1].teamRunTaskId === run_id;
2025-08-27 17:18:47 +08:00
return (
<div class="flex items-center">
2025-08-27 17:41:10 +08:00
{!extra_data && (
// ? (
// <Tooltip title="下载" onClick={onDownload}>
// <IconDownload size={16} class="color-#737478 cursor-pointer mr-12px" />
// </Tooltip>
// ) :
2025-08-27 17:18:47 +08:00
<Tooltip title="复制" onClick={() => onCopy(_targetTask.content.output)}>
<IconCopy size={16} class="color-#737478 cursor-pointer mr-12px" />
</Tooltip>
)}
2025-08-28 12:03:52 +08:00
{isShow && (
2025-08-27 17:18:47 +08:00
<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,
generateTeamRunTaskId,
2025-08-25 18:01:04 +08:00
handleMessage,
generateLoading,
conversationList,
showRightView,
2025-08-28 12:03:52 +08:00
rightViewData,
2025-08-25 18:01:04 +08:00
};
}