wujingjing
2025-04-10 8aa7ffddc511138d61d64029157c11cfccc5431d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { computed, ref, type Ref } from 'vue';
import { RoleEnum } from '../../../model/types';
import { uniqBy } from 'lodash-es';
 
type usePickHistoryOption = {
    msgList: Ref<any[]>;
    inputValue: Ref<string>;
};
export const usePickHistory = (option: usePickHistoryOption) => {
    const { msgList, inputValue } = option;
    let historyPickIndex = null;
    /** @description 是否循环选择 */
    const loopPick = false;
    const userMsgList = computed(() => {
        const userMsgs = msgList.value.filter((item) => item.role === RoleEnum.user && !!item?.content?.values);
        return userMsgs;
    });
 
    const getLastIndexMsg = (lastIndex: number) => {
        const absIndex = Math.abs(lastIndex);
        const index = absIndex % userMsgList.value.length;
        lastIndex = -(index === 0 ? userMsgList.value.length : index);
        return userMsgList.value[userMsgList.value.length + lastIndex];
    };
 
    //显示上一条消息
    const setUpQuestion = () => {
        let msg = null;
        // debugger;
        if (historyPickIndex === null) {
            historyPickIndex = -1;
            msg = getLastIndexMsg(historyPickIndex);
            if (!msg) {
                historyPickIndex = null;
                return;
            }
        } else {
            if (!loopPick && Math.abs(historyPickIndex) === userMsgList.value.length) return;
 
            historyPickIndex--;
            msg = getLastIndexMsg(historyPickIndex);
            if (!msg) {
                historyPickIndex++;
                return;
            }
        }
        inputValue.value = msg?.content?.values ?? '';
    };
    //显示下一条消息
    const setDownQuestion = () => {
        if (historyPickIndex === null) return;
        if (!loopPick && Math.abs(historyPickIndex) === 1) return;
 
        historyPickIndex++;
        const msg = getLastIndexMsg(historyPickIndex);
        if (!msg) {
            historyPickIndex--;
            return;
        }
        inputValue.value = msg?.content?.values ?? '';
    };
 
    return {
        setUpQuestion,
        setDownQuestion,
    };
};