d02ee7be8b
* fix:尝试修改
* fix:添加详细日志
* fix:进行详细修改,并添加日志
* fix:删除所有日志
* fix: 增加安全访问函数
- 给 localStorage 访问加了 try/catch + 可用性判断:dashboard/src/utils/chatConfigBinding.ts:13
- 新增 getFromLocalStorage/setToLocalStorage(在受限存储/无痕模式下异常时回退/忽略)
- getStoredDashboardUsername() / getStoredSelectedChatConfigId() 改为走安全读取:dashboard/src/utils/chatConfigBinding.ts:36 - 新增 setStoredSelectedChatConfigId(),写入失败静默忽略:dashboard/src/utils/chatConfigBinding.ts:44
- 把 ConfigSelector.vue 里直接 localStorage.getItem/setItem 全部替换为上述安全方法:dashboard/src/components/chat/ConfigSelector.vue:81
- 已重新跑过 pnpm run typecheck,通过。
* rm:删除个人用的文档文件
* Revert "rm:删除个人用的文档文件"
This reverts commit 0fceee0543.
* rm:删除个人用的文档文件
* rm:删除个人用的文档文件
176 lines
5.7 KiB
TypeScript
176 lines
5.7 KiB
TypeScript
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
|
|
};
|
|
}
|