Skip to content

Commit d02ee7b

Browse files
authored
fix: 修复新建对话时因缺少会话ID导致配置绑定失败的问题 (#5292)
* 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 0fceee0. * rm:删除个人用的文档文件 * rm:删除个人用的文档文件
1 parent dbeadb6 commit d02ee7b

File tree

4 files changed

+110
-7
lines changed

4 files changed

+110
-7
lines changed

dashboard/src/components/chat/ConfigSelector.vue

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ import { computed, onMounted, ref, watch } from 'vue';
7777
import axios from 'axios';
7878
import { useToast } from '@/utils/toast';
7979
import { useModuleI18n } from '@/i18n/composables';
80+
import {
81+
getStoredDashboardUsername,
82+
getStoredSelectedChatConfigId,
83+
setStoredSelectedChatConfigId
84+
} from '@/utils/chatConfigBinding';
8085
8186
interface ConfigInfo {
8287
id: string;
@@ -88,8 +93,6 @@ interface ConfigChangedPayload {
8893
agentRunnerType: string;
8994
}
9095
91-
const STORAGE_KEY = 'chat.selectedConfigId';
92-
9396
const props = withDefaults(defineProps<{
9497
sessionId?: string | null;
9598
platformId?: string;
@@ -128,7 +131,7 @@ const hasActiveSession = computed(() => !!normalizedSessionId.value);
128131
129132
const messageType = computed(() => (props.isGroup ? 'GroupMessage' : 'FriendMessage'));
130133
131-
const username = computed(() => localStorage.getItem('user') || 'guest');
134+
const username = computed(() => getStoredDashboardUsername());
132135
133136
const sessionKey = computed(() => {
134137
if (!normalizedSessionId.value) {
@@ -265,10 +268,10 @@ async function confirmSelection() {
265268
}
266269
const previousId = selectedConfigId.value;
267270
await setSelection(tempSelectedConfig.value);
268-
localStorage.setItem(STORAGE_KEY, tempSelectedConfig.value);
271+
setStoredSelectedChatConfigId(tempSelectedConfig.value);
269272
const applied = await applySelectionToBackend(tempSelectedConfig.value);
270273
if (!applied) {
271-
localStorage.setItem(STORAGE_KEY, previousId);
274+
setStoredSelectedChatConfigId(previousId);
272275
await setSelection(previousId);
273276
}
274277
dialog.value = false;
@@ -287,7 +290,7 @@ async function syncSelectionForSession() {
287290
await fetchRoutingEntries();
288291
const resolved = resolveConfigId(targetUmo.value);
289292
await setSelection(resolved);
290-
localStorage.setItem(STORAGE_KEY, resolved);
293+
setStoredSelectedChatConfigId(resolved);
291294
}
292295
293296
watch(
@@ -299,7 +302,7 @@ watch(
299302
300303
onMounted(async () => {
301304
await fetchConfigList();
302-
const stored = props.initialConfigId || localStorage.getItem(STORAGE_KEY) || 'default';
305+
const stored = props.initialConfigId || getStoredSelectedChatConfigId();
303306
selectedConfigId.value = stored;
304307
await setSelection(stored);
305308
await syncSelectionForSession();

dashboard/src/components/chat/StandaloneChat.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { useMessages } from '@/composables/useMessages';
7070
import { useMediaHandling } from '@/composables/useMediaHandling';
7171
import { useRecording } from '@/composables/useRecording';
7272
import { useToast } from '@/utils/toast';
73+
import { buildWebchatUmoDetails } from '@/utils/chatConfigBinding';
7374
7475
interface Props {
7576
configId?: string | null;
@@ -82,6 +83,7 @@ const props = withDefaults(defineProps<Props>(), {
8283
const { t } = useI18n();
8384
const { error: showError } = useToast();
8485
86+
8587
// UI 状态
8688
const imagePreviewDialog = ref(false);
8789
const previewImageUrl = ref('');
@@ -90,11 +92,33 @@ const previewImageUrl = ref('');
9092
const currSessionId = ref('');
9193
const getCurrentSession = computed(() => null); // 独立测试模式不需要会话信息
9294
95+
async function bindConfigToSession(sessionId: string) {
96+
const confId = (props.configId || '').trim();
97+
if (!confId || confId === 'default') {
98+
return;
99+
}
100+
101+
const umoDetails = buildWebchatUmoDetails(sessionId, false);
102+
103+
await axios.post('/api/config/umo_abconf_route/update', {
104+
umo: umoDetails.umo,
105+
conf_id: confId
106+
});
107+
}
108+
93109
async function newSession() {
94110
try {
95111
const response = await axios.get('/api/chat/new_session');
96112
const sessionId = response.data.data.session_id;
113+
114+
try {
115+
await bindConfigToSession(sessionId);
116+
} catch (err) {
117+
console.error('Failed to bind config to session', err);
118+
}
119+
97120
currSessionId.value = sessionId;
121+
98122
return sessionId;
99123
} catch (err) {
100124
console.error(err);

dashboard/src/composables/useSessions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ref, computed } from 'vue';
22
import axios from 'axios';
33
import { useRouter } from 'vue-router';
4+
import { buildWebchatUmoDetails, getStoredSelectedChatConfigId } from '@/utils/chatConfigBinding';
45

56
export interface Session {
67
session_id: string;
@@ -62,10 +63,25 @@ export function useSessions(chatboxMode: boolean = false) {
6263

6364
async function newSession() {
6465
try {
66+
const selectedConfigId = getStoredSelectedChatConfigId();
6567
const response = await axios.get('/api/chat/new_session');
6668
const sessionId = response.data.data.session_id;
69+
const platformId = response.data.data.platform_id;
70+
6771
currSessionId.value = sessionId;
6872

73+
if (selectedConfigId && selectedConfigId !== 'default' && platformId === 'webchat') {
74+
try {
75+
const umoDetails = buildWebchatUmoDetails(sessionId, false);
76+
await axios.post('/api/config/umo_abconf_route/update', {
77+
umo: umoDetails.umo,
78+
conf_id: selectedConfigId
79+
});
80+
} catch (err) {
81+
console.error('Failed to bind config to session', err);
82+
}
83+
}
84+
6985
// 更新 URL
7086
const basePath = chatboxMode ? '/chatbox' : '/chat';
7187
router.push(`${basePath}/${sessionId}`);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export const CHAT_SELECTED_CONFIG_STORAGE_KEY = 'chat.selectedConfigId';
2+
3+
export type ChatMessageType = 'FriendMessage' | 'GroupMessage';
4+
5+
export interface WebchatUmoDetails {
6+
platformId: string;
7+
messageType: ChatMessageType;
8+
username: string;
9+
sessionKey: string;
10+
umo: string;
11+
}
12+
13+
function getFromLocalStorage(key: string, fallback: string): string {
14+
try {
15+
if (typeof localStorage === 'undefined') {
16+
return fallback;
17+
}
18+
const value = localStorage.getItem(key);
19+
return value == null ? fallback : value;
20+
} catch {
21+
return fallback;
22+
}
23+
}
24+
25+
function setToLocalStorage(key: string, value: string): void {
26+
try {
27+
if (typeof localStorage === 'undefined') {
28+
return;
29+
}
30+
localStorage.setItem(key, value);
31+
} catch {
32+
// Ignore storage errors (e.g. private mode / restricted storage).
33+
}
34+
}
35+
36+
export function getStoredDashboardUsername(): string {
37+
return getFromLocalStorage('user', '').trim() || 'guest';
38+
}
39+
40+
export function getStoredSelectedChatConfigId(): string {
41+
return getFromLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, '').trim() || 'default';
42+
}
43+
44+
export function setStoredSelectedChatConfigId(configId: string): void {
45+
setToLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, configId);
46+
}
47+
48+
export function buildWebchatUmoDetails(sessionId: string, isGroup = false): WebchatUmoDetails {
49+
const platformId = 'webchat';
50+
const username = getStoredDashboardUsername();
51+
const messageType: ChatMessageType = isGroup ? 'GroupMessage' : 'FriendMessage';
52+
const sessionKey = `${platformId}!${username}!${sessionId}`;
53+
return {
54+
platformId,
55+
messageType,
56+
username,
57+
sessionKey,
58+
umo: `${platformId}:${messageType}:${sessionKey}`
59+
};
60+
}

0 commit comments

Comments
 (0)