import { ElMessage } from 'element-plus';
|
import type { ComputedRef } from 'vue';
|
import { computed, nextTick, ref } from 'vue';
|
// import useClipboard from 'vue-clipboard3';
|
import { onClickOutside, useClipboard } from '@vueuse/core';
|
import type { ChatMessage } from '../model/types';
|
import { AnswerState, AnswerType, RoleEnum } from '../model/types';
|
import { SetHistoryAnswerState } from '/@/api/ai/chat';
|
import { isSharePage } from '/@/stores/chatRoom';
|
|
export type AssistantContentOptOption = {
|
sendChatMessage: any;
|
displayMessageList: ComputedRef<ChatMessage[]>;
|
};
|
|
export const useAssistantContentOpt = (option: AssistantContentOptOption) => {
|
const { sendChatMessage, displayMessageList } = option;
|
const { copy } = useClipboard();
|
const preQuestion = ref(null);
|
|
const copyClick = (item) => {
|
const type = item.content.type;
|
let text = '';
|
if (type === AnswerType.Knowledge) {
|
text = item.content.values?.map((item) => item.answer).join('\n\n') ?? '';
|
} else {
|
text = item.content.values;
|
}
|
ElMessage.success('复制成功');
|
copy(text);
|
};
|
|
const likeClick = async (item) => {
|
const toSetState = item.state === AnswerState.Like ? AnswerState.Null : AnswerState.Like;
|
const res = await SetHistoryAnswerState({
|
history_id: item.historyId,
|
answer_state: toSetState,
|
});
|
item.state = toSetState;
|
|
};
|
|
const unLikeClick = async (item) => {
|
const toSetState = item.state === AnswerState.Unlike ? AnswerState.Null : AnswerState.Unlike;
|
const res = await SetHistoryAnswerState({
|
history_id: item.historyId,
|
answer_state: toSetState,
|
});
|
item.state = toSetState;
|
|
|
};
|
const feedbackPosition = ref({
|
x: 0,
|
y: 0,
|
});
|
|
const feedbackIsShow = ref(false);
|
const feedbackContent = ref('');
|
const feedbackPanelRef = ref<HTMLDivElement>(null);
|
const currentFeedbackMapItem = ref(null);
|
const curFeedbackIndex = ref(0);
|
const feedbackClick = async (e, item, index) => {
|
currentFeedbackMapItem.value = item;
|
curFeedbackIndex.value = index;
|
const offsetX = -4;
|
const offsetY = -8;
|
feedbackIsShow.value = true;
|
nextTick(() => {
|
feedbackPosition.value = {
|
x: -feedbackPanelRef.value[index].$el.clientWidth + offsetX,
|
y: -feedbackPanelRef.value[index].$el.clientHeight + offsetY,
|
};
|
});
|
};
|
|
onClickOutside(
|
computed(() => feedbackPanelRef.value?.[curFeedbackIndex.value]),
|
(e) => {
|
feedbackIsShow.value = false;
|
feedbackContent.value = '';
|
}
|
);
|
// useClickOther(
|
// computed(() => feedbackPanelRef.value?.[curFeedbackIndex.value]),
|
// feedbackIsShow,
|
// () => {
|
// feedbackIsShow.value = false;
|
// feedbackContent.value = '';
|
// }
|
// );
|
const showAskMore = computed(() => {
|
if (!displayMessageList.value || displayMessageList.value.length === 0) return false;
|
const last = displayMessageList.value.at(-1);
|
const isShow = last?.role === RoleEnum.assistant && last?.content?.values && last.content?.askMoreList?.length > 0;
|
return isShow && !isSharePage ;
|
});
|
|
const showFixQuestion = (item) => {
|
const isShow = item?.role === RoleEnum.assistant && item?.content?.values && item.content?.origin?.err_json?.fix_question;
|
return isShow;
|
};
|
const askMoreClick = (item) => {
|
if (!item.question) return;
|
sendChatMessage({ type: AnswerType.Text, values: item.question });
|
};
|
|
const fixQuestionClick = (item, originData) => {
|
if (!item.question) return;
|
preQuestion.value = originData?.question;
|
try {
|
sendChatMessage({
|
type: AnswerType.Text,
|
values: item.question,
|
});
|
} finally {
|
preQuestion.value = null;
|
}
|
};
|
|
return {
|
copyClick,
|
likeClick,
|
unLikeClick,
|
feedbackPosition,
|
feedbackIsShow,
|
feedbackContent,
|
feedbackPanelRef,
|
currentFeedbackMapItem,
|
curFeedbackIndex,
|
feedbackClick,
|
askMoreClick,
|
fixQuestionClick,
|
preQuestion,
|
showAskMore,
|
showFixQuestion,
|
};
|
};
|