yangyin
2024-10-29 5cb1b28d760e268a9e0bdb96843a654c240915b6
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { computed, ref } from 'vue';
import { getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat';
import type { ChatRoomItem } from '../layout/component/sidebar/components/types';
/**
 * Room 关联的一些配置
 */
export type RoomConfig = {
    /** 是否直接调用大模型(通义千问)回答 */
    isAnswerByLLM: boolean;
    /** @description 从首页进去获取的第一个回复,回调函数 */
    firstResCb: any;
    /** @description 当前聊天室的 group_type */
    activeGroupType: string;
};
 
export type RoomConfigKey = keyof RoomConfig;
export const roomConfig = ref<Record<string, RoomConfig>>(null);
 
export const setRoomConfig = <T extends RoomConfigKey>(roomId: string, key: T, value: RoomConfig[T]) => {
    if (!roomId) return;
    if (!roomConfig.value) {
        roomConfig.value = {};
    }
    if (!roomConfig.value[roomId]) {
        roomConfig.value[roomId] = {
            [key]: value,
        } as any;
    } else {
        roomConfig.value[roomId][key] = value;
    }
};
 
export const getRoomConfig = <T extends RoomConfigKey>(roomId: string, key: T) => {
    if (!roomId) return;
    if (!roomConfig.value) {
        return null;
    }
    if (!roomConfig.value[roomId]) {
        return null;
    } else {
        return roomConfig.value[roomId][key];
    }
};
 
export const chatRoomList = ref<ChatRoomItem[]>([]);
 
export const activeRoomId = ref(null);
export const activeChatRoom = computed(() => chatRoomList.value?.find((item) => item.id === activeRoomId.value));
export const activeSampleId = ref(null);
export const activeSectionAId = ref(null);
export const topGroupId = ref(null);
 
export const activeLLMId = ref(null);
 
/** @description 当前聊天室 groupType */
export const activeGroupType = computed({
    get: () => {
        const result = getRoomConfig(activeRoomId.value, 'activeGroupType') ?? '业务场景';
        return result;
    },
    set: (value) => {
        setRoomConfig(activeRoomId.value, 'activeGroupType', value);
    },
});
 
/**
 * 全局使用的 ref
 */
export const sectionAList = ref([]);
 
//#region ====================== 全局使用数据 ======================
// group 列表
export const sceneGroupList = ref([]);
// groupType 列表
export const groupTypeList = computed(() => Array.from(new Set(sceneGroupList.value.map((item) => item.group_type))));
// 办公/模板 列表
export const exampleSceneList = ref([]);
export const officeList = ref([]);
export const groupTypeMapIcon = {
    办公助手: 'ywicon-bangong',
    知识库: 'ywicon-changyonggongjuzhishisuoyin',
    业务场景: 'ywicon-yewu',
};
//获取场景选择列表
const getSceneGroupList = async () => {
    const res = await getSectionList();
    sceneGroupList.value = res?.groups ?? [];
};
const getSelectListSample = async () => {
    const res1 = getSelectSample({});
    const res2 = getUserTemplateList();
    const [sampleListPromise, templateListPromise] = await Promise.allSettled([res1, res2]);
    const samples = (sampleListPromise as any).value?.samples ?? [];
    const templateSamples = ((templateListPromise as any).value?.templates ?? []).map((item) => ({
        group_id: item.template_group,
        sample_id: item.template_id,
        sample_title: item.template_title,
        sample_question: item.template_value,
 
        //#region ====================== template 特有字段 ======================
        template_create_time: item.create_time,
        template_type: item.template_type,
        isTemplate: true,
        //#endregion
    }));
    exampleSceneList.value = samples
        .concat(templateSamples)
        .map((item) => ({ ...item, Icon: '/static/images/wave/ChatImg.png', BgColor: randomHexColor() }));
};
//随机生成颜色
const randomHexColor = () => {
    return `#${Math.floor(Math.random() * 16777215)
        .toString(16)
        .padEnd(6, '0')}`;
};
/**
 * 获取全局所有数据
 */
export const getAllData = async () => {
    getSceneGroupList();
    getSelectListSample();
};
 
//#endregion