File size: 5,808 Bytes
8ede856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { ref, computed } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { buildWebchatUmoDetails, getStoredSelectedChatConfigId } from '@/utils/chatConfigBinding';

export interface Session {
    session_id: string;
    display_name: string | null;
    updated_at: string;
    platform_id: string;
    creator: string;
    is_group: number;
    created_at: string;
}

export function useSessions(chatboxMode: boolean = false) {
    const router = useRouter();
    const sessions = ref<Session[]>([]);
    const selectedSessions = ref<string[]>([]);
    const currSessionId = ref('');
    const pendingSessionId = ref<string | null>(null);

    // 编辑标题相关
    const editTitleDialog = ref(false);
    const editingTitle = ref('');
    const editingSessionId = ref('');

    const getCurrentSession = computed(() => {
        if (!currSessionId.value) return null;
        return sessions.value.find(s => s.session_id === currSessionId.value);
    });

    async function getSessions() {
        try {
            const response = await axios.get('/api/chat/sessions');
            sessions.value = response.data.data;

            // 处理待加载的会话
            if (pendingSessionId.value) {
                const session = sessions.value.find(s => s.session_id === pendingSessionId.value);
                if (session) {
                    selectedSessions.value = [pendingSessionId.value];
                    pendingSessionId.value = null;
                }
            } else if (currSessionId.value) {
                // 如果当前有选中的会话,确保它在列表中并被选中
                const session = sessions.value.find(s => s.session_id === currSessionId.value);
                if (session) {
                    selectedSessions.value = [currSessionId.value];
                }
            } else if (sessions.value.length > 0) {
                // 默认选择第一个会话
                const firstSession = sessions.value[0];
                selectedSessions.value = [firstSession.session_id];
            }
        } catch (err: any) {
            if (err.response?.status === 401) {
                router.push('/auth/login?redirect=/chatbox');
            }
            console.error(err);
        }
    }

    async function newSession() {
        try {
            const selectedConfigId = getStoredSelectedChatConfigId();
            const response = await axios.get('/api/chat/new_session');
            const sessionId = response.data.data.session_id;
            const platformId = response.data.data.platform_id;

            currSessionId.value = sessionId;

            if (selectedConfigId && selectedConfigId !== 'default' && platformId === 'webchat') {
                try {
                    const umoDetails = buildWebchatUmoDetails(sessionId, false);
                    await axios.post('/api/config/umo_abconf_route/update', {
                        umo: umoDetails.umo,
                        conf_id: selectedConfigId
                    });
                } catch (err) {
                    console.error('Failed to bind config to session', err);
                }
            }

            // 更新 URL
            const basePath = chatboxMode ? '/chatbox' : '/chat';
            router.push(`${basePath}/${sessionId}`);
            
            await getSessions();
            
            // 确保新创建的会话被选中高亮
            selectedSessions.value = [sessionId];
            
            return sessionId;
        } catch (err) {
            console.error(err);
            throw err;
        }
    }

    async function deleteSession(sessionId: string) {
        try {
            await axios.get('/api/chat/delete_session?session_id=' + sessionId);
            await getSessions();
            currSessionId.value = '';
            selectedSessions.value = [];
        } catch (err) {
            console.error(err);
        }
    }

    function showEditTitleDialog(sessionId: string, title: string) {
        editingSessionId.value = sessionId;
        editingTitle.value = title || '';
        editTitleDialog.value = true;
    }

    async function saveTitle() {
        if (!editingSessionId.value) return;

        const trimmedTitle = editingTitle.value.trim();
        try {
            await axios.post('/api/chat/update_session_display_name', {
                session_id: editingSessionId.value,
                display_name: trimmedTitle
            });

            // 更新本地会话标题
            const session = sessions.value.find(s => s.session_id === editingSessionId.value);
            if (session) {
                session.display_name = trimmedTitle;
            }
            editTitleDialog.value = false;
        } catch (err) {
            console.error('重命名会话失败:', err);
        }
    }

    function updateSessionTitle(sessionId: string, title: string) {
        const session = sessions.value.find(s => s.session_id === sessionId);
        if (session) {
            session.display_name = title;
        }
    }

    function newChat(closeMobileSidebar?: () => void) {
        currSessionId.value = '';
        selectedSessions.value = [];
        
        const basePath = chatboxMode ? '/chatbox' : '/chat';
        router.push(basePath);
        
        if (closeMobileSidebar) {
            closeMobileSidebar();
        }
    }

    return {
        sessions,
        selectedSessions,
        currSessionId,
        pendingSessionId,
        editTitleDialog,
        editingTitle,
        editingSessionId,
        getCurrentSession,
        getSessions,
        newSession,
        deleteSession,
        showEditTitleDialog,
        saveTitle,
        updateSessionTitle,
        newChat
    };
}