Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions dashboard/src/components/chat/ConfigSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ import { computed, onMounted, ref, watch } from 'vue';
import axios from 'axios';
import { useToast } from '@/utils/toast';
import { useModuleI18n } from '@/i18n/composables';
import {
getStoredDashboardUsername,
getStoredSelectedChatConfigId,
setStoredSelectedChatConfigId
} from '@/utils/chatConfigBinding';

interface ConfigInfo {
id: string;
Expand All @@ -88,8 +93,6 @@ interface ConfigChangedPayload {
agentRunnerType: string;
}

const STORAGE_KEY = 'chat.selectedConfigId';

const props = withDefaults(defineProps<{
sessionId?: string | null;
platformId?: string;
Expand Down Expand Up @@ -128,7 +131,7 @@ const hasActiveSession = computed(() => !!normalizedSessionId.value);

const messageType = computed(() => (props.isGroup ? 'GroupMessage' : 'FriendMessage'));

const username = computed(() => localStorage.getItem('user') || 'guest');
const username = computed(() => getStoredDashboardUsername());

const sessionKey = computed(() => {
if (!normalizedSessionId.value) {
Expand Down Expand Up @@ -265,10 +268,10 @@ async function confirmSelection() {
}
const previousId = selectedConfigId.value;
await setSelection(tempSelectedConfig.value);
localStorage.setItem(STORAGE_KEY, tempSelectedConfig.value);
setStoredSelectedChatConfigId(tempSelectedConfig.value);
const applied = await applySelectionToBackend(tempSelectedConfig.value);
if (!applied) {
localStorage.setItem(STORAGE_KEY, previousId);
setStoredSelectedChatConfigId(previousId);
await setSelection(previousId);
}
dialog.value = false;
Expand All @@ -287,7 +290,7 @@ async function syncSelectionForSession() {
await fetchRoutingEntries();
const resolved = resolveConfigId(targetUmo.value);
await setSelection(resolved);
localStorage.setItem(STORAGE_KEY, resolved);
setStoredSelectedChatConfigId(resolved);
}

watch(
Expand All @@ -299,7 +302,7 @@ watch(

onMounted(async () => {
await fetchConfigList();
const stored = props.initialConfigId || localStorage.getItem(STORAGE_KEY) || 'default';
const stored = props.initialConfigId || getStoredSelectedChatConfigId();
selectedConfigId.value = stored;
await setSelection(stored);
await syncSelectionForSession();
Expand Down
24 changes: 24 additions & 0 deletions dashboard/src/components/chat/StandaloneChat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { useMessages } from '@/composables/useMessages';
import { useMediaHandling } from '@/composables/useMediaHandling';
import { useRecording } from '@/composables/useRecording';
import { useToast } from '@/utils/toast';
import { buildWebchatUmoDetails } from '@/utils/chatConfigBinding';

interface Props {
configId?: string | null;
Expand All @@ -82,6 +83,7 @@ const props = withDefaults(defineProps<Props>(), {
const { t } = useI18n();
const { error: showError } = useToast();


// UI 状态
const imagePreviewDialog = ref(false);
const previewImageUrl = ref('');
Expand All @@ -90,11 +92,33 @@ const previewImageUrl = ref('');
const currSessionId = ref('');
const getCurrentSession = computed(() => null); // 独立测试模式不需要会话信息

async function bindConfigToSession(sessionId: string) {
const confId = (props.configId || '').trim();
if (!confId || confId === 'default') {
return;
}

const umoDetails = buildWebchatUmoDetails(sessionId, false);

await axios.post('/api/config/umo_abconf_route/update', {
umo: umoDetails.umo,
conf_id: confId
});
}

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

try {
await bindConfigToSession(sessionId);
} catch (err) {
console.error('Failed to bind config to session', err);
}

currSessionId.value = sessionId;

return sessionId;
} catch (err) {
console.error(err);
Expand Down
16 changes: 16 additions & 0 deletions dashboard/src/composables/useSessions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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;
Expand Down Expand Up @@ -62,10 +63,25 @@ export function useSessions(chatboxMode: boolean = false) {

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}`);
Expand Down
60 changes: 60 additions & 0 deletions dashboard/src/utils/chatConfigBinding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export const CHAT_SELECTED_CONFIG_STORAGE_KEY = 'chat.selectedConfigId';

export type ChatMessageType = 'FriendMessage' | 'GroupMessage';

export interface WebchatUmoDetails {
platformId: string;
messageType: ChatMessageType;
username: string;
sessionKey: string;
umo: string;
}

function getFromLocalStorage(key: string, fallback: string): string {
try {
if (typeof localStorage === 'undefined') {
return fallback;
}
const value = localStorage.getItem(key);
return value == null ? fallback : value;
} catch {
return fallback;
}
}

function setToLocalStorage(key: string, value: string): void {
try {
if (typeof localStorage === 'undefined') {
return;
}
localStorage.setItem(key, value);
} catch {
// Ignore storage errors (e.g. private mode / restricted storage).
}
}

export function getStoredDashboardUsername(): string {
return getFromLocalStorage('user', '').trim() || 'guest';
}

export function getStoredSelectedChatConfigId(): string {
return getFromLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, '').trim() || 'default';
}

export function setStoredSelectedChatConfigId(configId: string): void {
setToLocalStorage(CHAT_SELECTED_CONFIG_STORAGE_KEY, configId);
}

export function buildWebchatUmoDetails(sessionId: string, isGroup = false): WebchatUmoDetails {
const platformId = 'webchat';
const username = getStoredDashboardUsername();
const messageType: ChatMessageType = isGroup ? 'GroupMessage' : 'FriendMessage';
const sessionKey = `${platformId}!${username}!${sessionId}`;
return {
platformId,
messageType,
username,
sessionKey,
umo: `${platformId}:${messageType}:${sessionKey}`
};
}